Fossil SCM

Merge latest changes from trunk

ashish 2012-01-15 18:06 UTC ashish-ipv6 merge
Commit b3130baa06b9d716f325ea548459bb951b864b2b
+1 -1
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1
-1.20
1
+1.21
22
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 1.20
2
--- VERSION
+++ VERSION
@@ -1,1 +1,1 @@
1 1.21
2
--- ajax/js/whajaj.js
+++ ajax/js/whajaj.js
@@ -103,11 +103,11 @@
103103
like PHP does.
104104
105105
*/
106106
WhAjaj.processUrlArgs = function(str) {
107107
if( 0 === arguments.length ) {
108
- if( (undefined === typeof window) ||
108
+ if( ('undefined' === typeof window) ||
109109
!window.location ||
110110
!window.location.search ) return false;
111111
else str = (''+window.location.search).substring(1);
112112
}
113113
if( ! str ) return false;
114114
--- ajax/js/whajaj.js
+++ ajax/js/whajaj.js
@@ -103,11 +103,11 @@
103 like PHP does.
104
105 */
106 WhAjaj.processUrlArgs = function(str) {
107 if( 0 === arguments.length ) {
108 if( (undefined === typeof window) ||
109 !window.location ||
110 !window.location.search ) return false;
111 else str = (''+window.location.search).substring(1);
112 }
113 if( ! str ) return false;
114
--- ajax/js/whajaj.js
+++ ajax/js/whajaj.js
@@ -103,11 +103,11 @@
103 like PHP does.
104
105 */
106 WhAjaj.processUrlArgs = function(str) {
107 if( 0 === arguments.length ) {
108 if( ('undefined' === typeof window) ||
109 !window.location ||
110 !window.location.search ) return false;
111 else str = (''+window.location.search).substring(1);
112 }
113 if( ! str ) return false;
114
+25 -19
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
9797
user-error "zlib not found please install it or specify the location with --with-zlib"
9898
}
9999
100100
set tclpath [opt-val with-tcl]
101101
if {$tclpath ne ""} {
102
- if {$tclpath ne "1"} {
103
- cc-with [list -cflags [list -I$tclpath/include -L$tclpath/lib]]
104
- }
105
- if {![cc-check-includes tcl.h]} {
106
- user-error "Cannot find tcl.h"
107
- }
108
- foreach tlib {tcl8.6 tcl8.5 tcl notfound} {
109
- if {$tlib=="notfound"} {
110
- user-error "Cannot find a usable libtcl"
111
- }
112
- if {[cc-check-function-in-lib Tcl_CreateInterp $tlib]} {
113
- define-append LIBS -l$tlib
114
- break
115
- }
116
- }
102
+ # Note parse-tclconfig-sh is in autosetup/local.tcl
103
+ if {$tclpath eq "1"} {
104
+ # Use the system Tcl. Look in some likely places.
105
+ array set tclconfig [parse-tclconfig-sh /usr /usr/local /usr/share /opt/local]
106
+ set msg "on your system"
107
+ } else {
108
+ array set tclconfig [parse-tclconfig-sh $tclpath]
109
+ set msg "at $tclpath"
110
+ }
111
+ if {![info exists tclconfig(TCL_INCLUDE_SPEC)]} {
112
+ user-error "Cannot find Tcl $msg"
113
+ }
114
+ set cflags $tclconfig(TCL_INCLUDE_SPEC)
115
+ set libs "$tclconfig(TCL_LIB_SPEC) $tclconfig(TCL_LIBS)"
116
+ cc-with [list -cflags $cflags -libs $libs] {
117
+ if {![cc-check-functions Tcl_CreateInterp]} {
118
+ user-error "Cannot find a usable Tcl $msg"
119
+ }
120
+ }
121
+ set version $tclconfig(TCL_VERSION)$tclconfig(TCL_PATCH_LEVEL)
122
+ msg-result "Found Tcl $version at $tclconfig(TCL_PREFIX)"
123
+ define-append LIBS $libs
124
+ define-append EXTRA_CFLAGS $cflags
125
+ define-append EXTRA_LDFLAGS $tclconfig(TCL_LD_FLAGS)
126
+
117127
define FOSSIL_ENABLE_TCL
118
- if {$tclpath ne "1"} {
119
- define-append EXTRA_CFLAGS -I$tclpath/include
120
- define-append EXTRA_LDFLAGS -L$tclpath/lib
121
- }
122128
}
123129
124130
# Helper for openssl checking
125131
proc check-for-openssl {msg {cflags {}}} {
126132
msg-checking "Checking for $msg..."
127133
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
97 user-error "zlib not found please install it or specify the location with --with-zlib"
98 }
99
100 set tclpath [opt-val with-tcl]
101 if {$tclpath ne ""} {
102 if {$tclpath ne "1"} {
103 cc-with [list -cflags [list -I$tclpath/include -L$tclpath/lib]]
104 }
105 if {![cc-check-includes tcl.h]} {
106 user-error "Cannot find tcl.h"
107 }
108 foreach tlib {tcl8.6 tcl8.5 tcl notfound} {
109 if {$tlib=="notfound"} {
110 user-error "Cannot find a usable libtcl"
111 }
112 if {[cc-check-function-in-lib Tcl_CreateInterp $tlib]} {
113 define-append LIBS -l$tlib
114 break
115 }
116 }
 
 
 
 
 
 
 
 
 
 
117 define FOSSIL_ENABLE_TCL
118 if {$tclpath ne "1"} {
119 define-append EXTRA_CFLAGS -I$tclpath/include
120 define-append EXTRA_LDFLAGS -L$tclpath/lib
121 }
122 }
123
124 # Helper for openssl checking
125 proc check-for-openssl {msg {cflags {}}} {
126 msg-checking "Checking for $msg..."
127
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
97 user-error "zlib not found please install it or specify the location with --with-zlib"
98 }
99
100 set tclpath [opt-val with-tcl]
101 if {$tclpath ne ""} {
102 # Note parse-tclconfig-sh is in autosetup/local.tcl
103 if {$tclpath eq "1"} {
104 # Use the system Tcl. Look in some likely places.
105 array set tclconfig [parse-tclconfig-sh /usr /usr/local /usr/share /opt/local]
106 set msg "on your system"
107 } else {
108 array set tclconfig [parse-tclconfig-sh $tclpath]
109 set msg "at $tclpath"
110 }
111 if {![info exists tclconfig(TCL_INCLUDE_SPEC)]} {
112 user-error "Cannot find Tcl $msg"
113 }
114 set cflags $tclconfig(TCL_INCLUDE_SPEC)
115 set libs "$tclconfig(TCL_LIB_SPEC) $tclconfig(TCL_LIBS)"
116 cc-with [list -cflags $cflags -libs $libs] {
117 if {![cc-check-functions Tcl_CreateInterp]} {
118 user-error "Cannot find a usable Tcl $msg"
119 }
120 }
121 set version $tclconfig(TCL_VERSION)$tclconfig(TCL_PATCH_LEVEL)
122 msg-result "Found Tcl $version at $tclconfig(TCL_PREFIX)"
123 define-append LIBS $libs
124 define-append EXTRA_CFLAGS $cflags
125 define-append EXTRA_LDFLAGS $tclconfig(TCL_LD_FLAGS)
126
127 define FOSSIL_ENABLE_TCL
 
 
 
 
128 }
129
130 # Helper for openssl checking
131 proc check-for-openssl {msg {cflags {}}} {
132 msg-checking "Checking for $msg..."
133
+25 -19
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
9797
user-error "zlib not found please install it or specify the location with --with-zlib"
9898
}
9999
100100
set tclpath [opt-val with-tcl]
101101
if {$tclpath ne ""} {
102
- if {$tclpath ne "1"} {
103
- cc-with [list -cflags [list -I$tclpath/include -L$tclpath/lib]]
104
- }
105
- if {![cc-check-includes tcl.h]} {
106
- user-error "Cannot find tcl.h"
107
- }
108
- foreach tlib {tcl8.6 tcl8.5 tcl notfound} {
109
- if {$tlib=="notfound"} {
110
- user-error "Cannot find a usable libtcl"
111
- }
112
- if {[cc-check-function-in-lib Tcl_CreateInterp $tlib]} {
113
- define-append LIBS -l$tlib
114
- break
115
- }
116
- }
102
+ # Note parse-tclconfig-sh is in autosetup/local.tcl
103
+ if {$tclpath eq "1"} {
104
+ # Use the system Tcl. Look in some likely places.
105
+ array set tclconfig [parse-tclconfig-sh /usr /usr/local /usr/share /opt/local]
106
+ set msg "on your system"
107
+ } else {
108
+ array set tclconfig [parse-tclconfig-sh $tclpath]
109
+ set msg "at $tclpath"
110
+ }
111
+ if {![info exists tclconfig(TCL_INCLUDE_SPEC)]} {
112
+ user-error "Cannot find Tcl $msg"
113
+ }
114
+ set cflags $tclconfig(TCL_INCLUDE_SPEC)
115
+ set libs "$tclconfig(TCL_LIB_SPEC) $tclconfig(TCL_LIBS)"
116
+ cc-with [list -cflags $cflags -libs $libs] {
117
+ if {![cc-check-functions Tcl_CreateInterp]} {
118
+ user-error "Cannot find a usable Tcl $msg"
119
+ }
120
+ }
121
+ set version $tclconfig(TCL_VERSION)$tclconfig(TCL_PATCH_LEVEL)
122
+ msg-result "Found Tcl $version at $tclconfig(TCL_PREFIX)"
123
+ define-append LIBS $libs
124
+ define-append EXTRA_CFLAGS $cflags
125
+ define-append EXTRA_LDFLAGS $tclconfig(TCL_LD_FLAGS)
126
+
117127
define FOSSIL_ENABLE_TCL
118
- if {$tclpath ne "1"} {
119
- define-append EXTRA_CFLAGS -I$tclpath/include
120
- define-append EXTRA_LDFLAGS -L$tclpath/lib
121
- }
122128
}
123129
124130
# Helper for openssl checking
125131
proc check-for-openssl {msg {cflags {}}} {
126132
msg-checking "Checking for $msg..."
127133
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
97 user-error "zlib not found please install it or specify the location with --with-zlib"
98 }
99
100 set tclpath [opt-val with-tcl]
101 if {$tclpath ne ""} {
102 if {$tclpath ne "1"} {
103 cc-with [list -cflags [list -I$tclpath/include -L$tclpath/lib]]
104 }
105 if {![cc-check-includes tcl.h]} {
106 user-error "Cannot find tcl.h"
107 }
108 foreach tlib {tcl8.6 tcl8.5 tcl notfound} {
109 if {$tlib=="notfound"} {
110 user-error "Cannot find a usable libtcl"
111 }
112 if {[cc-check-function-in-lib Tcl_CreateInterp $tlib]} {
113 define-append LIBS -l$tlib
114 break
115 }
116 }
 
 
 
 
 
 
 
 
 
 
117 define FOSSIL_ENABLE_TCL
118 if {$tclpath ne "1"} {
119 define-append EXTRA_CFLAGS -I$tclpath/include
120 define-append EXTRA_LDFLAGS -L$tclpath/lib
121 }
122 }
123
124 # Helper for openssl checking
125 proc check-for-openssl {msg {cflags {}}} {
126 msg-checking "Checking for $msg..."
127
--- auto.def
+++ auto.def
@@ -97,30 +97,36 @@
97 user-error "zlib not found please install it or specify the location with --with-zlib"
98 }
99
100 set tclpath [opt-val with-tcl]
101 if {$tclpath ne ""} {
102 # Note parse-tclconfig-sh is in autosetup/local.tcl
103 if {$tclpath eq "1"} {
104 # Use the system Tcl. Look in some likely places.
105 array set tclconfig [parse-tclconfig-sh /usr /usr/local /usr/share /opt/local]
106 set msg "on your system"
107 } else {
108 array set tclconfig [parse-tclconfig-sh $tclpath]
109 set msg "at $tclpath"
110 }
111 if {![info exists tclconfig(TCL_INCLUDE_SPEC)]} {
112 user-error "Cannot find Tcl $msg"
113 }
114 set cflags $tclconfig(TCL_INCLUDE_SPEC)
115 set libs "$tclconfig(TCL_LIB_SPEC) $tclconfig(TCL_LIBS)"
116 cc-with [list -cflags $cflags -libs $libs] {
117 if {![cc-check-functions Tcl_CreateInterp]} {
118 user-error "Cannot find a usable Tcl $msg"
119 }
120 }
121 set version $tclconfig(TCL_VERSION)$tclconfig(TCL_PATCH_LEVEL)
122 msg-result "Found Tcl $version at $tclconfig(TCL_PREFIX)"
123 define-append LIBS $libs
124 define-append EXTRA_CFLAGS $cflags
125 define-append EXTRA_LDFLAGS $tclconfig(TCL_LD_FLAGS)
126
127 define FOSSIL_ENABLE_TCL
 
 
 
 
128 }
129
130 # Helper for openssl checking
131 proc check-for-openssl {msg {cflags {}}} {
132 msg-checking "Checking for $msg..."
133
--- autosetup/local.tcl
+++ autosetup/local.tcl
@@ -1,2 +1,29 @@
11
# For this project, disable the pager for --help
22
set useropts(nopager) 1
3
+
4
+# Searches for a usable Tcl (prefer 8.6, 8.5, 8.4) in the given paths
5
+# Returns a dictionary of the contents of the tclConfig.sh file, or
6
+# empty if not found
7
+proc parse-tclconfig-sh {args} {
8
+ foreach p $args {
9
+ # Allow pointing directly to the path containing tclConfig.sh
10
+ if {[file exists $p/tclConfig.sh]} {
11
+ return [parse-tclconfig-sh-file $p/tclConfig.sh]
12
+ }
13
+ # Some systems allow for multiple versions
14
+ foreach libpath {lib/tcl8.6 lib/tcl8.5 lib/tcl8.4 lib/tcl tcl lib} {
15
+ if {[file exists $p/$libpath/tclConfig.sh]} {
16
+ return [parse-tclconfig-sh-file $p/$libpath/tclConfig.sh]
17
+ }
18
+ }
19
+ }
20
+}
21
+
22
+proc parse-tclconfig-sh-file {filename} {
23
+ foreach line [split [readfile $filename] \n] {
24
+ if {[regexp {^(TCL_[^=]*)=(.*)$} $line -> name value]} {
25
+ set tclconfig($name) [string trim $value ']
26
+ }
27
+ }
28
+ return [array get tclconfig]
29
+}
330
--- autosetup/local.tcl
+++ autosetup/local.tcl
@@ -1,2 +1,29 @@
1 # For this project, disable the pager for --help
2 set useropts(nopager) 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
--- autosetup/local.tcl
+++ autosetup/local.tcl
@@ -1,2 +1,29 @@
1 # For this project, disable the pager for --help
2 set useropts(nopager) 1
3
4 # Searches for a usable Tcl (prefer 8.6, 8.5, 8.4) in the given paths
5 # Returns a dictionary of the contents of the tclConfig.sh file, or
6 # empty if not found
7 proc parse-tclconfig-sh {args} {
8 foreach p $args {
9 # Allow pointing directly to the path containing tclConfig.sh
10 if {[file exists $p/tclConfig.sh]} {
11 return [parse-tclconfig-sh-file $p/tclConfig.sh]
12 }
13 # Some systems allow for multiple versions
14 foreach libpath {lib/tcl8.6 lib/tcl8.5 lib/tcl8.4 lib/tcl tcl lib} {
15 if {[file exists $p/$libpath/tclConfig.sh]} {
16 return [parse-tclconfig-sh-file $p/$libpath/tclConfig.sh]
17 }
18 }
19 }
20 }
21
22 proc parse-tclconfig-sh-file {filename} {
23 foreach line [split [readfile $filename] \n] {
24 if {[regexp {^(TCL_[^=]*)=(.*)$} $line -> name value]} {
25 set tclconfig($name) [string trim $value ']
26 }
27 }
28 return [array get tclconfig]
29 }
30
+24 -10
--- src/add.c
+++ src/add.c
@@ -245,13 +245,12 @@
245245
}else if( file_access(zName, R_OK) ){
246246
fossil_fatal("cannot open %s", zName);
247247
}else{
248248
char *zTreeName = &zName[nRoot];
249249
db_multi_exec(
250
- "INSERT OR IGNORE INTO sfile(x)"
251
- " SELECT %Q WHERE NOT EXISTS(SELECT 1 FROM vfile WHERE pathname=%Q)",
252
- zTreeName, zTreeName
250
+ "INSERT OR IGNORE INTO sfile(x) VALUES(%Q)",
251
+ zTreeName
253252
);
254253
}
255254
blob_reset(&fullName);
256255
}
257256
glob_free(pIgnore);
@@ -337,24 +336,39 @@
337336
**
338337
** The --case-sensitive BOOLEAN command-line option overrides any
339338
** setting.
340339
*/
341340
int filenames_are_case_sensitive(void){
342
- int caseSensitive;
341
+ static int caseSensitive;
342
+ static int once = 1;
343343
344
- if( zCaseSensitive ){
345
- caseSensitive = is_truth(zCaseSensitive);
346
- }else{
344
+ if( once ){
345
+ once = 0;
346
+ if( zCaseSensitive ){
347
+ caseSensitive = is_truth(zCaseSensitive);
348
+ }else{
347349
#if !defined(_WIN32) && !defined(__DARWIN__) && !defined(__APPLE__)
348
- caseSensitive = 1;
350
+ caseSensitive = 1; /* Unix */
349351
#else
350
- caseSensitive = 0;
352
+ caseSensitive = 0; /* Windows and Mac */
351353
#endif
352
- caseSensitive = db_get_boolean("case-sensitive",caseSensitive);
354
+ caseSensitive = db_get_boolean("case-sensitive",caseSensitive);
355
+ }
353356
}
354357
return caseSensitive;
355358
}
359
+
360
+/*
361
+** Return one of two things:
362
+**
363
+** "" (empty string) if filenames are case sensitive
364
+**
365
+** "COLLATE nocase" if filenames are not case sensitive.
366
+*/
367
+const char *filename_collation(void){
368
+ return filenames_are_case_sensitive() ? "" : "COLLATE nocase";
369
+}
356370
357371
/*
358372
** COMMAND: addremove
359373
**
360374
** Usage: %fossil addremove ?OPTIONS?
361375
--- src/add.c
+++ src/add.c
@@ -245,13 +245,12 @@
245 }else if( file_access(zName, R_OK) ){
246 fossil_fatal("cannot open %s", zName);
247 }else{
248 char *zTreeName = &zName[nRoot];
249 db_multi_exec(
250 "INSERT OR IGNORE INTO sfile(x)"
251 " SELECT %Q WHERE NOT EXISTS(SELECT 1 FROM vfile WHERE pathname=%Q)",
252 zTreeName, zTreeName
253 );
254 }
255 blob_reset(&fullName);
256 }
257 glob_free(pIgnore);
@@ -337,24 +336,39 @@
337 **
338 ** The --case-sensitive BOOLEAN command-line option overrides any
339 ** setting.
340 */
341 int filenames_are_case_sensitive(void){
342 int caseSensitive;
 
343
344 if( zCaseSensitive ){
345 caseSensitive = is_truth(zCaseSensitive);
346 }else{
 
 
347 #if !defined(_WIN32) && !defined(__DARWIN__) && !defined(__APPLE__)
348 caseSensitive = 1;
349 #else
350 caseSensitive = 0;
351 #endif
352 caseSensitive = db_get_boolean("case-sensitive",caseSensitive);
 
353 }
354 return caseSensitive;
355 }
 
 
 
 
 
 
 
 
 
 
 
356
357 /*
358 ** COMMAND: addremove
359 **
360 ** Usage: %fossil addremove ?OPTIONS?
361
--- src/add.c
+++ src/add.c
@@ -245,13 +245,12 @@
245 }else if( file_access(zName, R_OK) ){
246 fossil_fatal("cannot open %s", zName);
247 }else{
248 char *zTreeName = &zName[nRoot];
249 db_multi_exec(
250 "INSERT OR IGNORE INTO sfile(x) VALUES(%Q)",
251 zTreeName
 
252 );
253 }
254 blob_reset(&fullName);
255 }
256 glob_free(pIgnore);
@@ -337,24 +336,39 @@
336 **
337 ** The --case-sensitive BOOLEAN command-line option overrides any
338 ** setting.
339 */
340 int filenames_are_case_sensitive(void){
341 static int caseSensitive;
342 static int once = 1;
343
344 if( once ){
345 once = 0;
346 if( zCaseSensitive ){
347 caseSensitive = is_truth(zCaseSensitive);
348 }else{
349 #if !defined(_WIN32) && !defined(__DARWIN__) && !defined(__APPLE__)
350 caseSensitive = 1; /* Unix */
351 #else
352 caseSensitive = 0; /* Windows and Mac */
353 #endif
354 caseSensitive = db_get_boolean("case-sensitive",caseSensitive);
355 }
356 }
357 return caseSensitive;
358 }
359
360 /*
361 ** Return one of two things:
362 **
363 ** "" (empty string) if filenames are case sensitive
364 **
365 ** "COLLATE nocase" if filenames are not case sensitive.
366 */
367 const char *filename_collation(void){
368 return filenames_are_case_sensitive() ? "" : "COLLATE nocase";
369 }
370
371 /*
372 ** COMMAND: addremove
373 **
374 ** Usage: %fossil addremove ?OPTIONS?
375
+17 -7
--- src/browse.c
+++ src/browse.c
@@ -196,11 +196,12 @@
196196
** Subdirectory names begin with "/". This causes them to sort
197197
** first and it also gives us an easy way to distinguish files
198198
** from directories in the loop that follows.
199199
*/
200200
db_multi_exec(
201
- "CREATE TEMP TABLE localfiles(x UNIQUE NOT NULL, u);"
201
+ "CREATE TEMP TABLE localfiles(x UNIQUE NOT NULL %s, u);",
202
+ filename_collation()
202203
);
203204
if( zCI ){
204205
Stmt ins;
205206
ManifestFile *pFile;
206207
ManifestFile *pPrev = 0;
@@ -231,16 +232,25 @@
231232
for(nPrev=0; (c=pPrev->zName[nD+nPrev]) && c!='/'; nPrev++){}
232233
if( c=='/' ) nPrev++;
233234
}
234235
db_finalize(&ins);
235236
}else if( zD ){
236
- db_multi_exec(
237
- "INSERT OR IGNORE INTO localfiles"
238
- " SELECT pathelement(name,%d), NULL FROM filename"
239
- " WHERE name GLOB '%q/*'",
240
- nD, zD
241
- );
237
+ if( filenames_are_case_sensitive() ){
238
+ db_multi_exec(
239
+ "INSERT OR IGNORE INTO localfiles"
240
+ " SELECT pathelement(name,%d), NULL FROM filename"
241
+ " WHERE name GLOB '%q/*'",
242
+ nD, zD
243
+ );
244
+ }else{
245
+ db_multi_exec(
246
+ "INSERT OR IGNORE INTO localfiles"
247
+ " SELECT pathelement(name,%d), NULL FROM filename"
248
+ " WHERE name LIKE '%q/%%'",
249
+ nD, zD
250
+ );
251
+ }
242252
}else{
243253
db_multi_exec(
244254
"INSERT OR IGNORE INTO localfiles"
245255
" SELECT pathelement(name,0), NULL FROM filename"
246256
);
247257
--- src/browse.c
+++ src/browse.c
@@ -196,11 +196,12 @@
196 ** Subdirectory names begin with "/". This causes them to sort
197 ** first and it also gives us an easy way to distinguish files
198 ** from directories in the loop that follows.
199 */
200 db_multi_exec(
201 "CREATE TEMP TABLE localfiles(x UNIQUE NOT NULL, u);"
 
202 );
203 if( zCI ){
204 Stmt ins;
205 ManifestFile *pFile;
206 ManifestFile *pPrev = 0;
@@ -231,16 +232,25 @@
231 for(nPrev=0; (c=pPrev->zName[nD+nPrev]) && c!='/'; nPrev++){}
232 if( c=='/' ) nPrev++;
233 }
234 db_finalize(&ins);
235 }else if( zD ){
236 db_multi_exec(
237 "INSERT OR IGNORE INTO localfiles"
238 " SELECT pathelement(name,%d), NULL FROM filename"
239 " WHERE name GLOB '%q/*'",
240 nD, zD
241 );
 
 
 
 
 
 
 
 
 
242 }else{
243 db_multi_exec(
244 "INSERT OR IGNORE INTO localfiles"
245 " SELECT pathelement(name,0), NULL FROM filename"
246 );
247
--- src/browse.c
+++ src/browse.c
@@ -196,11 +196,12 @@
196 ** Subdirectory names begin with "/". This causes them to sort
197 ** first and it also gives us an easy way to distinguish files
198 ** from directories in the loop that follows.
199 */
200 db_multi_exec(
201 "CREATE TEMP TABLE localfiles(x UNIQUE NOT NULL %s, u);",
202 filename_collation()
203 );
204 if( zCI ){
205 Stmt ins;
206 ManifestFile *pFile;
207 ManifestFile *pPrev = 0;
@@ -231,16 +232,25 @@
232 for(nPrev=0; (c=pPrev->zName[nD+nPrev]) && c!='/'; nPrev++){}
233 if( c=='/' ) nPrev++;
234 }
235 db_finalize(&ins);
236 }else if( zD ){
237 if( filenames_are_case_sensitive() ){
238 db_multi_exec(
239 "INSERT OR IGNORE INTO localfiles"
240 " SELECT pathelement(name,%d), NULL FROM filename"
241 " WHERE name GLOB '%q/*'",
242 nD, zD
243 );
244 }else{
245 db_multi_exec(
246 "INSERT OR IGNORE INTO localfiles"
247 " SELECT pathelement(name,%d), NULL FROM filename"
248 " WHERE name LIKE '%q/%%'",
249 nD, zD
250 );
251 }
252 }else{
253 db_multi_exec(
254 "INSERT OR IGNORE INTO localfiles"
255 " SELECT pathelement(name,0), NULL FROM filename"
256 );
257
+4 -1
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411411
** deallocated after this routine returns.
412412
*/
413413
void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414414
if( nAllocQP<=nUsedQP ){
415415
nAllocQP = nAllocQP*2 + 10;
416
+ if( nAllocQP>1000 ){
417
+ /* Prevent a DOS service attack against the framework */
418
+ fossil_fatal("Too many query parameters");
419
+ }
416420
aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
417421
}
418422
aParamQP[nUsedQP].zName = zName;
419423
aParamQP[nUsedQP].zValue = zValue;
420424
if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
11781182
** with leftmost address corresponding to the client
11791183
*/
11801184
while(*p && *p != ',') p++;
11811185
*p = '\0';
11821186
zIpAddr = mprintf( "%s", zVal );
1183
- }
11841187
#if 0
11851188
}else if( fossil_strcmp(zFieldName,"referer:")==0 ){
11861189
cgi_setenv("HTTP_REFERER", zVal);
11871190
#endif
11881191
}else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
11891192
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411 ** deallocated after this routine returns.
412 */
413 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414 if( nAllocQP<=nUsedQP ){
415 nAllocQP = nAllocQP*2 + 10;
 
 
 
 
416 aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
417 }
418 aParamQP[nUsedQP].zName = zName;
419 aParamQP[nUsedQP].zValue = zValue;
420 if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
1178 ** with leftmost address corresponding to the client
1179 */
1180 while(*p && *p != ',') p++;
1181 *p = '\0';
1182 zIpAddr = mprintf( "%s", zVal );
1183 }
1184 #if 0
1185 }else if( fossil_strcmp(zFieldName,"referer:")==0 ){
1186 cgi_setenv("HTTP_REFERER", zVal);
1187 #endif
1188 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1189
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411 ** deallocated after this routine returns.
412 */
413 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414 if( nAllocQP<=nUsedQP ){
415 nAllocQP = nAllocQP*2 + 10;
416 if( nAllocQP>1000 ){
417 /* Prevent a DOS service attack against the framework */
418 fossil_fatal("Too many query parameters");
419 }
420 aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
421 }
422 aParamQP[nUsedQP].zName = zName;
423 aParamQP[nUsedQP].zValue = zValue;
424 if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
1182 ** with leftmost address corresponding to the client
1183 */
1184 while(*p && *p != ',') p++;
1185 *p = '\0';
1186 zIpAddr = mprintf( "%s", zVal );
 
1187 #if 0
1188 }else if( fossil_strcmp(zFieldName,"referer:")==0 ){
1189 cgi_setenv("HTTP_REFERER", zVal);
1190 #endif
1191 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1192
+4 -1
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411411
** deallocated after this routine returns.
412412
*/
413413
void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414414
if( nAllocQP<=nUsedQP ){
415415
nAllocQP = nAllocQP*2 + 10;
416
+ if( nAllocQP>1000 ){
417
+ /* Prevent a DOS service attack against the framework */
418
+ fossil_fatal("Too many query parameters");
419
+ }
416420
aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
417421
}
418422
aParamQP[nUsedQP].zName = zName;
419423
aParamQP[nUsedQP].zValue = zValue;
420424
if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
11781182
** with leftmost address corresponding to the client
11791183
*/
11801184
while(*p && *p != ',') p++;
11811185
*p = '\0';
11821186
zIpAddr = mprintf( "%s", zVal );
1183
- }
11841187
#if 0
11851188
}else if( fossil_strcmp(zFieldName,"referer:")==0 ){
11861189
cgi_setenv("HTTP_REFERER", zVal);
11871190
#endif
11881191
}else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
11891192
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411 ** deallocated after this routine returns.
412 */
413 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414 if( nAllocQP<=nUsedQP ){
415 nAllocQP = nAllocQP*2 + 10;
 
 
 
 
416 aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
417 }
418 aParamQP[nUsedQP].zName = zName;
419 aParamQP[nUsedQP].zValue = zValue;
420 if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
1178 ** with leftmost address corresponding to the client
1179 */
1180 while(*p && *p != ',') p++;
1181 *p = '\0';
1182 zIpAddr = mprintf( "%s", zVal );
1183 }
1184 #if 0
1185 }else if( fossil_strcmp(zFieldName,"referer:")==0 ){
1186 cgi_setenv("HTTP_REFERER", zVal);
1187 #endif
1188 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1189
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,14 @@
411 ** deallocated after this routine returns.
412 */
413 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
414 if( nAllocQP<=nUsedQP ){
415 nAllocQP = nAllocQP*2 + 10;
416 if( nAllocQP>1000 ){
417 /* Prevent a DOS service attack against the framework */
418 fossil_fatal("Too many query parameters");
419 }
420 aParamQP = fossil_realloc( aParamQP, nAllocQP*sizeof(aParamQP[0]) );
421 }
422 aParamQP[nUsedQP].zName = zName;
423 aParamQP[nUsedQP].zValue = zValue;
424 if( g.fHttpTrace ){
@@ -1178,11 +1182,10 @@
1182 ** with leftmost address corresponding to the client
1183 */
1184 while(*p && *p != ',') p++;
1185 *p = '\0';
1186 zIpAddr = mprintf( "%s", zVal );
 
1187 #if 0
1188 }else if( fossil_strcmp(zFieldName,"referer:")==0 ){
1189 cgi_setenv("HTTP_REFERER", zVal);
1190 #endif
1191 }else if( fossil_strcmp(zFieldName,"user-agent:")==0 ){
1192
+22 -1
--- src/checkin.c
+++ src/checkin.c
@@ -288,11 +288,12 @@
288288
Blob rewrittenPathname;
289289
const char *zPathname, *zDisplayName;
290290
291291
db_must_be_within_tree();
292292
cwdRelative = determine_cwd_relative_option();
293
- db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
293
+ db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
294
+ filename_collation());
294295
n = strlen(g.zLocalRoot);
295296
blob_init(&path, g.zLocalRoot, n-1);
296297
if( zIgnoreFlag==0 ){
297298
zIgnoreFlag = db_get("ignore-glob", 0);
298299
}
@@ -306,10 +307,11 @@
306307
fossil_all_reserved_names()
307308
);
308309
if( file_tree_name(g.zRepositoryName, &repo, 0) ){
309310
db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
310311
}
312
+ db_multi_exec("DELETE FROM sfile WHERE x IN (SELECT pathname FROM vfile)");
311313
blob_zero(&rewrittenPathname);
312314
while( db_step(&q)==SQLITE_ROW ){
313315
zDisplayName = zPathname = db_column_text(&q, 0);
314316
if( cwdRelative ) {
315317
char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
@@ -586,10 +588,13 @@
586588
** format YYYY-MM-DDTHH:MM:SS. If the string is not a valid date,
587589
** print a fatal error and quit.
588590
*/
589591
char *date_in_standard_format(const char *zInputDate){
590592
char *zDate;
593
+ if( g.perm.Setup && fossil_strcmp(zInputDate,"now")==0 ){
594
+ zInputDate = PD("date_override","now");
595
+ }
591596
zDate = db_text(0, "SELECT strftime('%%Y-%%m-%%dT%%H:%%M:%%f',%Q)",
592597
zInputDate);
593598
if( zDate[0]==0 ){
594599
fossil_fatal(
595600
"unrecognized date format (%s): use \"YYYY-MM-DD HH:MM:SS.SSS\"",
@@ -596,10 +601,26 @@
596601
zInputDate
597602
);
598603
}
599604
return zDate;
600605
}
606
+
607
+/*
608
+** COMMAND: test-date-format
609
+**
610
+** Usage: %fossil test-date-format DATE-STRING...
611
+**
612
+** Convert the DATE-STRING into the standard format used in artifacts
613
+** and display the result.
614
+*/
615
+void test_date_format(void){
616
+ int i;
617
+ db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
618
+ for(i=2; i<g.argc; i++){
619
+ fossil_print("%s -> %s\n", g.argv[i], date_in_standard_format(g.argv[i]));
620
+ }
621
+}
601622
602623
/*
603624
** Create a manifest.
604625
*/
605626
static void create_manifest(
606627
--- src/checkin.c
+++ src/checkin.c
@@ -288,11 +288,12 @@
288 Blob rewrittenPathname;
289 const char *zPathname, *zDisplayName;
290
291 db_must_be_within_tree();
292 cwdRelative = determine_cwd_relative_option();
293 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
 
294 n = strlen(g.zLocalRoot);
295 blob_init(&path, g.zLocalRoot, n-1);
296 if( zIgnoreFlag==0 ){
297 zIgnoreFlag = db_get("ignore-glob", 0);
298 }
@@ -306,10 +307,11 @@
306 fossil_all_reserved_names()
307 );
308 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
309 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
310 }
 
311 blob_zero(&rewrittenPathname);
312 while( db_step(&q)==SQLITE_ROW ){
313 zDisplayName = zPathname = db_column_text(&q, 0);
314 if( cwdRelative ) {
315 char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
@@ -586,10 +588,13 @@
586 ** format YYYY-MM-DDTHH:MM:SS. If the string is not a valid date,
587 ** print a fatal error and quit.
588 */
589 char *date_in_standard_format(const char *zInputDate){
590 char *zDate;
 
 
 
591 zDate = db_text(0, "SELECT strftime('%%Y-%%m-%%dT%%H:%%M:%%f',%Q)",
592 zInputDate);
593 if( zDate[0]==0 ){
594 fossil_fatal(
595 "unrecognized date format (%s): use \"YYYY-MM-DD HH:MM:SS.SSS\"",
@@ -596,10 +601,26 @@
596 zInputDate
597 );
598 }
599 return zDate;
600 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
601
602 /*
603 ** Create a manifest.
604 */
605 static void create_manifest(
606
--- src/checkin.c
+++ src/checkin.c
@@ -288,11 +288,12 @@
288 Blob rewrittenPathname;
289 const char *zPathname, *zDisplayName;
290
291 db_must_be_within_tree();
292 cwdRelative = determine_cwd_relative_option();
293 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
294 filename_collation());
295 n = strlen(g.zLocalRoot);
296 blob_init(&path, g.zLocalRoot, n-1);
297 if( zIgnoreFlag==0 ){
298 zIgnoreFlag = db_get("ignore-glob", 0);
299 }
@@ -306,10 +307,11 @@
307 fossil_all_reserved_names()
308 );
309 if( file_tree_name(g.zRepositoryName, &repo, 0) ){
310 db_multi_exec("DELETE FROM sfile WHERE x=%B", &repo);
311 }
312 db_multi_exec("DELETE FROM sfile WHERE x IN (SELECT pathname FROM vfile)");
313 blob_zero(&rewrittenPathname);
314 while( db_step(&q)==SQLITE_ROW ){
315 zDisplayName = zPathname = db_column_text(&q, 0);
316 if( cwdRelative ) {
317 char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
@@ -586,10 +588,13 @@
588 ** format YYYY-MM-DDTHH:MM:SS. If the string is not a valid date,
589 ** print a fatal error and quit.
590 */
591 char *date_in_standard_format(const char *zInputDate){
592 char *zDate;
593 if( g.perm.Setup && fossil_strcmp(zInputDate,"now")==0 ){
594 zInputDate = PD("date_override","now");
595 }
596 zDate = db_text(0, "SELECT strftime('%%Y-%%m-%%dT%%H:%%M:%%f',%Q)",
597 zInputDate);
598 if( zDate[0]==0 ){
599 fossil_fatal(
600 "unrecognized date format (%s): use \"YYYY-MM-DD HH:MM:SS.SSS\"",
@@ -596,10 +601,26 @@
601 zInputDate
602 );
603 }
604 return zDate;
605 }
606
607 /*
608 ** COMMAND: test-date-format
609 **
610 ** Usage: %fossil test-date-format DATE-STRING...
611 **
612 ** Convert the DATE-STRING into the standard format used in artifacts
613 ** and display the result.
614 */
615 void test_date_format(void){
616 int i;
617 db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
618 for(i=2; i<g.argc; i++){
619 fossil_print("%s -> %s\n", g.argv[i], date_in_standard_format(g.argv[i]));
620 }
621 }
622
623 /*
624 ** Create a manifest.
625 */
626 static void create_manifest(
627
--- src/configure.c
+++ src/configure.c
@@ -82,11 +82,15 @@
8282
{ "project-description", CONFIGSET_PROJ },
8383
{ "manifest", CONFIGSET_PROJ },
8484
{ "ignore-glob", CONFIGSET_PROJ },
8585
{ "crnl-glob", CONFIGSET_PROJ },
8686
{ "empty-dirs", CONFIGSET_PROJ },
87
+ { "allow-symlinks", CONFIGSET_PROJ },
8788
{ "index-page", CONFIGSET_SKIN },
89
+#ifdef FOSSIL_ENABLE_TCL
90
+ { "tcl", CONFIGSET_SKIN|CONFIGSET_TKT },
91
+#endif
8892
{ "timeline-block-markup", CONFIGSET_SKIN },
8993
{ "timeline-max-comment", CONFIGSET_SKIN },
9094
{ "ticket-table", CONFIGSET_TKT },
9195
{ "ticket-common", CONFIGSET_TKT },
9296
{ "ticket-newpage", CONFIGSET_TKT },
9397
--- src/configure.c
+++ src/configure.c
@@ -82,11 +82,15 @@
82 { "project-description", CONFIGSET_PROJ },
83 { "manifest", CONFIGSET_PROJ },
84 { "ignore-glob", CONFIGSET_PROJ },
85 { "crnl-glob", CONFIGSET_PROJ },
86 { "empty-dirs", CONFIGSET_PROJ },
 
87 { "index-page", CONFIGSET_SKIN },
 
 
 
88 { "timeline-block-markup", CONFIGSET_SKIN },
89 { "timeline-max-comment", CONFIGSET_SKIN },
90 { "ticket-table", CONFIGSET_TKT },
91 { "ticket-common", CONFIGSET_TKT },
92 { "ticket-newpage", CONFIGSET_TKT },
93
--- src/configure.c
+++ src/configure.c
@@ -82,11 +82,15 @@
82 { "project-description", CONFIGSET_PROJ },
83 { "manifest", CONFIGSET_PROJ },
84 { "ignore-glob", CONFIGSET_PROJ },
85 { "crnl-glob", CONFIGSET_PROJ },
86 { "empty-dirs", CONFIGSET_PROJ },
87 { "allow-symlinks", CONFIGSET_PROJ },
88 { "index-page", CONFIGSET_SKIN },
89 #ifdef FOSSIL_ENABLE_TCL
90 { "tcl", CONFIGSET_SKIN|CONFIGSET_TKT },
91 #endif
92 { "timeline-block-markup", CONFIGSET_SKIN },
93 { "timeline-max-comment", CONFIGSET_SKIN },
94 { "ticket-table", CONFIGSET_TKT },
95 { "ticket-common", CONFIGSET_TKT },
96 { "ticket-newpage", CONFIGSET_TKT },
97
+31 -5
--- src/content.c
+++ src/content.c
@@ -828,12 +828,35 @@
828828
Stmt q;
829829
Blob content;
830830
Blob cksum;
831831
int n1 = 0;
832832
int n2 = 0;
833
+ int nErr = 0;
833834
int total;
834835
db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
836
+
837
+ /* Make sure no public artifact is a delta from a private artifact */
838
+ db_prepare(&q,
839
+ "SELECT "
840
+ " rid, (SELECT uuid FROM blob WHERE rid=delta.rid),"
841
+ " srcid, (SELECT uuid FROM blob WHERE rid=delta.srcid)"
842
+ " FROM delta"
843
+ " WHERE srcid in private AND rid NOT IN private"
844
+ );
845
+ while( db_step(&q)==SQLITE_ROW ){
846
+ int rid = db_column_int(&q, 0);
847
+ const char *zId = db_column_text(&q, 1);
848
+ int srcid = db_column_int(&q, 2);
849
+ const char *zSrc = db_column_text(&q, 3);
850
+ fossil_print(
851
+ "public artifact %S (%d) is a delta from private artifact %S (%d)\n",
852
+ zId, rid, zSrc, srcid
853
+ );
854
+ nErr++;
855
+ }
856
+ db_finalize(&q);
857
+
835858
db_prepare(&q, "SELECT rid, uuid, size FROM blob ORDER BY rid");
836859
total = db_int(0, "SELECT max(rid) FROM blob");
837860
while( db_step(&q)==SQLITE_ROW ){
838861
int rid = db_column_int(&q, 0);
839862
const char *zUuid = db_column_text(&q, 1);
@@ -845,20 +868,23 @@
845868
fossil_print("skip phantom %d %s\n", rid, zUuid);
846869
continue; /* Ignore phantoms */
847870
}
848871
content_get(rid, &content);
849872
if( blob_size(&content)!=size ){
850
- fossil_warning("size mismatch on blob rid=%d: %d vs %d",
851
- rid, blob_size(&content), size);
873
+ fossil_print("size mismatch on artifact %d: wanted %d but got %d\n",
874
+ rid, size, blob_size(&content));
875
+ nErr++;
852876
}
853877
sha1sum_blob(&content, &cksum);
854878
if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
855
- fossil_fatal("checksum mismatch on blob rid=%d: %s vs %s",
856
- rid, blob_str(&cksum), zUuid);
879
+ fossil_print("checksum mismatch on artifact %d: wanted %s but got %s\n",
880
+ rid, zUuid, blob_str(&cksum));
881
+ nErr++;
857882
}
858883
blob_reset(&cksum);
859884
blob_reset(&content);
860885
n2++;
861886
}
862887
db_finalize(&q);
863
- fossil_print("%d non-phantom blobs (out of %d total) verified\n", n2, n1);
888
+ fossil_print("%d non-phantom blobs (out of %d total) checked: %d errors\n",
889
+ n2, n1, nErr);
864890
}
865891
--- src/content.c
+++ src/content.c
@@ -828,12 +828,35 @@
828 Stmt q;
829 Blob content;
830 Blob cksum;
831 int n1 = 0;
832 int n2 = 0;
 
833 int total;
834 db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
835 db_prepare(&q, "SELECT rid, uuid, size FROM blob ORDER BY rid");
836 total = db_int(0, "SELECT max(rid) FROM blob");
837 while( db_step(&q)==SQLITE_ROW ){
838 int rid = db_column_int(&q, 0);
839 const char *zUuid = db_column_text(&q, 1);
@@ -845,20 +868,23 @@
845 fossil_print("skip phantom %d %s\n", rid, zUuid);
846 continue; /* Ignore phantoms */
847 }
848 content_get(rid, &content);
849 if( blob_size(&content)!=size ){
850 fossil_warning("size mismatch on blob rid=%d: %d vs %d",
851 rid, blob_size(&content), size);
 
852 }
853 sha1sum_blob(&content, &cksum);
854 if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
855 fossil_fatal("checksum mismatch on blob rid=%d: %s vs %s",
856 rid, blob_str(&cksum), zUuid);
 
857 }
858 blob_reset(&cksum);
859 blob_reset(&content);
860 n2++;
861 }
862 db_finalize(&q);
863 fossil_print("%d non-phantom blobs (out of %d total) verified\n", n2, n1);
 
864 }
865
--- src/content.c
+++ src/content.c
@@ -828,12 +828,35 @@
828 Stmt q;
829 Blob content;
830 Blob cksum;
831 int n1 = 0;
832 int n2 = 0;
833 int nErr = 0;
834 int total;
835 db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
836
837 /* Make sure no public artifact is a delta from a private artifact */
838 db_prepare(&q,
839 "SELECT "
840 " rid, (SELECT uuid FROM blob WHERE rid=delta.rid),"
841 " srcid, (SELECT uuid FROM blob WHERE rid=delta.srcid)"
842 " FROM delta"
843 " WHERE srcid in private AND rid NOT IN private"
844 );
845 while( db_step(&q)==SQLITE_ROW ){
846 int rid = db_column_int(&q, 0);
847 const char *zId = db_column_text(&q, 1);
848 int srcid = db_column_int(&q, 2);
849 const char *zSrc = db_column_text(&q, 3);
850 fossil_print(
851 "public artifact %S (%d) is a delta from private artifact %S (%d)\n",
852 zId, rid, zSrc, srcid
853 );
854 nErr++;
855 }
856 db_finalize(&q);
857
858 db_prepare(&q, "SELECT rid, uuid, size FROM blob ORDER BY rid");
859 total = db_int(0, "SELECT max(rid) FROM blob");
860 while( db_step(&q)==SQLITE_ROW ){
861 int rid = db_column_int(&q, 0);
862 const char *zUuid = db_column_text(&q, 1);
@@ -845,20 +868,23 @@
868 fossil_print("skip phantom %d %s\n", rid, zUuid);
869 continue; /* Ignore phantoms */
870 }
871 content_get(rid, &content);
872 if( blob_size(&content)!=size ){
873 fossil_print("size mismatch on artifact %d: wanted %d but got %d\n",
874 rid, size, blob_size(&content));
875 nErr++;
876 }
877 sha1sum_blob(&content, &cksum);
878 if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
879 fossil_print("checksum mismatch on artifact %d: wanted %s but got %s\n",
880 rid, zUuid, blob_str(&cksum));
881 nErr++;
882 }
883 blob_reset(&cksum);
884 blob_reset(&content);
885 n2++;
886 }
887 db_finalize(&q);
888 fossil_print("%d non-phantom blobs (out of %d total) checked: %d errors\n",
889 n2, n1, nErr);
890 }
891
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
13961396
/* end file parser/JSON_parser.c */
13971397
/* begin file ./cson.c */
13981398
#include <assert.h>
13991399
#include <stdlib.h> /* malloc()/free() */
14001400
#include <string.h>
1401
+#include <errno.h>
14011402
14021403
#ifdef _MSC_VER
14031404
# if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
14041405
# pragma warning( push )
14051406
# pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
49504951
}
49514952
else continue;
49524953
}
49534954
return 0;
49544955
}
4956
+
4957
+static cson_value * cson_guess_arg_type(char const *arg){
4958
+ char * end = NULL;
4959
+ if(('0'<=*arg) && ('9'>=*arg)){
4960
+ goto do_string;
4961
+ }
4962
+ {
4963
+ long const val = strtol(arg, &end, 10);
4964
+ if(!*end){
4965
+ return cson_value_new_integer( (cson_int_t)val);
4966
+ }
4967
+ }
4968
+ {
4969
+ double const val = strtod(arg, &end);
4970
+ if(!*end){
4971
+ return cson_value_new_double(val);
4972
+ }
4973
+ }
4974
+
4975
+
4976
+ do_string:
4977
+ return cson_value_new_string(arg, strlen(arg));
4978
+}
4979
+
4980
+
4981
+int cson_parse_argv_flags( int argc, char const * const * argv,
4982
+ cson_object ** tgt, unsigned int * count ){
4983
+ cson_object * o = NULL;
4984
+ int rc = 0;
4985
+ int i = 0;
4986
+ if(argc<1 || !argc || !tgt) return cson_rc.ArgError;
4987
+ o = *tgt ? *tgt : cson_new_object();
4988
+ if(count) *count = 0;
4989
+ for( i = 0; i < argc; ++i ){
4990
+ char const * arg = argv[i];
4991
+ char const * key = arg;
4992
+ char const * pos;
4993
+ cson_string * k = NULL;
4994
+ cson_value * v = NULL;
4995
+ if('-' != *arg) continue;
4996
+ while('-'==*key) ++key;
4997
+ if(!*key) continue;
4998
+ pos = key;
4999
+ while( *pos && ('=' != *pos)) ++pos;
5000
+ k = cson_new_string(key, pos-key);
5001
+ if(!k){
5002
+ rc = cson_rc.AllocError;
5003
+ break;
5004
+ }
5005
+ if(!*pos){ /** --key */
5006
+ v = cson_value_true();
5007
+ }else{ /** --key=...*/
5008
+ assert('=' == *pos);
5009
+ ++pos /*skip '='*/;
5010
+ v = *pos
5011
+ ? cson_guess_arg_type(pos)
5012
+ : cson_value_null();
5013
+ }
5014
+ if(0 != (rc=cson_object_set_s(o, k, v))){
5015
+ cson_free_string(k);
5016
+ cson_value_free(v);
5017
+ break;
5018
+ }
5019
+ else if(count) ++*count;
5020
+ }
5021
+ if(o != *tgt){
5022
+ if(rc) cson_free_object(o);
5023
+ else *tgt = o;
5024
+ }
5025
+ return rc;
5026
+}
49555027
49565028
#if defined(__cplusplus)
49575029
} /*extern "C"*/
49585030
#endif
49595031
49605032
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
1396 /* end file parser/JSON_parser.c */
1397 /* begin file ./cson.c */
1398 #include <assert.h>
1399 #include <stdlib.h> /* malloc()/free() */
1400 #include <string.h>
 
1401
1402 #ifdef _MSC_VER
1403 # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
1404 # pragma warning( push )
1405 # pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
4950 }
4951 else continue;
4952 }
4953 return 0;
4954 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4955
4956 #if defined(__cplusplus)
4957 } /*extern "C"*/
4958 #endif
4959
4960
--- src/cson_amalgamation.c
+++ src/cson_amalgamation.c
@@ -1396,10 +1396,11 @@
1396 /* end file parser/JSON_parser.c */
1397 /* begin file ./cson.c */
1398 #include <assert.h>
1399 #include <stdlib.h> /* malloc()/free() */
1400 #include <string.h>
1401 #include <errno.h>
1402
1403 #ifdef _MSC_VER
1404 # if _MSC_VER >= 1400 /* Visual Studio 2005 and up */
1405 # pragma warning( push )
1406 # pragma warning(disable:4996) /* unsecure sscanf (but snscanf() isn't in c89) */
@@ -4950,10 +4951,81 @@
4951 }
4952 else continue;
4953 }
4954 return 0;
4955 }
4956
4957 static cson_value * cson_guess_arg_type(char const *arg){
4958 char * end = NULL;
4959 if(('0'<=*arg) && ('9'>=*arg)){
4960 goto do_string;
4961 }
4962 {
4963 long const val = strtol(arg, &end, 10);
4964 if(!*end){
4965 return cson_value_new_integer( (cson_int_t)val);
4966 }
4967 }
4968 {
4969 double const val = strtod(arg, &end);
4970 if(!*end){
4971 return cson_value_new_double(val);
4972 }
4973 }
4974
4975
4976 do_string:
4977 return cson_value_new_string(arg, strlen(arg));
4978 }
4979
4980
4981 int cson_parse_argv_flags( int argc, char const * const * argv,
4982 cson_object ** tgt, unsigned int * count ){
4983 cson_object * o = NULL;
4984 int rc = 0;
4985 int i = 0;
4986 if(argc<1 || !argc || !tgt) return cson_rc.ArgError;
4987 o = *tgt ? *tgt : cson_new_object();
4988 if(count) *count = 0;
4989 for( i = 0; i < argc; ++i ){
4990 char const * arg = argv[i];
4991 char const * key = arg;
4992 char const * pos;
4993 cson_string * k = NULL;
4994 cson_value * v = NULL;
4995 if('-' != *arg) continue;
4996 while('-'==*key) ++key;
4997 if(!*key) continue;
4998 pos = key;
4999 while( *pos && ('=' != *pos)) ++pos;
5000 k = cson_new_string(key, pos-key);
5001 if(!k){
5002 rc = cson_rc.AllocError;
5003 break;
5004 }
5005 if(!*pos){ /** --key */
5006 v = cson_value_true();
5007 }else{ /** --key=...*/
5008 assert('=' == *pos);
5009 ++pos /*skip '='*/;
5010 v = *pos
5011 ? cson_guess_arg_type(pos)
5012 : cson_value_null();
5013 }
5014 if(0 != (rc=cson_object_set_s(o, k, v))){
5015 cson_free_string(k);
5016 cson_value_free(v);
5017 break;
5018 }
5019 else if(count) ++*count;
5020 }
5021 if(o != *tgt){
5022 if(rc) cson_free_object(o);
5023 else *tgt = o;
5024 }
5025 return rc;
5026 }
5027
5028 #if defined(__cplusplus)
5029 } /*extern "C"*/
5030 #endif
5031
5032
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
21082108
necessarily increase when a new item is inserted into it. An interesting
21092109
side-effect of this is that when cson_clone()ing an array or object, the
21102110
size of the clone can actually be less than the original.
21112111
*/
21122112
unsigned int cson_value_msize(cson_value const * v);
2113
+
2114
+/**
2115
+ Parses command-line-style arguments into a JSON object.
2116
+
2117
+ It expects arguments to be in any of these forms, and any number
2118
+ of leading dashes are treated identically:
2119
+
2120
+ --key : Treats key as a boolean with a true value.
2121
+
2122
+ --key=VAL : Treats VAL as either a double, integer, or string.
2123
+
2124
+ --key= : Treats key as a JSON null (not literal NULL) value.
2125
+
2126
+ Arguments not starting with a dash are skipped.
2127
+
2128
+ Each key/value pair is inserted into an object. If a given key
2129
+ appears more than once then only the final entry is actually
2130
+ stored.
2131
+
2132
+ argc and argv are expected to be values from main() (or similar,
2133
+ possibly adjusted to remove argv[0]).
2134
+
2135
+ tgt must be either a pointer to NULL or a pointer to a
2136
+ client-provided Object. If (NULL==*tgt) then this function
2137
+ allocates a new object and on success it stores the new object in
2138
+ *tgt (it is owned by the caller). If (NULL!=*tgt) then it is
2139
+ assumed to be a properly allocated object. DO NOT pass a pointer to
2140
+ an unitialized pointer, as that will fool this function into
2141
+ thinking it is a valid object and Undefined Behaviour will ensue.
2142
+
2143
+ If count is not NULL then the number of arugments parsed by this
2144
+ function are assigned to it. On error, count will be the number of
2145
+ options successfully parsed before the error was encountered.
2146
+
2147
+ On success:
2148
+
2149
+ - 0 is returned.
2150
+
2151
+ - If (*tgt==NULL) then *tgt is assigned to a newly-allocated
2152
+ object, owned by the caller. Note that even if no arguments are
2153
+ parsed, the object is still created.
2154
+
2155
+ On error:
2156
+
2157
+ - non-0 is returned
2158
+
2159
+ - If (*tgt==NULL) then it is not modified.
2160
+
2161
+ - If (*tgt!=NULL) (i.e., the caller provides his own object) then
2162
+ it might contain partial results.
2163
+*/
2164
+int cson_parse_argv_flags( int argc, char const * const * argv,
2165
+ cson_object ** tgt, unsigned int * count );
2166
+
21132167
21142168
/* LICENSE
21152169
21162170
This software's source code, including accompanying documentation and
21172171
demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
23822436
#endif
23832437
23842438
#endif /* CSON_ENABLE_SQLITE3 */
23852439
#endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
23862440
/* end file include/wh/cson/cson_sqlite3.h */
2387
-
23882441
#endif /* FOSSIL_ENABLE_JSON */
23892442
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
2108 necessarily increase when a new item is inserted into it. An interesting
2109 side-effect of this is that when cson_clone()ing an array or object, the
2110 size of the clone can actually be less than the original.
2111 */
2112 unsigned int cson_value_msize(cson_value const * v);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2113
2114 /* LICENSE
2115
2116 This software's source code, including accompanying documentation and
2117 demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
2382 #endif
2383
2384 #endif /* CSON_ENABLE_SQLITE3 */
2385 #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
2386 /* end file include/wh/cson/cson_sqlite3.h */
2387
2388 #endif /* FOSSIL_ENABLE_JSON */
2389
--- src/cson_amalgamation.h
+++ src/cson_amalgamation.h
@@ -2108,10 +2108,64 @@
2108 necessarily increase when a new item is inserted into it. An interesting
2109 side-effect of this is that when cson_clone()ing an array or object, the
2110 size of the clone can actually be less than the original.
2111 */
2112 unsigned int cson_value_msize(cson_value const * v);
2113
2114 /**
2115 Parses command-line-style arguments into a JSON object.
2116
2117 It expects arguments to be in any of these forms, and any number
2118 of leading dashes are treated identically:
2119
2120 --key : Treats key as a boolean with a true value.
2121
2122 --key=VAL : Treats VAL as either a double, integer, or string.
2123
2124 --key= : Treats key as a JSON null (not literal NULL) value.
2125
2126 Arguments not starting with a dash are skipped.
2127
2128 Each key/value pair is inserted into an object. If a given key
2129 appears more than once then only the final entry is actually
2130 stored.
2131
2132 argc and argv are expected to be values from main() (or similar,
2133 possibly adjusted to remove argv[0]).
2134
2135 tgt must be either a pointer to NULL or a pointer to a
2136 client-provided Object. If (NULL==*tgt) then this function
2137 allocates a new object and on success it stores the new object in
2138 *tgt (it is owned by the caller). If (NULL!=*tgt) then it is
2139 assumed to be a properly allocated object. DO NOT pass a pointer to
2140 an unitialized pointer, as that will fool this function into
2141 thinking it is a valid object and Undefined Behaviour will ensue.
2142
2143 If count is not NULL then the number of arugments parsed by this
2144 function are assigned to it. On error, count will be the number of
2145 options successfully parsed before the error was encountered.
2146
2147 On success:
2148
2149 - 0 is returned.
2150
2151 - If (*tgt==NULL) then *tgt is assigned to a newly-allocated
2152 object, owned by the caller. Note that even if no arguments are
2153 parsed, the object is still created.
2154
2155 On error:
2156
2157 - non-0 is returned
2158
2159 - If (*tgt==NULL) then it is not modified.
2160
2161 - If (*tgt!=NULL) (i.e., the caller provides his own object) then
2162 it might contain partial results.
2163 */
2164 int cson_parse_argv_flags( int argc, char const * const * argv,
2165 cson_object ** tgt, unsigned int * count );
2166
2167
2168 /* LICENSE
2169
2170 This software's source code, including accompanying documentation and
2171 demonstration applications, are licensed under the following
@@ -2382,7 +2436,6 @@
2436 #endif
2437
2438 #endif /* CSON_ENABLE_SQLITE3 */
2439 #endif /* WANDERINGHORSE_NET_CSON_SQLITE3_H_INCLUDED */
2440 /* end file include/wh/cson/cson_sqlite3.h */
 
2441 #endif /* FOSSIL_ENABLE_JSON */
2442
+2 -2
--- src/db.c
+++ src/db.c
@@ -1809,11 +1809,11 @@
18091809
char const *def; /* Default value */
18101810
};
18111811
#endif /* INTERFACE */
18121812
struct stControlSettings const ctrlSettings[] = {
18131813
{ "access-log", 0, 0, 0, "off" },
1814
- { "allow-symlinks",0, 0, 0, "off" },
1814
+ { "allow-symlinks",0, 0, 1, "off" },
18151815
{ "auto-captcha", "autocaptcha", 0, 0, "on" },
18161816
{ "auto-shun", 0, 0, 0, "on" },
18171817
{ "autosync", 0, 0, 0, "on" },
18181818
{ "binary-glob", 0, 32, 1, "" },
18191819
{ "clearsign", 0, 0, 0, "off" },
@@ -1867,11 +1867,11 @@
18671867
**
18681868
** The "unset" command clears a property setting.
18691869
**
18701870
**
18711871
** allow-symlinks If enabled, don't follow symlinks, and instead treat
1872
-** them as symlinks on Unix. Has no effect on Windows
1872
+** (versionable) them as symlinks on Unix. Has no effect on Windows
18731873
** (existing links in repository created on Unix become
18741874
** plain-text files with link destination path inside).
18751875
** Default: off
18761876
**
18771877
** auto-captcha If enabled, the Login page provides a button to
18781878
--- src/db.c
+++ src/db.c
@@ -1809,11 +1809,11 @@
1809 char const *def; /* Default value */
1810 };
1811 #endif /* INTERFACE */
1812 struct stControlSettings const ctrlSettings[] = {
1813 { "access-log", 0, 0, 0, "off" },
1814 { "allow-symlinks",0, 0, 0, "off" },
1815 { "auto-captcha", "autocaptcha", 0, 0, "on" },
1816 { "auto-shun", 0, 0, 0, "on" },
1817 { "autosync", 0, 0, 0, "on" },
1818 { "binary-glob", 0, 32, 1, "" },
1819 { "clearsign", 0, 0, 0, "off" },
@@ -1867,11 +1867,11 @@
1867 **
1868 ** The "unset" command clears a property setting.
1869 **
1870 **
1871 ** allow-symlinks If enabled, don't follow symlinks, and instead treat
1872 ** them as symlinks on Unix. Has no effect on Windows
1873 ** (existing links in repository created on Unix become
1874 ** plain-text files with link destination path inside).
1875 ** Default: off
1876 **
1877 ** auto-captcha If enabled, the Login page provides a button to
1878
--- src/db.c
+++ src/db.c
@@ -1809,11 +1809,11 @@
1809 char const *def; /* Default value */
1810 };
1811 #endif /* INTERFACE */
1812 struct stControlSettings const ctrlSettings[] = {
1813 { "access-log", 0, 0, 0, "off" },
1814 { "allow-symlinks",0, 0, 1, "off" },
1815 { "auto-captcha", "autocaptcha", 0, 0, "on" },
1816 { "auto-shun", 0, 0, 0, "on" },
1817 { "autosync", 0, 0, 0, "on" },
1818 { "binary-glob", 0, 32, 1, "" },
1819 { "clearsign", 0, 0, 0, "off" },
@@ -1867,11 +1867,11 @@
1867 **
1868 ** The "unset" command clears a property setting.
1869 **
1870 **
1871 ** allow-symlinks If enabled, don't follow symlinks, and instead treat
1872 ** (versionable) them as symlinks on Unix. Has no effect on Windows
1873 ** (existing links in repository created on Unix become
1874 ** plain-text files with link destination path inside).
1875 ** Default: off
1876 **
1877 ** auto-captcha If enabled, the Login page provides a button to
1878
+2 -1
--- src/file.c
+++ src/file.c
@@ -997,11 +997,12 @@
997997
** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
998998
** to deallocate any memory used to store the returned pointer when done.
999999
*/
10001000
void fossil_mbcs_free(char *zOld){
10011001
#ifdef _WIN32
1002
- free(zOld);
1002
+ extern void sqlite3_free(void*);
1003
+ sqlite3_free(zOld);
10031004
#else
10041005
/* No-op on unix */
10051006
#endif
10061007
}
10071008
10081009
--- src/file.c
+++ src/file.c
@@ -997,11 +997,12 @@
997 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
998 ** to deallocate any memory used to store the returned pointer when done.
999 */
1000 void fossil_mbcs_free(char *zOld){
1001 #ifdef _WIN32
1002 free(zOld);
 
1003 #else
1004 /* No-op on unix */
1005 #endif
1006 }
1007
1008
--- src/file.c
+++ src/file.c
@@ -997,11 +997,12 @@
997 ** Translate MBCS to UTF8. Return a pointer. Call fossil_mbcs_free()
998 ** to deallocate any memory used to store the returned pointer when done.
999 */
1000 void fossil_mbcs_free(char *zOld){
1001 #ifdef _WIN32
1002 extern void sqlite3_free(void*);
1003 sqlite3_free(zOld);
1004 #else
1005 /* No-op on unix */
1006 #endif
1007 }
1008
1009
+23 -17
--- src/finfo.c
+++ src/finfo.c
@@ -39,22 +39,25 @@
3939
** In the -p form, there's an optional flag "-r|--revision REVISION".
4040
** The specified version (or the latest checked out version) is printed
4141
** to stdout.
4242
**
4343
** Options:
44
-** --brief|-b display a brief (one line / revision) summary
45
-** --limit N display the first N changes
46
-** --log|-l select log mode (the default)
47
-** --offset P skip P changes
48
-** -p select print mode
49
-** --revision|-r R print the given revision (or ckout, if none is given)
50
-** to stdout (only in print mode)
51
-** -s select status mode (print a status indicator for FILE)
44
+** --brief|-b display a brief (one line / revision) summary
45
+** --limit N display the first N changes
46
+** --log|-l select log mode (the default)
47
+** --offset P skip P changes
48
+** -p select print mode
49
+** --revision|-r R print the given revision (or ckout, if none is given)
50
+** to stdout (only in print mode)
51
+** -s select status mode (print a status indicator for FILE)
52
+** --case-sensitive B Enable or disable case-sensitive filenames. B is a
53
+** boolean: "yes", "no", "true", "false", etc.
5254
**
5355
** See also: descendants, info, leaves
5456
*/
5557
void finfo_cmd(void){
58
+ capture_case_sensitive_option();
5659
db_must_be_within_tree();
5760
if (find_option("status","s",0)) {
5861
Stmt q;
5962
Blob line;
6063
Blob fname;
@@ -67,11 +70,12 @@
6770
}
6871
vfile_check_signature(vid, 1, 0);
6972
file_tree_name(g.argv[2], &fname, 1);
7073
db_prepare(&q,
7174
"SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
72
- " FROM vfile WHERE vfile.pathname=%B", &fname);
75
+ " FROM vfile WHERE vfile.pathname=%B %s",
76
+ &fname, filename_collation());
7377
blob_zero(&line);
7478
if ( db_step(&q)==SQLITE_ROW ) {
7579
Blob uuid;
7680
int isDeleted = db_column_int(&q, 1);
7781
int isNew = db_column_int(&q,2) == 0;
@@ -80,12 +84,12 @@
8084
8185
blob_zero(&uuid);
8286
db_blob(&uuid,
8387
"SELECT uuid FROM blob, mlink, vfile WHERE "
8488
"blob.rid = mlink.mid AND mlink.fid = vfile.rid AND "
85
- "vfile.pathname=%B",
86
- &fname
89
+ "vfile.pathname=%B %s",
90
+ &fname, filename_collation()
8791
);
8892
if( isNew ){
8993
blob_appendf(&line, "new");
9094
}else if( isDeleted ){
9195
blob_appendf(&line, "deleted");
@@ -113,11 +117,12 @@
113117
114118
file_tree_name(g.argv[2], &fname, 1);
115119
if( zRevision ){
116120
historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0, 0);
117121
}else{
118
- int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
122
+ int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B %s",
123
+ &fname, filename_collation());
119124
if( rid==0 ){
120125
fossil_fatal("no history for file: %b", &fname);
121126
}
122127
content_get(rid, &record);
123128
}
@@ -144,27 +149,28 @@
144149
iBrief = (find_option("brief","b",0) == 0);
145150
if( g.argc!=3 ){
146151
usage("?-l|--log? ?-b|--brief? FILENAME");
147152
}
148153
file_tree_name(g.argv[2], &fname, 1);
149
- rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
154
+ rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B %s",
155
+ &fname, filename_collation());
150156
if( rid==0 ){
151157
fossil_fatal("no history for file: %b", &fname);
152158
}
153159
zFilename = blob_str(&fname);
154160
db_prepare(&q,
155161
"SELECT b.uuid, ci.uuid, date(event.mtime,'localtime'),"
156162
" coalesce(event.ecomment, event.comment),"
157163
" coalesce(event.euser, event.user)"
158164
" FROM mlink, blob b, event, blob ci, filename"
159
- " WHERE filename.name=%Q"
165
+ " WHERE filename.name=%Q %s"
160166
" AND mlink.fnid=filename.fnid"
161167
" AND b.rid=mlink.fid"
162168
" AND event.objid=mlink.mid"
163169
" AND event.objid=ci.rid"
164170
" ORDER BY event.mtime DESC LIMIT %d OFFSET %d",
165
- zFilename, iLimit, iOffset
171
+ zFilename, filename_collation(), iLimit, iOffset
166172
);
167173
blob_zero(&line);
168174
if( iBrief ){
169175
fossil_print("History of %s\n", blob_str(&fname));
170176
}
@@ -243,14 +249,14 @@
243249
" (SELECT uuid FROM blob WHERE rid=mlink.mid)," /* Check-in uuid */
244250
" event.bgcolor," /* Background color */
245251
" (SELECT value FROM tagxref WHERE tagid=%d AND tagtype>0"
246252
" AND tagxref.rid=mlink.mid)" /* Tags */
247253
" FROM mlink, event"
248
- " WHERE mlink.fnid=(SELECT fnid FROM filename WHERE name=%Q)"
254
+ " WHERE mlink.fnid IN (SELECT fnid FROM filename WHERE name=%Q %s)"
249255
" AND event.objid=mlink.mid",
250256
TAG_BRANCH,
251
- zFilename
257
+ zFilename, filename_collation()
252258
);
253259
if( (zA = P("a"))!=0 ){
254260
blob_appendf(&sql, " AND event.mtime>=julianday('%q')", zA);
255261
}
256262
if( (zB = P("b"))!=0 ){
257263
--- src/finfo.c
+++ src/finfo.c
@@ -39,22 +39,25 @@
39 ** In the -p form, there's an optional flag "-r|--revision REVISION".
40 ** The specified version (or the latest checked out version) is printed
41 ** to stdout.
42 **
43 ** Options:
44 ** --brief|-b display a brief (one line / revision) summary
45 ** --limit N display the first N changes
46 ** --log|-l select log mode (the default)
47 ** --offset P skip P changes
48 ** -p select print mode
49 ** --revision|-r R print the given revision (or ckout, if none is given)
50 ** to stdout (only in print mode)
51 ** -s select status mode (print a status indicator for FILE)
 
 
52 **
53 ** See also: descendants, info, leaves
54 */
55 void finfo_cmd(void){
 
56 db_must_be_within_tree();
57 if (find_option("status","s",0)) {
58 Stmt q;
59 Blob line;
60 Blob fname;
@@ -67,11 +70,12 @@
67 }
68 vfile_check_signature(vid, 1, 0);
69 file_tree_name(g.argv[2], &fname, 1);
70 db_prepare(&q,
71 "SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
72 " FROM vfile WHERE vfile.pathname=%B", &fname);
 
73 blob_zero(&line);
74 if ( db_step(&q)==SQLITE_ROW ) {
75 Blob uuid;
76 int isDeleted = db_column_int(&q, 1);
77 int isNew = db_column_int(&q,2) == 0;
@@ -80,12 +84,12 @@
80
81 blob_zero(&uuid);
82 db_blob(&uuid,
83 "SELECT uuid FROM blob, mlink, vfile WHERE "
84 "blob.rid = mlink.mid AND mlink.fid = vfile.rid AND "
85 "vfile.pathname=%B",
86 &fname
87 );
88 if( isNew ){
89 blob_appendf(&line, "new");
90 }else if( isDeleted ){
91 blob_appendf(&line, "deleted");
@@ -113,11 +117,12 @@
113
114 file_tree_name(g.argv[2], &fname, 1);
115 if( zRevision ){
116 historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0, 0);
117 }else{
118 int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
 
119 if( rid==0 ){
120 fossil_fatal("no history for file: %b", &fname);
121 }
122 content_get(rid, &record);
123 }
@@ -144,27 +149,28 @@
144 iBrief = (find_option("brief","b",0) == 0);
145 if( g.argc!=3 ){
146 usage("?-l|--log? ?-b|--brief? FILENAME");
147 }
148 file_tree_name(g.argv[2], &fname, 1);
149 rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
 
150 if( rid==0 ){
151 fossil_fatal("no history for file: %b", &fname);
152 }
153 zFilename = blob_str(&fname);
154 db_prepare(&q,
155 "SELECT b.uuid, ci.uuid, date(event.mtime,'localtime'),"
156 " coalesce(event.ecomment, event.comment),"
157 " coalesce(event.euser, event.user)"
158 " FROM mlink, blob b, event, blob ci, filename"
159 " WHERE filename.name=%Q"
160 " AND mlink.fnid=filename.fnid"
161 " AND b.rid=mlink.fid"
162 " AND event.objid=mlink.mid"
163 " AND event.objid=ci.rid"
164 " ORDER BY event.mtime DESC LIMIT %d OFFSET %d",
165 zFilename, iLimit, iOffset
166 );
167 blob_zero(&line);
168 if( iBrief ){
169 fossil_print("History of %s\n", blob_str(&fname));
170 }
@@ -243,14 +249,14 @@
243 " (SELECT uuid FROM blob WHERE rid=mlink.mid)," /* Check-in uuid */
244 " event.bgcolor," /* Background color */
245 " (SELECT value FROM tagxref WHERE tagid=%d AND tagtype>0"
246 " AND tagxref.rid=mlink.mid)" /* Tags */
247 " FROM mlink, event"
248 " WHERE mlink.fnid=(SELECT fnid FROM filename WHERE name=%Q)"
249 " AND event.objid=mlink.mid",
250 TAG_BRANCH,
251 zFilename
252 );
253 if( (zA = P("a"))!=0 ){
254 blob_appendf(&sql, " AND event.mtime>=julianday('%q')", zA);
255 }
256 if( (zB = P("b"))!=0 ){
257
--- src/finfo.c
+++ src/finfo.c
@@ -39,22 +39,25 @@
39 ** In the -p form, there's an optional flag "-r|--revision REVISION".
40 ** The specified version (or the latest checked out version) is printed
41 ** to stdout.
42 **
43 ** Options:
44 ** --brief|-b display a brief (one line / revision) summary
45 ** --limit N display the first N changes
46 ** --log|-l select log mode (the default)
47 ** --offset P skip P changes
48 ** -p select print mode
49 ** --revision|-r R print the given revision (or ckout, if none is given)
50 ** to stdout (only in print mode)
51 ** -s select status mode (print a status indicator for FILE)
52 ** --case-sensitive B Enable or disable case-sensitive filenames. B is a
53 ** boolean: "yes", "no", "true", "false", etc.
54 **
55 ** See also: descendants, info, leaves
56 */
57 void finfo_cmd(void){
58 capture_case_sensitive_option();
59 db_must_be_within_tree();
60 if (find_option("status","s",0)) {
61 Stmt q;
62 Blob line;
63 Blob fname;
@@ -67,11 +70,12 @@
70 }
71 vfile_check_signature(vid, 1, 0);
72 file_tree_name(g.argv[2], &fname, 1);
73 db_prepare(&q,
74 "SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
75 " FROM vfile WHERE vfile.pathname=%B %s",
76 &fname, filename_collation());
77 blob_zero(&line);
78 if ( db_step(&q)==SQLITE_ROW ) {
79 Blob uuid;
80 int isDeleted = db_column_int(&q, 1);
81 int isNew = db_column_int(&q,2) == 0;
@@ -80,12 +84,12 @@
84
85 blob_zero(&uuid);
86 db_blob(&uuid,
87 "SELECT uuid FROM blob, mlink, vfile WHERE "
88 "blob.rid = mlink.mid AND mlink.fid = vfile.rid AND "
89 "vfile.pathname=%B %s",
90 &fname, filename_collation()
91 );
92 if( isNew ){
93 blob_appendf(&line, "new");
94 }else if( isDeleted ){
95 blob_appendf(&line, "deleted");
@@ -113,11 +117,12 @@
117
118 file_tree_name(g.argv[2], &fname, 1);
119 if( zRevision ){
120 historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0, 0);
121 }else{
122 int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B %s",
123 &fname, filename_collation());
124 if( rid==0 ){
125 fossil_fatal("no history for file: %b", &fname);
126 }
127 content_get(rid, &record);
128 }
@@ -144,27 +149,28 @@
149 iBrief = (find_option("brief","b",0) == 0);
150 if( g.argc!=3 ){
151 usage("?-l|--log? ?-b|--brief? FILENAME");
152 }
153 file_tree_name(g.argv[2], &fname, 1);
154 rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B %s",
155 &fname, filename_collation());
156 if( rid==0 ){
157 fossil_fatal("no history for file: %b", &fname);
158 }
159 zFilename = blob_str(&fname);
160 db_prepare(&q,
161 "SELECT b.uuid, ci.uuid, date(event.mtime,'localtime'),"
162 " coalesce(event.ecomment, event.comment),"
163 " coalesce(event.euser, event.user)"
164 " FROM mlink, blob b, event, blob ci, filename"
165 " WHERE filename.name=%Q %s"
166 " AND mlink.fnid=filename.fnid"
167 " AND b.rid=mlink.fid"
168 " AND event.objid=mlink.mid"
169 " AND event.objid=ci.rid"
170 " ORDER BY event.mtime DESC LIMIT %d OFFSET %d",
171 zFilename, filename_collation(), iLimit, iOffset
172 );
173 blob_zero(&line);
174 if( iBrief ){
175 fossil_print("History of %s\n", blob_str(&fname));
176 }
@@ -243,14 +249,14 @@
249 " (SELECT uuid FROM blob WHERE rid=mlink.mid)," /* Check-in uuid */
250 " event.bgcolor," /* Background color */
251 " (SELECT value FROM tagxref WHERE tagid=%d AND tagtype>0"
252 " AND tagxref.rid=mlink.mid)" /* Tags */
253 " FROM mlink, event"
254 " WHERE mlink.fnid IN (SELECT fnid FROM filename WHERE name=%Q %s)"
255 " AND event.objid=mlink.mid",
256 TAG_BRANCH,
257 zFilename, filename_collation()
258 );
259 if( (zA = P("a"))!=0 ){
260 blob_appendf(&sql, " AND event.mtime>=julianday('%q')", zA);
261 }
262 if( (zB = P("b"))!=0 ){
263
+1 -1
--- src/http.c
+++ src/http.c
@@ -111,11 +111,11 @@
111111
fossil_free(zEncoded);
112112
fossil_free(zCredentials);
113113
}
114114
blob_appendf(pHdr, "Host: %s\r\n", g.urlHostname);
115115
blob_appendf(pHdr, "User-Agent: Fossil/" RELEASE_VERSION
116
- "-" MANIFEST_VERSION "\r\n");
116
+ " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
117117
if( g.fHttpTrace ){
118118
blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n");
119119
}else{
120120
blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n");
121121
}
122122
--- src/http.c
+++ src/http.c
@@ -111,11 +111,11 @@
111 fossil_free(zEncoded);
112 fossil_free(zCredentials);
113 }
114 blob_appendf(pHdr, "Host: %s\r\n", g.urlHostname);
115 blob_appendf(pHdr, "User-Agent: Fossil/" RELEASE_VERSION
116 "-" MANIFEST_VERSION "\r\n");
117 if( g.fHttpTrace ){
118 blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n");
119 }else{
120 blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n");
121 }
122
--- src/http.c
+++ src/http.c
@@ -111,11 +111,11 @@
111 fossil_free(zEncoded);
112 fossil_free(zCredentials);
113 }
114 blob_appendf(pHdr, "Host: %s\r\n", g.urlHostname);
115 blob_appendf(pHdr, "User-Agent: Fossil/" RELEASE_VERSION
116 " (" MANIFEST_DATE " " MANIFEST_VERSION ")\r\n");
117 if( g.fHttpTrace ){
118 blob_appendf(pHdr, "Content-Type: application/x-fossil-debug\r\n");
119 }else{
120 blob_appendf(pHdr, "Content-Type: application/x-fossil\r\n");
121 }
122
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
3333
# define sleep Sleep /* windows does not have sleep, but Sleep */
3434
# if defined(__MINGW32__)
3535
# include <ws2tcpip.h>
3636
# endif
3737
#else
38
+# include <netinet/in.h>
3839
# include <arpa/inet.h>
3940
# include <sys/socket.h>
4041
# include <netdb.h>
41
-# include <netinet/in.h>
4242
#endif
4343
#include <assert.h>
4444
#include <sys/types.h>
4545
#include <signal.h>
4646
4747
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
33 # define sleep Sleep /* windows does not have sleep, but Sleep */
34 # if defined(__MINGW32__)
35 # include <ws2tcpip.h>
36 # endif
37 #else
 
38 # include <arpa/inet.h>
39 # include <sys/socket.h>
40 # include <netdb.h>
41 # include <netinet/in.h>
42 #endif
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <signal.h>
46
47
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
33 # define sleep Sleep /* windows does not have sleep, but Sleep */
34 # if defined(__MINGW32__)
35 # include <ws2tcpip.h>
36 # endif
37 #else
38 # include <netinet/in.h>
39 # include <arpa/inet.h>
40 # include <sys/socket.h>
41 # include <netdb.h>
 
42 #endif
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <signal.h>
46
47
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
3333
# define sleep Sleep /* windows does not have sleep, but Sleep */
3434
# if defined(__MINGW32__)
3535
# include <ws2tcpip.h>
3636
# endif
3737
#else
38
+# include <netinet/in.h>
3839
# include <arpa/inet.h>
3940
# include <sys/socket.h>
4041
# include <netdb.h>
41
-# include <netinet/in.h>
4242
#endif
4343
#include <assert.h>
4444
#include <sys/types.h>
4545
#include <signal.h>
4646
4747
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
33 # define sleep Sleep /* windows does not have sleep, but Sleep */
34 # if defined(__MINGW32__)
35 # include <ws2tcpip.h>
36 # endif
37 #else
 
38 # include <arpa/inet.h>
39 # include <sys/socket.h>
40 # include <netdb.h>
41 # include <netinet/in.h>
42 #endif
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <signal.h>
46
47
--- src/http_socket.c
+++ src/http_socket.c
@@ -33,14 +33,14 @@
33 # define sleep Sleep /* windows does not have sleep, but Sleep */
34 # if defined(__MINGW32__)
35 # include <ws2tcpip.h>
36 # endif
37 #else
38 # include <netinet/in.h>
39 # include <arpa/inet.h>
40 # include <sys/socket.h>
41 # include <netdb.h>
 
42 #endif
43 #include <assert.h>
44 #include <sys/types.h>
45 #include <signal.h>
46
47
+12 -6
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -193,11 +193,10 @@
193193
*/
194194
int ssl_open(void){
195195
X509 *cert;
196196
int hasSavedCertificate = 0;
197197
int trusted = 0;
198
- char *connStr ;
199198
unsigned long e;
200199
201200
ssl_global_init();
202201
203202
/* Get certificate for current server from global config and
@@ -210,20 +209,27 @@
210209
hasSavedCertificate = 1;
211210
}
212211
213212
iBio = BIO_new_ssl_connect(sslCtx);
214213
BIO_get_ssl(iBio, &ssl);
214
+
215
+#if (SSLEAY_VERSION_NUMBER >= 0x00908070) && !defined(OPENSSL_NO_TLSEXT)
216
+ if( !SSL_set_tlsext_host_name(ssl, g.urlName) ){
217
+ fossil_warning("WARNING: failed to set server name indication (SNI), "
218
+ "continuing without it.\n");
219
+ }
220
+#endif
221
+
215222
SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
216223
if( iBio==NULL ) {
217224
ssl_set_errmsg("SSL: cannot open SSL (%s)",
218225
ERR_reason_error_string(ERR_get_error()));
219
- return 1;
226
+ return 1;
220227
}
221
-
222
- connStr = mprintf("%s:%d", g.urlName, g.urlPort);
223
- BIO_set_conn_hostname(iBio, connStr);
224
- free(connStr);
228
+
229
+ BIO_set_conn_hostname(iBio, g.urlName);
230
+ BIO_set_conn_int_port(iBio, &g.urlPort);
225231
226232
if( BIO_do_connect(iBio)<=0 ){
227233
ssl_set_errmsg("SSL: cannot connect to host %s:%d (%s)",
228234
g.urlName, g.urlPort, ERR_reason_error_string(ERR_get_error()));
229235
ssl_close();
230236
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -193,11 +193,10 @@
193 */
194 int ssl_open(void){
195 X509 *cert;
196 int hasSavedCertificate = 0;
197 int trusted = 0;
198 char *connStr ;
199 unsigned long e;
200
201 ssl_global_init();
202
203 /* Get certificate for current server from global config and
@@ -210,20 +209,27 @@
210 hasSavedCertificate = 1;
211 }
212
213 iBio = BIO_new_ssl_connect(sslCtx);
214 BIO_get_ssl(iBio, &ssl);
 
 
 
 
 
 
 
 
215 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
216 if( iBio==NULL ) {
217 ssl_set_errmsg("SSL: cannot open SSL (%s)",
218 ERR_reason_error_string(ERR_get_error()));
219 return 1;
220 }
221
222 connStr = mprintf("%s:%d", g.urlName, g.urlPort);
223 BIO_set_conn_hostname(iBio, connStr);
224 free(connStr);
225
226 if( BIO_do_connect(iBio)<=0 ){
227 ssl_set_errmsg("SSL: cannot connect to host %s:%d (%s)",
228 g.urlName, g.urlPort, ERR_reason_error_string(ERR_get_error()));
229 ssl_close();
230
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -193,11 +193,10 @@
193 */
194 int ssl_open(void){
195 X509 *cert;
196 int hasSavedCertificate = 0;
197 int trusted = 0;
 
198 unsigned long e;
199
200 ssl_global_init();
201
202 /* Get certificate for current server from global config and
@@ -210,20 +209,27 @@
209 hasSavedCertificate = 1;
210 }
211
212 iBio = BIO_new_ssl_connect(sslCtx);
213 BIO_get_ssl(iBio, &ssl);
214
215 #if (SSLEAY_VERSION_NUMBER >= 0x00908070) && !defined(OPENSSL_NO_TLSEXT)
216 if( !SSL_set_tlsext_host_name(ssl, g.urlName) ){
217 fossil_warning("WARNING: failed to set server name indication (SNI), "
218 "continuing without it.\n");
219 }
220 #endif
221
222 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
223 if( iBio==NULL ) {
224 ssl_set_errmsg("SSL: cannot open SSL (%s)",
225 ERR_reason_error_string(ERR_get_error()));
226 return 1;
227 }
228
229 BIO_set_conn_hostname(iBio, g.urlName);
230 BIO_set_conn_int_port(iBio, &g.urlPort);
 
231
232 if( BIO_do_connect(iBio)<=0 ){
233 ssl_set_errmsg("SSL: cannot connect to host %s:%d (%s)",
234 g.urlName, g.urlPort, ERR_reason_error_string(ERR_get_error()));
235 ssl_close();
236
+18 -2
--- src/json_login.c
+++ src/json_login.c
@@ -155,13 +155,29 @@
155155
payload = cson_value_new_object();
156156
po = cson_value_get_object(payload);
157157
cson_object_set(po, "authToken", json_new_string(cookie));
158158
free(cookie);
159159
cson_object_set(po, "name", json_new_string(name));
160
- cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q",name);
161
- cson_object_set(po, "capabilities", json_new_string(cap));
160
+ cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q", name);
161
+ cson_object_set(po, "capabilities", cap ? json_new_string(cap) : cson_value_null() );
162162
free(cap);
163
+ cson_object_set(po, "loginCookieName", json_new_string( login_cookie_name() ) );
164
+ /* TODO: add loginExpiryTime to the payload. To do this properly
165
+ we "should" add an ([unsigned] int *) to
166
+ login_set_user_cookie() and login_set_anon_cookie(), to which
167
+ the expiry time is assigned. (Remember that JSON doesn't do
168
+ unsigned int.)
169
+
170
+ For non-anonymous users we could also simply query the
171
+ user.cexpire db field after calling login_set_user_cookie(),
172
+ but for anonymous we need to get the time when the cookie is
173
+ set because anon does not get a db entry like normal users
174
+ do. Anonyous cookies currently have a hard-coded lifetime in
175
+ login_set_anon_cookie() (currently 6 hours), which we "should
176
+ arguably" change to use the time configured for non-anonymous
177
+ users (see login_set_user_cookie() for details).
178
+ */
163179
return payload;
164180
}
165181
}
166182
167183
/*
168184
--- src/json_login.c
+++ src/json_login.c
@@ -155,13 +155,29 @@
155 payload = cson_value_new_object();
156 po = cson_value_get_object(payload);
157 cson_object_set(po, "authToken", json_new_string(cookie));
158 free(cookie);
159 cson_object_set(po, "name", json_new_string(name));
160 cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q",name);
161 cson_object_set(po, "capabilities", json_new_string(cap));
162 free(cap);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163 return payload;
164 }
165 }
166
167 /*
168
--- src/json_login.c
+++ src/json_login.c
@@ -155,13 +155,29 @@
155 payload = cson_value_new_object();
156 po = cson_value_get_object(payload);
157 cson_object_set(po, "authToken", json_new_string(cookie));
158 free(cookie);
159 cson_object_set(po, "name", json_new_string(name));
160 cap = db_text(NULL, "SELECT cap FROM user WHERE login=%Q", name);
161 cson_object_set(po, "capabilities", cap ? json_new_string(cap) : cson_value_null() );
162 free(cap);
163 cson_object_set(po, "loginCookieName", json_new_string( login_cookie_name() ) );
164 /* TODO: add loginExpiryTime to the payload. To do this properly
165 we "should" add an ([unsigned] int *) to
166 login_set_user_cookie() and login_set_anon_cookie(), to which
167 the expiry time is assigned. (Remember that JSON doesn't do
168 unsigned int.)
169
170 For non-anonymous users we could also simply query the
171 user.cexpire db field after calling login_set_user_cookie(),
172 but for anonymous we need to get the time when the cookie is
173 set because anon does not get a db entry like normal users
174 do. Anonyous cookies currently have a hard-coded lifetime in
175 login_set_anon_cookie() (currently 6 hours), which we "should
176 arguably" change to use the time configured for non-anonymous
177 users (see login_set_user_cookie() for details).
178 */
179 return payload;
180 }
181 }
182
183 /*
184
+31 -6
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352352
** downstream problems here. We could alternately use "" here.
353353
*/
354354
;
355355
}
356356
}
357
+
358
+/*
359
+** Return true if the prefix of zStr matches zPattern. Return false if
360
+** they are different.
361
+**
362
+** A lowercase character in zPattern will match either upper or lower
363
+** case in zStr. But an uppercase in zPattern will only match an
364
+** uppercase in zStr.
365
+*/
366
+static int prefix_match(const char *zPattern, const char *zStr){
367
+ int i;
368
+ char c;
369
+ for(i=0; (c = zPattern[i])!=0; i++){
370
+ if( zStr[i]!=c && fossil_tolower(zStr[i])!=c ) return 0;
371
+ }
372
+ return 1;
373
+}
357374
358375
/*
359376
** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
360
-** is a manually operated browser or a bot. When in doubt, assume a bot. Return
361
-** true if we believe the agent is a real person.
377
+** is a manually operated browser or a bot. When in doubt, assume a bot.
378
+** Return true if we believe the agent is a real person.
362379
*/
363380
static int isHuman(const char *zAgent){
364381
int i;
365
- if( zAgent==0 ) return 0;
382
+ if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
366383
for(i=0; zAgent[i]; i++){
367
- if( zAgent[i]=='b' && memcmp(&zAgent[i],"bot",3)==0 ) return 0;
368
- if( zAgent[i]=='s' && memcmp(&zAgent[i],"spider",6)==0 ) return 0;
384
+ if( prefix_match("bot", zAgent+i) ) return 0;
385
+ if( prefix_match("spider", zAgent+i) ) return 0;
386
+ if( prefix_match("crawl", zAgent+i) ) return 0;
387
+ /* If a URI appears in the User-Agent, it is probably a bot */
388
+ if( memcmp("http", zAgent+i,4)==0 ) return 0;
369389
}
370390
if( memcmp(zAgent, "Mozilla/", 8)==0 ){
371
- return atoi(&zAgent[8])>=4;
391
+ if( atoi(&zAgent[8])<4 ) return 0; /* Many bots advertise as Mozilla/3 */
392
+ if( strglob("*Firefox/[1-9]*", zAgent) ) return 1;
393
+ if( strglob("*Chrome/[1-9]*", zAgent) ) return 1;
394
+ if( strglob("*(compatible;?MSIE?[1-9]*", zAgent) ) return 1;
395
+ if( strglob("*AppleWebKit/[1-9]*(KHTML*", zAgent) ) return 1;
396
+ return 0;
372397
}
373398
if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
374399
if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
375400
if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
376401
return 0;
377402
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352 ** downstream problems here. We could alternately use "" here.
353 */
354 ;
355 }
356 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
358 /*
359 ** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
360 ** is a manually operated browser or a bot. When in doubt, assume a bot. Return
361 ** true if we believe the agent is a real person.
362 */
363 static int isHuman(const char *zAgent){
364 int i;
365 if( zAgent==0 ) return 0;
366 for(i=0; zAgent[i]; i++){
367 if( zAgent[i]=='b' && memcmp(&zAgent[i],"bot",3)==0 ) return 0;
368 if( zAgent[i]=='s' && memcmp(&zAgent[i],"spider",6)==0 ) return 0;
 
 
 
369 }
370 if( memcmp(zAgent, "Mozilla/", 8)==0 ){
371 return atoi(&zAgent[8])>=4;
 
 
 
 
 
372 }
373 if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
374 if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
375 if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
376 return 0;
377
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352 ** downstream problems here. We could alternately use "" here.
353 */
354 ;
355 }
356 }
357
358 /*
359 ** Return true if the prefix of zStr matches zPattern. Return false if
360 ** they are different.
361 **
362 ** A lowercase character in zPattern will match either upper or lower
363 ** case in zStr. But an uppercase in zPattern will only match an
364 ** uppercase in zStr.
365 */
366 static int prefix_match(const char *zPattern, const char *zStr){
367 int i;
368 char c;
369 for(i=0; (c = zPattern[i])!=0; i++){
370 if( zStr[i]!=c && fossil_tolower(zStr[i])!=c ) return 0;
371 }
372 return 1;
373 }
374
375 /*
376 ** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
377 ** is a manually operated browser or a bot. When in doubt, assume a bot.
378 ** Return true if we believe the agent is a real person.
379 */
380 static int isHuman(const char *zAgent){
381 int i;
382 if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
383 for(i=0; zAgent[i]; i++){
384 if( prefix_match("bot", zAgent+i) ) return 0;
385 if( prefix_match("spider", zAgent+i) ) return 0;
386 if( prefix_match("crawl", zAgent+i) ) return 0;
387 /* If a URI appears in the User-Agent, it is probably a bot */
388 if( memcmp("http", zAgent+i,4)==0 ) return 0;
389 }
390 if( memcmp(zAgent, "Mozilla/", 8)==0 ){
391 if( atoi(&zAgent[8])<4 ) return 0; /* Many bots advertise as Mozilla/3 */
392 if( strglob("*Firefox/[1-9]*", zAgent) ) return 1;
393 if( strglob("*Chrome/[1-9]*", zAgent) ) return 1;
394 if( strglob("*(compatible;?MSIE?[1-9]*", zAgent) ) return 1;
395 if( strglob("*AppleWebKit/[1-9]*(KHTML*", zAgent) ) return 1;
396 return 0;
397 }
398 if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
399 if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
400 if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
401 return 0;
402
+31 -6
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352352
** downstream problems here. We could alternately use "" here.
353353
*/
354354
;
355355
}
356356
}
357
+
358
+/*
359
+** Return true if the prefix of zStr matches zPattern. Return false if
360
+** they are different.
361
+**
362
+** A lowercase character in zPattern will match either upper or lower
363
+** case in zStr. But an uppercase in zPattern will only match an
364
+** uppercase in zStr.
365
+*/
366
+static int prefix_match(const char *zPattern, const char *zStr){
367
+ int i;
368
+ char c;
369
+ for(i=0; (c = zPattern[i])!=0; i++){
370
+ if( zStr[i]!=c && fossil_tolower(zStr[i])!=c ) return 0;
371
+ }
372
+ return 1;
373
+}
357374
358375
/*
359376
** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
360
-** is a manually operated browser or a bot. When in doubt, assume a bot. Return
361
-** true if we believe the agent is a real person.
377
+** is a manually operated browser or a bot. When in doubt, assume a bot.
378
+** Return true if we believe the agent is a real person.
362379
*/
363380
static int isHuman(const char *zAgent){
364381
int i;
365
- if( zAgent==0 ) return 0;
382
+ if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
366383
for(i=0; zAgent[i]; i++){
367
- if( zAgent[i]=='b' && memcmp(&zAgent[i],"bot",3)==0 ) return 0;
368
- if( zAgent[i]=='s' && memcmp(&zAgent[i],"spider",6)==0 ) return 0;
384
+ if( prefix_match("bot", zAgent+i) ) return 0;
385
+ if( prefix_match("spider", zAgent+i) ) return 0;
386
+ if( prefix_match("crawl", zAgent+i) ) return 0;
387
+ /* If a URI appears in the User-Agent, it is probably a bot */
388
+ if( memcmp("http", zAgent+i,4)==0 ) return 0;
369389
}
370390
if( memcmp(zAgent, "Mozilla/", 8)==0 ){
371
- return atoi(&zAgent[8])>=4;
391
+ if( atoi(&zAgent[8])<4 ) return 0; /* Many bots advertise as Mozilla/3 */
392
+ if( strglob("*Firefox/[1-9]*", zAgent) ) return 1;
393
+ if( strglob("*Chrome/[1-9]*", zAgent) ) return 1;
394
+ if( strglob("*(compatible;?MSIE?[1-9]*", zAgent) ) return 1;
395
+ if( strglob("*AppleWebKit/[1-9]*(KHTML*", zAgent) ) return 1;
396
+ return 0;
372397
}
373398
if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
374399
if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
375400
if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
376401
return 0;
377402
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352 ** downstream problems here. We could alternately use "" here.
353 */
354 ;
355 }
356 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
358 /*
359 ** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
360 ** is a manually operated browser or a bot. When in doubt, assume a bot. Return
361 ** true if we believe the agent is a real person.
362 */
363 static int isHuman(const char *zAgent){
364 int i;
365 if( zAgent==0 ) return 0;
366 for(i=0; zAgent[i]; i++){
367 if( zAgent[i]=='b' && memcmp(&zAgent[i],"bot",3)==0 ) return 0;
368 if( zAgent[i]=='s' && memcmp(&zAgent[i],"spider",6)==0 ) return 0;
 
 
 
369 }
370 if( memcmp(zAgent, "Mozilla/", 8)==0 ){
371 return atoi(&zAgent[8])>=4;
 
 
 
 
 
372 }
373 if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
374 if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
375 if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
376 return 0;
377
--- src/login.c
+++ src/login.c
@@ -352,25 +352,50 @@
352 ** downstream problems here. We could alternately use "" here.
353 */
354 ;
355 }
356 }
357
358 /*
359 ** Return true if the prefix of zStr matches zPattern. Return false if
360 ** they are different.
361 **
362 ** A lowercase character in zPattern will match either upper or lower
363 ** case in zStr. But an uppercase in zPattern will only match an
364 ** uppercase in zStr.
365 */
366 static int prefix_match(const char *zPattern, const char *zStr){
367 int i;
368 char c;
369 for(i=0; (c = zPattern[i])!=0; i++){
370 if( zStr[i]!=c && fossil_tolower(zStr[i])!=c ) return 0;
371 }
372 return 1;
373 }
374
375 /*
376 ** Look at the HTTP_USER_AGENT parameter and try to determine if the user agent
377 ** is a manually operated browser or a bot. When in doubt, assume a bot.
378 ** Return true if we believe the agent is a real person.
379 */
380 static int isHuman(const char *zAgent){
381 int i;
382 if( zAgent==0 ) return 0; /* If not UserAgent, the probably a bot */
383 for(i=0; zAgent[i]; i++){
384 if( prefix_match("bot", zAgent+i) ) return 0;
385 if( prefix_match("spider", zAgent+i) ) return 0;
386 if( prefix_match("crawl", zAgent+i) ) return 0;
387 /* If a URI appears in the User-Agent, it is probably a bot */
388 if( memcmp("http", zAgent+i,4)==0 ) return 0;
389 }
390 if( memcmp(zAgent, "Mozilla/", 8)==0 ){
391 if( atoi(&zAgent[8])<4 ) return 0; /* Many bots advertise as Mozilla/3 */
392 if( strglob("*Firefox/[1-9]*", zAgent) ) return 1;
393 if( strglob("*Chrome/[1-9]*", zAgent) ) return 1;
394 if( strglob("*(compatible;?MSIE?[1-9]*", zAgent) ) return 1;
395 if( strglob("*AppleWebKit/[1-9]*(KHTML*", zAgent) ) return 1;
396 return 0;
397 }
398 if( memcmp(zAgent, "Opera/", 6)==0 ) return 1;
399 if( memcmp(zAgent, "Safari/", 7)==0 ) return 1;
400 if( memcmp(zAgent, "Lynx/", 5)==0 ) return 1;
401 return 0;
402
--- src/manifest.c
+++ src/manifest.c
@@ -1223,10 +1223,38 @@
12231223
fetch_baseline(p, 1);
12241224
pFile = manifest_file_seek_base(p->pBaseline, zName);
12251225
}
12261226
return pFile;
12271227
}
1228
+
1229
+/*
1230
+** Look for a file in a manifest, taking the case-sensitive option
1231
+** into account. If case-sensitive is off, then files in any case
1232
+** will match.
1233
+*/
1234
+ManifestFile *manifest_file_find(Manifest *p, const char *zName){
1235
+ int i;
1236
+ Manifest *pBase;
1237
+ if( filenames_are_case_sensitive() ){
1238
+ return manifest_file_seek(p, zName);
1239
+ }
1240
+ for(i=0; i<p->nFile; i++){
1241
+ if( fossil_stricmp(zName, p->aFile[i].zName)==0 ){
1242
+ return &p->aFile[i];
1243
+ }
1244
+ }
1245
+ if( p->zBaseline==0 ) return 0;
1246
+ fetch_baseline(p, 1);
1247
+ pBase = p->pBaseline;
1248
+ if( pBase==0 ) return 0;
1249
+ for(i=0; i<pBase->nFile; i++){
1250
+ if( fossil_stricmp(zName, p->aFile[i].zName)==0 ){
1251
+ return &p->aFile[i];
1252
+ }
1253
+ }
1254
+ return 0;
1255
+}
12281256
12291257
/*
12301258
** Add mlink table entries associated with manifest cid, pChild. The
12311259
** parent manifest is pid, pParent. One of either pChild or pParent
12321260
** will be NULL and it will be computed based on cid/pid.
12331261
--- src/manifest.c
+++ src/manifest.c
@@ -1223,10 +1223,38 @@
1223 fetch_baseline(p, 1);
1224 pFile = manifest_file_seek_base(p->pBaseline, zName);
1225 }
1226 return pFile;
1227 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1228
1229 /*
1230 ** Add mlink table entries associated with manifest cid, pChild. The
1231 ** parent manifest is pid, pParent. One of either pChild or pParent
1232 ** will be NULL and it will be computed based on cid/pid.
1233
--- src/manifest.c
+++ src/manifest.c
@@ -1223,10 +1223,38 @@
1223 fetch_baseline(p, 1);
1224 pFile = manifest_file_seek_base(p->pBaseline, zName);
1225 }
1226 return pFile;
1227 }
1228
1229 /*
1230 ** Look for a file in a manifest, taking the case-sensitive option
1231 ** into account. If case-sensitive is off, then files in any case
1232 ** will match.
1233 */
1234 ManifestFile *manifest_file_find(Manifest *p, const char *zName){
1235 int i;
1236 Manifest *pBase;
1237 if( filenames_are_case_sensitive() ){
1238 return manifest_file_seek(p, zName);
1239 }
1240 for(i=0; i<p->nFile; i++){
1241 if( fossil_stricmp(zName, p->aFile[i].zName)==0 ){
1242 return &p->aFile[i];
1243 }
1244 }
1245 if( p->zBaseline==0 ) return 0;
1246 fetch_baseline(p, 1);
1247 pBase = p->pBaseline;
1248 if( pBase==0 ) return 0;
1249 for(i=0; i<pBase->nFile; i++){
1250 if( fossil_stricmp(zName, p->aFile[i].zName)==0 ){
1251 return &p->aFile[i];
1252 }
1253 }
1254 return 0;
1255 }
1256
1257 /*
1258 ** Add mlink table entries associated with manifest cid, pChild. The
1259 ** parent manifest is pid, pParent. One of either pChild or pParent
1260 ** will be NULL and it will be computed based on cid/pid.
1261
+1 -1
--- src/setup.c
+++ src/setup.c
@@ -136,11 +136,11 @@
136136
if( g.perm.Admin ){
137137
@ </a>
138138
}
139139
@ </td>
140140
@ <td class="usetupListCap" style="text-align: center;padding-right: 15px;">%s(zCap)</td>
141
- @ <td class="usetupListCon" style="text-align: left;">%s(db_column_text(&s,3))</td>
141
+ @ <td class="usetupListCon" style="text-align: left;">%h(db_column_text(&s,3))</td>
142142
@ </tr>
143143
}
144144
@ </table>
145145
@ </td><td class="usetupColumnLayout">
146146
@ <span class="note">Notes:</span>
147147
--- src/setup.c
+++ src/setup.c
@@ -136,11 +136,11 @@
136 if( g.perm.Admin ){
137 @ </a>
138 }
139 @ </td>
140 @ <td class="usetupListCap" style="text-align: center;padding-right: 15px;">%s(zCap)</td>
141 @ <td class="usetupListCon" style="text-align: left;">%s(db_column_text(&s,3))</td>
142 @ </tr>
143 }
144 @ </table>
145 @ </td><td class="usetupColumnLayout">
146 @ <span class="note">Notes:</span>
147
--- src/setup.c
+++ src/setup.c
@@ -136,11 +136,11 @@
136 if( g.perm.Admin ){
137 @ </a>
138 }
139 @ </td>
140 @ <td class="usetupListCap" style="text-align: center;padding-right: 15px;">%s(zCap)</td>
141 @ <td class="usetupListCon" style="text-align: left;">%h(db_column_text(&s,3))</td>
142 @ </tr>
143 }
144 @ </table>
145 @ </td><td class="usetupColumnLayout">
146 @ <span class="note">Notes:</span>
147
+27 -6
--- src/shell.c
+++ src/shell.c
@@ -1124,10 +1124,19 @@
11241124
/* echo the sql statement if echo on */
11251125
if( pArg && pArg->echoOn ){
11261126
const char *zStmtSql = sqlite3_sql(pStmt);
11271127
fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
11281128
}
1129
+
1130
+ /* Output TESTCTRL_EXPLAIN text of requested */
1131
+ if( pArg && pArg->mode==MODE_Explain ){
1132
+ const char *zExplain = 0;
1133
+ sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1134
+ if( zExplain && zExplain[0] ){
1135
+ fprintf(pArg->out, "%s", zExplain);
1136
+ }
1137
+ }
11291138
11301139
/* perform the first step. this will tell us if we
11311140
** have a result set or not and how wide it is.
11321141
*/
11331142
rc = sqlite3_step(pStmt);
@@ -1394,10 +1403,11 @@
13941403
".stats ON|OFF Turn stats on or off\n"
13951404
".tables ?TABLE? List names of tables\n"
13961405
" If TABLE specified, only list tables matching\n"
13971406
" LIKE pattern TABLE.\n"
13981407
".timeout MS Try opening locked tables for MS milliseconds\n"
1408
+ ".vfsname ?AUX? Print the name of the VFS stack\n"
13991409
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
14001410
;
14011411
14021412
static char zTimerHelp[] =
14031413
".timer ON|OFF Turn the CPU timer measurement on or off\n"
@@ -2083,11 +2093,12 @@
20832093
rc = sqlite3_exec(p->db,
20842094
"SELECT sql FROM "
20852095
" (SELECT sql sql, type type, tbl_name tbl_name, name name"
20862096
" FROM sqlite_master UNION ALL"
20872097
" SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
2088
- "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
2098
+ "WHERE lower(tbl_name) LIKE shellstatic()"
2099
+ " AND type!='meta' AND sql NOTNULL "
20892100
"ORDER BY substr(type,2,1), name",
20902101
callback, &data, &zErrMsg);
20912102
zShellStatic = 0;
20922103
}
20932104
}else{
@@ -2217,11 +2228,10 @@
22172228
{ "assert", SQLITE_TESTCTRL_ASSERT },
22182229
{ "always", SQLITE_TESTCTRL_ALWAYS },
22192230
{ "reserve", SQLITE_TESTCTRL_RESERVE },
22202231
{ "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
22212232
{ "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
2222
- { "pghdrsz", SQLITE_TESTCTRL_PGHDRSZ },
22232233
{ "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
22242234
};
22252235
int testctrl = -1;
22262236
int rc = 0;
22272237
int i, n;
@@ -2262,11 +2272,10 @@
22622272
22632273
/* sqlite3_test_control(int) */
22642274
case SQLITE_TESTCTRL_PRNG_SAVE:
22652275
case SQLITE_TESTCTRL_PRNG_RESTORE:
22662276
case SQLITE_TESTCTRL_PRNG_RESET:
2267
- case SQLITE_TESTCTRL_PGHDRSZ:
22682277
if( nArg==2 ){
22692278
rc = sqlite3_test_control(testctrl);
22702279
printf("%d (0x%08x)\n", rc, rc);
22712280
} else {
22722281
fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
@@ -2334,13 +2343,25 @@
23342343
){
23352344
enableTimer = booleanValue(azArg[1]);
23362345
}else
23372346
23382347
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
2339
- printf("SQLite %s %s\n",
2348
+ printf("SQLite %s %s\n" /*extra-version-info*/,
23402349
sqlite3_libversion(), sqlite3_sourceid());
23412350
}else
2351
+
2352
+ if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
2353
+ const char *zDbName = nArg==2 ? azArg[1] : "main";
2354
+ char *zVfsName = 0;
2355
+ if( p->db ){
2356
+ sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
2357
+ if( zVfsName ){
2358
+ printf("%s\n", zVfsName);
2359
+ sqlite3_free(zVfsName);
2360
+ }
2361
+ }
2362
+ }else
23422363
23432364
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
23442365
int j;
23452366
assert( nArg<=ArraySize(azArg) );
23462367
for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
@@ -2794,11 +2815,11 @@
27942815
#ifndef SQLITE_OMIT_MEMORYDB
27952816
data.zDbFilename = ":memory:";
27962817
#else
27972818
data.zDbFilename = 0;
27982819
#endif
2799
- /***** Begin Fossil Patch *****/
2820
+ /***** Begin Fossil Patch *****/
28002821
{
28012822
extern void fossil_open(const char **);
28022823
fossil_open(&data.zDbFilename);
28032824
}
28042825
/***** End Fossil Patch *****/
@@ -2937,11 +2958,11 @@
29372958
if( stdin_is_interactive ){
29382959
char *zHome;
29392960
char *zHistory = 0;
29402961
int nHistory;
29412962
printf(
2942
- "SQLite version %s %.19s\n"
2963
+ "SQLite version %s %.19s\n" /*extra-version-info*/
29432964
"Enter \".help\" for instructions\n"
29442965
"Enter SQL statements terminated with a \";\"\n",
29452966
sqlite3_libversion(), sqlite3_sourceid()
29462967
);
29472968
zHome = find_home_dir();
29482969
--- src/shell.c
+++ src/shell.c
@@ -1124,10 +1124,19 @@
1124 /* echo the sql statement if echo on */
1125 if( pArg && pArg->echoOn ){
1126 const char *zStmtSql = sqlite3_sql(pStmt);
1127 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1128 }
 
 
 
 
 
 
 
 
 
1129
1130 /* perform the first step. this will tell us if we
1131 ** have a result set or not and how wide it is.
1132 */
1133 rc = sqlite3_step(pStmt);
@@ -1394,10 +1403,11 @@
1394 ".stats ON|OFF Turn stats on or off\n"
1395 ".tables ?TABLE? List names of tables\n"
1396 " If TABLE specified, only list tables matching\n"
1397 " LIKE pattern TABLE.\n"
1398 ".timeout MS Try opening locked tables for MS milliseconds\n"
 
1399 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1400 ;
1401
1402 static char zTimerHelp[] =
1403 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
@@ -2083,11 +2093,12 @@
2083 rc = sqlite3_exec(p->db,
2084 "SELECT sql FROM "
2085 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
2086 " FROM sqlite_master UNION ALL"
2087 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
2088 "WHERE tbl_name LIKE shellstatic() AND type!='meta' AND sql NOTNULL "
 
2089 "ORDER BY substr(type,2,1), name",
2090 callback, &data, &zErrMsg);
2091 zShellStatic = 0;
2092 }
2093 }else{
@@ -2217,11 +2228,10 @@
2217 { "assert", SQLITE_TESTCTRL_ASSERT },
2218 { "always", SQLITE_TESTCTRL_ALWAYS },
2219 { "reserve", SQLITE_TESTCTRL_RESERVE },
2220 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
2221 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
2222 { "pghdrsz", SQLITE_TESTCTRL_PGHDRSZ },
2223 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
2224 };
2225 int testctrl = -1;
2226 int rc = 0;
2227 int i, n;
@@ -2262,11 +2272,10 @@
2262
2263 /* sqlite3_test_control(int) */
2264 case SQLITE_TESTCTRL_PRNG_SAVE:
2265 case SQLITE_TESTCTRL_PRNG_RESTORE:
2266 case SQLITE_TESTCTRL_PRNG_RESET:
2267 case SQLITE_TESTCTRL_PGHDRSZ:
2268 if( nArg==2 ){
2269 rc = sqlite3_test_control(testctrl);
2270 printf("%d (0x%08x)\n", rc, rc);
2271 } else {
2272 fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
@@ -2334,13 +2343,25 @@
2334 ){
2335 enableTimer = booleanValue(azArg[1]);
2336 }else
2337
2338 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
2339 printf("SQLite %s %s\n",
2340 sqlite3_libversion(), sqlite3_sourceid());
2341 }else
 
 
 
 
 
 
 
 
 
 
 
 
2342
2343 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2344 int j;
2345 assert( nArg<=ArraySize(azArg) );
2346 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
@@ -2794,11 +2815,11 @@
2794 #ifndef SQLITE_OMIT_MEMORYDB
2795 data.zDbFilename = ":memory:";
2796 #else
2797 data.zDbFilename = 0;
2798 #endif
2799 /***** Begin Fossil Patch *****/
2800 {
2801 extern void fossil_open(const char **);
2802 fossil_open(&data.zDbFilename);
2803 }
2804 /***** End Fossil Patch *****/
@@ -2937,11 +2958,11 @@
2937 if( stdin_is_interactive ){
2938 char *zHome;
2939 char *zHistory = 0;
2940 int nHistory;
2941 printf(
2942 "SQLite version %s %.19s\n"
2943 "Enter \".help\" for instructions\n"
2944 "Enter SQL statements terminated with a \";\"\n",
2945 sqlite3_libversion(), sqlite3_sourceid()
2946 );
2947 zHome = find_home_dir();
2948
--- src/shell.c
+++ src/shell.c
@@ -1124,10 +1124,19 @@
1124 /* echo the sql statement if echo on */
1125 if( pArg && pArg->echoOn ){
1126 const char *zStmtSql = sqlite3_sql(pStmt);
1127 fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
1128 }
1129
1130 /* Output TESTCTRL_EXPLAIN text of requested */
1131 if( pArg && pArg->mode==MODE_Explain ){
1132 const char *zExplain = 0;
1133 sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT, pStmt, &zExplain);
1134 if( zExplain && zExplain[0] ){
1135 fprintf(pArg->out, "%s", zExplain);
1136 }
1137 }
1138
1139 /* perform the first step. this will tell us if we
1140 ** have a result set or not and how wide it is.
1141 */
1142 rc = sqlite3_step(pStmt);
@@ -1394,10 +1403,11 @@
1403 ".stats ON|OFF Turn stats on or off\n"
1404 ".tables ?TABLE? List names of tables\n"
1405 " If TABLE specified, only list tables matching\n"
1406 " LIKE pattern TABLE.\n"
1407 ".timeout MS Try opening locked tables for MS milliseconds\n"
1408 ".vfsname ?AUX? Print the name of the VFS stack\n"
1409 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1410 ;
1411
1412 static char zTimerHelp[] =
1413 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
@@ -2083,11 +2093,12 @@
2093 rc = sqlite3_exec(p->db,
2094 "SELECT sql FROM "
2095 " (SELECT sql sql, type type, tbl_name tbl_name, name name"
2096 " FROM sqlite_master UNION ALL"
2097 " SELECT sql, type, tbl_name, name FROM sqlite_temp_master) "
2098 "WHERE lower(tbl_name) LIKE shellstatic()"
2099 " AND type!='meta' AND sql NOTNULL "
2100 "ORDER BY substr(type,2,1), name",
2101 callback, &data, &zErrMsg);
2102 zShellStatic = 0;
2103 }
2104 }else{
@@ -2217,11 +2228,10 @@
2228 { "assert", SQLITE_TESTCTRL_ASSERT },
2229 { "always", SQLITE_TESTCTRL_ALWAYS },
2230 { "reserve", SQLITE_TESTCTRL_RESERVE },
2231 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
2232 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
 
2233 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
2234 };
2235 int testctrl = -1;
2236 int rc = 0;
2237 int i, n;
@@ -2262,11 +2272,10 @@
2272
2273 /* sqlite3_test_control(int) */
2274 case SQLITE_TESTCTRL_PRNG_SAVE:
2275 case SQLITE_TESTCTRL_PRNG_RESTORE:
2276 case SQLITE_TESTCTRL_PRNG_RESET:
 
2277 if( nArg==2 ){
2278 rc = sqlite3_test_control(testctrl);
2279 printf("%d (0x%08x)\n", rc, rc);
2280 } else {
2281 fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
@@ -2334,13 +2343,25 @@
2343 ){
2344 enableTimer = booleanValue(azArg[1]);
2345 }else
2346
2347 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
2348 printf("SQLite %s %s\n" /*extra-version-info*/,
2349 sqlite3_libversion(), sqlite3_sourceid());
2350 }else
2351
2352 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
2353 const char *zDbName = nArg==2 ? azArg[1] : "main";
2354 char *zVfsName = 0;
2355 if( p->db ){
2356 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
2357 if( zVfsName ){
2358 printf("%s\n", zVfsName);
2359 sqlite3_free(zVfsName);
2360 }
2361 }
2362 }else
2363
2364 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2365 int j;
2366 assert( nArg<=ArraySize(azArg) );
2367 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
@@ -2794,11 +2815,11 @@
2815 #ifndef SQLITE_OMIT_MEMORYDB
2816 data.zDbFilename = ":memory:";
2817 #else
2818 data.zDbFilename = 0;
2819 #endif
2820 /***** Begin Fossil Patch *****/
2821 {
2822 extern void fossil_open(const char **);
2823 fossil_open(&data.zDbFilename);
2824 }
2825 /***** End Fossil Patch *****/
@@ -2937,11 +2958,11 @@
2958 if( stdin_is_interactive ){
2959 char *zHome;
2960 char *zHistory = 0;
2961 int nHistory;
2962 printf(
2963 "SQLite version %s %.19s\n" /*extra-version-info*/
2964 "Enter \".help\" for instructions\n"
2965 "Enter SQL statements terminated with a \";\"\n",
2966 sqlite3_libversion(), sqlite3_sourceid()
2967 );
2968 zHome = find_home_dir();
2969
+4549 -2835
--- 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.9. By combining all the individual C code files into this
3
+** version 3.7.10. 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.
@@ -363,10 +363,18 @@
363363
#else
364364
# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
365365
#endif
366366
#endif
367367
368
+/*
369
+** Powersafe overwrite is on by default. But can be turned off using
370
+** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
371
+*/
372
+#ifndef SQLITE_POWERSAFE_OVERWRITE
373
+# define SQLITE_POWERSAFE_OVERWRITE 1
374
+#endif
375
+
368376
/*
369377
** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
370378
** It determines whether or not the features related to
371379
** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
372380
** be overridden at runtime using the sqlite3_config() API.
@@ -647,13 +655,13 @@
647655
**
648656
** See also: [sqlite3_libversion()],
649657
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
650658
** [sqlite_version()] and [sqlite_source_id()].
651659
*/
652
-#define SQLITE_VERSION "3.7.9"
653
-#define SQLITE_VERSION_NUMBER 3007009
654
-#define SQLITE_SOURCE_ID "2011-10-29 19:25:08 5b82ec6fbbd2f4195ad06dd911de3817373ad5bf"
660
+#define SQLITE_VERSION "3.7.10"
661
+#define SQLITE_VERSION_NUMBER 3007010
662
+#define SQLITE_SOURCE_ID "2012-01-11 16:16:08 9e31a275ef494ea8713a1d60a15b84157e57c3ff"
655663
656664
/*
657665
** CAPI3REF: Run-Time Library Version Numbers
658666
** KEYWORDS: sqlite3_version, sqlite3_sourceid
659667
**
@@ -717,11 +725,11 @@
717725
718726
/*
719727
** CAPI3REF: Test To See If The Library Is Threadsafe
720728
**
721729
** ^The sqlite3_threadsafe() function returns zero if and only if
722
-** SQLite was compiled mutexing code omitted due to the
730
+** SQLite was compiled with mutexing code omitted due to the
723731
** [SQLITE_THREADSAFE] compile-time option being set to 0.
724732
**
725733
** SQLite can be compiled with or without mutexes. When
726734
** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
727735
** are enabled and SQLite is threadsafe. When the
@@ -911,11 +919,11 @@
911919
** CAPI3REF: Result Codes
912920
** KEYWORDS: SQLITE_OK {error code} {error codes}
913921
** KEYWORDS: {result code} {result codes}
914922
**
915923
** Many SQLite functions return an integer result code from the set shown
916
-** here in order to indicates success or failure.
924
+** here in order to indicate success or failure.
917925
**
918926
** New error codes may be added in future versions of SQLite.
919927
**
920928
** See also: [SQLITE_IOERR_READ | extended result codes],
921929
** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -1049,11 +1057,15 @@
10491057
** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
10501058
** that when data is appended to a file, the data is appended
10511059
** first then the size of the file is extended, never the other
10521060
** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
10531061
** information is written to disk in the same order as calls
1054
-** to xWrite().
1062
+** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1063
+** after reboot following a crash or power loss, the only bytes in a
1064
+** file that were written at the application level might have changed
1065
+** and that adjacent bytes, even bytes within the same sector are
1066
+** guaranteed to be unchanged.
10551067
*/
10561068
#define SQLITE_IOCAP_ATOMIC 0x00000001
10571069
#define SQLITE_IOCAP_ATOMIC512 0x00000002
10581070
#define SQLITE_IOCAP_ATOMIC1K 0x00000004
10591071
#define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -1063,10 +1075,11 @@
10631075
#define SQLITE_IOCAP_ATOMIC32K 0x00000080
10641076
#define SQLITE_IOCAP_ATOMIC64K 0x00000100
10651077
#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
10661078
#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
10671079
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
1080
+#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
10681081
10691082
/*
10701083
** CAPI3REF: File Locking Levels
10711084
**
10721085
** SQLite uses one of these integer values as the second
@@ -1284,16 +1297,16 @@
12841297
** opcode as doing so may disrupt the operation of the specialized VFSes
12851298
** that do require it.
12861299
**
12871300
** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
12881301
** retry counts and intervals for certain disk I/O operations for the
1289
-** windows [VFS] in order to work to provide robustness against
1302
+** windows [VFS] in order to provide robustness in the presence of
12901303
** anti-virus programs. By default, the windows VFS will retry file read,
1291
-** file write, and file delete opertions up to 10 times, with a delay
1304
+** file write, and file delete operations up to 10 times, with a delay
12921305
** of 25 milliseconds before the first retry and with the delay increasing
12931306
** by an additional 25 milliseconds with each subsequent retry. This
1294
-** opcode allows those to values (10 retries and 25 milliseconds of delay)
1307
+** opcode allows these two values (10 retries and 25 milliseconds of delay)
12951308
** to be adjusted. The values are changed for all database connections
12961309
** within the same process. The argument is a pointer to an array of two
12971310
** integers where the first integer i the new retry count and the second
12981311
** integer is the delay. If either integer is negative, then the setting
12991312
** is not changed but instead the prior value of that setting is written
@@ -1311,27 +1324,49 @@
13111324
** in order for the database to be readable. The fourth parameter to
13121325
** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
13131326
** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
13141327
** WAL mode. If the integer is -1, then it is overwritten with the current
13151328
** WAL persistence setting.
1329
+**
1330
+** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1331
+** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1332
+** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1333
+** xDeviceCharacteristics methods. The fourth parameter to
1334
+** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1335
+** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1336
+** mode. If the integer is -1, then it is overwritten with the current
1337
+** zero-damage mode setting.
13161338
**
13171339
** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
13181340
** a write transaction to indicate that, unless it is rolled back for some
13191341
** reason, the entire database file will be overwritten by the current
13201342
** transaction. This is used by VACUUM operations.
1343
+**
1344
+** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1345
+** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1346
+** final bottom-level VFS are written into memory obtained from
1347
+** [sqlite3_malloc()] and the result is stored in the char* variable
1348
+** that the fourth parameter of [sqlite3_file_control()] points to.
1349
+** The caller is responsible for freeing the memory when done. As with
1350
+** all file-control actions, there is no guarantee that this will actually
1351
+** do anything. Callers should initialize the char* variable to a NULL
1352
+** pointer in case this file-control is not implemented. This file-control
1353
+** is intended for diagnostic use only.
13211354
*/
1322
-#define SQLITE_FCNTL_LOCKSTATE 1
1323
-#define SQLITE_GET_LOCKPROXYFILE 2
1324
-#define SQLITE_SET_LOCKPROXYFILE 3
1325
-#define SQLITE_LAST_ERRNO 4
1326
-#define SQLITE_FCNTL_SIZE_HINT 5
1327
-#define SQLITE_FCNTL_CHUNK_SIZE 6
1328
-#define SQLITE_FCNTL_FILE_POINTER 7
1329
-#define SQLITE_FCNTL_SYNC_OMITTED 8
1330
-#define SQLITE_FCNTL_WIN32_AV_RETRY 9
1331
-#define SQLITE_FCNTL_PERSIST_WAL 10
1332
-#define SQLITE_FCNTL_OVERWRITE 11
1355
+#define SQLITE_FCNTL_LOCKSTATE 1
1356
+#define SQLITE_GET_LOCKPROXYFILE 2
1357
+#define SQLITE_SET_LOCKPROXYFILE 3
1358
+#define SQLITE_LAST_ERRNO 4
1359
+#define SQLITE_FCNTL_SIZE_HINT 5
1360
+#define SQLITE_FCNTL_CHUNK_SIZE 6
1361
+#define SQLITE_FCNTL_FILE_POINTER 7
1362
+#define SQLITE_FCNTL_SYNC_OMITTED 8
1363
+#define SQLITE_FCNTL_WIN32_AV_RETRY 9
1364
+#define SQLITE_FCNTL_PERSIST_WAL 10
1365
+#define SQLITE_FCNTL_OVERWRITE 11
1366
+#define SQLITE_FCNTL_VFSNAME 12
1367
+#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
13331368
13341369
/*
13351370
** CAPI3REF: Mutex Handle
13361371
**
13371372
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1382,11 +1417,11 @@
13821417
** ^SQLite guarantees that the zFilename parameter to xOpen
13831418
** is either a NULL pointer or string obtained
13841419
** from xFullPathname() with an optional suffix added.
13851420
** ^If a suffix is added to the zFilename parameter, it will
13861421
** consist of a single "-" character followed by no more than
1387
-** 10 alphanumeric and/or "-" characters.
1422
+** 11 alphanumeric and/or "-" characters.
13881423
** ^SQLite further guarantees that
13891424
** the string will be valid and unchanged until xClose() is
13901425
** called. Because of the previous sentence,
13911426
** the [sqlite3_file] can safely store a pointer to the
13921427
** filename if it needs to remember the filename for some reason.
@@ -1913,11 +1948,11 @@
19131948
**
19141949
** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
19151950
** <dd> ^This option specifies a static memory buffer that SQLite can use for
19161951
** the database page cache with the default page cache implementation.
19171952
** This configuration should not be used if an application-define page
1918
-** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1953
+** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
19191954
** There are three arguments to this option: A pointer to 8-byte aligned
19201955
** memory, the size of each page buffer (sz), and the number of pages (N).
19211956
** The sz argument should be the size of the largest database page
19221957
** (a power of two between 512 and 32768) plus a little extra for each
19231958
** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1982,19 +2017,19 @@
19822017
** slots allocated to each database connection.)^ ^(This option sets the
19832018
** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
19842019
** verb to [sqlite3_db_config()] can be used to change the lookaside
19852020
** configuration on individual connections.)^ </dd>
19862021
**
1987
-** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
2022
+** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
19882023
** <dd> ^(This option takes a single argument which is a pointer to
1989
-** an [sqlite3_pcache_methods] object. This object specifies the interface
2024
+** an [sqlite3_pcache_methods2] object. This object specifies the interface
19902025
** to a custom page cache implementation.)^ ^SQLite makes a copy of the
19912026
** object and uses it for page cache memory allocations.</dd>
19922027
**
1993
-** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
2028
+** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
19942029
** <dd> ^(This option takes a single argument which is a pointer to an
1995
-** [sqlite3_pcache_methods] object. SQLite copies of the current
2030
+** [sqlite3_pcache_methods2] object. SQLite copies of the current
19962031
** page cache implementation into that object.)^ </dd>
19972032
**
19982033
** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
19992034
** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
20002035
** function with a call signature of void(*)(void*,int,const char*),
@@ -2023,10 +2058,15 @@
20232058
** connection is opened. If it is globally disabled, filenames are
20242059
** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
20252060
** database connection is opened. By default, URI handling is globally
20262061
** disabled. The default value may be changed by compiling with the
20272062
** [SQLITE_USE_URI] symbol defined.
2063
+**
2064
+** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2065
+** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
2066
+** <dd> These options are obsolete and should not be used by new code.
2067
+** They are retained for backwards compatibility but are now no-ops.
20282068
** </dl>
20292069
*/
20302070
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
20312071
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
20322072
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2038,14 +2078,16 @@
20382078
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
20392079
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
20402080
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
20412081
/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
20422082
#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2043
-#define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
2044
-#define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
2083
+#define SQLITE_CONFIG_PCACHE 14 /* no-op */
2084
+#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
20452085
#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
20462086
#define SQLITE_CONFIG_URI 17 /* int */
2087
+#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2088
+#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
20472089
20482090
/*
20492091
** CAPI3REF: Database Connection Configuration Options
20502092
**
20512093
** These constants are the available integer configuration options that
@@ -2526,11 +2568,11 @@
25262568
** These routines all implement some additional formatting
25272569
** options that are useful for constructing SQL statements.
25282570
** All of the usual printf() formatting options apply. In addition, there
25292571
** is are "%q", "%Q", and "%z" options.
25302572
**
2531
-** ^(The %q option works like %s in that it substitutes a null-terminated
2573
+** ^(The %q option works like %s in that it substitutes a nul-terminated
25322574
** string from the argument list. But %q also doubles every '\'' character.
25332575
** %q is designed for use inside a string literal.)^ By doubling each '\''
25342576
** character it escapes that character and allows it to be inserted into
25352577
** the string.
25362578
**
@@ -3134,25 +3176,41 @@
31343176
);
31353177
31363178
/*
31373179
** CAPI3REF: Obtain Values For URI Parameters
31383180
**
3139
-** This is a utility routine, useful to VFS implementations, that checks
3181
+** These are utility routines, useful to VFS implementations, that check
31403182
** to see if a database file was a URI that contained a specific query
3141
-** parameter, and if so obtains the value of the query parameter.
3142
-**
3143
-** The zFilename argument is the filename pointer passed into the xOpen()
3144
-** method of a VFS implementation. The zParam argument is the name of the
3145
-** query parameter we seek. This routine returns the value of the zParam
3146
-** parameter if it exists. If the parameter does not exist, this routine
3147
-** returns a NULL pointer.
3148
-**
3149
-** If the zFilename argument to this function is not a pointer that SQLite
3150
-** passed into the xOpen VFS method, then the behavior of this routine
3151
-** is undefined and probably undesirable.
3183
+** parameter, and if so obtains the value of that query parameter.
3184
+**
3185
+** If F is the filename pointer passed into the xOpen() method of a VFS
3186
+** implementation and P is the name of the query parameter, then
3187
+** sqlite3_uri_parameter(F,P) returns the value of the P
3188
+** parameter if it exists or a NULL pointer if P does not appear as a
3189
+** query parameter on F. If P is a query parameter of F
3190
+** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3191
+** a pointer to an empty string.
3192
+**
3193
+** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3194
+** parameter and returns true (1) or false (0) according to the value
3195
+** of P. The value of P is true if it is "yes" or "true" or "on" or
3196
+** a non-zero number and is false otherwise. If P is not a query parameter
3197
+** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3198
+**
3199
+** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3200
+** 64-bit signed integer and returns that integer, or D if P does not
3201
+** exist. If the value of P is something other than an integer, then
3202
+** zero is returned.
3203
+**
3204
+** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3205
+** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
3206
+** is not a pathname pointer that SQLite passed into the xOpen VFS method,
3207
+** then the behavior of this routine is undefined and probably undesirable.
31523208
*/
31533209
SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3210
+SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3211
+SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
31543212
31553213
31563214
/*
31573215
** CAPI3REF: Error Codes And Messages
31583216
**
@@ -3470,10 +3528,29 @@
34703528
** change the configuration of a database connection, they do not make
34713529
** changes to the content of the database files on disk.
34723530
*/
34733531
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
34743532
3533
+/*
3534
+** CAPI3REF: Determine If A Prepared Statement Has Been Reset
3535
+**
3536
+** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
3537
+** [prepared statement] S has been stepped at least once using
3538
+** [sqlite3_step(S)] but has not run to completion and/or has not
3539
+** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
3540
+** interface returns false if S is a NULL pointer. If S is not a
3541
+** NULL pointer and is not a pointer to a valid [prepared statement]
3542
+** object, then the behavior is undefined and probably undesirable.
3543
+**
3544
+** This interface can be used in combination [sqlite3_next_stmt()]
3545
+** to locate all prepared statements associated with a database
3546
+** connection that are in need of being reset. This can be used,
3547
+** for example, in diagnostic routines to search for prepared
3548
+** statements that are holding a transaction open.
3549
+*/
3550
+SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3551
+
34753552
/*
34763553
** CAPI3REF: Dynamically Typed Value Object
34773554
** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
34783555
**
34793556
** SQLite uses the sqlite3_value object to represent all values
@@ -4011,11 +4088,11 @@
40114088
** of the string. ^For clarity: the values returned by
40124089
** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
40134090
** bytes in the string, not the number of characters.
40144091
**
40154092
** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4016
-** even empty strings, are always zero terminated. ^The return
4093
+** even empty strings, are always zero-terminated. ^The return
40174094
** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
40184095
**
40194096
** ^The object returned by [sqlite3_column_value()] is an
40204097
** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
40214098
** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4911,10 +4988,26 @@
49114988
** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
49124989
** create the statement in the first place.
49134990
*/
49144991
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
49154992
4993
+/*
4994
+** CAPI3REF: Return The Filename For A Database Connection
4995
+**
4996
+** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4997
+** associated with database N of connection D. ^The main database file
4998
+** has the name "main". If there is no attached database N on the database
4999
+** connection D, or if database N is a temporary or in-memory database, then
5000
+** a NULL pointer is returned.
5001
+**
5002
+** ^The filename returned by this function is the output of the
5003
+** xFullPathname method of the [VFS]. ^In other words, the filename
5004
+** will be an absolute pathname, even if the filename used
5005
+** to open the database originally was a URI or relative pathname.
5006
+*/
5007
+SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
5008
+
49165009
/*
49175010
** CAPI3REF: Find the next prepared statement
49185011
**
49195012
** ^This interface returns a pointer to the next [prepared statement] after
49205013
** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4946,17 +5039,19 @@
49465039
** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
49475040
** return the P argument from the previous call of the same function
49485041
** on the same [database connection] D, or NULL for
49495042
** the first call for each function on D.
49505043
**
5044
+** The commit and rollback hook callbacks are not reentrant.
49515045
** The callback implementation must not do anything that will modify
49525046
** the database connection that invoked the callback. Any actions
49535047
** to modify the database connection must be deferred until after the
49545048
** completion of the [sqlite3_step()] call that triggered the commit
49555049
** or rollback hook in the first place.
4956
-** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4957
-** database connections for the meaning of "modify" in this paragraph.
5050
+** Note that running any other SQL statements, including SELECT statements,
5051
+** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
5052
+** the database connections for the meaning of "modify" in this paragraph.
49585053
**
49595054
** ^Registering a NULL function disables the callback.
49605055
**
49615056
** ^When the commit hook callback routine returns zero, the [COMMIT]
49625057
** operation is allowed to continue normally. ^If the commit hook
@@ -5065,13 +5160,28 @@
50655160
** pages to improve performance is an example of non-essential memory.
50665161
** ^sqlite3_release_memory() returns the number of bytes actually freed,
50675162
** which might be more or less than the amount requested.
50685163
** ^The sqlite3_release_memory() routine is a no-op returning zero
50695164
** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5165
+**
5166
+** See also: [sqlite3_db_release_memory()]
50705167
*/
50715168
SQLITE_API int sqlite3_release_memory(int);
50725169
5170
+/*
5171
+** CAPI3REF: Free Memory Used By A Database Connection
5172
+**
5173
+** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
5174
+** memory as possible from database connection D. Unlike the
5175
+** [sqlite3_release_memory()] interface, this interface is effect even
5176
+** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
5177
+** omitted.
5178
+**
5179
+** See also: [sqlite3_release_memory()]
5180
+*/
5181
+SQLITE_API int sqlite3_db_release_memory(sqlite3*);
5182
+
50735183
/*
50745184
** CAPI3REF: Impose A Limit On Heap Size
50755185
**
50765186
** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
50775187
** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -5082,11 +5192,12 @@
50825192
** below the limit, it will exceed the limit rather than generate
50835193
** an [SQLITE_NOMEM] error. In other words, the soft heap limit
50845194
** is advisory only.
50855195
**
50865196
** ^The return value from sqlite3_soft_heap_limit64() is the size of
5087
-** the soft heap limit prior to the call. ^If the argument N is negative
5197
+** the soft heap limit prior to the call, or negative in the case of an
5198
+** error. ^If the argument N is negative
50885199
** then no change is made to the soft heap limit. Hence, the current
50895200
** size of the soft heap limit can be determined by invoking
50905201
** sqlite3_soft_heap_limit64() with a negative argument.
50915202
**
50925203
** ^If the argument N is zero then the soft heap limit is disabled.
@@ -5098,11 +5209,11 @@
50985209
** <li> The soft heap limit is set to zero.
50995210
** <li> Memory accounting is disabled using a combination of the
51005211
** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
51015212
** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
51025213
** <li> An alternative page cache implementation is specified using
5103
-** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
5214
+** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
51045215
** <li> The page cache allocates from its own memory pool supplied
51055216
** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
51065217
** from the heap.
51075218
** </ul>)^
51085219
**
@@ -5840,19 +5951,19 @@
58405951
** is selected automatically at compile-time. ^(The following
58415952
** implementations are available in the SQLite core:
58425953
**
58435954
** <ul>
58445955
** <li> SQLITE_MUTEX_OS2
5845
-** <li> SQLITE_MUTEX_PTHREAD
5956
+** <li> SQLITE_MUTEX_PTHREADS
58465957
** <li> SQLITE_MUTEX_W32
58475958
** <li> SQLITE_MUTEX_NOOP
58485959
** </ul>)^
58495960
**
58505961
** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
58515962
** that does no real locking and is appropriate for use in
58525963
** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5853
-** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5964
+** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
58545965
** are appropriate for use on OS/2, Unix, and Windows.
58555966
**
58565967
** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
58575968
** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
58585969
** implementation is included with the library. In this case the
@@ -6038,11 +6149,11 @@
60386149
** defined and if NDEBUG is not defined.
60396150
**
60406151
** ^These routines should return true if the mutex in their argument
60416152
** is held or not held, respectively, by the calling thread.
60426153
**
6043
-** ^The implementation is not required to provided versions of these
6154
+** ^The implementation is not required to provide versions of these
60446155
** routines that actually work. If the implementation does not provide working
60456156
** versions of these routines, it should at least provide stubs that always
60466157
** return true so that one does not get spurious assertion failures.
60476158
**
60486159
** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -6166,13 +6277,13 @@
61666277
#define SQLITE_TESTCTRL_ASSERT 12
61676278
#define SQLITE_TESTCTRL_ALWAYS 13
61686279
#define SQLITE_TESTCTRL_RESERVE 14
61696280
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
61706281
#define SQLITE_TESTCTRL_ISKEYWORD 16
6171
-#define SQLITE_TESTCTRL_PGHDRSZ 17
6172
-#define SQLITE_TESTCTRL_SCRATCHMALLOC 18
6173
-#define SQLITE_TESTCTRL_LOCALTIME_FAULT 19
6282
+#define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6283
+#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6284
+#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
61746285
#define SQLITE_TESTCTRL_LAST 19
61756286
61766287
/*
61776288
** CAPI3REF: SQLite Runtime Status
61786289
**
@@ -6471,21 +6582,37 @@
64716582
** the pluggable module. The SQLite core has no knowledge of
64726583
** its size or internal structure and never deals with the
64736584
** sqlite3_pcache object except by holding and passing pointers
64746585
** to the object.
64756586
**
6476
-** See [sqlite3_pcache_methods] for additional information.
6587
+** See [sqlite3_pcache_methods2] for additional information.
64776588
*/
64786589
typedef struct sqlite3_pcache sqlite3_pcache;
6590
+
6591
+/*
6592
+** CAPI3REF: Custom Page Cache Object
6593
+**
6594
+** The sqlite3_pcache_page object represents a single page in the
6595
+** page cache. The page cache will allocate instances of this
6596
+** object. Various methods of the page cache use pointers to instances
6597
+** of this object as parameters or as their return value.
6598
+**
6599
+** See [sqlite3_pcache_methods2] for additional information.
6600
+*/
6601
+typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6602
+struct sqlite3_pcache_page {
6603
+ void *pBuf; /* The content of the page */
6604
+ void *pExtra; /* Extra information associated with the page */
6605
+};
64796606
64806607
/*
64816608
** CAPI3REF: Application Defined Page Cache.
64826609
** KEYWORDS: {page cache}
64836610
**
6484
-** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6611
+** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
64856612
** register an alternative page cache implementation by passing in an
6486
-** instance of the sqlite3_pcache_methods structure.)^
6613
+** instance of the sqlite3_pcache_methods2 structure.)^
64876614
** In many applications, most of the heap memory allocated by
64886615
** SQLite is used for the page cache.
64896616
** By implementing a
64906617
** custom page cache using this API, an application can better control
64916618
** the amount of memory consumed by SQLite, the way in which
@@ -6495,20 +6622,20 @@
64956622
**
64966623
** The alternative page cache mechanism is an
64976624
** extreme measure that is only needed by the most demanding applications.
64986625
** The built-in page cache is recommended for most uses.
64996626
**
6500
-** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6627
+** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
65016628
** internal buffer by SQLite within the call to [sqlite3_config]. Hence
65026629
** the application may discard the parameter after the call to
65036630
** [sqlite3_config()] returns.)^
65046631
**
65056632
** [[the xInit() page cache method]]
65066633
** ^(The xInit() method is called once for each effective
65076634
** call to [sqlite3_initialize()])^
65086635
** (usually only once during the lifetime of the process). ^(The xInit()
6509
-** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6636
+** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
65106637
** The intent of the xInit() method is to set up global data structures
65116638
** required by the custom page cache implementation.
65126639
** ^(If the xInit() method is NULL, then the
65136640
** built-in default page cache is used instead of the application defined
65146641
** page cache.)^
@@ -6531,21 +6658,19 @@
65316658
** [[the xCreate() page cache methods]]
65326659
** ^SQLite invokes the xCreate() method to construct a new cache instance.
65336660
** SQLite will typically create one cache instance for each open database file,
65346661
** though this is not guaranteed. ^The
65356662
** first parameter, szPage, is the size in bytes of the pages that must
6536
-** be allocated by the cache. ^szPage will not be a power of two. ^szPage
6537
-** will the page size of the database file that is to be cached plus an
6538
-** increment (here called "R") of less than 250. SQLite will use the
6539
-** extra R bytes on each page to store metadata about the underlying
6540
-** database page on disk. The value of R depends
6663
+** be allocated by the cache. ^szPage will always a power of two. ^The
6664
+** second parameter szExtra is a number of bytes of extra storage
6665
+** associated with each page cache entry. ^The szExtra parameter will
6666
+** a number less than 250. SQLite will use the
6667
+** extra szExtra bytes on each page to store metadata about the underlying
6668
+** database page on disk. The value passed into szExtra depends
65416669
** on the SQLite version, the target platform, and how SQLite was compiled.
6542
-** ^(R is constant for a particular build of SQLite. Except, there are two
6543
-** distinct values of R when SQLite is compiled with the proprietary
6544
-** ZIPVFS extension.)^ ^The second argument to
6545
-** xCreate(), bPurgeable, is true if the cache being created will
6546
-** be used to cache database pages of a file stored on disk, or
6670
+** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6671
+** created will be used to cache database pages of a file stored on disk, or
65476672
** false if it is used for an in-memory database. The cache implementation
65486673
** does not have to do anything special based with the value of bPurgeable;
65496674
** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
65506675
** never invoke xUnpin() except to deliberately delete a page.
65516676
** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6565,15 +6690,20 @@
65656690
** The xPagecount() method must return the number of pages currently
65666691
** stored in the cache, both pinned and unpinned.
65676692
**
65686693
** [[the xFetch() page cache methods]]
65696694
** The xFetch() method locates a page in the cache and returns a pointer to
6570
-** the page, or a NULL pointer.
6571
-** A "page", in this context, means a buffer of szPage bytes aligned at an
6572
-** 8-byte boundary. The page to be fetched is determined by the key. ^The
6573
-** minimum key value is 1. After it has been retrieved using xFetch, the page
6574
-** is considered to be "pinned".
6695
+** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6696
+** The pBuf element of the returned sqlite3_pcache_page object will be a
6697
+** pointer to a buffer of szPage bytes used to store the content of a
6698
+** single database page. The pExtra element of sqlite3_pcache_page will be
6699
+** a pointer to the szExtra bytes of extra storage that SQLite has requested
6700
+** for each entry in the page cache.
6701
+**
6702
+** The page to be fetched is determined by the key. ^The minimum key value
6703
+** is 1. After it has been retrieved using xFetch, the page is considered
6704
+** to be "pinned".
65756705
**
65766706
** If the requested page is already in the page cache, then the page cache
65776707
** implementation must return a pointer to the page buffer with its content
65786708
** intact. If the requested page is not already in the cache, then the
65796709
** cache implementation should use the value of the createFlag
@@ -6622,12 +6752,41 @@
66226752
**
66236753
** [[the xDestroy() page cache method]]
66246754
** ^The xDestroy() method is used to delete a cache allocated by xCreate().
66256755
** All resources associated with the specified cache should be freed. ^After
66266756
** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6627
-** handle invalid, and will not use it with any other sqlite3_pcache_methods
6757
+** handle invalid, and will not use it with any other sqlite3_pcache_methods2
66286758
** functions.
6759
+**
6760
+** [[the xShrink() page cache method]]
6761
+** ^SQLite invokes the xShrink() method when it wants the page cache to
6762
+** free up as much of heap memory as possible. The page cache implementation
6763
+** is not obligated to free any memory, but well-behaved implementions should
6764
+** do their best.
6765
+*/
6766
+typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6767
+struct sqlite3_pcache_methods2 {
6768
+ int iVersion;
6769
+ void *pArg;
6770
+ int (*xInit)(void*);
6771
+ void (*xShutdown)(void*);
6772
+ sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6773
+ void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6774
+ int (*xPagecount)(sqlite3_pcache*);
6775
+ sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6776
+ void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6777
+ void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6778
+ unsigned oldKey, unsigned newKey);
6779
+ void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6780
+ void (*xDestroy)(sqlite3_pcache*);
6781
+ void (*xShrink)(sqlite3_pcache*);
6782
+};
6783
+
6784
+/*
6785
+** This is the obsolete pcache_methods object that has now been replaced
6786
+** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
6787
+** retained in the header file for backwards compatibility only.
66296788
*/
66306789
typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
66316790
struct sqlite3_pcache_methods {
66326791
void *pArg;
66336792
int (*xInit)(void*);
@@ -6639,10 +6798,11 @@
66396798
void (*xUnpin)(sqlite3_pcache*, void*, int discard);
66406799
void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
66416800
void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
66426801
void (*xDestroy)(sqlite3_pcache*);
66436802
};
6803
+
66446804
66456805
/*
66466806
** CAPI3REF: Online Backup Object
66476807
**
66486808
** The sqlite3_backup object records state information about an ongoing
@@ -7643,11 +7803,11 @@
76437803
** the default file format for new databases and the maximum file format
76447804
** that the library can read.
76457805
*/
76467806
#define SQLITE_MAX_FILE_FORMAT 4
76477807
#ifndef SQLITE_DEFAULT_FILE_FORMAT
7648
-# define SQLITE_DEFAULT_FILE_FORMAT 1
7808
+# define SQLITE_DEFAULT_FILE_FORMAT 4
76497809
#endif
76507810
76517811
/*
76527812
** Determine whether triggers are recursive by default. This can be
76537813
** changed at run-time using a pragma.
@@ -8283,10 +8443,11 @@
82838443
struct SubProgram {
82848444
VdbeOp *aOp; /* Array of opcodes for sub-program */
82858445
int nOp; /* Elements in aOp[] */
82868446
int nMem; /* Number of memory cells required */
82878447
int nCsr; /* Number of cursors required */
8448
+ int nOnce; /* Number of OP_Once instructions */
82888449
void *token; /* id that may be used to recursive triggers */
82898450
SubProgram *pNext; /* Next sub-program already visited */
82908451
};
82918452
82928453
/*
@@ -8529,14 +8690,14 @@
85298690
#define OPFLG_IN2 0x0008 /* in2: P2 is an input */
85308691
#define OPFLG_IN3 0x0010 /* in3: P3 is an input */
85318692
#define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
85328693
#define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
85338694
#define OPFLG_INITIALIZER {\
8534
-/* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8695
+/* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
85358696
/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
85368697
/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8537
-/* 24 */ 0x00, 0x01, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
8698
+/* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
85388699
/* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
85398700
/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
85408701
/* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
85418702
/* 56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
85428703
/* 64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
@@ -8732,10 +8893,11 @@
87328893
/* Functions used to configure a Pager object. */
87338894
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
87348895
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
87358896
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
87368897
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
8898
+SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
87378899
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
87388900
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
87398901
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
87408902
SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
87418903
SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
@@ -8840,11 +9002,12 @@
88409002
/*
88419003
** Every page in the cache is controlled by an instance of the following
88429004
** structure.
88439005
*/
88449006
struct PgHdr {
8845
- void *pData; /* Content of this page */
9007
+ sqlite3_pcache_page *pPage; /* Pcache object page handle */
9008
+ void *pData; /* Page data */
88469009
void *pExtra; /* Extra content */
88479010
PgHdr *pDirty; /* Transient list of dirty pages */
88489011
Pgno pgno; /* Page number for this page */
88499012
Pager *pPager; /* The pager this page is part of */
88509013
#ifdef SQLITE_CHECK_PAGES
@@ -8958,10 +9121,13 @@
89589121
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
89599122
#ifdef SQLITE_TEST
89609123
SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
89619124
#endif
89629125
9126
+/* Free up as much memory as possible from the page cache */
9127
+SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
9128
+
89639129
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
89649130
/* Try to return memory used by the pcache module to the main memory heap */
89659131
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
89669132
#endif
89679133
@@ -9042,21 +9208,10 @@
90429208
#else
90439209
# ifndef SQLITE_OS_WIN
90449210
# define SQLITE_OS_WIN 0
90459211
# endif
90469212
#endif
9047
-
9048
-/*
9049
-** Determine if we are dealing with WindowsCE - which has a much
9050
-** reduced API.
9051
-*/
9052
-#if defined(_WIN32_WCE)
9053
-# define SQLITE_OS_WINCE 1
9054
-#else
9055
-# define SQLITE_OS_WINCE 0
9056
-#endif
9057
-
90589213
90599214
/*
90609215
** Define the maximum size of a temporary filename
90619216
*/
90629217
#if SQLITE_OS_WIN
@@ -9078,10 +9233,29 @@
90789233
# define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
90799234
#else
90809235
# define SQLITE_TEMPNAME_SIZE 200
90819236
#endif
90829237
9238
+/*
9239
+** Determine if we are dealing with Windows NT.
9240
+*/
9241
+#if defined(_WIN32_WINNT)
9242
+# define SQLITE_OS_WINNT 1
9243
+#else
9244
+# define SQLITE_OS_WINNT 0
9245
+#endif
9246
+
9247
+/*
9248
+** Determine if we are dealing with WindowsCE - which has a much
9249
+** reduced API.
9250
+*/
9251
+#if defined(_WIN32_WCE)
9252
+# define SQLITE_OS_WINCE 1
9253
+#else
9254
+# define SQLITE_OS_WINCE 0
9255
+#endif
9256
+
90839257
/* If the SET_FULLSYNC macro is not defined above, then make it
90849258
** a no-op
90859259
*/
90869260
#ifndef SET_FULLSYNC
90879261
# define SET_FULLSYNC(x,y)
@@ -9089,11 +9263,11 @@
90899263
90909264
/*
90919265
** The default size of a disk sector
90929266
*/
90939267
#ifndef SQLITE_DEFAULT_SECTOR_SIZE
9094
-# define SQLITE_DEFAULT_SECTOR_SIZE 512
9268
+# define SQLITE_DEFAULT_SECTOR_SIZE 4096
90959269
#endif
90969270
90979271
/*
90989272
** Temporary files are named starting with this prefix followed by 16 random
90999273
** alphanumeric characters, and no file extension. They are stored in the
@@ -9222,17 +9396,19 @@
92229396
SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
92239397
SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
92249398
SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
92259399
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
92269400
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
9401
+SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
92279402
#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
92289403
SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
92299404
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
92309405
SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
92319406
SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
92329407
SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
92339408
SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9409
+
92349410
92359411
/*
92369412
** Functions for accessing sqlite3_vfs methods
92379413
*/
92389414
SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -9820,24 +9996,15 @@
98209996
** collating sequence may not be read or written.
98219997
*/
98229998
struct CollSeq {
98239999
char *zName; /* Name of the collating sequence, UTF-8 encoded */
982410000
u8 enc; /* Text encoding handled by xCmp() */
9825
- u8 type; /* One of the SQLITE_COLL_... values below */
982610001
void *pUser; /* First argument to xCmp() */
982710002
int (*xCmp)(void*,int, const void*, int, const void*);
982810003
void (*xDel)(void*); /* Destructor for pUser */
982910004
};
983010005
9831
-/*
9832
-** Allowed values of CollSeq.type:
9833
-*/
9834
-#define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */
9835
-#define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */
9836
-#define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */
9837
-#define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */
9838
-
983910006
/*
984010007
** A sort order can be either ASC or DESC.
984110008
*/
984210009
#define SQLITE_SO_ASC 0 /* Sort in ascending order */
984310010
#define SQLITE_SO_DESC 1 /* Sort in ascending order */
@@ -10119,24 +10286,21 @@
1011910286
** into its constituent fields.
1012010287
*/
1012110288
struct UnpackedRecord {
1012210289
KeyInfo *pKeyInfo; /* Collation and sort-order information */
1012310290
u16 nField; /* Number of entries in apMem[] */
10124
- u16 flags; /* Boolean settings. UNPACKED_... below */
10291
+ u8 flags; /* Boolean settings. UNPACKED_... below */
1012510292
i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
1012610293
Mem *aMem; /* Values */
1012710294
};
1012810295
1012910296
/*
1013010297
** Allowed values of UnpackedRecord.flags
1013110298
*/
10132
-#define UNPACKED_NEED_FREE 0x0001 /* Memory is from sqlite3Malloc() */
10133
-#define UNPACKED_NEED_DESTROY 0x0002 /* apMem[]s should all be destroyed */
10134
-#define UNPACKED_IGNORE_ROWID 0x0004 /* Ignore trailing rowid on key1 */
10135
-#define UNPACKED_INCRKEY 0x0008 /* Make this key an epsilon larger */
10136
-#define UNPACKED_PREFIX_MATCH 0x0010 /* A prefix match is considered OK */
10137
-#define UNPACKED_PREFIX_SEARCH 0x0020 /* A prefix match is considered OK */
10299
+#define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */
10300
+#define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */
10301
+#define UNPACKED_PREFIX_SEARCH 0x04 /* Ignore final (rowid) field */
1013810302
1013910303
/*
1014010304
** Each SQL index is represented in memory by an
1014110305
** instance of the following structure.
1014210306
**
@@ -10395,14 +10559,14 @@
1039510559
#define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
1039610560
#define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
1039710561
#define EP_FixedDest 0x0200 /* Result needed in a specific register */
1039810562
#define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
1039910563
#define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
10400
-
10401
-#define EP_Reduced 0x1000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10402
-#define EP_TokenOnly 0x2000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10403
-#define EP_Static 0x4000 /* Held in memory not obtained from malloc() */
10564
+#define EP_Hint 0x1000 /* Optimizer hint. Not required for correctness */
10565
+#define EP_Reduced 0x2000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10566
+#define EP_TokenOnly 0x4000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10567
+#define EP_Static 0x8000 /* Held in memory not obtained from malloc() */
1040410568
1040510569
/*
1040610570
** The following are the meanings of bits in the Expr.flags2 field.
1040710571
*/
1040810572
#define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
@@ -10460,11 +10624,11 @@
1046010624
Expr *pExpr; /* The list of expressions */
1046110625
char *zName; /* Token associated with this expression */
1046210626
char *zSpan; /* Original text of the expression */
1046310627
u8 sortOrder; /* 1 for DESC or 0 for ASC */
1046410628
u8 done; /* A flag to indicate when processing is finished */
10465
- u16 iCol; /* For ORDER BY, column number in result set */
10629
+ u16 iOrderByCol; /* For ORDER BY, column number in result set */
1046610630
u16 iAlias; /* Index into Parse.aAlias[] for zName */
1046710631
} *a; /* One entry for each expression */
1046810632
};
1046910633
1047010634
/*
@@ -10760,17 +10924,17 @@
1076010924
1076110925
/*
1076210926
** Allowed values for Select.selFlags. The "SF" prefix stands for
1076310927
** "Select Flag".
1076410928
*/
10765
-#define SF_Distinct 0x0001 /* Output should be DISTINCT */
10766
-#define SF_Resolved 0x0002 /* Identifiers have been resolved */
10767
-#define SF_Aggregate 0x0004 /* Contains aggregate functions */
10768
-#define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
10769
-#define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
10770
-#define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
10771
-#define SF_UseSorter 0x0040 /* Sort using a sorter */
10929
+#define SF_Distinct 0x01 /* Output should be DISTINCT */
10930
+#define SF_Resolved 0x02 /* Identifiers have been resolved */
10931
+#define SF_Aggregate 0x04 /* Contains aggregate functions */
10932
+#define SF_UsesEphemeral 0x08 /* Uses the OpenEphemeral opcode */
10933
+#define SF_Expanded 0x10 /* sqlite3SelectExpand() called on this */
10934
+#define SF_HasTypeInfo 0x20 /* FROM subqueries have Table metadata */
10935
+#define SF_UseSorter 0x40 /* Sort using a sorter */
1077210936
1077310937
1077410938
/*
1077510939
** The results of a select can be distributed in several ways. The
1077610940
** "SRT" prefix means "SELECT Result Type".
@@ -10881,28 +11045,27 @@
1088111045
sqlite3 *db; /* The main database structure */
1088211046
int rc; /* Return code from execution */
1088311047
char *zErrMsg; /* An error message */
1088411048
Vdbe *pVdbe; /* An engine for executing database bytecode */
1088511049
u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
10886
- u8 nameClash; /* A permanent table name clashes with temp table name */
1088711050
u8 checkSchema; /* Causes schema cookie check after an error */
1088811051
u8 nested; /* Number of nested calls to the parser/code generator */
10889
- u8 parseError; /* True after a parsing error. Ticket #1794 */
1089011052
u8 nTempReg; /* Number of temporary registers in aTempReg[] */
1089111053
u8 nTempInUse; /* Number of aTempReg[] currently checked out */
1089211054
int aTempReg[8]; /* Holding area for temporary registers */
1089311055
int nRangeReg; /* Size of the temporary register block */
1089411056
int iRangeReg; /* First register in temporary register block */
1089511057
int nErr; /* Number of errors seen */
1089611058
int nTab; /* Number of previously allocated VDBE cursors */
1089711059
int nMem; /* Number of memory cells used so far */
1089811060
int nSet; /* Number of sets used so far */
11061
+ int nOnce; /* Number of OP_Once instructions so far */
1089911062
int ckBase; /* Base register of data during check constraints */
1090011063
int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
1090111064
int iCacheCnt; /* Counter used to generate aColCache[].lru values */
10902
- u8 nColCache; /* Number of entries in the column cache */
10903
- u8 iColCache; /* Next entry of the cache to replace */
11065
+ u8 nColCache; /* Number of entries in aColCache[] */
11066
+ u8 iColCache; /* Next entry in aColCache[] to replace */
1090411067
struct yColCache {
1090511068
int iTable; /* Table cursor number */
1090611069
int iColumn; /* Table column number */
1090711070
u8 tempReg; /* iReg is a temp register that needs to be freed */
1090811071
int iLevel; /* Nesting level */
@@ -10940,11 +11103,10 @@
1094011103
int nVar; /* Number of '?' variables seen in the SQL so far */
1094111104
int nzVar; /* Number of available slots in azVar[] */
1094211105
char **azVar; /* Pointers to names of parameters */
1094311106
Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
1094411107
int nAlias; /* Number of aliased result set columns */
10945
- int nAliasAlloc; /* Number of allocated slots for aAlias[] */
1094611108
int *aAlias; /* Register used to hold aliased result */
1094711109
u8 explain; /* True if the EXPLAIN flag is found on the query */
1094811110
Token sNameToken; /* Token with unqualified schema object name */
1094911111
Token sLastToken; /* The last token parsed */
1095011112
const char *zTail; /* All SQL text past the last semicolon parsed */
@@ -11135,11 +11297,11 @@
1113511297
int mxStrlen; /* Maximum string length */
1113611298
int szLookaside; /* Default lookaside buffer size */
1113711299
int nLookaside; /* Default lookaside buffer count */
1113811300
sqlite3_mem_methods m; /* Low-level memory allocation interface */
1113911301
sqlite3_mutex_methods mutex; /* Low-level mutex interface */
11140
- sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
11302
+ sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
1114111303
void *pHeap; /* Heap storage space */
1114211304
int nHeap; /* Size of pHeap[] */
1114311305
int mnReq, mxReq; /* Min and max heap requests sizes */
1114411306
void *pScratch; /* Scratch memory */
1114511307
int szScratch; /* Size of each scratch buffer */
@@ -11341,10 +11503,33 @@
1134111503
SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
1134211504
#endif
1134311505
#if defined(SQLITE_TEST)
1134411506
SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
1134511507
#endif
11508
+
11509
+/* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
11510
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
11511
+SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe*);
11512
+SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
11513
+SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe*);
11514
+SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe*);
11515
+SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe*);
11516
+SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe*);
11517
+SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe*, Select*);
11518
+SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe*, Expr*);
11519
+SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe*, ExprList*);
11520
+SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe*);
11521
+#else
11522
+# define sqlite3ExplainBegin(X)
11523
+# define sqlite3ExplainSelect(A,B)
11524
+# define sqlite3ExplainExpr(A,B)
11525
+# define sqlite3ExplainExprList(A,B)
11526
+# define sqlite3ExplainFinish(X)
11527
+# define sqlite3VdbeExplanation(X) 0
11528
+#endif
11529
+
11530
+
1134611531
SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
1134711532
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
1134811533
SQLITE_PRIVATE int sqlite3Dequote(char*);
1134911534
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
1135011535
SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
@@ -11351,10 +11536,11 @@
1135111536
SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
1135211537
SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
1135311538
SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
1135411539
SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
1135511540
SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
11541
+SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
1135611542
SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
1135711543
SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
1135811544
SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
1135911545
SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
1136011546
SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
@@ -11382,10 +11568,11 @@
1138211568
SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
1138311569
SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
1138411570
SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
1138511571
SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
1138611572
sqlite3_vfs**,char**,char **);
11573
+SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
1138711574
1138811575
SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
1138911576
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
1139011577
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
1139111578
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -11720,10 +11907,11 @@
1172011907
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
1172111908
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
1172211909
1172311910
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
1172411911
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
11912
+SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
1172511913
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
1172611914
SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
1172711915
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
1172811916
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
1172911917
@@ -12099,11 +12287,11 @@
1209912287
0x7ffffffe, /* mxStrlen */
1210012288
128, /* szLookaside */
1210112289
500, /* nLookaside */
1210212290
{0,0,0,0,0,0,0,0}, /* m */
1210312291
{0,0,0,0,0,0,0,0,0}, /* mutex */
12104
- {0,0,0,0,0,0,0,0,0,0,0}, /* pcache */
12292
+ {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
1210512293
(void*)0, /* pHeap */
1210612294
0, /* nHeap */
1210712295
0, 0, /* mnHeap, mxHeap */
1210812296
(void*)0, /* pScratch */
1210912297
0, /* szScratch */
@@ -12626,10 +12814,13 @@
1262612814
typedef unsigned char Bool;
1262712815
1262812816
/* Opaque type used by code in vdbesort.c */
1262912817
typedef struct VdbeSorter VdbeSorter;
1263012818
12819
+/* Opaque type used by the explainer */
12820
+typedef struct Explain Explain;
12821
+
1263112822
/*
1263212823
** A cursor is a pointer into a single BTree within a database file.
1263312824
** The cursor can seek to a BTree entry with a particular key, or
1263412825
** loop over all entries of the Btree. You can also insert new BTree
1263512826
** entries or retrieve the key or data from the entry that the cursor
@@ -12710,10 +12901,12 @@
1271012901
int pc; /* Program Counter in parent (calling) frame */
1271112902
Op *aOp; /* Program instructions for parent frame */
1271212903
int nOp; /* Size of aOp array */
1271312904
Mem *aMem; /* Array of memory cells for parent frame */
1271412905
int nMem; /* Number of entries in aMem */
12906
+ u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
12907
+ int nOnceFlag; /* Number of entries in aOnceFlag */
1271512908
VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
1271612909
u16 nCursor; /* Number of entries in apCsr */
1271712910
void *token; /* Copy of SubProgram.token */
1271812911
int nChildMem; /* Number of memory cells for child frame */
1271912912
int nChildCsr; /* Number of cursors for child frame */
@@ -12848,10 +13041,22 @@
1284813041
Mem *pMem; /* Memory cell used to store aggregate context */
1284913042
int isError; /* Error code returned by the function. */
1285013043
CollSeq *pColl; /* Collating sequence */
1285113044
};
1285213045
13046
+/*
13047
+** An Explain object accumulates indented output which is helpful
13048
+** in describing recursive data structures.
13049
+*/
13050
+struct Explain {
13051
+ Vdbe *pVdbe; /* Attach the explanation to this Vdbe */
13052
+ StrAccum str; /* The string being accumulated */
13053
+ int nIndent; /* Number of elements in aIndent */
13054
+ u16 aIndent[100]; /* Levels of indentation */
13055
+ char zBase[100]; /* Initial space */
13056
+};
13057
+
1285313058
/*
1285413059
** An instance of the virtual machine. This structure contains the complete
1285513060
** state of the virtual machine.
1285613061
**
1285713062
** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -12914,15 +13119,21 @@
1291413119
char *zSql; /* Text of the SQL statement that generated this */
1291513120
void *pFree; /* Free this when deleting the vdbe */
1291613121
#ifdef SQLITE_DEBUG
1291713122
FILE *trace; /* Write an execution trace here, if not NULL */
1291813123
#endif
13124
+#ifdef SQLITE_ENABLE_TREE_EXPLAIN
13125
+ Explain *pExplain; /* The explainer */
13126
+ char *zExplain; /* Explanation of data structures */
13127
+#endif
1291913128
VdbeFrame *pFrame; /* Parent frame */
1292013129
VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
1292113130
int nFrame; /* Number of frames in pFrame list */
1292213131
u32 expmask; /* Binding to these vars invalidates VM */
1292313132
SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
13133
+ int nOnceFlag; /* Size of array aOnceFlag[] */
13134
+ u8 *aOnceFlag; /* Flags for OP_Once */
1292413135
};
1292513136
1292613137
/*
1292713138
** The following are allowed values for Vdbe.magic
1292813139
*/
@@ -12978,11 +13189,11 @@
1297813189
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
1297913190
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
1298013191
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
1298113192
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
1298213193
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12983
-#define MemReleaseExt(X) \
13194
+#define VdbeMemRelease(X) \
1298413195
if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
1298513196
sqlite3VdbeMemReleaseExternal(X);
1298613197
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
1298713198
SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
1298813199
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
@@ -13017,11 +13228,11 @@
1301713228
# define sqlite3VdbeEnter(X)
1301813229
# define sqlite3VdbeLeave(X)
1301913230
#endif
1302013231
1302113232
#ifdef SQLITE_DEBUG
13022
-SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
13233
+SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
1302313234
#endif
1302413235
1302513236
#ifndef SQLITE_OMIT_FOREIGN_KEY
1302613237
SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
1302713238
#else
@@ -13035,12 +13246,14 @@
1303513246
#endif
1303613247
SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
1303713248
1303813249
#ifndef SQLITE_OMIT_INCRBLOB
1303913250
SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
13251
+ #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
1304013252
#else
1304113253
#define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
13254
+ #define ExpandBlob(P) SQLITE_OK
1304213255
#endif
1304313256
1304413257
#endif /* !defined(_VDBEINT_H_) */
1304513258
1304613259
/************** End of vdbeInt.h *********************************************/
@@ -14436,15 +14649,22 @@
1443614649
** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
1443714650
**
1443814651
** The following functions are instrumented for malloc() failure
1443914652
** testing:
1444014653
**
14441
-** sqlite3OsOpen()
1444214654
** sqlite3OsRead()
1444314655
** sqlite3OsWrite()
1444414656
** sqlite3OsSync()
14657
+** sqlite3OsFileSize()
1444514658
** sqlite3OsLock()
14659
+** sqlite3OsCheckReservedLock()
14660
+** sqlite3OsFileControl()
14661
+** sqlite3OsShmMap()
14662
+** sqlite3OsOpen()
14663
+** sqlite3OsDelete()
14664
+** sqlite3OsAccess()
14665
+** sqlite3OsFullPathname()
1444614666
**
1444714667
*/
1444814668
#if defined(SQLITE_TEST)
1444914669
SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
1445014670
#define DO_OS_MALLOC_TEST(x) \
@@ -14499,13 +14719,27 @@
1449914719
}
1450014720
SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
1450114721
DO_OS_MALLOC_TEST(id);
1450214722
return id->pMethods->xCheckReservedLock(id, pResOut);
1450314723
}
14724
+
14725
+/*
14726
+** Use sqlite3OsFileControl() when we are doing something that might fail
14727
+** and we need to know about the failures. Use sqlite3OsFileControlHint()
14728
+** when simply tossing information over the wall to the VFS and we do not
14729
+** really care if the VFS receives and understands the information since it
14730
+** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
14731
+** routine has no return value since the return value would be meaningless.
14732
+*/
1450414733
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
14734
+ DO_OS_MALLOC_TEST(id);
1450514735
return id->pMethods->xFileControl(id, op, pArg);
1450614736
}
14737
+SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
14738
+ (void)id->pMethods->xFileControl(id, op, pArg);
14739
+}
14740
+
1450714741
SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
1450814742
int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
1450914743
return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
1451014744
}
1451114745
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
@@ -14525,10 +14759,11 @@
1452514759
int iPage,
1452614760
int pgsz,
1452714761
int bExtend, /* True to extend file if necessary */
1452814762
void volatile **pp /* OUT: Pointer to mapping */
1452914763
){
14764
+ DO_OS_MALLOC_TEST(id);
1453014765
return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
1453114766
}
1453214767
1453314768
/*
1453414769
** The next group of routines are convenience wrappers around the
@@ -14541,19 +14776,21 @@
1454114776
int flags,
1454214777
int *pFlagsOut
1454314778
){
1454414779
int rc;
1454514780
DO_OS_MALLOC_TEST(0);
14546
- /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14781
+ /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
1454714782
** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
1454814783
** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
1454914784
** reaching the VFS. */
1455014785
rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
1455114786
assert( rc==SQLITE_OK || pFile->pMethods==0 );
1455214787
return rc;
1455314788
}
1455414789
SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14790
+ DO_OS_MALLOC_TEST(0);
14791
+ assert( dirSync==0 || dirSync==1 );
1455514792
return pVfs->xDelete(pVfs, zPath, dirSync);
1455614793
}
1455714794
SQLITE_PRIVATE int sqlite3OsAccess(
1455814795
sqlite3_vfs *pVfs,
1455914796
const char *zPath,
@@ -14567,10 +14804,11 @@
1456714804
sqlite3_vfs *pVfs,
1456814805
const char *zPath,
1456914806
int nPathOut,
1457014807
char *zPathOut
1457114808
){
14809
+ DO_OS_MALLOC_TEST(0);
1457214810
zPathOut[0] = 0;
1457314811
return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
1457414812
}
1457514813
#ifndef SQLITE_OMIT_LOAD_EXTENSION
1457614814
SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
@@ -14918,31 +15156,81 @@
1491815156
** used when no other memory allocator is specified using compile-time
1491915157
** macros.
1492015158
*/
1492115159
#ifdef SQLITE_SYSTEM_MALLOC
1492215160
15161
+/*
15162
+** Windows systems have malloc_usable_size() but it is called _msize()
15163
+*/
15164
+#if !defined(HAVE_MALLOC_USABLE_SIZE) && SQLITE_OS_WIN
15165
+# define HAVE_MALLOC_USABLE_SIZE 1
15166
+# define malloc_usable_size _msize
15167
+#endif
15168
+
15169
+#if defined(__APPLE__)
15170
+
15171
+/*
15172
+** Use the zone allocator available on apple products
15173
+*/
15174
+#include <sys/sysctl.h>
15175
+#include <malloc/malloc.h>
15176
+#include <libkern/OSAtomic.h>
15177
+static malloc_zone_t* _sqliteZone_;
15178
+#define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
15179
+#define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
15180
+#define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
15181
+#define SQLITE_MALLOCSIZE(x) \
15182
+ (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
15183
+
15184
+#else /* if not __APPLE__ */
15185
+
15186
+/*
15187
+** Use standard C library malloc and free on non-Apple systems.
15188
+*/
15189
+#define SQLITE_MALLOC(x) malloc(x)
15190
+#define SQLITE_FREE(x) free(x)
15191
+#define SQLITE_REALLOC(x,y) realloc((x),(y))
15192
+
15193
+#ifdef HAVE_MALLOC_USABLE_SIZE
15194
+#include <malloc.h>
15195
+#define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
15196
+#else
15197
+#undef SQLITE_MALLOCSIZE
15198
+#endif
15199
+
15200
+#endif /* __APPLE__ or not __APPLE__ */
15201
+
1492315202
/*
1492415203
** Like malloc(), but remember the size of the allocation
1492515204
** so that we can find it later using sqlite3MemSize().
1492615205
**
1492715206
** For this low-level routine, we are guaranteed that nByte>0 because
1492815207
** cases of nByte<=0 will be intercepted and dealt with by higher level
1492915208
** routines.
1493015209
*/
1493115210
static void *sqlite3MemMalloc(int nByte){
15211
+#ifdef SQLITE_MALLOCSIZE
15212
+ void *p = SQLITE_MALLOC( nByte );
15213
+ if( p==0 ){
15214
+ testcase( sqlite3GlobalConfig.xLog!=0 );
15215
+ sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
15216
+ }
15217
+ return p;
15218
+#else
1493215219
sqlite3_int64 *p;
1493315220
assert( nByte>0 );
1493415221
nByte = ROUND8(nByte);
14935
- p = malloc( nByte+8 );
15222
+ p = SQLITE_MALLOC( nByte+8 );
1493615223
if( p ){
1493715224
p[0] = nByte;
1493815225
p++;
1493915226
}else{
1494015227
testcase( sqlite3GlobalConfig.xLog!=0 );
1494115228
sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
1494215229
}
1494315230
return (void *)p;
15231
+#endif
1494415232
}
1494515233
1494615234
/*
1494715235
** Like free() but works for allocations obtained from sqlite3MemMalloc()
1494815236
** or sqlite3MemRealloc().
@@ -14950,26 +15238,34 @@
1495015238
** For this low-level routine, we already know that pPrior!=0 since
1495115239
** cases where pPrior==0 will have been intecepted and dealt with
1495215240
** by higher-level routines.
1495315241
*/
1495415242
static void sqlite3MemFree(void *pPrior){
15243
+#ifdef SQLITE_MALLOCSIZE
15244
+ SQLITE_FREE(pPrior);
15245
+#else
1495515246
sqlite3_int64 *p = (sqlite3_int64*)pPrior;
1495615247
assert( pPrior!=0 );
1495715248
p--;
14958
- free(p);
15249
+ SQLITE_FREE(p);
15250
+#endif
1495915251
}
1496015252
1496115253
/*
1496215254
** Report the allocated size of a prior return from xMalloc()
1496315255
** or xRealloc().
1496415256
*/
1496515257
static int sqlite3MemSize(void *pPrior){
15258
+#ifdef SQLITE_MALLOCSIZE
15259
+ return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
15260
+#else
1496615261
sqlite3_int64 *p;
1496715262
if( pPrior==0 ) return 0;
1496815263
p = (sqlite3_int64*)pPrior;
1496915264
p--;
1497015265
return (int)p[0];
15266
+#endif
1497115267
}
1497215268
1497315269
/*
1497415270
** Like realloc(). Resize an allocation previously obtained from
1497515271
** sqlite3MemMalloc().
@@ -14979,15 +15275,25 @@
1497915275
** redirected to xMalloc. Similarly, we know that nByte>0 becauses
1498015276
** cases where nByte<=0 will have been intercepted by higher-level
1498115277
** routines and redirected to xFree.
1498215278
*/
1498315279
static void *sqlite3MemRealloc(void *pPrior, int nByte){
15280
+#ifdef SQLITE_MALLOCSIZE
15281
+ void *p = SQLITE_REALLOC(pPrior, nByte);
15282
+ if( p==0 ){
15283
+ testcase( sqlite3GlobalConfig.xLog!=0 );
15284
+ sqlite3_log(SQLITE_NOMEM,
15285
+ "failed memory resize %u to %u bytes",
15286
+ SQLITE_MALLOCSIZE(pPrior), nByte);
15287
+ }
15288
+ return p;
15289
+#else
1498415290
sqlite3_int64 *p = (sqlite3_int64*)pPrior;
1498515291
assert( pPrior!=0 && nByte>0 );
1498615292
assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
1498715293
p--;
14988
- p = realloc(p, nByte+8 );
15294
+ p = SQLITE_REALLOC(p, nByte+8 );
1498915295
if( p ){
1499015296
p[0] = nByte;
1499115297
p++;
1499215298
}else{
1499315299
testcase( sqlite3GlobalConfig.xLog!=0 );
@@ -14994,10 +15300,11 @@
1499415300
sqlite3_log(SQLITE_NOMEM,
1499515301
"failed memory resize %u to %u bytes",
1499615302
sqlite3MemSize(pPrior), nByte);
1499715303
}
1499815304
return (void*)p;
15305
+#endif
1499915306
}
1500015307
1500115308
/*
1500215309
** Round up a request size to the next valid allocation size.
1500315310
*/
@@ -15007,10 +15314,38 @@
1500715314
1500815315
/*
1500915316
** Initialize this module.
1501015317
*/
1501115318
static int sqlite3MemInit(void *NotUsed){
15319
+#if defined(__APPLE__)
15320
+ int cpuCount;
15321
+ size_t len;
15322
+ if( _sqliteZone_ ){
15323
+ return SQLITE_OK;
15324
+ }
15325
+ len = sizeof(cpuCount);
15326
+ /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
15327
+ sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
15328
+ if( cpuCount>1 ){
15329
+ /* defer MT decisions to system malloc */
15330
+ _sqliteZone_ = malloc_default_zone();
15331
+ }else{
15332
+ /* only 1 core, use our own zone to contention over global locks,
15333
+ ** e.g. we have our own dedicated locks */
15334
+ bool success;
15335
+ malloc_zone_t* newzone = malloc_create_zone(4096, 0);
15336
+ malloc_set_zone_name(newzone, "Sqlite_Heap");
15337
+ do{
15338
+ success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
15339
+ (void * volatile *)&_sqliteZone_);
15340
+ }while(!_sqliteZone_);
15341
+ if( !success ){
15342
+ /* somebody registered a zone first */
15343
+ malloc_destroy_zone(newzone);
15344
+ }
15345
+ }
15346
+#endif
1501215347
UNUSED_PARAMETER(NotUsed);
1501315348
return SQLITE_OK;
1501415349
}
1501515350
1501615351
/*
@@ -16996,11 +17331,11 @@
1699617331
SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
1699717332
return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
1699817333
}
1699917334
#endif
1700017335
17001
-#endif /* SQLITE_MUTEX_OMIT */
17336
+#endif /* !defined(SQLITE_MUTEX_OMIT) */
1700217337
1700317338
/************** End of mutex.c ***********************************************/
1700417339
/************** Begin file mutex_noop.c **************************************/
1700517340
/*
1700617341
** 2008 October 07
@@ -17203,12 +17538,12 @@
1720317538
*/
1720417539
#ifdef SQLITE_MUTEX_NOOP
1720517540
SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
1720617541
return sqlite3NoopMutex();
1720717542
}
17208
-#endif /* SQLITE_MUTEX_NOOP */
17209
-#endif /* SQLITE_MUTEX_OMIT */
17543
+#endif /* defined(SQLITE_MUTEX_NOOP) */
17544
+#endif /* !defined(SQLITE_MUTEX_OMIT) */
1721017545
1721117546
/************** End of mutex_noop.c ******************************************/
1721217547
/************** Begin file mutex_os2.c ***************************************/
1721317548
/*
1721417549
** 2007 August 28
@@ -17833,11 +18168,11 @@
1783318168
};
1783418169
1783518170
return &sMutex;
1783618171
}
1783718172
17838
-#endif /* SQLITE_MUTEX_PTHREAD */
18173
+#endif /* SQLITE_MUTEX_PTHREADS */
1783918174
1784018175
/************** End of mutex_unix.c ******************************************/
1784118176
/************** Begin file mutex_w32.c ***************************************/
1784218177
/*
1784318178
** 2007 August 14
@@ -18302,11 +18637,12 @@
1830218637
*/
1830318638
SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
1830418639
sqlite3_int64 priorLimit;
1830518640
sqlite3_int64 excess;
1830618641
#ifndef SQLITE_OMIT_AUTOINIT
18307
- sqlite3_initialize();
18642
+ int rc = sqlite3_initialize();
18643
+ if( rc ) return -1;
1830818644
#endif
1830918645
sqlite3_mutex_enter(mem0.mutex);
1831018646
priorLimit = mem0.alarmThreshold;
1831118647
sqlite3_mutex_leave(mem0.mutex);
1831218648
if( n<0 ) return priorLimit;
@@ -19087,11 +19423,11 @@
1908719423
#endif /* SQLITE_OMIT_FLOATING_POINT */
1908819424
1908919425
/*
1909019426
** Append N space characters to the given string buffer.
1909119427
*/
19092
-static void appendSpace(StrAccum *pAccum, int N){
19428
+SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
1909319429
static const char zSpaces[] = " ";
1909419430
while( N>=(int)sizeof(zSpaces)-1 ){
1909519431
sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
1909619432
N -= sizeof(zSpaces)-1;
1909719433
}
@@ -19615,21 +19951,21 @@
1961519951
*/
1961619952
if( !flag_leftjustify ){
1961719953
register int nspace;
1961819954
nspace = width-length;
1961919955
if( nspace>0 ){
19620
- appendSpace(pAccum, nspace);
19956
+ sqlite3AppendSpace(pAccum, nspace);
1962119957
}
1962219958
}
1962319959
if( length>0 ){
1962419960
sqlite3StrAccumAppend(pAccum, bufpt, length);
1962519961
}
1962619962
if( flag_leftjustify ){
1962719963
register int nspace;
1962819964
nspace = width-length;
1962919965
if( nspace>0 ){
19630
- appendSpace(pAccum, nspace);
19966
+ sqlite3AppendSpace(pAccum, nspace);
1963119967
}
1963219968
}
1963319969
sqlite3_free(zExtra);
1963419970
}/* End for loop over the format string */
1963519971
} /* End of function */
@@ -21800,22 +22136,21 @@
2180022136
** Examples:
2180122137
**
2180222138
** test.db-journal => test.nal
2180322139
** test.db-wal => test.wal
2180422140
** test.db-shm => test.shm
22141
+** test.db-mj7f3319fa => test.9fa
2180522142
*/
2180622143
SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
2180722144
#if SQLITE_ENABLE_8_3_NAMES<2
21808
- const char *zOk;
21809
- zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21810
- if( zOk && sqlite3GetBoolean(zOk) )
22145
+ if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
2181122146
#endif
2181222147
{
2181322148
int i, sz;
2181422149
sz = sqlite3Strlen30(z);
2181522150
for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21816
- if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
22151
+ if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
2181722152
}
2181822153
}
2181922154
#endif
2182022155
2182122156
/************** End of util.c ************************************************/
@@ -24516,10 +24851,11 @@
2451624851
#include <sys/time.h>
2451724852
#include <errno.h>
2451824853
#ifndef SQLITE_OMIT_WAL
2451924854
#include <sys/mman.h>
2452024855
#endif
24856
+
2452124857
2452224858
#if SQLITE_ENABLE_LOCKING_STYLE
2452324859
# include <sys/ioctl.h>
2452424860
# if OS_VXWORKS
2452524861
# include <semaphore.h>
@@ -24600,10 +24936,11 @@
2460024936
** VFS implementations.
2460124937
*/
2460224938
typedef struct unixFile unixFile;
2460324939
struct unixFile {
2460424940
sqlite3_io_methods const *pMethod; /* Always the first entry */
24941
+ sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
2460524942
unixInodeInfo *pInode; /* Info about locks on this inode */
2460624943
int h; /* The file descriptor */
2460724944
unsigned char eFileLock; /* The type of lock held on this fd */
2460824945
unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
2460924946
int lastErrno; /* The unix errno from last I/O error */
@@ -24617,11 +24954,10 @@
2461724954
#endif
2461824955
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
2461924956
unsigned fsFlags; /* cached details from statfs() */
2462024957
#endif
2462124958
#if OS_VXWORKS
24622
- int isDelete; /* Delete on close if true */
2462324959
struct vxworksFileId *pId; /* Unique file ID */
2462424960
#endif
2462524961
#ifndef NDEBUG
2462624962
/* The next group of variables are used to track whether or not the
2462724963
** transaction counter in bytes 24-27 of database files are updated
@@ -24651,10 +24987,14 @@
2465124987
#ifndef SQLITE_DISABLE_DIRSYNC
2465224988
# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
2465324989
#else
2465424990
# define UNIXFILE_DIRSYNC 0x00
2465524991
#endif
24992
+#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
24993
+#define UNIXFILE_DELETE 0x20 /* Delete on close */
24994
+#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
24995
+#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
2465624996
2465724997
/*
2465824998
** Include code that is common to all os_*.c files
2465924999
*/
2466025000
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -25009,10 +25349,16 @@
2500925349
#define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
2501025350
2501125351
{ "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
2501225352
#define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
2501325353
25354
+ { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
25355
+#define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
25356
+
25357
+ { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
25358
+#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
25359
+
2501425360
}; /* End of the overrideable system calls */
2501525361
2501625362
/*
2501725363
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
2501825364
** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26358,11 +26704,11 @@
2635826704
robust_close(pFile, pFile->h, __LINE__);
2635926705
pFile->h = -1;
2636026706
}
2636126707
#if OS_VXWORKS
2636226708
if( pFile->pId ){
26363
- if( pFile->isDelete ){
26709
+ if( pFile->ctrlFlags & UNIXFILE_DELETE ){
2636426710
osUnlink(pFile->pId->zCanonicalName);
2636526711
}
2636626712
vxworksReleaseFileId(pFile->pId);
2636726713
pFile->pId = 0;
2636826714
}
@@ -26447,12 +26793,12 @@
2644726793
2644826794
/******************************************************************************
2644926795
************************* Begin dot-file Locking ******************************
2645026796
**
2645126797
** The dotfile locking implementation uses the existance of separate lock
26452
-** files in order to control access to the database. This works on just
26453
-** about every filesystem imaginable. But there are serious downsides:
26798
+** files (really a directory) to control access to the database. This works
26799
+** on just about every filesystem imaginable. But there are serious downsides:
2645426800
**
2645526801
** (1) There is zero concurrency. A single reader blocks all other
2645626802
** connections from reading or writing the database.
2645726803
**
2645826804
** (2) An application crash or power loss can leave stale lock files
@@ -26459,19 +26805,19 @@
2645926805
** sitting around that need to be cleared manually.
2646026806
**
2646126807
** Nevertheless, a dotlock is an appropriate locking mode for use if no
2646226808
** other locking strategy is available.
2646326809
**
26464
-** Dotfile locking works by creating a file in the same directory as the
26465
-** database and with the same name but with a ".lock" extension added.
26466
-** The existance of a lock file implies an EXCLUSIVE lock. All other lock
26467
-** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
26810
+** Dotfile locking works by creating a subdirectory in the same directory as
26811
+** the database and with the same name but with a ".lock" extension added.
26812
+** The existance of a lock directory implies an EXCLUSIVE lock. All other
26813
+** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
2646826814
*/
2646926815
2647026816
/*
2647126817
** The file suffix added to the data base filename in order to create the
26472
-** lock file.
26818
+** lock directory.
2647326819
*/
2647426820
#define DOTLOCK_SUFFIX ".lock"
2647526821
2647626822
/*
2647726823
** This routine checks if there is a RESERVED lock held on the specified
@@ -26534,11 +26880,10 @@
2653426880
** With dotfile locking, we really only support state (4): EXCLUSIVE.
2653526881
** But we track the other locking levels internally.
2653626882
*/
2653726883
static int dotlockLock(sqlite3_file *id, int eFileLock) {
2653826884
unixFile *pFile = (unixFile*)id;
26539
- int fd;
2654026885
char *zLockFile = (char *)pFile->lockingContext;
2654126886
int rc = SQLITE_OK;
2654226887
2654326888
2654426889
/* If we have any lock, then the lock file already exists. All we have
@@ -26554,13 +26899,13 @@
2655426899
#endif
2655526900
return SQLITE_OK;
2655626901
}
2655726902
2655826903
/* grab an exclusive lock */
26559
- fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
26560
- if( fd<0 ){
26561
- /* failed to open/create the file, someone else may have stolen the lock */
26904
+ rc = osMkdir(zLockFile, 0777);
26905
+ if( rc<0 ){
26906
+ /* failed to open/create the lock directory */
2656226907
int tErrno = errno;
2656326908
if( EEXIST == tErrno ){
2656426909
rc = SQLITE_BUSY;
2656526910
} else {
2656626911
rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -26568,11 +26913,10 @@
2656826913
pFile->lastErrno = tErrno;
2656926914
}
2657026915
}
2657126916
return rc;
2657226917
}
26573
- robust_close(pFile, fd, __LINE__);
2657426918
2657526919
/* got it, set the type and return ok */
2657626920
pFile->eFileLock = eFileLock;
2657726921
return rc;
2657826922
}
@@ -26587,10 +26931,11 @@
2658726931
** When the locking level reaches NO_LOCK, delete the lock file.
2658826932
*/
2658926933
static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
2659026934
unixFile *pFile = (unixFile*)id;
2659126935
char *zLockFile = (char *)pFile->lockingContext;
26936
+ int rc;
2659226937
2659326938
assert( pFile );
2659426939
OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
2659526940
pFile->eFileLock, getpid()));
2659626941
assert( eFileLock<=SHARED_LOCK );
@@ -26608,13 +26953,15 @@
2660826953
return SQLITE_OK;
2660926954
}
2661026955
2661126956
/* To fully unlock the database, delete the lock file */
2661226957
assert( eFileLock==NO_LOCK );
26613
- if( osUnlink(zLockFile) ){
26614
- int rc = 0;
26958
+ rc = osRmdir(zLockFile);
26959
+ if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
26960
+ if( rc<0 ){
2661526961
int tErrno = errno;
26962
+ rc = 0;
2661626963
if( ENOENT != tErrno ){
2661726964
rc = SQLITE_IOERR_UNLOCK;
2661826965
}
2661926966
if( IS_LOCK_ERROR(rc) ){
2662026967
pFile->lastErrno = tErrno;
@@ -27546,39 +27893,52 @@
2754627893
** To avoid stomping the errno value on a failed read the lastErrno value
2754727894
** is set before returning.
2754827895
*/
2754927896
static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
2755027897
int got;
27898
+ int prior = 0;
2755127899
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2755227900
i64 newOffset;
2755327901
#endif
2755427902
TIMER_START;
27903
+ do{
2755527904
#if defined(USE_PREAD)
27556
- do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27557
- SimulateIOError( got = -1 );
27905
+ got = osPread(id->h, pBuf, cnt, offset);
27906
+ SimulateIOError( got = -1 );
2755827907
#elif defined(USE_PREAD64)
27559
- do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
27560
- SimulateIOError( got = -1 );
27561
-#else
27562
- newOffset = lseek(id->h, offset, SEEK_SET);
27563
- SimulateIOError( newOffset-- );
27564
- if( newOffset!=offset ){
27565
- if( newOffset == -1 ){
27566
- ((unixFile*)id)->lastErrno = errno;
27567
- }else{
27568
- ((unixFile*)id)->lastErrno = 0;
27569
- }
27570
- return -1;
27571
- }
27572
- do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27573
-#endif
27574
- TIMER_END;
27575
- if( got<0 ){
27576
- ((unixFile*)id)->lastErrno = errno;
27577
- }
27578
- OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27579
- return got;
27908
+ got = osPread64(id->h, pBuf, cnt, offset);
27909
+ SimulateIOError( got = -1 );
27910
+#else
27911
+ newOffset = lseek(id->h, offset, SEEK_SET);
27912
+ SimulateIOError( newOffset-- );
27913
+ if( newOffset!=offset ){
27914
+ if( newOffset == -1 ){
27915
+ ((unixFile*)id)->lastErrno = errno;
27916
+ }else{
27917
+ ((unixFile*)id)->lastErrno = 0;
27918
+ }
27919
+ return -1;
27920
+ }
27921
+ got = osRead(id->h, pBuf, cnt);
27922
+#endif
27923
+ if( got==cnt ) break;
27924
+ if( got<0 ){
27925
+ if( errno==EINTR ){ got = 1; continue; }
27926
+ prior = 0;
27927
+ ((unixFile*)id)->lastErrno = errno;
27928
+ break;
27929
+ }else if( got>0 ){
27930
+ cnt -= got;
27931
+ offset += got;
27932
+ prior += got;
27933
+ pBuf = (void*)(got + (char*)pBuf);
27934
+ }
27935
+ }while( got>0 );
27936
+ TIMER_END;
27937
+ OSTRACE(("READ %-3d %5d %7lld %llu\n",
27938
+ id->h, got+prior, offset-prior, TIMER_ELAPSED));
27939
+ return got+prior;
2758027940
}
2758127941
2758227942
/*
2758327943
** Read data from a file into a buffer. Return SQLITE_OK if all
2758427944
** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -28078,10 +28438,26 @@
2807828438
}
2807928439
}
2808028440
2808128441
return SQLITE_OK;
2808228442
}
28443
+
28444
+/*
28445
+** If *pArg is inititially negative then this is a query. Set *pArg to
28446
+** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
28447
+**
28448
+** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
28449
+*/
28450
+static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
28451
+ if( *pArg<0 ){
28452
+ *pArg = (pFile->ctrlFlags & mask)!=0;
28453
+ }else if( (*pArg)==0 ){
28454
+ pFile->ctrlFlags &= ~mask;
28455
+ }else{
28456
+ pFile->ctrlFlags |= mask;
28457
+ }
28458
+}
2808328459
2808428460
/*
2808528461
** Information and control of an open file handle.
2808628462
*/
2808728463
static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -28105,18 +28481,19 @@
2810528481
rc = fcntlSizeHint(pFile, *(i64 *)pArg);
2810628482
SimulateIOErrorBenign(0);
2810728483
return rc;
2810828484
}
2810928485
case SQLITE_FCNTL_PERSIST_WAL: {
28110
- int bPersist = *(int*)pArg;
28111
- if( bPersist<0 ){
28112
- *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
28113
- }else if( bPersist==0 ){
28114
- pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL;
28115
- }else{
28116
- pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL;
28117
- }
28486
+ unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
28487
+ return SQLITE_OK;
28488
+ }
28489
+ case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
28490
+ unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
28491
+ return SQLITE_OK;
28492
+ }
28493
+ case SQLITE_FCNTL_VFSNAME: {
28494
+ *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
2811828495
return SQLITE_OK;
2811928496
}
2812028497
#ifndef NDEBUG
2812128498
/* The pager calls this method to signal that it has done
2812228499
** a rollback and that the database is therefore unchanged and
@@ -28132,13 +28509,10 @@
2813228509
case SQLITE_SET_LOCKPROXYFILE:
2813328510
case SQLITE_GET_LOCKPROXYFILE: {
2813428511
return proxyFileControl(id,op,pArg);
2813528512
}
2813628513
#endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
28137
- case SQLITE_FCNTL_SYNC_OMITTED: {
28138
- return SQLITE_OK; /* A no-op */
28139
- }
2814028514
}
2814128515
return SQLITE_NOTFOUND;
2814228516
}
2814328517
2814428518
/*
@@ -28149,21 +28523,35 @@
2814928523
** SQLite code assumes this function cannot fail. It also assumes that
2815028524
** if two files are created in the same file-system directory (i.e.
2815128525
** a database and its journal file) that the sector size will be the
2815228526
** same for both.
2815328527
*/
28154
-static int unixSectorSize(sqlite3_file *NotUsed){
28155
- UNUSED_PARAMETER(NotUsed);
28528
+static int unixSectorSize(sqlite3_file *pFile){
28529
+ (void)pFile;
2815628530
return SQLITE_DEFAULT_SECTOR_SIZE;
2815728531
}
2815828532
2815928533
/*
28160
-** Return the device characteristics for the file. This is always 0 for unix.
28534
+** Return the device characteristics for the file.
28535
+**
28536
+** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
28537
+** However, that choice is contraversial since technically the underlying
28538
+** file system does not always provide powersafe overwrites. (In other
28539
+** words, after a power-loss event, parts of the file that were never
28540
+** written might end up being altered.) However, non-PSOW behavior is very,
28541
+** very rare. And asserting PSOW makes a large reduction in the amount
28542
+** of required I/O for journaling, since a lot of padding is eliminated.
28543
+** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
28544
+** available to turn it off and URI query parameter available to turn it off.
2816128545
*/
28162
-static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
28163
- UNUSED_PARAMETER(NotUsed);
28164
- return 0;
28546
+static int unixDeviceCharacteristics(sqlite3_file *id){
28547
+ unixFile *p = (unixFile*)id;
28548
+ if( p->ctrlFlags & UNIXFILE_PSOW ){
28549
+ return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
28550
+ }else{
28551
+ return 0;
28552
+ }
2816528553
}
2816628554
2816728555
#ifndef SQLITE_OMIT_WAL
2816828556
2816928557
@@ -28414,20 +28802,20 @@
2841428802
rc = SQLITE_IOERR_FSTAT;
2841528803
goto shm_open_err;
2841628804
}
2841728805
2841828806
#ifdef SQLITE_SHM_DIRECTORY
28419
- nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
28807
+ nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
2842028808
#else
28421
- nShmFilename = 5 + (int)strlen(pDbFd->zPath);
28809
+ nShmFilename = 6 + (int)strlen(pDbFd->zPath);
2842228810
#endif
2842328811
pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
2842428812
if( pShmNode==0 ){
2842528813
rc = SQLITE_NOMEM;
2842628814
goto shm_open_err;
2842728815
}
28428
- memset(pShmNode, 0, sizeof(*pShmNode));
28816
+ memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
2842928817
zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
2843028818
#ifdef SQLITE_SHM_DIRECTORY
2843128819
sqlite3_snprintf(nShmFilename, zShmFilename,
2843228820
SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
2843328821
(u32)sStat.st_ino, (u32)sStat.st_dev);
@@ -28443,14 +28831,12 @@
2844328831
rc = SQLITE_NOMEM;
2844428832
goto shm_open_err;
2844528833
}
2844628834
2844728835
if( pInode->bProcessLock==0 ){
28448
- const char *zRO;
2844928836
int openFlags = O_RDWR | O_CREAT;
28450
- zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28451
- if( zRO && sqlite3GetBoolean(zRO) ){
28837
+ if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
2845228838
openFlags = O_RDONLY;
2845328839
pShmNode->isReadonly = 1;
2845428840
}
2845528841
pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
2845628842
if( pShmNode->h<0 ){
@@ -29108,28 +29494,20 @@
2910829494
** Initialize the contents of the unixFile structure pointed to by pId.
2910929495
*/
2911029496
static int fillInUnixFile(
2911129497
sqlite3_vfs *pVfs, /* Pointer to vfs object */
2911229498
int h, /* Open file descriptor of file being opened */
29113
- int syncDir, /* True to sync directory on first sync */
2911429499
sqlite3_file *pId, /* Write to the unixFile structure here */
2911529500
const char *zFilename, /* Name of the file being opened */
29116
- int noLock, /* Omit locking if true */
29117
- int isDelete, /* Delete on close if true */
29118
- int isReadOnly /* True if the file is opened read-only */
29501
+ int ctrlFlags /* Zero or more UNIXFILE_* values */
2911929502
){
2912029503
const sqlite3_io_methods *pLockingStyle;
2912129504
unixFile *pNew = (unixFile *)pId;
2912229505
int rc = SQLITE_OK;
2912329506
2912429507
assert( pNew->pInode==NULL );
2912529508
29126
- /* Parameter isDelete is only used on vxworks. Express this explicitly
29127
- ** here to prevent compiler warnings about unused parameters.
29128
- */
29129
- UNUSED_PARAMETER(isDelete);
29130
-
2913129509
/* Usually the path zFilename should not be a relative pathname. The
2913229510
** exception is when opening the proxy "conch" file in builds that
2913329511
** include the special Apple locking styles.
2913429512
*/
2913529513
#if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29138,36 +29516,34 @@
2913829516
#else
2913929517
assert( zFilename==0 || zFilename[0]=='/' );
2914029518
#endif
2914129519
2914229520
/* No locking occurs in temporary files */
29143
- assert( zFilename!=0 || noLock );
29521
+ assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
2914429522
2914529523
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2914629524
pNew->h = h;
29525
+ pNew->pVfs = pVfs;
2914729526
pNew->zPath = zFilename;
29527
+ pNew->ctrlFlags = (u8)ctrlFlags;
29528
+ if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
29529
+ "psow", SQLITE_POWERSAFE_OVERWRITE) ){
29530
+ pNew->ctrlFlags |= UNIXFILE_PSOW;
29531
+ }
2914829532
if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
29149
- pNew->ctrlFlags = UNIXFILE_EXCL;
29150
- }else{
29151
- pNew->ctrlFlags = 0;
29152
- }
29153
- if( isReadOnly ){
29154
- pNew->ctrlFlags |= UNIXFILE_RDONLY;
29155
- }
29156
- if( syncDir ){
29157
- pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
29533
+ pNew->ctrlFlags |= UNIXFILE_EXCL;
2915829534
}
2915929535
2916029536
#if OS_VXWORKS
2916129537
pNew->pId = vxworksFindFileId(zFilename);
2916229538
if( pNew->pId==0 ){
29163
- noLock = 1;
29539
+ ctrlFlags |= UNIXFILE_NOLOCK;
2916429540
rc = SQLITE_NOMEM;
2916529541
}
2916629542
#endif
2916729543
29168
- if( noLock ){
29544
+ if( ctrlFlags & UNIXFILE_NOLOCK ){
2916929545
pLockingStyle = &nolockIoMethods;
2917029546
}else{
2917129547
pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
2917229548
#if SQLITE_ENABLE_LOCKING_STYLE
2917329549
/* Cache zFilename in the locking context (AFP and dotlock override) for
@@ -29284,11 +29660,11 @@
2928429660
if( h>=0 ) robust_close(pNew, h, __LINE__);
2928529661
h = -1;
2928629662
osUnlink(zFilename);
2928729663
isDelete = 0;
2928829664
}
29289
- pNew->isDelete = isDelete;
29665
+ if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
2929029666
#endif
2929129667
if( rc!=SQLITE_OK ){
2929229668
if( h>=0 ) robust_close(pNew, h, __LINE__);
2929329669
}else{
2929429670
pNew->pMethod = pLockingStyle;
@@ -29349,22 +29725,23 @@
2934929725
if( zDir==0 ) zDir = ".";
2935029726
2935129727
/* Check that the output buffer is large enough for the temporary file
2935229728
** name. If it is not, return SQLITE_ERROR.
2935329729
*/
29354
- if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
29730
+ if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
2935529731
return SQLITE_ERROR;
2935629732
}
2935729733
2935829734
do{
29359
- sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29735
+ sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
2936029736
j = (int)strlen(zBuf);
2936129737
sqlite3_randomness(15, &zBuf[j]);
2936229738
for(i=0; i<15; i++, j++){
2936329739
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
2936429740
}
2936529741
zBuf[j] = 0;
29742
+ zBuf[j+1] = 0;
2936629743
}while( osAccess(zBuf,0)==0 );
2936729744
return SQLITE_OK;
2936829745
}
2936929746
2937029747
#if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
@@ -29481,11 +29858,11 @@
2948129858
** where NN is a decimal number. The NN naming schemes are
2948229859
** used by the test_multiplex.c module.
2948329860
*/
2948429861
nDb = sqlite3Strlen30(zPath) - 1;
2948529862
#ifdef SQLITE_ENABLE_8_3_NAMES
29486
- while( nDb>0 && !sqlite3Isalnum(zPath[nDb]) ) nDb--;
29863
+ while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
2948729864
if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
2948829865
#else
2948929866
while( zPath[nDb]!='-' ){
2949029867
assert( nDb>0 );
2949129868
assert( zPath[nDb]!='\n' );
@@ -29539,10 +29916,11 @@
2953929916
int fd = -1; /* File descriptor returned by open() */
2954029917
int openFlags = 0; /* Flags to pass to open() */
2954129918
int eType = flags&0xFFFFFF00; /* Type of file to open */
2954229919
int noLock; /* True to omit locking primitives */
2954329920
int rc = SQLITE_OK; /* Function Return Code */
29921
+ int ctrlFlags = 0; /* UNIXFILE_* flags */
2954429922
2954529923
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
2954629924
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
2954729925
int isCreate = (flags & SQLITE_OPEN_CREATE);
2954829926
int isReadonly = (flags & SQLITE_OPEN_READONLY);
@@ -29565,11 +29943,11 @@
2956529943
));
2956629944
2956729945
/* If argument zPath is a NULL pointer, this function is required to open
2956829946
** a temporary file. Use this buffer to store the file name in.
2956929947
*/
29570
- char zTmpname[MAX_PATHNAME+1];
29948
+ char zTmpname[MAX_PATHNAME+2];
2957129949
const char *zName = zPath;
2957229950
2957329951
/* Check the following statements are true:
2957429952
**
2957529953
** (a) Exactly one of the READWRITE and READONLY flags must be set, and
@@ -29608,18 +29986,28 @@
2960829986
if( !pUnused ){
2960929987
return SQLITE_NOMEM;
2961029988
}
2961129989
}
2961229990
p->pUnused = pUnused;
29991
+
29992
+ /* Database filenames are double-zero terminated if they are not
29993
+ ** URIs with parameters. Hence, they can always be passed into
29994
+ ** sqlite3_uri_parameter(). */
29995
+ assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
29996
+
2961329997
}else if( !zName ){
2961429998
/* If zName is NULL, the upper layer is requesting a temp file. */
2961529999
assert(isDelete && !syncDir);
29616
- rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
30000
+ rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
2961730001
if( rc!=SQLITE_OK ){
2961830002
return rc;
2961930003
}
2962030004
zName = zTmpname;
30005
+
30006
+ /* Generated temporary filenames are always double-zero terminated
30007
+ ** for use by sqlite3_uri_parameter(). */
30008
+ assert( zName[strlen(zName)+1]==0 );
2962130009
}
2962230010
2962330011
/* Determine the value of the flags parameter passed to POSIX function
2962430012
** open(). These must be calculated even if open() is not called, as
2962530013
** they may be stored as part of the file handle and used by the
@@ -29692,11 +30080,18 @@
2969230080
}
2969330081
if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
2969430082
((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
2969530083
}
2969630084
#endif
29697
-
30085
+
30086
+ /* Set up appropriate ctrlFlags */
30087
+ if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
30088
+ if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
30089
+ if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
30090
+ if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
30091
+ if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
30092
+
2969830093
#if SQLITE_ENABLE_LOCKING_STYLE
2969930094
#if SQLITE_PREFER_PROXY_LOCKING
2970030095
isAutoProxy = 1;
2970130096
#endif
2970230097
if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
@@ -29722,12 +30117,11 @@
2972230117
goto open_finished;
2972330118
}
2972430119
useProxy = !(fsInfo.f_flags&MNT_LOCAL);
2972530120
}
2972630121
if( useProxy ){
29727
- rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29728
- isDelete, isReadonly);
30122
+ rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
2972930123
if( rc==SQLITE_OK ){
2973030124
rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
2973130125
if( rc!=SQLITE_OK ){
2973230126
/* Use unixClose to clean up the resources added in fillInUnixFile
2973330127
** and clear all the structure's references. Specifically,
@@ -29740,12 +30134,12 @@
2974030134
goto open_finished;
2974130135
}
2974230136
}
2974330137
#endif
2974430138
29745
- rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29746
- isDelete, isReadonly);
30139
+ rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
30140
+
2974730141
open_finished:
2974830142
if( rc!=SQLITE_OK ){
2974930143
sqlite3_free(p->pUnused);
2975030144
}
2975130145
return rc;
@@ -29766,11 +30160,11 @@
2976630160
SimulateIOError(return SQLITE_IOERR_DELETE);
2976730161
if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
2976830162
return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
2976930163
}
2977030164
#ifndef SQLITE_DISABLE_DIRSYNC
29771
- if( dirSync ){
30165
+ if( (dirSync & 1)!=0 ){
2977230166
int fd;
2977330167
rc = osOpenDirectory(zPath, &fd);
2977430168
if( rc==SQLITE_OK ){
2977530169
#if OS_VXWORKS
2977630170
if( fsync(fd)==-1 )
@@ -30317,11 +30711,11 @@
3031730711
if( lockPath[i] == '/' && (i - start > 0) ){
3031830712
/* only mkdir if leaf dir != "." or "/" or ".." */
3031930713
if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
3032030714
|| (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
3032130715
buf[i]='\0';
30322
- if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30716
+ if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
3032330717
int err=errno;
3032430718
if( err!=EEXIST ) {
3032530719
OSTRACE(("CREATELOCKPATH FAILED creating %s, "
3032630720
"'%s' proxy lock path=%s pid=%d\n",
3032730721
buf, strerror(err), lockPath, getpid()));
@@ -30412,11 +30806,11 @@
3041230806
dummyVfs.zName = "dummy";
3041330807
pUnused->fd = fd;
3041430808
pUnused->flags = openFlags;
3041530809
pNew->pUnused = pUnused;
3041630810
30417
- rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
30811
+ rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
3041830812
if( rc==SQLITE_OK ){
3041930813
*ppFile = pNew;
3042030814
return SQLITE_OK;
3042130815
}
3042230816
end_create_proxy:
@@ -31353,11 +31747,11 @@
3135331747
};
3135431748
unsigned int i; /* Loop counter */
3135531749
3135631750
/* Double-check that the aSyscall[] array has been constructed
3135731751
** correctly. See ticket [bb3a86e890c8e96ab] */
31358
- assert( ArraySize(aSyscall)==18 );
31752
+ assert( ArraySize(aSyscall)==20 );
3135931753
3136031754
/* Register all VFSes defined in the aVfs[] array */
3136131755
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3136231756
sqlite3_vfs_register(&aVfs[i], i==0);
3136331757
}
@@ -31389,54 +31783,18 @@
3138931783
** May you find forgiveness for yourself and forgive others.
3139031784
** May you share freely, never taking more than you give.
3139131785
**
3139231786
******************************************************************************
3139331787
**
31394
-** This file contains code that is specific to windows.
31395
-*/
31396
-#if SQLITE_OS_WIN /* This file is used for windows only */
31397
-
31398
-
31399
-/*
31400
-** A Note About Memory Allocation:
31401
-**
31402
-** This driver uses malloc()/free() directly rather than going through
31403
-** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
31404
-** are designed for use on embedded systems where memory is scarce and
31405
-** malloc failures happen frequently. Win32 does not typically run on
31406
-** embedded systems, and when it does the developers normally have bigger
31407
-** problems to worry about than running out of memory. So there is not
31408
-** a compelling need to use the wrappers.
31409
-**
31410
-** But there is a good reason to not use the wrappers. If we use the
31411
-** wrappers then we will get simulated malloc() failures within this
31412
-** driver. And that causes all kinds of problems for our tests. We
31413
-** could enhance SQLite to deal with simulated malloc failures within
31414
-** the OS driver, but the code to deal with those failure would not
31415
-** be exercised on Linux (which does not need to malloc() in the driver)
31416
-** and so we would have difficulty writing coverage tests for that
31417
-** code. Better to leave the code out, we think.
31418
-**
31419
-** The point of this discussion is as follows: When creating a new
31420
-** OS layer for an embedded system, if you use this file as an example,
31421
-** avoid the use of malloc()/free(). Those routines work ok on windows
31422
-** desktops but not so well in embedded systems.
31423
-*/
31424
-
31425
-#include <winbase.h>
31788
+** This file contains code that is specific to Windows.
31789
+*/
31790
+#if SQLITE_OS_WIN /* This file is used for Windows only */
3142631791
3142731792
#ifdef __CYGWIN__
3142831793
# include <sys/cygwin.h>
3142931794
#endif
3143031795
31431
-/*
31432
-** Macros used to determine whether or not to use threads.
31433
-*/
31434
-#if defined(THREADSAFE) && THREADSAFE
31435
-# define SQLITE_W32_THREADS 1
31436
-#endif
31437
-
3143831796
/*
3143931797
** Include code that is common to all os_*.c files
3144031798
*/
3144131799
/************** Include os_common.h in the middle of os_win.c ****************/
3144231800
/************** Begin file os_common.h ***************************************/
@@ -31647,25 +32005,16 @@
3164732005
3164832006
/************** End of os_common.h *******************************************/
3164932007
/************** Continuing where we left off in os_win.c *********************/
3165032008
3165132009
/*
31652
-** Some microsoft compilers lack this definition.
32010
+** Some Microsoft compilers lack this definition.
3165332011
*/
3165432012
#ifndef INVALID_FILE_ATTRIBUTES
3165532013
# define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
3165632014
#endif
3165732015
31658
-/*
31659
-** Determine if we are dealing with WindowsCE - which has a much
31660
-** reduced API.
31661
-*/
31662
-#if SQLITE_OS_WINCE
31663
-# define AreFileApisANSI() 1
31664
-# define FormatMessageW(a,b,c,d,e,f,g) 0
31665
-#endif
31666
-
3166732016
/* Forward references */
3166832017
typedef struct winShm winShm; /* A connection to shared-memory */
3166932018
typedef struct winShmNode winShmNode; /* A region of shared-memory */
3167032019
3167132020
/*
@@ -31690,25 +32039,30 @@
3169032039
const sqlite3_io_methods *pMethod; /*** Must be first ***/
3169132040
sqlite3_vfs *pVfs; /* The VFS used to open this file */
3169232041
HANDLE h; /* Handle for accessing the file */
3169332042
u8 locktype; /* Type of lock currently held on this file */
3169432043
short sharedLockByte; /* Randomly chosen byte used as a shared lock */
31695
- u8 bPersistWal; /* True to persist WAL files */
32044
+ u8 ctrlFlags; /* Flags. See WINFILE_* below */
3169632045
DWORD lastErrno; /* The Windows errno from the last I/O error */
31697
- DWORD sectorSize; /* Sector size of the device file is on */
3169832046
winShm *pShm; /* Instance of shared memory on this file */
3169932047
const char *zPath; /* Full pathname of this file */
3170032048
int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
3170132049
#if SQLITE_OS_WINCE
31702
- WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
32050
+ LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
3170332051
HANDLE hMutex; /* Mutex used to control access to shared lock */
3170432052
HANDLE hShared; /* Shared memory segment used for locking */
3170532053
winceLock local; /* Locks obtained by this instance of winFile */
3170632054
winceLock *shared; /* Global shared lock memory for the file */
3170732055
#endif
3170832056
};
3170932057
32058
+/*
32059
+** Allowed values for winFile.ctrlFlags
32060
+*/
32061
+#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
32062
+#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
32063
+
3171032064
/*
3171132065
* If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
3171232066
* various Win32 API heap functions instead of our own.
3171332067
*/
3171432068
#ifdef SQLITE_WIN32_MALLOC
@@ -31776,25 +32130,17 @@
3177632130
static void winMemShutdown(void *pAppData);
3177732131
3177832132
SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
3177932133
#endif /* SQLITE_WIN32_MALLOC */
3178032134
31781
-/*
31782
-** Forward prototypes.
31783
-*/
31784
-static int getSectorSize(
31785
- sqlite3_vfs *pVfs,
31786
- const char *zRelative /* UTF-8 file name */
31787
-);
31788
-
3178932135
/*
3179032136
** The following variable is (normally) set once and never changes
31791
-** thereafter. It records whether the operating system is Win95
32137
+** thereafter. It records whether the operating system is Win9x
3179232138
** or WinNT.
3179332139
**
3179432140
** 0: Operating system unknown.
31795
-** 1: Operating system is Win95.
32141
+** 1: Operating system is Win9x.
3179632142
** 2: Operating system is WinNT.
3179732143
**
3179832144
** In order to facilitate testing on a WinNT system, the test fixture
3179932145
** can manually set this value to 1 to emulate Win98 behavior.
3180032146
*/
@@ -31801,10 +32147,540 @@
3180132147
#ifdef SQLITE_TEST
3180232148
SQLITE_API int sqlite3_os_type = 0;
3180332149
#else
3180432150
static int sqlite3_os_type = 0;
3180532151
#endif
32152
+
32153
+/*
32154
+** Many system calls are accessed through pointer-to-functions so that
32155
+** they may be overridden at runtime to facilitate fault injection during
32156
+** testing and sandboxing. The following array holds the names and pointers
32157
+** to all overrideable system calls.
32158
+*/
32159
+#if !SQLITE_OS_WINCE
32160
+# define SQLITE_WIN32_HAS_ANSI
32161
+#endif
32162
+
32163
+#if SQLITE_OS_WINCE || SQLITE_OS_WINNT
32164
+# define SQLITE_WIN32_HAS_WIDE
32165
+#endif
32166
+
32167
+#ifndef SYSCALL
32168
+# define SYSCALL sqlite3_syscall_ptr
32169
+#endif
32170
+
32171
+#if SQLITE_OS_WINCE
32172
+/*
32173
+** These macros are necessary because Windows CE does not natively support the
32174
+** Win32 APIs LockFile, UnlockFile, and LockFileEx.
32175
+ */
32176
+
32177
+# define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
32178
+# define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
32179
+# define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
32180
+
32181
+/*
32182
+** These are the special syscall hacks for Windows CE. The locking related
32183
+** defines here refer to the macros defined just above.
32184
+ */
32185
+
32186
+# define osAreFileApisANSI() 1
32187
+# define osLockFile LockFile
32188
+# define osUnlockFile UnlockFile
32189
+# define osLockFileEx LockFileEx
32190
+#endif
32191
+
32192
+static struct win_syscall {
32193
+ const char *zName; /* Name of the sytem call */
32194
+ sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
32195
+ sqlite3_syscall_ptr pDefault; /* Default value */
32196
+} aSyscall[] = {
32197
+#if !SQLITE_OS_WINCE
32198
+ { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
32199
+
32200
+#define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
32201
+#else
32202
+ { "AreFileApisANSI", (SYSCALL)0, 0 },
32203
+#endif
32204
+
32205
+#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32206
+ { "CharLowerW", (SYSCALL)CharLowerW, 0 },
32207
+#else
32208
+ { "CharLowerW", (SYSCALL)0, 0 },
32209
+#endif
32210
+
32211
+#define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
32212
+
32213
+#if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32214
+ { "CharUpperW", (SYSCALL)CharUpperW, 0 },
32215
+#else
32216
+ { "CharUpperW", (SYSCALL)0, 0 },
32217
+#endif
32218
+
32219
+#define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
32220
+
32221
+ { "CloseHandle", (SYSCALL)CloseHandle, 0 },
32222
+
32223
+#define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
32224
+
32225
+#if defined(SQLITE_WIN32_HAS_ANSI)
32226
+ { "CreateFileA", (SYSCALL)CreateFileA, 0 },
32227
+#else
32228
+ { "CreateFileA", (SYSCALL)0, 0 },
32229
+#endif
32230
+
32231
+#define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
32232
+ LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
32233
+
32234
+#if defined(SQLITE_WIN32_HAS_WIDE)
32235
+ { "CreateFileW", (SYSCALL)CreateFileW, 0 },
32236
+#else
32237
+ { "CreateFileW", (SYSCALL)0, 0 },
32238
+#endif
32239
+
32240
+#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
32241
+ LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
32242
+
32243
+ { "CreateFileMapping", (SYSCALL)CreateFileMapping, 0 },
32244
+
32245
+#define osCreateFileMapping ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
32246
+ DWORD,DWORD,DWORD,LPCTSTR))aSyscall[6].pCurrent)
32247
+
32248
+#if defined(SQLITE_WIN32_HAS_WIDE)
32249
+ { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
32250
+#else
32251
+ { "CreateFileMappingW", (SYSCALL)0, 0 },
32252
+#endif
32253
+
32254
+#define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
32255
+ DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
32256
+
32257
+#if defined(SQLITE_WIN32_HAS_WIDE)
32258
+ { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
32259
+#else
32260
+ { "CreateMutexW", (SYSCALL)0, 0 },
32261
+#endif
32262
+
32263
+#define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
32264
+ LPCWSTR))aSyscall[8].pCurrent)
32265
+
32266
+#if defined(SQLITE_WIN32_HAS_ANSI)
32267
+ { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
32268
+#else
32269
+ { "DeleteFileA", (SYSCALL)0, 0 },
32270
+#endif
32271
+
32272
+#define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
32273
+
32274
+#if defined(SQLITE_WIN32_HAS_WIDE)
32275
+ { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
32276
+#else
32277
+ { "DeleteFileW", (SYSCALL)0, 0 },
32278
+#endif
32279
+
32280
+#define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
32281
+
32282
+#if SQLITE_OS_WINCE
32283
+ { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
32284
+#else
32285
+ { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
32286
+#endif
32287
+
32288
+#define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
32289
+ LPFILETIME))aSyscall[11].pCurrent)
32290
+
32291
+#if SQLITE_OS_WINCE
32292
+ { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
32293
+#else
32294
+ { "FileTimeToSystemTime", (SYSCALL)0, 0 },
32295
+#endif
32296
+
32297
+#define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
32298
+ LPSYSTEMTIME))aSyscall[12].pCurrent)
32299
+
32300
+ { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
32301
+
32302
+#define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
32303
+
32304
+#if defined(SQLITE_WIN32_HAS_ANSI)
32305
+ { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
32306
+#else
32307
+ { "FormatMessageA", (SYSCALL)0, 0 },
32308
+#endif
32309
+
32310
+#define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
32311
+ DWORD,va_list*))aSyscall[14].pCurrent)
32312
+
32313
+#if defined(SQLITE_WIN32_HAS_WIDE)
32314
+ { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
32315
+#else
32316
+ { "FormatMessageW", (SYSCALL)0, 0 },
32317
+#endif
32318
+
32319
+#define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
32320
+ DWORD,va_list*))aSyscall[15].pCurrent)
32321
+
32322
+ { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
32323
+
32324
+#define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
32325
+
32326
+ { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
32327
+
32328
+#define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
32329
+
32330
+#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
32331
+ { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
32332
+#else
32333
+ { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
32334
+#endif
32335
+
32336
+#define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
32337
+ LPDWORD))aSyscall[18].pCurrent)
32338
+
32339
+#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32340
+ { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
32341
+#else
32342
+ { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
32343
+#endif
32344
+
32345
+#define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
32346
+ LPDWORD))aSyscall[19].pCurrent)
32347
+
32348
+#if defined(SQLITE_WIN32_HAS_ANSI)
32349
+ { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
32350
+#else
32351
+ { "GetFileAttributesA", (SYSCALL)0, 0 },
32352
+#endif
32353
+
32354
+#define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
32355
+
32356
+#if defined(SQLITE_WIN32_HAS_WIDE)
32357
+ { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
32358
+#else
32359
+ { "GetFileAttributesW", (SYSCALL)0, 0 },
32360
+#endif
32361
+
32362
+#define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
32363
+
32364
+#if defined(SQLITE_WIN32_HAS_WIDE)
32365
+ { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
32366
+#else
32367
+ { "GetFileAttributesExW", (SYSCALL)0, 0 },
32368
+#endif
32369
+
32370
+#define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
32371
+ LPVOID))aSyscall[22].pCurrent)
32372
+
32373
+ { "GetFileSize", (SYSCALL)GetFileSize, 0 },
32374
+
32375
+#define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
32376
+
32377
+#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
32378
+ { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
32379
+#else
32380
+ { "GetFullPathNameA", (SYSCALL)0, 0 },
32381
+#endif
32382
+
32383
+#define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
32384
+ LPSTR*))aSyscall[24].pCurrent)
32385
+
32386
+#if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32387
+ { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
32388
+#else
32389
+ { "GetFullPathNameW", (SYSCALL)0, 0 },
32390
+#endif
32391
+
32392
+#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
32393
+ LPWSTR*))aSyscall[25].pCurrent)
32394
+
32395
+ { "GetLastError", (SYSCALL)GetLastError, 0 },
32396
+
32397
+#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
32398
+
32399
+#if SQLITE_OS_WINCE
32400
+ /* The GetProcAddressA() routine is only available on Windows CE. */
32401
+ { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
32402
+#else
32403
+ /* All other Windows platforms expect GetProcAddress() to take
32404
+ ** an ANSI string regardless of the _UNICODE setting */
32405
+ { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
32406
+#endif
32407
+
32408
+#define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
32409
+ LPCSTR))aSyscall[27].pCurrent)
32410
+
32411
+ { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
32412
+
32413
+#define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
32414
+
32415
+ { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
32416
+
32417
+#define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
32418
+
32419
+#if !SQLITE_OS_WINCE
32420
+ { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
32421
+#else
32422
+ { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
32423
+#endif
32424
+
32425
+#define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
32426
+ LPFILETIME))aSyscall[30].pCurrent)
32427
+
32428
+#if defined(SQLITE_WIN32_HAS_ANSI)
32429
+ { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
32430
+#else
32431
+ { "GetTempPathA", (SYSCALL)0, 0 },
32432
+#endif
32433
+
32434
+#define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
32435
+
32436
+#if defined(SQLITE_WIN32_HAS_WIDE)
32437
+ { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
32438
+#else
32439
+ { "GetTempPathW", (SYSCALL)0, 0 },
32440
+#endif
32441
+
32442
+#define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
32443
+
32444
+ { "GetTickCount", (SYSCALL)GetTickCount, 0 },
32445
+
32446
+#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
32447
+
32448
+#if defined(SQLITE_WIN32_HAS_ANSI)
32449
+ { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
32450
+#else
32451
+ { "GetVersionExA", (SYSCALL)0, 0 },
32452
+#endif
32453
+
32454
+#define osGetVersionExA ((BOOL(WINAPI*)( \
32455
+ LPOSVERSIONINFOA))aSyscall[34].pCurrent)
32456
+
32457
+ { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
32458
+
32459
+#define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
32460
+ SIZE_T))aSyscall[35].pCurrent)
32461
+
32462
+ { "HeapCreate", (SYSCALL)HeapCreate, 0 },
32463
+
32464
+#define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
32465
+ SIZE_T))aSyscall[36].pCurrent)
32466
+
32467
+ { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
32468
+
32469
+#define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
32470
+
32471
+ { "HeapFree", (SYSCALL)HeapFree, 0 },
32472
+
32473
+#define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
32474
+
32475
+ { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
32476
+
32477
+#define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
32478
+ SIZE_T))aSyscall[39].pCurrent)
32479
+
32480
+ { "HeapSize", (SYSCALL)HeapSize, 0 },
32481
+
32482
+#define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
32483
+ LPCVOID))aSyscall[40].pCurrent)
32484
+
32485
+ { "HeapValidate", (SYSCALL)HeapValidate, 0 },
32486
+
32487
+#define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
32488
+ LPCVOID))aSyscall[41].pCurrent)
32489
+
32490
+#if defined(SQLITE_WIN32_HAS_ANSI)
32491
+ { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
32492
+#else
32493
+ { "LoadLibraryA", (SYSCALL)0, 0 },
32494
+#endif
32495
+
32496
+#define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
32497
+
32498
+#if defined(SQLITE_WIN32_HAS_WIDE)
32499
+ { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
32500
+#else
32501
+ { "LoadLibraryW", (SYSCALL)0, 0 },
32502
+#endif
32503
+
32504
+#define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
32505
+
32506
+ { "LocalFree", (SYSCALL)LocalFree, 0 },
32507
+
32508
+#define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
32509
+
32510
+#if !SQLITE_OS_WINCE
32511
+ { "LockFile", (SYSCALL)LockFile, 0 },
32512
+
32513
+#define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32514
+ DWORD))aSyscall[45].pCurrent)
32515
+#else
32516
+ { "LockFile", (SYSCALL)0, 0 },
32517
+#endif
32518
+
32519
+#if !SQLITE_OS_WINCE
32520
+ { "LockFileEx", (SYSCALL)LockFileEx, 0 },
32521
+
32522
+#define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
32523
+ LPOVERLAPPED))aSyscall[46].pCurrent)
32524
+#else
32525
+ { "LockFileEx", (SYSCALL)0, 0 },
32526
+#endif
32527
+
32528
+ { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
32529
+
32530
+#define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32531
+ SIZE_T))aSyscall[47].pCurrent)
32532
+
32533
+ { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
32534
+
32535
+#define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
32536
+ int))aSyscall[48].pCurrent)
32537
+
32538
+ { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
32539
+
32540
+#define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
32541
+ LARGE_INTEGER*))aSyscall[49].pCurrent)
32542
+
32543
+ { "ReadFile", (SYSCALL)ReadFile, 0 },
32544
+
32545
+#define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
32546
+ LPOVERLAPPED))aSyscall[50].pCurrent)
32547
+
32548
+ { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
32549
+
32550
+#define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
32551
+
32552
+ { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
32553
+
32554
+#define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
32555
+ DWORD))aSyscall[52].pCurrent)
32556
+
32557
+ { "Sleep", (SYSCALL)Sleep, 0 },
32558
+
32559
+#define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
32560
+
32561
+ { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
32562
+
32563
+#define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
32564
+ LPFILETIME))aSyscall[54].pCurrent)
32565
+
32566
+#if !SQLITE_OS_WINCE
32567
+ { "UnlockFile", (SYSCALL)UnlockFile, 0 },
32568
+
32569
+#define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32570
+ DWORD))aSyscall[55].pCurrent)
32571
+#else
32572
+ { "UnlockFile", (SYSCALL)0, 0 },
32573
+#endif
32574
+
32575
+#if !SQLITE_OS_WINCE
32576
+ { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
32577
+
32578
+#define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32579
+ LPOVERLAPPED))aSyscall[56].pCurrent)
32580
+#else
32581
+ { "UnlockFileEx", (SYSCALL)0, 0 },
32582
+#endif
32583
+
32584
+ { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
32585
+
32586
+#define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
32587
+
32588
+ { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
32589
+
32590
+#define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
32591
+ LPCSTR,LPBOOL))aSyscall[58].pCurrent)
32592
+
32593
+ { "WriteFile", (SYSCALL)WriteFile, 0 },
32594
+
32595
+#define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
32596
+ LPOVERLAPPED))aSyscall[59].pCurrent)
32597
+
32598
+}; /* End of the overrideable system calls */
32599
+
32600
+/*
32601
+** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32602
+** "win32" VFSes. Return SQLITE_OK opon successfully updating the
32603
+** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32604
+** system call named zName.
32605
+*/
32606
+static int winSetSystemCall(
32607
+ sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
32608
+ const char *zName, /* Name of system call to override */
32609
+ sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
32610
+){
32611
+ unsigned int i;
32612
+ int rc = SQLITE_NOTFOUND;
32613
+
32614
+ UNUSED_PARAMETER(pNotUsed);
32615
+ if( zName==0 ){
32616
+ /* If no zName is given, restore all system calls to their default
32617
+ ** settings and return NULL
32618
+ */
32619
+ rc = SQLITE_OK;
32620
+ for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32621
+ if( aSyscall[i].pDefault ){
32622
+ aSyscall[i].pCurrent = aSyscall[i].pDefault;
32623
+ }
32624
+ }
32625
+ }else{
32626
+ /* If zName is specified, operate on only the one system call
32627
+ ** specified.
32628
+ */
32629
+ for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32630
+ if( strcmp(zName, aSyscall[i].zName)==0 ){
32631
+ if( aSyscall[i].pDefault==0 ){
32632
+ aSyscall[i].pDefault = aSyscall[i].pCurrent;
32633
+ }
32634
+ rc = SQLITE_OK;
32635
+ if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32636
+ aSyscall[i].pCurrent = pNewFunc;
32637
+ break;
32638
+ }
32639
+ }
32640
+ }
32641
+ return rc;
32642
+}
32643
+
32644
+/*
32645
+** Return the value of a system call. Return NULL if zName is not a
32646
+** recognized system call name. NULL is also returned if the system call
32647
+** is currently undefined.
32648
+*/
32649
+static sqlite3_syscall_ptr winGetSystemCall(
32650
+ sqlite3_vfs *pNotUsed,
32651
+ const char *zName
32652
+){
32653
+ unsigned int i;
32654
+
32655
+ UNUSED_PARAMETER(pNotUsed);
32656
+ for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32657
+ if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32658
+ }
32659
+ return 0;
32660
+}
32661
+
32662
+/*
32663
+** Return the name of the first system call after zName. If zName==NULL
32664
+** then return the name of the first system call. Return NULL if zName
32665
+** is the last system call or if zName is not the name of a valid
32666
+** system call.
32667
+*/
32668
+static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
32669
+ int i = -1;
32670
+
32671
+ UNUSED_PARAMETER(p);
32672
+ if( zName ){
32673
+ for(i=0; i<ArraySize(aSyscall)-1; i++){
32674
+ if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32675
+ }
32676
+ }
32677
+ for(i++; i<ArraySize(aSyscall); i++){
32678
+ if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32679
+ }
32680
+ return 0;
32681
+}
3180632682
3180732683
/*
3180832684
** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
3180932685
** or WinCE. Return false (zero) for Win95, Win98, or WinME.
3181032686
**
@@ -31818,13 +32694,13 @@
3181832694
#if SQLITE_OS_WINCE
3181932695
# define isNT() (1)
3182032696
#else
3182132697
static int isNT(void){
3182232698
if( sqlite3_os_type==0 ){
31823
- OSVERSIONINFO sInfo;
32699
+ OSVERSIONINFOA sInfo;
3182432700
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31825
- GetVersionEx(&sInfo);
32701
+ osGetVersionExA(&sInfo);
3182632702
sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
3182732703
}
3182832704
return sqlite3_os_type==2;
3182932705
}
3183032706
#endif /* SQLITE_OS_WINCE */
@@ -31840,17 +32716,17 @@
3184032716
winMemAssertMagic();
3184132717
hHeap = winMemGetHeap();
3184232718
assert( hHeap!=0 );
3184332719
assert( hHeap!=INVALID_HANDLE_VALUE );
3184432720
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31845
- assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32721
+ assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
3184632722
#endif
3184732723
assert( nBytes>=0 );
31848
- p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32724
+ p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
3184932725
if( !p ){
3185032726
sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
31851
- nBytes, GetLastError(), (void*)hHeap);
32727
+ nBytes, osGetLastError(), (void*)hHeap);
3185232728
}
3185332729
return p;
3185432730
}
3185532731
3185632732
/*
@@ -31862,16 +32738,16 @@
3186232738
winMemAssertMagic();
3186332739
hHeap = winMemGetHeap();
3186432740
assert( hHeap!=0 );
3186532741
assert( hHeap!=INVALID_HANDLE_VALUE );
3186632742
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31867
- assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32743
+ assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
3186832744
#endif
3186932745
if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
31870
- if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
32746
+ if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
3187132747
sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
31872
- pPrior, GetLastError(), (void*)hHeap);
32748
+ pPrior, osGetLastError(), (void*)hHeap);
3187332749
}
3187432750
}
3187532751
3187632752
/*
3187732753
** Change the size of an existing memory allocation
@@ -31883,22 +32759,22 @@
3188332759
winMemAssertMagic();
3188432760
hHeap = winMemGetHeap();
3188532761
assert( hHeap!=0 );
3188632762
assert( hHeap!=INVALID_HANDLE_VALUE );
3188732763
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31888
- assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32764
+ assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
3188932765
#endif
3189032766
assert( nBytes>=0 );
3189132767
if( !pPrior ){
31892
- p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32768
+ p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
3189332769
}else{
31894
- p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
32770
+ p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
3189532771
}
3189632772
if( !p ){
3189732773
sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
31898
- pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
31899
- (void*)hHeap);
32774
+ pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
32775
+ (void*)hHeap);
3190032776
}
3190132777
return p;
3190232778
}
3190332779
3190432780
/*
@@ -31911,17 +32787,17 @@
3191132787
winMemAssertMagic();
3191232788
hHeap = winMemGetHeap();
3191332789
assert( hHeap!=0 );
3191432790
assert( hHeap!=INVALID_HANDLE_VALUE );
3191532791
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31916
- assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32792
+ assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
3191732793
#endif
3191832794
if( !p ) return 0;
31919
- n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
32795
+ n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
3192032796
if( n==(SIZE_T)-1 ){
3192132797
sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
31922
- p, GetLastError(), (void*)hHeap);
32798
+ p, osGetLastError(), (void*)hHeap);
3192332799
return 0;
3192432800
}
3192532801
return (int)n;
3192632802
}
3192732803
@@ -31939,26 +32815,26 @@
3193932815
winMemData *pWinMemData = (winMemData *)pAppData;
3194032816
3194132817
if( !pWinMemData ) return SQLITE_ERROR;
3194232818
assert( pWinMemData->magic==WINMEM_MAGIC );
3194332819
if( !pWinMemData->hHeap ){
31944
- pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS,
31945
- SQLITE_WIN32_HEAP_INIT_SIZE,
31946
- SQLITE_WIN32_HEAP_MAX_SIZE);
32820
+ pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
32821
+ SQLITE_WIN32_HEAP_INIT_SIZE,
32822
+ SQLITE_WIN32_HEAP_MAX_SIZE);
3194732823
if( !pWinMemData->hHeap ){
3194832824
sqlite3_log(SQLITE_NOMEM,
3194932825
"failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
31950
- GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE,
31951
- SQLITE_WIN32_HEAP_MAX_SIZE);
32826
+ osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
32827
+ SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
3195232828
return SQLITE_NOMEM;
3195332829
}
3195432830
pWinMemData->bOwned = TRUE;
3195532831
}
3195632832
assert( pWinMemData->hHeap!=0 );
3195732833
assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
3195832834
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31959
- assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32835
+ assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
3196032836
#endif
3196132837
return SQLITE_OK;
3196232838
}
3196332839
3196432840
/*
@@ -31969,16 +32845,16 @@
3196932845
3197032846
if( !pWinMemData ) return;
3197132847
if( pWinMemData->hHeap ){
3197232848
assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
3197332849
#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31974
- assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32850
+ assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
3197532851
#endif
3197632852
if( pWinMemData->bOwned ){
31977
- if( !HeapDestroy(pWinMemData->hHeap) ){
32853
+ if( !osHeapDestroy(pWinMemData->hHeap) ){
3197832854
sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
31979
- GetLastError(), (void*)pWinMemData->hHeap);
32855
+ osGetLastError(), (void*)pWinMemData->hHeap);
3198032856
}
3198132857
pWinMemData->bOwned = FALSE;
3198232858
}
3198332859
pWinMemData->hHeap = NULL;
3198432860
}
@@ -32010,197 +32886,219 @@
3201032886
sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
3201132887
}
3201232888
#endif /* SQLITE_WIN32_MALLOC */
3201332889
3201432890
/*
32015
-** Convert a UTF-8 string to microsoft unicode (UTF-16?).
32891
+** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
3201632892
**
3201732893
** Space to hold the returned string is obtained from malloc.
3201832894
*/
32019
-static WCHAR *utf8ToUnicode(const char *zFilename){
32895
+static LPWSTR utf8ToUnicode(const char *zFilename){
3202032896
int nChar;
32021
- WCHAR *zWideFilename;
32897
+ LPWSTR zWideFilename;
3202232898
32023
- nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32024
- zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
32899
+ nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32900
+ if( nChar==0 ){
32901
+ return 0;
32902
+ }
32903
+ zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
3202532904
if( zWideFilename==0 ){
3202632905
return 0;
3202732906
}
32028
- nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
32907
+ nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
32908
+ nChar);
3202932909
if( nChar==0 ){
32030
- free(zWideFilename);
32910
+ sqlite3_free(zWideFilename);
3203132911
zWideFilename = 0;
3203232912
}
3203332913
return zWideFilename;
3203432914
}
3203532915
3203632916
/*
32037
-** Convert microsoft unicode to UTF-8. Space to hold the returned string is
32038
-** obtained from malloc().
32917
+** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
32918
+** obtained from sqlite3_malloc().
3203932919
*/
32040
-static char *unicodeToUtf8(const WCHAR *zWideFilename){
32920
+static char *unicodeToUtf8(LPCWSTR zWideFilename){
3204132921
int nByte;
3204232922
char *zFilename;
3204332923
32044
- nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32045
- zFilename = malloc( nByte );
32924
+ nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32925
+ if( nByte == 0 ){
32926
+ return 0;
32927
+ }
32928
+ zFilename = sqlite3_malloc( nByte );
3204632929
if( zFilename==0 ){
3204732930
return 0;
3204832931
}
32049
- nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32050
- 0, 0);
32932
+ nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32933
+ 0, 0);
3205132934
if( nByte == 0 ){
32052
- free(zFilename);
32935
+ sqlite3_free(zFilename);
3205332936
zFilename = 0;
3205432937
}
3205532938
return zFilename;
3205632939
}
3205732940
3205832941
/*
32059
-** Convert an ansi string to microsoft unicode, based on the
32942
+** Convert an ANSI string to Microsoft Unicode, based on the
3206032943
** current codepage settings for file apis.
3206132944
**
3206232945
** Space to hold the returned string is obtained
32063
-** from malloc.
32946
+** from sqlite3_malloc.
3206432947
*/
32065
-static WCHAR *mbcsToUnicode(const char *zFilename){
32948
+static LPWSTR mbcsToUnicode(const char *zFilename){
3206632949
int nByte;
32067
- WCHAR *zMbcsFilename;
32068
- int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32950
+ LPWSTR zMbcsFilename;
32951
+ int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3206932952
32070
- nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
32071
- zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
32953
+ nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
32954
+ 0)*sizeof(WCHAR);
32955
+ if( nByte==0 ){
32956
+ return 0;
32957
+ }
32958
+ zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
3207232959
if( zMbcsFilename==0 ){
3207332960
return 0;
3207432961
}
32075
- nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
32962
+ nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
32963
+ nByte);
3207632964
if( nByte==0 ){
32077
- free(zMbcsFilename);
32965
+ sqlite3_free(zMbcsFilename);
3207832966
zMbcsFilename = 0;
3207932967
}
3208032968
return zMbcsFilename;
3208132969
}
3208232970
3208332971
/*
32084
-** Convert microsoft unicode to multibyte character string, based on the
32085
-** user's Ansi codepage.
32972
+** Convert Microsoft Unicode to multi-byte character string, based on the
32973
+** user's ANSI codepage.
3208632974
**
3208732975
** Space to hold the returned string is obtained from
32088
-** malloc().
32976
+** sqlite3_malloc().
3208932977
*/
32090
-static char *unicodeToMbcs(const WCHAR *zWideFilename){
32978
+static char *unicodeToMbcs(LPCWSTR zWideFilename){
3209132979
int nByte;
3209232980
char *zFilename;
32093
- int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32981
+ int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3209432982
32095
- nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32096
- zFilename = malloc( nByte );
32983
+ nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32984
+ if( nByte == 0 ){
32985
+ return 0;
32986
+ }
32987
+ zFilename = sqlite3_malloc( nByte );
3209732988
if( zFilename==0 ){
3209832989
return 0;
3209932990
}
32100
- nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
32101
- 0, 0);
32991
+ nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
32992
+ nByte, 0, 0);
3210232993
if( nByte == 0 ){
32103
- free(zFilename);
32994
+ sqlite3_free(zFilename);
3210432995
zFilename = 0;
3210532996
}
3210632997
return zFilename;
3210732998
}
3210832999
3210933000
/*
3211033001
** Convert multibyte character string to UTF-8. Space to hold the
32111
-** returned string is obtained from malloc().
33002
+** returned string is obtained from sqlite3_malloc().
3211233003
*/
3211333004
SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
3211433005
char *zFilenameUtf8;
32115
- WCHAR *zTmpWide;
33006
+ LPWSTR zTmpWide;
3211633007
3211733008
zTmpWide = mbcsToUnicode(zFilename);
3211833009
if( zTmpWide==0 ){
3211933010
return 0;
3212033011
}
3212133012
zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32122
- free(zTmpWide);
33013
+ sqlite3_free(zTmpWide);
3212333014
return zFilenameUtf8;
3212433015
}
3212533016
3212633017
/*
3212733018
** Convert UTF-8 to multibyte character string. Space to hold the
32128
-** returned string is obtained from malloc().
33019
+** returned string is obtained from sqlite3_malloc().
3212933020
*/
3213033021
SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
3213133022
char *zFilenameMbcs;
32132
- WCHAR *zTmpWide;
33023
+ LPWSTR zTmpWide;
3213333024
3213433025
zTmpWide = utf8ToUnicode(zFilename);
3213533026
if( zTmpWide==0 ){
3213633027
return 0;
3213733028
}
3213833029
zFilenameMbcs = unicodeToMbcs(zTmpWide);
32139
- free(zTmpWide);
33030
+ sqlite3_free(zTmpWide);
3214033031
return zFilenameMbcs;
3214133032
}
3214233033
3214333034
3214433035
/*
3214533036
** The return value of getLastErrorMsg
3214633037
** is zero if the error message fits in the buffer, or non-zero
3214733038
** otherwise (if the message was truncated).
3214833039
*/
32149
-static int getLastErrorMsg(int nBuf, char *zBuf){
33040
+static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
3215033041
/* FormatMessage returns 0 on failure. Otherwise it
3215133042
** returns the number of TCHARs written to the output
3215233043
** buffer, excluding the terminating null char.
3215333044
*/
32154
- DWORD error = GetLastError();
3215533045
DWORD dwLen = 0;
3215633046
char *zOut = 0;
3215733047
3215833048
if( isNT() ){
32159
- WCHAR *zTempWide = NULL;
32160
- dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32161
- NULL,
32162
- error,
32163
- 0,
32164
- (LPWSTR) &zTempWide,
32165
- 0,
32166
- 0);
33049
+ LPWSTR zTempWide = NULL;
33050
+ dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33051
+ FORMAT_MESSAGE_FROM_SYSTEM |
33052
+ FORMAT_MESSAGE_IGNORE_INSERTS,
33053
+ NULL,
33054
+ lastErrno,
33055
+ 0,
33056
+ (LPWSTR) &zTempWide,
33057
+ 0,
33058
+ 0);
3216733059
if( dwLen > 0 ){
3216833060
/* allocate a buffer and convert to UTF8 */
33061
+ sqlite3BeginBenignMalloc();
3216933062
zOut = unicodeToUtf8(zTempWide);
33063
+ sqlite3EndBenignMalloc();
3217033064
/* free the system buffer allocated by FormatMessage */
32171
- LocalFree(zTempWide);
33065
+ osLocalFree(zTempWide);
3217233066
}
3217333067
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32174
-** Since the ASCII version of these Windows API do not exist for WINCE,
33068
+** Since the ANSI version of these Windows API do not exist for WINCE,
3217533069
** it's important to not reference them for WINCE builds.
3217633070
*/
3217733071
#if SQLITE_OS_WINCE==0
3217833072
}else{
3217933073
char *zTemp = NULL;
32180
- dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32181
- NULL,
32182
- error,
32183
- 0,
32184
- (LPSTR) &zTemp,
32185
- 0,
32186
- 0);
33074
+ dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33075
+ FORMAT_MESSAGE_FROM_SYSTEM |
33076
+ FORMAT_MESSAGE_IGNORE_INSERTS,
33077
+ NULL,
33078
+ lastErrno,
33079
+ 0,
33080
+ (LPSTR) &zTemp,
33081
+ 0,
33082
+ 0);
3218733083
if( dwLen > 0 ){
3218833084
/* allocate a buffer and convert to UTF8 */
33085
+ sqlite3BeginBenignMalloc();
3218933086
zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33087
+ sqlite3EndBenignMalloc();
3219033088
/* free the system buffer allocated by FormatMessage */
32191
- LocalFree(zTemp);
33089
+ osLocalFree(zTemp);
3219233090
}
3219333091
#endif
3219433092
}
3219533093
if( 0 == dwLen ){
32196
- sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
33094
+ sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
3219733095
}else{
3219833096
/* copy a maximum of nBuf chars to output buffer */
3219933097
sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
3220033098
/* free the UTF8 buffer */
32201
- free(zOut);
33099
+ sqlite3_free(zOut);
3220233100
}
3220333101
return 0;
3220433102
}
3220533103
3220633104
/*
@@ -32216,30 +33114,30 @@
3221633114
** The first argument passed to the macro should be the error code that
3221733115
** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
3221833116
** The two subsequent arguments should be the name of the OS function that
3221933117
** failed and the the associated file-system path, if any.
3222033118
*/
32221
-#define winLogError(a,b,c) winLogErrorAtLine(a,b,c,__LINE__)
33119
+#define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
3222233120
static int winLogErrorAtLine(
3222333121
int errcode, /* SQLite error code */
33122
+ DWORD lastErrno, /* Win32 last error */
3222433123
const char *zFunc, /* Name of OS function that failed */
3222533124
const char *zPath, /* File path associated with error */
3222633125
int iLine /* Source line number where error occurred */
3222733126
){
3222833127
char zMsg[500]; /* Human readable error text */
3222933128
int i; /* Loop counter */
32230
- DWORD iErrno = GetLastError(); /* Error code */
3223133129
3223233130
zMsg[0] = 0;
32233
- getLastErrorMsg(sizeof(zMsg), zMsg);
33131
+ getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
3223433132
assert( errcode!=SQLITE_OK );
3223533133
if( zPath==0 ) zPath = "";
3223633134
for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
3223733135
zMsg[i] = 0;
3223833136
sqlite3_log(errcode,
3223933137
"os_win.c:%d: (%d) %s(%s) - %s",
32240
- iLine, iErrno, zFunc, zPath, zMsg
33138
+ iLine, lastErrno, zFunc, zPath, zMsg
3224133139
);
3224233140
3224333141
return errcode;
3224433142
}
3224533143
@@ -32261,23 +33159,28 @@
3226133159
/*
3226233160
** If a ReadFile() or WriteFile() error occurs, invoke this routine
3226333161
** to see if it should be retried. Return TRUE to retry. Return FALSE
3226433162
** to give up with an error.
3226533163
*/
32266
-static int retryIoerr(int *pnRetry){
32267
- DWORD e;
33164
+static int retryIoerr(int *pnRetry, DWORD *pError){
33165
+ DWORD e = osGetLastError();
3226833166
if( *pnRetry>=win32IoerrRetry ){
33167
+ if( pError ){
33168
+ *pError = e;
33169
+ }
3226933170
return 0;
3227033171
}
32271
- e = GetLastError();
3227233172
if( e==ERROR_ACCESS_DENIED ||
3227333173
e==ERROR_LOCK_VIOLATION ||
3227433174
e==ERROR_SHARING_VIOLATION ){
32275
- Sleep(win32IoerrRetryDelay*(1+*pnRetry));
33175
+ osSleep(win32IoerrRetryDelay*(1+*pnRetry));
3227633176
++*pnRetry;
3227733177
return 1;
3227833178
}
33179
+ if( pError ){
33180
+ *pError = e;
33181
+ }
3227933182
return 0;
3228033183
}
3228133184
3228233185
/*
3228333186
** Log a I/O error retry episode.
@@ -32294,11 +33197,11 @@
3229433197
#if SQLITE_OS_WINCE
3229533198
/*************************************************************************
3229633199
** This section contains code for WinCE only.
3229733200
*/
3229833201
/*
32299
-** WindowsCE does not have a localtime() function. So create a
33202
+** Windows CE does not have a localtime() function. So create a
3230033203
** substitute.
3230133204
*/
3230233205
/* #include <time.h> */
3230333206
struct tm *__cdecl localtime(const time_t *t)
3230433207
{
@@ -32308,12 +33211,12 @@
3230833211
sqlite3_int64 t64;
3230933212
t64 = *t;
3231033213
t64 = (t64 + 11644473600)*10000000;
3231133214
uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
3231233215
uTm.dwHighDateTime= (DWORD)(t64 >> 32);
32313
- FileTimeToLocalFileTime(&uTm,&lTm);
32314
- FileTimeToSystemTime(&lTm,&pTm);
33216
+ osFileTimeToLocalFileTime(&uTm,&lTm);
33217
+ osFileTimeToSystemTime(&lTm,&pTm);
3231533218
y.tm_year = pTm.wYear - 1900;
3231633219
y.tm_mon = pTm.wMonth - 1;
3231733220
y.tm_wday = pTm.wDayOfWeek;
3231833221
y.tm_mday = pTm.wDay;
3231933222
y.tm_hour = pTm.wHour;
@@ -32320,17 +33223,10 @@
3232033223
y.tm_min = pTm.wMinute;
3232133224
y.tm_sec = pTm.wSecond;
3232233225
return &y;
3232333226
}
3232433227
32325
-/* This will never be called, but defined to make the code compile */
32326
-#define GetTempPathA(a,b)
32327
-
32328
-#define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
32329
-#define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
32330
-#define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
32331
-
3233233228
#define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
3233333229
3233433230
/*
3233533231
** Acquire a lock on the handle h
3233633232
*/
@@ -32348,30 +33244,36 @@
3234833244
/*
3234933245
** Create the mutex and shared memory used for locking in the file
3235033246
** descriptor pFile
3235133247
*/
3235233248
static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
32353
- WCHAR *zTok;
32354
- WCHAR *zName = utf8ToUnicode(zFilename);
33249
+ LPWSTR zTok;
33250
+ LPWSTR zName;
3235533251
BOOL bInit = TRUE;
33252
+
33253
+ zName = utf8ToUnicode(zFilename);
33254
+ if( zName==0 ){
33255
+ /* out of memory */
33256
+ return FALSE;
33257
+ }
3235633258
3235733259
/* Initialize the local lockdata */
32358
- ZeroMemory(&pFile->local, sizeof(pFile->local));
33260
+ memset(&pFile->local, 0, sizeof(pFile->local));
3235933261
3236033262
/* Replace the backslashes from the filename and lowercase it
3236133263
** to derive a mutex name. */
32362
- zTok = CharLowerW(zName);
33264
+ zTok = osCharLowerW(zName);
3236333265
for (;*zTok;zTok++){
3236433266
if (*zTok == '\\') *zTok = '_';
3236533267
}
3236633268
3236733269
/* Create/open the named mutex */
32368
- pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
33270
+ pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
3236933271
if (!pFile->hMutex){
32370
- pFile->lastErrno = GetLastError();
32371
- winLogError(SQLITE_ERROR, "winceCreateLock1", zFilename);
32372
- free(zName);
33272
+ pFile->lastErrno = osGetLastError();
33273
+ winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
33274
+ sqlite3_free(zName);
3237333275
return FALSE;
3237433276
}
3237533277
3237633278
/* Acquire the mutex before continuing */
3237733279
winceMutexAcquire(pFile->hMutex);
@@ -32378,47 +33280,48 @@
3237833280
3237933281
/* Since the names of named mutexes, semaphores, file mappings etc are
3238033282
** case-sensitive, take advantage of that by uppercasing the mutex name
3238133283
** and using that as the shared filemapping name.
3238233284
*/
32383
- CharUpperW(zName);
32384
- pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
32385
- PAGE_READWRITE, 0, sizeof(winceLock),
32386
- zName);
33285
+ osCharUpperW(zName);
33286
+ pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
33287
+ PAGE_READWRITE, 0, sizeof(winceLock),
33288
+ zName);
3238733289
3238833290
/* Set a flag that indicates we're the first to create the memory so it
3238933291
** must be zero-initialized */
32390
- if (GetLastError() == ERROR_ALREADY_EXISTS){
33292
+ if (osGetLastError() == ERROR_ALREADY_EXISTS){
3239133293
bInit = FALSE;
3239233294
}
3239333295
32394
- free(zName);
33296
+ sqlite3_free(zName);
3239533297
3239633298
/* If we succeeded in making the shared memory handle, map it. */
3239733299
if (pFile->hShared){
32398
- pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
33300
+ pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
3239933301
FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
3240033302
/* If mapping failed, close the shared memory handle and erase it */
3240133303
if (!pFile->shared){
32402
- pFile->lastErrno = GetLastError();
32403
- winLogError(SQLITE_ERROR, "winceCreateLock2", zFilename);
32404
- CloseHandle(pFile->hShared);
33304
+ pFile->lastErrno = osGetLastError();
33305
+ winLogError(SQLITE_ERROR, pFile->lastErrno,
33306
+ "winceCreateLock2", zFilename);
33307
+ osCloseHandle(pFile->hShared);
3240533308
pFile->hShared = NULL;
3240633309
}
3240733310
}
3240833311
3240933312
/* If shared memory could not be created, then close the mutex and fail */
3241033313
if (pFile->hShared == NULL){
3241133314
winceMutexRelease(pFile->hMutex);
32412
- CloseHandle(pFile->hMutex);
33315
+ osCloseHandle(pFile->hMutex);
3241333316
pFile->hMutex = NULL;
3241433317
return FALSE;
3241533318
}
3241633319
3241733320
/* Initialize the shared memory if we're supposed to */
3241833321
if (bInit) {
32419
- ZeroMemory(pFile->shared, sizeof(winceLock));
33322
+ memset(pFile->shared, 0, sizeof(winceLock));
3242033323
}
3242133324
3242233325
winceMutexRelease(pFile->hMutex);
3242333326
return TRUE;
3242433327
}
@@ -32445,22 +33348,22 @@
3244533348
if (pFile->local.bExclusive){
3244633349
pFile->shared->bExclusive = FALSE;
3244733350
}
3244833351
3244933352
/* De-reference and close our copy of the shared memory handle */
32450
- UnmapViewOfFile(pFile->shared);
32451
- CloseHandle(pFile->hShared);
33353
+ osUnmapViewOfFile(pFile->shared);
33354
+ osCloseHandle(pFile->hShared);
3245233355
3245333356
/* Done with the mutex */
3245433357
winceMutexRelease(pFile->hMutex);
32455
- CloseHandle(pFile->hMutex);
33358
+ osCloseHandle(pFile->hMutex);
3245633359
pFile->hMutex = NULL;
3245733360
}
3245833361
}
3245933362
3246033363
/*
32461
-** An implementation of the LockFile() API of windows for wince
33364
+** An implementation of the LockFile() API of Windows for CE
3246233365
*/
3246333366
static BOOL winceLockFile(
3246433367
HANDLE *phFile,
3246533368
DWORD dwFileOffsetLow,
3246633369
DWORD dwFileOffsetHigh,
@@ -32520,11 +33423,11 @@
3252033423
winceMutexRelease(pFile->hMutex);
3252133424
return bReturn;
3252233425
}
3252333426
3252433427
/*
32525
-** An implementation of the UnlockFile API of windows for wince
33428
+** An implementation of the UnlockFile API of Windows for CE
3252633429
*/
3252733430
static BOOL winceUnlockFile(
3252833431
HANDLE *phFile,
3252933432
DWORD dwFileOffsetLow,
3253033433
DWORD dwFileOffsetHigh,
@@ -32582,11 +33485,11 @@
3258233485
winceMutexRelease(pFile->hMutex);
3258333486
return bReturn;
3258433487
}
3258533488
3258633489
/*
32587
-** An implementation of the LockFileEx() API of windows for wince
33490
+** An implementation of the LockFileEx() API of Windows for CE
3258833491
*/
3258933492
static BOOL winceLockFileEx(
3259033493
HANDLE *phFile,
3259133494
DWORD dwFlags,
3259233495
DWORD dwReserved,
@@ -32615,11 +33518,11 @@
3261533518
** The next group of routines implement the I/O methods specified
3261633519
** by the sqlite3_io_methods object.
3261733520
******************************************************************************/
3261833521
3261933522
/*
32620
-** Some microsoft compilers lack this definition.
33523
+** Some Microsoft compilers lack this definition.
3262133524
*/
3262233525
#ifndef INVALID_SET_FILE_POINTER
3262333526
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
3262433527
#endif
3262533528
@@ -32630,10 +33533,11 @@
3263033533
*/
3263133534
static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
3263233535
LONG upperBits; /* Most sig. 32 bits of new offset */
3263333536
LONG lowerBits; /* Least sig. 32 bits of new offset */
3263433537
DWORD dwRet; /* Value returned by SetFilePointer() */
33538
+ DWORD lastErrno; /* Value returned by GetLastError() */
3263533539
3263633540
upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
3263733541
lowerBits = (LONG)(iOffset & 0xffffffff);
3263833542
3263933543
/* API oddity: If successful, SetFilePointer() returns a dword
@@ -32641,14 +33545,17 @@
3264133545
** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
3264233546
** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
3264333547
** whether an error has actually occured, it is also necessary to call
3264433548
** GetLastError().
3264533549
*/
32646
- dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
32647
- if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
32648
- pFile->lastErrno = GetLastError();
32649
- winLogError(SQLITE_IOERR_SEEK, "seekWinFile", pFile->zPath);
33550
+ dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
33551
+
33552
+ if( (dwRet==INVALID_SET_FILE_POINTER
33553
+ && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
33554
+ pFile->lastErrno = lastErrno;
33555
+ winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33556
+ "seekWinFile", pFile->zPath);
3265033557
return 1;
3265133558
}
3265233559
3265333560
return 0;
3265433561
}
@@ -32655,11 +33562,11 @@
3265533562
3265633563
/*
3265733564
** Close a file.
3265833565
**
3265933566
** It is reported that an attempt to close a handle might sometimes
32660
-** fail. This is a very unreasonable result, but windows is notorious
33567
+** fail. This is a very unreasonable result, but Windows is notorious
3266133568
** for being unreasonable so I do not doubt that it might happen. If
3266233569
** the close fails, we pause for 100 milliseconds and try again. As
3266333570
** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
3266433571
** giving up and returning an error.
3266533572
*/
@@ -32670,32 +33577,33 @@
3267033577
3267133578
assert( id!=0 );
3267233579
assert( pFile->pShm==0 );
3267333580
OSTRACE(("CLOSE %d\n", pFile->h));
3267433581
do{
32675
- rc = CloseHandle(pFile->h);
33582
+ rc = osCloseHandle(pFile->h);
3267633583
/* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32677
- }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
33584
+ }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (osSleep(100), 1) );
3267833585
#if SQLITE_OS_WINCE
3267933586
#define WINCE_DELETION_ATTEMPTS 3
3268033587
winceDestroyLock(pFile);
3268133588
if( pFile->zDeleteOnClose ){
3268233589
int cnt = 0;
3268333590
while(
32684
- DeleteFileW(pFile->zDeleteOnClose)==0
32685
- && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
33591
+ osDeleteFileW(pFile->zDeleteOnClose)==0
33592
+ && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
3268633593
&& cnt++ < WINCE_DELETION_ATTEMPTS
3268733594
){
32688
- Sleep(100); /* Wait a little before trying again */
33595
+ osSleep(100); /* Wait a little before trying again */
3268933596
}
32690
- free(pFile->zDeleteOnClose);
33597
+ sqlite3_free(pFile->zDeleteOnClose);
3269133598
}
3269233599
#endif
3269333600
OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
3269433601
OpenCounter(-1);
3269533602
return rc ? SQLITE_OK
32696
- : winLogError(SQLITE_IOERR_CLOSE, "winClose", pFile->zPath);
33603
+ : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
33604
+ "winClose", pFile->zPath);
3269733605
}
3269833606
3269933607
/*
3270033608
** Read data from a file into a buffer. Return SQLITE_OK if all
3270133609
** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -32716,14 +33624,16 @@
3271633624
OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
3271733625
3271833626
if( seekWinFile(pFile, offset) ){
3271933627
return SQLITE_FULL;
3272033628
}
32721
- while( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32722
- if( retryIoerr(&nRetry) ) continue;
32723
- pFile->lastErrno = GetLastError();
32724
- return winLogError(SQLITE_IOERR_READ, "winRead", pFile->zPath);
33629
+ while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
33630
+ DWORD lastErrno;
33631
+ if( retryIoerr(&nRetry, &lastErrno) ) continue;
33632
+ pFile->lastErrno = lastErrno;
33633
+ return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
33634
+ "winRead", pFile->zPath);
3272533635
}
3272633636
logIoerr(nRetry);
3272733637
if( nRead<(DWORD)amt ){
3272833638
/* Unread parts of the buffer must be zero-filled */
3272933639
memset(&((char*)pBuf)[nRead], 0, amt-nRead);
@@ -32757,32 +33667,34 @@
3275733667
rc = seekWinFile(pFile, offset);
3275833668
if( rc==0 ){
3275933669
u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
3276033670
int nRem = amt; /* Number of bytes yet to be written */
3276133671
DWORD nWrite; /* Bytes written by each WriteFile() call */
33672
+ DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
3276233673
3276333674
while( nRem>0 ){
32764
- if( !WriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
32765
- if( retryIoerr(&nRetry) ) continue;
33675
+ if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33676
+ if( retryIoerr(&nRetry, &lastErrno) ) continue;
3276633677
break;
3276733678
}
3276833679
if( nWrite<=0 ) break;
3276933680
aRem += nWrite;
3277033681
nRem -= nWrite;
3277133682
}
3277233683
if( nRem>0 ){
32773
- pFile->lastErrno = GetLastError();
33684
+ pFile->lastErrno = lastErrno;
3277433685
rc = 1;
3277533686
}
3277633687
}
3277733688
3277833689
if( rc ){
3277933690
if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
3278033691
|| ( pFile->lastErrno==ERROR_DISK_FULL )){
3278133692
return SQLITE_FULL;
3278233693
}
32783
- return winLogError(SQLITE_IOERR_WRITE, "winWrite", pFile->zPath);
33694
+ return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33695
+ "winWrite", pFile->zPath);
3278433696
}else{
3278533697
logIoerr(nRetry);
3278633698
}
3278733699
return SQLITE_OK;
3278833700
}
@@ -32808,14 +33720,16 @@
3280833720
nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3280933721
}
3281033722
3281133723
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
3281233724
if( seekWinFile(pFile, nByte) ){
32813
- rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
32814
- }else if( 0==SetEndOfFile(pFile->h) ){
32815
- pFile->lastErrno = GetLastError();
32816
- rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate2", pFile->zPath);
33725
+ rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33726
+ "winTruncate1", pFile->zPath);
33727
+ }else if( 0==osSetEndOfFile(pFile->h) ){
33728
+ pFile->lastErrno = osGetLastError();
33729
+ rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33730
+ "winTruncate2", pFile->zPath);
3281733731
}
3281833732
3281933733
OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
3282033734
return rc;
3282133735
}
@@ -32876,17 +33790,18 @@
3287633790
** no-op
3287733791
*/
3287833792
#ifdef SQLITE_NO_SYNC
3287933793
return SQLITE_OK;
3288033794
#else
32881
- rc = FlushFileBuffers(pFile->h);
33795
+ rc = osFlushFileBuffers(pFile->h);
3288233796
SimulateIOError( rc=FALSE );
3288333797
if( rc ){
3288433798
return SQLITE_OK;
3288533799
}else{
32886
- pFile->lastErrno = GetLastError();
32887
- return winLogError(SQLITE_IOERR_FSYNC, "winSync", pFile->zPath);
33800
+ pFile->lastErrno = osGetLastError();
33801
+ return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33802
+ "winSync", pFile->zPath);
3288833803
}
3288933804
#endif
3289033805
}
3289133806
3289233807
/*
@@ -32894,20 +33809,21 @@
3289433809
*/
3289533810
static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
3289633811
DWORD upperBits;
3289733812
DWORD lowerBits;
3289833813
winFile *pFile = (winFile*)id;
32899
- DWORD error;
33814
+ DWORD lastErrno;
3290033815
3290133816
assert( id!=0 );
3290233817
SimulateIOError(return SQLITE_IOERR_FSTAT);
32903
- lowerBits = GetFileSize(pFile->h, &upperBits);
33818
+ lowerBits = osGetFileSize(pFile->h, &upperBits);
3290433819
if( (lowerBits == INVALID_FILE_SIZE)
32905
- && ((error = GetLastError()) != NO_ERROR) )
33820
+ && ((lastErrno = osGetLastError())!=NO_ERROR) )
3290633821
{
32907
- pFile->lastErrno = error;
32908
- return winLogError(SQLITE_IOERR_FSTAT, "winFileSize", pFile->zPath);
33822
+ pFile->lastErrno = lastErrno;
33823
+ return winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33824
+ "winFileSize", pFile->zPath);
3290933825
}
3291033826
*pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
3291133827
return SQLITE_OK;
3291233828
}
3291333829
@@ -32919,33 +33835,33 @@
3291933835
#endif
3292033836
3292133837
/*
3292233838
** Acquire a reader lock.
3292333839
** Different API routines are called depending on whether or not this
32924
-** is Win95 or WinNT.
33840
+** is Win9x or WinNT.
3292533841
*/
3292633842
static int getReadLock(winFile *pFile){
3292733843
int res;
3292833844
if( isNT() ){
3292933845
OVERLAPPED ovlp;
3293033846
ovlp.Offset = SHARED_FIRST;
3293133847
ovlp.OffsetHigh = 0;
3293233848
ovlp.hEvent = 0;
32933
- res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
32934
- 0, SHARED_SIZE, 0, &ovlp);
33849
+ res = osLockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
33850
+ 0, SHARED_SIZE, 0, &ovlp);
3293533851
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
3293633852
*/
3293733853
#if SQLITE_OS_WINCE==0
3293833854
}else{
3293933855
int lk;
3294033856
sqlite3_randomness(sizeof(lk), &lk);
3294133857
pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
32942
- res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
33858
+ res = osLockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
3294333859
#endif
3294433860
}
3294533861
if( res == 0 ){
32946
- pFile->lastErrno = GetLastError();
33862
+ pFile->lastErrno = osGetLastError();
3294733863
/* No need to log a failure to lock */
3294833864
}
3294933865
return res;
3295033866
}
3295133867
@@ -32952,22 +33868,24 @@
3295233868
/*
3295333869
** Undo a readlock
3295433870
*/
3295533871
static int unlockReadLock(winFile *pFile){
3295633872
int res;
33873
+ DWORD lastErrno;
3295733874
if( isNT() ){
32958
- res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33875
+ res = osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3295933876
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
3296033877
*/
3296133878
#if SQLITE_OS_WINCE==0
3296233879
}else{
32963
- res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
33880
+ res = osUnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
3296433881
#endif
3296533882
}
32966
- if( res==0 && GetLastError()!=ERROR_NOT_LOCKED ){
32967
- pFile->lastErrno = GetLastError();
32968
- winLogError(SQLITE_IOERR_UNLOCK, "unlockReadLock", pFile->zPath);
33883
+ if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33884
+ pFile->lastErrno = lastErrno;
33885
+ winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33886
+ "unlockReadLock", pFile->zPath);
3296933887
}
3297033888
return res;
3297133889
}
3297233890
3297333891
/*
@@ -32996,15 +33914,15 @@
3299633914
** It is not possible to lower the locking level one step at a time. You
3299733915
** must go straight to locking level 0.
3299833916
*/
3299933917
static int winLock(sqlite3_file *id, int locktype){
3300033918
int rc = SQLITE_OK; /* Return code from subroutines */
33001
- int res = 1; /* Result of a windows lock call */
33919
+ int res = 1; /* Result of a Windows lock call */
3300233920
int newLocktype; /* Set pFile->locktype to this value before exiting */
3300333921
int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
3300433922
winFile *pFile = (winFile*)id;
33005
- DWORD error = NO_ERROR;
33923
+ DWORD lastErrno = NO_ERROR;
3300633924
3300733925
assert( id!=0 );
3300833926
OSTRACE(("LOCK %d %d was %d(%d)\n",
3300933927
pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
3301033928
@@ -33030,20 +33948,23 @@
3303033948
if( (pFile->locktype==NO_LOCK)
3303133949
|| ( (locktype==EXCLUSIVE_LOCK)
3303233950
&& (pFile->locktype==RESERVED_LOCK))
3303333951
){
3303433952
int cnt = 3;
33035
- while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
33036
- /* Try 3 times to get the pending lock. The pending lock might be
33037
- ** held by another reader process who will release it momentarily.
33953
+ while( cnt-->0 && (res = osLockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
33954
+ /* Try 3 times to get the pending lock. This is needed to work
33955
+ ** around problems caused by indexing and/or anti-virus software on
33956
+ ** Windows systems.
33957
+ ** If you are using this code as a model for alternative VFSes, do not
33958
+ ** copy this retry logic. It is a hack intended for Windows only.
3303833959
*/
3303933960
OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
33040
- Sleep(1);
33961
+ if( cnt ) osSleep(1);
3304133962
}
3304233963
gotPendingLock = res;
3304333964
if( !res ){
33044
- error = GetLastError();
33965
+ lastErrno = osGetLastError();
3304533966
}
3304633967
}
3304733968
3304833969
/* Acquire a shared lock
3304933970
*/
@@ -33051,23 +33972,23 @@
3305133972
assert( pFile->locktype==NO_LOCK );
3305233973
res = getReadLock(pFile);
3305333974
if( res ){
3305433975
newLocktype = SHARED_LOCK;
3305533976
}else{
33056
- error = GetLastError();
33977
+ lastErrno = osGetLastError();
3305733978
}
3305833979
}
3305933980
3306033981
/* Acquire a RESERVED lock
3306133982
*/
3306233983
if( locktype==RESERVED_LOCK && res ){
3306333984
assert( pFile->locktype==SHARED_LOCK );
33064
- res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33985
+ res = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
3306533986
if( res ){
3306633987
newLocktype = RESERVED_LOCK;
3306733988
}else{
33068
- error = GetLastError();
33989
+ lastErrno = osGetLastError();
3306933990
}
3307033991
}
3307133992
3307233993
/* Acquire a PENDING lock
3307333994
*/
@@ -33080,25 +34001,25 @@
3308034001
*/
3308134002
if( locktype==EXCLUSIVE_LOCK && res ){
3308234003
assert( pFile->locktype>=SHARED_LOCK );
3308334004
res = unlockReadLock(pFile);
3308434005
OSTRACE(("unreadlock = %d\n", res));
33085
- res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34006
+ res = osLockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3308634007
if( res ){
3308734008
newLocktype = EXCLUSIVE_LOCK;
3308834009
}else{
33089
- error = GetLastError();
33090
- OSTRACE(("error-code = %d\n", error));
34010
+ lastErrno = osGetLastError();
34011
+ OSTRACE(("error-code = %d\n", lastErrno));
3309134012
getReadLock(pFile);
3309234013
}
3309334014
}
3309434015
3309534016
/* If we are holding a PENDING lock that ought to be released, then
3309634017
** release it now.
3309734018
*/
3309834019
if( gotPendingLock && locktype==SHARED_LOCK ){
33099
- UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
34020
+ osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
3310034021
}
3310134022
3310234023
/* Update the state of the lock has held in the file descriptor then
3310334024
** return the appropriate result code.
3310434025
*/
@@ -33105,11 +34026,11 @@
3310534026
if( res ){
3310634027
rc = SQLITE_OK;
3310734028
}else{
3310834029
OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
3310934030
locktype, newLocktype));
33110
- pFile->lastErrno = error;
34031
+ pFile->lastErrno = lastErrno;
3311134032
rc = SQLITE_BUSY;
3311234033
}
3311334034
pFile->locktype = (u8)newLocktype;
3311434035
return rc;
3311534036
}
@@ -33128,13 +34049,13 @@
3312834049
assert( id!=0 );
3312934050
if( pFile->locktype>=RESERVED_LOCK ){
3313034051
rc = 1;
3313134052
OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
3313234053
}else{
33133
- rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34054
+ rc = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
3313434055
if( rc ){
33135
- UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34056
+ osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
3313634057
}
3313734058
rc = !rc;
3313834059
OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
3313934060
}
3314034061
*pResOut = rc;
@@ -33160,29 +34081,46 @@
3316034081
assert( locktype<=SHARED_LOCK );
3316134082
OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
3316234083
pFile->locktype, pFile->sharedLockByte));
3316334084
type = pFile->locktype;
3316434085
if( type>=EXCLUSIVE_LOCK ){
33165
- UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34086
+ osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3316634087
if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
3316734088
/* This should never happen. We should always be able to
3316834089
** reacquire the read lock */
33169
- rc = winLogError(SQLITE_IOERR_UNLOCK, "winUnlock", pFile->zPath);
34090
+ rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
34091
+ "winUnlock", pFile->zPath);
3317034092
}
3317134093
}
3317234094
if( type>=RESERVED_LOCK ){
33173
- UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34095
+ osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
3317434096
}
3317534097
if( locktype==NO_LOCK && type>=SHARED_LOCK ){
3317634098
unlockReadLock(pFile);
3317734099
}
3317834100
if( type>=PENDING_LOCK ){
33179
- UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
34101
+ osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
3318034102
}
3318134103
pFile->locktype = (u8)locktype;
3318234104
return rc;
3318334105
}
34106
+
34107
+/*
34108
+** If *pArg is inititially negative then this is a query. Set *pArg to
34109
+** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34110
+**
34111
+** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34112
+*/
34113
+static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
34114
+ if( *pArg<0 ){
34115
+ *pArg = (pFile->ctrlFlags & mask)!=0;
34116
+ }else if( (*pArg)==0 ){
34117
+ pFile->ctrlFlags &= ~mask;
34118
+ }else{
34119
+ pFile->ctrlFlags |= mask;
34120
+ }
34121
+}
3318434122
3318534123
/*
3318634124
** Control and query of the open file handle.
3318734125
*/
3318834126
static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33215,19 +34153,19 @@
3321534153
return rc;
3321634154
}
3321734155
return SQLITE_OK;
3321834156
}
3321934157
case SQLITE_FCNTL_PERSIST_WAL: {
33220
- int bPersist = *(int*)pArg;
33221
- if( bPersist<0 ){
33222
- *(int*)pArg = pFile->bPersistWal;
33223
- }else{
33224
- pFile->bPersistWal = bPersist!=0;
33225
- }
34158
+ winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
34159
+ return SQLITE_OK;
34160
+ }
34161
+ case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
34162
+ winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
3322634163
return SQLITE_OK;
3322734164
}
33228
- case SQLITE_FCNTL_SYNC_OMITTED: {
34165
+ case SQLITE_FCNTL_VFSNAME: {
34166
+ *(char**)pArg = sqlite3_mprintf("win32");
3322934167
return SQLITE_OK;
3323034168
}
3323134169
case SQLITE_FCNTL_WIN32_AV_RETRY: {
3323234170
int *a = (int*)pArg;
3323334171
if( a[0]>0 ){
@@ -33255,20 +34193,21 @@
3325534193
** if two files are created in the same file-system directory (i.e.
3325634194
** a database and its journal file) that the sector size will be the
3325734195
** same for both.
3325834196
*/
3325934197
static int winSectorSize(sqlite3_file *id){
33260
- assert( id!=0 );
33261
- return (int)(((winFile*)id)->sectorSize);
34198
+ (void)id;
34199
+ return SQLITE_DEFAULT_SECTOR_SIZE;
3326234200
}
3326334201
3326434202
/*
3326534203
** Return a vector of device characteristics.
3326634204
*/
3326734205
static int winDeviceCharacteristics(sqlite3_file *id){
33268
- UNUSED_PARAMETER(id);
33269
- return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
34206
+ winFile *p = (winFile*)id;
34207
+ return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
34208
+ ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
3327034209
}
3327134210
3327234211
#ifndef SQLITE_OMIT_WAL
3327334212
3327434213
/*
@@ -33411,19 +34350,19 @@
3341134350
memset(&ovlp, 0, sizeof(OVERLAPPED));
3341234351
ovlp.Offset = ofst;
3341334352
3341434353
/* Release/Acquire the system-level lock */
3341534354
if( lockType==_SHM_UNLCK ){
33416
- rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
34355
+ rc = osUnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
3341734356
}else{
33418
- rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
34357
+ rc = osLockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
3341934358
}
3342034359
3342134360
if( rc!= 0 ){
3342234361
rc = SQLITE_OK;
3342334362
}else{
33424
- pFile->lastErrno = GetLastError();
34363
+ pFile->lastErrno = osGetLastError();
3342534364
rc = SQLITE_BUSY;
3342634365
}
3342734366
3342834367
OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
3342934368
pFile->hFile.h,
@@ -33453,27 +34392,29 @@
3345334392
while( (p = *pp)!=0 ){
3345434393
if( p->nRef==0 ){
3345534394
int i;
3345634395
if( p->mutex ) sqlite3_mutex_free(p->mutex);
3345734396
for(i=0; i<p->nRegion; i++){
33458
- bRc = UnmapViewOfFile(p->aRegion[i].pMap);
34397
+ bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
3345934398
OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
33460
- (int)GetCurrentProcessId(), i,
34399
+ (int)osGetCurrentProcessId(), i,
3346134400
bRc ? "ok" : "failed"));
33462
- bRc = CloseHandle(p->aRegion[i].hMap);
34401
+ bRc = osCloseHandle(p->aRegion[i].hMap);
3346334402
OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
33464
- (int)GetCurrentProcessId(), i,
34403
+ (int)osGetCurrentProcessId(), i,
3346534404
bRc ? "ok" : "failed"));
3346634405
}
3346734406
if( p->hFile.h != INVALID_HANDLE_VALUE ){
3346834407
SimulateIOErrorBenign(1);
3346934408
winClose((sqlite3_file *)&p->hFile);
3347034409
SimulateIOErrorBenign(0);
3347134410
}
3347234411
if( deleteFlag ){
3347334412
SimulateIOErrorBenign(1);
34413
+ sqlite3BeginBenignMalloc();
3347434414
winDelete(pVfs, p->zFilename, 0);
34415
+ sqlite3EndBenignMalloc();
3347534416
SimulateIOErrorBenign(0);
3347634417
}
3347734418
*pp = p->pNext;
3347834419
sqlite3_free(p->aRegion);
3347934420
sqlite3_free(p);
@@ -33501,19 +34442,19 @@
3350134442
3350234443
/* Allocate space for the new sqlite3_shm object. Also speculatively
3350334444
** allocate space for a new winShmNode and filename.
3350434445
*/
3350534446
p = sqlite3_malloc( sizeof(*p) );
33506
- if( p==0 ) return SQLITE_NOMEM;
34447
+ if( p==0 ) return SQLITE_IOERR_NOMEM;
3350734448
memset(p, 0, sizeof(*p));
3350834449
nName = sqlite3Strlen30(pDbFd->zPath);
33509
- pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
34450
+ pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
3351034451
if( pNew==0 ){
3351134452
sqlite3_free(p);
33512
- return SQLITE_NOMEM;
34453
+ return SQLITE_IOERR_NOMEM;
3351334454
}
33514
- memset(pNew, 0, sizeof(*pNew));
34455
+ memset(pNew, 0, sizeof(*pNew) + nName + 17);
3351534456
pNew->zFilename = (char*)&pNew[1];
3351634457
sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
3351734458
sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
3351834459
3351934460
/* Look to see if there is an existing winShmNode that can be used.
@@ -33535,31 +34476,31 @@
3353534476
pShmNode->pNext = winShmNodeList;
3353634477
winShmNodeList = pShmNode;
3353734478
3353834479
pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
3353934480
if( pShmNode->mutex==0 ){
33540
- rc = SQLITE_NOMEM;
34481
+ rc = SQLITE_IOERR_NOMEM;
3354134482
goto shm_open_err;
3354234483
}
3354334484
3354434485
rc = winOpen(pDbFd->pVfs,
3354534486
pShmNode->zFilename, /* Name of the file (UTF-8) */
3354634487
(sqlite3_file*)&pShmNode->hFile, /* File handle here */
3354734488
SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
3354834489
0);
3354934490
if( SQLITE_OK!=rc ){
33550
- rc = SQLITE_CANTOPEN_BKPT;
3355134491
goto shm_open_err;
3355234492
}
3355334493
3355434494
/* Check to see if another process is holding the dead-man switch.
3355534495
** If not, truncate the file to zero length.
3355634496
*/
3355734497
if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
3355834498
rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
3355934499
if( rc!=SQLITE_OK ){
33560
- rc = winLogError(SQLITE_IOERR_SHMOPEN, "winOpenShm", pDbFd->zPath);
34500
+ rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
34501
+ "winOpenShm", pDbFd->zPath);
3356134502
}
3356234503
}
3356334504
if( rc==SQLITE_OK ){
3356434505
winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
3356534506
rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -33740,11 +34681,11 @@
3374034681
}
3374134682
}
3374234683
}
3374334684
sqlite3_mutex_leave(pShmNode->mutex);
3374434685
OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
33745
- p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
34686
+ p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
3374634687
rc ? "failed" : "ok"));
3374734688
return rc;
3374834689
}
3374934690
3375034691
/*
@@ -33814,11 +34755,12 @@
3381434755
** Check to see if it has been allocated (i.e. if the wal-index file is
3381534756
** large enough to contain the requested region).
3381634757
*/
3381734758
rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
3381834759
if( rc!=SQLITE_OK ){
33819
- rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap1", pDbFd->zPath);
34760
+ rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34761
+ "winShmMap1", pDbFd->zPath);
3382034762
goto shmpage_out;
3382134763
}
3382234764
3382334765
if( sz<nByte ){
3382434766
/* The requested memory region does not exist. If isWrite is set to
@@ -33828,11 +34770,12 @@
3382834770
** the requested memory region.
3382934771
*/
3383034772
if( !isWrite ) goto shmpage_out;
3383134773
rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
3383234774
if( rc!=SQLITE_OK ){
33833
- rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap2", pDbFd->zPath);
34775
+ rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34776
+ "winShmMap2", pDbFd->zPath);
3383434777
goto shmpage_out;
3383534778
}
3383634779
}
3383734780
3383834781
/* Map the requested memory region into this processes address space. */
@@ -33847,30 +34790,31 @@
3384734790
3384834791
while( pShmNode->nRegion<=iRegion ){
3384934792
HANDLE hMap; /* file-mapping handle */
3385034793
void *pMap = 0; /* Mapped memory region */
3385134794
33852
- hMap = CreateFileMapping(pShmNode->hFile.h,
34795
+ hMap = osCreateFileMapping(pShmNode->hFile.h,
3385334796
NULL, PAGE_READWRITE, 0, nByte, NULL
3385434797
);
3385534798
OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
33856
- (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
34799
+ (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
3385734800
hMap ? "ok" : "failed"));
3385834801
if( hMap ){
3385934802
int iOffset = pShmNode->nRegion*szRegion;
3386034803
int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33861
- pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
34804
+ pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
3386234805
0, iOffset - iOffsetShift, szRegion + iOffsetShift
3386334806
);
3386434807
OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
33865
- (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
33866
- pMap ? "ok" : "failed"));
34808
+ (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
34809
+ szRegion, pMap ? "ok" : "failed"));
3386734810
}
3386834811
if( !pMap ){
33869
- pShmNode->lastErrno = GetLastError();
33870
- rc = winLogError(SQLITE_IOERR_SHMMAP, "winShmMap3", pDbFd->zPath);
33871
- if( hMap ) CloseHandle(hMap);
34812
+ pShmNode->lastErrno = osGetLastError();
34813
+ rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
34814
+ "winShmMap3", pDbFd->zPath);
34815
+ if( hMap ) osCloseHandle(hMap);
3387234816
goto shmpage_out;
3387334817
}
3387434818
3387534819
pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
3387634820
pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
@@ -33964,11 +34908,11 @@
3396434908
static char zChars[] =
3396534909
"abcdefghijklmnopqrstuvwxyz"
3396634910
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3396734911
"0123456789";
3396834912
size_t i, j;
33969
- char zTempPath[MAX_PATH+1];
34913
+ char zTempPath[MAX_PATH+2];
3397034914
3397134915
/* It's odd to simulate an io-error here, but really this is just
3397234916
** using the io-error infrastructure to test that SQLite handles this
3397334917
** function failing.
3397434918
*/
@@ -33977,55 +34921,56 @@
3397734921
if( sqlite3_temp_directory ){
3397834922
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
3397934923
}else if( isNT() ){
3398034924
char *zMulti;
3398134925
WCHAR zWidePath[MAX_PATH];
33982
- GetTempPathW(MAX_PATH-30, zWidePath);
34926
+ osGetTempPathW(MAX_PATH-30, zWidePath);
3398334927
zMulti = unicodeToUtf8(zWidePath);
3398434928
if( zMulti ){
3398534929
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
33986
- free(zMulti);
34930
+ sqlite3_free(zMulti);
3398734931
}else{
33988
- return SQLITE_NOMEM;
34932
+ return SQLITE_IOERR_NOMEM;
3398934933
}
3399034934
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33991
-** Since the ASCII version of these Windows API do not exist for WINCE,
34935
+** Since the ANSI version of these Windows API do not exist for WINCE,
3399234936
** it's important to not reference them for WINCE builds.
3399334937
*/
3399434938
#if SQLITE_OS_WINCE==0
3399534939
}else{
3399634940
char *zUtf8;
3399734941
char zMbcsPath[MAX_PATH];
33998
- GetTempPathA(MAX_PATH-30, zMbcsPath);
34942
+ osGetTempPathA(MAX_PATH-30, zMbcsPath);
3399934943
zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
3400034944
if( zUtf8 ){
3400134945
sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
34002
- free(zUtf8);
34946
+ sqlite3_free(zUtf8);
3400334947
}else{
34004
- return SQLITE_NOMEM;
34948
+ return SQLITE_IOERR_NOMEM;
3400534949
}
3400634950
#endif
3400734951
}
3400834952
3400934953
/* Check that the output buffer is large enough for the temporary file
3401034954
** name. If it is not, return SQLITE_ERROR.
3401134955
*/
34012
- if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
34956
+ if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
3401334957
return SQLITE_ERROR;
3401434958
}
3401534959
3401634960
for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
3401734961
zTempPath[i] = 0;
3401834962
34019
- sqlite3_snprintf(nBuf-17, zBuf,
34963
+ sqlite3_snprintf(nBuf-18, zBuf,
3402034964
"%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
3402134965
j = sqlite3Strlen30(zBuf);
3402234966
sqlite3_randomness(15, &zBuf[j]);
3402334967
for(i=0; i<15; i++, j++){
3402434968
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3402534969
}
3402634970
zBuf[j] = 0;
34971
+ zBuf[j+1] = 0;
3402734972
3402834973
OSTRACE(("TEMP FILENAME: %s\n", zBuf));
3402934974
return SQLITE_OK;
3403034975
}
3403134976
@@ -34038,10 +34983,11 @@
3403834983
sqlite3_file *id, /* Write the SQLite file handle here */
3403934984
int flags, /* Open mode flags */
3404034985
int *pOutFlags /* Status return flags */
3404134986
){
3404234987
HANDLE h;
34988
+ DWORD lastErrno;
3404334989
DWORD dwDesiredAccess;
3404434990
DWORD dwShareMode;
3404534991
DWORD dwCreationDisposition;
3404634992
DWORD dwFlagsAndAttributes = 0;
3404734993
#if SQLITE_OS_WINCE
@@ -34053,11 +34999,11 @@
3405334999
int cnt = 0;
3405435000
3405535001
/* If argument zPath is a NULL pointer, this function is required to open
3405635002
** a temporary file. Use this buffer to store the file name in.
3405735003
*/
34058
- char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
35004
+ char zTmpname[MAX_PATH+2]; /* Buffer used to create temp filename */
3405935005
3406035006
int rc = SQLITE_OK; /* Function Return Code */
3406135007
#if !defined(NDEBUG) || SQLITE_OS_WINCE
3406235008
int eType = flags&0xFFFFFF00; /* Type of file to open */
3406335009
#endif
@@ -34112,21 +35058,28 @@
3411235058
/* If the second argument to this function is NULL, generate a
3411335059
** temporary file name to use
3411435060
*/
3411535061
if( !zUtf8Name ){
3411635062
assert(isDelete && !isOpenJournal);
34117
- rc = getTempname(MAX_PATH+1, zTmpname);
35063
+ rc = getTempname(MAX_PATH+2, zTmpname);
3411835064
if( rc!=SQLITE_OK ){
3411935065
return rc;
3412035066
}
3412135067
zUtf8Name = zTmpname;
3412235068
}
35069
+
35070
+ /* Database filenames are double-zero terminated if they are not
35071
+ ** URIs with parameters. Hence, they can always be passed into
35072
+ ** sqlite3_uri_parameter().
35073
+ */
35074
+ assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
35075
+ zUtf8Name[strlen(zUtf8Name)+1]==0 );
3412335076
3412435077
/* Convert the filename to the system encoding. */
3412535078
zConverted = convertUtf8Filename(zUtf8Name);
3412635079
if( zConverted==0 ){
34127
- return SQLITE_NOMEM;
35080
+ return SQLITE_IOERR_NOMEM;
3412835081
}
3412935082
3413035083
if( isReadWrite ){
3413135084
dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
3413235085
}else{
@@ -34168,30 +35121,30 @@
3416835121
#if SQLITE_OS_WINCE
3416935122
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
3417035123
#endif
3417135124
3417235125
if( isNT() ){
34173
- while( (h = CreateFileW((WCHAR*)zConverted,
34174
- dwDesiredAccess,
34175
- dwShareMode, NULL,
34176
- dwCreationDisposition,
34177
- dwFlagsAndAttributes,
34178
- NULL))==INVALID_HANDLE_VALUE &&
34179
- retryIoerr(&cnt) ){}
35126
+ while( (h = osCreateFileW((LPCWSTR)zConverted,
35127
+ dwDesiredAccess,
35128
+ dwShareMode, NULL,
35129
+ dwCreationDisposition,
35130
+ dwFlagsAndAttributes,
35131
+ NULL))==INVALID_HANDLE_VALUE &&
35132
+ retryIoerr(&cnt, &lastErrno) ){}
3418035133
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34181
-** Since the ASCII version of these Windows API do not exist for WINCE,
35134
+** Since the ANSI version of these Windows API do not exist for WINCE,
3418235135
** it's important to not reference them for WINCE builds.
3418335136
*/
3418435137
#if SQLITE_OS_WINCE==0
3418535138
}else{
34186
- while( (h = CreateFileA((char*)zConverted,
34187
- dwDesiredAccess,
34188
- dwShareMode, NULL,
34189
- dwCreationDisposition,
34190
- dwFlagsAndAttributes,
34191
- NULL))==INVALID_HANDLE_VALUE &&
34192
- retryIoerr(&cnt) ){}
35139
+ while( (h = osCreateFileA((LPCSTR)zConverted,
35140
+ dwDesiredAccess,
35141
+ dwShareMode, NULL,
35142
+ dwCreationDisposition,
35143
+ dwFlagsAndAttributes,
35144
+ NULL))==INVALID_HANDLE_VALUE &&
35145
+ retryIoerr(&cnt, &lastErrno) ){}
3419335146
#endif
3419435147
}
3419535148
3419635149
logIoerr(cnt);
3419735150
@@ -34198,13 +35151,13 @@
3419835151
OSTRACE(("OPEN %d %s 0x%lx %s\n",
3419935152
h, zName, dwDesiredAccess,
3420035153
h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
3420135154
3420235155
if( h==INVALID_HANDLE_VALUE ){
34203
- pFile->lastErrno = GetLastError();
34204
- winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
34205
- free(zConverted);
35156
+ pFile->lastErrno = lastErrno;
35157
+ winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
35158
+ sqlite3_free(zConverted);
3420635159
if( isReadWrite && !isExclusive ){
3420735160
return winOpen(pVfs, zName, id,
3420835161
((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
3420935162
}else{
3421035163
return SQLITE_CANTOPEN_BKPT;
@@ -34224,36 +35177,38 @@
3422435177
pFile->h = h;
3422535178
pFile->lastErrno = NO_ERROR;
3422635179
pFile->pVfs = pVfs;
3422735180
pFile->pShm = 0;
3422835181
pFile->zPath = zName;
34229
- pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
35182
+ if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
35183
+ pFile->ctrlFlags |= WINFILE_PSOW;
35184
+ }
3423035185
3423135186
#if SQLITE_OS_WINCE
3423235187
if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
3423335188
&& !winceCreateLock(zName, pFile)
3423435189
){
34235
- CloseHandle(h);
34236
- free(zConverted);
35190
+ osCloseHandle(h);
35191
+ sqlite3_free(zConverted);
3423735192
return SQLITE_CANTOPEN_BKPT;
3423835193
}
3423935194
if( isTemp ){
3424035195
pFile->zDeleteOnClose = zConverted;
3424135196
}else
3424235197
#endif
3424335198
{
34244
- free(zConverted);
35199
+ sqlite3_free(zConverted);
3424535200
}
3424635201
3424735202
OpenCounter(+1);
3424835203
return rc;
3424935204
}
3425035205
3425135206
/*
3425235207
** Delete the named file.
3425335208
**
34254
-** Note that windows does not allow a file to be deleted if some other
35209
+** Note that Windows does not allow a file to be deleted if some other
3425535210
** process has it open. Sometimes a virus scanner or indexing program
3425635211
** will open a journal file shortly after it is created in order to do
3425735212
** whatever it does. While this other process is holding the
3425835213
** file open, we will be unable to delete it. To work around this
3425935214
** problem, we delay 100 milliseconds and try to delete again. Up
@@ -34265,42 +35220,44 @@
3426535220
const char *zFilename, /* Name of file to delete */
3426635221
int syncDir /* Not used on win32 */
3426735222
){
3426835223
int cnt = 0;
3426935224
int rc;
35225
+ DWORD lastErrno;
3427035226
void *zConverted;
3427135227
UNUSED_PARAMETER(pVfs);
3427235228
UNUSED_PARAMETER(syncDir);
3427335229
3427435230
SimulateIOError(return SQLITE_IOERR_DELETE);
3427535231
zConverted = convertUtf8Filename(zFilename);
3427635232
if( zConverted==0 ){
34277
- return SQLITE_NOMEM;
35233
+ return SQLITE_IOERR_NOMEM;
3427835234
}
3427935235
if( isNT() ){
3428035236
rc = 1;
34281
- while( GetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34282
- (rc = DeleteFileW(zConverted))==0 && retryIoerr(&cnt) ){}
35237
+ while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
35238
+ (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
3428335239
rc = rc ? SQLITE_OK : SQLITE_ERROR;
3428435240
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34285
-** Since the ASCII version of these Windows API do not exist for WINCE,
35241
+** Since the ANSI version of these Windows API do not exist for WINCE,
3428635242
** it's important to not reference them for WINCE builds.
3428735243
*/
3428835244
#if SQLITE_OS_WINCE==0
3428935245
}else{
3429035246
rc = 1;
34291
- while( GetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34292
- (rc = DeleteFileA(zConverted))==0 && retryIoerr(&cnt) ){}
35247
+ while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
35248
+ (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
3429335249
rc = rc ? SQLITE_OK : SQLITE_ERROR;
3429435250
#endif
3429535251
}
3429635252
if( rc ){
34297
- rc = winLogError(SQLITE_IOERR_DELETE, "winDelete", zFilename);
35253
+ rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35254
+ "winDelete", zFilename);
3429835255
}else{
3429935256
logIoerr(cnt);
3430035257
}
34301
- free(zConverted);
35258
+ sqlite3_free(zConverted);
3430235259
OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
3430335260
return rc;
3430435261
}
3430535262
3430635263
/*
@@ -34312,25 +35269,26 @@
3431235269
int flags, /* Type of test to make on this file */
3431335270
int *pResOut /* OUT: Result */
3431435271
){
3431535272
DWORD attr;
3431635273
int rc = 0;
35274
+ DWORD lastErrno;
3431735275
void *zConverted;
3431835276
UNUSED_PARAMETER(pVfs);
3431935277
3432035278
SimulateIOError( return SQLITE_IOERR_ACCESS; );
3432135279
zConverted = convertUtf8Filename(zFilename);
3432235280
if( zConverted==0 ){
34323
- return SQLITE_NOMEM;
35281
+ return SQLITE_IOERR_NOMEM;
3432435282
}
3432535283
if( isNT() ){
3432635284
int cnt = 0;
3432735285
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3432835286
memset(&sAttrData, 0, sizeof(sAttrData));
34329
- while( !(rc = GetFileAttributesExW((WCHAR*)zConverted,
35287
+ while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
3433035288
GetFileExInfoStandard,
34331
- &sAttrData)) && retryIoerr(&cnt) ){}
35289
+ &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
3433235290
if( rc ){
3433335291
/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
3433435292
** as if it does not exist.
3433535293
*/
3433635294
if( flags==SQLITE_ACCESS_EXISTS
@@ -34340,28 +35298,28 @@
3434035298
}else{
3434135299
attr = sAttrData.dwFileAttributes;
3434235300
}
3434335301
}else{
3434435302
logIoerr(cnt);
34345
- if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
34346
- winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
34347
- free(zConverted);
35303
+ if( lastErrno!=ERROR_FILE_NOT_FOUND ){
35304
+ winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
35305
+ sqlite3_free(zConverted);
3434835306
return SQLITE_IOERR_ACCESS;
3434935307
}else{
3435035308
attr = INVALID_FILE_ATTRIBUTES;
3435135309
}
3435235310
}
3435335311
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34354
-** Since the ASCII version of these Windows API do not exist for WINCE,
35312
+** Since the ANSI version of these Windows API do not exist for WINCE,
3435535313
** it's important to not reference them for WINCE builds.
3435635314
*/
3435735315
#if SQLITE_OS_WINCE==0
3435835316
}else{
34359
- attr = GetFileAttributesA((char*)zConverted);
35317
+ attr = osGetFileAttributesA((char*)zConverted);
3436035318
#endif
3436135319
}
34362
- free(zConverted);
35320
+ sqlite3_free(zConverted);
3436335321
switch( flags ){
3436435322
case SQLITE_ACCESS_READ:
3436535323
case SQLITE_ACCESS_EXISTS:
3436635324
rc = attr!=INVALID_FILE_ATTRIBUTES;
3436735325
break;
@@ -34422,119 +35380,52 @@
3442235380
** current working directory has been unlinked.
3442335381
*/
3442435382
SimulateIOError( return SQLITE_ERROR );
3442535383
UNUSED_PARAMETER(nFull);
3442635384
zConverted = convertUtf8Filename(zRelative);
35385
+ if( zConverted==0 ){
35386
+ return SQLITE_IOERR_NOMEM;
35387
+ }
3442735388
if( isNT() ){
34428
- WCHAR *zTemp;
34429
- nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
34430
- zTemp = malloc( nByte*sizeof(zTemp[0]) );
35389
+ LPWSTR zTemp;
35390
+ nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
35391
+ zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
3443135392
if( zTemp==0 ){
34432
- free(zConverted);
34433
- return SQLITE_NOMEM;
35393
+ sqlite3_free(zConverted);
35394
+ return SQLITE_IOERR_NOMEM;
3443435395
}
34435
- GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
34436
- free(zConverted);
35396
+ osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
35397
+ sqlite3_free(zConverted);
3443735398
zOut = unicodeToUtf8(zTemp);
34438
- free(zTemp);
35399
+ sqlite3_free(zTemp);
3443935400
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34440
-** Since the ASCII version of these Windows API do not exist for WINCE,
35401
+** Since the ANSI version of these Windows API do not exist for WINCE,
3444135402
** it's important to not reference them for WINCE builds.
3444235403
*/
3444335404
#if SQLITE_OS_WINCE==0
3444435405
}else{
3444535406
char *zTemp;
34446
- nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
34447
- zTemp = malloc( nByte*sizeof(zTemp[0]) );
35407
+ nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
35408
+ zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
3444835409
if( zTemp==0 ){
34449
- free(zConverted);
34450
- return SQLITE_NOMEM;
35410
+ sqlite3_free(zConverted);
35411
+ return SQLITE_IOERR_NOMEM;
3445135412
}
34452
- GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
34453
- free(zConverted);
35413
+ osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
35414
+ sqlite3_free(zConverted);
3445435415
zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
34455
- free(zTemp);
35416
+ sqlite3_free(zTemp);
3445635417
#endif
3445735418
}
3445835419
if( zOut ){
3445935420
sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
34460
- free(zOut);
35421
+ sqlite3_free(zOut);
3446135422
return SQLITE_OK;
3446235423
}else{
34463
- return SQLITE_NOMEM;
34464
- }
34465
-#endif
34466
-}
34467
-
34468
-/*
34469
-** Get the sector size of the device used to store
34470
-** file.
34471
-*/
34472
-static int getSectorSize(
34473
- sqlite3_vfs *pVfs,
34474
- const char *zRelative /* UTF-8 file name */
34475
-){
34476
- DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34477
- /* GetDiskFreeSpace is not supported under WINCE */
34478
-#if SQLITE_OS_WINCE
34479
- UNUSED_PARAMETER(pVfs);
34480
- UNUSED_PARAMETER(zRelative);
34481
-#else
34482
- char zFullpath[MAX_PATH+1];
34483
- int rc;
34484
- DWORD dwRet = 0;
34485
- DWORD dwDummy;
34486
-
34487
- /*
34488
- ** We need to get the full path name of the file
34489
- ** to get the drive letter to look up the sector
34490
- ** size.
34491
- */
34492
- SimulateIOErrorBenign(1);
34493
- rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
34494
- SimulateIOErrorBenign(0);
34495
- if( rc == SQLITE_OK )
34496
- {
34497
- void *zConverted = convertUtf8Filename(zFullpath);
34498
- if( zConverted ){
34499
- if( isNT() ){
34500
- /* trim path to just drive reference */
34501
- WCHAR *p = zConverted;
34502
- for(;*p;p++){
34503
- if( *p == '\\' ){
34504
- *p = '\0';
34505
- break;
34506
- }
34507
- }
34508
- dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
34509
- &dwDummy,
34510
- &bytesPerSector,
34511
- &dwDummy,
34512
- &dwDummy);
34513
- }else{
34514
- /* trim path to just drive reference */
34515
- char *p = (char *)zConverted;
34516
- for(;*p;p++){
34517
- if( *p == '\\' ){
34518
- *p = '\0';
34519
- break;
34520
- }
34521
- }
34522
- dwRet = GetDiskFreeSpaceA((char*)zConverted,
34523
- &dwDummy,
34524
- &bytesPerSector,
34525
- &dwDummy,
34526
- &dwDummy);
34527
- }
34528
- free(zConverted);
34529
- }
34530
- if( !dwRet ){
34531
- bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34532
- }
34533
- }
34534
-#endif
34535
- return (int) bytesPerSector;
35424
+ return SQLITE_IOERR_NOMEM;
35425
+ }
35426
+#endif
3453635427
}
3453735428
3453835429
#ifndef SQLITE_OMIT_LOAD_EXTENSION
3453935430
/*
3454035431
** Interfaces for opening a shared library, finding entry points
@@ -34550,41 +35441,34 @@
3455035441
UNUSED_PARAMETER(pVfs);
3455135442
if( zConverted==0 ){
3455235443
return 0;
3455335444
}
3455435445
if( isNT() ){
34555
- h = LoadLibraryW((WCHAR*)zConverted);
35446
+ h = osLoadLibraryW((LPCWSTR)zConverted);
3455635447
/* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34557
-** Since the ASCII version of these Windows API do not exist for WINCE,
35448
+** Since the ANSI version of these Windows API do not exist for WINCE,
3455835449
** it's important to not reference them for WINCE builds.
3455935450
*/
3456035451
#if SQLITE_OS_WINCE==0
3456135452
}else{
34562
- h = LoadLibraryA((char*)zConverted);
35453
+ h = osLoadLibraryA((char*)zConverted);
3456335454
#endif
3456435455
}
34565
- free(zConverted);
35456
+ sqlite3_free(zConverted);
3456635457
return (void*)h;
3456735458
}
3456835459
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
3456935460
UNUSED_PARAMETER(pVfs);
34570
- getLastErrorMsg(nBuf, zBufOut);
35461
+ getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
3457135462
}
3457235463
static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
3457335464
UNUSED_PARAMETER(pVfs);
34574
-#if SQLITE_OS_WINCE
34575
- /* The GetProcAddressA() routine is only available on wince. */
34576
- return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34577
-#else
34578
- /* All other windows platforms expect GetProcAddress() to take
34579
- ** an Ansi string regardless of the _UNICODE setting */
34580
- return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34581
-#endif
35465
+ return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
3458235466
}
3458335467
static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
3458435468
UNUSED_PARAMETER(pVfs);
34585
- FreeLibrary((HANDLE)pHandle);
35469
+ osFreeLibrary((HANDLE)pHandle);
3458635470
}
3458735471
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3458835472
#define winDlOpen 0
3458935473
#define winDlError 0
3459035474
#define winDlSym 0
@@ -34602,27 +35486,27 @@
3460235486
n = nBuf;
3460335487
memset(zBuf, 0, nBuf);
3460435488
#else
3460535489
if( sizeof(SYSTEMTIME)<=nBuf-n ){
3460635490
SYSTEMTIME x;
34607
- GetSystemTime(&x);
35491
+ osGetSystemTime(&x);
3460835492
memcpy(&zBuf[n], &x, sizeof(x));
3460935493
n += sizeof(x);
3461035494
}
3461135495
if( sizeof(DWORD)<=nBuf-n ){
34612
- DWORD pid = GetCurrentProcessId();
35496
+ DWORD pid = osGetCurrentProcessId();
3461335497
memcpy(&zBuf[n], &pid, sizeof(pid));
3461435498
n += sizeof(pid);
3461535499
}
3461635500
if( sizeof(DWORD)<=nBuf-n ){
34617
- DWORD cnt = GetTickCount();
35501
+ DWORD cnt = osGetTickCount();
3461835502
memcpy(&zBuf[n], &cnt, sizeof(cnt));
3461935503
n += sizeof(cnt);
3462035504
}
3462135505
if( sizeof(LARGE_INTEGER)<=nBuf-n ){
3462235506
LARGE_INTEGER i;
34623
- QueryPerformanceCounter(&i);
35507
+ osQueryPerformanceCounter(&i);
3462435508
memcpy(&zBuf[n], &i, sizeof(i));
3462535509
n += sizeof(i);
3462635510
}
3462735511
#endif
3462835512
return n;
@@ -34631,11 +35515,11 @@
3463135515
3463235516
/*
3463335517
** Sleep for a little while. Return the amount of time slept.
3463435518
*/
3463535519
static int winSleep(sqlite3_vfs *pVfs, int microsec){
34636
- Sleep((microsec+999)/1000);
35520
+ osSleep((microsec+999)/1000);
3463735521
UNUSED_PARAMETER(pVfs);
3463835522
return ((microsec+999)/1000)*1000;
3463935523
}
3464035524
3464135525
/*
@@ -34670,17 +35554,17 @@
3467035554
static const sqlite3_int64 max32BitValue =
3467135555
(sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
3467235556
3467335557
#if SQLITE_OS_WINCE
3467435558
SYSTEMTIME time;
34675
- GetSystemTime(&time);
35559
+ osGetSystemTime(&time);
3467635560
/* if SystemTimeToFileTime() fails, it returns zero. */
34677
- if (!SystemTimeToFileTime(&time,&ft)){
35561
+ if (!osSystemTimeToFileTime(&time,&ft)){
3467835562
return SQLITE_ERROR;
3467935563
}
3468035564
#else
34681
- GetSystemTimeAsFileTime( &ft );
35565
+ osGetSystemTimeAsFileTime( &ft );
3468235566
#endif
3468335567
3468435568
*piNow = winFiletimeEpoch +
3468535569
((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
3468635570
(sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
@@ -34709,12 +35593,12 @@
3470935593
return rc;
3471035594
}
3471135595
3471235596
/*
3471335597
** The idea is that this function works like a combination of
34714
-** GetLastError() and FormatMessage() on windows (or errno and
34715
-** strerror_r() on unix). After an error is returned by an OS
35598
+** GetLastError() and FormatMessage() on Windows (or errno and
35599
+** strerror_r() on Unix). After an error is returned by an OS
3471635600
** function, SQLite calls this function with zBuf pointing to
3471735601
** a buffer of nBuf bytes. The OS layer should populate the
3471835602
** buffer with a nul-terminated UTF-8 encoded error message
3471935603
** describing the last IO error to have occurred within the calling
3472035604
** thread.
@@ -34739,14 +35623,12 @@
3473935623
** by sqlite into the error message available to the user using
3474035624
** sqlite3_errmsg(), possibly making IO errors easier to debug.
3474135625
*/
3474235626
static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
3474335627
UNUSED_PARAMETER(pVfs);
34744
- return getLastErrorMsg(nBuf, zBuf);
35628
+ return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
3474535629
}
34746
-
34747
-
3474835630
3474935631
/*
3475035632
** Initialize and deinitialize the operating system interface.
3475135633
*/
3475235634
SQLITE_API int sqlite3_os_init(void){
@@ -34768,25 +35650,30 @@
3476835650
winRandomness, /* xRandomness */
3476935651
winSleep, /* xSleep */
3477035652
winCurrentTime, /* xCurrentTime */
3477135653
winGetLastError, /* xGetLastError */
3477235654
winCurrentTimeInt64, /* xCurrentTimeInt64 */
34773
- 0, /* xSetSystemCall */
34774
- 0, /* xGetSystemCall */
34775
- 0, /* xNextSystemCall */
35655
+ winSetSystemCall, /* xSetSystemCall */
35656
+ winGetSystemCall, /* xGetSystemCall */
35657
+ winNextSystemCall, /* xNextSystemCall */
3477635658
};
35659
+
35660
+ /* Double-check that the aSyscall[] array has been constructed
35661
+ ** correctly. See ticket [bb3a86e890c8e96ab] */
35662
+ assert( ArraySize(aSyscall)==60 );
3477735663
3477835664
#ifndef SQLITE_OMIT_WAL
3477935665
/* get memory map allocation granularity */
3478035666
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
34781
- GetSystemInfo(&winSysInfo);
35667
+ osGetSystemInfo(&winSysInfo);
3478235668
assert(winSysInfo.dwAllocationGranularity > 0);
3478335669
#endif
3478435670
3478535671
sqlite3_vfs_register(&winVfs, 1);
3478635672
return SQLITE_OK;
3478735673
}
35674
+
3478835675
SQLITE_API int sqlite3_os_end(void){
3478935676
return SQLITE_OK;
3479035677
}
3479135678
3479235679
#endif /* SQLITE_OS_WIN */
@@ -35222,11 +36109,11 @@
3522236109
*/
3522336110
struct PCache {
3522436111
PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
3522536112
PgHdr *pSynced; /* Last synced page in dirty page list */
3522636113
int nRef; /* Number of referenced pages */
35227
- int nMax; /* Configured cache size */
36114
+ int szCache; /* Configured cache size */
3522836115
int szPage; /* Size of every page in this cache */
3522936116
int szExtra; /* Size of extra space for each page */
3523036117
int bPurgeable; /* True if pages are on backing store */
3523136118
int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
3523236119
void *pStress; /* Argument to xStress */
@@ -35333,32 +36220,32 @@
3533336220
PCache *pCache = p->pCache;
3533436221
if( pCache->bPurgeable ){
3533536222
if( p->pgno==1 ){
3533636223
pCache->pPage1 = 0;
3533736224
}
35338
- sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
36225
+ sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
3533936226
}
3534036227
}
3534136228
3534236229
/*************************************************** General Interfaces ******
3534336230
**
3534436231
** Initialize and shutdown the page cache subsystem. Neither of these
3534536232
** functions are threadsafe.
3534636233
*/
3534736234
SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
35348
- if( sqlite3GlobalConfig.pcache.xInit==0 ){
36235
+ if( sqlite3GlobalConfig.pcache2.xInit==0 ){
3534936236
/* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
3535036237
** built-in default page cache is used instead of the application defined
3535136238
** page cache. */
3535236239
sqlite3PCacheSetDefault();
3535336240
}
35354
- return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
36241
+ return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
3535536242
}
3535636243
SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
35357
- if( sqlite3GlobalConfig.pcache.xShutdown ){
36244
+ if( sqlite3GlobalConfig.pcache2.xShutdown ){
3535836245
/* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
35359
- sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
36246
+ sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
3536036247
}
3536136248
}
3536236249
3536336250
/*
3536436251
** Return the size in bytes of a PCache object.
@@ -35383,26 +36270,37 @@
3538336270
p->szPage = szPage;
3538436271
p->szExtra = szExtra;
3538536272
p->bPurgeable = bPurgeable;
3538636273
p->xStress = xStress;
3538736274
p->pStress = pStress;
35388
- p->nMax = 100;
36275
+ p->szCache = 100;
3538936276
}
3539036277
3539136278
/*
3539236279
** Change the page size for PCache object. The caller must ensure that there
3539336280
** are no outstanding page references when this function is called.
3539436281
*/
3539536282
SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
3539636283
assert( pCache->nRef==0 && pCache->pDirty==0 );
3539736284
if( pCache->pCache ){
35398
- sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
36285
+ sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
3539936286
pCache->pCache = 0;
3540036287
pCache->pPage1 = 0;
3540136288
}
3540236289
pCache->szPage = szPage;
3540336290
}
36291
+
36292
+/*
36293
+** Compute the number of pages of cache requested.
36294
+*/
36295
+static int numberOfCachePages(PCache *p){
36296
+ if( p->szCache>=0 ){
36297
+ return p->szCache;
36298
+ }else{
36299
+ return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
36300
+ }
36301
+}
3540436302
3540536303
/*
3540636304
** Try to obtain a page from the cache.
3540736305
*/
3540836306
SQLITE_PRIVATE int sqlite3PcacheFetch(
@@ -35409,11 +36307,12 @@
3540936307
PCache *pCache, /* Obtain the page from this cache */
3541036308
Pgno pgno, /* Page number to obtain */
3541136309
int createFlag, /* If true, create page if it does not exist already */
3541236310
PgHdr **ppPage /* Write the page here */
3541336311
){
35414
- PgHdr *pPage = 0;
36312
+ sqlite3_pcache_page *pPage = 0;
36313
+ PgHdr *pPgHdr = 0;
3541536314
int eCreate;
3541636315
3541736316
assert( pCache!=0 );
3541836317
assert( createFlag==1 || createFlag==0 );
3541936318
assert( pgno>0 );
@@ -35421,23 +36320,23 @@
3542136320
/* If the pluggable cache (sqlite3_pcache*) has not been allocated,
3542236321
** allocate it now.
3542336322
*/
3542436323
if( !pCache->pCache && createFlag ){
3542536324
sqlite3_pcache *p;
35426
- int nByte;
35427
- nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
35428
- p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
36325
+ p = sqlite3GlobalConfig.pcache2.xCreate(
36326
+ pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
36327
+ );
3542936328
if( !p ){
3543036329
return SQLITE_NOMEM;
3543136330
}
35432
- sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
36331
+ sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
3543336332
pCache->pCache = p;
3543436333
}
3543536334
3543636335
eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
3543736336
if( pCache->pCache ){
35438
- pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
36337
+ pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
3543936338
}
3544036339
3544136340
if( !pPage && eCreate==1 ){
3544236341
PgHdr *pPg;
3544336342
@@ -35460,45 +36359,48 @@
3546036359
#ifdef SQLITE_LOG_CACHE_SPILL
3546136360
sqlite3_log(SQLITE_FULL,
3546236361
"spill page %d making room for %d - cache used: %d/%d",
3546336362
pPg->pgno, pgno,
3546436363
sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
35465
- pCache->nMax);
36364
+ numberOfCachePages(pCache));
3546636365
#endif
3546736366
rc = pCache->xStress(pCache->pStress, pPg);
3546836367
if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
3546936368
return rc;
3547036369
}
3547136370
}
3547236371
35473
- pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
36372
+ pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
3547436373
}
3547536374
3547636375
if( pPage ){
35477
- if( !pPage->pData ){
35478
- memset(pPage, 0, sizeof(PgHdr));
35479
- pPage->pData = (void *)&pPage[1];
35480
- pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
35481
- memset(pPage->pExtra, 0, pCache->szExtra);
35482
- pPage->pCache = pCache;
35483
- pPage->pgno = pgno;
35484
- }
35485
- assert( pPage->pCache==pCache );
35486
- assert( pPage->pgno==pgno );
35487
- assert( pPage->pData==(void *)&pPage[1] );
35488
- assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
35489
-
35490
- if( 0==pPage->nRef ){
36376
+ pPgHdr = (PgHdr *)pPage->pExtra;
36377
+
36378
+ if( !pPgHdr->pPage ){
36379
+ memset(pPgHdr, 0, sizeof(PgHdr));
36380
+ pPgHdr->pPage = pPage;
36381
+ pPgHdr->pData = pPage->pBuf;
36382
+ pPgHdr->pExtra = (void *)&pPgHdr[1];
36383
+ memset(pPgHdr->pExtra, 0, pCache->szExtra);
36384
+ pPgHdr->pCache = pCache;
36385
+ pPgHdr->pgno = pgno;
36386
+ }
36387
+ assert( pPgHdr->pCache==pCache );
36388
+ assert( pPgHdr->pgno==pgno );
36389
+ assert( pPgHdr->pData==pPage->pBuf );
36390
+ assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
36391
+
36392
+ if( 0==pPgHdr->nRef ){
3549136393
pCache->nRef++;
3549236394
}
35493
- pPage->nRef++;
36395
+ pPgHdr->nRef++;
3549436396
if( pgno==1 ){
35495
- pCache->pPage1 = pPage;
36397
+ pCache->pPage1 = pPgHdr;
3549636398
}
3549736399
}
35498
- *ppPage = pPage;
35499
- return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
36400
+ *ppPage = pPgHdr;
36401
+ return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
3550036402
}
3550136403
3550236404
/*
3550336405
** Decrement the reference count on a page. If the page is clean and the
3550436406
** reference count drops to 0, then it is made elible for recycling.
@@ -35541,11 +36443,11 @@
3554136443
pCache = p->pCache;
3554236444
pCache->nRef--;
3554336445
if( p->pgno==1 ){
3554436446
pCache->pPage1 = 0;
3554536447
}
35546
- sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
36448
+ sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
3554736449
}
3554836450
3554936451
/*
3555036452
** Make sure the page is marked as dirty. If it isn't dirty already,
3555136453
** make it so.
@@ -35599,11 +36501,11 @@
3559936501
*/
3560036502
SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
3560136503
PCache *pCache = p->pCache;
3560236504
assert( p->nRef>0 );
3560336505
assert( newPgno>0 );
35604
- sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
36506
+ sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
3560536507
p->pgno = newPgno;
3560636508
if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
3560736509
pcacheRemoveFromDirtyList(p);
3560836510
pcacheAddToDirtyList(p);
3560936511
}
@@ -35636,20 +36538,20 @@
3563636538
}
3563736539
if( pgno==0 && pCache->pPage1 ){
3563836540
memset(pCache->pPage1->pData, 0, pCache->szPage);
3563936541
pgno = 1;
3564036542
}
35641
- sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
36543
+ sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
3564236544
}
3564336545
}
3564436546
3564536547
/*
3564636548
** Close a cache.
3564736549
*/
3564836550
SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
3564936551
if( pCache->pCache ){
35650
- sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
36552
+ sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
3565136553
}
3565236554
}
3565336555
3565436556
/*
3565536557
** Discard the contents of the cache.
@@ -35757,31 +36659,41 @@
3575736659
** Return the total number of pages in the cache.
3575836660
*/
3575936661
SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
3576036662
int nPage = 0;
3576136663
if( pCache->pCache ){
35762
- nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
36664
+ nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
3576336665
}
3576436666
return nPage;
3576536667
}
3576636668
3576736669
#ifdef SQLITE_TEST
3576836670
/*
3576936671
** Get the suggested cache-size value.
3577036672
*/
3577136673
SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
35772
- return pCache->nMax;
36674
+ return numberOfCachePages(pCache);
3577336675
}
3577436676
#endif
3577536677
3577636678
/*
3577736679
** Set the suggested cache-size value.
3577836680
*/
3577936681
SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
35780
- pCache->nMax = mxPage;
36682
+ pCache->szCache = mxPage;
36683
+ if( pCache->pCache ){
36684
+ sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
36685
+ numberOfCachePages(pCache));
36686
+ }
36687
+}
36688
+
36689
+/*
36690
+** Free up as much memory as possible from the page cache.
36691
+*/
36692
+SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
3578136693
if( pCache->pCache ){
35782
- sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
36694
+ sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
3578336695
}
3578436696
}
3578536697
3578636698
#if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
3578736699
/*
@@ -35822,11 +36734,10 @@
3582236734
typedef struct PCache1 PCache1;
3582336735
typedef struct PgHdr1 PgHdr1;
3582436736
typedef struct PgFreeslot PgFreeslot;
3582536737
typedef struct PGroup PGroup;
3582636738
35827
-
3582836739
/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
3582936740
** of one or more PCaches that are able to recycle each others unpinned
3583036741
** pages when they are under memory pressure. A PGroup is an instance of
3583136742
** the following object.
3583236743
**
@@ -35839,22 +36750,22 @@
3583936750
** of.
3584036751
**
3584136752
** Mode 1 uses more memory (since PCache instances are not able to rob
3584236753
** unused pages from other PCaches) but it also operates without a mutex,
3584336754
** and is therefore often faster. Mode 2 requires a mutex in order to be
35844
-** threadsafe, but is able recycle pages more efficient.
36755
+** threadsafe, but recycles pages more efficiently.
3584536756
**
3584636757
** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
3584736758
** PGroup which is the pcache1.grp global variable and its mutex is
3584836759
** SQLITE_MUTEX_STATIC_LRU.
3584936760
*/
3585036761
struct PGroup {
3585136762
sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
35852
- int nMaxPage; /* Sum of nMax for purgeable caches */
35853
- int nMinPage; /* Sum of nMin for purgeable caches */
35854
- int mxPinned; /* nMaxpage + 10 - nMinPage */
35855
- int nCurrentPage; /* Number of purgeable pages allocated */
36763
+ unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
36764
+ unsigned int nMinPage; /* Sum of nMin for purgeable caches */
36765
+ unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
36766
+ unsigned int nCurrentPage; /* Number of purgeable pages allocated */
3585636767
PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
3585736768
};
3585836769
3585936770
/* Each page cache is an instance of the following object. Every
3586036771
** open database file (including each in-memory database and each
@@ -35865,15 +36776,16 @@
3586536776
** opaque sqlite3_pcache* handles.
3586636777
*/
3586736778
struct PCache1 {
3586836779
/* Cache configuration parameters. Page size (szPage) and the purgeable
3586936780
** flag (bPurgeable) are set when the cache is created. nMax may be
35870
- ** modified at any time by a call to the pcache1CacheSize() method.
36781
+ ** modified at any time by a call to the pcache1Cachesize() method.
3587136782
** The PGroup mutex must be held when accessing nMax.
3587236783
*/
3587336784
PGroup *pGroup; /* PGroup this cache belongs to */
3587436785
int szPage; /* Size of allocated pages in bytes */
36786
+ int szExtra; /* Size of extra space in bytes */
3587536787
int bPurgeable; /* True if cache is purgeable */
3587636788
unsigned int nMin; /* Minimum number of pages reserved */
3587736789
unsigned int nMax; /* Configured "cache_size" value */
3587836790
unsigned int n90pct; /* nMax*9/10 */
3587936791
@@ -35888,15 +36800,16 @@
3588836800
unsigned int iMaxKey; /* Largest key seen since xTruncate() */
3588936801
};
3589036802
3589136803
/*
3589236804
** Each cache entry is represented by an instance of the following
35893
-** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
35894
-** directly before this structure in memory (see the PGHDR1_TO_PAGE()
35895
-** macro below).
36805
+** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
36806
+** PgHdr1.pCache->szPage bytes is allocated directly before this structure
36807
+** in memory.
3589636808
*/
3589736809
struct PgHdr1 {
36810
+ sqlite3_pcache_page page;
3589836811
unsigned int iKey; /* Key value (page number) */
3589936812
PgHdr1 *pNext; /* Next in hash table chain */
3590036813
PCache1 *pCache; /* Cache that currently owns this page */
3590136814
PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
3590236815
PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
@@ -35942,25 +36855,10 @@
3594236855
** alias "pcache1". This ensures that the WSD emulation is used when
3594336856
** compiling for systems that do not support real WSD.
3594436857
*/
3594536858
#define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
3594636859
35947
-/*
35948
-** When a PgHdr1 structure is allocated, the associated PCache1.szPage
35949
-** bytes of data are located directly before it in memory (i.e. the total
35950
-** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
35951
-** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
35952
-** an argument and returns a pointer to the associated block of szPage
35953
-** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
35954
-** a pointer to a block of szPage bytes of data and the return value is
35955
-** a pointer to the associated PgHdr1 structure.
35956
-**
35957
-** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35958
-*/
35959
-#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35960
-#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35961
-
3596236860
/*
3596336861
** Macros to enter and leave the PCache LRU mutex.
3596436862
*/
3596536863
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
3596636864
#define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
@@ -36039,12 +36937,13 @@
3603936937
}
3604036938
3604136939
/*
3604236940
** Free an allocated buffer obtained from pcache1Alloc().
3604336941
*/
36044
-static void pcache1Free(void *p){
36045
- if( p==0 ) return;
36942
+static int pcache1Free(void *p){
36943
+ int nFreed = 0;
36944
+ if( p==0 ) return 0;
3604636945
if( p>=pcache1.pStart && p<pcache1.pEnd ){
3604736946
PgFreeslot *pSlot;
3604836947
sqlite3_mutex_enter(pcache1.mutex);
3604936948
sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
3605036949
pSlot = (PgFreeslot*)p;
@@ -36053,19 +36952,19 @@
3605336952
pcache1.nFreeSlot++;
3605436953
pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
3605536954
assert( pcache1.nFreeSlot<=pcache1.nSlot );
3605636955
sqlite3_mutex_leave(pcache1.mutex);
3605736956
}else{
36058
- int iSize;
3605936957
assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
3606036958
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
36061
- iSize = sqlite3MallocSize(p);
36959
+ nFreed = sqlite3MallocSize(p);
3606236960
sqlite3_mutex_enter(pcache1.mutex);
36063
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
36961
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
3606436962
sqlite3_mutex_leave(pcache1.mutex);
3606536963
sqlite3_free(p);
3606636964
}
36965
+ return nFreed;
3606736966
}
3606836967
3606936968
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
3607036969
/*
3607136970
** Return the size of a pcache allocation
@@ -36086,29 +36985,41 @@
3608636985
3608736986
/*
3608836987
** Allocate a new page object initially associated with cache pCache.
3608936988
*/
3609036989
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36091
- int nByte = sizeof(PgHdr1) + pCache->szPage;
3609236990
PgHdr1 *p = 0;
3609336991
void *pPg;
3609436992
3609536993
/* The group mutex must be released before pcache1Alloc() is called. This
3609636994
** is because it may call sqlite3_release_memory(), which assumes that
3609736995
** this mutex is not held. */
3609836996
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
3609936997
pcache1LeaveMutex(pCache->pGroup);
36100
- pPg = pcache1Alloc(nByte);
36998
+#ifdef SQLITE_PCACHE_SEPARATE_HEADER
36999
+ pPg = pcache1Alloc(pCache->szPage);
37000
+ p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
37001
+ if( !pPg || !p ){
37002
+ pcache1Free(pPg);
37003
+ sqlite3_free(p);
37004
+ pPg = 0;
37005
+ }
37006
+#else
37007
+ pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
37008
+ p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
37009
+#endif
3610137010
pcache1EnterMutex(pCache->pGroup);
3610237011
3610337012
if( pPg ){
36104
- p = PAGE_TO_PGHDR1(pCache, pPg);
37013
+ p->page.pBuf = pPg;
37014
+ p->page.pExtra = &p[1];
3610537015
if( pCache->bPurgeable ){
3610637016
pCache->pGroup->nCurrentPage++;
3610737017
}
37018
+ return p;
3610837019
}
36109
- return p;
37020
+ return 0;
3611037021
}
3611137022
3611237023
/*
3611337024
** Free a page object allocated by pcache1AllocPage().
3611437025
**
@@ -36118,11 +37029,14 @@
3611837029
*/
3611937030
static void pcache1FreePage(PgHdr1 *p){
3612037031
if( ALWAYS(p) ){
3612137032
PCache1 *pCache = p->pCache;
3612237033
assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36123
- pcache1Free(PGHDR1_TO_PAGE(p));
37034
+ pcache1Free(p->page.pBuf);
37035
+#ifdef SQLITE_PCACHE_SEPARATE_HEADER
37036
+ sqlite3_free(p);
37037
+#endif
3612437038
if( pCache->bPurgeable ){
3612537039
pCache->pGroup->nCurrentPage--;
3612637040
}
3612737041
}
3612837042
}
@@ -36153,17 +37067,17 @@
3615337067
** it is desirable to avoid allocating a new page cache entry because
3615437068
** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
3615537069
** for all page cache needs and we should not need to spill the
3615637070
** allocation onto the heap.
3615737071
**
36158
-** Or, the heap is used for all page cache memory put the heap is
37072
+** Or, the heap is used for all page cache memory but the heap is
3615937073
** under memory pressure, then again it is desirable to avoid
3616037074
** allocating a new page cache entry in order to avoid stressing
3616137075
** the heap even further.
3616237076
*/
3616337077
static int pcache1UnderMemoryPressure(PCache1 *pCache){
36164
- if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
37078
+ if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
3616537079
return pcache1.bUnderPressure;
3616637080
}else{
3616737081
return sqlite3HeapNearlyFull();
3616837082
}
3616937083
}
@@ -36350,11 +37264,11 @@
3635037264
/*
3635137265
** Implementation of the sqlite3_pcache.xCreate method.
3635237266
**
3635337267
** Allocate a new cache.
3635437268
*/
36355
-static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
37269
+static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
3635637270
PCache1 *pCache; /* The newly created page cache */
3635737271
PGroup *pGroup; /* The group the new page cache will belong to */
3635837272
int sz; /* Bytes of memory required to allocate the new cache */
3635937273
3636037274
/*
@@ -36372,10 +37286,13 @@
3637237286
#if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
3637337287
const int separateCache = 0;
3637437288
#else
3637537289
int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
3637637290
#endif
37291
+
37292
+ assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
37293
+ assert( szExtra < 300 );
3637737294
3637837295
sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
3637937296
pCache = (PCache1 *)sqlite3_malloc(sz);
3638037297
if( pCache ){
3638137298
memset(pCache, 0, sz);
@@ -36385,10 +37302,11 @@
3638537302
}else{
3638637303
pGroup = &pcache1.grp;
3638737304
}
3638837305
pCache->pGroup = pGroup;
3638937306
pCache->szPage = szPage;
37307
+ pCache->szExtra = szExtra;
3639037308
pCache->bPurgeable = (bPurgeable ? 1 : 0);
3639137309
if( bPurgeable ){
3639237310
pCache->nMin = 10;
3639337311
pcache1EnterMutex(pGroup);
3639437312
pGroup->nMinPage += pCache->nMin;
@@ -36415,10 +37333,29 @@
3641537333
pCache->n90pct = pCache->nMax*9/10;
3641637334
pcache1EnforceMaxPage(pGroup);
3641737335
pcache1LeaveMutex(pGroup);
3641837336
}
3641937337
}
37338
+
37339
+/*
37340
+** Implementation of the sqlite3_pcache.xShrink method.
37341
+**
37342
+** Free up as much memory as possible.
37343
+*/
37344
+static void pcache1Shrink(sqlite3_pcache *p){
37345
+ PCache1 *pCache = (PCache1*)p;
37346
+ if( pCache->bPurgeable ){
37347
+ PGroup *pGroup = pCache->pGroup;
37348
+ int savedMaxPage;
37349
+ pcache1EnterMutex(pGroup);
37350
+ savedMaxPage = pGroup->nMaxPage;
37351
+ pGroup->nMaxPage = 0;
37352
+ pcache1EnforceMaxPage(pGroup);
37353
+ pGroup->nMaxPage = savedMaxPage;
37354
+ pcache1LeaveMutex(pGroup);
37355
+ }
37356
+}
3642037357
3642137358
/*
3642237359
** Implementation of the sqlite3_pcache.xPagecount method.
3642337360
*/
3642437361
static int pcache1Pagecount(sqlite3_pcache *p){
@@ -36441,11 +37378,11 @@
3644137378
** means to try really hard to allocate a new page.
3644237379
**
3644337380
** For a non-purgeable cache (a cache used as the storage for an in-memory
3644437381
** database) there is really no difference between createFlag 1 and 2. So
3644537382
** the calling function (pcache.c) will never have a createFlag of 1 on
36446
-** a non-purgable cache.
37383
+** a non-purgeable cache.
3644737384
**
3644837385
** There are three different approaches to obtaining space for a page,
3644937386
** depending on the value of parameter createFlag (which may be 0, 1 or 2).
3645037387
**
3645137388
** 1. Regardless of the value of createFlag, the cache is searched for a
@@ -36482,12 +37419,16 @@
3648237419
** size, return the recycled buffer. Otherwise, free the buffer and
3648337420
** proceed to step 5.
3648437421
**
3648537422
** 5. Otherwise, allocate and return a new page buffer.
3648637423
*/
36487
-static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
36488
- int nPinned;
37424
+static sqlite3_pcache_page *pcache1Fetch(
37425
+ sqlite3_pcache *p,
37426
+ unsigned int iKey,
37427
+ int createFlag
37428
+){
37429
+ unsigned int nPinned;
3648937430
PCache1 *pCache = (PCache1 *)p;
3649037431
PGroup *pGroup;
3649137432
PgHdr1 *pPage = 0;
3649237433
3649337434
assert( pCache->bPurgeable || createFlag!=1 );
@@ -36517,19 +37458,18 @@
3651737458
*/
3651837459
#ifdef SQLITE_MUTEX_OMIT
3651937460
pGroup = pCache->pGroup;
3652037461
#endif
3652137462
36522
-
3652337463
/* Step 3: Abort if createFlag is 1 but the cache is nearly full */
37464
+ assert( pCache->nPage >= pCache->nRecyclable );
3652437465
nPinned = pCache->nPage - pCache->nRecyclable;
36525
- assert( nPinned>=0 );
3652637466
assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
3652737467
assert( pCache->n90pct == pCache->nMax*9/10 );
3652837468
if( createFlag==1 && (
3652937469
nPinned>=pGroup->mxPinned
36530
- || nPinned>=(int)pCache->n90pct
37470
+ || nPinned>=pCache->n90pct
3653137471
|| pcache1UnderMemoryPressure(pCache)
3653237472
)){
3653337473
goto fetch_out;
3653437474
}
3653537475
@@ -36541,20 +37481,28 @@
3654137481
if( pCache->bPurgeable && pGroup->pLruTail && (
3654237482
(pCache->nPage+1>=pCache->nMax)
3654337483
|| pGroup->nCurrentPage>=pGroup->nMaxPage
3654437484
|| pcache1UnderMemoryPressure(pCache)
3654537485
)){
36546
- PCache1 *pOtherCache;
37486
+ PCache1 *pOther;
3654737487
pPage = pGroup->pLruTail;
3654837488
pcache1RemoveFromHash(pPage);
3654937489
pcache1PinPage(pPage);
36550
- if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
37490
+ pOther = pPage->pCache;
37491
+
37492
+ /* We want to verify that szPage and szExtra are the same for pOther
37493
+ ** and pCache. Assert that we can verify this by comparing sums. */
37494
+ assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
37495
+ assert( pCache->szExtra<512 );
37496
+ assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
37497
+ assert( pOther->szExtra<512 );
37498
+
37499
+ if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
3655137500
pcache1FreePage(pPage);
3655237501
pPage = 0;
3655337502
}else{
36554
- pGroup->nCurrentPage -=
36555
- (pOtherCache->bPurgeable - pCache->bPurgeable);
37503
+ pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
3655637504
}
3655737505
}
3655837506
3655937507
/* Step 5. If a usable page buffer has still not been found,
3656037508
** attempt to allocate a new one.
@@ -36571,31 +37519,35 @@
3657137519
pPage->iKey = iKey;
3657237520
pPage->pNext = pCache->apHash[h];
3657337521
pPage->pCache = pCache;
3657437522
pPage->pLruPrev = 0;
3657537523
pPage->pLruNext = 0;
36576
- *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
37524
+ *(void **)pPage->page.pExtra = 0;
3657737525
pCache->apHash[h] = pPage;
3657837526
}
3657937527
3658037528
fetch_out:
3658137529
if( pPage && iKey>pCache->iMaxKey ){
3658237530
pCache->iMaxKey = iKey;
3658337531
}
3658437532
pcache1LeaveMutex(pGroup);
36585
- return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
37533
+ return &pPage->page;
3658637534
}
3658737535
3658837536
3658937537
/*
3659037538
** Implementation of the sqlite3_pcache.xUnpin method.
3659137539
**
3659237540
** Mark a page as unpinned (eligible for asynchronous recycling).
3659337541
*/
36594
-static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
37542
+static void pcache1Unpin(
37543
+ sqlite3_pcache *p,
37544
+ sqlite3_pcache_page *pPg,
37545
+ int reuseUnlikely
37546
+){
3659537547
PCache1 *pCache = (PCache1 *)p;
36596
- PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
37548
+ PgHdr1 *pPage = (PgHdr1 *)pPg;
3659737549
PGroup *pGroup = pCache->pGroup;
3659837550
3659937551
assert( pPage->pCache==pCache );
3660037552
pcache1EnterMutex(pGroup);
3660137553
@@ -36627,16 +37579,16 @@
3662737579
/*
3662837580
** Implementation of the sqlite3_pcache.xRekey method.
3662937581
*/
3663037582
static void pcache1Rekey(
3663137583
sqlite3_pcache *p,
36632
- void *pPg,
37584
+ sqlite3_pcache_page *pPg,
3663337585
unsigned int iOld,
3663437586
unsigned int iNew
3663537587
){
3663637588
PCache1 *pCache = (PCache1 *)p;
36637
- PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
37589
+ PgHdr1 *pPage = (PgHdr1 *)pPg;
3663837590
PgHdr1 **pp;
3663937591
unsigned int h;
3664037592
assert( pPage->iKey==iOld );
3664137593
assert( pPage->pCache==pCache );
3664237594
@@ -36686,11 +37638,13 @@
3668637638
PCache1 *pCache = (PCache1 *)p;
3668737639
PGroup *pGroup = pCache->pGroup;
3668837640
assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
3668937641
pcache1EnterMutex(pGroup);
3669037642
pcache1TruncateUnsafe(pCache, 0);
37643
+ assert( pGroup->nMaxPage >= pCache->nMax );
3669137644
pGroup->nMaxPage -= pCache->nMax;
37645
+ assert( pGroup->nMinPage >= pCache->nMin );
3669237646
pGroup->nMinPage -= pCache->nMin;
3669337647
pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
3669437648
pcache1EnforceMaxPage(pGroup);
3669537649
pcache1LeaveMutex(pGroup);
3669637650
sqlite3_free(pCache->apHash);
@@ -36701,11 +37655,12 @@
3670137655
** This function is called during initialization (sqlite3_initialize()) to
3670237656
** install the default pluggable cache module, assuming the user has not
3670337657
** already provided an alternative.
3670437658
*/
3670537659
SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
36706
- static const sqlite3_pcache_methods defaultMethods = {
37660
+ static const sqlite3_pcache_methods2 defaultMethods = {
37661
+ 1, /* iVersion */
3670737662
0, /* pArg */
3670837663
pcache1Init, /* xInit */
3670937664
pcache1Shutdown, /* xShutdown */
3671037665
pcache1Create, /* xCreate */
3671137666
pcache1Cachesize, /* xCachesize */
@@ -36712,13 +37667,14 @@
3671237667
pcache1Pagecount, /* xPagecount */
3671337668
pcache1Fetch, /* xFetch */
3671437669
pcache1Unpin, /* xUnpin */
3671537670
pcache1Rekey, /* xRekey */
3671637671
pcache1Truncate, /* xTruncate */
36717
- pcache1Destroy /* xDestroy */
37672
+ pcache1Destroy, /* xDestroy */
37673
+ pcache1Shrink /* xShrink */
3671837674
};
36719
- sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
37675
+ sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
3672037676
}
3672137677
3672237678
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
3672337679
/*
3672437680
** This function is called to free superfluous dynamically allocated memory
@@ -36735,11 +37691,14 @@
3673537691
assert( sqlite3_mutex_notheld(pcache1.mutex) );
3673637692
if( pcache1.pStart==0 ){
3673737693
PgHdr1 *p;
3673837694
pcache1EnterMutex(&pcache1.grp);
3673937695
while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
36740
- nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
37696
+ nFree += pcache1MemSize(p->page.pBuf);
37697
+#ifdef SQLITE_PCACHE_SEPARATE_HEADER
37698
+ nFree += sqlite3MemSize(p);
37699
+#endif
3674137700
pcache1PinPage(p);
3674237701
pcache1RemoveFromHash(p);
3674337702
pcache1FreePage(p);
3674437703
}
3674537704
pcache1LeaveMutex(&pcache1.grp);
@@ -36763,12 +37722,12 @@
3676337722
int nRecyclable = 0;
3676437723
for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
3676537724
nRecyclable++;
3676637725
}
3676737726
*pnCurrent = pcache1.grp.nCurrentPage;
36768
- *pnMax = pcache1.grp.nMaxPage;
36769
- *pnMin = pcache1.grp.nMinPage;
37727
+ *pnMax = (int)pcache1.grp.nMaxPage;
37728
+ *pnMin = (int)pcache1.grp.nMinPage;
3677037729
*pnRecyclable = nRecyclable;
3677137730
}
3677237731
#endif
3677337732
3677437733
/************** End of pcache1.c *********************************************/
@@ -37238,10 +38197,16 @@
3723838197
3723938198
#ifndef _WAL_H_
3724038199
#define _WAL_H_
3724138200
3724238201
38202
+/* Additional values that can be added to the sync_flags argument of
38203
+** sqlite3WalFrames():
38204
+*/
38205
+#define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
38206
+#define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */
38207
+
3724338208
#ifdef SQLITE_OMIT_WAL
3724438209
# define sqlite3WalOpen(x,y,z) 0
3724538210
# define sqlite3WalLimit(x,y)
3724638211
# define sqlite3WalClose(w,x,y,z) 0
3724738212
# define sqlite3WalBeginReadTransaction(y,z) 0
@@ -37937,10 +38902,11 @@
3793738902
u8 useJournal; /* Use a rollback journal on this file */
3793838903
u8 noReadlock; /* Do not bother to obtain readlocks */
3793938904
u8 noSync; /* Do not sync the journal if true */
3794038905
u8 fullSync; /* Do extra syncs of the journal for robustness */
3794138906
u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
38907
+ u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
3794238908
u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
3794338909
u8 tempFile; /* zFilename is a temporary file */
3794438910
u8 readOnly; /* True for a read-only database */
3794538911
u8 memDb; /* True to inhibit all file I/O */
3794638912
@@ -38107,11 +39073,11 @@
3810739073
return (pPager->pWal!=0);
3810839074
}
3810939075
#else
3811039076
# define pagerUseWal(x) 0
3811139077
# define pagerRollbackWal(x) 0
38112
-# define pagerWalFrames(v,w,x,y,z) 0
39078
+# define pagerWalFrames(v,w,x,y) 0
3811339079
# define pagerOpenWalIfPresent(z) SQLITE_OK
3811439080
# define pagerBeginReadTransaction(z) SQLITE_OK
3811539081
#endif
3811639082
3811739083
#ifndef NDEBUG
@@ -39806,14 +40772,13 @@
3980640772
rc = sqlite3OsFileSize(pPager->fd, &currentSize);
3980740773
newSize = szPage*(i64)nPage;
3980840774
if( rc==SQLITE_OK && currentSize!=newSize ){
3980940775
if( currentSize>newSize ){
3981040776
rc = sqlite3OsTruncate(pPager->fd, newSize);
39811
- }else{
40777
+ }else if( (currentSize+szPage)<=newSize ){
3981240778
char *pTmp = pPager->pTmpSpace;
3981340779
memset(pTmp, 0, szPage);
39814
- testcase( (newSize-szPage) < currentSize );
3981540780
testcase( (newSize-szPage) == currentSize );
3981640781
testcase( (newSize-szPage) > currentSize );
3981740782
rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
3981840783
}
3981940784
if( rc==SQLITE_OK ){
@@ -39835,27 +40800,40 @@
3983540800
**
3983640801
** Otherwise, for non-temporary files, the effective sector size is
3983740802
** the value returned by the xSectorSize() method rounded up to 32 if
3983840803
** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
3983940804
** is greater than MAX_SECTOR_SIZE.
40805
+**
40806
+** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
40807
+** the effective sector size to its minimum value (512). The purpose of
40808
+** pPager->sectorSize is to define the "blast radius" of bytes that
40809
+** might change if a crash occurs while writing to a single byte in
40810
+** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
40811
+** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
40812
+** size. For backwards compatibility of the rollback journal file format,
40813
+** we cannot reduce the effective sector size below 512.
3984040814
*/
3984140815
static void setSectorSize(Pager *pPager){
3984240816
assert( isOpen(pPager->fd) || pPager->tempFile );
3984340817
39844
- if( !pPager->tempFile ){
40818
+ if( pPager->tempFile
40819
+ || (sqlite3OsDeviceCharacteristics(pPager->fd) &
40820
+ SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
40821
+ ){
3984540822
/* Sector size doesn't matter for temporary files. Also, the file
3984640823
** may not have been opened yet, in which case the OsSectorSize()
39847
- ** call will segfault.
39848
- */
40824
+ ** call will segfault. */
40825
+ pPager->sectorSize = 512;
40826
+ }else{
3984940827
pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
39850
- }
39851
- if( pPager->sectorSize<32 ){
39852
- pPager->sectorSize = 512;
39853
- }
39854
- if( pPager->sectorSize>MAX_SECTOR_SIZE ){
39855
- assert( MAX_SECTOR_SIZE>=512 );
39856
- pPager->sectorSize = MAX_SECTOR_SIZE;
40828
+ if( pPager->sectorSize<32 ){
40829
+ pPager->sectorSize = 512;
40830
+ }
40831
+ if( pPager->sectorSize>MAX_SECTOR_SIZE ){
40832
+ assert( MAX_SECTOR_SIZE>=512 );
40833
+ pPager->sectorSize = MAX_SECTOR_SIZE;
40834
+ }
3985740835
}
3985840836
}
3985940837
3986040838
/*
3986140839
** Playback the journal and thus restore the database file to
@@ -40054,14 +41032,15 @@
4005441032
/* Following a rollback, the database file should be back in its original
4005541033
** state prior to the start of the transaction, so invoke the
4005641034
** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
4005741035
** assertion that the transaction counter was modified.
4005841036
*/
40059
- assert(
40060
- pPager->fd->pMethods==0 ||
40061
- sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
40062
- );
41037
+#ifdef SQLITE_DEBUG
41038
+ if( pPager->fd->pMethods ){
41039
+ sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
41040
+ }
41041
+#endif
4006341042
4006441043
/* If this playback is happening automatically as a result of an IO or
4006541044
** malloc error that occurred after the change-counter was updated but
4006641045
** before the transaction was committed, then the change-counter
4006741046
** modification may just have been reverted. If this happens in exclusive
@@ -40276,12 +41255,11 @@
4027641255
*/
4027741256
static int pagerWalFrames(
4027841257
Pager *pPager, /* Pager object */
4027941258
PgHdr *pList, /* List of frames to log */
4028041259
Pgno nTruncate, /* Database size after this commit */
40281
- int isCommit, /* True if this is a commit */
40282
- int syncFlags /* Flags to pass to OsSync() (or 0) */
41260
+ int isCommit /* True if this is a commit */
4028341261
){
4028441262
int rc; /* Return code */
4028541263
#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
4028641264
PgHdr *p; /* For looping over pages */
4028741265
#endif
@@ -40308,11 +41286,11 @@
4030841286
assert( pList );
4030941287
}
4031041288
4031141289
if( pList->pgno==1 ) pager_write_changecounter(pList);
4031241290
rc = sqlite3WalFrames(pPager->pWal,
40313
- pPager->pageSize, pList, nTruncate, isCommit, syncFlags
41291
+ pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
4031441292
);
4031541293
if( rc==SQLITE_OK && pPager->pBackup ){
4031641294
PgHdr *p;
4031741295
for(p=pList; p; p=p->pDirty){
4031841296
sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
@@ -40395,14 +41373,11 @@
4039541373
int rc = sqlite3OsFileSize(pPager->fd, &n);
4039641374
if( rc!=SQLITE_OK ){
4039741375
return rc;
4039841376
}
4039941377
}
40400
- nPage = (Pgno)(n / pPager->pageSize);
40401
- if( nPage==0 && n>0 ){
40402
- nPage = 1;
40403
- }
41378
+ nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
4040441379
}
4040541380
4040641381
/* If the current number of pages in the file is greater than the
4040741382
** configured maximum pager number, increase the allowed limit so
4040841383
** that the file can be read.
@@ -40588,17 +41563,17 @@
4058841563
** previously rolled back out of the main journal (and are hence in pDone)
4058941564
** will be skipped. Out-of-range pages are also skipped.
4059041565
*/
4059141566
if( pSavepoint ){
4059241567
u32 ii; /* Loop counter */
40593
- i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
41568
+ i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
4059441569
4059541570
if( pagerUseWal(pPager) ){
4059641571
rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
4059741572
}
4059841573
for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
40599
- assert( offset==ii*(4+pPager->pageSize) );
41574
+ assert( offset==(i64)ii*(4+pPager->pageSize) );
4060041575
rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
4060141576
}
4060241577
assert( rc!=SQLITE_DONE );
4060341578
}
4060441579
@@ -40614,10 +41589,17 @@
4061441589
** Change the maximum number of in-memory pages that are allowed.
4061541590
*/
4061641591
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
4061741592
sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
4061841593
}
41594
+
41595
+/*
41596
+** Free as much memory as possible from the pager.
41597
+*/
41598
+SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41599
+ sqlite3PcacheShrink(pPager->pPCache);
41600
+}
4061941601
4062041602
/*
4062141603
** Adjust the robustness of the database to damage due to OS crashes
4062241604
** or power failures by changing the number of syncs()s when writing
4062341605
** the rollback journal. There are three levels:
@@ -40681,10 +41663,14 @@
4068141663
pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
4068241664
}else{
4068341665
pPager->syncFlags = SQLITE_SYNC_NORMAL;
4068441666
pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
4068541667
}
41668
+ pPager->walSyncFlags = pPager->syncFlags;
41669
+ if( pPager->fullSync ){
41670
+ pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41671
+ }
4068641672
}
4068741673
#endif
4068841674
4068941675
/*
4069041676
** The following global variable is incremented whenever the library
@@ -40818,11 +41804,11 @@
4081841804
if( !pNew ) rc = SQLITE_NOMEM;
4081941805
}
4082041806
4082141807
if( rc==SQLITE_OK ){
4082241808
pager_reset(pPager);
40823
- pPager->dbSize = (Pgno)(nByte/pageSize);
41809
+ pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
4082441810
pPager->pageSize = pageSize;
4082541811
sqlite3PageFree(pPager->pTmpSpace);
4082641812
pPager->pTmpSpace = pNew;
4082741813
sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
4082841814
}
@@ -41326,11 +42312,11 @@
4132642312
** file size will be.
4132742313
*/
4132842314
assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
4132942315
if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
4133042316
sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
41331
- sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
42317
+ sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
4133242318
pPager->dbHintSize = pPager->dbSize;
4133342319
}
4133442320
4133542321
while( rc==SQLITE_OK && pList ){
4133642322
Pgno pgno = pList->pgno;
@@ -41435,11 +42421,11 @@
4143542421
4143642422
/* If the sub-journal was opened successfully (or was already open),
4143742423
** write the journal record into the file. */
4143842424
if( rc==SQLITE_OK ){
4143942425
void *pData = pPg->pData;
41440
- i64 offset = pPager->nSubRec*(4+pPager->pageSize);
42426
+ i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
4144142427
char *pData2;
4144242428
4144342429
CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
4144442430
PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
4144542431
rc = write32bits(pPager->sjfd, offset, pPg->pgno);
@@ -41508,11 +42494,11 @@
4150842494
/* Write a single frame for this page to the log. */
4150942495
if( subjRequiresPage(pPg) ){
4151042496
rc = subjournalPage(pPg);
4151142497
}
4151242498
if( rc==SQLITE_OK ){
41513
- rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
42499
+ rc = pagerWalFrames(pPager, pPg, 0, 0);
4151442500
}
4151542501
}else{
4151642502
4151742503
/* Sync the journal file if required. */
4151842504
if( pPg->flags&PGHDR_NEED_SYNC
@@ -41667,11 +42653,12 @@
4166742653
z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
4166842654
while( *z ){
4166942655
z += sqlite3Strlen30(z)+1;
4167042656
z += sqlite3Strlen30(z)+1;
4167142657
}
41672
- nUri = &z[1] - zUri;
42658
+ nUri = (int)(&z[1] - zUri);
42659
+ assert( nUri>=0 );
4167342660
if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
4167442661
/* This branch is taken when the journal path required by
4167542662
** the database being opened will be more than pVfs->mxPathname
4167642663
** bytes in length. This means the database cannot be opened,
4167742664
** as it will not be possible to open the journal file or even
@@ -41701,13 +42688,13 @@
4170142688
ROUND8(sizeof(*pPager)) + /* Pager structure */
4170242689
ROUND8(pcacheSize) + /* PCache object */
4170342690
ROUND8(pVfs->szOsFile) + /* The main db file */
4170442691
journalFileSize * 2 + /* The two journal files */
4170542692
nPathname + 1 + nUri + /* zFilename */
41706
- nPathname + 8 + 1 /* zJournal */
42693
+ nPathname + 8 + 2 /* zJournal */
4170742694
#ifndef SQLITE_OMIT_WAL
41708
- + nPathname + 4 + 1 /* zWal */
42695
+ + nPathname + 4 + 2 /* zWal */
4170942696
#endif
4171042697
);
4171142698
assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
4171242699
if( !pPtr ){
4171342700
sqlite3_free(zPathname);
@@ -41726,16 +42713,16 @@
4172642713
assert( nPathname>0 );
4172742714
pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
4172842715
memcpy(pPager->zFilename, zPathname, nPathname);
4172942716
memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
4173042717
memcpy(pPager->zJournal, zPathname, nPathname);
41731
- memcpy(&pPager->zJournal[nPathname], "-journal", 8);
42718
+ memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
4173242719
sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
4173342720
#ifndef SQLITE_OMIT_WAL
4173442721
pPager->zWal = &pPager->zJournal[nPathname+8+1];
4173542722
memcpy(pPager->zWal, zPathname, nPathname);
41736
- memcpy(&pPager->zWal[nPathname], "-wal", 4);
42723
+ memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
4173742724
sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
4173842725
#endif
4173942726
sqlite3_free(zPathname);
4174042727
}
4174142728
pPager->pVfs = pVfs;
@@ -41847,13 +42834,21 @@
4184742834
pPager->changeCountDone = pPager->tempFile;
4184842835
pPager->memDb = (u8)memDb;
4184942836
pPager->readOnly = (u8)readOnly;
4185042837
assert( useJournal || pPager->tempFile );
4185142838
pPager->noSync = pPager->tempFile;
41852
- pPager->fullSync = pPager->noSync ?0:1;
41853
- pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL;
41854
- pPager->ckptSyncFlags = pPager->syncFlags;
42839
+ if( pPager->noSync ){
42840
+ assert( pPager->fullSync==0 );
42841
+ assert( pPager->syncFlags==0 );
42842
+ assert( pPager->walSyncFlags==0 );
42843
+ assert( pPager->ckptSyncFlags==0 );
42844
+ }else{
42845
+ pPager->fullSync = 1;
42846
+ pPager->syncFlags = SQLITE_SYNC_NORMAL;
42847
+ pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
42848
+ pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
42849
+ }
4185542850
/* pPager->pFirst = 0; */
4185642851
/* pPager->pFirstSynced = 0; */
4185742852
/* pPager->pLast = 0; */
4185842853
pPager->nExtra = (u16)nExtra;
4185942854
pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
@@ -42982,11 +43977,14 @@
4298243977
if( !pPager->noSync ){
4298343978
assert( !MEMDB );
4298443979
rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
4298543980
}else if( isOpen(pPager->fd) ){
4298643981
assert( !MEMDB );
42987
- sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
43982
+ rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
43983
+ if( rc==SQLITE_NOTFOUND ){
43984
+ rc = SQLITE_OK;
43985
+ }
4298843986
}
4298943987
return rc;
4299043988
}
4299143989
4299243990
/*
@@ -43079,13 +44077,11 @@
4307944077
pList = pPageOne;
4308044078
pList->pDirty = 0;
4308144079
}
4308244080
assert( rc==SQLITE_OK );
4308344081
if( ALWAYS(pList) ){
43084
- rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
43085
- (pPager->fullSync ? pPager->syncFlags : 0)
43086
- );
44082
+ rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
4308744083
}
4308844084
sqlite3PagerUnref(pPageOne);
4308944085
if( rc==SQLITE_OK ){
4309044086
sqlite3PcacheCleanAll(pPager->pPCache);
4309144087
}
@@ -43340,11 +44336,12 @@
4334044336
}else{
4334144337
rc = pager_playback(pPager, 0);
4334244338
}
4334344339
4334444340
assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
43345
- assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
44341
+ assert( rc==SQLITE_OK || rc==SQLITE_FULL
44342
+ || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
4334644343
4334744344
/* If an error occurs during a ROLLBACK, we can no longer trust the pager
4334844345
** cache. So call pager_error() on the way out to make any error persistent.
4334944346
*/
4335044347
return pager_error(pPager, rc);
@@ -43980,10 +44977,19 @@
4398044977
** sqlite3BackupUpdate() only.
4398144978
*/
4398244979
SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
4398344980
return &pPager->pBackup;
4398444981
}
44982
+
44983
+#ifndef SQLITE_OMIT_VACUUM
44984
+/*
44985
+** Unless this is an in-memory or temporary database, clear the pager cache.
44986
+*/
44987
+SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
44988
+ if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
44989
+}
44990
+#endif
4398544991
4398644992
#ifndef SQLITE_OMIT_WAL
4398744993
/*
4398844994
** This function is called when the user invokes "PRAGMA wal_checkpoint",
4398944995
** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
@@ -44157,17 +45163,10 @@
4415745163
}
4415845164
}
4415945165
return rc;
4416045166
}
4416145167
44162
-/*
44163
-** Unless this is an in-memory or temporary database, clear the pager cache.
44164
-*/
44165
-SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
44166
- if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
44167
-}
44168
-
4416945168
#ifdef SQLITE_HAS_CODEC
4417045169
/*
4417145170
** This function is called by the wal module when writing page content
4417245171
** into the log file.
4417345172
**
@@ -44600,17 +45599,22 @@
4460045599
sqlite3_file *pDbFd; /* File handle for the database file */
4460145600
sqlite3_file *pWalFd; /* File handle for WAL file */
4460245601
u32 iCallback; /* Value to pass to log callback (or 0) */
4460345602
i64 mxWalSize; /* Truncate WAL to this size upon reset */
4460445603
int nWiData; /* Size of array apWiData */
45604
+ int szFirstBlock; /* Size of first block written to WAL file */
4460545605
volatile u32 **apWiData; /* Pointer to wal-index content in memory */
4460645606
u32 szPage; /* Database page size */
4460745607
i16 readLock; /* Which read lock is being held. -1 for none */
45608
+ u8 syncFlags; /* Flags to use to sync header writes */
4460845609
u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
4460945610
u8 writeLock; /* True if in a write transaction */
4461045611
u8 ckptLock; /* True if holding a checkpoint lock */
4461145612
u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
45613
+ u8 truncateOnCommit; /* True to truncate WAL file on commit */
45614
+ u8 syncHeader; /* Fsync the WAL header if true */
45615
+ u8 padToSectorBoundary; /* Pad transactions out to the next sector */
4461245616
WalIndexHdr hdr; /* Wal-index header for current transaction */
4461345617
const char *zWalName; /* Name of WAL file */
4461445618
u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
4461545619
#ifdef SQLITE_DEBUG
4461645620
u8 lockError; /* True if a locking error has occurred */
@@ -45279,10 +46283,11 @@
4527946283
int iFrame; /* Index of last frame read */
4528046284
i64 iOffset; /* Next offset to read from log file */
4528146285
int szPage; /* Page size according to the log */
4528246286
u32 magic; /* Magic value read from WAL header */
4528346287
u32 version; /* Magic value read from WAL header */
46288
+ int isValid; /* True if this frame is valid */
4528446289
4528546290
/* Read in the WAL header. */
4528646291
rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
4528746292
if( rc!=SQLITE_OK ){
4528846293
goto recovery_error;
@@ -45337,18 +46342,18 @@
4533746342
/* Read all frames from the log file. */
4533846343
iFrame = 0;
4533946344
for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
4534046345
u32 pgno; /* Database page number for frame */
4534146346
u32 nTruncate; /* dbsize field from frame header */
45342
- int isValid; /* True if this frame is valid */
4534346347
4534446348
/* Read and decode the next log frame. */
46349
+ iFrame++;
4534546350
rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
4534646351
if( rc!=SQLITE_OK ) break;
4534746352
isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
4534846353
if( !isValid ) break;
45349
- rc = walIndexAppend(pWal, ++iFrame, pgno);
46354
+ rc = walIndexAppend(pWal, iFrame, pgno);
4535046355
if( rc!=SQLITE_OK ) break;
4535146356
4535246357
/* If nTruncate is non-zero, this is a commit record. */
4535346358
if( nTruncate ){
4535446359
pWal->hdr.mxFrame = iFrame;
@@ -45467,10 +46472,12 @@
4546746472
pRet->pWalFd = (sqlite3_file *)&pRet[1];
4546846473
pRet->pDbFd = pDbFd;
4546946474
pRet->readLock = -1;
4547046475
pRet->mxWalSize = mxWalSize;
4547146476
pRet->zWalName = zWalName;
46477
+ pRet->syncHeader = 1;
46478
+ pRet->padToSectorBoundary = 1;
4547246479
pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
4547346480
4547446481
/* Open file handle on the write-ahead log file. */
4547546482
flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
4547646483
rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
@@ -45481,10 +46488,15 @@
4548146488
if( rc!=SQLITE_OK ){
4548246489
walIndexClose(pRet, 0);
4548346490
sqlite3OsClose(pRet->pWalFd);
4548446491
sqlite3_free(pRet);
4548546492
}else{
46493
+ int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
46494
+ if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
46495
+ if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
46496
+ pRet->padToSectorBoundary = 0;
46497
+ }
4548646498
*ppWal = pRet;
4548746499
WALTRACE(("WAL%d: opened\n", pRet));
4548846500
}
4548946501
return rc;
4549046502
}
@@ -45900,11 +46912,11 @@
4590046912
*/
4590146913
if( rc==SQLITE_OK ){
4590246914
i64 nReq = ((i64)mxPage * szPage);
4590346915
rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
4590446916
if( rc==SQLITE_OK && nSize<nReq ){
45905
- sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
46917
+ sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
4590646918
}
4590746919
}
4590846920
4590946921
/* Iterate through the contents of the WAL, copying data to the db file. */
4591046922
while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
@@ -45966,10 +46978,28 @@
4596646978
4596746979
walcheckpoint_out:
4596846980
walIteratorFree(pIter);
4596946981
return rc;
4597046982
}
46983
+
46984
+/*
46985
+** If the WAL file is currently larger than nMax bytes in size, truncate
46986
+** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
46987
+*/
46988
+static void walLimitSize(Wal *pWal, i64 nMax){
46989
+ i64 sz;
46990
+ int rx;
46991
+ sqlite3BeginBenignMalloc();
46992
+ rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46993
+ if( rx==SQLITE_OK && (sz > nMax ) ){
46994
+ rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
46995
+ }
46996
+ sqlite3EndBenignMalloc();
46997
+ if( rx ){
46998
+ sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46999
+ }
47000
+}
4597147001
4597247002
/*
4597347003
** Close a connection to a log file.
4597447004
*/
4597547005
SQLITE_PRIVATE int sqlite3WalClose(
@@ -45990,27 +47020,44 @@
4599047020
**
4599147021
** The EXCLUSIVE lock is not released before returning.
4599247022
*/
4599347023
rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
4599447024
if( rc==SQLITE_OK ){
45995
- int bPersistWal = -1;
4599647025
if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
4599747026
pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
4599847027
}
4599947028
rc = sqlite3WalCheckpoint(
4600047029
pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
4600147030
);
46002
- sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersistWal);
46003
- if( rc==SQLITE_OK && bPersistWal!=1 ){
46004
- isDelete = 1;
47031
+ if( rc==SQLITE_OK ){
47032
+ int bPersist = -1;
47033
+ sqlite3OsFileControlHint(
47034
+ pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
47035
+ );
47036
+ if( bPersist!=1 ){
47037
+ /* Try to delete the WAL file if the checkpoint completed and
47038
+ ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
47039
+ ** mode (!bPersist) */
47040
+ isDelete = 1;
47041
+ }else if( pWal->mxWalSize>=0 ){
47042
+ /* Try to truncate the WAL file to zero bytes if the checkpoint
47043
+ ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
47044
+ ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
47045
+ ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
47046
+ ** to zero bytes as truncating to the journal_size_limit might
47047
+ ** leave a corrupt WAL file on disk. */
47048
+ walLimitSize(pWal, 0);
47049
+ }
4600547050
}
4600647051
}
4600747052
4600847053
walIndexClose(pWal, isDelete);
4600947054
sqlite3OsClose(pWal->pWalFd);
4601047055
if( isDelete ){
47056
+ sqlite3BeginBenignMalloc();
4601147057
sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
47058
+ sqlite3EndBenignMalloc();
4601247059
}
4601347060
WALTRACE(("WAL%p: closed\n", pWal));
4601447061
sqlite3_free((void *)pWal->apWiData);
4601547062
sqlite3_free(pWal);
4601647063
}
@@ -46496,11 +47543,11 @@
4649647543
}
4649747544
nCollide = HASHTABLE_NSLOT;
4649847545
for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
4649947546
u32 iFrame = aHash[iKey] + iZero;
4650047547
if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
46501
- assert( iFrame>iRead );
47548
+ /* assert( iFrame>iRead ); -- not true if there is corruption */
4650247549
iRead = iFrame;
4650347550
}
4650447551
if( (nCollide--)==0 ){
4650547552
return SQLITE_CORRUPT_BKPT;
4650647553
}
@@ -46608,10 +47655,11 @@
4660847655
*/
4660947656
SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
4661047657
if( pWal->writeLock ){
4661147658
walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
4661247659
pWal->writeLock = 0;
47660
+ pWal->truncateOnCommit = 0;
4661347661
}
4661447662
return SQLITE_OK;
4661547663
}
4661647664
4661747665
/*
@@ -46703,10 +47751,11 @@
4670347751
walCleanupHash(pWal);
4670447752
}
4670547753
4670647754
return rc;
4670747755
}
47756
+
4670847757
4670947758
/*
4671047759
** This function is called just before writing a set of frames to the log
4671147760
** file (see sqlite3WalFrames()). It checks to see if, instead of appending
4671247761
** to the current log file, it is possible to overwrite the start of the
@@ -46741,27 +47790,10 @@
4674147790
** to handle if this transaction is rolled back.
4674247791
*/
4674347792
int i; /* Loop counter */
4674447793
u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
4674547794
46746
- /* Limit the size of WAL file if the journal_size_limit PRAGMA is
46747
- ** set to a non-negative value. Log errors encountered
46748
- ** during the truncation attempt. */
46749
- if( pWal->mxWalSize>=0 ){
46750
- i64 sz;
46751
- int rx;
46752
- sqlite3BeginBenignMalloc();
46753
- rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46754
- if( rx==SQLITE_OK && (sz > pWal->mxWalSize) ){
46755
- rx = sqlite3OsTruncate(pWal->pWalFd, pWal->mxWalSize);
46756
- }
46757
- sqlite3EndBenignMalloc();
46758
- if( rx ){
46759
- sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46760
- }
46761
- }
46762
-
4676347795
pWal->nCkpt++;
4676447796
pWal->hdr.mxFrame = 0;
4676547797
sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
4676647798
aSalt[1] = salt1;
4676747799
walIndexWriteHdr(pWal);
@@ -46785,10 +47817,78 @@
4678547817
testcase( rc==SQLITE_PROTOCOL );
4678647818
testcase( rc==SQLITE_OK );
4678747819
}
4678847820
return rc;
4678947821
}
47822
+
47823
+/*
47824
+** Information about the current state of the WAL file and where
47825
+** the next fsync should occur - passed from sqlite3WalFrames() into
47826
+** walWriteToLog().
47827
+*/
47828
+typedef struct WalWriter {
47829
+ Wal *pWal; /* The complete WAL information */
47830
+ sqlite3_file *pFd; /* The WAL file to which we write */
47831
+ sqlite3_int64 iSyncPoint; /* Fsync at this offset */
47832
+ int syncFlags; /* Flags for the fsync */
47833
+ int szPage; /* Size of one page */
47834
+} WalWriter;
47835
+
47836
+/*
47837
+** Write iAmt bytes of content into the WAL file beginning at iOffset.
47838
+** Do a sync when crossing the p->iSyncPoint boundary.
47839
+**
47840
+** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
47841
+** first write the part before iSyncPoint, then sync, then write the
47842
+** rest.
47843
+*/
47844
+static int walWriteToLog(
47845
+ WalWriter *p, /* WAL to write to */
47846
+ void *pContent, /* Content to be written */
47847
+ int iAmt, /* Number of bytes to write */
47848
+ sqlite3_int64 iOffset /* Start writing at this offset */
47849
+){
47850
+ int rc;
47851
+ if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
47852
+ int iFirstAmt = (int)(p->iSyncPoint - iOffset);
47853
+ rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
47854
+ if( rc ) return rc;
47855
+ iOffset += iFirstAmt;
47856
+ iAmt -= iFirstAmt;
47857
+ pContent = (void*)(iFirstAmt + (char*)pContent);
47858
+ assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
47859
+ rc = sqlite3OsSync(p->pFd, p->syncFlags);
47860
+ if( iAmt==0 || rc ) return rc;
47861
+ }
47862
+ rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
47863
+ return rc;
47864
+}
47865
+
47866
+/*
47867
+** Write out a single frame of the WAL
47868
+*/
47869
+static int walWriteOneFrame(
47870
+ WalWriter *p, /* Where to write the frame */
47871
+ PgHdr *pPage, /* The page of the frame to be written */
47872
+ int nTruncate, /* The commit flag. Usually 0. >0 for commit */
47873
+ sqlite3_int64 iOffset /* Byte offset at which to write */
47874
+){
47875
+ int rc; /* Result code from subfunctions */
47876
+ void *pData; /* Data actually written */
47877
+ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
47878
+#if defined(SQLITE_HAS_CODEC)
47879
+ if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
47880
+#else
47881
+ pData = pPage->pData;
47882
+#endif
47883
+ walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
47884
+ rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
47885
+ if( rc ) return rc;
47886
+ /* Write the page data */
47887
+ rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
47888
+ return rc;
47889
+}
4679047890
4679147891
/*
4679247892
** Write a set of frames to the log. The caller must hold the write-lock
4679347893
** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
4679447894
*/
@@ -46800,17 +47900,23 @@
4680047900
int isCommit, /* True if this is a commit */
4680147901
int sync_flags /* Flags to pass to OsSync() (or 0) */
4680247902
){
4680347903
int rc; /* Used to catch return codes */
4680447904
u32 iFrame; /* Next frame address */
46805
- u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
4680647905
PgHdr *p; /* Iterator to run through pList with. */
4680747906
PgHdr *pLast = 0; /* Last frame in list */
46808
- int nLast = 0; /* Number of extra copies of last page */
47907
+ int nExtra = 0; /* Number of extra copies of last page */
47908
+ int szFrame; /* The size of a single frame */
47909
+ i64 iOffset; /* Next byte to write in WAL file */
47910
+ WalWriter w; /* The writer */
4680947911
4681047912
assert( pList );
4681147913
assert( pWal->writeLock );
47914
+
47915
+ /* If this frame set completes a transaction, then nTruncate>0. If
47916
+ ** nTruncate==0 then this frame set does not complete the transaction. */
47917
+ assert( (isCommit!=0)==(nTruncate!=0) );
4681247918
4681347919
#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
4681447920
{ int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
4681547921
WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
4681647922
pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
@@ -46835,91 +47941,103 @@
4683547941
4683647942
sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
4683747943
sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
4683847944
sqlite3Put4byte(&aWalHdr[8], szPage);
4683947945
sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
46840
- sqlite3_randomness(8, pWal->hdr.aSalt);
47946
+ if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
4684147947
memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
4684247948
walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
4684347949
sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
4684447950
sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
4684547951
4684647952
pWal->szPage = szPage;
4684747953
pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
4684847954
pWal->hdr.aFrameCksum[0] = aCksum[0];
4684947955
pWal->hdr.aFrameCksum[1] = aCksum[1];
47956
+ pWal->truncateOnCommit = 1;
4685047957
4685147958
rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
4685247959
WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
4685347960
if( rc!=SQLITE_OK ){
4685447961
return rc;
4685547962
}
47963
+
47964
+ /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
47965
+ ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
47966
+ ** an out-of-order write following a WAL restart could result in
47967
+ ** database corruption. See the ticket:
47968
+ **
47969
+ ** http://localhost:591/sqlite/info/ff5be73dee
47970
+ */
47971
+ if( pWal->syncHeader && sync_flags ){
47972
+ rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
47973
+ if( rc ) return rc;
47974
+ }
4685647975
}
4685747976
assert( (int)pWal->szPage==szPage );
4685847977
46859
- /* Write the log file. */
47978
+ /* Setup information needed to write frames into the WAL */
47979
+ w.pWal = pWal;
47980
+ w.pFd = pWal->pWalFd;
47981
+ w.iSyncPoint = 0;
47982
+ w.syncFlags = sync_flags;
47983
+ w.szPage = szPage;
47984
+ iOffset = walFrameOffset(iFrame+1, szPage);
47985
+ szFrame = szPage + WAL_FRAME_HDRSIZE;
47986
+
47987
+ /* Write all frames into the log file exactly once */
4686047988
for(p=pList; p; p=p->pDirty){
46861
- u32 nDbsize; /* Db-size field for frame header */
46862
- i64 iOffset; /* Write offset in log file */
46863
- void *pData;
46864
-
46865
- iOffset = walFrameOffset(++iFrame, szPage);
46866
- /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46867
-
46868
- /* Populate and write the frame header */
46869
- nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
46870
-#if defined(SQLITE_HAS_CODEC)
46871
- if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
46872
-#else
46873
- pData = p->pData;
46874
-#endif
46875
- walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
46876
- rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46877
- if( rc!=SQLITE_OK ){
46878
- return rc;
46879
- }
46880
-
46881
- /* Write the page data */
46882
- rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
46883
- if( rc!=SQLITE_OK ){
46884
- return rc;
46885
- }
47989
+ int nDbSize; /* 0 normally. Positive == commit flag */
47990
+ iFrame++;
47991
+ assert( iOffset==walFrameOffset(iFrame, szPage) );
47992
+ nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
47993
+ rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
47994
+ if( rc ) return rc;
4688647995
pLast = p;
46887
- }
46888
-
46889
- /* Sync the log file if the 'isSync' flag was specified. */
46890
- if( sync_flags ){
46891
- i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
46892
- i64 iOffset = walFrameOffset(iFrame+1, szPage);
46893
-
46894
- assert( isCommit );
46895
- assert( iSegment>0 );
46896
-
46897
- iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
46898
- while( iOffset<iSegment ){
46899
- void *pData;
46900
-#if defined(SQLITE_HAS_CODEC)
46901
- if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
46902
-#else
46903
- pData = pLast->pData;
46904
-#endif
46905
- walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
46906
- /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46907
- rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46908
- if( rc!=SQLITE_OK ){
46909
- return rc;
46910
- }
46911
- iOffset += WAL_FRAME_HDRSIZE;
46912
- rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
46913
- if( rc!=SQLITE_OK ){
46914
- return rc;
46915
- }
46916
- nLast++;
46917
- iOffset += szPage;
46918
- }
46919
-
46920
- rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
47996
+ iOffset += szFrame;
47997
+ }
47998
+
47999
+ /* If this is the end of a transaction, then we might need to pad
48000
+ ** the transaction and/or sync the WAL file.
48001
+ **
48002
+ ** Padding and syncing only occur if this set of frames complete a
48003
+ ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
48004
+ ** or synchonous==OFF, then no padding or syncing are needed.
48005
+ **
48006
+ ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
48007
+ ** needed and only the sync is done. If padding is needed, then the
48008
+ ** final frame is repeated (with its commit mark) until the next sector
48009
+ ** boundary is crossed. Only the part of the WAL prior to the last
48010
+ ** sector boundary is synced; the part of the last frame that extends
48011
+ ** past the sector boundary is written after the sync.
48012
+ */
48013
+ if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
48014
+ if( pWal->padToSectorBoundary ){
48015
+ int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
48016
+ w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
48017
+ while( iOffset<w.iSyncPoint ){
48018
+ rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
48019
+ if( rc ) return rc;
48020
+ iOffset += szFrame;
48021
+ nExtra++;
48022
+ }
48023
+ }else{
48024
+ rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
48025
+ }
48026
+ }
48027
+
48028
+ /* If this frame set completes the first transaction in the WAL and
48029
+ ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
48030
+ ** journal size limit, if possible.
48031
+ */
48032
+ if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
48033
+ i64 sz = pWal->mxWalSize;
48034
+ if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
48035
+ sz = walFrameOffset(iFrame+nExtra+1, szPage);
48036
+ }
48037
+ walLimitSize(pWal, sz);
48038
+ pWal->truncateOnCommit = 0;
4692148039
}
4692248040
4692348041
/* Append data to the wal-index. It is not necessary to lock the
4692448042
** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
4692548043
** guarantees that there are no other writers, and no data that may
@@ -46928,13 +48046,13 @@
4692848046
iFrame = pWal->hdr.mxFrame;
4692948047
for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
4693048048
iFrame++;
4693148049
rc = walIndexAppend(pWal, iFrame, p->pgno);
4693248050
}
46933
- while( nLast>0 && rc==SQLITE_OK ){
48051
+ while( rc==SQLITE_OK && nExtra>0 ){
4693448052
iFrame++;
46935
- nLast--;
48053
+ nExtra--;
4693648054
rc = walIndexAppend(pWal, iFrame, pLast->pgno);
4693748055
}
4693848056
4693948057
if( rc==SQLITE_OK ){
4694048058
/* Update the private copy of the header. */
@@ -47436,10 +48554,11 @@
4743648554
u8 intKey; /* True if intkey flag is set */
4743748555
u8 leaf; /* True if leaf flag is set */
4743848556
u8 hasData; /* True if this page stores data */
4743948557
u8 hdrOffset; /* 100 for page 1. 0 otherwise */
4744048558
u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
48559
+ u8 max1bytePayload; /* min(maxLocal,127) */
4744148560
u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
4744248561
u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
4744348562
u16 cellOffset; /* Index in aData of first cell pointer */
4744448563
u16 nFree; /* Number of free bytes on the page */
4744548564
u16 nCell; /* Number of cells on this page, local and ovfl */
@@ -47448,10 +48567,12 @@
4744848567
u8 *pCell; /* Pointers to the body of the overflow cell */
4744948568
u16 idx; /* Insert this cell before idx-th non-overflow cell */
4745048569
} aOvfl[5];
4745148570
BtShared *pBt; /* Pointer to BtShared that this page is part of */
4745248571
u8 *aData; /* Pointer to disk image of the page data */
48572
+ u8 *aDataEnd; /* One byte past the end of usable data */
48573
+ u8 *aCellIdx; /* The cell index area */
4745348574
DbPage *pDbPage; /* Pager page handle */
4745448575
Pgno pgno; /* Page number for this page */
4745548576
};
4745648577
4745748578
/*
@@ -47527,11 +48648,11 @@
4752748648
#define TRANS_WRITE 2
4752848649
4752948650
/*
4753048651
** An instance of this object represents a single database file.
4753148652
**
47532
-** A single database file can be in use as the same time by two
48653
+** A single database file can be in use at the same time by two
4753348654
** or more database connections. When two or more connections are
4753448655
** sharing the same database file, each connection has it own
4753548656
** private Btree object for the file and each of those Btrees points
4753648657
** to this one BtShared object. BtShared.nRef is the number of
4753748658
** connections currently sharing this database file.
@@ -47564,21 +48685,18 @@
4756448685
struct BtShared {
4756548686
Pager *pPager; /* The page cache */
4756648687
sqlite3 *db; /* Database connection currently using this Btree */
4756748688
BtCursor *pCursor; /* A list of all open cursors */
4756848689
MemPage *pPage1; /* First page of the database */
47569
- u8 readOnly; /* True if the underlying file is readonly */
47570
- u8 pageSizeFixed; /* True if the page size can no longer be changed */
47571
- u8 secureDelete; /* True if secure_delete is enabled */
47572
- u8 initiallyEmpty; /* Database is empty at start of transaction */
4757348690
u8 openFlags; /* Flags to sqlite3BtreeOpen() */
4757448691
#ifndef SQLITE_OMIT_AUTOVACUUM
4757548692
u8 autoVacuum; /* True if auto-vacuum is enabled */
4757648693
u8 incrVacuum; /* True if incr-vacuum is enabled */
4757748694
#endif
4757848695
u8 inTransaction; /* Transaction state */
47579
- u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
48696
+ u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
48697
+ u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
4758048698
u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
4758148699
u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
4758248700
u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
4758348701
u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
4758448702
u32 pageSize; /* Total number of bytes on a page */
@@ -47592,16 +48710,25 @@
4759248710
#ifndef SQLITE_OMIT_SHARED_CACHE
4759348711
int nRef; /* Number of references to this structure */
4759448712
BtShared *pNext; /* Next on a list of sharable BtShared structs */
4759548713
BtLock *pLock; /* List of locks held on this shared-btree struct */
4759648714
Btree *pWriter; /* Btree with currently open write transaction */
47597
- u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */
47598
- u8 isPending; /* If waiting for read-locks to clear */
4759948715
#endif
4760048716
u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
4760148717
};
4760248718
48719
+/*
48720
+** Allowed values for BtShared.btsFlags
48721
+*/
48722
+#define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
48723
+#define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
48724
+#define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
48725
+#define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */
48726
+#define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */
48727
+#define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */
48728
+#define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */
48729
+
4760348730
/*
4760448731
** An instance of the following structure is used to hold information
4760548732
** about a cell. The parseCellPtr() function fills in this structure
4760648733
** based on information extract from the raw disk page.
4760748734
*/
@@ -47633,11 +48760,11 @@
4763348760
** b-tree within a database file.
4763448761
**
4763548762
** The entry is identified by its MemPage and the index in
4763648763
** MemPage.aCell[] of the entry.
4763748764
**
47638
-** A single database file can shared by two more database connections,
48765
+** A single database file can be shared by two more database connections,
4763948766
** but cursors cannot be shared. Each cursor is associated with a
4764048767
** particular database connection identified BtCursor.pBtree.db.
4764148768
**
4764248769
** Fields in this structure are accessed under the BtShared.mutex
4764348770
** found at self->pBt->mutex.
@@ -47794,11 +48921,11 @@
4779448921
int mallocFailed; /* A memory allocation error has occurred */
4779548922
StrAccum errMsg; /* Accumulate the error message text here */
4779648923
};
4779748924
4779848925
/*
47799
-** Read or write a two- and four-byte big-endian integer values.
48926
+** Routines to read or write a two- and four-byte big-endian integer values.
4780048927
*/
4780148928
#define get2byte(x) ((x)[0]<<8 | (x)[1])
4780248929
#define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
4780348930
#define get4byte sqlite3Get4byte
4780448931
#define put4byte sqlite3Put4byte
@@ -48319,11 +49446,11 @@
4831949446
}
4832049447
4832149448
/* If some other connection is holding an exclusive lock, the
4832249449
** requested lock may not be obtained.
4832349450
*/
48324
- if( pBt->pWriter!=p && pBt->isExclusive ){
49451
+ if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
4832549452
sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
4832649453
return SQLITE_LOCKED_SHAREDCACHE;
4832749454
}
4832849455
4832949456
for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
@@ -48340,11 +49467,11 @@
4834049467
assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
4834149468
if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
4834249469
sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
4834349470
if( eLock==WRITE_LOCK ){
4834449471
assert( p==pBt->pWriter );
48345
- pBt->isPending = 1;
49472
+ pBt->btsFlags |= BTS_PENDING;
4834649473
}
4834749474
return SQLITE_LOCKED_SHAREDCACHE;
4834849475
}
4834949476
}
4835049477
return SQLITE_OK;
@@ -48428,11 +49555,11 @@
4842849555
/*
4842949556
** Release all the table locks (locks obtained via calls to
4843049557
** the setSharedCacheTableLock() procedure) held by Btree object p.
4843149558
**
4843249559
** This function assumes that Btree p has an open read or write
48433
-** transaction. If it does not, then the BtShared.isPending variable
49560
+** transaction. If it does not, then the BTS_PENDING flag
4843449561
** may be incorrectly cleared.
4843549562
*/
4843649563
static void clearAllSharedCacheTableLocks(Btree *p){
4843749564
BtShared *pBt = p->pBt;
4843849565
BtLock **ppIter = &pBt->pLock;
@@ -48441,11 +49568,11 @@
4844149568
assert( p->sharable || 0==*ppIter );
4844249569
assert( p->inTrans>0 );
4844349570
4844449571
while( *ppIter ){
4844549572
BtLock *pLock = *ppIter;
48446
- assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
49573
+ assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
4844749574
assert( pLock->pBtree->inTrans>=pLock->eLock );
4844849575
if( pLock->pBtree==p ){
4844949576
*ppIter = pLock->pNext;
4845049577
assert( pLock->iTable!=1 || pLock==&p->lock );
4845149578
if( pLock->iTable!=1 ){
@@ -48454,26 +49581,25 @@
4845449581
}else{
4845549582
ppIter = &pLock->pNext;
4845649583
}
4845749584
}
4845849585
48459
- assert( pBt->isPending==0 || pBt->pWriter );
49586
+ assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
4846049587
if( pBt->pWriter==p ){
4846149588
pBt->pWriter = 0;
48462
- pBt->isExclusive = 0;
48463
- pBt->isPending = 0;
49589
+ pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
4846449590
}else if( pBt->nTransaction==2 ){
4846549591
/* This function is called when Btree p is concluding its
4846649592
** transaction. If there currently exists a writer, and p is not
4846749593
** that writer, then the number of locks held by connections other
4846849594
** than the writer must be about to drop to zero. In this case
48469
- ** set the isPending flag to 0.
49595
+ ** set the BTS_PENDING flag to 0.
4847049596
**
48471
- ** If there is not currently a writer, then BtShared.isPending must
49597
+ ** If there is not currently a writer, then BTS_PENDING must
4847249598
** be zero already. So this next line is harmless in that case.
4847349599
*/
48474
- pBt->isPending = 0;
49600
+ pBt->btsFlags &= ~BTS_PENDING;
4847549601
}
4847649602
}
4847749603
4847849604
/*
4847949605
** This function changes all write-locks held by Btree p into read-locks.
@@ -48481,12 +49607,11 @@
4848149607
static void downgradeAllSharedCacheTableLocks(Btree *p){
4848249608
BtShared *pBt = p->pBt;
4848349609
if( pBt->pWriter==p ){
4848449610
BtLock *pLock;
4848549611
pBt->pWriter = 0;
48486
- pBt->isExclusive = 0;
48487
- pBt->isPending = 0;
49612
+ pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
4848849613
for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
4848949614
assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
4849049615
pLock->eLock = READ_LOCK;
4849149616
}
4849249617
}
@@ -48935,11 +50060,11 @@
4893550060
** to the cell content.
4893650061
**
4893750062
** This routine works only for pages that do not contain overflow cells.
4893850063
*/
4893950064
#define findCell(P,I) \
48940
- ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
50065
+ ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
4894150066
#define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
4894250067
4894350068
4894450069
/*
4894550070
** This a more complex version of findCell() that works for
@@ -49340,11 +50465,11 @@
4934050465
assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
4934150466
assert( (start + size) <= (int)pPage->pBt->usableSize );
4934250467
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
4934350468
assert( size>=0 ); /* Minimum cell size is 4 */
4934450469
49345
- if( pPage->pBt->secureDelete ){
50470
+ if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
4934650471
/* Overwrite deleted information with zeros when the secure_delete
4934750472
** option is enabled */
4934850473
memset(&data[start], 0, size);
4934950474
}
4935050475
@@ -49443,10 +50568,11 @@
4944350568
pPage->maxLocal = pBt->maxLocal;
4944450569
pPage->minLocal = pBt->minLocal;
4944550570
}else{
4944650571
return SQLITE_CORRUPT_BKPT;
4944750572
}
50573
+ pPage->max1bytePayload = pBt->max1bytePayload;
4944850574
return SQLITE_OK;
4944950575
}
4945050576
4945150577
/*
4945250578
** Initialize the auxiliary information for a disk block.
@@ -49485,10 +50611,12 @@
4948550611
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
4948650612
pPage->maskPage = (u16)(pBt->pageSize - 1);
4948750613
pPage->nOverflow = 0;
4948850614
usableSize = pBt->usableSize;
4948950615
pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
50616
+ pPage->aDataEnd = &data[usableSize];
50617
+ pPage->aCellIdx = &data[cellOffset];
4949050618
top = get2byteNotZero(&data[hdr+5]);
4949150619
pPage->nCell = get2byte(&data[hdr+3]);
4949250620
if( pPage->nCell>MX_CELL(pBt) ){
4949350621
/* To many cells for a single page. The page must be corrupt */
4949450622
return SQLITE_CORRUPT_BKPT;
@@ -49576,11 +50704,11 @@
4957650704
assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
4957750705
assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
4957850706
assert( sqlite3PagerGetData(pPage->pDbPage) == data );
4957950707
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
4958050708
assert( sqlite3_mutex_held(pBt->mutex) );
49581
- if( pBt->secureDelete ){
50709
+ if( pBt->btsFlags & BTS_SECURE_DELETE ){
4958250710
memset(&data[hdr], 0, pBt->usableSize - hdr);
4958350711
}
4958450712
data[hdr] = (char)flags;
4958550713
first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
4958650714
memset(&data[hdr+1], 0, 4);
@@ -49588,10 +50716,12 @@
4958850716
put2byte(&data[hdr+5], pBt->usableSize);
4958950717
pPage->nFree = (u16)(pBt->usableSize - first);
4959050718
decodeFlags(pPage, flags);
4959150719
pPage->hdrOffset = hdr;
4959250720
pPage->cellOffset = first;
50721
+ pPage->aDataEnd = &data[pBt->usableSize];
50722
+ pPage->aCellIdx = &data[first];
4959350723
pPage->nOverflow = 0;
4959450724
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
4959550725
pPage->maskPage = (u16)(pBt->pageSize - 1);
4959650726
pPage->nCell = 0;
4959750727
pPage->isInit = 1;
@@ -49848,11 +50978,16 @@
4984850978
p->sharable = 1;
4984950979
if( !zFullPathname ){
4985050980
sqlite3_free(p);
4985150981
return SQLITE_NOMEM;
4985250982
}
49853
- sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
50983
+ rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
50984
+ if( rc ){
50985
+ sqlite3_free(zFullPathname);
50986
+ sqlite3_free(p);
50987
+ return rc;
50988
+ }
4985450989
#if SQLITE_THREADSAFE
4985550990
mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
4985650991
sqlite3_mutex_enter(mutexOpen);
4985750992
mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
4985850993
sqlite3_mutex_enter(mutexShared);
@@ -49922,13 +51057,13 @@
4992251057
sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
4992351058
p->pBt = pBt;
4992451059
4992551060
pBt->pCursor = 0;
4992651061
pBt->pPage1 = 0;
49927
- pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
51062
+ if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
4992851063
#ifdef SQLITE_SECURE_DELETE
49929
- pBt->secureDelete = 1;
51064
+ pBt->btsFlags |= BTS_SECURE_DELETE;
4993051065
#endif
4993151066
pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
4993251067
if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
4993351068
|| ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
4993451069
pBt->pageSize = 0;
@@ -49945,11 +51080,11 @@
4994551080
}
4994651081
#endif
4994751082
nReserve = 0;
4994851083
}else{
4994951084
nReserve = zDbHeader[20];
49950
- pBt->pageSizeFixed = 1;
51085
+ pBt->btsFlags |= BTS_PAGESIZE_FIXED;
4995151086
#ifndef SQLITE_OMIT_AUTOVACUUM
4995251087
pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
4995351088
pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
4995451089
#endif
4995551090
}
@@ -50233,19 +51368,19 @@
5023351368
** at the beginning of a page.
5023451369
**
5023551370
** If parameter nReserve is less than zero, then the number of reserved
5023651371
** bytes per page is left unchanged.
5023751372
**
50238
-** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
51373
+** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
5023951374
** and autovacuum mode can no longer be changed.
5024051375
*/
5024151376
SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
5024251377
int rc = SQLITE_OK;
5024351378
BtShared *pBt = p->pBt;
5024451379
assert( nReserve>=-1 && nReserve<=255 );
5024551380
sqlite3BtreeEnter(p);
50246
- if( pBt->pageSizeFixed ){
51381
+ if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
5024751382
sqlite3BtreeLeave(p);
5024851383
return SQLITE_READONLY;
5024951384
}
5025051385
if( nReserve<0 ){
5025151386
nReserve = pBt->pageSize - pBt->usableSize;
@@ -50258,11 +51393,11 @@
5025851393
pBt->pageSize = (u32)pageSize;
5025951394
freeTempSpace(pBt);
5026051395
}
5026151396
rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
5026251397
pBt->usableSize = pBt->pageSize - (u16)nReserve;
50263
- if( iFix ) pBt->pageSizeFixed = 1;
51398
+ if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
5026451399
sqlite3BtreeLeave(p);
5026551400
return rc;
5026651401
}
5026751402
5026851403
/*
@@ -50298,22 +51433,23 @@
5029851433
sqlite3BtreeLeave(p);
5029951434
return n;
5030051435
}
5030151436
5030251437
/*
50303
-** Set the secureDelete flag if newFlag is 0 or 1. If newFlag is -1,
50304
-** then make no changes. Always return the value of the secureDelete
51438
+** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1,
51439
+** then make no changes. Always return the value of the BTS_SECURE_DELETE
5030551440
** setting after the change.
5030651441
*/
5030751442
SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
5030851443
int b;
5030951444
if( p==0 ) return 0;
5031051445
sqlite3BtreeEnter(p);
5031151446
if( newFlag>=0 ){
50312
- p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
51447
+ p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
51448
+ if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
5031351449
}
50314
- b = p->pBt->secureDelete;
51450
+ b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
5031551451
sqlite3BtreeLeave(p);
5031651452
return b;
5031751453
}
5031851454
#endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
5031951455
@@ -50330,11 +51466,11 @@
5033051466
BtShared *pBt = p->pBt;
5033151467
int rc = SQLITE_OK;
5033251468
u8 av = (u8)autoVacuum;
5033351469
5033451470
sqlite3BtreeEnter(p);
50335
- if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
51471
+ if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
5033651472
rc = SQLITE_READONLY;
5033751473
}else{
5033851474
pBt->autoVacuum = av ?1:0;
5033951475
pBt->incrVacuum = av==2 ?1:0;
5034051476
}
@@ -50404,18 +51540,18 @@
5040451540
goto page1_init_failed;
5040551541
}
5040651542
5040751543
#ifdef SQLITE_OMIT_WAL
5040851544
if( page1[18]>1 ){
50409
- pBt->readOnly = 1;
51545
+ pBt->btsFlags |= BTS_READ_ONLY;
5041051546
}
5041151547
if( page1[19]>1 ){
5041251548
goto page1_init_failed;
5041351549
}
5041451550
#else
5041551551
if( page1[18]>2 ){
50416
- pBt->readOnly = 1;
51552
+ pBt->btsFlags |= BTS_READ_ONLY;
5041751553
}
5041851554
if( page1[19]>2 ){
5041951555
goto page1_init_failed;
5042051556
}
5042151557
@@ -50425,11 +51561,11 @@
5042551561
** The caller detects this and calls this function again. This is
5042651562
** required as the version of page 1 currently in the page1 buffer
5042751563
** may not be the latest version - there may be a newer one in the log
5042851564
** file.
5042951565
*/
50430
- if( page1[19]==2 && pBt->doNotUseWAL==0 ){
51566
+ if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
5043151567
int isOpen = 0;
5043251568
rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
5043351569
if( rc!=SQLITE_OK ){
5043451570
goto page1_init_failed;
5043551571
}else if( isOpen==0 ){
@@ -50502,10 +51638,15 @@
5050251638
*/
5050351639
pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
5050451640
pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
5050551641
pBt->maxLeaf = (u16)(pBt->usableSize - 35);
5050651642
pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
51643
+ if( pBt->maxLocal>127 ){
51644
+ pBt->max1bytePayload = 127;
51645
+ }else{
51646
+ pBt->max1bytePayload = (u8)pBt->maxLocal;
51647
+ }
5050751648
assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
5050851649
pBt->pPage1 = pPage1;
5050951650
pBt->nPage = nPage;
5051051651
return SQLITE_OK;
5051151652
@@ -50565,11 +51706,11 @@
5056551706
data[21] = 64;
5056651707
data[22] = 32;
5056751708
data[23] = 32;
5056851709
memset(&data[24], 0, 100-24);
5056951710
zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
50570
- pBt->pageSizeFixed = 1;
51711
+ pBt->btsFlags |= BTS_PAGESIZE_FIXED;
5057151712
#ifndef SQLITE_OMIT_AUTOVACUUM
5057251713
assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
5057351714
assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
5057451715
put4byte(&data[36 + 4*4], pBt->autoVacuum);
5057551716
put4byte(&data[36 + 7*4], pBt->incrVacuum);
@@ -50629,21 +51770,23 @@
5062951770
if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
5063051771
goto trans_begun;
5063151772
}
5063251773
5063351774
/* Write transactions are not possible on a read-only database */
50634
- if( pBt->readOnly && wrflag ){
51775
+ if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
5063551776
rc = SQLITE_READONLY;
5063651777
goto trans_begun;
5063751778
}
5063851779
5063951780
#ifndef SQLITE_OMIT_SHARED_CACHE
5064051781
/* If another database handle has already opened a write transaction
5064151782
** on this shared-btree structure and a second write transaction is
5064251783
** requested, return SQLITE_LOCKED.
5064351784
*/
50644
- if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
51785
+ if( (wrflag && pBt->inTransaction==TRANS_WRITE)
51786
+ || (pBt->btsFlags & BTS_PENDING)!=0
51787
+ ){
5064551788
pBlock = pBt->pWriter->db;
5064651789
}else if( wrflag>1 ){
5064751790
BtLock *pIter;
5064851791
for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
5064951792
if( pIter->pBtree!=p ){
@@ -50663,11 +51806,12 @@
5066351806
** page 1. So if some other shared-cache client already has a write-lock
5066451807
** on page 1, the transaction cannot be opened. */
5066551808
rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
5066651809
if( SQLITE_OK!=rc ) goto trans_begun;
5066751810
50668
- pBt->initiallyEmpty = (u8)(pBt->nPage==0);
51811
+ pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
51812
+ if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
5066951813
do {
5067051814
/* Call lockBtree() until either pBt->pPage1 is populated or
5067151815
** lockBtree() returns something other than SQLITE_OK. lockBtree()
5067251816
** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
5067351817
** reading page 1 it discovers that the page-size of the database
@@ -50675,11 +51819,11 @@
5067551819
** pBt->pageSize to the page-size of the file on disk.
5067651820
*/
5067751821
while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
5067851822
5067951823
if( rc==SQLITE_OK && wrflag ){
50680
- if( pBt->readOnly ){
51824
+ if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
5068151825
rc = SQLITE_READONLY;
5068251826
}else{
5068351827
rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
5068451828
if( rc==SQLITE_OK ){
5068551829
rc = newDatabase(pBt);
@@ -50712,11 +51856,12 @@
5071251856
if( wrflag ){
5071351857
MemPage *pPage1 = pBt->pPage1;
5071451858
#ifndef SQLITE_OMIT_SHARED_CACHE
5071551859
assert( !pBt->pWriter );
5071651860
pBt->pWriter = p;
50717
- pBt->isExclusive = (u8)(wrflag>1);
51861
+ pBt->btsFlags &= ~BTS_EXCLUSIVE;
51862
+ if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
5071851863
#endif
5071951864
5072051865
/* If the db-size header field is incorrect (as it may be if an old
5072151866
** client has been writing the database file), update it now. Doing
5072251867
** this sooner rather than later means the database size can safely
@@ -51441,11 +52586,11 @@
5144152586
SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
5144252587
int rc;
5144352588
BtShared *pBt = p->pBt;
5144452589
sqlite3BtreeEnter(p);
5144552590
assert( p->inTrans==TRANS_WRITE );
51446
- assert( pBt->readOnly==0 );
52591
+ assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
5144752592
assert( iStatement>0 );
5144852593
assert( iStatement>p->db->nSavepoint );
5144952594
assert( pBt->inTransaction==TRANS_WRITE );
5145052595
/* At the pager level, a statement transaction is a savepoint with
5145152596
** an index greater than all savepoints created explicitly using
@@ -51476,11 +52621,13 @@
5147652621
assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
5147752622
assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
5147852623
sqlite3BtreeEnter(p);
5147952624
rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
5148052625
if( rc==SQLITE_OK ){
51481
- if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
52626
+ if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
52627
+ pBt->nPage = 0;
52628
+ }
5148252629
rc = newDatabase(pBt);
5148352630
pBt->nPage = get4byte(28 + pBt->pPage1->aData);
5148452631
5148552632
/* The database size was written into the offset 28 of the header
5148652633
** when the transaction started, so we know that the value at offset
@@ -51546,11 +52693,11 @@
5154652693
/* Assert that the caller has opened the required transaction. */
5154752694
assert( p->inTrans>TRANS_NONE );
5154852695
assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
5154952696
assert( pBt->pPage1 && pBt->pPage1->aData );
5155052697
51551
- if( NEVER(wrFlag && pBt->readOnly) ){
52698
+ if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
5155252699
return SQLITE_READONLY;
5155352700
}
5155452701
if( iTable==1 && btreePagecount(pBt)==0 ){
5155552702
assert( wrFlag==0 );
5155652703
iTable = 0;
@@ -52046,11 +53193,11 @@
5204653193
&& pBt->pPage1->aData[19]==0x01 /* (5) */
5204753194
){
5204853195
u8 aSave[4];
5204953196
u8 *aWrite = &pBuf[-4];
5205053197
memcpy(aSave, aWrite, 4);
52051
- rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
53198
+ rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
5205253199
nextPage = get4byte(aWrite);
5205353200
memcpy(aWrite, aSave, 4);
5205453201
}else
5205553202
#endif
5205653203
@@ -52626,20 +53773,25 @@
5262653773
** the entire cell by checking for the cases where the record is
5262753774
** stored entirely within the b-tree page by inspecting the first
5262853775
** 2 bytes of the cell.
5262953776
*/
5263053777
int nCell = pCell[0];
52631
- if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
53778
+ if( nCell<=pPage->max1bytePayload
53779
+ /* && (pCell+nCell)<pPage->aDataEnd */
53780
+ ){
5263253781
/* This branch runs if the record-size field of the cell is a
5263353782
** single byte varint and the record fits entirely on the main
5263453783
** b-tree page. */
53784
+ testcase( pCell+nCell+1==pPage->aDataEnd );
5263553785
c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
5263653786
}else if( !(pCell[1] & 0x80)
5263753787
&& (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
53788
+ /* && (pCell+nCell+2)<=pPage->aDataEnd */
5263853789
){
5263953790
/* The record-size field is a 2 byte varint and the record
5264053791
** fits entirely on the main b-tree page. */
53792
+ testcase( pCell+nCell+2==pPage->aDataEnd );
5264153793
c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
5264253794
}else{
5264353795
/* The record flows over onto one or more overflow pages. In
5264453796
** this case the whole cell needs to be parsed, a buffer allocated
5264553797
** and accessPayload() used to retrieve the record into the
@@ -53177,11 +54329,11 @@
5317754329
rc = sqlite3PagerWrite(pPage1->pDbPage);
5317854330
if( rc ) goto freepage_out;
5317954331
nFree = get4byte(&pPage1->aData[36]);
5318054332
put4byte(&pPage1->aData[36], nFree+1);
5318154333
53182
- if( pBt->secureDelete ){
54334
+ if( pBt->btsFlags & BTS_SECURE_DELETE ){
5318354335
/* If the secure_delete option is enabled, then
5318454336
** always fully overwrite deleted information with zeros.
5318554337
*/
5318654338
if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
5318754339
|| ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
@@ -53238,11 +54390,11 @@
5323854390
*/
5323954391
rc = sqlite3PagerWrite(pTrunk->pDbPage);
5324054392
if( rc==SQLITE_OK ){
5324154393
put4byte(&pTrunk->aData[4], nLeaf+1);
5324254394
put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
53243
- if( pPage && !pBt->secureDelete ){
54395
+ if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
5324454396
sqlite3PagerDontWrite(pPage->pDbPage);
5324554397
}
5324654398
rc = btreeSetHasContent(pBt, iPage);
5324754399
}
5324854400
TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
@@ -53530,11 +54682,11 @@
5353054682
assert( idx>=0 && idx<pPage->nCell );
5353154683
assert( sz==cellSize(pPage, idx) );
5353254684
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
5353354685
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5353454686
data = pPage->aData;
53535
- ptr = &data[pPage->cellOffset + 2*idx];
54687
+ ptr = &pPage->aCellIdx[2*idx];
5353654688
pc = get2byte(ptr);
5353754689
hdr = pPage->hdrOffset;
5353854690
testcase( pc==get2byte(&data[hdr+5]) );
5353954691
testcase( pc+sz==pPage->pBt->usableSize );
5354054692
if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
@@ -53544,11 +54696,11 @@
5354454696
rc = freeSpace(pPage, pc, sz);
5354554697
if( rc ){
5354654698
*pRC = rc;
5354754699
return;
5354854700
}
53549
- endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
54701
+ endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
5355054702
assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 ); /* ptr is always 2-byte aligned */
5355154703
while( ptr<endPtr ){
5355254704
*(u16*)ptr = *(u16*)&ptr[2];
5355354705
ptr += 2;
5355454706
}
@@ -53686,11 +54838,11 @@
5368654838
5368754839
/* Check that the page has just been zeroed by zeroPage() */
5368854840
assert( pPage->nCell==0 );
5368954841
assert( get2byteNotZero(&data[hdr+5])==nUsable );
5369054842
53691
- pCellptr = &data[pPage->cellOffset + nCell*2];
54843
+ pCellptr = &pPage->aCellIdx[nCell*2];
5369254844
cellbody = nUsable;
5369354845
for(i=nCell-1; i>=0; i--){
5369454846
u16 sz = aSize[i];
5369554847
pCellptr -= 2;
5369654848
cellbody -= sz;
@@ -54079,11 +55231,11 @@
5407955231
** But not if we are in secure-delete mode. In secure-delete mode,
5408055232
** the dropCell() routine will overwrite the entire cell with zeroes.
5408155233
** In this case, temporarily copy the cell into the aOvflSpace[]
5408255234
** buffer. It will be copied out again as soon as the aSpace[] buffer
5408355235
** is allocated. */
54084
- if( pBt->secureDelete ){
55236
+ if( pBt->btsFlags & BTS_SECURE_DELETE ){
5408555237
int iOff;
5408655238
5408755239
iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
5408855240
if( (iOff+szNew[i])>(int)pBt->usableSize ){
5408955241
rc = SQLITE_CORRUPT_BKPT;
@@ -54264,12 +55416,18 @@
5426455416
}
5426555417
5426655418
/* Either we found one or more cells (cntnew[0])>0) or pPage is
5426755419
** a virtual root page. A virtual root page is when the real root
5426855420
** page is page 1 and we are the only child of that page.
55421
+ **
55422
+ ** UPDATE: The assert() below is not necessarily true if the database
55423
+ ** file is corrupt. The corruption will be detected and reported later
55424
+ ** in this procedure so there is no need to act upon it now.
5426955425
*/
55426
+#if 0
5427055427
assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
55428
+#endif
5427155429
5427255430
TRACE(("BALANCE: old: %d %d %d ",
5427355431
apOld[0]->pgno,
5427455432
nOld>=2 ? apOld[1]->pgno : 0,
5427555433
nOld>=3 ? apOld[2]->pgno : 0
@@ -54815,11 +55973,12 @@
5481555973
assert( pCur->skipNext!=SQLITE_OK );
5481655974
return pCur->skipNext;
5481755975
}
5481855976
5481955977
assert( cursorHoldsMutex(pCur) );
54820
- assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
55978
+ assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
55979
+ && (pBt->btsFlags & BTS_READ_ONLY)==0 );
5482155980
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
5482255981
5482355982
/* Assert that the caller has been consistent. If this cursor was opened
5482455983
** expecting an index b-tree, then the caller should be inserting blob
5482555984
** keys with no associated data. If the cursor was opened expecting an
@@ -54944,11 +56103,11 @@
5494456103
int iCellIdx; /* Index of cell to delete */
5494556104
int iCellDepth; /* Depth of node containing pCell */
5494656105
5494756106
assert( cursorHoldsMutex(pCur) );
5494856107
assert( pBt->inTransaction==TRANS_WRITE );
54949
- assert( !pBt->readOnly );
56108
+ assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
5495056109
assert( pCur->wrFlag );
5495156110
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
5495256111
assert( !hasReadConflicts(p, pCur->pgnoRoot) );
5495356112
5495456113
if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
@@ -55065,11 +56224,11 @@
5506556224
int rc;
5506656225
int ptfFlags; /* Page-type flage for the root page of new table */
5506756226
5506856227
assert( sqlite3BtreeHoldsMutex(p) );
5506956228
assert( pBt->inTransaction==TRANS_WRITE );
55070
- assert( !pBt->readOnly );
56229
+ assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
5507156230
5507256231
#ifdef SQLITE_OMIT_AUTOVACUUM
5507356232
rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
5507456233
if( rc ){
5507556234
return rc;
@@ -55439,11 +56598,13 @@
5543956598
*pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
5544056599
5544156600
/* If auto-vacuum is disabled in this build and this is an auto-vacuum
5544256601
** database, mark the database as read-only. */
5544356602
#ifdef SQLITE_OMIT_AUTOVACUUM
55444
- if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
56603
+ if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
56604
+ pBt->btsFlags |= BTS_READ_ONLY;
56605
+ }
5544556606
#endif
5544656607
5544756608
sqlite3BtreeLeave(p);
5544856609
}
5544956610
@@ -56239,11 +57400,12 @@
5623957400
** (e) the cursor points at a valid row of an intKey table.
5624057401
*/
5624157402
if( !pCsr->wrFlag ){
5624257403
return SQLITE_READONLY;
5624357404
}
56244
- assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
57405
+ assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
57406
+ && pCsr->pBt->inTransaction==TRANS_WRITE );
5624557407
assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
5624657408
assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
5624757409
assert( pCsr->apPage[pCsr->iPage]->intKey );
5624857410
5624957411
return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
@@ -56279,11 +57441,12 @@
5627957441
assert( iVersion==1 || iVersion==2 );
5628057442
5628157443
/* If setting the version fields to 1, do not automatically open the
5628257444
** WAL connection, even if the version fields are currently set to 2.
5628357445
*/
56284
- pBt->doNotUseWAL = (u8)(iVersion==1);
57446
+ pBt->btsFlags &= ~BTS_NO_WAL;
57447
+ if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
5628557448
5628657449
rc = sqlite3BtreeBeginTrans(pBtree, 0);
5628757450
if( rc==SQLITE_OK ){
5628857451
u8 *aData = pBt->pPage1->aData;
5628957452
if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
@@ -56296,11 +57459,11 @@
5629657459
}
5629757460
}
5629857461
}
5629957462
}
5630057463
56301
- pBt->doNotUseWAL = 0;
57464
+ pBt->btsFlags &= ~BTS_NO_WAL;
5630257465
return rc;
5630357466
}
5630457467
5630557468
/************** End of btree.c ***********************************************/
5630657469
/************** Begin file backup.c ******************************************/
@@ -56980,11 +58143,13 @@
5698058143
5698158144
assert( sqlite3BtreeIsInTrans(pTo) );
5698258145
pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
5698358146
if( pFd->pMethods ){
5698458147
i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
56985
- sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
58148
+ rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
58149
+ if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
58150
+ if( rc ) goto copy_finished;
5698658151
}
5698758152
5698858153
/* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
5698958154
** to 0. This is used by the implementations of sqlite3_backup_step()
5699058155
** and sqlite3_backup_finish() to detect that they are being called
@@ -57005,16 +58170,17 @@
5700558170
*/
5700658171
sqlite3_backup_step(&b, 0x7FFFFFFF);
5700758172
assert( b.rc!=SQLITE_OK );
5700858173
rc = sqlite3_backup_finish(&b);
5700958174
if( rc==SQLITE_OK ){
57010
- pTo->pBt->pageSizeFixed = 0;
58175
+ pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
5701158176
}else{
5701258177
sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
5701358178
}
5701458179
5701558180
assert( sqlite3BtreeIsInTrans(pTo)==0 );
58181
+copy_finished:
5701658182
sqlite3BtreeLeave(pFrom);
5701758183
sqlite3BtreeLeave(pTo);
5701858184
return rc;
5701958185
}
5702058186
#endif /* SQLITE_OMIT_VACUUM */
@@ -57037,16 +58203,10 @@
5703758203
** stores a single value in the VDBE. Mem is an opaque structure visible
5703858204
** only within the VDBE. Interface routines refer to a Mem using the
5703958205
** name sqlite_value
5704058206
*/
5704158207
57042
-/*
57043
-** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
57044
-** P if required.
57045
-*/
57046
-#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
57047
-
5704858208
/*
5704958209
** If pMem is an object with a valid string representation, this routine
5705058210
** ensures the internal encoding for the string representation is
5705158211
** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
5705258212
**
@@ -57142,11 +58302,11 @@
5714258302
*/
5714358303
SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
5714458304
int f;
5714558305
assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
5714658306
assert( (pMem->flags&MEM_RowSet)==0 );
57147
- expandBlob(pMem);
58307
+ ExpandBlob(pMem);
5714858308
f = pMem->flags;
5714958309
if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
5715058310
if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
5715158311
return SQLITE_NOMEM;
5715258312
}
@@ -57311,11 +58471,11 @@
5731158471
** Release any memory held by the Mem. This may leave the Mem in an
5731258472
** inconsistent state, for example with (Mem.z==0) and
5731358473
** (Mem.type==SQLITE_TEXT).
5731458474
*/
5731558475
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
57316
- MemReleaseExt(p);
58476
+ VdbeMemRelease(p);
5731758477
sqlite3DbFree(p->db, p->zMalloc);
5731858478
p->z = 0;
5731958479
p->zMalloc = 0;
5732058480
p->xDel = 0;
5732158481
}
@@ -57607,11 +58767,11 @@
5760758767
** copies of this cell as invalid.
5760858768
**
5760958769
** This is used for testing and debugging only - to make sure shallow
5761058770
** copies are not misused.
5761158771
*/
57612
-SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
58772
+SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
5761358773
int i;
5761458774
Mem *pX;
5761558775
for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
5761658776
if( pX->pScopyFrom==pMem ){
5761758777
pX->flags |= MEM_Invalid;
@@ -57633,11 +58793,11 @@
5763358793
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
5763458794
** and flags gets srcType (either MEM_Ephem or MEM_Static).
5763558795
*/
5763658796
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
5763758797
assert( (pFrom->flags & MEM_RowSet)==0 );
57638
- MemReleaseExt(pTo);
58798
+ VdbeMemRelease(pTo);
5763958799
memcpy(pTo, pFrom, MEMCELLSIZE);
5764058800
pTo->xDel = 0;
5764158801
if( (pFrom->flags&MEM_Static)==0 ){
5764258802
pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
5764358803
assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57651,11 +58811,11 @@
5765158811
*/
5765258812
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
5765358813
int rc = SQLITE_OK;
5765458814
5765558815
assert( (pFrom->flags & MEM_RowSet)==0 );
57656
- MemReleaseExt(pTo);
58816
+ VdbeMemRelease(pTo);
5765758817
memcpy(pTo, pFrom, MEMCELLSIZE);
5765858818
pTo->flags &= ~MEM_Dyn;
5765958819
5766058820
if( pTo->flags&(MEM_Str|MEM_Blob) ){
5766158821
if( 0==(pFrom->flags&MEM_Static) ){
@@ -57979,20 +59139,20 @@
5797959139
if( pVal->flags&MEM_Null ){
5798059140
return 0;
5798159141
}
5798259142
assert( (MEM_Blob>>3) == MEM_Str );
5798359143
pVal->flags |= (pVal->flags & MEM_Blob)>>3;
57984
- expandBlob(pVal);
59144
+ ExpandBlob(pVal);
5798559145
if( pVal->flags&MEM_Str ){
5798659146
sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
5798759147
if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
5798859148
assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
5798959149
if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
5799059150
return 0;
5799159151
}
5799259152
}
57993
- sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
59153
+ sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
5799459154
}else{
5799559155
assert( (pVal->flags&MEM_Blob)==0 );
5799659156
sqlite3VdbeMemStringify(pVal, enc);
5799759157
assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
5799859158
}
@@ -58369,11 +59529,12 @@
5836959529
return addr;
5837059530
}
5837159531
5837259532
/*
5837359533
** Add an OP_ParseSchema opcode. This routine is broken out from
58374
-** sqlite3VdbeAddOp4() since it needs to also local all btrees.
59534
+** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
59535
+** as having been used.
5837559536
**
5837659537
** The zWhere string must have been obtained from sqlite3_malloc().
5837759538
** This routine will take ownership of the allocated memory.
5837859539
*/
5837959540
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
@@ -59086,17 +60247,18 @@
5908660247
sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
5908760248
break;
5908860249
}
5908960250
case P4_MEM: {
5909060251
Mem *pMem = pOp->p4.pMem;
59091
- assert( (pMem->flags & MEM_Null)==0 );
5909260252
if( pMem->flags & MEM_Str ){
5909360253
zP4 = pMem->z;
5909460254
}else if( pMem->flags & MEM_Int ){
5909560255
sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
5909660256
}else if( pMem->flags & MEM_Real ){
5909760257
sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
60258
+ }else if( pMem->flags & MEM_Null ){
60259
+ sqlite3_snprintf(nTemp, zTemp, "NULL");
5909860260
}else{
5909960261
assert( pMem->flags & MEM_Blob );
5910060262
zP4 = "(blob)";
5910160263
}
5910260264
break;
@@ -59135,12 +60297,13 @@
5913560297
5913660298
/*
5913760299
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
5913860300
**
5913960301
** The prepared statements need to know in advance the complete set of
59140
-** attached databases that they will be using. A mask of these databases
59141
-** is maintained in p->btreeMask and is used for locking and other purposes.
60302
+** attached databases that will be use. A mask of these databases
60303
+** is maintained in p->btreeMask. The p->lockMask value is the subset of
60304
+** p->btreeMask of databases that will require a lock.
5914260305
*/
5914360306
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
5914460307
assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
5914560308
assert( i<(int)sizeof(p->btreeMask)*8 );
5914660309
p->btreeMask |= ((yDbMask)1)<<i;
@@ -59267,11 +60430,11 @@
5926760430
}else if( p->zMalloc ){
5926860431
sqlite3DbFree(db, p->zMalloc);
5926960432
p->zMalloc = 0;
5927060433
}
5927160434
59272
- p->flags = MEM_Null;
60435
+ p->flags = MEM_Invalid;
5927360436
}
5927460437
db->mallocFailed = malloc_failed;
5927560438
}
5927660439
}
5927760440
@@ -59642,10 +60805,11 @@
5964260805
sqlite3 *db; /* The database connection */
5964360806
int nVar; /* Number of parameters */
5964460807
int nMem; /* Number of VM memory registers */
5964560808
int nCursor; /* Number of cursors required */
5964660809
int nArg; /* Number of arguments in subprograms */
60810
+ int nOnce; /* Number of OP_Once instructions */
5964760811
int n; /* Loop counter */
5964860812
u8 *zCsr; /* Memory available for allocation */
5964960813
u8 *zEnd; /* First byte past allocated memory */
5965060814
int nByte; /* How much extra memory is needed */
5965160815
@@ -59657,10 +60821,12 @@
5965760821
assert( db->mallocFailed==0 );
5965860822
nVar = pParse->nVar;
5965960823
nMem = pParse->nMem;
5966060824
nCursor = pParse->nTab;
5966160825
nArg = pParse->nMaxArg;
60826
+ nOnce = pParse->nOnce;
60827
+ if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
5966260828
5966360829
/* For each cursor required, also allocate a memory cell. Memory
5966460830
** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
5966560831
** the vdbe program. Instead they are used to allocate space for
5966660832
** VdbeCursor/BtCursor structures. The blob of memory associated with
@@ -59703,18 +60869,20 @@
5970360869
p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
5970460870
p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
5970560871
p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
5970660872
p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
5970760873
&zCsr, zEnd, &nByte);
60874
+ p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
5970860875
if( nByte ){
5970960876
p->pFree = sqlite3DbMallocZero(db, nByte);
5971060877
}
5971160878
zCsr = p->pFree;
5971260879
zEnd = &zCsr[nByte];
5971360880
}while( nByte && !db->mallocFailed );
5971460881
5971560882
p->nCursor = (u16)nCursor;
60883
+ p->nOnceFlag = nOnce;
5971660884
if( p->aVar ){
5971760885
p->nVar = (ynVar)nVar;
5971860886
for(n=0; n<nVar; n++){
5971960887
p->aVar[n].flags = MEM_Null;
5972060888
p->aVar[n].db = db;
@@ -59727,11 +60895,11 @@
5972760895
}
5972860896
if( p->aMem ){
5972960897
p->aMem--; /* aMem[] goes from 1..nMem */
5973060898
p->nMem = nMem; /* not from 0..nMem-1 */
5973160899
for(n=1; n<=nMem; n++){
59732
- p->aMem[n].flags = MEM_Null;
60900
+ p->aMem[n].flags = MEM_Invalid;
5973360901
p->aMem[n].db = db;
5973460902
}
5973560903
}
5973660904
p->explain = pParse->explain;
5973760905
sqlite3VdbeRewind(p);
@@ -59769,10 +60937,12 @@
5976960937
** is used, for example, when a trigger sub-program is halted to restore
5977060938
** control to the main program.
5977160939
*/
5977260940
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
5977360941
Vdbe *v = pFrame->v;
60942
+ v->aOnceFlag = pFrame->aOnceFlag;
60943
+ v->nOnceFlag = pFrame->nOnceFlag;
5977460944
v->aOp = pFrame->aOp;
5977560945
v->nOp = pFrame->nOp;
5977660946
v->aMem = pFrame->aMem;
5977760947
v->nMem = pFrame->nMem;
5977860948
v->apCsr = pFrame->apCsr;
@@ -59831,12 +61001,14 @@
5983161001
5983261002
#ifdef SQLITE_DEBUG
5983361003
/* Execute assert() statements to ensure that the Vdbe.apCsr[] and
5983461004
** Vdbe.aMem[] arrays have already been cleaned up. */
5983561005
int i;
59836
- for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
59837
- for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
61006
+ if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
61007
+ if( p->aMem ){
61008
+ for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
61009
+ }
5983861010
#endif
5983961011
5984061012
sqlite3DbFree(db, p->zErrMsg);
5984161013
p->zErrMsg = 0;
5984261014
p->pResultSet = 0;
@@ -59997,20 +61169,35 @@
5999761169
char *zMaster = 0; /* File-name for the master journal */
5999861170
char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
5999961171
sqlite3_file *pMaster = 0;
6000061172
i64 offset = 0;
6000161173
int res;
61174
+ int retryCount = 0;
61175
+ int nMainFile;
6000261176
6000361177
/* Select a master journal file name */
61178
+ nMainFile = sqlite3Strlen30(zMainFile);
61179
+ zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
61180
+ if( zMaster==0 ) return SQLITE_NOMEM;
6000461181
do {
6000561182
u32 iRandom;
60006
- sqlite3DbFree(db, zMaster);
61183
+ if( retryCount ){
61184
+ if( retryCount>100 ){
61185
+ sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
61186
+ sqlite3OsDelete(pVfs, zMaster, 0);
61187
+ break;
61188
+ }else if( retryCount==1 ){
61189
+ sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
61190
+ }
61191
+ }
61192
+ retryCount++;
6000761193
sqlite3_randomness(sizeof(iRandom), &iRandom);
60008
- zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
60009
- if( !zMaster ){
60010
- return SQLITE_NOMEM;
60011
- }
61194
+ sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
61195
+ (iRandom>>8)&0xffffff, iRandom&0xff);
61196
+ /* The antipenultimate character of the master journal name must
61197
+ ** be "9" to avoid name collisions when using 8+3 filenames. */
61198
+ assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
6001261199
sqlite3FileSuffix3(zMainFile, zMaster);
6001361200
rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
6001461201
}while( rc==SQLITE_OK && res );
6001561202
if( rc==SQLITE_OK ){
6001661203
/* Open the master journal. */
@@ -60300,10 +61487,11 @@
6030061487
*/
6030161488
6030261489
if( p->db->mallocFailed ){
6030361490
p->rc = SQLITE_NOMEM;
6030461491
}
61492
+ if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
6030561493
closeAllCursors(p);
6030661494
if( p->magic!=VDBE_MAGIC_RUN ){
6030761495
return SQLITE_OK;
6030861496
}
6030961497
checkActiveVdbeCnt(db);
@@ -60637,10 +61825,14 @@
6063761825
vdbeFreeOpArray(db, p->aOp, p->nOp);
6063861826
sqlite3DbFree(db, p->aLabel);
6063961827
sqlite3DbFree(db, p->aColName);
6064061828
sqlite3DbFree(db, p->zSql);
6064161829
sqlite3DbFree(db, p->pFree);
61830
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
61831
+ sqlite3DbFree(db, p->zExplain);
61832
+ sqlite3DbFree(db, p->pExplain);
61833
+#endif
6064261834
sqlite3DbFree(db, p);
6064361835
}
6064461836
6064561837
/*
6064661838
** Delete an entire VDBE.
@@ -61116,19 +62308,10 @@
6111662308
** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
6111762309
** and the common prefixes are equal, then key1 is less than key2.
6111862310
** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
6111962311
** equal, then the keys are considered to be equal and
6112062312
** the parts beyond the common prefix are ignored.
61121
-**
61122
-** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
61123
-** the header of pKey1 is ignored. It is assumed that pKey1 is
61124
-** an index key, and thus ends with a rowid value. The last byte
61125
-** of the header will therefore be the serial type of the rowid:
61126
-** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
61127
-** The serial type of the final rowid will always be a single byte.
61128
-** By ignoring this last byte of the header, we force the comparison
61129
-** to ignore the rowid at the end of key1.
6113062313
*/
6113162314
SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
6113262315
int nKey1, const void *pKey1, /* Left key */
6113362316
UnpackedRecord *pPKey2 /* Right key */
6113462317
){
@@ -61157,13 +62340,10 @@
6115762340
*/
6115862341
/* mem1.u.i = 0; // not needed, here to silence compiler warning */
6115962342
6116062343
idx1 = getVarint32(aKey1, szHdr1);
6116162344
d1 = szHdr1;
61162
- if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
61163
- szHdr1--;
61164
- }
6116562345
nField = pKeyInfo->nField;
6116662346
while( idx1<szHdr1 && i<pPKey2->nField ){
6116762347
u32 serial_type1;
6116862348
6116962349
/* Read the serial types for the next element in each key. */
@@ -61339,11 +62519,11 @@
6133962519
memset(&m, 0, sizeof(m));
6134062520
rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
6134162521
if( rc ){
6134262522
return rc;
6134362523
}
61344
- assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
62524
+ assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
6134562525
*res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
6134662526
sqlite3VdbeMemRelease(&m);
6134762527
return SQLITE_OK;
6134862528
}
6134962529
@@ -61782,11 +62962,11 @@
6178262962
** since any application that receives an SQLITE_MISUSE is broken by
6178362963
** definition.
6178462964
**
6178562965
** Nevertheless, some published applications that were originally written
6178662966
** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
61787
- ** returns, and the so were broken by the automatic-reset change. As a
62967
+ ** returns, and those were broken by the automatic-reset change. As a
6178862968
** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
6178962969
** legacy behavior of returning SQLITE_MISUSE for cases where the
6179062970
** previous sqlite3_step() returned something other than a SQLITE_LOCKED
6179162971
** or SQLITE_BUSY error.
6179262972
*/
@@ -62128,17 +63308,17 @@
6212863308
pOut = &pVm->pResultSet[i];
6212963309
}else{
6213063310
/* If the value passed as the second argument is out of range, return
6213163311
** a pointer to the following static Mem object which contains the
6213263312
** value SQL NULL. Even though the Mem structure contains an element
62133
- ** of type i64, on certain architecture (x86) with certain compiler
63313
+ ** of type i64, on certain architectures (x86) with certain compiler
6213463314
** switches (-Os), gcc may align this Mem object on a 4-byte boundary
6213563315
** instead of an 8-byte one. This all works fine, except that when
6213663316
** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
6213763317
** that a Mem structure is located on an 8-byte boundary. To prevent
62138
- ** this assert() from failing, when building with SQLITE_DEBUG defined
62139
- ** using gcc, force nullMem to be 8-byte aligned using the magical
63318
+ ** these assert()s from failing, when building with SQLITE_DEBUG defined
63319
+ ** using gcc, we force nullMem to be 8-byte aligned using the magical
6214063320
** __attribute__((aligned(8))) macro. */
6214163321
static const Mem nullMem
6214263322
#if defined(SQLITE_DEBUG) && defined(__GNUC__)
6214363323
__attribute__((aligned(8)))
6214463324
#endif
@@ -62704,10 +63884,18 @@
6270463884
** database.
6270563885
*/
6270663886
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
6270763887
return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
6270863888
}
63889
+
63890
+/*
63891
+** Return true if the prepared statement is in need of being reset.
63892
+*/
63893
+SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
63894
+ Vdbe *v = (Vdbe*)pStmt;
63895
+ return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
63896
+}
6270963897
6271063898
/*
6271163899
** Return a pointer to the next prepared statement after pStmt associated
6271263900
** with database connection pDb. If pStmt is NULL, return the first
6271363901
** prepared statement for the database connection. Return NULL if there
@@ -62749,10 +63937,12 @@
6274963937
**
6275063938
*************************************************************************
6275163939
**
6275263940
** This file contains code used to insert the values of host parameters
6275363941
** (aka "wildcards") into the SQL text output by sqlite3_trace().
63942
+**
63943
+** The Vdbe parse-tree explainer is also found here.
6275463944
*/
6275563945
6275663946
#ifndef SQLITE_OMIT_TRACE
6275763947
6275863948
/*
@@ -62887,10 +64077,125 @@
6288764077
}
6288864078
return sqlite3StrAccumFinish(&out);
6288964079
}
6289064080
6289164081
#endif /* #ifndef SQLITE_OMIT_TRACE */
64082
+
64083
+/*****************************************************************************
64084
+** The following code implements the data-structure explaining logic
64085
+** for the Vdbe.
64086
+*/
64087
+
64088
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
64089
+
64090
+/*
64091
+** Allocate a new Explain object
64092
+*/
64093
+SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
64094
+ if( pVdbe ){
64095
+ sqlite3BeginBenignMalloc();
64096
+ Explain *p = sqlite3_malloc( sizeof(Explain) );
64097
+ if( p ){
64098
+ memset(p, 0, sizeof(*p));
64099
+ p->pVdbe = pVdbe;
64100
+ sqlite3_free(pVdbe->pExplain);
64101
+ pVdbe->pExplain = p;
64102
+ sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
64103
+ SQLITE_MAX_LENGTH);
64104
+ p->str.useMalloc = 2;
64105
+ }else{
64106
+ sqlite3EndBenignMalloc();
64107
+ }
64108
+ }
64109
+}
64110
+
64111
+/*
64112
+** Return true if the Explain ends with a new-line.
64113
+*/
64114
+static int endsWithNL(Explain *p){
64115
+ return p && p->str.zText && p->str.nChar
64116
+ && p->str.zText[p->str.nChar-1]=='\n';
64117
+}
64118
+
64119
+/*
64120
+** Append text to the indentation
64121
+*/
64122
+SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
64123
+ Explain *p;
64124
+ if( pVdbe && (p = pVdbe->pExplain)!=0 ){
64125
+ va_list ap;
64126
+ if( p->nIndent && endsWithNL(p) ){
64127
+ int n = p->nIndent;
64128
+ if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
64129
+ sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
64130
+ }
64131
+ va_start(ap, zFormat);
64132
+ sqlite3VXPrintf(&p->str, 1, zFormat, ap);
64133
+ va_end(ap);
64134
+ }
64135
+}
64136
+
64137
+/*
64138
+** Append a '\n' if there is not already one.
64139
+*/
64140
+SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
64141
+ Explain *p;
64142
+ if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
64143
+ sqlite3StrAccumAppend(&p->str, "\n", 1);
64144
+ }
64145
+}
64146
+
64147
+/*
64148
+** Push a new indentation level. Subsequent lines will be indented
64149
+** so that they begin at the current cursor position.
64150
+*/
64151
+SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
64152
+ Explain *p;
64153
+ if( pVdbe && (p = pVdbe->pExplain)!=0 ){
64154
+ if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
64155
+ const char *z = p->str.zText;
64156
+ int i = p->str.nChar-1;
64157
+ int x;
64158
+ while( i>=0 && z[i]!='\n' ){ i--; }
64159
+ x = (p->str.nChar - 1) - i;
64160
+ if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
64161
+ x = p->aIndent[p->nIndent-1];
64162
+ }
64163
+ p->aIndent[p->nIndent] = x;
64164
+ }
64165
+ p->nIndent++;
64166
+ }
64167
+}
64168
+
64169
+/*
64170
+** Pop the indentation stack by one level.
64171
+*/
64172
+SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
64173
+ if( p && p->pExplain ) p->pExplain->nIndent--;
64174
+}
64175
+
64176
+/*
64177
+** Free the indentation structure
64178
+*/
64179
+SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
64180
+ if( pVdbe && pVdbe->pExplain ){
64181
+ sqlite3_free(pVdbe->zExplain);
64182
+ sqlite3ExplainNL(pVdbe);
64183
+ pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
64184
+ sqlite3_free(pVdbe->pExplain);
64185
+ pVdbe->pExplain = 0;
64186
+ sqlite3EndBenignMalloc();
64187
+ }
64188
+}
64189
+
64190
+/*
64191
+** Return the explanation of a virtual machine.
64192
+*/
64193
+SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
64194
+ return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
64195
+}
64196
+#endif /* defined(SQLITE_DEBUG) */
6289264197
6289364198
/************** End of vdbetrace.c *******************************************/
6289464199
/************** Begin file vdbe.c ********************************************/
6289564200
/*
6289664201
** 2001 September 15
@@ -62942,11 +64247,11 @@
6294264247
** Invoke this macro on memory cells just prior to changing the
6294364248
** value of the cell. This macro verifies that shallow copies are
6294464249
** not misused.
6294564250
*/
6294664251
#ifdef SQLITE_DEBUG
62947
-# define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
64252
+# define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
6294864253
#else
6294964254
# define memAboutToChange(P,M)
6295064255
#endif
6295164256
6295264257
/*
@@ -62960,12 +64265,12 @@
6296064265
SQLITE_API int sqlite3_search_count = 0;
6296164266
#endif
6296264267
6296364268
/*
6296464269
** When this global variable is positive, it gets decremented once before
62965
-** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
62966
-** field of the sqlite3 structure is set in order to simulate and interrupt.
64270
+** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
64271
+** field of the sqlite3 structure is set in order to simulate an interrupt.
6296764272
**
6296864273
** This facility is used for testing purposes only. It does not function
6296964274
** in an ordinary build.
6297064275
*/
6297164276
#ifdef SQLITE_TEST
@@ -63041,16 +64346,10 @@
6304164346
*/
6304264347
#define Deephemeralize(P) \
6304364348
if( ((P)->flags&MEM_Ephem)!=0 \
6304464349
&& sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
6304564350
63046
-/*
63047
-** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
63048
-** P if required.
63049
-*/
63050
-#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
63051
-
6305264351
/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
6305364352
#ifdef SQLITE_OMIT_MERGE_SORT
6305464353
# define isSorter(x) 0
6305564354
#else
6305664355
# define isSorter(x) ((x)->pSorter!=0)
@@ -63086,11 +64385,11 @@
6308664385
*/
6308764386
static VdbeCursor *allocateCursor(
6308864387
Vdbe *p, /* The virtual machine */
6308964388
int iCur, /* Index of the new VdbeCursor */
6309064389
int nField, /* Number of fields in the table or index */
63091
- int iDb, /* When database the cursor belongs to, or -1 */
64390
+ int iDb, /* Database the cursor belongs to, or -1 */
6309264391
int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
6309364392
){
6309464393
/* Find the memory cell that will be used to store the blob of memory
6309564394
** required for this VdbeCursor structure. It is convenient to use a
6309664395
** vdbe memory cell to manage the memory allocation required for a
@@ -63457,11 +64756,11 @@
6345764756
** sqlite3_interrupt() routine has been called. If it has been, then
6345864757
** processing of the VDBE program is interrupted.
6345964758
**
6346064759
** This macro added to every instruction that does a jump in order to
6346164760
** implement a loop. This test used to be on every single instruction,
63462
-** but that meant we more testing that we needed. By only testing the
64761
+** but that meant we more testing than we needed. By only testing the
6346364762
** flag on jump instructions, we get a (small) speed improvement.
6346464763
*/
6346564764
#define CHECK_FOR_INTERRUPT \
6346664765
if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
6346764766
@@ -63567,69 +64866,72 @@
6356764866
*/
6356864867
union vdbeExecUnion {
6356964868
struct OP_Yield_stack_vars {
6357064869
int pcDest;
6357164870
} aa;
64871
+ struct OP_Null_stack_vars {
64872
+ int cnt;
64873
+ } ab;
6357264874
struct OP_Variable_stack_vars {
6357364875
Mem *pVar; /* Value being transferred */
63574
- } ab;
64876
+ } ac;
6357564877
struct OP_Move_stack_vars {
6357664878
char *zMalloc; /* Holding variable for allocated memory */
6357764879
int n; /* Number of registers left to copy */
6357864880
int p1; /* Register to copy from */
6357964881
int p2; /* Register to copy to */
63580
- } ac;
64882
+ } ad;
6358164883
struct OP_ResultRow_stack_vars {
6358264884
Mem *pMem;
6358364885
int i;
63584
- } ad;
64886
+ } ae;
6358564887
struct OP_Concat_stack_vars {
6358664888
i64 nByte;
63587
- } ae;
64889
+ } af;
6358864890
struct OP_Remainder_stack_vars {
6358964891
int flags; /* Combined MEM_* flags from both inputs */
6359064892
i64 iA; /* Integer value of left operand */
6359164893
i64 iB; /* Integer value of right operand */
6359264894
double rA; /* Real value of left operand */
6359364895
double rB; /* Real value of right operand */
63594
- } af;
64896
+ } ag;
6359564897
struct OP_Function_stack_vars {
6359664898
int i;
6359764899
Mem *pArg;
6359864900
sqlite3_context ctx;
6359964901
sqlite3_value **apVal;
6360064902
int n;
63601
- } ag;
64903
+ } ah;
6360264904
struct OP_ShiftRight_stack_vars {
6360364905
i64 iA;
6360464906
u64 uA;
6360564907
i64 iB;
6360664908
u8 op;
63607
- } ah;
64909
+ } ai;
6360864910
struct OP_Ge_stack_vars {
6360964911
int res; /* Result of the comparison of pIn1 against pIn3 */
6361064912
char affinity; /* Affinity to use for comparison */
6361164913
u16 flags1; /* Copy of initial value of pIn1->flags */
6361264914
u16 flags3; /* Copy of initial value of pIn3->flags */
63613
- } ai;
64915
+ } aj;
6361464916
struct OP_Compare_stack_vars {
6361564917
int n;
6361664918
int i;
6361764919
int p1;
6361864920
int p2;
6361964921
const KeyInfo *pKeyInfo;
6362064922
int idx;
6362164923
CollSeq *pColl; /* Collating sequence to use on this term */
6362264924
int bRev; /* True for DESCENDING sort order */
63623
- } aj;
64925
+ } ak;
6362464926
struct OP_Or_stack_vars {
6362564927
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
6362664928
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63627
- } ak;
64929
+ } al;
6362864930
struct OP_IfNot_stack_vars {
6362964931
int c;
63630
- } al;
64932
+ } am;
6363164933
struct OP_Column_stack_vars {
6363264934
u32 payloadSize; /* Number of bytes in the record */
6363364935
i64 payloadSize64; /* Number of bytes in the record */
6363464936
int p1; /* P1 value of the opcode */
6363564937
int p2; /* column number to retrieve */
@@ -63650,15 +64952,15 @@
6365064952
u32 szField; /* Number of bytes in the content of a field */
6365164953
int szHdr; /* Size of the header size field at start of record */
6365264954
int avail; /* Number of bytes of available data */
6365364955
u32 t; /* A type code from the record header */
6365464956
Mem *pReg; /* PseudoTable input register */
63655
- } am;
64957
+ } an;
6365664958
struct OP_Affinity_stack_vars {
6365764959
const char *zAffinity; /* The affinity to be applied */
6365864960
char cAff; /* A single character of affinity */
63659
- } an;
64961
+ } ao;
6366064962
struct OP_MakeRecord_stack_vars {
6366164963
u8 *zNewRecord; /* A buffer to hold the data for the new record */
6366264964
Mem *pRec; /* The new record */
6366364965
u64 nData; /* Number of bytes of data space */
6366464966
int nHdr; /* Number of bytes of header space */
@@ -63671,108 +64973,108 @@
6367164973
int nField; /* Number of fields in the record */
6367264974
char *zAffinity; /* The affinity string for the record */
6367364975
int file_format; /* File format to use for encoding */
6367464976
int i; /* Space used in zNewRecord[] */
6367564977
int len; /* Length of a field */
63676
- } ao;
64978
+ } ap;
6367764979
struct OP_Count_stack_vars {
6367864980
i64 nEntry;
6367964981
BtCursor *pCrsr;
63680
- } ap;
64982
+ } aq;
6368164983
struct OP_Savepoint_stack_vars {
6368264984
int p1; /* Value of P1 operand */
6368364985
char *zName; /* Name of savepoint */
6368464986
int nName;
6368564987
Savepoint *pNew;
6368664988
Savepoint *pSavepoint;
6368764989
Savepoint *pTmp;
6368864990
int iSavepoint;
6368964991
int ii;
63690
- } aq;
64992
+ } ar;
6369164993
struct OP_AutoCommit_stack_vars {
6369264994
int desiredAutoCommit;
6369364995
int iRollback;
6369464996
int turnOnAC;
63695
- } ar;
64997
+ } as;
6369664998
struct OP_Transaction_stack_vars {
6369764999
Btree *pBt;
63698
- } as;
65000
+ } at;
6369965001
struct OP_ReadCookie_stack_vars {
6370065002
int iMeta;
6370165003
int iDb;
6370265004
int iCookie;
63703
- } at;
65005
+ } au;
6370465006
struct OP_SetCookie_stack_vars {
6370565007
Db *pDb;
63706
- } au;
65008
+ } av;
6370765009
struct OP_VerifyCookie_stack_vars {
6370865010
int iMeta;
6370965011
int iGen;
6371065012
Btree *pBt;
63711
- } av;
65013
+ } aw;
6371265014
struct OP_OpenWrite_stack_vars {
6371365015
int nField;
6371465016
KeyInfo *pKeyInfo;
6371565017
int p2;
6371665018
int iDb;
6371765019
int wrFlag;
6371865020
Btree *pX;
6371965021
VdbeCursor *pCur;
6372065022
Db *pDb;
63721
- } aw;
65023
+ } ax;
6372265024
struct OP_OpenEphemeral_stack_vars {
6372365025
VdbeCursor *pCx;
63724
- } ax;
65026
+ } ay;
6372565027
struct OP_SorterOpen_stack_vars {
6372665028
VdbeCursor *pCx;
63727
- } ay;
65029
+ } az;
6372865030
struct OP_OpenPseudo_stack_vars {
6372965031
VdbeCursor *pCx;
63730
- } az;
65032
+ } ba;
6373165033
struct OP_SeekGt_stack_vars {
6373265034
int res;
6373365035
int oc;
6373465036
VdbeCursor *pC;
6373565037
UnpackedRecord r;
6373665038
int nField;
6373765039
i64 iKey; /* The rowid we are to seek to */
63738
- } ba;
65040
+ } bb;
6373965041
struct OP_Seek_stack_vars {
6374065042
VdbeCursor *pC;
63741
- } bb;
65043
+ } bc;
6374265044
struct OP_Found_stack_vars {
6374365045
int alreadyExists;
6374465046
VdbeCursor *pC;
6374565047
int res;
6374665048
char *pFree;
6374765049
UnpackedRecord *pIdxKey;
6374865050
UnpackedRecord r;
6374965051
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63750
- } bc;
65052
+ } bd;
6375165053
struct OP_IsUnique_stack_vars {
6375265054
u16 ii;
6375365055
VdbeCursor *pCx;
6375465056
BtCursor *pCrsr;
6375565057
u16 nField;
6375665058
Mem *aMx;
6375765059
UnpackedRecord r; /* B-Tree index search key */
6375865060
i64 R; /* Rowid stored in register P3 */
63759
- } bd;
65061
+ } be;
6376065062
struct OP_NotExists_stack_vars {
6376165063
VdbeCursor *pC;
6376265064
BtCursor *pCrsr;
6376365065
int res;
6376465066
u64 iKey;
63765
- } be;
65067
+ } bf;
6376665068
struct OP_NewRowid_stack_vars {
6376765069
i64 v; /* The new rowid */
6376865070
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6376965071
int res; /* Result of an sqlite3BtreeLast() */
6377065072
int cnt; /* Counter to limit the number of searches */
6377165073
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6377265074
VdbeFrame *pFrame; /* Root frame of VDBE */
63773
- } bf;
65075
+ } bg;
6377465076
struct OP_InsertInt_stack_vars {
6377565077
Mem *pData; /* MEM cell holding data for the record to be inserted */
6377665078
Mem *pKey; /* MEM cell holding key for the record */
6377765079
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6377865080
VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63779,161 +65081,161 @@
6377965081
int nZero; /* Number of zero-bytes to append */
6378065082
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6378165083
const char *zDb; /* database name - used by the update hook */
6378265084
const char *zTbl; /* Table name - used by the opdate hook */
6378365085
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63784
- } bg;
65086
+ } bh;
6378565087
struct OP_Delete_stack_vars {
6378665088
i64 iKey;
6378765089
VdbeCursor *pC;
63788
- } bh;
65090
+ } bi;
6378965091
struct OP_SorterCompare_stack_vars {
6379065092
VdbeCursor *pC;
6379165093
int res;
63792
- } bi;
65094
+ } bj;
6379365095
struct OP_SorterData_stack_vars {
6379465096
VdbeCursor *pC;
63795
- } bj;
65097
+ } bk;
6379665098
struct OP_RowData_stack_vars {
6379765099
VdbeCursor *pC;
6379865100
BtCursor *pCrsr;
6379965101
u32 n;
6380065102
i64 n64;
63801
- } bk;
65103
+ } bl;
6380265104
struct OP_Rowid_stack_vars {
6380365105
VdbeCursor *pC;
6380465106
i64 v;
6380565107
sqlite3_vtab *pVtab;
6380665108
const sqlite3_module *pModule;
63807
- } bl;
65109
+ } bm;
6380865110
struct OP_NullRow_stack_vars {
6380965111
VdbeCursor *pC;
63810
- } bm;
65112
+ } bn;
6381165113
struct OP_Last_stack_vars {
6381265114
VdbeCursor *pC;
6381365115
BtCursor *pCrsr;
6381465116
int res;
63815
- } bn;
65117
+ } bo;
6381665118
struct OP_Rewind_stack_vars {
6381765119
VdbeCursor *pC;
6381865120
BtCursor *pCrsr;
6381965121
int res;
63820
- } bo;
65122
+ } bp;
6382165123
struct OP_Next_stack_vars {
6382265124
VdbeCursor *pC;
6382365125
int res;
63824
- } bp;
65126
+ } bq;
6382565127
struct OP_IdxInsert_stack_vars {
6382665128
VdbeCursor *pC;
6382765129
BtCursor *pCrsr;
6382865130
int nKey;
6382965131
const char *zKey;
63830
- } bq;
65132
+ } br;
6383165133
struct OP_IdxDelete_stack_vars {
6383265134
VdbeCursor *pC;
6383365135
BtCursor *pCrsr;
6383465136
int res;
6383565137
UnpackedRecord r;
63836
- } br;
65138
+ } bs;
6383765139
struct OP_IdxRowid_stack_vars {
6383865140
BtCursor *pCrsr;
6383965141
VdbeCursor *pC;
6384065142
i64 rowid;
63841
- } bs;
65143
+ } bt;
6384265144
struct OP_IdxGE_stack_vars {
6384365145
VdbeCursor *pC;
6384465146
int res;
6384565147
UnpackedRecord r;
63846
- } bt;
65148
+ } bu;
6384765149
struct OP_Destroy_stack_vars {
6384865150
int iMoved;
6384965151
int iCnt;
6385065152
Vdbe *pVdbe;
6385165153
int iDb;
63852
- } bu;
65154
+ } bv;
6385365155
struct OP_Clear_stack_vars {
6385465156
int nChange;
63855
- } bv;
65157
+ } bw;
6385665158
struct OP_CreateTable_stack_vars {
6385765159
int pgno;
6385865160
int flags;
6385965161
Db *pDb;
63860
- } bw;
65162
+ } bx;
6386165163
struct OP_ParseSchema_stack_vars {
6386265164
int iDb;
6386365165
const char *zMaster;
6386465166
char *zSql;
6386565167
InitData initData;
63866
- } bx;
65168
+ } by;
6386765169
struct OP_IntegrityCk_stack_vars {
6386865170
int nRoot; /* Number of tables to check. (Number of root pages.) */
6386965171
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6387065172
int j; /* Loop counter */
6387165173
int nErr; /* Number of errors reported */
6387265174
char *z; /* Text of the error report */
6387365175
Mem *pnErr; /* Register keeping track of errors remaining */
63874
- } by;
65176
+ } bz;
6387565177
struct OP_RowSetRead_stack_vars {
6387665178
i64 val;
63877
- } bz;
65179
+ } ca;
6387865180
struct OP_RowSetTest_stack_vars {
6387965181
int iSet;
6388065182
int exists;
63881
- } ca;
65183
+ } cb;
6388265184
struct OP_Program_stack_vars {
6388365185
int nMem; /* Number of memory registers for sub-program */
6388465186
int nByte; /* Bytes of runtime space required for sub-program */
6388565187
Mem *pRt; /* Register to allocate runtime space */
6388665188
Mem *pMem; /* Used to iterate through memory cells */
6388765189
Mem *pEnd; /* Last memory cell in new array */
6388865190
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6388965191
SubProgram *pProgram; /* Sub-program to execute */
6389065192
void *t; /* Token identifying trigger */
63891
- } cb;
65193
+ } cc;
6389265194
struct OP_Param_stack_vars {
6389365195
VdbeFrame *pFrame;
6389465196
Mem *pIn;
63895
- } cc;
65197
+ } cd;
6389665198
struct OP_MemMax_stack_vars {
6389765199
Mem *pIn1;
6389865200
VdbeFrame *pFrame;
63899
- } cd;
65201
+ } ce;
6390065202
struct OP_AggStep_stack_vars {
6390165203
int n;
6390265204
int i;
6390365205
Mem *pMem;
6390465206
Mem *pRec;
6390565207
sqlite3_context ctx;
6390665208
sqlite3_value **apVal;
63907
- } ce;
65209
+ } cf;
6390865210
struct OP_AggFinal_stack_vars {
6390965211
Mem *pMem;
63910
- } cf;
65212
+ } cg;
6391165213
struct OP_Checkpoint_stack_vars {
6391265214
int i; /* Loop counter */
6391365215
int aRes[3]; /* Results */
6391465216
Mem *pMem; /* Write results here */
63915
- } cg;
65217
+ } ch;
6391665218
struct OP_JournalMode_stack_vars {
6391765219
Btree *pBt; /* Btree to change journal mode of */
6391865220
Pager *pPager; /* Pager associated with pBt */
6391965221
int eNew; /* New journal mode */
6392065222
int eOld; /* The old journal mode */
6392165223
const char *zFilename; /* Name of database file for pPager */
63922
- } ch;
65224
+ } ci;
6392365225
struct OP_IncrVacuum_stack_vars {
6392465226
Btree *pBt;
63925
- } ci;
65227
+ } cj;
6392665228
struct OP_VBegin_stack_vars {
6392765229
VTable *pVTab;
63928
- } cj;
65230
+ } ck;
6392965231
struct OP_VOpen_stack_vars {
6393065232
VdbeCursor *pCur;
6393165233
sqlite3_vtab_cursor *pVtabCursor;
6393265234
sqlite3_vtab *pVtab;
6393365235
sqlite3_module *pModule;
63934
- } ck;
65236
+ } cl;
6393565237
struct OP_VFilter_stack_vars {
6393665238
int nArg;
6393765239
int iQuery;
6393865240
const sqlite3_module *pModule;
6393965241
Mem *pQuery;
@@ -63942,40 +65244,40 @@
6394265244
sqlite3_vtab *pVtab;
6394365245
VdbeCursor *pCur;
6394465246
int res;
6394565247
int i;
6394665248
Mem **apArg;
63947
- } cl;
65249
+ } cm;
6394865250
struct OP_VColumn_stack_vars {
6394965251
sqlite3_vtab *pVtab;
6395065252
const sqlite3_module *pModule;
6395165253
Mem *pDest;
6395265254
sqlite3_context sContext;
63953
- } cm;
65255
+ } cn;
6395465256
struct OP_VNext_stack_vars {
6395565257
sqlite3_vtab *pVtab;
6395665258
const sqlite3_module *pModule;
6395765259
int res;
6395865260
VdbeCursor *pCur;
63959
- } cn;
65261
+ } co;
6396065262
struct OP_VRename_stack_vars {
6396165263
sqlite3_vtab *pVtab;
6396265264
Mem *pName;
63963
- } co;
65265
+ } cp;
6396465266
struct OP_VUpdate_stack_vars {
6396565267
sqlite3_vtab *pVtab;
6396665268
sqlite3_module *pModule;
6396765269
int nArg;
6396865270
int i;
6396965271
sqlite_int64 rowid;
6397065272
Mem **apArg;
6397165273
Mem *pX;
63972
- } cp;
65274
+ } cq;
6397365275
struct OP_Trace_stack_vars {
6397465276
char *zTrace;
6397565277
char *z;
63976
- } cq;
65278
+ } cr;
6397765279
} u;
6397865280
/* End automatically generated code
6397965281
********************************************************************/
6398065282
6398165283
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64071,11 +65373,11 @@
6407165373
if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
6407265374
assert( pOp->p2>0 );
6407365375
assert( pOp->p2<=p->nMem );
6407465376
pOut = &aMem[pOp->p2];
6407565377
memAboutToChange(p, pOut);
64076
- MemReleaseExt(pOut);
65378
+ VdbeMemRelease(pOut);
6407765379
pOut->flags = MEM_Int;
6407865380
}
6407965381
6408065382
/* Sanity checking on other operands */
6408165383
#ifdef SQLITE_DEBUG
@@ -64162,11 +65464,12 @@
6416265464
/* Opcode: Gosub P1 P2 * * *
6416365465
**
6416465466
** Write the current address onto register P1
6416565467
** and then jump to address P2.
6416665468
*/
64167
-case OP_Gosub: { /* jump, in1 */
65469
+case OP_Gosub: { /* jump */
65470
+ assert( pOp->p1>0 && pOp->p1<=p->nMem );
6416865471
pIn1 = &aMem[pOp->p1];
6416965472
assert( (pIn1->flags & MEM_Dyn)==0 );
6417065473
memAboutToChange(p, pIn1);
6417165474
pIn1->flags = MEM_Int;
6417265475
pIn1->u.i = pc;
@@ -64361,16 +65664,31 @@
6436165664
pOut->enc = encoding;
6436265665
UPDATE_MAX_BLOBSIZE(pOut);
6436365666
break;
6436465667
}
6436565668
64366
-/* Opcode: Null * P2 * * *
65669
+/* Opcode: Null * P2 P3 * *
6436765670
**
64368
-** Write a NULL into register P2.
65671
+** Write a NULL into registers P2. If P3 greater than P2, then also write
65672
+** NULL into register P3 and ever register in between P2 and P3. If P3
65673
+** is less than P2 (typically P3 is zero) then only register P2 is
65674
+** set to NULL
6436965675
*/
6437065676
case OP_Null: { /* out2-prerelease */
65677
+#if 0 /* local variables moved into u.ab */
65678
+ int cnt;
65679
+#endif /* local variables moved into u.ab */
65680
+ u.ab.cnt = pOp->p3-pOp->p2;
65681
+ assert( pOp->p3<=p->nMem );
6437165682
pOut->flags = MEM_Null;
65683
+ while( u.ab.cnt>0 ){
65684
+ pOut++;
65685
+ memAboutToChange(p, pOut);
65686
+ VdbeMemRelease(pOut);
65687
+ pOut->flags = MEM_Null;
65688
+ u.ab.cnt--;
65689
+ }
6437265690
break;
6437365691
}
6437465692
6437565693
6437665694
/* Opcode: Blob P1 P2 * P4
@@ -64392,21 +65710,21 @@
6439265710
**
6439365711
** If the parameter is named, then its name appears in P4 and P3==1.
6439465712
** The P4 value is used by sqlite3_bind_parameter_name().
6439565713
*/
6439665714
case OP_Variable: { /* out2-prerelease */
64397
-#if 0 /* local variables moved into u.ab */
65715
+#if 0 /* local variables moved into u.ac */
6439865716
Mem *pVar; /* Value being transferred */
64399
-#endif /* local variables moved into u.ab */
65717
+#endif /* local variables moved into u.ac */
6440065718
6440165719
assert( pOp->p1>0 && pOp->p1<=p->nVar );
6440265720
assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
64403
- u.ab.pVar = &p->aVar[pOp->p1 - 1];
64404
- if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
65721
+ u.ac.pVar = &p->aVar[pOp->p1 - 1];
65722
+ if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
6440565723
goto too_big;
6440665724
}
64407
- sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
65725
+ sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
6440865726
UPDATE_MAX_BLOBSIZE(pOut);
6440965727
break;
6441065728
}
6441165729
6441265730
/* Opcode: Move P1 P2 P3 * *
@@ -64415,40 +65733,40 @@
6441565733
** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
6441665734
** left holding a NULL. It is an error for register ranges
6441765735
** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
6441865736
*/
6441965737
case OP_Move: {
64420
-#if 0 /* local variables moved into u.ac */
65738
+#if 0 /* local variables moved into u.ad */
6442165739
char *zMalloc; /* Holding variable for allocated memory */
6442265740
int n; /* Number of registers left to copy */
6442365741
int p1; /* Register to copy from */
6442465742
int p2; /* Register to copy to */
64425
-#endif /* local variables moved into u.ac */
64426
-
64427
- u.ac.n = pOp->p3;
64428
- u.ac.p1 = pOp->p1;
64429
- u.ac.p2 = pOp->p2;
64430
- assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
64431
- assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
64432
-
64433
- pIn1 = &aMem[u.ac.p1];
64434
- pOut = &aMem[u.ac.p2];
64435
- while( u.ac.n-- ){
65743
+#endif /* local variables moved into u.ad */
65744
+
65745
+ u.ad.n = pOp->p3;
65746
+ u.ad.p1 = pOp->p1;
65747
+ u.ad.p2 = pOp->p2;
65748
+ assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
65749
+ assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
65750
+
65751
+ pIn1 = &aMem[u.ad.p1];
65752
+ pOut = &aMem[u.ad.p2];
65753
+ while( u.ad.n-- ){
6443665754
assert( pOut<=&aMem[p->nMem] );
6443765755
assert( pIn1<=&aMem[p->nMem] );
6443865756
assert( memIsValid(pIn1) );
6443965757
memAboutToChange(p, pOut);
64440
- u.ac.zMalloc = pOut->zMalloc;
65758
+ u.ad.zMalloc = pOut->zMalloc;
6444165759
pOut->zMalloc = 0;
6444265760
sqlite3VdbeMemMove(pOut, pIn1);
6444365761
#ifdef SQLITE_DEBUG
64444
- if( pOut->pScopyFrom>=&aMem[u.ac.p1] && pOut->pScopyFrom<&aMem[u.ac.p1+pOp->p3] ){
64445
- pOut->pScopyFrom += u.ac.p1 - pOp->p2;
65762
+ if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
65763
+ pOut->pScopyFrom += u.ad.p1 - pOp->p2;
6444665764
}
6444765765
#endif
64448
- pIn1->zMalloc = u.ac.zMalloc;
64449
- REGISTER_TRACE(u.ac.p2++, pOut);
65766
+ pIn1->zMalloc = u.ad.zMalloc;
65767
+ REGISTER_TRACE(u.ad.p2++, pOut);
6445065768
pIn1++;
6445165769
pOut++;
6445265770
}
6445365771
break;
6445465772
}
@@ -64501,14 +65819,14 @@
6450165819
** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
6450265820
** structure to provide access to the top P1 values as the result
6450365821
** row.
6450465822
*/
6450565823
case OP_ResultRow: {
64506
-#if 0 /* local variables moved into u.ad */
65824
+#if 0 /* local variables moved into u.ae */
6450765825
Mem *pMem;
6450865826
int i;
64509
-#endif /* local variables moved into u.ad */
65827
+#endif /* local variables moved into u.ae */
6451065828
assert( p->nResColumn==pOp->p2 );
6451165829
assert( pOp->p1>0 );
6451265830
assert( pOp->p1+pOp->p2<=p->nMem+1 );
6451365831
6451465832
/* If this statement has violated immediate foreign key constraints, do
@@ -64544,21 +65862,21 @@
6454465862
/* Invalidate all ephemeral cursor row caches */
6454565863
p->cacheCtr = (p->cacheCtr + 2)|1;
6454665864
6454765865
/* Make sure the results of the current row are \000 terminated
6454865866
** and have an assigned type. The results are de-ephemeralized as
64549
- ** as side effect.
65867
+ ** a side effect.
6455065868
*/
64551
- u.ad.pMem = p->pResultSet = &aMem[pOp->p1];
64552
- for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
64553
- assert( memIsValid(&u.ad.pMem[u.ad.i]) );
64554
- Deephemeralize(&u.ad.pMem[u.ad.i]);
64555
- assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0
64556
- || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 );
64557
- sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
64558
- sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]);
64559
- REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
65869
+ u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
65870
+ for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
65871
+ assert( memIsValid(&u.ae.pMem[u.ae.i]) );
65872
+ Deephemeralize(&u.ae.pMem[u.ae.i]);
65873
+ assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
65874
+ || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
65875
+ sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
65876
+ sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
65877
+ REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
6456065878
}
6456165879
if( db->mallocFailed ) goto no_mem;
6456265880
6456365881
/* Return SQLITE_ROW
6456465882
*/
@@ -64578,13 +65896,13 @@
6457865896
** It is illegal for P1 and P3 to be the same register. Sometimes,
6457965897
** if P3 is the same register as P2, the implementation is able
6458065898
** to avoid a memcpy().
6458165899
*/
6458265900
case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
64583
-#if 0 /* local variables moved into u.ae */
65901
+#if 0 /* local variables moved into u.af */
6458465902
i64 nByte;
64585
-#endif /* local variables moved into u.ae */
65903
+#endif /* local variables moved into u.af */
6458665904
6458765905
pIn1 = &aMem[pOp->p1];
6458865906
pIn2 = &aMem[pOp->p2];
6458965907
pOut = &aMem[pOp->p3];
6459065908
assert( pIn1!=pOut );
@@ -64593,26 +65911,26 @@
6459365911
break;
6459465912
}
6459565913
if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
6459665914
Stringify(pIn1, encoding);
6459765915
Stringify(pIn2, encoding);
64598
- u.ae.nByte = pIn1->n + pIn2->n;
64599
- if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
65916
+ u.af.nByte = pIn1->n + pIn2->n;
65917
+ if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6460065918
goto too_big;
6460165919
}
6460265920
MemSetTypeFlag(pOut, MEM_Str);
64603
- if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
65921
+ if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
6460465922
goto no_mem;
6460565923
}
6460665924
if( pOut!=pIn2 ){
6460765925
memcpy(pOut->z, pIn2->z, pIn2->n);
6460865926
}
6460965927
memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64610
- pOut->z[u.ae.nByte] = 0;
64611
- pOut->z[u.ae.nByte+1] = 0;
65928
+ pOut->z[u.af.nByte] = 0;
65929
+ pOut->z[u.af.nByte+1] = 0;
6461265930
pOut->flags |= MEM_Term;
64613
- pOut->n = (int)u.ae.nByte;
65931
+ pOut->n = (int)u.af.nByte;
6461465932
pOut->enc = encoding;
6461565933
UPDATE_MAX_BLOBSIZE(pOut);
6461665934
break;
6461765935
}
6461865936
@@ -64652,80 +65970,80 @@
6465265970
case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
6465365971
case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
6465465972
case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
6465565973
case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
6465665974
case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
64657
-#if 0 /* local variables moved into u.af */
65975
+#if 0 /* local variables moved into u.ag */
6465865976
int flags; /* Combined MEM_* flags from both inputs */
6465965977
i64 iA; /* Integer value of left operand */
6466065978
i64 iB; /* Integer value of right operand */
6466165979
double rA; /* Real value of left operand */
6466265980
double rB; /* Real value of right operand */
64663
-#endif /* local variables moved into u.af */
65981
+#endif /* local variables moved into u.ag */
6466465982
6466565983
pIn1 = &aMem[pOp->p1];
6466665984
applyNumericAffinity(pIn1);
6466765985
pIn2 = &aMem[pOp->p2];
6466865986
applyNumericAffinity(pIn2);
6466965987
pOut = &aMem[pOp->p3];
64670
- u.af.flags = pIn1->flags | pIn2->flags;
64671
- if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
65988
+ u.ag.flags = pIn1->flags | pIn2->flags;
65989
+ if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
6467265990
if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64673
- u.af.iA = pIn1->u.i;
64674
- u.af.iB = pIn2->u.i;
65991
+ u.ag.iA = pIn1->u.i;
65992
+ u.ag.iB = pIn2->u.i;
6467565993
switch( pOp->opcode ){
64676
- case OP_Add: if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
64677
- case OP_Subtract: if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
64678
- case OP_Multiply: if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
65994
+ case OP_Add: if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
65995
+ case OP_Subtract: if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
65996
+ case OP_Multiply: if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
6467965997
case OP_Divide: {
64680
- if( u.af.iA==0 ) goto arithmetic_result_is_null;
64681
- if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
64682
- u.af.iB /= u.af.iA;
65998
+ if( u.ag.iA==0 ) goto arithmetic_result_is_null;
65999
+ if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
66000
+ u.ag.iB /= u.ag.iA;
6468366001
break;
6468466002
}
6468566003
default: {
64686
- if( u.af.iA==0 ) goto arithmetic_result_is_null;
64687
- if( u.af.iA==-1 ) u.af.iA = 1;
64688
- u.af.iB %= u.af.iA;
66004
+ if( u.ag.iA==0 ) goto arithmetic_result_is_null;
66005
+ if( u.ag.iA==-1 ) u.ag.iA = 1;
66006
+ u.ag.iB %= u.ag.iA;
6468966007
break;
6469066008
}
6469166009
}
64692
- pOut->u.i = u.af.iB;
66010
+ pOut->u.i = u.ag.iB;
6469366011
MemSetTypeFlag(pOut, MEM_Int);
6469466012
}else{
6469566013
fp_math:
64696
- u.af.rA = sqlite3VdbeRealValue(pIn1);
64697
- u.af.rB = sqlite3VdbeRealValue(pIn2);
66014
+ u.ag.rA = sqlite3VdbeRealValue(pIn1);
66015
+ u.ag.rB = sqlite3VdbeRealValue(pIn2);
6469866016
switch( pOp->opcode ){
64699
- case OP_Add: u.af.rB += u.af.rA; break;
64700
- case OP_Subtract: u.af.rB -= u.af.rA; break;
64701
- case OP_Multiply: u.af.rB *= u.af.rA; break;
66017
+ case OP_Add: u.ag.rB += u.ag.rA; break;
66018
+ case OP_Subtract: u.ag.rB -= u.ag.rA; break;
66019
+ case OP_Multiply: u.ag.rB *= u.ag.rA; break;
6470266020
case OP_Divide: {
6470366021
/* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64704
- if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
64705
- u.af.rB /= u.af.rA;
66022
+ if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
66023
+ u.ag.rB /= u.ag.rA;
6470666024
break;
6470766025
}
6470866026
default: {
64709
- u.af.iA = (i64)u.af.rA;
64710
- u.af.iB = (i64)u.af.rB;
64711
- if( u.af.iA==0 ) goto arithmetic_result_is_null;
64712
- if( u.af.iA==-1 ) u.af.iA = 1;
64713
- u.af.rB = (double)(u.af.iB % u.af.iA);
66027
+ u.ag.iA = (i64)u.ag.rA;
66028
+ u.ag.iB = (i64)u.ag.rB;
66029
+ if( u.ag.iA==0 ) goto arithmetic_result_is_null;
66030
+ if( u.ag.iA==-1 ) u.ag.iA = 1;
66031
+ u.ag.rB = (double)(u.ag.iB % u.ag.iA);
6471466032
break;
6471566033
}
6471666034
}
6471766035
#ifdef SQLITE_OMIT_FLOATING_POINT
64718
- pOut->u.i = u.af.rB;
66036
+ pOut->u.i = u.ag.rB;
6471966037
MemSetTypeFlag(pOut, MEM_Int);
6472066038
#else
64721
- if( sqlite3IsNaN(u.af.rB) ){
66039
+ if( sqlite3IsNaN(u.ag.rB) ){
6472266040
goto arithmetic_result_is_null;
6472366041
}
64724
- pOut->r = u.af.rB;
66042
+ pOut->r = u.ag.rB;
6472566043
MemSetTypeFlag(pOut, MEM_Real);
64726
- if( (u.af.flags & MEM_Real)==0 ){
66044
+ if( (u.ag.flags & MEM_Real)==0 ){
6472766045
sqlite3VdbeIntegerAffinity(pOut);
6472866046
}
6472966047
#endif
6473066048
}
6473166049
break;
@@ -64766,96 +66084,96 @@
6476666084
** invocation of this opcode.
6476766085
**
6476866086
** See also: AggStep and AggFinal
6476966087
*/
6477066088
case OP_Function: {
64771
-#if 0 /* local variables moved into u.ag */
66089
+#if 0 /* local variables moved into u.ah */
6477266090
int i;
6477366091
Mem *pArg;
6477466092
sqlite3_context ctx;
6477566093
sqlite3_value **apVal;
6477666094
int n;
64777
-#endif /* local variables moved into u.ag */
66095
+#endif /* local variables moved into u.ah */
6477866096
64779
- u.ag.n = pOp->p5;
64780
- u.ag.apVal = p->apArg;
64781
- assert( u.ag.apVal || u.ag.n==0 );
66097
+ u.ah.n = pOp->p5;
66098
+ u.ah.apVal = p->apArg;
66099
+ assert( u.ah.apVal || u.ah.n==0 );
6478266100
assert( pOp->p3>0 && pOp->p3<=p->nMem );
6478366101
pOut = &aMem[pOp->p3];
6478466102
memAboutToChange(p, pOut);
6478566103
64786
- assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
64787
- assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
64788
- u.ag.pArg = &aMem[pOp->p2];
64789
- for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
64790
- assert( memIsValid(u.ag.pArg) );
64791
- u.ag.apVal[u.ag.i] = u.ag.pArg;
64792
- Deephemeralize(u.ag.pArg);
64793
- sqlite3VdbeMemStoreType(u.ag.pArg);
64794
- REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg);
66104
+ assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
66105
+ assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
66106
+ u.ah.pArg = &aMem[pOp->p2];
66107
+ for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
66108
+ assert( memIsValid(u.ah.pArg) );
66109
+ u.ah.apVal[u.ah.i] = u.ah.pArg;
66110
+ Deephemeralize(u.ah.pArg);
66111
+ sqlite3VdbeMemStoreType(u.ah.pArg);
66112
+ REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
6479566113
}
6479666114
6479766115
assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
6479866116
if( pOp->p4type==P4_FUNCDEF ){
64799
- u.ag.ctx.pFunc = pOp->p4.pFunc;
64800
- u.ag.ctx.pVdbeFunc = 0;
66117
+ u.ah.ctx.pFunc = pOp->p4.pFunc;
66118
+ u.ah.ctx.pVdbeFunc = 0;
6480166119
}else{
64802
- u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64803
- u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
66120
+ u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
66121
+ u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
6480466122
}
6480566123
64806
- u.ag.ctx.s.flags = MEM_Null;
64807
- u.ag.ctx.s.db = db;
64808
- u.ag.ctx.s.xDel = 0;
64809
- u.ag.ctx.s.zMalloc = 0;
66124
+ u.ah.ctx.s.flags = MEM_Null;
66125
+ u.ah.ctx.s.db = db;
66126
+ u.ah.ctx.s.xDel = 0;
66127
+ u.ah.ctx.s.zMalloc = 0;
6481066128
6481166129
/* The output cell may already have a buffer allocated. Move
64812
- ** the pointer to u.ag.ctx.s so in case the user-function can use
66130
+ ** the pointer to u.ah.ctx.s so in case the user-function can use
6481366131
** the already allocated buffer instead of allocating a new one.
6481466132
*/
64815
- sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
64816
- MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
66133
+ sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
66134
+ MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
6481766135
64818
- u.ag.ctx.isError = 0;
64819
- if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
66136
+ u.ah.ctx.isError = 0;
66137
+ if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6482066138
assert( pOp>aOp );
6482166139
assert( pOp[-1].p4type==P4_COLLSEQ );
6482266140
assert( pOp[-1].opcode==OP_CollSeq );
64823
- u.ag.ctx.pColl = pOp[-1].p4.pColl;
66141
+ u.ah.ctx.pColl = pOp[-1].p4.pColl;
6482466142
}
6482566143
db->lastRowid = lastRowid;
64826
- (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
66144
+ (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
6482766145
lastRowid = db->lastRowid;
6482866146
6482966147
/* If any auxiliary data functions have been called by this user function,
6483066148
** immediately call the destructor for any non-static values.
6483166149
*/
64832
- if( u.ag.ctx.pVdbeFunc ){
64833
- sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
64834
- pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
66150
+ if( u.ah.ctx.pVdbeFunc ){
66151
+ sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
66152
+ pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
6483566153
pOp->p4type = P4_VDBEFUNC;
6483666154
}
6483766155
6483866156
if( db->mallocFailed ){
6483966157
/* Even though a malloc() has failed, the implementation of the
6484066158
** user function may have called an sqlite3_result_XXX() function
6484166159
** to return a value. The following call releases any resources
6484266160
** associated with such a value.
6484366161
*/
64844
- sqlite3VdbeMemRelease(&u.ag.ctx.s);
66162
+ sqlite3VdbeMemRelease(&u.ah.ctx.s);
6484566163
goto no_mem;
6484666164
}
6484766165
6484866166
/* If the function returned an error, throw an exception */
64849
- if( u.ag.ctx.isError ){
64850
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
64851
- rc = u.ag.ctx.isError;
66167
+ if( u.ah.ctx.isError ){
66168
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
66169
+ rc = u.ah.ctx.isError;
6485266170
}
6485366171
6485466172
/* Copy the result of the function into register P3 */
64855
- sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
64856
- sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
66173
+ sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
66174
+ sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
6485766175
if( sqlite3VdbeMemTooBig(pOut) ){
6485866176
goto too_big;
6485966177
}
6486066178
6486166179
#if 0
@@ -64899,56 +66217,56 @@
6489966217
*/
6490066218
case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
6490166219
case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
6490266220
case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
6490366221
case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
64904
-#if 0 /* local variables moved into u.ah */
66222
+#if 0 /* local variables moved into u.ai */
6490566223
i64 iA;
6490666224
u64 uA;
6490766225
i64 iB;
6490866226
u8 op;
64909
-#endif /* local variables moved into u.ah */
66227
+#endif /* local variables moved into u.ai */
6491066228
6491166229
pIn1 = &aMem[pOp->p1];
6491266230
pIn2 = &aMem[pOp->p2];
6491366231
pOut = &aMem[pOp->p3];
6491466232
if( (pIn1->flags | pIn2->flags) & MEM_Null ){
6491566233
sqlite3VdbeMemSetNull(pOut);
6491666234
break;
6491766235
}
64918
- u.ah.iA = sqlite3VdbeIntValue(pIn2);
64919
- u.ah.iB = sqlite3VdbeIntValue(pIn1);
64920
- u.ah.op = pOp->opcode;
64921
- if( u.ah.op==OP_BitAnd ){
64922
- u.ah.iA &= u.ah.iB;
64923
- }else if( u.ah.op==OP_BitOr ){
64924
- u.ah.iA |= u.ah.iB;
64925
- }else if( u.ah.iB!=0 ){
64926
- assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
66236
+ u.ai.iA = sqlite3VdbeIntValue(pIn2);
66237
+ u.ai.iB = sqlite3VdbeIntValue(pIn1);
66238
+ u.ai.op = pOp->opcode;
66239
+ if( u.ai.op==OP_BitAnd ){
66240
+ u.ai.iA &= u.ai.iB;
66241
+ }else if( u.ai.op==OP_BitOr ){
66242
+ u.ai.iA |= u.ai.iB;
66243
+ }else if( u.ai.iB!=0 ){
66244
+ assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
6492766245
6492866246
/* If shifting by a negative amount, shift in the other direction */
64929
- if( u.ah.iB<0 ){
66247
+ if( u.ai.iB<0 ){
6493066248
assert( OP_ShiftRight==OP_ShiftLeft+1 );
64931
- u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
64932
- u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
66249
+ u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
66250
+ u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
6493366251
}
6493466252
64935
- if( u.ah.iB>=64 ){
64936
- u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
64937
- }else{
64938
- memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
64939
- if( u.ah.op==OP_ShiftLeft ){
64940
- u.ah.uA <<= u.ah.iB;
64941
- }else{
64942
- u.ah.uA >>= u.ah.iB;
66253
+ if( u.ai.iB>=64 ){
66254
+ u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
66255
+ }else{
66256
+ memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
66257
+ if( u.ai.op==OP_ShiftLeft ){
66258
+ u.ai.uA <<= u.ai.iB;
66259
+ }else{
66260
+ u.ai.uA >>= u.ai.iB;
6494366261
/* Sign-extend on a right shift of a negative number */
64944
- if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
66262
+ if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
6494566263
}
64946
- memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
66264
+ memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
6494766265
}
6494866266
}
64949
- pOut->u.i = u.ah.iA;
66267
+ pOut->u.i = u.ai.iA;
6495066268
MemSetTypeFlag(pOut, MEM_Int);
6495166269
break;
6495266270
}
6495366271
6495466272
/* Opcode: AddImm P1 P2 * * *
@@ -65185,30 +66503,30 @@
6518566503
case OP_Ne: /* same as TK_NE, jump, in1, in3 */
6518666504
case OP_Lt: /* same as TK_LT, jump, in1, in3 */
6518766505
case OP_Le: /* same as TK_LE, jump, in1, in3 */
6518866506
case OP_Gt: /* same as TK_GT, jump, in1, in3 */
6518966507
case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
65190
-#if 0 /* local variables moved into u.ai */
66508
+#if 0 /* local variables moved into u.aj */
6519166509
int res; /* Result of the comparison of pIn1 against pIn3 */
6519266510
char affinity; /* Affinity to use for comparison */
6519366511
u16 flags1; /* Copy of initial value of pIn1->flags */
6519466512
u16 flags3; /* Copy of initial value of pIn3->flags */
65195
-#endif /* local variables moved into u.ai */
66513
+#endif /* local variables moved into u.aj */
6519666514
6519766515
pIn1 = &aMem[pOp->p1];
6519866516
pIn3 = &aMem[pOp->p3];
65199
- u.ai.flags1 = pIn1->flags;
65200
- u.ai.flags3 = pIn3->flags;
65201
- if( (u.ai.flags1 | u.ai.flags3)&MEM_Null ){
66517
+ u.aj.flags1 = pIn1->flags;
66518
+ u.aj.flags3 = pIn3->flags;
66519
+ if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
6520266520
/* One or both operands are NULL */
6520366521
if( pOp->p5 & SQLITE_NULLEQ ){
6520466522
/* If SQLITE_NULLEQ is set (which will only happen if the operator is
6520566523
** OP_Eq or OP_Ne) then take the jump or not depending on whether
6520666524
** or not both operands are null.
6520766525
*/
6520866526
assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65209
- u.ai.res = (u.ai.flags1 & u.ai.flags3 & MEM_Null)==0;
66527
+ u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
6521066528
}else{
6521166529
/* SQLITE_NULLEQ is clear and at least one operand is NULL,
6521266530
** then the result is always NULL.
6521366531
** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
6521466532
*/
@@ -65221,44 +66539,44 @@
6522166539
}
6522266540
break;
6522366541
}
6522466542
}else{
6522566543
/* Neither operand is NULL. Do a comparison. */
65226
- u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
65227
- if( u.ai.affinity ){
65228
- applyAffinity(pIn1, u.ai.affinity, encoding);
65229
- applyAffinity(pIn3, u.ai.affinity, encoding);
66544
+ u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
66545
+ if( u.aj.affinity ){
66546
+ applyAffinity(pIn1, u.aj.affinity, encoding);
66547
+ applyAffinity(pIn3, u.aj.affinity, encoding);
6523066548
if( db->mallocFailed ) goto no_mem;
6523166549
}
6523266550
6523366551
assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
6523466552
ExpandBlob(pIn1);
6523566553
ExpandBlob(pIn3);
65236
- u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
66554
+ u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
6523766555
}
6523866556
switch( pOp->opcode ){
65239
- case OP_Eq: u.ai.res = u.ai.res==0; break;
65240
- case OP_Ne: u.ai.res = u.ai.res!=0; break;
65241
- case OP_Lt: u.ai.res = u.ai.res<0; break;
65242
- case OP_Le: u.ai.res = u.ai.res<=0; break;
65243
- case OP_Gt: u.ai.res = u.ai.res>0; break;
65244
- default: u.ai.res = u.ai.res>=0; break;
66557
+ case OP_Eq: u.aj.res = u.aj.res==0; break;
66558
+ case OP_Ne: u.aj.res = u.aj.res!=0; break;
66559
+ case OP_Lt: u.aj.res = u.aj.res<0; break;
66560
+ case OP_Le: u.aj.res = u.aj.res<=0; break;
66561
+ case OP_Gt: u.aj.res = u.aj.res>0; break;
66562
+ default: u.aj.res = u.aj.res>=0; break;
6524566563
}
6524666564
6524766565
if( pOp->p5 & SQLITE_STOREP2 ){
6524866566
pOut = &aMem[pOp->p2];
6524966567
memAboutToChange(p, pOut);
6525066568
MemSetTypeFlag(pOut, MEM_Int);
65251
- pOut->u.i = u.ai.res;
66569
+ pOut->u.i = u.aj.res;
6525266570
REGISTER_TRACE(pOp->p2, pOut);
65253
- }else if( u.ai.res ){
66571
+ }else if( u.aj.res ){
6525466572
pc = pOp->p2-1;
6525566573
}
6525666574
6525766575
/* Undo any changes made by applyAffinity() to the input registers. */
65258
- pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ai.flags1&MEM_TypeMask);
65259
- pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ai.flags3&MEM_TypeMask);
66576
+ pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
66577
+ pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
6526066578
break;
6526166579
}
6526266580
6526366581
/* Opcode: Permutation * * * P4 *
6526466582
**
@@ -65289,50 +66607,50 @@
6528966607
** The comparison is a sort comparison, so NULLs compare equal,
6529066608
** NULLs are less than numbers, numbers are less than strings,
6529166609
** and strings are less than blobs.
6529266610
*/
6529366611
case OP_Compare: {
65294
-#if 0 /* local variables moved into u.aj */
66612
+#if 0 /* local variables moved into u.ak */
6529566613
int n;
6529666614
int i;
6529766615
int p1;
6529866616
int p2;
6529966617
const KeyInfo *pKeyInfo;
6530066618
int idx;
6530166619
CollSeq *pColl; /* Collating sequence to use on this term */
6530266620
int bRev; /* True for DESCENDING sort order */
65303
-#endif /* local variables moved into u.aj */
65304
-
65305
- u.aj.n = pOp->p3;
65306
- u.aj.pKeyInfo = pOp->p4.pKeyInfo;
65307
- assert( u.aj.n>0 );
65308
- assert( u.aj.pKeyInfo!=0 );
65309
- u.aj.p1 = pOp->p1;
65310
- u.aj.p2 = pOp->p2;
66621
+#endif /* local variables moved into u.ak */
66622
+
66623
+ u.ak.n = pOp->p3;
66624
+ u.ak.pKeyInfo = pOp->p4.pKeyInfo;
66625
+ assert( u.ak.n>0 );
66626
+ assert( u.ak.pKeyInfo!=0 );
66627
+ u.ak.p1 = pOp->p1;
66628
+ u.ak.p2 = pOp->p2;
6531166629
#if SQLITE_DEBUG
6531266630
if( aPermute ){
6531366631
int k, mx = 0;
65314
- for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65315
- assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
65316
- assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
66632
+ for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
66633
+ assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
66634
+ assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
6531766635
}else{
65318
- assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
65319
- assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
66636
+ assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
66637
+ assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
6532066638
}
6532166639
#endif /* SQLITE_DEBUG */
65322
- for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
65323
- u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
65324
- assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) );
65325
- assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) );
65326
- REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]);
65327
- REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]);
65328
- assert( u.aj.i<u.aj.pKeyInfo->nField );
65329
- u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
65330
- u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
65331
- iCompare = sqlite3MemCompare(&aMem[u.aj.p1+u.aj.idx], &aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
66640
+ for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
66641
+ u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
66642
+ assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
66643
+ assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
66644
+ REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
66645
+ REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
66646
+ assert( u.ak.i<u.ak.pKeyInfo->nField );
66647
+ u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
66648
+ u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
66649
+ iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
6533266650
if( iCompare ){
65333
- if( u.aj.bRev ) iCompare = -iCompare;
66651
+ if( u.ak.bRev ) iCompare = -iCompare;
6533466652
break;
6533566653
}
6533666654
}
6533766655
aPermute = 0;
6533866656
break;
@@ -65373,39 +66691,39 @@
6537366691
** even if the other input is NULL. A NULL and false or two NULLs
6537466692
** give a NULL output.
6537566693
*/
6537666694
case OP_And: /* same as TK_AND, in1, in2, out3 */
6537766695
case OP_Or: { /* same as TK_OR, in1, in2, out3 */
65378
-#if 0 /* local variables moved into u.ak */
66696
+#if 0 /* local variables moved into u.al */
6537966697
int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
6538066698
int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65381
-#endif /* local variables moved into u.ak */
66699
+#endif /* local variables moved into u.al */
6538266700
6538366701
pIn1 = &aMem[pOp->p1];
6538466702
if( pIn1->flags & MEM_Null ){
65385
- u.ak.v1 = 2;
66703
+ u.al.v1 = 2;
6538666704
}else{
65387
- u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
66705
+ u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
6538866706
}
6538966707
pIn2 = &aMem[pOp->p2];
6539066708
if( pIn2->flags & MEM_Null ){
65391
- u.ak.v2 = 2;
66709
+ u.al.v2 = 2;
6539266710
}else{
65393
- u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
66711
+ u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
6539466712
}
6539566713
if( pOp->opcode==OP_And ){
6539666714
static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65397
- u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
66715
+ u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
6539866716
}else{
6539966717
static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65400
- u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
66718
+ u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
6540166719
}
6540266720
pOut = &aMem[pOp->p3];
65403
- if( u.ak.v1==2 ){
66721
+ if( u.al.v1==2 ){
6540466722
MemSetTypeFlag(pOut, MEM_Null);
6540566723
}else{
65406
- pOut->u.i = u.ak.v1;
66724
+ pOut->u.i = u.al.v1;
6540766725
MemSetTypeFlag(pOut, MEM_Int);
6540866726
}
6540966727
break;
6541066728
}
6541166729
@@ -65443,55 +66761,55 @@
6544366761
break;
6544466762
}
6544566763
6544666764
/* Opcode: Once P1 P2 * * *
6544766765
**
65448
-** Jump to P2 if the value in register P1 is a not null or zero. If
65449
-** the value is NULL or zero, fall through and change the P1 register
65450
-** to an integer 1.
66766
+** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
66767
+** set the flag and fall through to the next instruction.
6545166768
**
65452
-** When P1 is not used otherwise in a program, this opcode falls through
65453
-** once and jumps on all subsequent invocations. It is the equivalent
65454
-** of "OP_If P1 P2", followed by "OP_Integer 1 P1".
66769
+** See also: JumpOnce
6545566770
*/
66771
+case OP_Once: { /* jump */
66772
+ assert( pOp->p1<p->nOnceFlag );
66773
+ if( p->aOnceFlag[pOp->p1] ){
66774
+ pc = pOp->p2-1;
66775
+ }else{
66776
+ p->aOnceFlag[pOp->p1] = 1;
66777
+ }
66778
+ break;
66779
+}
66780
+
6545666781
/* Opcode: If P1 P2 P3 * *
6545766782
**
6545866783
** Jump to P2 if the value in register P1 is true. The value
6545966784
** is considered true if it is numeric and non-zero. If the value
65460
-** in P1 is NULL then take the jump if P3 is true.
66785
+** in P1 is NULL then take the jump if P3 is non-zero.
6546166786
*/
6546266787
/* Opcode: IfNot P1 P2 P3 * *
6546366788
**
6546466789
** Jump to P2 if the value in register P1 is False. The value
65465
-** is considered true if it has a numeric value of zero. If the value
65466
-** in P1 is NULL then take the jump if P3 is true.
66790
+** is considered false if it has a numeric value of zero. If the value
66791
+** in P1 is NULL then take the jump if P3 is zero.
6546766792
*/
65468
-case OP_Once: /* jump, in1 */
6546966793
case OP_If: /* jump, in1 */
6547066794
case OP_IfNot: { /* jump, in1 */
65471
-#if 0 /* local variables moved into u.al */
66795
+#if 0 /* local variables moved into u.am */
6547266796
int c;
65473
-#endif /* local variables moved into u.al */
66797
+#endif /* local variables moved into u.am */
6547466798
pIn1 = &aMem[pOp->p1];
6547566799
if( pIn1->flags & MEM_Null ){
65476
- u.al.c = pOp->p3;
66800
+ u.am.c = pOp->p3;
6547766801
}else{
6547866802
#ifdef SQLITE_OMIT_FLOATING_POINT
65479
- u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
66803
+ u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
6548066804
#else
65481
- u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
66805
+ u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
6548266806
#endif
65483
- if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
65484
- }
65485
- if( u.al.c ){
65486
- pc = pOp->p2-1;
65487
- }else if( pOp->opcode==OP_Once ){
65488
- assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 );
65489
- memAboutToChange(p, pIn1);
65490
- pIn1->flags = MEM_Int;
65491
- pIn1->u.i = 1;
65492
- REGISTER_TRACE(pOp->p1, pIn1);
66807
+ if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
66808
+ }
66809
+ if( u.am.c ){
66810
+ pc = pOp->p2-1;
6549366811
}
6549466812
break;
6549566813
}
6549666814
6549766815
/* Opcode: IsNull P1 P2 * * *
@@ -65536,11 +66854,11 @@
6553666854
** then the cache of the cursor is reset prior to extracting the column.
6553766855
** The first OP_Column against a pseudo-table after the value of the content
6553866856
** register has changed should have this bit set.
6553966857
*/
6554066858
case OP_Column: {
65541
-#if 0 /* local variables moved into u.am */
66859
+#if 0 /* local variables moved into u.an */
6554266860
u32 payloadSize; /* Number of bytes in the record */
6554366861
i64 payloadSize64; /* Number of bytes in the record */
6554466862
int p1; /* P1 value of the opcode */
6554566863
int p2; /* column number to retrieve */
6554666864
VdbeCursor *pC; /* The VDBE cursor */
@@ -65560,130 +66878,130 @@
6556066878
u32 szField; /* Number of bytes in the content of a field */
6556166879
int szHdr; /* Size of the header size field at start of record */
6556266880
int avail; /* Number of bytes of available data */
6556366881
u32 t; /* A type code from the record header */
6556466882
Mem *pReg; /* PseudoTable input register */
65565
-#endif /* local variables moved into u.am */
66883
+#endif /* local variables moved into u.an */
6556666884
6556766885
65568
- u.am.p1 = pOp->p1;
65569
- u.am.p2 = pOp->p2;
65570
- u.am.pC = 0;
65571
- memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65572
- assert( u.am.p1<p->nCursor );
66886
+ u.an.p1 = pOp->p1;
66887
+ u.an.p2 = pOp->p2;
66888
+ u.an.pC = 0;
66889
+ memset(&u.an.sMem, 0, sizeof(u.an.sMem));
66890
+ assert( u.an.p1<p->nCursor );
6557366891
assert( pOp->p3>0 && pOp->p3<=p->nMem );
65574
- u.am.pDest = &aMem[pOp->p3];
65575
- memAboutToChange(p, u.am.pDest);
65576
- u.am.zRec = 0;
66892
+ u.an.pDest = &aMem[pOp->p3];
66893
+ memAboutToChange(p, u.an.pDest);
66894
+ u.an.zRec = 0;
6557766895
65578
- /* This block sets the variable u.am.payloadSize to be the total number of
66896
+ /* This block sets the variable u.an.payloadSize to be the total number of
6557966897
** bytes in the record.
6558066898
**
65581
- ** u.am.zRec is set to be the complete text of the record if it is available.
66899
+ ** u.an.zRec is set to be the complete text of the record if it is available.
6558266900
** The complete record text is always available for pseudo-tables
6558366901
** If the record is stored in a cursor, the complete record text
65584
- ** might be available in the u.am.pC->aRow cache. Or it might not be.
65585
- ** If the data is unavailable, u.am.zRec is set to NULL.
66902
+ ** might be available in the u.an.pC->aRow cache. Or it might not be.
66903
+ ** If the data is unavailable, u.an.zRec is set to NULL.
6558666904
**
6558766905
** We also compute the number of columns in the record. For cursors,
6558866906
** the number of columns is stored in the VdbeCursor.nField element.
6558966907
*/
65590
- u.am.pC = p->apCsr[u.am.p1];
65591
- assert( u.am.pC!=0 );
66908
+ u.an.pC = p->apCsr[u.an.p1];
66909
+ assert( u.an.pC!=0 );
6559266910
#ifndef SQLITE_OMIT_VIRTUALTABLE
65593
- assert( u.am.pC->pVtabCursor==0 );
66911
+ assert( u.an.pC->pVtabCursor==0 );
6559466912
#endif
65595
- u.am.pCrsr = u.am.pC->pCursor;
65596
- if( u.am.pCrsr!=0 ){
66913
+ u.an.pCrsr = u.an.pC->pCursor;
66914
+ if( u.an.pCrsr!=0 ){
6559766915
/* The record is stored in a B-Tree */
65598
- rc = sqlite3VdbeCursorMoveto(u.am.pC);
66916
+ rc = sqlite3VdbeCursorMoveto(u.an.pC);
6559966917
if( rc ) goto abort_due_to_error;
65600
- if( u.am.pC->nullRow ){
65601
- u.am.payloadSize = 0;
65602
- }else if( u.am.pC->cacheStatus==p->cacheCtr ){
65603
- u.am.payloadSize = u.am.pC->payloadSize;
65604
- u.am.zRec = (char*)u.am.pC->aRow;
65605
- }else if( u.am.pC->isIndex ){
65606
- assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65607
- VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
66918
+ if( u.an.pC->nullRow ){
66919
+ u.an.payloadSize = 0;
66920
+ }else if( u.an.pC->cacheStatus==p->cacheCtr ){
66921
+ u.an.payloadSize = u.an.pC->payloadSize;
66922
+ u.an.zRec = (char*)u.an.pC->aRow;
66923
+ }else if( u.an.pC->isIndex ){
66924
+ assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
66925
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
6560866926
assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
6560966927
/* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65610
- ** payload size, so it is impossible for u.am.payloadSize64 to be
66928
+ ** payload size, so it is impossible for u.an.payloadSize64 to be
6561166929
** larger than 32 bits. */
65612
- assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
65613
- u.am.payloadSize = (u32)u.am.payloadSize64;
66930
+ assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
66931
+ u.an.payloadSize = (u32)u.an.payloadSize64;
6561466932
}else{
65615
- assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65616
- VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
66933
+ assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
66934
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
6561766935
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6561866936
}
65619
- }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65620
- u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65621
- assert( u.am.pReg->flags & MEM_Blob );
65622
- assert( memIsValid(u.am.pReg) );
65623
- u.am.payloadSize = u.am.pReg->n;
65624
- u.am.zRec = u.am.pReg->z;
65625
- u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65626
- assert( u.am.payloadSize==0 || u.am.zRec!=0 );
66937
+ }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
66938
+ u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
66939
+ assert( u.an.pReg->flags & MEM_Blob );
66940
+ assert( memIsValid(u.an.pReg) );
66941
+ u.an.payloadSize = u.an.pReg->n;
66942
+ u.an.zRec = u.an.pReg->z;
66943
+ u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
66944
+ assert( u.an.payloadSize==0 || u.an.zRec!=0 );
6562766945
}else{
6562866946
/* Consider the row to be NULL */
65629
- u.am.payloadSize = 0;
66947
+ u.an.payloadSize = 0;
6563066948
}
6563166949
65632
- /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
66950
+ /* If u.an.payloadSize is 0, then just store a NULL. This can happen because of
6563366951
** nullRow or because of a corrupt database. */
65634
- if( u.am.payloadSize==0 ){
65635
- MemSetTypeFlag(u.am.pDest, MEM_Null);
66952
+ if( u.an.payloadSize==0 ){
66953
+ MemSetTypeFlag(u.an.pDest, MEM_Null);
6563666954
goto op_column_out;
6563766955
}
6563866956
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65639
- if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
66957
+ if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
6564066958
goto too_big;
6564166959
}
6564266960
65643
- u.am.nField = u.am.pC->nField;
65644
- assert( u.am.p2<u.am.nField );
66961
+ u.an.nField = u.an.pC->nField;
66962
+ assert( u.an.p2<u.an.nField );
6564566963
6564666964
/* Read and parse the table header. Store the results of the parse
6564766965
** into the record header cache fields of the cursor.
6564866966
*/
65649
- u.am.aType = u.am.pC->aType;
65650
- if( u.am.pC->cacheStatus==p->cacheCtr ){
65651
- u.am.aOffset = u.am.pC->aOffset;
66967
+ u.an.aType = u.an.pC->aType;
66968
+ if( u.an.pC->cacheStatus==p->cacheCtr ){
66969
+ u.an.aOffset = u.an.pC->aOffset;
6565266970
}else{
65653
- assert(u.am.aType);
65654
- u.am.avail = 0;
65655
- u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
65656
- u.am.pC->payloadSize = u.am.payloadSize;
65657
- u.am.pC->cacheStatus = p->cacheCtr;
66971
+ assert(u.an.aType);
66972
+ u.an.avail = 0;
66973
+ u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
66974
+ u.an.pC->payloadSize = u.an.payloadSize;
66975
+ u.an.pC->cacheStatus = p->cacheCtr;
6565866976
6565966977
/* Figure out how many bytes are in the header */
65660
- if( u.am.zRec ){
65661
- u.am.zData = u.am.zRec;
66978
+ if( u.an.zRec ){
66979
+ u.an.zData = u.an.zRec;
6566266980
}else{
65663
- if( u.am.pC->isIndex ){
65664
- u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
66981
+ if( u.an.pC->isIndex ){
66982
+ u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
6566566983
}else{
65666
- u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
66984
+ u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
6566766985
}
6566866986
/* If KeyFetch()/DataFetch() managed to get the entire payload,
65669
- ** save the payload in the u.am.pC->aRow cache. That will save us from
66987
+ ** save the payload in the u.an.pC->aRow cache. That will save us from
6567066988
** having to make additional calls to fetch the content portion of
6567166989
** the record.
6567266990
*/
65673
- assert( u.am.avail>=0 );
65674
- if( u.am.payloadSize <= (u32)u.am.avail ){
65675
- u.am.zRec = u.am.zData;
65676
- u.am.pC->aRow = (u8*)u.am.zData;
66991
+ assert( u.an.avail>=0 );
66992
+ if( u.an.payloadSize <= (u32)u.an.avail ){
66993
+ u.an.zRec = u.an.zData;
66994
+ u.an.pC->aRow = (u8*)u.an.zData;
6567766995
}else{
65678
- u.am.pC->aRow = 0;
66996
+ u.an.pC->aRow = 0;
6567966997
}
6568066998
}
6568166999
/* The following assert is true in all cases accept when
6568267000
** the database file has been corrupted externally.
65683
- ** assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
65684
- u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
67001
+ ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
67002
+ u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
6568567003
6568667004
/* Make sure a corrupt database has not given us an oversize header.
6568767005
** Do this now to avoid an oversize memory allocation.
6568867006
**
6568967007
** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65690,146 +67008,146 @@
6569067008
** types use so much data space that there can only be 4096 and 32 of
6569167009
** them, respectively. So the maximum header length results from a
6569267010
** 3-byte type for each of the maximum of 32768 columns plus three
6569367011
** extra bytes for the header length itself. 32768*3 + 3 = 98307.
6569467012
*/
65695
- if( u.am.offset > 98307 ){
67013
+ if( u.an.offset > 98307 ){
6569667014
rc = SQLITE_CORRUPT_BKPT;
6569767015
goto op_column_out;
6569867016
}
6569967017
65700
- /* Compute in u.am.len the number of bytes of data we need to read in order
65701
- ** to get u.am.nField type values. u.am.offset is an upper bound on this. But
65702
- ** u.am.nField might be significantly less than the true number of columns
65703
- ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
65704
- ** We want to minimize u.am.len in order to limit the size of the memory
65705
- ** allocation, especially if a corrupt database file has caused u.am.offset
67018
+ /* Compute in u.an.len the number of bytes of data we need to read in order
67019
+ ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
67020
+ ** u.an.nField might be significantly less than the true number of columns
67021
+ ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
67022
+ ** We want to minimize u.an.len in order to limit the size of the memory
67023
+ ** allocation, especially if a corrupt database file has caused u.an.offset
6570667024
** to be oversized. Offset is limited to 98307 above. But 98307 might
6570767025
** still exceed Robson memory allocation limits on some configurations.
65708
- ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
65709
- ** will likely be much smaller since u.am.nField will likely be less than
67026
+ ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
67027
+ ** will likely be much smaller since u.an.nField will likely be less than
6571067028
** 20 or so. This insures that Robson memory allocation limits are
6571167029
** not exceeded even for corrupt database files.
6571267030
*/
65713
- u.am.len = u.am.nField*5 + 3;
65714
- if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
67031
+ u.an.len = u.an.nField*5 + 3;
67032
+ if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
6571567033
6571667034
/* The KeyFetch() or DataFetch() above are fast and will get the entire
6571767035
** record header in most cases. But they will fail to get the complete
6571867036
** record header if the record header does not fit on a single page
6571967037
** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
6572067038
** acquire the complete header text.
6572167039
*/
65722
- if( !u.am.zRec && u.am.avail<u.am.len ){
65723
- u.am.sMem.flags = 0;
65724
- u.am.sMem.db = 0;
65725
- rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
67040
+ if( !u.an.zRec && u.an.avail<u.an.len ){
67041
+ u.an.sMem.flags = 0;
67042
+ u.an.sMem.db = 0;
67043
+ rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
6572667044
if( rc!=SQLITE_OK ){
6572767045
goto op_column_out;
6572867046
}
65729
- u.am.zData = u.am.sMem.z;
65730
- }
65731
- u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
65732
- u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
65733
-
65734
- /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
65735
- ** arrays. u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
65736
- ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
65737
- ** of the record to the start of the data for the u.am.i-th column
65738
- */
65739
- for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65740
- if( u.am.zIdx<u.am.zEndHdr ){
65741
- u.am.aOffset[u.am.i] = u.am.offset;
65742
- if( u.am.zIdx[0]<0x80 ){
65743
- u.am.t = u.am.zIdx[0];
65744
- u.am.zIdx++;
67047
+ u.an.zData = u.an.sMem.z;
67048
+ }
67049
+ u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
67050
+ u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
67051
+
67052
+ /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
67053
+ ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
67054
+ ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
67055
+ ** of the record to the start of the data for the u.an.i-th column
67056
+ */
67057
+ for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
67058
+ if( u.an.zIdx<u.an.zEndHdr ){
67059
+ u.an.aOffset[u.an.i] = u.an.offset;
67060
+ if( u.an.zIdx[0]<0x80 ){
67061
+ u.an.t = u.an.zIdx[0];
67062
+ u.an.zIdx++;
6574567063
}else{
65746
- u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
67064
+ u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
6574767065
}
65748
- u.am.aType[u.am.i] = u.am.t;
65749
- u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
65750
- u.am.offset += u.am.szField;
65751
- if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65752
- u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
67066
+ u.an.aType[u.an.i] = u.an.t;
67067
+ u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
67068
+ u.an.offset += u.an.szField;
67069
+ if( u.an.offset<u.an.szField ){ /* True if u.an.offset overflows */
67070
+ u.an.zIdx = &u.an.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
6575367071
break;
6575467072
}
6575567073
}else{
65756
- /* If u.am.i is less that u.am.nField, then there are less fields in this
67074
+ /* If u.an.i is less that u.an.nField, then there are less fields in this
6575767075
** record than SetNumColumns indicated there are columns in the
65758
- ** table. Set the u.am.offset for any extra columns not present in
67076
+ ** table. Set the u.an.offset for any extra columns not present in
6575967077
** the record to 0. This tells code below to store a NULL
6576067078
** instead of deserializing a value from the record.
6576167079
*/
65762
- u.am.aOffset[u.am.i] = 0;
67080
+ u.an.aOffset[u.an.i] = 0;
6576367081
}
6576467082
}
65765
- sqlite3VdbeMemRelease(&u.am.sMem);
65766
- u.am.sMem.flags = MEM_Null;
67083
+ sqlite3VdbeMemRelease(&u.an.sMem);
67084
+ u.an.sMem.flags = MEM_Null;
6576767085
6576867086
/* If we have read more header data than was contained in the header,
6576967087
** or if the end of the last field appears to be past the end of the
6577067088
** record, or if the end of the last field appears to be before the end
6577167089
** of the record (when all fields present), then we must be dealing
6577267090
** with a corrupt database.
6577367091
*/
65774
- if( (u.am.zIdx > u.am.zEndHdr) || (u.am.offset > u.am.payloadSize)
65775
- || (u.am.zIdx==u.am.zEndHdr && u.am.offset!=u.am.payloadSize) ){
67092
+ if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
67093
+ || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
6577667094
rc = SQLITE_CORRUPT_BKPT;
6577767095
goto op_column_out;
6577867096
}
6577967097
}
6578067098
65781
- /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
65782
- ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
67099
+ /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
67100
+ ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
6578367101
** then there are not enough fields in the record to satisfy the
6578467102
** request. In this case, set the value NULL or to P4 if P4 is
6578567103
** a pointer to a Mem object.
6578667104
*/
65787
- if( u.am.aOffset[u.am.p2] ){
67105
+ if( u.an.aOffset[u.an.p2] ){
6578867106
assert( rc==SQLITE_OK );
65789
- if( u.am.zRec ){
65790
- MemReleaseExt(u.am.pDest);
65791
- sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
67107
+ if( u.an.zRec ){
67108
+ VdbeMemRelease(u.an.pDest);
67109
+ sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
6579267110
}else{
65793
- u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65794
- sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65795
- rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
67111
+ u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
67112
+ sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
67113
+ rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
6579667114
if( rc!=SQLITE_OK ){
6579767115
goto op_column_out;
6579867116
}
65799
- u.am.zData = u.am.sMem.z;
65800
- sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
67117
+ u.an.zData = u.an.sMem.z;
67118
+ sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
6580167119
}
65802
- u.am.pDest->enc = encoding;
67120
+ u.an.pDest->enc = encoding;
6580367121
}else{
6580467122
if( pOp->p4type==P4_MEM ){
65805
- sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
67123
+ sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
6580667124
}else{
65807
- MemSetTypeFlag(u.am.pDest, MEM_Null);
67125
+ MemSetTypeFlag(u.an.pDest, MEM_Null);
6580867126
}
6580967127
}
6581067128
6581167129
/* If we dynamically allocated space to hold the data (in the
6581267130
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65813
- ** dynamically allocated space over to the u.am.pDest structure.
67131
+ ** dynamically allocated space over to the u.an.pDest structure.
6581467132
** This prevents a memory copy.
6581567133
*/
65816
- if( u.am.sMem.zMalloc ){
65817
- assert( u.am.sMem.z==u.am.sMem.zMalloc );
65818
- assert( !(u.am.pDest->flags & MEM_Dyn) );
65819
- assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
65820
- u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65821
- u.am.pDest->flags |= MEM_Term;
65822
- u.am.pDest->z = u.am.sMem.z;
65823
- u.am.pDest->zMalloc = u.am.sMem.zMalloc;
67134
+ if( u.an.sMem.zMalloc ){
67135
+ assert( u.an.sMem.z==u.an.sMem.zMalloc );
67136
+ assert( !(u.an.pDest->flags & MEM_Dyn) );
67137
+ assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
67138
+ u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
67139
+ u.an.pDest->flags |= MEM_Term;
67140
+ u.an.pDest->z = u.an.sMem.z;
67141
+ u.an.pDest->zMalloc = u.an.sMem.zMalloc;
6582467142
}
6582567143
65826
- rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
67144
+ rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
6582767145
6582867146
op_column_out:
65829
- UPDATE_MAX_BLOBSIZE(u.am.pDest);
65830
- REGISTER_TRACE(pOp->p3, u.am.pDest);
67147
+ UPDATE_MAX_BLOBSIZE(u.an.pDest);
67148
+ REGISTER_TRACE(pOp->p3, u.an.pDest);
6583167149
break;
6583267150
}
6583367151
6583467152
/* Opcode: Affinity P1 P2 * P4 *
6583567153
**
@@ -65838,24 +67156,24 @@
6583867156
** P4 is a string that is P2 characters long. The nth character of the
6583967157
** string indicates the column affinity that should be used for the nth
6584067158
** memory cell in the range.
6584167159
*/
6584267160
case OP_Affinity: {
65843
-#if 0 /* local variables moved into u.an */
67161
+#if 0 /* local variables moved into u.ao */
6584467162
const char *zAffinity; /* The affinity to be applied */
6584567163
char cAff; /* A single character of affinity */
65846
-#endif /* local variables moved into u.an */
67164
+#endif /* local variables moved into u.ao */
6584767165
65848
- u.an.zAffinity = pOp->p4.z;
65849
- assert( u.an.zAffinity!=0 );
65850
- assert( u.an.zAffinity[pOp->p2]==0 );
67166
+ u.ao.zAffinity = pOp->p4.z;
67167
+ assert( u.ao.zAffinity!=0 );
67168
+ assert( u.ao.zAffinity[pOp->p2]==0 );
6585167169
pIn1 = &aMem[pOp->p1];
65852
- while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){
67170
+ while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
6585367171
assert( pIn1 <= &p->aMem[p->nMem] );
6585467172
assert( memIsValid(pIn1) );
6585567173
ExpandBlob(pIn1);
65856
- applyAffinity(pIn1, u.an.cAff, encoding);
67174
+ applyAffinity(pIn1, u.ao.cAff, encoding);
6585767175
pIn1++;
6585867176
}
6585967177
break;
6586067178
}
6586167179
@@ -65873,11 +67191,11 @@
6587367191
** macros defined in sqliteInt.h.
6587467192
**
6587567193
** If P4 is NULL then all index fields have the affinity NONE.
6587667194
*/
6587767195
case OP_MakeRecord: {
65878
-#if 0 /* local variables moved into u.ao */
67196
+#if 0 /* local variables moved into u.ap */
6587967197
u8 *zNewRecord; /* A buffer to hold the data for the new record */
6588067198
Mem *pRec; /* The new record */
6588167199
u64 nData; /* Number of bytes of data space */
6588267200
int nHdr; /* Number of bytes of header space */
6588367201
i64 nByte; /* Data space required for this record */
@@ -65889,11 +67207,11 @@
6588967207
int nField; /* Number of fields in the record */
6589067208
char *zAffinity; /* The affinity string for the record */
6589167209
int file_format; /* File format to use for encoding */
6589267210
int i; /* Space used in zNewRecord[] */
6589367211
int len; /* Length of a field */
65894
-#endif /* local variables moved into u.ao */
67212
+#endif /* local variables moved into u.ap */
6589567213
6589667214
/* Assuming the record contains N fields, the record format looks
6589767215
** like this:
6589867216
**
6589967217
** ------------------------------------------------------------------------
@@ -65906,87 +67224,87 @@
6590667224
** Each type field is a varint representing the serial type of the
6590767225
** corresponding data element (see sqlite3VdbeSerialType()). The
6590867226
** hdr-size field is also a varint which is the offset from the beginning
6590967227
** of the record to data0.
6591067228
*/
65911
- u.ao.nData = 0; /* Number of bytes of data space */
65912
- u.ao.nHdr = 0; /* Number of bytes of header space */
65913
- u.ao.nZero = 0; /* Number of zero bytes at the end of the record */
65914
- u.ao.nField = pOp->p1;
65915
- u.ao.zAffinity = pOp->p4.z;
65916
- assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
65917
- u.ao.pData0 = &aMem[u.ao.nField];
65918
- u.ao.nField = pOp->p2;
65919
- u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
65920
- u.ao.file_format = p->minWriteFileFormat;
67229
+ u.ap.nData = 0; /* Number of bytes of data space */
67230
+ u.ap.nHdr = 0; /* Number of bytes of header space */
67231
+ u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
67232
+ u.ap.nField = pOp->p1;
67233
+ u.ap.zAffinity = pOp->p4.z;
67234
+ assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
67235
+ u.ap.pData0 = &aMem[u.ap.nField];
67236
+ u.ap.nField = pOp->p2;
67237
+ u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
67238
+ u.ap.file_format = p->minWriteFileFormat;
6592167239
6592267240
/* Identify the output register */
6592367241
assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
6592467242
pOut = &aMem[pOp->p3];
6592567243
memAboutToChange(p, pOut);
6592667244
6592767245
/* Loop through the elements that will make up the record to figure
6592867246
** out how much space is required for the new record.
6592967247
*/
65930
- for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65931
- assert( memIsValid(u.ao.pRec) );
65932
- if( u.ao.zAffinity ){
65933
- applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
65934
- }
65935
- if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
65936
- sqlite3VdbeMemExpandBlob(u.ao.pRec);
65937
- }
65938
- u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65939
- u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
65940
- u.ao.nData += u.ao.len;
65941
- u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
65942
- if( u.ao.pRec->flags & MEM_Zero ){
67248
+ for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
67249
+ assert( memIsValid(u.ap.pRec) );
67250
+ if( u.ap.zAffinity ){
67251
+ applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
67252
+ }
67253
+ if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
67254
+ sqlite3VdbeMemExpandBlob(u.ap.pRec);
67255
+ }
67256
+ u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
67257
+ u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
67258
+ u.ap.nData += u.ap.len;
67259
+ u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
67260
+ if( u.ap.pRec->flags & MEM_Zero ){
6594367261
/* Only pure zero-filled BLOBs can be input to this Opcode.
6594467262
** We do not allow blobs with a prefix and a zero-filled tail. */
65945
- u.ao.nZero += u.ao.pRec->u.nZero;
65946
- }else if( u.ao.len ){
65947
- u.ao.nZero = 0;
67263
+ u.ap.nZero += u.ap.pRec->u.nZero;
67264
+ }else if( u.ap.len ){
67265
+ u.ap.nZero = 0;
6594867266
}
6594967267
}
6595067268
6595167269
/* Add the initial header varint and total the size */
65952
- u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
65953
- if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
65954
- u.ao.nHdr++;
67270
+ u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
67271
+ if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
67272
+ u.ap.nHdr++;
6595567273
}
65956
- u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
65957
- if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67274
+ u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
67275
+ if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6595867276
goto too_big;
6595967277
}
6596067278
6596167279
/* Make sure the output register has a buffer large enough to store
6596267280
** the new record. The output register (pOp->p3) is not allowed to
6596367281
** be one of the input registers (because the following call to
6596467282
** sqlite3VdbeMemGrow() could clobber the value before it is used).
6596567283
*/
65966
- if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
67284
+ if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
6596767285
goto no_mem;
6596867286
}
65969
- u.ao.zNewRecord = (u8 *)pOut->z;
67287
+ u.ap.zNewRecord = (u8 *)pOut->z;
6597067288
6597167289
/* Write the record */
65972
- u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
65973
- for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65974
- u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65975
- u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type); /* serial type */
65976
- }
65977
- for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ /* serial data */
65978
- u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
65979
- }
65980
- assert( u.ao.i==u.ao.nByte );
67290
+ u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
67291
+ for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
67292
+ u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
67293
+ u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
67294
+ }
67295
+ for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
67296
+ u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
67297
+ }
67298
+ assert( u.ap.i==u.ap.nByte );
6598167299
6598267300
assert( pOp->p3>0 && pOp->p3<=p->nMem );
65983
- pOut->n = (int)u.ao.nByte;
67301
+ pOut->n = (int)u.ap.nByte;
6598467302
pOut->flags = MEM_Blob | MEM_Dyn;
6598567303
pOut->xDel = 0;
65986
- if( u.ao.nZero ){
65987
- pOut->u.nZero = u.ao.nZero;
67304
+ if( u.ap.nZero ){
67305
+ pOut->u.nZero = u.ap.nZero;
6598867306
pOut->flags |= MEM_Zero;
6598967307
}
6599067308
pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
6599167309
REGISTER_TRACE(pOp->p3, pOut);
6599267310
UPDATE_MAX_BLOBSIZE(pOut);
@@ -65998,22 +67316,22 @@
6599867316
** Store the number of entries (an integer value) in the table or index
6599967317
** opened by cursor P1 in register P2
6600067318
*/
6600167319
#ifndef SQLITE_OMIT_BTREECOUNT
6600267320
case OP_Count: { /* out2-prerelease */
66003
-#if 0 /* local variables moved into u.ap */
67321
+#if 0 /* local variables moved into u.aq */
6600467322
i64 nEntry;
6600567323
BtCursor *pCrsr;
66006
-#endif /* local variables moved into u.ap */
66007
-
66008
- u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
66009
- if( ALWAYS(u.ap.pCrsr) ){
66010
- rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
66011
- }else{
66012
- u.ap.nEntry = 0;
66013
- }
66014
- pOut->u.i = u.ap.nEntry;
67324
+#endif /* local variables moved into u.aq */
67325
+
67326
+ u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
67327
+ if( ALWAYS(u.aq.pCrsr) ){
67328
+ rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
67329
+ }else{
67330
+ u.aq.nEntry = 0;
67331
+ }
67332
+ pOut->u.i = u.aq.nEntry;
6601567333
break;
6601667334
}
6601767335
#endif
6601867336
6601967337
/* Opcode: Savepoint P1 * * P4 *
@@ -66021,42 +67339,42 @@
6602167339
** Open, release or rollback the savepoint named by parameter P4, depending
6602267340
** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
6602367341
** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
6602467342
*/
6602567343
case OP_Savepoint: {
66026
-#if 0 /* local variables moved into u.aq */
67344
+#if 0 /* local variables moved into u.ar */
6602767345
int p1; /* Value of P1 operand */
6602867346
char *zName; /* Name of savepoint */
6602967347
int nName;
6603067348
Savepoint *pNew;
6603167349
Savepoint *pSavepoint;
6603267350
Savepoint *pTmp;
6603367351
int iSavepoint;
6603467352
int ii;
66035
-#endif /* local variables moved into u.aq */
67353
+#endif /* local variables moved into u.ar */
6603667354
66037
- u.aq.p1 = pOp->p1;
66038
- u.aq.zName = pOp->p4.z;
67355
+ u.ar.p1 = pOp->p1;
67356
+ u.ar.zName = pOp->p4.z;
6603967357
66040
- /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
67358
+ /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
6604167359
** transaction, then there cannot be any savepoints.
6604267360
*/
6604367361
assert( db->pSavepoint==0 || db->autoCommit==0 );
66044
- assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
67362
+ assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
6604567363
assert( db->pSavepoint || db->isTransactionSavepoint==0 );
6604667364
assert( checkSavepointCount(db) );
6604767365
66048
- if( u.aq.p1==SAVEPOINT_BEGIN ){
67366
+ if( u.ar.p1==SAVEPOINT_BEGIN ){
6604967367
if( db->writeVdbeCnt>0 ){
6605067368
/* A new savepoint cannot be created if there are active write
6605167369
** statements (i.e. open read/write incremental blob handles).
6605267370
*/
6605367371
sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
6605467372
"SQL statements in progress");
6605567373
rc = SQLITE_BUSY;
6605667374
}else{
66057
- u.aq.nName = sqlite3Strlen30(u.aq.zName);
67375
+ u.ar.nName = sqlite3Strlen30(u.ar.zName);
6605867376
6605967377
#ifndef SQLITE_OMIT_VIRTUALTABLE
6606067378
/* This call is Ok even if this savepoint is actually a transaction
6606167379
** savepoint (and therefore should not prompt xSavepoint()) callbacks.
6606267380
** If this is a transaction savepoint being opened, it is guaranteed
@@ -66066,14 +67384,14 @@
6606667384
db->nStatement+db->nSavepoint);
6606767385
if( rc!=SQLITE_OK ) goto abort_due_to_error;
6606867386
#endif
6606967387
6607067388
/* Create a new savepoint structure. */
66071
- u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
66072
- if( u.aq.pNew ){
66073
- u.aq.pNew->zName = (char *)&u.aq.pNew[1];
66074
- memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
67389
+ u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
67390
+ if( u.ar.pNew ){
67391
+ u.ar.pNew->zName = (char *)&u.ar.pNew[1];
67392
+ memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
6607567393
6607667394
/* If there is no open transaction, then mark this as a special
6607767395
** "transaction savepoint". */
6607867396
if( db->autoCommit ){
6607967397
db->autoCommit = 0;
@@ -66081,50 +67399,50 @@
6608167399
}else{
6608267400
db->nSavepoint++;
6608367401
}
6608467402
6608567403
/* Link the new savepoint into the database handle's list. */
66086
- u.aq.pNew->pNext = db->pSavepoint;
66087
- db->pSavepoint = u.aq.pNew;
66088
- u.aq.pNew->nDeferredCons = db->nDeferredCons;
67404
+ u.ar.pNew->pNext = db->pSavepoint;
67405
+ db->pSavepoint = u.ar.pNew;
67406
+ u.ar.pNew->nDeferredCons = db->nDeferredCons;
6608967407
}
6609067408
}
6609167409
}else{
66092
- u.aq.iSavepoint = 0;
67410
+ u.ar.iSavepoint = 0;
6609367411
6609467412
/* Find the named savepoint. If there is no such savepoint, then an
6609567413
** an error is returned to the user. */
6609667414
for(
66097
- u.aq.pSavepoint = db->pSavepoint;
66098
- u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
66099
- u.aq.pSavepoint = u.aq.pSavepoint->pNext
67415
+ u.ar.pSavepoint = db->pSavepoint;
67416
+ u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
67417
+ u.ar.pSavepoint = u.ar.pSavepoint->pNext
6610067418
){
66101
- u.aq.iSavepoint++;
67419
+ u.ar.iSavepoint++;
6610267420
}
66103
- if( !u.aq.pSavepoint ){
66104
- sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
67421
+ if( !u.ar.pSavepoint ){
67422
+ sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
6610567423
rc = SQLITE_ERROR;
6610667424
}else if(
66107
- db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
67425
+ db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
6610867426
){
6610967427
/* It is not possible to release (commit) a savepoint if there are
6611067428
** active write statements. It is not possible to rollback a savepoint
6611167429
** if there are any active statements at all.
6611267430
*/
6611367431
sqlite3SetString(&p->zErrMsg, db,
6611467432
"cannot %s savepoint - SQL statements in progress",
66115
- (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
67433
+ (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
6611667434
);
6611767435
rc = SQLITE_BUSY;
6611867436
}else{
6611967437
6612067438
/* Determine whether or not this is a transaction savepoint. If so,
6612167439
** and this is a RELEASE command, then the current transaction
6612267440
** is committed.
6612367441
*/
66124
- int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66125
- if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
67442
+ int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
67443
+ if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
6612667444
if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
6612767445
goto vdbe_return;
6612867446
}
6612967447
db->autoCommit = 1;
6613067448
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66134,50 +67452,50 @@
6613467452
goto vdbe_return;
6613567453
}
6613667454
db->isTransactionSavepoint = 0;
6613767455
rc = p->rc;
6613867456
}else{
66139
- u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
66140
- for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
66141
- rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
67457
+ u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
67458
+ for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
67459
+ rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
6614267460
if( rc!=SQLITE_OK ){
6614367461
goto abort_due_to_error;
6614467462
}
6614567463
}
66146
- if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
67464
+ if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
6614767465
sqlite3ExpirePreparedStatements(db);
6614867466
sqlite3ResetInternalSchema(db, -1);
6614967467
db->flags = (db->flags | SQLITE_InternChanges);
6615067468
}
6615167469
}
6615267470
6615367471
/* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
6615467472
** savepoints nested inside of the savepoint being operated on. */
66155
- while( db->pSavepoint!=u.aq.pSavepoint ){
66156
- u.aq.pTmp = db->pSavepoint;
66157
- db->pSavepoint = u.aq.pTmp->pNext;
66158
- sqlite3DbFree(db, u.aq.pTmp);
67473
+ while( db->pSavepoint!=u.ar.pSavepoint ){
67474
+ u.ar.pTmp = db->pSavepoint;
67475
+ db->pSavepoint = u.ar.pTmp->pNext;
67476
+ sqlite3DbFree(db, u.ar.pTmp);
6615967477
db->nSavepoint--;
6616067478
}
6616167479
6616267480
/* If it is a RELEASE, then destroy the savepoint being operated on
6616367481
** too. If it is a ROLLBACK TO, then set the number of deferred
6616467482
** constraint violations present in the database to the value stored
6616567483
** when the savepoint was created. */
66166
- if( u.aq.p1==SAVEPOINT_RELEASE ){
66167
- assert( u.aq.pSavepoint==db->pSavepoint );
66168
- db->pSavepoint = u.aq.pSavepoint->pNext;
66169
- sqlite3DbFree(db, u.aq.pSavepoint);
67484
+ if( u.ar.p1==SAVEPOINT_RELEASE ){
67485
+ assert( u.ar.pSavepoint==db->pSavepoint );
67486
+ db->pSavepoint = u.ar.pSavepoint->pNext;
67487
+ sqlite3DbFree(db, u.ar.pSavepoint);
6617067488
if( !isTransaction ){
6617167489
db->nSavepoint--;
6617267490
}
6617367491
}else{
66174
- db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
67492
+ db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
6617567493
}
6617667494
6617767495
if( !isTransaction ){
66178
- rc = sqlite3VtabSavepoint(db, u.aq.p1, u.aq.iSavepoint);
67496
+ rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
6617967497
if( rc!=SQLITE_OK ) goto abort_due_to_error;
6618067498
}
6618167499
}
6618267500
}
6618367501
@@ -66192,50 +67510,50 @@
6619267510
** there are active writing VMs or active VMs that use shared cache.
6619367511
**
6619467512
** This instruction causes the VM to halt.
6619567513
*/
6619667514
case OP_AutoCommit: {
66197
-#if 0 /* local variables moved into u.ar */
67515
+#if 0 /* local variables moved into u.as */
6619867516
int desiredAutoCommit;
6619967517
int iRollback;
6620067518
int turnOnAC;
66201
-#endif /* local variables moved into u.ar */
67519
+#endif /* local variables moved into u.as */
6620267520
66203
- u.ar.desiredAutoCommit = pOp->p1;
66204
- u.ar.iRollback = pOp->p2;
66205
- u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
66206
- assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
66207
- assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
67521
+ u.as.desiredAutoCommit = pOp->p1;
67522
+ u.as.iRollback = pOp->p2;
67523
+ u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
67524
+ assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
67525
+ assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
6620867526
assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
6620967527
66210
- if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
67528
+ if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
6621167529
/* If this instruction implements a ROLLBACK and other VMs are
6621267530
** still running, and a transaction is active, return an error indicating
6621367531
** that the other VMs must complete first.
6621467532
*/
6621567533
sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
6621667534
"SQL statements in progress");
6621767535
rc = SQLITE_BUSY;
66218
- }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
67536
+ }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
6621967537
/* If this instruction implements a COMMIT and other VMs are writing
6622067538
** return an error indicating that the other VMs must complete first.
6622167539
*/
6622267540
sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
6622367541
"SQL statements in progress");
6622467542
rc = SQLITE_BUSY;
66225
- }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
66226
- if( u.ar.iRollback ){
66227
- assert( u.ar.desiredAutoCommit==1 );
67543
+ }else if( u.as.desiredAutoCommit!=db->autoCommit ){
67544
+ if( u.as.iRollback ){
67545
+ assert( u.as.desiredAutoCommit==1 );
6622867546
sqlite3RollbackAll(db);
6622967547
db->autoCommit = 1;
6623067548
}else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
6623167549
goto vdbe_return;
6623267550
}else{
66233
- db->autoCommit = (u8)u.ar.desiredAutoCommit;
67551
+ db->autoCommit = (u8)u.as.desiredAutoCommit;
6623467552
if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
6623567553
p->pc = pc;
66236
- db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
67554
+ db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
6623767555
p->rc = rc = SQLITE_BUSY;
6623867556
goto vdbe_return;
6623967557
}
6624067558
}
6624167559
assert( db->nStatement==0 );
@@ -66246,12 +67564,12 @@
6624667564
rc = SQLITE_ERROR;
6624767565
}
6624867566
goto vdbe_return;
6624967567
}else{
6625067568
sqlite3SetString(&p->zErrMsg, db,
66251
- (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66252
- (u.ar.iRollback)?"cannot rollback - no transaction is active":
67569
+ (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
67570
+ (u.as.iRollback)?"cannot rollback - no transaction is active":
6625367571
"cannot commit - no transaction is active"));
6625467572
6625567573
rc = SQLITE_ERROR;
6625667574
}
6625767575
break;
@@ -66287,20 +67605,20 @@
6628767605
** will automatically commit when the VDBE halts.
6628867606
**
6628967607
** If P2 is zero, then a read-lock is obtained on the database file.
6629067608
*/
6629167609
case OP_Transaction: {
66292
-#if 0 /* local variables moved into u.as */
67610
+#if 0 /* local variables moved into u.at */
6629367611
Btree *pBt;
66294
-#endif /* local variables moved into u.as */
67612
+#endif /* local variables moved into u.at */
6629567613
6629667614
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6629767615
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66298
- u.as.pBt = db->aDb[pOp->p1].pBt;
67616
+ u.at.pBt = db->aDb[pOp->p1].pBt;
6629967617
66300
- if( u.as.pBt ){
66301
- rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
67618
+ if( u.at.pBt ){
67619
+ rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
6630267620
if( rc==SQLITE_BUSY ){
6630367621
p->pc = pc;
6630467622
p->rc = rc = SQLITE_BUSY;
6630567623
goto vdbe_return;
6630667624
}
@@ -66309,20 +67627,20 @@
6630967627
}
6631067628
6631167629
if( pOp->p2 && p->usesStmtJournal
6631267630
&& (db->autoCommit==0 || db->activeVdbeCnt>1)
6631367631
){
66314
- assert( sqlite3BtreeIsInTrans(u.as.pBt) );
67632
+ assert( sqlite3BtreeIsInTrans(u.at.pBt) );
6631567633
if( p->iStatement==0 ){
6631667634
assert( db->nStatement>=0 && db->nSavepoint>=0 );
6631767635
db->nStatement++;
6631867636
p->iStatement = db->nSavepoint + db->nStatement;
6631967637
}
6632067638
6632167639
rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
6632267640
if( rc==SQLITE_OK ){
66323
- rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
67641
+ rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
6632467642
}
6632567643
6632667644
/* Store the current value of the database handles deferred constraint
6632767645
** counter. If the statement transaction needs to be rolled back,
6632867646
** the value of this counter needs to be restored too. */
@@ -66343,25 +67661,25 @@
6634367661
** There must be a read-lock on the database (either a transaction
6634467662
** must be started or there must be an open cursor) before
6634567663
** executing this instruction.
6634667664
*/
6634767665
case OP_ReadCookie: { /* out2-prerelease */
66348
-#if 0 /* local variables moved into u.at */
67666
+#if 0 /* local variables moved into u.au */
6634967667
int iMeta;
6635067668
int iDb;
6635167669
int iCookie;
66352
-#endif /* local variables moved into u.at */
67670
+#endif /* local variables moved into u.au */
6635367671
66354
- u.at.iDb = pOp->p1;
66355
- u.at.iCookie = pOp->p3;
67672
+ u.au.iDb = pOp->p1;
67673
+ u.au.iCookie = pOp->p3;
6635667674
assert( pOp->p3<SQLITE_N_BTREE_META );
66357
- assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
66358
- assert( db->aDb[u.at.iDb].pBt!=0 );
66359
- assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
67675
+ assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
67676
+ assert( db->aDb[u.au.iDb].pBt!=0 );
67677
+ assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
6636067678
66361
- sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
66362
- pOut->u.i = u.at.iMeta;
67679
+ sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
67680
+ pOut->u.i = u.au.iMeta;
6636367681
break;
6636467682
}
6636567683
6636667684
/* Opcode: SetCookie P1 P2 P3 * *
6636767685
**
@@ -66372,30 +67690,30 @@
6637267690
** database file used to store temporary tables.
6637367691
**
6637467692
** A transaction must be started before executing this opcode.
6637567693
*/
6637667694
case OP_SetCookie: { /* in3 */
66377
-#if 0 /* local variables moved into u.au */
67695
+#if 0 /* local variables moved into u.av */
6637867696
Db *pDb;
66379
-#endif /* local variables moved into u.au */
67697
+#endif /* local variables moved into u.av */
6638067698
assert( pOp->p2<SQLITE_N_BTREE_META );
6638167699
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6638267700
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66383
- u.au.pDb = &db->aDb[pOp->p1];
66384
- assert( u.au.pDb->pBt!=0 );
67701
+ u.av.pDb = &db->aDb[pOp->p1];
67702
+ assert( u.av.pDb->pBt!=0 );
6638567703
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
6638667704
pIn3 = &aMem[pOp->p3];
6638767705
sqlite3VdbeMemIntegerify(pIn3);
6638867706
/* See note about index shifting on OP_ReadCookie */
66389
- rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
67707
+ rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
6639067708
if( pOp->p2==BTREE_SCHEMA_VERSION ){
6639167709
/* When the schema cookie changes, record the new cookie internally */
66392
- u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
67710
+ u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
6639367711
db->flags |= SQLITE_InternChanges;
6639467712
}else if( pOp->p2==BTREE_FILE_FORMAT ){
6639567713
/* Record changes in the file format */
66396
- u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
67714
+ u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
6639767715
}
6639867716
if( pOp->p1==1 ){
6639967717
/* Invalidate all prepared statements whenever the TEMP database
6640067718
** schema is changed. Ticket #1644 */
6640167719
sqlite3ExpirePreparedStatements(db);
@@ -66421,27 +67739,27 @@
6642167739
** Either a transaction needs to have been started or an OP_Open needs
6642267740
** to be executed (to establish a read lock) before this opcode is
6642367741
** invoked.
6642467742
*/
6642567743
case OP_VerifyCookie: {
66426
-#if 0 /* local variables moved into u.av */
67744
+#if 0 /* local variables moved into u.aw */
6642767745
int iMeta;
6642867746
int iGen;
6642967747
Btree *pBt;
66430
-#endif /* local variables moved into u.av */
67748
+#endif /* local variables moved into u.aw */
6643167749
6643267750
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6643367751
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
6643467752
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66435
- u.av.pBt = db->aDb[pOp->p1].pBt;
66436
- if( u.av.pBt ){
66437
- sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
66438
- u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
67753
+ u.aw.pBt = db->aDb[pOp->p1].pBt;
67754
+ if( u.aw.pBt ){
67755
+ sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
67756
+ u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
6643967757
}else{
66440
- u.av.iGen = u.av.iMeta = 0;
67758
+ u.aw.iGen = u.aw.iMeta = 0;
6644167759
}
66442
- if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
67760
+ if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
6644367761
sqlite3DbFree(db, p->zErrMsg);
6644467762
p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
6644567763
/* If the schema-cookie from the database file matches the cookie
6644667764
** stored with the in-memory representation of the schema, do
6644767765
** not reload the schema from the database file.
@@ -66453,11 +67771,11 @@
6645367771
** discard the database schema, as the user code implementing the
6645467772
** v-table would have to be ready for the sqlite3_vtab structure itself
6645567773
** to be invalidated whenever sqlite3_step() is called from within
6645667774
** a v-table method.
6645767775
*/
66458
- if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
67776
+ if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
6645967777
sqlite3ResetInternalSchema(db, pOp->p1);
6646067778
}
6646167779
6646267780
p->expired = 1;
6646367781
rc = SQLITE_SCHEMA;
@@ -66514,86 +67832,86 @@
6651467832
**
6651567833
** See also OpenRead.
6651667834
*/
6651767835
case OP_OpenRead:
6651867836
case OP_OpenWrite: {
66519
-#if 0 /* local variables moved into u.aw */
67837
+#if 0 /* local variables moved into u.ax */
6652067838
int nField;
6652167839
KeyInfo *pKeyInfo;
6652267840
int p2;
6652367841
int iDb;
6652467842
int wrFlag;
6652567843
Btree *pX;
6652667844
VdbeCursor *pCur;
6652767845
Db *pDb;
66528
-#endif /* local variables moved into u.aw */
67846
+#endif /* local variables moved into u.ax */
6652967847
6653067848
if( p->expired ){
6653167849
rc = SQLITE_ABORT;
6653267850
break;
6653367851
}
6653467852
66535
- u.aw.nField = 0;
66536
- u.aw.pKeyInfo = 0;
66537
- u.aw.p2 = pOp->p2;
66538
- u.aw.iDb = pOp->p3;
66539
- assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
66540
- assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
66541
- u.aw.pDb = &db->aDb[u.aw.iDb];
66542
- u.aw.pX = u.aw.pDb->pBt;
66543
- assert( u.aw.pX!=0 );
67853
+ u.ax.nField = 0;
67854
+ u.ax.pKeyInfo = 0;
67855
+ u.ax.p2 = pOp->p2;
67856
+ u.ax.iDb = pOp->p3;
67857
+ assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
67858
+ assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
67859
+ u.ax.pDb = &db->aDb[u.ax.iDb];
67860
+ u.ax.pX = u.ax.pDb->pBt;
67861
+ assert( u.ax.pX!=0 );
6654467862
if( pOp->opcode==OP_OpenWrite ){
66545
- u.aw.wrFlag = 1;
66546
- assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
66547
- if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
66548
- p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
67863
+ u.ax.wrFlag = 1;
67864
+ assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
67865
+ if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
67866
+ p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
6654967867
}
6655067868
}else{
66551
- u.aw.wrFlag = 0;
67869
+ u.ax.wrFlag = 0;
6655267870
}
6655367871
if( pOp->p5 ){
66554
- assert( u.aw.p2>0 );
66555
- assert( u.aw.p2<=p->nMem );
66556
- pIn2 = &aMem[u.aw.p2];
67872
+ assert( u.ax.p2>0 );
67873
+ assert( u.ax.p2<=p->nMem );
67874
+ pIn2 = &aMem[u.ax.p2];
6655767875
assert( memIsValid(pIn2) );
6655867876
assert( (pIn2->flags & MEM_Int)!=0 );
6655967877
sqlite3VdbeMemIntegerify(pIn2);
66560
- u.aw.p2 = (int)pIn2->u.i;
66561
- /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
66562
- ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
67878
+ u.ax.p2 = (int)pIn2->u.i;
67879
+ /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
67880
+ ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
6656367881
** If there were a failure, the prepared statement would have halted
6656467882
** before reaching this instruction. */
66565
- if( NEVER(u.aw.p2<2) ) {
67883
+ if( NEVER(u.ax.p2<2) ) {
6656667884
rc = SQLITE_CORRUPT_BKPT;
6656767885
goto abort_due_to_error;
6656867886
}
6656967887
}
6657067888
if( pOp->p4type==P4_KEYINFO ){
66571
- u.aw.pKeyInfo = pOp->p4.pKeyInfo;
66572
- u.aw.pKeyInfo->enc = ENC(p->db);
66573
- u.aw.nField = u.aw.pKeyInfo->nField+1;
67889
+ u.ax.pKeyInfo = pOp->p4.pKeyInfo;
67890
+ u.ax.pKeyInfo->enc = ENC(p->db);
67891
+ u.ax.nField = u.ax.pKeyInfo->nField+1;
6657467892
}else if( pOp->p4type==P4_INT32 ){
66575
- u.aw.nField = pOp->p4.i;
67893
+ u.ax.nField = pOp->p4.i;
6657667894
}
6657767895
assert( pOp->p1>=0 );
66578
- u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
66579
- if( u.aw.pCur==0 ) goto no_mem;
66580
- u.aw.pCur->nullRow = 1;
66581
- u.aw.pCur->isOrdered = 1;
66582
- rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66583
- u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
67896
+ u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
67897
+ if( u.ax.pCur==0 ) goto no_mem;
67898
+ u.ax.pCur->nullRow = 1;
67899
+ u.ax.pCur->isOrdered = 1;
67900
+ rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
67901
+ u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
6658467902
6658567903
/* Since it performs no memory allocation or IO, the only value that
6658667904
** sqlite3BtreeCursor() may return is SQLITE_OK. */
6658767905
assert( rc==SQLITE_OK );
6658867906
6658967907
/* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
6659067908
** SQLite used to check if the root-page flags were sane at this point
6659167909
** and report database corruption if they were not, but this check has
6659267910
** since moved into the btree layer. */
66593
- u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66594
- u.aw.pCur->isIndex = !u.aw.pCur->isTable;
67911
+ u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
67912
+ u.ax.pCur->isIndex = !u.ax.pCur->isTable;
6659567913
break;
6659667914
}
6659767915
6659867916
/* Opcode: OpenEphemeral P1 P2 * P4 P5
6659967917
**
@@ -66625,28 +67943,28 @@
6662567943
** by this opcode will be used for automatically created transient
6662667944
** indices in joins.
6662767945
*/
6662867946
case OP_OpenAutoindex:
6662967947
case OP_OpenEphemeral: {
66630
-#if 0 /* local variables moved into u.ax */
67948
+#if 0 /* local variables moved into u.ay */
6663167949
VdbeCursor *pCx;
66632
-#endif /* local variables moved into u.ax */
67950
+#endif /* local variables moved into u.ay */
6663367951
static const int vfsFlags =
6663467952
SQLITE_OPEN_READWRITE |
6663567953
SQLITE_OPEN_CREATE |
6663667954
SQLITE_OPEN_EXCLUSIVE |
6663767955
SQLITE_OPEN_DELETEONCLOSE |
6663867956
SQLITE_OPEN_TRANSIENT_DB;
6663967957
6664067958
assert( pOp->p1>=0 );
66641
- u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66642
- if( u.ax.pCx==0 ) goto no_mem;
66643
- u.ax.pCx->nullRow = 1;
66644
- rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
67959
+ u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
67960
+ if( u.ay.pCx==0 ) goto no_mem;
67961
+ u.ay.pCx->nullRow = 1;
67962
+ rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
6664567963
BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
6664667964
if( rc==SQLITE_OK ){
66647
- rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
67965
+ rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
6664867966
}
6664967967
if( rc==SQLITE_OK ){
6665067968
/* If a transient index is required, create it by calling
6665167969
** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
6665267970
** opening it. If a transient table is required, just use the
@@ -66653,26 +67971,26 @@
6665367971
** automatically created table with root-page 1 (an BLOB_INTKEY table).
6665467972
*/
6665567973
if( pOp->p4.pKeyInfo ){
6665667974
int pgno;
6665767975
assert( pOp->p4type==P4_KEYINFO );
66658
- rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
67976
+ rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
6665967977
if( rc==SQLITE_OK ){
6666067978
assert( pgno==MASTER_ROOT+1 );
66661
- rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
66662
- (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
66663
- u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66664
- u.ax.pCx->pKeyInfo->enc = ENC(p->db);
66665
- }
66666
- u.ax.pCx->isTable = 0;
66667
- }else{
66668
- rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
66669
- u.ax.pCx->isTable = 1;
67979
+ rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
67980
+ (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
67981
+ u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
67982
+ u.ay.pCx->pKeyInfo->enc = ENC(p->db);
67983
+ }
67984
+ u.ay.pCx->isTable = 0;
67985
+ }else{
67986
+ rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
67987
+ u.ay.pCx->isTable = 1;
6667067988
}
6667167989
}
66672
- u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66673
- u.ax.pCx->isIndex = !u.ax.pCx->isTable;
67990
+ u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
67991
+ u.ay.pCx->isIndex = !u.ay.pCx->isTable;
6667467992
break;
6667567993
}
6667667994
6667767995
/* Opcode: OpenSorter P1 P2 * P4 *
6667867996
**
@@ -66679,20 +67997,20 @@
6667967997
** This opcode works like OP_OpenEphemeral except that it opens
6668067998
** a transient index that is specifically designed to sort large
6668167999
** tables using an external merge-sort algorithm.
6668268000
*/
6668368001
case OP_SorterOpen: {
66684
-#if 0 /* local variables moved into u.ay */
68002
+#if 0 /* local variables moved into u.az */
6668568003
VdbeCursor *pCx;
66686
-#endif /* local variables moved into u.ay */
68004
+#endif /* local variables moved into u.az */
6668768005
#ifndef SQLITE_OMIT_MERGE_SORT
66688
- u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66689
- if( u.ay.pCx==0 ) goto no_mem;
66690
- u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66691
- u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66692
- u.ay.pCx->isSorter = 1;
66693
- rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
68006
+ u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
68007
+ if( u.az.pCx==0 ) goto no_mem;
68008
+ u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
68009
+ u.az.pCx->pKeyInfo->enc = ENC(p->db);
68010
+ u.az.pCx->isSorter = 1;
68011
+ rc = sqlite3VdbeSorterInit(db, u.az.pCx);
6669468012
#else
6669568013
pOp->opcode = OP_OpenEphemeral;
6669668014
pc--;
6669768015
#endif
6669868016
break;
@@ -66712,21 +68030,21 @@
6671268030
**
6671368031
** P3 is the number of fields in the records that will be stored by
6671468032
** the pseudo-table.
6671568033
*/
6671668034
case OP_OpenPseudo: {
66717
-#if 0 /* local variables moved into u.az */
68035
+#if 0 /* local variables moved into u.ba */
6671868036
VdbeCursor *pCx;
66719
-#endif /* local variables moved into u.az */
68037
+#endif /* local variables moved into u.ba */
6672068038
6672168039
assert( pOp->p1>=0 );
66722
- u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66723
- if( u.az.pCx==0 ) goto no_mem;
66724
- u.az.pCx->nullRow = 1;
66725
- u.az.pCx->pseudoTableReg = pOp->p2;
66726
- u.az.pCx->isTable = 1;
66727
- u.az.pCx->isIndex = 0;
68040
+ u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
68041
+ if( u.ba.pCx==0 ) goto no_mem;
68042
+ u.ba.pCx->nullRow = 1;
68043
+ u.ba.pCx->pseudoTableReg = pOp->p2;
68044
+ u.ba.pCx->isTable = 1;
68045
+ u.ba.pCx->isIndex = 0;
6672868046
break;
6672968047
}
6673068048
6673168049
/* Opcode: Close P1 * * * *
6673268050
**
@@ -66794,39 +68112,39 @@
6679468112
*/
6679568113
case OP_SeekLt: /* jump, in3 */
6679668114
case OP_SeekLe: /* jump, in3 */
6679768115
case OP_SeekGe: /* jump, in3 */
6679868116
case OP_SeekGt: { /* jump, in3 */
66799
-#if 0 /* local variables moved into u.ba */
68117
+#if 0 /* local variables moved into u.bb */
6680068118
int res;
6680168119
int oc;
6680268120
VdbeCursor *pC;
6680368121
UnpackedRecord r;
6680468122
int nField;
6680568123
i64 iKey; /* The rowid we are to seek to */
66806
-#endif /* local variables moved into u.ba */
68124
+#endif /* local variables moved into u.bb */
6680768125
6680868126
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6680968127
assert( pOp->p2!=0 );
66810
- u.ba.pC = p->apCsr[pOp->p1];
66811
- assert( u.ba.pC!=0 );
66812
- assert( u.ba.pC->pseudoTableReg==0 );
68128
+ u.bb.pC = p->apCsr[pOp->p1];
68129
+ assert( u.bb.pC!=0 );
68130
+ assert( u.bb.pC->pseudoTableReg==0 );
6681368131
assert( OP_SeekLe == OP_SeekLt+1 );
6681468132
assert( OP_SeekGe == OP_SeekLt+2 );
6681568133
assert( OP_SeekGt == OP_SeekLt+3 );
66816
- assert( u.ba.pC->isOrdered );
66817
- if( ALWAYS(u.ba.pC->pCursor!=0) ){
66818
- u.ba.oc = pOp->opcode;
66819
- u.ba.pC->nullRow = 0;
66820
- if( u.ba.pC->isTable ){
68134
+ assert( u.bb.pC->isOrdered );
68135
+ if( ALWAYS(u.bb.pC->pCursor!=0) ){
68136
+ u.bb.oc = pOp->opcode;
68137
+ u.bb.pC->nullRow = 0;
68138
+ if( u.bb.pC->isTable ){
6682168139
/* The input value in P3 might be of any type: integer, real, string,
6682268140
** blob, or NULL. But it needs to be an integer before we can do
6682368141
** the seek, so covert it. */
6682468142
pIn3 = &aMem[pOp->p3];
6682568143
applyNumericAffinity(pIn3);
66826
- u.ba.iKey = sqlite3VdbeIntValue(pIn3);
66827
- u.ba.pC->rowidIsValid = 0;
68144
+ u.bb.iKey = sqlite3VdbeIntValue(pIn3);
68145
+ u.bb.pC->rowidIsValid = 0;
6682868146
6682968147
/* If the P3 value could not be converted into an integer without
6683068148
** loss of information, then special processing is required... */
6683168149
if( (pIn3->flags & MEM_Int)==0 ){
6683268150
if( (pIn3->flags & MEM_Real)==0 ){
@@ -66837,105 +68155,105 @@
6683768155
}
6683868156
/* If we reach this point, then the P3 value must be a floating
6683968157
** point number. */
6684068158
assert( (pIn3->flags & MEM_Real)!=0 );
6684168159
66842
- if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
66843
- /* The P3 value is too large in magnitude to be expressed as an
66844
- ** integer. */
66845
- u.ba.res = 1;
66846
- if( pIn3->r<0 ){
66847
- if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66848
- rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
66849
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66850
- }
66851
- }else{
66852
- if( u.ba.oc<=OP_SeekLe ){ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66853
- rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
66854
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66855
- }
66856
- }
66857
- if( u.ba.res ){
66858
- pc = pOp->p2 - 1;
66859
- }
66860
- break;
66861
- }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
66862
- /* Use the ceiling() function to convert real->int */
66863
- if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
66864
- }else{
66865
- /* Use the floor() function to convert real->int */
66866
- assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
66867
- if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
66868
- }
66869
- }
66870
- rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
66871
- if( rc!=SQLITE_OK ){
66872
- goto abort_due_to_error;
66873
- }
66874
- if( u.ba.res==0 ){
66875
- u.ba.pC->rowidIsValid = 1;
66876
- u.ba.pC->lastRowid = u.ba.iKey;
66877
- }
66878
- }else{
66879
- u.ba.nField = pOp->p4.i;
66880
- assert( pOp->p4type==P4_INT32 );
66881
- assert( u.ba.nField>0 );
66882
- u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
66883
- u.ba.r.nField = (u16)u.ba.nField;
66884
-
66885
- /* The next line of code computes as follows, only faster:
66886
- ** if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
66887
- ** u.ba.r.flags = UNPACKED_INCRKEY;
66888
- ** }else{
66889
- ** u.ba.r.flags = 0;
66890
- ** }
66891
- */
66892
- u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
66893
- assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
66894
- assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
66895
- assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
66896
- assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
66897
-
66898
- u.ba.r.aMem = &aMem[pOp->p3];
66899
-#ifdef SQLITE_DEBUG
66900
- { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
66901
-#endif
66902
- ExpandBlob(u.ba.r.aMem);
66903
- rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
66904
- if( rc!=SQLITE_OK ){
66905
- goto abort_due_to_error;
66906
- }
66907
- u.ba.pC->rowidIsValid = 0;
66908
- }
66909
- u.ba.pC->deferredMoveto = 0;
66910
- u.ba.pC->cacheStatus = CACHE_STALE;
66911
-#ifdef SQLITE_TEST
66912
- sqlite3_search_count++;
66913
-#endif
66914
- if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66915
- if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
66916
- rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
66917
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66918
- u.ba.pC->rowidIsValid = 0;
66919
- }else{
66920
- u.ba.res = 0;
66921
- }
66922
- }else{
66923
- assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66924
- if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
66925
- rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
66926
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66927
- u.ba.pC->rowidIsValid = 0;
66928
- }else{
66929
- /* u.ba.res might be negative because the table is empty. Check to
66930
- ** see if this is the case.
66931
- */
66932
- u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
66933
- }
66934
- }
66935
- assert( pOp->p2>0 );
66936
- if( u.ba.res ){
68160
+ if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
68161
+ /* The P3 value is too large in magnitude to be expressed as an
68162
+ ** integer. */
68163
+ u.bb.res = 1;
68164
+ if( pIn3->r<0 ){
68165
+ if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
68166
+ rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
68167
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
68168
+ }
68169
+ }else{
68170
+ if( u.bb.oc<=OP_SeekLe ){ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
68171
+ rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
68172
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
68173
+ }
68174
+ }
68175
+ if( u.bb.res ){
68176
+ pc = pOp->p2 - 1;
68177
+ }
68178
+ break;
68179
+ }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
68180
+ /* Use the ceiling() function to convert real->int */
68181
+ if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
68182
+ }else{
68183
+ /* Use the floor() function to convert real->int */
68184
+ assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
68185
+ if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
68186
+ }
68187
+ }
68188
+ rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
68189
+ if( rc!=SQLITE_OK ){
68190
+ goto abort_due_to_error;
68191
+ }
68192
+ if( u.bb.res==0 ){
68193
+ u.bb.pC->rowidIsValid = 1;
68194
+ u.bb.pC->lastRowid = u.bb.iKey;
68195
+ }
68196
+ }else{
68197
+ u.bb.nField = pOp->p4.i;
68198
+ assert( pOp->p4type==P4_INT32 );
68199
+ assert( u.bb.nField>0 );
68200
+ u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
68201
+ u.bb.r.nField = (u16)u.bb.nField;
68202
+
68203
+ /* The next line of code computes as follows, only faster:
68204
+ ** if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
68205
+ ** u.bb.r.flags = UNPACKED_INCRKEY;
68206
+ ** }else{
68207
+ ** u.bb.r.flags = 0;
68208
+ ** }
68209
+ */
68210
+ u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
68211
+ assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
68212
+ assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
68213
+ assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
68214
+ assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
68215
+
68216
+ u.bb.r.aMem = &aMem[pOp->p3];
68217
+#ifdef SQLITE_DEBUG
68218
+ { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
68219
+#endif
68220
+ ExpandBlob(u.bb.r.aMem);
68221
+ rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
68222
+ if( rc!=SQLITE_OK ){
68223
+ goto abort_due_to_error;
68224
+ }
68225
+ u.bb.pC->rowidIsValid = 0;
68226
+ }
68227
+ u.bb.pC->deferredMoveto = 0;
68228
+ u.bb.pC->cacheStatus = CACHE_STALE;
68229
+#ifdef SQLITE_TEST
68230
+ sqlite3_search_count++;
68231
+#endif
68232
+ if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
68233
+ if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
68234
+ rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
68235
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
68236
+ u.bb.pC->rowidIsValid = 0;
68237
+ }else{
68238
+ u.bb.res = 0;
68239
+ }
68240
+ }else{
68241
+ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
68242
+ if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
68243
+ rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
68244
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
68245
+ u.bb.pC->rowidIsValid = 0;
68246
+ }else{
68247
+ /* u.bb.res might be negative because the table is empty. Check to
68248
+ ** see if this is the case.
68249
+ */
68250
+ u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
68251
+ }
68252
+ }
68253
+ assert( pOp->p2>0 );
68254
+ if( u.bb.res ){
6693768255
pc = pOp->p2 - 1;
6693868256
}
6693968257
}else{
6694068258
/* This happens when attempting to open the sqlite3_master table
6694168259
** for read access returns SQLITE_EMPTY. In this case always
@@ -66954,24 +68272,24 @@
6695468272
** This is actually a deferred seek. Nothing actually happens until
6695568273
** the cursor is used to read a record. That way, if no reads
6695668274
** occur, no unnecessary I/O happens.
6695768275
*/
6695868276
case OP_Seek: { /* in2 */
66959
-#if 0 /* local variables moved into u.bb */
68277
+#if 0 /* local variables moved into u.bc */
6696068278
VdbeCursor *pC;
66961
-#endif /* local variables moved into u.bb */
68279
+#endif /* local variables moved into u.bc */
6696268280
6696368281
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66964
- u.bb.pC = p->apCsr[pOp->p1];
66965
- assert( u.bb.pC!=0 );
66966
- if( ALWAYS(u.bb.pC->pCursor!=0) ){
66967
- assert( u.bb.pC->isTable );
66968
- u.bb.pC->nullRow = 0;
68282
+ u.bc.pC = p->apCsr[pOp->p1];
68283
+ assert( u.bc.pC!=0 );
68284
+ if( ALWAYS(u.bc.pC->pCursor!=0) ){
68285
+ assert( u.bc.pC->isTable );
68286
+ u.bc.pC->nullRow = 0;
6696968287
pIn2 = &aMem[pOp->p2];
66970
- u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
66971
- u.bb.pC->rowidIsValid = 0;
66972
- u.bb.pC->deferredMoveto = 1;
68288
+ u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
68289
+ u.bc.pC->rowidIsValid = 0;
68290
+ u.bc.pC->deferredMoveto = 1;
6697368291
}
6697468292
break;
6697568293
}
6697668294
6697768295
@@ -66999,67 +68317,67 @@
6699968317
**
6700068318
** See also: Found, NotExists, IsUnique
6700168319
*/
6700268320
case OP_NotFound: /* jump, in3 */
6700368321
case OP_Found: { /* jump, in3 */
67004
-#if 0 /* local variables moved into u.bc */
68322
+#if 0 /* local variables moved into u.bd */
6700568323
int alreadyExists;
6700668324
VdbeCursor *pC;
6700768325
int res;
6700868326
char *pFree;
6700968327
UnpackedRecord *pIdxKey;
6701068328
UnpackedRecord r;
6701168329
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67012
-#endif /* local variables moved into u.bc */
68330
+#endif /* local variables moved into u.bd */
6701368331
6701468332
#ifdef SQLITE_TEST
6701568333
sqlite3_found_count++;
6701668334
#endif
6701768335
67018
- u.bc.alreadyExists = 0;
68336
+ u.bd.alreadyExists = 0;
6701968337
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6702068338
assert( pOp->p4type==P4_INT32 );
67021
- u.bc.pC = p->apCsr[pOp->p1];
67022
- assert( u.bc.pC!=0 );
68339
+ u.bd.pC = p->apCsr[pOp->p1];
68340
+ assert( u.bd.pC!=0 );
6702368341
pIn3 = &aMem[pOp->p3];
67024
- if( ALWAYS(u.bc.pC->pCursor!=0) ){
68342
+ if( ALWAYS(u.bd.pC->pCursor!=0) ){
6702568343
67026
- assert( u.bc.pC->isTable==0 );
68344
+ assert( u.bd.pC->isTable==0 );
6702768345
if( pOp->p4.i>0 ){
67028
- u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67029
- u.bc.r.nField = (u16)pOp->p4.i;
67030
- u.bc.r.aMem = pIn3;
68346
+ u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
68347
+ u.bd.r.nField = (u16)pOp->p4.i;
68348
+ u.bd.r.aMem = pIn3;
6703168349
#ifdef SQLITE_DEBUG
67032
- { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
68350
+ { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
6703368351
#endif
67034
- u.bc.r.flags = UNPACKED_PREFIX_MATCH;
67035
- u.bc.pIdxKey = &u.bc.r;
68352
+ u.bd.r.flags = UNPACKED_PREFIX_MATCH;
68353
+ u.bd.pIdxKey = &u.bd.r;
6703668354
}else{
67037
- u.bc.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67038
- u.bc.pC->pKeyInfo, u.bc.aTempRec, sizeof(u.bc.aTempRec), &u.bc.pFree
68355
+ u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
68356
+ u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
6703968357
);
67040
- if( u.bc.pIdxKey==0 ) goto no_mem;
68358
+ if( u.bd.pIdxKey==0 ) goto no_mem;
6704168359
assert( pIn3->flags & MEM_Blob );
6704268360
assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67043
- sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z, u.bc.pIdxKey);
67044
- u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
68361
+ sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
68362
+ u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
6704568363
}
67046
- rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
68364
+ rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
6704768365
if( pOp->p4.i==0 ){
67048
- sqlite3DbFree(db, u.bc.pFree);
68366
+ sqlite3DbFree(db, u.bd.pFree);
6704968367
}
6705068368
if( rc!=SQLITE_OK ){
6705168369
break;
6705268370
}
67053
- u.bc.alreadyExists = (u.bc.res==0);
67054
- u.bc.pC->deferredMoveto = 0;
67055
- u.bc.pC->cacheStatus = CACHE_STALE;
68371
+ u.bd.alreadyExists = (u.bd.res==0);
68372
+ u.bd.pC->deferredMoveto = 0;
68373
+ u.bd.pC->cacheStatus = CACHE_STALE;
6705668374
}
6705768375
if( pOp->opcode==OP_Found ){
67058
- if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
68376
+ if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
6705968377
}else{
67060
- if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
68378
+ if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
6706168379
}
6706268380
break;
6706368381
}
6706468382
6706568383
/* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67087,67 +68405,67 @@
6708768405
** instruction.
6708868406
**
6708968407
** See also: NotFound, NotExists, Found
6709068408
*/
6709168409
case OP_IsUnique: { /* jump, in3 */
67092
-#if 0 /* local variables moved into u.bd */
68410
+#if 0 /* local variables moved into u.be */
6709368411
u16 ii;
6709468412
VdbeCursor *pCx;
6709568413
BtCursor *pCrsr;
6709668414
u16 nField;
6709768415
Mem *aMx;
6709868416
UnpackedRecord r; /* B-Tree index search key */
6709968417
i64 R; /* Rowid stored in register P3 */
67100
-#endif /* local variables moved into u.bd */
68418
+#endif /* local variables moved into u.be */
6710168419
6710268420
pIn3 = &aMem[pOp->p3];
67103
- u.bd.aMx = &aMem[pOp->p4.i];
68421
+ u.be.aMx = &aMem[pOp->p4.i];
6710468422
/* Assert that the values of parameters P1 and P4 are in range. */
6710568423
assert( pOp->p4type==P4_INT32 );
6710668424
assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
6710768425
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6710868426
6710968427
/* Find the index cursor. */
67110
- u.bd.pCx = p->apCsr[pOp->p1];
67111
- assert( u.bd.pCx->deferredMoveto==0 );
67112
- u.bd.pCx->seekResult = 0;
67113
- u.bd.pCx->cacheStatus = CACHE_STALE;
67114
- u.bd.pCrsr = u.bd.pCx->pCursor;
68428
+ u.be.pCx = p->apCsr[pOp->p1];
68429
+ assert( u.be.pCx->deferredMoveto==0 );
68430
+ u.be.pCx->seekResult = 0;
68431
+ u.be.pCx->cacheStatus = CACHE_STALE;
68432
+ u.be.pCrsr = u.be.pCx->pCursor;
6711568433
6711668434
/* If any of the values are NULL, take the jump. */
67117
- u.bd.nField = u.bd.pCx->pKeyInfo->nField;
67118
- for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
67119
- if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
68435
+ u.be.nField = u.be.pCx->pKeyInfo->nField;
68436
+ for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
68437
+ if( u.be.aMx[u.be.ii].flags & MEM_Null ){
6712068438
pc = pOp->p2 - 1;
67121
- u.bd.pCrsr = 0;
68439
+ u.be.pCrsr = 0;
6712268440
break;
6712368441
}
6712468442
}
67125
- assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
68443
+ assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
6712668444
67127
- if( u.bd.pCrsr!=0 ){
68445
+ if( u.be.pCrsr!=0 ){
6712868446
/* Populate the index search key. */
67129
- u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
67130
- u.bd.r.nField = u.bd.nField + 1;
67131
- u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
67132
- u.bd.r.aMem = u.bd.aMx;
68447
+ u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
68448
+ u.be.r.nField = u.be.nField + 1;
68449
+ u.be.r.flags = UNPACKED_PREFIX_SEARCH;
68450
+ u.be.r.aMem = u.be.aMx;
6713368451
#ifdef SQLITE_DEBUG
67134
- { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
68452
+ { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
6713568453
#endif
6713668454
67137
- /* Extract the value of u.bd.R from register P3. */
68455
+ /* Extract the value of u.be.R from register P3. */
6713868456
sqlite3VdbeMemIntegerify(pIn3);
67139
- u.bd.R = pIn3->u.i;
68457
+ u.be.R = pIn3->u.i;
6714068458
6714168459
/* Search the B-Tree index. If no conflicting record is found, jump
6714268460
** to P2. Otherwise, copy the rowid of the conflicting record to
6714368461
** register P3 and fall through to the next instruction. */
67144
- rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
67145
- if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
68462
+ rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
68463
+ if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
6714668464
pc = pOp->p2 - 1;
6714768465
}else{
67148
- pIn3->u.i = u.bd.r.rowid;
68466
+ pIn3->u.i = u.be.r.rowid;
6714968467
}
6715068468
}
6715168469
break;
6715268470
}
6715368471
@@ -67164,46 +68482,46 @@
6716468482
** P1 is an index.
6716568483
**
6716668484
** See also: Found, NotFound, IsUnique
6716768485
*/
6716868486
case OP_NotExists: { /* jump, in3 */
67169
-#if 0 /* local variables moved into u.be */
68487
+#if 0 /* local variables moved into u.bf */
6717068488
VdbeCursor *pC;
6717168489
BtCursor *pCrsr;
6717268490
int res;
6717368491
u64 iKey;
67174
-#endif /* local variables moved into u.be */
68492
+#endif /* local variables moved into u.bf */
6717568493
6717668494
pIn3 = &aMem[pOp->p3];
6717768495
assert( pIn3->flags & MEM_Int );
6717868496
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67179
- u.be.pC = p->apCsr[pOp->p1];
67180
- assert( u.be.pC!=0 );
67181
- assert( u.be.pC->isTable );
67182
- assert( u.be.pC->pseudoTableReg==0 );
67183
- u.be.pCrsr = u.be.pC->pCursor;
67184
- if( ALWAYS(u.be.pCrsr!=0) ){
67185
- u.be.res = 0;
67186
- u.be.iKey = pIn3->u.i;
67187
- rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
67188
- u.be.pC->lastRowid = pIn3->u.i;
67189
- u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
67190
- u.be.pC->nullRow = 0;
67191
- u.be.pC->cacheStatus = CACHE_STALE;
67192
- u.be.pC->deferredMoveto = 0;
67193
- if( u.be.res!=0 ){
68497
+ u.bf.pC = p->apCsr[pOp->p1];
68498
+ assert( u.bf.pC!=0 );
68499
+ assert( u.bf.pC->isTable );
68500
+ assert( u.bf.pC->pseudoTableReg==0 );
68501
+ u.bf.pCrsr = u.bf.pC->pCursor;
68502
+ if( ALWAYS(u.bf.pCrsr!=0) ){
68503
+ u.bf.res = 0;
68504
+ u.bf.iKey = pIn3->u.i;
68505
+ rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
68506
+ u.bf.pC->lastRowid = pIn3->u.i;
68507
+ u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
68508
+ u.bf.pC->nullRow = 0;
68509
+ u.bf.pC->cacheStatus = CACHE_STALE;
68510
+ u.bf.pC->deferredMoveto = 0;
68511
+ if( u.bf.res!=0 ){
6719468512
pc = pOp->p2 - 1;
67195
- assert( u.be.pC->rowidIsValid==0 );
68513
+ assert( u.bf.pC->rowidIsValid==0 );
6719668514
}
67197
- u.be.pC->seekResult = u.be.res;
68515
+ u.bf.pC->seekResult = u.bf.res;
6719868516
}else{
6719968517
/* This happens when an attempt to open a read cursor on the
6720068518
** sqlite_master table returns SQLITE_EMPTY.
6720168519
*/
6720268520
pc = pOp->p2 - 1;
67203
- assert( u.be.pC->rowidIsValid==0 );
67204
- u.be.pC->seekResult = 0;
68521
+ assert( u.bf.pC->rowidIsValid==0 );
68522
+ u.bf.pC->seekResult = 0;
6720568523
}
6720668524
break;
6720768525
}
6720868526
6720968527
/* Opcode: Sequence P1 P2 * * *
@@ -67234,25 +68552,25 @@
6723468552
** an SQLITE_FULL error is generated. The P3 register is updated with the '
6723568553
** generated record number. This P3 mechanism is used to help implement the
6723668554
** AUTOINCREMENT feature.
6723768555
*/
6723868556
case OP_NewRowid: { /* out2-prerelease */
67239
-#if 0 /* local variables moved into u.bf */
68557
+#if 0 /* local variables moved into u.bg */
6724068558
i64 v; /* The new rowid */
6724168559
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6724268560
int res; /* Result of an sqlite3BtreeLast() */
6724368561
int cnt; /* Counter to limit the number of searches */
6724468562
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6724568563
VdbeFrame *pFrame; /* Root frame of VDBE */
67246
-#endif /* local variables moved into u.bf */
68564
+#endif /* local variables moved into u.bg */
6724768565
67248
- u.bf.v = 0;
67249
- u.bf.res = 0;
68566
+ u.bg.v = 0;
68567
+ u.bg.res = 0;
6725068568
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67251
- u.bf.pC = p->apCsr[pOp->p1];
67252
- assert( u.bf.pC!=0 );
67253
- if( NEVER(u.bf.pC->pCursor==0) ){
68569
+ u.bg.pC = p->apCsr[pOp->p1];
68570
+ assert( u.bg.pC!=0 );
68571
+ if( NEVER(u.bg.pC->pCursor==0) ){
6725468572
/* The zero initialization above is all that is needed */
6725568573
}else{
6725668574
/* The next rowid or record number (different terms for the same
6725768575
** thing) is obtained in a two-step algorithm.
6725868576
**
@@ -67264,11 +68582,11 @@
6726468582
** The second algorithm is to select a rowid at random and see if
6726568583
** it already exists in the table. If it does not exist, we have
6726668584
** succeeded. If the random rowid does exist, we select a new one
6726768585
** and try again, up to 100 times.
6726868586
*/
67269
- assert( u.bf.pC->isTable );
68587
+ assert( u.bg.pC->isTable );
6727068588
6727168589
#ifdef SQLITE_32BIT_ROWID
6727268590
# define MAX_ROWID 0x7fffffff
6727368591
#else
6727468592
/* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67276,101 +68594,101 @@
6727668594
** to provide the constant while making all compilers happy.
6727768595
*/
6727868596
# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
6727968597
#endif
6728068598
67281
- if( !u.bf.pC->useRandomRowid ){
67282
- u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
67283
- if( u.bf.v==0 ){
67284
- rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
68599
+ if( !u.bg.pC->useRandomRowid ){
68600
+ u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
68601
+ if( u.bg.v==0 ){
68602
+ rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
6728568603
if( rc!=SQLITE_OK ){
6728668604
goto abort_due_to_error;
6728768605
}
67288
- if( u.bf.res ){
67289
- u.bf.v = 1; /* IMP: R-61914-48074 */
68606
+ if( u.bg.res ){
68607
+ u.bg.v = 1; /* IMP: R-61914-48074 */
6729068608
}else{
67291
- assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
67292
- rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
68609
+ assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
68610
+ rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
6729368611
assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67294
- if( u.bf.v==MAX_ROWID ){
67295
- u.bf.pC->useRandomRowid = 1;
68612
+ if( u.bg.v==MAX_ROWID ){
68613
+ u.bg.pC->useRandomRowid = 1;
6729668614
}else{
67297
- u.bf.v++; /* IMP: R-29538-34987 */
68615
+ u.bg.v++; /* IMP: R-29538-34987 */
6729868616
}
6729968617
}
6730068618
}
6730168619
6730268620
#ifndef SQLITE_OMIT_AUTOINCREMENT
6730368621
if( pOp->p3 ){
6730468622
/* Assert that P3 is a valid memory cell. */
6730568623
assert( pOp->p3>0 );
6730668624
if( p->pFrame ){
67307
- for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
68625
+ for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
6730868626
/* Assert that P3 is a valid memory cell. */
67309
- assert( pOp->p3<=u.bf.pFrame->nMem );
67310
- u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
68627
+ assert( pOp->p3<=u.bg.pFrame->nMem );
68628
+ u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
6731168629
}else{
6731268630
/* Assert that P3 is a valid memory cell. */
6731368631
assert( pOp->p3<=p->nMem );
67314
- u.bf.pMem = &aMem[pOp->p3];
67315
- memAboutToChange(p, u.bf.pMem);
67316
- }
67317
- assert( memIsValid(u.bf.pMem) );
67318
-
67319
- REGISTER_TRACE(pOp->p3, u.bf.pMem);
67320
- sqlite3VdbeMemIntegerify(u.bf.pMem);
67321
- assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67322
- if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
68632
+ u.bg.pMem = &aMem[pOp->p3];
68633
+ memAboutToChange(p, u.bg.pMem);
68634
+ }
68635
+ assert( memIsValid(u.bg.pMem) );
68636
+
68637
+ REGISTER_TRACE(pOp->p3, u.bg.pMem);
68638
+ sqlite3VdbeMemIntegerify(u.bg.pMem);
68639
+ assert( (u.bg.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
68640
+ if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
6732368641
rc = SQLITE_FULL; /* IMP: R-12275-61338 */
6732468642
goto abort_due_to_error;
6732568643
}
67326
- if( u.bf.v<u.bf.pMem->u.i+1 ){
67327
- u.bf.v = u.bf.pMem->u.i + 1;
68644
+ if( u.bg.v<u.bg.pMem->u.i+1 ){
68645
+ u.bg.v = u.bg.pMem->u.i + 1;
6732868646
}
67329
- u.bf.pMem->u.i = u.bf.v;
68647
+ u.bg.pMem->u.i = u.bg.v;
6733068648
}
6733168649
#endif
6733268650
67333
- sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
68651
+ sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
6733468652
}
67335
- if( u.bf.pC->useRandomRowid ){
68653
+ if( u.bg.pC->useRandomRowid ){
6733668654
/* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
6733768655
** largest possible integer (9223372036854775807) then the database
6733868656
** engine starts picking positive candidate ROWIDs at random until
6733968657
** it finds one that is not previously used. */
6734068658
assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
6734168659
** an AUTOINCREMENT table. */
6734268660
/* on the first attempt, simply do one more than previous */
67343
- u.bf.v = lastRowid;
67344
- u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67345
- u.bf.v++; /* ensure non-zero */
67346
- u.bf.cnt = 0;
67347
- while( ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
67348
- 0, &u.bf.res))==SQLITE_OK)
67349
- && (u.bf.res==0)
67350
- && (++u.bf.cnt<100)){
68661
+ u.bg.v = lastRowid;
68662
+ u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
68663
+ u.bg.v++; /* ensure non-zero */
68664
+ u.bg.cnt = 0;
68665
+ while( ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
68666
+ 0, &u.bg.res))==SQLITE_OK)
68667
+ && (u.bg.res==0)
68668
+ && (++u.bg.cnt<100)){
6735168669
/* collision - try another random rowid */
67352
- sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
67353
- if( u.bf.cnt<5 ){
68670
+ sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
68671
+ if( u.bg.cnt<5 ){
6735468672
/* try "small" random rowids for the initial attempts */
67355
- u.bf.v &= 0xffffff;
68673
+ u.bg.v &= 0xffffff;
6735668674
}else{
67357
- u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
68675
+ u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
6735868676
}
67359
- u.bf.v++; /* ensure non-zero */
68677
+ u.bg.v++; /* ensure non-zero */
6736068678
}
67361
- if( rc==SQLITE_OK && u.bf.res==0 ){
68679
+ if( rc==SQLITE_OK && u.bg.res==0 ){
6736268680
rc = SQLITE_FULL; /* IMP: R-38219-53002 */
6736368681
goto abort_due_to_error;
6736468682
}
67365
- assert( u.bf.v>0 ); /* EV: R-40812-03570 */
68683
+ assert( u.bg.v>0 ); /* EV: R-40812-03570 */
6736668684
}
67367
- u.bf.pC->rowidIsValid = 0;
67368
- u.bf.pC->deferredMoveto = 0;
67369
- u.bf.pC->cacheStatus = CACHE_STALE;
68685
+ u.bg.pC->rowidIsValid = 0;
68686
+ u.bg.pC->deferredMoveto = 0;
68687
+ u.bg.pC->cacheStatus = CACHE_STALE;
6737068688
}
67371
- pOut->u.i = u.bf.v;
68689
+ pOut->u.i = u.bg.v;
6737268690
break;
6737368691
}
6737468692
6737568693
/* Opcode: Insert P1 P2 P3 P4 P5
6737668694
**
@@ -67416,74 +68734,74 @@
6741668734
** This works exactly like OP_Insert except that the key is the
6741768735
** integer value P3, not the value of the integer stored in register P3.
6741868736
*/
6741968737
case OP_Insert:
6742068738
case OP_InsertInt: {
67421
-#if 0 /* local variables moved into u.bg */
68739
+#if 0 /* local variables moved into u.bh */
6742268740
Mem *pData; /* MEM cell holding data for the record to be inserted */
6742368741
Mem *pKey; /* MEM cell holding key for the record */
6742468742
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6742568743
VdbeCursor *pC; /* Cursor to table into which insert is written */
6742668744
int nZero; /* Number of zero-bytes to append */
6742768745
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6742868746
const char *zDb; /* database name - used by the update hook */
6742968747
const char *zTbl; /* Table name - used by the opdate hook */
6743068748
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67431
-#endif /* local variables moved into u.bg */
68749
+#endif /* local variables moved into u.bh */
6743268750
67433
- u.bg.pData = &aMem[pOp->p2];
68751
+ u.bh.pData = &aMem[pOp->p2];
6743468752
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67435
- assert( memIsValid(u.bg.pData) );
67436
- u.bg.pC = p->apCsr[pOp->p1];
67437
- assert( u.bg.pC!=0 );
67438
- assert( u.bg.pC->pCursor!=0 );
67439
- assert( u.bg.pC->pseudoTableReg==0 );
67440
- assert( u.bg.pC->isTable );
67441
- REGISTER_TRACE(pOp->p2, u.bg.pData);
68753
+ assert( memIsValid(u.bh.pData) );
68754
+ u.bh.pC = p->apCsr[pOp->p1];
68755
+ assert( u.bh.pC!=0 );
68756
+ assert( u.bh.pC->pCursor!=0 );
68757
+ assert( u.bh.pC->pseudoTableReg==0 );
68758
+ assert( u.bh.pC->isTable );
68759
+ REGISTER_TRACE(pOp->p2, u.bh.pData);
6744268760
6744368761
if( pOp->opcode==OP_Insert ){
67444
- u.bg.pKey = &aMem[pOp->p3];
67445
- assert( u.bg.pKey->flags & MEM_Int );
67446
- assert( memIsValid(u.bg.pKey) );
67447
- REGISTER_TRACE(pOp->p3, u.bg.pKey);
67448
- u.bg.iKey = u.bg.pKey->u.i;
68762
+ u.bh.pKey = &aMem[pOp->p3];
68763
+ assert( u.bh.pKey->flags & MEM_Int );
68764
+ assert( memIsValid(u.bh.pKey) );
68765
+ REGISTER_TRACE(pOp->p3, u.bh.pKey);
68766
+ u.bh.iKey = u.bh.pKey->u.i;
6744968767
}else{
6745068768
assert( pOp->opcode==OP_InsertInt );
67451
- u.bg.iKey = pOp->p3;
68769
+ u.bh.iKey = pOp->p3;
6745268770
}
6745368771
6745468772
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67455
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
67456
- if( u.bg.pData->flags & MEM_Null ){
67457
- u.bg.pData->z = 0;
67458
- u.bg.pData->n = 0;
67459
- }else{
67460
- assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
67461
- }
67462
- u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
67463
- if( u.bg.pData->flags & MEM_Zero ){
67464
- u.bg.nZero = u.bg.pData->u.nZero;
67465
- }else{
67466
- u.bg.nZero = 0;
67467
- }
67468
- sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67469
- rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
67470
- u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
67471
- pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
68773
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
68774
+ if( u.bh.pData->flags & MEM_Null ){
68775
+ u.bh.pData->z = 0;
68776
+ u.bh.pData->n = 0;
68777
+ }else{
68778
+ assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
68779
+ }
68780
+ u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
68781
+ if( u.bh.pData->flags & MEM_Zero ){
68782
+ u.bh.nZero = u.bh.pData->u.nZero;
68783
+ }else{
68784
+ u.bh.nZero = 0;
68785
+ }
68786
+ sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
68787
+ rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
68788
+ u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
68789
+ pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
6747268790
);
67473
- u.bg.pC->rowidIsValid = 0;
67474
- u.bg.pC->deferredMoveto = 0;
67475
- u.bg.pC->cacheStatus = CACHE_STALE;
68791
+ u.bh.pC->rowidIsValid = 0;
68792
+ u.bh.pC->deferredMoveto = 0;
68793
+ u.bh.pC->cacheStatus = CACHE_STALE;
6747668794
6747768795
/* Invoke the update-hook if required. */
6747868796
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67479
- u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
67480
- u.bg.zTbl = pOp->p4.z;
67481
- u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67482
- assert( u.bg.pC->isTable );
67483
- db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
67484
- assert( u.bg.pC->iDb>=0 );
68797
+ u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
68798
+ u.bh.zTbl = pOp->p4.z;
68799
+ u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
68800
+ assert( u.bh.pC->isTable );
68801
+ db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
68802
+ assert( u.bh.pC->iDb>=0 );
6748568803
}
6748668804
break;
6748768805
}
6748868806
6748968807
/* Opcode: Delete P1 P2 * P4 *
@@ -67505,51 +68823,51 @@
6750568823
** pointing to. The update hook will be invoked, if it exists.
6750668824
** If P4 is not NULL then the P1 cursor must have been positioned
6750768825
** using OP_NotFound prior to invoking this opcode.
6750868826
*/
6750968827
case OP_Delete: {
67510
-#if 0 /* local variables moved into u.bh */
68828
+#if 0 /* local variables moved into u.bi */
6751168829
i64 iKey;
6751268830
VdbeCursor *pC;
67513
-#endif /* local variables moved into u.bh */
68831
+#endif /* local variables moved into u.bi */
6751468832
67515
- u.bh.iKey = 0;
68833
+ u.bi.iKey = 0;
6751668834
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67517
- u.bh.pC = p->apCsr[pOp->p1];
67518
- assert( u.bh.pC!=0 );
67519
- assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
68835
+ u.bi.pC = p->apCsr[pOp->p1];
68836
+ assert( u.bi.pC!=0 );
68837
+ assert( u.bi.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
6752068838
67521
- /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
68839
+ /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
6752268840
** row being deleted.
6752368841
*/
6752468842
if( db->xUpdateCallback && pOp->p4.z ){
67525
- assert( u.bh.pC->isTable );
67526
- assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67527
- u.bh.iKey = u.bh.pC->lastRowid;
68843
+ assert( u.bi.pC->isTable );
68844
+ assert( u.bi.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
68845
+ u.bi.iKey = u.bi.pC->lastRowid;
6752868846
}
6752968847
6753068848
/* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
6753168849
** OP_Column on the same table without any intervening operations that
67532
- ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
68850
+ ** might move or invalidate the cursor. Hence cursor u.bi.pC is always pointing
6753368851
** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
6753468852
** below is always a no-op and cannot fail. We will run it anyhow, though,
6753568853
** to guard against future changes to the code generator.
6753668854
**/
67537
- assert( u.bh.pC->deferredMoveto==0 );
67538
- rc = sqlite3VdbeCursorMoveto(u.bh.pC);
68855
+ assert( u.bi.pC->deferredMoveto==0 );
68856
+ rc = sqlite3VdbeCursorMoveto(u.bi.pC);
6753968857
if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
6754068858
67541
- sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67542
- rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
67543
- u.bh.pC->cacheStatus = CACHE_STALE;
68859
+ sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
68860
+ rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
68861
+ u.bi.pC->cacheStatus = CACHE_STALE;
6754468862
6754568863
/* Invoke the update-hook if required. */
6754668864
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67547
- const char *zDb = db->aDb[u.bh.pC->iDb].zName;
68865
+ const char *zDb = db->aDb[u.bi.pC->iDb].zName;
6754868866
const char *zTbl = pOp->p4.z;
67549
- db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
67550
- assert( u.bh.pC->iDb>=0 );
68867
+ db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
68868
+ assert( u.bi.pC->iDb>=0 );
6755168869
}
6755268870
if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
6755368871
break;
6755468872
}
6755568873
/* Opcode: ResetCount * * * * *
@@ -67571,20 +68889,20 @@
6757168889
** register P3 with the entry that the sorter cursor currently points to.
6757268890
** If, excluding the rowid fields at the end, the two records are a match,
6757368891
** fall through to the next instruction. Otherwise, jump to instruction P2.
6757468892
*/
6757568893
case OP_SorterCompare: {
67576
-#if 0 /* local variables moved into u.bi */
68894
+#if 0 /* local variables moved into u.bj */
6757768895
VdbeCursor *pC;
6757868896
int res;
67579
-#endif /* local variables moved into u.bi */
68897
+#endif /* local variables moved into u.bj */
6758068898
67581
- u.bi.pC = p->apCsr[pOp->p1];
67582
- assert( isSorter(u.bi.pC) );
68899
+ u.bj.pC = p->apCsr[pOp->p1];
68900
+ assert( isSorter(u.bj.pC) );
6758368901
pIn3 = &aMem[pOp->p3];
67584
- rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
67585
- if( u.bi.res ){
68902
+ rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
68903
+ if( u.bj.res ){
6758668904
pc = pOp->p2-1;
6758768905
}
6758868906
break;
6758968907
};
6759068908
@@ -67591,18 +68909,18 @@
6759168909
/* Opcode: SorterData P1 P2 * * *
6759268910
**
6759368911
** Write into register P2 the current sorter data for sorter cursor P1.
6759468912
*/
6759568913
case OP_SorterData: {
67596
-#if 0 /* local variables moved into u.bj */
68914
+#if 0 /* local variables moved into u.bk */
6759768915
VdbeCursor *pC;
67598
-#endif /* local variables moved into u.bj */
68916
+#endif /* local variables moved into u.bk */
6759968917
#ifndef SQLITE_OMIT_MERGE_SORT
6760068918
pOut = &aMem[pOp->p2];
67601
- u.bj.pC = p->apCsr[pOp->p1];
67602
- assert( u.bj.pC->isSorter );
67603
- rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
68919
+ u.bk.pC = p->apCsr[pOp->p1];
68920
+ assert( u.bk.pC->isSorter );
68921
+ rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
6760468922
#else
6760568923
pOp->opcode = OP_RowKey;
6760668924
pc--;
6760768925
#endif
6760868926
break;
@@ -67628,67 +68946,67 @@
6762868946
** If the P1 cursor must be pointing to a valid row (not a NULL row)
6762968947
** of a real table, not a pseudo-table.
6763068948
*/
6763168949
case OP_RowKey:
6763268950
case OP_RowData: {
67633
-#if 0 /* local variables moved into u.bk */
68951
+#if 0 /* local variables moved into u.bl */
6763468952
VdbeCursor *pC;
6763568953
BtCursor *pCrsr;
6763668954
u32 n;
6763768955
i64 n64;
67638
-#endif /* local variables moved into u.bk */
68956
+#endif /* local variables moved into u.bl */
6763968957
6764068958
pOut = &aMem[pOp->p2];
6764168959
memAboutToChange(p, pOut);
6764268960
6764368961
/* Note that RowKey and RowData are really exactly the same instruction */
6764468962
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67645
- u.bk.pC = p->apCsr[pOp->p1];
67646
- assert( u.bk.pC->isSorter==0 );
67647
- assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
67648
- assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
67649
- assert( u.bk.pC!=0 );
67650
- assert( u.bk.pC->nullRow==0 );
67651
- assert( u.bk.pC->pseudoTableReg==0 );
67652
- assert( !u.bk.pC->isSorter );
67653
- assert( u.bk.pC->pCursor!=0 );
67654
- u.bk.pCrsr = u.bk.pC->pCursor;
67655
- assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
68963
+ u.bl.pC = p->apCsr[pOp->p1];
68964
+ assert( u.bl.pC->isSorter==0 );
68965
+ assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
68966
+ assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
68967
+ assert( u.bl.pC!=0 );
68968
+ assert( u.bl.pC->nullRow==0 );
68969
+ assert( u.bl.pC->pseudoTableReg==0 );
68970
+ assert( !u.bl.pC->isSorter );
68971
+ assert( u.bl.pC->pCursor!=0 );
68972
+ u.bl.pCrsr = u.bl.pC->pCursor;
68973
+ assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
6765668974
6765768975
/* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
6765868976
** OP_Rewind/Op_Next with no intervening instructions that might invalidate
6765968977
** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
6766068978
** a no-op and can never fail. But we leave it in place as a safety.
6766168979
*/
67662
- assert( u.bk.pC->deferredMoveto==0 );
67663
- rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67664
- if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67665
-
67666
- if( u.bk.pC->isIndex ){
67667
- assert( !u.bk.pC->isTable );
67668
- VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67669
- assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67670
- if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67671
- goto too_big;
67672
- }
67673
- u.bk.n = (u32)u.bk.n64;
67674
- }else{
67675
- VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67676
- assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67677
- if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67678
- goto too_big;
67679
- }
67680
- }
67681
- if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
67682
- goto no_mem;
67683
- }
67684
- pOut->n = u.bk.n;
67685
- MemSetTypeFlag(pOut, MEM_Blob);
67686
- if( u.bk.pC->isIndex ){
67687
- rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67688
- }else{
67689
- rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
68980
+ assert( u.bl.pC->deferredMoveto==0 );
68981
+ rc = sqlite3VdbeCursorMoveto(u.bl.pC);
68982
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
68983
+
68984
+ if( u.bl.pC->isIndex ){
68985
+ assert( !u.bl.pC->isTable );
68986
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
68987
+ assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
68988
+ if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68989
+ goto too_big;
68990
+ }
68991
+ u.bl.n = (u32)u.bl.n64;
68992
+ }else{
68993
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
68994
+ assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
68995
+ if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
68996
+ goto too_big;
68997
+ }
68998
+ }
68999
+ if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
69000
+ goto no_mem;
69001
+ }
69002
+ pOut->n = u.bl.n;
69003
+ MemSetTypeFlag(pOut, MEM_Blob);
69004
+ if( u.bl.pC->isIndex ){
69005
+ rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
69006
+ }else{
69007
+ rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
6769069008
}
6769169009
pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
6769269010
UPDATE_MAX_BLOBSIZE(pOut);
6769369011
break;
6769469012
}
@@ -67701,46 +69019,46 @@
6770169019
** P1 can be either an ordinary table or a virtual table. There used to
6770269020
** be a separate OP_VRowid opcode for use with virtual tables, but this
6770369021
** one opcode now works for both table types.
6770469022
*/
6770569023
case OP_Rowid: { /* out2-prerelease */
67706
-#if 0 /* local variables moved into u.bl */
69024
+#if 0 /* local variables moved into u.bm */
6770769025
VdbeCursor *pC;
6770869026
i64 v;
6770969027
sqlite3_vtab *pVtab;
6771069028
const sqlite3_module *pModule;
67711
-#endif /* local variables moved into u.bl */
69029
+#endif /* local variables moved into u.bm */
6771269030
6771369031
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67714
- u.bl.pC = p->apCsr[pOp->p1];
67715
- assert( u.bl.pC!=0 );
67716
- assert( u.bl.pC->pseudoTableReg==0 );
67717
- if( u.bl.pC->nullRow ){
69032
+ u.bm.pC = p->apCsr[pOp->p1];
69033
+ assert( u.bm.pC!=0 );
69034
+ assert( u.bm.pC->pseudoTableReg==0 );
69035
+ if( u.bm.pC->nullRow ){
6771869036
pOut->flags = MEM_Null;
6771969037
break;
67720
- }else if( u.bl.pC->deferredMoveto ){
67721
- u.bl.v = u.bl.pC->movetoTarget;
69038
+ }else if( u.bm.pC->deferredMoveto ){
69039
+ u.bm.v = u.bm.pC->movetoTarget;
6772269040
#ifndef SQLITE_OMIT_VIRTUALTABLE
67723
- }else if( u.bl.pC->pVtabCursor ){
67724
- u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
67725
- u.bl.pModule = u.bl.pVtab->pModule;
67726
- assert( u.bl.pModule->xRowid );
67727
- rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
67728
- importVtabErrMsg(p, u.bl.pVtab);
69041
+ }else if( u.bm.pC->pVtabCursor ){
69042
+ u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
69043
+ u.bm.pModule = u.bm.pVtab->pModule;
69044
+ assert( u.bm.pModule->xRowid );
69045
+ rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
69046
+ importVtabErrMsg(p, u.bm.pVtab);
6772969047
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6773069048
}else{
67731
- assert( u.bl.pC->pCursor!=0 );
67732
- rc = sqlite3VdbeCursorMoveto(u.bl.pC);
69049
+ assert( u.bm.pC->pCursor!=0 );
69050
+ rc = sqlite3VdbeCursorMoveto(u.bm.pC);
6773369051
if( rc ) goto abort_due_to_error;
67734
- if( u.bl.pC->rowidIsValid ){
67735
- u.bl.v = u.bl.pC->lastRowid;
69052
+ if( u.bm.pC->rowidIsValid ){
69053
+ u.bm.v = u.bm.pC->lastRowid;
6773669054
}else{
67737
- rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
69055
+ rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
6773869056
assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
6773969057
}
6774069058
}
67741
- pOut->u.i = u.bl.v;
69059
+ pOut->u.i = u.bm.v;
6774269060
break;
6774369061
}
6774469062
6774569063
/* Opcode: NullRow P1 * * * *
6774669064
**
@@ -67747,22 +69065,22 @@
6774769065
** Move the cursor P1 to a null row. Any OP_Column operations
6774869066
** that occur while the cursor is on the null row will always
6774969067
** write a NULL.
6775069068
*/
6775169069
case OP_NullRow: {
67752
-#if 0 /* local variables moved into u.bm */
69070
+#if 0 /* local variables moved into u.bn */
6775369071
VdbeCursor *pC;
67754
-#endif /* local variables moved into u.bm */
69072
+#endif /* local variables moved into u.bn */
6775569073
6775669074
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67757
- u.bm.pC = p->apCsr[pOp->p1];
67758
- assert( u.bm.pC!=0 );
67759
- u.bm.pC->nullRow = 1;
67760
- u.bm.pC->rowidIsValid = 0;
67761
- assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
67762
- if( u.bm.pC->pCursor ){
67763
- sqlite3BtreeClearCursor(u.bm.pC->pCursor);
69075
+ u.bn.pC = p->apCsr[pOp->p1];
69076
+ assert( u.bn.pC!=0 );
69077
+ u.bn.pC->nullRow = 1;
69078
+ u.bn.pC->rowidIsValid = 0;
69079
+ assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
69080
+ if( u.bn.pC->pCursor ){
69081
+ sqlite3BtreeClearCursor(u.bn.pC->pCursor);
6776469082
}
6776569083
break;
6776669084
}
6776769085
6776869086
/* Opcode: Last P1 P2 * * *
@@ -67772,29 +69090,29 @@
6777269090
** If the table or index is empty and P2>0, then jump immediately to P2.
6777369091
** If P2 is 0 or if the table or index is not empty, fall through
6777469092
** to the following instruction.
6777569093
*/
6777669094
case OP_Last: { /* jump */
67777
-#if 0 /* local variables moved into u.bn */
69095
+#if 0 /* local variables moved into u.bo */
6777869096
VdbeCursor *pC;
6777969097
BtCursor *pCrsr;
6778069098
int res;
67781
-#endif /* local variables moved into u.bn */
69099
+#endif /* local variables moved into u.bo */
6778269100
6778369101
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67784
- u.bn.pC = p->apCsr[pOp->p1];
67785
- assert( u.bn.pC!=0 );
67786
- u.bn.pCrsr = u.bn.pC->pCursor;
67787
- u.bn.res = 0;
67788
- if( ALWAYS(u.bn.pCrsr!=0) ){
67789
- rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
67790
- }
67791
- u.bn.pC->nullRow = (u8)u.bn.res;
67792
- u.bn.pC->deferredMoveto = 0;
67793
- u.bn.pC->rowidIsValid = 0;
67794
- u.bn.pC->cacheStatus = CACHE_STALE;
67795
- if( pOp->p2>0 && u.bn.res ){
69102
+ u.bo.pC = p->apCsr[pOp->p1];
69103
+ assert( u.bo.pC!=0 );
69104
+ u.bo.pCrsr = u.bo.pC->pCursor;
69105
+ u.bo.res = 0;
69106
+ if( ALWAYS(u.bo.pCrsr!=0) ){
69107
+ rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
69108
+ }
69109
+ u.bo.pC->nullRow = (u8)u.bo.res;
69110
+ u.bo.pC->deferredMoveto = 0;
69111
+ u.bo.pC->rowidIsValid = 0;
69112
+ u.bo.pC->cacheStatus = CACHE_STALE;
69113
+ if( pOp->p2>0 && u.bo.res ){
6779669114
pc = pOp->p2 - 1;
6779769115
}
6779869116
break;
6779969117
}
6780069118
@@ -67830,35 +69148,35 @@
6783069148
** If the table or index is empty and P2>0, then jump immediately to P2.
6783169149
** If P2 is 0 or if the table or index is not empty, fall through
6783269150
** to the following instruction.
6783369151
*/
6783469152
case OP_Rewind: { /* jump */
67835
-#if 0 /* local variables moved into u.bo */
69153
+#if 0 /* local variables moved into u.bp */
6783669154
VdbeCursor *pC;
6783769155
BtCursor *pCrsr;
6783869156
int res;
67839
-#endif /* local variables moved into u.bo */
69157
+#endif /* local variables moved into u.bp */
6784069158
6784169159
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67842
- u.bo.pC = p->apCsr[pOp->p1];
67843
- assert( u.bo.pC!=0 );
67844
- assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
67845
- u.bo.res = 1;
67846
- if( isSorter(u.bo.pC) ){
67847
- rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
69160
+ u.bp.pC = p->apCsr[pOp->p1];
69161
+ assert( u.bp.pC!=0 );
69162
+ assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
69163
+ u.bp.res = 1;
69164
+ if( isSorter(u.bp.pC) ){
69165
+ rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
6784869166
}else{
67849
- u.bo.pCrsr = u.bo.pC->pCursor;
67850
- assert( u.bo.pCrsr );
67851
- rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
67852
- u.bo.pC->atFirst = u.bo.res==0 ?1:0;
67853
- u.bo.pC->deferredMoveto = 0;
67854
- u.bo.pC->cacheStatus = CACHE_STALE;
67855
- u.bo.pC->rowidIsValid = 0;
67856
- }
67857
- u.bo.pC->nullRow = (u8)u.bo.res;
69167
+ u.bp.pCrsr = u.bp.pC->pCursor;
69168
+ assert( u.bp.pCrsr );
69169
+ rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
69170
+ u.bp.pC->atFirst = u.bp.res==0 ?1:0;
69171
+ u.bp.pC->deferredMoveto = 0;
69172
+ u.bp.pC->cacheStatus = CACHE_STALE;
69173
+ u.bp.pC->rowidIsValid = 0;
69174
+ }
69175
+ u.bp.pC->nullRow = (u8)u.bp.res;
6785869176
assert( pOp->p2>0 && pOp->p2<p->nOp );
67859
- if( u.bo.res ){
69177
+ if( u.bp.res ){
6786069178
pc = pOp->p2 - 1;
6786169179
}
6786269180
break;
6786369181
}
6786469182
@@ -67898,44 +69216,44 @@
6789869216
#ifdef SQLITE_OMIT_MERGE_SORT
6789969217
pOp->opcode = OP_Next;
6790069218
#endif
6790169219
case OP_Prev: /* jump */
6790269220
case OP_Next: { /* jump */
67903
-#if 0 /* local variables moved into u.bp */
69221
+#if 0 /* local variables moved into u.bq */
6790469222
VdbeCursor *pC;
6790569223
int res;
67906
-#endif /* local variables moved into u.bp */
69224
+#endif /* local variables moved into u.bq */
6790769225
6790869226
CHECK_FOR_INTERRUPT;
6790969227
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6791069228
assert( pOp->p5<=ArraySize(p->aCounter) );
67911
- u.bp.pC = p->apCsr[pOp->p1];
67912
- if( u.bp.pC==0 ){
69229
+ u.bq.pC = p->apCsr[pOp->p1];
69230
+ if( u.bq.pC==0 ){
6791369231
break; /* See ticket #2273 */
6791469232
}
67915
- assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
67916
- if( isSorter(u.bp.pC) ){
69233
+ assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
69234
+ if( isSorter(u.bq.pC) ){
6791769235
assert( pOp->opcode==OP_SorterNext );
67918
- rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
69236
+ rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
6791969237
}else{
67920
- u.bp.res = 1;
67921
- assert( u.bp.pC->deferredMoveto==0 );
67922
- assert( u.bp.pC->pCursor );
69238
+ u.bq.res = 1;
69239
+ assert( u.bq.pC->deferredMoveto==0 );
69240
+ assert( u.bq.pC->pCursor );
6792369241
assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
6792469242
assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
67925
- rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
69243
+ rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
6792669244
}
67927
- u.bp.pC->nullRow = (u8)u.bp.res;
67928
- u.bp.pC->cacheStatus = CACHE_STALE;
67929
- if( u.bp.res==0 ){
69245
+ u.bq.pC->nullRow = (u8)u.bq.res;
69246
+ u.bq.pC->cacheStatus = CACHE_STALE;
69247
+ if( u.bq.res==0 ){
6793069248
pc = pOp->p2 - 1;
6793169249
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
6793269250
#ifdef SQLITE_TEST
6793369251
sqlite3_search_count++;
6793469252
#endif
6793569253
}
67936
- u.bp.pC->rowidIsValid = 0;
69254
+ u.bq.pC->rowidIsValid = 0;
6793769255
break;
6793869256
}
6793969257
6794069258
/* Opcode: IdxInsert P1 P2 P3 * P5
6794169259
**
@@ -67952,38 +69270,38 @@
6795269270
case OP_SorterInsert: /* in2 */
6795369271
#ifdef SQLITE_OMIT_MERGE_SORT
6795469272
pOp->opcode = OP_IdxInsert;
6795569273
#endif
6795669274
case OP_IdxInsert: { /* in2 */
67957
-#if 0 /* local variables moved into u.bq */
69275
+#if 0 /* local variables moved into u.br */
6795869276
VdbeCursor *pC;
6795969277
BtCursor *pCrsr;
6796069278
int nKey;
6796169279
const char *zKey;
67962
-#endif /* local variables moved into u.bq */
69280
+#endif /* local variables moved into u.br */
6796369281
6796469282
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67965
- u.bq.pC = p->apCsr[pOp->p1];
67966
- assert( u.bq.pC!=0 );
67967
- assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
69283
+ u.br.pC = p->apCsr[pOp->p1];
69284
+ assert( u.br.pC!=0 );
69285
+ assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
6796869286
pIn2 = &aMem[pOp->p2];
6796969287
assert( pIn2->flags & MEM_Blob );
67970
- u.bq.pCrsr = u.bq.pC->pCursor;
67971
- if( ALWAYS(u.bq.pCrsr!=0) ){
67972
- assert( u.bq.pC->isTable==0 );
69288
+ u.br.pCrsr = u.br.pC->pCursor;
69289
+ if( ALWAYS(u.br.pCrsr!=0) ){
69290
+ assert( u.br.pC->isTable==0 );
6797369291
rc = ExpandBlob(pIn2);
6797469292
if( rc==SQLITE_OK ){
67975
- if( isSorter(u.bq.pC) ){
67976
- rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
69293
+ if( isSorter(u.br.pC) ){
69294
+ rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
6797769295
}else{
67978
- u.bq.nKey = pIn2->n;
67979
- u.bq.zKey = pIn2->z;
67980
- rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
67981
- ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
69296
+ u.br.nKey = pIn2->n;
69297
+ u.br.zKey = pIn2->z;
69298
+ rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
69299
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
6798269300
);
67983
- assert( u.bq.pC->deferredMoveto==0 );
67984
- u.bq.pC->cacheStatus = CACHE_STALE;
69301
+ assert( u.br.pC->deferredMoveto==0 );
69302
+ u.br.pC->cacheStatus = CACHE_STALE;
6798569303
}
6798669304
}
6798769305
}
6798869306
break;
6798969307
}
@@ -67993,37 +69311,37 @@
6799369311
** The content of P3 registers starting at register P2 form
6799469312
** an unpacked index key. This opcode removes that entry from the
6799569313
** index opened by cursor P1.
6799669314
*/
6799769315
case OP_IdxDelete: {
67998
-#if 0 /* local variables moved into u.br */
69316
+#if 0 /* local variables moved into u.bs */
6799969317
VdbeCursor *pC;
6800069318
BtCursor *pCrsr;
6800169319
int res;
6800269320
UnpackedRecord r;
68003
-#endif /* local variables moved into u.br */
69321
+#endif /* local variables moved into u.bs */
6800469322
6800569323
assert( pOp->p3>0 );
6800669324
assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
6800769325
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68008
- u.br.pC = p->apCsr[pOp->p1];
68009
- assert( u.br.pC!=0 );
68010
- u.br.pCrsr = u.br.pC->pCursor;
68011
- if( ALWAYS(u.br.pCrsr!=0) ){
68012
- u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
68013
- u.br.r.nField = (u16)pOp->p3;
68014
- u.br.r.flags = 0;
68015
- u.br.r.aMem = &aMem[pOp->p2];
69326
+ u.bs.pC = p->apCsr[pOp->p1];
69327
+ assert( u.bs.pC!=0 );
69328
+ u.bs.pCrsr = u.bs.pC->pCursor;
69329
+ if( ALWAYS(u.bs.pCrsr!=0) ){
69330
+ u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
69331
+ u.bs.r.nField = (u16)pOp->p3;
69332
+ u.bs.r.flags = 0;
69333
+ u.bs.r.aMem = &aMem[pOp->p2];
6801669334
#ifdef SQLITE_DEBUG
68017
- { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
69335
+ { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
6801869336
#endif
68019
- rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
68020
- if( rc==SQLITE_OK && u.br.res==0 ){
68021
- rc = sqlite3BtreeDelete(u.br.pCrsr);
69337
+ rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
69338
+ if( rc==SQLITE_OK && u.bs.res==0 ){
69339
+ rc = sqlite3BtreeDelete(u.bs.pCrsr);
6802269340
}
68023
- assert( u.br.pC->deferredMoveto==0 );
68024
- u.br.pC->cacheStatus = CACHE_STALE;
69341
+ assert( u.bs.pC->deferredMoveto==0 );
69342
+ u.bs.pC->cacheStatus = CACHE_STALE;
6802569343
}
6802669344
break;
6802769345
}
6802869346
6802969347
/* Opcode: IdxRowid P1 P2 * * *
@@ -68033,32 +69351,32 @@
6803369351
** the rowid of the table entry to which this index entry points.
6803469352
**
6803569353
** See also: Rowid, MakeRecord.
6803669354
*/
6803769355
case OP_IdxRowid: { /* out2-prerelease */
68038
-#if 0 /* local variables moved into u.bs */
69356
+#if 0 /* local variables moved into u.bt */
6803969357
BtCursor *pCrsr;
6804069358
VdbeCursor *pC;
6804169359
i64 rowid;
68042
-#endif /* local variables moved into u.bs */
69360
+#endif /* local variables moved into u.bt */
6804369361
6804469362
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68045
- u.bs.pC = p->apCsr[pOp->p1];
68046
- assert( u.bs.pC!=0 );
68047
- u.bs.pCrsr = u.bs.pC->pCursor;
69363
+ u.bt.pC = p->apCsr[pOp->p1];
69364
+ assert( u.bt.pC!=0 );
69365
+ u.bt.pCrsr = u.bt.pC->pCursor;
6804869366
pOut->flags = MEM_Null;
68049
- if( ALWAYS(u.bs.pCrsr!=0) ){
68050
- rc = sqlite3VdbeCursorMoveto(u.bs.pC);
69367
+ if( ALWAYS(u.bt.pCrsr!=0) ){
69368
+ rc = sqlite3VdbeCursorMoveto(u.bt.pC);
6805169369
if( NEVER(rc) ) goto abort_due_to_error;
68052
- assert( u.bs.pC->deferredMoveto==0 );
68053
- assert( u.bs.pC->isTable==0 );
68054
- if( !u.bs.pC->nullRow ){
68055
- rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
69370
+ assert( u.bt.pC->deferredMoveto==0 );
69371
+ assert( u.bt.pC->isTable==0 );
69372
+ if( !u.bt.pC->nullRow ){
69373
+ rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
6805669374
if( rc!=SQLITE_OK ){
6805769375
goto abort_due_to_error;
6805869376
}
68059
- pOut->u.i = u.bs.rowid;
69377
+ pOut->u.i = u.bt.rowid;
6806069378
pOut->flags = MEM_Int;
6806169379
}
6806269380
}
6806369381
break;
6806469382
}
@@ -68089,43 +69407,43 @@
6808969407
** If P5 is non-zero then the key value is increased by an epsilon prior
6809069408
** to the comparison. This makes the opcode work like IdxLE.
6809169409
*/
6809269410
case OP_IdxLT: /* jump */
6809369411
case OP_IdxGE: { /* jump */
68094
-#if 0 /* local variables moved into u.bt */
69412
+#if 0 /* local variables moved into u.bu */
6809569413
VdbeCursor *pC;
6809669414
int res;
6809769415
UnpackedRecord r;
68098
-#endif /* local variables moved into u.bt */
69416
+#endif /* local variables moved into u.bu */
6809969417
6810069418
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68101
- u.bt.pC = p->apCsr[pOp->p1];
68102
- assert( u.bt.pC!=0 );
68103
- assert( u.bt.pC->isOrdered );
68104
- if( ALWAYS(u.bt.pC->pCursor!=0) ){
68105
- assert( u.bt.pC->deferredMoveto==0 );
69419
+ u.bu.pC = p->apCsr[pOp->p1];
69420
+ assert( u.bu.pC!=0 );
69421
+ assert( u.bu.pC->isOrdered );
69422
+ if( ALWAYS(u.bu.pC->pCursor!=0) ){
69423
+ assert( u.bu.pC->deferredMoveto==0 );
6810669424
assert( pOp->p5==0 || pOp->p5==1 );
6810769425
assert( pOp->p4type==P4_INT32 );
68108
- u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68109
- u.bt.r.nField = (u16)pOp->p4.i;
69426
+ u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
69427
+ u.bu.r.nField = (u16)pOp->p4.i;
6811069428
if( pOp->p5 ){
68111
- u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
69429
+ u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
6811269430
}else{
68113
- u.bt.r.flags = UNPACKED_IGNORE_ROWID;
69431
+ u.bu.r.flags = UNPACKED_PREFIX_MATCH;
6811469432
}
68115
- u.bt.r.aMem = &aMem[pOp->p3];
69433
+ u.bu.r.aMem = &aMem[pOp->p3];
6811669434
#ifdef SQLITE_DEBUG
68117
- { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
69435
+ { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
6811869436
#endif
68119
- rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
69437
+ rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
6812069438
if( pOp->opcode==OP_IdxLT ){
68121
- u.bt.res = -u.bt.res;
69439
+ u.bu.res = -u.bu.res;
6812269440
}else{
6812369441
assert( pOp->opcode==OP_IdxGE );
68124
- u.bt.res++;
69442
+ u.bu.res++;
6812569443
}
68126
- if( u.bt.res>0 ){
69444
+ if( u.bu.res>0 ){
6812769445
pc = pOp->p2 - 1 ;
6812869446
}
6812969447
}
6813069448
break;
6813169449
}
@@ -68149,43 +69467,43 @@
6814969467
** If AUTOVACUUM is disabled then a zero is stored in register P2.
6815069468
**
6815169469
** See also: Clear
6815269470
*/
6815369471
case OP_Destroy: { /* out2-prerelease */
68154
-#if 0 /* local variables moved into u.bu */
69472
+#if 0 /* local variables moved into u.bv */
6815569473
int iMoved;
6815669474
int iCnt;
6815769475
Vdbe *pVdbe;
6815869476
int iDb;
68159
-#endif /* local variables moved into u.bu */
69477
+#endif /* local variables moved into u.bv */
6816069478
#ifndef SQLITE_OMIT_VIRTUALTABLE
68161
- u.bu.iCnt = 0;
68162
- for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
68163
- if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
68164
- u.bu.iCnt++;
69479
+ u.bv.iCnt = 0;
69480
+ for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
69481
+ if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
69482
+ u.bv.iCnt++;
6816569483
}
6816669484
}
6816769485
#else
68168
- u.bu.iCnt = db->activeVdbeCnt;
69486
+ u.bv.iCnt = db->activeVdbeCnt;
6816969487
#endif
6817069488
pOut->flags = MEM_Null;
68171
- if( u.bu.iCnt>1 ){
69489
+ if( u.bv.iCnt>1 ){
6817269490
rc = SQLITE_LOCKED;
6817369491
p->errorAction = OE_Abort;
6817469492
}else{
68175
- u.bu.iDb = pOp->p3;
68176
- assert( u.bu.iCnt==1 );
68177
- assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
68178
- rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
69493
+ u.bv.iDb = pOp->p3;
69494
+ assert( u.bv.iCnt==1 );
69495
+ assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
69496
+ rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
6817969497
pOut->flags = MEM_Int;
68180
- pOut->u.i = u.bu.iMoved;
69498
+ pOut->u.i = u.bv.iMoved;
6818169499
#ifndef SQLITE_OMIT_AUTOVACUUM
68182
- if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
68183
- sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
69500
+ if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
69501
+ sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
6818469502
/* All OP_Destroy operations occur on the same btree */
68185
- assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
68186
- resetSchemaOnFault = u.bu.iDb+1;
69503
+ assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
69504
+ resetSchemaOnFault = u.bv.iDb+1;
6818769505
}
6818869506
#endif
6818969507
}
6819069508
break;
6819169509
}
@@ -68207,25 +69525,25 @@
6820769525
** also incremented by the number of rows in the table being cleared.
6820869526
**
6820969527
** See also: Destroy
6821069528
*/
6821169529
case OP_Clear: {
68212
-#if 0 /* local variables moved into u.bv */
69530
+#if 0 /* local variables moved into u.bw */
6821369531
int nChange;
68214
-#endif /* local variables moved into u.bv */
69532
+#endif /* local variables moved into u.bw */
6821569533
68216
- u.bv.nChange = 0;
69534
+ u.bw.nChange = 0;
6821769535
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
6821869536
rc = sqlite3BtreeClearTable(
68219
- db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
69537
+ db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
6822069538
);
6822169539
if( pOp->p3 ){
68222
- p->nChange += u.bv.nChange;
69540
+ p->nChange += u.bw.nChange;
6822369541
if( pOp->p3>0 ){
6822469542
assert( memIsValid(&aMem[pOp->p3]) );
6822569543
memAboutToChange(p, &aMem[pOp->p3]);
68226
- aMem[pOp->p3].u.i += u.bv.nChange;
69544
+ aMem[pOp->p3].u.i += u.bw.nChange;
6822769545
}
6822869546
}
6822969547
break;
6823069548
}
6823169549
@@ -68251,29 +69569,29 @@
6825169569
**
6825269570
** See documentation on OP_CreateTable for additional information.
6825369571
*/
6825469572
case OP_CreateIndex: /* out2-prerelease */
6825569573
case OP_CreateTable: { /* out2-prerelease */
68256
-#if 0 /* local variables moved into u.bw */
69574
+#if 0 /* local variables moved into u.bx */
6825769575
int pgno;
6825869576
int flags;
6825969577
Db *pDb;
68260
-#endif /* local variables moved into u.bw */
69578
+#endif /* local variables moved into u.bx */
6826169579
68262
- u.bw.pgno = 0;
69580
+ u.bx.pgno = 0;
6826369581
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6826469582
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68265
- u.bw.pDb = &db->aDb[pOp->p1];
68266
- assert( u.bw.pDb->pBt!=0 );
69583
+ u.bx.pDb = &db->aDb[pOp->p1];
69584
+ assert( u.bx.pDb->pBt!=0 );
6826769585
if( pOp->opcode==OP_CreateTable ){
68268
- /* u.bw.flags = BTREE_INTKEY; */
68269
- u.bw.flags = BTREE_INTKEY;
69586
+ /* u.bx.flags = BTREE_INTKEY; */
69587
+ u.bx.flags = BTREE_INTKEY;
6827069588
}else{
68271
- u.bw.flags = BTREE_BLOBKEY;
69589
+ u.bx.flags = BTREE_BLOBKEY;
6827269590
}
68273
- rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
68274
- pOut->u.i = u.bw.pgno;
69591
+ rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
69592
+ pOut->u.i = u.bx.pgno;
6827569593
break;
6827669594
}
6827769595
6827869596
/* Opcode: ParseSchema P1 * * P4 *
6827969597
**
@@ -68282,48 +69600,48 @@
6828269600
**
6828369601
** This opcode invokes the parser to create a new virtual machine,
6828469602
** then runs the new virtual machine. It is thus a re-entrant opcode.
6828569603
*/
6828669604
case OP_ParseSchema: {
68287
-#if 0 /* local variables moved into u.bx */
69605
+#if 0 /* local variables moved into u.by */
6828869606
int iDb;
6828969607
const char *zMaster;
6829069608
char *zSql;
6829169609
InitData initData;
68292
-#endif /* local variables moved into u.bx */
69610
+#endif /* local variables moved into u.by */
6829369611
6829469612
/* Any prepared statement that invokes this opcode will hold mutexes
6829569613
** on every btree. This is a prerequisite for invoking
6829669614
** sqlite3InitCallback().
6829769615
*/
6829869616
#ifdef SQLITE_DEBUG
68299
- for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
68300
- assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
69617
+ for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
69618
+ assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
6830169619
}
6830269620
#endif
6830369621
68304
- u.bx.iDb = pOp->p1;
68305
- assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
68306
- assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
69622
+ u.by.iDb = pOp->p1;
69623
+ assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
69624
+ assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
6830769625
/* Used to be a conditional */ {
68308
- u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
68309
- u.bx.initData.db = db;
68310
- u.bx.initData.iDb = pOp->p1;
68311
- u.bx.initData.pzErrMsg = &p->zErrMsg;
68312
- u.bx.zSql = sqlite3MPrintf(db,
69626
+ u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
69627
+ u.by.initData.db = db;
69628
+ u.by.initData.iDb = pOp->p1;
69629
+ u.by.initData.pzErrMsg = &p->zErrMsg;
69630
+ u.by.zSql = sqlite3MPrintf(db,
6831369631
"SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68314
- db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
68315
- if( u.bx.zSql==0 ){
69632
+ db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
69633
+ if( u.by.zSql==0 ){
6831669634
rc = SQLITE_NOMEM;
6831769635
}else{
6831869636
assert( db->init.busy==0 );
6831969637
db->init.busy = 1;
68320
- u.bx.initData.rc = SQLITE_OK;
69638
+ u.by.initData.rc = SQLITE_OK;
6832169639
assert( !db->mallocFailed );
68322
- rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
68323
- if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
68324
- sqlite3DbFree(db, u.bx.zSql);
69640
+ rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
69641
+ if( rc==SQLITE_OK ) rc = u.by.initData.rc;
69642
+ sqlite3DbFree(db, u.by.zSql);
6832569643
db->init.busy = 0;
6832669644
}
6832769645
}
6832869646
if( rc==SQLITE_NOMEM ){
6832969647
goto no_mem;
@@ -68402,45 +69720,45 @@
6840269720
** file, not the main database file.
6840369721
**
6840469722
** This opcode is used to implement the integrity_check pragma.
6840569723
*/
6840669724
case OP_IntegrityCk: {
68407
-#if 0 /* local variables moved into u.by */
69725
+#if 0 /* local variables moved into u.bz */
6840869726
int nRoot; /* Number of tables to check. (Number of root pages.) */
6840969727
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6841069728
int j; /* Loop counter */
6841169729
int nErr; /* Number of errors reported */
6841269730
char *z; /* Text of the error report */
6841369731
Mem *pnErr; /* Register keeping track of errors remaining */
68414
-#endif /* local variables moved into u.by */
69732
+#endif /* local variables moved into u.bz */
6841569733
68416
- u.by.nRoot = pOp->p2;
68417
- assert( u.by.nRoot>0 );
68418
- u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
68419
- if( u.by.aRoot==0 ) goto no_mem;
69734
+ u.bz.nRoot = pOp->p2;
69735
+ assert( u.bz.nRoot>0 );
69736
+ u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
69737
+ if( u.bz.aRoot==0 ) goto no_mem;
6842069738
assert( pOp->p3>0 && pOp->p3<=p->nMem );
68421
- u.by.pnErr = &aMem[pOp->p3];
68422
- assert( (u.by.pnErr->flags & MEM_Int)!=0 );
68423
- assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
69739
+ u.bz.pnErr = &aMem[pOp->p3];
69740
+ assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
69741
+ assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
6842469742
pIn1 = &aMem[pOp->p1];
68425
- for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
68426
- u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
69743
+ for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
69744
+ u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
6842769745
}
68428
- u.by.aRoot[u.by.j] = 0;
69746
+ u.bz.aRoot[u.bz.j] = 0;
6842969747
assert( pOp->p5<db->nDb );
6843069748
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68431
- u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
68432
- (int)u.by.pnErr->u.i, &u.by.nErr);
68433
- sqlite3DbFree(db, u.by.aRoot);
68434
- u.by.pnErr->u.i -= u.by.nErr;
69749
+ u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
69750
+ (int)u.bz.pnErr->u.i, &u.bz.nErr);
69751
+ sqlite3DbFree(db, u.bz.aRoot);
69752
+ u.bz.pnErr->u.i -= u.bz.nErr;
6843569753
sqlite3VdbeMemSetNull(pIn1);
68436
- if( u.by.nErr==0 ){
68437
- assert( u.by.z==0 );
68438
- }else if( u.by.z==0 ){
69754
+ if( u.bz.nErr==0 ){
69755
+ assert( u.bz.z==0 );
69756
+ }else if( u.bz.z==0 ){
6843969757
goto no_mem;
6844069758
}else{
68441
- sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
69759
+ sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
6844269760
}
6844369761
UPDATE_MAX_BLOBSIZE(pIn1);
6844469762
sqlite3VdbeChangeEncoding(pIn1, encoding);
6844569763
break;
6844669764
}
@@ -68470,24 +69788,24 @@
6847069788
** Extract the smallest value from boolean index P1 and put that value into
6847169789
** register P3. Or, if boolean index P1 is initially empty, leave P3
6847269790
** unchanged and jump to instruction P2.
6847369791
*/
6847469792
case OP_RowSetRead: { /* jump, in1, out3 */
68475
-#if 0 /* local variables moved into u.bz */
69793
+#if 0 /* local variables moved into u.ca */
6847669794
i64 val;
68477
-#endif /* local variables moved into u.bz */
69795
+#endif /* local variables moved into u.ca */
6847869796
CHECK_FOR_INTERRUPT;
6847969797
pIn1 = &aMem[pOp->p1];
6848069798
if( (pIn1->flags & MEM_RowSet)==0
68481
- || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
69799
+ || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
6848269800
){
6848369801
/* The boolean index is empty */
6848469802
sqlite3VdbeMemSetNull(pIn1);
6848569803
pc = pOp->p2 - 1;
6848669804
}else{
6848769805
/* A value was pulled from the index */
68488
- sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
69806
+ sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
6848969807
}
6849069808
break;
6849169809
}
6849269810
6849369811
/* Opcode: RowSetTest P1 P2 P3 P4
@@ -68512,18 +69830,18 @@
6851269830
** inserted, there is no need to search to see if the same value was
6851369831
** previously inserted as part of set X (only if it was previously
6851469832
** inserted as part of some other set).
6851569833
*/
6851669834
case OP_RowSetTest: { /* jump, in1, in3 */
68517
-#if 0 /* local variables moved into u.ca */
69835
+#if 0 /* local variables moved into u.cb */
6851869836
int iSet;
6851969837
int exists;
68520
-#endif /* local variables moved into u.ca */
69838
+#endif /* local variables moved into u.cb */
6852169839
6852269840
pIn1 = &aMem[pOp->p1];
6852369841
pIn3 = &aMem[pOp->p3];
68524
- u.ca.iSet = pOp->p4.i;
69842
+ u.cb.iSet = pOp->p4.i;
6852569843
assert( pIn3->flags&MEM_Int );
6852669844
6852769845
/* If there is anything other than a rowset object in memory cell P1,
6852869846
** delete it now and initialize P1 with an empty rowset
6852969847
*/
@@ -68531,21 +69849,21 @@
6853169849
sqlite3VdbeMemSetRowSet(pIn1);
6853269850
if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
6853369851
}
6853469852
6853569853
assert( pOp->p4type==P4_INT32 );
68536
- assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
68537
- if( u.ca.iSet ){
68538
- u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68539
- (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
69854
+ assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
69855
+ if( u.cb.iSet ){
69856
+ u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
69857
+ (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
6854069858
pIn3->u.i);
68541
- if( u.ca.exists ){
69859
+ if( u.cb.exists ){
6854269860
pc = pOp->p2 - 1;
6854369861
break;
6854469862
}
6854569863
}
68546
- if( u.ca.iSet>=0 ){
69864
+ if( u.cb.iSet>=0 ){
6854769865
sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
6854869866
}
6854969867
break;
6855069868
}
6855169869
@@ -68564,25 +69882,24 @@
6856469882
** memory required by the sub-vdbe at runtime.
6856569883
**
6856669884
** P4 is a pointer to the VM containing the trigger program.
6856769885
*/
6856869886
case OP_Program: { /* jump */
68569
-#if 0 /* local variables moved into u.cb */
69887
+#if 0 /* local variables moved into u.cc */
6857069888
int nMem; /* Number of memory registers for sub-program */
6857169889
int nByte; /* Bytes of runtime space required for sub-program */
6857269890
Mem *pRt; /* Register to allocate runtime space */
6857369891
Mem *pMem; /* Used to iterate through memory cells */
6857469892
Mem *pEnd; /* Last memory cell in new array */
6857569893
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6857669894
SubProgram *pProgram; /* Sub-program to execute */
6857769895
void *t; /* Token identifying trigger */
68578
-#endif /* local variables moved into u.cb */
69896
+#endif /* local variables moved into u.cc */
6857969897
68580
- u.cb.pProgram = pOp->p4.pProgram;
68581
- u.cb.pRt = &aMem[pOp->p3];
68582
- assert( memIsValid(u.cb.pRt) );
68583
- assert( u.cb.pProgram->nOp>0 );
69898
+ u.cc.pProgram = pOp->p4.pProgram;
69899
+ u.cc.pRt = &aMem[pOp->p3];
69900
+ assert( u.cc.pProgram->nOp>0 );
6858469901
6858569902
/* If the p5 flag is clear, then recursive invocation of triggers is
6858669903
** disabled for backwards compatibility (p5 is set if this sub-program
6858769904
** is really a trigger, not a foreign key action, and the flag set
6858869905
** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68592,80 +69909,87 @@
6859269909
** SubProgram (if the trigger may be executed with more than one different
6859369910
** ON CONFLICT algorithm). SubProgram structures associated with a
6859469911
** single trigger all have the same value for the SubProgram.token
6859569912
** variable. */
6859669913
if( pOp->p5 ){
68597
- u.cb.t = u.cb.pProgram->token;
68598
- for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
68599
- if( u.cb.pFrame ) break;
69914
+ u.cc.t = u.cc.pProgram->token;
69915
+ for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
69916
+ if( u.cc.pFrame ) break;
6860069917
}
6860169918
6860269919
if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
6860369920
rc = SQLITE_ERROR;
6860469921
sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
6860569922
break;
6860669923
}
6860769924
68608
- /* Register u.cb.pRt is used to store the memory required to save the state
69925
+ /* Register u.cc.pRt is used to store the memory required to save the state
6860969926
** of the current program, and the memory required at runtime to execute
68610
- ** the trigger program. If this trigger has been fired before, then u.cb.pRt
69927
+ ** the trigger program. If this trigger has been fired before, then u.cc.pRt
6861169928
** is already allocated. Otherwise, it must be initialized. */
68612
- if( (u.cb.pRt->flags&MEM_Frame)==0 ){
69929
+ if( (u.cc.pRt->flags&MEM_Frame)==0 ){
6861369930
/* SubProgram.nMem is set to the number of memory cells used by the
6861469931
** program stored in SubProgram.aOp. As well as these, one memory
6861569932
** cell is required for each cursor used by the program. Set local
68616
- ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
69933
+ ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
6861769934
*/
68618
- u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
68619
- u.cb.nByte = ROUND8(sizeof(VdbeFrame))
68620
- + u.cb.nMem * sizeof(Mem)
68621
- + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
68622
- u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
68623
- if( !u.cb.pFrame ){
69935
+ u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
69936
+ u.cc.nByte = ROUND8(sizeof(VdbeFrame))
69937
+ + u.cc.nMem * sizeof(Mem)
69938
+ + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
69939
+ + u.cc.pProgram->nOnce * sizeof(u8);
69940
+ u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
69941
+ if( !u.cc.pFrame ){
6862469942
goto no_mem;
6862569943
}
68626
- sqlite3VdbeMemRelease(u.cb.pRt);
68627
- u.cb.pRt->flags = MEM_Frame;
68628
- u.cb.pRt->u.pFrame = u.cb.pFrame;
68629
-
68630
- u.cb.pFrame->v = p;
68631
- u.cb.pFrame->nChildMem = u.cb.nMem;
68632
- u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
68633
- u.cb.pFrame->pc = pc;
68634
- u.cb.pFrame->aMem = p->aMem;
68635
- u.cb.pFrame->nMem = p->nMem;
68636
- u.cb.pFrame->apCsr = p->apCsr;
68637
- u.cb.pFrame->nCursor = p->nCursor;
68638
- u.cb.pFrame->aOp = p->aOp;
68639
- u.cb.pFrame->nOp = p->nOp;
68640
- u.cb.pFrame->token = u.cb.pProgram->token;
68641
-
68642
- u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
68643
- for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
68644
- u.cb.pMem->flags = MEM_Null;
68645
- u.cb.pMem->db = db;
69944
+ sqlite3VdbeMemRelease(u.cc.pRt);
69945
+ u.cc.pRt->flags = MEM_Frame;
69946
+ u.cc.pRt->u.pFrame = u.cc.pFrame;
69947
+
69948
+ u.cc.pFrame->v = p;
69949
+ u.cc.pFrame->nChildMem = u.cc.nMem;
69950
+ u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
69951
+ u.cc.pFrame->pc = pc;
69952
+ u.cc.pFrame->aMem = p->aMem;
69953
+ u.cc.pFrame->nMem = p->nMem;
69954
+ u.cc.pFrame->apCsr = p->apCsr;
69955
+ u.cc.pFrame->nCursor = p->nCursor;
69956
+ u.cc.pFrame->aOp = p->aOp;
69957
+ u.cc.pFrame->nOp = p->nOp;
69958
+ u.cc.pFrame->token = u.cc.pProgram->token;
69959
+ u.cc.pFrame->aOnceFlag = p->aOnceFlag;
69960
+ u.cc.pFrame->nOnceFlag = p->nOnceFlag;
69961
+
69962
+ u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
69963
+ for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
69964
+ u.cc.pMem->flags = MEM_Invalid;
69965
+ u.cc.pMem->db = db;
6864669966
}
6864769967
}else{
68648
- u.cb.pFrame = u.cb.pRt->u.pFrame;
68649
- assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
68650
- assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
68651
- assert( pc==u.cb.pFrame->pc );
69968
+ u.cc.pFrame = u.cc.pRt->u.pFrame;
69969
+ assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
69970
+ assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
69971
+ assert( pc==u.cc.pFrame->pc );
6865269972
}
6865369973
6865469974
p->nFrame++;
68655
- u.cb.pFrame->pParent = p->pFrame;
68656
- u.cb.pFrame->lastRowid = lastRowid;
68657
- u.cb.pFrame->nChange = p->nChange;
69975
+ u.cc.pFrame->pParent = p->pFrame;
69976
+ u.cc.pFrame->lastRowid = lastRowid;
69977
+ u.cc.pFrame->nChange = p->nChange;
6865869978
p->nChange = 0;
68659
- p->pFrame = u.cb.pFrame;
68660
- p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
68661
- p->nMem = u.cb.pFrame->nChildMem;
68662
- p->nCursor = (u16)u.cb.pFrame->nChildCsr;
69979
+ p->pFrame = u.cc.pFrame;
69980
+ p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
69981
+ p->nMem = u.cc.pFrame->nChildMem;
69982
+ p->nCursor = (u16)u.cc.pFrame->nChildCsr;
6866369983
p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68664
- p->aOp = aOp = u.cb.pProgram->aOp;
68665
- p->nOp = u.cb.pProgram->nOp;
69984
+ p->aOp = aOp = u.cc.pProgram->aOp;
69985
+ p->nOp = u.cc.pProgram->nOp;
69986
+ p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
69987
+ p->nOnceFlag = u.cc.pProgram->nOnce;
69988
+ p->nOp = u.cc.pProgram->nOp;
6866669989
pc = -1;
69990
+ memset(p->aOnceFlag, 0, p->nOnceFlag);
6866769991
6866869992
break;
6866969993
}
6867069994
6867169995
/* Opcode: Param P1 P2 * * *
@@ -68679,17 +70003,17 @@
6867970003
** The address of the cell in the parent frame is determined by adding
6868070004
** the value of the P1 argument to the value of the P1 argument to the
6868170005
** calling OP_Program instruction.
6868270006
*/
6868370007
case OP_Param: { /* out2-prerelease */
68684
-#if 0 /* local variables moved into u.cc */
70008
+#if 0 /* local variables moved into u.cd */
6868570009
VdbeFrame *pFrame;
6868670010
Mem *pIn;
68687
-#endif /* local variables moved into u.cc */
68688
- u.cc.pFrame = p->pFrame;
68689
- u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
68690
- sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
70011
+#endif /* local variables moved into u.cd */
70012
+ u.cd.pFrame = p->pFrame;
70013
+ u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
70014
+ sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
6869170015
break;
6869270016
}
6869370017
6869470018
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
6869570019
@@ -68741,26 +70065,26 @@
6874170065
**
6874270066
** This instruction throws an error if the memory cell is not initially
6874370067
** an integer.
6874470068
*/
6874570069
case OP_MemMax: { /* in2 */
68746
-#if 0 /* local variables moved into u.cd */
70070
+#if 0 /* local variables moved into u.ce */
6874770071
Mem *pIn1;
6874870072
VdbeFrame *pFrame;
68749
-#endif /* local variables moved into u.cd */
70073
+#endif /* local variables moved into u.ce */
6875070074
if( p->pFrame ){
68751
- for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
68752
- u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
70075
+ for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
70076
+ u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
6875370077
}else{
68754
- u.cd.pIn1 = &aMem[pOp->p1];
70078
+ u.ce.pIn1 = &aMem[pOp->p1];
6875570079
}
68756
- assert( memIsValid(u.cd.pIn1) );
68757
- sqlite3VdbeMemIntegerify(u.cd.pIn1);
70080
+ assert( memIsValid(u.ce.pIn1) );
70081
+ sqlite3VdbeMemIntegerify(u.ce.pIn1);
6875870082
pIn2 = &aMem[pOp->p2];
6875970083
sqlite3VdbeMemIntegerify(pIn2);
68760
- if( u.cd.pIn1->u.i<pIn2->u.i){
68761
- u.cd.pIn1->u.i = pIn2->u.i;
70084
+ if( u.ce.pIn1->u.i<pIn2->u.i){
70085
+ u.ce.pIn1->u.i = pIn2->u.i;
6876270086
}
6876370087
break;
6876470088
}
6876570089
#endif /* SQLITE_OMIT_AUTOINCREMENT */
6876670090
@@ -68823,54 +70147,54 @@
6882370147
**
6882470148
** The P5 arguments are taken from register P2 and its
6882570149
** successors.
6882670150
*/
6882770151
case OP_AggStep: {
68828
-#if 0 /* local variables moved into u.ce */
70152
+#if 0 /* local variables moved into u.cf */
6882970153
int n;
6883070154
int i;
6883170155
Mem *pMem;
6883270156
Mem *pRec;
6883370157
sqlite3_context ctx;
6883470158
sqlite3_value **apVal;
68835
-#endif /* local variables moved into u.ce */
68836
-
68837
- u.ce.n = pOp->p5;
68838
- assert( u.ce.n>=0 );
68839
- u.ce.pRec = &aMem[pOp->p2];
68840
- u.ce.apVal = p->apArg;
68841
- assert( u.ce.apVal || u.ce.n==0 );
68842
- for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
68843
- assert( memIsValid(u.ce.pRec) );
68844
- u.ce.apVal[u.ce.i] = u.ce.pRec;
68845
- memAboutToChange(p, u.ce.pRec);
68846
- sqlite3VdbeMemStoreType(u.ce.pRec);
68847
- }
68848
- u.ce.ctx.pFunc = pOp->p4.pFunc;
70159
+#endif /* local variables moved into u.cf */
70160
+
70161
+ u.cf.n = pOp->p5;
70162
+ assert( u.cf.n>=0 );
70163
+ u.cf.pRec = &aMem[pOp->p2];
70164
+ u.cf.apVal = p->apArg;
70165
+ assert( u.cf.apVal || u.cf.n==0 );
70166
+ for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
70167
+ assert( memIsValid(u.cf.pRec) );
70168
+ u.cf.apVal[u.cf.i] = u.cf.pRec;
70169
+ memAboutToChange(p, u.cf.pRec);
70170
+ sqlite3VdbeMemStoreType(u.cf.pRec);
70171
+ }
70172
+ u.cf.ctx.pFunc = pOp->p4.pFunc;
6884970173
assert( pOp->p3>0 && pOp->p3<=p->nMem );
68850
- u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
68851
- u.ce.pMem->n++;
68852
- u.ce.ctx.s.flags = MEM_Null;
68853
- u.ce.ctx.s.z = 0;
68854
- u.ce.ctx.s.zMalloc = 0;
68855
- u.ce.ctx.s.xDel = 0;
68856
- u.ce.ctx.s.db = db;
68857
- u.ce.ctx.isError = 0;
68858
- u.ce.ctx.pColl = 0;
68859
- if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
70174
+ u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
70175
+ u.cf.pMem->n++;
70176
+ u.cf.ctx.s.flags = MEM_Null;
70177
+ u.cf.ctx.s.z = 0;
70178
+ u.cf.ctx.s.zMalloc = 0;
70179
+ u.cf.ctx.s.xDel = 0;
70180
+ u.cf.ctx.s.db = db;
70181
+ u.cf.ctx.isError = 0;
70182
+ u.cf.ctx.pColl = 0;
70183
+ if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6886070184
assert( pOp>p->aOp );
6886170185
assert( pOp[-1].p4type==P4_COLLSEQ );
6886270186
assert( pOp[-1].opcode==OP_CollSeq );
68863
- u.ce.ctx.pColl = pOp[-1].p4.pColl;
70187
+ u.cf.ctx.pColl = pOp[-1].p4.pColl;
6886470188
}
68865
- (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
68866
- if( u.ce.ctx.isError ){
68867
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
68868
- rc = u.ce.ctx.isError;
70189
+ (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
70190
+ if( u.cf.ctx.isError ){
70191
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
70192
+ rc = u.cf.ctx.isError;
6886970193
}
6887070194
68871
- sqlite3VdbeMemRelease(&u.ce.ctx.s);
70195
+ sqlite3VdbeMemRelease(&u.cf.ctx.s);
6887270196
6887370197
break;
6887470198
}
6887570199
6887670200
/* Opcode: AggFinal P1 P2 * P4 *
@@ -68884,23 +70208,23 @@
6888470208
** functions that can take varying numbers of arguments. The
6888570209
** P4 argument is only needed for the degenerate case where
6888670210
** the step function was not previously called.
6888770211
*/
6888870212
case OP_AggFinal: {
68889
-#if 0 /* local variables moved into u.cf */
70213
+#if 0 /* local variables moved into u.cg */
6889070214
Mem *pMem;
68891
-#endif /* local variables moved into u.cf */
70215
+#endif /* local variables moved into u.cg */
6889270216
assert( pOp->p1>0 && pOp->p1<=p->nMem );
68893
- u.cf.pMem = &aMem[pOp->p1];
68894
- assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68895
- rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
70217
+ u.cg.pMem = &aMem[pOp->p1];
70218
+ assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
70219
+ rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
6889670220
if( rc ){
68897
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
70221
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
6889870222
}
68899
- sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
68900
- UPDATE_MAX_BLOBSIZE(u.cf.pMem);
68901
- if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
70223
+ sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
70224
+ UPDATE_MAX_BLOBSIZE(u.cg.pMem);
70225
+ if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
6890270226
goto too_big;
6890370227
}
6890470228
break;
6890570229
}
6890670230
@@ -68915,29 +70239,29 @@
6891570239
** in the WAL that have been checkpointed after the checkpoint
6891670240
** completes into mem[P3+2]. However on an error, mem[P3+1] and
6891770241
** mem[P3+2] are initialized to -1.
6891870242
*/
6891970243
case OP_Checkpoint: {
68920
-#if 0 /* local variables moved into u.cg */
70244
+#if 0 /* local variables moved into u.ch */
6892170245
int i; /* Loop counter */
6892270246
int aRes[3]; /* Results */
6892370247
Mem *pMem; /* Write results here */
68924
-#endif /* local variables moved into u.cg */
70248
+#endif /* local variables moved into u.ch */
6892570249
68926
- u.cg.aRes[0] = 0;
68927
- u.cg.aRes[1] = u.cg.aRes[2] = -1;
70250
+ u.ch.aRes[0] = 0;
70251
+ u.ch.aRes[1] = u.ch.aRes[2] = -1;
6892870252
assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6892970253
|| pOp->p2==SQLITE_CHECKPOINT_FULL
6893070254
|| pOp->p2==SQLITE_CHECKPOINT_RESTART
6893170255
);
68932
- rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
70256
+ rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
6893370257
if( rc==SQLITE_BUSY ){
6893470258
rc = SQLITE_OK;
68935
- u.cg.aRes[0] = 1;
70259
+ u.ch.aRes[0] = 1;
6893670260
}
68937
- for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
68938
- sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
70261
+ for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
70262
+ sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
6893970263
}
6894070264
break;
6894170265
};
6894270266
#endif
6894370267
@@ -68952,95 +70276,95 @@
6895270276
** If changing into or out of WAL mode the procedure is more complicated.
6895370277
**
6895470278
** Write a string containing the final journal-mode to register P2.
6895570279
*/
6895670280
case OP_JournalMode: { /* out2-prerelease */
68957
-#if 0 /* local variables moved into u.ch */
70281
+#if 0 /* local variables moved into u.ci */
6895870282
Btree *pBt; /* Btree to change journal mode of */
6895970283
Pager *pPager; /* Pager associated with pBt */
6896070284
int eNew; /* New journal mode */
6896170285
int eOld; /* The old journal mode */
6896270286
const char *zFilename; /* Name of database file for pPager */
68963
-#endif /* local variables moved into u.ch */
68964
-
68965
- u.ch.eNew = pOp->p3;
68966
- assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
68967
- || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
68968
- || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
68969
- || u.ch.eNew==PAGER_JOURNALMODE_OFF
68970
- || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
68971
- || u.ch.eNew==PAGER_JOURNALMODE_WAL
68972
- || u.ch.eNew==PAGER_JOURNALMODE_QUERY
70287
+#endif /* local variables moved into u.ci */
70288
+
70289
+ u.ci.eNew = pOp->p3;
70290
+ assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
70291
+ || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
70292
+ || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
70293
+ || u.ci.eNew==PAGER_JOURNALMODE_OFF
70294
+ || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
70295
+ || u.ci.eNew==PAGER_JOURNALMODE_WAL
70296
+ || u.ci.eNew==PAGER_JOURNALMODE_QUERY
6897370297
);
6897470298
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6897570299
68976
- u.ch.pBt = db->aDb[pOp->p1].pBt;
68977
- u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
68978
- u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
68979
- if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
68980
- if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
70300
+ u.ci.pBt = db->aDb[pOp->p1].pBt;
70301
+ u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
70302
+ u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
70303
+ if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
70304
+ if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
6898170305
6898270306
#ifndef SQLITE_OMIT_WAL
68983
- u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
70307
+ u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
6898470308
6898570309
/* Do not allow a transition to journal_mode=WAL for a database
6898670310
** in temporary storage or if the VFS does not support shared memory
6898770311
*/
68988
- if( u.ch.eNew==PAGER_JOURNALMODE_WAL
68989
- && (sqlite3Strlen30(u.ch.zFilename)==0 /* Temp file */
68990
- || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */
70312
+ if( u.ci.eNew==PAGER_JOURNALMODE_WAL
70313
+ && (sqlite3Strlen30(u.ci.zFilename)==0 /* Temp file */
70314
+ || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
6899170315
){
68992
- u.ch.eNew = u.ch.eOld;
70316
+ u.ci.eNew = u.ci.eOld;
6899370317
}
6899470318
68995
- if( (u.ch.eNew!=u.ch.eOld)
68996
- && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
70319
+ if( (u.ci.eNew!=u.ci.eOld)
70320
+ && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
6899770321
){
6899870322
if( !db->autoCommit || db->activeVdbeCnt>1 ){
6899970323
rc = SQLITE_ERROR;
6900070324
sqlite3SetString(&p->zErrMsg, db,
6900170325
"cannot change %s wal mode from within a transaction",
69002
- (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
70326
+ (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6900370327
);
6900470328
break;
6900570329
}else{
6900670330
69007
- if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
70331
+ if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
6900870332
/* If leaving WAL mode, close the log file. If successful, the call
6900970333
** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6901070334
** file. An EXCLUSIVE lock may still be held on the database file
6901170335
** after a successful return.
6901270336
*/
69013
- rc = sqlite3PagerCloseWal(u.ch.pPager);
70337
+ rc = sqlite3PagerCloseWal(u.ci.pPager);
6901470338
if( rc==SQLITE_OK ){
69015
- sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
70339
+ sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
6901670340
}
69017
- }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
70341
+ }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
6901870342
/* Cannot transition directly from MEMORY to WAL. Use mode OFF
6901970343
** as an intermediate */
69020
- sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
70344
+ sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
6902170345
}
6902270346
6902370347
/* Open a transaction on the database file. Regardless of the journal
6902470348
** mode, this transaction always uses a rollback journal.
6902570349
*/
69026
- assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
70350
+ assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
6902770351
if( rc==SQLITE_OK ){
69028
- rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
70352
+ rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6902970353
}
6903070354
}
6903170355
}
6903270356
#endif /* ifndef SQLITE_OMIT_WAL */
6903370357
6903470358
if( rc ){
69035
- u.ch.eNew = u.ch.eOld;
70359
+ u.ci.eNew = u.ci.eOld;
6903670360
}
69037
- u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
70361
+ u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
6903870362
6903970363
pOut = &aMem[pOp->p2];
6904070364
pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69041
- pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
70365
+ pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
6904270366
pOut->n = sqlite3Strlen30(pOut->z);
6904370367
pOut->enc = SQLITE_UTF8;
6904470368
sqlite3VdbeChangeEncoding(pOut, encoding);
6904570369
break;
6904670370
};
@@ -69065,18 +70389,18 @@
6906570389
** Perform a single step of the incremental vacuum procedure on
6906670390
** the P1 database. If the vacuum has finished, jump to instruction
6906770391
** P2. Otherwise, fall through to the next instruction.
6906870392
*/
6906970393
case OP_IncrVacuum: { /* jump */
69070
-#if 0 /* local variables moved into u.ci */
70394
+#if 0 /* local variables moved into u.cj */
6907170395
Btree *pBt;
69072
-#endif /* local variables moved into u.ci */
70396
+#endif /* local variables moved into u.cj */
6907370397
6907470398
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6907570399
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69076
- u.ci.pBt = db->aDb[pOp->p1].pBt;
69077
- rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
70400
+ u.cj.pBt = db->aDb[pOp->p1].pBt;
70401
+ rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
6907870402
if( rc==SQLITE_DONE ){
6907970403
pc = pOp->p2 - 1;
6908070404
rc = SQLITE_OK;
6908170405
}
6908270406
break;
@@ -69142,16 +70466,16 @@
6914270466
** Also, whether or not P4 is set, check that this is not being called from
6914370467
** within a callback to a virtual table xSync() method. If it is, the error
6914470468
** code will be set to SQLITE_LOCKED.
6914570469
*/
6914670470
case OP_VBegin: {
69147
-#if 0 /* local variables moved into u.cj */
70471
+#if 0 /* local variables moved into u.ck */
6914870472
VTable *pVTab;
69149
-#endif /* local variables moved into u.cj */
69150
- u.cj.pVTab = pOp->p4.pVtab;
69151
- rc = sqlite3VtabBegin(db, u.cj.pVTab);
69152
- if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
70473
+#endif /* local variables moved into u.ck */
70474
+ u.ck.pVTab = pOp->p4.pVtab;
70475
+ rc = sqlite3VtabBegin(db, u.ck.pVTab);
70476
+ if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
6915370477
break;
6915470478
}
6915570479
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6915670480
6915770481
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69186,36 +70510,36 @@
6918670510
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6918770511
** P1 is a cursor number. This opcode opens a cursor to the virtual
6918870512
** table and stores that cursor in P1.
6918970513
*/
6919070514
case OP_VOpen: {
69191
-#if 0 /* local variables moved into u.ck */
70515
+#if 0 /* local variables moved into u.cl */
6919270516
VdbeCursor *pCur;
6919370517
sqlite3_vtab_cursor *pVtabCursor;
6919470518
sqlite3_vtab *pVtab;
6919570519
sqlite3_module *pModule;
69196
-#endif /* local variables moved into u.ck */
69197
-
69198
- u.ck.pCur = 0;
69199
- u.ck.pVtabCursor = 0;
69200
- u.ck.pVtab = pOp->p4.pVtab->pVtab;
69201
- u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
69202
- assert(u.ck.pVtab && u.ck.pModule);
69203
- rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
69204
- importVtabErrMsg(p, u.ck.pVtab);
70520
+#endif /* local variables moved into u.cl */
70521
+
70522
+ u.cl.pCur = 0;
70523
+ u.cl.pVtabCursor = 0;
70524
+ u.cl.pVtab = pOp->p4.pVtab->pVtab;
70525
+ u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
70526
+ assert(u.cl.pVtab && u.cl.pModule);
70527
+ rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
70528
+ importVtabErrMsg(p, u.cl.pVtab);
6920570529
if( SQLITE_OK==rc ){
6920670530
/* Initialize sqlite3_vtab_cursor base class */
69207
- u.ck.pVtabCursor->pVtab = u.ck.pVtab;
70531
+ u.cl.pVtabCursor->pVtab = u.cl.pVtab;
6920870532
6920970533
/* Initialise vdbe cursor object */
69210
- u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69211
- if( u.ck.pCur ){
69212
- u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
69213
- u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
70534
+ u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
70535
+ if( u.cl.pCur ){
70536
+ u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
70537
+ u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
6921470538
}else{
6921570539
db->mallocFailed = 1;
69216
- u.ck.pModule->xClose(u.ck.pVtabCursor);
70540
+ u.cl.pModule->xClose(u.cl.pVtabCursor);
6921770541
}
6921870542
}
6921970543
break;
6922070544
}
6922170545
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69238,11 +70562,11 @@
6923870562
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
6923970563
**
6924070564
** A jump is made to P2 if the result set after filtering would be empty.
6924170565
*/
6924270566
case OP_VFilter: { /* jump */
69243
-#if 0 /* local variables moved into u.cl */
70567
+#if 0 /* local variables moved into u.cm */
6924470568
int nArg;
6924570569
int iQuery;
6924670570
const sqlite3_module *pModule;
6924770571
Mem *pQuery;
6924870572
Mem *pArgc;
@@ -69250,49 +70574,49 @@
6925070574
sqlite3_vtab *pVtab;
6925170575
VdbeCursor *pCur;
6925270576
int res;
6925370577
int i;
6925470578
Mem **apArg;
69255
-#endif /* local variables moved into u.cl */
69256
-
69257
- u.cl.pQuery = &aMem[pOp->p3];
69258
- u.cl.pArgc = &u.cl.pQuery[1];
69259
- u.cl.pCur = p->apCsr[pOp->p1];
69260
- assert( memIsValid(u.cl.pQuery) );
69261
- REGISTER_TRACE(pOp->p3, u.cl.pQuery);
69262
- assert( u.cl.pCur->pVtabCursor );
69263
- u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
69264
- u.cl.pVtab = u.cl.pVtabCursor->pVtab;
69265
- u.cl.pModule = u.cl.pVtab->pModule;
70579
+#endif /* local variables moved into u.cm */
70580
+
70581
+ u.cm.pQuery = &aMem[pOp->p3];
70582
+ u.cm.pArgc = &u.cm.pQuery[1];
70583
+ u.cm.pCur = p->apCsr[pOp->p1];
70584
+ assert( memIsValid(u.cm.pQuery) );
70585
+ REGISTER_TRACE(pOp->p3, u.cm.pQuery);
70586
+ assert( u.cm.pCur->pVtabCursor );
70587
+ u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
70588
+ u.cm.pVtab = u.cm.pVtabCursor->pVtab;
70589
+ u.cm.pModule = u.cm.pVtab->pModule;
6926670590
6926770591
/* Grab the index number and argc parameters */
69268
- assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
69269
- u.cl.nArg = (int)u.cl.pArgc->u.i;
69270
- u.cl.iQuery = (int)u.cl.pQuery->u.i;
70592
+ assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
70593
+ u.cm.nArg = (int)u.cm.pArgc->u.i;
70594
+ u.cm.iQuery = (int)u.cm.pQuery->u.i;
6927170595
6927270596
/* Invoke the xFilter method */
6927370597
{
69274
- u.cl.res = 0;
69275
- u.cl.apArg = p->apArg;
69276
- for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
69277
- u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
69278
- sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
70598
+ u.cm.res = 0;
70599
+ u.cm.apArg = p->apArg;
70600
+ for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
70601
+ u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
70602
+ sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
6927970603
}
6928070604
6928170605
p->inVtabMethod = 1;
69282
- rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
70606
+ rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
6928370607
p->inVtabMethod = 0;
69284
- importVtabErrMsg(p, u.cl.pVtab);
70608
+ importVtabErrMsg(p, u.cm.pVtab);
6928570609
if( rc==SQLITE_OK ){
69286
- u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
70610
+ u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
6928770611
}
6928870612
69289
- if( u.cl.res ){
70613
+ if( u.cm.res ){
6929070614
pc = pOp->p2 - 1;
6929170615
}
6929270616
}
69293
- u.cl.pCur->nullRow = 0;
70617
+ u.cm.pCur->nullRow = 0;
6929470618
6929570619
break;
6929670620
}
6929770621
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6929870622
@@ -69302,55 +70626,55 @@
6930270626
** Store the value of the P2-th column of
6930370627
** the row of the virtual-table that the
6930470628
** P1 cursor is pointing to into register P3.
6930570629
*/
6930670630
case OP_VColumn: {
69307
-#if 0 /* local variables moved into u.cm */
70631
+#if 0 /* local variables moved into u.cn */
6930870632
sqlite3_vtab *pVtab;
6930970633
const sqlite3_module *pModule;
6931070634
Mem *pDest;
6931170635
sqlite3_context sContext;
69312
-#endif /* local variables moved into u.cm */
70636
+#endif /* local variables moved into u.cn */
6931370637
6931470638
VdbeCursor *pCur = p->apCsr[pOp->p1];
6931570639
assert( pCur->pVtabCursor );
6931670640
assert( pOp->p3>0 && pOp->p3<=p->nMem );
69317
- u.cm.pDest = &aMem[pOp->p3];
69318
- memAboutToChange(p, u.cm.pDest);
70641
+ u.cn.pDest = &aMem[pOp->p3];
70642
+ memAboutToChange(p, u.cn.pDest);
6931970643
if( pCur->nullRow ){
69320
- sqlite3VdbeMemSetNull(u.cm.pDest);
70644
+ sqlite3VdbeMemSetNull(u.cn.pDest);
6932170645
break;
6932270646
}
69323
- u.cm.pVtab = pCur->pVtabCursor->pVtab;
69324
- u.cm.pModule = u.cm.pVtab->pModule;
69325
- assert( u.cm.pModule->xColumn );
69326
- memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
70647
+ u.cn.pVtab = pCur->pVtabCursor->pVtab;
70648
+ u.cn.pModule = u.cn.pVtab->pModule;
70649
+ assert( u.cn.pModule->xColumn );
70650
+ memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
6932770651
6932870652
/* The output cell may already have a buffer allocated. Move
69329
- ** the current contents to u.cm.sContext.s so in case the user-function
70653
+ ** the current contents to u.cn.sContext.s so in case the user-function
6933070654
** can use the already allocated buffer instead of allocating a
6933170655
** new one.
6933270656
*/
69333
- sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
69334
- MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
70657
+ sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
70658
+ MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
6933570659
69336
- rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
69337
- importVtabErrMsg(p, u.cm.pVtab);
69338
- if( u.cm.sContext.isError ){
69339
- rc = u.cm.sContext.isError;
70660
+ rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
70661
+ importVtabErrMsg(p, u.cn.pVtab);
70662
+ if( u.cn.sContext.isError ){
70663
+ rc = u.cn.sContext.isError;
6934070664
}
6934170665
6934270666
/* Copy the result of the function to the P3 register. We
6934370667
** do this regardless of whether or not an error occurred to ensure any
69344
- ** dynamic allocation in u.cm.sContext.s (a Mem struct) is released.
70668
+ ** dynamic allocation in u.cn.sContext.s (a Mem struct) is released.
6934570669
*/
69346
- sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
69347
- sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
69348
- REGISTER_TRACE(pOp->p3, u.cm.pDest);
69349
- UPDATE_MAX_BLOBSIZE(u.cm.pDest);
70670
+ sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
70671
+ sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
70672
+ REGISTER_TRACE(pOp->p3, u.cn.pDest);
70673
+ UPDATE_MAX_BLOBSIZE(u.cn.pDest);
6935070674
69351
- if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
70675
+ if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
6935270676
goto too_big;
6935370677
}
6935470678
break;
6935570679
}
6935670680
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69361,42 +70685,42 @@
6936170685
** Advance virtual table P1 to the next row in its result set and
6936270686
** jump to instruction P2. Or, if the virtual table has reached
6936370687
** the end of its result set, then fall through to the next instruction.
6936470688
*/
6936570689
case OP_VNext: { /* jump */
69366
-#if 0 /* local variables moved into u.cn */
70690
+#if 0 /* local variables moved into u.co */
6936770691
sqlite3_vtab *pVtab;
6936870692
const sqlite3_module *pModule;
6936970693
int res;
6937070694
VdbeCursor *pCur;
69371
-#endif /* local variables moved into u.cn */
70695
+#endif /* local variables moved into u.co */
6937270696
69373
- u.cn.res = 0;
69374
- u.cn.pCur = p->apCsr[pOp->p1];
69375
- assert( u.cn.pCur->pVtabCursor );
69376
- if( u.cn.pCur->nullRow ){
70697
+ u.co.res = 0;
70698
+ u.co.pCur = p->apCsr[pOp->p1];
70699
+ assert( u.co.pCur->pVtabCursor );
70700
+ if( u.co.pCur->nullRow ){
6937770701
break;
6937870702
}
69379
- u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
69380
- u.cn.pModule = u.cn.pVtab->pModule;
69381
- assert( u.cn.pModule->xNext );
70703
+ u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
70704
+ u.co.pModule = u.co.pVtab->pModule;
70705
+ assert( u.co.pModule->xNext );
6938270706
6938370707
/* Invoke the xNext() method of the module. There is no way for the
6938470708
** underlying implementation to return an error if one occurs during
6938570709
** xNext(). Instead, if an error occurs, true is returned (indicating that
6938670710
** data is available) and the error code returned when xColumn or
6938770711
** some other method is next invoked on the save virtual table cursor.
6938870712
*/
6938970713
p->inVtabMethod = 1;
69390
- rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
70714
+ rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
6939170715
p->inVtabMethod = 0;
69392
- importVtabErrMsg(p, u.cn.pVtab);
70716
+ importVtabErrMsg(p, u.co.pVtab);
6939370717
if( rc==SQLITE_OK ){
69394
- u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
70718
+ u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
6939570719
}
6939670720
69397
- if( !u.cn.res ){
70721
+ if( !u.co.res ){
6939870722
/* If there is data, jump to P2 */
6939970723
pc = pOp->p2 - 1;
6940070724
}
6940170725
break;
6940270726
}
@@ -69408,28 +70732,28 @@
6940870732
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6940970733
** This opcode invokes the corresponding xRename method. The value
6941070734
** in register P1 is passed as the zName argument to the xRename method.
6941170735
*/
6941270736
case OP_VRename: {
69413
-#if 0 /* local variables moved into u.co */
70737
+#if 0 /* local variables moved into u.cp */
6941470738
sqlite3_vtab *pVtab;
6941570739
Mem *pName;
69416
-#endif /* local variables moved into u.co */
69417
-
69418
- u.co.pVtab = pOp->p4.pVtab->pVtab;
69419
- u.co.pName = &aMem[pOp->p1];
69420
- assert( u.co.pVtab->pModule->xRename );
69421
- assert( memIsValid(u.co.pName) );
69422
- REGISTER_TRACE(pOp->p1, u.co.pName);
69423
- assert( u.co.pName->flags & MEM_Str );
69424
- testcase( u.co.pName->enc==SQLITE_UTF8 );
69425
- testcase( u.co.pName->enc==SQLITE_UTF16BE );
69426
- testcase( u.co.pName->enc==SQLITE_UTF16LE );
69427
- rc = sqlite3VdbeChangeEncoding(u.co.pName, SQLITE_UTF8);
70740
+#endif /* local variables moved into u.cp */
70741
+
70742
+ u.cp.pVtab = pOp->p4.pVtab->pVtab;
70743
+ u.cp.pName = &aMem[pOp->p1];
70744
+ assert( u.cp.pVtab->pModule->xRename );
70745
+ assert( memIsValid(u.cp.pName) );
70746
+ REGISTER_TRACE(pOp->p1, u.cp.pName);
70747
+ assert( u.cp.pName->flags & MEM_Str );
70748
+ testcase( u.cp.pName->enc==SQLITE_UTF8 );
70749
+ testcase( u.cp.pName->enc==SQLITE_UTF16BE );
70750
+ testcase( u.cp.pName->enc==SQLITE_UTF16LE );
70751
+ rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
6942870752
if( rc==SQLITE_OK ){
69429
- rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
69430
- importVtabErrMsg(p, u.co.pVtab);
70753
+ rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
70754
+ importVtabErrMsg(p, u.cp.pVtab);
6943170755
p->expired = 0;
6943270756
}
6943370757
break;
6943470758
}
6943570759
#endif
@@ -69457,45 +70781,45 @@
6945770781
** P1 is a boolean flag. If it is set to true and the xUpdate call
6945870782
** is successful, then the value returned by sqlite3_last_insert_rowid()
6945970783
** is set to the value of the rowid for the row just inserted.
6946070784
*/
6946170785
case OP_VUpdate: {
69462
-#if 0 /* local variables moved into u.cp */
70786
+#if 0 /* local variables moved into u.cq */
6946370787
sqlite3_vtab *pVtab;
6946470788
sqlite3_module *pModule;
6946570789
int nArg;
6946670790
int i;
6946770791
sqlite_int64 rowid;
6946870792
Mem **apArg;
6946970793
Mem *pX;
69470
-#endif /* local variables moved into u.cp */
70794
+#endif /* local variables moved into u.cq */
6947170795
6947270796
assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6947370797
|| pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6947470798
);
69475
- u.cp.pVtab = pOp->p4.pVtab->pVtab;
69476
- u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
69477
- u.cp.nArg = pOp->p2;
70799
+ u.cq.pVtab = pOp->p4.pVtab->pVtab;
70800
+ u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
70801
+ u.cq.nArg = pOp->p2;
6947870802
assert( pOp->p4type==P4_VTAB );
69479
- if( ALWAYS(u.cp.pModule->xUpdate) ){
70803
+ if( ALWAYS(u.cq.pModule->xUpdate) ){
6948070804
u8 vtabOnConflict = db->vtabOnConflict;
69481
- u.cp.apArg = p->apArg;
69482
- u.cp.pX = &aMem[pOp->p3];
69483
- for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
69484
- assert( memIsValid(u.cp.pX) );
69485
- memAboutToChange(p, u.cp.pX);
69486
- sqlite3VdbeMemStoreType(u.cp.pX);
69487
- u.cp.apArg[u.cp.i] = u.cp.pX;
69488
- u.cp.pX++;
70805
+ u.cq.apArg = p->apArg;
70806
+ u.cq.pX = &aMem[pOp->p3];
70807
+ for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
70808
+ assert( memIsValid(u.cq.pX) );
70809
+ memAboutToChange(p, u.cq.pX);
70810
+ sqlite3VdbeMemStoreType(u.cq.pX);
70811
+ u.cq.apArg[u.cq.i] = u.cq.pX;
70812
+ u.cq.pX++;
6948970813
}
6949070814
db->vtabOnConflict = pOp->p5;
69491
- rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
70815
+ rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
6949270816
db->vtabOnConflict = vtabOnConflict;
69493
- importVtabErrMsg(p, u.cp.pVtab);
70817
+ importVtabErrMsg(p, u.cq.pVtab);
6949470818
if( rc==SQLITE_OK && pOp->p1 ){
69495
- assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
69496
- db->lastRowid = lastRowid = u.cp.rowid;
70819
+ assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
70820
+ db->lastRowid = lastRowid = u.cq.rowid;
6949770821
}
6949870822
if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6949970823
if( pOp->p5==OE_Ignore ){
6950070824
rc = SQLITE_OK;
6950170825
}else{
@@ -69551,25 +70875,25 @@
6955170875
**
6955270876
** If tracing is enabled (by the sqlite3_trace()) interface, then
6955370877
** the UTF-8 string contained in P4 is emitted on the trace callback.
6955470878
*/
6955570879
case OP_Trace: {
69556
-#if 0 /* local variables moved into u.cq */
70880
+#if 0 /* local variables moved into u.cr */
6955770881
char *zTrace;
6955870882
char *z;
69559
-#endif /* local variables moved into u.cq */
70883
+#endif /* local variables moved into u.cr */
6956070884
69561
- if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
69562
- u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
69563
- db->xTrace(db->pTraceArg, u.cq.z);
69564
- sqlite3DbFree(db, u.cq.z);
70885
+ if( db->xTrace && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
70886
+ u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
70887
+ db->xTrace(db->pTraceArg, u.cr.z);
70888
+ sqlite3DbFree(db, u.cr.z);
6956570889
}
6956670890
#ifdef SQLITE_DEBUG
6956770891
if( (db->flags & SQLITE_SqlTrace)!=0
69568
- && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
70892
+ && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6956970893
){
69570
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
70894
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
6957170895
}
6957270896
#endif /* SQLITE_DEBUG */
6957370897
break;
6957470898
}
6957570899
#endif
@@ -72487,11 +73811,11 @@
7248773811
pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
7248873812
if( pE==0 ) return 1;
7248973813
pE->pColl = pColl;
7249073814
pE->flags |= EP_IntValue | flags;
7249173815
pE->u.iValue = iCol;
72492
- pItem->iCol = (u16)iCol;
73816
+ pItem->iOrderByCol = (u16)iCol;
7249373817
pItem->done = 1;
7249473818
}else{
7249573819
moreToDo = 1;
7249673820
}
7249773821
}
@@ -72536,16 +73860,16 @@
7253673860
}
7253773861
#endif
7253873862
pEList = pSelect->pEList;
7253973863
assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
7254073864
for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
72541
- if( pItem->iCol ){
72542
- if( pItem->iCol>pEList->nExpr ){
73865
+ if( pItem->iOrderByCol ){
73866
+ if( pItem->iOrderByCol>pEList->nExpr ){
7254373867
resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
7254473868
return 1;
7254573869
}
72546
- resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
73870
+ resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
7254773871
}
7254873872
}
7254973873
return 0;
7255073874
}
7255173875
@@ -72588,11 +73912,11 @@
7258873912
if( iCol>0 ){
7258973913
/* If an AS-name match is found, mark this ORDER BY column as being
7259073914
** a copy of the iCol-th result-set column. The subsequent call to
7259173915
** sqlite3ResolveOrderGroupBy() will convert the expression to a
7259273916
** copy of the iCol-th result-set expression. */
72593
- pItem->iCol = (u16)iCol;
73917
+ pItem->iOrderByCol = (u16)iCol;
7259473918
continue;
7259573919
}
7259673920
if( sqlite3ExprIsInteger(pE, &iCol) ){
7259773921
/* The ORDER BY term is an integer constant. Again, set the column
7259873922
** number so that sqlite3ResolveOrderGroupBy() will convert the
@@ -72599,16 +73923,16 @@
7259973923
** order-by term to a copy of the result-set expression */
7260073924
if( iCol<1 ){
7260173925
resolveOutOfRangeError(pParse, zType, i+1, nResult);
7260273926
return 1;
7260373927
}
72604
- pItem->iCol = (u16)iCol;
73928
+ pItem->iOrderByCol = (u16)iCol;
7260573929
continue;
7260673930
}
7260773931
7260873932
/* Otherwise, treat the ORDER BY term as an ordinary expression */
72609
- pItem->iCol = 0;
73933
+ pItem->iOrderByCol = 0;
7261073934
if( sqlite3ResolveExprNames(pNC, pE) ){
7261173935
return 1;
7261273936
}
7261373937
}
7261473938
return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
@@ -73783,11 +75107,11 @@
7378375107
pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
7378475108
pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
7378575109
pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
7378675110
pItem->sortOrder = pOldItem->sortOrder;
7378775111
pItem->done = 0;
73788
- pItem->iCol = pOldItem->iCol;
75112
+ pItem->iOrderByCol = pOldItem->iOrderByCol;
7378975113
pItem->iAlias = pOldItem->iAlias;
7379075114
}
7379175115
return pNew;
7379275116
}
7379375117
@@ -73853,11 +75177,11 @@
7385375177
pNewItem->idx = pOldItem->idx;
7385475178
}
7385575179
return pNew;
7385675180
}
7385775181
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
73858
- Select *pNew;
75182
+ Select *pNew, *pPrior;
7385975183
if( p==0 ) return 0;
7386075184
pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
7386175185
if( pNew==0 ) return 0;
7386275186
pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
7386375187
pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
@@ -73864,11 +75188,13 @@
7386475188
pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
7386575189
pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
7386675190
pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
7386775191
pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
7386875192
pNew->op = p->op;
73869
- pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
75193
+ pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
75194
+ if( pPrior ) pPrior->pNext = pNew;
75195
+ pNew->pNext = 0;
7387075196
pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
7387175197
pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
7387275198
pNew->iLimit = 0;
7387375199
pNew->iOffset = 0;
7387475200
pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
@@ -74285,10 +75611,19 @@
7428575611
if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
7428675612
if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
7428775613
return 1;
7428875614
}
7428975615
#endif /* SQLITE_OMIT_SUBQUERY */
75616
+
75617
+/*
75618
+** Code an OP_Once instruction and allocate space for its flag. Return the
75619
+** address of the new instruction.
75620
+*/
75621
+SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
75622
+ Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
75623
+ return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
75624
+}
7429075625
7429175626
/*
7429275627
** This function is used by the implementation of the IN (...) operator.
7429375628
** It's job is to find or create a b-tree structure that may be used
7429475629
** either to test for membership of the (...) set or to iterate through
@@ -74346,10 +75681,11 @@
7434675681
SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
7434775682
Select *p; /* SELECT to the right of IN operator */
7434875683
int eType = 0; /* Type of RHS table. IN_INDEX_* */
7434975684
int iTab = pParse->nTab++; /* Cursor of the RHS table */
7435075685
int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
75686
+ Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
7435175687
7435275688
assert( pX->op==TK_IN );
7435375689
7435475690
/* Check to see if an existing table or index can be used to
7435575691
** satisfy the query. This is preferable to generating a new
@@ -74356,11 +75692,10 @@
7435675692
** ephemeral table.
7435775693
*/
7435875694
p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
7435975695
if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
7436075696
sqlite3 *db = pParse->db; /* Database connection */
74361
- Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
7436275697
Table *pTab; /* Table <table>. */
7436375698
Expr *pExpr; /* Expression <column> */
7436475699
int iCol; /* Index of column <column> */
7436575700
int iDb; /* Database idx for pTab */
7436675701
@@ -74381,14 +75716,13 @@
7438175716
** has already been allocated. So assume sqlite3GetVdbe() is always
7438275717
** successful here.
7438375718
*/
7438475719
assert(v);
7438575720
if( iCol<0 ){
74386
- int iMem = ++pParse->nMem;
7438775721
int iAddr;
7438875722
74389
- iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
75723
+ iAddr = sqlite3CodeOnce(pParse);
7439075724
7439175725
sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
7439275726
eType = IN_INDEX_ROWID;
7439375727
7439475728
sqlite3VdbeJumpHere(v, iAddr);
@@ -74410,25 +75744,25 @@
7441075744
for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
7441175745
if( (pIdx->aiColumn[0]==iCol)
7441275746
&& sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
7441375747
&& (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
7441475748
){
74415
- int iMem = ++pParse->nMem;
7441675749
int iAddr;
7441775750
char *pKey;
7441875751
7441975752
pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
74420
- iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
75753
+ iAddr = sqlite3CodeOnce(pParse);
7442175754
7442275755
sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
7442375756
pKey,P4_KEYINFO_HANDOFF);
7442475757
VdbeComment((v, "%s", pIdx->zName));
7442575758
eType = IN_INDEX_INDEX;
7442675759
7442775760
sqlite3VdbeJumpHere(v, iAddr);
7442875761
if( prNotFound && !pTab->aCol[iCol].notNull ){
7442975762
*prNotFound = ++pParse->nMem;
75763
+ sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
7443075764
}
7443175765
}
7443275766
}
7443375767
}
7443475768
}
@@ -74440,10 +75774,11 @@
7444075774
double savedNQueryLoop = pParse->nQueryLoop;
7444175775
int rMayHaveNull = 0;
7444275776
eType = IN_INDEX_EPH;
7444375777
if( prNotFound ){
7444475778
*prNotFound = rMayHaveNull = ++pParse->nMem;
75779
+ sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
7444575780
}else{
7444675781
testcase( pParse->nQueryLoop>(double)1 );
7444775782
pParse->nQueryLoop = (double)1;
7444875783
if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
7444975784
eType = IN_INDEX_ROWID;
@@ -74512,13 +75847,12 @@
7451275847
** * We are inside a trigger
7451375848
**
7451475849
** If all of the above are false, then we can run this code just once
7451575850
** save the results, and reuse the same result on subsequent invocations.
7451675851
*/
74517
- if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
74518
- int mem = ++pParse->nMem;
74519
- testAddr = sqlite3VdbeAddOp1(v, OP_Once, mem);
75852
+ if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
75853
+ testAddr = sqlite3CodeOnce(pParse);
7452075854
}
7452175855
7452275856
#ifndef SQLITE_OMIT_EXPLAIN
7452375857
if( pParse->explain==2 ){
7452475858
char *zMsg = sqlite3MPrintf(
@@ -75852,10 +77186,268 @@
7585277186
pExpr->op = TK_REGISTER;
7585377187
}
7585477188
return inReg;
7585577189
}
7585677190
77191
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
77192
+/*
77193
+** Generate a human-readable explanation of an expression tree.
77194
+*/
77195
+SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
77196
+ int op; /* The opcode being coded */
77197
+ const char *zBinOp = 0; /* Binary operator */
77198
+ const char *zUniOp = 0; /* Unary operator */
77199
+ if( pExpr==0 ){
77200
+ op = TK_NULL;
77201
+ }else{
77202
+ op = pExpr->op;
77203
+ }
77204
+ switch( op ){
77205
+ case TK_AGG_COLUMN: {
77206
+ sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
77207
+ pExpr->iTable, pExpr->iColumn);
77208
+ break;
77209
+ }
77210
+ case TK_COLUMN: {
77211
+ if( pExpr->iTable<0 ){
77212
+ /* This only happens when coding check constraints */
77213
+ sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
77214
+ }else{
77215
+ sqlite3ExplainPrintf(pOut, "{%d:%d}",
77216
+ pExpr->iTable, pExpr->iColumn);
77217
+ }
77218
+ break;
77219
+ }
77220
+ case TK_INTEGER: {
77221
+ if( pExpr->flags & EP_IntValue ){
77222
+ sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
77223
+ }else{
77224
+ sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
77225
+ }
77226
+ break;
77227
+ }
77228
+#ifndef SQLITE_OMIT_FLOATING_POINT
77229
+ case TK_FLOAT: {
77230
+ sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
77231
+ break;
77232
+ }
77233
+#endif
77234
+ case TK_STRING: {
77235
+ sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
77236
+ break;
77237
+ }
77238
+ case TK_NULL: {
77239
+ sqlite3ExplainPrintf(pOut,"NULL");
77240
+ break;
77241
+ }
77242
+#ifndef SQLITE_OMIT_BLOB_LITERAL
77243
+ case TK_BLOB: {
77244
+ sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
77245
+ break;
77246
+ }
77247
+#endif
77248
+ case TK_VARIABLE: {
77249
+ sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
77250
+ pExpr->u.zToken, pExpr->iColumn);
77251
+ break;
77252
+ }
77253
+ case TK_REGISTER: {
77254
+ sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
77255
+ break;
77256
+ }
77257
+ case TK_AS: {
77258
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77259
+ break;
77260
+ }
77261
+#ifndef SQLITE_OMIT_CAST
77262
+ case TK_CAST: {
77263
+ /* Expressions of the form: CAST(pLeft AS token) */
77264
+ const char *zAff = "unk";
77265
+ switch( sqlite3AffinityType(pExpr->u.zToken) ){
77266
+ case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
77267
+ case SQLITE_AFF_NONE: zAff = "NONE"; break;
77268
+ case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
77269
+ case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
77270
+ case SQLITE_AFF_REAL: zAff = "REAL"; break;
77271
+ }
77272
+ sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
77273
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77274
+ sqlite3ExplainPrintf(pOut, ")");
77275
+ break;
77276
+ }
77277
+#endif /* SQLITE_OMIT_CAST */
77278
+ case TK_LT: zBinOp = "LT"; break;
77279
+ case TK_LE: zBinOp = "LE"; break;
77280
+ case TK_GT: zBinOp = "GT"; break;
77281
+ case TK_GE: zBinOp = "GE"; break;
77282
+ case TK_NE: zBinOp = "NE"; break;
77283
+ case TK_EQ: zBinOp = "EQ"; break;
77284
+ case TK_IS: zBinOp = "IS"; break;
77285
+ case TK_ISNOT: zBinOp = "ISNOT"; break;
77286
+ case TK_AND: zBinOp = "AND"; break;
77287
+ case TK_OR: zBinOp = "OR"; break;
77288
+ case TK_PLUS: zBinOp = "ADD"; break;
77289
+ case TK_STAR: zBinOp = "MUL"; break;
77290
+ case TK_MINUS: zBinOp = "SUB"; break;
77291
+ case TK_REM: zBinOp = "REM"; break;
77292
+ case TK_BITAND: zBinOp = "BITAND"; break;
77293
+ case TK_BITOR: zBinOp = "BITOR"; break;
77294
+ case TK_SLASH: zBinOp = "DIV"; break;
77295
+ case TK_LSHIFT: zBinOp = "LSHIFT"; break;
77296
+ case TK_RSHIFT: zBinOp = "RSHIFT"; break;
77297
+ case TK_CONCAT: zBinOp = "CONCAT"; break;
77298
+
77299
+ case TK_UMINUS: zUniOp = "UMINUS"; break;
77300
+ case TK_UPLUS: zUniOp = "UPLUS"; break;
77301
+ case TK_BITNOT: zUniOp = "BITNOT"; break;
77302
+ case TK_NOT: zUniOp = "NOT"; break;
77303
+ case TK_ISNULL: zUniOp = "ISNULL"; break;
77304
+ case TK_NOTNULL: zUniOp = "NOTNULL"; break;
77305
+
77306
+ case TK_AGG_FUNCTION:
77307
+ case TK_CONST_FUNC:
77308
+ case TK_FUNCTION: {
77309
+ ExprList *pFarg; /* List of function arguments */
77310
+ if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
77311
+ pFarg = 0;
77312
+ }else{
77313
+ pFarg = pExpr->x.pList;
77314
+ }
77315
+ sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
77316
+ op==TK_AGG_FUNCTION ? "AGG_" : "",
77317
+ pExpr->u.zToken);
77318
+ if( pFarg ){
77319
+ sqlite3ExplainExprList(pOut, pFarg);
77320
+ }
77321
+ sqlite3ExplainPrintf(pOut, ")");
77322
+ break;
77323
+ }
77324
+#ifndef SQLITE_OMIT_SUBQUERY
77325
+ case TK_EXISTS: {
77326
+ sqlite3ExplainPrintf(pOut, "EXISTS(");
77327
+ sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77328
+ sqlite3ExplainPrintf(pOut,")");
77329
+ break;
77330
+ }
77331
+ case TK_SELECT: {
77332
+ sqlite3ExplainPrintf(pOut, "(");
77333
+ sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77334
+ sqlite3ExplainPrintf(pOut, ")");
77335
+ break;
77336
+ }
77337
+ case TK_IN: {
77338
+ sqlite3ExplainPrintf(pOut, "IN(");
77339
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77340
+ sqlite3ExplainPrintf(pOut, ",");
77341
+ if( ExprHasProperty(pExpr, EP_xIsSelect) ){
77342
+ sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77343
+ }else{
77344
+ sqlite3ExplainExprList(pOut, pExpr->x.pList);
77345
+ }
77346
+ sqlite3ExplainPrintf(pOut, ")");
77347
+ break;
77348
+ }
77349
+#endif /* SQLITE_OMIT_SUBQUERY */
77350
+
77351
+ /*
77352
+ ** x BETWEEN y AND z
77353
+ **
77354
+ ** This is equivalent to
77355
+ **
77356
+ ** x>=y AND x<=z
77357
+ **
77358
+ ** X is stored in pExpr->pLeft.
77359
+ ** Y is stored in pExpr->pList->a[0].pExpr.
77360
+ ** Z is stored in pExpr->pList->a[1].pExpr.
77361
+ */
77362
+ case TK_BETWEEN: {
77363
+ Expr *pX = pExpr->pLeft;
77364
+ Expr *pY = pExpr->x.pList->a[0].pExpr;
77365
+ Expr *pZ = pExpr->x.pList->a[1].pExpr;
77366
+ sqlite3ExplainPrintf(pOut, "BETWEEN(");
77367
+ sqlite3ExplainExpr(pOut, pX);
77368
+ sqlite3ExplainPrintf(pOut, ",");
77369
+ sqlite3ExplainExpr(pOut, pY);
77370
+ sqlite3ExplainPrintf(pOut, ",");
77371
+ sqlite3ExplainExpr(pOut, pZ);
77372
+ sqlite3ExplainPrintf(pOut, ")");
77373
+ break;
77374
+ }
77375
+ case TK_TRIGGER: {
77376
+ /* If the opcode is TK_TRIGGER, then the expression is a reference
77377
+ ** to a column in the new.* or old.* pseudo-tables available to
77378
+ ** trigger programs. In this case Expr.iTable is set to 1 for the
77379
+ ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
77380
+ ** is set to the column of the pseudo-table to read, or to -1 to
77381
+ ** read the rowid field.
77382
+ */
77383
+ sqlite3ExplainPrintf(pOut, "%s(%d)",
77384
+ pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
77385
+ break;
77386
+ }
77387
+ case TK_CASE: {
77388
+ sqlite3ExplainPrintf(pOut, "CASE(");
77389
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77390
+ sqlite3ExplainPrintf(pOut, ",");
77391
+ sqlite3ExplainExprList(pOut, pExpr->x.pList);
77392
+ break;
77393
+ }
77394
+#ifndef SQLITE_OMIT_TRIGGER
77395
+ case TK_RAISE: {
77396
+ const char *zType = "unk";
77397
+ switch( pExpr->affinity ){
77398
+ case OE_Rollback: zType = "rollback"; break;
77399
+ case OE_Abort: zType = "abort"; break;
77400
+ case OE_Fail: zType = "fail"; break;
77401
+ case OE_Ignore: zType = "ignore"; break;
77402
+ }
77403
+ sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
77404
+ break;
77405
+ }
77406
+#endif
77407
+ }
77408
+ if( zBinOp ){
77409
+ sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
77410
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77411
+ sqlite3ExplainPrintf(pOut,",");
77412
+ sqlite3ExplainExpr(pOut, pExpr->pRight);
77413
+ sqlite3ExplainPrintf(pOut,")");
77414
+ }else if( zUniOp ){
77415
+ sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
77416
+ sqlite3ExplainExpr(pOut, pExpr->pLeft);
77417
+ sqlite3ExplainPrintf(pOut,")");
77418
+ }
77419
+}
77420
+#endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
77421
+
77422
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
77423
+/*
77424
+** Generate a human-readable explanation of an expression list.
77425
+*/
77426
+SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
77427
+ int i;
77428
+ if( pList==0 || pList->nExpr==0 ){
77429
+ sqlite3ExplainPrintf(pOut, "(empty-list)");
77430
+ return;
77431
+ }else if( pList->nExpr==1 ){
77432
+ sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
77433
+ }else{
77434
+ sqlite3ExplainPush(pOut);
77435
+ for(i=0; i<pList->nExpr; i++){
77436
+ sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
77437
+ sqlite3ExplainPush(pOut);
77438
+ sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
77439
+ sqlite3ExplainPop(pOut);
77440
+ if( i<pList->nExpr-1 ){
77441
+ sqlite3ExplainNL(pOut);
77442
+ }
77443
+ }
77444
+ sqlite3ExplainPop(pOut);
77445
+ }
77446
+}
77447
+#endif /* SQLITE_DEBUG */
77448
+
7585777449
/*
7585877450
** Return TRUE if pExpr is an constant expression that is appropriate
7585977451
** for factoring out of a loop. Appropriate expressions are:
7586077452
**
7586177453
** * Any expression that evaluates to two or more opcodes.
@@ -76675,10 +78267,18 @@
7667578267
if( nReg>pParse->nRangeReg ){
7667678268
pParse->nRangeReg = nReg;
7667778269
pParse->iRangeReg = iReg;
7667878270
}
7667978271
}
78272
+
78273
+/*
78274
+** Mark all temporary registers as being unavailable for reuse.
78275
+*/
78276
+SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
78277
+ pParse->nTempReg = 0;
78278
+ pParse->nRangeReg = 0;
78279
+}
7668078280
7668178281
/************** End of expr.c ************************************************/
7668278282
/************** Begin file alter.c *******************************************/
7668378283
/*
7668478284
** 2005 February 15
@@ -78036,10 +79636,11 @@
7803679636
sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
7803779637
sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
7803879638
sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
7803979639
sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
7804079640
sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
79641
+ sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
7804179642
sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
7804279643
(char*)&stat3InitFuncdef, P4_FUNCDEF);
7804379644
sqlite3VdbeChangeP5(v, 2);
7804479645
#endif /* SQLITE_ENABLE_STAT3 */
7804579646
@@ -82100,23 +83701,27 @@
8210083701
** Allocate the index structure.
8210183702
*/
8210283703
nName = sqlite3Strlen30(zName);
8210383704
nCol = pList->nExpr;
8210483705
pIndex = sqlite3DbMallocZero(db,
82105
- sizeof(Index) + /* Index structure */
82106
- sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
82107
- sizeof(int)*nCol + /* Index.aiColumn */
82108
- sizeof(char *)*nCol + /* Index.azColl */
82109
- sizeof(u8)*nCol + /* Index.aSortOrder */
82110
- nName + 1 + /* Index.zName */
82111
- nExtra /* Collation sequence names */
83706
+ ROUND8(sizeof(Index)) + /* Index structure */
83707
+ ROUND8(sizeof(tRowcnt)*(nCol+1)) + /* Index.aiRowEst */
83708
+ sizeof(char *)*nCol + /* Index.azColl */
83709
+ sizeof(int)*nCol + /* Index.aiColumn */
83710
+ sizeof(u8)*nCol + /* Index.aSortOrder */
83711
+ nName + 1 + /* Index.zName */
83712
+ nExtra /* Collation sequence names */
8211283713
);
8211383714
if( db->mallocFailed ){
8211483715
goto exit_create_index;
8211583716
}
82116
- pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
82117
- pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
83717
+ zExtra = (char*)pIndex;
83718
+ pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
83719
+ pIndex->azColl = (char**)
83720
+ ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
83721
+ assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
83722
+ assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
8211883723
pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
8211983724
pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
8212083725
pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
8212183726
zExtra = (char *)(&pIndex->zName[nName+1]);
8212283727
memcpy(pIndex->zName, zName, nName+1);
@@ -83866,11 +85471,10 @@
8386685471
8386785472
/* Check that there isn't an ORDER BY without a LIMIT clause.
8386885473
*/
8386985474
if( pOrderBy && (pLimit == 0) ) {
8387085475
sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
83871
- pParse->parseError = 1;
8387285476
goto limit_where_cleanup_2;
8387385477
}
8387485478
8387585479
/* We only need to generate a select expression if there
8387685480
** is a limit/offset term to enforce.
@@ -87246,11 +88850,11 @@
8724688850
** 'b' NONE
8724788851
** 'c' NUMERIC
8724888852
** 'd' INTEGER
8724988853
** 'e' REAL
8725088854
**
87251
-** An extra 'b' is appended to the end of the string to cover the
88855
+** An extra 'd' is appended to the end of the string to cover the
8725288856
** rowid that appears as the last column in every index.
8725388857
**
8725488858
** Memory for the buffer containing the column index affinity string
8725588859
** is managed along with the rest of the Index structure. It will be
8725688860
** released when sqlite3DeleteIndex() is called.
@@ -87274,11 +88878,11 @@
8727488878
return 0;
8727588879
}
8727688880
for(n=0; n<pIdx->nColumn; n++){
8727788881
pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
8727888882
}
87279
- pIdx->zColAff[n++] = SQLITE_AFF_NONE;
88883
+ pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
8728088884
pIdx->zColAff[n] = 0;
8728188885
}
8728288886
8728388887
return pIdx->zColAff;
8728488888
}
@@ -87438,10 +89042,11 @@
8743889042
for(p = pParse->pAinc; p; p = p->pNext){
8743989043
pDb = &db->aDb[p->iDb];
8744089044
memId = p->regCtr;
8744189045
assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
8744289046
sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
89047
+ sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
8744389048
addr = sqlite3VdbeCurrentAddr(v);
8744489049
sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
8744589050
sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
8744689051
sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
8744789052
sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
@@ -88305,11 +89910,11 @@
8830589910
** any ABORT Back out changes from the current command
8830689911
** only (do not do a complete rollback) then
8830789912
** cause sqlite3_exec() to return immediately
8830889913
** with SQLITE_CONSTRAINT.
8830989914
**
88310
-** any FAIL Sqlite_exec() returns immediately with a
89915
+** any FAIL Sqlite3_exec() returns immediately with a
8831189916
** return code of SQLITE_CONSTRAINT. The
8831289917
** transaction is not rolled back and any
8831389918
** prior changes are retained.
8831489919
**
8831589920
** any IGNORE The record number and data is popped from
@@ -88777,35 +90382,29 @@
8877790382
/*
8877890383
** Attempt the transfer optimization on INSERTs of the form
8877990384
**
8878090385
** INSERT INTO tab1 SELECT * FROM tab2;
8878190386
**
88782
-** This optimization is only attempted if
88783
-**
88784
-** (1) tab1 and tab2 have identical schemas including all the
88785
-** same indices and constraints
88786
-**
88787
-** (2) tab1 and tab2 are different tables
88788
-**
88789
-** (3) There must be no triggers on tab1
88790
-**
88791
-** (4) The result set of the SELECT statement is "*"
88792
-**
88793
-** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
88794
-** or LIMIT clause.
88795
-**
88796
-** (6) The SELECT statement is a simple (not a compound) select that
88797
-** contains only tab2 in its FROM clause
88798
-**
88799
-** This method for implementing the INSERT transfers raw records from
88800
-** tab2 over to tab1. The columns are not decoded. Raw records from
88801
-** the indices of tab2 are transfered to tab1 as well. In so doing,
88802
-** the resulting tab1 has much less fragmentation.
88803
-**
88804
-** This routine returns TRUE if the optimization is attempted. If any
88805
-** of the conditions above fail so that the optimization should not
88806
-** be attempted, then this routine returns FALSE.
90387
+** The xfer optimization transfers raw records from tab2 over to tab1.
90388
+** Columns are not decoded and reassemblied, which greatly improves
90389
+** performance. Raw index records are transferred in the same way.
90390
+**
90391
+** The xfer optimization is only attempted if tab1 and tab2 are compatible.
90392
+** There are lots of rules for determining compatibility - see comments
90393
+** embedded in the code for details.
90394
+**
90395
+** This routine returns TRUE if the optimization is guaranteed to be used.
90396
+** Sometimes the xfer optimization will only work if the destination table
90397
+** is empty - a factor that can only be determined at run-time. In that
90398
+** case, this routine generates code for the xfer optimization but also
90399
+** does a test to see if the destination table is empty and jumps over the
90400
+** xfer optimization code if the test fails. In that case, this routine
90401
+** returns FALSE so that the caller will know to go ahead and generate
90402
+** an unoptimized transfer. This routine also returns FALSE if there
90403
+** is no chance that the xfer optimization can be applied.
90404
+**
90405
+** This optimization is particularly useful at making VACUUM run faster.
8880790406
*/
8880890407
static int xferOptimization(
8880990408
Parse *pParse, /* Parser context */
8881090409
Table *pDest, /* The table we are inserting into */
8881190410
Select *pSelect, /* A SELECT statement to use as the data source */
@@ -88838,14 +90437,12 @@
8883890437
if( pDest->tabFlags & TF_Virtual ){
8883990438
return 0; /* tab1 must not be a virtual table */
8884090439
}
8884190440
#endif
8884290441
if( onError==OE_Default ){
88843
- onError = OE_Abort;
88844
- }
88845
- if( onError!=OE_Abort && onError!=OE_Rollback ){
88846
- return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
90442
+ if( pDest->iPKey>=0 ) onError = pDest->keyConf;
90443
+ if( onError==OE_Default ) onError = OE_Abort;
8884790444
}
8884890445
assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
8884990446
if( pSelect->pSrc->nSrc!=1 ){
8885090447
return 0; /* FROM clause must have exactly one term */
8885190448
}
@@ -88947,20 +90544,16 @@
8894790544
if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
8894890545
return 0;
8894990546
}
8895090547
#endif
8895190548
if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
88952
- return 0;
90549
+ return 0; /* xfer opt does not play well with PRAGMA count_changes */
8895390550
}
8895490551
88955
- /* If we get this far, it means either:
88956
- **
88957
- ** * We can always do the transfer if the table contains an
88958
- ** an integer primary key
88959
- **
88960
- ** * We can conditionally do the transfer if the destination
88961
- ** table is empty.
90552
+ /* If we get this far, it means that the xfer optimization is at
90553
+ ** least a possibility, though it might only work if the destination
90554
+ ** table (tab1) is initially empty.
8896290555
*/
8896390556
#ifdef SQLITE_TEST
8896490557
sqlite3_xferopt_count++;
8896590558
#endif
8896690559
iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
@@ -88968,20 +90561,27 @@
8896890561
sqlite3CodeVerifySchema(pParse, iDbSrc);
8896990562
iSrc = pParse->nTab++;
8897090563
iDest = pParse->nTab++;
8897190564
regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
8897290565
sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
88973
- if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
88974
- /* If tables do not have an INTEGER PRIMARY KEY and there
88975
- ** are indices to be copied and the destination is not empty,
88976
- ** we have to disallow the transfer optimization because the
88977
- ** the rowids might change which will mess up indexing.
90566
+ if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
90567
+ || destHasUniqueIdx /* (2) */
90568
+ || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
90569
+ ){
90570
+ /* In some circumstances, we are able to run the xfer optimization
90571
+ ** only if the destination table is initially empty. This code makes
90572
+ ** that determination. Conditions under which the destination must
90573
+ ** be empty:
8897890574
**
88979
- ** Or if the destination has a UNIQUE index and is not empty,
88980
- ** we also disallow the transfer optimization because we cannot
88981
- ** insure that all entries in the union of DEST and SRC will be
88982
- ** unique.
90575
+ ** (1) There is no INTEGER PRIMARY KEY but there are indices.
90576
+ ** (If the destination is not initially empty, the rowid fields
90577
+ ** of index entries might need to change.)
90578
+ **
90579
+ ** (2) The destination has a unique index. (The xfer optimization
90580
+ ** is unable to test uniqueness.)
90581
+ **
90582
+ ** (3) onError is something other than OE_Abort and OE_Rollback.
8898390583
*/
8898490584
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
8898590585
emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
8898690586
sqlite3VdbeJumpHere(v, addr1);
8898790587
}else{
@@ -90269,10 +91869,11 @@
9026991869
** If anything goes wrong, set an error in the database connection.
9027091870
*/
9027191871
SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
9027291872
int i;
9027391873
int go = 1;
91874
+ int rc;
9027491875
int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
9027591876
9027691877
wsdAutoextInit;
9027791878
if( wsdAutoext.nExt==0 ){
9027891879
/* Common case: early out without every having to acquire a mutex */
@@ -90291,12 +91892,12 @@
9029191892
xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
9029291893
wsdAutoext.aExt[i];
9029391894
}
9029491895
sqlite3_mutex_leave(mutex);
9029591896
zErrmsg = 0;
90296
- if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
90297
- sqlite3Error(db, SQLITE_ERROR,
91897
+ if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
91898
+ sqlite3Error(db, rc,
9029891899
"automatic extension loading failed: %s", zErrmsg);
9029991900
go = 0;
9030091901
}
9030191902
sqlite3_free(zErrmsg);
9030291903
}
@@ -90649,11 +92250,11 @@
9064992250
zDb = pId2->n>0 ? pDb->zName : 0;
9065092251
if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
9065192252
goto pragma_out;
9065292253
}
9065392254
90654
-#ifndef SQLITE_OMIT_PAGER_PRAGMAS
92255
+#if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
9065592256
/*
9065692257
** PRAGMA [database.]default_cache_size
9065792258
** PRAGMA [database.]default_cache_size=N
9065892259
**
9065992260
** The first form reports the current persistent setting for the
@@ -90698,11 +92299,13 @@
9069892299
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9069992300
pDb->pSchema->cache_size = size;
9070092301
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9070192302
}
9070292303
}else
92304
+#endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
9070392305
92306
+#if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
9070492307
/*
9070592308
** PRAGMA [database.]page_size
9070692309
** PRAGMA [database.]page_size=N
9070792310
**
9070892311
** The first form reports the current setting for the
@@ -90719,11 +92322,11 @@
9071992322
}else{
9072092323
/* Malloc may fail when setting the page-size, as there is an internal
9072192324
** buffer that the pager module resizes using sqlite3_realloc().
9072292325
*/
9072392326
db->nextPagesize = sqlite3Atoi(zRight);
90724
- if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
92327
+ if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
9072592328
db->mallocFailed = 1;
9072692329
}
9072792330
}
9072892331
}else
9072992332
@@ -90759,10 +92362,14 @@
9075992362
** The first form reports the current setting for the
9076092363
** maximum number of pages in the database file. The
9076192364
** second form attempts to change this setting. Both
9076292365
** forms return the current setting.
9076392366
**
92367
+ ** The absolute value of N is used. This is undocumented and might
92368
+ ** change. The only purpose is to provide an easy way to test
92369
+ ** the sqlite3AbsInt32() function.
92370
+ **
9076492371
** PRAGMA [database.]page_count
9076592372
**
9076692373
** Return the number of pages in the specified database.
9076792374
*/
9076892375
if( sqlite3StrICmp(zLeft,"page_count")==0
@@ -90773,11 +92380,12 @@
9077392380
sqlite3CodeVerifySchema(pParse, iDb);
9077492381
iReg = ++pParse->nMem;
9077592382
if( sqlite3Tolower(zLeft[0])=='p' ){
9077692383
sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
9077792384
}else{
90778
- sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
92385
+ sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
92386
+ sqlite3AbsInt32(sqlite3Atoi(zRight)));
9077992387
}
9078092388
sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
9078192389
sqlite3VdbeSetNumCols(v, 1);
9078292390
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
9078392391
}else
@@ -90987,26 +92595,23 @@
9098792595
/*
9098892596
** PRAGMA [database.]cache_size
9098992597
** PRAGMA [database.]cache_size=N
9099092598
**
9099192599
** The first form reports the current local setting for the
90992
- ** page cache size. The local setting can be different from
90993
- ** the persistent cache size value that is stored in the database
90994
- ** file itself. The value returned is the maximum number of
90995
- ** pages in the page cache. The second form sets the local
90996
- ** page cache size value. It does not change the persistent
90997
- ** cache size stored on the disk so the cache size will revert
90998
- ** to its default value when the database is closed and reopened.
90999
- ** N should be a positive integer.
92600
+ ** page cache size. The second form sets the local
92601
+ ** page cache size value. If N is positive then that is the
92602
+ ** number of pages in the cache. If N is negative, then the
92603
+ ** number of pages is adjusted so that the cache uses -N kibibytes
92604
+ ** of memory.
9100092605
*/
9100192606
if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
9100292607
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
9100392608
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9100492609
if( !zRight ){
9100592610
returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
9100692611
}else{
91007
- int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
92612
+ int size = sqlite3Atoi(zRight);
9100892613
pDb->pSchema->cache_size = size;
9100992614
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9101092615
}
9101192616
}else
9101292617
@@ -91094,11 +92699,11 @@
9109492699
if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
9109592700
if( !zRight ){
9109692701
Pager *pPager = sqlite3BtreePager(pDb->pBt);
9109792702
char *proxy_file_path = NULL;
9109892703
sqlite3_file *pFile = sqlite3PagerFile(pPager);
91099
- sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
92704
+ sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
9110092705
&proxy_file_path);
9110192706
9110292707
if( proxy_file_path ){
9110392708
sqlite3VdbeSetNumCols(v, 1);
9110492709
sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
@@ -91737,10 +93342,20 @@
9173793342
db->xWalCallback==sqlite3WalDefaultHook ?
9173893343
SQLITE_PTR_TO_INT(db->pWalArg) : 0);
9173993344
}else
9174093345
#endif
9174193346
93347
+ /*
93348
+ ** PRAGMA shrink_memory
93349
+ **
93350
+ ** This pragma attempts to free as much memory as possible from the
93351
+ ** current database connection.
93352
+ */
93353
+ if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
93354
+ sqlite3_db_release_memory(db);
93355
+ }else
93356
+
9174293357
#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
9174393358
/*
9174493359
** Report the current state of file logs for all databases
9174593360
*/
9174693361
if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92111,13 +93726,17 @@
9211193726
DbSetProperty(db, iDb, DB_Empty);
9211293727
}
9211393728
pDb->pSchema->enc = ENC(db);
9211493729
9211593730
if( pDb->pSchema->cache_size==0 ){
93731
+#ifndef SQLITE_OMIT_DEPRECATED
9211693732
size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
9211793733
if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
9211893734
pDb->pSchema->cache_size = size;
93735
+#else
93736
+ pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
93737
+#endif
9211993738
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9212093739
}
9212193740
9212293741
/*
9212393742
** file_format==1 Version 3.0.0.
@@ -94912,20 +96531,20 @@
9491296531
*/
9491396532
if( op!=TK_ALL ){
9491496533
for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
9491596534
struct ExprList_item *pItem;
9491696535
for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
94917
- assert( pItem->iCol>0 );
94918
- if( pItem->iCol==i ) break;
96536
+ assert( pItem->iOrderByCol>0 );
96537
+ if( pItem->iOrderByCol==i ) break;
9491996538
}
9492096539
if( j==nOrderBy ){
9492196540
Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
9492296541
if( pNew==0 ) return SQLITE_NOMEM;
9492396542
pNew->flags |= EP_IntValue;
9492496543
pNew->u.iValue = i;
9492596544
pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
94926
- pOrderBy->a[nOrderBy++].iCol = (u16)i;
96545
+ pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
9492796546
}
9492896547
}
9492996548
}
9493096549
9493196550
/* Compute the comparison permutation and keyinfo that is used with
@@ -94937,12 +96556,12 @@
9493796556
*/
9493896557
aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
9493996558
if( aPermute ){
9494096559
struct ExprList_item *pItem;
9494196560
for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
94942
- assert( pItem->iCol>0 && pItem->iCol<=p->pEList->nExpr );
94943
- aPermute[i] = pItem->iCol - 1;
96561
+ assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
96562
+ aPermute[i] = pItem->iOrderByCol - 1;
9494496563
}
9494596564
pKeyMerge =
9494696565
sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
9494796566
if( pKeyMerge ){
9494896567
pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
@@ -95281,13 +96900,12 @@
9528196900
}
9528296901
#endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
9528396902
9528496903
#if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
9528596904
/*
95286
-** This routine attempts to flatten subqueries in order to speed
95287
-** execution. It returns 1 if it makes changes and 0 if no flattening
95288
-** occurs.
96905
+** This routine attempts to flatten subqueries as a performance optimization.
96906
+** This routine returns 1 if it makes changes and 0 if no flattening occurs.
9528996907
**
9529096908
** To understand the concept of flattening, consider the following
9529196909
** query:
9529296910
**
9529396911
** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
@@ -95325,11 +96943,14 @@
9532596943
** (4) has since been expanded to exclude all DISTINCT subqueries.
9532696944
**
9532796945
** (6) The subquery does not use aggregates or the outer query is not
9532896946
** DISTINCT.
9532996947
**
95330
-** (7) The subquery has a FROM clause.
96948
+** (7) The subquery has a FROM clause. TODO: For subqueries without
96949
+** A FROM clause, consider adding a FROM close with the special
96950
+** table sqlite_once that consists of a single row containing a
96951
+** single NULL.
9533196952
**
9533296953
** (8) The subquery does not use LIMIT or the outer query is not a join.
9533396954
**
9533496955
** (9) The subquery does not use LIMIT or the outer query does not use
9533596956
** aggregates.
@@ -95358,15 +96979,18 @@
9535896979
** compound clause made up entirely of non-aggregate queries, and
9535996980
** the parent query:
9536096981
**
9536196982
** * is not itself part of a compound select,
9536296983
** * is not an aggregate or DISTINCT query, and
95363
-** * has no other tables or sub-selects in the FROM clause.
96984
+** * is not a join
9536496985
**
9536596986
** The parent and sub-query may contain WHERE clauses. Subject to
9536696987
** rules (11), (13) and (14), they may also contain ORDER BY,
95367
-** LIMIT and OFFSET clauses.
96988
+** LIMIT and OFFSET clauses. The subquery cannot use any compound
96989
+** operator other than UNION ALL because all the other compound
96990
+** operators have an implied DISTINCT which is disallowed by
96991
+** restriction (4).
9536896992
**
9536996993
** (18) If the sub-query is a compound select, then all terms of the
9537096994
** ORDER by clause of the parent must be simple references to
9537196995
** columns of the sub-query.
9537296996
**
@@ -95374,11 +96998,11 @@
9537496998
** have a WHERE clause.
9537596999
**
9537697000
** (20) If the sub-query is a compound select, then it must not use
9537797001
** an ORDER BY clause. Ticket #3773. We could relax this constraint
9537897002
** somewhat by saying that the terms of the ORDER BY clause must
95379
-** appear as unmodified result columns in the outer query. But
97003
+** appear as unmodified result columns in the outer query. But we
9538097004
** have other optimizations in mind to deal with that case.
9538197005
**
9538297006
** (21) The subquery does not use LIMIT or the outer query is not
9538397007
** DISTINCT. (See ticket [752e1646fc]).
9538497008
**
@@ -95503,23 +97127,25 @@
9550397127
return 0;
9550497128
}
9550597129
for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
9550697130
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
9550797131
testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
97132
+ assert( pSub->pSrc!=0 );
9550897133
if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
9550997134
|| (pSub1->pPrior && pSub1->op!=TK_ALL)
95510
- || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
97135
+ || pSub1->pSrc->nSrc<1
9551197136
){
9551297137
return 0;
9551397138
}
97139
+ testcase( pSub1->pSrc->nSrc>1 );
9551497140
}
9551597141
9551697142
/* Restriction 18. */
9551797143
if( p->pOrderBy ){
9551897144
int ii;
9551997145
for(ii=0; ii<p->pOrderBy->nExpr; ii++){
95520
- if( p->pOrderBy->a[ii].iCol==0 ) return 0;
97146
+ if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
9552197147
}
9552297148
}
9552397149
}
9552497150
9552597151
/***** If we reach this point, flattening is permitted. *****/
@@ -96538,26 +98164,25 @@
9653898164
assert( pItem->addrFillSub==0 );
9653998165
pItem->regReturn = ++pParse->nMem;
9654098166
topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
9654198167
pItem->addrFillSub = topAddr+1;
9654298168
VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
96543
- if( pItem->isCorrelated==0 && pParse->pTriggerTab==0 ){
98169
+ if( pItem->isCorrelated==0 ){
9654498170
/* If the subquery is no correlated and if we are not inside of
9654598171
** a trigger, then we only need to compute the value of the subquery
9654698172
** once. */
96547
- int regOnce = ++pParse->nMem;
96548
- onceAddr = sqlite3VdbeAddOp1(v, OP_Once, regOnce);
98173
+ onceAddr = sqlite3CodeOnce(pParse);
9654998174
}
9655098175
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
9655198176
explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
9655298177
sqlite3Select(pParse, pSub, &dest);
9655398178
pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
9655498179
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
9655598180
retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
9655698181
VdbeComment((v, "end %s", pItem->pTab->zName));
9655798182
sqlite3VdbeChangeP1(v, topAddr, retAddr);
96558
-
98183
+ sqlite3ClearTempRegCache(pParse);
9655998184
}
9656098185
if( /*pParse->nErr ||*/ db->mallocFailed ){
9656198186
goto select_end;
9656298187
}
9656398188
pParse->nHeight -= sqlite3SelectExprHeight(p);
@@ -96848,10 +98473,11 @@
9684898473
pParse->nMem += pGroupBy->nExpr;
9684998474
sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
9685098475
VdbeComment((v, "clear abort flag"));
9685198476
sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
9685298477
VdbeComment((v, "indicate accumulator empty"));
98478
+ sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
9685398479
9685498480
/* Begin a loop that will extract all source rows in GROUP BY order.
9685598481
** This might involve two separate loops with an OP_Sort in between, or
9685698482
** it might be a single loop that uses an index to extract information
9685798483
** in the right order to begin with.
@@ -97187,102 +98813,102 @@
9718798813
sqlite3DbFree(db, sAggInfo.aCol);
9718898814
sqlite3DbFree(db, sAggInfo.aFunc);
9718998815
return rc;
9719098816
}
9719198817
97192
-#if defined(SQLITE_DEBUG)
97193
-/*
97194
-*******************************************************************************
97195
-** The following code is used for testing and debugging only. The code
97196
-** that follows does not appear in normal builds.
97197
-**
97198
-** These routines are used to print out the content of all or part of a
97199
-** parse structures such as Select or Expr. Such printouts are useful
97200
-** for helping to understand what is happening inside the code generator
97201
-** during the execution of complex SELECT statements.
97202
-**
97203
-** These routine are not called anywhere from within the normal
97204
-** code base. Then are intended to be called from within the debugger
97205
-** or from temporary "printf" statements inserted for debugging.
97206
-*/
97207
-SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
97208
- if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
97209
- sqlite3DebugPrintf("(%s", p->u.zToken);
97210
- }else{
97211
- sqlite3DebugPrintf("(%d", p->op);
97212
- }
97213
- if( p->pLeft ){
97214
- sqlite3DebugPrintf(" ");
97215
- sqlite3PrintExpr(p->pLeft);
97216
- }
97217
- if( p->pRight ){
97218
- sqlite3DebugPrintf(" ");
97219
- sqlite3PrintExpr(p->pRight);
97220
- }
97221
- sqlite3DebugPrintf(")");
97222
-}
97223
-SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
97224
- int i;
97225
- for(i=0; i<pList->nExpr; i++){
97226
- sqlite3PrintExpr(pList->a[i].pExpr);
97227
- if( i<pList->nExpr-1 ){
97228
- sqlite3DebugPrintf(", ");
97229
- }
97230
- }
97231
-}
97232
-SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
97233
- sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
97234
- sqlite3PrintExprList(p->pEList);
97235
- sqlite3DebugPrintf("\n");
97236
- if( p->pSrc ){
97237
- char *zPrefix;
97238
- int i;
97239
- zPrefix = "FROM";
98818
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
98819
+/*
98820
+** Generate a human-readable description of a the Select object.
98821
+*/
98822
+static void explainOneSelect(Vdbe *pVdbe, Select *p){
98823
+ sqlite3ExplainPrintf(pVdbe, "SELECT ");
98824
+ if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
98825
+ if( p->selFlags & SF_Distinct ){
98826
+ sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
98827
+ }
98828
+ if( p->selFlags & SF_Aggregate ){
98829
+ sqlite3ExplainPrintf(pVdbe, "agg_flag ");
98830
+ }
98831
+ sqlite3ExplainNL(pVdbe);
98832
+ sqlite3ExplainPrintf(pVdbe, " ");
98833
+ }
98834
+ sqlite3ExplainExprList(pVdbe, p->pEList);
98835
+ sqlite3ExplainNL(pVdbe);
98836
+ if( p->pSrc && p->pSrc->nSrc ){
98837
+ int i;
98838
+ sqlite3ExplainPrintf(pVdbe, "FROM ");
98839
+ sqlite3ExplainPush(pVdbe);
9724098840
for(i=0; i<p->pSrc->nSrc; i++){
9724198841
struct SrcList_item *pItem = &p->pSrc->a[i];
97242
- sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
97243
- zPrefix = "";
98842
+ sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
9724498843
if( pItem->pSelect ){
97245
- sqlite3DebugPrintf("(\n");
97246
- sqlite3PrintSelect(pItem->pSelect, indent+10);
97247
- sqlite3DebugPrintf("%*s)", indent+8, "");
98844
+ sqlite3ExplainSelect(pVdbe, pItem->pSelect);
98845
+ if( pItem->pTab ){
98846
+ sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
98847
+ }
9724898848
}else if( pItem->zName ){
97249
- sqlite3DebugPrintf("%s", pItem->zName);
97250
- }
97251
- if( pItem->pTab ){
97252
- sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
98849
+ sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
9725398850
}
9725498851
if( pItem->zAlias ){
97255
- sqlite3DebugPrintf(" AS %s", pItem->zAlias);
98852
+ sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
9725698853
}
97257
- if( i<p->pSrc->nSrc-1 ){
97258
- sqlite3DebugPrintf(",");
98854
+ if( pItem->jointype & JT_LEFT ){
98855
+ sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
9725998856
}
97260
- sqlite3DebugPrintf("\n");
98857
+ sqlite3ExplainNL(pVdbe);
9726198858
}
98859
+ sqlite3ExplainPop(pVdbe);
9726298860
}
9726398861
if( p->pWhere ){
97264
- sqlite3DebugPrintf("%*s WHERE ", indent, "");
97265
- sqlite3PrintExpr(p->pWhere);
97266
- sqlite3DebugPrintf("\n");
98862
+ sqlite3ExplainPrintf(pVdbe, "WHERE ");
98863
+ sqlite3ExplainExpr(pVdbe, p->pWhere);
98864
+ sqlite3ExplainNL(pVdbe);
9726798865
}
9726898866
if( p->pGroupBy ){
97269
- sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
97270
- sqlite3PrintExprList(p->pGroupBy);
97271
- sqlite3DebugPrintf("\n");
98867
+ sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
98868
+ sqlite3ExplainExprList(pVdbe, p->pGroupBy);
98869
+ sqlite3ExplainNL(pVdbe);
9727298870
}
9727398871
if( p->pHaving ){
97274
- sqlite3DebugPrintf("%*s HAVING ", indent, "");
97275
- sqlite3PrintExpr(p->pHaving);
97276
- sqlite3DebugPrintf("\n");
98872
+ sqlite3ExplainPrintf(pVdbe, "HAVING ");
98873
+ sqlite3ExplainExpr(pVdbe, p->pHaving);
98874
+ sqlite3ExplainNL(pVdbe);
9727798875
}
9727898876
if( p->pOrderBy ){
97279
- sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
97280
- sqlite3PrintExprList(p->pOrderBy);
97281
- sqlite3DebugPrintf("\n");
98877
+ sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
98878
+ sqlite3ExplainExprList(pVdbe, p->pOrderBy);
98879
+ sqlite3ExplainNL(pVdbe);
98880
+ }
98881
+ if( p->pLimit ){
98882
+ sqlite3ExplainPrintf(pVdbe, "LIMIT ");
98883
+ sqlite3ExplainExpr(pVdbe, p->pLimit);
98884
+ sqlite3ExplainNL(pVdbe);
98885
+ }
98886
+ if( p->pOffset ){
98887
+ sqlite3ExplainPrintf(pVdbe, "OFFSET ");
98888
+ sqlite3ExplainExpr(pVdbe, p->pOffset);
98889
+ sqlite3ExplainNL(pVdbe);
9728298890
}
9728398891
}
98892
+SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
98893
+ if( p==0 ){
98894
+ sqlite3ExplainPrintf(pVdbe, "(null-select)");
98895
+ return;
98896
+ }
98897
+ while( p->pPrior ) p = p->pPrior;
98898
+ sqlite3ExplainPush(pVdbe);
98899
+ while( p ){
98900
+ explainOneSelect(pVdbe, p);
98901
+ p = p->pNext;
98902
+ if( p==0 ) break;
98903
+ sqlite3ExplainNL(pVdbe);
98904
+ sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
98905
+ }
98906
+ sqlite3ExplainPrintf(pVdbe, "END");
98907
+ sqlite3ExplainPop(pVdbe);
98908
+}
98909
+
9728498910
/* End of the structure debug printing code
9728598911
*****************************************************************************/
9728698912
#endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
9728798913
9728898914
/************** End of select.c **********************************************/
@@ -98389,10 +100015,11 @@
98389100015
if( db->mallocFailed==0 ){
98390100016
pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
98391100017
}
98392100018
pProgram->nMem = pSubParse->nMem;
98393100019
pProgram->nCsr = pSubParse->nTab;
100020
+ pProgram->nOnce = pSubParse->nOnce;
98394100021
pProgram->token = (void *)pTrigger;
98395100022
pPrg->aColmask[0] = pSubParse->oldmask;
98396100023
pPrg->aColmask[1] = pSubParse->newmask;
98397100024
sqlite3VdbeDelete(v);
98398100025
}
@@ -98736,12 +100363,12 @@
98736100363
98737100364
/* Register Allocations */
98738100365
int regRowCount = 0; /* A count of rows changed */
98739100366
int regOldRowid; /* The old rowid */
98740100367
int regNewRowid; /* The new rowid */
98741
- int regNew;
98742
- int regOld = 0;
100368
+ int regNew; /* Content of the NEW.* table in triggers */
100369
+ int regOld = 0; /* Content of OLD.* table in triggers */
98743100370
int regRowSet = 0; /* Rowset of rows to be updated */
98744100371
98745100372
memset(&sContext, 0, sizeof(sContext));
98746100373
db = pParse->db;
98747100374
if( pParse->nErr || db->mallocFailed ){
@@ -98886,10 +100513,11 @@
98886100513
goto update_cleanup;
98887100514
}
98888100515
#endif
98889100516
98890100517
/* Allocate required registers. */
100518
+ regRowSet = ++pParse->nMem;
98891100519
regOldRowid = regNewRowid = ++pParse->nMem;
98892100520
if( pTrigger || hasFK ){
98893100521
regOld = pParse->nMem + 1;
98894100522
pParse->nMem += pTab->nCol;
98895100523
}
@@ -98920,11 +100548,11 @@
98920100548
goto update_cleanup;
98921100549
}
98922100550
98923100551
/* Begin the database scan
98924100552
*/
98925
- sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
100553
+ sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
98926100554
pWInfo = sqlite3WhereBegin(
98927100555
pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
98928100556
);
98929100557
if( pWInfo==0 ) goto update_cleanup;
98930100558
okOnePass = pWInfo->okOnePass;
@@ -98931,11 +100559,10 @@
98931100559
98932100560
/* Remember the rowid of every item to be updated.
98933100561
*/
98934100562
sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
98935100563
if( !okOnePass ){
98936
- regRowSet = ++pParse->nMem;
98937100564
sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
98938100565
}
98939100566
98940100567
/* End the database scan loop.
98941100568
*/
@@ -99035,13 +100662,14 @@
99035100662
** be used eliminates some redundant opcodes.
99036100663
*/
99037100664
newmask = sqlite3TriggerColmask(
99038100665
pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
99039100666
);
100667
+ sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
99040100668
for(i=0; i<pTab->nCol; i++){
99041100669
if( i==pTab->iPKey ){
99042
- sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
100670
+ /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
99043100671
}else{
99044100672
j = aXRef[i];
99045100673
if( j>=0 ){
99046100674
sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
99047100675
}else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
@@ -101300,11 +102928,11 @@
101300102928
if( pTerm->leftCursor==iCur
101301102929
&& (pTerm->prereqRight & notReady)==0
101302102930
&& pTerm->u.leftColumn==iColumn
101303102931
&& (pTerm->eOperator & op)!=0
101304102932
){
101305
- if( pIdx && pTerm->eOperator!=WO_ISNULL ){
102933
+ if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
101306102934
Expr *pX = pTerm->pExpr;
101307102935
CollSeq *pColl;
101308102936
char idxaff;
101309102937
int j;
101310102938
Parse *pParse = pWC->pParse;
@@ -102701,11 +104329,10 @@
102701104329
WhereTerm *pTerm; /* A single term of the WHERE clause */
102702104330
WhereTerm *pWCEnd; /* End of pWC->a[] */
102703104331
int nByte; /* Byte of memory needed for pIdx */
102704104332
Index *pIdx; /* Object describing the transient index */
102705104333
Vdbe *v; /* Prepared statement under construction */
102706
- int regIsInit; /* Register set by initialization */
102707104334
int addrInit; /* Address of the initialization bypass jump */
102708104335
Table *pTable; /* The table being indexed */
102709104336
KeyInfo *pKeyinfo; /* Key information for the index */
102710104337
int addrTop; /* Top of the index fill loop */
102711104338
int regRecord; /* Register holding an index record */
@@ -102718,12 +104345,11 @@
102718104345
102719104346
/* Generate code to skip over the creation and initialization of the
102720104347
** transient index on 2nd and subsequent iterations of the loop. */
102721104348
v = pParse->pVdbe;
102722104349
assert( v!=0 );
102723
- regIsInit = ++pParse->nMem;
102724
- addrInit = sqlite3VdbeAddOp1(v, OP_Once, regIsInit);
104350
+ addrInit = sqlite3CodeOnce(pParse);
102725104351
102726104352
/* Count the number of columns that will be added to the index
102727104353
** and used to match WHERE clause constraints */
102728104354
nColumn = 0;
102729104355
pTable = pSrc->pTab;
@@ -103748,14 +105374,28 @@
103748105374
#ifdef SQLITE_ENABLE_STAT3
103749105375
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103750105376
#endif
103751105377
used |= pTerm->prereqRight;
103752105378
}
103753
-
103754
- /* Determine the value of rangeDiv */
103755
- if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103756
- int j = pProbe->aiColumn[nEq];
105379
+
105380
+ /* If the index being considered is UNIQUE, and there is an equality
105381
+ ** constraint for all columns in the index, then this search will find
105382
+ ** at most a single row. In this case set the WHERE_UNIQUE flag to
105383
+ ** indicate this to the caller.
105384
+ **
105385
+ ** Otherwise, if the search may find more than one row, test to see if
105386
+ ** there is a range constraint on indexed column (nEq+1) that can be
105387
+ ** optimized using the index.
105388
+ */
105389
+ if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105390
+ testcase( wsFlags & WHERE_COLUMN_IN );
105391
+ testcase( wsFlags & WHERE_COLUMN_NULL );
105392
+ if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105393
+ wsFlags |= WHERE_UNIQUE;
105394
+ }
105395
+ }else if( pProbe->bUnordered==0 ){
105396
+ int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
103757105397
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103758105398
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103759105399
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103760105400
whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103761105401
if( pTop ){
@@ -103770,16 +105410,10 @@
103770105410
used |= pBtm->prereqRight;
103771105411
testcase( pBtm->pWC!=pWC );
103772105412
}
103773105413
wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103774105414
}
103775
- }else if( pProbe->onError!=OE_None ){
103776
- testcase( wsFlags & WHERE_COLUMN_IN );
103777
- testcase( wsFlags & WHERE_COLUMN_NULL );
103778
- if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
103779
- wsFlags |= WHERE_UNIQUE;
103780
- }
103781105415
}
103782105416
103783105417
/* If there is an ORDER BY clause and the index being considered will
103784105418
** naturally scan rows in the required order, set the appropriate flags
103785105419
** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
@@ -104386,14 +106020,16 @@
104386106020
explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
104387106021
}
104388106022
104389106023
j = i;
104390106024
if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
104391
- explainAppendTerm(&txt, i++, aCol[aiColumn[j]].zName, ">");
106025
+ char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
106026
+ explainAppendTerm(&txt, i++, z, ">");
104392106027
}
104393106028
if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
104394
- explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<");
106029
+ char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
106030
+ explainAppendTerm(&txt, i, z, "<");
104395106031
}
104396106032
sqlite3StrAccumAppend(&txt, ")", 1);
104397106033
return sqlite3StrAccumFinish(&txt);
104398106034
}
104399106035
@@ -104747,11 +106383,11 @@
104747106383
char *zStartAff; /* Affinity for start of range constraint */
104748106384
char *zEndAff; /* Affinity for end of range constraint */
104749106385
104750106386
pIdx = pLevel->plan.u.pIdx;
104751106387
iIdxCur = pLevel->iIdxCur;
104752
- k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
106388
+ k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
104753106389
104754106390
/* If this loop satisfies a sort order (pOrderBy) request that
104755106391
** was passed to this function to implement a "SELECT min(x) ..."
104756106392
** query, then the caller will only allow the loop to run for
104757106393
** a single iteration. This means that the first row returned
@@ -104793,11 +106429,13 @@
104793106429
104794106430
/* If we are doing a reverse order scan on an ascending index, or
104795106431
** a forward order scan on a descending index, interchange the
104796106432
** start and end terms (pRangeStart and pRangeEnd).
104797106433
*/
104798
- if( nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
106434
+ if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
106435
+ || (bRev && pIdx->nColumn==nEq)
106436
+ ){
104799106437
SWAP(WhereTerm *, pRangeEnd, pRangeStart);
104800106438
}
104801106439
104802106440
testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
104803106441
testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
@@ -107559,11 +109197,10 @@
107559109197
/* Here code is inserted which will execute if the parser
107560109198
** stack every overflows */
107561109199
107562109200
UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
107563109201
sqlite3ErrorMsg(pParse, "parser stack overflow");
107564
- pParse->parseError = 1;
107565109202
sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
107566109203
}
107567109204
107568109205
/*
107569109206
** Perform a shift action.
@@ -108282,10 +109919,13 @@
108282109919
break;
108283109920
case 112: /* cmd ::= select */
108284109921
{
108285109922
SelectDest dest = {SRT_Output, 0, 0, 0, 0};
108286109923
sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
109924
+ sqlite3ExplainBegin(pParse->pVdbe);
109925
+ sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy387);
109926
+ sqlite3ExplainFinish(pParse->pVdbe);
108287109927
sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
108288109928
}
108289109929
break;
108290109930
case 113: /* select ::= oneselect */
108291109931
{yygotominor.yy387 = yymsp[0].minor.yy387;}
@@ -109160,11 +110800,10 @@
109160110800
#define TOKEN (yyminor.yy0)
109161110801
109162110802
UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
109163110803
assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
109164110804
sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
109165
- pParse->parseError = 1;
109166110805
sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
109167110806
}
109168110807
109169110808
/*
109170110809
** The following is executed when the parser accepts
@@ -109755,11 +111394,11 @@
109755111394
*tokenType = TK_SPACE;
109756111395
return i;
109757111396
}
109758111397
case '-': {
109759111398
if( z[1]=='-' ){
109760
- /* IMP: R-15891-05542 -- syntax diagram for comments */
111399
+ /* IMP: R-50417-27976 -- syntax diagram for comments */
109761111400
for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
109762111401
*tokenType = TK_SPACE; /* IMP: R-22934-25134 */
109763111402
return i;
109764111403
}
109765111404
*tokenType = TK_MINUS;
@@ -109788,11 +111427,11 @@
109788111427
case '/': {
109789111428
if( z[1]!='*' || z[2]==0 ){
109790111429
*tokenType = TK_SLASH;
109791111430
return 1;
109792111431
}
109793
- /* IMP: R-15891-05542 -- syntax diagram for comments */
111432
+ /* IMP: R-50417-27976 -- syntax diagram for comments */
109794111433
for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
109795111434
if( c ) i++;
109796111435
*tokenType = TK_SPACE; /* IMP: R-22934-25134 */
109797111436
return i;
109798111437
}
@@ -110582,12 +112221,12 @@
110582112221
/* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
110583112222
** returns an integer equal to SQLITE_VERSION_NUMBER.
110584112223
*/
110585112224
SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
110586112225
110587
-/* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
110588
-** zero if and only if SQLite was compiled mutexing code omitted due to
112226
+/* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
112227
+** zero if and only if SQLite was compiled with mutexing code omitted due to
110589112228
** the SQLITE_THREADSAFE compile-time option being set to 0.
110590112229
*/
110591112230
SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
110592112231
110593112232
#if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
@@ -110772,12 +112411,12 @@
110772112411
/* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110773112412
** compile-time option.
110774112413
*/
110775112414
#ifdef SQLITE_EXTRA_INIT
110776112415
if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110777
- int SQLITE_EXTRA_INIT(void);
110778
- rc = SQLITE_EXTRA_INIT();
112416
+ int SQLITE_EXTRA_INIT(const char*);
112417
+ rc = SQLITE_EXTRA_INIT(0);
110779112418
}
110780112419
#endif
110781112420
110782112421
return rc;
110783112422
}
@@ -110790,10 +112429,14 @@
110790112429
** on when SQLite is already shut down. If SQLite is already shut down
110791112430
** when this routine is invoked, then this routine is a harmless no-op.
110792112431
*/
110793112432
SQLITE_API int sqlite3_shutdown(void){
110794112433
if( sqlite3GlobalConfig.isInit ){
112434
+#ifdef SQLITE_EXTRA_SHUTDOWN
112435
+ void SQLITE_EXTRA_SHUTDOWN(void);
112436
+ SQLITE_EXTRA_SHUTDOWN();
112437
+#endif
110795112438
sqlite3_os_end();
110796112439
sqlite3_reset_auto_extension();
110797112440
sqlite3GlobalConfig.isInit = 0;
110798112441
}
110799112442
if( sqlite3GlobalConfig.isPCacheInit ){
@@ -110898,20 +112541,29 @@
110898112541
sqlite3GlobalConfig.nPage = va_arg(ap, int);
110899112542
break;
110900112543
}
110901112544
110902112545
case SQLITE_CONFIG_PCACHE: {
110903
- /* Specify an alternative page cache implementation */
110904
- sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
112546
+ /* no-op */
110905112547
break;
110906112548
}
110907
-
110908112549
case SQLITE_CONFIG_GETPCACHE: {
110909
- if( sqlite3GlobalConfig.pcache.xInit==0 ){
112550
+ /* now an error */
112551
+ rc = SQLITE_ERROR;
112552
+ break;
112553
+ }
112554
+
112555
+ case SQLITE_CONFIG_PCACHE2: {
112556
+ /* Specify an alternative page cache implementation */
112557
+ sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
112558
+ break;
112559
+ }
112560
+ case SQLITE_CONFIG_GETPCACHE2: {
112561
+ if( sqlite3GlobalConfig.pcache2.xInit==0 ){
110910112562
sqlite3PCacheSetDefault();
110911112563
}
110912
- *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
112564
+ *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
110913112565
break;
110914112566
}
110915112567
110916112568
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
110917112569
case SQLITE_CONFIG_HEAP: {
@@ -111006,25 +112658,25 @@
111006112658
** both at the same time.
111007112659
*/
111008112660
if( db->lookaside.bMalloced ){
111009112661
sqlite3_free(db->lookaside.pStart);
111010112662
}
111011
- /* The size of a lookaside slot needs to be larger than a pointer
111012
- ** to be useful.
112663
+ /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
112664
+ ** than a pointer to be useful.
111013112665
*/
112666
+ sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
111014112667
if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
111015112668
if( cnt<0 ) cnt = 0;
111016112669
if( sz==0 || cnt==0 ){
111017112670
sz = 0;
111018112671
pStart = 0;
111019112672
}else if( pBuf==0 ){
111020
- sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
111021112673
sqlite3BeginBenignMalloc();
111022112674
pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
111023112675
sqlite3EndBenignMalloc();
112676
+ if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
111024112677
}else{
111025
- sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
111026112678
pStart = pBuf;
111027112679
}
111028112680
db->lookaside.pStart = pStart;
111029112681
db->lookaside.pFree = 0;
111030112682
db->lookaside.sz = (u16)sz;
@@ -111053,10 +112705,30 @@
111053112705
** Return the mutex associated with a database connection.
111054112706
*/
111055112707
SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
111056112708
return db->mutex;
111057112709
}
112710
+
112711
+/*
112712
+** Free up as much memory as we can from the given database
112713
+** connection.
112714
+*/
112715
+SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
112716
+ int i;
112717
+ sqlite3_mutex_enter(db->mutex);
112718
+ sqlite3BtreeEnterAll(db);
112719
+ for(i=0; i<db->nDb; i++){
112720
+ Btree *pBt = db->aDb[i].pBt;
112721
+ if( pBt ){
112722
+ Pager *pPager = sqlite3BtreePager(pBt);
112723
+ sqlite3PagerShrink(pPager);
112724
+ }
112725
+ }
112726
+ sqlite3BtreeLeaveAll(db);
112727
+ sqlite3_mutex_leave(db->mutex);
112728
+ return SQLITE_OK;
112729
+}
111058112730
111059112731
/*
111060112732
** Configuration settings for an individual database connection
111061112733
*/
111062112734
SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
@@ -112161,11 +113833,10 @@
112161113833
*/
112162113834
static int createCollation(
112163113835
sqlite3* db,
112164113836
const char *zName,
112165113837
u8 enc,
112166
- u8 collType,
112167113838
void* pCtx,
112168113839
int(*xCompare)(void*,int,const void*,int,const void*),
112169113840
void(*xDel)(void*)
112170113841
){
112171113842
CollSeq *pColl;
@@ -112226,11 +113897,10 @@
112226113897
if( pColl==0 ) return SQLITE_NOMEM;
112227113898
pColl->xCmp = xCompare;
112228113899
pColl->pUser = pCtx;
112229113900
pColl->xDel = xDel;
112230113901
pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
112231
- pColl->type = collType;
112232113902
sqlite3Error(db, SQLITE_OK, 0);
112233113903
return SQLITE_OK;
112234113904
}
112235113905
112236113906
@@ -112687,27 +114357,22 @@
112687114357
112688114358
/* Add the default collation sequence BINARY. BINARY works for both UTF-8
112689114359
** and UTF-16, so add a version for each to avoid any unnecessary
112690114360
** conversions. The only error that can occur here is a malloc() failure.
112691114361
*/
112692
- createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
112693
- binCollFunc, 0);
112694
- createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
112695
- binCollFunc, 0);
112696
- createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
112697
- binCollFunc, 0);
112698
- createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
112699
- binCollFunc, 0);
114362
+ createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
114363
+ createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
114364
+ createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
114365
+ createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
112700114366
if( db->mallocFailed ){
112701114367
goto opendb_out;
112702114368
}
112703114369
db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
112704114370
assert( db->pDfltColl!=0 );
112705114371
112706114372
/* Also add a UTF-8 case-insensitive collation sequence. */
112707
- createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
112708
- nocaseCollatingFunc, 0);
114373
+ createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
112709114374
112710114375
/* Parse the filename/URI argument. */
112711114376
db->openFlags = flags;
112712114377
rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
112713114378
if( rc!=SQLITE_OK ){
@@ -112752,14 +114417,17 @@
112752114417
sqlite3RegisterBuiltinFunctions(db);
112753114418
112754114419
/* Load automatic extensions - extensions that have been registered
112755114420
** using the sqlite3_automatic_extension() API.
112756114421
*/
112757
- sqlite3AutoLoadExtensions(db);
112758114422
rc = sqlite3_errcode(db);
112759
- if( rc!=SQLITE_OK ){
112760
- goto opendb_out;
114423
+ if( rc==SQLITE_OK ){
114424
+ sqlite3AutoLoadExtensions(db);
114425
+ rc = sqlite3_errcode(db);
114426
+ if( rc!=SQLITE_OK ){
114427
+ goto opendb_out;
114428
+ }
112761114429
}
112762114430
112763114431
#ifdef SQLITE_ENABLE_FTS1
112764114432
if( !db->mallocFailed ){
112765114433
extern int sqlite3Fts1Init(sqlite3*);
@@ -112896,11 +114564,11 @@
112896114564
int(*xCompare)(void*,int,const void*,int,const void*)
112897114565
){
112898114566
int rc;
112899114567
sqlite3_mutex_enter(db->mutex);
112900114568
assert( !db->mallocFailed );
112901
- rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
114569
+ rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
112902114570
rc = sqlite3ApiExit(db, rc);
112903114571
sqlite3_mutex_leave(db->mutex);
112904114572
return rc;
112905114573
}
112906114574
@@ -112916,11 +114584,11 @@
112916114584
void(*xDel)(void*)
112917114585
){
112918114586
int rc;
112919114587
sqlite3_mutex_enter(db->mutex);
112920114588
assert( !db->mallocFailed );
112921
- rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
114589
+ rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
112922114590
rc = sqlite3ApiExit(db, rc);
112923114591
sqlite3_mutex_leave(db->mutex);
112924114592
return rc;
112925114593
}
112926114594
@@ -112939,11 +114607,11 @@
112939114607
char *zName8;
112940114608
sqlite3_mutex_enter(db->mutex);
112941114609
assert( !db->mallocFailed );
112942114610
zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
112943114611
if( zName8 ){
112944
- rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
114612
+ rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
112945114613
sqlite3DbFree(db, zName8);
112946114614
}
112947114615
rc = sqlite3ApiExit(db, rc);
112948114616
sqlite3_mutex_leave(db->mutex);
112949114617
return rc;
@@ -113422,19 +115090,10 @@
113422115090
rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
113423115091
break;
113424115092
}
113425115093
#endif
113426115094
113427
- /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
113428
- **
113429
- ** Return the size of a pcache header in bytes.
113430
- */
113431
- case SQLITE_TESTCTRL_PGHDRSZ: {
113432
- rc = sizeof(PgHdr);
113433
- break;
113434
- }
113435
-
113436115095
/* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
113437115096
**
113438115097
** Pass pFree into sqlite3ScratchFree().
113439115098
** If sz>0 then allocate a scratch buffer into pNew.
113440115099
*/
@@ -113458,10 +115117,26 @@
113458115117
case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
113459115118
sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
113460115119
break;
113461115120
}
113462115121
115122
+#if defined(SQLITE_ENABLE_TREE_EXPLAIN)
115123
+ /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
115124
+ ** sqlite3_stmt*,const char**);
115125
+ **
115126
+ ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
115127
+ ** a string that describes the optimized parse tree. This test-control
115128
+ ** returns a pointer to that string.
115129
+ */
115130
+ case SQLITE_TESTCTRL_EXPLAIN_STMT: {
115131
+ sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
115132
+ const char **pzRet = va_arg(ap, const char**);
115133
+ *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
115134
+ break;
115135
+ }
115136
+#endif
115137
+
113463115138
}
113464115139
va_end(ap);
113465115140
#endif /* SQLITE_OMIT_BUILTIN_TEST */
113466115141
return rc;
113467115142
}
@@ -113476,16 +115151,55 @@
113476115151
** query parameter we seek. This routine returns the value of the zParam
113477115152
** parameter if it exists. If the parameter does not exist, this routine
113478115153
** returns a NULL pointer.
113479115154
*/
113480115155
SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
115156
+ if( zFilename==0 ) return 0;
113481115157
zFilename += sqlite3Strlen30(zFilename) + 1;
113482115158
while( zFilename[0] ){
113483115159
int x = strcmp(zFilename, zParam);
113484115160
zFilename += sqlite3Strlen30(zFilename) + 1;
113485115161
if( x==0 ) return zFilename;
113486115162
zFilename += sqlite3Strlen30(zFilename) + 1;
115163
+ }
115164
+ return 0;
115165
+}
115166
+
115167
+/*
115168
+** Return a boolean value for a query parameter.
115169
+*/
115170
+SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
115171
+ const char *z = sqlite3_uri_parameter(zFilename, zParam);
115172
+ return z ? sqlite3GetBoolean(z) : (bDflt!=0);
115173
+}
115174
+
115175
+/*
115176
+** Return a 64-bit integer value for a query parameter.
115177
+*/
115178
+SQLITE_API sqlite3_int64 sqlite3_uri_int64(
115179
+ const char *zFilename, /* Filename as passed to xOpen */
115180
+ const char *zParam, /* URI parameter sought */
115181
+ sqlite3_int64 bDflt /* return if parameter is missing */
115182
+){
115183
+ const char *z = sqlite3_uri_parameter(zFilename, zParam);
115184
+ sqlite3_int64 v;
115185
+ if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
115186
+ bDflt = v;
115187
+ }
115188
+ return bDflt;
115189
+}
115190
+
115191
+/*
115192
+** Return the filename of the database associated with a database
115193
+** connection.
115194
+*/
115195
+SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
115196
+ int i;
115197
+ for(i=0; i<db->nDb; i++){
115198
+ if( db->aDb[i].pBt && sqlite3StrICmp(zDbName, db->aDb[i].zName)==0 ){
115199
+ return sqlite3BtreeGetFilename(db->aDb[i].pBt);
115200
+ }
113487115201
}
113488115202
return 0;
113489115203
}
113490115204
113491115205
/************** End of main.c ************************************************/
@@ -115329,10 +117043,11 @@
115329117043
if( *pRc==SQLITE_OK ){
115330117044
va_list ap;
115331117045
char *z;
115332117046
va_start(ap, zFormat);
115333117047
z = sqlite3_vmprintf(zFormat, ap);
117048
+ va_end(ap);
115334117049
if( z && *pz ){
115335117050
char *z2 = sqlite3_mprintf("%s%s", *pz, z);
115336117051
sqlite3_free(z);
115337117052
z = z2;
115338117053
}
@@ -121596,11 +123311,11 @@
121596123311
typedef struct porter_tokenizer {
121597123312
sqlite3_tokenizer base; /* Base class */
121598123313
} porter_tokenizer;
121599123314
121600123315
/*
121601
-** Class derived from sqlit3_tokenizer_cursor
123316
+** Class derived from sqlite3_tokenizer_cursor
121602123317
*/
121603123318
typedef struct porter_tokenizer_cursor {
121604123319
sqlite3_tokenizer_cursor base;
121605123320
const char *zInput; /* input we are tokenizing */
121606123321
int nInput; /* size of the input */
@@ -124314,11 +126029,10 @@
124314126029
sqlite3_int64 iEndBlock, /* Final block of segment */
124315126030
const char *zRoot, /* Buffer containing root node */
124316126031
int nRoot, /* Size of buffer containing root node */
124317126032
Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
124318126033
){
124319
- int rc = SQLITE_OK; /* Return code */
124320126034
Fts3SegReader *pReader; /* Newly allocated SegReader object */
124321126035
int nExtra = 0; /* Bytes to allocate segment root node */
124322126036
124323126037
assert( iStartLeaf<=iEndLeaf );
124324126038
if( iStartLeaf==0 ){
@@ -124342,17 +126056,12 @@
124342126056
memcpy(pReader->aNode, zRoot, nRoot);
124343126057
memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
124344126058
}else{
124345126059
pReader->iCurrentBlock = iStartLeaf-1;
124346126060
}
124347
-
124348
- if( rc==SQLITE_OK ){
124349
- *ppReader = pReader;
124350
- }else{
124351
- sqlite3Fts3SegReaderFree(pReader);
124352
- }
124353
- return rc;
126061
+ *ppReader = pReader;
126062
+ return SQLITE_OK;
124354126063
}
124355126064
124356126065
/*
124357126066
** This is a comparison function used as a qsort() callback when sorting
124358126067
** an array of pending terms by term. This occurs as part of flushing
@@ -124398,19 +126107,19 @@
124398126107
int nTerm, /* Size of buffer zTerm */
124399126108
int bPrefix, /* True for a prefix iterator */
124400126109
Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
124401126110
){
124402126111
Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
126112
+ Fts3HashElem *pE; /* Iterator variable */
124403126113
Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
124404126114
int nElem = 0; /* Size of array at aElem */
124405126115
int rc = SQLITE_OK; /* Return Code */
124406126116
Fts3Hash *pHash;
124407126117
124408126118
pHash = &p->aIndex[iIndex].hPending;
124409126119
if( bPrefix ){
124410126120
int nAlloc = 0; /* Size of allocated array at aElem */
124411
- Fts3HashElem *pE = 0; /* Iterator variable */
124412126121
124413126122
for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
124414126123
char *zKey = (char *)fts3HashKey(pE);
124415126124
int nKey = fts3HashKeysize(pE);
124416126125
if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
@@ -124440,12 +126149,17 @@
124440126149
qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
124441126150
}
124442126151
124443126152
}else{
124444126153
/* The query is a simple term lookup that matches at most one term in
124445
- ** the index. All that is required is a straight hash-lookup. */
124446
- Fts3HashElem *pE = fts3HashFindElem(pHash, zTerm, nTerm);
126154
+ ** the index. All that is required is a straight hash-lookup.
126155
+ **
126156
+ ** Because the stack address of pE may be accessed via the aElem pointer
126157
+ ** below, the "Fts3HashElem *pE" must be declared so that it is valid
126158
+ ** within this entire function, not just this "else{...}" block.
126159
+ */
126160
+ pE = fts3HashFindElem(pHash, zTerm, nTerm);
124447126161
if( pE ){
124448126162
aElem = &pE;
124449126163
nElem = 1;
124450126164
}
124451126165
}
@@ -129021,11 +130735,11 @@
129021130735
RtreeMatchArg *p;
129022130736
sqlite3_rtree_geometry *pGeom;
129023130737
int nBlob;
129024130738
129025130739
/* Check that value is actually a blob. */
129026
- if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR;
130740
+ if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
129027130741
129028130742
/* Check that the blob is roughly the right size. */
129029130743
nBlob = sqlite3_value_bytes(pValue);
129030130744
if( nBlob<(int)sizeof(RtreeMatchArg)
129031130745
|| ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
129032130746
--- 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.9. 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.
@@ -363,10 +363,18 @@
363 #else
364 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
365 #endif
366 #endif
367
 
 
 
 
 
 
 
 
368 /*
369 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
370 ** It determines whether or not the features related to
371 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
372 ** be overridden at runtime using the sqlite3_config() API.
@@ -647,13 +655,13 @@
647 **
648 ** See also: [sqlite3_libversion()],
649 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
650 ** [sqlite_version()] and [sqlite_source_id()].
651 */
652 #define SQLITE_VERSION "3.7.9"
653 #define SQLITE_VERSION_NUMBER 3007009
654 #define SQLITE_SOURCE_ID "2011-10-29 19:25:08 5b82ec6fbbd2f4195ad06dd911de3817373ad5bf"
655
656 /*
657 ** CAPI3REF: Run-Time Library Version Numbers
658 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
659 **
@@ -717,11 +725,11 @@
717
718 /*
719 ** CAPI3REF: Test To See If The Library Is Threadsafe
720 **
721 ** ^The sqlite3_threadsafe() function returns zero if and only if
722 ** SQLite was compiled mutexing code omitted due to the
723 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
724 **
725 ** SQLite can be compiled with or without mutexes. When
726 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
727 ** are enabled and SQLite is threadsafe. When the
@@ -911,11 +919,11 @@
911 ** CAPI3REF: Result Codes
912 ** KEYWORDS: SQLITE_OK {error code} {error codes}
913 ** KEYWORDS: {result code} {result codes}
914 **
915 ** Many SQLite functions return an integer result code from the set shown
916 ** here in order to indicates success or failure.
917 **
918 ** New error codes may be added in future versions of SQLite.
919 **
920 ** See also: [SQLITE_IOERR_READ | extended result codes],
921 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -1049,11 +1057,15 @@
1049 ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1050 ** that when data is appended to a file, the data is appended
1051 ** first then the size of the file is extended, never the other
1052 ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1053 ** information is written to disk in the same order as calls
1054 ** to xWrite().
 
 
 
 
1055 */
1056 #define SQLITE_IOCAP_ATOMIC 0x00000001
1057 #define SQLITE_IOCAP_ATOMIC512 0x00000002
1058 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
1059 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -1063,10 +1075,11 @@
1063 #define SQLITE_IOCAP_ATOMIC32K 0x00000080
1064 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
1065 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
1066 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
1067 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
 
1068
1069 /*
1070 ** CAPI3REF: File Locking Levels
1071 **
1072 ** SQLite uses one of these integer values as the second
@@ -1284,16 +1297,16 @@
1284 ** opcode as doing so may disrupt the operation of the specialized VFSes
1285 ** that do require it.
1286 **
1287 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1288 ** retry counts and intervals for certain disk I/O operations for the
1289 ** windows [VFS] in order to work to provide robustness against
1290 ** anti-virus programs. By default, the windows VFS will retry file read,
1291 ** file write, and file delete opertions up to 10 times, with a delay
1292 ** of 25 milliseconds before the first retry and with the delay increasing
1293 ** by an additional 25 milliseconds with each subsequent retry. This
1294 ** opcode allows those to values (10 retries and 25 milliseconds of delay)
1295 ** to be adjusted. The values are changed for all database connections
1296 ** within the same process. The argument is a pointer to an array of two
1297 ** integers where the first integer i the new retry count and the second
1298 ** integer is the delay. If either integer is negative, then the setting
1299 ** is not changed but instead the prior value of that setting is written
@@ -1311,27 +1324,49 @@
1311 ** in order for the database to be readable. The fourth parameter to
1312 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1313 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1314 ** WAL mode. If the integer is -1, then it is overwritten with the current
1315 ** WAL persistence setting.
 
 
 
 
 
 
 
 
 
1316 **
1317 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1318 ** a write transaction to indicate that, unless it is rolled back for some
1319 ** reason, the entire database file will be overwritten by the current
1320 ** transaction. This is used by VACUUM operations.
 
 
 
 
 
 
 
 
 
 
 
1321 */
1322 #define SQLITE_FCNTL_LOCKSTATE 1
1323 #define SQLITE_GET_LOCKPROXYFILE 2
1324 #define SQLITE_SET_LOCKPROXYFILE 3
1325 #define SQLITE_LAST_ERRNO 4
1326 #define SQLITE_FCNTL_SIZE_HINT 5
1327 #define SQLITE_FCNTL_CHUNK_SIZE 6
1328 #define SQLITE_FCNTL_FILE_POINTER 7
1329 #define SQLITE_FCNTL_SYNC_OMITTED 8
1330 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
1331 #define SQLITE_FCNTL_PERSIST_WAL 10
1332 #define SQLITE_FCNTL_OVERWRITE 11
 
 
1333
1334 /*
1335 ** CAPI3REF: Mutex Handle
1336 **
1337 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1382,11 +1417,11 @@
1382 ** ^SQLite guarantees that the zFilename parameter to xOpen
1383 ** is either a NULL pointer or string obtained
1384 ** from xFullPathname() with an optional suffix added.
1385 ** ^If a suffix is added to the zFilename parameter, it will
1386 ** consist of a single "-" character followed by no more than
1387 ** 10 alphanumeric and/or "-" characters.
1388 ** ^SQLite further guarantees that
1389 ** the string will be valid and unchanged until xClose() is
1390 ** called. Because of the previous sentence,
1391 ** the [sqlite3_file] can safely store a pointer to the
1392 ** filename if it needs to remember the filename for some reason.
@@ -1913,11 +1948,11 @@
1913 **
1914 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1915 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1916 ** the database page cache with the default page cache implementation.
1917 ** This configuration should not be used if an application-define page
1918 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1919 ** There are three arguments to this option: A pointer to 8-byte aligned
1920 ** memory, the size of each page buffer (sz), and the number of pages (N).
1921 ** The sz argument should be the size of the largest database page
1922 ** (a power of two between 512 and 32768) plus a little extra for each
1923 ** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1982,19 +2017,19 @@
1982 ** slots allocated to each database connection.)^ ^(This option sets the
1983 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1984 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1985 ** configuration on individual connections.)^ </dd>
1986 **
1987 ** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
1988 ** <dd> ^(This option takes a single argument which is a pointer to
1989 ** an [sqlite3_pcache_methods] object. This object specifies the interface
1990 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1991 ** object and uses it for page cache memory allocations.</dd>
1992 **
1993 ** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
1994 ** <dd> ^(This option takes a single argument which is a pointer to an
1995 ** [sqlite3_pcache_methods] object. SQLite copies of the current
1996 ** page cache implementation into that object.)^ </dd>
1997 **
1998 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1999 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2000 ** function with a call signature of void(*)(void*,int,const char*),
@@ -2023,10 +2058,15 @@
2023 ** connection is opened. If it is globally disabled, filenames are
2024 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2025 ** database connection is opened. By default, URI handling is globally
2026 ** disabled. The default value may be changed by compiling with the
2027 ** [SQLITE_USE_URI] symbol defined.
 
 
 
 
 
2028 ** </dl>
2029 */
2030 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2031 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2032 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2038,14 +2078,16 @@
2038 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
2039 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
2040 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
2041 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2042 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2043 #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
2044 #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
2045 #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
2046 #define SQLITE_CONFIG_URI 17 /* int */
 
 
2047
2048 /*
2049 ** CAPI3REF: Database Connection Configuration Options
2050 **
2051 ** These constants are the available integer configuration options that
@@ -2526,11 +2568,11 @@
2526 ** These routines all implement some additional formatting
2527 ** options that are useful for constructing SQL statements.
2528 ** All of the usual printf() formatting options apply. In addition, there
2529 ** is are "%q", "%Q", and "%z" options.
2530 **
2531 ** ^(The %q option works like %s in that it substitutes a null-terminated
2532 ** string from the argument list. But %q also doubles every '\'' character.
2533 ** %q is designed for use inside a string literal.)^ By doubling each '\''
2534 ** character it escapes that character and allows it to be inserted into
2535 ** the string.
2536 **
@@ -3134,25 +3176,41 @@
3134 );
3135
3136 /*
3137 ** CAPI3REF: Obtain Values For URI Parameters
3138 **
3139 ** This is a utility routine, useful to VFS implementations, that checks
3140 ** to see if a database file was a URI that contained a specific query
3141 ** parameter, and if so obtains the value of the query parameter.
3142 **
3143 ** The zFilename argument is the filename pointer passed into the xOpen()
3144 ** method of a VFS implementation. The zParam argument is the name of the
3145 ** query parameter we seek. This routine returns the value of the zParam
3146 ** parameter if it exists. If the parameter does not exist, this routine
3147 ** returns a NULL pointer.
3148 **
3149 ** If the zFilename argument to this function is not a pointer that SQLite
3150 ** passed into the xOpen VFS method, then the behavior of this routine
3151 ** is undefined and probably undesirable.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3152 */
3153 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
 
 
3154
3155
3156 /*
3157 ** CAPI3REF: Error Codes And Messages
3158 **
@@ -3470,10 +3528,29 @@
3470 ** change the configuration of a database connection, they do not make
3471 ** changes to the content of the database files on disk.
3472 */
3473 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3474
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3475 /*
3476 ** CAPI3REF: Dynamically Typed Value Object
3477 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3478 **
3479 ** SQLite uses the sqlite3_value object to represent all values
@@ -4011,11 +4088,11 @@
4011 ** of the string. ^For clarity: the values returned by
4012 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
4013 ** bytes in the string, not the number of characters.
4014 **
4015 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4016 ** even empty strings, are always zero terminated. ^The return
4017 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4018 **
4019 ** ^The object returned by [sqlite3_column_value()] is an
4020 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
4021 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4911,10 +4988,26 @@
4911 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4912 ** create the statement in the first place.
4913 */
4914 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4915
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4916 /*
4917 ** CAPI3REF: Find the next prepared statement
4918 **
4919 ** ^This interface returns a pointer to the next [prepared statement] after
4920 ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4946,17 +5039,19 @@
4946 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4947 ** return the P argument from the previous call of the same function
4948 ** on the same [database connection] D, or NULL for
4949 ** the first call for each function on D.
4950 **
 
4951 ** The callback implementation must not do anything that will modify
4952 ** the database connection that invoked the callback. Any actions
4953 ** to modify the database connection must be deferred until after the
4954 ** completion of the [sqlite3_step()] call that triggered the commit
4955 ** or rollback hook in the first place.
4956 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4957 ** database connections for the meaning of "modify" in this paragraph.
 
4958 **
4959 ** ^Registering a NULL function disables the callback.
4960 **
4961 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4962 ** operation is allowed to continue normally. ^If the commit hook
@@ -5065,13 +5160,28 @@
5065 ** pages to improve performance is an example of non-essential memory.
5066 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
5067 ** which might be more or less than the amount requested.
5068 ** ^The sqlite3_release_memory() routine is a no-op returning zero
5069 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
 
 
5070 */
5071 SQLITE_API int sqlite3_release_memory(int);
5072
 
 
 
 
 
 
 
 
 
 
 
 
 
5073 /*
5074 ** CAPI3REF: Impose A Limit On Heap Size
5075 **
5076 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5077 ** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -5082,11 +5192,12 @@
5082 ** below the limit, it will exceed the limit rather than generate
5083 ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
5084 ** is advisory only.
5085 **
5086 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
5087 ** the soft heap limit prior to the call. ^If the argument N is negative
 
5088 ** then no change is made to the soft heap limit. Hence, the current
5089 ** size of the soft heap limit can be determined by invoking
5090 ** sqlite3_soft_heap_limit64() with a negative argument.
5091 **
5092 ** ^If the argument N is zero then the soft heap limit is disabled.
@@ -5098,11 +5209,11 @@
5098 ** <li> The soft heap limit is set to zero.
5099 ** <li> Memory accounting is disabled using a combination of the
5100 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5101 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5102 ** <li> An alternative page cache implementation is specified using
5103 ** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
5104 ** <li> The page cache allocates from its own memory pool supplied
5105 ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5106 ** from the heap.
5107 ** </ul>)^
5108 **
@@ -5840,19 +5951,19 @@
5840 ** is selected automatically at compile-time. ^(The following
5841 ** implementations are available in the SQLite core:
5842 **
5843 ** <ul>
5844 ** <li> SQLITE_MUTEX_OS2
5845 ** <li> SQLITE_MUTEX_PTHREAD
5846 ** <li> SQLITE_MUTEX_W32
5847 ** <li> SQLITE_MUTEX_NOOP
5848 ** </ul>)^
5849 **
5850 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5851 ** that does no real locking and is appropriate for use in
5852 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5853 ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5854 ** are appropriate for use on OS/2, Unix, and Windows.
5855 **
5856 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5857 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5858 ** implementation is included with the library. In this case the
@@ -6038,11 +6149,11 @@
6038 ** defined and if NDEBUG is not defined.
6039 **
6040 ** ^These routines should return true if the mutex in their argument
6041 ** is held or not held, respectively, by the calling thread.
6042 **
6043 ** ^The implementation is not required to provided versions of these
6044 ** routines that actually work. If the implementation does not provide working
6045 ** versions of these routines, it should at least provide stubs that always
6046 ** return true so that one does not get spurious assertion failures.
6047 **
6048 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -6166,13 +6277,13 @@
6166 #define SQLITE_TESTCTRL_ASSERT 12
6167 #define SQLITE_TESTCTRL_ALWAYS 13
6168 #define SQLITE_TESTCTRL_RESERVE 14
6169 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6170 #define SQLITE_TESTCTRL_ISKEYWORD 16
6171 #define SQLITE_TESTCTRL_PGHDRSZ 17
6172 #define SQLITE_TESTCTRL_SCRATCHMALLOC 18
6173 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 19
6174 #define SQLITE_TESTCTRL_LAST 19
6175
6176 /*
6177 ** CAPI3REF: SQLite Runtime Status
6178 **
@@ -6471,21 +6582,37 @@
6471 ** the pluggable module. The SQLite core has no knowledge of
6472 ** its size or internal structure and never deals with the
6473 ** sqlite3_pcache object except by holding and passing pointers
6474 ** to the object.
6475 **
6476 ** See [sqlite3_pcache_methods] for additional information.
6477 */
6478 typedef struct sqlite3_pcache sqlite3_pcache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6479
6480 /*
6481 ** CAPI3REF: Application Defined Page Cache.
6482 ** KEYWORDS: {page cache}
6483 **
6484 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6485 ** register an alternative page cache implementation by passing in an
6486 ** instance of the sqlite3_pcache_methods structure.)^
6487 ** In many applications, most of the heap memory allocated by
6488 ** SQLite is used for the page cache.
6489 ** By implementing a
6490 ** custom page cache using this API, an application can better control
6491 ** the amount of memory consumed by SQLite, the way in which
@@ -6495,20 +6622,20 @@
6495 **
6496 ** The alternative page cache mechanism is an
6497 ** extreme measure that is only needed by the most demanding applications.
6498 ** The built-in page cache is recommended for most uses.
6499 **
6500 ** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6501 ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
6502 ** the application may discard the parameter after the call to
6503 ** [sqlite3_config()] returns.)^
6504 **
6505 ** [[the xInit() page cache method]]
6506 ** ^(The xInit() method is called once for each effective
6507 ** call to [sqlite3_initialize()])^
6508 ** (usually only once during the lifetime of the process). ^(The xInit()
6509 ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6510 ** The intent of the xInit() method is to set up global data structures
6511 ** required by the custom page cache implementation.
6512 ** ^(If the xInit() method is NULL, then the
6513 ** built-in default page cache is used instead of the application defined
6514 ** page cache.)^
@@ -6531,21 +6658,19 @@
6531 ** [[the xCreate() page cache methods]]
6532 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6533 ** SQLite will typically create one cache instance for each open database file,
6534 ** though this is not guaranteed. ^The
6535 ** first parameter, szPage, is the size in bytes of the pages that must
6536 ** be allocated by the cache. ^szPage will not be a power of two. ^szPage
6537 ** will the page size of the database file that is to be cached plus an
6538 ** increment (here called "R") of less than 250. SQLite will use the
6539 ** extra R bytes on each page to store metadata about the underlying
6540 ** database page on disk. The value of R depends
 
6541 ** on the SQLite version, the target platform, and how SQLite was compiled.
6542 ** ^(R is constant for a particular build of SQLite. Except, there are two
6543 ** distinct values of R when SQLite is compiled with the proprietary
6544 ** ZIPVFS extension.)^ ^The second argument to
6545 ** xCreate(), bPurgeable, is true if the cache being created will
6546 ** be used to cache database pages of a file stored on disk, or
6547 ** false if it is used for an in-memory database. The cache implementation
6548 ** does not have to do anything special based with the value of bPurgeable;
6549 ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
6550 ** never invoke xUnpin() except to deliberately delete a page.
6551 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6565,15 +6690,20 @@
6565 ** The xPagecount() method must return the number of pages currently
6566 ** stored in the cache, both pinned and unpinned.
6567 **
6568 ** [[the xFetch() page cache methods]]
6569 ** The xFetch() method locates a page in the cache and returns a pointer to
6570 ** the page, or a NULL pointer.
6571 ** A "page", in this context, means a buffer of szPage bytes aligned at an
6572 ** 8-byte boundary. The page to be fetched is determined by the key. ^The
6573 ** minimum key value is 1. After it has been retrieved using xFetch, the page
6574 ** is considered to be "pinned".
 
 
 
 
 
6575 **
6576 ** If the requested page is already in the page cache, then the page cache
6577 ** implementation must return a pointer to the page buffer with its content
6578 ** intact. If the requested page is not already in the cache, then the
6579 ** cache implementation should use the value of the createFlag
@@ -6622,12 +6752,41 @@
6622 **
6623 ** [[the xDestroy() page cache method]]
6624 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6625 ** All resources associated with the specified cache should be freed. ^After
6626 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6627 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
6628 ** functions.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6629 */
6630 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6631 struct sqlite3_pcache_methods {
6632 void *pArg;
6633 int (*xInit)(void*);
@@ -6639,10 +6798,11 @@
6639 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6640 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6641 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6642 void (*xDestroy)(sqlite3_pcache*);
6643 };
 
6644
6645 /*
6646 ** CAPI3REF: Online Backup Object
6647 **
6648 ** The sqlite3_backup object records state information about an ongoing
@@ -7643,11 +7803,11 @@
7643 ** the default file format for new databases and the maximum file format
7644 ** that the library can read.
7645 */
7646 #define SQLITE_MAX_FILE_FORMAT 4
7647 #ifndef SQLITE_DEFAULT_FILE_FORMAT
7648 # define SQLITE_DEFAULT_FILE_FORMAT 1
7649 #endif
7650
7651 /*
7652 ** Determine whether triggers are recursive by default. This can be
7653 ** changed at run-time using a pragma.
@@ -8283,10 +8443,11 @@
8283 struct SubProgram {
8284 VdbeOp *aOp; /* Array of opcodes for sub-program */
8285 int nOp; /* Elements in aOp[] */
8286 int nMem; /* Number of memory cells required */
8287 int nCsr; /* Number of cursors required */
 
8288 void *token; /* id that may be used to recursive triggers */
8289 SubProgram *pNext; /* Next sub-program already visited */
8290 };
8291
8292 /*
@@ -8529,14 +8690,14 @@
8529 #define OPFLG_IN2 0x0008 /* in2: P2 is an input */
8530 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
8531 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
8532 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
8533 #define OPFLG_INITIALIZER {\
8534 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8535 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8536 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8537 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00,\
8538 /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
8539 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
8540 /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
8541 /* 56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8542 /* 64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
@@ -8732,10 +8893,11 @@
8732 /* Functions used to configure a Pager object. */
8733 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8734 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
8735 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8736 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 
8737 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
8738 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
8739 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
8740 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
8741 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
@@ -8840,11 +9002,12 @@
8840 /*
8841 ** Every page in the cache is controlled by an instance of the following
8842 ** structure.
8843 */
8844 struct PgHdr {
8845 void *pData; /* Content of this page */
 
8846 void *pExtra; /* Extra content */
8847 PgHdr *pDirty; /* Transient list of dirty pages */
8848 Pgno pgno; /* Page number for this page */
8849 Pager *pPager; /* The pager this page is part of */
8850 #ifdef SQLITE_CHECK_PAGES
@@ -8958,10 +9121,13 @@
8958 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
8959 #ifdef SQLITE_TEST
8960 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
8961 #endif
8962
 
 
 
8963 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
8964 /* Try to return memory used by the pcache module to the main memory heap */
8965 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
8966 #endif
8967
@@ -9042,21 +9208,10 @@
9042 #else
9043 # ifndef SQLITE_OS_WIN
9044 # define SQLITE_OS_WIN 0
9045 # endif
9046 #endif
9047
9048 /*
9049 ** Determine if we are dealing with WindowsCE - which has a much
9050 ** reduced API.
9051 */
9052 #if defined(_WIN32_WCE)
9053 # define SQLITE_OS_WINCE 1
9054 #else
9055 # define SQLITE_OS_WINCE 0
9056 #endif
9057
9058
9059 /*
9060 ** Define the maximum size of a temporary filename
9061 */
9062 #if SQLITE_OS_WIN
@@ -9078,10 +9233,29 @@
9078 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
9079 #else
9080 # define SQLITE_TEMPNAME_SIZE 200
9081 #endif
9082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9083 /* If the SET_FULLSYNC macro is not defined above, then make it
9084 ** a no-op
9085 */
9086 #ifndef SET_FULLSYNC
9087 # define SET_FULLSYNC(x,y)
@@ -9089,11 +9263,11 @@
9089
9090 /*
9091 ** The default size of a disk sector
9092 */
9093 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
9094 # define SQLITE_DEFAULT_SECTOR_SIZE 512
9095 #endif
9096
9097 /*
9098 ** Temporary files are named starting with this prefix followed by 16 random
9099 ** alphanumeric characters, and no file extension. They are stored in the
@@ -9222,17 +9396,19 @@
9222 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
9223 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
9224 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
9225 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
9226 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
 
9227 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
9228 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
9229 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9230 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9231 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9232 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9233 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
 
9234
9235 /*
9236 ** Functions for accessing sqlite3_vfs methods
9237 */
9238 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -9820,24 +9996,15 @@
9820 ** collating sequence may not be read or written.
9821 */
9822 struct CollSeq {
9823 char *zName; /* Name of the collating sequence, UTF-8 encoded */
9824 u8 enc; /* Text encoding handled by xCmp() */
9825 u8 type; /* One of the SQLITE_COLL_... values below */
9826 void *pUser; /* First argument to xCmp() */
9827 int (*xCmp)(void*,int, const void*, int, const void*);
9828 void (*xDel)(void*); /* Destructor for pUser */
9829 };
9830
9831 /*
9832 ** Allowed values of CollSeq.type:
9833 */
9834 #define SQLITE_COLL_BINARY 1 /* The default memcmp() collating sequence */
9835 #define SQLITE_COLL_NOCASE 2 /* The built-in NOCASE collating sequence */
9836 #define SQLITE_COLL_REVERSE 3 /* The built-in REVERSE collating sequence */
9837 #define SQLITE_COLL_USER 0 /* Any other user-defined collating sequence */
9838
9839 /*
9840 ** A sort order can be either ASC or DESC.
9841 */
9842 #define SQLITE_SO_ASC 0 /* Sort in ascending order */
9843 #define SQLITE_SO_DESC 1 /* Sort in ascending order */
@@ -10119,24 +10286,21 @@
10119 ** into its constituent fields.
10120 */
10121 struct UnpackedRecord {
10122 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10123 u16 nField; /* Number of entries in apMem[] */
10124 u16 flags; /* Boolean settings. UNPACKED_... below */
10125 i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
10126 Mem *aMem; /* Values */
10127 };
10128
10129 /*
10130 ** Allowed values of UnpackedRecord.flags
10131 */
10132 #define UNPACKED_NEED_FREE 0x0001 /* Memory is from sqlite3Malloc() */
10133 #define UNPACKED_NEED_DESTROY 0x0002 /* apMem[]s should all be destroyed */
10134 #define UNPACKED_IGNORE_ROWID 0x0004 /* Ignore trailing rowid on key1 */
10135 #define UNPACKED_INCRKEY 0x0008 /* Make this key an epsilon larger */
10136 #define UNPACKED_PREFIX_MATCH 0x0010 /* A prefix match is considered OK */
10137 #define UNPACKED_PREFIX_SEARCH 0x0020 /* A prefix match is considered OK */
10138
10139 /*
10140 ** Each SQL index is represented in memory by an
10141 ** instance of the following structure.
10142 **
@@ -10395,14 +10559,14 @@
10395 #define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
10396 #define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
10397 #define EP_FixedDest 0x0200 /* Result needed in a specific register */
10398 #define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
10399 #define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
10400
10401 #define EP_Reduced 0x1000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10402 #define EP_TokenOnly 0x2000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10403 #define EP_Static 0x4000 /* Held in memory not obtained from malloc() */
10404
10405 /*
10406 ** The following are the meanings of bits in the Expr.flags2 field.
10407 */
10408 #define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
@@ -10460,11 +10624,11 @@
10460 Expr *pExpr; /* The list of expressions */
10461 char *zName; /* Token associated with this expression */
10462 char *zSpan; /* Original text of the expression */
10463 u8 sortOrder; /* 1 for DESC or 0 for ASC */
10464 u8 done; /* A flag to indicate when processing is finished */
10465 u16 iCol; /* For ORDER BY, column number in result set */
10466 u16 iAlias; /* Index into Parse.aAlias[] for zName */
10467 } *a; /* One entry for each expression */
10468 };
10469
10470 /*
@@ -10760,17 +10924,17 @@
10760
10761 /*
10762 ** Allowed values for Select.selFlags. The "SF" prefix stands for
10763 ** "Select Flag".
10764 */
10765 #define SF_Distinct 0x0001 /* Output should be DISTINCT */
10766 #define SF_Resolved 0x0002 /* Identifiers have been resolved */
10767 #define SF_Aggregate 0x0004 /* Contains aggregate functions */
10768 #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
10769 #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
10770 #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
10771 #define SF_UseSorter 0x0040 /* Sort using a sorter */
10772
10773
10774 /*
10775 ** The results of a select can be distributed in several ways. The
10776 ** "SRT" prefix means "SELECT Result Type".
@@ -10881,28 +11045,27 @@
10881 sqlite3 *db; /* The main database structure */
10882 int rc; /* Return code from execution */
10883 char *zErrMsg; /* An error message */
10884 Vdbe *pVdbe; /* An engine for executing database bytecode */
10885 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
10886 u8 nameClash; /* A permanent table name clashes with temp table name */
10887 u8 checkSchema; /* Causes schema cookie check after an error */
10888 u8 nested; /* Number of nested calls to the parser/code generator */
10889 u8 parseError; /* True after a parsing error. Ticket #1794 */
10890 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
10891 u8 nTempInUse; /* Number of aTempReg[] currently checked out */
10892 int aTempReg[8]; /* Holding area for temporary registers */
10893 int nRangeReg; /* Size of the temporary register block */
10894 int iRangeReg; /* First register in temporary register block */
10895 int nErr; /* Number of errors seen */
10896 int nTab; /* Number of previously allocated VDBE cursors */
10897 int nMem; /* Number of memory cells used so far */
10898 int nSet; /* Number of sets used so far */
 
10899 int ckBase; /* Base register of data during check constraints */
10900 int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
10901 int iCacheCnt; /* Counter used to generate aColCache[].lru values */
10902 u8 nColCache; /* Number of entries in the column cache */
10903 u8 iColCache; /* Next entry of the cache to replace */
10904 struct yColCache {
10905 int iTable; /* Table cursor number */
10906 int iColumn; /* Table column number */
10907 u8 tempReg; /* iReg is a temp register that needs to be freed */
10908 int iLevel; /* Nesting level */
@@ -10940,11 +11103,10 @@
10940 int nVar; /* Number of '?' variables seen in the SQL so far */
10941 int nzVar; /* Number of available slots in azVar[] */
10942 char **azVar; /* Pointers to names of parameters */
10943 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
10944 int nAlias; /* Number of aliased result set columns */
10945 int nAliasAlloc; /* Number of allocated slots for aAlias[] */
10946 int *aAlias; /* Register used to hold aliased result */
10947 u8 explain; /* True if the EXPLAIN flag is found on the query */
10948 Token sNameToken; /* Token with unqualified schema object name */
10949 Token sLastToken; /* The last token parsed */
10950 const char *zTail; /* All SQL text past the last semicolon parsed */
@@ -11135,11 +11297,11 @@
11135 int mxStrlen; /* Maximum string length */
11136 int szLookaside; /* Default lookaside buffer size */
11137 int nLookaside; /* Default lookaside buffer count */
11138 sqlite3_mem_methods m; /* Low-level memory allocation interface */
11139 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
11140 sqlite3_pcache_methods pcache; /* Low-level page-cache interface */
11141 void *pHeap; /* Heap storage space */
11142 int nHeap; /* Size of pHeap[] */
11143 int mnReq, mxReq; /* Min and max heap requests sizes */
11144 void *pScratch; /* Scratch memory */
11145 int szScratch; /* Size of each scratch buffer */
@@ -11341,10 +11503,33 @@
11341 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
11342 #endif
11343 #if defined(SQLITE_TEST)
11344 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
11345 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11346 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
11347 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
11348 SQLITE_PRIVATE int sqlite3Dequote(char*);
11349 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
11350 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
@@ -11351,10 +11536,11 @@
11351 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
11352 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
11353 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
11354 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
11355 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
 
11356 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
11357 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
11358 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
11359 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
11360 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
@@ -11382,10 +11568,11 @@
11382 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
11383 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
11384 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
11385 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
11386 sqlite3_vfs**,char**,char **);
 
11387
11388 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
11389 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
11390 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
11391 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -11720,10 +11907,11 @@
11720 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
11721 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
11722
11723 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
11724 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
 
11725 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
11726 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
11727 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
11728 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
11729
@@ -12099,11 +12287,11 @@
12099 0x7ffffffe, /* mxStrlen */
12100 128, /* szLookaside */
12101 500, /* nLookaside */
12102 {0,0,0,0,0,0,0,0}, /* m */
12103 {0,0,0,0,0,0,0,0,0}, /* mutex */
12104 {0,0,0,0,0,0,0,0,0,0,0}, /* pcache */
12105 (void*)0, /* pHeap */
12106 0, /* nHeap */
12107 0, 0, /* mnHeap, mxHeap */
12108 (void*)0, /* pScratch */
12109 0, /* szScratch */
@@ -12626,10 +12814,13 @@
12626 typedef unsigned char Bool;
12627
12628 /* Opaque type used by code in vdbesort.c */
12629 typedef struct VdbeSorter VdbeSorter;
12630
 
 
 
12631 /*
12632 ** A cursor is a pointer into a single BTree within a database file.
12633 ** The cursor can seek to a BTree entry with a particular key, or
12634 ** loop over all entries of the Btree. You can also insert new BTree
12635 ** entries or retrieve the key or data from the entry that the cursor
@@ -12710,10 +12901,12 @@
12710 int pc; /* Program Counter in parent (calling) frame */
12711 Op *aOp; /* Program instructions for parent frame */
12712 int nOp; /* Size of aOp array */
12713 Mem *aMem; /* Array of memory cells for parent frame */
12714 int nMem; /* Number of entries in aMem */
 
 
12715 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
12716 u16 nCursor; /* Number of entries in apCsr */
12717 void *token; /* Copy of SubProgram.token */
12718 int nChildMem; /* Number of memory cells for child frame */
12719 int nChildCsr; /* Number of cursors for child frame */
@@ -12848,10 +13041,22 @@
12848 Mem *pMem; /* Memory cell used to store aggregate context */
12849 int isError; /* Error code returned by the function. */
12850 CollSeq *pColl; /* Collating sequence */
12851 };
12852
 
 
 
 
 
 
 
 
 
 
 
 
12853 /*
12854 ** An instance of the virtual machine. This structure contains the complete
12855 ** state of the virtual machine.
12856 **
12857 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -12914,15 +13119,21 @@
12914 char *zSql; /* Text of the SQL statement that generated this */
12915 void *pFree; /* Free this when deleting the vdbe */
12916 #ifdef SQLITE_DEBUG
12917 FILE *trace; /* Write an execution trace here, if not NULL */
12918 #endif
 
 
 
 
12919 VdbeFrame *pFrame; /* Parent frame */
12920 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
12921 int nFrame; /* Number of frames in pFrame list */
12922 u32 expmask; /* Binding to these vars invalidates VM */
12923 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
 
 
12924 };
12925
12926 /*
12927 ** The following are allowed values for Vdbe.magic
12928 */
@@ -12978,11 +13189,11 @@
12978 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12979 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12980 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12981 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12982 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12983 #define MemReleaseExt(X) \
12984 if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12985 sqlite3VdbeMemReleaseExternal(X);
12986 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12987 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12988 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
@@ -13017,11 +13228,11 @@
13017 # define sqlite3VdbeEnter(X)
13018 # define sqlite3VdbeLeave(X)
13019 #endif
13020
13021 #ifdef SQLITE_DEBUG
13022 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
13023 #endif
13024
13025 #ifndef SQLITE_OMIT_FOREIGN_KEY
13026 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
13027 #else
@@ -13035,12 +13246,14 @@
13035 #endif
13036 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
13037
13038 #ifndef SQLITE_OMIT_INCRBLOB
13039 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
 
13040 #else
13041 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
 
13042 #endif
13043
13044 #endif /* !defined(_VDBEINT_H_) */
13045
13046 /************** End of vdbeInt.h *********************************************/
@@ -14436,15 +14649,22 @@
14436 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
14437 **
14438 ** The following functions are instrumented for malloc() failure
14439 ** testing:
14440 **
14441 ** sqlite3OsOpen()
14442 ** sqlite3OsRead()
14443 ** sqlite3OsWrite()
14444 ** sqlite3OsSync()
 
14445 ** sqlite3OsLock()
 
 
 
 
 
 
 
14446 **
14447 */
14448 #if defined(SQLITE_TEST)
14449 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
14450 #define DO_OS_MALLOC_TEST(x) \
@@ -14499,13 +14719,27 @@
14499 }
14500 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
14501 DO_OS_MALLOC_TEST(id);
14502 return id->pMethods->xCheckReservedLock(id, pResOut);
14503 }
 
 
 
 
 
 
 
 
 
14504 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
 
14505 return id->pMethods->xFileControl(id, op, pArg);
14506 }
 
 
 
 
14507 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
14508 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
14509 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
14510 }
14511 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
@@ -14525,10 +14759,11 @@
14525 int iPage,
14526 int pgsz,
14527 int bExtend, /* True to extend file if necessary */
14528 void volatile **pp /* OUT: Pointer to mapping */
14529 ){
 
14530 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
14531 }
14532
14533 /*
14534 ** The next group of routines are convenience wrappers around the
@@ -14541,19 +14776,21 @@
14541 int flags,
14542 int *pFlagsOut
14543 ){
14544 int rc;
14545 DO_OS_MALLOC_TEST(0);
14546 /* 0x87f3f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14547 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
14548 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14549 ** reaching the VFS. */
14550 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
14551 assert( rc==SQLITE_OK || pFile->pMethods==0 );
14552 return rc;
14553 }
14554 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
 
 
14555 return pVfs->xDelete(pVfs, zPath, dirSync);
14556 }
14557 SQLITE_PRIVATE int sqlite3OsAccess(
14558 sqlite3_vfs *pVfs,
14559 const char *zPath,
@@ -14567,10 +14804,11 @@
14567 sqlite3_vfs *pVfs,
14568 const char *zPath,
14569 int nPathOut,
14570 char *zPathOut
14571 ){
 
14572 zPathOut[0] = 0;
14573 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
14574 }
14575 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14576 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
@@ -14918,31 +15156,81 @@
14918 ** used when no other memory allocator is specified using compile-time
14919 ** macros.
14920 */
14921 #ifdef SQLITE_SYSTEM_MALLOC
14922
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14923 /*
14924 ** Like malloc(), but remember the size of the allocation
14925 ** so that we can find it later using sqlite3MemSize().
14926 **
14927 ** For this low-level routine, we are guaranteed that nByte>0 because
14928 ** cases of nByte<=0 will be intercepted and dealt with by higher level
14929 ** routines.
14930 */
14931 static void *sqlite3MemMalloc(int nByte){
 
 
 
 
 
 
 
 
14932 sqlite3_int64 *p;
14933 assert( nByte>0 );
14934 nByte = ROUND8(nByte);
14935 p = malloc( nByte+8 );
14936 if( p ){
14937 p[0] = nByte;
14938 p++;
14939 }else{
14940 testcase( sqlite3GlobalConfig.xLog!=0 );
14941 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
14942 }
14943 return (void *)p;
 
14944 }
14945
14946 /*
14947 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
14948 ** or sqlite3MemRealloc().
@@ -14950,26 +15238,34 @@
14950 ** For this low-level routine, we already know that pPrior!=0 since
14951 ** cases where pPrior==0 will have been intecepted and dealt with
14952 ** by higher-level routines.
14953 */
14954 static void sqlite3MemFree(void *pPrior){
 
 
 
14955 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14956 assert( pPrior!=0 );
14957 p--;
14958 free(p);
 
14959 }
14960
14961 /*
14962 ** Report the allocated size of a prior return from xMalloc()
14963 ** or xRealloc().
14964 */
14965 static int sqlite3MemSize(void *pPrior){
 
 
 
14966 sqlite3_int64 *p;
14967 if( pPrior==0 ) return 0;
14968 p = (sqlite3_int64*)pPrior;
14969 p--;
14970 return (int)p[0];
 
14971 }
14972
14973 /*
14974 ** Like realloc(). Resize an allocation previously obtained from
14975 ** sqlite3MemMalloc().
@@ -14979,15 +15275,25 @@
14979 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses
14980 ** cases where nByte<=0 will have been intercepted by higher-level
14981 ** routines and redirected to xFree.
14982 */
14983 static void *sqlite3MemRealloc(void *pPrior, int nByte){
 
 
 
 
 
 
 
 
 
 
14984 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
14985 assert( pPrior!=0 && nByte>0 );
14986 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
14987 p--;
14988 p = realloc(p, nByte+8 );
14989 if( p ){
14990 p[0] = nByte;
14991 p++;
14992 }else{
14993 testcase( sqlite3GlobalConfig.xLog!=0 );
@@ -14994,10 +15300,11 @@
14994 sqlite3_log(SQLITE_NOMEM,
14995 "failed memory resize %u to %u bytes",
14996 sqlite3MemSize(pPrior), nByte);
14997 }
14998 return (void*)p;
 
14999 }
15000
15001 /*
15002 ** Round up a request size to the next valid allocation size.
15003 */
@@ -15007,10 +15314,38 @@
15007
15008 /*
15009 ** Initialize this module.
15010 */
15011 static int sqlite3MemInit(void *NotUsed){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15012 UNUSED_PARAMETER(NotUsed);
15013 return SQLITE_OK;
15014 }
15015
15016 /*
@@ -16996,11 +17331,11 @@
16996 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
16997 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
16998 }
16999 #endif
17000
17001 #endif /* SQLITE_MUTEX_OMIT */
17002
17003 /************** End of mutex.c ***********************************************/
17004 /************** Begin file mutex_noop.c **************************************/
17005 /*
17006 ** 2008 October 07
@@ -17203,12 +17538,12 @@
17203 */
17204 #ifdef SQLITE_MUTEX_NOOP
17205 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17206 return sqlite3NoopMutex();
17207 }
17208 #endif /* SQLITE_MUTEX_NOOP */
17209 #endif /* SQLITE_MUTEX_OMIT */
17210
17211 /************** End of mutex_noop.c ******************************************/
17212 /************** Begin file mutex_os2.c ***************************************/
17213 /*
17214 ** 2007 August 28
@@ -17833,11 +18168,11 @@
17833 };
17834
17835 return &sMutex;
17836 }
17837
17838 #endif /* SQLITE_MUTEX_PTHREAD */
17839
17840 /************** End of mutex_unix.c ******************************************/
17841 /************** Begin file mutex_w32.c ***************************************/
17842 /*
17843 ** 2007 August 14
@@ -18302,11 +18637,12 @@
18302 */
18303 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
18304 sqlite3_int64 priorLimit;
18305 sqlite3_int64 excess;
18306 #ifndef SQLITE_OMIT_AUTOINIT
18307 sqlite3_initialize();
 
18308 #endif
18309 sqlite3_mutex_enter(mem0.mutex);
18310 priorLimit = mem0.alarmThreshold;
18311 sqlite3_mutex_leave(mem0.mutex);
18312 if( n<0 ) return priorLimit;
@@ -19087,11 +19423,11 @@
19087 #endif /* SQLITE_OMIT_FLOATING_POINT */
19088
19089 /*
19090 ** Append N space characters to the given string buffer.
19091 */
19092 static void appendSpace(StrAccum *pAccum, int N){
19093 static const char zSpaces[] = " ";
19094 while( N>=(int)sizeof(zSpaces)-1 ){
19095 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
19096 N -= sizeof(zSpaces)-1;
19097 }
@@ -19615,21 +19951,21 @@
19615 */
19616 if( !flag_leftjustify ){
19617 register int nspace;
19618 nspace = width-length;
19619 if( nspace>0 ){
19620 appendSpace(pAccum, nspace);
19621 }
19622 }
19623 if( length>0 ){
19624 sqlite3StrAccumAppend(pAccum, bufpt, length);
19625 }
19626 if( flag_leftjustify ){
19627 register int nspace;
19628 nspace = width-length;
19629 if( nspace>0 ){
19630 appendSpace(pAccum, nspace);
19631 }
19632 }
19633 sqlite3_free(zExtra);
19634 }/* End for loop over the format string */
19635 } /* End of function */
@@ -21800,22 +22136,21 @@
21800 ** Examples:
21801 **
21802 ** test.db-journal => test.nal
21803 ** test.db-wal => test.wal
21804 ** test.db-shm => test.shm
 
21805 */
21806 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
21807 #if SQLITE_ENABLE_8_3_NAMES<2
21808 const char *zOk;
21809 zOk = sqlite3_uri_parameter(zBaseFilename, "8_3_names");
21810 if( zOk && sqlite3GetBoolean(zOk) )
21811 #endif
21812 {
21813 int i, sz;
21814 sz = sqlite3Strlen30(z);
21815 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
21816 if( z[i]=='.' && ALWAYS(sz>i+4) ) memcpy(&z[i+1], &z[sz-3], 4);
21817 }
21818 }
21819 #endif
21820
21821 /************** End of util.c ************************************************/
@@ -24516,10 +24851,11 @@
24516 #include <sys/time.h>
24517 #include <errno.h>
24518 #ifndef SQLITE_OMIT_WAL
24519 #include <sys/mman.h>
24520 #endif
 
24521
24522 #if SQLITE_ENABLE_LOCKING_STYLE
24523 # include <sys/ioctl.h>
24524 # if OS_VXWORKS
24525 # include <semaphore.h>
@@ -24600,10 +24936,11 @@
24600 ** VFS implementations.
24601 */
24602 typedef struct unixFile unixFile;
24603 struct unixFile {
24604 sqlite3_io_methods const *pMethod; /* Always the first entry */
 
24605 unixInodeInfo *pInode; /* Info about locks on this inode */
24606 int h; /* The file descriptor */
24607 unsigned char eFileLock; /* The type of lock held on this fd */
24608 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
24609 int lastErrno; /* The unix errno from last I/O error */
@@ -24617,11 +24954,10 @@
24617 #endif
24618 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
24619 unsigned fsFlags; /* cached details from statfs() */
24620 #endif
24621 #if OS_VXWORKS
24622 int isDelete; /* Delete on close if true */
24623 struct vxworksFileId *pId; /* Unique file ID */
24624 #endif
24625 #ifndef NDEBUG
24626 /* The next group of variables are used to track whether or not the
24627 ** transaction counter in bytes 24-27 of database files are updated
@@ -24651,10 +24987,14 @@
24651 #ifndef SQLITE_DISABLE_DIRSYNC
24652 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24653 #else
24654 # define UNIXFILE_DIRSYNC 0x00
24655 #endif
 
 
 
 
24656
24657 /*
24658 ** Include code that is common to all os_*.c files
24659 */
24660 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -25009,10 +25349,16 @@
25009 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
25010
25011 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
25012 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
25013
 
 
 
 
 
 
25014 }; /* End of the overrideable system calls */
25015
25016 /*
25017 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
25018 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26358,11 +26704,11 @@
26358 robust_close(pFile, pFile->h, __LINE__);
26359 pFile->h = -1;
26360 }
26361 #if OS_VXWORKS
26362 if( pFile->pId ){
26363 if( pFile->isDelete ){
26364 osUnlink(pFile->pId->zCanonicalName);
26365 }
26366 vxworksReleaseFileId(pFile->pId);
26367 pFile->pId = 0;
26368 }
@@ -26447,12 +26793,12 @@
26447
26448 /******************************************************************************
26449 ************************* Begin dot-file Locking ******************************
26450 **
26451 ** The dotfile locking implementation uses the existance of separate lock
26452 ** files in order to control access to the database. This works on just
26453 ** about every filesystem imaginable. But there are serious downsides:
26454 **
26455 ** (1) There is zero concurrency. A single reader blocks all other
26456 ** connections from reading or writing the database.
26457 **
26458 ** (2) An application crash or power loss can leave stale lock files
@@ -26459,19 +26805,19 @@
26459 ** sitting around that need to be cleared manually.
26460 **
26461 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
26462 ** other locking strategy is available.
26463 **
26464 ** Dotfile locking works by creating a file in the same directory as the
26465 ** database and with the same name but with a ".lock" extension added.
26466 ** The existance of a lock file implies an EXCLUSIVE lock. All other lock
26467 ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
26468 */
26469
26470 /*
26471 ** The file suffix added to the data base filename in order to create the
26472 ** lock file.
26473 */
26474 #define DOTLOCK_SUFFIX ".lock"
26475
26476 /*
26477 ** This routine checks if there is a RESERVED lock held on the specified
@@ -26534,11 +26880,10 @@
26534 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
26535 ** But we track the other locking levels internally.
26536 */
26537 static int dotlockLock(sqlite3_file *id, int eFileLock) {
26538 unixFile *pFile = (unixFile*)id;
26539 int fd;
26540 char *zLockFile = (char *)pFile->lockingContext;
26541 int rc = SQLITE_OK;
26542
26543
26544 /* If we have any lock, then the lock file already exists. All we have
@@ -26554,13 +26899,13 @@
26554 #endif
26555 return SQLITE_OK;
26556 }
26557
26558 /* grab an exclusive lock */
26559 fd = robust_open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600);
26560 if( fd<0 ){
26561 /* failed to open/create the file, someone else may have stolen the lock */
26562 int tErrno = errno;
26563 if( EEXIST == tErrno ){
26564 rc = SQLITE_BUSY;
26565 } else {
26566 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -26568,11 +26913,10 @@
26568 pFile->lastErrno = tErrno;
26569 }
26570 }
26571 return rc;
26572 }
26573 robust_close(pFile, fd, __LINE__);
26574
26575 /* got it, set the type and return ok */
26576 pFile->eFileLock = eFileLock;
26577 return rc;
26578 }
@@ -26587,10 +26931,11 @@
26587 ** When the locking level reaches NO_LOCK, delete the lock file.
26588 */
26589 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
26590 unixFile *pFile = (unixFile*)id;
26591 char *zLockFile = (char *)pFile->lockingContext;
 
26592
26593 assert( pFile );
26594 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
26595 pFile->eFileLock, getpid()));
26596 assert( eFileLock<=SHARED_LOCK );
@@ -26608,13 +26953,15 @@
26608 return SQLITE_OK;
26609 }
26610
26611 /* To fully unlock the database, delete the lock file */
26612 assert( eFileLock==NO_LOCK );
26613 if( osUnlink(zLockFile) ){
26614 int rc = 0;
 
26615 int tErrno = errno;
 
26616 if( ENOENT != tErrno ){
26617 rc = SQLITE_IOERR_UNLOCK;
26618 }
26619 if( IS_LOCK_ERROR(rc) ){
26620 pFile->lastErrno = tErrno;
@@ -27546,39 +27893,52 @@
27546 ** To avoid stomping the errno value on a failed read the lastErrno value
27547 ** is set before returning.
27548 */
27549 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
27550 int got;
 
27551 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27552 i64 newOffset;
27553 #endif
27554 TIMER_START;
 
27555 #if defined(USE_PREAD)
27556 do{ got = osPread(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27557 SimulateIOError( got = -1 );
27558 #elif defined(USE_PREAD64)
27559 do{ got = osPread64(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR);
27560 SimulateIOError( got = -1 );
27561 #else
27562 newOffset = lseek(id->h, offset, SEEK_SET);
27563 SimulateIOError( newOffset-- );
27564 if( newOffset!=offset ){
27565 if( newOffset == -1 ){
27566 ((unixFile*)id)->lastErrno = errno;
27567 }else{
27568 ((unixFile*)id)->lastErrno = 0;
27569 }
27570 return -1;
27571 }
27572 do{ got = osRead(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27573 #endif
27574 TIMER_END;
27575 if( got<0 ){
27576 ((unixFile*)id)->lastErrno = errno;
27577 }
27578 OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED));
27579 return got;
 
 
 
 
 
 
 
 
 
 
 
27580 }
27581
27582 /*
27583 ** Read data from a file into a buffer. Return SQLITE_OK if all
27584 ** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -28078,10 +28438,26 @@
28078 }
28079 }
28080
28081 return SQLITE_OK;
28082 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28083
28084 /*
28085 ** Information and control of an open file handle.
28086 */
28087 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -28105,18 +28481,19 @@
28105 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28106 SimulateIOErrorBenign(0);
28107 return rc;
28108 }
28109 case SQLITE_FCNTL_PERSIST_WAL: {
28110 int bPersist = *(int*)pArg;
28111 if( bPersist<0 ){
28112 *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
28113 }else if( bPersist==0 ){
28114 pFile->ctrlFlags &= ~UNIXFILE_PERSIST_WAL;
28115 }else{
28116 pFile->ctrlFlags |= UNIXFILE_PERSIST_WAL;
28117 }
 
28118 return SQLITE_OK;
28119 }
28120 #ifndef NDEBUG
28121 /* The pager calls this method to signal that it has done
28122 ** a rollback and that the database is therefore unchanged and
@@ -28132,13 +28509,10 @@
28132 case SQLITE_SET_LOCKPROXYFILE:
28133 case SQLITE_GET_LOCKPROXYFILE: {
28134 return proxyFileControl(id,op,pArg);
28135 }
28136 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
28137 case SQLITE_FCNTL_SYNC_OMITTED: {
28138 return SQLITE_OK; /* A no-op */
28139 }
28140 }
28141 return SQLITE_NOTFOUND;
28142 }
28143
28144 /*
@@ -28149,21 +28523,35 @@
28149 ** SQLite code assumes this function cannot fail. It also assumes that
28150 ** if two files are created in the same file-system directory (i.e.
28151 ** a database and its journal file) that the sector size will be the
28152 ** same for both.
28153 */
28154 static int unixSectorSize(sqlite3_file *NotUsed){
28155 UNUSED_PARAMETER(NotUsed);
28156 return SQLITE_DEFAULT_SECTOR_SIZE;
28157 }
28158
28159 /*
28160 ** Return the device characteristics for the file. This is always 0 for unix.
 
 
 
 
 
 
 
 
 
 
28161 */
28162 static int unixDeviceCharacteristics(sqlite3_file *NotUsed){
28163 UNUSED_PARAMETER(NotUsed);
28164 return 0;
 
 
 
 
28165 }
28166
28167 #ifndef SQLITE_OMIT_WAL
28168
28169
@@ -28414,20 +28802,20 @@
28414 rc = SQLITE_IOERR_FSTAT;
28415 goto shm_open_err;
28416 }
28417
28418 #ifdef SQLITE_SHM_DIRECTORY
28419 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30;
28420 #else
28421 nShmFilename = 5 + (int)strlen(pDbFd->zPath);
28422 #endif
28423 pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
28424 if( pShmNode==0 ){
28425 rc = SQLITE_NOMEM;
28426 goto shm_open_err;
28427 }
28428 memset(pShmNode, 0, sizeof(*pShmNode));
28429 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
28430 #ifdef SQLITE_SHM_DIRECTORY
28431 sqlite3_snprintf(nShmFilename, zShmFilename,
28432 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
28433 (u32)sStat.st_ino, (u32)sStat.st_dev);
@@ -28443,14 +28831,12 @@
28443 rc = SQLITE_NOMEM;
28444 goto shm_open_err;
28445 }
28446
28447 if( pInode->bProcessLock==0 ){
28448 const char *zRO;
28449 int openFlags = O_RDWR | O_CREAT;
28450 zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28451 if( zRO && sqlite3GetBoolean(zRO) ){
28452 openFlags = O_RDONLY;
28453 pShmNode->isReadonly = 1;
28454 }
28455 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
28456 if( pShmNode->h<0 ){
@@ -29108,28 +29494,20 @@
29108 ** Initialize the contents of the unixFile structure pointed to by pId.
29109 */
29110 static int fillInUnixFile(
29111 sqlite3_vfs *pVfs, /* Pointer to vfs object */
29112 int h, /* Open file descriptor of file being opened */
29113 int syncDir, /* True to sync directory on first sync */
29114 sqlite3_file *pId, /* Write to the unixFile structure here */
29115 const char *zFilename, /* Name of the file being opened */
29116 int noLock, /* Omit locking if true */
29117 int isDelete, /* Delete on close if true */
29118 int isReadOnly /* True if the file is opened read-only */
29119 ){
29120 const sqlite3_io_methods *pLockingStyle;
29121 unixFile *pNew = (unixFile *)pId;
29122 int rc = SQLITE_OK;
29123
29124 assert( pNew->pInode==NULL );
29125
29126 /* Parameter isDelete is only used on vxworks. Express this explicitly
29127 ** here to prevent compiler warnings about unused parameters.
29128 */
29129 UNUSED_PARAMETER(isDelete);
29130
29131 /* Usually the path zFilename should not be a relative pathname. The
29132 ** exception is when opening the proxy "conch" file in builds that
29133 ** include the special Apple locking styles.
29134 */
29135 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29138,36 +29516,34 @@
29138 #else
29139 assert( zFilename==0 || zFilename[0]=='/' );
29140 #endif
29141
29142 /* No locking occurs in temporary files */
29143 assert( zFilename!=0 || noLock );
29144
29145 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
29146 pNew->h = h;
 
29147 pNew->zPath = zFilename;
 
 
 
 
 
29148 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
29149 pNew->ctrlFlags = UNIXFILE_EXCL;
29150 }else{
29151 pNew->ctrlFlags = 0;
29152 }
29153 if( isReadOnly ){
29154 pNew->ctrlFlags |= UNIXFILE_RDONLY;
29155 }
29156 if( syncDir ){
29157 pNew->ctrlFlags |= UNIXFILE_DIRSYNC;
29158 }
29159
29160 #if OS_VXWORKS
29161 pNew->pId = vxworksFindFileId(zFilename);
29162 if( pNew->pId==0 ){
29163 noLock = 1;
29164 rc = SQLITE_NOMEM;
29165 }
29166 #endif
29167
29168 if( noLock ){
29169 pLockingStyle = &nolockIoMethods;
29170 }else{
29171 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
29172 #if SQLITE_ENABLE_LOCKING_STYLE
29173 /* Cache zFilename in the locking context (AFP and dotlock override) for
@@ -29284,11 +29660,11 @@
29284 if( h>=0 ) robust_close(pNew, h, __LINE__);
29285 h = -1;
29286 osUnlink(zFilename);
29287 isDelete = 0;
29288 }
29289 pNew->isDelete = isDelete;
29290 #endif
29291 if( rc!=SQLITE_OK ){
29292 if( h>=0 ) robust_close(pNew, h, __LINE__);
29293 }else{
29294 pNew->pMethod = pLockingStyle;
@@ -29349,22 +29725,23 @@
29349 if( zDir==0 ) zDir = ".";
29350
29351 /* Check that the output buffer is large enough for the temporary file
29352 ** name. If it is not, return SQLITE_ERROR.
29353 */
29354 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){
29355 return SQLITE_ERROR;
29356 }
29357
29358 do{
29359 sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29360 j = (int)strlen(zBuf);
29361 sqlite3_randomness(15, &zBuf[j]);
29362 for(i=0; i<15; i++, j++){
29363 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29364 }
29365 zBuf[j] = 0;
 
29366 }while( osAccess(zBuf,0)==0 );
29367 return SQLITE_OK;
29368 }
29369
29370 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
@@ -29481,11 +29858,11 @@
29481 ** where NN is a decimal number. The NN naming schemes are
29482 ** used by the test_multiplex.c module.
29483 */
29484 nDb = sqlite3Strlen30(zPath) - 1;
29485 #ifdef SQLITE_ENABLE_8_3_NAMES
29486 while( nDb>0 && !sqlite3Isalnum(zPath[nDb]) ) nDb--;
29487 if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
29488 #else
29489 while( zPath[nDb]!='-' ){
29490 assert( nDb>0 );
29491 assert( zPath[nDb]!='\n' );
@@ -29539,10 +29916,11 @@
29539 int fd = -1; /* File descriptor returned by open() */
29540 int openFlags = 0; /* Flags to pass to open() */
29541 int eType = flags&0xFFFFFF00; /* Type of file to open */
29542 int noLock; /* True to omit locking primitives */
29543 int rc = SQLITE_OK; /* Function Return Code */
 
29544
29545 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
29546 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
29547 int isCreate = (flags & SQLITE_OPEN_CREATE);
29548 int isReadonly = (flags & SQLITE_OPEN_READONLY);
@@ -29565,11 +29943,11 @@
29565 ));
29566
29567 /* If argument zPath is a NULL pointer, this function is required to open
29568 ** a temporary file. Use this buffer to store the file name in.
29569 */
29570 char zTmpname[MAX_PATHNAME+1];
29571 const char *zName = zPath;
29572
29573 /* Check the following statements are true:
29574 **
29575 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
@@ -29608,18 +29986,28 @@
29608 if( !pUnused ){
29609 return SQLITE_NOMEM;
29610 }
29611 }
29612 p->pUnused = pUnused;
 
 
 
 
 
 
29613 }else if( !zName ){
29614 /* If zName is NULL, the upper layer is requesting a temp file. */
29615 assert(isDelete && !syncDir);
29616 rc = unixGetTempname(MAX_PATHNAME+1, zTmpname);
29617 if( rc!=SQLITE_OK ){
29618 return rc;
29619 }
29620 zName = zTmpname;
 
 
 
 
29621 }
29622
29623 /* Determine the value of the flags parameter passed to POSIX function
29624 ** open(). These must be calculated even if open() is not called, as
29625 ** they may be stored as part of the file handle and used by the
@@ -29692,11 +30080,18 @@
29692 }
29693 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29694 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29695 }
29696 #endif
29697
 
 
 
 
 
 
 
29698 #if SQLITE_ENABLE_LOCKING_STYLE
29699 #if SQLITE_PREFER_PROXY_LOCKING
29700 isAutoProxy = 1;
29701 #endif
29702 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
@@ -29722,12 +30117,11 @@
29722 goto open_finished;
29723 }
29724 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29725 }
29726 if( useProxy ){
29727 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29728 isDelete, isReadonly);
29729 if( rc==SQLITE_OK ){
29730 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29731 if( rc!=SQLITE_OK ){
29732 /* Use unixClose to clean up the resources added in fillInUnixFile
29733 ** and clear all the structure's references. Specifically,
@@ -29740,12 +30134,12 @@
29740 goto open_finished;
29741 }
29742 }
29743 #endif
29744
29745 rc = fillInUnixFile(pVfs, fd, syncDir, pFile, zPath, noLock,
29746 isDelete, isReadonly);
29747 open_finished:
29748 if( rc!=SQLITE_OK ){
29749 sqlite3_free(p->pUnused);
29750 }
29751 return rc;
@@ -29766,11 +30160,11 @@
29766 SimulateIOError(return SQLITE_IOERR_DELETE);
29767 if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
29768 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29769 }
29770 #ifndef SQLITE_DISABLE_DIRSYNC
29771 if( dirSync ){
29772 int fd;
29773 rc = osOpenDirectory(zPath, &fd);
29774 if( rc==SQLITE_OK ){
29775 #if OS_VXWORKS
29776 if( fsync(fd)==-1 )
@@ -30317,11 +30711,11 @@
30317 if( lockPath[i] == '/' && (i - start > 0) ){
30318 /* only mkdir if leaf dir != "." or "/" or ".." */
30319 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30320 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30321 buf[i]='\0';
30322 if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30323 int err=errno;
30324 if( err!=EEXIST ) {
30325 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
30326 "'%s' proxy lock path=%s pid=%d\n",
30327 buf, strerror(err), lockPath, getpid()));
@@ -30412,11 +30806,11 @@
30412 dummyVfs.zName = "dummy";
30413 pUnused->fd = fd;
30414 pUnused->flags = openFlags;
30415 pNew->pUnused = pUnused;
30416
30417 rc = fillInUnixFile(&dummyVfs, fd, 0, (sqlite3_file*)pNew, path, 0, 0, 0);
30418 if( rc==SQLITE_OK ){
30419 *ppFile = pNew;
30420 return SQLITE_OK;
30421 }
30422 end_create_proxy:
@@ -31353,11 +31747,11 @@
31353 };
31354 unsigned int i; /* Loop counter */
31355
31356 /* Double-check that the aSyscall[] array has been constructed
31357 ** correctly. See ticket [bb3a86e890c8e96ab] */
31358 assert( ArraySize(aSyscall)==18 );
31359
31360 /* Register all VFSes defined in the aVfs[] array */
31361 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31362 sqlite3_vfs_register(&aVfs[i], i==0);
31363 }
@@ -31389,54 +31783,18 @@
31389 ** May you find forgiveness for yourself and forgive others.
31390 ** May you share freely, never taking more than you give.
31391 **
31392 ******************************************************************************
31393 **
31394 ** This file contains code that is specific to windows.
31395 */
31396 #if SQLITE_OS_WIN /* This file is used for windows only */
31397
31398
31399 /*
31400 ** A Note About Memory Allocation:
31401 **
31402 ** This driver uses malloc()/free() directly rather than going through
31403 ** the SQLite-wrappers sqlite3_malloc()/sqlite3_free(). Those wrappers
31404 ** are designed for use on embedded systems where memory is scarce and
31405 ** malloc failures happen frequently. Win32 does not typically run on
31406 ** embedded systems, and when it does the developers normally have bigger
31407 ** problems to worry about than running out of memory. So there is not
31408 ** a compelling need to use the wrappers.
31409 **
31410 ** But there is a good reason to not use the wrappers. If we use the
31411 ** wrappers then we will get simulated malloc() failures within this
31412 ** driver. And that causes all kinds of problems for our tests. We
31413 ** could enhance SQLite to deal with simulated malloc failures within
31414 ** the OS driver, but the code to deal with those failure would not
31415 ** be exercised on Linux (which does not need to malloc() in the driver)
31416 ** and so we would have difficulty writing coverage tests for that
31417 ** code. Better to leave the code out, we think.
31418 **
31419 ** The point of this discussion is as follows: When creating a new
31420 ** OS layer for an embedded system, if you use this file as an example,
31421 ** avoid the use of malloc()/free(). Those routines work ok on windows
31422 ** desktops but not so well in embedded systems.
31423 */
31424
31425 #include <winbase.h>
31426
31427 #ifdef __CYGWIN__
31428 # include <sys/cygwin.h>
31429 #endif
31430
31431 /*
31432 ** Macros used to determine whether or not to use threads.
31433 */
31434 #if defined(THREADSAFE) && THREADSAFE
31435 # define SQLITE_W32_THREADS 1
31436 #endif
31437
31438 /*
31439 ** Include code that is common to all os_*.c files
31440 */
31441 /************** Include os_common.h in the middle of os_win.c ****************/
31442 /************** Begin file os_common.h ***************************************/
@@ -31647,25 +32005,16 @@
31647
31648 /************** End of os_common.h *******************************************/
31649 /************** Continuing where we left off in os_win.c *********************/
31650
31651 /*
31652 ** Some microsoft compilers lack this definition.
31653 */
31654 #ifndef INVALID_FILE_ATTRIBUTES
31655 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31656 #endif
31657
31658 /*
31659 ** Determine if we are dealing with WindowsCE - which has a much
31660 ** reduced API.
31661 */
31662 #if SQLITE_OS_WINCE
31663 # define AreFileApisANSI() 1
31664 # define FormatMessageW(a,b,c,d,e,f,g) 0
31665 #endif
31666
31667 /* Forward references */
31668 typedef struct winShm winShm; /* A connection to shared-memory */
31669 typedef struct winShmNode winShmNode; /* A region of shared-memory */
31670
31671 /*
@@ -31690,25 +32039,30 @@
31690 const sqlite3_io_methods *pMethod; /*** Must be first ***/
31691 sqlite3_vfs *pVfs; /* The VFS used to open this file */
31692 HANDLE h; /* Handle for accessing the file */
31693 u8 locktype; /* Type of lock currently held on this file */
31694 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
31695 u8 bPersistWal; /* True to persist WAL files */
31696 DWORD lastErrno; /* The Windows errno from the last I/O error */
31697 DWORD sectorSize; /* Sector size of the device file is on */
31698 winShm *pShm; /* Instance of shared memory on this file */
31699 const char *zPath; /* Full pathname of this file */
31700 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
31701 #if SQLITE_OS_WINCE
31702 WCHAR *zDeleteOnClose; /* Name of file to delete when closing */
31703 HANDLE hMutex; /* Mutex used to control access to shared lock */
31704 HANDLE hShared; /* Shared memory segment used for locking */
31705 winceLock local; /* Locks obtained by this instance of winFile */
31706 winceLock *shared; /* Global shared lock memory for the file */
31707 #endif
31708 };
31709
 
 
 
 
 
 
31710 /*
31711 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31712 * various Win32 API heap functions instead of our own.
31713 */
31714 #ifdef SQLITE_WIN32_MALLOC
@@ -31776,25 +32130,17 @@
31776 static void winMemShutdown(void *pAppData);
31777
31778 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31779 #endif /* SQLITE_WIN32_MALLOC */
31780
31781 /*
31782 ** Forward prototypes.
31783 */
31784 static int getSectorSize(
31785 sqlite3_vfs *pVfs,
31786 const char *zRelative /* UTF-8 file name */
31787 );
31788
31789 /*
31790 ** The following variable is (normally) set once and never changes
31791 ** thereafter. It records whether the operating system is Win95
31792 ** or WinNT.
31793 **
31794 ** 0: Operating system unknown.
31795 ** 1: Operating system is Win95.
31796 ** 2: Operating system is WinNT.
31797 **
31798 ** In order to facilitate testing on a WinNT system, the test fixture
31799 ** can manually set this value to 1 to emulate Win98 behavior.
31800 */
@@ -31801,10 +32147,540 @@
31801 #ifdef SQLITE_TEST
31802 SQLITE_API int sqlite3_os_type = 0;
31803 #else
31804 static int sqlite3_os_type = 0;
31805 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31806
31807 /*
31808 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
31809 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
31810 **
@@ -31818,13 +32694,13 @@
31818 #if SQLITE_OS_WINCE
31819 # define isNT() (1)
31820 #else
31821 static int isNT(void){
31822 if( sqlite3_os_type==0 ){
31823 OSVERSIONINFO sInfo;
31824 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31825 GetVersionEx(&sInfo);
31826 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
31827 }
31828 return sqlite3_os_type==2;
31829 }
31830 #endif /* SQLITE_OS_WINCE */
@@ -31840,17 +32716,17 @@
31840 winMemAssertMagic();
31841 hHeap = winMemGetHeap();
31842 assert( hHeap!=0 );
31843 assert( hHeap!=INVALID_HANDLE_VALUE );
31844 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31845 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31846 #endif
31847 assert( nBytes>=0 );
31848 p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31849 if( !p ){
31850 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
31851 nBytes, GetLastError(), (void*)hHeap);
31852 }
31853 return p;
31854 }
31855
31856 /*
@@ -31862,16 +32738,16 @@
31862 winMemAssertMagic();
31863 hHeap = winMemGetHeap();
31864 assert( hHeap!=0 );
31865 assert( hHeap!=INVALID_HANDLE_VALUE );
31866 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31867 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31868 #endif
31869 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
31870 if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
31871 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
31872 pPrior, GetLastError(), (void*)hHeap);
31873 }
31874 }
31875
31876 /*
31877 ** Change the size of an existing memory allocation
@@ -31883,22 +32759,22 @@
31883 winMemAssertMagic();
31884 hHeap = winMemGetHeap();
31885 assert( hHeap!=0 );
31886 assert( hHeap!=INVALID_HANDLE_VALUE );
31887 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31888 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31889 #endif
31890 assert( nBytes>=0 );
31891 if( !pPrior ){
31892 p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31893 }else{
31894 p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
31895 }
31896 if( !p ){
31897 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
31898 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
31899 (void*)hHeap);
31900 }
31901 return p;
31902 }
31903
31904 /*
@@ -31911,17 +32787,17 @@
31911 winMemAssertMagic();
31912 hHeap = winMemGetHeap();
31913 assert( hHeap!=0 );
31914 assert( hHeap!=INVALID_HANDLE_VALUE );
31915 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31916 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31917 #endif
31918 if( !p ) return 0;
31919 n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
31920 if( n==(SIZE_T)-1 ){
31921 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
31922 p, GetLastError(), (void*)hHeap);
31923 return 0;
31924 }
31925 return (int)n;
31926 }
31927
@@ -31939,26 +32815,26 @@
31939 winMemData *pWinMemData = (winMemData *)pAppData;
31940
31941 if( !pWinMemData ) return SQLITE_ERROR;
31942 assert( pWinMemData->magic==WINMEM_MAGIC );
31943 if( !pWinMemData->hHeap ){
31944 pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS,
31945 SQLITE_WIN32_HEAP_INIT_SIZE,
31946 SQLITE_WIN32_HEAP_MAX_SIZE);
31947 if( !pWinMemData->hHeap ){
31948 sqlite3_log(SQLITE_NOMEM,
31949 "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
31950 GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE,
31951 SQLITE_WIN32_HEAP_MAX_SIZE);
31952 return SQLITE_NOMEM;
31953 }
31954 pWinMemData->bOwned = TRUE;
31955 }
31956 assert( pWinMemData->hHeap!=0 );
31957 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31958 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31959 assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31960 #endif
31961 return SQLITE_OK;
31962 }
31963
31964 /*
@@ -31969,16 +32845,16 @@
31969
31970 if( !pWinMemData ) return;
31971 if( pWinMemData->hHeap ){
31972 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31973 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31974 assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31975 #endif
31976 if( pWinMemData->bOwned ){
31977 if( !HeapDestroy(pWinMemData->hHeap) ){
31978 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
31979 GetLastError(), (void*)pWinMemData->hHeap);
31980 }
31981 pWinMemData->bOwned = FALSE;
31982 }
31983 pWinMemData->hHeap = NULL;
31984 }
@@ -32010,197 +32886,219 @@
32010 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
32011 }
32012 #endif /* SQLITE_WIN32_MALLOC */
32013
32014 /*
32015 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
32016 **
32017 ** Space to hold the returned string is obtained from malloc.
32018 */
32019 static WCHAR *utf8ToUnicode(const char *zFilename){
32020 int nChar;
32021 WCHAR *zWideFilename;
32022
32023 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32024 zWideFilename = malloc( nChar*sizeof(zWideFilename[0]) );
 
 
 
32025 if( zWideFilename==0 ){
32026 return 0;
32027 }
32028 nChar = MultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename, nChar);
 
32029 if( nChar==0 ){
32030 free(zWideFilename);
32031 zWideFilename = 0;
32032 }
32033 return zWideFilename;
32034 }
32035
32036 /*
32037 ** Convert microsoft unicode to UTF-8. Space to hold the returned string is
32038 ** obtained from malloc().
32039 */
32040 static char *unicodeToUtf8(const WCHAR *zWideFilename){
32041 int nByte;
32042 char *zFilename;
32043
32044 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32045 zFilename = malloc( nByte );
 
 
 
32046 if( zFilename==0 ){
32047 return 0;
32048 }
32049 nByte = WideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32050 0, 0);
32051 if( nByte == 0 ){
32052 free(zFilename);
32053 zFilename = 0;
32054 }
32055 return zFilename;
32056 }
32057
32058 /*
32059 ** Convert an ansi string to microsoft unicode, based on the
32060 ** current codepage settings for file apis.
32061 **
32062 ** Space to hold the returned string is obtained
32063 ** from malloc.
32064 */
32065 static WCHAR *mbcsToUnicode(const char *zFilename){
32066 int nByte;
32067 WCHAR *zMbcsFilename;
32068 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32069
32070 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, NULL,0)*sizeof(WCHAR);
32071 zMbcsFilename = malloc( nByte*sizeof(zMbcsFilename[0]) );
 
 
 
 
32072 if( zMbcsFilename==0 ){
32073 return 0;
32074 }
32075 nByte = MultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename, nByte);
 
32076 if( nByte==0 ){
32077 free(zMbcsFilename);
32078 zMbcsFilename = 0;
32079 }
32080 return zMbcsFilename;
32081 }
32082
32083 /*
32084 ** Convert microsoft unicode to multibyte character string, based on the
32085 ** user's Ansi codepage.
32086 **
32087 ** Space to hold the returned string is obtained from
32088 ** malloc().
32089 */
32090 static char *unicodeToMbcs(const WCHAR *zWideFilename){
32091 int nByte;
32092 char *zFilename;
32093 int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
32094
32095 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32096 zFilename = malloc( nByte );
 
 
 
32097 if( zFilename==0 ){
32098 return 0;
32099 }
32100 nByte = WideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename, nByte,
32101 0, 0);
32102 if( nByte == 0 ){
32103 free(zFilename);
32104 zFilename = 0;
32105 }
32106 return zFilename;
32107 }
32108
32109 /*
32110 ** Convert multibyte character string to UTF-8. Space to hold the
32111 ** returned string is obtained from malloc().
32112 */
32113 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32114 char *zFilenameUtf8;
32115 WCHAR *zTmpWide;
32116
32117 zTmpWide = mbcsToUnicode(zFilename);
32118 if( zTmpWide==0 ){
32119 return 0;
32120 }
32121 zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32122 free(zTmpWide);
32123 return zFilenameUtf8;
32124 }
32125
32126 /*
32127 ** Convert UTF-8 to multibyte character string. Space to hold the
32128 ** returned string is obtained from malloc().
32129 */
32130 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32131 char *zFilenameMbcs;
32132 WCHAR *zTmpWide;
32133
32134 zTmpWide = utf8ToUnicode(zFilename);
32135 if( zTmpWide==0 ){
32136 return 0;
32137 }
32138 zFilenameMbcs = unicodeToMbcs(zTmpWide);
32139 free(zTmpWide);
32140 return zFilenameMbcs;
32141 }
32142
32143
32144 /*
32145 ** The return value of getLastErrorMsg
32146 ** is zero if the error message fits in the buffer, or non-zero
32147 ** otherwise (if the message was truncated).
32148 */
32149 static int getLastErrorMsg(int nBuf, char *zBuf){
32150 /* FormatMessage returns 0 on failure. Otherwise it
32151 ** returns the number of TCHARs written to the output
32152 ** buffer, excluding the terminating null char.
32153 */
32154 DWORD error = GetLastError();
32155 DWORD dwLen = 0;
32156 char *zOut = 0;
32157
32158 if( isNT() ){
32159 WCHAR *zTempWide = NULL;
32160 dwLen = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32161 NULL,
32162 error,
32163 0,
32164 (LPWSTR) &zTempWide,
32165 0,
32166 0);
 
 
32167 if( dwLen > 0 ){
32168 /* allocate a buffer and convert to UTF8 */
 
32169 zOut = unicodeToUtf8(zTempWide);
 
32170 /* free the system buffer allocated by FormatMessage */
32171 LocalFree(zTempWide);
32172 }
32173 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32174 ** Since the ASCII version of these Windows API do not exist for WINCE,
32175 ** it's important to not reference them for WINCE builds.
32176 */
32177 #if SQLITE_OS_WINCE==0
32178 }else{
32179 char *zTemp = NULL;
32180 dwLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
32181 NULL,
32182 error,
32183 0,
32184 (LPSTR) &zTemp,
32185 0,
32186 0);
 
 
32187 if( dwLen > 0 ){
32188 /* allocate a buffer and convert to UTF8 */
 
32189 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
 
32190 /* free the system buffer allocated by FormatMessage */
32191 LocalFree(zTemp);
32192 }
32193 #endif
32194 }
32195 if( 0 == dwLen ){
32196 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", error, error);
32197 }else{
32198 /* copy a maximum of nBuf chars to output buffer */
32199 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
32200 /* free the UTF8 buffer */
32201 free(zOut);
32202 }
32203 return 0;
32204 }
32205
32206 /*
@@ -32216,30 +33114,30 @@
32216 ** The first argument passed to the macro should be the error code that
32217 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
32218 ** The two subsequent arguments should be the name of the OS function that
32219 ** failed and the the associated file-system path, if any.
32220 */
32221 #define winLogError(a,b,c) winLogErrorAtLine(a,b,c,__LINE__)
32222 static int winLogErrorAtLine(
32223 int errcode, /* SQLite error code */
 
32224 const char *zFunc, /* Name of OS function that failed */
32225 const char *zPath, /* File path associated with error */
32226 int iLine /* Source line number where error occurred */
32227 ){
32228 char zMsg[500]; /* Human readable error text */
32229 int i; /* Loop counter */
32230 DWORD iErrno = GetLastError(); /* Error code */
32231
32232 zMsg[0] = 0;
32233 getLastErrorMsg(sizeof(zMsg), zMsg);
32234 assert( errcode!=SQLITE_OK );
32235 if( zPath==0 ) zPath = "";
32236 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32237 zMsg[i] = 0;
32238 sqlite3_log(errcode,
32239 "os_win.c:%d: (%d) %s(%s) - %s",
32240 iLine, iErrno, zFunc, zPath, zMsg
32241 );
32242
32243 return errcode;
32244 }
32245
@@ -32261,23 +33159,28 @@
32261 /*
32262 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
32263 ** to see if it should be retried. Return TRUE to retry. Return FALSE
32264 ** to give up with an error.
32265 */
32266 static int retryIoerr(int *pnRetry){
32267 DWORD e;
32268 if( *pnRetry>=win32IoerrRetry ){
 
 
 
32269 return 0;
32270 }
32271 e = GetLastError();
32272 if( e==ERROR_ACCESS_DENIED ||
32273 e==ERROR_LOCK_VIOLATION ||
32274 e==ERROR_SHARING_VIOLATION ){
32275 Sleep(win32IoerrRetryDelay*(1+*pnRetry));
32276 ++*pnRetry;
32277 return 1;
32278 }
 
 
 
32279 return 0;
32280 }
32281
32282 /*
32283 ** Log a I/O error retry episode.
@@ -32294,11 +33197,11 @@
32294 #if SQLITE_OS_WINCE
32295 /*************************************************************************
32296 ** This section contains code for WinCE only.
32297 */
32298 /*
32299 ** WindowsCE does not have a localtime() function. So create a
32300 ** substitute.
32301 */
32302 /* #include <time.h> */
32303 struct tm *__cdecl localtime(const time_t *t)
32304 {
@@ -32308,12 +33211,12 @@
32308 sqlite3_int64 t64;
32309 t64 = *t;
32310 t64 = (t64 + 11644473600)*10000000;
32311 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
32312 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
32313 FileTimeToLocalFileTime(&uTm,&lTm);
32314 FileTimeToSystemTime(&lTm,&pTm);
32315 y.tm_year = pTm.wYear - 1900;
32316 y.tm_mon = pTm.wMonth - 1;
32317 y.tm_wday = pTm.wDayOfWeek;
32318 y.tm_mday = pTm.wDay;
32319 y.tm_hour = pTm.wHour;
@@ -32320,17 +33223,10 @@
32320 y.tm_min = pTm.wMinute;
32321 y.tm_sec = pTm.wSecond;
32322 return &y;
32323 }
32324
32325 /* This will never be called, but defined to make the code compile */
32326 #define GetTempPathA(a,b)
32327
32328 #define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
32329 #define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
32330 #define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
32331
32332 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
32333
32334 /*
32335 ** Acquire a lock on the handle h
32336 */
@@ -32348,30 +33244,36 @@
32348 /*
32349 ** Create the mutex and shared memory used for locking in the file
32350 ** descriptor pFile
32351 */
32352 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
32353 WCHAR *zTok;
32354 WCHAR *zName = utf8ToUnicode(zFilename);
32355 BOOL bInit = TRUE;
 
 
 
 
 
 
32356
32357 /* Initialize the local lockdata */
32358 ZeroMemory(&pFile->local, sizeof(pFile->local));
32359
32360 /* Replace the backslashes from the filename and lowercase it
32361 ** to derive a mutex name. */
32362 zTok = CharLowerW(zName);
32363 for (;*zTok;zTok++){
32364 if (*zTok == '\\') *zTok = '_';
32365 }
32366
32367 /* Create/open the named mutex */
32368 pFile->hMutex = CreateMutexW(NULL, FALSE, zName);
32369 if (!pFile->hMutex){
32370 pFile->lastErrno = GetLastError();
32371 winLogError(SQLITE_ERROR, "winceCreateLock1", zFilename);
32372 free(zName);
32373 return FALSE;
32374 }
32375
32376 /* Acquire the mutex before continuing */
32377 winceMutexAcquire(pFile->hMutex);
@@ -32378,47 +33280,48 @@
32378
32379 /* Since the names of named mutexes, semaphores, file mappings etc are
32380 ** case-sensitive, take advantage of that by uppercasing the mutex name
32381 ** and using that as the shared filemapping name.
32382 */
32383 CharUpperW(zName);
32384 pFile->hShared = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
32385 PAGE_READWRITE, 0, sizeof(winceLock),
32386 zName);
32387
32388 /* Set a flag that indicates we're the first to create the memory so it
32389 ** must be zero-initialized */
32390 if (GetLastError() == ERROR_ALREADY_EXISTS){
32391 bInit = FALSE;
32392 }
32393
32394 free(zName);
32395
32396 /* If we succeeded in making the shared memory handle, map it. */
32397 if (pFile->hShared){
32398 pFile->shared = (winceLock*)MapViewOfFile(pFile->hShared,
32399 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
32400 /* If mapping failed, close the shared memory handle and erase it */
32401 if (!pFile->shared){
32402 pFile->lastErrno = GetLastError();
32403 winLogError(SQLITE_ERROR, "winceCreateLock2", zFilename);
32404 CloseHandle(pFile->hShared);
 
32405 pFile->hShared = NULL;
32406 }
32407 }
32408
32409 /* If shared memory could not be created, then close the mutex and fail */
32410 if (pFile->hShared == NULL){
32411 winceMutexRelease(pFile->hMutex);
32412 CloseHandle(pFile->hMutex);
32413 pFile->hMutex = NULL;
32414 return FALSE;
32415 }
32416
32417 /* Initialize the shared memory if we're supposed to */
32418 if (bInit) {
32419 ZeroMemory(pFile->shared, sizeof(winceLock));
32420 }
32421
32422 winceMutexRelease(pFile->hMutex);
32423 return TRUE;
32424 }
@@ -32445,22 +33348,22 @@
32445 if (pFile->local.bExclusive){
32446 pFile->shared->bExclusive = FALSE;
32447 }
32448
32449 /* De-reference and close our copy of the shared memory handle */
32450 UnmapViewOfFile(pFile->shared);
32451 CloseHandle(pFile->hShared);
32452
32453 /* Done with the mutex */
32454 winceMutexRelease(pFile->hMutex);
32455 CloseHandle(pFile->hMutex);
32456 pFile->hMutex = NULL;
32457 }
32458 }
32459
32460 /*
32461 ** An implementation of the LockFile() API of windows for wince
32462 */
32463 static BOOL winceLockFile(
32464 HANDLE *phFile,
32465 DWORD dwFileOffsetLow,
32466 DWORD dwFileOffsetHigh,
@@ -32520,11 +33423,11 @@
32520 winceMutexRelease(pFile->hMutex);
32521 return bReturn;
32522 }
32523
32524 /*
32525 ** An implementation of the UnlockFile API of windows for wince
32526 */
32527 static BOOL winceUnlockFile(
32528 HANDLE *phFile,
32529 DWORD dwFileOffsetLow,
32530 DWORD dwFileOffsetHigh,
@@ -32582,11 +33485,11 @@
32582 winceMutexRelease(pFile->hMutex);
32583 return bReturn;
32584 }
32585
32586 /*
32587 ** An implementation of the LockFileEx() API of windows for wince
32588 */
32589 static BOOL winceLockFileEx(
32590 HANDLE *phFile,
32591 DWORD dwFlags,
32592 DWORD dwReserved,
@@ -32615,11 +33518,11 @@
32615 ** The next group of routines implement the I/O methods specified
32616 ** by the sqlite3_io_methods object.
32617 ******************************************************************************/
32618
32619 /*
32620 ** Some microsoft compilers lack this definition.
32621 */
32622 #ifndef INVALID_SET_FILE_POINTER
32623 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
32624 #endif
32625
@@ -32630,10 +33533,11 @@
32630 */
32631 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32632 LONG upperBits; /* Most sig. 32 bits of new offset */
32633 LONG lowerBits; /* Least sig. 32 bits of new offset */
32634 DWORD dwRet; /* Value returned by SetFilePointer() */
 
32635
32636 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
32637 lowerBits = (LONG)(iOffset & 0xffffffff);
32638
32639 /* API oddity: If successful, SetFilePointer() returns a dword
@@ -32641,14 +33545,17 @@
32641 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
32642 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
32643 ** whether an error has actually occured, it is also necessary to call
32644 ** GetLastError().
32645 */
32646 dwRet = SetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
32647 if( (dwRet==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR) ){
32648 pFile->lastErrno = GetLastError();
32649 winLogError(SQLITE_IOERR_SEEK, "seekWinFile", pFile->zPath);
 
 
 
32650 return 1;
32651 }
32652
32653 return 0;
32654 }
@@ -32655,11 +33562,11 @@
32655
32656 /*
32657 ** Close a file.
32658 **
32659 ** It is reported that an attempt to close a handle might sometimes
32660 ** fail. This is a very unreasonable result, but windows is notorious
32661 ** for being unreasonable so I do not doubt that it might happen. If
32662 ** the close fails, we pause for 100 milliseconds and try again. As
32663 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
32664 ** giving up and returning an error.
32665 */
@@ -32670,32 +33577,33 @@
32670
32671 assert( id!=0 );
32672 assert( pFile->pShm==0 );
32673 OSTRACE(("CLOSE %d\n", pFile->h));
32674 do{
32675 rc = CloseHandle(pFile->h);
32676 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32677 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
32678 #if SQLITE_OS_WINCE
32679 #define WINCE_DELETION_ATTEMPTS 3
32680 winceDestroyLock(pFile);
32681 if( pFile->zDeleteOnClose ){
32682 int cnt = 0;
32683 while(
32684 DeleteFileW(pFile->zDeleteOnClose)==0
32685 && GetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
32686 && cnt++ < WINCE_DELETION_ATTEMPTS
32687 ){
32688 Sleep(100); /* Wait a little before trying again */
32689 }
32690 free(pFile->zDeleteOnClose);
32691 }
32692 #endif
32693 OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
32694 OpenCounter(-1);
32695 return rc ? SQLITE_OK
32696 : winLogError(SQLITE_IOERR_CLOSE, "winClose", pFile->zPath);
 
32697 }
32698
32699 /*
32700 ** Read data from a file into a buffer. Return SQLITE_OK if all
32701 ** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -32716,14 +33624,16 @@
32716 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
32717
32718 if( seekWinFile(pFile, offset) ){
32719 return SQLITE_FULL;
32720 }
32721 while( !ReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32722 if( retryIoerr(&nRetry) ) continue;
32723 pFile->lastErrno = GetLastError();
32724 return winLogError(SQLITE_IOERR_READ, "winRead", pFile->zPath);
 
 
32725 }
32726 logIoerr(nRetry);
32727 if( nRead<(DWORD)amt ){
32728 /* Unread parts of the buffer must be zero-filled */
32729 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
@@ -32757,32 +33667,34 @@
32757 rc = seekWinFile(pFile, offset);
32758 if( rc==0 ){
32759 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
32760 int nRem = amt; /* Number of bytes yet to be written */
32761 DWORD nWrite; /* Bytes written by each WriteFile() call */
 
32762
32763 while( nRem>0 ){
32764 if( !WriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
32765 if( retryIoerr(&nRetry) ) continue;
32766 break;
32767 }
32768 if( nWrite<=0 ) break;
32769 aRem += nWrite;
32770 nRem -= nWrite;
32771 }
32772 if( nRem>0 ){
32773 pFile->lastErrno = GetLastError();
32774 rc = 1;
32775 }
32776 }
32777
32778 if( rc ){
32779 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
32780 || ( pFile->lastErrno==ERROR_DISK_FULL )){
32781 return SQLITE_FULL;
32782 }
32783 return winLogError(SQLITE_IOERR_WRITE, "winWrite", pFile->zPath);
 
32784 }else{
32785 logIoerr(nRetry);
32786 }
32787 return SQLITE_OK;
32788 }
@@ -32808,14 +33720,16 @@
32808 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
32809 }
32810
32811 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32812 if( seekWinFile(pFile, nByte) ){
32813 rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate1", pFile->zPath);
32814 }else if( 0==SetEndOfFile(pFile->h) ){
32815 pFile->lastErrno = GetLastError();
32816 rc = winLogError(SQLITE_IOERR_TRUNCATE, "winTruncate2", pFile->zPath);
 
 
32817 }
32818
32819 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
32820 return rc;
32821 }
@@ -32876,17 +33790,18 @@
32876 ** no-op
32877 */
32878 #ifdef SQLITE_NO_SYNC
32879 return SQLITE_OK;
32880 #else
32881 rc = FlushFileBuffers(pFile->h);
32882 SimulateIOError( rc=FALSE );
32883 if( rc ){
32884 return SQLITE_OK;
32885 }else{
32886 pFile->lastErrno = GetLastError();
32887 return winLogError(SQLITE_IOERR_FSYNC, "winSync", pFile->zPath);
 
32888 }
32889 #endif
32890 }
32891
32892 /*
@@ -32894,20 +33809,21 @@
32894 */
32895 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
32896 DWORD upperBits;
32897 DWORD lowerBits;
32898 winFile *pFile = (winFile*)id;
32899 DWORD error;
32900
32901 assert( id!=0 );
32902 SimulateIOError(return SQLITE_IOERR_FSTAT);
32903 lowerBits = GetFileSize(pFile->h, &upperBits);
32904 if( (lowerBits == INVALID_FILE_SIZE)
32905 && ((error = GetLastError()) != NO_ERROR) )
32906 {
32907 pFile->lastErrno = error;
32908 return winLogError(SQLITE_IOERR_FSTAT, "winFileSize", pFile->zPath);
 
32909 }
32910 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
32911 return SQLITE_OK;
32912 }
32913
@@ -32919,33 +33835,33 @@
32919 #endif
32920
32921 /*
32922 ** Acquire a reader lock.
32923 ** Different API routines are called depending on whether or not this
32924 ** is Win95 or WinNT.
32925 */
32926 static int getReadLock(winFile *pFile){
32927 int res;
32928 if( isNT() ){
32929 OVERLAPPED ovlp;
32930 ovlp.Offset = SHARED_FIRST;
32931 ovlp.OffsetHigh = 0;
32932 ovlp.hEvent = 0;
32933 res = LockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
32934 0, SHARED_SIZE, 0, &ovlp);
32935 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32936 */
32937 #if SQLITE_OS_WINCE==0
32938 }else{
32939 int lk;
32940 sqlite3_randomness(sizeof(lk), &lk);
32941 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
32942 res = LockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
32943 #endif
32944 }
32945 if( res == 0 ){
32946 pFile->lastErrno = GetLastError();
32947 /* No need to log a failure to lock */
32948 }
32949 return res;
32950 }
32951
@@ -32952,22 +33868,24 @@
32952 /*
32953 ** Undo a readlock
32954 */
32955 static int unlockReadLock(winFile *pFile){
32956 int res;
 
32957 if( isNT() ){
32958 res = UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
32959 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
32960 */
32961 #if SQLITE_OS_WINCE==0
32962 }else{
32963 res = UnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
32964 #endif
32965 }
32966 if( res==0 && GetLastError()!=ERROR_NOT_LOCKED ){
32967 pFile->lastErrno = GetLastError();
32968 winLogError(SQLITE_IOERR_UNLOCK, "unlockReadLock", pFile->zPath);
 
32969 }
32970 return res;
32971 }
32972
32973 /*
@@ -32996,15 +33914,15 @@
32996 ** It is not possible to lower the locking level one step at a time. You
32997 ** must go straight to locking level 0.
32998 */
32999 static int winLock(sqlite3_file *id, int locktype){
33000 int rc = SQLITE_OK; /* Return code from subroutines */
33001 int res = 1; /* Result of a windows lock call */
33002 int newLocktype; /* Set pFile->locktype to this value before exiting */
33003 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
33004 winFile *pFile = (winFile*)id;
33005 DWORD error = NO_ERROR;
33006
33007 assert( id!=0 );
33008 OSTRACE(("LOCK %d %d was %d(%d)\n",
33009 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
33010
@@ -33030,20 +33948,23 @@
33030 if( (pFile->locktype==NO_LOCK)
33031 || ( (locktype==EXCLUSIVE_LOCK)
33032 && (pFile->locktype==RESERVED_LOCK))
33033 ){
33034 int cnt = 3;
33035 while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
33036 /* Try 3 times to get the pending lock. The pending lock might be
33037 ** held by another reader process who will release it momentarily.
 
 
 
33038 */
33039 OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
33040 Sleep(1);
33041 }
33042 gotPendingLock = res;
33043 if( !res ){
33044 error = GetLastError();
33045 }
33046 }
33047
33048 /* Acquire a shared lock
33049 */
@@ -33051,23 +33972,23 @@
33051 assert( pFile->locktype==NO_LOCK );
33052 res = getReadLock(pFile);
33053 if( res ){
33054 newLocktype = SHARED_LOCK;
33055 }else{
33056 error = GetLastError();
33057 }
33058 }
33059
33060 /* Acquire a RESERVED lock
33061 */
33062 if( locktype==RESERVED_LOCK && res ){
33063 assert( pFile->locktype==SHARED_LOCK );
33064 res = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33065 if( res ){
33066 newLocktype = RESERVED_LOCK;
33067 }else{
33068 error = GetLastError();
33069 }
33070 }
33071
33072 /* Acquire a PENDING lock
33073 */
@@ -33080,25 +34001,25 @@
33080 */
33081 if( locktype==EXCLUSIVE_LOCK && res ){
33082 assert( pFile->locktype>=SHARED_LOCK );
33083 res = unlockReadLock(pFile);
33084 OSTRACE(("unreadlock = %d\n", res));
33085 res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33086 if( res ){
33087 newLocktype = EXCLUSIVE_LOCK;
33088 }else{
33089 error = GetLastError();
33090 OSTRACE(("error-code = %d\n", error));
33091 getReadLock(pFile);
33092 }
33093 }
33094
33095 /* If we are holding a PENDING lock that ought to be released, then
33096 ** release it now.
33097 */
33098 if( gotPendingLock && locktype==SHARED_LOCK ){
33099 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
33100 }
33101
33102 /* Update the state of the lock has held in the file descriptor then
33103 ** return the appropriate result code.
33104 */
@@ -33105,11 +34026,11 @@
33105 if( res ){
33106 rc = SQLITE_OK;
33107 }else{
33108 OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
33109 locktype, newLocktype));
33110 pFile->lastErrno = error;
33111 rc = SQLITE_BUSY;
33112 }
33113 pFile->locktype = (u8)newLocktype;
33114 return rc;
33115 }
@@ -33128,13 +34049,13 @@
33128 assert( id!=0 );
33129 if( pFile->locktype>=RESERVED_LOCK ){
33130 rc = 1;
33131 OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
33132 }else{
33133 rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33134 if( rc ){
33135 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33136 }
33137 rc = !rc;
33138 OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
33139 }
33140 *pResOut = rc;
@@ -33160,29 +34081,46 @@
33160 assert( locktype<=SHARED_LOCK );
33161 OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
33162 pFile->locktype, pFile->sharedLockByte));
33163 type = pFile->locktype;
33164 if( type>=EXCLUSIVE_LOCK ){
33165 UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33166 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33167 /* This should never happen. We should always be able to
33168 ** reacquire the read lock */
33169 rc = winLogError(SQLITE_IOERR_UNLOCK, "winUnlock", pFile->zPath);
 
33170 }
33171 }
33172 if( type>=RESERVED_LOCK ){
33173 UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33174 }
33175 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33176 unlockReadLock(pFile);
33177 }
33178 if( type>=PENDING_LOCK ){
33179 UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
33180 }
33181 pFile->locktype = (u8)locktype;
33182 return rc;
33183 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33184
33185 /*
33186 ** Control and query of the open file handle.
33187 */
33188 static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33215,19 +34153,19 @@
33215 return rc;
33216 }
33217 return SQLITE_OK;
33218 }
33219 case SQLITE_FCNTL_PERSIST_WAL: {
33220 int bPersist = *(int*)pArg;
33221 if( bPersist<0 ){
33222 *(int*)pArg = pFile->bPersistWal;
33223 }else{
33224 pFile->bPersistWal = bPersist!=0;
33225 }
33226 return SQLITE_OK;
33227 }
33228 case SQLITE_FCNTL_SYNC_OMITTED: {
 
33229 return SQLITE_OK;
33230 }
33231 case SQLITE_FCNTL_WIN32_AV_RETRY: {
33232 int *a = (int*)pArg;
33233 if( a[0]>0 ){
@@ -33255,20 +34193,21 @@
33255 ** if two files are created in the same file-system directory (i.e.
33256 ** a database and its journal file) that the sector size will be the
33257 ** same for both.
33258 */
33259 static int winSectorSize(sqlite3_file *id){
33260 assert( id!=0 );
33261 return (int)(((winFile*)id)->sectorSize);
33262 }
33263
33264 /*
33265 ** Return a vector of device characteristics.
33266 */
33267 static int winDeviceCharacteristics(sqlite3_file *id){
33268 UNUSED_PARAMETER(id);
33269 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN;
 
33270 }
33271
33272 #ifndef SQLITE_OMIT_WAL
33273
33274 /*
@@ -33411,19 +34350,19 @@
33411 memset(&ovlp, 0, sizeof(OVERLAPPED));
33412 ovlp.Offset = ofst;
33413
33414 /* Release/Acquire the system-level lock */
33415 if( lockType==_SHM_UNLCK ){
33416 rc = UnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
33417 }else{
33418 rc = LockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
33419 }
33420
33421 if( rc!= 0 ){
33422 rc = SQLITE_OK;
33423 }else{
33424 pFile->lastErrno = GetLastError();
33425 rc = SQLITE_BUSY;
33426 }
33427
33428 OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
33429 pFile->hFile.h,
@@ -33453,27 +34392,29 @@
33453 while( (p = *pp)!=0 ){
33454 if( p->nRef==0 ){
33455 int i;
33456 if( p->mutex ) sqlite3_mutex_free(p->mutex);
33457 for(i=0; i<p->nRegion; i++){
33458 bRc = UnmapViewOfFile(p->aRegion[i].pMap);
33459 OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
33460 (int)GetCurrentProcessId(), i,
33461 bRc ? "ok" : "failed"));
33462 bRc = CloseHandle(p->aRegion[i].hMap);
33463 OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
33464 (int)GetCurrentProcessId(), i,
33465 bRc ? "ok" : "failed"));
33466 }
33467 if( p->hFile.h != INVALID_HANDLE_VALUE ){
33468 SimulateIOErrorBenign(1);
33469 winClose((sqlite3_file *)&p->hFile);
33470 SimulateIOErrorBenign(0);
33471 }
33472 if( deleteFlag ){
33473 SimulateIOErrorBenign(1);
 
33474 winDelete(pVfs, p->zFilename, 0);
 
33475 SimulateIOErrorBenign(0);
33476 }
33477 *pp = p->pNext;
33478 sqlite3_free(p->aRegion);
33479 sqlite3_free(p);
@@ -33501,19 +34442,19 @@
33501
33502 /* Allocate space for the new sqlite3_shm object. Also speculatively
33503 ** allocate space for a new winShmNode and filename.
33504 */
33505 p = sqlite3_malloc( sizeof(*p) );
33506 if( p==0 ) return SQLITE_NOMEM;
33507 memset(p, 0, sizeof(*p));
33508 nName = sqlite3Strlen30(pDbFd->zPath);
33509 pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 15 );
33510 if( pNew==0 ){
33511 sqlite3_free(p);
33512 return SQLITE_NOMEM;
33513 }
33514 memset(pNew, 0, sizeof(*pNew));
33515 pNew->zFilename = (char*)&pNew[1];
33516 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
33517 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
33518
33519 /* Look to see if there is an existing winShmNode that can be used.
@@ -33535,31 +34476,31 @@
33535 pShmNode->pNext = winShmNodeList;
33536 winShmNodeList = pShmNode;
33537
33538 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
33539 if( pShmNode->mutex==0 ){
33540 rc = SQLITE_NOMEM;
33541 goto shm_open_err;
33542 }
33543
33544 rc = winOpen(pDbFd->pVfs,
33545 pShmNode->zFilename, /* Name of the file (UTF-8) */
33546 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
33547 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
33548 0);
33549 if( SQLITE_OK!=rc ){
33550 rc = SQLITE_CANTOPEN_BKPT;
33551 goto shm_open_err;
33552 }
33553
33554 /* Check to see if another process is holding the dead-man switch.
33555 ** If not, truncate the file to zero length.
33556 */
33557 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
33558 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
33559 if( rc!=SQLITE_OK ){
33560 rc = winLogError(SQLITE_IOERR_SHMOPEN, "winOpenShm", pDbFd->zPath);
 
33561 }
33562 }
33563 if( rc==SQLITE_OK ){
33564 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33565 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -33740,11 +34681,11 @@
33740 }
33741 }
33742 }
33743 sqlite3_mutex_leave(pShmNode->mutex);
33744 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
33745 p->id, (int)GetCurrentProcessId(), p->sharedMask, p->exclMask,
33746 rc ? "failed" : "ok"));
33747 return rc;
33748 }
33749
33750 /*
@@ -33814,11 +34755,12 @@
33814 ** Check to see if it has been allocated (i.e. if the wal-index file is
33815 ** large enough to contain the requested region).
33816 */
33817 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
33818 if( rc!=SQLITE_OK ){
33819 rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap1", pDbFd->zPath);
 
33820 goto shmpage_out;
33821 }
33822
33823 if( sz<nByte ){
33824 /* The requested memory region does not exist. If isWrite is set to
@@ -33828,11 +34770,12 @@
33828 ** the requested memory region.
33829 */
33830 if( !isWrite ) goto shmpage_out;
33831 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
33832 if( rc!=SQLITE_OK ){
33833 rc = winLogError(SQLITE_IOERR_SHMSIZE, "winShmMap2", pDbFd->zPath);
 
33834 goto shmpage_out;
33835 }
33836 }
33837
33838 /* Map the requested memory region into this processes address space. */
@@ -33847,30 +34790,31 @@
33847
33848 while( pShmNode->nRegion<=iRegion ){
33849 HANDLE hMap; /* file-mapping handle */
33850 void *pMap = 0; /* Mapped memory region */
33851
33852 hMap = CreateFileMapping(pShmNode->hFile.h,
33853 NULL, PAGE_READWRITE, 0, nByte, NULL
33854 );
33855 OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
33856 (int)GetCurrentProcessId(), pShmNode->nRegion, nByte,
33857 hMap ? "ok" : "failed"));
33858 if( hMap ){
33859 int iOffset = pShmNode->nRegion*szRegion;
33860 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
33861 pMap = MapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
33862 0, iOffset - iOffsetShift, szRegion + iOffsetShift
33863 );
33864 OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
33865 (int)GetCurrentProcessId(), pShmNode->nRegion, iOffset, szRegion,
33866 pMap ? "ok" : "failed"));
33867 }
33868 if( !pMap ){
33869 pShmNode->lastErrno = GetLastError();
33870 rc = winLogError(SQLITE_IOERR_SHMMAP, "winShmMap3", pDbFd->zPath);
33871 if( hMap ) CloseHandle(hMap);
 
33872 goto shmpage_out;
33873 }
33874
33875 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
33876 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
@@ -33964,11 +34908,11 @@
33964 static char zChars[] =
33965 "abcdefghijklmnopqrstuvwxyz"
33966 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33967 "0123456789";
33968 size_t i, j;
33969 char zTempPath[MAX_PATH+1];
33970
33971 /* It's odd to simulate an io-error here, but really this is just
33972 ** using the io-error infrastructure to test that SQLite handles this
33973 ** function failing.
33974 */
@@ -33977,55 +34921,56 @@
33977 if( sqlite3_temp_directory ){
33978 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
33979 }else if( isNT() ){
33980 char *zMulti;
33981 WCHAR zWidePath[MAX_PATH];
33982 GetTempPathW(MAX_PATH-30, zWidePath);
33983 zMulti = unicodeToUtf8(zWidePath);
33984 if( zMulti ){
33985 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
33986 free(zMulti);
33987 }else{
33988 return SQLITE_NOMEM;
33989 }
33990 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33991 ** Since the ASCII version of these Windows API do not exist for WINCE,
33992 ** it's important to not reference them for WINCE builds.
33993 */
33994 #if SQLITE_OS_WINCE==0
33995 }else{
33996 char *zUtf8;
33997 char zMbcsPath[MAX_PATH];
33998 GetTempPathA(MAX_PATH-30, zMbcsPath);
33999 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34000 if( zUtf8 ){
34001 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
34002 free(zUtf8);
34003 }else{
34004 return SQLITE_NOMEM;
34005 }
34006 #endif
34007 }
34008
34009 /* Check that the output buffer is large enough for the temporary file
34010 ** name. If it is not, return SQLITE_ERROR.
34011 */
34012 if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 17) >= nBuf ){
34013 return SQLITE_ERROR;
34014 }
34015
34016 for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
34017 zTempPath[i] = 0;
34018
34019 sqlite3_snprintf(nBuf-17, zBuf,
34020 "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
34021 j = sqlite3Strlen30(zBuf);
34022 sqlite3_randomness(15, &zBuf[j]);
34023 for(i=0; i<15; i++, j++){
34024 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34025 }
34026 zBuf[j] = 0;
 
34027
34028 OSTRACE(("TEMP FILENAME: %s\n", zBuf));
34029 return SQLITE_OK;
34030 }
34031
@@ -34038,10 +34983,11 @@
34038 sqlite3_file *id, /* Write the SQLite file handle here */
34039 int flags, /* Open mode flags */
34040 int *pOutFlags /* Status return flags */
34041 ){
34042 HANDLE h;
 
34043 DWORD dwDesiredAccess;
34044 DWORD dwShareMode;
34045 DWORD dwCreationDisposition;
34046 DWORD dwFlagsAndAttributes = 0;
34047 #if SQLITE_OS_WINCE
@@ -34053,11 +34999,11 @@
34053 int cnt = 0;
34054
34055 /* If argument zPath is a NULL pointer, this function is required to open
34056 ** a temporary file. Use this buffer to store the file name in.
34057 */
34058 char zTmpname[MAX_PATH+1]; /* Buffer used to create temp filename */
34059
34060 int rc = SQLITE_OK; /* Function Return Code */
34061 #if !defined(NDEBUG) || SQLITE_OS_WINCE
34062 int eType = flags&0xFFFFFF00; /* Type of file to open */
34063 #endif
@@ -34112,21 +35058,28 @@
34112 /* If the second argument to this function is NULL, generate a
34113 ** temporary file name to use
34114 */
34115 if( !zUtf8Name ){
34116 assert(isDelete && !isOpenJournal);
34117 rc = getTempname(MAX_PATH+1, zTmpname);
34118 if( rc!=SQLITE_OK ){
34119 return rc;
34120 }
34121 zUtf8Name = zTmpname;
34122 }
 
 
 
 
 
 
 
34123
34124 /* Convert the filename to the system encoding. */
34125 zConverted = convertUtf8Filename(zUtf8Name);
34126 if( zConverted==0 ){
34127 return SQLITE_NOMEM;
34128 }
34129
34130 if( isReadWrite ){
34131 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
34132 }else{
@@ -34168,30 +35121,30 @@
34168 #if SQLITE_OS_WINCE
34169 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
34170 #endif
34171
34172 if( isNT() ){
34173 while( (h = CreateFileW((WCHAR*)zConverted,
34174 dwDesiredAccess,
34175 dwShareMode, NULL,
34176 dwCreationDisposition,
34177 dwFlagsAndAttributes,
34178 NULL))==INVALID_HANDLE_VALUE &&
34179 retryIoerr(&cnt) ){}
34180 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34181 ** Since the ASCII version of these Windows API do not exist for WINCE,
34182 ** it's important to not reference them for WINCE builds.
34183 */
34184 #if SQLITE_OS_WINCE==0
34185 }else{
34186 while( (h = CreateFileA((char*)zConverted,
34187 dwDesiredAccess,
34188 dwShareMode, NULL,
34189 dwCreationDisposition,
34190 dwFlagsAndAttributes,
34191 NULL))==INVALID_HANDLE_VALUE &&
34192 retryIoerr(&cnt) ){}
34193 #endif
34194 }
34195
34196 logIoerr(cnt);
34197
@@ -34198,13 +35151,13 @@
34198 OSTRACE(("OPEN %d %s 0x%lx %s\n",
34199 h, zName, dwDesiredAccess,
34200 h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
34201
34202 if( h==INVALID_HANDLE_VALUE ){
34203 pFile->lastErrno = GetLastError();
34204 winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
34205 free(zConverted);
34206 if( isReadWrite && !isExclusive ){
34207 return winOpen(pVfs, zName, id,
34208 ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
34209 }else{
34210 return SQLITE_CANTOPEN_BKPT;
@@ -34224,36 +35177,38 @@
34224 pFile->h = h;
34225 pFile->lastErrno = NO_ERROR;
34226 pFile->pVfs = pVfs;
34227 pFile->pShm = 0;
34228 pFile->zPath = zName;
34229 pFile->sectorSize = getSectorSize(pVfs, zUtf8Name);
 
 
34230
34231 #if SQLITE_OS_WINCE
34232 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
34233 && !winceCreateLock(zName, pFile)
34234 ){
34235 CloseHandle(h);
34236 free(zConverted);
34237 return SQLITE_CANTOPEN_BKPT;
34238 }
34239 if( isTemp ){
34240 pFile->zDeleteOnClose = zConverted;
34241 }else
34242 #endif
34243 {
34244 free(zConverted);
34245 }
34246
34247 OpenCounter(+1);
34248 return rc;
34249 }
34250
34251 /*
34252 ** Delete the named file.
34253 **
34254 ** Note that windows does not allow a file to be deleted if some other
34255 ** process has it open. Sometimes a virus scanner or indexing program
34256 ** will open a journal file shortly after it is created in order to do
34257 ** whatever it does. While this other process is holding the
34258 ** file open, we will be unable to delete it. To work around this
34259 ** problem, we delay 100 milliseconds and try to delete again. Up
@@ -34265,42 +35220,44 @@
34265 const char *zFilename, /* Name of file to delete */
34266 int syncDir /* Not used on win32 */
34267 ){
34268 int cnt = 0;
34269 int rc;
 
34270 void *zConverted;
34271 UNUSED_PARAMETER(pVfs);
34272 UNUSED_PARAMETER(syncDir);
34273
34274 SimulateIOError(return SQLITE_IOERR_DELETE);
34275 zConverted = convertUtf8Filename(zFilename);
34276 if( zConverted==0 ){
34277 return SQLITE_NOMEM;
34278 }
34279 if( isNT() ){
34280 rc = 1;
34281 while( GetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34282 (rc = DeleteFileW(zConverted))==0 && retryIoerr(&cnt) ){}
34283 rc = rc ? SQLITE_OK : SQLITE_ERROR;
34284 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34285 ** Since the ASCII version of these Windows API do not exist for WINCE,
34286 ** it's important to not reference them for WINCE builds.
34287 */
34288 #if SQLITE_OS_WINCE==0
34289 }else{
34290 rc = 1;
34291 while( GetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
34292 (rc = DeleteFileA(zConverted))==0 && retryIoerr(&cnt) ){}
34293 rc = rc ? SQLITE_OK : SQLITE_ERROR;
34294 #endif
34295 }
34296 if( rc ){
34297 rc = winLogError(SQLITE_IOERR_DELETE, "winDelete", zFilename);
 
34298 }else{
34299 logIoerr(cnt);
34300 }
34301 free(zConverted);
34302 OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
34303 return rc;
34304 }
34305
34306 /*
@@ -34312,25 +35269,26 @@
34312 int flags, /* Type of test to make on this file */
34313 int *pResOut /* OUT: Result */
34314 ){
34315 DWORD attr;
34316 int rc = 0;
 
34317 void *zConverted;
34318 UNUSED_PARAMETER(pVfs);
34319
34320 SimulateIOError( return SQLITE_IOERR_ACCESS; );
34321 zConverted = convertUtf8Filename(zFilename);
34322 if( zConverted==0 ){
34323 return SQLITE_NOMEM;
34324 }
34325 if( isNT() ){
34326 int cnt = 0;
34327 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34328 memset(&sAttrData, 0, sizeof(sAttrData));
34329 while( !(rc = GetFileAttributesExW((WCHAR*)zConverted,
34330 GetFileExInfoStandard,
34331 &sAttrData)) && retryIoerr(&cnt) ){}
34332 if( rc ){
34333 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
34334 ** as if it does not exist.
34335 */
34336 if( flags==SQLITE_ACCESS_EXISTS
@@ -34340,28 +35298,28 @@
34340 }else{
34341 attr = sAttrData.dwFileAttributes;
34342 }
34343 }else{
34344 logIoerr(cnt);
34345 if( GetLastError()!=ERROR_FILE_NOT_FOUND ){
34346 winLogError(SQLITE_IOERR_ACCESS, "winAccess", zFilename);
34347 free(zConverted);
34348 return SQLITE_IOERR_ACCESS;
34349 }else{
34350 attr = INVALID_FILE_ATTRIBUTES;
34351 }
34352 }
34353 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34354 ** Since the ASCII version of these Windows API do not exist for WINCE,
34355 ** it's important to not reference them for WINCE builds.
34356 */
34357 #if SQLITE_OS_WINCE==0
34358 }else{
34359 attr = GetFileAttributesA((char*)zConverted);
34360 #endif
34361 }
34362 free(zConverted);
34363 switch( flags ){
34364 case SQLITE_ACCESS_READ:
34365 case SQLITE_ACCESS_EXISTS:
34366 rc = attr!=INVALID_FILE_ATTRIBUTES;
34367 break;
@@ -34422,119 +35380,52 @@
34422 ** current working directory has been unlinked.
34423 */
34424 SimulateIOError( return SQLITE_ERROR );
34425 UNUSED_PARAMETER(nFull);
34426 zConverted = convertUtf8Filename(zRelative);
 
 
 
34427 if( isNT() ){
34428 WCHAR *zTemp;
34429 nByte = GetFullPathNameW((WCHAR*)zConverted, 0, 0, 0) + 3;
34430 zTemp = malloc( nByte*sizeof(zTemp[0]) );
34431 if( zTemp==0 ){
34432 free(zConverted);
34433 return SQLITE_NOMEM;
34434 }
34435 GetFullPathNameW((WCHAR*)zConverted, nByte, zTemp, 0);
34436 free(zConverted);
34437 zOut = unicodeToUtf8(zTemp);
34438 free(zTemp);
34439 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34440 ** Since the ASCII version of these Windows API do not exist for WINCE,
34441 ** it's important to not reference them for WINCE builds.
34442 */
34443 #if SQLITE_OS_WINCE==0
34444 }else{
34445 char *zTemp;
34446 nByte = GetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
34447 zTemp = malloc( nByte*sizeof(zTemp[0]) );
34448 if( zTemp==0 ){
34449 free(zConverted);
34450 return SQLITE_NOMEM;
34451 }
34452 GetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
34453 free(zConverted);
34454 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
34455 free(zTemp);
34456 #endif
34457 }
34458 if( zOut ){
34459 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
34460 free(zOut);
34461 return SQLITE_OK;
34462 }else{
34463 return SQLITE_NOMEM;
34464 }
34465 #endif
34466 }
34467
34468 /*
34469 ** Get the sector size of the device used to store
34470 ** file.
34471 */
34472 static int getSectorSize(
34473 sqlite3_vfs *pVfs,
34474 const char *zRelative /* UTF-8 file name */
34475 ){
34476 DWORD bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34477 /* GetDiskFreeSpace is not supported under WINCE */
34478 #if SQLITE_OS_WINCE
34479 UNUSED_PARAMETER(pVfs);
34480 UNUSED_PARAMETER(zRelative);
34481 #else
34482 char zFullpath[MAX_PATH+1];
34483 int rc;
34484 DWORD dwRet = 0;
34485 DWORD dwDummy;
34486
34487 /*
34488 ** We need to get the full path name of the file
34489 ** to get the drive letter to look up the sector
34490 ** size.
34491 */
34492 SimulateIOErrorBenign(1);
34493 rc = winFullPathname(pVfs, zRelative, MAX_PATH, zFullpath);
34494 SimulateIOErrorBenign(0);
34495 if( rc == SQLITE_OK )
34496 {
34497 void *zConverted = convertUtf8Filename(zFullpath);
34498 if( zConverted ){
34499 if( isNT() ){
34500 /* trim path to just drive reference */
34501 WCHAR *p = zConverted;
34502 for(;*p;p++){
34503 if( *p == '\\' ){
34504 *p = '\0';
34505 break;
34506 }
34507 }
34508 dwRet = GetDiskFreeSpaceW((WCHAR*)zConverted,
34509 &dwDummy,
34510 &bytesPerSector,
34511 &dwDummy,
34512 &dwDummy);
34513 }else{
34514 /* trim path to just drive reference */
34515 char *p = (char *)zConverted;
34516 for(;*p;p++){
34517 if( *p == '\\' ){
34518 *p = '\0';
34519 break;
34520 }
34521 }
34522 dwRet = GetDiskFreeSpaceA((char*)zConverted,
34523 &dwDummy,
34524 &bytesPerSector,
34525 &dwDummy,
34526 &dwDummy);
34527 }
34528 free(zConverted);
34529 }
34530 if( !dwRet ){
34531 bytesPerSector = SQLITE_DEFAULT_SECTOR_SIZE;
34532 }
34533 }
34534 #endif
34535 return (int) bytesPerSector;
34536 }
34537
34538 #ifndef SQLITE_OMIT_LOAD_EXTENSION
34539 /*
34540 ** Interfaces for opening a shared library, finding entry points
@@ -34550,41 +35441,34 @@
34550 UNUSED_PARAMETER(pVfs);
34551 if( zConverted==0 ){
34552 return 0;
34553 }
34554 if( isNT() ){
34555 h = LoadLibraryW((WCHAR*)zConverted);
34556 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34557 ** Since the ASCII version of these Windows API do not exist for WINCE,
34558 ** it's important to not reference them for WINCE builds.
34559 */
34560 #if SQLITE_OS_WINCE==0
34561 }else{
34562 h = LoadLibraryA((char*)zConverted);
34563 #endif
34564 }
34565 free(zConverted);
34566 return (void*)h;
34567 }
34568 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
34569 UNUSED_PARAMETER(pVfs);
34570 getLastErrorMsg(nBuf, zBufOut);
34571 }
34572 static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34573 UNUSED_PARAMETER(pVfs);
34574 #if SQLITE_OS_WINCE
34575 /* The GetProcAddressA() routine is only available on wince. */
34576 return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34577 #else
34578 /* All other windows platforms expect GetProcAddress() to take
34579 ** an Ansi string regardless of the _UNICODE setting */
34580 return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34581 #endif
34582 }
34583 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34584 UNUSED_PARAMETER(pVfs);
34585 FreeLibrary((HANDLE)pHandle);
34586 }
34587 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
34588 #define winDlOpen 0
34589 #define winDlError 0
34590 #define winDlSym 0
@@ -34602,27 +35486,27 @@
34602 n = nBuf;
34603 memset(zBuf, 0, nBuf);
34604 #else
34605 if( sizeof(SYSTEMTIME)<=nBuf-n ){
34606 SYSTEMTIME x;
34607 GetSystemTime(&x);
34608 memcpy(&zBuf[n], &x, sizeof(x));
34609 n += sizeof(x);
34610 }
34611 if( sizeof(DWORD)<=nBuf-n ){
34612 DWORD pid = GetCurrentProcessId();
34613 memcpy(&zBuf[n], &pid, sizeof(pid));
34614 n += sizeof(pid);
34615 }
34616 if( sizeof(DWORD)<=nBuf-n ){
34617 DWORD cnt = GetTickCount();
34618 memcpy(&zBuf[n], &cnt, sizeof(cnt));
34619 n += sizeof(cnt);
34620 }
34621 if( sizeof(LARGE_INTEGER)<=nBuf-n ){
34622 LARGE_INTEGER i;
34623 QueryPerformanceCounter(&i);
34624 memcpy(&zBuf[n], &i, sizeof(i));
34625 n += sizeof(i);
34626 }
34627 #endif
34628 return n;
@@ -34631,11 +35515,11 @@
34631
34632 /*
34633 ** Sleep for a little while. Return the amount of time slept.
34634 */
34635 static int winSleep(sqlite3_vfs *pVfs, int microsec){
34636 Sleep((microsec+999)/1000);
34637 UNUSED_PARAMETER(pVfs);
34638 return ((microsec+999)/1000)*1000;
34639 }
34640
34641 /*
@@ -34670,17 +35554,17 @@
34670 static const sqlite3_int64 max32BitValue =
34671 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
34672
34673 #if SQLITE_OS_WINCE
34674 SYSTEMTIME time;
34675 GetSystemTime(&time);
34676 /* if SystemTimeToFileTime() fails, it returns zero. */
34677 if (!SystemTimeToFileTime(&time,&ft)){
34678 return SQLITE_ERROR;
34679 }
34680 #else
34681 GetSystemTimeAsFileTime( &ft );
34682 #endif
34683
34684 *piNow = winFiletimeEpoch +
34685 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
34686 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
@@ -34709,12 +35593,12 @@
34709 return rc;
34710 }
34711
34712 /*
34713 ** The idea is that this function works like a combination of
34714 ** GetLastError() and FormatMessage() on windows (or errno and
34715 ** strerror_r() on unix). After an error is returned by an OS
34716 ** function, SQLite calls this function with zBuf pointing to
34717 ** a buffer of nBuf bytes. The OS layer should populate the
34718 ** buffer with a nul-terminated UTF-8 encoded error message
34719 ** describing the last IO error to have occurred within the calling
34720 ** thread.
@@ -34739,14 +35623,12 @@
34739 ** by sqlite into the error message available to the user using
34740 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
34741 */
34742 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
34743 UNUSED_PARAMETER(pVfs);
34744 return getLastErrorMsg(nBuf, zBuf);
34745 }
34746
34747
34748
34749 /*
34750 ** Initialize and deinitialize the operating system interface.
34751 */
34752 SQLITE_API int sqlite3_os_init(void){
@@ -34768,25 +35650,30 @@
34768 winRandomness, /* xRandomness */
34769 winSleep, /* xSleep */
34770 winCurrentTime, /* xCurrentTime */
34771 winGetLastError, /* xGetLastError */
34772 winCurrentTimeInt64, /* xCurrentTimeInt64 */
34773 0, /* xSetSystemCall */
34774 0, /* xGetSystemCall */
34775 0, /* xNextSystemCall */
34776 };
 
 
 
 
34777
34778 #ifndef SQLITE_OMIT_WAL
34779 /* get memory map allocation granularity */
34780 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
34781 GetSystemInfo(&winSysInfo);
34782 assert(winSysInfo.dwAllocationGranularity > 0);
34783 #endif
34784
34785 sqlite3_vfs_register(&winVfs, 1);
34786 return SQLITE_OK;
34787 }
 
34788 SQLITE_API int sqlite3_os_end(void){
34789 return SQLITE_OK;
34790 }
34791
34792 #endif /* SQLITE_OS_WIN */
@@ -35222,11 +36109,11 @@
35222 */
35223 struct PCache {
35224 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
35225 PgHdr *pSynced; /* Last synced page in dirty page list */
35226 int nRef; /* Number of referenced pages */
35227 int nMax; /* Configured cache size */
35228 int szPage; /* Size of every page in this cache */
35229 int szExtra; /* Size of extra space for each page */
35230 int bPurgeable; /* True if pages are on backing store */
35231 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
35232 void *pStress; /* Argument to xStress */
@@ -35333,32 +36220,32 @@
35333 PCache *pCache = p->pCache;
35334 if( pCache->bPurgeable ){
35335 if( p->pgno==1 ){
35336 pCache->pPage1 = 0;
35337 }
35338 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 0);
35339 }
35340 }
35341
35342 /*************************************************** General Interfaces ******
35343 **
35344 ** Initialize and shutdown the page cache subsystem. Neither of these
35345 ** functions are threadsafe.
35346 */
35347 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
35348 if( sqlite3GlobalConfig.pcache.xInit==0 ){
35349 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
35350 ** built-in default page cache is used instead of the application defined
35351 ** page cache. */
35352 sqlite3PCacheSetDefault();
35353 }
35354 return sqlite3GlobalConfig.pcache.xInit(sqlite3GlobalConfig.pcache.pArg);
35355 }
35356 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
35357 if( sqlite3GlobalConfig.pcache.xShutdown ){
35358 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
35359 sqlite3GlobalConfig.pcache.xShutdown(sqlite3GlobalConfig.pcache.pArg);
35360 }
35361 }
35362
35363 /*
35364 ** Return the size in bytes of a PCache object.
@@ -35383,26 +36270,37 @@
35383 p->szPage = szPage;
35384 p->szExtra = szExtra;
35385 p->bPurgeable = bPurgeable;
35386 p->xStress = xStress;
35387 p->pStress = pStress;
35388 p->nMax = 100;
35389 }
35390
35391 /*
35392 ** Change the page size for PCache object. The caller must ensure that there
35393 ** are no outstanding page references when this function is called.
35394 */
35395 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
35396 assert( pCache->nRef==0 && pCache->pDirty==0 );
35397 if( pCache->pCache ){
35398 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
35399 pCache->pCache = 0;
35400 pCache->pPage1 = 0;
35401 }
35402 pCache->szPage = szPage;
35403 }
 
 
 
 
 
 
 
 
 
 
 
35404
35405 /*
35406 ** Try to obtain a page from the cache.
35407 */
35408 SQLITE_PRIVATE int sqlite3PcacheFetch(
@@ -35409,11 +36307,12 @@
35409 PCache *pCache, /* Obtain the page from this cache */
35410 Pgno pgno, /* Page number to obtain */
35411 int createFlag, /* If true, create page if it does not exist already */
35412 PgHdr **ppPage /* Write the page here */
35413 ){
35414 PgHdr *pPage = 0;
 
35415 int eCreate;
35416
35417 assert( pCache!=0 );
35418 assert( createFlag==1 || createFlag==0 );
35419 assert( pgno>0 );
@@ -35421,23 +36320,23 @@
35421 /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
35422 ** allocate it now.
35423 */
35424 if( !pCache->pCache && createFlag ){
35425 sqlite3_pcache *p;
35426 int nByte;
35427 nByte = pCache->szPage + pCache->szExtra + sizeof(PgHdr);
35428 p = sqlite3GlobalConfig.pcache.xCreate(nByte, pCache->bPurgeable);
35429 if( !p ){
35430 return SQLITE_NOMEM;
35431 }
35432 sqlite3GlobalConfig.pcache.xCachesize(p, pCache->nMax);
35433 pCache->pCache = p;
35434 }
35435
35436 eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
35437 if( pCache->pCache ){
35438 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, eCreate);
35439 }
35440
35441 if( !pPage && eCreate==1 ){
35442 PgHdr *pPg;
35443
@@ -35460,45 +36359,48 @@
35460 #ifdef SQLITE_LOG_CACHE_SPILL
35461 sqlite3_log(SQLITE_FULL,
35462 "spill page %d making room for %d - cache used: %d/%d",
35463 pPg->pgno, pgno,
35464 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
35465 pCache->nMax);
35466 #endif
35467 rc = pCache->xStress(pCache->pStress, pPg);
35468 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
35469 return rc;
35470 }
35471 }
35472
35473 pPage = sqlite3GlobalConfig.pcache.xFetch(pCache->pCache, pgno, 2);
35474 }
35475
35476 if( pPage ){
35477 if( !pPage->pData ){
35478 memset(pPage, 0, sizeof(PgHdr));
35479 pPage->pData = (void *)&pPage[1];
35480 pPage->pExtra = (void*)&((char *)pPage->pData)[pCache->szPage];
35481 memset(pPage->pExtra, 0, pCache->szExtra);
35482 pPage->pCache = pCache;
35483 pPage->pgno = pgno;
35484 }
35485 assert( pPage->pCache==pCache );
35486 assert( pPage->pgno==pgno );
35487 assert( pPage->pData==(void *)&pPage[1] );
35488 assert( pPage->pExtra==(void *)&((char *)&pPage[1])[pCache->szPage] );
35489
35490 if( 0==pPage->nRef ){
 
 
 
35491 pCache->nRef++;
35492 }
35493 pPage->nRef++;
35494 if( pgno==1 ){
35495 pCache->pPage1 = pPage;
35496 }
35497 }
35498 *ppPage = pPage;
35499 return (pPage==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
35500 }
35501
35502 /*
35503 ** Decrement the reference count on a page. If the page is clean and the
35504 ** reference count drops to 0, then it is made elible for recycling.
@@ -35541,11 +36443,11 @@
35541 pCache = p->pCache;
35542 pCache->nRef--;
35543 if( p->pgno==1 ){
35544 pCache->pPage1 = 0;
35545 }
35546 sqlite3GlobalConfig.pcache.xUnpin(pCache->pCache, p, 1);
35547 }
35548
35549 /*
35550 ** Make sure the page is marked as dirty. If it isn't dirty already,
35551 ** make it so.
@@ -35599,11 +36501,11 @@
35599 */
35600 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
35601 PCache *pCache = p->pCache;
35602 assert( p->nRef>0 );
35603 assert( newPgno>0 );
35604 sqlite3GlobalConfig.pcache.xRekey(pCache->pCache, p, p->pgno, newPgno);
35605 p->pgno = newPgno;
35606 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
35607 pcacheRemoveFromDirtyList(p);
35608 pcacheAddToDirtyList(p);
35609 }
@@ -35636,20 +36538,20 @@
35636 }
35637 if( pgno==0 && pCache->pPage1 ){
35638 memset(pCache->pPage1->pData, 0, pCache->szPage);
35639 pgno = 1;
35640 }
35641 sqlite3GlobalConfig.pcache.xTruncate(pCache->pCache, pgno+1);
35642 }
35643 }
35644
35645 /*
35646 ** Close a cache.
35647 */
35648 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
35649 if( pCache->pCache ){
35650 sqlite3GlobalConfig.pcache.xDestroy(pCache->pCache);
35651 }
35652 }
35653
35654 /*
35655 ** Discard the contents of the cache.
@@ -35757,31 +36659,41 @@
35757 ** Return the total number of pages in the cache.
35758 */
35759 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
35760 int nPage = 0;
35761 if( pCache->pCache ){
35762 nPage = sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache);
35763 }
35764 return nPage;
35765 }
35766
35767 #ifdef SQLITE_TEST
35768 /*
35769 ** Get the suggested cache-size value.
35770 */
35771 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
35772 return pCache->nMax;
35773 }
35774 #endif
35775
35776 /*
35777 ** Set the suggested cache-size value.
35778 */
35779 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
35780 pCache->nMax = mxPage;
 
 
 
 
 
 
 
 
 
 
35781 if( pCache->pCache ){
35782 sqlite3GlobalConfig.pcache.xCachesize(pCache->pCache, mxPage);
35783 }
35784 }
35785
35786 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
35787 /*
@@ -35822,11 +36734,10 @@
35822 typedef struct PCache1 PCache1;
35823 typedef struct PgHdr1 PgHdr1;
35824 typedef struct PgFreeslot PgFreeslot;
35825 typedef struct PGroup PGroup;
35826
35827
35828 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35829 ** of one or more PCaches that are able to recycle each others unpinned
35830 ** pages when they are under memory pressure. A PGroup is an instance of
35831 ** the following object.
35832 **
@@ -35839,22 +36750,22 @@
35839 ** of.
35840 **
35841 ** Mode 1 uses more memory (since PCache instances are not able to rob
35842 ** unused pages from other PCaches) but it also operates without a mutex,
35843 ** and is therefore often faster. Mode 2 requires a mutex in order to be
35844 ** threadsafe, but is able recycle pages more efficient.
35845 **
35846 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
35847 ** PGroup which is the pcache1.grp global variable and its mutex is
35848 ** SQLITE_MUTEX_STATIC_LRU.
35849 */
35850 struct PGroup {
35851 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
35852 int nMaxPage; /* Sum of nMax for purgeable caches */
35853 int nMinPage; /* Sum of nMin for purgeable caches */
35854 int mxPinned; /* nMaxpage + 10 - nMinPage */
35855 int nCurrentPage; /* Number of purgeable pages allocated */
35856 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35857 };
35858
35859 /* Each page cache is an instance of the following object. Every
35860 ** open database file (including each in-memory database and each
@@ -35865,15 +36776,16 @@
35865 ** opaque sqlite3_pcache* handles.
35866 */
35867 struct PCache1 {
35868 /* Cache configuration parameters. Page size (szPage) and the purgeable
35869 ** flag (bPurgeable) are set when the cache is created. nMax may be
35870 ** modified at any time by a call to the pcache1CacheSize() method.
35871 ** The PGroup mutex must be held when accessing nMax.
35872 */
35873 PGroup *pGroup; /* PGroup this cache belongs to */
35874 int szPage; /* Size of allocated pages in bytes */
 
35875 int bPurgeable; /* True if cache is purgeable */
35876 unsigned int nMin; /* Minimum number of pages reserved */
35877 unsigned int nMax; /* Configured "cache_size" value */
35878 unsigned int n90pct; /* nMax*9/10 */
35879
@@ -35888,15 +36800,16 @@
35888 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
35889 };
35890
35891 /*
35892 ** Each cache entry is represented by an instance of the following
35893 ** structure. A buffer of PgHdr1.pCache->szPage bytes is allocated
35894 ** directly before this structure in memory (see the PGHDR1_TO_PAGE()
35895 ** macro below).
35896 */
35897 struct PgHdr1 {
 
35898 unsigned int iKey; /* Key value (page number) */
35899 PgHdr1 *pNext; /* Next in hash table chain */
35900 PCache1 *pCache; /* Cache that currently owns this page */
35901 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
35902 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
@@ -35942,25 +36855,10 @@
35942 ** alias "pcache1". This ensures that the WSD emulation is used when
35943 ** compiling for systems that do not support real WSD.
35944 */
35945 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
35946
35947 /*
35948 ** When a PgHdr1 structure is allocated, the associated PCache1.szPage
35949 ** bytes of data are located directly before it in memory (i.e. the total
35950 ** size of the allocation is sizeof(PgHdr1)+PCache1.szPage byte). The
35951 ** PGHDR1_TO_PAGE() macro takes a pointer to a PgHdr1 structure as
35952 ** an argument and returns a pointer to the associated block of szPage
35953 ** bytes. The PAGE_TO_PGHDR1() macro does the opposite: its argument is
35954 ** a pointer to a block of szPage bytes of data and the return value is
35955 ** a pointer to the associated PgHdr1 structure.
35956 **
35957 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35958 */
35959 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35960 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35961
35962 /*
35963 ** Macros to enter and leave the PCache LRU mutex.
35964 */
35965 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
35966 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
@@ -36039,12 +36937,13 @@
36039 }
36040
36041 /*
36042 ** Free an allocated buffer obtained from pcache1Alloc().
36043 */
36044 static void pcache1Free(void *p){
36045 if( p==0 ) return;
 
36046 if( p>=pcache1.pStart && p<pcache1.pEnd ){
36047 PgFreeslot *pSlot;
36048 sqlite3_mutex_enter(pcache1.mutex);
36049 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
36050 pSlot = (PgFreeslot*)p;
@@ -36053,19 +36952,19 @@
36053 pcache1.nFreeSlot++;
36054 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
36055 assert( pcache1.nFreeSlot<=pcache1.nSlot );
36056 sqlite3_mutex_leave(pcache1.mutex);
36057 }else{
36058 int iSize;
36059 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
36060 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
36061 iSize = sqlite3MallocSize(p);
36062 sqlite3_mutex_enter(pcache1.mutex);
36063 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -iSize);
36064 sqlite3_mutex_leave(pcache1.mutex);
36065 sqlite3_free(p);
36066 }
 
36067 }
36068
36069 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36070 /*
36071 ** Return the size of a pcache allocation
@@ -36086,29 +36985,41 @@
36086
36087 /*
36088 ** Allocate a new page object initially associated with cache pCache.
36089 */
36090 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36091 int nByte = sizeof(PgHdr1) + pCache->szPage;
36092 PgHdr1 *p = 0;
36093 void *pPg;
36094
36095 /* The group mutex must be released before pcache1Alloc() is called. This
36096 ** is because it may call sqlite3_release_memory(), which assumes that
36097 ** this mutex is not held. */
36098 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36099 pcache1LeaveMutex(pCache->pGroup);
36100 pPg = pcache1Alloc(nByte);
 
 
 
 
 
 
 
 
 
 
 
36101 pcache1EnterMutex(pCache->pGroup);
36102
36103 if( pPg ){
36104 p = PAGE_TO_PGHDR1(pCache, pPg);
 
36105 if( pCache->bPurgeable ){
36106 pCache->pGroup->nCurrentPage++;
36107 }
 
36108 }
36109 return p;
36110 }
36111
36112 /*
36113 ** Free a page object allocated by pcache1AllocPage().
36114 **
@@ -36118,11 +37029,14 @@
36118 */
36119 static void pcache1FreePage(PgHdr1 *p){
36120 if( ALWAYS(p) ){
36121 PCache1 *pCache = p->pCache;
36122 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36123 pcache1Free(PGHDR1_TO_PAGE(p));
 
 
 
36124 if( pCache->bPurgeable ){
36125 pCache->pGroup->nCurrentPage--;
36126 }
36127 }
36128 }
@@ -36153,17 +37067,17 @@
36153 ** it is desirable to avoid allocating a new page cache entry because
36154 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
36155 ** for all page cache needs and we should not need to spill the
36156 ** allocation onto the heap.
36157 **
36158 ** Or, the heap is used for all page cache memory put the heap is
36159 ** under memory pressure, then again it is desirable to avoid
36160 ** allocating a new page cache entry in order to avoid stressing
36161 ** the heap even further.
36162 */
36163 static int pcache1UnderMemoryPressure(PCache1 *pCache){
36164 if( pcache1.nSlot && pCache->szPage<=pcache1.szSlot ){
36165 return pcache1.bUnderPressure;
36166 }else{
36167 return sqlite3HeapNearlyFull();
36168 }
36169 }
@@ -36350,11 +37264,11 @@
36350 /*
36351 ** Implementation of the sqlite3_pcache.xCreate method.
36352 **
36353 ** Allocate a new cache.
36354 */
36355 static sqlite3_pcache *pcache1Create(int szPage, int bPurgeable){
36356 PCache1 *pCache; /* The newly created page cache */
36357 PGroup *pGroup; /* The group the new page cache will belong to */
36358 int sz; /* Bytes of memory required to allocate the new cache */
36359
36360 /*
@@ -36372,10 +37286,13 @@
36372 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
36373 const int separateCache = 0;
36374 #else
36375 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
36376 #endif
 
 
 
36377
36378 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
36379 pCache = (PCache1 *)sqlite3_malloc(sz);
36380 if( pCache ){
36381 memset(pCache, 0, sz);
@@ -36385,10 +37302,11 @@
36385 }else{
36386 pGroup = &pcache1.grp;
36387 }
36388 pCache->pGroup = pGroup;
36389 pCache->szPage = szPage;
 
36390 pCache->bPurgeable = (bPurgeable ? 1 : 0);
36391 if( bPurgeable ){
36392 pCache->nMin = 10;
36393 pcache1EnterMutex(pGroup);
36394 pGroup->nMinPage += pCache->nMin;
@@ -36415,10 +37333,29 @@
36415 pCache->n90pct = pCache->nMax*9/10;
36416 pcache1EnforceMaxPage(pGroup);
36417 pcache1LeaveMutex(pGroup);
36418 }
36419 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36420
36421 /*
36422 ** Implementation of the sqlite3_pcache.xPagecount method.
36423 */
36424 static int pcache1Pagecount(sqlite3_pcache *p){
@@ -36441,11 +37378,11 @@
36441 ** means to try really hard to allocate a new page.
36442 **
36443 ** For a non-purgeable cache (a cache used as the storage for an in-memory
36444 ** database) there is really no difference between createFlag 1 and 2. So
36445 ** the calling function (pcache.c) will never have a createFlag of 1 on
36446 ** a non-purgable cache.
36447 **
36448 ** There are three different approaches to obtaining space for a page,
36449 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
36450 **
36451 ** 1. Regardless of the value of createFlag, the cache is searched for a
@@ -36482,12 +37419,16 @@
36482 ** size, return the recycled buffer. Otherwise, free the buffer and
36483 ** proceed to step 5.
36484 **
36485 ** 5. Otherwise, allocate and return a new page buffer.
36486 */
36487 static void *pcache1Fetch(sqlite3_pcache *p, unsigned int iKey, int createFlag){
36488 int nPinned;
 
 
 
 
36489 PCache1 *pCache = (PCache1 *)p;
36490 PGroup *pGroup;
36491 PgHdr1 *pPage = 0;
36492
36493 assert( pCache->bPurgeable || createFlag!=1 );
@@ -36517,19 +37458,18 @@
36517 */
36518 #ifdef SQLITE_MUTEX_OMIT
36519 pGroup = pCache->pGroup;
36520 #endif
36521
36522
36523 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
 
36524 nPinned = pCache->nPage - pCache->nRecyclable;
36525 assert( nPinned>=0 );
36526 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
36527 assert( pCache->n90pct == pCache->nMax*9/10 );
36528 if( createFlag==1 && (
36529 nPinned>=pGroup->mxPinned
36530 || nPinned>=(int)pCache->n90pct
36531 || pcache1UnderMemoryPressure(pCache)
36532 )){
36533 goto fetch_out;
36534 }
36535
@@ -36541,20 +37481,28 @@
36541 if( pCache->bPurgeable && pGroup->pLruTail && (
36542 (pCache->nPage+1>=pCache->nMax)
36543 || pGroup->nCurrentPage>=pGroup->nMaxPage
36544 || pcache1UnderMemoryPressure(pCache)
36545 )){
36546 PCache1 *pOtherCache;
36547 pPage = pGroup->pLruTail;
36548 pcache1RemoveFromHash(pPage);
36549 pcache1PinPage(pPage);
36550 if( (pOtherCache = pPage->pCache)->szPage!=pCache->szPage ){
 
 
 
 
 
 
 
 
 
36551 pcache1FreePage(pPage);
36552 pPage = 0;
36553 }else{
36554 pGroup->nCurrentPage -=
36555 (pOtherCache->bPurgeable - pCache->bPurgeable);
36556 }
36557 }
36558
36559 /* Step 5. If a usable page buffer has still not been found,
36560 ** attempt to allocate a new one.
@@ -36571,31 +37519,35 @@
36571 pPage->iKey = iKey;
36572 pPage->pNext = pCache->apHash[h];
36573 pPage->pCache = pCache;
36574 pPage->pLruPrev = 0;
36575 pPage->pLruNext = 0;
36576 *(void **)(PGHDR1_TO_PAGE(pPage)) = 0;
36577 pCache->apHash[h] = pPage;
36578 }
36579
36580 fetch_out:
36581 if( pPage && iKey>pCache->iMaxKey ){
36582 pCache->iMaxKey = iKey;
36583 }
36584 pcache1LeaveMutex(pGroup);
36585 return (pPage ? PGHDR1_TO_PAGE(pPage) : 0);
36586 }
36587
36588
36589 /*
36590 ** Implementation of the sqlite3_pcache.xUnpin method.
36591 **
36592 ** Mark a page as unpinned (eligible for asynchronous recycling).
36593 */
36594 static void pcache1Unpin(sqlite3_pcache *p, void *pPg, int reuseUnlikely){
 
 
 
 
36595 PCache1 *pCache = (PCache1 *)p;
36596 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36597 PGroup *pGroup = pCache->pGroup;
36598
36599 assert( pPage->pCache==pCache );
36600 pcache1EnterMutex(pGroup);
36601
@@ -36627,16 +37579,16 @@
36627 /*
36628 ** Implementation of the sqlite3_pcache.xRekey method.
36629 */
36630 static void pcache1Rekey(
36631 sqlite3_pcache *p,
36632 void *pPg,
36633 unsigned int iOld,
36634 unsigned int iNew
36635 ){
36636 PCache1 *pCache = (PCache1 *)p;
36637 PgHdr1 *pPage = PAGE_TO_PGHDR1(pCache, pPg);
36638 PgHdr1 **pp;
36639 unsigned int h;
36640 assert( pPage->iKey==iOld );
36641 assert( pPage->pCache==pCache );
36642
@@ -36686,11 +37638,13 @@
36686 PCache1 *pCache = (PCache1 *)p;
36687 PGroup *pGroup = pCache->pGroup;
36688 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
36689 pcache1EnterMutex(pGroup);
36690 pcache1TruncateUnsafe(pCache, 0);
 
36691 pGroup->nMaxPage -= pCache->nMax;
 
36692 pGroup->nMinPage -= pCache->nMin;
36693 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
36694 pcache1EnforceMaxPage(pGroup);
36695 pcache1LeaveMutex(pGroup);
36696 sqlite3_free(pCache->apHash);
@@ -36701,11 +37655,12 @@
36701 ** This function is called during initialization (sqlite3_initialize()) to
36702 ** install the default pluggable cache module, assuming the user has not
36703 ** already provided an alternative.
36704 */
36705 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
36706 static const sqlite3_pcache_methods defaultMethods = {
 
36707 0, /* pArg */
36708 pcache1Init, /* xInit */
36709 pcache1Shutdown, /* xShutdown */
36710 pcache1Create, /* xCreate */
36711 pcache1Cachesize, /* xCachesize */
@@ -36712,13 +37667,14 @@
36712 pcache1Pagecount, /* xPagecount */
36713 pcache1Fetch, /* xFetch */
36714 pcache1Unpin, /* xUnpin */
36715 pcache1Rekey, /* xRekey */
36716 pcache1Truncate, /* xTruncate */
36717 pcache1Destroy /* xDestroy */
 
36718 };
36719 sqlite3_config(SQLITE_CONFIG_PCACHE, &defaultMethods);
36720 }
36721
36722 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36723 /*
36724 ** This function is called to free superfluous dynamically allocated memory
@@ -36735,11 +37691,14 @@
36735 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36736 if( pcache1.pStart==0 ){
36737 PgHdr1 *p;
36738 pcache1EnterMutex(&pcache1.grp);
36739 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
36740 nFree += pcache1MemSize(PGHDR1_TO_PAGE(p));
 
 
 
36741 pcache1PinPage(p);
36742 pcache1RemoveFromHash(p);
36743 pcache1FreePage(p);
36744 }
36745 pcache1LeaveMutex(&pcache1.grp);
@@ -36763,12 +37722,12 @@
36763 int nRecyclable = 0;
36764 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
36765 nRecyclable++;
36766 }
36767 *pnCurrent = pcache1.grp.nCurrentPage;
36768 *pnMax = pcache1.grp.nMaxPage;
36769 *pnMin = pcache1.grp.nMinPage;
36770 *pnRecyclable = nRecyclable;
36771 }
36772 #endif
36773
36774 /************** End of pcache1.c *********************************************/
@@ -37238,10 +38197,16 @@
37238
37239 #ifndef _WAL_H_
37240 #define _WAL_H_
37241
37242
 
 
 
 
 
 
37243 #ifdef SQLITE_OMIT_WAL
37244 # define sqlite3WalOpen(x,y,z) 0
37245 # define sqlite3WalLimit(x,y)
37246 # define sqlite3WalClose(w,x,y,z) 0
37247 # define sqlite3WalBeginReadTransaction(y,z) 0
@@ -37937,10 +38902,11 @@
37937 u8 useJournal; /* Use a rollback journal on this file */
37938 u8 noReadlock; /* Do not bother to obtain readlocks */
37939 u8 noSync; /* Do not sync the journal if true */
37940 u8 fullSync; /* Do extra syncs of the journal for robustness */
37941 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
 
37942 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
37943 u8 tempFile; /* zFilename is a temporary file */
37944 u8 readOnly; /* True for a read-only database */
37945 u8 memDb; /* True to inhibit all file I/O */
37946
@@ -38107,11 +39073,11 @@
38107 return (pPager->pWal!=0);
38108 }
38109 #else
38110 # define pagerUseWal(x) 0
38111 # define pagerRollbackWal(x) 0
38112 # define pagerWalFrames(v,w,x,y,z) 0
38113 # define pagerOpenWalIfPresent(z) SQLITE_OK
38114 # define pagerBeginReadTransaction(z) SQLITE_OK
38115 #endif
38116
38117 #ifndef NDEBUG
@@ -39806,14 +40772,13 @@
39806 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
39807 newSize = szPage*(i64)nPage;
39808 if( rc==SQLITE_OK && currentSize!=newSize ){
39809 if( currentSize>newSize ){
39810 rc = sqlite3OsTruncate(pPager->fd, newSize);
39811 }else{
39812 char *pTmp = pPager->pTmpSpace;
39813 memset(pTmp, 0, szPage);
39814 testcase( (newSize-szPage) < currentSize );
39815 testcase( (newSize-szPage) == currentSize );
39816 testcase( (newSize-szPage) > currentSize );
39817 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
39818 }
39819 if( rc==SQLITE_OK ){
@@ -39835,27 +40800,40 @@
39835 **
39836 ** Otherwise, for non-temporary files, the effective sector size is
39837 ** the value returned by the xSectorSize() method rounded up to 32 if
39838 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
39839 ** is greater than MAX_SECTOR_SIZE.
 
 
 
 
 
 
 
 
 
39840 */
39841 static void setSectorSize(Pager *pPager){
39842 assert( isOpen(pPager->fd) || pPager->tempFile );
39843
39844 if( !pPager->tempFile ){
 
 
 
39845 /* Sector size doesn't matter for temporary files. Also, the file
39846 ** may not have been opened yet, in which case the OsSectorSize()
39847 ** call will segfault.
39848 */
 
39849 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
39850 }
39851 if( pPager->sectorSize<32 ){
39852 pPager->sectorSize = 512;
39853 }
39854 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
39855 assert( MAX_SECTOR_SIZE>=512 );
39856 pPager->sectorSize = MAX_SECTOR_SIZE;
39857 }
39858 }
39859
39860 /*
39861 ** Playback the journal and thus restore the database file to
@@ -40054,14 +41032,15 @@
40054 /* Following a rollback, the database file should be back in its original
40055 ** state prior to the start of the transaction, so invoke the
40056 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
40057 ** assertion that the transaction counter was modified.
40058 */
40059 assert(
40060 pPager->fd->pMethods==0 ||
40061 sqlite3OsFileControl(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0)>=SQLITE_OK
40062 );
 
40063
40064 /* If this playback is happening automatically as a result of an IO or
40065 ** malloc error that occurred after the change-counter was updated but
40066 ** before the transaction was committed, then the change-counter
40067 ** modification may just have been reverted. If this happens in exclusive
@@ -40276,12 +41255,11 @@
40276 */
40277 static int pagerWalFrames(
40278 Pager *pPager, /* Pager object */
40279 PgHdr *pList, /* List of frames to log */
40280 Pgno nTruncate, /* Database size after this commit */
40281 int isCommit, /* True if this is a commit */
40282 int syncFlags /* Flags to pass to OsSync() (or 0) */
40283 ){
40284 int rc; /* Return code */
40285 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
40286 PgHdr *p; /* For looping over pages */
40287 #endif
@@ -40308,11 +41286,11 @@
40308 assert( pList );
40309 }
40310
40311 if( pList->pgno==1 ) pager_write_changecounter(pList);
40312 rc = sqlite3WalFrames(pPager->pWal,
40313 pPager->pageSize, pList, nTruncate, isCommit, syncFlags
40314 );
40315 if( rc==SQLITE_OK && pPager->pBackup ){
40316 PgHdr *p;
40317 for(p=pList; p; p=p->pDirty){
40318 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
@@ -40395,14 +41373,11 @@
40395 int rc = sqlite3OsFileSize(pPager->fd, &n);
40396 if( rc!=SQLITE_OK ){
40397 return rc;
40398 }
40399 }
40400 nPage = (Pgno)(n / pPager->pageSize);
40401 if( nPage==0 && n>0 ){
40402 nPage = 1;
40403 }
40404 }
40405
40406 /* If the current number of pages in the file is greater than the
40407 ** configured maximum pager number, increase the allowed limit so
40408 ** that the file can be read.
@@ -40588,17 +41563,17 @@
40588 ** previously rolled back out of the main journal (and are hence in pDone)
40589 ** will be skipped. Out-of-range pages are also skipped.
40590 */
40591 if( pSavepoint ){
40592 u32 ii; /* Loop counter */
40593 i64 offset = pSavepoint->iSubRec*(4+pPager->pageSize);
40594
40595 if( pagerUseWal(pPager) ){
40596 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
40597 }
40598 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
40599 assert( offset==ii*(4+pPager->pageSize) );
40600 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
40601 }
40602 assert( rc!=SQLITE_DONE );
40603 }
40604
@@ -40614,10 +41589,17 @@
40614 ** Change the maximum number of in-memory pages that are allowed.
40615 */
40616 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
40617 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
40618 }
 
 
 
 
 
 
 
40619
40620 /*
40621 ** Adjust the robustness of the database to damage due to OS crashes
40622 ** or power failures by changing the number of syncs()s when writing
40623 ** the rollback journal. There are three levels:
@@ -40681,10 +41663,14 @@
40681 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
40682 }else{
40683 pPager->syncFlags = SQLITE_SYNC_NORMAL;
40684 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
40685 }
 
 
 
 
40686 }
40687 #endif
40688
40689 /*
40690 ** The following global variable is incremented whenever the library
@@ -40818,11 +41804,11 @@
40818 if( !pNew ) rc = SQLITE_NOMEM;
40819 }
40820
40821 if( rc==SQLITE_OK ){
40822 pager_reset(pPager);
40823 pPager->dbSize = (Pgno)(nByte/pageSize);
40824 pPager->pageSize = pageSize;
40825 sqlite3PageFree(pPager->pTmpSpace);
40826 pPager->pTmpSpace = pNew;
40827 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
40828 }
@@ -41326,11 +42312,11 @@
41326 ** file size will be.
41327 */
41328 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
41329 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
41330 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
41331 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
41332 pPager->dbHintSize = pPager->dbSize;
41333 }
41334
41335 while( rc==SQLITE_OK && pList ){
41336 Pgno pgno = pList->pgno;
@@ -41435,11 +42421,11 @@
41435
41436 /* If the sub-journal was opened successfully (or was already open),
41437 ** write the journal record into the file. */
41438 if( rc==SQLITE_OK ){
41439 void *pData = pPg->pData;
41440 i64 offset = pPager->nSubRec*(4+pPager->pageSize);
41441 char *pData2;
41442
41443 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
41444 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
41445 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
@@ -41508,11 +42494,11 @@
41508 /* Write a single frame for this page to the log. */
41509 if( subjRequiresPage(pPg) ){
41510 rc = subjournalPage(pPg);
41511 }
41512 if( rc==SQLITE_OK ){
41513 rc = pagerWalFrames(pPager, pPg, 0, 0, 0);
41514 }
41515 }else{
41516
41517 /* Sync the journal file if required. */
41518 if( pPg->flags&PGHDR_NEED_SYNC
@@ -41667,11 +42653,12 @@
41667 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
41668 while( *z ){
41669 z += sqlite3Strlen30(z)+1;
41670 z += sqlite3Strlen30(z)+1;
41671 }
41672 nUri = &z[1] - zUri;
 
41673 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
41674 /* This branch is taken when the journal path required by
41675 ** the database being opened will be more than pVfs->mxPathname
41676 ** bytes in length. This means the database cannot be opened,
41677 ** as it will not be possible to open the journal file or even
@@ -41701,13 +42688,13 @@
41701 ROUND8(sizeof(*pPager)) + /* Pager structure */
41702 ROUND8(pcacheSize) + /* PCache object */
41703 ROUND8(pVfs->szOsFile) + /* The main db file */
41704 journalFileSize * 2 + /* The two journal files */
41705 nPathname + 1 + nUri + /* zFilename */
41706 nPathname + 8 + 1 /* zJournal */
41707 #ifndef SQLITE_OMIT_WAL
41708 + nPathname + 4 + 1 /* zWal */
41709 #endif
41710 );
41711 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
41712 if( !pPtr ){
41713 sqlite3_free(zPathname);
@@ -41726,16 +42713,16 @@
41726 assert( nPathname>0 );
41727 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
41728 memcpy(pPager->zFilename, zPathname, nPathname);
41729 memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
41730 memcpy(pPager->zJournal, zPathname, nPathname);
41731 memcpy(&pPager->zJournal[nPathname], "-journal", 8);
41732 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
41733 #ifndef SQLITE_OMIT_WAL
41734 pPager->zWal = &pPager->zJournal[nPathname+8+1];
41735 memcpy(pPager->zWal, zPathname, nPathname);
41736 memcpy(&pPager->zWal[nPathname], "-wal", 4);
41737 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
41738 #endif
41739 sqlite3_free(zPathname);
41740 }
41741 pPager->pVfs = pVfs;
@@ -41847,13 +42834,21 @@
41847 pPager->changeCountDone = pPager->tempFile;
41848 pPager->memDb = (u8)memDb;
41849 pPager->readOnly = (u8)readOnly;
41850 assert( useJournal || pPager->tempFile );
41851 pPager->noSync = pPager->tempFile;
41852 pPager->fullSync = pPager->noSync ?0:1;
41853 pPager->syncFlags = pPager->noSync ? 0 : SQLITE_SYNC_NORMAL;
41854 pPager->ckptSyncFlags = pPager->syncFlags;
 
 
 
 
 
 
 
 
41855 /* pPager->pFirst = 0; */
41856 /* pPager->pFirstSynced = 0; */
41857 /* pPager->pLast = 0; */
41858 pPager->nExtra = (u16)nExtra;
41859 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
@@ -42982,11 +43977,14 @@
42982 if( !pPager->noSync ){
42983 assert( !MEMDB );
42984 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
42985 }else if( isOpen(pPager->fd) ){
42986 assert( !MEMDB );
42987 sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, (void *)&rc);
 
 
 
42988 }
42989 return rc;
42990 }
42991
42992 /*
@@ -43079,13 +44077,11 @@
43079 pList = pPageOne;
43080 pList->pDirty = 0;
43081 }
43082 assert( rc==SQLITE_OK );
43083 if( ALWAYS(pList) ){
43084 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1,
43085 (pPager->fullSync ? pPager->syncFlags : 0)
43086 );
43087 }
43088 sqlite3PagerUnref(pPageOne);
43089 if( rc==SQLITE_OK ){
43090 sqlite3PcacheCleanAll(pPager->pPCache);
43091 }
@@ -43340,11 +44336,12 @@
43340 }else{
43341 rc = pager_playback(pPager, 0);
43342 }
43343
43344 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
43345 assert( rc==SQLITE_OK || rc==SQLITE_FULL || (rc&0xFF)==SQLITE_IOERR );
 
43346
43347 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
43348 ** cache. So call pager_error() on the way out to make any error persistent.
43349 */
43350 return pager_error(pPager, rc);
@@ -43980,10 +44977,19 @@
43980 ** sqlite3BackupUpdate() only.
43981 */
43982 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
43983 return &pPager->pBackup;
43984 }
 
 
 
 
 
 
 
 
 
43985
43986 #ifndef SQLITE_OMIT_WAL
43987 /*
43988 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
43989 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
@@ -44157,17 +45163,10 @@
44157 }
44158 }
44159 return rc;
44160 }
44161
44162 /*
44163 ** Unless this is an in-memory or temporary database, clear the pager cache.
44164 */
44165 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
44166 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
44167 }
44168
44169 #ifdef SQLITE_HAS_CODEC
44170 /*
44171 ** This function is called by the wal module when writing page content
44172 ** into the log file.
44173 **
@@ -44600,17 +45599,22 @@
44600 sqlite3_file *pDbFd; /* File handle for the database file */
44601 sqlite3_file *pWalFd; /* File handle for WAL file */
44602 u32 iCallback; /* Value to pass to log callback (or 0) */
44603 i64 mxWalSize; /* Truncate WAL to this size upon reset */
44604 int nWiData; /* Size of array apWiData */
 
44605 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
44606 u32 szPage; /* Database page size */
44607 i16 readLock; /* Which read lock is being held. -1 for none */
 
44608 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
44609 u8 writeLock; /* True if in a write transaction */
44610 u8 ckptLock; /* True if holding a checkpoint lock */
44611 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
 
 
 
44612 WalIndexHdr hdr; /* Wal-index header for current transaction */
44613 const char *zWalName; /* Name of WAL file */
44614 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
44615 #ifdef SQLITE_DEBUG
44616 u8 lockError; /* True if a locking error has occurred */
@@ -45279,10 +46283,11 @@
45279 int iFrame; /* Index of last frame read */
45280 i64 iOffset; /* Next offset to read from log file */
45281 int szPage; /* Page size according to the log */
45282 u32 magic; /* Magic value read from WAL header */
45283 u32 version; /* Magic value read from WAL header */
 
45284
45285 /* Read in the WAL header. */
45286 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
45287 if( rc!=SQLITE_OK ){
45288 goto recovery_error;
@@ -45337,18 +46342,18 @@
45337 /* Read all frames from the log file. */
45338 iFrame = 0;
45339 for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
45340 u32 pgno; /* Database page number for frame */
45341 u32 nTruncate; /* dbsize field from frame header */
45342 int isValid; /* True if this frame is valid */
45343
45344 /* Read and decode the next log frame. */
 
45345 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
45346 if( rc!=SQLITE_OK ) break;
45347 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
45348 if( !isValid ) break;
45349 rc = walIndexAppend(pWal, ++iFrame, pgno);
45350 if( rc!=SQLITE_OK ) break;
45351
45352 /* If nTruncate is non-zero, this is a commit record. */
45353 if( nTruncate ){
45354 pWal->hdr.mxFrame = iFrame;
@@ -45467,10 +46472,12 @@
45467 pRet->pWalFd = (sqlite3_file *)&pRet[1];
45468 pRet->pDbFd = pDbFd;
45469 pRet->readLock = -1;
45470 pRet->mxWalSize = mxWalSize;
45471 pRet->zWalName = zWalName;
 
 
45472 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
45473
45474 /* Open file handle on the write-ahead log file. */
45475 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
45476 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
@@ -45481,10 +46488,15 @@
45481 if( rc!=SQLITE_OK ){
45482 walIndexClose(pRet, 0);
45483 sqlite3OsClose(pRet->pWalFd);
45484 sqlite3_free(pRet);
45485 }else{
 
 
 
 
 
45486 *ppWal = pRet;
45487 WALTRACE(("WAL%d: opened\n", pRet));
45488 }
45489 return rc;
45490 }
@@ -45900,11 +46912,11 @@
45900 */
45901 if( rc==SQLITE_OK ){
45902 i64 nReq = ((i64)mxPage * szPage);
45903 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
45904 if( rc==SQLITE_OK && nSize<nReq ){
45905 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
45906 }
45907 }
45908
45909 /* Iterate through the contents of the WAL, copying data to the db file. */
45910 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
@@ -45966,10 +46978,28 @@
45966
45967 walcheckpoint_out:
45968 walIteratorFree(pIter);
45969 return rc;
45970 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45971
45972 /*
45973 ** Close a connection to a log file.
45974 */
45975 SQLITE_PRIVATE int sqlite3WalClose(
@@ -45990,27 +47020,44 @@
45990 **
45991 ** The EXCLUSIVE lock is not released before returning.
45992 */
45993 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
45994 if( rc==SQLITE_OK ){
45995 int bPersistWal = -1;
45996 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
45997 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
45998 }
45999 rc = sqlite3WalCheckpoint(
46000 pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
46001 );
46002 sqlite3OsFileControl(pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersistWal);
46003 if( rc==SQLITE_OK && bPersistWal!=1 ){
46004 isDelete = 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46005 }
46006 }
46007
46008 walIndexClose(pWal, isDelete);
46009 sqlite3OsClose(pWal->pWalFd);
46010 if( isDelete ){
 
46011 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
 
46012 }
46013 WALTRACE(("WAL%p: closed\n", pWal));
46014 sqlite3_free((void *)pWal->apWiData);
46015 sqlite3_free(pWal);
46016 }
@@ -46496,11 +47543,11 @@
46496 }
46497 nCollide = HASHTABLE_NSLOT;
46498 for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
46499 u32 iFrame = aHash[iKey] + iZero;
46500 if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
46501 assert( iFrame>iRead );
46502 iRead = iFrame;
46503 }
46504 if( (nCollide--)==0 ){
46505 return SQLITE_CORRUPT_BKPT;
46506 }
@@ -46608,10 +47655,11 @@
46608 */
46609 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
46610 if( pWal->writeLock ){
46611 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
46612 pWal->writeLock = 0;
 
46613 }
46614 return SQLITE_OK;
46615 }
46616
46617 /*
@@ -46703,10 +47751,11 @@
46703 walCleanupHash(pWal);
46704 }
46705
46706 return rc;
46707 }
 
46708
46709 /*
46710 ** This function is called just before writing a set of frames to the log
46711 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
46712 ** to the current log file, it is possible to overwrite the start of the
@@ -46741,27 +47790,10 @@
46741 ** to handle if this transaction is rolled back.
46742 */
46743 int i; /* Loop counter */
46744 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
46745
46746 /* Limit the size of WAL file if the journal_size_limit PRAGMA is
46747 ** set to a non-negative value. Log errors encountered
46748 ** during the truncation attempt. */
46749 if( pWal->mxWalSize>=0 ){
46750 i64 sz;
46751 int rx;
46752 sqlite3BeginBenignMalloc();
46753 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46754 if( rx==SQLITE_OK && (sz > pWal->mxWalSize) ){
46755 rx = sqlite3OsTruncate(pWal->pWalFd, pWal->mxWalSize);
46756 }
46757 sqlite3EndBenignMalloc();
46758 if( rx ){
46759 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46760 }
46761 }
46762
46763 pWal->nCkpt++;
46764 pWal->hdr.mxFrame = 0;
46765 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
46766 aSalt[1] = salt1;
46767 walIndexWriteHdr(pWal);
@@ -46785,10 +47817,78 @@
46785 testcase( rc==SQLITE_PROTOCOL );
46786 testcase( rc==SQLITE_OK );
46787 }
46788 return rc;
46789 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46790
46791 /*
46792 ** Write a set of frames to the log. The caller must hold the write-lock
46793 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
46794 */
@@ -46800,17 +47900,23 @@
46800 int isCommit, /* True if this is a commit */
46801 int sync_flags /* Flags to pass to OsSync() (or 0) */
46802 ){
46803 int rc; /* Used to catch return codes */
46804 u32 iFrame; /* Next frame address */
46805 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
46806 PgHdr *p; /* Iterator to run through pList with. */
46807 PgHdr *pLast = 0; /* Last frame in list */
46808 int nLast = 0; /* Number of extra copies of last page */
 
 
 
46809
46810 assert( pList );
46811 assert( pWal->writeLock );
 
 
 
 
46812
46813 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
46814 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
46815 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
46816 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
@@ -46835,91 +47941,103 @@
46835
46836 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
46837 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
46838 sqlite3Put4byte(&aWalHdr[8], szPage);
46839 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
46840 sqlite3_randomness(8, pWal->hdr.aSalt);
46841 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
46842 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
46843 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
46844 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
46845
46846 pWal->szPage = szPage;
46847 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
46848 pWal->hdr.aFrameCksum[0] = aCksum[0];
46849 pWal->hdr.aFrameCksum[1] = aCksum[1];
 
46850
46851 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
46852 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
46853 if( rc!=SQLITE_OK ){
46854 return rc;
46855 }
 
 
 
 
 
 
 
 
 
 
 
 
46856 }
46857 assert( (int)pWal->szPage==szPage );
46858
46859 /* Write the log file. */
 
 
 
 
 
 
 
 
 
46860 for(p=pList; p; p=p->pDirty){
46861 u32 nDbsize; /* Db-size field for frame header */
46862 i64 iOffset; /* Write offset in log file */
46863 void *pData;
46864
46865 iOffset = walFrameOffset(++iFrame, szPage);
46866 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46867
46868 /* Populate and write the frame header */
46869 nDbsize = (isCommit && p->pDirty==0) ? nTruncate : 0;
46870 #if defined(SQLITE_HAS_CODEC)
46871 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
46872 #else
46873 pData = p->pData;
46874 #endif
46875 walEncodeFrame(pWal, p->pgno, nDbsize, pData, aFrame);
46876 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46877 if( rc!=SQLITE_OK ){
46878 return rc;
46879 }
46880
46881 /* Write the page data */
46882 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset+sizeof(aFrame));
46883 if( rc!=SQLITE_OK ){
46884 return rc;
46885 }
46886 pLast = p;
46887 }
46888
46889 /* Sync the log file if the 'isSync' flag was specified. */
46890 if( sync_flags ){
46891 i64 iSegment = sqlite3OsSectorSize(pWal->pWalFd);
46892 i64 iOffset = walFrameOffset(iFrame+1, szPage);
46893
46894 assert( isCommit );
46895 assert( iSegment>0 );
46896
46897 iSegment = (((iOffset+iSegment-1)/iSegment) * iSegment);
46898 while( iOffset<iSegment ){
46899 void *pData;
46900 #if defined(SQLITE_HAS_CODEC)
46901 if( (pData = sqlite3PagerCodec(pLast))==0 ) return SQLITE_NOMEM;
46902 #else
46903 pData = pLast->pData;
46904 #endif
46905 walEncodeFrame(pWal, pLast->pgno, nTruncate, pData, aFrame);
46906 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46907 rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOffset);
46908 if( rc!=SQLITE_OK ){
46909 return rc;
46910 }
46911 iOffset += WAL_FRAME_HDRSIZE;
46912 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOffset);
46913 if( rc!=SQLITE_OK ){
46914 return rc;
46915 }
46916 nLast++;
46917 iOffset += szPage;
46918 }
46919
46920 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
 
 
 
 
 
 
 
 
 
46921 }
46922
46923 /* Append data to the wal-index. It is not necessary to lock the
46924 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
46925 ** guarantees that there are no other writers, and no data that may
@@ -46928,13 +48046,13 @@
46928 iFrame = pWal->hdr.mxFrame;
46929 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
46930 iFrame++;
46931 rc = walIndexAppend(pWal, iFrame, p->pgno);
46932 }
46933 while( nLast>0 && rc==SQLITE_OK ){
46934 iFrame++;
46935 nLast--;
46936 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
46937 }
46938
46939 if( rc==SQLITE_OK ){
46940 /* Update the private copy of the header. */
@@ -47436,10 +48554,11 @@
47436 u8 intKey; /* True if intkey flag is set */
47437 u8 leaf; /* True if leaf flag is set */
47438 u8 hasData; /* True if this page stores data */
47439 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
47440 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
 
47441 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
47442 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
47443 u16 cellOffset; /* Index in aData of first cell pointer */
47444 u16 nFree; /* Number of free bytes on the page */
47445 u16 nCell; /* Number of cells on this page, local and ovfl */
@@ -47448,10 +48567,12 @@
47448 u8 *pCell; /* Pointers to the body of the overflow cell */
47449 u16 idx; /* Insert this cell before idx-th non-overflow cell */
47450 } aOvfl[5];
47451 BtShared *pBt; /* Pointer to BtShared that this page is part of */
47452 u8 *aData; /* Pointer to disk image of the page data */
 
 
47453 DbPage *pDbPage; /* Pager page handle */
47454 Pgno pgno; /* Page number for this page */
47455 };
47456
47457 /*
@@ -47527,11 +48648,11 @@
47527 #define TRANS_WRITE 2
47528
47529 /*
47530 ** An instance of this object represents a single database file.
47531 **
47532 ** A single database file can be in use as the same time by two
47533 ** or more database connections. When two or more connections are
47534 ** sharing the same database file, each connection has it own
47535 ** private Btree object for the file and each of those Btrees points
47536 ** to this one BtShared object. BtShared.nRef is the number of
47537 ** connections currently sharing this database file.
@@ -47564,21 +48685,18 @@
47564 struct BtShared {
47565 Pager *pPager; /* The page cache */
47566 sqlite3 *db; /* Database connection currently using this Btree */
47567 BtCursor *pCursor; /* A list of all open cursors */
47568 MemPage *pPage1; /* First page of the database */
47569 u8 readOnly; /* True if the underlying file is readonly */
47570 u8 pageSizeFixed; /* True if the page size can no longer be changed */
47571 u8 secureDelete; /* True if secure_delete is enabled */
47572 u8 initiallyEmpty; /* Database is empty at start of transaction */
47573 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
47574 #ifndef SQLITE_OMIT_AUTOVACUUM
47575 u8 autoVacuum; /* True if auto-vacuum is enabled */
47576 u8 incrVacuum; /* True if incr-vacuum is enabled */
47577 #endif
47578 u8 inTransaction; /* Transaction state */
47579 u8 doNotUseWAL; /* If true, do not open write-ahead-log file */
 
47580 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
47581 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
47582 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
47583 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
47584 u32 pageSize; /* Total number of bytes on a page */
@@ -47592,16 +48710,25 @@
47592 #ifndef SQLITE_OMIT_SHARED_CACHE
47593 int nRef; /* Number of references to this structure */
47594 BtShared *pNext; /* Next on a list of sharable BtShared structs */
47595 BtLock *pLock; /* List of locks held on this shared-btree struct */
47596 Btree *pWriter; /* Btree with currently open write transaction */
47597 u8 isExclusive; /* True if pWriter has an EXCLUSIVE lock on the db */
47598 u8 isPending; /* If waiting for read-locks to clear */
47599 #endif
47600 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
47601 };
47602
 
 
 
 
 
 
 
 
 
 
 
47603 /*
47604 ** An instance of the following structure is used to hold information
47605 ** about a cell. The parseCellPtr() function fills in this structure
47606 ** based on information extract from the raw disk page.
47607 */
@@ -47633,11 +48760,11 @@
47633 ** b-tree within a database file.
47634 **
47635 ** The entry is identified by its MemPage and the index in
47636 ** MemPage.aCell[] of the entry.
47637 **
47638 ** A single database file can shared by two more database connections,
47639 ** but cursors cannot be shared. Each cursor is associated with a
47640 ** particular database connection identified BtCursor.pBtree.db.
47641 **
47642 ** Fields in this structure are accessed under the BtShared.mutex
47643 ** found at self->pBt->mutex.
@@ -47794,11 +48921,11 @@
47794 int mallocFailed; /* A memory allocation error has occurred */
47795 StrAccum errMsg; /* Accumulate the error message text here */
47796 };
47797
47798 /*
47799 ** Read or write a two- and four-byte big-endian integer values.
47800 */
47801 #define get2byte(x) ((x)[0]<<8 | (x)[1])
47802 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
47803 #define get4byte sqlite3Get4byte
47804 #define put4byte sqlite3Put4byte
@@ -48319,11 +49446,11 @@
48319 }
48320
48321 /* If some other connection is holding an exclusive lock, the
48322 ** requested lock may not be obtained.
48323 */
48324 if( pBt->pWriter!=p && pBt->isExclusive ){
48325 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
48326 return SQLITE_LOCKED_SHAREDCACHE;
48327 }
48328
48329 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
@@ -48340,11 +49467,11 @@
48340 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
48341 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
48342 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
48343 if( eLock==WRITE_LOCK ){
48344 assert( p==pBt->pWriter );
48345 pBt->isPending = 1;
48346 }
48347 return SQLITE_LOCKED_SHAREDCACHE;
48348 }
48349 }
48350 return SQLITE_OK;
@@ -48428,11 +49555,11 @@
48428 /*
48429 ** Release all the table locks (locks obtained via calls to
48430 ** the setSharedCacheTableLock() procedure) held by Btree object p.
48431 **
48432 ** This function assumes that Btree p has an open read or write
48433 ** transaction. If it does not, then the BtShared.isPending variable
48434 ** may be incorrectly cleared.
48435 */
48436 static void clearAllSharedCacheTableLocks(Btree *p){
48437 BtShared *pBt = p->pBt;
48438 BtLock **ppIter = &pBt->pLock;
@@ -48441,11 +49568,11 @@
48441 assert( p->sharable || 0==*ppIter );
48442 assert( p->inTrans>0 );
48443
48444 while( *ppIter ){
48445 BtLock *pLock = *ppIter;
48446 assert( pBt->isExclusive==0 || pBt->pWriter==pLock->pBtree );
48447 assert( pLock->pBtree->inTrans>=pLock->eLock );
48448 if( pLock->pBtree==p ){
48449 *ppIter = pLock->pNext;
48450 assert( pLock->iTable!=1 || pLock==&p->lock );
48451 if( pLock->iTable!=1 ){
@@ -48454,26 +49581,25 @@
48454 }else{
48455 ppIter = &pLock->pNext;
48456 }
48457 }
48458
48459 assert( pBt->isPending==0 || pBt->pWriter );
48460 if( pBt->pWriter==p ){
48461 pBt->pWriter = 0;
48462 pBt->isExclusive = 0;
48463 pBt->isPending = 0;
48464 }else if( pBt->nTransaction==2 ){
48465 /* This function is called when Btree p is concluding its
48466 ** transaction. If there currently exists a writer, and p is not
48467 ** that writer, then the number of locks held by connections other
48468 ** than the writer must be about to drop to zero. In this case
48469 ** set the isPending flag to 0.
48470 **
48471 ** If there is not currently a writer, then BtShared.isPending must
48472 ** be zero already. So this next line is harmless in that case.
48473 */
48474 pBt->isPending = 0;
48475 }
48476 }
48477
48478 /*
48479 ** This function changes all write-locks held by Btree p into read-locks.
@@ -48481,12 +49607,11 @@
48481 static void downgradeAllSharedCacheTableLocks(Btree *p){
48482 BtShared *pBt = p->pBt;
48483 if( pBt->pWriter==p ){
48484 BtLock *pLock;
48485 pBt->pWriter = 0;
48486 pBt->isExclusive = 0;
48487 pBt->isPending = 0;
48488 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
48489 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
48490 pLock->eLock = READ_LOCK;
48491 }
48492 }
@@ -48935,11 +50060,11 @@
48935 ** to the cell content.
48936 **
48937 ** This routine works only for pages that do not contain overflow cells.
48938 */
48939 #define findCell(P,I) \
48940 ((P)->aData + ((P)->maskPage & get2byte(&(P)->aData[(P)->cellOffset+2*(I)])))
48941 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
48942
48943
48944 /*
48945 ** This a more complex version of findCell() that works for
@@ -49340,11 +50465,11 @@
49340 assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
49341 assert( (start + size) <= (int)pPage->pBt->usableSize );
49342 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
49343 assert( size>=0 ); /* Minimum cell size is 4 */
49344
49345 if( pPage->pBt->secureDelete ){
49346 /* Overwrite deleted information with zeros when the secure_delete
49347 ** option is enabled */
49348 memset(&data[start], 0, size);
49349 }
49350
@@ -49443,10 +50568,11 @@
49443 pPage->maxLocal = pBt->maxLocal;
49444 pPage->minLocal = pBt->minLocal;
49445 }else{
49446 return SQLITE_CORRUPT_BKPT;
49447 }
 
49448 return SQLITE_OK;
49449 }
49450
49451 /*
49452 ** Initialize the auxiliary information for a disk block.
@@ -49485,10 +50611,12 @@
49485 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
49486 pPage->maskPage = (u16)(pBt->pageSize - 1);
49487 pPage->nOverflow = 0;
49488 usableSize = pBt->usableSize;
49489 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
 
 
49490 top = get2byteNotZero(&data[hdr+5]);
49491 pPage->nCell = get2byte(&data[hdr+3]);
49492 if( pPage->nCell>MX_CELL(pBt) ){
49493 /* To many cells for a single page. The page must be corrupt */
49494 return SQLITE_CORRUPT_BKPT;
@@ -49576,11 +50704,11 @@
49576 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
49577 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
49578 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
49579 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
49580 assert( sqlite3_mutex_held(pBt->mutex) );
49581 if( pBt->secureDelete ){
49582 memset(&data[hdr], 0, pBt->usableSize - hdr);
49583 }
49584 data[hdr] = (char)flags;
49585 first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
49586 memset(&data[hdr+1], 0, 4);
@@ -49588,10 +50716,12 @@
49588 put2byte(&data[hdr+5], pBt->usableSize);
49589 pPage->nFree = (u16)(pBt->usableSize - first);
49590 decodeFlags(pPage, flags);
49591 pPage->hdrOffset = hdr;
49592 pPage->cellOffset = first;
 
 
49593 pPage->nOverflow = 0;
49594 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
49595 pPage->maskPage = (u16)(pBt->pageSize - 1);
49596 pPage->nCell = 0;
49597 pPage->isInit = 1;
@@ -49848,11 +50978,16 @@
49848 p->sharable = 1;
49849 if( !zFullPathname ){
49850 sqlite3_free(p);
49851 return SQLITE_NOMEM;
49852 }
49853 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
 
 
 
 
 
49854 #if SQLITE_THREADSAFE
49855 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
49856 sqlite3_mutex_enter(mutexOpen);
49857 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49858 sqlite3_mutex_enter(mutexShared);
@@ -49922,13 +51057,13 @@
49922 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
49923 p->pBt = pBt;
49924
49925 pBt->pCursor = 0;
49926 pBt->pPage1 = 0;
49927 pBt->readOnly = sqlite3PagerIsreadonly(pBt->pPager);
49928 #ifdef SQLITE_SECURE_DELETE
49929 pBt->secureDelete = 1;
49930 #endif
49931 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
49932 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
49933 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
49934 pBt->pageSize = 0;
@@ -49945,11 +51080,11 @@
49945 }
49946 #endif
49947 nReserve = 0;
49948 }else{
49949 nReserve = zDbHeader[20];
49950 pBt->pageSizeFixed = 1;
49951 #ifndef SQLITE_OMIT_AUTOVACUUM
49952 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
49953 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
49954 #endif
49955 }
@@ -50233,19 +51368,19 @@
50233 ** at the beginning of a page.
50234 **
50235 ** If parameter nReserve is less than zero, then the number of reserved
50236 ** bytes per page is left unchanged.
50237 **
50238 ** If the iFix!=0 then the pageSizeFixed flag is set so that the page size
50239 ** and autovacuum mode can no longer be changed.
50240 */
50241 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
50242 int rc = SQLITE_OK;
50243 BtShared *pBt = p->pBt;
50244 assert( nReserve>=-1 && nReserve<=255 );
50245 sqlite3BtreeEnter(p);
50246 if( pBt->pageSizeFixed ){
50247 sqlite3BtreeLeave(p);
50248 return SQLITE_READONLY;
50249 }
50250 if( nReserve<0 ){
50251 nReserve = pBt->pageSize - pBt->usableSize;
@@ -50258,11 +51393,11 @@
50258 pBt->pageSize = (u32)pageSize;
50259 freeTempSpace(pBt);
50260 }
50261 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
50262 pBt->usableSize = pBt->pageSize - (u16)nReserve;
50263 if( iFix ) pBt->pageSizeFixed = 1;
50264 sqlite3BtreeLeave(p);
50265 return rc;
50266 }
50267
50268 /*
@@ -50298,22 +51433,23 @@
50298 sqlite3BtreeLeave(p);
50299 return n;
50300 }
50301
50302 /*
50303 ** Set the secureDelete flag if newFlag is 0 or 1. If newFlag is -1,
50304 ** then make no changes. Always return the value of the secureDelete
50305 ** setting after the change.
50306 */
50307 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
50308 int b;
50309 if( p==0 ) return 0;
50310 sqlite3BtreeEnter(p);
50311 if( newFlag>=0 ){
50312 p->pBt->secureDelete = (newFlag!=0) ? 1 : 0;
 
50313 }
50314 b = p->pBt->secureDelete;
50315 sqlite3BtreeLeave(p);
50316 return b;
50317 }
50318 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
50319
@@ -50330,11 +51466,11 @@
50330 BtShared *pBt = p->pBt;
50331 int rc = SQLITE_OK;
50332 u8 av = (u8)autoVacuum;
50333
50334 sqlite3BtreeEnter(p);
50335 if( pBt->pageSizeFixed && (av ?1:0)!=pBt->autoVacuum ){
50336 rc = SQLITE_READONLY;
50337 }else{
50338 pBt->autoVacuum = av ?1:0;
50339 pBt->incrVacuum = av==2 ?1:0;
50340 }
@@ -50404,18 +51540,18 @@
50404 goto page1_init_failed;
50405 }
50406
50407 #ifdef SQLITE_OMIT_WAL
50408 if( page1[18]>1 ){
50409 pBt->readOnly = 1;
50410 }
50411 if( page1[19]>1 ){
50412 goto page1_init_failed;
50413 }
50414 #else
50415 if( page1[18]>2 ){
50416 pBt->readOnly = 1;
50417 }
50418 if( page1[19]>2 ){
50419 goto page1_init_failed;
50420 }
50421
@@ -50425,11 +51561,11 @@
50425 ** The caller detects this and calls this function again. This is
50426 ** required as the version of page 1 currently in the page1 buffer
50427 ** may not be the latest version - there may be a newer one in the log
50428 ** file.
50429 */
50430 if( page1[19]==2 && pBt->doNotUseWAL==0 ){
50431 int isOpen = 0;
50432 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
50433 if( rc!=SQLITE_OK ){
50434 goto page1_init_failed;
50435 }else if( isOpen==0 ){
@@ -50502,10 +51638,15 @@
50502 */
50503 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
50504 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
50505 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
50506 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
 
 
 
 
 
50507 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
50508 pBt->pPage1 = pPage1;
50509 pBt->nPage = nPage;
50510 return SQLITE_OK;
50511
@@ -50565,11 +51706,11 @@
50565 data[21] = 64;
50566 data[22] = 32;
50567 data[23] = 32;
50568 memset(&data[24], 0, 100-24);
50569 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
50570 pBt->pageSizeFixed = 1;
50571 #ifndef SQLITE_OMIT_AUTOVACUUM
50572 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
50573 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
50574 put4byte(&data[36 + 4*4], pBt->autoVacuum);
50575 put4byte(&data[36 + 7*4], pBt->incrVacuum);
@@ -50629,21 +51770,23 @@
50629 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
50630 goto trans_begun;
50631 }
50632
50633 /* Write transactions are not possible on a read-only database */
50634 if( pBt->readOnly && wrflag ){
50635 rc = SQLITE_READONLY;
50636 goto trans_begun;
50637 }
50638
50639 #ifndef SQLITE_OMIT_SHARED_CACHE
50640 /* If another database handle has already opened a write transaction
50641 ** on this shared-btree structure and a second write transaction is
50642 ** requested, return SQLITE_LOCKED.
50643 */
50644 if( (wrflag && pBt->inTransaction==TRANS_WRITE) || pBt->isPending ){
 
 
50645 pBlock = pBt->pWriter->db;
50646 }else if( wrflag>1 ){
50647 BtLock *pIter;
50648 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
50649 if( pIter->pBtree!=p ){
@@ -50663,11 +51806,12 @@
50663 ** page 1. So if some other shared-cache client already has a write-lock
50664 ** on page 1, the transaction cannot be opened. */
50665 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
50666 if( SQLITE_OK!=rc ) goto trans_begun;
50667
50668 pBt->initiallyEmpty = (u8)(pBt->nPage==0);
 
50669 do {
50670 /* Call lockBtree() until either pBt->pPage1 is populated or
50671 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
50672 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
50673 ** reading page 1 it discovers that the page-size of the database
@@ -50675,11 +51819,11 @@
50675 ** pBt->pageSize to the page-size of the file on disk.
50676 */
50677 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
50678
50679 if( rc==SQLITE_OK && wrflag ){
50680 if( pBt->readOnly ){
50681 rc = SQLITE_READONLY;
50682 }else{
50683 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
50684 if( rc==SQLITE_OK ){
50685 rc = newDatabase(pBt);
@@ -50712,11 +51856,12 @@
50712 if( wrflag ){
50713 MemPage *pPage1 = pBt->pPage1;
50714 #ifndef SQLITE_OMIT_SHARED_CACHE
50715 assert( !pBt->pWriter );
50716 pBt->pWriter = p;
50717 pBt->isExclusive = (u8)(wrflag>1);
 
50718 #endif
50719
50720 /* If the db-size header field is incorrect (as it may be if an old
50721 ** client has been writing the database file), update it now. Doing
50722 ** this sooner rather than later means the database size can safely
@@ -51441,11 +52586,11 @@
51441 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
51442 int rc;
51443 BtShared *pBt = p->pBt;
51444 sqlite3BtreeEnter(p);
51445 assert( p->inTrans==TRANS_WRITE );
51446 assert( pBt->readOnly==0 );
51447 assert( iStatement>0 );
51448 assert( iStatement>p->db->nSavepoint );
51449 assert( pBt->inTransaction==TRANS_WRITE );
51450 /* At the pager level, a statement transaction is a savepoint with
51451 ** an index greater than all savepoints created explicitly using
@@ -51476,11 +52621,13 @@
51476 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
51477 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
51478 sqlite3BtreeEnter(p);
51479 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
51480 if( rc==SQLITE_OK ){
51481 if( iSavepoint<0 && pBt->initiallyEmpty ) pBt->nPage = 0;
 
 
51482 rc = newDatabase(pBt);
51483 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
51484
51485 /* The database size was written into the offset 28 of the header
51486 ** when the transaction started, so we know that the value at offset
@@ -51546,11 +52693,11 @@
51546 /* Assert that the caller has opened the required transaction. */
51547 assert( p->inTrans>TRANS_NONE );
51548 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
51549 assert( pBt->pPage1 && pBt->pPage1->aData );
51550
51551 if( NEVER(wrFlag && pBt->readOnly) ){
51552 return SQLITE_READONLY;
51553 }
51554 if( iTable==1 && btreePagecount(pBt)==0 ){
51555 assert( wrFlag==0 );
51556 iTable = 0;
@@ -52046,11 +53193,11 @@
52046 && pBt->pPage1->aData[19]==0x01 /* (5) */
52047 ){
52048 u8 aSave[4];
52049 u8 *aWrite = &pBuf[-4];
52050 memcpy(aSave, aWrite, 4);
52051 rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
52052 nextPage = get4byte(aWrite);
52053 memcpy(aWrite, aSave, 4);
52054 }else
52055 #endif
52056
@@ -52626,20 +53773,25 @@
52626 ** the entire cell by checking for the cases where the record is
52627 ** stored entirely within the b-tree page by inspecting the first
52628 ** 2 bytes of the cell.
52629 */
52630 int nCell = pCell[0];
52631 if( !(nCell & 0x80) && nCell<=pPage->maxLocal ){
 
 
52632 /* This branch runs if the record-size field of the cell is a
52633 ** single byte varint and the record fits entirely on the main
52634 ** b-tree page. */
 
52635 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
52636 }else if( !(pCell[1] & 0x80)
52637 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
 
52638 ){
52639 /* The record-size field is a 2 byte varint and the record
52640 ** fits entirely on the main b-tree page. */
 
52641 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
52642 }else{
52643 /* The record flows over onto one or more overflow pages. In
52644 ** this case the whole cell needs to be parsed, a buffer allocated
52645 ** and accessPayload() used to retrieve the record into the
@@ -53177,11 +54329,11 @@
53177 rc = sqlite3PagerWrite(pPage1->pDbPage);
53178 if( rc ) goto freepage_out;
53179 nFree = get4byte(&pPage1->aData[36]);
53180 put4byte(&pPage1->aData[36], nFree+1);
53181
53182 if( pBt->secureDelete ){
53183 /* If the secure_delete option is enabled, then
53184 ** always fully overwrite deleted information with zeros.
53185 */
53186 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
53187 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
@@ -53238,11 +54390,11 @@
53238 */
53239 rc = sqlite3PagerWrite(pTrunk->pDbPage);
53240 if( rc==SQLITE_OK ){
53241 put4byte(&pTrunk->aData[4], nLeaf+1);
53242 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
53243 if( pPage && !pBt->secureDelete ){
53244 sqlite3PagerDontWrite(pPage->pDbPage);
53245 }
53246 rc = btreeSetHasContent(pBt, iPage);
53247 }
53248 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
@@ -53530,11 +54682,11 @@
53530 assert( idx>=0 && idx<pPage->nCell );
53531 assert( sz==cellSize(pPage, idx) );
53532 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53533 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53534 data = pPage->aData;
53535 ptr = &data[pPage->cellOffset + 2*idx];
53536 pc = get2byte(ptr);
53537 hdr = pPage->hdrOffset;
53538 testcase( pc==get2byte(&data[hdr+5]) );
53539 testcase( pc+sz==pPage->pBt->usableSize );
53540 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
@@ -53544,11 +54696,11 @@
53544 rc = freeSpace(pPage, pc, sz);
53545 if( rc ){
53546 *pRC = rc;
53547 return;
53548 }
53549 endPtr = &data[pPage->cellOffset + 2*pPage->nCell - 2];
53550 assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 ); /* ptr is always 2-byte aligned */
53551 while( ptr<endPtr ){
53552 *(u16*)ptr = *(u16*)&ptr[2];
53553 ptr += 2;
53554 }
@@ -53686,11 +54838,11 @@
53686
53687 /* Check that the page has just been zeroed by zeroPage() */
53688 assert( pPage->nCell==0 );
53689 assert( get2byteNotZero(&data[hdr+5])==nUsable );
53690
53691 pCellptr = &data[pPage->cellOffset + nCell*2];
53692 cellbody = nUsable;
53693 for(i=nCell-1; i>=0; i--){
53694 u16 sz = aSize[i];
53695 pCellptr -= 2;
53696 cellbody -= sz;
@@ -54079,11 +55231,11 @@
54079 ** But not if we are in secure-delete mode. In secure-delete mode,
54080 ** the dropCell() routine will overwrite the entire cell with zeroes.
54081 ** In this case, temporarily copy the cell into the aOvflSpace[]
54082 ** buffer. It will be copied out again as soon as the aSpace[] buffer
54083 ** is allocated. */
54084 if( pBt->secureDelete ){
54085 int iOff;
54086
54087 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
54088 if( (iOff+szNew[i])>(int)pBt->usableSize ){
54089 rc = SQLITE_CORRUPT_BKPT;
@@ -54264,12 +55416,18 @@
54264 }
54265
54266 /* Either we found one or more cells (cntnew[0])>0) or pPage is
54267 ** a virtual root page. A virtual root page is when the real root
54268 ** page is page 1 and we are the only child of that page.
 
 
 
 
54269 */
 
54270 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
 
54271
54272 TRACE(("BALANCE: old: %d %d %d ",
54273 apOld[0]->pgno,
54274 nOld>=2 ? apOld[1]->pgno : 0,
54275 nOld>=3 ? apOld[2]->pgno : 0
@@ -54815,11 +55973,12 @@
54815 assert( pCur->skipNext!=SQLITE_OK );
54816 return pCur->skipNext;
54817 }
54818
54819 assert( cursorHoldsMutex(pCur) );
54820 assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE && !pBt->readOnly );
 
54821 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
54822
54823 /* Assert that the caller has been consistent. If this cursor was opened
54824 ** expecting an index b-tree, then the caller should be inserting blob
54825 ** keys with no associated data. If the cursor was opened expecting an
@@ -54944,11 +56103,11 @@
54944 int iCellIdx; /* Index of cell to delete */
54945 int iCellDepth; /* Depth of node containing pCell */
54946
54947 assert( cursorHoldsMutex(pCur) );
54948 assert( pBt->inTransaction==TRANS_WRITE );
54949 assert( !pBt->readOnly );
54950 assert( pCur->wrFlag );
54951 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
54952 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
54953
54954 if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
@@ -55065,11 +56224,11 @@
55065 int rc;
55066 int ptfFlags; /* Page-type flage for the root page of new table */
55067
55068 assert( sqlite3BtreeHoldsMutex(p) );
55069 assert( pBt->inTransaction==TRANS_WRITE );
55070 assert( !pBt->readOnly );
55071
55072 #ifdef SQLITE_OMIT_AUTOVACUUM
55073 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
55074 if( rc ){
55075 return rc;
@@ -55439,11 +56598,13 @@
55439 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
55440
55441 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
55442 ** database, mark the database as read-only. */
55443 #ifdef SQLITE_OMIT_AUTOVACUUM
55444 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ) pBt->readOnly = 1;
 
 
55445 #endif
55446
55447 sqlite3BtreeLeave(p);
55448 }
55449
@@ -56239,11 +57400,12 @@
56239 ** (e) the cursor points at a valid row of an intKey table.
56240 */
56241 if( !pCsr->wrFlag ){
56242 return SQLITE_READONLY;
56243 }
56244 assert( !pCsr->pBt->readOnly && pCsr->pBt->inTransaction==TRANS_WRITE );
 
56245 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
56246 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
56247 assert( pCsr->apPage[pCsr->iPage]->intKey );
56248
56249 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
@@ -56279,11 +57441,12 @@
56279 assert( iVersion==1 || iVersion==2 );
56280
56281 /* If setting the version fields to 1, do not automatically open the
56282 ** WAL connection, even if the version fields are currently set to 2.
56283 */
56284 pBt->doNotUseWAL = (u8)(iVersion==1);
 
56285
56286 rc = sqlite3BtreeBeginTrans(pBtree, 0);
56287 if( rc==SQLITE_OK ){
56288 u8 *aData = pBt->pPage1->aData;
56289 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
@@ -56296,11 +57459,11 @@
56296 }
56297 }
56298 }
56299 }
56300
56301 pBt->doNotUseWAL = 0;
56302 return rc;
56303 }
56304
56305 /************** End of btree.c ***********************************************/
56306 /************** Begin file backup.c ******************************************/
@@ -56980,11 +58143,13 @@
56980
56981 assert( sqlite3BtreeIsInTrans(pTo) );
56982 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
56983 if( pFd->pMethods ){
56984 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
56985 sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
 
 
56986 }
56987
56988 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
56989 ** to 0. This is used by the implementations of sqlite3_backup_step()
56990 ** and sqlite3_backup_finish() to detect that they are being called
@@ -57005,16 +58170,17 @@
57005 */
57006 sqlite3_backup_step(&b, 0x7FFFFFFF);
57007 assert( b.rc!=SQLITE_OK );
57008 rc = sqlite3_backup_finish(&b);
57009 if( rc==SQLITE_OK ){
57010 pTo->pBt->pageSizeFixed = 0;
57011 }else{
57012 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
57013 }
57014
57015 assert( sqlite3BtreeIsInTrans(pTo)==0 );
 
57016 sqlite3BtreeLeave(pFrom);
57017 sqlite3BtreeLeave(pTo);
57018 return rc;
57019 }
57020 #endif /* SQLITE_OMIT_VACUUM */
@@ -57037,16 +58203,10 @@
57037 ** stores a single value in the VDBE. Mem is an opaque structure visible
57038 ** only within the VDBE. Interface routines refer to a Mem using the
57039 ** name sqlite_value
57040 */
57041
57042 /*
57043 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
57044 ** P if required.
57045 */
57046 #define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
57047
57048 /*
57049 ** If pMem is an object with a valid string representation, this routine
57050 ** ensures the internal encoding for the string representation is
57051 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
57052 **
@@ -57142,11 +58302,11 @@
57142 */
57143 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
57144 int f;
57145 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
57146 assert( (pMem->flags&MEM_RowSet)==0 );
57147 expandBlob(pMem);
57148 f = pMem->flags;
57149 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
57150 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
57151 return SQLITE_NOMEM;
57152 }
@@ -57311,11 +58471,11 @@
57311 ** Release any memory held by the Mem. This may leave the Mem in an
57312 ** inconsistent state, for example with (Mem.z==0) and
57313 ** (Mem.type==SQLITE_TEXT).
57314 */
57315 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
57316 MemReleaseExt(p);
57317 sqlite3DbFree(p->db, p->zMalloc);
57318 p->z = 0;
57319 p->zMalloc = 0;
57320 p->xDel = 0;
57321 }
@@ -57607,11 +58767,11 @@
57607 ** copies of this cell as invalid.
57608 **
57609 ** This is used for testing and debugging only - to make sure shallow
57610 ** copies are not misused.
57611 */
57612 SQLITE_PRIVATE void sqlite3VdbeMemPrepareToChange(Vdbe *pVdbe, Mem *pMem){
57613 int i;
57614 Mem *pX;
57615 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
57616 if( pX->pScopyFrom==pMem ){
57617 pX->flags |= MEM_Invalid;
@@ -57633,11 +58793,11 @@
57633 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57634 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57635 */
57636 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57637 assert( (pFrom->flags & MEM_RowSet)==0 );
57638 MemReleaseExt(pTo);
57639 memcpy(pTo, pFrom, MEMCELLSIZE);
57640 pTo->xDel = 0;
57641 if( (pFrom->flags&MEM_Static)==0 ){
57642 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57643 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57651,11 +58811,11 @@
57651 */
57652 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57653 int rc = SQLITE_OK;
57654
57655 assert( (pFrom->flags & MEM_RowSet)==0 );
57656 MemReleaseExt(pTo);
57657 memcpy(pTo, pFrom, MEMCELLSIZE);
57658 pTo->flags &= ~MEM_Dyn;
57659
57660 if( pTo->flags&(MEM_Str|MEM_Blob) ){
57661 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57979,20 +59139,20 @@
57979 if( pVal->flags&MEM_Null ){
57980 return 0;
57981 }
57982 assert( (MEM_Blob>>3) == MEM_Str );
57983 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
57984 expandBlob(pVal);
57985 if( pVal->flags&MEM_Str ){
57986 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
57987 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
57988 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
57989 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
57990 return 0;
57991 }
57992 }
57993 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-59893-45467 */
57994 }else{
57995 assert( (pVal->flags&MEM_Blob)==0 );
57996 sqlite3VdbeMemStringify(pVal, enc);
57997 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
57998 }
@@ -58369,11 +59529,12 @@
58369 return addr;
58370 }
58371
58372 /*
58373 ** Add an OP_ParseSchema opcode. This routine is broken out from
58374 ** sqlite3VdbeAddOp4() since it needs to also local all btrees.
 
58375 **
58376 ** The zWhere string must have been obtained from sqlite3_malloc().
58377 ** This routine will take ownership of the allocated memory.
58378 */
58379 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
@@ -59086,17 +60247,18 @@
59086 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
59087 break;
59088 }
59089 case P4_MEM: {
59090 Mem *pMem = pOp->p4.pMem;
59091 assert( (pMem->flags & MEM_Null)==0 );
59092 if( pMem->flags & MEM_Str ){
59093 zP4 = pMem->z;
59094 }else if( pMem->flags & MEM_Int ){
59095 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
59096 }else if( pMem->flags & MEM_Real ){
59097 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
 
 
59098 }else{
59099 assert( pMem->flags & MEM_Blob );
59100 zP4 = "(blob)";
59101 }
59102 break;
@@ -59135,12 +60297,13 @@
59135
59136 /*
59137 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
59138 **
59139 ** The prepared statements need to know in advance the complete set of
59140 ** attached databases that they will be using. A mask of these databases
59141 ** is maintained in p->btreeMask and is used for locking and other purposes.
 
59142 */
59143 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
59144 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
59145 assert( i<(int)sizeof(p->btreeMask)*8 );
59146 p->btreeMask |= ((yDbMask)1)<<i;
@@ -59267,11 +60430,11 @@
59267 }else if( p->zMalloc ){
59268 sqlite3DbFree(db, p->zMalloc);
59269 p->zMalloc = 0;
59270 }
59271
59272 p->flags = MEM_Null;
59273 }
59274 db->mallocFailed = malloc_failed;
59275 }
59276 }
59277
@@ -59642,10 +60805,11 @@
59642 sqlite3 *db; /* The database connection */
59643 int nVar; /* Number of parameters */
59644 int nMem; /* Number of VM memory registers */
59645 int nCursor; /* Number of cursors required */
59646 int nArg; /* Number of arguments in subprograms */
 
59647 int n; /* Loop counter */
59648 u8 *zCsr; /* Memory available for allocation */
59649 u8 *zEnd; /* First byte past allocated memory */
59650 int nByte; /* How much extra memory is needed */
59651
@@ -59657,10 +60821,12 @@
59657 assert( db->mallocFailed==0 );
59658 nVar = pParse->nVar;
59659 nMem = pParse->nMem;
59660 nCursor = pParse->nTab;
59661 nArg = pParse->nMaxArg;
 
 
59662
59663 /* For each cursor required, also allocate a memory cell. Memory
59664 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
59665 ** the vdbe program. Instead they are used to allocate space for
59666 ** VdbeCursor/BtCursor structures. The blob of memory associated with
@@ -59703,18 +60869,20 @@
59703 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
59704 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
59705 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
59706 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
59707 &zCsr, zEnd, &nByte);
 
59708 if( nByte ){
59709 p->pFree = sqlite3DbMallocZero(db, nByte);
59710 }
59711 zCsr = p->pFree;
59712 zEnd = &zCsr[nByte];
59713 }while( nByte && !db->mallocFailed );
59714
59715 p->nCursor = (u16)nCursor;
 
59716 if( p->aVar ){
59717 p->nVar = (ynVar)nVar;
59718 for(n=0; n<nVar; n++){
59719 p->aVar[n].flags = MEM_Null;
59720 p->aVar[n].db = db;
@@ -59727,11 +60895,11 @@
59727 }
59728 if( p->aMem ){
59729 p->aMem--; /* aMem[] goes from 1..nMem */
59730 p->nMem = nMem; /* not from 0..nMem-1 */
59731 for(n=1; n<=nMem; n++){
59732 p->aMem[n].flags = MEM_Null;
59733 p->aMem[n].db = db;
59734 }
59735 }
59736 p->explain = pParse->explain;
59737 sqlite3VdbeRewind(p);
@@ -59769,10 +60937,12 @@
59769 ** is used, for example, when a trigger sub-program is halted to restore
59770 ** control to the main program.
59771 */
59772 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
59773 Vdbe *v = pFrame->v;
 
 
59774 v->aOp = pFrame->aOp;
59775 v->nOp = pFrame->nOp;
59776 v->aMem = pFrame->aMem;
59777 v->nMem = pFrame->nMem;
59778 v->apCsr = pFrame->apCsr;
@@ -59831,12 +61001,14 @@
59831
59832 #ifdef SQLITE_DEBUG
59833 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
59834 ** Vdbe.aMem[] arrays have already been cleaned up. */
59835 int i;
59836 for(i=0; i<p->nCursor; i++) assert( p->apCsr==0 || p->apCsr[i]==0 );
59837 for(i=1; i<=p->nMem; i++) assert( p->aMem==0 || p->aMem[i].flags==MEM_Null );
 
 
59838 #endif
59839
59840 sqlite3DbFree(db, p->zErrMsg);
59841 p->zErrMsg = 0;
59842 p->pResultSet = 0;
@@ -59997,20 +61169,35 @@
59997 char *zMaster = 0; /* File-name for the master journal */
59998 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
59999 sqlite3_file *pMaster = 0;
60000 i64 offset = 0;
60001 int res;
 
 
60002
60003 /* Select a master journal file name */
 
 
 
60004 do {
60005 u32 iRandom;
60006 sqlite3DbFree(db, zMaster);
 
 
 
 
 
 
 
 
 
60007 sqlite3_randomness(sizeof(iRandom), &iRandom);
60008 zMaster = sqlite3MPrintf(db, "%s-mj%08X", zMainFile, iRandom&0x7fffffff);
60009 if( !zMaster ){
60010 return SQLITE_NOMEM;
60011 }
 
60012 sqlite3FileSuffix3(zMainFile, zMaster);
60013 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
60014 }while( rc==SQLITE_OK && res );
60015 if( rc==SQLITE_OK ){
60016 /* Open the master journal. */
@@ -60300,10 +61487,11 @@
60300 */
60301
60302 if( p->db->mallocFailed ){
60303 p->rc = SQLITE_NOMEM;
60304 }
 
60305 closeAllCursors(p);
60306 if( p->magic!=VDBE_MAGIC_RUN ){
60307 return SQLITE_OK;
60308 }
60309 checkActiveVdbeCnt(db);
@@ -60637,10 +61825,14 @@
60637 vdbeFreeOpArray(db, p->aOp, p->nOp);
60638 sqlite3DbFree(db, p->aLabel);
60639 sqlite3DbFree(db, p->aColName);
60640 sqlite3DbFree(db, p->zSql);
60641 sqlite3DbFree(db, p->pFree);
 
 
 
 
60642 sqlite3DbFree(db, p);
60643 }
60644
60645 /*
60646 ** Delete an entire VDBE.
@@ -61116,19 +62308,10 @@
61116 ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
61117 ** and the common prefixes are equal, then key1 is less than key2.
61118 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
61119 ** equal, then the keys are considered to be equal and
61120 ** the parts beyond the common prefix are ignored.
61121 **
61122 ** If the UNPACKED_IGNORE_ROWID flag is set, then the last byte of
61123 ** the header of pKey1 is ignored. It is assumed that pKey1 is
61124 ** an index key, and thus ends with a rowid value. The last byte
61125 ** of the header will therefore be the serial type of the rowid:
61126 ** one of 1, 2, 3, 4, 5, 6, 8, or 9 - the integer serial types.
61127 ** The serial type of the final rowid will always be a single byte.
61128 ** By ignoring this last byte of the header, we force the comparison
61129 ** to ignore the rowid at the end of key1.
61130 */
61131 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
61132 int nKey1, const void *pKey1, /* Left key */
61133 UnpackedRecord *pPKey2 /* Right key */
61134 ){
@@ -61157,13 +62340,10 @@
61157 */
61158 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
61159
61160 idx1 = getVarint32(aKey1, szHdr1);
61161 d1 = szHdr1;
61162 if( pPKey2->flags & UNPACKED_IGNORE_ROWID ){
61163 szHdr1--;
61164 }
61165 nField = pKeyInfo->nField;
61166 while( idx1<szHdr1 && i<pPKey2->nField ){
61167 u32 serial_type1;
61168
61169 /* Read the serial types for the next element in each key. */
@@ -61339,11 +62519,11 @@
61339 memset(&m, 0, sizeof(m));
61340 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
61341 if( rc ){
61342 return rc;
61343 }
61344 assert( pUnpacked->flags & UNPACKED_IGNORE_ROWID );
61345 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
61346 sqlite3VdbeMemRelease(&m);
61347 return SQLITE_OK;
61348 }
61349
@@ -61782,11 +62962,11 @@
61782 ** since any application that receives an SQLITE_MISUSE is broken by
61783 ** definition.
61784 **
61785 ** Nevertheless, some published applications that were originally written
61786 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
61787 ** returns, and the so were broken by the automatic-reset change. As a
61788 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
61789 ** legacy behavior of returning SQLITE_MISUSE for cases where the
61790 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
61791 ** or SQLITE_BUSY error.
61792 */
@@ -62128,17 +63308,17 @@
62128 pOut = &pVm->pResultSet[i];
62129 }else{
62130 /* If the value passed as the second argument is out of range, return
62131 ** a pointer to the following static Mem object which contains the
62132 ** value SQL NULL. Even though the Mem structure contains an element
62133 ** of type i64, on certain architecture (x86) with certain compiler
62134 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
62135 ** instead of an 8-byte one. This all works fine, except that when
62136 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
62137 ** that a Mem structure is located on an 8-byte boundary. To prevent
62138 ** this assert() from failing, when building with SQLITE_DEBUG defined
62139 ** using gcc, force nullMem to be 8-byte aligned using the magical
62140 ** __attribute__((aligned(8))) macro. */
62141 static const Mem nullMem
62142 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
62143 __attribute__((aligned(8)))
62144 #endif
@@ -62704,10 +63884,18 @@
62704 ** database.
62705 */
62706 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
62707 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
62708 }
 
 
 
 
 
 
 
 
62709
62710 /*
62711 ** Return a pointer to the next prepared statement after pStmt associated
62712 ** with database connection pDb. If pStmt is NULL, return the first
62713 ** prepared statement for the database connection. Return NULL if there
@@ -62749,10 +63937,12 @@
62749 **
62750 *************************************************************************
62751 **
62752 ** This file contains code used to insert the values of host parameters
62753 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
 
 
62754 */
62755
62756 #ifndef SQLITE_OMIT_TRACE
62757
62758 /*
@@ -62887,10 +64077,125 @@
62887 }
62888 return sqlite3StrAccumFinish(&out);
62889 }
62890
62891 #endif /* #ifndef SQLITE_OMIT_TRACE */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62892
62893 /************** End of vdbetrace.c *******************************************/
62894 /************** Begin file vdbe.c ********************************************/
62895 /*
62896 ** 2001 September 15
@@ -62942,11 +64247,11 @@
62942 ** Invoke this macro on memory cells just prior to changing the
62943 ** value of the cell. This macro verifies that shallow copies are
62944 ** not misused.
62945 */
62946 #ifdef SQLITE_DEBUG
62947 # define memAboutToChange(P,M) sqlite3VdbeMemPrepareToChange(P,M)
62948 #else
62949 # define memAboutToChange(P,M)
62950 #endif
62951
62952 /*
@@ -62960,12 +64265,12 @@
62960 SQLITE_API int sqlite3_search_count = 0;
62961 #endif
62962
62963 /*
62964 ** When this global variable is positive, it gets decremented once before
62965 ** each instruction in the VDBE. When reaches zero, the u1.isInterrupted
62966 ** field of the sqlite3 structure is set in order to simulate and interrupt.
62967 **
62968 ** This facility is used for testing purposes only. It does not function
62969 ** in an ordinary build.
62970 */
62971 #ifdef SQLITE_TEST
@@ -63041,16 +64346,10 @@
63041 */
63042 #define Deephemeralize(P) \
63043 if( ((P)->flags&MEM_Ephem)!=0 \
63044 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
63045
63046 /*
63047 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
63048 ** P if required.
63049 */
63050 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
63051
63052 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63053 #ifdef SQLITE_OMIT_MERGE_SORT
63054 # define isSorter(x) 0
63055 #else
63056 # define isSorter(x) ((x)->pSorter!=0)
@@ -63086,11 +64385,11 @@
63086 */
63087 static VdbeCursor *allocateCursor(
63088 Vdbe *p, /* The virtual machine */
63089 int iCur, /* Index of the new VdbeCursor */
63090 int nField, /* Number of fields in the table or index */
63091 int iDb, /* When database the cursor belongs to, or -1 */
63092 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
63093 ){
63094 /* Find the memory cell that will be used to store the blob of memory
63095 ** required for this VdbeCursor structure. It is convenient to use a
63096 ** vdbe memory cell to manage the memory allocation required for a
@@ -63457,11 +64756,11 @@
63457 ** sqlite3_interrupt() routine has been called. If it has been, then
63458 ** processing of the VDBE program is interrupted.
63459 **
63460 ** This macro added to every instruction that does a jump in order to
63461 ** implement a loop. This test used to be on every single instruction,
63462 ** but that meant we more testing that we needed. By only testing the
63463 ** flag on jump instructions, we get a (small) speed improvement.
63464 */
63465 #define CHECK_FOR_INTERRUPT \
63466 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
63467
@@ -63567,69 +64866,72 @@
63567 */
63568 union vdbeExecUnion {
63569 struct OP_Yield_stack_vars {
63570 int pcDest;
63571 } aa;
 
 
 
63572 struct OP_Variable_stack_vars {
63573 Mem *pVar; /* Value being transferred */
63574 } ab;
63575 struct OP_Move_stack_vars {
63576 char *zMalloc; /* Holding variable for allocated memory */
63577 int n; /* Number of registers left to copy */
63578 int p1; /* Register to copy from */
63579 int p2; /* Register to copy to */
63580 } ac;
63581 struct OP_ResultRow_stack_vars {
63582 Mem *pMem;
63583 int i;
63584 } ad;
63585 struct OP_Concat_stack_vars {
63586 i64 nByte;
63587 } ae;
63588 struct OP_Remainder_stack_vars {
63589 int flags; /* Combined MEM_* flags from both inputs */
63590 i64 iA; /* Integer value of left operand */
63591 i64 iB; /* Integer value of right operand */
63592 double rA; /* Real value of left operand */
63593 double rB; /* Real value of right operand */
63594 } af;
63595 struct OP_Function_stack_vars {
63596 int i;
63597 Mem *pArg;
63598 sqlite3_context ctx;
63599 sqlite3_value **apVal;
63600 int n;
63601 } ag;
63602 struct OP_ShiftRight_stack_vars {
63603 i64 iA;
63604 u64 uA;
63605 i64 iB;
63606 u8 op;
63607 } ah;
63608 struct OP_Ge_stack_vars {
63609 int res; /* Result of the comparison of pIn1 against pIn3 */
63610 char affinity; /* Affinity to use for comparison */
63611 u16 flags1; /* Copy of initial value of pIn1->flags */
63612 u16 flags3; /* Copy of initial value of pIn3->flags */
63613 } ai;
63614 struct OP_Compare_stack_vars {
63615 int n;
63616 int i;
63617 int p1;
63618 int p2;
63619 const KeyInfo *pKeyInfo;
63620 int idx;
63621 CollSeq *pColl; /* Collating sequence to use on this term */
63622 int bRev; /* True for DESCENDING sort order */
63623 } aj;
63624 struct OP_Or_stack_vars {
63625 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63626 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
63627 } ak;
63628 struct OP_IfNot_stack_vars {
63629 int c;
63630 } al;
63631 struct OP_Column_stack_vars {
63632 u32 payloadSize; /* Number of bytes in the record */
63633 i64 payloadSize64; /* Number of bytes in the record */
63634 int p1; /* P1 value of the opcode */
63635 int p2; /* column number to retrieve */
@@ -63650,15 +64952,15 @@
63650 u32 szField; /* Number of bytes in the content of a field */
63651 int szHdr; /* Size of the header size field at start of record */
63652 int avail; /* Number of bytes of available data */
63653 u32 t; /* A type code from the record header */
63654 Mem *pReg; /* PseudoTable input register */
63655 } am;
63656 struct OP_Affinity_stack_vars {
63657 const char *zAffinity; /* The affinity to be applied */
63658 char cAff; /* A single character of affinity */
63659 } an;
63660 struct OP_MakeRecord_stack_vars {
63661 u8 *zNewRecord; /* A buffer to hold the data for the new record */
63662 Mem *pRec; /* The new record */
63663 u64 nData; /* Number of bytes of data space */
63664 int nHdr; /* Number of bytes of header space */
@@ -63671,108 +64973,108 @@
63671 int nField; /* Number of fields in the record */
63672 char *zAffinity; /* The affinity string for the record */
63673 int file_format; /* File format to use for encoding */
63674 int i; /* Space used in zNewRecord[] */
63675 int len; /* Length of a field */
63676 } ao;
63677 struct OP_Count_stack_vars {
63678 i64 nEntry;
63679 BtCursor *pCrsr;
63680 } ap;
63681 struct OP_Savepoint_stack_vars {
63682 int p1; /* Value of P1 operand */
63683 char *zName; /* Name of savepoint */
63684 int nName;
63685 Savepoint *pNew;
63686 Savepoint *pSavepoint;
63687 Savepoint *pTmp;
63688 int iSavepoint;
63689 int ii;
63690 } aq;
63691 struct OP_AutoCommit_stack_vars {
63692 int desiredAutoCommit;
63693 int iRollback;
63694 int turnOnAC;
63695 } ar;
63696 struct OP_Transaction_stack_vars {
63697 Btree *pBt;
63698 } as;
63699 struct OP_ReadCookie_stack_vars {
63700 int iMeta;
63701 int iDb;
63702 int iCookie;
63703 } at;
63704 struct OP_SetCookie_stack_vars {
63705 Db *pDb;
63706 } au;
63707 struct OP_VerifyCookie_stack_vars {
63708 int iMeta;
63709 int iGen;
63710 Btree *pBt;
63711 } av;
63712 struct OP_OpenWrite_stack_vars {
63713 int nField;
63714 KeyInfo *pKeyInfo;
63715 int p2;
63716 int iDb;
63717 int wrFlag;
63718 Btree *pX;
63719 VdbeCursor *pCur;
63720 Db *pDb;
63721 } aw;
63722 struct OP_OpenEphemeral_stack_vars {
63723 VdbeCursor *pCx;
63724 } ax;
63725 struct OP_SorterOpen_stack_vars {
63726 VdbeCursor *pCx;
63727 } ay;
63728 struct OP_OpenPseudo_stack_vars {
63729 VdbeCursor *pCx;
63730 } az;
63731 struct OP_SeekGt_stack_vars {
63732 int res;
63733 int oc;
63734 VdbeCursor *pC;
63735 UnpackedRecord r;
63736 int nField;
63737 i64 iKey; /* The rowid we are to seek to */
63738 } ba;
63739 struct OP_Seek_stack_vars {
63740 VdbeCursor *pC;
63741 } bb;
63742 struct OP_Found_stack_vars {
63743 int alreadyExists;
63744 VdbeCursor *pC;
63745 int res;
63746 char *pFree;
63747 UnpackedRecord *pIdxKey;
63748 UnpackedRecord r;
63749 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63750 } bc;
63751 struct OP_IsUnique_stack_vars {
63752 u16 ii;
63753 VdbeCursor *pCx;
63754 BtCursor *pCrsr;
63755 u16 nField;
63756 Mem *aMx;
63757 UnpackedRecord r; /* B-Tree index search key */
63758 i64 R; /* Rowid stored in register P3 */
63759 } bd;
63760 struct OP_NotExists_stack_vars {
63761 VdbeCursor *pC;
63762 BtCursor *pCrsr;
63763 int res;
63764 u64 iKey;
63765 } be;
63766 struct OP_NewRowid_stack_vars {
63767 i64 v; /* The new rowid */
63768 VdbeCursor *pC; /* Cursor of table to get the new rowid */
63769 int res; /* Result of an sqlite3BtreeLast() */
63770 int cnt; /* Counter to limit the number of searches */
63771 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
63772 VdbeFrame *pFrame; /* Root frame of VDBE */
63773 } bf;
63774 struct OP_InsertInt_stack_vars {
63775 Mem *pData; /* MEM cell holding data for the record to be inserted */
63776 Mem *pKey; /* MEM cell holding key for the record */
63777 i64 iKey; /* The integer ROWID or key for the record to be inserted */
63778 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63779,161 +65081,161 @@
63779 int nZero; /* Number of zero-bytes to append */
63780 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
63781 const char *zDb; /* database name - used by the update hook */
63782 const char *zTbl; /* Table name - used by the opdate hook */
63783 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63784 } bg;
63785 struct OP_Delete_stack_vars {
63786 i64 iKey;
63787 VdbeCursor *pC;
63788 } bh;
63789 struct OP_SorterCompare_stack_vars {
63790 VdbeCursor *pC;
63791 int res;
63792 } bi;
63793 struct OP_SorterData_stack_vars {
63794 VdbeCursor *pC;
63795 } bj;
63796 struct OP_RowData_stack_vars {
63797 VdbeCursor *pC;
63798 BtCursor *pCrsr;
63799 u32 n;
63800 i64 n64;
63801 } bk;
63802 struct OP_Rowid_stack_vars {
63803 VdbeCursor *pC;
63804 i64 v;
63805 sqlite3_vtab *pVtab;
63806 const sqlite3_module *pModule;
63807 } bl;
63808 struct OP_NullRow_stack_vars {
63809 VdbeCursor *pC;
63810 } bm;
63811 struct OP_Last_stack_vars {
63812 VdbeCursor *pC;
63813 BtCursor *pCrsr;
63814 int res;
63815 } bn;
63816 struct OP_Rewind_stack_vars {
63817 VdbeCursor *pC;
63818 BtCursor *pCrsr;
63819 int res;
63820 } bo;
63821 struct OP_Next_stack_vars {
63822 VdbeCursor *pC;
63823 int res;
63824 } bp;
63825 struct OP_IdxInsert_stack_vars {
63826 VdbeCursor *pC;
63827 BtCursor *pCrsr;
63828 int nKey;
63829 const char *zKey;
63830 } bq;
63831 struct OP_IdxDelete_stack_vars {
63832 VdbeCursor *pC;
63833 BtCursor *pCrsr;
63834 int res;
63835 UnpackedRecord r;
63836 } br;
63837 struct OP_IdxRowid_stack_vars {
63838 BtCursor *pCrsr;
63839 VdbeCursor *pC;
63840 i64 rowid;
63841 } bs;
63842 struct OP_IdxGE_stack_vars {
63843 VdbeCursor *pC;
63844 int res;
63845 UnpackedRecord r;
63846 } bt;
63847 struct OP_Destroy_stack_vars {
63848 int iMoved;
63849 int iCnt;
63850 Vdbe *pVdbe;
63851 int iDb;
63852 } bu;
63853 struct OP_Clear_stack_vars {
63854 int nChange;
63855 } bv;
63856 struct OP_CreateTable_stack_vars {
63857 int pgno;
63858 int flags;
63859 Db *pDb;
63860 } bw;
63861 struct OP_ParseSchema_stack_vars {
63862 int iDb;
63863 const char *zMaster;
63864 char *zSql;
63865 InitData initData;
63866 } bx;
63867 struct OP_IntegrityCk_stack_vars {
63868 int nRoot; /* Number of tables to check. (Number of root pages.) */
63869 int *aRoot; /* Array of rootpage numbers for tables to be checked */
63870 int j; /* Loop counter */
63871 int nErr; /* Number of errors reported */
63872 char *z; /* Text of the error report */
63873 Mem *pnErr; /* Register keeping track of errors remaining */
63874 } by;
63875 struct OP_RowSetRead_stack_vars {
63876 i64 val;
63877 } bz;
63878 struct OP_RowSetTest_stack_vars {
63879 int iSet;
63880 int exists;
63881 } ca;
63882 struct OP_Program_stack_vars {
63883 int nMem; /* Number of memory registers for sub-program */
63884 int nByte; /* Bytes of runtime space required for sub-program */
63885 Mem *pRt; /* Register to allocate runtime space */
63886 Mem *pMem; /* Used to iterate through memory cells */
63887 Mem *pEnd; /* Last memory cell in new array */
63888 VdbeFrame *pFrame; /* New vdbe frame to execute in */
63889 SubProgram *pProgram; /* Sub-program to execute */
63890 void *t; /* Token identifying trigger */
63891 } cb;
63892 struct OP_Param_stack_vars {
63893 VdbeFrame *pFrame;
63894 Mem *pIn;
63895 } cc;
63896 struct OP_MemMax_stack_vars {
63897 Mem *pIn1;
63898 VdbeFrame *pFrame;
63899 } cd;
63900 struct OP_AggStep_stack_vars {
63901 int n;
63902 int i;
63903 Mem *pMem;
63904 Mem *pRec;
63905 sqlite3_context ctx;
63906 sqlite3_value **apVal;
63907 } ce;
63908 struct OP_AggFinal_stack_vars {
63909 Mem *pMem;
63910 } cf;
63911 struct OP_Checkpoint_stack_vars {
63912 int i; /* Loop counter */
63913 int aRes[3]; /* Results */
63914 Mem *pMem; /* Write results here */
63915 } cg;
63916 struct OP_JournalMode_stack_vars {
63917 Btree *pBt; /* Btree to change journal mode of */
63918 Pager *pPager; /* Pager associated with pBt */
63919 int eNew; /* New journal mode */
63920 int eOld; /* The old journal mode */
63921 const char *zFilename; /* Name of database file for pPager */
63922 } ch;
63923 struct OP_IncrVacuum_stack_vars {
63924 Btree *pBt;
63925 } ci;
63926 struct OP_VBegin_stack_vars {
63927 VTable *pVTab;
63928 } cj;
63929 struct OP_VOpen_stack_vars {
63930 VdbeCursor *pCur;
63931 sqlite3_vtab_cursor *pVtabCursor;
63932 sqlite3_vtab *pVtab;
63933 sqlite3_module *pModule;
63934 } ck;
63935 struct OP_VFilter_stack_vars {
63936 int nArg;
63937 int iQuery;
63938 const sqlite3_module *pModule;
63939 Mem *pQuery;
@@ -63942,40 +65244,40 @@
63942 sqlite3_vtab *pVtab;
63943 VdbeCursor *pCur;
63944 int res;
63945 int i;
63946 Mem **apArg;
63947 } cl;
63948 struct OP_VColumn_stack_vars {
63949 sqlite3_vtab *pVtab;
63950 const sqlite3_module *pModule;
63951 Mem *pDest;
63952 sqlite3_context sContext;
63953 } cm;
63954 struct OP_VNext_stack_vars {
63955 sqlite3_vtab *pVtab;
63956 const sqlite3_module *pModule;
63957 int res;
63958 VdbeCursor *pCur;
63959 } cn;
63960 struct OP_VRename_stack_vars {
63961 sqlite3_vtab *pVtab;
63962 Mem *pName;
63963 } co;
63964 struct OP_VUpdate_stack_vars {
63965 sqlite3_vtab *pVtab;
63966 sqlite3_module *pModule;
63967 int nArg;
63968 int i;
63969 sqlite_int64 rowid;
63970 Mem **apArg;
63971 Mem *pX;
63972 } cp;
63973 struct OP_Trace_stack_vars {
63974 char *zTrace;
63975 char *z;
63976 } cq;
63977 } u;
63978 /* End automatically generated code
63979 ********************************************************************/
63980
63981 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64071,11 +65373,11 @@
64071 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
64072 assert( pOp->p2>0 );
64073 assert( pOp->p2<=p->nMem );
64074 pOut = &aMem[pOp->p2];
64075 memAboutToChange(p, pOut);
64076 MemReleaseExt(pOut);
64077 pOut->flags = MEM_Int;
64078 }
64079
64080 /* Sanity checking on other operands */
64081 #ifdef SQLITE_DEBUG
@@ -64162,11 +65464,12 @@
64162 /* Opcode: Gosub P1 P2 * * *
64163 **
64164 ** Write the current address onto register P1
64165 ** and then jump to address P2.
64166 */
64167 case OP_Gosub: { /* jump, in1 */
 
64168 pIn1 = &aMem[pOp->p1];
64169 assert( (pIn1->flags & MEM_Dyn)==0 );
64170 memAboutToChange(p, pIn1);
64171 pIn1->flags = MEM_Int;
64172 pIn1->u.i = pc;
@@ -64361,16 +65664,31 @@
64361 pOut->enc = encoding;
64362 UPDATE_MAX_BLOBSIZE(pOut);
64363 break;
64364 }
64365
64366 /* Opcode: Null * P2 * * *
64367 **
64368 ** Write a NULL into register P2.
 
 
 
64369 */
64370 case OP_Null: { /* out2-prerelease */
 
 
 
 
 
64371 pOut->flags = MEM_Null;
 
 
 
 
 
 
 
64372 break;
64373 }
64374
64375
64376 /* Opcode: Blob P1 P2 * P4
@@ -64392,21 +65710,21 @@
64392 **
64393 ** If the parameter is named, then its name appears in P4 and P3==1.
64394 ** The P4 value is used by sqlite3_bind_parameter_name().
64395 */
64396 case OP_Variable: { /* out2-prerelease */
64397 #if 0 /* local variables moved into u.ab */
64398 Mem *pVar; /* Value being transferred */
64399 #endif /* local variables moved into u.ab */
64400
64401 assert( pOp->p1>0 && pOp->p1<=p->nVar );
64402 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
64403 u.ab.pVar = &p->aVar[pOp->p1 - 1];
64404 if( sqlite3VdbeMemTooBig(u.ab.pVar) ){
64405 goto too_big;
64406 }
64407 sqlite3VdbeMemShallowCopy(pOut, u.ab.pVar, MEM_Static);
64408 UPDATE_MAX_BLOBSIZE(pOut);
64409 break;
64410 }
64411
64412 /* Opcode: Move P1 P2 P3 * *
@@ -64415,40 +65733,40 @@
64415 ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
64416 ** left holding a NULL. It is an error for register ranges
64417 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
64418 */
64419 case OP_Move: {
64420 #if 0 /* local variables moved into u.ac */
64421 char *zMalloc; /* Holding variable for allocated memory */
64422 int n; /* Number of registers left to copy */
64423 int p1; /* Register to copy from */
64424 int p2; /* Register to copy to */
64425 #endif /* local variables moved into u.ac */
64426
64427 u.ac.n = pOp->p3;
64428 u.ac.p1 = pOp->p1;
64429 u.ac.p2 = pOp->p2;
64430 assert( u.ac.n>0 && u.ac.p1>0 && u.ac.p2>0 );
64431 assert( u.ac.p1+u.ac.n<=u.ac.p2 || u.ac.p2+u.ac.n<=u.ac.p1 );
64432
64433 pIn1 = &aMem[u.ac.p1];
64434 pOut = &aMem[u.ac.p2];
64435 while( u.ac.n-- ){
64436 assert( pOut<=&aMem[p->nMem] );
64437 assert( pIn1<=&aMem[p->nMem] );
64438 assert( memIsValid(pIn1) );
64439 memAboutToChange(p, pOut);
64440 u.ac.zMalloc = pOut->zMalloc;
64441 pOut->zMalloc = 0;
64442 sqlite3VdbeMemMove(pOut, pIn1);
64443 #ifdef SQLITE_DEBUG
64444 if( pOut->pScopyFrom>=&aMem[u.ac.p1] && pOut->pScopyFrom<&aMem[u.ac.p1+pOp->p3] ){
64445 pOut->pScopyFrom += u.ac.p1 - pOp->p2;
64446 }
64447 #endif
64448 pIn1->zMalloc = u.ac.zMalloc;
64449 REGISTER_TRACE(u.ac.p2++, pOut);
64450 pIn1++;
64451 pOut++;
64452 }
64453 break;
64454 }
@@ -64501,14 +65819,14 @@
64501 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
64502 ** structure to provide access to the top P1 values as the result
64503 ** row.
64504 */
64505 case OP_ResultRow: {
64506 #if 0 /* local variables moved into u.ad */
64507 Mem *pMem;
64508 int i;
64509 #endif /* local variables moved into u.ad */
64510 assert( p->nResColumn==pOp->p2 );
64511 assert( pOp->p1>0 );
64512 assert( pOp->p1+pOp->p2<=p->nMem+1 );
64513
64514 /* If this statement has violated immediate foreign key constraints, do
@@ -64544,21 +65862,21 @@
64544 /* Invalidate all ephemeral cursor row caches */
64545 p->cacheCtr = (p->cacheCtr + 2)|1;
64546
64547 /* Make sure the results of the current row are \000 terminated
64548 ** and have an assigned type. The results are de-ephemeralized as
64549 ** as side effect.
64550 */
64551 u.ad.pMem = p->pResultSet = &aMem[pOp->p1];
64552 for(u.ad.i=0; u.ad.i<pOp->p2; u.ad.i++){
64553 assert( memIsValid(&u.ad.pMem[u.ad.i]) );
64554 Deephemeralize(&u.ad.pMem[u.ad.i]);
64555 assert( (u.ad.pMem[u.ad.i].flags & MEM_Ephem)==0
64556 || (u.ad.pMem[u.ad.i].flags & (MEM_Str|MEM_Blob))==0 );
64557 sqlite3VdbeMemNulTerminate(&u.ad.pMem[u.ad.i]);
64558 sqlite3VdbeMemStoreType(&u.ad.pMem[u.ad.i]);
64559 REGISTER_TRACE(pOp->p1+u.ad.i, &u.ad.pMem[u.ad.i]);
64560 }
64561 if( db->mallocFailed ) goto no_mem;
64562
64563 /* Return SQLITE_ROW
64564 */
@@ -64578,13 +65896,13 @@
64578 ** It is illegal for P1 and P3 to be the same register. Sometimes,
64579 ** if P3 is the same register as P2, the implementation is able
64580 ** to avoid a memcpy().
64581 */
64582 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
64583 #if 0 /* local variables moved into u.ae */
64584 i64 nByte;
64585 #endif /* local variables moved into u.ae */
64586
64587 pIn1 = &aMem[pOp->p1];
64588 pIn2 = &aMem[pOp->p2];
64589 pOut = &aMem[pOp->p3];
64590 assert( pIn1!=pOut );
@@ -64593,26 +65911,26 @@
64593 break;
64594 }
64595 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
64596 Stringify(pIn1, encoding);
64597 Stringify(pIn2, encoding);
64598 u.ae.nByte = pIn1->n + pIn2->n;
64599 if( u.ae.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
64600 goto too_big;
64601 }
64602 MemSetTypeFlag(pOut, MEM_Str);
64603 if( sqlite3VdbeMemGrow(pOut, (int)u.ae.nByte+2, pOut==pIn2) ){
64604 goto no_mem;
64605 }
64606 if( pOut!=pIn2 ){
64607 memcpy(pOut->z, pIn2->z, pIn2->n);
64608 }
64609 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
64610 pOut->z[u.ae.nByte] = 0;
64611 pOut->z[u.ae.nByte+1] = 0;
64612 pOut->flags |= MEM_Term;
64613 pOut->n = (int)u.ae.nByte;
64614 pOut->enc = encoding;
64615 UPDATE_MAX_BLOBSIZE(pOut);
64616 break;
64617 }
64618
@@ -64652,80 +65970,80 @@
64652 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
64653 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
64654 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
64655 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
64656 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
64657 #if 0 /* local variables moved into u.af */
64658 int flags; /* Combined MEM_* flags from both inputs */
64659 i64 iA; /* Integer value of left operand */
64660 i64 iB; /* Integer value of right operand */
64661 double rA; /* Real value of left operand */
64662 double rB; /* Real value of right operand */
64663 #endif /* local variables moved into u.af */
64664
64665 pIn1 = &aMem[pOp->p1];
64666 applyNumericAffinity(pIn1);
64667 pIn2 = &aMem[pOp->p2];
64668 applyNumericAffinity(pIn2);
64669 pOut = &aMem[pOp->p3];
64670 u.af.flags = pIn1->flags | pIn2->flags;
64671 if( (u.af.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
64672 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
64673 u.af.iA = pIn1->u.i;
64674 u.af.iB = pIn2->u.i;
64675 switch( pOp->opcode ){
64676 case OP_Add: if( sqlite3AddInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
64677 case OP_Subtract: if( sqlite3SubInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
64678 case OP_Multiply: if( sqlite3MulInt64(&u.af.iB,u.af.iA) ) goto fp_math; break;
64679 case OP_Divide: {
64680 if( u.af.iA==0 ) goto arithmetic_result_is_null;
64681 if( u.af.iA==-1 && u.af.iB==SMALLEST_INT64 ) goto fp_math;
64682 u.af.iB /= u.af.iA;
64683 break;
64684 }
64685 default: {
64686 if( u.af.iA==0 ) goto arithmetic_result_is_null;
64687 if( u.af.iA==-1 ) u.af.iA = 1;
64688 u.af.iB %= u.af.iA;
64689 break;
64690 }
64691 }
64692 pOut->u.i = u.af.iB;
64693 MemSetTypeFlag(pOut, MEM_Int);
64694 }else{
64695 fp_math:
64696 u.af.rA = sqlite3VdbeRealValue(pIn1);
64697 u.af.rB = sqlite3VdbeRealValue(pIn2);
64698 switch( pOp->opcode ){
64699 case OP_Add: u.af.rB += u.af.rA; break;
64700 case OP_Subtract: u.af.rB -= u.af.rA; break;
64701 case OP_Multiply: u.af.rB *= u.af.rA; break;
64702 case OP_Divide: {
64703 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
64704 if( u.af.rA==(double)0 ) goto arithmetic_result_is_null;
64705 u.af.rB /= u.af.rA;
64706 break;
64707 }
64708 default: {
64709 u.af.iA = (i64)u.af.rA;
64710 u.af.iB = (i64)u.af.rB;
64711 if( u.af.iA==0 ) goto arithmetic_result_is_null;
64712 if( u.af.iA==-1 ) u.af.iA = 1;
64713 u.af.rB = (double)(u.af.iB % u.af.iA);
64714 break;
64715 }
64716 }
64717 #ifdef SQLITE_OMIT_FLOATING_POINT
64718 pOut->u.i = u.af.rB;
64719 MemSetTypeFlag(pOut, MEM_Int);
64720 #else
64721 if( sqlite3IsNaN(u.af.rB) ){
64722 goto arithmetic_result_is_null;
64723 }
64724 pOut->r = u.af.rB;
64725 MemSetTypeFlag(pOut, MEM_Real);
64726 if( (u.af.flags & MEM_Real)==0 ){
64727 sqlite3VdbeIntegerAffinity(pOut);
64728 }
64729 #endif
64730 }
64731 break;
@@ -64766,96 +66084,96 @@
64766 ** invocation of this opcode.
64767 **
64768 ** See also: AggStep and AggFinal
64769 */
64770 case OP_Function: {
64771 #if 0 /* local variables moved into u.ag */
64772 int i;
64773 Mem *pArg;
64774 sqlite3_context ctx;
64775 sqlite3_value **apVal;
64776 int n;
64777 #endif /* local variables moved into u.ag */
64778
64779 u.ag.n = pOp->p5;
64780 u.ag.apVal = p->apArg;
64781 assert( u.ag.apVal || u.ag.n==0 );
64782 assert( pOp->p3>0 && pOp->p3<=p->nMem );
64783 pOut = &aMem[pOp->p3];
64784 memAboutToChange(p, pOut);
64785
64786 assert( u.ag.n==0 || (pOp->p2>0 && pOp->p2+u.ag.n<=p->nMem+1) );
64787 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ag.n );
64788 u.ag.pArg = &aMem[pOp->p2];
64789 for(u.ag.i=0; u.ag.i<u.ag.n; u.ag.i++, u.ag.pArg++){
64790 assert( memIsValid(u.ag.pArg) );
64791 u.ag.apVal[u.ag.i] = u.ag.pArg;
64792 Deephemeralize(u.ag.pArg);
64793 sqlite3VdbeMemStoreType(u.ag.pArg);
64794 REGISTER_TRACE(pOp->p2+u.ag.i, u.ag.pArg);
64795 }
64796
64797 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
64798 if( pOp->p4type==P4_FUNCDEF ){
64799 u.ag.ctx.pFunc = pOp->p4.pFunc;
64800 u.ag.ctx.pVdbeFunc = 0;
64801 }else{
64802 u.ag.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
64803 u.ag.ctx.pFunc = u.ag.ctx.pVdbeFunc->pFunc;
64804 }
64805
64806 u.ag.ctx.s.flags = MEM_Null;
64807 u.ag.ctx.s.db = db;
64808 u.ag.ctx.s.xDel = 0;
64809 u.ag.ctx.s.zMalloc = 0;
64810
64811 /* The output cell may already have a buffer allocated. Move
64812 ** the pointer to u.ag.ctx.s so in case the user-function can use
64813 ** the already allocated buffer instead of allocating a new one.
64814 */
64815 sqlite3VdbeMemMove(&u.ag.ctx.s, pOut);
64816 MemSetTypeFlag(&u.ag.ctx.s, MEM_Null);
64817
64818 u.ag.ctx.isError = 0;
64819 if( u.ag.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
64820 assert( pOp>aOp );
64821 assert( pOp[-1].p4type==P4_COLLSEQ );
64822 assert( pOp[-1].opcode==OP_CollSeq );
64823 u.ag.ctx.pColl = pOp[-1].p4.pColl;
64824 }
64825 db->lastRowid = lastRowid;
64826 (*u.ag.ctx.pFunc->xFunc)(&u.ag.ctx, u.ag.n, u.ag.apVal); /* IMP: R-24505-23230 */
64827 lastRowid = db->lastRowid;
64828
64829 /* If any auxiliary data functions have been called by this user function,
64830 ** immediately call the destructor for any non-static values.
64831 */
64832 if( u.ag.ctx.pVdbeFunc ){
64833 sqlite3VdbeDeleteAuxData(u.ag.ctx.pVdbeFunc, pOp->p1);
64834 pOp->p4.pVdbeFunc = u.ag.ctx.pVdbeFunc;
64835 pOp->p4type = P4_VDBEFUNC;
64836 }
64837
64838 if( db->mallocFailed ){
64839 /* Even though a malloc() has failed, the implementation of the
64840 ** user function may have called an sqlite3_result_XXX() function
64841 ** to return a value. The following call releases any resources
64842 ** associated with such a value.
64843 */
64844 sqlite3VdbeMemRelease(&u.ag.ctx.s);
64845 goto no_mem;
64846 }
64847
64848 /* If the function returned an error, throw an exception */
64849 if( u.ag.ctx.isError ){
64850 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ag.ctx.s));
64851 rc = u.ag.ctx.isError;
64852 }
64853
64854 /* Copy the result of the function into register P3 */
64855 sqlite3VdbeChangeEncoding(&u.ag.ctx.s, encoding);
64856 sqlite3VdbeMemMove(pOut, &u.ag.ctx.s);
64857 if( sqlite3VdbeMemTooBig(pOut) ){
64858 goto too_big;
64859 }
64860
64861 #if 0
@@ -64899,56 +66217,56 @@
64899 */
64900 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
64901 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
64902 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
64903 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
64904 #if 0 /* local variables moved into u.ah */
64905 i64 iA;
64906 u64 uA;
64907 i64 iB;
64908 u8 op;
64909 #endif /* local variables moved into u.ah */
64910
64911 pIn1 = &aMem[pOp->p1];
64912 pIn2 = &aMem[pOp->p2];
64913 pOut = &aMem[pOp->p3];
64914 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
64915 sqlite3VdbeMemSetNull(pOut);
64916 break;
64917 }
64918 u.ah.iA = sqlite3VdbeIntValue(pIn2);
64919 u.ah.iB = sqlite3VdbeIntValue(pIn1);
64920 u.ah.op = pOp->opcode;
64921 if( u.ah.op==OP_BitAnd ){
64922 u.ah.iA &= u.ah.iB;
64923 }else if( u.ah.op==OP_BitOr ){
64924 u.ah.iA |= u.ah.iB;
64925 }else if( u.ah.iB!=0 ){
64926 assert( u.ah.op==OP_ShiftRight || u.ah.op==OP_ShiftLeft );
64927
64928 /* If shifting by a negative amount, shift in the other direction */
64929 if( u.ah.iB<0 ){
64930 assert( OP_ShiftRight==OP_ShiftLeft+1 );
64931 u.ah.op = 2*OP_ShiftLeft + 1 - u.ah.op;
64932 u.ah.iB = u.ah.iB>(-64) ? -u.ah.iB : 64;
64933 }
64934
64935 if( u.ah.iB>=64 ){
64936 u.ah.iA = (u.ah.iA>=0 || u.ah.op==OP_ShiftLeft) ? 0 : -1;
64937 }else{
64938 memcpy(&u.ah.uA, &u.ah.iA, sizeof(u.ah.uA));
64939 if( u.ah.op==OP_ShiftLeft ){
64940 u.ah.uA <<= u.ah.iB;
64941 }else{
64942 u.ah.uA >>= u.ah.iB;
64943 /* Sign-extend on a right shift of a negative number */
64944 if( u.ah.iA<0 ) u.ah.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ah.iB);
64945 }
64946 memcpy(&u.ah.iA, &u.ah.uA, sizeof(u.ah.iA));
64947 }
64948 }
64949 pOut->u.i = u.ah.iA;
64950 MemSetTypeFlag(pOut, MEM_Int);
64951 break;
64952 }
64953
64954 /* Opcode: AddImm P1 P2 * * *
@@ -65185,30 +66503,30 @@
65185 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
65186 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
65187 case OP_Le: /* same as TK_LE, jump, in1, in3 */
65188 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
65189 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
65190 #if 0 /* local variables moved into u.ai */
65191 int res; /* Result of the comparison of pIn1 against pIn3 */
65192 char affinity; /* Affinity to use for comparison */
65193 u16 flags1; /* Copy of initial value of pIn1->flags */
65194 u16 flags3; /* Copy of initial value of pIn3->flags */
65195 #endif /* local variables moved into u.ai */
65196
65197 pIn1 = &aMem[pOp->p1];
65198 pIn3 = &aMem[pOp->p3];
65199 u.ai.flags1 = pIn1->flags;
65200 u.ai.flags3 = pIn3->flags;
65201 if( (u.ai.flags1 | u.ai.flags3)&MEM_Null ){
65202 /* One or both operands are NULL */
65203 if( pOp->p5 & SQLITE_NULLEQ ){
65204 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
65205 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
65206 ** or not both operands are null.
65207 */
65208 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
65209 u.ai.res = (u.ai.flags1 & u.ai.flags3 & MEM_Null)==0;
65210 }else{
65211 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
65212 ** then the result is always NULL.
65213 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
65214 */
@@ -65221,44 +66539,44 @@
65221 }
65222 break;
65223 }
65224 }else{
65225 /* Neither operand is NULL. Do a comparison. */
65226 u.ai.affinity = pOp->p5 & SQLITE_AFF_MASK;
65227 if( u.ai.affinity ){
65228 applyAffinity(pIn1, u.ai.affinity, encoding);
65229 applyAffinity(pIn3, u.ai.affinity, encoding);
65230 if( db->mallocFailed ) goto no_mem;
65231 }
65232
65233 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
65234 ExpandBlob(pIn1);
65235 ExpandBlob(pIn3);
65236 u.ai.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
65237 }
65238 switch( pOp->opcode ){
65239 case OP_Eq: u.ai.res = u.ai.res==0; break;
65240 case OP_Ne: u.ai.res = u.ai.res!=0; break;
65241 case OP_Lt: u.ai.res = u.ai.res<0; break;
65242 case OP_Le: u.ai.res = u.ai.res<=0; break;
65243 case OP_Gt: u.ai.res = u.ai.res>0; break;
65244 default: u.ai.res = u.ai.res>=0; break;
65245 }
65246
65247 if( pOp->p5 & SQLITE_STOREP2 ){
65248 pOut = &aMem[pOp->p2];
65249 memAboutToChange(p, pOut);
65250 MemSetTypeFlag(pOut, MEM_Int);
65251 pOut->u.i = u.ai.res;
65252 REGISTER_TRACE(pOp->p2, pOut);
65253 }else if( u.ai.res ){
65254 pc = pOp->p2-1;
65255 }
65256
65257 /* Undo any changes made by applyAffinity() to the input registers. */
65258 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.ai.flags1&MEM_TypeMask);
65259 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.ai.flags3&MEM_TypeMask);
65260 break;
65261 }
65262
65263 /* Opcode: Permutation * * * P4 *
65264 **
@@ -65289,50 +66607,50 @@
65289 ** The comparison is a sort comparison, so NULLs compare equal,
65290 ** NULLs are less than numbers, numbers are less than strings,
65291 ** and strings are less than blobs.
65292 */
65293 case OP_Compare: {
65294 #if 0 /* local variables moved into u.aj */
65295 int n;
65296 int i;
65297 int p1;
65298 int p2;
65299 const KeyInfo *pKeyInfo;
65300 int idx;
65301 CollSeq *pColl; /* Collating sequence to use on this term */
65302 int bRev; /* True for DESCENDING sort order */
65303 #endif /* local variables moved into u.aj */
65304
65305 u.aj.n = pOp->p3;
65306 u.aj.pKeyInfo = pOp->p4.pKeyInfo;
65307 assert( u.aj.n>0 );
65308 assert( u.aj.pKeyInfo!=0 );
65309 u.aj.p1 = pOp->p1;
65310 u.aj.p2 = pOp->p2;
65311 #if SQLITE_DEBUG
65312 if( aPermute ){
65313 int k, mx = 0;
65314 for(k=0; k<u.aj.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
65315 assert( u.aj.p1>0 && u.aj.p1+mx<=p->nMem+1 );
65316 assert( u.aj.p2>0 && u.aj.p2+mx<=p->nMem+1 );
65317 }else{
65318 assert( u.aj.p1>0 && u.aj.p1+u.aj.n<=p->nMem+1 );
65319 assert( u.aj.p2>0 && u.aj.p2+u.aj.n<=p->nMem+1 );
65320 }
65321 #endif /* SQLITE_DEBUG */
65322 for(u.aj.i=0; u.aj.i<u.aj.n; u.aj.i++){
65323 u.aj.idx = aPermute ? aPermute[u.aj.i] : u.aj.i;
65324 assert( memIsValid(&aMem[u.aj.p1+u.aj.idx]) );
65325 assert( memIsValid(&aMem[u.aj.p2+u.aj.idx]) );
65326 REGISTER_TRACE(u.aj.p1+u.aj.idx, &aMem[u.aj.p1+u.aj.idx]);
65327 REGISTER_TRACE(u.aj.p2+u.aj.idx, &aMem[u.aj.p2+u.aj.idx]);
65328 assert( u.aj.i<u.aj.pKeyInfo->nField );
65329 u.aj.pColl = u.aj.pKeyInfo->aColl[u.aj.i];
65330 u.aj.bRev = u.aj.pKeyInfo->aSortOrder[u.aj.i];
65331 iCompare = sqlite3MemCompare(&aMem[u.aj.p1+u.aj.idx], &aMem[u.aj.p2+u.aj.idx], u.aj.pColl);
65332 if( iCompare ){
65333 if( u.aj.bRev ) iCompare = -iCompare;
65334 break;
65335 }
65336 }
65337 aPermute = 0;
65338 break;
@@ -65373,39 +66691,39 @@
65373 ** even if the other input is NULL. A NULL and false or two NULLs
65374 ** give a NULL output.
65375 */
65376 case OP_And: /* same as TK_AND, in1, in2, out3 */
65377 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
65378 #if 0 /* local variables moved into u.ak */
65379 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65380 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
65381 #endif /* local variables moved into u.ak */
65382
65383 pIn1 = &aMem[pOp->p1];
65384 if( pIn1->flags & MEM_Null ){
65385 u.ak.v1 = 2;
65386 }else{
65387 u.ak.v1 = sqlite3VdbeIntValue(pIn1)!=0;
65388 }
65389 pIn2 = &aMem[pOp->p2];
65390 if( pIn2->flags & MEM_Null ){
65391 u.ak.v2 = 2;
65392 }else{
65393 u.ak.v2 = sqlite3VdbeIntValue(pIn2)!=0;
65394 }
65395 if( pOp->opcode==OP_And ){
65396 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
65397 u.ak.v1 = and_logic[u.ak.v1*3+u.ak.v2];
65398 }else{
65399 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
65400 u.ak.v1 = or_logic[u.ak.v1*3+u.ak.v2];
65401 }
65402 pOut = &aMem[pOp->p3];
65403 if( u.ak.v1==2 ){
65404 MemSetTypeFlag(pOut, MEM_Null);
65405 }else{
65406 pOut->u.i = u.ak.v1;
65407 MemSetTypeFlag(pOut, MEM_Int);
65408 }
65409 break;
65410 }
65411
@@ -65443,55 +66761,55 @@
65443 break;
65444 }
65445
65446 /* Opcode: Once P1 P2 * * *
65447 **
65448 ** Jump to P2 if the value in register P1 is a not null or zero. If
65449 ** the value is NULL or zero, fall through and change the P1 register
65450 ** to an integer 1.
65451 **
65452 ** When P1 is not used otherwise in a program, this opcode falls through
65453 ** once and jumps on all subsequent invocations. It is the equivalent
65454 ** of "OP_If P1 P2", followed by "OP_Integer 1 P1".
65455 */
 
 
 
 
 
 
 
 
 
 
65456 /* Opcode: If P1 P2 P3 * *
65457 **
65458 ** Jump to P2 if the value in register P1 is true. The value
65459 ** is considered true if it is numeric and non-zero. If the value
65460 ** in P1 is NULL then take the jump if P3 is true.
65461 */
65462 /* Opcode: IfNot P1 P2 P3 * *
65463 **
65464 ** Jump to P2 if the value in register P1 is False. The value
65465 ** is considered true if it has a numeric value of zero. If the value
65466 ** in P1 is NULL then take the jump if P3 is true.
65467 */
65468 case OP_Once: /* jump, in1 */
65469 case OP_If: /* jump, in1 */
65470 case OP_IfNot: { /* jump, in1 */
65471 #if 0 /* local variables moved into u.al */
65472 int c;
65473 #endif /* local variables moved into u.al */
65474 pIn1 = &aMem[pOp->p1];
65475 if( pIn1->flags & MEM_Null ){
65476 u.al.c = pOp->p3;
65477 }else{
65478 #ifdef SQLITE_OMIT_FLOATING_POINT
65479 u.al.c = sqlite3VdbeIntValue(pIn1)!=0;
65480 #else
65481 u.al.c = sqlite3VdbeRealValue(pIn1)!=0.0;
65482 #endif
65483 if( pOp->opcode==OP_IfNot ) u.al.c = !u.al.c;
65484 }
65485 if( u.al.c ){
65486 pc = pOp->p2-1;
65487 }else if( pOp->opcode==OP_Once ){
65488 assert( (pIn1->flags & (MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))==0 );
65489 memAboutToChange(p, pIn1);
65490 pIn1->flags = MEM_Int;
65491 pIn1->u.i = 1;
65492 REGISTER_TRACE(pOp->p1, pIn1);
65493 }
65494 break;
65495 }
65496
65497 /* Opcode: IsNull P1 P2 * * *
@@ -65536,11 +66854,11 @@
65536 ** then the cache of the cursor is reset prior to extracting the column.
65537 ** The first OP_Column against a pseudo-table after the value of the content
65538 ** register has changed should have this bit set.
65539 */
65540 case OP_Column: {
65541 #if 0 /* local variables moved into u.am */
65542 u32 payloadSize; /* Number of bytes in the record */
65543 i64 payloadSize64; /* Number of bytes in the record */
65544 int p1; /* P1 value of the opcode */
65545 int p2; /* column number to retrieve */
65546 VdbeCursor *pC; /* The VDBE cursor */
@@ -65560,130 +66878,130 @@
65560 u32 szField; /* Number of bytes in the content of a field */
65561 int szHdr; /* Size of the header size field at start of record */
65562 int avail; /* Number of bytes of available data */
65563 u32 t; /* A type code from the record header */
65564 Mem *pReg; /* PseudoTable input register */
65565 #endif /* local variables moved into u.am */
65566
65567
65568 u.am.p1 = pOp->p1;
65569 u.am.p2 = pOp->p2;
65570 u.am.pC = 0;
65571 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65572 assert( u.am.p1<p->nCursor );
65573 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65574 u.am.pDest = &aMem[pOp->p3];
65575 memAboutToChange(p, u.am.pDest);
65576 u.am.zRec = 0;
65577
65578 /* This block sets the variable u.am.payloadSize to be the total number of
65579 ** bytes in the record.
65580 **
65581 ** u.am.zRec is set to be the complete text of the record if it is available.
65582 ** The complete record text is always available for pseudo-tables
65583 ** If the record is stored in a cursor, the complete record text
65584 ** might be available in the u.am.pC->aRow cache. Or it might not be.
65585 ** If the data is unavailable, u.am.zRec is set to NULL.
65586 **
65587 ** We also compute the number of columns in the record. For cursors,
65588 ** the number of columns is stored in the VdbeCursor.nField element.
65589 */
65590 u.am.pC = p->apCsr[u.am.p1];
65591 assert( u.am.pC!=0 );
65592 #ifndef SQLITE_OMIT_VIRTUALTABLE
65593 assert( u.am.pC->pVtabCursor==0 );
65594 #endif
65595 u.am.pCrsr = u.am.pC->pCursor;
65596 if( u.am.pCrsr!=0 ){
65597 /* The record is stored in a B-Tree */
65598 rc = sqlite3VdbeCursorMoveto(u.am.pC);
65599 if( rc ) goto abort_due_to_error;
65600 if( u.am.pC->nullRow ){
65601 u.am.payloadSize = 0;
65602 }else if( u.am.pC->cacheStatus==p->cacheCtr ){
65603 u.am.payloadSize = u.am.pC->payloadSize;
65604 u.am.zRec = (char*)u.am.pC->aRow;
65605 }else if( u.am.pC->isIndex ){
65606 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65607 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
65608 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
65609 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65610 ** payload size, so it is impossible for u.am.payloadSize64 to be
65611 ** larger than 32 bits. */
65612 assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
65613 u.am.payloadSize = (u32)u.am.payloadSize64;
65614 }else{
65615 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65616 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65617 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65618 }
65619 }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65620 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65621 assert( u.am.pReg->flags & MEM_Blob );
65622 assert( memIsValid(u.am.pReg) );
65623 u.am.payloadSize = u.am.pReg->n;
65624 u.am.zRec = u.am.pReg->z;
65625 u.am.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
65626 assert( u.am.payloadSize==0 || u.am.zRec!=0 );
65627 }else{
65628 /* Consider the row to be NULL */
65629 u.am.payloadSize = 0;
65630 }
65631
65632 /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
65633 ** nullRow or because of a corrupt database. */
65634 if( u.am.payloadSize==0 ){
65635 MemSetTypeFlag(u.am.pDest, MEM_Null);
65636 goto op_column_out;
65637 }
65638 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65639 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65640 goto too_big;
65641 }
65642
65643 u.am.nField = u.am.pC->nField;
65644 assert( u.am.p2<u.am.nField );
65645
65646 /* Read and parse the table header. Store the results of the parse
65647 ** into the record header cache fields of the cursor.
65648 */
65649 u.am.aType = u.am.pC->aType;
65650 if( u.am.pC->cacheStatus==p->cacheCtr ){
65651 u.am.aOffset = u.am.pC->aOffset;
65652 }else{
65653 assert(u.am.aType);
65654 u.am.avail = 0;
65655 u.am.pC->aOffset = u.am.aOffset = &u.am.aType[u.am.nField];
65656 u.am.pC->payloadSize = u.am.payloadSize;
65657 u.am.pC->cacheStatus = p->cacheCtr;
65658
65659 /* Figure out how many bytes are in the header */
65660 if( u.am.zRec ){
65661 u.am.zData = u.am.zRec;
65662 }else{
65663 if( u.am.pC->isIndex ){
65664 u.am.zData = (char*)sqlite3BtreeKeyFetch(u.am.pCrsr, &u.am.avail);
65665 }else{
65666 u.am.zData = (char*)sqlite3BtreeDataFetch(u.am.pCrsr, &u.am.avail);
65667 }
65668 /* If KeyFetch()/DataFetch() managed to get the entire payload,
65669 ** save the payload in the u.am.pC->aRow cache. That will save us from
65670 ** having to make additional calls to fetch the content portion of
65671 ** the record.
65672 */
65673 assert( u.am.avail>=0 );
65674 if( u.am.payloadSize <= (u32)u.am.avail ){
65675 u.am.zRec = u.am.zData;
65676 u.am.pC->aRow = (u8*)u.am.zData;
65677 }else{
65678 u.am.pC->aRow = 0;
65679 }
65680 }
65681 /* The following assert is true in all cases accept when
65682 ** the database file has been corrupted externally.
65683 ** assert( u.am.zRec!=0 || u.am.avail>=u.am.payloadSize || u.am.avail>=9 ); */
65684 u.am.szHdr = getVarint32((u8*)u.am.zData, u.am.offset);
65685
65686 /* Make sure a corrupt database has not given us an oversize header.
65687 ** Do this now to avoid an oversize memory allocation.
65688 **
65689 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65690,146 +67008,146 @@
65690 ** types use so much data space that there can only be 4096 and 32 of
65691 ** them, respectively. So the maximum header length results from a
65692 ** 3-byte type for each of the maximum of 32768 columns plus three
65693 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
65694 */
65695 if( u.am.offset > 98307 ){
65696 rc = SQLITE_CORRUPT_BKPT;
65697 goto op_column_out;
65698 }
65699
65700 /* Compute in u.am.len the number of bytes of data we need to read in order
65701 ** to get u.am.nField type values. u.am.offset is an upper bound on this. But
65702 ** u.am.nField might be significantly less than the true number of columns
65703 ** in the table, and in that case, 5*u.am.nField+3 might be smaller than u.am.offset.
65704 ** We want to minimize u.am.len in order to limit the size of the memory
65705 ** allocation, especially if a corrupt database file has caused u.am.offset
65706 ** to be oversized. Offset is limited to 98307 above. But 98307 might
65707 ** still exceed Robson memory allocation limits on some configurations.
65708 ** On systems that cannot tolerate large memory allocations, u.am.nField*5+3
65709 ** will likely be much smaller since u.am.nField will likely be less than
65710 ** 20 or so. This insures that Robson memory allocation limits are
65711 ** not exceeded even for corrupt database files.
65712 */
65713 u.am.len = u.am.nField*5 + 3;
65714 if( u.am.len > (int)u.am.offset ) u.am.len = (int)u.am.offset;
65715
65716 /* The KeyFetch() or DataFetch() above are fast and will get the entire
65717 ** record header in most cases. But they will fail to get the complete
65718 ** record header if the record header does not fit on a single page
65719 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
65720 ** acquire the complete header text.
65721 */
65722 if( !u.am.zRec && u.am.avail<u.am.len ){
65723 u.am.sMem.flags = 0;
65724 u.am.sMem.db = 0;
65725 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, 0, u.am.len, u.am.pC->isIndex, &u.am.sMem);
65726 if( rc!=SQLITE_OK ){
65727 goto op_column_out;
65728 }
65729 u.am.zData = u.am.sMem.z;
65730 }
65731 u.am.zEndHdr = (u8 *)&u.am.zData[u.am.len];
65732 u.am.zIdx = (u8 *)&u.am.zData[u.am.szHdr];
65733
65734 /* Scan the header and use it to fill in the u.am.aType[] and u.am.aOffset[]
65735 ** arrays. u.am.aType[u.am.i] will contain the type integer for the u.am.i-th
65736 ** column and u.am.aOffset[u.am.i] will contain the u.am.offset from the beginning
65737 ** of the record to the start of the data for the u.am.i-th column
65738 */
65739 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65740 if( u.am.zIdx<u.am.zEndHdr ){
65741 u.am.aOffset[u.am.i] = u.am.offset;
65742 if( u.am.zIdx[0]<0x80 ){
65743 u.am.t = u.am.zIdx[0];
65744 u.am.zIdx++;
65745 }else{
65746 u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65747 }
65748 u.am.aType[u.am.i] = u.am.t;
65749 u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
65750 u.am.offset += u.am.szField;
65751 if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65752 u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65753 break;
65754 }
65755 }else{
65756 /* If u.am.i is less that u.am.nField, then there are less fields in this
65757 ** record than SetNumColumns indicated there are columns in the
65758 ** table. Set the u.am.offset for any extra columns not present in
65759 ** the record to 0. This tells code below to store a NULL
65760 ** instead of deserializing a value from the record.
65761 */
65762 u.am.aOffset[u.am.i] = 0;
65763 }
65764 }
65765 sqlite3VdbeMemRelease(&u.am.sMem);
65766 u.am.sMem.flags = MEM_Null;
65767
65768 /* If we have read more header data than was contained in the header,
65769 ** or if the end of the last field appears to be past the end of the
65770 ** record, or if the end of the last field appears to be before the end
65771 ** of the record (when all fields present), then we must be dealing
65772 ** with a corrupt database.
65773 */
65774 if( (u.am.zIdx > u.am.zEndHdr) || (u.am.offset > u.am.payloadSize)
65775 || (u.am.zIdx==u.am.zEndHdr && u.am.offset!=u.am.payloadSize) ){
65776 rc = SQLITE_CORRUPT_BKPT;
65777 goto op_column_out;
65778 }
65779 }
65780
65781 /* Get the column information. If u.am.aOffset[u.am.p2] is non-zero, then
65782 ** deserialize the value from the record. If u.am.aOffset[u.am.p2] is zero,
65783 ** then there are not enough fields in the record to satisfy the
65784 ** request. In this case, set the value NULL or to P4 if P4 is
65785 ** a pointer to a Mem object.
65786 */
65787 if( u.am.aOffset[u.am.p2] ){
65788 assert( rc==SQLITE_OK );
65789 if( u.am.zRec ){
65790 MemReleaseExt(u.am.pDest);
65791 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65792 }else{
65793 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65794 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65795 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
65796 if( rc!=SQLITE_OK ){
65797 goto op_column_out;
65798 }
65799 u.am.zData = u.am.sMem.z;
65800 sqlite3VdbeSerialGet((u8*)u.am.zData, u.am.aType[u.am.p2], u.am.pDest);
65801 }
65802 u.am.pDest->enc = encoding;
65803 }else{
65804 if( pOp->p4type==P4_MEM ){
65805 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65806 }else{
65807 MemSetTypeFlag(u.am.pDest, MEM_Null);
65808 }
65809 }
65810
65811 /* If we dynamically allocated space to hold the data (in the
65812 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
65813 ** dynamically allocated space over to the u.am.pDest structure.
65814 ** This prevents a memory copy.
65815 */
65816 if( u.am.sMem.zMalloc ){
65817 assert( u.am.sMem.z==u.am.sMem.zMalloc );
65818 assert( !(u.am.pDest->flags & MEM_Dyn) );
65819 assert( !(u.am.pDest->flags & (MEM_Blob|MEM_Str)) || u.am.pDest->z==u.am.sMem.z );
65820 u.am.pDest->flags &= ~(MEM_Ephem|MEM_Static);
65821 u.am.pDest->flags |= MEM_Term;
65822 u.am.pDest->z = u.am.sMem.z;
65823 u.am.pDest->zMalloc = u.am.sMem.zMalloc;
65824 }
65825
65826 rc = sqlite3VdbeMemMakeWriteable(u.am.pDest);
65827
65828 op_column_out:
65829 UPDATE_MAX_BLOBSIZE(u.am.pDest);
65830 REGISTER_TRACE(pOp->p3, u.am.pDest);
65831 break;
65832 }
65833
65834 /* Opcode: Affinity P1 P2 * P4 *
65835 **
@@ -65838,24 +67156,24 @@
65838 ** P4 is a string that is P2 characters long. The nth character of the
65839 ** string indicates the column affinity that should be used for the nth
65840 ** memory cell in the range.
65841 */
65842 case OP_Affinity: {
65843 #if 0 /* local variables moved into u.an */
65844 const char *zAffinity; /* The affinity to be applied */
65845 char cAff; /* A single character of affinity */
65846 #endif /* local variables moved into u.an */
65847
65848 u.an.zAffinity = pOp->p4.z;
65849 assert( u.an.zAffinity!=0 );
65850 assert( u.an.zAffinity[pOp->p2]==0 );
65851 pIn1 = &aMem[pOp->p1];
65852 while( (u.an.cAff = *(u.an.zAffinity++))!=0 ){
65853 assert( pIn1 <= &p->aMem[p->nMem] );
65854 assert( memIsValid(pIn1) );
65855 ExpandBlob(pIn1);
65856 applyAffinity(pIn1, u.an.cAff, encoding);
65857 pIn1++;
65858 }
65859 break;
65860 }
65861
@@ -65873,11 +67191,11 @@
65873 ** macros defined in sqliteInt.h.
65874 **
65875 ** If P4 is NULL then all index fields have the affinity NONE.
65876 */
65877 case OP_MakeRecord: {
65878 #if 0 /* local variables moved into u.ao */
65879 u8 *zNewRecord; /* A buffer to hold the data for the new record */
65880 Mem *pRec; /* The new record */
65881 u64 nData; /* Number of bytes of data space */
65882 int nHdr; /* Number of bytes of header space */
65883 i64 nByte; /* Data space required for this record */
@@ -65889,11 +67207,11 @@
65889 int nField; /* Number of fields in the record */
65890 char *zAffinity; /* The affinity string for the record */
65891 int file_format; /* File format to use for encoding */
65892 int i; /* Space used in zNewRecord[] */
65893 int len; /* Length of a field */
65894 #endif /* local variables moved into u.ao */
65895
65896 /* Assuming the record contains N fields, the record format looks
65897 ** like this:
65898 **
65899 ** ------------------------------------------------------------------------
@@ -65906,87 +67224,87 @@
65906 ** Each type field is a varint representing the serial type of the
65907 ** corresponding data element (see sqlite3VdbeSerialType()). The
65908 ** hdr-size field is also a varint which is the offset from the beginning
65909 ** of the record to data0.
65910 */
65911 u.ao.nData = 0; /* Number of bytes of data space */
65912 u.ao.nHdr = 0; /* Number of bytes of header space */
65913 u.ao.nZero = 0; /* Number of zero bytes at the end of the record */
65914 u.ao.nField = pOp->p1;
65915 u.ao.zAffinity = pOp->p4.z;
65916 assert( u.ao.nField>0 && pOp->p2>0 && pOp->p2+u.ao.nField<=p->nMem+1 );
65917 u.ao.pData0 = &aMem[u.ao.nField];
65918 u.ao.nField = pOp->p2;
65919 u.ao.pLast = &u.ao.pData0[u.ao.nField-1];
65920 u.ao.file_format = p->minWriteFileFormat;
65921
65922 /* Identify the output register */
65923 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
65924 pOut = &aMem[pOp->p3];
65925 memAboutToChange(p, pOut);
65926
65927 /* Loop through the elements that will make up the record to figure
65928 ** out how much space is required for the new record.
65929 */
65930 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65931 assert( memIsValid(u.ao.pRec) );
65932 if( u.ao.zAffinity ){
65933 applyAffinity(u.ao.pRec, u.ao.zAffinity[u.ao.pRec-u.ao.pData0], encoding);
65934 }
65935 if( u.ao.pRec->flags&MEM_Zero && u.ao.pRec->n>0 ){
65936 sqlite3VdbeMemExpandBlob(u.ao.pRec);
65937 }
65938 u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65939 u.ao.len = sqlite3VdbeSerialTypeLen(u.ao.serial_type);
65940 u.ao.nData += u.ao.len;
65941 u.ao.nHdr += sqlite3VarintLen(u.ao.serial_type);
65942 if( u.ao.pRec->flags & MEM_Zero ){
65943 /* Only pure zero-filled BLOBs can be input to this Opcode.
65944 ** We do not allow blobs with a prefix and a zero-filled tail. */
65945 u.ao.nZero += u.ao.pRec->u.nZero;
65946 }else if( u.ao.len ){
65947 u.ao.nZero = 0;
65948 }
65949 }
65950
65951 /* Add the initial header varint and total the size */
65952 u.ao.nHdr += u.ao.nVarint = sqlite3VarintLen(u.ao.nHdr);
65953 if( u.ao.nVarint<sqlite3VarintLen(u.ao.nHdr) ){
65954 u.ao.nHdr++;
65955 }
65956 u.ao.nByte = u.ao.nHdr+u.ao.nData-u.ao.nZero;
65957 if( u.ao.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
65958 goto too_big;
65959 }
65960
65961 /* Make sure the output register has a buffer large enough to store
65962 ** the new record. The output register (pOp->p3) is not allowed to
65963 ** be one of the input registers (because the following call to
65964 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
65965 */
65966 if( sqlite3VdbeMemGrow(pOut, (int)u.ao.nByte, 0) ){
65967 goto no_mem;
65968 }
65969 u.ao.zNewRecord = (u8 *)pOut->z;
65970
65971 /* Write the record */
65972 u.ao.i = putVarint32(u.ao.zNewRecord, u.ao.nHdr);
65973 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){
65974 u.ao.serial_type = sqlite3VdbeSerialType(u.ao.pRec, u.ao.file_format);
65975 u.ao.i += putVarint32(&u.ao.zNewRecord[u.ao.i], u.ao.serial_type); /* serial type */
65976 }
65977 for(u.ao.pRec=u.ao.pData0; u.ao.pRec<=u.ao.pLast; u.ao.pRec++){ /* serial data */
65978 u.ao.i += sqlite3VdbeSerialPut(&u.ao.zNewRecord[u.ao.i], (int)(u.ao.nByte-u.ao.i), u.ao.pRec,u.ao.file_format);
65979 }
65980 assert( u.ao.i==u.ao.nByte );
65981
65982 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65983 pOut->n = (int)u.ao.nByte;
65984 pOut->flags = MEM_Blob | MEM_Dyn;
65985 pOut->xDel = 0;
65986 if( u.ao.nZero ){
65987 pOut->u.nZero = u.ao.nZero;
65988 pOut->flags |= MEM_Zero;
65989 }
65990 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
65991 REGISTER_TRACE(pOp->p3, pOut);
65992 UPDATE_MAX_BLOBSIZE(pOut);
@@ -65998,22 +67316,22 @@
65998 ** Store the number of entries (an integer value) in the table or index
65999 ** opened by cursor P1 in register P2
66000 */
66001 #ifndef SQLITE_OMIT_BTREECOUNT
66002 case OP_Count: { /* out2-prerelease */
66003 #if 0 /* local variables moved into u.ap */
66004 i64 nEntry;
66005 BtCursor *pCrsr;
66006 #endif /* local variables moved into u.ap */
66007
66008 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
66009 if( ALWAYS(u.ap.pCrsr) ){
66010 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
66011 }else{
66012 u.ap.nEntry = 0;
66013 }
66014 pOut->u.i = u.ap.nEntry;
66015 break;
66016 }
66017 #endif
66018
66019 /* Opcode: Savepoint P1 * * P4 *
@@ -66021,42 +67339,42 @@
66021 ** Open, release or rollback the savepoint named by parameter P4, depending
66022 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
66023 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
66024 */
66025 case OP_Savepoint: {
66026 #if 0 /* local variables moved into u.aq */
66027 int p1; /* Value of P1 operand */
66028 char *zName; /* Name of savepoint */
66029 int nName;
66030 Savepoint *pNew;
66031 Savepoint *pSavepoint;
66032 Savepoint *pTmp;
66033 int iSavepoint;
66034 int ii;
66035 #endif /* local variables moved into u.aq */
66036
66037 u.aq.p1 = pOp->p1;
66038 u.aq.zName = pOp->p4.z;
66039
66040 /* Assert that the u.aq.p1 parameter is valid. Also that if there is no open
66041 ** transaction, then there cannot be any savepoints.
66042 */
66043 assert( db->pSavepoint==0 || db->autoCommit==0 );
66044 assert( u.aq.p1==SAVEPOINT_BEGIN||u.aq.p1==SAVEPOINT_RELEASE||u.aq.p1==SAVEPOINT_ROLLBACK );
66045 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
66046 assert( checkSavepointCount(db) );
66047
66048 if( u.aq.p1==SAVEPOINT_BEGIN ){
66049 if( db->writeVdbeCnt>0 ){
66050 /* A new savepoint cannot be created if there are active write
66051 ** statements (i.e. open read/write incremental blob handles).
66052 */
66053 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
66054 "SQL statements in progress");
66055 rc = SQLITE_BUSY;
66056 }else{
66057 u.aq.nName = sqlite3Strlen30(u.aq.zName);
66058
66059 #ifndef SQLITE_OMIT_VIRTUALTABLE
66060 /* This call is Ok even if this savepoint is actually a transaction
66061 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
66062 ** If this is a transaction savepoint being opened, it is guaranteed
@@ -66066,14 +67384,14 @@
66066 db->nStatement+db->nSavepoint);
66067 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66068 #endif
66069
66070 /* Create a new savepoint structure. */
66071 u.aq.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.aq.nName+1);
66072 if( u.aq.pNew ){
66073 u.aq.pNew->zName = (char *)&u.aq.pNew[1];
66074 memcpy(u.aq.pNew->zName, u.aq.zName, u.aq.nName+1);
66075
66076 /* If there is no open transaction, then mark this as a special
66077 ** "transaction savepoint". */
66078 if( db->autoCommit ){
66079 db->autoCommit = 0;
@@ -66081,50 +67399,50 @@
66081 }else{
66082 db->nSavepoint++;
66083 }
66084
66085 /* Link the new savepoint into the database handle's list. */
66086 u.aq.pNew->pNext = db->pSavepoint;
66087 db->pSavepoint = u.aq.pNew;
66088 u.aq.pNew->nDeferredCons = db->nDeferredCons;
66089 }
66090 }
66091 }else{
66092 u.aq.iSavepoint = 0;
66093
66094 /* Find the named savepoint. If there is no such savepoint, then an
66095 ** an error is returned to the user. */
66096 for(
66097 u.aq.pSavepoint = db->pSavepoint;
66098 u.aq.pSavepoint && sqlite3StrICmp(u.aq.pSavepoint->zName, u.aq.zName);
66099 u.aq.pSavepoint = u.aq.pSavepoint->pNext
66100 ){
66101 u.aq.iSavepoint++;
66102 }
66103 if( !u.aq.pSavepoint ){
66104 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.aq.zName);
66105 rc = SQLITE_ERROR;
66106 }else if(
66107 db->writeVdbeCnt>0 || (u.aq.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
66108 ){
66109 /* It is not possible to release (commit) a savepoint if there are
66110 ** active write statements. It is not possible to rollback a savepoint
66111 ** if there are any active statements at all.
66112 */
66113 sqlite3SetString(&p->zErrMsg, db,
66114 "cannot %s savepoint - SQL statements in progress",
66115 (u.aq.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
66116 );
66117 rc = SQLITE_BUSY;
66118 }else{
66119
66120 /* Determine whether or not this is a transaction savepoint. If so,
66121 ** and this is a RELEASE command, then the current transaction
66122 ** is committed.
66123 */
66124 int isTransaction = u.aq.pSavepoint->pNext==0 && db->isTransactionSavepoint;
66125 if( isTransaction && u.aq.p1==SAVEPOINT_RELEASE ){
66126 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66127 goto vdbe_return;
66128 }
66129 db->autoCommit = 1;
66130 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66134,50 +67452,50 @@
66134 goto vdbe_return;
66135 }
66136 db->isTransactionSavepoint = 0;
66137 rc = p->rc;
66138 }else{
66139 u.aq.iSavepoint = db->nSavepoint - u.aq.iSavepoint - 1;
66140 for(u.aq.ii=0; u.aq.ii<db->nDb; u.aq.ii++){
66141 rc = sqlite3BtreeSavepoint(db->aDb[u.aq.ii].pBt, u.aq.p1, u.aq.iSavepoint);
66142 if( rc!=SQLITE_OK ){
66143 goto abort_due_to_error;
66144 }
66145 }
66146 if( u.aq.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
66147 sqlite3ExpirePreparedStatements(db);
66148 sqlite3ResetInternalSchema(db, -1);
66149 db->flags = (db->flags | SQLITE_InternChanges);
66150 }
66151 }
66152
66153 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
66154 ** savepoints nested inside of the savepoint being operated on. */
66155 while( db->pSavepoint!=u.aq.pSavepoint ){
66156 u.aq.pTmp = db->pSavepoint;
66157 db->pSavepoint = u.aq.pTmp->pNext;
66158 sqlite3DbFree(db, u.aq.pTmp);
66159 db->nSavepoint--;
66160 }
66161
66162 /* If it is a RELEASE, then destroy the savepoint being operated on
66163 ** too. If it is a ROLLBACK TO, then set the number of deferred
66164 ** constraint violations present in the database to the value stored
66165 ** when the savepoint was created. */
66166 if( u.aq.p1==SAVEPOINT_RELEASE ){
66167 assert( u.aq.pSavepoint==db->pSavepoint );
66168 db->pSavepoint = u.aq.pSavepoint->pNext;
66169 sqlite3DbFree(db, u.aq.pSavepoint);
66170 if( !isTransaction ){
66171 db->nSavepoint--;
66172 }
66173 }else{
66174 db->nDeferredCons = u.aq.pSavepoint->nDeferredCons;
66175 }
66176
66177 if( !isTransaction ){
66178 rc = sqlite3VtabSavepoint(db, u.aq.p1, u.aq.iSavepoint);
66179 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66180 }
66181 }
66182 }
66183
@@ -66192,50 +67510,50 @@
66192 ** there are active writing VMs or active VMs that use shared cache.
66193 **
66194 ** This instruction causes the VM to halt.
66195 */
66196 case OP_AutoCommit: {
66197 #if 0 /* local variables moved into u.ar */
66198 int desiredAutoCommit;
66199 int iRollback;
66200 int turnOnAC;
66201 #endif /* local variables moved into u.ar */
66202
66203 u.ar.desiredAutoCommit = pOp->p1;
66204 u.ar.iRollback = pOp->p2;
66205 u.ar.turnOnAC = u.ar.desiredAutoCommit && !db->autoCommit;
66206 assert( u.ar.desiredAutoCommit==1 || u.ar.desiredAutoCommit==0 );
66207 assert( u.ar.desiredAutoCommit==1 || u.ar.iRollback==0 );
66208 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
66209
66210 if( u.ar.turnOnAC && u.ar.iRollback && db->activeVdbeCnt>1 ){
66211 /* If this instruction implements a ROLLBACK and other VMs are
66212 ** still running, and a transaction is active, return an error indicating
66213 ** that the other VMs must complete first.
66214 */
66215 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
66216 "SQL statements in progress");
66217 rc = SQLITE_BUSY;
66218 }else if( u.ar.turnOnAC && !u.ar.iRollback && db->writeVdbeCnt>0 ){
66219 /* If this instruction implements a COMMIT and other VMs are writing
66220 ** return an error indicating that the other VMs must complete first.
66221 */
66222 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
66223 "SQL statements in progress");
66224 rc = SQLITE_BUSY;
66225 }else if( u.ar.desiredAutoCommit!=db->autoCommit ){
66226 if( u.ar.iRollback ){
66227 assert( u.ar.desiredAutoCommit==1 );
66228 sqlite3RollbackAll(db);
66229 db->autoCommit = 1;
66230 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
66231 goto vdbe_return;
66232 }else{
66233 db->autoCommit = (u8)u.ar.desiredAutoCommit;
66234 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
66235 p->pc = pc;
66236 db->autoCommit = (u8)(1-u.ar.desiredAutoCommit);
66237 p->rc = rc = SQLITE_BUSY;
66238 goto vdbe_return;
66239 }
66240 }
66241 assert( db->nStatement==0 );
@@ -66246,12 +67564,12 @@
66246 rc = SQLITE_ERROR;
66247 }
66248 goto vdbe_return;
66249 }else{
66250 sqlite3SetString(&p->zErrMsg, db,
66251 (!u.ar.desiredAutoCommit)?"cannot start a transaction within a transaction":(
66252 (u.ar.iRollback)?"cannot rollback - no transaction is active":
66253 "cannot commit - no transaction is active"));
66254
66255 rc = SQLITE_ERROR;
66256 }
66257 break;
@@ -66287,20 +67605,20 @@
66287 ** will automatically commit when the VDBE halts.
66288 **
66289 ** If P2 is zero, then a read-lock is obtained on the database file.
66290 */
66291 case OP_Transaction: {
66292 #if 0 /* local variables moved into u.as */
66293 Btree *pBt;
66294 #endif /* local variables moved into u.as */
66295
66296 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66297 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66298 u.as.pBt = db->aDb[pOp->p1].pBt;
66299
66300 if( u.as.pBt ){
66301 rc = sqlite3BtreeBeginTrans(u.as.pBt, pOp->p2);
66302 if( rc==SQLITE_BUSY ){
66303 p->pc = pc;
66304 p->rc = rc = SQLITE_BUSY;
66305 goto vdbe_return;
66306 }
@@ -66309,20 +67627,20 @@
66309 }
66310
66311 if( pOp->p2 && p->usesStmtJournal
66312 && (db->autoCommit==0 || db->activeVdbeCnt>1)
66313 ){
66314 assert( sqlite3BtreeIsInTrans(u.as.pBt) );
66315 if( p->iStatement==0 ){
66316 assert( db->nStatement>=0 && db->nSavepoint>=0 );
66317 db->nStatement++;
66318 p->iStatement = db->nSavepoint + db->nStatement;
66319 }
66320
66321 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
66322 if( rc==SQLITE_OK ){
66323 rc = sqlite3BtreeBeginStmt(u.as.pBt, p->iStatement);
66324 }
66325
66326 /* Store the current value of the database handles deferred constraint
66327 ** counter. If the statement transaction needs to be rolled back,
66328 ** the value of this counter needs to be restored too. */
@@ -66343,25 +67661,25 @@
66343 ** There must be a read-lock on the database (either a transaction
66344 ** must be started or there must be an open cursor) before
66345 ** executing this instruction.
66346 */
66347 case OP_ReadCookie: { /* out2-prerelease */
66348 #if 0 /* local variables moved into u.at */
66349 int iMeta;
66350 int iDb;
66351 int iCookie;
66352 #endif /* local variables moved into u.at */
66353
66354 u.at.iDb = pOp->p1;
66355 u.at.iCookie = pOp->p3;
66356 assert( pOp->p3<SQLITE_N_BTREE_META );
66357 assert( u.at.iDb>=0 && u.at.iDb<db->nDb );
66358 assert( db->aDb[u.at.iDb].pBt!=0 );
66359 assert( (p->btreeMask & (((yDbMask)1)<<u.at.iDb))!=0 );
66360
66361 sqlite3BtreeGetMeta(db->aDb[u.at.iDb].pBt, u.at.iCookie, (u32 *)&u.at.iMeta);
66362 pOut->u.i = u.at.iMeta;
66363 break;
66364 }
66365
66366 /* Opcode: SetCookie P1 P2 P3 * *
66367 **
@@ -66372,30 +67690,30 @@
66372 ** database file used to store temporary tables.
66373 **
66374 ** A transaction must be started before executing this opcode.
66375 */
66376 case OP_SetCookie: { /* in3 */
66377 #if 0 /* local variables moved into u.au */
66378 Db *pDb;
66379 #endif /* local variables moved into u.au */
66380 assert( pOp->p2<SQLITE_N_BTREE_META );
66381 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66382 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66383 u.au.pDb = &db->aDb[pOp->p1];
66384 assert( u.au.pDb->pBt!=0 );
66385 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66386 pIn3 = &aMem[pOp->p3];
66387 sqlite3VdbeMemIntegerify(pIn3);
66388 /* See note about index shifting on OP_ReadCookie */
66389 rc = sqlite3BtreeUpdateMeta(u.au.pDb->pBt, pOp->p2, (int)pIn3->u.i);
66390 if( pOp->p2==BTREE_SCHEMA_VERSION ){
66391 /* When the schema cookie changes, record the new cookie internally */
66392 u.au.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
66393 db->flags |= SQLITE_InternChanges;
66394 }else if( pOp->p2==BTREE_FILE_FORMAT ){
66395 /* Record changes in the file format */
66396 u.au.pDb->pSchema->file_format = (u8)pIn3->u.i;
66397 }
66398 if( pOp->p1==1 ){
66399 /* Invalidate all prepared statements whenever the TEMP database
66400 ** schema is changed. Ticket #1644 */
66401 sqlite3ExpirePreparedStatements(db);
@@ -66421,27 +67739,27 @@
66421 ** Either a transaction needs to have been started or an OP_Open needs
66422 ** to be executed (to establish a read lock) before this opcode is
66423 ** invoked.
66424 */
66425 case OP_VerifyCookie: {
66426 #if 0 /* local variables moved into u.av */
66427 int iMeta;
66428 int iGen;
66429 Btree *pBt;
66430 #endif /* local variables moved into u.av */
66431
66432 assert( pOp->p1>=0 && pOp->p1<db->nDb );
66433 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
66434 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
66435 u.av.pBt = db->aDb[pOp->p1].pBt;
66436 if( u.av.pBt ){
66437 sqlite3BtreeGetMeta(u.av.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.av.iMeta);
66438 u.av.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
66439 }else{
66440 u.av.iGen = u.av.iMeta = 0;
66441 }
66442 if( u.av.iMeta!=pOp->p2 || u.av.iGen!=pOp->p3 ){
66443 sqlite3DbFree(db, p->zErrMsg);
66444 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
66445 /* If the schema-cookie from the database file matches the cookie
66446 ** stored with the in-memory representation of the schema, do
66447 ** not reload the schema from the database file.
@@ -66453,11 +67771,11 @@
66453 ** discard the database schema, as the user code implementing the
66454 ** v-table would have to be ready for the sqlite3_vtab structure itself
66455 ** to be invalidated whenever sqlite3_step() is called from within
66456 ** a v-table method.
66457 */
66458 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.av.iMeta ){
66459 sqlite3ResetInternalSchema(db, pOp->p1);
66460 }
66461
66462 p->expired = 1;
66463 rc = SQLITE_SCHEMA;
@@ -66514,86 +67832,86 @@
66514 **
66515 ** See also OpenRead.
66516 */
66517 case OP_OpenRead:
66518 case OP_OpenWrite: {
66519 #if 0 /* local variables moved into u.aw */
66520 int nField;
66521 KeyInfo *pKeyInfo;
66522 int p2;
66523 int iDb;
66524 int wrFlag;
66525 Btree *pX;
66526 VdbeCursor *pCur;
66527 Db *pDb;
66528 #endif /* local variables moved into u.aw */
66529
66530 if( p->expired ){
66531 rc = SQLITE_ABORT;
66532 break;
66533 }
66534
66535 u.aw.nField = 0;
66536 u.aw.pKeyInfo = 0;
66537 u.aw.p2 = pOp->p2;
66538 u.aw.iDb = pOp->p3;
66539 assert( u.aw.iDb>=0 && u.aw.iDb<db->nDb );
66540 assert( (p->btreeMask & (((yDbMask)1)<<u.aw.iDb))!=0 );
66541 u.aw.pDb = &db->aDb[u.aw.iDb];
66542 u.aw.pX = u.aw.pDb->pBt;
66543 assert( u.aw.pX!=0 );
66544 if( pOp->opcode==OP_OpenWrite ){
66545 u.aw.wrFlag = 1;
66546 assert( sqlite3SchemaMutexHeld(db, u.aw.iDb, 0) );
66547 if( u.aw.pDb->pSchema->file_format < p->minWriteFileFormat ){
66548 p->minWriteFileFormat = u.aw.pDb->pSchema->file_format;
66549 }
66550 }else{
66551 u.aw.wrFlag = 0;
66552 }
66553 if( pOp->p5 ){
66554 assert( u.aw.p2>0 );
66555 assert( u.aw.p2<=p->nMem );
66556 pIn2 = &aMem[u.aw.p2];
66557 assert( memIsValid(pIn2) );
66558 assert( (pIn2->flags & MEM_Int)!=0 );
66559 sqlite3VdbeMemIntegerify(pIn2);
66560 u.aw.p2 = (int)pIn2->u.i;
66561 /* The u.aw.p2 value always comes from a prior OP_CreateTable opcode and
66562 ** that opcode will always set the u.aw.p2 value to 2 or more or else fail.
66563 ** If there were a failure, the prepared statement would have halted
66564 ** before reaching this instruction. */
66565 if( NEVER(u.aw.p2<2) ) {
66566 rc = SQLITE_CORRUPT_BKPT;
66567 goto abort_due_to_error;
66568 }
66569 }
66570 if( pOp->p4type==P4_KEYINFO ){
66571 u.aw.pKeyInfo = pOp->p4.pKeyInfo;
66572 u.aw.pKeyInfo->enc = ENC(p->db);
66573 u.aw.nField = u.aw.pKeyInfo->nField+1;
66574 }else if( pOp->p4type==P4_INT32 ){
66575 u.aw.nField = pOp->p4.i;
66576 }
66577 assert( pOp->p1>=0 );
66578 u.aw.pCur = allocateCursor(p, pOp->p1, u.aw.nField, u.aw.iDb, 1);
66579 if( u.aw.pCur==0 ) goto no_mem;
66580 u.aw.pCur->nullRow = 1;
66581 u.aw.pCur->isOrdered = 1;
66582 rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66583 u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66584
66585 /* Since it performs no memory allocation or IO, the only value that
66586 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66587 assert( rc==SQLITE_OK );
66588
66589 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66590 ** SQLite used to check if the root-page flags were sane at this point
66591 ** and report database corruption if they were not, but this check has
66592 ** since moved into the btree layer. */
66593 u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66594 u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66595 break;
66596 }
66597
66598 /* Opcode: OpenEphemeral P1 P2 * P4 P5
66599 **
@@ -66625,28 +67943,28 @@
66625 ** by this opcode will be used for automatically created transient
66626 ** indices in joins.
66627 */
66628 case OP_OpenAutoindex:
66629 case OP_OpenEphemeral: {
66630 #if 0 /* local variables moved into u.ax */
66631 VdbeCursor *pCx;
66632 #endif /* local variables moved into u.ax */
66633 static const int vfsFlags =
66634 SQLITE_OPEN_READWRITE |
66635 SQLITE_OPEN_CREATE |
66636 SQLITE_OPEN_EXCLUSIVE |
66637 SQLITE_OPEN_DELETEONCLOSE |
66638 SQLITE_OPEN_TRANSIENT_DB;
66639
66640 assert( pOp->p1>=0 );
66641 u.ax.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66642 if( u.ax.pCx==0 ) goto no_mem;
66643 u.ax.pCx->nullRow = 1;
66644 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ax.pCx->pBt,
66645 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
66646 if( rc==SQLITE_OK ){
66647 rc = sqlite3BtreeBeginTrans(u.ax.pCx->pBt, 1);
66648 }
66649 if( rc==SQLITE_OK ){
66650 /* If a transient index is required, create it by calling
66651 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
66652 ** opening it. If a transient table is required, just use the
@@ -66653,26 +67971,26 @@
66653 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
66654 */
66655 if( pOp->p4.pKeyInfo ){
66656 int pgno;
66657 assert( pOp->p4type==P4_KEYINFO );
66658 rc = sqlite3BtreeCreateTable(u.ax.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
66659 if( rc==SQLITE_OK ){
66660 assert( pgno==MASTER_ROOT+1 );
66661 rc = sqlite3BtreeCursor(u.ax.pCx->pBt, pgno, 1,
66662 (KeyInfo*)pOp->p4.z, u.ax.pCx->pCursor);
66663 u.ax.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66664 u.ax.pCx->pKeyInfo->enc = ENC(p->db);
66665 }
66666 u.ax.pCx->isTable = 0;
66667 }else{
66668 rc = sqlite3BtreeCursor(u.ax.pCx->pBt, MASTER_ROOT, 1, 0, u.ax.pCx->pCursor);
66669 u.ax.pCx->isTable = 1;
66670 }
66671 }
66672 u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66673 u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66674 break;
66675 }
66676
66677 /* Opcode: OpenSorter P1 P2 * P4 *
66678 **
@@ -66679,20 +67997,20 @@
66679 ** This opcode works like OP_OpenEphemeral except that it opens
66680 ** a transient index that is specifically designed to sort large
66681 ** tables using an external merge-sort algorithm.
66682 */
66683 case OP_SorterOpen: {
66684 #if 0 /* local variables moved into u.ay */
66685 VdbeCursor *pCx;
66686 #endif /* local variables moved into u.ay */
66687 #ifndef SQLITE_OMIT_MERGE_SORT
66688 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66689 if( u.ay.pCx==0 ) goto no_mem;
66690 u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66691 u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66692 u.ay.pCx->isSorter = 1;
66693 rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
66694 #else
66695 pOp->opcode = OP_OpenEphemeral;
66696 pc--;
66697 #endif
66698 break;
@@ -66712,21 +68030,21 @@
66712 **
66713 ** P3 is the number of fields in the records that will be stored by
66714 ** the pseudo-table.
66715 */
66716 case OP_OpenPseudo: {
66717 #if 0 /* local variables moved into u.az */
66718 VdbeCursor *pCx;
66719 #endif /* local variables moved into u.az */
66720
66721 assert( pOp->p1>=0 );
66722 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66723 if( u.az.pCx==0 ) goto no_mem;
66724 u.az.pCx->nullRow = 1;
66725 u.az.pCx->pseudoTableReg = pOp->p2;
66726 u.az.pCx->isTable = 1;
66727 u.az.pCx->isIndex = 0;
66728 break;
66729 }
66730
66731 /* Opcode: Close P1 * * * *
66732 **
@@ -66794,39 +68112,39 @@
66794 */
66795 case OP_SeekLt: /* jump, in3 */
66796 case OP_SeekLe: /* jump, in3 */
66797 case OP_SeekGe: /* jump, in3 */
66798 case OP_SeekGt: { /* jump, in3 */
66799 #if 0 /* local variables moved into u.ba */
66800 int res;
66801 int oc;
66802 VdbeCursor *pC;
66803 UnpackedRecord r;
66804 int nField;
66805 i64 iKey; /* The rowid we are to seek to */
66806 #endif /* local variables moved into u.ba */
66807
66808 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66809 assert( pOp->p2!=0 );
66810 u.ba.pC = p->apCsr[pOp->p1];
66811 assert( u.ba.pC!=0 );
66812 assert( u.ba.pC->pseudoTableReg==0 );
66813 assert( OP_SeekLe == OP_SeekLt+1 );
66814 assert( OP_SeekGe == OP_SeekLt+2 );
66815 assert( OP_SeekGt == OP_SeekLt+3 );
66816 assert( u.ba.pC->isOrdered );
66817 if( ALWAYS(u.ba.pC->pCursor!=0) ){
66818 u.ba.oc = pOp->opcode;
66819 u.ba.pC->nullRow = 0;
66820 if( u.ba.pC->isTable ){
66821 /* The input value in P3 might be of any type: integer, real, string,
66822 ** blob, or NULL. But it needs to be an integer before we can do
66823 ** the seek, so covert it. */
66824 pIn3 = &aMem[pOp->p3];
66825 applyNumericAffinity(pIn3);
66826 u.ba.iKey = sqlite3VdbeIntValue(pIn3);
66827 u.ba.pC->rowidIsValid = 0;
66828
66829 /* If the P3 value could not be converted into an integer without
66830 ** loss of information, then special processing is required... */
66831 if( (pIn3->flags & MEM_Int)==0 ){
66832 if( (pIn3->flags & MEM_Real)==0 ){
@@ -66837,105 +68155,105 @@
66837 }
66838 /* If we reach this point, then the P3 value must be a floating
66839 ** point number. */
66840 assert( (pIn3->flags & MEM_Real)!=0 );
66841
66842 if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
66843 /* The P3 value is too large in magnitude to be expressed as an
66844 ** integer. */
66845 u.ba.res = 1;
66846 if( pIn3->r<0 ){
66847 if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66848 rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
66849 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66850 }
66851 }else{
66852 if( u.ba.oc<=OP_SeekLe ){ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66853 rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
66854 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66855 }
66856 }
66857 if( u.ba.res ){
66858 pc = pOp->p2 - 1;
66859 }
66860 break;
66861 }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
66862 /* Use the ceiling() function to convert real->int */
66863 if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
66864 }else{
66865 /* Use the floor() function to convert real->int */
66866 assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
66867 if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
66868 }
66869 }
66870 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
66871 if( rc!=SQLITE_OK ){
66872 goto abort_due_to_error;
66873 }
66874 if( u.ba.res==0 ){
66875 u.ba.pC->rowidIsValid = 1;
66876 u.ba.pC->lastRowid = u.ba.iKey;
66877 }
66878 }else{
66879 u.ba.nField = pOp->p4.i;
66880 assert( pOp->p4type==P4_INT32 );
66881 assert( u.ba.nField>0 );
66882 u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
66883 u.ba.r.nField = (u16)u.ba.nField;
66884
66885 /* The next line of code computes as follows, only faster:
66886 ** if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
66887 ** u.ba.r.flags = UNPACKED_INCRKEY;
66888 ** }else{
66889 ** u.ba.r.flags = 0;
66890 ** }
66891 */
66892 u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
66893 assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
66894 assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
66895 assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
66896 assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
66897
66898 u.ba.r.aMem = &aMem[pOp->p3];
66899 #ifdef SQLITE_DEBUG
66900 { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
66901 #endif
66902 ExpandBlob(u.ba.r.aMem);
66903 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
66904 if( rc!=SQLITE_OK ){
66905 goto abort_due_to_error;
66906 }
66907 u.ba.pC->rowidIsValid = 0;
66908 }
66909 u.ba.pC->deferredMoveto = 0;
66910 u.ba.pC->cacheStatus = CACHE_STALE;
66911 #ifdef SQLITE_TEST
66912 sqlite3_search_count++;
66913 #endif
66914 if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66915 if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
66916 rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
66917 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66918 u.ba.pC->rowidIsValid = 0;
66919 }else{
66920 u.ba.res = 0;
66921 }
66922 }else{
66923 assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66924 if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
66925 rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
66926 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66927 u.ba.pC->rowidIsValid = 0;
66928 }else{
66929 /* u.ba.res might be negative because the table is empty. Check to
66930 ** see if this is the case.
66931 */
66932 u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
66933 }
66934 }
66935 assert( pOp->p2>0 );
66936 if( u.ba.res ){
66937 pc = pOp->p2 - 1;
66938 }
66939 }else{
66940 /* This happens when attempting to open the sqlite3_master table
66941 ** for read access returns SQLITE_EMPTY. In this case always
@@ -66954,24 +68272,24 @@
66954 ** This is actually a deferred seek. Nothing actually happens until
66955 ** the cursor is used to read a record. That way, if no reads
66956 ** occur, no unnecessary I/O happens.
66957 */
66958 case OP_Seek: { /* in2 */
66959 #if 0 /* local variables moved into u.bb */
66960 VdbeCursor *pC;
66961 #endif /* local variables moved into u.bb */
66962
66963 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66964 u.bb.pC = p->apCsr[pOp->p1];
66965 assert( u.bb.pC!=0 );
66966 if( ALWAYS(u.bb.pC->pCursor!=0) ){
66967 assert( u.bb.pC->isTable );
66968 u.bb.pC->nullRow = 0;
66969 pIn2 = &aMem[pOp->p2];
66970 u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
66971 u.bb.pC->rowidIsValid = 0;
66972 u.bb.pC->deferredMoveto = 1;
66973 }
66974 break;
66975 }
66976
66977
@@ -66999,67 +68317,67 @@
66999 **
67000 ** See also: Found, NotExists, IsUnique
67001 */
67002 case OP_NotFound: /* jump, in3 */
67003 case OP_Found: { /* jump, in3 */
67004 #if 0 /* local variables moved into u.bc */
67005 int alreadyExists;
67006 VdbeCursor *pC;
67007 int res;
67008 char *pFree;
67009 UnpackedRecord *pIdxKey;
67010 UnpackedRecord r;
67011 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67012 #endif /* local variables moved into u.bc */
67013
67014 #ifdef SQLITE_TEST
67015 sqlite3_found_count++;
67016 #endif
67017
67018 u.bc.alreadyExists = 0;
67019 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67020 assert( pOp->p4type==P4_INT32 );
67021 u.bc.pC = p->apCsr[pOp->p1];
67022 assert( u.bc.pC!=0 );
67023 pIn3 = &aMem[pOp->p3];
67024 if( ALWAYS(u.bc.pC->pCursor!=0) ){
67025
67026 assert( u.bc.pC->isTable==0 );
67027 if( pOp->p4.i>0 ){
67028 u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67029 u.bc.r.nField = (u16)pOp->p4.i;
67030 u.bc.r.aMem = pIn3;
67031 #ifdef SQLITE_DEBUG
67032 { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67033 #endif
67034 u.bc.r.flags = UNPACKED_PREFIX_MATCH;
67035 u.bc.pIdxKey = &u.bc.r;
67036 }else{
67037 u.bc.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
67038 u.bc.pC->pKeyInfo, u.bc.aTempRec, sizeof(u.bc.aTempRec), &u.bc.pFree
67039 );
67040 if( u.bc.pIdxKey==0 ) goto no_mem;
67041 assert( pIn3->flags & MEM_Blob );
67042 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67043 sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z, u.bc.pIdxKey);
67044 u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67045 }
67046 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
67047 if( pOp->p4.i==0 ){
67048 sqlite3DbFree(db, u.bc.pFree);
67049 }
67050 if( rc!=SQLITE_OK ){
67051 break;
67052 }
67053 u.bc.alreadyExists = (u.bc.res==0);
67054 u.bc.pC->deferredMoveto = 0;
67055 u.bc.pC->cacheStatus = CACHE_STALE;
67056 }
67057 if( pOp->opcode==OP_Found ){
67058 if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
67059 }else{
67060 if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
67061 }
67062 break;
67063 }
67064
67065 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67087,67 +68405,67 @@
67087 ** instruction.
67088 **
67089 ** See also: NotFound, NotExists, Found
67090 */
67091 case OP_IsUnique: { /* jump, in3 */
67092 #if 0 /* local variables moved into u.bd */
67093 u16 ii;
67094 VdbeCursor *pCx;
67095 BtCursor *pCrsr;
67096 u16 nField;
67097 Mem *aMx;
67098 UnpackedRecord r; /* B-Tree index search key */
67099 i64 R; /* Rowid stored in register P3 */
67100 #endif /* local variables moved into u.bd */
67101
67102 pIn3 = &aMem[pOp->p3];
67103 u.bd.aMx = &aMem[pOp->p4.i];
67104 /* Assert that the values of parameters P1 and P4 are in range. */
67105 assert( pOp->p4type==P4_INT32 );
67106 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
67107 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67108
67109 /* Find the index cursor. */
67110 u.bd.pCx = p->apCsr[pOp->p1];
67111 assert( u.bd.pCx->deferredMoveto==0 );
67112 u.bd.pCx->seekResult = 0;
67113 u.bd.pCx->cacheStatus = CACHE_STALE;
67114 u.bd.pCrsr = u.bd.pCx->pCursor;
67115
67116 /* If any of the values are NULL, take the jump. */
67117 u.bd.nField = u.bd.pCx->pKeyInfo->nField;
67118 for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
67119 if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
67120 pc = pOp->p2 - 1;
67121 u.bd.pCrsr = 0;
67122 break;
67123 }
67124 }
67125 assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
67126
67127 if( u.bd.pCrsr!=0 ){
67128 /* Populate the index search key. */
67129 u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
67130 u.bd.r.nField = u.bd.nField + 1;
67131 u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
67132 u.bd.r.aMem = u.bd.aMx;
67133 #ifdef SQLITE_DEBUG
67134 { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
67135 #endif
67136
67137 /* Extract the value of u.bd.R from register P3. */
67138 sqlite3VdbeMemIntegerify(pIn3);
67139 u.bd.R = pIn3->u.i;
67140
67141 /* Search the B-Tree index. If no conflicting record is found, jump
67142 ** to P2. Otherwise, copy the rowid of the conflicting record to
67143 ** register P3 and fall through to the next instruction. */
67144 rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
67145 if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
67146 pc = pOp->p2 - 1;
67147 }else{
67148 pIn3->u.i = u.bd.r.rowid;
67149 }
67150 }
67151 break;
67152 }
67153
@@ -67164,46 +68482,46 @@
67164 ** P1 is an index.
67165 **
67166 ** See also: Found, NotFound, IsUnique
67167 */
67168 case OP_NotExists: { /* jump, in3 */
67169 #if 0 /* local variables moved into u.be */
67170 VdbeCursor *pC;
67171 BtCursor *pCrsr;
67172 int res;
67173 u64 iKey;
67174 #endif /* local variables moved into u.be */
67175
67176 pIn3 = &aMem[pOp->p3];
67177 assert( pIn3->flags & MEM_Int );
67178 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67179 u.be.pC = p->apCsr[pOp->p1];
67180 assert( u.be.pC!=0 );
67181 assert( u.be.pC->isTable );
67182 assert( u.be.pC->pseudoTableReg==0 );
67183 u.be.pCrsr = u.be.pC->pCursor;
67184 if( ALWAYS(u.be.pCrsr!=0) ){
67185 u.be.res = 0;
67186 u.be.iKey = pIn3->u.i;
67187 rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
67188 u.be.pC->lastRowid = pIn3->u.i;
67189 u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
67190 u.be.pC->nullRow = 0;
67191 u.be.pC->cacheStatus = CACHE_STALE;
67192 u.be.pC->deferredMoveto = 0;
67193 if( u.be.res!=0 ){
67194 pc = pOp->p2 - 1;
67195 assert( u.be.pC->rowidIsValid==0 );
67196 }
67197 u.be.pC->seekResult = u.be.res;
67198 }else{
67199 /* This happens when an attempt to open a read cursor on the
67200 ** sqlite_master table returns SQLITE_EMPTY.
67201 */
67202 pc = pOp->p2 - 1;
67203 assert( u.be.pC->rowidIsValid==0 );
67204 u.be.pC->seekResult = 0;
67205 }
67206 break;
67207 }
67208
67209 /* Opcode: Sequence P1 P2 * * *
@@ -67234,25 +68552,25 @@
67234 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
67235 ** generated record number. This P3 mechanism is used to help implement the
67236 ** AUTOINCREMENT feature.
67237 */
67238 case OP_NewRowid: { /* out2-prerelease */
67239 #if 0 /* local variables moved into u.bf */
67240 i64 v; /* The new rowid */
67241 VdbeCursor *pC; /* Cursor of table to get the new rowid */
67242 int res; /* Result of an sqlite3BtreeLast() */
67243 int cnt; /* Counter to limit the number of searches */
67244 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
67245 VdbeFrame *pFrame; /* Root frame of VDBE */
67246 #endif /* local variables moved into u.bf */
67247
67248 u.bf.v = 0;
67249 u.bf.res = 0;
67250 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67251 u.bf.pC = p->apCsr[pOp->p1];
67252 assert( u.bf.pC!=0 );
67253 if( NEVER(u.bf.pC->pCursor==0) ){
67254 /* The zero initialization above is all that is needed */
67255 }else{
67256 /* The next rowid or record number (different terms for the same
67257 ** thing) is obtained in a two-step algorithm.
67258 **
@@ -67264,11 +68582,11 @@
67264 ** The second algorithm is to select a rowid at random and see if
67265 ** it already exists in the table. If it does not exist, we have
67266 ** succeeded. If the random rowid does exist, we select a new one
67267 ** and try again, up to 100 times.
67268 */
67269 assert( u.bf.pC->isTable );
67270
67271 #ifdef SQLITE_32BIT_ROWID
67272 # define MAX_ROWID 0x7fffffff
67273 #else
67274 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67276,101 +68594,101 @@
67276 ** to provide the constant while making all compilers happy.
67277 */
67278 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
67279 #endif
67280
67281 if( !u.bf.pC->useRandomRowid ){
67282 u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
67283 if( u.bf.v==0 ){
67284 rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
67285 if( rc!=SQLITE_OK ){
67286 goto abort_due_to_error;
67287 }
67288 if( u.bf.res ){
67289 u.bf.v = 1; /* IMP: R-61914-48074 */
67290 }else{
67291 assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
67292 rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
67293 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67294 if( u.bf.v==MAX_ROWID ){
67295 u.bf.pC->useRandomRowid = 1;
67296 }else{
67297 u.bf.v++; /* IMP: R-29538-34987 */
67298 }
67299 }
67300 }
67301
67302 #ifndef SQLITE_OMIT_AUTOINCREMENT
67303 if( pOp->p3 ){
67304 /* Assert that P3 is a valid memory cell. */
67305 assert( pOp->p3>0 );
67306 if( p->pFrame ){
67307 for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
67308 /* Assert that P3 is a valid memory cell. */
67309 assert( pOp->p3<=u.bf.pFrame->nMem );
67310 u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
67311 }else{
67312 /* Assert that P3 is a valid memory cell. */
67313 assert( pOp->p3<=p->nMem );
67314 u.bf.pMem = &aMem[pOp->p3];
67315 memAboutToChange(p, u.bf.pMem);
67316 }
67317 assert( memIsValid(u.bf.pMem) );
67318
67319 REGISTER_TRACE(pOp->p3, u.bf.pMem);
67320 sqlite3VdbeMemIntegerify(u.bf.pMem);
67321 assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67322 if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
67323 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
67324 goto abort_due_to_error;
67325 }
67326 if( u.bf.v<u.bf.pMem->u.i+1 ){
67327 u.bf.v = u.bf.pMem->u.i + 1;
67328 }
67329 u.bf.pMem->u.i = u.bf.v;
67330 }
67331 #endif
67332
67333 sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
67334 }
67335 if( u.bf.pC->useRandomRowid ){
67336 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
67337 ** largest possible integer (9223372036854775807) then the database
67338 ** engine starts picking positive candidate ROWIDs at random until
67339 ** it finds one that is not previously used. */
67340 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
67341 ** an AUTOINCREMENT table. */
67342 /* on the first attempt, simply do one more than previous */
67343 u.bf.v = lastRowid;
67344 u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67345 u.bf.v++; /* ensure non-zero */
67346 u.bf.cnt = 0;
67347 while( ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
67348 0, &u.bf.res))==SQLITE_OK)
67349 && (u.bf.res==0)
67350 && (++u.bf.cnt<100)){
67351 /* collision - try another random rowid */
67352 sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
67353 if( u.bf.cnt<5 ){
67354 /* try "small" random rowids for the initial attempts */
67355 u.bf.v &= 0xffffff;
67356 }else{
67357 u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67358 }
67359 u.bf.v++; /* ensure non-zero */
67360 }
67361 if( rc==SQLITE_OK && u.bf.res==0 ){
67362 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
67363 goto abort_due_to_error;
67364 }
67365 assert( u.bf.v>0 ); /* EV: R-40812-03570 */
67366 }
67367 u.bf.pC->rowidIsValid = 0;
67368 u.bf.pC->deferredMoveto = 0;
67369 u.bf.pC->cacheStatus = CACHE_STALE;
67370 }
67371 pOut->u.i = u.bf.v;
67372 break;
67373 }
67374
67375 /* Opcode: Insert P1 P2 P3 P4 P5
67376 **
@@ -67416,74 +68734,74 @@
67416 ** This works exactly like OP_Insert except that the key is the
67417 ** integer value P3, not the value of the integer stored in register P3.
67418 */
67419 case OP_Insert:
67420 case OP_InsertInt: {
67421 #if 0 /* local variables moved into u.bg */
67422 Mem *pData; /* MEM cell holding data for the record to be inserted */
67423 Mem *pKey; /* MEM cell holding key for the record */
67424 i64 iKey; /* The integer ROWID or key for the record to be inserted */
67425 VdbeCursor *pC; /* Cursor to table into which insert is written */
67426 int nZero; /* Number of zero-bytes to append */
67427 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
67428 const char *zDb; /* database name - used by the update hook */
67429 const char *zTbl; /* Table name - used by the opdate hook */
67430 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67431 #endif /* local variables moved into u.bg */
67432
67433 u.bg.pData = &aMem[pOp->p2];
67434 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67435 assert( memIsValid(u.bg.pData) );
67436 u.bg.pC = p->apCsr[pOp->p1];
67437 assert( u.bg.pC!=0 );
67438 assert( u.bg.pC->pCursor!=0 );
67439 assert( u.bg.pC->pseudoTableReg==0 );
67440 assert( u.bg.pC->isTable );
67441 REGISTER_TRACE(pOp->p2, u.bg.pData);
67442
67443 if( pOp->opcode==OP_Insert ){
67444 u.bg.pKey = &aMem[pOp->p3];
67445 assert( u.bg.pKey->flags & MEM_Int );
67446 assert( memIsValid(u.bg.pKey) );
67447 REGISTER_TRACE(pOp->p3, u.bg.pKey);
67448 u.bg.iKey = u.bg.pKey->u.i;
67449 }else{
67450 assert( pOp->opcode==OP_InsertInt );
67451 u.bg.iKey = pOp->p3;
67452 }
67453
67454 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67455 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
67456 if( u.bg.pData->flags & MEM_Null ){
67457 u.bg.pData->z = 0;
67458 u.bg.pData->n = 0;
67459 }else{
67460 assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
67461 }
67462 u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
67463 if( u.bg.pData->flags & MEM_Zero ){
67464 u.bg.nZero = u.bg.pData->u.nZero;
67465 }else{
67466 u.bg.nZero = 0;
67467 }
67468 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67469 rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
67470 u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
67471 pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
67472 );
67473 u.bg.pC->rowidIsValid = 0;
67474 u.bg.pC->deferredMoveto = 0;
67475 u.bg.pC->cacheStatus = CACHE_STALE;
67476
67477 /* Invoke the update-hook if required. */
67478 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67479 u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
67480 u.bg.zTbl = pOp->p4.z;
67481 u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67482 assert( u.bg.pC->isTable );
67483 db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
67484 assert( u.bg.pC->iDb>=0 );
67485 }
67486 break;
67487 }
67488
67489 /* Opcode: Delete P1 P2 * P4 *
@@ -67505,51 +68823,51 @@
67505 ** pointing to. The update hook will be invoked, if it exists.
67506 ** If P4 is not NULL then the P1 cursor must have been positioned
67507 ** using OP_NotFound prior to invoking this opcode.
67508 */
67509 case OP_Delete: {
67510 #if 0 /* local variables moved into u.bh */
67511 i64 iKey;
67512 VdbeCursor *pC;
67513 #endif /* local variables moved into u.bh */
67514
67515 u.bh.iKey = 0;
67516 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67517 u.bh.pC = p->apCsr[pOp->p1];
67518 assert( u.bh.pC!=0 );
67519 assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67520
67521 /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
67522 ** row being deleted.
67523 */
67524 if( db->xUpdateCallback && pOp->p4.z ){
67525 assert( u.bh.pC->isTable );
67526 assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67527 u.bh.iKey = u.bh.pC->lastRowid;
67528 }
67529
67530 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67531 ** OP_Column on the same table without any intervening operations that
67532 ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
67533 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67534 ** below is always a no-op and cannot fail. We will run it anyhow, though,
67535 ** to guard against future changes to the code generator.
67536 **/
67537 assert( u.bh.pC->deferredMoveto==0 );
67538 rc = sqlite3VdbeCursorMoveto(u.bh.pC);
67539 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67540
67541 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67542 rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
67543 u.bh.pC->cacheStatus = CACHE_STALE;
67544
67545 /* Invoke the update-hook if required. */
67546 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67547 const char *zDb = db->aDb[u.bh.pC->iDb].zName;
67548 const char *zTbl = pOp->p4.z;
67549 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
67550 assert( u.bh.pC->iDb>=0 );
67551 }
67552 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67553 break;
67554 }
67555 /* Opcode: ResetCount * * * * *
@@ -67571,20 +68889,20 @@
67571 ** register P3 with the entry that the sorter cursor currently points to.
67572 ** If, excluding the rowid fields at the end, the two records are a match,
67573 ** fall through to the next instruction. Otherwise, jump to instruction P2.
67574 */
67575 case OP_SorterCompare: {
67576 #if 0 /* local variables moved into u.bi */
67577 VdbeCursor *pC;
67578 int res;
67579 #endif /* local variables moved into u.bi */
67580
67581 u.bi.pC = p->apCsr[pOp->p1];
67582 assert( isSorter(u.bi.pC) );
67583 pIn3 = &aMem[pOp->p3];
67584 rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
67585 if( u.bi.res ){
67586 pc = pOp->p2-1;
67587 }
67588 break;
67589 };
67590
@@ -67591,18 +68909,18 @@
67591 /* Opcode: SorterData P1 P2 * * *
67592 **
67593 ** Write into register P2 the current sorter data for sorter cursor P1.
67594 */
67595 case OP_SorterData: {
67596 #if 0 /* local variables moved into u.bj */
67597 VdbeCursor *pC;
67598 #endif /* local variables moved into u.bj */
67599 #ifndef SQLITE_OMIT_MERGE_SORT
67600 pOut = &aMem[pOp->p2];
67601 u.bj.pC = p->apCsr[pOp->p1];
67602 assert( u.bj.pC->isSorter );
67603 rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
67604 #else
67605 pOp->opcode = OP_RowKey;
67606 pc--;
67607 #endif
67608 break;
@@ -67628,67 +68946,67 @@
67628 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
67629 ** of a real table, not a pseudo-table.
67630 */
67631 case OP_RowKey:
67632 case OP_RowData: {
67633 #if 0 /* local variables moved into u.bk */
67634 VdbeCursor *pC;
67635 BtCursor *pCrsr;
67636 u32 n;
67637 i64 n64;
67638 #endif /* local variables moved into u.bk */
67639
67640 pOut = &aMem[pOp->p2];
67641 memAboutToChange(p, pOut);
67642
67643 /* Note that RowKey and RowData are really exactly the same instruction */
67644 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67645 u.bk.pC = p->apCsr[pOp->p1];
67646 assert( u.bk.pC->isSorter==0 );
67647 assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
67648 assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
67649 assert( u.bk.pC!=0 );
67650 assert( u.bk.pC->nullRow==0 );
67651 assert( u.bk.pC->pseudoTableReg==0 );
67652 assert( !u.bk.pC->isSorter );
67653 assert( u.bk.pC->pCursor!=0 );
67654 u.bk.pCrsr = u.bk.pC->pCursor;
67655 assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
67656
67657 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67658 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67659 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
67660 ** a no-op and can never fail. But we leave it in place as a safety.
67661 */
67662 assert( u.bk.pC->deferredMoveto==0 );
67663 rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67664 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67665
67666 if( u.bk.pC->isIndex ){
67667 assert( !u.bk.pC->isTable );
67668 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67669 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67670 if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67671 goto too_big;
67672 }
67673 u.bk.n = (u32)u.bk.n64;
67674 }else{
67675 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67676 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67677 if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67678 goto too_big;
67679 }
67680 }
67681 if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
67682 goto no_mem;
67683 }
67684 pOut->n = u.bk.n;
67685 MemSetTypeFlag(pOut, MEM_Blob);
67686 if( u.bk.pC->isIndex ){
67687 rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67688 }else{
67689 rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67690 }
67691 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
67692 UPDATE_MAX_BLOBSIZE(pOut);
67693 break;
67694 }
@@ -67701,46 +69019,46 @@
67701 ** P1 can be either an ordinary table or a virtual table. There used to
67702 ** be a separate OP_VRowid opcode for use with virtual tables, but this
67703 ** one opcode now works for both table types.
67704 */
67705 case OP_Rowid: { /* out2-prerelease */
67706 #if 0 /* local variables moved into u.bl */
67707 VdbeCursor *pC;
67708 i64 v;
67709 sqlite3_vtab *pVtab;
67710 const sqlite3_module *pModule;
67711 #endif /* local variables moved into u.bl */
67712
67713 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67714 u.bl.pC = p->apCsr[pOp->p1];
67715 assert( u.bl.pC!=0 );
67716 assert( u.bl.pC->pseudoTableReg==0 );
67717 if( u.bl.pC->nullRow ){
67718 pOut->flags = MEM_Null;
67719 break;
67720 }else if( u.bl.pC->deferredMoveto ){
67721 u.bl.v = u.bl.pC->movetoTarget;
67722 #ifndef SQLITE_OMIT_VIRTUALTABLE
67723 }else if( u.bl.pC->pVtabCursor ){
67724 u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
67725 u.bl.pModule = u.bl.pVtab->pModule;
67726 assert( u.bl.pModule->xRowid );
67727 rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
67728 importVtabErrMsg(p, u.bl.pVtab);
67729 #endif /* SQLITE_OMIT_VIRTUALTABLE */
67730 }else{
67731 assert( u.bl.pC->pCursor!=0 );
67732 rc = sqlite3VdbeCursorMoveto(u.bl.pC);
67733 if( rc ) goto abort_due_to_error;
67734 if( u.bl.pC->rowidIsValid ){
67735 u.bl.v = u.bl.pC->lastRowid;
67736 }else{
67737 rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
67738 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
67739 }
67740 }
67741 pOut->u.i = u.bl.v;
67742 break;
67743 }
67744
67745 /* Opcode: NullRow P1 * * * *
67746 **
@@ -67747,22 +69065,22 @@
67747 ** Move the cursor P1 to a null row. Any OP_Column operations
67748 ** that occur while the cursor is on the null row will always
67749 ** write a NULL.
67750 */
67751 case OP_NullRow: {
67752 #if 0 /* local variables moved into u.bm */
67753 VdbeCursor *pC;
67754 #endif /* local variables moved into u.bm */
67755
67756 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67757 u.bm.pC = p->apCsr[pOp->p1];
67758 assert( u.bm.pC!=0 );
67759 u.bm.pC->nullRow = 1;
67760 u.bm.pC->rowidIsValid = 0;
67761 assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
67762 if( u.bm.pC->pCursor ){
67763 sqlite3BtreeClearCursor(u.bm.pC->pCursor);
67764 }
67765 break;
67766 }
67767
67768 /* Opcode: Last P1 P2 * * *
@@ -67772,29 +69090,29 @@
67772 ** If the table or index is empty and P2>0, then jump immediately to P2.
67773 ** If P2 is 0 or if the table or index is not empty, fall through
67774 ** to the following instruction.
67775 */
67776 case OP_Last: { /* jump */
67777 #if 0 /* local variables moved into u.bn */
67778 VdbeCursor *pC;
67779 BtCursor *pCrsr;
67780 int res;
67781 #endif /* local variables moved into u.bn */
67782
67783 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67784 u.bn.pC = p->apCsr[pOp->p1];
67785 assert( u.bn.pC!=0 );
67786 u.bn.pCrsr = u.bn.pC->pCursor;
67787 u.bn.res = 0;
67788 if( ALWAYS(u.bn.pCrsr!=0) ){
67789 rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
67790 }
67791 u.bn.pC->nullRow = (u8)u.bn.res;
67792 u.bn.pC->deferredMoveto = 0;
67793 u.bn.pC->rowidIsValid = 0;
67794 u.bn.pC->cacheStatus = CACHE_STALE;
67795 if( pOp->p2>0 && u.bn.res ){
67796 pc = pOp->p2 - 1;
67797 }
67798 break;
67799 }
67800
@@ -67830,35 +69148,35 @@
67830 ** If the table or index is empty and P2>0, then jump immediately to P2.
67831 ** If P2 is 0 or if the table or index is not empty, fall through
67832 ** to the following instruction.
67833 */
67834 case OP_Rewind: { /* jump */
67835 #if 0 /* local variables moved into u.bo */
67836 VdbeCursor *pC;
67837 BtCursor *pCrsr;
67838 int res;
67839 #endif /* local variables moved into u.bo */
67840
67841 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67842 u.bo.pC = p->apCsr[pOp->p1];
67843 assert( u.bo.pC!=0 );
67844 assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
67845 u.bo.res = 1;
67846 if( isSorter(u.bo.pC) ){
67847 rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
67848 }else{
67849 u.bo.pCrsr = u.bo.pC->pCursor;
67850 assert( u.bo.pCrsr );
67851 rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
67852 u.bo.pC->atFirst = u.bo.res==0 ?1:0;
67853 u.bo.pC->deferredMoveto = 0;
67854 u.bo.pC->cacheStatus = CACHE_STALE;
67855 u.bo.pC->rowidIsValid = 0;
67856 }
67857 u.bo.pC->nullRow = (u8)u.bo.res;
67858 assert( pOp->p2>0 && pOp->p2<p->nOp );
67859 if( u.bo.res ){
67860 pc = pOp->p2 - 1;
67861 }
67862 break;
67863 }
67864
@@ -67898,44 +69216,44 @@
67898 #ifdef SQLITE_OMIT_MERGE_SORT
67899 pOp->opcode = OP_Next;
67900 #endif
67901 case OP_Prev: /* jump */
67902 case OP_Next: { /* jump */
67903 #if 0 /* local variables moved into u.bp */
67904 VdbeCursor *pC;
67905 int res;
67906 #endif /* local variables moved into u.bp */
67907
67908 CHECK_FOR_INTERRUPT;
67909 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67910 assert( pOp->p5<=ArraySize(p->aCounter) );
67911 u.bp.pC = p->apCsr[pOp->p1];
67912 if( u.bp.pC==0 ){
67913 break; /* See ticket #2273 */
67914 }
67915 assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
67916 if( isSorter(u.bp.pC) ){
67917 assert( pOp->opcode==OP_SorterNext );
67918 rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
67919 }else{
67920 u.bp.res = 1;
67921 assert( u.bp.pC->deferredMoveto==0 );
67922 assert( u.bp.pC->pCursor );
67923 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
67924 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
67925 rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
67926 }
67927 u.bp.pC->nullRow = (u8)u.bp.res;
67928 u.bp.pC->cacheStatus = CACHE_STALE;
67929 if( u.bp.res==0 ){
67930 pc = pOp->p2 - 1;
67931 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
67932 #ifdef SQLITE_TEST
67933 sqlite3_search_count++;
67934 #endif
67935 }
67936 u.bp.pC->rowidIsValid = 0;
67937 break;
67938 }
67939
67940 /* Opcode: IdxInsert P1 P2 P3 * P5
67941 **
@@ -67952,38 +69270,38 @@
67952 case OP_SorterInsert: /* in2 */
67953 #ifdef SQLITE_OMIT_MERGE_SORT
67954 pOp->opcode = OP_IdxInsert;
67955 #endif
67956 case OP_IdxInsert: { /* in2 */
67957 #if 0 /* local variables moved into u.bq */
67958 VdbeCursor *pC;
67959 BtCursor *pCrsr;
67960 int nKey;
67961 const char *zKey;
67962 #endif /* local variables moved into u.bq */
67963
67964 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67965 u.bq.pC = p->apCsr[pOp->p1];
67966 assert( u.bq.pC!=0 );
67967 assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
67968 pIn2 = &aMem[pOp->p2];
67969 assert( pIn2->flags & MEM_Blob );
67970 u.bq.pCrsr = u.bq.pC->pCursor;
67971 if( ALWAYS(u.bq.pCrsr!=0) ){
67972 assert( u.bq.pC->isTable==0 );
67973 rc = ExpandBlob(pIn2);
67974 if( rc==SQLITE_OK ){
67975 if( isSorter(u.bq.pC) ){
67976 rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
67977 }else{
67978 u.bq.nKey = pIn2->n;
67979 u.bq.zKey = pIn2->z;
67980 rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
67981 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
67982 );
67983 assert( u.bq.pC->deferredMoveto==0 );
67984 u.bq.pC->cacheStatus = CACHE_STALE;
67985 }
67986 }
67987 }
67988 break;
67989 }
@@ -67993,37 +69311,37 @@
67993 ** The content of P3 registers starting at register P2 form
67994 ** an unpacked index key. This opcode removes that entry from the
67995 ** index opened by cursor P1.
67996 */
67997 case OP_IdxDelete: {
67998 #if 0 /* local variables moved into u.br */
67999 VdbeCursor *pC;
68000 BtCursor *pCrsr;
68001 int res;
68002 UnpackedRecord r;
68003 #endif /* local variables moved into u.br */
68004
68005 assert( pOp->p3>0 );
68006 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
68007 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68008 u.br.pC = p->apCsr[pOp->p1];
68009 assert( u.br.pC!=0 );
68010 u.br.pCrsr = u.br.pC->pCursor;
68011 if( ALWAYS(u.br.pCrsr!=0) ){
68012 u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
68013 u.br.r.nField = (u16)pOp->p3;
68014 u.br.r.flags = 0;
68015 u.br.r.aMem = &aMem[pOp->p2];
68016 #ifdef SQLITE_DEBUG
68017 { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
68018 #endif
68019 rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
68020 if( rc==SQLITE_OK && u.br.res==0 ){
68021 rc = sqlite3BtreeDelete(u.br.pCrsr);
68022 }
68023 assert( u.br.pC->deferredMoveto==0 );
68024 u.br.pC->cacheStatus = CACHE_STALE;
68025 }
68026 break;
68027 }
68028
68029 /* Opcode: IdxRowid P1 P2 * * *
@@ -68033,32 +69351,32 @@
68033 ** the rowid of the table entry to which this index entry points.
68034 **
68035 ** See also: Rowid, MakeRecord.
68036 */
68037 case OP_IdxRowid: { /* out2-prerelease */
68038 #if 0 /* local variables moved into u.bs */
68039 BtCursor *pCrsr;
68040 VdbeCursor *pC;
68041 i64 rowid;
68042 #endif /* local variables moved into u.bs */
68043
68044 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68045 u.bs.pC = p->apCsr[pOp->p1];
68046 assert( u.bs.pC!=0 );
68047 u.bs.pCrsr = u.bs.pC->pCursor;
68048 pOut->flags = MEM_Null;
68049 if( ALWAYS(u.bs.pCrsr!=0) ){
68050 rc = sqlite3VdbeCursorMoveto(u.bs.pC);
68051 if( NEVER(rc) ) goto abort_due_to_error;
68052 assert( u.bs.pC->deferredMoveto==0 );
68053 assert( u.bs.pC->isTable==0 );
68054 if( !u.bs.pC->nullRow ){
68055 rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
68056 if( rc!=SQLITE_OK ){
68057 goto abort_due_to_error;
68058 }
68059 pOut->u.i = u.bs.rowid;
68060 pOut->flags = MEM_Int;
68061 }
68062 }
68063 break;
68064 }
@@ -68089,43 +69407,43 @@
68089 ** If P5 is non-zero then the key value is increased by an epsilon prior
68090 ** to the comparison. This makes the opcode work like IdxLE.
68091 */
68092 case OP_IdxLT: /* jump */
68093 case OP_IdxGE: { /* jump */
68094 #if 0 /* local variables moved into u.bt */
68095 VdbeCursor *pC;
68096 int res;
68097 UnpackedRecord r;
68098 #endif /* local variables moved into u.bt */
68099
68100 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68101 u.bt.pC = p->apCsr[pOp->p1];
68102 assert( u.bt.pC!=0 );
68103 assert( u.bt.pC->isOrdered );
68104 if( ALWAYS(u.bt.pC->pCursor!=0) ){
68105 assert( u.bt.pC->deferredMoveto==0 );
68106 assert( pOp->p5==0 || pOp->p5==1 );
68107 assert( pOp->p4type==P4_INT32 );
68108 u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68109 u.bt.r.nField = (u16)pOp->p4.i;
68110 if( pOp->p5 ){
68111 u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
68112 }else{
68113 u.bt.r.flags = UNPACKED_IGNORE_ROWID;
68114 }
68115 u.bt.r.aMem = &aMem[pOp->p3];
68116 #ifdef SQLITE_DEBUG
68117 { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
68118 #endif
68119 rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
68120 if( pOp->opcode==OP_IdxLT ){
68121 u.bt.res = -u.bt.res;
68122 }else{
68123 assert( pOp->opcode==OP_IdxGE );
68124 u.bt.res++;
68125 }
68126 if( u.bt.res>0 ){
68127 pc = pOp->p2 - 1 ;
68128 }
68129 }
68130 break;
68131 }
@@ -68149,43 +69467,43 @@
68149 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
68150 **
68151 ** See also: Clear
68152 */
68153 case OP_Destroy: { /* out2-prerelease */
68154 #if 0 /* local variables moved into u.bu */
68155 int iMoved;
68156 int iCnt;
68157 Vdbe *pVdbe;
68158 int iDb;
68159 #endif /* local variables moved into u.bu */
68160 #ifndef SQLITE_OMIT_VIRTUALTABLE
68161 u.bu.iCnt = 0;
68162 for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
68163 if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
68164 u.bu.iCnt++;
68165 }
68166 }
68167 #else
68168 u.bu.iCnt = db->activeVdbeCnt;
68169 #endif
68170 pOut->flags = MEM_Null;
68171 if( u.bu.iCnt>1 ){
68172 rc = SQLITE_LOCKED;
68173 p->errorAction = OE_Abort;
68174 }else{
68175 u.bu.iDb = pOp->p3;
68176 assert( u.bu.iCnt==1 );
68177 assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
68178 rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
68179 pOut->flags = MEM_Int;
68180 pOut->u.i = u.bu.iMoved;
68181 #ifndef SQLITE_OMIT_AUTOVACUUM
68182 if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
68183 sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
68184 /* All OP_Destroy operations occur on the same btree */
68185 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
68186 resetSchemaOnFault = u.bu.iDb+1;
68187 }
68188 #endif
68189 }
68190 break;
68191 }
@@ -68207,25 +69525,25 @@
68207 ** also incremented by the number of rows in the table being cleared.
68208 **
68209 ** See also: Destroy
68210 */
68211 case OP_Clear: {
68212 #if 0 /* local variables moved into u.bv */
68213 int nChange;
68214 #endif /* local variables moved into u.bv */
68215
68216 u.bv.nChange = 0;
68217 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
68218 rc = sqlite3BtreeClearTable(
68219 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
68220 );
68221 if( pOp->p3 ){
68222 p->nChange += u.bv.nChange;
68223 if( pOp->p3>0 ){
68224 assert( memIsValid(&aMem[pOp->p3]) );
68225 memAboutToChange(p, &aMem[pOp->p3]);
68226 aMem[pOp->p3].u.i += u.bv.nChange;
68227 }
68228 }
68229 break;
68230 }
68231
@@ -68251,29 +69569,29 @@
68251 **
68252 ** See documentation on OP_CreateTable for additional information.
68253 */
68254 case OP_CreateIndex: /* out2-prerelease */
68255 case OP_CreateTable: { /* out2-prerelease */
68256 #if 0 /* local variables moved into u.bw */
68257 int pgno;
68258 int flags;
68259 Db *pDb;
68260 #endif /* local variables moved into u.bw */
68261
68262 u.bw.pgno = 0;
68263 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68264 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68265 u.bw.pDb = &db->aDb[pOp->p1];
68266 assert( u.bw.pDb->pBt!=0 );
68267 if( pOp->opcode==OP_CreateTable ){
68268 /* u.bw.flags = BTREE_INTKEY; */
68269 u.bw.flags = BTREE_INTKEY;
68270 }else{
68271 u.bw.flags = BTREE_BLOBKEY;
68272 }
68273 rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
68274 pOut->u.i = u.bw.pgno;
68275 break;
68276 }
68277
68278 /* Opcode: ParseSchema P1 * * P4 *
68279 **
@@ -68282,48 +69600,48 @@
68282 **
68283 ** This opcode invokes the parser to create a new virtual machine,
68284 ** then runs the new virtual machine. It is thus a re-entrant opcode.
68285 */
68286 case OP_ParseSchema: {
68287 #if 0 /* local variables moved into u.bx */
68288 int iDb;
68289 const char *zMaster;
68290 char *zSql;
68291 InitData initData;
68292 #endif /* local variables moved into u.bx */
68293
68294 /* Any prepared statement that invokes this opcode will hold mutexes
68295 ** on every btree. This is a prerequisite for invoking
68296 ** sqlite3InitCallback().
68297 */
68298 #ifdef SQLITE_DEBUG
68299 for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
68300 assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
68301 }
68302 #endif
68303
68304 u.bx.iDb = pOp->p1;
68305 assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
68306 assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
68307 /* Used to be a conditional */ {
68308 u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
68309 u.bx.initData.db = db;
68310 u.bx.initData.iDb = pOp->p1;
68311 u.bx.initData.pzErrMsg = &p->zErrMsg;
68312 u.bx.zSql = sqlite3MPrintf(db,
68313 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68314 db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
68315 if( u.bx.zSql==0 ){
68316 rc = SQLITE_NOMEM;
68317 }else{
68318 assert( db->init.busy==0 );
68319 db->init.busy = 1;
68320 u.bx.initData.rc = SQLITE_OK;
68321 assert( !db->mallocFailed );
68322 rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
68323 if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
68324 sqlite3DbFree(db, u.bx.zSql);
68325 db->init.busy = 0;
68326 }
68327 }
68328 if( rc==SQLITE_NOMEM ){
68329 goto no_mem;
@@ -68402,45 +69720,45 @@
68402 ** file, not the main database file.
68403 **
68404 ** This opcode is used to implement the integrity_check pragma.
68405 */
68406 case OP_IntegrityCk: {
68407 #if 0 /* local variables moved into u.by */
68408 int nRoot; /* Number of tables to check. (Number of root pages.) */
68409 int *aRoot; /* Array of rootpage numbers for tables to be checked */
68410 int j; /* Loop counter */
68411 int nErr; /* Number of errors reported */
68412 char *z; /* Text of the error report */
68413 Mem *pnErr; /* Register keeping track of errors remaining */
68414 #endif /* local variables moved into u.by */
68415
68416 u.by.nRoot = pOp->p2;
68417 assert( u.by.nRoot>0 );
68418 u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
68419 if( u.by.aRoot==0 ) goto no_mem;
68420 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68421 u.by.pnErr = &aMem[pOp->p3];
68422 assert( (u.by.pnErr->flags & MEM_Int)!=0 );
68423 assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68424 pIn1 = &aMem[pOp->p1];
68425 for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
68426 u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
68427 }
68428 u.by.aRoot[u.by.j] = 0;
68429 assert( pOp->p5<db->nDb );
68430 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68431 u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
68432 (int)u.by.pnErr->u.i, &u.by.nErr);
68433 sqlite3DbFree(db, u.by.aRoot);
68434 u.by.pnErr->u.i -= u.by.nErr;
68435 sqlite3VdbeMemSetNull(pIn1);
68436 if( u.by.nErr==0 ){
68437 assert( u.by.z==0 );
68438 }else if( u.by.z==0 ){
68439 goto no_mem;
68440 }else{
68441 sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
68442 }
68443 UPDATE_MAX_BLOBSIZE(pIn1);
68444 sqlite3VdbeChangeEncoding(pIn1, encoding);
68445 break;
68446 }
@@ -68470,24 +69788,24 @@
68470 ** Extract the smallest value from boolean index P1 and put that value into
68471 ** register P3. Or, if boolean index P1 is initially empty, leave P3
68472 ** unchanged and jump to instruction P2.
68473 */
68474 case OP_RowSetRead: { /* jump, in1, out3 */
68475 #if 0 /* local variables moved into u.bz */
68476 i64 val;
68477 #endif /* local variables moved into u.bz */
68478 CHECK_FOR_INTERRUPT;
68479 pIn1 = &aMem[pOp->p1];
68480 if( (pIn1->flags & MEM_RowSet)==0
68481 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
68482 ){
68483 /* The boolean index is empty */
68484 sqlite3VdbeMemSetNull(pIn1);
68485 pc = pOp->p2 - 1;
68486 }else{
68487 /* A value was pulled from the index */
68488 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
68489 }
68490 break;
68491 }
68492
68493 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -68512,18 +69830,18 @@
68512 ** inserted, there is no need to search to see if the same value was
68513 ** previously inserted as part of set X (only if it was previously
68514 ** inserted as part of some other set).
68515 */
68516 case OP_RowSetTest: { /* jump, in1, in3 */
68517 #if 0 /* local variables moved into u.ca */
68518 int iSet;
68519 int exists;
68520 #endif /* local variables moved into u.ca */
68521
68522 pIn1 = &aMem[pOp->p1];
68523 pIn3 = &aMem[pOp->p3];
68524 u.ca.iSet = pOp->p4.i;
68525 assert( pIn3->flags&MEM_Int );
68526
68527 /* If there is anything other than a rowset object in memory cell P1,
68528 ** delete it now and initialize P1 with an empty rowset
68529 */
@@ -68531,21 +69849,21 @@
68531 sqlite3VdbeMemSetRowSet(pIn1);
68532 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68533 }
68534
68535 assert( pOp->p4type==P4_INT32 );
68536 assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
68537 if( u.ca.iSet ){
68538 u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68539 (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
68540 pIn3->u.i);
68541 if( u.ca.exists ){
68542 pc = pOp->p2 - 1;
68543 break;
68544 }
68545 }
68546 if( u.ca.iSet>=0 ){
68547 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
68548 }
68549 break;
68550 }
68551
@@ -68564,25 +69882,24 @@
68564 ** memory required by the sub-vdbe at runtime.
68565 **
68566 ** P4 is a pointer to the VM containing the trigger program.
68567 */
68568 case OP_Program: { /* jump */
68569 #if 0 /* local variables moved into u.cb */
68570 int nMem; /* Number of memory registers for sub-program */
68571 int nByte; /* Bytes of runtime space required for sub-program */
68572 Mem *pRt; /* Register to allocate runtime space */
68573 Mem *pMem; /* Used to iterate through memory cells */
68574 Mem *pEnd; /* Last memory cell in new array */
68575 VdbeFrame *pFrame; /* New vdbe frame to execute in */
68576 SubProgram *pProgram; /* Sub-program to execute */
68577 void *t; /* Token identifying trigger */
68578 #endif /* local variables moved into u.cb */
68579
68580 u.cb.pProgram = pOp->p4.pProgram;
68581 u.cb.pRt = &aMem[pOp->p3];
68582 assert( memIsValid(u.cb.pRt) );
68583 assert( u.cb.pProgram->nOp>0 );
68584
68585 /* If the p5 flag is clear, then recursive invocation of triggers is
68586 ** disabled for backwards compatibility (p5 is set if this sub-program
68587 ** is really a trigger, not a foreign key action, and the flag set
68588 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68592,80 +69909,87 @@
68592 ** SubProgram (if the trigger may be executed with more than one different
68593 ** ON CONFLICT algorithm). SubProgram structures associated with a
68594 ** single trigger all have the same value for the SubProgram.token
68595 ** variable. */
68596 if( pOp->p5 ){
68597 u.cb.t = u.cb.pProgram->token;
68598 for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
68599 if( u.cb.pFrame ) break;
68600 }
68601
68602 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68603 rc = SQLITE_ERROR;
68604 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68605 break;
68606 }
68607
68608 /* Register u.cb.pRt is used to store the memory required to save the state
68609 ** of the current program, and the memory required at runtime to execute
68610 ** the trigger program. If this trigger has been fired before, then u.cb.pRt
68611 ** is already allocated. Otherwise, it must be initialized. */
68612 if( (u.cb.pRt->flags&MEM_Frame)==0 ){
68613 /* SubProgram.nMem is set to the number of memory cells used by the
68614 ** program stored in SubProgram.aOp. As well as these, one memory
68615 ** cell is required for each cursor used by the program. Set local
68616 ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
68617 */
68618 u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
68619 u.cb.nByte = ROUND8(sizeof(VdbeFrame))
68620 + u.cb.nMem * sizeof(Mem)
68621 + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
68622 u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
68623 if( !u.cb.pFrame ){
 
68624 goto no_mem;
68625 }
68626 sqlite3VdbeMemRelease(u.cb.pRt);
68627 u.cb.pRt->flags = MEM_Frame;
68628 u.cb.pRt->u.pFrame = u.cb.pFrame;
68629
68630 u.cb.pFrame->v = p;
68631 u.cb.pFrame->nChildMem = u.cb.nMem;
68632 u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
68633 u.cb.pFrame->pc = pc;
68634 u.cb.pFrame->aMem = p->aMem;
68635 u.cb.pFrame->nMem = p->nMem;
68636 u.cb.pFrame->apCsr = p->apCsr;
68637 u.cb.pFrame->nCursor = p->nCursor;
68638 u.cb.pFrame->aOp = p->aOp;
68639 u.cb.pFrame->nOp = p->nOp;
68640 u.cb.pFrame->token = u.cb.pProgram->token;
68641
68642 u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
68643 for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
68644 u.cb.pMem->flags = MEM_Null;
68645 u.cb.pMem->db = db;
 
 
68646 }
68647 }else{
68648 u.cb.pFrame = u.cb.pRt->u.pFrame;
68649 assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
68650 assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
68651 assert( pc==u.cb.pFrame->pc );
68652 }
68653
68654 p->nFrame++;
68655 u.cb.pFrame->pParent = p->pFrame;
68656 u.cb.pFrame->lastRowid = lastRowid;
68657 u.cb.pFrame->nChange = p->nChange;
68658 p->nChange = 0;
68659 p->pFrame = u.cb.pFrame;
68660 p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
68661 p->nMem = u.cb.pFrame->nChildMem;
68662 p->nCursor = (u16)u.cb.pFrame->nChildCsr;
68663 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68664 p->aOp = aOp = u.cb.pProgram->aOp;
68665 p->nOp = u.cb.pProgram->nOp;
 
 
 
68666 pc = -1;
 
68667
68668 break;
68669 }
68670
68671 /* Opcode: Param P1 P2 * * *
@@ -68679,17 +70003,17 @@
68679 ** The address of the cell in the parent frame is determined by adding
68680 ** the value of the P1 argument to the value of the P1 argument to the
68681 ** calling OP_Program instruction.
68682 */
68683 case OP_Param: { /* out2-prerelease */
68684 #if 0 /* local variables moved into u.cc */
68685 VdbeFrame *pFrame;
68686 Mem *pIn;
68687 #endif /* local variables moved into u.cc */
68688 u.cc.pFrame = p->pFrame;
68689 u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
68690 sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
68691 break;
68692 }
68693
68694 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
68695
@@ -68741,26 +70065,26 @@
68741 **
68742 ** This instruction throws an error if the memory cell is not initially
68743 ** an integer.
68744 */
68745 case OP_MemMax: { /* in2 */
68746 #if 0 /* local variables moved into u.cd */
68747 Mem *pIn1;
68748 VdbeFrame *pFrame;
68749 #endif /* local variables moved into u.cd */
68750 if( p->pFrame ){
68751 for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
68752 u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
68753 }else{
68754 u.cd.pIn1 = &aMem[pOp->p1];
68755 }
68756 assert( memIsValid(u.cd.pIn1) );
68757 sqlite3VdbeMemIntegerify(u.cd.pIn1);
68758 pIn2 = &aMem[pOp->p2];
68759 sqlite3VdbeMemIntegerify(pIn2);
68760 if( u.cd.pIn1->u.i<pIn2->u.i){
68761 u.cd.pIn1->u.i = pIn2->u.i;
68762 }
68763 break;
68764 }
68765 #endif /* SQLITE_OMIT_AUTOINCREMENT */
68766
@@ -68823,54 +70147,54 @@
68823 **
68824 ** The P5 arguments are taken from register P2 and its
68825 ** successors.
68826 */
68827 case OP_AggStep: {
68828 #if 0 /* local variables moved into u.ce */
68829 int n;
68830 int i;
68831 Mem *pMem;
68832 Mem *pRec;
68833 sqlite3_context ctx;
68834 sqlite3_value **apVal;
68835 #endif /* local variables moved into u.ce */
68836
68837 u.ce.n = pOp->p5;
68838 assert( u.ce.n>=0 );
68839 u.ce.pRec = &aMem[pOp->p2];
68840 u.ce.apVal = p->apArg;
68841 assert( u.ce.apVal || u.ce.n==0 );
68842 for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
68843 assert( memIsValid(u.ce.pRec) );
68844 u.ce.apVal[u.ce.i] = u.ce.pRec;
68845 memAboutToChange(p, u.ce.pRec);
68846 sqlite3VdbeMemStoreType(u.ce.pRec);
68847 }
68848 u.ce.ctx.pFunc = pOp->p4.pFunc;
68849 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68850 u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
68851 u.ce.pMem->n++;
68852 u.ce.ctx.s.flags = MEM_Null;
68853 u.ce.ctx.s.z = 0;
68854 u.ce.ctx.s.zMalloc = 0;
68855 u.ce.ctx.s.xDel = 0;
68856 u.ce.ctx.s.db = db;
68857 u.ce.ctx.isError = 0;
68858 u.ce.ctx.pColl = 0;
68859 if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68860 assert( pOp>p->aOp );
68861 assert( pOp[-1].p4type==P4_COLLSEQ );
68862 assert( pOp[-1].opcode==OP_CollSeq );
68863 u.ce.ctx.pColl = pOp[-1].p4.pColl;
68864 }
68865 (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
68866 if( u.ce.ctx.isError ){
68867 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
68868 rc = u.ce.ctx.isError;
68869 }
68870
68871 sqlite3VdbeMemRelease(&u.ce.ctx.s);
68872
68873 break;
68874 }
68875
68876 /* Opcode: AggFinal P1 P2 * P4 *
@@ -68884,23 +70208,23 @@
68884 ** functions that can take varying numbers of arguments. The
68885 ** P4 argument is only needed for the degenerate case where
68886 ** the step function was not previously called.
68887 */
68888 case OP_AggFinal: {
68889 #if 0 /* local variables moved into u.cf */
68890 Mem *pMem;
68891 #endif /* local variables moved into u.cf */
68892 assert( pOp->p1>0 && pOp->p1<=p->nMem );
68893 u.cf.pMem = &aMem[pOp->p1];
68894 assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68895 rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
68896 if( rc ){
68897 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
68898 }
68899 sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
68900 UPDATE_MAX_BLOBSIZE(u.cf.pMem);
68901 if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
68902 goto too_big;
68903 }
68904 break;
68905 }
68906
@@ -68915,29 +70239,29 @@
68915 ** in the WAL that have been checkpointed after the checkpoint
68916 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
68917 ** mem[P3+2] are initialized to -1.
68918 */
68919 case OP_Checkpoint: {
68920 #if 0 /* local variables moved into u.cg */
68921 int i; /* Loop counter */
68922 int aRes[3]; /* Results */
68923 Mem *pMem; /* Write results here */
68924 #endif /* local variables moved into u.cg */
68925
68926 u.cg.aRes[0] = 0;
68927 u.cg.aRes[1] = u.cg.aRes[2] = -1;
68928 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
68929 || pOp->p2==SQLITE_CHECKPOINT_FULL
68930 || pOp->p2==SQLITE_CHECKPOINT_RESTART
68931 );
68932 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
68933 if( rc==SQLITE_BUSY ){
68934 rc = SQLITE_OK;
68935 u.cg.aRes[0] = 1;
68936 }
68937 for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
68938 sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
68939 }
68940 break;
68941 };
68942 #endif
68943
@@ -68952,95 +70276,95 @@
68952 ** If changing into or out of WAL mode the procedure is more complicated.
68953 **
68954 ** Write a string containing the final journal-mode to register P2.
68955 */
68956 case OP_JournalMode: { /* out2-prerelease */
68957 #if 0 /* local variables moved into u.ch */
68958 Btree *pBt; /* Btree to change journal mode of */
68959 Pager *pPager; /* Pager associated with pBt */
68960 int eNew; /* New journal mode */
68961 int eOld; /* The old journal mode */
68962 const char *zFilename; /* Name of database file for pPager */
68963 #endif /* local variables moved into u.ch */
68964
68965 u.ch.eNew = pOp->p3;
68966 assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
68967 || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
68968 || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
68969 || u.ch.eNew==PAGER_JOURNALMODE_OFF
68970 || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
68971 || u.ch.eNew==PAGER_JOURNALMODE_WAL
68972 || u.ch.eNew==PAGER_JOURNALMODE_QUERY
68973 );
68974 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68975
68976 u.ch.pBt = db->aDb[pOp->p1].pBt;
68977 u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
68978 u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
68979 if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
68980 if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
68981
68982 #ifndef SQLITE_OMIT_WAL
68983 u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
68984
68985 /* Do not allow a transition to journal_mode=WAL for a database
68986 ** in temporary storage or if the VFS does not support shared memory
68987 */
68988 if( u.ch.eNew==PAGER_JOURNALMODE_WAL
68989 && (sqlite3Strlen30(u.ch.zFilename)==0 /* Temp file */
68990 || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */
68991 ){
68992 u.ch.eNew = u.ch.eOld;
68993 }
68994
68995 if( (u.ch.eNew!=u.ch.eOld)
68996 && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
68997 ){
68998 if( !db->autoCommit || db->activeVdbeCnt>1 ){
68999 rc = SQLITE_ERROR;
69000 sqlite3SetString(&p->zErrMsg, db,
69001 "cannot change %s wal mode from within a transaction",
69002 (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69003 );
69004 break;
69005 }else{
69006
69007 if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
69008 /* If leaving WAL mode, close the log file. If successful, the call
69009 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
69010 ** file. An EXCLUSIVE lock may still be held on the database file
69011 ** after a successful return.
69012 */
69013 rc = sqlite3PagerCloseWal(u.ch.pPager);
69014 if( rc==SQLITE_OK ){
69015 sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69016 }
69017 }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
69018 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
69019 ** as an intermediate */
69020 sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
69021 }
69022
69023 /* Open a transaction on the database file. Regardless of the journal
69024 ** mode, this transaction always uses a rollback journal.
69025 */
69026 assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
69027 if( rc==SQLITE_OK ){
69028 rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69029 }
69030 }
69031 }
69032 #endif /* ifndef SQLITE_OMIT_WAL */
69033
69034 if( rc ){
69035 u.ch.eNew = u.ch.eOld;
69036 }
69037 u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69038
69039 pOut = &aMem[pOp->p2];
69040 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69041 pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
69042 pOut->n = sqlite3Strlen30(pOut->z);
69043 pOut->enc = SQLITE_UTF8;
69044 sqlite3VdbeChangeEncoding(pOut, encoding);
69045 break;
69046 };
@@ -69065,18 +70389,18 @@
69065 ** Perform a single step of the incremental vacuum procedure on
69066 ** the P1 database. If the vacuum has finished, jump to instruction
69067 ** P2. Otherwise, fall through to the next instruction.
69068 */
69069 case OP_IncrVacuum: { /* jump */
69070 #if 0 /* local variables moved into u.ci */
69071 Btree *pBt;
69072 #endif /* local variables moved into u.ci */
69073
69074 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69075 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69076 u.ci.pBt = db->aDb[pOp->p1].pBt;
69077 rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
69078 if( rc==SQLITE_DONE ){
69079 pc = pOp->p2 - 1;
69080 rc = SQLITE_OK;
69081 }
69082 break;
@@ -69142,16 +70466,16 @@
69142 ** Also, whether or not P4 is set, check that this is not being called from
69143 ** within a callback to a virtual table xSync() method. If it is, the error
69144 ** code will be set to SQLITE_LOCKED.
69145 */
69146 case OP_VBegin: {
69147 #if 0 /* local variables moved into u.cj */
69148 VTable *pVTab;
69149 #endif /* local variables moved into u.cj */
69150 u.cj.pVTab = pOp->p4.pVtab;
69151 rc = sqlite3VtabBegin(db, u.cj.pVTab);
69152 if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
69153 break;
69154 }
69155 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69156
69157 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69186,36 +70510,36 @@
69186 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69187 ** P1 is a cursor number. This opcode opens a cursor to the virtual
69188 ** table and stores that cursor in P1.
69189 */
69190 case OP_VOpen: {
69191 #if 0 /* local variables moved into u.ck */
69192 VdbeCursor *pCur;
69193 sqlite3_vtab_cursor *pVtabCursor;
69194 sqlite3_vtab *pVtab;
69195 sqlite3_module *pModule;
69196 #endif /* local variables moved into u.ck */
69197
69198 u.ck.pCur = 0;
69199 u.ck.pVtabCursor = 0;
69200 u.ck.pVtab = pOp->p4.pVtab->pVtab;
69201 u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
69202 assert(u.ck.pVtab && u.ck.pModule);
69203 rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
69204 importVtabErrMsg(p, u.ck.pVtab);
69205 if( SQLITE_OK==rc ){
69206 /* Initialize sqlite3_vtab_cursor base class */
69207 u.ck.pVtabCursor->pVtab = u.ck.pVtab;
69208
69209 /* Initialise vdbe cursor object */
69210 u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69211 if( u.ck.pCur ){
69212 u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
69213 u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
69214 }else{
69215 db->mallocFailed = 1;
69216 u.ck.pModule->xClose(u.ck.pVtabCursor);
69217 }
69218 }
69219 break;
69220 }
69221 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69238,11 +70562,11 @@
69238 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
69239 **
69240 ** A jump is made to P2 if the result set after filtering would be empty.
69241 */
69242 case OP_VFilter: { /* jump */
69243 #if 0 /* local variables moved into u.cl */
69244 int nArg;
69245 int iQuery;
69246 const sqlite3_module *pModule;
69247 Mem *pQuery;
69248 Mem *pArgc;
@@ -69250,49 +70574,49 @@
69250 sqlite3_vtab *pVtab;
69251 VdbeCursor *pCur;
69252 int res;
69253 int i;
69254 Mem **apArg;
69255 #endif /* local variables moved into u.cl */
69256
69257 u.cl.pQuery = &aMem[pOp->p3];
69258 u.cl.pArgc = &u.cl.pQuery[1];
69259 u.cl.pCur = p->apCsr[pOp->p1];
69260 assert( memIsValid(u.cl.pQuery) );
69261 REGISTER_TRACE(pOp->p3, u.cl.pQuery);
69262 assert( u.cl.pCur->pVtabCursor );
69263 u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
69264 u.cl.pVtab = u.cl.pVtabCursor->pVtab;
69265 u.cl.pModule = u.cl.pVtab->pModule;
69266
69267 /* Grab the index number and argc parameters */
69268 assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
69269 u.cl.nArg = (int)u.cl.pArgc->u.i;
69270 u.cl.iQuery = (int)u.cl.pQuery->u.i;
69271
69272 /* Invoke the xFilter method */
69273 {
69274 u.cl.res = 0;
69275 u.cl.apArg = p->apArg;
69276 for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
69277 u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
69278 sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
69279 }
69280
69281 p->inVtabMethod = 1;
69282 rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
69283 p->inVtabMethod = 0;
69284 importVtabErrMsg(p, u.cl.pVtab);
69285 if( rc==SQLITE_OK ){
69286 u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
69287 }
69288
69289 if( u.cl.res ){
69290 pc = pOp->p2 - 1;
69291 }
69292 }
69293 u.cl.pCur->nullRow = 0;
69294
69295 break;
69296 }
69297 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69298
@@ -69302,55 +70626,55 @@
69302 ** Store the value of the P2-th column of
69303 ** the row of the virtual-table that the
69304 ** P1 cursor is pointing to into register P3.
69305 */
69306 case OP_VColumn: {
69307 #if 0 /* local variables moved into u.cm */
69308 sqlite3_vtab *pVtab;
69309 const sqlite3_module *pModule;
69310 Mem *pDest;
69311 sqlite3_context sContext;
69312 #endif /* local variables moved into u.cm */
69313
69314 VdbeCursor *pCur = p->apCsr[pOp->p1];
69315 assert( pCur->pVtabCursor );
69316 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69317 u.cm.pDest = &aMem[pOp->p3];
69318 memAboutToChange(p, u.cm.pDest);
69319 if( pCur->nullRow ){
69320 sqlite3VdbeMemSetNull(u.cm.pDest);
69321 break;
69322 }
69323 u.cm.pVtab = pCur->pVtabCursor->pVtab;
69324 u.cm.pModule = u.cm.pVtab->pModule;
69325 assert( u.cm.pModule->xColumn );
69326 memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
69327
69328 /* The output cell may already have a buffer allocated. Move
69329 ** the current contents to u.cm.sContext.s so in case the user-function
69330 ** can use the already allocated buffer instead of allocating a
69331 ** new one.
69332 */
69333 sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
69334 MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
69335
69336 rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
69337 importVtabErrMsg(p, u.cm.pVtab);
69338 if( u.cm.sContext.isError ){
69339 rc = u.cm.sContext.isError;
69340 }
69341
69342 /* Copy the result of the function to the P3 register. We
69343 ** do this regardless of whether or not an error occurred to ensure any
69344 ** dynamic allocation in u.cm.sContext.s (a Mem struct) is released.
69345 */
69346 sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
69347 sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
69348 REGISTER_TRACE(pOp->p3, u.cm.pDest);
69349 UPDATE_MAX_BLOBSIZE(u.cm.pDest);
69350
69351 if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
69352 goto too_big;
69353 }
69354 break;
69355 }
69356 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69361,42 +70685,42 @@
69361 ** Advance virtual table P1 to the next row in its result set and
69362 ** jump to instruction P2. Or, if the virtual table has reached
69363 ** the end of its result set, then fall through to the next instruction.
69364 */
69365 case OP_VNext: { /* jump */
69366 #if 0 /* local variables moved into u.cn */
69367 sqlite3_vtab *pVtab;
69368 const sqlite3_module *pModule;
69369 int res;
69370 VdbeCursor *pCur;
69371 #endif /* local variables moved into u.cn */
69372
69373 u.cn.res = 0;
69374 u.cn.pCur = p->apCsr[pOp->p1];
69375 assert( u.cn.pCur->pVtabCursor );
69376 if( u.cn.pCur->nullRow ){
69377 break;
69378 }
69379 u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
69380 u.cn.pModule = u.cn.pVtab->pModule;
69381 assert( u.cn.pModule->xNext );
69382
69383 /* Invoke the xNext() method of the module. There is no way for the
69384 ** underlying implementation to return an error if one occurs during
69385 ** xNext(). Instead, if an error occurs, true is returned (indicating that
69386 ** data is available) and the error code returned when xColumn or
69387 ** some other method is next invoked on the save virtual table cursor.
69388 */
69389 p->inVtabMethod = 1;
69390 rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
69391 p->inVtabMethod = 0;
69392 importVtabErrMsg(p, u.cn.pVtab);
69393 if( rc==SQLITE_OK ){
69394 u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
69395 }
69396
69397 if( !u.cn.res ){
69398 /* If there is data, jump to P2 */
69399 pc = pOp->p2 - 1;
69400 }
69401 break;
69402 }
@@ -69408,28 +70732,28 @@
69408 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69409 ** This opcode invokes the corresponding xRename method. The value
69410 ** in register P1 is passed as the zName argument to the xRename method.
69411 */
69412 case OP_VRename: {
69413 #if 0 /* local variables moved into u.co */
69414 sqlite3_vtab *pVtab;
69415 Mem *pName;
69416 #endif /* local variables moved into u.co */
69417
69418 u.co.pVtab = pOp->p4.pVtab->pVtab;
69419 u.co.pName = &aMem[pOp->p1];
69420 assert( u.co.pVtab->pModule->xRename );
69421 assert( memIsValid(u.co.pName) );
69422 REGISTER_TRACE(pOp->p1, u.co.pName);
69423 assert( u.co.pName->flags & MEM_Str );
69424 testcase( u.co.pName->enc==SQLITE_UTF8 );
69425 testcase( u.co.pName->enc==SQLITE_UTF16BE );
69426 testcase( u.co.pName->enc==SQLITE_UTF16LE );
69427 rc = sqlite3VdbeChangeEncoding(u.co.pName, SQLITE_UTF8);
69428 if( rc==SQLITE_OK ){
69429 rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
69430 importVtabErrMsg(p, u.co.pVtab);
69431 p->expired = 0;
69432 }
69433 break;
69434 }
69435 #endif
@@ -69457,45 +70781,45 @@
69457 ** P1 is a boolean flag. If it is set to true and the xUpdate call
69458 ** is successful, then the value returned by sqlite3_last_insert_rowid()
69459 ** is set to the value of the rowid for the row just inserted.
69460 */
69461 case OP_VUpdate: {
69462 #if 0 /* local variables moved into u.cp */
69463 sqlite3_vtab *pVtab;
69464 sqlite3_module *pModule;
69465 int nArg;
69466 int i;
69467 sqlite_int64 rowid;
69468 Mem **apArg;
69469 Mem *pX;
69470 #endif /* local variables moved into u.cp */
69471
69472 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
69473 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
69474 );
69475 u.cp.pVtab = pOp->p4.pVtab->pVtab;
69476 u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
69477 u.cp.nArg = pOp->p2;
69478 assert( pOp->p4type==P4_VTAB );
69479 if( ALWAYS(u.cp.pModule->xUpdate) ){
69480 u8 vtabOnConflict = db->vtabOnConflict;
69481 u.cp.apArg = p->apArg;
69482 u.cp.pX = &aMem[pOp->p3];
69483 for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
69484 assert( memIsValid(u.cp.pX) );
69485 memAboutToChange(p, u.cp.pX);
69486 sqlite3VdbeMemStoreType(u.cp.pX);
69487 u.cp.apArg[u.cp.i] = u.cp.pX;
69488 u.cp.pX++;
69489 }
69490 db->vtabOnConflict = pOp->p5;
69491 rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
69492 db->vtabOnConflict = vtabOnConflict;
69493 importVtabErrMsg(p, u.cp.pVtab);
69494 if( rc==SQLITE_OK && pOp->p1 ){
69495 assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
69496 db->lastRowid = lastRowid = u.cp.rowid;
69497 }
69498 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
69499 if( pOp->p5==OE_Ignore ){
69500 rc = SQLITE_OK;
69501 }else{
@@ -69551,25 +70875,25 @@
69551 **
69552 ** If tracing is enabled (by the sqlite3_trace()) interface, then
69553 ** the UTF-8 string contained in P4 is emitted on the trace callback.
69554 */
69555 case OP_Trace: {
69556 #if 0 /* local variables moved into u.cq */
69557 char *zTrace;
69558 char *z;
69559 #endif /* local variables moved into u.cq */
69560
69561 if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
69562 u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
69563 db->xTrace(db->pTraceArg, u.cq.z);
69564 sqlite3DbFree(db, u.cq.z);
69565 }
69566 #ifdef SQLITE_DEBUG
69567 if( (db->flags & SQLITE_SqlTrace)!=0
69568 && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69569 ){
69570 sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
69571 }
69572 #endif /* SQLITE_DEBUG */
69573 break;
69574 }
69575 #endif
@@ -72487,11 +73811,11 @@
72487 pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
72488 if( pE==0 ) return 1;
72489 pE->pColl = pColl;
72490 pE->flags |= EP_IntValue | flags;
72491 pE->u.iValue = iCol;
72492 pItem->iCol = (u16)iCol;
72493 pItem->done = 1;
72494 }else{
72495 moreToDo = 1;
72496 }
72497 }
@@ -72536,16 +73860,16 @@
72536 }
72537 #endif
72538 pEList = pSelect->pEList;
72539 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
72540 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
72541 if( pItem->iCol ){
72542 if( pItem->iCol>pEList->nExpr ){
72543 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
72544 return 1;
72545 }
72546 resolveAlias(pParse, pEList, pItem->iCol-1, pItem->pExpr, zType);
72547 }
72548 }
72549 return 0;
72550 }
72551
@@ -72588,11 +73912,11 @@
72588 if( iCol>0 ){
72589 /* If an AS-name match is found, mark this ORDER BY column as being
72590 ** a copy of the iCol-th result-set column. The subsequent call to
72591 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
72592 ** copy of the iCol-th result-set expression. */
72593 pItem->iCol = (u16)iCol;
72594 continue;
72595 }
72596 if( sqlite3ExprIsInteger(pE, &iCol) ){
72597 /* The ORDER BY term is an integer constant. Again, set the column
72598 ** number so that sqlite3ResolveOrderGroupBy() will convert the
@@ -72599,16 +73923,16 @@
72599 ** order-by term to a copy of the result-set expression */
72600 if( iCol<1 ){
72601 resolveOutOfRangeError(pParse, zType, i+1, nResult);
72602 return 1;
72603 }
72604 pItem->iCol = (u16)iCol;
72605 continue;
72606 }
72607
72608 /* Otherwise, treat the ORDER BY term as an ordinary expression */
72609 pItem->iCol = 0;
72610 if( sqlite3ResolveExprNames(pNC, pE) ){
72611 return 1;
72612 }
72613 }
72614 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
@@ -73783,11 +75107,11 @@
73783 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
73784 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
73785 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
73786 pItem->sortOrder = pOldItem->sortOrder;
73787 pItem->done = 0;
73788 pItem->iCol = pOldItem->iCol;
73789 pItem->iAlias = pOldItem->iAlias;
73790 }
73791 return pNew;
73792 }
73793
@@ -73853,11 +75177,11 @@
73853 pNewItem->idx = pOldItem->idx;
73854 }
73855 return pNew;
73856 }
73857 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
73858 Select *pNew;
73859 if( p==0 ) return 0;
73860 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
73861 if( pNew==0 ) return 0;
73862 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
73863 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
@@ -73864,11 +75188,13 @@
73864 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
73865 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
73866 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
73867 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
73868 pNew->op = p->op;
73869 pNew->pPrior = sqlite3SelectDup(db, p->pPrior, flags);
 
 
73870 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
73871 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
73872 pNew->iLimit = 0;
73873 pNew->iOffset = 0;
73874 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
@@ -74285,10 +75611,19 @@
74285 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
74286 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
74287 return 1;
74288 }
74289 #endif /* SQLITE_OMIT_SUBQUERY */
 
 
 
 
 
 
 
 
 
74290
74291 /*
74292 ** This function is used by the implementation of the IN (...) operator.
74293 ** It's job is to find or create a b-tree structure that may be used
74294 ** either to test for membership of the (...) set or to iterate through
@@ -74346,10 +75681,11 @@
74346 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
74347 Select *p; /* SELECT to the right of IN operator */
74348 int eType = 0; /* Type of RHS table. IN_INDEX_* */
74349 int iTab = pParse->nTab++; /* Cursor of the RHS table */
74350 int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
 
74351
74352 assert( pX->op==TK_IN );
74353
74354 /* Check to see if an existing table or index can be used to
74355 ** satisfy the query. This is preferable to generating a new
@@ -74356,11 +75692,10 @@
74356 ** ephemeral table.
74357 */
74358 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
74359 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
74360 sqlite3 *db = pParse->db; /* Database connection */
74361 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
74362 Table *pTab; /* Table <table>. */
74363 Expr *pExpr; /* Expression <column> */
74364 int iCol; /* Index of column <column> */
74365 int iDb; /* Database idx for pTab */
74366
@@ -74381,14 +75716,13 @@
74381 ** has already been allocated. So assume sqlite3GetVdbe() is always
74382 ** successful here.
74383 */
74384 assert(v);
74385 if( iCol<0 ){
74386 int iMem = ++pParse->nMem;
74387 int iAddr;
74388
74389 iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
74390
74391 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
74392 eType = IN_INDEX_ROWID;
74393
74394 sqlite3VdbeJumpHere(v, iAddr);
@@ -74410,25 +75744,25 @@
74410 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
74411 if( (pIdx->aiColumn[0]==iCol)
74412 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
74413 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
74414 ){
74415 int iMem = ++pParse->nMem;
74416 int iAddr;
74417 char *pKey;
74418
74419 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
74420 iAddr = sqlite3VdbeAddOp1(v, OP_Once, iMem);
74421
74422 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
74423 pKey,P4_KEYINFO_HANDOFF);
74424 VdbeComment((v, "%s", pIdx->zName));
74425 eType = IN_INDEX_INDEX;
74426
74427 sqlite3VdbeJumpHere(v, iAddr);
74428 if( prNotFound && !pTab->aCol[iCol].notNull ){
74429 *prNotFound = ++pParse->nMem;
 
74430 }
74431 }
74432 }
74433 }
74434 }
@@ -74440,10 +75774,11 @@
74440 double savedNQueryLoop = pParse->nQueryLoop;
74441 int rMayHaveNull = 0;
74442 eType = IN_INDEX_EPH;
74443 if( prNotFound ){
74444 *prNotFound = rMayHaveNull = ++pParse->nMem;
 
74445 }else{
74446 testcase( pParse->nQueryLoop>(double)1 );
74447 pParse->nQueryLoop = (double)1;
74448 if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
74449 eType = IN_INDEX_ROWID;
@@ -74512,13 +75847,12 @@
74512 ** * We are inside a trigger
74513 **
74514 ** If all of the above are false, then we can run this code just once
74515 ** save the results, and reuse the same result on subsequent invocations.
74516 */
74517 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->pTriggerTab ){
74518 int mem = ++pParse->nMem;
74519 testAddr = sqlite3VdbeAddOp1(v, OP_Once, mem);
74520 }
74521
74522 #ifndef SQLITE_OMIT_EXPLAIN
74523 if( pParse->explain==2 ){
74524 char *zMsg = sqlite3MPrintf(
@@ -75852,10 +77186,268 @@
75852 pExpr->op = TK_REGISTER;
75853 }
75854 return inReg;
75855 }
75856
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75857 /*
75858 ** Return TRUE if pExpr is an constant expression that is appropriate
75859 ** for factoring out of a loop. Appropriate expressions are:
75860 **
75861 ** * Any expression that evaluates to two or more opcodes.
@@ -76675,10 +78267,18 @@
76675 if( nReg>pParse->nRangeReg ){
76676 pParse->nRangeReg = nReg;
76677 pParse->iRangeReg = iReg;
76678 }
76679 }
 
 
 
 
 
 
 
 
76680
76681 /************** End of expr.c ************************************************/
76682 /************** Begin file alter.c *******************************************/
76683 /*
76684 ** 2005 February 15
@@ -78036,10 +79636,11 @@
78036 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
78037 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
78038 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
78039 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
78040 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
 
78041 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
78042 (char*)&stat3InitFuncdef, P4_FUNCDEF);
78043 sqlite3VdbeChangeP5(v, 2);
78044 #endif /* SQLITE_ENABLE_STAT3 */
78045
@@ -82100,23 +83701,27 @@
82100 ** Allocate the index structure.
82101 */
82102 nName = sqlite3Strlen30(zName);
82103 nCol = pList->nExpr;
82104 pIndex = sqlite3DbMallocZero(db,
82105 sizeof(Index) + /* Index structure */
82106 sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
82107 sizeof(int)*nCol + /* Index.aiColumn */
82108 sizeof(char *)*nCol + /* Index.azColl */
82109 sizeof(u8)*nCol + /* Index.aSortOrder */
82110 nName + 1 + /* Index.zName */
82111 nExtra /* Collation sequence names */
82112 );
82113 if( db->mallocFailed ){
82114 goto exit_create_index;
82115 }
82116 pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
82117 pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
 
 
 
 
82118 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
82119 pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
82120 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
82121 zExtra = (char *)(&pIndex->zName[nName+1]);
82122 memcpy(pIndex->zName, zName, nName+1);
@@ -83866,11 +85471,10 @@
83866
83867 /* Check that there isn't an ORDER BY without a LIMIT clause.
83868 */
83869 if( pOrderBy && (pLimit == 0) ) {
83870 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
83871 pParse->parseError = 1;
83872 goto limit_where_cleanup_2;
83873 }
83874
83875 /* We only need to generate a select expression if there
83876 ** is a limit/offset term to enforce.
@@ -87246,11 +88850,11 @@
87246 ** 'b' NONE
87247 ** 'c' NUMERIC
87248 ** 'd' INTEGER
87249 ** 'e' REAL
87250 **
87251 ** An extra 'b' is appended to the end of the string to cover the
87252 ** rowid that appears as the last column in every index.
87253 **
87254 ** Memory for the buffer containing the column index affinity string
87255 ** is managed along with the rest of the Index structure. It will be
87256 ** released when sqlite3DeleteIndex() is called.
@@ -87274,11 +88878,11 @@
87274 return 0;
87275 }
87276 for(n=0; n<pIdx->nColumn; n++){
87277 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
87278 }
87279 pIdx->zColAff[n++] = SQLITE_AFF_NONE;
87280 pIdx->zColAff[n] = 0;
87281 }
87282
87283 return pIdx->zColAff;
87284 }
@@ -87438,10 +89042,11 @@
87438 for(p = pParse->pAinc; p; p = p->pNext){
87439 pDb = &db->aDb[p->iDb];
87440 memId = p->regCtr;
87441 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
87442 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 
87443 addr = sqlite3VdbeCurrentAddr(v);
87444 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
87445 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
87446 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
87447 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
@@ -88305,11 +89910,11 @@
88305 ** any ABORT Back out changes from the current command
88306 ** only (do not do a complete rollback) then
88307 ** cause sqlite3_exec() to return immediately
88308 ** with SQLITE_CONSTRAINT.
88309 **
88310 ** any FAIL Sqlite_exec() returns immediately with a
88311 ** return code of SQLITE_CONSTRAINT. The
88312 ** transaction is not rolled back and any
88313 ** prior changes are retained.
88314 **
88315 ** any IGNORE The record number and data is popped from
@@ -88777,35 +90382,29 @@
88777 /*
88778 ** Attempt the transfer optimization on INSERTs of the form
88779 **
88780 ** INSERT INTO tab1 SELECT * FROM tab2;
88781 **
88782 ** This optimization is only attempted if
88783 **
88784 ** (1) tab1 and tab2 have identical schemas including all the
88785 ** same indices and constraints
88786 **
88787 ** (2) tab1 and tab2 are different tables
88788 **
88789 ** (3) There must be no triggers on tab1
88790 **
88791 ** (4) The result set of the SELECT statement is "*"
88792 **
88793 ** (5) The SELECT statement has no WHERE, HAVING, ORDER BY, GROUP BY,
88794 ** or LIMIT clause.
88795 **
88796 ** (6) The SELECT statement is a simple (not a compound) select that
88797 ** contains only tab2 in its FROM clause
88798 **
88799 ** This method for implementing the INSERT transfers raw records from
88800 ** tab2 over to tab1. The columns are not decoded. Raw records from
88801 ** the indices of tab2 are transfered to tab1 as well. In so doing,
88802 ** the resulting tab1 has much less fragmentation.
88803 **
88804 ** This routine returns TRUE if the optimization is attempted. If any
88805 ** of the conditions above fail so that the optimization should not
88806 ** be attempted, then this routine returns FALSE.
88807 */
88808 static int xferOptimization(
88809 Parse *pParse, /* Parser context */
88810 Table *pDest, /* The table we are inserting into */
88811 Select *pSelect, /* A SELECT statement to use as the data source */
@@ -88838,14 +90437,12 @@
88838 if( pDest->tabFlags & TF_Virtual ){
88839 return 0; /* tab1 must not be a virtual table */
88840 }
88841 #endif
88842 if( onError==OE_Default ){
88843 onError = OE_Abort;
88844 }
88845 if( onError!=OE_Abort && onError!=OE_Rollback ){
88846 return 0; /* Cannot do OR REPLACE or OR IGNORE or OR FAIL */
88847 }
88848 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
88849 if( pSelect->pSrc->nSrc!=1 ){
88850 return 0; /* FROM clause must have exactly one term */
88851 }
@@ -88947,20 +90544,16 @@
88947 if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
88948 return 0;
88949 }
88950 #endif
88951 if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
88952 return 0;
88953 }
88954
88955 /* If we get this far, it means either:
88956 **
88957 ** * We can always do the transfer if the table contains an
88958 ** an integer primary key
88959 **
88960 ** * We can conditionally do the transfer if the destination
88961 ** table is empty.
88962 */
88963 #ifdef SQLITE_TEST
88964 sqlite3_xferopt_count++;
88965 #endif
88966 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
@@ -88968,20 +90561,27 @@
88968 sqlite3CodeVerifySchema(pParse, iDbSrc);
88969 iSrc = pParse->nTab++;
88970 iDest = pParse->nTab++;
88971 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
88972 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
88973 if( (pDest->iPKey<0 && pDest->pIndex!=0) || destHasUniqueIdx ){
88974 /* If tables do not have an INTEGER PRIMARY KEY and there
88975 ** are indices to be copied and the destination is not empty,
88976 ** we have to disallow the transfer optimization because the
88977 ** the rowids might change which will mess up indexing.
 
 
 
88978 **
88979 ** Or if the destination has a UNIQUE index and is not empty,
88980 ** we also disallow the transfer optimization because we cannot
88981 ** insure that all entries in the union of DEST and SRC will be
88982 ** unique.
 
 
 
 
88983 */
88984 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
88985 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
88986 sqlite3VdbeJumpHere(v, addr1);
88987 }else{
@@ -90269,10 +91869,11 @@
90269 ** If anything goes wrong, set an error in the database connection.
90270 */
90271 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
90272 int i;
90273 int go = 1;
 
90274 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
90275
90276 wsdAutoextInit;
90277 if( wsdAutoext.nExt==0 ){
90278 /* Common case: early out without every having to acquire a mutex */
@@ -90291,12 +91892,12 @@
90291 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
90292 wsdAutoext.aExt[i];
90293 }
90294 sqlite3_mutex_leave(mutex);
90295 zErrmsg = 0;
90296 if( xInit && xInit(db, &zErrmsg, &sqlite3Apis) ){
90297 sqlite3Error(db, SQLITE_ERROR,
90298 "automatic extension loading failed: %s", zErrmsg);
90299 go = 0;
90300 }
90301 sqlite3_free(zErrmsg);
90302 }
@@ -90649,11 +92250,11 @@
90649 zDb = pId2->n>0 ? pDb->zName : 0;
90650 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
90651 goto pragma_out;
90652 }
90653
90654 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
90655 /*
90656 ** PRAGMA [database.]default_cache_size
90657 ** PRAGMA [database.]default_cache_size=N
90658 **
90659 ** The first form reports the current persistent setting for the
@@ -90698,11 +92299,13 @@
90698 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
90699 pDb->pSchema->cache_size = size;
90700 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
90701 }
90702 }else
 
90703
 
90704 /*
90705 ** PRAGMA [database.]page_size
90706 ** PRAGMA [database.]page_size=N
90707 **
90708 ** The first form reports the current setting for the
@@ -90719,11 +92322,11 @@
90719 }else{
90720 /* Malloc may fail when setting the page-size, as there is an internal
90721 ** buffer that the pager module resizes using sqlite3_realloc().
90722 */
90723 db->nextPagesize = sqlite3Atoi(zRight);
90724 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
90725 db->mallocFailed = 1;
90726 }
90727 }
90728 }else
90729
@@ -90759,10 +92362,14 @@
90759 ** The first form reports the current setting for the
90760 ** maximum number of pages in the database file. The
90761 ** second form attempts to change this setting. Both
90762 ** forms return the current setting.
90763 **
 
 
 
 
90764 ** PRAGMA [database.]page_count
90765 **
90766 ** Return the number of pages in the specified database.
90767 */
90768 if( sqlite3StrICmp(zLeft,"page_count")==0
@@ -90773,11 +92380,12 @@
90773 sqlite3CodeVerifySchema(pParse, iDb);
90774 iReg = ++pParse->nMem;
90775 if( sqlite3Tolower(zLeft[0])=='p' ){
90776 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
90777 }else{
90778 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
 
90779 }
90780 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
90781 sqlite3VdbeSetNumCols(v, 1);
90782 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
90783 }else
@@ -90987,26 +92595,23 @@
90987 /*
90988 ** PRAGMA [database.]cache_size
90989 ** PRAGMA [database.]cache_size=N
90990 **
90991 ** The first form reports the current local setting for the
90992 ** page cache size. The local setting can be different from
90993 ** the persistent cache size value that is stored in the database
90994 ** file itself. The value returned is the maximum number of
90995 ** pages in the page cache. The second form sets the local
90996 ** page cache size value. It does not change the persistent
90997 ** cache size stored on the disk so the cache size will revert
90998 ** to its default value when the database is closed and reopened.
90999 ** N should be a positive integer.
91000 */
91001 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
91002 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91003 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
91004 if( !zRight ){
91005 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
91006 }else{
91007 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
91008 pDb->pSchema->cache_size = size;
91009 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
91010 }
91011 }else
91012
@@ -91094,11 +92699,11 @@
91094 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
91095 if( !zRight ){
91096 Pager *pPager = sqlite3BtreePager(pDb->pBt);
91097 char *proxy_file_path = NULL;
91098 sqlite3_file *pFile = sqlite3PagerFile(pPager);
91099 sqlite3OsFileControl(pFile, SQLITE_GET_LOCKPROXYFILE,
91100 &proxy_file_path);
91101
91102 if( proxy_file_path ){
91103 sqlite3VdbeSetNumCols(v, 1);
91104 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
@@ -91737,10 +93342,20 @@
91737 db->xWalCallback==sqlite3WalDefaultHook ?
91738 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
91739 }else
91740 #endif
91741
 
 
 
 
 
 
 
 
 
 
91742 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
91743 /*
91744 ** Report the current state of file logs for all databases
91745 */
91746 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92111,13 +93726,17 @@
92111 DbSetProperty(db, iDb, DB_Empty);
92112 }
92113 pDb->pSchema->enc = ENC(db);
92114
92115 if( pDb->pSchema->cache_size==0 ){
 
92116 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
92117 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
92118 pDb->pSchema->cache_size = size;
 
 
 
92119 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
92120 }
92121
92122 /*
92123 ** file_format==1 Version 3.0.0.
@@ -94912,20 +96531,20 @@
94912 */
94913 if( op!=TK_ALL ){
94914 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
94915 struct ExprList_item *pItem;
94916 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
94917 assert( pItem->iCol>0 );
94918 if( pItem->iCol==i ) break;
94919 }
94920 if( j==nOrderBy ){
94921 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
94922 if( pNew==0 ) return SQLITE_NOMEM;
94923 pNew->flags |= EP_IntValue;
94924 pNew->u.iValue = i;
94925 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
94926 pOrderBy->a[nOrderBy++].iCol = (u16)i;
94927 }
94928 }
94929 }
94930
94931 /* Compute the comparison permutation and keyinfo that is used with
@@ -94937,12 +96556,12 @@
94937 */
94938 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
94939 if( aPermute ){
94940 struct ExprList_item *pItem;
94941 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
94942 assert( pItem->iCol>0 && pItem->iCol<=p->pEList->nExpr );
94943 aPermute[i] = pItem->iCol - 1;
94944 }
94945 pKeyMerge =
94946 sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
94947 if( pKeyMerge ){
94948 pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
@@ -95281,13 +96900,12 @@
95281 }
95282 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
95283
95284 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
95285 /*
95286 ** This routine attempts to flatten subqueries in order to speed
95287 ** execution. It returns 1 if it makes changes and 0 if no flattening
95288 ** occurs.
95289 **
95290 ** To understand the concept of flattening, consider the following
95291 ** query:
95292 **
95293 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
@@ -95325,11 +96943,14 @@
95325 ** (4) has since been expanded to exclude all DISTINCT subqueries.
95326 **
95327 ** (6) The subquery does not use aggregates or the outer query is not
95328 ** DISTINCT.
95329 **
95330 ** (7) The subquery has a FROM clause.
 
 
 
95331 **
95332 ** (8) The subquery does not use LIMIT or the outer query is not a join.
95333 **
95334 ** (9) The subquery does not use LIMIT or the outer query does not use
95335 ** aggregates.
@@ -95358,15 +96979,18 @@
95358 ** compound clause made up entirely of non-aggregate queries, and
95359 ** the parent query:
95360 **
95361 ** * is not itself part of a compound select,
95362 ** * is not an aggregate or DISTINCT query, and
95363 ** * has no other tables or sub-selects in the FROM clause.
95364 **
95365 ** The parent and sub-query may contain WHERE clauses. Subject to
95366 ** rules (11), (13) and (14), they may also contain ORDER BY,
95367 ** LIMIT and OFFSET clauses.
 
 
 
95368 **
95369 ** (18) If the sub-query is a compound select, then all terms of the
95370 ** ORDER by clause of the parent must be simple references to
95371 ** columns of the sub-query.
95372 **
@@ -95374,11 +96998,11 @@
95374 ** have a WHERE clause.
95375 **
95376 ** (20) If the sub-query is a compound select, then it must not use
95377 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
95378 ** somewhat by saying that the terms of the ORDER BY clause must
95379 ** appear as unmodified result columns in the outer query. But
95380 ** have other optimizations in mind to deal with that case.
95381 **
95382 ** (21) The subquery does not use LIMIT or the outer query is not
95383 ** DISTINCT. (See ticket [752e1646fc]).
95384 **
@@ -95503,23 +97127,25 @@
95503 return 0;
95504 }
95505 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
95506 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
95507 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
 
95508 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
95509 || (pSub1->pPrior && pSub1->op!=TK_ALL)
95510 || NEVER(pSub1->pSrc==0) || pSub1->pSrc->nSrc!=1
95511 ){
95512 return 0;
95513 }
 
95514 }
95515
95516 /* Restriction 18. */
95517 if( p->pOrderBy ){
95518 int ii;
95519 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
95520 if( p->pOrderBy->a[ii].iCol==0 ) return 0;
95521 }
95522 }
95523 }
95524
95525 /***** If we reach this point, flattening is permitted. *****/
@@ -96538,26 +98164,25 @@
96538 assert( pItem->addrFillSub==0 );
96539 pItem->regReturn = ++pParse->nMem;
96540 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
96541 pItem->addrFillSub = topAddr+1;
96542 VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
96543 if( pItem->isCorrelated==0 && pParse->pTriggerTab==0 ){
96544 /* If the subquery is no correlated and if we are not inside of
96545 ** a trigger, then we only need to compute the value of the subquery
96546 ** once. */
96547 int regOnce = ++pParse->nMem;
96548 onceAddr = sqlite3VdbeAddOp1(v, OP_Once, regOnce);
96549 }
96550 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
96551 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
96552 sqlite3Select(pParse, pSub, &dest);
96553 pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
96554 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
96555 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
96556 VdbeComment((v, "end %s", pItem->pTab->zName));
96557 sqlite3VdbeChangeP1(v, topAddr, retAddr);
96558
96559 }
96560 if( /*pParse->nErr ||*/ db->mallocFailed ){
96561 goto select_end;
96562 }
96563 pParse->nHeight -= sqlite3SelectExprHeight(p);
@@ -96848,10 +98473,11 @@
96848 pParse->nMem += pGroupBy->nExpr;
96849 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
96850 VdbeComment((v, "clear abort flag"));
96851 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
96852 VdbeComment((v, "indicate accumulator empty"));
 
96853
96854 /* Begin a loop that will extract all source rows in GROUP BY order.
96855 ** This might involve two separate loops with an OP_Sort in between, or
96856 ** it might be a single loop that uses an index to extract information
96857 ** in the right order to begin with.
@@ -97187,102 +98813,102 @@
97187 sqlite3DbFree(db, sAggInfo.aCol);
97188 sqlite3DbFree(db, sAggInfo.aFunc);
97189 return rc;
97190 }
97191
97192 #if defined(SQLITE_DEBUG)
97193 /*
97194 *******************************************************************************
97195 ** The following code is used for testing and debugging only. The code
97196 ** that follows does not appear in normal builds.
97197 **
97198 ** These routines are used to print out the content of all or part of a
97199 ** parse structures such as Select or Expr. Such printouts are useful
97200 ** for helping to understand what is happening inside the code generator
97201 ** during the execution of complex SELECT statements.
97202 **
97203 ** These routine are not called anywhere from within the normal
97204 ** code base. Then are intended to be called from within the debugger
97205 ** or from temporary "printf" statements inserted for debugging.
97206 */
97207 SQLITE_PRIVATE void sqlite3PrintExpr(Expr *p){
97208 if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
97209 sqlite3DebugPrintf("(%s", p->u.zToken);
97210 }else{
97211 sqlite3DebugPrintf("(%d", p->op);
97212 }
97213 if( p->pLeft ){
97214 sqlite3DebugPrintf(" ");
97215 sqlite3PrintExpr(p->pLeft);
97216 }
97217 if( p->pRight ){
97218 sqlite3DebugPrintf(" ");
97219 sqlite3PrintExpr(p->pRight);
97220 }
97221 sqlite3DebugPrintf(")");
97222 }
97223 SQLITE_PRIVATE void sqlite3PrintExprList(ExprList *pList){
97224 int i;
97225 for(i=0; i<pList->nExpr; i++){
97226 sqlite3PrintExpr(pList->a[i].pExpr);
97227 if( i<pList->nExpr-1 ){
97228 sqlite3DebugPrintf(", ");
97229 }
97230 }
97231 }
97232 SQLITE_PRIVATE void sqlite3PrintSelect(Select *p, int indent){
97233 sqlite3DebugPrintf("%*sSELECT(%p) ", indent, "", p);
97234 sqlite3PrintExprList(p->pEList);
97235 sqlite3DebugPrintf("\n");
97236 if( p->pSrc ){
97237 char *zPrefix;
97238 int i;
97239 zPrefix = "FROM";
97240 for(i=0; i<p->pSrc->nSrc; i++){
97241 struct SrcList_item *pItem = &p->pSrc->a[i];
97242 sqlite3DebugPrintf("%*s ", indent+6, zPrefix);
97243 zPrefix = "";
97244 if( pItem->pSelect ){
97245 sqlite3DebugPrintf("(\n");
97246 sqlite3PrintSelect(pItem->pSelect, indent+10);
97247 sqlite3DebugPrintf("%*s)", indent+8, "");
 
97248 }else if( pItem->zName ){
97249 sqlite3DebugPrintf("%s", pItem->zName);
97250 }
97251 if( pItem->pTab ){
97252 sqlite3DebugPrintf("(table: %s)", pItem->pTab->zName);
97253 }
97254 if( pItem->zAlias ){
97255 sqlite3DebugPrintf(" AS %s", pItem->zAlias);
97256 }
97257 if( i<p->pSrc->nSrc-1 ){
97258 sqlite3DebugPrintf(",");
97259 }
97260 sqlite3DebugPrintf("\n");
97261 }
 
97262 }
97263 if( p->pWhere ){
97264 sqlite3DebugPrintf("%*s WHERE ", indent, "");
97265 sqlite3PrintExpr(p->pWhere);
97266 sqlite3DebugPrintf("\n");
97267 }
97268 if( p->pGroupBy ){
97269 sqlite3DebugPrintf("%*s GROUP BY ", indent, "");
97270 sqlite3PrintExprList(p->pGroupBy);
97271 sqlite3DebugPrintf("\n");
97272 }
97273 if( p->pHaving ){
97274 sqlite3DebugPrintf("%*s HAVING ", indent, "");
97275 sqlite3PrintExpr(p->pHaving);
97276 sqlite3DebugPrintf("\n");
97277 }
97278 if( p->pOrderBy ){
97279 sqlite3DebugPrintf("%*s ORDER BY ", indent, "");
97280 sqlite3PrintExprList(p->pOrderBy);
97281 sqlite3DebugPrintf("\n");
 
 
 
 
 
 
 
 
 
 
97282 }
97283 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97284 /* End of the structure debug printing code
97285 *****************************************************************************/
97286 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
97287
97288 /************** End of select.c **********************************************/
@@ -98389,10 +100015,11 @@
98389 if( db->mallocFailed==0 ){
98390 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
98391 }
98392 pProgram->nMem = pSubParse->nMem;
98393 pProgram->nCsr = pSubParse->nTab;
 
98394 pProgram->token = (void *)pTrigger;
98395 pPrg->aColmask[0] = pSubParse->oldmask;
98396 pPrg->aColmask[1] = pSubParse->newmask;
98397 sqlite3VdbeDelete(v);
98398 }
@@ -98736,12 +100363,12 @@
98736
98737 /* Register Allocations */
98738 int regRowCount = 0; /* A count of rows changed */
98739 int regOldRowid; /* The old rowid */
98740 int regNewRowid; /* The new rowid */
98741 int regNew;
98742 int regOld = 0;
98743 int regRowSet = 0; /* Rowset of rows to be updated */
98744
98745 memset(&sContext, 0, sizeof(sContext));
98746 db = pParse->db;
98747 if( pParse->nErr || db->mallocFailed ){
@@ -98886,10 +100513,11 @@
98886 goto update_cleanup;
98887 }
98888 #endif
98889
98890 /* Allocate required registers. */
 
98891 regOldRowid = regNewRowid = ++pParse->nMem;
98892 if( pTrigger || hasFK ){
98893 regOld = pParse->nMem + 1;
98894 pParse->nMem += pTab->nCol;
98895 }
@@ -98920,11 +100548,11 @@
98920 goto update_cleanup;
98921 }
98922
98923 /* Begin the database scan
98924 */
98925 sqlite3VdbeAddOp2(v, OP_Null, 0, regOldRowid);
98926 pWInfo = sqlite3WhereBegin(
98927 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
98928 );
98929 if( pWInfo==0 ) goto update_cleanup;
98930 okOnePass = pWInfo->okOnePass;
@@ -98931,11 +100559,10 @@
98931
98932 /* Remember the rowid of every item to be updated.
98933 */
98934 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
98935 if( !okOnePass ){
98936 regRowSet = ++pParse->nMem;
98937 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
98938 }
98939
98940 /* End the database scan loop.
98941 */
@@ -99035,13 +100662,14 @@
99035 ** be used eliminates some redundant opcodes.
99036 */
99037 newmask = sqlite3TriggerColmask(
99038 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
99039 );
 
99040 for(i=0; i<pTab->nCol; i++){
99041 if( i==pTab->iPKey ){
99042 sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
99043 }else{
99044 j = aXRef[i];
99045 if( j>=0 ){
99046 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
99047 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
@@ -101300,11 +102928,11 @@
101300 if( pTerm->leftCursor==iCur
101301 && (pTerm->prereqRight & notReady)==0
101302 && pTerm->u.leftColumn==iColumn
101303 && (pTerm->eOperator & op)!=0
101304 ){
101305 if( pIdx && pTerm->eOperator!=WO_ISNULL ){
101306 Expr *pX = pTerm->pExpr;
101307 CollSeq *pColl;
101308 char idxaff;
101309 int j;
101310 Parse *pParse = pWC->pParse;
@@ -102701,11 +104329,10 @@
102701 WhereTerm *pTerm; /* A single term of the WHERE clause */
102702 WhereTerm *pWCEnd; /* End of pWC->a[] */
102703 int nByte; /* Byte of memory needed for pIdx */
102704 Index *pIdx; /* Object describing the transient index */
102705 Vdbe *v; /* Prepared statement under construction */
102706 int regIsInit; /* Register set by initialization */
102707 int addrInit; /* Address of the initialization bypass jump */
102708 Table *pTable; /* The table being indexed */
102709 KeyInfo *pKeyinfo; /* Key information for the index */
102710 int addrTop; /* Top of the index fill loop */
102711 int regRecord; /* Register holding an index record */
@@ -102718,12 +104345,11 @@
102718
102719 /* Generate code to skip over the creation and initialization of the
102720 ** transient index on 2nd and subsequent iterations of the loop. */
102721 v = pParse->pVdbe;
102722 assert( v!=0 );
102723 regIsInit = ++pParse->nMem;
102724 addrInit = sqlite3VdbeAddOp1(v, OP_Once, regIsInit);
102725
102726 /* Count the number of columns that will be added to the index
102727 ** and used to match WHERE clause constraints */
102728 nColumn = 0;
102729 pTable = pSrc->pTab;
@@ -103748,14 +105374,28 @@
103748 #ifdef SQLITE_ENABLE_STAT3
103749 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103750 #endif
103751 used |= pTerm->prereqRight;
103752 }
103753
103754 /* Determine the value of rangeDiv */
103755 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103756 int j = pProbe->aiColumn[nEq];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103757 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103758 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103759 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103760 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103761 if( pTop ){
@@ -103770,16 +105410,10 @@
103770 used |= pBtm->prereqRight;
103771 testcase( pBtm->pWC!=pWC );
103772 }
103773 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103774 }
103775 }else if( pProbe->onError!=OE_None ){
103776 testcase( wsFlags & WHERE_COLUMN_IN );
103777 testcase( wsFlags & WHERE_COLUMN_NULL );
103778 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
103779 wsFlags |= WHERE_UNIQUE;
103780 }
103781 }
103782
103783 /* If there is an ORDER BY clause and the index being considered will
103784 ** naturally scan rows in the required order, set the appropriate flags
103785 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
@@ -104386,14 +106020,16 @@
104386 explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
104387 }
104388
104389 j = i;
104390 if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
104391 explainAppendTerm(&txt, i++, aCol[aiColumn[j]].zName, ">");
 
104392 }
104393 if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
104394 explainAppendTerm(&txt, i, aCol[aiColumn[j]].zName, "<");
 
104395 }
104396 sqlite3StrAccumAppend(&txt, ")", 1);
104397 return sqlite3StrAccumFinish(&txt);
104398 }
104399
@@ -104747,11 +106383,11 @@
104747 char *zStartAff; /* Affinity for start of range constraint */
104748 char *zEndAff; /* Affinity for end of range constraint */
104749
104750 pIdx = pLevel->plan.u.pIdx;
104751 iIdxCur = pLevel->iIdxCur;
104752 k = pIdx->aiColumn[nEq]; /* Column for inequality constraints */
104753
104754 /* If this loop satisfies a sort order (pOrderBy) request that
104755 ** was passed to this function to implement a "SELECT min(x) ..."
104756 ** query, then the caller will only allow the loop to run for
104757 ** a single iteration. This means that the first row returned
@@ -104793,11 +106429,13 @@
104793
104794 /* If we are doing a reverse order scan on an ascending index, or
104795 ** a forward order scan on a descending index, interchange the
104796 ** start and end terms (pRangeStart and pRangeEnd).
104797 */
104798 if( nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC) ){
 
 
104799 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
104800 }
104801
104802 testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
104803 testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
@@ -107559,11 +109197,10 @@
107559 /* Here code is inserted which will execute if the parser
107560 ** stack every overflows */
107561
107562 UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
107563 sqlite3ErrorMsg(pParse, "parser stack overflow");
107564 pParse->parseError = 1;
107565 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
107566 }
107567
107568 /*
107569 ** Perform a shift action.
@@ -108282,10 +109919,13 @@
108282 break;
108283 case 112: /* cmd ::= select */
108284 {
108285 SelectDest dest = {SRT_Output, 0, 0, 0, 0};
108286 sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
 
 
 
108287 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
108288 }
108289 break;
108290 case 113: /* select ::= oneselect */
108291 {yygotominor.yy387 = yymsp[0].minor.yy387;}
@@ -109160,11 +110800,10 @@
109160 #define TOKEN (yyminor.yy0)
109161
109162 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
109163 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
109164 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
109165 pParse->parseError = 1;
109166 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
109167 }
109168
109169 /*
109170 ** The following is executed when the parser accepts
@@ -109755,11 +111394,11 @@
109755 *tokenType = TK_SPACE;
109756 return i;
109757 }
109758 case '-': {
109759 if( z[1]=='-' ){
109760 /* IMP: R-15891-05542 -- syntax diagram for comments */
109761 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
109762 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
109763 return i;
109764 }
109765 *tokenType = TK_MINUS;
@@ -109788,11 +111427,11 @@
109788 case '/': {
109789 if( z[1]!='*' || z[2]==0 ){
109790 *tokenType = TK_SLASH;
109791 return 1;
109792 }
109793 /* IMP: R-15891-05542 -- syntax diagram for comments */
109794 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
109795 if( c ) i++;
109796 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
109797 return i;
109798 }
@@ -110582,12 +112221,12 @@
110582 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
110583 ** returns an integer equal to SQLITE_VERSION_NUMBER.
110584 */
110585 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
110586
110587 /* IMPLEMENTATION-OF: R-54823-41343 The sqlite3_threadsafe() function returns
110588 ** zero if and only if SQLite was compiled mutexing code omitted due to
110589 ** the SQLITE_THREADSAFE compile-time option being set to 0.
110590 */
110591 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
110592
110593 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
@@ -110772,12 +112411,12 @@
110772 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110773 ** compile-time option.
110774 */
110775 #ifdef SQLITE_EXTRA_INIT
110776 if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110777 int SQLITE_EXTRA_INIT(void);
110778 rc = SQLITE_EXTRA_INIT();
110779 }
110780 #endif
110781
110782 return rc;
110783 }
@@ -110790,10 +112429,14 @@
110790 ** on when SQLite is already shut down. If SQLite is already shut down
110791 ** when this routine is invoked, then this routine is a harmless no-op.
110792 */
110793 SQLITE_API int sqlite3_shutdown(void){
110794 if( sqlite3GlobalConfig.isInit ){
 
 
 
 
110795 sqlite3_os_end();
110796 sqlite3_reset_auto_extension();
110797 sqlite3GlobalConfig.isInit = 0;
110798 }
110799 if( sqlite3GlobalConfig.isPCacheInit ){
@@ -110898,20 +112541,29 @@
110898 sqlite3GlobalConfig.nPage = va_arg(ap, int);
110899 break;
110900 }
110901
110902 case SQLITE_CONFIG_PCACHE: {
110903 /* Specify an alternative page cache implementation */
110904 sqlite3GlobalConfig.pcache = *va_arg(ap, sqlite3_pcache_methods*);
110905 break;
110906 }
110907
110908 case SQLITE_CONFIG_GETPCACHE: {
110909 if( sqlite3GlobalConfig.pcache.xInit==0 ){
 
 
 
 
 
 
 
 
 
 
 
110910 sqlite3PCacheSetDefault();
110911 }
110912 *va_arg(ap, sqlite3_pcache_methods*) = sqlite3GlobalConfig.pcache;
110913 break;
110914 }
110915
110916 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
110917 case SQLITE_CONFIG_HEAP: {
@@ -111006,25 +112658,25 @@
111006 ** both at the same time.
111007 */
111008 if( db->lookaside.bMalloced ){
111009 sqlite3_free(db->lookaside.pStart);
111010 }
111011 /* The size of a lookaside slot needs to be larger than a pointer
111012 ** to be useful.
111013 */
 
111014 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
111015 if( cnt<0 ) cnt = 0;
111016 if( sz==0 || cnt==0 ){
111017 sz = 0;
111018 pStart = 0;
111019 }else if( pBuf==0 ){
111020 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
111021 sqlite3BeginBenignMalloc();
111022 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
111023 sqlite3EndBenignMalloc();
 
111024 }else{
111025 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
111026 pStart = pBuf;
111027 }
111028 db->lookaside.pStart = pStart;
111029 db->lookaside.pFree = 0;
111030 db->lookaside.sz = (u16)sz;
@@ -111053,10 +112705,30 @@
111053 ** Return the mutex associated with a database connection.
111054 */
111055 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
111056 return db->mutex;
111057 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111058
111059 /*
111060 ** Configuration settings for an individual database connection
111061 */
111062 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
@@ -112161,11 +113833,10 @@
112161 */
112162 static int createCollation(
112163 sqlite3* db,
112164 const char *zName,
112165 u8 enc,
112166 u8 collType,
112167 void* pCtx,
112168 int(*xCompare)(void*,int,const void*,int,const void*),
112169 void(*xDel)(void*)
112170 ){
112171 CollSeq *pColl;
@@ -112226,11 +113897,10 @@
112226 if( pColl==0 ) return SQLITE_NOMEM;
112227 pColl->xCmp = xCompare;
112228 pColl->pUser = pCtx;
112229 pColl->xDel = xDel;
112230 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
112231 pColl->type = collType;
112232 sqlite3Error(db, SQLITE_OK, 0);
112233 return SQLITE_OK;
112234 }
112235
112236
@@ -112687,27 +114357,22 @@
112687
112688 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
112689 ** and UTF-16, so add a version for each to avoid any unnecessary
112690 ** conversions. The only error that can occur here is a malloc() failure.
112691 */
112692 createCollation(db, "BINARY", SQLITE_UTF8, SQLITE_COLL_BINARY, 0,
112693 binCollFunc, 0);
112694 createCollation(db, "BINARY", SQLITE_UTF16BE, SQLITE_COLL_BINARY, 0,
112695 binCollFunc, 0);
112696 createCollation(db, "BINARY", SQLITE_UTF16LE, SQLITE_COLL_BINARY, 0,
112697 binCollFunc, 0);
112698 createCollation(db, "RTRIM", SQLITE_UTF8, SQLITE_COLL_USER, (void*)1,
112699 binCollFunc, 0);
112700 if( db->mallocFailed ){
112701 goto opendb_out;
112702 }
112703 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
112704 assert( db->pDfltColl!=0 );
112705
112706 /* Also add a UTF-8 case-insensitive collation sequence. */
112707 createCollation(db, "NOCASE", SQLITE_UTF8, SQLITE_COLL_NOCASE, 0,
112708 nocaseCollatingFunc, 0);
112709
112710 /* Parse the filename/URI argument. */
112711 db->openFlags = flags;
112712 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
112713 if( rc!=SQLITE_OK ){
@@ -112752,14 +114417,17 @@
112752 sqlite3RegisterBuiltinFunctions(db);
112753
112754 /* Load automatic extensions - extensions that have been registered
112755 ** using the sqlite3_automatic_extension() API.
112756 */
112757 sqlite3AutoLoadExtensions(db);
112758 rc = sqlite3_errcode(db);
112759 if( rc!=SQLITE_OK ){
112760 goto opendb_out;
 
 
 
 
112761 }
112762
112763 #ifdef SQLITE_ENABLE_FTS1
112764 if( !db->mallocFailed ){
112765 extern int sqlite3Fts1Init(sqlite3*);
@@ -112896,11 +114564,11 @@
112896 int(*xCompare)(void*,int,const void*,int,const void*)
112897 ){
112898 int rc;
112899 sqlite3_mutex_enter(db->mutex);
112900 assert( !db->mallocFailed );
112901 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
112902 rc = sqlite3ApiExit(db, rc);
112903 sqlite3_mutex_leave(db->mutex);
112904 return rc;
112905 }
112906
@@ -112916,11 +114584,11 @@
112916 void(*xDel)(void*)
112917 ){
112918 int rc;
112919 sqlite3_mutex_enter(db->mutex);
112920 assert( !db->mallocFailed );
112921 rc = createCollation(db, zName, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, xDel);
112922 rc = sqlite3ApiExit(db, rc);
112923 sqlite3_mutex_leave(db->mutex);
112924 return rc;
112925 }
112926
@@ -112939,11 +114607,11 @@
112939 char *zName8;
112940 sqlite3_mutex_enter(db->mutex);
112941 assert( !db->mallocFailed );
112942 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
112943 if( zName8 ){
112944 rc = createCollation(db, zName8, (u8)enc, SQLITE_COLL_USER, pCtx, xCompare, 0);
112945 sqlite3DbFree(db, zName8);
112946 }
112947 rc = sqlite3ApiExit(db, rc);
112948 sqlite3_mutex_leave(db->mutex);
112949 return rc;
@@ -113422,19 +115090,10 @@
113422 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
113423 break;
113424 }
113425 #endif
113426
113427 /* sqlite3_test_control(SQLITE_TESTCTRL_PGHDRSZ)
113428 **
113429 ** Return the size of a pcache header in bytes.
113430 */
113431 case SQLITE_TESTCTRL_PGHDRSZ: {
113432 rc = sizeof(PgHdr);
113433 break;
113434 }
113435
113436 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
113437 **
113438 ** Pass pFree into sqlite3ScratchFree().
113439 ** If sz>0 then allocate a scratch buffer into pNew.
113440 */
@@ -113458,10 +115117,26 @@
113458 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
113459 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
113460 break;
113461 }
113462
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113463 }
113464 va_end(ap);
113465 #endif /* SQLITE_OMIT_BUILTIN_TEST */
113466 return rc;
113467 }
@@ -113476,16 +115151,55 @@
113476 ** query parameter we seek. This routine returns the value of the zParam
113477 ** parameter if it exists. If the parameter does not exist, this routine
113478 ** returns a NULL pointer.
113479 */
113480 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
 
113481 zFilename += sqlite3Strlen30(zFilename) + 1;
113482 while( zFilename[0] ){
113483 int x = strcmp(zFilename, zParam);
113484 zFilename += sqlite3Strlen30(zFilename) + 1;
113485 if( x==0 ) return zFilename;
113486 zFilename += sqlite3Strlen30(zFilename) + 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113487 }
113488 return 0;
113489 }
113490
113491 /************** End of main.c ************************************************/
@@ -115329,10 +117043,11 @@
115329 if( *pRc==SQLITE_OK ){
115330 va_list ap;
115331 char *z;
115332 va_start(ap, zFormat);
115333 z = sqlite3_vmprintf(zFormat, ap);
 
115334 if( z && *pz ){
115335 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
115336 sqlite3_free(z);
115337 z = z2;
115338 }
@@ -121596,11 +123311,11 @@
121596 typedef struct porter_tokenizer {
121597 sqlite3_tokenizer base; /* Base class */
121598 } porter_tokenizer;
121599
121600 /*
121601 ** Class derived from sqlit3_tokenizer_cursor
121602 */
121603 typedef struct porter_tokenizer_cursor {
121604 sqlite3_tokenizer_cursor base;
121605 const char *zInput; /* input we are tokenizing */
121606 int nInput; /* size of the input */
@@ -124314,11 +126029,10 @@
124314 sqlite3_int64 iEndBlock, /* Final block of segment */
124315 const char *zRoot, /* Buffer containing root node */
124316 int nRoot, /* Size of buffer containing root node */
124317 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
124318 ){
124319 int rc = SQLITE_OK; /* Return code */
124320 Fts3SegReader *pReader; /* Newly allocated SegReader object */
124321 int nExtra = 0; /* Bytes to allocate segment root node */
124322
124323 assert( iStartLeaf<=iEndLeaf );
124324 if( iStartLeaf==0 ){
@@ -124342,17 +126056,12 @@
124342 memcpy(pReader->aNode, zRoot, nRoot);
124343 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
124344 }else{
124345 pReader->iCurrentBlock = iStartLeaf-1;
124346 }
124347
124348 if( rc==SQLITE_OK ){
124349 *ppReader = pReader;
124350 }else{
124351 sqlite3Fts3SegReaderFree(pReader);
124352 }
124353 return rc;
124354 }
124355
124356 /*
124357 ** This is a comparison function used as a qsort() callback when sorting
124358 ** an array of pending terms by term. This occurs as part of flushing
@@ -124398,19 +126107,19 @@
124398 int nTerm, /* Size of buffer zTerm */
124399 int bPrefix, /* True for a prefix iterator */
124400 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
124401 ){
124402 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
 
124403 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
124404 int nElem = 0; /* Size of array at aElem */
124405 int rc = SQLITE_OK; /* Return Code */
124406 Fts3Hash *pHash;
124407
124408 pHash = &p->aIndex[iIndex].hPending;
124409 if( bPrefix ){
124410 int nAlloc = 0; /* Size of allocated array at aElem */
124411 Fts3HashElem *pE = 0; /* Iterator variable */
124412
124413 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
124414 char *zKey = (char *)fts3HashKey(pE);
124415 int nKey = fts3HashKeysize(pE);
124416 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
@@ -124440,12 +126149,17 @@
124440 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
124441 }
124442
124443 }else{
124444 /* The query is a simple term lookup that matches at most one term in
124445 ** the index. All that is required is a straight hash-lookup. */
124446 Fts3HashElem *pE = fts3HashFindElem(pHash, zTerm, nTerm);
 
 
 
 
 
124447 if( pE ){
124448 aElem = &pE;
124449 nElem = 1;
124450 }
124451 }
@@ -129021,11 +130735,11 @@
129021 RtreeMatchArg *p;
129022 sqlite3_rtree_geometry *pGeom;
129023 int nBlob;
129024
129025 /* Check that value is actually a blob. */
129026 if( !sqlite3_value_type(pValue)==SQLITE_BLOB ) return SQLITE_ERROR;
129027
129028 /* Check that the blob is roughly the right size. */
129029 nBlob = sqlite3_value_bytes(pValue);
129030 if( nBlob<(int)sizeof(RtreeMatchArg)
129031 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
129032
--- 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.10. 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.
@@ -363,10 +363,18 @@
363 #else
364 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
365 #endif
366 #endif
367
368 /*
369 ** Powersafe overwrite is on by default. But can be turned off using
370 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
371 */
372 #ifndef SQLITE_POWERSAFE_OVERWRITE
373 # define SQLITE_POWERSAFE_OVERWRITE 1
374 #endif
375
376 /*
377 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
378 ** It determines whether or not the features related to
379 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
380 ** be overridden at runtime using the sqlite3_config() API.
@@ -647,13 +655,13 @@
655 **
656 ** See also: [sqlite3_libversion()],
657 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
658 ** [sqlite_version()] and [sqlite_source_id()].
659 */
660 #define SQLITE_VERSION "3.7.10"
661 #define SQLITE_VERSION_NUMBER 3007010
662 #define SQLITE_SOURCE_ID "2012-01-11 16:16:08 9e31a275ef494ea8713a1d60a15b84157e57c3ff"
663
664 /*
665 ** CAPI3REF: Run-Time Library Version Numbers
666 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
667 **
@@ -717,11 +725,11 @@
725
726 /*
727 ** CAPI3REF: Test To See If The Library Is Threadsafe
728 **
729 ** ^The sqlite3_threadsafe() function returns zero if and only if
730 ** SQLite was compiled with mutexing code omitted due to the
731 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
732 **
733 ** SQLite can be compiled with or without mutexes. When
734 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
735 ** are enabled and SQLite is threadsafe. When the
@@ -911,11 +919,11 @@
919 ** CAPI3REF: Result Codes
920 ** KEYWORDS: SQLITE_OK {error code} {error codes}
921 ** KEYWORDS: {result code} {result codes}
922 **
923 ** Many SQLite functions return an integer result code from the set shown
924 ** here in order to indicate success or failure.
925 **
926 ** New error codes may be added in future versions of SQLite.
927 **
928 ** See also: [SQLITE_IOERR_READ | extended result codes],
929 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -1049,11 +1057,15 @@
1057 ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
1058 ** that when data is appended to a file, the data is appended
1059 ** first then the size of the file is extended, never the other
1060 ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
1061 ** information is written to disk in the same order as calls
1062 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
1063 ** after reboot following a crash or power loss, the only bytes in a
1064 ** file that were written at the application level might have changed
1065 ** and that adjacent bytes, even bytes within the same sector are
1066 ** guaranteed to be unchanged.
1067 */
1068 #define SQLITE_IOCAP_ATOMIC 0x00000001
1069 #define SQLITE_IOCAP_ATOMIC512 0x00000002
1070 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
1071 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -1063,10 +1075,11 @@
1075 #define SQLITE_IOCAP_ATOMIC32K 0x00000080
1076 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
1077 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
1078 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
1079 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
1080 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
1081
1082 /*
1083 ** CAPI3REF: File Locking Levels
1084 **
1085 ** SQLite uses one of these integer values as the second
@@ -1284,16 +1297,16 @@
1297 ** opcode as doing so may disrupt the operation of the specialized VFSes
1298 ** that do require it.
1299 **
1300 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
1301 ** retry counts and intervals for certain disk I/O operations for the
1302 ** windows [VFS] in order to provide robustness in the presence of
1303 ** anti-virus programs. By default, the windows VFS will retry file read,
1304 ** file write, and file delete operations up to 10 times, with a delay
1305 ** of 25 milliseconds before the first retry and with the delay increasing
1306 ** by an additional 25 milliseconds with each subsequent retry. This
1307 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
1308 ** to be adjusted. The values are changed for all database connections
1309 ** within the same process. The argument is a pointer to an array of two
1310 ** integers where the first integer i the new retry count and the second
1311 ** integer is the delay. If either integer is negative, then the setting
1312 ** is not changed but instead the prior value of that setting is written
@@ -1311,27 +1324,49 @@
1324 ** in order for the database to be readable. The fourth parameter to
1325 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1326 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1327 ** WAL mode. If the integer is -1, then it is overwritten with the current
1328 ** WAL persistence setting.
1329 **
1330 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
1331 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
1332 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
1333 ** xDeviceCharacteristics methods. The fourth parameter to
1334 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1335 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
1336 ** mode. If the integer is -1, then it is overwritten with the current
1337 ** zero-damage mode setting.
1338 **
1339 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1340 ** a write transaction to indicate that, unless it is rolled back for some
1341 ** reason, the entire database file will be overwritten by the current
1342 ** transaction. This is used by VACUUM operations.
1343 **
1344 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
1345 ** all [VFSes] in the VFS stack. The names are of all VFS shims and the
1346 ** final bottom-level VFS are written into memory obtained from
1347 ** [sqlite3_malloc()] and the result is stored in the char* variable
1348 ** that the fourth parameter of [sqlite3_file_control()] points to.
1349 ** The caller is responsible for freeing the memory when done. As with
1350 ** all file-control actions, there is no guarantee that this will actually
1351 ** do anything. Callers should initialize the char* variable to a NULL
1352 ** pointer in case this file-control is not implemented. This file-control
1353 ** is intended for diagnostic use only.
1354 */
1355 #define SQLITE_FCNTL_LOCKSTATE 1
1356 #define SQLITE_GET_LOCKPROXYFILE 2
1357 #define SQLITE_SET_LOCKPROXYFILE 3
1358 #define SQLITE_LAST_ERRNO 4
1359 #define SQLITE_FCNTL_SIZE_HINT 5
1360 #define SQLITE_FCNTL_CHUNK_SIZE 6
1361 #define SQLITE_FCNTL_FILE_POINTER 7
1362 #define SQLITE_FCNTL_SYNC_OMITTED 8
1363 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
1364 #define SQLITE_FCNTL_PERSIST_WAL 10
1365 #define SQLITE_FCNTL_OVERWRITE 11
1366 #define SQLITE_FCNTL_VFSNAME 12
1367 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1368
1369 /*
1370 ** CAPI3REF: Mutex Handle
1371 **
1372 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1382,11 +1417,11 @@
1417 ** ^SQLite guarantees that the zFilename parameter to xOpen
1418 ** is either a NULL pointer or string obtained
1419 ** from xFullPathname() with an optional suffix added.
1420 ** ^If a suffix is added to the zFilename parameter, it will
1421 ** consist of a single "-" character followed by no more than
1422 ** 11 alphanumeric and/or "-" characters.
1423 ** ^SQLite further guarantees that
1424 ** the string will be valid and unchanged until xClose() is
1425 ** called. Because of the previous sentence,
1426 ** the [sqlite3_file] can safely store a pointer to the
1427 ** filename if it needs to remember the filename for some reason.
@@ -1913,11 +1948,11 @@
1948 **
1949 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1950 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1951 ** the database page cache with the default page cache implementation.
1952 ** This configuration should not be used if an application-define page
1953 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1954 ** There are three arguments to this option: A pointer to 8-byte aligned
1955 ** memory, the size of each page buffer (sz), and the number of pages (N).
1956 ** The sz argument should be the size of the largest database page
1957 ** (a power of two between 512 and 32768) plus a little extra for each
1958 ** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1982,19 +2017,19 @@
2017 ** slots allocated to each database connection.)^ ^(This option sets the
2018 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
2019 ** verb to [sqlite3_db_config()] can be used to change the lookaside
2020 ** configuration on individual connections.)^ </dd>
2021 **
2022 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
2023 ** <dd> ^(This option takes a single argument which is a pointer to
2024 ** an [sqlite3_pcache_methods2] object. This object specifies the interface
2025 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
2026 ** object and uses it for page cache memory allocations.</dd>
2027 **
2028 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
2029 ** <dd> ^(This option takes a single argument which is a pointer to an
2030 ** [sqlite3_pcache_methods2] object. SQLite copies of the current
2031 ** page cache implementation into that object.)^ </dd>
2032 **
2033 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
2034 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
2035 ** function with a call signature of void(*)(void*,int,const char*),
@@ -2023,10 +2058,15 @@
2058 ** connection is opened. If it is globally disabled, filenames are
2059 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
2060 ** database connection is opened. By default, URI handling is globally
2061 ** disabled. The default value may be changed by compiling with the
2062 ** [SQLITE_USE_URI] symbol defined.
2063 **
2064 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2065 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
2066 ** <dd> These options are obsolete and should not be used by new code.
2067 ** They are retained for backwards compatibility but are now no-ops.
2068 ** </dl>
2069 */
2070 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2071 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2072 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2038,14 +2078,16 @@
2078 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
2079 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
2080 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
2081 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
2082 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
2083 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
2084 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
2085 #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
2086 #define SQLITE_CONFIG_URI 17 /* int */
2087 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2088 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
2089
2090 /*
2091 ** CAPI3REF: Database Connection Configuration Options
2092 **
2093 ** These constants are the available integer configuration options that
@@ -2526,11 +2568,11 @@
2568 ** These routines all implement some additional formatting
2569 ** options that are useful for constructing SQL statements.
2570 ** All of the usual printf() formatting options apply. In addition, there
2571 ** is are "%q", "%Q", and "%z" options.
2572 **
2573 ** ^(The %q option works like %s in that it substitutes a nul-terminated
2574 ** string from the argument list. But %q also doubles every '\'' character.
2575 ** %q is designed for use inside a string literal.)^ By doubling each '\''
2576 ** character it escapes that character and allows it to be inserted into
2577 ** the string.
2578 **
@@ -3134,25 +3176,41 @@
3176 );
3177
3178 /*
3179 ** CAPI3REF: Obtain Values For URI Parameters
3180 **
3181 ** These are utility routines, useful to VFS implementations, that check
3182 ** to see if a database file was a URI that contained a specific query
3183 ** parameter, and if so obtains the value of that query parameter.
3184 **
3185 ** If F is the filename pointer passed into the xOpen() method of a VFS
3186 ** implementation and P is the name of the query parameter, then
3187 ** sqlite3_uri_parameter(F,P) returns the value of the P
3188 ** parameter if it exists or a NULL pointer if P does not appear as a
3189 ** query parameter on F. If P is a query parameter of F
3190 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
3191 ** a pointer to an empty string.
3192 **
3193 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
3194 ** parameter and returns true (1) or false (0) according to the value
3195 ** of P. The value of P is true if it is "yes" or "true" or "on" or
3196 ** a non-zero number and is false otherwise. If P is not a query parameter
3197 ** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
3198 **
3199 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
3200 ** 64-bit signed integer and returns that integer, or D if P does not
3201 ** exist. If the value of P is something other than an integer, then
3202 ** zero is returned.
3203 **
3204 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
3205 ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
3206 ** is not a pathname pointer that SQLite passed into the xOpen VFS method,
3207 ** then the behavior of this routine is undefined and probably undesirable.
3208 */
3209 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
3210 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
3211 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
3212
3213
3214 /*
3215 ** CAPI3REF: Error Codes And Messages
3216 **
@@ -3470,10 +3528,29 @@
3528 ** change the configuration of a database connection, they do not make
3529 ** changes to the content of the database files on disk.
3530 */
3531 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3532
3533 /*
3534 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
3535 **
3536 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
3537 ** [prepared statement] S has been stepped at least once using
3538 ** [sqlite3_step(S)] but has not run to completion and/or has not
3539 ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
3540 ** interface returns false if S is a NULL pointer. If S is not a
3541 ** NULL pointer and is not a pointer to a valid [prepared statement]
3542 ** object, then the behavior is undefined and probably undesirable.
3543 **
3544 ** This interface can be used in combination [sqlite3_next_stmt()]
3545 ** to locate all prepared statements associated with a database
3546 ** connection that are in need of being reset. This can be used,
3547 ** for example, in diagnostic routines to search for prepared
3548 ** statements that are holding a transaction open.
3549 */
3550 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3551
3552 /*
3553 ** CAPI3REF: Dynamically Typed Value Object
3554 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3555 **
3556 ** SQLite uses the sqlite3_value object to represent all values
@@ -4011,11 +4088,11 @@
4088 ** of the string. ^For clarity: the values returned by
4089 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
4090 ** bytes in the string, not the number of characters.
4091 **
4092 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
4093 ** even empty strings, are always zero-terminated. ^The return
4094 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
4095 **
4096 ** ^The object returned by [sqlite3_column_value()] is an
4097 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
4098 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4911,10 +4988,26 @@
4988 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4989 ** create the statement in the first place.
4990 */
4991 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4992
4993 /*
4994 ** CAPI3REF: Return The Filename For A Database Connection
4995 **
4996 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4997 ** associated with database N of connection D. ^The main database file
4998 ** has the name "main". If there is no attached database N on the database
4999 ** connection D, or if database N is a temporary or in-memory database, then
5000 ** a NULL pointer is returned.
5001 **
5002 ** ^The filename returned by this function is the output of the
5003 ** xFullPathname method of the [VFS]. ^In other words, the filename
5004 ** will be an absolute pathname, even if the filename used
5005 ** to open the database originally was a URI or relative pathname.
5006 */
5007 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
5008
5009 /*
5010 ** CAPI3REF: Find the next prepared statement
5011 **
5012 ** ^This interface returns a pointer to the next [prepared statement] after
5013 ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4946,17 +5039,19 @@
5039 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
5040 ** return the P argument from the previous call of the same function
5041 ** on the same [database connection] D, or NULL for
5042 ** the first call for each function on D.
5043 **
5044 ** The commit and rollback hook callbacks are not reentrant.
5045 ** The callback implementation must not do anything that will modify
5046 ** the database connection that invoked the callback. Any actions
5047 ** to modify the database connection must be deferred until after the
5048 ** completion of the [sqlite3_step()] call that triggered the commit
5049 ** or rollback hook in the first place.
5050 ** Note that running any other SQL statements, including SELECT statements,
5051 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
5052 ** the database connections for the meaning of "modify" in this paragraph.
5053 **
5054 ** ^Registering a NULL function disables the callback.
5055 **
5056 ** ^When the commit hook callback routine returns zero, the [COMMIT]
5057 ** operation is allowed to continue normally. ^If the commit hook
@@ -5065,13 +5160,28 @@
5160 ** pages to improve performance is an example of non-essential memory.
5161 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
5162 ** which might be more or less than the amount requested.
5163 ** ^The sqlite3_release_memory() routine is a no-op returning zero
5164 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5165 **
5166 ** See also: [sqlite3_db_release_memory()]
5167 */
5168 SQLITE_API int sqlite3_release_memory(int);
5169
5170 /*
5171 ** CAPI3REF: Free Memory Used By A Database Connection
5172 **
5173 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
5174 ** memory as possible from database connection D. Unlike the
5175 ** [sqlite3_release_memory()] interface, this interface is effect even
5176 ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
5177 ** omitted.
5178 **
5179 ** See also: [sqlite3_release_memory()]
5180 */
5181 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
5182
5183 /*
5184 ** CAPI3REF: Impose A Limit On Heap Size
5185 **
5186 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5187 ** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -5082,11 +5192,12 @@
5192 ** below the limit, it will exceed the limit rather than generate
5193 ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
5194 ** is advisory only.
5195 **
5196 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
5197 ** the soft heap limit prior to the call, or negative in the case of an
5198 ** error. ^If the argument N is negative
5199 ** then no change is made to the soft heap limit. Hence, the current
5200 ** size of the soft heap limit can be determined by invoking
5201 ** sqlite3_soft_heap_limit64() with a negative argument.
5202 **
5203 ** ^If the argument N is zero then the soft heap limit is disabled.
@@ -5098,11 +5209,11 @@
5209 ** <li> The soft heap limit is set to zero.
5210 ** <li> Memory accounting is disabled using a combination of the
5211 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5212 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5213 ** <li> An alternative page cache implementation is specified using
5214 ** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
5215 ** <li> The page cache allocates from its own memory pool supplied
5216 ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5217 ** from the heap.
5218 ** </ul>)^
5219 **
@@ -5840,19 +5951,19 @@
5951 ** is selected automatically at compile-time. ^(The following
5952 ** implementations are available in the SQLite core:
5953 **
5954 ** <ul>
5955 ** <li> SQLITE_MUTEX_OS2
5956 ** <li> SQLITE_MUTEX_PTHREADS
5957 ** <li> SQLITE_MUTEX_W32
5958 ** <li> SQLITE_MUTEX_NOOP
5959 ** </ul>)^
5960 **
5961 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5962 ** that does no real locking and is appropriate for use in
5963 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5964 ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
5965 ** are appropriate for use on OS/2, Unix, and Windows.
5966 **
5967 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5968 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5969 ** implementation is included with the library. In this case the
@@ -6038,11 +6149,11 @@
6149 ** defined and if NDEBUG is not defined.
6150 **
6151 ** ^These routines should return true if the mutex in their argument
6152 ** is held or not held, respectively, by the calling thread.
6153 **
6154 ** ^The implementation is not required to provide versions of these
6155 ** routines that actually work. If the implementation does not provide working
6156 ** versions of these routines, it should at least provide stubs that always
6157 ** return true so that one does not get spurious assertion failures.
6158 **
6159 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -6166,13 +6277,13 @@
6277 #define SQLITE_TESTCTRL_ASSERT 12
6278 #define SQLITE_TESTCTRL_ALWAYS 13
6279 #define SQLITE_TESTCTRL_RESERVE 14
6280 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
6281 #define SQLITE_TESTCTRL_ISKEYWORD 16
6282 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
6283 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
6284 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
6285 #define SQLITE_TESTCTRL_LAST 19
6286
6287 /*
6288 ** CAPI3REF: SQLite Runtime Status
6289 **
@@ -6471,21 +6582,37 @@
6582 ** the pluggable module. The SQLite core has no knowledge of
6583 ** its size or internal structure and never deals with the
6584 ** sqlite3_pcache object except by holding and passing pointers
6585 ** to the object.
6586 **
6587 ** See [sqlite3_pcache_methods2] for additional information.
6588 */
6589 typedef struct sqlite3_pcache sqlite3_pcache;
6590
6591 /*
6592 ** CAPI3REF: Custom Page Cache Object
6593 **
6594 ** The sqlite3_pcache_page object represents a single page in the
6595 ** page cache. The page cache will allocate instances of this
6596 ** object. Various methods of the page cache use pointers to instances
6597 ** of this object as parameters or as their return value.
6598 **
6599 ** See [sqlite3_pcache_methods2] for additional information.
6600 */
6601 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6602 struct sqlite3_pcache_page {
6603 void *pBuf; /* The content of the page */
6604 void *pExtra; /* Extra information associated with the page */
6605 };
6606
6607 /*
6608 ** CAPI3REF: Application Defined Page Cache.
6609 ** KEYWORDS: {page cache}
6610 **
6611 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
6612 ** register an alternative page cache implementation by passing in an
6613 ** instance of the sqlite3_pcache_methods2 structure.)^
6614 ** In many applications, most of the heap memory allocated by
6615 ** SQLite is used for the page cache.
6616 ** By implementing a
6617 ** custom page cache using this API, an application can better control
6618 ** the amount of memory consumed by SQLite, the way in which
@@ -6495,20 +6622,20 @@
6622 **
6623 ** The alternative page cache mechanism is an
6624 ** extreme measure that is only needed by the most demanding applications.
6625 ** The built-in page cache is recommended for most uses.
6626 **
6627 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
6628 ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
6629 ** the application may discard the parameter after the call to
6630 ** [sqlite3_config()] returns.)^
6631 **
6632 ** [[the xInit() page cache method]]
6633 ** ^(The xInit() method is called once for each effective
6634 ** call to [sqlite3_initialize()])^
6635 ** (usually only once during the lifetime of the process). ^(The xInit()
6636 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
6637 ** The intent of the xInit() method is to set up global data structures
6638 ** required by the custom page cache implementation.
6639 ** ^(If the xInit() method is NULL, then the
6640 ** built-in default page cache is used instead of the application defined
6641 ** page cache.)^
@@ -6531,21 +6658,19 @@
6658 ** [[the xCreate() page cache methods]]
6659 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6660 ** SQLite will typically create one cache instance for each open database file,
6661 ** though this is not guaranteed. ^The
6662 ** first parameter, szPage, is the size in bytes of the pages that must
6663 ** be allocated by the cache. ^szPage will always a power of two. ^The
6664 ** second parameter szExtra is a number of bytes of extra storage
6665 ** associated with each page cache entry. ^The szExtra parameter will
6666 ** a number less than 250. SQLite will use the
6667 ** extra szExtra bytes on each page to store metadata about the underlying
6668 ** database page on disk. The value passed into szExtra depends
6669 ** on the SQLite version, the target platform, and how SQLite was compiled.
6670 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6671 ** created will be used to cache database pages of a file stored on disk, or
 
 
 
6672 ** false if it is used for an in-memory database. The cache implementation
6673 ** does not have to do anything special based with the value of bPurgeable;
6674 ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
6675 ** never invoke xUnpin() except to deliberately delete a page.
6676 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6565,15 +6690,20 @@
6690 ** The xPagecount() method must return the number of pages currently
6691 ** stored in the cache, both pinned and unpinned.
6692 **
6693 ** [[the xFetch() page cache methods]]
6694 ** The xFetch() method locates a page in the cache and returns a pointer to
6695 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6696 ** The pBuf element of the returned sqlite3_pcache_page object will be a
6697 ** pointer to a buffer of szPage bytes used to store the content of a
6698 ** single database page. The pExtra element of sqlite3_pcache_page will be
6699 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
6700 ** for each entry in the page cache.
6701 **
6702 ** The page to be fetched is determined by the key. ^The minimum key value
6703 ** is 1. After it has been retrieved using xFetch, the page is considered
6704 ** to be "pinned".
6705 **
6706 ** If the requested page is already in the page cache, then the page cache
6707 ** implementation must return a pointer to the page buffer with its content
6708 ** intact. If the requested page is not already in the cache, then the
6709 ** cache implementation should use the value of the createFlag
@@ -6622,12 +6752,41 @@
6752 **
6753 ** [[the xDestroy() page cache method]]
6754 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6755 ** All resources associated with the specified cache should be freed. ^After
6756 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6757 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
6758 ** functions.
6759 **
6760 ** [[the xShrink() page cache method]]
6761 ** ^SQLite invokes the xShrink() method when it wants the page cache to
6762 ** free up as much of heap memory as possible. The page cache implementation
6763 ** is not obligated to free any memory, but well-behaved implementions should
6764 ** do their best.
6765 */
6766 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6767 struct sqlite3_pcache_methods2 {
6768 int iVersion;
6769 void *pArg;
6770 int (*xInit)(void*);
6771 void (*xShutdown)(void*);
6772 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6773 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6774 int (*xPagecount)(sqlite3_pcache*);
6775 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6776 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6777 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6778 unsigned oldKey, unsigned newKey);
6779 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6780 void (*xDestroy)(sqlite3_pcache*);
6781 void (*xShrink)(sqlite3_pcache*);
6782 };
6783
6784 /*
6785 ** This is the obsolete pcache_methods object that has now been replaced
6786 ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
6787 ** retained in the header file for backwards compatibility only.
6788 */
6789 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6790 struct sqlite3_pcache_methods {
6791 void *pArg;
6792 int (*xInit)(void*);
@@ -6639,10 +6798,11 @@
6798 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6799 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6800 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6801 void (*xDestroy)(sqlite3_pcache*);
6802 };
6803
6804
6805 /*
6806 ** CAPI3REF: Online Backup Object
6807 **
6808 ** The sqlite3_backup object records state information about an ongoing
@@ -7643,11 +7803,11 @@
7803 ** the default file format for new databases and the maximum file format
7804 ** that the library can read.
7805 */
7806 #define SQLITE_MAX_FILE_FORMAT 4
7807 #ifndef SQLITE_DEFAULT_FILE_FORMAT
7808 # define SQLITE_DEFAULT_FILE_FORMAT 4
7809 #endif
7810
7811 /*
7812 ** Determine whether triggers are recursive by default. This can be
7813 ** changed at run-time using a pragma.
@@ -8283,10 +8443,11 @@
8443 struct SubProgram {
8444 VdbeOp *aOp; /* Array of opcodes for sub-program */
8445 int nOp; /* Elements in aOp[] */
8446 int nMem; /* Number of memory cells required */
8447 int nCsr; /* Number of cursors required */
8448 int nOnce; /* Number of OP_Once instructions */
8449 void *token; /* id that may be used to recursive triggers */
8450 SubProgram *pNext; /* Next sub-program already visited */
8451 };
8452
8453 /*
@@ -8529,14 +8690,14 @@
8690 #define OPFLG_IN2 0x0008 /* in2: P2 is an input */
8691 #define OPFLG_IN3 0x0010 /* in3: P3 is an input */
8692 #define OPFLG_OUT2 0x0020 /* out2: P2 is an output */
8693 #define OPFLG_OUT3 0x0040 /* out3: P3 is an output */
8694 #define OPFLG_INITIALIZER {\
8695 /* 0 */ 0x00, 0x01, 0x01, 0x04, 0x04, 0x10, 0x00, 0x02,\
8696 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8697 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8698 /* 24 */ 0x00, 0x01, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00,\
8699 /* 32 */ 0x02, 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00,\
8700 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11,\
8701 /* 48 */ 0x11, 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02,\
8702 /* 56 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8703 /* 64 */ 0x00, 0x02, 0x00, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
@@ -8732,10 +8893,11 @@
8893 /* Functions used to configure a Pager object. */
8894 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
8895 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
8896 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
8897 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
8898 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
8899 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
8900 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
8901 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
8902 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
8903 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
@@ -8840,11 +9002,12 @@
9002 /*
9003 ** Every page in the cache is controlled by an instance of the following
9004 ** structure.
9005 */
9006 struct PgHdr {
9007 sqlite3_pcache_page *pPage; /* Pcache object page handle */
9008 void *pData; /* Page data */
9009 void *pExtra; /* Extra content */
9010 PgHdr *pDirty; /* Transient list of dirty pages */
9011 Pgno pgno; /* Page number for this page */
9012 Pager *pPager; /* The pager this page is part of */
9013 #ifdef SQLITE_CHECK_PAGES
@@ -8958,10 +9121,13 @@
9121 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
9122 #ifdef SQLITE_TEST
9123 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
9124 #endif
9125
9126 /* Free up as much memory as possible from the page cache */
9127 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
9128
9129 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
9130 /* Try to return memory used by the pcache module to the main memory heap */
9131 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
9132 #endif
9133
@@ -9042,21 +9208,10 @@
9208 #else
9209 # ifndef SQLITE_OS_WIN
9210 # define SQLITE_OS_WIN 0
9211 # endif
9212 #endif
 
 
 
 
 
 
 
 
 
 
 
9213
9214 /*
9215 ** Define the maximum size of a temporary filename
9216 */
9217 #if SQLITE_OS_WIN
@@ -9078,10 +9233,29 @@
9233 # define SQLITE_TEMPNAME_SIZE (CCHMAXPATHCOMP)
9234 #else
9235 # define SQLITE_TEMPNAME_SIZE 200
9236 #endif
9237
9238 /*
9239 ** Determine if we are dealing with Windows NT.
9240 */
9241 #if defined(_WIN32_WINNT)
9242 # define SQLITE_OS_WINNT 1
9243 #else
9244 # define SQLITE_OS_WINNT 0
9245 #endif
9246
9247 /*
9248 ** Determine if we are dealing with WindowsCE - which has a much
9249 ** reduced API.
9250 */
9251 #if defined(_WIN32_WCE)
9252 # define SQLITE_OS_WINCE 1
9253 #else
9254 # define SQLITE_OS_WINCE 0
9255 #endif
9256
9257 /* If the SET_FULLSYNC macro is not defined above, then make it
9258 ** a no-op
9259 */
9260 #ifndef SET_FULLSYNC
9261 # define SET_FULLSYNC(x,y)
@@ -9089,11 +9263,11 @@
9263
9264 /*
9265 ** The default size of a disk sector
9266 */
9267 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
9268 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
9269 #endif
9270
9271 /*
9272 ** Temporary files are named starting with this prefix followed by 16 random
9273 ** alphanumeric characters, and no file extension. They are stored in the
@@ -9222,17 +9396,19 @@
9396 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
9397 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
9398 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
9399 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
9400 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
9401 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
9402 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
9403 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
9404 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9405 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9406 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9407 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9408 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9409
9410
9411 /*
9412 ** Functions for accessing sqlite3_vfs methods
9413 */
9414 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
@@ -9820,24 +9996,15 @@
9996 ** collating sequence may not be read or written.
9997 */
9998 struct CollSeq {
9999 char *zName; /* Name of the collating sequence, UTF-8 encoded */
10000 u8 enc; /* Text encoding handled by xCmp() */
 
10001 void *pUser; /* First argument to xCmp() */
10002 int (*xCmp)(void*,int, const void*, int, const void*);
10003 void (*xDel)(void*); /* Destructor for pUser */
10004 };
10005
 
 
 
 
 
 
 
 
10006 /*
10007 ** A sort order can be either ASC or DESC.
10008 */
10009 #define SQLITE_SO_ASC 0 /* Sort in ascending order */
10010 #define SQLITE_SO_DESC 1 /* Sort in ascending order */
@@ -10119,24 +10286,21 @@
10286 ** into its constituent fields.
10287 */
10288 struct UnpackedRecord {
10289 KeyInfo *pKeyInfo; /* Collation and sort-order information */
10290 u16 nField; /* Number of entries in apMem[] */
10291 u8 flags; /* Boolean settings. UNPACKED_... below */
10292 i64 rowid; /* Used by UNPACKED_PREFIX_SEARCH */
10293 Mem *aMem; /* Values */
10294 };
10295
10296 /*
10297 ** Allowed values of UnpackedRecord.flags
10298 */
10299 #define UNPACKED_INCRKEY 0x01 /* Make this key an epsilon larger */
10300 #define UNPACKED_PREFIX_MATCH 0x02 /* A prefix match is considered OK */
10301 #define UNPACKED_PREFIX_SEARCH 0x04 /* Ignore final (rowid) field */
 
 
 
10302
10303 /*
10304 ** Each SQL index is represented in memory by an
10305 ** instance of the following structure.
10306 **
@@ -10395,14 +10559,14 @@
10559 #define EP_InfixFunc 0x0080 /* True for an infix function: LIKE, GLOB, etc */
10560 #define EP_ExpCollate 0x0100 /* Collating sequence specified explicitly */
10561 #define EP_FixedDest 0x0200 /* Result needed in a specific register */
10562 #define EP_IntValue 0x0400 /* Integer value contained in u.iValue */
10563 #define EP_xIsSelect 0x0800 /* x.pSelect is valid (otherwise x.pList is) */
10564 #define EP_Hint 0x1000 /* Optimizer hint. Not required for correctness */
10565 #define EP_Reduced 0x2000 /* Expr struct is EXPR_REDUCEDSIZE bytes only */
10566 #define EP_TokenOnly 0x4000 /* Expr struct is EXPR_TOKENONLYSIZE bytes only */
10567 #define EP_Static 0x8000 /* Held in memory not obtained from malloc() */
10568
10569 /*
10570 ** The following are the meanings of bits in the Expr.flags2 field.
10571 */
10572 #define EP2_MallocedToken 0x0001 /* Need to sqlite3DbFree() Expr.zToken */
@@ -10460,11 +10624,11 @@
10624 Expr *pExpr; /* The list of expressions */
10625 char *zName; /* Token associated with this expression */
10626 char *zSpan; /* Original text of the expression */
10627 u8 sortOrder; /* 1 for DESC or 0 for ASC */
10628 u8 done; /* A flag to indicate when processing is finished */
10629 u16 iOrderByCol; /* For ORDER BY, column number in result set */
10630 u16 iAlias; /* Index into Parse.aAlias[] for zName */
10631 } *a; /* One entry for each expression */
10632 };
10633
10634 /*
@@ -10760,17 +10924,17 @@
10924
10925 /*
10926 ** Allowed values for Select.selFlags. The "SF" prefix stands for
10927 ** "Select Flag".
10928 */
10929 #define SF_Distinct 0x01 /* Output should be DISTINCT */
10930 #define SF_Resolved 0x02 /* Identifiers have been resolved */
10931 #define SF_Aggregate 0x04 /* Contains aggregate functions */
10932 #define SF_UsesEphemeral 0x08 /* Uses the OpenEphemeral opcode */
10933 #define SF_Expanded 0x10 /* sqlite3SelectExpand() called on this */
10934 #define SF_HasTypeInfo 0x20 /* FROM subqueries have Table metadata */
10935 #define SF_UseSorter 0x40 /* Sort using a sorter */
10936
10937
10938 /*
10939 ** The results of a select can be distributed in several ways. The
10940 ** "SRT" prefix means "SELECT Result Type".
@@ -10881,28 +11045,27 @@
11045 sqlite3 *db; /* The main database structure */
11046 int rc; /* Return code from execution */
11047 char *zErrMsg; /* An error message */
11048 Vdbe *pVdbe; /* An engine for executing database bytecode */
11049 u8 colNamesSet; /* TRUE after OP_ColumnName has been issued to pVdbe */
 
11050 u8 checkSchema; /* Causes schema cookie check after an error */
11051 u8 nested; /* Number of nested calls to the parser/code generator */
 
11052 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
11053 u8 nTempInUse; /* Number of aTempReg[] currently checked out */
11054 int aTempReg[8]; /* Holding area for temporary registers */
11055 int nRangeReg; /* Size of the temporary register block */
11056 int iRangeReg; /* First register in temporary register block */
11057 int nErr; /* Number of errors seen */
11058 int nTab; /* Number of previously allocated VDBE cursors */
11059 int nMem; /* Number of memory cells used so far */
11060 int nSet; /* Number of sets used so far */
11061 int nOnce; /* Number of OP_Once instructions so far */
11062 int ckBase; /* Base register of data during check constraints */
11063 int iCacheLevel; /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
11064 int iCacheCnt; /* Counter used to generate aColCache[].lru values */
11065 u8 nColCache; /* Number of entries in aColCache[] */
11066 u8 iColCache; /* Next entry in aColCache[] to replace */
11067 struct yColCache {
11068 int iTable; /* Table cursor number */
11069 int iColumn; /* Table column number */
11070 u8 tempReg; /* iReg is a temp register that needs to be freed */
11071 int iLevel; /* Nesting level */
@@ -10940,11 +11103,10 @@
11103 int nVar; /* Number of '?' variables seen in the SQL so far */
11104 int nzVar; /* Number of available slots in azVar[] */
11105 char **azVar; /* Pointers to names of parameters */
11106 Vdbe *pReprepare; /* VM being reprepared (sqlite3Reprepare()) */
11107 int nAlias; /* Number of aliased result set columns */
 
11108 int *aAlias; /* Register used to hold aliased result */
11109 u8 explain; /* True if the EXPLAIN flag is found on the query */
11110 Token sNameToken; /* Token with unqualified schema object name */
11111 Token sLastToken; /* The last token parsed */
11112 const char *zTail; /* All SQL text past the last semicolon parsed */
@@ -11135,11 +11297,11 @@
11297 int mxStrlen; /* Maximum string length */
11298 int szLookaside; /* Default lookaside buffer size */
11299 int nLookaside; /* Default lookaside buffer count */
11300 sqlite3_mem_methods m; /* Low-level memory allocation interface */
11301 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
11302 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
11303 void *pHeap; /* Heap storage space */
11304 int nHeap; /* Size of pHeap[] */
11305 int mnReq, mxReq; /* Min and max heap requests sizes */
11306 void *pScratch; /* Scratch memory */
11307 int szScratch; /* Size of each scratch buffer */
@@ -11341,10 +11503,33 @@
11503 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
11504 #endif
11505 #if defined(SQLITE_TEST)
11506 SQLITE_PRIVATE void *sqlite3TestTextToPtr(const char*);
11507 #endif
11508
11509 /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
11510 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
11511 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe*);
11512 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
11513 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe*);
11514 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe*);
11515 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe*);
11516 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe*);
11517 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe*, Select*);
11518 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe*, Expr*);
11519 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe*, ExprList*);
11520 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe*);
11521 #else
11522 # define sqlite3ExplainBegin(X)
11523 # define sqlite3ExplainSelect(A,B)
11524 # define sqlite3ExplainExpr(A,B)
11525 # define sqlite3ExplainExprList(A,B)
11526 # define sqlite3ExplainFinish(X)
11527 # define sqlite3VdbeExplanation(X) 0
11528 #endif
11529
11530
11531 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
11532 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
11533 SQLITE_PRIVATE int sqlite3Dequote(char*);
11534 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
11535 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
@@ -11351,10 +11536,11 @@
11536 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
11537 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
11538 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
11539 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
11540 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
11541 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
11542 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
11543 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
11544 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
11545 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
11546 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
@@ -11382,10 +11568,11 @@
11568 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
11569 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
11570 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,Select*);
11571 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
11572 sqlite3_vfs**,char**,char **);
11573 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
11574
11575 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
11576 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
11577 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
11578 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -11720,10 +11907,11 @@
11907 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
11908 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
11909
11910 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
11911 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
11912 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
11913 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
11914 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
11915 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
11916 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
11917
@@ -12099,11 +12287,11 @@
12287 0x7ffffffe, /* mxStrlen */
12288 128, /* szLookaside */
12289 500, /* nLookaside */
12290 {0,0,0,0,0,0,0,0}, /* m */
12291 {0,0,0,0,0,0,0,0,0}, /* mutex */
12292 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
12293 (void*)0, /* pHeap */
12294 0, /* nHeap */
12295 0, 0, /* mnHeap, mxHeap */
12296 (void*)0, /* pScratch */
12297 0, /* szScratch */
@@ -12626,10 +12814,13 @@
12814 typedef unsigned char Bool;
12815
12816 /* Opaque type used by code in vdbesort.c */
12817 typedef struct VdbeSorter VdbeSorter;
12818
12819 /* Opaque type used by the explainer */
12820 typedef struct Explain Explain;
12821
12822 /*
12823 ** A cursor is a pointer into a single BTree within a database file.
12824 ** The cursor can seek to a BTree entry with a particular key, or
12825 ** loop over all entries of the Btree. You can also insert new BTree
12826 ** entries or retrieve the key or data from the entry that the cursor
@@ -12710,10 +12901,12 @@
12901 int pc; /* Program Counter in parent (calling) frame */
12902 Op *aOp; /* Program instructions for parent frame */
12903 int nOp; /* Size of aOp array */
12904 Mem *aMem; /* Array of memory cells for parent frame */
12905 int nMem; /* Number of entries in aMem */
12906 u8 *aOnceFlag; /* Array of OP_Once flags for parent frame */
12907 int nOnceFlag; /* Number of entries in aOnceFlag */
12908 VdbeCursor **apCsr; /* Array of Vdbe cursors for parent frame */
12909 u16 nCursor; /* Number of entries in apCsr */
12910 void *token; /* Copy of SubProgram.token */
12911 int nChildMem; /* Number of memory cells for child frame */
12912 int nChildCsr; /* Number of cursors for child frame */
@@ -12848,10 +13041,22 @@
13041 Mem *pMem; /* Memory cell used to store aggregate context */
13042 int isError; /* Error code returned by the function. */
13043 CollSeq *pColl; /* Collating sequence */
13044 };
13045
13046 /*
13047 ** An Explain object accumulates indented output which is helpful
13048 ** in describing recursive data structures.
13049 */
13050 struct Explain {
13051 Vdbe *pVdbe; /* Attach the explanation to this Vdbe */
13052 StrAccum str; /* The string being accumulated */
13053 int nIndent; /* Number of elements in aIndent */
13054 u16 aIndent[100]; /* Levels of indentation */
13055 char zBase[100]; /* Initial space */
13056 };
13057
13058 /*
13059 ** An instance of the virtual machine. This structure contains the complete
13060 ** state of the virtual machine.
13061 **
13062 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
@@ -12914,15 +13119,21 @@
13119 char *zSql; /* Text of the SQL statement that generated this */
13120 void *pFree; /* Free this when deleting the vdbe */
13121 #ifdef SQLITE_DEBUG
13122 FILE *trace; /* Write an execution trace here, if not NULL */
13123 #endif
13124 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
13125 Explain *pExplain; /* The explainer */
13126 char *zExplain; /* Explanation of data structures */
13127 #endif
13128 VdbeFrame *pFrame; /* Parent frame */
13129 VdbeFrame *pDelFrame; /* List of frame objects to free on VM reset */
13130 int nFrame; /* Number of frames in pFrame list */
13131 u32 expmask; /* Binding to these vars invalidates VM */
13132 SubProgram *pProgram; /* Linked list of all sub-programs used by VM */
13133 int nOnceFlag; /* Size of array aOnceFlag[] */
13134 u8 *aOnceFlag; /* Flags for OP_Once */
13135 };
13136
13137 /*
13138 ** The following are allowed values for Vdbe.magic
13139 */
@@ -12978,11 +13189,11 @@
13189 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
13190 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
13191 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
13192 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
13193 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
13194 #define VdbeMemRelease(X) \
13195 if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
13196 sqlite3VdbeMemReleaseExternal(X);
13197 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
13198 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
13199 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
@@ -13017,11 +13228,11 @@
13228 # define sqlite3VdbeEnter(X)
13229 # define sqlite3VdbeLeave(X)
13230 #endif
13231
13232 #ifdef SQLITE_DEBUG
13233 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
13234 #endif
13235
13236 #ifndef SQLITE_OMIT_FOREIGN_KEY
13237 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
13238 #else
@@ -13035,12 +13246,14 @@
13246 #endif
13247 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
13248
13249 #ifndef SQLITE_OMIT_INCRBLOB
13250 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *);
13251 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
13252 #else
13253 #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
13254 #define ExpandBlob(P) SQLITE_OK
13255 #endif
13256
13257 #endif /* !defined(_VDBEINT_H_) */
13258
13259 /************** End of vdbeInt.h *********************************************/
@@ -14436,15 +14649,22 @@
14649 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
14650 **
14651 ** The following functions are instrumented for malloc() failure
14652 ** testing:
14653 **
 
14654 ** sqlite3OsRead()
14655 ** sqlite3OsWrite()
14656 ** sqlite3OsSync()
14657 ** sqlite3OsFileSize()
14658 ** sqlite3OsLock()
14659 ** sqlite3OsCheckReservedLock()
14660 ** sqlite3OsFileControl()
14661 ** sqlite3OsShmMap()
14662 ** sqlite3OsOpen()
14663 ** sqlite3OsDelete()
14664 ** sqlite3OsAccess()
14665 ** sqlite3OsFullPathname()
14666 **
14667 */
14668 #if defined(SQLITE_TEST)
14669 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
14670 #define DO_OS_MALLOC_TEST(x) \
@@ -14499,13 +14719,27 @@
14719 }
14720 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
14721 DO_OS_MALLOC_TEST(id);
14722 return id->pMethods->xCheckReservedLock(id, pResOut);
14723 }
14724
14725 /*
14726 ** Use sqlite3OsFileControl() when we are doing something that might fail
14727 ** and we need to know about the failures. Use sqlite3OsFileControlHint()
14728 ** when simply tossing information over the wall to the VFS and we do not
14729 ** really care if the VFS receives and understands the information since it
14730 ** is only a hint and can be safely ignored. The sqlite3OsFileControlHint()
14731 ** routine has no return value since the return value would be meaningless.
14732 */
14733 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
14734 DO_OS_MALLOC_TEST(id);
14735 return id->pMethods->xFileControl(id, op, pArg);
14736 }
14737 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
14738 (void)id->pMethods->xFileControl(id, op, pArg);
14739 }
14740
14741 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
14742 int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
14743 return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
14744 }
14745 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
@@ -14525,10 +14759,11 @@
14759 int iPage,
14760 int pgsz,
14761 int bExtend, /* True to extend file if necessary */
14762 void volatile **pp /* OUT: Pointer to mapping */
14763 ){
14764 DO_OS_MALLOC_TEST(id);
14765 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
14766 }
14767
14768 /*
14769 ** The next group of routines are convenience wrappers around the
@@ -14541,19 +14776,21 @@
14776 int flags,
14777 int *pFlagsOut
14778 ){
14779 int rc;
14780 DO_OS_MALLOC_TEST(0);
14781 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
14782 ** down into the VFS layer. Some SQLITE_OPEN_ flags (for example,
14783 ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
14784 ** reaching the VFS. */
14785 rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
14786 assert( rc==SQLITE_OK || pFile->pMethods==0 );
14787 return rc;
14788 }
14789 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
14790 DO_OS_MALLOC_TEST(0);
14791 assert( dirSync==0 || dirSync==1 );
14792 return pVfs->xDelete(pVfs, zPath, dirSync);
14793 }
14794 SQLITE_PRIVATE int sqlite3OsAccess(
14795 sqlite3_vfs *pVfs,
14796 const char *zPath,
@@ -14567,10 +14804,11 @@
14804 sqlite3_vfs *pVfs,
14805 const char *zPath,
14806 int nPathOut,
14807 char *zPathOut
14808 ){
14809 DO_OS_MALLOC_TEST(0);
14810 zPathOut[0] = 0;
14811 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
14812 }
14813 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14814 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
@@ -14918,31 +15156,81 @@
15156 ** used when no other memory allocator is specified using compile-time
15157 ** macros.
15158 */
15159 #ifdef SQLITE_SYSTEM_MALLOC
15160
15161 /*
15162 ** Windows systems have malloc_usable_size() but it is called _msize()
15163 */
15164 #if !defined(HAVE_MALLOC_USABLE_SIZE) && SQLITE_OS_WIN
15165 # define HAVE_MALLOC_USABLE_SIZE 1
15166 # define malloc_usable_size _msize
15167 #endif
15168
15169 #if defined(__APPLE__)
15170
15171 /*
15172 ** Use the zone allocator available on apple products
15173 */
15174 #include <sys/sysctl.h>
15175 #include <malloc/malloc.h>
15176 #include <libkern/OSAtomic.h>
15177 static malloc_zone_t* _sqliteZone_;
15178 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
15179 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
15180 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
15181 #define SQLITE_MALLOCSIZE(x) \
15182 (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
15183
15184 #else /* if not __APPLE__ */
15185
15186 /*
15187 ** Use standard C library malloc and free on non-Apple systems.
15188 */
15189 #define SQLITE_MALLOC(x) malloc(x)
15190 #define SQLITE_FREE(x) free(x)
15191 #define SQLITE_REALLOC(x,y) realloc((x),(y))
15192
15193 #ifdef HAVE_MALLOC_USABLE_SIZE
15194 #include <malloc.h>
15195 #define SQLITE_MALLOCSIZE(x) malloc_usable_size(x)
15196 #else
15197 #undef SQLITE_MALLOCSIZE
15198 #endif
15199
15200 #endif /* __APPLE__ or not __APPLE__ */
15201
15202 /*
15203 ** Like malloc(), but remember the size of the allocation
15204 ** so that we can find it later using sqlite3MemSize().
15205 **
15206 ** For this low-level routine, we are guaranteed that nByte>0 because
15207 ** cases of nByte<=0 will be intercepted and dealt with by higher level
15208 ** routines.
15209 */
15210 static void *sqlite3MemMalloc(int nByte){
15211 #ifdef SQLITE_MALLOCSIZE
15212 void *p = SQLITE_MALLOC( nByte );
15213 if( p==0 ){
15214 testcase( sqlite3GlobalConfig.xLog!=0 );
15215 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
15216 }
15217 return p;
15218 #else
15219 sqlite3_int64 *p;
15220 assert( nByte>0 );
15221 nByte = ROUND8(nByte);
15222 p = SQLITE_MALLOC( nByte+8 );
15223 if( p ){
15224 p[0] = nByte;
15225 p++;
15226 }else{
15227 testcase( sqlite3GlobalConfig.xLog!=0 );
15228 sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
15229 }
15230 return (void *)p;
15231 #endif
15232 }
15233
15234 /*
15235 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
15236 ** or sqlite3MemRealloc().
@@ -14950,26 +15238,34 @@
15238 ** For this low-level routine, we already know that pPrior!=0 since
15239 ** cases where pPrior==0 will have been intecepted and dealt with
15240 ** by higher-level routines.
15241 */
15242 static void sqlite3MemFree(void *pPrior){
15243 #ifdef SQLITE_MALLOCSIZE
15244 SQLITE_FREE(pPrior);
15245 #else
15246 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
15247 assert( pPrior!=0 );
15248 p--;
15249 SQLITE_FREE(p);
15250 #endif
15251 }
15252
15253 /*
15254 ** Report the allocated size of a prior return from xMalloc()
15255 ** or xRealloc().
15256 */
15257 static int sqlite3MemSize(void *pPrior){
15258 #ifdef SQLITE_MALLOCSIZE
15259 return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
15260 #else
15261 sqlite3_int64 *p;
15262 if( pPrior==0 ) return 0;
15263 p = (sqlite3_int64*)pPrior;
15264 p--;
15265 return (int)p[0];
15266 #endif
15267 }
15268
15269 /*
15270 ** Like realloc(). Resize an allocation previously obtained from
15271 ** sqlite3MemMalloc().
@@ -14979,15 +15275,25 @@
15275 ** redirected to xMalloc. Similarly, we know that nByte>0 becauses
15276 ** cases where nByte<=0 will have been intercepted by higher-level
15277 ** routines and redirected to xFree.
15278 */
15279 static void *sqlite3MemRealloc(void *pPrior, int nByte){
15280 #ifdef SQLITE_MALLOCSIZE
15281 void *p = SQLITE_REALLOC(pPrior, nByte);
15282 if( p==0 ){
15283 testcase( sqlite3GlobalConfig.xLog!=0 );
15284 sqlite3_log(SQLITE_NOMEM,
15285 "failed memory resize %u to %u bytes",
15286 SQLITE_MALLOCSIZE(pPrior), nByte);
15287 }
15288 return p;
15289 #else
15290 sqlite3_int64 *p = (sqlite3_int64*)pPrior;
15291 assert( pPrior!=0 && nByte>0 );
15292 assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
15293 p--;
15294 p = SQLITE_REALLOC(p, nByte+8 );
15295 if( p ){
15296 p[0] = nByte;
15297 p++;
15298 }else{
15299 testcase( sqlite3GlobalConfig.xLog!=0 );
@@ -14994,10 +15300,11 @@
15300 sqlite3_log(SQLITE_NOMEM,
15301 "failed memory resize %u to %u bytes",
15302 sqlite3MemSize(pPrior), nByte);
15303 }
15304 return (void*)p;
15305 #endif
15306 }
15307
15308 /*
15309 ** Round up a request size to the next valid allocation size.
15310 */
@@ -15007,10 +15314,38 @@
15314
15315 /*
15316 ** Initialize this module.
15317 */
15318 static int sqlite3MemInit(void *NotUsed){
15319 #if defined(__APPLE__)
15320 int cpuCount;
15321 size_t len;
15322 if( _sqliteZone_ ){
15323 return SQLITE_OK;
15324 }
15325 len = sizeof(cpuCount);
15326 /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
15327 sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
15328 if( cpuCount>1 ){
15329 /* defer MT decisions to system malloc */
15330 _sqliteZone_ = malloc_default_zone();
15331 }else{
15332 /* only 1 core, use our own zone to contention over global locks,
15333 ** e.g. we have our own dedicated locks */
15334 bool success;
15335 malloc_zone_t* newzone = malloc_create_zone(4096, 0);
15336 malloc_set_zone_name(newzone, "Sqlite_Heap");
15337 do{
15338 success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
15339 (void * volatile *)&_sqliteZone_);
15340 }while(!_sqliteZone_);
15341 if( !success ){
15342 /* somebody registered a zone first */
15343 malloc_destroy_zone(newzone);
15344 }
15345 }
15346 #endif
15347 UNUSED_PARAMETER(NotUsed);
15348 return SQLITE_OK;
15349 }
15350
15351 /*
@@ -16996,11 +17331,11 @@
17331 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
17332 return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
17333 }
17334 #endif
17335
17336 #endif /* !defined(SQLITE_MUTEX_OMIT) */
17337
17338 /************** End of mutex.c ***********************************************/
17339 /************** Begin file mutex_noop.c **************************************/
17340 /*
17341 ** 2008 October 07
@@ -17203,12 +17538,12 @@
17538 */
17539 #ifdef SQLITE_MUTEX_NOOP
17540 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
17541 return sqlite3NoopMutex();
17542 }
17543 #endif /* defined(SQLITE_MUTEX_NOOP) */
17544 #endif /* !defined(SQLITE_MUTEX_OMIT) */
17545
17546 /************** End of mutex_noop.c ******************************************/
17547 /************** Begin file mutex_os2.c ***************************************/
17548 /*
17549 ** 2007 August 28
@@ -17833,11 +18168,11 @@
18168 };
18169
18170 return &sMutex;
18171 }
18172
18173 #endif /* SQLITE_MUTEX_PTHREADS */
18174
18175 /************** End of mutex_unix.c ******************************************/
18176 /************** Begin file mutex_w32.c ***************************************/
18177 /*
18178 ** 2007 August 14
@@ -18302,11 +18637,12 @@
18637 */
18638 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
18639 sqlite3_int64 priorLimit;
18640 sqlite3_int64 excess;
18641 #ifndef SQLITE_OMIT_AUTOINIT
18642 int rc = sqlite3_initialize();
18643 if( rc ) return -1;
18644 #endif
18645 sqlite3_mutex_enter(mem0.mutex);
18646 priorLimit = mem0.alarmThreshold;
18647 sqlite3_mutex_leave(mem0.mutex);
18648 if( n<0 ) return priorLimit;
@@ -19087,11 +19423,11 @@
19423 #endif /* SQLITE_OMIT_FLOATING_POINT */
19424
19425 /*
19426 ** Append N space characters to the given string buffer.
19427 */
19428 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
19429 static const char zSpaces[] = " ";
19430 while( N>=(int)sizeof(zSpaces)-1 ){
19431 sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
19432 N -= sizeof(zSpaces)-1;
19433 }
@@ -19615,21 +19951,21 @@
19951 */
19952 if( !flag_leftjustify ){
19953 register int nspace;
19954 nspace = width-length;
19955 if( nspace>0 ){
19956 sqlite3AppendSpace(pAccum, nspace);
19957 }
19958 }
19959 if( length>0 ){
19960 sqlite3StrAccumAppend(pAccum, bufpt, length);
19961 }
19962 if( flag_leftjustify ){
19963 register int nspace;
19964 nspace = width-length;
19965 if( nspace>0 ){
19966 sqlite3AppendSpace(pAccum, nspace);
19967 }
19968 }
19969 sqlite3_free(zExtra);
19970 }/* End for loop over the format string */
19971 } /* End of function */
@@ -21800,22 +22136,21 @@
22136 ** Examples:
22137 **
22138 ** test.db-journal => test.nal
22139 ** test.db-wal => test.wal
22140 ** test.db-shm => test.shm
22141 ** test.db-mj7f3319fa => test.9fa
22142 */
22143 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
22144 #if SQLITE_ENABLE_8_3_NAMES<2
22145 if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
 
 
22146 #endif
22147 {
22148 int i, sz;
22149 sz = sqlite3Strlen30(z);
22150 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
22151 if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
22152 }
22153 }
22154 #endif
22155
22156 /************** End of util.c ************************************************/
@@ -24516,10 +24851,11 @@
24851 #include <sys/time.h>
24852 #include <errno.h>
24853 #ifndef SQLITE_OMIT_WAL
24854 #include <sys/mman.h>
24855 #endif
24856
24857
24858 #if SQLITE_ENABLE_LOCKING_STYLE
24859 # include <sys/ioctl.h>
24860 # if OS_VXWORKS
24861 # include <semaphore.h>
@@ -24600,10 +24936,11 @@
24936 ** VFS implementations.
24937 */
24938 typedef struct unixFile unixFile;
24939 struct unixFile {
24940 sqlite3_io_methods const *pMethod; /* Always the first entry */
24941 sqlite3_vfs *pVfs; /* The VFS that created this unixFile */
24942 unixInodeInfo *pInode; /* Info about locks on this inode */
24943 int h; /* The file descriptor */
24944 unsigned char eFileLock; /* The type of lock held on this fd */
24945 unsigned char ctrlFlags; /* Behavioral bits. UNIXFILE_* flags */
24946 int lastErrno; /* The unix errno from last I/O error */
@@ -24617,11 +24954,10 @@
24954 #endif
24955 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
24956 unsigned fsFlags; /* cached details from statfs() */
24957 #endif
24958 #if OS_VXWORKS
 
24959 struct vxworksFileId *pId; /* Unique file ID */
24960 #endif
24961 #ifndef NDEBUG
24962 /* The next group of variables are used to track whether or not the
24963 ** transaction counter in bytes 24-27 of database files are updated
@@ -24651,10 +24987,14 @@
24987 #ifndef SQLITE_DISABLE_DIRSYNC
24988 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24989 #else
24990 # define UNIXFILE_DIRSYNC 0x00
24991 #endif
24992 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
24993 #define UNIXFILE_DELETE 0x20 /* Delete on close */
24994 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
24995 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
24996
24997 /*
24998 ** Include code that is common to all os_*.c files
24999 */
25000 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -25009,10 +25349,16 @@
25349 #define osUnlink ((int(*)(const char*))aSyscall[16].pCurrent)
25350
25351 { "openDirectory", (sqlite3_syscall_ptr)openDirectory, 0 },
25352 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
25353
25354 { "mkdir", (sqlite3_syscall_ptr)mkdir, 0 },
25355 #define osMkdir ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
25356
25357 { "rmdir", (sqlite3_syscall_ptr)rmdir, 0 },
25358 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
25359
25360 }; /* End of the overrideable system calls */
25361
25362 /*
25363 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
25364 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -26358,11 +26704,11 @@
26704 robust_close(pFile, pFile->h, __LINE__);
26705 pFile->h = -1;
26706 }
26707 #if OS_VXWORKS
26708 if( pFile->pId ){
26709 if( pFile->ctrlFlags & UNIXFILE_DELETE ){
26710 osUnlink(pFile->pId->zCanonicalName);
26711 }
26712 vxworksReleaseFileId(pFile->pId);
26713 pFile->pId = 0;
26714 }
@@ -26447,12 +26793,12 @@
26793
26794 /******************************************************************************
26795 ************************* Begin dot-file Locking ******************************
26796 **
26797 ** The dotfile locking implementation uses the existance of separate lock
26798 ** files (really a directory) to control access to the database. This works
26799 ** on just about every filesystem imaginable. But there are serious downsides:
26800 **
26801 ** (1) There is zero concurrency. A single reader blocks all other
26802 ** connections from reading or writing the database.
26803 **
26804 ** (2) An application crash or power loss can leave stale lock files
@@ -26459,19 +26805,19 @@
26805 ** sitting around that need to be cleared manually.
26806 **
26807 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
26808 ** other locking strategy is available.
26809 **
26810 ** Dotfile locking works by creating a subdirectory in the same directory as
26811 ** the database and with the same name but with a ".lock" extension added.
26812 ** The existance of a lock directory implies an EXCLUSIVE lock. All other
26813 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
26814 */
26815
26816 /*
26817 ** The file suffix added to the data base filename in order to create the
26818 ** lock directory.
26819 */
26820 #define DOTLOCK_SUFFIX ".lock"
26821
26822 /*
26823 ** This routine checks if there is a RESERVED lock held on the specified
@@ -26534,11 +26880,10 @@
26880 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
26881 ** But we track the other locking levels internally.
26882 */
26883 static int dotlockLock(sqlite3_file *id, int eFileLock) {
26884 unixFile *pFile = (unixFile*)id;
 
26885 char *zLockFile = (char *)pFile->lockingContext;
26886 int rc = SQLITE_OK;
26887
26888
26889 /* If we have any lock, then the lock file already exists. All we have
@@ -26554,13 +26899,13 @@
26899 #endif
26900 return SQLITE_OK;
26901 }
26902
26903 /* grab an exclusive lock */
26904 rc = osMkdir(zLockFile, 0777);
26905 if( rc<0 ){
26906 /* failed to open/create the lock directory */
26907 int tErrno = errno;
26908 if( EEXIST == tErrno ){
26909 rc = SQLITE_BUSY;
26910 } else {
26911 rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
@@ -26568,11 +26913,10 @@
26913 pFile->lastErrno = tErrno;
26914 }
26915 }
26916 return rc;
26917 }
 
26918
26919 /* got it, set the type and return ok */
26920 pFile->eFileLock = eFileLock;
26921 return rc;
26922 }
@@ -26587,10 +26931,11 @@
26931 ** When the locking level reaches NO_LOCK, delete the lock file.
26932 */
26933 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
26934 unixFile *pFile = (unixFile*)id;
26935 char *zLockFile = (char *)pFile->lockingContext;
26936 int rc;
26937
26938 assert( pFile );
26939 OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
26940 pFile->eFileLock, getpid()));
26941 assert( eFileLock<=SHARED_LOCK );
@@ -26608,13 +26953,15 @@
26953 return SQLITE_OK;
26954 }
26955
26956 /* To fully unlock the database, delete the lock file */
26957 assert( eFileLock==NO_LOCK );
26958 rc = osRmdir(zLockFile);
26959 if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
26960 if( rc<0 ){
26961 int tErrno = errno;
26962 rc = 0;
26963 if( ENOENT != tErrno ){
26964 rc = SQLITE_IOERR_UNLOCK;
26965 }
26966 if( IS_LOCK_ERROR(rc) ){
26967 pFile->lastErrno = tErrno;
@@ -27546,39 +27893,52 @@
27893 ** To avoid stomping the errno value on a failed read the lastErrno value
27894 ** is set before returning.
27895 */
27896 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
27897 int got;
27898 int prior = 0;
27899 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
27900 i64 newOffset;
27901 #endif
27902 TIMER_START;
27903 do{
27904 #if defined(USE_PREAD)
27905 got = osPread(id->h, pBuf, cnt, offset);
27906 SimulateIOError( got = -1 );
27907 #elif defined(USE_PREAD64)
27908 got = osPread64(id->h, pBuf, cnt, offset);
27909 SimulateIOError( got = -1 );
27910 #else
27911 newOffset = lseek(id->h, offset, SEEK_SET);
27912 SimulateIOError( newOffset-- );
27913 if( newOffset!=offset ){
27914 if( newOffset == -1 ){
27915 ((unixFile*)id)->lastErrno = errno;
27916 }else{
27917 ((unixFile*)id)->lastErrno = 0;
27918 }
27919 return -1;
27920 }
27921 got = osRead(id->h, pBuf, cnt);
27922 #endif
27923 if( got==cnt ) break;
27924 if( got<0 ){
27925 if( errno==EINTR ){ got = 1; continue; }
27926 prior = 0;
27927 ((unixFile*)id)->lastErrno = errno;
27928 break;
27929 }else if( got>0 ){
27930 cnt -= got;
27931 offset += got;
27932 prior += got;
27933 pBuf = (void*)(got + (char*)pBuf);
27934 }
27935 }while( got>0 );
27936 TIMER_END;
27937 OSTRACE(("READ %-3d %5d %7lld %llu\n",
27938 id->h, got+prior, offset-prior, TIMER_ELAPSED));
27939 return got+prior;
27940 }
27941
27942 /*
27943 ** Read data from a file into a buffer. Return SQLITE_OK if all
27944 ** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -28078,10 +28438,26 @@
28438 }
28439 }
28440
28441 return SQLITE_OK;
28442 }
28443
28444 /*
28445 ** If *pArg is inititially negative then this is a query. Set *pArg to
28446 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
28447 **
28448 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
28449 */
28450 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
28451 if( *pArg<0 ){
28452 *pArg = (pFile->ctrlFlags & mask)!=0;
28453 }else if( (*pArg)==0 ){
28454 pFile->ctrlFlags &= ~mask;
28455 }else{
28456 pFile->ctrlFlags |= mask;
28457 }
28458 }
28459
28460 /*
28461 ** Information and control of an open file handle.
28462 */
28463 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
@@ -28105,18 +28481,19 @@
28481 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28482 SimulateIOErrorBenign(0);
28483 return rc;
28484 }
28485 case SQLITE_FCNTL_PERSIST_WAL: {
28486 unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
28487 return SQLITE_OK;
28488 }
28489 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
28490 unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
28491 return SQLITE_OK;
28492 }
28493 case SQLITE_FCNTL_VFSNAME: {
28494 *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
28495 return SQLITE_OK;
28496 }
28497 #ifndef NDEBUG
28498 /* The pager calls this method to signal that it has done
28499 ** a rollback and that the database is therefore unchanged and
@@ -28132,13 +28509,10 @@
28509 case SQLITE_SET_LOCKPROXYFILE:
28510 case SQLITE_GET_LOCKPROXYFILE: {
28511 return proxyFileControl(id,op,pArg);
28512 }
28513 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
 
 
 
28514 }
28515 return SQLITE_NOTFOUND;
28516 }
28517
28518 /*
@@ -28149,21 +28523,35 @@
28523 ** SQLite code assumes this function cannot fail. It also assumes that
28524 ** if two files are created in the same file-system directory (i.e.
28525 ** a database and its journal file) that the sector size will be the
28526 ** same for both.
28527 */
28528 static int unixSectorSize(sqlite3_file *pFile){
28529 (void)pFile;
28530 return SQLITE_DEFAULT_SECTOR_SIZE;
28531 }
28532
28533 /*
28534 ** Return the device characteristics for the file.
28535 **
28536 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
28537 ** However, that choice is contraversial since technically the underlying
28538 ** file system does not always provide powersafe overwrites. (In other
28539 ** words, after a power-loss event, parts of the file that were never
28540 ** written might end up being altered.) However, non-PSOW behavior is very,
28541 ** very rare. And asserting PSOW makes a large reduction in the amount
28542 ** of required I/O for journaling, since a lot of padding is eliminated.
28543 ** Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
28544 ** available to turn it off and URI query parameter available to turn it off.
28545 */
28546 static int unixDeviceCharacteristics(sqlite3_file *id){
28547 unixFile *p = (unixFile*)id;
28548 if( p->ctrlFlags & UNIXFILE_PSOW ){
28549 return SQLITE_IOCAP_POWERSAFE_OVERWRITE;
28550 }else{
28551 return 0;
28552 }
28553 }
28554
28555 #ifndef SQLITE_OMIT_WAL
28556
28557
@@ -28414,20 +28802,20 @@
28802 rc = SQLITE_IOERR_FSTAT;
28803 goto shm_open_err;
28804 }
28805
28806 #ifdef SQLITE_SHM_DIRECTORY
28807 nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
28808 #else
28809 nShmFilename = 6 + (int)strlen(pDbFd->zPath);
28810 #endif
28811 pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
28812 if( pShmNode==0 ){
28813 rc = SQLITE_NOMEM;
28814 goto shm_open_err;
28815 }
28816 memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
28817 zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
28818 #ifdef SQLITE_SHM_DIRECTORY
28819 sqlite3_snprintf(nShmFilename, zShmFilename,
28820 SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
28821 (u32)sStat.st_ino, (u32)sStat.st_dev);
@@ -28443,14 +28831,12 @@
28831 rc = SQLITE_NOMEM;
28832 goto shm_open_err;
28833 }
28834
28835 if( pInode->bProcessLock==0 ){
 
28836 int openFlags = O_RDWR | O_CREAT;
28837 if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
 
28838 openFlags = O_RDONLY;
28839 pShmNode->isReadonly = 1;
28840 }
28841 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
28842 if( pShmNode->h<0 ){
@@ -29108,28 +29494,20 @@
29494 ** Initialize the contents of the unixFile structure pointed to by pId.
29495 */
29496 static int fillInUnixFile(
29497 sqlite3_vfs *pVfs, /* Pointer to vfs object */
29498 int h, /* Open file descriptor of file being opened */
 
29499 sqlite3_file *pId, /* Write to the unixFile structure here */
29500 const char *zFilename, /* Name of the file being opened */
29501 int ctrlFlags /* Zero or more UNIXFILE_* values */
 
 
29502 ){
29503 const sqlite3_io_methods *pLockingStyle;
29504 unixFile *pNew = (unixFile *)pId;
29505 int rc = SQLITE_OK;
29506
29507 assert( pNew->pInode==NULL );
29508
 
 
 
 
 
29509 /* Usually the path zFilename should not be a relative pathname. The
29510 ** exception is when opening the proxy "conch" file in builds that
29511 ** include the special Apple locking styles.
29512 */
29513 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
@@ -29138,36 +29516,34 @@
29516 #else
29517 assert( zFilename==0 || zFilename[0]=='/' );
29518 #endif
29519
29520 /* No locking occurs in temporary files */
29521 assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
29522
29523 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
29524 pNew->h = h;
29525 pNew->pVfs = pVfs;
29526 pNew->zPath = zFilename;
29527 pNew->ctrlFlags = (u8)ctrlFlags;
29528 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
29529 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
29530 pNew->ctrlFlags |= UNIXFILE_PSOW;
29531 }
29532 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
29533 pNew->ctrlFlags |= UNIXFILE_EXCL;
 
 
 
 
 
 
 
 
29534 }
29535
29536 #if OS_VXWORKS
29537 pNew->pId = vxworksFindFileId(zFilename);
29538 if( pNew->pId==0 ){
29539 ctrlFlags |= UNIXFILE_NOLOCK;
29540 rc = SQLITE_NOMEM;
29541 }
29542 #endif
29543
29544 if( ctrlFlags & UNIXFILE_NOLOCK ){
29545 pLockingStyle = &nolockIoMethods;
29546 }else{
29547 pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
29548 #if SQLITE_ENABLE_LOCKING_STYLE
29549 /* Cache zFilename in the locking context (AFP and dotlock override) for
@@ -29284,11 +29660,11 @@
29660 if( h>=0 ) robust_close(pNew, h, __LINE__);
29661 h = -1;
29662 osUnlink(zFilename);
29663 isDelete = 0;
29664 }
29665 if( isDelete ) pNew->ctrlFlags |= UNIXFILE_DELETE;
29666 #endif
29667 if( rc!=SQLITE_OK ){
29668 if( h>=0 ) robust_close(pNew, h, __LINE__);
29669 }else{
29670 pNew->pMethod = pLockingStyle;
@@ -29349,22 +29725,23 @@
29725 if( zDir==0 ) zDir = ".";
29726
29727 /* Check that the output buffer is large enough for the temporary file
29728 ** name. If it is not, return SQLITE_ERROR.
29729 */
29730 if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
29731 return SQLITE_ERROR;
29732 }
29733
29734 do{
29735 sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29736 j = (int)strlen(zBuf);
29737 sqlite3_randomness(15, &zBuf[j]);
29738 for(i=0; i<15; i++, j++){
29739 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29740 }
29741 zBuf[j] = 0;
29742 zBuf[j+1] = 0;
29743 }while( osAccess(zBuf,0)==0 );
29744 return SQLITE_OK;
29745 }
29746
29747 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
@@ -29481,11 +29858,11 @@
29858 ** where NN is a decimal number. The NN naming schemes are
29859 ** used by the test_multiplex.c module.
29860 */
29861 nDb = sqlite3Strlen30(zPath) - 1;
29862 #ifdef SQLITE_ENABLE_8_3_NAMES
29863 while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
29864 if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
29865 #else
29866 while( zPath[nDb]!='-' ){
29867 assert( nDb>0 );
29868 assert( zPath[nDb]!='\n' );
@@ -29539,10 +29916,11 @@
29916 int fd = -1; /* File descriptor returned by open() */
29917 int openFlags = 0; /* Flags to pass to open() */
29918 int eType = flags&0xFFFFFF00; /* Type of file to open */
29919 int noLock; /* True to omit locking primitives */
29920 int rc = SQLITE_OK; /* Function Return Code */
29921 int ctrlFlags = 0; /* UNIXFILE_* flags */
29922
29923 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
29924 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
29925 int isCreate = (flags & SQLITE_OPEN_CREATE);
29926 int isReadonly = (flags & SQLITE_OPEN_READONLY);
@@ -29565,11 +29943,11 @@
29943 ));
29944
29945 /* If argument zPath is a NULL pointer, this function is required to open
29946 ** a temporary file. Use this buffer to store the file name in.
29947 */
29948 char zTmpname[MAX_PATHNAME+2];
29949 const char *zName = zPath;
29950
29951 /* Check the following statements are true:
29952 **
29953 ** (a) Exactly one of the READWRITE and READONLY flags must be set, and
@@ -29608,18 +29986,28 @@
29986 if( !pUnused ){
29987 return SQLITE_NOMEM;
29988 }
29989 }
29990 p->pUnused = pUnused;
29991
29992 /* Database filenames are double-zero terminated if they are not
29993 ** URIs with parameters. Hence, they can always be passed into
29994 ** sqlite3_uri_parameter(). */
29995 assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
29996
29997 }else if( !zName ){
29998 /* If zName is NULL, the upper layer is requesting a temp file. */
29999 assert(isDelete && !syncDir);
30000 rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
30001 if( rc!=SQLITE_OK ){
30002 return rc;
30003 }
30004 zName = zTmpname;
30005
30006 /* Generated temporary filenames are always double-zero terminated
30007 ** for use by sqlite3_uri_parameter(). */
30008 assert( zName[strlen(zName)+1]==0 );
30009 }
30010
30011 /* Determine the value of the flags parameter passed to POSIX function
30012 ** open(). These must be calculated even if open() is not called, as
30013 ** they may be stored as part of the file handle and used by the
@@ -29692,11 +30080,18 @@
30080 }
30081 if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
30082 ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
30083 }
30084 #endif
30085
30086 /* Set up appropriate ctrlFlags */
30087 if( isDelete ) ctrlFlags |= UNIXFILE_DELETE;
30088 if( isReadonly ) ctrlFlags |= UNIXFILE_RDONLY;
30089 if( noLock ) ctrlFlags |= UNIXFILE_NOLOCK;
30090 if( syncDir ) ctrlFlags |= UNIXFILE_DIRSYNC;
30091 if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
30092
30093 #if SQLITE_ENABLE_LOCKING_STYLE
30094 #if SQLITE_PREFER_PROXY_LOCKING
30095 isAutoProxy = 1;
30096 #endif
30097 if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
@@ -29722,12 +30117,11 @@
30117 goto open_finished;
30118 }
30119 useProxy = !(fsInfo.f_flags&MNT_LOCAL);
30120 }
30121 if( useProxy ){
30122 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
 
30123 if( rc==SQLITE_OK ){
30124 rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
30125 if( rc!=SQLITE_OK ){
30126 /* Use unixClose to clean up the resources added in fillInUnixFile
30127 ** and clear all the structure's references. Specifically,
@@ -29740,12 +30134,12 @@
30134 goto open_finished;
30135 }
30136 }
30137 #endif
30138
30139 rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
30140
30141 open_finished:
30142 if( rc!=SQLITE_OK ){
30143 sqlite3_free(p->pUnused);
30144 }
30145 return rc;
@@ -29766,11 +30160,11 @@
30160 SimulateIOError(return SQLITE_IOERR_DELETE);
30161 if( osUnlink(zPath)==(-1) && errno!=ENOENT ){
30162 return unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
30163 }
30164 #ifndef SQLITE_DISABLE_DIRSYNC
30165 if( (dirSync & 1)!=0 ){
30166 int fd;
30167 rc = osOpenDirectory(zPath, &fd);
30168 if( rc==SQLITE_OK ){
30169 #if OS_VXWORKS
30170 if( fsync(fd)==-1 )
@@ -30317,11 +30711,11 @@
30711 if( lockPath[i] == '/' && (i - start > 0) ){
30712 /* only mkdir if leaf dir != "." or "/" or ".." */
30713 if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30714 || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30715 buf[i]='\0';
30716 if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30717 int err=errno;
30718 if( err!=EEXIST ) {
30719 OSTRACE(("CREATELOCKPATH FAILED creating %s, "
30720 "'%s' proxy lock path=%s pid=%d\n",
30721 buf, strerror(err), lockPath, getpid()));
@@ -30412,11 +30806,11 @@
30806 dummyVfs.zName = "dummy";
30807 pUnused->fd = fd;
30808 pUnused->flags = openFlags;
30809 pNew->pUnused = pUnused;
30810
30811 rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
30812 if( rc==SQLITE_OK ){
30813 *ppFile = pNew;
30814 return SQLITE_OK;
30815 }
30816 end_create_proxy:
@@ -31353,11 +31747,11 @@
31747 };
31748 unsigned int i; /* Loop counter */
31749
31750 /* Double-check that the aSyscall[] array has been constructed
31751 ** correctly. See ticket [bb3a86e890c8e96ab] */
31752 assert( ArraySize(aSyscall)==20 );
31753
31754 /* Register all VFSes defined in the aVfs[] array */
31755 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31756 sqlite3_vfs_register(&aVfs[i], i==0);
31757 }
@@ -31389,54 +31783,18 @@
31783 ** May you find forgiveness for yourself and forgive others.
31784 ** May you share freely, never taking more than you give.
31785 **
31786 ******************************************************************************
31787 **
31788 ** This file contains code that is specific to Windows.
31789 */
31790 #if SQLITE_OS_WIN /* This file is used for Windows only */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31791
31792 #ifdef __CYGWIN__
31793 # include <sys/cygwin.h>
31794 #endif
31795
 
 
 
 
 
 
 
31796 /*
31797 ** Include code that is common to all os_*.c files
31798 */
31799 /************** Include os_common.h in the middle of os_win.c ****************/
31800 /************** Begin file os_common.h ***************************************/
@@ -31647,25 +32005,16 @@
32005
32006 /************** End of os_common.h *******************************************/
32007 /************** Continuing where we left off in os_win.c *********************/
32008
32009 /*
32010 ** Some Microsoft compilers lack this definition.
32011 */
32012 #ifndef INVALID_FILE_ATTRIBUTES
32013 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
32014 #endif
32015
 
 
 
 
 
 
 
 
 
32016 /* Forward references */
32017 typedef struct winShm winShm; /* A connection to shared-memory */
32018 typedef struct winShmNode winShmNode; /* A region of shared-memory */
32019
32020 /*
@@ -31690,25 +32039,30 @@
32039 const sqlite3_io_methods *pMethod; /*** Must be first ***/
32040 sqlite3_vfs *pVfs; /* The VFS used to open this file */
32041 HANDLE h; /* Handle for accessing the file */
32042 u8 locktype; /* Type of lock currently held on this file */
32043 short sharedLockByte; /* Randomly chosen byte used as a shared lock */
32044 u8 ctrlFlags; /* Flags. See WINFILE_* below */
32045 DWORD lastErrno; /* The Windows errno from the last I/O error */
 
32046 winShm *pShm; /* Instance of shared memory on this file */
32047 const char *zPath; /* Full pathname of this file */
32048 int szChunk; /* Chunk size configured by FCNTL_CHUNK_SIZE */
32049 #if SQLITE_OS_WINCE
32050 LPWSTR zDeleteOnClose; /* Name of file to delete when closing */
32051 HANDLE hMutex; /* Mutex used to control access to shared lock */
32052 HANDLE hShared; /* Shared memory segment used for locking */
32053 winceLock local; /* Locks obtained by this instance of winFile */
32054 winceLock *shared; /* Global shared lock memory for the file */
32055 #endif
32056 };
32057
32058 /*
32059 ** Allowed values for winFile.ctrlFlags
32060 */
32061 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
32062 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
32063
32064 /*
32065 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
32066 * various Win32 API heap functions instead of our own.
32067 */
32068 #ifdef SQLITE_WIN32_MALLOC
@@ -31776,25 +32130,17 @@
32130 static void winMemShutdown(void *pAppData);
32131
32132 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
32133 #endif /* SQLITE_WIN32_MALLOC */
32134
 
 
 
 
 
 
 
 
32135 /*
32136 ** The following variable is (normally) set once and never changes
32137 ** thereafter. It records whether the operating system is Win9x
32138 ** or WinNT.
32139 **
32140 ** 0: Operating system unknown.
32141 ** 1: Operating system is Win9x.
32142 ** 2: Operating system is WinNT.
32143 **
32144 ** In order to facilitate testing on a WinNT system, the test fixture
32145 ** can manually set this value to 1 to emulate Win98 behavior.
32146 */
@@ -31801,10 +32147,540 @@
32147 #ifdef SQLITE_TEST
32148 SQLITE_API int sqlite3_os_type = 0;
32149 #else
32150 static int sqlite3_os_type = 0;
32151 #endif
32152
32153 /*
32154 ** Many system calls are accessed through pointer-to-functions so that
32155 ** they may be overridden at runtime to facilitate fault injection during
32156 ** testing and sandboxing. The following array holds the names and pointers
32157 ** to all overrideable system calls.
32158 */
32159 #if !SQLITE_OS_WINCE
32160 # define SQLITE_WIN32_HAS_ANSI
32161 #endif
32162
32163 #if SQLITE_OS_WINCE || SQLITE_OS_WINNT
32164 # define SQLITE_WIN32_HAS_WIDE
32165 #endif
32166
32167 #ifndef SYSCALL
32168 # define SYSCALL sqlite3_syscall_ptr
32169 #endif
32170
32171 #if SQLITE_OS_WINCE
32172 /*
32173 ** These macros are necessary because Windows CE does not natively support the
32174 ** Win32 APIs LockFile, UnlockFile, and LockFileEx.
32175 */
32176
32177 # define LockFile(a,b,c,d,e) winceLockFile(&a, b, c, d, e)
32178 # define UnlockFile(a,b,c,d,e) winceUnlockFile(&a, b, c, d, e)
32179 # define LockFileEx(a,b,c,d,e,f) winceLockFileEx(&a, b, c, d, e, f)
32180
32181 /*
32182 ** These are the special syscall hacks for Windows CE. The locking related
32183 ** defines here refer to the macros defined just above.
32184 */
32185
32186 # define osAreFileApisANSI() 1
32187 # define osLockFile LockFile
32188 # define osUnlockFile UnlockFile
32189 # define osLockFileEx LockFileEx
32190 #endif
32191
32192 static struct win_syscall {
32193 const char *zName; /* Name of the sytem call */
32194 sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
32195 sqlite3_syscall_ptr pDefault; /* Default value */
32196 } aSyscall[] = {
32197 #if !SQLITE_OS_WINCE
32198 { "AreFileApisANSI", (SYSCALL)AreFileApisANSI, 0 },
32199
32200 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
32201 #else
32202 { "AreFileApisANSI", (SYSCALL)0, 0 },
32203 #endif
32204
32205 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32206 { "CharLowerW", (SYSCALL)CharLowerW, 0 },
32207 #else
32208 { "CharLowerW", (SYSCALL)0, 0 },
32209 #endif
32210
32211 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
32212
32213 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32214 { "CharUpperW", (SYSCALL)CharUpperW, 0 },
32215 #else
32216 { "CharUpperW", (SYSCALL)0, 0 },
32217 #endif
32218
32219 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
32220
32221 { "CloseHandle", (SYSCALL)CloseHandle, 0 },
32222
32223 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
32224
32225 #if defined(SQLITE_WIN32_HAS_ANSI)
32226 { "CreateFileA", (SYSCALL)CreateFileA, 0 },
32227 #else
32228 { "CreateFileA", (SYSCALL)0, 0 },
32229 #endif
32230
32231 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
32232 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
32233
32234 #if defined(SQLITE_WIN32_HAS_WIDE)
32235 { "CreateFileW", (SYSCALL)CreateFileW, 0 },
32236 #else
32237 { "CreateFileW", (SYSCALL)0, 0 },
32238 #endif
32239
32240 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
32241 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
32242
32243 { "CreateFileMapping", (SYSCALL)CreateFileMapping, 0 },
32244
32245 #define osCreateFileMapping ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
32246 DWORD,DWORD,DWORD,LPCTSTR))aSyscall[6].pCurrent)
32247
32248 #if defined(SQLITE_WIN32_HAS_WIDE)
32249 { "CreateFileMappingW", (SYSCALL)CreateFileMappingW, 0 },
32250 #else
32251 { "CreateFileMappingW", (SYSCALL)0, 0 },
32252 #endif
32253
32254 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
32255 DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
32256
32257 #if defined(SQLITE_WIN32_HAS_WIDE)
32258 { "CreateMutexW", (SYSCALL)CreateMutexW, 0 },
32259 #else
32260 { "CreateMutexW", (SYSCALL)0, 0 },
32261 #endif
32262
32263 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
32264 LPCWSTR))aSyscall[8].pCurrent)
32265
32266 #if defined(SQLITE_WIN32_HAS_ANSI)
32267 { "DeleteFileA", (SYSCALL)DeleteFileA, 0 },
32268 #else
32269 { "DeleteFileA", (SYSCALL)0, 0 },
32270 #endif
32271
32272 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
32273
32274 #if defined(SQLITE_WIN32_HAS_WIDE)
32275 { "DeleteFileW", (SYSCALL)DeleteFileW, 0 },
32276 #else
32277 { "DeleteFileW", (SYSCALL)0, 0 },
32278 #endif
32279
32280 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
32281
32282 #if SQLITE_OS_WINCE
32283 { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
32284 #else
32285 { "FileTimeToLocalFileTime", (SYSCALL)0, 0 },
32286 #endif
32287
32288 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
32289 LPFILETIME))aSyscall[11].pCurrent)
32290
32291 #if SQLITE_OS_WINCE
32292 { "FileTimeToSystemTime", (SYSCALL)FileTimeToSystemTime, 0 },
32293 #else
32294 { "FileTimeToSystemTime", (SYSCALL)0, 0 },
32295 #endif
32296
32297 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
32298 LPSYSTEMTIME))aSyscall[12].pCurrent)
32299
32300 { "FlushFileBuffers", (SYSCALL)FlushFileBuffers, 0 },
32301
32302 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
32303
32304 #if defined(SQLITE_WIN32_HAS_ANSI)
32305 { "FormatMessageA", (SYSCALL)FormatMessageA, 0 },
32306 #else
32307 { "FormatMessageA", (SYSCALL)0, 0 },
32308 #endif
32309
32310 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
32311 DWORD,va_list*))aSyscall[14].pCurrent)
32312
32313 #if defined(SQLITE_WIN32_HAS_WIDE)
32314 { "FormatMessageW", (SYSCALL)FormatMessageW, 0 },
32315 #else
32316 { "FormatMessageW", (SYSCALL)0, 0 },
32317 #endif
32318
32319 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
32320 DWORD,va_list*))aSyscall[15].pCurrent)
32321
32322 { "FreeLibrary", (SYSCALL)FreeLibrary, 0 },
32323
32324 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
32325
32326 { "GetCurrentProcessId", (SYSCALL)GetCurrentProcessId, 0 },
32327
32328 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
32329
32330 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
32331 { "GetDiskFreeSpaceA", (SYSCALL)GetDiskFreeSpaceA, 0 },
32332 #else
32333 { "GetDiskFreeSpaceA", (SYSCALL)0, 0 },
32334 #endif
32335
32336 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
32337 LPDWORD))aSyscall[18].pCurrent)
32338
32339 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32340 { "GetDiskFreeSpaceW", (SYSCALL)GetDiskFreeSpaceW, 0 },
32341 #else
32342 { "GetDiskFreeSpaceW", (SYSCALL)0, 0 },
32343 #endif
32344
32345 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
32346 LPDWORD))aSyscall[19].pCurrent)
32347
32348 #if defined(SQLITE_WIN32_HAS_ANSI)
32349 { "GetFileAttributesA", (SYSCALL)GetFileAttributesA, 0 },
32350 #else
32351 { "GetFileAttributesA", (SYSCALL)0, 0 },
32352 #endif
32353
32354 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
32355
32356 #if defined(SQLITE_WIN32_HAS_WIDE)
32357 { "GetFileAttributesW", (SYSCALL)GetFileAttributesW, 0 },
32358 #else
32359 { "GetFileAttributesW", (SYSCALL)0, 0 },
32360 #endif
32361
32362 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
32363
32364 #if defined(SQLITE_WIN32_HAS_WIDE)
32365 { "GetFileAttributesExW", (SYSCALL)GetFileAttributesExW, 0 },
32366 #else
32367 { "GetFileAttributesExW", (SYSCALL)0, 0 },
32368 #endif
32369
32370 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
32371 LPVOID))aSyscall[22].pCurrent)
32372
32373 { "GetFileSize", (SYSCALL)GetFileSize, 0 },
32374
32375 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
32376
32377 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
32378 { "GetFullPathNameA", (SYSCALL)GetFullPathNameA, 0 },
32379 #else
32380 { "GetFullPathNameA", (SYSCALL)0, 0 },
32381 #endif
32382
32383 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
32384 LPSTR*))aSyscall[24].pCurrent)
32385
32386 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
32387 { "GetFullPathNameW", (SYSCALL)GetFullPathNameW, 0 },
32388 #else
32389 { "GetFullPathNameW", (SYSCALL)0, 0 },
32390 #endif
32391
32392 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
32393 LPWSTR*))aSyscall[25].pCurrent)
32394
32395 { "GetLastError", (SYSCALL)GetLastError, 0 },
32396
32397 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
32398
32399 #if SQLITE_OS_WINCE
32400 /* The GetProcAddressA() routine is only available on Windows CE. */
32401 { "GetProcAddressA", (SYSCALL)GetProcAddressA, 0 },
32402 #else
32403 /* All other Windows platforms expect GetProcAddress() to take
32404 ** an ANSI string regardless of the _UNICODE setting */
32405 { "GetProcAddressA", (SYSCALL)GetProcAddress, 0 },
32406 #endif
32407
32408 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
32409 LPCSTR))aSyscall[27].pCurrent)
32410
32411 { "GetSystemInfo", (SYSCALL)GetSystemInfo, 0 },
32412
32413 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
32414
32415 { "GetSystemTime", (SYSCALL)GetSystemTime, 0 },
32416
32417 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
32418
32419 #if !SQLITE_OS_WINCE
32420 { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
32421 #else
32422 { "GetSystemTimeAsFileTime", (SYSCALL)0, 0 },
32423 #endif
32424
32425 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
32426 LPFILETIME))aSyscall[30].pCurrent)
32427
32428 #if defined(SQLITE_WIN32_HAS_ANSI)
32429 { "GetTempPathA", (SYSCALL)GetTempPathA, 0 },
32430 #else
32431 { "GetTempPathA", (SYSCALL)0, 0 },
32432 #endif
32433
32434 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
32435
32436 #if defined(SQLITE_WIN32_HAS_WIDE)
32437 { "GetTempPathW", (SYSCALL)GetTempPathW, 0 },
32438 #else
32439 { "GetTempPathW", (SYSCALL)0, 0 },
32440 #endif
32441
32442 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
32443
32444 { "GetTickCount", (SYSCALL)GetTickCount, 0 },
32445
32446 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
32447
32448 #if defined(SQLITE_WIN32_HAS_ANSI)
32449 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
32450 #else
32451 { "GetVersionExA", (SYSCALL)0, 0 },
32452 #endif
32453
32454 #define osGetVersionExA ((BOOL(WINAPI*)( \
32455 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
32456
32457 { "HeapAlloc", (SYSCALL)HeapAlloc, 0 },
32458
32459 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
32460 SIZE_T))aSyscall[35].pCurrent)
32461
32462 { "HeapCreate", (SYSCALL)HeapCreate, 0 },
32463
32464 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
32465 SIZE_T))aSyscall[36].pCurrent)
32466
32467 { "HeapDestroy", (SYSCALL)HeapDestroy, 0 },
32468
32469 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[37].pCurrent)
32470
32471 { "HeapFree", (SYSCALL)HeapFree, 0 },
32472
32473 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[38].pCurrent)
32474
32475 { "HeapReAlloc", (SYSCALL)HeapReAlloc, 0 },
32476
32477 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
32478 SIZE_T))aSyscall[39].pCurrent)
32479
32480 { "HeapSize", (SYSCALL)HeapSize, 0 },
32481
32482 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
32483 LPCVOID))aSyscall[40].pCurrent)
32484
32485 { "HeapValidate", (SYSCALL)HeapValidate, 0 },
32486
32487 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
32488 LPCVOID))aSyscall[41].pCurrent)
32489
32490 #if defined(SQLITE_WIN32_HAS_ANSI)
32491 { "LoadLibraryA", (SYSCALL)LoadLibraryA, 0 },
32492 #else
32493 { "LoadLibraryA", (SYSCALL)0, 0 },
32494 #endif
32495
32496 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[42].pCurrent)
32497
32498 #if defined(SQLITE_WIN32_HAS_WIDE)
32499 { "LoadLibraryW", (SYSCALL)LoadLibraryW, 0 },
32500 #else
32501 { "LoadLibraryW", (SYSCALL)0, 0 },
32502 #endif
32503
32504 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[43].pCurrent)
32505
32506 { "LocalFree", (SYSCALL)LocalFree, 0 },
32507
32508 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[44].pCurrent)
32509
32510 #if !SQLITE_OS_WINCE
32511 { "LockFile", (SYSCALL)LockFile, 0 },
32512
32513 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32514 DWORD))aSyscall[45].pCurrent)
32515 #else
32516 { "LockFile", (SYSCALL)0, 0 },
32517 #endif
32518
32519 #if !SQLITE_OS_WINCE
32520 { "LockFileEx", (SYSCALL)LockFileEx, 0 },
32521
32522 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
32523 LPOVERLAPPED))aSyscall[46].pCurrent)
32524 #else
32525 { "LockFileEx", (SYSCALL)0, 0 },
32526 #endif
32527
32528 { "MapViewOfFile", (SYSCALL)MapViewOfFile, 0 },
32529
32530 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32531 SIZE_T))aSyscall[47].pCurrent)
32532
32533 { "MultiByteToWideChar", (SYSCALL)MultiByteToWideChar, 0 },
32534
32535 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
32536 int))aSyscall[48].pCurrent)
32537
32538 { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
32539
32540 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
32541 LARGE_INTEGER*))aSyscall[49].pCurrent)
32542
32543 { "ReadFile", (SYSCALL)ReadFile, 0 },
32544
32545 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
32546 LPOVERLAPPED))aSyscall[50].pCurrent)
32547
32548 { "SetEndOfFile", (SYSCALL)SetEndOfFile, 0 },
32549
32550 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[51].pCurrent)
32551
32552 { "SetFilePointer", (SYSCALL)SetFilePointer, 0 },
32553
32554 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
32555 DWORD))aSyscall[52].pCurrent)
32556
32557 { "Sleep", (SYSCALL)Sleep, 0 },
32558
32559 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[53].pCurrent)
32560
32561 { "SystemTimeToFileTime", (SYSCALL)SystemTimeToFileTime, 0 },
32562
32563 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
32564 LPFILETIME))aSyscall[54].pCurrent)
32565
32566 #if !SQLITE_OS_WINCE
32567 { "UnlockFile", (SYSCALL)UnlockFile, 0 },
32568
32569 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32570 DWORD))aSyscall[55].pCurrent)
32571 #else
32572 { "UnlockFile", (SYSCALL)0, 0 },
32573 #endif
32574
32575 #if !SQLITE_OS_WINCE
32576 { "UnlockFileEx", (SYSCALL)UnlockFileEx, 0 },
32577
32578 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32579 LPOVERLAPPED))aSyscall[56].pCurrent)
32580 #else
32581 { "UnlockFileEx", (SYSCALL)0, 0 },
32582 #endif
32583
32584 { "UnmapViewOfFile", (SYSCALL)UnmapViewOfFile, 0 },
32585
32586 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[57].pCurrent)
32587
32588 { "WideCharToMultiByte", (SYSCALL)WideCharToMultiByte, 0 },
32589
32590 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
32591 LPCSTR,LPBOOL))aSyscall[58].pCurrent)
32592
32593 { "WriteFile", (SYSCALL)WriteFile, 0 },
32594
32595 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
32596 LPOVERLAPPED))aSyscall[59].pCurrent)
32597
32598 }; /* End of the overrideable system calls */
32599
32600 /*
32601 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32602 ** "win32" VFSes. Return SQLITE_OK opon successfully updating the
32603 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32604 ** system call named zName.
32605 */
32606 static int winSetSystemCall(
32607 sqlite3_vfs *pNotUsed, /* The VFS pointer. Not used */
32608 const char *zName, /* Name of system call to override */
32609 sqlite3_syscall_ptr pNewFunc /* Pointer to new system call value */
32610 ){
32611 unsigned int i;
32612 int rc = SQLITE_NOTFOUND;
32613
32614 UNUSED_PARAMETER(pNotUsed);
32615 if( zName==0 ){
32616 /* If no zName is given, restore all system calls to their default
32617 ** settings and return NULL
32618 */
32619 rc = SQLITE_OK;
32620 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32621 if( aSyscall[i].pDefault ){
32622 aSyscall[i].pCurrent = aSyscall[i].pDefault;
32623 }
32624 }
32625 }else{
32626 /* If zName is specified, operate on only the one system call
32627 ** specified.
32628 */
32629 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32630 if( strcmp(zName, aSyscall[i].zName)==0 ){
32631 if( aSyscall[i].pDefault==0 ){
32632 aSyscall[i].pDefault = aSyscall[i].pCurrent;
32633 }
32634 rc = SQLITE_OK;
32635 if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32636 aSyscall[i].pCurrent = pNewFunc;
32637 break;
32638 }
32639 }
32640 }
32641 return rc;
32642 }
32643
32644 /*
32645 ** Return the value of a system call. Return NULL if zName is not a
32646 ** recognized system call name. NULL is also returned if the system call
32647 ** is currently undefined.
32648 */
32649 static sqlite3_syscall_ptr winGetSystemCall(
32650 sqlite3_vfs *pNotUsed,
32651 const char *zName
32652 ){
32653 unsigned int i;
32654
32655 UNUSED_PARAMETER(pNotUsed);
32656 for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32657 if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32658 }
32659 return 0;
32660 }
32661
32662 /*
32663 ** Return the name of the first system call after zName. If zName==NULL
32664 ** then return the name of the first system call. Return NULL if zName
32665 ** is the last system call or if zName is not the name of a valid
32666 ** system call.
32667 */
32668 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
32669 int i = -1;
32670
32671 UNUSED_PARAMETER(p);
32672 if( zName ){
32673 for(i=0; i<ArraySize(aSyscall)-1; i++){
32674 if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32675 }
32676 }
32677 for(i++; i<ArraySize(aSyscall); i++){
32678 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32679 }
32680 return 0;
32681 }
32682
32683 /*
32684 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
32685 ** or WinCE. Return false (zero) for Win95, Win98, or WinME.
32686 **
@@ -31818,13 +32694,13 @@
32694 #if SQLITE_OS_WINCE
32695 # define isNT() (1)
32696 #else
32697 static int isNT(void){
32698 if( sqlite3_os_type==0 ){
32699 OSVERSIONINFOA sInfo;
32700 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32701 osGetVersionExA(&sInfo);
32702 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
32703 }
32704 return sqlite3_os_type==2;
32705 }
32706 #endif /* SQLITE_OS_WINCE */
@@ -31840,17 +32716,17 @@
32716 winMemAssertMagic();
32717 hHeap = winMemGetHeap();
32718 assert( hHeap!=0 );
32719 assert( hHeap!=INVALID_HANDLE_VALUE );
32720 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32721 assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32722 #endif
32723 assert( nBytes>=0 );
32724 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32725 if( !p ){
32726 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
32727 nBytes, osGetLastError(), (void*)hHeap);
32728 }
32729 return p;
32730 }
32731
32732 /*
@@ -31862,16 +32738,16 @@
32738 winMemAssertMagic();
32739 hHeap = winMemGetHeap();
32740 assert( hHeap!=0 );
32741 assert( hHeap!=INVALID_HANDLE_VALUE );
32742 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32743 assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32744 #endif
32745 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
32746 if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
32747 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
32748 pPrior, osGetLastError(), (void*)hHeap);
32749 }
32750 }
32751
32752 /*
32753 ** Change the size of an existing memory allocation
@@ -31883,22 +32759,22 @@
32759 winMemAssertMagic();
32760 hHeap = winMemGetHeap();
32761 assert( hHeap!=0 );
32762 assert( hHeap!=INVALID_HANDLE_VALUE );
32763 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32764 assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32765 #endif
32766 assert( nBytes>=0 );
32767 if( !pPrior ){
32768 p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32769 }else{
32770 p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
32771 }
32772 if( !p ){
32773 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
32774 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
32775 (void*)hHeap);
32776 }
32777 return p;
32778 }
32779
32780 /*
@@ -31911,17 +32787,17 @@
32787 winMemAssertMagic();
32788 hHeap = winMemGetHeap();
32789 assert( hHeap!=0 );
32790 assert( hHeap!=INVALID_HANDLE_VALUE );
32791 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32792 assert ( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32793 #endif
32794 if( !p ) return 0;
32795 n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
32796 if( n==(SIZE_T)-1 ){
32797 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
32798 p, osGetLastError(), (void*)hHeap);
32799 return 0;
32800 }
32801 return (int)n;
32802 }
32803
@@ -31939,26 +32815,26 @@
32815 winMemData *pWinMemData = (winMemData *)pAppData;
32816
32817 if( !pWinMemData ) return SQLITE_ERROR;
32818 assert( pWinMemData->magic==WINMEM_MAGIC );
32819 if( !pWinMemData->hHeap ){
32820 pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
32821 SQLITE_WIN32_HEAP_INIT_SIZE,
32822 SQLITE_WIN32_HEAP_MAX_SIZE);
32823 if( !pWinMemData->hHeap ){
32824 sqlite3_log(SQLITE_NOMEM,
32825 "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
32826 osGetLastError(), SQLITE_WIN32_HEAP_FLAGS,
32827 SQLITE_WIN32_HEAP_INIT_SIZE, SQLITE_WIN32_HEAP_MAX_SIZE);
32828 return SQLITE_NOMEM;
32829 }
32830 pWinMemData->bOwned = TRUE;
32831 }
32832 assert( pWinMemData->hHeap!=0 );
32833 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32834 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32835 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32836 #endif
32837 return SQLITE_OK;
32838 }
32839
32840 /*
@@ -31969,16 +32845,16 @@
32845
32846 if( !pWinMemData ) return;
32847 if( pWinMemData->hHeap ){
32848 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32849 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
32850 assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32851 #endif
32852 if( pWinMemData->bOwned ){
32853 if( !osHeapDestroy(pWinMemData->hHeap) ){
32854 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
32855 osGetLastError(), (void*)pWinMemData->hHeap);
32856 }
32857 pWinMemData->bOwned = FALSE;
32858 }
32859 pWinMemData->hHeap = NULL;
32860 }
@@ -32010,197 +32886,219 @@
32886 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
32887 }
32888 #endif /* SQLITE_WIN32_MALLOC */
32889
32890 /*
32891 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
32892 **
32893 ** Space to hold the returned string is obtained from malloc.
32894 */
32895 static LPWSTR utf8ToUnicode(const char *zFilename){
32896 int nChar;
32897 LPWSTR zWideFilename;
32898
32899 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32900 if( nChar==0 ){
32901 return 0;
32902 }
32903 zWideFilename = sqlite3_malloc( nChar*sizeof(zWideFilename[0]) );
32904 if( zWideFilename==0 ){
32905 return 0;
32906 }
32907 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
32908 nChar);
32909 if( nChar==0 ){
32910 sqlite3_free(zWideFilename);
32911 zWideFilename = 0;
32912 }
32913 return zWideFilename;
32914 }
32915
32916 /*
32917 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
32918 ** obtained from sqlite3_malloc().
32919 */
32920 static char *unicodeToUtf8(LPCWSTR zWideFilename){
32921 int nByte;
32922 char *zFilename;
32923
32924 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32925 if( nByte == 0 ){
32926 return 0;
32927 }
32928 zFilename = sqlite3_malloc( nByte );
32929 if( zFilename==0 ){
32930 return 0;
32931 }
32932 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32933 0, 0);
32934 if( nByte == 0 ){
32935 sqlite3_free(zFilename);
32936 zFilename = 0;
32937 }
32938 return zFilename;
32939 }
32940
32941 /*
32942 ** Convert an ANSI string to Microsoft Unicode, based on the
32943 ** current codepage settings for file apis.
32944 **
32945 ** Space to hold the returned string is obtained
32946 ** from sqlite3_malloc.
32947 */
32948 static LPWSTR mbcsToUnicode(const char *zFilename){
32949 int nByte;
32950 LPWSTR zMbcsFilename;
32951 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32952
32953 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
32954 0)*sizeof(WCHAR);
32955 if( nByte==0 ){
32956 return 0;
32957 }
32958 zMbcsFilename = sqlite3_malloc( nByte*sizeof(zMbcsFilename[0]) );
32959 if( zMbcsFilename==0 ){
32960 return 0;
32961 }
32962 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
32963 nByte);
32964 if( nByte==0 ){
32965 sqlite3_free(zMbcsFilename);
32966 zMbcsFilename = 0;
32967 }
32968 return zMbcsFilename;
32969 }
32970
32971 /*
32972 ** Convert Microsoft Unicode to multi-byte character string, based on the
32973 ** user's ANSI codepage.
32974 **
32975 ** Space to hold the returned string is obtained from
32976 ** sqlite3_malloc().
32977 */
32978 static char *unicodeToMbcs(LPCWSTR zWideFilename){
32979 int nByte;
32980 char *zFilename;
32981 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32982
32983 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32984 if( nByte == 0 ){
32985 return 0;
32986 }
32987 zFilename = sqlite3_malloc( nByte );
32988 if( zFilename==0 ){
32989 return 0;
32990 }
32991 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
32992 nByte, 0, 0);
32993 if( nByte == 0 ){
32994 sqlite3_free(zFilename);
32995 zFilename = 0;
32996 }
32997 return zFilename;
32998 }
32999
33000 /*
33001 ** Convert multibyte character string to UTF-8. Space to hold the
33002 ** returned string is obtained from sqlite3_malloc().
33003 */
33004 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
33005 char *zFilenameUtf8;
33006 LPWSTR zTmpWide;
33007
33008 zTmpWide = mbcsToUnicode(zFilename);
33009 if( zTmpWide==0 ){
33010 return 0;
33011 }
33012 zFilenameUtf8 = unicodeToUtf8(zTmpWide);
33013 sqlite3_free(zTmpWide);
33014 return zFilenameUtf8;
33015 }
33016
33017 /*
33018 ** Convert UTF-8 to multibyte character string. Space to hold the
33019 ** returned string is obtained from sqlite3_malloc().
33020 */
33021 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
33022 char *zFilenameMbcs;
33023 LPWSTR zTmpWide;
33024
33025 zTmpWide = utf8ToUnicode(zFilename);
33026 if( zTmpWide==0 ){
33027 return 0;
33028 }
33029 zFilenameMbcs = unicodeToMbcs(zTmpWide);
33030 sqlite3_free(zTmpWide);
33031 return zFilenameMbcs;
33032 }
33033
33034
33035 /*
33036 ** The return value of getLastErrorMsg
33037 ** is zero if the error message fits in the buffer, or non-zero
33038 ** otherwise (if the message was truncated).
33039 */
33040 static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
33041 /* FormatMessage returns 0 on failure. Otherwise it
33042 ** returns the number of TCHARs written to the output
33043 ** buffer, excluding the terminating null char.
33044 */
 
33045 DWORD dwLen = 0;
33046 char *zOut = 0;
33047
33048 if( isNT() ){
33049 LPWSTR zTempWide = NULL;
33050 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33051 FORMAT_MESSAGE_FROM_SYSTEM |
33052 FORMAT_MESSAGE_IGNORE_INSERTS,
33053 NULL,
33054 lastErrno,
33055 0,
33056 (LPWSTR) &zTempWide,
33057 0,
33058 0);
33059 if( dwLen > 0 ){
33060 /* allocate a buffer and convert to UTF8 */
33061 sqlite3BeginBenignMalloc();
33062 zOut = unicodeToUtf8(zTempWide);
33063 sqlite3EndBenignMalloc();
33064 /* free the system buffer allocated by FormatMessage */
33065 osLocalFree(zTempWide);
33066 }
33067 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33068 ** Since the ANSI version of these Windows API do not exist for WINCE,
33069 ** it's important to not reference them for WINCE builds.
33070 */
33071 #if SQLITE_OS_WINCE==0
33072 }else{
33073 char *zTemp = NULL;
33074 dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33075 FORMAT_MESSAGE_FROM_SYSTEM |
33076 FORMAT_MESSAGE_IGNORE_INSERTS,
33077 NULL,
33078 lastErrno,
33079 0,
33080 (LPSTR) &zTemp,
33081 0,
33082 0);
33083 if( dwLen > 0 ){
33084 /* allocate a buffer and convert to UTF8 */
33085 sqlite3BeginBenignMalloc();
33086 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33087 sqlite3EndBenignMalloc();
33088 /* free the system buffer allocated by FormatMessage */
33089 osLocalFree(zTemp);
33090 }
33091 #endif
33092 }
33093 if( 0 == dwLen ){
33094 sqlite3_snprintf(nBuf, zBuf, "OsError 0x%x (%u)", lastErrno, lastErrno);
33095 }else{
33096 /* copy a maximum of nBuf chars to output buffer */
33097 sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
33098 /* free the UTF8 buffer */
33099 sqlite3_free(zOut);
33100 }
33101 return 0;
33102 }
33103
33104 /*
@@ -32216,30 +33114,30 @@
33114 ** The first argument passed to the macro should be the error code that
33115 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33116 ** The two subsequent arguments should be the name of the OS function that
33117 ** failed and the the associated file-system path, if any.
33118 */
33119 #define winLogError(a,b,c,d) winLogErrorAtLine(a,b,c,d,__LINE__)
33120 static int winLogErrorAtLine(
33121 int errcode, /* SQLite error code */
33122 DWORD lastErrno, /* Win32 last error */
33123 const char *zFunc, /* Name of OS function that failed */
33124 const char *zPath, /* File path associated with error */
33125 int iLine /* Source line number where error occurred */
33126 ){
33127 char zMsg[500]; /* Human readable error text */
33128 int i; /* Loop counter */
 
33129
33130 zMsg[0] = 0;
33131 getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
33132 assert( errcode!=SQLITE_OK );
33133 if( zPath==0 ) zPath = "";
33134 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
33135 zMsg[i] = 0;
33136 sqlite3_log(errcode,
33137 "os_win.c:%d: (%d) %s(%s) - %s",
33138 iLine, lastErrno, zFunc, zPath, zMsg
33139 );
33140
33141 return errcode;
33142 }
33143
@@ -32261,23 +33159,28 @@
33159 /*
33160 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
33161 ** to see if it should be retried. Return TRUE to retry. Return FALSE
33162 ** to give up with an error.
33163 */
33164 static int retryIoerr(int *pnRetry, DWORD *pError){
33165 DWORD e = osGetLastError();
33166 if( *pnRetry>=win32IoerrRetry ){
33167 if( pError ){
33168 *pError = e;
33169 }
33170 return 0;
33171 }
 
33172 if( e==ERROR_ACCESS_DENIED ||
33173 e==ERROR_LOCK_VIOLATION ||
33174 e==ERROR_SHARING_VIOLATION ){
33175 osSleep(win32IoerrRetryDelay*(1+*pnRetry));
33176 ++*pnRetry;
33177 return 1;
33178 }
33179 if( pError ){
33180 *pError = e;
33181 }
33182 return 0;
33183 }
33184
33185 /*
33186 ** Log a I/O error retry episode.
@@ -32294,11 +33197,11 @@
33197 #if SQLITE_OS_WINCE
33198 /*************************************************************************
33199 ** This section contains code for WinCE only.
33200 */
33201 /*
33202 ** Windows CE does not have a localtime() function. So create a
33203 ** substitute.
33204 */
33205 /* #include <time.h> */
33206 struct tm *__cdecl localtime(const time_t *t)
33207 {
@@ -32308,12 +33211,12 @@
33211 sqlite3_int64 t64;
33212 t64 = *t;
33213 t64 = (t64 + 11644473600)*10000000;
33214 uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
33215 uTm.dwHighDateTime= (DWORD)(t64 >> 32);
33216 osFileTimeToLocalFileTime(&uTm,&lTm);
33217 osFileTimeToSystemTime(&lTm,&pTm);
33218 y.tm_year = pTm.wYear - 1900;
33219 y.tm_mon = pTm.wMonth - 1;
33220 y.tm_wday = pTm.wDayOfWeek;
33221 y.tm_mday = pTm.wDay;
33222 y.tm_hour = pTm.wHour;
@@ -32320,17 +33223,10 @@
33223 y.tm_min = pTm.wMinute;
33224 y.tm_sec = pTm.wSecond;
33225 return &y;
33226 }
33227
 
 
 
 
 
 
 
33228 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
33229
33230 /*
33231 ** Acquire a lock on the handle h
33232 */
@@ -32348,30 +33244,36 @@
33244 /*
33245 ** Create the mutex and shared memory used for locking in the file
33246 ** descriptor pFile
33247 */
33248 static BOOL winceCreateLock(const char *zFilename, winFile *pFile){
33249 LPWSTR zTok;
33250 LPWSTR zName;
33251 BOOL bInit = TRUE;
33252
33253 zName = utf8ToUnicode(zFilename);
33254 if( zName==0 ){
33255 /* out of memory */
33256 return FALSE;
33257 }
33258
33259 /* Initialize the local lockdata */
33260 memset(&pFile->local, 0, sizeof(pFile->local));
33261
33262 /* Replace the backslashes from the filename and lowercase it
33263 ** to derive a mutex name. */
33264 zTok = osCharLowerW(zName);
33265 for (;*zTok;zTok++){
33266 if (*zTok == '\\') *zTok = '_';
33267 }
33268
33269 /* Create/open the named mutex */
33270 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
33271 if (!pFile->hMutex){
33272 pFile->lastErrno = osGetLastError();
33273 winLogError(SQLITE_ERROR, pFile->lastErrno, "winceCreateLock1", zFilename);
33274 sqlite3_free(zName);
33275 return FALSE;
33276 }
33277
33278 /* Acquire the mutex before continuing */
33279 winceMutexAcquire(pFile->hMutex);
@@ -32378,47 +33280,48 @@
33280
33281 /* Since the names of named mutexes, semaphores, file mappings etc are
33282 ** case-sensitive, take advantage of that by uppercasing the mutex name
33283 ** and using that as the shared filemapping name.
33284 */
33285 osCharUpperW(zName);
33286 pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
33287 PAGE_READWRITE, 0, sizeof(winceLock),
33288 zName);
33289
33290 /* Set a flag that indicates we're the first to create the memory so it
33291 ** must be zero-initialized */
33292 if (osGetLastError() == ERROR_ALREADY_EXISTS){
33293 bInit = FALSE;
33294 }
33295
33296 sqlite3_free(zName);
33297
33298 /* If we succeeded in making the shared memory handle, map it. */
33299 if (pFile->hShared){
33300 pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
33301 FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
33302 /* If mapping failed, close the shared memory handle and erase it */
33303 if (!pFile->shared){
33304 pFile->lastErrno = osGetLastError();
33305 winLogError(SQLITE_ERROR, pFile->lastErrno,
33306 "winceCreateLock2", zFilename);
33307 osCloseHandle(pFile->hShared);
33308 pFile->hShared = NULL;
33309 }
33310 }
33311
33312 /* If shared memory could not be created, then close the mutex and fail */
33313 if (pFile->hShared == NULL){
33314 winceMutexRelease(pFile->hMutex);
33315 osCloseHandle(pFile->hMutex);
33316 pFile->hMutex = NULL;
33317 return FALSE;
33318 }
33319
33320 /* Initialize the shared memory if we're supposed to */
33321 if (bInit) {
33322 memset(pFile->shared, 0, sizeof(winceLock));
33323 }
33324
33325 winceMutexRelease(pFile->hMutex);
33326 return TRUE;
33327 }
@@ -32445,22 +33348,22 @@
33348 if (pFile->local.bExclusive){
33349 pFile->shared->bExclusive = FALSE;
33350 }
33351
33352 /* De-reference and close our copy of the shared memory handle */
33353 osUnmapViewOfFile(pFile->shared);
33354 osCloseHandle(pFile->hShared);
33355
33356 /* Done with the mutex */
33357 winceMutexRelease(pFile->hMutex);
33358 osCloseHandle(pFile->hMutex);
33359 pFile->hMutex = NULL;
33360 }
33361 }
33362
33363 /*
33364 ** An implementation of the LockFile() API of Windows for CE
33365 */
33366 static BOOL winceLockFile(
33367 HANDLE *phFile,
33368 DWORD dwFileOffsetLow,
33369 DWORD dwFileOffsetHigh,
@@ -32520,11 +33423,11 @@
33423 winceMutexRelease(pFile->hMutex);
33424 return bReturn;
33425 }
33426
33427 /*
33428 ** An implementation of the UnlockFile API of Windows for CE
33429 */
33430 static BOOL winceUnlockFile(
33431 HANDLE *phFile,
33432 DWORD dwFileOffsetLow,
33433 DWORD dwFileOffsetHigh,
@@ -32582,11 +33485,11 @@
33485 winceMutexRelease(pFile->hMutex);
33486 return bReturn;
33487 }
33488
33489 /*
33490 ** An implementation of the LockFileEx() API of Windows for CE
33491 */
33492 static BOOL winceLockFileEx(
33493 HANDLE *phFile,
33494 DWORD dwFlags,
33495 DWORD dwReserved,
@@ -32615,11 +33518,11 @@
33518 ** The next group of routines implement the I/O methods specified
33519 ** by the sqlite3_io_methods object.
33520 ******************************************************************************/
33521
33522 /*
33523 ** Some Microsoft compilers lack this definition.
33524 */
33525 #ifndef INVALID_SET_FILE_POINTER
33526 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
33527 #endif
33528
@@ -32630,10 +33533,11 @@
33533 */
33534 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
33535 LONG upperBits; /* Most sig. 32 bits of new offset */
33536 LONG lowerBits; /* Least sig. 32 bits of new offset */
33537 DWORD dwRet; /* Value returned by SetFilePointer() */
33538 DWORD lastErrno; /* Value returned by GetLastError() */
33539
33540 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
33541 lowerBits = (LONG)(iOffset & 0xffffffff);
33542
33543 /* API oddity: If successful, SetFilePointer() returns a dword
@@ -32641,14 +33545,17 @@
33545 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
33546 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
33547 ** whether an error has actually occured, it is also necessary to call
33548 ** GetLastError().
33549 */
33550 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
33551
33552 if( (dwRet==INVALID_SET_FILE_POINTER
33553 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
33554 pFile->lastErrno = lastErrno;
33555 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33556 "seekWinFile", pFile->zPath);
33557 return 1;
33558 }
33559
33560 return 0;
33561 }
@@ -32655,11 +33562,11 @@
33562
33563 /*
33564 ** Close a file.
33565 **
33566 ** It is reported that an attempt to close a handle might sometimes
33567 ** fail. This is a very unreasonable result, but Windows is notorious
33568 ** for being unreasonable so I do not doubt that it might happen. If
33569 ** the close fails, we pause for 100 milliseconds and try again. As
33570 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
33571 ** giving up and returning an error.
33572 */
@@ -32670,32 +33577,33 @@
33577
33578 assert( id!=0 );
33579 assert( pFile->pShm==0 );
33580 OSTRACE(("CLOSE %d\n", pFile->h));
33581 do{
33582 rc = osCloseHandle(pFile->h);
33583 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
33584 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (osSleep(100), 1) );
33585 #if SQLITE_OS_WINCE
33586 #define WINCE_DELETION_ATTEMPTS 3
33587 winceDestroyLock(pFile);
33588 if( pFile->zDeleteOnClose ){
33589 int cnt = 0;
33590 while(
33591 osDeleteFileW(pFile->zDeleteOnClose)==0
33592 && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
33593 && cnt++ < WINCE_DELETION_ATTEMPTS
33594 ){
33595 osSleep(100); /* Wait a little before trying again */
33596 }
33597 sqlite3_free(pFile->zDeleteOnClose);
33598 }
33599 #endif
33600 OSTRACE(("CLOSE %d %s\n", pFile->h, rc ? "ok" : "failed"));
33601 OpenCounter(-1);
33602 return rc ? SQLITE_OK
33603 : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
33604 "winClose", pFile->zPath);
33605 }
33606
33607 /*
33608 ** Read data from a file into a buffer. Return SQLITE_OK if all
33609 ** bytes were read successfully and SQLITE_IOERR if anything goes
@@ -32716,14 +33624,16 @@
33624 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
33625
33626 if( seekWinFile(pFile, offset) ){
33627 return SQLITE_FULL;
33628 }
33629 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
33630 DWORD lastErrno;
33631 if( retryIoerr(&nRetry, &lastErrno) ) continue;
33632 pFile->lastErrno = lastErrno;
33633 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
33634 "winRead", pFile->zPath);
33635 }
33636 logIoerr(nRetry);
33637 if( nRead<(DWORD)amt ){
33638 /* Unread parts of the buffer must be zero-filled */
33639 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
@@ -32757,32 +33667,34 @@
33667 rc = seekWinFile(pFile, offset);
33668 if( rc==0 ){
33669 u8 *aRem = (u8 *)pBuf; /* Data yet to be written */
33670 int nRem = amt; /* Number of bytes yet to be written */
33671 DWORD nWrite; /* Bytes written by each WriteFile() call */
33672 DWORD lastErrno = NO_ERROR; /* Value returned by GetLastError() */
33673
33674 while( nRem>0 ){
33675 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33676 if( retryIoerr(&nRetry, &lastErrno) ) continue;
33677 break;
33678 }
33679 if( nWrite<=0 ) break;
33680 aRem += nWrite;
33681 nRem -= nWrite;
33682 }
33683 if( nRem>0 ){
33684 pFile->lastErrno = lastErrno;
33685 rc = 1;
33686 }
33687 }
33688
33689 if( rc ){
33690 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
33691 || ( pFile->lastErrno==ERROR_DISK_FULL )){
33692 return SQLITE_FULL;
33693 }
33694 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33695 "winWrite", pFile->zPath);
33696 }else{
33697 logIoerr(nRetry);
33698 }
33699 return SQLITE_OK;
33700 }
@@ -32808,14 +33720,16 @@
33720 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33721 }
33722
33723 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33724 if( seekWinFile(pFile, nByte) ){
33725 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33726 "winTruncate1", pFile->zPath);
33727 }else if( 0==osSetEndOfFile(pFile->h) ){
33728 pFile->lastErrno = osGetLastError();
33729 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33730 "winTruncate2", pFile->zPath);
33731 }
33732
33733 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
33734 return rc;
33735 }
@@ -32876,17 +33790,18 @@
33790 ** no-op
33791 */
33792 #ifdef SQLITE_NO_SYNC
33793 return SQLITE_OK;
33794 #else
33795 rc = osFlushFileBuffers(pFile->h);
33796 SimulateIOError( rc=FALSE );
33797 if( rc ){
33798 return SQLITE_OK;
33799 }else{
33800 pFile->lastErrno = osGetLastError();
33801 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33802 "winSync", pFile->zPath);
33803 }
33804 #endif
33805 }
33806
33807 /*
@@ -32894,20 +33809,21 @@
33809 */
33810 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
33811 DWORD upperBits;
33812 DWORD lowerBits;
33813 winFile *pFile = (winFile*)id;
33814 DWORD lastErrno;
33815
33816 assert( id!=0 );
33817 SimulateIOError(return SQLITE_IOERR_FSTAT);
33818 lowerBits = osGetFileSize(pFile->h, &upperBits);
33819 if( (lowerBits == INVALID_FILE_SIZE)
33820 && ((lastErrno = osGetLastError())!=NO_ERROR) )
33821 {
33822 pFile->lastErrno = lastErrno;
33823 return winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33824 "winFileSize", pFile->zPath);
33825 }
33826 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
33827 return SQLITE_OK;
33828 }
33829
@@ -32919,33 +33835,33 @@
33835 #endif
33836
33837 /*
33838 ** Acquire a reader lock.
33839 ** Different API routines are called depending on whether or not this
33840 ** is Win9x or WinNT.
33841 */
33842 static int getReadLock(winFile *pFile){
33843 int res;
33844 if( isNT() ){
33845 OVERLAPPED ovlp;
33846 ovlp.Offset = SHARED_FIRST;
33847 ovlp.OffsetHigh = 0;
33848 ovlp.hEvent = 0;
33849 res = osLockFileEx(pFile->h, LOCKFILE_FAIL_IMMEDIATELY,
33850 0, SHARED_SIZE, 0, &ovlp);
33851 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33852 */
33853 #if SQLITE_OS_WINCE==0
33854 }else{
33855 int lk;
33856 sqlite3_randomness(sizeof(lk), &lk);
33857 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
33858 res = osLockFile(pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
33859 #endif
33860 }
33861 if( res == 0 ){
33862 pFile->lastErrno = osGetLastError();
33863 /* No need to log a failure to lock */
33864 }
33865 return res;
33866 }
33867
@@ -32952,22 +33868,24 @@
33868 /*
33869 ** Undo a readlock
33870 */
33871 static int unlockReadLock(winFile *pFile){
33872 int res;
33873 DWORD lastErrno;
33874 if( isNT() ){
33875 res = osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33876 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
33877 */
33878 #if SQLITE_OS_WINCE==0
33879 }else{
33880 res = osUnlockFile(pFile->h, SHARED_FIRST + pFile->sharedLockByte, 0, 1, 0);
33881 #endif
33882 }
33883 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33884 pFile->lastErrno = lastErrno;
33885 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33886 "unlockReadLock", pFile->zPath);
33887 }
33888 return res;
33889 }
33890
33891 /*
@@ -32996,15 +33914,15 @@
33914 ** It is not possible to lower the locking level one step at a time. You
33915 ** must go straight to locking level 0.
33916 */
33917 static int winLock(sqlite3_file *id, int locktype){
33918 int rc = SQLITE_OK; /* Return code from subroutines */
33919 int res = 1; /* Result of a Windows lock call */
33920 int newLocktype; /* Set pFile->locktype to this value before exiting */
33921 int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
33922 winFile *pFile = (winFile*)id;
33923 DWORD lastErrno = NO_ERROR;
33924
33925 assert( id!=0 );
33926 OSTRACE(("LOCK %d %d was %d(%d)\n",
33927 pFile->h, locktype, pFile->locktype, pFile->sharedLockByte));
33928
@@ -33030,20 +33948,23 @@
33948 if( (pFile->locktype==NO_LOCK)
33949 || ( (locktype==EXCLUSIVE_LOCK)
33950 && (pFile->locktype==RESERVED_LOCK))
33951 ){
33952 int cnt = 3;
33953 while( cnt-->0 && (res = osLockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
33954 /* Try 3 times to get the pending lock. This is needed to work
33955 ** around problems caused by indexing and/or anti-virus software on
33956 ** Windows systems.
33957 ** If you are using this code as a model for alternative VFSes, do not
33958 ** copy this retry logic. It is a hack intended for Windows only.
33959 */
33960 OSTRACE(("could not get a PENDING lock. cnt=%d\n", cnt));
33961 if( cnt ) osSleep(1);
33962 }
33963 gotPendingLock = res;
33964 if( !res ){
33965 lastErrno = osGetLastError();
33966 }
33967 }
33968
33969 /* Acquire a shared lock
33970 */
@@ -33051,23 +33972,23 @@
33972 assert( pFile->locktype==NO_LOCK );
33973 res = getReadLock(pFile);
33974 if( res ){
33975 newLocktype = SHARED_LOCK;
33976 }else{
33977 lastErrno = osGetLastError();
33978 }
33979 }
33980
33981 /* Acquire a RESERVED lock
33982 */
33983 if( locktype==RESERVED_LOCK && res ){
33984 assert( pFile->locktype==SHARED_LOCK );
33985 res = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
33986 if( res ){
33987 newLocktype = RESERVED_LOCK;
33988 }else{
33989 lastErrno = osGetLastError();
33990 }
33991 }
33992
33993 /* Acquire a PENDING lock
33994 */
@@ -33080,25 +34001,25 @@
34001 */
34002 if( locktype==EXCLUSIVE_LOCK && res ){
34003 assert( pFile->locktype>=SHARED_LOCK );
34004 res = unlockReadLock(pFile);
34005 OSTRACE(("unreadlock = %d\n", res));
34006 res = osLockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34007 if( res ){
34008 newLocktype = EXCLUSIVE_LOCK;
34009 }else{
34010 lastErrno = osGetLastError();
34011 OSTRACE(("error-code = %d\n", lastErrno));
34012 getReadLock(pFile);
34013 }
34014 }
34015
34016 /* If we are holding a PENDING lock that ought to be released, then
34017 ** release it now.
34018 */
34019 if( gotPendingLock && locktype==SHARED_LOCK ){
34020 osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
34021 }
34022
34023 /* Update the state of the lock has held in the file descriptor then
34024 ** return the appropriate result code.
34025 */
@@ -33105,11 +34026,11 @@
34026 if( res ){
34027 rc = SQLITE_OK;
34028 }else{
34029 OSTRACE(("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
34030 locktype, newLocktype));
34031 pFile->lastErrno = lastErrno;
34032 rc = SQLITE_BUSY;
34033 }
34034 pFile->locktype = (u8)newLocktype;
34035 return rc;
34036 }
@@ -33128,13 +34049,13 @@
34049 assert( id!=0 );
34050 if( pFile->locktype>=RESERVED_LOCK ){
34051 rc = 1;
34052 OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
34053 }else{
34054 rc = osLockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34055 if( rc ){
34056 osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34057 }
34058 rc = !rc;
34059 OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
34060 }
34061 *pResOut = rc;
@@ -33160,29 +34081,46 @@
34081 assert( locktype<=SHARED_LOCK );
34082 OSTRACE(("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
34083 pFile->locktype, pFile->sharedLockByte));
34084 type = pFile->locktype;
34085 if( type>=EXCLUSIVE_LOCK ){
34086 osUnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34087 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
34088 /* This should never happen. We should always be able to
34089 ** reacquire the read lock */
34090 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
34091 "winUnlock", pFile->zPath);
34092 }
34093 }
34094 if( type>=RESERVED_LOCK ){
34095 osUnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
34096 }
34097 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
34098 unlockReadLock(pFile);
34099 }
34100 if( type>=PENDING_LOCK ){
34101 osUnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
34102 }
34103 pFile->locktype = (u8)locktype;
34104 return rc;
34105 }
34106
34107 /*
34108 ** If *pArg is inititially negative then this is a query. Set *pArg to
34109 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34110 **
34111 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34112 */
34113 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
34114 if( *pArg<0 ){
34115 *pArg = (pFile->ctrlFlags & mask)!=0;
34116 }else if( (*pArg)==0 ){
34117 pFile->ctrlFlags &= ~mask;
34118 }else{
34119 pFile->ctrlFlags |= mask;
34120 }
34121 }
34122
34123 /*
34124 ** Control and query of the open file handle.
34125 */
34126 static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33215,19 +34153,19 @@
34153 return rc;
34154 }
34155 return SQLITE_OK;
34156 }
34157 case SQLITE_FCNTL_PERSIST_WAL: {
34158 winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
34159 return SQLITE_OK;
34160 }
34161 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
34162 winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
 
34163 return SQLITE_OK;
34164 }
34165 case SQLITE_FCNTL_VFSNAME: {
34166 *(char**)pArg = sqlite3_mprintf("win32");
34167 return SQLITE_OK;
34168 }
34169 case SQLITE_FCNTL_WIN32_AV_RETRY: {
34170 int *a = (int*)pArg;
34171 if( a[0]>0 ){
@@ -33255,20 +34193,21 @@
34193 ** if two files are created in the same file-system directory (i.e.
34194 ** a database and its journal file) that the sector size will be the
34195 ** same for both.
34196 */
34197 static int winSectorSize(sqlite3_file *id){
34198 (void)id;
34199 return SQLITE_DEFAULT_SECTOR_SIZE;
34200 }
34201
34202 /*
34203 ** Return a vector of device characteristics.
34204 */
34205 static int winDeviceCharacteristics(sqlite3_file *id){
34206 winFile *p = (winFile*)id;
34207 return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
34208 ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
34209 }
34210
34211 #ifndef SQLITE_OMIT_WAL
34212
34213 /*
@@ -33411,19 +34350,19 @@
34350 memset(&ovlp, 0, sizeof(OVERLAPPED));
34351 ovlp.Offset = ofst;
34352
34353 /* Release/Acquire the system-level lock */
34354 if( lockType==_SHM_UNLCK ){
34355 rc = osUnlockFileEx(pFile->hFile.h, 0, nByte, 0, &ovlp);
34356 }else{
34357 rc = osLockFileEx(pFile->hFile.h, dwFlags, 0, nByte, 0, &ovlp);
34358 }
34359
34360 if( rc!= 0 ){
34361 rc = SQLITE_OK;
34362 }else{
34363 pFile->lastErrno = osGetLastError();
34364 rc = SQLITE_BUSY;
34365 }
34366
34367 OSTRACE(("SHM-LOCK %d %s %s 0x%08lx\n",
34368 pFile->hFile.h,
@@ -33453,27 +34392,29 @@
34392 while( (p = *pp)!=0 ){
34393 if( p->nRef==0 ){
34394 int i;
34395 if( p->mutex ) sqlite3_mutex_free(p->mutex);
34396 for(i=0; i<p->nRegion; i++){
34397 bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
34398 OSTRACE(("SHM-PURGE pid-%d unmap region=%d %s\n",
34399 (int)osGetCurrentProcessId(), i,
34400 bRc ? "ok" : "failed"));
34401 bRc = osCloseHandle(p->aRegion[i].hMap);
34402 OSTRACE(("SHM-PURGE pid-%d close region=%d %s\n",
34403 (int)osGetCurrentProcessId(), i,
34404 bRc ? "ok" : "failed"));
34405 }
34406 if( p->hFile.h != INVALID_HANDLE_VALUE ){
34407 SimulateIOErrorBenign(1);
34408 winClose((sqlite3_file *)&p->hFile);
34409 SimulateIOErrorBenign(0);
34410 }
34411 if( deleteFlag ){
34412 SimulateIOErrorBenign(1);
34413 sqlite3BeginBenignMalloc();
34414 winDelete(pVfs, p->zFilename, 0);
34415 sqlite3EndBenignMalloc();
34416 SimulateIOErrorBenign(0);
34417 }
34418 *pp = p->pNext;
34419 sqlite3_free(p->aRegion);
34420 sqlite3_free(p);
@@ -33501,19 +34442,19 @@
34442
34443 /* Allocate space for the new sqlite3_shm object. Also speculatively
34444 ** allocate space for a new winShmNode and filename.
34445 */
34446 p = sqlite3_malloc( sizeof(*p) );
34447 if( p==0 ) return SQLITE_IOERR_NOMEM;
34448 memset(p, 0, sizeof(*p));
34449 nName = sqlite3Strlen30(pDbFd->zPath);
34450 pNew = sqlite3_malloc( sizeof(*pShmNode) + nName + 17 );
34451 if( pNew==0 ){
34452 sqlite3_free(p);
34453 return SQLITE_IOERR_NOMEM;
34454 }
34455 memset(pNew, 0, sizeof(*pNew) + nName + 17);
34456 pNew->zFilename = (char*)&pNew[1];
34457 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
34458 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
34459
34460 /* Look to see if there is an existing winShmNode that can be used.
@@ -33535,31 +34476,31 @@
34476 pShmNode->pNext = winShmNodeList;
34477 winShmNodeList = pShmNode;
34478
34479 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34480 if( pShmNode->mutex==0 ){
34481 rc = SQLITE_IOERR_NOMEM;
34482 goto shm_open_err;
34483 }
34484
34485 rc = winOpen(pDbFd->pVfs,
34486 pShmNode->zFilename, /* Name of the file (UTF-8) */
34487 (sqlite3_file*)&pShmNode->hFile, /* File handle here */
34488 SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, /* Mode flags */
34489 0);
34490 if( SQLITE_OK!=rc ){
 
34491 goto shm_open_err;
34492 }
34493
34494 /* Check to see if another process is holding the dead-man switch.
34495 ** If not, truncate the file to zero length.
34496 */
34497 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
34498 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
34499 if( rc!=SQLITE_OK ){
34500 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
34501 "winOpenShm", pDbFd->zPath);
34502 }
34503 }
34504 if( rc==SQLITE_OK ){
34505 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34506 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -33740,11 +34681,11 @@
34681 }
34682 }
34683 }
34684 sqlite3_mutex_leave(pShmNode->mutex);
34685 OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x %s\n",
34686 p->id, (int)osGetCurrentProcessId(), p->sharedMask, p->exclMask,
34687 rc ? "failed" : "ok"));
34688 return rc;
34689 }
34690
34691 /*
@@ -33814,11 +34755,12 @@
34755 ** Check to see if it has been allocated (i.e. if the wal-index file is
34756 ** large enough to contain the requested region).
34757 */
34758 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
34759 if( rc!=SQLITE_OK ){
34760 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34761 "winShmMap1", pDbFd->zPath);
34762 goto shmpage_out;
34763 }
34764
34765 if( sz<nByte ){
34766 /* The requested memory region does not exist. If isWrite is set to
@@ -33828,11 +34770,12 @@
34770 ** the requested memory region.
34771 */
34772 if( !isWrite ) goto shmpage_out;
34773 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
34774 if( rc!=SQLITE_OK ){
34775 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34776 "winShmMap2", pDbFd->zPath);
34777 goto shmpage_out;
34778 }
34779 }
34780
34781 /* Map the requested memory region into this processes address space. */
@@ -33847,30 +34790,31 @@
34790
34791 while( pShmNode->nRegion<=iRegion ){
34792 HANDLE hMap; /* file-mapping handle */
34793 void *pMap = 0; /* Mapped memory region */
34794
34795 hMap = osCreateFileMapping(pShmNode->hFile.h,
34796 NULL, PAGE_READWRITE, 0, nByte, NULL
34797 );
34798 OSTRACE(("SHM-MAP pid-%d create region=%d nbyte=%d %s\n",
34799 (int)osGetCurrentProcessId(), pShmNode->nRegion, nByte,
34800 hMap ? "ok" : "failed"));
34801 if( hMap ){
34802 int iOffset = pShmNode->nRegion*szRegion;
34803 int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
34804 pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
34805 0, iOffset - iOffsetShift, szRegion + iOffsetShift
34806 );
34807 OSTRACE(("SHM-MAP pid-%d map region=%d offset=%d size=%d %s\n",
34808 (int)osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
34809 szRegion, pMap ? "ok" : "failed"));
34810 }
34811 if( !pMap ){
34812 pShmNode->lastErrno = osGetLastError();
34813 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
34814 "winShmMap3", pDbFd->zPath);
34815 if( hMap ) osCloseHandle(hMap);
34816 goto shmpage_out;
34817 }
34818
34819 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
34820 pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
@@ -33964,11 +34908,11 @@
34908 static char zChars[] =
34909 "abcdefghijklmnopqrstuvwxyz"
34910 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34911 "0123456789";
34912 size_t i, j;
34913 char zTempPath[MAX_PATH+2];
34914
34915 /* It's odd to simulate an io-error here, but really this is just
34916 ** using the io-error infrastructure to test that SQLite handles this
34917 ** function failing.
34918 */
@@ -33977,55 +34921,56 @@
34921 if( sqlite3_temp_directory ){
34922 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", sqlite3_temp_directory);
34923 }else if( isNT() ){
34924 char *zMulti;
34925 WCHAR zWidePath[MAX_PATH];
34926 osGetTempPathW(MAX_PATH-30, zWidePath);
34927 zMulti = unicodeToUtf8(zWidePath);
34928 if( zMulti ){
34929 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zMulti);
34930 sqlite3_free(zMulti);
34931 }else{
34932 return SQLITE_IOERR_NOMEM;
34933 }
34934 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
34935 ** Since the ANSI version of these Windows API do not exist for WINCE,
34936 ** it's important to not reference them for WINCE builds.
34937 */
34938 #if SQLITE_OS_WINCE==0
34939 }else{
34940 char *zUtf8;
34941 char zMbcsPath[MAX_PATH];
34942 osGetTempPathA(MAX_PATH-30, zMbcsPath);
34943 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34944 if( zUtf8 ){
34945 sqlite3_snprintf(MAX_PATH-30, zTempPath, "%s", zUtf8);
34946 sqlite3_free(zUtf8);
34947 }else{
34948 return SQLITE_IOERR_NOMEM;
34949 }
34950 #endif
34951 }
34952
34953 /* Check that the output buffer is large enough for the temporary file
34954 ** name. If it is not, return SQLITE_ERROR.
34955 */
34956 if( (sqlite3Strlen30(zTempPath) + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34957 return SQLITE_ERROR;
34958 }
34959
34960 for(i=sqlite3Strlen30(zTempPath); i>0 && zTempPath[i-1]=='\\'; i--){}
34961 zTempPath[i] = 0;
34962
34963 sqlite3_snprintf(nBuf-18, zBuf,
34964 "%s\\"SQLITE_TEMP_FILE_PREFIX, zTempPath);
34965 j = sqlite3Strlen30(zBuf);
34966 sqlite3_randomness(15, &zBuf[j]);
34967 for(i=0; i<15; i++, j++){
34968 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34969 }
34970 zBuf[j] = 0;
34971 zBuf[j+1] = 0;
34972
34973 OSTRACE(("TEMP FILENAME: %s\n", zBuf));
34974 return SQLITE_OK;
34975 }
34976
@@ -34038,10 +34983,11 @@
34983 sqlite3_file *id, /* Write the SQLite file handle here */
34984 int flags, /* Open mode flags */
34985 int *pOutFlags /* Status return flags */
34986 ){
34987 HANDLE h;
34988 DWORD lastErrno;
34989 DWORD dwDesiredAccess;
34990 DWORD dwShareMode;
34991 DWORD dwCreationDisposition;
34992 DWORD dwFlagsAndAttributes = 0;
34993 #if SQLITE_OS_WINCE
@@ -34053,11 +34999,11 @@
34999 int cnt = 0;
35000
35001 /* If argument zPath is a NULL pointer, this function is required to open
35002 ** a temporary file. Use this buffer to store the file name in.
35003 */
35004 char zTmpname[MAX_PATH+2]; /* Buffer used to create temp filename */
35005
35006 int rc = SQLITE_OK; /* Function Return Code */
35007 #if !defined(NDEBUG) || SQLITE_OS_WINCE
35008 int eType = flags&0xFFFFFF00; /* Type of file to open */
35009 #endif
@@ -34112,21 +35058,28 @@
35058 /* If the second argument to this function is NULL, generate a
35059 ** temporary file name to use
35060 */
35061 if( !zUtf8Name ){
35062 assert(isDelete && !isOpenJournal);
35063 rc = getTempname(MAX_PATH+2, zTmpname);
35064 if( rc!=SQLITE_OK ){
35065 return rc;
35066 }
35067 zUtf8Name = zTmpname;
35068 }
35069
35070 /* Database filenames are double-zero terminated if they are not
35071 ** URIs with parameters. Hence, they can always be passed into
35072 ** sqlite3_uri_parameter().
35073 */
35074 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
35075 zUtf8Name[strlen(zUtf8Name)+1]==0 );
35076
35077 /* Convert the filename to the system encoding. */
35078 zConverted = convertUtf8Filename(zUtf8Name);
35079 if( zConverted==0 ){
35080 return SQLITE_IOERR_NOMEM;
35081 }
35082
35083 if( isReadWrite ){
35084 dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
35085 }else{
@@ -34168,30 +35121,30 @@
35121 #if SQLITE_OS_WINCE
35122 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
35123 #endif
35124
35125 if( isNT() ){
35126 while( (h = osCreateFileW((LPCWSTR)zConverted,
35127 dwDesiredAccess,
35128 dwShareMode, NULL,
35129 dwCreationDisposition,
35130 dwFlagsAndAttributes,
35131 NULL))==INVALID_HANDLE_VALUE &&
35132 retryIoerr(&cnt, &lastErrno) ){}
35133 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
35134 ** Since the ANSI version of these Windows API do not exist for WINCE,
35135 ** it's important to not reference them for WINCE builds.
35136 */
35137 #if SQLITE_OS_WINCE==0
35138 }else{
35139 while( (h = osCreateFileA((LPCSTR)zConverted,
35140 dwDesiredAccess,
35141 dwShareMode, NULL,
35142 dwCreationDisposition,
35143 dwFlagsAndAttributes,
35144 NULL))==INVALID_HANDLE_VALUE &&
35145 retryIoerr(&cnt, &lastErrno) ){}
35146 #endif
35147 }
35148
35149 logIoerr(cnt);
35150
@@ -34198,13 +35151,13 @@
35151 OSTRACE(("OPEN %d %s 0x%lx %s\n",
35152 h, zName, dwDesiredAccess,
35153 h==INVALID_HANDLE_VALUE ? "failed" : "ok"));
35154
35155 if( h==INVALID_HANDLE_VALUE ){
35156 pFile->lastErrno = lastErrno;
35157 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
35158 sqlite3_free(zConverted);
35159 if( isReadWrite && !isExclusive ){
35160 return winOpen(pVfs, zName, id,
35161 ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
35162 }else{
35163 return SQLITE_CANTOPEN_BKPT;
@@ -34224,36 +35177,38 @@
35177 pFile->h = h;
35178 pFile->lastErrno = NO_ERROR;
35179 pFile->pVfs = pVfs;
35180 pFile->pShm = 0;
35181 pFile->zPath = zName;
35182 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
35183 pFile->ctrlFlags |= WINFILE_PSOW;
35184 }
35185
35186 #if SQLITE_OS_WINCE
35187 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
35188 && !winceCreateLock(zName, pFile)
35189 ){
35190 osCloseHandle(h);
35191 sqlite3_free(zConverted);
35192 return SQLITE_CANTOPEN_BKPT;
35193 }
35194 if( isTemp ){
35195 pFile->zDeleteOnClose = zConverted;
35196 }else
35197 #endif
35198 {
35199 sqlite3_free(zConverted);
35200 }
35201
35202 OpenCounter(+1);
35203 return rc;
35204 }
35205
35206 /*
35207 ** Delete the named file.
35208 **
35209 ** Note that Windows does not allow a file to be deleted if some other
35210 ** process has it open. Sometimes a virus scanner or indexing program
35211 ** will open a journal file shortly after it is created in order to do
35212 ** whatever it does. While this other process is holding the
35213 ** file open, we will be unable to delete it. To work around this
35214 ** problem, we delay 100 milliseconds and try to delete again. Up
@@ -34265,42 +35220,44 @@
35220 const char *zFilename, /* Name of file to delete */
35221 int syncDir /* Not used on win32 */
35222 ){
35223 int cnt = 0;
35224 int rc;
35225 DWORD lastErrno;
35226 void *zConverted;
35227 UNUSED_PARAMETER(pVfs);
35228 UNUSED_PARAMETER(syncDir);
35229
35230 SimulateIOError(return SQLITE_IOERR_DELETE);
35231 zConverted = convertUtf8Filename(zFilename);
35232 if( zConverted==0 ){
35233 return SQLITE_IOERR_NOMEM;
35234 }
35235 if( isNT() ){
35236 rc = 1;
35237 while( osGetFileAttributesW(zConverted)!=INVALID_FILE_ATTRIBUTES &&
35238 (rc = osDeleteFileW(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
35239 rc = rc ? SQLITE_OK : SQLITE_ERROR;
35240 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
35241 ** Since the ANSI version of these Windows API do not exist for WINCE,
35242 ** it's important to not reference them for WINCE builds.
35243 */
35244 #if SQLITE_OS_WINCE==0
35245 }else{
35246 rc = 1;
35247 while( osGetFileAttributesA(zConverted)!=INVALID_FILE_ATTRIBUTES &&
35248 (rc = osDeleteFileA(zConverted))==0 && retryIoerr(&cnt, &lastErrno) ){}
35249 rc = rc ? SQLITE_OK : SQLITE_ERROR;
35250 #endif
35251 }
35252 if( rc ){
35253 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35254 "winDelete", zFilename);
35255 }else{
35256 logIoerr(cnt);
35257 }
35258 sqlite3_free(zConverted);
35259 OSTRACE(("DELETE \"%s\" %s\n", zFilename, (rc ? "failed" : "ok" )));
35260 return rc;
35261 }
35262
35263 /*
@@ -34312,25 +35269,26 @@
35269 int flags, /* Type of test to make on this file */
35270 int *pResOut /* OUT: Result */
35271 ){
35272 DWORD attr;
35273 int rc = 0;
35274 DWORD lastErrno;
35275 void *zConverted;
35276 UNUSED_PARAMETER(pVfs);
35277
35278 SimulateIOError( return SQLITE_IOERR_ACCESS; );
35279 zConverted = convertUtf8Filename(zFilename);
35280 if( zConverted==0 ){
35281 return SQLITE_IOERR_NOMEM;
35282 }
35283 if( isNT() ){
35284 int cnt = 0;
35285 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35286 memset(&sAttrData, 0, sizeof(sAttrData));
35287 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35288 GetFileExInfoStandard,
35289 &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
35290 if( rc ){
35291 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
35292 ** as if it does not exist.
35293 */
35294 if( flags==SQLITE_ACCESS_EXISTS
@@ -34340,28 +35298,28 @@
35298 }else{
35299 attr = sAttrData.dwFileAttributes;
35300 }
35301 }else{
35302 logIoerr(cnt);
35303 if( lastErrno!=ERROR_FILE_NOT_FOUND ){
35304 winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
35305 sqlite3_free(zConverted);
35306 return SQLITE_IOERR_ACCESS;
35307 }else{
35308 attr = INVALID_FILE_ATTRIBUTES;
35309 }
35310 }
35311 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
35312 ** Since the ANSI version of these Windows API do not exist for WINCE,
35313 ** it's important to not reference them for WINCE builds.
35314 */
35315 #if SQLITE_OS_WINCE==0
35316 }else{
35317 attr = osGetFileAttributesA((char*)zConverted);
35318 #endif
35319 }
35320 sqlite3_free(zConverted);
35321 switch( flags ){
35322 case SQLITE_ACCESS_READ:
35323 case SQLITE_ACCESS_EXISTS:
35324 rc = attr!=INVALID_FILE_ATTRIBUTES;
35325 break;
@@ -34422,119 +35380,52 @@
35380 ** current working directory has been unlinked.
35381 */
35382 SimulateIOError( return SQLITE_ERROR );
35383 UNUSED_PARAMETER(nFull);
35384 zConverted = convertUtf8Filename(zRelative);
35385 if( zConverted==0 ){
35386 return SQLITE_IOERR_NOMEM;
35387 }
35388 if( isNT() ){
35389 LPWSTR zTemp;
35390 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0) + 3;
35391 zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
35392 if( zTemp==0 ){
35393 sqlite3_free(zConverted);
35394 return SQLITE_IOERR_NOMEM;
35395 }
35396 osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
35397 sqlite3_free(zConverted);
35398 zOut = unicodeToUtf8(zTemp);
35399 sqlite3_free(zTemp);
35400 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
35401 ** Since the ANSI version of these Windows API do not exist for WINCE,
35402 ** it's important to not reference them for WINCE builds.
35403 */
35404 #if SQLITE_OS_WINCE==0
35405 }else{
35406 char *zTemp;
35407 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0) + 3;
35408 zTemp = sqlite3_malloc( nByte*sizeof(zTemp[0]) );
35409 if( zTemp==0 ){
35410 sqlite3_free(zConverted);
35411 return SQLITE_IOERR_NOMEM;
35412 }
35413 osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
35414 sqlite3_free(zConverted);
35415 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
35416 sqlite3_free(zTemp);
35417 #endif
35418 }
35419 if( zOut ){
35420 sqlite3_snprintf(pVfs->mxPathname, zFull, "%s", zOut);
35421 sqlite3_free(zOut);
35422 return SQLITE_OK;
35423 }else{
35424 return SQLITE_IOERR_NOMEM;
35425 }
35426 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35427 }
35428
35429 #ifndef SQLITE_OMIT_LOAD_EXTENSION
35430 /*
35431 ** Interfaces for opening a shared library, finding entry points
@@ -34550,41 +35441,34 @@
35441 UNUSED_PARAMETER(pVfs);
35442 if( zConverted==0 ){
35443 return 0;
35444 }
35445 if( isNT() ){
35446 h = osLoadLibraryW((LPCWSTR)zConverted);
35447 /* isNT() is 1 if SQLITE_OS_WINCE==1, so this else is never executed.
35448 ** Since the ANSI version of these Windows API do not exist for WINCE,
35449 ** it's important to not reference them for WINCE builds.
35450 */
35451 #if SQLITE_OS_WINCE==0
35452 }else{
35453 h = osLoadLibraryA((char*)zConverted);
35454 #endif
35455 }
35456 sqlite3_free(zConverted);
35457 return (void*)h;
35458 }
35459 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
35460 UNUSED_PARAMETER(pVfs);
35461 getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35462 }
35463 static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
35464 UNUSED_PARAMETER(pVfs);
35465 return (void(*)(void))osGetProcAddressA((HANDLE)pHandle, zSymbol);
 
 
 
 
 
 
 
35466 }
35467 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
35468 UNUSED_PARAMETER(pVfs);
35469 osFreeLibrary((HANDLE)pHandle);
35470 }
35471 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
35472 #define winDlOpen 0
35473 #define winDlError 0
35474 #define winDlSym 0
@@ -34602,27 +35486,27 @@
35486 n = nBuf;
35487 memset(zBuf, 0, nBuf);
35488 #else
35489 if( sizeof(SYSTEMTIME)<=nBuf-n ){
35490 SYSTEMTIME x;
35491 osGetSystemTime(&x);
35492 memcpy(&zBuf[n], &x, sizeof(x));
35493 n += sizeof(x);
35494 }
35495 if( sizeof(DWORD)<=nBuf-n ){
35496 DWORD pid = osGetCurrentProcessId();
35497 memcpy(&zBuf[n], &pid, sizeof(pid));
35498 n += sizeof(pid);
35499 }
35500 if( sizeof(DWORD)<=nBuf-n ){
35501 DWORD cnt = osGetTickCount();
35502 memcpy(&zBuf[n], &cnt, sizeof(cnt));
35503 n += sizeof(cnt);
35504 }
35505 if( sizeof(LARGE_INTEGER)<=nBuf-n ){
35506 LARGE_INTEGER i;
35507 osQueryPerformanceCounter(&i);
35508 memcpy(&zBuf[n], &i, sizeof(i));
35509 n += sizeof(i);
35510 }
35511 #endif
35512 return n;
@@ -34631,11 +35515,11 @@
35515
35516 /*
35517 ** Sleep for a little while. Return the amount of time slept.
35518 */
35519 static int winSleep(sqlite3_vfs *pVfs, int microsec){
35520 osSleep((microsec+999)/1000);
35521 UNUSED_PARAMETER(pVfs);
35522 return ((microsec+999)/1000)*1000;
35523 }
35524
35525 /*
@@ -34670,17 +35554,17 @@
35554 static const sqlite3_int64 max32BitValue =
35555 (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 + (sqlite3_int64)294967296;
35556
35557 #if SQLITE_OS_WINCE
35558 SYSTEMTIME time;
35559 osGetSystemTime(&time);
35560 /* if SystemTimeToFileTime() fails, it returns zero. */
35561 if (!osSystemTimeToFileTime(&time,&ft)){
35562 return SQLITE_ERROR;
35563 }
35564 #else
35565 osGetSystemTimeAsFileTime( &ft );
35566 #endif
35567
35568 *piNow = winFiletimeEpoch +
35569 ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
35570 (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
@@ -34709,12 +35593,12 @@
35593 return rc;
35594 }
35595
35596 /*
35597 ** The idea is that this function works like a combination of
35598 ** GetLastError() and FormatMessage() on Windows (or errno and
35599 ** strerror_r() on Unix). After an error is returned by an OS
35600 ** function, SQLite calls this function with zBuf pointing to
35601 ** a buffer of nBuf bytes. The OS layer should populate the
35602 ** buffer with a nul-terminated UTF-8 encoded error message
35603 ** describing the last IO error to have occurred within the calling
35604 ** thread.
@@ -34739,14 +35623,12 @@
35623 ** by sqlite into the error message available to the user using
35624 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
35625 */
35626 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
35627 UNUSED_PARAMETER(pVfs);
35628 return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
35629 }
 
 
35630
35631 /*
35632 ** Initialize and deinitialize the operating system interface.
35633 */
35634 SQLITE_API int sqlite3_os_init(void){
@@ -34768,25 +35650,30 @@
35650 winRandomness, /* xRandomness */
35651 winSleep, /* xSleep */
35652 winCurrentTime, /* xCurrentTime */
35653 winGetLastError, /* xGetLastError */
35654 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35655 winSetSystemCall, /* xSetSystemCall */
35656 winGetSystemCall, /* xGetSystemCall */
35657 winNextSystemCall, /* xNextSystemCall */
35658 };
35659
35660 /* Double-check that the aSyscall[] array has been constructed
35661 ** correctly. See ticket [bb3a86e890c8e96ab] */
35662 assert( ArraySize(aSyscall)==60 );
35663
35664 #ifndef SQLITE_OMIT_WAL
35665 /* get memory map allocation granularity */
35666 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
35667 osGetSystemInfo(&winSysInfo);
35668 assert(winSysInfo.dwAllocationGranularity > 0);
35669 #endif
35670
35671 sqlite3_vfs_register(&winVfs, 1);
35672 return SQLITE_OK;
35673 }
35674
35675 SQLITE_API int sqlite3_os_end(void){
35676 return SQLITE_OK;
35677 }
35678
35679 #endif /* SQLITE_OS_WIN */
@@ -35222,11 +36109,11 @@
36109 */
36110 struct PCache {
36111 PgHdr *pDirty, *pDirtyTail; /* List of dirty pages in LRU order */
36112 PgHdr *pSynced; /* Last synced page in dirty page list */
36113 int nRef; /* Number of referenced pages */
36114 int szCache; /* Configured cache size */
36115 int szPage; /* Size of every page in this cache */
36116 int szExtra; /* Size of extra space for each page */
36117 int bPurgeable; /* True if pages are on backing store */
36118 int (*xStress)(void*,PgHdr*); /* Call to try make a page clean */
36119 void *pStress; /* Argument to xStress */
@@ -35333,32 +36220,32 @@
36220 PCache *pCache = p->pCache;
36221 if( pCache->bPurgeable ){
36222 if( p->pgno==1 ){
36223 pCache->pPage1 = 0;
36224 }
36225 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
36226 }
36227 }
36228
36229 /*************************************************** General Interfaces ******
36230 **
36231 ** Initialize and shutdown the page cache subsystem. Neither of these
36232 ** functions are threadsafe.
36233 */
36234 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
36235 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
36236 /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
36237 ** built-in default page cache is used instead of the application defined
36238 ** page cache. */
36239 sqlite3PCacheSetDefault();
36240 }
36241 return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
36242 }
36243 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
36244 if( sqlite3GlobalConfig.pcache2.xShutdown ){
36245 /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
36246 sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
36247 }
36248 }
36249
36250 /*
36251 ** Return the size in bytes of a PCache object.
@@ -35383,26 +36270,37 @@
36270 p->szPage = szPage;
36271 p->szExtra = szExtra;
36272 p->bPurgeable = bPurgeable;
36273 p->xStress = xStress;
36274 p->pStress = pStress;
36275 p->szCache = 100;
36276 }
36277
36278 /*
36279 ** Change the page size for PCache object. The caller must ensure that there
36280 ** are no outstanding page references when this function is called.
36281 */
36282 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
36283 assert( pCache->nRef==0 && pCache->pDirty==0 );
36284 if( pCache->pCache ){
36285 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
36286 pCache->pCache = 0;
36287 pCache->pPage1 = 0;
36288 }
36289 pCache->szPage = szPage;
36290 }
36291
36292 /*
36293 ** Compute the number of pages of cache requested.
36294 */
36295 static int numberOfCachePages(PCache *p){
36296 if( p->szCache>=0 ){
36297 return p->szCache;
36298 }else{
36299 return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
36300 }
36301 }
36302
36303 /*
36304 ** Try to obtain a page from the cache.
36305 */
36306 SQLITE_PRIVATE int sqlite3PcacheFetch(
@@ -35409,11 +36307,12 @@
36307 PCache *pCache, /* Obtain the page from this cache */
36308 Pgno pgno, /* Page number to obtain */
36309 int createFlag, /* If true, create page if it does not exist already */
36310 PgHdr **ppPage /* Write the page here */
36311 ){
36312 sqlite3_pcache_page *pPage = 0;
36313 PgHdr *pPgHdr = 0;
36314 int eCreate;
36315
36316 assert( pCache!=0 );
36317 assert( createFlag==1 || createFlag==0 );
36318 assert( pgno>0 );
@@ -35421,23 +36320,23 @@
36320 /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
36321 ** allocate it now.
36322 */
36323 if( !pCache->pCache && createFlag ){
36324 sqlite3_pcache *p;
36325 p = sqlite3GlobalConfig.pcache2.xCreate(
36326 pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
36327 );
36328 if( !p ){
36329 return SQLITE_NOMEM;
36330 }
36331 sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
36332 pCache->pCache = p;
36333 }
36334
36335 eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
36336 if( pCache->pCache ){
36337 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
36338 }
36339
36340 if( !pPage && eCreate==1 ){
36341 PgHdr *pPg;
36342
@@ -35460,45 +36359,48 @@
36359 #ifdef SQLITE_LOG_CACHE_SPILL
36360 sqlite3_log(SQLITE_FULL,
36361 "spill page %d making room for %d - cache used: %d/%d",
36362 pPg->pgno, pgno,
36363 sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
36364 numberOfCachePages(pCache));
36365 #endif
36366 rc = pCache->xStress(pCache->pStress, pPg);
36367 if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
36368 return rc;
36369 }
36370 }
36371
36372 pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
36373 }
36374
36375 if( pPage ){
36376 pPgHdr = (PgHdr *)pPage->pExtra;
36377
36378 if( !pPgHdr->pPage ){
36379 memset(pPgHdr, 0, sizeof(PgHdr));
36380 pPgHdr->pPage = pPage;
36381 pPgHdr->pData = pPage->pBuf;
36382 pPgHdr->pExtra = (void *)&pPgHdr[1];
36383 memset(pPgHdr->pExtra, 0, pCache->szExtra);
36384 pPgHdr->pCache = pCache;
36385 pPgHdr->pgno = pgno;
36386 }
36387 assert( pPgHdr->pCache==pCache );
36388 assert( pPgHdr->pgno==pgno );
36389 assert( pPgHdr->pData==pPage->pBuf );
36390 assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
36391
36392 if( 0==pPgHdr->nRef ){
36393 pCache->nRef++;
36394 }
36395 pPgHdr->nRef++;
36396 if( pgno==1 ){
36397 pCache->pPage1 = pPgHdr;
36398 }
36399 }
36400 *ppPage = pPgHdr;
36401 return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
36402 }
36403
36404 /*
36405 ** Decrement the reference count on a page. If the page is clean and the
36406 ** reference count drops to 0, then it is made elible for recycling.
@@ -35541,11 +36443,11 @@
36443 pCache = p->pCache;
36444 pCache->nRef--;
36445 if( p->pgno==1 ){
36446 pCache->pPage1 = 0;
36447 }
36448 sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
36449 }
36450
36451 /*
36452 ** Make sure the page is marked as dirty. If it isn't dirty already,
36453 ** make it so.
@@ -35599,11 +36501,11 @@
36501 */
36502 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
36503 PCache *pCache = p->pCache;
36504 assert( p->nRef>0 );
36505 assert( newPgno>0 );
36506 sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
36507 p->pgno = newPgno;
36508 if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
36509 pcacheRemoveFromDirtyList(p);
36510 pcacheAddToDirtyList(p);
36511 }
@@ -35636,20 +36538,20 @@
36538 }
36539 if( pgno==0 && pCache->pPage1 ){
36540 memset(pCache->pPage1->pData, 0, pCache->szPage);
36541 pgno = 1;
36542 }
36543 sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
36544 }
36545 }
36546
36547 /*
36548 ** Close a cache.
36549 */
36550 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
36551 if( pCache->pCache ){
36552 sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
36553 }
36554 }
36555
36556 /*
36557 ** Discard the contents of the cache.
@@ -35757,31 +36659,41 @@
36659 ** Return the total number of pages in the cache.
36660 */
36661 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
36662 int nPage = 0;
36663 if( pCache->pCache ){
36664 nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
36665 }
36666 return nPage;
36667 }
36668
36669 #ifdef SQLITE_TEST
36670 /*
36671 ** Get the suggested cache-size value.
36672 */
36673 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
36674 return numberOfCachePages(pCache);
36675 }
36676 #endif
36677
36678 /*
36679 ** Set the suggested cache-size value.
36680 */
36681 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
36682 pCache->szCache = mxPage;
36683 if( pCache->pCache ){
36684 sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
36685 numberOfCachePages(pCache));
36686 }
36687 }
36688
36689 /*
36690 ** Free up as much memory as possible from the page cache.
36691 */
36692 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
36693 if( pCache->pCache ){
36694 sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
36695 }
36696 }
36697
36698 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
36699 /*
@@ -35822,11 +36734,10 @@
36734 typedef struct PCache1 PCache1;
36735 typedef struct PgHdr1 PgHdr1;
36736 typedef struct PgFreeslot PgFreeslot;
36737 typedef struct PGroup PGroup;
36738
 
36739 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
36740 ** of one or more PCaches that are able to recycle each others unpinned
36741 ** pages when they are under memory pressure. A PGroup is an instance of
36742 ** the following object.
36743 **
@@ -35839,22 +36750,22 @@
36750 ** of.
36751 **
36752 ** Mode 1 uses more memory (since PCache instances are not able to rob
36753 ** unused pages from other PCaches) but it also operates without a mutex,
36754 ** and is therefore often faster. Mode 2 requires a mutex in order to be
36755 ** threadsafe, but recycles pages more efficiently.
36756 **
36757 ** For mode (1), PGroup.mutex is NULL. For mode (2) there is only a single
36758 ** PGroup which is the pcache1.grp global variable and its mutex is
36759 ** SQLITE_MUTEX_STATIC_LRU.
36760 */
36761 struct PGroup {
36762 sqlite3_mutex *mutex; /* MUTEX_STATIC_LRU or NULL */
36763 unsigned int nMaxPage; /* Sum of nMax for purgeable caches */
36764 unsigned int nMinPage; /* Sum of nMin for purgeable caches */
36765 unsigned int mxPinned; /* nMaxpage + 10 - nMinPage */
36766 unsigned int nCurrentPage; /* Number of purgeable pages allocated */
36767 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
36768 };
36769
36770 /* Each page cache is an instance of the following object. Every
36771 ** open database file (including each in-memory database and each
@@ -35865,15 +36776,16 @@
36776 ** opaque sqlite3_pcache* handles.
36777 */
36778 struct PCache1 {
36779 /* Cache configuration parameters. Page size (szPage) and the purgeable
36780 ** flag (bPurgeable) are set when the cache is created. nMax may be
36781 ** modified at any time by a call to the pcache1Cachesize() method.
36782 ** The PGroup mutex must be held when accessing nMax.
36783 */
36784 PGroup *pGroup; /* PGroup this cache belongs to */
36785 int szPage; /* Size of allocated pages in bytes */
36786 int szExtra; /* Size of extra space in bytes */
36787 int bPurgeable; /* True if cache is purgeable */
36788 unsigned int nMin; /* Minimum number of pages reserved */
36789 unsigned int nMax; /* Configured "cache_size" value */
36790 unsigned int n90pct; /* nMax*9/10 */
36791
@@ -35888,15 +36800,16 @@
36800 unsigned int iMaxKey; /* Largest key seen since xTruncate() */
36801 };
36802
36803 /*
36804 ** Each cache entry is represented by an instance of the following
36805 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
36806 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
36807 ** in memory.
36808 */
36809 struct PgHdr1 {
36810 sqlite3_pcache_page page;
36811 unsigned int iKey; /* Key value (page number) */
36812 PgHdr1 *pNext; /* Next in hash table chain */
36813 PCache1 *pCache; /* Cache that currently owns this page */
36814 PgHdr1 *pLruNext; /* Next in LRU list of unpinned pages */
36815 PgHdr1 *pLruPrev; /* Previous in LRU list of unpinned pages */
@@ -35942,25 +36855,10 @@
36855 ** alias "pcache1". This ensures that the WSD emulation is used when
36856 ** compiling for systems that do not support real WSD.
36857 */
36858 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
36859
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36860 /*
36861 ** Macros to enter and leave the PCache LRU mutex.
36862 */
36863 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
36864 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
@@ -36039,12 +36937,13 @@
36937 }
36938
36939 /*
36940 ** Free an allocated buffer obtained from pcache1Alloc().
36941 */
36942 static int pcache1Free(void *p){
36943 int nFreed = 0;
36944 if( p==0 ) return 0;
36945 if( p>=pcache1.pStart && p<pcache1.pEnd ){
36946 PgFreeslot *pSlot;
36947 sqlite3_mutex_enter(pcache1.mutex);
36948 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
36949 pSlot = (PgFreeslot*)p;
@@ -36053,19 +36952,19 @@
36952 pcache1.nFreeSlot++;
36953 pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
36954 assert( pcache1.nFreeSlot<=pcache1.nSlot );
36955 sqlite3_mutex_leave(pcache1.mutex);
36956 }else{
 
36957 assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
36958 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
36959 nFreed = sqlite3MallocSize(p);
36960 sqlite3_mutex_enter(pcache1.mutex);
36961 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
36962 sqlite3_mutex_leave(pcache1.mutex);
36963 sqlite3_free(p);
36964 }
36965 return nFreed;
36966 }
36967
36968 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
36969 /*
36970 ** Return the size of a pcache allocation
@@ -36086,29 +36985,41 @@
36985
36986 /*
36987 ** Allocate a new page object initially associated with cache pCache.
36988 */
36989 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
 
36990 PgHdr1 *p = 0;
36991 void *pPg;
36992
36993 /* The group mutex must be released before pcache1Alloc() is called. This
36994 ** is because it may call sqlite3_release_memory(), which assumes that
36995 ** this mutex is not held. */
36996 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36997 pcache1LeaveMutex(pCache->pGroup);
36998 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
36999 pPg = pcache1Alloc(pCache->szPage);
37000 p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
37001 if( !pPg || !p ){
37002 pcache1Free(pPg);
37003 sqlite3_free(p);
37004 pPg = 0;
37005 }
37006 #else
37007 pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
37008 p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
37009 #endif
37010 pcache1EnterMutex(pCache->pGroup);
37011
37012 if( pPg ){
37013 p->page.pBuf = pPg;
37014 p->page.pExtra = &p[1];
37015 if( pCache->bPurgeable ){
37016 pCache->pGroup->nCurrentPage++;
37017 }
37018 return p;
37019 }
37020 return 0;
37021 }
37022
37023 /*
37024 ** Free a page object allocated by pcache1AllocPage().
37025 **
@@ -36118,11 +37029,14 @@
37029 */
37030 static void pcache1FreePage(PgHdr1 *p){
37031 if( ALWAYS(p) ){
37032 PCache1 *pCache = p->pCache;
37033 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
37034 pcache1Free(p->page.pBuf);
37035 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
37036 sqlite3_free(p);
37037 #endif
37038 if( pCache->bPurgeable ){
37039 pCache->pGroup->nCurrentPage--;
37040 }
37041 }
37042 }
@@ -36153,17 +37067,17 @@
37067 ** it is desirable to avoid allocating a new page cache entry because
37068 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
37069 ** for all page cache needs and we should not need to spill the
37070 ** allocation onto the heap.
37071 **
37072 ** Or, the heap is used for all page cache memory but the heap is
37073 ** under memory pressure, then again it is desirable to avoid
37074 ** allocating a new page cache entry in order to avoid stressing
37075 ** the heap even further.
37076 */
37077 static int pcache1UnderMemoryPressure(PCache1 *pCache){
37078 if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
37079 return pcache1.bUnderPressure;
37080 }else{
37081 return sqlite3HeapNearlyFull();
37082 }
37083 }
@@ -36350,11 +37264,11 @@
37264 /*
37265 ** Implementation of the sqlite3_pcache.xCreate method.
37266 **
37267 ** Allocate a new cache.
37268 */
37269 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
37270 PCache1 *pCache; /* The newly created page cache */
37271 PGroup *pGroup; /* The group the new page cache will belong to */
37272 int sz; /* Bytes of memory required to allocate the new cache */
37273
37274 /*
@@ -36372,10 +37286,13 @@
37286 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
37287 const int separateCache = 0;
37288 #else
37289 int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
37290 #endif
37291
37292 assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
37293 assert( szExtra < 300 );
37294
37295 sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
37296 pCache = (PCache1 *)sqlite3_malloc(sz);
37297 if( pCache ){
37298 memset(pCache, 0, sz);
@@ -36385,10 +37302,11 @@
37302 }else{
37303 pGroup = &pcache1.grp;
37304 }
37305 pCache->pGroup = pGroup;
37306 pCache->szPage = szPage;
37307 pCache->szExtra = szExtra;
37308 pCache->bPurgeable = (bPurgeable ? 1 : 0);
37309 if( bPurgeable ){
37310 pCache->nMin = 10;
37311 pcache1EnterMutex(pGroup);
37312 pGroup->nMinPage += pCache->nMin;
@@ -36415,10 +37333,29 @@
37333 pCache->n90pct = pCache->nMax*9/10;
37334 pcache1EnforceMaxPage(pGroup);
37335 pcache1LeaveMutex(pGroup);
37336 }
37337 }
37338
37339 /*
37340 ** Implementation of the sqlite3_pcache.xShrink method.
37341 **
37342 ** Free up as much memory as possible.
37343 */
37344 static void pcache1Shrink(sqlite3_pcache *p){
37345 PCache1 *pCache = (PCache1*)p;
37346 if( pCache->bPurgeable ){
37347 PGroup *pGroup = pCache->pGroup;
37348 int savedMaxPage;
37349 pcache1EnterMutex(pGroup);
37350 savedMaxPage = pGroup->nMaxPage;
37351 pGroup->nMaxPage = 0;
37352 pcache1EnforceMaxPage(pGroup);
37353 pGroup->nMaxPage = savedMaxPage;
37354 pcache1LeaveMutex(pGroup);
37355 }
37356 }
37357
37358 /*
37359 ** Implementation of the sqlite3_pcache.xPagecount method.
37360 */
37361 static int pcache1Pagecount(sqlite3_pcache *p){
@@ -36441,11 +37378,11 @@
37378 ** means to try really hard to allocate a new page.
37379 **
37380 ** For a non-purgeable cache (a cache used as the storage for an in-memory
37381 ** database) there is really no difference between createFlag 1 and 2. So
37382 ** the calling function (pcache.c) will never have a createFlag of 1 on
37383 ** a non-purgeable cache.
37384 **
37385 ** There are three different approaches to obtaining space for a page,
37386 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
37387 **
37388 ** 1. Regardless of the value of createFlag, the cache is searched for a
@@ -36482,12 +37419,16 @@
37419 ** size, return the recycled buffer. Otherwise, free the buffer and
37420 ** proceed to step 5.
37421 **
37422 ** 5. Otherwise, allocate and return a new page buffer.
37423 */
37424 static sqlite3_pcache_page *pcache1Fetch(
37425 sqlite3_pcache *p,
37426 unsigned int iKey,
37427 int createFlag
37428 ){
37429 unsigned int nPinned;
37430 PCache1 *pCache = (PCache1 *)p;
37431 PGroup *pGroup;
37432 PgHdr1 *pPage = 0;
37433
37434 assert( pCache->bPurgeable || createFlag!=1 );
@@ -36517,19 +37458,18 @@
37458 */
37459 #ifdef SQLITE_MUTEX_OMIT
37460 pGroup = pCache->pGroup;
37461 #endif
37462
 
37463 /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
37464 assert( pCache->nPage >= pCache->nRecyclable );
37465 nPinned = pCache->nPage - pCache->nRecyclable;
 
37466 assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
37467 assert( pCache->n90pct == pCache->nMax*9/10 );
37468 if( createFlag==1 && (
37469 nPinned>=pGroup->mxPinned
37470 || nPinned>=pCache->n90pct
37471 || pcache1UnderMemoryPressure(pCache)
37472 )){
37473 goto fetch_out;
37474 }
37475
@@ -36541,20 +37481,28 @@
37481 if( pCache->bPurgeable && pGroup->pLruTail && (
37482 (pCache->nPage+1>=pCache->nMax)
37483 || pGroup->nCurrentPage>=pGroup->nMaxPage
37484 || pcache1UnderMemoryPressure(pCache)
37485 )){
37486 PCache1 *pOther;
37487 pPage = pGroup->pLruTail;
37488 pcache1RemoveFromHash(pPage);
37489 pcache1PinPage(pPage);
37490 pOther = pPage->pCache;
37491
37492 /* We want to verify that szPage and szExtra are the same for pOther
37493 ** and pCache. Assert that we can verify this by comparing sums. */
37494 assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
37495 assert( pCache->szExtra<512 );
37496 assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
37497 assert( pOther->szExtra<512 );
37498
37499 if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
37500 pcache1FreePage(pPage);
37501 pPage = 0;
37502 }else{
37503 pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
 
37504 }
37505 }
37506
37507 /* Step 5. If a usable page buffer has still not been found,
37508 ** attempt to allocate a new one.
@@ -36571,31 +37519,35 @@
37519 pPage->iKey = iKey;
37520 pPage->pNext = pCache->apHash[h];
37521 pPage->pCache = pCache;
37522 pPage->pLruPrev = 0;
37523 pPage->pLruNext = 0;
37524 *(void **)pPage->page.pExtra = 0;
37525 pCache->apHash[h] = pPage;
37526 }
37527
37528 fetch_out:
37529 if( pPage && iKey>pCache->iMaxKey ){
37530 pCache->iMaxKey = iKey;
37531 }
37532 pcache1LeaveMutex(pGroup);
37533 return &pPage->page;
37534 }
37535
37536
37537 /*
37538 ** Implementation of the sqlite3_pcache.xUnpin method.
37539 **
37540 ** Mark a page as unpinned (eligible for asynchronous recycling).
37541 */
37542 static void pcache1Unpin(
37543 sqlite3_pcache *p,
37544 sqlite3_pcache_page *pPg,
37545 int reuseUnlikely
37546 ){
37547 PCache1 *pCache = (PCache1 *)p;
37548 PgHdr1 *pPage = (PgHdr1 *)pPg;
37549 PGroup *pGroup = pCache->pGroup;
37550
37551 assert( pPage->pCache==pCache );
37552 pcache1EnterMutex(pGroup);
37553
@@ -36627,16 +37579,16 @@
37579 /*
37580 ** Implementation of the sqlite3_pcache.xRekey method.
37581 */
37582 static void pcache1Rekey(
37583 sqlite3_pcache *p,
37584 sqlite3_pcache_page *pPg,
37585 unsigned int iOld,
37586 unsigned int iNew
37587 ){
37588 PCache1 *pCache = (PCache1 *)p;
37589 PgHdr1 *pPage = (PgHdr1 *)pPg;
37590 PgHdr1 **pp;
37591 unsigned int h;
37592 assert( pPage->iKey==iOld );
37593 assert( pPage->pCache==pCache );
37594
@@ -36686,11 +37638,13 @@
37638 PCache1 *pCache = (PCache1 *)p;
37639 PGroup *pGroup = pCache->pGroup;
37640 assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
37641 pcache1EnterMutex(pGroup);
37642 pcache1TruncateUnsafe(pCache, 0);
37643 assert( pGroup->nMaxPage >= pCache->nMax );
37644 pGroup->nMaxPage -= pCache->nMax;
37645 assert( pGroup->nMinPage >= pCache->nMin );
37646 pGroup->nMinPage -= pCache->nMin;
37647 pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
37648 pcache1EnforceMaxPage(pGroup);
37649 pcache1LeaveMutex(pGroup);
37650 sqlite3_free(pCache->apHash);
@@ -36701,11 +37655,12 @@
37655 ** This function is called during initialization (sqlite3_initialize()) to
37656 ** install the default pluggable cache module, assuming the user has not
37657 ** already provided an alternative.
37658 */
37659 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
37660 static const sqlite3_pcache_methods2 defaultMethods = {
37661 1, /* iVersion */
37662 0, /* pArg */
37663 pcache1Init, /* xInit */
37664 pcache1Shutdown, /* xShutdown */
37665 pcache1Create, /* xCreate */
37666 pcache1Cachesize, /* xCachesize */
@@ -36712,13 +37667,14 @@
37667 pcache1Pagecount, /* xPagecount */
37668 pcache1Fetch, /* xFetch */
37669 pcache1Unpin, /* xUnpin */
37670 pcache1Rekey, /* xRekey */
37671 pcache1Truncate, /* xTruncate */
37672 pcache1Destroy, /* xDestroy */
37673 pcache1Shrink /* xShrink */
37674 };
37675 sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
37676 }
37677
37678 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
37679 /*
37680 ** This function is called to free superfluous dynamically allocated memory
@@ -36735,11 +37691,14 @@
37691 assert( sqlite3_mutex_notheld(pcache1.mutex) );
37692 if( pcache1.pStart==0 ){
37693 PgHdr1 *p;
37694 pcache1EnterMutex(&pcache1.grp);
37695 while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
37696 nFree += pcache1MemSize(p->page.pBuf);
37697 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
37698 nFree += sqlite3MemSize(p);
37699 #endif
37700 pcache1PinPage(p);
37701 pcache1RemoveFromHash(p);
37702 pcache1FreePage(p);
37703 }
37704 pcache1LeaveMutex(&pcache1.grp);
@@ -36763,12 +37722,12 @@
37722 int nRecyclable = 0;
37723 for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
37724 nRecyclable++;
37725 }
37726 *pnCurrent = pcache1.grp.nCurrentPage;
37727 *pnMax = (int)pcache1.grp.nMaxPage;
37728 *pnMin = (int)pcache1.grp.nMinPage;
37729 *pnRecyclable = nRecyclable;
37730 }
37731 #endif
37732
37733 /************** End of pcache1.c *********************************************/
@@ -37238,10 +38197,16 @@
38197
38198 #ifndef _WAL_H_
38199 #define _WAL_H_
38200
38201
38202 /* Additional values that can be added to the sync_flags argument of
38203 ** sqlite3WalFrames():
38204 */
38205 #define WAL_SYNC_TRANSACTIONS 0x20 /* Sync at the end of each transaction */
38206 #define SQLITE_SYNC_MASK 0x13 /* Mask off the SQLITE_SYNC_* values */
38207
38208 #ifdef SQLITE_OMIT_WAL
38209 # define sqlite3WalOpen(x,y,z) 0
38210 # define sqlite3WalLimit(x,y)
38211 # define sqlite3WalClose(w,x,y,z) 0
38212 # define sqlite3WalBeginReadTransaction(y,z) 0
@@ -37937,10 +38902,11 @@
38902 u8 useJournal; /* Use a rollback journal on this file */
38903 u8 noReadlock; /* Do not bother to obtain readlocks */
38904 u8 noSync; /* Do not sync the journal if true */
38905 u8 fullSync; /* Do extra syncs of the journal for robustness */
38906 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
38907 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
38908 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
38909 u8 tempFile; /* zFilename is a temporary file */
38910 u8 readOnly; /* True for a read-only database */
38911 u8 memDb; /* True to inhibit all file I/O */
38912
@@ -38107,11 +39073,11 @@
39073 return (pPager->pWal!=0);
39074 }
39075 #else
39076 # define pagerUseWal(x) 0
39077 # define pagerRollbackWal(x) 0
39078 # define pagerWalFrames(v,w,x,y) 0
39079 # define pagerOpenWalIfPresent(z) SQLITE_OK
39080 # define pagerBeginReadTransaction(z) SQLITE_OK
39081 #endif
39082
39083 #ifndef NDEBUG
@@ -39806,14 +40772,13 @@
40772 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
40773 newSize = szPage*(i64)nPage;
40774 if( rc==SQLITE_OK && currentSize!=newSize ){
40775 if( currentSize>newSize ){
40776 rc = sqlite3OsTruncate(pPager->fd, newSize);
40777 }else if( (currentSize+szPage)<=newSize ){
40778 char *pTmp = pPager->pTmpSpace;
40779 memset(pTmp, 0, szPage);
 
40780 testcase( (newSize-szPage) == currentSize );
40781 testcase( (newSize-szPage) > currentSize );
40782 rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
40783 }
40784 if( rc==SQLITE_OK ){
@@ -39835,27 +40800,40 @@
40800 **
40801 ** Otherwise, for non-temporary files, the effective sector size is
40802 ** the value returned by the xSectorSize() method rounded up to 32 if
40803 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
40804 ** is greater than MAX_SECTOR_SIZE.
40805 **
40806 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
40807 ** the effective sector size to its minimum value (512). The purpose of
40808 ** pPager->sectorSize is to define the "blast radius" of bytes that
40809 ** might change if a crash occurs while writing to a single byte in
40810 ** that range. But with POWERSAFE_OVERWRITE, the blast radius is zero
40811 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
40812 ** size. For backwards compatibility of the rollback journal file format,
40813 ** we cannot reduce the effective sector size below 512.
40814 */
40815 static void setSectorSize(Pager *pPager){
40816 assert( isOpen(pPager->fd) || pPager->tempFile );
40817
40818 if( pPager->tempFile
40819 || (sqlite3OsDeviceCharacteristics(pPager->fd) &
40820 SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
40821 ){
40822 /* Sector size doesn't matter for temporary files. Also, the file
40823 ** may not have been opened yet, in which case the OsSectorSize()
40824 ** call will segfault. */
40825 pPager->sectorSize = 512;
40826 }else{
40827 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
40828 if( pPager->sectorSize<32 ){
40829 pPager->sectorSize = 512;
40830 }
40831 if( pPager->sectorSize>MAX_SECTOR_SIZE ){
40832 assert( MAX_SECTOR_SIZE>=512 );
40833 pPager->sectorSize = MAX_SECTOR_SIZE;
40834 }
40835 }
40836 }
40837
40838 /*
40839 ** Playback the journal and thus restore the database file to
@@ -40054,14 +41032,15 @@
41032 /* Following a rollback, the database file should be back in its original
41033 ** state prior to the start of the transaction, so invoke the
41034 ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
41035 ** assertion that the transaction counter was modified.
41036 */
41037 #ifdef SQLITE_DEBUG
41038 if( pPager->fd->pMethods ){
41039 sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
41040 }
41041 #endif
41042
41043 /* If this playback is happening automatically as a result of an IO or
41044 ** malloc error that occurred after the change-counter was updated but
41045 ** before the transaction was committed, then the change-counter
41046 ** modification may just have been reverted. If this happens in exclusive
@@ -40276,12 +41255,11 @@
41255 */
41256 static int pagerWalFrames(
41257 Pager *pPager, /* Pager object */
41258 PgHdr *pList, /* List of frames to log */
41259 Pgno nTruncate, /* Database size after this commit */
41260 int isCommit /* True if this is a commit */
 
41261 ){
41262 int rc; /* Return code */
41263 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
41264 PgHdr *p; /* For looping over pages */
41265 #endif
@@ -40308,11 +41286,11 @@
41286 assert( pList );
41287 }
41288
41289 if( pList->pgno==1 ) pager_write_changecounter(pList);
41290 rc = sqlite3WalFrames(pPager->pWal,
41291 pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
41292 );
41293 if( rc==SQLITE_OK && pPager->pBackup ){
41294 PgHdr *p;
41295 for(p=pList; p; p=p->pDirty){
41296 sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
@@ -40395,14 +41373,11 @@
41373 int rc = sqlite3OsFileSize(pPager->fd, &n);
41374 if( rc!=SQLITE_OK ){
41375 return rc;
41376 }
41377 }
41378 nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
 
 
 
41379 }
41380
41381 /* If the current number of pages in the file is greater than the
41382 ** configured maximum pager number, increase the allowed limit so
41383 ** that the file can be read.
@@ -40588,17 +41563,17 @@
41563 ** previously rolled back out of the main journal (and are hence in pDone)
41564 ** will be skipped. Out-of-range pages are also skipped.
41565 */
41566 if( pSavepoint ){
41567 u32 ii; /* Loop counter */
41568 i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
41569
41570 if( pagerUseWal(pPager) ){
41571 rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
41572 }
41573 for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
41574 assert( offset==(i64)ii*(4+pPager->pageSize) );
41575 rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
41576 }
41577 assert( rc!=SQLITE_DONE );
41578 }
41579
@@ -40614,10 +41589,17 @@
41589 ** Change the maximum number of in-memory pages that are allowed.
41590 */
41591 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
41592 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
41593 }
41594
41595 /*
41596 ** Free as much memory as possible from the pager.
41597 */
41598 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41599 sqlite3PcacheShrink(pPager->pPCache);
41600 }
41601
41602 /*
41603 ** Adjust the robustness of the database to damage due to OS crashes
41604 ** or power failures by changing the number of syncs()s when writing
41605 ** the rollback journal. There are three levels:
@@ -40681,10 +41663,14 @@
41663 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41664 }else{
41665 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41666 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
41667 }
41668 pPager->walSyncFlags = pPager->syncFlags;
41669 if( pPager->fullSync ){
41670 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41671 }
41672 }
41673 #endif
41674
41675 /*
41676 ** The following global variable is incremented whenever the library
@@ -40818,11 +41804,11 @@
41804 if( !pNew ) rc = SQLITE_NOMEM;
41805 }
41806
41807 if( rc==SQLITE_OK ){
41808 pager_reset(pPager);
41809 pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
41810 pPager->pageSize = pageSize;
41811 sqlite3PageFree(pPager->pTmpSpace);
41812 pPager->pTmpSpace = pNew;
41813 sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
41814 }
@@ -41326,11 +42312,11 @@
42312 ** file size will be.
42313 */
42314 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
42315 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
42316 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
42317 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
42318 pPager->dbHintSize = pPager->dbSize;
42319 }
42320
42321 while( rc==SQLITE_OK && pList ){
42322 Pgno pgno = pList->pgno;
@@ -41435,11 +42421,11 @@
42421
42422 /* If the sub-journal was opened successfully (or was already open),
42423 ** write the journal record into the file. */
42424 if( rc==SQLITE_OK ){
42425 void *pData = pPg->pData;
42426 i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
42427 char *pData2;
42428
42429 CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
42430 PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
42431 rc = write32bits(pPager->sjfd, offset, pPg->pgno);
@@ -41508,11 +42494,11 @@
42494 /* Write a single frame for this page to the log. */
42495 if( subjRequiresPage(pPg) ){
42496 rc = subjournalPage(pPg);
42497 }
42498 if( rc==SQLITE_OK ){
42499 rc = pagerWalFrames(pPager, pPg, 0, 0);
42500 }
42501 }else{
42502
42503 /* Sync the journal file if required. */
42504 if( pPg->flags&PGHDR_NEED_SYNC
@@ -41667,11 +42653,12 @@
42653 z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
42654 while( *z ){
42655 z += sqlite3Strlen30(z)+1;
42656 z += sqlite3Strlen30(z)+1;
42657 }
42658 nUri = (int)(&z[1] - zUri);
42659 assert( nUri>=0 );
42660 if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
42661 /* This branch is taken when the journal path required by
42662 ** the database being opened will be more than pVfs->mxPathname
42663 ** bytes in length. This means the database cannot be opened,
42664 ** as it will not be possible to open the journal file or even
@@ -41701,13 +42688,13 @@
42688 ROUND8(sizeof(*pPager)) + /* Pager structure */
42689 ROUND8(pcacheSize) + /* PCache object */
42690 ROUND8(pVfs->szOsFile) + /* The main db file */
42691 journalFileSize * 2 + /* The two journal files */
42692 nPathname + 1 + nUri + /* zFilename */
42693 nPathname + 8 + 2 /* zJournal */
42694 #ifndef SQLITE_OMIT_WAL
42695 + nPathname + 4 + 2 /* zWal */
42696 #endif
42697 );
42698 assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
42699 if( !pPtr ){
42700 sqlite3_free(zPathname);
@@ -41726,16 +42713,16 @@
42713 assert( nPathname>0 );
42714 pPager->zJournal = (char*)(pPtr += nPathname + 1 + nUri);
42715 memcpy(pPager->zFilename, zPathname, nPathname);
42716 memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
42717 memcpy(pPager->zJournal, zPathname, nPathname);
42718 memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+1);
42719 sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
42720 #ifndef SQLITE_OMIT_WAL
42721 pPager->zWal = &pPager->zJournal[nPathname+8+1];
42722 memcpy(pPager->zWal, zPathname, nPathname);
42723 memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
42724 sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
42725 #endif
42726 sqlite3_free(zPathname);
42727 }
42728 pPager->pVfs = pVfs;
@@ -41847,13 +42834,21 @@
42834 pPager->changeCountDone = pPager->tempFile;
42835 pPager->memDb = (u8)memDb;
42836 pPager->readOnly = (u8)readOnly;
42837 assert( useJournal || pPager->tempFile );
42838 pPager->noSync = pPager->tempFile;
42839 if( pPager->noSync ){
42840 assert( pPager->fullSync==0 );
42841 assert( pPager->syncFlags==0 );
42842 assert( pPager->walSyncFlags==0 );
42843 assert( pPager->ckptSyncFlags==0 );
42844 }else{
42845 pPager->fullSync = 1;
42846 pPager->syncFlags = SQLITE_SYNC_NORMAL;
42847 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
42848 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
42849 }
42850 /* pPager->pFirst = 0; */
42851 /* pPager->pFirstSynced = 0; */
42852 /* pPager->pLast = 0; */
42853 pPager->nExtra = (u16)nExtra;
42854 pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
@@ -42982,11 +43977,14 @@
43977 if( !pPager->noSync ){
43978 assert( !MEMDB );
43979 rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
43980 }else if( isOpen(pPager->fd) ){
43981 assert( !MEMDB );
43982 rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC_OMITTED, 0);
43983 if( rc==SQLITE_NOTFOUND ){
43984 rc = SQLITE_OK;
43985 }
43986 }
43987 return rc;
43988 }
43989
43990 /*
@@ -43079,13 +44077,11 @@
44077 pList = pPageOne;
44078 pList->pDirty = 0;
44079 }
44080 assert( rc==SQLITE_OK );
44081 if( ALWAYS(pList) ){
44082 rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
 
 
44083 }
44084 sqlite3PagerUnref(pPageOne);
44085 if( rc==SQLITE_OK ){
44086 sqlite3PcacheCleanAll(pPager->pPCache);
44087 }
@@ -43340,11 +44336,12 @@
44336 }else{
44337 rc = pager_playback(pPager, 0);
44338 }
44339
44340 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
44341 assert( rc==SQLITE_OK || rc==SQLITE_FULL
44342 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
44343
44344 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
44345 ** cache. So call pager_error() on the way out to make any error persistent.
44346 */
44347 return pager_error(pPager, rc);
@@ -43980,10 +44977,19 @@
44977 ** sqlite3BackupUpdate() only.
44978 */
44979 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
44980 return &pPager->pBackup;
44981 }
44982
44983 #ifndef SQLITE_OMIT_VACUUM
44984 /*
44985 ** Unless this is an in-memory or temporary database, clear the pager cache.
44986 */
44987 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
44988 if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
44989 }
44990 #endif
44991
44992 #ifndef SQLITE_OMIT_WAL
44993 /*
44994 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
44995 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
@@ -44157,17 +45163,10 @@
45163 }
45164 }
45165 return rc;
45166 }
45167
 
 
 
 
 
 
 
45168 #ifdef SQLITE_HAS_CODEC
45169 /*
45170 ** This function is called by the wal module when writing page content
45171 ** into the log file.
45172 **
@@ -44600,17 +45599,22 @@
45599 sqlite3_file *pDbFd; /* File handle for the database file */
45600 sqlite3_file *pWalFd; /* File handle for WAL file */
45601 u32 iCallback; /* Value to pass to log callback (or 0) */
45602 i64 mxWalSize; /* Truncate WAL to this size upon reset */
45603 int nWiData; /* Size of array apWiData */
45604 int szFirstBlock; /* Size of first block written to WAL file */
45605 volatile u32 **apWiData; /* Pointer to wal-index content in memory */
45606 u32 szPage; /* Database page size */
45607 i16 readLock; /* Which read lock is being held. -1 for none */
45608 u8 syncFlags; /* Flags to use to sync header writes */
45609 u8 exclusiveMode; /* Non-zero if connection is in exclusive mode */
45610 u8 writeLock; /* True if in a write transaction */
45611 u8 ckptLock; /* True if holding a checkpoint lock */
45612 u8 readOnly; /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
45613 u8 truncateOnCommit; /* True to truncate WAL file on commit */
45614 u8 syncHeader; /* Fsync the WAL header if true */
45615 u8 padToSectorBoundary; /* Pad transactions out to the next sector */
45616 WalIndexHdr hdr; /* Wal-index header for current transaction */
45617 const char *zWalName; /* Name of WAL file */
45618 u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
45619 #ifdef SQLITE_DEBUG
45620 u8 lockError; /* True if a locking error has occurred */
@@ -45279,10 +46283,11 @@
46283 int iFrame; /* Index of last frame read */
46284 i64 iOffset; /* Next offset to read from log file */
46285 int szPage; /* Page size according to the log */
46286 u32 magic; /* Magic value read from WAL header */
46287 u32 version; /* Magic value read from WAL header */
46288 int isValid; /* True if this frame is valid */
46289
46290 /* Read in the WAL header. */
46291 rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
46292 if( rc!=SQLITE_OK ){
46293 goto recovery_error;
@@ -45337,18 +46342,18 @@
46342 /* Read all frames from the log file. */
46343 iFrame = 0;
46344 for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
46345 u32 pgno; /* Database page number for frame */
46346 u32 nTruncate; /* dbsize field from frame header */
 
46347
46348 /* Read and decode the next log frame. */
46349 iFrame++;
46350 rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
46351 if( rc!=SQLITE_OK ) break;
46352 isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
46353 if( !isValid ) break;
46354 rc = walIndexAppend(pWal, iFrame, pgno);
46355 if( rc!=SQLITE_OK ) break;
46356
46357 /* If nTruncate is non-zero, this is a commit record. */
46358 if( nTruncate ){
46359 pWal->hdr.mxFrame = iFrame;
@@ -45467,10 +46472,12 @@
46472 pRet->pWalFd = (sqlite3_file *)&pRet[1];
46473 pRet->pDbFd = pDbFd;
46474 pRet->readLock = -1;
46475 pRet->mxWalSize = mxWalSize;
46476 pRet->zWalName = zWalName;
46477 pRet->syncHeader = 1;
46478 pRet->padToSectorBoundary = 1;
46479 pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
46480
46481 /* Open file handle on the write-ahead log file. */
46482 flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
46483 rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
@@ -45481,10 +46488,15 @@
46488 if( rc!=SQLITE_OK ){
46489 walIndexClose(pRet, 0);
46490 sqlite3OsClose(pRet->pWalFd);
46491 sqlite3_free(pRet);
46492 }else{
46493 int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
46494 if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
46495 if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
46496 pRet->padToSectorBoundary = 0;
46497 }
46498 *ppWal = pRet;
46499 WALTRACE(("WAL%d: opened\n", pRet));
46500 }
46501 return rc;
46502 }
@@ -45900,11 +46912,11 @@
46912 */
46913 if( rc==SQLITE_OK ){
46914 i64 nReq = ((i64)mxPage * szPage);
46915 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
46916 if( rc==SQLITE_OK && nSize<nReq ){
46917 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
46918 }
46919 }
46920
46921 /* Iterate through the contents of the WAL, copying data to the db file. */
46922 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
@@ -45966,10 +46978,28 @@
46978
46979 walcheckpoint_out:
46980 walIteratorFree(pIter);
46981 return rc;
46982 }
46983
46984 /*
46985 ** If the WAL file is currently larger than nMax bytes in size, truncate
46986 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
46987 */
46988 static void walLimitSize(Wal *pWal, i64 nMax){
46989 i64 sz;
46990 int rx;
46991 sqlite3BeginBenignMalloc();
46992 rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
46993 if( rx==SQLITE_OK && (sz > nMax ) ){
46994 rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
46995 }
46996 sqlite3EndBenignMalloc();
46997 if( rx ){
46998 sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
46999 }
47000 }
47001
47002 /*
47003 ** Close a connection to a log file.
47004 */
47005 SQLITE_PRIVATE int sqlite3WalClose(
@@ -45990,27 +47020,44 @@
47020 **
47021 ** The EXCLUSIVE lock is not released before returning.
47022 */
47023 rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
47024 if( rc==SQLITE_OK ){
 
47025 if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
47026 pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
47027 }
47028 rc = sqlite3WalCheckpoint(
47029 pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
47030 );
47031 if( rc==SQLITE_OK ){
47032 int bPersist = -1;
47033 sqlite3OsFileControlHint(
47034 pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
47035 );
47036 if( bPersist!=1 ){
47037 /* Try to delete the WAL file if the checkpoint completed and
47038 ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
47039 ** mode (!bPersist) */
47040 isDelete = 1;
47041 }else if( pWal->mxWalSize>=0 ){
47042 /* Try to truncate the WAL file to zero bytes if the checkpoint
47043 ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
47044 ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
47045 ** non-negative value (pWal->mxWalSize>=0). Note that we truncate
47046 ** to zero bytes as truncating to the journal_size_limit might
47047 ** leave a corrupt WAL file on disk. */
47048 walLimitSize(pWal, 0);
47049 }
47050 }
47051 }
47052
47053 walIndexClose(pWal, isDelete);
47054 sqlite3OsClose(pWal->pWalFd);
47055 if( isDelete ){
47056 sqlite3BeginBenignMalloc();
47057 sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
47058 sqlite3EndBenignMalloc();
47059 }
47060 WALTRACE(("WAL%p: closed\n", pWal));
47061 sqlite3_free((void *)pWal->apWiData);
47062 sqlite3_free(pWal);
47063 }
@@ -46496,11 +47543,11 @@
47543 }
47544 nCollide = HASHTABLE_NSLOT;
47545 for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
47546 u32 iFrame = aHash[iKey] + iZero;
47547 if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
47548 /* assert( iFrame>iRead ); -- not true if there is corruption */
47549 iRead = iFrame;
47550 }
47551 if( (nCollide--)==0 ){
47552 return SQLITE_CORRUPT_BKPT;
47553 }
@@ -46608,10 +47655,11 @@
47655 */
47656 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
47657 if( pWal->writeLock ){
47658 walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
47659 pWal->writeLock = 0;
47660 pWal->truncateOnCommit = 0;
47661 }
47662 return SQLITE_OK;
47663 }
47664
47665 /*
@@ -46703,10 +47751,11 @@
47751 walCleanupHash(pWal);
47752 }
47753
47754 return rc;
47755 }
47756
47757
47758 /*
47759 ** This function is called just before writing a set of frames to the log
47760 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
47761 ** to the current log file, it is possible to overwrite the start of the
@@ -46741,27 +47790,10 @@
47790 ** to handle if this transaction is rolled back.
47791 */
47792 int i; /* Loop counter */
47793 u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */
47794
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47795 pWal->nCkpt++;
47796 pWal->hdr.mxFrame = 0;
47797 sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
47798 aSalt[1] = salt1;
47799 walIndexWriteHdr(pWal);
@@ -46785,10 +47817,78 @@
47817 testcase( rc==SQLITE_PROTOCOL );
47818 testcase( rc==SQLITE_OK );
47819 }
47820 return rc;
47821 }
47822
47823 /*
47824 ** Information about the current state of the WAL file and where
47825 ** the next fsync should occur - passed from sqlite3WalFrames() into
47826 ** walWriteToLog().
47827 */
47828 typedef struct WalWriter {
47829 Wal *pWal; /* The complete WAL information */
47830 sqlite3_file *pFd; /* The WAL file to which we write */
47831 sqlite3_int64 iSyncPoint; /* Fsync at this offset */
47832 int syncFlags; /* Flags for the fsync */
47833 int szPage; /* Size of one page */
47834 } WalWriter;
47835
47836 /*
47837 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
47838 ** Do a sync when crossing the p->iSyncPoint boundary.
47839 **
47840 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
47841 ** first write the part before iSyncPoint, then sync, then write the
47842 ** rest.
47843 */
47844 static int walWriteToLog(
47845 WalWriter *p, /* WAL to write to */
47846 void *pContent, /* Content to be written */
47847 int iAmt, /* Number of bytes to write */
47848 sqlite3_int64 iOffset /* Start writing at this offset */
47849 ){
47850 int rc;
47851 if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
47852 int iFirstAmt = (int)(p->iSyncPoint - iOffset);
47853 rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
47854 if( rc ) return rc;
47855 iOffset += iFirstAmt;
47856 iAmt -= iFirstAmt;
47857 pContent = (void*)(iFirstAmt + (char*)pContent);
47858 assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
47859 rc = sqlite3OsSync(p->pFd, p->syncFlags);
47860 if( iAmt==0 || rc ) return rc;
47861 }
47862 rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
47863 return rc;
47864 }
47865
47866 /*
47867 ** Write out a single frame of the WAL
47868 */
47869 static int walWriteOneFrame(
47870 WalWriter *p, /* Where to write the frame */
47871 PgHdr *pPage, /* The page of the frame to be written */
47872 int nTruncate, /* The commit flag. Usually 0. >0 for commit */
47873 sqlite3_int64 iOffset /* Byte offset at which to write */
47874 ){
47875 int rc; /* Result code from subfunctions */
47876 void *pData; /* Data actually written */
47877 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
47878 #if defined(SQLITE_HAS_CODEC)
47879 if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
47880 #else
47881 pData = pPage->pData;
47882 #endif
47883 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
47884 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
47885 if( rc ) return rc;
47886 /* Write the page data */
47887 rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
47888 return rc;
47889 }
47890
47891 /*
47892 ** Write a set of frames to the log. The caller must hold the write-lock
47893 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
47894 */
@@ -46800,17 +47900,23 @@
47900 int isCommit, /* True if this is a commit */
47901 int sync_flags /* Flags to pass to OsSync() (or 0) */
47902 ){
47903 int rc; /* Used to catch return codes */
47904 u32 iFrame; /* Next frame address */
 
47905 PgHdr *p; /* Iterator to run through pList with. */
47906 PgHdr *pLast = 0; /* Last frame in list */
47907 int nExtra = 0; /* Number of extra copies of last page */
47908 int szFrame; /* The size of a single frame */
47909 i64 iOffset; /* Next byte to write in WAL file */
47910 WalWriter w; /* The writer */
47911
47912 assert( pList );
47913 assert( pWal->writeLock );
47914
47915 /* If this frame set completes a transaction, then nTruncate>0. If
47916 ** nTruncate==0 then this frame set does not complete the transaction. */
47917 assert( (isCommit!=0)==(nTruncate!=0) );
47918
47919 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
47920 { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
47921 WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
47922 pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
@@ -46835,91 +47941,103 @@
47941
47942 sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
47943 sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
47944 sqlite3Put4byte(&aWalHdr[8], szPage);
47945 sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
47946 if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
47947 memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
47948 walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
47949 sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
47950 sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
47951
47952 pWal->szPage = szPage;
47953 pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
47954 pWal->hdr.aFrameCksum[0] = aCksum[0];
47955 pWal->hdr.aFrameCksum[1] = aCksum[1];
47956 pWal->truncateOnCommit = 1;
47957
47958 rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
47959 WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
47960 if( rc!=SQLITE_OK ){
47961 return rc;
47962 }
47963
47964 /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
47965 ** all syncing is turned off by PRAGMA synchronous=OFF). Otherwise
47966 ** an out-of-order write following a WAL restart could result in
47967 ** database corruption. See the ticket:
47968 **
47969 ** http://localhost:591/sqlite/info/ff5be73dee
47970 */
47971 if( pWal->syncHeader && sync_flags ){
47972 rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
47973 if( rc ) return rc;
47974 }
47975 }
47976 assert( (int)pWal->szPage==szPage );
47977
47978 /* Setup information needed to write frames into the WAL */
47979 w.pWal = pWal;
47980 w.pFd = pWal->pWalFd;
47981 w.iSyncPoint = 0;
47982 w.syncFlags = sync_flags;
47983 w.szPage = szPage;
47984 iOffset = walFrameOffset(iFrame+1, szPage);
47985 szFrame = szPage + WAL_FRAME_HDRSIZE;
47986
47987 /* Write all frames into the log file exactly once */
47988 for(p=pList; p; p=p->pDirty){
47989 int nDbSize; /* 0 normally. Positive == commit flag */
47990 iFrame++;
47991 assert( iOffset==walFrameOffset(iFrame, szPage) );
47992 nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
47993 rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
47994 if( rc ) return rc;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47995 pLast = p;
47996 iOffset += szFrame;
47997 }
47998
47999 /* If this is the end of a transaction, then we might need to pad
48000 ** the transaction and/or sync the WAL file.
48001 **
48002 ** Padding and syncing only occur if this set of frames complete a
48003 ** transaction and if PRAGMA synchronous=FULL. If synchronous==NORMAL
48004 ** or synchonous==OFF, then no padding or syncing are needed.
48005 **
48006 ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
48007 ** needed and only the sync is done. If padding is needed, then the
48008 ** final frame is repeated (with its commit mark) until the next sector
48009 ** boundary is crossed. Only the part of the WAL prior to the last
48010 ** sector boundary is synced; the part of the last frame that extends
48011 ** past the sector boundary is written after the sync.
48012 */
48013 if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
48014 if( pWal->padToSectorBoundary ){
48015 int sectorSize = sqlite3OsSectorSize(pWal->pWalFd);
48016 w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
48017 while( iOffset<w.iSyncPoint ){
48018 rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
48019 if( rc ) return rc;
48020 iOffset += szFrame;
48021 nExtra++;
48022 }
48023 }else{
48024 rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
48025 }
48026 }
48027
48028 /* If this frame set completes the first transaction in the WAL and
48029 ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
48030 ** journal size limit, if possible.
48031 */
48032 if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
48033 i64 sz = pWal->mxWalSize;
48034 if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
48035 sz = walFrameOffset(iFrame+nExtra+1, szPage);
48036 }
48037 walLimitSize(pWal, sz);
48038 pWal->truncateOnCommit = 0;
48039 }
48040
48041 /* Append data to the wal-index. It is not necessary to lock the
48042 ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
48043 ** guarantees that there are no other writers, and no data that may
@@ -46928,13 +48046,13 @@
48046 iFrame = pWal->hdr.mxFrame;
48047 for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
48048 iFrame++;
48049 rc = walIndexAppend(pWal, iFrame, p->pgno);
48050 }
48051 while( rc==SQLITE_OK && nExtra>0 ){
48052 iFrame++;
48053 nExtra--;
48054 rc = walIndexAppend(pWal, iFrame, pLast->pgno);
48055 }
48056
48057 if( rc==SQLITE_OK ){
48058 /* Update the private copy of the header. */
@@ -47436,10 +48554,11 @@
48554 u8 intKey; /* True if intkey flag is set */
48555 u8 leaf; /* True if leaf flag is set */
48556 u8 hasData; /* True if this page stores data */
48557 u8 hdrOffset; /* 100 for page 1. 0 otherwise */
48558 u8 childPtrSize; /* 0 if leaf==1. 4 if leaf==0 */
48559 u8 max1bytePayload; /* min(maxLocal,127) */
48560 u16 maxLocal; /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
48561 u16 minLocal; /* Copy of BtShared.minLocal or BtShared.minLeaf */
48562 u16 cellOffset; /* Index in aData of first cell pointer */
48563 u16 nFree; /* Number of free bytes on the page */
48564 u16 nCell; /* Number of cells on this page, local and ovfl */
@@ -47448,10 +48567,12 @@
48567 u8 *pCell; /* Pointers to the body of the overflow cell */
48568 u16 idx; /* Insert this cell before idx-th non-overflow cell */
48569 } aOvfl[5];
48570 BtShared *pBt; /* Pointer to BtShared that this page is part of */
48571 u8 *aData; /* Pointer to disk image of the page data */
48572 u8 *aDataEnd; /* One byte past the end of usable data */
48573 u8 *aCellIdx; /* The cell index area */
48574 DbPage *pDbPage; /* Pager page handle */
48575 Pgno pgno; /* Page number for this page */
48576 };
48577
48578 /*
@@ -47527,11 +48648,11 @@
48648 #define TRANS_WRITE 2
48649
48650 /*
48651 ** An instance of this object represents a single database file.
48652 **
48653 ** A single database file can be in use at the same time by two
48654 ** or more database connections. When two or more connections are
48655 ** sharing the same database file, each connection has it own
48656 ** private Btree object for the file and each of those Btrees points
48657 ** to this one BtShared object. BtShared.nRef is the number of
48658 ** connections currently sharing this database file.
@@ -47564,21 +48685,18 @@
48685 struct BtShared {
48686 Pager *pPager; /* The page cache */
48687 sqlite3 *db; /* Database connection currently using this Btree */
48688 BtCursor *pCursor; /* A list of all open cursors */
48689 MemPage *pPage1; /* First page of the database */
 
 
 
 
48690 u8 openFlags; /* Flags to sqlite3BtreeOpen() */
48691 #ifndef SQLITE_OMIT_AUTOVACUUM
48692 u8 autoVacuum; /* True if auto-vacuum is enabled */
48693 u8 incrVacuum; /* True if incr-vacuum is enabled */
48694 #endif
48695 u8 inTransaction; /* Transaction state */
48696 u8 max1bytePayload; /* Maximum first byte of cell for a 1-byte payload */
48697 u16 btsFlags; /* Boolean parameters. See BTS_* macros below */
48698 u16 maxLocal; /* Maximum local payload in non-LEAFDATA tables */
48699 u16 minLocal; /* Minimum local payload in non-LEAFDATA tables */
48700 u16 maxLeaf; /* Maximum local payload in a LEAFDATA table */
48701 u16 minLeaf; /* Minimum local payload in a LEAFDATA table */
48702 u32 pageSize; /* Total number of bytes on a page */
@@ -47592,16 +48710,25 @@
48710 #ifndef SQLITE_OMIT_SHARED_CACHE
48711 int nRef; /* Number of references to this structure */
48712 BtShared *pNext; /* Next on a list of sharable BtShared structs */
48713 BtLock *pLock; /* List of locks held on this shared-btree struct */
48714 Btree *pWriter; /* Btree with currently open write transaction */
 
 
48715 #endif
48716 u8 *pTmpSpace; /* BtShared.pageSize bytes of space for tmp use */
48717 };
48718
48719 /*
48720 ** Allowed values for BtShared.btsFlags
48721 */
48722 #define BTS_READ_ONLY 0x0001 /* Underlying file is readonly */
48723 #define BTS_PAGESIZE_FIXED 0x0002 /* Page size can no longer be changed */
48724 #define BTS_SECURE_DELETE 0x0004 /* PRAGMA secure_delete is enabled */
48725 #define BTS_INITIALLY_EMPTY 0x0008 /* Database was empty at trans start */
48726 #define BTS_NO_WAL 0x0010 /* Do not open write-ahead-log files */
48727 #define BTS_EXCLUSIVE 0x0020 /* pWriter has an exclusive lock */
48728 #define BTS_PENDING 0x0040 /* Waiting for read-locks to clear */
48729
48730 /*
48731 ** An instance of the following structure is used to hold information
48732 ** about a cell. The parseCellPtr() function fills in this structure
48733 ** based on information extract from the raw disk page.
48734 */
@@ -47633,11 +48760,11 @@
48760 ** b-tree within a database file.
48761 **
48762 ** The entry is identified by its MemPage and the index in
48763 ** MemPage.aCell[] of the entry.
48764 **
48765 ** A single database file can be shared by two more database connections,
48766 ** but cursors cannot be shared. Each cursor is associated with a
48767 ** particular database connection identified BtCursor.pBtree.db.
48768 **
48769 ** Fields in this structure are accessed under the BtShared.mutex
48770 ** found at self->pBt->mutex.
@@ -47794,11 +48921,11 @@
48921 int mallocFailed; /* A memory allocation error has occurred */
48922 StrAccum errMsg; /* Accumulate the error message text here */
48923 };
48924
48925 /*
48926 ** Routines to read or write a two- and four-byte big-endian integer values.
48927 */
48928 #define get2byte(x) ((x)[0]<<8 | (x)[1])
48929 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
48930 #define get4byte sqlite3Get4byte
48931 #define put4byte sqlite3Put4byte
@@ -48319,11 +49446,11 @@
49446 }
49447
49448 /* If some other connection is holding an exclusive lock, the
49449 ** requested lock may not be obtained.
49450 */
49451 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
49452 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
49453 return SQLITE_LOCKED_SHAREDCACHE;
49454 }
49455
49456 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
@@ -48340,11 +49467,11 @@
49467 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
49468 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
49469 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
49470 if( eLock==WRITE_LOCK ){
49471 assert( p==pBt->pWriter );
49472 pBt->btsFlags |= BTS_PENDING;
49473 }
49474 return SQLITE_LOCKED_SHAREDCACHE;
49475 }
49476 }
49477 return SQLITE_OK;
@@ -48428,11 +49555,11 @@
49555 /*
49556 ** Release all the table locks (locks obtained via calls to
49557 ** the setSharedCacheTableLock() procedure) held by Btree object p.
49558 **
49559 ** This function assumes that Btree p has an open read or write
49560 ** transaction. If it does not, then the BTS_PENDING flag
49561 ** may be incorrectly cleared.
49562 */
49563 static void clearAllSharedCacheTableLocks(Btree *p){
49564 BtShared *pBt = p->pBt;
49565 BtLock **ppIter = &pBt->pLock;
@@ -48441,11 +49568,11 @@
49568 assert( p->sharable || 0==*ppIter );
49569 assert( p->inTrans>0 );
49570
49571 while( *ppIter ){
49572 BtLock *pLock = *ppIter;
49573 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
49574 assert( pLock->pBtree->inTrans>=pLock->eLock );
49575 if( pLock->pBtree==p ){
49576 *ppIter = pLock->pNext;
49577 assert( pLock->iTable!=1 || pLock==&p->lock );
49578 if( pLock->iTable!=1 ){
@@ -48454,26 +49581,25 @@
49581 }else{
49582 ppIter = &pLock->pNext;
49583 }
49584 }
49585
49586 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
49587 if( pBt->pWriter==p ){
49588 pBt->pWriter = 0;
49589 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 
49590 }else if( pBt->nTransaction==2 ){
49591 /* This function is called when Btree p is concluding its
49592 ** transaction. If there currently exists a writer, and p is not
49593 ** that writer, then the number of locks held by connections other
49594 ** than the writer must be about to drop to zero. In this case
49595 ** set the BTS_PENDING flag to 0.
49596 **
49597 ** If there is not currently a writer, then BTS_PENDING must
49598 ** be zero already. So this next line is harmless in that case.
49599 */
49600 pBt->btsFlags &= ~BTS_PENDING;
49601 }
49602 }
49603
49604 /*
49605 ** This function changes all write-locks held by Btree p into read-locks.
@@ -48481,12 +49607,11 @@
49607 static void downgradeAllSharedCacheTableLocks(Btree *p){
49608 BtShared *pBt = p->pBt;
49609 if( pBt->pWriter==p ){
49610 BtLock *pLock;
49611 pBt->pWriter = 0;
49612 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
 
49613 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
49614 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
49615 pLock->eLock = READ_LOCK;
49616 }
49617 }
@@ -48935,11 +50060,11 @@
50060 ** to the cell content.
50061 **
50062 ** This routine works only for pages that do not contain overflow cells.
50063 */
50064 #define findCell(P,I) \
50065 ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
50066 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
50067
50068
50069 /*
50070 ** This a more complex version of findCell() that works for
@@ -49340,11 +50465,11 @@
50465 assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
50466 assert( (start + size) <= (int)pPage->pBt->usableSize );
50467 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
50468 assert( size>=0 ); /* Minimum cell size is 4 */
50469
50470 if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
50471 /* Overwrite deleted information with zeros when the secure_delete
50472 ** option is enabled */
50473 memset(&data[start], 0, size);
50474 }
50475
@@ -49443,10 +50568,11 @@
50568 pPage->maxLocal = pBt->maxLocal;
50569 pPage->minLocal = pBt->minLocal;
50570 }else{
50571 return SQLITE_CORRUPT_BKPT;
50572 }
50573 pPage->max1bytePayload = pBt->max1bytePayload;
50574 return SQLITE_OK;
50575 }
50576
50577 /*
50578 ** Initialize the auxiliary information for a disk block.
@@ -49485,10 +50611,12 @@
50611 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
50612 pPage->maskPage = (u16)(pBt->pageSize - 1);
50613 pPage->nOverflow = 0;
50614 usableSize = pBt->usableSize;
50615 pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
50616 pPage->aDataEnd = &data[usableSize];
50617 pPage->aCellIdx = &data[cellOffset];
50618 top = get2byteNotZero(&data[hdr+5]);
50619 pPage->nCell = get2byte(&data[hdr+3]);
50620 if( pPage->nCell>MX_CELL(pBt) ){
50621 /* To many cells for a single page. The page must be corrupt */
50622 return SQLITE_CORRUPT_BKPT;
@@ -49576,11 +50704,11 @@
50704 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
50705 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
50706 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
50707 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
50708 assert( sqlite3_mutex_held(pBt->mutex) );
50709 if( pBt->btsFlags & BTS_SECURE_DELETE ){
50710 memset(&data[hdr], 0, pBt->usableSize - hdr);
50711 }
50712 data[hdr] = (char)flags;
50713 first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
50714 memset(&data[hdr+1], 0, 4);
@@ -49588,10 +50716,12 @@
50716 put2byte(&data[hdr+5], pBt->usableSize);
50717 pPage->nFree = (u16)(pBt->usableSize - first);
50718 decodeFlags(pPage, flags);
50719 pPage->hdrOffset = hdr;
50720 pPage->cellOffset = first;
50721 pPage->aDataEnd = &data[pBt->usableSize];
50722 pPage->aCellIdx = &data[first];
50723 pPage->nOverflow = 0;
50724 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
50725 pPage->maskPage = (u16)(pBt->pageSize - 1);
50726 pPage->nCell = 0;
50727 pPage->isInit = 1;
@@ -49848,11 +50978,16 @@
50978 p->sharable = 1;
50979 if( !zFullPathname ){
50980 sqlite3_free(p);
50981 return SQLITE_NOMEM;
50982 }
50983 rc = sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
50984 if( rc ){
50985 sqlite3_free(zFullPathname);
50986 sqlite3_free(p);
50987 return rc;
50988 }
50989 #if SQLITE_THREADSAFE
50990 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
50991 sqlite3_mutex_enter(mutexOpen);
50992 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50993 sqlite3_mutex_enter(mutexShared);
@@ -49922,13 +51057,13 @@
51057 sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
51058 p->pBt = pBt;
51059
51060 pBt->pCursor = 0;
51061 pBt->pPage1 = 0;
51062 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
51063 #ifdef SQLITE_SECURE_DELETE
51064 pBt->btsFlags |= BTS_SECURE_DELETE;
51065 #endif
51066 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
51067 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
51068 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
51069 pBt->pageSize = 0;
@@ -49945,11 +51080,11 @@
51080 }
51081 #endif
51082 nReserve = 0;
51083 }else{
51084 nReserve = zDbHeader[20];
51085 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
51086 #ifndef SQLITE_OMIT_AUTOVACUUM
51087 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
51088 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
51089 #endif
51090 }
@@ -50233,19 +51368,19 @@
51368 ** at the beginning of a page.
51369 **
51370 ** If parameter nReserve is less than zero, then the number of reserved
51371 ** bytes per page is left unchanged.
51372 **
51373 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
51374 ** and autovacuum mode can no longer be changed.
51375 */
51376 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
51377 int rc = SQLITE_OK;
51378 BtShared *pBt = p->pBt;
51379 assert( nReserve>=-1 && nReserve<=255 );
51380 sqlite3BtreeEnter(p);
51381 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
51382 sqlite3BtreeLeave(p);
51383 return SQLITE_READONLY;
51384 }
51385 if( nReserve<0 ){
51386 nReserve = pBt->pageSize - pBt->usableSize;
@@ -50258,11 +51393,11 @@
51393 pBt->pageSize = (u32)pageSize;
51394 freeTempSpace(pBt);
51395 }
51396 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
51397 pBt->usableSize = pBt->pageSize - (u16)nReserve;
51398 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
51399 sqlite3BtreeLeave(p);
51400 return rc;
51401 }
51402
51403 /*
@@ -50298,22 +51433,23 @@
51433 sqlite3BtreeLeave(p);
51434 return n;
51435 }
51436
51437 /*
51438 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1. If newFlag is -1,
51439 ** then make no changes. Always return the value of the BTS_SECURE_DELETE
51440 ** setting after the change.
51441 */
51442 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
51443 int b;
51444 if( p==0 ) return 0;
51445 sqlite3BtreeEnter(p);
51446 if( newFlag>=0 ){
51447 p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
51448 if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
51449 }
51450 b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
51451 sqlite3BtreeLeave(p);
51452 return b;
51453 }
51454 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
51455
@@ -50330,11 +51466,11 @@
51466 BtShared *pBt = p->pBt;
51467 int rc = SQLITE_OK;
51468 u8 av = (u8)autoVacuum;
51469
51470 sqlite3BtreeEnter(p);
51471 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
51472 rc = SQLITE_READONLY;
51473 }else{
51474 pBt->autoVacuum = av ?1:0;
51475 pBt->incrVacuum = av==2 ?1:0;
51476 }
@@ -50404,18 +51540,18 @@
51540 goto page1_init_failed;
51541 }
51542
51543 #ifdef SQLITE_OMIT_WAL
51544 if( page1[18]>1 ){
51545 pBt->btsFlags |= BTS_READ_ONLY;
51546 }
51547 if( page1[19]>1 ){
51548 goto page1_init_failed;
51549 }
51550 #else
51551 if( page1[18]>2 ){
51552 pBt->btsFlags |= BTS_READ_ONLY;
51553 }
51554 if( page1[19]>2 ){
51555 goto page1_init_failed;
51556 }
51557
@@ -50425,11 +51561,11 @@
51561 ** The caller detects this and calls this function again. This is
51562 ** required as the version of page 1 currently in the page1 buffer
51563 ** may not be the latest version - there may be a newer one in the log
51564 ** file.
51565 */
51566 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
51567 int isOpen = 0;
51568 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
51569 if( rc!=SQLITE_OK ){
51570 goto page1_init_failed;
51571 }else if( isOpen==0 ){
@@ -50502,10 +51638,15 @@
51638 */
51639 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
51640 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
51641 pBt->maxLeaf = (u16)(pBt->usableSize - 35);
51642 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
51643 if( pBt->maxLocal>127 ){
51644 pBt->max1bytePayload = 127;
51645 }else{
51646 pBt->max1bytePayload = (u8)pBt->maxLocal;
51647 }
51648 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
51649 pBt->pPage1 = pPage1;
51650 pBt->nPage = nPage;
51651 return SQLITE_OK;
51652
@@ -50565,11 +51706,11 @@
51706 data[21] = 64;
51707 data[22] = 32;
51708 data[23] = 32;
51709 memset(&data[24], 0, 100-24);
51710 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
51711 pBt->btsFlags |= BTS_PAGESIZE_FIXED;
51712 #ifndef SQLITE_OMIT_AUTOVACUUM
51713 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
51714 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
51715 put4byte(&data[36 + 4*4], pBt->autoVacuum);
51716 put4byte(&data[36 + 7*4], pBt->incrVacuum);
@@ -50629,21 +51770,23 @@
51770 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
51771 goto trans_begun;
51772 }
51773
51774 /* Write transactions are not possible on a read-only database */
51775 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
51776 rc = SQLITE_READONLY;
51777 goto trans_begun;
51778 }
51779
51780 #ifndef SQLITE_OMIT_SHARED_CACHE
51781 /* If another database handle has already opened a write transaction
51782 ** on this shared-btree structure and a second write transaction is
51783 ** requested, return SQLITE_LOCKED.
51784 */
51785 if( (wrflag && pBt->inTransaction==TRANS_WRITE)
51786 || (pBt->btsFlags & BTS_PENDING)!=0
51787 ){
51788 pBlock = pBt->pWriter->db;
51789 }else if( wrflag>1 ){
51790 BtLock *pIter;
51791 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
51792 if( pIter->pBtree!=p ){
@@ -50663,11 +51806,12 @@
51806 ** page 1. So if some other shared-cache client already has a write-lock
51807 ** on page 1, the transaction cannot be opened. */
51808 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
51809 if( SQLITE_OK!=rc ) goto trans_begun;
51810
51811 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
51812 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
51813 do {
51814 /* Call lockBtree() until either pBt->pPage1 is populated or
51815 ** lockBtree() returns something other than SQLITE_OK. lockBtree()
51816 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
51817 ** reading page 1 it discovers that the page-size of the database
@@ -50675,11 +51819,11 @@
51819 ** pBt->pageSize to the page-size of the file on disk.
51820 */
51821 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
51822
51823 if( rc==SQLITE_OK && wrflag ){
51824 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
51825 rc = SQLITE_READONLY;
51826 }else{
51827 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
51828 if( rc==SQLITE_OK ){
51829 rc = newDatabase(pBt);
@@ -50712,11 +51856,12 @@
51856 if( wrflag ){
51857 MemPage *pPage1 = pBt->pPage1;
51858 #ifndef SQLITE_OMIT_SHARED_CACHE
51859 assert( !pBt->pWriter );
51860 pBt->pWriter = p;
51861 pBt->btsFlags &= ~BTS_EXCLUSIVE;
51862 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
51863 #endif
51864
51865 /* If the db-size header field is incorrect (as it may be if an old
51866 ** client has been writing the database file), update it now. Doing
51867 ** this sooner rather than later means the database size can safely
@@ -51441,11 +52586,11 @@
52586 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
52587 int rc;
52588 BtShared *pBt = p->pBt;
52589 sqlite3BtreeEnter(p);
52590 assert( p->inTrans==TRANS_WRITE );
52591 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
52592 assert( iStatement>0 );
52593 assert( iStatement>p->db->nSavepoint );
52594 assert( pBt->inTransaction==TRANS_WRITE );
52595 /* At the pager level, a statement transaction is a savepoint with
52596 ** an index greater than all savepoints created explicitly using
@@ -51476,11 +52621,13 @@
52621 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
52622 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
52623 sqlite3BtreeEnter(p);
52624 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
52625 if( rc==SQLITE_OK ){
52626 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
52627 pBt->nPage = 0;
52628 }
52629 rc = newDatabase(pBt);
52630 pBt->nPage = get4byte(28 + pBt->pPage1->aData);
52631
52632 /* The database size was written into the offset 28 of the header
52633 ** when the transaction started, so we know that the value at offset
@@ -51546,11 +52693,11 @@
52693 /* Assert that the caller has opened the required transaction. */
52694 assert( p->inTrans>TRANS_NONE );
52695 assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
52696 assert( pBt->pPage1 && pBt->pPage1->aData );
52697
52698 if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
52699 return SQLITE_READONLY;
52700 }
52701 if( iTable==1 && btreePagecount(pBt)==0 ){
52702 assert( wrFlag==0 );
52703 iTable = 0;
@@ -52046,11 +53193,11 @@
53193 && pBt->pPage1->aData[19]==0x01 /* (5) */
53194 ){
53195 u8 aSave[4];
53196 u8 *aWrite = &pBuf[-4];
53197 memcpy(aSave, aWrite, 4);
53198 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
53199 nextPage = get4byte(aWrite);
53200 memcpy(aWrite, aSave, 4);
53201 }else
53202 #endif
53203
@@ -52626,20 +53773,25 @@
53773 ** the entire cell by checking for the cases where the record is
53774 ** stored entirely within the b-tree page by inspecting the first
53775 ** 2 bytes of the cell.
53776 */
53777 int nCell = pCell[0];
53778 if( nCell<=pPage->max1bytePayload
53779 /* && (pCell+nCell)<pPage->aDataEnd */
53780 ){
53781 /* This branch runs if the record-size field of the cell is a
53782 ** single byte varint and the record fits entirely on the main
53783 ** b-tree page. */
53784 testcase( pCell+nCell+1==pPage->aDataEnd );
53785 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
53786 }else if( !(pCell[1] & 0x80)
53787 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
53788 /* && (pCell+nCell+2)<=pPage->aDataEnd */
53789 ){
53790 /* The record-size field is a 2 byte varint and the record
53791 ** fits entirely on the main b-tree page. */
53792 testcase( pCell+nCell+2==pPage->aDataEnd );
53793 c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
53794 }else{
53795 /* The record flows over onto one or more overflow pages. In
53796 ** this case the whole cell needs to be parsed, a buffer allocated
53797 ** and accessPayload() used to retrieve the record into the
@@ -53177,11 +54329,11 @@
54329 rc = sqlite3PagerWrite(pPage1->pDbPage);
54330 if( rc ) goto freepage_out;
54331 nFree = get4byte(&pPage1->aData[36]);
54332 put4byte(&pPage1->aData[36], nFree+1);
54333
54334 if( pBt->btsFlags & BTS_SECURE_DELETE ){
54335 /* If the secure_delete option is enabled, then
54336 ** always fully overwrite deleted information with zeros.
54337 */
54338 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
54339 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
@@ -53238,11 +54390,11 @@
54390 */
54391 rc = sqlite3PagerWrite(pTrunk->pDbPage);
54392 if( rc==SQLITE_OK ){
54393 put4byte(&pTrunk->aData[4], nLeaf+1);
54394 put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
54395 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
54396 sqlite3PagerDontWrite(pPage->pDbPage);
54397 }
54398 rc = btreeSetHasContent(pBt, iPage);
54399 }
54400 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
@@ -53530,11 +54682,11 @@
54682 assert( idx>=0 && idx<pPage->nCell );
54683 assert( sz==cellSize(pPage, idx) );
54684 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
54685 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
54686 data = pPage->aData;
54687 ptr = &pPage->aCellIdx[2*idx];
54688 pc = get2byte(ptr);
54689 hdr = pPage->hdrOffset;
54690 testcase( pc==get2byte(&data[hdr+5]) );
54691 testcase( pc+sz==pPage->pBt->usableSize );
54692 if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
@@ -53544,11 +54696,11 @@
54696 rc = freeSpace(pPage, pc, sz);
54697 if( rc ){
54698 *pRC = rc;
54699 return;
54700 }
54701 endPtr = &pPage->aCellIdx[2*pPage->nCell - 2];
54702 assert( (SQLITE_PTR_TO_INT(ptr)&1)==0 ); /* ptr is always 2-byte aligned */
54703 while( ptr<endPtr ){
54704 *(u16*)ptr = *(u16*)&ptr[2];
54705 ptr += 2;
54706 }
@@ -53686,11 +54838,11 @@
54838
54839 /* Check that the page has just been zeroed by zeroPage() */
54840 assert( pPage->nCell==0 );
54841 assert( get2byteNotZero(&data[hdr+5])==nUsable );
54842
54843 pCellptr = &pPage->aCellIdx[nCell*2];
54844 cellbody = nUsable;
54845 for(i=nCell-1; i>=0; i--){
54846 u16 sz = aSize[i];
54847 pCellptr -= 2;
54848 cellbody -= sz;
@@ -54079,11 +55231,11 @@
55231 ** But not if we are in secure-delete mode. In secure-delete mode,
55232 ** the dropCell() routine will overwrite the entire cell with zeroes.
55233 ** In this case, temporarily copy the cell into the aOvflSpace[]
55234 ** buffer. It will be copied out again as soon as the aSpace[] buffer
55235 ** is allocated. */
55236 if( pBt->btsFlags & BTS_SECURE_DELETE ){
55237 int iOff;
55238
55239 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
55240 if( (iOff+szNew[i])>(int)pBt->usableSize ){
55241 rc = SQLITE_CORRUPT_BKPT;
@@ -54264,12 +55416,18 @@
55416 }
55417
55418 /* Either we found one or more cells (cntnew[0])>0) or pPage is
55419 ** a virtual root page. A virtual root page is when the real root
55420 ** page is page 1 and we are the only child of that page.
55421 **
55422 ** UPDATE: The assert() below is not necessarily true if the database
55423 ** file is corrupt. The corruption will be detected and reported later
55424 ** in this procedure so there is no need to act upon it now.
55425 */
55426 #if 0
55427 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
55428 #endif
55429
55430 TRACE(("BALANCE: old: %d %d %d ",
55431 apOld[0]->pgno,
55432 nOld>=2 ? apOld[1]->pgno : 0,
55433 nOld>=3 ? apOld[2]->pgno : 0
@@ -54815,11 +55973,12 @@
55973 assert( pCur->skipNext!=SQLITE_OK );
55974 return pCur->skipNext;
55975 }
55976
55977 assert( cursorHoldsMutex(pCur) );
55978 assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
55979 && (pBt->btsFlags & BTS_READ_ONLY)==0 );
55980 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
55981
55982 /* Assert that the caller has been consistent. If this cursor was opened
55983 ** expecting an index b-tree, then the caller should be inserting blob
55984 ** keys with no associated data. If the cursor was opened expecting an
@@ -54944,11 +56103,11 @@
56103 int iCellIdx; /* Index of cell to delete */
56104 int iCellDepth; /* Depth of node containing pCell */
56105
56106 assert( cursorHoldsMutex(pCur) );
56107 assert( pBt->inTransaction==TRANS_WRITE );
56108 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
56109 assert( pCur->wrFlag );
56110 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
56111 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
56112
56113 if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
@@ -55065,11 +56224,11 @@
56224 int rc;
56225 int ptfFlags; /* Page-type flage for the root page of new table */
56226
56227 assert( sqlite3BtreeHoldsMutex(p) );
56228 assert( pBt->inTransaction==TRANS_WRITE );
56229 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
56230
56231 #ifdef SQLITE_OMIT_AUTOVACUUM
56232 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
56233 if( rc ){
56234 return rc;
@@ -55439,11 +56598,13 @@
56598 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
56599
56600 /* If auto-vacuum is disabled in this build and this is an auto-vacuum
56601 ** database, mark the database as read-only. */
56602 #ifdef SQLITE_OMIT_AUTOVACUUM
56603 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
56604 pBt->btsFlags |= BTS_READ_ONLY;
56605 }
56606 #endif
56607
56608 sqlite3BtreeLeave(p);
56609 }
56610
@@ -56239,11 +57400,12 @@
57400 ** (e) the cursor points at a valid row of an intKey table.
57401 */
57402 if( !pCsr->wrFlag ){
57403 return SQLITE_READONLY;
57404 }
57405 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
57406 && pCsr->pBt->inTransaction==TRANS_WRITE );
57407 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
57408 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
57409 assert( pCsr->apPage[pCsr->iPage]->intKey );
57410
57411 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
@@ -56279,11 +57441,12 @@
57441 assert( iVersion==1 || iVersion==2 );
57442
57443 /* If setting the version fields to 1, do not automatically open the
57444 ** WAL connection, even if the version fields are currently set to 2.
57445 */
57446 pBt->btsFlags &= ~BTS_NO_WAL;
57447 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
57448
57449 rc = sqlite3BtreeBeginTrans(pBtree, 0);
57450 if( rc==SQLITE_OK ){
57451 u8 *aData = pBt->pPage1->aData;
57452 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
@@ -56296,11 +57459,11 @@
57459 }
57460 }
57461 }
57462 }
57463
57464 pBt->btsFlags &= ~BTS_NO_WAL;
57465 return rc;
57466 }
57467
57468 /************** End of btree.c ***********************************************/
57469 /************** Begin file backup.c ******************************************/
@@ -56980,11 +58143,13 @@
58143
58144 assert( sqlite3BtreeIsInTrans(pTo) );
58145 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
58146 if( pFd->pMethods ){
58147 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
58148 rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
58149 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
58150 if( rc ) goto copy_finished;
58151 }
58152
58153 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
58154 ** to 0. This is used by the implementations of sqlite3_backup_step()
58155 ** and sqlite3_backup_finish() to detect that they are being called
@@ -57005,16 +58170,17 @@
58170 */
58171 sqlite3_backup_step(&b, 0x7FFFFFFF);
58172 assert( b.rc!=SQLITE_OK );
58173 rc = sqlite3_backup_finish(&b);
58174 if( rc==SQLITE_OK ){
58175 pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
58176 }else{
58177 sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
58178 }
58179
58180 assert( sqlite3BtreeIsInTrans(pTo)==0 );
58181 copy_finished:
58182 sqlite3BtreeLeave(pFrom);
58183 sqlite3BtreeLeave(pTo);
58184 return rc;
58185 }
58186 #endif /* SQLITE_OMIT_VACUUM */
@@ -57037,16 +58203,10 @@
58203 ** stores a single value in the VDBE. Mem is an opaque structure visible
58204 ** only within the VDBE. Interface routines refer to a Mem using the
58205 ** name sqlite_value
58206 */
58207
 
 
 
 
 
 
58208 /*
58209 ** If pMem is an object with a valid string representation, this routine
58210 ** ensures the internal encoding for the string representation is
58211 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
58212 **
@@ -57142,11 +58302,11 @@
58302 */
58303 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
58304 int f;
58305 assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
58306 assert( (pMem->flags&MEM_RowSet)==0 );
58307 ExpandBlob(pMem);
58308 f = pMem->flags;
58309 if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
58310 if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
58311 return SQLITE_NOMEM;
58312 }
@@ -57311,11 +58471,11 @@
58471 ** Release any memory held by the Mem. This may leave the Mem in an
58472 ** inconsistent state, for example with (Mem.z==0) and
58473 ** (Mem.type==SQLITE_TEXT).
58474 */
58475 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
58476 VdbeMemRelease(p);
58477 sqlite3DbFree(p->db, p->zMalloc);
58478 p->z = 0;
58479 p->zMalloc = 0;
58480 p->xDel = 0;
58481 }
@@ -57607,11 +58767,11 @@
58767 ** copies of this cell as invalid.
58768 **
58769 ** This is used for testing and debugging only - to make sure shallow
58770 ** copies are not misused.
58771 */
58772 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
58773 int i;
58774 Mem *pX;
58775 for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
58776 if( pX->pScopyFrom==pMem ){
58777 pX->flags |= MEM_Invalid;
@@ -57633,11 +58793,11 @@
58793 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
58794 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
58795 */
58796 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
58797 assert( (pFrom->flags & MEM_RowSet)==0 );
58798 VdbeMemRelease(pTo);
58799 memcpy(pTo, pFrom, MEMCELLSIZE);
58800 pTo->xDel = 0;
58801 if( (pFrom->flags&MEM_Static)==0 ){
58802 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
58803 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57651,11 +58811,11 @@
58811 */
58812 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
58813 int rc = SQLITE_OK;
58814
58815 assert( (pFrom->flags & MEM_RowSet)==0 );
58816 VdbeMemRelease(pTo);
58817 memcpy(pTo, pFrom, MEMCELLSIZE);
58818 pTo->flags &= ~MEM_Dyn;
58819
58820 if( pTo->flags&(MEM_Str|MEM_Blob) ){
58821 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57979,20 +59139,20 @@
59139 if( pVal->flags&MEM_Null ){
59140 return 0;
59141 }
59142 assert( (MEM_Blob>>3) == MEM_Str );
59143 pVal->flags |= (pVal->flags & MEM_Blob)>>3;
59144 ExpandBlob(pVal);
59145 if( pVal->flags&MEM_Str ){
59146 sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
59147 if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
59148 assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
59149 if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
59150 return 0;
59151 }
59152 }
59153 sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
59154 }else{
59155 assert( (pVal->flags&MEM_Blob)==0 );
59156 sqlite3VdbeMemStringify(pVal, enc);
59157 assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
59158 }
@@ -58369,11 +59529,12 @@
59529 return addr;
59530 }
59531
59532 /*
59533 ** Add an OP_ParseSchema opcode. This routine is broken out from
59534 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
59535 ** as having been used.
59536 **
59537 ** The zWhere string must have been obtained from sqlite3_malloc().
59538 ** This routine will take ownership of the allocated memory.
59539 */
59540 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
@@ -59086,17 +60247,18 @@
60247 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
60248 break;
60249 }
60250 case P4_MEM: {
60251 Mem *pMem = pOp->p4.pMem;
 
60252 if( pMem->flags & MEM_Str ){
60253 zP4 = pMem->z;
60254 }else if( pMem->flags & MEM_Int ){
60255 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
60256 }else if( pMem->flags & MEM_Real ){
60257 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
60258 }else if( pMem->flags & MEM_Null ){
60259 sqlite3_snprintf(nTemp, zTemp, "NULL");
60260 }else{
60261 assert( pMem->flags & MEM_Blob );
60262 zP4 = "(blob)";
60263 }
60264 break;
@@ -59135,12 +60297,13 @@
60297
60298 /*
60299 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
60300 **
60301 ** The prepared statements need to know in advance the complete set of
60302 ** attached databases that will be use. A mask of these databases
60303 ** is maintained in p->btreeMask. The p->lockMask value is the subset of
60304 ** p->btreeMask of databases that will require a lock.
60305 */
60306 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
60307 assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
60308 assert( i<(int)sizeof(p->btreeMask)*8 );
60309 p->btreeMask |= ((yDbMask)1)<<i;
@@ -59267,11 +60430,11 @@
60430 }else if( p->zMalloc ){
60431 sqlite3DbFree(db, p->zMalloc);
60432 p->zMalloc = 0;
60433 }
60434
60435 p->flags = MEM_Invalid;
60436 }
60437 db->mallocFailed = malloc_failed;
60438 }
60439 }
60440
@@ -59642,10 +60805,11 @@
60805 sqlite3 *db; /* The database connection */
60806 int nVar; /* Number of parameters */
60807 int nMem; /* Number of VM memory registers */
60808 int nCursor; /* Number of cursors required */
60809 int nArg; /* Number of arguments in subprograms */
60810 int nOnce; /* Number of OP_Once instructions */
60811 int n; /* Loop counter */
60812 u8 *zCsr; /* Memory available for allocation */
60813 u8 *zEnd; /* First byte past allocated memory */
60814 int nByte; /* How much extra memory is needed */
60815
@@ -59657,10 +60821,12 @@
60821 assert( db->mallocFailed==0 );
60822 nVar = pParse->nVar;
60823 nMem = pParse->nMem;
60824 nCursor = pParse->nTab;
60825 nArg = pParse->nMaxArg;
60826 nOnce = pParse->nOnce;
60827 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
60828
60829 /* For each cursor required, also allocate a memory cell. Memory
60830 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
60831 ** the vdbe program. Instead they are used to allocate space for
60832 ** VdbeCursor/BtCursor structures. The blob of memory associated with
@@ -59703,18 +60869,20 @@
60869 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
60870 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
60871 p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
60872 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
60873 &zCsr, zEnd, &nByte);
60874 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
60875 if( nByte ){
60876 p->pFree = sqlite3DbMallocZero(db, nByte);
60877 }
60878 zCsr = p->pFree;
60879 zEnd = &zCsr[nByte];
60880 }while( nByte && !db->mallocFailed );
60881
60882 p->nCursor = (u16)nCursor;
60883 p->nOnceFlag = nOnce;
60884 if( p->aVar ){
60885 p->nVar = (ynVar)nVar;
60886 for(n=0; n<nVar; n++){
60887 p->aVar[n].flags = MEM_Null;
60888 p->aVar[n].db = db;
@@ -59727,11 +60895,11 @@
60895 }
60896 if( p->aMem ){
60897 p->aMem--; /* aMem[] goes from 1..nMem */
60898 p->nMem = nMem; /* not from 0..nMem-1 */
60899 for(n=1; n<=nMem; n++){
60900 p->aMem[n].flags = MEM_Invalid;
60901 p->aMem[n].db = db;
60902 }
60903 }
60904 p->explain = pParse->explain;
60905 sqlite3VdbeRewind(p);
@@ -59769,10 +60937,12 @@
60937 ** is used, for example, when a trigger sub-program is halted to restore
60938 ** control to the main program.
60939 */
60940 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
60941 Vdbe *v = pFrame->v;
60942 v->aOnceFlag = pFrame->aOnceFlag;
60943 v->nOnceFlag = pFrame->nOnceFlag;
60944 v->aOp = pFrame->aOp;
60945 v->nOp = pFrame->nOp;
60946 v->aMem = pFrame->aMem;
60947 v->nMem = pFrame->nMem;
60948 v->apCsr = pFrame->apCsr;
@@ -59831,12 +61001,14 @@
61001
61002 #ifdef SQLITE_DEBUG
61003 /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
61004 ** Vdbe.aMem[] arrays have already been cleaned up. */
61005 int i;
61006 if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
61007 if( p->aMem ){
61008 for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
61009 }
61010 #endif
61011
61012 sqlite3DbFree(db, p->zErrMsg);
61013 p->zErrMsg = 0;
61014 p->pResultSet = 0;
@@ -59997,20 +61169,35 @@
61169 char *zMaster = 0; /* File-name for the master journal */
61170 char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
61171 sqlite3_file *pMaster = 0;
61172 i64 offset = 0;
61173 int res;
61174 int retryCount = 0;
61175 int nMainFile;
61176
61177 /* Select a master journal file name */
61178 nMainFile = sqlite3Strlen30(zMainFile);
61179 zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
61180 if( zMaster==0 ) return SQLITE_NOMEM;
61181 do {
61182 u32 iRandom;
61183 if( retryCount ){
61184 if( retryCount>100 ){
61185 sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
61186 sqlite3OsDelete(pVfs, zMaster, 0);
61187 break;
61188 }else if( retryCount==1 ){
61189 sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
61190 }
61191 }
61192 retryCount++;
61193 sqlite3_randomness(sizeof(iRandom), &iRandom);
61194 sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
61195 (iRandom>>8)&0xffffff, iRandom&0xff);
61196 /* The antipenultimate character of the master journal name must
61197 ** be "9" to avoid name collisions when using 8+3 filenames. */
61198 assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
61199 sqlite3FileSuffix3(zMainFile, zMaster);
61200 rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
61201 }while( rc==SQLITE_OK && res );
61202 if( rc==SQLITE_OK ){
61203 /* Open the master journal. */
@@ -60300,10 +61487,11 @@
61487 */
61488
61489 if( p->db->mallocFailed ){
61490 p->rc = SQLITE_NOMEM;
61491 }
61492 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
61493 closeAllCursors(p);
61494 if( p->magic!=VDBE_MAGIC_RUN ){
61495 return SQLITE_OK;
61496 }
61497 checkActiveVdbeCnt(db);
@@ -60637,10 +61825,14 @@
61825 vdbeFreeOpArray(db, p->aOp, p->nOp);
61826 sqlite3DbFree(db, p->aLabel);
61827 sqlite3DbFree(db, p->aColName);
61828 sqlite3DbFree(db, p->zSql);
61829 sqlite3DbFree(db, p->pFree);
61830 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
61831 sqlite3DbFree(db, p->zExplain);
61832 sqlite3DbFree(db, p->pExplain);
61833 #endif
61834 sqlite3DbFree(db, p);
61835 }
61836
61837 /*
61838 ** Delete an entire VDBE.
@@ -61116,19 +62308,10 @@
62308 ** longer key. However if the UNPACKED_INCRKEY flags in pPKey2 is set
62309 ** and the common prefixes are equal, then key1 is less than key2.
62310 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
62311 ** equal, then the keys are considered to be equal and
62312 ** the parts beyond the common prefix are ignored.
 
 
 
 
 
 
 
 
 
62313 */
62314 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
62315 int nKey1, const void *pKey1, /* Left key */
62316 UnpackedRecord *pPKey2 /* Right key */
62317 ){
@@ -61157,13 +62340,10 @@
62340 */
62341 /* mem1.u.i = 0; // not needed, here to silence compiler warning */
62342
62343 idx1 = getVarint32(aKey1, szHdr1);
62344 d1 = szHdr1;
 
 
 
62345 nField = pKeyInfo->nField;
62346 while( idx1<szHdr1 && i<pPKey2->nField ){
62347 u32 serial_type1;
62348
62349 /* Read the serial types for the next element in each key. */
@@ -61339,11 +62519,11 @@
62519 memset(&m, 0, sizeof(m));
62520 rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (int)nCellKey, 1, &m);
62521 if( rc ){
62522 return rc;
62523 }
62524 assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
62525 *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
62526 sqlite3VdbeMemRelease(&m);
62527 return SQLITE_OK;
62528 }
62529
@@ -61782,11 +62962,11 @@
62962 ** since any application that receives an SQLITE_MISUSE is broken by
62963 ** definition.
62964 **
62965 ** Nevertheless, some published applications that were originally written
62966 ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
62967 ** returns, and those were broken by the automatic-reset change. As a
62968 ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
62969 ** legacy behavior of returning SQLITE_MISUSE for cases where the
62970 ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
62971 ** or SQLITE_BUSY error.
62972 */
@@ -62128,17 +63308,17 @@
63308 pOut = &pVm->pResultSet[i];
63309 }else{
63310 /* If the value passed as the second argument is out of range, return
63311 ** a pointer to the following static Mem object which contains the
63312 ** value SQL NULL. Even though the Mem structure contains an element
63313 ** of type i64, on certain architectures (x86) with certain compiler
63314 ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
63315 ** instead of an 8-byte one. This all works fine, except that when
63316 ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
63317 ** that a Mem structure is located on an 8-byte boundary. To prevent
63318 ** these assert()s from failing, when building with SQLITE_DEBUG defined
63319 ** using gcc, we force nullMem to be 8-byte aligned using the magical
63320 ** __attribute__((aligned(8))) macro. */
63321 static const Mem nullMem
63322 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
63323 __attribute__((aligned(8)))
63324 #endif
@@ -62704,10 +63884,18 @@
63884 ** database.
63885 */
63886 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
63887 return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
63888 }
63889
63890 /*
63891 ** Return true if the prepared statement is in need of being reset.
63892 */
63893 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
63894 Vdbe *v = (Vdbe*)pStmt;
63895 return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
63896 }
63897
63898 /*
63899 ** Return a pointer to the next prepared statement after pStmt associated
63900 ** with database connection pDb. If pStmt is NULL, return the first
63901 ** prepared statement for the database connection. Return NULL if there
@@ -62749,10 +63937,12 @@
63937 **
63938 *************************************************************************
63939 **
63940 ** This file contains code used to insert the values of host parameters
63941 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
63942 **
63943 ** The Vdbe parse-tree explainer is also found here.
63944 */
63945
63946 #ifndef SQLITE_OMIT_TRACE
63947
63948 /*
@@ -62887,10 +64077,125 @@
64077 }
64078 return sqlite3StrAccumFinish(&out);
64079 }
64080
64081 #endif /* #ifndef SQLITE_OMIT_TRACE */
64082
64083 /*****************************************************************************
64084 ** The following code implements the data-structure explaining logic
64085 ** for the Vdbe.
64086 */
64087
64088 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
64089
64090 /*
64091 ** Allocate a new Explain object
64092 */
64093 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
64094 if( pVdbe ){
64095 sqlite3BeginBenignMalloc();
64096 Explain *p = sqlite3_malloc( sizeof(Explain) );
64097 if( p ){
64098 memset(p, 0, sizeof(*p));
64099 p->pVdbe = pVdbe;
64100 sqlite3_free(pVdbe->pExplain);
64101 pVdbe->pExplain = p;
64102 sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
64103 SQLITE_MAX_LENGTH);
64104 p->str.useMalloc = 2;
64105 }else{
64106 sqlite3EndBenignMalloc();
64107 }
64108 }
64109 }
64110
64111 /*
64112 ** Return true if the Explain ends with a new-line.
64113 */
64114 static int endsWithNL(Explain *p){
64115 return p && p->str.zText && p->str.nChar
64116 && p->str.zText[p->str.nChar-1]=='\n';
64117 }
64118
64119 /*
64120 ** Append text to the indentation
64121 */
64122 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
64123 Explain *p;
64124 if( pVdbe && (p = pVdbe->pExplain)!=0 ){
64125 va_list ap;
64126 if( p->nIndent && endsWithNL(p) ){
64127 int n = p->nIndent;
64128 if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
64129 sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
64130 }
64131 va_start(ap, zFormat);
64132 sqlite3VXPrintf(&p->str, 1, zFormat, ap);
64133 va_end(ap);
64134 }
64135 }
64136
64137 /*
64138 ** Append a '\n' if there is not already one.
64139 */
64140 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
64141 Explain *p;
64142 if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
64143 sqlite3StrAccumAppend(&p->str, "\n", 1);
64144 }
64145 }
64146
64147 /*
64148 ** Push a new indentation level. Subsequent lines will be indented
64149 ** so that they begin at the current cursor position.
64150 */
64151 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
64152 Explain *p;
64153 if( pVdbe && (p = pVdbe->pExplain)!=0 ){
64154 if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
64155 const char *z = p->str.zText;
64156 int i = p->str.nChar-1;
64157 int x;
64158 while( i>=0 && z[i]!='\n' ){ i--; }
64159 x = (p->str.nChar - 1) - i;
64160 if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
64161 x = p->aIndent[p->nIndent-1];
64162 }
64163 p->aIndent[p->nIndent] = x;
64164 }
64165 p->nIndent++;
64166 }
64167 }
64168
64169 /*
64170 ** Pop the indentation stack by one level.
64171 */
64172 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
64173 if( p && p->pExplain ) p->pExplain->nIndent--;
64174 }
64175
64176 /*
64177 ** Free the indentation structure
64178 */
64179 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
64180 if( pVdbe && pVdbe->pExplain ){
64181 sqlite3_free(pVdbe->zExplain);
64182 sqlite3ExplainNL(pVdbe);
64183 pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
64184 sqlite3_free(pVdbe->pExplain);
64185 pVdbe->pExplain = 0;
64186 sqlite3EndBenignMalloc();
64187 }
64188 }
64189
64190 /*
64191 ** Return the explanation of a virtual machine.
64192 */
64193 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
64194 return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
64195 }
64196 #endif /* defined(SQLITE_DEBUG) */
64197
64198 /************** End of vdbetrace.c *******************************************/
64199 /************** Begin file vdbe.c ********************************************/
64200 /*
64201 ** 2001 September 15
@@ -62942,11 +64247,11 @@
64247 ** Invoke this macro on memory cells just prior to changing the
64248 ** value of the cell. This macro verifies that shallow copies are
64249 ** not misused.
64250 */
64251 #ifdef SQLITE_DEBUG
64252 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
64253 #else
64254 # define memAboutToChange(P,M)
64255 #endif
64256
64257 /*
@@ -62960,12 +64265,12 @@
64265 SQLITE_API int sqlite3_search_count = 0;
64266 #endif
64267
64268 /*
64269 ** When this global variable is positive, it gets decremented once before
64270 ** each instruction in the VDBE. When it reaches zero, the u1.isInterrupted
64271 ** field of the sqlite3 structure is set in order to simulate an interrupt.
64272 **
64273 ** This facility is used for testing purposes only. It does not function
64274 ** in an ordinary build.
64275 */
64276 #ifdef SQLITE_TEST
@@ -63041,16 +64346,10 @@
64346 */
64347 #define Deephemeralize(P) \
64348 if( ((P)->flags&MEM_Ephem)!=0 \
64349 && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
64350
 
 
 
 
 
 
64351 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
64352 #ifdef SQLITE_OMIT_MERGE_SORT
64353 # define isSorter(x) 0
64354 #else
64355 # define isSorter(x) ((x)->pSorter!=0)
@@ -63086,11 +64385,11 @@
64385 */
64386 static VdbeCursor *allocateCursor(
64387 Vdbe *p, /* The virtual machine */
64388 int iCur, /* Index of the new VdbeCursor */
64389 int nField, /* Number of fields in the table or index */
64390 int iDb, /* Database the cursor belongs to, or -1 */
64391 int isBtreeCursor /* True for B-Tree. False for pseudo-table or vtab */
64392 ){
64393 /* Find the memory cell that will be used to store the blob of memory
64394 ** required for this VdbeCursor structure. It is convenient to use a
64395 ** vdbe memory cell to manage the memory allocation required for a
@@ -63457,11 +64756,11 @@
64756 ** sqlite3_interrupt() routine has been called. If it has been, then
64757 ** processing of the VDBE program is interrupted.
64758 **
64759 ** This macro added to every instruction that does a jump in order to
64760 ** implement a loop. This test used to be on every single instruction,
64761 ** but that meant we more testing than we needed. By only testing the
64762 ** flag on jump instructions, we get a (small) speed improvement.
64763 */
64764 #define CHECK_FOR_INTERRUPT \
64765 if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
64766
@@ -63567,69 +64866,72 @@
64866 */
64867 union vdbeExecUnion {
64868 struct OP_Yield_stack_vars {
64869 int pcDest;
64870 } aa;
64871 struct OP_Null_stack_vars {
64872 int cnt;
64873 } ab;
64874 struct OP_Variable_stack_vars {
64875 Mem *pVar; /* Value being transferred */
64876 } ac;
64877 struct OP_Move_stack_vars {
64878 char *zMalloc; /* Holding variable for allocated memory */
64879 int n; /* Number of registers left to copy */
64880 int p1; /* Register to copy from */
64881 int p2; /* Register to copy to */
64882 } ad;
64883 struct OP_ResultRow_stack_vars {
64884 Mem *pMem;
64885 int i;
64886 } ae;
64887 struct OP_Concat_stack_vars {
64888 i64 nByte;
64889 } af;
64890 struct OP_Remainder_stack_vars {
64891 int flags; /* Combined MEM_* flags from both inputs */
64892 i64 iA; /* Integer value of left operand */
64893 i64 iB; /* Integer value of right operand */
64894 double rA; /* Real value of left operand */
64895 double rB; /* Real value of right operand */
64896 } ag;
64897 struct OP_Function_stack_vars {
64898 int i;
64899 Mem *pArg;
64900 sqlite3_context ctx;
64901 sqlite3_value **apVal;
64902 int n;
64903 } ah;
64904 struct OP_ShiftRight_stack_vars {
64905 i64 iA;
64906 u64 uA;
64907 i64 iB;
64908 u8 op;
64909 } ai;
64910 struct OP_Ge_stack_vars {
64911 int res; /* Result of the comparison of pIn1 against pIn3 */
64912 char affinity; /* Affinity to use for comparison */
64913 u16 flags1; /* Copy of initial value of pIn1->flags */
64914 u16 flags3; /* Copy of initial value of pIn3->flags */
64915 } aj;
64916 struct OP_Compare_stack_vars {
64917 int n;
64918 int i;
64919 int p1;
64920 int p2;
64921 const KeyInfo *pKeyInfo;
64922 int idx;
64923 CollSeq *pColl; /* Collating sequence to use on this term */
64924 int bRev; /* True for DESCENDING sort order */
64925 } ak;
64926 struct OP_Or_stack_vars {
64927 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64928 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
64929 } al;
64930 struct OP_IfNot_stack_vars {
64931 int c;
64932 } am;
64933 struct OP_Column_stack_vars {
64934 u32 payloadSize; /* Number of bytes in the record */
64935 i64 payloadSize64; /* Number of bytes in the record */
64936 int p1; /* P1 value of the opcode */
64937 int p2; /* column number to retrieve */
@@ -63650,15 +64952,15 @@
64952 u32 szField; /* Number of bytes in the content of a field */
64953 int szHdr; /* Size of the header size field at start of record */
64954 int avail; /* Number of bytes of available data */
64955 u32 t; /* A type code from the record header */
64956 Mem *pReg; /* PseudoTable input register */
64957 } an;
64958 struct OP_Affinity_stack_vars {
64959 const char *zAffinity; /* The affinity to be applied */
64960 char cAff; /* A single character of affinity */
64961 } ao;
64962 struct OP_MakeRecord_stack_vars {
64963 u8 *zNewRecord; /* A buffer to hold the data for the new record */
64964 Mem *pRec; /* The new record */
64965 u64 nData; /* Number of bytes of data space */
64966 int nHdr; /* Number of bytes of header space */
@@ -63671,108 +64973,108 @@
64973 int nField; /* Number of fields in the record */
64974 char *zAffinity; /* The affinity string for the record */
64975 int file_format; /* File format to use for encoding */
64976 int i; /* Space used in zNewRecord[] */
64977 int len; /* Length of a field */
64978 } ap;
64979 struct OP_Count_stack_vars {
64980 i64 nEntry;
64981 BtCursor *pCrsr;
64982 } aq;
64983 struct OP_Savepoint_stack_vars {
64984 int p1; /* Value of P1 operand */
64985 char *zName; /* Name of savepoint */
64986 int nName;
64987 Savepoint *pNew;
64988 Savepoint *pSavepoint;
64989 Savepoint *pTmp;
64990 int iSavepoint;
64991 int ii;
64992 } ar;
64993 struct OP_AutoCommit_stack_vars {
64994 int desiredAutoCommit;
64995 int iRollback;
64996 int turnOnAC;
64997 } as;
64998 struct OP_Transaction_stack_vars {
64999 Btree *pBt;
65000 } at;
65001 struct OP_ReadCookie_stack_vars {
65002 int iMeta;
65003 int iDb;
65004 int iCookie;
65005 } au;
65006 struct OP_SetCookie_stack_vars {
65007 Db *pDb;
65008 } av;
65009 struct OP_VerifyCookie_stack_vars {
65010 int iMeta;
65011 int iGen;
65012 Btree *pBt;
65013 } aw;
65014 struct OP_OpenWrite_stack_vars {
65015 int nField;
65016 KeyInfo *pKeyInfo;
65017 int p2;
65018 int iDb;
65019 int wrFlag;
65020 Btree *pX;
65021 VdbeCursor *pCur;
65022 Db *pDb;
65023 } ax;
65024 struct OP_OpenEphemeral_stack_vars {
65025 VdbeCursor *pCx;
65026 } ay;
65027 struct OP_SorterOpen_stack_vars {
65028 VdbeCursor *pCx;
65029 } az;
65030 struct OP_OpenPseudo_stack_vars {
65031 VdbeCursor *pCx;
65032 } ba;
65033 struct OP_SeekGt_stack_vars {
65034 int res;
65035 int oc;
65036 VdbeCursor *pC;
65037 UnpackedRecord r;
65038 int nField;
65039 i64 iKey; /* The rowid we are to seek to */
65040 } bb;
65041 struct OP_Seek_stack_vars {
65042 VdbeCursor *pC;
65043 } bc;
65044 struct OP_Found_stack_vars {
65045 int alreadyExists;
65046 VdbeCursor *pC;
65047 int res;
65048 char *pFree;
65049 UnpackedRecord *pIdxKey;
65050 UnpackedRecord r;
65051 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
65052 } bd;
65053 struct OP_IsUnique_stack_vars {
65054 u16 ii;
65055 VdbeCursor *pCx;
65056 BtCursor *pCrsr;
65057 u16 nField;
65058 Mem *aMx;
65059 UnpackedRecord r; /* B-Tree index search key */
65060 i64 R; /* Rowid stored in register P3 */
65061 } be;
65062 struct OP_NotExists_stack_vars {
65063 VdbeCursor *pC;
65064 BtCursor *pCrsr;
65065 int res;
65066 u64 iKey;
65067 } bf;
65068 struct OP_NewRowid_stack_vars {
65069 i64 v; /* The new rowid */
65070 VdbeCursor *pC; /* Cursor of table to get the new rowid */
65071 int res; /* Result of an sqlite3BtreeLast() */
65072 int cnt; /* Counter to limit the number of searches */
65073 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
65074 VdbeFrame *pFrame; /* Root frame of VDBE */
65075 } bg;
65076 struct OP_InsertInt_stack_vars {
65077 Mem *pData; /* MEM cell holding data for the record to be inserted */
65078 Mem *pKey; /* MEM cell holding key for the record */
65079 i64 iKey; /* The integer ROWID or key for the record to be inserted */
65080 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63779,161 +65081,161 @@
65081 int nZero; /* Number of zero-bytes to append */
65082 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
65083 const char *zDb; /* database name - used by the update hook */
65084 const char *zTbl; /* Table name - used by the opdate hook */
65085 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
65086 } bh;
65087 struct OP_Delete_stack_vars {
65088 i64 iKey;
65089 VdbeCursor *pC;
65090 } bi;
65091 struct OP_SorterCompare_stack_vars {
65092 VdbeCursor *pC;
65093 int res;
65094 } bj;
65095 struct OP_SorterData_stack_vars {
65096 VdbeCursor *pC;
65097 } bk;
65098 struct OP_RowData_stack_vars {
65099 VdbeCursor *pC;
65100 BtCursor *pCrsr;
65101 u32 n;
65102 i64 n64;
65103 } bl;
65104 struct OP_Rowid_stack_vars {
65105 VdbeCursor *pC;
65106 i64 v;
65107 sqlite3_vtab *pVtab;
65108 const sqlite3_module *pModule;
65109 } bm;
65110 struct OP_NullRow_stack_vars {
65111 VdbeCursor *pC;
65112 } bn;
65113 struct OP_Last_stack_vars {
65114 VdbeCursor *pC;
65115 BtCursor *pCrsr;
65116 int res;
65117 } bo;
65118 struct OP_Rewind_stack_vars {
65119 VdbeCursor *pC;
65120 BtCursor *pCrsr;
65121 int res;
65122 } bp;
65123 struct OP_Next_stack_vars {
65124 VdbeCursor *pC;
65125 int res;
65126 } bq;
65127 struct OP_IdxInsert_stack_vars {
65128 VdbeCursor *pC;
65129 BtCursor *pCrsr;
65130 int nKey;
65131 const char *zKey;
65132 } br;
65133 struct OP_IdxDelete_stack_vars {
65134 VdbeCursor *pC;
65135 BtCursor *pCrsr;
65136 int res;
65137 UnpackedRecord r;
65138 } bs;
65139 struct OP_IdxRowid_stack_vars {
65140 BtCursor *pCrsr;
65141 VdbeCursor *pC;
65142 i64 rowid;
65143 } bt;
65144 struct OP_IdxGE_stack_vars {
65145 VdbeCursor *pC;
65146 int res;
65147 UnpackedRecord r;
65148 } bu;
65149 struct OP_Destroy_stack_vars {
65150 int iMoved;
65151 int iCnt;
65152 Vdbe *pVdbe;
65153 int iDb;
65154 } bv;
65155 struct OP_Clear_stack_vars {
65156 int nChange;
65157 } bw;
65158 struct OP_CreateTable_stack_vars {
65159 int pgno;
65160 int flags;
65161 Db *pDb;
65162 } bx;
65163 struct OP_ParseSchema_stack_vars {
65164 int iDb;
65165 const char *zMaster;
65166 char *zSql;
65167 InitData initData;
65168 } by;
65169 struct OP_IntegrityCk_stack_vars {
65170 int nRoot; /* Number of tables to check. (Number of root pages.) */
65171 int *aRoot; /* Array of rootpage numbers for tables to be checked */
65172 int j; /* Loop counter */
65173 int nErr; /* Number of errors reported */
65174 char *z; /* Text of the error report */
65175 Mem *pnErr; /* Register keeping track of errors remaining */
65176 } bz;
65177 struct OP_RowSetRead_stack_vars {
65178 i64 val;
65179 } ca;
65180 struct OP_RowSetTest_stack_vars {
65181 int iSet;
65182 int exists;
65183 } cb;
65184 struct OP_Program_stack_vars {
65185 int nMem; /* Number of memory registers for sub-program */
65186 int nByte; /* Bytes of runtime space required for sub-program */
65187 Mem *pRt; /* Register to allocate runtime space */
65188 Mem *pMem; /* Used to iterate through memory cells */
65189 Mem *pEnd; /* Last memory cell in new array */
65190 VdbeFrame *pFrame; /* New vdbe frame to execute in */
65191 SubProgram *pProgram; /* Sub-program to execute */
65192 void *t; /* Token identifying trigger */
65193 } cc;
65194 struct OP_Param_stack_vars {
65195 VdbeFrame *pFrame;
65196 Mem *pIn;
65197 } cd;
65198 struct OP_MemMax_stack_vars {
65199 Mem *pIn1;
65200 VdbeFrame *pFrame;
65201 } ce;
65202 struct OP_AggStep_stack_vars {
65203 int n;
65204 int i;
65205 Mem *pMem;
65206 Mem *pRec;
65207 sqlite3_context ctx;
65208 sqlite3_value **apVal;
65209 } cf;
65210 struct OP_AggFinal_stack_vars {
65211 Mem *pMem;
65212 } cg;
65213 struct OP_Checkpoint_stack_vars {
65214 int i; /* Loop counter */
65215 int aRes[3]; /* Results */
65216 Mem *pMem; /* Write results here */
65217 } ch;
65218 struct OP_JournalMode_stack_vars {
65219 Btree *pBt; /* Btree to change journal mode of */
65220 Pager *pPager; /* Pager associated with pBt */
65221 int eNew; /* New journal mode */
65222 int eOld; /* The old journal mode */
65223 const char *zFilename; /* Name of database file for pPager */
65224 } ci;
65225 struct OP_IncrVacuum_stack_vars {
65226 Btree *pBt;
65227 } cj;
65228 struct OP_VBegin_stack_vars {
65229 VTable *pVTab;
65230 } ck;
65231 struct OP_VOpen_stack_vars {
65232 VdbeCursor *pCur;
65233 sqlite3_vtab_cursor *pVtabCursor;
65234 sqlite3_vtab *pVtab;
65235 sqlite3_module *pModule;
65236 } cl;
65237 struct OP_VFilter_stack_vars {
65238 int nArg;
65239 int iQuery;
65240 const sqlite3_module *pModule;
65241 Mem *pQuery;
@@ -63942,40 +65244,40 @@
65244 sqlite3_vtab *pVtab;
65245 VdbeCursor *pCur;
65246 int res;
65247 int i;
65248 Mem **apArg;
65249 } cm;
65250 struct OP_VColumn_stack_vars {
65251 sqlite3_vtab *pVtab;
65252 const sqlite3_module *pModule;
65253 Mem *pDest;
65254 sqlite3_context sContext;
65255 } cn;
65256 struct OP_VNext_stack_vars {
65257 sqlite3_vtab *pVtab;
65258 const sqlite3_module *pModule;
65259 int res;
65260 VdbeCursor *pCur;
65261 } co;
65262 struct OP_VRename_stack_vars {
65263 sqlite3_vtab *pVtab;
65264 Mem *pName;
65265 } cp;
65266 struct OP_VUpdate_stack_vars {
65267 sqlite3_vtab *pVtab;
65268 sqlite3_module *pModule;
65269 int nArg;
65270 int i;
65271 sqlite_int64 rowid;
65272 Mem **apArg;
65273 Mem *pX;
65274 } cq;
65275 struct OP_Trace_stack_vars {
65276 char *zTrace;
65277 char *z;
65278 } cr;
65279 } u;
65280 /* End automatically generated code
65281 ********************************************************************/
65282
65283 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -64071,11 +65373,11 @@
65373 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
65374 assert( pOp->p2>0 );
65375 assert( pOp->p2<=p->nMem );
65376 pOut = &aMem[pOp->p2];
65377 memAboutToChange(p, pOut);
65378 VdbeMemRelease(pOut);
65379 pOut->flags = MEM_Int;
65380 }
65381
65382 /* Sanity checking on other operands */
65383 #ifdef SQLITE_DEBUG
@@ -64162,11 +65464,12 @@
65464 /* Opcode: Gosub P1 P2 * * *
65465 **
65466 ** Write the current address onto register P1
65467 ** and then jump to address P2.
65468 */
65469 case OP_Gosub: { /* jump */
65470 assert( pOp->p1>0 && pOp->p1<=p->nMem );
65471 pIn1 = &aMem[pOp->p1];
65472 assert( (pIn1->flags & MEM_Dyn)==0 );
65473 memAboutToChange(p, pIn1);
65474 pIn1->flags = MEM_Int;
65475 pIn1->u.i = pc;
@@ -64361,16 +65664,31 @@
65664 pOut->enc = encoding;
65665 UPDATE_MAX_BLOBSIZE(pOut);
65666 break;
65667 }
65668
65669 /* Opcode: Null * P2 P3 * *
65670 **
65671 ** Write a NULL into registers P2. If P3 greater than P2, then also write
65672 ** NULL into register P3 and ever register in between P2 and P3. If P3
65673 ** is less than P2 (typically P3 is zero) then only register P2 is
65674 ** set to NULL
65675 */
65676 case OP_Null: { /* out2-prerelease */
65677 #if 0 /* local variables moved into u.ab */
65678 int cnt;
65679 #endif /* local variables moved into u.ab */
65680 u.ab.cnt = pOp->p3-pOp->p2;
65681 assert( pOp->p3<=p->nMem );
65682 pOut->flags = MEM_Null;
65683 while( u.ab.cnt>0 ){
65684 pOut++;
65685 memAboutToChange(p, pOut);
65686 VdbeMemRelease(pOut);
65687 pOut->flags = MEM_Null;
65688 u.ab.cnt--;
65689 }
65690 break;
65691 }
65692
65693
65694 /* Opcode: Blob P1 P2 * P4
@@ -64392,21 +65710,21 @@
65710 **
65711 ** If the parameter is named, then its name appears in P4 and P3==1.
65712 ** The P4 value is used by sqlite3_bind_parameter_name().
65713 */
65714 case OP_Variable: { /* out2-prerelease */
65715 #if 0 /* local variables moved into u.ac */
65716 Mem *pVar; /* Value being transferred */
65717 #endif /* local variables moved into u.ac */
65718
65719 assert( pOp->p1>0 && pOp->p1<=p->nVar );
65720 assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
65721 u.ac.pVar = &p->aVar[pOp->p1 - 1];
65722 if( sqlite3VdbeMemTooBig(u.ac.pVar) ){
65723 goto too_big;
65724 }
65725 sqlite3VdbeMemShallowCopy(pOut, u.ac.pVar, MEM_Static);
65726 UPDATE_MAX_BLOBSIZE(pOut);
65727 break;
65728 }
65729
65730 /* Opcode: Move P1 P2 P3 * *
@@ -64415,40 +65733,40 @@
65733 ** registers P2..P2+P3-1. Registers P1..P1+P1-1 are
65734 ** left holding a NULL. It is an error for register ranges
65735 ** P1..P1+P3-1 and P2..P2+P3-1 to overlap.
65736 */
65737 case OP_Move: {
65738 #if 0 /* local variables moved into u.ad */
65739 char *zMalloc; /* Holding variable for allocated memory */
65740 int n; /* Number of registers left to copy */
65741 int p1; /* Register to copy from */
65742 int p2; /* Register to copy to */
65743 #endif /* local variables moved into u.ad */
65744
65745 u.ad.n = pOp->p3;
65746 u.ad.p1 = pOp->p1;
65747 u.ad.p2 = pOp->p2;
65748 assert( u.ad.n>0 && u.ad.p1>0 && u.ad.p2>0 );
65749 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
65750
65751 pIn1 = &aMem[u.ad.p1];
65752 pOut = &aMem[u.ad.p2];
65753 while( u.ad.n-- ){
65754 assert( pOut<=&aMem[p->nMem] );
65755 assert( pIn1<=&aMem[p->nMem] );
65756 assert( memIsValid(pIn1) );
65757 memAboutToChange(p, pOut);
65758 u.ad.zMalloc = pOut->zMalloc;
65759 pOut->zMalloc = 0;
65760 sqlite3VdbeMemMove(pOut, pIn1);
65761 #ifdef SQLITE_DEBUG
65762 if( pOut->pScopyFrom>=&aMem[u.ad.p1] && pOut->pScopyFrom<&aMem[u.ad.p1+pOp->p3] ){
65763 pOut->pScopyFrom += u.ad.p1 - pOp->p2;
65764 }
65765 #endif
65766 pIn1->zMalloc = u.ad.zMalloc;
65767 REGISTER_TRACE(u.ad.p2++, pOut);
65768 pIn1++;
65769 pOut++;
65770 }
65771 break;
65772 }
@@ -64501,14 +65819,14 @@
65819 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
65820 ** structure to provide access to the top P1 values as the result
65821 ** row.
65822 */
65823 case OP_ResultRow: {
65824 #if 0 /* local variables moved into u.ae */
65825 Mem *pMem;
65826 int i;
65827 #endif /* local variables moved into u.ae */
65828 assert( p->nResColumn==pOp->p2 );
65829 assert( pOp->p1>0 );
65830 assert( pOp->p1+pOp->p2<=p->nMem+1 );
65831
65832 /* If this statement has violated immediate foreign key constraints, do
@@ -64544,21 +65862,21 @@
65862 /* Invalidate all ephemeral cursor row caches */
65863 p->cacheCtr = (p->cacheCtr + 2)|1;
65864
65865 /* Make sure the results of the current row are \000 terminated
65866 ** and have an assigned type. The results are de-ephemeralized as
65867 ** a side effect.
65868 */
65869 u.ae.pMem = p->pResultSet = &aMem[pOp->p1];
65870 for(u.ae.i=0; u.ae.i<pOp->p2; u.ae.i++){
65871 assert( memIsValid(&u.ae.pMem[u.ae.i]) );
65872 Deephemeralize(&u.ae.pMem[u.ae.i]);
65873 assert( (u.ae.pMem[u.ae.i].flags & MEM_Ephem)==0
65874 || (u.ae.pMem[u.ae.i].flags & (MEM_Str|MEM_Blob))==0 );
65875 sqlite3VdbeMemNulTerminate(&u.ae.pMem[u.ae.i]);
65876 sqlite3VdbeMemStoreType(&u.ae.pMem[u.ae.i]);
65877 REGISTER_TRACE(pOp->p1+u.ae.i, &u.ae.pMem[u.ae.i]);
65878 }
65879 if( db->mallocFailed ) goto no_mem;
65880
65881 /* Return SQLITE_ROW
65882 */
@@ -64578,13 +65896,13 @@
65896 ** It is illegal for P1 and P3 to be the same register. Sometimes,
65897 ** if P3 is the same register as P2, the implementation is able
65898 ** to avoid a memcpy().
65899 */
65900 case OP_Concat: { /* same as TK_CONCAT, in1, in2, out3 */
65901 #if 0 /* local variables moved into u.af */
65902 i64 nByte;
65903 #endif /* local variables moved into u.af */
65904
65905 pIn1 = &aMem[pOp->p1];
65906 pIn2 = &aMem[pOp->p2];
65907 pOut = &aMem[pOp->p3];
65908 assert( pIn1!=pOut );
@@ -64593,26 +65911,26 @@
65911 break;
65912 }
65913 if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
65914 Stringify(pIn1, encoding);
65915 Stringify(pIn2, encoding);
65916 u.af.nByte = pIn1->n + pIn2->n;
65917 if( u.af.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
65918 goto too_big;
65919 }
65920 MemSetTypeFlag(pOut, MEM_Str);
65921 if( sqlite3VdbeMemGrow(pOut, (int)u.af.nByte+2, pOut==pIn2) ){
65922 goto no_mem;
65923 }
65924 if( pOut!=pIn2 ){
65925 memcpy(pOut->z, pIn2->z, pIn2->n);
65926 }
65927 memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
65928 pOut->z[u.af.nByte] = 0;
65929 pOut->z[u.af.nByte+1] = 0;
65930 pOut->flags |= MEM_Term;
65931 pOut->n = (int)u.af.nByte;
65932 pOut->enc = encoding;
65933 UPDATE_MAX_BLOBSIZE(pOut);
65934 break;
65935 }
65936
@@ -64652,80 +65970,80 @@
65970 case OP_Add: /* same as TK_PLUS, in1, in2, out3 */
65971 case OP_Subtract: /* same as TK_MINUS, in1, in2, out3 */
65972 case OP_Multiply: /* same as TK_STAR, in1, in2, out3 */
65973 case OP_Divide: /* same as TK_SLASH, in1, in2, out3 */
65974 case OP_Remainder: { /* same as TK_REM, in1, in2, out3 */
65975 #if 0 /* local variables moved into u.ag */
65976 int flags; /* Combined MEM_* flags from both inputs */
65977 i64 iA; /* Integer value of left operand */
65978 i64 iB; /* Integer value of right operand */
65979 double rA; /* Real value of left operand */
65980 double rB; /* Real value of right operand */
65981 #endif /* local variables moved into u.ag */
65982
65983 pIn1 = &aMem[pOp->p1];
65984 applyNumericAffinity(pIn1);
65985 pIn2 = &aMem[pOp->p2];
65986 applyNumericAffinity(pIn2);
65987 pOut = &aMem[pOp->p3];
65988 u.ag.flags = pIn1->flags | pIn2->flags;
65989 if( (u.ag.flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
65990 if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
65991 u.ag.iA = pIn1->u.i;
65992 u.ag.iB = pIn2->u.i;
65993 switch( pOp->opcode ){
65994 case OP_Add: if( sqlite3AddInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
65995 case OP_Subtract: if( sqlite3SubInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
65996 case OP_Multiply: if( sqlite3MulInt64(&u.ag.iB,u.ag.iA) ) goto fp_math; break;
65997 case OP_Divide: {
65998 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
65999 if( u.ag.iA==-1 && u.ag.iB==SMALLEST_INT64 ) goto fp_math;
66000 u.ag.iB /= u.ag.iA;
66001 break;
66002 }
66003 default: {
66004 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
66005 if( u.ag.iA==-1 ) u.ag.iA = 1;
66006 u.ag.iB %= u.ag.iA;
66007 break;
66008 }
66009 }
66010 pOut->u.i = u.ag.iB;
66011 MemSetTypeFlag(pOut, MEM_Int);
66012 }else{
66013 fp_math:
66014 u.ag.rA = sqlite3VdbeRealValue(pIn1);
66015 u.ag.rB = sqlite3VdbeRealValue(pIn2);
66016 switch( pOp->opcode ){
66017 case OP_Add: u.ag.rB += u.ag.rA; break;
66018 case OP_Subtract: u.ag.rB -= u.ag.rA; break;
66019 case OP_Multiply: u.ag.rB *= u.ag.rA; break;
66020 case OP_Divide: {
66021 /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
66022 if( u.ag.rA==(double)0 ) goto arithmetic_result_is_null;
66023 u.ag.rB /= u.ag.rA;
66024 break;
66025 }
66026 default: {
66027 u.ag.iA = (i64)u.ag.rA;
66028 u.ag.iB = (i64)u.ag.rB;
66029 if( u.ag.iA==0 ) goto arithmetic_result_is_null;
66030 if( u.ag.iA==-1 ) u.ag.iA = 1;
66031 u.ag.rB = (double)(u.ag.iB % u.ag.iA);
66032 break;
66033 }
66034 }
66035 #ifdef SQLITE_OMIT_FLOATING_POINT
66036 pOut->u.i = u.ag.rB;
66037 MemSetTypeFlag(pOut, MEM_Int);
66038 #else
66039 if( sqlite3IsNaN(u.ag.rB) ){
66040 goto arithmetic_result_is_null;
66041 }
66042 pOut->r = u.ag.rB;
66043 MemSetTypeFlag(pOut, MEM_Real);
66044 if( (u.ag.flags & MEM_Real)==0 ){
66045 sqlite3VdbeIntegerAffinity(pOut);
66046 }
66047 #endif
66048 }
66049 break;
@@ -64766,96 +66084,96 @@
66084 ** invocation of this opcode.
66085 **
66086 ** See also: AggStep and AggFinal
66087 */
66088 case OP_Function: {
66089 #if 0 /* local variables moved into u.ah */
66090 int i;
66091 Mem *pArg;
66092 sqlite3_context ctx;
66093 sqlite3_value **apVal;
66094 int n;
66095 #endif /* local variables moved into u.ah */
66096
66097 u.ah.n = pOp->p5;
66098 u.ah.apVal = p->apArg;
66099 assert( u.ah.apVal || u.ah.n==0 );
66100 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66101 pOut = &aMem[pOp->p3];
66102 memAboutToChange(p, pOut);
66103
66104 assert( u.ah.n==0 || (pOp->p2>0 && pOp->p2+u.ah.n<=p->nMem+1) );
66105 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ah.n );
66106 u.ah.pArg = &aMem[pOp->p2];
66107 for(u.ah.i=0; u.ah.i<u.ah.n; u.ah.i++, u.ah.pArg++){
66108 assert( memIsValid(u.ah.pArg) );
66109 u.ah.apVal[u.ah.i] = u.ah.pArg;
66110 Deephemeralize(u.ah.pArg);
66111 sqlite3VdbeMemStoreType(u.ah.pArg);
66112 REGISTER_TRACE(pOp->p2+u.ah.i, u.ah.pArg);
66113 }
66114
66115 assert( pOp->p4type==P4_FUNCDEF || pOp->p4type==P4_VDBEFUNC );
66116 if( pOp->p4type==P4_FUNCDEF ){
66117 u.ah.ctx.pFunc = pOp->p4.pFunc;
66118 u.ah.ctx.pVdbeFunc = 0;
66119 }else{
66120 u.ah.ctx.pVdbeFunc = (VdbeFunc*)pOp->p4.pVdbeFunc;
66121 u.ah.ctx.pFunc = u.ah.ctx.pVdbeFunc->pFunc;
66122 }
66123
66124 u.ah.ctx.s.flags = MEM_Null;
66125 u.ah.ctx.s.db = db;
66126 u.ah.ctx.s.xDel = 0;
66127 u.ah.ctx.s.zMalloc = 0;
66128
66129 /* The output cell may already have a buffer allocated. Move
66130 ** the pointer to u.ah.ctx.s so in case the user-function can use
66131 ** the already allocated buffer instead of allocating a new one.
66132 */
66133 sqlite3VdbeMemMove(&u.ah.ctx.s, pOut);
66134 MemSetTypeFlag(&u.ah.ctx.s, MEM_Null);
66135
66136 u.ah.ctx.isError = 0;
66137 if( u.ah.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
66138 assert( pOp>aOp );
66139 assert( pOp[-1].p4type==P4_COLLSEQ );
66140 assert( pOp[-1].opcode==OP_CollSeq );
66141 u.ah.ctx.pColl = pOp[-1].p4.pColl;
66142 }
66143 db->lastRowid = lastRowid;
66144 (*u.ah.ctx.pFunc->xFunc)(&u.ah.ctx, u.ah.n, u.ah.apVal); /* IMP: R-24505-23230 */
66145 lastRowid = db->lastRowid;
66146
66147 /* If any auxiliary data functions have been called by this user function,
66148 ** immediately call the destructor for any non-static values.
66149 */
66150 if( u.ah.ctx.pVdbeFunc ){
66151 sqlite3VdbeDeleteAuxData(u.ah.ctx.pVdbeFunc, pOp->p1);
66152 pOp->p4.pVdbeFunc = u.ah.ctx.pVdbeFunc;
66153 pOp->p4type = P4_VDBEFUNC;
66154 }
66155
66156 if( db->mallocFailed ){
66157 /* Even though a malloc() has failed, the implementation of the
66158 ** user function may have called an sqlite3_result_XXX() function
66159 ** to return a value. The following call releases any resources
66160 ** associated with such a value.
66161 */
66162 sqlite3VdbeMemRelease(&u.ah.ctx.s);
66163 goto no_mem;
66164 }
66165
66166 /* If the function returned an error, throw an exception */
66167 if( u.ah.ctx.isError ){
66168 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ah.ctx.s));
66169 rc = u.ah.ctx.isError;
66170 }
66171
66172 /* Copy the result of the function into register P3 */
66173 sqlite3VdbeChangeEncoding(&u.ah.ctx.s, encoding);
66174 sqlite3VdbeMemMove(pOut, &u.ah.ctx.s);
66175 if( sqlite3VdbeMemTooBig(pOut) ){
66176 goto too_big;
66177 }
66178
66179 #if 0
@@ -64899,56 +66217,56 @@
66217 */
66218 case OP_BitAnd: /* same as TK_BITAND, in1, in2, out3 */
66219 case OP_BitOr: /* same as TK_BITOR, in1, in2, out3 */
66220 case OP_ShiftLeft: /* same as TK_LSHIFT, in1, in2, out3 */
66221 case OP_ShiftRight: { /* same as TK_RSHIFT, in1, in2, out3 */
66222 #if 0 /* local variables moved into u.ai */
66223 i64 iA;
66224 u64 uA;
66225 i64 iB;
66226 u8 op;
66227 #endif /* local variables moved into u.ai */
66228
66229 pIn1 = &aMem[pOp->p1];
66230 pIn2 = &aMem[pOp->p2];
66231 pOut = &aMem[pOp->p3];
66232 if( (pIn1->flags | pIn2->flags) & MEM_Null ){
66233 sqlite3VdbeMemSetNull(pOut);
66234 break;
66235 }
66236 u.ai.iA = sqlite3VdbeIntValue(pIn2);
66237 u.ai.iB = sqlite3VdbeIntValue(pIn1);
66238 u.ai.op = pOp->opcode;
66239 if( u.ai.op==OP_BitAnd ){
66240 u.ai.iA &= u.ai.iB;
66241 }else if( u.ai.op==OP_BitOr ){
66242 u.ai.iA |= u.ai.iB;
66243 }else if( u.ai.iB!=0 ){
66244 assert( u.ai.op==OP_ShiftRight || u.ai.op==OP_ShiftLeft );
66245
66246 /* If shifting by a negative amount, shift in the other direction */
66247 if( u.ai.iB<0 ){
66248 assert( OP_ShiftRight==OP_ShiftLeft+1 );
66249 u.ai.op = 2*OP_ShiftLeft + 1 - u.ai.op;
66250 u.ai.iB = u.ai.iB>(-64) ? -u.ai.iB : 64;
66251 }
66252
66253 if( u.ai.iB>=64 ){
66254 u.ai.iA = (u.ai.iA>=0 || u.ai.op==OP_ShiftLeft) ? 0 : -1;
66255 }else{
66256 memcpy(&u.ai.uA, &u.ai.iA, sizeof(u.ai.uA));
66257 if( u.ai.op==OP_ShiftLeft ){
66258 u.ai.uA <<= u.ai.iB;
66259 }else{
66260 u.ai.uA >>= u.ai.iB;
66261 /* Sign-extend on a right shift of a negative number */
66262 if( u.ai.iA<0 ) u.ai.uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-u.ai.iB);
66263 }
66264 memcpy(&u.ai.iA, &u.ai.uA, sizeof(u.ai.iA));
66265 }
66266 }
66267 pOut->u.i = u.ai.iA;
66268 MemSetTypeFlag(pOut, MEM_Int);
66269 break;
66270 }
66271
66272 /* Opcode: AddImm P1 P2 * * *
@@ -65185,30 +66503,30 @@
66503 case OP_Ne: /* same as TK_NE, jump, in1, in3 */
66504 case OP_Lt: /* same as TK_LT, jump, in1, in3 */
66505 case OP_Le: /* same as TK_LE, jump, in1, in3 */
66506 case OP_Gt: /* same as TK_GT, jump, in1, in3 */
66507 case OP_Ge: { /* same as TK_GE, jump, in1, in3 */
66508 #if 0 /* local variables moved into u.aj */
66509 int res; /* Result of the comparison of pIn1 against pIn3 */
66510 char affinity; /* Affinity to use for comparison */
66511 u16 flags1; /* Copy of initial value of pIn1->flags */
66512 u16 flags3; /* Copy of initial value of pIn3->flags */
66513 #endif /* local variables moved into u.aj */
66514
66515 pIn1 = &aMem[pOp->p1];
66516 pIn3 = &aMem[pOp->p3];
66517 u.aj.flags1 = pIn1->flags;
66518 u.aj.flags3 = pIn3->flags;
66519 if( (u.aj.flags1 | u.aj.flags3)&MEM_Null ){
66520 /* One or both operands are NULL */
66521 if( pOp->p5 & SQLITE_NULLEQ ){
66522 /* If SQLITE_NULLEQ is set (which will only happen if the operator is
66523 ** OP_Eq or OP_Ne) then take the jump or not depending on whether
66524 ** or not both operands are null.
66525 */
66526 assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
66527 u.aj.res = (u.aj.flags1 & u.aj.flags3 & MEM_Null)==0;
66528 }else{
66529 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
66530 ** then the result is always NULL.
66531 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
66532 */
@@ -65221,44 +66539,44 @@
66539 }
66540 break;
66541 }
66542 }else{
66543 /* Neither operand is NULL. Do a comparison. */
66544 u.aj.affinity = pOp->p5 & SQLITE_AFF_MASK;
66545 if( u.aj.affinity ){
66546 applyAffinity(pIn1, u.aj.affinity, encoding);
66547 applyAffinity(pIn3, u.aj.affinity, encoding);
66548 if( db->mallocFailed ) goto no_mem;
66549 }
66550
66551 assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
66552 ExpandBlob(pIn1);
66553 ExpandBlob(pIn3);
66554 u.aj.res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
66555 }
66556 switch( pOp->opcode ){
66557 case OP_Eq: u.aj.res = u.aj.res==0; break;
66558 case OP_Ne: u.aj.res = u.aj.res!=0; break;
66559 case OP_Lt: u.aj.res = u.aj.res<0; break;
66560 case OP_Le: u.aj.res = u.aj.res<=0; break;
66561 case OP_Gt: u.aj.res = u.aj.res>0; break;
66562 default: u.aj.res = u.aj.res>=0; break;
66563 }
66564
66565 if( pOp->p5 & SQLITE_STOREP2 ){
66566 pOut = &aMem[pOp->p2];
66567 memAboutToChange(p, pOut);
66568 MemSetTypeFlag(pOut, MEM_Int);
66569 pOut->u.i = u.aj.res;
66570 REGISTER_TRACE(pOp->p2, pOut);
66571 }else if( u.aj.res ){
66572 pc = pOp->p2-1;
66573 }
66574
66575 /* Undo any changes made by applyAffinity() to the input registers. */
66576 pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (u.aj.flags1&MEM_TypeMask);
66577 pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (u.aj.flags3&MEM_TypeMask);
66578 break;
66579 }
66580
66581 /* Opcode: Permutation * * * P4 *
66582 **
@@ -65289,50 +66607,50 @@
66607 ** The comparison is a sort comparison, so NULLs compare equal,
66608 ** NULLs are less than numbers, numbers are less than strings,
66609 ** and strings are less than blobs.
66610 */
66611 case OP_Compare: {
66612 #if 0 /* local variables moved into u.ak */
66613 int n;
66614 int i;
66615 int p1;
66616 int p2;
66617 const KeyInfo *pKeyInfo;
66618 int idx;
66619 CollSeq *pColl; /* Collating sequence to use on this term */
66620 int bRev; /* True for DESCENDING sort order */
66621 #endif /* local variables moved into u.ak */
66622
66623 u.ak.n = pOp->p3;
66624 u.ak.pKeyInfo = pOp->p4.pKeyInfo;
66625 assert( u.ak.n>0 );
66626 assert( u.ak.pKeyInfo!=0 );
66627 u.ak.p1 = pOp->p1;
66628 u.ak.p2 = pOp->p2;
66629 #if SQLITE_DEBUG
66630 if( aPermute ){
66631 int k, mx = 0;
66632 for(k=0; k<u.ak.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
66633 assert( u.ak.p1>0 && u.ak.p1+mx<=p->nMem+1 );
66634 assert( u.ak.p2>0 && u.ak.p2+mx<=p->nMem+1 );
66635 }else{
66636 assert( u.ak.p1>0 && u.ak.p1+u.ak.n<=p->nMem+1 );
66637 assert( u.ak.p2>0 && u.ak.p2+u.ak.n<=p->nMem+1 );
66638 }
66639 #endif /* SQLITE_DEBUG */
66640 for(u.ak.i=0; u.ak.i<u.ak.n; u.ak.i++){
66641 u.ak.idx = aPermute ? aPermute[u.ak.i] : u.ak.i;
66642 assert( memIsValid(&aMem[u.ak.p1+u.ak.idx]) );
66643 assert( memIsValid(&aMem[u.ak.p2+u.ak.idx]) );
66644 REGISTER_TRACE(u.ak.p1+u.ak.idx, &aMem[u.ak.p1+u.ak.idx]);
66645 REGISTER_TRACE(u.ak.p2+u.ak.idx, &aMem[u.ak.p2+u.ak.idx]);
66646 assert( u.ak.i<u.ak.pKeyInfo->nField );
66647 u.ak.pColl = u.ak.pKeyInfo->aColl[u.ak.i];
66648 u.ak.bRev = u.ak.pKeyInfo->aSortOrder[u.ak.i];
66649 iCompare = sqlite3MemCompare(&aMem[u.ak.p1+u.ak.idx], &aMem[u.ak.p2+u.ak.idx], u.ak.pColl);
66650 if( iCompare ){
66651 if( u.ak.bRev ) iCompare = -iCompare;
66652 break;
66653 }
66654 }
66655 aPermute = 0;
66656 break;
@@ -65373,39 +66691,39 @@
66691 ** even if the other input is NULL. A NULL and false or two NULLs
66692 ** give a NULL output.
66693 */
66694 case OP_And: /* same as TK_AND, in1, in2, out3 */
66695 case OP_Or: { /* same as TK_OR, in1, in2, out3 */
66696 #if 0 /* local variables moved into u.al */
66697 int v1; /* Left operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
66698 int v2; /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
66699 #endif /* local variables moved into u.al */
66700
66701 pIn1 = &aMem[pOp->p1];
66702 if( pIn1->flags & MEM_Null ){
66703 u.al.v1 = 2;
66704 }else{
66705 u.al.v1 = sqlite3VdbeIntValue(pIn1)!=0;
66706 }
66707 pIn2 = &aMem[pOp->p2];
66708 if( pIn2->flags & MEM_Null ){
66709 u.al.v2 = 2;
66710 }else{
66711 u.al.v2 = sqlite3VdbeIntValue(pIn2)!=0;
66712 }
66713 if( pOp->opcode==OP_And ){
66714 static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
66715 u.al.v1 = and_logic[u.al.v1*3+u.al.v2];
66716 }else{
66717 static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
66718 u.al.v1 = or_logic[u.al.v1*3+u.al.v2];
66719 }
66720 pOut = &aMem[pOp->p3];
66721 if( u.al.v1==2 ){
66722 MemSetTypeFlag(pOut, MEM_Null);
66723 }else{
66724 pOut->u.i = u.al.v1;
66725 MemSetTypeFlag(pOut, MEM_Int);
66726 }
66727 break;
66728 }
66729
@@ -65443,55 +66761,55 @@
66761 break;
66762 }
66763
66764 /* Opcode: Once P1 P2 * * *
66765 **
66766 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
66767 ** set the flag and fall through to the next instruction.
 
66768 **
66769 ** See also: JumpOnce
 
 
66770 */
66771 case OP_Once: { /* jump */
66772 assert( pOp->p1<p->nOnceFlag );
66773 if( p->aOnceFlag[pOp->p1] ){
66774 pc = pOp->p2-1;
66775 }else{
66776 p->aOnceFlag[pOp->p1] = 1;
66777 }
66778 break;
66779 }
66780
66781 /* Opcode: If P1 P2 P3 * *
66782 **
66783 ** Jump to P2 if the value in register P1 is true. The value
66784 ** is considered true if it is numeric and non-zero. If the value
66785 ** in P1 is NULL then take the jump if P3 is non-zero.
66786 */
66787 /* Opcode: IfNot P1 P2 P3 * *
66788 **
66789 ** Jump to P2 if the value in register P1 is False. The value
66790 ** is considered false if it has a numeric value of zero. If the value
66791 ** in P1 is NULL then take the jump if P3 is zero.
66792 */
 
66793 case OP_If: /* jump, in1 */
66794 case OP_IfNot: { /* jump, in1 */
66795 #if 0 /* local variables moved into u.am */
66796 int c;
66797 #endif /* local variables moved into u.am */
66798 pIn1 = &aMem[pOp->p1];
66799 if( pIn1->flags & MEM_Null ){
66800 u.am.c = pOp->p3;
66801 }else{
66802 #ifdef SQLITE_OMIT_FLOATING_POINT
66803 u.am.c = sqlite3VdbeIntValue(pIn1)!=0;
66804 #else
66805 u.am.c = sqlite3VdbeRealValue(pIn1)!=0.0;
66806 #endif
66807 if( pOp->opcode==OP_IfNot ) u.am.c = !u.am.c;
66808 }
66809 if( u.am.c ){
66810 pc = pOp->p2-1;
 
 
 
 
 
 
66811 }
66812 break;
66813 }
66814
66815 /* Opcode: IsNull P1 P2 * * *
@@ -65536,11 +66854,11 @@
66854 ** then the cache of the cursor is reset prior to extracting the column.
66855 ** The first OP_Column against a pseudo-table after the value of the content
66856 ** register has changed should have this bit set.
66857 */
66858 case OP_Column: {
66859 #if 0 /* local variables moved into u.an */
66860 u32 payloadSize; /* Number of bytes in the record */
66861 i64 payloadSize64; /* Number of bytes in the record */
66862 int p1; /* P1 value of the opcode */
66863 int p2; /* column number to retrieve */
66864 VdbeCursor *pC; /* The VDBE cursor */
@@ -65560,130 +66878,130 @@
66878 u32 szField; /* Number of bytes in the content of a field */
66879 int szHdr; /* Size of the header size field at start of record */
66880 int avail; /* Number of bytes of available data */
66881 u32 t; /* A type code from the record header */
66882 Mem *pReg; /* PseudoTable input register */
66883 #endif /* local variables moved into u.an */
66884
66885
66886 u.an.p1 = pOp->p1;
66887 u.an.p2 = pOp->p2;
66888 u.an.pC = 0;
66889 memset(&u.an.sMem, 0, sizeof(u.an.sMem));
66890 assert( u.an.p1<p->nCursor );
66891 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66892 u.an.pDest = &aMem[pOp->p3];
66893 memAboutToChange(p, u.an.pDest);
66894 u.an.zRec = 0;
66895
66896 /* This block sets the variable u.an.payloadSize to be the total number of
66897 ** bytes in the record.
66898 **
66899 ** u.an.zRec is set to be the complete text of the record if it is available.
66900 ** The complete record text is always available for pseudo-tables
66901 ** If the record is stored in a cursor, the complete record text
66902 ** might be available in the u.an.pC->aRow cache. Or it might not be.
66903 ** If the data is unavailable, u.an.zRec is set to NULL.
66904 **
66905 ** We also compute the number of columns in the record. For cursors,
66906 ** the number of columns is stored in the VdbeCursor.nField element.
66907 */
66908 u.an.pC = p->apCsr[u.an.p1];
66909 assert( u.an.pC!=0 );
66910 #ifndef SQLITE_OMIT_VIRTUALTABLE
66911 assert( u.an.pC->pVtabCursor==0 );
66912 #endif
66913 u.an.pCrsr = u.an.pC->pCursor;
66914 if( u.an.pCrsr!=0 ){
66915 /* The record is stored in a B-Tree */
66916 rc = sqlite3VdbeCursorMoveto(u.an.pC);
66917 if( rc ) goto abort_due_to_error;
66918 if( u.an.pC->nullRow ){
66919 u.an.payloadSize = 0;
66920 }else if( u.an.pC->cacheStatus==p->cacheCtr ){
66921 u.an.payloadSize = u.an.pC->payloadSize;
66922 u.an.zRec = (char*)u.an.pC->aRow;
66923 }else if( u.an.pC->isIndex ){
66924 assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
66925 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.an.pCrsr, &u.an.payloadSize64);
66926 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
66927 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
66928 ** payload size, so it is impossible for u.an.payloadSize64 to be
66929 ** larger than 32 bits. */
66930 assert( (u.an.payloadSize64 & SQLITE_MAX_U32)==(u64)u.an.payloadSize64 );
66931 u.an.payloadSize = (u32)u.an.payloadSize64;
66932 }else{
66933 assert( sqlite3BtreeCursorIsValid(u.an.pCrsr) );
66934 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.an.pCrsr, &u.an.payloadSize);
66935 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
66936 }
66937 }else if( ALWAYS(u.an.pC->pseudoTableReg>0) ){
66938 u.an.pReg = &aMem[u.an.pC->pseudoTableReg];
66939 assert( u.an.pReg->flags & MEM_Blob );
66940 assert( memIsValid(u.an.pReg) );
66941 u.an.payloadSize = u.an.pReg->n;
66942 u.an.zRec = u.an.pReg->z;
66943 u.an.pC->cacheStatus = (pOp->p5&OPFLAG_CLEARCACHE) ? CACHE_STALE : p->cacheCtr;
66944 assert( u.an.payloadSize==0 || u.an.zRec!=0 );
66945 }else{
66946 /* Consider the row to be NULL */
66947 u.an.payloadSize = 0;
66948 }
66949
66950 /* If u.an.payloadSize is 0, then just store a NULL. This can happen because of
66951 ** nullRow or because of a corrupt database. */
66952 if( u.an.payloadSize==0 ){
66953 MemSetTypeFlag(u.an.pDest, MEM_Null);
66954 goto op_column_out;
66955 }
66956 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
66957 if( u.an.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
66958 goto too_big;
66959 }
66960
66961 u.an.nField = u.an.pC->nField;
66962 assert( u.an.p2<u.an.nField );
66963
66964 /* Read and parse the table header. Store the results of the parse
66965 ** into the record header cache fields of the cursor.
66966 */
66967 u.an.aType = u.an.pC->aType;
66968 if( u.an.pC->cacheStatus==p->cacheCtr ){
66969 u.an.aOffset = u.an.pC->aOffset;
66970 }else{
66971 assert(u.an.aType);
66972 u.an.avail = 0;
66973 u.an.pC->aOffset = u.an.aOffset = &u.an.aType[u.an.nField];
66974 u.an.pC->payloadSize = u.an.payloadSize;
66975 u.an.pC->cacheStatus = p->cacheCtr;
66976
66977 /* Figure out how many bytes are in the header */
66978 if( u.an.zRec ){
66979 u.an.zData = u.an.zRec;
66980 }else{
66981 if( u.an.pC->isIndex ){
66982 u.an.zData = (char*)sqlite3BtreeKeyFetch(u.an.pCrsr, &u.an.avail);
66983 }else{
66984 u.an.zData = (char*)sqlite3BtreeDataFetch(u.an.pCrsr, &u.an.avail);
66985 }
66986 /* If KeyFetch()/DataFetch() managed to get the entire payload,
66987 ** save the payload in the u.an.pC->aRow cache. That will save us from
66988 ** having to make additional calls to fetch the content portion of
66989 ** the record.
66990 */
66991 assert( u.an.avail>=0 );
66992 if( u.an.payloadSize <= (u32)u.an.avail ){
66993 u.an.zRec = u.an.zData;
66994 u.an.pC->aRow = (u8*)u.an.zData;
66995 }else{
66996 u.an.pC->aRow = 0;
66997 }
66998 }
66999 /* The following assert is true in all cases accept when
67000 ** the database file has been corrupted externally.
67001 ** assert( u.an.zRec!=0 || u.an.avail>=u.an.payloadSize || u.an.avail>=9 ); */
67002 u.an.szHdr = getVarint32((u8*)u.an.zData, u.an.offset);
67003
67004 /* Make sure a corrupt database has not given us an oversize header.
67005 ** Do this now to avoid an oversize memory allocation.
67006 **
67007 ** Type entries can be between 1 and 5 bytes each. But 4 and 5 byte
@@ -65690,146 +67008,146 @@
67008 ** types use so much data space that there can only be 4096 and 32 of
67009 ** them, respectively. So the maximum header length results from a
67010 ** 3-byte type for each of the maximum of 32768 columns plus three
67011 ** extra bytes for the header length itself. 32768*3 + 3 = 98307.
67012 */
67013 if( u.an.offset > 98307 ){
67014 rc = SQLITE_CORRUPT_BKPT;
67015 goto op_column_out;
67016 }
67017
67018 /* Compute in u.an.len the number of bytes of data we need to read in order
67019 ** to get u.an.nField type values. u.an.offset is an upper bound on this. But
67020 ** u.an.nField might be significantly less than the true number of columns
67021 ** in the table, and in that case, 5*u.an.nField+3 might be smaller than u.an.offset.
67022 ** We want to minimize u.an.len in order to limit the size of the memory
67023 ** allocation, especially if a corrupt database file has caused u.an.offset
67024 ** to be oversized. Offset is limited to 98307 above. But 98307 might
67025 ** still exceed Robson memory allocation limits on some configurations.
67026 ** On systems that cannot tolerate large memory allocations, u.an.nField*5+3
67027 ** will likely be much smaller since u.an.nField will likely be less than
67028 ** 20 or so. This insures that Robson memory allocation limits are
67029 ** not exceeded even for corrupt database files.
67030 */
67031 u.an.len = u.an.nField*5 + 3;
67032 if( u.an.len > (int)u.an.offset ) u.an.len = (int)u.an.offset;
67033
67034 /* The KeyFetch() or DataFetch() above are fast and will get the entire
67035 ** record header in most cases. But they will fail to get the complete
67036 ** record header if the record header does not fit on a single page
67037 ** in the B-Tree. When that happens, use sqlite3VdbeMemFromBtree() to
67038 ** acquire the complete header text.
67039 */
67040 if( !u.an.zRec && u.an.avail<u.an.len ){
67041 u.an.sMem.flags = 0;
67042 u.an.sMem.db = 0;
67043 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, 0, u.an.len, u.an.pC->isIndex, &u.an.sMem);
67044 if( rc!=SQLITE_OK ){
67045 goto op_column_out;
67046 }
67047 u.an.zData = u.an.sMem.z;
67048 }
67049 u.an.zEndHdr = (u8 *)&u.an.zData[u.an.len];
67050 u.an.zIdx = (u8 *)&u.an.zData[u.an.szHdr];
67051
67052 /* Scan the header and use it to fill in the u.an.aType[] and u.an.aOffset[]
67053 ** arrays. u.an.aType[u.an.i] will contain the type integer for the u.an.i-th
67054 ** column and u.an.aOffset[u.an.i] will contain the u.an.offset from the beginning
67055 ** of the record to the start of the data for the u.an.i-th column
67056 */
67057 for(u.an.i=0; u.an.i<u.an.nField; u.an.i++){
67058 if( u.an.zIdx<u.an.zEndHdr ){
67059 u.an.aOffset[u.an.i] = u.an.offset;
67060 if( u.an.zIdx[0]<0x80 ){
67061 u.an.t = u.an.zIdx[0];
67062 u.an.zIdx++;
67063 }else{
67064 u.an.zIdx += sqlite3GetVarint32(u.an.zIdx, &u.an.t);
67065 }
67066 u.an.aType[u.an.i] = u.an.t;
67067 u.an.szField = sqlite3VdbeSerialTypeLen(u.an.t);
67068 u.an.offset += u.an.szField;
67069 if( u.an.offset<u.an.szField ){ /* True if u.an.offset overflows */
67070 u.an.zIdx = &u.an.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
67071 break;
67072 }
67073 }else{
67074 /* If u.an.i is less that u.an.nField, then there are less fields in this
67075 ** record than SetNumColumns indicated there are columns in the
67076 ** table. Set the u.an.offset for any extra columns not present in
67077 ** the record to 0. This tells code below to store a NULL
67078 ** instead of deserializing a value from the record.
67079 */
67080 u.an.aOffset[u.an.i] = 0;
67081 }
67082 }
67083 sqlite3VdbeMemRelease(&u.an.sMem);
67084 u.an.sMem.flags = MEM_Null;
67085
67086 /* If we have read more header data than was contained in the header,
67087 ** or if the end of the last field appears to be past the end of the
67088 ** record, or if the end of the last field appears to be before the end
67089 ** of the record (when all fields present), then we must be dealing
67090 ** with a corrupt database.
67091 */
67092 if( (u.an.zIdx > u.an.zEndHdr) || (u.an.offset > u.an.payloadSize)
67093 || (u.an.zIdx==u.an.zEndHdr && u.an.offset!=u.an.payloadSize) ){
67094 rc = SQLITE_CORRUPT_BKPT;
67095 goto op_column_out;
67096 }
67097 }
67098
67099 /* Get the column information. If u.an.aOffset[u.an.p2] is non-zero, then
67100 ** deserialize the value from the record. If u.an.aOffset[u.an.p2] is zero,
67101 ** then there are not enough fields in the record to satisfy the
67102 ** request. In this case, set the value NULL or to P4 if P4 is
67103 ** a pointer to a Mem object.
67104 */
67105 if( u.an.aOffset[u.an.p2] ){
67106 assert( rc==SQLITE_OK );
67107 if( u.an.zRec ){
67108 VdbeMemRelease(u.an.pDest);
67109 sqlite3VdbeSerialGet((u8 *)&u.an.zRec[u.an.aOffset[u.an.p2]], u.an.aType[u.an.p2], u.an.pDest);
67110 }else{
67111 u.an.len = sqlite3VdbeSerialTypeLen(u.an.aType[u.an.p2]);
67112 sqlite3VdbeMemMove(&u.an.sMem, u.an.pDest);
67113 rc = sqlite3VdbeMemFromBtree(u.an.pCrsr, u.an.aOffset[u.an.p2], u.an.len, u.an.pC->isIndex, &u.an.sMem);
67114 if( rc!=SQLITE_OK ){
67115 goto op_column_out;
67116 }
67117 u.an.zData = u.an.sMem.z;
67118 sqlite3VdbeSerialGet((u8*)u.an.zData, u.an.aType[u.an.p2], u.an.pDest);
67119 }
67120 u.an.pDest->enc = encoding;
67121 }else{
67122 if( pOp->p4type==P4_MEM ){
67123 sqlite3VdbeMemShallowCopy(u.an.pDest, pOp->p4.pMem, MEM_Static);
67124 }else{
67125 MemSetTypeFlag(u.an.pDest, MEM_Null);
67126 }
67127 }
67128
67129 /* If we dynamically allocated space to hold the data (in the
67130 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
67131 ** dynamically allocated space over to the u.an.pDest structure.
67132 ** This prevents a memory copy.
67133 */
67134 if( u.an.sMem.zMalloc ){
67135 assert( u.an.sMem.z==u.an.sMem.zMalloc );
67136 assert( !(u.an.pDest->flags & MEM_Dyn) );
67137 assert( !(u.an.pDest->flags & (MEM_Blob|MEM_Str)) || u.an.pDest->z==u.an.sMem.z );
67138 u.an.pDest->flags &= ~(MEM_Ephem|MEM_Static);
67139 u.an.pDest->flags |= MEM_Term;
67140 u.an.pDest->z = u.an.sMem.z;
67141 u.an.pDest->zMalloc = u.an.sMem.zMalloc;
67142 }
67143
67144 rc = sqlite3VdbeMemMakeWriteable(u.an.pDest);
67145
67146 op_column_out:
67147 UPDATE_MAX_BLOBSIZE(u.an.pDest);
67148 REGISTER_TRACE(pOp->p3, u.an.pDest);
67149 break;
67150 }
67151
67152 /* Opcode: Affinity P1 P2 * P4 *
67153 **
@@ -65838,24 +67156,24 @@
67156 ** P4 is a string that is P2 characters long. The nth character of the
67157 ** string indicates the column affinity that should be used for the nth
67158 ** memory cell in the range.
67159 */
67160 case OP_Affinity: {
67161 #if 0 /* local variables moved into u.ao */
67162 const char *zAffinity; /* The affinity to be applied */
67163 char cAff; /* A single character of affinity */
67164 #endif /* local variables moved into u.ao */
67165
67166 u.ao.zAffinity = pOp->p4.z;
67167 assert( u.ao.zAffinity!=0 );
67168 assert( u.ao.zAffinity[pOp->p2]==0 );
67169 pIn1 = &aMem[pOp->p1];
67170 while( (u.ao.cAff = *(u.ao.zAffinity++))!=0 ){
67171 assert( pIn1 <= &p->aMem[p->nMem] );
67172 assert( memIsValid(pIn1) );
67173 ExpandBlob(pIn1);
67174 applyAffinity(pIn1, u.ao.cAff, encoding);
67175 pIn1++;
67176 }
67177 break;
67178 }
67179
@@ -65873,11 +67191,11 @@
67191 ** macros defined in sqliteInt.h.
67192 **
67193 ** If P4 is NULL then all index fields have the affinity NONE.
67194 */
67195 case OP_MakeRecord: {
67196 #if 0 /* local variables moved into u.ap */
67197 u8 *zNewRecord; /* A buffer to hold the data for the new record */
67198 Mem *pRec; /* The new record */
67199 u64 nData; /* Number of bytes of data space */
67200 int nHdr; /* Number of bytes of header space */
67201 i64 nByte; /* Data space required for this record */
@@ -65889,11 +67207,11 @@
67207 int nField; /* Number of fields in the record */
67208 char *zAffinity; /* The affinity string for the record */
67209 int file_format; /* File format to use for encoding */
67210 int i; /* Space used in zNewRecord[] */
67211 int len; /* Length of a field */
67212 #endif /* local variables moved into u.ap */
67213
67214 /* Assuming the record contains N fields, the record format looks
67215 ** like this:
67216 **
67217 ** ------------------------------------------------------------------------
@@ -65906,87 +67224,87 @@
67224 ** Each type field is a varint representing the serial type of the
67225 ** corresponding data element (see sqlite3VdbeSerialType()). The
67226 ** hdr-size field is also a varint which is the offset from the beginning
67227 ** of the record to data0.
67228 */
67229 u.ap.nData = 0; /* Number of bytes of data space */
67230 u.ap.nHdr = 0; /* Number of bytes of header space */
67231 u.ap.nZero = 0; /* Number of zero bytes at the end of the record */
67232 u.ap.nField = pOp->p1;
67233 u.ap.zAffinity = pOp->p4.z;
67234 assert( u.ap.nField>0 && pOp->p2>0 && pOp->p2+u.ap.nField<=p->nMem+1 );
67235 u.ap.pData0 = &aMem[u.ap.nField];
67236 u.ap.nField = pOp->p2;
67237 u.ap.pLast = &u.ap.pData0[u.ap.nField-1];
67238 u.ap.file_format = p->minWriteFileFormat;
67239
67240 /* Identify the output register */
67241 assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
67242 pOut = &aMem[pOp->p3];
67243 memAboutToChange(p, pOut);
67244
67245 /* Loop through the elements that will make up the record to figure
67246 ** out how much space is required for the new record.
67247 */
67248 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
67249 assert( memIsValid(u.ap.pRec) );
67250 if( u.ap.zAffinity ){
67251 applyAffinity(u.ap.pRec, u.ap.zAffinity[u.ap.pRec-u.ap.pData0], encoding);
67252 }
67253 if( u.ap.pRec->flags&MEM_Zero && u.ap.pRec->n>0 ){
67254 sqlite3VdbeMemExpandBlob(u.ap.pRec);
67255 }
67256 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
67257 u.ap.len = sqlite3VdbeSerialTypeLen(u.ap.serial_type);
67258 u.ap.nData += u.ap.len;
67259 u.ap.nHdr += sqlite3VarintLen(u.ap.serial_type);
67260 if( u.ap.pRec->flags & MEM_Zero ){
67261 /* Only pure zero-filled BLOBs can be input to this Opcode.
67262 ** We do not allow blobs with a prefix and a zero-filled tail. */
67263 u.ap.nZero += u.ap.pRec->u.nZero;
67264 }else if( u.ap.len ){
67265 u.ap.nZero = 0;
67266 }
67267 }
67268
67269 /* Add the initial header varint and total the size */
67270 u.ap.nHdr += u.ap.nVarint = sqlite3VarintLen(u.ap.nHdr);
67271 if( u.ap.nVarint<sqlite3VarintLen(u.ap.nHdr) ){
67272 u.ap.nHdr++;
67273 }
67274 u.ap.nByte = u.ap.nHdr+u.ap.nData-u.ap.nZero;
67275 if( u.ap.nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67276 goto too_big;
67277 }
67278
67279 /* Make sure the output register has a buffer large enough to store
67280 ** the new record. The output register (pOp->p3) is not allowed to
67281 ** be one of the input registers (because the following call to
67282 ** sqlite3VdbeMemGrow() could clobber the value before it is used).
67283 */
67284 if( sqlite3VdbeMemGrow(pOut, (int)u.ap.nByte, 0) ){
67285 goto no_mem;
67286 }
67287 u.ap.zNewRecord = (u8 *)pOut->z;
67288
67289 /* Write the record */
67290 u.ap.i = putVarint32(u.ap.zNewRecord, u.ap.nHdr);
67291 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){
67292 u.ap.serial_type = sqlite3VdbeSerialType(u.ap.pRec, u.ap.file_format);
67293 u.ap.i += putVarint32(&u.ap.zNewRecord[u.ap.i], u.ap.serial_type); /* serial type */
67294 }
67295 for(u.ap.pRec=u.ap.pData0; u.ap.pRec<=u.ap.pLast; u.ap.pRec++){ /* serial data */
67296 u.ap.i += sqlite3VdbeSerialPut(&u.ap.zNewRecord[u.ap.i], (int)(u.ap.nByte-u.ap.i), u.ap.pRec,u.ap.file_format);
67297 }
67298 assert( u.ap.i==u.ap.nByte );
67299
67300 assert( pOp->p3>0 && pOp->p3<=p->nMem );
67301 pOut->n = (int)u.ap.nByte;
67302 pOut->flags = MEM_Blob | MEM_Dyn;
67303 pOut->xDel = 0;
67304 if( u.ap.nZero ){
67305 pOut->u.nZero = u.ap.nZero;
67306 pOut->flags |= MEM_Zero;
67307 }
67308 pOut->enc = SQLITE_UTF8; /* In case the blob is ever converted to text */
67309 REGISTER_TRACE(pOp->p3, pOut);
67310 UPDATE_MAX_BLOBSIZE(pOut);
@@ -65998,22 +67316,22 @@
67316 ** Store the number of entries (an integer value) in the table or index
67317 ** opened by cursor P1 in register P2
67318 */
67319 #ifndef SQLITE_OMIT_BTREECOUNT
67320 case OP_Count: { /* out2-prerelease */
67321 #if 0 /* local variables moved into u.aq */
67322 i64 nEntry;
67323 BtCursor *pCrsr;
67324 #endif /* local variables moved into u.aq */
67325
67326 u.aq.pCrsr = p->apCsr[pOp->p1]->pCursor;
67327 if( ALWAYS(u.aq.pCrsr) ){
67328 rc = sqlite3BtreeCount(u.aq.pCrsr, &u.aq.nEntry);
67329 }else{
67330 u.aq.nEntry = 0;
67331 }
67332 pOut->u.i = u.aq.nEntry;
67333 break;
67334 }
67335 #endif
67336
67337 /* Opcode: Savepoint P1 * * P4 *
@@ -66021,42 +67339,42 @@
67339 ** Open, release or rollback the savepoint named by parameter P4, depending
67340 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
67341 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
67342 */
67343 case OP_Savepoint: {
67344 #if 0 /* local variables moved into u.ar */
67345 int p1; /* Value of P1 operand */
67346 char *zName; /* Name of savepoint */
67347 int nName;
67348 Savepoint *pNew;
67349 Savepoint *pSavepoint;
67350 Savepoint *pTmp;
67351 int iSavepoint;
67352 int ii;
67353 #endif /* local variables moved into u.ar */
67354
67355 u.ar.p1 = pOp->p1;
67356 u.ar.zName = pOp->p4.z;
67357
67358 /* Assert that the u.ar.p1 parameter is valid. Also that if there is no open
67359 ** transaction, then there cannot be any savepoints.
67360 */
67361 assert( db->pSavepoint==0 || db->autoCommit==0 );
67362 assert( u.ar.p1==SAVEPOINT_BEGIN||u.ar.p1==SAVEPOINT_RELEASE||u.ar.p1==SAVEPOINT_ROLLBACK );
67363 assert( db->pSavepoint || db->isTransactionSavepoint==0 );
67364 assert( checkSavepointCount(db) );
67365
67366 if( u.ar.p1==SAVEPOINT_BEGIN ){
67367 if( db->writeVdbeCnt>0 ){
67368 /* A new savepoint cannot be created if there are active write
67369 ** statements (i.e. open read/write incremental blob handles).
67370 */
67371 sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
67372 "SQL statements in progress");
67373 rc = SQLITE_BUSY;
67374 }else{
67375 u.ar.nName = sqlite3Strlen30(u.ar.zName);
67376
67377 #ifndef SQLITE_OMIT_VIRTUALTABLE
67378 /* This call is Ok even if this savepoint is actually a transaction
67379 ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
67380 ** If this is a transaction savepoint being opened, it is guaranteed
@@ -66066,14 +67384,14 @@
67384 db->nStatement+db->nSavepoint);
67385 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67386 #endif
67387
67388 /* Create a new savepoint structure. */
67389 u.ar.pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+u.ar.nName+1);
67390 if( u.ar.pNew ){
67391 u.ar.pNew->zName = (char *)&u.ar.pNew[1];
67392 memcpy(u.ar.pNew->zName, u.ar.zName, u.ar.nName+1);
67393
67394 /* If there is no open transaction, then mark this as a special
67395 ** "transaction savepoint". */
67396 if( db->autoCommit ){
67397 db->autoCommit = 0;
@@ -66081,50 +67399,50 @@
67399 }else{
67400 db->nSavepoint++;
67401 }
67402
67403 /* Link the new savepoint into the database handle's list. */
67404 u.ar.pNew->pNext = db->pSavepoint;
67405 db->pSavepoint = u.ar.pNew;
67406 u.ar.pNew->nDeferredCons = db->nDeferredCons;
67407 }
67408 }
67409 }else{
67410 u.ar.iSavepoint = 0;
67411
67412 /* Find the named savepoint. If there is no such savepoint, then an
67413 ** an error is returned to the user. */
67414 for(
67415 u.ar.pSavepoint = db->pSavepoint;
67416 u.ar.pSavepoint && sqlite3StrICmp(u.ar.pSavepoint->zName, u.ar.zName);
67417 u.ar.pSavepoint = u.ar.pSavepoint->pNext
67418 ){
67419 u.ar.iSavepoint++;
67420 }
67421 if( !u.ar.pSavepoint ){
67422 sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", u.ar.zName);
67423 rc = SQLITE_ERROR;
67424 }else if(
67425 db->writeVdbeCnt>0 || (u.ar.p1==SAVEPOINT_ROLLBACK && db->activeVdbeCnt>1)
67426 ){
67427 /* It is not possible to release (commit) a savepoint if there are
67428 ** active write statements. It is not possible to rollback a savepoint
67429 ** if there are any active statements at all.
67430 */
67431 sqlite3SetString(&p->zErrMsg, db,
67432 "cannot %s savepoint - SQL statements in progress",
67433 (u.ar.p1==SAVEPOINT_ROLLBACK ? "rollback": "release")
67434 );
67435 rc = SQLITE_BUSY;
67436 }else{
67437
67438 /* Determine whether or not this is a transaction savepoint. If so,
67439 ** and this is a RELEASE command, then the current transaction
67440 ** is committed.
67441 */
67442 int isTransaction = u.ar.pSavepoint->pNext==0 && db->isTransactionSavepoint;
67443 if( isTransaction && u.ar.p1==SAVEPOINT_RELEASE ){
67444 if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
67445 goto vdbe_return;
67446 }
67447 db->autoCommit = 1;
67448 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
@@ -66134,50 +67452,50 @@
67452 goto vdbe_return;
67453 }
67454 db->isTransactionSavepoint = 0;
67455 rc = p->rc;
67456 }else{
67457 u.ar.iSavepoint = db->nSavepoint - u.ar.iSavepoint - 1;
67458 for(u.ar.ii=0; u.ar.ii<db->nDb; u.ar.ii++){
67459 rc = sqlite3BtreeSavepoint(db->aDb[u.ar.ii].pBt, u.ar.p1, u.ar.iSavepoint);
67460 if( rc!=SQLITE_OK ){
67461 goto abort_due_to_error;
67462 }
67463 }
67464 if( u.ar.p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
67465 sqlite3ExpirePreparedStatements(db);
67466 sqlite3ResetInternalSchema(db, -1);
67467 db->flags = (db->flags | SQLITE_InternChanges);
67468 }
67469 }
67470
67471 /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
67472 ** savepoints nested inside of the savepoint being operated on. */
67473 while( db->pSavepoint!=u.ar.pSavepoint ){
67474 u.ar.pTmp = db->pSavepoint;
67475 db->pSavepoint = u.ar.pTmp->pNext;
67476 sqlite3DbFree(db, u.ar.pTmp);
67477 db->nSavepoint--;
67478 }
67479
67480 /* If it is a RELEASE, then destroy the savepoint being operated on
67481 ** too. If it is a ROLLBACK TO, then set the number of deferred
67482 ** constraint violations present in the database to the value stored
67483 ** when the savepoint was created. */
67484 if( u.ar.p1==SAVEPOINT_RELEASE ){
67485 assert( u.ar.pSavepoint==db->pSavepoint );
67486 db->pSavepoint = u.ar.pSavepoint->pNext;
67487 sqlite3DbFree(db, u.ar.pSavepoint);
67488 if( !isTransaction ){
67489 db->nSavepoint--;
67490 }
67491 }else{
67492 db->nDeferredCons = u.ar.pSavepoint->nDeferredCons;
67493 }
67494
67495 if( !isTransaction ){
67496 rc = sqlite3VtabSavepoint(db, u.ar.p1, u.ar.iSavepoint);
67497 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67498 }
67499 }
67500 }
67501
@@ -66192,50 +67510,50 @@
67510 ** there are active writing VMs or active VMs that use shared cache.
67511 **
67512 ** This instruction causes the VM to halt.
67513 */
67514 case OP_AutoCommit: {
67515 #if 0 /* local variables moved into u.as */
67516 int desiredAutoCommit;
67517 int iRollback;
67518 int turnOnAC;
67519 #endif /* local variables moved into u.as */
67520
67521 u.as.desiredAutoCommit = pOp->p1;
67522 u.as.iRollback = pOp->p2;
67523 u.as.turnOnAC = u.as.desiredAutoCommit && !db->autoCommit;
67524 assert( u.as.desiredAutoCommit==1 || u.as.desiredAutoCommit==0 );
67525 assert( u.as.desiredAutoCommit==1 || u.as.iRollback==0 );
67526 assert( db->activeVdbeCnt>0 ); /* At least this one VM is active */
67527
67528 if( u.as.turnOnAC && u.as.iRollback && db->activeVdbeCnt>1 ){
67529 /* If this instruction implements a ROLLBACK and other VMs are
67530 ** still running, and a transaction is active, return an error indicating
67531 ** that the other VMs must complete first.
67532 */
67533 sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
67534 "SQL statements in progress");
67535 rc = SQLITE_BUSY;
67536 }else if( u.as.turnOnAC && !u.as.iRollback && db->writeVdbeCnt>0 ){
67537 /* If this instruction implements a COMMIT and other VMs are writing
67538 ** return an error indicating that the other VMs must complete first.
67539 */
67540 sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
67541 "SQL statements in progress");
67542 rc = SQLITE_BUSY;
67543 }else if( u.as.desiredAutoCommit!=db->autoCommit ){
67544 if( u.as.iRollback ){
67545 assert( u.as.desiredAutoCommit==1 );
67546 sqlite3RollbackAll(db);
67547 db->autoCommit = 1;
67548 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
67549 goto vdbe_return;
67550 }else{
67551 db->autoCommit = (u8)u.as.desiredAutoCommit;
67552 if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
67553 p->pc = pc;
67554 db->autoCommit = (u8)(1-u.as.desiredAutoCommit);
67555 p->rc = rc = SQLITE_BUSY;
67556 goto vdbe_return;
67557 }
67558 }
67559 assert( db->nStatement==0 );
@@ -66246,12 +67564,12 @@
67564 rc = SQLITE_ERROR;
67565 }
67566 goto vdbe_return;
67567 }else{
67568 sqlite3SetString(&p->zErrMsg, db,
67569 (!u.as.desiredAutoCommit)?"cannot start a transaction within a transaction":(
67570 (u.as.iRollback)?"cannot rollback - no transaction is active":
67571 "cannot commit - no transaction is active"));
67572
67573 rc = SQLITE_ERROR;
67574 }
67575 break;
@@ -66287,20 +67605,20 @@
67605 ** will automatically commit when the VDBE halts.
67606 **
67607 ** If P2 is zero, then a read-lock is obtained on the database file.
67608 */
67609 case OP_Transaction: {
67610 #if 0 /* local variables moved into u.at */
67611 Btree *pBt;
67612 #endif /* local variables moved into u.at */
67613
67614 assert( pOp->p1>=0 && pOp->p1<db->nDb );
67615 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67616 u.at.pBt = db->aDb[pOp->p1].pBt;
67617
67618 if( u.at.pBt ){
67619 rc = sqlite3BtreeBeginTrans(u.at.pBt, pOp->p2);
67620 if( rc==SQLITE_BUSY ){
67621 p->pc = pc;
67622 p->rc = rc = SQLITE_BUSY;
67623 goto vdbe_return;
67624 }
@@ -66309,20 +67627,20 @@
67627 }
67628
67629 if( pOp->p2 && p->usesStmtJournal
67630 && (db->autoCommit==0 || db->activeVdbeCnt>1)
67631 ){
67632 assert( sqlite3BtreeIsInTrans(u.at.pBt) );
67633 if( p->iStatement==0 ){
67634 assert( db->nStatement>=0 && db->nSavepoint>=0 );
67635 db->nStatement++;
67636 p->iStatement = db->nSavepoint + db->nStatement;
67637 }
67638
67639 rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
67640 if( rc==SQLITE_OK ){
67641 rc = sqlite3BtreeBeginStmt(u.at.pBt, p->iStatement);
67642 }
67643
67644 /* Store the current value of the database handles deferred constraint
67645 ** counter. If the statement transaction needs to be rolled back,
67646 ** the value of this counter needs to be restored too. */
@@ -66343,25 +67661,25 @@
67661 ** There must be a read-lock on the database (either a transaction
67662 ** must be started or there must be an open cursor) before
67663 ** executing this instruction.
67664 */
67665 case OP_ReadCookie: { /* out2-prerelease */
67666 #if 0 /* local variables moved into u.au */
67667 int iMeta;
67668 int iDb;
67669 int iCookie;
67670 #endif /* local variables moved into u.au */
67671
67672 u.au.iDb = pOp->p1;
67673 u.au.iCookie = pOp->p3;
67674 assert( pOp->p3<SQLITE_N_BTREE_META );
67675 assert( u.au.iDb>=0 && u.au.iDb<db->nDb );
67676 assert( db->aDb[u.au.iDb].pBt!=0 );
67677 assert( (p->btreeMask & (((yDbMask)1)<<u.au.iDb))!=0 );
67678
67679 sqlite3BtreeGetMeta(db->aDb[u.au.iDb].pBt, u.au.iCookie, (u32 *)&u.au.iMeta);
67680 pOut->u.i = u.au.iMeta;
67681 break;
67682 }
67683
67684 /* Opcode: SetCookie P1 P2 P3 * *
67685 **
@@ -66372,30 +67690,30 @@
67690 ** database file used to store temporary tables.
67691 **
67692 ** A transaction must be started before executing this opcode.
67693 */
67694 case OP_SetCookie: { /* in3 */
67695 #if 0 /* local variables moved into u.av */
67696 Db *pDb;
67697 #endif /* local variables moved into u.av */
67698 assert( pOp->p2<SQLITE_N_BTREE_META );
67699 assert( pOp->p1>=0 && pOp->p1<db->nDb );
67700 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67701 u.av.pDb = &db->aDb[pOp->p1];
67702 assert( u.av.pDb->pBt!=0 );
67703 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
67704 pIn3 = &aMem[pOp->p3];
67705 sqlite3VdbeMemIntegerify(pIn3);
67706 /* See note about index shifting on OP_ReadCookie */
67707 rc = sqlite3BtreeUpdateMeta(u.av.pDb->pBt, pOp->p2, (int)pIn3->u.i);
67708 if( pOp->p2==BTREE_SCHEMA_VERSION ){
67709 /* When the schema cookie changes, record the new cookie internally */
67710 u.av.pDb->pSchema->schema_cookie = (int)pIn3->u.i;
67711 db->flags |= SQLITE_InternChanges;
67712 }else if( pOp->p2==BTREE_FILE_FORMAT ){
67713 /* Record changes in the file format */
67714 u.av.pDb->pSchema->file_format = (u8)pIn3->u.i;
67715 }
67716 if( pOp->p1==1 ){
67717 /* Invalidate all prepared statements whenever the TEMP database
67718 ** schema is changed. Ticket #1644 */
67719 sqlite3ExpirePreparedStatements(db);
@@ -66421,27 +67739,27 @@
67739 ** Either a transaction needs to have been started or an OP_Open needs
67740 ** to be executed (to establish a read lock) before this opcode is
67741 ** invoked.
67742 */
67743 case OP_VerifyCookie: {
67744 #if 0 /* local variables moved into u.aw */
67745 int iMeta;
67746 int iGen;
67747 Btree *pBt;
67748 #endif /* local variables moved into u.aw */
67749
67750 assert( pOp->p1>=0 && pOp->p1<db->nDb );
67751 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67752 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
67753 u.aw.pBt = db->aDb[pOp->p1].pBt;
67754 if( u.aw.pBt ){
67755 sqlite3BtreeGetMeta(u.aw.pBt, BTREE_SCHEMA_VERSION, (u32 *)&u.aw.iMeta);
67756 u.aw.iGen = db->aDb[pOp->p1].pSchema->iGeneration;
67757 }else{
67758 u.aw.iGen = u.aw.iMeta = 0;
67759 }
67760 if( u.aw.iMeta!=pOp->p2 || u.aw.iGen!=pOp->p3 ){
67761 sqlite3DbFree(db, p->zErrMsg);
67762 p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
67763 /* If the schema-cookie from the database file matches the cookie
67764 ** stored with the in-memory representation of the schema, do
67765 ** not reload the schema from the database file.
@@ -66453,11 +67771,11 @@
67771 ** discard the database schema, as the user code implementing the
67772 ** v-table would have to be ready for the sqlite3_vtab structure itself
67773 ** to be invalidated whenever sqlite3_step() is called from within
67774 ** a v-table method.
67775 */
67776 if( db->aDb[pOp->p1].pSchema->schema_cookie!=u.aw.iMeta ){
67777 sqlite3ResetInternalSchema(db, pOp->p1);
67778 }
67779
67780 p->expired = 1;
67781 rc = SQLITE_SCHEMA;
@@ -66514,86 +67832,86 @@
67832 **
67833 ** See also OpenRead.
67834 */
67835 case OP_OpenRead:
67836 case OP_OpenWrite: {
67837 #if 0 /* local variables moved into u.ax */
67838 int nField;
67839 KeyInfo *pKeyInfo;
67840 int p2;
67841 int iDb;
67842 int wrFlag;
67843 Btree *pX;
67844 VdbeCursor *pCur;
67845 Db *pDb;
67846 #endif /* local variables moved into u.ax */
67847
67848 if( p->expired ){
67849 rc = SQLITE_ABORT;
67850 break;
67851 }
67852
67853 u.ax.nField = 0;
67854 u.ax.pKeyInfo = 0;
67855 u.ax.p2 = pOp->p2;
67856 u.ax.iDb = pOp->p3;
67857 assert( u.ax.iDb>=0 && u.ax.iDb<db->nDb );
67858 assert( (p->btreeMask & (((yDbMask)1)<<u.ax.iDb))!=0 );
67859 u.ax.pDb = &db->aDb[u.ax.iDb];
67860 u.ax.pX = u.ax.pDb->pBt;
67861 assert( u.ax.pX!=0 );
67862 if( pOp->opcode==OP_OpenWrite ){
67863 u.ax.wrFlag = 1;
67864 assert( sqlite3SchemaMutexHeld(db, u.ax.iDb, 0) );
67865 if( u.ax.pDb->pSchema->file_format < p->minWriteFileFormat ){
67866 p->minWriteFileFormat = u.ax.pDb->pSchema->file_format;
67867 }
67868 }else{
67869 u.ax.wrFlag = 0;
67870 }
67871 if( pOp->p5 ){
67872 assert( u.ax.p2>0 );
67873 assert( u.ax.p2<=p->nMem );
67874 pIn2 = &aMem[u.ax.p2];
67875 assert( memIsValid(pIn2) );
67876 assert( (pIn2->flags & MEM_Int)!=0 );
67877 sqlite3VdbeMemIntegerify(pIn2);
67878 u.ax.p2 = (int)pIn2->u.i;
67879 /* The u.ax.p2 value always comes from a prior OP_CreateTable opcode and
67880 ** that opcode will always set the u.ax.p2 value to 2 or more or else fail.
67881 ** If there were a failure, the prepared statement would have halted
67882 ** before reaching this instruction. */
67883 if( NEVER(u.ax.p2<2) ) {
67884 rc = SQLITE_CORRUPT_BKPT;
67885 goto abort_due_to_error;
67886 }
67887 }
67888 if( pOp->p4type==P4_KEYINFO ){
67889 u.ax.pKeyInfo = pOp->p4.pKeyInfo;
67890 u.ax.pKeyInfo->enc = ENC(p->db);
67891 u.ax.nField = u.ax.pKeyInfo->nField+1;
67892 }else if( pOp->p4type==P4_INT32 ){
67893 u.ax.nField = pOp->p4.i;
67894 }
67895 assert( pOp->p1>=0 );
67896 u.ax.pCur = allocateCursor(p, pOp->p1, u.ax.nField, u.ax.iDb, 1);
67897 if( u.ax.pCur==0 ) goto no_mem;
67898 u.ax.pCur->nullRow = 1;
67899 u.ax.pCur->isOrdered = 1;
67900 rc = sqlite3BtreeCursor(u.ax.pX, u.ax.p2, u.ax.wrFlag, u.ax.pKeyInfo, u.ax.pCur->pCursor);
67901 u.ax.pCur->pKeyInfo = u.ax.pKeyInfo;
67902
67903 /* Since it performs no memory allocation or IO, the only value that
67904 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
67905 assert( rc==SQLITE_OK );
67906
67907 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
67908 ** SQLite used to check if the root-page flags were sane at this point
67909 ** and report database corruption if they were not, but this check has
67910 ** since moved into the btree layer. */
67911 u.ax.pCur->isTable = pOp->p4type!=P4_KEYINFO;
67912 u.ax.pCur->isIndex = !u.ax.pCur->isTable;
67913 break;
67914 }
67915
67916 /* Opcode: OpenEphemeral P1 P2 * P4 P5
67917 **
@@ -66625,28 +67943,28 @@
67943 ** by this opcode will be used for automatically created transient
67944 ** indices in joins.
67945 */
67946 case OP_OpenAutoindex:
67947 case OP_OpenEphemeral: {
67948 #if 0 /* local variables moved into u.ay */
67949 VdbeCursor *pCx;
67950 #endif /* local variables moved into u.ay */
67951 static const int vfsFlags =
67952 SQLITE_OPEN_READWRITE |
67953 SQLITE_OPEN_CREATE |
67954 SQLITE_OPEN_EXCLUSIVE |
67955 SQLITE_OPEN_DELETEONCLOSE |
67956 SQLITE_OPEN_TRANSIENT_DB;
67957
67958 assert( pOp->p1>=0 );
67959 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
67960 if( u.ay.pCx==0 ) goto no_mem;
67961 u.ay.pCx->nullRow = 1;
67962 rc = sqlite3BtreeOpen(db->pVfs, 0, db, &u.ay.pCx->pBt,
67963 BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
67964 if( rc==SQLITE_OK ){
67965 rc = sqlite3BtreeBeginTrans(u.ay.pCx->pBt, 1);
67966 }
67967 if( rc==SQLITE_OK ){
67968 /* If a transient index is required, create it by calling
67969 ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
67970 ** opening it. If a transient table is required, just use the
@@ -66653,26 +67971,26 @@
67971 ** automatically created table with root-page 1 (an BLOB_INTKEY table).
67972 */
67973 if( pOp->p4.pKeyInfo ){
67974 int pgno;
67975 assert( pOp->p4type==P4_KEYINFO );
67976 rc = sqlite3BtreeCreateTable(u.ay.pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
67977 if( rc==SQLITE_OK ){
67978 assert( pgno==MASTER_ROOT+1 );
67979 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, pgno, 1,
67980 (KeyInfo*)pOp->p4.z, u.ay.pCx->pCursor);
67981 u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
67982 u.ay.pCx->pKeyInfo->enc = ENC(p->db);
67983 }
67984 u.ay.pCx->isTable = 0;
67985 }else{
67986 rc = sqlite3BtreeCursor(u.ay.pCx->pBt, MASTER_ROOT, 1, 0, u.ay.pCx->pCursor);
67987 u.ay.pCx->isTable = 1;
67988 }
67989 }
67990 u.ay.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
67991 u.ay.pCx->isIndex = !u.ay.pCx->isTable;
67992 break;
67993 }
67994
67995 /* Opcode: OpenSorter P1 P2 * P4 *
67996 **
@@ -66679,20 +67997,20 @@
67997 ** This opcode works like OP_OpenEphemeral except that it opens
67998 ** a transient index that is specifically designed to sort large
67999 ** tables using an external merge-sort algorithm.
68000 */
68001 case OP_SorterOpen: {
68002 #if 0 /* local variables moved into u.az */
68003 VdbeCursor *pCx;
68004 #endif /* local variables moved into u.az */
68005 #ifndef SQLITE_OMIT_MERGE_SORT
68006 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
68007 if( u.az.pCx==0 ) goto no_mem;
68008 u.az.pCx->pKeyInfo = pOp->p4.pKeyInfo;
68009 u.az.pCx->pKeyInfo->enc = ENC(p->db);
68010 u.az.pCx->isSorter = 1;
68011 rc = sqlite3VdbeSorterInit(db, u.az.pCx);
68012 #else
68013 pOp->opcode = OP_OpenEphemeral;
68014 pc--;
68015 #endif
68016 break;
@@ -66712,21 +68030,21 @@
68030 **
68031 ** P3 is the number of fields in the records that will be stored by
68032 ** the pseudo-table.
68033 */
68034 case OP_OpenPseudo: {
68035 #if 0 /* local variables moved into u.ba */
68036 VdbeCursor *pCx;
68037 #endif /* local variables moved into u.ba */
68038
68039 assert( pOp->p1>=0 );
68040 u.ba.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
68041 if( u.ba.pCx==0 ) goto no_mem;
68042 u.ba.pCx->nullRow = 1;
68043 u.ba.pCx->pseudoTableReg = pOp->p2;
68044 u.ba.pCx->isTable = 1;
68045 u.ba.pCx->isIndex = 0;
68046 break;
68047 }
68048
68049 /* Opcode: Close P1 * * * *
68050 **
@@ -66794,39 +68112,39 @@
68112 */
68113 case OP_SeekLt: /* jump, in3 */
68114 case OP_SeekLe: /* jump, in3 */
68115 case OP_SeekGe: /* jump, in3 */
68116 case OP_SeekGt: { /* jump, in3 */
68117 #if 0 /* local variables moved into u.bb */
68118 int res;
68119 int oc;
68120 VdbeCursor *pC;
68121 UnpackedRecord r;
68122 int nField;
68123 i64 iKey; /* The rowid we are to seek to */
68124 #endif /* local variables moved into u.bb */
68125
68126 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68127 assert( pOp->p2!=0 );
68128 u.bb.pC = p->apCsr[pOp->p1];
68129 assert( u.bb.pC!=0 );
68130 assert( u.bb.pC->pseudoTableReg==0 );
68131 assert( OP_SeekLe == OP_SeekLt+1 );
68132 assert( OP_SeekGe == OP_SeekLt+2 );
68133 assert( OP_SeekGt == OP_SeekLt+3 );
68134 assert( u.bb.pC->isOrdered );
68135 if( ALWAYS(u.bb.pC->pCursor!=0) ){
68136 u.bb.oc = pOp->opcode;
68137 u.bb.pC->nullRow = 0;
68138 if( u.bb.pC->isTable ){
68139 /* The input value in P3 might be of any type: integer, real, string,
68140 ** blob, or NULL. But it needs to be an integer before we can do
68141 ** the seek, so covert it. */
68142 pIn3 = &aMem[pOp->p3];
68143 applyNumericAffinity(pIn3);
68144 u.bb.iKey = sqlite3VdbeIntValue(pIn3);
68145 u.bb.pC->rowidIsValid = 0;
68146
68147 /* If the P3 value could not be converted into an integer without
68148 ** loss of information, then special processing is required... */
68149 if( (pIn3->flags & MEM_Int)==0 ){
68150 if( (pIn3->flags & MEM_Real)==0 ){
@@ -66837,105 +68155,105 @@
68155 }
68156 /* If we reach this point, then the P3 value must be a floating
68157 ** point number. */
68158 assert( (pIn3->flags & MEM_Real)!=0 );
68159
68160 if( u.bb.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.bb.iKey || pIn3->r>0) ){
68161 /* The P3 value is too large in magnitude to be expressed as an
68162 ** integer. */
68163 u.bb.res = 1;
68164 if( pIn3->r<0 ){
68165 if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
68166 rc = sqlite3BtreeFirst(u.bb.pC->pCursor, &u.bb.res);
68167 if( rc!=SQLITE_OK ) goto abort_due_to_error;
68168 }
68169 }else{
68170 if( u.bb.oc<=OP_SeekLe ){ assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
68171 rc = sqlite3BtreeLast(u.bb.pC->pCursor, &u.bb.res);
68172 if( rc!=SQLITE_OK ) goto abort_due_to_error;
68173 }
68174 }
68175 if( u.bb.res ){
68176 pc = pOp->p2 - 1;
68177 }
68178 break;
68179 }else if( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekGe ){
68180 /* Use the ceiling() function to convert real->int */
68181 if( pIn3->r > (double)u.bb.iKey ) u.bb.iKey++;
68182 }else{
68183 /* Use the floor() function to convert real->int */
68184 assert( u.bb.oc==OP_SeekLe || u.bb.oc==OP_SeekGt );
68185 if( pIn3->r < (double)u.bb.iKey ) u.bb.iKey--;
68186 }
68187 }
68188 rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, 0, (u64)u.bb.iKey, 0, &u.bb.res);
68189 if( rc!=SQLITE_OK ){
68190 goto abort_due_to_error;
68191 }
68192 if( u.bb.res==0 ){
68193 u.bb.pC->rowidIsValid = 1;
68194 u.bb.pC->lastRowid = u.bb.iKey;
68195 }
68196 }else{
68197 u.bb.nField = pOp->p4.i;
68198 assert( pOp->p4type==P4_INT32 );
68199 assert( u.bb.nField>0 );
68200 u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
68201 u.bb.r.nField = (u16)u.bb.nField;
68202
68203 /* The next line of code computes as follows, only faster:
68204 ** if( u.bb.oc==OP_SeekGt || u.bb.oc==OP_SeekLe ){
68205 ** u.bb.r.flags = UNPACKED_INCRKEY;
68206 ** }else{
68207 ** u.bb.r.flags = 0;
68208 ** }
68209 */
68210 u.bb.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bb.oc - OP_SeekLt)));
68211 assert( u.bb.oc!=OP_SeekGt || u.bb.r.flags==UNPACKED_INCRKEY );
68212 assert( u.bb.oc!=OP_SeekLe || u.bb.r.flags==UNPACKED_INCRKEY );
68213 assert( u.bb.oc!=OP_SeekGe || u.bb.r.flags==0 );
68214 assert( u.bb.oc!=OP_SeekLt || u.bb.r.flags==0 );
68215
68216 u.bb.r.aMem = &aMem[pOp->p3];
68217 #ifdef SQLITE_DEBUG
68218 { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
68219 #endif
68220 ExpandBlob(u.bb.r.aMem);
68221 rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, &u.bb.r, 0, 0, &u.bb.res);
68222 if( rc!=SQLITE_OK ){
68223 goto abort_due_to_error;
68224 }
68225 u.bb.pC->rowidIsValid = 0;
68226 }
68227 u.bb.pC->deferredMoveto = 0;
68228 u.bb.pC->cacheStatus = CACHE_STALE;
68229 #ifdef SQLITE_TEST
68230 sqlite3_search_count++;
68231 #endif
68232 if( u.bb.oc>=OP_SeekGe ){ assert( u.bb.oc==OP_SeekGe || u.bb.oc==OP_SeekGt );
68233 if( u.bb.res<0 || (u.bb.res==0 && u.bb.oc==OP_SeekGt) ){
68234 rc = sqlite3BtreeNext(u.bb.pC->pCursor, &u.bb.res);
68235 if( rc!=SQLITE_OK ) goto abort_due_to_error;
68236 u.bb.pC->rowidIsValid = 0;
68237 }else{
68238 u.bb.res = 0;
68239 }
68240 }else{
68241 assert( u.bb.oc==OP_SeekLt || u.bb.oc==OP_SeekLe );
68242 if( u.bb.res>0 || (u.bb.res==0 && u.bb.oc==OP_SeekLt) ){
68243 rc = sqlite3BtreePrevious(u.bb.pC->pCursor, &u.bb.res);
68244 if( rc!=SQLITE_OK ) goto abort_due_to_error;
68245 u.bb.pC->rowidIsValid = 0;
68246 }else{
68247 /* u.bb.res might be negative because the table is empty. Check to
68248 ** see if this is the case.
68249 */
68250 u.bb.res = sqlite3BtreeEof(u.bb.pC->pCursor);
68251 }
68252 }
68253 assert( pOp->p2>0 );
68254 if( u.bb.res ){
68255 pc = pOp->p2 - 1;
68256 }
68257 }else{
68258 /* This happens when attempting to open the sqlite3_master table
68259 ** for read access returns SQLITE_EMPTY. In this case always
@@ -66954,24 +68272,24 @@
68272 ** This is actually a deferred seek. Nothing actually happens until
68273 ** the cursor is used to read a record. That way, if no reads
68274 ** occur, no unnecessary I/O happens.
68275 */
68276 case OP_Seek: { /* in2 */
68277 #if 0 /* local variables moved into u.bc */
68278 VdbeCursor *pC;
68279 #endif /* local variables moved into u.bc */
68280
68281 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68282 u.bc.pC = p->apCsr[pOp->p1];
68283 assert( u.bc.pC!=0 );
68284 if( ALWAYS(u.bc.pC->pCursor!=0) ){
68285 assert( u.bc.pC->isTable );
68286 u.bc.pC->nullRow = 0;
68287 pIn2 = &aMem[pOp->p2];
68288 u.bc.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
68289 u.bc.pC->rowidIsValid = 0;
68290 u.bc.pC->deferredMoveto = 1;
68291 }
68292 break;
68293 }
68294
68295
@@ -66999,67 +68317,67 @@
68317 **
68318 ** See also: Found, NotExists, IsUnique
68319 */
68320 case OP_NotFound: /* jump, in3 */
68321 case OP_Found: { /* jump, in3 */
68322 #if 0 /* local variables moved into u.bd */
68323 int alreadyExists;
68324 VdbeCursor *pC;
68325 int res;
68326 char *pFree;
68327 UnpackedRecord *pIdxKey;
68328 UnpackedRecord r;
68329 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
68330 #endif /* local variables moved into u.bd */
68331
68332 #ifdef SQLITE_TEST
68333 sqlite3_found_count++;
68334 #endif
68335
68336 u.bd.alreadyExists = 0;
68337 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68338 assert( pOp->p4type==P4_INT32 );
68339 u.bd.pC = p->apCsr[pOp->p1];
68340 assert( u.bd.pC!=0 );
68341 pIn3 = &aMem[pOp->p3];
68342 if( ALWAYS(u.bd.pC->pCursor!=0) ){
68343
68344 assert( u.bd.pC->isTable==0 );
68345 if( pOp->p4.i>0 ){
68346 u.bd.r.pKeyInfo = u.bd.pC->pKeyInfo;
68347 u.bd.r.nField = (u16)pOp->p4.i;
68348 u.bd.r.aMem = pIn3;
68349 #ifdef SQLITE_DEBUG
68350 { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
68351 #endif
68352 u.bd.r.flags = UNPACKED_PREFIX_MATCH;
68353 u.bd.pIdxKey = &u.bd.r;
68354 }else{
68355 u.bd.pIdxKey = sqlite3VdbeAllocUnpackedRecord(
68356 u.bd.pC->pKeyInfo, u.bd.aTempRec, sizeof(u.bd.aTempRec), &u.bd.pFree
68357 );
68358 if( u.bd.pIdxKey==0 ) goto no_mem;
68359 assert( pIn3->flags & MEM_Blob );
68360 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
68361 sqlite3VdbeRecordUnpack(u.bd.pC->pKeyInfo, pIn3->n, pIn3->z, u.bd.pIdxKey);
68362 u.bd.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
68363 }
68364 rc = sqlite3BtreeMovetoUnpacked(u.bd.pC->pCursor, u.bd.pIdxKey, 0, 0, &u.bd.res);
68365 if( pOp->p4.i==0 ){
68366 sqlite3DbFree(db, u.bd.pFree);
68367 }
68368 if( rc!=SQLITE_OK ){
68369 break;
68370 }
68371 u.bd.alreadyExists = (u.bd.res==0);
68372 u.bd.pC->deferredMoveto = 0;
68373 u.bd.pC->cacheStatus = CACHE_STALE;
68374 }
68375 if( pOp->opcode==OP_Found ){
68376 if( u.bd.alreadyExists ) pc = pOp->p2 - 1;
68377 }else{
68378 if( !u.bd.alreadyExists ) pc = pOp->p2 - 1;
68379 }
68380 break;
68381 }
68382
68383 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -67087,67 +68405,67 @@
68405 ** instruction.
68406 **
68407 ** See also: NotFound, NotExists, Found
68408 */
68409 case OP_IsUnique: { /* jump, in3 */
68410 #if 0 /* local variables moved into u.be */
68411 u16 ii;
68412 VdbeCursor *pCx;
68413 BtCursor *pCrsr;
68414 u16 nField;
68415 Mem *aMx;
68416 UnpackedRecord r; /* B-Tree index search key */
68417 i64 R; /* Rowid stored in register P3 */
68418 #endif /* local variables moved into u.be */
68419
68420 pIn3 = &aMem[pOp->p3];
68421 u.be.aMx = &aMem[pOp->p4.i];
68422 /* Assert that the values of parameters P1 and P4 are in range. */
68423 assert( pOp->p4type==P4_INT32 );
68424 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
68425 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68426
68427 /* Find the index cursor. */
68428 u.be.pCx = p->apCsr[pOp->p1];
68429 assert( u.be.pCx->deferredMoveto==0 );
68430 u.be.pCx->seekResult = 0;
68431 u.be.pCx->cacheStatus = CACHE_STALE;
68432 u.be.pCrsr = u.be.pCx->pCursor;
68433
68434 /* If any of the values are NULL, take the jump. */
68435 u.be.nField = u.be.pCx->pKeyInfo->nField;
68436 for(u.be.ii=0; u.be.ii<u.be.nField; u.be.ii++){
68437 if( u.be.aMx[u.be.ii].flags & MEM_Null ){
68438 pc = pOp->p2 - 1;
68439 u.be.pCrsr = 0;
68440 break;
68441 }
68442 }
68443 assert( (u.be.aMx[u.be.nField].flags & MEM_Null)==0 );
68444
68445 if( u.be.pCrsr!=0 ){
68446 /* Populate the index search key. */
68447 u.be.r.pKeyInfo = u.be.pCx->pKeyInfo;
68448 u.be.r.nField = u.be.nField + 1;
68449 u.be.r.flags = UNPACKED_PREFIX_SEARCH;
68450 u.be.r.aMem = u.be.aMx;
68451 #ifdef SQLITE_DEBUG
68452 { int i; for(i=0; i<u.be.r.nField; i++) assert( memIsValid(&u.be.r.aMem[i]) ); }
68453 #endif
68454
68455 /* Extract the value of u.be.R from register P3. */
68456 sqlite3VdbeMemIntegerify(pIn3);
68457 u.be.R = pIn3->u.i;
68458
68459 /* Search the B-Tree index. If no conflicting record is found, jump
68460 ** to P2. Otherwise, copy the rowid of the conflicting record to
68461 ** register P3 and fall through to the next instruction. */
68462 rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, &u.be.r, 0, 0, &u.be.pCx->seekResult);
68463 if( (u.be.r.flags & UNPACKED_PREFIX_SEARCH) || u.be.r.rowid==u.be.R ){
68464 pc = pOp->p2 - 1;
68465 }else{
68466 pIn3->u.i = u.be.r.rowid;
68467 }
68468 }
68469 break;
68470 }
68471
@@ -67164,46 +68482,46 @@
68482 ** P1 is an index.
68483 **
68484 ** See also: Found, NotFound, IsUnique
68485 */
68486 case OP_NotExists: { /* jump, in3 */
68487 #if 0 /* local variables moved into u.bf */
68488 VdbeCursor *pC;
68489 BtCursor *pCrsr;
68490 int res;
68491 u64 iKey;
68492 #endif /* local variables moved into u.bf */
68493
68494 pIn3 = &aMem[pOp->p3];
68495 assert( pIn3->flags & MEM_Int );
68496 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68497 u.bf.pC = p->apCsr[pOp->p1];
68498 assert( u.bf.pC!=0 );
68499 assert( u.bf.pC->isTable );
68500 assert( u.bf.pC->pseudoTableReg==0 );
68501 u.bf.pCrsr = u.bf.pC->pCursor;
68502 if( ALWAYS(u.bf.pCrsr!=0) ){
68503 u.bf.res = 0;
68504 u.bf.iKey = pIn3->u.i;
68505 rc = sqlite3BtreeMovetoUnpacked(u.bf.pCrsr, 0, u.bf.iKey, 0, &u.bf.res);
68506 u.bf.pC->lastRowid = pIn3->u.i;
68507 u.bf.pC->rowidIsValid = u.bf.res==0 ?1:0;
68508 u.bf.pC->nullRow = 0;
68509 u.bf.pC->cacheStatus = CACHE_STALE;
68510 u.bf.pC->deferredMoveto = 0;
68511 if( u.bf.res!=0 ){
68512 pc = pOp->p2 - 1;
68513 assert( u.bf.pC->rowidIsValid==0 );
68514 }
68515 u.bf.pC->seekResult = u.bf.res;
68516 }else{
68517 /* This happens when an attempt to open a read cursor on the
68518 ** sqlite_master table returns SQLITE_EMPTY.
68519 */
68520 pc = pOp->p2 - 1;
68521 assert( u.bf.pC->rowidIsValid==0 );
68522 u.bf.pC->seekResult = 0;
68523 }
68524 break;
68525 }
68526
68527 /* Opcode: Sequence P1 P2 * * *
@@ -67234,25 +68552,25 @@
68552 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
68553 ** generated record number. This P3 mechanism is used to help implement the
68554 ** AUTOINCREMENT feature.
68555 */
68556 case OP_NewRowid: { /* out2-prerelease */
68557 #if 0 /* local variables moved into u.bg */
68558 i64 v; /* The new rowid */
68559 VdbeCursor *pC; /* Cursor of table to get the new rowid */
68560 int res; /* Result of an sqlite3BtreeLast() */
68561 int cnt; /* Counter to limit the number of searches */
68562 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
68563 VdbeFrame *pFrame; /* Root frame of VDBE */
68564 #endif /* local variables moved into u.bg */
68565
68566 u.bg.v = 0;
68567 u.bg.res = 0;
68568 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68569 u.bg.pC = p->apCsr[pOp->p1];
68570 assert( u.bg.pC!=0 );
68571 if( NEVER(u.bg.pC->pCursor==0) ){
68572 /* The zero initialization above is all that is needed */
68573 }else{
68574 /* The next rowid or record number (different terms for the same
68575 ** thing) is obtained in a two-step algorithm.
68576 **
@@ -67264,11 +68582,11 @@
68582 ** The second algorithm is to select a rowid at random and see if
68583 ** it already exists in the table. If it does not exist, we have
68584 ** succeeded. If the random rowid does exist, we select a new one
68585 ** and try again, up to 100 times.
68586 */
68587 assert( u.bg.pC->isTable );
68588
68589 #ifdef SQLITE_32BIT_ROWID
68590 # define MAX_ROWID 0x7fffffff
68591 #else
68592 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -67276,101 +68594,101 @@
68594 ** to provide the constant while making all compilers happy.
68595 */
68596 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
68597 #endif
68598
68599 if( !u.bg.pC->useRandomRowid ){
68600 u.bg.v = sqlite3BtreeGetCachedRowid(u.bg.pC->pCursor);
68601 if( u.bg.v==0 ){
68602 rc = sqlite3BtreeLast(u.bg.pC->pCursor, &u.bg.res);
68603 if( rc!=SQLITE_OK ){
68604 goto abort_due_to_error;
68605 }
68606 if( u.bg.res ){
68607 u.bg.v = 1; /* IMP: R-61914-48074 */
68608 }else{
68609 assert( sqlite3BtreeCursorIsValid(u.bg.pC->pCursor) );
68610 rc = sqlite3BtreeKeySize(u.bg.pC->pCursor, &u.bg.v);
68611 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
68612 if( u.bg.v==MAX_ROWID ){
68613 u.bg.pC->useRandomRowid = 1;
68614 }else{
68615 u.bg.v++; /* IMP: R-29538-34987 */
68616 }
68617 }
68618 }
68619
68620 #ifndef SQLITE_OMIT_AUTOINCREMENT
68621 if( pOp->p3 ){
68622 /* Assert that P3 is a valid memory cell. */
68623 assert( pOp->p3>0 );
68624 if( p->pFrame ){
68625 for(u.bg.pFrame=p->pFrame; u.bg.pFrame->pParent; u.bg.pFrame=u.bg.pFrame->pParent);
68626 /* Assert that P3 is a valid memory cell. */
68627 assert( pOp->p3<=u.bg.pFrame->nMem );
68628 u.bg.pMem = &u.bg.pFrame->aMem[pOp->p3];
68629 }else{
68630 /* Assert that P3 is a valid memory cell. */
68631 assert( pOp->p3<=p->nMem );
68632 u.bg.pMem = &aMem[pOp->p3];
68633 memAboutToChange(p, u.bg.pMem);
68634 }
68635 assert( memIsValid(u.bg.pMem) );
68636
68637 REGISTER_TRACE(pOp->p3, u.bg.pMem);
68638 sqlite3VdbeMemIntegerify(u.bg.pMem);
68639 assert( (u.bg.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
68640 if( u.bg.pMem->u.i==MAX_ROWID || u.bg.pC->useRandomRowid ){
68641 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
68642 goto abort_due_to_error;
68643 }
68644 if( u.bg.v<u.bg.pMem->u.i+1 ){
68645 u.bg.v = u.bg.pMem->u.i + 1;
68646 }
68647 u.bg.pMem->u.i = u.bg.v;
68648 }
68649 #endif
68650
68651 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, u.bg.v<MAX_ROWID ? u.bg.v+1 : 0);
68652 }
68653 if( u.bg.pC->useRandomRowid ){
68654 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
68655 ** largest possible integer (9223372036854775807) then the database
68656 ** engine starts picking positive candidate ROWIDs at random until
68657 ** it finds one that is not previously used. */
68658 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
68659 ** an AUTOINCREMENT table. */
68660 /* on the first attempt, simply do one more than previous */
68661 u.bg.v = lastRowid;
68662 u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
68663 u.bg.v++; /* ensure non-zero */
68664 u.bg.cnt = 0;
68665 while( ((rc = sqlite3BtreeMovetoUnpacked(u.bg.pC->pCursor, 0, (u64)u.bg.v,
68666 0, &u.bg.res))==SQLITE_OK)
68667 && (u.bg.res==0)
68668 && (++u.bg.cnt<100)){
68669 /* collision - try another random rowid */
68670 sqlite3_randomness(sizeof(u.bg.v), &u.bg.v);
68671 if( u.bg.cnt<5 ){
68672 /* try "small" random rowids for the initial attempts */
68673 u.bg.v &= 0xffffff;
68674 }else{
68675 u.bg.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
68676 }
68677 u.bg.v++; /* ensure non-zero */
68678 }
68679 if( rc==SQLITE_OK && u.bg.res==0 ){
68680 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
68681 goto abort_due_to_error;
68682 }
68683 assert( u.bg.v>0 ); /* EV: R-40812-03570 */
68684 }
68685 u.bg.pC->rowidIsValid = 0;
68686 u.bg.pC->deferredMoveto = 0;
68687 u.bg.pC->cacheStatus = CACHE_STALE;
68688 }
68689 pOut->u.i = u.bg.v;
68690 break;
68691 }
68692
68693 /* Opcode: Insert P1 P2 P3 P4 P5
68694 **
@@ -67416,74 +68734,74 @@
68734 ** This works exactly like OP_Insert except that the key is the
68735 ** integer value P3, not the value of the integer stored in register P3.
68736 */
68737 case OP_Insert:
68738 case OP_InsertInt: {
68739 #if 0 /* local variables moved into u.bh */
68740 Mem *pData; /* MEM cell holding data for the record to be inserted */
68741 Mem *pKey; /* MEM cell holding key for the record */
68742 i64 iKey; /* The integer ROWID or key for the record to be inserted */
68743 VdbeCursor *pC; /* Cursor to table into which insert is written */
68744 int nZero; /* Number of zero-bytes to append */
68745 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
68746 const char *zDb; /* database name - used by the update hook */
68747 const char *zTbl; /* Table name - used by the opdate hook */
68748 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
68749 #endif /* local variables moved into u.bh */
68750
68751 u.bh.pData = &aMem[pOp->p2];
68752 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68753 assert( memIsValid(u.bh.pData) );
68754 u.bh.pC = p->apCsr[pOp->p1];
68755 assert( u.bh.pC!=0 );
68756 assert( u.bh.pC->pCursor!=0 );
68757 assert( u.bh.pC->pseudoTableReg==0 );
68758 assert( u.bh.pC->isTable );
68759 REGISTER_TRACE(pOp->p2, u.bh.pData);
68760
68761 if( pOp->opcode==OP_Insert ){
68762 u.bh.pKey = &aMem[pOp->p3];
68763 assert( u.bh.pKey->flags & MEM_Int );
68764 assert( memIsValid(u.bh.pKey) );
68765 REGISTER_TRACE(pOp->p3, u.bh.pKey);
68766 u.bh.iKey = u.bh.pKey->u.i;
68767 }else{
68768 assert( pOp->opcode==OP_InsertInt );
68769 u.bh.iKey = pOp->p3;
68770 }
68771
68772 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
68773 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bh.iKey;
68774 if( u.bh.pData->flags & MEM_Null ){
68775 u.bh.pData->z = 0;
68776 u.bh.pData->n = 0;
68777 }else{
68778 assert( u.bh.pData->flags & (MEM_Blob|MEM_Str) );
68779 }
68780 u.bh.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bh.pC->seekResult : 0);
68781 if( u.bh.pData->flags & MEM_Zero ){
68782 u.bh.nZero = u.bh.pData->u.nZero;
68783 }else{
68784 u.bh.nZero = 0;
68785 }
68786 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
68787 rc = sqlite3BtreeInsert(u.bh.pC->pCursor, 0, u.bh.iKey,
68788 u.bh.pData->z, u.bh.pData->n, u.bh.nZero,
68789 pOp->p5 & OPFLAG_APPEND, u.bh.seekResult
68790 );
68791 u.bh.pC->rowidIsValid = 0;
68792 u.bh.pC->deferredMoveto = 0;
68793 u.bh.pC->cacheStatus = CACHE_STALE;
68794
68795 /* Invoke the update-hook if required. */
68796 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
68797 u.bh.zDb = db->aDb[u.bh.pC->iDb].zName;
68798 u.bh.zTbl = pOp->p4.z;
68799 u.bh.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
68800 assert( u.bh.pC->isTable );
68801 db->xUpdateCallback(db->pUpdateArg, u.bh.op, u.bh.zDb, u.bh.zTbl, u.bh.iKey);
68802 assert( u.bh.pC->iDb>=0 );
68803 }
68804 break;
68805 }
68806
68807 /* Opcode: Delete P1 P2 * P4 *
@@ -67505,51 +68823,51 @@
68823 ** pointing to. The update hook will be invoked, if it exists.
68824 ** If P4 is not NULL then the P1 cursor must have been positioned
68825 ** using OP_NotFound prior to invoking this opcode.
68826 */
68827 case OP_Delete: {
68828 #if 0 /* local variables moved into u.bi */
68829 i64 iKey;
68830 VdbeCursor *pC;
68831 #endif /* local variables moved into u.bi */
68832
68833 u.bi.iKey = 0;
68834 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68835 u.bi.pC = p->apCsr[pOp->p1];
68836 assert( u.bi.pC!=0 );
68837 assert( u.bi.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
68838
68839 /* If the update-hook will be invoked, set u.bi.iKey to the rowid of the
68840 ** row being deleted.
68841 */
68842 if( db->xUpdateCallback && pOp->p4.z ){
68843 assert( u.bi.pC->isTable );
68844 assert( u.bi.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
68845 u.bi.iKey = u.bi.pC->lastRowid;
68846 }
68847
68848 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
68849 ** OP_Column on the same table without any intervening operations that
68850 ** might move or invalidate the cursor. Hence cursor u.bi.pC is always pointing
68851 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
68852 ** below is always a no-op and cannot fail. We will run it anyhow, though,
68853 ** to guard against future changes to the code generator.
68854 **/
68855 assert( u.bi.pC->deferredMoveto==0 );
68856 rc = sqlite3VdbeCursorMoveto(u.bi.pC);
68857 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
68858
68859 sqlite3BtreeSetCachedRowid(u.bi.pC->pCursor, 0);
68860 rc = sqlite3BtreeDelete(u.bi.pC->pCursor);
68861 u.bi.pC->cacheStatus = CACHE_STALE;
68862
68863 /* Invoke the update-hook if required. */
68864 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
68865 const char *zDb = db->aDb[u.bi.pC->iDb].zName;
68866 const char *zTbl = pOp->p4.z;
68867 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bi.iKey);
68868 assert( u.bi.pC->iDb>=0 );
68869 }
68870 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
68871 break;
68872 }
68873 /* Opcode: ResetCount * * * * *
@@ -67571,20 +68889,20 @@
68889 ** register P3 with the entry that the sorter cursor currently points to.
68890 ** If, excluding the rowid fields at the end, the two records are a match,
68891 ** fall through to the next instruction. Otherwise, jump to instruction P2.
68892 */
68893 case OP_SorterCompare: {
68894 #if 0 /* local variables moved into u.bj */
68895 VdbeCursor *pC;
68896 int res;
68897 #endif /* local variables moved into u.bj */
68898
68899 u.bj.pC = p->apCsr[pOp->p1];
68900 assert( isSorter(u.bj.pC) );
68901 pIn3 = &aMem[pOp->p3];
68902 rc = sqlite3VdbeSorterCompare(u.bj.pC, pIn3, &u.bj.res);
68903 if( u.bj.res ){
68904 pc = pOp->p2-1;
68905 }
68906 break;
68907 };
68908
@@ -67591,18 +68909,18 @@
68909 /* Opcode: SorterData P1 P2 * * *
68910 **
68911 ** Write into register P2 the current sorter data for sorter cursor P1.
68912 */
68913 case OP_SorterData: {
68914 #if 0 /* local variables moved into u.bk */
68915 VdbeCursor *pC;
68916 #endif /* local variables moved into u.bk */
68917 #ifndef SQLITE_OMIT_MERGE_SORT
68918 pOut = &aMem[pOp->p2];
68919 u.bk.pC = p->apCsr[pOp->p1];
68920 assert( u.bk.pC->isSorter );
68921 rc = sqlite3VdbeSorterRowkey(u.bk.pC, pOut);
68922 #else
68923 pOp->opcode = OP_RowKey;
68924 pc--;
68925 #endif
68926 break;
@@ -67628,67 +68946,67 @@
68946 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
68947 ** of a real table, not a pseudo-table.
68948 */
68949 case OP_RowKey:
68950 case OP_RowData: {
68951 #if 0 /* local variables moved into u.bl */
68952 VdbeCursor *pC;
68953 BtCursor *pCrsr;
68954 u32 n;
68955 i64 n64;
68956 #endif /* local variables moved into u.bl */
68957
68958 pOut = &aMem[pOp->p2];
68959 memAboutToChange(p, pOut);
68960
68961 /* Note that RowKey and RowData are really exactly the same instruction */
68962 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68963 u.bl.pC = p->apCsr[pOp->p1];
68964 assert( u.bl.pC->isSorter==0 );
68965 assert( u.bl.pC->isTable || pOp->opcode!=OP_RowData );
68966 assert( u.bl.pC->isIndex || pOp->opcode==OP_RowData );
68967 assert( u.bl.pC!=0 );
68968 assert( u.bl.pC->nullRow==0 );
68969 assert( u.bl.pC->pseudoTableReg==0 );
68970 assert( !u.bl.pC->isSorter );
68971 assert( u.bl.pC->pCursor!=0 );
68972 u.bl.pCrsr = u.bl.pC->pCursor;
68973 assert( sqlite3BtreeCursorIsValid(u.bl.pCrsr) );
68974
68975 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
68976 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
68977 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
68978 ** a no-op and can never fail. But we leave it in place as a safety.
68979 */
68980 assert( u.bl.pC->deferredMoveto==0 );
68981 rc = sqlite3VdbeCursorMoveto(u.bl.pC);
68982 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
68983
68984 if( u.bl.pC->isIndex ){
68985 assert( !u.bl.pC->isTable );
68986 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bl.pCrsr, &u.bl.n64);
68987 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
68988 if( u.bl.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
68989 goto too_big;
68990 }
68991 u.bl.n = (u32)u.bl.n64;
68992 }else{
68993 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bl.pCrsr, &u.bl.n);
68994 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
68995 if( u.bl.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
68996 goto too_big;
68997 }
68998 }
68999 if( sqlite3VdbeMemGrow(pOut, u.bl.n, 0) ){
69000 goto no_mem;
69001 }
69002 pOut->n = u.bl.n;
69003 MemSetTypeFlag(pOut, MEM_Blob);
69004 if( u.bl.pC->isIndex ){
69005 rc = sqlite3BtreeKey(u.bl.pCrsr, 0, u.bl.n, pOut->z);
69006 }else{
69007 rc = sqlite3BtreeData(u.bl.pCrsr, 0, u.bl.n, pOut->z);
69008 }
69009 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
69010 UPDATE_MAX_BLOBSIZE(pOut);
69011 break;
69012 }
@@ -67701,46 +69019,46 @@
69019 ** P1 can be either an ordinary table or a virtual table. There used to
69020 ** be a separate OP_VRowid opcode for use with virtual tables, but this
69021 ** one opcode now works for both table types.
69022 */
69023 case OP_Rowid: { /* out2-prerelease */
69024 #if 0 /* local variables moved into u.bm */
69025 VdbeCursor *pC;
69026 i64 v;
69027 sqlite3_vtab *pVtab;
69028 const sqlite3_module *pModule;
69029 #endif /* local variables moved into u.bm */
69030
69031 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69032 u.bm.pC = p->apCsr[pOp->p1];
69033 assert( u.bm.pC!=0 );
69034 assert( u.bm.pC->pseudoTableReg==0 );
69035 if( u.bm.pC->nullRow ){
69036 pOut->flags = MEM_Null;
69037 break;
69038 }else if( u.bm.pC->deferredMoveto ){
69039 u.bm.v = u.bm.pC->movetoTarget;
69040 #ifndef SQLITE_OMIT_VIRTUALTABLE
69041 }else if( u.bm.pC->pVtabCursor ){
69042 u.bm.pVtab = u.bm.pC->pVtabCursor->pVtab;
69043 u.bm.pModule = u.bm.pVtab->pModule;
69044 assert( u.bm.pModule->xRowid );
69045 rc = u.bm.pModule->xRowid(u.bm.pC->pVtabCursor, &u.bm.v);
69046 importVtabErrMsg(p, u.bm.pVtab);
69047 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69048 }else{
69049 assert( u.bm.pC->pCursor!=0 );
69050 rc = sqlite3VdbeCursorMoveto(u.bm.pC);
69051 if( rc ) goto abort_due_to_error;
69052 if( u.bm.pC->rowidIsValid ){
69053 u.bm.v = u.bm.pC->lastRowid;
69054 }else{
69055 rc = sqlite3BtreeKeySize(u.bm.pC->pCursor, &u.bm.v);
69056 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
69057 }
69058 }
69059 pOut->u.i = u.bm.v;
69060 break;
69061 }
69062
69063 /* Opcode: NullRow P1 * * * *
69064 **
@@ -67747,22 +69065,22 @@
69065 ** Move the cursor P1 to a null row. Any OP_Column operations
69066 ** that occur while the cursor is on the null row will always
69067 ** write a NULL.
69068 */
69069 case OP_NullRow: {
69070 #if 0 /* local variables moved into u.bn */
69071 VdbeCursor *pC;
69072 #endif /* local variables moved into u.bn */
69073
69074 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69075 u.bn.pC = p->apCsr[pOp->p1];
69076 assert( u.bn.pC!=0 );
69077 u.bn.pC->nullRow = 1;
69078 u.bn.pC->rowidIsValid = 0;
69079 assert( u.bn.pC->pCursor || u.bn.pC->pVtabCursor );
69080 if( u.bn.pC->pCursor ){
69081 sqlite3BtreeClearCursor(u.bn.pC->pCursor);
69082 }
69083 break;
69084 }
69085
69086 /* Opcode: Last P1 P2 * * *
@@ -67772,29 +69090,29 @@
69090 ** If the table or index is empty and P2>0, then jump immediately to P2.
69091 ** If P2 is 0 or if the table or index is not empty, fall through
69092 ** to the following instruction.
69093 */
69094 case OP_Last: { /* jump */
69095 #if 0 /* local variables moved into u.bo */
69096 VdbeCursor *pC;
69097 BtCursor *pCrsr;
69098 int res;
69099 #endif /* local variables moved into u.bo */
69100
69101 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69102 u.bo.pC = p->apCsr[pOp->p1];
69103 assert( u.bo.pC!=0 );
69104 u.bo.pCrsr = u.bo.pC->pCursor;
69105 u.bo.res = 0;
69106 if( ALWAYS(u.bo.pCrsr!=0) ){
69107 rc = sqlite3BtreeLast(u.bo.pCrsr, &u.bo.res);
69108 }
69109 u.bo.pC->nullRow = (u8)u.bo.res;
69110 u.bo.pC->deferredMoveto = 0;
69111 u.bo.pC->rowidIsValid = 0;
69112 u.bo.pC->cacheStatus = CACHE_STALE;
69113 if( pOp->p2>0 && u.bo.res ){
69114 pc = pOp->p2 - 1;
69115 }
69116 break;
69117 }
69118
@@ -67830,35 +69148,35 @@
69148 ** If the table or index is empty and P2>0, then jump immediately to P2.
69149 ** If P2 is 0 or if the table or index is not empty, fall through
69150 ** to the following instruction.
69151 */
69152 case OP_Rewind: { /* jump */
69153 #if 0 /* local variables moved into u.bp */
69154 VdbeCursor *pC;
69155 BtCursor *pCrsr;
69156 int res;
69157 #endif /* local variables moved into u.bp */
69158
69159 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69160 u.bp.pC = p->apCsr[pOp->p1];
69161 assert( u.bp.pC!=0 );
69162 assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterSort) );
69163 u.bp.res = 1;
69164 if( isSorter(u.bp.pC) ){
69165 rc = sqlite3VdbeSorterRewind(db, u.bp.pC, &u.bp.res);
69166 }else{
69167 u.bp.pCrsr = u.bp.pC->pCursor;
69168 assert( u.bp.pCrsr );
69169 rc = sqlite3BtreeFirst(u.bp.pCrsr, &u.bp.res);
69170 u.bp.pC->atFirst = u.bp.res==0 ?1:0;
69171 u.bp.pC->deferredMoveto = 0;
69172 u.bp.pC->cacheStatus = CACHE_STALE;
69173 u.bp.pC->rowidIsValid = 0;
69174 }
69175 u.bp.pC->nullRow = (u8)u.bp.res;
69176 assert( pOp->p2>0 && pOp->p2<p->nOp );
69177 if( u.bp.res ){
69178 pc = pOp->p2 - 1;
69179 }
69180 break;
69181 }
69182
@@ -67898,44 +69216,44 @@
69216 #ifdef SQLITE_OMIT_MERGE_SORT
69217 pOp->opcode = OP_Next;
69218 #endif
69219 case OP_Prev: /* jump */
69220 case OP_Next: { /* jump */
69221 #if 0 /* local variables moved into u.bq */
69222 VdbeCursor *pC;
69223 int res;
69224 #endif /* local variables moved into u.bq */
69225
69226 CHECK_FOR_INTERRUPT;
69227 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69228 assert( pOp->p5<=ArraySize(p->aCounter) );
69229 u.bq.pC = p->apCsr[pOp->p1];
69230 if( u.bq.pC==0 ){
69231 break; /* See ticket #2273 */
69232 }
69233 assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterNext) );
69234 if( isSorter(u.bq.pC) ){
69235 assert( pOp->opcode==OP_SorterNext );
69236 rc = sqlite3VdbeSorterNext(db, u.bq.pC, &u.bq.res);
69237 }else{
69238 u.bq.res = 1;
69239 assert( u.bq.pC->deferredMoveto==0 );
69240 assert( u.bq.pC->pCursor );
69241 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
69242 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
69243 rc = pOp->p4.xAdvance(u.bq.pC->pCursor, &u.bq.res);
69244 }
69245 u.bq.pC->nullRow = (u8)u.bq.res;
69246 u.bq.pC->cacheStatus = CACHE_STALE;
69247 if( u.bq.res==0 ){
69248 pc = pOp->p2 - 1;
69249 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
69250 #ifdef SQLITE_TEST
69251 sqlite3_search_count++;
69252 #endif
69253 }
69254 u.bq.pC->rowidIsValid = 0;
69255 break;
69256 }
69257
69258 /* Opcode: IdxInsert P1 P2 P3 * P5
69259 **
@@ -67952,38 +69270,38 @@
69270 case OP_SorterInsert: /* in2 */
69271 #ifdef SQLITE_OMIT_MERGE_SORT
69272 pOp->opcode = OP_IdxInsert;
69273 #endif
69274 case OP_IdxInsert: { /* in2 */
69275 #if 0 /* local variables moved into u.br */
69276 VdbeCursor *pC;
69277 BtCursor *pCrsr;
69278 int nKey;
69279 const char *zKey;
69280 #endif /* local variables moved into u.br */
69281
69282 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69283 u.br.pC = p->apCsr[pOp->p1];
69284 assert( u.br.pC!=0 );
69285 assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
69286 pIn2 = &aMem[pOp->p2];
69287 assert( pIn2->flags & MEM_Blob );
69288 u.br.pCrsr = u.br.pC->pCursor;
69289 if( ALWAYS(u.br.pCrsr!=0) ){
69290 assert( u.br.pC->isTable==0 );
69291 rc = ExpandBlob(pIn2);
69292 if( rc==SQLITE_OK ){
69293 if( isSorter(u.br.pC) ){
69294 rc = sqlite3VdbeSorterWrite(db, u.br.pC, pIn2);
69295 }else{
69296 u.br.nKey = pIn2->n;
69297 u.br.zKey = pIn2->z;
69298 rc = sqlite3BtreeInsert(u.br.pCrsr, u.br.zKey, u.br.nKey, "", 0, 0, pOp->p3,
69299 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.br.pC->seekResult : 0)
69300 );
69301 assert( u.br.pC->deferredMoveto==0 );
69302 u.br.pC->cacheStatus = CACHE_STALE;
69303 }
69304 }
69305 }
69306 break;
69307 }
@@ -67993,37 +69311,37 @@
69311 ** The content of P3 registers starting at register P2 form
69312 ** an unpacked index key. This opcode removes that entry from the
69313 ** index opened by cursor P1.
69314 */
69315 case OP_IdxDelete: {
69316 #if 0 /* local variables moved into u.bs */
69317 VdbeCursor *pC;
69318 BtCursor *pCrsr;
69319 int res;
69320 UnpackedRecord r;
69321 #endif /* local variables moved into u.bs */
69322
69323 assert( pOp->p3>0 );
69324 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
69325 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69326 u.bs.pC = p->apCsr[pOp->p1];
69327 assert( u.bs.pC!=0 );
69328 u.bs.pCrsr = u.bs.pC->pCursor;
69329 if( ALWAYS(u.bs.pCrsr!=0) ){
69330 u.bs.r.pKeyInfo = u.bs.pC->pKeyInfo;
69331 u.bs.r.nField = (u16)pOp->p3;
69332 u.bs.r.flags = 0;
69333 u.bs.r.aMem = &aMem[pOp->p2];
69334 #ifdef SQLITE_DEBUG
69335 { int i; for(i=0; i<u.bs.r.nField; i++) assert( memIsValid(&u.bs.r.aMem[i]) ); }
69336 #endif
69337 rc = sqlite3BtreeMovetoUnpacked(u.bs.pCrsr, &u.bs.r, 0, 0, &u.bs.res);
69338 if( rc==SQLITE_OK && u.bs.res==0 ){
69339 rc = sqlite3BtreeDelete(u.bs.pCrsr);
69340 }
69341 assert( u.bs.pC->deferredMoveto==0 );
69342 u.bs.pC->cacheStatus = CACHE_STALE;
69343 }
69344 break;
69345 }
69346
69347 /* Opcode: IdxRowid P1 P2 * * *
@@ -68033,32 +69351,32 @@
69351 ** the rowid of the table entry to which this index entry points.
69352 **
69353 ** See also: Rowid, MakeRecord.
69354 */
69355 case OP_IdxRowid: { /* out2-prerelease */
69356 #if 0 /* local variables moved into u.bt */
69357 BtCursor *pCrsr;
69358 VdbeCursor *pC;
69359 i64 rowid;
69360 #endif /* local variables moved into u.bt */
69361
69362 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69363 u.bt.pC = p->apCsr[pOp->p1];
69364 assert( u.bt.pC!=0 );
69365 u.bt.pCrsr = u.bt.pC->pCursor;
69366 pOut->flags = MEM_Null;
69367 if( ALWAYS(u.bt.pCrsr!=0) ){
69368 rc = sqlite3VdbeCursorMoveto(u.bt.pC);
69369 if( NEVER(rc) ) goto abort_due_to_error;
69370 assert( u.bt.pC->deferredMoveto==0 );
69371 assert( u.bt.pC->isTable==0 );
69372 if( !u.bt.pC->nullRow ){
69373 rc = sqlite3VdbeIdxRowid(db, u.bt.pCrsr, &u.bt.rowid);
69374 if( rc!=SQLITE_OK ){
69375 goto abort_due_to_error;
69376 }
69377 pOut->u.i = u.bt.rowid;
69378 pOut->flags = MEM_Int;
69379 }
69380 }
69381 break;
69382 }
@@ -68089,43 +69407,43 @@
69407 ** If P5 is non-zero then the key value is increased by an epsilon prior
69408 ** to the comparison. This makes the opcode work like IdxLE.
69409 */
69410 case OP_IdxLT: /* jump */
69411 case OP_IdxGE: { /* jump */
69412 #if 0 /* local variables moved into u.bu */
69413 VdbeCursor *pC;
69414 int res;
69415 UnpackedRecord r;
69416 #endif /* local variables moved into u.bu */
69417
69418 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69419 u.bu.pC = p->apCsr[pOp->p1];
69420 assert( u.bu.pC!=0 );
69421 assert( u.bu.pC->isOrdered );
69422 if( ALWAYS(u.bu.pC->pCursor!=0) ){
69423 assert( u.bu.pC->deferredMoveto==0 );
69424 assert( pOp->p5==0 || pOp->p5==1 );
69425 assert( pOp->p4type==P4_INT32 );
69426 u.bu.r.pKeyInfo = u.bu.pC->pKeyInfo;
69427 u.bu.r.nField = (u16)pOp->p4.i;
69428 if( pOp->p5 ){
69429 u.bu.r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
69430 }else{
69431 u.bu.r.flags = UNPACKED_PREFIX_MATCH;
69432 }
69433 u.bu.r.aMem = &aMem[pOp->p3];
69434 #ifdef SQLITE_DEBUG
69435 { int i; for(i=0; i<u.bu.r.nField; i++) assert( memIsValid(&u.bu.r.aMem[i]) ); }
69436 #endif
69437 rc = sqlite3VdbeIdxKeyCompare(u.bu.pC, &u.bu.r, &u.bu.res);
69438 if( pOp->opcode==OP_IdxLT ){
69439 u.bu.res = -u.bu.res;
69440 }else{
69441 assert( pOp->opcode==OP_IdxGE );
69442 u.bu.res++;
69443 }
69444 if( u.bu.res>0 ){
69445 pc = pOp->p2 - 1 ;
69446 }
69447 }
69448 break;
69449 }
@@ -68149,43 +69467,43 @@
69467 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
69468 **
69469 ** See also: Clear
69470 */
69471 case OP_Destroy: { /* out2-prerelease */
69472 #if 0 /* local variables moved into u.bv */
69473 int iMoved;
69474 int iCnt;
69475 Vdbe *pVdbe;
69476 int iDb;
69477 #endif /* local variables moved into u.bv */
69478 #ifndef SQLITE_OMIT_VIRTUALTABLE
69479 u.bv.iCnt = 0;
69480 for(u.bv.pVdbe=db->pVdbe; u.bv.pVdbe; u.bv.pVdbe = u.bv.pVdbe->pNext){
69481 if( u.bv.pVdbe->magic==VDBE_MAGIC_RUN && u.bv.pVdbe->inVtabMethod<2 && u.bv.pVdbe->pc>=0 ){
69482 u.bv.iCnt++;
69483 }
69484 }
69485 #else
69486 u.bv.iCnt = db->activeVdbeCnt;
69487 #endif
69488 pOut->flags = MEM_Null;
69489 if( u.bv.iCnt>1 ){
69490 rc = SQLITE_LOCKED;
69491 p->errorAction = OE_Abort;
69492 }else{
69493 u.bv.iDb = pOp->p3;
69494 assert( u.bv.iCnt==1 );
69495 assert( (p->btreeMask & (((yDbMask)1)<<u.bv.iDb))!=0 );
69496 rc = sqlite3BtreeDropTable(db->aDb[u.bv.iDb].pBt, pOp->p1, &u.bv.iMoved);
69497 pOut->flags = MEM_Int;
69498 pOut->u.i = u.bv.iMoved;
69499 #ifndef SQLITE_OMIT_AUTOVACUUM
69500 if( rc==SQLITE_OK && u.bv.iMoved!=0 ){
69501 sqlite3RootPageMoved(db, u.bv.iDb, u.bv.iMoved, pOp->p1);
69502 /* All OP_Destroy operations occur on the same btree */
69503 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bv.iDb+1 );
69504 resetSchemaOnFault = u.bv.iDb+1;
69505 }
69506 #endif
69507 }
69508 break;
69509 }
@@ -68207,25 +69525,25 @@
69525 ** also incremented by the number of rows in the table being cleared.
69526 **
69527 ** See also: Destroy
69528 */
69529 case OP_Clear: {
69530 #if 0 /* local variables moved into u.bw */
69531 int nChange;
69532 #endif /* local variables moved into u.bw */
69533
69534 u.bw.nChange = 0;
69535 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
69536 rc = sqlite3BtreeClearTable(
69537 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bw.nChange : 0)
69538 );
69539 if( pOp->p3 ){
69540 p->nChange += u.bw.nChange;
69541 if( pOp->p3>0 ){
69542 assert( memIsValid(&aMem[pOp->p3]) );
69543 memAboutToChange(p, &aMem[pOp->p3]);
69544 aMem[pOp->p3].u.i += u.bw.nChange;
69545 }
69546 }
69547 break;
69548 }
69549
@@ -68251,29 +69569,29 @@
69569 **
69570 ** See documentation on OP_CreateTable for additional information.
69571 */
69572 case OP_CreateIndex: /* out2-prerelease */
69573 case OP_CreateTable: { /* out2-prerelease */
69574 #if 0 /* local variables moved into u.bx */
69575 int pgno;
69576 int flags;
69577 Db *pDb;
69578 #endif /* local variables moved into u.bx */
69579
69580 u.bx.pgno = 0;
69581 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69582 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69583 u.bx.pDb = &db->aDb[pOp->p1];
69584 assert( u.bx.pDb->pBt!=0 );
69585 if( pOp->opcode==OP_CreateTable ){
69586 /* u.bx.flags = BTREE_INTKEY; */
69587 u.bx.flags = BTREE_INTKEY;
69588 }else{
69589 u.bx.flags = BTREE_BLOBKEY;
69590 }
69591 rc = sqlite3BtreeCreateTable(u.bx.pDb->pBt, &u.bx.pgno, u.bx.flags);
69592 pOut->u.i = u.bx.pgno;
69593 break;
69594 }
69595
69596 /* Opcode: ParseSchema P1 * * P4 *
69597 **
@@ -68282,48 +69600,48 @@
69600 **
69601 ** This opcode invokes the parser to create a new virtual machine,
69602 ** then runs the new virtual machine. It is thus a re-entrant opcode.
69603 */
69604 case OP_ParseSchema: {
69605 #if 0 /* local variables moved into u.by */
69606 int iDb;
69607 const char *zMaster;
69608 char *zSql;
69609 InitData initData;
69610 #endif /* local variables moved into u.by */
69611
69612 /* Any prepared statement that invokes this opcode will hold mutexes
69613 ** on every btree. This is a prerequisite for invoking
69614 ** sqlite3InitCallback().
69615 */
69616 #ifdef SQLITE_DEBUG
69617 for(u.by.iDb=0; u.by.iDb<db->nDb; u.by.iDb++){
69618 assert( u.by.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.by.iDb].pBt) );
69619 }
69620 #endif
69621
69622 u.by.iDb = pOp->p1;
69623 assert( u.by.iDb>=0 && u.by.iDb<db->nDb );
69624 assert( DbHasProperty(db, u.by.iDb, DB_SchemaLoaded) );
69625 /* Used to be a conditional */ {
69626 u.by.zMaster = SCHEMA_TABLE(u.by.iDb);
69627 u.by.initData.db = db;
69628 u.by.initData.iDb = pOp->p1;
69629 u.by.initData.pzErrMsg = &p->zErrMsg;
69630 u.by.zSql = sqlite3MPrintf(db,
69631 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
69632 db->aDb[u.by.iDb].zName, u.by.zMaster, pOp->p4.z);
69633 if( u.by.zSql==0 ){
69634 rc = SQLITE_NOMEM;
69635 }else{
69636 assert( db->init.busy==0 );
69637 db->init.busy = 1;
69638 u.by.initData.rc = SQLITE_OK;
69639 assert( !db->mallocFailed );
69640 rc = sqlite3_exec(db, u.by.zSql, sqlite3InitCallback, &u.by.initData, 0);
69641 if( rc==SQLITE_OK ) rc = u.by.initData.rc;
69642 sqlite3DbFree(db, u.by.zSql);
69643 db->init.busy = 0;
69644 }
69645 }
69646 if( rc==SQLITE_NOMEM ){
69647 goto no_mem;
@@ -68402,45 +69720,45 @@
69720 ** file, not the main database file.
69721 **
69722 ** This opcode is used to implement the integrity_check pragma.
69723 */
69724 case OP_IntegrityCk: {
69725 #if 0 /* local variables moved into u.bz */
69726 int nRoot; /* Number of tables to check. (Number of root pages.) */
69727 int *aRoot; /* Array of rootpage numbers for tables to be checked */
69728 int j; /* Loop counter */
69729 int nErr; /* Number of errors reported */
69730 char *z; /* Text of the error report */
69731 Mem *pnErr; /* Register keeping track of errors remaining */
69732 #endif /* local variables moved into u.bz */
69733
69734 u.bz.nRoot = pOp->p2;
69735 assert( u.bz.nRoot>0 );
69736 u.bz.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bz.nRoot+1) );
69737 if( u.bz.aRoot==0 ) goto no_mem;
69738 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69739 u.bz.pnErr = &aMem[pOp->p3];
69740 assert( (u.bz.pnErr->flags & MEM_Int)!=0 );
69741 assert( (u.bz.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
69742 pIn1 = &aMem[pOp->p1];
69743 for(u.bz.j=0; u.bz.j<u.bz.nRoot; u.bz.j++){
69744 u.bz.aRoot[u.bz.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bz.j]);
69745 }
69746 u.bz.aRoot[u.bz.j] = 0;
69747 assert( pOp->p5<db->nDb );
69748 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
69749 u.bz.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bz.aRoot, u.bz.nRoot,
69750 (int)u.bz.pnErr->u.i, &u.bz.nErr);
69751 sqlite3DbFree(db, u.bz.aRoot);
69752 u.bz.pnErr->u.i -= u.bz.nErr;
69753 sqlite3VdbeMemSetNull(pIn1);
69754 if( u.bz.nErr==0 ){
69755 assert( u.bz.z==0 );
69756 }else if( u.bz.z==0 ){
69757 goto no_mem;
69758 }else{
69759 sqlite3VdbeMemSetStr(pIn1, u.bz.z, -1, SQLITE_UTF8, sqlite3_free);
69760 }
69761 UPDATE_MAX_BLOBSIZE(pIn1);
69762 sqlite3VdbeChangeEncoding(pIn1, encoding);
69763 break;
69764 }
@@ -68470,24 +69788,24 @@
69788 ** Extract the smallest value from boolean index P1 and put that value into
69789 ** register P3. Or, if boolean index P1 is initially empty, leave P3
69790 ** unchanged and jump to instruction P2.
69791 */
69792 case OP_RowSetRead: { /* jump, in1, out3 */
69793 #if 0 /* local variables moved into u.ca */
69794 i64 val;
69795 #endif /* local variables moved into u.ca */
69796 CHECK_FOR_INTERRUPT;
69797 pIn1 = &aMem[pOp->p1];
69798 if( (pIn1->flags & MEM_RowSet)==0
69799 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.ca.val)==0
69800 ){
69801 /* The boolean index is empty */
69802 sqlite3VdbeMemSetNull(pIn1);
69803 pc = pOp->p2 - 1;
69804 }else{
69805 /* A value was pulled from the index */
69806 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.ca.val);
69807 }
69808 break;
69809 }
69810
69811 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -68512,18 +69830,18 @@
69830 ** inserted, there is no need to search to see if the same value was
69831 ** previously inserted as part of set X (only if it was previously
69832 ** inserted as part of some other set).
69833 */
69834 case OP_RowSetTest: { /* jump, in1, in3 */
69835 #if 0 /* local variables moved into u.cb */
69836 int iSet;
69837 int exists;
69838 #endif /* local variables moved into u.cb */
69839
69840 pIn1 = &aMem[pOp->p1];
69841 pIn3 = &aMem[pOp->p3];
69842 u.cb.iSet = pOp->p4.i;
69843 assert( pIn3->flags&MEM_Int );
69844
69845 /* If there is anything other than a rowset object in memory cell P1,
69846 ** delete it now and initialize P1 with an empty rowset
69847 */
@@ -68531,21 +69849,21 @@
69849 sqlite3VdbeMemSetRowSet(pIn1);
69850 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
69851 }
69852
69853 assert( pOp->p4type==P4_INT32 );
69854 assert( u.cb.iSet==-1 || u.cb.iSet>=0 );
69855 if( u.cb.iSet ){
69856 u.cb.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
69857 (u8)(u.cb.iSet>=0 ? u.cb.iSet & 0xf : 0xff),
69858 pIn3->u.i);
69859 if( u.cb.exists ){
69860 pc = pOp->p2 - 1;
69861 break;
69862 }
69863 }
69864 if( u.cb.iSet>=0 ){
69865 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
69866 }
69867 break;
69868 }
69869
@@ -68564,25 +69882,24 @@
69882 ** memory required by the sub-vdbe at runtime.
69883 **
69884 ** P4 is a pointer to the VM containing the trigger program.
69885 */
69886 case OP_Program: { /* jump */
69887 #if 0 /* local variables moved into u.cc */
69888 int nMem; /* Number of memory registers for sub-program */
69889 int nByte; /* Bytes of runtime space required for sub-program */
69890 Mem *pRt; /* Register to allocate runtime space */
69891 Mem *pMem; /* Used to iterate through memory cells */
69892 Mem *pEnd; /* Last memory cell in new array */
69893 VdbeFrame *pFrame; /* New vdbe frame to execute in */
69894 SubProgram *pProgram; /* Sub-program to execute */
69895 void *t; /* Token identifying trigger */
69896 #endif /* local variables moved into u.cc */
69897
69898 u.cc.pProgram = pOp->p4.pProgram;
69899 u.cc.pRt = &aMem[pOp->p3];
69900 assert( u.cc.pProgram->nOp>0 );
 
69901
69902 /* If the p5 flag is clear, then recursive invocation of triggers is
69903 ** disabled for backwards compatibility (p5 is set if this sub-program
69904 ** is really a trigger, not a foreign key action, and the flag set
69905 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -68592,80 +69909,87 @@
69909 ** SubProgram (if the trigger may be executed with more than one different
69910 ** ON CONFLICT algorithm). SubProgram structures associated with a
69911 ** single trigger all have the same value for the SubProgram.token
69912 ** variable. */
69913 if( pOp->p5 ){
69914 u.cc.t = u.cc.pProgram->token;
69915 for(u.cc.pFrame=p->pFrame; u.cc.pFrame && u.cc.pFrame->token!=u.cc.t; u.cc.pFrame=u.cc.pFrame->pParent);
69916 if( u.cc.pFrame ) break;
69917 }
69918
69919 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
69920 rc = SQLITE_ERROR;
69921 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
69922 break;
69923 }
69924
69925 /* Register u.cc.pRt is used to store the memory required to save the state
69926 ** of the current program, and the memory required at runtime to execute
69927 ** the trigger program. If this trigger has been fired before, then u.cc.pRt
69928 ** is already allocated. Otherwise, it must be initialized. */
69929 if( (u.cc.pRt->flags&MEM_Frame)==0 ){
69930 /* SubProgram.nMem is set to the number of memory cells used by the
69931 ** program stored in SubProgram.aOp. As well as these, one memory
69932 ** cell is required for each cursor used by the program. Set local
69933 ** variable u.cc.nMem (and later, VdbeFrame.nChildMem) to this value.
69934 */
69935 u.cc.nMem = u.cc.pProgram->nMem + u.cc.pProgram->nCsr;
69936 u.cc.nByte = ROUND8(sizeof(VdbeFrame))
69937 + u.cc.nMem * sizeof(Mem)
69938 + u.cc.pProgram->nCsr * sizeof(VdbeCursor *)
69939 + u.cc.pProgram->nOnce * sizeof(u8);
69940 u.cc.pFrame = sqlite3DbMallocZero(db, u.cc.nByte);
69941 if( !u.cc.pFrame ){
69942 goto no_mem;
69943 }
69944 sqlite3VdbeMemRelease(u.cc.pRt);
69945 u.cc.pRt->flags = MEM_Frame;
69946 u.cc.pRt->u.pFrame = u.cc.pFrame;
69947
69948 u.cc.pFrame->v = p;
69949 u.cc.pFrame->nChildMem = u.cc.nMem;
69950 u.cc.pFrame->nChildCsr = u.cc.pProgram->nCsr;
69951 u.cc.pFrame->pc = pc;
69952 u.cc.pFrame->aMem = p->aMem;
69953 u.cc.pFrame->nMem = p->nMem;
69954 u.cc.pFrame->apCsr = p->apCsr;
69955 u.cc.pFrame->nCursor = p->nCursor;
69956 u.cc.pFrame->aOp = p->aOp;
69957 u.cc.pFrame->nOp = p->nOp;
69958 u.cc.pFrame->token = u.cc.pProgram->token;
69959 u.cc.pFrame->aOnceFlag = p->aOnceFlag;
69960 u.cc.pFrame->nOnceFlag = p->nOnceFlag;
69961
69962 u.cc.pEnd = &VdbeFrameMem(u.cc.pFrame)[u.cc.pFrame->nChildMem];
69963 for(u.cc.pMem=VdbeFrameMem(u.cc.pFrame); u.cc.pMem!=u.cc.pEnd; u.cc.pMem++){
69964 u.cc.pMem->flags = MEM_Invalid;
69965 u.cc.pMem->db = db;
69966 }
69967 }else{
69968 u.cc.pFrame = u.cc.pRt->u.pFrame;
69969 assert( u.cc.pProgram->nMem+u.cc.pProgram->nCsr==u.cc.pFrame->nChildMem );
69970 assert( u.cc.pProgram->nCsr==u.cc.pFrame->nChildCsr );
69971 assert( pc==u.cc.pFrame->pc );
69972 }
69973
69974 p->nFrame++;
69975 u.cc.pFrame->pParent = p->pFrame;
69976 u.cc.pFrame->lastRowid = lastRowid;
69977 u.cc.pFrame->nChange = p->nChange;
69978 p->nChange = 0;
69979 p->pFrame = u.cc.pFrame;
69980 p->aMem = aMem = &VdbeFrameMem(u.cc.pFrame)[-1];
69981 p->nMem = u.cc.pFrame->nChildMem;
69982 p->nCursor = (u16)u.cc.pFrame->nChildCsr;
69983 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
69984 p->aOp = aOp = u.cc.pProgram->aOp;
69985 p->nOp = u.cc.pProgram->nOp;
69986 p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
69987 p->nOnceFlag = u.cc.pProgram->nOnce;
69988 p->nOp = u.cc.pProgram->nOp;
69989 pc = -1;
69990 memset(p->aOnceFlag, 0, p->nOnceFlag);
69991
69992 break;
69993 }
69994
69995 /* Opcode: Param P1 P2 * * *
@@ -68679,17 +70003,17 @@
70003 ** The address of the cell in the parent frame is determined by adding
70004 ** the value of the P1 argument to the value of the P1 argument to the
70005 ** calling OP_Program instruction.
70006 */
70007 case OP_Param: { /* out2-prerelease */
70008 #if 0 /* local variables moved into u.cd */
70009 VdbeFrame *pFrame;
70010 Mem *pIn;
70011 #endif /* local variables moved into u.cd */
70012 u.cd.pFrame = p->pFrame;
70013 u.cd.pIn = &u.cd.pFrame->aMem[pOp->p1 + u.cd.pFrame->aOp[u.cd.pFrame->pc].p1];
70014 sqlite3VdbeMemShallowCopy(pOut, u.cd.pIn, MEM_Ephem);
70015 break;
70016 }
70017
70018 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
70019
@@ -68741,26 +70065,26 @@
70065 **
70066 ** This instruction throws an error if the memory cell is not initially
70067 ** an integer.
70068 */
70069 case OP_MemMax: { /* in2 */
70070 #if 0 /* local variables moved into u.ce */
70071 Mem *pIn1;
70072 VdbeFrame *pFrame;
70073 #endif /* local variables moved into u.ce */
70074 if( p->pFrame ){
70075 for(u.ce.pFrame=p->pFrame; u.ce.pFrame->pParent; u.ce.pFrame=u.ce.pFrame->pParent);
70076 u.ce.pIn1 = &u.ce.pFrame->aMem[pOp->p1];
70077 }else{
70078 u.ce.pIn1 = &aMem[pOp->p1];
70079 }
70080 assert( memIsValid(u.ce.pIn1) );
70081 sqlite3VdbeMemIntegerify(u.ce.pIn1);
70082 pIn2 = &aMem[pOp->p2];
70083 sqlite3VdbeMemIntegerify(pIn2);
70084 if( u.ce.pIn1->u.i<pIn2->u.i){
70085 u.ce.pIn1->u.i = pIn2->u.i;
70086 }
70087 break;
70088 }
70089 #endif /* SQLITE_OMIT_AUTOINCREMENT */
70090
@@ -68823,54 +70147,54 @@
70147 **
70148 ** The P5 arguments are taken from register P2 and its
70149 ** successors.
70150 */
70151 case OP_AggStep: {
70152 #if 0 /* local variables moved into u.cf */
70153 int n;
70154 int i;
70155 Mem *pMem;
70156 Mem *pRec;
70157 sqlite3_context ctx;
70158 sqlite3_value **apVal;
70159 #endif /* local variables moved into u.cf */
70160
70161 u.cf.n = pOp->p5;
70162 assert( u.cf.n>=0 );
70163 u.cf.pRec = &aMem[pOp->p2];
70164 u.cf.apVal = p->apArg;
70165 assert( u.cf.apVal || u.cf.n==0 );
70166 for(u.cf.i=0; u.cf.i<u.cf.n; u.cf.i++, u.cf.pRec++){
70167 assert( memIsValid(u.cf.pRec) );
70168 u.cf.apVal[u.cf.i] = u.cf.pRec;
70169 memAboutToChange(p, u.cf.pRec);
70170 sqlite3VdbeMemStoreType(u.cf.pRec);
70171 }
70172 u.cf.ctx.pFunc = pOp->p4.pFunc;
70173 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70174 u.cf.ctx.pMem = u.cf.pMem = &aMem[pOp->p3];
70175 u.cf.pMem->n++;
70176 u.cf.ctx.s.flags = MEM_Null;
70177 u.cf.ctx.s.z = 0;
70178 u.cf.ctx.s.zMalloc = 0;
70179 u.cf.ctx.s.xDel = 0;
70180 u.cf.ctx.s.db = db;
70181 u.cf.ctx.isError = 0;
70182 u.cf.ctx.pColl = 0;
70183 if( u.cf.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
70184 assert( pOp>p->aOp );
70185 assert( pOp[-1].p4type==P4_COLLSEQ );
70186 assert( pOp[-1].opcode==OP_CollSeq );
70187 u.cf.ctx.pColl = pOp[-1].p4.pColl;
70188 }
70189 (u.cf.ctx.pFunc->xStep)(&u.cf.ctx, u.cf.n, u.cf.apVal); /* IMP: R-24505-23230 */
70190 if( u.cf.ctx.isError ){
70191 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cf.ctx.s));
70192 rc = u.cf.ctx.isError;
70193 }
70194
70195 sqlite3VdbeMemRelease(&u.cf.ctx.s);
70196
70197 break;
70198 }
70199
70200 /* Opcode: AggFinal P1 P2 * P4 *
@@ -68884,23 +70208,23 @@
70208 ** functions that can take varying numbers of arguments. The
70209 ** P4 argument is only needed for the degenerate case where
70210 ** the step function was not previously called.
70211 */
70212 case OP_AggFinal: {
70213 #if 0 /* local variables moved into u.cg */
70214 Mem *pMem;
70215 #endif /* local variables moved into u.cg */
70216 assert( pOp->p1>0 && pOp->p1<=p->nMem );
70217 u.cg.pMem = &aMem[pOp->p1];
70218 assert( (u.cg.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
70219 rc = sqlite3VdbeMemFinalize(u.cg.pMem, pOp->p4.pFunc);
70220 if( rc ){
70221 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cg.pMem));
70222 }
70223 sqlite3VdbeChangeEncoding(u.cg.pMem, encoding);
70224 UPDATE_MAX_BLOBSIZE(u.cg.pMem);
70225 if( sqlite3VdbeMemTooBig(u.cg.pMem) ){
70226 goto too_big;
70227 }
70228 break;
70229 }
70230
@@ -68915,29 +70239,29 @@
70239 ** in the WAL that have been checkpointed after the checkpoint
70240 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
70241 ** mem[P3+2] are initialized to -1.
70242 */
70243 case OP_Checkpoint: {
70244 #if 0 /* local variables moved into u.ch */
70245 int i; /* Loop counter */
70246 int aRes[3]; /* Results */
70247 Mem *pMem; /* Write results here */
70248 #endif /* local variables moved into u.ch */
70249
70250 u.ch.aRes[0] = 0;
70251 u.ch.aRes[1] = u.ch.aRes[2] = -1;
70252 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
70253 || pOp->p2==SQLITE_CHECKPOINT_FULL
70254 || pOp->p2==SQLITE_CHECKPOINT_RESTART
70255 );
70256 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.ch.aRes[1], &u.ch.aRes[2]);
70257 if( rc==SQLITE_BUSY ){
70258 rc = SQLITE_OK;
70259 u.ch.aRes[0] = 1;
70260 }
70261 for(u.ch.i=0, u.ch.pMem = &aMem[pOp->p3]; u.ch.i<3; u.ch.i++, u.ch.pMem++){
70262 sqlite3VdbeMemSetInt64(u.ch.pMem, (i64)u.ch.aRes[u.ch.i]);
70263 }
70264 break;
70265 };
70266 #endif
70267
@@ -68952,95 +70276,95 @@
70276 ** If changing into or out of WAL mode the procedure is more complicated.
70277 **
70278 ** Write a string containing the final journal-mode to register P2.
70279 */
70280 case OP_JournalMode: { /* out2-prerelease */
70281 #if 0 /* local variables moved into u.ci */
70282 Btree *pBt; /* Btree to change journal mode of */
70283 Pager *pPager; /* Pager associated with pBt */
70284 int eNew; /* New journal mode */
70285 int eOld; /* The old journal mode */
70286 const char *zFilename; /* Name of database file for pPager */
70287 #endif /* local variables moved into u.ci */
70288
70289 u.ci.eNew = pOp->p3;
70290 assert( u.ci.eNew==PAGER_JOURNALMODE_DELETE
70291 || u.ci.eNew==PAGER_JOURNALMODE_TRUNCATE
70292 || u.ci.eNew==PAGER_JOURNALMODE_PERSIST
70293 || u.ci.eNew==PAGER_JOURNALMODE_OFF
70294 || u.ci.eNew==PAGER_JOURNALMODE_MEMORY
70295 || u.ci.eNew==PAGER_JOURNALMODE_WAL
70296 || u.ci.eNew==PAGER_JOURNALMODE_QUERY
70297 );
70298 assert( pOp->p1>=0 && pOp->p1<db->nDb );
70299
70300 u.ci.pBt = db->aDb[pOp->p1].pBt;
70301 u.ci.pPager = sqlite3BtreePager(u.ci.pBt);
70302 u.ci.eOld = sqlite3PagerGetJournalMode(u.ci.pPager);
70303 if( u.ci.eNew==PAGER_JOURNALMODE_QUERY ) u.ci.eNew = u.ci.eOld;
70304 if( !sqlite3PagerOkToChangeJournalMode(u.ci.pPager) ) u.ci.eNew = u.ci.eOld;
70305
70306 #ifndef SQLITE_OMIT_WAL
70307 u.ci.zFilename = sqlite3PagerFilename(u.ci.pPager);
70308
70309 /* Do not allow a transition to journal_mode=WAL for a database
70310 ** in temporary storage or if the VFS does not support shared memory
70311 */
70312 if( u.ci.eNew==PAGER_JOURNALMODE_WAL
70313 && (sqlite3Strlen30(u.ci.zFilename)==0 /* Temp file */
70314 || !sqlite3PagerWalSupported(u.ci.pPager)) /* No shared-memory support */
70315 ){
70316 u.ci.eNew = u.ci.eOld;
70317 }
70318
70319 if( (u.ci.eNew!=u.ci.eOld)
70320 && (u.ci.eOld==PAGER_JOURNALMODE_WAL || u.ci.eNew==PAGER_JOURNALMODE_WAL)
70321 ){
70322 if( !db->autoCommit || db->activeVdbeCnt>1 ){
70323 rc = SQLITE_ERROR;
70324 sqlite3SetString(&p->zErrMsg, db,
70325 "cannot change %s wal mode from within a transaction",
70326 (u.ci.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
70327 );
70328 break;
70329 }else{
70330
70331 if( u.ci.eOld==PAGER_JOURNALMODE_WAL ){
70332 /* If leaving WAL mode, close the log file. If successful, the call
70333 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
70334 ** file. An EXCLUSIVE lock may still be held on the database file
70335 ** after a successful return.
70336 */
70337 rc = sqlite3PagerCloseWal(u.ci.pPager);
70338 if( rc==SQLITE_OK ){
70339 sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
70340 }
70341 }else if( u.ci.eOld==PAGER_JOURNALMODE_MEMORY ){
70342 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
70343 ** as an intermediate */
70344 sqlite3PagerSetJournalMode(u.ci.pPager, PAGER_JOURNALMODE_OFF);
70345 }
70346
70347 /* Open a transaction on the database file. Regardless of the journal
70348 ** mode, this transaction always uses a rollback journal.
70349 */
70350 assert( sqlite3BtreeIsInTrans(u.ci.pBt)==0 );
70351 if( rc==SQLITE_OK ){
70352 rc = sqlite3BtreeSetVersion(u.ci.pBt, (u.ci.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
70353 }
70354 }
70355 }
70356 #endif /* ifndef SQLITE_OMIT_WAL */
70357
70358 if( rc ){
70359 u.ci.eNew = u.ci.eOld;
70360 }
70361 u.ci.eNew = sqlite3PagerSetJournalMode(u.ci.pPager, u.ci.eNew);
70362
70363 pOut = &aMem[pOp->p2];
70364 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
70365 pOut->z = (char *)sqlite3JournalModename(u.ci.eNew);
70366 pOut->n = sqlite3Strlen30(pOut->z);
70367 pOut->enc = SQLITE_UTF8;
70368 sqlite3VdbeChangeEncoding(pOut, encoding);
70369 break;
70370 };
@@ -69065,18 +70389,18 @@
70389 ** Perform a single step of the incremental vacuum procedure on
70390 ** the P1 database. If the vacuum has finished, jump to instruction
70391 ** P2. Otherwise, fall through to the next instruction.
70392 */
70393 case OP_IncrVacuum: { /* jump */
70394 #if 0 /* local variables moved into u.cj */
70395 Btree *pBt;
70396 #endif /* local variables moved into u.cj */
70397
70398 assert( pOp->p1>=0 && pOp->p1<db->nDb );
70399 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
70400 u.cj.pBt = db->aDb[pOp->p1].pBt;
70401 rc = sqlite3BtreeIncrVacuum(u.cj.pBt);
70402 if( rc==SQLITE_DONE ){
70403 pc = pOp->p2 - 1;
70404 rc = SQLITE_OK;
70405 }
70406 break;
@@ -69142,16 +70466,16 @@
70466 ** Also, whether or not P4 is set, check that this is not being called from
70467 ** within a callback to a virtual table xSync() method. If it is, the error
70468 ** code will be set to SQLITE_LOCKED.
70469 */
70470 case OP_VBegin: {
70471 #if 0 /* local variables moved into u.ck */
70472 VTable *pVTab;
70473 #endif /* local variables moved into u.ck */
70474 u.ck.pVTab = pOp->p4.pVtab;
70475 rc = sqlite3VtabBegin(db, u.ck.pVTab);
70476 if( u.ck.pVTab ) importVtabErrMsg(p, u.ck.pVTab->pVtab);
70477 break;
70478 }
70479 #endif /* SQLITE_OMIT_VIRTUALTABLE */
70480
70481 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -69186,36 +70510,36 @@
70510 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
70511 ** P1 is a cursor number. This opcode opens a cursor to the virtual
70512 ** table and stores that cursor in P1.
70513 */
70514 case OP_VOpen: {
70515 #if 0 /* local variables moved into u.cl */
70516 VdbeCursor *pCur;
70517 sqlite3_vtab_cursor *pVtabCursor;
70518 sqlite3_vtab *pVtab;
70519 sqlite3_module *pModule;
70520 #endif /* local variables moved into u.cl */
70521
70522 u.cl.pCur = 0;
70523 u.cl.pVtabCursor = 0;
70524 u.cl.pVtab = pOp->p4.pVtab->pVtab;
70525 u.cl.pModule = (sqlite3_module *)u.cl.pVtab->pModule;
70526 assert(u.cl.pVtab && u.cl.pModule);
70527 rc = u.cl.pModule->xOpen(u.cl.pVtab, &u.cl.pVtabCursor);
70528 importVtabErrMsg(p, u.cl.pVtab);
70529 if( SQLITE_OK==rc ){
70530 /* Initialize sqlite3_vtab_cursor base class */
70531 u.cl.pVtabCursor->pVtab = u.cl.pVtab;
70532
70533 /* Initialise vdbe cursor object */
70534 u.cl.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
70535 if( u.cl.pCur ){
70536 u.cl.pCur->pVtabCursor = u.cl.pVtabCursor;
70537 u.cl.pCur->pModule = u.cl.pVtabCursor->pVtab->pModule;
70538 }else{
70539 db->mallocFailed = 1;
70540 u.cl.pModule->xClose(u.cl.pVtabCursor);
70541 }
70542 }
70543 break;
70544 }
70545 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69238,11 +70562,11 @@
70562 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
70563 **
70564 ** A jump is made to P2 if the result set after filtering would be empty.
70565 */
70566 case OP_VFilter: { /* jump */
70567 #if 0 /* local variables moved into u.cm */
70568 int nArg;
70569 int iQuery;
70570 const sqlite3_module *pModule;
70571 Mem *pQuery;
70572 Mem *pArgc;
@@ -69250,49 +70574,49 @@
70574 sqlite3_vtab *pVtab;
70575 VdbeCursor *pCur;
70576 int res;
70577 int i;
70578 Mem **apArg;
70579 #endif /* local variables moved into u.cm */
70580
70581 u.cm.pQuery = &aMem[pOp->p3];
70582 u.cm.pArgc = &u.cm.pQuery[1];
70583 u.cm.pCur = p->apCsr[pOp->p1];
70584 assert( memIsValid(u.cm.pQuery) );
70585 REGISTER_TRACE(pOp->p3, u.cm.pQuery);
70586 assert( u.cm.pCur->pVtabCursor );
70587 u.cm.pVtabCursor = u.cm.pCur->pVtabCursor;
70588 u.cm.pVtab = u.cm.pVtabCursor->pVtab;
70589 u.cm.pModule = u.cm.pVtab->pModule;
70590
70591 /* Grab the index number and argc parameters */
70592 assert( (u.cm.pQuery->flags&MEM_Int)!=0 && u.cm.pArgc->flags==MEM_Int );
70593 u.cm.nArg = (int)u.cm.pArgc->u.i;
70594 u.cm.iQuery = (int)u.cm.pQuery->u.i;
70595
70596 /* Invoke the xFilter method */
70597 {
70598 u.cm.res = 0;
70599 u.cm.apArg = p->apArg;
70600 for(u.cm.i = 0; u.cm.i<u.cm.nArg; u.cm.i++){
70601 u.cm.apArg[u.cm.i] = &u.cm.pArgc[u.cm.i+1];
70602 sqlite3VdbeMemStoreType(u.cm.apArg[u.cm.i]);
70603 }
70604
70605 p->inVtabMethod = 1;
70606 rc = u.cm.pModule->xFilter(u.cm.pVtabCursor, u.cm.iQuery, pOp->p4.z, u.cm.nArg, u.cm.apArg);
70607 p->inVtabMethod = 0;
70608 importVtabErrMsg(p, u.cm.pVtab);
70609 if( rc==SQLITE_OK ){
70610 u.cm.res = u.cm.pModule->xEof(u.cm.pVtabCursor);
70611 }
70612
70613 if( u.cm.res ){
70614 pc = pOp->p2 - 1;
70615 }
70616 }
70617 u.cm.pCur->nullRow = 0;
70618
70619 break;
70620 }
70621 #endif /* SQLITE_OMIT_VIRTUALTABLE */
70622
@@ -69302,55 +70626,55 @@
70626 ** Store the value of the P2-th column of
70627 ** the row of the virtual-table that the
70628 ** P1 cursor is pointing to into register P3.
70629 */
70630 case OP_VColumn: {
70631 #if 0 /* local variables moved into u.cn */
70632 sqlite3_vtab *pVtab;
70633 const sqlite3_module *pModule;
70634 Mem *pDest;
70635 sqlite3_context sContext;
70636 #endif /* local variables moved into u.cn */
70637
70638 VdbeCursor *pCur = p->apCsr[pOp->p1];
70639 assert( pCur->pVtabCursor );
70640 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70641 u.cn.pDest = &aMem[pOp->p3];
70642 memAboutToChange(p, u.cn.pDest);
70643 if( pCur->nullRow ){
70644 sqlite3VdbeMemSetNull(u.cn.pDest);
70645 break;
70646 }
70647 u.cn.pVtab = pCur->pVtabCursor->pVtab;
70648 u.cn.pModule = u.cn.pVtab->pModule;
70649 assert( u.cn.pModule->xColumn );
70650 memset(&u.cn.sContext, 0, sizeof(u.cn.sContext));
70651
70652 /* The output cell may already have a buffer allocated. Move
70653 ** the current contents to u.cn.sContext.s so in case the user-function
70654 ** can use the already allocated buffer instead of allocating a
70655 ** new one.
70656 */
70657 sqlite3VdbeMemMove(&u.cn.sContext.s, u.cn.pDest);
70658 MemSetTypeFlag(&u.cn.sContext.s, MEM_Null);
70659
70660 rc = u.cn.pModule->xColumn(pCur->pVtabCursor, &u.cn.sContext, pOp->p2);
70661 importVtabErrMsg(p, u.cn.pVtab);
70662 if( u.cn.sContext.isError ){
70663 rc = u.cn.sContext.isError;
70664 }
70665
70666 /* Copy the result of the function to the P3 register. We
70667 ** do this regardless of whether or not an error occurred to ensure any
70668 ** dynamic allocation in u.cn.sContext.s (a Mem struct) is released.
70669 */
70670 sqlite3VdbeChangeEncoding(&u.cn.sContext.s, encoding);
70671 sqlite3VdbeMemMove(u.cn.pDest, &u.cn.sContext.s);
70672 REGISTER_TRACE(pOp->p3, u.cn.pDest);
70673 UPDATE_MAX_BLOBSIZE(u.cn.pDest);
70674
70675 if( sqlite3VdbeMemTooBig(u.cn.pDest) ){
70676 goto too_big;
70677 }
70678 break;
70679 }
70680 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -69361,42 +70685,42 @@
70685 ** Advance virtual table P1 to the next row in its result set and
70686 ** jump to instruction P2. Or, if the virtual table has reached
70687 ** the end of its result set, then fall through to the next instruction.
70688 */
70689 case OP_VNext: { /* jump */
70690 #if 0 /* local variables moved into u.co */
70691 sqlite3_vtab *pVtab;
70692 const sqlite3_module *pModule;
70693 int res;
70694 VdbeCursor *pCur;
70695 #endif /* local variables moved into u.co */
70696
70697 u.co.res = 0;
70698 u.co.pCur = p->apCsr[pOp->p1];
70699 assert( u.co.pCur->pVtabCursor );
70700 if( u.co.pCur->nullRow ){
70701 break;
70702 }
70703 u.co.pVtab = u.co.pCur->pVtabCursor->pVtab;
70704 u.co.pModule = u.co.pVtab->pModule;
70705 assert( u.co.pModule->xNext );
70706
70707 /* Invoke the xNext() method of the module. There is no way for the
70708 ** underlying implementation to return an error if one occurs during
70709 ** xNext(). Instead, if an error occurs, true is returned (indicating that
70710 ** data is available) and the error code returned when xColumn or
70711 ** some other method is next invoked on the save virtual table cursor.
70712 */
70713 p->inVtabMethod = 1;
70714 rc = u.co.pModule->xNext(u.co.pCur->pVtabCursor);
70715 p->inVtabMethod = 0;
70716 importVtabErrMsg(p, u.co.pVtab);
70717 if( rc==SQLITE_OK ){
70718 u.co.res = u.co.pModule->xEof(u.co.pCur->pVtabCursor);
70719 }
70720
70721 if( !u.co.res ){
70722 /* If there is data, jump to P2 */
70723 pc = pOp->p2 - 1;
70724 }
70725 break;
70726 }
@@ -69408,28 +70732,28 @@
70732 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
70733 ** This opcode invokes the corresponding xRename method. The value
70734 ** in register P1 is passed as the zName argument to the xRename method.
70735 */
70736 case OP_VRename: {
70737 #if 0 /* local variables moved into u.cp */
70738 sqlite3_vtab *pVtab;
70739 Mem *pName;
70740 #endif /* local variables moved into u.cp */
70741
70742 u.cp.pVtab = pOp->p4.pVtab->pVtab;
70743 u.cp.pName = &aMem[pOp->p1];
70744 assert( u.cp.pVtab->pModule->xRename );
70745 assert( memIsValid(u.cp.pName) );
70746 REGISTER_TRACE(pOp->p1, u.cp.pName);
70747 assert( u.cp.pName->flags & MEM_Str );
70748 testcase( u.cp.pName->enc==SQLITE_UTF8 );
70749 testcase( u.cp.pName->enc==SQLITE_UTF16BE );
70750 testcase( u.cp.pName->enc==SQLITE_UTF16LE );
70751 rc = sqlite3VdbeChangeEncoding(u.cp.pName, SQLITE_UTF8);
70752 if( rc==SQLITE_OK ){
70753 rc = u.cp.pVtab->pModule->xRename(u.cp.pVtab, u.cp.pName->z);
70754 importVtabErrMsg(p, u.cp.pVtab);
70755 p->expired = 0;
70756 }
70757 break;
70758 }
70759 #endif
@@ -69457,45 +70781,45 @@
70781 ** P1 is a boolean flag. If it is set to true and the xUpdate call
70782 ** is successful, then the value returned by sqlite3_last_insert_rowid()
70783 ** is set to the value of the rowid for the row just inserted.
70784 */
70785 case OP_VUpdate: {
70786 #if 0 /* local variables moved into u.cq */
70787 sqlite3_vtab *pVtab;
70788 sqlite3_module *pModule;
70789 int nArg;
70790 int i;
70791 sqlite_int64 rowid;
70792 Mem **apArg;
70793 Mem *pX;
70794 #endif /* local variables moved into u.cq */
70795
70796 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
70797 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
70798 );
70799 u.cq.pVtab = pOp->p4.pVtab->pVtab;
70800 u.cq.pModule = (sqlite3_module *)u.cq.pVtab->pModule;
70801 u.cq.nArg = pOp->p2;
70802 assert( pOp->p4type==P4_VTAB );
70803 if( ALWAYS(u.cq.pModule->xUpdate) ){
70804 u8 vtabOnConflict = db->vtabOnConflict;
70805 u.cq.apArg = p->apArg;
70806 u.cq.pX = &aMem[pOp->p3];
70807 for(u.cq.i=0; u.cq.i<u.cq.nArg; u.cq.i++){
70808 assert( memIsValid(u.cq.pX) );
70809 memAboutToChange(p, u.cq.pX);
70810 sqlite3VdbeMemStoreType(u.cq.pX);
70811 u.cq.apArg[u.cq.i] = u.cq.pX;
70812 u.cq.pX++;
70813 }
70814 db->vtabOnConflict = pOp->p5;
70815 rc = u.cq.pModule->xUpdate(u.cq.pVtab, u.cq.nArg, u.cq.apArg, &u.cq.rowid);
70816 db->vtabOnConflict = vtabOnConflict;
70817 importVtabErrMsg(p, u.cq.pVtab);
70818 if( rc==SQLITE_OK && pOp->p1 ){
70819 assert( u.cq.nArg>1 && u.cq.apArg[0] && (u.cq.apArg[0]->flags&MEM_Null) );
70820 db->lastRowid = lastRowid = u.cq.rowid;
70821 }
70822 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
70823 if( pOp->p5==OE_Ignore ){
70824 rc = SQLITE_OK;
70825 }else{
@@ -69551,25 +70875,25 @@
70875 **
70876 ** If tracing is enabled (by the sqlite3_trace()) interface, then
70877 ** the UTF-8 string contained in P4 is emitted on the trace callback.
70878 */
70879 case OP_Trace: {
70880 #if 0 /* local variables moved into u.cr */
70881 char *zTrace;
70882 char *z;
70883 #endif /* local variables moved into u.cr */
70884
70885 if( db->xTrace && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
70886 u.cr.z = sqlite3VdbeExpandSql(p, u.cr.zTrace);
70887 db->xTrace(db->pTraceArg, u.cr.z);
70888 sqlite3DbFree(db, u.cr.z);
70889 }
70890 #ifdef SQLITE_DEBUG
70891 if( (db->flags & SQLITE_SqlTrace)!=0
70892 && (u.cr.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
70893 ){
70894 sqlite3DebugPrintf("SQL-trace: %s\n", u.cr.zTrace);
70895 }
70896 #endif /* SQLITE_DEBUG */
70897 break;
70898 }
70899 #endif
@@ -72487,11 +73811,11 @@
73811 pItem->pExpr = pE = sqlite3Expr(db, TK_INTEGER, 0);
73812 if( pE==0 ) return 1;
73813 pE->pColl = pColl;
73814 pE->flags |= EP_IntValue | flags;
73815 pE->u.iValue = iCol;
73816 pItem->iOrderByCol = (u16)iCol;
73817 pItem->done = 1;
73818 }else{
73819 moreToDo = 1;
73820 }
73821 }
@@ -72536,16 +73860,16 @@
73860 }
73861 #endif
73862 pEList = pSelect->pEList;
73863 assert( pEList!=0 ); /* sqlite3SelectNew() guarantees this */
73864 for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
73865 if( pItem->iOrderByCol ){
73866 if( pItem->iOrderByCol>pEList->nExpr ){
73867 resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
73868 return 1;
73869 }
73870 resolveAlias(pParse, pEList, pItem->iOrderByCol-1, pItem->pExpr, zType);
73871 }
73872 }
73873 return 0;
73874 }
73875
@@ -72588,11 +73912,11 @@
73912 if( iCol>0 ){
73913 /* If an AS-name match is found, mark this ORDER BY column as being
73914 ** a copy of the iCol-th result-set column. The subsequent call to
73915 ** sqlite3ResolveOrderGroupBy() will convert the expression to a
73916 ** copy of the iCol-th result-set expression. */
73917 pItem->iOrderByCol = (u16)iCol;
73918 continue;
73919 }
73920 if( sqlite3ExprIsInteger(pE, &iCol) ){
73921 /* The ORDER BY term is an integer constant. Again, set the column
73922 ** number so that sqlite3ResolveOrderGroupBy() will convert the
@@ -72599,16 +73923,16 @@
73923 ** order-by term to a copy of the result-set expression */
73924 if( iCol<1 ){
73925 resolveOutOfRangeError(pParse, zType, i+1, nResult);
73926 return 1;
73927 }
73928 pItem->iOrderByCol = (u16)iCol;
73929 continue;
73930 }
73931
73932 /* Otherwise, treat the ORDER BY term as an ordinary expression */
73933 pItem->iOrderByCol = 0;
73934 if( sqlite3ResolveExprNames(pNC, pE) ){
73935 return 1;
73936 }
73937 }
73938 return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
@@ -73783,11 +75107,11 @@
75107 pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
75108 pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
75109 pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
75110 pItem->sortOrder = pOldItem->sortOrder;
75111 pItem->done = 0;
75112 pItem->iOrderByCol = pOldItem->iOrderByCol;
75113 pItem->iAlias = pOldItem->iAlias;
75114 }
75115 return pNew;
75116 }
75117
@@ -73853,11 +75177,11 @@
75177 pNewItem->idx = pOldItem->idx;
75178 }
75179 return pNew;
75180 }
75181 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
75182 Select *pNew, *pPrior;
75183 if( p==0 ) return 0;
75184 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
75185 if( pNew==0 ) return 0;
75186 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
75187 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
@@ -73864,11 +75188,13 @@
75188 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
75189 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
75190 pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
75191 pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
75192 pNew->op = p->op;
75193 pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
75194 if( pPrior ) pPrior->pNext = pNew;
75195 pNew->pNext = 0;
75196 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
75197 pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
75198 pNew->iLimit = 0;
75199 pNew->iOffset = 0;
75200 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
@@ -74285,10 +75611,19 @@
75611 if( pEList->nExpr!=1 ) return 0; /* One column in the result set */
75612 if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
75613 return 1;
75614 }
75615 #endif /* SQLITE_OMIT_SUBQUERY */
75616
75617 /*
75618 ** Code an OP_Once instruction and allocate space for its flag. Return the
75619 ** address of the new instruction.
75620 */
75621 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
75622 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
75623 return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
75624 }
75625
75626 /*
75627 ** This function is used by the implementation of the IN (...) operator.
75628 ** It's job is to find or create a b-tree structure that may be used
75629 ** either to test for membership of the (...) set or to iterate through
@@ -74346,10 +75681,11 @@
75681 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
75682 Select *p; /* SELECT to the right of IN operator */
75683 int eType = 0; /* Type of RHS table. IN_INDEX_* */
75684 int iTab = pParse->nTab++; /* Cursor of the RHS table */
75685 int mustBeUnique = (prNotFound==0); /* True if RHS must be unique */
75686 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
75687
75688 assert( pX->op==TK_IN );
75689
75690 /* Check to see if an existing table or index can be used to
75691 ** satisfy the query. This is preferable to generating a new
@@ -74356,11 +75692,10 @@
75692 ** ephemeral table.
75693 */
75694 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
75695 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
75696 sqlite3 *db = pParse->db; /* Database connection */
 
75697 Table *pTab; /* Table <table>. */
75698 Expr *pExpr; /* Expression <column> */
75699 int iCol; /* Index of column <column> */
75700 int iDb; /* Database idx for pTab */
75701
@@ -74381,14 +75716,13 @@
75716 ** has already been allocated. So assume sqlite3GetVdbe() is always
75717 ** successful here.
75718 */
75719 assert(v);
75720 if( iCol<0 ){
 
75721 int iAddr;
75722
75723 iAddr = sqlite3CodeOnce(pParse);
75724
75725 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
75726 eType = IN_INDEX_ROWID;
75727
75728 sqlite3VdbeJumpHere(v, iAddr);
@@ -74410,25 +75744,25 @@
75744 for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
75745 if( (pIdx->aiColumn[0]==iCol)
75746 && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
75747 && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
75748 ){
 
75749 int iAddr;
75750 char *pKey;
75751
75752 pKey = (char *)sqlite3IndexKeyinfo(pParse, pIdx);
75753 iAddr = sqlite3CodeOnce(pParse);
75754
75755 sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pIdx->tnum, iDb,
75756 pKey,P4_KEYINFO_HANDOFF);
75757 VdbeComment((v, "%s", pIdx->zName));
75758 eType = IN_INDEX_INDEX;
75759
75760 sqlite3VdbeJumpHere(v, iAddr);
75761 if( prNotFound && !pTab->aCol[iCol].notNull ){
75762 *prNotFound = ++pParse->nMem;
75763 sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
75764 }
75765 }
75766 }
75767 }
75768 }
@@ -74440,10 +75774,11 @@
75774 double savedNQueryLoop = pParse->nQueryLoop;
75775 int rMayHaveNull = 0;
75776 eType = IN_INDEX_EPH;
75777 if( prNotFound ){
75778 *prNotFound = rMayHaveNull = ++pParse->nMem;
75779 sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
75780 }else{
75781 testcase( pParse->nQueryLoop>(double)1 );
75782 pParse->nQueryLoop = (double)1;
75783 if( pX->pLeft->iColumn<0 && !ExprHasAnyProperty(pX, EP_xIsSelect) ){
75784 eType = IN_INDEX_ROWID;
@@ -74512,13 +75847,12 @@
75847 ** * We are inside a trigger
75848 **
75849 ** If all of the above are false, then we can run this code just once
75850 ** save the results, and reuse the same result on subsequent invocations.
75851 */
75852 if( !ExprHasAnyProperty(pExpr, EP_VarSelect) ){
75853 testAddr = sqlite3CodeOnce(pParse);
 
75854 }
75855
75856 #ifndef SQLITE_OMIT_EXPLAIN
75857 if( pParse->explain==2 ){
75858 char *zMsg = sqlite3MPrintf(
@@ -75852,10 +77186,268 @@
77186 pExpr->op = TK_REGISTER;
77187 }
77188 return inReg;
77189 }
77190
77191 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
77192 /*
77193 ** Generate a human-readable explanation of an expression tree.
77194 */
77195 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
77196 int op; /* The opcode being coded */
77197 const char *zBinOp = 0; /* Binary operator */
77198 const char *zUniOp = 0; /* Unary operator */
77199 if( pExpr==0 ){
77200 op = TK_NULL;
77201 }else{
77202 op = pExpr->op;
77203 }
77204 switch( op ){
77205 case TK_AGG_COLUMN: {
77206 sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
77207 pExpr->iTable, pExpr->iColumn);
77208 break;
77209 }
77210 case TK_COLUMN: {
77211 if( pExpr->iTable<0 ){
77212 /* This only happens when coding check constraints */
77213 sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
77214 }else{
77215 sqlite3ExplainPrintf(pOut, "{%d:%d}",
77216 pExpr->iTable, pExpr->iColumn);
77217 }
77218 break;
77219 }
77220 case TK_INTEGER: {
77221 if( pExpr->flags & EP_IntValue ){
77222 sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
77223 }else{
77224 sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
77225 }
77226 break;
77227 }
77228 #ifndef SQLITE_OMIT_FLOATING_POINT
77229 case TK_FLOAT: {
77230 sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
77231 break;
77232 }
77233 #endif
77234 case TK_STRING: {
77235 sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
77236 break;
77237 }
77238 case TK_NULL: {
77239 sqlite3ExplainPrintf(pOut,"NULL");
77240 break;
77241 }
77242 #ifndef SQLITE_OMIT_BLOB_LITERAL
77243 case TK_BLOB: {
77244 sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
77245 break;
77246 }
77247 #endif
77248 case TK_VARIABLE: {
77249 sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
77250 pExpr->u.zToken, pExpr->iColumn);
77251 break;
77252 }
77253 case TK_REGISTER: {
77254 sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
77255 break;
77256 }
77257 case TK_AS: {
77258 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77259 break;
77260 }
77261 #ifndef SQLITE_OMIT_CAST
77262 case TK_CAST: {
77263 /* Expressions of the form: CAST(pLeft AS token) */
77264 const char *zAff = "unk";
77265 switch( sqlite3AffinityType(pExpr->u.zToken) ){
77266 case SQLITE_AFF_TEXT: zAff = "TEXT"; break;
77267 case SQLITE_AFF_NONE: zAff = "NONE"; break;
77268 case SQLITE_AFF_NUMERIC: zAff = "NUMERIC"; break;
77269 case SQLITE_AFF_INTEGER: zAff = "INTEGER"; break;
77270 case SQLITE_AFF_REAL: zAff = "REAL"; break;
77271 }
77272 sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
77273 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77274 sqlite3ExplainPrintf(pOut, ")");
77275 break;
77276 }
77277 #endif /* SQLITE_OMIT_CAST */
77278 case TK_LT: zBinOp = "LT"; break;
77279 case TK_LE: zBinOp = "LE"; break;
77280 case TK_GT: zBinOp = "GT"; break;
77281 case TK_GE: zBinOp = "GE"; break;
77282 case TK_NE: zBinOp = "NE"; break;
77283 case TK_EQ: zBinOp = "EQ"; break;
77284 case TK_IS: zBinOp = "IS"; break;
77285 case TK_ISNOT: zBinOp = "ISNOT"; break;
77286 case TK_AND: zBinOp = "AND"; break;
77287 case TK_OR: zBinOp = "OR"; break;
77288 case TK_PLUS: zBinOp = "ADD"; break;
77289 case TK_STAR: zBinOp = "MUL"; break;
77290 case TK_MINUS: zBinOp = "SUB"; break;
77291 case TK_REM: zBinOp = "REM"; break;
77292 case TK_BITAND: zBinOp = "BITAND"; break;
77293 case TK_BITOR: zBinOp = "BITOR"; break;
77294 case TK_SLASH: zBinOp = "DIV"; break;
77295 case TK_LSHIFT: zBinOp = "LSHIFT"; break;
77296 case TK_RSHIFT: zBinOp = "RSHIFT"; break;
77297 case TK_CONCAT: zBinOp = "CONCAT"; break;
77298
77299 case TK_UMINUS: zUniOp = "UMINUS"; break;
77300 case TK_UPLUS: zUniOp = "UPLUS"; break;
77301 case TK_BITNOT: zUniOp = "BITNOT"; break;
77302 case TK_NOT: zUniOp = "NOT"; break;
77303 case TK_ISNULL: zUniOp = "ISNULL"; break;
77304 case TK_NOTNULL: zUniOp = "NOTNULL"; break;
77305
77306 case TK_AGG_FUNCTION:
77307 case TK_CONST_FUNC:
77308 case TK_FUNCTION: {
77309 ExprList *pFarg; /* List of function arguments */
77310 if( ExprHasAnyProperty(pExpr, EP_TokenOnly) ){
77311 pFarg = 0;
77312 }else{
77313 pFarg = pExpr->x.pList;
77314 }
77315 sqlite3ExplainPrintf(pOut, "%sFUNCTION:%s(",
77316 op==TK_AGG_FUNCTION ? "AGG_" : "",
77317 pExpr->u.zToken);
77318 if( pFarg ){
77319 sqlite3ExplainExprList(pOut, pFarg);
77320 }
77321 sqlite3ExplainPrintf(pOut, ")");
77322 break;
77323 }
77324 #ifndef SQLITE_OMIT_SUBQUERY
77325 case TK_EXISTS: {
77326 sqlite3ExplainPrintf(pOut, "EXISTS(");
77327 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77328 sqlite3ExplainPrintf(pOut,")");
77329 break;
77330 }
77331 case TK_SELECT: {
77332 sqlite3ExplainPrintf(pOut, "(");
77333 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77334 sqlite3ExplainPrintf(pOut, ")");
77335 break;
77336 }
77337 case TK_IN: {
77338 sqlite3ExplainPrintf(pOut, "IN(");
77339 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77340 sqlite3ExplainPrintf(pOut, ",");
77341 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
77342 sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
77343 }else{
77344 sqlite3ExplainExprList(pOut, pExpr->x.pList);
77345 }
77346 sqlite3ExplainPrintf(pOut, ")");
77347 break;
77348 }
77349 #endif /* SQLITE_OMIT_SUBQUERY */
77350
77351 /*
77352 ** x BETWEEN y AND z
77353 **
77354 ** This is equivalent to
77355 **
77356 ** x>=y AND x<=z
77357 **
77358 ** X is stored in pExpr->pLeft.
77359 ** Y is stored in pExpr->pList->a[0].pExpr.
77360 ** Z is stored in pExpr->pList->a[1].pExpr.
77361 */
77362 case TK_BETWEEN: {
77363 Expr *pX = pExpr->pLeft;
77364 Expr *pY = pExpr->x.pList->a[0].pExpr;
77365 Expr *pZ = pExpr->x.pList->a[1].pExpr;
77366 sqlite3ExplainPrintf(pOut, "BETWEEN(");
77367 sqlite3ExplainExpr(pOut, pX);
77368 sqlite3ExplainPrintf(pOut, ",");
77369 sqlite3ExplainExpr(pOut, pY);
77370 sqlite3ExplainPrintf(pOut, ",");
77371 sqlite3ExplainExpr(pOut, pZ);
77372 sqlite3ExplainPrintf(pOut, ")");
77373 break;
77374 }
77375 case TK_TRIGGER: {
77376 /* If the opcode is TK_TRIGGER, then the expression is a reference
77377 ** to a column in the new.* or old.* pseudo-tables available to
77378 ** trigger programs. In this case Expr.iTable is set to 1 for the
77379 ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
77380 ** is set to the column of the pseudo-table to read, or to -1 to
77381 ** read the rowid field.
77382 */
77383 sqlite3ExplainPrintf(pOut, "%s(%d)",
77384 pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
77385 break;
77386 }
77387 case TK_CASE: {
77388 sqlite3ExplainPrintf(pOut, "CASE(");
77389 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77390 sqlite3ExplainPrintf(pOut, ",");
77391 sqlite3ExplainExprList(pOut, pExpr->x.pList);
77392 break;
77393 }
77394 #ifndef SQLITE_OMIT_TRIGGER
77395 case TK_RAISE: {
77396 const char *zType = "unk";
77397 switch( pExpr->affinity ){
77398 case OE_Rollback: zType = "rollback"; break;
77399 case OE_Abort: zType = "abort"; break;
77400 case OE_Fail: zType = "fail"; break;
77401 case OE_Ignore: zType = "ignore"; break;
77402 }
77403 sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
77404 break;
77405 }
77406 #endif
77407 }
77408 if( zBinOp ){
77409 sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
77410 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77411 sqlite3ExplainPrintf(pOut,",");
77412 sqlite3ExplainExpr(pOut, pExpr->pRight);
77413 sqlite3ExplainPrintf(pOut,")");
77414 }else if( zUniOp ){
77415 sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
77416 sqlite3ExplainExpr(pOut, pExpr->pLeft);
77417 sqlite3ExplainPrintf(pOut,")");
77418 }
77419 }
77420 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
77421
77422 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
77423 /*
77424 ** Generate a human-readable explanation of an expression list.
77425 */
77426 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
77427 int i;
77428 if( pList==0 || pList->nExpr==0 ){
77429 sqlite3ExplainPrintf(pOut, "(empty-list)");
77430 return;
77431 }else if( pList->nExpr==1 ){
77432 sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
77433 }else{
77434 sqlite3ExplainPush(pOut);
77435 for(i=0; i<pList->nExpr; i++){
77436 sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
77437 sqlite3ExplainPush(pOut);
77438 sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
77439 sqlite3ExplainPop(pOut);
77440 if( i<pList->nExpr-1 ){
77441 sqlite3ExplainNL(pOut);
77442 }
77443 }
77444 sqlite3ExplainPop(pOut);
77445 }
77446 }
77447 #endif /* SQLITE_DEBUG */
77448
77449 /*
77450 ** Return TRUE if pExpr is an constant expression that is appropriate
77451 ** for factoring out of a loop. Appropriate expressions are:
77452 **
77453 ** * Any expression that evaluates to two or more opcodes.
@@ -76675,10 +78267,18 @@
78267 if( nReg>pParse->nRangeReg ){
78268 pParse->nRangeReg = nReg;
78269 pParse->iRangeReg = iReg;
78270 }
78271 }
78272
78273 /*
78274 ** Mark all temporary registers as being unavailable for reuse.
78275 */
78276 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
78277 pParse->nTempReg = 0;
78278 pParse->nRangeReg = 0;
78279 }
78280
78281 /************** End of expr.c ************************************************/
78282 /************** Begin file alter.c *******************************************/
78283 /*
78284 ** 2005 February 15
@@ -78036,10 +79636,11 @@
79636 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
79637 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
79638 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
79639 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
79640 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
79641 sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
79642 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
79643 (char*)&stat3InitFuncdef, P4_FUNCDEF);
79644 sqlite3VdbeChangeP5(v, 2);
79645 #endif /* SQLITE_ENABLE_STAT3 */
79646
@@ -82100,23 +83701,27 @@
83701 ** Allocate the index structure.
83702 */
83703 nName = sqlite3Strlen30(zName);
83704 nCol = pList->nExpr;
83705 pIndex = sqlite3DbMallocZero(db,
83706 ROUND8(sizeof(Index)) + /* Index structure */
83707 ROUND8(sizeof(tRowcnt)*(nCol+1)) + /* Index.aiRowEst */
83708 sizeof(char *)*nCol + /* Index.azColl */
83709 sizeof(int)*nCol + /* Index.aiColumn */
83710 sizeof(u8)*nCol + /* Index.aSortOrder */
83711 nName + 1 + /* Index.zName */
83712 nExtra /* Collation sequence names */
83713 );
83714 if( db->mallocFailed ){
83715 goto exit_create_index;
83716 }
83717 zExtra = (char*)pIndex;
83718 pIndex->aiRowEst = (tRowcnt*)&zExtra[ROUND8(sizeof(Index))];
83719 pIndex->azColl = (char**)
83720 ((char*)pIndex->aiRowEst + ROUND8(sizeof(tRowcnt)*nCol+1));
83721 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
83722 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
83723 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
83724 pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
83725 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
83726 zExtra = (char *)(&pIndex->zName[nName+1]);
83727 memcpy(pIndex->zName, zName, nName+1);
@@ -83866,11 +85471,10 @@
85471
85472 /* Check that there isn't an ORDER BY without a LIMIT clause.
85473 */
85474 if( pOrderBy && (pLimit == 0) ) {
85475 sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
 
85476 goto limit_where_cleanup_2;
85477 }
85478
85479 /* We only need to generate a select expression if there
85480 ** is a limit/offset term to enforce.
@@ -87246,11 +88850,11 @@
88850 ** 'b' NONE
88851 ** 'c' NUMERIC
88852 ** 'd' INTEGER
88853 ** 'e' REAL
88854 **
88855 ** An extra 'd' is appended to the end of the string to cover the
88856 ** rowid that appears as the last column in every index.
88857 **
88858 ** Memory for the buffer containing the column index affinity string
88859 ** is managed along with the rest of the Index structure. It will be
88860 ** released when sqlite3DeleteIndex() is called.
@@ -87274,11 +88878,11 @@
88878 return 0;
88879 }
88880 for(n=0; n<pIdx->nColumn; n++){
88881 pIdx->zColAff[n] = pTab->aCol[pIdx->aiColumn[n]].affinity;
88882 }
88883 pIdx->zColAff[n++] = SQLITE_AFF_INTEGER;
88884 pIdx->zColAff[n] = 0;
88885 }
88886
88887 return pIdx->zColAff;
88888 }
@@ -87438,10 +89042,11 @@
89042 for(p = pParse->pAinc; p; p = p->pNext){
89043 pDb = &db->aDb[p->iDb];
89044 memId = p->regCtr;
89045 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
89046 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
89047 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
89048 addr = sqlite3VdbeCurrentAddr(v);
89049 sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
89050 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
89051 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
89052 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
@@ -88305,11 +89910,11 @@
89910 ** any ABORT Back out changes from the current command
89911 ** only (do not do a complete rollback) then
89912 ** cause sqlite3_exec() to return immediately
89913 ** with SQLITE_CONSTRAINT.
89914 **
89915 ** any FAIL Sqlite3_exec() returns immediately with a
89916 ** return code of SQLITE_CONSTRAINT. The
89917 ** transaction is not rolled back and any
89918 ** prior changes are retained.
89919 **
89920 ** any IGNORE The record number and data is popped from
@@ -88777,35 +90382,29 @@
90382 /*
90383 ** Attempt the transfer optimization on INSERTs of the form
90384 **
90385 ** INSERT INTO tab1 SELECT * FROM tab2;
90386 **
90387 ** The xfer optimization transfers raw records from tab2 over to tab1.
90388 ** Columns are not decoded and reassemblied, which greatly improves
90389 ** performance. Raw index records are transferred in the same way.
90390 **
90391 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
90392 ** There are lots of rules for determining compatibility - see comments
90393 ** embedded in the code for details.
90394 **
90395 ** This routine returns TRUE if the optimization is guaranteed to be used.
90396 ** Sometimes the xfer optimization will only work if the destination table
90397 ** is empty - a factor that can only be determined at run-time. In that
90398 ** case, this routine generates code for the xfer optimization but also
90399 ** does a test to see if the destination table is empty and jumps over the
90400 ** xfer optimization code if the test fails. In that case, this routine
90401 ** returns FALSE so that the caller will know to go ahead and generate
90402 ** an unoptimized transfer. This routine also returns FALSE if there
90403 ** is no chance that the xfer optimization can be applied.
90404 **
90405 ** This optimization is particularly useful at making VACUUM run faster.
 
 
 
 
 
 
90406 */
90407 static int xferOptimization(
90408 Parse *pParse, /* Parser context */
90409 Table *pDest, /* The table we are inserting into */
90410 Select *pSelect, /* A SELECT statement to use as the data source */
@@ -88838,14 +90437,12 @@
90437 if( pDest->tabFlags & TF_Virtual ){
90438 return 0; /* tab1 must not be a virtual table */
90439 }
90440 #endif
90441 if( onError==OE_Default ){
90442 if( pDest->iPKey>=0 ) onError = pDest->keyConf;
90443 if( onError==OE_Default ) onError = OE_Abort;
 
 
90444 }
90445 assert(pSelect->pSrc); /* allocated even if there is no FROM clause */
90446 if( pSelect->pSrc->nSrc!=1 ){
90447 return 0; /* FROM clause must have exactly one term */
90448 }
@@ -88947,20 +90544,16 @@
90544 if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
90545 return 0;
90546 }
90547 #endif
90548 if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
90549 return 0; /* xfer opt does not play well with PRAGMA count_changes */
90550 }
90551
90552 /* If we get this far, it means that the xfer optimization is at
90553 ** least a possibility, though it might only work if the destination
90554 ** table (tab1) is initially empty.
 
 
 
 
90555 */
90556 #ifdef SQLITE_TEST
90557 sqlite3_xferopt_count++;
90558 #endif
90559 iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
@@ -88968,20 +90561,27 @@
90561 sqlite3CodeVerifySchema(pParse, iDbSrc);
90562 iSrc = pParse->nTab++;
90563 iDest = pParse->nTab++;
90564 regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
90565 sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
90566 if( (pDest->iPKey<0 && pDest->pIndex!=0) /* (1) */
90567 || destHasUniqueIdx /* (2) */
90568 || (onError!=OE_Abort && onError!=OE_Rollback) /* (3) */
90569 ){
90570 /* In some circumstances, we are able to run the xfer optimization
90571 ** only if the destination table is initially empty. This code makes
90572 ** that determination. Conditions under which the destination must
90573 ** be empty:
90574 **
90575 ** (1) There is no INTEGER PRIMARY KEY but there are indices.
90576 ** (If the destination is not initially empty, the rowid fields
90577 ** of index entries might need to change.)
90578 **
90579 ** (2) The destination has a unique index. (The xfer optimization
90580 ** is unable to test uniqueness.)
90581 **
90582 ** (3) onError is something other than OE_Abort and OE_Rollback.
90583 */
90584 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
90585 emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
90586 sqlite3VdbeJumpHere(v, addr1);
90587 }else{
@@ -90269,10 +91869,11 @@
91869 ** If anything goes wrong, set an error in the database connection.
91870 */
91871 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
91872 int i;
91873 int go = 1;
91874 int rc;
91875 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
91876
91877 wsdAutoextInit;
91878 if( wsdAutoext.nExt==0 ){
91879 /* Common case: early out without every having to acquire a mutex */
@@ -90291,12 +91892,12 @@
91892 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
91893 wsdAutoext.aExt[i];
91894 }
91895 sqlite3_mutex_leave(mutex);
91896 zErrmsg = 0;
91897 if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
91898 sqlite3Error(db, rc,
91899 "automatic extension loading failed: %s", zErrmsg);
91900 go = 0;
91901 }
91902 sqlite3_free(zErrmsg);
91903 }
@@ -90649,11 +92250,11 @@
92250 zDb = pId2->n>0 ? pDb->zName : 0;
92251 if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
92252 goto pragma_out;
92253 }
92254
92255 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
92256 /*
92257 ** PRAGMA [database.]default_cache_size
92258 ** PRAGMA [database.]default_cache_size=N
92259 **
92260 ** The first form reports the current persistent setting for the
@@ -90698,11 +92299,13 @@
92299 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92300 pDb->pSchema->cache_size = size;
92301 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
92302 }
92303 }else
92304 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
92305
92306 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
92307 /*
92308 ** PRAGMA [database.]page_size
92309 ** PRAGMA [database.]page_size=N
92310 **
92311 ** The first form reports the current setting for the
@@ -90719,11 +92322,11 @@
92322 }else{
92323 /* Malloc may fail when setting the page-size, as there is an internal
92324 ** buffer that the pager module resizes using sqlite3_realloc().
92325 */
92326 db->nextPagesize = sqlite3Atoi(zRight);
92327 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
92328 db->mallocFailed = 1;
92329 }
92330 }
92331 }else
92332
@@ -90759,10 +92362,14 @@
92362 ** The first form reports the current setting for the
92363 ** maximum number of pages in the database file. The
92364 ** second form attempts to change this setting. Both
92365 ** forms return the current setting.
92366 **
92367 ** The absolute value of N is used. This is undocumented and might
92368 ** change. The only purpose is to provide an easy way to test
92369 ** the sqlite3AbsInt32() function.
92370 **
92371 ** PRAGMA [database.]page_count
92372 **
92373 ** Return the number of pages in the specified database.
92374 */
92375 if( sqlite3StrICmp(zLeft,"page_count")==0
@@ -90773,11 +92380,12 @@
92380 sqlite3CodeVerifySchema(pParse, iDb);
92381 iReg = ++pParse->nMem;
92382 if( sqlite3Tolower(zLeft[0])=='p' ){
92383 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
92384 }else{
92385 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
92386 sqlite3AbsInt32(sqlite3Atoi(zRight)));
92387 }
92388 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
92389 sqlite3VdbeSetNumCols(v, 1);
92390 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
92391 }else
@@ -90987,26 +92595,23 @@
92595 /*
92596 ** PRAGMA [database.]cache_size
92597 ** PRAGMA [database.]cache_size=N
92598 **
92599 ** The first form reports the current local setting for the
92600 ** page cache size. The second form sets the local
92601 ** page cache size value. If N is positive then that is the
92602 ** number of pages in the cache. If N is negative, then the
92603 ** number of pages is adjusted so that the cache uses -N kibibytes
92604 ** of memory.
 
 
 
92605 */
92606 if( sqlite3StrICmp(zLeft,"cache_size")==0 ){
92607 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
92608 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
92609 if( !zRight ){
92610 returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
92611 }else{
92612 int size = sqlite3Atoi(zRight);
92613 pDb->pSchema->cache_size = size;
92614 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
92615 }
92616 }else
92617
@@ -91094,11 +92699,11 @@
92699 if( sqlite3StrICmp(zLeft, "lock_proxy_file")==0 ){
92700 if( !zRight ){
92701 Pager *pPager = sqlite3BtreePager(pDb->pBt);
92702 char *proxy_file_path = NULL;
92703 sqlite3_file *pFile = sqlite3PagerFile(pPager);
92704 sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
92705 &proxy_file_path);
92706
92707 if( proxy_file_path ){
92708 sqlite3VdbeSetNumCols(v, 1);
92709 sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
@@ -91737,10 +93342,20 @@
93342 db->xWalCallback==sqlite3WalDefaultHook ?
93343 SQLITE_PTR_TO_INT(db->pWalArg) : 0);
93344 }else
93345 #endif
93346
93347 /*
93348 ** PRAGMA shrink_memory
93349 **
93350 ** This pragma attempts to free as much memory as possible from the
93351 ** current database connection.
93352 */
93353 if( sqlite3StrICmp(zLeft, "shrink_memory")==0 ){
93354 sqlite3_db_release_memory(db);
93355 }else
93356
93357 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
93358 /*
93359 ** Report the current state of file logs for all databases
93360 */
93361 if( sqlite3StrICmp(zLeft, "lock_status")==0 ){
@@ -92111,13 +93726,17 @@
93726 DbSetProperty(db, iDb, DB_Empty);
93727 }
93728 pDb->pSchema->enc = ENC(db);
93729
93730 if( pDb->pSchema->cache_size==0 ){
93731 #ifndef SQLITE_OMIT_DEPRECATED
93732 size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
93733 if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
93734 pDb->pSchema->cache_size = size;
93735 #else
93736 pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
93737 #endif
93738 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
93739 }
93740
93741 /*
93742 ** file_format==1 Version 3.0.0.
@@ -94912,20 +96531,20 @@
96531 */
96532 if( op!=TK_ALL ){
96533 for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
96534 struct ExprList_item *pItem;
96535 for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
96536 assert( pItem->iOrderByCol>0 );
96537 if( pItem->iOrderByCol==i ) break;
96538 }
96539 if( j==nOrderBy ){
96540 Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
96541 if( pNew==0 ) return SQLITE_NOMEM;
96542 pNew->flags |= EP_IntValue;
96543 pNew->u.iValue = i;
96544 pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
96545 pOrderBy->a[nOrderBy++].iOrderByCol = (u16)i;
96546 }
96547 }
96548 }
96549
96550 /* Compute the comparison permutation and keyinfo that is used with
@@ -94937,12 +96556,12 @@
96556 */
96557 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
96558 if( aPermute ){
96559 struct ExprList_item *pItem;
96560 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
96561 assert( pItem->iOrderByCol>0 && pItem->iOrderByCol<=p->pEList->nExpr );
96562 aPermute[i] = pItem->iOrderByCol - 1;
96563 }
96564 pKeyMerge =
96565 sqlite3DbMallocRaw(db, sizeof(*pKeyMerge)+nOrderBy*(sizeof(CollSeq*)+1));
96566 if( pKeyMerge ){
96567 pKeyMerge->aSortOrder = (u8*)&pKeyMerge->aColl[nOrderBy];
@@ -95281,13 +96900,12 @@
96900 }
96901 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
96902
96903 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
96904 /*
96905 ** This routine attempts to flatten subqueries as a performance optimization.
96906 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
 
96907 **
96908 ** To understand the concept of flattening, consider the following
96909 ** query:
96910 **
96911 ** SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
@@ -95325,11 +96943,14 @@
96943 ** (4) has since been expanded to exclude all DISTINCT subqueries.
96944 **
96945 ** (6) The subquery does not use aggregates or the outer query is not
96946 ** DISTINCT.
96947 **
96948 ** (7) The subquery has a FROM clause. TODO: For subqueries without
96949 ** A FROM clause, consider adding a FROM close with the special
96950 ** table sqlite_once that consists of a single row containing a
96951 ** single NULL.
96952 **
96953 ** (8) The subquery does not use LIMIT or the outer query is not a join.
96954 **
96955 ** (9) The subquery does not use LIMIT or the outer query does not use
96956 ** aggregates.
@@ -95358,15 +96979,18 @@
96979 ** compound clause made up entirely of non-aggregate queries, and
96980 ** the parent query:
96981 **
96982 ** * is not itself part of a compound select,
96983 ** * is not an aggregate or DISTINCT query, and
96984 ** * is not a join
96985 **
96986 ** The parent and sub-query may contain WHERE clauses. Subject to
96987 ** rules (11), (13) and (14), they may also contain ORDER BY,
96988 ** LIMIT and OFFSET clauses. The subquery cannot use any compound
96989 ** operator other than UNION ALL because all the other compound
96990 ** operators have an implied DISTINCT which is disallowed by
96991 ** restriction (4).
96992 **
96993 ** (18) If the sub-query is a compound select, then all terms of the
96994 ** ORDER by clause of the parent must be simple references to
96995 ** columns of the sub-query.
96996 **
@@ -95374,11 +96998,11 @@
96998 ** have a WHERE clause.
96999 **
97000 ** (20) If the sub-query is a compound select, then it must not use
97001 ** an ORDER BY clause. Ticket #3773. We could relax this constraint
97002 ** somewhat by saying that the terms of the ORDER BY clause must
97003 ** appear as unmodified result columns in the outer query. But we
97004 ** have other optimizations in mind to deal with that case.
97005 **
97006 ** (21) The subquery does not use LIMIT or the outer query is not
97007 ** DISTINCT. (See ticket [752e1646fc]).
97008 **
@@ -95503,23 +97127,25 @@
97127 return 0;
97128 }
97129 for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
97130 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
97131 testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
97132 assert( pSub->pSrc!=0 );
97133 if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
97134 || (pSub1->pPrior && pSub1->op!=TK_ALL)
97135 || pSub1->pSrc->nSrc<1
97136 ){
97137 return 0;
97138 }
97139 testcase( pSub1->pSrc->nSrc>1 );
97140 }
97141
97142 /* Restriction 18. */
97143 if( p->pOrderBy ){
97144 int ii;
97145 for(ii=0; ii<p->pOrderBy->nExpr; ii++){
97146 if( p->pOrderBy->a[ii].iOrderByCol==0 ) return 0;
97147 }
97148 }
97149 }
97150
97151 /***** If we reach this point, flattening is permitted. *****/
@@ -96538,26 +98164,25 @@
98164 assert( pItem->addrFillSub==0 );
98165 pItem->regReturn = ++pParse->nMem;
98166 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
98167 pItem->addrFillSub = topAddr+1;
98168 VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
98169 if( pItem->isCorrelated==0 ){
98170 /* If the subquery is no correlated and if we are not inside of
98171 ** a trigger, then we only need to compute the value of the subquery
98172 ** once. */
98173 onceAddr = sqlite3CodeOnce(pParse);
 
98174 }
98175 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
98176 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
98177 sqlite3Select(pParse, pSub, &dest);
98178 pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
98179 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
98180 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
98181 VdbeComment((v, "end %s", pItem->pTab->zName));
98182 sqlite3VdbeChangeP1(v, topAddr, retAddr);
98183 sqlite3ClearTempRegCache(pParse);
98184 }
98185 if( /*pParse->nErr ||*/ db->mallocFailed ){
98186 goto select_end;
98187 }
98188 pParse->nHeight -= sqlite3SelectExprHeight(p);
@@ -96848,10 +98473,11 @@
98473 pParse->nMem += pGroupBy->nExpr;
98474 sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
98475 VdbeComment((v, "clear abort flag"));
98476 sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
98477 VdbeComment((v, "indicate accumulator empty"));
98478 sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
98479
98480 /* Begin a loop that will extract all source rows in GROUP BY order.
98481 ** This might involve two separate loops with an OP_Sort in between, or
98482 ** it might be a single loop that uses an index to extract information
98483 ** in the right order to begin with.
@@ -97187,102 +98813,102 @@
98813 sqlite3DbFree(db, sAggInfo.aCol);
98814 sqlite3DbFree(db, sAggInfo.aFunc);
98815 return rc;
98816 }
98817
98818 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
98819 /*
98820 ** Generate a human-readable description of a the Select object.
98821 */
98822 static void explainOneSelect(Vdbe *pVdbe, Select *p){
98823 sqlite3ExplainPrintf(pVdbe, "SELECT ");
98824 if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
98825 if( p->selFlags & SF_Distinct ){
98826 sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
98827 }
98828 if( p->selFlags & SF_Aggregate ){
98829 sqlite3ExplainPrintf(pVdbe, "agg_flag ");
98830 }
98831 sqlite3ExplainNL(pVdbe);
98832 sqlite3ExplainPrintf(pVdbe, " ");
98833 }
98834 sqlite3ExplainExprList(pVdbe, p->pEList);
98835 sqlite3ExplainNL(pVdbe);
98836 if( p->pSrc && p->pSrc->nSrc ){
98837 int i;
98838 sqlite3ExplainPrintf(pVdbe, "FROM ");
98839 sqlite3ExplainPush(pVdbe);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98840 for(i=0; i<p->pSrc->nSrc; i++){
98841 struct SrcList_item *pItem = &p->pSrc->a[i];
98842 sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
 
98843 if( pItem->pSelect ){
98844 sqlite3ExplainSelect(pVdbe, pItem->pSelect);
98845 if( pItem->pTab ){
98846 sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
98847 }
98848 }else if( pItem->zName ){
98849 sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
 
 
 
98850 }
98851 if( pItem->zAlias ){
98852 sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
98853 }
98854 if( pItem->jointype & JT_LEFT ){
98855 sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
98856 }
98857 sqlite3ExplainNL(pVdbe);
98858 }
98859 sqlite3ExplainPop(pVdbe);
98860 }
98861 if( p->pWhere ){
98862 sqlite3ExplainPrintf(pVdbe, "WHERE ");
98863 sqlite3ExplainExpr(pVdbe, p->pWhere);
98864 sqlite3ExplainNL(pVdbe);
98865 }
98866 if( p->pGroupBy ){
98867 sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
98868 sqlite3ExplainExprList(pVdbe, p->pGroupBy);
98869 sqlite3ExplainNL(pVdbe);
98870 }
98871 if( p->pHaving ){
98872 sqlite3ExplainPrintf(pVdbe, "HAVING ");
98873 sqlite3ExplainExpr(pVdbe, p->pHaving);
98874 sqlite3ExplainNL(pVdbe);
98875 }
98876 if( p->pOrderBy ){
98877 sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
98878 sqlite3ExplainExprList(pVdbe, p->pOrderBy);
98879 sqlite3ExplainNL(pVdbe);
98880 }
98881 if( p->pLimit ){
98882 sqlite3ExplainPrintf(pVdbe, "LIMIT ");
98883 sqlite3ExplainExpr(pVdbe, p->pLimit);
98884 sqlite3ExplainNL(pVdbe);
98885 }
98886 if( p->pOffset ){
98887 sqlite3ExplainPrintf(pVdbe, "OFFSET ");
98888 sqlite3ExplainExpr(pVdbe, p->pOffset);
98889 sqlite3ExplainNL(pVdbe);
98890 }
98891 }
98892 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
98893 if( p==0 ){
98894 sqlite3ExplainPrintf(pVdbe, "(null-select)");
98895 return;
98896 }
98897 while( p->pPrior ) p = p->pPrior;
98898 sqlite3ExplainPush(pVdbe);
98899 while( p ){
98900 explainOneSelect(pVdbe, p);
98901 p = p->pNext;
98902 if( p==0 ) break;
98903 sqlite3ExplainNL(pVdbe);
98904 sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
98905 }
98906 sqlite3ExplainPrintf(pVdbe, "END");
98907 sqlite3ExplainPop(pVdbe);
98908 }
98909
98910 /* End of the structure debug printing code
98911 *****************************************************************************/
98912 #endif /* defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
98913
98914 /************** End of select.c **********************************************/
@@ -98389,10 +100015,11 @@
100015 if( db->mallocFailed==0 ){
100016 pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
100017 }
100018 pProgram->nMem = pSubParse->nMem;
100019 pProgram->nCsr = pSubParse->nTab;
100020 pProgram->nOnce = pSubParse->nOnce;
100021 pProgram->token = (void *)pTrigger;
100022 pPrg->aColmask[0] = pSubParse->oldmask;
100023 pPrg->aColmask[1] = pSubParse->newmask;
100024 sqlite3VdbeDelete(v);
100025 }
@@ -98736,12 +100363,12 @@
100363
100364 /* Register Allocations */
100365 int regRowCount = 0; /* A count of rows changed */
100366 int regOldRowid; /* The old rowid */
100367 int regNewRowid; /* The new rowid */
100368 int regNew; /* Content of the NEW.* table in triggers */
100369 int regOld = 0; /* Content of OLD.* table in triggers */
100370 int regRowSet = 0; /* Rowset of rows to be updated */
100371
100372 memset(&sContext, 0, sizeof(sContext));
100373 db = pParse->db;
100374 if( pParse->nErr || db->mallocFailed ){
@@ -98886,10 +100513,11 @@
100513 goto update_cleanup;
100514 }
100515 #endif
100516
100517 /* Allocate required registers. */
100518 regRowSet = ++pParse->nMem;
100519 regOldRowid = regNewRowid = ++pParse->nMem;
100520 if( pTrigger || hasFK ){
100521 regOld = pParse->nMem + 1;
100522 pParse->nMem += pTab->nCol;
100523 }
@@ -98920,11 +100548,11 @@
100548 goto update_cleanup;
100549 }
100550
100551 /* Begin the database scan
100552 */
100553 sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
100554 pWInfo = sqlite3WhereBegin(
100555 pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED
100556 );
100557 if( pWInfo==0 ) goto update_cleanup;
100558 okOnePass = pWInfo->okOnePass;
@@ -98931,11 +100559,10 @@
100559
100560 /* Remember the rowid of every item to be updated.
100561 */
100562 sqlite3VdbeAddOp2(v, OP_Rowid, iCur, regOldRowid);
100563 if( !okOnePass ){
 
100564 sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
100565 }
100566
100567 /* End the database scan loop.
100568 */
@@ -99035,13 +100662,14 @@
100662 ** be used eliminates some redundant opcodes.
100663 */
100664 newmask = sqlite3TriggerColmask(
100665 pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
100666 );
100667 sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);
100668 for(i=0; i<pTab->nCol; i++){
100669 if( i==pTab->iPKey ){
100670 /*sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);*/
100671 }else{
100672 j = aXRef[i];
100673 if( j>=0 ){
100674 sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
100675 }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask&(1<<i)) ){
@@ -101300,11 +102928,11 @@
102928 if( pTerm->leftCursor==iCur
102929 && (pTerm->prereqRight & notReady)==0
102930 && pTerm->u.leftColumn==iColumn
102931 && (pTerm->eOperator & op)!=0
102932 ){
102933 if( iColumn>=0 && pIdx && pTerm->eOperator!=WO_ISNULL ){
102934 Expr *pX = pTerm->pExpr;
102935 CollSeq *pColl;
102936 char idxaff;
102937 int j;
102938 Parse *pParse = pWC->pParse;
@@ -102701,11 +104329,10 @@
104329 WhereTerm *pTerm; /* A single term of the WHERE clause */
104330 WhereTerm *pWCEnd; /* End of pWC->a[] */
104331 int nByte; /* Byte of memory needed for pIdx */
104332 Index *pIdx; /* Object describing the transient index */
104333 Vdbe *v; /* Prepared statement under construction */
 
104334 int addrInit; /* Address of the initialization bypass jump */
104335 Table *pTable; /* The table being indexed */
104336 KeyInfo *pKeyinfo; /* Key information for the index */
104337 int addrTop; /* Top of the index fill loop */
104338 int regRecord; /* Register holding an index record */
@@ -102718,12 +104345,11 @@
104345
104346 /* Generate code to skip over the creation and initialization of the
104347 ** transient index on 2nd and subsequent iterations of the loop. */
104348 v = pParse->pVdbe;
104349 assert( v!=0 );
104350 addrInit = sqlite3CodeOnce(pParse);
 
104351
104352 /* Count the number of columns that will be added to the index
104353 ** and used to match WHERE clause constraints */
104354 nColumn = 0;
104355 pTable = pSrc->pTab;
@@ -103748,14 +105374,28 @@
105374 #ifdef SQLITE_ENABLE_STAT3
105375 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
105376 #endif
105377 used |= pTerm->prereqRight;
105378 }
105379
105380 /* If the index being considered is UNIQUE, and there is an equality
105381 ** constraint for all columns in the index, then this search will find
105382 ** at most a single row. In this case set the WHERE_UNIQUE flag to
105383 ** indicate this to the caller.
105384 **
105385 ** Otherwise, if the search may find more than one row, test to see if
105386 ** there is a range constraint on indexed column (nEq+1) that can be
105387 ** optimized using the index.
105388 */
105389 if( nEq==pProbe->nColumn && pProbe->onError!=OE_None ){
105390 testcase( wsFlags & WHERE_COLUMN_IN );
105391 testcase( wsFlags & WHERE_COLUMN_NULL );
105392 if( (wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_NULL))==0 ){
105393 wsFlags |= WHERE_UNIQUE;
105394 }
105395 }else if( pProbe->bUnordered==0 ){
105396 int j = (nEq==pProbe->nColumn ? -1 : pProbe->aiColumn[nEq]);
105397 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
105398 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
105399 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
105400 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
105401 if( pTop ){
@@ -103770,16 +105410,10 @@
105410 used |= pBtm->prereqRight;
105411 testcase( pBtm->pWC!=pWC );
105412 }
105413 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
105414 }
 
 
 
 
 
 
105415 }
105416
105417 /* If there is an ORDER BY clause and the index being considered will
105418 ** naturally scan rows in the required order, set the appropriate flags
105419 ** in wsFlags. Otherwise, if there is an ORDER BY clause but the index
@@ -104386,14 +106020,16 @@
106020 explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
106021 }
106022
106023 j = i;
106024 if( pPlan->wsFlags&WHERE_BTM_LIMIT ){
106025 char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
106026 explainAppendTerm(&txt, i++, z, ">");
106027 }
106028 if( pPlan->wsFlags&WHERE_TOP_LIMIT ){
106029 char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
106030 explainAppendTerm(&txt, i, z, "<");
106031 }
106032 sqlite3StrAccumAppend(&txt, ")", 1);
106033 return sqlite3StrAccumFinish(&txt);
106034 }
106035
@@ -104747,11 +106383,11 @@
106383 char *zStartAff; /* Affinity for start of range constraint */
106384 char *zEndAff; /* Affinity for end of range constraint */
106385
106386 pIdx = pLevel->plan.u.pIdx;
106387 iIdxCur = pLevel->iIdxCur;
106388 k = (nEq==pIdx->nColumn ? -1 : pIdx->aiColumn[nEq]);
106389
106390 /* If this loop satisfies a sort order (pOrderBy) request that
106391 ** was passed to this function to implement a "SELECT min(x) ..."
106392 ** query, then the caller will only allow the loop to run for
106393 ** a single iteration. This means that the first row returned
@@ -104793,11 +106429,13 @@
106429
106430 /* If we are doing a reverse order scan on an ascending index, or
106431 ** a forward order scan on a descending index, interchange the
106432 ** start and end terms (pRangeStart and pRangeEnd).
106433 */
106434 if( (nEq<pIdx->nColumn && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
106435 || (bRev && pIdx->nColumn==nEq)
106436 ){
106437 SWAP(WhereTerm *, pRangeEnd, pRangeStart);
106438 }
106439
106440 testcase( pRangeStart && pRangeStart->eOperator & WO_LE );
106441 testcase( pRangeStart && pRangeStart->eOperator & WO_GE );
@@ -107559,11 +109197,10 @@
109197 /* Here code is inserted which will execute if the parser
109198 ** stack every overflows */
109199
109200 UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
109201 sqlite3ErrorMsg(pParse, "parser stack overflow");
 
109202 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
109203 }
109204
109205 /*
109206 ** Perform a shift action.
@@ -108282,10 +109919,13 @@
109919 break;
109920 case 112: /* cmd ::= select */
109921 {
109922 SelectDest dest = {SRT_Output, 0, 0, 0, 0};
109923 sqlite3Select(pParse, yymsp[0].minor.yy387, &dest);
109924 sqlite3ExplainBegin(pParse->pVdbe);
109925 sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy387);
109926 sqlite3ExplainFinish(pParse->pVdbe);
109927 sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy387);
109928 }
109929 break;
109930 case 113: /* select ::= oneselect */
109931 {yygotominor.yy387 = yymsp[0].minor.yy387;}
@@ -109160,11 +110800,10 @@
110800 #define TOKEN (yyminor.yy0)
110801
110802 UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */
110803 assert( TOKEN.z[0] ); /* The tokenizer always gives us a token */
110804 sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
 
110805 sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
110806 }
110807
110808 /*
110809 ** The following is executed when the parser accepts
@@ -109755,11 +111394,11 @@
111394 *tokenType = TK_SPACE;
111395 return i;
111396 }
111397 case '-': {
111398 if( z[1]=='-' ){
111399 /* IMP: R-50417-27976 -- syntax diagram for comments */
111400 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
111401 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
111402 return i;
111403 }
111404 *tokenType = TK_MINUS;
@@ -109788,11 +111427,11 @@
111427 case '/': {
111428 if( z[1]!='*' || z[2]==0 ){
111429 *tokenType = TK_SLASH;
111430 return 1;
111431 }
111432 /* IMP: R-50417-27976 -- syntax diagram for comments */
111433 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
111434 if( c ) i++;
111435 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
111436 return i;
111437 }
@@ -110582,12 +112221,12 @@
112221 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
112222 ** returns an integer equal to SQLITE_VERSION_NUMBER.
112223 */
112224 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
112225
112226 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
112227 ** zero if and only if SQLite was compiled with mutexing code omitted due to
112228 ** the SQLITE_THREADSAFE compile-time option being set to 0.
112229 */
112230 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
112231
112232 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
@@ -110772,12 +112411,12 @@
112411 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
112412 ** compile-time option.
112413 */
112414 #ifdef SQLITE_EXTRA_INIT
112415 if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
112416 int SQLITE_EXTRA_INIT(const char*);
112417 rc = SQLITE_EXTRA_INIT(0);
112418 }
112419 #endif
112420
112421 return rc;
112422 }
@@ -110790,10 +112429,14 @@
112429 ** on when SQLite is already shut down. If SQLite is already shut down
112430 ** when this routine is invoked, then this routine is a harmless no-op.
112431 */
112432 SQLITE_API int sqlite3_shutdown(void){
112433 if( sqlite3GlobalConfig.isInit ){
112434 #ifdef SQLITE_EXTRA_SHUTDOWN
112435 void SQLITE_EXTRA_SHUTDOWN(void);
112436 SQLITE_EXTRA_SHUTDOWN();
112437 #endif
112438 sqlite3_os_end();
112439 sqlite3_reset_auto_extension();
112440 sqlite3GlobalConfig.isInit = 0;
112441 }
112442 if( sqlite3GlobalConfig.isPCacheInit ){
@@ -110898,20 +112541,29 @@
112541 sqlite3GlobalConfig.nPage = va_arg(ap, int);
112542 break;
112543 }
112544
112545 case SQLITE_CONFIG_PCACHE: {
112546 /* no-op */
 
112547 break;
112548 }
 
112549 case SQLITE_CONFIG_GETPCACHE: {
112550 /* now an error */
112551 rc = SQLITE_ERROR;
112552 break;
112553 }
112554
112555 case SQLITE_CONFIG_PCACHE2: {
112556 /* Specify an alternative page cache implementation */
112557 sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
112558 break;
112559 }
112560 case SQLITE_CONFIG_GETPCACHE2: {
112561 if( sqlite3GlobalConfig.pcache2.xInit==0 ){
112562 sqlite3PCacheSetDefault();
112563 }
112564 *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
112565 break;
112566 }
112567
112568 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
112569 case SQLITE_CONFIG_HEAP: {
@@ -111006,25 +112658,25 @@
112658 ** both at the same time.
112659 */
112660 if( db->lookaside.bMalloced ){
112661 sqlite3_free(db->lookaside.pStart);
112662 }
112663 /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
112664 ** than a pointer to be useful.
112665 */
112666 sz = ROUNDDOWN8(sz); /* IMP: R-33038-09382 */
112667 if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
112668 if( cnt<0 ) cnt = 0;
112669 if( sz==0 || cnt==0 ){
112670 sz = 0;
112671 pStart = 0;
112672 }else if( pBuf==0 ){
 
112673 sqlite3BeginBenignMalloc();
112674 pStart = sqlite3Malloc( sz*cnt ); /* IMP: R-61949-35727 */
112675 sqlite3EndBenignMalloc();
112676 if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
112677 }else{
 
112678 pStart = pBuf;
112679 }
112680 db->lookaside.pStart = pStart;
112681 db->lookaside.pFree = 0;
112682 db->lookaside.sz = (u16)sz;
@@ -111053,10 +112705,30 @@
112705 ** Return the mutex associated with a database connection.
112706 */
112707 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
112708 return db->mutex;
112709 }
112710
112711 /*
112712 ** Free up as much memory as we can from the given database
112713 ** connection.
112714 */
112715 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
112716 int i;
112717 sqlite3_mutex_enter(db->mutex);
112718 sqlite3BtreeEnterAll(db);
112719 for(i=0; i<db->nDb; i++){
112720 Btree *pBt = db->aDb[i].pBt;
112721 if( pBt ){
112722 Pager *pPager = sqlite3BtreePager(pBt);
112723 sqlite3PagerShrink(pPager);
112724 }
112725 }
112726 sqlite3BtreeLeaveAll(db);
112727 sqlite3_mutex_leave(db->mutex);
112728 return SQLITE_OK;
112729 }
112730
112731 /*
112732 ** Configuration settings for an individual database connection
112733 */
112734 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
@@ -112161,11 +113833,10 @@
113833 */
113834 static int createCollation(
113835 sqlite3* db,
113836 const char *zName,
113837 u8 enc,
 
113838 void* pCtx,
113839 int(*xCompare)(void*,int,const void*,int,const void*),
113840 void(*xDel)(void*)
113841 ){
113842 CollSeq *pColl;
@@ -112226,11 +113897,10 @@
113897 if( pColl==0 ) return SQLITE_NOMEM;
113898 pColl->xCmp = xCompare;
113899 pColl->pUser = pCtx;
113900 pColl->xDel = xDel;
113901 pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
 
113902 sqlite3Error(db, SQLITE_OK, 0);
113903 return SQLITE_OK;
113904 }
113905
113906
@@ -112687,27 +114357,22 @@
114357
114358 /* Add the default collation sequence BINARY. BINARY works for both UTF-8
114359 ** and UTF-16, so add a version for each to avoid any unnecessary
114360 ** conversions. The only error that can occur here is a malloc() failure.
114361 */
114362 createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
114363 createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
114364 createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
114365 createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
 
 
 
 
114366 if( db->mallocFailed ){
114367 goto opendb_out;
114368 }
114369 db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
114370 assert( db->pDfltColl!=0 );
114371
114372 /* Also add a UTF-8 case-insensitive collation sequence. */
114373 createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
 
114374
114375 /* Parse the filename/URI argument. */
114376 db->openFlags = flags;
114377 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
114378 if( rc!=SQLITE_OK ){
@@ -112752,14 +114417,17 @@
114417 sqlite3RegisterBuiltinFunctions(db);
114418
114419 /* Load automatic extensions - extensions that have been registered
114420 ** using the sqlite3_automatic_extension() API.
114421 */
 
114422 rc = sqlite3_errcode(db);
114423 if( rc==SQLITE_OK ){
114424 sqlite3AutoLoadExtensions(db);
114425 rc = sqlite3_errcode(db);
114426 if( rc!=SQLITE_OK ){
114427 goto opendb_out;
114428 }
114429 }
114430
114431 #ifdef SQLITE_ENABLE_FTS1
114432 if( !db->mallocFailed ){
114433 extern int sqlite3Fts1Init(sqlite3*);
@@ -112896,11 +114564,11 @@
114564 int(*xCompare)(void*,int,const void*,int,const void*)
114565 ){
114566 int rc;
114567 sqlite3_mutex_enter(db->mutex);
114568 assert( !db->mallocFailed );
114569 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
114570 rc = sqlite3ApiExit(db, rc);
114571 sqlite3_mutex_leave(db->mutex);
114572 return rc;
114573 }
114574
@@ -112916,11 +114584,11 @@
114584 void(*xDel)(void*)
114585 ){
114586 int rc;
114587 sqlite3_mutex_enter(db->mutex);
114588 assert( !db->mallocFailed );
114589 rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
114590 rc = sqlite3ApiExit(db, rc);
114591 sqlite3_mutex_leave(db->mutex);
114592 return rc;
114593 }
114594
@@ -112939,11 +114607,11 @@
114607 char *zName8;
114608 sqlite3_mutex_enter(db->mutex);
114609 assert( !db->mallocFailed );
114610 zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
114611 if( zName8 ){
114612 rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
114613 sqlite3DbFree(db, zName8);
114614 }
114615 rc = sqlite3ApiExit(db, rc);
114616 sqlite3_mutex_leave(db->mutex);
114617 return rc;
@@ -113422,19 +115090,10 @@
115090 rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
115091 break;
115092 }
115093 #endif
115094
 
 
 
 
 
 
 
 
 
115095 /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
115096 **
115097 ** Pass pFree into sqlite3ScratchFree().
115098 ** If sz>0 then allocate a scratch buffer into pNew.
115099 */
@@ -113458,10 +115117,26 @@
115117 case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
115118 sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
115119 break;
115120 }
115121
115122 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
115123 /* sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
115124 ** sqlite3_stmt*,const char**);
115125 **
115126 ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
115127 ** a string that describes the optimized parse tree. This test-control
115128 ** returns a pointer to that string.
115129 */
115130 case SQLITE_TESTCTRL_EXPLAIN_STMT: {
115131 sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
115132 const char **pzRet = va_arg(ap, const char**);
115133 *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
115134 break;
115135 }
115136 #endif
115137
115138 }
115139 va_end(ap);
115140 #endif /* SQLITE_OMIT_BUILTIN_TEST */
115141 return rc;
115142 }
@@ -113476,16 +115151,55 @@
115151 ** query parameter we seek. This routine returns the value of the zParam
115152 ** parameter if it exists. If the parameter does not exist, this routine
115153 ** returns a NULL pointer.
115154 */
115155 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
115156 if( zFilename==0 ) return 0;
115157 zFilename += sqlite3Strlen30(zFilename) + 1;
115158 while( zFilename[0] ){
115159 int x = strcmp(zFilename, zParam);
115160 zFilename += sqlite3Strlen30(zFilename) + 1;
115161 if( x==0 ) return zFilename;
115162 zFilename += sqlite3Strlen30(zFilename) + 1;
115163 }
115164 return 0;
115165 }
115166
115167 /*
115168 ** Return a boolean value for a query parameter.
115169 */
115170 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
115171 const char *z = sqlite3_uri_parameter(zFilename, zParam);
115172 return z ? sqlite3GetBoolean(z) : (bDflt!=0);
115173 }
115174
115175 /*
115176 ** Return a 64-bit integer value for a query parameter.
115177 */
115178 SQLITE_API sqlite3_int64 sqlite3_uri_int64(
115179 const char *zFilename, /* Filename as passed to xOpen */
115180 const char *zParam, /* URI parameter sought */
115181 sqlite3_int64 bDflt /* return if parameter is missing */
115182 ){
115183 const char *z = sqlite3_uri_parameter(zFilename, zParam);
115184 sqlite3_int64 v;
115185 if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
115186 bDflt = v;
115187 }
115188 return bDflt;
115189 }
115190
115191 /*
115192 ** Return the filename of the database associated with a database
115193 ** connection.
115194 */
115195 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
115196 int i;
115197 for(i=0; i<db->nDb; i++){
115198 if( db->aDb[i].pBt && sqlite3StrICmp(zDbName, db->aDb[i].zName)==0 ){
115199 return sqlite3BtreeGetFilename(db->aDb[i].pBt);
115200 }
115201 }
115202 return 0;
115203 }
115204
115205 /************** End of main.c ************************************************/
@@ -115329,10 +117043,11 @@
117043 if( *pRc==SQLITE_OK ){
117044 va_list ap;
117045 char *z;
117046 va_start(ap, zFormat);
117047 z = sqlite3_vmprintf(zFormat, ap);
117048 va_end(ap);
117049 if( z && *pz ){
117050 char *z2 = sqlite3_mprintf("%s%s", *pz, z);
117051 sqlite3_free(z);
117052 z = z2;
117053 }
@@ -121596,11 +123311,11 @@
123311 typedef struct porter_tokenizer {
123312 sqlite3_tokenizer base; /* Base class */
123313 } porter_tokenizer;
123314
123315 /*
123316 ** Class derived from sqlite3_tokenizer_cursor
123317 */
123318 typedef struct porter_tokenizer_cursor {
123319 sqlite3_tokenizer_cursor base;
123320 const char *zInput; /* input we are tokenizing */
123321 int nInput; /* size of the input */
@@ -124314,11 +126029,10 @@
126029 sqlite3_int64 iEndBlock, /* Final block of segment */
126030 const char *zRoot, /* Buffer containing root node */
126031 int nRoot, /* Size of buffer containing root node */
126032 Fts3SegReader **ppReader /* OUT: Allocated Fts3SegReader */
126033 ){
 
126034 Fts3SegReader *pReader; /* Newly allocated SegReader object */
126035 int nExtra = 0; /* Bytes to allocate segment root node */
126036
126037 assert( iStartLeaf<=iEndLeaf );
126038 if( iStartLeaf==0 ){
@@ -124342,17 +126056,12 @@
126056 memcpy(pReader->aNode, zRoot, nRoot);
126057 memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
126058 }else{
126059 pReader->iCurrentBlock = iStartLeaf-1;
126060 }
126061 *ppReader = pReader;
126062 return SQLITE_OK;
 
 
 
 
 
126063 }
126064
126065 /*
126066 ** This is a comparison function used as a qsort() callback when sorting
126067 ** an array of pending terms by term. This occurs as part of flushing
@@ -124398,19 +126107,19 @@
126107 int nTerm, /* Size of buffer zTerm */
126108 int bPrefix, /* True for a prefix iterator */
126109 Fts3SegReader **ppReader /* OUT: SegReader for pending-terms */
126110 ){
126111 Fts3SegReader *pReader = 0; /* Fts3SegReader object to return */
126112 Fts3HashElem *pE; /* Iterator variable */
126113 Fts3HashElem **aElem = 0; /* Array of term hash entries to scan */
126114 int nElem = 0; /* Size of array at aElem */
126115 int rc = SQLITE_OK; /* Return Code */
126116 Fts3Hash *pHash;
126117
126118 pHash = &p->aIndex[iIndex].hPending;
126119 if( bPrefix ){
126120 int nAlloc = 0; /* Size of allocated array at aElem */
 
126121
126122 for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
126123 char *zKey = (char *)fts3HashKey(pE);
126124 int nKey = fts3HashKeysize(pE);
126125 if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
@@ -124440,12 +126149,17 @@
126149 qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
126150 }
126151
126152 }else{
126153 /* The query is a simple term lookup that matches at most one term in
126154 ** the index. All that is required is a straight hash-lookup.
126155 **
126156 ** Because the stack address of pE may be accessed via the aElem pointer
126157 ** below, the "Fts3HashElem *pE" must be declared so that it is valid
126158 ** within this entire function, not just this "else{...}" block.
126159 */
126160 pE = fts3HashFindElem(pHash, zTerm, nTerm);
126161 if( pE ){
126162 aElem = &pE;
126163 nElem = 1;
126164 }
126165 }
@@ -129021,11 +130735,11 @@
130735 RtreeMatchArg *p;
130736 sqlite3_rtree_geometry *pGeom;
130737 int nBlob;
130738
130739 /* Check that value is actually a blob. */
130740 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
130741
130742 /* Check that the blob is roughly the right size. */
130743 nBlob = sqlite3_value_bytes(pValue);
130744 if( nBlob<(int)sizeof(RtreeMatchArg)
130745 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(double))!=0
130746
+225 -73
--- 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.9"
111
-#define SQLITE_VERSION_NUMBER 3007009
112
-#define SQLITE_SOURCE_ID "2011-10-29 19:25:08 5b82ec6fbbd2f4195ad06dd911de3817373ad5bf"
110
+#define SQLITE_VERSION "3.7.10"
111
+#define SQLITE_VERSION_NUMBER 3007010
112
+#define SQLITE_SOURCE_ID "2012-01-11 16:16:08 9e31a275ef494ea8713a1d60a15b84157e57c3ff"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -175,11 +175,11 @@
175175
176176
/*
177177
** CAPI3REF: Test To See If The Library Is Threadsafe
178178
**
179179
** ^The sqlite3_threadsafe() function returns zero if and only if
180
-** SQLite was compiled mutexing code omitted due to the
180
+** SQLite was compiled with mutexing code omitted due to the
181181
** [SQLITE_THREADSAFE] compile-time option being set to 0.
182182
**
183183
** SQLite can be compiled with or without mutexes. When
184184
** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
185185
** are enabled and SQLite is threadsafe. When the
@@ -369,11 +369,11 @@
369369
** CAPI3REF: Result Codes
370370
** KEYWORDS: SQLITE_OK {error code} {error codes}
371371
** KEYWORDS: {result code} {result codes}
372372
**
373373
** Many SQLite functions return an integer result code from the set shown
374
-** here in order to indicates success or failure.
374
+** here in order to indicate success or failure.
375375
**
376376
** New error codes may be added in future versions of SQLite.
377377
**
378378
** See also: [SQLITE_IOERR_READ | extended result codes],
379379
** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -507,11 +507,15 @@
507507
** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
508508
** that when data is appended to a file, the data is appended
509509
** first then the size of the file is extended, never the other
510510
** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
511511
** information is written to disk in the same order as calls
512
-** to xWrite().
512
+** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
513
+** after reboot following a crash or power loss, the only bytes in a
514
+** file that were written at the application level might have changed
515
+** and that adjacent bytes, even bytes within the same sector are
516
+** guaranteed to be unchanged.
513517
*/
514518
#define SQLITE_IOCAP_ATOMIC 0x00000001
515519
#define SQLITE_IOCAP_ATOMIC512 0x00000002
516520
#define SQLITE_IOCAP_ATOMIC1K 0x00000004
517521
#define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -521,10 +525,11 @@
521525
#define SQLITE_IOCAP_ATOMIC32K 0x00000080
522526
#define SQLITE_IOCAP_ATOMIC64K 0x00000100
523527
#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
524528
#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
525529
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
530
+#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
526531
527532
/*
528533
** CAPI3REF: File Locking Levels
529534
**
530535
** SQLite uses one of these integer values as the second
@@ -742,16 +747,16 @@
742747
** opcode as doing so may disrupt the operation of the specialized VFSes
743748
** that do require it.
744749
**
745750
** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
746751
** retry counts and intervals for certain disk I/O operations for the
747
-** windows [VFS] in order to work to provide robustness against
752
+** windows [VFS] in order to provide robustness in the presence of
748753
** anti-virus programs. By default, the windows VFS will retry file read,
749
-** file write, and file delete opertions up to 10 times, with a delay
754
+** file write, and file delete operations up to 10 times, with a delay
750755
** of 25 milliseconds before the first retry and with the delay increasing
751756
** by an additional 25 milliseconds with each subsequent retry. This
752
-** opcode allows those to values (10 retries and 25 milliseconds of delay)
757
+** opcode allows these two values (10 retries and 25 milliseconds of delay)
753758
** to be adjusted. The values are changed for all database connections
754759
** within the same process. The argument is a pointer to an array of two
755760
** integers where the first integer i the new retry count and the second
756761
** integer is the delay. If either integer is negative, then the setting
757762
** is not changed but instead the prior value of that setting is written
@@ -769,27 +774,49 @@
769774
** in order for the database to be readable. The fourth parameter to
770775
** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771776
** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772777
** WAL mode. If the integer is -1, then it is overwritten with the current
773778
** WAL persistence setting.
779
+**
780
+** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
781
+** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
782
+** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
783
+** xDeviceCharacteristics methods. The fourth parameter to
784
+** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
785
+** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
786
+** mode. If the integer is -1, then it is overwritten with the current
787
+** zero-damage mode setting.
774788
**
775789
** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
776790
** a write transaction to indicate that, unless it is rolled back for some
777791
** reason, the entire database file will be overwritten by the current
778792
** transaction. This is used by VACUUM operations.
793
+**
794
+** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
795
+** all [VFSes] in the VFS stack. The names are of all VFS shims and the
796
+** final bottom-level VFS are written into memory obtained from
797
+** [sqlite3_malloc()] and the result is stored in the char* variable
798
+** that the fourth parameter of [sqlite3_file_control()] points to.
799
+** The caller is responsible for freeing the memory when done. As with
800
+** all file-control actions, there is no guarantee that this will actually
801
+** do anything. Callers should initialize the char* variable to a NULL
802
+** pointer in case this file-control is not implemented. This file-control
803
+** is intended for diagnostic use only.
779804
*/
780
-#define SQLITE_FCNTL_LOCKSTATE 1
781
-#define SQLITE_GET_LOCKPROXYFILE 2
782
-#define SQLITE_SET_LOCKPROXYFILE 3
783
-#define SQLITE_LAST_ERRNO 4
784
-#define SQLITE_FCNTL_SIZE_HINT 5
785
-#define SQLITE_FCNTL_CHUNK_SIZE 6
786
-#define SQLITE_FCNTL_FILE_POINTER 7
787
-#define SQLITE_FCNTL_SYNC_OMITTED 8
788
-#define SQLITE_FCNTL_WIN32_AV_RETRY 9
789
-#define SQLITE_FCNTL_PERSIST_WAL 10
790
-#define SQLITE_FCNTL_OVERWRITE 11
805
+#define SQLITE_FCNTL_LOCKSTATE 1
806
+#define SQLITE_GET_LOCKPROXYFILE 2
807
+#define SQLITE_SET_LOCKPROXYFILE 3
808
+#define SQLITE_LAST_ERRNO 4
809
+#define SQLITE_FCNTL_SIZE_HINT 5
810
+#define SQLITE_FCNTL_CHUNK_SIZE 6
811
+#define SQLITE_FCNTL_FILE_POINTER 7
812
+#define SQLITE_FCNTL_SYNC_OMITTED 8
813
+#define SQLITE_FCNTL_WIN32_AV_RETRY 9
814
+#define SQLITE_FCNTL_PERSIST_WAL 10
815
+#define SQLITE_FCNTL_OVERWRITE 11
816
+#define SQLITE_FCNTL_VFSNAME 12
817
+#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
791818
792819
/*
793820
** CAPI3REF: Mutex Handle
794821
**
795822
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -840,11 +867,11 @@
840867
** ^SQLite guarantees that the zFilename parameter to xOpen
841868
** is either a NULL pointer or string obtained
842869
** from xFullPathname() with an optional suffix added.
843870
** ^If a suffix is added to the zFilename parameter, it will
844871
** consist of a single "-" character followed by no more than
845
-** 10 alphanumeric and/or "-" characters.
872
+** 11 alphanumeric and/or "-" characters.
846873
** ^SQLite further guarantees that
847874
** the string will be valid and unchanged until xClose() is
848875
** called. Because of the previous sentence,
849876
** the [sqlite3_file] can safely store a pointer to the
850877
** filename if it needs to remember the filename for some reason.
@@ -1371,11 +1398,11 @@
13711398
**
13721399
** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
13731400
** <dd> ^This option specifies a static memory buffer that SQLite can use for
13741401
** the database page cache with the default page cache implementation.
13751402
** This configuration should not be used if an application-define page
1376
-** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1403
+** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
13771404
** There are three arguments to this option: A pointer to 8-byte aligned
13781405
** memory, the size of each page buffer (sz), and the number of pages (N).
13791406
** The sz argument should be the size of the largest database page
13801407
** (a power of two between 512 and 32768) plus a little extra for each
13811408
** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1440,19 +1467,19 @@
14401467
** slots allocated to each database connection.)^ ^(This option sets the
14411468
** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
14421469
** verb to [sqlite3_db_config()] can be used to change the lookaside
14431470
** configuration on individual connections.)^ </dd>
14441471
**
1445
-** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
1472
+** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
14461473
** <dd> ^(This option takes a single argument which is a pointer to
1447
-** an [sqlite3_pcache_methods] object. This object specifies the interface
1474
+** an [sqlite3_pcache_methods2] object. This object specifies the interface
14481475
** to a custom page cache implementation.)^ ^SQLite makes a copy of the
14491476
** object and uses it for page cache memory allocations.</dd>
14501477
**
1451
-** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
1478
+** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
14521479
** <dd> ^(This option takes a single argument which is a pointer to an
1453
-** [sqlite3_pcache_methods] object. SQLite copies of the current
1480
+** [sqlite3_pcache_methods2] object. SQLite copies of the current
14541481
** page cache implementation into that object.)^ </dd>
14551482
**
14561483
** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
14571484
** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
14581485
** function with a call signature of void(*)(void*,int,const char*),
@@ -1481,10 +1508,15 @@
14811508
** connection is opened. If it is globally disabled, filenames are
14821509
** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
14831510
** database connection is opened. By default, URI handling is globally
14841511
** disabled. The default value may be changed by compiling with the
14851512
** [SQLITE_USE_URI] symbol defined.
1513
+**
1514
+** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1515
+** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
1516
+** <dd> These options are obsolete and should not be used by new code.
1517
+** They are retained for backwards compatibility but are now no-ops.
14861518
** </dl>
14871519
*/
14881520
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
14891521
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
14901522
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1496,14 +1528,16 @@
14961528
#define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
14971529
#define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
14981530
#define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
14991531
/* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
15001532
#define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1501
-#define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1502
-#define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
1533
+#define SQLITE_CONFIG_PCACHE 14 /* no-op */
1534
+#define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
15031535
#define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
15041536
#define SQLITE_CONFIG_URI 17 /* int */
1537
+#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
1538
+#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
15051539
15061540
/*
15071541
** CAPI3REF: Database Connection Configuration Options
15081542
**
15091543
** These constants are the available integer configuration options that
@@ -1984,11 +2018,11 @@
19842018
** These routines all implement some additional formatting
19852019
** options that are useful for constructing SQL statements.
19862020
** All of the usual printf() formatting options apply. In addition, there
19872021
** is are "%q", "%Q", and "%z" options.
19882022
**
1989
-** ^(The %q option works like %s in that it substitutes a null-terminated
2023
+** ^(The %q option works like %s in that it substitutes a nul-terminated
19902024
** string from the argument list. But %q also doubles every '\'' character.
19912025
** %q is designed for use inside a string literal.)^ By doubling each '\''
19922026
** character it escapes that character and allows it to be inserted into
19932027
** the string.
19942028
**
@@ -2592,25 +2626,41 @@
25922626
);
25932627
25942628
/*
25952629
** CAPI3REF: Obtain Values For URI Parameters
25962630
**
2597
-** This is a utility routine, useful to VFS implementations, that checks
2631
+** These are utility routines, useful to VFS implementations, that check
25982632
** to see if a database file was a URI that contained a specific query
2599
-** parameter, and if so obtains the value of the query parameter.
2600
-**
2601
-** The zFilename argument is the filename pointer passed into the xOpen()
2602
-** method of a VFS implementation. The zParam argument is the name of the
2603
-** query parameter we seek. This routine returns the value of the zParam
2604
-** parameter if it exists. If the parameter does not exist, this routine
2605
-** returns a NULL pointer.
2606
-**
2607
-** If the zFilename argument to this function is not a pointer that SQLite
2608
-** passed into the xOpen VFS method, then the behavior of this routine
2609
-** is undefined and probably undesirable.
2633
+** parameter, and if so obtains the value of that query parameter.
2634
+**
2635
+** If F is the filename pointer passed into the xOpen() method of a VFS
2636
+** implementation and P is the name of the query parameter, then
2637
+** sqlite3_uri_parameter(F,P) returns the value of the P
2638
+** parameter if it exists or a NULL pointer if P does not appear as a
2639
+** query parameter on F. If P is a query parameter of F
2640
+** has no explicit value, then sqlite3_uri_parameter(F,P) returns
2641
+** a pointer to an empty string.
2642
+**
2643
+** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
2644
+** parameter and returns true (1) or false (0) according to the value
2645
+** of P. The value of P is true if it is "yes" or "true" or "on" or
2646
+** a non-zero number and is false otherwise. If P is not a query parameter
2647
+** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
2648
+**
2649
+** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
2650
+** 64-bit signed integer and returns that integer, or D if P does not
2651
+** exist. If the value of P is something other than an integer, then
2652
+** zero is returned.
2653
+**
2654
+** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
2655
+** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
2656
+** is not a pathname pointer that SQLite passed into the xOpen VFS method,
2657
+** then the behavior of this routine is undefined and probably undesirable.
26102658
*/
26112659
SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
2660
+SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
2661
+SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
26122662
26132663
26142664
/*
26152665
** CAPI3REF: Error Codes And Messages
26162666
**
@@ -2928,10 +2978,29 @@
29282978
** change the configuration of a database connection, they do not make
29292979
** changes to the content of the database files on disk.
29302980
*/
29312981
SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
29322982
2983
+/*
2984
+** CAPI3REF: Determine If A Prepared Statement Has Been Reset
2985
+**
2986
+** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
2987
+** [prepared statement] S has been stepped at least once using
2988
+** [sqlite3_step(S)] but has not run to completion and/or has not
2989
+** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
2990
+** interface returns false if S is a NULL pointer. If S is not a
2991
+** NULL pointer and is not a pointer to a valid [prepared statement]
2992
+** object, then the behavior is undefined and probably undesirable.
2993
+**
2994
+** This interface can be used in combination [sqlite3_next_stmt()]
2995
+** to locate all prepared statements associated with a database
2996
+** connection that are in need of being reset. This can be used,
2997
+** for example, in diagnostic routines to search for prepared
2998
+** statements that are holding a transaction open.
2999
+*/
3000
+SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3001
+
29333002
/*
29343003
** CAPI3REF: Dynamically Typed Value Object
29353004
** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
29363005
**
29373006
** SQLite uses the sqlite3_value object to represent all values
@@ -3469,11 +3538,11 @@
34693538
** of the string. ^For clarity: the values returned by
34703539
** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
34713540
** bytes in the string, not the number of characters.
34723541
**
34733542
** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3474
-** even empty strings, are always zero terminated. ^The return
3543
+** even empty strings, are always zero-terminated. ^The return
34753544
** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
34763545
**
34773546
** ^The object returned by [sqlite3_column_value()] is an
34783547
** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
34793548
** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4369,10 +4438,26 @@
43694438
** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
43704439
** create the statement in the first place.
43714440
*/
43724441
SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
43734442
4443
+/*
4444
+** CAPI3REF: Return The Filename For A Database Connection
4445
+**
4446
+** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4447
+** associated with database N of connection D. ^The main database file
4448
+** has the name "main". If there is no attached database N on the database
4449
+** connection D, or if database N is a temporary or in-memory database, then
4450
+** a NULL pointer is returned.
4451
+**
4452
+** ^The filename returned by this function is the output of the
4453
+** xFullPathname method of the [VFS]. ^In other words, the filename
4454
+** will be an absolute pathname, even if the filename used
4455
+** to open the database originally was a URI or relative pathname.
4456
+*/
4457
+SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
4458
+
43744459
/*
43754460
** CAPI3REF: Find the next prepared statement
43764461
**
43774462
** ^This interface returns a pointer to the next [prepared statement] after
43784463
** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4404,17 +4489,19 @@
44044489
** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
44054490
** return the P argument from the previous call of the same function
44064491
** on the same [database connection] D, or NULL for
44074492
** the first call for each function on D.
44084493
**
4494
+** The commit and rollback hook callbacks are not reentrant.
44094495
** The callback implementation must not do anything that will modify
44104496
** the database connection that invoked the callback. Any actions
44114497
** to modify the database connection must be deferred until after the
44124498
** completion of the [sqlite3_step()] call that triggered the commit
44134499
** or rollback hook in the first place.
4414
-** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4415
-** database connections for the meaning of "modify" in this paragraph.
4500
+** Note that running any other SQL statements, including SELECT statements,
4501
+** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
4502
+** the database connections for the meaning of "modify" in this paragraph.
44164503
**
44174504
** ^Registering a NULL function disables the callback.
44184505
**
44194506
** ^When the commit hook callback routine returns zero, the [COMMIT]
44204507
** operation is allowed to continue normally. ^If the commit hook
@@ -4523,13 +4610,28 @@
45234610
** pages to improve performance is an example of non-essential memory.
45244611
** ^sqlite3_release_memory() returns the number of bytes actually freed,
45254612
** which might be more or less than the amount requested.
45264613
** ^The sqlite3_release_memory() routine is a no-op returning zero
45274614
** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
4615
+**
4616
+** See also: [sqlite3_db_release_memory()]
45284617
*/
45294618
SQLITE_API int sqlite3_release_memory(int);
45304619
4620
+/*
4621
+** CAPI3REF: Free Memory Used By A Database Connection
4622
+**
4623
+** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4624
+** memory as possible from database connection D. Unlike the
4625
+** [sqlite3_release_memory()] interface, this interface is effect even
4626
+** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4627
+** omitted.
4628
+**
4629
+** See also: [sqlite3_release_memory()]
4630
+*/
4631
+SQLITE_API int sqlite3_db_release_memory(sqlite3*);
4632
+
45314633
/*
45324634
** CAPI3REF: Impose A Limit On Heap Size
45334635
**
45344636
** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
45354637
** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -4540,11 +4642,12 @@
45404642
** below the limit, it will exceed the limit rather than generate
45414643
** an [SQLITE_NOMEM] error. In other words, the soft heap limit
45424644
** is advisory only.
45434645
**
45444646
** ^The return value from sqlite3_soft_heap_limit64() is the size of
4545
-** the soft heap limit prior to the call. ^If the argument N is negative
4647
+** the soft heap limit prior to the call, or negative in the case of an
4648
+** error. ^If the argument N is negative
45464649
** then no change is made to the soft heap limit. Hence, the current
45474650
** size of the soft heap limit can be determined by invoking
45484651
** sqlite3_soft_heap_limit64() with a negative argument.
45494652
**
45504653
** ^If the argument N is zero then the soft heap limit is disabled.
@@ -4556,11 +4659,11 @@
45564659
** <li> The soft heap limit is set to zero.
45574660
** <li> Memory accounting is disabled using a combination of the
45584661
** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
45594662
** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
45604663
** <li> An alternative page cache implementation is specified using
4561
-** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
4664
+** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
45624665
** <li> The page cache allocates from its own memory pool supplied
45634666
** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
45644667
** from the heap.
45654668
** </ul>)^
45664669
**
@@ -5298,19 +5401,19 @@
52985401
** is selected automatically at compile-time. ^(The following
52995402
** implementations are available in the SQLite core:
53005403
**
53015404
** <ul>
53025405
** <li> SQLITE_MUTEX_OS2
5303
-** <li> SQLITE_MUTEX_PTHREAD
5406
+** <li> SQLITE_MUTEX_PTHREADS
53045407
** <li> SQLITE_MUTEX_W32
53055408
** <li> SQLITE_MUTEX_NOOP
53065409
** </ul>)^
53075410
**
53085411
** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
53095412
** that does no real locking and is appropriate for use in
53105413
** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5311
-** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5414
+** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
53125415
** are appropriate for use on OS/2, Unix, and Windows.
53135416
**
53145417
** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
53155418
** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
53165419
** implementation is included with the library. In this case the
@@ -5496,11 +5599,11 @@
54965599
** defined and if NDEBUG is not defined.
54975600
**
54985601
** ^These routines should return true if the mutex in their argument
54995602
** is held or not held, respectively, by the calling thread.
55005603
**
5501
-** ^The implementation is not required to provided versions of these
5604
+** ^The implementation is not required to provide versions of these
55025605
** routines that actually work. If the implementation does not provide working
55035606
** versions of these routines, it should at least provide stubs that always
55045607
** return true so that one does not get spurious assertion failures.
55055608
**
55065609
** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -5624,13 +5727,13 @@
56245727
#define SQLITE_TESTCTRL_ASSERT 12
56255728
#define SQLITE_TESTCTRL_ALWAYS 13
56265729
#define SQLITE_TESTCTRL_RESERVE 14
56275730
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
56285731
#define SQLITE_TESTCTRL_ISKEYWORD 16
5629
-#define SQLITE_TESTCTRL_PGHDRSZ 17
5630
-#define SQLITE_TESTCTRL_SCRATCHMALLOC 18
5631
-#define SQLITE_TESTCTRL_LOCALTIME_FAULT 19
5732
+#define SQLITE_TESTCTRL_SCRATCHMALLOC 17
5733
+#define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
5734
+#define SQLITE_TESTCTRL_EXPLAIN_STMT 19
56325735
#define SQLITE_TESTCTRL_LAST 19
56335736
56345737
/*
56355738
** CAPI3REF: SQLite Runtime Status
56365739
**
@@ -5929,21 +6032,37 @@
59296032
** the pluggable module. The SQLite core has no knowledge of
59306033
** its size or internal structure and never deals with the
59316034
** sqlite3_pcache object except by holding and passing pointers
59326035
** to the object.
59336036
**
5934
-** See [sqlite3_pcache_methods] for additional information.
6037
+** See [sqlite3_pcache_methods2] for additional information.
59356038
*/
59366039
typedef struct sqlite3_pcache sqlite3_pcache;
6040
+
6041
+/*
6042
+** CAPI3REF: Custom Page Cache Object
6043
+**
6044
+** The sqlite3_pcache_page object represents a single page in the
6045
+** page cache. The page cache will allocate instances of this
6046
+** object. Various methods of the page cache use pointers to instances
6047
+** of this object as parameters or as their return value.
6048
+**
6049
+** See [sqlite3_pcache_methods2] for additional information.
6050
+*/
6051
+typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6052
+struct sqlite3_pcache_page {
6053
+ void *pBuf; /* The content of the page */
6054
+ void *pExtra; /* Extra information associated with the page */
6055
+};
59376056
59386057
/*
59396058
** CAPI3REF: Application Defined Page Cache.
59406059
** KEYWORDS: {page cache}
59416060
**
5942
-** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
6061
+** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
59436062
** register an alternative page cache implementation by passing in an
5944
-** instance of the sqlite3_pcache_methods structure.)^
6063
+** instance of the sqlite3_pcache_methods2 structure.)^
59456064
** In many applications, most of the heap memory allocated by
59466065
** SQLite is used for the page cache.
59476066
** By implementing a
59486067
** custom page cache using this API, an application can better control
59496068
** the amount of memory consumed by SQLite, the way in which
@@ -5953,20 +6072,20 @@
59536072
**
59546073
** The alternative page cache mechanism is an
59556074
** extreme measure that is only needed by the most demanding applications.
59566075
** The built-in page cache is recommended for most uses.
59576076
**
5958
-** ^(The contents of the sqlite3_pcache_methods structure are copied to an
6077
+** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
59596078
** internal buffer by SQLite within the call to [sqlite3_config]. Hence
59606079
** the application may discard the parameter after the call to
59616080
** [sqlite3_config()] returns.)^
59626081
**
59636082
** [[the xInit() page cache method]]
59646083
** ^(The xInit() method is called once for each effective
59656084
** call to [sqlite3_initialize()])^
59666085
** (usually only once during the lifetime of the process). ^(The xInit()
5967
-** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
6086
+** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
59686087
** The intent of the xInit() method is to set up global data structures
59696088
** required by the custom page cache implementation.
59706089
** ^(If the xInit() method is NULL, then the
59716090
** built-in default page cache is used instead of the application defined
59726091
** page cache.)^
@@ -5989,21 +6108,19 @@
59896108
** [[the xCreate() page cache methods]]
59906109
** ^SQLite invokes the xCreate() method to construct a new cache instance.
59916110
** SQLite will typically create one cache instance for each open database file,
59926111
** though this is not guaranteed. ^The
59936112
** first parameter, szPage, is the size in bytes of the pages that must
5994
-** be allocated by the cache. ^szPage will not be a power of two. ^szPage
5995
-** will the page size of the database file that is to be cached plus an
5996
-** increment (here called "R") of less than 250. SQLite will use the
5997
-** extra R bytes on each page to store metadata about the underlying
5998
-** database page on disk. The value of R depends
6113
+** be allocated by the cache. ^szPage will always a power of two. ^The
6114
+** second parameter szExtra is a number of bytes of extra storage
6115
+** associated with each page cache entry. ^The szExtra parameter will
6116
+** a number less than 250. SQLite will use the
6117
+** extra szExtra bytes on each page to store metadata about the underlying
6118
+** database page on disk. The value passed into szExtra depends
59996119
** on the SQLite version, the target platform, and how SQLite was compiled.
6000
-** ^(R is constant for a particular build of SQLite. Except, there are two
6001
-** distinct values of R when SQLite is compiled with the proprietary
6002
-** ZIPVFS extension.)^ ^The second argument to
6003
-** xCreate(), bPurgeable, is true if the cache being created will
6004
-** be used to cache database pages of a file stored on disk, or
6120
+** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6121
+** created will be used to cache database pages of a file stored on disk, or
60056122
** false if it is used for an in-memory database. The cache implementation
60066123
** does not have to do anything special based with the value of bPurgeable;
60076124
** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
60086125
** never invoke xUnpin() except to deliberately delete a page.
60096126
** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6023,15 +6140,20 @@
60236140
** The xPagecount() method must return the number of pages currently
60246141
** stored in the cache, both pinned and unpinned.
60256142
**
60266143
** [[the xFetch() page cache methods]]
60276144
** The xFetch() method locates a page in the cache and returns a pointer to
6028
-** the page, or a NULL pointer.
6029
-** A "page", in this context, means a buffer of szPage bytes aligned at an
6030
-** 8-byte boundary. The page to be fetched is determined by the key. ^The
6031
-** minimum key value is 1. After it has been retrieved using xFetch, the page
6032
-** is considered to be "pinned".
6145
+** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6146
+** The pBuf element of the returned sqlite3_pcache_page object will be a
6147
+** pointer to a buffer of szPage bytes used to store the content of a
6148
+** single database page. The pExtra element of sqlite3_pcache_page will be
6149
+** a pointer to the szExtra bytes of extra storage that SQLite has requested
6150
+** for each entry in the page cache.
6151
+**
6152
+** The page to be fetched is determined by the key. ^The minimum key value
6153
+** is 1. After it has been retrieved using xFetch, the page is considered
6154
+** to be "pinned".
60336155
**
60346156
** If the requested page is already in the page cache, then the page cache
60356157
** implementation must return a pointer to the page buffer with its content
60366158
** intact. If the requested page is not already in the cache, then the
60376159
** cache implementation should use the value of the createFlag
@@ -6080,12 +6202,41 @@
60806202
**
60816203
** [[the xDestroy() page cache method]]
60826204
** ^The xDestroy() method is used to delete a cache allocated by xCreate().
60836205
** All resources associated with the specified cache should be freed. ^After
60846206
** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6085
-** handle invalid, and will not use it with any other sqlite3_pcache_methods
6207
+** handle invalid, and will not use it with any other sqlite3_pcache_methods2
60866208
** functions.
6209
+**
6210
+** [[the xShrink() page cache method]]
6211
+** ^SQLite invokes the xShrink() method when it wants the page cache to
6212
+** free up as much of heap memory as possible. The page cache implementation
6213
+** is not obligated to free any memory, but well-behaved implementions should
6214
+** do their best.
6215
+*/
6216
+typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6217
+struct sqlite3_pcache_methods2 {
6218
+ int iVersion;
6219
+ void *pArg;
6220
+ int (*xInit)(void*);
6221
+ void (*xShutdown)(void*);
6222
+ sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6223
+ void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6224
+ int (*xPagecount)(sqlite3_pcache*);
6225
+ sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6226
+ void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6227
+ void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6228
+ unsigned oldKey, unsigned newKey);
6229
+ void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6230
+ void (*xDestroy)(sqlite3_pcache*);
6231
+ void (*xShrink)(sqlite3_pcache*);
6232
+};
6233
+
6234
+/*
6235
+** This is the obsolete pcache_methods object that has now been replaced
6236
+** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
6237
+** retained in the header file for backwards compatibility only.
60876238
*/
60886239
typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
60896240
struct sqlite3_pcache_methods {
60906241
void *pArg;
60916242
int (*xInit)(void*);
@@ -6097,10 +6248,11 @@
60976248
void (*xUnpin)(sqlite3_pcache*, void*, int discard);
60986249
void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
60996250
void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
61006251
void (*xDestroy)(sqlite3_pcache*);
61016252
};
6253
+
61026254
61036255
/*
61046256
** CAPI3REF: Online Backup Object
61056257
**
61066258
** The sqlite3_backup object records state information about an ongoing
61076259
--- 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.9"
111 #define SQLITE_VERSION_NUMBER 3007009
112 #define SQLITE_SOURCE_ID "2011-10-29 19:25:08 5b82ec6fbbd2f4195ad06dd911de3817373ad5bf"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -175,11 +175,11 @@
175
176 /*
177 ** CAPI3REF: Test To See If The Library Is Threadsafe
178 **
179 ** ^The sqlite3_threadsafe() function returns zero if and only if
180 ** SQLite was compiled mutexing code omitted due to the
181 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
182 **
183 ** SQLite can be compiled with or without mutexes. When
184 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
185 ** are enabled and SQLite is threadsafe. When the
@@ -369,11 +369,11 @@
369 ** CAPI3REF: Result Codes
370 ** KEYWORDS: SQLITE_OK {error code} {error codes}
371 ** KEYWORDS: {result code} {result codes}
372 **
373 ** Many SQLite functions return an integer result code from the set shown
374 ** here in order to indicates success or failure.
375 **
376 ** New error codes may be added in future versions of SQLite.
377 **
378 ** See also: [SQLITE_IOERR_READ | extended result codes],
379 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -507,11 +507,15 @@
507 ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
508 ** that when data is appended to a file, the data is appended
509 ** first then the size of the file is extended, never the other
510 ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
511 ** information is written to disk in the same order as calls
512 ** to xWrite().
 
 
 
 
513 */
514 #define SQLITE_IOCAP_ATOMIC 0x00000001
515 #define SQLITE_IOCAP_ATOMIC512 0x00000002
516 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
517 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -521,10 +525,11 @@
521 #define SQLITE_IOCAP_ATOMIC32K 0x00000080
522 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
523 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
524 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
525 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
 
526
527 /*
528 ** CAPI3REF: File Locking Levels
529 **
530 ** SQLite uses one of these integer values as the second
@@ -742,16 +747,16 @@
742 ** opcode as doing so may disrupt the operation of the specialized VFSes
743 ** that do require it.
744 **
745 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
746 ** retry counts and intervals for certain disk I/O operations for the
747 ** windows [VFS] in order to work to provide robustness against
748 ** anti-virus programs. By default, the windows VFS will retry file read,
749 ** file write, and file delete opertions up to 10 times, with a delay
750 ** of 25 milliseconds before the first retry and with the delay increasing
751 ** by an additional 25 milliseconds with each subsequent retry. This
752 ** opcode allows those to values (10 retries and 25 milliseconds of delay)
753 ** to be adjusted. The values are changed for all database connections
754 ** within the same process. The argument is a pointer to an array of two
755 ** integers where the first integer i the new retry count and the second
756 ** integer is the delay. If either integer is negative, then the setting
757 ** is not changed but instead the prior value of that setting is written
@@ -769,27 +774,49 @@
769 ** in order for the database to be readable. The fourth parameter to
770 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772 ** WAL mode. If the integer is -1, then it is overwritten with the current
773 ** WAL persistence setting.
 
 
 
 
 
 
 
 
 
774 **
775 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
776 ** a write transaction to indicate that, unless it is rolled back for some
777 ** reason, the entire database file will be overwritten by the current
778 ** transaction. This is used by VACUUM operations.
 
 
 
 
 
 
 
 
 
 
 
779 */
780 #define SQLITE_FCNTL_LOCKSTATE 1
781 #define SQLITE_GET_LOCKPROXYFILE 2
782 #define SQLITE_SET_LOCKPROXYFILE 3
783 #define SQLITE_LAST_ERRNO 4
784 #define SQLITE_FCNTL_SIZE_HINT 5
785 #define SQLITE_FCNTL_CHUNK_SIZE 6
786 #define SQLITE_FCNTL_FILE_POINTER 7
787 #define SQLITE_FCNTL_SYNC_OMITTED 8
788 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
789 #define SQLITE_FCNTL_PERSIST_WAL 10
790 #define SQLITE_FCNTL_OVERWRITE 11
 
 
791
792 /*
793 ** CAPI3REF: Mutex Handle
794 **
795 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -840,11 +867,11 @@
840 ** ^SQLite guarantees that the zFilename parameter to xOpen
841 ** is either a NULL pointer or string obtained
842 ** from xFullPathname() with an optional suffix added.
843 ** ^If a suffix is added to the zFilename parameter, it will
844 ** consist of a single "-" character followed by no more than
845 ** 10 alphanumeric and/or "-" characters.
846 ** ^SQLite further guarantees that
847 ** the string will be valid and unchanged until xClose() is
848 ** called. Because of the previous sentence,
849 ** the [sqlite3_file] can safely store a pointer to the
850 ** filename if it needs to remember the filename for some reason.
@@ -1371,11 +1398,11 @@
1371 **
1372 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1373 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1374 ** the database page cache with the default page cache implementation.
1375 ** This configuration should not be used if an application-define page
1376 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE option.
1377 ** There are three arguments to this option: A pointer to 8-byte aligned
1378 ** memory, the size of each page buffer (sz), and the number of pages (N).
1379 ** The sz argument should be the size of the largest database page
1380 ** (a power of two between 512 and 32768) plus a little extra for each
1381 ** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1440,19 +1467,19 @@
1440 ** slots allocated to each database connection.)^ ^(This option sets the
1441 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1442 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1443 ** configuration on individual connections.)^ </dd>
1444 **
1445 ** [[SQLITE_CONFIG_PCACHE]] <dt>SQLITE_CONFIG_PCACHE</dt>
1446 ** <dd> ^(This option takes a single argument which is a pointer to
1447 ** an [sqlite3_pcache_methods] object. This object specifies the interface
1448 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1449 ** object and uses it for page cache memory allocations.</dd>
1450 **
1451 ** [[SQLITE_CONFIG_GETPCACHE]] <dt>SQLITE_CONFIG_GETPCACHE</dt>
1452 ** <dd> ^(This option takes a single argument which is a pointer to an
1453 ** [sqlite3_pcache_methods] object. SQLite copies of the current
1454 ** page cache implementation into that object.)^ </dd>
1455 **
1456 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1457 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1458 ** function with a call signature of void(*)(void*,int,const char*),
@@ -1481,10 +1508,15 @@
1481 ** connection is opened. If it is globally disabled, filenames are
1482 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1483 ** database connection is opened. By default, URI handling is globally
1484 ** disabled. The default value may be changed by compiling with the
1485 ** [SQLITE_USE_URI] symbol defined.
 
 
 
 
 
1486 ** </dl>
1487 */
1488 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1489 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1490 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1496,14 +1528,16 @@
1496 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1497 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1498 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1499 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1500 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1501 #define SQLITE_CONFIG_PCACHE 14 /* sqlite3_pcache_methods* */
1502 #define SQLITE_CONFIG_GETPCACHE 15 /* sqlite3_pcache_methods* */
1503 #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
1504 #define SQLITE_CONFIG_URI 17 /* int */
 
 
1505
1506 /*
1507 ** CAPI3REF: Database Connection Configuration Options
1508 **
1509 ** These constants are the available integer configuration options that
@@ -1984,11 +2018,11 @@
1984 ** These routines all implement some additional formatting
1985 ** options that are useful for constructing SQL statements.
1986 ** All of the usual printf() formatting options apply. In addition, there
1987 ** is are "%q", "%Q", and "%z" options.
1988 **
1989 ** ^(The %q option works like %s in that it substitutes a null-terminated
1990 ** string from the argument list. But %q also doubles every '\'' character.
1991 ** %q is designed for use inside a string literal.)^ By doubling each '\''
1992 ** character it escapes that character and allows it to be inserted into
1993 ** the string.
1994 **
@@ -2592,25 +2626,41 @@
2592 );
2593
2594 /*
2595 ** CAPI3REF: Obtain Values For URI Parameters
2596 **
2597 ** This is a utility routine, useful to VFS implementations, that checks
2598 ** to see if a database file was a URI that contained a specific query
2599 ** parameter, and if so obtains the value of the query parameter.
2600 **
2601 ** The zFilename argument is the filename pointer passed into the xOpen()
2602 ** method of a VFS implementation. The zParam argument is the name of the
2603 ** query parameter we seek. This routine returns the value of the zParam
2604 ** parameter if it exists. If the parameter does not exist, this routine
2605 ** returns a NULL pointer.
2606 **
2607 ** If the zFilename argument to this function is not a pointer that SQLite
2608 ** passed into the xOpen VFS method, then the behavior of this routine
2609 ** is undefined and probably undesirable.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2610 */
2611 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
 
 
2612
2613
2614 /*
2615 ** CAPI3REF: Error Codes And Messages
2616 **
@@ -2928,10 +2978,29 @@
2928 ** change the configuration of a database connection, they do not make
2929 ** changes to the content of the database files on disk.
2930 */
2931 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
2932
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2933 /*
2934 ** CAPI3REF: Dynamically Typed Value Object
2935 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
2936 **
2937 ** SQLite uses the sqlite3_value object to represent all values
@@ -3469,11 +3538,11 @@
3469 ** of the string. ^For clarity: the values returned by
3470 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3471 ** bytes in the string, not the number of characters.
3472 **
3473 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3474 ** even empty strings, are always zero terminated. ^The return
3475 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3476 **
3477 ** ^The object returned by [sqlite3_column_value()] is an
3478 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
3479 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4369,10 +4438,26 @@
4369 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4370 ** create the statement in the first place.
4371 */
4372 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4373
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4374 /*
4375 ** CAPI3REF: Find the next prepared statement
4376 **
4377 ** ^This interface returns a pointer to the next [prepared statement] after
4378 ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4404,17 +4489,19 @@
4404 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4405 ** return the P argument from the previous call of the same function
4406 ** on the same [database connection] D, or NULL for
4407 ** the first call for each function on D.
4408 **
 
4409 ** The callback implementation must not do anything that will modify
4410 ** the database connection that invoked the callback. Any actions
4411 ** to modify the database connection must be deferred until after the
4412 ** completion of the [sqlite3_step()] call that triggered the commit
4413 ** or rollback hook in the first place.
4414 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4415 ** database connections for the meaning of "modify" in this paragraph.
 
4416 **
4417 ** ^Registering a NULL function disables the callback.
4418 **
4419 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4420 ** operation is allowed to continue normally. ^If the commit hook
@@ -4523,13 +4610,28 @@
4523 ** pages to improve performance is an example of non-essential memory.
4524 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
4525 ** which might be more or less than the amount requested.
4526 ** ^The sqlite3_release_memory() routine is a no-op returning zero
4527 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
 
 
4528 */
4529 SQLITE_API int sqlite3_release_memory(int);
4530
 
 
 
 
 
 
 
 
 
 
 
 
 
4531 /*
4532 ** CAPI3REF: Impose A Limit On Heap Size
4533 **
4534 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
4535 ** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -4540,11 +4642,12 @@
4540 ** below the limit, it will exceed the limit rather than generate
4541 ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
4542 ** is advisory only.
4543 **
4544 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
4545 ** the soft heap limit prior to the call. ^If the argument N is negative
 
4546 ** then no change is made to the soft heap limit. Hence, the current
4547 ** size of the soft heap limit can be determined by invoking
4548 ** sqlite3_soft_heap_limit64() with a negative argument.
4549 **
4550 ** ^If the argument N is zero then the soft heap limit is disabled.
@@ -4556,11 +4659,11 @@
4556 ** <li> The soft heap limit is set to zero.
4557 ** <li> Memory accounting is disabled using a combination of the
4558 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
4559 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
4560 ** <li> An alternative page cache implementation is specified using
4561 ** [sqlite3_config]([SQLITE_CONFIG_PCACHE],...).
4562 ** <li> The page cache allocates from its own memory pool supplied
4563 ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
4564 ** from the heap.
4565 ** </ul>)^
4566 **
@@ -5298,19 +5401,19 @@
5298 ** is selected automatically at compile-time. ^(The following
5299 ** implementations are available in the SQLite core:
5300 **
5301 ** <ul>
5302 ** <li> SQLITE_MUTEX_OS2
5303 ** <li> SQLITE_MUTEX_PTHREAD
5304 ** <li> SQLITE_MUTEX_W32
5305 ** <li> SQLITE_MUTEX_NOOP
5306 ** </ul>)^
5307 **
5308 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5309 ** that does no real locking and is appropriate for use in
5310 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5311 ** SQLITE_MUTEX_PTHREAD, and SQLITE_MUTEX_W32 implementations
5312 ** are appropriate for use on OS/2, Unix, and Windows.
5313 **
5314 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5315 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5316 ** implementation is included with the library. In this case the
@@ -5496,11 +5599,11 @@
5496 ** defined and if NDEBUG is not defined.
5497 **
5498 ** ^These routines should return true if the mutex in their argument
5499 ** is held or not held, respectively, by the calling thread.
5500 **
5501 ** ^The implementation is not required to provided versions of these
5502 ** routines that actually work. If the implementation does not provide working
5503 ** versions of these routines, it should at least provide stubs that always
5504 ** return true so that one does not get spurious assertion failures.
5505 **
5506 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -5624,13 +5727,13 @@
5624 #define SQLITE_TESTCTRL_ASSERT 12
5625 #define SQLITE_TESTCTRL_ALWAYS 13
5626 #define SQLITE_TESTCTRL_RESERVE 14
5627 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
5628 #define SQLITE_TESTCTRL_ISKEYWORD 16
5629 #define SQLITE_TESTCTRL_PGHDRSZ 17
5630 #define SQLITE_TESTCTRL_SCRATCHMALLOC 18
5631 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 19
5632 #define SQLITE_TESTCTRL_LAST 19
5633
5634 /*
5635 ** CAPI3REF: SQLite Runtime Status
5636 **
@@ -5929,21 +6032,37 @@
5929 ** the pluggable module. The SQLite core has no knowledge of
5930 ** its size or internal structure and never deals with the
5931 ** sqlite3_pcache object except by holding and passing pointers
5932 ** to the object.
5933 **
5934 ** See [sqlite3_pcache_methods] for additional information.
5935 */
5936 typedef struct sqlite3_pcache sqlite3_pcache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5937
5938 /*
5939 ** CAPI3REF: Application Defined Page Cache.
5940 ** KEYWORDS: {page cache}
5941 **
5942 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE], ...) interface can
5943 ** register an alternative page cache implementation by passing in an
5944 ** instance of the sqlite3_pcache_methods structure.)^
5945 ** In many applications, most of the heap memory allocated by
5946 ** SQLite is used for the page cache.
5947 ** By implementing a
5948 ** custom page cache using this API, an application can better control
5949 ** the amount of memory consumed by SQLite, the way in which
@@ -5953,20 +6072,20 @@
5953 **
5954 ** The alternative page cache mechanism is an
5955 ** extreme measure that is only needed by the most demanding applications.
5956 ** The built-in page cache is recommended for most uses.
5957 **
5958 ** ^(The contents of the sqlite3_pcache_methods structure are copied to an
5959 ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
5960 ** the application may discard the parameter after the call to
5961 ** [sqlite3_config()] returns.)^
5962 **
5963 ** [[the xInit() page cache method]]
5964 ** ^(The xInit() method is called once for each effective
5965 ** call to [sqlite3_initialize()])^
5966 ** (usually only once during the lifetime of the process). ^(The xInit()
5967 ** method is passed a copy of the sqlite3_pcache_methods.pArg value.)^
5968 ** The intent of the xInit() method is to set up global data structures
5969 ** required by the custom page cache implementation.
5970 ** ^(If the xInit() method is NULL, then the
5971 ** built-in default page cache is used instead of the application defined
5972 ** page cache.)^
@@ -5989,21 +6108,19 @@
5989 ** [[the xCreate() page cache methods]]
5990 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
5991 ** SQLite will typically create one cache instance for each open database file,
5992 ** though this is not guaranteed. ^The
5993 ** first parameter, szPage, is the size in bytes of the pages that must
5994 ** be allocated by the cache. ^szPage will not be a power of two. ^szPage
5995 ** will the page size of the database file that is to be cached plus an
5996 ** increment (here called "R") of less than 250. SQLite will use the
5997 ** extra R bytes on each page to store metadata about the underlying
5998 ** database page on disk. The value of R depends
 
5999 ** on the SQLite version, the target platform, and how SQLite was compiled.
6000 ** ^(R is constant for a particular build of SQLite. Except, there are two
6001 ** distinct values of R when SQLite is compiled with the proprietary
6002 ** ZIPVFS extension.)^ ^The second argument to
6003 ** xCreate(), bPurgeable, is true if the cache being created will
6004 ** be used to cache database pages of a file stored on disk, or
6005 ** false if it is used for an in-memory database. The cache implementation
6006 ** does not have to do anything special based with the value of bPurgeable;
6007 ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
6008 ** never invoke xUnpin() except to deliberately delete a page.
6009 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6023,15 +6140,20 @@
6023 ** The xPagecount() method must return the number of pages currently
6024 ** stored in the cache, both pinned and unpinned.
6025 **
6026 ** [[the xFetch() page cache methods]]
6027 ** The xFetch() method locates a page in the cache and returns a pointer to
6028 ** the page, or a NULL pointer.
6029 ** A "page", in this context, means a buffer of szPage bytes aligned at an
6030 ** 8-byte boundary. The page to be fetched is determined by the key. ^The
6031 ** minimum key value is 1. After it has been retrieved using xFetch, the page
6032 ** is considered to be "pinned".
 
 
 
 
 
6033 **
6034 ** If the requested page is already in the page cache, then the page cache
6035 ** implementation must return a pointer to the page buffer with its content
6036 ** intact. If the requested page is not already in the cache, then the
6037 ** cache implementation should use the value of the createFlag
@@ -6080,12 +6202,41 @@
6080 **
6081 ** [[the xDestroy() page cache method]]
6082 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6083 ** All resources associated with the specified cache should be freed. ^After
6084 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6085 ** handle invalid, and will not use it with any other sqlite3_pcache_methods
6086 ** functions.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6087 */
6088 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6089 struct sqlite3_pcache_methods {
6090 void *pArg;
6091 int (*xInit)(void*);
@@ -6097,10 +6248,11 @@
6097 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6098 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6099 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6100 void (*xDestroy)(sqlite3_pcache*);
6101 };
 
6102
6103 /*
6104 ** CAPI3REF: Online Backup Object
6105 **
6106 ** The sqlite3_backup object records state information about an ongoing
6107
--- 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.10"
111 #define SQLITE_VERSION_NUMBER 3007010
112 #define SQLITE_SOURCE_ID "2012-01-11 16:16:08 9e31a275ef494ea8713a1d60a15b84157e57c3ff"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -175,11 +175,11 @@
175
176 /*
177 ** CAPI3REF: Test To See If The Library Is Threadsafe
178 **
179 ** ^The sqlite3_threadsafe() function returns zero if and only if
180 ** SQLite was compiled with mutexing code omitted due to the
181 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
182 **
183 ** SQLite can be compiled with or without mutexes. When
184 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
185 ** are enabled and SQLite is threadsafe. When the
@@ -369,11 +369,11 @@
369 ** CAPI3REF: Result Codes
370 ** KEYWORDS: SQLITE_OK {error code} {error codes}
371 ** KEYWORDS: {result code} {result codes}
372 **
373 ** Many SQLite functions return an integer result code from the set shown
374 ** here in order to indicate success or failure.
375 **
376 ** New error codes may be added in future versions of SQLite.
377 **
378 ** See also: [SQLITE_IOERR_READ | extended result codes],
379 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
@@ -507,11 +507,15 @@
507 ** nnn are atomic. The SQLITE_IOCAP_SAFE_APPEND value means
508 ** that when data is appended to a file, the data is appended
509 ** first then the size of the file is extended, never the other
510 ** way around. The SQLITE_IOCAP_SEQUENTIAL property means that
511 ** information is written to disk in the same order as calls
512 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
513 ** after reboot following a crash or power loss, the only bytes in a
514 ** file that were written at the application level might have changed
515 ** and that adjacent bytes, even bytes within the same sector are
516 ** guaranteed to be unchanged.
517 */
518 #define SQLITE_IOCAP_ATOMIC 0x00000001
519 #define SQLITE_IOCAP_ATOMIC512 0x00000002
520 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
521 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -521,10 +525,11 @@
525 #define SQLITE_IOCAP_ATOMIC32K 0x00000080
526 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
527 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
528 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
529 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
530 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
531
532 /*
533 ** CAPI3REF: File Locking Levels
534 **
535 ** SQLite uses one of these integer values as the second
@@ -742,16 +747,16 @@
747 ** opcode as doing so may disrupt the operation of the specialized VFSes
748 ** that do require it.
749 **
750 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
751 ** retry counts and intervals for certain disk I/O operations for the
752 ** windows [VFS] in order to provide robustness in the presence of
753 ** anti-virus programs. By default, the windows VFS will retry file read,
754 ** file write, and file delete operations up to 10 times, with a delay
755 ** of 25 milliseconds before the first retry and with the delay increasing
756 ** by an additional 25 milliseconds with each subsequent retry. This
757 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
758 ** to be adjusted. The values are changed for all database connections
759 ** within the same process. The argument is a pointer to an array of two
760 ** integers where the first integer i the new retry count and the second
761 ** integer is the delay. If either integer is negative, then the setting
762 ** is not changed but instead the prior value of that setting is written
@@ -769,27 +774,49 @@
774 ** in order for the database to be readable. The fourth parameter to
775 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
776 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
777 ** WAL mode. If the integer is -1, then it is overwritten with the current
778 ** WAL persistence setting.
779 **
780 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
781 ** persistent "powersafe-overwrite" or "PSOW" setting. The PSOW setting
782 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
783 ** xDeviceCharacteristics methods. The fourth parameter to
784 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
785 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
786 ** mode. If the integer is -1, then it is overwritten with the current
787 ** zero-damage mode setting.
788 **
789 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
790 ** a write transaction to indicate that, unless it is rolled back for some
791 ** reason, the entire database file will be overwritten by the current
792 ** transaction. This is used by VACUUM operations.
793 **
794 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
795 ** all [VFSes] in the VFS stack. The names are of all VFS shims and the
796 ** final bottom-level VFS are written into memory obtained from
797 ** [sqlite3_malloc()] and the result is stored in the char* variable
798 ** that the fourth parameter of [sqlite3_file_control()] points to.
799 ** The caller is responsible for freeing the memory when done. As with
800 ** all file-control actions, there is no guarantee that this will actually
801 ** do anything. Callers should initialize the char* variable to a NULL
802 ** pointer in case this file-control is not implemented. This file-control
803 ** is intended for diagnostic use only.
804 */
805 #define SQLITE_FCNTL_LOCKSTATE 1
806 #define SQLITE_GET_LOCKPROXYFILE 2
807 #define SQLITE_SET_LOCKPROXYFILE 3
808 #define SQLITE_LAST_ERRNO 4
809 #define SQLITE_FCNTL_SIZE_HINT 5
810 #define SQLITE_FCNTL_CHUNK_SIZE 6
811 #define SQLITE_FCNTL_FILE_POINTER 7
812 #define SQLITE_FCNTL_SYNC_OMITTED 8
813 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
814 #define SQLITE_FCNTL_PERSIST_WAL 10
815 #define SQLITE_FCNTL_OVERWRITE 11
816 #define SQLITE_FCNTL_VFSNAME 12
817 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
818
819 /*
820 ** CAPI3REF: Mutex Handle
821 **
822 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -840,11 +867,11 @@
867 ** ^SQLite guarantees that the zFilename parameter to xOpen
868 ** is either a NULL pointer or string obtained
869 ** from xFullPathname() with an optional suffix added.
870 ** ^If a suffix is added to the zFilename parameter, it will
871 ** consist of a single "-" character followed by no more than
872 ** 11 alphanumeric and/or "-" characters.
873 ** ^SQLite further guarantees that
874 ** the string will be valid and unchanged until xClose() is
875 ** called. Because of the previous sentence,
876 ** the [sqlite3_file] can safely store a pointer to the
877 ** filename if it needs to remember the filename for some reason.
@@ -1371,11 +1398,11 @@
1398 **
1399 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1400 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1401 ** the database page cache with the default page cache implementation.
1402 ** This configuration should not be used if an application-define page
1403 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1404 ** There are three arguments to this option: A pointer to 8-byte aligned
1405 ** memory, the size of each page buffer (sz), and the number of pages (N).
1406 ** The sz argument should be the size of the largest database page
1407 ** (a power of two between 512 and 32768) plus a little extra for each
1408 ** page header. ^The page header size is 20 to 40 bytes depending on
@@ -1440,19 +1467,19 @@
1467 ** slots allocated to each database connection.)^ ^(This option sets the
1468 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1469 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1470 ** configuration on individual connections.)^ </dd>
1471 **
1472 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1473 ** <dd> ^(This option takes a single argument which is a pointer to
1474 ** an [sqlite3_pcache_methods2] object. This object specifies the interface
1475 ** to a custom page cache implementation.)^ ^SQLite makes a copy of the
1476 ** object and uses it for page cache memory allocations.</dd>
1477 **
1478 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1479 ** <dd> ^(This option takes a single argument which is a pointer to an
1480 ** [sqlite3_pcache_methods2] object. SQLite copies of the current
1481 ** page cache implementation into that object.)^ </dd>
1482 **
1483 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1484 ** <dd> ^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1485 ** function with a call signature of void(*)(void*,int,const char*),
@@ -1481,10 +1508,15 @@
1508 ** connection is opened. If it is globally disabled, filenames are
1509 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1510 ** database connection is opened. By default, URI handling is globally
1511 ** disabled. The default value may be changed by compiling with the
1512 ** [SQLITE_USE_URI] symbol defined.
1513 **
1514 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1515 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFNIG_GETPCACHE
1516 ** <dd> These options are obsolete and should not be used by new code.
1517 ** They are retained for backwards compatibility but are now no-ops.
1518 ** </dl>
1519 */
1520 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1521 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1522 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1496,14 +1528,16 @@
1528 #define SQLITE_CONFIG_MEMSTATUS 9 /* boolean */
1529 #define SQLITE_CONFIG_MUTEX 10 /* sqlite3_mutex_methods* */
1530 #define SQLITE_CONFIG_GETMUTEX 11 /* sqlite3_mutex_methods* */
1531 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1532 #define SQLITE_CONFIG_LOOKASIDE 13 /* int int */
1533 #define SQLITE_CONFIG_PCACHE 14 /* no-op */
1534 #define SQLITE_CONFIG_GETPCACHE 15 /* no-op */
1535 #define SQLITE_CONFIG_LOG 16 /* xFunc, void* */
1536 #define SQLITE_CONFIG_URI 17 /* int */
1537 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
1538 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1539
1540 /*
1541 ** CAPI3REF: Database Connection Configuration Options
1542 **
1543 ** These constants are the available integer configuration options that
@@ -1984,11 +2018,11 @@
2018 ** These routines all implement some additional formatting
2019 ** options that are useful for constructing SQL statements.
2020 ** All of the usual printf() formatting options apply. In addition, there
2021 ** is are "%q", "%Q", and "%z" options.
2022 **
2023 ** ^(The %q option works like %s in that it substitutes a nul-terminated
2024 ** string from the argument list. But %q also doubles every '\'' character.
2025 ** %q is designed for use inside a string literal.)^ By doubling each '\''
2026 ** character it escapes that character and allows it to be inserted into
2027 ** the string.
2028 **
@@ -2592,25 +2626,41 @@
2626 );
2627
2628 /*
2629 ** CAPI3REF: Obtain Values For URI Parameters
2630 **
2631 ** These are utility routines, useful to VFS implementations, that check
2632 ** to see if a database file was a URI that contained a specific query
2633 ** parameter, and if so obtains the value of that query parameter.
2634 **
2635 ** If F is the filename pointer passed into the xOpen() method of a VFS
2636 ** implementation and P is the name of the query parameter, then
2637 ** sqlite3_uri_parameter(F,P) returns the value of the P
2638 ** parameter if it exists or a NULL pointer if P does not appear as a
2639 ** query parameter on F. If P is a query parameter of F
2640 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
2641 ** a pointer to an empty string.
2642 **
2643 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
2644 ** parameter and returns true (1) or false (0) according to the value
2645 ** of P. The value of P is true if it is "yes" or "true" or "on" or
2646 ** a non-zero number and is false otherwise. If P is not a query parameter
2647 ** on F then sqlite3_uri_boolean(F,P,B) returns (B!=0).
2648 **
2649 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
2650 ** 64-bit signed integer and returns that integer, or D if P does not
2651 ** exist. If the value of P is something other than an integer, then
2652 ** zero is returned.
2653 **
2654 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
2655 ** sqlite3_uri_boolean(F,P,B) returns B. If F is not a NULL pointer and
2656 ** is not a pathname pointer that SQLite passed into the xOpen VFS method,
2657 ** then the behavior of this routine is undefined and probably undesirable.
2658 */
2659 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
2660 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
2661 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
2662
2663
2664 /*
2665 ** CAPI3REF: Error Codes And Messages
2666 **
@@ -2928,10 +2978,29 @@
2978 ** change the configuration of a database connection, they do not make
2979 ** changes to the content of the database files on disk.
2980 */
2981 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
2982
2983 /*
2984 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
2985 **
2986 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
2987 ** [prepared statement] S has been stepped at least once using
2988 ** [sqlite3_step(S)] but has not run to completion and/or has not
2989 ** been reset using [sqlite3_reset(S)]. ^The sqlite3_stmt_busy(S)
2990 ** interface returns false if S is a NULL pointer. If S is not a
2991 ** NULL pointer and is not a pointer to a valid [prepared statement]
2992 ** object, then the behavior is undefined and probably undesirable.
2993 **
2994 ** This interface can be used in combination [sqlite3_next_stmt()]
2995 ** to locate all prepared statements associated with a database
2996 ** connection that are in need of being reset. This can be used,
2997 ** for example, in diagnostic routines to search for prepared
2998 ** statements that are holding a transaction open.
2999 */
3000 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3001
3002 /*
3003 ** CAPI3REF: Dynamically Typed Value Object
3004 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3005 **
3006 ** SQLite uses the sqlite3_value object to represent all values
@@ -3469,11 +3538,11 @@
3538 ** of the string. ^For clarity: the values returned by
3539 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3540 ** bytes in the string, not the number of characters.
3541 **
3542 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3543 ** even empty strings, are always zero-terminated. ^The return
3544 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3545 **
3546 ** ^The object returned by [sqlite3_column_value()] is an
3547 ** [unprotected sqlite3_value] object. An unprotected sqlite3_value object
3548 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
@@ -4369,10 +4438,26 @@
4438 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4439 ** create the statement in the first place.
4440 */
4441 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4442
4443 /*
4444 ** CAPI3REF: Return The Filename For A Database Connection
4445 **
4446 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4447 ** associated with database N of connection D. ^The main database file
4448 ** has the name "main". If there is no attached database N on the database
4449 ** connection D, or if database N is a temporary or in-memory database, then
4450 ** a NULL pointer is returned.
4451 **
4452 ** ^The filename returned by this function is the output of the
4453 ** xFullPathname method of the [VFS]. ^In other words, the filename
4454 ** will be an absolute pathname, even if the filename used
4455 ** to open the database originally was a URI or relative pathname.
4456 */
4457 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
4458
4459 /*
4460 ** CAPI3REF: Find the next prepared statement
4461 **
4462 ** ^This interface returns a pointer to the next [prepared statement] after
4463 ** pStmt associated with the [database connection] pDb. ^If pStmt is NULL
@@ -4404,17 +4489,19 @@
4489 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4490 ** return the P argument from the previous call of the same function
4491 ** on the same [database connection] D, or NULL for
4492 ** the first call for each function on D.
4493 **
4494 ** The commit and rollback hook callbacks are not reentrant.
4495 ** The callback implementation must not do anything that will modify
4496 ** the database connection that invoked the callback. Any actions
4497 ** to modify the database connection must be deferred until after the
4498 ** completion of the [sqlite3_step()] call that triggered the commit
4499 ** or rollback hook in the first place.
4500 ** Note that running any other SQL statements, including SELECT statements,
4501 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
4502 ** the database connections for the meaning of "modify" in this paragraph.
4503 **
4504 ** ^Registering a NULL function disables the callback.
4505 **
4506 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4507 ** operation is allowed to continue normally. ^If the commit hook
@@ -4523,13 +4610,28 @@
4610 ** pages to improve performance is an example of non-essential memory.
4611 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
4612 ** which might be more or less than the amount requested.
4613 ** ^The sqlite3_release_memory() routine is a no-op returning zero
4614 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
4615 **
4616 ** See also: [sqlite3_db_release_memory()]
4617 */
4618 SQLITE_API int sqlite3_release_memory(int);
4619
4620 /*
4621 ** CAPI3REF: Free Memory Used By A Database Connection
4622 **
4623 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
4624 ** memory as possible from database connection D. Unlike the
4625 ** [sqlite3_release_memory()] interface, this interface is effect even
4626 ** when then [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
4627 ** omitted.
4628 **
4629 ** See also: [sqlite3_release_memory()]
4630 */
4631 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
4632
4633 /*
4634 ** CAPI3REF: Impose A Limit On Heap Size
4635 **
4636 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
4637 ** soft limit on the amount of heap memory that may be allocated by SQLite.
@@ -4540,11 +4642,12 @@
4642 ** below the limit, it will exceed the limit rather than generate
4643 ** an [SQLITE_NOMEM] error. In other words, the soft heap limit
4644 ** is advisory only.
4645 **
4646 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
4647 ** the soft heap limit prior to the call, or negative in the case of an
4648 ** error. ^If the argument N is negative
4649 ** then no change is made to the soft heap limit. Hence, the current
4650 ** size of the soft heap limit can be determined by invoking
4651 ** sqlite3_soft_heap_limit64() with a negative argument.
4652 **
4653 ** ^If the argument N is zero then the soft heap limit is disabled.
@@ -4556,11 +4659,11 @@
4659 ** <li> The soft heap limit is set to zero.
4660 ** <li> Memory accounting is disabled using a combination of the
4661 ** [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
4662 ** the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
4663 ** <li> An alternative page cache implementation is specified using
4664 ** [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
4665 ** <li> The page cache allocates from its own memory pool supplied
4666 ** by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
4667 ** from the heap.
4668 ** </ul>)^
4669 **
@@ -5298,19 +5401,19 @@
5401 ** is selected automatically at compile-time. ^(The following
5402 ** implementations are available in the SQLite core:
5403 **
5404 ** <ul>
5405 ** <li> SQLITE_MUTEX_OS2
5406 ** <li> SQLITE_MUTEX_PTHREADS
5407 ** <li> SQLITE_MUTEX_W32
5408 ** <li> SQLITE_MUTEX_NOOP
5409 ** </ul>)^
5410 **
5411 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5412 ** that does no real locking and is appropriate for use in
5413 ** a single-threaded application. ^The SQLITE_MUTEX_OS2,
5414 ** SQLITE_MUTEX_PTHREADS, and SQLITE_MUTEX_W32 implementations
5415 ** are appropriate for use on OS/2, Unix, and Windows.
5416 **
5417 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5418 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5419 ** implementation is included with the library. In this case the
@@ -5496,11 +5599,11 @@
5599 ** defined and if NDEBUG is not defined.
5600 **
5601 ** ^These routines should return true if the mutex in their argument
5602 ** is held or not held, respectively, by the calling thread.
5603 **
5604 ** ^The implementation is not required to provide versions of these
5605 ** routines that actually work. If the implementation does not provide working
5606 ** versions of these routines, it should at least provide stubs that always
5607 ** return true so that one does not get spurious assertion failures.
5608 **
5609 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
@@ -5624,13 +5727,13 @@
5727 #define SQLITE_TESTCTRL_ASSERT 12
5728 #define SQLITE_TESTCTRL_ALWAYS 13
5729 #define SQLITE_TESTCTRL_RESERVE 14
5730 #define SQLITE_TESTCTRL_OPTIMIZATIONS 15
5731 #define SQLITE_TESTCTRL_ISKEYWORD 16
5732 #define SQLITE_TESTCTRL_SCRATCHMALLOC 17
5733 #define SQLITE_TESTCTRL_LOCALTIME_FAULT 18
5734 #define SQLITE_TESTCTRL_EXPLAIN_STMT 19
5735 #define SQLITE_TESTCTRL_LAST 19
5736
5737 /*
5738 ** CAPI3REF: SQLite Runtime Status
5739 **
@@ -5929,21 +6032,37 @@
6032 ** the pluggable module. The SQLite core has no knowledge of
6033 ** its size or internal structure and never deals with the
6034 ** sqlite3_pcache object except by holding and passing pointers
6035 ** to the object.
6036 **
6037 ** See [sqlite3_pcache_methods2] for additional information.
6038 */
6039 typedef struct sqlite3_pcache sqlite3_pcache;
6040
6041 /*
6042 ** CAPI3REF: Custom Page Cache Object
6043 **
6044 ** The sqlite3_pcache_page object represents a single page in the
6045 ** page cache. The page cache will allocate instances of this
6046 ** object. Various methods of the page cache use pointers to instances
6047 ** of this object as parameters or as their return value.
6048 **
6049 ** See [sqlite3_pcache_methods2] for additional information.
6050 */
6051 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6052 struct sqlite3_pcache_page {
6053 void *pBuf; /* The content of the page */
6054 void *pExtra; /* Extra information associated with the page */
6055 };
6056
6057 /*
6058 ** CAPI3REF: Application Defined Page Cache.
6059 ** KEYWORDS: {page cache}
6060 **
6061 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
6062 ** register an alternative page cache implementation by passing in an
6063 ** instance of the sqlite3_pcache_methods2 structure.)^
6064 ** In many applications, most of the heap memory allocated by
6065 ** SQLite is used for the page cache.
6066 ** By implementing a
6067 ** custom page cache using this API, an application can better control
6068 ** the amount of memory consumed by SQLite, the way in which
@@ -5953,20 +6072,20 @@
6072 **
6073 ** The alternative page cache mechanism is an
6074 ** extreme measure that is only needed by the most demanding applications.
6075 ** The built-in page cache is recommended for most uses.
6076 **
6077 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
6078 ** internal buffer by SQLite within the call to [sqlite3_config]. Hence
6079 ** the application may discard the parameter after the call to
6080 ** [sqlite3_config()] returns.)^
6081 **
6082 ** [[the xInit() page cache method]]
6083 ** ^(The xInit() method is called once for each effective
6084 ** call to [sqlite3_initialize()])^
6085 ** (usually only once during the lifetime of the process). ^(The xInit()
6086 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
6087 ** The intent of the xInit() method is to set up global data structures
6088 ** required by the custom page cache implementation.
6089 ** ^(If the xInit() method is NULL, then the
6090 ** built-in default page cache is used instead of the application defined
6091 ** page cache.)^
@@ -5989,21 +6108,19 @@
6108 ** [[the xCreate() page cache methods]]
6109 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6110 ** SQLite will typically create one cache instance for each open database file,
6111 ** though this is not guaranteed. ^The
6112 ** first parameter, szPage, is the size in bytes of the pages that must
6113 ** be allocated by the cache. ^szPage will always a power of two. ^The
6114 ** second parameter szExtra is a number of bytes of extra storage
6115 ** associated with each page cache entry. ^The szExtra parameter will
6116 ** a number less than 250. SQLite will use the
6117 ** extra szExtra bytes on each page to store metadata about the underlying
6118 ** database page on disk. The value passed into szExtra depends
6119 ** on the SQLite version, the target platform, and how SQLite was compiled.
6120 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6121 ** created will be used to cache database pages of a file stored on disk, or
 
 
 
6122 ** false if it is used for an in-memory database. The cache implementation
6123 ** does not have to do anything special based with the value of bPurgeable;
6124 ** it is purely advisory. ^On a cache where bPurgeable is false, SQLite will
6125 ** never invoke xUnpin() except to deliberately delete a page.
6126 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
@@ -6023,15 +6140,20 @@
6140 ** The xPagecount() method must return the number of pages currently
6141 ** stored in the cache, both pinned and unpinned.
6142 **
6143 ** [[the xFetch() page cache methods]]
6144 ** The xFetch() method locates a page in the cache and returns a pointer to
6145 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6146 ** The pBuf element of the returned sqlite3_pcache_page object will be a
6147 ** pointer to a buffer of szPage bytes used to store the content of a
6148 ** single database page. The pExtra element of sqlite3_pcache_page will be
6149 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
6150 ** for each entry in the page cache.
6151 **
6152 ** The page to be fetched is determined by the key. ^The minimum key value
6153 ** is 1. After it has been retrieved using xFetch, the page is considered
6154 ** to be "pinned".
6155 **
6156 ** If the requested page is already in the page cache, then the page cache
6157 ** implementation must return a pointer to the page buffer with its content
6158 ** intact. If the requested page is not already in the cache, then the
6159 ** cache implementation should use the value of the createFlag
@@ -6080,12 +6202,41 @@
6202 **
6203 ** [[the xDestroy() page cache method]]
6204 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6205 ** All resources associated with the specified cache should be freed. ^After
6206 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6207 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
6208 ** functions.
6209 **
6210 ** [[the xShrink() page cache method]]
6211 ** ^SQLite invokes the xShrink() method when it wants the page cache to
6212 ** free up as much of heap memory as possible. The page cache implementation
6213 ** is not obligated to free any memory, but well-behaved implementions should
6214 ** do their best.
6215 */
6216 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6217 struct sqlite3_pcache_methods2 {
6218 int iVersion;
6219 void *pArg;
6220 int (*xInit)(void*);
6221 void (*xShutdown)(void*);
6222 sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6223 void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6224 int (*xPagecount)(sqlite3_pcache*);
6225 sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6226 void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6227 void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6228 unsigned oldKey, unsigned newKey);
6229 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6230 void (*xDestroy)(sqlite3_pcache*);
6231 void (*xShrink)(sqlite3_pcache*);
6232 };
6233
6234 /*
6235 ** This is the obsolete pcache_methods object that has now been replaced
6236 ** by sqlite3_pcache_methods2. This object is not used by SQLite. It is
6237 ** retained in the header file for backwards compatibility only.
6238 */
6239 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6240 struct sqlite3_pcache_methods {
6241 void *pArg;
6242 int (*xInit)(void*);
@@ -6097,10 +6248,11 @@
6248 void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6249 void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6250 void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6251 void (*xDestroy)(sqlite3_pcache*);
6252 };
6253
6254
6255 /*
6256 ** CAPI3REF: Online Backup Object
6257 **
6258 ** The sqlite3_backup object records state information about an ongoing
6259
--- src/style.c
+++ src/style.c
@@ -893,10 +893,11 @@
893893
zCap[i] = 0;
894894
@ g.userUid = %d(g.userUid)<br />
895895
@ g.zLogin = %h(g.zLogin)<br />
896896
@ capabilities = %s(zCap)<br />
897897
@ <hr>
898
+ P("HTTP_USER_AGENT");
898899
cgi_print_all(atoi(PD("showall","0")));
899900
if( g.perm.Setup ){
900901
const char *zRedir = P("redirect");
901902
if( zRedir ) cgi_redirect(zRedir);
902903
}
903904
--- src/style.c
+++ src/style.c
@@ -893,10 +893,11 @@
893 zCap[i] = 0;
894 @ g.userUid = %d(g.userUid)<br />
895 @ g.zLogin = %h(g.zLogin)<br />
896 @ capabilities = %s(zCap)<br />
897 @ <hr>
 
898 cgi_print_all(atoi(PD("showall","0")));
899 if( g.perm.Setup ){
900 const char *zRedir = P("redirect");
901 if( zRedir ) cgi_redirect(zRedir);
902 }
903
--- src/style.c
+++ src/style.c
@@ -893,10 +893,11 @@
893 zCap[i] = 0;
894 @ g.userUid = %d(g.userUid)<br />
895 @ g.zLogin = %h(g.zLogin)<br />
896 @ capabilities = %s(zCap)<br />
897 @ <hr>
898 P("HTTP_USER_AGENT");
899 cgi_print_all(atoi(PD("showall","0")));
900 if( g.perm.Setup ){
901 const char *zRedir = P("redirect");
902 if( zRedir ) cgi_redirect(zRedir);
903 }
904
+3
--- src/tkt.c
+++ src/tkt.c
@@ -520,10 +520,13 @@
520520
getAllTicketFields();
521521
initializeVariablesFromDb();
522522
initializeVariablesFromCGI();
523523
@ <form method="post" action="%s(g.zTop)/%s(g.zPath)"><p>
524524
login_insert_csrf_secret();
525
+ if( P("date_override") && g.perm.Setup ){
526
+ @ <input type="hidden" name="date_override" value="%h(P("date_override"))">
527
+ }
525528
@ </p>
526529
zScript = ticket_newpage_code();
527530
Th_Store("login", g.zLogin);
528531
Th_Store("date", db_text(0, "SELECT datetime('now')"));
529532
Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd,
530533
--- src/tkt.c
+++ src/tkt.c
@@ -520,10 +520,13 @@
520 getAllTicketFields();
521 initializeVariablesFromDb();
522 initializeVariablesFromCGI();
523 @ <form method="post" action="%s(g.zTop)/%s(g.zPath)"><p>
524 login_insert_csrf_secret();
 
 
 
525 @ </p>
526 zScript = ticket_newpage_code();
527 Th_Store("login", g.zLogin);
528 Th_Store("date", db_text(0, "SELECT datetime('now')"));
529 Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd,
530
--- src/tkt.c
+++ src/tkt.c
@@ -520,10 +520,13 @@
520 getAllTicketFields();
521 initializeVariablesFromDb();
522 initializeVariablesFromCGI();
523 @ <form method="post" action="%s(g.zTop)/%s(g.zPath)"><p>
524 login_insert_csrf_secret();
525 if( P("date_override") && g.perm.Setup ){
526 @ <input type="hidden" name="date_override" value="%h(P("date_override"))">
527 }
528 @ </p>
529 zScript = ticket_newpage_code();
530 Th_Store("login", g.zLogin);
531 Th_Store("date", db_text(0, "SELECT datetime('now')"));
532 Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd,
533
+1 -1
--- src/update.c
+++ src/update.c
@@ -575,11 +575,11 @@
575575
fossil_fatal("no such checkin: %s", revision);
576576
}
577577
pManifest = manifest_get(rid, CFTYPE_MANIFEST);
578578
579579
if( pManifest ){
580
- pFile = manifest_file_seek(pManifest, file);
580
+ pFile = manifest_file_find(pManifest, file);
581581
if( pFile ){
582582
rid = uuid_to_rid(pFile->zUuid, 0);
583583
if( pIsExe ) *pIsExe = ( manifest_file_mperm(pFile)==PERM_EXE );
584584
if( pIsLink ) *pIsLink = ( manifest_file_mperm(pFile)==PERM_LNK );
585585
manifest_destroy(pManifest);
586586
--- src/update.c
+++ src/update.c
@@ -575,11 +575,11 @@
575 fossil_fatal("no such checkin: %s", revision);
576 }
577 pManifest = manifest_get(rid, CFTYPE_MANIFEST);
578
579 if( pManifest ){
580 pFile = manifest_file_seek(pManifest, file);
581 if( pFile ){
582 rid = uuid_to_rid(pFile->zUuid, 0);
583 if( pIsExe ) *pIsExe = ( manifest_file_mperm(pFile)==PERM_EXE );
584 if( pIsLink ) *pIsLink = ( manifest_file_mperm(pFile)==PERM_LNK );
585 manifest_destroy(pManifest);
586
--- src/update.c
+++ src/update.c
@@ -575,11 +575,11 @@
575 fossil_fatal("no such checkin: %s", revision);
576 }
577 pManifest = manifest_get(rid, CFTYPE_MANIFEST);
578
579 if( pManifest ){
580 pFile = manifest_file_find(pManifest, file);
581 if( pFile ){
582 rid = uuid_to_rid(pFile->zUuid, 0);
583 if( pIsExe ) *pIsExe = ( manifest_file_mperm(pFile)==PERM_EXE );
584 if( pIsLink ) *pIsLink = ( manifest_file_mperm(pFile)==PERM_LNK );
585 manifest_destroy(pManifest);
586
+60 -38
--- src/vfile.c
+++ src/vfile.c
@@ -124,30 +124,40 @@
124124
manifest_destroy(p);
125125
db_end_transaction(0);
126126
}
127127
128128
/*
129
-** Check the file signature of the disk image for every VFILE of vid.
130
-**
131
-** Set the VFILE.CHNGED field on every file that has changed. Also
132
-** set VFILE.CHNGED on every folder that contains a file or folder
133
-** that has changed.
134
-**
135
-** If VFILE.DELETED is null or if VFILE.RID is zero, then we can assume
136
-** the file has changed without having the check the on-disk image.
137
-**
138
-** If the size of the file has changed, then we assume that it has
139
-** changed. If the mtime of the file has not changed and useSha1sum is false
140
-** and the mtime-changes setting is true (the default) then we assume that
141
-** the file has not changed. If the mtime has changed, we go ahead and
142
-** double-check that the file has changed by looking at its SHA1 sum.
129
+** Look at every VFILE entry with the given vid and set update
130
+** VFILE.CHNGED field on every file according to whether or not
131
+** the file has changes. 0 means no change. 1 means edited. 2 means
132
+** the file has changed due to a merge. 3 means the file was added
133
+** by a merge.
134
+**
135
+** If VFILE.DELETED is true or if VFILE.RID is zero, then the file was
136
+** either removed from managemented via "fossil rm" or added via
137
+** "fossil add", respectively, and in both cases we always know that
138
+** the file has changed without having the check the size, mtime,
139
+** or on-disk content.
140
+**
141
+** If the size of the file has changed, then we always know that the file
142
+** changed without having to look at the mtime or on-disk content.
143
+**
144
+** The mtime of the file is only a factor if the mtime-changes setting
145
+** is false and the useSha1sum flag is false. If the mtime-changes
146
+** setting is true (or undefined - it defaults to true) or if useSha1sum
147
+** is true, then we do not trust the mtime and will examine the on-disk
148
+** content to determine if a file really is the same.
149
+**
150
+** If the mtime is used, it is used only to determine if files are the same.
151
+** If the mtime of a file has changed, we still examine the on-disk content
152
+** to see whether or not the edit was a null-edit.
143153
*/
144154
void vfile_check_signature(int vid, int notFileIsFatal, int useSha1sum){
145155
int nErr = 0;
146156
Stmt q;
147157
Blob fileCksum, origCksum;
148
- int checkMtime = useSha1sum==0 && db_get_boolean("mtime-changes", 1);
158
+ int useMtime = useSha1sum==0 && db_get_boolean("mtime-changes", 1);
149159
150160
db_begin_transaction();
151161
db_prepare(&q, "SELECT id, %Q || pathname,"
152162
" vfile.mrid, deleted, chnged, uuid, size, mtime"
153163
" FROM vfile LEFT JOIN blob ON vfile.mrid=blob.rid"
@@ -157,55 +167,67 @@
157167
const char *zName;
158168
int chnged = 0;
159169
int oldChnged;
160170
i64 oldMtime;
161171
i64 currentMtime;
172
+ i64 origSize;
173
+ i64 currentSize;
162174
163175
id = db_column_int(&q, 0);
164176
zName = db_column_text(&q, 1);
165177
rid = db_column_int(&q, 2);
166178
isDeleted = db_column_int(&q, 3);
167
- oldChnged = db_column_int(&q, 4);
179
+ oldChnged = chnged = db_column_int(&q, 4);
168180
oldMtime = db_column_int64(&q, 7);
169
- if( isDeleted ){
181
+ currentSize = file_wd_size(zName);
182
+ origSize = db_column_int64(&q, 6);
183
+ currentMtime = file_wd_mtime(0);
184
+ if( chnged==0 && (isDeleted || rid==0) ){
185
+ /* "fossil rm" or "fossil add" always change the file */
170186
chnged = 1;
171
- }else if( !file_wd_isfile_or_link(zName) && file_wd_size(0)>=0 ){
187
+ }else if( !file_wd_isfile_or_link(0) && currentSize>=0 ){
172188
if( notFileIsFatal ){
173189
fossil_warning("not an ordinary file: %s", zName);
174190
nErr++;
175191
}
176192
chnged = 1;
177
- }else if( oldChnged>=2 ){
178
- chnged = oldChnged;
179
- }else if( rid==0 ){
180
- chnged = 1;
181
- }
182
- if( chnged!=1 ){
183
- i64 origSize = db_column_int64(&q, 6);
184
- currentMtime = file_wd_mtime(0);
185
- if( origSize!=file_wd_size(0) ){
186
- /* A file size change is definitive - the file has changed. No
187
- ** need to check the sha1sum */
188
- chnged = 1;
189
- }
190
- }
191
- if( chnged!=1 && (checkMtime==0 || currentMtime!=oldMtime) ){
193
+ }
194
+ if( origSize!=currentSize ){
195
+ if( chnged==0 ){
196
+ /* A file size change is definitive - the file has changed. No
197
+ ** need to check the mtime or sha1sum */
198
+ chnged = 1;
199
+ }
200
+ }else if( chnged==1 && rid!=0 && !isDeleted ){
201
+ /* File is believed to have changed but it is the same size.
202
+ ** Double check that it really has changed by looking at content. */
203
+ assert( origSize==currentSize );
204
+ db_ephemeral_blob(&q, 5, &origCksum);
205
+ if( sha1sum_file(zName, &fileCksum) ){
206
+ blob_zero(&fileCksum);
207
+ }
208
+ if( blob_compare(&fileCksum, &origCksum)==0 ) chnged = 0;
209
+ blob_reset(&origCksum);
210
+ blob_reset(&fileCksum);
211
+ }else if( chnged==0 && (useMtime==0 || currentMtime!=oldMtime) ){
212
+ /* For files that were formerly believed to be unchanged, if their
213
+ ** mtime changes, or unconditionally if --sha1sum is used, check
214
+ ** to see if they have been edited by looking at their SHA1 sum */
215
+ assert( origSize==currentSize );
192216
db_ephemeral_blob(&q, 5, &origCksum);
193217
if( sha1sum_file(zName, &fileCksum) ){
194218
blob_zero(&fileCksum);
195219
}
196220
if( blob_compare(&fileCksum, &origCksum) ){
197221
chnged = 1;
198
- }else if( currentMtime!=oldMtime ){
199
- db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
200
- currentMtime, id);
201222
}
202223
blob_reset(&origCksum);
203224
blob_reset(&fileCksum);
204225
}
205
- if( chnged!=oldChnged && (chnged || !checkMtime) ){
206
- db_multi_exec("UPDATE vfile SET chnged=%d WHERE id=%d", chnged, id);
226
+ if( currentMtime!=oldMtime || chnged!=oldChnged ){
227
+ db_multi_exec("UPDATE vfile SET mtime=%lld, chnged=%d WHERE id=%d",
228
+ currentMtime, chnged, id);
207229
}
208230
}
209231
db_finalize(&q);
210232
if( nErr ) fossil_fatal("abort due to prior errors");
211233
db_end_transaction(0);
212234
--- src/vfile.c
+++ src/vfile.c
@@ -124,30 +124,40 @@
124 manifest_destroy(p);
125 db_end_transaction(0);
126 }
127
128 /*
129 ** Check the file signature of the disk image for every VFILE of vid.
130 **
131 ** Set the VFILE.CHNGED field on every file that has changed. Also
132 ** set VFILE.CHNGED on every folder that contains a file or folder
133 ** that has changed.
134 **
135 ** If VFILE.DELETED is null or if VFILE.RID is zero, then we can assume
136 ** the file has changed without having the check the on-disk image.
137 **
138 ** If the size of the file has changed, then we assume that it has
139 ** changed. If the mtime of the file has not changed and useSha1sum is false
140 ** and the mtime-changes setting is true (the default) then we assume that
141 ** the file has not changed. If the mtime has changed, we go ahead and
142 ** double-check that the file has changed by looking at its SHA1 sum.
 
 
 
 
 
 
 
 
 
 
143 */
144 void vfile_check_signature(int vid, int notFileIsFatal, int useSha1sum){
145 int nErr = 0;
146 Stmt q;
147 Blob fileCksum, origCksum;
148 int checkMtime = useSha1sum==0 && db_get_boolean("mtime-changes", 1);
149
150 db_begin_transaction();
151 db_prepare(&q, "SELECT id, %Q || pathname,"
152 " vfile.mrid, deleted, chnged, uuid, size, mtime"
153 " FROM vfile LEFT JOIN blob ON vfile.mrid=blob.rid"
@@ -157,55 +167,67 @@
157 const char *zName;
158 int chnged = 0;
159 int oldChnged;
160 i64 oldMtime;
161 i64 currentMtime;
 
 
162
163 id = db_column_int(&q, 0);
164 zName = db_column_text(&q, 1);
165 rid = db_column_int(&q, 2);
166 isDeleted = db_column_int(&q, 3);
167 oldChnged = db_column_int(&q, 4);
168 oldMtime = db_column_int64(&q, 7);
169 if( isDeleted ){
 
 
 
 
170 chnged = 1;
171 }else if( !file_wd_isfile_or_link(zName) && file_wd_size(0)>=0 ){
172 if( notFileIsFatal ){
173 fossil_warning("not an ordinary file: %s", zName);
174 nErr++;
175 }
176 chnged = 1;
177 }else if( oldChnged>=2 ){
178 chnged = oldChnged;
179 }else if( rid==0 ){
180 chnged = 1;
181 }
182 if( chnged!=1 ){
183 i64 origSize = db_column_int64(&q, 6);
184 currentMtime = file_wd_mtime(0);
185 if( origSize!=file_wd_size(0) ){
186 /* A file size change is definitive - the file has changed. No
187 ** need to check the sha1sum */
188 chnged = 1;
189 }
190 }
191 if( chnged!=1 && (checkMtime==0 || currentMtime!=oldMtime) ){
 
 
 
 
 
 
 
 
192 db_ephemeral_blob(&q, 5, &origCksum);
193 if( sha1sum_file(zName, &fileCksum) ){
194 blob_zero(&fileCksum);
195 }
196 if( blob_compare(&fileCksum, &origCksum) ){
197 chnged = 1;
198 }else if( currentMtime!=oldMtime ){
199 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
200 currentMtime, id);
201 }
202 blob_reset(&origCksum);
203 blob_reset(&fileCksum);
204 }
205 if( chnged!=oldChnged && (chnged || !checkMtime) ){
206 db_multi_exec("UPDATE vfile SET chnged=%d WHERE id=%d", chnged, id);
 
207 }
208 }
209 db_finalize(&q);
210 if( nErr ) fossil_fatal("abort due to prior errors");
211 db_end_transaction(0);
212
--- src/vfile.c
+++ src/vfile.c
@@ -124,30 +124,40 @@
124 manifest_destroy(p);
125 db_end_transaction(0);
126 }
127
128 /*
129 ** Look at every VFILE entry with the given vid and set update
130 ** VFILE.CHNGED field on every file according to whether or not
131 ** the file has changes. 0 means no change. 1 means edited. 2 means
132 ** the file has changed due to a merge. 3 means the file was added
133 ** by a merge.
134 **
135 ** If VFILE.DELETED is true or if VFILE.RID is zero, then the file was
136 ** either removed from managemented via "fossil rm" or added via
137 ** "fossil add", respectively, and in both cases we always know that
138 ** the file has changed without having the check the size, mtime,
139 ** or on-disk content.
140 **
141 ** If the size of the file has changed, then we always know that the file
142 ** changed without having to look at the mtime or on-disk content.
143 **
144 ** The mtime of the file is only a factor if the mtime-changes setting
145 ** is false and the useSha1sum flag is false. If the mtime-changes
146 ** setting is true (or undefined - it defaults to true) or if useSha1sum
147 ** is true, then we do not trust the mtime and will examine the on-disk
148 ** content to determine if a file really is the same.
149 **
150 ** If the mtime is used, it is used only to determine if files are the same.
151 ** If the mtime of a file has changed, we still examine the on-disk content
152 ** to see whether or not the edit was a null-edit.
153 */
154 void vfile_check_signature(int vid, int notFileIsFatal, int useSha1sum){
155 int nErr = 0;
156 Stmt q;
157 Blob fileCksum, origCksum;
158 int useMtime = useSha1sum==0 && db_get_boolean("mtime-changes", 1);
159
160 db_begin_transaction();
161 db_prepare(&q, "SELECT id, %Q || pathname,"
162 " vfile.mrid, deleted, chnged, uuid, size, mtime"
163 " FROM vfile LEFT JOIN blob ON vfile.mrid=blob.rid"
@@ -157,55 +167,67 @@
167 const char *zName;
168 int chnged = 0;
169 int oldChnged;
170 i64 oldMtime;
171 i64 currentMtime;
172 i64 origSize;
173 i64 currentSize;
174
175 id = db_column_int(&q, 0);
176 zName = db_column_text(&q, 1);
177 rid = db_column_int(&q, 2);
178 isDeleted = db_column_int(&q, 3);
179 oldChnged = chnged = db_column_int(&q, 4);
180 oldMtime = db_column_int64(&q, 7);
181 currentSize = file_wd_size(zName);
182 origSize = db_column_int64(&q, 6);
183 currentMtime = file_wd_mtime(0);
184 if( chnged==0 && (isDeleted || rid==0) ){
185 /* "fossil rm" or "fossil add" always change the file */
186 chnged = 1;
187 }else if( !file_wd_isfile_or_link(0) && currentSize>=0 ){
188 if( notFileIsFatal ){
189 fossil_warning("not an ordinary file: %s", zName);
190 nErr++;
191 }
192 chnged = 1;
193 }
194 if( origSize!=currentSize ){
195 if( chnged==0 ){
196 /* A file size change is definitive - the file has changed. No
197 ** need to check the mtime or sha1sum */
198 chnged = 1;
199 }
200 }else if( chnged==1 && rid!=0 && !isDeleted ){
201 /* File is believed to have changed but it is the same size.
202 ** Double check that it really has changed by looking at content. */
203 assert( origSize==currentSize );
204 db_ephemeral_blob(&q, 5, &origCksum);
205 if( sha1sum_file(zName, &fileCksum) ){
206 blob_zero(&fileCksum);
207 }
208 if( blob_compare(&fileCksum, &origCksum)==0 ) chnged = 0;
209 blob_reset(&origCksum);
210 blob_reset(&fileCksum);
211 }else if( chnged==0 && (useMtime==0 || currentMtime!=oldMtime) ){
212 /* For files that were formerly believed to be unchanged, if their
213 ** mtime changes, or unconditionally if --sha1sum is used, check
214 ** to see if they have been edited by looking at their SHA1 sum */
215 assert( origSize==currentSize );
216 db_ephemeral_blob(&q, 5, &origCksum);
217 if( sha1sum_file(zName, &fileCksum) ){
218 blob_zero(&fileCksum);
219 }
220 if( blob_compare(&fileCksum, &origCksum) ){
221 chnged = 1;
 
 
 
222 }
223 blob_reset(&origCksum);
224 blob_reset(&fileCksum);
225 }
226 if( currentMtime!=oldMtime || chnged!=oldChnged ){
227 db_multi_exec("UPDATE vfile SET mtime=%lld, chnged=%d WHERE id=%d",
228 currentMtime, chnged, id);
229 }
230 }
231 db_finalize(&q);
232 if( nErr ) fossil_fatal("abort due to prior errors");
233 db_end_transaction(0);
234
+21 -8
--- src/xfer.c
+++ src/xfer.c
@@ -443,43 +443,56 @@
443443
const char *zDelta;
444444
int szU;
445445
int szC;
446446
int rc;
447447
int isPrivate;
448
+ int srcIsPrivate;
448449
static Stmt q1;
450
+ Blob fullContent;
449451
450452
isPrivate = content_is_private(rid);
451453
if( isPrivate && pXfer->syncPrivate==0 ) return;
452454
db_static_prepare(&q1,
453
- "SELECT uuid, size, content,"
454
- " (SELECT uuid FROM delta, blob"
455
- " WHERE delta.rid=:rid AND delta.srcid=blob.rid)"
456
- " FROM blob"
457
- " WHERE rid=:rid"
458
- " AND size>=0"
455
+ "SELECT uuid, size, content, delta.srcid IN private,"
456
+ " (SELECT uuid FROM blob WHERE rid=delta.srcid)"
457
+ " FROM blob LEFT JOIN delta ON (blob.rid=delta.rid)"
458
+ " WHERE blob.rid=:rid"
459
+ " AND blob.size>=0"
459460
" AND NOT EXISTS(SELECT 1 FROM shun WHERE shun.uuid=blob.uuid)"
460461
);
461462
db_bind_int(&q1, ":rid", rid);
462463
rc = db_step(&q1);
463464
if( rc==SQLITE_ROW ){
464465
zUuid = db_column_text(&q1, 0);
465466
szU = db_column_int(&q1, 1);
466467
szC = db_column_bytes(&q1, 2);
467468
zContent = db_column_raw(&q1, 2);
468
- zDelta = db_column_text(&q1, 3);
469
+ srcIsPrivate = db_column_int(&q1, 3);
470
+ zDelta = db_column_text(&q1, 4);
469471
if( isPrivate ) blob_append(pXfer->pOut, "private\n", -1);
470472
blob_appendf(pXfer->pOut, "cfile %s ", zUuid);
471
- if( zDelta ){
473
+ if( !isPrivate && srcIsPrivate ){
474
+ content_get(rid, &fullContent);
475
+ szU = blob_size(&fullContent);
476
+ blob_compress(&fullContent, &fullContent);
477
+ szC = blob_size(&fullContent);
478
+ zContent = blob_buffer(&fullContent);
479
+ zDelta = 0;
480
+ }
481
+ if( zDelta ){
472482
blob_appendf(pXfer->pOut, "%s ", zDelta);
473483
pXfer->nDeltaSent++;
474484
}else{
475485
pXfer->nFileSent++;
476486
}
477487
blob_appendf(pXfer->pOut, "%d %d\n", szU, szC);
478488
blob_append(pXfer->pOut, zContent, szC);
479489
if( blob_buffer(pXfer->pOut)[blob_size(pXfer->pOut)-1]!='\n' ){
480490
blob_appendf(pXfer->pOut, "\n", 1);
491
+ }
492
+ if( !isPrivate && srcIsPrivate ){
493
+ blob_reset(&fullContent);
481494
}
482495
}
483496
db_reset(&q1);
484497
}
485498
486499
--- src/xfer.c
+++ src/xfer.c
@@ -443,43 +443,56 @@
443 const char *zDelta;
444 int szU;
445 int szC;
446 int rc;
447 int isPrivate;
 
448 static Stmt q1;
 
449
450 isPrivate = content_is_private(rid);
451 if( isPrivate && pXfer->syncPrivate==0 ) return;
452 db_static_prepare(&q1,
453 "SELECT uuid, size, content,"
454 " (SELECT uuid FROM delta, blob"
455 " WHERE delta.rid=:rid AND delta.srcid=blob.rid)"
456 " FROM blob"
457 " WHERE rid=:rid"
458 " AND size>=0"
459 " AND NOT EXISTS(SELECT 1 FROM shun WHERE shun.uuid=blob.uuid)"
460 );
461 db_bind_int(&q1, ":rid", rid);
462 rc = db_step(&q1);
463 if( rc==SQLITE_ROW ){
464 zUuid = db_column_text(&q1, 0);
465 szU = db_column_int(&q1, 1);
466 szC = db_column_bytes(&q1, 2);
467 zContent = db_column_raw(&q1, 2);
468 zDelta = db_column_text(&q1, 3);
 
469 if( isPrivate ) blob_append(pXfer->pOut, "private\n", -1);
470 blob_appendf(pXfer->pOut, "cfile %s ", zUuid);
471 if( zDelta ){
 
 
 
 
 
 
 
 
472 blob_appendf(pXfer->pOut, "%s ", zDelta);
473 pXfer->nDeltaSent++;
474 }else{
475 pXfer->nFileSent++;
476 }
477 blob_appendf(pXfer->pOut, "%d %d\n", szU, szC);
478 blob_append(pXfer->pOut, zContent, szC);
479 if( blob_buffer(pXfer->pOut)[blob_size(pXfer->pOut)-1]!='\n' ){
480 blob_appendf(pXfer->pOut, "\n", 1);
 
 
 
481 }
482 }
483 db_reset(&q1);
484 }
485
486
--- src/xfer.c
+++ src/xfer.c
@@ -443,43 +443,56 @@
443 const char *zDelta;
444 int szU;
445 int szC;
446 int rc;
447 int isPrivate;
448 int srcIsPrivate;
449 static Stmt q1;
450 Blob fullContent;
451
452 isPrivate = content_is_private(rid);
453 if( isPrivate && pXfer->syncPrivate==0 ) return;
454 db_static_prepare(&q1,
455 "SELECT uuid, size, content, delta.srcid IN private,"
456 " (SELECT uuid FROM blob WHERE rid=delta.srcid)"
457 " FROM blob LEFT JOIN delta ON (blob.rid=delta.rid)"
458 " WHERE blob.rid=:rid"
459 " AND blob.size>=0"
 
460 " AND NOT EXISTS(SELECT 1 FROM shun WHERE shun.uuid=blob.uuid)"
461 );
462 db_bind_int(&q1, ":rid", rid);
463 rc = db_step(&q1);
464 if( rc==SQLITE_ROW ){
465 zUuid = db_column_text(&q1, 0);
466 szU = db_column_int(&q1, 1);
467 szC = db_column_bytes(&q1, 2);
468 zContent = db_column_raw(&q1, 2);
469 srcIsPrivate = db_column_int(&q1, 3);
470 zDelta = db_column_text(&q1, 4);
471 if( isPrivate ) blob_append(pXfer->pOut, "private\n", -1);
472 blob_appendf(pXfer->pOut, "cfile %s ", zUuid);
473 if( !isPrivate && srcIsPrivate ){
474 content_get(rid, &fullContent);
475 szU = blob_size(&fullContent);
476 blob_compress(&fullContent, &fullContent);
477 szC = blob_size(&fullContent);
478 zContent = blob_buffer(&fullContent);
479 zDelta = 0;
480 }
481 if( zDelta ){
482 blob_appendf(pXfer->pOut, "%s ", zDelta);
483 pXfer->nDeltaSent++;
484 }else{
485 pXfer->nFileSent++;
486 }
487 blob_appendf(pXfer->pOut, "%d %d\n", szU, szC);
488 blob_append(pXfer->pOut, zContent, szC);
489 if( blob_buffer(pXfer->pOut)[blob_size(pXfer->pOut)-1]!='\n' ){
490 blob_appendf(pXfer->pOut, "\n", 1);
491 }
492 if( !isPrivate && srcIsPrivate ){
493 blob_reset(&fullContent);
494 }
495 }
496 db_reset(&q1);
497 }
498
499
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,6 +1,31 @@
11
<title>Change Log</title>
2
+
3
+<h2>Changes For Version 1.21 (2011-12-13)</h2>
4
+ * Added side-by-side diffs in the command-line interface
5
+ * Automatically enable hyperlinks if the UserAgent string in the
6
+ HTTP header suggests that the requestor is a human and not a bot.
7
+ * Show only commonly used commands with "fossil help". Use
8
+ "fossil help --all" to see the complete list now.
9
+ * Improvements to the "stash" command: (1) Stash all files, not just
10
+ those below the working directory. (2) Add the --detail option to
11
+ "list". (3) Confirm before "drop --all". (4) Add the "help"
12
+ subcommand.
13
+ * Add an Admin/Access setting to change the number of octets of the
14
+ IP address that are saved in login cookies - allowing this setting
15
+ to be changed to zero
16
+ * Promote the "test-md5sum" command to "md5sum".
17
+ * Added the "whatis" command.
18
+ * Stop showing the server-code in status outputs - it is no longer used
19
+ for anything.
20
+ * Added a compile-time option (--with-tcl) to build in the full
21
+ TCL interpreter to augment TH1.
22
+ * Merged the JSON branch into trunk. Disabled by default. Enabled
23
+ by a compile-time option. Probably it will be enabled by default
24
+ in some future release.
25
+ * Update to use SQLite version 3.7.9 plus the alignment fix for Sparc.
26
+ align
227
328
<h2>Changes For Version 1.20 (2011-10-21)</h2>
429
* Added side-by-side diffs in HTML interface. [0bde74ea1e]
530
* Added support for symlinks. (Controlled by "allow-symlinks" setting,
631
off by default). [e4f1c1fe95]
732
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,6 +1,31 @@
1 <title>Change Log</title>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3 <h2>Changes For Version 1.20 (2011-10-21)</h2>
4 * Added side-by-side diffs in HTML interface. [0bde74ea1e]
5 * Added support for symlinks. (Controlled by "allow-symlinks" setting,
6 off by default). [e4f1c1fe95]
7
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,6 +1,31 @@
1 <title>Change Log</title>
2
3 <h2>Changes For Version 1.21 (2011-12-13)</h2>
4 * Added side-by-side diffs in the command-line interface
5 * Automatically enable hyperlinks if the UserAgent string in the
6 HTTP header suggests that the requestor is a human and not a bot.
7 * Show only commonly used commands with "fossil help". Use
8 "fossil help --all" to see the complete list now.
9 * Improvements to the "stash" command: (1) Stash all files, not just
10 those below the working directory. (2) Add the --detail option to
11 "list". (3) Confirm before "drop --all". (4) Add the "help"
12 subcommand.
13 * Add an Admin/Access setting to change the number of octets of the
14 IP address that are saved in login cookies - allowing this setting
15 to be changed to zero
16 * Promote the "test-md5sum" command to "md5sum".
17 * Added the "whatis" command.
18 * Stop showing the server-code in status outputs - it is no longer used
19 for anything.
20 * Added a compile-time option (--with-tcl) to build in the full
21 TCL interpreter to augment TH1.
22 * Merged the JSON branch into trunk. Disabled by default. Enabled
23 by a compile-time option. Probably it will be enabled by default
24 in some future release.
25 * Update to use SQLite version 3.7.9 plus the alignment fix for Sparc.
26 align
27
28 <h2>Changes For Version 1.20 (2011-10-21)</h2>
29 * Added side-by-side diffs in HTML interface. [0bde74ea1e]
30 * Added support for symlinks. (Controlled by "allow-symlinks" setting,
31 off by default). [e4f1c1fe95]
32
--- www/settings.wiki
+++ www/settings.wiki
@@ -18,11 +18,11 @@
1818
1919
<h3>"Versionable" settings</h3>
2020
2121
Most of the settings control the behaviour of fossil on your local machine, largely acting to reflect your preference on how you want to use Fossil, how you communicate with the server, or options for hosting a repository on the web.
2222
23
-However, for historical reasons, some settings affect how you work with versioned files. These are <tt>binary-glob</tt>, <tt>crnl-glob</tt>, <tt>ignore-glob</tt>, <tt>empty-dirs</tt> and <tt>manifest</tt>. The most important is <tt>ignore-glob</tt> which specifies which files should be ignored when looking for unmanaged files with the <tt>extras</tt> command.
23
+However, for historical reasons, some settings affect how you work with versioned files. These are <tt>allow-symlinks</tt>, <tt>binary-glob</tt>, <tt>crnl-glob</tt>, <tt>ignore-glob</tt>, <tt>empty-dirs</tt> and <tt>manifest</tt>. The most important is <tt>ignore-glob</tt> which specifies which files should be ignored when looking for unmanaged files with the <tt>extras</tt> command.
2424
2525
Because these options can change over time, and the inconvenience of replicating changes, these settings are "versionable". As well as being able to be set using the <tt>settings</tt> command or the web interface, you can created versioned files in the <tt>.fossil-settings</tt> directory named with the setting name. The contents of the file is the value of the setting, and these files are checked in, committed, merged, and so on, as with any other file.
2626
2727
Where a setting is a list of values, such as <tt>ignore-glob</tt>, you can also a newline as a separator as well as a comma.
2828
2929
--- www/settings.wiki
+++ www/settings.wiki
@@ -18,11 +18,11 @@
18
19 <h3>"Versionable" settings</h3>
20
21 Most of the settings control the behaviour of fossil on your local machine, largely acting to reflect your preference on how you want to use Fossil, how you communicate with the server, or options for hosting a repository on the web.
22
23 However, for historical reasons, some settings affect how you work with versioned files. These are <tt>binary-glob</tt>, <tt>crnl-glob</tt>, <tt>ignore-glob</tt>, <tt>empty-dirs</tt> and <tt>manifest</tt>. The most important is <tt>ignore-glob</tt> which specifies which files should be ignored when looking for unmanaged files with the <tt>extras</tt> command.
24
25 Because these options can change over time, and the inconvenience of replicating changes, these settings are "versionable". As well as being able to be set using the <tt>settings</tt> command or the web interface, you can created versioned files in the <tt>.fossil-settings</tt> directory named with the setting name. The contents of the file is the value of the setting, and these files are checked in, committed, merged, and so on, as with any other file.
26
27 Where a setting is a list of values, such as <tt>ignore-glob</tt>, you can also a newline as a separator as well as a comma.
28
29
--- www/settings.wiki
+++ www/settings.wiki
@@ -18,11 +18,11 @@
18
19 <h3>"Versionable" settings</h3>
20
21 Most of the settings control the behaviour of fossil on your local machine, largely acting to reflect your preference on how you want to use Fossil, how you communicate with the server, or options for hosting a repository on the web.
22
23 However, for historical reasons, some settings affect how you work with versioned files. These are <tt>allow-symlinks</tt>, <tt>binary-glob</tt>, <tt>crnl-glob</tt>, <tt>ignore-glob</tt>, <tt>empty-dirs</tt> and <tt>manifest</tt>. The most important is <tt>ignore-glob</tt> which specifies which files should be ignored when looking for unmanaged files with the <tt>extras</tt> command.
24
25 Because these options can change over time, and the inconvenience of replicating changes, these settings are "versionable". As well as being able to be set using the <tt>settings</tt> command or the web interface, you can created versioned files in the <tt>.fossil-settings</tt> directory named with the setting name. The contents of the file is the value of the setting, and these files are checked in, committed, merged, and so on, as with any other file.
26
27 Where a setting is a list of values, such as <tt>ignore-glob</tt>, you can also a newline as a separator as well as a comma.
28
29

Keyboard Shortcuts

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