Fossil SCM

catch up with trunk. Remove C++ style comments from http_ssl.c.

martin.weber 2011-09-06 20:12 UTC msw-hack merge
Commit 0f1c41bc20f8856376cdd65e3355fe399c0361bb
+7
--- auto.def
+++ auto.def
@@ -134,10 +134,17 @@
134134
define FOSSIL_ENABLE_SSL
135135
define-append EXTRA_CFLAGS $cflags
136136
define-append EXTRA_LDFLAGS $ldflags
137137
define-append LIBS -lssl -lcrypto
138138
msg-result "HTTP support enabled"
139
+
140
+ # Silence OpenSSL deprecation warnings on Mac OS X 10.7.
141
+ if {[string match *-darwin* [get-define host]]} {
142
+ if {[cctest -cflags {-Wdeprecated-declarations}]} {
143
+ define-append EXTRA_CFLAGS -Wdeprecated-declarations
144
+ }
145
+ }
139146
} else {
140147
user-error "OpenSSL not found. Consider --with-openssl=none to disable HTTPS support"
141148
}
142149
}
143150
144151
--- auto.def
+++ auto.def
@@ -134,10 +134,17 @@
134 define FOSSIL_ENABLE_SSL
135 define-append EXTRA_CFLAGS $cflags
136 define-append EXTRA_LDFLAGS $ldflags
137 define-append LIBS -lssl -lcrypto
138 msg-result "HTTP support enabled"
 
 
 
 
 
 
 
139 } else {
140 user-error "OpenSSL not found. Consider --with-openssl=none to disable HTTPS support"
141 }
142 }
143
144
--- auto.def
+++ auto.def
@@ -134,10 +134,17 @@
134 define FOSSIL_ENABLE_SSL
135 define-append EXTRA_CFLAGS $cflags
136 define-append EXTRA_LDFLAGS $ldflags
137 define-append LIBS -lssl -lcrypto
138 msg-result "HTTP support enabled"
139
140 # Silence OpenSSL deprecation warnings on Mac OS X 10.7.
141 if {[string match *-darwin* [get-define host]]} {
142 if {[cctest -cflags {-Wdeprecated-declarations}]} {
143 define-append EXTRA_CFLAGS -Wdeprecated-declarations
144 }
145 }
146 } else {
147 user-error "OpenSSL not found. Consider --with-openssl=none to disable HTTPS support"
148 }
149 }
150
151
--- autosetup/autosetup
+++ autosetup/autosetup
@@ -1,11 +1,11 @@
11
#!/bin/sh
22
# Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/
33
# All rights reserved
44
# vim:se syntax=tcl:
55
# \
6
-dir=`dirname "$0"`; exec `"$dir/find-tclsh" || echo false` "$0" "$@"
6
+dir=`dirname "$0"`; exec "`$dir/find-tclsh`" "$0" "$@"
77
88
set autosetup(version) 0.6.2
99
1010
# Can be set to 1 to debug early-init problems
1111
set autosetup(debug) 0
@@ -182,11 +182,11 @@
182182
183183
# @opt-bool option ...
184184
#
185185
# Check each of the named, boolean options and return 1 if any of them have
186186
# been set by the user.
187
-#
187
+#
188188
proc opt-bool {args} {
189189
option-check-names {*}$args
190190
opt_bool ::useropts {*}$args
191191
}
192192
@@ -198,11 +198,11 @@
198198
# If only a single value is required, use something like:
199199
#
200200
## lindex [opt-val $names] end
201201
#
202202
# If no options were set, $default is returned (exactly, not as a list).
203
-#
203
+#
204204
proc opt-val {names {default ""}} {
205205
option-check-names {*}$names
206206
join [opt_val ::useropts $names $default]
207207
}
208208
@@ -396,11 +396,11 @@
396396
# These options are not displayed with --help and can be useful for internal options or as aliases.
397397
#
398398
# For example, --disable-lfs is an alias for --disable=largefile:
399399
#
400400
## lfs=1 largefile=1 => "Disable large file support"
401
-#
401
+#
402402
proc options {optlist} {
403403
# Allow options as a list or args
404404
options-add $optlist "Local Options:"
405405
406406
if {$::autosetup(showhelp)} {
@@ -434,11 +434,11 @@
434434
return $alias
435435
}
436436
}
437437
438438
# @define name ?value=1?
439
-#
439
+#
440440
# Defines the named variable to the given value.
441441
# These (name, value) pairs represent the results of the configuration check
442442
# and are available to be checked, modified and substituted.
443443
#
444444
proc define {name {value 1}} {
@@ -564,10 +564,40 @@
564564
foreach arg $argv {
565565
lappend args [quote-if-needed $arg]
566566
}
567567
join $args
568568
}
569
+
570
+# @suffix suf list
571
+#
572
+# Takes a list and returns a new list with $suf appended
573
+# to each element
574
+#
575
+## suffix .c {a b c} => {a.c b.c c.c}
576
+#
577
+proc suffix {suf list} {
578
+ set result {}
579
+ foreach p $list {
580
+ lappend result $p$suf
581
+ }
582
+ return $result
583
+}
584
+
585
+# @prefix pre list
586
+#
587
+# Takes a list and returns a new list with $pre prepended
588
+# to each element
589
+#
590
+## prefix jim- {a.c b.c} => {jim-a.c jim-b.c}
591
+#
592
+proc prefix {pre list} {
593
+ set result {}
594
+ foreach p $list {
595
+ lappend result $pre$p
596
+ }
597
+ return $result
598
+}
569599
570600
# @find-executable name
571601
#
572602
# Searches the path for an executable with the given name.
573603
# Note that the name may include some parameters, e.g. "cc -mbig-endian",
@@ -999,21 +1029,24 @@
9991029
10001030
# Simple getopt module
10011031
10021032
# Parse everything out of the argv list which looks like an option
10031033
# Knows about --enable-thing and --disable-thing as alternatives for --thing=0 or --thing=1
1034
+# Everything which doesn't look like an option, or is after --, is left unchanged
10041035
proc getopt {argvname} {
10051036
upvar $argvname argv
1037
+ set nargv {}
10061038
10071039
for {set i 0} {$i < [llength $argv]} {incr i} {
10081040
set arg [lindex $argv $i]
10091041
10101042
#dputs arg=$arg
10111043
10121044
if {$arg eq "--"} {
10131045
# End of options
10141046
incr i
1047
+ lappend nargv {*}[lrange $argv $i end]
10151048
break
10161049
}
10171050
10181051
if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} {
10191052
lappend opts($name) $value
@@ -1023,18 +1056,18 @@
10231056
} else {
10241057
set value 1
10251058
}
10261059
lappend opts($name) $value
10271060
} else {
1028
- break
1061
+ lappend nargv $arg
10291062
}
10301063
}
10311064
1032
- #puts "getopt: argv=[join $argv] => [join [lrange $argv $i end]]"
1065
+ #puts "getopt: argv=[join $argv] => [join $nargv]"
10331066
#parray opts
10341067
1035
- set argv [lrange $argv $i end]
1068
+ set argv $nargv
10361069
10371070
return [array get opts]
10381071
}
10391072
10401073
proc opt_val {optarrayname options {default {}}} {
@@ -1264,11 +1297,11 @@
12641297
writefile configure "#!/bin/sh\nWRAPPER=\"\$0\" exec $::autosetup(dir)/autosetup \"\$@\"\n"
12651298
} else {
12661299
writefile configure \
12671300
{#!/bin/sh
12681301
dir="`dirname "$0"`/autosetup"
1269
-WRAPPER="$0" exec `"$dir/find-tclsh" || echo false` "$dir/autosetup" "$@"
1302
+WRAPPER="$0" exec "`$dir/find-tclsh`" "$dir/autosetup" "$@"
12701303
}
12711304
}
12721305
catch {exec chmod 755 configure}
12731306
}
12741307
if {![file exists auto.def]} {
@@ -1279,11 +1312,11 @@
12791312
12801313
# Add any user options here
12811314
options {
12821315
}
12831316
1284
-make-autoconf-h config.h
1317
+make-config-header config.h
12851318
make-template Makefile.in
12861319
}
12871320
}
12881321
if {![file exists Makefile.in]} {
12891322
puts "Note: I don't see Makefile.in. You will probably need to create one."
@@ -1511,11 +1544,11 @@
15111544
string map {\\ /} [env $name {*}$args]
15121545
}
15131546
# Jim uses system() for exec under mingw, so
15141547
# we need to fetch the output ourselves
15151548
proc exec-with-stderr {args} {
1516
- set tmpfile /tmp/autosetup.[format %05x [rand 10000]].tmp
1549
+ set tmpfile auto[format %04x [rand 10000]].tmp
15171550
set rc [catch [list exec {*}$args >$tmpfile 2>&1] result]
15181551
set result [readfile $tmpfile]
15191552
file delete $tmpfile
15201553
return -code $rc $result
15211554
}
15221555
--- autosetup/autosetup
+++ autosetup/autosetup
@@ -1,11 +1,11 @@
1 #!/bin/sh
2 # Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/
3 # All rights reserved
4 # vim:se syntax=tcl:
5 # \
6 dir=`dirname "$0"`; exec `"$dir/find-tclsh" || echo false` "$0" "$@"
7
8 set autosetup(version) 0.6.2
9
10 # Can be set to 1 to debug early-init problems
11 set autosetup(debug) 0
@@ -182,11 +182,11 @@
182
183 # @opt-bool option ...
184 #
185 # Check each of the named, boolean options and return 1 if any of them have
186 # been set by the user.
187 #
188 proc opt-bool {args} {
189 option-check-names {*}$args
190 opt_bool ::useropts {*}$args
191 }
192
@@ -198,11 +198,11 @@
198 # If only a single value is required, use something like:
199 #
200 ## lindex [opt-val $names] end
201 #
202 # If no options were set, $default is returned (exactly, not as a list).
203 #
204 proc opt-val {names {default ""}} {
205 option-check-names {*}$names
206 join [opt_val ::useropts $names $default]
207 }
208
@@ -396,11 +396,11 @@
396 # These options are not displayed with --help and can be useful for internal options or as aliases.
397 #
398 # For example, --disable-lfs is an alias for --disable=largefile:
399 #
400 ## lfs=1 largefile=1 => "Disable large file support"
401 #
402 proc options {optlist} {
403 # Allow options as a list or args
404 options-add $optlist "Local Options:"
405
406 if {$::autosetup(showhelp)} {
@@ -434,11 +434,11 @@
434 return $alias
435 }
436 }
437
438 # @define name ?value=1?
439 #
440 # Defines the named variable to the given value.
441 # These (name, value) pairs represent the results of the configuration check
442 # and are available to be checked, modified and substituted.
443 #
444 proc define {name {value 1}} {
@@ -564,10 +564,40 @@
564 foreach arg $argv {
565 lappend args [quote-if-needed $arg]
566 }
567 join $args
568 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
569
570 # @find-executable name
571 #
572 # Searches the path for an executable with the given name.
573 # Note that the name may include some parameters, e.g. "cc -mbig-endian",
@@ -999,21 +1029,24 @@
999
1000 # Simple getopt module
1001
1002 # Parse everything out of the argv list which looks like an option
1003 # Knows about --enable-thing and --disable-thing as alternatives for --thing=0 or --thing=1
 
1004 proc getopt {argvname} {
1005 upvar $argvname argv
 
1006
1007 for {set i 0} {$i < [llength $argv]} {incr i} {
1008 set arg [lindex $argv $i]
1009
1010 #dputs arg=$arg
1011
1012 if {$arg eq "--"} {
1013 # End of options
1014 incr i
 
1015 break
1016 }
1017
1018 if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} {
1019 lappend opts($name) $value
@@ -1023,18 +1056,18 @@
1023 } else {
1024 set value 1
1025 }
1026 lappend opts($name) $value
1027 } else {
1028 break
1029 }
1030 }
1031
1032 #puts "getopt: argv=[join $argv] => [join [lrange $argv $i end]]"
1033 #parray opts
1034
1035 set argv [lrange $argv $i end]
1036
1037 return [array get opts]
1038 }
1039
1040 proc opt_val {optarrayname options {default {}}} {
@@ -1264,11 +1297,11 @@
1264 writefile configure "#!/bin/sh\nWRAPPER=\"\$0\" exec $::autosetup(dir)/autosetup \"\$@\"\n"
1265 } else {
1266 writefile configure \
1267 {#!/bin/sh
1268 dir="`dirname "$0"`/autosetup"
1269 WRAPPER="$0" exec `"$dir/find-tclsh" || echo false` "$dir/autosetup" "$@"
1270 }
1271 }
1272 catch {exec chmod 755 configure}
1273 }
1274 if {![file exists auto.def]} {
@@ -1279,11 +1312,11 @@
1279
1280 # Add any user options here
1281 options {
1282 }
1283
1284 make-autoconf-h config.h
1285 make-template Makefile.in
1286 }
1287 }
1288 if {![file exists Makefile.in]} {
1289 puts "Note: I don't see Makefile.in. You will probably need to create one."
@@ -1511,11 +1544,11 @@
1511 string map {\\ /} [env $name {*}$args]
1512 }
1513 # Jim uses system() for exec under mingw, so
1514 # we need to fetch the output ourselves
1515 proc exec-with-stderr {args} {
1516 set tmpfile /tmp/autosetup.[format %05x [rand 10000]].tmp
1517 set rc [catch [list exec {*}$args >$tmpfile 2>&1] result]
1518 set result [readfile $tmpfile]
1519 file delete $tmpfile
1520 return -code $rc $result
1521 }
1522
--- autosetup/autosetup
+++ autosetup/autosetup
@@ -1,11 +1,11 @@
1 #!/bin/sh
2 # Copyright (c) 2006-2011 WorkWare Systems http://www.workware.net.au/
3 # All rights reserved
4 # vim:se syntax=tcl:
5 # \
6 dir=`dirname "$0"`; exec "`$dir/find-tclsh`" "$0" "$@"
7
8 set autosetup(version) 0.6.2
9
10 # Can be set to 1 to debug early-init problems
11 set autosetup(debug) 0
@@ -182,11 +182,11 @@
182
183 # @opt-bool option ...
184 #
185 # Check each of the named, boolean options and return 1 if any of them have
186 # been set by the user.
187 #
188 proc opt-bool {args} {
189 option-check-names {*}$args
190 opt_bool ::useropts {*}$args
191 }
192
@@ -198,11 +198,11 @@
198 # If only a single value is required, use something like:
199 #
200 ## lindex [opt-val $names] end
201 #
202 # If no options were set, $default is returned (exactly, not as a list).
203 #
204 proc opt-val {names {default ""}} {
205 option-check-names {*}$names
206 join [opt_val ::useropts $names $default]
207 }
208
@@ -396,11 +396,11 @@
396 # These options are not displayed with --help and can be useful for internal options or as aliases.
397 #
398 # For example, --disable-lfs is an alias for --disable=largefile:
399 #
400 ## lfs=1 largefile=1 => "Disable large file support"
401 #
402 proc options {optlist} {
403 # Allow options as a list or args
404 options-add $optlist "Local Options:"
405
406 if {$::autosetup(showhelp)} {
@@ -434,11 +434,11 @@
434 return $alias
435 }
436 }
437
438 # @define name ?value=1?
439 #
440 # Defines the named variable to the given value.
441 # These (name, value) pairs represent the results of the configuration check
442 # and are available to be checked, modified and substituted.
443 #
444 proc define {name {value 1}} {
@@ -564,10 +564,40 @@
564 foreach arg $argv {
565 lappend args [quote-if-needed $arg]
566 }
567 join $args
568 }
569
570 # @suffix suf list
571 #
572 # Takes a list and returns a new list with $suf appended
573 # to each element
574 #
575 ## suffix .c {a b c} => {a.c b.c c.c}
576 #
577 proc suffix {suf list} {
578 set result {}
579 foreach p $list {
580 lappend result $p$suf
581 }
582 return $result
583 }
584
585 # @prefix pre list
586 #
587 # Takes a list and returns a new list with $pre prepended
588 # to each element
589 #
590 ## prefix jim- {a.c b.c} => {jim-a.c jim-b.c}
591 #
592 proc prefix {pre list} {
593 set result {}
594 foreach p $list {
595 lappend result $pre$p
596 }
597 return $result
598 }
599
600 # @find-executable name
601 #
602 # Searches the path for an executable with the given name.
603 # Note that the name may include some parameters, e.g. "cc -mbig-endian",
@@ -999,21 +1029,24 @@
1029
1030 # Simple getopt module
1031
1032 # Parse everything out of the argv list which looks like an option
1033 # Knows about --enable-thing and --disable-thing as alternatives for --thing=0 or --thing=1
1034 # Everything which doesn't look like an option, or is after --, is left unchanged
1035 proc getopt {argvname} {
1036 upvar $argvname argv
1037 set nargv {}
1038
1039 for {set i 0} {$i < [llength $argv]} {incr i} {
1040 set arg [lindex $argv $i]
1041
1042 #dputs arg=$arg
1043
1044 if {$arg eq "--"} {
1045 # End of options
1046 incr i
1047 lappend nargv {*}[lrange $argv $i end]
1048 break
1049 }
1050
1051 if {[regexp {^--([^=][^=]+)=(.*)$} $arg -> name value]} {
1052 lappend opts($name) $value
@@ -1023,18 +1056,18 @@
1056 } else {
1057 set value 1
1058 }
1059 lappend opts($name) $value
1060 } else {
1061 lappend nargv $arg
1062 }
1063 }
1064
1065 #puts "getopt: argv=[join $argv] => [join $nargv]"
1066 #parray opts
1067
1068 set argv $nargv
1069
1070 return [array get opts]
1071 }
1072
1073 proc opt_val {optarrayname options {default {}}} {
@@ -1264,11 +1297,11 @@
1297 writefile configure "#!/bin/sh\nWRAPPER=\"\$0\" exec $::autosetup(dir)/autosetup \"\$@\"\n"
1298 } else {
1299 writefile configure \
1300 {#!/bin/sh
1301 dir="`dirname "$0"`/autosetup"
1302 WRAPPER="$0" exec "`$dir/find-tclsh`" "$dir/autosetup" "$@"
1303 }
1304 }
1305 catch {exec chmod 755 configure}
1306 }
1307 if {![file exists auto.def]} {
@@ -1279,11 +1312,11 @@
1312
1313 # Add any user options here
1314 options {
1315 }
1316
1317 make-config-header config.h
1318 make-template Makefile.in
1319 }
1320 }
1321 if {![file exists Makefile.in]} {
1322 puts "Note: I don't see Makefile.in. You will probably need to create one."
@@ -1511,11 +1544,11 @@
1544 string map {\\ /} [env $name {*}$args]
1545 }
1546 # Jim uses system() for exec under mingw, so
1547 # we need to fetch the output ourselves
1548 proc exec-with-stderr {args} {
1549 set tmpfile auto[format %04x [rand 10000]].tmp
1550 set rc [catch [list exec {*}$args >$tmpfile 2>&1] result]
1551 set result [readfile $tmpfile]
1552 file delete $tmpfile
1553 return -code $rc $result
1554 }
1555
--- autosetup/cc-lib.tcl
+++ autosetup/cc-lib.tcl
@@ -48,21 +48,21 @@
4848
cc-check-includes sys/types.h sys/param.h
4949
set rc 0
5050
msg-checking "Checking endian..."
5151
cc-with {-includes {sys/types.h sys/param.h}} {
5252
if {[cctest -code {
53
- #if! defined(BIG_ENDIAN) || !defined(BYTE_ORDER)
53
+ #if !defined(BIG_ENDIAN) || !defined(BYTE_ORDER)
5454
#error unknown
5555
#elif BYTE_ORDER != BIG_ENDIAN
5656
#error little
5757
#endif
5858
}]} {
5959
define-feature big-endian
6060
msg-result "big"
6161
set rc 1
6262
} elseif {[cctest -code {
63
- #if! defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER)
63
+ #if !defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER)
6464
#error unknown
6565
#elif BYTE_ORDER != LITTLE_ENDIAN
6666
#error big
6767
#endif
6868
}]} {
6969
--- autosetup/cc-lib.tcl
+++ autosetup/cc-lib.tcl
@@ -48,21 +48,21 @@
48 cc-check-includes sys/types.h sys/param.h
49 set rc 0
50 msg-checking "Checking endian..."
51 cc-with {-includes {sys/types.h sys/param.h}} {
52 if {[cctest -code {
53 #if! defined(BIG_ENDIAN) || !defined(BYTE_ORDER)
54 #error unknown
55 #elif BYTE_ORDER != BIG_ENDIAN
56 #error little
57 #endif
58 }]} {
59 define-feature big-endian
60 msg-result "big"
61 set rc 1
62 } elseif {[cctest -code {
63 #if! defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER)
64 #error unknown
65 #elif BYTE_ORDER != LITTLE_ENDIAN
66 #error big
67 #endif
68 }]} {
69
--- autosetup/cc-lib.tcl
+++ autosetup/cc-lib.tcl
@@ -48,21 +48,21 @@
48 cc-check-includes sys/types.h sys/param.h
49 set rc 0
50 msg-checking "Checking endian..."
51 cc-with {-includes {sys/types.h sys/param.h}} {
52 if {[cctest -code {
53 #if !defined(BIG_ENDIAN) || !defined(BYTE_ORDER)
54 #error unknown
55 #elif BYTE_ORDER != BIG_ENDIAN
56 #error little
57 #endif
58 }]} {
59 define-feature big-endian
60 msg-result "big"
61 set rc 1
62 } elseif {[cctest -code {
63 #if !defined(LITTLE_ENDIAN) || !defined(BYTE_ORDER)
64 #error unknown
65 #elif BYTE_ORDER != LITTLE_ENDIAN
66 #error big
67 #endif
68 }]} {
69
--- autosetup/cc-shared.tcl
+++ autosetup/cc-shared.tcl
@@ -35,14 +35,29 @@
3535
}
3636
*-*-cygwin {
3737
define SH_LDFLAGS -shared
3838
define SHOBJ_LDFLAGS -shared
3939
}
40
+ *-*-solaris* {
41
+ # XXX: These haven't been fully tested.
42
+ #define SH_LINKFLAGS -Wl,-export-dynamic
43
+ define SH_CFLAGS -Kpic
44
+ define SHOBJ_CFLAGS -Kpic
45
+ define SHOBJ_LDFLAGS "-G"
46
+ }
47
+ *-*-hpux {
48
+ # XXX: These haven't been tested
49
+ define SH_LINKFLAGS -Wl,+s
50
+ define SH_CFLAGS +z
51
+ define SHOBJ_CFLAGS "+O3 +z"
52
+ define SHOBJ_LDFLAGS -b
53
+ define LD_LIBRARY_PATH SHLIB_PATH
54
+ }
4055
* {
4156
# Generic Unix settings
4257
define SH_LINKFLAGS -rdynamic
43
- define SH_CFLAGS -fPIC
58
+ define SH_CFLAGS -fpic
4459
define SH_LDFLAGS -shared
45
- define SHOBJ_CFLAGS -fPIC
60
+ define SHOBJ_CFLAGS -fpic
4661
define SHOBJ_LDFLAGS "-shared -nostartfiles"
4762
}
4863
}
4964
--- autosetup/cc-shared.tcl
+++ autosetup/cc-shared.tcl
@@ -35,14 +35,29 @@
35 }
36 *-*-cygwin {
37 define SH_LDFLAGS -shared
38 define SHOBJ_LDFLAGS -shared
39 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40 * {
41 # Generic Unix settings
42 define SH_LINKFLAGS -rdynamic
43 define SH_CFLAGS -fPIC
44 define SH_LDFLAGS -shared
45 define SHOBJ_CFLAGS -fPIC
46 define SHOBJ_LDFLAGS "-shared -nostartfiles"
47 }
48 }
49
--- autosetup/cc-shared.tcl
+++ autosetup/cc-shared.tcl
@@ -35,14 +35,29 @@
35 }
36 *-*-cygwin {
37 define SH_LDFLAGS -shared
38 define SHOBJ_LDFLAGS -shared
39 }
40 *-*-solaris* {
41 # XXX: These haven't been fully tested.
42 #define SH_LINKFLAGS -Wl,-export-dynamic
43 define SH_CFLAGS -Kpic
44 define SHOBJ_CFLAGS -Kpic
45 define SHOBJ_LDFLAGS "-G"
46 }
47 *-*-hpux {
48 # XXX: These haven't been tested
49 define SH_LINKFLAGS -Wl,+s
50 define SH_CFLAGS +z
51 define SHOBJ_CFLAGS "+O3 +z"
52 define SHOBJ_LDFLAGS -b
53 define LD_LIBRARY_PATH SHLIB_PATH
54 }
55 * {
56 # Generic Unix settings
57 define SH_LINKFLAGS -rdynamic
58 define SH_CFLAGS -fpic
59 define SH_LDFLAGS -shared
60 define SHOBJ_CFLAGS -fpic
61 define SHOBJ_LDFLAGS "-shared -nostartfiles"
62 }
63 }
64
--- autosetup/cc.tcl
+++ autosetup/cc.tcl
@@ -331,17 +331,11 @@
331331
# simply replaces the existing value.
332332
#
333333
# Returns the previous settings
334334
proc cc-update-settings {args} {
335335
set prev [cc-get-settings]
336
- array set new $prev
337
-
338
- foreach {name value} $args {
339
- set new($name) $value
340
- }
341
- cc-store-settings $new
342
-
336
+ cc-store-settings [dict merge $prev $args]
343337
return $prev
344338
}
345339
346340
# @cc-with settings ?{ script }?
347341
#
@@ -416,11 +410,11 @@
416410
#
417411
# Any failures are recorded in 'config.log'
418412
#
419413
proc cctest {args} {
420414
set src conftest__.c
421
- set tmp conftest__.o
415
+ set tmp conftest__
422416
423417
# Easiest way to merge in the settings
424418
cc-with $args {
425419
array set opts [cc-get-settings]
426420
}
@@ -468,10 +462,11 @@
468462
autosetup-error "cctest called with unknown language: $opts(-lang)"
469463
}
470464
}
471465
472466
if {!$opts(-link)} {
467
+ set tmp conftest__.o
473468
lappend cmdline -c
474469
}
475470
lappend cmdline {*}$opts(-cflags)
476471
477472
switch -glob -- [get-define host] {
478473
--- autosetup/cc.tcl
+++ autosetup/cc.tcl
@@ -331,17 +331,11 @@
331 # simply replaces the existing value.
332 #
333 # Returns the previous settings
334 proc cc-update-settings {args} {
335 set prev [cc-get-settings]
336 array set new $prev
337
338 foreach {name value} $args {
339 set new($name) $value
340 }
341 cc-store-settings $new
342
343 return $prev
344 }
345
346 # @cc-with settings ?{ script }?
347 #
@@ -416,11 +410,11 @@
416 #
417 # Any failures are recorded in 'config.log'
418 #
419 proc cctest {args} {
420 set src conftest__.c
421 set tmp conftest__.o
422
423 # Easiest way to merge in the settings
424 cc-with $args {
425 array set opts [cc-get-settings]
426 }
@@ -468,10 +462,11 @@
468 autosetup-error "cctest called with unknown language: $opts(-lang)"
469 }
470 }
471
472 if {!$opts(-link)} {
 
473 lappend cmdline -c
474 }
475 lappend cmdline {*}$opts(-cflags)
476
477 switch -glob -- [get-define host] {
478
--- autosetup/cc.tcl
+++ autosetup/cc.tcl
@@ -331,17 +331,11 @@
331 # simply replaces the existing value.
332 #
333 # Returns the previous settings
334 proc cc-update-settings {args} {
335 set prev [cc-get-settings]
336 cc-store-settings [dict merge $prev $args]
 
 
 
 
 
 
337 return $prev
338 }
339
340 # @cc-with settings ?{ script }?
341 #
@@ -416,11 +410,11 @@
410 #
411 # Any failures are recorded in 'config.log'
412 #
413 proc cctest {args} {
414 set src conftest__.c
415 set tmp conftest__
416
417 # Easiest way to merge in the settings
418 cc-with $args {
419 array set opts [cc-get-settings]
420 }
@@ -468,10 +462,11 @@
462 autosetup-error "cctest called with unknown language: $opts(-lang)"
463 }
464 }
465
466 if {!$opts(-link)} {
467 set tmp conftest__.o
468 lappend cmdline -c
469 }
470 lappend cmdline {*}$opts(-cflags)
471
472 switch -glob -- [get-define host] {
473
--- autosetup/system.tcl
+++ autosetup/system.tcl
@@ -6,28 +6,32 @@
66
# This module supports common system interrogation and options
77
# such as --host, --build, --prefix, and setting srcdir, builddir, and EXEXT.
88
#
99
# It also support the 'feature' naming convention, where searching
1010
# for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
11
-
12
-# Note that the hidden options are supported for autoconf compatibility
13
-
11
+#
1412
module-options {
1513
host:host-alias => {a complete or partial cpu-vendor-opsys for the system where
1614
the application will run (defaults to the same value as --build)}
1715
build:build-alias => {a complete or partial cpu-vendor-opsys for the system
1816
where the application will be built (defaults to the
1917
result of running config.guess)}
2018
prefix:dir => {the target directory for the build (defaults to /usr/local)}
2119
20
+ # These (hidden) options are supported for autoconf/automake compatibility
21
+ exec-prefix:
22
+ bindir:
23
+ sbindir:
2224
includedir:
2325
mandir:
2426
infodir:
2527
libexecdir:
28
+ datadir:
29
+ libdir:
2630
sysconfdir:
31
+ sharedstatedir:
2732
localstatedir:
28
-
2933
maintainer-mode=0
3034
dependency-tracking=0
3135
}
3236
3337
# Returns 1 if exists, or 0 if not
@@ -107,10 +111,28 @@
107111
#
108112
# The special value @srcdir@ is subsituted with the relative
109113
# path to the source directory from the directory where the output
110114
# file is created. Use @top_srcdir@ for the absolute path.
111115
#
116
+# Conditional sections may be specified as follows:
117
+## @if name == value
118
+## lines
119
+## @else
120
+## lines
121
+## @endif
122
+#
123
+# Where 'name' is a defined variable name and @else is optional.
124
+# If the expression does not match, all lines through '@endif' are ignored.
125
+#
126
+# The alternative forms may also be used:
127
+## @if name
128
+## @if name != value
129
+#
130
+# Where the first form is true if the variable is defined, but not empty or 0
131
+#
132
+# Currently these expressions can't be nested.
133
+#
112134
proc make-template {template {out {}}} {
113135
set infile [file join $::autosetup(srcdir) $template]
114136
115137
if {![file exists $infile]} {
116138
user-error "Template $template is missing"
@@ -136,11 +158,43 @@
136158
137159
set mapping {}
138160
foreach {n v} [array get ::define] {
139161
lappend mapping @$n@ $v
140162
}
141
- writefile $out [string map $mapping [readfile $infile]]\n
163
+ set result {}
164
+ foreach line [split [readfile $infile] \n] {
165
+ if {[info exists cond]} {
166
+ set l [string trimright $line]
167
+ if {$l eq "@endif"} {
168
+ unset cond
169
+ continue
170
+ }
171
+ if {$l eq "@else"} {
172
+ set cond [expr {!$cond}]
173
+ continue
174
+ }
175
+ if {$cond} {
176
+ lappend result $line
177
+ }
178
+ continue
179
+ }
180
+ if {[regexp {^@if\s+(\w+)(.*)} $line -> name expression]} {
181
+ lassign $expression equal value
182
+ set varval [get-define $name ""]
183
+ if {$equal eq ""} {
184
+ set cond [expr {$varval ni {"" 0}}]
185
+ } else {
186
+ set cond [expr {$varval eq $value}]
187
+ if {$equal ne "=="} {
188
+ set cond [expr {!$cond}]
189
+ }
190
+ }
191
+ continue
192
+ }
193
+ lappend result $line
194
+ }
195
+ writefile $out [string map $mapping [join $result \n]]\n
142196
143197
msg-result "Created [relative-path $out] from [relative-path $template]"
144198
}
145199
146200
# build/host tuples and cross-compilation prefix
@@ -171,23 +225,27 @@
171225
define builddir $autosetup(builddir)
172226
define srcdir $autosetup(srcdir)
173227
# Allow this to come from the environment
174228
define top_srcdir [get-env top_srcdir [get-define srcdir]]
175229
176
-# And less common ones too
177
-define exec_prefix \${prefix}
178
-define bindir \${exec_prefix}/bin
179
-define sbindir \${exec_prefix}/sbin
180
-define libexecdir [get-env libexecdir \${exec_prefix}/libexec]
181
-define datadir \${prefix}/share
182
-define sysconfdir [get-env sysconfdir \${prefix}/etc]
183
-define sharedstatedir \${prefix}/com
184
-define localstatedir [get-env localstatedir \${prefix}/var]
185
-define libdir \${exec_prefix}/lib
186
-define infodir [get-env infodir \${prefix}/share/info]
187
-define mandir [get-env mandir \${prefix}/share/man]
188
-define includedir [get-env includdir \${prefix}/include]
230
+# autoconf supports all of these
231
+define exec_prefix [opt-val exec-prefix [get-env exec-prefix \${prefix}]]
232
+foreach {name defpath} {
233
+ bindir \${exec_prefix}/bin
234
+ sbindir \${exec_prefix}/sbin
235
+ libexecdir \${exec_prefix}/libexec
236
+ libdir \${exec_prefix}/lib
237
+ datadir \${prefix}/share
238
+ sysconfdir \${prefix}/etc
239
+ sharedstatedir \${prefix}/com
240
+ localstatedir \${prefix}/var
241
+ infodir \${prefix}/share/info
242
+ mandir \${prefix}/share/man
243
+ includedir \${prefix}/include
244
+} {
245
+ define $name [opt-val $name [get-env $name $defpath]]
246
+}
189247
190248
define SHELL [get-env SHELL [find-an-executable sh bash ksh]]
191249
192250
# Windows vs. non-Windows
193251
switch -glob -- [get-define host] {
194252
--- autosetup/system.tcl
+++ autosetup/system.tcl
@@ -6,28 +6,32 @@
6 # This module supports common system interrogation and options
7 # such as --host, --build, --prefix, and setting srcdir, builddir, and EXEXT.
8 #
9 # It also support the 'feature' naming convention, where searching
10 # for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
11
12 # Note that the hidden options are supported for autoconf compatibility
13
14 module-options {
15 host:host-alias => {a complete or partial cpu-vendor-opsys for the system where
16 the application will run (defaults to the same value as --build)}
17 build:build-alias => {a complete or partial cpu-vendor-opsys for the system
18 where the application will be built (defaults to the
19 result of running config.guess)}
20 prefix:dir => {the target directory for the build (defaults to /usr/local)}
21
 
 
 
 
22 includedir:
23 mandir:
24 infodir:
25 libexecdir:
 
 
26 sysconfdir:
 
27 localstatedir:
28
29 maintainer-mode=0
30 dependency-tracking=0
31 }
32
33 # Returns 1 if exists, or 0 if not
@@ -107,10 +111,28 @@
107 #
108 # The special value @srcdir@ is subsituted with the relative
109 # path to the source directory from the directory where the output
110 # file is created. Use @top_srcdir@ for the absolute path.
111 #
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112 proc make-template {template {out {}}} {
113 set infile [file join $::autosetup(srcdir) $template]
114
115 if {![file exists $infile]} {
116 user-error "Template $template is missing"
@@ -136,11 +158,43 @@
136
137 set mapping {}
138 foreach {n v} [array get ::define] {
139 lappend mapping @$n@ $v
140 }
141 writefile $out [string map $mapping [readfile $infile]]\n
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
143 msg-result "Created [relative-path $out] from [relative-path $template]"
144 }
145
146 # build/host tuples and cross-compilation prefix
@@ -171,23 +225,27 @@
171 define builddir $autosetup(builddir)
172 define srcdir $autosetup(srcdir)
173 # Allow this to come from the environment
174 define top_srcdir [get-env top_srcdir [get-define srcdir]]
175
176 # And less common ones too
177 define exec_prefix \${prefix}
178 define bindir \${exec_prefix}/bin
179 define sbindir \${exec_prefix}/sbin
180 define libexecdir [get-env libexecdir \${exec_prefix}/libexec]
181 define datadir \${prefix}/share
182 define sysconfdir [get-env sysconfdir \${prefix}/etc]
183 define sharedstatedir \${prefix}/com
184 define localstatedir [get-env localstatedir \${prefix}/var]
185 define libdir \${exec_prefix}/lib
186 define infodir [get-env infodir \${prefix}/share/info]
187 define mandir [get-env mandir \${prefix}/share/man]
188 define includedir [get-env includdir \${prefix}/include]
 
 
 
 
189
190 define SHELL [get-env SHELL [find-an-executable sh bash ksh]]
191
192 # Windows vs. non-Windows
193 switch -glob -- [get-define host] {
194
--- autosetup/system.tcl
+++ autosetup/system.tcl
@@ -6,28 +6,32 @@
6 # This module supports common system interrogation and options
7 # such as --host, --build, --prefix, and setting srcdir, builddir, and EXEXT.
8 #
9 # It also support the 'feature' naming convention, where searching
10 # for a feature such as sys/type.h defines HAVE_SYS_TYPES_H
11 #
 
 
12 module-options {
13 host:host-alias => {a complete or partial cpu-vendor-opsys for the system where
14 the application will run (defaults to the same value as --build)}
15 build:build-alias => {a complete or partial cpu-vendor-opsys for the system
16 where the application will be built (defaults to the
17 result of running config.guess)}
18 prefix:dir => {the target directory for the build (defaults to /usr/local)}
19
20 # These (hidden) options are supported for autoconf/automake compatibility
21 exec-prefix:
22 bindir:
23 sbindir:
24 includedir:
25 mandir:
26 infodir:
27 libexecdir:
28 datadir:
29 libdir:
30 sysconfdir:
31 sharedstatedir:
32 localstatedir:
 
33 maintainer-mode=0
34 dependency-tracking=0
35 }
36
37 # Returns 1 if exists, or 0 if not
@@ -107,10 +111,28 @@
111 #
112 # The special value @srcdir@ is subsituted with the relative
113 # path to the source directory from the directory where the output
114 # file is created. Use @top_srcdir@ for the absolute path.
115 #
116 # Conditional sections may be specified as follows:
117 ## @if name == value
118 ## lines
119 ## @else
120 ## lines
121 ## @endif
122 #
123 # Where 'name' is a defined variable name and @else is optional.
124 # If the expression does not match, all lines through '@endif' are ignored.
125 #
126 # The alternative forms may also be used:
127 ## @if name
128 ## @if name != value
129 #
130 # Where the first form is true if the variable is defined, but not empty or 0
131 #
132 # Currently these expressions can't be nested.
133 #
134 proc make-template {template {out {}}} {
135 set infile [file join $::autosetup(srcdir) $template]
136
137 if {![file exists $infile]} {
138 user-error "Template $template is missing"
@@ -136,11 +158,43 @@
158
159 set mapping {}
160 foreach {n v} [array get ::define] {
161 lappend mapping @$n@ $v
162 }
163 set result {}
164 foreach line [split [readfile $infile] \n] {
165 if {[info exists cond]} {
166 set l [string trimright $line]
167 if {$l eq "@endif"} {
168 unset cond
169 continue
170 }
171 if {$l eq "@else"} {
172 set cond [expr {!$cond}]
173 continue
174 }
175 if {$cond} {
176 lappend result $line
177 }
178 continue
179 }
180 if {[regexp {^@if\s+(\w+)(.*)} $line -> name expression]} {
181 lassign $expression equal value
182 set varval [get-define $name ""]
183 if {$equal eq ""} {
184 set cond [expr {$varval ni {"" 0}}]
185 } else {
186 set cond [expr {$varval eq $value}]
187 if {$equal ne "=="} {
188 set cond [expr {!$cond}]
189 }
190 }
191 continue
192 }
193 lappend result $line
194 }
195 writefile $out [string map $mapping [join $result \n]]\n
196
197 msg-result "Created [relative-path $out] from [relative-path $template]"
198 }
199
200 # build/host tuples and cross-compilation prefix
@@ -171,23 +225,27 @@
225 define builddir $autosetup(builddir)
226 define srcdir $autosetup(srcdir)
227 # Allow this to come from the environment
228 define top_srcdir [get-env top_srcdir [get-define srcdir]]
229
230 # autoconf supports all of these
231 define exec_prefix [opt-val exec-prefix [get-env exec-prefix \${prefix}]]
232 foreach {name defpath} {
233 bindir \${exec_prefix}/bin
234 sbindir \${exec_prefix}/sbin
235 libexecdir \${exec_prefix}/libexec
236 libdir \${exec_prefix}/lib
237 datadir \${prefix}/share
238 sysconfdir \${prefix}/etc
239 sharedstatedir \${prefix}/com
240 localstatedir \${prefix}/var
241 infodir \${prefix}/share/info
242 mandir \${prefix}/share/man
243 includedir \${prefix}/include
244 } {
245 define $name [opt-val $name [get-env $name $defpath]]
246 }
247
248 define SHELL [get-env SHELL [find-an-executable sh bash ksh]]
249
250 # Windows vs. non-Windows
251 switch -glob -- [get-define host] {
252
+1 -1
--- configure
+++ configure
@@ -1,3 +1,3 @@
11
#!/bin/sh
22
dir="`dirname "$0"`/autosetup"
3
-WRAPPER="$0" exec `"$dir/find-tclsh" || echo false` "$dir/autosetup" "$@"
3
+WRAPPER="$0" exec "`$dir/find-tclsh`" "$dir/autosetup" "$@"
44
--- configure
+++ configure
@@ -1,3 +1,3 @@
1 #!/bin/sh
2 dir="`dirname "$0"`/autosetup"
3 WRAPPER="$0" exec `"$dir/find-tclsh" || echo false` "$dir/autosetup" "$@"
4
--- configure
+++ configure
@@ -1,3 +1,3 @@
1 #!/bin/sh
2 dir="`dirname "$0"`/autosetup"
3 WRAPPER="$0" exec "`$dir/find-tclsh`" "$dir/autosetup" "$@"
4
+4 -4
--- src/add.c
+++ src/add.c
@@ -106,13 +106,13 @@
106106
db_multi_exec("UPDATE vfile SET deleted=0"
107107
" WHERE pathname=%Q COLLATE %s", zPath, zCollate);
108108
}else{
109109
char *zFullname = mprintf("%s%s", g.zLocalRoot, zPath);
110110
db_multi_exec(
111
- "INSERT INTO vfile(vid,deleted,rid,mrid,pathname,isexe)"
112
- "VALUES(%d,0,0,0,%Q,%d)",
113
- vid, zPath, file_isexe(zFullname));
111
+ "INSERT INTO vfile(vid,deleted,rid,mrid,pathname,isexe,islink)"
112
+ "VALUES(%d,0,0,0,%Q,%d,%d)",
113
+ vid, zPath, file_isexe(zFullname), file_islink(zFullname));
114114
fossil_free(zFullname);
115115
}
116116
if( db_changes() ){
117117
fossil_print("ADDED %s\n", zPath);
118118
return 1;
@@ -426,11 +426,11 @@
426426
const char * zFile;
427427
const char * zPath;
428428
429429
zFile = db_column_text(&q, 0);
430430
zPath = db_column_text(&q, 1);
431
- if( !file_isfile(zPath) ){
431
+ if( !file_isfile_or_link(zPath) ){
432432
if( !isTest ){
433433
db_multi_exec("UPDATE vfile SET deleted=1 WHERE pathname=%Q", zFile);
434434
}
435435
fossil_print("DELETED %s\n", zFile);
436436
nDelete++;
437437
--- src/add.c
+++ src/add.c
@@ -106,13 +106,13 @@
106 db_multi_exec("UPDATE vfile SET deleted=0"
107 " WHERE pathname=%Q COLLATE %s", zPath, zCollate);
108 }else{
109 char *zFullname = mprintf("%s%s", g.zLocalRoot, zPath);
110 db_multi_exec(
111 "INSERT INTO vfile(vid,deleted,rid,mrid,pathname,isexe)"
112 "VALUES(%d,0,0,0,%Q,%d)",
113 vid, zPath, file_isexe(zFullname));
114 fossil_free(zFullname);
115 }
116 if( db_changes() ){
117 fossil_print("ADDED %s\n", zPath);
118 return 1;
@@ -426,11 +426,11 @@
426 const char * zFile;
427 const char * zPath;
428
429 zFile = db_column_text(&q, 0);
430 zPath = db_column_text(&q, 1);
431 if( !file_isfile(zPath) ){
432 if( !isTest ){
433 db_multi_exec("UPDATE vfile SET deleted=1 WHERE pathname=%Q", zFile);
434 }
435 fossil_print("DELETED %s\n", zFile);
436 nDelete++;
437
--- src/add.c
+++ src/add.c
@@ -106,13 +106,13 @@
106 db_multi_exec("UPDATE vfile SET deleted=0"
107 " WHERE pathname=%Q COLLATE %s", zPath, zCollate);
108 }else{
109 char *zFullname = mprintf("%s%s", g.zLocalRoot, zPath);
110 db_multi_exec(
111 "INSERT INTO vfile(vid,deleted,rid,mrid,pathname,isexe,islink)"
112 "VALUES(%d,0,0,0,%Q,%d,%d)",
113 vid, zPath, file_isexe(zFullname), file_islink(zFullname));
114 fossil_free(zFullname);
115 }
116 if( db_changes() ){
117 fossil_print("ADDED %s\n", zPath);
118 return 1;
@@ -426,11 +426,11 @@
426 const char * zFile;
427 const char * zPath;
428
429 zFile = db_column_text(&q, 0);
430 zPath = db_column_text(&q, 1);
431 if( !file_isfile_or_link(zPath) ){
432 if( !isTest ){
433 db_multi_exec("UPDATE vfile SET deleted=1 WHERE pathname=%Q", zFile);
434 }
435 fossil_print("DELETED %s\n", zFile);
436 nDelete++;
437
+1 -1
--- src/bisect.c
+++ src/bisect.c
@@ -42,11 +42,11 @@
4242
}
4343
bisect.good = db_lget_int("bisect-good", 0);
4444
if( bisect.good==0 ){
4545
fossil_fatal("no \"good\" version has been identified");
4646
}
47
- p = path_shortest(bisect.good, bisect.bad, bisect_option("direct-only"));
47
+ p = path_shortest(bisect.good, bisect.bad, bisect_option("direct-only"), 0);
4848
if( p==0 ){
4949
char *zBad = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.bad);
5050
char *zGood = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.good);
5151
fossil_fatal("no path from good ([%S]) to bad ([%S]) or back",
5252
zGood, zBad);
5353
--- src/bisect.c
+++ src/bisect.c
@@ -42,11 +42,11 @@
42 }
43 bisect.good = db_lget_int("bisect-good", 0);
44 if( bisect.good==0 ){
45 fossil_fatal("no \"good\" version has been identified");
46 }
47 p = path_shortest(bisect.good, bisect.bad, bisect_option("direct-only"));
48 if( p==0 ){
49 char *zBad = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.bad);
50 char *zGood = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.good);
51 fossil_fatal("no path from good ([%S]) to bad ([%S]) or back",
52 zGood, zBad);
53
--- src/bisect.c
+++ src/bisect.c
@@ -42,11 +42,11 @@
42 }
43 bisect.good = db_lget_int("bisect-good", 0);
44 if( bisect.good==0 ){
45 fossil_fatal("no \"good\" version has been identified");
46 }
47 p = path_shortest(bisect.good, bisect.bad, bisect_option("direct-only"), 0);
48 if( p==0 ){
49 char *zBad = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.bad);
50 char *zGood = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", bisect.good);
51 fossil_fatal("no path from good ([%S]) to bad ([%S]) or back",
52 zGood, zBad);
53
+26
--- src/blob.c
+++ src/blob.c
@@ -701,10 +701,36 @@
701701
if( got<size ){
702702
blob_resize(pBlob, got);
703703
}
704704
return got;
705705
}
706
+
707
+/*
708
+** Reads symlink destination path and puts int into blob.
709
+** Any prior content of the blob is discarded, not freed.
710
+**
711
+** Returns length of destination path.
712
+**
713
+** On windows, zeros blob and returns 0.
714
+*/
715
+int blob_read_link(Blob *pBlob, const char *zFilename){
716
+#if !defined(_WIN32)
717
+ char zBuf[1024];
718
+ ssize_t len = readlink(zFilename, zBuf, 1023);
719
+ if( len < 0 ){
720
+ fossil_panic("cannot read symbolic link %s", zFilename);
721
+ }
722
+ zBuf[len] = 0; /* null-terminate */
723
+ blob_zero(pBlob);
724
+ blob_appendf(pBlob, "%s", zBuf);
725
+ return len;
726
+#else
727
+ blob_zero(pBlob);
728
+ return 0;
729
+#endif
730
+}
731
+
706732
707733
/*
708734
** Write the content of a blob into a file.
709735
**
710736
** If the filename is blank or "-" then write to standard output.
711737
--- src/blob.c
+++ src/blob.c
@@ -701,10 +701,36 @@
701 if( got<size ){
702 blob_resize(pBlob, got);
703 }
704 return got;
705 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
706
707 /*
708 ** Write the content of a blob into a file.
709 **
710 ** If the filename is blank or "-" then write to standard output.
711
--- src/blob.c
+++ src/blob.c
@@ -701,10 +701,36 @@
701 if( got<size ){
702 blob_resize(pBlob, got);
703 }
704 return got;
705 }
706
707 /*
708 ** Reads symlink destination path and puts int into blob.
709 ** Any prior content of the blob is discarded, not freed.
710 **
711 ** Returns length of destination path.
712 **
713 ** On windows, zeros blob and returns 0.
714 */
715 int blob_read_link(Blob *pBlob, const char *zFilename){
716 #if !defined(_WIN32)
717 char zBuf[1024];
718 ssize_t len = readlink(zFilename, zBuf, 1023);
719 if( len < 0 ){
720 fossil_panic("cannot read symbolic link %s", zFilename);
721 }
722 zBuf[len] = 0; /* null-terminate */
723 blob_zero(pBlob);
724 blob_appendf(pBlob, "%s", zBuf);
725 return len;
726 #else
727 blob_zero(pBlob);
728 return 0;
729 #endif
730 }
731
732
733 /*
734 ** Write the content of a blob into a file.
735 **
736 ** If the filename is blank or "-" then write to standard output.
737
+21 -3
--- src/branch.c
+++ src/branch.c
@@ -281,13 +281,18 @@
281281
void brlist_page(void){
282282
Stmt q;
283283
int cnt;
284284
int showClosed = P("closed")!=0;
285285
int showAll = P("all")!=0;
286
+ int colorTest = P("colortest")!=0;
286287
287288
login_check_credentials();
288289
if( !g.okRead ){ login_needed(); return; }
290
+ if( colorTest ){
291
+ showClosed = 0;
292
+ showAll = 1;
293
+ }
289294
290295
style_header(showClosed ? "Closed Branches" :
291296
showAll ? "All Branches" : "Open Branches");
292297
style_submenu_element("Timeline", "Timeline", "brtimeline");
293298
if( showClosed ){
@@ -298,10 +303,15 @@
298303
style_submenu_element("Open","Open","brlist");
299304
}else{
300305
style_submenu_element("All", "All", "brlist?all");
301306
style_submenu_element("Closed","Closed","brlist?closed");
302307
}
308
+ if( !colorTest ){
309
+ style_submenu_element("Color-Test", "Color-Test", "brlist?colortest");
310
+ }else{
311
+ style_submenu_element("All", "All", "brlist?all");
312
+ }
303313
login_anonymous_available();
304314
style_sidebox_begin("Nomenclature:", "33%");
305315
@ <ol>
306316
@ <li> An <div class="sideboxDescribed"><a href="brlist">
307317
@ open branch</a></div> is a branch that has one or
@@ -320,20 +330,28 @@
320330
prepareBranchQuery(&q, showAll, showClosed);
321331
cnt = 0;
322332
while( db_step(&q)==SQLITE_ROW ){
323333
const char *zBr = db_column_text(&q, 0);
324334
if( cnt==0 ){
325
- if( showClosed ){
335
+ if( colorTest ){
336
+ @ <h2>Default background colors for all branches:</h2>
337
+ }else if( showAll ){
338
+ @ <h2>All Branches:</h2>
339
+ }else if( showClosed ){
326340
@ <h2>Closed Branches:</h2>
327341
}else{
328342
@ <h2>Open Branches:</h2>
329343
}
330344
@ <ul>
331345
cnt++;
332346
}
333
- if( g.okHistory ){
334
- @ <li><a href="%s(g.zTop)/timeline?r=%T(zBr)">%h(zBr)</a></li>
347
+ if( colorTest ){
348
+ const char *zColor = hash_color(zBr);
349
+ @ <li><span style="background-color: %s(zColor)">
350
+ @ %h(zBr) &rarr; %s(zColor)</span></li>
351
+ }else if( g.okHistory ){
352
+ @ <li><a href="%s(g.zTop)/timeline?r=%T(zBr)")>%h(zBr)</a></li>
335353
}else{
336354
@ <li><b>%h(zBr)</b></li>
337355
}
338356
}
339357
if( cnt ){
340358
--- src/branch.c
+++ src/branch.c
@@ -281,13 +281,18 @@
281 void brlist_page(void){
282 Stmt q;
283 int cnt;
284 int showClosed = P("closed")!=0;
285 int showAll = P("all")!=0;
 
286
287 login_check_credentials();
288 if( !g.okRead ){ login_needed(); return; }
 
 
 
 
289
290 style_header(showClosed ? "Closed Branches" :
291 showAll ? "All Branches" : "Open Branches");
292 style_submenu_element("Timeline", "Timeline", "brtimeline");
293 if( showClosed ){
@@ -298,10 +303,15 @@
298 style_submenu_element("Open","Open","brlist");
299 }else{
300 style_submenu_element("All", "All", "brlist?all");
301 style_submenu_element("Closed","Closed","brlist?closed");
302 }
 
 
 
 
 
303 login_anonymous_available();
304 style_sidebox_begin("Nomenclature:", "33%");
305 @ <ol>
306 @ <li> An <div class="sideboxDescribed"><a href="brlist">
307 @ open branch</a></div> is a branch that has one or
@@ -320,20 +330,28 @@
320 prepareBranchQuery(&q, showAll, showClosed);
321 cnt = 0;
322 while( db_step(&q)==SQLITE_ROW ){
323 const char *zBr = db_column_text(&q, 0);
324 if( cnt==0 ){
325 if( showClosed ){
 
 
 
 
326 @ <h2>Closed Branches:</h2>
327 }else{
328 @ <h2>Open Branches:</h2>
329 }
330 @ <ul>
331 cnt++;
332 }
333 if( g.okHistory ){
334 @ <li><a href="%s(g.zTop)/timeline?r=%T(zBr)">%h(zBr)</a></li>
 
 
 
 
335 }else{
336 @ <li><b>%h(zBr)</b></li>
337 }
338 }
339 if( cnt ){
340
--- src/branch.c
+++ src/branch.c
@@ -281,13 +281,18 @@
281 void brlist_page(void){
282 Stmt q;
283 int cnt;
284 int showClosed = P("closed")!=0;
285 int showAll = P("all")!=0;
286 int colorTest = P("colortest")!=0;
287
288 login_check_credentials();
289 if( !g.okRead ){ login_needed(); return; }
290 if( colorTest ){
291 showClosed = 0;
292 showAll = 1;
293 }
294
295 style_header(showClosed ? "Closed Branches" :
296 showAll ? "All Branches" : "Open Branches");
297 style_submenu_element("Timeline", "Timeline", "brtimeline");
298 if( showClosed ){
@@ -298,10 +303,15 @@
303 style_submenu_element("Open","Open","brlist");
304 }else{
305 style_submenu_element("All", "All", "brlist?all");
306 style_submenu_element("Closed","Closed","brlist?closed");
307 }
308 if( !colorTest ){
309 style_submenu_element("Color-Test", "Color-Test", "brlist?colortest");
310 }else{
311 style_submenu_element("All", "All", "brlist?all");
312 }
313 login_anonymous_available();
314 style_sidebox_begin("Nomenclature:", "33%");
315 @ <ol>
316 @ <li> An <div class="sideboxDescribed"><a href="brlist">
317 @ open branch</a></div> is a branch that has one or
@@ -320,20 +330,28 @@
330 prepareBranchQuery(&q, showAll, showClosed);
331 cnt = 0;
332 while( db_step(&q)==SQLITE_ROW ){
333 const char *zBr = db_column_text(&q, 0);
334 if( cnt==0 ){
335 if( colorTest ){
336 @ <h2>Default background colors for all branches:</h2>
337 }else if( showAll ){
338 @ <h2>All Branches:</h2>
339 }else if( showClosed ){
340 @ <h2>Closed Branches:</h2>
341 }else{
342 @ <h2>Open Branches:</h2>
343 }
344 @ <ul>
345 cnt++;
346 }
347 if( colorTest ){
348 const char *zColor = hash_color(zBr);
349 @ <li><span style="background-color: %s(zColor)">
350 @ %h(zBr) &rarr; %s(zColor)</span></li>
351 }else if( g.okHistory ){
352 @ <li><a href="%s(g.zTop)/timeline?r=%T(zBr)")>%h(zBr)</a></li>
353 }else{
354 @ <li><b>%h(zBr)</b></li>
355 }
356 }
357 if( cnt ){
358
+18 -5
--- src/checkin.c
+++ src/checkin.c
@@ -64,11 +64,11 @@
6464
}
6565
}
6666
blob_append(report, zPrefix, nPrefix);
6767
if( isDeleted ){
6868
blob_appendf(report, "DELETED %s\n", zDisplayName);
69
- }else if( !file_isfile(zFullName) ){
69
+ }else if( !file_isfile_or_link(zFullName) ){
7070
if( file_access(zFullName, 0)==0 ){
7171
blob_appendf(report, "NOT_A_FILE %s\n", zDisplayName);
7272
if( missingIsFatal ){
7373
fossil_warning("not a file: %s", zDisplayName);
7474
nErr++;
@@ -227,11 +227,11 @@
227227
fossil_print("%s\n", zPathname);
228228
}else if( isNew ){
229229
fossil_print("ADDED %s\n", zPathname);
230230
}else if( isDeleted ){
231231
fossil_print("DELETED %s\n", zPathname);
232
- }else if( !file_isfile(zFullName) ){
232
+ }else if( !file_isfile_or_link(zFullName) ){
233233
if( file_access(zFullName, 0)==0 ){
234234
fossil_print("NOT_A_FILE %s\n", zPathname);
235235
}else{
236236
fossil_print("MISSING %s\n", zPathname);
237237
}
@@ -635,11 +635,11 @@
635635
blob_appendf(pOut, "C %F\n", blob_str(pComment));
636636
zDate = date_in_standard_format(zDateOvrd ? zDateOvrd : "now");
637637
blob_appendf(pOut, "D %s\n", zDate);
638638
zDate[10] = ' ';
639639
db_prepare(&q,
640
- "SELECT pathname, uuid, origname, blob.rid, isexe,"
640
+ "SELECT pathname, uuid, origname, blob.rid, isexe, islink,"
641641
" file_is_selected(vfile.id)"
642642
" FROM vfile JOIN blob ON vfile.mrid=blob.rid"
643643
" WHERE (NOT deleted OR NOT file_is_selected(vfile.id))"
644644
" AND vfile.vid=%d"
645645
" ORDER BY 1", vid);
@@ -650,11 +650,12 @@
650650
const char *zName = db_column_text(&q, 0);
651651
const char *zUuid = db_column_text(&q, 1);
652652
const char *zOrig = db_column_text(&q, 2);
653653
int frid = db_column_int(&q, 3);
654654
int isexe = db_column_int(&q, 4);
655
- int isSelected = db_column_int(&q, 5);
655
+ int isLink = db_column_int(&q, 5);
656
+ int isSelected = db_column_int(&q, 6);
656657
const char *zPerm;
657658
int cmp;
658659
#if !defined(_WIN32)
659660
/* For unix, extract the "executable" permission bit directly from
660661
** the filesystem. On windows, the "executable" bit is retained
@@ -661,13 +662,20 @@
661662
** unchanged from the original.
662663
*/
663664
blob_resize(&filename, nBasename);
664665
blob_append(&filename, zName, -1);
665666
isexe = file_isexe(blob_str(&filename));
667
+
668
+ /* For unix, check if the file on the filesystem is symlink.
669
+ ** On windows, the bit is retained unchanged from original.
670
+ */
671
+ isLink = file_islink(blob_str(&filename));
666672
#endif
667673
if( isexe ){
668674
zPerm = " x";
675
+ }else if( isLink ){
676
+ zPerm = " l"; /* note: symlinks don't have executable bit on unix */
669677
}else{
670678
zPerm = "";
671679
}
672680
if( !g.markPrivate ) content_make_public(frid);
673681
while( pFile && fossil_strcmp(pFile->zName,zName)<0 ){
@@ -1063,11 +1071,16 @@
10631071
zFullname = db_column_text(&q, 1);
10641072
rid = db_column_int(&q, 2);
10651073
crnlOk = db_column_int(&q, 3);
10661074
10671075
blob_zero(&content);
1068
- blob_read_from_file(&content, zFullname);
1076
+ if( file_islink(zFullname) ){
1077
+ /* Instead of file content, put link destination path */
1078
+ blob_read_link(&content, zFullname);
1079
+ }else{
1080
+ blob_read_from_file(&content, zFullname);
1081
+ }
10691082
if( !crnlOk ) cr_warning(&content, zFullname);
10701083
nrid = content_put(&content);
10711084
blob_reset(&content);
10721085
if( rid>0 ){
10731086
content_deltify(rid, nrid, 0);
10741087
--- src/checkin.c
+++ src/checkin.c
@@ -64,11 +64,11 @@
64 }
65 }
66 blob_append(report, zPrefix, nPrefix);
67 if( isDeleted ){
68 blob_appendf(report, "DELETED %s\n", zDisplayName);
69 }else if( !file_isfile(zFullName) ){
70 if( file_access(zFullName, 0)==0 ){
71 blob_appendf(report, "NOT_A_FILE %s\n", zDisplayName);
72 if( missingIsFatal ){
73 fossil_warning("not a file: %s", zDisplayName);
74 nErr++;
@@ -227,11 +227,11 @@
227 fossil_print("%s\n", zPathname);
228 }else if( isNew ){
229 fossil_print("ADDED %s\n", zPathname);
230 }else if( isDeleted ){
231 fossil_print("DELETED %s\n", zPathname);
232 }else if( !file_isfile(zFullName) ){
233 if( file_access(zFullName, 0)==0 ){
234 fossil_print("NOT_A_FILE %s\n", zPathname);
235 }else{
236 fossil_print("MISSING %s\n", zPathname);
237 }
@@ -635,11 +635,11 @@
635 blob_appendf(pOut, "C %F\n", blob_str(pComment));
636 zDate = date_in_standard_format(zDateOvrd ? zDateOvrd : "now");
637 blob_appendf(pOut, "D %s\n", zDate);
638 zDate[10] = ' ';
639 db_prepare(&q,
640 "SELECT pathname, uuid, origname, blob.rid, isexe,"
641 " file_is_selected(vfile.id)"
642 " FROM vfile JOIN blob ON vfile.mrid=blob.rid"
643 " WHERE (NOT deleted OR NOT file_is_selected(vfile.id))"
644 " AND vfile.vid=%d"
645 " ORDER BY 1", vid);
@@ -650,11 +650,12 @@
650 const char *zName = db_column_text(&q, 0);
651 const char *zUuid = db_column_text(&q, 1);
652 const char *zOrig = db_column_text(&q, 2);
653 int frid = db_column_int(&q, 3);
654 int isexe = db_column_int(&q, 4);
655 int isSelected = db_column_int(&q, 5);
 
656 const char *zPerm;
657 int cmp;
658 #if !defined(_WIN32)
659 /* For unix, extract the "executable" permission bit directly from
660 ** the filesystem. On windows, the "executable" bit is retained
@@ -661,13 +662,20 @@
661 ** unchanged from the original.
662 */
663 blob_resize(&filename, nBasename);
664 blob_append(&filename, zName, -1);
665 isexe = file_isexe(blob_str(&filename));
 
 
 
 
 
666 #endif
667 if( isexe ){
668 zPerm = " x";
 
 
669 }else{
670 zPerm = "";
671 }
672 if( !g.markPrivate ) content_make_public(frid);
673 while( pFile && fossil_strcmp(pFile->zName,zName)<0 ){
@@ -1063,11 +1071,16 @@
1063 zFullname = db_column_text(&q, 1);
1064 rid = db_column_int(&q, 2);
1065 crnlOk = db_column_int(&q, 3);
1066
1067 blob_zero(&content);
1068 blob_read_from_file(&content, zFullname);
 
 
 
 
 
1069 if( !crnlOk ) cr_warning(&content, zFullname);
1070 nrid = content_put(&content);
1071 blob_reset(&content);
1072 if( rid>0 ){
1073 content_deltify(rid, nrid, 0);
1074
--- src/checkin.c
+++ src/checkin.c
@@ -64,11 +64,11 @@
64 }
65 }
66 blob_append(report, zPrefix, nPrefix);
67 if( isDeleted ){
68 blob_appendf(report, "DELETED %s\n", zDisplayName);
69 }else if( !file_isfile_or_link(zFullName) ){
70 if( file_access(zFullName, 0)==0 ){
71 blob_appendf(report, "NOT_A_FILE %s\n", zDisplayName);
72 if( missingIsFatal ){
73 fossil_warning("not a file: %s", zDisplayName);
74 nErr++;
@@ -227,11 +227,11 @@
227 fossil_print("%s\n", zPathname);
228 }else if( isNew ){
229 fossil_print("ADDED %s\n", zPathname);
230 }else if( isDeleted ){
231 fossil_print("DELETED %s\n", zPathname);
232 }else if( !file_isfile_or_link(zFullName) ){
233 if( file_access(zFullName, 0)==0 ){
234 fossil_print("NOT_A_FILE %s\n", zPathname);
235 }else{
236 fossil_print("MISSING %s\n", zPathname);
237 }
@@ -635,11 +635,11 @@
635 blob_appendf(pOut, "C %F\n", blob_str(pComment));
636 zDate = date_in_standard_format(zDateOvrd ? zDateOvrd : "now");
637 blob_appendf(pOut, "D %s\n", zDate);
638 zDate[10] = ' ';
639 db_prepare(&q,
640 "SELECT pathname, uuid, origname, blob.rid, isexe, islink,"
641 " file_is_selected(vfile.id)"
642 " FROM vfile JOIN blob ON vfile.mrid=blob.rid"
643 " WHERE (NOT deleted OR NOT file_is_selected(vfile.id))"
644 " AND vfile.vid=%d"
645 " ORDER BY 1", vid);
@@ -650,11 +650,12 @@
650 const char *zName = db_column_text(&q, 0);
651 const char *zUuid = db_column_text(&q, 1);
652 const char *zOrig = db_column_text(&q, 2);
653 int frid = db_column_int(&q, 3);
654 int isexe = db_column_int(&q, 4);
655 int isLink = db_column_int(&q, 5);
656 int isSelected = db_column_int(&q, 6);
657 const char *zPerm;
658 int cmp;
659 #if !defined(_WIN32)
660 /* For unix, extract the "executable" permission bit directly from
661 ** the filesystem. On windows, the "executable" bit is retained
@@ -661,13 +662,20 @@
662 ** unchanged from the original.
663 */
664 blob_resize(&filename, nBasename);
665 blob_append(&filename, zName, -1);
666 isexe = file_isexe(blob_str(&filename));
667
668 /* For unix, check if the file on the filesystem is symlink.
669 ** On windows, the bit is retained unchanged from original.
670 */
671 isLink = file_islink(blob_str(&filename));
672 #endif
673 if( isexe ){
674 zPerm = " x";
675 }else if( isLink ){
676 zPerm = " l"; /* note: symlinks don't have executable bit on unix */
677 }else{
678 zPerm = "";
679 }
680 if( !g.markPrivate ) content_make_public(frid);
681 while( pFile && fossil_strcmp(pFile->zName,zName)<0 ){
@@ -1063,11 +1071,16 @@
1071 zFullname = db_column_text(&q, 1);
1072 rid = db_column_int(&q, 2);
1073 crnlOk = db_column_int(&q, 3);
1074
1075 blob_zero(&content);
1076 if( file_islink(zFullname) ){
1077 /* Instead of file content, put link destination path */
1078 blob_read_link(&content, zFullname);
1079 }else{
1080 blob_read_from_file(&content, zFullname);
1081 }
1082 if( !crnlOk ) cr_warning(&content, zFullname);
1083 nrid = content_put(&content);
1084 blob_reset(&content);
1085 if( rid>0 ){
1086 content_deltify(rid, nrid, 0);
1087
+2 -1
--- src/checkout.c
+++ src/checkout.c
@@ -92,11 +92,12 @@
9292
db_reset(&s);
9393
}
9494
9595
/*
9696
** Set or clear the execute permission bit (as appropriate) for all
97
-** files in the current check-out.
97
+** files in the current check-out, and replace files that have
98
+** symlink bit with actual symlinks.
9899
*/
99100
void checkout_set_all_exe(int vid){
100101
Blob filename;
101102
int baseLen;
102103
Manifest *pManifest;
103104
--- src/checkout.c
+++ src/checkout.c
@@ -92,11 +92,12 @@
92 db_reset(&s);
93 }
94
95 /*
96 ** Set or clear the execute permission bit (as appropriate) for all
97 ** files in the current check-out.
 
98 */
99 void checkout_set_all_exe(int vid){
100 Blob filename;
101 int baseLen;
102 Manifest *pManifest;
103
--- src/checkout.c
+++ src/checkout.c
@@ -92,11 +92,12 @@
92 db_reset(&s);
93 }
94
95 /*
96 ** Set or clear the execute permission bit (as appropriate) for all
97 ** files in the current check-out, and replace files that have
98 ** symlink bit with actual symlinks.
99 */
100 void checkout_set_all_exe(int vid){
101 Blob filename;
102 int baseLen;
103 Manifest *pManifest;
104
+1 -1
--- src/configure.c
+++ src/configure.c
@@ -726,11 +726,11 @@
726726
727727
728728
/*
729729
** COMMAND: configuration
730730
**
731
-** Usage: %fossil configure METHOD ... ?-R|--repository REPOSITORY?
731
+** Usage: %fossil configuration METHOD ... ?-R|--repository REPOSITORY?
732732
**
733733
** Where METHOD is one of: export import merge pull push reset. All methods
734734
** accept the -R or --repository option to specific a repository.
735735
**
736736
** %fossil configuration export AREA FILENAME
737737
--- src/configure.c
+++ src/configure.c
@@ -726,11 +726,11 @@
726
727
728 /*
729 ** COMMAND: configuration
730 **
731 ** Usage: %fossil configure METHOD ... ?-R|--repository REPOSITORY?
732 **
733 ** Where METHOD is one of: export import merge pull push reset. All methods
734 ** accept the -R or --repository option to specific a repository.
735 **
736 ** %fossil configuration export AREA FILENAME
737
--- src/configure.c
+++ src/configure.c
@@ -726,11 +726,11 @@
726
727
728 /*
729 ** COMMAND: configuration
730 **
731 ** Usage: %fossil configuration METHOD ... ?-R|--repository REPOSITORY?
732 **
733 ** Where METHOD is one of: export import merge pull push reset. All methods
734 ** accept the -R or --repository option to specific a repository.
735 **
736 ** %fossil configuration export AREA FILENAME
737
+51 -34
--- src/db.c
+++ src/db.c
@@ -736,18 +736,35 @@
736736
}
737737
g.configOpen = 1;
738738
free(zDbName);
739739
}
740740
741
+
742
+/*
743
+ * * Returns TRUE if zTable exists in the local database.
744
+ */
745
+static int db_local_table_exists(const char *zTable){
746
+ return db_exists("SELECT 1 FROM %s.sqlite_master"
747
+ " WHERE name=='%s'",
748
+ db_name("localdb"), zTable);
749
+}
750
+
751
+/*
752
+** Returns TRUE if zColumn exists in zTable in the local database.
753
+*/
754
+static int db_local_column_exists(const char *zTable, const char *zColumn){
755
+ return db_exists("SELECT 1 FROM %s.sqlite_master"
756
+ " WHERE name=='%s' AND sql GLOB '* %s *'",
757
+ db_name("localdb"), zTable, zColumn);
758
+}
759
+
741760
/*
742761
** If zDbName is a valid local database file, open it and return
743762
** true. If it is not a valid local database file, return 0.
744763
*/
745764
static int isValidLocalDb(const char *zDbName){
746765
i64 lsize;
747
- int rc;
748
- sqlite3_stmt *pStmt;
749766
750767
if( file_access(zDbName, F_OK) ) return 0;
751768
lsize = file_size(zDbName);
752769
if( lsize%1024!=0 || lsize<4096 ) return 0;
753770
db_open_or_attach(zDbName, "localdb");
@@ -757,40 +774,31 @@
757774
758775
/* If the "isexe" column is missing from the vfile table, then
759776
** add it now. This code added on 2010-03-06. After all users have
760777
** upgraded, this code can be safely deleted.
761778
*/
762
- rc = sqlite3_prepare(g.db, "SELECT isexe FROM vfile", -1, &pStmt, 0);
763
- nPrepare++;
764
- sqlite3_finalize(pStmt);
765
- if( rc==SQLITE_ERROR ){
766
- sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN isexe BOOLEAN", 0, 0, 0);
767
- }
768
-
769
-#if 0
770
- /* If the "mtime" column is missing from the vfile table, then
771
- ** add it now. This code added on 2008-12-06. After all users have
772
- ** upgraded, this code can be safely deleted.
773
- */
774
- rc = sqlite3_prepare(g.db, "SELECT mtime FROM vfile", -1, &pStmt, 0);
775
- sqlite3_finalize(pStmt);
776
- if( rc==SQLITE_ERROR ){
777
- sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN mtime INTEGER", 0, 0, 0);
778
- }
779
-#endif
780
-
781
-#if 0
782
- /* If the "origname" column is missing from the vfile table, then
783
- ** add it now. This code added on 2008-11-09. After all users have
784
- ** upgraded, this code can be safely deleted.
785
- */
786
- rc = sqlite3_prepare(g.db, "SELECT origname FROM vfile", -1, &pStmt, 0);
787
- sqlite3_finalize(pStmt);
788
- if( rc==SQLITE_ERROR ){
789
- sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN origname TEXT", 0, 0, 0);
790
- }
791
-#endif
779
+ if( !db_local_column_exists("vfile", "isexe") )
780
+ db_multi_exec("ALTER TABLE vfile ADD COLUMN isexe BOOLEAN DEFAULT 0");
781
+
782
+ /* If "islink"/"isLink" columns are missing from tables, then
783
+ ** add them now. This code added on 2011-01-17 and 2011-08-27.
784
+ ** After all users have upgraded, this code can be safely deleted.
785
+ */
786
+ if( !db_local_column_exists("vfile", "islink") )
787
+ db_multi_exec("ALTER TABLE vfile ADD COLUMN islink BOOLEAN DEFAULT 0");
788
+
789
+ if( !db_local_column_exists("stashfile", "isLink") &&
790
+ db_local_table_exists("stashfile") )
791
+ db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOLEAN DEFAULT 0");
792
+
793
+ if( !db_local_column_exists("undo", "isLink") &&
794
+ db_local_table_exists("undo") )
795
+ db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0");
796
+
797
+ if( !db_local_column_exists("undo_vfile", "islink") &&
798
+ db_local_table_exists("undo_vfile") )
799
+ db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOLEAN DEFAULT 0");
792800
793801
return 1;
794802
}
795803
796804
/*
@@ -866,10 +874,12 @@
866874
}
867875
}
868876
db_open_or_attach(zDbName, "repository");
869877
g.repositoryOpen = 1;
870878
g.zRepositoryName = mprintf("%s", zDbName);
879
+ /* Cache "allow-symlinks" option, because we'll need it on every stat call */
880
+ g.allowSymlinks = db_get_boolean("allow-symlinks", 0);
871881
}
872882
873883
/*
874884
** Flags for the db_find_and_open_repository() function.
875885
*/
@@ -1426,11 +1436,11 @@
14261436
}
14271437
cacheEntry = cacheEntry->next;
14281438
}
14291439
/* Attempt to read value from file in checkout if there wasn't a cache hit
14301440
** and a checkout is open. */
1431
- if( cacheEntry==0 && db_open_local() ){
1441
+ if( cacheEntry==0 ){
14321442
Blob versionedPathname;
14331443
char *zVersionedPathname;
14341444
blob_zero(&versionedPathname);
14351445
blob_appendf(&versionedPathname, "%s/.fossil-settings/%s",
14361446
g.zLocalRoot, zName);
@@ -1500,11 +1510,11 @@
15001510
if( z==0 && g.configOpen ){
15011511
db_swap_connections();
15021512
z = db_text(0, "SELECT value FROM global_config WHERE name=%Q", zName);
15031513
db_swap_connections();
15041514
}
1505
- if( ctrlSetting!=0 && ctrlSetting->versionable ){
1515
+ if( ctrlSetting!=0 && ctrlSetting->versionable && g.localOpen ){
15061516
/* This is a versionable setting, try and get the info from a checked out file */
15071517
z = db_get_do_versionable(zName, z);
15081518
}
15091519
if( z==0 ){
15101520
z = zDefault;
@@ -1747,10 +1757,11 @@
17471757
char const *def; /* Default value */
17481758
};
17491759
#endif /* INTERFACE */
17501760
struct stControlSettings const ctrlSettings[] = {
17511761
{ "access-log", 0, 0, 0, "off" },
1762
+ { "allow-symlinks",0, 0, 0, "off" },
17521763
{ "auto-captcha", "autocaptcha", 0, 0, "on" },
17531764
{ "auto-shun", 0, 0, 0, "on" },
17541765
{ "autosync", 0, 0, 0, "on" },
17551766
{ "binary-glob", 0, 32, 1, "" },
17561767
{ "clearsign", 0, 0, 0, "off" },
@@ -1799,10 +1810,16 @@
17991810
** file named .fossil-settings/PROPERTY in the checked out files, if that
18001811
** file exists.
18011812
**
18021813
** The "unset" command clears a property setting.
18031814
**
1815
+**
1816
+** allow-symlinks If enabled, don't follow symlinks, and instead treat
1817
+** them as symlinks on Unix. Has no effect on Windows
1818
+** (existing links in repository created on Unix become
1819
+** plain-text files with link destination path inside).
1820
+** Default: off
18041821
**
18051822
** auto-captcha If enabled, the Login page provides a button to
18061823
** fill in the captcha password. Default: on
18071824
**
18081825
** auto-shun If enabled, automatically pull the shunning list
18091826
--- src/db.c
+++ src/db.c
@@ -736,18 +736,35 @@
736 }
737 g.configOpen = 1;
738 free(zDbName);
739 }
740
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
741 /*
742 ** If zDbName is a valid local database file, open it and return
743 ** true. If it is not a valid local database file, return 0.
744 */
745 static int isValidLocalDb(const char *zDbName){
746 i64 lsize;
747 int rc;
748 sqlite3_stmt *pStmt;
749
750 if( file_access(zDbName, F_OK) ) return 0;
751 lsize = file_size(zDbName);
752 if( lsize%1024!=0 || lsize<4096 ) return 0;
753 db_open_or_attach(zDbName, "localdb");
@@ -757,40 +774,31 @@
757
758 /* If the "isexe" column is missing from the vfile table, then
759 ** add it now. This code added on 2010-03-06. After all users have
760 ** upgraded, this code can be safely deleted.
761 */
762 rc = sqlite3_prepare(g.db, "SELECT isexe FROM vfile", -1, &pStmt, 0);
763 nPrepare++;
764 sqlite3_finalize(pStmt);
765 if( rc==SQLITE_ERROR ){
766 sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN isexe BOOLEAN", 0, 0, 0);
767 }
768
769 #if 0
770 /* If the "mtime" column is missing from the vfile table, then
771 ** add it now. This code added on 2008-12-06. After all users have
772 ** upgraded, this code can be safely deleted.
773 */
774 rc = sqlite3_prepare(g.db, "SELECT mtime FROM vfile", -1, &pStmt, 0);
775 sqlite3_finalize(pStmt);
776 if( rc==SQLITE_ERROR ){
777 sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN mtime INTEGER", 0, 0, 0);
778 }
779 #endif
780
781 #if 0
782 /* If the "origname" column is missing from the vfile table, then
783 ** add it now. This code added on 2008-11-09. After all users have
784 ** upgraded, this code can be safely deleted.
785 */
786 rc = sqlite3_prepare(g.db, "SELECT origname FROM vfile", -1, &pStmt, 0);
787 sqlite3_finalize(pStmt);
788 if( rc==SQLITE_ERROR ){
789 sqlite3_exec(g.db, "ALTER TABLE vfile ADD COLUMN origname TEXT", 0, 0, 0);
790 }
791 #endif
792
793 return 1;
794 }
795
796 /*
@@ -866,10 +874,12 @@
866 }
867 }
868 db_open_or_attach(zDbName, "repository");
869 g.repositoryOpen = 1;
870 g.zRepositoryName = mprintf("%s", zDbName);
 
 
871 }
872
873 /*
874 ** Flags for the db_find_and_open_repository() function.
875 */
@@ -1426,11 +1436,11 @@
1426 }
1427 cacheEntry = cacheEntry->next;
1428 }
1429 /* Attempt to read value from file in checkout if there wasn't a cache hit
1430 ** and a checkout is open. */
1431 if( cacheEntry==0 && db_open_local() ){
1432 Blob versionedPathname;
1433 char *zVersionedPathname;
1434 blob_zero(&versionedPathname);
1435 blob_appendf(&versionedPathname, "%s/.fossil-settings/%s",
1436 g.zLocalRoot, zName);
@@ -1500,11 +1510,11 @@
1500 if( z==0 && g.configOpen ){
1501 db_swap_connections();
1502 z = db_text(0, "SELECT value FROM global_config WHERE name=%Q", zName);
1503 db_swap_connections();
1504 }
1505 if( ctrlSetting!=0 && ctrlSetting->versionable ){
1506 /* This is a versionable setting, try and get the info from a checked out file */
1507 z = db_get_do_versionable(zName, z);
1508 }
1509 if( z==0 ){
1510 z = zDefault;
@@ -1747,10 +1757,11 @@
1747 char const *def; /* Default value */
1748 };
1749 #endif /* INTERFACE */
1750 struct stControlSettings const ctrlSettings[] = {
1751 { "access-log", 0, 0, 0, "off" },
 
1752 { "auto-captcha", "autocaptcha", 0, 0, "on" },
1753 { "auto-shun", 0, 0, 0, "on" },
1754 { "autosync", 0, 0, 0, "on" },
1755 { "binary-glob", 0, 32, 1, "" },
1756 { "clearsign", 0, 0, 0, "off" },
@@ -1799,10 +1810,16 @@
1799 ** file named .fossil-settings/PROPERTY in the checked out files, if that
1800 ** file exists.
1801 **
1802 ** The "unset" command clears a property setting.
1803 **
 
 
 
 
 
 
1804 **
1805 ** auto-captcha If enabled, the Login page provides a button to
1806 ** fill in the captcha password. Default: on
1807 **
1808 ** auto-shun If enabled, automatically pull the shunning list
1809
--- src/db.c
+++ src/db.c
@@ -736,18 +736,35 @@
736 }
737 g.configOpen = 1;
738 free(zDbName);
739 }
740
741
742 /*
743 * * Returns TRUE if zTable exists in the local database.
744 */
745 static int db_local_table_exists(const char *zTable){
746 return db_exists("SELECT 1 FROM %s.sqlite_master"
747 " WHERE name=='%s'",
748 db_name("localdb"), zTable);
749 }
750
751 /*
752 ** Returns TRUE if zColumn exists in zTable in the local database.
753 */
754 static int db_local_column_exists(const char *zTable, const char *zColumn){
755 return db_exists("SELECT 1 FROM %s.sqlite_master"
756 " WHERE name=='%s' AND sql GLOB '* %s *'",
757 db_name("localdb"), zTable, zColumn);
758 }
759
760 /*
761 ** If zDbName is a valid local database file, open it and return
762 ** true. If it is not a valid local database file, return 0.
763 */
764 static int isValidLocalDb(const char *zDbName){
765 i64 lsize;
 
 
766
767 if( file_access(zDbName, F_OK) ) return 0;
768 lsize = file_size(zDbName);
769 if( lsize%1024!=0 || lsize<4096 ) return 0;
770 db_open_or_attach(zDbName, "localdb");
@@ -757,40 +774,31 @@
774
775 /* If the "isexe" column is missing from the vfile table, then
776 ** add it now. This code added on 2010-03-06. After all users have
777 ** upgraded, this code can be safely deleted.
778 */
779 if( !db_local_column_exists("vfile", "isexe") )
780 db_multi_exec("ALTER TABLE vfile ADD COLUMN isexe BOOLEAN DEFAULT 0");
781
782 /* If "islink"/"isLink" columns are missing from tables, then
783 ** add them now. This code added on 2011-01-17 and 2011-08-27.
784 ** After all users have upgraded, this code can be safely deleted.
785 */
786 if( !db_local_column_exists("vfile", "islink") )
787 db_multi_exec("ALTER TABLE vfile ADD COLUMN islink BOOLEAN DEFAULT 0");
788
789 if( !db_local_column_exists("stashfile", "isLink") &&
790 db_local_table_exists("stashfile") )
791 db_multi_exec("ALTER TABLE stashfile ADD COLUMN isLink BOOLEAN DEFAULT 0");
792
793 if( !db_local_column_exists("undo", "isLink") &&
794 db_local_table_exists("undo") )
795 db_multi_exec("ALTER TABLE undo ADD COLUMN isLink BOOLEAN DEFAULT 0");
796
797 if( !db_local_column_exists("undo_vfile", "islink") &&
798 db_local_table_exists("undo_vfile") )
799 db_multi_exec("ALTER TABLE undo_vfile ADD COLUMN islink BOOLEAN DEFAULT 0");
 
 
 
 
 
 
 
 
 
800
801 return 1;
802 }
803
804 /*
@@ -866,10 +874,12 @@
874 }
875 }
876 db_open_or_attach(zDbName, "repository");
877 g.repositoryOpen = 1;
878 g.zRepositoryName = mprintf("%s", zDbName);
879 /* Cache "allow-symlinks" option, because we'll need it on every stat call */
880 g.allowSymlinks = db_get_boolean("allow-symlinks", 0);
881 }
882
883 /*
884 ** Flags for the db_find_and_open_repository() function.
885 */
@@ -1426,11 +1436,11 @@
1436 }
1437 cacheEntry = cacheEntry->next;
1438 }
1439 /* Attempt to read value from file in checkout if there wasn't a cache hit
1440 ** and a checkout is open. */
1441 if( cacheEntry==0 ){
1442 Blob versionedPathname;
1443 char *zVersionedPathname;
1444 blob_zero(&versionedPathname);
1445 blob_appendf(&versionedPathname, "%s/.fossil-settings/%s",
1446 g.zLocalRoot, zName);
@@ -1500,11 +1510,11 @@
1510 if( z==0 && g.configOpen ){
1511 db_swap_connections();
1512 z = db_text(0, "SELECT value FROM global_config WHERE name=%Q", zName);
1513 db_swap_connections();
1514 }
1515 if( ctrlSetting!=0 && ctrlSetting->versionable && g.localOpen ){
1516 /* This is a versionable setting, try and get the info from a checked out file */
1517 z = db_get_do_versionable(zName, z);
1518 }
1519 if( z==0 ){
1520 z = zDefault;
@@ -1747,10 +1757,11 @@
1757 char const *def; /* Default value */
1758 };
1759 #endif /* INTERFACE */
1760 struct stControlSettings const ctrlSettings[] = {
1761 { "access-log", 0, 0, 0, "off" },
1762 { "allow-symlinks",0, 0, 0, "off" },
1763 { "auto-captcha", "autocaptcha", 0, 0, "on" },
1764 { "auto-shun", 0, 0, 0, "on" },
1765 { "autosync", 0, 0, 0, "on" },
1766 { "binary-glob", 0, 32, 1, "" },
1767 { "clearsign", 0, 0, 0, "off" },
@@ -1799,10 +1810,16 @@
1810 ** file named .fossil-settings/PROPERTY in the checked out files, if that
1811 ** file exists.
1812 **
1813 ** The "unset" command clears a property setting.
1814 **
1815 **
1816 ** allow-symlinks If enabled, don't follow symlinks, and instead treat
1817 ** them as symlinks on Unix. Has no effect on Windows
1818 ** (existing links in repository created on Unix become
1819 ** plain-text files with link destination path inside).
1820 ** Default: off
1821 **
1822 ** auto-captcha If enabled, the Login page provides a button to
1823 ** fill in the captcha password. Default: on
1824 **
1825 ** auto-shun If enabled, automatically pull the shunning list
1826
+32 -10
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -74,11 +74,15 @@
7474
/* Read content of zFile2 into memory */
7575
blob_zero(&file2);
7676
if( file_size(zFile2)<0 ){
7777
zName2 = "/dev/null";
7878
}else{
79
- blob_read_from_file(&file2, zFile2);
79
+ if( file_islink(zFile2) ){
80
+ blob_read_link(&file2, zFile2);
81
+ }else{
82
+ blob_read_from_file(&file2, zFile2);
83
+ }
8084
zName2 = zName;
8185
}
8286
8387
/* Compute and output the differences */
8488
blob_zero(&out);
@@ -186,13 +190,18 @@
186190
int ignoreEolWs, /* Ignore whitespace changes at end of lines */
187191
const char *zFileTreeName
188192
){
189193
Blob fname;
190194
Blob content;
195
+ int isLink;
191196
file_tree_name(zFileTreeName, &fname, 1);
192
- historical_version_of_file(zFrom, blob_str(&fname), &content, 0, 0);
193
- diff_file(&content, zFileTreeName, zFileTreeName, zDiffCmd, ignoreEolWs);
197
+ historical_version_of_file(zFrom, blob_str(&fname), &content, &isLink, 0, 0);
198
+ if( !isLink != !file_islink(zFrom) ){
199
+ diff_printf("cannot compute difference between symlink and regular file\n");
200
+ }else{
201
+ diff_file(&content, zFileTreeName, zFileTreeName, zDiffCmd, ignoreEolWs);
202
+ }
194203
blob_reset(&content);
195204
blob_reset(&fname);
196205
}
197206
198207
/*
@@ -222,32 +231,32 @@
222231
if( !is_a_version(rid) ){
223232
fossil_fatal("no such check-in: %s", zFrom);
224233
}
225234
load_vfile_from_rid(rid);
226235
blob_appendf(&sql,
227
- "SELECT v2.pathname, v2.deleted, v2.chnged, v2.rid==0, v1.rid"
236
+ "SELECT v2.pathname, v2.deleted, v2.chnged, v2.rid==0, v1.rid, v1.islink"
228237
" FROM vfile v1, vfile v2 "
229238
" WHERE v1.pathname=v2.pathname AND v1.vid=%d AND v2.vid=%d"
230239
" AND (v2.deleted OR v2.chnged OR v1.mrid!=v2.rid)"
231240
"UNION "
232
- "SELECT pathname, 1, 0, 0, 0"
241
+ "SELECT pathname, 1, 0, 0, 0, islink"
233242
" FROM vfile v1"
234243
" WHERE v1.vid=%d"
235244
" AND NOT EXISTS(SELECT 1 FROM vfile v2"
236245
" WHERE v2.vid=%d AND v2.pathname=v1.pathname)"
237246
"UNION "
238
- "SELECT pathname, 0, 0, 1, 0"
247
+ "SELECT pathname, 0, 0, 1, 0, islink"
239248
" FROM vfile v2"
240249
" WHERE v2.vid=%d"
241250
" AND NOT EXISTS(SELECT 1 FROM vfile v1"
242251
" WHERE v1.vid=%d AND v1.pathname=v2.pathname)"
243252
" ORDER BY 1",
244253
rid, vid, rid, vid, vid, rid
245254
);
246255
}else{
247256
blob_appendf(&sql,
248
- "SELECT pathname, deleted, chnged , rid==0, rid"
257
+ "SELECT pathname, deleted, chnged , rid==0, rid, islink"
249258
" FROM vfile"
250259
" WHERE vid=%d"
251260
" AND (deleted OR chnged OR rid==0)"
252261
" ORDER BY pathname",
253262
vid
@@ -258,10 +267,11 @@
258267
const char *zPathname = db_column_text(&q,0);
259268
int isDeleted = db_column_int(&q, 1);
260269
int isChnged = db_column_int(&q,2);
261270
int isNew = db_column_int(&q,3);
262271
int srcid = db_column_int(&q, 4);
272
+ int isLink = db_column_int(&q, 5);
263273
char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
264274
char *zToFree = zFullName;
265275
int showDiff = 1;
266276
if( isDeleted ){
267277
diff_printf("DELETED %s\n", zPathname);
@@ -278,10 +288,16 @@
278288
srcid = 0;
279289
if( !asNewFile ){ showDiff = 0; }
280290
}
281291
if( showDiff ){
282292
Blob content;
293
+ if( !isLink != !file_islink(zFullName) ){
294
+ diff_print_index(zPathname);
295
+ diff_printf("--- %s\n+++ %s\n", zPathname, zPathname);
296
+ diff_printf("cannot compute difference between symlink and regular file\n");
297
+ continue;
298
+ }
283299
if( srcid>0 ){
284300
content_get(srcid, &content);
285301
}else{
286302
blob_zero(&content);
287303
}
@@ -307,15 +323,21 @@
307323
const char *zFileTreeName
308324
){
309325
char *zName;
310326
Blob fname;
311327
Blob v1, v2;
328
+ int isLink1, isLink2;
312329
file_tree_name(zFileTreeName, &fname, 1);
313330
zName = blob_str(&fname);
314
- historical_version_of_file(zFrom, zName, &v1, 0, 0);
315
- historical_version_of_file(zTo, zName, &v2, 0, 0);
316
- diff_file_mem(&v1, &v2, zName, zDiffCmd, ignoreEolWs);
331
+ historical_version_of_file(zFrom, zName, &v1, &isLink1, 0, 0);
332
+ historical_version_of_file(zTo, zName, &v2, &isLink2, 0, 0);
333
+ if( isLink1 != isLink2 ){
334
+ diff_printf("--- %s\n+++ %s\n", zName, zName);
335
+ diff_printf("cannot compute difference between symlink and regular file\n");
336
+ }else{
337
+ diff_file_mem(&v1, &v2, zName, zDiffCmd, ignoreEolWs);
338
+ }
317339
blob_reset(&v1);
318340
blob_reset(&v2);
319341
blob_reset(&fname);
320342
}
321343
322344
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -74,11 +74,15 @@
74 /* Read content of zFile2 into memory */
75 blob_zero(&file2);
76 if( file_size(zFile2)<0 ){
77 zName2 = "/dev/null";
78 }else{
79 blob_read_from_file(&file2, zFile2);
 
 
 
 
80 zName2 = zName;
81 }
82
83 /* Compute and output the differences */
84 blob_zero(&out);
@@ -186,13 +190,18 @@
186 int ignoreEolWs, /* Ignore whitespace changes at end of lines */
187 const char *zFileTreeName
188 ){
189 Blob fname;
190 Blob content;
 
191 file_tree_name(zFileTreeName, &fname, 1);
192 historical_version_of_file(zFrom, blob_str(&fname), &content, 0, 0);
193 diff_file(&content, zFileTreeName, zFileTreeName, zDiffCmd, ignoreEolWs);
 
 
 
 
194 blob_reset(&content);
195 blob_reset(&fname);
196 }
197
198 /*
@@ -222,32 +231,32 @@
222 if( !is_a_version(rid) ){
223 fossil_fatal("no such check-in: %s", zFrom);
224 }
225 load_vfile_from_rid(rid);
226 blob_appendf(&sql,
227 "SELECT v2.pathname, v2.deleted, v2.chnged, v2.rid==0, v1.rid"
228 " FROM vfile v1, vfile v2 "
229 " WHERE v1.pathname=v2.pathname AND v1.vid=%d AND v2.vid=%d"
230 " AND (v2.deleted OR v2.chnged OR v1.mrid!=v2.rid)"
231 "UNION "
232 "SELECT pathname, 1, 0, 0, 0"
233 " FROM vfile v1"
234 " WHERE v1.vid=%d"
235 " AND NOT EXISTS(SELECT 1 FROM vfile v2"
236 " WHERE v2.vid=%d AND v2.pathname=v1.pathname)"
237 "UNION "
238 "SELECT pathname, 0, 0, 1, 0"
239 " FROM vfile v2"
240 " WHERE v2.vid=%d"
241 " AND NOT EXISTS(SELECT 1 FROM vfile v1"
242 " WHERE v1.vid=%d AND v1.pathname=v2.pathname)"
243 " ORDER BY 1",
244 rid, vid, rid, vid, vid, rid
245 );
246 }else{
247 blob_appendf(&sql,
248 "SELECT pathname, deleted, chnged , rid==0, rid"
249 " FROM vfile"
250 " WHERE vid=%d"
251 " AND (deleted OR chnged OR rid==0)"
252 " ORDER BY pathname",
253 vid
@@ -258,10 +267,11 @@
258 const char *zPathname = db_column_text(&q,0);
259 int isDeleted = db_column_int(&q, 1);
260 int isChnged = db_column_int(&q,2);
261 int isNew = db_column_int(&q,3);
262 int srcid = db_column_int(&q, 4);
 
263 char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
264 char *zToFree = zFullName;
265 int showDiff = 1;
266 if( isDeleted ){
267 diff_printf("DELETED %s\n", zPathname);
@@ -278,10 +288,16 @@
278 srcid = 0;
279 if( !asNewFile ){ showDiff = 0; }
280 }
281 if( showDiff ){
282 Blob content;
 
 
 
 
 
 
283 if( srcid>0 ){
284 content_get(srcid, &content);
285 }else{
286 blob_zero(&content);
287 }
@@ -307,15 +323,21 @@
307 const char *zFileTreeName
308 ){
309 char *zName;
310 Blob fname;
311 Blob v1, v2;
 
312 file_tree_name(zFileTreeName, &fname, 1);
313 zName = blob_str(&fname);
314 historical_version_of_file(zFrom, zName, &v1, 0, 0);
315 historical_version_of_file(zTo, zName, &v2, 0, 0);
316 diff_file_mem(&v1, &v2, zName, zDiffCmd, ignoreEolWs);
 
 
 
 
 
317 blob_reset(&v1);
318 blob_reset(&v2);
319 blob_reset(&fname);
320 }
321
322
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -74,11 +74,15 @@
74 /* Read content of zFile2 into memory */
75 blob_zero(&file2);
76 if( file_size(zFile2)<0 ){
77 zName2 = "/dev/null";
78 }else{
79 if( file_islink(zFile2) ){
80 blob_read_link(&file2, zFile2);
81 }else{
82 blob_read_from_file(&file2, zFile2);
83 }
84 zName2 = zName;
85 }
86
87 /* Compute and output the differences */
88 blob_zero(&out);
@@ -186,13 +190,18 @@
190 int ignoreEolWs, /* Ignore whitespace changes at end of lines */
191 const char *zFileTreeName
192 ){
193 Blob fname;
194 Blob content;
195 int isLink;
196 file_tree_name(zFileTreeName, &fname, 1);
197 historical_version_of_file(zFrom, blob_str(&fname), &content, &isLink, 0, 0);
198 if( !isLink != !file_islink(zFrom) ){
199 diff_printf("cannot compute difference between symlink and regular file\n");
200 }else{
201 diff_file(&content, zFileTreeName, zFileTreeName, zDiffCmd, ignoreEolWs);
202 }
203 blob_reset(&content);
204 blob_reset(&fname);
205 }
206
207 /*
@@ -222,32 +231,32 @@
231 if( !is_a_version(rid) ){
232 fossil_fatal("no such check-in: %s", zFrom);
233 }
234 load_vfile_from_rid(rid);
235 blob_appendf(&sql,
236 "SELECT v2.pathname, v2.deleted, v2.chnged, v2.rid==0, v1.rid, v1.islink"
237 " FROM vfile v1, vfile v2 "
238 " WHERE v1.pathname=v2.pathname AND v1.vid=%d AND v2.vid=%d"
239 " AND (v2.deleted OR v2.chnged OR v1.mrid!=v2.rid)"
240 "UNION "
241 "SELECT pathname, 1, 0, 0, 0, islink"
242 " FROM vfile v1"
243 " WHERE v1.vid=%d"
244 " AND NOT EXISTS(SELECT 1 FROM vfile v2"
245 " WHERE v2.vid=%d AND v2.pathname=v1.pathname)"
246 "UNION "
247 "SELECT pathname, 0, 0, 1, 0, islink"
248 " FROM vfile v2"
249 " WHERE v2.vid=%d"
250 " AND NOT EXISTS(SELECT 1 FROM vfile v1"
251 " WHERE v1.vid=%d AND v1.pathname=v2.pathname)"
252 " ORDER BY 1",
253 rid, vid, rid, vid, vid, rid
254 );
255 }else{
256 blob_appendf(&sql,
257 "SELECT pathname, deleted, chnged , rid==0, rid, islink"
258 " FROM vfile"
259 " WHERE vid=%d"
260 " AND (deleted OR chnged OR rid==0)"
261 " ORDER BY pathname",
262 vid
@@ -258,10 +267,11 @@
267 const char *zPathname = db_column_text(&q,0);
268 int isDeleted = db_column_int(&q, 1);
269 int isChnged = db_column_int(&q,2);
270 int isNew = db_column_int(&q,3);
271 int srcid = db_column_int(&q, 4);
272 int isLink = db_column_int(&q, 5);
273 char *zFullName = mprintf("%s%s", g.zLocalRoot, zPathname);
274 char *zToFree = zFullName;
275 int showDiff = 1;
276 if( isDeleted ){
277 diff_printf("DELETED %s\n", zPathname);
@@ -278,10 +288,16 @@
288 srcid = 0;
289 if( !asNewFile ){ showDiff = 0; }
290 }
291 if( showDiff ){
292 Blob content;
293 if( !isLink != !file_islink(zFullName) ){
294 diff_print_index(zPathname);
295 diff_printf("--- %s\n+++ %s\n", zPathname, zPathname);
296 diff_printf("cannot compute difference between symlink and regular file\n");
297 continue;
298 }
299 if( srcid>0 ){
300 content_get(srcid, &content);
301 }else{
302 blob_zero(&content);
303 }
@@ -307,15 +323,21 @@
323 const char *zFileTreeName
324 ){
325 char *zName;
326 Blob fname;
327 Blob v1, v2;
328 int isLink1, isLink2;
329 file_tree_name(zFileTreeName, &fname, 1);
330 zName = blob_str(&fname);
331 historical_version_of_file(zFrom, zName, &v1, &isLink1, 0, 0);
332 historical_version_of_file(zTo, zName, &v2, &isLink2, 0, 0);
333 if( isLink1 != isLink2 ){
334 diff_printf("--- %s\n+++ %s\n", zName, zName);
335 diff_printf("cannot compute difference between symlink and regular file\n");
336 }else{
337 diff_file_mem(&v1, &v2, zName, zDiffCmd, ignoreEolWs);
338 }
339 blob_reset(&v1);
340 blob_reset(&v2);
341 blob_reset(&fname);
342 }
343
344
+9 -2
--- src/export.c
+++ src/export.c
@@ -269,12 +269,19 @@
269269
const char *zName = db_column_text(&q4,0);
270270
int zNew = db_column_int(&q4,1);
271271
int mPerm = db_column_int(&q4,2);
272272
if( zNew==0)
273273
printf("D %s\n", zName);
274
- else if( bag_find(&blobs, zNew) )
275
- printf("M %s :%d %s\n", mPerm ? "100755" : "100644", BLOBMARK(zNew), zName);
274
+ else if( bag_find(&blobs, zNew) ) {
275
+ const char *zPerm;
276
+ switch( mPerm ){
277
+ case PERM_LNK: zPerm = "120000"; break;
278
+ case PERM_EXE: zPerm = "100755"; break;
279
+ default: zPerm = "100644"; break;
280
+ }
281
+ printf("M %s :%d %s\n", zPerm, BLOBMARK(zNew), zName);
282
+ }
276283
}
277284
db_finalize(&q4);
278285
db_finalize(&q3);
279286
printf("\n");
280287
}
281288
--- src/export.c
+++ src/export.c
@@ -269,12 +269,19 @@
269 const char *zName = db_column_text(&q4,0);
270 int zNew = db_column_int(&q4,1);
271 int mPerm = db_column_int(&q4,2);
272 if( zNew==0)
273 printf("D %s\n", zName);
274 else if( bag_find(&blobs, zNew) )
275 printf("M %s :%d %s\n", mPerm ? "100755" : "100644", BLOBMARK(zNew), zName);
 
 
 
 
 
 
 
276 }
277 db_finalize(&q4);
278 db_finalize(&q3);
279 printf("\n");
280 }
281
--- src/export.c
+++ src/export.c
@@ -269,12 +269,19 @@
269 const char *zName = db_column_text(&q4,0);
270 int zNew = db_column_int(&q4,1);
271 int mPerm = db_column_int(&q4,2);
272 if( zNew==0)
273 printf("D %s\n", zName);
274 else if( bag_find(&blobs, zNew) ) {
275 const char *zPerm;
276 switch( mPerm ){
277 case PERM_LNK: zPerm = "120000"; break;
278 case PERM_EXE: zPerm = "100755"; break;
279 default: zPerm = "100644"; break;
280 }
281 printf("M %s :%d %s\n", zPerm, BLOBMARK(zNew), zName);
282 }
283 }
284 db_finalize(&q4);
285 db_finalize(&q3);
286 printf("\n");
287 }
288
+123 -12
--- src/file.c
+++ src/file.c
@@ -30,16 +30,26 @@
3030
**
3131
** Use _stati64 rather than stat on windows, in order to handle files
3232
** larger than 2GB.
3333
*/
3434
#if defined(_WIN32) && defined(__MSVCRT__)
35
- static struct _stati64 fileStat;
3635
# define stat _stati64
37
-#else
38
- static struct stat fileStat;
3936
#endif
4037
static int fileStatValid = 0;
38
+static struct stat fileStat;
39
+
40
+static int fossil_stat(const char *zFilename, struct stat *buf){
41
+#if !defined(_WIN32)
42
+ if( g.allowSymlinks ){
43
+ return lstat(zFilename, buf);
44
+ }else{
45
+ return stat(zFilename, buf);
46
+ }
47
+#else
48
+ return stat(zFilename, buf);
49
+#endif
50
+}
4151
4252
/*
4353
** Fill in the fileStat variable for the file named zFilename.
4454
** If zFilename==0, then use the previous value of fileStat if
4555
** there is a previous value.
@@ -50,11 +60,11 @@
5060
int rc = 0;
5161
if( zFilename==0 ){
5262
if( fileStatValid==0 ) rc = 1;
5363
}else{
5464
char *zMbcs = fossil_utf8_to_mbcs(zFilename);
55
- if( stat(zMbcs, &fileStat)!=0 ){
65
+ if( fossil_stat(zMbcs, &fileStat)!=0 ){
5666
fileStatValid = 0;
5767
rc = 1;
5868
}else{
5969
fileStatValid = 1;
6070
rc = 0;
@@ -80,10 +90,24 @@
8090
** recently stat-ed file.
8191
*/
8292
i64 file_mtime(const char *zFilename){
8393
return getStat(zFilename) ? -1 : fileStat.st_mtime;
8494
}
95
+
96
+/*
97
+** Return TRUE if the named file is an ordinary file or symlink
98
+** and symlinks are allowed.
99
+** Return false for directories, devices, fifos, etc.
100
+*/
101
+int file_isfile_or_link(const char *zFilename){
102
+#if !defined(_WIN32)
103
+ if ( g.allowSymlinks ){
104
+ return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode) || S_ISLNK(fileStat.st_mode);
105
+ }
106
+#endif
107
+ return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
108
+}
85109
86110
/*
87111
** Return TRUE if the named file is an ordinary file. Return false
88112
** for directories, devices, fifos, symlinks, etc.
89113
*/
@@ -90,25 +114,98 @@
90114
int file_isfile(const char *zFilename){
91115
return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
92116
}
93117
94118
/*
95
-** Return TRUE if the named file is an executable. Return false
96
-** for directories, devices, fifos, symlinks, etc.
119
+** Create symlink to file on Unix, or plain-text file with
120
+** symlink target if "allow-symlinks" is off or we're on Windows.
121
+**
122
+** Arguments: target file (symlink will point to it), link file
123
+**/
124
+void create_symlink(const char *zTargetFile, const char *zLinkFile){
125
+#if !defined(_WIN32)
126
+ if( g.allowSymlinks ){
127
+ int i, nName;
128
+ char *zName, zBuf[1000];
129
+
130
+ nName = strlen(zLinkFile);
131
+ if( nName>=sizeof(zBuf) ){
132
+ zName = mprintf("%s", zLinkFile);
133
+ }else{
134
+ zName = zBuf;
135
+ memcpy(zName, zLinkFile, nName+1);
136
+ }
137
+ nName = file_simplify_name(zName, nName);
138
+ for(i=1; i<nName; i++){
139
+ if( zName[i]=='/' ){
140
+ zName[i] = 0;
141
+ if( file_mkdir(zName, 1) ){
142
+ fossil_fatal_recursive("unable to create directory %s", zName);
143
+ return;
144
+ }
145
+ zName[i] = '/';
146
+ }
147
+ }
148
+ if( zName!=zBuf ) free(zName);
149
+
150
+ if( symlink(zTargetFile, zName)!=0 ){
151
+ fossil_fatal_recursive("unable to create symlink \"%s\"", zName);
152
+ }
153
+ }else
154
+#endif
155
+ {
156
+ Blob content;
157
+ blob_set(&content, zTargetFile);
158
+ blob_write_to_file(&content, zLinkFile);
159
+ blob_reset(&content);
160
+ }
161
+}
162
+
163
+/*
164
+** Return file permissions (normal, executable, or symlink):
165
+** - PERM_EXE if file is executable;
166
+** - PERM_LNK on Unix if file is symlink and allow-symlinks option is on;
167
+** - PERM_REG for all other cases (regular file, directory, fifo, etc).
97168
*/
98
-int file_isexe(const char *zFilename){
99
- if( getStat(zFilename) || !S_ISREG(fileStat.st_mode) ) return 0;
169
+int file_perm(const char *zFilename){
170
+ if( getStat(zFilename) ) return PERM_REG;
100171
#if defined(_WIN32)
101172
# if defined(__DMC__) || defined(_MSC_VER)
102173
# define S_IXUSR _S_IEXEC
103174
# endif
104
- return ((S_IXUSR)&fileStat.st_mode)!=0;
175
+ if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
176
+ return PERM_EXE;
177
+ else
178
+ return PERM_REG;
105179
#else
106
- return ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0;
180
+ if( S_ISREG(fileStat.st_mode) &&
181
+ ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0 )
182
+ return PERM_EXE;
183
+ else if( g.allowSymlinks && S_ISLNK(fileStat.st_mode) )
184
+ return PERM_LNK;
185
+ else
186
+ return PERM_REG;
107187
#endif
108188
}
109189
190
+/*
191
+** Return TRUE if the named file is an executable. Return false
192
+** for directories, devices, fifos, symlinks, etc.
193
+*/
194
+int file_isexe(const char *zFilename){
195
+ return file_perm(zFilename)==PERM_EXE;
196
+}
197
+
198
+/*
199
+** Return TRUE if the named file is a symlink and symlinks are allowed.
200
+** Return false for all other cases.
201
+**
202
+** On Windows, always return False.
203
+*/
204
+int file_islink(const char *zFilename){
205
+ return file_perm(zFilename)==PERM_LNK;
206
+}
110207
111208
/*
112209
** Return 1 if zFilename is a directory. Return 0 if zFilename
113210
** does not exist. Return 2 if zFilename exists but is something
114211
** other than a directory.
@@ -122,11 +219,19 @@
122219
rc = getStat(zFN);
123220
free(zFN);
124221
}else{
125222
rc = getStat(0);
126223
}
224
+#if !defined(_WIN32)
225
+ if( g.allowSymlinks ){
226
+ return rc ? 0 : (S_ISDIR(fileStat.st_mode) && !S_ISLNK(fileStat.st_mode) ? 1 : 2);
227
+ }else{
228
+ return rc ? 0 : (S_ISDIR(fileStat.st_mode) ? 1 : 2);
229
+ }
230
+#else
127231
return rc ? 0 : (S_ISDIR(fileStat.st_mode) ? 1 : 2);
232
+#endif
128233
}
129234
130235
/*
131236
** Wrapper around the access() system call.
132237
*/
@@ -199,11 +304,11 @@
199304
*/
200305
int file_setexe(const char *zFilename, int onoff){
201306
int rc = 0;
202307
#if !defined(_WIN32)
203308
struct stat buf;
204
- if( stat(zFilename, &buf)!=0 ) return 0;
309
+ if( fossil_stat(zFilename, &buf)!=0 || S_ISLNK(buf.st_mode) ) return 0;
205310
if( onoff ){
206311
int targetMode = (buf.st_mode & 0444)>>2;
207312
if( (buf.st_mode & 0111)!=targetMode ){
208313
chmod(zFilename, buf.st_mode | targetMode);
209314
rc = 1;
@@ -469,10 +574,12 @@
469574
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zName));
470575
fossil_print(" file_size = %s\n", zBuf);
471576
sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_mtime(zName));
472577
fossil_print(" file_mtime = %s\n", zBuf);
473578
fossil_print(" file_isfile = %d\n", file_isfile(zName));
579
+ fossil_print(" file_isfile_or_link = %d\n", file_isfile_or_link(zName));
580
+ fossil_print(" file_islink = %d\n", file_islink(zName));
474581
fossil_print(" file_isexe = %d\n", file_isexe(zName));
475582
fossil_print(" file_isdir = %d\n", file_isdir(zName));
476583
}
477584
}
478585
@@ -739,11 +846,15 @@
739846
Blob onDisk;
740847
741848
iSize = file_size(zName);
742849
if( iSize<0 ) return 0;
743850
if( iSize!=blob_size(pContent) ) return 0;
744
- blob_read_from_file(&onDisk, zName);
851
+ if( file_islink(zName) ){
852
+ blob_read_link(&onDisk, zName);
853
+ }else{
854
+ blob_read_from_file(&onDisk, zName);
855
+ }
745856
rc = blob_compare(&onDisk, pContent);
746857
blob_reset(&onDisk);
747858
return rc==0;
748859
}
749860
750861
--- src/file.c
+++ src/file.c
@@ -30,16 +30,26 @@
30 **
31 ** Use _stati64 rather than stat on windows, in order to handle files
32 ** larger than 2GB.
33 */
34 #if defined(_WIN32) && defined(__MSVCRT__)
35 static struct _stati64 fileStat;
36 # define stat _stati64
37 #else
38 static struct stat fileStat;
39 #endif
40 static int fileStatValid = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
41
42 /*
43 ** Fill in the fileStat variable for the file named zFilename.
44 ** If zFilename==0, then use the previous value of fileStat if
45 ** there is a previous value.
@@ -50,11 +60,11 @@
50 int rc = 0;
51 if( zFilename==0 ){
52 if( fileStatValid==0 ) rc = 1;
53 }else{
54 char *zMbcs = fossil_utf8_to_mbcs(zFilename);
55 if( stat(zMbcs, &fileStat)!=0 ){
56 fileStatValid = 0;
57 rc = 1;
58 }else{
59 fileStatValid = 1;
60 rc = 0;
@@ -80,10 +90,24 @@
80 ** recently stat-ed file.
81 */
82 i64 file_mtime(const char *zFilename){
83 return getStat(zFilename) ? -1 : fileStat.st_mtime;
84 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
86 /*
87 ** Return TRUE if the named file is an ordinary file. Return false
88 ** for directories, devices, fifos, symlinks, etc.
89 */
@@ -90,25 +114,98 @@
90 int file_isfile(const char *zFilename){
91 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
92 }
93
94 /*
95 ** Return TRUE if the named file is an executable. Return false
96 ** for directories, devices, fifos, symlinks, etc.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97 */
98 int file_isexe(const char *zFilename){
99 if( getStat(zFilename) || !S_ISREG(fileStat.st_mode) ) return 0;
100 #if defined(_WIN32)
101 # if defined(__DMC__) || defined(_MSC_VER)
102 # define S_IXUSR _S_IEXEC
103 # endif
104 return ((S_IXUSR)&fileStat.st_mode)!=0;
 
 
 
105 #else
106 return ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0;
 
 
 
 
 
 
107 #endif
108 }
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
111 /*
112 ** Return 1 if zFilename is a directory. Return 0 if zFilename
113 ** does not exist. Return 2 if zFilename exists but is something
114 ** other than a directory.
@@ -122,11 +219,19 @@
122 rc = getStat(zFN);
123 free(zFN);
124 }else{
125 rc = getStat(0);
126 }
 
 
 
 
 
 
 
127 return rc ? 0 : (S_ISDIR(fileStat.st_mode) ? 1 : 2);
 
128 }
129
130 /*
131 ** Wrapper around the access() system call.
132 */
@@ -199,11 +304,11 @@
199 */
200 int file_setexe(const char *zFilename, int onoff){
201 int rc = 0;
202 #if !defined(_WIN32)
203 struct stat buf;
204 if( stat(zFilename, &buf)!=0 ) return 0;
205 if( onoff ){
206 int targetMode = (buf.st_mode & 0444)>>2;
207 if( (buf.st_mode & 0111)!=targetMode ){
208 chmod(zFilename, buf.st_mode | targetMode);
209 rc = 1;
@@ -469,10 +574,12 @@
469 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zName));
470 fossil_print(" file_size = %s\n", zBuf);
471 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_mtime(zName));
472 fossil_print(" file_mtime = %s\n", zBuf);
473 fossil_print(" file_isfile = %d\n", file_isfile(zName));
 
 
474 fossil_print(" file_isexe = %d\n", file_isexe(zName));
475 fossil_print(" file_isdir = %d\n", file_isdir(zName));
476 }
477 }
478
@@ -739,11 +846,15 @@
739 Blob onDisk;
740
741 iSize = file_size(zName);
742 if( iSize<0 ) return 0;
743 if( iSize!=blob_size(pContent) ) return 0;
744 blob_read_from_file(&onDisk, zName);
 
 
 
 
745 rc = blob_compare(&onDisk, pContent);
746 blob_reset(&onDisk);
747 return rc==0;
748 }
749
750
--- src/file.c
+++ src/file.c
@@ -30,16 +30,26 @@
30 **
31 ** Use _stati64 rather than stat on windows, in order to handle files
32 ** larger than 2GB.
33 */
34 #if defined(_WIN32) && defined(__MSVCRT__)
 
35 # define stat _stati64
 
 
36 #endif
37 static int fileStatValid = 0;
38 static struct stat fileStat;
39
40 static int fossil_stat(const char *zFilename, struct stat *buf){
41 #if !defined(_WIN32)
42 if( g.allowSymlinks ){
43 return lstat(zFilename, buf);
44 }else{
45 return stat(zFilename, buf);
46 }
47 #else
48 return stat(zFilename, buf);
49 #endif
50 }
51
52 /*
53 ** Fill in the fileStat variable for the file named zFilename.
54 ** If zFilename==0, then use the previous value of fileStat if
55 ** there is a previous value.
@@ -50,11 +60,11 @@
60 int rc = 0;
61 if( zFilename==0 ){
62 if( fileStatValid==0 ) rc = 1;
63 }else{
64 char *zMbcs = fossil_utf8_to_mbcs(zFilename);
65 if( fossil_stat(zMbcs, &fileStat)!=0 ){
66 fileStatValid = 0;
67 rc = 1;
68 }else{
69 fileStatValid = 1;
70 rc = 0;
@@ -80,10 +90,24 @@
90 ** recently stat-ed file.
91 */
92 i64 file_mtime(const char *zFilename){
93 return getStat(zFilename) ? -1 : fileStat.st_mtime;
94 }
95
96 /*
97 ** Return TRUE if the named file is an ordinary file or symlink
98 ** and symlinks are allowed.
99 ** Return false for directories, devices, fifos, etc.
100 */
101 int file_isfile_or_link(const char *zFilename){
102 #if !defined(_WIN32)
103 if ( g.allowSymlinks ){
104 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode) || S_ISLNK(fileStat.st_mode);
105 }
106 #endif
107 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
108 }
109
110 /*
111 ** Return TRUE if the named file is an ordinary file. Return false
112 ** for directories, devices, fifos, symlinks, etc.
113 */
@@ -90,25 +114,98 @@
114 int file_isfile(const char *zFilename){
115 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
116 }
117
118 /*
119 ** Create symlink to file on Unix, or plain-text file with
120 ** symlink target if "allow-symlinks" is off or we're on Windows.
121 **
122 ** Arguments: target file (symlink will point to it), link file
123 **/
124 void create_symlink(const char *zTargetFile, const char *zLinkFile){
125 #if !defined(_WIN32)
126 if( g.allowSymlinks ){
127 int i, nName;
128 char *zName, zBuf[1000];
129
130 nName = strlen(zLinkFile);
131 if( nName>=sizeof(zBuf) ){
132 zName = mprintf("%s", zLinkFile);
133 }else{
134 zName = zBuf;
135 memcpy(zName, zLinkFile, nName+1);
136 }
137 nName = file_simplify_name(zName, nName);
138 for(i=1; i<nName; i++){
139 if( zName[i]=='/' ){
140 zName[i] = 0;
141 if( file_mkdir(zName, 1) ){
142 fossil_fatal_recursive("unable to create directory %s", zName);
143 return;
144 }
145 zName[i] = '/';
146 }
147 }
148 if( zName!=zBuf ) free(zName);
149
150 if( symlink(zTargetFile, zName)!=0 ){
151 fossil_fatal_recursive("unable to create symlink \"%s\"", zName);
152 }
153 }else
154 #endif
155 {
156 Blob content;
157 blob_set(&content, zTargetFile);
158 blob_write_to_file(&content, zLinkFile);
159 blob_reset(&content);
160 }
161 }
162
163 /*
164 ** Return file permissions (normal, executable, or symlink):
165 ** - PERM_EXE if file is executable;
166 ** - PERM_LNK on Unix if file is symlink and allow-symlinks option is on;
167 ** - PERM_REG for all other cases (regular file, directory, fifo, etc).
168 */
169 int file_perm(const char *zFilename){
170 if( getStat(zFilename) ) return PERM_REG;
171 #if defined(_WIN32)
172 # if defined(__DMC__) || defined(_MSC_VER)
173 # define S_IXUSR _S_IEXEC
174 # endif
175 if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
176 return PERM_EXE;
177 else
178 return PERM_REG;
179 #else
180 if( S_ISREG(fileStat.st_mode) &&
181 ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0 )
182 return PERM_EXE;
183 else if( g.allowSymlinks && S_ISLNK(fileStat.st_mode) )
184 return PERM_LNK;
185 else
186 return PERM_REG;
187 #endif
188 }
189
190 /*
191 ** Return TRUE if the named file is an executable. Return false
192 ** for directories, devices, fifos, symlinks, etc.
193 */
194 int file_isexe(const char *zFilename){
195 return file_perm(zFilename)==PERM_EXE;
196 }
197
198 /*
199 ** Return TRUE if the named file is a symlink and symlinks are allowed.
200 ** Return false for all other cases.
201 **
202 ** On Windows, always return False.
203 */
204 int file_islink(const char *zFilename){
205 return file_perm(zFilename)==PERM_LNK;
206 }
207
208 /*
209 ** Return 1 if zFilename is a directory. Return 0 if zFilename
210 ** does not exist. Return 2 if zFilename exists but is something
211 ** other than a directory.
@@ -122,11 +219,19 @@
219 rc = getStat(zFN);
220 free(zFN);
221 }else{
222 rc = getStat(0);
223 }
224 #if !defined(_WIN32)
225 if( g.allowSymlinks ){
226 return rc ? 0 : (S_ISDIR(fileStat.st_mode) && !S_ISLNK(fileStat.st_mode) ? 1 : 2);
227 }else{
228 return rc ? 0 : (S_ISDIR(fileStat.st_mode) ? 1 : 2);
229 }
230 #else
231 return rc ? 0 : (S_ISDIR(fileStat.st_mode) ? 1 : 2);
232 #endif
233 }
234
235 /*
236 ** Wrapper around the access() system call.
237 */
@@ -199,11 +304,11 @@
304 */
305 int file_setexe(const char *zFilename, int onoff){
306 int rc = 0;
307 #if !defined(_WIN32)
308 struct stat buf;
309 if( fossil_stat(zFilename, &buf)!=0 || S_ISLNK(buf.st_mode) ) return 0;
310 if( onoff ){
311 int targetMode = (buf.st_mode & 0444)>>2;
312 if( (buf.st_mode & 0111)!=targetMode ){
313 chmod(zFilename, buf.st_mode | targetMode);
314 rc = 1;
@@ -469,10 +574,12 @@
574 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_size(zName));
575 fossil_print(" file_size = %s\n", zBuf);
576 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", file_mtime(zName));
577 fossil_print(" file_mtime = %s\n", zBuf);
578 fossil_print(" file_isfile = %d\n", file_isfile(zName));
579 fossil_print(" file_isfile_or_link = %d\n", file_isfile_or_link(zName));
580 fossil_print(" file_islink = %d\n", file_islink(zName));
581 fossil_print(" file_isexe = %d\n", file_isexe(zName));
582 fossil_print(" file_isdir = %d\n", file_isdir(zName));
583 }
584 }
585
@@ -739,11 +846,15 @@
846 Blob onDisk;
847
848 iSize = file_size(zName);
849 if( iSize<0 ) return 0;
850 if( iSize!=blob_size(pContent) ) return 0;
851 if( file_islink(zName) ){
852 blob_read_link(&onDisk, zName);
853 }else{
854 blob_read_from_file(&onDisk, zName);
855 }
856 rc = blob_compare(&onDisk, pContent);
857 blob_reset(&onDisk);
858 return rc==0;
859 }
860
861
+1 -1
--- src/finfo.c
+++ src/finfo.c
@@ -99,11 +99,11 @@
9999
Blob fname;
100100
const char *zRevision = find_option("revision", "r", 1);
101101
102102
file_tree_name(g.argv[2], &fname, 1);
103103
if( zRevision ){
104
- historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0);
104
+ historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0, 0);
105105
}else{
106106
int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
107107
if( rid==0 ){
108108
fossil_fatal("no history for file: %b", &fname);
109109
}
110110
--- src/finfo.c
+++ src/finfo.c
@@ -99,11 +99,11 @@
99 Blob fname;
100 const char *zRevision = find_option("revision", "r", 1);
101
102 file_tree_name(g.argv[2], &fname, 1);
103 if( zRevision ){
104 historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0);
105 }else{
106 int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
107 if( rid==0 ){
108 fossil_fatal("no history for file: %b", &fname);
109 }
110
--- src/finfo.c
+++ src/finfo.c
@@ -99,11 +99,11 @@
99 Blob fname;
100 const char *zRevision = find_option("revision", "r", 1);
101
102 file_tree_name(g.argv[2], &fname, 1);
103 if( zRevision ){
104 historical_version_of_file(zRevision, blob_str(&fname), &record, 0, 0, 0);
105 }else{
106 int rid = db_int(0, "SELECT rid FROM vfile WHERE pathname=%B", &fname);
107 if( rid==0 ){
108 fossil_fatal("no history for file: %b", &fname);
109 }
110
+13 -2
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249249
int j;
250250
for( j = 0; j < mdLength; ++j ) {
251251
BIO_printf(mem, " %02x", md[j]);
252252
}
253253
}
254
- BIO_write(mem, "", 1); // null-terminate mem buffer
254
+ BIO_write(mem, "", 1); /* null-terminate mem buffer */
255255
BIO_get_mem_data(mem, &desc);
256256
257257
if( hasSavedCertificate ){
258258
warning = "WARNING: Certificate doesn't match the "
259259
"saved certificate for this host!";
@@ -280,10 +280,21 @@
280280
if( blob_str(&ans)[0]=='a' ) {
281281
ssl_save_certificate(cert);
282282
}
283283
blob_reset(&ans);
284284
}
285
+
286
+ /* Set the Global.zIpAddr variable to the server we are talking to.
287
+ ** This is used to populate the ipaddr column of the rcvfrom table,
288
+ ** if any files are received from the server.
289
+ */
290
+ {
291
+ /* IPv4 only code */
292
+ const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
293
+ g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
294
+ }
295
+
285296
X509_free(cert);
286297
return 0;
287298
}
288299
289300
/*
@@ -293,11 +304,11 @@
293304
BIO *mem;
294305
char *zCert, *zHost;
295306
296307
mem = BIO_new(BIO_s_mem());
297308
PEM_write_bio_X509(mem, cert);
298
- BIO_write(mem, "", 1); // null-terminate mem buffer
309
+ BIO_write(mem, "", 1); /* null-terminate mem buffer */
299310
BIO_get_mem_data(mem, &zCert);
300311
zHost = mprintf("cert:%s", g.urlName);
301312
db_set(zHost, zCert, 1);
302313
free(zHost);
303314
BIO_free(mem);
304315
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249 int j;
250 for( j = 0; j < mdLength; ++j ) {
251 BIO_printf(mem, " %02x", md[j]);
252 }
253 }
254 BIO_write(mem, "", 1); // null-terminate mem buffer
255 BIO_get_mem_data(mem, &desc);
256
257 if( hasSavedCertificate ){
258 warning = "WARNING: Certificate doesn't match the "
259 "saved certificate for this host!";
@@ -280,10 +280,21 @@
280 if( blob_str(&ans)[0]=='a' ) {
281 ssl_save_certificate(cert);
282 }
283 blob_reset(&ans);
284 }
 
 
 
 
 
 
 
 
 
 
 
285 X509_free(cert);
286 return 0;
287 }
288
289 /*
@@ -293,11 +304,11 @@
293 BIO *mem;
294 char *zCert, *zHost;
295
296 mem = BIO_new(BIO_s_mem());
297 PEM_write_bio_X509(mem, cert);
298 BIO_write(mem, "", 1); // null-terminate mem buffer
299 BIO_get_mem_data(mem, &zCert);
300 zHost = mprintf("cert:%s", g.urlName);
301 db_set(zHost, zCert, 1);
302 free(zHost);
303 BIO_free(mem);
304
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249 int j;
250 for( j = 0; j < mdLength; ++j ) {
251 BIO_printf(mem, " %02x", md[j]);
252 }
253 }
254 BIO_write(mem, "", 1); /* null-terminate mem buffer */
255 BIO_get_mem_data(mem, &desc);
256
257 if( hasSavedCertificate ){
258 warning = "WARNING: Certificate doesn't match the "
259 "saved certificate for this host!";
@@ -280,10 +280,21 @@
280 if( blob_str(&ans)[0]=='a' ) {
281 ssl_save_certificate(cert);
282 }
283 blob_reset(&ans);
284 }
285
286 /* Set the Global.zIpAddr variable to the server we are talking to.
287 ** This is used to populate the ipaddr column of the rcvfrom table,
288 ** if any files are received from the server.
289 */
290 {
291 /* IPv4 only code */
292 const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
293 g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
294 }
295
296 X509_free(cert);
297 return 0;
298 }
299
300 /*
@@ -293,11 +304,11 @@
304 BIO *mem;
305 char *zCert, *zHost;
306
307 mem = BIO_new(BIO_s_mem());
308 PEM_write_bio_X509(mem, cert);
309 BIO_write(mem, "", 1); /* null-terminate mem buffer */
310 BIO_get_mem_data(mem, &zCert);
311 zHost = mprintf("cert:%s", g.urlName);
312 db_set(zHost, zCert, 1);
313 free(zHost);
314 BIO_free(mem);
315
+13 -2
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249249
int j;
250250
for( j = 0; j < mdLength; ++j ) {
251251
BIO_printf(mem, " %02x", md[j]);
252252
}
253253
}
254
- BIO_write(mem, "", 1); // null-terminate mem buffer
254
+ BIO_write(mem, "", 1); /* null-terminate mem buffer */
255255
BIO_get_mem_data(mem, &desc);
256256
257257
if( hasSavedCertificate ){
258258
warning = "WARNING: Certificate doesn't match the "
259259
"saved certificate for this host!";
@@ -280,10 +280,21 @@
280280
if( blob_str(&ans)[0]=='a' ) {
281281
ssl_save_certificate(cert);
282282
}
283283
blob_reset(&ans);
284284
}
285
+
286
+ /* Set the Global.zIpAddr variable to the server we are talking to.
287
+ ** This is used to populate the ipaddr column of the rcvfrom table,
288
+ ** if any files are received from the server.
289
+ */
290
+ {
291
+ /* IPv4 only code */
292
+ const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
293
+ g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
294
+ }
295
+
285296
X509_free(cert);
286297
return 0;
287298
}
288299
289300
/*
@@ -293,11 +304,11 @@
293304
BIO *mem;
294305
char *zCert, *zHost;
295306
296307
mem = BIO_new(BIO_s_mem());
297308
PEM_write_bio_X509(mem, cert);
298
- BIO_write(mem, "", 1); // null-terminate mem buffer
309
+ BIO_write(mem, "", 1); /* null-terminate mem buffer */
299310
BIO_get_mem_data(mem, &zCert);
300311
zHost = mprintf("cert:%s", g.urlName);
301312
db_set(zHost, zCert, 1);
302313
free(zHost);
303314
BIO_free(mem);
304315
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249 int j;
250 for( j = 0; j < mdLength; ++j ) {
251 BIO_printf(mem, " %02x", md[j]);
252 }
253 }
254 BIO_write(mem, "", 1); // null-terminate mem buffer
255 BIO_get_mem_data(mem, &desc);
256
257 if( hasSavedCertificate ){
258 warning = "WARNING: Certificate doesn't match the "
259 "saved certificate for this host!";
@@ -280,10 +280,21 @@
280 if( blob_str(&ans)[0]=='a' ) {
281 ssl_save_certificate(cert);
282 }
283 blob_reset(&ans);
284 }
 
 
 
 
 
 
 
 
 
 
 
285 X509_free(cert);
286 return 0;
287 }
288
289 /*
@@ -293,11 +304,11 @@
293 BIO *mem;
294 char *zCert, *zHost;
295
296 mem = BIO_new(BIO_s_mem());
297 PEM_write_bio_X509(mem, cert);
298 BIO_write(mem, "", 1); // null-terminate mem buffer
299 BIO_get_mem_data(mem, &zCert);
300 zHost = mprintf("cert:%s", g.urlName);
301 db_set(zHost, zCert, 1);
302 free(zHost);
303 BIO_free(mem);
304
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -249,11 +249,11 @@
249 int j;
250 for( j = 0; j < mdLength; ++j ) {
251 BIO_printf(mem, " %02x", md[j]);
252 }
253 }
254 BIO_write(mem, "", 1); /* null-terminate mem buffer */
255 BIO_get_mem_data(mem, &desc);
256
257 if( hasSavedCertificate ){
258 warning = "WARNING: Certificate doesn't match the "
259 "saved certificate for this host!";
@@ -280,10 +280,21 @@
280 if( blob_str(&ans)[0]=='a' ) {
281 ssl_save_certificate(cert);
282 }
283 blob_reset(&ans);
284 }
285
286 /* Set the Global.zIpAddr variable to the server we are talking to.
287 ** This is used to populate the ipaddr column of the rcvfrom table,
288 ** if any files are received from the server.
289 */
290 {
291 /* IPv4 only code */
292 const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
293 g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
294 }
295
296 X509_free(cert);
297 return 0;
298 }
299
300 /*
@@ -293,11 +304,11 @@
304 BIO *mem;
305 char *zCert, *zHost;
306
307 mem = BIO_new(BIO_s_mem());
308 PEM_write_bio_X509(mem, cert);
309 BIO_write(mem, "", 1); /* null-terminate mem buffer */
310 BIO_get_mem_data(mem, &zCert);
311 zHost = mprintf("cert:%s", g.urlName);
312 db_set(zHost, zCert, 1);
313 free(zHost);
314 BIO_free(mem);
315
+12
--- src/import.c
+++ src/import.c
@@ -30,10 +30,11 @@
3030
char *zName; /* Name of a file */
3131
char *zUuid; /* UUID of the file */
3232
char *zPrior; /* Prior name if the name was changed */
3333
char isFrom; /* True if obtained from the parent */
3434
char isExe; /* True if executable */
35
+ char isLink; /* True if symlink */
3536
};
3637
#endif
3738
3839
3940
/*
@@ -58,10 +59,11 @@
5859
char **azMerge; /* Merge values */
5960
int nFile; /* Number of aFile values */
6061
int nFileAlloc; /* Number of slots in aFile[] */
6162
ImportFile *aFile; /* Information about files in a commit */
6263
int fromLoaded; /* True zFrom content loaded into aFile[] */
64
+ int hasLinks; /* True if git repository contains symlinks */
6365
int tagCommit; /* True if the commit adds a tag */
6466
} gg;
6567
6668
/*
6769
** Duplicate a string.
@@ -242,10 +244,13 @@
242244
const char *zUuid = gg.aFile[i].zUuid;
243245
if( zUuid==0 ) continue;
244246
blob_appendf(&record, "F %F %s", gg.aFile[i].zName, zUuid);
245247
if( gg.aFile[i].isExe ){
246248
blob_append(&record, " x\n", 3);
249
+ }else if( gg.aFile[i].isLink ){
250
+ blob_append(&record, " l\n", 3);
251
+ gg.hasLinks = 1;
247252
}else{
248253
blob_append(&record, "\n", 1);
249254
}
250255
}
251256
if( gg.zFrom ){
@@ -421,10 +426,11 @@
421426
manifest_file_rewind(p);
422427
while( (pOld = manifest_file_next(p, 0))!=0 ){
423428
pNew = import_add_file();
424429
pNew->zName = fossil_strdup(pOld->zName);
425430
pNew->isExe = pOld->zPerm && strstr(pOld->zPerm, "x")!=0;
431
+ pNew->isLink = pOld->zPerm && strstr(pOld->zPerm, "l")!=0;
426432
pNew->zUuid = fossil_strdup(pOld->zUuid);
427433
pNew->isFrom = 1;
428434
}
429435
manifest_destroy(p);
430436
}
@@ -597,10 +603,11 @@
597603
if( pFile==0 ){
598604
pFile = import_add_file();
599605
pFile->zName = fossil_strdup(zName);
600606
}
601607
pFile->isExe = (fossil_strcmp(zPerm, "100755")==0);
608
+ pFile->isLink = (fossil_strcmp(zPerm, "120000")==0);
602609
fossil_free(pFile->zUuid);
603610
pFile->zUuid = resolve_committish(zUuid);
604611
pFile->isFrom = 0;
605612
}else
606613
if( memcmp(zLine, "D ", 2)==0 ){
@@ -634,10 +641,11 @@
634641
pNew->zName = mprintf("%s%s", zTo, pFile->zName[nFrom]);
635642
}else{
636643
pNew->zName = fossil_strdup(pFile->zName);
637644
}
638645
pNew->isExe = pFile->isExe;
646
+ pNew->isLink = pFile->isLink;
639647
pNew->zUuid = fossil_strdup(pFile->zUuid);
640648
pNew->isFrom = 0;
641649
}
642650
}else
643651
if( memcmp(zLine, "R ", 2)==0 ){
@@ -657,10 +665,11 @@
657665
}else{
658666
pNew->zName = fossil_strdup(pFile->zName);
659667
}
660668
pNew->zPrior = pFile->zName;
661669
pNew->isExe = pFile->isExe;
670
+ pNew->isLink = pFile->isLink;
662671
pNew->zUuid = pFile->zUuid;
663672
pNew->isFrom = 0;
664673
gg.nFile--;
665674
*pFile = *pNew;
666675
memset(pNew, 0, sizeof(*pNew));
@@ -677,10 +686,13 @@
677686
{
678687
goto malformed_line;
679688
}
680689
}
681690
gg.xFinish();
691
+ if( gg.hasLinks ){
692
+ db_set_int("allow-symlinks", 1, 0);
693
+ }
682694
import_reset(1);
683695
return;
684696
685697
malformed_line:
686698
trim_newline(zLine);
687699
--- src/import.c
+++ src/import.c
@@ -30,10 +30,11 @@
30 char *zName; /* Name of a file */
31 char *zUuid; /* UUID of the file */
32 char *zPrior; /* Prior name if the name was changed */
33 char isFrom; /* True if obtained from the parent */
34 char isExe; /* True if executable */
 
35 };
36 #endif
37
38
39 /*
@@ -58,10 +59,11 @@
58 char **azMerge; /* Merge values */
59 int nFile; /* Number of aFile values */
60 int nFileAlloc; /* Number of slots in aFile[] */
61 ImportFile *aFile; /* Information about files in a commit */
62 int fromLoaded; /* True zFrom content loaded into aFile[] */
 
63 int tagCommit; /* True if the commit adds a tag */
64 } gg;
65
66 /*
67 ** Duplicate a string.
@@ -242,10 +244,13 @@
242 const char *zUuid = gg.aFile[i].zUuid;
243 if( zUuid==0 ) continue;
244 blob_appendf(&record, "F %F %s", gg.aFile[i].zName, zUuid);
245 if( gg.aFile[i].isExe ){
246 blob_append(&record, " x\n", 3);
 
 
 
247 }else{
248 blob_append(&record, "\n", 1);
249 }
250 }
251 if( gg.zFrom ){
@@ -421,10 +426,11 @@
421 manifest_file_rewind(p);
422 while( (pOld = manifest_file_next(p, 0))!=0 ){
423 pNew = import_add_file();
424 pNew->zName = fossil_strdup(pOld->zName);
425 pNew->isExe = pOld->zPerm && strstr(pOld->zPerm, "x")!=0;
 
426 pNew->zUuid = fossil_strdup(pOld->zUuid);
427 pNew->isFrom = 1;
428 }
429 manifest_destroy(p);
430 }
@@ -597,10 +603,11 @@
597 if( pFile==0 ){
598 pFile = import_add_file();
599 pFile->zName = fossil_strdup(zName);
600 }
601 pFile->isExe = (fossil_strcmp(zPerm, "100755")==0);
 
602 fossil_free(pFile->zUuid);
603 pFile->zUuid = resolve_committish(zUuid);
604 pFile->isFrom = 0;
605 }else
606 if( memcmp(zLine, "D ", 2)==0 ){
@@ -634,10 +641,11 @@
634 pNew->zName = mprintf("%s%s", zTo, pFile->zName[nFrom]);
635 }else{
636 pNew->zName = fossil_strdup(pFile->zName);
637 }
638 pNew->isExe = pFile->isExe;
 
639 pNew->zUuid = fossil_strdup(pFile->zUuid);
640 pNew->isFrom = 0;
641 }
642 }else
643 if( memcmp(zLine, "R ", 2)==0 ){
@@ -657,10 +665,11 @@
657 }else{
658 pNew->zName = fossil_strdup(pFile->zName);
659 }
660 pNew->zPrior = pFile->zName;
661 pNew->isExe = pFile->isExe;
 
662 pNew->zUuid = pFile->zUuid;
663 pNew->isFrom = 0;
664 gg.nFile--;
665 *pFile = *pNew;
666 memset(pNew, 0, sizeof(*pNew));
@@ -677,10 +686,13 @@
677 {
678 goto malformed_line;
679 }
680 }
681 gg.xFinish();
 
 
 
682 import_reset(1);
683 return;
684
685 malformed_line:
686 trim_newline(zLine);
687
--- src/import.c
+++ src/import.c
@@ -30,10 +30,11 @@
30 char *zName; /* Name of a file */
31 char *zUuid; /* UUID of the file */
32 char *zPrior; /* Prior name if the name was changed */
33 char isFrom; /* True if obtained from the parent */
34 char isExe; /* True if executable */
35 char isLink; /* True if symlink */
36 };
37 #endif
38
39
40 /*
@@ -58,10 +59,11 @@
59 char **azMerge; /* Merge values */
60 int nFile; /* Number of aFile values */
61 int nFileAlloc; /* Number of slots in aFile[] */
62 ImportFile *aFile; /* Information about files in a commit */
63 int fromLoaded; /* True zFrom content loaded into aFile[] */
64 int hasLinks; /* True if git repository contains symlinks */
65 int tagCommit; /* True if the commit adds a tag */
66 } gg;
67
68 /*
69 ** Duplicate a string.
@@ -242,10 +244,13 @@
244 const char *zUuid = gg.aFile[i].zUuid;
245 if( zUuid==0 ) continue;
246 blob_appendf(&record, "F %F %s", gg.aFile[i].zName, zUuid);
247 if( gg.aFile[i].isExe ){
248 blob_append(&record, " x\n", 3);
249 }else if( gg.aFile[i].isLink ){
250 blob_append(&record, " l\n", 3);
251 gg.hasLinks = 1;
252 }else{
253 blob_append(&record, "\n", 1);
254 }
255 }
256 if( gg.zFrom ){
@@ -421,10 +426,11 @@
426 manifest_file_rewind(p);
427 while( (pOld = manifest_file_next(p, 0))!=0 ){
428 pNew = import_add_file();
429 pNew->zName = fossil_strdup(pOld->zName);
430 pNew->isExe = pOld->zPerm && strstr(pOld->zPerm, "x")!=0;
431 pNew->isLink = pOld->zPerm && strstr(pOld->zPerm, "l")!=0;
432 pNew->zUuid = fossil_strdup(pOld->zUuid);
433 pNew->isFrom = 1;
434 }
435 manifest_destroy(p);
436 }
@@ -597,10 +603,11 @@
603 if( pFile==0 ){
604 pFile = import_add_file();
605 pFile->zName = fossil_strdup(zName);
606 }
607 pFile->isExe = (fossil_strcmp(zPerm, "100755")==0);
608 pFile->isLink = (fossil_strcmp(zPerm, "120000")==0);
609 fossil_free(pFile->zUuid);
610 pFile->zUuid = resolve_committish(zUuid);
611 pFile->isFrom = 0;
612 }else
613 if( memcmp(zLine, "D ", 2)==0 ){
@@ -634,10 +641,11 @@
641 pNew->zName = mprintf("%s%s", zTo, pFile->zName[nFrom]);
642 }else{
643 pNew->zName = fossil_strdup(pFile->zName);
644 }
645 pNew->isExe = pFile->isExe;
646 pNew->isLink = pFile->isLink;
647 pNew->zUuid = fossil_strdup(pFile->zUuid);
648 pNew->isFrom = 0;
649 }
650 }else
651 if( memcmp(zLine, "R ", 2)==0 ){
@@ -657,10 +665,11 @@
665 }else{
666 pNew->zName = fossil_strdup(pFile->zName);
667 }
668 pNew->zPrior = pFile->zName;
669 pNew->isExe = pFile->isExe;
670 pNew->isLink = pFile->isLink;
671 pNew->zUuid = pFile->zUuid;
672 pNew->isFrom = 0;
673 gg.nFile--;
674 *pFile = *pNew;
675 memset(pNew, 0, sizeof(*pNew));
@@ -677,10 +686,13 @@
686 {
687 goto malformed_line;
688 }
689 }
690 gg.xFinish();
691 if( gg.hasLinks ){
692 db_set_int("allow-symlinks", 1, 0);
693 }
694 import_reset(1);
695 return;
696
697 malformed_line:
698 trim_newline(zLine);
699
+51 -30
--- src/info.c
+++ src/info.c
@@ -282,21 +282,22 @@
282282
const char *zName, /* Name of the file that has changed */
283283
const char *zOld, /* blob.uuid before change. NULL for added files */
284284
const char *zNew, /* blob.uuid after change. NULL for deletes */
285285
const char *zOldName, /* Prior name. NULL if no name change. */
286286
int showDiff, /* Show edit diffs if true */
287
- int mperm /* EXE permission for zNew */
287
+ int mperm /* executable or symlink permission for zNew */
288288
){
289289
if( !g.okHistory ){
290290
if( zNew==0 ){
291291
@ <p>Deleted %h(zName)</p>
292292
}else if( zOld==0 ){
293293
@ <p>Added %h(zName)</p>
294294
}else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
295295
@ <p>Name change from %h(zOldName) to %h(zName)
296296
}else if( fossil_strcmp(zNew, zOld)==0 ){
297
- @ <p>Execute permission %s(mperm?"set":"cleared") for %h(zName)</p>
297
+ @ <p>Execute permission %s(( mperm==PERM_EXE )?"set":"cleared")
298
+ @ for %h(zName)</p>
298299
}else{
299300
@ <p>Changes to %h(zName)</p>
300301
}
301302
if( showDiff ){
302303
@ <blockquote><pre>
@@ -312,11 +313,11 @@
312313
}else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
313314
@ <p>Name change from
314315
@ from <a href="%s(g.zTop)/finfo?name=%T(zOldName)">%h(zOldName)</a>
315316
@ to <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>.
316317
}else{
317
- @ <p>Execute permission %s(mperm?"set":"cleared") for
318
+ @ <p>Execute permission %s(( mperm==PERM_EXE )?"set":"cleared") for
318319
@ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
319320
}
320321
}else if( zOld ){
321322
@ <p>Deleted <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
322323
@ version <a href="%s(g.zTop)/artifact/%s(zOld)">[%S(zOld)]</a>
@@ -776,53 +777,81 @@
776777
Stmt q;
777778
int cnt = 0;
778779
int nWiki = 0;
779780
char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
780781
782
+ char *prevName = 0;
783
+
781784
db_prepare(&q,
782785
"SELECT filename.name, datetime(event.mtime),"
783786
" coalesce(event.ecomment,event.comment),"
784787
" coalesce(event.euser,event.user),"
785
- " b.uuid"
788
+ " b.uuid, mlink.mperm,"
789
+ " coalesce((SELECT value FROM tagxref"
790
+ " WHERE tagid=%d AND tagtype>0 AND rid=mlink.mid),'trunk')"
786791
" FROM mlink, filename, event, blob a, blob b"
787792
" WHERE filename.fnid=mlink.fnid"
788793
" AND event.objid=mlink.mid"
789794
" AND a.rid=mlink.fid"
790795
" AND b.rid=mlink.mid"
791
- " AND mlink.fid=%d",
792
- rid
796
+ " AND mlink.fid=%d"
797
+ " ORDER BY filename.name, event.mtime",
798
+ TAG_BRANCH, rid
793799
);
800
+ @ <ul>
794801
while( db_step(&q)==SQLITE_ROW ){
795802
const char *zName = db_column_text(&q, 0);
796803
const char *zDate = db_column_text(&q, 1);
797804
const char *zCom = db_column_text(&q, 2);
798805
const char *zUser = db_column_text(&q, 3);
799806
const char *zVers = db_column_text(&q, 4);
800
- if( cnt>0 ){
801
- @ Also file
802
- }else{
803
- @ File
804
- }
805
- if( g.okHistory ){
806
- @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
807
- }else{
808
- @ %h(zName)
809
- }
810
- @ part of check-in
807
+ int mPerm = db_column_int(&q, 5);
808
+ const char *zBr = db_column_text(&q, 6);
809
+ if( !prevName || fossil_strcmp(zName, prevName) ) {
810
+ if( prevName ) {
811
+ @ </ul>
812
+ }
813
+ if( mPerm==PERM_LNK ){
814
+ @ <li>Symbolic link
815
+ }else if( mPerm==PERM_EXE ){
816
+ @ <li>Executable file
817
+ }else{
818
+ @ <li>File
819
+ }
820
+ if( g.okHistory ){
821
+ @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
822
+ }else{
823
+ @ %h(zName)
824
+ }
825
+ @ <ul>
826
+ prevName = fossil_strdup(zName);
827
+ }
828
+ @ <li>
829
+ hyperlink_to_date(zDate,"");
830
+ @ - part of checkin
811831
hyperlink_to_uuid(zVers);
812
- @ - %w(zCom) by
813
- hyperlink_to_user(zUser,zDate," on");
814
- hyperlink_to_date(zDate,".");
832
+ if( zBr && zBr[0] ){
833
+ if( g.okHistory ){
834
+ @ on branch <a href="%s(g.zTop)/timeline?r=%T(zBr)">%h(zBr)</a>
835
+ }else{
836
+ @ on branch %h(zBr)
837
+ }
838
+ }
839
+ @ - %w(zCom) (user:
840
+ hyperlink_to_user(zUser,zDate,"");
841
+ @ )
815842
if( g.okHistory ){
816843
@ <a href="%s(g.zTop)/annotate?checkin=%S(zVers)&filename=%T(zName)">
817844
@ [annotate]</a>
818845
}
819846
cnt++;
820847
if( pDownloadName && blob_size(pDownloadName)==0 ){
821848
blob_append(pDownloadName, zName, -1);
822849
}
823850
}
851
+ @ </ul></ul>
852
+ free(prevName);
824853
db_finalize(&q);
825854
db_prepare(&q,
826855
"SELECT substr(tagname, 6, 10000), datetime(event.mtime),"
827856
" coalesce(event.euser, event.user)"
828857
" FROM tagxref, tag, event"
@@ -988,17 +1017,13 @@
9881017
style_header("Diff");
9891018
style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
9901019
g.zTop, P("v1"), P("v2"));
9911020
@ <h2>Differences From
9921021
@ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
993
- @ <blockquote><p>
994
- object_description(v1, 1, 0);
995
- @ </p></blockquote>
1022
+ object_description(v1, 0, 0);
9961023
@ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
997
- @ <blockquote><p>
998
- object_description(v2, 1, 0);
999
- @ </p></blockquote>
1024
+ object_description(v2, 0, 0);
10001025
@ <hr />
10011026
@ <blockquote><pre>
10021027
@ %h(blob_str(&diff))
10031028
@ </pre></blockquote>
10041029
blob_reset(&diff);
@@ -1107,16 +1132,14 @@
11071132
}
11081133
}
11091134
style_header("Hex Artifact Content");
11101135
zUuid = db_text("?","SELECT uuid FROM blob WHERE rid=%d", rid);
11111136
@ <h2>Artifact %s(zUuid):</h2>
1112
- @ <blockquote><p>
11131137
blob_zero(&downloadName);
11141138
object_description(rid, 0, &downloadName);
11151139
style_submenu_element("Download", "Download",
11161140
"%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
1117
- @ </p></blockquote>
11181141
@ <hr />
11191142
content_get(rid, &content);
11201143
@ <blockquote><pre>
11211144
hexdump(&content);
11221145
@ </pre></blockquote>
@@ -1256,11 +1279,10 @@
12561279
}
12571280
}
12581281
style_header("Artifact Content");
12591282
zUuid = db_text("?", "SELECT uuid FROM blob WHERE rid=%d", rid);
12601283
@ <h2>Artifact %s(zUuid)</h2>
1261
- @ <blockquote><p>
12621284
blob_zero(&downloadName);
12631285
object_description(rid, 0, &downloadName);
12641286
style_submenu_element("Download", "Download",
12651287
"%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
12661288
zMime = mimetype_from_name(blob_str(&downloadName));
@@ -1283,11 +1305,10 @@
12831305
style_submenu_element("Text", "Text",
12841306
"%s/artifact/%s?txt=1", g.zTop, zUuid);
12851307
}
12861308
}
12871309
}
1288
- @ </p></blockquote>
12891310
@ <hr />
12901311
content_get(rid, &content);
12911312
if( renderAsWiki ){
12921313
wiki_convert(&content, 0, 0);
12931314
}else if( renderAsHtml ){
12941315
--- src/info.c
+++ src/info.c
@@ -282,21 +282,22 @@
282 const char *zName, /* Name of the file that has changed */
283 const char *zOld, /* blob.uuid before change. NULL for added files */
284 const char *zNew, /* blob.uuid after change. NULL for deletes */
285 const char *zOldName, /* Prior name. NULL if no name change. */
286 int showDiff, /* Show edit diffs if true */
287 int mperm /* EXE permission for zNew */
288 ){
289 if( !g.okHistory ){
290 if( zNew==0 ){
291 @ <p>Deleted %h(zName)</p>
292 }else if( zOld==0 ){
293 @ <p>Added %h(zName)</p>
294 }else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
295 @ <p>Name change from %h(zOldName) to %h(zName)
296 }else if( fossil_strcmp(zNew, zOld)==0 ){
297 @ <p>Execute permission %s(mperm?"set":"cleared") for %h(zName)</p>
 
298 }else{
299 @ <p>Changes to %h(zName)</p>
300 }
301 if( showDiff ){
302 @ <blockquote><pre>
@@ -312,11 +313,11 @@
312 }else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
313 @ <p>Name change from
314 @ from <a href="%s(g.zTop)/finfo?name=%T(zOldName)">%h(zOldName)</a>
315 @ to <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>.
316 }else{
317 @ <p>Execute permission %s(mperm?"set":"cleared") for
318 @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
319 }
320 }else if( zOld ){
321 @ <p>Deleted <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
322 @ version <a href="%s(g.zTop)/artifact/%s(zOld)">[%S(zOld)]</a>
@@ -776,53 +777,81 @@
776 Stmt q;
777 int cnt = 0;
778 int nWiki = 0;
779 char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
780
 
 
781 db_prepare(&q,
782 "SELECT filename.name, datetime(event.mtime),"
783 " coalesce(event.ecomment,event.comment),"
784 " coalesce(event.euser,event.user),"
785 " b.uuid"
 
 
786 " FROM mlink, filename, event, blob a, blob b"
787 " WHERE filename.fnid=mlink.fnid"
788 " AND event.objid=mlink.mid"
789 " AND a.rid=mlink.fid"
790 " AND b.rid=mlink.mid"
791 " AND mlink.fid=%d",
792 rid
 
793 );
 
794 while( db_step(&q)==SQLITE_ROW ){
795 const char *zName = db_column_text(&q, 0);
796 const char *zDate = db_column_text(&q, 1);
797 const char *zCom = db_column_text(&q, 2);
798 const char *zUser = db_column_text(&q, 3);
799 const char *zVers = db_column_text(&q, 4);
800 if( cnt>0 ){
801 @ Also file
802 }else{
803 @ File
804 }
805 if( g.okHistory ){
806 @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
807 }else{
808 @ %h(zName)
809 }
810 @ part of check-in
 
 
 
 
 
 
 
 
 
 
 
 
 
811 hyperlink_to_uuid(zVers);
812 @ - %w(zCom) by
813 hyperlink_to_user(zUser,zDate," on");
814 hyperlink_to_date(zDate,".");
 
 
 
 
 
 
 
815 if( g.okHistory ){
816 @ <a href="%s(g.zTop)/annotate?checkin=%S(zVers)&filename=%T(zName)">
817 @ [annotate]</a>
818 }
819 cnt++;
820 if( pDownloadName && blob_size(pDownloadName)==0 ){
821 blob_append(pDownloadName, zName, -1);
822 }
823 }
 
 
824 db_finalize(&q);
825 db_prepare(&q,
826 "SELECT substr(tagname, 6, 10000), datetime(event.mtime),"
827 " coalesce(event.euser, event.user)"
828 " FROM tagxref, tag, event"
@@ -988,17 +1017,13 @@
988 style_header("Diff");
989 style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
990 g.zTop, P("v1"), P("v2"));
991 @ <h2>Differences From
992 @ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
993 @ <blockquote><p>
994 object_description(v1, 1, 0);
995 @ </p></blockquote>
996 @ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
997 @ <blockquote><p>
998 object_description(v2, 1, 0);
999 @ </p></blockquote>
1000 @ <hr />
1001 @ <blockquote><pre>
1002 @ %h(blob_str(&diff))
1003 @ </pre></blockquote>
1004 blob_reset(&diff);
@@ -1107,16 +1132,14 @@
1107 }
1108 }
1109 style_header("Hex Artifact Content");
1110 zUuid = db_text("?","SELECT uuid FROM blob WHERE rid=%d", rid);
1111 @ <h2>Artifact %s(zUuid):</h2>
1112 @ <blockquote><p>
1113 blob_zero(&downloadName);
1114 object_description(rid, 0, &downloadName);
1115 style_submenu_element("Download", "Download",
1116 "%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
1117 @ </p></blockquote>
1118 @ <hr />
1119 content_get(rid, &content);
1120 @ <blockquote><pre>
1121 hexdump(&content);
1122 @ </pre></blockquote>
@@ -1256,11 +1279,10 @@
1256 }
1257 }
1258 style_header("Artifact Content");
1259 zUuid = db_text("?", "SELECT uuid FROM blob WHERE rid=%d", rid);
1260 @ <h2>Artifact %s(zUuid)</h2>
1261 @ <blockquote><p>
1262 blob_zero(&downloadName);
1263 object_description(rid, 0, &downloadName);
1264 style_submenu_element("Download", "Download",
1265 "%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
1266 zMime = mimetype_from_name(blob_str(&downloadName));
@@ -1283,11 +1305,10 @@
1283 style_submenu_element("Text", "Text",
1284 "%s/artifact/%s?txt=1", g.zTop, zUuid);
1285 }
1286 }
1287 }
1288 @ </p></blockquote>
1289 @ <hr />
1290 content_get(rid, &content);
1291 if( renderAsWiki ){
1292 wiki_convert(&content, 0, 0);
1293 }else if( renderAsHtml ){
1294
--- src/info.c
+++ src/info.c
@@ -282,21 +282,22 @@
282 const char *zName, /* Name of the file that has changed */
283 const char *zOld, /* blob.uuid before change. NULL for added files */
284 const char *zNew, /* blob.uuid after change. NULL for deletes */
285 const char *zOldName, /* Prior name. NULL if no name change. */
286 int showDiff, /* Show edit diffs if true */
287 int mperm /* executable or symlink permission for zNew */
288 ){
289 if( !g.okHistory ){
290 if( zNew==0 ){
291 @ <p>Deleted %h(zName)</p>
292 }else if( zOld==0 ){
293 @ <p>Added %h(zName)</p>
294 }else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
295 @ <p>Name change from %h(zOldName) to %h(zName)
296 }else if( fossil_strcmp(zNew, zOld)==0 ){
297 @ <p>Execute permission %s(( mperm==PERM_EXE )?"set":"cleared")
298 @ for %h(zName)</p>
299 }else{
300 @ <p>Changes to %h(zName)</p>
301 }
302 if( showDiff ){
303 @ <blockquote><pre>
@@ -312,11 +313,11 @@
313 }else if( zOldName!=0 && fossil_strcmp(zName,zOldName)!=0 ){
314 @ <p>Name change from
315 @ from <a href="%s(g.zTop)/finfo?name=%T(zOldName)">%h(zOldName)</a>
316 @ to <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>.
317 }else{
318 @ <p>Execute permission %s(( mperm==PERM_EXE )?"set":"cleared") for
319 @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
320 }
321 }else if( zOld ){
322 @ <p>Deleted <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
323 @ version <a href="%s(g.zTop)/artifact/%s(zOld)">[%S(zOld)]</a>
@@ -776,53 +777,81 @@
777 Stmt q;
778 int cnt = 0;
779 int nWiki = 0;
780 char *zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
781
782 char *prevName = 0;
783
784 db_prepare(&q,
785 "SELECT filename.name, datetime(event.mtime),"
786 " coalesce(event.ecomment,event.comment),"
787 " coalesce(event.euser,event.user),"
788 " b.uuid, mlink.mperm,"
789 " coalesce((SELECT value FROM tagxref"
790 " WHERE tagid=%d AND tagtype>0 AND rid=mlink.mid),'trunk')"
791 " FROM mlink, filename, event, blob a, blob b"
792 " WHERE filename.fnid=mlink.fnid"
793 " AND event.objid=mlink.mid"
794 " AND a.rid=mlink.fid"
795 " AND b.rid=mlink.mid"
796 " AND mlink.fid=%d"
797 " ORDER BY filename.name, event.mtime",
798 TAG_BRANCH, rid
799 );
800 @ <ul>
801 while( db_step(&q)==SQLITE_ROW ){
802 const char *zName = db_column_text(&q, 0);
803 const char *zDate = db_column_text(&q, 1);
804 const char *zCom = db_column_text(&q, 2);
805 const char *zUser = db_column_text(&q, 3);
806 const char *zVers = db_column_text(&q, 4);
807 int mPerm = db_column_int(&q, 5);
808 const char *zBr = db_column_text(&q, 6);
809 if( !prevName || fossil_strcmp(zName, prevName) ) {
810 if( prevName ) {
811 @ </ul>
812 }
813 if( mPerm==PERM_LNK ){
814 @ <li>Symbolic link
815 }else if( mPerm==PERM_EXE ){
816 @ <li>Executable file
817 }else{
818 @ <li>File
819 }
820 if( g.okHistory ){
821 @ <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
822 }else{
823 @ %h(zName)
824 }
825 @ <ul>
826 prevName = fossil_strdup(zName);
827 }
828 @ <li>
829 hyperlink_to_date(zDate,"");
830 @ - part of checkin
831 hyperlink_to_uuid(zVers);
832 if( zBr && zBr[0] ){
833 if( g.okHistory ){
834 @ on branch <a href="%s(g.zTop)/timeline?r=%T(zBr)">%h(zBr)</a>
835 }else{
836 @ on branch %h(zBr)
837 }
838 }
839 @ - %w(zCom) (user:
840 hyperlink_to_user(zUser,zDate,"");
841 @ )
842 if( g.okHistory ){
843 @ <a href="%s(g.zTop)/annotate?checkin=%S(zVers)&filename=%T(zName)">
844 @ [annotate]</a>
845 }
846 cnt++;
847 if( pDownloadName && blob_size(pDownloadName)==0 ){
848 blob_append(pDownloadName, zName, -1);
849 }
850 }
851 @ </ul></ul>
852 free(prevName);
853 db_finalize(&q);
854 db_prepare(&q,
855 "SELECT substr(tagname, 6, 10000), datetime(event.mtime),"
856 " coalesce(event.euser, event.user)"
857 " FROM tagxref, tag, event"
@@ -988,17 +1017,13 @@
1017 style_header("Diff");
1018 style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
1019 g.zTop, P("v1"), P("v2"));
1020 @ <h2>Differences From
1021 @ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
1022 object_description(v1, 0, 0);
 
 
1023 @ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
1024 object_description(v2, 0, 0);
 
 
1025 @ <hr />
1026 @ <blockquote><pre>
1027 @ %h(blob_str(&diff))
1028 @ </pre></blockquote>
1029 blob_reset(&diff);
@@ -1107,16 +1132,14 @@
1132 }
1133 }
1134 style_header("Hex Artifact Content");
1135 zUuid = db_text("?","SELECT uuid FROM blob WHERE rid=%d", rid);
1136 @ <h2>Artifact %s(zUuid):</h2>
 
1137 blob_zero(&downloadName);
1138 object_description(rid, 0, &downloadName);
1139 style_submenu_element("Download", "Download",
1140 "%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
 
1141 @ <hr />
1142 content_get(rid, &content);
1143 @ <blockquote><pre>
1144 hexdump(&content);
1145 @ </pre></blockquote>
@@ -1256,11 +1279,10 @@
1279 }
1280 }
1281 style_header("Artifact Content");
1282 zUuid = db_text("?", "SELECT uuid FROM blob WHERE rid=%d", rid);
1283 @ <h2>Artifact %s(zUuid)</h2>
 
1284 blob_zero(&downloadName);
1285 object_description(rid, 0, &downloadName);
1286 style_submenu_element("Download", "Download",
1287 "%s/raw/%T?name=%s", g.zTop, blob_str(&downloadName), zUuid);
1288 zMime = mimetype_from_name(blob_str(&downloadName));
@@ -1283,11 +1305,10 @@
1305 style_submenu_element("Text", "Text",
1306 "%s/artifact/%s?txt=1", g.zTop, zUuid);
1307 }
1308 }
1309 }
 
1310 @ <hr />
1311 content_get(rid, &content);
1312 if( renderAsWiki ){
1313 wiki_convert(&content, 0, 0);
1314 }else if( renderAsHtml ){
1315
+2
--- src/main.c
+++ src/main.c
@@ -156,10 +156,12 @@
156156
const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
157157
char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
158158
const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
159159
const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
160160
int anAuxCols[MX_AUX]; /* Number of columns for option() values */
161
+
162
+ int allowSymlinks; /* Cached "allow-symlinks" option */
161163
};
162164
163165
/*
164166
** Macro for debugging:
165167
*/
166168
--- src/main.c
+++ src/main.c
@@ -156,10 +156,12 @@
156 const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
157 char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
158 const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
159 const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
160 int anAuxCols[MX_AUX]; /* Number of columns for option() values */
 
 
161 };
162
163 /*
164 ** Macro for debugging:
165 */
166
--- src/main.c
+++ src/main.c
@@ -156,10 +156,12 @@
156 const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
157 char *azAuxParam[MX_AUX]; /* Param of each aux() or option() value */
158 const char *azAuxVal[MX_AUX]; /* Value of each aux() or option() value */
159 const char **azAuxOpt[MX_AUX]; /* Options of each option() value */
160 int anAuxCols[MX_AUX]; /* Number of columns for option() values */
161
162 int allowSymlinks; /* Cached "allow-symlinks" option */
163 };
164
165 /*
166 ** Macro for debugging:
167 */
168
+14 -4
--- src/manifest.c
+++ src/manifest.c
@@ -35,10 +35,17 @@
3535
#define CFTYPE_WIKI 4
3636
#define CFTYPE_TICKET 5
3737
#define CFTYPE_ATTACHMENT 6
3838
#define CFTYPE_EVENT 7
3939
40
+/*
41
+** File permissions used by Fossil internally.
42
+*/
43
+#define PERM_REG 0 /* regular file */
44
+#define PERM_EXE 1 /* executable */
45
+#define PERM_LNK 2 /* symlink */
46
+
4047
/*
4148
** A single F-card within a manifest
4249
*/
4350
struct ManifestFile {
4451
char *zName; /* Name of a file */
@@ -1085,13 +1092,16 @@
10851092
/*
10861093
** Compute an appropriate mlink.mperm integer for the permission string
10871094
** of a file.
10881095
*/
10891096
int manifest_file_mperm(ManifestFile *pFile){
1090
- int mperm = 0;
1091
- if( pFile && pFile->zPerm && strstr(pFile->zPerm,"x")!=0 ){
1092
- mperm = 1;
1097
+ int mperm = PERM_REG;
1098
+ if( pFile && pFile->zPerm){
1099
+ if( strstr(pFile->zPerm,"x")!=0 )
1100
+ mperm = PERM_EXE;
1101
+ else if( strstr(pFile->zPerm,"l")!=0 )
1102
+ mperm = PERM_LNK;
10931103
}
10941104
return mperm;
10951105
}
10961106
10971107
/*
@@ -1103,11 +1113,11 @@
11031113
const char *zFromUuid, /* UUID for the mlink.pid. "" to add file */
11041114
const char *zToUuid, /* UUID for the mlink.fid. "" to delele */
11051115
const char *zFilename, /* Filename */
11061116
const char *zPrior, /* Previous filename. NULL if unchanged */
11071117
int isPublic, /* True if mid is not a private manifest */
1108
- int mperm /* 1: exec */
1118
+ int mperm /* 1: exec, 2: symlink */
11091119
){
11101120
int fnid, pfnid, pid, fid;
11111121
static Stmt s1;
11121122
11131123
fnid = filename_to_fnid(zFilename);
11141124
--- src/manifest.c
+++ src/manifest.c
@@ -35,10 +35,17 @@
35 #define CFTYPE_WIKI 4
36 #define CFTYPE_TICKET 5
37 #define CFTYPE_ATTACHMENT 6
38 #define CFTYPE_EVENT 7
39
 
 
 
 
 
 
 
40 /*
41 ** A single F-card within a manifest
42 */
43 struct ManifestFile {
44 char *zName; /* Name of a file */
@@ -1085,13 +1092,16 @@
1085 /*
1086 ** Compute an appropriate mlink.mperm integer for the permission string
1087 ** of a file.
1088 */
1089 int manifest_file_mperm(ManifestFile *pFile){
1090 int mperm = 0;
1091 if( pFile && pFile->zPerm && strstr(pFile->zPerm,"x")!=0 ){
1092 mperm = 1;
 
 
 
1093 }
1094 return mperm;
1095 }
1096
1097 /*
@@ -1103,11 +1113,11 @@
1103 const char *zFromUuid, /* UUID for the mlink.pid. "" to add file */
1104 const char *zToUuid, /* UUID for the mlink.fid. "" to delele */
1105 const char *zFilename, /* Filename */
1106 const char *zPrior, /* Previous filename. NULL if unchanged */
1107 int isPublic, /* True if mid is not a private manifest */
1108 int mperm /* 1: exec */
1109 ){
1110 int fnid, pfnid, pid, fid;
1111 static Stmt s1;
1112
1113 fnid = filename_to_fnid(zFilename);
1114
--- src/manifest.c
+++ src/manifest.c
@@ -35,10 +35,17 @@
35 #define CFTYPE_WIKI 4
36 #define CFTYPE_TICKET 5
37 #define CFTYPE_ATTACHMENT 6
38 #define CFTYPE_EVENT 7
39
40 /*
41 ** File permissions used by Fossil internally.
42 */
43 #define PERM_REG 0 /* regular file */
44 #define PERM_EXE 1 /* executable */
45 #define PERM_LNK 2 /* symlink */
46
47 /*
48 ** A single F-card within a manifest
49 */
50 struct ManifestFile {
51 char *zName; /* Name of a file */
@@ -1085,13 +1092,16 @@
1092 /*
1093 ** Compute an appropriate mlink.mperm integer for the permission string
1094 ** of a file.
1095 */
1096 int manifest_file_mperm(ManifestFile *pFile){
1097 int mperm = PERM_REG;
1098 if( pFile && pFile->zPerm){
1099 if( strstr(pFile->zPerm,"x")!=0 )
1100 mperm = PERM_EXE;
1101 else if( strstr(pFile->zPerm,"l")!=0 )
1102 mperm = PERM_LNK;
1103 }
1104 return mperm;
1105 }
1106
1107 /*
@@ -1103,11 +1113,11 @@
1113 const char *zFromUuid, /* UUID for the mlink.pid. "" to add file */
1114 const char *zToUuid, /* UUID for the mlink.fid. "" to delele */
1115 const char *zFilename, /* Filename */
1116 const char *zPrior, /* Previous filename. NULL if unchanged */
1117 int isPublic, /* True if mid is not a private manifest */
1118 int mperm /* 1: exec, 2: symlink */
1119 ){
1120 int fnid, pfnid, pid, fid;
1121 static Stmt s1;
1122
1123 fnid = filename_to_fnid(zFilename);
1124
+72 -44
--- src/merge.c
+++ src/merge.c
@@ -148,10 +148,19 @@
148148
vfile_check_signature(vid, 1, 0);
149149
db_begin_transaction();
150150
if( !nochangeFlag ) undo_begin();
151151
load_vfile_from_rid(mid);
152152
load_vfile_from_rid(pid);
153
+ if( debugFlag ){
154
+ char *z;
155
+ z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", pid);
156
+ fossil_print("P=%d %z\n", pid, z);
157
+ z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
158
+ fossil_print("M=%d %z\n", mid, z);
159
+ z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
160
+ fossil_print("V=%d %z\n", vid, z);
161
+ }
153162
154163
/*
155164
** The vfile.pathname field is used to match files against each other. The
156165
** FV table contains one row for each each unique filename in
157166
** in the current checkout, the pivot, and the version being merged.
@@ -167,11 +176,13 @@
167176
" ridv INTEGER," /* Record ID for current version */
168177
" ridp INTEGER," /* Record ID for pivot */
169178
" ridm INTEGER," /* Record ID for merge */
170179
" isexe BOOLEAN," /* Execute permission enabled */
171180
" fnp TEXT," /* The filename in the pivot */
172
- " fnm TEXT" /* the filename in the merged version */
181
+ " fnm TEXT," /* the filename in the merged version */
182
+ " islinkv BOOLEAN," /* True if current version is a symlink */
183
+ " islinkm BOOLEAN" /* True if merged version in is a symlink */
173184
");",
174185
caseSensitive ? "binary" : "nocase"
175186
);
176187
177188
/* Add files found in V
@@ -185,19 +196,19 @@
185196
);
186197
187198
/*
188199
** Compute name changes from P->V
189200
*/
190
- find_filename_changes(vid, pid, &nChng, &aChng);
201
+ find_filename_changes(pid, vid, 0, &nChng, &aChng, debugFlag ? "P->V" : 0);
191202
if( nChng ){
192203
for(i=0; i<nChng; i++){
193204
char *z;
194
- z = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
205
+ z = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
195206
db_multi_exec(
196207
"UPDATE fv SET fnp=%Q, fnm=%Q"
197208
" WHERE fn=(SELECT name FROM filename WHERE fnid=%d)",
198
- z, z, aChng[i*2]
209
+ z, z, aChng[i*2+1]
199210
);
200211
free(z);
201212
}
202213
fossil_free(aChng);
203214
db_multi_exec("UPDATE fv SET fnm=fnp WHERE fnp!=fn");
@@ -215,11 +226,11 @@
215226
);
216227
217228
/*
218229
** Compute name changes from P->M
219230
*/
220
- find_filename_changes(pid, mid, &nChng, &aChng);
231
+ find_filename_changes(pid, mid, 0, &nChng, &aChng, debugFlag ? "P->M" : 0);
221232
if( nChng ){
222233
if( nChng>4 ) db_multi_exec("CREATE INDEX fv_fnp ON fv(fnp)");
223234
for(i=0; i<nChng; i++){
224235
db_multi_exec(
225236
"UPDATE fv SET fnm=(SELECT name FROM filename WHERE fnid=%d)"
@@ -248,26 +259,34 @@
248259
db_multi_exec(
249260
"UPDATE fv SET"
250261
" idp=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnp),0),"
251262
" ridp=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnp),0),"
252263
" idm=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnm),0),"
253
- " ridm=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnm),0)",
254
- pid, pid, mid, mid
264
+ " ridm=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnm),0),"
265
+ " islinkv=coalesce((SELECT islink FROM vfile"
266
+ " WHERE vid=%d AND pathname=fnm),0),"
267
+ " islinkm=coalesce((SELECT islink FROM vfile"
268
+ " WHERE vid=%d AND pathname=fnm),0)",
269
+ pid, pid, mid, mid, vid, mid
255270
);
256271
257272
if( debugFlag ){
258273
db_prepare(&q,
259
- "SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, isexe FROM fv"
274
+ "SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, "
275
+ " isexe, islinkv, islinkm FROM fv"
260276
);
261277
while( db_step(&q)==SQLITE_ROW ){
262
- fossil_print("%3d: ridv=%-4d ridp=%-4d ridm=%-4d chnged=%d isexe=%d\n",
278
+ fossil_print("%3d: ridv=%-4d ridp=%-4d ridm=%-4d chnged=%d isexe=%d "
279
+ " islinkv=%d islinkm=%d\n",
263280
db_column_int(&q, 0),
264281
db_column_int(&q, 5),
265282
db_column_int(&q, 6),
266283
db_column_int(&q, 7),
267284
db_column_int(&q, 4),
268
- db_column_int(&q, 8));
285
+ db_column_int(&q, 8),
286
+ db_column_int(&q, 9),
287
+ db_column_int(&q, 10));
269288
fossil_print(" fn = [%s]\n", db_column_text(&q, 1));
270289
fossil_print(" fnp = [%s]\n", db_column_text(&q, 2));
271290
fossil_print(" fnm = [%s]\n", db_column_text(&q, 3));
272291
}
273292
db_finalize(&q);
@@ -301,12 +320,12 @@
301320
int idm = db_column_int(&q, 0);
302321
int rowid = db_column_int(&q, 1);
303322
int idv;
304323
const char *zName;
305324
db_multi_exec(
306
- "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,pathname)"
307
- " SELECT %d,3,0,rid,mrid,isexe,pathname FROM vfile WHERE id=%d",
325
+ "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,islink,pathname)"
326
+ " SELECT %d,3,0,rid,mrid,isexe,islink,pathname FROM vfile WHERE id=%d",
308327
vid, idm
309328
);
310329
idv = db_last_insert_rowid();
311330
db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
312331
zName = db_column_text(&q, 2);
@@ -315,30 +334,32 @@
315334
undo_save(zName);
316335
vfile_to_disk(0, idm, 0, 0);
317336
}
318337
}
319338
db_finalize(&q);
320
-
339
+
321340
/*
322
- ** Find files that have changed from P->M but not P->V.
341
+ ** Find files that have changed from P->M but not P->V.
323342
** Copy the M content over into V.
324343
*/
325344
db_prepare(&q,
326
- "SELECT idv, ridm, fn FROM fv"
345
+ "SELECT idv, ridm, fn, islinkm FROM fv"
327346
" WHERE idp>0 AND idv>0 AND idm>0"
328347
" AND ridm!=ridp AND ridv=ridp AND NOT chnged"
329348
);
330349
while( db_step(&q)==SQLITE_ROW ){
331350
int idv = db_column_int(&q, 0);
332351
int ridm = db_column_int(&q, 1);
333352
const char *zName = db_column_text(&q, 2);
353
+ int islinkm = db_column_int(&q, 3);
334354
/* Copy content from idm over into idv. Overwrite idv. */
335355
fossil_print("UPDATE %s\n", zName);
336356
if( !nochangeFlag ){
337357
undo_save(zName);
338358
db_multi_exec(
339
- "UPDATE vfile SET mtime=0, mrid=%d, chnged=2 WHERE id=%d", ridm, idv
359
+ "UPDATE vfile SET mtime=0, mrid=%d, chnged=2, islink=%d "
360
+ " WHERE id=%d", ridm, islinkm, idv
340361
);
341362
vfile_to_disk(0, idv, 0, 0);
342363
}
343364
}
344365
db_finalize(&q);
@@ -345,11 +366,11 @@
345366
346367
/*
347368
** Do a three-way merge on files that have changes on both P->M and P->V.
348369
*/
349370
db_prepare(&q,
350
- "SELECT ridm, idv, ridp, ridv, %s, fn, isexe FROM fv"
371
+ "SELECT ridm, idv, ridp, ridv, %s, fn, isexe, islinkv, islinkm FROM fv"
351372
" WHERE idp>0 AND idv>0 AND idm>0"
352373
" AND ridm!=ridp AND (ridv!=ridp OR chnged)",
353374
glob_expr("fv.fn", zBinGlob)
354375
);
355376
while( db_step(&q)==SQLITE_ROW ){
@@ -358,10 +379,12 @@
358379
int ridp = db_column_int(&q, 2);
359380
int ridv = db_column_int(&q, 3);
360381
int isBinary = db_column_int(&q, 4);
361382
const char *zName = db_column_text(&q, 5);
362383
int isExe = db_column_int(&q, 6);
384
+ int islinkv = db_column_int(&q, 7);
385
+ int islinkm = db_column_int(&q, 8);
363386
int rc;
364387
char *zFullPath;
365388
Blob m, p, r;
366389
/* Do a 3-way merge of idp->idm into idp->idv. The results go into idv. */
367390
if( detailFlag ){
@@ -368,37 +391,42 @@
368391
fossil_print("MERGE %s (pivot=%d v1=%d v2=%d)\n",
369392
zName, ridp, ridm, ridv);
370393
}else{
371394
fossil_print("MERGE %s\n", zName);
372395
}
373
- undo_save(zName);
374
- zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
375
- content_get(ridp, &p);
376
- content_get(ridm, &m);
377
- if( isBinary ){
378
- rc = -1;
379
- blob_zero(&r);
380
- }else{
381
- rc = merge_3way(&p, zFullPath, &m, &r);
382
- }
383
- if( rc>=0 ){
384
- if( !nochangeFlag ){
385
- blob_write_to_file(&r, zFullPath);
386
- file_setexe(zFullPath, isExe);
387
- }
388
- db_multi_exec("UPDATE vfile SET mtime=0 WHERE id=%d", idv);
389
- if( rc>0 ){
390
- fossil_print("***** %d merge conflicts in %s\n", rc, zName);
391
- nConflict++;
392
- }
393
- }else{
394
- fossil_print("***** Cannot merge binary file %s\n", zName);
395
- nConflict++;
396
- }
397
- blob_reset(&p);
398
- blob_reset(&m);
399
- blob_reset(&r);
396
+ if( islinkv || islinkm /* || file_islink(zFullPath) */ ){
397
+ fossil_print("***** Cannot merge symlink %s\n", zName);
398
+ nConflict++;
399
+ }else{
400
+ undo_save(zName);
401
+ zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
402
+ content_get(ridp, &p);
403
+ content_get(ridm, &m);
404
+ if( isBinary ){
405
+ rc = -1;
406
+ blob_zero(&r);
407
+ }else{
408
+ rc = merge_3way(&p, zFullPath, &m, &r);
409
+ }
410
+ if( rc>=0 ){
411
+ if( !nochangeFlag ){
412
+ blob_write_to_file(&r, zFullPath);
413
+ file_setexe(zFullPath, isExe);
414
+ }
415
+ db_multi_exec("UPDATE vfile SET mtime=0 WHERE id=%d", idv);
416
+ if( rc>0 ){
417
+ fossil_print("***** %d merge conflicts in %s\n", rc, zName);
418
+ nConflict++;
419
+ }
420
+ }else{
421
+ fossil_print("***** Cannot merge binary file %s\n", zName);
422
+ nConflict++;
423
+ }
424
+ blob_reset(&p);
425
+ blob_reset(&m);
426
+ blob_reset(&r);
427
+ }
400428
db_multi_exec("INSERT OR IGNORE INTO vmerge(id,merge) VALUES(%d,%d)",
401429
idv,ridm);
402430
}
403431
db_finalize(&q);
404432
405433
--- src/merge.c
+++ src/merge.c
@@ -148,10 +148,19 @@
148 vfile_check_signature(vid, 1, 0);
149 db_begin_transaction();
150 if( !nochangeFlag ) undo_begin();
151 load_vfile_from_rid(mid);
152 load_vfile_from_rid(pid);
 
 
 
 
 
 
 
 
 
153
154 /*
155 ** The vfile.pathname field is used to match files against each other. The
156 ** FV table contains one row for each each unique filename in
157 ** in the current checkout, the pivot, and the version being merged.
@@ -167,11 +176,13 @@
167 " ridv INTEGER," /* Record ID for current version */
168 " ridp INTEGER," /* Record ID for pivot */
169 " ridm INTEGER," /* Record ID for merge */
170 " isexe BOOLEAN," /* Execute permission enabled */
171 " fnp TEXT," /* The filename in the pivot */
172 " fnm TEXT" /* the filename in the merged version */
 
 
173 ");",
174 caseSensitive ? "binary" : "nocase"
175 );
176
177 /* Add files found in V
@@ -185,19 +196,19 @@
185 );
186
187 /*
188 ** Compute name changes from P->V
189 */
190 find_filename_changes(vid, pid, &nChng, &aChng);
191 if( nChng ){
192 for(i=0; i<nChng; i++){
193 char *z;
194 z = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
195 db_multi_exec(
196 "UPDATE fv SET fnp=%Q, fnm=%Q"
197 " WHERE fn=(SELECT name FROM filename WHERE fnid=%d)",
198 z, z, aChng[i*2]
199 );
200 free(z);
201 }
202 fossil_free(aChng);
203 db_multi_exec("UPDATE fv SET fnm=fnp WHERE fnp!=fn");
@@ -215,11 +226,11 @@
215 );
216
217 /*
218 ** Compute name changes from P->M
219 */
220 find_filename_changes(pid, mid, &nChng, &aChng);
221 if( nChng ){
222 if( nChng>4 ) db_multi_exec("CREATE INDEX fv_fnp ON fv(fnp)");
223 for(i=0; i<nChng; i++){
224 db_multi_exec(
225 "UPDATE fv SET fnm=(SELECT name FROM filename WHERE fnid=%d)"
@@ -248,26 +259,34 @@
248 db_multi_exec(
249 "UPDATE fv SET"
250 " idp=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnp),0),"
251 " ridp=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnp),0),"
252 " idm=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnm),0),"
253 " ridm=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnm),0)",
254 pid, pid, mid, mid
 
 
 
 
255 );
256
257 if( debugFlag ){
258 db_prepare(&q,
259 "SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, isexe FROM fv"
 
260 );
261 while( db_step(&q)==SQLITE_ROW ){
262 fossil_print("%3d: ridv=%-4d ridp=%-4d ridm=%-4d chnged=%d isexe=%d\n",
 
263 db_column_int(&q, 0),
264 db_column_int(&q, 5),
265 db_column_int(&q, 6),
266 db_column_int(&q, 7),
267 db_column_int(&q, 4),
268 db_column_int(&q, 8));
 
 
269 fossil_print(" fn = [%s]\n", db_column_text(&q, 1));
270 fossil_print(" fnp = [%s]\n", db_column_text(&q, 2));
271 fossil_print(" fnm = [%s]\n", db_column_text(&q, 3));
272 }
273 db_finalize(&q);
@@ -301,12 +320,12 @@
301 int idm = db_column_int(&q, 0);
302 int rowid = db_column_int(&q, 1);
303 int idv;
304 const char *zName;
305 db_multi_exec(
306 "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,pathname)"
307 " SELECT %d,3,0,rid,mrid,isexe,pathname FROM vfile WHERE id=%d",
308 vid, idm
309 );
310 idv = db_last_insert_rowid();
311 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
312 zName = db_column_text(&q, 2);
@@ -315,30 +334,32 @@
315 undo_save(zName);
316 vfile_to_disk(0, idm, 0, 0);
317 }
318 }
319 db_finalize(&q);
320
321 /*
322 ** Find files that have changed from P->M but not P->V.
323 ** Copy the M content over into V.
324 */
325 db_prepare(&q,
326 "SELECT idv, ridm, fn FROM fv"
327 " WHERE idp>0 AND idv>0 AND idm>0"
328 " AND ridm!=ridp AND ridv=ridp AND NOT chnged"
329 );
330 while( db_step(&q)==SQLITE_ROW ){
331 int idv = db_column_int(&q, 0);
332 int ridm = db_column_int(&q, 1);
333 const char *zName = db_column_text(&q, 2);
 
334 /* Copy content from idm over into idv. Overwrite idv. */
335 fossil_print("UPDATE %s\n", zName);
336 if( !nochangeFlag ){
337 undo_save(zName);
338 db_multi_exec(
339 "UPDATE vfile SET mtime=0, mrid=%d, chnged=2 WHERE id=%d", ridm, idv
 
340 );
341 vfile_to_disk(0, idv, 0, 0);
342 }
343 }
344 db_finalize(&q);
@@ -345,11 +366,11 @@
345
346 /*
347 ** Do a three-way merge on files that have changes on both P->M and P->V.
348 */
349 db_prepare(&q,
350 "SELECT ridm, idv, ridp, ridv, %s, fn, isexe FROM fv"
351 " WHERE idp>0 AND idv>0 AND idm>0"
352 " AND ridm!=ridp AND (ridv!=ridp OR chnged)",
353 glob_expr("fv.fn", zBinGlob)
354 );
355 while( db_step(&q)==SQLITE_ROW ){
@@ -358,10 +379,12 @@
358 int ridp = db_column_int(&q, 2);
359 int ridv = db_column_int(&q, 3);
360 int isBinary = db_column_int(&q, 4);
361 const char *zName = db_column_text(&q, 5);
362 int isExe = db_column_int(&q, 6);
 
 
363 int rc;
364 char *zFullPath;
365 Blob m, p, r;
366 /* Do a 3-way merge of idp->idm into idp->idv. The results go into idv. */
367 if( detailFlag ){
@@ -368,37 +391,42 @@
368 fossil_print("MERGE %s (pivot=%d v1=%d v2=%d)\n",
369 zName, ridp, ridm, ridv);
370 }else{
371 fossil_print("MERGE %s\n", zName);
372 }
373 undo_save(zName);
374 zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
375 content_get(ridp, &p);
376 content_get(ridm, &m);
377 if( isBinary ){
378 rc = -1;
379 blob_zero(&r);
380 }else{
381 rc = merge_3way(&p, zFullPath, &m, &r);
382 }
383 if( rc>=0 ){
384 if( !nochangeFlag ){
385 blob_write_to_file(&r, zFullPath);
386 file_setexe(zFullPath, isExe);
387 }
388 db_multi_exec("UPDATE vfile SET mtime=0 WHERE id=%d", idv);
389 if( rc>0 ){
390 fossil_print("***** %d merge conflicts in %s\n", rc, zName);
391 nConflict++;
392 }
393 }else{
394 fossil_print("***** Cannot merge binary file %s\n", zName);
395 nConflict++;
396 }
397 blob_reset(&p);
398 blob_reset(&m);
399 blob_reset(&r);
 
 
 
 
 
400 db_multi_exec("INSERT OR IGNORE INTO vmerge(id,merge) VALUES(%d,%d)",
401 idv,ridm);
402 }
403 db_finalize(&q);
404
405
--- src/merge.c
+++ src/merge.c
@@ -148,10 +148,19 @@
148 vfile_check_signature(vid, 1, 0);
149 db_begin_transaction();
150 if( !nochangeFlag ) undo_begin();
151 load_vfile_from_rid(mid);
152 load_vfile_from_rid(pid);
153 if( debugFlag ){
154 char *z;
155 z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", pid);
156 fossil_print("P=%d %z\n", pid, z);
157 z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
158 fossil_print("M=%d %z\n", mid, z);
159 z = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
160 fossil_print("V=%d %z\n", vid, z);
161 }
162
163 /*
164 ** The vfile.pathname field is used to match files against each other. The
165 ** FV table contains one row for each each unique filename in
166 ** in the current checkout, the pivot, and the version being merged.
@@ -167,11 +176,13 @@
176 " ridv INTEGER," /* Record ID for current version */
177 " ridp INTEGER," /* Record ID for pivot */
178 " ridm INTEGER," /* Record ID for merge */
179 " isexe BOOLEAN," /* Execute permission enabled */
180 " fnp TEXT," /* The filename in the pivot */
181 " fnm TEXT," /* the filename in the merged version */
182 " islinkv BOOLEAN," /* True if current version is a symlink */
183 " islinkm BOOLEAN" /* True if merged version in is a symlink */
184 ");",
185 caseSensitive ? "binary" : "nocase"
186 );
187
188 /* Add files found in V
@@ -185,19 +196,19 @@
196 );
197
198 /*
199 ** Compute name changes from P->V
200 */
201 find_filename_changes(pid, vid, 0, &nChng, &aChng, debugFlag ? "P->V" : 0);
202 if( nChng ){
203 for(i=0; i<nChng; i++){
204 char *z;
205 z = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
206 db_multi_exec(
207 "UPDATE fv SET fnp=%Q, fnm=%Q"
208 " WHERE fn=(SELECT name FROM filename WHERE fnid=%d)",
209 z, z, aChng[i*2+1]
210 );
211 free(z);
212 }
213 fossil_free(aChng);
214 db_multi_exec("UPDATE fv SET fnm=fnp WHERE fnp!=fn");
@@ -215,11 +226,11 @@
226 );
227
228 /*
229 ** Compute name changes from P->M
230 */
231 find_filename_changes(pid, mid, 0, &nChng, &aChng, debugFlag ? "P->M" : 0);
232 if( nChng ){
233 if( nChng>4 ) db_multi_exec("CREATE INDEX fv_fnp ON fv(fnp)");
234 for(i=0; i<nChng; i++){
235 db_multi_exec(
236 "UPDATE fv SET fnm=(SELECT name FROM filename WHERE fnid=%d)"
@@ -248,26 +259,34 @@
259 db_multi_exec(
260 "UPDATE fv SET"
261 " idp=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnp),0),"
262 " ridp=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnp),0),"
263 " idm=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnm),0),"
264 " ridm=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnm),0),"
265 " islinkv=coalesce((SELECT islink FROM vfile"
266 " WHERE vid=%d AND pathname=fnm),0),"
267 " islinkm=coalesce((SELECT islink FROM vfile"
268 " WHERE vid=%d AND pathname=fnm),0)",
269 pid, pid, mid, mid, vid, mid
270 );
271
272 if( debugFlag ){
273 db_prepare(&q,
274 "SELECT rowid, fn, fnp, fnm, chnged, ridv, ridp, ridm, "
275 " isexe, islinkv, islinkm FROM fv"
276 );
277 while( db_step(&q)==SQLITE_ROW ){
278 fossil_print("%3d: ridv=%-4d ridp=%-4d ridm=%-4d chnged=%d isexe=%d "
279 " islinkv=%d islinkm=%d\n",
280 db_column_int(&q, 0),
281 db_column_int(&q, 5),
282 db_column_int(&q, 6),
283 db_column_int(&q, 7),
284 db_column_int(&q, 4),
285 db_column_int(&q, 8),
286 db_column_int(&q, 9),
287 db_column_int(&q, 10));
288 fossil_print(" fn = [%s]\n", db_column_text(&q, 1));
289 fossil_print(" fnp = [%s]\n", db_column_text(&q, 2));
290 fossil_print(" fnm = [%s]\n", db_column_text(&q, 3));
291 }
292 db_finalize(&q);
@@ -301,12 +320,12 @@
320 int idm = db_column_int(&q, 0);
321 int rowid = db_column_int(&q, 1);
322 int idv;
323 const char *zName;
324 db_multi_exec(
325 "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,islink,pathname)"
326 " SELECT %d,3,0,rid,mrid,isexe,islink,pathname FROM vfile WHERE id=%d",
327 vid, idm
328 );
329 idv = db_last_insert_rowid();
330 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
331 zName = db_column_text(&q, 2);
@@ -315,30 +334,32 @@
334 undo_save(zName);
335 vfile_to_disk(0, idm, 0, 0);
336 }
337 }
338 db_finalize(&q);
339
340 /*
341 ** Find files that have changed from P->M but not P->V.
342 ** Copy the M content over into V.
343 */
344 db_prepare(&q,
345 "SELECT idv, ridm, fn, islinkm FROM fv"
346 " WHERE idp>0 AND idv>0 AND idm>0"
347 " AND ridm!=ridp AND ridv=ridp AND NOT chnged"
348 );
349 while( db_step(&q)==SQLITE_ROW ){
350 int idv = db_column_int(&q, 0);
351 int ridm = db_column_int(&q, 1);
352 const char *zName = db_column_text(&q, 2);
353 int islinkm = db_column_int(&q, 3);
354 /* Copy content from idm over into idv. Overwrite idv. */
355 fossil_print("UPDATE %s\n", zName);
356 if( !nochangeFlag ){
357 undo_save(zName);
358 db_multi_exec(
359 "UPDATE vfile SET mtime=0, mrid=%d, chnged=2, islink=%d "
360 " WHERE id=%d", ridm, islinkm, idv
361 );
362 vfile_to_disk(0, idv, 0, 0);
363 }
364 }
365 db_finalize(&q);
@@ -345,11 +366,11 @@
366
367 /*
368 ** Do a three-way merge on files that have changes on both P->M and P->V.
369 */
370 db_prepare(&q,
371 "SELECT ridm, idv, ridp, ridv, %s, fn, isexe, islinkv, islinkm FROM fv"
372 " WHERE idp>0 AND idv>0 AND idm>0"
373 " AND ridm!=ridp AND (ridv!=ridp OR chnged)",
374 glob_expr("fv.fn", zBinGlob)
375 );
376 while( db_step(&q)==SQLITE_ROW ){
@@ -358,10 +379,12 @@
379 int ridp = db_column_int(&q, 2);
380 int ridv = db_column_int(&q, 3);
381 int isBinary = db_column_int(&q, 4);
382 const char *zName = db_column_text(&q, 5);
383 int isExe = db_column_int(&q, 6);
384 int islinkv = db_column_int(&q, 7);
385 int islinkm = db_column_int(&q, 8);
386 int rc;
387 char *zFullPath;
388 Blob m, p, r;
389 /* Do a 3-way merge of idp->idm into idp->idv. The results go into idv. */
390 if( detailFlag ){
@@ -368,37 +391,42 @@
391 fossil_print("MERGE %s (pivot=%d v1=%d v2=%d)\n",
392 zName, ridp, ridm, ridv);
393 }else{
394 fossil_print("MERGE %s\n", zName);
395 }
396 if( islinkv || islinkm /* || file_islink(zFullPath) */ ){
397 fossil_print("***** Cannot merge symlink %s\n", zName);
398 nConflict++;
399 }else{
400 undo_save(zName);
401 zFullPath = mprintf("%s/%s", g.zLocalRoot, zName);
402 content_get(ridp, &p);
403 content_get(ridm, &m);
404 if( isBinary ){
405 rc = -1;
406 blob_zero(&r);
407 }else{
408 rc = merge_3way(&p, zFullPath, &m, &r);
409 }
410 if( rc>=0 ){
411 if( !nochangeFlag ){
412 blob_write_to_file(&r, zFullPath);
413 file_setexe(zFullPath, isExe);
414 }
415 db_multi_exec("UPDATE vfile SET mtime=0 WHERE id=%d", idv);
416 if( rc>0 ){
417 fossil_print("***** %d merge conflicts in %s\n", rc, zName);
418 nConflict++;
419 }
420 }else{
421 fossil_print("***** Cannot merge binary file %s\n", zName);
422 nConflict++;
423 }
424 blob_reset(&p);
425 blob_reset(&m);
426 blob_reset(&r);
427 }
428 db_multi_exec("INSERT OR IGNORE INTO vmerge(id,merge) VALUES(%d,%d)",
429 idv,ridm);
430 }
431 db_finalize(&q);
432
433
+94 -43
--- src/path.c
+++ src/path.c
@@ -89,14 +89,11 @@
8989
p = path.pAll;
9090
path.pAll = p->pAll;
9191
fossil_free(p);
9292
}
9393
bag_clear(&path.seen);
94
- path.pCurrent = 0;
95
- path.pAll = 0;
96
- path.pEnd = 0;
97
- path.nStep = 0;
94
+ memset(&path, 0, sizeof(&path));
9895
}
9996
10097
/*
10198
** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
10299
*/
@@ -119,11 +116,16 @@
119116
** Elements of the path can be traversed by following the PathNode.u.pTo
120117
** pointer chain.
121118
**
122119
** Return NULL if no path is found.
123120
*/
124
-PathNode *path_shortest(int iFrom, int iTo, int directOnly){
121
+PathNode *path_shortest(
122
+ int iFrom, /* Path starts here */
123
+ int iTo, /* Path ends here */
124
+ int directOnly, /* No merge links if true */
125
+ int oneWayOnly /* Parent->child only if true */
126
+){
125127
Stmt s;
126128
PathNode *pPrev;
127129
PathNode *p;
128130
129131
path_reset();
@@ -130,11 +132,15 @@
130132
path.pStart = path_new_node(iFrom, 0, 0);
131133
if( iTo==iFrom ){
132134
path.pEnd = path.pStart;
133135
return path.pStart;
134136
}
135
- if( directOnly ){
137
+ if( oneWayOnly ){
138
+ db_prepare(&s,
139
+ "SELECT cid, 1 FROM plink WHERE pid=:pid "
140
+ );
141
+ }else if( directOnly ){
136142
db_prepare(&s,
137143
"SELECT cid, 1 FROM plink WHERE pid=:pid AND isprim "
138144
"UNION ALL "
139145
"SELECT pid, 0 FROM plink WHERE cid=:pid AND isprim"
140146
);
@@ -196,17 +202,19 @@
196202
int iFrom;
197203
int iTo;
198204
PathNode *p;
199205
int n;
200206
int directOnly;
207
+ int oneWay;
201208
202209
db_find_and_open_repository(0,0);
203210
directOnly = find_option("no-merge",0,0)!=0;
211
+ oneWay = find_option("one-way",0,0)!=0;
204212
if( g.argc!=4 ) usage("VERSION1 VERSION2");
205213
iFrom = name_to_rid(g.argv[2]);
206214
iTo = name_to_rid(g.argv[3]);
207
- p = path_shortest(iFrom, iTo, directOnly);
215
+ p = path_shortest(iFrom, iTo, directOnly, oneWay);
208216
if( p==0 ){
209217
fossil_fatal("no path from %s to %s", g.argv[1], g.argv[2]);
210218
}
211219
for(n=1, p=path.pStart; p; p=p->u.pTo, n++){
212220
char *z;
@@ -213,11 +221,11 @@
213221
z = db_text(0,
214222
"SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
215223
" FROM blob, event"
216224
" WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
217225
p->rid, p->rid);
218
- fossil_print("%4d: %s", n, z);
226
+ fossil_print("%4d: %5d %s", n, p->rid, z);
219227
fossil_free(z);
220228
if( p->u.pTo ){
221229
fossil_print(" is a %s of\n",
222230
p->u.pTo->fromIsParent ? "parent" : "child");
223231
}else{
@@ -313,11 +321,11 @@
313321
z = db_text(0,
314322
"SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
315323
" FROM blob, event"
316324
" WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
317325
p->rid, p->rid);
318
- fossil_print("%4d: %s", n, z);
326
+ fossil_print("%4d: %5d %s", n, p->rid, z);
319327
fossil_free(z);
320328
if( p->rid==iFrom ) fossil_print(" VERSION1");
321329
if( p->rid==iTo ) fossil_print(" VERSION2");
322330
if( p->rid==iPivot ) fossil_print(" PIVOT");
323331
fossil_print("\n");
@@ -340,25 +348,29 @@
340348
** Compute all file name changes that occur going from checkin iFrom
341349
** to checkin iTo.
342350
**
343351
** The number of name changes is written into *pnChng. For each name
344352
** change, two integers are allocated for *piChng. The first is the
345
-** filename.fnid for the original name and the second is for new name.
353
+** filename.fnid for the original name as seen in check-in iFrom and
354
+** the second is for new name as it is used in check-in iTo.
355
+**
346356
** Space to hold *piChng is obtained from fossil_malloc() and should
347357
** be released by the caller.
348358
**
349
-** This routine really has nothing to do with pathion. It is located
359
+** This routine really has nothing to do with path. It is located
350360
** in this path.c module in order to leverage some of the path
351361
** infrastructure.
352362
*/
353363
void find_filename_changes(
354
- int iFrom,
355
- int iTo,
356
- int *pnChng,
357
- int **aiChng
364
+ int iFrom, /* Ancestor check-in */
365
+ int iTo, /* Recent check-in */
366
+ int revOk, /* Ok to move backwards (child->parent) if true */
367
+ int *pnChng, /* Number of name changes along the path */
368
+ int **aiChng, /* Name changes */
369
+ const char *zDebug /* Generate trace output if no NULL */
358370
){
359
- PathNode *p; /* For looping over path from iFrom to iTo */
371
+ PathNode *p; /* For looping over path from iFrom to iTo */
360372
NameChange *pAll = 0; /* List of all name changes seen so far */
361373
NameChange *pChng; /* For looping through the name change list */
362374
int nChng = 0; /* Number of files whose names have changed */
363375
int *aChng; /* Two integers per name change */
364376
int i; /* Loop counter */
@@ -366,58 +378,87 @@
366378
367379
*pnChng = 0;
368380
*aiChng = 0;
369381
if( iFrom==iTo ) return;
370382
path_reset();
371
- p = path_shortest(iFrom, iTo, 1);
383
+ p = path_shortest(iFrom, iTo, 1, revOk==0);
372384
if( p==0 ) return;
373385
path_reverse_path();
374386
db_prepare(&q1,
375
- "SELECT pfnid, fnid FROM mlink WHERE mid=:mid AND pfnid>0"
387
+ "SELECT pfnid, fnid FROM mlink"
388
+ " WHERE mid=:mid AND (pfnid>0 OR fid==0)"
389
+ " ORDER BY pfnid"
376390
);
377391
for(p=path.pStart; p; p=p->u.pTo){
378392
int fnid, pfnid;
379393
if( !p->fromIsParent && (p->u.pTo==0 || p->u.pTo->fromIsParent) ){
380394
/* Skip nodes where the parent is not on the path */
381395
continue;
382396
}
383397
db_bind_int(&q1, ":mid", p->rid);
384398
while( db_step(&q1)==SQLITE_ROW ){
385
- if( p->fromIsParent ){
386
- fnid = db_column_int(&q1, 1);
387
- pfnid = db_column_int(&q1, 0);
388
- }else{
389
- fnid = db_column_int(&q1, 0);
390
- pfnid = db_column_int(&q1, 1);
399
+ fnid = db_column_int(&q1, 1);
400
+ pfnid = db_column_int(&q1, 0);
401
+ if( pfnid==0 ){
402
+ pfnid = fnid;
403
+ fnid = 0;
404
+ }
405
+ if( !p->fromIsParent ){
406
+ int t = fnid;
407
+ fnid = pfnid;
408
+ pfnid = t;
409
+ }
410
+ if( zDebug ){
411
+ fossil_print("%s at %d%s %.10z: %d[%z] -> %d[%z]\n",
412
+ zDebug, p->rid, p->fromIsParent ? ">" : "<",
413
+ db_text(0, "SELECT uuid FROM blob WHERE rid=%d", p->rid),
414
+ pfnid,
415
+ db_text(0, "SELECT name FROM filename WHERE fnid=%d", pfnid),
416
+ fnid,
417
+ db_text(0, "SELECT name FROM filename WHERE fnid=%d", fnid));
391418
}
392419
for(pChng=pAll; pChng; pChng=pChng->pNext){
393420
if( pChng->curName==pfnid ){
394421
pChng->newName = fnid;
395422
break;
396423
}
397424
}
398
- if( pChng==0 ){
425
+ if( pChng==0 && fnid>0 ){
399426
pChng = fossil_malloc( sizeof(*pChng) );
400427
pChng->pNext = pAll;
401428
pAll = pChng;
402429
pChng->origName = pfnid;
403430
pChng->curName = pfnid;
404431
pChng->newName = fnid;
405432
nChng++;
406433
}
407434
}
408
- for(pChng=pAll; pChng; pChng=pChng->pNext) pChng->curName = pChng->newName;
435
+ for(pChng=pAll; pChng; pChng=pChng->pNext){
436
+ pChng->curName = pChng->newName;
437
+ }
409438
db_reset(&q1);
410439
}
411440
db_finalize(&q1);
412441
if( nChng ){
413
- *pnChng = nChng;
414442
aChng = *aiChng = fossil_malloc( nChng*2*sizeof(int) );
415
- for(pChng=pAll, i=0; pChng; pChng=pChng->pNext, i+=2){
443
+ for(pChng=pAll, i=0; pChng; pChng=pChng->pNext){
444
+ if( pChng->newName==0 ) continue;
445
+ if( pChng->origName==0 ) continue;
446
+ if( pChng->newName==pChng->origName ) continue;
416447
aChng[i] = pChng->origName;
417448
aChng[i+1] = pChng->newName;
449
+ if( zDebug ){
450
+ fossil_print("%s summary %d[%z] -> %d[%z]\n",
451
+ zDebug,
452
+ aChng[i],
453
+ db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i]),
454
+ aChng[i+1],
455
+ db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i+1]));
456
+ }
457
+ i += 2;
418458
}
459
+ *pnChng = i/2;
419460
while( pAll ){
420461
pChng = pAll;
421462
pAll = pAll->pNext;
422463
fossil_free(pChng);
423464
}
@@ -425,32 +466,42 @@
425466
}
426467
427468
/*
428469
** COMMAND: test-name-changes
429470
**
430
-** Usage: %fossil test-name-changes VERSION1 VERSION2
471
+** Usage: %fossil test-name-changes [--debug] VERSION1 VERSION2
431472
**
432473
** Show all filename changes that occur going from VERSION1 to VERSION2
433474
*/
434475
void test_name_change(void){
435476
int iFrom;
436477
int iTo;
437478
int *aChng;
438479
int nChng;
439480
int i;
481
+ const char *zDebug = 0;
482
+ int revOk = 0;
440483
441484
db_find_and_open_repository(0,0);
442
- if( g.argc!=4 ) usage("VERSION1 VERSION2");
443
- iFrom = name_to_rid(g.argv[2]);
444
- iTo = name_to_rid(g.argv[3]);
445
- find_filename_changes(iFrom, iTo, &nChng, &aChng);
446
- for(i=0; i<nChng; i++){
447
- char *zFrom, *zTo;
448
-
449
- zFrom = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
450
- zTo = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
451
- fossil_print("[%s] -> [%s]\n", zFrom, zTo);
452
- fossil_free(zFrom);
453
- fossil_free(zTo);
454
- }
455
- fossil_free(aChng);
485
+ zDebug = find_option("debug",0,0)!=0 ? "debug" : 0;
486
+ revOk = find_option("bidirectional",0,0)!=0;
487
+ if( g.argc<4 ) usage("VERSION1 VERSION2");
488
+ while( g.argc>=4 ){
489
+ iFrom = name_to_rid(g.argv[2]);
490
+ iTo = name_to_rid(g.argv[3]);
491
+ find_filename_changes(iFrom, iTo, revOk, &nChng, &aChng, zDebug);
492
+ fossil_print("------ Changes for (%d) %s -> (%d) %s\n",
493
+ iFrom, g.argv[2], iTo, g.argv[3]);
494
+ for(i=0; i<nChng; i++){
495
+ char *zFrom, *zTo;
496
+
497
+ zFrom = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
498
+ zTo = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
499
+ fossil_print("[%s] -> [%s]\n", zFrom, zTo);
500
+ fossil_free(zFrom);
501
+ fossil_free(zTo);
502
+ }
503
+ fossil_free(aChng);
504
+ g.argv += 2;
505
+ g.argc -= 2;
506
+ }
456507
}
457508
--- src/path.c
+++ src/path.c
@@ -89,14 +89,11 @@
89 p = path.pAll;
90 path.pAll = p->pAll;
91 fossil_free(p);
92 }
93 bag_clear(&path.seen);
94 path.pCurrent = 0;
95 path.pAll = 0;
96 path.pEnd = 0;
97 path.nStep = 0;
98 }
99
100 /*
101 ** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
102 */
@@ -119,11 +116,16 @@
119 ** Elements of the path can be traversed by following the PathNode.u.pTo
120 ** pointer chain.
121 **
122 ** Return NULL if no path is found.
123 */
124 PathNode *path_shortest(int iFrom, int iTo, int directOnly){
 
 
 
 
 
125 Stmt s;
126 PathNode *pPrev;
127 PathNode *p;
128
129 path_reset();
@@ -130,11 +132,15 @@
130 path.pStart = path_new_node(iFrom, 0, 0);
131 if( iTo==iFrom ){
132 path.pEnd = path.pStart;
133 return path.pStart;
134 }
135 if( directOnly ){
 
 
 
 
136 db_prepare(&s,
137 "SELECT cid, 1 FROM plink WHERE pid=:pid AND isprim "
138 "UNION ALL "
139 "SELECT pid, 0 FROM plink WHERE cid=:pid AND isprim"
140 );
@@ -196,17 +202,19 @@
196 int iFrom;
197 int iTo;
198 PathNode *p;
199 int n;
200 int directOnly;
 
201
202 db_find_and_open_repository(0,0);
203 directOnly = find_option("no-merge",0,0)!=0;
 
204 if( g.argc!=4 ) usage("VERSION1 VERSION2");
205 iFrom = name_to_rid(g.argv[2]);
206 iTo = name_to_rid(g.argv[3]);
207 p = path_shortest(iFrom, iTo, directOnly);
208 if( p==0 ){
209 fossil_fatal("no path from %s to %s", g.argv[1], g.argv[2]);
210 }
211 for(n=1, p=path.pStart; p; p=p->u.pTo, n++){
212 char *z;
@@ -213,11 +221,11 @@
213 z = db_text(0,
214 "SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
215 " FROM blob, event"
216 " WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
217 p->rid, p->rid);
218 fossil_print("%4d: %s", n, z);
219 fossil_free(z);
220 if( p->u.pTo ){
221 fossil_print(" is a %s of\n",
222 p->u.pTo->fromIsParent ? "parent" : "child");
223 }else{
@@ -313,11 +321,11 @@
313 z = db_text(0,
314 "SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
315 " FROM blob, event"
316 " WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
317 p->rid, p->rid);
318 fossil_print("%4d: %s", n, z);
319 fossil_free(z);
320 if( p->rid==iFrom ) fossil_print(" VERSION1");
321 if( p->rid==iTo ) fossil_print(" VERSION2");
322 if( p->rid==iPivot ) fossil_print(" PIVOT");
323 fossil_print("\n");
@@ -340,25 +348,29 @@
340 ** Compute all file name changes that occur going from checkin iFrom
341 ** to checkin iTo.
342 **
343 ** The number of name changes is written into *pnChng. For each name
344 ** change, two integers are allocated for *piChng. The first is the
345 ** filename.fnid for the original name and the second is for new name.
 
 
346 ** Space to hold *piChng is obtained from fossil_malloc() and should
347 ** be released by the caller.
348 **
349 ** This routine really has nothing to do with pathion. It is located
350 ** in this path.c module in order to leverage some of the path
351 ** infrastructure.
352 */
353 void find_filename_changes(
354 int iFrom,
355 int iTo,
356 int *pnChng,
357 int **aiChng
 
 
358 ){
359 PathNode *p; /* For looping over path from iFrom to iTo */
360 NameChange *pAll = 0; /* List of all name changes seen so far */
361 NameChange *pChng; /* For looping through the name change list */
362 int nChng = 0; /* Number of files whose names have changed */
363 int *aChng; /* Two integers per name change */
364 int i; /* Loop counter */
@@ -366,58 +378,87 @@
366
367 *pnChng = 0;
368 *aiChng = 0;
369 if( iFrom==iTo ) return;
370 path_reset();
371 p = path_shortest(iFrom, iTo, 1);
372 if( p==0 ) return;
373 path_reverse_path();
374 db_prepare(&q1,
375 "SELECT pfnid, fnid FROM mlink WHERE mid=:mid AND pfnid>0"
 
 
376 );
377 for(p=path.pStart; p; p=p->u.pTo){
378 int fnid, pfnid;
379 if( !p->fromIsParent && (p->u.pTo==0 || p->u.pTo->fromIsParent) ){
380 /* Skip nodes where the parent is not on the path */
381 continue;
382 }
383 db_bind_int(&q1, ":mid", p->rid);
384 while( db_step(&q1)==SQLITE_ROW ){
385 if( p->fromIsParent ){
386 fnid = db_column_int(&q1, 1);
387 pfnid = db_column_int(&q1, 0);
388 }else{
389 fnid = db_column_int(&q1, 0);
390 pfnid = db_column_int(&q1, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
391 }
392 for(pChng=pAll; pChng; pChng=pChng->pNext){
393 if( pChng->curName==pfnid ){
394 pChng->newName = fnid;
395 break;
396 }
397 }
398 if( pChng==0 ){
399 pChng = fossil_malloc( sizeof(*pChng) );
400 pChng->pNext = pAll;
401 pAll = pChng;
402 pChng->origName = pfnid;
403 pChng->curName = pfnid;
404 pChng->newName = fnid;
405 nChng++;
406 }
407 }
408 for(pChng=pAll; pChng; pChng=pChng->pNext) pChng->curName = pChng->newName;
 
 
409 db_reset(&q1);
410 }
411 db_finalize(&q1);
412 if( nChng ){
413 *pnChng = nChng;
414 aChng = *aiChng = fossil_malloc( nChng*2*sizeof(int) );
415 for(pChng=pAll, i=0; pChng; pChng=pChng->pNext, i+=2){
 
 
 
416 aChng[i] = pChng->origName;
417 aChng[i+1] = pChng->newName;
 
 
 
 
 
 
 
 
 
418 }
 
419 while( pAll ){
420 pChng = pAll;
421 pAll = pAll->pNext;
422 fossil_free(pChng);
423 }
@@ -425,32 +466,42 @@
425 }
426
427 /*
428 ** COMMAND: test-name-changes
429 **
430 ** Usage: %fossil test-name-changes VERSION1 VERSION2
431 **
432 ** Show all filename changes that occur going from VERSION1 to VERSION2
433 */
434 void test_name_change(void){
435 int iFrom;
436 int iTo;
437 int *aChng;
438 int nChng;
439 int i;
 
 
440
441 db_find_and_open_repository(0,0);
442 if( g.argc!=4 ) usage("VERSION1 VERSION2");
443 iFrom = name_to_rid(g.argv[2]);
444 iTo = name_to_rid(g.argv[3]);
445 find_filename_changes(iFrom, iTo, &nChng, &aChng);
446 for(i=0; i<nChng; i++){
447 char *zFrom, *zTo;
448
449 zFrom = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
450 zTo = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
451 fossil_print("[%s] -> [%s]\n", zFrom, zTo);
452 fossil_free(zFrom);
453 fossil_free(zTo);
454 }
455 fossil_free(aChng);
 
 
 
 
 
 
 
 
456 }
457
--- src/path.c
+++ src/path.c
@@ -89,14 +89,11 @@
89 p = path.pAll;
90 path.pAll = p->pAll;
91 fossil_free(p);
92 }
93 bag_clear(&path.seen);
94 memset(&path, 0, sizeof(&path));
 
 
 
95 }
96
97 /*
98 ** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
99 */
@@ -119,11 +116,16 @@
116 ** Elements of the path can be traversed by following the PathNode.u.pTo
117 ** pointer chain.
118 **
119 ** Return NULL if no path is found.
120 */
121 PathNode *path_shortest(
122 int iFrom, /* Path starts here */
123 int iTo, /* Path ends here */
124 int directOnly, /* No merge links if true */
125 int oneWayOnly /* Parent->child only if true */
126 ){
127 Stmt s;
128 PathNode *pPrev;
129 PathNode *p;
130
131 path_reset();
@@ -130,11 +132,15 @@
132 path.pStart = path_new_node(iFrom, 0, 0);
133 if( iTo==iFrom ){
134 path.pEnd = path.pStart;
135 return path.pStart;
136 }
137 if( oneWayOnly ){
138 db_prepare(&s,
139 "SELECT cid, 1 FROM plink WHERE pid=:pid "
140 );
141 }else if( directOnly ){
142 db_prepare(&s,
143 "SELECT cid, 1 FROM plink WHERE pid=:pid AND isprim "
144 "UNION ALL "
145 "SELECT pid, 0 FROM plink WHERE cid=:pid AND isprim"
146 );
@@ -196,17 +202,19 @@
202 int iFrom;
203 int iTo;
204 PathNode *p;
205 int n;
206 int directOnly;
207 int oneWay;
208
209 db_find_and_open_repository(0,0);
210 directOnly = find_option("no-merge",0,0)!=0;
211 oneWay = find_option("one-way",0,0)!=0;
212 if( g.argc!=4 ) usage("VERSION1 VERSION2");
213 iFrom = name_to_rid(g.argv[2]);
214 iTo = name_to_rid(g.argv[3]);
215 p = path_shortest(iFrom, iTo, directOnly, oneWay);
216 if( p==0 ){
217 fossil_fatal("no path from %s to %s", g.argv[1], g.argv[2]);
218 }
219 for(n=1, p=path.pStart; p; p=p->u.pTo, n++){
220 char *z;
@@ -213,11 +221,11 @@
221 z = db_text(0,
222 "SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
223 " FROM blob, event"
224 " WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
225 p->rid, p->rid);
226 fossil_print("%4d: %5d %s", n, p->rid, z);
227 fossil_free(z);
228 if( p->u.pTo ){
229 fossil_print(" is a %s of\n",
230 p->u.pTo->fromIsParent ? "parent" : "child");
231 }else{
@@ -313,11 +321,11 @@
321 z = db_text(0,
322 "SELECT substr(uuid,1,12) || ' ' || datetime(mtime)"
323 " FROM blob, event"
324 " WHERE blob.rid=%d AND event.objid=%d AND event.type='ci'",
325 p->rid, p->rid);
326 fossil_print("%4d: %5d %s", n, p->rid, z);
327 fossil_free(z);
328 if( p->rid==iFrom ) fossil_print(" VERSION1");
329 if( p->rid==iTo ) fossil_print(" VERSION2");
330 if( p->rid==iPivot ) fossil_print(" PIVOT");
331 fossil_print("\n");
@@ -340,25 +348,29 @@
348 ** Compute all file name changes that occur going from checkin iFrom
349 ** to checkin iTo.
350 **
351 ** The number of name changes is written into *pnChng. For each name
352 ** change, two integers are allocated for *piChng. The first is the
353 ** filename.fnid for the original name as seen in check-in iFrom and
354 ** the second is for new name as it is used in check-in iTo.
355 **
356 ** Space to hold *piChng is obtained from fossil_malloc() and should
357 ** be released by the caller.
358 **
359 ** This routine really has nothing to do with path. It is located
360 ** in this path.c module in order to leverage some of the path
361 ** infrastructure.
362 */
363 void find_filename_changes(
364 int iFrom, /* Ancestor check-in */
365 int iTo, /* Recent check-in */
366 int revOk, /* Ok to move backwards (child->parent) if true */
367 int *pnChng, /* Number of name changes along the path */
368 int **aiChng, /* Name changes */
369 const char *zDebug /* Generate trace output if no NULL */
370 ){
371 PathNode *p; /* For looping over path from iFrom to iTo */
372 NameChange *pAll = 0; /* List of all name changes seen so far */
373 NameChange *pChng; /* For looping through the name change list */
374 int nChng = 0; /* Number of files whose names have changed */
375 int *aChng; /* Two integers per name change */
376 int i; /* Loop counter */
@@ -366,58 +378,87 @@
378
379 *pnChng = 0;
380 *aiChng = 0;
381 if( iFrom==iTo ) return;
382 path_reset();
383 p = path_shortest(iFrom, iTo, 1, revOk==0);
384 if( p==0 ) return;
385 path_reverse_path();
386 db_prepare(&q1,
387 "SELECT pfnid, fnid FROM mlink"
388 " WHERE mid=:mid AND (pfnid>0 OR fid==0)"
389 " ORDER BY pfnid"
390 );
391 for(p=path.pStart; p; p=p->u.pTo){
392 int fnid, pfnid;
393 if( !p->fromIsParent && (p->u.pTo==0 || p->u.pTo->fromIsParent) ){
394 /* Skip nodes where the parent is not on the path */
395 continue;
396 }
397 db_bind_int(&q1, ":mid", p->rid);
398 while( db_step(&q1)==SQLITE_ROW ){
399 fnid = db_column_int(&q1, 1);
400 pfnid = db_column_int(&q1, 0);
401 if( pfnid==0 ){
402 pfnid = fnid;
403 fnid = 0;
404 }
405 if( !p->fromIsParent ){
406 int t = fnid;
407 fnid = pfnid;
408 pfnid = t;
409 }
410 if( zDebug ){
411 fossil_print("%s at %d%s %.10z: %d[%z] -> %d[%z]\n",
412 zDebug, p->rid, p->fromIsParent ? ">" : "<",
413 db_text(0, "SELECT uuid FROM blob WHERE rid=%d", p->rid),
414 pfnid,
415 db_text(0, "SELECT name FROM filename WHERE fnid=%d", pfnid),
416 fnid,
417 db_text(0, "SELECT name FROM filename WHERE fnid=%d", fnid));
418 }
419 for(pChng=pAll; pChng; pChng=pChng->pNext){
420 if( pChng->curName==pfnid ){
421 pChng->newName = fnid;
422 break;
423 }
424 }
425 if( pChng==0 && fnid>0 ){
426 pChng = fossil_malloc( sizeof(*pChng) );
427 pChng->pNext = pAll;
428 pAll = pChng;
429 pChng->origName = pfnid;
430 pChng->curName = pfnid;
431 pChng->newName = fnid;
432 nChng++;
433 }
434 }
435 for(pChng=pAll; pChng; pChng=pChng->pNext){
436 pChng->curName = pChng->newName;
437 }
438 db_reset(&q1);
439 }
440 db_finalize(&q1);
441 if( nChng ){
 
442 aChng = *aiChng = fossil_malloc( nChng*2*sizeof(int) );
443 for(pChng=pAll, i=0; pChng; pChng=pChng->pNext){
444 if( pChng->newName==0 ) continue;
445 if( pChng->origName==0 ) continue;
446 if( pChng->newName==pChng->origName ) continue;
447 aChng[i] = pChng->origName;
448 aChng[i+1] = pChng->newName;
449 if( zDebug ){
450 fossil_print("%s summary %d[%z] -> %d[%z]\n",
451 zDebug,
452 aChng[i],
453 db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i]),
454 aChng[i+1],
455 db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i+1]));
456 }
457 i += 2;
458 }
459 *pnChng = i/2;
460 while( pAll ){
461 pChng = pAll;
462 pAll = pAll->pNext;
463 fossil_free(pChng);
464 }
@@ -425,32 +466,42 @@
466 }
467
468 /*
469 ** COMMAND: test-name-changes
470 **
471 ** Usage: %fossil test-name-changes [--debug] VERSION1 VERSION2
472 **
473 ** Show all filename changes that occur going from VERSION1 to VERSION2
474 */
475 void test_name_change(void){
476 int iFrom;
477 int iTo;
478 int *aChng;
479 int nChng;
480 int i;
481 const char *zDebug = 0;
482 int revOk = 0;
483
484 db_find_and_open_repository(0,0);
485 zDebug = find_option("debug",0,0)!=0 ? "debug" : 0;
486 revOk = find_option("bidirectional",0,0)!=0;
487 if( g.argc<4 ) usage("VERSION1 VERSION2");
488 while( g.argc>=4 ){
489 iFrom = name_to_rid(g.argv[2]);
490 iTo = name_to_rid(g.argv[3]);
491 find_filename_changes(iFrom, iTo, revOk, &nChng, &aChng, zDebug);
492 fossil_print("------ Changes for (%d) %s -> (%d) %s\n",
493 iFrom, g.argv[2], iTo, g.argv[3]);
494 for(i=0; i<nChng; i++){
495 char *zFrom, *zTo;
496
497 zFrom = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2]);
498 zTo = db_text(0, "SELECT name FROM filename WHERE fnid=%d", aChng[i*2+1]);
499 fossil_print("[%s] -> [%s]\n", zFrom, zTo);
500 fossil_free(zFrom);
501 fossil_free(zTo);
502 }
503 fossil_free(aChng);
504 g.argv += 2;
505 g.argc -= 2;
506 }
507 }
508
+19 -9
--- src/report.c
+++ src/report.c
@@ -198,10 +198,21 @@
198198
}
199199
}
200200
return rc;
201201
}
202202
203
+/*
204
+** Activate the query authorizer
205
+*/
206
+static void report_restrict_sql(char **pzErr){
207
+ (void)fossil_localtime(0);
208
+ sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)pzErr);
209
+}
210
+static void report_unrestrict_sql(void){
211
+ sqlite3_set_authorizer(g.db, 0, 0);
212
+}
213
+
203214
204215
/*
205216
** Check the given SQL to see if is a valid query that does not
206217
** attempt to do anything dangerous. Return 0 on success and a
207218
** pointer to an error message string (obtained from malloc) if
@@ -237,11 +248,11 @@
237248
}
238249
}
239250
}
240251
241252
/* Compile the statement and check for illegal accesses or syntax errors. */
242
- sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr);
253
+ report_restrict_sql(&zErr);
243254
rc = sqlite3_prepare(g.db, zSql, -1, &pStmt, &zTail);
244255
if( rc!=SQLITE_OK ){
245256
zErr = mprintf("Syntax error: %s", sqlite3_errmsg(g.db));
246257
}
247258
if( !sqlite3_stmt_readonly(pStmt) ){
@@ -248,11 +259,11 @@
248259
zErr = mprintf("SQL must not modify the database");
249260
}
250261
if( pStmt ){
251262
sqlite3_finalize(pStmt);
252263
}
253
- sqlite3_set_authorizer(g.db, 0, 0);
264
+ report_unrestrict_sql();
254265
return zErr;
255266
}
256267
257268
/*
258269
** WEBPAGE: /rptsql
@@ -963,25 +974,24 @@
963974
output_color_key(zClrKey, 1,
964975
"border=\"0\" cellpadding=\"3\" cellspacing=\"0\" class=\"report\"");
965976
@ <table border="1" cellpadding="2" cellspacing="0" class="report">
966977
sState.rn = rn;
967978
sState.nCount = 0;
968
- (void)fossil_localtime(0); /* initialize the g.fTimeFormat variable */
969
- sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
979
+ report_restrict_sql(&zErr1);
970980
sqlite3_exec_readonly(g.db, zSql, generate_html, &sState, &zErr2);
971
- sqlite3_set_authorizer(g.db, 0, 0);
981
+ report_unrestrict_sql();
972982
@ </table>
973983
if( zErr1 ){
974984
@ <p class="reportError">Error: %h(zErr1)</p>
975985
}else if( zErr2 ){
976986
@ <p class="reportError">Error: %h(zErr2)</p>
977987
}
978988
style_footer();
979989
}else{
980
- sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
990
+ report_restrict_sql(&zErr1);
981991
sqlite3_exec_readonly(g.db, zSql, output_tab_separated, &count, &zErr2);
982
- sqlite3_set_authorizer(g.db, 0, 0);
992
+ report_unrestrict_sql();
983993
cgi_set_content_type("text/plain");
984994
}
985995
}
986996
987997
/*
@@ -1137,12 +1147,12 @@
11371147
zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
11381148
}
11391149
count = 0;
11401150
tktEncode = enc;
11411151
zSep = zSepIn;
1142
- sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
1152
+ report_restrict_sql(&zErr1);
11431153
sqlite3_exec_readonly(g.db, zSql, output_separated_file, &count, &zErr2);
1144
- sqlite3_set_authorizer(g.db, 0, 0);
1154
+ report_unrestrict_sql();
11451155
if( zFilter ){
11461156
free(zSql);
11471157
}
11481158
}
11491159
--- src/report.c
+++ src/report.c
@@ -198,10 +198,21 @@
198 }
199 }
200 return rc;
201 }
202
 
 
 
 
 
 
 
 
 
 
 
203
204 /*
205 ** Check the given SQL to see if is a valid query that does not
206 ** attempt to do anything dangerous. Return 0 on success and a
207 ** pointer to an error message string (obtained from malloc) if
@@ -237,11 +248,11 @@
237 }
238 }
239 }
240
241 /* Compile the statement and check for illegal accesses or syntax errors. */
242 sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr);
243 rc = sqlite3_prepare(g.db, zSql, -1, &pStmt, &zTail);
244 if( rc!=SQLITE_OK ){
245 zErr = mprintf("Syntax error: %s", sqlite3_errmsg(g.db));
246 }
247 if( !sqlite3_stmt_readonly(pStmt) ){
@@ -248,11 +259,11 @@
248 zErr = mprintf("SQL must not modify the database");
249 }
250 if( pStmt ){
251 sqlite3_finalize(pStmt);
252 }
253 sqlite3_set_authorizer(g.db, 0, 0);
254 return zErr;
255 }
256
257 /*
258 ** WEBPAGE: /rptsql
@@ -963,25 +974,24 @@
963 output_color_key(zClrKey, 1,
964 "border=\"0\" cellpadding=\"3\" cellspacing=\"0\" class=\"report\"");
965 @ <table border="1" cellpadding="2" cellspacing="0" class="report">
966 sState.rn = rn;
967 sState.nCount = 0;
968 (void)fossil_localtime(0); /* initialize the g.fTimeFormat variable */
969 sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
970 sqlite3_exec_readonly(g.db, zSql, generate_html, &sState, &zErr2);
971 sqlite3_set_authorizer(g.db, 0, 0);
972 @ </table>
973 if( zErr1 ){
974 @ <p class="reportError">Error: %h(zErr1)</p>
975 }else if( zErr2 ){
976 @ <p class="reportError">Error: %h(zErr2)</p>
977 }
978 style_footer();
979 }else{
980 sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
981 sqlite3_exec_readonly(g.db, zSql, output_tab_separated, &count, &zErr2);
982 sqlite3_set_authorizer(g.db, 0, 0);
983 cgi_set_content_type("text/plain");
984 }
985 }
986
987 /*
@@ -1137,12 +1147,12 @@
1137 zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
1138 }
1139 count = 0;
1140 tktEncode = enc;
1141 zSep = zSepIn;
1142 sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)&zErr1);
1143 sqlite3_exec_readonly(g.db, zSql, output_separated_file, &count, &zErr2);
1144 sqlite3_set_authorizer(g.db, 0, 0);
1145 if( zFilter ){
1146 free(zSql);
1147 }
1148 }
1149
--- src/report.c
+++ src/report.c
@@ -198,10 +198,21 @@
198 }
199 }
200 return rc;
201 }
202
203 /*
204 ** Activate the query authorizer
205 */
206 static void report_restrict_sql(char **pzErr){
207 (void)fossil_localtime(0);
208 sqlite3_set_authorizer(g.db, report_query_authorizer, (void*)pzErr);
209 }
210 static void report_unrestrict_sql(void){
211 sqlite3_set_authorizer(g.db, 0, 0);
212 }
213
214
215 /*
216 ** Check the given SQL to see if is a valid query that does not
217 ** attempt to do anything dangerous. Return 0 on success and a
218 ** pointer to an error message string (obtained from malloc) if
@@ -237,11 +248,11 @@
248 }
249 }
250 }
251
252 /* Compile the statement and check for illegal accesses or syntax errors. */
253 report_restrict_sql(&zErr);
254 rc = sqlite3_prepare(g.db, zSql, -1, &pStmt, &zTail);
255 if( rc!=SQLITE_OK ){
256 zErr = mprintf("Syntax error: %s", sqlite3_errmsg(g.db));
257 }
258 if( !sqlite3_stmt_readonly(pStmt) ){
@@ -248,11 +259,11 @@
259 zErr = mprintf("SQL must not modify the database");
260 }
261 if( pStmt ){
262 sqlite3_finalize(pStmt);
263 }
264 report_unrestrict_sql();
265 return zErr;
266 }
267
268 /*
269 ** WEBPAGE: /rptsql
@@ -963,25 +974,24 @@
974 output_color_key(zClrKey, 1,
975 "border=\"0\" cellpadding=\"3\" cellspacing=\"0\" class=\"report\"");
976 @ <table border="1" cellpadding="2" cellspacing="0" class="report">
977 sState.rn = rn;
978 sState.nCount = 0;
979 report_restrict_sql(&zErr1);
 
980 sqlite3_exec_readonly(g.db, zSql, generate_html, &sState, &zErr2);
981 report_unrestrict_sql();
982 @ </table>
983 if( zErr1 ){
984 @ <p class="reportError">Error: %h(zErr1)</p>
985 }else if( zErr2 ){
986 @ <p class="reportError">Error: %h(zErr2)</p>
987 }
988 style_footer();
989 }else{
990 report_restrict_sql(&zErr1);
991 sqlite3_exec_readonly(g.db, zSql, output_tab_separated, &count, &zErr2);
992 report_unrestrict_sql();
993 cgi_set_content_type("text/plain");
994 }
995 }
996
997 /*
@@ -1137,12 +1147,12 @@
1147 zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
1148 }
1149 count = 0;
1150 tktEncode = enc;
1151 zSep = zSepIn;
1152 report_restrict_sql(&zErr1);
1153 sqlite3_exec_readonly(g.db, zSql, output_separated_file, &count, &zErr2);
1154 report_unrestrict_sql();
1155 if( zFilter ){
1156 free(zSql);
1157 }
1158 }
1159
--- src/schema.c
+++ src/schema.c
@@ -461,10 +461,11 @@
461461
@ id INTEGER PRIMARY KEY, -- ID of the checked out file
462462
@ vid INTEGER REFERENCES blob, -- The baseline this file is part of.
463463
@ chnged INT DEFAULT 0, -- 0:unchnged 1:edited 2:m-chng 3:m-add
464464
@ deleted BOOLEAN DEFAULT 0, -- True if deleted
465465
@ isexe BOOLEAN, -- True if file should be executable
466
+@ islink BOOLEAN, -- True if file should be symlink
466467
@ rid INTEGER, -- Originally from this repository record
467468
@ mrid INTEGER, -- Based on this record due to a merge
468469
@ mtime INTEGER, -- Mtime of file on disk. sec since 1970
469470
@ pathname TEXT, -- Full pathname relative to root
470471
@ origname TEXT, -- Original pathname. NULL if unchanged
471472
--- src/schema.c
+++ src/schema.c
@@ -461,10 +461,11 @@
461 @ id INTEGER PRIMARY KEY, -- ID of the checked out file
462 @ vid INTEGER REFERENCES blob, -- The baseline this file is part of.
463 @ chnged INT DEFAULT 0, -- 0:unchnged 1:edited 2:m-chng 3:m-add
464 @ deleted BOOLEAN DEFAULT 0, -- True if deleted
465 @ isexe BOOLEAN, -- True if file should be executable
 
466 @ rid INTEGER, -- Originally from this repository record
467 @ mrid INTEGER, -- Based on this record due to a merge
468 @ mtime INTEGER, -- Mtime of file on disk. sec since 1970
469 @ pathname TEXT, -- Full pathname relative to root
470 @ origname TEXT, -- Original pathname. NULL if unchanged
471
--- src/schema.c
+++ src/schema.c
@@ -461,10 +461,11 @@
461 @ id INTEGER PRIMARY KEY, -- ID of the checked out file
462 @ vid INTEGER REFERENCES blob, -- The baseline this file is part of.
463 @ chnged INT DEFAULT 0, -- 0:unchnged 1:edited 2:m-chng 3:m-add
464 @ deleted BOOLEAN DEFAULT 0, -- True if deleted
465 @ isexe BOOLEAN, -- True if file should be executable
466 @ islink BOOLEAN, -- True if file should be symlink
467 @ rid INTEGER, -- Originally from this repository record
468 @ mrid INTEGER, -- Based on this record due to a merge
469 @ mtime INTEGER, -- Mtime of file on disk. sec since 1970
470 @ pathname TEXT, -- Full pathname relative to root
471 @ origname TEXT, -- Original pathname. NULL if unchanged
472
--- src/setup.c
+++ src/setup.c
@@ -1050,10 +1050,11 @@
10501050
if( !g.okSetup ){
10511051
login_needed();
10521052
}
10531053
10541054
style_header("Settings");
1055
+ db_open_local();
10551056
db_begin_transaction();
10561057
@ <p>This page provides a simple interface to the "fossil setting" command.
10571058
@ See the "fossil help setting" output below for further information on
10581059
@ the meaning of each setting.</p><hr />
10591060
@ <form action="%s(g.zTop)/setup_settings" method="post"><div>
10601061
--- src/setup.c
+++ src/setup.c
@@ -1050,10 +1050,11 @@
1050 if( !g.okSetup ){
1051 login_needed();
1052 }
1053
1054 style_header("Settings");
 
1055 db_begin_transaction();
1056 @ <p>This page provides a simple interface to the "fossil setting" command.
1057 @ See the "fossil help setting" output below for further information on
1058 @ the meaning of each setting.</p><hr />
1059 @ <form action="%s(g.zTop)/setup_settings" method="post"><div>
1060
--- src/setup.c
+++ src/setup.c
@@ -1050,10 +1050,11 @@
1050 if( !g.okSetup ){
1051 login_needed();
1052 }
1053
1054 style_header("Settings");
1055 db_open_local();
1056 db_begin_transaction();
1057 @ <p>This page provides a simple interface to the "fossil setting" command.
1058 @ See the "fossil help setting" output below for further information on
1059 @ the meaning of each setting.</p><hr />
1060 @ <form action="%s(g.zTop)/setup_settings" method="post"><div>
1061
+60 -34
--- src/sha1.c
+++ src/sha1.c
@@ -27,60 +27,79 @@
2727
* blk0() and blk() perform the initial expand.
2828
* I got the idea of expanding during the round function from SSLeay
2929
*
3030
* blk0le() for little-endian and blk0be() for big-endian.
3131
*/
32
-#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
33
-#define blk0le(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
34
- |(rol(block->l[i],8)&0x00FF00FF))
35
-#define blk0be(i) block->l[i]
36
-#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
37
- ^block->l[(i+2)&15]^block->l[i&15],1))
32
+#if __GNUC__ && (defined(__i386__) || defined(__x86_64__))
33
+/*
34
+ * GCC by itself only generates left rotates. Use right rotates if
35
+ * possible to be kinder to dinky implementations with iterative rotate
36
+ * instructions.
37
+ */
38
+#define SHA_ROT(op, x, k) \
39
+ ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; })
40
+#define rol(x,k) SHA_ROT("roll", x, k)
41
+#define ror(x,k) SHA_ROT("rorl", x, k)
42
+
43
+#else
44
+/* Generic C equivalent */
45
+#define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
46
+#define rol(x,k) SHA_ROT(x,k,32-(k))
47
+#define ror(x,k) SHA_ROT(x,32-(k),k)
48
+#endif
49
+
50
+
51
+#define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
52
+ |(rol(block[i],8)&0x00FF00FF))
53
+#define blk0be(i) block[i]
54
+#define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
55
+ ^block[(i+2)&15]^block[i&15],1))
3856
3957
/*
4058
* (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
4159
*
4260
* Rl0() for little-endian and Rb0() for big-endian. Endianness is
4361
* determined at run-time.
4462
*/
4563
#define Rl0(v,w,x,y,z,i) \
46
- z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=rol(w,30);
64
+ z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
4765
#define Rb0(v,w,x,y,z,i) \
48
- z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=rol(w,30);
66
+ z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
4967
#define R1(v,w,x,y,z,i) \
50
- z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
68
+ z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
5169
#define R2(v,w,x,y,z,i) \
52
- z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
70
+ z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
5371
#define R3(v,w,x,y,z,i) \
54
- z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
72
+ z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
5573
#define R4(v,w,x,y,z,i) \
56
- z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
57
-
58
-typedef union {
59
- unsigned char c[64];
60
- unsigned int l[16];
61
-} CHAR64LONG16;
74
+ z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
6275
6376
/*
6477
* Hash a single 512-bit block. This is the core of the algorithm.
6578
*/
79
+#define a qq[0]
80
+#define b qq[1]
81
+#define c qq[2]
82
+#define d qq[3]
83
+#define e qq[4]
84
+
6685
void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
6786
{
68
- unsigned int a, b, c, d, e;
69
- CHAR64LONG16 *block;
87
+ unsigned int qq[5]; // a, b, c, d, e;
7088
static int one = 1;
71
- CHAR64LONG16 workspace;
72
-
73
- block = &workspace;
74
- (void)memcpy(block, buffer, 64);
89
+ unsigned int block[16];
90
+ memcpy(block, buffer, 64);
91
+ memcpy(qq,state,5*sizeof(unsigned int));
7592
7693
/* Copy context->state[] to working vars */
94
+ /*
7795
a = state[0];
7896
b = state[1];
7997
c = state[2];
8098
d = state[3];
8199
e = state[4];
100
+ */
82101
83102
/* 4 rounds of 20 operations each. Loop unrolled. */
84103
if( 1 == *(unsigned char*)&one ){
85104
Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
86105
Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
@@ -113,13 +132,10 @@
113132
state[0] += a;
114133
state[1] += b;
115134
state[2] += c;
116135
state[3] += d;
117136
state[4] += e;
118
-
119
- /* Wipe variables */
120
- a = b = c = d = e = 0;
121137
}
122138
123139
124140
/*
125141
* SHA1Init - Initialize new context
@@ -192,18 +208,17 @@
192208
** digest is stored in the first 20 bytes. zBuf should
193209
** be "char zBuf[41]".
194210
*/
195211
static void DigestToBase16(unsigned char *digest, char *zBuf){
196212
static char const zEncode[] = "0123456789abcdef";
197
- int i, j;
198
-
199
- for(j=i=0; i<20; i++){
200
- int a = digest[i];
201
- zBuf[j++] = zEncode[(a>>4)&0xf];
202
- zBuf[j++] = zEncode[a & 0xf];
203
- }
204
- zBuf[j] = 0;
213
+ int ix;
214
+
215
+ for(ix=0; ix<20; ix++){
216
+ *zBuf++ = zEncode[(*digest>>4)&0xf];
217
+ *zBuf++ = zEncode[*digest++ & 0xf];
218
+ }
219
+ *zBuf = '\0';
205220
}
206221
207222
/*
208223
** The state of a incremental SHA1 checksum computation. Only one
209224
** such computation can be underway at a time, of course.
@@ -265,10 +280,21 @@
265280
int sha1sum_file(const char *zFilename, Blob *pCksum){
266281
FILE *in;
267282
SHA1Context ctx;
268283
unsigned char zResult[20];
269284
char zBuf[10240];
285
+
286
+ if( file_islink(zFilename) ){
287
+ /* Instead of file content, return sha1 of link destination path */
288
+ Blob destinationPath;
289
+ int rc;
290
+
291
+ blob_read_link(&destinationPath, zFilename);
292
+ rc = sha1sum_blob(&destinationPath, pCksum);
293
+ blob_reset(&destinationPath);
294
+ return rc;
295
+ }
270296
271297
in = fossil_fopen(zFilename,"rb");
272298
if( in==0 ){
273299
return 1;
274300
}
275301
--- src/sha1.c
+++ src/sha1.c
@@ -27,60 +27,79 @@
27 * blk0() and blk() perform the initial expand.
28 * I got the idea of expanding during the round function from SSLeay
29 *
30 * blk0le() for little-endian and blk0be() for big-endian.
31 */
32 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
33 #define blk0le(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
34 |(rol(block->l[i],8)&0x00FF00FF))
35 #define blk0be(i) block->l[i]
36 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
37 ^block->l[(i+2)&15]^block->l[i&15],1))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
39 /*
40 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
41 *
42 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
43 * determined at run-time.
44 */
45 #define Rl0(v,w,x,y,z,i) \
46 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=rol(w,30);
47 #define Rb0(v,w,x,y,z,i) \
48 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=rol(w,30);
49 #define R1(v,w,x,y,z,i) \
50 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
51 #define R2(v,w,x,y,z,i) \
52 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
53 #define R3(v,w,x,y,z,i) \
54 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
55 #define R4(v,w,x,y,z,i) \
56 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
57
58 typedef union {
59 unsigned char c[64];
60 unsigned int l[16];
61 } CHAR64LONG16;
62
63 /*
64 * Hash a single 512-bit block. This is the core of the algorithm.
65 */
 
 
 
 
 
 
66 void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
67 {
68 unsigned int a, b, c, d, e;
69 CHAR64LONG16 *block;
70 static int one = 1;
71 CHAR64LONG16 workspace;
72
73 block = &workspace;
74 (void)memcpy(block, buffer, 64);
75
76 /* Copy context->state[] to working vars */
 
77 a = state[0];
78 b = state[1];
79 c = state[2];
80 d = state[3];
81 e = state[4];
 
82
83 /* 4 rounds of 20 operations each. Loop unrolled. */
84 if( 1 == *(unsigned char*)&one ){
85 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
86 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
@@ -113,13 +132,10 @@
113 state[0] += a;
114 state[1] += b;
115 state[2] += c;
116 state[3] += d;
117 state[4] += e;
118
119 /* Wipe variables */
120 a = b = c = d = e = 0;
121 }
122
123
124 /*
125 * SHA1Init - Initialize new context
@@ -192,18 +208,17 @@
192 ** digest is stored in the first 20 bytes. zBuf should
193 ** be "char zBuf[41]".
194 */
195 static void DigestToBase16(unsigned char *digest, char *zBuf){
196 static char const zEncode[] = "0123456789abcdef";
197 int i, j;
198
199 for(j=i=0; i<20; i++){
200 int a = digest[i];
201 zBuf[j++] = zEncode[(a>>4)&0xf];
202 zBuf[j++] = zEncode[a & 0xf];
203 }
204 zBuf[j] = 0;
205 }
206
207 /*
208 ** The state of a incremental SHA1 checksum computation. Only one
209 ** such computation can be underway at a time, of course.
@@ -265,10 +280,21 @@
265 int sha1sum_file(const char *zFilename, Blob *pCksum){
266 FILE *in;
267 SHA1Context ctx;
268 unsigned char zResult[20];
269 char zBuf[10240];
 
 
 
 
 
 
 
 
 
 
 
270
271 in = fossil_fopen(zFilename,"rb");
272 if( in==0 ){
273 return 1;
274 }
275
--- src/sha1.c
+++ src/sha1.c
@@ -27,60 +27,79 @@
27 * blk0() and blk() perform the initial expand.
28 * I got the idea of expanding during the round function from SSLeay
29 *
30 * blk0le() for little-endian and blk0be() for big-endian.
31 */
32 #if __GNUC__ && (defined(__i386__) || defined(__x86_64__))
33 /*
34 * GCC by itself only generates left rotates. Use right rotates if
35 * possible to be kinder to dinky implementations with iterative rotate
36 * instructions.
37 */
38 #define SHA_ROT(op, x, k) \
39 ({ unsigned int y; asm(op " %1,%0" : "=r" (y) : "I" (k), "0" (x)); y; })
40 #define rol(x,k) SHA_ROT("roll", x, k)
41 #define ror(x,k) SHA_ROT("rorl", x, k)
42
43 #else
44 /* Generic C equivalent */
45 #define SHA_ROT(x,l,r) ((x) << (l) | (x) >> (r))
46 #define rol(x,k) SHA_ROT(x,k,32-(k))
47 #define ror(x,k) SHA_ROT(x,32-(k),k)
48 #endif
49
50
51 #define blk0le(i) (block[i] = (ror(block[i],8)&0xFF00FF00) \
52 |(rol(block[i],8)&0x00FF00FF))
53 #define blk0be(i) block[i]
54 #define blk(i) (block[i&15] = rol(block[(i+13)&15]^block[(i+8)&15] \
55 ^block[(i+2)&15]^block[i&15],1))
56
57 /*
58 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
59 *
60 * Rl0() for little-endian and Rb0() for big-endian. Endianness is
61 * determined at run-time.
62 */
63 #define Rl0(v,w,x,y,z,i) \
64 z+=((w&(x^y))^y)+blk0le(i)+0x5A827999+rol(v,5);w=ror(w,2);
65 #define Rb0(v,w,x,y,z,i) \
66 z+=((w&(x^y))^y)+blk0be(i)+0x5A827999+rol(v,5);w=ror(w,2);
67 #define R1(v,w,x,y,z,i) \
68 z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=ror(w,2);
69 #define R2(v,w,x,y,z,i) \
70 z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=ror(w,2);
71 #define R3(v,w,x,y,z,i) \
72 z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=ror(w,2);
73 #define R4(v,w,x,y,z,i) \
74 z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=ror(w,2);
 
 
 
 
 
75
76 /*
77 * Hash a single 512-bit block. This is the core of the algorithm.
78 */
79 #define a qq[0]
80 #define b qq[1]
81 #define c qq[2]
82 #define d qq[3]
83 #define e qq[4]
84
85 void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
86 {
87 unsigned int qq[5]; // a, b, c, d, e;
 
88 static int one = 1;
89 unsigned int block[16];
90 memcpy(block, buffer, 64);
91 memcpy(qq,state,5*sizeof(unsigned int));
 
92
93 /* Copy context->state[] to working vars */
94 /*
95 a = state[0];
96 b = state[1];
97 c = state[2];
98 d = state[3];
99 e = state[4];
100 */
101
102 /* 4 rounds of 20 operations each. Loop unrolled. */
103 if( 1 == *(unsigned char*)&one ){
104 Rl0(a,b,c,d,e, 0); Rl0(e,a,b,c,d, 1); Rl0(d,e,a,b,c, 2); Rl0(c,d,e,a,b, 3);
105 Rl0(b,c,d,e,a, 4); Rl0(a,b,c,d,e, 5); Rl0(e,a,b,c,d, 6); Rl0(d,e,a,b,c, 7);
@@ -113,13 +132,10 @@
132 state[0] += a;
133 state[1] += b;
134 state[2] += c;
135 state[3] += d;
136 state[4] += e;
 
 
 
137 }
138
139
140 /*
141 * SHA1Init - Initialize new context
@@ -192,18 +208,17 @@
208 ** digest is stored in the first 20 bytes. zBuf should
209 ** be "char zBuf[41]".
210 */
211 static void DigestToBase16(unsigned char *digest, char *zBuf){
212 static char const zEncode[] = "0123456789abcdef";
213 int ix;
214
215 for(ix=0; ix<20; ix++){
216 *zBuf++ = zEncode[(*digest>>4)&0xf];
217 *zBuf++ = zEncode[*digest++ & 0xf];
218 }
219 *zBuf = '\0';
 
220 }
221
222 /*
223 ** The state of a incremental SHA1 checksum computation. Only one
224 ** such computation can be underway at a time, of course.
@@ -265,10 +280,21 @@
280 int sha1sum_file(const char *zFilename, Blob *pCksum){
281 FILE *in;
282 SHA1Context ctx;
283 unsigned char zResult[20];
284 char zBuf[10240];
285
286 if( file_islink(zFilename) ){
287 /* Instead of file content, return sha1 of link destination path */
288 Blob destinationPath;
289 int rc;
290
291 blob_read_link(&destinationPath, zFilename);
292 rc = sha1sum_blob(&destinationPath, pCksum);
293 blob_reset(&destinationPath);
294 return rc;
295 }
296
297 in = fossil_fopen(zFilename,"rb");
298 if( in==0 ){
299 return 1;
300 }
301
+3546 -2193
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385385
/*
386386
** Exactly one of the following macros must be defined in order to
387387
** specify which memory allocation subsystem to use.
388388
**
389389
** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
390
+** SQLITE_WIN32_MALLOC // Use Win32 native heap API
390391
** SQLITE_MEMDEBUG // Debugging version of system malloc()
392
+**
393
+** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
394
+** assert() macro is enabled, each call into the Win32 native heap subsystem
395
+** will cause HeapValidate to be called. If heap validation should fail, an
396
+** assertion will be triggered.
391397
**
392398
** (Historical note: There used to be several other options, but we've
393399
** pared it down to just these two.)
394400
**
395401
** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
396402
** the default.
397403
*/
398
-#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
404
+#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
399405
# error "At most one of the following compile-time configuration options\
400
- is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
406
+ is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
401407
#endif
402
-#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
408
+#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
403409
# define SQLITE_SYSTEM_MALLOC 1
404410
#endif
405411
406412
/*
407413
** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
650656
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651657
** [sqlite_version()] and [sqlite_source_id()].
652658
*/
653659
#define SQLITE_VERSION "3.7.8"
654660
#define SQLITE_VERSION_NUMBER 3007008
655
-#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
661
+#define SQLITE_SOURCE_ID "2011-09-04 01:27:00 6b657ae75035eb10b0ad640998d3c9eadfdffa6e"
656662
657663
/*
658664
** CAPI3REF: Run-Time Library Version Numbers
659665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
660666
**
@@ -1751,20 +1757,14 @@
17511757
** also used during testing of SQLite in order to specify an alternative
17521758
** memory allocator that simulates memory out-of-memory conditions in
17531759
** order to verify that SQLite recovers gracefully from such
17541760
** conditions.
17551761
**
1756
-** The xMalloc and xFree methods must work like the
1757
-** malloc() and free() functions from the standard C library.
1758
-** The xRealloc method must work like realloc() from the standard C library
1759
-** with the exception that if the second argument to xRealloc is zero,
1760
-** xRealloc must be a no-op - it must not perform any allocation or
1761
-** deallocation. ^SQLite guarantees that the second argument to
1762
+** The xMalloc, xRealloc, and xFree methods must work like the
1763
+** malloc(), realloc() and free() functions from the standard C library.
1764
+** ^SQLite guarantees that the second argument to
17621765
** xRealloc is always a value returned by a prior call to xRoundup.
1763
-** And so in cases where xRoundup always returns a positive number,
1764
-** xRealloc can perform exactly as the standard library realloc() and
1765
-** still be in compliance with this specification.
17661766
**
17671767
** xSize should return the allocated size of a memory allocation
17681768
** previously obtained from xMalloc or xRealloc. The allocated size
17691769
** is always at least as big as the requested size but may be larger.
17701770
**
@@ -3397,11 +3397,11 @@
33973397
** a schema change, on the first [sqlite3_step()] call following any change
33983398
** to the [sqlite3_bind_text | bindings] of that [parameter].
33993399
** ^The specific value of WHERE-clause [parameter] might influence the
34003400
** choice of query plan if the parameter is the left-hand side of a [LIKE]
34013401
** or [GLOB] operator or if the parameter is compared to an indexed column
3402
-** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3402
+** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
34033403
** the
34043404
** </li>
34053405
** </ol>
34063406
*/
34073407
SQLITE_API int sqlite3_prepare(
@@ -7711,22 +7711,10 @@
77117711
** is 0x00000000ffffffff. But because of quirks of some compilers, we
77127712
** have to specify the value in the less intuitive manner shown:
77137713
*/
77147714
#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
77157715
7716
-/*
7717
-** The datatype used to store estimates of the number of rows in a
7718
-** table or index. This is an unsigned integer type. For 99.9% of
7719
-** the world, a 32-bit integer is sufficient. But a 64-bit integer
7720
-** can be used at compile-time if desired.
7721
-*/
7722
-#ifdef SQLITE_64BIT_STATS
7723
- typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
7724
-#else
7725
- typedef u32 tRowcnt; /* 32-bit is the default */
7726
-#endif
7727
-
77287716
/*
77297717
** Macros to determine whether the machine is big or little endian,
77307718
** evaluated at runtime.
77317719
*/
77327720
#ifdef SQLITE_AMALGAMATION
@@ -8188,10 +8176,11 @@
81888176
** or VDBE. The VDBE implements an abstract machine that runs a
81898177
** simple program to access and modify the underlying database.
81908178
*/
81918179
#ifndef _SQLITE_VDBE_H_
81928180
#define _SQLITE_VDBE_H_
8181
+/* #include <stdio.h> */
81938182
81948183
/*
81958184
** A single VDBE is an opaque structure named "Vdbe". Only routines
81968185
** in the source file sqliteVdbe.c are allowed to see the insides
81978186
** of this structure.
@@ -8231,10 +8220,11 @@
82318220
Mem *pMem; /* Used when p4type is P4_MEM */
82328221
VTable *pVtab; /* Used when p4type is P4_VTAB */
82338222
KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
82348223
int *ai; /* Used when p4type is P4_INTARRAY */
82358224
SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
8225
+ int (*xAdvance)(BtCursor *, int *);
82368226
} p4;
82378227
#ifdef SQLITE_DEBUG
82388228
char *zComment; /* Comment to improve readability */
82398229
#endif
82408230
#ifdef VDBE_PROFILE
@@ -8286,10 +8276,11 @@
82868276
#define P4_REAL (-12) /* P4 is a 64-bit floating point value */
82878277
#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
82888278
#define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
82898279
#define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
82908280
#define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
8281
+#define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
82918282
82928283
/* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
82938284
** is made. That copy is freed when the Vdbe is finalized. But if the
82948285
** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
82958286
** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8401,88 +8392,90 @@
84018392
#define OP_VerifyCookie 37
84028393
#define OP_OpenRead 38
84038394
#define OP_OpenWrite 39
84048395
#define OP_OpenAutoindex 40
84058396
#define OP_OpenEphemeral 41
8406
-#define OP_OpenPseudo 42
8407
-#define OP_Close 43
8408
-#define OP_SeekLt 44
8409
-#define OP_SeekLe 45
8410
-#define OP_SeekGe 46
8411
-#define OP_SeekGt 47
8412
-#define OP_Seek 48
8413
-#define OP_NotFound 49
8414
-#define OP_Found 50
8415
-#define OP_IsUnique 51
8416
-#define OP_NotExists 52
8417
-#define OP_Sequence 53
8418
-#define OP_NewRowid 54
8419
-#define OP_Insert 55
8420
-#define OP_InsertInt 56
8421
-#define OP_Delete 57
8422
-#define OP_ResetCount 58
8423
-#define OP_RowKey 59
8424
-#define OP_RowData 60
8425
-#define OP_Rowid 61
8426
-#define OP_NullRow 62
8427
-#define OP_Last 63
8428
-#define OP_Sort 64
8429
-#define OP_Rewind 65
8430
-#define OP_Prev 66
8431
-#define OP_Next 67
8432
-#define OP_IdxInsert 70
8433
-#define OP_IdxDelete 71
8434
-#define OP_IdxRowid 72
8435
-#define OP_IdxLT 81
8436
-#define OP_IdxGE 92
8437
-#define OP_Destroy 95
8438
-#define OP_Clear 96
8439
-#define OP_CreateIndex 97
8440
-#define OP_CreateTable 98
8441
-#define OP_ParseSchema 99
8442
-#define OP_LoadAnalysis 100
8443
-#define OP_DropTable 101
8444
-#define OP_DropIndex 102
8445
-#define OP_DropTrigger 103
8446
-#define OP_IntegrityCk 104
8447
-#define OP_RowSetAdd 105
8448
-#define OP_RowSetRead 106
8449
-#define OP_RowSetTest 107
8450
-#define OP_Program 108
8451
-#define OP_Param 109
8452
-#define OP_FkCounter 110
8453
-#define OP_FkIfZero 111
8454
-#define OP_MemMax 112
8455
-#define OP_IfPos 113
8456
-#define OP_IfNeg 114
8457
-#define OP_IfZero 115
8458
-#define OP_AggStep 116
8459
-#define OP_AggFinal 117
8460
-#define OP_Checkpoint 118
8461
-#define OP_JournalMode 119
8462
-#define OP_Vacuum 120
8463
-#define OP_IncrVacuum 121
8464
-#define OP_Expire 122
8465
-#define OP_TableLock 123
8466
-#define OP_VBegin 124
8467
-#define OP_VCreate 125
8468
-#define OP_VDestroy 126
8469
-#define OP_VOpen 127
8470
-#define OP_VFilter 128
8471
-#define OP_VColumn 129
8472
-#define OP_VNext 131
8473
-#define OP_VRename 132
8474
-#define OP_VUpdate 133
8475
-#define OP_Pagecount 134
8476
-#define OP_MaxPgcnt 135
8477
-#define OP_Trace 136
8478
-#define OP_Noop 137
8479
-#define OP_Explain 138
8480
-
8481
-/* The following opcode values are never used */
8482
-#define OP_NotUsed_139 139
8483
-#define OP_NotUsed_140 140
8397
+#define OP_SorterOpen 42
8398
+#define OP_OpenPseudo 43
8399
+#define OP_Close 44
8400
+#define OP_SeekLt 45
8401
+#define OP_SeekLe 46
8402
+#define OP_SeekGe 47
8403
+#define OP_SeekGt 48
8404
+#define OP_Seek 49
8405
+#define OP_NotFound 50
8406
+#define OP_Found 51
8407
+#define OP_IsUnique 52
8408
+#define OP_NotExists 53
8409
+#define OP_Sequence 54
8410
+#define OP_NewRowid 55
8411
+#define OP_Insert 56
8412
+#define OP_InsertInt 57
8413
+#define OP_Delete 58
8414
+#define OP_ResetCount 59
8415
+#define OP_SorterCompare 60
8416
+#define OP_SorterData 61
8417
+#define OP_RowKey 62
8418
+#define OP_RowData 63
8419
+#define OP_Rowid 64
8420
+#define OP_NullRow 65
8421
+#define OP_Last 66
8422
+#define OP_SorterSort 67
8423
+#define OP_Sort 70
8424
+#define OP_Rewind 71
8425
+#define OP_SorterNext 72
8426
+#define OP_Prev 81
8427
+#define OP_Next 92
8428
+#define OP_SorterInsert 95
8429
+#define OP_IdxInsert 96
8430
+#define OP_IdxDelete 97
8431
+#define OP_IdxRowid 98
8432
+#define OP_IdxLT 99
8433
+#define OP_IdxGE 100
8434
+#define OP_Destroy 101
8435
+#define OP_Clear 102
8436
+#define OP_CreateIndex 103
8437
+#define OP_CreateTable 104
8438
+#define OP_ParseSchema 105
8439
+#define OP_LoadAnalysis 106
8440
+#define OP_DropTable 107
8441
+#define OP_DropIndex 108
8442
+#define OP_DropTrigger 109
8443
+#define OP_IntegrityCk 110
8444
+#define OP_RowSetAdd 111
8445
+#define OP_RowSetRead 112
8446
+#define OP_RowSetTest 113
8447
+#define OP_Program 114
8448
+#define OP_Param 115
8449
+#define OP_FkCounter 116
8450
+#define OP_FkIfZero 117
8451
+#define OP_MemMax 118
8452
+#define OP_IfPos 119
8453
+#define OP_IfNeg 120
8454
+#define OP_IfZero 121
8455
+#define OP_AggStep 122
8456
+#define OP_AggFinal 123
8457
+#define OP_Checkpoint 124
8458
+#define OP_JournalMode 125
8459
+#define OP_Vacuum 126
8460
+#define OP_IncrVacuum 127
8461
+#define OP_Expire 128
8462
+#define OP_TableLock 129
8463
+#define OP_VBegin 131
8464
+#define OP_VCreate 132
8465
+#define OP_VDestroy 133
8466
+#define OP_VOpen 134
8467
+#define OP_VFilter 135
8468
+#define OP_VColumn 136
8469
+#define OP_VNext 137
8470
+#define OP_VRename 138
8471
+#define OP_VUpdate 139
8472
+#define OP_Pagecount 140
8473
+#define OP_MaxPgcnt 146
8474
+#define OP_Trace 147
8475
+#define OP_Noop 148
8476
+#define OP_Explain 149
84848477
84858478
84868479
/* Properties such as "out2" or "jump" that are specified in
84878480
** comments following the "case" for each opcode in the vdbe.c
84888481
** are encoded into bitvectors as follows:
@@ -8498,24 +8491,24 @@
84988491
/* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
84998492
/* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
85008493
/* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
85018494
/* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
85028495
/* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8503
-/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8504
-/* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8505
-/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8506
-/* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8507
-/* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8496
+/* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\
8497
+/* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\
8498
+/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8499
+/* 64 */ 0x02, 0x00, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
8500
+/* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
85088501
/* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509
-/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8510
-/* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8511
-/* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8512
-/* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8513
-/* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514
-/* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8515
-/* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8516
-/* 144 */ 0x04, 0x04,}
8502
+/* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x08,\
8503
+/* 96 */ 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x02,\
8504
+/* 104 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c,\
8505
+/* 112 */ 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08, 0x05,\
8506
+/* 120 */ 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8507
+/* 128 */ 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01,\
8508
+/* 136 */ 0x00, 0x01, 0x00, 0x00, 0x02, 0x04, 0x04, 0x04,\
8509
+/* 144 */ 0x04, 0x04, 0x02, 0x00, 0x00, 0x00,}
85178510
85188511
/************** End of opcodes.h *********************************************/
85198512
/************** Continuing where we left off in vdbe.h ***********************/
85208513
85218514
/*
@@ -8529,13 +8522,13 @@
85298522
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
85308523
SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
85318524
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
85328525
SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
85338526
SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8534
-SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8535
-SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8536
-SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8527
+SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
8528
+SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
8529
+SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
85378530
SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
85388531
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
85398532
SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
85408533
SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
85418534
SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8646,11 @@
86538646
** NOTE: These values must match the corresponding BTREE_ values in btree.h.
86548647
*/
86558648
#define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
86568649
#define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
86578650
#define PAGER_MEMORY 0x0004 /* In-memory database */
8651
+#define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */
86588652
86598653
/*
86608654
** Valid values for the second argument to sqlite3PagerLockingMode().
86618655
*/
86628656
#define PAGER_LOCKINGMODE_QUERY -1
@@ -9924,11 +9918,11 @@
99249918
int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
99259919
int nCol; /* Number of columns in this table */
99269920
Column *aCol; /* Information about each column */
99279921
Index *pIndex; /* List of SQL indexes on this table. */
99289922
int tnum; /* Root BTree node for this table (see note above) */
9929
- tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9923
+ unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
99309924
Select *pSelect; /* NULL for tables. Points to definition if a view. */
99319925
u16 nRef; /* Number of pointers to this Table */
99329926
u8 tabFlags; /* Mask of TF_* values */
99339927
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
99349928
FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10117,35 @@
1012310117
*/
1012410118
struct Index {
1012510119
char *zName; /* Name of this index */
1012610120
int nColumn; /* Number of columns in the table used by this index */
1012710121
int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10128
- tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10122
+ unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
1012910123
Table *pTable; /* The SQL table being indexed */
1013010124
int tnum; /* Page containing root of this index in database file */
1013110125
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1013210126
u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
1013310127
u8 bUnordered; /* Use this index for == or IN queries only */
10134
- u8 nSample; /* Number of elements in aSample[] */
1013510128
char *zColAff; /* String defining the affinity of each column */
1013610129
Index *pNext; /* The next index associated with the same table */
1013710130
Schema *pSchema; /* Schema containing this index */
1013810131
u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
1013910132
char **azColl; /* Array of collation sequence names for index */
10140
-#ifdef SQLITE_ENABLE_STAT3
10141
- tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10142
- IndexSample *aSample; /* Samples of the left-most key */
10143
-#endif
10133
+ IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
1014410134
};
1014510135
1014610136
/*
1014710137
** Each sample stored in the sqlite_stat2 table is represented in memory
1014810138
** using a structure of this type.
1014910139
*/
1015010140
struct IndexSample {
1015110141
union {
1015210142
char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10153
- double r; /* Value if eType is SQLITE_FLOAT */
10154
- i64 i; /* Value if eType is SQLITE_INTEGER */
10143
+ double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
1015510144
} u;
1015610145
u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10157
- u16 nByte; /* Size in byte of text or blob. */
10158
- tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10159
- tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10160
- tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10146
+ u8 nByte; /* Size in byte of text or blob. */
1016110147
};
1016210148
1016310149
/*
1016410150
** Each token coming out of the lexer is an instance of
1016510151
** this structure. Tokens are also used as part of an expression.
@@ -10190,10 +10176,11 @@
1019010176
u8 directMode; /* Direct rendering mode means take data directly
1019110177
** from source tables rather than from accumulators */
1019210178
u8 useSortingIdx; /* In direct mode, reference the sorting index rather
1019310179
** than the source table */
1019410180
int sortingIdx; /* Cursor number of the sorting index */
10181
+ int sortingIdxPTab; /* Cursor number of pseudo-table */
1019510182
ExprList *pGroupBy; /* The group by clause */
1019610183
int nSortingColumn; /* Number of columns in the sorting index */
1019710184
struct AggInfo_col { /* For each column used in source tables */
1019810185
Table *pTab; /* Source table */
1019910186
int iTable; /* Cursor number of the source table */
@@ -10722,10 +10709,11 @@
1072210709
#define SF_Resolved 0x0002 /* Identifiers have been resolved */
1072310710
#define SF_Aggregate 0x0004 /* Contains aggregate functions */
1072410711
#define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
1072510712
#define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
1072610713
#define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
10714
+#define SF_UseSorter 0x0040 /* Sort using a sorter */
1072710715
1072810716
1072910717
/*
1073010718
** The results of a select can be distributed in several ways. The
1073110719
** "SRT" prefix means "SELECT Result Type".
@@ -11361,11 +11349,10 @@
1136111349
#else
1136211350
# define sqlite3ViewGetColumnNames(A,B) 0
1136311351
#endif
1136411352
1136511353
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11366
-SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
1136711354
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
1136811355
#ifndef SQLITE_OMIT_AUTOINCREMENT
1136911356
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
1137011357
SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
1137111358
#else
@@ -11618,11 +11605,11 @@
1161811605
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1161911606
void(*)(void*));
1162011607
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1162111608
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1162211609
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11623
-#ifdef SQLITE_ENABLE_STAT3
11610
+#ifdef SQLITE_ENABLE_STAT2
1162411611
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
1162511612
#endif
1162611613
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1162711614
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1162811615
#ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12232,10 @@
1224512232
"ENABLE_RTREE",
1224612233
#endif
1224712234
#ifdef SQLITE_ENABLE_STAT2
1224812235
"ENABLE_STAT2",
1224912236
#endif
12250
-#ifdef SQLITE_ENABLE_STAT3
12251
- "ENABLE_STAT3",
12252
-#endif
1225312237
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1225412238
"ENABLE_UNLOCK_NOTIFY",
1225512239
#endif
1225612240
#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
1225712241
"ENABLE_UPDATE_DELETE_LIMIT",
@@ -12275,10 +12259,13 @@
1227512259
"INT64_TYPE",
1227612260
#endif
1227712261
#ifdef SQLITE_LOCK_TRACE
1227812262
"LOCK_TRACE",
1227912263
#endif
12264
+#ifdef SQLITE_MAX_SCHEMA_RETRY
12265
+ "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
12266
+#endif
1228012267
#ifdef SQLITE_MEMDEBUG
1228112268
"MEMDEBUG",
1228212269
#endif
1228312270
#ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
1228412271
"MIXED_ENDIAN_64BIT_FLOAT",
@@ -12387,10 +12374,13 @@
1238712374
#ifdef SQLITE_OMIT_LOOKASIDE
1238812375
"OMIT_LOOKASIDE",
1238912376
#endif
1239012377
#ifdef SQLITE_OMIT_MEMORYDB
1239112378
"OMIT_MEMORYDB",
12379
+#endif
12380
+#ifdef SQLITE_OMIT_MERGE_SORT
12381
+ "OMIT_MERGE_SORT",
1239212382
#endif
1239312383
#ifdef SQLITE_OMIT_OR_OPTIMIZATION
1239412384
"OMIT_OR_OPTIMIZATION",
1239512385
#endif
1239612386
#ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12443,13 @@
1245312443
#ifdef SQLITE_OMIT_WSD
1245412444
"OMIT_WSD",
1245512445
#endif
1245612446
#ifdef SQLITE_OMIT_XFER_OPT
1245712447
"OMIT_XFER_OPT",
12448
+#endif
12449
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
12450
+ "PAGECACHE_BLOCKALLOC",
1245812451
#endif
1245912452
#ifdef SQLITE_PERFORMANCE_TRACE
1246012453
"PERFORMANCE_TRACE",
1246112454
#endif
1246212455
#ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12567,13 @@
1257412567
/*
1257512568
** Boolean values
1257612569
*/
1257712570
typedef unsigned char Bool;
1257812571
12572
+/* Opaque type used by code in vdbesort.c */
12573
+typedef struct VdbeSorter VdbeSorter;
12574
+
1257912575
/*
1258012576
** A cursor is a pointer into a single BTree within a database file.
1258112577
** The cursor can seek to a BTree entry with a particular key, or
1258212578
** loop over all entries of the Btree. You can also insert new BTree
1258312579
** entries or retrieve the key or data from the entry that the cursor
@@ -12600,15 +12596,17 @@
1260012596
Bool nullRow; /* True if pointing to a row with no data */
1260112597
Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
1260212598
Bool isTable; /* True if a table requiring integer keys */
1260312599
Bool isIndex; /* True if an index containing keys only - no data */
1260412600
Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */
12601
+ Bool isSorter; /* True if a new-style sorter */
1260512602
sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
1260612603
const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
1260712604
i64 seqCount; /* Sequence counter */
1260812605
i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
1260912606
i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
12607
+ VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
1261012608
1261112609
/* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
1261212610
** OP_IsUnique opcode on this cursor. */
1261312611
int seekResult;
1261412612
@@ -12924,17 +12922,38 @@
1292412922
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
1292512923
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
1292612924
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
1292712925
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
1292812926
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12927
+#define MemReleaseExt(X) \
12928
+ if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12929
+ sqlite3VdbeMemReleaseExternal(X);
1292912930
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
1293012931
SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
1293112932
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
1293212933
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
1293312934
SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
1293412935
SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
1293512936
SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12937
+
12938
+#ifdef SQLITE_OMIT_MERGE_SORT
12939
+# define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
12940
+# define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
12941
+# define sqlite3VdbeSorterClose(Y,Z)
12942
+# define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
12943
+# define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
12944
+# define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
12945
+# define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
12946
+#else
12947
+SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
12948
+SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
12949
+SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
12950
+SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
12951
+SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
12952
+SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
12953
+SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
12954
+#endif
1293612955
1293712956
#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
1293812957
SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
1293912958
SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
1294012959
#else
@@ -13225,10 +13244,12 @@
1322513244
** Astronomical Algorithms, 2nd Edition, 1998
1322613245
** ISBM 0-943396-61-1
1322713246
** Willmann-Bell, Inc
1322813247
** Richmond, Virginia (USA)
1322913248
*/
13249
+/* #include <stdlib.h> */
13250
+/* #include <assert.h> */
1323013251
#include <time.h>
1323113252
1323213253
#ifndef SQLITE_OMIT_DATETIME_FUNCS
1323313254
1323413255
@@ -14978,10 +14999,11 @@
1497814999
extern void backtrace_symbols_fd(void*const*,int,int);
1497915000
#else
1498015001
# define backtrace(A,B) 1
1498115002
# define backtrace_symbols_fd(A,B,C)
1498215003
#endif
15004
+/* #include <stdio.h> */
1498315005
1498415006
/*
1498515007
** Each memory allocation looks like this:
1498615008
**
1498715009
** ------------------------------------------------------------------------
@@ -18081,10 +18103,11 @@
1808118103
**
1808218104
*************************************************************************
1808318105
**
1808418106
** Memory allocation functions used throughout sqlite.
1808518107
*/
18108
+/* #include <stdarg.h> */
1808618109
1808718110
/*
1808818111
** Attempt to release up to n bytes of non-essential memory currently
1808918112
** held by SQLite. An example of non-essential memory is memory used to
1809018113
** cache database pages that are not currently in use.
@@ -20058,10 +20081,11 @@
2005820081
** BOM or Byte Order Mark:
2005920082
** 0xff 0xfe little-endian utf-16 follows
2006020083
** 0xfe 0xff big-endian utf-16 follows
2006120084
**
2006220085
*/
20086
+/* #include <assert.h> */
2006320087
2006420088
#ifndef SQLITE_AMALGAMATION
2006520089
/*
2006620090
** The following constant value is used by the SQLITE_BIGENDIAN and
2006720091
** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20510,11 @@
2048620510
** no longer required.
2048720511
**
2048820512
** If a malloc failure occurs, NULL is returned and the db.mallocFailed
2048920513
** flag set.
2049020514
*/
20491
-#ifdef SQLITE_ENABLE_STAT3
20515
+#ifdef SQLITE_ENABLE_STAT2
2049220516
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
2049320517
Mem m;
2049420518
memset(&m, 0, sizeof(m));
2049520519
m.db = db;
2049620520
sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20624,11 @@
2060020624
**
2060120625
** This file contains functions for allocating memory, comparing
2060220626
** strings, and stuff like that.
2060320627
**
2060420628
*/
20629
+/* #include <stdarg.h> */
2060520630
#ifdef SQLITE_HAVE_ISNAN
2060620631
# include <math.h>
2060720632
#endif
2060820633
2060920634
/*
@@ -21778,10 +21803,11 @@
2177821803
**
2177921804
*************************************************************************
2178021805
** This is the implementation of generic hash-tables
2178121806
** used in SQLite.
2178221807
*/
21808
+/* #include <assert.h> */
2178321809
2178421810
/* Turn bulk memory into a hash table object by initializing the
2178521811
** fields of the Hash structure.
2178621812
**
2178721813
** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22088,50 +22114,50 @@
2208822114
/* 37 */ "VerifyCookie",
2208922115
/* 38 */ "OpenRead",
2209022116
/* 39 */ "OpenWrite",
2209122117
/* 40 */ "OpenAutoindex",
2209222118
/* 41 */ "OpenEphemeral",
22093
- /* 42 */ "OpenPseudo",
22094
- /* 43 */ "Close",
22095
- /* 44 */ "SeekLt",
22096
- /* 45 */ "SeekLe",
22097
- /* 46 */ "SeekGe",
22098
- /* 47 */ "SeekGt",
22099
- /* 48 */ "Seek",
22100
- /* 49 */ "NotFound",
22101
- /* 50 */ "Found",
22102
- /* 51 */ "IsUnique",
22103
- /* 52 */ "NotExists",
22104
- /* 53 */ "Sequence",
22105
- /* 54 */ "NewRowid",
22106
- /* 55 */ "Insert",
22107
- /* 56 */ "InsertInt",
22108
- /* 57 */ "Delete",
22109
- /* 58 */ "ResetCount",
22110
- /* 59 */ "RowKey",
22111
- /* 60 */ "RowData",
22112
- /* 61 */ "Rowid",
22113
- /* 62 */ "NullRow",
22114
- /* 63 */ "Last",
22115
- /* 64 */ "Sort",
22116
- /* 65 */ "Rewind",
22117
- /* 66 */ "Prev",
22118
- /* 67 */ "Next",
22119
+ /* 42 */ "SorterOpen",
22120
+ /* 43 */ "OpenPseudo",
22121
+ /* 44 */ "Close",
22122
+ /* 45 */ "SeekLt",
22123
+ /* 46 */ "SeekLe",
22124
+ /* 47 */ "SeekGe",
22125
+ /* 48 */ "SeekGt",
22126
+ /* 49 */ "Seek",
22127
+ /* 50 */ "NotFound",
22128
+ /* 51 */ "Found",
22129
+ /* 52 */ "IsUnique",
22130
+ /* 53 */ "NotExists",
22131
+ /* 54 */ "Sequence",
22132
+ /* 55 */ "NewRowid",
22133
+ /* 56 */ "Insert",
22134
+ /* 57 */ "InsertInt",
22135
+ /* 58 */ "Delete",
22136
+ /* 59 */ "ResetCount",
22137
+ /* 60 */ "SorterCompare",
22138
+ /* 61 */ "SorterData",
22139
+ /* 62 */ "RowKey",
22140
+ /* 63 */ "RowData",
22141
+ /* 64 */ "Rowid",
22142
+ /* 65 */ "NullRow",
22143
+ /* 66 */ "Last",
22144
+ /* 67 */ "SorterSort",
2211922145
/* 68 */ "Or",
2212022146
/* 69 */ "And",
22121
- /* 70 */ "IdxInsert",
22122
- /* 71 */ "IdxDelete",
22123
- /* 72 */ "IdxRowid",
22147
+ /* 70 */ "Sort",
22148
+ /* 71 */ "Rewind",
22149
+ /* 72 */ "SorterNext",
2212422150
/* 73 */ "IsNull",
2212522151
/* 74 */ "NotNull",
2212622152
/* 75 */ "Ne",
2212722153
/* 76 */ "Eq",
2212822154
/* 77 */ "Gt",
2212922155
/* 78 */ "Le",
2213022156
/* 79 */ "Lt",
2213122157
/* 80 */ "Ge",
22132
- /* 81 */ "IdxLT",
22158
+ /* 81 */ "Prev",
2213322159
/* 82 */ "BitAnd",
2213422160
/* 83 */ "BitOr",
2213522161
/* 84 */ "ShiftLeft",
2213622162
/* 85 */ "ShiftRight",
2213722163
/* 86 */ "Add",
@@ -22138,64 +22164,68 @@
2213822164
/* 87 */ "Subtract",
2213922165
/* 88 */ "Multiply",
2214022166
/* 89 */ "Divide",
2214122167
/* 90 */ "Remainder",
2214222168
/* 91 */ "Concat",
22143
- /* 92 */ "IdxGE",
22169
+ /* 92 */ "Next",
2214422170
/* 93 */ "BitNot",
2214522171
/* 94 */ "String8",
22146
- /* 95 */ "Destroy",
22147
- /* 96 */ "Clear",
22148
- /* 97 */ "CreateIndex",
22149
- /* 98 */ "CreateTable",
22150
- /* 99 */ "ParseSchema",
22151
- /* 100 */ "LoadAnalysis",
22152
- /* 101 */ "DropTable",
22153
- /* 102 */ "DropIndex",
22154
- /* 103 */ "DropTrigger",
22155
- /* 104 */ "IntegrityCk",
22156
- /* 105 */ "RowSetAdd",
22157
- /* 106 */ "RowSetRead",
22158
- /* 107 */ "RowSetTest",
22159
- /* 108 */ "Program",
22160
- /* 109 */ "Param",
22161
- /* 110 */ "FkCounter",
22162
- /* 111 */ "FkIfZero",
22163
- /* 112 */ "MemMax",
22164
- /* 113 */ "IfPos",
22165
- /* 114 */ "IfNeg",
22166
- /* 115 */ "IfZero",
22167
- /* 116 */ "AggStep",
22168
- /* 117 */ "AggFinal",
22169
- /* 118 */ "Checkpoint",
22170
- /* 119 */ "JournalMode",
22171
- /* 120 */ "Vacuum",
22172
- /* 121 */ "IncrVacuum",
22173
- /* 122 */ "Expire",
22174
- /* 123 */ "TableLock",
22175
- /* 124 */ "VBegin",
22176
- /* 125 */ "VCreate",
22177
- /* 126 */ "VDestroy",
22178
- /* 127 */ "VOpen",
22179
- /* 128 */ "VFilter",
22180
- /* 129 */ "VColumn",
22172
+ /* 95 */ "SorterInsert",
22173
+ /* 96 */ "IdxInsert",
22174
+ /* 97 */ "IdxDelete",
22175
+ /* 98 */ "IdxRowid",
22176
+ /* 99 */ "IdxLT",
22177
+ /* 100 */ "IdxGE",
22178
+ /* 101 */ "Destroy",
22179
+ /* 102 */ "Clear",
22180
+ /* 103 */ "CreateIndex",
22181
+ /* 104 */ "CreateTable",
22182
+ /* 105 */ "ParseSchema",
22183
+ /* 106 */ "LoadAnalysis",
22184
+ /* 107 */ "DropTable",
22185
+ /* 108 */ "DropIndex",
22186
+ /* 109 */ "DropTrigger",
22187
+ /* 110 */ "IntegrityCk",
22188
+ /* 111 */ "RowSetAdd",
22189
+ /* 112 */ "RowSetRead",
22190
+ /* 113 */ "RowSetTest",
22191
+ /* 114 */ "Program",
22192
+ /* 115 */ "Param",
22193
+ /* 116 */ "FkCounter",
22194
+ /* 117 */ "FkIfZero",
22195
+ /* 118 */ "MemMax",
22196
+ /* 119 */ "IfPos",
22197
+ /* 120 */ "IfNeg",
22198
+ /* 121 */ "IfZero",
22199
+ /* 122 */ "AggStep",
22200
+ /* 123 */ "AggFinal",
22201
+ /* 124 */ "Checkpoint",
22202
+ /* 125 */ "JournalMode",
22203
+ /* 126 */ "Vacuum",
22204
+ /* 127 */ "IncrVacuum",
22205
+ /* 128 */ "Expire",
22206
+ /* 129 */ "TableLock",
2218122207
/* 130 */ "Real",
22182
- /* 131 */ "VNext",
22183
- /* 132 */ "VRename",
22184
- /* 133 */ "VUpdate",
22185
- /* 134 */ "Pagecount",
22186
- /* 135 */ "MaxPgcnt",
22187
- /* 136 */ "Trace",
22188
- /* 137 */ "Noop",
22189
- /* 138 */ "Explain",
22190
- /* 139 */ "NotUsed_139",
22191
- /* 140 */ "NotUsed_140",
22208
+ /* 131 */ "VBegin",
22209
+ /* 132 */ "VCreate",
22210
+ /* 133 */ "VDestroy",
22211
+ /* 134 */ "VOpen",
22212
+ /* 135 */ "VFilter",
22213
+ /* 136 */ "VColumn",
22214
+ /* 137 */ "VNext",
22215
+ /* 138 */ "VRename",
22216
+ /* 139 */ "VUpdate",
22217
+ /* 140 */ "Pagecount",
2219222218
/* 141 */ "ToText",
2219322219
/* 142 */ "ToBlob",
2219422220
/* 143 */ "ToNumeric",
2219522221
/* 144 */ "ToInt",
2219622222
/* 145 */ "ToReal",
22223
+ /* 146 */ "MaxPgcnt",
22224
+ /* 147 */ "Trace",
22225
+ /* 148 */ "Noop",
22226
+ /* 149 */ "Explain",
2219722227
};
2219822228
return azName[i];
2219922229
}
2220022230
#endif
2220122231
@@ -22286,11 +22316,11 @@
2228622316
*/
2228722317
#ifdef MEMORY_DEBUG
2228822318
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2228922319
#endif
2229022320
22291
-#ifdef SQLITE_DEBUG
22321
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
2229222322
# ifndef SQLITE_DEBUG_OS_TRACE
2229322323
# define SQLITE_DEBUG_OS_TRACE 0
2229422324
# endif
2229522325
int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
2229622326
# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -24450,10 +24480,11 @@
2445024480
*/
2445124481
#include <sys/types.h>
2445224482
#include <sys/stat.h>
2445324483
#include <fcntl.h>
2445424484
#include <unistd.h>
24485
+/* #include <time.h> */
2445524486
#include <sys/time.h>
2445624487
#include <errno.h>
2445724488
#ifndef SQLITE_OMIT_WAL
2445824489
#include <sys/mman.h>
2445924490
#endif
@@ -24485,10 +24516,11 @@
2448524516
/*
2448624517
** If we are to be thread-safe, include the pthreads header and define
2448724518
** the SQLITE_UNIX_THREADS macro.
2448824519
*/
2448924520
#if SQLITE_THREADSAFE
24521
+/* # include <pthread.h> */
2449024522
# define SQLITE_UNIX_THREADS 1
2449124523
#endif
2449224524
2449324525
/*
2449424526
** Default permissions when creating a new file
@@ -24584,11 +24616,15 @@
2458424616
** Allowed values for the unixFile.ctrlFlags bitmask:
2458524617
*/
2458624618
#define UNIXFILE_EXCL 0x01 /* Connections from one process only */
2458724619
#define UNIXFILE_RDONLY 0x02 /* Connection is read only */
2458824620
#define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24589
-#define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24621
+#ifndef SQLITE_DISABLE_DIRSYNC
24622
+# define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24623
+#else
24624
+# define UNIXFILE_DIRSYNC 0x00
24625
+#endif
2459024626
2459124627
/*
2459224628
** Include code that is common to all os_*.c files
2459324629
*/
2459424630
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -24622,11 +24658,11 @@
2462224658
*/
2462324659
#ifdef MEMORY_DEBUG
2462424660
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
2462524661
#endif
2462624662
24627
-#ifdef SQLITE_DEBUG
24663
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
2462824664
# ifndef SQLITE_DEBUG_OS_TRACE
2462924665
# define SQLITE_DEBUG_OS_TRACE 0
2463024666
# endif
2463124667
int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
2463224668
# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -27061,15 +27097,16 @@
2706127097
*/
2706227098
static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
2706327099
int rc = SQLITE_OK;
2706427100
int reserved = 0;
2706527101
unixFile *pFile = (unixFile*)id;
27102
+ afpLockingContext *context;
2706627103
2706727104
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
2706827105
2706927106
assert( pFile );
27070
- afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27107
+ context = (afpLockingContext *) pFile->lockingContext;
2707127108
if( context->reserved ){
2707227109
*pResOut = 1;
2707327110
return SQLITE_OK;
2707427111
}
2707527112
unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27242,11 @@
2720527242
2720627243
/* If control gets to this point, then actually go ahead and make
2720727244
** operating system calls for the specified lock.
2720827245
*/
2720927246
if( eFileLock==SHARED_LOCK ){
27210
- int lrc1, lrc2, lrc1Errno;
27247
+ int lrc1, lrc2, lrc1Errno = 0;
2721127248
long lk, mask;
2721227249
2721327250
assert( pInode->nShared==0 );
2721427251
assert( pInode->eFileLock==0 );
2721527252
@@ -27579,21 +27616,23 @@
2757927616
#if defined(USE_PREAD)
2758027617
do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
2758127618
#elif defined(USE_PREAD64)
2758227619
do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
2758327620
#else
27584
- newOffset = lseek(id->h, offset, SEEK_SET);
27585
- SimulateIOError( newOffset-- );
27586
- if( newOffset!=offset ){
27587
- if( newOffset == -1 ){
27588
- ((unixFile*)id)->lastErrno = errno;
27589
- }else{
27590
- ((unixFile*)id)->lastErrno = 0;
27591
- }
27592
- return -1;
27593
- }
27594
- do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
27621
+ do{
27622
+ newOffset = lseek(id->h, offset, SEEK_SET);
27623
+ SimulateIOError( newOffset-- );
27624
+ if( newOffset!=offset ){
27625
+ if( newOffset == -1 ){
27626
+ ((unixFile*)id)->lastErrno = errno;
27627
+ }else{
27628
+ ((unixFile*)id)->lastErrno = 0;
27629
+ }
27630
+ return -1;
27631
+ }
27632
+ got = osWrite(id->h, pBuf, cnt);
27633
+ }while( got<0 && errno==EINTR );
2759527634
#endif
2759627635
TIMER_END;
2759727636
if( got<0 ){
2759827637
((unixFile*)id)->lastErrno = errno;
2759927638
}
@@ -27679,15 +27718,15 @@
2767927718
SQLITE_API int sqlite3_fullsync_count = 0;
2768027719
#endif
2768127720
2768227721
/*
2768327722
** We do not trust systems to provide a working fdatasync(). Some do.
27684
-** Others do no. To be safe, we will stick with the (slower) fsync().
27685
-** If you know that your system does support fdatasync() correctly,
27723
+** Others do no. To be safe, we will stick with the (slightly slower)
27724
+** fsync(). If you know that your system does support fdatasync() correctly,
2768627725
** then simply compile with -Dfdatasync=fdatasync
2768727726
*/
27688
-#if !defined(fdatasync) && !defined(__linux__)
27727
+#if !defined(fdatasync)
2768927728
# define fdatasync fsync
2769027729
#endif
2769127730
2769227731
/*
2769327732
** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
@@ -27888,10 +27927,12 @@
2788827927
HAVE_FULLFSYNC, isFullsync));
2788927928
rc = osOpenDirectory(pFile->zPath, &dirfd);
2789027929
if( rc==SQLITE_OK && dirfd>=0 ){
2789127930
full_fsync(dirfd, 0, 0);
2789227931
robust_close(pFile, dirfd, __LINE__);
27932
+ }else if( rc==SQLITE_CANTOPEN ){
27933
+ rc = SQLITE_OK;
2789327934
}
2789427935
pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
2789527936
}
2789627937
return rc;
2789727938
}
@@ -27971,30 +28012,22 @@
2797128012
static int proxyFileControl(sqlite3_file*,int,void*);
2797228013
#endif
2797328014
2797428015
/*
2797528016
** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27976
-** file-control operation.
27977
-**
27978
-** If the user has configured a chunk-size for this file, it could be
27979
-** that the file needs to be extended at this point. Otherwise, the
27980
-** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
28017
+** file-control operation. Enlarge the database to nBytes in size
28018
+** (rounded up to the next chunk-size). If the database is already
28019
+** nBytes or larger, this routine is a no-op.
2798128020
*/
2798228021
static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27983
- { /* preserve indentation of removed "if" */
28022
+ if( pFile->szChunk>0 ){
2798428023
i64 nSize; /* Required file size */
27985
- i64 szChunk; /* Chunk size */
2798628024
struct stat buf; /* Used to hold return values of fstat() */
2798728025
2798828026
if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
2798928027
27990
- szChunk = pFile->szChunk;
27991
- if( szChunk==0 ){
27992
- nSize = nByte;
27993
- }else{
27994
- nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
27995
- }
28028
+ nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
2799628029
if( nSize>(i64)buf.st_size ){
2799728030
2799828031
#if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
2799928032
/* The code below is handling the return value of osFallocate()
2800028033
** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28081,15 @@
2804828081
case SQLITE_FCNTL_CHUNK_SIZE: {
2804928082
pFile->szChunk = *(int *)pArg;
2805028083
return SQLITE_OK;
2805128084
}
2805228085
case SQLITE_FCNTL_SIZE_HINT: {
28053
- return fcntlSizeHint(pFile, *(i64 *)pArg);
28086
+ int rc;
28087
+ SimulateIOErrorBenign(1);
28088
+ rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28089
+ SimulateIOErrorBenign(0);
28090
+ return rc;
2805428091
}
2805528092
case SQLITE_FCNTL_PERSIST_WAL: {
2805628093
int bPersist = *(int*)pArg;
2805728094
if( bPersist<0 ){
2805828095
*(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -28175,15 +28212,13 @@
2817528212
*/
2817628213
struct unixShm {
2817728214
unixShmNode *pShmNode; /* The underlying unixShmNode object */
2817828215
unixShm *pNext; /* Next unixShm with the same unixShmNode */
2817928216
u8 hasMutex; /* True if holding the unixShmNode mutex */
28217
+ u8 id; /* Id of this connection within its unixShmNode */
2818028218
u16 sharedMask; /* Mask of shared locks held */
2818128219
u16 exclMask; /* Mask of exclusive locks held */
28182
-#ifdef SQLITE_DEBUG
28183
- u8 id; /* Id of this connection within its unixShmNode */
28184
-#endif
2818528220
};
2818628221
2818728222
/*
2818828223
** Constants used for locking
2818928224
*/
@@ -29485,10 +29520,13 @@
2948529520
int isReadonly = (flags & SQLITE_OPEN_READONLY);
2948629521
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
2948729522
#if SQLITE_ENABLE_LOCKING_STYLE
2948829523
int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
2948929524
#endif
29525
+#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29526
+ struct statfs fsInfo;
29527
+#endif
2949029528
2949129529
/* If creating a master or main-file journal, this function will open
2949229530
** a file-descriptor on the directory too. The first time unixSync()
2949329531
** is called the directory file descriptor will be fsync()ed and close()d.
2949429532
*/
@@ -29617,11 +29655,10 @@
2961729655
2961829656
noLock = eType!=SQLITE_OPEN_MAIN_DB;
2961929657
2962029658
2962129659
#if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29622
- struct statfs fsInfo;
2962329660
if( fstatfs(fd, &fsInfo) == -1 ){
2962429661
((unixFile*)pFile)->lastErrno = errno;
2962529662
robust_close(p, fd, __LINE__);
2962629663
return SQLITE_IOERR_ACCESS;
2962729664
}
@@ -29641,11 +29678,10 @@
2964129678
/* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
2964229679
** never use proxy, NULL means use proxy for non-local files only. */
2964329680
if( envforce!=NULL ){
2964429681
useProxy = atoi(envforce)>0;
2964529682
}else{
29646
- struct statfs fsInfo;
2964729683
if( statfs(zPath, &fsInfo) == -1 ){
2964829684
/* In theory, the close(fd) call is sub-optimal. If the file opened
2964929685
** with fd is a database file, and there are other connections open
2965029686
** on that file that are currently holding advisory locks on it,
2965129687
** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
2971529751
#endif
2971629752
{
2971729753
rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
2971829754
}
2971929755
robust_close(0, fd, __LINE__);
29756
+ }else if( rc==SQLITE_CANTOPEN ){
29757
+ rc = SQLITE_OK;
2972029758
}
2972129759
}
2972229760
#endif
2972329761
return rc;
2972429762
}
@@ -30380,10 +30418,12 @@
3038030418
*pError = err;
3038130419
}
3038230420
return SQLITE_IOERR;
3038330421
}
3038430422
}
30423
+#else
30424
+ UNUSED_PARAMETER(pError);
3038530425
#endif
3038630426
#ifdef SQLITE_TEST
3038730427
/* simulate multiple hosts by creating unique hostid file paths */
3038830428
if( sqlite3_hostid_num != 0){
3038930429
pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
3047230512
unixFile *conchFile = pCtx->conchFile;
3047330513
int rc = SQLITE_OK;
3047430514
int nTries = 0;
3047530515
struct timespec conchModTime;
3047630516
30517
+ memset(&conchModTime, 0, sizeof(conchModTime));
3047730518
do {
3047830519
rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
3047930520
nTries ++;
3048030521
if( rc==SQLITE_BUSY ){
3048130522
/* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
3070330744
conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
3070430745
3070530746
end_takeconch:
3070630747
OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
3070730748
if( rc==SQLITE_OK && pFile->openFlags ){
30749
+ int fd;
3070830750
if( pFile->h>=0 ){
3070930751
robust_close(pFile, pFile->h, __LINE__);
3071030752
}
3071130753
pFile->h = -1;
30712
- int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30754
+ fd = robust_open(pCtx->dbPath, pFile->openFlags,
3071330755
SQLITE_DEFAULT_FILE_PERMISSIONS);
3071430756
OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
3071530757
if( fd>=0 ){
3071630758
pFile->h = fd;
3071730759
}else{
@@ -31393,11 +31435,11 @@
3139331435
*/
3139431436
#ifdef MEMORY_DEBUG
3139531437
# error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
3139631438
#endif
3139731439
31398
-#ifdef SQLITE_DEBUG
31440
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
3139931441
# ifndef SQLITE_DEBUG_OS_TRACE
3140031442
# define SQLITE_DEBUG_OS_TRACE 0
3140131443
# endif
3140231444
int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
3140331445
# define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -31629,10 +31671,80 @@
3162931671
winceLock local; /* Locks obtained by this instance of winFile */
3163031672
winceLock *shared; /* Global shared lock memory for the file */
3163131673
#endif
3163231674
};
3163331675
31676
+/*
31677
+ * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31678
+ * various Win32 API heap functions instead of our own.
31679
+ */
31680
+#ifdef SQLITE_WIN32_MALLOC
31681
+/*
31682
+ * The initial size of the Win32-specific heap. This value may be zero.
31683
+ */
31684
+#ifndef SQLITE_WIN32_HEAP_INIT_SIZE
31685
+# define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
31686
+ (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
31687
+#endif
31688
+
31689
+/*
31690
+ * The maximum size of the Win32-specific heap. This value may be zero.
31691
+ */
31692
+#ifndef SQLITE_WIN32_HEAP_MAX_SIZE
31693
+# define SQLITE_WIN32_HEAP_MAX_SIZE (0)
31694
+#endif
31695
+
31696
+/*
31697
+ * The extra flags to use in calls to the Win32 heap APIs. This value may be
31698
+ * zero for the default behavior.
31699
+ */
31700
+#ifndef SQLITE_WIN32_HEAP_FLAGS
31701
+# define SQLITE_WIN32_HEAP_FLAGS (0)
31702
+#endif
31703
+
31704
+/*
31705
+** The winMemData structure stores information required by the Win32-specific
31706
+** sqlite3_mem_methods implementation.
31707
+*/
31708
+typedef struct winMemData winMemData;
31709
+struct winMemData {
31710
+#ifndef NDEBUG
31711
+ u32 magic; /* Magic number to detect structure corruption. */
31712
+#endif
31713
+ HANDLE hHeap; /* The handle to our heap. */
31714
+ BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
31715
+};
31716
+
31717
+#ifndef NDEBUG
31718
+#define WINMEM_MAGIC 0x42b2830b
31719
+#endif
31720
+
31721
+static struct winMemData win_mem_data = {
31722
+#ifndef NDEBUG
31723
+ WINMEM_MAGIC,
31724
+#endif
31725
+ NULL, FALSE
31726
+};
31727
+
31728
+#ifndef NDEBUG
31729
+#define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
31730
+#else
31731
+#define winMemAssertMagic()
31732
+#endif
31733
+
31734
+#define winMemGetHeap() win_mem_data.hHeap
31735
+
31736
+static void *winMemMalloc(int nBytes);
31737
+static void winMemFree(void *pPrior);
31738
+static void *winMemRealloc(void *pPrior, int nBytes);
31739
+static int winMemSize(void *p);
31740
+static int winMemRoundup(int n);
31741
+static int winMemInit(void *pAppData);
31742
+static void winMemShutdown(void *pAppData);
31743
+
31744
+SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31745
+#endif /* SQLITE_WIN32_MALLOC */
3163431746
3163531747
/*
3163631748
** Forward prototypes.
3163731749
*/
3163831750
static int getSectorSize(
@@ -31681,10 +31793,192 @@
3168131793
}
3168231794
return sqlite3_os_type==2;
3168331795
}
3168431796
#endif /* SQLITE_OS_WINCE */
3168531797
31798
+#ifdef SQLITE_WIN32_MALLOC
31799
+/*
31800
+** Allocate nBytes of memory.
31801
+*/
31802
+static void *winMemMalloc(int nBytes){
31803
+ HANDLE hHeap;
31804
+ void *p;
31805
+
31806
+ winMemAssertMagic();
31807
+ hHeap = winMemGetHeap();
31808
+ assert( hHeap!=0 );
31809
+ assert( hHeap!=INVALID_HANDLE_VALUE );
31810
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31811
+ assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31812
+#endif
31813
+ assert( nBytes>=0 );
31814
+ p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31815
+ if( !p ){
31816
+ sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
31817
+ nBytes, GetLastError(), (void*)hHeap);
31818
+ }
31819
+ return p;
31820
+}
31821
+
31822
+/*
31823
+** Free memory.
31824
+*/
31825
+static void winMemFree(void *pPrior){
31826
+ HANDLE hHeap;
31827
+
31828
+ winMemAssertMagic();
31829
+ hHeap = winMemGetHeap();
31830
+ assert( hHeap!=0 );
31831
+ assert( hHeap!=INVALID_HANDLE_VALUE );
31832
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31833
+ assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31834
+#endif
31835
+ if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
31836
+ if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
31837
+ sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
31838
+ pPrior, GetLastError(), (void*)hHeap);
31839
+ }
31840
+}
31841
+
31842
+/*
31843
+** Change the size of an existing memory allocation
31844
+*/
31845
+static void *winMemRealloc(void *pPrior, int nBytes){
31846
+ HANDLE hHeap;
31847
+ void *p;
31848
+
31849
+ winMemAssertMagic();
31850
+ hHeap = winMemGetHeap();
31851
+ assert( hHeap!=0 );
31852
+ assert( hHeap!=INVALID_HANDLE_VALUE );
31853
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31854
+ assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31855
+#endif
31856
+ assert( nBytes>=0 );
31857
+ if( !pPrior ){
31858
+ p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31859
+ }else{
31860
+ p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
31861
+ }
31862
+ if( !p ){
31863
+ sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
31864
+ pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
31865
+ (void*)hHeap);
31866
+ }
31867
+ return p;
31868
+}
31869
+
31870
+/*
31871
+** Return the size of an outstanding allocation, in bytes.
31872
+*/
31873
+static int winMemSize(void *p){
31874
+ HANDLE hHeap;
31875
+ SIZE_T n;
31876
+
31877
+ winMemAssertMagic();
31878
+ hHeap = winMemGetHeap();
31879
+ assert( hHeap!=0 );
31880
+ assert( hHeap!=INVALID_HANDLE_VALUE );
31881
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31882
+ assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31883
+#endif
31884
+ if( !p ) return 0;
31885
+ n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
31886
+ if( n==(SIZE_T)-1 ){
31887
+ sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
31888
+ p, GetLastError(), (void*)hHeap);
31889
+ return 0;
31890
+ }
31891
+ return (int)n;
31892
+}
31893
+
31894
+/*
31895
+** Round up a request size to the next valid allocation size.
31896
+*/
31897
+static int winMemRoundup(int n){
31898
+ return n;
31899
+}
31900
+
31901
+/*
31902
+** Initialize this module.
31903
+*/
31904
+static int winMemInit(void *pAppData){
31905
+ winMemData *pWinMemData = (winMemData *)pAppData;
31906
+
31907
+ if( !pWinMemData ) return SQLITE_ERROR;
31908
+ assert( pWinMemData->magic==WINMEM_MAGIC );
31909
+ if( !pWinMemData->hHeap ){
31910
+ pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS,
31911
+ SQLITE_WIN32_HEAP_INIT_SIZE,
31912
+ SQLITE_WIN32_HEAP_MAX_SIZE);
31913
+ if( !pWinMemData->hHeap ){
31914
+ sqlite3_log(SQLITE_NOMEM,
31915
+ "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
31916
+ GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE,
31917
+ SQLITE_WIN32_HEAP_MAX_SIZE);
31918
+ return SQLITE_NOMEM;
31919
+ }
31920
+ pWinMemData->bOwned = TRUE;
31921
+ }
31922
+ assert( pWinMemData->hHeap!=0 );
31923
+ assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31924
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31925
+ assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31926
+#endif
31927
+ return SQLITE_OK;
31928
+}
31929
+
31930
+/*
31931
+** Deinitialize this module.
31932
+*/
31933
+static void winMemShutdown(void *pAppData){
31934
+ winMemData *pWinMemData = (winMemData *)pAppData;
31935
+
31936
+ if( !pWinMemData ) return;
31937
+ if( pWinMemData->hHeap ){
31938
+ assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31939
+#ifdef SQLITE_WIN32_MALLOC_VALIDATE
31940
+ assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31941
+#endif
31942
+ if( pWinMemData->bOwned ){
31943
+ if( !HeapDestroy(pWinMemData->hHeap) ){
31944
+ sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
31945
+ GetLastError(), (void*)pWinMemData->hHeap);
31946
+ }
31947
+ pWinMemData->bOwned = FALSE;
31948
+ }
31949
+ pWinMemData->hHeap = NULL;
31950
+ }
31951
+}
31952
+
31953
+/*
31954
+** Populate the low-level memory allocation function pointers in
31955
+** sqlite3GlobalConfig.m with pointers to the routines in this file. The
31956
+** arguments specify the block of memory to manage.
31957
+**
31958
+** This routine is only called by sqlite3_config(), and therefore
31959
+** is not required to be threadsafe (it is not).
31960
+*/
31961
+SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
31962
+ static const sqlite3_mem_methods winMemMethods = {
31963
+ winMemMalloc,
31964
+ winMemFree,
31965
+ winMemRealloc,
31966
+ winMemSize,
31967
+ winMemRoundup,
31968
+ winMemInit,
31969
+ winMemShutdown,
31970
+ &win_mem_data
31971
+ };
31972
+ return &winMemMethods;
31973
+}
31974
+
31975
+SQLITE_PRIVATE void sqlite3MemSetDefault(void){
31976
+ sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
31977
+}
31978
+#endif /* SQLITE_WIN32_MALLOC */
31979
+
3168631980
/*
3168731981
** Convert a UTF-8 string to microsoft unicode (UTF-16?).
3168831982
**
3168931983
** Space to hold the returned string is obtained from malloc.
3169031984
*/
@@ -31969,10 +32263,11 @@
3196932263
*/
3197032264
/*
3197132265
** WindowsCE does not have a localtime() function. So create a
3197232266
** substitute.
3197332267
*/
32268
+/* #include <time.h> */
3197432269
struct tm *__cdecl localtime(const time_t *t)
3197532270
{
3197632271
static struct tm y;
3197732272
FILETIME uTm, lTm;
3197832273
SYSTEMTIME pTm;
@@ -32473,11 +32768,11 @@
3247332768
/* If the user has configured a chunk-size for this file, truncate the
3247432769
** file so that it consists of an integer number of chunks (i.e. the
3247532770
** actual file size after the operation may be larger than the requested
3247632771
** size).
3247732772
*/
32478
- if( pFile->szChunk ){
32773
+ if( pFile->szChunk>0 ){
3247932774
nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3248032775
}
3248132776
3248232777
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
3248332778
if( seekWinFile(pFile, nByte) ){
@@ -32860,14 +33155,23 @@
3286033155
case SQLITE_FCNTL_CHUNK_SIZE: {
3286133156
pFile->szChunk = *(int *)pArg;
3286233157
return SQLITE_OK;
3286333158
}
3286433159
case SQLITE_FCNTL_SIZE_HINT: {
32865
- sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32866
- SimulateIOErrorBenign(1);
32867
- winTruncate(id, sz);
32868
- SimulateIOErrorBenign(0);
33160
+ if( pFile->szChunk>0 ){
33161
+ sqlite3_int64 oldSz;
33162
+ int rc = winFileSize(id, &oldSz);
33163
+ if( rc==SQLITE_OK ){
33164
+ sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
33165
+ if( newSz>oldSz ){
33166
+ SimulateIOErrorBenign(1);
33167
+ rc = winTruncate(id, newSz);
33168
+ SimulateIOErrorBenign(0);
33169
+ }
33170
+ }
33171
+ return rc;
33172
+ }
3286933173
return SQLITE_OK;
3287033174
}
3287133175
case SQLITE_FCNTL_PERSIST_WAL: {
3287233176
int bPersist = *(int*)pArg;
3287333177
if( bPersist<0 ){
@@ -35473,10 +35777,13 @@
3547335777
typedef struct PCache1 PCache1;
3547435778
typedef struct PgHdr1 PgHdr1;
3547535779
typedef struct PgFreeslot PgFreeslot;
3547635780
typedef struct PGroup PGroup;
3547735781
35782
+typedef struct PGroupBlock PGroupBlock;
35783
+typedef struct PGroupBlockList PGroupBlockList;
35784
+
3547835785
/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
3547935786
** of one or more PCaches that are able to recycle each others unpinned
3548035787
** pages when they are under memory pressure. A PGroup is an instance of
3548135788
** the following object.
3548235789
**
@@ -35502,12 +35809,70 @@
3550235809
int nMaxPage; /* Sum of nMax for purgeable caches */
3550335810
int nMinPage; /* Sum of nMin for purgeable caches */
3550435811
int mxPinned; /* nMaxpage + 10 - nMinPage */
3550535812
int nCurrentPage; /* Number of purgeable pages allocated */
3550635813
PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35814
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
35815
+ int isBusy; /* Do not run ReleaseMemory() if true */
35816
+ PGroupBlockList *pBlockList; /* List of block-lists for this group */
35817
+#endif
3550735818
};
3550835819
35820
+/*
35821
+** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35822
+** each PGroup structure has a linked list of the the following starting
35823
+** at PGroup.pBlockList. There is one entry for each distinct page-size
35824
+** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35825
+** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35826
+** size requested by each pcache, which is the database page-size plus
35827
+** the various header structures used by the pcache, pager and btree layers.
35828
+** Usually around (pgsz+200) bytes.
35829
+**
35830
+** This size (pgsz+200) bytes is not allocated efficiently by some
35831
+** implementations of malloc. In particular, some implementations are only
35832
+** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35833
+** integer value. Since the page-size is a power of 2, this means we
35834
+** end up wasting (pgsz-200) bytes in each allocation.
35835
+**
35836
+** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35837
+** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35838
+** are requested from malloc allocator. After a block is returned,
35839
+** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35840
+** allocations can fit in the space returned by malloc(). This value may
35841
+** be more than M.
35842
+**
35843
+** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35844
+** contains the number of allocations that will fit in the aData[] space.
35845
+** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35846
+** within aData is in use, the corresponding bit in mUsed is set. Thus
35847
+** when (mUsed+1==(1 << nEntry)) the block is completely full.
35848
+**
35849
+** Each time a slot within a block is freed, the block is moved to the start
35850
+** of the linked-list. And if a block becomes completely full, then it is
35851
+** moved to the end of the list. As a result, when searching for a free
35852
+** slot, only the first block in the list need be examined. If it is full,
35853
+** then it is guaranteed that all blocks are full.
35854
+*/
35855
+struct PGroupBlockList {
35856
+ int nByte; /* Size of each allocation in bytes */
35857
+ PGroupBlock *pFirst; /* First PGroupBlock in list */
35858
+ PGroupBlock *pLast; /* Last PGroupBlock in list */
35859
+ PGroupBlockList *pNext; /* Next block-list attached to group */
35860
+};
35861
+
35862
+struct PGroupBlock {
35863
+ Bitmask mUsed; /* Mask of used slots */
35864
+ int nEntry; /* Maximum number of allocations in aData[] */
35865
+ u8 *aData; /* Pointer to data block */
35866
+ PGroupBlock *pNext; /* Next PGroupBlock in list */
35867
+ PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35868
+ PGroupBlockList *pList; /* Owner list */
35869
+};
35870
+
35871
+/* Minimum value for PGroupBlock.nEntry */
35872
+#define PAGECACHE_BLOCKALLOC_MINENTRY 15
35873
+
3550935874
/* Each page cache is an instance of the following object. Every
3551035875
** open database file (including each in-memory database and each
3551135876
** temporary or transient database) has a single page cache which
3551235877
** is an instance of this object.
3551335878
**
@@ -35606,10 +35971,21 @@
3560635971
**
3560735972
** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
3560835973
*/
3560935974
#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
3561035975
#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35976
+
35977
+/*
35978
+** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35979
+** a PGroupBlock pointer based on a pointer to a page buffer.
35980
+*/
35981
+#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35982
+ ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35983
+
35984
+#define PAGE_GET_BLOCKPTR(pCache, pPg) \
35985
+ ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35986
+
3561135987
3561235988
/*
3561335989
** Macros to enter and leave the PCache LRU mutex.
3561435990
*/
3561535991
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36108,159 @@
3573236108
return iSize;
3573336109
}
3573436110
}
3573536111
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
3573636112
36113
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36114
+/*
36115
+** The block pBlock belongs to list pList but is not currently linked in.
36116
+** Insert it into the start of the list.
36117
+*/
36118
+static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36119
+ pBlock->pPrev = 0;
36120
+ pBlock->pNext = pList->pFirst;
36121
+ pList->pFirst = pBlock;
36122
+ if( pBlock->pNext ){
36123
+ pBlock->pNext->pPrev = pBlock;
36124
+ }else{
36125
+ assert( pList->pLast==0 );
36126
+ pList->pLast = pBlock;
36127
+ }
36128
+}
36129
+
36130
+/*
36131
+** If there are no blocks in the list headed by pList, remove pList
36132
+** from the pGroup->pBlockList list and free it with sqlite3_free().
36133
+*/
36134
+static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36135
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36136
+ if( pList->pFirst==0 ){
36137
+ PGroupBlockList **pp;
36138
+ for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36139
+ *pp = (*pp)->pNext;
36140
+ sqlite3_free(pList);
36141
+ }
36142
+}
36143
+#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36144
+
3573736145
/*
3573836146
** Allocate a new page object initially associated with cache pCache.
3573936147
*/
3574036148
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
3574136149
int nByte = sizeof(PgHdr1) + pCache->szPage;
35742
- void *pPg = pcache1Alloc(nByte);
36150
+ void *pPg = 0;
3574336151
PgHdr1 *p;
36152
+
36153
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36154
+ PGroup *pGroup = pCache->pGroup;
36155
+ PGroupBlockList *pList;
36156
+ PGroupBlock *pBlock;
36157
+ int i;
36158
+
36159
+ nByte += sizeof(PGroupBlockList *);
36160
+ nByte = ROUND8(nByte);
36161
+
36162
+ for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36163
+ if( pList->nByte==nByte ) break;
36164
+ }
36165
+ if( pList==0 ){
36166
+ PGroupBlockList *pNew;
36167
+ assert( pGroup->isBusy==0 );
36168
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36169
+ pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36170
+ pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36171
+ pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36172
+ if( pNew==0 ){
36173
+ /* malloc() failure. Return early. */
36174
+ return 0;
36175
+ }
36176
+#ifdef SQLITE_DEBUG
36177
+ for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36178
+ assert( pList->nByte!=nByte );
36179
+ }
36180
+#endif
36181
+ pNew->nByte = nByte;
36182
+ pNew->pNext = pGroup->pBlockList;
36183
+ pGroup->pBlockList = pNew;
36184
+ pList = pNew;
36185
+ }
36186
+
36187
+ pBlock = pList->pFirst;
36188
+ if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36189
+ int sz;
36190
+
36191
+ /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36192
+ ** structure and MINENTRY allocations of nByte bytes each. If the
36193
+ ** allocator returns more memory than requested, then more than MINENTRY
36194
+ ** allocations may fit in it. */
36195
+ assert( sqlite3_mutex_held(pGroup->mutex) );
36196
+ pcache1LeaveMutex(pCache->pGroup);
36197
+ sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36198
+ pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36199
+ pcache1EnterMutex(pCache->pGroup);
36200
+
36201
+ if( !pBlock ){
36202
+ freeListIfEmpty(pGroup, pList);
36203
+ return 0;
36204
+ }
36205
+ pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36206
+ if( pBlock->nEntry>=BMS ){
36207
+ pBlock->nEntry = BMS-1;
36208
+ }
36209
+ pBlock->pList = pList;
36210
+ pBlock->mUsed = 0;
36211
+ pBlock->aData = (u8 *)&pBlock[1];
36212
+ addBlockToList(pList, pBlock);
36213
+
36214
+ sz = sqlite3MallocSize(pBlock);
36215
+ sqlite3_mutex_enter(pcache1.mutex);
36216
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36217
+ sqlite3_mutex_leave(pcache1.mutex);
36218
+ }
36219
+
36220
+ for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36221
+ if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36222
+ pBlock->mUsed |= ((Bitmask)1<<i);
36223
+ pPg = (void *)&pBlock->aData[pList->nByte * i];
36224
+ }
36225
+ }
36226
+ assert( pPg );
36227
+ PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36228
+
36229
+ /* If the block is now full, shift it to the end of the list */
36230
+ if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36231
+ assert( pList->pFirst==pBlock );
36232
+ assert( pBlock->pPrev==0 );
36233
+ assert( pList->pLast->pNext==0 );
36234
+ pList->pFirst = pBlock->pNext;
36235
+ pList->pFirst->pPrev = 0;
36236
+ pBlock->pPrev = pList->pLast;
36237
+ pBlock->pNext = 0;
36238
+ pList->pLast->pNext = pBlock;
36239
+ pList->pLast = pBlock;
36240
+ }
36241
+ p = PAGE_TO_PGHDR1(pCache, pPg);
36242
+ if( pCache->bPurgeable ){
36243
+ pCache->pGroup->nCurrentPage++;
36244
+ }
36245
+#else
36246
+ /* The group mutex must be released before pcache1Alloc() is called. This
36247
+ ** is because it may call sqlite3_release_memory(), which assumes that
36248
+ ** this mutex is not held. */
36249
+ assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36250
+ pcache1LeaveMutex(pCache->pGroup);
36251
+ pPg = pcache1Alloc(nByte);
36252
+ pcache1EnterMutex(pCache->pGroup);
3574436253
if( pPg ){
3574536254
p = PAGE_TO_PGHDR1(pCache, pPg);
3574636255
if( pCache->bPurgeable ){
3574736256
pCache->pGroup->nCurrentPage++;
3574836257
}
3574936258
}else{
3575036259
p = 0;
3575136260
}
36261
+#endif
3575236262
return p;
3575336263
}
3575436264
3575536265
/*
3575636266
** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36270,56 @@
3576036270
** with a NULL pointer, so we mark the NULL test with ALWAYS().
3576136271
*/
3576236272
static void pcache1FreePage(PgHdr1 *p){
3576336273
if( ALWAYS(p) ){
3576436274
PCache1 *pCache = p->pCache;
36275
+ void *pPg = PGHDR1_TO_PAGE(p);
36276
+
36277
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36278
+ PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36279
+ PGroupBlockList *pList = pBlock->pList;
36280
+ int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36281
+
36282
+ assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36283
+ assert( pBlock->mUsed & ((Bitmask)1<<i) );
36284
+ pBlock->mUsed &= ~((Bitmask)1<<i);
36285
+
36286
+ /* Remove the block from the list. If it is completely empty, free it.
36287
+ ** Or if it is not completely empty, re-insert it at the start of the
36288
+ ** list. */
36289
+ if( pList->pFirst==pBlock ){
36290
+ pList->pFirst = pBlock->pNext;
36291
+ if( pList->pFirst ) pList->pFirst->pPrev = 0;
36292
+ }else{
36293
+ pBlock->pPrev->pNext = pBlock->pNext;
36294
+ }
36295
+ if( pList->pLast==pBlock ){
36296
+ pList->pLast = pBlock->pPrev;
36297
+ if( pList->pLast ) pList->pLast->pNext = 0;
36298
+ }else{
36299
+ pBlock->pNext->pPrev = pBlock->pPrev;
36300
+ }
36301
+
36302
+ if( pBlock->mUsed==0 ){
36303
+ PGroup *pGroup = p->pCache->pGroup;
36304
+
36305
+ int sz = sqlite3MallocSize(pBlock);
36306
+ sqlite3_mutex_enter(pcache1.mutex);
36307
+ sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36308
+ sqlite3_mutex_leave(pcache1.mutex);
36309
+ freeListIfEmpty(pGroup, pList);
36310
+ sqlite3_free(pBlock);
36311
+ }else{
36312
+ addBlockToList(pList, pBlock);
36313
+ }
36314
+#else
36315
+ assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36316
+ pcache1Free(pPg);
36317
+#endif
3576536318
if( pCache->bPurgeable ){
3576636319
pCache->pGroup->nCurrentPage--;
3576736320
}
35768
- pcache1Free(PGHDR1_TO_PAGE(p));
3576936321
}
3577036322
}
3577136323
3577236324
/*
3577336325
** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36753,11 @@
3620136753
/* Step 5. If a usable page buffer has still not been found,
3620236754
** attempt to allocate a new one.
3620336755
*/
3620436756
if( !pPage ){
3620536757
if( createFlag==1 ) sqlite3BeginBenignMalloc();
36206
- pcache1LeaveMutex(pGroup);
3620736758
pPage = pcache1AllocPage(pCache);
36208
- pcache1EnterMutex(pGroup);
3620936759
if( createFlag==1 ) sqlite3EndBenignMalloc();
3621036760
}
3621136761
3621236762
if( pPage ){
3621336763
unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36923,13 @@
3637336923
** been released, the function returns. The return value is the total number
3637436924
** of bytes of memory released.
3637536925
*/
3637636926
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
3637736927
int nFree = 0;
36928
+#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36929
+ if( pcache1.grp.isBusy ) return 0;
36930
+#endif
3637836931
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
3637936932
assert( sqlite3_mutex_notheld(pcache1.mutex) );
3638036933
if( pcache1.pStart==0 ){
3638136934
PgHdr1 *p;
3638236935
pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38138,11 @@
3758538138
u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
3758638139
u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
3758738140
u8 tempFile; /* zFilename is a temporary file */
3758838141
u8 readOnly; /* True for a read-only database */
3758938142
u8 memDb; /* True to inhibit all file I/O */
38143
+ u8 hasSeenStress; /* pagerStress() called one or more times */
3759038144
3759138145
/**************************************************************************
3759238146
** The following block contains those class members that change during
3759338147
** routine opertion. Class members not in this block are either fixed
3759438148
** when the pager is first created or else only change when there is a
@@ -40704,10 +41258,11 @@
4070441258
** to the caller.
4070541259
*/
4070641260
SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
4070741261
u8 *pTmp = (u8 *)pPager->pTmpSpace;
4070841262
41263
+ assert( assert_pager_state(pPager) );
4070941264
disable_simulated_io_errors();
4071041265
sqlite3BeginBenignMalloc();
4071141266
/* pPager->errCode = 0; */
4071241267
pPager->exclusiveMode = 0;
4071341268
#ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41693,11 @@
4113841693
** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
4113941694
** while in the error state, hence it is impossible for this routine to
4114041695
** be called in the error state. Nevertheless, we include a NEVER()
4114141696
** test for the error state as a safeguard against future changes.
4114241697
*/
41698
+ pPager->hasSeenStress = 1;
4114341699
if( NEVER(pPager->errCode) ) return SQLITE_OK;
4114441700
if( pPager->doNotSpill ) return SQLITE_OK;
4114541701
if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
4114641702
return SQLITE_OK;
4114741703
}
@@ -50430,15 +50986,16 @@
5043050986
for(i=0; i<nCell; i++){
5043150987
u8 *pCell = findCell(pPage, i);
5043250988
if( eType==PTRMAP_OVERFLOW1 ){
5043350989
CellInfo info;
5043450990
btreeParseCellPtr(pPage, pCell, &info);
50435
- if( info.iOverflow ){
50436
- if( iFrom==get4byte(&pCell[info.iOverflow]) ){
50437
- put4byte(&pCell[info.iOverflow], iTo);
50438
- break;
50439
- }
50991
+ if( info.iOverflow
50992
+ && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
50993
+ && iFrom==get4byte(&pCell[info.iOverflow])
50994
+ ){
50995
+ put4byte(&pCell[info.iOverflow], iTo);
50996
+ break;
5044050997
}
5044150998
}else{
5044250999
if( get4byte(pCell)==iFrom ){
5044351000
put4byte(pCell, iTo);
5044451001
break;
@@ -51155,11 +51712,12 @@
5115551712
5115651713
if( NEVER(wrFlag && pBt->readOnly) ){
5115751714
return SQLITE_READONLY;
5115851715
}
5115951716
if( iTable==1 && btreePagecount(pBt)==0 ){
51160
- return SQLITE_EMPTY;
51717
+ assert( wrFlag==0 );
51718
+ iTable = 0;
5116151719
}
5116251720
5116351721
/* Now that no other errors can occur, finish filling in the BtCursor
5116451722
** variables and link the cursor into the BtShared list. */
5116551723
pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52467,13 @@
5190952467
int i;
5191052468
for(i=1; i<=pCur->iPage; i++){
5191152469
releasePage(pCur->apPage[i]);
5191252470
}
5191352471
pCur->iPage = 0;
52472
+ }else if( pCur->pgnoRoot==0 ){
52473
+ pCur->eState = CURSOR_INVALID;
52474
+ return SQLITE_OK;
5191452475
}else{
5191552476
rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
5191652477
if( rc!=SQLITE_OK ){
5191752478
pCur->eState = CURSOR_INVALID;
5191852479
return rc;
@@ -52018,11 +52579,11 @@
5201852579
assert( cursorHoldsMutex(pCur) );
5201952580
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5202052581
rc = moveToRoot(pCur);
5202152582
if( rc==SQLITE_OK ){
5202252583
if( pCur->eState==CURSOR_INVALID ){
52023
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52584
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5202452585
*pRes = 1;
5202552586
}else{
5202652587
assert( pCur->apPage[pCur->iPage]->nCell>0 );
5202752588
*pRes = 0;
5202852589
rc = moveToLeftmost(pCur);
@@ -52057,11 +52618,11 @@
5205752618
}
5205852619
5205952620
rc = moveToRoot(pCur);
5206052621
if( rc==SQLITE_OK ){
5206152622
if( CURSOR_INVALID==pCur->eState ){
52062
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52623
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5206352624
*pRes = 1;
5206452625
}else{
5206552626
assert( pCur->eState==CURSOR_VALID );
5206652627
*pRes = 0;
5206752628
rc = moveToRightmost(pCur);
@@ -52130,16 +52691,16 @@
5213052691
5213152692
rc = moveToRoot(pCur);
5213252693
if( rc ){
5213352694
return rc;
5213452695
}
52135
- assert( pCur->apPage[pCur->iPage] );
52136
- assert( pCur->apPage[pCur->iPage]->isInit );
52137
- assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
52696
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
52697
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
52698
+ assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
5213852699
if( pCur->eState==CURSOR_INVALID ){
5213952700
*pRes = -1;
52140
- assert( pCur->apPage[pCur->iPage]->nCell==0 );
52701
+ assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
5214152702
return SQLITE_OK;
5214252703
}
5214352704
assert( pCur->apPage[0]->intKey || pIdxKey );
5214452705
for(;;){
5214552706
int lwr, upr, idx;
@@ -52862,10 +53423,13 @@
5286253423
assert( sqlite3_mutex_held(pPage->pBt->mutex) );
5286353424
btreeParseCellPtr(pPage, pCell, &info);
5286453425
if( info.iOverflow==0 ){
5286553426
return SQLITE_OK; /* No overflow pages. Return without doing anything */
5286653427
}
53428
+ if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53429
+ return SQLITE_CORRUPT; /* Cell extends past end of page */
53430
+ }
5286753431
ovflPgno = get4byte(&pCell[info.iOverflow]);
5286853432
assert( pBt->usableSize > 4 );
5286953433
ovflPageSize = pBt->usableSize - 4;
5287053434
nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
5287153435
assert( ovflPgno==0 || nOvfl>0 );
@@ -55045,10 +55609,15 @@
5504555609
** corruption) an SQLite error code is returned.
5504655610
*/
5504755611
SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
5504855612
i64 nEntry = 0; /* Value to return in *pnEntry */
5504955613
int rc; /* Return code */
55614
+
55615
+ if( pCur->pgnoRoot==0 ){
55616
+ *pnEntry = 0;
55617
+ return SQLITE_OK;
55618
+ }
5505055619
rc = moveToRoot(pCur);
5505155620
5505255621
/* Unless an error occurs, the following loop runs one iteration for each
5505355622
** page in the B-Tree structure (not including overflow pages).
5505455623
*/
@@ -55829,11 +56398,10 @@
5582956398
*/
5583056399
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
5583156400
BtShared *pBt = pBtree->pBt;
5583256401
int rc; /* Return code */
5583356402
55834
- assert( pBtree->inTrans==TRANS_NONE );
5583556403
assert( iVersion==1 || iVersion==2 );
5583656404
5583756405
/* If setting the version fields to 1, do not automatically open the
5583856406
** WAL connection, even if the version fields are currently set to 2.
5583956407
*/
@@ -56268,106 +56836,110 @@
5626856836
/* Update the schema version field in the destination database. This
5626956837
** is to make sure that the schema-version really does change in
5627056838
** the case where the source and destination databases have the
5627156839
** same schema version.
5627256840
*/
56273
- if( rc==SQLITE_DONE
56274
- && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
56275
- ){
56276
- int nDestTruncate;
56277
-
56278
- if( p->pDestDb ){
56279
- sqlite3ResetInternalSchema(p->pDestDb, -1);
56280
- }
56281
-
56282
- /* Set nDestTruncate to the final number of pages in the destination
56283
- ** database. The complication here is that the destination page
56284
- ** size may be different to the source page size.
56285
- **
56286
- ** If the source page size is smaller than the destination page size,
56287
- ** round up. In this case the call to sqlite3OsTruncate() below will
56288
- ** fix the size of the file. However it is important to call
56289
- ** sqlite3PagerTruncateImage() here so that any pages in the
56290
- ** destination file that lie beyond the nDestTruncate page mark are
56291
- ** journalled by PagerCommitPhaseOne() before they are destroyed
56292
- ** by the file truncation.
56293
- */
56294
- assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56295
- assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56296
- if( pgszSrc<pgszDest ){
56297
- int ratio = pgszDest/pgszSrc;
56298
- nDestTruncate = (nSrcPage+ratio-1)/ratio;
56299
- if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56300
- nDestTruncate--;
56301
- }
56302
- }else{
56303
- nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56304
- }
56305
- sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56306
-
56307
- if( pgszSrc<pgszDest ){
56308
- /* If the source page-size is smaller than the destination page-size,
56309
- ** two extra things may need to happen:
56310
- **
56311
- ** * The destination may need to be truncated, and
56312
- **
56313
- ** * Data stored on the pages immediately following the
56314
- ** pending-byte page in the source database may need to be
56315
- ** copied into the destination database.
56316
- */
56317
- const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56318
- sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56319
- i64 iOff;
56320
- i64 iEnd;
56321
-
56322
- assert( pFile );
56323
- assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56324
- nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56325
- && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56326
- ));
56327
-
56328
- /* This call ensures that all data required to recreate the original
56329
- ** database has been stored in the journal for pDestPager and the
56330
- ** journal synced to disk. So at this point we may safely modify
56331
- ** the database file in any way, knowing that if a power failure
56332
- ** occurs, the original database will be reconstructed from the
56333
- ** journal file. */
56334
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56335
-
56336
- /* Write the extra pages and truncate the database file as required. */
56337
- iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56338
- for(
56339
- iOff=PENDING_BYTE+pgszSrc;
56340
- rc==SQLITE_OK && iOff<iEnd;
56341
- iOff+=pgszSrc
56342
- ){
56343
- PgHdr *pSrcPg = 0;
56344
- const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56345
- rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56346
- if( rc==SQLITE_OK ){
56347
- u8 *zData = sqlite3PagerGetData(pSrcPg);
56348
- rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56349
- }
56350
- sqlite3PagerUnref(pSrcPg);
56351
- }
56352
- if( rc==SQLITE_OK ){
56353
- rc = backupTruncateFile(pFile, iSize);
56354
- }
56355
-
56356
- /* Sync the database file to disk. */
56357
- if( rc==SQLITE_OK ){
56358
- rc = sqlite3PagerSync(pDestPager);
56359
- }
56360
- }else{
56361
- rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56362
- }
56363
-
56364
- /* Finish committing the transaction to the destination database. */
56365
- if( SQLITE_OK==rc
56366
- && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56367
- ){
56368
- rc = SQLITE_DONE;
56841
+ if( rc==SQLITE_DONE ){
56842
+ rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
56843
+ if( rc==SQLITE_OK ){
56844
+ if( p->pDestDb ){
56845
+ sqlite3ResetInternalSchema(p->pDestDb, -1);
56846
+ }
56847
+ if( destMode==PAGER_JOURNALMODE_WAL ){
56848
+ rc = sqlite3BtreeSetVersion(p->pDest, 2);
56849
+ }
56850
+ }
56851
+ if( rc==SQLITE_OK ){
56852
+ int nDestTruncate;
56853
+ /* Set nDestTruncate to the final number of pages in the destination
56854
+ ** database. The complication here is that the destination page
56855
+ ** size may be different to the source page size.
56856
+ **
56857
+ ** If the source page size is smaller than the destination page size,
56858
+ ** round up. In this case the call to sqlite3OsTruncate() below will
56859
+ ** fix the size of the file. However it is important to call
56860
+ ** sqlite3PagerTruncateImage() here so that any pages in the
56861
+ ** destination file that lie beyond the nDestTruncate page mark are
56862
+ ** journalled by PagerCommitPhaseOne() before they are destroyed
56863
+ ** by the file truncation.
56864
+ */
56865
+ assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56866
+ assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56867
+ if( pgszSrc<pgszDest ){
56868
+ int ratio = pgszDest/pgszSrc;
56869
+ nDestTruncate = (nSrcPage+ratio-1)/ratio;
56870
+ if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56871
+ nDestTruncate--;
56872
+ }
56873
+ }else{
56874
+ nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56875
+ }
56876
+ sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56877
+
56878
+ if( pgszSrc<pgszDest ){
56879
+ /* If the source page-size is smaller than the destination page-size,
56880
+ ** two extra things may need to happen:
56881
+ **
56882
+ ** * The destination may need to be truncated, and
56883
+ **
56884
+ ** * Data stored on the pages immediately following the
56885
+ ** pending-byte page in the source database may need to be
56886
+ ** copied into the destination database.
56887
+ */
56888
+ const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56889
+ sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56890
+ i64 iOff;
56891
+ i64 iEnd;
56892
+
56893
+ assert( pFile );
56894
+ assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56895
+ nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56896
+ && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56897
+ ));
56898
+
56899
+ /* This call ensures that all data required to recreate the original
56900
+ ** database has been stored in the journal for pDestPager and the
56901
+ ** journal synced to disk. So at this point we may safely modify
56902
+ ** the database file in any way, knowing that if a power failure
56903
+ ** occurs, the original database will be reconstructed from the
56904
+ ** journal file. */
56905
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56906
+
56907
+ /* Write the extra pages and truncate the database file as required */
56908
+ iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56909
+ for(
56910
+ iOff=PENDING_BYTE+pgszSrc;
56911
+ rc==SQLITE_OK && iOff<iEnd;
56912
+ iOff+=pgszSrc
56913
+ ){
56914
+ PgHdr *pSrcPg = 0;
56915
+ const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56916
+ rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56917
+ if( rc==SQLITE_OK ){
56918
+ u8 *zData = sqlite3PagerGetData(pSrcPg);
56919
+ rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56920
+ }
56921
+ sqlite3PagerUnref(pSrcPg);
56922
+ }
56923
+ if( rc==SQLITE_OK ){
56924
+ rc = backupTruncateFile(pFile, iSize);
56925
+ }
56926
+
56927
+ /* Sync the database file to disk. */
56928
+ if( rc==SQLITE_OK ){
56929
+ rc = sqlite3PagerSync(pDestPager);
56930
+ }
56931
+ }else{
56932
+ rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56933
+ }
56934
+
56935
+ /* Finish committing the transaction to the destination database. */
56936
+ if( SQLITE_OK==rc
56937
+ && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56938
+ ){
56939
+ rc = SQLITE_DONE;
56940
+ }
5636956941
}
5637056942
}
5637156943
5637256944
/* If bCloseTrans is true, then this function opened a read transaction
5637356945
** on the source database. Close the read transaction here. There is
@@ -56831,38 +57403,32 @@
5683157403
** invoking an external callback, free it now. Calling this function
5683257404
** does not free any Mem.zMalloc buffer.
5683357405
*/
5683457406
SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
5683557407
assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
56836
- testcase( p->flags & MEM_Agg );
56837
- testcase( p->flags & MEM_Dyn );
56838
- testcase( p->flags & MEM_RowSet );
56839
- testcase( p->flags & MEM_Frame );
56840
- if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
56841
- if( p->flags&MEM_Agg ){
56842
- sqlite3VdbeMemFinalize(p, p->u.pDef);
56843
- assert( (p->flags & MEM_Agg)==0 );
56844
- sqlite3VdbeMemRelease(p);
56845
- }else if( p->flags&MEM_Dyn && p->xDel ){
56846
- assert( (p->flags&MEM_RowSet)==0 );
56847
- p->xDel((void *)p->z);
56848
- p->xDel = 0;
56849
- }else if( p->flags&MEM_RowSet ){
56850
- sqlite3RowSetClear(p->u.pRowSet);
56851
- }else if( p->flags&MEM_Frame ){
56852
- sqlite3VdbeMemSetNull(p);
56853
- }
57408
+ if( p->flags&MEM_Agg ){
57409
+ sqlite3VdbeMemFinalize(p, p->u.pDef);
57410
+ assert( (p->flags & MEM_Agg)==0 );
57411
+ sqlite3VdbeMemRelease(p);
57412
+ }else if( p->flags&MEM_Dyn && p->xDel ){
57413
+ assert( (p->flags&MEM_RowSet)==0 );
57414
+ p->xDel((void *)p->z);
57415
+ p->xDel = 0;
57416
+ }else if( p->flags&MEM_RowSet ){
57417
+ sqlite3RowSetClear(p->u.pRowSet);
57418
+ }else if( p->flags&MEM_Frame ){
57419
+ sqlite3VdbeMemSetNull(p);
5685457420
}
5685557421
}
5685657422
5685757423
/*
5685857424
** Release any memory held by the Mem. This may leave the Mem in an
5685957425
** inconsistent state, for example with (Mem.z==0) and
5686057426
** (Mem.type==SQLITE_TEXT).
5686157427
*/
5686257428
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56863
- sqlite3VdbeMemReleaseExternal(p);
57429
+ MemReleaseExt(p);
5686457430
sqlite3DbFree(p->db, p->zMalloc);
5686557431
p->z = 0;
5686657432
p->zMalloc = 0;
5686757433
p->xDel = 0;
5686857434
}
@@ -57180,11 +57746,11 @@
5718057746
** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
5718157747
** and flags gets srcType (either MEM_Ephem or MEM_Static).
5718257748
*/
5718357749
SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
5718457750
assert( (pFrom->flags & MEM_RowSet)==0 );
57185
- sqlite3VdbeMemReleaseExternal(pTo);
57751
+ MemReleaseExt(pTo);
5718657752
memcpy(pTo, pFrom, MEMCELLSIZE);
5718757753
pTo->xDel = 0;
5718857754
if( (pFrom->flags&MEM_Static)==0 ){
5718957755
pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
5719057756
assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57764,11 @@
5719857764
*/
5719957765
SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
5720057766
int rc = SQLITE_OK;
5720157767
5720257768
assert( (pFrom->flags & MEM_RowSet)==0 );
57203
- sqlite3VdbeMemReleaseExternal(pTo);
57769
+ MemReleaseExt(pTo);
5720457770
memcpy(pTo, pFrom, MEMCELLSIZE);
5720557771
pTo->flags &= ~MEM_Dyn;
5720657772
5720757773
if( pTo->flags&(MEM_Str|MEM_Blob) ){
5720857774
if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58158,15 @@
5759258158
*ppVal = 0;
5759358159
return SQLITE_OK;
5759458160
}
5759558161
op = pExpr->op;
5759658162
57597
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
58163
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
5759858164
** The ifdef here is to enable us to achieve 100% branch test coverage even
57599
- ** when SQLITE_ENABLE_STAT3 is omitted.
58165
+ ** when SQLITE_ENABLE_STAT2 is omitted.
5760058166
*/
57601
-#ifdef SQLITE_ENABLE_STAT3
58167
+#ifdef SQLITE_ENABLE_STAT2
5760258168
if( op==TK_REGISTER ) op = pExpr->op2;
5760358169
#else
5760458170
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5760558171
#endif
5760658172
@@ -58153,10 +58719,16 @@
5815358719
assert( p->nOp - i >= 3 );
5815458720
assert( pOp[-1].opcode==OP_Integer );
5815558721
n = pOp[-1].p1;
5815658722
if( n>nMaxArgs ) nMaxArgs = n;
5815758723
#endif
58724
+ }else if( opcode==OP_Next || opcode==OP_SorterNext ){
58725
+ pOp->p4.xAdvance = sqlite3BtreeNext;
58726
+ pOp->p4type = P4_ADVANCE;
58727
+ }else if( opcode==OP_Prev ){
58728
+ pOp->p4.xAdvance = sqlite3BtreePrevious;
58729
+ pOp->p4type = P4_ADVANCE;
5815858730
}
5815958731
5816058732
if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
5816158733
assert( -1-pOp->p2<p->nLabel );
5816258734
pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58816,34 @@
5824458816
** Change the value of the P1 operand for a specific instruction.
5824558817
** This routine is useful when a large program is loaded from a
5824658818
** static array using sqlite3VdbeAddOpList but we want to make a
5824758819
** few minor changes to the program.
5824858820
*/
58249
-SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
58821
+SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
5825058822
assert( p!=0 );
58251
- assert( addr>=0 );
58252
- if( p->nOp>addr ){
58823
+ if( ((u32)p->nOp)>addr ){
5825358824
p->aOp[addr].p1 = val;
5825458825
}
5825558826
}
5825658827
5825758828
/*
5825858829
** Change the value of the P2 operand for a specific instruction.
5825958830
** This routine is useful for setting a jump destination.
5826058831
*/
58261
-SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
58832
+SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
5826258833
assert( p!=0 );
58263
- assert( addr>=0 );
58264
- if( p->nOp>addr ){
58834
+ if( ((u32)p->nOp)>addr ){
5826558835
p->aOp[addr].p2 = val;
5826658836
}
5826758837
}
5826858838
5826958839
/*
5827058840
** Change the value of the P3 operand for a specific instruction.
5827158841
*/
58272
-SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
58842
+SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
5827358843
assert( p!=0 );
58274
- assert( addr>=0 );
58275
- if( p->nOp>addr ){
58844
+ if( ((u32)p->nOp)>addr ){
5827658845
p->aOp[addr].p3 = val;
5827758846
}
5827858847
}
5827958848
5828058849
/*
@@ -58292,12 +58861,12 @@
5829258861
/*
5829358862
** Change the P2 operand of instruction addr so that it points to
5829458863
** the address of the next instruction to be coded.
5829558864
*/
5829658865
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58297
- assert( addr>=0 || p->db->mallocFailed );
58298
- if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
58866
+ assert( addr>=0 );
58867
+ sqlite3VdbeChangeP2(p, addr, p->nOp);
5829958868
}
5830058869
5830158870
5830258871
/*
5830358872
** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59230,14 @@
5866159230
break;
5866259231
}
5866359232
case P4_SUBPROGRAM: {
5866459233
sqlite3_snprintf(nTemp, zTemp, "program");
5866559234
break;
59235
+ }
59236
+ case P4_ADVANCE: {
59237
+ zTemp[0] = 0;
59238
+ break;
5866659239
}
5866759240
default: {
5866859241
zP4 = pOp->p4.z;
5866959242
if( zP4==0 ){
5867059243
zP4 = zTemp;
@@ -59285,10 +59858,11 @@
5928559858
*/
5928659859
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
5928759860
if( pCx==0 ){
5928859861
return;
5928959862
}
59863
+ sqlite3VdbeSorterClose(p->db, pCx);
5929059864
if( pCx->pBt ){
5929159865
sqlite3BtreeClose(pCx->pBt);
5929259866
/* The pCx->pCursor will be close automatically, if it exists, by
5929359867
** the call above. */
5929459868
}else if( pCx->pCursor ){
@@ -62585,10 +63159,17 @@
6258563159
** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
6258663160
** P if required.
6258763161
*/
6258863162
#define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
6258963163
63164
+/* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63165
+#ifdef SQLITE_OMIT_MERGE_SORT
63166
+# define isSorter(x) 0
63167
+#else
63168
+# define isSorter(x) ((x)->pSorter!=0)
63169
+#endif
63170
+
6259063171
/*
6259163172
** Argument pMem points at a register that will be passed to a
6259263173
** user-defined function or returned to the user as the result of a query.
6259363174
** This routine sets the pMem->type variable used by the sqlite3_value_*()
6259463175
** routines.
@@ -63179,10 +63760,11 @@
6317963760
u8 *zEndHdr; /* Pointer to first byte after the header */
6318063761
u32 offset; /* Offset into the data */
6318163762
u32 szField; /* Number of bytes in the content of a field */
6318263763
int szHdr; /* Size of the header size field at start of record */
6318363764
int avail; /* Number of bytes of available data */
63765
+ u32 t; /* A type code from the record header */
6318463766
Mem *pReg; /* PseudoTable input register */
6318563767
} am;
6318663768
struct OP_Affinity_stack_vars {
6318763769
const char *zAffinity; /* The affinity to be applied */
6318863770
char cAff; /* A single character of affinity */
@@ -63250,55 +63832,58 @@
6325063832
Db *pDb;
6325163833
} aw;
6325263834
struct OP_OpenEphemeral_stack_vars {
6325363835
VdbeCursor *pCx;
6325463836
} ax;
63255
- struct OP_OpenPseudo_stack_vars {
63837
+ struct OP_SorterOpen_stack_vars {
6325663838
VdbeCursor *pCx;
6325763839
} ay;
63840
+ struct OP_OpenPseudo_stack_vars {
63841
+ VdbeCursor *pCx;
63842
+ } az;
6325863843
struct OP_SeekGt_stack_vars {
6325963844
int res;
6326063845
int oc;
6326163846
VdbeCursor *pC;
6326263847
UnpackedRecord r;
6326363848
int nField;
6326463849
i64 iKey; /* The rowid we are to seek to */
63265
- } az;
63850
+ } ba;
6326663851
struct OP_Seek_stack_vars {
6326763852
VdbeCursor *pC;
63268
- } ba;
63853
+ } bb;
6326963854
struct OP_Found_stack_vars {
6327063855
int alreadyExists;
6327163856
VdbeCursor *pC;
6327263857
int res;
6327363858
UnpackedRecord *pIdxKey;
6327463859
UnpackedRecord r;
6327563860
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63276
- } bb;
63861
+ } bc;
6327763862
struct OP_IsUnique_stack_vars {
6327863863
u16 ii;
6327963864
VdbeCursor *pCx;
6328063865
BtCursor *pCrsr;
6328163866
u16 nField;
6328263867
Mem *aMx;
6328363868
UnpackedRecord r; /* B-Tree index search key */
6328463869
i64 R; /* Rowid stored in register P3 */
63285
- } bc;
63870
+ } bd;
6328663871
struct OP_NotExists_stack_vars {
6328763872
VdbeCursor *pC;
6328863873
BtCursor *pCrsr;
6328963874
int res;
6329063875
u64 iKey;
63291
- } bd;
63876
+ } be;
6329263877
struct OP_NewRowid_stack_vars {
6329363878
i64 v; /* The new rowid */
6329463879
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6329563880
int res; /* Result of an sqlite3BtreeLast() */
6329663881
int cnt; /* Counter to limit the number of searches */
6329763882
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6329863883
VdbeFrame *pFrame; /* Root frame of VDBE */
63299
- } be;
63884
+ } bf;
6330063885
struct OP_InsertInt_stack_vars {
6330163886
Mem *pData; /* MEM cell holding data for the record to be inserted */
6330263887
Mem *pKey; /* MEM cell holding key for the record */
6330363888
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6330463889
VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63305,155 +63890,161 @@
6330563890
int nZero; /* Number of zero-bytes to append */
6330663891
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6330763892
const char *zDb; /* database name - used by the update hook */
6330863893
const char *zTbl; /* Table name - used by the opdate hook */
6330963894
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63310
- } bf;
63895
+ } bg;
6331163896
struct OP_Delete_stack_vars {
6331263897
i64 iKey;
6331363898
VdbeCursor *pC;
63314
- } bg;
63899
+ } bh;
63900
+ struct OP_SorterCompare_stack_vars {
63901
+ VdbeCursor *pC;
63902
+ int res;
63903
+ } bi;
63904
+ struct OP_SorterData_stack_vars {
63905
+ VdbeCursor *pC;
63906
+ } bj;
6331563907
struct OP_RowData_stack_vars {
6331663908
VdbeCursor *pC;
6331763909
BtCursor *pCrsr;
6331863910
u32 n;
6331963911
i64 n64;
63320
- } bh;
63912
+ } bk;
6332163913
struct OP_Rowid_stack_vars {
6332263914
VdbeCursor *pC;
6332363915
i64 v;
6332463916
sqlite3_vtab *pVtab;
6332563917
const sqlite3_module *pModule;
63326
- } bi;
63918
+ } bl;
6332763919
struct OP_NullRow_stack_vars {
6332863920
VdbeCursor *pC;
63329
- } bj;
63921
+ } bm;
6333063922
struct OP_Last_stack_vars {
6333163923
VdbeCursor *pC;
6333263924
BtCursor *pCrsr;
6333363925
int res;
63334
- } bk;
63926
+ } bn;
6333563927
struct OP_Rewind_stack_vars {
6333663928
VdbeCursor *pC;
6333763929
BtCursor *pCrsr;
6333863930
int res;
63339
- } bl;
63931
+ } bo;
6334063932
struct OP_Next_stack_vars {
6334163933
VdbeCursor *pC;
63342
- BtCursor *pCrsr;
6334363934
int res;
63344
- } bm;
63935
+ } bp;
6334563936
struct OP_IdxInsert_stack_vars {
6334663937
VdbeCursor *pC;
6334763938
BtCursor *pCrsr;
6334863939
int nKey;
6334963940
const char *zKey;
63350
- } bn;
63941
+ } bq;
6335163942
struct OP_IdxDelete_stack_vars {
6335263943
VdbeCursor *pC;
6335363944
BtCursor *pCrsr;
6335463945
int res;
6335563946
UnpackedRecord r;
63356
- } bo;
63947
+ } br;
6335763948
struct OP_IdxRowid_stack_vars {
6335863949
BtCursor *pCrsr;
6335963950
VdbeCursor *pC;
6336063951
i64 rowid;
63361
- } bp;
63952
+ } bs;
6336263953
struct OP_IdxGE_stack_vars {
6336363954
VdbeCursor *pC;
6336463955
int res;
6336563956
UnpackedRecord r;
63366
- } bq;
63957
+ } bt;
6336763958
struct OP_Destroy_stack_vars {
6336863959
int iMoved;
6336963960
int iCnt;
6337063961
Vdbe *pVdbe;
6337163962
int iDb;
63372
- } br;
63963
+ } bu;
6337363964
struct OP_Clear_stack_vars {
6337463965
int nChange;
63375
- } bs;
63966
+ } bv;
6337663967
struct OP_CreateTable_stack_vars {
6337763968
int pgno;
6337863969
int flags;
6337963970
Db *pDb;
63380
- } bt;
63971
+ } bw;
6338163972
struct OP_ParseSchema_stack_vars {
6338263973
int iDb;
6338363974
const char *zMaster;
6338463975
char *zSql;
6338563976
InitData initData;
63386
- } bu;
63977
+ } bx;
6338763978
struct OP_IntegrityCk_stack_vars {
6338863979
int nRoot; /* Number of tables to check. (Number of root pages.) */
6338963980
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6339063981
int j; /* Loop counter */
6339163982
int nErr; /* Number of errors reported */
6339263983
char *z; /* Text of the error report */
6339363984
Mem *pnErr; /* Register keeping track of errors remaining */
63394
- } bv;
63985
+ } by;
6339563986
struct OP_RowSetRead_stack_vars {
6339663987
i64 val;
63397
- } bw;
63988
+ } bz;
6339863989
struct OP_RowSetTest_stack_vars {
6339963990
int iSet;
6340063991
int exists;
63401
- } bx;
63992
+ } ca;
6340263993
struct OP_Program_stack_vars {
6340363994
int nMem; /* Number of memory registers for sub-program */
6340463995
int nByte; /* Bytes of runtime space required for sub-program */
6340563996
Mem *pRt; /* Register to allocate runtime space */
6340663997
Mem *pMem; /* Used to iterate through memory cells */
6340763998
Mem *pEnd; /* Last memory cell in new array */
6340863999
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6340964000
SubProgram *pProgram; /* Sub-program to execute */
6341064001
void *t; /* Token identifying trigger */
63411
- } by;
64002
+ } cb;
6341264003
struct OP_Param_stack_vars {
6341364004
VdbeFrame *pFrame;
6341464005
Mem *pIn;
63415
- } bz;
64006
+ } cc;
6341664007
struct OP_MemMax_stack_vars {
6341764008
Mem *pIn1;
6341864009
VdbeFrame *pFrame;
63419
- } ca;
64010
+ } cd;
6342064011
struct OP_AggStep_stack_vars {
6342164012
int n;
6342264013
int i;
6342364014
Mem *pMem;
6342464015
Mem *pRec;
6342564016
sqlite3_context ctx;
6342664017
sqlite3_value **apVal;
63427
- } cb;
64018
+ } ce;
6342864019
struct OP_AggFinal_stack_vars {
6342964020
Mem *pMem;
63430
- } cc;
64021
+ } cf;
6343164022
struct OP_Checkpoint_stack_vars {
6343264023
int i; /* Loop counter */
6343364024
int aRes[3]; /* Results */
6343464025
Mem *pMem; /* Write results here */
63435
- } cd;
64026
+ } cg;
6343664027
struct OP_JournalMode_stack_vars {
6343764028
Btree *pBt; /* Btree to change journal mode of */
6343864029
Pager *pPager; /* Pager associated with pBt */
6343964030
int eNew; /* New journal mode */
6344064031
int eOld; /* The old journal mode */
6344164032
const char *zFilename; /* Name of database file for pPager */
63442
- } ce;
64033
+ } ch;
6344364034
struct OP_IncrVacuum_stack_vars {
6344464035
Btree *pBt;
63445
- } cf;
64036
+ } ci;
6344664037
struct OP_VBegin_stack_vars {
6344764038
VTable *pVTab;
63448
- } cg;
64039
+ } cj;
6344964040
struct OP_VOpen_stack_vars {
6345064041
VdbeCursor *pCur;
6345164042
sqlite3_vtab_cursor *pVtabCursor;
6345264043
sqlite3_vtab *pVtab;
6345364044
sqlite3_module *pModule;
63454
- } ch;
64045
+ } ck;
6345564046
struct OP_VFilter_stack_vars {
6345664047
int nArg;
6345764048
int iQuery;
6345864049
const sqlite3_module *pModule;
6345964050
Mem *pQuery;
@@ -63462,40 +64053,40 @@
6346264053
sqlite3_vtab *pVtab;
6346364054
VdbeCursor *pCur;
6346464055
int res;
6346564056
int i;
6346664057
Mem **apArg;
63467
- } ci;
64058
+ } cl;
6346864059
struct OP_VColumn_stack_vars {
6346964060
sqlite3_vtab *pVtab;
6347064061
const sqlite3_module *pModule;
6347164062
Mem *pDest;
6347264063
sqlite3_context sContext;
63473
- } cj;
64064
+ } cm;
6347464065
struct OP_VNext_stack_vars {
6347564066
sqlite3_vtab *pVtab;
6347664067
const sqlite3_module *pModule;
6347764068
int res;
6347864069
VdbeCursor *pCur;
63479
- } ck;
64070
+ } cn;
6348064071
struct OP_VRename_stack_vars {
6348164072
sqlite3_vtab *pVtab;
6348264073
Mem *pName;
63483
- } cl;
64074
+ } co;
6348464075
struct OP_VUpdate_stack_vars {
6348564076
sqlite3_vtab *pVtab;
6348664077
sqlite3_module *pModule;
6348764078
int nArg;
6348864079
int i;
6348964080
sqlite_int64 rowid;
6349064081
Mem **apArg;
6349164082
Mem *pX;
63492
- } cm;
64083
+ } cp;
6349364084
struct OP_Trace_stack_vars {
6349464085
char *zTrace;
6349564086
char *z;
63496
- } cn;
64087
+ } cq;
6349764088
} u;
6349864089
/* End automatically generated code
6349964090
********************************************************************/
6350064091
6350164092
assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -63591,11 +64182,11 @@
6359164182
if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
6359264183
assert( pOp->p2>0 );
6359364184
assert( pOp->p2<=p->nMem );
6359464185
pOut = &aMem[pOp->p2];
6359564186
memAboutToChange(p, pOut);
63596
- sqlite3VdbeMemReleaseExternal(pOut);
64187
+ MemReleaseExt(pOut);
6359764188
pOut->flags = MEM_Int;
6359864189
}
6359964190
6360064191
/* Sanity checking on other operands */
6360164192
#ifdef SQLITE_DEBUG
@@ -65061,10 +65652,11 @@
6506165652
u8 *zEndHdr; /* Pointer to first byte after the header */
6506265653
u32 offset; /* Offset into the data */
6506365654
u32 szField; /* Number of bytes in the content of a field */
6506465655
int szHdr; /* Size of the header size field at start of record */
6506565656
int avail; /* Number of bytes of available data */
65657
+ u32 t; /* A type code from the record header */
6506665658
Mem *pReg; /* PseudoTable input register */
6506765659
#endif /* local variables moved into u.am */
6506865660
6506965661
6507065662
u.am.p1 = pOp->p1;
@@ -65073,11 +65665,10 @@
6507365665
memset(&u.am.sMem, 0, sizeof(u.am.sMem));
6507465666
assert( u.am.p1<p->nCursor );
6507565667
assert( pOp->p3>0 && pOp->p3<=p->nMem );
6507665668
u.am.pDest = &aMem[pOp->p3];
6507765669
memAboutToChange(p, u.am.pDest);
65078
- MemSetTypeFlag(u.am.pDest, MEM_Null);
6507965670
u.am.zRec = 0;
6508065671
6508165672
/* This block sets the variable u.am.payloadSize to be the total number of
6508265673
** bytes in the record.
6508365674
**
@@ -65117,11 +65708,11 @@
6511765708
}else{
6511865709
assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
6511965710
rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
6512065711
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6512165712
}
65122
- }else if( u.am.pC->pseudoTableReg>0 ){
65713
+ }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
6512365714
u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
6512465715
assert( u.am.pReg->flags & MEM_Blob );
6512565716
assert( memIsValid(u.am.pReg) );
6512665717
u.am.payloadSize = u.am.pReg->n;
6512765718
u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65721,14 @@
6513065721
}else{
6513165722
/* Consider the row to be NULL */
6513265723
u.am.payloadSize = 0;
6513365724
}
6513465725
65135
- /* If u.am.payloadSize is 0, then just store a NULL */
65726
+ /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
65727
+ ** nullRow or because of a corrupt database. */
6513665728
if( u.am.payloadSize==0 ){
65137
- assert( u.am.pDest->flags&MEM_Null );
65729
+ MemSetTypeFlag(u.am.pDest, MEM_Null);
6513865730
goto op_column_out;
6513965731
}
6514065732
assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
6514165733
if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
6514265734
goto too_big;
@@ -65239,12 +65831,18 @@
6523965831
** of the record to the start of the data for the u.am.i-th column
6524065832
*/
6524165833
for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
6524265834
if( u.am.zIdx<u.am.zEndHdr ){
6524365835
u.am.aOffset[u.am.i] = u.am.offset;
65244
- u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
65245
- u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
65836
+ if( u.am.zIdx[0]<0x80 ){
65837
+ u.am.t = u.am.zIdx[0];
65838
+ u.am.zIdx++;
65839
+ }else{
65840
+ u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65841
+ }
65842
+ u.am.aType[u.am.i] = u.am.t;
65843
+ u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
6524665844
u.am.offset += u.am.szField;
6524765845
if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
6524865846
u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
6524965847
break;
6525065848
}
@@ -65281,11 +65879,11 @@
6528165879
** a pointer to a Mem object.
6528265880
*/
6528365881
if( u.am.aOffset[u.am.p2] ){
6528465882
assert( rc==SQLITE_OK );
6528565883
if( u.am.zRec ){
65286
- sqlite3VdbeMemReleaseExternal(u.am.pDest);
65884
+ MemReleaseExt(u.am.pDest);
6528765885
sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
6528865886
}else{
6528965887
u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
6529065888
sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
6529165889
rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65896,11 @@
6529865896
u.am.pDest->enc = encoding;
6529965897
}else{
6530065898
if( pOp->p4type==P4_MEM ){
6530165899
sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
6530265900
}else{
65303
- assert( u.am.pDest->flags&MEM_Null );
65901
+ MemSetTypeFlag(u.am.pDest, MEM_Null);
6530465902
}
6530565903
}
6530665904
6530765905
/* If we dynamically allocated space to hold the data (in the
6530865906
** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66098,11 @@
6550066098
i64 nEntry;
6550166099
BtCursor *pCrsr;
6550266100
#endif /* local variables moved into u.ap */
6550366101
6550466102
u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
65505
- if( u.ap.pCrsr ){
66103
+ if( ALWAYS(u.ap.pCrsr) ){
6550666104
rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
6550766105
}else{
6550866106
u.ap.nEntry = 0;
6550966107
}
6551066108
pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66674,13 @@
6607666674
u.aw.pCur->nullRow = 1;
6607766675
u.aw.pCur->isOrdered = 1;
6607866676
rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
6607966677
u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
6608066678
66081
- /* Since it performs no memory allocation or IO, the only values that
66082
- ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
66083
- ** SQLITE_EMPTY is only returned when attempting to open the table
66084
- ** rooted at page 1 of a zero-byte database. */
66085
- assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
66086
- if( rc==SQLITE_EMPTY ){
66087
- u.aw.pCur->pCursor = 0;
66088
- rc = SQLITE_OK;
66089
- }
66679
+ /* Since it performs no memory allocation or IO, the only value that
66680
+ ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66681
+ assert( rc==SQLITE_OK );
6609066682
6609166683
/* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
6609266684
** SQLite used to check if the root-page flags were sane at this point
6609366685
** and report database corruption if they were not, but this check has
6609466686
** since moved into the btree layer. */
@@ -66095,11 +66687,11 @@
6609566687
u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
6609666688
u.aw.pCur->isIndex = !u.aw.pCur->isTable;
6609766689
break;
6609866690
}
6609966691
66100
-/* Opcode: OpenEphemeral P1 P2 * P4 *
66692
+/* Opcode: OpenEphemeral P1 P2 * P4 P5
6610166693
**
6610266694
** Open a new cursor P1 to a transient table.
6610366695
** The cursor is always opened read/write even if
6610466696
** the main database is read-only. The ephemeral
6610566697
** table is deleted automatically when the cursor is closed.
@@ -66112,10 +66704,15 @@
6611266704
** This opcode was once called OpenTemp. But that created
6611366705
** confusion because the term "temp table", might refer either
6611466706
** to a TEMP table at the SQL level, or to a table opened by
6611566707
** this opcode. Then this opcode was call OpenVirtual. But
6611666708
** that created confusion with the whole virtual-table idea.
66709
+**
66710
+** The P5 parameter can be a mask of the BTREE_* flags defined
66711
+** in btree.h. These flags control aspects of the operation of
66712
+** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
66713
+** added automatically.
6611766714
*/
6611866715
/* Opcode: OpenAutoindex P1 P2 * P4 *
6611966716
**
6612066717
** This opcode works the same as OP_OpenEphemeral. It has a
6612166718
** different name to distinguish its use. Tables created using
@@ -66168,10 +66765,34 @@
6616866765
}
6616966766
u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
6617066767
u.ax.pCx->isIndex = !u.ax.pCx->isTable;
6617166768
break;
6617266769
}
66770
+
66771
+/* Opcode: OpenSorter P1 P2 * P4 *
66772
+**
66773
+** This opcode works like OP_OpenEphemeral except that it opens
66774
+** a transient index that is specifically designed to sort large
66775
+** tables using an external merge-sort algorithm.
66776
+*/
66777
+case OP_SorterOpen: {
66778
+#if 0 /* local variables moved into u.ay */
66779
+ VdbeCursor *pCx;
66780
+#endif /* local variables moved into u.ay */
66781
+#ifndef SQLITE_OMIT_MERGE_SORT
66782
+ u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66783
+ if( u.ay.pCx==0 ) goto no_mem;
66784
+ u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66785
+ u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66786
+ u.ay.pCx->isSorter = 1;
66787
+ rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
66788
+#else
66789
+ pOp->opcode = OP_OpenEphemeral;
66790
+ pc--;
66791
+#endif
66792
+ break;
66793
+}
6617366794
6617466795
/* Opcode: OpenPseudo P1 P2 P3 * *
6617566796
**
6617666797
** Open a new cursor that points to a fake table that contains a single
6617766798
** row of data. The content of that one row in the content of memory
@@ -66185,21 +66806,21 @@
6618566806
**
6618666807
** P3 is the number of fields in the records that will be stored by
6618766808
** the pseudo-table.
6618866809
*/
6618966810
case OP_OpenPseudo: {
66190
-#if 0 /* local variables moved into u.ay */
66811
+#if 0 /* local variables moved into u.az */
6619166812
VdbeCursor *pCx;
66192
-#endif /* local variables moved into u.ay */
66813
+#endif /* local variables moved into u.az */
6619366814
6619466815
assert( pOp->p1>=0 );
66195
- u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66196
- if( u.ay.pCx==0 ) goto no_mem;
66197
- u.ay.pCx->nullRow = 1;
66198
- u.ay.pCx->pseudoTableReg = pOp->p2;
66199
- u.ay.pCx->isTable = 1;
66200
- u.ay.pCx->isIndex = 0;
66816
+ u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66817
+ if( u.az.pCx==0 ) goto no_mem;
66818
+ u.az.pCx->nullRow = 1;
66819
+ u.az.pCx->pseudoTableReg = pOp->p2;
66820
+ u.az.pCx->isTable = 1;
66821
+ u.az.pCx->isIndex = 0;
6620166822
break;
6620266823
}
6620366824
6620466825
/* Opcode: Close P1 * * * *
6620566826
**
@@ -66267,39 +66888,39 @@
6626766888
*/
6626866889
case OP_SeekLt: /* jump, in3 */
6626966890
case OP_SeekLe: /* jump, in3 */
6627066891
case OP_SeekGe: /* jump, in3 */
6627166892
case OP_SeekGt: { /* jump, in3 */
66272
-#if 0 /* local variables moved into u.az */
66893
+#if 0 /* local variables moved into u.ba */
6627366894
int res;
6627466895
int oc;
6627566896
VdbeCursor *pC;
6627666897
UnpackedRecord r;
6627766898
int nField;
6627866899
i64 iKey; /* The rowid we are to seek to */
66279
-#endif /* local variables moved into u.az */
66900
+#endif /* local variables moved into u.ba */
6628066901
6628166902
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6628266903
assert( pOp->p2!=0 );
66283
- u.az.pC = p->apCsr[pOp->p1];
66284
- assert( u.az.pC!=0 );
66285
- assert( u.az.pC->pseudoTableReg==0 );
66904
+ u.ba.pC = p->apCsr[pOp->p1];
66905
+ assert( u.ba.pC!=0 );
66906
+ assert( u.ba.pC->pseudoTableReg==0 );
6628666907
assert( OP_SeekLe == OP_SeekLt+1 );
6628766908
assert( OP_SeekGe == OP_SeekLt+2 );
6628866909
assert( OP_SeekGt == OP_SeekLt+3 );
66289
- assert( u.az.pC->isOrdered );
66290
- if( u.az.pC->pCursor!=0 ){
66291
- u.az.oc = pOp->opcode;
66292
- u.az.pC->nullRow = 0;
66293
- if( u.az.pC->isTable ){
66910
+ assert( u.ba.pC->isOrdered );
66911
+ if( ALWAYS(u.ba.pC->pCursor!=0) ){
66912
+ u.ba.oc = pOp->opcode;
66913
+ u.ba.pC->nullRow = 0;
66914
+ if( u.ba.pC->isTable ){
6629466915
/* The input value in P3 might be of any type: integer, real, string,
6629566916
** blob, or NULL. But it needs to be an integer before we can do
6629666917
** the seek, so covert it. */
6629766918
pIn3 = &aMem[pOp->p3];
6629866919
applyNumericAffinity(pIn3);
66299
- u.az.iKey = sqlite3VdbeIntValue(pIn3);
66300
- u.az.pC->rowidIsValid = 0;
66920
+ u.ba.iKey = sqlite3VdbeIntValue(pIn3);
66921
+ u.ba.pC->rowidIsValid = 0;
6630166922
6630266923
/* If the P3 value could not be converted into an integer without
6630366924
** loss of information, then special processing is required... */
6630466925
if( (pIn3->flags & MEM_Int)==0 ){
6630566926
if( (pIn3->flags & MEM_Real)==0 ){
@@ -66310,105 +66931,105 @@
6631066931
}
6631166932
/* If we reach this point, then the P3 value must be a floating
6631266933
** point number. */
6631366934
assert( (pIn3->flags & MEM_Real)!=0 );
6631466935
66315
- if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
66316
- /* The P3 value is too large in magnitude to be expressed as an
66317
- ** integer. */
66318
- u.az.res = 1;
66319
- if( pIn3->r<0 ){
66320
- if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66321
- rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
66322
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66323
- }
66324
- }else{
66325
- if( u.az.oc<=OP_SeekLe ){ assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66326
- rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
66327
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66328
- }
66329
- }
66330
- if( u.az.res ){
66331
- pc = pOp->p2 - 1;
66332
- }
66333
- break;
66334
- }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
66335
- /* Use the ceiling() function to convert real->int */
66336
- if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
66337
- }else{
66338
- /* Use the floor() function to convert real->int */
66339
- assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
66340
- if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
66341
- }
66342
- }
66343
- rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
66344
- if( rc!=SQLITE_OK ){
66345
- goto abort_due_to_error;
66346
- }
66347
- if( u.az.res==0 ){
66348
- u.az.pC->rowidIsValid = 1;
66349
- u.az.pC->lastRowid = u.az.iKey;
66350
- }
66351
- }else{
66352
- u.az.nField = pOp->p4.i;
66353
- assert( pOp->p4type==P4_INT32 );
66354
- assert( u.az.nField>0 );
66355
- u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
66356
- u.az.r.nField = (u16)u.az.nField;
66357
-
66358
- /* The next line of code computes as follows, only faster:
66359
- ** if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
66360
- ** u.az.r.flags = UNPACKED_INCRKEY;
66361
- ** }else{
66362
- ** u.az.r.flags = 0;
66363
- ** }
66364
- */
66365
- u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt)));
66366
- assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY );
66367
- assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY );
66368
- assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 );
66369
- assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 );
66370
-
66371
- u.az.r.aMem = &aMem[pOp->p3];
66372
-#ifdef SQLITE_DEBUG
66373
- { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); }
66374
-#endif
66375
- ExpandBlob(u.az.r.aMem);
66376
- rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
66377
- if( rc!=SQLITE_OK ){
66378
- goto abort_due_to_error;
66379
- }
66380
- u.az.pC->rowidIsValid = 0;
66381
- }
66382
- u.az.pC->deferredMoveto = 0;
66383
- u.az.pC->cacheStatus = CACHE_STALE;
66384
-#ifdef SQLITE_TEST
66385
- sqlite3_search_count++;
66386
-#endif
66387
- if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66388
- if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
66389
- rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
66390
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66391
- u.az.pC->rowidIsValid = 0;
66392
- }else{
66393
- u.az.res = 0;
66394
- }
66395
- }else{
66396
- assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66397
- if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
66398
- rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
66399
- if( rc!=SQLITE_OK ) goto abort_due_to_error;
66400
- u.az.pC->rowidIsValid = 0;
66401
- }else{
66402
- /* u.az.res might be negative because the table is empty. Check to
66403
- ** see if this is the case.
66404
- */
66405
- u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
66406
- }
66407
- }
66408
- assert( pOp->p2>0 );
66409
- if( u.az.res ){
66936
+ if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
66937
+ /* The P3 value is too large in magnitude to be expressed as an
66938
+ ** integer. */
66939
+ u.ba.res = 1;
66940
+ if( pIn3->r<0 ){
66941
+ if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66942
+ rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
66943
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
66944
+ }
66945
+ }else{
66946
+ if( u.ba.oc<=OP_SeekLe ){ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66947
+ rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
66948
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
66949
+ }
66950
+ }
66951
+ if( u.ba.res ){
66952
+ pc = pOp->p2 - 1;
66953
+ }
66954
+ break;
66955
+ }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
66956
+ /* Use the ceiling() function to convert real->int */
66957
+ if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
66958
+ }else{
66959
+ /* Use the floor() function to convert real->int */
66960
+ assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
66961
+ if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
66962
+ }
66963
+ }
66964
+ rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
66965
+ if( rc!=SQLITE_OK ){
66966
+ goto abort_due_to_error;
66967
+ }
66968
+ if( u.ba.res==0 ){
66969
+ u.ba.pC->rowidIsValid = 1;
66970
+ u.ba.pC->lastRowid = u.ba.iKey;
66971
+ }
66972
+ }else{
66973
+ u.ba.nField = pOp->p4.i;
66974
+ assert( pOp->p4type==P4_INT32 );
66975
+ assert( u.ba.nField>0 );
66976
+ u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
66977
+ u.ba.r.nField = (u16)u.ba.nField;
66978
+
66979
+ /* The next line of code computes as follows, only faster:
66980
+ ** if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
66981
+ ** u.ba.r.flags = UNPACKED_INCRKEY;
66982
+ ** }else{
66983
+ ** u.ba.r.flags = 0;
66984
+ ** }
66985
+ */
66986
+ u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
66987
+ assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
66988
+ assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
66989
+ assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
66990
+ assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
66991
+
66992
+ u.ba.r.aMem = &aMem[pOp->p3];
66993
+#ifdef SQLITE_DEBUG
66994
+ { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
66995
+#endif
66996
+ ExpandBlob(u.ba.r.aMem);
66997
+ rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
66998
+ if( rc!=SQLITE_OK ){
66999
+ goto abort_due_to_error;
67000
+ }
67001
+ u.ba.pC->rowidIsValid = 0;
67002
+ }
67003
+ u.ba.pC->deferredMoveto = 0;
67004
+ u.ba.pC->cacheStatus = CACHE_STALE;
67005
+#ifdef SQLITE_TEST
67006
+ sqlite3_search_count++;
67007
+#endif
67008
+ if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
67009
+ if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
67010
+ rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
67011
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67012
+ u.ba.pC->rowidIsValid = 0;
67013
+ }else{
67014
+ u.ba.res = 0;
67015
+ }
67016
+ }else{
67017
+ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
67018
+ if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
67019
+ rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
67020
+ if( rc!=SQLITE_OK ) goto abort_due_to_error;
67021
+ u.ba.pC->rowidIsValid = 0;
67022
+ }else{
67023
+ /* u.ba.res might be negative because the table is empty. Check to
67024
+ ** see if this is the case.
67025
+ */
67026
+ u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
67027
+ }
67028
+ }
67029
+ assert( pOp->p2>0 );
67030
+ if( u.ba.res ){
6641067031
pc = pOp->p2 - 1;
6641167032
}
6641267033
}else{
6641367034
/* This happens when attempting to open the sqlite3_master table
6641467035
** for read access returns SQLITE_EMPTY. In this case always
@@ -66427,24 +67048,24 @@
6642767048
** This is actually a deferred seek. Nothing actually happens until
6642867049
** the cursor is used to read a record. That way, if no reads
6642967050
** occur, no unnecessary I/O happens.
6643067051
*/
6643167052
case OP_Seek: { /* in2 */
66432
-#if 0 /* local variables moved into u.ba */
67053
+#if 0 /* local variables moved into u.bb */
6643367054
VdbeCursor *pC;
66434
-#endif /* local variables moved into u.ba */
67055
+#endif /* local variables moved into u.bb */
6643567056
6643667057
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66437
- u.ba.pC = p->apCsr[pOp->p1];
66438
- assert( u.ba.pC!=0 );
66439
- if( ALWAYS(u.ba.pC->pCursor!=0) ){
66440
- assert( u.ba.pC->isTable );
66441
- u.ba.pC->nullRow = 0;
67058
+ u.bb.pC = p->apCsr[pOp->p1];
67059
+ assert( u.bb.pC!=0 );
67060
+ if( ALWAYS(u.bb.pC->pCursor!=0) ){
67061
+ assert( u.bb.pC->isTable );
67062
+ u.bb.pC->nullRow = 0;
6644267063
pIn2 = &aMem[pOp->p2];
66443
- u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
66444
- u.ba.pC->rowidIsValid = 0;
66445
- u.ba.pC->deferredMoveto = 1;
67064
+ u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67065
+ u.bb.pC->rowidIsValid = 0;
67066
+ u.bb.pC->deferredMoveto = 1;
6644667067
}
6644767068
break;
6644867069
}
6644967070
6645067071
@@ -66472,66 +67093,66 @@
6647267093
**
6647367094
** See also: Found, NotExists, IsUnique
6647467095
*/
6647567096
case OP_NotFound: /* jump, in3 */
6647667097
case OP_Found: { /* jump, in3 */
66477
-#if 0 /* local variables moved into u.bb */
67098
+#if 0 /* local variables moved into u.bc */
6647867099
int alreadyExists;
6647967100
VdbeCursor *pC;
6648067101
int res;
6648167102
UnpackedRecord *pIdxKey;
6648267103
UnpackedRecord r;
6648367104
char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
66484
-#endif /* local variables moved into u.bb */
67105
+#endif /* local variables moved into u.bc */
6648567106
6648667107
#ifdef SQLITE_TEST
6648767108
sqlite3_found_count++;
6648867109
#endif
6648967110
66490
- u.bb.alreadyExists = 0;
67111
+ u.bc.alreadyExists = 0;
6649167112
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6649267113
assert( pOp->p4type==P4_INT32 );
66493
- u.bb.pC = p->apCsr[pOp->p1];
66494
- assert( u.bb.pC!=0 );
67114
+ u.bc.pC = p->apCsr[pOp->p1];
67115
+ assert( u.bc.pC!=0 );
6649567116
pIn3 = &aMem[pOp->p3];
66496
- if( ALWAYS(u.bb.pC->pCursor!=0) ){
67117
+ if( ALWAYS(u.bc.pC->pCursor!=0) ){
6649767118
66498
- assert( u.bb.pC->isTable==0 );
67119
+ assert( u.bc.pC->isTable==0 );
6649967120
if( pOp->p4.i>0 ){
66500
- u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
66501
- u.bb.r.nField = (u16)pOp->p4.i;
66502
- u.bb.r.aMem = pIn3;
67121
+ u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67122
+ u.bc.r.nField = (u16)pOp->p4.i;
67123
+ u.bc.r.aMem = pIn3;
6650367124
#ifdef SQLITE_DEBUG
66504
- { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
67125
+ { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
6650567126
#endif
66506
- u.bb.r.flags = UNPACKED_PREFIX_MATCH;
66507
- u.bb.pIdxKey = &u.bb.r;
67127
+ u.bc.r.flags = UNPACKED_PREFIX_MATCH;
67128
+ u.bc.pIdxKey = &u.bc.r;
6650867129
}else{
6650967130
assert( pIn3->flags & MEM_Blob );
6651067131
assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
66511
- u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
66512
- u.bb.aTempRec, sizeof(u.bb.aTempRec));
66513
- if( u.bb.pIdxKey==0 ){
67132
+ u.bc.pIdxKey = sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z,
67133
+ u.bc.aTempRec, sizeof(u.bc.aTempRec));
67134
+ if( u.bc.pIdxKey==0 ){
6651467135
goto no_mem;
6651567136
}
66516
- u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67137
+ u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
6651767138
}
66518
- rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
67139
+ rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
6651967140
if( pOp->p4.i==0 ){
66520
- sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
67141
+ sqlite3VdbeDeleteUnpackedRecord(u.bc.pIdxKey);
6652167142
}
6652267143
if( rc!=SQLITE_OK ){
6652367144
break;
6652467145
}
66525
- u.bb.alreadyExists = (u.bb.res==0);
66526
- u.bb.pC->deferredMoveto = 0;
66527
- u.bb.pC->cacheStatus = CACHE_STALE;
67146
+ u.bc.alreadyExists = (u.bc.res==0);
67147
+ u.bc.pC->deferredMoveto = 0;
67148
+ u.bc.pC->cacheStatus = CACHE_STALE;
6652867149
}
6652967150
if( pOp->opcode==OP_Found ){
66530
- if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
67151
+ if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
6653167152
}else{
66532
- if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
67153
+ if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
6653367154
}
6653467155
break;
6653567156
}
6653667157
6653767158
/* Opcode: IsUnique P1 P2 P3 P4 *
@@ -66559,67 +67180,67 @@
6655967180
** instruction.
6656067181
**
6656167182
** See also: NotFound, NotExists, Found
6656267183
*/
6656367184
case OP_IsUnique: { /* jump, in3 */
66564
-#if 0 /* local variables moved into u.bc */
67185
+#if 0 /* local variables moved into u.bd */
6656567186
u16 ii;
6656667187
VdbeCursor *pCx;
6656767188
BtCursor *pCrsr;
6656867189
u16 nField;
6656967190
Mem *aMx;
6657067191
UnpackedRecord r; /* B-Tree index search key */
6657167192
i64 R; /* Rowid stored in register P3 */
66572
-#endif /* local variables moved into u.bc */
67193
+#endif /* local variables moved into u.bd */
6657367194
6657467195
pIn3 = &aMem[pOp->p3];
66575
- u.bc.aMx = &aMem[pOp->p4.i];
67196
+ u.bd.aMx = &aMem[pOp->p4.i];
6657667197
/* Assert that the values of parameters P1 and P4 are in range. */
6657767198
assert( pOp->p4type==P4_INT32 );
6657867199
assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
6657967200
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6658067201
6658167202
/* Find the index cursor. */
66582
- u.bc.pCx = p->apCsr[pOp->p1];
66583
- assert( u.bc.pCx->deferredMoveto==0 );
66584
- u.bc.pCx->seekResult = 0;
66585
- u.bc.pCx->cacheStatus = CACHE_STALE;
66586
- u.bc.pCrsr = u.bc.pCx->pCursor;
67203
+ u.bd.pCx = p->apCsr[pOp->p1];
67204
+ assert( u.bd.pCx->deferredMoveto==0 );
67205
+ u.bd.pCx->seekResult = 0;
67206
+ u.bd.pCx->cacheStatus = CACHE_STALE;
67207
+ u.bd.pCrsr = u.bd.pCx->pCursor;
6658767208
6658867209
/* If any of the values are NULL, take the jump. */
66589
- u.bc.nField = u.bc.pCx->pKeyInfo->nField;
66590
- for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
66591
- if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){
67210
+ u.bd.nField = u.bd.pCx->pKeyInfo->nField;
67211
+ for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
67212
+ if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
6659267213
pc = pOp->p2 - 1;
66593
- u.bc.pCrsr = 0;
67214
+ u.bd.pCrsr = 0;
6659467215
break;
6659567216
}
6659667217
}
66597
- assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 );
67218
+ assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
6659867219
66599
- if( u.bc.pCrsr!=0 ){
67220
+ if( u.bd.pCrsr!=0 ){
6660067221
/* Populate the index search key. */
66601
- u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
66602
- u.bc.r.nField = u.bc.nField + 1;
66603
- u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
66604
- u.bc.r.aMem = u.bc.aMx;
67222
+ u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
67223
+ u.bd.r.nField = u.bd.nField + 1;
67224
+ u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
67225
+ u.bd.r.aMem = u.bd.aMx;
6660567226
#ifdef SQLITE_DEBUG
66606
- { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67227
+ { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
6660767228
#endif
6660867229
66609
- /* Extract the value of u.bc.R from register P3. */
67230
+ /* Extract the value of u.bd.R from register P3. */
6661067231
sqlite3VdbeMemIntegerify(pIn3);
66611
- u.bc.R = pIn3->u.i;
67232
+ u.bd.R = pIn3->u.i;
6661267233
6661367234
/* Search the B-Tree index. If no conflicting record is found, jump
6661467235
** to P2. Otherwise, copy the rowid of the conflicting record to
6661567236
** register P3 and fall through to the next instruction. */
66616
- rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
66617
- if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
67237
+ rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
67238
+ if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
6661867239
pc = pOp->p2 - 1;
6661967240
}else{
66620
- pIn3->u.i = u.bc.r.rowid;
67241
+ pIn3->u.i = u.bd.r.rowid;
6662167242
}
6662267243
}
6662367244
break;
6662467245
}
6662567246
@@ -66636,46 +67257,46 @@
6663667257
** P1 is an index.
6663767258
**
6663867259
** See also: Found, NotFound, IsUnique
6663967260
*/
6664067261
case OP_NotExists: { /* jump, in3 */
66641
-#if 0 /* local variables moved into u.bd */
67262
+#if 0 /* local variables moved into u.be */
6664267263
VdbeCursor *pC;
6664367264
BtCursor *pCrsr;
6664467265
int res;
6664567266
u64 iKey;
66646
-#endif /* local variables moved into u.bd */
67267
+#endif /* local variables moved into u.be */
6664767268
6664867269
pIn3 = &aMem[pOp->p3];
6664967270
assert( pIn3->flags & MEM_Int );
6665067271
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66651
- u.bd.pC = p->apCsr[pOp->p1];
66652
- assert( u.bd.pC!=0 );
66653
- assert( u.bd.pC->isTable );
66654
- assert( u.bd.pC->pseudoTableReg==0 );
66655
- u.bd.pCrsr = u.bd.pC->pCursor;
66656
- if( u.bd.pCrsr!=0 ){
66657
- u.bd.res = 0;
66658
- u.bd.iKey = pIn3->u.i;
66659
- rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
66660
- u.bd.pC->lastRowid = pIn3->u.i;
66661
- u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
66662
- u.bd.pC->nullRow = 0;
66663
- u.bd.pC->cacheStatus = CACHE_STALE;
66664
- u.bd.pC->deferredMoveto = 0;
66665
- if( u.bd.res!=0 ){
67272
+ u.be.pC = p->apCsr[pOp->p1];
67273
+ assert( u.be.pC!=0 );
67274
+ assert( u.be.pC->isTable );
67275
+ assert( u.be.pC->pseudoTableReg==0 );
67276
+ u.be.pCrsr = u.be.pC->pCursor;
67277
+ if( ALWAYS(u.be.pCrsr!=0) ){
67278
+ u.be.res = 0;
67279
+ u.be.iKey = pIn3->u.i;
67280
+ rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
67281
+ u.be.pC->lastRowid = pIn3->u.i;
67282
+ u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
67283
+ u.be.pC->nullRow = 0;
67284
+ u.be.pC->cacheStatus = CACHE_STALE;
67285
+ u.be.pC->deferredMoveto = 0;
67286
+ if( u.be.res!=0 ){
6666667287
pc = pOp->p2 - 1;
66667
- assert( u.bd.pC->rowidIsValid==0 );
67288
+ assert( u.be.pC->rowidIsValid==0 );
6666867289
}
66669
- u.bd.pC->seekResult = u.bd.res;
67290
+ u.be.pC->seekResult = u.be.res;
6667067291
}else{
6667167292
/* This happens when an attempt to open a read cursor on the
6667267293
** sqlite_master table returns SQLITE_EMPTY.
6667367294
*/
6667467295
pc = pOp->p2 - 1;
66675
- assert( u.bd.pC->rowidIsValid==0 );
66676
- u.bd.pC->seekResult = 0;
67296
+ assert( u.be.pC->rowidIsValid==0 );
67297
+ u.be.pC->seekResult = 0;
6667767298
}
6667867299
break;
6667967300
}
6668067301
6668167302
/* Opcode: Sequence P1 P2 * * *
@@ -66706,25 +67327,25 @@
6670667327
** an SQLITE_FULL error is generated. The P3 register is updated with the '
6670767328
** generated record number. This P3 mechanism is used to help implement the
6670867329
** AUTOINCREMENT feature.
6670967330
*/
6671067331
case OP_NewRowid: { /* out2-prerelease */
66711
-#if 0 /* local variables moved into u.be */
67332
+#if 0 /* local variables moved into u.bf */
6671267333
i64 v; /* The new rowid */
6671367334
VdbeCursor *pC; /* Cursor of table to get the new rowid */
6671467335
int res; /* Result of an sqlite3BtreeLast() */
6671567336
int cnt; /* Counter to limit the number of searches */
6671667337
Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
6671767338
VdbeFrame *pFrame; /* Root frame of VDBE */
66718
-#endif /* local variables moved into u.be */
67339
+#endif /* local variables moved into u.bf */
6671967340
66720
- u.be.v = 0;
66721
- u.be.res = 0;
67341
+ u.bf.v = 0;
67342
+ u.bf.res = 0;
6672267343
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66723
- u.be.pC = p->apCsr[pOp->p1];
66724
- assert( u.be.pC!=0 );
66725
- if( NEVER(u.be.pC->pCursor==0) ){
67344
+ u.bf.pC = p->apCsr[pOp->p1];
67345
+ assert( u.bf.pC!=0 );
67346
+ if( NEVER(u.bf.pC->pCursor==0) ){
6672667347
/* The zero initialization above is all that is needed */
6672767348
}else{
6672867349
/* The next rowid or record number (different terms for the same
6672967350
** thing) is obtained in a two-step algorithm.
6673067351
**
@@ -66736,11 +67357,11 @@
6673667357
** The second algorithm is to select a rowid at random and see if
6673767358
** it already exists in the table. If it does not exist, we have
6673867359
** succeeded. If the random rowid does exist, we select a new one
6673967360
** and try again, up to 100 times.
6674067361
*/
66741
- assert( u.be.pC->isTable );
67362
+ assert( u.bf.pC->isTable );
6674267363
6674367364
#ifdef SQLITE_32BIT_ROWID
6674467365
# define MAX_ROWID 0x7fffffff
6674567366
#else
6674667367
/* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -66748,101 +67369,101 @@
6674867369
** to provide the constant while making all compilers happy.
6674967370
*/
6675067371
# define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
6675167372
#endif
6675267373
66753
- if( !u.be.pC->useRandomRowid ){
66754
- u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
66755
- if( u.be.v==0 ){
66756
- rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
67374
+ if( !u.bf.pC->useRandomRowid ){
67375
+ u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
67376
+ if( u.bf.v==0 ){
67377
+ rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
6675767378
if( rc!=SQLITE_OK ){
6675867379
goto abort_due_to_error;
6675967380
}
66760
- if( u.be.res ){
66761
- u.be.v = 1; /* IMP: R-61914-48074 */
67381
+ if( u.bf.res ){
67382
+ u.bf.v = 1; /* IMP: R-61914-48074 */
6676267383
}else{
66763
- assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
66764
- rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
67384
+ assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
67385
+ rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
6676567386
assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
66766
- if( u.be.v==MAX_ROWID ){
66767
- u.be.pC->useRandomRowid = 1;
67387
+ if( u.bf.v==MAX_ROWID ){
67388
+ u.bf.pC->useRandomRowid = 1;
6676867389
}else{
66769
- u.be.v++; /* IMP: R-29538-34987 */
67390
+ u.bf.v++; /* IMP: R-29538-34987 */
6677067391
}
6677167392
}
6677267393
}
6677367394
6677467395
#ifndef SQLITE_OMIT_AUTOINCREMENT
6677567396
if( pOp->p3 ){
6677667397
/* Assert that P3 is a valid memory cell. */
6677767398
assert( pOp->p3>0 );
6677867399
if( p->pFrame ){
66779
- for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
67400
+ for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
6678067401
/* Assert that P3 is a valid memory cell. */
66781
- assert( pOp->p3<=u.be.pFrame->nMem );
66782
- u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
67402
+ assert( pOp->p3<=u.bf.pFrame->nMem );
67403
+ u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
6678367404
}else{
6678467405
/* Assert that P3 is a valid memory cell. */
6678567406
assert( pOp->p3<=p->nMem );
66786
- u.be.pMem = &aMem[pOp->p3];
66787
- memAboutToChange(p, u.be.pMem);
66788
- }
66789
- assert( memIsValid(u.be.pMem) );
66790
-
66791
- REGISTER_TRACE(pOp->p3, u.be.pMem);
66792
- sqlite3VdbeMemIntegerify(u.be.pMem);
66793
- assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
66794
- if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
67407
+ u.bf.pMem = &aMem[pOp->p3];
67408
+ memAboutToChange(p, u.bf.pMem);
67409
+ }
67410
+ assert( memIsValid(u.bf.pMem) );
67411
+
67412
+ REGISTER_TRACE(pOp->p3, u.bf.pMem);
67413
+ sqlite3VdbeMemIntegerify(u.bf.pMem);
67414
+ assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67415
+ if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
6679567416
rc = SQLITE_FULL; /* IMP: R-12275-61338 */
6679667417
goto abort_due_to_error;
6679767418
}
66798
- if( u.be.v<u.be.pMem->u.i+1 ){
66799
- u.be.v = u.be.pMem->u.i + 1;
67419
+ if( u.bf.v<u.bf.pMem->u.i+1 ){
67420
+ u.bf.v = u.bf.pMem->u.i + 1;
6680067421
}
66801
- u.be.pMem->u.i = u.be.v;
67422
+ u.bf.pMem->u.i = u.bf.v;
6680267423
}
6680367424
#endif
6680467425
66805
- sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
67426
+ sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
6680667427
}
66807
- if( u.be.pC->useRandomRowid ){
67428
+ if( u.bf.pC->useRandomRowid ){
6680867429
/* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
6680967430
** largest possible integer (9223372036854775807) then the database
6681067431
** engine starts picking positive candidate ROWIDs at random until
6681167432
** it finds one that is not previously used. */
6681267433
assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
6681367434
** an AUTOINCREMENT table. */
6681467435
/* on the first attempt, simply do one more than previous */
66815
- u.be.v = lastRowid;
66816
- u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66817
- u.be.v++; /* ensure non-zero */
66818
- u.be.cnt = 0;
66819
- while( ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v,
66820
- 0, &u.be.res))==SQLITE_OK)
66821
- && (u.be.res==0)
66822
- && (++u.be.cnt<100)){
67436
+ u.bf.v = lastRowid;
67437
+ u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67438
+ u.bf.v++; /* ensure non-zero */
67439
+ u.bf.cnt = 0;
67440
+ while( ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
67441
+ 0, &u.bf.res))==SQLITE_OK)
67442
+ && (u.bf.res==0)
67443
+ && (++u.bf.cnt<100)){
6682367444
/* collision - try another random rowid */
66824
- sqlite3_randomness(sizeof(u.be.v), &u.be.v);
66825
- if( u.be.cnt<5 ){
67445
+ sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
67446
+ if( u.bf.cnt<5 ){
6682667447
/* try "small" random rowids for the initial attempts */
66827
- u.be.v &= 0xffffff;
67448
+ u.bf.v &= 0xffffff;
6682867449
}else{
66829
- u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67450
+ u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
6683067451
}
66831
- u.be.v++; /* ensure non-zero */
67452
+ u.bf.v++; /* ensure non-zero */
6683267453
}
66833
- if( rc==SQLITE_OK && u.be.res==0 ){
67454
+ if( rc==SQLITE_OK && u.bf.res==0 ){
6683467455
rc = SQLITE_FULL; /* IMP: R-38219-53002 */
6683567456
goto abort_due_to_error;
6683667457
}
66837
- assert( u.be.v>0 ); /* EV: R-40812-03570 */
67458
+ assert( u.bf.v>0 ); /* EV: R-40812-03570 */
6683867459
}
66839
- u.be.pC->rowidIsValid = 0;
66840
- u.be.pC->deferredMoveto = 0;
66841
- u.be.pC->cacheStatus = CACHE_STALE;
67460
+ u.bf.pC->rowidIsValid = 0;
67461
+ u.bf.pC->deferredMoveto = 0;
67462
+ u.bf.pC->cacheStatus = CACHE_STALE;
6684267463
}
66843
- pOut->u.i = u.be.v;
67464
+ pOut->u.i = u.bf.v;
6684467465
break;
6684567466
}
6684667467
6684767468
/* Opcode: Insert P1 P2 P3 P4 P5
6684867469
**
@@ -66888,74 +67509,74 @@
6688867509
** This works exactly like OP_Insert except that the key is the
6688967510
** integer value P3, not the value of the integer stored in register P3.
6689067511
*/
6689167512
case OP_Insert:
6689267513
case OP_InsertInt: {
66893
-#if 0 /* local variables moved into u.bf */
67514
+#if 0 /* local variables moved into u.bg */
6689467515
Mem *pData; /* MEM cell holding data for the record to be inserted */
6689567516
Mem *pKey; /* MEM cell holding key for the record */
6689667517
i64 iKey; /* The integer ROWID or key for the record to be inserted */
6689767518
VdbeCursor *pC; /* Cursor to table into which insert is written */
6689867519
int nZero; /* Number of zero-bytes to append */
6689967520
int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
6690067521
const char *zDb; /* database name - used by the update hook */
6690167522
const char *zTbl; /* Table name - used by the opdate hook */
6690267523
int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
66903
-#endif /* local variables moved into u.bf */
67524
+#endif /* local variables moved into u.bg */
6690467525
66905
- u.bf.pData = &aMem[pOp->p2];
67526
+ u.bg.pData = &aMem[pOp->p2];
6690667527
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66907
- assert( memIsValid(u.bf.pData) );
66908
- u.bf.pC = p->apCsr[pOp->p1];
66909
- assert( u.bf.pC!=0 );
66910
- assert( u.bf.pC->pCursor!=0 );
66911
- assert( u.bf.pC->pseudoTableReg==0 );
66912
- assert( u.bf.pC->isTable );
66913
- REGISTER_TRACE(pOp->p2, u.bf.pData);
67528
+ assert( memIsValid(u.bg.pData) );
67529
+ u.bg.pC = p->apCsr[pOp->p1];
67530
+ assert( u.bg.pC!=0 );
67531
+ assert( u.bg.pC->pCursor!=0 );
67532
+ assert( u.bg.pC->pseudoTableReg==0 );
67533
+ assert( u.bg.pC->isTable );
67534
+ REGISTER_TRACE(pOp->p2, u.bg.pData);
6691467535
6691567536
if( pOp->opcode==OP_Insert ){
66916
- u.bf.pKey = &aMem[pOp->p3];
66917
- assert( u.bf.pKey->flags & MEM_Int );
66918
- assert( memIsValid(u.bf.pKey) );
66919
- REGISTER_TRACE(pOp->p3, u.bf.pKey);
66920
- u.bf.iKey = u.bf.pKey->u.i;
67537
+ u.bg.pKey = &aMem[pOp->p3];
67538
+ assert( u.bg.pKey->flags & MEM_Int );
67539
+ assert( memIsValid(u.bg.pKey) );
67540
+ REGISTER_TRACE(pOp->p3, u.bg.pKey);
67541
+ u.bg.iKey = u.bg.pKey->u.i;
6692167542
}else{
6692267543
assert( pOp->opcode==OP_InsertInt );
66923
- u.bf.iKey = pOp->p3;
67544
+ u.bg.iKey = pOp->p3;
6692467545
}
6692567546
6692667547
if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
66927
- if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bf.iKey;
66928
- if( u.bf.pData->flags & MEM_Null ){
66929
- u.bf.pData->z = 0;
66930
- u.bf.pData->n = 0;
66931
- }else{
66932
- assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
66933
- }
66934
- u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
66935
- if( u.bf.pData->flags & MEM_Zero ){
66936
- u.bf.nZero = u.bf.pData->u.nZero;
66937
- }else{
66938
- u.bf.nZero = 0;
66939
- }
66940
- sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
66941
- rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
66942
- u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
66943
- pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
67548
+ if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
67549
+ if( u.bg.pData->flags & MEM_Null ){
67550
+ u.bg.pData->z = 0;
67551
+ u.bg.pData->n = 0;
67552
+ }else{
67553
+ assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
67554
+ }
67555
+ u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
67556
+ if( u.bg.pData->flags & MEM_Zero ){
67557
+ u.bg.nZero = u.bg.pData->u.nZero;
67558
+ }else{
67559
+ u.bg.nZero = 0;
67560
+ }
67561
+ sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67562
+ rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
67563
+ u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
67564
+ pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
6694467565
);
66945
- u.bf.pC->rowidIsValid = 0;
66946
- u.bf.pC->deferredMoveto = 0;
66947
- u.bf.pC->cacheStatus = CACHE_STALE;
67566
+ u.bg.pC->rowidIsValid = 0;
67567
+ u.bg.pC->deferredMoveto = 0;
67568
+ u.bg.pC->cacheStatus = CACHE_STALE;
6694867569
6694967570
/* Invoke the update-hook if required. */
6695067571
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66951
- u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
66952
- u.bf.zTbl = pOp->p4.z;
66953
- u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
66954
- assert( u.bf.pC->isTable );
66955
- db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
66956
- assert( u.bf.pC->iDb>=0 );
67572
+ u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
67573
+ u.bg.zTbl = pOp->p4.z;
67574
+ u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67575
+ assert( u.bg.pC->isTable );
67576
+ db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
67577
+ assert( u.bg.pC->iDb>=0 );
6695767578
}
6695867579
break;
6695967580
}
6696067581
6696167582
/* Opcode: Delete P1 P2 * P4 *
@@ -66977,51 +67598,51 @@
6697767598
** pointing to. The update hook will be invoked, if it exists.
6697867599
** If P4 is not NULL then the P1 cursor must have been positioned
6697967600
** using OP_NotFound prior to invoking this opcode.
6698067601
*/
6698167602
case OP_Delete: {
66982
-#if 0 /* local variables moved into u.bg */
67603
+#if 0 /* local variables moved into u.bh */
6698367604
i64 iKey;
6698467605
VdbeCursor *pC;
66985
-#endif /* local variables moved into u.bg */
67606
+#endif /* local variables moved into u.bh */
6698667607
66987
- u.bg.iKey = 0;
67608
+ u.bh.iKey = 0;
6698867609
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66989
- u.bg.pC = p->apCsr[pOp->p1];
66990
- assert( u.bg.pC!=0 );
66991
- assert( u.bg.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67610
+ u.bh.pC = p->apCsr[pOp->p1];
67611
+ assert( u.bh.pC!=0 );
67612
+ assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
6699267613
66993
- /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
67614
+ /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
6699467615
** row being deleted.
6699567616
*/
6699667617
if( db->xUpdateCallback && pOp->p4.z ){
66997
- assert( u.bg.pC->isTable );
66998
- assert( u.bg.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
66999
- u.bg.iKey = u.bg.pC->lastRowid;
67618
+ assert( u.bh.pC->isTable );
67619
+ assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67620
+ u.bh.iKey = u.bh.pC->lastRowid;
6700067621
}
6700167622
6700267623
/* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
6700367624
** OP_Column on the same table without any intervening operations that
67004
- ** might move or invalidate the cursor. Hence cursor u.bg.pC is always pointing
67625
+ ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
6700567626
** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
6700667627
** below is always a no-op and cannot fail. We will run it anyhow, though,
6700767628
** to guard against future changes to the code generator.
6700867629
**/
67009
- assert( u.bg.pC->deferredMoveto==0 );
67010
- rc = sqlite3VdbeCursorMoveto(u.bg.pC);
67630
+ assert( u.bh.pC->deferredMoveto==0 );
67631
+ rc = sqlite3VdbeCursorMoveto(u.bh.pC);
6701167632
if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
6701267633
67013
- sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67014
- rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
67015
- u.bg.pC->cacheStatus = CACHE_STALE;
67634
+ sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67635
+ rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
67636
+ u.bh.pC->cacheStatus = CACHE_STALE;
6701667637
6701767638
/* Invoke the update-hook if required. */
6701867639
if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67019
- const char *zDb = db->aDb[u.bg.pC->iDb].zName;
67640
+ const char *zDb = db->aDb[u.bh.pC->iDb].zName;
6702067641
const char *zTbl = pOp->p4.z;
67021
- db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
67022
- assert( u.bg.pC->iDb>=0 );
67642
+ db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
67643
+ assert( u.bh.pC->iDb>=0 );
6702367644
}
6702467645
if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
6702567646
break;
6702667647
}
6702767648
/* Opcode: ResetCount * * * * *
@@ -67034,10 +67655,53 @@
6703467655
case OP_ResetCount: {
6703567656
sqlite3VdbeSetChanges(db, p->nChange);
6703667657
p->nChange = 0;
6703767658
break;
6703867659
}
67660
+
67661
+/* Opcode: SorterCompare P1 P2 P3
67662
+**
67663
+** P1 is a sorter cursor. This instruction compares the record blob in
67664
+** register P3 with the entry that the sorter cursor currently points to.
67665
+** If, excluding the rowid fields at the end, the two records are a match,
67666
+** fall through to the next instruction. Otherwise, jump to instruction P2.
67667
+*/
67668
+case OP_SorterCompare: {
67669
+#if 0 /* local variables moved into u.bi */
67670
+ VdbeCursor *pC;
67671
+ int res;
67672
+#endif /* local variables moved into u.bi */
67673
+
67674
+ u.bi.pC = p->apCsr[pOp->p1];
67675
+ assert( isSorter(u.bi.pC) );
67676
+ pIn3 = &aMem[pOp->p3];
67677
+ rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
67678
+ if( u.bi.res ){
67679
+ pc = pOp->p2-1;
67680
+ }
67681
+ break;
67682
+};
67683
+
67684
+/* Opcode: SorterData P1 P2 * * *
67685
+**
67686
+** Write into register P2 the current sorter data for sorter cursor P1.
67687
+*/
67688
+case OP_SorterData: {
67689
+#if 0 /* local variables moved into u.bj */
67690
+ VdbeCursor *pC;
67691
+#endif /* local variables moved into u.bj */
67692
+#ifndef SQLITE_OMIT_MERGE_SORT
67693
+ pOut = &aMem[pOp->p2];
67694
+ u.bj.pC = p->apCsr[pOp->p1];
67695
+ assert( u.bj.pC->isSorter );
67696
+ rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
67697
+#else
67698
+ pOp->opcode = OP_RowKey;
67699
+ pc--;
67700
+#endif
67701
+ break;
67702
+}
6703967703
6704067704
/* Opcode: RowData P1 P2 * * *
6704167705
**
6704267706
** Write into register P2 the complete row data for cursor P1.
6704367707
** There is no interpretation of the data.
@@ -67057,65 +67721,67 @@
6705767721
** If the P1 cursor must be pointing to a valid row (not a NULL row)
6705867722
** of a real table, not a pseudo-table.
6705967723
*/
6706067724
case OP_RowKey:
6706167725
case OP_RowData: {
67062
-#if 0 /* local variables moved into u.bh */
67726
+#if 0 /* local variables moved into u.bk */
6706367727
VdbeCursor *pC;
6706467728
BtCursor *pCrsr;
6706567729
u32 n;
6706667730
i64 n64;
67067
-#endif /* local variables moved into u.bh */
67731
+#endif /* local variables moved into u.bk */
6706867732
6706967733
pOut = &aMem[pOp->p2];
6707067734
memAboutToChange(p, pOut);
6707167735
6707267736
/* Note that RowKey and RowData are really exactly the same instruction */
6707367737
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67074
- u.bh.pC = p->apCsr[pOp->p1];
67075
- assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
67076
- assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
67077
- assert( u.bh.pC!=0 );
67078
- assert( u.bh.pC->nullRow==0 );
67079
- assert( u.bh.pC->pseudoTableReg==0 );
67080
- assert( u.bh.pC->pCursor!=0 );
67081
- u.bh.pCrsr = u.bh.pC->pCursor;
67082
- assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
67738
+ u.bk.pC = p->apCsr[pOp->p1];
67739
+ assert( u.bk.pC->isSorter==0 );
67740
+ assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
67741
+ assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
67742
+ assert( u.bk.pC!=0 );
67743
+ assert( u.bk.pC->nullRow==0 );
67744
+ assert( u.bk.pC->pseudoTableReg==0 );
67745
+ assert( !u.bk.pC->isSorter );
67746
+ assert( u.bk.pC->pCursor!=0 );
67747
+ u.bk.pCrsr = u.bk.pC->pCursor;
67748
+ assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
6708367749
6708467750
/* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
6708567751
** OP_Rewind/Op_Next with no intervening instructions that might invalidate
6708667752
** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
6708767753
** a no-op and can never fail. But we leave it in place as a safety.
6708867754
*/
67089
- assert( u.bh.pC->deferredMoveto==0 );
67090
- rc = sqlite3VdbeCursorMoveto(u.bh.pC);
67091
- if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67092
-
67093
- if( u.bh.pC->isIndex ){
67094
- assert( !u.bh.pC->isTable );
67095
- rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
67096
- assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67097
- if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67098
- goto too_big;
67099
- }
67100
- u.bh.n = (u32)u.bh.n64;
67101
- }else{
67102
- rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
67103
- assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67104
- if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67105
- goto too_big;
67106
- }
67107
- }
67108
- if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
67109
- goto no_mem;
67110
- }
67111
- pOut->n = u.bh.n;
67112
- MemSetTypeFlag(pOut, MEM_Blob);
67113
- if( u.bh.pC->isIndex ){
67114
- rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
67115
- }else{
67116
- rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
67755
+ assert( u.bk.pC->deferredMoveto==0 );
67756
+ rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67757
+ if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67758
+
67759
+ if( u.bk.pC->isIndex ){
67760
+ assert( !u.bk.pC->isTable );
67761
+ rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67762
+ assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67763
+ if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67764
+ goto too_big;
67765
+ }
67766
+ u.bk.n = (u32)u.bk.n64;
67767
+ }else{
67768
+ rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67769
+ assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67770
+ if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67771
+ goto too_big;
67772
+ }
67773
+ }
67774
+ if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
67775
+ goto no_mem;
67776
+ }
67777
+ pOut->n = u.bk.n;
67778
+ MemSetTypeFlag(pOut, MEM_Blob);
67779
+ if( u.bk.pC->isIndex ){
67780
+ rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67781
+ }else{
67782
+ rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
6711767783
}
6711867784
pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
6711967785
UPDATE_MAX_BLOBSIZE(pOut);
6712067786
break;
6712167787
}
@@ -67128,46 +67794,46 @@
6712867794
** P1 can be either an ordinary table or a virtual table. There used to
6712967795
** be a separate OP_VRowid opcode for use with virtual tables, but this
6713067796
** one opcode now works for both table types.
6713167797
*/
6713267798
case OP_Rowid: { /* out2-prerelease */
67133
-#if 0 /* local variables moved into u.bi */
67799
+#if 0 /* local variables moved into u.bl */
6713467800
VdbeCursor *pC;
6713567801
i64 v;
6713667802
sqlite3_vtab *pVtab;
6713767803
const sqlite3_module *pModule;
67138
-#endif /* local variables moved into u.bi */
67804
+#endif /* local variables moved into u.bl */
6713967805
6714067806
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67141
- u.bi.pC = p->apCsr[pOp->p1];
67142
- assert( u.bi.pC!=0 );
67143
- assert( u.bi.pC->pseudoTableReg==0 );
67144
- if( u.bi.pC->nullRow ){
67807
+ u.bl.pC = p->apCsr[pOp->p1];
67808
+ assert( u.bl.pC!=0 );
67809
+ assert( u.bl.pC->pseudoTableReg==0 );
67810
+ if( u.bl.pC->nullRow ){
6714567811
pOut->flags = MEM_Null;
6714667812
break;
67147
- }else if( u.bi.pC->deferredMoveto ){
67148
- u.bi.v = u.bi.pC->movetoTarget;
67813
+ }else if( u.bl.pC->deferredMoveto ){
67814
+ u.bl.v = u.bl.pC->movetoTarget;
6714967815
#ifndef SQLITE_OMIT_VIRTUALTABLE
67150
- }else if( u.bi.pC->pVtabCursor ){
67151
- u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
67152
- u.bi.pModule = u.bi.pVtab->pModule;
67153
- assert( u.bi.pModule->xRowid );
67154
- rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
67155
- importVtabErrMsg(p, u.bi.pVtab);
67816
+ }else if( u.bl.pC->pVtabCursor ){
67817
+ u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
67818
+ u.bl.pModule = u.bl.pVtab->pModule;
67819
+ assert( u.bl.pModule->xRowid );
67820
+ rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
67821
+ importVtabErrMsg(p, u.bl.pVtab);
6715667822
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6715767823
}else{
67158
- assert( u.bi.pC->pCursor!=0 );
67159
- rc = sqlite3VdbeCursorMoveto(u.bi.pC);
67824
+ assert( u.bl.pC->pCursor!=0 );
67825
+ rc = sqlite3VdbeCursorMoveto(u.bl.pC);
6716067826
if( rc ) goto abort_due_to_error;
67161
- if( u.bi.pC->rowidIsValid ){
67162
- u.bi.v = u.bi.pC->lastRowid;
67827
+ if( u.bl.pC->rowidIsValid ){
67828
+ u.bl.v = u.bl.pC->lastRowid;
6716367829
}else{
67164
- rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
67830
+ rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
6716567831
assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
6716667832
}
6716767833
}
67168
- pOut->u.i = u.bi.v;
67834
+ pOut->u.i = u.bl.v;
6716967835
break;
6717067836
}
6717167837
6717267838
/* Opcode: NullRow P1 * * * *
6717367839
**
@@ -67174,21 +67840,22 @@
6717467840
** Move the cursor P1 to a null row. Any OP_Column operations
6717567841
** that occur while the cursor is on the null row will always
6717667842
** write a NULL.
6717767843
*/
6717867844
case OP_NullRow: {
67179
-#if 0 /* local variables moved into u.bj */
67845
+#if 0 /* local variables moved into u.bm */
6718067846
VdbeCursor *pC;
67181
-#endif /* local variables moved into u.bj */
67847
+#endif /* local variables moved into u.bm */
6718267848
6718367849
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67184
- u.bj.pC = p->apCsr[pOp->p1];
67185
- assert( u.bj.pC!=0 );
67186
- u.bj.pC->nullRow = 1;
67187
- u.bj.pC->rowidIsValid = 0;
67188
- if( u.bj.pC->pCursor ){
67189
- sqlite3BtreeClearCursor(u.bj.pC->pCursor);
67850
+ u.bm.pC = p->apCsr[pOp->p1];
67851
+ assert( u.bm.pC!=0 );
67852
+ u.bm.pC->nullRow = 1;
67853
+ u.bm.pC->rowidIsValid = 0;
67854
+ assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
67855
+ if( u.bm.pC->pCursor ){
67856
+ sqlite3BtreeClearCursor(u.bm.pC->pCursor);
6719067857
}
6719167858
break;
6719267859
}
6719367860
6719467861
/* Opcode: Last P1 P2 * * *
@@ -67198,30 +67865,30 @@
6719867865
** If the table or index is empty and P2>0, then jump immediately to P2.
6719967866
** If P2 is 0 or if the table or index is not empty, fall through
6720067867
** to the following instruction.
6720167868
*/
6720267869
case OP_Last: { /* jump */
67203
-#if 0 /* local variables moved into u.bk */
67870
+#if 0 /* local variables moved into u.bn */
6720467871
VdbeCursor *pC;
6720567872
BtCursor *pCrsr;
6720667873
int res;
67207
-#endif /* local variables moved into u.bk */
67874
+#endif /* local variables moved into u.bn */
6720867875
6720967876
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67210
- u.bk.pC = p->apCsr[pOp->p1];
67211
- assert( u.bk.pC!=0 );
67212
- u.bk.pCrsr = u.bk.pC->pCursor;
67213
- if( u.bk.pCrsr==0 ){
67214
- u.bk.res = 1;
67877
+ u.bn.pC = p->apCsr[pOp->p1];
67878
+ assert( u.bn.pC!=0 );
67879
+ u.bn.pCrsr = u.bn.pC->pCursor;
67880
+ if( NEVER(u.bn.pCrsr==0) ){
67881
+ u.bn.res = 1;
6721567882
}else{
67216
- rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
67883
+ rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
6721767884
}
67218
- u.bk.pC->nullRow = (u8)u.bk.res;
67219
- u.bk.pC->deferredMoveto = 0;
67220
- u.bk.pC->rowidIsValid = 0;
67221
- u.bk.pC->cacheStatus = CACHE_STALE;
67222
- if( pOp->p2>0 && u.bk.res ){
67885
+ u.bn.pC->nullRow = (u8)u.bn.res;
67886
+ u.bn.pC->deferredMoveto = 0;
67887
+ u.bn.pC->rowidIsValid = 0;
67888
+ u.bn.pC->cacheStatus = CACHE_STALE;
67889
+ if( pOp->p2>0 && u.bn.res ){
6722367890
pc = pOp->p2 - 1;
6722467891
}
6722567892
break;
6722667893
}
6722767894
@@ -67236,10 +67903,14 @@
6723667903
** end. We use the OP_Sort opcode instead of OP_Rewind to do the
6723767904
** rewinding so that the global variable will be incremented and
6723867905
** regression tests can determine whether or not the optimizer is
6723967906
** correctly optimizing out sorts.
6724067907
*/
67908
+case OP_SorterSort: /* jump */
67909
+#ifdef SQLITE_OMIT_MERGE_SORT
67910
+ pOp->opcode = OP_Sort;
67911
+#endif
6724167912
case OP_Sort: { /* jump */
6724267913
#ifdef SQLITE_TEST
6724367914
sqlite3_sort_count++;
6724467915
sqlite3_search_count--;
6724567916
#endif
@@ -67253,43 +67924,51 @@
6725367924
** If the table or index is empty and P2>0, then jump immediately to P2.
6725467925
** If P2 is 0 or if the table or index is not empty, fall through
6725567926
** to the following instruction.
6725667927
*/
6725767928
case OP_Rewind: { /* jump */
67258
-#if 0 /* local variables moved into u.bl */
67929
+#if 0 /* local variables moved into u.bo */
6725967930
VdbeCursor *pC;
6726067931
BtCursor *pCrsr;
6726167932
int res;
67262
-#endif /* local variables moved into u.bl */
67933
+#endif /* local variables moved into u.bo */
6726367934
6726467935
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67265
- u.bl.pC = p->apCsr[pOp->p1];
67266
- assert( u.bl.pC!=0 );
67267
- u.bl.res = 1;
67268
- if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
67269
- rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
67270
- u.bl.pC->atFirst = u.bl.res==0 ?1:0;
67271
- u.bl.pC->deferredMoveto = 0;
67272
- u.bl.pC->cacheStatus = CACHE_STALE;
67273
- u.bl.pC->rowidIsValid = 0;
67274
- }
67275
- u.bl.pC->nullRow = (u8)u.bl.res;
67936
+ u.bo.pC = p->apCsr[pOp->p1];
67937
+ assert( u.bo.pC!=0 );
67938
+ assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
67939
+ u.bo.res = 1;
67940
+ if( isSorter(u.bo.pC) ){
67941
+ rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
67942
+ }else{
67943
+ u.bo.pCrsr = u.bo.pC->pCursor;
67944
+ assert( u.bo.pCrsr );
67945
+ rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
67946
+ u.bo.pC->atFirst = u.bo.res==0 ?1:0;
67947
+ u.bo.pC->deferredMoveto = 0;
67948
+ u.bo.pC->cacheStatus = CACHE_STALE;
67949
+ u.bo.pC->rowidIsValid = 0;
67950
+ }
67951
+ u.bo.pC->nullRow = (u8)u.bo.res;
6727667952
assert( pOp->p2>0 && pOp->p2<p->nOp );
67277
- if( u.bl.res ){
67953
+ if( u.bo.res ){
6727867954
pc = pOp->p2 - 1;
6727967955
}
6728067956
break;
6728167957
}
6728267958
67283
-/* Opcode: Next P1 P2 * * P5
67959
+/* Opcode: Next P1 P2 * P4 P5
6728467960
**
6728567961
** Advance cursor P1 so that it points to the next key/data pair in its
6728667962
** table or index. If there are no more key/value pairs then fall through
6728767963
** to the following instruction. But if the cursor advance was successful,
6728867964
** jump immediately to P2.
6728967965
**
6729067966
** The P1 cursor must be for a real table, not a pseudo-table.
67967
+**
67968
+** P4 is always of type P4_ADVANCE. The function pointer points to
67969
+** sqlite3BtreeNext().
6729167970
**
6729267971
** If P5 is positive and the jump is taken, then event counter
6729367972
** number P5-1 in the prepared statement is incremented.
6729467973
**
6729567974
** See also: Prev
@@ -67300,48 +67979,57 @@
6730067979
** table or index. If there is no previous key/value pairs then fall through
6730167980
** to the following instruction. But if the cursor backup was successful,
6730267981
** jump immediately to P2.
6730367982
**
6730467983
** The P1 cursor must be for a real table, not a pseudo-table.
67984
+**
67985
+** P4 is always of type P4_ADVANCE. The function pointer points to
67986
+** sqlite3BtreePrevious().
6730567987
**
6730667988
** If P5 is positive and the jump is taken, then event counter
6730767989
** number P5-1 in the prepared statement is incremented.
6730867990
*/
67991
+case OP_SorterNext: /* jump */
67992
+#ifdef SQLITE_OMIT_MERGE_SORT
67993
+ pOp->opcode = OP_Next;
67994
+#endif
6730967995
case OP_Prev: /* jump */
6731067996
case OP_Next: { /* jump */
67311
-#if 0 /* local variables moved into u.bm */
67997
+#if 0 /* local variables moved into u.bp */
6731267998
VdbeCursor *pC;
67313
- BtCursor *pCrsr;
6731467999
int res;
67315
-#endif /* local variables moved into u.bm */
68000
+#endif /* local variables moved into u.bp */
6731668001
6731768002
CHECK_FOR_INTERRUPT;
6731868003
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6731968004
assert( pOp->p5<=ArraySize(p->aCounter) );
67320
- u.bm.pC = p->apCsr[pOp->p1];
67321
- if( u.bm.pC==0 ){
68005
+ u.bp.pC = p->apCsr[pOp->p1];
68006
+ if( u.bp.pC==0 ){
6732268007
break; /* See ticket #2273 */
6732368008
}
67324
- u.bm.pCrsr = u.bm.pC->pCursor;
67325
- if( u.bm.pCrsr==0 ){
67326
- u.bm.pC->nullRow = 1;
67327
- break;
67328
- }
67329
- u.bm.res = 1;
67330
- assert( u.bm.pC->deferredMoveto==0 );
67331
- rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
67332
- sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
67333
- u.bm.pC->nullRow = (u8)u.bm.res;
67334
- u.bm.pC->cacheStatus = CACHE_STALE;
67335
- if( u.bm.res==0 ){
68009
+ assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68010
+ if( isSorter(u.bp.pC) ){
68011
+ assert( pOp->opcode==OP_SorterNext );
68012
+ rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
68013
+ }else{
68014
+ u.bp.res = 1;
68015
+ assert( u.bp.pC->deferredMoveto==0 );
68016
+ assert( u.bp.pC->pCursor );
68017
+ assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
68018
+ assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68019
+ rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
68020
+ }
68021
+ u.bp.pC->nullRow = (u8)u.bp.res;
68022
+ u.bp.pC->cacheStatus = CACHE_STALE;
68023
+ if( u.bp.res==0 ){
6733668024
pc = pOp->p2 - 1;
6733768025
if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
6733868026
#ifdef SQLITE_TEST
6733968027
sqlite3_search_count++;
6734068028
#endif
6734168029
}
67342
- u.bm.pC->rowidIsValid = 0;
68030
+ u.bp.pC->rowidIsValid = 0;
6734368031
break;
6734468032
}
6734568033
6734668034
/* Opcode: IdxInsert P1 P2 P3 * P5
6734768035
**
@@ -67353,35 +68041,44 @@
6735368041
** insert is likely to be an append.
6735468042
**
6735568043
** This instruction only works for indices. The equivalent instruction
6735668044
** for tables is OP_Insert.
6735768045
*/
68046
+case OP_SorterInsert: /* in2 */
68047
+#ifdef SQLITE_OMIT_MERGE_SORT
68048
+ pOp->opcode = OP_IdxInsert;
68049
+#endif
6735868050
case OP_IdxInsert: { /* in2 */
67359
-#if 0 /* local variables moved into u.bn */
68051
+#if 0 /* local variables moved into u.bq */
6736068052
VdbeCursor *pC;
6736168053
BtCursor *pCrsr;
6736268054
int nKey;
6736368055
const char *zKey;
67364
-#endif /* local variables moved into u.bn */
68056
+#endif /* local variables moved into u.bq */
6736568057
6736668058
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67367
- u.bn.pC = p->apCsr[pOp->p1];
67368
- assert( u.bn.pC!=0 );
68059
+ u.bq.pC = p->apCsr[pOp->p1];
68060
+ assert( u.bq.pC!=0 );
68061
+ assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
6736968062
pIn2 = &aMem[pOp->p2];
6737068063
assert( pIn2->flags & MEM_Blob );
67371
- u.bn.pCrsr = u.bn.pC->pCursor;
67372
- if( ALWAYS(u.bn.pCrsr!=0) ){
67373
- assert( u.bn.pC->isTable==0 );
68064
+ u.bq.pCrsr = u.bq.pC->pCursor;
68065
+ if( ALWAYS(u.bq.pCrsr!=0) ){
68066
+ assert( u.bq.pC->isTable==0 );
6737468067
rc = ExpandBlob(pIn2);
6737568068
if( rc==SQLITE_OK ){
67376
- u.bn.nKey = pIn2->n;
67377
- u.bn.zKey = pIn2->z;
67378
- rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
67379
- ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
67380
- );
67381
- assert( u.bn.pC->deferredMoveto==0 );
67382
- u.bn.pC->cacheStatus = CACHE_STALE;
68069
+ if( isSorter(u.bq.pC) ){
68070
+ rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
68071
+ }else{
68072
+ u.bq.nKey = pIn2->n;
68073
+ u.bq.zKey = pIn2->z;
68074
+ rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
68075
+ ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
68076
+ );
68077
+ assert( u.bq.pC->deferredMoveto==0 );
68078
+ u.bq.pC->cacheStatus = CACHE_STALE;
68079
+ }
6738368080
}
6738468081
}
6738568082
break;
6738668083
}
6738768084
@@ -67390,37 +68087,37 @@
6739068087
** The content of P3 registers starting at register P2 form
6739168088
** an unpacked index key. This opcode removes that entry from the
6739268089
** index opened by cursor P1.
6739368090
*/
6739468091
case OP_IdxDelete: {
67395
-#if 0 /* local variables moved into u.bo */
68092
+#if 0 /* local variables moved into u.br */
6739668093
VdbeCursor *pC;
6739768094
BtCursor *pCrsr;
6739868095
int res;
6739968096
UnpackedRecord r;
67400
-#endif /* local variables moved into u.bo */
68097
+#endif /* local variables moved into u.br */
6740168098
6740268099
assert( pOp->p3>0 );
6740368100
assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
6740468101
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67405
- u.bo.pC = p->apCsr[pOp->p1];
67406
- assert( u.bo.pC!=0 );
67407
- u.bo.pCrsr = u.bo.pC->pCursor;
67408
- if( ALWAYS(u.bo.pCrsr!=0) ){
67409
- u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
67410
- u.bo.r.nField = (u16)pOp->p3;
67411
- u.bo.r.flags = 0;
67412
- u.bo.r.aMem = &aMem[pOp->p2];
68102
+ u.br.pC = p->apCsr[pOp->p1];
68103
+ assert( u.br.pC!=0 );
68104
+ u.br.pCrsr = u.br.pC->pCursor;
68105
+ if( ALWAYS(u.br.pCrsr!=0) ){
68106
+ u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
68107
+ u.br.r.nField = (u16)pOp->p3;
68108
+ u.br.r.flags = 0;
68109
+ u.br.r.aMem = &aMem[pOp->p2];
6741368110
#ifdef SQLITE_DEBUG
67414
- { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); }
68111
+ { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
6741568112
#endif
67416
- rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
67417
- if( rc==SQLITE_OK && u.bo.res==0 ){
67418
- rc = sqlite3BtreeDelete(u.bo.pCrsr);
68113
+ rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
68114
+ if( rc==SQLITE_OK && u.br.res==0 ){
68115
+ rc = sqlite3BtreeDelete(u.br.pCrsr);
6741968116
}
67420
- assert( u.bo.pC->deferredMoveto==0 );
67421
- u.bo.pC->cacheStatus = CACHE_STALE;
68117
+ assert( u.br.pC->deferredMoveto==0 );
68118
+ u.br.pC->cacheStatus = CACHE_STALE;
6742268119
}
6742368120
break;
6742468121
}
6742568122
6742668123
/* Opcode: IdxRowid P1 P2 * * *
@@ -67430,32 +68127,32 @@
6743068127
** the rowid of the table entry to which this index entry points.
6743168128
**
6743268129
** See also: Rowid, MakeRecord.
6743368130
*/
6743468131
case OP_IdxRowid: { /* out2-prerelease */
67435
-#if 0 /* local variables moved into u.bp */
68132
+#if 0 /* local variables moved into u.bs */
6743668133
BtCursor *pCrsr;
6743768134
VdbeCursor *pC;
6743868135
i64 rowid;
67439
-#endif /* local variables moved into u.bp */
68136
+#endif /* local variables moved into u.bs */
6744068137
6744168138
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67442
- u.bp.pC = p->apCsr[pOp->p1];
67443
- assert( u.bp.pC!=0 );
67444
- u.bp.pCrsr = u.bp.pC->pCursor;
68139
+ u.bs.pC = p->apCsr[pOp->p1];
68140
+ assert( u.bs.pC!=0 );
68141
+ u.bs.pCrsr = u.bs.pC->pCursor;
6744568142
pOut->flags = MEM_Null;
67446
- if( ALWAYS(u.bp.pCrsr!=0) ){
67447
- rc = sqlite3VdbeCursorMoveto(u.bp.pC);
68143
+ if( ALWAYS(u.bs.pCrsr!=0) ){
68144
+ rc = sqlite3VdbeCursorMoveto(u.bs.pC);
6744868145
if( NEVER(rc) ) goto abort_due_to_error;
67449
- assert( u.bp.pC->deferredMoveto==0 );
67450
- assert( u.bp.pC->isTable==0 );
67451
- if( !u.bp.pC->nullRow ){
67452
- rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
68146
+ assert( u.bs.pC->deferredMoveto==0 );
68147
+ assert( u.bs.pC->isTable==0 );
68148
+ if( !u.bs.pC->nullRow ){
68149
+ rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
6745368150
if( rc!=SQLITE_OK ){
6745468151
goto abort_due_to_error;
6745568152
}
67456
- pOut->u.i = u.bp.rowid;
68153
+ pOut->u.i = u.bs.rowid;
6745768154
pOut->flags = MEM_Int;
6745868155
}
6745968156
}
6746068157
break;
6746168158
}
@@ -67486,43 +68183,43 @@
6748668183
** If P5 is non-zero then the key value is increased by an epsilon prior
6748768184
** to the comparison. This makes the opcode work like IdxLE.
6748868185
*/
6748968186
case OP_IdxLT: /* jump */
6749068187
case OP_IdxGE: { /* jump */
67491
-#if 0 /* local variables moved into u.bq */
68188
+#if 0 /* local variables moved into u.bt */
6749268189
VdbeCursor *pC;
6749368190
int res;
6749468191
UnpackedRecord r;
67495
-#endif /* local variables moved into u.bq */
68192
+#endif /* local variables moved into u.bt */
6749668193
6749768194
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67498
- u.bq.pC = p->apCsr[pOp->p1];
67499
- assert( u.bq.pC!=0 );
67500
- assert( u.bq.pC->isOrdered );
67501
- if( ALWAYS(u.bq.pC->pCursor!=0) ){
67502
- assert( u.bq.pC->deferredMoveto==0 );
68195
+ u.bt.pC = p->apCsr[pOp->p1];
68196
+ assert( u.bt.pC!=0 );
68197
+ assert( u.bt.pC->isOrdered );
68198
+ if( ALWAYS(u.bt.pC->pCursor!=0) ){
68199
+ assert( u.bt.pC->deferredMoveto==0 );
6750368200
assert( pOp->p5==0 || pOp->p5==1 );
6750468201
assert( pOp->p4type==P4_INT32 );
67505
- u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
67506
- u.bq.r.nField = (u16)pOp->p4.i;
68202
+ u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68203
+ u.bt.r.nField = (u16)pOp->p4.i;
6750768204
if( pOp->p5 ){
67508
- u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
68205
+ u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
6750968206
}else{
67510
- u.bq.r.flags = UNPACKED_IGNORE_ROWID;
68207
+ u.bt.r.flags = UNPACKED_IGNORE_ROWID;
6751168208
}
67512
- u.bq.r.aMem = &aMem[pOp->p3];
68209
+ u.bt.r.aMem = &aMem[pOp->p3];
6751368210
#ifdef SQLITE_DEBUG
67514
- { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); }
68211
+ { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
6751568212
#endif
67516
- rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
68213
+ rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
6751768214
if( pOp->opcode==OP_IdxLT ){
67518
- u.bq.res = -u.bq.res;
68215
+ u.bt.res = -u.bt.res;
6751968216
}else{
6752068217
assert( pOp->opcode==OP_IdxGE );
67521
- u.bq.res++;
68218
+ u.bt.res++;
6752268219
}
67523
- if( u.bq.res>0 ){
68220
+ if( u.bt.res>0 ){
6752468221
pc = pOp->p2 - 1 ;
6752568222
}
6752668223
}
6752768224
break;
6752868225
}
@@ -67546,43 +68243,43 @@
6754668243
** If AUTOVACUUM is disabled then a zero is stored in register P2.
6754768244
**
6754868245
** See also: Clear
6754968246
*/
6755068247
case OP_Destroy: { /* out2-prerelease */
67551
-#if 0 /* local variables moved into u.br */
68248
+#if 0 /* local variables moved into u.bu */
6755268249
int iMoved;
6755368250
int iCnt;
6755468251
Vdbe *pVdbe;
6755568252
int iDb;
67556
-#endif /* local variables moved into u.br */
68253
+#endif /* local variables moved into u.bu */
6755768254
#ifndef SQLITE_OMIT_VIRTUALTABLE
67558
- u.br.iCnt = 0;
67559
- for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
67560
- if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
67561
- u.br.iCnt++;
68255
+ u.bu.iCnt = 0;
68256
+ for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
68257
+ if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
68258
+ u.bu.iCnt++;
6756268259
}
6756368260
}
6756468261
#else
67565
- u.br.iCnt = db->activeVdbeCnt;
68262
+ u.bu.iCnt = db->activeVdbeCnt;
6756668263
#endif
6756768264
pOut->flags = MEM_Null;
67568
- if( u.br.iCnt>1 ){
68265
+ if( u.bu.iCnt>1 ){
6756968266
rc = SQLITE_LOCKED;
6757068267
p->errorAction = OE_Abort;
6757168268
}else{
67572
- u.br.iDb = pOp->p3;
67573
- assert( u.br.iCnt==1 );
67574
- assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
67575
- rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
68269
+ u.bu.iDb = pOp->p3;
68270
+ assert( u.bu.iCnt==1 );
68271
+ assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
68272
+ rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
6757668273
pOut->flags = MEM_Int;
67577
- pOut->u.i = u.br.iMoved;
68274
+ pOut->u.i = u.bu.iMoved;
6757868275
#ifndef SQLITE_OMIT_AUTOVACUUM
67579
- if( rc==SQLITE_OK && u.br.iMoved!=0 ){
67580
- sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
68276
+ if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
68277
+ sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
6758168278
/* All OP_Destroy operations occur on the same btree */
67582
- assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
67583
- resetSchemaOnFault = u.br.iDb+1;
68279
+ assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
68280
+ resetSchemaOnFault = u.bu.iDb+1;
6758468281
}
6758568282
#endif
6758668283
}
6758768284
break;
6758868285
}
@@ -67604,25 +68301,25 @@
6760468301
** also incremented by the number of rows in the table being cleared.
6760568302
**
6760668303
** See also: Destroy
6760768304
*/
6760868305
case OP_Clear: {
67609
-#if 0 /* local variables moved into u.bs */
68306
+#if 0 /* local variables moved into u.bv */
6761068307
int nChange;
67611
-#endif /* local variables moved into u.bs */
68308
+#endif /* local variables moved into u.bv */
6761268309
67613
- u.bs.nChange = 0;
68310
+ u.bv.nChange = 0;
6761468311
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
6761568312
rc = sqlite3BtreeClearTable(
67616
- db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
68313
+ db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
6761768314
);
6761868315
if( pOp->p3 ){
67619
- p->nChange += u.bs.nChange;
68316
+ p->nChange += u.bv.nChange;
6762068317
if( pOp->p3>0 ){
6762168318
assert( memIsValid(&aMem[pOp->p3]) );
6762268319
memAboutToChange(p, &aMem[pOp->p3]);
67623
- aMem[pOp->p3].u.i += u.bs.nChange;
68320
+ aMem[pOp->p3].u.i += u.bv.nChange;
6762468321
}
6762568322
}
6762668323
break;
6762768324
}
6762868325
@@ -67648,29 +68345,29 @@
6764868345
**
6764968346
** See documentation on OP_CreateTable for additional information.
6765068347
*/
6765168348
case OP_CreateIndex: /* out2-prerelease */
6765268349
case OP_CreateTable: { /* out2-prerelease */
67653
-#if 0 /* local variables moved into u.bt */
68350
+#if 0 /* local variables moved into u.bw */
6765468351
int pgno;
6765568352
int flags;
6765668353
Db *pDb;
67657
-#endif /* local variables moved into u.bt */
68354
+#endif /* local variables moved into u.bw */
6765868355
67659
- u.bt.pgno = 0;
68356
+ u.bw.pgno = 0;
6766068357
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6766168358
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67662
- u.bt.pDb = &db->aDb[pOp->p1];
67663
- assert( u.bt.pDb->pBt!=0 );
68359
+ u.bw.pDb = &db->aDb[pOp->p1];
68360
+ assert( u.bw.pDb->pBt!=0 );
6766468361
if( pOp->opcode==OP_CreateTable ){
67665
- /* u.bt.flags = BTREE_INTKEY; */
67666
- u.bt.flags = BTREE_INTKEY;
68362
+ /* u.bw.flags = BTREE_INTKEY; */
68363
+ u.bw.flags = BTREE_INTKEY;
6766768364
}else{
67668
- u.bt.flags = BTREE_BLOBKEY;
68365
+ u.bw.flags = BTREE_BLOBKEY;
6766968366
}
67670
- rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
67671
- pOut->u.i = u.bt.pgno;
68367
+ rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
68368
+ pOut->u.i = u.bw.pgno;
6767268369
break;
6767368370
}
6767468371
6767568372
/* Opcode: ParseSchema P1 * * P4 *
6767668373
**
@@ -67679,48 +68376,48 @@
6767968376
**
6768068377
** This opcode invokes the parser to create a new virtual machine,
6768168378
** then runs the new virtual machine. It is thus a re-entrant opcode.
6768268379
*/
6768368380
case OP_ParseSchema: {
67684
-#if 0 /* local variables moved into u.bu */
68381
+#if 0 /* local variables moved into u.bx */
6768568382
int iDb;
6768668383
const char *zMaster;
6768768384
char *zSql;
6768868385
InitData initData;
67689
-#endif /* local variables moved into u.bu */
68386
+#endif /* local variables moved into u.bx */
6769068387
6769168388
/* Any prepared statement that invokes this opcode will hold mutexes
6769268389
** on every btree. This is a prerequisite for invoking
6769368390
** sqlite3InitCallback().
6769468391
*/
6769568392
#ifdef SQLITE_DEBUG
67696
- for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
67697
- assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
68393
+ for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
68394
+ assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
6769868395
}
6769968396
#endif
6770068397
67701
- u.bu.iDb = pOp->p1;
67702
- assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
67703
- assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
68398
+ u.bx.iDb = pOp->p1;
68399
+ assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
68400
+ assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
6770468401
/* Used to be a conditional */ {
67705
- u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
67706
- u.bu.initData.db = db;
67707
- u.bu.initData.iDb = pOp->p1;
67708
- u.bu.initData.pzErrMsg = &p->zErrMsg;
67709
- u.bu.zSql = sqlite3MPrintf(db,
68402
+ u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
68403
+ u.bx.initData.db = db;
68404
+ u.bx.initData.iDb = pOp->p1;
68405
+ u.bx.initData.pzErrMsg = &p->zErrMsg;
68406
+ u.bx.zSql = sqlite3MPrintf(db,
6771068407
"SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
67711
- db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
67712
- if( u.bu.zSql==0 ){
68408
+ db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
68409
+ if( u.bx.zSql==0 ){
6771368410
rc = SQLITE_NOMEM;
6771468411
}else{
6771568412
assert( db->init.busy==0 );
6771668413
db->init.busy = 1;
67717
- u.bu.initData.rc = SQLITE_OK;
68414
+ u.bx.initData.rc = SQLITE_OK;
6771868415
assert( !db->mallocFailed );
67719
- rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
67720
- if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
67721
- sqlite3DbFree(db, u.bu.zSql);
68416
+ rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
68417
+ if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
68418
+ sqlite3DbFree(db, u.bx.zSql);
6772268419
db->init.busy = 0;
6772368420
}
6772468421
}
6772568422
if( rc==SQLITE_NOMEM ){
6772668423
goto no_mem;
@@ -67799,45 +68496,45 @@
6779968496
** file, not the main database file.
6780068497
**
6780168498
** This opcode is used to implement the integrity_check pragma.
6780268499
*/
6780368500
case OP_IntegrityCk: {
67804
-#if 0 /* local variables moved into u.bv */
68501
+#if 0 /* local variables moved into u.by */
6780568502
int nRoot; /* Number of tables to check. (Number of root pages.) */
6780668503
int *aRoot; /* Array of rootpage numbers for tables to be checked */
6780768504
int j; /* Loop counter */
6780868505
int nErr; /* Number of errors reported */
6780968506
char *z; /* Text of the error report */
6781068507
Mem *pnErr; /* Register keeping track of errors remaining */
67811
-#endif /* local variables moved into u.bv */
68508
+#endif /* local variables moved into u.by */
6781268509
67813
- u.bv.nRoot = pOp->p2;
67814
- assert( u.bv.nRoot>0 );
67815
- u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
67816
- if( u.bv.aRoot==0 ) goto no_mem;
68510
+ u.by.nRoot = pOp->p2;
68511
+ assert( u.by.nRoot>0 );
68512
+ u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
68513
+ if( u.by.aRoot==0 ) goto no_mem;
6781768514
assert( pOp->p3>0 && pOp->p3<=p->nMem );
67818
- u.bv.pnErr = &aMem[pOp->p3];
67819
- assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
67820
- assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68515
+ u.by.pnErr = &aMem[pOp->p3];
68516
+ assert( (u.by.pnErr->flags & MEM_Int)!=0 );
68517
+ assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
6782168518
pIn1 = &aMem[pOp->p1];
67822
- for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
67823
- u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
68519
+ for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
68520
+ u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
6782468521
}
67825
- u.bv.aRoot[u.bv.j] = 0;
68522
+ u.by.aRoot[u.by.j] = 0;
6782668523
assert( pOp->p5<db->nDb );
6782768524
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
67828
- u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
67829
- (int)u.bv.pnErr->u.i, &u.bv.nErr);
67830
- sqlite3DbFree(db, u.bv.aRoot);
67831
- u.bv.pnErr->u.i -= u.bv.nErr;
68525
+ u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
68526
+ (int)u.by.pnErr->u.i, &u.by.nErr);
68527
+ sqlite3DbFree(db, u.by.aRoot);
68528
+ u.by.pnErr->u.i -= u.by.nErr;
6783268529
sqlite3VdbeMemSetNull(pIn1);
67833
- if( u.bv.nErr==0 ){
67834
- assert( u.bv.z==0 );
67835
- }else if( u.bv.z==0 ){
68530
+ if( u.by.nErr==0 ){
68531
+ assert( u.by.z==0 );
68532
+ }else if( u.by.z==0 ){
6783668533
goto no_mem;
6783768534
}else{
67838
- sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
68535
+ sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
6783968536
}
6784068537
UPDATE_MAX_BLOBSIZE(pIn1);
6784168538
sqlite3VdbeChangeEncoding(pIn1, encoding);
6784268539
break;
6784368540
}
@@ -67867,24 +68564,24 @@
6786768564
** Extract the smallest value from boolean index P1 and put that value into
6786868565
** register P3. Or, if boolean index P1 is initially empty, leave P3
6786968566
** unchanged and jump to instruction P2.
6787068567
*/
6787168568
case OP_RowSetRead: { /* jump, in1, out3 */
67872
-#if 0 /* local variables moved into u.bw */
68569
+#if 0 /* local variables moved into u.bz */
6787368570
i64 val;
67874
-#endif /* local variables moved into u.bw */
68571
+#endif /* local variables moved into u.bz */
6787568572
CHECK_FOR_INTERRUPT;
6787668573
pIn1 = &aMem[pOp->p1];
6787768574
if( (pIn1->flags & MEM_RowSet)==0
67878
- || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0
68575
+ || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
6787968576
){
6788068577
/* The boolean index is empty */
6788168578
sqlite3VdbeMemSetNull(pIn1);
6788268579
pc = pOp->p2 - 1;
6788368580
}else{
6788468581
/* A value was pulled from the index */
67885
- sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val);
68582
+ sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
6788668583
}
6788768584
break;
6788868585
}
6788968586
6789068587
/* Opcode: RowSetTest P1 P2 P3 P4
@@ -67909,18 +68606,18 @@
6790968606
** inserted, there is no need to search to see if the same value was
6791068607
** previously inserted as part of set X (only if it was previously
6791168608
** inserted as part of some other set).
6791268609
*/
6791368610
case OP_RowSetTest: { /* jump, in1, in3 */
67914
-#if 0 /* local variables moved into u.bx */
68611
+#if 0 /* local variables moved into u.ca */
6791568612
int iSet;
6791668613
int exists;
67917
-#endif /* local variables moved into u.bx */
68614
+#endif /* local variables moved into u.ca */
6791868615
6791968616
pIn1 = &aMem[pOp->p1];
6792068617
pIn3 = &aMem[pOp->p3];
67921
- u.bx.iSet = pOp->p4.i;
68618
+ u.ca.iSet = pOp->p4.i;
6792268619
assert( pIn3->flags&MEM_Int );
6792368620
6792468621
/* If there is anything other than a rowset object in memory cell P1,
6792568622
** delete it now and initialize P1 with an empty rowset
6792668623
*/
@@ -67928,21 +68625,21 @@
6792868625
sqlite3VdbeMemSetRowSet(pIn1);
6792968626
if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
6793068627
}
6793168628
6793268629
assert( pOp->p4type==P4_INT32 );
67933
- assert( u.bx.iSet==-1 || u.bx.iSet>=0 );
67934
- if( u.bx.iSet ){
67935
- u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
67936
- (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff),
68630
+ assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
68631
+ if( u.ca.iSet ){
68632
+ u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68633
+ (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
6793768634
pIn3->u.i);
67938
- if( u.bx.exists ){
68635
+ if( u.ca.exists ){
6793968636
pc = pOp->p2 - 1;
6794068637
break;
6794168638
}
6794268639
}
67943
- if( u.bx.iSet>=0 ){
68640
+ if( u.ca.iSet>=0 ){
6794468641
sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
6794568642
}
6794668643
break;
6794768644
}
6794868645
@@ -67961,25 +68658,25 @@
6796168658
** memory required by the sub-vdbe at runtime.
6796268659
**
6796368660
** P4 is a pointer to the VM containing the trigger program.
6796468661
*/
6796568662
case OP_Program: { /* jump */
67966
-#if 0 /* local variables moved into u.by */
68663
+#if 0 /* local variables moved into u.cb */
6796768664
int nMem; /* Number of memory registers for sub-program */
6796868665
int nByte; /* Bytes of runtime space required for sub-program */
6796968666
Mem *pRt; /* Register to allocate runtime space */
6797068667
Mem *pMem; /* Used to iterate through memory cells */
6797168668
Mem *pEnd; /* Last memory cell in new array */
6797268669
VdbeFrame *pFrame; /* New vdbe frame to execute in */
6797368670
SubProgram *pProgram; /* Sub-program to execute */
6797468671
void *t; /* Token identifying trigger */
67975
-#endif /* local variables moved into u.by */
68672
+#endif /* local variables moved into u.cb */
6797668673
67977
- u.by.pProgram = pOp->p4.pProgram;
67978
- u.by.pRt = &aMem[pOp->p3];
67979
- assert( memIsValid(u.by.pRt) );
67980
- assert( u.by.pProgram->nOp>0 );
68674
+ u.cb.pProgram = pOp->p4.pProgram;
68675
+ u.cb.pRt = &aMem[pOp->p3];
68676
+ assert( memIsValid(u.cb.pRt) );
68677
+ assert( u.cb.pProgram->nOp>0 );
6798168678
6798268679
/* If the p5 flag is clear, then recursive invocation of triggers is
6798368680
** disabled for backwards compatibility (p5 is set if this sub-program
6798468681
** is really a trigger, not a foreign key action, and the flag set
6798568682
** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -67989,79 +68686,79 @@
6798968686
** SubProgram (if the trigger may be executed with more than one different
6799068687
** ON CONFLICT algorithm). SubProgram structures associated with a
6799168688
** single trigger all have the same value for the SubProgram.token
6799268689
** variable. */
6799368690
if( pOp->p5 ){
67994
- u.by.t = u.by.pProgram->token;
67995
- for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent);
67996
- if( u.by.pFrame ) break;
68691
+ u.cb.t = u.cb.pProgram->token;
68692
+ for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
68693
+ if( u.cb.pFrame ) break;
6799768694
}
6799868695
6799968696
if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
6800068697
rc = SQLITE_ERROR;
6800168698
sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
6800268699
break;
6800368700
}
6800468701
68005
- /* Register u.by.pRt is used to store the memory required to save the state
68702
+ /* Register u.cb.pRt is used to store the memory required to save the state
6800668703
** of the current program, and the memory required at runtime to execute
68007
- ** the trigger program. If this trigger has been fired before, then u.by.pRt
68704
+ ** the trigger program. If this trigger has been fired before, then u.cb.pRt
6800868705
** is already allocated. Otherwise, it must be initialized. */
68009
- if( (u.by.pRt->flags&MEM_Frame)==0 ){
68706
+ if( (u.cb.pRt->flags&MEM_Frame)==0 ){
6801068707
/* SubProgram.nMem is set to the number of memory cells used by the
6801168708
** program stored in SubProgram.aOp. As well as these, one memory
6801268709
** cell is required for each cursor used by the program. Set local
68013
- ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value.
68710
+ ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
6801468711
*/
68015
- u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr;
68016
- u.by.nByte = ROUND8(sizeof(VdbeFrame))
68017
- + u.by.nMem * sizeof(Mem)
68018
- + u.by.pProgram->nCsr * sizeof(VdbeCursor *);
68019
- u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte);
68020
- if( !u.by.pFrame ){
68712
+ u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
68713
+ u.cb.nByte = ROUND8(sizeof(VdbeFrame))
68714
+ + u.cb.nMem * sizeof(Mem)
68715
+ + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
68716
+ u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
68717
+ if( !u.cb.pFrame ){
6802168718
goto no_mem;
6802268719
}
68023
- sqlite3VdbeMemRelease(u.by.pRt);
68024
- u.by.pRt->flags = MEM_Frame;
68025
- u.by.pRt->u.pFrame = u.by.pFrame;
68026
-
68027
- u.by.pFrame->v = p;
68028
- u.by.pFrame->nChildMem = u.by.nMem;
68029
- u.by.pFrame->nChildCsr = u.by.pProgram->nCsr;
68030
- u.by.pFrame->pc = pc;
68031
- u.by.pFrame->aMem = p->aMem;
68032
- u.by.pFrame->nMem = p->nMem;
68033
- u.by.pFrame->apCsr = p->apCsr;
68034
- u.by.pFrame->nCursor = p->nCursor;
68035
- u.by.pFrame->aOp = p->aOp;
68036
- u.by.pFrame->nOp = p->nOp;
68037
- u.by.pFrame->token = u.by.pProgram->token;
68038
-
68039
- u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem];
68040
- for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){
68041
- u.by.pMem->flags = MEM_Null;
68042
- u.by.pMem->db = db;
68720
+ sqlite3VdbeMemRelease(u.cb.pRt);
68721
+ u.cb.pRt->flags = MEM_Frame;
68722
+ u.cb.pRt->u.pFrame = u.cb.pFrame;
68723
+
68724
+ u.cb.pFrame->v = p;
68725
+ u.cb.pFrame->nChildMem = u.cb.nMem;
68726
+ u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
68727
+ u.cb.pFrame->pc = pc;
68728
+ u.cb.pFrame->aMem = p->aMem;
68729
+ u.cb.pFrame->nMem = p->nMem;
68730
+ u.cb.pFrame->apCsr = p->apCsr;
68731
+ u.cb.pFrame->nCursor = p->nCursor;
68732
+ u.cb.pFrame->aOp = p->aOp;
68733
+ u.cb.pFrame->nOp = p->nOp;
68734
+ u.cb.pFrame->token = u.cb.pProgram->token;
68735
+
68736
+ u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
68737
+ for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
68738
+ u.cb.pMem->flags = MEM_Null;
68739
+ u.cb.pMem->db = db;
6804368740
}
6804468741
}else{
68045
- u.by.pFrame = u.by.pRt->u.pFrame;
68046
- assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem );
68047
- assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr );
68048
- assert( pc==u.by.pFrame->pc );
68742
+ u.cb.pFrame = u.cb.pRt->u.pFrame;
68743
+ assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
68744
+ assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
68745
+ assert( pc==u.cb.pFrame->pc );
6804968746
}
6805068747
6805168748
p->nFrame++;
68052
- u.by.pFrame->pParent = p->pFrame;
68053
- u.by.pFrame->lastRowid = lastRowid;
68054
- u.by.pFrame->nChange = p->nChange;
68749
+ u.cb.pFrame->pParent = p->pFrame;
68750
+ u.cb.pFrame->lastRowid = lastRowid;
68751
+ u.cb.pFrame->nChange = p->nChange;
6805568752
p->nChange = 0;
68056
- p->pFrame = u.by.pFrame;
68057
- p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1];
68058
- p->nMem = u.by.pFrame->nChildMem;
68059
- p->nCursor = (u16)u.by.pFrame->nChildCsr;
68753
+ p->pFrame = u.cb.pFrame;
68754
+ p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
68755
+ p->nMem = u.cb.pFrame->nChildMem;
68756
+ p->nCursor = (u16)u.cb.pFrame->nChildCsr;
6806068757
p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68061
- p->aOp = aOp = u.by.pProgram->aOp;
68062
- p->nOp = u.by.pProgram->nOp;
68758
+ p->aOp = aOp = u.cb.pProgram->aOp;
68759
+ p->nOp = u.cb.pProgram->nOp;
6806368760
pc = -1;
6806468761
6806568762
break;
6806668763
}
6806768764
@@ -68076,17 +68773,17 @@
6807668773
** The address of the cell in the parent frame is determined by adding
6807768774
** the value of the P1 argument to the value of the P1 argument to the
6807868775
** calling OP_Program instruction.
6807968776
*/
6808068777
case OP_Param: { /* out2-prerelease */
68081
-#if 0 /* local variables moved into u.bz */
68778
+#if 0 /* local variables moved into u.cc */
6808268779
VdbeFrame *pFrame;
6808368780
Mem *pIn;
68084
-#endif /* local variables moved into u.bz */
68085
- u.bz.pFrame = p->pFrame;
68086
- u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1];
68087
- sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem);
68781
+#endif /* local variables moved into u.cc */
68782
+ u.cc.pFrame = p->pFrame;
68783
+ u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
68784
+ sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
6808868785
break;
6808968786
}
6809068787
6809168788
#endif /* #ifndef SQLITE_OMIT_TRIGGER */
6809268789
@@ -68138,26 +68835,26 @@
6813868835
**
6813968836
** This instruction throws an error if the memory cell is not initially
6814068837
** an integer.
6814168838
*/
6814268839
case OP_MemMax: { /* in2 */
68143
-#if 0 /* local variables moved into u.ca */
68840
+#if 0 /* local variables moved into u.cd */
6814468841
Mem *pIn1;
6814568842
VdbeFrame *pFrame;
68146
-#endif /* local variables moved into u.ca */
68843
+#endif /* local variables moved into u.cd */
6814768844
if( p->pFrame ){
68148
- for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent);
68149
- u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1];
68845
+ for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
68846
+ u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
6815068847
}else{
68151
- u.ca.pIn1 = &aMem[pOp->p1];
68848
+ u.cd.pIn1 = &aMem[pOp->p1];
6815268849
}
68153
- assert( memIsValid(u.ca.pIn1) );
68154
- sqlite3VdbeMemIntegerify(u.ca.pIn1);
68850
+ assert( memIsValid(u.cd.pIn1) );
68851
+ sqlite3VdbeMemIntegerify(u.cd.pIn1);
6815568852
pIn2 = &aMem[pOp->p2];
6815668853
sqlite3VdbeMemIntegerify(pIn2);
68157
- if( u.ca.pIn1->u.i<pIn2->u.i){
68158
- u.ca.pIn1->u.i = pIn2->u.i;
68854
+ if( u.cd.pIn1->u.i<pIn2->u.i){
68855
+ u.cd.pIn1->u.i = pIn2->u.i;
6815968856
}
6816068857
break;
6816168858
}
6816268859
#endif /* SQLITE_OMIT_AUTOINCREMENT */
6816368860
@@ -68220,54 +68917,54 @@
6822068917
**
6822168918
** The P5 arguments are taken from register P2 and its
6822268919
** successors.
6822368920
*/
6822468921
case OP_AggStep: {
68225
-#if 0 /* local variables moved into u.cb */
68922
+#if 0 /* local variables moved into u.ce */
6822668923
int n;
6822768924
int i;
6822868925
Mem *pMem;
6822968926
Mem *pRec;
6823068927
sqlite3_context ctx;
6823168928
sqlite3_value **apVal;
68232
-#endif /* local variables moved into u.cb */
68233
-
68234
- u.cb.n = pOp->p5;
68235
- assert( u.cb.n>=0 );
68236
- u.cb.pRec = &aMem[pOp->p2];
68237
- u.cb.apVal = p->apArg;
68238
- assert( u.cb.apVal || u.cb.n==0 );
68239
- for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){
68240
- assert( memIsValid(u.cb.pRec) );
68241
- u.cb.apVal[u.cb.i] = u.cb.pRec;
68242
- memAboutToChange(p, u.cb.pRec);
68243
- sqlite3VdbeMemStoreType(u.cb.pRec);
68244
- }
68245
- u.cb.ctx.pFunc = pOp->p4.pFunc;
68929
+#endif /* local variables moved into u.ce */
68930
+
68931
+ u.ce.n = pOp->p5;
68932
+ assert( u.ce.n>=0 );
68933
+ u.ce.pRec = &aMem[pOp->p2];
68934
+ u.ce.apVal = p->apArg;
68935
+ assert( u.ce.apVal || u.ce.n==0 );
68936
+ for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
68937
+ assert( memIsValid(u.ce.pRec) );
68938
+ u.ce.apVal[u.ce.i] = u.ce.pRec;
68939
+ memAboutToChange(p, u.ce.pRec);
68940
+ sqlite3VdbeMemStoreType(u.ce.pRec);
68941
+ }
68942
+ u.ce.ctx.pFunc = pOp->p4.pFunc;
6824668943
assert( pOp->p3>0 && pOp->p3<=p->nMem );
68247
- u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3];
68248
- u.cb.pMem->n++;
68249
- u.cb.ctx.s.flags = MEM_Null;
68250
- u.cb.ctx.s.z = 0;
68251
- u.cb.ctx.s.zMalloc = 0;
68252
- u.cb.ctx.s.xDel = 0;
68253
- u.cb.ctx.s.db = db;
68254
- u.cb.ctx.isError = 0;
68255
- u.cb.ctx.pColl = 0;
68256
- if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68944
+ u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
68945
+ u.ce.pMem->n++;
68946
+ u.ce.ctx.s.flags = MEM_Null;
68947
+ u.ce.ctx.s.z = 0;
68948
+ u.ce.ctx.s.zMalloc = 0;
68949
+ u.ce.ctx.s.xDel = 0;
68950
+ u.ce.ctx.s.db = db;
68951
+ u.ce.ctx.isError = 0;
68952
+ u.ce.ctx.pColl = 0;
68953
+ if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6825768954
assert( pOp>p->aOp );
6825868955
assert( pOp[-1].p4type==P4_COLLSEQ );
6825968956
assert( pOp[-1].opcode==OP_CollSeq );
68260
- u.cb.ctx.pColl = pOp[-1].p4.pColl;
68957
+ u.ce.ctx.pColl = pOp[-1].p4.pColl;
6826168958
}
68262
- (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
68263
- if( u.cb.ctx.isError ){
68264
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
68265
- rc = u.cb.ctx.isError;
68959
+ (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
68960
+ if( u.ce.ctx.isError ){
68961
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
68962
+ rc = u.ce.ctx.isError;
6826668963
}
6826768964
68268
- sqlite3VdbeMemRelease(&u.cb.ctx.s);
68965
+ sqlite3VdbeMemRelease(&u.ce.ctx.s);
6826968966
6827068967
break;
6827168968
}
6827268969
6827368970
/* Opcode: AggFinal P1 P2 * P4 *
@@ -68281,23 +68978,23 @@
6828168978
** functions that can take varying numbers of arguments. The
6828268979
** P4 argument is only needed for the degenerate case where
6828368980
** the step function was not previously called.
6828468981
*/
6828568982
case OP_AggFinal: {
68286
-#if 0 /* local variables moved into u.cc */
68983
+#if 0 /* local variables moved into u.cf */
6828768984
Mem *pMem;
68288
-#endif /* local variables moved into u.cc */
68985
+#endif /* local variables moved into u.cf */
6828968986
assert( pOp->p1>0 && pOp->p1<=p->nMem );
68290
- u.cc.pMem = &aMem[pOp->p1];
68291
- assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68292
- rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
68987
+ u.cf.pMem = &aMem[pOp->p1];
68988
+ assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68989
+ rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
6829368990
if( rc ){
68294
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
68991
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
6829568992
}
68296
- sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
68297
- UPDATE_MAX_BLOBSIZE(u.cc.pMem);
68298
- if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
68993
+ sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
68994
+ UPDATE_MAX_BLOBSIZE(u.cf.pMem);
68995
+ if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
6829968996
goto too_big;
6830068997
}
6830168998
break;
6830268999
}
6830369000
@@ -68312,29 +69009,29 @@
6831269009
** in the WAL that have been checkpointed after the checkpoint
6831369010
** completes into mem[P3+2]. However on an error, mem[P3+1] and
6831469011
** mem[P3+2] are initialized to -1.
6831569012
*/
6831669013
case OP_Checkpoint: {
68317
-#if 0 /* local variables moved into u.cd */
69014
+#if 0 /* local variables moved into u.cg */
6831869015
int i; /* Loop counter */
6831969016
int aRes[3]; /* Results */
6832069017
Mem *pMem; /* Write results here */
68321
-#endif /* local variables moved into u.cd */
69018
+#endif /* local variables moved into u.cg */
6832269019
68323
- u.cd.aRes[0] = 0;
68324
- u.cd.aRes[1] = u.cd.aRes[2] = -1;
69020
+ u.cg.aRes[0] = 0;
69021
+ u.cg.aRes[1] = u.cg.aRes[2] = -1;
6832569022
assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
6832669023
|| pOp->p2==SQLITE_CHECKPOINT_FULL
6832769024
|| pOp->p2==SQLITE_CHECKPOINT_RESTART
6832869025
);
68329
- rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
69026
+ rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
6833069027
if( rc==SQLITE_BUSY ){
6833169028
rc = SQLITE_OK;
68332
- u.cd.aRes[0] = 1;
69029
+ u.cg.aRes[0] = 1;
6833369030
}
68334
- for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
68335
- sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
69031
+ for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
69032
+ sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
6833669033
}
6833769034
break;
6833869035
};
6833969036
#endif
6834069037
@@ -68349,95 +69046,95 @@
6834969046
** If changing into or out of WAL mode the procedure is more complicated.
6835069047
**
6835169048
** Write a string containing the final journal-mode to register P2.
6835269049
*/
6835369050
case OP_JournalMode: { /* out2-prerelease */
68354
-#if 0 /* local variables moved into u.ce */
69051
+#if 0 /* local variables moved into u.ch */
6835569052
Btree *pBt; /* Btree to change journal mode of */
6835669053
Pager *pPager; /* Pager associated with pBt */
6835769054
int eNew; /* New journal mode */
6835869055
int eOld; /* The old journal mode */
6835969056
const char *zFilename; /* Name of database file for pPager */
68360
-#endif /* local variables moved into u.ce */
68361
-
68362
- u.ce.eNew = pOp->p3;
68363
- assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
68364
- || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
68365
- || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
68366
- || u.ce.eNew==PAGER_JOURNALMODE_OFF
68367
- || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
68368
- || u.ce.eNew==PAGER_JOURNALMODE_WAL
68369
- || u.ce.eNew==PAGER_JOURNALMODE_QUERY
69057
+#endif /* local variables moved into u.ch */
69058
+
69059
+ u.ch.eNew = pOp->p3;
69060
+ assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
69061
+ || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
69062
+ || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
69063
+ || u.ch.eNew==PAGER_JOURNALMODE_OFF
69064
+ || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
69065
+ || u.ch.eNew==PAGER_JOURNALMODE_WAL
69066
+ || u.ch.eNew==PAGER_JOURNALMODE_QUERY
6837069067
);
6837169068
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6837269069
68373
- u.ce.pBt = db->aDb[pOp->p1].pBt;
68374
- u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
68375
- u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
68376
- if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
68377
- if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
69070
+ u.ch.pBt = db->aDb[pOp->p1].pBt;
69071
+ u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
69072
+ u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
69073
+ if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
69074
+ if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
6837869075
6837969076
#ifndef SQLITE_OMIT_WAL
68380
- u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
69077
+ u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
6838169078
6838269079
/* Do not allow a transition to journal_mode=WAL for a database
6838369080
** in temporary storage or if the VFS does not support shared memory
6838469081
*/
68385
- if( u.ce.eNew==PAGER_JOURNALMODE_WAL
68386
- && (u.ce.zFilename[0]==0 /* Temp file */
68387
- || !sqlite3PagerWalSupported(u.ce.pPager)) /* No shared-memory support */
69082
+ if( u.ch.eNew==PAGER_JOURNALMODE_WAL
69083
+ && (u.ch.zFilename[0]==0 /* Temp file */
69084
+ || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */
6838869085
){
68389
- u.ce.eNew = u.ce.eOld;
69086
+ u.ch.eNew = u.ch.eOld;
6839069087
}
6839169088
68392
- if( (u.ce.eNew!=u.ce.eOld)
68393
- && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
69089
+ if( (u.ch.eNew!=u.ch.eOld)
69090
+ && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
6839469091
){
6839569092
if( !db->autoCommit || db->activeVdbeCnt>1 ){
6839669093
rc = SQLITE_ERROR;
6839769094
sqlite3SetString(&p->zErrMsg, db,
6839869095
"cannot change %s wal mode from within a transaction",
68399
- (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69096
+ (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
6840069097
);
6840169098
break;
6840269099
}else{
6840369100
68404
- if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
69101
+ if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
6840569102
/* If leaving WAL mode, close the log file. If successful, the call
6840669103
** to PagerCloseWal() checkpoints and deletes the write-ahead-log
6840769104
** file. An EXCLUSIVE lock may still be held on the database file
6840869105
** after a successful return.
6840969106
*/
68410
- rc = sqlite3PagerCloseWal(u.ce.pPager);
69107
+ rc = sqlite3PagerCloseWal(u.ch.pPager);
6841169108
if( rc==SQLITE_OK ){
68412
- sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
69109
+ sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
6841369110
}
68414
- }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
69111
+ }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
6841569112
/* Cannot transition directly from MEMORY to WAL. Use mode OFF
6841669113
** as an intermediate */
68417
- sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
69114
+ sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
6841869115
}
6841969116
6842069117
/* Open a transaction on the database file. Regardless of the journal
6842169118
** mode, this transaction always uses a rollback journal.
6842269119
*/
68423
- assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
69120
+ assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
6842469121
if( rc==SQLITE_OK ){
68425
- rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69122
+ rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
6842669123
}
6842769124
}
6842869125
}
6842969126
#endif /* ifndef SQLITE_OMIT_WAL */
6843069127
6843169128
if( rc ){
68432
- u.ce.eNew = u.ce.eOld;
69129
+ u.ch.eNew = u.ch.eOld;
6843369130
}
68434
- u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
69131
+ u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
6843569132
6843669133
pOut = &aMem[pOp->p2];
6843769134
pOut->flags = MEM_Str|MEM_Static|MEM_Term;
68438
- pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
69135
+ pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
6843969136
pOut->n = sqlite3Strlen30(pOut->z);
6844069137
pOut->enc = SQLITE_UTF8;
6844169138
sqlite3VdbeChangeEncoding(pOut, encoding);
6844269139
break;
6844369140
};
@@ -68462,18 +69159,18 @@
6846269159
** Perform a single step of the incremental vacuum procedure on
6846369160
** the P1 database. If the vacuum has finished, jump to instruction
6846469161
** P2. Otherwise, fall through to the next instruction.
6846569162
*/
6846669163
case OP_IncrVacuum: { /* jump */
68467
-#if 0 /* local variables moved into u.cf */
69164
+#if 0 /* local variables moved into u.ci */
6846869165
Btree *pBt;
68469
-#endif /* local variables moved into u.cf */
69166
+#endif /* local variables moved into u.ci */
6847069167
6847169168
assert( pOp->p1>=0 && pOp->p1<db->nDb );
6847269169
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68473
- u.cf.pBt = db->aDb[pOp->p1].pBt;
68474
- rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
69170
+ u.ci.pBt = db->aDb[pOp->p1].pBt;
69171
+ rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
6847569172
if( rc==SQLITE_DONE ){
6847669173
pc = pOp->p2 - 1;
6847769174
rc = SQLITE_OK;
6847869175
}
6847969176
break;
@@ -68539,16 +69236,16 @@
6853969236
** Also, whether or not P4 is set, check that this is not being called from
6854069237
** within a callback to a virtual table xSync() method. If it is, the error
6854169238
** code will be set to SQLITE_LOCKED.
6854269239
*/
6854369240
case OP_VBegin: {
68544
-#if 0 /* local variables moved into u.cg */
69241
+#if 0 /* local variables moved into u.cj */
6854569242
VTable *pVTab;
68546
-#endif /* local variables moved into u.cg */
68547
- u.cg.pVTab = pOp->p4.pVtab;
68548
- rc = sqlite3VtabBegin(db, u.cg.pVTab);
68549
- if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
69243
+#endif /* local variables moved into u.cj */
69244
+ u.cj.pVTab = pOp->p4.pVtab;
69245
+ rc = sqlite3VtabBegin(db, u.cj.pVTab);
69246
+ if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
6855069247
break;
6855169248
}
6855269249
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6855369250
6855469251
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -68583,36 +69280,36 @@
6858369280
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6858469281
** P1 is a cursor number. This opcode opens a cursor to the virtual
6858569282
** table and stores that cursor in P1.
6858669283
*/
6858769284
case OP_VOpen: {
68588
-#if 0 /* local variables moved into u.ch */
69285
+#if 0 /* local variables moved into u.ck */
6858969286
VdbeCursor *pCur;
6859069287
sqlite3_vtab_cursor *pVtabCursor;
6859169288
sqlite3_vtab *pVtab;
6859269289
sqlite3_module *pModule;
68593
-#endif /* local variables moved into u.ch */
68594
-
68595
- u.ch.pCur = 0;
68596
- u.ch.pVtabCursor = 0;
68597
- u.ch.pVtab = pOp->p4.pVtab->pVtab;
68598
- u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
68599
- assert(u.ch.pVtab && u.ch.pModule);
68600
- rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
68601
- importVtabErrMsg(p, u.ch.pVtab);
69290
+#endif /* local variables moved into u.ck */
69291
+
69292
+ u.ck.pCur = 0;
69293
+ u.ck.pVtabCursor = 0;
69294
+ u.ck.pVtab = pOp->p4.pVtab->pVtab;
69295
+ u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
69296
+ assert(u.ck.pVtab && u.ck.pModule);
69297
+ rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
69298
+ importVtabErrMsg(p, u.ck.pVtab);
6860269299
if( SQLITE_OK==rc ){
6860369300
/* Initialize sqlite3_vtab_cursor base class */
68604
- u.ch.pVtabCursor->pVtab = u.ch.pVtab;
69301
+ u.ck.pVtabCursor->pVtab = u.ck.pVtab;
6860569302
6860669303
/* Initialise vdbe cursor object */
68607
- u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
68608
- if( u.ch.pCur ){
68609
- u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
68610
- u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
69304
+ u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69305
+ if( u.ck.pCur ){
69306
+ u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
69307
+ u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
6861169308
}else{
6861269309
db->mallocFailed = 1;
68613
- u.ch.pModule->xClose(u.ch.pVtabCursor);
69310
+ u.ck.pModule->xClose(u.ck.pVtabCursor);
6861469311
}
6861569312
}
6861669313
break;
6861769314
}
6861869315
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68635,11 +69332,11 @@
6863569332
** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
6863669333
**
6863769334
** A jump is made to P2 if the result set after filtering would be empty.
6863869335
*/
6863969336
case OP_VFilter: { /* jump */
68640
-#if 0 /* local variables moved into u.ci */
69337
+#if 0 /* local variables moved into u.cl */
6864169338
int nArg;
6864269339
int iQuery;
6864369340
const sqlite3_module *pModule;
6864469341
Mem *pQuery;
6864569342
Mem *pArgc;
@@ -68647,49 +69344,49 @@
6864769344
sqlite3_vtab *pVtab;
6864869345
VdbeCursor *pCur;
6864969346
int res;
6865069347
int i;
6865169348
Mem **apArg;
68652
-#endif /* local variables moved into u.ci */
68653
-
68654
- u.ci.pQuery = &aMem[pOp->p3];
68655
- u.ci.pArgc = &u.ci.pQuery[1];
68656
- u.ci.pCur = p->apCsr[pOp->p1];
68657
- assert( memIsValid(u.ci.pQuery) );
68658
- REGISTER_TRACE(pOp->p3, u.ci.pQuery);
68659
- assert( u.ci.pCur->pVtabCursor );
68660
- u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
68661
- u.ci.pVtab = u.ci.pVtabCursor->pVtab;
68662
- u.ci.pModule = u.ci.pVtab->pModule;
69349
+#endif /* local variables moved into u.cl */
69350
+
69351
+ u.cl.pQuery = &aMem[pOp->p3];
69352
+ u.cl.pArgc = &u.cl.pQuery[1];
69353
+ u.cl.pCur = p->apCsr[pOp->p1];
69354
+ assert( memIsValid(u.cl.pQuery) );
69355
+ REGISTER_TRACE(pOp->p3, u.cl.pQuery);
69356
+ assert( u.cl.pCur->pVtabCursor );
69357
+ u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
69358
+ u.cl.pVtab = u.cl.pVtabCursor->pVtab;
69359
+ u.cl.pModule = u.cl.pVtab->pModule;
6866369360
6866469361
/* Grab the index number and argc parameters */
68665
- assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
68666
- u.ci.nArg = (int)u.ci.pArgc->u.i;
68667
- u.ci.iQuery = (int)u.ci.pQuery->u.i;
69362
+ assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
69363
+ u.cl.nArg = (int)u.cl.pArgc->u.i;
69364
+ u.cl.iQuery = (int)u.cl.pQuery->u.i;
6866869365
6866969366
/* Invoke the xFilter method */
6867069367
{
68671
- u.ci.res = 0;
68672
- u.ci.apArg = p->apArg;
68673
- for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
68674
- u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
68675
- sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
69368
+ u.cl.res = 0;
69369
+ u.cl.apArg = p->apArg;
69370
+ for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
69371
+ u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
69372
+ sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
6867669373
}
6867769374
6867869375
p->inVtabMethod = 1;
68679
- rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
69376
+ rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
6868069377
p->inVtabMethod = 0;
68681
- importVtabErrMsg(p, u.ci.pVtab);
69378
+ importVtabErrMsg(p, u.cl.pVtab);
6868269379
if( rc==SQLITE_OK ){
68683
- u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
69380
+ u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
6868469381
}
6868569382
68686
- if( u.ci.res ){
69383
+ if( u.cl.res ){
6868769384
pc = pOp->p2 - 1;
6868869385
}
6868969386
}
68690
- u.ci.pCur->nullRow = 0;
69387
+ u.cl.pCur->nullRow = 0;
6869169388
6869269389
break;
6869369390
}
6869469391
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6869569392
@@ -68699,55 +69396,55 @@
6869969396
** Store the value of the P2-th column of
6870069397
** the row of the virtual-table that the
6870169398
** P1 cursor is pointing to into register P3.
6870269399
*/
6870369400
case OP_VColumn: {
68704
-#if 0 /* local variables moved into u.cj */
69401
+#if 0 /* local variables moved into u.cm */
6870569402
sqlite3_vtab *pVtab;
6870669403
const sqlite3_module *pModule;
6870769404
Mem *pDest;
6870869405
sqlite3_context sContext;
68709
-#endif /* local variables moved into u.cj */
69406
+#endif /* local variables moved into u.cm */
6871069407
6871169408
VdbeCursor *pCur = p->apCsr[pOp->p1];
6871269409
assert( pCur->pVtabCursor );
6871369410
assert( pOp->p3>0 && pOp->p3<=p->nMem );
68714
- u.cj.pDest = &aMem[pOp->p3];
68715
- memAboutToChange(p, u.cj.pDest);
69411
+ u.cm.pDest = &aMem[pOp->p3];
69412
+ memAboutToChange(p, u.cm.pDest);
6871669413
if( pCur->nullRow ){
68717
- sqlite3VdbeMemSetNull(u.cj.pDest);
69414
+ sqlite3VdbeMemSetNull(u.cm.pDest);
6871869415
break;
6871969416
}
68720
- u.cj.pVtab = pCur->pVtabCursor->pVtab;
68721
- u.cj.pModule = u.cj.pVtab->pModule;
68722
- assert( u.cj.pModule->xColumn );
68723
- memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
69417
+ u.cm.pVtab = pCur->pVtabCursor->pVtab;
69418
+ u.cm.pModule = u.cm.pVtab->pModule;
69419
+ assert( u.cm.pModule->xColumn );
69420
+ memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
6872469421
6872569422
/* The output cell may already have a buffer allocated. Move
68726
- ** the current contents to u.cj.sContext.s so in case the user-function
69423
+ ** the current contents to u.cm.sContext.s so in case the user-function
6872769424
** can use the already allocated buffer instead of allocating a
6872869425
** new one.
6872969426
*/
68730
- sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
68731
- MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
69427
+ sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
69428
+ MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
6873269429
68733
- rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
68734
- importVtabErrMsg(p, u.cj.pVtab);
68735
- if( u.cj.sContext.isError ){
68736
- rc = u.cj.sContext.isError;
69430
+ rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
69431
+ importVtabErrMsg(p, u.cm.pVtab);
69432
+ if( u.cm.sContext.isError ){
69433
+ rc = u.cm.sContext.isError;
6873769434
}
6873869435
6873969436
/* Copy the result of the function to the P3 register. We
6874069437
** do this regardless of whether or not an error occurred to ensure any
68741
- ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
69438
+ ** dynamic allocation in u.cm.sContext.s (a Mem struct) is released.
6874269439
*/
68743
- sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
68744
- sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
68745
- REGISTER_TRACE(pOp->p3, u.cj.pDest);
68746
- UPDATE_MAX_BLOBSIZE(u.cj.pDest);
69440
+ sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
69441
+ sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
69442
+ REGISTER_TRACE(pOp->p3, u.cm.pDest);
69443
+ UPDATE_MAX_BLOBSIZE(u.cm.pDest);
6874769444
68748
- if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
69445
+ if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
6874969446
goto too_big;
6875069447
}
6875169448
break;
6875269449
}
6875369450
#endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68758,42 +69455,42 @@
6875869455
** Advance virtual table P1 to the next row in its result set and
6875969456
** jump to instruction P2. Or, if the virtual table has reached
6876069457
** the end of its result set, then fall through to the next instruction.
6876169458
*/
6876269459
case OP_VNext: { /* jump */
68763
-#if 0 /* local variables moved into u.ck */
69460
+#if 0 /* local variables moved into u.cn */
6876469461
sqlite3_vtab *pVtab;
6876569462
const sqlite3_module *pModule;
6876669463
int res;
6876769464
VdbeCursor *pCur;
68768
-#endif /* local variables moved into u.ck */
69465
+#endif /* local variables moved into u.cn */
6876969466
68770
- u.ck.res = 0;
68771
- u.ck.pCur = p->apCsr[pOp->p1];
68772
- assert( u.ck.pCur->pVtabCursor );
68773
- if( u.ck.pCur->nullRow ){
69467
+ u.cn.res = 0;
69468
+ u.cn.pCur = p->apCsr[pOp->p1];
69469
+ assert( u.cn.pCur->pVtabCursor );
69470
+ if( u.cn.pCur->nullRow ){
6877469471
break;
6877569472
}
68776
- u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
68777
- u.ck.pModule = u.ck.pVtab->pModule;
68778
- assert( u.ck.pModule->xNext );
69473
+ u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
69474
+ u.cn.pModule = u.cn.pVtab->pModule;
69475
+ assert( u.cn.pModule->xNext );
6877969476
6878069477
/* Invoke the xNext() method of the module. There is no way for the
6878169478
** underlying implementation to return an error if one occurs during
6878269479
** xNext(). Instead, if an error occurs, true is returned (indicating that
6878369480
** data is available) and the error code returned when xColumn or
6878469481
** some other method is next invoked on the save virtual table cursor.
6878569482
*/
6878669483
p->inVtabMethod = 1;
68787
- rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
69484
+ rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
6878869485
p->inVtabMethod = 0;
68789
- importVtabErrMsg(p, u.ck.pVtab);
69486
+ importVtabErrMsg(p, u.cn.pVtab);
6879069487
if( rc==SQLITE_OK ){
68791
- u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
69488
+ u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
6879269489
}
6879369490
68794
- if( !u.ck.res ){
69491
+ if( !u.cn.res ){
6879569492
/* If there is data, jump to P2 */
6879669493
pc = pOp->p2 - 1;
6879769494
}
6879869495
break;
6879969496
}
@@ -68805,23 +69502,23 @@
6880569502
** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
6880669503
** This opcode invokes the corresponding xRename method. The value
6880769504
** in register P1 is passed as the zName argument to the xRename method.
6880869505
*/
6880969506
case OP_VRename: {
68810
-#if 0 /* local variables moved into u.cl */
69507
+#if 0 /* local variables moved into u.co */
6881169508
sqlite3_vtab *pVtab;
6881269509
Mem *pName;
68813
-#endif /* local variables moved into u.cl */
68814
-
68815
- u.cl.pVtab = pOp->p4.pVtab->pVtab;
68816
- u.cl.pName = &aMem[pOp->p1];
68817
- assert( u.cl.pVtab->pModule->xRename );
68818
- assert( memIsValid(u.cl.pName) );
68819
- REGISTER_TRACE(pOp->p1, u.cl.pName);
68820
- assert( u.cl.pName->flags & MEM_Str );
68821
- rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
68822
- importVtabErrMsg(p, u.cl.pVtab);
69510
+#endif /* local variables moved into u.co */
69511
+
69512
+ u.co.pVtab = pOp->p4.pVtab->pVtab;
69513
+ u.co.pName = &aMem[pOp->p1];
69514
+ assert( u.co.pVtab->pModule->xRename );
69515
+ assert( memIsValid(u.co.pName) );
69516
+ REGISTER_TRACE(pOp->p1, u.co.pName);
69517
+ assert( u.co.pName->flags & MEM_Str );
69518
+ rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
69519
+ importVtabErrMsg(p, u.co.pVtab);
6882369520
p->expired = 0;
6882469521
6882569522
break;
6882669523
}
6882769524
#endif
@@ -68849,45 +69546,45 @@
6884969546
** P1 is a boolean flag. If it is set to true and the xUpdate call
6885069547
** is successful, then the value returned by sqlite3_last_insert_rowid()
6885169548
** is set to the value of the rowid for the row just inserted.
6885269549
*/
6885369550
case OP_VUpdate: {
68854
-#if 0 /* local variables moved into u.cm */
69551
+#if 0 /* local variables moved into u.cp */
6885569552
sqlite3_vtab *pVtab;
6885669553
sqlite3_module *pModule;
6885769554
int nArg;
6885869555
int i;
6885969556
sqlite_int64 rowid;
6886069557
Mem **apArg;
6886169558
Mem *pX;
68862
-#endif /* local variables moved into u.cm */
69559
+#endif /* local variables moved into u.cp */
6886369560
6886469561
assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
6886569562
|| pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
6886669563
);
68867
- u.cm.pVtab = pOp->p4.pVtab->pVtab;
68868
- u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
68869
- u.cm.nArg = pOp->p2;
69564
+ u.cp.pVtab = pOp->p4.pVtab->pVtab;
69565
+ u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
69566
+ u.cp.nArg = pOp->p2;
6887069567
assert( pOp->p4type==P4_VTAB );
68871
- if( ALWAYS(u.cm.pModule->xUpdate) ){
69568
+ if( ALWAYS(u.cp.pModule->xUpdate) ){
6887269569
u8 vtabOnConflict = db->vtabOnConflict;
68873
- u.cm.apArg = p->apArg;
68874
- u.cm.pX = &aMem[pOp->p3];
68875
- for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
68876
- assert( memIsValid(u.cm.pX) );
68877
- memAboutToChange(p, u.cm.pX);
68878
- sqlite3VdbeMemStoreType(u.cm.pX);
68879
- u.cm.apArg[u.cm.i] = u.cm.pX;
68880
- u.cm.pX++;
69570
+ u.cp.apArg = p->apArg;
69571
+ u.cp.pX = &aMem[pOp->p3];
69572
+ for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
69573
+ assert( memIsValid(u.cp.pX) );
69574
+ memAboutToChange(p, u.cp.pX);
69575
+ sqlite3VdbeMemStoreType(u.cp.pX);
69576
+ u.cp.apArg[u.cp.i] = u.cp.pX;
69577
+ u.cp.pX++;
6888169578
}
6888269579
db->vtabOnConflict = pOp->p5;
68883
- rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
69580
+ rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
6888469581
db->vtabOnConflict = vtabOnConflict;
68885
- importVtabErrMsg(p, u.cm.pVtab);
69582
+ importVtabErrMsg(p, u.cp.pVtab);
6888669583
if( rc==SQLITE_OK && pOp->p1 ){
68887
- assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
68888
- db->lastRowid = lastRowid = u.cm.rowid;
69584
+ assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
69585
+ db->lastRowid = lastRowid = u.cp.rowid;
6888969586
}
6889069587
if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
6889169588
if( pOp->p5==OE_Ignore ){
6889269589
rc = SQLITE_OK;
6889369590
}else{
@@ -68943,25 +69640,25 @@
6894369640
**
6894469641
** If tracing is enabled (by the sqlite3_trace()) interface, then
6894569642
** the UTF-8 string contained in P4 is emitted on the trace callback.
6894669643
*/
6894769644
case OP_Trace: {
68948
-#if 0 /* local variables moved into u.cn */
69645
+#if 0 /* local variables moved into u.cq */
6894969646
char *zTrace;
6895069647
char *z;
68951
-#endif /* local variables moved into u.cn */
69648
+#endif /* local variables moved into u.cq */
6895269649
68953
- if( db->xTrace && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
68954
- u.cn.z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
68955
- db->xTrace(db->pTraceArg, u.cn.z);
68956
- sqlite3DbFree(db, u.cn.z);
69650
+ if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
69651
+ u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
69652
+ db->xTrace(db->pTraceArg, u.cq.z);
69653
+ sqlite3DbFree(db, u.cq.z);
6895769654
}
6895869655
#ifdef SQLITE_DEBUG
6895969656
if( (db->flags & SQLITE_SqlTrace)!=0
68960
- && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69657
+ && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
6896169658
){
68962
- sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
69659
+ sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
6896369660
}
6896469661
#endif /* SQLITE_DEBUG */
6896569662
break;
6896669663
}
6896769664
#endif
@@ -69556,10 +70253,930 @@
6955670253
}
6955770254
6955870255
#endif /* #ifndef SQLITE_OMIT_INCRBLOB */
6955970256
6956070257
/************** End of vdbeblob.c ********************************************/
70258
+/************** Begin file vdbesort.c ****************************************/
70259
+/*
70260
+** 2011 July 9
70261
+**
70262
+** The author disclaims copyright to this source code. In place of
70263
+** a legal notice, here is a blessing:
70264
+**
70265
+** May you do good and not evil.
70266
+** May you find forgiveness for yourself and forgive others.
70267
+** May you share freely, never taking more than you give.
70268
+**
70269
+*************************************************************************
70270
+** This file contains code for the VdbeSorter object, used in concert with
70271
+** a VdbeCursor to sort large numbers of keys (as may be required, for
70272
+** example, by CREATE INDEX statements on tables too large to fit in main
70273
+** memory).
70274
+*/
70275
+
70276
+
70277
+#ifndef SQLITE_OMIT_MERGE_SORT
70278
+
70279
+typedef struct VdbeSorterIter VdbeSorterIter;
70280
+typedef struct SorterRecord SorterRecord;
70281
+
70282
+/*
70283
+** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
70284
+**
70285
+** As keys are added to the sorter, they are written to disk in a series
70286
+** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
70287
+** the same as the cache-size allowed for temporary databases. In order
70288
+** to allow the caller to extract keys from the sorter in sorted order,
70289
+** all PMAs currently stored on disk must be merged together. This comment
70290
+** describes the data structure used to do so. The structure supports
70291
+** merging any number of arrays in a single pass with no redundant comparison
70292
+** operations.
70293
+**
70294
+** The aIter[] array contains an iterator for each of the PMAs being merged.
70295
+** An aIter[] iterator either points to a valid key or else is at EOF. For
70296
+** the purposes of the paragraphs below, we assume that the array is actually
70297
+** N elements in size, where N is the smallest power of 2 greater to or equal
70298
+** to the number of iterators being merged. The extra aIter[] elements are
70299
+** treated as if they are empty (always at EOF).
70300
+**
70301
+** The aTree[] array is also N elements in size. The value of N is stored in
70302
+** the VdbeSorter.nTree variable.
70303
+**
70304
+** The final (N/2) elements of aTree[] contain the results of comparing
70305
+** pairs of iterator keys together. Element i contains the result of
70306
+** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
70307
+** aTree element is set to the index of it.
70308
+**
70309
+** For the purposes of this comparison, EOF is considered greater than any
70310
+** other key value. If the keys are equal (only possible with two EOF
70311
+** values), it doesn't matter which index is stored.
70312
+**
70313
+** The (N/4) elements of aTree[] that preceed the final (N/2) described
70314
+** above contains the index of the smallest of each block of 4 iterators.
70315
+** And so on. So that aTree[1] contains the index of the iterator that
70316
+** currently points to the smallest key value. aTree[0] is unused.
70317
+**
70318
+** Example:
70319
+**
70320
+** aIter[0] -> Banana
70321
+** aIter[1] -> Feijoa
70322
+** aIter[2] -> Elderberry
70323
+** aIter[3] -> Currant
70324
+** aIter[4] -> Grapefruit
70325
+** aIter[5] -> Apple
70326
+** aIter[6] -> Durian
70327
+** aIter[7] -> EOF
70328
+**
70329
+** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
70330
+**
70331
+** The current element is "Apple" (the value of the key indicated by
70332
+** iterator 5). When the Next() operation is invoked, iterator 5 will
70333
+** be advanced to the next key in its segment. Say the next key is
70334
+** "Eggplant":
70335
+**
70336
+** aIter[5] -> Eggplant
70337
+**
70338
+** The contents of aTree[] are updated first by comparing the new iterator
70339
+** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
70340
+** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
70341
+** The value of iterator 6 - "Durian" - is now smaller than that of iterator
70342
+** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
70343
+** so the value written into element 1 of the array is 0. As follows:
70344
+**
70345
+** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
70346
+**
70347
+** In other words, each time we advance to the next sorter element, log2(N)
70348
+** key comparison operations are required, where N is the number of segments
70349
+** being merged (rounded up to the next power of 2).
70350
+*/
70351
+struct VdbeSorter {
70352
+ int nInMemory; /* Current size of pRecord list as PMA */
70353
+ int nTree; /* Used size of aTree/aIter (power of 2) */
70354
+ VdbeSorterIter *aIter; /* Array of iterators to merge */
70355
+ int *aTree; /* Current state of incremental merge */
70356
+ i64 iWriteOff; /* Current write offset within file pTemp1 */
70357
+ i64 iReadOff; /* Current read offset within file pTemp1 */
70358
+ sqlite3_file *pTemp1; /* PMA file 1 */
70359
+ int nPMA; /* Number of PMAs stored in pTemp1 */
70360
+ SorterRecord *pRecord; /* Head of in-memory record list */
70361
+ int mnPmaSize; /* Minimum PMA size, in bytes */
70362
+ int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
70363
+ char *aSpace; /* Space for UnpackRecord() */
70364
+ int nSpace; /* Size of aSpace in bytes */
70365
+};
70366
+
70367
+/*
70368
+** The following type is an iterator for a PMA. It caches the current key in
70369
+** variables nKey/aKey. If the iterator is at EOF, pFile==0.
70370
+*/
70371
+struct VdbeSorterIter {
70372
+ i64 iReadOff; /* Current read offset */
70373
+ i64 iEof; /* 1 byte past EOF for this iterator */
70374
+ sqlite3_file *pFile; /* File iterator is reading from */
70375
+ int nAlloc; /* Bytes of space at aAlloc */
70376
+ u8 *aAlloc; /* Allocated space */
70377
+ int nKey; /* Number of bytes in key */
70378
+ u8 *aKey; /* Pointer to current key */
70379
+};
70380
+
70381
+/*
70382
+** A structure to store a single record. All in-memory records are connected
70383
+** together into a linked list headed at VdbeSorter.pRecord using the
70384
+** SorterRecord.pNext pointer.
70385
+*/
70386
+struct SorterRecord {
70387
+ void *pVal;
70388
+ int nVal;
70389
+ SorterRecord *pNext;
70390
+};
70391
+
70392
+/* Minimum allowable value for the VdbeSorter.nWorking variable */
70393
+#define SORTER_MIN_WORKING 10
70394
+
70395
+/* Maximum number of segments to merge in a single pass. */
70396
+#define SORTER_MAX_MERGE_COUNT 16
70397
+
70398
+/*
70399
+** Free all memory belonging to the VdbeSorterIter object passed as the second
70400
+** argument. All structure fields are set to zero before returning.
70401
+*/
70402
+static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
70403
+ sqlite3DbFree(db, pIter->aAlloc);
70404
+ memset(pIter, 0, sizeof(VdbeSorterIter));
70405
+}
70406
+
70407
+/*
70408
+** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
70409
+** no error occurs, or an SQLite error code if one does.
70410
+*/
70411
+static int vdbeSorterIterNext(
70412
+ sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */
70413
+ VdbeSorterIter *pIter /* Iterator to advance */
70414
+){
70415
+ int rc; /* Return Code */
70416
+ int nRead; /* Number of bytes read */
70417
+ int nRec = 0; /* Size of record in bytes */
70418
+ int iOff = 0; /* Size of serialized size varint in bytes */
70419
+
70420
+ nRead = pIter->iEof - pIter->iReadOff;
70421
+ if( nRead>5 ) nRead = 5;
70422
+ if( nRead<=0 ){
70423
+ /* This is an EOF condition */
70424
+ vdbeSorterIterZero(db, pIter);
70425
+ return SQLITE_OK;
70426
+ }
70427
+
70428
+ rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
70429
+ if( rc==SQLITE_OK ){
70430
+ iOff = getVarint32(pIter->aAlloc, nRec);
70431
+ if( (iOff+nRec)>nRead ){
70432
+ int nRead2; /* Number of extra bytes to read */
70433
+ if( (iOff+nRec)>pIter->nAlloc ){
70434
+ int nNew = pIter->nAlloc*2;
70435
+ while( (iOff+nRec)>nNew ) nNew = nNew*2;
70436
+ pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
70437
+ if( !pIter->aAlloc ) return SQLITE_NOMEM;
70438
+ pIter->nAlloc = nNew;
70439
+ }
70440
+
70441
+ nRead2 = iOff + nRec - nRead;
70442
+ rc = sqlite3OsRead(
70443
+ pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
70444
+ );
70445
+ }
70446
+ }
70447
+
70448
+ assert( rc!=SQLITE_OK || nRec>0 );
70449
+ pIter->iReadOff += iOff+nRec;
70450
+ pIter->nKey = nRec;
70451
+ pIter->aKey = &pIter->aAlloc[iOff];
70452
+ return rc;
70453
+}
70454
+
70455
+/*
70456
+** Write a single varint, value iVal, to file-descriptor pFile. Return
70457
+** SQLITE_OK if successful, or an SQLite error code if some error occurs.
70458
+**
70459
+** The value of *piOffset when this function is called is used as the byte
70460
+** offset in file pFile to write to. Before returning, *piOffset is
70461
+** incremented by the number of bytes written.
70462
+*/
70463
+static int vdbeSorterWriteVarint(
70464
+ sqlite3_file *pFile, /* File to write to */
70465
+ i64 iVal, /* Value to write as a varint */
70466
+ i64 *piOffset /* IN/OUT: Write offset in file pFile */
70467
+){
70468
+ u8 aVarint[9]; /* Buffer large enough for a varint */
70469
+ int nVarint; /* Number of used bytes in varint */
70470
+ int rc; /* Result of write() call */
70471
+
70472
+ nVarint = sqlite3PutVarint(aVarint, iVal);
70473
+ rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
70474
+ *piOffset += nVarint;
70475
+
70476
+ return rc;
70477
+}
70478
+
70479
+/*
70480
+** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
70481
+** successful, or an SQLite error code if some error occurs.
70482
+**
70483
+** The value of *piOffset when this function is called is used as the
70484
+** byte offset in file pFile from whence to read the varint. If successful
70485
+** (i.e. if no IO error occurs), then *piOffset is set to the offset of
70486
+** the first byte past the end of the varint before returning. *piVal is
70487
+** set to the integer value read. If an error occurs, the final values of
70488
+** both *piOffset and *piVal are undefined.
70489
+*/
70490
+static int vdbeSorterReadVarint(
70491
+ sqlite3_file *pFile, /* File to read from */
70492
+ i64 *piOffset, /* IN/OUT: Read offset in pFile */
70493
+ i64 *piVal /* OUT: Value read from file */
70494
+){
70495
+ u8 aVarint[9]; /* Buffer large enough for a varint */
70496
+ i64 iOff = *piOffset; /* Offset in file to read from */
70497
+ int rc; /* Return code */
70498
+
70499
+ rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
70500
+ if( rc==SQLITE_OK ){
70501
+ *piOffset += getVarint(aVarint, (u64 *)piVal);
70502
+ }
70503
+
70504
+ return rc;
70505
+}
70506
+
70507
+/*
70508
+** Initialize iterator pIter to scan through the PMA stored in file pFile
70509
+** starting at offset iStart and ending at offset iEof-1. This function
70510
+** leaves the iterator pointing to the first key in the PMA (or EOF if the
70511
+** PMA is empty).
70512
+*/
70513
+static int vdbeSorterIterInit(
70514
+ sqlite3 *db, /* Database handle */
70515
+ VdbeSorter *pSorter, /* Sorter object */
70516
+ i64 iStart, /* Start offset in pFile */
70517
+ VdbeSorterIter *pIter, /* Iterator to populate */
70518
+ i64 *pnByte /* IN/OUT: Increment this value by PMA size */
70519
+){
70520
+ int rc;
70521
+
70522
+ assert( pSorter->iWriteOff>iStart );
70523
+ assert( pIter->aAlloc==0 );
70524
+ pIter->pFile = pSorter->pTemp1;
70525
+ pIter->iReadOff = iStart;
70526
+ pIter->nAlloc = 128;
70527
+ pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
70528
+ if( !pIter->aAlloc ){
70529
+ rc = SQLITE_NOMEM;
70530
+ }else{
70531
+ i64 nByte; /* Total size of PMA in bytes */
70532
+ rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
70533
+ *pnByte += nByte;
70534
+ pIter->iEof = pIter->iReadOff + nByte;
70535
+ }
70536
+ if( rc==SQLITE_OK ){
70537
+ rc = vdbeSorterIterNext(db, pIter);
70538
+ }
70539
+ return rc;
70540
+}
70541
+
70542
+
70543
+/*
70544
+** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
70545
+** size nKey2 bytes). Argument pKeyInfo supplies the collation functions
70546
+** used by the comparison. If an error occurs, return an SQLite error code.
70547
+** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
70548
+** value, depending on whether key1 is smaller, equal to or larger than key2.
70549
+**
70550
+** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
70551
+** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
70552
+** is true and key1 contains even a single NULL value, it is considered to
70553
+** be less than key2. Even if key2 also contains NULL values.
70554
+**
70555
+** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
70556
+** has been allocated and contains an unpacked record that is used as key2.
70557
+*/
70558
+static int vdbeSorterCompare(
70559
+ VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
70560
+ int bOmitRowid, /* Ignore rowid field at end of keys */
70561
+ void *pKey1, int nKey1, /* Left side of comparison */
70562
+ void *pKey2, int nKey2, /* Right side of comparison */
70563
+ int *pRes /* OUT: Result of comparison */
70564
+){
70565
+ KeyInfo *pKeyInfo = pCsr->pKeyInfo;
70566
+ VdbeSorter *pSorter = pCsr->pSorter;
70567
+ char *aSpace = pSorter->aSpace;
70568
+ int nSpace = pSorter->nSpace;
70569
+ UnpackedRecord *r2;
70570
+ int i;
70571
+
70572
+ if( aSpace==0 ){
70573
+ nSpace = ROUND8(sizeof(UnpackedRecord))+(pKeyInfo->nField+1)*sizeof(Mem);
70574
+ aSpace = (char *)sqlite3Malloc(nSpace);
70575
+ if( aSpace==0 ) return SQLITE_NOMEM;
70576
+ pSorter->aSpace = aSpace;
70577
+ pSorter->nSpace = nSpace;
70578
+ }
70579
+
70580
+ if( pKey2 ){
70581
+ /* This call cannot fail. As the memory is already allocated. */
70582
+ r2 = sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, aSpace, nSpace);
70583
+ assert( r2 && (r2->flags & UNPACKED_NEED_FREE)==0 );
70584
+ assert( r2==(UnpackedRecord*)aSpace );
70585
+ }else{
70586
+ r2 = (UnpackedRecord *)aSpace;
70587
+ assert( !bOmitRowid );
70588
+ }
70589
+
70590
+ if( bOmitRowid ){
70591
+ for(i=0; i<r2->nField-1; i++){
70592
+ if( r2->aMem[i].flags & MEM_Null ){
70593
+ *pRes = -1;
70594
+ return SQLITE_OK;
70595
+ }
70596
+ }
70597
+ r2->flags |= UNPACKED_PREFIX_MATCH;
70598
+ r2->nField--;
70599
+ assert( r2->nField>0 );
70600
+ }
70601
+
70602
+ *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
70603
+ return SQLITE_OK;
70604
+}
70605
+
70606
+/*
70607
+** This function is called to compare two iterator keys when merging
70608
+** multiple b-tree segments. Parameter iOut is the index of the aTree[]
70609
+** value to recalculate.
70610
+*/
70611
+static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
70612
+ VdbeSorter *pSorter = pCsr->pSorter;
70613
+ int i1;
70614
+ int i2;
70615
+ int iRes;
70616
+ VdbeSorterIter *p1;
70617
+ VdbeSorterIter *p2;
70618
+
70619
+ assert( iOut<pSorter->nTree && iOut>0 );
70620
+
70621
+ if( iOut>=(pSorter->nTree/2) ){
70622
+ i1 = (iOut - pSorter->nTree/2) * 2;
70623
+ i2 = i1 + 1;
70624
+ }else{
70625
+ i1 = pSorter->aTree[iOut*2];
70626
+ i2 = pSorter->aTree[iOut*2+1];
70627
+ }
70628
+
70629
+ p1 = &pSorter->aIter[i1];
70630
+ p2 = &pSorter->aIter[i2];
70631
+
70632
+ if( p1->pFile==0 ){
70633
+ iRes = i2;
70634
+ }else if( p2->pFile==0 ){
70635
+ iRes = i1;
70636
+ }else{
70637
+ int res;
70638
+ int rc;
70639
+ assert( pCsr->pSorter->aSpace!=0 ); /* allocated in vdbeSorterMerge() */
70640
+ rc = vdbeSorterCompare(
70641
+ pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
70642
+ );
70643
+ /* The vdbeSorterCompare() call cannot fail since pCsr->pSorter->aSpace
70644
+ ** has already been allocated. */
70645
+ assert( rc==SQLITE_OK );
70646
+
70647
+ if( res<=0 ){
70648
+ iRes = i1;
70649
+ }else{
70650
+ iRes = i2;
70651
+ }
70652
+ }
70653
+
70654
+ pSorter->aTree[iOut] = iRes;
70655
+ return SQLITE_OK;
70656
+}
70657
+
70658
+/*
70659
+** Initialize the temporary index cursor just opened as a sorter cursor.
70660
+*/
70661
+SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
70662
+ int pgsz; /* Page size of main database */
70663
+ int mxCache; /* Cache size */
70664
+ VdbeSorter *pSorter; /* The new sorter */
70665
+
70666
+ assert( pCsr->pKeyInfo && pCsr->pBt==0 );
70667
+ pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
70668
+ if( pSorter==0 ){
70669
+ return SQLITE_NOMEM;
70670
+ }
70671
+
70672
+ if( !sqlite3TempInMemory(db) ){
70673
+ pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
70674
+ pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
70675
+ mxCache = db->aDb[0].pSchema->cache_size;
70676
+ if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
70677
+ pSorter->mxPmaSize = mxCache * pgsz;
70678
+ }
70679
+
70680
+ return SQLITE_OK;
70681
+}
70682
+
70683
+/*
70684
+** Free the list of sorted records starting at pRecord.
70685
+*/
70686
+static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
70687
+ SorterRecord *p;
70688
+ SorterRecord *pNext;
70689
+ for(p=pRecord; p; p=pNext){
70690
+ pNext = p->pNext;
70691
+ sqlite3DbFree(db, p);
70692
+ }
70693
+}
70694
+
70695
+/*
70696
+** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
70697
+*/
70698
+SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
70699
+ VdbeSorter *pSorter = pCsr->pSorter;
70700
+ if( pSorter ){
70701
+ if( pSorter->aIter ){
70702
+ int i;
70703
+ for(i=0; i<pSorter->nTree; i++){
70704
+ vdbeSorterIterZero(db, &pSorter->aIter[i]);
70705
+ }
70706
+ sqlite3DbFree(db, pSorter->aIter);
70707
+ }
70708
+ if( pSorter->pTemp1 ){
70709
+ sqlite3OsCloseFree(pSorter->pTemp1);
70710
+ }
70711
+ vdbeSorterRecordFree(db, pSorter->pRecord);
70712
+ sqlite3_free(pSorter->aSpace);
70713
+ sqlite3DbFree(db, pSorter);
70714
+ pCsr->pSorter = 0;
70715
+ }
70716
+}
70717
+
70718
+/*
70719
+** Allocate space for a file-handle and open a temporary file. If successful,
70720
+** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
70721
+** Otherwise, set *ppFile to 0 and return an SQLite error code.
70722
+*/
70723
+static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
70724
+ int dummy;
70725
+ return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
70726
+ SQLITE_OPEN_TEMP_JOURNAL |
70727
+ SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
70728
+ SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy
70729
+ );
70730
+}
70731
+
70732
+/*
70733
+** Attemp to merge the two sorted lists p1 and p2 into a single list. If no
70734
+** error occurs set *ppOut to the head of the new list and return SQLITE_OK.
70735
+*/
70736
+static int vdbeSorterMerge(
70737
+ sqlite3 *db, /* Database handle */
70738
+ VdbeCursor *pCsr, /* For pKeyInfo */
70739
+ SorterRecord *p1, /* First list to merge */
70740
+ SorterRecord *p2, /* Second list to merge */
70741
+ SorterRecord **ppOut /* OUT: Head of merged list */
70742
+){
70743
+ int rc = SQLITE_OK;
70744
+ SorterRecord *pFinal = 0;
70745
+ SorterRecord **pp = &pFinal;
70746
+ void *pVal2 = p2 ? p2->pVal : 0;
70747
+
70748
+ while( p1 && p2 ){
70749
+ int res;
70750
+ rc = vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
70751
+ if( rc!=SQLITE_OK ){
70752
+ *pp = 0;
70753
+ vdbeSorterRecordFree(db, p1);
70754
+ vdbeSorterRecordFree(db, p2);
70755
+ vdbeSorterRecordFree(db, pFinal);
70756
+ *ppOut = 0;
70757
+ return rc;
70758
+ }
70759
+ if( res<=0 ){
70760
+ *pp = p1;
70761
+ pp = &p1->pNext;
70762
+ p1 = p1->pNext;
70763
+ pVal2 = 0;
70764
+ }else{
70765
+ *pp = p2;
70766
+ pp = &p2->pNext;
70767
+ p2 = p2->pNext;
70768
+ if( p2==0 ) break;
70769
+ pVal2 = p2->pVal;
70770
+ }
70771
+ }
70772
+ *pp = p1 ? p1 : p2;
70773
+
70774
+ *ppOut = pFinal;
70775
+ return SQLITE_OK;
70776
+}
70777
+
70778
+/*
70779
+** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
70780
+** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
70781
+** occurs.
70782
+*/
70783
+static int vdbeSorterSort(sqlite3 *db, VdbeCursor *pCsr){
70784
+ int rc = SQLITE_OK;
70785
+ int i;
70786
+ SorterRecord **aSlot;
70787
+ SorterRecord *p;
70788
+ VdbeSorter *pSorter = pCsr->pSorter;
70789
+
70790
+ aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
70791
+ if( !aSlot ){
70792
+ return SQLITE_NOMEM;
70793
+ }
70794
+
70795
+ p = pSorter->pRecord;
70796
+ while( p ){
70797
+ SorterRecord *pNext = p->pNext;
70798
+ p->pNext = 0;
70799
+ for(i=0; rc==SQLITE_OK && aSlot[i]; i++){
70800
+ rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
70801
+ aSlot[i] = 0;
70802
+ }
70803
+ if( rc!=SQLITE_OK ){
70804
+ vdbeSorterRecordFree(db, pNext);
70805
+ break;
70806
+ }
70807
+ aSlot[i] = p;
70808
+ p = pNext;
70809
+ }
70810
+
70811
+ p = 0;
70812
+ for(i=0; i<64; i++){
70813
+ if( rc==SQLITE_OK ){
70814
+ rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
70815
+ }else{
70816
+ vdbeSorterRecordFree(db, aSlot[i]);
70817
+ }
70818
+ }
70819
+ pSorter->pRecord = p;
70820
+
70821
+ sqlite3_free(aSlot);
70822
+ return rc;
70823
+}
70824
+
70825
+
70826
+/*
70827
+** Write the current contents of the in-memory linked-list to a PMA. Return
70828
+** SQLITE_OK if successful, or an SQLite error code otherwise.
70829
+**
70830
+** The format of a PMA is:
70831
+**
70832
+** * A varint. This varint contains the total number of bytes of content
70833
+** in the PMA (not including the varint itself).
70834
+**
70835
+** * One or more records packed end-to-end in order of ascending keys.
70836
+** Each record consists of a varint followed by a blob of data (the
70837
+** key). The varint is the number of bytes in the blob of data.
70838
+*/
70839
+static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
70840
+ int rc = SQLITE_OK; /* Return code */
70841
+ VdbeSorter *pSorter = pCsr->pSorter;
70842
+
70843
+ if( pSorter->nInMemory==0 ){
70844
+ assert( pSorter->pRecord==0 );
70845
+ return rc;
70846
+ }
70847
+
70848
+ rc = vdbeSorterSort(db, pCsr);
70849
+
70850
+ /* If the first temporary PMA file has not been opened, open it now. */
70851
+ if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
70852
+ rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
70853
+ assert( rc!=SQLITE_OK || pSorter->pTemp1 );
70854
+ assert( pSorter->iWriteOff==0 );
70855
+ assert( pSorter->nPMA==0 );
70856
+ }
70857
+
70858
+ if( rc==SQLITE_OK ){
70859
+ i64 iOff = pSorter->iWriteOff;
70860
+ SorterRecord *p;
70861
+ SorterRecord *pNext = 0;
70862
+ static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
70863
+
70864
+ pSorter->nPMA++;
70865
+ rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
70866
+ for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
70867
+ pNext = p->pNext;
70868
+ rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
70869
+
70870
+ if( rc==SQLITE_OK ){
70871
+ rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
70872
+ iOff += p->nVal;
70873
+ }
70874
+
70875
+ sqlite3DbFree(db, p);
70876
+ }
70877
+
70878
+ /* This assert verifies that unless an error has occurred, the size of
70879
+ ** the PMA on disk is the same as the expected size stored in
70880
+ ** pSorter->nInMemory. */
70881
+ assert( rc!=SQLITE_OK || pSorter->nInMemory==(
70882
+ iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
70883
+ ));
70884
+
70885
+ pSorter->iWriteOff = iOff;
70886
+ if( rc==SQLITE_OK ){
70887
+ /* Terminate each file with 8 extra bytes so that from any offset
70888
+ ** in the file we can always read 9 bytes without a SHORT_READ error */
70889
+ rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
70890
+ }
70891
+ pSorter->pRecord = p;
70892
+ }
70893
+
70894
+ return rc;
70895
+}
70896
+
70897
+/*
70898
+** Add a record to the sorter.
70899
+*/
70900
+SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
70901
+ sqlite3 *db, /* Database handle */
70902
+ VdbeCursor *pCsr, /* Sorter cursor */
70903
+ Mem *pVal /* Memory cell containing record */
70904
+){
70905
+ VdbeSorter *pSorter = pCsr->pSorter;
70906
+ int rc = SQLITE_OK; /* Return Code */
70907
+ SorterRecord *pNew; /* New list element */
70908
+
70909
+ assert( pSorter );
70910
+ pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
70911
+
70912
+ pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
70913
+ if( pNew==0 ){
70914
+ rc = SQLITE_NOMEM;
70915
+ }else{
70916
+ pNew->pVal = (void *)&pNew[1];
70917
+ memcpy(pNew->pVal, pVal->z, pVal->n);
70918
+ pNew->nVal = pVal->n;
70919
+ pNew->pNext = pSorter->pRecord;
70920
+ pSorter->pRecord = pNew;
70921
+ }
70922
+
70923
+ /* See if the contents of the sorter should now be written out. They
70924
+ ** are written out when either of the following are true:
70925
+ **
70926
+ ** * The total memory allocated for the in-memory list is greater
70927
+ ** than (page-size * cache-size), or
70928
+ **
70929
+ ** * The total memory allocated for the in-memory list is greater
70930
+ ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
70931
+ */
70932
+ if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
70933
+ (pSorter->nInMemory>pSorter->mxPmaSize)
70934
+ || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
70935
+ )){
70936
+ rc = vdbeSorterListToPMA(db, pCsr);
70937
+ pSorter->nInMemory = 0;
70938
+ }
70939
+
70940
+ return rc;
70941
+}
70942
+
70943
+/*
70944
+** Helper function for sqlite3VdbeSorterRewind().
70945
+*/
70946
+static int vdbeSorterInitMerge(
70947
+ sqlite3 *db, /* Database handle */
70948
+ VdbeCursor *pCsr, /* Cursor handle for this sorter */
70949
+ i64 *pnByte /* Sum of bytes in all opened PMAs */
70950
+){
70951
+ VdbeSorter *pSorter = pCsr->pSorter;
70952
+ int rc = SQLITE_OK; /* Return code */
70953
+ int i; /* Used to iterator through aIter[] */
70954
+ i64 nByte = 0; /* Total bytes in all opened PMAs */
70955
+
70956
+ /* Initialize the iterators. */
70957
+ for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
70958
+ VdbeSorterIter *pIter = &pSorter->aIter[i];
70959
+ rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
70960
+ pSorter->iReadOff = pIter->iEof;
70961
+ assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
70962
+ if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
70963
+ }
70964
+
70965
+ /* Initialize the aTree[] array. */
70966
+ for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
70967
+ rc = vdbeSorterDoCompare(pCsr, i);
70968
+ }
70969
+
70970
+ *pnByte = nByte;
70971
+ return rc;
70972
+}
70973
+
70974
+/*
70975
+** Once the sorter has been populated, this function is called to prepare
70976
+** for iterating through its contents in sorted order.
70977
+*/
70978
+SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70979
+ VdbeSorter *pSorter = pCsr->pSorter;
70980
+ int rc; /* Return code */
70981
+ sqlite3_file *pTemp2 = 0; /* Second temp file to use */
70982
+ i64 iWrite2 = 0; /* Write offset for pTemp2 */
70983
+ int nIter; /* Number of iterators used */
70984
+ int nByte; /* Bytes of space required for aIter/aTree */
70985
+ int N = 2; /* Power of 2 >= nIter */
70986
+
70987
+ assert( pSorter );
70988
+
70989
+ /* If no data has been written to disk, then do not do so now. Instead,
70990
+ ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
70991
+ ** from the in-memory list. */
70992
+ if( pSorter->nPMA==0 ){
70993
+ *pbEof = !pSorter->pRecord;
70994
+ assert( pSorter->aTree==0 );
70995
+ return vdbeSorterSort(db, pCsr);
70996
+ }
70997
+
70998
+ /* Write the current b-tree to a PMA. Close the b-tree cursor. */
70999
+ rc = vdbeSorterListToPMA(db, pCsr);
71000
+ if( rc!=SQLITE_OK ) return rc;
71001
+
71002
+ /* Allocate space for aIter[] and aTree[]. */
71003
+ nIter = pSorter->nPMA;
71004
+ if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
71005
+ assert( nIter>0 );
71006
+ while( N<nIter ) N += N;
71007
+ nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
71008
+ pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
71009
+ if( !pSorter->aIter ) return SQLITE_NOMEM;
71010
+ pSorter->aTree = (int *)&pSorter->aIter[N];
71011
+ pSorter->nTree = N;
71012
+
71013
+ do {
71014
+ int iNew; /* Index of new, merged, PMA */
71015
+
71016
+ for(iNew=0;
71017
+ rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
71018
+ iNew++
71019
+ ){
71020
+ i64 nWrite; /* Number of bytes in new PMA */
71021
+
71022
+ /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
71023
+ ** initialize an iterator for each of them and break out of the loop.
71024
+ ** These iterators will be incrementally merged as the VDBE layer calls
71025
+ ** sqlite3VdbeSorterNext().
71026
+ **
71027
+ ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
71028
+ ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
71029
+ ** are merged into a single PMA that is written to file pTemp2.
71030
+ */
71031
+ rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
71032
+ assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
71033
+ if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71034
+ break;
71035
+ }
71036
+
71037
+ /* Open the second temp file, if it is not already open. */
71038
+ if( pTemp2==0 ){
71039
+ assert( iWrite2==0 );
71040
+ rc = vdbeSorterOpenTempFile(db, &pTemp2);
71041
+ }
71042
+
71043
+ if( rc==SQLITE_OK ){
71044
+ rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
71045
+ }
71046
+
71047
+ if( rc==SQLITE_OK ){
71048
+ int bEof = 0;
71049
+ while( rc==SQLITE_OK && bEof==0 ){
71050
+ int nToWrite;
71051
+ VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71052
+ assert( pIter->pFile );
71053
+ nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
71054
+ rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
71055
+ iWrite2 += nToWrite;
71056
+ if( rc==SQLITE_OK ){
71057
+ rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
71058
+ }
71059
+ }
71060
+ }
71061
+ }
71062
+
71063
+ if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71064
+ break;
71065
+ }else{
71066
+ sqlite3_file *pTmp = pSorter->pTemp1;
71067
+ pSorter->nPMA = iNew;
71068
+ pSorter->pTemp1 = pTemp2;
71069
+ pTemp2 = pTmp;
71070
+ pSorter->iWriteOff = iWrite2;
71071
+ pSorter->iReadOff = 0;
71072
+ iWrite2 = 0;
71073
+ }
71074
+ }while( rc==SQLITE_OK );
71075
+
71076
+ if( pTemp2 ){
71077
+ sqlite3OsCloseFree(pTemp2);
71078
+ }
71079
+ *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71080
+ return rc;
71081
+}
71082
+
71083
+/*
71084
+** Advance to the next element in the sorter.
71085
+*/
71086
+SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
71087
+ VdbeSorter *pSorter = pCsr->pSorter;
71088
+ int rc; /* Return code */
71089
+
71090
+ if( pSorter->aTree ){
71091
+ int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
71092
+ int i; /* Index of aTree[] to recalculate */
71093
+
71094
+ rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
71095
+ for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
71096
+ rc = vdbeSorterDoCompare(pCsr, i);
71097
+ }
71098
+
71099
+ *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71100
+ }else{
71101
+ SorterRecord *pFree = pSorter->pRecord;
71102
+ pSorter->pRecord = pFree->pNext;
71103
+ pFree->pNext = 0;
71104
+ vdbeSorterRecordFree(db, pFree);
71105
+ *pbEof = !pSorter->pRecord;
71106
+ rc = SQLITE_OK;
71107
+ }
71108
+ return rc;
71109
+}
71110
+
71111
+/*
71112
+** Return a pointer to a buffer owned by the sorter that contains the
71113
+** current key.
71114
+*/
71115
+static void *vdbeSorterRowkey(
71116
+ VdbeSorter *pSorter, /* Sorter object */
71117
+ int *pnKey /* OUT: Size of current key in bytes */
71118
+){
71119
+ void *pKey;
71120
+ if( pSorter->aTree ){
71121
+ VdbeSorterIter *pIter;
71122
+ pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71123
+ *pnKey = pIter->nKey;
71124
+ pKey = pIter->aKey;
71125
+ }else{
71126
+ *pnKey = pSorter->pRecord->nVal;
71127
+ pKey = pSorter->pRecord->pVal;
71128
+ }
71129
+ return pKey;
71130
+}
71131
+
71132
+/*
71133
+** Copy the current sorter key into the memory cell pOut.
71134
+*/
71135
+SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
71136
+ VdbeSorter *pSorter = pCsr->pSorter;
71137
+ void *pKey; int nKey; /* Sorter key to copy into pOut */
71138
+
71139
+ pKey = vdbeSorterRowkey(pSorter, &nKey);
71140
+ if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
71141
+ return SQLITE_NOMEM;
71142
+ }
71143
+ pOut->n = nKey;
71144
+ MemSetTypeFlag(pOut, MEM_Blob);
71145
+ memcpy(pOut->z, pKey, nKey);
71146
+
71147
+ return SQLITE_OK;
71148
+}
71149
+
71150
+/*
71151
+** Compare the key in memory cell pVal with the key that the sorter cursor
71152
+** passed as the first argument currently points to. For the purposes of
71153
+** the comparison, ignore the rowid field at the end of each record.
71154
+**
71155
+** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
71156
+** Otherwise, set *pRes to a negative, zero or positive value if the
71157
+** key in pVal is smaller than, equal to or larger than the current sorter
71158
+** key.
71159
+*/
71160
+SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
71161
+ VdbeCursor *pCsr, /* Sorter cursor */
71162
+ Mem *pVal, /* Value to compare to current sorter key */
71163
+ int *pRes /* OUT: Result of comparison */
71164
+){
71165
+ int rc;
71166
+ VdbeSorter *pSorter = pCsr->pSorter;
71167
+ void *pKey; int nKey; /* Sorter key to compare pVal with */
71168
+
71169
+ pKey = vdbeSorterRowkey(pSorter, &nKey);
71170
+ rc = vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
71171
+ assert( rc!=SQLITE_OK || pVal->db->mallocFailed || (*pRes)<=0 );
71172
+ return rc;
71173
+}
71174
+
71175
+#endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
71176
+
71177
+/************** End of vdbesort.c ********************************************/
6956171178
/************** Begin file journal.c *****************************************/
6956271179
/*
6956371180
** 2007 August 22
6956471181
**
6956571182
** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71689,12 @@
7007271689
**
7007371690
*************************************************************************
7007471691
** This file contains routines used for walking the parser tree for
7007571692
** an SQL statement.
7007671693
*/
71694
+/* #include <stdlib.h> */
71695
+/* #include <string.h> */
7007771696
7007871697
7007971698
/*
7008071699
** Walk an expression tree. Invoke the callback once for each node
7008171700
** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71829,12 @@
7021071829
**
7021171830
** This file contains routines used for walking the parser tree and
7021271831
** resolve all identifiers by associating them with a particular
7021371832
** table and column.
7021471833
*/
71834
+/* #include <stdlib.h> */
71835
+/* #include <string.h> */
7021571836
7021671837
/*
7021771838
** Turn the pExpr expression into an alias for the iCol-th column of the
7021871839
** result set in pEList.
7021971840
**
@@ -73700,11 +75321,11 @@
7370075321
if( !pAggInfo->directMode ){
7370175322
assert( pCol->iMem>0 );
7370275323
inReg = pCol->iMem;
7370375324
break;
7370475325
}else if( pAggInfo->useSortingIdx ){
73705
- sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
75326
+ sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
7370675327
pCol->iSorterColumn, target);
7370775328
break;
7370875329
}
7370975330
/* Otherwise, fall thru into the TK_COLUMN case */
7371075331
}
@@ -76012,100 +77633,10 @@
7601277633
** May you find forgiveness for yourself and forgive others.
7601377634
** May you share freely, never taking more than you give.
7601477635
**
7601577636
*************************************************************************
7601677637
** This file contains code associated with the ANALYZE command.
76017
-**
76018
-** The ANALYZE command gather statistics about the content of tables
76019
-** and indices. These statistics are made available to the query planner
76020
-** to help it make better decisions about how to perform queries.
76021
-**
76022
-** The following system tables are or have been supported:
76023
-**
76024
-** CREATE TABLE sqlite_stat1(tbl, idx, stat);
76025
-** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
76026
-** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
76027
-**
76028
-** Additional tables might be added in future releases of SQLite.
76029
-** The sqlite_stat2 table is not created or used unless the SQLite version
76030
-** is between 3.6.18 and 3.7.7, inclusive, and unless SQLite is compiled
76031
-** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
76032
-** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
76033
-** created and used by SQLite versions after 2011-08-09 with
76034
-** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
76035
-** is a superset of sqlite_stat2.
76036
-**
76037
-** Format of sqlite_stat1:
76038
-**
76039
-** There is normally one row per index, with the index identified by the
76040
-** name in the idx column. The tbl column is the name of the table to
76041
-** which the index belongs. In each such row, the stat column will be
76042
-** a string consisting of a list of integers. The first integer in this
76043
-** list is the number of rows in the index and in the table. The second
76044
-** integer is the average number of rows in the index that have the same
76045
-** value in the first column of the index. The third integer is the average
76046
-** number of rows in the index that have the same value for the first two
76047
-** columns. The N-th integer (for N>1) is the average number of rows in
76048
-** the index which have the same value for the first N-1 columns. For
76049
-** a K-column index, there will be K+1 integers in the stat column. If
76050
-** the index is unique, then the last integer will be 1.
76051
-**
76052
-** The list of integers in the stat column can optionally be followed
76053
-** by the keyword "unordered". The "unordered" keyword, if it is present,
76054
-** must be separated from the last integer by a single space. If the
76055
-** "unordered" keyword is present, then the query planner assumes that
76056
-** the index is unordered and will not use the index for a range query.
76057
-**
76058
-** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
76059
-** column contains a single integer which is the (estimated) number of
76060
-** rows in the table identified by sqlite_stat1.tbl.
76061
-**
76062
-** Format of sqlite_stat2:
76063
-**
76064
-** The sqlite_stat2 is only created and is only used if SQLite is compiled
76065
-** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
76066
-** 3.6.18 and 3.7.7. The "stat2" table contains additional information
76067
-** about the distribution of keys within an index. The index is identified by
76068
-** the "idx" column and the "tbl" column is the name of the table to which
76069
-** the index belongs. There are usually 10 rows in the sqlite_stat2
76070
-** table for each index.
76071
-**
76072
-** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
76073
-** inclusive are samples of the left-most key value in the index taken at
76074
-** evenly spaced points along the index. Let the number of samples be S
76075
-** (10 in the standard build) and let C be the number of rows in the index.
76076
-** Then the sampled rows are given by:
76077
-**
76078
-** rownumber = (i*C*2 + C)/(S*2)
76079
-**
76080
-** For i between 0 and S-1. Conceptually, the index space is divided into
76081
-** S uniform buckets and the samples are the middle row from each bucket.
76082
-**
76083
-** The format for sqlite_stat2 is recorded here for legacy reference. This
76084
-** version of SQLite does not support sqlite_stat2. It neither reads nor
76085
-** writes the sqlite_stat2 table. This version of SQLite only supports
76086
-** sqlite_stat3.
76087
-**
76088
-** Format for sqlite_stat3:
76089
-**
76090
-** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
76091
-** used to avoid compatibility problems.
76092
-**
76093
-** The format of the sqlite_stat3 table is similar to the format for
76094
-** the sqlite_stat2 table, with the following changes: (1)
76095
-** The sampleno column is removed. (2) Every sample has nEq, nLt, and nDLt
76096
-** columns which hold the approximate number of rows in the table that
76097
-** exactly match the sample, the approximate number of rows with values
76098
-** less than the sample, and the approximate number of distinct key values
76099
-** less than the sample, respectively. (3) The number of samples can vary
76100
-** from one table to the next; the sample count does not have to be
76101
-** exactly 10 as it is with sqlite_stat2.
76102
-**
76103
-** The ANALYZE command will typically generate sqlite_stat3 tables
76104
-** that contain between 10 and 40 samples which are distributed across
76105
-** the key space, though not uniformly, and which include samples with
76106
-** largest possible nEq values.
7610777638
*/
7610877639
#ifndef SQLITE_OMIT_ANALYZE
7610977640
7611077641
/*
7611177642
** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77664,12 @@
7613377664
static const struct {
7613477665
const char *zName;
7613577666
const char *zCols;
7613677667
} aTable[] = {
7613777668
{ "sqlite_stat1", "tbl,idx,stat" },
76138
-#ifdef SQLITE_ENABLE_STAT3
76139
- { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
76140
-#endif
76141
- };
76142
- static const char *azToDrop[] = {
76143
- "sqlite_stat2",
76144
-#ifndef SQLITE_ENABLE_STAT3
76145
- "sqlite_stat3",
77669
+#ifdef SQLITE_ENABLE_STAT2
77670
+ { "sqlite_stat2", "tbl,idx,sampleno,sample" },
7614677671
#endif
7614777672
};
7614877673
7614977674
int aRoot[] = {0, 0};
7615077675
u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77681,10 @@
7615677681
if( v==0 ) return;
7615777682
assert( sqlite3BtreeHoldsAllMutexes(db) );
7615877683
assert( sqlite3VdbeDb(v)==db );
7615977684
pDb = &db->aDb[iDb];
7616077685
76161
- /* Drop all statistics tables that this version of SQLite does not
76162
- ** understand.
76163
- */
76164
- for(i=0; i<ArraySize(azToDrop); i++){
76165
- Table *pTab = sqlite3FindTable(db, azToDrop[i], pDb->zName);
76166
- if( pTab ) sqlite3CodeDropTable(pParse, pTab, iDb, 0);
76167
- }
76168
-
76169
- /* Create new statistic tables if they do not exist, or clear them
76170
- ** if they do already exist.
76171
- */
7617277686
for(i=0; i<ArraySize(aTable); i++){
7617377687
const char *zTab = aTable[i].zName;
7617477688
Table *pStat;
7617577689
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
7617677690
/* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77711,17 @@
7619777711
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
7619877712
}
7619977713
}
7620077714
}
7620177715
76202
- /* Open the sqlite_stat[13] tables for writing. */
77716
+ /* Open the sqlite_stat[12] tables for writing. */
7620377717
for(i=0; i<ArraySize(aTable); i++){
7620477718
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
7620577719
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
7620677720
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
7620777721
}
7620877722
}
76209
-
76210
-/*
76211
-** Recommended number of samples for sqlite_stat3
76212
-*/
76213
-#ifndef SQLITE_STAT3_SAMPLES
76214
-# define SQLITE_STAT3_SAMPLES 24
76215
-#endif
76216
-
76217
-/*
76218
-** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
76219
-** share an instance of the following structure to hold their state
76220
-** information.
76221
-*/
76222
-typedef struct Stat3Accum Stat3Accum;
76223
-struct Stat3Accum {
76224
- tRowcnt nRow; /* Number of rows in the entire table */
76225
- tRowcnt nPSample; /* How often to do a periodic sample */
76226
- int iMin; /* Index of entry with minimum nEq and hash */
76227
- int mxSample; /* Maximum number of samples to accumulate */
76228
- int nSample; /* Current number of samples */
76229
- u32 iPrn; /* Pseudo-random number used for sampling */
76230
- struct Stat3Sample {
76231
- i64 iRowid; /* Rowid in main table of the key */
76232
- tRowcnt nEq; /* sqlite_stat3.nEq */
76233
- tRowcnt nLt; /* sqlite_stat3.nLt */
76234
- tRowcnt nDLt; /* sqlite_stat3.nDLt */
76235
- u8 isPSample; /* True if a periodic sample */
76236
- u32 iHash; /* Tiebreaker hash */
76237
- } *a; /* An array of samples */
76238
-};
76239
-
76240
-#ifdef SQLITE_ENABLE_STAT3
76241
-/*
76242
-** Implementation of the stat3_init(C,S) SQL function. The two parameters
76243
-** are the number of rows in the table or index (C) and the number of samples
76244
-** to accumulate (S).
76245
-**
76246
-** This routine allocates the Stat3Accum object.
76247
-**
76248
-** The return value is the Stat3Accum object (P).
76249
-*/
76250
-static void stat3Init(
76251
- sqlite3_context *context,
76252
- int argc,
76253
- sqlite3_value **argv
76254
-){
76255
- Stat3Accum *p;
76256
- tRowcnt nRow;
76257
- int mxSample;
76258
- int n;
76259
-
76260
- UNUSED_PARAMETER(argc);
76261
- nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
76262
- mxSample = sqlite3_value_int(argv[1]);
76263
- n = sizeof(*p) + sizeof(p->a[0])*mxSample;
76264
- p = sqlite3_malloc( n );
76265
- if( p==0 ){
76266
- sqlite3_result_error_nomem(context);
76267
- return;
76268
- }
76269
- memset(p, 0, n);
76270
- p->a = (struct Stat3Sample*)&p[1];
76271
- p->nRow = nRow;
76272
- p->mxSample = mxSample;
76273
- p->nPSample = p->nRow/(mxSample/3+1) + 1;
76274
- sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
76275
- sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
76276
-}
76277
-static const FuncDef stat3InitFuncdef = {
76278
- 2, /* nArg */
76279
- SQLITE_UTF8, /* iPrefEnc */
76280
- 0, /* flags */
76281
- 0, /* pUserData */
76282
- 0, /* pNext */
76283
- stat3Init, /* xFunc */
76284
- 0, /* xStep */
76285
- 0, /* xFinalize */
76286
- "stat3_init", /* zName */
76287
- 0, /* pHash */
76288
- 0 /* pDestructor */
76289
-};
76290
-
76291
-
76292
-/*
76293
-** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
76294
-** arguments describe a single key instance. This routine makes the
76295
-** decision about whether or not to retain this key for the sqlite_stat3
76296
-** table.
76297
-**
76298
-** The return value is NULL.
76299
-*/
76300
-static void stat3Push(
76301
- sqlite3_context *context,
76302
- int argc,
76303
- sqlite3_value **argv
76304
-){
76305
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
76306
- tRowcnt nEq = sqlite3_value_int64(argv[0]);
76307
- tRowcnt nLt = sqlite3_value_int64(argv[1]);
76308
- tRowcnt nDLt = sqlite3_value_int64(argv[2]);
76309
- i64 rowid = sqlite3_value_int64(argv[3]);
76310
- u8 isPSample = 0;
76311
- u8 doInsert = 0;
76312
- int iMin = p->iMin;
76313
- struct Stat3Sample *pSample;
76314
- int i;
76315
- u32 h;
76316
-
76317
- UNUSED_PARAMETER(context);
76318
- UNUSED_PARAMETER(argc);
76319
- if( nEq==0 ) return;
76320
- h = p->iPrn = p->iPrn*1103515245 + 12345;
76321
- if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
76322
- doInsert = isPSample = 1;
76323
- }else if( p->nSample<p->mxSample ){
76324
- doInsert = 1;
76325
- }else{
76326
- if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
76327
- doInsert = 1;
76328
- }
76329
- }
76330
- if( !doInsert ) return;
76331
- if( p->nSample==p->mxSample ){
76332
- if( iMin<p->nSample ){
76333
- memcpy(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin));
76334
- }
76335
- pSample = &p->a[p->nSample-1];
76336
- }else{
76337
- pSample = &p->a[p->nSample++];
76338
- }
76339
- pSample->iRowid = rowid;
76340
- pSample->nEq = nEq;
76341
- pSample->nLt = nLt;
76342
- pSample->nDLt = nDLt;
76343
- pSample->iHash = h;
76344
- pSample->isPSample = isPSample;
76345
-
76346
- /* Find the new minimum */
76347
- if( p->nSample==p->mxSample ){
76348
- pSample = p->a;
76349
- i = 0;
76350
- while( pSample->isPSample ){
76351
- i++;
76352
- pSample++;
76353
- assert( i<p->nSample );
76354
- }
76355
- nEq = pSample->nEq;
76356
- h = pSample->iHash;
76357
- iMin = i;
76358
- for(i++, pSample++; i<p->nSample; i++, pSample++){
76359
- if( pSample->isPSample ) continue;
76360
- if( pSample->nEq<nEq
76361
- || (pSample->nEq==nEq && pSample->iHash<h)
76362
- ){
76363
- iMin = i;
76364
- nEq = pSample->nEq;
76365
- h = pSample->iHash;
76366
- }
76367
- }
76368
- p->iMin = iMin;
76369
- }
76370
-}
76371
-static const FuncDef stat3PushFuncdef = {
76372
- 5, /* nArg */
76373
- SQLITE_UTF8, /* iPrefEnc */
76374
- 0, /* flags */
76375
- 0, /* pUserData */
76376
- 0, /* pNext */
76377
- stat3Push, /* xFunc */
76378
- 0, /* xStep */
76379
- 0, /* xFinalize */
76380
- "stat3_push", /* zName */
76381
- 0, /* pHash */
76382
- 0 /* pDestructor */
76383
-};
76384
-
76385
-/*
76386
-** Implementation of the stat3_get(P,N,...) SQL function. This routine is
76387
-** used to query the results. Content is returned for the Nth sqlite_stat3
76388
-** row where N is between 0 and S-1 and S is the number of samples. The
76389
-** value returned depends on the number of arguments.
76390
-**
76391
-** argc==2 result: rowid
76392
-** argc==3 result: nEq
76393
-** argc==4 result: nLt
76394
-** argc==5 result: nDLt
76395
-*/
76396
-static void stat3Get(
76397
- sqlite3_context *context,
76398
- int argc,
76399
- sqlite3_value **argv
76400
-){
76401
- int n = sqlite3_value_int(argv[1]);
76402
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
76403
-
76404
- assert( p!=0 );
76405
- if( p->nSample<=n ) return;
76406
- switch( argc ){
76407
- case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
76408
- case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
76409
- case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
76410
- case 5: sqlite3_result_int64(context, p->a[n].nDLt); break;
76411
- }
76412
-}
76413
-static const FuncDef stat3GetFuncdef = {
76414
- -1, /* nArg */
76415
- SQLITE_UTF8, /* iPrefEnc */
76416
- 0, /* flags */
76417
- 0, /* pUserData */
76418
- 0, /* pNext */
76419
- stat3Get, /* xFunc */
76420
- 0, /* xStep */
76421
- 0, /* xFinalize */
76422
- "stat3_get", /* zName */
76423
- 0, /* pHash */
76424
- 0 /* pDestructor */
76425
-};
76426
-#endif /* SQLITE_ENABLE_STAT3 */
76427
-
76428
-
76429
-
7643077723
7643177724
/*
7643277725
** Generate code to do an analysis of all indices associated with
7643377726
** a single table.
7643477727
*/
@@ -76448,31 +77741,24 @@
7644877741
int endOfLoop; /* The end of the loop */
7644977742
int jZeroRows = -1; /* Jump from here if number of rows is zero */
7645077743
int iDb; /* Index of database containing pTab */
7645177744
int regTabname = iMem++; /* Register containing table name */
7645277745
int regIdxname = iMem++; /* Register containing index name */
76453
- int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
76454
-#ifdef SQLITE_ENABLE_STAT3
76455
- int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
76456
- int regNumLt = iMem++; /* Number of keys less than regSample */
76457
- int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
76458
- int regSample = iMem++; /* The next sample value */
76459
- int regRowid = regSample; /* Rowid of a sample */
76460
- int regAccum = iMem++; /* Register to hold Stat3Accum object */
76461
- int regLoop = iMem++; /* Loop counter */
76462
- int regCount = iMem++; /* Number of rows in the table or index */
76463
- int regTemp1 = iMem++; /* Intermediate register */
76464
- int regTemp2 = iMem++; /* Intermediate register */
76465
- int once = 1; /* One-time initialization */
76466
- int shortJump = 0; /* Instruction address */
76467
- int iTabCur = pParse->nTab++; /* Table cursor */
76468
-#endif
76469
- int regCol = iMem++; /* Content of a column in analyzed table */
77746
+ int regSampleno = iMem++; /* Register containing next sample number */
77747
+ int regCol = iMem++; /* Content of a column analyzed table */
7647077748
int regRec = iMem++; /* Register holding completed record */
7647177749
int regTemp = iMem++; /* Temporary use register */
76472
- int regNewRowid = iMem++; /* Rowid for the inserted record */
77750
+ int regRowid = iMem++; /* Rowid for the inserted record */
7647377751
77752
+#ifdef SQLITE_ENABLE_STAT2
77753
+ int addr = 0; /* Instruction address */
77754
+ int regTemp2 = iMem++; /* Temporary use register */
77755
+ int regSamplerecno = iMem++; /* Index of next sample to record */
77756
+ int regRecno = iMem++; /* Current sample index */
77757
+ int regLast = iMem++; /* Index of last sample to record */
77758
+ int regFirst = iMem++; /* Index of first sample to record */
77759
+#endif
7647477760
7647577761
v = sqlite3GetVdbe(pParse);
7647677762
if( v==0 || NEVER(pTab==0) ){
7647777763
return;
7647877764
}
@@ -76501,22 +77787,17 @@
7650177787
iIdxCur = pParse->nTab++;
7650277788
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
7650377789
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7650477790
int nCol;
7650577791
KeyInfo *pKey;
76506
- int addrIfNot = 0; /* address of OP_IfNot */
76507
- int *aChngAddr; /* Array of jump instruction addresses */
7650877792
7650977793
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
76510
- VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
7651177794
nCol = pIdx->nColumn;
7651277795
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
7651377796
if( iMem+1+(nCol*2)>pParse->nMem ){
7651477797
pParse->nMem = iMem+1+(nCol*2);
7651577798
}
76516
- aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn);
76517
- if( aChngAddr==0 ) continue;
7651877799
7651977800
/* Open a cursor to the index to be analyzed. */
7652077801
assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
7652177802
sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
7652277803
(char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77804,35 @@
7652377804
VdbeComment((v, "%s", pIdx->zName));
7652477805
7652577806
/* Populate the register containing the index name. */
7652677807
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
7652777808
76528
-#ifdef SQLITE_ENABLE_STAT3
76529
- if( once ){
76530
- once = 0;
76531
- sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
76532
- }
76533
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
76534
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
76535
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
76536
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
76537
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
76538
- sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
76539
- (char*)&stat3InitFuncdef, P4_FUNCDEF);
76540
- sqlite3VdbeChangeP5(v, 2);
76541
-#endif /* SQLITE_ENABLE_STAT3 */
77809
+#ifdef SQLITE_ENABLE_STAT2
77810
+
77811
+ /* If this iteration of the loop is generating code to analyze the
77812
+ ** first index in the pTab->pIndex list, then register regLast has
77813
+ ** not been populated. In this case populate it now. */
77814
+ if( pTab->pIndex==pIdx ){
77815
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77816
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77817
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77818
+
77819
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77820
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77821
+ addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77822
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77823
+ sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77824
+ sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77825
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77826
+ sqlite3VdbeJumpHere(v, addr);
77827
+ }
77828
+
77829
+ /* Zero the regSampleno and regRecno registers. */
77830
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77831
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77832
+ sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77833
+#endif
7654277834
7654377835
/* The block of memory cells initialized here is used as follows.
7654477836
**
7654577837
** iMem:
7654677838
** The total number of rows in the table.
@@ -76566,87 +77858,79 @@
7656677858
/* Start the analysis loop. This loop runs through all the entries in
7656777859
** the index b-tree. */
7656877860
endOfLoop = sqlite3VdbeMakeLabel(v);
7656977861
sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
7657077862
topOfLoop = sqlite3VdbeCurrentAddr(v);
76571
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
77863
+ sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
7657277864
7657377865
for(i=0; i<nCol; i++){
7657477866
CollSeq *pColl;
7657577867
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
7657677868
if( i==0 ){
77869
+#ifdef SQLITE_ENABLE_STAT2
77870
+ /* Check if the record that cursor iIdxCur points to contains a
77871
+ ** value that should be stored in the sqlite_stat2 table. If so,
77872
+ ** store it. */
77873
+ int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77874
+ assert( regTabname+1==regIdxname
77875
+ && regTabname+2==regSampleno
77876
+ && regTabname+3==regCol
77877
+ );
77878
+ sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77879
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77880
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77881
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77882
+
77883
+ /* Calculate new values for regSamplerecno and regSampleno.
77884
+ **
77885
+ ** sampleno = sampleno + 1
77886
+ ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77887
+ */
77888
+ sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77889
+ sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77890
+ sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77891
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77892
+ sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77893
+ sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77894
+ sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77895
+
77896
+ sqlite3VdbeJumpHere(v, ne);
77897
+ sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77898
+#endif
77899
+
7657777900
/* Always record the very first row */
76578
- addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77901
+ sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
7657977902
}
7658077903
assert( pIdx->azColl!=0 );
7658177904
assert( pIdx->azColl[i]!=0 );
7658277905
pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76583
- aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76584
- (char*)pColl, P4_COLLSEQ);
77906
+ sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77907
+ (char*)pColl, P4_COLLSEQ);
7658577908
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76586
- VdbeComment((v, "jump if column %d changed", i));
76587
-#ifdef SQLITE_ENABLE_STAT3
76588
- if( i==0 ){
76589
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
76590
- VdbeComment((v, "incr repeat count"));
76591
- }
76592
-#endif
77909
+ }
77910
+ if( db->mallocFailed ){
77911
+ /* If a malloc failure has occurred, then the result of the expression
77912
+ ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77913
+ ** below may be negative. Which causes an assert() to fail (or an
77914
+ ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77915
+ return;
7659377916
}
7659477917
sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
7659577918
for(i=0; i<nCol; i++){
76596
- sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
77919
+ int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
7659777920
if( i==0 ){
76598
- sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
76599
-#ifdef SQLITE_ENABLE_STAT3
76600
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
76601
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
76602
- sqlite3VdbeChangeP5(v, 5);
76603
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
76604
- sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
76605
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
76606
- sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
76607
-#endif
76608
- }
77921
+ sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
77922
+ }
77923
+ sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
7660977924
sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
7661077925
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
7661177926
}
76612
- sqlite3DbFree(db, aChngAddr);
7661377927
76614
- /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
77928
+ /* End of the analysis loop. */
7661577929
sqlite3VdbeResolveLabel(v, endOfLoop);
76616
-
7661777930
sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
7661877931
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76619
-#ifdef SQLITE_ENABLE_STAT3
76620
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
76621
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
76622
- sqlite3VdbeChangeP5(v, 5);
76623
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
76624
- shortJump =
76625
- sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
76626
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
76627
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
76628
- sqlite3VdbeChangeP5(v, 2);
76629
- sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
76630
- sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
76631
- sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
76632
- sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
76633
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
76634
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
76635
- sqlite3VdbeChangeP5(v, 3);
76636
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
76637
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
76638
- sqlite3VdbeChangeP5(v, 4);
76639
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
76640
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
76641
- sqlite3VdbeChangeP5(v, 5);
76642
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
76643
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
76644
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
76645
- sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
76646
- sqlite3VdbeJumpHere(v, shortJump+2);
76647
-#endif
7664877932
7664977933
/* Store the results in sqlite_stat1.
7665077934
**
7665177935
** The result is a single row of the sqlite_stat1 table. The first
7665277936
** two columns are the names of the table and index. The third column
@@ -76662,51 +77946,50 @@
7666277946
**
7666377947
** If K==0 then no entry is made into the sqlite_stat1 table.
7666477948
** If K>0 then it is always the case the D>0 so division by zero
7666577949
** is never possible.
7666677950
*/
76667
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
77951
+ sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
7666877952
if( jZeroRows<0 ){
7666977953
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
7667077954
}
7667177955
for(i=0; i<nCol; i++){
7667277956
sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76673
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
77957
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
7667477958
sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
7667577959
sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
7667677960
sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
7667777961
sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76678
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
77962
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
7667977963
}
7668077964
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76681
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76682
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
77965
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77966
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
7668377967
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7668477968
}
7668577969
7668677970
/* If the table has no indices, create a single sqlite_stat1 entry
7668777971
** containing NULL as the index name and the row count as the content.
7668877972
*/
7668977973
if( pTab->pIndex==0 ){
7669077974
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
7669177975
VdbeComment((v, "%s", pTab->zName));
76692
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
77976
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
7669377977
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76694
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
77978
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
7669577979
}else{
7669677980
sqlite3VdbeJumpHere(v, jZeroRows);
7669777981
jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
7669877982
}
7669977983
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
7670077984
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76701
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76702
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
77985
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77986
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
7670377987
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7670477988
if( pParse->nMem<regRec ) pParse->nMem = regRec;
7670577989
sqlite3VdbeJumpHere(v, jZeroRows);
7670677990
}
76707
-
7670877991
7670977992
/*
7671077993
** Generate code that will cause the most recent index analysis to
7671177994
** be loaded into internal hash tables where is can be used.
7671277995
*/
@@ -76727,11 +78010,11 @@
7672778010
int iStatCur;
7672878011
int iMem;
7672978012
7673078013
sqlite3BeginWriteOperation(pParse, 0, iDb);
7673178014
iStatCur = pParse->nTab;
76732
- pParse->nTab += 3;
78015
+ pParse->nTab += 2;
7673378016
openStatTable(pParse, iDb, iStatCur, 0, 0);
7673478017
iMem = pParse->nMem+1;
7673578018
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7673678019
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
7673778020
Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +78035,11 @@
7675278035
assert( pTab!=0 );
7675378036
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
7675478037
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
7675578038
sqlite3BeginWriteOperation(pParse, 0, iDb);
7675678039
iStatCur = pParse->nTab;
76757
- pParse->nTab += 3;
78040
+ pParse->nTab += 2;
7675878041
if( pOnlyIdx ){
7675978042
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
7676078043
}else{
7676178044
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
7676278045
}
@@ -76857,11 +78140,11 @@
7685778140
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
7685878141
analysisInfo *pInfo = (analysisInfo*)pData;
7685978142
Index *pIndex;
7686078143
Table *pTable;
7686178144
int i, c, n;
76862
- tRowcnt v;
78145
+ unsigned int v;
7686378146
const char *z;
7686478147
7686578148
assert( argc==3 );
7686678149
UNUSED_PARAMETER2(NotUsed, argc);
7686778150
@@ -76900,172 +78183,40 @@
7690078183
/*
7690178184
** If the Index.aSample variable is not NULL, delete the aSample[] array
7690278185
** and its contents.
7690378186
*/
7690478187
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76905
-#ifdef SQLITE_ENABLE_STAT3
78188
+#ifdef SQLITE_ENABLE_STAT2
7690678189
if( pIdx->aSample ){
7690778190
int j;
76908
- for(j=0; j<pIdx->nSample; j++){
78191
+ for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
7690978192
IndexSample *p = &pIdx->aSample[j];
7691078193
if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76911
- sqlite3_free(p->u.z);
78194
+ sqlite3DbFree(db, p->u.z);
7691278195
}
7691378196
}
76914
- sqlite3_free(pIdx->aSample);
78197
+ sqlite3DbFree(db, pIdx->aSample);
7691578198
}
76916
- UNUSED_PARAMETER(db);
76917
- pIdx->nSample = 0;
76918
- pIdx->aSample = 0;
7691978199
#else
7692078200
UNUSED_PARAMETER(db);
7692178201
UNUSED_PARAMETER(pIdx);
7692278202
#endif
7692378203
}
7692478204
76925
-#ifdef SQLITE_ENABLE_STAT3
76926
-/*
76927
-** Load content from the sqlite_stat3 table into the Index.aSample[]
76928
-** arrays of all indices.
76929
-*/
76930
-static int loadStat3(sqlite3 *db, const char *zDb){
76931
- int rc; /* Result codes from subroutines */
76932
- sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
76933
- char *zSql; /* Text of the SQL statement */
76934
- Index *pPrevIdx = 0; /* Previous index in the loop */
76935
- int idx = 0; /* slot in pIdx->aSample[] for next sample */
76936
- int eType; /* Datatype of a sample */
76937
- IndexSample *pSample; /* A slot in pIdx->aSample[] */
76938
-
76939
- if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
76940
- return SQLITE_OK;
76941
- }
76942
-
76943
- zSql = sqlite3MPrintf(db,
76944
- "SELECT idx,count(*) FROM %Q.sqlite_stat3"
76945
- " GROUP BY idx", zDb);
76946
- if( !zSql ){
76947
- return SQLITE_NOMEM;
76948
- }
76949
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76950
- sqlite3DbFree(db, zSql);
76951
- if( rc ) return rc;
76952
-
76953
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
76954
- char *zIndex; /* Index name */
76955
- Index *pIdx; /* Pointer to the index object */
76956
- int nSample; /* Number of samples */
76957
-
76958
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
76959
- if( zIndex==0 ) continue;
76960
- nSample = sqlite3_column_int(pStmt, 1);
76961
- if( nSample>255 ) continue;
76962
- pIdx = sqlite3FindIndex(db, zIndex, zDb);
76963
- if( pIdx==0 ) continue;
76964
- assert( pIdx->nSample==0 );
76965
- pIdx->nSample = (u8)nSample;
76966
- pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
76967
- pIdx->avgEq = pIdx->aiRowEst[1];
76968
- if( pIdx->aSample==0 ){
76969
- db->mallocFailed = 1;
76970
- sqlite3_finalize(pStmt);
76971
- return SQLITE_NOMEM;
76972
- }
76973
- }
76974
- sqlite3_finalize(pStmt);
76975
-
76976
- zSql = sqlite3MPrintf(db,
76977
- "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
76978
- if( !zSql ){
76979
- return SQLITE_NOMEM;
76980
- }
76981
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76982
- sqlite3DbFree(db, zSql);
76983
- if( rc ) return rc;
76984
-
76985
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
76986
- char *zIndex; /* Index name */
76987
- Index *pIdx; /* Pointer to the index object */
76988
- int i; /* Loop counter */
76989
- tRowcnt sumEq; /* Sum of the nEq values */
76990
-
76991
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
76992
- if( zIndex==0 ) continue;
76993
- pIdx = sqlite3FindIndex(db, zIndex, zDb);
76994
- if( pIdx==0 ) continue;
76995
- if( pIdx==pPrevIdx ){
76996
- idx++;
76997
- }else{
76998
- pPrevIdx = pIdx;
76999
- idx = 0;
77000
- }
77001
- assert( idx<pIdx->nSample );
77002
- pSample = &pIdx->aSample[idx];
77003
- pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
77004
- pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
77005
- pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
77006
- if( idx==pIdx->nSample-1 ){
77007
- if( pSample->nDLt>0 ){
77008
- for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
77009
- pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
77010
- }
77011
- if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
77012
- }
77013
- eType = sqlite3_column_type(pStmt, 4);
77014
- pSample->eType = (u8)eType;
77015
- switch( eType ){
77016
- case SQLITE_INTEGER: {
77017
- pSample->u.i = sqlite3_column_int64(pStmt, 4);
77018
- break;
77019
- }
77020
- case SQLITE_FLOAT: {
77021
- pSample->u.r = sqlite3_column_double(pStmt, 4);
77022
- break;
77023
- }
77024
- case SQLITE_NULL: {
77025
- break;
77026
- }
77027
- default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
77028
- const char *z = (const char *)(
77029
- (eType==SQLITE_BLOB) ?
77030
- sqlite3_column_blob(pStmt, 4):
77031
- sqlite3_column_text(pStmt, 4)
77032
- );
77033
- int n = sqlite3_column_bytes(pStmt, 4);
77034
- if( n>0xffff ) n = 0xffff;
77035
- pSample->nByte = (u16)n;
77036
- if( n < 1){
77037
- pSample->u.z = 0;
77038
- }else{
77039
- pSample->u.z = sqlite3Malloc(n);
77040
- if( pSample->u.z==0 ){
77041
- db->mallocFailed = 1;
77042
- sqlite3_finalize(pStmt);
77043
- return SQLITE_NOMEM;
77044
- }
77045
- memcpy(pSample->u.z, z, n);
77046
- }
77047
- }
77048
- }
77049
- }
77050
- return sqlite3_finalize(pStmt);
77051
-}
77052
-#endif /* SQLITE_ENABLE_STAT3 */
77053
-
77054
-/*
77055
-** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
78205
+/*
78206
+** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
7705678207
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
77057
-** arrays. The contents of sqlite_stat3 are used to populate the
78208
+** arrays. The contents of sqlite_stat2 are used to populate the
7705878209
** Index.aSample[] arrays.
7705978210
**
7706078211
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
77061
-** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
77062
-** during compilation and the sqlite_stat3 table is present, no data is
78212
+** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
78213
+** during compilation and the sqlite_stat2 table is present, no data is
7706378214
** read from it.
7706478215
**
77065
-** If SQLITE_ENABLE_STAT3 was defined during compilation and the
77066
-** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
78216
+** If SQLITE_ENABLE_STAT2 was defined during compilation and the
78217
+** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
7706778218
** returned. However, in this case, data is read from the sqlite_stat1
7706878219
** table (if it is present) before returning.
7706978220
**
7707078221
** If an OOM error occurs, this function always sets db->mallocFailed.
7707178222
** This means if the caller does not care about other errors, the return
@@ -77083,14 +78234,12 @@
7708378234
/* Clear any prior statistics */
7708478235
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7708578236
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
7708678237
Index *pIdx = sqliteHashData(i);
7708778238
sqlite3DefaultRowEst(pIdx);
77088
-#ifdef SQLITE_ENABLE_STAT3
7708978239
sqlite3DeleteIndexSamples(db, pIdx);
7709078240
pIdx->aSample = 0;
77091
-#endif
7709278241
}
7709378242
7709478243
/* Check to make sure the sqlite_stat1 table exists */
7709578244
sInfo.db = db;
7709678245
sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78247,91 @@
7709878247
return SQLITE_ERROR;
7709978248
}
7710078249
7710178250
/* Load new statistics out of the sqlite_stat1 table */
7710278251
zSql = sqlite3MPrintf(db,
77103
- "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78252
+ "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
7710478253
if( zSql==0 ){
7710578254
rc = SQLITE_NOMEM;
7710678255
}else{
7710778256
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
7710878257
sqlite3DbFree(db, zSql);
7710978258
}
7711078259
7711178260
77112
- /* Load the statistics from the sqlite_stat3 table. */
77113
-#ifdef SQLITE_ENABLE_STAT3
78261
+ /* Load the statistics from the sqlite_stat2 table. */
78262
+#ifdef SQLITE_ENABLE_STAT2
78263
+ if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78264
+ rc = SQLITE_ERROR;
78265
+ }
7711478266
if( rc==SQLITE_OK ){
77115
- rc = loadStat3(db, sInfo.zDatabase);
78267
+ sqlite3_stmt *pStmt = 0;
78268
+
78269
+ zSql = sqlite3MPrintf(db,
78270
+ "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78271
+ if( !zSql ){
78272
+ rc = SQLITE_NOMEM;
78273
+ }else{
78274
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78275
+ sqlite3DbFree(db, zSql);
78276
+ }
78277
+
78278
+ if( rc==SQLITE_OK ){
78279
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
78280
+ char *zIndex; /* Index name */
78281
+ Index *pIdx; /* Pointer to the index object */
78282
+
78283
+ zIndex = (char *)sqlite3_column_text(pStmt, 0);
78284
+ pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78285
+ if( pIdx ){
78286
+ int iSample = sqlite3_column_int(pStmt, 1);
78287
+ if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78288
+ int eType = sqlite3_column_type(pStmt, 2);
78289
+
78290
+ if( pIdx->aSample==0 ){
78291
+ static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78292
+ pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78293
+ if( pIdx->aSample==0 ){
78294
+ db->mallocFailed = 1;
78295
+ break;
78296
+ }
78297
+ memset(pIdx->aSample, 0, sz);
78298
+ }
78299
+
78300
+ assert( pIdx->aSample );
78301
+ {
78302
+ IndexSample *pSample = &pIdx->aSample[iSample];
78303
+ pSample->eType = (u8)eType;
78304
+ if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78305
+ pSample->u.r = sqlite3_column_double(pStmt, 2);
78306
+ }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78307
+ const char *z = (const char *)(
78308
+ (eType==SQLITE_BLOB) ?
78309
+ sqlite3_column_blob(pStmt, 2):
78310
+ sqlite3_column_text(pStmt, 2)
78311
+ );
78312
+ int n = sqlite3_column_bytes(pStmt, 2);
78313
+ if( n>24 ){
78314
+ n = 24;
78315
+ }
78316
+ pSample->nByte = (u8)n;
78317
+ if( n < 1){
78318
+ pSample->u.z = 0;
78319
+ }else{
78320
+ pSample->u.z = sqlite3DbStrNDup(0, z, n);
78321
+ if( pSample->u.z==0 ){
78322
+ db->mallocFailed = 1;
78323
+ break;
78324
+ }
78325
+ }
78326
+ }
78327
+ }
78328
+ }
78329
+ }
78330
+ }
78331
+ rc = sqlite3_finalize(pStmt);
78332
+ }
7711678333
}
7711778334
#endif
7711878335
7711978336
if( rc==SQLITE_NOMEM ){
7712078337
db->mallocFailed = 1;
@@ -79610,11 +80827,11 @@
7961080827
Table *p;
7961180828
int n;
7961280829
const char *z;
7961380830
Token sEnd;
7961480831
DbFixer sFix;
79615
- Token *pName;
80832
+ Token *pName = 0;
7961680833
int iDb;
7961780834
sqlite3 *db = pParse->db;
7961880835
7961980836
if( pParse->nVar>0 ){
7962080837
sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +81143,11 @@
7992681143
Parse *pParse, /* The parsing context */
7992781144
int iDb, /* The database number */
7992881145
const char *zType, /* "idx" or "tbl" */
7992981146
const char *zName /* Name of index or table */
7993081147
){
79931
- static const char *azStatTab[] = {
79932
- "sqlite_stat1",
79933
- "sqlite_stat2",
79934
- "sqlite_stat3",
79935
- };
81148
+ static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
7993681149
int i;
7993781150
const char *zDbName = pParse->db->aDb[iDb].zName;
7993881151
for(i=0; i<ArraySize(azStatTab); i++){
7993981152
if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
7994081153
sqlite3NestedParse(pParse,
@@ -79941,81 +81154,10 @@
7994181154
"DELETE FROM %Q.%s WHERE %s=%Q",
7994281155
zDbName, azStatTab[i], zType, zName
7994381156
);
7994481157
}
7994581158
}
79946
-}
79947
-
79948
-/*
79949
-** Generate code to drop a table.
79950
-*/
79951
-SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
79952
- Vdbe *v;
79953
- sqlite3 *db = pParse->db;
79954
- Trigger *pTrigger;
79955
- Db *pDb = &db->aDb[iDb];
79956
-
79957
- v = sqlite3GetVdbe(pParse);
79958
- assert( v!=0 );
79959
- sqlite3BeginWriteOperation(pParse, 1, iDb);
79960
-
79961
-#ifndef SQLITE_OMIT_VIRTUALTABLE
79962
- if( IsVirtual(pTab) ){
79963
- sqlite3VdbeAddOp0(v, OP_VBegin);
79964
- }
79965
-#endif
79966
-
79967
- /* Drop all triggers associated with the table being dropped. Code
79968
- ** is generated to remove entries from sqlite_master and/or
79969
- ** sqlite_temp_master if required.
79970
- */
79971
- pTrigger = sqlite3TriggerList(pParse, pTab);
79972
- while( pTrigger ){
79973
- assert( pTrigger->pSchema==pTab->pSchema ||
79974
- pTrigger->pSchema==db->aDb[1].pSchema );
79975
- sqlite3DropTriggerPtr(pParse, pTrigger);
79976
- pTrigger = pTrigger->pNext;
79977
- }
79978
-
79979
-#ifndef SQLITE_OMIT_AUTOINCREMENT
79980
- /* Remove any entries of the sqlite_sequence table associated with
79981
- ** the table being dropped. This is done before the table is dropped
79982
- ** at the btree level, in case the sqlite_sequence table needs to
79983
- ** move as a result of the drop (can happen in auto-vacuum mode).
79984
- */
79985
- if( pTab->tabFlags & TF_Autoincrement ){
79986
- sqlite3NestedParse(pParse,
79987
- "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
79988
- pDb->zName, pTab->zName
79989
- );
79990
- }
79991
-#endif
79992
-
79993
- /* Drop all SQLITE_MASTER table and index entries that refer to the
79994
- ** table. The program name loops through the master table and deletes
79995
- ** every row that refers to a table of the same name as the one being
79996
- ** dropped. Triggers are handled seperately because a trigger can be
79997
- ** created in the temp database that refers to a table in another
79998
- ** database.
79999
- */
80000
- sqlite3NestedParse(pParse,
80001
- "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
80002
- pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
80003
- if( !isView && !IsVirtual(pTab) ){
80004
- destroyTable(pParse, pTab);
80005
- }
80006
-
80007
- /* Remove the table entry from SQLite's internal schema and modify
80008
- ** the schema cookie.
80009
- */
80010
- if( IsVirtual(pTab) ){
80011
- sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
80012
- }
80013
- sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
80014
- sqlite3ChangeCookie(pParse, iDb);
80015
- sqliteViewResetAll(db, iDb);
80016
-
8001781159
}
8001881160
8001981161
/*
8002081162
** This routine is called to do the work of a DROP TABLE statement.
8002181163
** pName is the name of the table to be dropped.
@@ -80082,11 +81224,11 @@
8008281224
if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
8008381225
goto exit_drop_table;
8008481226
}
8008581227
}
8008681228
#endif
80087
- if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
81229
+ if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
8008881230
sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
8008981231
goto exit_drop_table;
8009081232
}
8009181233
8009281234
#ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81248,72 @@
8010681248
/* Generate code to remove the table from the master table
8010781249
** on disk.
8010881250
*/
8010981251
v = sqlite3GetVdbe(pParse);
8011081252
if( v ){
81253
+ Trigger *pTrigger;
81254
+ Db *pDb = &db->aDb[iDb];
8011181255
sqlite3BeginWriteOperation(pParse, 1, iDb);
81256
+
81257
+#ifndef SQLITE_OMIT_VIRTUALTABLE
81258
+ if( IsVirtual(pTab) ){
81259
+ sqlite3VdbeAddOp0(v, OP_VBegin);
81260
+ }
81261
+#endif
8011281262
sqlite3FkDropTable(pParse, pName, pTab);
80113
- sqlite3CodeDropTable(pParse, pTab, iDb, isView);
81263
+
81264
+ /* Drop all triggers associated with the table being dropped. Code
81265
+ ** is generated to remove entries from sqlite_master and/or
81266
+ ** sqlite_temp_master if required.
81267
+ */
81268
+ pTrigger = sqlite3TriggerList(pParse, pTab);
81269
+ while( pTrigger ){
81270
+ assert( pTrigger->pSchema==pTab->pSchema ||
81271
+ pTrigger->pSchema==db->aDb[1].pSchema );
81272
+ sqlite3DropTriggerPtr(pParse, pTrigger);
81273
+ pTrigger = pTrigger->pNext;
81274
+ }
81275
+
81276
+#ifndef SQLITE_OMIT_AUTOINCREMENT
81277
+ /* Remove any entries of the sqlite_sequence table associated with
81278
+ ** the table being dropped. This is done before the table is dropped
81279
+ ** at the btree level, in case the sqlite_sequence table needs to
81280
+ ** move as a result of the drop (can happen in auto-vacuum mode).
81281
+ */
81282
+ if( pTab->tabFlags & TF_Autoincrement ){
81283
+ sqlite3NestedParse(pParse,
81284
+ "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81285
+ pDb->zName, pTab->zName
81286
+ );
81287
+ }
81288
+#endif
81289
+
81290
+ /* Drop all SQLITE_MASTER table and index entries that refer to the
81291
+ ** table. The program name loops through the master table and deletes
81292
+ ** every row that refers to a table of the same name as the one being
81293
+ ** dropped. Triggers are handled seperately because a trigger can be
81294
+ ** created in the temp database that refers to a table in another
81295
+ ** database.
81296
+ */
81297
+ sqlite3NestedParse(pParse,
81298
+ "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81299
+ pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
8011481300
sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81301
+ if( !isView && !IsVirtual(pTab) ){
81302
+ destroyTable(pParse, pTab);
81303
+ }
81304
+
81305
+ /* Remove the table entry from SQLite's internal schema and modify
81306
+ ** the schema cookie.
81307
+ */
81308
+ if( IsVirtual(pTab) ){
81309
+ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81310
+ }
81311
+ sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81312
+ sqlite3ChangeCookie(pParse, iDb);
8011581313
}
81314
+ sqliteViewResetAll(db, iDb);
8011681315
8011781316
exit_drop_table:
8011881317
sqlite3SrcListDelete(db, pName);
8011981318
}
8012081319
@@ -80278,11 +81477,13 @@
8027881477
*/
8027981478
static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
8028081479
Table *pTab = pIndex->pTable; /* The table that is indexed */
8028181480
int iTab = pParse->nTab++; /* Btree cursor used for pTab */
8028281481
int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81482
+ int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
8028381483
int addr1; /* Address of top of loop */
81484
+ int addr2; /* Address to jump to for next iteration */
8028481485
int tnum; /* Root page of index */
8028581486
Vdbe *v; /* Generate code into this virtual machine */
8028681487
KeyInfo *pKey; /* KeyInfo for index */
8028781488
int regIdxKey; /* Registers containing the index key */
8028881489
int regRecord; /* Register holding assemblied index record */
@@ -80311,14 +81512,45 @@
8031181512
sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
8031281513
(char *)pKey, P4_KEYINFO_HANDOFF);
8031381514
if( memRootPage>=0 ){
8031481515
sqlite3VdbeChangeP5(v, 1);
8031581516
}
81517
+
81518
+#ifndef SQLITE_OMIT_MERGE_SORT
81519
+ /* Open the sorter cursor if we are to use one. */
81520
+ iSorter = pParse->nTab++;
81521
+ sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81522
+#endif
81523
+
81524
+ /* Open the table. Loop through all rows of the table, inserting index
81525
+ ** records into the sorter. */
8031681526
sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
8031781527
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81528
+ addr2 = addr1 + 1;
8031881529
regRecord = sqlite3GetTempReg(pParse);
8031981530
regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81531
+
81532
+#ifndef SQLITE_OMIT_MERGE_SORT
81533
+ sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
81534
+ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81535
+ sqlite3VdbeJumpHere(v, addr1);
81536
+ addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
81537
+ if( pIndex->onError!=OE_None ){
81538
+ int j2 = sqlite3VdbeCurrentAddr(v) + 3;
81539
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
81540
+ addr2 = sqlite3VdbeCurrentAddr(v);
81541
+ sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
81542
+ sqlite3HaltConstraint(
81543
+ pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
81544
+ );
81545
+ }else{
81546
+ addr2 = sqlite3VdbeCurrentAddr(v);
81547
+ }
81548
+ sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
81549
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
81550
+ sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81551
+#else
8032081552
if( pIndex->onError!=OE_None ){
8032181553
const int regRowid = regIdxKey + pIndex->nColumn;
8032281554
const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
8032381555
void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
8032481556
@@ -80333,17 +81565,20 @@
8033381565
*/
8033481566
sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
8033581567
sqlite3HaltConstraint(
8033681568
pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
8033781569
}
80338
- sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
81570
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
8033981571
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81572
+#endif
8034081573
sqlite3ReleaseTempReg(pParse, regRecord);
80341
- sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81574
+ sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
8034281575
sqlite3VdbeJumpHere(v, addr1);
81576
+
8034381577
sqlite3VdbeAddOp1(v, OP_Close, iTab);
8034481578
sqlite3VdbeAddOp1(v, OP_Close, iIdx);
81579
+ sqlite3VdbeAddOp1(v, OP_Close, iSorter);
8034581580
}
8034681581
8034781582
/*
8034881583
** Create a new index for an SQL table. pName1.pName2 is the name of the index
8034981584
** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81792,24 @@
8055781792
*/
8055881793
nName = sqlite3Strlen30(zName);
8055981794
nCol = pList->nExpr;
8056081795
pIndex = sqlite3DbMallocZero(db,
8056181796
sizeof(Index) + /* Index structure */
80562
- sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
8056381797
sizeof(int)*nCol + /* Index.aiColumn */
81798
+ sizeof(int)*(nCol+1) + /* Index.aiRowEst */
8056481799
sizeof(char *)*nCol + /* Index.azColl */
8056581800
sizeof(u8)*nCol + /* Index.aSortOrder */
8056681801
nName + 1 + /* Index.zName */
8056781802
nExtra /* Collation sequence names */
8056881803
);
8056981804
if( db->mallocFailed ){
8057081805
goto exit_create_index;
8057181806
}
80572
- pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
80573
- pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
81807
+ pIndex->azColl = (char**)(&pIndex[1]);
8057481808
pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80575
- pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
81809
+ pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81810
+ pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
8057681811
pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
8057781812
zExtra = (char *)(&pIndex->zName[nName+1]);
8057881813
memcpy(pIndex->zName, zName, nName+1);
8057981814
pIndex->pTable = pTab;
8058081815
pIndex->nColumn = pList->nExpr;
@@ -80847,13 +82082,13 @@
8084782082
** Apart from that, we have little to go on besides intuition as to
8084882083
** how aiRowEst[] should be initialized. The numbers generated here
8084982084
** are based on typical values found in actual indices.
8085082085
*/
8085182086
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80852
- tRowcnt *a = pIdx->aiRowEst;
82087
+ unsigned *a = pIdx->aiRowEst;
8085382088
int i;
80854
- tRowcnt n;
82089
+ unsigned n;
8085582090
assert( a!=0 );
8085682091
a[0] = pIdx->pTable->nRowEst;
8085782092
if( a[0]<10 ) a[0] = 10;
8085882093
n = 10;
8085982094
for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82528,13 @@
8129382528
** The operator is "natural cross join". The A and B operands are stored
8129482529
** in p->a[0] and p->a[1], respectively. The parser initially stores the
8129582530
** operator with A. This routine shifts that operator over to B.
8129682531
*/
8129782532
SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
81298
- if( p && p->a ){
82533
+ if( p ){
8129982534
int i;
82535
+ assert( p->a || p->nSrc==0 );
8130082536
for(i=p->nSrc-1; i>0; i--){
8130182537
p->a[i].jointype = p->a[i-1].jointype;
8130282538
}
8130382539
p->a[0].jointype = 0;
8130482540
}
@@ -82850,10 +84086,12 @@
8285084086
**
8285184087
** There is only one exported symbol in this file - the function
8285284088
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
8285384089
** All other code has file scope.
8285484090
*/
84091
+/* #include <stdlib.h> */
84092
+/* #include <assert.h> */
8285584093
8285684094
/*
8285784095
** Return the collating function associated with a function.
8285884096
*/
8285984097
static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86411,28 @@
8517386411
pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
8517486412
}else{
8517586413
pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
8517686414
}
8517786415
if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
86416
+ assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
8517886417
if( !isIgnoreErrors || db->mallocFailed ) return;
86418
+ if( pTo==0 ){
86419
+ /* If isIgnoreErrors is true, then a table is being dropped. In this
86420
+ ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
86421
+ ** before actually dropping it in order to check FK constraints.
86422
+ ** If the parent table of an FK constraint on the current table is
86423
+ ** missing, behave as if it is empty. i.e. decrement the relevant
86424
+ ** FK counter for each row of the current table with non-NULL keys.
86425
+ */
86426
+ Vdbe *v = sqlite3GetVdbe(pParse);
86427
+ int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
86428
+ for(i=0; i<pFKey->nCol; i++){
86429
+ int iReg = pFKey->aCol[i].iFrom + regOld + 1;
86430
+ sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
86431
+ }
86432
+ sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
86433
+ }
8517986434
continue;
8518086435
}
8518186436
assert( pFKey->nCol==1 || (aiFree && pIdx) );
8518286437
8518386438
if( aiFree ){
@@ -88081,10 +89336,11 @@
8808189336
8808289337
#endif /* _SQLITE3EXT_H_ */
8808389338
8808489339
/************** End of sqlite3ext.h ******************************************/
8808589340
/************** Continuing where we left off in loadext.c ********************/
89341
+/* #include <string.h> */
8808689342
8808789343
#ifndef SQLITE_OMIT_LOAD_EXTENSION
8808889344
8808989345
/*
8809089346
** Some API routines are omitted when various features are
@@ -91527,16 +92783,22 @@
9152792783
){
9152892784
Vdbe *v = pParse->pVdbe;
9152992785
int nExpr = pOrderBy->nExpr;
9153092786
int regBase = sqlite3GetTempRange(pParse, nExpr+2);
9153192787
int regRecord = sqlite3GetTempReg(pParse);
92788
+ int op;
9153292789
sqlite3ExprCacheClear(pParse);
9153392790
sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
9153492791
sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
9153592792
sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
9153692793
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
91537
- sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
92794
+ if( pSelect->selFlags & SF_UseSorter ){
92795
+ op = OP_SorterInsert;
92796
+ }else{
92797
+ op = OP_IdxInsert;
92798
+ }
92799
+ sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
9153892800
sqlite3ReleaseTempReg(pParse, regRecord);
9153992801
sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
9154092802
if( pSelect->iLimit ){
9154192803
int addr1, addr2;
9154292804
int iLimit;
@@ -92001,13 +93263,24 @@
9200193263
sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
9200293264
regRowid = 0;
9200393265
}else{
9200493266
regRowid = sqlite3GetTempReg(pParse);
9200593267
}
92006
- addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
92007
- codeOffset(v, p, addrContinue);
92008
- sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
93268
+ if( p->selFlags & SF_UseSorter ){
93269
+ int regSortOut = ++pParse->nMem;
93270
+ int ptab2 = pParse->nTab++;
93271
+ sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
93272
+ addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
93273
+ codeOffset(v, p, addrContinue);
93274
+ sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
93275
+ sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
93276
+ sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
93277
+ }else{
93278
+ addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
93279
+ codeOffset(v, p, addrContinue);
93280
+ sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
93281
+ }
9200993282
switch( eDest ){
9201093283
case SRT_Table:
9201193284
case SRT_EphemTab: {
9201293285
testcase( eDest==SRT_Table );
9201393286
testcase( eDest==SRT_EphemTab );
@@ -92056,11 +93329,15 @@
9205693329
sqlite3ReleaseTempReg(pParse, regRowid);
9205793330
9205893331
/* The bottom of the loop
9205993332
*/
9206093333
sqlite3VdbeResolveLabel(v, addrContinue);
92061
- sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
93334
+ if( p->selFlags & SF_UseSorter ){
93335
+ sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
93336
+ }else{
93337
+ sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
93338
+ }
9206293339
sqlite3VdbeResolveLabel(v, addrBreak);
9206393340
if( eDest==SRT_Output || eDest==SRT_Coroutine ){
9206493341
sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
9206593342
}
9206693343
}
@@ -95022,10 +96299,14 @@
9502296299
/* Set the limiter.
9502396300
*/
9502496301
iEnd = sqlite3VdbeMakeLabel(v);
9502596302
p->nSelectRow = (double)LARGEST_INT64;
9502696303
computeLimitRegisters(pParse, p, iEnd);
96304
+ if( p->iLimit==0 && addrSortIndex>=0 ){
96305
+ sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
96306
+ p->selFlags |= SF_UseSorter;
96307
+ }
9502796308
9502896309
/* Open a virtual index to use for the distinct set.
9502996310
*/
9503096311
if( p->selFlags & SF_Distinct ){
9503196312
KeyInfo *pKeyInfo;
@@ -95057,11 +96338,11 @@
9505796338
}
9505896339
9505996340
if( pWInfo->eDistinct ){
9506096341
VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
9506196342
95062
- assert( addrDistinctIndex>0 );
96343
+ assert( addrDistinctIndex>=0 );
9506396344
pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
9506496345
9506596346
assert( isDistinct );
9506696347
assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
9506796348
|| pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
@@ -95116,10 +96397,12 @@
9511696397
** one row of the input to the aggregator has been
9511796398
** processed */
9511896399
int iAbortFlag; /* Mem address which causes query abort if positive */
9511996400
int groupBySort; /* Rows come from source in GROUP BY order */
9512096401
int addrEnd; /* End of processing for this SELECT */
96402
+ int sortPTab = 0; /* Pseudotable used to decode sorting results */
96403
+ int sortOut = 0; /* Output register from the sorter */
9512196404
9512296405
/* Remove any and all aliases between the result set and the
9512396406
** GROUP BY clause.
9512496407
*/
9512596408
if( pGroupBy ){
@@ -95177,16 +96460,16 @@
9517796460
int addrReset; /* Subroutine for resetting the accumulator */
9517896461
int regReset; /* Return address register for reset subroutine */
9517996462
9518096463
/* If there is a GROUP BY clause we might need a sorting index to
9518196464
** implement it. Allocate that sorting index now. If it turns out
95182
- ** that we do not need it after all, the OpenEphemeral instruction
96465
+ ** that we do not need it after all, the OP_SorterOpen instruction
9518396466
** will be converted into a Noop.
9518496467
*/
9518596468
sAggInfo.sortingIdx = pParse->nTab++;
9518696469
pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
95187
- addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
96470
+ addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
9518896471
sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
9518996472
0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
9519096473
9519196474
/* Initialize memory locations used by GROUP BY aggregate processing
9519296475
*/
@@ -95263,15 +96546,18 @@
9526396546
j++;
9526496547
}
9526596548
}
9526696549
regRecord = sqlite3GetTempReg(pParse);
9526796550
sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
95268
- sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
96551
+ sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
9526996552
sqlite3ReleaseTempReg(pParse, regRecord);
9527096553
sqlite3ReleaseTempRange(pParse, regBase, nCol);
9527196554
sqlite3WhereEnd(pWInfo);
95272
- sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
96555
+ sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
96556
+ sortOut = sqlite3GetTempReg(pParse);
96557
+ sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
96558
+ sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
9527396559
VdbeComment((v, "GROUP BY sort"));
9527496560
sAggInfo.useSortingIdx = 1;
9527596561
sqlite3ExprCacheClear(pParse);
9527696562
}
9527796563
@@ -95280,13 +96566,17 @@
9528096566
** Then compare the current GROUP BY terms against the GROUP BY terms
9528196567
** from the previous row currently stored in a0, a1, a2...
9528296568
*/
9528396569
addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
9528496570
sqlite3ExprCacheClear(pParse);
96571
+ if( groupBySort ){
96572
+ sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
96573
+ }
9528596574
for(j=0; j<pGroupBy->nExpr; j++){
9528696575
if( groupBySort ){
95287
- sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
96576
+ sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
96577
+ if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
9528896578
}else{
9528996579
sAggInfo.directMode = 1;
9529096580
sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
9529196581
}
9529296582
}
@@ -95321,11 +96611,11 @@
9532196611
VdbeComment((v, "indicate data in accumulator"));
9532296612
9532396613
/* End of the loop
9532496614
*/
9532596615
if( groupBySort ){
95326
- sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
96616
+ sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
9532796617
}else{
9532896618
sqlite3WhereEnd(pWInfo);
9532996619
sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
9533096620
}
9533196621
@@ -95650,10 +96940,12 @@
9565096940
** interface routine of sqlite3_exec().
9565196941
**
9565296942
** These routines are in a separate files so that they will not be linked
9565396943
** if they are not used.
9565496944
*/
96945
+/* #include <stdlib.h> */
96946
+/* #include <string.h> */
9565596947
9565696948
#ifndef SQLITE_OMIT_GET_TABLE
9565796949
9565896950
/*
9565996951
** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100451,11 @@
99159100451
#define TERM_CODED 0x04 /* This term is already coded */
99160100452
#define TERM_COPIED 0x08 /* Has a child */
99161100453
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
99162100454
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
99163100455
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
99164
-#ifdef SQLITE_ENABLE_STAT3
100456
+#ifdef SQLITE_ENABLE_STAT2
99165100457
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
99166100458
#else
99167100459
# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
99168100460
#endif
99169100461
@@ -100373,11 +101665,11 @@
100373101665
pNewTerm->prereqAll = pTerm->prereqAll;
100374101666
}
100375101667
}
100376101668
#endif /* SQLITE_OMIT_VIRTUALTABLE */
100377101669
100378
-#ifdef SQLITE_ENABLE_STAT3
101670
+#ifdef SQLITE_ENABLE_STAT2
100379101671
/* When sqlite_stat2 histogram data is available an operator of the
100380101672
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
100381101673
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
100382101674
** virtual term of that form.
100383101675
**
@@ -100412,11 +101704,11 @@
100412101704
pTerm->nChild = 1;
100413101705
pTerm->wtFlags |= TERM_COPIED;
100414101706
pNewTerm->prereqAll = pTerm->prereqAll;
100415101707
}
100416101708
}
100417
-#endif /* SQLITE_ENABLE_STAT */
101709
+#endif /* SQLITE_ENABLE_STAT2 */
100418101710
100419101711
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
100420101712
** an index for tables to the left of the join.
100421101713
*/
100422101714
pTerm->prereqRight |= extraRight;
@@ -101461,89 +102753,71 @@
101461102753
*/
101462102754
bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101463102755
}
101464102756
#endif /* SQLITE_OMIT_VIRTUALTABLE */
101465102757
101466
-#ifdef SQLITE_ENABLE_STAT3
101467102758
/*
101468
-** Estimate the location of a particular key among all keys in an
101469
-** index. Store the results in aStat as follows:
101470
-**
101471
-** aStat[0] Est. number of rows less than pVal
101472
-** aStat[1] Est. number of rows equal to pVal
101473
-**
101474
-** Return SQLITE_OK on success.
101475
-*/
101476
-static int whereKeyStats(
102759
+** Argument pIdx is a pointer to an index structure that has an array of
102760
+** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102761
+** stored in Index.aSample. These samples divide the domain of values stored
102762
+** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102763
+** Region 0 contains all values less than the first sample value. Region
102764
+** 1 contains values between the first and second samples. Region 2 contains
102765
+** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102766
+** contains values larger than the last sample.
102767
+**
102768
+** If the index contains many duplicates of a single value, then it is
102769
+** possible that two or more adjacent samples can hold the same value.
102770
+** When that is the case, the smallest possible region code is returned
102771
+** when roundUp is false and the largest possible region code is returned
102772
+** when roundUp is true.
102773
+**
102774
+** If successful, this function determines which of the regions value
102775
+** pVal lies in, sets *piRegion to the region index (a value between 0
102776
+** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102777
+** Or, if an OOM occurs while converting text values between encodings,
102778
+** SQLITE_NOMEM is returned and *piRegion is undefined.
102779
+*/
102780
+#ifdef SQLITE_ENABLE_STAT2
102781
+static int whereRangeRegion(
101477102782
Parse *pParse, /* Database connection */
101478102783
Index *pIdx, /* Index to consider domain of */
101479102784
sqlite3_value *pVal, /* Value to consider */
101480
- int roundUp, /* Round up if true. Round down if false */
101481
- tRowcnt *aStat /* OUT: stats written here */
102785
+ int roundUp, /* Return largest valid region if true */
102786
+ int *piRegion /* OUT: Region of domain in which value lies */
101482102787
){
101483
- tRowcnt n;
101484
- IndexSample *aSample;
101485
- int i, eType;
101486
- int isEq = 0;
101487
- i64 v;
101488
- double r, rS;
101489
-
101490102788
assert( roundUp==0 || roundUp==1 );
101491
- if( pVal==0 ) return SQLITE_ERROR;
101492
- n = pIdx->aiRowEst[0];
101493
- aSample = pIdx->aSample;
101494
- i = 0;
101495
- eType = sqlite3_value_type(pVal);
101496
-
101497
- if( eType==SQLITE_INTEGER ){
101498
- v = sqlite3_value_int64(pVal);
101499
- r = (i64)v;
101500
- for(i=0; i<pIdx->nSample; i++){
101501
- if( aSample[i].eType==SQLITE_NULL ) continue;
101502
- if( aSample[i].eType>=SQLITE_TEXT ) break;
101503
- if( aSample[i].eType==SQLITE_INTEGER ){
101504
- if( aSample[i].u.i>=v ){
101505
- isEq = aSample[i].u.i==v;
101506
- break;
101507
- }
101508
- }else{
101509
- assert( aSample[i].eType==SQLITE_FLOAT );
101510
- if( aSample[i].u.r>=r ){
101511
- isEq = aSample[i].u.r==r;
101512
- break;
101513
- }
101514
- }
101515
- }
101516
- }else if( eType==SQLITE_FLOAT ){
101517
- r = sqlite3_value_double(pVal);
101518
- for(i=0; i<pIdx->nSample; i++){
101519
- if( aSample[i].eType==SQLITE_NULL ) continue;
101520
- if( aSample[i].eType>=SQLITE_TEXT ) break;
101521
- if( aSample[i].eType==SQLITE_FLOAT ){
101522
- rS = aSample[i].u.r;
101523
- }else{
101524
- rS = aSample[i].u.i;
101525
- }
101526
- if( rS>=r ){
101527
- isEq = rS==r;
101528
- break;
101529
- }
101530
- }
101531
- }else if( eType==SQLITE_NULL ){
101532
- i = 0;
101533
- if( pIdx->nSample>=1 && aSample[0].eType==SQLITE_NULL ) isEq = 1;
101534
- }else{
101535
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
101536
- for(i=0; i<pIdx->nSample; i++){
101537
- if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
101538
- break;
101539
- }
101540
- }
101541
- if( i<pIdx->nSample ){
102789
+ if( ALWAYS(pVal) ){
102790
+ IndexSample *aSample = pIdx->aSample;
102791
+ int i = 0;
102792
+ int eType = sqlite3_value_type(pVal);
102793
+
102794
+ if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102795
+ double r = sqlite3_value_double(pVal);
102796
+ for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102797
+ if( aSample[i].eType==SQLITE_NULL ) continue;
102798
+ if( aSample[i].eType>=SQLITE_TEXT ) break;
102799
+ if( roundUp ){
102800
+ if( aSample[i].u.r>r ) break;
102801
+ }else{
102802
+ if( aSample[i].u.r>=r ) break;
102803
+ }
102804
+ }
102805
+ }else if( eType==SQLITE_NULL ){
102806
+ i = 0;
102807
+ if( roundUp ){
102808
+ while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102809
+ }
102810
+ }else{
101542102811
sqlite3 *db = pParse->db;
101543102812
CollSeq *pColl;
101544102813
const u8 *z;
102814
+ int n;
102815
+
102816
+ /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102817
+ assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102818
+
101545102819
if( eType==SQLITE_BLOB ){
101546102820
z = (const u8 *)sqlite3_value_blob(pVal);
101547102821
pColl = db->pDfltColl;
101548102822
assert( pColl->enc==SQLITE_UTF8 );
101549102823
}else{
@@ -101558,16 +102832,16 @@
101558102832
return SQLITE_NOMEM;
101559102833
}
101560102834
assert( z && pColl && pColl->xCmp );
101561102835
}
101562102836
n = sqlite3ValueBytes(pVal, pColl->enc);
101563
-
101564
- for(; i<pIdx->nSample; i++){
102837
+
102838
+ for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
101565102839
int c;
101566102840
int eSampletype = aSample[i].eType;
101567
- if( eSampletype<eType ) continue;
101568
- if( eSampletype!=eType ) break;
102841
+ if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102842
+ if( (eSampletype!=eType) ) break;
101569102843
#ifndef SQLITE_OMIT_UTF16
101570102844
if( pColl->enc!=SQLITE_UTF8 ){
101571102845
int nSample;
101572102846
char *zSample = sqlite3Utf8to16(
101573102847
db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102855,20 @@
101581102855
}else
101582102856
#endif
101583102857
{
101584102858
c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101585102859
}
101586
- if( c>=0 ){
101587
- if( c==0 ) isEq = 1;
101588
- break;
101589
- }
101590
- }
101591
- }
101592
- }
101593
-
101594
- /* At this point, aSample[i] is the first sample that is greater than
101595
- ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
101596
- ** than pVal. If aSample[i]==pVal, then isEq==1.
101597
- */
101598
- if( isEq ){
101599
- assert( i<pIdx->nSample );
101600
- aStat[0] = aSample[i].nLt;
101601
- aStat[1] = aSample[i].nEq;
101602
- }else{
101603
- tRowcnt iLower, iUpper, iGap;
101604
- if( i==0 ){
101605
- iLower = 0;
101606
- iUpper = aSample[0].nLt;
101607
- }else{
101608
- iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
101609
- iLower = aSample[i-1].nEq + aSample[i-1].nLt;
101610
- }
101611
- aStat[1] = pIdx->avgEq;
101612
- if( iLower>=iUpper ){
101613
- iGap = 0;
101614
- }else{
101615
- iGap = iUpper - iLower;
101616
- if( iGap>=aStat[1]/2 ) iGap -= aStat[1]/2;
101617
- }
101618
- if( roundUp ){
101619
- iGap = (iGap*2)/3;
101620
- }else{
101621
- iGap = iGap/3;
101622
- }
101623
- aStat[0] = iLower + iGap;
102860
+ if( c-roundUp>=0 ) break;
102861
+ }
102862
+ }
102863
+
102864
+ assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102865
+ *piRegion = i;
101624102866
}
101625102867
return SQLITE_OK;
101626102868
}
101627
-#endif /* SQLITE_ENABLE_STAT3 */
102869
+#endif /* #ifdef SQLITE_ENABLE_STAT2 */
101628102870
101629102871
/*
101630102872
** If expression pExpr represents a literal value, set *pp to point to
101631102873
** an sqlite3_value structure containing the same value, with affinity
101632102874
** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102882,11 @@
101640102882
**
101641102883
** If neither of the above apply, set *pp to NULL.
101642102884
**
101643102885
** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101644102886
*/
101645
-#ifdef SQLITE_ENABLE_STAT3
102887
+#ifdef SQLITE_ENABLE_STAT2
101646102888
static int valueFromExpr(
101647102889
Parse *pParse,
101648102890
Expr *pExpr,
101649102891
u8 aff,
101650102892
sqlite3_value **pp
@@ -101688,92 +102930,106 @@
101688102930
**
101689102931
** ... FROM t1 WHERE a > ? AND a < ? ...
101690102932
**
101691102933
** then nEq should be passed 0.
101692102934
**
101693
-** The returned value is an integer divisor to reduce the estimated
101694
-** search space. A return value of 1 means that range constraints are
101695
-** no help at all. A return value of 2 means range constraints are
101696
-** expected to reduce the search space by half. And so forth...
102935
+** The returned value is an integer between 1 and 100, inclusive. A return
102936
+** value of 1 indicates that the proposed range scan is expected to visit
102937
+** approximately 1/100th (1%) of the rows selected by the nEq equality
102938
+** constraints (if any). A return value of 100 indicates that it is expected
102939
+** that the range scan will visit every row (100%) selected by the equality
102940
+** constraints.
101697102941
**
101698
-** In the absence of sqlite_stat3 ANALYZE data, each range inequality
101699
-** reduces the search space by a factor of 4. Hence a single constraint (x>?)
101700
-** results in a return of 4 and a range constraint (x>? AND x<?) results
101701
-** in a return of 16.
102942
+** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102943
+** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102944
+** results in a return of 25 and a range constraint (x>? AND x<?) results
102945
+** in a return of 6.
101702102946
*/
101703102947
static int whereRangeScanEst(
101704102948
Parse *pParse, /* Parsing & code generating context */
101705102949
Index *p, /* The index containing the range-compared column; "x" */
101706102950
int nEq, /* index into p->aCol[] of the range-compared column */
101707102951
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101708102952
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101709
- double *pRangeDiv /* OUT: Reduce search space by this divisor */
102953
+ int *piEst /* OUT: Return value */
101710102954
){
101711102955
int rc = SQLITE_OK;
101712102956
101713
-#ifdef SQLITE_ENABLE_STAT3
102957
+#ifdef SQLITE_ENABLE_STAT2
101714102958
101715
- if( nEq==0 && p->nSample ){
101716
- sqlite3_value *pRangeVal;
101717
- tRowcnt iLower = 0;
101718
- tRowcnt iUpper = p->aiRowEst[0];
101719
- tRowcnt a[2];
102959
+ if( nEq==0 && p->aSample ){
102960
+ sqlite3_value *pLowerVal = 0;
102961
+ sqlite3_value *pUpperVal = 0;
102962
+ int iEst;
102963
+ int iLower = 0;
102964
+ int iUpper = SQLITE_INDEX_SAMPLES;
102965
+ int roundUpUpper = 0;
102966
+ int roundUpLower = 0;
101720102967
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101721102968
101722102969
if( pLower ){
101723102970
Expr *pExpr = pLower->pExpr->pRight;
101724
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102971
+ rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
101725102972
assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
101726
- if( rc==SQLITE_OK
101727
- && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
101728
- ){
101729
- iLower = a[0];
101730
- if( pLower->eOperator==WO_GT ) iLower += a[1];
101731
- }
101732
- sqlite3ValueFree(pRangeVal);
102973
+ roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
101733102974
}
101734102975
if( rc==SQLITE_OK && pUpper ){
101735102976
Expr *pExpr = pUpper->pExpr->pRight;
101736
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102977
+ rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
101737102978
assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
101738
- if( rc==SQLITE_OK
101739
- && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
101740
- ){
101741
- iUpper = a[0];
101742
- if( pUpper->eOperator==WO_LE ) iUpper += a[1];
101743
- }
101744
- sqlite3ValueFree(pRangeVal);
101745
- }
101746
- if( rc==SQLITE_OK ){
101747
- if( iUpper<=iLower ){
101748
- *pRangeDiv = (double)p->aiRowEst[0];
101749
- }else{
101750
- *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
101751
- }
101752
- WHERETRACE(("range scan regions: %u..%u div=%g\n",
101753
- (u32)iLower, (u32)iUpper, *pRangeDiv));
101754
- return SQLITE_OK;
101755
- }
101756
- }
102979
+ roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102980
+ }
102981
+
102982
+ if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102983
+ sqlite3ValueFree(pLowerVal);
102984
+ sqlite3ValueFree(pUpperVal);
102985
+ goto range_est_fallback;
102986
+ }else if( pLowerVal==0 ){
102987
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102988
+ if( pLower ) iLower = iUpper/2;
102989
+ }else if( pUpperVal==0 ){
102990
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102991
+ if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
102992
+ }else{
102993
+ rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102994
+ if( rc==SQLITE_OK ){
102995
+ rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102996
+ }
102997
+ }
102998
+ WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
102999
+
103000
+ iEst = iUpper - iLower;
103001
+ testcase( iEst==SQLITE_INDEX_SAMPLES );
103002
+ assert( iEst<=SQLITE_INDEX_SAMPLES );
103003
+ if( iEst<1 ){
103004
+ *piEst = 50/SQLITE_INDEX_SAMPLES;
103005
+ }else{
103006
+ *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103007
+ }
103008
+ sqlite3ValueFree(pLowerVal);
103009
+ sqlite3ValueFree(pUpperVal);
103010
+ return rc;
103011
+ }
103012
+range_est_fallback:
101757103013
#else
101758103014
UNUSED_PARAMETER(pParse);
101759103015
UNUSED_PARAMETER(p);
101760103016
UNUSED_PARAMETER(nEq);
101761103017
#endif
101762103018
assert( pLower || pUpper );
101763
- *pRangeDiv = (double)1;
101764
- if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
101765
- if( pUpper ) *pRangeDiv *= (double)4;
103019
+ *piEst = 100;
103020
+ if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103021
+ if( pUpper ) *piEst /= 4;
101766103022
return rc;
101767103023
}
101768103024
101769
-#ifdef SQLITE_ENABLE_STAT3
103025
+#ifdef SQLITE_ENABLE_STAT2
101770103026
/*
101771103027
** Estimate the number of rows that will be returned based on
101772103028
** an equality constraint x=VALUE and where that VALUE occurs in
101773103029
** the histogram data. This only works when x is the left-most
101774
-** column of an index and sqlite_stat3 histogram data is available
103030
+** column of an index and sqlite_stat2 histogram data is available
101775103031
** for that index. When pExpr==NULL that means the constraint is
101776103032
** "x IS NULL" instead of "x=VALUE".
101777103033
**
101778103034
** Write the estimated row count into *pnRow and return SQLITE_OK.
101779103035
** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +103045,14 @@
101789103045
Index *p, /* The index whose left-most column is pTerm */
101790103046
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101791103047
double *pnRow /* Write the revised row estimate here */
101792103048
){
101793103049
sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
103050
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
101794103051
u8 aff; /* Column affinity */
101795103052
int rc; /* Subfunction return code */
101796
- tRowcnt a[2]; /* Statistics */
103053
+ double nRowEst; /* New estimate of the number of rows */
101797103054
101798103055
assert( p->aSample!=0 );
101799103056
aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101800103057
if( pExpr ){
101801103058
rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +103059,30 @@
101802103059
if( rc ) goto whereEqualScanEst_cancel;
101803103060
}else{
101804103061
pRhs = sqlite3ValueNew(pParse->db);
101805103062
}
101806103063
if( pRhs==0 ) return SQLITE_NOTFOUND;
101807
- rc = whereKeyStats(pParse, p, pRhs, 0, a);
101808
- if( rc==SQLITE_OK ){
101809
- WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
101810
- *pnRow = a[1];
103064
+ rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103065
+ if( rc ) goto whereEqualScanEst_cancel;
103066
+ rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103067
+ if( rc ) goto whereEqualScanEst_cancel;
103068
+ WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103069
+ if( iLower>=iUpper ){
103070
+ nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103071
+ if( nRowEst<*pnRow ) *pnRow = nRowEst;
103072
+ }else{
103073
+ nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103074
+ *pnRow = nRowEst;
101811103075
}
103076
+
101812103077
whereEqualScanEst_cancel:
101813103078
sqlite3ValueFree(pRhs);
101814103079
return rc;
101815103080
}
101816
-#endif /* defined(SQLITE_ENABLE_STAT3) */
103081
+#endif /* defined(SQLITE_ENABLE_STAT2) */
101817103082
101818
-#ifdef SQLITE_ENABLE_STAT3
103083
+#ifdef SQLITE_ENABLE_STAT2
101819103084
/*
101820103085
** Estimate the number of rows that will be returned based on
101821103086
** an IN constraint where the right-hand side of the IN operator
101822103087
** is a list of values. Example:
101823103088
**
@@ -101836,29 +103101,64 @@
101836103101
Parse *pParse, /* Parsing & code generating context */
101837103102
Index *p, /* The index whose left-most column is pTerm */
101838103103
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101839103104
double *pnRow /* Write the revised row estimate here */
101840103105
){
101841
- int rc = SQLITE_OK; /* Subfunction return code */
101842
- double nEst; /* Number of rows for a single term */
101843
- double nRowEst = (double)0; /* New estimate of the number of rows */
101844
- int i; /* Loop counter */
103106
+ sqlite3_value *pVal = 0; /* One value from list */
103107
+ int iLower, iUpper; /* Range of histogram regions containing pRhs */
103108
+ u8 aff; /* Column affinity */
103109
+ int rc = SQLITE_OK; /* Subfunction return code */
103110
+ double nRowEst; /* New estimate of the number of rows */
103111
+ int nSpan = 0; /* Number of histogram regions spanned */
103112
+ int nSingle = 0; /* Histogram regions hit by a single value */
103113
+ int nNotFound = 0; /* Count of values that are not constants */
103114
+ int i; /* Loop counter */
103115
+ u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
103116
+ u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
101845103117
101846103118
assert( p->aSample!=0 );
101847
- for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
101848
- nEst = p->aiRowEst[0];
101849
- rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
101850
- nRowEst += nEst;
103119
+ aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103120
+ memset(aSpan, 0, sizeof(aSpan));
103121
+ memset(aSingle, 0, sizeof(aSingle));
103122
+ for(i=0; i<pList->nExpr; i++){
103123
+ sqlite3ValueFree(pVal);
103124
+ rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103125
+ if( rc ) break;
103126
+ if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103127
+ nNotFound++;
103128
+ continue;
103129
+ }
103130
+ rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103131
+ if( rc ) break;
103132
+ rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103133
+ if( rc ) break;
103134
+ if( iLower>=iUpper ){
103135
+ aSingle[iLower] = 1;
103136
+ }else{
103137
+ assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103138
+ while( iLower<iUpper ) aSpan[iLower++] = 1;
103139
+ }
101851103140
}
101852103141
if( rc==SQLITE_OK ){
103142
+ for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103143
+ if( aSpan[i] ){
103144
+ nSpan++;
103145
+ }else if( aSingle[i] ){
103146
+ nSingle++;
103147
+ }
103148
+ }
103149
+ nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103150
+ + nNotFound*p->aiRowEst[1];
101853103151
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101854103152
*pnRow = nRowEst;
101855
- WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
103153
+ WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103154
+ nSpan, nSingle, nNotFound, nRowEst));
101856103155
}
103156
+ sqlite3ValueFree(pVal);
101857103157
return rc;
101858103158
}
101859
-#endif /* defined(SQLITE_ENABLE_STAT3) */
103159
+#endif /* defined(SQLITE_ENABLE_STAT2) */
101860103160
101861103161
101862103162
/*
101863103163
** Find the best query plan for accessing a particular table. Write the
101864103164
** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +103201,11 @@
101901103201
Index *pProbe; /* An index we are evaluating */
101902103202
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101903103203
int eqTermMask; /* Current mask of valid equality operators */
101904103204
int idxEqTermMask; /* Index mask of valid equality operators */
101905103205
Index sPk; /* A fake index object for the primary key */
101906
- tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103206
+ unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101907103207
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101908103208
int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101909103209
101910103210
/* Initialize the cost to a worst-case value */
101911103211
memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +103256,11 @@
101956103256
}
101957103257
101958103258
/* Loop over all indices looking for the best one to use
101959103259
*/
101960103260
for(; pProbe; pIdx=pProbe=pProbe->pNext){
101961
- const tRowcnt * const aiRowEst = pProbe->aiRowEst;
103261
+ const unsigned int * const aiRowEst = pProbe->aiRowEst;
101962103262
double cost; /* Cost of using pProbe */
101963103263
double nRow; /* Estimated number of rows in result set */
101964103264
double log10N; /* base-10 logarithm of nRow (inexact) */
101965103265
int rev; /* True to scan in reverse order */
101966103266
int wsFlags = 0;
@@ -101999,16 +103299,18 @@
101999103299
** Set to true if there was at least one "x IN (SELECT ...)" term used
102000103300
** in determining the value of nInMul. Note that the RHS of the
102001103301
** IN operator must be a SELECT, not a value list, for this variable
102002103302
** to be true.
102003103303
**
102004
- ** rangeDiv:
102005
- ** An estimate of a divisor by which to reduce the search space due
102006
- ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
102007
- ** data, a single inequality reduces the search space to 1/4rd its
102008
- ** original size (rangeDiv==4). Two inequalities reduce the search
102009
- ** space to 1/16th of its original size (rangeDiv==16).
103304
+ ** estBound:
103305
+ ** An estimate on the amount of the table that must be searched. A
103306
+ ** value of 100 means the entire table is searched. Range constraints
103307
+ ** might reduce this to a value less than 100 to indicate that only
103308
+ ** a fraction of the table needs searching. In the absence of
103309
+ ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103310
+ ** space to 1/4rd its original size. So an x>? constraint reduces
103311
+ ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
102010103312
**
102011103313
** bSort:
102012103314
** Boolean. True if there is an ORDER BY clause that will require an
102013103315
** external sort (i.e. scanning the index being evaluated will not
102014103316
** correctly order records).
@@ -102029,17 +103331,17 @@
102029103331
** SELECT a, b, c FROM tbl WHERE a = 1;
102030103332
*/
102031103333
int nEq; /* Number of == or IN terms matching index */
102032103334
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
102033103335
int nInMul = 1; /* Number of distinct equalities to lookup */
102034
- double rangeDiv = (double)1; /* Estimated reduction in search space */
103336
+ int estBound = 100; /* Estimated reduction in search space */
102035103337
int nBound = 0; /* Number of range constraints seen */
102036103338
int bSort = !!pOrderBy; /* True if external sort required */
102037103339
int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
102038103340
int bLookup = 0; /* True if not a covering index */
102039103341
WhereTerm *pTerm; /* A single term of the WHERE clause */
102040
-#ifdef SQLITE_ENABLE_STAT3
103342
+#ifdef SQLITE_ENABLE_STAT2
102041103343
WhereTerm *pFirstTerm = 0; /* First term matching the index */
102042103344
#endif
102043103345
102044103346
/* Determine the values of nEq and nInMul */
102045103347
for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103361,23 @@
102059103361
nInMul *= pExpr->x.pList->nExpr;
102060103362
}
102061103363
}else if( pTerm->eOperator & WO_ISNULL ){
102062103364
wsFlags |= WHERE_COLUMN_NULL;
102063103365
}
102064
-#ifdef SQLITE_ENABLE_STAT3
103366
+#ifdef SQLITE_ENABLE_STAT2
102065103367
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
102066103368
#endif
102067103369
used |= pTerm->prereqRight;
102068103370
}
102069103371
102070
- /* Determine the value of rangeDiv */
103372
+ /* Determine the value of estBound. */
102071103373
if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
102072103374
int j = pProbe->aiColumn[nEq];
102073103375
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
102074103376
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
102075103377
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
102076
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103378
+ whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
102077103379
if( pTop ){
102078103380
nBound = 1;
102079103381
wsFlags |= WHERE_TOP_LIMIT;
102080103382
used |= pTop->prereqRight;
102081103383
}
@@ -102143,11 +103445,11 @@
102143103445
if( bInEst && nRow*2>aiRowEst[0] ){
102144103446
nRow = aiRowEst[0]/2;
102145103447
nInMul = (int)(nRow / aiRowEst[nEq]);
102146103448
}
102147103449
102148
-#ifdef SQLITE_ENABLE_STAT3
103450
+#ifdef SQLITE_ENABLE_STAT2
102149103451
/* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
102150103452
** and we do not think that values of x are unique and if histogram
102151103453
** data is available for column x, then it might be possible
102152103454
** to get a better estimate on the number of rows based on
102153103455
** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103461,16 @@
102159103461
whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
102160103462
}else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
102161103463
whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
102162103464
}
102163103465
}
102164
-#endif /* SQLITE_ENABLE_STAT3 */
103466
+#endif /* SQLITE_ENABLE_STAT2 */
102165103467
102166103468
/* Adjust the number of output rows and downward to reflect rows
102167103469
** that are excluded by range constraints.
102168103470
*/
102169
- nRow = nRow/rangeDiv;
103471
+ nRow = (nRow * (double)estBound) / (double)100;
102170103472
if( nRow<1 ) nRow = 1;
102171103473
102172103474
/* Experiments run on real SQLite databases show that the time needed
102173103475
** to do a binary search to locate a row in a table or index is roughly
102174103476
** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103595,14 @@
102293103595
if( nRow<2 ) nRow = 2;
102294103596
}
102295103597
102296103598
102297103599
WHERETRACE((
102298
- "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103600
+ "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
102299103601
" notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
102300103602
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
102301
- nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
103603
+ nEq, nInMul, estBound, bSort, bLookup, wsFlags,
102302103604
notReady, log10N, nRow, cost, used
102303103605
));
102304103606
102305103607
/* If this index is the best we have seen so far, then record this
102306103608
** index and its cost in the pCost structure.
@@ -104227,10 +105529,11 @@
104227105529
** LALR(1) grammar but which are always false in the
104228105530
** specific grammar used by SQLite.
104229105531
*/
104230105532
/* First off, code is included that follows the "include" declaration
104231105533
** in the input grammar file. */
105534
+/* #include <stdio.h> */
104232105535
104233105536
104234105537
/*
104235105538
** Disable all error recovery processing in the parser push-down
104236105539
** automaton.
@@ -105087,10 +106390,11 @@
105087106390
#endif
105088106391
};
105089106392
typedef struct yyParser yyParser;
105090106393
105091106394
#ifndef NDEBUG
106395
+/* #include <stdio.h> */
105092106396
static FILE *yyTraceFILE = 0;
105093106397
static char *yyTracePrompt = 0;
105094106398
#endif /* NDEBUG */
105095106399
105096106400
#ifndef NDEBUG
@@ -107662,10 +108966,11 @@
107662108966
**
107663108967
** This file contains C code that splits an SQL input string up into
107664108968
** individual tokens and sends those tokens one-by-one over to the
107665108969
** parser for analysis.
107666108970
*/
108971
+/* #include <stdlib.h> */
107667108972
107668108973
/*
107669108974
** The charMap() macro maps alphabetic characters into their
107670108975
** lower-case ASCII equivalent. On ASCII machines, this is just
107671108976
** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110358,20 @@
109053110358
memcpy(&y, &x, 8);
109054110359
assert( sqlite3IsNaN(y) );
109055110360
}
109056110361
#endif
109057110362
#endif
110363
+
110364
+ /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110365
+ ** compile-time option.
110366
+ */
110367
+#ifdef SQLITE_EXTRA_INIT
110368
+ if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110369
+ int SQLITE_EXTRA_INIT(void);
110370
+ rc = SQLITE_EXTRA_INIT();
110371
+ }
110372
+#endif
109058110373
109059110374
return rc;
109060110375
}
109061110376
109062110377
/*
@@ -113184,10 +114499,16 @@
113184114499
113185114500
#if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
113186114501
# define SQLITE_CORE 1
113187114502
#endif
113188114503
114504
+/* #include <assert.h> */
114505
+/* #include <stdlib.h> */
114506
+/* #include <stddef.h> */
114507
+/* #include <stdio.h> */
114508
+/* #include <string.h> */
114509
+/* #include <stdarg.h> */
113189114510
113190114511
#ifndef SQLITE_CORE
113191114512
SQLITE_EXTENSION_INIT1
113192114513
#endif
113193114514
@@ -117705,10 +119026,12 @@
117705119026
******************************************************************************
117706119027
**
117707119028
*/
117708119029
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117709119030
119031
+/* #include <string.h> */
119032
+/* #include <assert.h> */
117710119033
117711119034
typedef struct Fts3auxTable Fts3auxTable;
117712119035
typedef struct Fts3auxCursor Fts3auxCursor;
117713119036
117714119037
struct Fts3auxTable {
@@ -118243,10 +119566,12 @@
118243119566
/*
118244119567
** Default span for NEAR operators.
118245119568
*/
118246119569
#define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
118247119570
119571
+/* #include <string.h> */
119572
+/* #include <assert.h> */
118248119573
118249119574
/*
118250119575
** isNot:
118251119576
** This variable is used by function getNextNode(). When getNextNode() is
118252119577
** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +120269,11 @@
118944120269
** Everything after this point is just test code.
118945120270
*/
118946120271
118947120272
#ifdef SQLITE_TEST
118948120273
120274
+/* #include <stdio.h> */
118949120275
118950120276
/*
118951120277
** Function to query the hash-table of tokenizers (see README.tokenizers).
118952120278
*/
118953120279
static int queryTestTokenizer(
@@ -119154,10 +120480,13 @@
119154120480
** * The FTS3 module is being built into the core of
119155120481
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119156120482
*/
119157120483
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119158120484
120485
+/* #include <assert.h> */
120486
+/* #include <stdlib.h> */
120487
+/* #include <string.h> */
119159120488
119160120489
119161120490
/*
119162120491
** Malloc and Free functions
119163120492
*/
@@ -119534,10 +120863,14 @@
119534120863
** * The FTS3 module is being built into the core of
119535120864
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119536120865
*/
119537120866
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119538120867
120868
+/* #include <assert.h> */
120869
+/* #include <stdlib.h> */
120870
+/* #include <stdio.h> */
120871
+/* #include <string.h> */
119539120872
119540120873
119541120874
/*
119542120875
** Class derived from sqlite3_tokenizer
119543120876
*/
@@ -120177,10 +121510,12 @@
120177121510
** * The FTS3 module is being built into the core of
120178121511
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120179121512
*/
120180121513
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120181121514
121515
+/* #include <assert.h> */
121516
+/* #include <string.h> */
120182121517
120183121518
/*
120184121519
** Implementation of the SQL scalar function for accessing the underlying
120185121520
** hash table. This function may be called as follows:
120186121521
**
@@ -120352,10 +121687,12 @@
120352121687
}
120353121688
120354121689
120355121690
#ifdef SQLITE_TEST
120356121691
121692
+/* #include <tcl.h> */
121693
+/* #include <string.h> */
120357121694
120358121695
/*
120359121696
** Implementation of a special SQL scalar function for testing tokenizers
120360121697
** designed to be used in concert with the Tcl testing framework. This
120361121698
** function must be called with two arguments:
@@ -120663,10 +122000,14 @@
120663122000
** * The FTS3 module is being built into the core of
120664122001
** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120665122002
*/
120666122003
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120667122004
122005
+/* #include <assert.h> */
122006
+/* #include <stdlib.h> */
122007
+/* #include <stdio.h> */
122008
+/* #include <string.h> */
120668122009
120669122010
120670122011
typedef struct simple_tokenizer {
120671122012
sqlite3_tokenizer base;
120672122013
char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +122229,13 @@
120888122229
** code in fts3.c.
120889122230
*/
120890122231
120891122232
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120892122233
122234
+/* #include <string.h> */
122235
+/* #include <assert.h> */
122236
+/* #include <stdlib.h> */
120893122237
120894122238
/*
120895122239
** When full-text index nodes are loaded from disk, the buffer that they
120896122240
** are loaded into has the following number of bytes of padding at the end
120897122241
** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125493,12 @@
124149125493
******************************************************************************
124150125494
*/
124151125495
124152125496
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124153125497
125498
+/* #include <string.h> */
125499
+/* #include <assert.h> */
124154125500
124155125501
/*
124156125502
** Characters that may appear in the second argument to matchinfo().
124157125503
*/
124158125504
#define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +127082,12 @@
125736127082
#ifndef SQLITE_CORE
125737127083
SQLITE_EXTENSION_INIT1
125738127084
#else
125739127085
#endif
125740127086
127087
+/* #include <string.h> */
127088
+/* #include <assert.h> */
125741127089
125742127090
#ifndef SQLITE_AMALGAMATION
125743127091
#include "sqlite3rtree.h"
125744127092
typedef sqlite3_int64 i64;
125745127093
typedef unsigned char u8;
@@ -128950,10 +130298,11 @@
128950130298
#include <unicode/utypes.h>
128951130299
#include <unicode/uregex.h>
128952130300
#include <unicode/ustring.h>
128953130301
#include <unicode/ucol.h>
128954130302
130303
+/* #include <assert.h> */
128955130304
128956130305
#ifndef SQLITE_CORE
128957130306
SQLITE_EXTENSION_INIT1
128958130307
#else
128959130308
#endif
@@ -129429,12 +130778,16 @@
129429130778
** This file implements a tokenizer for fts3 based on the ICU library.
129430130779
*/
129431130780
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
129432130781
#ifdef SQLITE_ENABLE_ICU
129433130782
130783
+/* #include <assert.h> */
130784
+/* #include <string.h> */
129434130785
129435130786
#include <unicode/ubrk.h>
130787
+/* #include <unicode/ucol.h> */
130788
+/* #include <unicode/ustring.h> */
129436130789
#include <unicode/utf16.h>
129437130790
129438130791
typedef struct IcuTokenizer IcuTokenizer;
129439130792
typedef struct IcuCursor IcuCursor;
129440130793
129441130794
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385 /*
386 ** Exactly one of the following macros must be defined in order to
387 ** specify which memory allocation subsystem to use.
388 **
389 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
 
390 ** SQLITE_MEMDEBUG // Debugging version of system malloc()
 
 
 
 
 
391 **
392 ** (Historical note: There used to be several other options, but we've
393 ** pared it down to just these two.)
394 **
395 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
396 ** the default.
397 */
398 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)>1
399 # error "At most one of the following compile-time configuration options\
400 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_MEMDEBUG"
401 #endif
402 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_MEMDEBUG)==0
403 # define SQLITE_SYSTEM_MALLOC 1
404 #endif
405
406 /*
407 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
650 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
651 ** [sqlite_version()] and [sqlite_source_id()].
652 */
653 #define SQLITE_VERSION "3.7.8"
654 #define SQLITE_VERSION_NUMBER 3007008
655 #define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
656
657 /*
658 ** CAPI3REF: Run-Time Library Version Numbers
659 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
660 **
@@ -1751,20 +1757,14 @@
1751 ** also used during testing of SQLite in order to specify an alternative
1752 ** memory allocator that simulates memory out-of-memory conditions in
1753 ** order to verify that SQLite recovers gracefully from such
1754 ** conditions.
1755 **
1756 ** The xMalloc and xFree methods must work like the
1757 ** malloc() and free() functions from the standard C library.
1758 ** The xRealloc method must work like realloc() from the standard C library
1759 ** with the exception that if the second argument to xRealloc is zero,
1760 ** xRealloc must be a no-op - it must not perform any allocation or
1761 ** deallocation. ^SQLite guarantees that the second argument to
1762 ** xRealloc is always a value returned by a prior call to xRoundup.
1763 ** And so in cases where xRoundup always returns a positive number,
1764 ** xRealloc can perform exactly as the standard library realloc() and
1765 ** still be in compliance with this specification.
1766 **
1767 ** xSize should return the allocated size of a memory allocation
1768 ** previously obtained from xMalloc or xRealloc. The allocated size
1769 ** is always at least as big as the requested size but may be larger.
1770 **
@@ -3397,11 +3397,11 @@
3397 ** a schema change, on the first [sqlite3_step()] call following any change
3398 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3399 ** ^The specific value of WHERE-clause [parameter] might influence the
3400 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3401 ** or [GLOB] operator or if the parameter is compared to an indexed column
3402 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3403 ** the
3404 ** </li>
3405 ** </ol>
3406 */
3407 SQLITE_API int sqlite3_prepare(
@@ -7711,22 +7711,10 @@
7711 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7712 ** have to specify the value in the less intuitive manner shown:
7713 */
7714 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7715
7716 /*
7717 ** The datatype used to store estimates of the number of rows in a
7718 ** table or index. This is an unsigned integer type. For 99.9% of
7719 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
7720 ** can be used at compile-time if desired.
7721 */
7722 #ifdef SQLITE_64BIT_STATS
7723 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
7724 #else
7725 typedef u32 tRowcnt; /* 32-bit is the default */
7726 #endif
7727
7728 /*
7729 ** Macros to determine whether the machine is big or little endian,
7730 ** evaluated at runtime.
7731 */
7732 #ifdef SQLITE_AMALGAMATION
@@ -8188,10 +8176,11 @@
8188 ** or VDBE. The VDBE implements an abstract machine that runs a
8189 ** simple program to access and modify the underlying database.
8190 */
8191 #ifndef _SQLITE_VDBE_H_
8192 #define _SQLITE_VDBE_H_
 
8193
8194 /*
8195 ** A single VDBE is an opaque structure named "Vdbe". Only routines
8196 ** in the source file sqliteVdbe.c are allowed to see the insides
8197 ** of this structure.
@@ -8231,10 +8220,11 @@
8231 Mem *pMem; /* Used when p4type is P4_MEM */
8232 VTable *pVtab; /* Used when p4type is P4_VTAB */
8233 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
8234 int *ai; /* Used when p4type is P4_INTARRAY */
8235 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
 
8236 } p4;
8237 #ifdef SQLITE_DEBUG
8238 char *zComment; /* Comment to improve readability */
8239 #endif
8240 #ifdef VDBE_PROFILE
@@ -8286,10 +8276,11 @@
8286 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
8287 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
8288 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
8289 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8290 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
 
8291
8292 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8293 ** is made. That copy is freed when the Vdbe is finalized. But if the
8294 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
8295 ** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8401,88 +8392,90 @@
8401 #define OP_VerifyCookie 37
8402 #define OP_OpenRead 38
8403 #define OP_OpenWrite 39
8404 #define OP_OpenAutoindex 40
8405 #define OP_OpenEphemeral 41
8406 #define OP_OpenPseudo 42
8407 #define OP_Close 43
8408 #define OP_SeekLt 44
8409 #define OP_SeekLe 45
8410 #define OP_SeekGe 46
8411 #define OP_SeekGt 47
8412 #define OP_Seek 48
8413 #define OP_NotFound 49
8414 #define OP_Found 50
8415 #define OP_IsUnique 51
8416 #define OP_NotExists 52
8417 #define OP_Sequence 53
8418 #define OP_NewRowid 54
8419 #define OP_Insert 55
8420 #define OP_InsertInt 56
8421 #define OP_Delete 57
8422 #define OP_ResetCount 58
8423 #define OP_RowKey 59
8424 #define OP_RowData 60
8425 #define OP_Rowid 61
8426 #define OP_NullRow 62
8427 #define OP_Last 63
8428 #define OP_Sort 64
8429 #define OP_Rewind 65
8430 #define OP_Prev 66
8431 #define OP_Next 67
8432 #define OP_IdxInsert 70
8433 #define OP_IdxDelete 71
8434 #define OP_IdxRowid 72
8435 #define OP_IdxLT 81
8436 #define OP_IdxGE 92
8437 #define OP_Destroy 95
8438 #define OP_Clear 96
8439 #define OP_CreateIndex 97
8440 #define OP_CreateTable 98
8441 #define OP_ParseSchema 99
8442 #define OP_LoadAnalysis 100
8443 #define OP_DropTable 101
8444 #define OP_DropIndex 102
8445 #define OP_DropTrigger 103
8446 #define OP_IntegrityCk 104
8447 #define OP_RowSetAdd 105
8448 #define OP_RowSetRead 106
8449 #define OP_RowSetTest 107
8450 #define OP_Program 108
8451 #define OP_Param 109
8452 #define OP_FkCounter 110
8453 #define OP_FkIfZero 111
8454 #define OP_MemMax 112
8455 #define OP_IfPos 113
8456 #define OP_IfNeg 114
8457 #define OP_IfZero 115
8458 #define OP_AggStep 116
8459 #define OP_AggFinal 117
8460 #define OP_Checkpoint 118
8461 #define OP_JournalMode 119
8462 #define OP_Vacuum 120
8463 #define OP_IncrVacuum 121
8464 #define OP_Expire 122
8465 #define OP_TableLock 123
8466 #define OP_VBegin 124
8467 #define OP_VCreate 125
8468 #define OP_VDestroy 126
8469 #define OP_VOpen 127
8470 #define OP_VFilter 128
8471 #define OP_VColumn 129
8472 #define OP_VNext 131
8473 #define OP_VRename 132
8474 #define OP_VUpdate 133
8475 #define OP_Pagecount 134
8476 #define OP_MaxPgcnt 135
8477 #define OP_Trace 136
8478 #define OP_Noop 137
8479 #define OP_Explain 138
8480
8481 /* The following opcode values are never used */
8482 #define OP_NotUsed_139 139
8483 #define OP_NotUsed_140 140
 
 
8484
8485
8486 /* Properties such as "out2" or "jump" that are specified in
8487 ** comments following the "case" for each opcode in the vdbe.c
8488 ** are encoded into bitvectors as follows:
@@ -8498,24 +8491,24 @@
8498 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8499 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8500 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8501 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8502 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8503 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11,\
8504 /* 48 */ 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02, 0x00,\
8505 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8506 /* 64 */ 0x01, 0x01, 0x01, 0x01, 0x4c, 0x4c, 0x08, 0x00,\
8507 /* 72 */ 0x02, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8508 /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8509 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x02,\
8510 /* 96 */ 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,\
8511 /* 104 */ 0x00, 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01,\
8512 /* 112 */ 0x08, 0x05, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8513 /* 120 */ 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8514 /* 128 */ 0x01, 0x00, 0x02, 0x01, 0x00, 0x00, 0x02, 0x02,\
8515 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x04, 0x04,\
8516 /* 144 */ 0x04, 0x04,}
8517
8518 /************** End of opcodes.h *********************************************/
8519 /************** Continuing where we left off in vdbe.h ***********************/
8520
8521 /*
@@ -8529,13 +8522,13 @@
8529 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8530 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8531 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8532 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8533 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8534 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, int addr, int P1);
8535 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, int addr, int P2);
8536 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, int addr, int P3);
8537 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8538 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8539 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8540 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8541 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8646,11 @@
8653 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8654 */
8655 #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
8656 #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
8657 #define PAGER_MEMORY 0x0004 /* In-memory database */
 
8658
8659 /*
8660 ** Valid values for the second argument to sqlite3PagerLockingMode().
8661 */
8662 #define PAGER_LOCKINGMODE_QUERY -1
@@ -9924,11 +9918,11 @@
9924 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9925 int nCol; /* Number of columns in this table */
9926 Column *aCol; /* Information about each column */
9927 Index *pIndex; /* List of SQL indexes on this table. */
9928 int tnum; /* Root BTree node for this table (see note above) */
9929 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9930 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9931 u16 nRef; /* Number of pointers to this Table */
9932 u8 tabFlags; /* Mask of TF_* values */
9933 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9934 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10117,35 @@
10123 */
10124 struct Index {
10125 char *zName; /* Name of this index */
10126 int nColumn; /* Number of columns in the table used by this index */
10127 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10128 tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10129 Table *pTable; /* The SQL table being indexed */
10130 int tnum; /* Page containing root of this index in database file */
10131 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10132 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10133 u8 bUnordered; /* Use this index for == or IN queries only */
10134 u8 nSample; /* Number of elements in aSample[] */
10135 char *zColAff; /* String defining the affinity of each column */
10136 Index *pNext; /* The next index associated with the same table */
10137 Schema *pSchema; /* Schema containing this index */
10138 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10139 char **azColl; /* Array of collation sequence names for index */
10140 #ifdef SQLITE_ENABLE_STAT3
10141 tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10142 IndexSample *aSample; /* Samples of the left-most key */
10143 #endif
10144 };
10145
10146 /*
10147 ** Each sample stored in the sqlite_stat2 table is represented in memory
10148 ** using a structure of this type.
10149 */
10150 struct IndexSample {
10151 union {
10152 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10153 double r; /* Value if eType is SQLITE_FLOAT */
10154 i64 i; /* Value if eType is SQLITE_INTEGER */
10155 } u;
10156 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10157 u16 nByte; /* Size in byte of text or blob. */
10158 tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10159 tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10160 tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10161 };
10162
10163 /*
10164 ** Each token coming out of the lexer is an instance of
10165 ** this structure. Tokens are also used as part of an expression.
@@ -10190,10 +10176,11 @@
10190 u8 directMode; /* Direct rendering mode means take data directly
10191 ** from source tables rather than from accumulators */
10192 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
10193 ** than the source table */
10194 int sortingIdx; /* Cursor number of the sorting index */
 
10195 ExprList *pGroupBy; /* The group by clause */
10196 int nSortingColumn; /* Number of columns in the sorting index */
10197 struct AggInfo_col { /* For each column used in source tables */
10198 Table *pTab; /* Source table */
10199 int iTable; /* Cursor number of the source table */
@@ -10722,10 +10709,11 @@
10722 #define SF_Resolved 0x0002 /* Identifiers have been resolved */
10723 #define SF_Aggregate 0x0004 /* Contains aggregate functions */
10724 #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
10725 #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
10726 #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
 
10727
10728
10729 /*
10730 ** The results of a select can be distributed in several ways. The
10731 ** "SRT" prefix means "SELECT Result Type".
@@ -11361,11 +11349,10 @@
11361 #else
11362 # define sqlite3ViewGetColumnNames(A,B) 0
11363 #endif
11364
11365 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11366 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
11367 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11368 #ifndef SQLITE_OMIT_AUTOINCREMENT
11369 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11370 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11371 #else
@@ -11618,11 +11605,11 @@
11618 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11619 void(*)(void*));
11620 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11621 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11622 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11623 #ifdef SQLITE_ENABLE_STAT3
11624 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11625 #endif
11626 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11627 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11628 #ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12232,10 @@
12245 "ENABLE_RTREE",
12246 #endif
12247 #ifdef SQLITE_ENABLE_STAT2
12248 "ENABLE_STAT2",
12249 #endif
12250 #ifdef SQLITE_ENABLE_STAT3
12251 "ENABLE_STAT3",
12252 #endif
12253 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12254 "ENABLE_UNLOCK_NOTIFY",
12255 #endif
12256 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12257 "ENABLE_UPDATE_DELETE_LIMIT",
@@ -12275,10 +12259,13 @@
12275 "INT64_TYPE",
12276 #endif
12277 #ifdef SQLITE_LOCK_TRACE
12278 "LOCK_TRACE",
12279 #endif
 
 
 
12280 #ifdef SQLITE_MEMDEBUG
12281 "MEMDEBUG",
12282 #endif
12283 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
12284 "MIXED_ENDIAN_64BIT_FLOAT",
@@ -12387,10 +12374,13 @@
12387 #ifdef SQLITE_OMIT_LOOKASIDE
12388 "OMIT_LOOKASIDE",
12389 #endif
12390 #ifdef SQLITE_OMIT_MEMORYDB
12391 "OMIT_MEMORYDB",
 
 
 
12392 #endif
12393 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
12394 "OMIT_OR_OPTIMIZATION",
12395 #endif
12396 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12443,13 @@
12453 #ifdef SQLITE_OMIT_WSD
12454 "OMIT_WSD",
12455 #endif
12456 #ifdef SQLITE_OMIT_XFER_OPT
12457 "OMIT_XFER_OPT",
 
 
 
12458 #endif
12459 #ifdef SQLITE_PERFORMANCE_TRACE
12460 "PERFORMANCE_TRACE",
12461 #endif
12462 #ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12567,13 @@
12574 /*
12575 ** Boolean values
12576 */
12577 typedef unsigned char Bool;
12578
 
 
 
12579 /*
12580 ** A cursor is a pointer into a single BTree within a database file.
12581 ** The cursor can seek to a BTree entry with a particular key, or
12582 ** loop over all entries of the Btree. You can also insert new BTree
12583 ** entries or retrieve the key or data from the entry that the cursor
@@ -12600,15 +12596,17 @@
12600 Bool nullRow; /* True if pointing to a row with no data */
12601 Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
12602 Bool isTable; /* True if a table requiring integer keys */
12603 Bool isIndex; /* True if an index containing keys only - no data */
12604 Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */
 
12605 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
12606 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
12607 i64 seqCount; /* Sequence counter */
12608 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
12609 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
 
12610
12611 /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12612 ** OP_IsUnique opcode on this cursor. */
12613 int seekResult;
12614
@@ -12924,17 +12922,38 @@
12924 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12925 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12926 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12927 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12928 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
 
 
 
12929 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12930 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12931 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12932 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12933 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12934 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12935 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12936
12937 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12938 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12939 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12940 #else
@@ -13225,10 +13244,12 @@
13225 ** Astronomical Algorithms, 2nd Edition, 1998
13226 ** ISBM 0-943396-61-1
13227 ** Willmann-Bell, Inc
13228 ** Richmond, Virginia (USA)
13229 */
 
 
13230 #include <time.h>
13231
13232 #ifndef SQLITE_OMIT_DATETIME_FUNCS
13233
13234
@@ -14978,10 +14999,11 @@
14978 extern void backtrace_symbols_fd(void*const*,int,int);
14979 #else
14980 # define backtrace(A,B) 1
14981 # define backtrace_symbols_fd(A,B,C)
14982 #endif
 
14983
14984 /*
14985 ** Each memory allocation looks like this:
14986 **
14987 ** ------------------------------------------------------------------------
@@ -18081,10 +18103,11 @@
18081 **
18082 *************************************************************************
18083 **
18084 ** Memory allocation functions used throughout sqlite.
18085 */
 
18086
18087 /*
18088 ** Attempt to release up to n bytes of non-essential memory currently
18089 ** held by SQLite. An example of non-essential memory is memory used to
18090 ** cache database pages that are not currently in use.
@@ -20058,10 +20081,11 @@
20058 ** BOM or Byte Order Mark:
20059 ** 0xff 0xfe little-endian utf-16 follows
20060 ** 0xfe 0xff big-endian utf-16 follows
20061 **
20062 */
 
20063
20064 #ifndef SQLITE_AMALGAMATION
20065 /*
20066 ** The following constant value is used by the SQLITE_BIGENDIAN and
20067 ** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20510,11 @@
20486 ** no longer required.
20487 **
20488 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20489 ** flag set.
20490 */
20491 #ifdef SQLITE_ENABLE_STAT3
20492 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20493 Mem m;
20494 memset(&m, 0, sizeof(m));
20495 m.db = db;
20496 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20624,11 @@
20600 **
20601 ** This file contains functions for allocating memory, comparing
20602 ** strings, and stuff like that.
20603 **
20604 */
 
20605 #ifdef SQLITE_HAVE_ISNAN
20606 # include <math.h>
20607 #endif
20608
20609 /*
@@ -21778,10 +21803,11 @@
21778 **
21779 *************************************************************************
21780 ** This is the implementation of generic hash-tables
21781 ** used in SQLite.
21782 */
 
21783
21784 /* Turn bulk memory into a hash table object by initializing the
21785 ** fields of the Hash structure.
21786 **
21787 ** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22088,50 +22114,50 @@
22088 /* 37 */ "VerifyCookie",
22089 /* 38 */ "OpenRead",
22090 /* 39 */ "OpenWrite",
22091 /* 40 */ "OpenAutoindex",
22092 /* 41 */ "OpenEphemeral",
22093 /* 42 */ "OpenPseudo",
22094 /* 43 */ "Close",
22095 /* 44 */ "SeekLt",
22096 /* 45 */ "SeekLe",
22097 /* 46 */ "SeekGe",
22098 /* 47 */ "SeekGt",
22099 /* 48 */ "Seek",
22100 /* 49 */ "NotFound",
22101 /* 50 */ "Found",
22102 /* 51 */ "IsUnique",
22103 /* 52 */ "NotExists",
22104 /* 53 */ "Sequence",
22105 /* 54 */ "NewRowid",
22106 /* 55 */ "Insert",
22107 /* 56 */ "InsertInt",
22108 /* 57 */ "Delete",
22109 /* 58 */ "ResetCount",
22110 /* 59 */ "RowKey",
22111 /* 60 */ "RowData",
22112 /* 61 */ "Rowid",
22113 /* 62 */ "NullRow",
22114 /* 63 */ "Last",
22115 /* 64 */ "Sort",
22116 /* 65 */ "Rewind",
22117 /* 66 */ "Prev",
22118 /* 67 */ "Next",
22119 /* 68 */ "Or",
22120 /* 69 */ "And",
22121 /* 70 */ "IdxInsert",
22122 /* 71 */ "IdxDelete",
22123 /* 72 */ "IdxRowid",
22124 /* 73 */ "IsNull",
22125 /* 74 */ "NotNull",
22126 /* 75 */ "Ne",
22127 /* 76 */ "Eq",
22128 /* 77 */ "Gt",
22129 /* 78 */ "Le",
22130 /* 79 */ "Lt",
22131 /* 80 */ "Ge",
22132 /* 81 */ "IdxLT",
22133 /* 82 */ "BitAnd",
22134 /* 83 */ "BitOr",
22135 /* 84 */ "ShiftLeft",
22136 /* 85 */ "ShiftRight",
22137 /* 86 */ "Add",
@@ -22138,64 +22164,68 @@
22138 /* 87 */ "Subtract",
22139 /* 88 */ "Multiply",
22140 /* 89 */ "Divide",
22141 /* 90 */ "Remainder",
22142 /* 91 */ "Concat",
22143 /* 92 */ "IdxGE",
22144 /* 93 */ "BitNot",
22145 /* 94 */ "String8",
22146 /* 95 */ "Destroy",
22147 /* 96 */ "Clear",
22148 /* 97 */ "CreateIndex",
22149 /* 98 */ "CreateTable",
22150 /* 99 */ "ParseSchema",
22151 /* 100 */ "LoadAnalysis",
22152 /* 101 */ "DropTable",
22153 /* 102 */ "DropIndex",
22154 /* 103 */ "DropTrigger",
22155 /* 104 */ "IntegrityCk",
22156 /* 105 */ "RowSetAdd",
22157 /* 106 */ "RowSetRead",
22158 /* 107 */ "RowSetTest",
22159 /* 108 */ "Program",
22160 /* 109 */ "Param",
22161 /* 110 */ "FkCounter",
22162 /* 111 */ "FkIfZero",
22163 /* 112 */ "MemMax",
22164 /* 113 */ "IfPos",
22165 /* 114 */ "IfNeg",
22166 /* 115 */ "IfZero",
22167 /* 116 */ "AggStep",
22168 /* 117 */ "AggFinal",
22169 /* 118 */ "Checkpoint",
22170 /* 119 */ "JournalMode",
22171 /* 120 */ "Vacuum",
22172 /* 121 */ "IncrVacuum",
22173 /* 122 */ "Expire",
22174 /* 123 */ "TableLock",
22175 /* 124 */ "VBegin",
22176 /* 125 */ "VCreate",
22177 /* 126 */ "VDestroy",
22178 /* 127 */ "VOpen",
22179 /* 128 */ "VFilter",
22180 /* 129 */ "VColumn",
22181 /* 130 */ "Real",
22182 /* 131 */ "VNext",
22183 /* 132 */ "VRename",
22184 /* 133 */ "VUpdate",
22185 /* 134 */ "Pagecount",
22186 /* 135 */ "MaxPgcnt",
22187 /* 136 */ "Trace",
22188 /* 137 */ "Noop",
22189 /* 138 */ "Explain",
22190 /* 139 */ "NotUsed_139",
22191 /* 140 */ "NotUsed_140",
22192 /* 141 */ "ToText",
22193 /* 142 */ "ToBlob",
22194 /* 143 */ "ToNumeric",
22195 /* 144 */ "ToInt",
22196 /* 145 */ "ToReal",
 
 
 
 
22197 };
22198 return azName[i];
22199 }
22200 #endif
22201
@@ -22286,11 +22316,11 @@
22286 */
22287 #ifdef MEMORY_DEBUG
22288 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22289 #endif
22290
22291 #ifdef SQLITE_DEBUG
22292 # ifndef SQLITE_DEBUG_OS_TRACE
22293 # define SQLITE_DEBUG_OS_TRACE 0
22294 # endif
22295 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22296 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -24450,10 +24480,11 @@
24450 */
24451 #include <sys/types.h>
24452 #include <sys/stat.h>
24453 #include <fcntl.h>
24454 #include <unistd.h>
 
24455 #include <sys/time.h>
24456 #include <errno.h>
24457 #ifndef SQLITE_OMIT_WAL
24458 #include <sys/mman.h>
24459 #endif
@@ -24485,10 +24516,11 @@
24485 /*
24486 ** If we are to be thread-safe, include the pthreads header and define
24487 ** the SQLITE_UNIX_THREADS macro.
24488 */
24489 #if SQLITE_THREADSAFE
 
24490 # define SQLITE_UNIX_THREADS 1
24491 #endif
24492
24493 /*
24494 ** Default permissions when creating a new file
@@ -24584,11 +24616,15 @@
24584 ** Allowed values for the unixFile.ctrlFlags bitmask:
24585 */
24586 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24587 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
24588 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24589 #define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
 
 
 
 
24590
24591 /*
24592 ** Include code that is common to all os_*.c files
24593 */
24594 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -24622,11 +24658,11 @@
24622 */
24623 #ifdef MEMORY_DEBUG
24624 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
24625 #endif
24626
24627 #ifdef SQLITE_DEBUG
24628 # ifndef SQLITE_DEBUG_OS_TRACE
24629 # define SQLITE_DEBUG_OS_TRACE 0
24630 # endif
24631 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
24632 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -27061,15 +27097,16 @@
27061 */
27062 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
27063 int rc = SQLITE_OK;
27064 int reserved = 0;
27065 unixFile *pFile = (unixFile*)id;
 
27066
27067 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27068
27069 assert( pFile );
27070 afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
27071 if( context->reserved ){
27072 *pResOut = 1;
27073 return SQLITE_OK;
27074 }
27075 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27242,11 @@
27205
27206 /* If control gets to this point, then actually go ahead and make
27207 ** operating system calls for the specified lock.
27208 */
27209 if( eFileLock==SHARED_LOCK ){
27210 int lrc1, lrc2, lrc1Errno;
27211 long lk, mask;
27212
27213 assert( pInode->nShared==0 );
27214 assert( pInode->eFileLock==0 );
27215
@@ -27579,21 +27616,23 @@
27579 #if defined(USE_PREAD)
27580 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27581 #elif defined(USE_PREAD64)
27582 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27583 #else
27584 newOffset = lseek(id->h, offset, SEEK_SET);
27585 SimulateIOError( newOffset-- );
27586 if( newOffset!=offset ){
27587 if( newOffset == -1 ){
27588 ((unixFile*)id)->lastErrno = errno;
27589 }else{
27590 ((unixFile*)id)->lastErrno = 0;
27591 }
27592 return -1;
27593 }
27594 do{ got = osWrite(id->h, pBuf, cnt); }while( got<0 && errno==EINTR );
 
 
27595 #endif
27596 TIMER_END;
27597 if( got<0 ){
27598 ((unixFile*)id)->lastErrno = errno;
27599 }
@@ -27679,15 +27718,15 @@
27679 SQLITE_API int sqlite3_fullsync_count = 0;
27680 #endif
27681
27682 /*
27683 ** We do not trust systems to provide a working fdatasync(). Some do.
27684 ** Others do no. To be safe, we will stick with the (slower) fsync().
27685 ** If you know that your system does support fdatasync() correctly,
27686 ** then simply compile with -Dfdatasync=fdatasync
27687 */
27688 #if !defined(fdatasync) && !defined(__linux__)
27689 # define fdatasync fsync
27690 #endif
27691
27692 /*
27693 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
@@ -27888,10 +27927,12 @@
27888 HAVE_FULLFSYNC, isFullsync));
27889 rc = osOpenDirectory(pFile->zPath, &dirfd);
27890 if( rc==SQLITE_OK && dirfd>=0 ){
27891 full_fsync(dirfd, 0, 0);
27892 robust_close(pFile, dirfd, __LINE__);
 
 
27893 }
27894 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27895 }
27896 return rc;
27897 }
@@ -27971,30 +28012,22 @@
27971 static int proxyFileControl(sqlite3_file*,int,void*);
27972 #endif
27973
27974 /*
27975 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27976 ** file-control operation.
27977 **
27978 ** If the user has configured a chunk-size for this file, it could be
27979 ** that the file needs to be extended at this point. Otherwise, the
27980 ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix.
27981 */
27982 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27983 { /* preserve indentation of removed "if" */
27984 i64 nSize; /* Required file size */
27985 i64 szChunk; /* Chunk size */
27986 struct stat buf; /* Used to hold return values of fstat() */
27987
27988 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27989
27990 szChunk = pFile->szChunk;
27991 if( szChunk==0 ){
27992 nSize = nByte;
27993 }else{
27994 nSize = ((nByte+szChunk-1) / szChunk) * szChunk;
27995 }
27996 if( nSize>(i64)buf.st_size ){
27997
27998 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27999 /* The code below is handling the return value of osFallocate()
28000 ** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28081,15 @@
28048 case SQLITE_FCNTL_CHUNK_SIZE: {
28049 pFile->szChunk = *(int *)pArg;
28050 return SQLITE_OK;
28051 }
28052 case SQLITE_FCNTL_SIZE_HINT: {
28053 return fcntlSizeHint(pFile, *(i64 *)pArg);
 
 
 
 
28054 }
28055 case SQLITE_FCNTL_PERSIST_WAL: {
28056 int bPersist = *(int*)pArg;
28057 if( bPersist<0 ){
28058 *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -28175,15 +28212,13 @@
28175 */
28176 struct unixShm {
28177 unixShmNode *pShmNode; /* The underlying unixShmNode object */
28178 unixShm *pNext; /* Next unixShm with the same unixShmNode */
28179 u8 hasMutex; /* True if holding the unixShmNode mutex */
 
28180 u16 sharedMask; /* Mask of shared locks held */
28181 u16 exclMask; /* Mask of exclusive locks held */
28182 #ifdef SQLITE_DEBUG
28183 u8 id; /* Id of this connection within its unixShmNode */
28184 #endif
28185 };
28186
28187 /*
28188 ** Constants used for locking
28189 */
@@ -29485,10 +29520,13 @@
29485 int isReadonly = (flags & SQLITE_OPEN_READONLY);
29486 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
29487 #if SQLITE_ENABLE_LOCKING_STYLE
29488 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
29489 #endif
 
 
 
29490
29491 /* If creating a master or main-file journal, this function will open
29492 ** a file-descriptor on the directory too. The first time unixSync()
29493 ** is called the directory file descriptor will be fsync()ed and close()d.
29494 */
@@ -29617,11 +29655,10 @@
29617
29618 noLock = eType!=SQLITE_OPEN_MAIN_DB;
29619
29620
29621 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29622 struct statfs fsInfo;
29623 if( fstatfs(fd, &fsInfo) == -1 ){
29624 ((unixFile*)pFile)->lastErrno = errno;
29625 robust_close(p, fd, __LINE__);
29626 return SQLITE_IOERR_ACCESS;
29627 }
@@ -29641,11 +29678,10 @@
29641 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29642 ** never use proxy, NULL means use proxy for non-local files only. */
29643 if( envforce!=NULL ){
29644 useProxy = atoi(envforce)>0;
29645 }else{
29646 struct statfs fsInfo;
29647 if( statfs(zPath, &fsInfo) == -1 ){
29648 /* In theory, the close(fd) call is sub-optimal. If the file opened
29649 ** with fd is a database file, and there are other connections open
29650 ** on that file that are currently holding advisory locks on it,
29651 ** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
29715 #endif
29716 {
29717 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29718 }
29719 robust_close(0, fd, __LINE__);
 
 
29720 }
29721 }
29722 #endif
29723 return rc;
29724 }
@@ -30380,10 +30418,12 @@
30380 *pError = err;
30381 }
30382 return SQLITE_IOERR;
30383 }
30384 }
 
 
30385 #endif
30386 #ifdef SQLITE_TEST
30387 /* simulate multiple hosts by creating unique hostid file paths */
30388 if( sqlite3_hostid_num != 0){
30389 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
30472 unixFile *conchFile = pCtx->conchFile;
30473 int rc = SQLITE_OK;
30474 int nTries = 0;
30475 struct timespec conchModTime;
30476
 
30477 do {
30478 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30479 nTries ++;
30480 if( rc==SQLITE_BUSY ){
30481 /* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
30703 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30704
30705 end_takeconch:
30706 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
30707 if( rc==SQLITE_OK && pFile->openFlags ){
 
30708 if( pFile->h>=0 ){
30709 robust_close(pFile, pFile->h, __LINE__);
30710 }
30711 pFile->h = -1;
30712 int fd = robust_open(pCtx->dbPath, pFile->openFlags,
30713 SQLITE_DEFAULT_FILE_PERMISSIONS);
30714 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
30715 if( fd>=0 ){
30716 pFile->h = fd;
30717 }else{
@@ -31393,11 +31435,11 @@
31393 */
31394 #ifdef MEMORY_DEBUG
31395 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
31396 #endif
31397
31398 #ifdef SQLITE_DEBUG
31399 # ifndef SQLITE_DEBUG_OS_TRACE
31400 # define SQLITE_DEBUG_OS_TRACE 0
31401 # endif
31402 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31403 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -31629,10 +31671,80 @@
31629 winceLock local; /* Locks obtained by this instance of winFile */
31630 winceLock *shared; /* Global shared lock memory for the file */
31631 #endif
31632 };
31633
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31634
31635 /*
31636 ** Forward prototypes.
31637 */
31638 static int getSectorSize(
@@ -31681,10 +31793,192 @@
31681 }
31682 return sqlite3_os_type==2;
31683 }
31684 #endif /* SQLITE_OS_WINCE */
31685
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31686 /*
31687 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31688 **
31689 ** Space to hold the returned string is obtained from malloc.
31690 */
@@ -31969,10 +32263,11 @@
31969 */
31970 /*
31971 ** WindowsCE does not have a localtime() function. So create a
31972 ** substitute.
31973 */
 
31974 struct tm *__cdecl localtime(const time_t *t)
31975 {
31976 static struct tm y;
31977 FILETIME uTm, lTm;
31978 SYSTEMTIME pTm;
@@ -32473,11 +32768,11 @@
32473 /* If the user has configured a chunk-size for this file, truncate the
32474 ** file so that it consists of an integer number of chunks (i.e. the
32475 ** actual file size after the operation may be larger than the requested
32476 ** size).
32477 */
32478 if( pFile->szChunk ){
32479 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
32480 }
32481
32482 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32483 if( seekWinFile(pFile, nByte) ){
@@ -32860,14 +33155,23 @@
32860 case SQLITE_FCNTL_CHUNK_SIZE: {
32861 pFile->szChunk = *(int *)pArg;
32862 return SQLITE_OK;
32863 }
32864 case SQLITE_FCNTL_SIZE_HINT: {
32865 sqlite3_int64 sz = *(sqlite3_int64*)pArg;
32866 SimulateIOErrorBenign(1);
32867 winTruncate(id, sz);
32868 SimulateIOErrorBenign(0);
 
 
 
 
 
 
 
 
 
32869 return SQLITE_OK;
32870 }
32871 case SQLITE_FCNTL_PERSIST_WAL: {
32872 int bPersist = *(int*)pArg;
32873 if( bPersist<0 ){
@@ -35473,10 +35777,13 @@
35473 typedef struct PCache1 PCache1;
35474 typedef struct PgHdr1 PgHdr1;
35475 typedef struct PgFreeslot PgFreeslot;
35476 typedef struct PGroup PGroup;
35477
 
 
 
35478 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35479 ** of one or more PCaches that are able to recycle each others unpinned
35480 ** pages when they are under memory pressure. A PGroup is an instance of
35481 ** the following object.
35482 **
@@ -35502,12 +35809,70 @@
35502 int nMaxPage; /* Sum of nMax for purgeable caches */
35503 int nMinPage; /* Sum of nMin for purgeable caches */
35504 int mxPinned; /* nMaxpage + 10 - nMinPage */
35505 int nCurrentPage; /* Number of purgeable pages allocated */
35506 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
 
 
 
 
35507 };
35508
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35509 /* Each page cache is an instance of the following object. Every
35510 ** open database file (including each in-memory database and each
35511 ** temporary or transient database) has a single page cache which
35512 ** is an instance of this object.
35513 **
@@ -35606,10 +35971,21 @@
35606 **
35607 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35608 */
35609 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35610 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
 
 
 
 
 
 
 
 
 
 
 
35611
35612 /*
35613 ** Macros to enter and leave the PCache LRU mutex.
35614 */
35615 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36108,159 @@
35732 return iSize;
35733 }
35734 }
35735 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
35736
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35737 /*
35738 ** Allocate a new page object initially associated with cache pCache.
35739 */
35740 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
35741 int nByte = sizeof(PgHdr1) + pCache->szPage;
35742 void *pPg = pcache1Alloc(nByte);
35743 PgHdr1 *p;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35744 if( pPg ){
35745 p = PAGE_TO_PGHDR1(pCache, pPg);
35746 if( pCache->bPurgeable ){
35747 pCache->pGroup->nCurrentPage++;
35748 }
35749 }else{
35750 p = 0;
35751 }
 
35752 return p;
35753 }
35754
35755 /*
35756 ** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36270,56 @@
35760 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
35761 */
35762 static void pcache1FreePage(PgHdr1 *p){
35763 if( ALWAYS(p) ){
35764 PCache1 *pCache = p->pCache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35765 if( pCache->bPurgeable ){
35766 pCache->pGroup->nCurrentPage--;
35767 }
35768 pcache1Free(PGHDR1_TO_PAGE(p));
35769 }
35770 }
35771
35772 /*
35773 ** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36753,11 @@
36201 /* Step 5. If a usable page buffer has still not been found,
36202 ** attempt to allocate a new one.
36203 */
36204 if( !pPage ){
36205 if( createFlag==1 ) sqlite3BeginBenignMalloc();
36206 pcache1LeaveMutex(pGroup);
36207 pPage = pcache1AllocPage(pCache);
36208 pcache1EnterMutex(pGroup);
36209 if( createFlag==1 ) sqlite3EndBenignMalloc();
36210 }
36211
36212 if( pPage ){
36213 unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36923,13 @@
36373 ** been released, the function returns. The return value is the total number
36374 ** of bytes of memory released.
36375 */
36376 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36377 int nFree = 0;
 
 
 
36378 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36379 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36380 if( pcache1.pStart==0 ){
36381 PgHdr1 *p;
36382 pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38138,11 @@
37585 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
37586 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
37587 u8 tempFile; /* zFilename is a temporary file */
37588 u8 readOnly; /* True for a read-only database */
37589 u8 memDb; /* True to inhibit all file I/O */
 
37590
37591 /**************************************************************************
37592 ** The following block contains those class members that change during
37593 ** routine opertion. Class members not in this block are either fixed
37594 ** when the pager is first created or else only change when there is a
@@ -40704,10 +41258,11 @@
40704 ** to the caller.
40705 */
40706 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
40707 u8 *pTmp = (u8 *)pPager->pTmpSpace;
40708
 
40709 disable_simulated_io_errors();
40710 sqlite3BeginBenignMalloc();
40711 /* pPager->errCode = 0; */
40712 pPager->exclusiveMode = 0;
40713 #ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41693,11 @@
41138 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41139 ** while in the error state, hence it is impossible for this routine to
41140 ** be called in the error state. Nevertheless, we include a NEVER()
41141 ** test for the error state as a safeguard against future changes.
41142 */
 
41143 if( NEVER(pPager->errCode) ) return SQLITE_OK;
41144 if( pPager->doNotSpill ) return SQLITE_OK;
41145 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
41146 return SQLITE_OK;
41147 }
@@ -50430,15 +50986,16 @@
50430 for(i=0; i<nCell; i++){
50431 u8 *pCell = findCell(pPage, i);
50432 if( eType==PTRMAP_OVERFLOW1 ){
50433 CellInfo info;
50434 btreeParseCellPtr(pPage, pCell, &info);
50435 if( info.iOverflow ){
50436 if( iFrom==get4byte(&pCell[info.iOverflow]) ){
50437 put4byte(&pCell[info.iOverflow], iTo);
50438 break;
50439 }
 
50440 }
50441 }else{
50442 if( get4byte(pCell)==iFrom ){
50443 put4byte(pCell, iTo);
50444 break;
@@ -51155,11 +51712,12 @@
51155
51156 if( NEVER(wrFlag && pBt->readOnly) ){
51157 return SQLITE_READONLY;
51158 }
51159 if( iTable==1 && btreePagecount(pBt)==0 ){
51160 return SQLITE_EMPTY;
 
51161 }
51162
51163 /* Now that no other errors can occur, finish filling in the BtCursor
51164 ** variables and link the cursor into the BtShared list. */
51165 pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52467,13 @@
51909 int i;
51910 for(i=1; i<=pCur->iPage; i++){
51911 releasePage(pCur->apPage[i]);
51912 }
51913 pCur->iPage = 0;
 
 
 
51914 }else{
51915 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
51916 if( rc!=SQLITE_OK ){
51917 pCur->eState = CURSOR_INVALID;
51918 return rc;
@@ -52018,11 +52579,11 @@
52018 assert( cursorHoldsMutex(pCur) );
52019 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52020 rc = moveToRoot(pCur);
52021 if( rc==SQLITE_OK ){
52022 if( pCur->eState==CURSOR_INVALID ){
52023 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52024 *pRes = 1;
52025 }else{
52026 assert( pCur->apPage[pCur->iPage]->nCell>0 );
52027 *pRes = 0;
52028 rc = moveToLeftmost(pCur);
@@ -52057,11 +52618,11 @@
52057 }
52058
52059 rc = moveToRoot(pCur);
52060 if( rc==SQLITE_OK ){
52061 if( CURSOR_INVALID==pCur->eState ){
52062 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52063 *pRes = 1;
52064 }else{
52065 assert( pCur->eState==CURSOR_VALID );
52066 *pRes = 0;
52067 rc = moveToRightmost(pCur);
@@ -52130,16 +52691,16 @@
52130
52131 rc = moveToRoot(pCur);
52132 if( rc ){
52133 return rc;
52134 }
52135 assert( pCur->apPage[pCur->iPage] );
52136 assert( pCur->apPage[pCur->iPage]->isInit );
52137 assert( pCur->apPage[pCur->iPage]->nCell>0 || pCur->eState==CURSOR_INVALID );
52138 if( pCur->eState==CURSOR_INVALID ){
52139 *pRes = -1;
52140 assert( pCur->apPage[pCur->iPage]->nCell==0 );
52141 return SQLITE_OK;
52142 }
52143 assert( pCur->apPage[0]->intKey || pIdxKey );
52144 for(;;){
52145 int lwr, upr, idx;
@@ -52862,10 +53423,13 @@
52862 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52863 btreeParseCellPtr(pPage, pCell, &info);
52864 if( info.iOverflow==0 ){
52865 return SQLITE_OK; /* No overflow pages. Return without doing anything */
52866 }
 
 
 
52867 ovflPgno = get4byte(&pCell[info.iOverflow]);
52868 assert( pBt->usableSize > 4 );
52869 ovflPageSize = pBt->usableSize - 4;
52870 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
52871 assert( ovflPgno==0 || nOvfl>0 );
@@ -55045,10 +55609,15 @@
55045 ** corruption) an SQLite error code is returned.
55046 */
55047 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
55048 i64 nEntry = 0; /* Value to return in *pnEntry */
55049 int rc; /* Return code */
 
 
 
 
 
55050 rc = moveToRoot(pCur);
55051
55052 /* Unless an error occurs, the following loop runs one iteration for each
55053 ** page in the B-Tree structure (not including overflow pages).
55054 */
@@ -55829,11 +56398,10 @@
55829 */
55830 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
55831 BtShared *pBt = pBtree->pBt;
55832 int rc; /* Return code */
55833
55834 assert( pBtree->inTrans==TRANS_NONE );
55835 assert( iVersion==1 || iVersion==2 );
55836
55837 /* If setting the version fields to 1, do not automatically open the
55838 ** WAL connection, even if the version fields are currently set to 2.
55839 */
@@ -56268,106 +56836,110 @@
56268 /* Update the schema version field in the destination database. This
56269 ** is to make sure that the schema-version really does change in
56270 ** the case where the source and destination databases have the
56271 ** same schema version.
56272 */
56273 if( rc==SQLITE_DONE
56274 && (rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1))==SQLITE_OK
56275 ){
56276 int nDestTruncate;
56277
56278 if( p->pDestDb ){
56279 sqlite3ResetInternalSchema(p->pDestDb, -1);
56280 }
56281
56282 /* Set nDestTruncate to the final number of pages in the destination
56283 ** database. The complication here is that the destination page
56284 ** size may be different to the source page size.
56285 **
56286 ** If the source page size is smaller than the destination page size,
56287 ** round up. In this case the call to sqlite3OsTruncate() below will
56288 ** fix the size of the file. However it is important to call
56289 ** sqlite3PagerTruncateImage() here so that any pages in the
56290 ** destination file that lie beyond the nDestTruncate page mark are
56291 ** journalled by PagerCommitPhaseOne() before they are destroyed
56292 ** by the file truncation.
56293 */
56294 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56295 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56296 if( pgszSrc<pgszDest ){
56297 int ratio = pgszDest/pgszSrc;
56298 nDestTruncate = (nSrcPage+ratio-1)/ratio;
56299 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56300 nDestTruncate--;
56301 }
56302 }else{
56303 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56304 }
56305 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56306
56307 if( pgszSrc<pgszDest ){
56308 /* If the source page-size is smaller than the destination page-size,
56309 ** two extra things may need to happen:
56310 **
56311 ** * The destination may need to be truncated, and
56312 **
56313 ** * Data stored on the pages immediately following the
56314 ** pending-byte page in the source database may need to be
56315 ** copied into the destination database.
56316 */
56317 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56318 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56319 i64 iOff;
56320 i64 iEnd;
56321
56322 assert( pFile );
56323 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56324 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56325 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56326 ));
56327
56328 /* This call ensures that all data required to recreate the original
56329 ** database has been stored in the journal for pDestPager and the
56330 ** journal synced to disk. So at this point we may safely modify
56331 ** the database file in any way, knowing that if a power failure
56332 ** occurs, the original database will be reconstructed from the
56333 ** journal file. */
56334 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56335
56336 /* Write the extra pages and truncate the database file as required. */
56337 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56338 for(
56339 iOff=PENDING_BYTE+pgszSrc;
56340 rc==SQLITE_OK && iOff<iEnd;
56341 iOff+=pgszSrc
56342 ){
56343 PgHdr *pSrcPg = 0;
56344 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56345 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56346 if( rc==SQLITE_OK ){
56347 u8 *zData = sqlite3PagerGetData(pSrcPg);
56348 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56349 }
56350 sqlite3PagerUnref(pSrcPg);
56351 }
56352 if( rc==SQLITE_OK ){
56353 rc = backupTruncateFile(pFile, iSize);
56354 }
56355
56356 /* Sync the database file to disk. */
56357 if( rc==SQLITE_OK ){
56358 rc = sqlite3PagerSync(pDestPager);
56359 }
56360 }else{
56361 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56362 }
56363
56364 /* Finish committing the transaction to the destination database. */
56365 if( SQLITE_OK==rc
56366 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56367 ){
56368 rc = SQLITE_DONE;
 
 
 
 
56369 }
56370 }
56371
56372 /* If bCloseTrans is true, then this function opened a read transaction
56373 ** on the source database. Close the read transaction here. There is
@@ -56831,38 +57403,32 @@
56831 ** invoking an external callback, free it now. Calling this function
56832 ** does not free any Mem.zMalloc buffer.
56833 */
56834 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
56835 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
56836 testcase( p->flags & MEM_Agg );
56837 testcase( p->flags & MEM_Dyn );
56838 testcase( p->flags & MEM_RowSet );
56839 testcase( p->flags & MEM_Frame );
56840 if( p->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame) ){
56841 if( p->flags&MEM_Agg ){
56842 sqlite3VdbeMemFinalize(p, p->u.pDef);
56843 assert( (p->flags & MEM_Agg)==0 );
56844 sqlite3VdbeMemRelease(p);
56845 }else if( p->flags&MEM_Dyn && p->xDel ){
56846 assert( (p->flags&MEM_RowSet)==0 );
56847 p->xDel((void *)p->z);
56848 p->xDel = 0;
56849 }else if( p->flags&MEM_RowSet ){
56850 sqlite3RowSetClear(p->u.pRowSet);
56851 }else if( p->flags&MEM_Frame ){
56852 sqlite3VdbeMemSetNull(p);
56853 }
56854 }
56855 }
56856
56857 /*
56858 ** Release any memory held by the Mem. This may leave the Mem in an
56859 ** inconsistent state, for example with (Mem.z==0) and
56860 ** (Mem.type==SQLITE_TEXT).
56861 */
56862 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
56863 sqlite3VdbeMemReleaseExternal(p);
56864 sqlite3DbFree(p->db, p->zMalloc);
56865 p->z = 0;
56866 p->zMalloc = 0;
56867 p->xDel = 0;
56868 }
@@ -57180,11 +57746,11 @@
57180 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57181 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57182 */
57183 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57184 assert( (pFrom->flags & MEM_RowSet)==0 );
57185 sqlite3VdbeMemReleaseExternal(pTo);
57186 memcpy(pTo, pFrom, MEMCELLSIZE);
57187 pTo->xDel = 0;
57188 if( (pFrom->flags&MEM_Static)==0 ){
57189 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57190 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57764,11 @@
57198 */
57199 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57200 int rc = SQLITE_OK;
57201
57202 assert( (pFrom->flags & MEM_RowSet)==0 );
57203 sqlite3VdbeMemReleaseExternal(pTo);
57204 memcpy(pTo, pFrom, MEMCELLSIZE);
57205 pTo->flags &= ~MEM_Dyn;
57206
57207 if( pTo->flags&(MEM_Str|MEM_Blob) ){
57208 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58158,15 @@
57592 *ppVal = 0;
57593 return SQLITE_OK;
57594 }
57595 op = pExpr->op;
57596
57597 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
57598 ** The ifdef here is to enable us to achieve 100% branch test coverage even
57599 ** when SQLITE_ENABLE_STAT3 is omitted.
57600 */
57601 #ifdef SQLITE_ENABLE_STAT3
57602 if( op==TK_REGISTER ) op = pExpr->op2;
57603 #else
57604 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
57605 #endif
57606
@@ -58153,10 +58719,16 @@
58153 assert( p->nOp - i >= 3 );
58154 assert( pOp[-1].opcode==OP_Integer );
58155 n = pOp[-1].p1;
58156 if( n>nMaxArgs ) nMaxArgs = n;
58157 #endif
 
 
 
 
 
 
58158 }
58159
58160 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
58161 assert( -1-pOp->p2<p->nLabel );
58162 pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58816,34 @@
58244 ** Change the value of the P1 operand for a specific instruction.
58245 ** This routine is useful when a large program is loaded from a
58246 ** static array using sqlite3VdbeAddOpList but we want to make a
58247 ** few minor changes to the program.
58248 */
58249 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, int addr, int val){
58250 assert( p!=0 );
58251 assert( addr>=0 );
58252 if( p->nOp>addr ){
58253 p->aOp[addr].p1 = val;
58254 }
58255 }
58256
58257 /*
58258 ** Change the value of the P2 operand for a specific instruction.
58259 ** This routine is useful for setting a jump destination.
58260 */
58261 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, int addr, int val){
58262 assert( p!=0 );
58263 assert( addr>=0 );
58264 if( p->nOp>addr ){
58265 p->aOp[addr].p2 = val;
58266 }
58267 }
58268
58269 /*
58270 ** Change the value of the P3 operand for a specific instruction.
58271 */
58272 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, int addr, int val){
58273 assert( p!=0 );
58274 assert( addr>=0 );
58275 if( p->nOp>addr ){
58276 p->aOp[addr].p3 = val;
58277 }
58278 }
58279
58280 /*
@@ -58292,12 +58861,12 @@
58292 /*
58293 ** Change the P2 operand of instruction addr so that it points to
58294 ** the address of the next instruction to be coded.
58295 */
58296 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58297 assert( addr>=0 || p->db->mallocFailed );
58298 if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
58299 }
58300
58301
58302 /*
58303 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59230,14 @@
58661 break;
58662 }
58663 case P4_SUBPROGRAM: {
58664 sqlite3_snprintf(nTemp, zTemp, "program");
58665 break;
 
 
 
 
58666 }
58667 default: {
58668 zP4 = pOp->p4.z;
58669 if( zP4==0 ){
58670 zP4 = zTemp;
@@ -59285,10 +59858,11 @@
59285 */
59286 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59287 if( pCx==0 ){
59288 return;
59289 }
 
59290 if( pCx->pBt ){
59291 sqlite3BtreeClose(pCx->pBt);
59292 /* The pCx->pCursor will be close automatically, if it exists, by
59293 ** the call above. */
59294 }else if( pCx->pCursor ){
@@ -62585,10 +63159,17 @@
62585 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
62586 ** P if required.
62587 */
62588 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
62589
 
 
 
 
 
 
 
62590 /*
62591 ** Argument pMem points at a register that will be passed to a
62592 ** user-defined function or returned to the user as the result of a query.
62593 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
62594 ** routines.
@@ -63179,10 +63760,11 @@
63179 u8 *zEndHdr; /* Pointer to first byte after the header */
63180 u32 offset; /* Offset into the data */
63181 u32 szField; /* Number of bytes in the content of a field */
63182 int szHdr; /* Size of the header size field at start of record */
63183 int avail; /* Number of bytes of available data */
 
63184 Mem *pReg; /* PseudoTable input register */
63185 } am;
63186 struct OP_Affinity_stack_vars {
63187 const char *zAffinity; /* The affinity to be applied */
63188 char cAff; /* A single character of affinity */
@@ -63250,55 +63832,58 @@
63250 Db *pDb;
63251 } aw;
63252 struct OP_OpenEphemeral_stack_vars {
63253 VdbeCursor *pCx;
63254 } ax;
63255 struct OP_OpenPseudo_stack_vars {
63256 VdbeCursor *pCx;
63257 } ay;
 
 
 
63258 struct OP_SeekGt_stack_vars {
63259 int res;
63260 int oc;
63261 VdbeCursor *pC;
63262 UnpackedRecord r;
63263 int nField;
63264 i64 iKey; /* The rowid we are to seek to */
63265 } az;
63266 struct OP_Seek_stack_vars {
63267 VdbeCursor *pC;
63268 } ba;
63269 struct OP_Found_stack_vars {
63270 int alreadyExists;
63271 VdbeCursor *pC;
63272 int res;
63273 UnpackedRecord *pIdxKey;
63274 UnpackedRecord r;
63275 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63276 } bb;
63277 struct OP_IsUnique_stack_vars {
63278 u16 ii;
63279 VdbeCursor *pCx;
63280 BtCursor *pCrsr;
63281 u16 nField;
63282 Mem *aMx;
63283 UnpackedRecord r; /* B-Tree index search key */
63284 i64 R; /* Rowid stored in register P3 */
63285 } bc;
63286 struct OP_NotExists_stack_vars {
63287 VdbeCursor *pC;
63288 BtCursor *pCrsr;
63289 int res;
63290 u64 iKey;
63291 } bd;
63292 struct OP_NewRowid_stack_vars {
63293 i64 v; /* The new rowid */
63294 VdbeCursor *pC; /* Cursor of table to get the new rowid */
63295 int res; /* Result of an sqlite3BtreeLast() */
63296 int cnt; /* Counter to limit the number of searches */
63297 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
63298 VdbeFrame *pFrame; /* Root frame of VDBE */
63299 } be;
63300 struct OP_InsertInt_stack_vars {
63301 Mem *pData; /* MEM cell holding data for the record to be inserted */
63302 Mem *pKey; /* MEM cell holding key for the record */
63303 i64 iKey; /* The integer ROWID or key for the record to be inserted */
63304 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63305,155 +63890,161 @@
63305 int nZero; /* Number of zero-bytes to append */
63306 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
63307 const char *zDb; /* database name - used by the update hook */
63308 const char *zTbl; /* Table name - used by the opdate hook */
63309 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63310 } bf;
63311 struct OP_Delete_stack_vars {
63312 i64 iKey;
63313 VdbeCursor *pC;
63314 } bg;
 
 
 
 
 
 
 
63315 struct OP_RowData_stack_vars {
63316 VdbeCursor *pC;
63317 BtCursor *pCrsr;
63318 u32 n;
63319 i64 n64;
63320 } bh;
63321 struct OP_Rowid_stack_vars {
63322 VdbeCursor *pC;
63323 i64 v;
63324 sqlite3_vtab *pVtab;
63325 const sqlite3_module *pModule;
63326 } bi;
63327 struct OP_NullRow_stack_vars {
63328 VdbeCursor *pC;
63329 } bj;
63330 struct OP_Last_stack_vars {
63331 VdbeCursor *pC;
63332 BtCursor *pCrsr;
63333 int res;
63334 } bk;
63335 struct OP_Rewind_stack_vars {
63336 VdbeCursor *pC;
63337 BtCursor *pCrsr;
63338 int res;
63339 } bl;
63340 struct OP_Next_stack_vars {
63341 VdbeCursor *pC;
63342 BtCursor *pCrsr;
63343 int res;
63344 } bm;
63345 struct OP_IdxInsert_stack_vars {
63346 VdbeCursor *pC;
63347 BtCursor *pCrsr;
63348 int nKey;
63349 const char *zKey;
63350 } bn;
63351 struct OP_IdxDelete_stack_vars {
63352 VdbeCursor *pC;
63353 BtCursor *pCrsr;
63354 int res;
63355 UnpackedRecord r;
63356 } bo;
63357 struct OP_IdxRowid_stack_vars {
63358 BtCursor *pCrsr;
63359 VdbeCursor *pC;
63360 i64 rowid;
63361 } bp;
63362 struct OP_IdxGE_stack_vars {
63363 VdbeCursor *pC;
63364 int res;
63365 UnpackedRecord r;
63366 } bq;
63367 struct OP_Destroy_stack_vars {
63368 int iMoved;
63369 int iCnt;
63370 Vdbe *pVdbe;
63371 int iDb;
63372 } br;
63373 struct OP_Clear_stack_vars {
63374 int nChange;
63375 } bs;
63376 struct OP_CreateTable_stack_vars {
63377 int pgno;
63378 int flags;
63379 Db *pDb;
63380 } bt;
63381 struct OP_ParseSchema_stack_vars {
63382 int iDb;
63383 const char *zMaster;
63384 char *zSql;
63385 InitData initData;
63386 } bu;
63387 struct OP_IntegrityCk_stack_vars {
63388 int nRoot; /* Number of tables to check. (Number of root pages.) */
63389 int *aRoot; /* Array of rootpage numbers for tables to be checked */
63390 int j; /* Loop counter */
63391 int nErr; /* Number of errors reported */
63392 char *z; /* Text of the error report */
63393 Mem *pnErr; /* Register keeping track of errors remaining */
63394 } bv;
63395 struct OP_RowSetRead_stack_vars {
63396 i64 val;
63397 } bw;
63398 struct OP_RowSetTest_stack_vars {
63399 int iSet;
63400 int exists;
63401 } bx;
63402 struct OP_Program_stack_vars {
63403 int nMem; /* Number of memory registers for sub-program */
63404 int nByte; /* Bytes of runtime space required for sub-program */
63405 Mem *pRt; /* Register to allocate runtime space */
63406 Mem *pMem; /* Used to iterate through memory cells */
63407 Mem *pEnd; /* Last memory cell in new array */
63408 VdbeFrame *pFrame; /* New vdbe frame to execute in */
63409 SubProgram *pProgram; /* Sub-program to execute */
63410 void *t; /* Token identifying trigger */
63411 } by;
63412 struct OP_Param_stack_vars {
63413 VdbeFrame *pFrame;
63414 Mem *pIn;
63415 } bz;
63416 struct OP_MemMax_stack_vars {
63417 Mem *pIn1;
63418 VdbeFrame *pFrame;
63419 } ca;
63420 struct OP_AggStep_stack_vars {
63421 int n;
63422 int i;
63423 Mem *pMem;
63424 Mem *pRec;
63425 sqlite3_context ctx;
63426 sqlite3_value **apVal;
63427 } cb;
63428 struct OP_AggFinal_stack_vars {
63429 Mem *pMem;
63430 } cc;
63431 struct OP_Checkpoint_stack_vars {
63432 int i; /* Loop counter */
63433 int aRes[3]; /* Results */
63434 Mem *pMem; /* Write results here */
63435 } cd;
63436 struct OP_JournalMode_stack_vars {
63437 Btree *pBt; /* Btree to change journal mode of */
63438 Pager *pPager; /* Pager associated with pBt */
63439 int eNew; /* New journal mode */
63440 int eOld; /* The old journal mode */
63441 const char *zFilename; /* Name of database file for pPager */
63442 } ce;
63443 struct OP_IncrVacuum_stack_vars {
63444 Btree *pBt;
63445 } cf;
63446 struct OP_VBegin_stack_vars {
63447 VTable *pVTab;
63448 } cg;
63449 struct OP_VOpen_stack_vars {
63450 VdbeCursor *pCur;
63451 sqlite3_vtab_cursor *pVtabCursor;
63452 sqlite3_vtab *pVtab;
63453 sqlite3_module *pModule;
63454 } ch;
63455 struct OP_VFilter_stack_vars {
63456 int nArg;
63457 int iQuery;
63458 const sqlite3_module *pModule;
63459 Mem *pQuery;
@@ -63462,40 +64053,40 @@
63462 sqlite3_vtab *pVtab;
63463 VdbeCursor *pCur;
63464 int res;
63465 int i;
63466 Mem **apArg;
63467 } ci;
63468 struct OP_VColumn_stack_vars {
63469 sqlite3_vtab *pVtab;
63470 const sqlite3_module *pModule;
63471 Mem *pDest;
63472 sqlite3_context sContext;
63473 } cj;
63474 struct OP_VNext_stack_vars {
63475 sqlite3_vtab *pVtab;
63476 const sqlite3_module *pModule;
63477 int res;
63478 VdbeCursor *pCur;
63479 } ck;
63480 struct OP_VRename_stack_vars {
63481 sqlite3_vtab *pVtab;
63482 Mem *pName;
63483 } cl;
63484 struct OP_VUpdate_stack_vars {
63485 sqlite3_vtab *pVtab;
63486 sqlite3_module *pModule;
63487 int nArg;
63488 int i;
63489 sqlite_int64 rowid;
63490 Mem **apArg;
63491 Mem *pX;
63492 } cm;
63493 struct OP_Trace_stack_vars {
63494 char *zTrace;
63495 char *z;
63496 } cn;
63497 } u;
63498 /* End automatically generated code
63499 ********************************************************************/
63500
63501 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -63591,11 +64182,11 @@
63591 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
63592 assert( pOp->p2>0 );
63593 assert( pOp->p2<=p->nMem );
63594 pOut = &aMem[pOp->p2];
63595 memAboutToChange(p, pOut);
63596 sqlite3VdbeMemReleaseExternal(pOut);
63597 pOut->flags = MEM_Int;
63598 }
63599
63600 /* Sanity checking on other operands */
63601 #ifdef SQLITE_DEBUG
@@ -65061,10 +65652,11 @@
65061 u8 *zEndHdr; /* Pointer to first byte after the header */
65062 u32 offset; /* Offset into the data */
65063 u32 szField; /* Number of bytes in the content of a field */
65064 int szHdr; /* Size of the header size field at start of record */
65065 int avail; /* Number of bytes of available data */
 
65066 Mem *pReg; /* PseudoTable input register */
65067 #endif /* local variables moved into u.am */
65068
65069
65070 u.am.p1 = pOp->p1;
@@ -65073,11 +65665,10 @@
65073 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65074 assert( u.am.p1<p->nCursor );
65075 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65076 u.am.pDest = &aMem[pOp->p3];
65077 memAboutToChange(p, u.am.pDest);
65078 MemSetTypeFlag(u.am.pDest, MEM_Null);
65079 u.am.zRec = 0;
65080
65081 /* This block sets the variable u.am.payloadSize to be the total number of
65082 ** bytes in the record.
65083 **
@@ -65117,11 +65708,11 @@
65117 }else{
65118 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65119 rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65120 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65121 }
65122 }else if( u.am.pC->pseudoTableReg>0 ){
65123 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65124 assert( u.am.pReg->flags & MEM_Blob );
65125 assert( memIsValid(u.am.pReg) );
65126 u.am.payloadSize = u.am.pReg->n;
65127 u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65721,14 @@
65130 }else{
65131 /* Consider the row to be NULL */
65132 u.am.payloadSize = 0;
65133 }
65134
65135 /* If u.am.payloadSize is 0, then just store a NULL */
 
65136 if( u.am.payloadSize==0 ){
65137 assert( u.am.pDest->flags&MEM_Null );
65138 goto op_column_out;
65139 }
65140 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65141 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65142 goto too_big;
@@ -65239,12 +65831,18 @@
65239 ** of the record to the start of the data for the u.am.i-th column
65240 */
65241 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65242 if( u.am.zIdx<u.am.zEndHdr ){
65243 u.am.aOffset[u.am.i] = u.am.offset;
65244 u.am.zIdx += getVarint32(u.am.zIdx, u.am.aType[u.am.i]);
65245 u.am.szField = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.i]);
 
 
 
 
 
 
65246 u.am.offset += u.am.szField;
65247 if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65248 u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65249 break;
65250 }
@@ -65281,11 +65879,11 @@
65281 ** a pointer to a Mem object.
65282 */
65283 if( u.am.aOffset[u.am.p2] ){
65284 assert( rc==SQLITE_OK );
65285 if( u.am.zRec ){
65286 sqlite3VdbeMemReleaseExternal(u.am.pDest);
65287 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65288 }else{
65289 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65290 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65291 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65896,11 @@
65298 u.am.pDest->enc = encoding;
65299 }else{
65300 if( pOp->p4type==P4_MEM ){
65301 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65302 }else{
65303 assert( u.am.pDest->flags&MEM_Null );
65304 }
65305 }
65306
65307 /* If we dynamically allocated space to hold the data (in the
65308 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66098,11 @@
65500 i64 nEntry;
65501 BtCursor *pCrsr;
65502 #endif /* local variables moved into u.ap */
65503
65504 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
65505 if( u.ap.pCrsr ){
65506 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
65507 }else{
65508 u.ap.nEntry = 0;
65509 }
65510 pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66674,13 @@
66076 u.aw.pCur->nullRow = 1;
66077 u.aw.pCur->isOrdered = 1;
66078 rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66079 u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66080
66081 /* Since it performs no memory allocation or IO, the only values that
66082 ** sqlite3BtreeCursor() may return are SQLITE_EMPTY and SQLITE_OK.
66083 ** SQLITE_EMPTY is only returned when attempting to open the table
66084 ** rooted at page 1 of a zero-byte database. */
66085 assert( rc==SQLITE_EMPTY || rc==SQLITE_OK );
66086 if( rc==SQLITE_EMPTY ){
66087 u.aw.pCur->pCursor = 0;
66088 rc = SQLITE_OK;
66089 }
66090
66091 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66092 ** SQLite used to check if the root-page flags were sane at this point
66093 ** and report database corruption if they were not, but this check has
66094 ** since moved into the btree layer. */
@@ -66095,11 +66687,11 @@
66095 u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66096 u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66097 break;
66098 }
66099
66100 /* Opcode: OpenEphemeral P1 P2 * P4 *
66101 **
66102 ** Open a new cursor P1 to a transient table.
66103 ** The cursor is always opened read/write even if
66104 ** the main database is read-only. The ephemeral
66105 ** table is deleted automatically when the cursor is closed.
@@ -66112,10 +66704,15 @@
66112 ** This opcode was once called OpenTemp. But that created
66113 ** confusion because the term "temp table", might refer either
66114 ** to a TEMP table at the SQL level, or to a table opened by
66115 ** this opcode. Then this opcode was call OpenVirtual. But
66116 ** that created confusion with the whole virtual-table idea.
 
 
 
 
 
66117 */
66118 /* Opcode: OpenAutoindex P1 P2 * P4 *
66119 **
66120 ** This opcode works the same as OP_OpenEphemeral. It has a
66121 ** different name to distinguish its use. Tables created using
@@ -66168,10 +66765,34 @@
66168 }
66169 u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66170 u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66171 break;
66172 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66173
66174 /* Opcode: OpenPseudo P1 P2 P3 * *
66175 **
66176 ** Open a new cursor that points to a fake table that contains a single
66177 ** row of data. The content of that one row in the content of memory
@@ -66185,21 +66806,21 @@
66185 **
66186 ** P3 is the number of fields in the records that will be stored by
66187 ** the pseudo-table.
66188 */
66189 case OP_OpenPseudo: {
66190 #if 0 /* local variables moved into u.ay */
66191 VdbeCursor *pCx;
66192 #endif /* local variables moved into u.ay */
66193
66194 assert( pOp->p1>=0 );
66195 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66196 if( u.ay.pCx==0 ) goto no_mem;
66197 u.ay.pCx->nullRow = 1;
66198 u.ay.pCx->pseudoTableReg = pOp->p2;
66199 u.ay.pCx->isTable = 1;
66200 u.ay.pCx->isIndex = 0;
66201 break;
66202 }
66203
66204 /* Opcode: Close P1 * * * *
66205 **
@@ -66267,39 +66888,39 @@
66267 */
66268 case OP_SeekLt: /* jump, in3 */
66269 case OP_SeekLe: /* jump, in3 */
66270 case OP_SeekGe: /* jump, in3 */
66271 case OP_SeekGt: { /* jump, in3 */
66272 #if 0 /* local variables moved into u.az */
66273 int res;
66274 int oc;
66275 VdbeCursor *pC;
66276 UnpackedRecord r;
66277 int nField;
66278 i64 iKey; /* The rowid we are to seek to */
66279 #endif /* local variables moved into u.az */
66280
66281 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66282 assert( pOp->p2!=0 );
66283 u.az.pC = p->apCsr[pOp->p1];
66284 assert( u.az.pC!=0 );
66285 assert( u.az.pC->pseudoTableReg==0 );
66286 assert( OP_SeekLe == OP_SeekLt+1 );
66287 assert( OP_SeekGe == OP_SeekLt+2 );
66288 assert( OP_SeekGt == OP_SeekLt+3 );
66289 assert( u.az.pC->isOrdered );
66290 if( u.az.pC->pCursor!=0 ){
66291 u.az.oc = pOp->opcode;
66292 u.az.pC->nullRow = 0;
66293 if( u.az.pC->isTable ){
66294 /* The input value in P3 might be of any type: integer, real, string,
66295 ** blob, or NULL. But it needs to be an integer before we can do
66296 ** the seek, so covert it. */
66297 pIn3 = &aMem[pOp->p3];
66298 applyNumericAffinity(pIn3);
66299 u.az.iKey = sqlite3VdbeIntValue(pIn3);
66300 u.az.pC->rowidIsValid = 0;
66301
66302 /* If the P3 value could not be converted into an integer without
66303 ** loss of information, then special processing is required... */
66304 if( (pIn3->flags & MEM_Int)==0 ){
66305 if( (pIn3->flags & MEM_Real)==0 ){
@@ -66310,105 +66931,105 @@
66310 }
66311 /* If we reach this point, then the P3 value must be a floating
66312 ** point number. */
66313 assert( (pIn3->flags & MEM_Real)!=0 );
66314
66315 if( u.az.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.az.iKey || pIn3->r>0) ){
66316 /* The P3 value is too large in magnitude to be expressed as an
66317 ** integer. */
66318 u.az.res = 1;
66319 if( pIn3->r<0 ){
66320 if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66321 rc = sqlite3BtreeFirst(u.az.pC->pCursor, &u.az.res);
66322 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66323 }
66324 }else{
66325 if( u.az.oc<=OP_SeekLe ){ assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66326 rc = sqlite3BtreeLast(u.az.pC->pCursor, &u.az.res);
66327 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66328 }
66329 }
66330 if( u.az.res ){
66331 pc = pOp->p2 - 1;
66332 }
66333 break;
66334 }else if( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekGe ){
66335 /* Use the ceiling() function to convert real->int */
66336 if( pIn3->r > (double)u.az.iKey ) u.az.iKey++;
66337 }else{
66338 /* Use the floor() function to convert real->int */
66339 assert( u.az.oc==OP_SeekLe || u.az.oc==OP_SeekGt );
66340 if( pIn3->r < (double)u.az.iKey ) u.az.iKey--;
66341 }
66342 }
66343 rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, 0, (u64)u.az.iKey, 0, &u.az.res);
66344 if( rc!=SQLITE_OK ){
66345 goto abort_due_to_error;
66346 }
66347 if( u.az.res==0 ){
66348 u.az.pC->rowidIsValid = 1;
66349 u.az.pC->lastRowid = u.az.iKey;
66350 }
66351 }else{
66352 u.az.nField = pOp->p4.i;
66353 assert( pOp->p4type==P4_INT32 );
66354 assert( u.az.nField>0 );
66355 u.az.r.pKeyInfo = u.az.pC->pKeyInfo;
66356 u.az.r.nField = (u16)u.az.nField;
66357
66358 /* The next line of code computes as follows, only faster:
66359 ** if( u.az.oc==OP_SeekGt || u.az.oc==OP_SeekLe ){
66360 ** u.az.r.flags = UNPACKED_INCRKEY;
66361 ** }else{
66362 ** u.az.r.flags = 0;
66363 ** }
66364 */
66365 u.az.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.az.oc - OP_SeekLt)));
66366 assert( u.az.oc!=OP_SeekGt || u.az.r.flags==UNPACKED_INCRKEY );
66367 assert( u.az.oc!=OP_SeekLe || u.az.r.flags==UNPACKED_INCRKEY );
66368 assert( u.az.oc!=OP_SeekGe || u.az.r.flags==0 );
66369 assert( u.az.oc!=OP_SeekLt || u.az.r.flags==0 );
66370
66371 u.az.r.aMem = &aMem[pOp->p3];
66372 #ifdef SQLITE_DEBUG
66373 { int i; for(i=0; i<u.az.r.nField; i++) assert( memIsValid(&u.az.r.aMem[i]) ); }
66374 #endif
66375 ExpandBlob(u.az.r.aMem);
66376 rc = sqlite3BtreeMovetoUnpacked(u.az.pC->pCursor, &u.az.r, 0, 0, &u.az.res);
66377 if( rc!=SQLITE_OK ){
66378 goto abort_due_to_error;
66379 }
66380 u.az.pC->rowidIsValid = 0;
66381 }
66382 u.az.pC->deferredMoveto = 0;
66383 u.az.pC->cacheStatus = CACHE_STALE;
66384 #ifdef SQLITE_TEST
66385 sqlite3_search_count++;
66386 #endif
66387 if( u.az.oc>=OP_SeekGe ){ assert( u.az.oc==OP_SeekGe || u.az.oc==OP_SeekGt );
66388 if( u.az.res<0 || (u.az.res==0 && u.az.oc==OP_SeekGt) ){
66389 rc = sqlite3BtreeNext(u.az.pC->pCursor, &u.az.res);
66390 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66391 u.az.pC->rowidIsValid = 0;
66392 }else{
66393 u.az.res = 0;
66394 }
66395 }else{
66396 assert( u.az.oc==OP_SeekLt || u.az.oc==OP_SeekLe );
66397 if( u.az.res>0 || (u.az.res==0 && u.az.oc==OP_SeekLt) ){
66398 rc = sqlite3BtreePrevious(u.az.pC->pCursor, &u.az.res);
66399 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66400 u.az.pC->rowidIsValid = 0;
66401 }else{
66402 /* u.az.res might be negative because the table is empty. Check to
66403 ** see if this is the case.
66404 */
66405 u.az.res = sqlite3BtreeEof(u.az.pC->pCursor);
66406 }
66407 }
66408 assert( pOp->p2>0 );
66409 if( u.az.res ){
66410 pc = pOp->p2 - 1;
66411 }
66412 }else{
66413 /* This happens when attempting to open the sqlite3_master table
66414 ** for read access returns SQLITE_EMPTY. In this case always
@@ -66427,24 +67048,24 @@
66427 ** This is actually a deferred seek. Nothing actually happens until
66428 ** the cursor is used to read a record. That way, if no reads
66429 ** occur, no unnecessary I/O happens.
66430 */
66431 case OP_Seek: { /* in2 */
66432 #if 0 /* local variables moved into u.ba */
66433 VdbeCursor *pC;
66434 #endif /* local variables moved into u.ba */
66435
66436 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66437 u.ba.pC = p->apCsr[pOp->p1];
66438 assert( u.ba.pC!=0 );
66439 if( ALWAYS(u.ba.pC->pCursor!=0) ){
66440 assert( u.ba.pC->isTable );
66441 u.ba.pC->nullRow = 0;
66442 pIn2 = &aMem[pOp->p2];
66443 u.ba.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
66444 u.ba.pC->rowidIsValid = 0;
66445 u.ba.pC->deferredMoveto = 1;
66446 }
66447 break;
66448 }
66449
66450
@@ -66472,66 +67093,66 @@
66472 **
66473 ** See also: Found, NotExists, IsUnique
66474 */
66475 case OP_NotFound: /* jump, in3 */
66476 case OP_Found: { /* jump, in3 */
66477 #if 0 /* local variables moved into u.bb */
66478 int alreadyExists;
66479 VdbeCursor *pC;
66480 int res;
66481 UnpackedRecord *pIdxKey;
66482 UnpackedRecord r;
66483 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
66484 #endif /* local variables moved into u.bb */
66485
66486 #ifdef SQLITE_TEST
66487 sqlite3_found_count++;
66488 #endif
66489
66490 u.bb.alreadyExists = 0;
66491 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66492 assert( pOp->p4type==P4_INT32 );
66493 u.bb.pC = p->apCsr[pOp->p1];
66494 assert( u.bb.pC!=0 );
66495 pIn3 = &aMem[pOp->p3];
66496 if( ALWAYS(u.bb.pC->pCursor!=0) ){
66497
66498 assert( u.bb.pC->isTable==0 );
66499 if( pOp->p4.i>0 ){
66500 u.bb.r.pKeyInfo = u.bb.pC->pKeyInfo;
66501 u.bb.r.nField = (u16)pOp->p4.i;
66502 u.bb.r.aMem = pIn3;
66503 #ifdef SQLITE_DEBUG
66504 { int i; for(i=0; i<u.bb.r.nField; i++) assert( memIsValid(&u.bb.r.aMem[i]) ); }
66505 #endif
66506 u.bb.r.flags = UNPACKED_PREFIX_MATCH;
66507 u.bb.pIdxKey = &u.bb.r;
66508 }else{
66509 assert( pIn3->flags & MEM_Blob );
66510 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
66511 u.bb.pIdxKey = sqlite3VdbeRecordUnpack(u.bb.pC->pKeyInfo, pIn3->n, pIn3->z,
66512 u.bb.aTempRec, sizeof(u.bb.aTempRec));
66513 if( u.bb.pIdxKey==0 ){
66514 goto no_mem;
66515 }
66516 u.bb.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
66517 }
66518 rc = sqlite3BtreeMovetoUnpacked(u.bb.pC->pCursor, u.bb.pIdxKey, 0, 0, &u.bb.res);
66519 if( pOp->p4.i==0 ){
66520 sqlite3VdbeDeleteUnpackedRecord(u.bb.pIdxKey);
66521 }
66522 if( rc!=SQLITE_OK ){
66523 break;
66524 }
66525 u.bb.alreadyExists = (u.bb.res==0);
66526 u.bb.pC->deferredMoveto = 0;
66527 u.bb.pC->cacheStatus = CACHE_STALE;
66528 }
66529 if( pOp->opcode==OP_Found ){
66530 if( u.bb.alreadyExists ) pc = pOp->p2 - 1;
66531 }else{
66532 if( !u.bb.alreadyExists ) pc = pOp->p2 - 1;
66533 }
66534 break;
66535 }
66536
66537 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -66559,67 +67180,67 @@
66559 ** instruction.
66560 **
66561 ** See also: NotFound, NotExists, Found
66562 */
66563 case OP_IsUnique: { /* jump, in3 */
66564 #if 0 /* local variables moved into u.bc */
66565 u16 ii;
66566 VdbeCursor *pCx;
66567 BtCursor *pCrsr;
66568 u16 nField;
66569 Mem *aMx;
66570 UnpackedRecord r; /* B-Tree index search key */
66571 i64 R; /* Rowid stored in register P3 */
66572 #endif /* local variables moved into u.bc */
66573
66574 pIn3 = &aMem[pOp->p3];
66575 u.bc.aMx = &aMem[pOp->p4.i];
66576 /* Assert that the values of parameters P1 and P4 are in range. */
66577 assert( pOp->p4type==P4_INT32 );
66578 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
66579 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66580
66581 /* Find the index cursor. */
66582 u.bc.pCx = p->apCsr[pOp->p1];
66583 assert( u.bc.pCx->deferredMoveto==0 );
66584 u.bc.pCx->seekResult = 0;
66585 u.bc.pCx->cacheStatus = CACHE_STALE;
66586 u.bc.pCrsr = u.bc.pCx->pCursor;
66587
66588 /* If any of the values are NULL, take the jump. */
66589 u.bc.nField = u.bc.pCx->pKeyInfo->nField;
66590 for(u.bc.ii=0; u.bc.ii<u.bc.nField; u.bc.ii++){
66591 if( u.bc.aMx[u.bc.ii].flags & MEM_Null ){
66592 pc = pOp->p2 - 1;
66593 u.bc.pCrsr = 0;
66594 break;
66595 }
66596 }
66597 assert( (u.bc.aMx[u.bc.nField].flags & MEM_Null)==0 );
66598
66599 if( u.bc.pCrsr!=0 ){
66600 /* Populate the index search key. */
66601 u.bc.r.pKeyInfo = u.bc.pCx->pKeyInfo;
66602 u.bc.r.nField = u.bc.nField + 1;
66603 u.bc.r.flags = UNPACKED_PREFIX_SEARCH;
66604 u.bc.r.aMem = u.bc.aMx;
66605 #ifdef SQLITE_DEBUG
66606 { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
66607 #endif
66608
66609 /* Extract the value of u.bc.R from register P3. */
66610 sqlite3VdbeMemIntegerify(pIn3);
66611 u.bc.R = pIn3->u.i;
66612
66613 /* Search the B-Tree index. If no conflicting record is found, jump
66614 ** to P2. Otherwise, copy the rowid of the conflicting record to
66615 ** register P3 and fall through to the next instruction. */
66616 rc = sqlite3BtreeMovetoUnpacked(u.bc.pCrsr, &u.bc.r, 0, 0, &u.bc.pCx->seekResult);
66617 if( (u.bc.r.flags & UNPACKED_PREFIX_SEARCH) || u.bc.r.rowid==u.bc.R ){
66618 pc = pOp->p2 - 1;
66619 }else{
66620 pIn3->u.i = u.bc.r.rowid;
66621 }
66622 }
66623 break;
66624 }
66625
@@ -66636,46 +67257,46 @@
66636 ** P1 is an index.
66637 **
66638 ** See also: Found, NotFound, IsUnique
66639 */
66640 case OP_NotExists: { /* jump, in3 */
66641 #if 0 /* local variables moved into u.bd */
66642 VdbeCursor *pC;
66643 BtCursor *pCrsr;
66644 int res;
66645 u64 iKey;
66646 #endif /* local variables moved into u.bd */
66647
66648 pIn3 = &aMem[pOp->p3];
66649 assert( pIn3->flags & MEM_Int );
66650 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66651 u.bd.pC = p->apCsr[pOp->p1];
66652 assert( u.bd.pC!=0 );
66653 assert( u.bd.pC->isTable );
66654 assert( u.bd.pC->pseudoTableReg==0 );
66655 u.bd.pCrsr = u.bd.pC->pCursor;
66656 if( u.bd.pCrsr!=0 ){
66657 u.bd.res = 0;
66658 u.bd.iKey = pIn3->u.i;
66659 rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, 0, u.bd.iKey, 0, &u.bd.res);
66660 u.bd.pC->lastRowid = pIn3->u.i;
66661 u.bd.pC->rowidIsValid = u.bd.res==0 ?1:0;
66662 u.bd.pC->nullRow = 0;
66663 u.bd.pC->cacheStatus = CACHE_STALE;
66664 u.bd.pC->deferredMoveto = 0;
66665 if( u.bd.res!=0 ){
66666 pc = pOp->p2 - 1;
66667 assert( u.bd.pC->rowidIsValid==0 );
66668 }
66669 u.bd.pC->seekResult = u.bd.res;
66670 }else{
66671 /* This happens when an attempt to open a read cursor on the
66672 ** sqlite_master table returns SQLITE_EMPTY.
66673 */
66674 pc = pOp->p2 - 1;
66675 assert( u.bd.pC->rowidIsValid==0 );
66676 u.bd.pC->seekResult = 0;
66677 }
66678 break;
66679 }
66680
66681 /* Opcode: Sequence P1 P2 * * *
@@ -66706,25 +67327,25 @@
66706 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
66707 ** generated record number. This P3 mechanism is used to help implement the
66708 ** AUTOINCREMENT feature.
66709 */
66710 case OP_NewRowid: { /* out2-prerelease */
66711 #if 0 /* local variables moved into u.be */
66712 i64 v; /* The new rowid */
66713 VdbeCursor *pC; /* Cursor of table to get the new rowid */
66714 int res; /* Result of an sqlite3BtreeLast() */
66715 int cnt; /* Counter to limit the number of searches */
66716 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
66717 VdbeFrame *pFrame; /* Root frame of VDBE */
66718 #endif /* local variables moved into u.be */
66719
66720 u.be.v = 0;
66721 u.be.res = 0;
66722 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66723 u.be.pC = p->apCsr[pOp->p1];
66724 assert( u.be.pC!=0 );
66725 if( NEVER(u.be.pC->pCursor==0) ){
66726 /* The zero initialization above is all that is needed */
66727 }else{
66728 /* The next rowid or record number (different terms for the same
66729 ** thing) is obtained in a two-step algorithm.
66730 **
@@ -66736,11 +67357,11 @@
66736 ** The second algorithm is to select a rowid at random and see if
66737 ** it already exists in the table. If it does not exist, we have
66738 ** succeeded. If the random rowid does exist, we select a new one
66739 ** and try again, up to 100 times.
66740 */
66741 assert( u.be.pC->isTable );
66742
66743 #ifdef SQLITE_32BIT_ROWID
66744 # define MAX_ROWID 0x7fffffff
66745 #else
66746 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -66748,101 +67369,101 @@
66748 ** to provide the constant while making all compilers happy.
66749 */
66750 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
66751 #endif
66752
66753 if( !u.be.pC->useRandomRowid ){
66754 u.be.v = sqlite3BtreeGetCachedRowid(u.be.pC->pCursor);
66755 if( u.be.v==0 ){
66756 rc = sqlite3BtreeLast(u.be.pC->pCursor, &u.be.res);
66757 if( rc!=SQLITE_OK ){
66758 goto abort_due_to_error;
66759 }
66760 if( u.be.res ){
66761 u.be.v = 1; /* IMP: R-61914-48074 */
66762 }else{
66763 assert( sqlite3BtreeCursorIsValid(u.be.pC->pCursor) );
66764 rc = sqlite3BtreeKeySize(u.be.pC->pCursor, &u.be.v);
66765 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
66766 if( u.be.v==MAX_ROWID ){
66767 u.be.pC->useRandomRowid = 1;
66768 }else{
66769 u.be.v++; /* IMP: R-29538-34987 */
66770 }
66771 }
66772 }
66773
66774 #ifndef SQLITE_OMIT_AUTOINCREMENT
66775 if( pOp->p3 ){
66776 /* Assert that P3 is a valid memory cell. */
66777 assert( pOp->p3>0 );
66778 if( p->pFrame ){
66779 for(u.be.pFrame=p->pFrame; u.be.pFrame->pParent; u.be.pFrame=u.be.pFrame->pParent);
66780 /* Assert that P3 is a valid memory cell. */
66781 assert( pOp->p3<=u.be.pFrame->nMem );
66782 u.be.pMem = &u.be.pFrame->aMem[pOp->p3];
66783 }else{
66784 /* Assert that P3 is a valid memory cell. */
66785 assert( pOp->p3<=p->nMem );
66786 u.be.pMem = &aMem[pOp->p3];
66787 memAboutToChange(p, u.be.pMem);
66788 }
66789 assert( memIsValid(u.be.pMem) );
66790
66791 REGISTER_TRACE(pOp->p3, u.be.pMem);
66792 sqlite3VdbeMemIntegerify(u.be.pMem);
66793 assert( (u.be.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
66794 if( u.be.pMem->u.i==MAX_ROWID || u.be.pC->useRandomRowid ){
66795 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
66796 goto abort_due_to_error;
66797 }
66798 if( u.be.v<u.be.pMem->u.i+1 ){
66799 u.be.v = u.be.pMem->u.i + 1;
66800 }
66801 u.be.pMem->u.i = u.be.v;
66802 }
66803 #endif
66804
66805 sqlite3BtreeSetCachedRowid(u.be.pC->pCursor, u.be.v<MAX_ROWID ? u.be.v+1 : 0);
66806 }
66807 if( u.be.pC->useRandomRowid ){
66808 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
66809 ** largest possible integer (9223372036854775807) then the database
66810 ** engine starts picking positive candidate ROWIDs at random until
66811 ** it finds one that is not previously used. */
66812 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
66813 ** an AUTOINCREMENT table. */
66814 /* on the first attempt, simply do one more than previous */
66815 u.be.v = lastRowid;
66816 u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66817 u.be.v++; /* ensure non-zero */
66818 u.be.cnt = 0;
66819 while( ((rc = sqlite3BtreeMovetoUnpacked(u.be.pC->pCursor, 0, (u64)u.be.v,
66820 0, &u.be.res))==SQLITE_OK)
66821 && (u.be.res==0)
66822 && (++u.be.cnt<100)){
66823 /* collision - try another random rowid */
66824 sqlite3_randomness(sizeof(u.be.v), &u.be.v);
66825 if( u.be.cnt<5 ){
66826 /* try "small" random rowids for the initial attempts */
66827 u.be.v &= 0xffffff;
66828 }else{
66829 u.be.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
66830 }
66831 u.be.v++; /* ensure non-zero */
66832 }
66833 if( rc==SQLITE_OK && u.be.res==0 ){
66834 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
66835 goto abort_due_to_error;
66836 }
66837 assert( u.be.v>0 ); /* EV: R-40812-03570 */
66838 }
66839 u.be.pC->rowidIsValid = 0;
66840 u.be.pC->deferredMoveto = 0;
66841 u.be.pC->cacheStatus = CACHE_STALE;
66842 }
66843 pOut->u.i = u.be.v;
66844 break;
66845 }
66846
66847 /* Opcode: Insert P1 P2 P3 P4 P5
66848 **
@@ -66888,74 +67509,74 @@
66888 ** This works exactly like OP_Insert except that the key is the
66889 ** integer value P3, not the value of the integer stored in register P3.
66890 */
66891 case OP_Insert:
66892 case OP_InsertInt: {
66893 #if 0 /* local variables moved into u.bf */
66894 Mem *pData; /* MEM cell holding data for the record to be inserted */
66895 Mem *pKey; /* MEM cell holding key for the record */
66896 i64 iKey; /* The integer ROWID or key for the record to be inserted */
66897 VdbeCursor *pC; /* Cursor to table into which insert is written */
66898 int nZero; /* Number of zero-bytes to append */
66899 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
66900 const char *zDb; /* database name - used by the update hook */
66901 const char *zTbl; /* Table name - used by the opdate hook */
66902 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
66903 #endif /* local variables moved into u.bf */
66904
66905 u.bf.pData = &aMem[pOp->p2];
66906 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66907 assert( memIsValid(u.bf.pData) );
66908 u.bf.pC = p->apCsr[pOp->p1];
66909 assert( u.bf.pC!=0 );
66910 assert( u.bf.pC->pCursor!=0 );
66911 assert( u.bf.pC->pseudoTableReg==0 );
66912 assert( u.bf.pC->isTable );
66913 REGISTER_TRACE(pOp->p2, u.bf.pData);
66914
66915 if( pOp->opcode==OP_Insert ){
66916 u.bf.pKey = &aMem[pOp->p3];
66917 assert( u.bf.pKey->flags & MEM_Int );
66918 assert( memIsValid(u.bf.pKey) );
66919 REGISTER_TRACE(pOp->p3, u.bf.pKey);
66920 u.bf.iKey = u.bf.pKey->u.i;
66921 }else{
66922 assert( pOp->opcode==OP_InsertInt );
66923 u.bf.iKey = pOp->p3;
66924 }
66925
66926 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
66927 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bf.iKey;
66928 if( u.bf.pData->flags & MEM_Null ){
66929 u.bf.pData->z = 0;
66930 u.bf.pData->n = 0;
66931 }else{
66932 assert( u.bf.pData->flags & (MEM_Blob|MEM_Str) );
66933 }
66934 u.bf.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bf.pC->seekResult : 0);
66935 if( u.bf.pData->flags & MEM_Zero ){
66936 u.bf.nZero = u.bf.pData->u.nZero;
66937 }else{
66938 u.bf.nZero = 0;
66939 }
66940 sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, 0);
66941 rc = sqlite3BtreeInsert(u.bf.pC->pCursor, 0, u.bf.iKey,
66942 u.bf.pData->z, u.bf.pData->n, u.bf.nZero,
66943 pOp->p5 & OPFLAG_APPEND, u.bf.seekResult
66944 );
66945 u.bf.pC->rowidIsValid = 0;
66946 u.bf.pC->deferredMoveto = 0;
66947 u.bf.pC->cacheStatus = CACHE_STALE;
66948
66949 /* Invoke the update-hook if required. */
66950 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
66951 u.bf.zDb = db->aDb[u.bf.pC->iDb].zName;
66952 u.bf.zTbl = pOp->p4.z;
66953 u.bf.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
66954 assert( u.bf.pC->isTable );
66955 db->xUpdateCallback(db->pUpdateArg, u.bf.op, u.bf.zDb, u.bf.zTbl, u.bf.iKey);
66956 assert( u.bf.pC->iDb>=0 );
66957 }
66958 break;
66959 }
66960
66961 /* Opcode: Delete P1 P2 * P4 *
@@ -66977,51 +67598,51 @@
66977 ** pointing to. The update hook will be invoked, if it exists.
66978 ** If P4 is not NULL then the P1 cursor must have been positioned
66979 ** using OP_NotFound prior to invoking this opcode.
66980 */
66981 case OP_Delete: {
66982 #if 0 /* local variables moved into u.bg */
66983 i64 iKey;
66984 VdbeCursor *pC;
66985 #endif /* local variables moved into u.bg */
66986
66987 u.bg.iKey = 0;
66988 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66989 u.bg.pC = p->apCsr[pOp->p1];
66990 assert( u.bg.pC!=0 );
66991 assert( u.bg.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
66992
66993 /* If the update-hook will be invoked, set u.bg.iKey to the rowid of the
66994 ** row being deleted.
66995 */
66996 if( db->xUpdateCallback && pOp->p4.z ){
66997 assert( u.bg.pC->isTable );
66998 assert( u.bg.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
66999 u.bg.iKey = u.bg.pC->lastRowid;
67000 }
67001
67002 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67003 ** OP_Column on the same table without any intervening operations that
67004 ** might move or invalidate the cursor. Hence cursor u.bg.pC is always pointing
67005 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67006 ** below is always a no-op and cannot fail. We will run it anyhow, though,
67007 ** to guard against future changes to the code generator.
67008 **/
67009 assert( u.bg.pC->deferredMoveto==0 );
67010 rc = sqlite3VdbeCursorMoveto(u.bg.pC);
67011 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67012
67013 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67014 rc = sqlite3BtreeDelete(u.bg.pC->pCursor);
67015 u.bg.pC->cacheStatus = CACHE_STALE;
67016
67017 /* Invoke the update-hook if required. */
67018 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67019 const char *zDb = db->aDb[u.bg.pC->iDb].zName;
67020 const char *zTbl = pOp->p4.z;
67021 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bg.iKey);
67022 assert( u.bg.pC->iDb>=0 );
67023 }
67024 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67025 break;
67026 }
67027 /* Opcode: ResetCount * * * * *
@@ -67034,10 +67655,53 @@
67034 case OP_ResetCount: {
67035 sqlite3VdbeSetChanges(db, p->nChange);
67036 p->nChange = 0;
67037 break;
67038 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67039
67040 /* Opcode: RowData P1 P2 * * *
67041 **
67042 ** Write into register P2 the complete row data for cursor P1.
67043 ** There is no interpretation of the data.
@@ -67057,65 +67721,67 @@
67057 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
67058 ** of a real table, not a pseudo-table.
67059 */
67060 case OP_RowKey:
67061 case OP_RowData: {
67062 #if 0 /* local variables moved into u.bh */
67063 VdbeCursor *pC;
67064 BtCursor *pCrsr;
67065 u32 n;
67066 i64 n64;
67067 #endif /* local variables moved into u.bh */
67068
67069 pOut = &aMem[pOp->p2];
67070 memAboutToChange(p, pOut);
67071
67072 /* Note that RowKey and RowData are really exactly the same instruction */
67073 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67074 u.bh.pC = p->apCsr[pOp->p1];
67075 assert( u.bh.pC->isTable || pOp->opcode==OP_RowKey );
67076 assert( u.bh.pC->isIndex || pOp->opcode==OP_RowData );
67077 assert( u.bh.pC!=0 );
67078 assert( u.bh.pC->nullRow==0 );
67079 assert( u.bh.pC->pseudoTableReg==0 );
67080 assert( u.bh.pC->pCursor!=0 );
67081 u.bh.pCrsr = u.bh.pC->pCursor;
67082 assert( sqlite3BtreeCursorIsValid(u.bh.pCrsr) );
 
 
67083
67084 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67085 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67086 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
67087 ** a no-op and can never fail. But we leave it in place as a safety.
67088 */
67089 assert( u.bh.pC->deferredMoveto==0 );
67090 rc = sqlite3VdbeCursorMoveto(u.bh.pC);
67091 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67092
67093 if( u.bh.pC->isIndex ){
67094 assert( !u.bh.pC->isTable );
67095 rc = sqlite3BtreeKeySize(u.bh.pCrsr, &u.bh.n64);
67096 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67097 if( u.bh.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67098 goto too_big;
67099 }
67100 u.bh.n = (u32)u.bh.n64;
67101 }else{
67102 rc = sqlite3BtreeDataSize(u.bh.pCrsr, &u.bh.n);
67103 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67104 if( u.bh.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67105 goto too_big;
67106 }
67107 }
67108 if( sqlite3VdbeMemGrow(pOut, u.bh.n, 0) ){
67109 goto no_mem;
67110 }
67111 pOut->n = u.bh.n;
67112 MemSetTypeFlag(pOut, MEM_Blob);
67113 if( u.bh.pC->isIndex ){
67114 rc = sqlite3BtreeKey(u.bh.pCrsr, 0, u.bh.n, pOut->z);
67115 }else{
67116 rc = sqlite3BtreeData(u.bh.pCrsr, 0, u.bh.n, pOut->z);
67117 }
67118 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
67119 UPDATE_MAX_BLOBSIZE(pOut);
67120 break;
67121 }
@@ -67128,46 +67794,46 @@
67128 ** P1 can be either an ordinary table or a virtual table. There used to
67129 ** be a separate OP_VRowid opcode for use with virtual tables, but this
67130 ** one opcode now works for both table types.
67131 */
67132 case OP_Rowid: { /* out2-prerelease */
67133 #if 0 /* local variables moved into u.bi */
67134 VdbeCursor *pC;
67135 i64 v;
67136 sqlite3_vtab *pVtab;
67137 const sqlite3_module *pModule;
67138 #endif /* local variables moved into u.bi */
67139
67140 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67141 u.bi.pC = p->apCsr[pOp->p1];
67142 assert( u.bi.pC!=0 );
67143 assert( u.bi.pC->pseudoTableReg==0 );
67144 if( u.bi.pC->nullRow ){
67145 pOut->flags = MEM_Null;
67146 break;
67147 }else if( u.bi.pC->deferredMoveto ){
67148 u.bi.v = u.bi.pC->movetoTarget;
67149 #ifndef SQLITE_OMIT_VIRTUALTABLE
67150 }else if( u.bi.pC->pVtabCursor ){
67151 u.bi.pVtab = u.bi.pC->pVtabCursor->pVtab;
67152 u.bi.pModule = u.bi.pVtab->pModule;
67153 assert( u.bi.pModule->xRowid );
67154 rc = u.bi.pModule->xRowid(u.bi.pC->pVtabCursor, &u.bi.v);
67155 importVtabErrMsg(p, u.bi.pVtab);
67156 #endif /* SQLITE_OMIT_VIRTUALTABLE */
67157 }else{
67158 assert( u.bi.pC->pCursor!=0 );
67159 rc = sqlite3VdbeCursorMoveto(u.bi.pC);
67160 if( rc ) goto abort_due_to_error;
67161 if( u.bi.pC->rowidIsValid ){
67162 u.bi.v = u.bi.pC->lastRowid;
67163 }else{
67164 rc = sqlite3BtreeKeySize(u.bi.pC->pCursor, &u.bi.v);
67165 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
67166 }
67167 }
67168 pOut->u.i = u.bi.v;
67169 break;
67170 }
67171
67172 /* Opcode: NullRow P1 * * * *
67173 **
@@ -67174,21 +67840,22 @@
67174 ** Move the cursor P1 to a null row. Any OP_Column operations
67175 ** that occur while the cursor is on the null row will always
67176 ** write a NULL.
67177 */
67178 case OP_NullRow: {
67179 #if 0 /* local variables moved into u.bj */
67180 VdbeCursor *pC;
67181 #endif /* local variables moved into u.bj */
67182
67183 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67184 u.bj.pC = p->apCsr[pOp->p1];
67185 assert( u.bj.pC!=0 );
67186 u.bj.pC->nullRow = 1;
67187 u.bj.pC->rowidIsValid = 0;
67188 if( u.bj.pC->pCursor ){
67189 sqlite3BtreeClearCursor(u.bj.pC->pCursor);
 
67190 }
67191 break;
67192 }
67193
67194 /* Opcode: Last P1 P2 * * *
@@ -67198,30 +67865,30 @@
67198 ** If the table or index is empty and P2>0, then jump immediately to P2.
67199 ** If P2 is 0 or if the table or index is not empty, fall through
67200 ** to the following instruction.
67201 */
67202 case OP_Last: { /* jump */
67203 #if 0 /* local variables moved into u.bk */
67204 VdbeCursor *pC;
67205 BtCursor *pCrsr;
67206 int res;
67207 #endif /* local variables moved into u.bk */
67208
67209 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67210 u.bk.pC = p->apCsr[pOp->p1];
67211 assert( u.bk.pC!=0 );
67212 u.bk.pCrsr = u.bk.pC->pCursor;
67213 if( u.bk.pCrsr==0 ){
67214 u.bk.res = 1;
67215 }else{
67216 rc = sqlite3BtreeLast(u.bk.pCrsr, &u.bk.res);
67217 }
67218 u.bk.pC->nullRow = (u8)u.bk.res;
67219 u.bk.pC->deferredMoveto = 0;
67220 u.bk.pC->rowidIsValid = 0;
67221 u.bk.pC->cacheStatus = CACHE_STALE;
67222 if( pOp->p2>0 && u.bk.res ){
67223 pc = pOp->p2 - 1;
67224 }
67225 break;
67226 }
67227
@@ -67236,10 +67903,14 @@
67236 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
67237 ** rewinding so that the global variable will be incremented and
67238 ** regression tests can determine whether or not the optimizer is
67239 ** correctly optimizing out sorts.
67240 */
 
 
 
 
67241 case OP_Sort: { /* jump */
67242 #ifdef SQLITE_TEST
67243 sqlite3_sort_count++;
67244 sqlite3_search_count--;
67245 #endif
@@ -67253,43 +67924,51 @@
67253 ** If the table or index is empty and P2>0, then jump immediately to P2.
67254 ** If P2 is 0 or if the table or index is not empty, fall through
67255 ** to the following instruction.
67256 */
67257 case OP_Rewind: { /* jump */
67258 #if 0 /* local variables moved into u.bl */
67259 VdbeCursor *pC;
67260 BtCursor *pCrsr;
67261 int res;
67262 #endif /* local variables moved into u.bl */
67263
67264 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67265 u.bl.pC = p->apCsr[pOp->p1];
67266 assert( u.bl.pC!=0 );
67267 u.bl.res = 1;
67268 if( (u.bl.pCrsr = u.bl.pC->pCursor)!=0 ){
67269 rc = sqlite3BtreeFirst(u.bl.pCrsr, &u.bl.res);
67270 u.bl.pC->atFirst = u.bl.res==0 ?1:0;
67271 u.bl.pC->deferredMoveto = 0;
67272 u.bl.pC->cacheStatus = CACHE_STALE;
67273 u.bl.pC->rowidIsValid = 0;
67274 }
67275 u.bl.pC->nullRow = (u8)u.bl.res;
 
 
 
 
 
67276 assert( pOp->p2>0 && pOp->p2<p->nOp );
67277 if( u.bl.res ){
67278 pc = pOp->p2 - 1;
67279 }
67280 break;
67281 }
67282
67283 /* Opcode: Next P1 P2 * * P5
67284 **
67285 ** Advance cursor P1 so that it points to the next key/data pair in its
67286 ** table or index. If there are no more key/value pairs then fall through
67287 ** to the following instruction. But if the cursor advance was successful,
67288 ** jump immediately to P2.
67289 **
67290 ** The P1 cursor must be for a real table, not a pseudo-table.
 
 
 
67291 **
67292 ** If P5 is positive and the jump is taken, then event counter
67293 ** number P5-1 in the prepared statement is incremented.
67294 **
67295 ** See also: Prev
@@ -67300,48 +67979,57 @@
67300 ** table or index. If there is no previous key/value pairs then fall through
67301 ** to the following instruction. But if the cursor backup was successful,
67302 ** jump immediately to P2.
67303 **
67304 ** The P1 cursor must be for a real table, not a pseudo-table.
 
 
 
67305 **
67306 ** If P5 is positive and the jump is taken, then event counter
67307 ** number P5-1 in the prepared statement is incremented.
67308 */
 
 
 
 
67309 case OP_Prev: /* jump */
67310 case OP_Next: { /* jump */
67311 #if 0 /* local variables moved into u.bm */
67312 VdbeCursor *pC;
67313 BtCursor *pCrsr;
67314 int res;
67315 #endif /* local variables moved into u.bm */
67316
67317 CHECK_FOR_INTERRUPT;
67318 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67319 assert( pOp->p5<=ArraySize(p->aCounter) );
67320 u.bm.pC = p->apCsr[pOp->p1];
67321 if( u.bm.pC==0 ){
67322 break; /* See ticket #2273 */
67323 }
67324 u.bm.pCrsr = u.bm.pC->pCursor;
67325 if( u.bm.pCrsr==0 ){
67326 u.bm.pC->nullRow = 1;
67327 break;
67328 }
67329 u.bm.res = 1;
67330 assert( u.bm.pC->deferredMoveto==0 );
67331 rc = pOp->opcode==OP_Next ? sqlite3BtreeNext(u.bm.pCrsr, &u.bm.res) :
67332 sqlite3BtreePrevious(u.bm.pCrsr, &u.bm.res);
67333 u.bm.pC->nullRow = (u8)u.bm.res;
67334 u.bm.pC->cacheStatus = CACHE_STALE;
67335 if( u.bm.res==0 ){
 
 
 
67336 pc = pOp->p2 - 1;
67337 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
67338 #ifdef SQLITE_TEST
67339 sqlite3_search_count++;
67340 #endif
67341 }
67342 u.bm.pC->rowidIsValid = 0;
67343 break;
67344 }
67345
67346 /* Opcode: IdxInsert P1 P2 P3 * P5
67347 **
@@ -67353,35 +68041,44 @@
67353 ** insert is likely to be an append.
67354 **
67355 ** This instruction only works for indices. The equivalent instruction
67356 ** for tables is OP_Insert.
67357 */
 
 
 
 
67358 case OP_IdxInsert: { /* in2 */
67359 #if 0 /* local variables moved into u.bn */
67360 VdbeCursor *pC;
67361 BtCursor *pCrsr;
67362 int nKey;
67363 const char *zKey;
67364 #endif /* local variables moved into u.bn */
67365
67366 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67367 u.bn.pC = p->apCsr[pOp->p1];
67368 assert( u.bn.pC!=0 );
 
67369 pIn2 = &aMem[pOp->p2];
67370 assert( pIn2->flags & MEM_Blob );
67371 u.bn.pCrsr = u.bn.pC->pCursor;
67372 if( ALWAYS(u.bn.pCrsr!=0) ){
67373 assert( u.bn.pC->isTable==0 );
67374 rc = ExpandBlob(pIn2);
67375 if( rc==SQLITE_OK ){
67376 u.bn.nKey = pIn2->n;
67377 u.bn.zKey = pIn2->z;
67378 rc = sqlite3BtreeInsert(u.bn.pCrsr, u.bn.zKey, u.bn.nKey, "", 0, 0, pOp->p3,
67379 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bn.pC->seekResult : 0)
67380 );
67381 assert( u.bn.pC->deferredMoveto==0 );
67382 u.bn.pC->cacheStatus = CACHE_STALE;
 
 
 
 
67383 }
67384 }
67385 break;
67386 }
67387
@@ -67390,37 +68087,37 @@
67390 ** The content of P3 registers starting at register P2 form
67391 ** an unpacked index key. This opcode removes that entry from the
67392 ** index opened by cursor P1.
67393 */
67394 case OP_IdxDelete: {
67395 #if 0 /* local variables moved into u.bo */
67396 VdbeCursor *pC;
67397 BtCursor *pCrsr;
67398 int res;
67399 UnpackedRecord r;
67400 #endif /* local variables moved into u.bo */
67401
67402 assert( pOp->p3>0 );
67403 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
67404 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67405 u.bo.pC = p->apCsr[pOp->p1];
67406 assert( u.bo.pC!=0 );
67407 u.bo.pCrsr = u.bo.pC->pCursor;
67408 if( ALWAYS(u.bo.pCrsr!=0) ){
67409 u.bo.r.pKeyInfo = u.bo.pC->pKeyInfo;
67410 u.bo.r.nField = (u16)pOp->p3;
67411 u.bo.r.flags = 0;
67412 u.bo.r.aMem = &aMem[pOp->p2];
67413 #ifdef SQLITE_DEBUG
67414 { int i; for(i=0; i<u.bo.r.nField; i++) assert( memIsValid(&u.bo.r.aMem[i]) ); }
67415 #endif
67416 rc = sqlite3BtreeMovetoUnpacked(u.bo.pCrsr, &u.bo.r, 0, 0, &u.bo.res);
67417 if( rc==SQLITE_OK && u.bo.res==0 ){
67418 rc = sqlite3BtreeDelete(u.bo.pCrsr);
67419 }
67420 assert( u.bo.pC->deferredMoveto==0 );
67421 u.bo.pC->cacheStatus = CACHE_STALE;
67422 }
67423 break;
67424 }
67425
67426 /* Opcode: IdxRowid P1 P2 * * *
@@ -67430,32 +68127,32 @@
67430 ** the rowid of the table entry to which this index entry points.
67431 **
67432 ** See also: Rowid, MakeRecord.
67433 */
67434 case OP_IdxRowid: { /* out2-prerelease */
67435 #if 0 /* local variables moved into u.bp */
67436 BtCursor *pCrsr;
67437 VdbeCursor *pC;
67438 i64 rowid;
67439 #endif /* local variables moved into u.bp */
67440
67441 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67442 u.bp.pC = p->apCsr[pOp->p1];
67443 assert( u.bp.pC!=0 );
67444 u.bp.pCrsr = u.bp.pC->pCursor;
67445 pOut->flags = MEM_Null;
67446 if( ALWAYS(u.bp.pCrsr!=0) ){
67447 rc = sqlite3VdbeCursorMoveto(u.bp.pC);
67448 if( NEVER(rc) ) goto abort_due_to_error;
67449 assert( u.bp.pC->deferredMoveto==0 );
67450 assert( u.bp.pC->isTable==0 );
67451 if( !u.bp.pC->nullRow ){
67452 rc = sqlite3VdbeIdxRowid(db, u.bp.pCrsr, &u.bp.rowid);
67453 if( rc!=SQLITE_OK ){
67454 goto abort_due_to_error;
67455 }
67456 pOut->u.i = u.bp.rowid;
67457 pOut->flags = MEM_Int;
67458 }
67459 }
67460 break;
67461 }
@@ -67486,43 +68183,43 @@
67486 ** If P5 is non-zero then the key value is increased by an epsilon prior
67487 ** to the comparison. This makes the opcode work like IdxLE.
67488 */
67489 case OP_IdxLT: /* jump */
67490 case OP_IdxGE: { /* jump */
67491 #if 0 /* local variables moved into u.bq */
67492 VdbeCursor *pC;
67493 int res;
67494 UnpackedRecord r;
67495 #endif /* local variables moved into u.bq */
67496
67497 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67498 u.bq.pC = p->apCsr[pOp->p1];
67499 assert( u.bq.pC!=0 );
67500 assert( u.bq.pC->isOrdered );
67501 if( ALWAYS(u.bq.pC->pCursor!=0) ){
67502 assert( u.bq.pC->deferredMoveto==0 );
67503 assert( pOp->p5==0 || pOp->p5==1 );
67504 assert( pOp->p4type==P4_INT32 );
67505 u.bq.r.pKeyInfo = u.bq.pC->pKeyInfo;
67506 u.bq.r.nField = (u16)pOp->p4.i;
67507 if( pOp->p5 ){
67508 u.bq.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
67509 }else{
67510 u.bq.r.flags = UNPACKED_IGNORE_ROWID;
67511 }
67512 u.bq.r.aMem = &aMem[pOp->p3];
67513 #ifdef SQLITE_DEBUG
67514 { int i; for(i=0; i<u.bq.r.nField; i++) assert( memIsValid(&u.bq.r.aMem[i]) ); }
67515 #endif
67516 rc = sqlite3VdbeIdxKeyCompare(u.bq.pC, &u.bq.r, &u.bq.res);
67517 if( pOp->opcode==OP_IdxLT ){
67518 u.bq.res = -u.bq.res;
67519 }else{
67520 assert( pOp->opcode==OP_IdxGE );
67521 u.bq.res++;
67522 }
67523 if( u.bq.res>0 ){
67524 pc = pOp->p2 - 1 ;
67525 }
67526 }
67527 break;
67528 }
@@ -67546,43 +68243,43 @@
67546 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
67547 **
67548 ** See also: Clear
67549 */
67550 case OP_Destroy: { /* out2-prerelease */
67551 #if 0 /* local variables moved into u.br */
67552 int iMoved;
67553 int iCnt;
67554 Vdbe *pVdbe;
67555 int iDb;
67556 #endif /* local variables moved into u.br */
67557 #ifndef SQLITE_OMIT_VIRTUALTABLE
67558 u.br.iCnt = 0;
67559 for(u.br.pVdbe=db->pVdbe; u.br.pVdbe; u.br.pVdbe = u.br.pVdbe->pNext){
67560 if( u.br.pVdbe->magic==VDBE_MAGIC_RUN && u.br.pVdbe->inVtabMethod<2 && u.br.pVdbe->pc>=0 ){
67561 u.br.iCnt++;
67562 }
67563 }
67564 #else
67565 u.br.iCnt = db->activeVdbeCnt;
67566 #endif
67567 pOut->flags = MEM_Null;
67568 if( u.br.iCnt>1 ){
67569 rc = SQLITE_LOCKED;
67570 p->errorAction = OE_Abort;
67571 }else{
67572 u.br.iDb = pOp->p3;
67573 assert( u.br.iCnt==1 );
67574 assert( (p->btreeMask & (((yDbMask)1)<<u.br.iDb))!=0 );
67575 rc = sqlite3BtreeDropTable(db->aDb[u.br.iDb].pBt, pOp->p1, &u.br.iMoved);
67576 pOut->flags = MEM_Int;
67577 pOut->u.i = u.br.iMoved;
67578 #ifndef SQLITE_OMIT_AUTOVACUUM
67579 if( rc==SQLITE_OK && u.br.iMoved!=0 ){
67580 sqlite3RootPageMoved(db, u.br.iDb, u.br.iMoved, pOp->p1);
67581 /* All OP_Destroy operations occur on the same btree */
67582 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.br.iDb+1 );
67583 resetSchemaOnFault = u.br.iDb+1;
67584 }
67585 #endif
67586 }
67587 break;
67588 }
@@ -67604,25 +68301,25 @@
67604 ** also incremented by the number of rows in the table being cleared.
67605 **
67606 ** See also: Destroy
67607 */
67608 case OP_Clear: {
67609 #if 0 /* local variables moved into u.bs */
67610 int nChange;
67611 #endif /* local variables moved into u.bs */
67612
67613 u.bs.nChange = 0;
67614 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
67615 rc = sqlite3BtreeClearTable(
67616 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bs.nChange : 0)
67617 );
67618 if( pOp->p3 ){
67619 p->nChange += u.bs.nChange;
67620 if( pOp->p3>0 ){
67621 assert( memIsValid(&aMem[pOp->p3]) );
67622 memAboutToChange(p, &aMem[pOp->p3]);
67623 aMem[pOp->p3].u.i += u.bs.nChange;
67624 }
67625 }
67626 break;
67627 }
67628
@@ -67648,29 +68345,29 @@
67648 **
67649 ** See documentation on OP_CreateTable for additional information.
67650 */
67651 case OP_CreateIndex: /* out2-prerelease */
67652 case OP_CreateTable: { /* out2-prerelease */
67653 #if 0 /* local variables moved into u.bt */
67654 int pgno;
67655 int flags;
67656 Db *pDb;
67657 #endif /* local variables moved into u.bt */
67658
67659 u.bt.pgno = 0;
67660 assert( pOp->p1>=0 && pOp->p1<db->nDb );
67661 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
67662 u.bt.pDb = &db->aDb[pOp->p1];
67663 assert( u.bt.pDb->pBt!=0 );
67664 if( pOp->opcode==OP_CreateTable ){
67665 /* u.bt.flags = BTREE_INTKEY; */
67666 u.bt.flags = BTREE_INTKEY;
67667 }else{
67668 u.bt.flags = BTREE_BLOBKEY;
67669 }
67670 rc = sqlite3BtreeCreateTable(u.bt.pDb->pBt, &u.bt.pgno, u.bt.flags);
67671 pOut->u.i = u.bt.pgno;
67672 break;
67673 }
67674
67675 /* Opcode: ParseSchema P1 * * P4 *
67676 **
@@ -67679,48 +68376,48 @@
67679 **
67680 ** This opcode invokes the parser to create a new virtual machine,
67681 ** then runs the new virtual machine. It is thus a re-entrant opcode.
67682 */
67683 case OP_ParseSchema: {
67684 #if 0 /* local variables moved into u.bu */
67685 int iDb;
67686 const char *zMaster;
67687 char *zSql;
67688 InitData initData;
67689 #endif /* local variables moved into u.bu */
67690
67691 /* Any prepared statement that invokes this opcode will hold mutexes
67692 ** on every btree. This is a prerequisite for invoking
67693 ** sqlite3InitCallback().
67694 */
67695 #ifdef SQLITE_DEBUG
67696 for(u.bu.iDb=0; u.bu.iDb<db->nDb; u.bu.iDb++){
67697 assert( u.bu.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bu.iDb].pBt) );
67698 }
67699 #endif
67700
67701 u.bu.iDb = pOp->p1;
67702 assert( u.bu.iDb>=0 && u.bu.iDb<db->nDb );
67703 assert( DbHasProperty(db, u.bu.iDb, DB_SchemaLoaded) );
67704 /* Used to be a conditional */ {
67705 u.bu.zMaster = SCHEMA_TABLE(u.bu.iDb);
67706 u.bu.initData.db = db;
67707 u.bu.initData.iDb = pOp->p1;
67708 u.bu.initData.pzErrMsg = &p->zErrMsg;
67709 u.bu.zSql = sqlite3MPrintf(db,
67710 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
67711 db->aDb[u.bu.iDb].zName, u.bu.zMaster, pOp->p4.z);
67712 if( u.bu.zSql==0 ){
67713 rc = SQLITE_NOMEM;
67714 }else{
67715 assert( db->init.busy==0 );
67716 db->init.busy = 1;
67717 u.bu.initData.rc = SQLITE_OK;
67718 assert( !db->mallocFailed );
67719 rc = sqlite3_exec(db, u.bu.zSql, sqlite3InitCallback, &u.bu.initData, 0);
67720 if( rc==SQLITE_OK ) rc = u.bu.initData.rc;
67721 sqlite3DbFree(db, u.bu.zSql);
67722 db->init.busy = 0;
67723 }
67724 }
67725 if( rc==SQLITE_NOMEM ){
67726 goto no_mem;
@@ -67799,45 +68496,45 @@
67799 ** file, not the main database file.
67800 **
67801 ** This opcode is used to implement the integrity_check pragma.
67802 */
67803 case OP_IntegrityCk: {
67804 #if 0 /* local variables moved into u.bv */
67805 int nRoot; /* Number of tables to check. (Number of root pages.) */
67806 int *aRoot; /* Array of rootpage numbers for tables to be checked */
67807 int j; /* Loop counter */
67808 int nErr; /* Number of errors reported */
67809 char *z; /* Text of the error report */
67810 Mem *pnErr; /* Register keeping track of errors remaining */
67811 #endif /* local variables moved into u.bv */
67812
67813 u.bv.nRoot = pOp->p2;
67814 assert( u.bv.nRoot>0 );
67815 u.bv.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.bv.nRoot+1) );
67816 if( u.bv.aRoot==0 ) goto no_mem;
67817 assert( pOp->p3>0 && pOp->p3<=p->nMem );
67818 u.bv.pnErr = &aMem[pOp->p3];
67819 assert( (u.bv.pnErr->flags & MEM_Int)!=0 );
67820 assert( (u.bv.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
67821 pIn1 = &aMem[pOp->p1];
67822 for(u.bv.j=0; u.bv.j<u.bv.nRoot; u.bv.j++){
67823 u.bv.aRoot[u.bv.j] = (int)sqlite3VdbeIntValue(&pIn1[u.bv.j]);
67824 }
67825 u.bv.aRoot[u.bv.j] = 0;
67826 assert( pOp->p5<db->nDb );
67827 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
67828 u.bv.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.bv.aRoot, u.bv.nRoot,
67829 (int)u.bv.pnErr->u.i, &u.bv.nErr);
67830 sqlite3DbFree(db, u.bv.aRoot);
67831 u.bv.pnErr->u.i -= u.bv.nErr;
67832 sqlite3VdbeMemSetNull(pIn1);
67833 if( u.bv.nErr==0 ){
67834 assert( u.bv.z==0 );
67835 }else if( u.bv.z==0 ){
67836 goto no_mem;
67837 }else{
67838 sqlite3VdbeMemSetStr(pIn1, u.bv.z, -1, SQLITE_UTF8, sqlite3_free);
67839 }
67840 UPDATE_MAX_BLOBSIZE(pIn1);
67841 sqlite3VdbeChangeEncoding(pIn1, encoding);
67842 break;
67843 }
@@ -67867,24 +68564,24 @@
67867 ** Extract the smallest value from boolean index P1 and put that value into
67868 ** register P3. Or, if boolean index P1 is initially empty, leave P3
67869 ** unchanged and jump to instruction P2.
67870 */
67871 case OP_RowSetRead: { /* jump, in1, out3 */
67872 #if 0 /* local variables moved into u.bw */
67873 i64 val;
67874 #endif /* local variables moved into u.bw */
67875 CHECK_FOR_INTERRUPT;
67876 pIn1 = &aMem[pOp->p1];
67877 if( (pIn1->flags & MEM_RowSet)==0
67878 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bw.val)==0
67879 ){
67880 /* The boolean index is empty */
67881 sqlite3VdbeMemSetNull(pIn1);
67882 pc = pOp->p2 - 1;
67883 }else{
67884 /* A value was pulled from the index */
67885 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bw.val);
67886 }
67887 break;
67888 }
67889
67890 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -67909,18 +68606,18 @@
67909 ** inserted, there is no need to search to see if the same value was
67910 ** previously inserted as part of set X (only if it was previously
67911 ** inserted as part of some other set).
67912 */
67913 case OP_RowSetTest: { /* jump, in1, in3 */
67914 #if 0 /* local variables moved into u.bx */
67915 int iSet;
67916 int exists;
67917 #endif /* local variables moved into u.bx */
67918
67919 pIn1 = &aMem[pOp->p1];
67920 pIn3 = &aMem[pOp->p3];
67921 u.bx.iSet = pOp->p4.i;
67922 assert( pIn3->flags&MEM_Int );
67923
67924 /* If there is anything other than a rowset object in memory cell P1,
67925 ** delete it now and initialize P1 with an empty rowset
67926 */
@@ -67928,21 +68625,21 @@
67928 sqlite3VdbeMemSetRowSet(pIn1);
67929 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
67930 }
67931
67932 assert( pOp->p4type==P4_INT32 );
67933 assert( u.bx.iSet==-1 || u.bx.iSet>=0 );
67934 if( u.bx.iSet ){
67935 u.bx.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
67936 (u8)(u.bx.iSet>=0 ? u.bx.iSet & 0xf : 0xff),
67937 pIn3->u.i);
67938 if( u.bx.exists ){
67939 pc = pOp->p2 - 1;
67940 break;
67941 }
67942 }
67943 if( u.bx.iSet>=0 ){
67944 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
67945 }
67946 break;
67947 }
67948
@@ -67961,25 +68658,25 @@
67961 ** memory required by the sub-vdbe at runtime.
67962 **
67963 ** P4 is a pointer to the VM containing the trigger program.
67964 */
67965 case OP_Program: { /* jump */
67966 #if 0 /* local variables moved into u.by */
67967 int nMem; /* Number of memory registers for sub-program */
67968 int nByte; /* Bytes of runtime space required for sub-program */
67969 Mem *pRt; /* Register to allocate runtime space */
67970 Mem *pMem; /* Used to iterate through memory cells */
67971 Mem *pEnd; /* Last memory cell in new array */
67972 VdbeFrame *pFrame; /* New vdbe frame to execute in */
67973 SubProgram *pProgram; /* Sub-program to execute */
67974 void *t; /* Token identifying trigger */
67975 #endif /* local variables moved into u.by */
67976
67977 u.by.pProgram = pOp->p4.pProgram;
67978 u.by.pRt = &aMem[pOp->p3];
67979 assert( memIsValid(u.by.pRt) );
67980 assert( u.by.pProgram->nOp>0 );
67981
67982 /* If the p5 flag is clear, then recursive invocation of triggers is
67983 ** disabled for backwards compatibility (p5 is set if this sub-program
67984 ** is really a trigger, not a foreign key action, and the flag set
67985 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -67989,79 +68686,79 @@
67989 ** SubProgram (if the trigger may be executed with more than one different
67990 ** ON CONFLICT algorithm). SubProgram structures associated with a
67991 ** single trigger all have the same value for the SubProgram.token
67992 ** variable. */
67993 if( pOp->p5 ){
67994 u.by.t = u.by.pProgram->token;
67995 for(u.by.pFrame=p->pFrame; u.by.pFrame && u.by.pFrame->token!=u.by.t; u.by.pFrame=u.by.pFrame->pParent);
67996 if( u.by.pFrame ) break;
67997 }
67998
67999 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68000 rc = SQLITE_ERROR;
68001 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68002 break;
68003 }
68004
68005 /* Register u.by.pRt is used to store the memory required to save the state
68006 ** of the current program, and the memory required at runtime to execute
68007 ** the trigger program. If this trigger has been fired before, then u.by.pRt
68008 ** is already allocated. Otherwise, it must be initialized. */
68009 if( (u.by.pRt->flags&MEM_Frame)==0 ){
68010 /* SubProgram.nMem is set to the number of memory cells used by the
68011 ** program stored in SubProgram.aOp. As well as these, one memory
68012 ** cell is required for each cursor used by the program. Set local
68013 ** variable u.by.nMem (and later, VdbeFrame.nChildMem) to this value.
68014 */
68015 u.by.nMem = u.by.pProgram->nMem + u.by.pProgram->nCsr;
68016 u.by.nByte = ROUND8(sizeof(VdbeFrame))
68017 + u.by.nMem * sizeof(Mem)
68018 + u.by.pProgram->nCsr * sizeof(VdbeCursor *);
68019 u.by.pFrame = sqlite3DbMallocZero(db, u.by.nByte);
68020 if( !u.by.pFrame ){
68021 goto no_mem;
68022 }
68023 sqlite3VdbeMemRelease(u.by.pRt);
68024 u.by.pRt->flags = MEM_Frame;
68025 u.by.pRt->u.pFrame = u.by.pFrame;
68026
68027 u.by.pFrame->v = p;
68028 u.by.pFrame->nChildMem = u.by.nMem;
68029 u.by.pFrame->nChildCsr = u.by.pProgram->nCsr;
68030 u.by.pFrame->pc = pc;
68031 u.by.pFrame->aMem = p->aMem;
68032 u.by.pFrame->nMem = p->nMem;
68033 u.by.pFrame->apCsr = p->apCsr;
68034 u.by.pFrame->nCursor = p->nCursor;
68035 u.by.pFrame->aOp = p->aOp;
68036 u.by.pFrame->nOp = p->nOp;
68037 u.by.pFrame->token = u.by.pProgram->token;
68038
68039 u.by.pEnd = &VdbeFrameMem(u.by.pFrame)[u.by.pFrame->nChildMem];
68040 for(u.by.pMem=VdbeFrameMem(u.by.pFrame); u.by.pMem!=u.by.pEnd; u.by.pMem++){
68041 u.by.pMem->flags = MEM_Null;
68042 u.by.pMem->db = db;
68043 }
68044 }else{
68045 u.by.pFrame = u.by.pRt->u.pFrame;
68046 assert( u.by.pProgram->nMem+u.by.pProgram->nCsr==u.by.pFrame->nChildMem );
68047 assert( u.by.pProgram->nCsr==u.by.pFrame->nChildCsr );
68048 assert( pc==u.by.pFrame->pc );
68049 }
68050
68051 p->nFrame++;
68052 u.by.pFrame->pParent = p->pFrame;
68053 u.by.pFrame->lastRowid = lastRowid;
68054 u.by.pFrame->nChange = p->nChange;
68055 p->nChange = 0;
68056 p->pFrame = u.by.pFrame;
68057 p->aMem = aMem = &VdbeFrameMem(u.by.pFrame)[-1];
68058 p->nMem = u.by.pFrame->nChildMem;
68059 p->nCursor = (u16)u.by.pFrame->nChildCsr;
68060 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68061 p->aOp = aOp = u.by.pProgram->aOp;
68062 p->nOp = u.by.pProgram->nOp;
68063 pc = -1;
68064
68065 break;
68066 }
68067
@@ -68076,17 +68773,17 @@
68076 ** The address of the cell in the parent frame is determined by adding
68077 ** the value of the P1 argument to the value of the P1 argument to the
68078 ** calling OP_Program instruction.
68079 */
68080 case OP_Param: { /* out2-prerelease */
68081 #if 0 /* local variables moved into u.bz */
68082 VdbeFrame *pFrame;
68083 Mem *pIn;
68084 #endif /* local variables moved into u.bz */
68085 u.bz.pFrame = p->pFrame;
68086 u.bz.pIn = &u.bz.pFrame->aMem[pOp->p1 + u.bz.pFrame->aOp[u.bz.pFrame->pc].p1];
68087 sqlite3VdbeMemShallowCopy(pOut, u.bz.pIn, MEM_Ephem);
68088 break;
68089 }
68090
68091 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
68092
@@ -68138,26 +68835,26 @@
68138 **
68139 ** This instruction throws an error if the memory cell is not initially
68140 ** an integer.
68141 */
68142 case OP_MemMax: { /* in2 */
68143 #if 0 /* local variables moved into u.ca */
68144 Mem *pIn1;
68145 VdbeFrame *pFrame;
68146 #endif /* local variables moved into u.ca */
68147 if( p->pFrame ){
68148 for(u.ca.pFrame=p->pFrame; u.ca.pFrame->pParent; u.ca.pFrame=u.ca.pFrame->pParent);
68149 u.ca.pIn1 = &u.ca.pFrame->aMem[pOp->p1];
68150 }else{
68151 u.ca.pIn1 = &aMem[pOp->p1];
68152 }
68153 assert( memIsValid(u.ca.pIn1) );
68154 sqlite3VdbeMemIntegerify(u.ca.pIn1);
68155 pIn2 = &aMem[pOp->p2];
68156 sqlite3VdbeMemIntegerify(pIn2);
68157 if( u.ca.pIn1->u.i<pIn2->u.i){
68158 u.ca.pIn1->u.i = pIn2->u.i;
68159 }
68160 break;
68161 }
68162 #endif /* SQLITE_OMIT_AUTOINCREMENT */
68163
@@ -68220,54 +68917,54 @@
68220 **
68221 ** The P5 arguments are taken from register P2 and its
68222 ** successors.
68223 */
68224 case OP_AggStep: {
68225 #if 0 /* local variables moved into u.cb */
68226 int n;
68227 int i;
68228 Mem *pMem;
68229 Mem *pRec;
68230 sqlite3_context ctx;
68231 sqlite3_value **apVal;
68232 #endif /* local variables moved into u.cb */
68233
68234 u.cb.n = pOp->p5;
68235 assert( u.cb.n>=0 );
68236 u.cb.pRec = &aMem[pOp->p2];
68237 u.cb.apVal = p->apArg;
68238 assert( u.cb.apVal || u.cb.n==0 );
68239 for(u.cb.i=0; u.cb.i<u.cb.n; u.cb.i++, u.cb.pRec++){
68240 assert( memIsValid(u.cb.pRec) );
68241 u.cb.apVal[u.cb.i] = u.cb.pRec;
68242 memAboutToChange(p, u.cb.pRec);
68243 sqlite3VdbeMemStoreType(u.cb.pRec);
68244 }
68245 u.cb.ctx.pFunc = pOp->p4.pFunc;
68246 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68247 u.cb.ctx.pMem = u.cb.pMem = &aMem[pOp->p3];
68248 u.cb.pMem->n++;
68249 u.cb.ctx.s.flags = MEM_Null;
68250 u.cb.ctx.s.z = 0;
68251 u.cb.ctx.s.zMalloc = 0;
68252 u.cb.ctx.s.xDel = 0;
68253 u.cb.ctx.s.db = db;
68254 u.cb.ctx.isError = 0;
68255 u.cb.ctx.pColl = 0;
68256 if( u.cb.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68257 assert( pOp>p->aOp );
68258 assert( pOp[-1].p4type==P4_COLLSEQ );
68259 assert( pOp[-1].opcode==OP_CollSeq );
68260 u.cb.ctx.pColl = pOp[-1].p4.pColl;
68261 }
68262 (u.cb.ctx.pFunc->xStep)(&u.cb.ctx, u.cb.n, u.cb.apVal); /* IMP: R-24505-23230 */
68263 if( u.cb.ctx.isError ){
68264 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.cb.ctx.s));
68265 rc = u.cb.ctx.isError;
68266 }
68267
68268 sqlite3VdbeMemRelease(&u.cb.ctx.s);
68269
68270 break;
68271 }
68272
68273 /* Opcode: AggFinal P1 P2 * P4 *
@@ -68281,23 +68978,23 @@
68281 ** functions that can take varying numbers of arguments. The
68282 ** P4 argument is only needed for the degenerate case where
68283 ** the step function was not previously called.
68284 */
68285 case OP_AggFinal: {
68286 #if 0 /* local variables moved into u.cc */
68287 Mem *pMem;
68288 #endif /* local variables moved into u.cc */
68289 assert( pOp->p1>0 && pOp->p1<=p->nMem );
68290 u.cc.pMem = &aMem[pOp->p1];
68291 assert( (u.cc.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68292 rc = sqlite3VdbeMemFinalize(u.cc.pMem, pOp->p4.pFunc);
68293 if( rc ){
68294 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cc.pMem));
68295 }
68296 sqlite3VdbeChangeEncoding(u.cc.pMem, encoding);
68297 UPDATE_MAX_BLOBSIZE(u.cc.pMem);
68298 if( sqlite3VdbeMemTooBig(u.cc.pMem) ){
68299 goto too_big;
68300 }
68301 break;
68302 }
68303
@@ -68312,29 +69009,29 @@
68312 ** in the WAL that have been checkpointed after the checkpoint
68313 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
68314 ** mem[P3+2] are initialized to -1.
68315 */
68316 case OP_Checkpoint: {
68317 #if 0 /* local variables moved into u.cd */
68318 int i; /* Loop counter */
68319 int aRes[3]; /* Results */
68320 Mem *pMem; /* Write results here */
68321 #endif /* local variables moved into u.cd */
68322
68323 u.cd.aRes[0] = 0;
68324 u.cd.aRes[1] = u.cd.aRes[2] = -1;
68325 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
68326 || pOp->p2==SQLITE_CHECKPOINT_FULL
68327 || pOp->p2==SQLITE_CHECKPOINT_RESTART
68328 );
68329 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cd.aRes[1], &u.cd.aRes[2]);
68330 if( rc==SQLITE_BUSY ){
68331 rc = SQLITE_OK;
68332 u.cd.aRes[0] = 1;
68333 }
68334 for(u.cd.i=0, u.cd.pMem = &aMem[pOp->p3]; u.cd.i<3; u.cd.i++, u.cd.pMem++){
68335 sqlite3VdbeMemSetInt64(u.cd.pMem, (i64)u.cd.aRes[u.cd.i]);
68336 }
68337 break;
68338 };
68339 #endif
68340
@@ -68349,95 +69046,95 @@
68349 ** If changing into or out of WAL mode the procedure is more complicated.
68350 **
68351 ** Write a string containing the final journal-mode to register P2.
68352 */
68353 case OP_JournalMode: { /* out2-prerelease */
68354 #if 0 /* local variables moved into u.ce */
68355 Btree *pBt; /* Btree to change journal mode of */
68356 Pager *pPager; /* Pager associated with pBt */
68357 int eNew; /* New journal mode */
68358 int eOld; /* The old journal mode */
68359 const char *zFilename; /* Name of database file for pPager */
68360 #endif /* local variables moved into u.ce */
68361
68362 u.ce.eNew = pOp->p3;
68363 assert( u.ce.eNew==PAGER_JOURNALMODE_DELETE
68364 || u.ce.eNew==PAGER_JOURNALMODE_TRUNCATE
68365 || u.ce.eNew==PAGER_JOURNALMODE_PERSIST
68366 || u.ce.eNew==PAGER_JOURNALMODE_OFF
68367 || u.ce.eNew==PAGER_JOURNALMODE_MEMORY
68368 || u.ce.eNew==PAGER_JOURNALMODE_WAL
68369 || u.ce.eNew==PAGER_JOURNALMODE_QUERY
68370 );
68371 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68372
68373 u.ce.pBt = db->aDb[pOp->p1].pBt;
68374 u.ce.pPager = sqlite3BtreePager(u.ce.pBt);
68375 u.ce.eOld = sqlite3PagerGetJournalMode(u.ce.pPager);
68376 if( u.ce.eNew==PAGER_JOURNALMODE_QUERY ) u.ce.eNew = u.ce.eOld;
68377 if( !sqlite3PagerOkToChangeJournalMode(u.ce.pPager) ) u.ce.eNew = u.ce.eOld;
68378
68379 #ifndef SQLITE_OMIT_WAL
68380 u.ce.zFilename = sqlite3PagerFilename(u.ce.pPager);
68381
68382 /* Do not allow a transition to journal_mode=WAL for a database
68383 ** in temporary storage or if the VFS does not support shared memory
68384 */
68385 if( u.ce.eNew==PAGER_JOURNALMODE_WAL
68386 && (u.ce.zFilename[0]==0 /* Temp file */
68387 || !sqlite3PagerWalSupported(u.ce.pPager)) /* No shared-memory support */
68388 ){
68389 u.ce.eNew = u.ce.eOld;
68390 }
68391
68392 if( (u.ce.eNew!=u.ce.eOld)
68393 && (u.ce.eOld==PAGER_JOURNALMODE_WAL || u.ce.eNew==PAGER_JOURNALMODE_WAL)
68394 ){
68395 if( !db->autoCommit || db->activeVdbeCnt>1 ){
68396 rc = SQLITE_ERROR;
68397 sqlite3SetString(&p->zErrMsg, db,
68398 "cannot change %s wal mode from within a transaction",
68399 (u.ce.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
68400 );
68401 break;
68402 }else{
68403
68404 if( u.ce.eOld==PAGER_JOURNALMODE_WAL ){
68405 /* If leaving WAL mode, close the log file. If successful, the call
68406 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
68407 ** file. An EXCLUSIVE lock may still be held on the database file
68408 ** after a successful return.
68409 */
68410 rc = sqlite3PagerCloseWal(u.ce.pPager);
68411 if( rc==SQLITE_OK ){
68412 sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
68413 }
68414 }else if( u.ce.eOld==PAGER_JOURNALMODE_MEMORY ){
68415 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
68416 ** as an intermediate */
68417 sqlite3PagerSetJournalMode(u.ce.pPager, PAGER_JOURNALMODE_OFF);
68418 }
68419
68420 /* Open a transaction on the database file. Regardless of the journal
68421 ** mode, this transaction always uses a rollback journal.
68422 */
68423 assert( sqlite3BtreeIsInTrans(u.ce.pBt)==0 );
68424 if( rc==SQLITE_OK ){
68425 rc = sqlite3BtreeSetVersion(u.ce.pBt, (u.ce.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
68426 }
68427 }
68428 }
68429 #endif /* ifndef SQLITE_OMIT_WAL */
68430
68431 if( rc ){
68432 u.ce.eNew = u.ce.eOld;
68433 }
68434 u.ce.eNew = sqlite3PagerSetJournalMode(u.ce.pPager, u.ce.eNew);
68435
68436 pOut = &aMem[pOp->p2];
68437 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
68438 pOut->z = (char *)sqlite3JournalModename(u.ce.eNew);
68439 pOut->n = sqlite3Strlen30(pOut->z);
68440 pOut->enc = SQLITE_UTF8;
68441 sqlite3VdbeChangeEncoding(pOut, encoding);
68442 break;
68443 };
@@ -68462,18 +69159,18 @@
68462 ** Perform a single step of the incremental vacuum procedure on
68463 ** the P1 database. If the vacuum has finished, jump to instruction
68464 ** P2. Otherwise, fall through to the next instruction.
68465 */
68466 case OP_IncrVacuum: { /* jump */
68467 #if 0 /* local variables moved into u.cf */
68468 Btree *pBt;
68469 #endif /* local variables moved into u.cf */
68470
68471 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68472 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68473 u.cf.pBt = db->aDb[pOp->p1].pBt;
68474 rc = sqlite3BtreeIncrVacuum(u.cf.pBt);
68475 if( rc==SQLITE_DONE ){
68476 pc = pOp->p2 - 1;
68477 rc = SQLITE_OK;
68478 }
68479 break;
@@ -68539,16 +69236,16 @@
68539 ** Also, whether or not P4 is set, check that this is not being called from
68540 ** within a callback to a virtual table xSync() method. If it is, the error
68541 ** code will be set to SQLITE_LOCKED.
68542 */
68543 case OP_VBegin: {
68544 #if 0 /* local variables moved into u.cg */
68545 VTable *pVTab;
68546 #endif /* local variables moved into u.cg */
68547 u.cg.pVTab = pOp->p4.pVtab;
68548 rc = sqlite3VtabBegin(db, u.cg.pVTab);
68549 if( u.cg.pVTab ) importVtabErrMsg(p, u.cg.pVTab->pVtab);
68550 break;
68551 }
68552 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68553
68554 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -68583,36 +69280,36 @@
68583 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68584 ** P1 is a cursor number. This opcode opens a cursor to the virtual
68585 ** table and stores that cursor in P1.
68586 */
68587 case OP_VOpen: {
68588 #if 0 /* local variables moved into u.ch */
68589 VdbeCursor *pCur;
68590 sqlite3_vtab_cursor *pVtabCursor;
68591 sqlite3_vtab *pVtab;
68592 sqlite3_module *pModule;
68593 #endif /* local variables moved into u.ch */
68594
68595 u.ch.pCur = 0;
68596 u.ch.pVtabCursor = 0;
68597 u.ch.pVtab = pOp->p4.pVtab->pVtab;
68598 u.ch.pModule = (sqlite3_module *)u.ch.pVtab->pModule;
68599 assert(u.ch.pVtab && u.ch.pModule);
68600 rc = u.ch.pModule->xOpen(u.ch.pVtab, &u.ch.pVtabCursor);
68601 importVtabErrMsg(p, u.ch.pVtab);
68602 if( SQLITE_OK==rc ){
68603 /* Initialize sqlite3_vtab_cursor base class */
68604 u.ch.pVtabCursor->pVtab = u.ch.pVtab;
68605
68606 /* Initialise vdbe cursor object */
68607 u.ch.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
68608 if( u.ch.pCur ){
68609 u.ch.pCur->pVtabCursor = u.ch.pVtabCursor;
68610 u.ch.pCur->pModule = u.ch.pVtabCursor->pVtab->pModule;
68611 }else{
68612 db->mallocFailed = 1;
68613 u.ch.pModule->xClose(u.ch.pVtabCursor);
68614 }
68615 }
68616 break;
68617 }
68618 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68635,11 +69332,11 @@
68635 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
68636 **
68637 ** A jump is made to P2 if the result set after filtering would be empty.
68638 */
68639 case OP_VFilter: { /* jump */
68640 #if 0 /* local variables moved into u.ci */
68641 int nArg;
68642 int iQuery;
68643 const sqlite3_module *pModule;
68644 Mem *pQuery;
68645 Mem *pArgc;
@@ -68647,49 +69344,49 @@
68647 sqlite3_vtab *pVtab;
68648 VdbeCursor *pCur;
68649 int res;
68650 int i;
68651 Mem **apArg;
68652 #endif /* local variables moved into u.ci */
68653
68654 u.ci.pQuery = &aMem[pOp->p3];
68655 u.ci.pArgc = &u.ci.pQuery[1];
68656 u.ci.pCur = p->apCsr[pOp->p1];
68657 assert( memIsValid(u.ci.pQuery) );
68658 REGISTER_TRACE(pOp->p3, u.ci.pQuery);
68659 assert( u.ci.pCur->pVtabCursor );
68660 u.ci.pVtabCursor = u.ci.pCur->pVtabCursor;
68661 u.ci.pVtab = u.ci.pVtabCursor->pVtab;
68662 u.ci.pModule = u.ci.pVtab->pModule;
68663
68664 /* Grab the index number and argc parameters */
68665 assert( (u.ci.pQuery->flags&MEM_Int)!=0 && u.ci.pArgc->flags==MEM_Int );
68666 u.ci.nArg = (int)u.ci.pArgc->u.i;
68667 u.ci.iQuery = (int)u.ci.pQuery->u.i;
68668
68669 /* Invoke the xFilter method */
68670 {
68671 u.ci.res = 0;
68672 u.ci.apArg = p->apArg;
68673 for(u.ci.i = 0; u.ci.i<u.ci.nArg; u.ci.i++){
68674 u.ci.apArg[u.ci.i] = &u.ci.pArgc[u.ci.i+1];
68675 sqlite3VdbeMemStoreType(u.ci.apArg[u.ci.i]);
68676 }
68677
68678 p->inVtabMethod = 1;
68679 rc = u.ci.pModule->xFilter(u.ci.pVtabCursor, u.ci.iQuery, pOp->p4.z, u.ci.nArg, u.ci.apArg);
68680 p->inVtabMethod = 0;
68681 importVtabErrMsg(p, u.ci.pVtab);
68682 if( rc==SQLITE_OK ){
68683 u.ci.res = u.ci.pModule->xEof(u.ci.pVtabCursor);
68684 }
68685
68686 if( u.ci.res ){
68687 pc = pOp->p2 - 1;
68688 }
68689 }
68690 u.ci.pCur->nullRow = 0;
68691
68692 break;
68693 }
68694 #endif /* SQLITE_OMIT_VIRTUALTABLE */
68695
@@ -68699,55 +69396,55 @@
68699 ** Store the value of the P2-th column of
68700 ** the row of the virtual-table that the
68701 ** P1 cursor is pointing to into register P3.
68702 */
68703 case OP_VColumn: {
68704 #if 0 /* local variables moved into u.cj */
68705 sqlite3_vtab *pVtab;
68706 const sqlite3_module *pModule;
68707 Mem *pDest;
68708 sqlite3_context sContext;
68709 #endif /* local variables moved into u.cj */
68710
68711 VdbeCursor *pCur = p->apCsr[pOp->p1];
68712 assert( pCur->pVtabCursor );
68713 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68714 u.cj.pDest = &aMem[pOp->p3];
68715 memAboutToChange(p, u.cj.pDest);
68716 if( pCur->nullRow ){
68717 sqlite3VdbeMemSetNull(u.cj.pDest);
68718 break;
68719 }
68720 u.cj.pVtab = pCur->pVtabCursor->pVtab;
68721 u.cj.pModule = u.cj.pVtab->pModule;
68722 assert( u.cj.pModule->xColumn );
68723 memset(&u.cj.sContext, 0, sizeof(u.cj.sContext));
68724
68725 /* The output cell may already have a buffer allocated. Move
68726 ** the current contents to u.cj.sContext.s so in case the user-function
68727 ** can use the already allocated buffer instead of allocating a
68728 ** new one.
68729 */
68730 sqlite3VdbeMemMove(&u.cj.sContext.s, u.cj.pDest);
68731 MemSetTypeFlag(&u.cj.sContext.s, MEM_Null);
68732
68733 rc = u.cj.pModule->xColumn(pCur->pVtabCursor, &u.cj.sContext, pOp->p2);
68734 importVtabErrMsg(p, u.cj.pVtab);
68735 if( u.cj.sContext.isError ){
68736 rc = u.cj.sContext.isError;
68737 }
68738
68739 /* Copy the result of the function to the P3 register. We
68740 ** do this regardless of whether or not an error occurred to ensure any
68741 ** dynamic allocation in u.cj.sContext.s (a Mem struct) is released.
68742 */
68743 sqlite3VdbeChangeEncoding(&u.cj.sContext.s, encoding);
68744 sqlite3VdbeMemMove(u.cj.pDest, &u.cj.sContext.s);
68745 REGISTER_TRACE(pOp->p3, u.cj.pDest);
68746 UPDATE_MAX_BLOBSIZE(u.cj.pDest);
68747
68748 if( sqlite3VdbeMemTooBig(u.cj.pDest) ){
68749 goto too_big;
68750 }
68751 break;
68752 }
68753 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68758,42 +69455,42 @@
68758 ** Advance virtual table P1 to the next row in its result set and
68759 ** jump to instruction P2. Or, if the virtual table has reached
68760 ** the end of its result set, then fall through to the next instruction.
68761 */
68762 case OP_VNext: { /* jump */
68763 #if 0 /* local variables moved into u.ck */
68764 sqlite3_vtab *pVtab;
68765 const sqlite3_module *pModule;
68766 int res;
68767 VdbeCursor *pCur;
68768 #endif /* local variables moved into u.ck */
68769
68770 u.ck.res = 0;
68771 u.ck.pCur = p->apCsr[pOp->p1];
68772 assert( u.ck.pCur->pVtabCursor );
68773 if( u.ck.pCur->nullRow ){
68774 break;
68775 }
68776 u.ck.pVtab = u.ck.pCur->pVtabCursor->pVtab;
68777 u.ck.pModule = u.ck.pVtab->pModule;
68778 assert( u.ck.pModule->xNext );
68779
68780 /* Invoke the xNext() method of the module. There is no way for the
68781 ** underlying implementation to return an error if one occurs during
68782 ** xNext(). Instead, if an error occurs, true is returned (indicating that
68783 ** data is available) and the error code returned when xColumn or
68784 ** some other method is next invoked on the save virtual table cursor.
68785 */
68786 p->inVtabMethod = 1;
68787 rc = u.ck.pModule->xNext(u.ck.pCur->pVtabCursor);
68788 p->inVtabMethod = 0;
68789 importVtabErrMsg(p, u.ck.pVtab);
68790 if( rc==SQLITE_OK ){
68791 u.ck.res = u.ck.pModule->xEof(u.ck.pCur->pVtabCursor);
68792 }
68793
68794 if( !u.ck.res ){
68795 /* If there is data, jump to P2 */
68796 pc = pOp->p2 - 1;
68797 }
68798 break;
68799 }
@@ -68805,23 +69502,23 @@
68805 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
68806 ** This opcode invokes the corresponding xRename method. The value
68807 ** in register P1 is passed as the zName argument to the xRename method.
68808 */
68809 case OP_VRename: {
68810 #if 0 /* local variables moved into u.cl */
68811 sqlite3_vtab *pVtab;
68812 Mem *pName;
68813 #endif /* local variables moved into u.cl */
68814
68815 u.cl.pVtab = pOp->p4.pVtab->pVtab;
68816 u.cl.pName = &aMem[pOp->p1];
68817 assert( u.cl.pVtab->pModule->xRename );
68818 assert( memIsValid(u.cl.pName) );
68819 REGISTER_TRACE(pOp->p1, u.cl.pName);
68820 assert( u.cl.pName->flags & MEM_Str );
68821 rc = u.cl.pVtab->pModule->xRename(u.cl.pVtab, u.cl.pName->z);
68822 importVtabErrMsg(p, u.cl.pVtab);
68823 p->expired = 0;
68824
68825 break;
68826 }
68827 #endif
@@ -68849,45 +69546,45 @@
68849 ** P1 is a boolean flag. If it is set to true and the xUpdate call
68850 ** is successful, then the value returned by sqlite3_last_insert_rowid()
68851 ** is set to the value of the rowid for the row just inserted.
68852 */
68853 case OP_VUpdate: {
68854 #if 0 /* local variables moved into u.cm */
68855 sqlite3_vtab *pVtab;
68856 sqlite3_module *pModule;
68857 int nArg;
68858 int i;
68859 sqlite_int64 rowid;
68860 Mem **apArg;
68861 Mem *pX;
68862 #endif /* local variables moved into u.cm */
68863
68864 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
68865 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
68866 );
68867 u.cm.pVtab = pOp->p4.pVtab->pVtab;
68868 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
68869 u.cm.nArg = pOp->p2;
68870 assert( pOp->p4type==P4_VTAB );
68871 if( ALWAYS(u.cm.pModule->xUpdate) ){
68872 u8 vtabOnConflict = db->vtabOnConflict;
68873 u.cm.apArg = p->apArg;
68874 u.cm.pX = &aMem[pOp->p3];
68875 for(u.cm.i=0; u.cm.i<u.cm.nArg; u.cm.i++){
68876 assert( memIsValid(u.cm.pX) );
68877 memAboutToChange(p, u.cm.pX);
68878 sqlite3VdbeMemStoreType(u.cm.pX);
68879 u.cm.apArg[u.cm.i] = u.cm.pX;
68880 u.cm.pX++;
68881 }
68882 db->vtabOnConflict = pOp->p5;
68883 rc = u.cm.pModule->xUpdate(u.cm.pVtab, u.cm.nArg, u.cm.apArg, &u.cm.rowid);
68884 db->vtabOnConflict = vtabOnConflict;
68885 importVtabErrMsg(p, u.cm.pVtab);
68886 if( rc==SQLITE_OK && pOp->p1 ){
68887 assert( u.cm.nArg>1 && u.cm.apArg[0] && (u.cm.apArg[0]->flags&MEM_Null) );
68888 db->lastRowid = lastRowid = u.cm.rowid;
68889 }
68890 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
68891 if( pOp->p5==OE_Ignore ){
68892 rc = SQLITE_OK;
68893 }else{
@@ -68943,25 +69640,25 @@
68943 **
68944 ** If tracing is enabled (by the sqlite3_trace()) interface, then
68945 ** the UTF-8 string contained in P4 is emitted on the trace callback.
68946 */
68947 case OP_Trace: {
68948 #if 0 /* local variables moved into u.cn */
68949 char *zTrace;
68950 char *z;
68951 #endif /* local variables moved into u.cn */
68952
68953 if( db->xTrace && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
68954 u.cn.z = sqlite3VdbeExpandSql(p, u.cn.zTrace);
68955 db->xTrace(db->pTraceArg, u.cn.z);
68956 sqlite3DbFree(db, u.cn.z);
68957 }
68958 #ifdef SQLITE_DEBUG
68959 if( (db->flags & SQLITE_SqlTrace)!=0
68960 && (u.cn.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
68961 ){
68962 sqlite3DebugPrintf("SQL-trace: %s\n", u.cn.zTrace);
68963 }
68964 #endif /* SQLITE_DEBUG */
68965 break;
68966 }
68967 #endif
@@ -69556,10 +70253,930 @@
69556 }
69557
69558 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
69559
69560 /************** End of vdbeblob.c ********************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69561 /************** Begin file journal.c *****************************************/
69562 /*
69563 ** 2007 August 22
69564 **
69565 ** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71689,12 @@
70072 **
70073 *************************************************************************
70074 ** This file contains routines used for walking the parser tree for
70075 ** an SQL statement.
70076 */
 
 
70077
70078
70079 /*
70080 ** Walk an expression tree. Invoke the callback once for each node
70081 ** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71829,12 @@
70210 **
70211 ** This file contains routines used for walking the parser tree and
70212 ** resolve all identifiers by associating them with a particular
70213 ** table and column.
70214 */
 
 
70215
70216 /*
70217 ** Turn the pExpr expression into an alias for the iCol-th column of the
70218 ** result set in pEList.
70219 **
@@ -73700,11 +75321,11 @@
73700 if( !pAggInfo->directMode ){
73701 assert( pCol->iMem>0 );
73702 inReg = pCol->iMem;
73703 break;
73704 }else if( pAggInfo->useSortingIdx ){
73705 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdx,
73706 pCol->iSorterColumn, target);
73707 break;
73708 }
73709 /* Otherwise, fall thru into the TK_COLUMN case */
73710 }
@@ -76012,100 +77633,10 @@
76012 ** May you find forgiveness for yourself and forgive others.
76013 ** May you share freely, never taking more than you give.
76014 **
76015 *************************************************************************
76016 ** This file contains code associated with the ANALYZE command.
76017 **
76018 ** The ANALYZE command gather statistics about the content of tables
76019 ** and indices. These statistics are made available to the query planner
76020 ** to help it make better decisions about how to perform queries.
76021 **
76022 ** The following system tables are or have been supported:
76023 **
76024 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
76025 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
76026 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
76027 **
76028 ** Additional tables might be added in future releases of SQLite.
76029 ** The sqlite_stat2 table is not created or used unless the SQLite version
76030 ** is between 3.6.18 and 3.7.7, inclusive, and unless SQLite is compiled
76031 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
76032 ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
76033 ** created and used by SQLite versions after 2011-08-09 with
76034 ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
76035 ** is a superset of sqlite_stat2.
76036 **
76037 ** Format of sqlite_stat1:
76038 **
76039 ** There is normally one row per index, with the index identified by the
76040 ** name in the idx column. The tbl column is the name of the table to
76041 ** which the index belongs. In each such row, the stat column will be
76042 ** a string consisting of a list of integers. The first integer in this
76043 ** list is the number of rows in the index and in the table. The second
76044 ** integer is the average number of rows in the index that have the same
76045 ** value in the first column of the index. The third integer is the average
76046 ** number of rows in the index that have the same value for the first two
76047 ** columns. The N-th integer (for N>1) is the average number of rows in
76048 ** the index which have the same value for the first N-1 columns. For
76049 ** a K-column index, there will be K+1 integers in the stat column. If
76050 ** the index is unique, then the last integer will be 1.
76051 **
76052 ** The list of integers in the stat column can optionally be followed
76053 ** by the keyword "unordered". The "unordered" keyword, if it is present,
76054 ** must be separated from the last integer by a single space. If the
76055 ** "unordered" keyword is present, then the query planner assumes that
76056 ** the index is unordered and will not use the index for a range query.
76057 **
76058 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
76059 ** column contains a single integer which is the (estimated) number of
76060 ** rows in the table identified by sqlite_stat1.tbl.
76061 **
76062 ** Format of sqlite_stat2:
76063 **
76064 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
76065 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
76066 ** 3.6.18 and 3.7.7. The "stat2" table contains additional information
76067 ** about the distribution of keys within an index. The index is identified by
76068 ** the "idx" column and the "tbl" column is the name of the table to which
76069 ** the index belongs. There are usually 10 rows in the sqlite_stat2
76070 ** table for each index.
76071 **
76072 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
76073 ** inclusive are samples of the left-most key value in the index taken at
76074 ** evenly spaced points along the index. Let the number of samples be S
76075 ** (10 in the standard build) and let C be the number of rows in the index.
76076 ** Then the sampled rows are given by:
76077 **
76078 ** rownumber = (i*C*2 + C)/(S*2)
76079 **
76080 ** For i between 0 and S-1. Conceptually, the index space is divided into
76081 ** S uniform buckets and the samples are the middle row from each bucket.
76082 **
76083 ** The format for sqlite_stat2 is recorded here for legacy reference. This
76084 ** version of SQLite does not support sqlite_stat2. It neither reads nor
76085 ** writes the sqlite_stat2 table. This version of SQLite only supports
76086 ** sqlite_stat3.
76087 **
76088 ** Format for sqlite_stat3:
76089 **
76090 ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
76091 ** used to avoid compatibility problems.
76092 **
76093 ** The format of the sqlite_stat3 table is similar to the format for
76094 ** the sqlite_stat2 table, with the following changes: (1)
76095 ** The sampleno column is removed. (2) Every sample has nEq, nLt, and nDLt
76096 ** columns which hold the approximate number of rows in the table that
76097 ** exactly match the sample, the approximate number of rows with values
76098 ** less than the sample, and the approximate number of distinct key values
76099 ** less than the sample, respectively. (3) The number of samples can vary
76100 ** from one table to the next; the sample count does not have to be
76101 ** exactly 10 as it is with sqlite_stat2.
76102 **
76103 ** The ANALYZE command will typically generate sqlite_stat3 tables
76104 ** that contain between 10 and 40 samples which are distributed across
76105 ** the key space, though not uniformly, and which include samples with
76106 ** largest possible nEq values.
76107 */
76108 #ifndef SQLITE_OMIT_ANALYZE
76109
76110 /*
76111 ** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77664,12 @@
76133 static const struct {
76134 const char *zName;
76135 const char *zCols;
76136 } aTable[] = {
76137 { "sqlite_stat1", "tbl,idx,stat" },
76138 #ifdef SQLITE_ENABLE_STAT3
76139 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
76140 #endif
76141 };
76142 static const char *azToDrop[] = {
76143 "sqlite_stat2",
76144 #ifndef SQLITE_ENABLE_STAT3
76145 "sqlite_stat3",
76146 #endif
76147 };
76148
76149 int aRoot[] = {0, 0};
76150 u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77681,10 @@
76156 if( v==0 ) return;
76157 assert( sqlite3BtreeHoldsAllMutexes(db) );
76158 assert( sqlite3VdbeDb(v)==db );
76159 pDb = &db->aDb[iDb];
76160
76161 /* Drop all statistics tables that this version of SQLite does not
76162 ** understand.
76163 */
76164 for(i=0; i<ArraySize(azToDrop); i++){
76165 Table *pTab = sqlite3FindTable(db, azToDrop[i], pDb->zName);
76166 if( pTab ) sqlite3CodeDropTable(pParse, pTab, iDb, 0);
76167 }
76168
76169 /* Create new statistic tables if they do not exist, or clear them
76170 ** if they do already exist.
76171 */
76172 for(i=0; i<ArraySize(aTable); i++){
76173 const char *zTab = aTable[i].zName;
76174 Table *pStat;
76175 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
76176 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77711,17 @@
76197 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
76198 }
76199 }
76200 }
76201
76202 /* Open the sqlite_stat[13] tables for writing. */
76203 for(i=0; i<ArraySize(aTable); i++){
76204 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
76205 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
76206 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
76207 }
76208 }
76209
76210 /*
76211 ** Recommended number of samples for sqlite_stat3
76212 */
76213 #ifndef SQLITE_STAT3_SAMPLES
76214 # define SQLITE_STAT3_SAMPLES 24
76215 #endif
76216
76217 /*
76218 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
76219 ** share an instance of the following structure to hold their state
76220 ** information.
76221 */
76222 typedef struct Stat3Accum Stat3Accum;
76223 struct Stat3Accum {
76224 tRowcnt nRow; /* Number of rows in the entire table */
76225 tRowcnt nPSample; /* How often to do a periodic sample */
76226 int iMin; /* Index of entry with minimum nEq and hash */
76227 int mxSample; /* Maximum number of samples to accumulate */
76228 int nSample; /* Current number of samples */
76229 u32 iPrn; /* Pseudo-random number used for sampling */
76230 struct Stat3Sample {
76231 i64 iRowid; /* Rowid in main table of the key */
76232 tRowcnt nEq; /* sqlite_stat3.nEq */
76233 tRowcnt nLt; /* sqlite_stat3.nLt */
76234 tRowcnt nDLt; /* sqlite_stat3.nDLt */
76235 u8 isPSample; /* True if a periodic sample */
76236 u32 iHash; /* Tiebreaker hash */
76237 } *a; /* An array of samples */
76238 };
76239
76240 #ifdef SQLITE_ENABLE_STAT3
76241 /*
76242 ** Implementation of the stat3_init(C,S) SQL function. The two parameters
76243 ** are the number of rows in the table or index (C) and the number of samples
76244 ** to accumulate (S).
76245 **
76246 ** This routine allocates the Stat3Accum object.
76247 **
76248 ** The return value is the Stat3Accum object (P).
76249 */
76250 static void stat3Init(
76251 sqlite3_context *context,
76252 int argc,
76253 sqlite3_value **argv
76254 ){
76255 Stat3Accum *p;
76256 tRowcnt nRow;
76257 int mxSample;
76258 int n;
76259
76260 UNUSED_PARAMETER(argc);
76261 nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
76262 mxSample = sqlite3_value_int(argv[1]);
76263 n = sizeof(*p) + sizeof(p->a[0])*mxSample;
76264 p = sqlite3_malloc( n );
76265 if( p==0 ){
76266 sqlite3_result_error_nomem(context);
76267 return;
76268 }
76269 memset(p, 0, n);
76270 p->a = (struct Stat3Sample*)&p[1];
76271 p->nRow = nRow;
76272 p->mxSample = mxSample;
76273 p->nPSample = p->nRow/(mxSample/3+1) + 1;
76274 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
76275 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
76276 }
76277 static const FuncDef stat3InitFuncdef = {
76278 2, /* nArg */
76279 SQLITE_UTF8, /* iPrefEnc */
76280 0, /* flags */
76281 0, /* pUserData */
76282 0, /* pNext */
76283 stat3Init, /* xFunc */
76284 0, /* xStep */
76285 0, /* xFinalize */
76286 "stat3_init", /* zName */
76287 0, /* pHash */
76288 0 /* pDestructor */
76289 };
76290
76291
76292 /*
76293 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
76294 ** arguments describe a single key instance. This routine makes the
76295 ** decision about whether or not to retain this key for the sqlite_stat3
76296 ** table.
76297 **
76298 ** The return value is NULL.
76299 */
76300 static void stat3Push(
76301 sqlite3_context *context,
76302 int argc,
76303 sqlite3_value **argv
76304 ){
76305 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
76306 tRowcnt nEq = sqlite3_value_int64(argv[0]);
76307 tRowcnt nLt = sqlite3_value_int64(argv[1]);
76308 tRowcnt nDLt = sqlite3_value_int64(argv[2]);
76309 i64 rowid = sqlite3_value_int64(argv[3]);
76310 u8 isPSample = 0;
76311 u8 doInsert = 0;
76312 int iMin = p->iMin;
76313 struct Stat3Sample *pSample;
76314 int i;
76315 u32 h;
76316
76317 UNUSED_PARAMETER(context);
76318 UNUSED_PARAMETER(argc);
76319 if( nEq==0 ) return;
76320 h = p->iPrn = p->iPrn*1103515245 + 12345;
76321 if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
76322 doInsert = isPSample = 1;
76323 }else if( p->nSample<p->mxSample ){
76324 doInsert = 1;
76325 }else{
76326 if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
76327 doInsert = 1;
76328 }
76329 }
76330 if( !doInsert ) return;
76331 if( p->nSample==p->mxSample ){
76332 if( iMin<p->nSample ){
76333 memcpy(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin));
76334 }
76335 pSample = &p->a[p->nSample-1];
76336 }else{
76337 pSample = &p->a[p->nSample++];
76338 }
76339 pSample->iRowid = rowid;
76340 pSample->nEq = nEq;
76341 pSample->nLt = nLt;
76342 pSample->nDLt = nDLt;
76343 pSample->iHash = h;
76344 pSample->isPSample = isPSample;
76345
76346 /* Find the new minimum */
76347 if( p->nSample==p->mxSample ){
76348 pSample = p->a;
76349 i = 0;
76350 while( pSample->isPSample ){
76351 i++;
76352 pSample++;
76353 assert( i<p->nSample );
76354 }
76355 nEq = pSample->nEq;
76356 h = pSample->iHash;
76357 iMin = i;
76358 for(i++, pSample++; i<p->nSample; i++, pSample++){
76359 if( pSample->isPSample ) continue;
76360 if( pSample->nEq<nEq
76361 || (pSample->nEq==nEq && pSample->iHash<h)
76362 ){
76363 iMin = i;
76364 nEq = pSample->nEq;
76365 h = pSample->iHash;
76366 }
76367 }
76368 p->iMin = iMin;
76369 }
76370 }
76371 static const FuncDef stat3PushFuncdef = {
76372 5, /* nArg */
76373 SQLITE_UTF8, /* iPrefEnc */
76374 0, /* flags */
76375 0, /* pUserData */
76376 0, /* pNext */
76377 stat3Push, /* xFunc */
76378 0, /* xStep */
76379 0, /* xFinalize */
76380 "stat3_push", /* zName */
76381 0, /* pHash */
76382 0 /* pDestructor */
76383 };
76384
76385 /*
76386 ** Implementation of the stat3_get(P,N,...) SQL function. This routine is
76387 ** used to query the results. Content is returned for the Nth sqlite_stat3
76388 ** row where N is between 0 and S-1 and S is the number of samples. The
76389 ** value returned depends on the number of arguments.
76390 **
76391 ** argc==2 result: rowid
76392 ** argc==3 result: nEq
76393 ** argc==4 result: nLt
76394 ** argc==5 result: nDLt
76395 */
76396 static void stat3Get(
76397 sqlite3_context *context,
76398 int argc,
76399 sqlite3_value **argv
76400 ){
76401 int n = sqlite3_value_int(argv[1]);
76402 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
76403
76404 assert( p!=0 );
76405 if( p->nSample<=n ) return;
76406 switch( argc ){
76407 case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
76408 case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
76409 case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
76410 case 5: sqlite3_result_int64(context, p->a[n].nDLt); break;
76411 }
76412 }
76413 static const FuncDef stat3GetFuncdef = {
76414 -1, /* nArg */
76415 SQLITE_UTF8, /* iPrefEnc */
76416 0, /* flags */
76417 0, /* pUserData */
76418 0, /* pNext */
76419 stat3Get, /* xFunc */
76420 0, /* xStep */
76421 0, /* xFinalize */
76422 "stat3_get", /* zName */
76423 0, /* pHash */
76424 0 /* pDestructor */
76425 };
76426 #endif /* SQLITE_ENABLE_STAT3 */
76427
76428
76429
76430
76431 /*
76432 ** Generate code to do an analysis of all indices associated with
76433 ** a single table.
76434 */
@@ -76448,31 +77741,24 @@
76448 int endOfLoop; /* The end of the loop */
76449 int jZeroRows = -1; /* Jump from here if number of rows is zero */
76450 int iDb; /* Index of database containing pTab */
76451 int regTabname = iMem++; /* Register containing table name */
76452 int regIdxname = iMem++; /* Register containing index name */
76453 int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
76454 #ifdef SQLITE_ENABLE_STAT3
76455 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
76456 int regNumLt = iMem++; /* Number of keys less than regSample */
76457 int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
76458 int regSample = iMem++; /* The next sample value */
76459 int regRowid = regSample; /* Rowid of a sample */
76460 int regAccum = iMem++; /* Register to hold Stat3Accum object */
76461 int regLoop = iMem++; /* Loop counter */
76462 int regCount = iMem++; /* Number of rows in the table or index */
76463 int regTemp1 = iMem++; /* Intermediate register */
76464 int regTemp2 = iMem++; /* Intermediate register */
76465 int once = 1; /* One-time initialization */
76466 int shortJump = 0; /* Instruction address */
76467 int iTabCur = pParse->nTab++; /* Table cursor */
76468 #endif
76469 int regCol = iMem++; /* Content of a column in analyzed table */
76470 int regRec = iMem++; /* Register holding completed record */
76471 int regTemp = iMem++; /* Temporary use register */
76472 int regNewRowid = iMem++; /* Rowid for the inserted record */
76473
 
 
 
 
 
 
 
 
76474
76475 v = sqlite3GetVdbe(pParse);
76476 if( v==0 || NEVER(pTab==0) ){
76477 return;
76478 }
@@ -76501,22 +77787,17 @@
76501 iIdxCur = pParse->nTab++;
76502 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
76503 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
76504 int nCol;
76505 KeyInfo *pKey;
76506 int addrIfNot = 0; /* address of OP_IfNot */
76507 int *aChngAddr; /* Array of jump instruction addresses */
76508
76509 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
76510 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
76511 nCol = pIdx->nColumn;
76512 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
76513 if( iMem+1+(nCol*2)>pParse->nMem ){
76514 pParse->nMem = iMem+1+(nCol*2);
76515 }
76516 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*pIdx->nColumn);
76517 if( aChngAddr==0 ) continue;
76518
76519 /* Open a cursor to the index to be analyzed. */
76520 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
76521 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
76522 (char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77804,35 @@
76523 VdbeComment((v, "%s", pIdx->zName));
76524
76525 /* Populate the register containing the index name. */
76526 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
76527
76528 #ifdef SQLITE_ENABLE_STAT3
76529 if( once ){
76530 once = 0;
76531 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
76532 }
76533 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
76534 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
76535 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
76536 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
76537 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
76538 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
76539 (char*)&stat3InitFuncdef, P4_FUNCDEF);
76540 sqlite3VdbeChangeP5(v, 2);
76541 #endif /* SQLITE_ENABLE_STAT3 */
 
 
 
 
 
 
 
 
 
 
 
76542
76543 /* The block of memory cells initialized here is used as follows.
76544 **
76545 ** iMem:
76546 ** The total number of rows in the table.
@@ -76566,87 +77858,79 @@
76566 /* Start the analysis loop. This loop runs through all the entries in
76567 ** the index b-tree. */
76568 endOfLoop = sqlite3VdbeMakeLabel(v);
76569 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
76570 topOfLoop = sqlite3VdbeCurrentAddr(v);
76571 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
76572
76573 for(i=0; i<nCol; i++){
76574 CollSeq *pColl;
76575 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
76576 if( i==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76577 /* Always record the very first row */
76578 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
76579 }
76580 assert( pIdx->azColl!=0 );
76581 assert( pIdx->azColl[i]!=0 );
76582 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
76583 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
76584 (char*)pColl, P4_COLLSEQ);
76585 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
76586 VdbeComment((v, "jump if column %d changed", i));
76587 #ifdef SQLITE_ENABLE_STAT3
76588 if( i==0 ){
76589 sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
76590 VdbeComment((v, "incr repeat count"));
76591 }
76592 #endif
76593 }
76594 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
76595 for(i=0; i<nCol; i++){
76596 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
76597 if( i==0 ){
76598 sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
76599 #ifdef SQLITE_ENABLE_STAT3
76600 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
76601 (char*)&stat3PushFuncdef, P4_FUNCDEF);
76602 sqlite3VdbeChangeP5(v, 5);
76603 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
76604 sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
76605 sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
76606 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
76607 #endif
76608 }
76609 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
76610 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
76611 }
76612 sqlite3DbFree(db, aChngAddr);
76613
76614 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
76615 sqlite3VdbeResolveLabel(v, endOfLoop);
76616
76617 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
76618 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76619 #ifdef SQLITE_ENABLE_STAT3
76620 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
76621 (char*)&stat3PushFuncdef, P4_FUNCDEF);
76622 sqlite3VdbeChangeP5(v, 5);
76623 sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
76624 shortJump =
76625 sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
76626 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
76627 (char*)&stat3GetFuncdef, P4_FUNCDEF);
76628 sqlite3VdbeChangeP5(v, 2);
76629 sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
76630 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
76631 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
76632 sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
76633 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
76634 (char*)&stat3GetFuncdef, P4_FUNCDEF);
76635 sqlite3VdbeChangeP5(v, 3);
76636 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
76637 (char*)&stat3GetFuncdef, P4_FUNCDEF);
76638 sqlite3VdbeChangeP5(v, 4);
76639 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
76640 (char*)&stat3GetFuncdef, P4_FUNCDEF);
76641 sqlite3VdbeChangeP5(v, 5);
76642 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
76643 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
76644 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
76645 sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
76646 sqlite3VdbeJumpHere(v, shortJump+2);
76647 #endif
76648
76649 /* Store the results in sqlite_stat1.
76650 **
76651 ** The result is a single row of the sqlite_stat1 table. The first
76652 ** two columns are the names of the table and index. The third column
@@ -76662,51 +77946,50 @@
76662 **
76663 ** If K==0 then no entry is made into the sqlite_stat1 table.
76664 ** If K>0 then it is always the case the D>0 so division by zero
76665 ** is never possible.
76666 */
76667 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
76668 if( jZeroRows<0 ){
76669 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
76670 }
76671 for(i=0; i<nCol; i++){
76672 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
76673 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
76674 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
76675 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
76676 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
76677 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
76678 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
76679 }
76680 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76681 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76682 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
76683 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76684 }
76685
76686 /* If the table has no indices, create a single sqlite_stat1 entry
76687 ** containing NULL as the index name and the row count as the content.
76688 */
76689 if( pTab->pIndex==0 ){
76690 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
76691 VdbeComment((v, "%s", pTab->zName));
76692 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
76693 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
76694 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
76695 }else{
76696 sqlite3VdbeJumpHere(v, jZeroRows);
76697 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
76698 }
76699 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
76700 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
76701 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
76702 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
76703 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
76704 if( pParse->nMem<regRec ) pParse->nMem = regRec;
76705 sqlite3VdbeJumpHere(v, jZeroRows);
76706 }
76707
76708
76709 /*
76710 ** Generate code that will cause the most recent index analysis to
76711 ** be loaded into internal hash tables where is can be used.
76712 */
@@ -76727,11 +78010,11 @@
76727 int iStatCur;
76728 int iMem;
76729
76730 sqlite3BeginWriteOperation(pParse, 0, iDb);
76731 iStatCur = pParse->nTab;
76732 pParse->nTab += 3;
76733 openStatTable(pParse, iDb, iStatCur, 0, 0);
76734 iMem = pParse->nMem+1;
76735 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
76736 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
76737 Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +78035,11 @@
76752 assert( pTab!=0 );
76753 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
76754 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
76755 sqlite3BeginWriteOperation(pParse, 0, iDb);
76756 iStatCur = pParse->nTab;
76757 pParse->nTab += 3;
76758 if( pOnlyIdx ){
76759 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
76760 }else{
76761 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
76762 }
@@ -76857,11 +78140,11 @@
76857 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
76858 analysisInfo *pInfo = (analysisInfo*)pData;
76859 Index *pIndex;
76860 Table *pTable;
76861 int i, c, n;
76862 tRowcnt v;
76863 const char *z;
76864
76865 assert( argc==3 );
76866 UNUSED_PARAMETER2(NotUsed, argc);
76867
@@ -76900,172 +78183,40 @@
76900 /*
76901 ** If the Index.aSample variable is not NULL, delete the aSample[] array
76902 ** and its contents.
76903 */
76904 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
76905 #ifdef SQLITE_ENABLE_STAT3
76906 if( pIdx->aSample ){
76907 int j;
76908 for(j=0; j<pIdx->nSample; j++){
76909 IndexSample *p = &pIdx->aSample[j];
76910 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
76911 sqlite3_free(p->u.z);
76912 }
76913 }
76914 sqlite3_free(pIdx->aSample);
76915 }
76916 UNUSED_PARAMETER(db);
76917 pIdx->nSample = 0;
76918 pIdx->aSample = 0;
76919 #else
76920 UNUSED_PARAMETER(db);
76921 UNUSED_PARAMETER(pIdx);
76922 #endif
76923 }
76924
76925 #ifdef SQLITE_ENABLE_STAT3
76926 /*
76927 ** Load content from the sqlite_stat3 table into the Index.aSample[]
76928 ** arrays of all indices.
76929 */
76930 static int loadStat3(sqlite3 *db, const char *zDb){
76931 int rc; /* Result codes from subroutines */
76932 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
76933 char *zSql; /* Text of the SQL statement */
76934 Index *pPrevIdx = 0; /* Previous index in the loop */
76935 int idx = 0; /* slot in pIdx->aSample[] for next sample */
76936 int eType; /* Datatype of a sample */
76937 IndexSample *pSample; /* A slot in pIdx->aSample[] */
76938
76939 if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
76940 return SQLITE_OK;
76941 }
76942
76943 zSql = sqlite3MPrintf(db,
76944 "SELECT idx,count(*) FROM %Q.sqlite_stat3"
76945 " GROUP BY idx", zDb);
76946 if( !zSql ){
76947 return SQLITE_NOMEM;
76948 }
76949 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76950 sqlite3DbFree(db, zSql);
76951 if( rc ) return rc;
76952
76953 while( sqlite3_step(pStmt)==SQLITE_ROW ){
76954 char *zIndex; /* Index name */
76955 Index *pIdx; /* Pointer to the index object */
76956 int nSample; /* Number of samples */
76957
76958 zIndex = (char *)sqlite3_column_text(pStmt, 0);
76959 if( zIndex==0 ) continue;
76960 nSample = sqlite3_column_int(pStmt, 1);
76961 if( nSample>255 ) continue;
76962 pIdx = sqlite3FindIndex(db, zIndex, zDb);
76963 if( pIdx==0 ) continue;
76964 assert( pIdx->nSample==0 );
76965 pIdx->nSample = (u8)nSample;
76966 pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
76967 pIdx->avgEq = pIdx->aiRowEst[1];
76968 if( pIdx->aSample==0 ){
76969 db->mallocFailed = 1;
76970 sqlite3_finalize(pStmt);
76971 return SQLITE_NOMEM;
76972 }
76973 }
76974 sqlite3_finalize(pStmt);
76975
76976 zSql = sqlite3MPrintf(db,
76977 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
76978 if( !zSql ){
76979 return SQLITE_NOMEM;
76980 }
76981 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
76982 sqlite3DbFree(db, zSql);
76983 if( rc ) return rc;
76984
76985 while( sqlite3_step(pStmt)==SQLITE_ROW ){
76986 char *zIndex; /* Index name */
76987 Index *pIdx; /* Pointer to the index object */
76988 int i; /* Loop counter */
76989 tRowcnt sumEq; /* Sum of the nEq values */
76990
76991 zIndex = (char *)sqlite3_column_text(pStmt, 0);
76992 if( zIndex==0 ) continue;
76993 pIdx = sqlite3FindIndex(db, zIndex, zDb);
76994 if( pIdx==0 ) continue;
76995 if( pIdx==pPrevIdx ){
76996 idx++;
76997 }else{
76998 pPrevIdx = pIdx;
76999 idx = 0;
77000 }
77001 assert( idx<pIdx->nSample );
77002 pSample = &pIdx->aSample[idx];
77003 pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
77004 pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
77005 pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
77006 if( idx==pIdx->nSample-1 ){
77007 if( pSample->nDLt>0 ){
77008 for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
77009 pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
77010 }
77011 if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
77012 }
77013 eType = sqlite3_column_type(pStmt, 4);
77014 pSample->eType = (u8)eType;
77015 switch( eType ){
77016 case SQLITE_INTEGER: {
77017 pSample->u.i = sqlite3_column_int64(pStmt, 4);
77018 break;
77019 }
77020 case SQLITE_FLOAT: {
77021 pSample->u.r = sqlite3_column_double(pStmt, 4);
77022 break;
77023 }
77024 case SQLITE_NULL: {
77025 break;
77026 }
77027 default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
77028 const char *z = (const char *)(
77029 (eType==SQLITE_BLOB) ?
77030 sqlite3_column_blob(pStmt, 4):
77031 sqlite3_column_text(pStmt, 4)
77032 );
77033 int n = sqlite3_column_bytes(pStmt, 4);
77034 if( n>0xffff ) n = 0xffff;
77035 pSample->nByte = (u16)n;
77036 if( n < 1){
77037 pSample->u.z = 0;
77038 }else{
77039 pSample->u.z = sqlite3Malloc(n);
77040 if( pSample->u.z==0 ){
77041 db->mallocFailed = 1;
77042 sqlite3_finalize(pStmt);
77043 return SQLITE_NOMEM;
77044 }
77045 memcpy(pSample->u.z, z, n);
77046 }
77047 }
77048 }
77049 }
77050 return sqlite3_finalize(pStmt);
77051 }
77052 #endif /* SQLITE_ENABLE_STAT3 */
77053
77054 /*
77055 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
77056 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
77057 ** arrays. The contents of sqlite_stat3 are used to populate the
77058 ** Index.aSample[] arrays.
77059 **
77060 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
77061 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
77062 ** during compilation and the sqlite_stat3 table is present, no data is
77063 ** read from it.
77064 **
77065 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
77066 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
77067 ** returned. However, in this case, data is read from the sqlite_stat1
77068 ** table (if it is present) before returning.
77069 **
77070 ** If an OOM error occurs, this function always sets db->mallocFailed.
77071 ** This means if the caller does not care about other errors, the return
@@ -77083,14 +78234,12 @@
77083 /* Clear any prior statistics */
77084 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77085 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
77086 Index *pIdx = sqliteHashData(i);
77087 sqlite3DefaultRowEst(pIdx);
77088 #ifdef SQLITE_ENABLE_STAT3
77089 sqlite3DeleteIndexSamples(db, pIdx);
77090 pIdx->aSample = 0;
77091 #endif
77092 }
77093
77094 /* Check to make sure the sqlite_stat1 table exists */
77095 sInfo.db = db;
77096 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78247,91 @@
77098 return SQLITE_ERROR;
77099 }
77100
77101 /* Load new statistics out of the sqlite_stat1 table */
77102 zSql = sqlite3MPrintf(db,
77103 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
77104 if( zSql==0 ){
77105 rc = SQLITE_NOMEM;
77106 }else{
77107 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
77108 sqlite3DbFree(db, zSql);
77109 }
77110
77111
77112 /* Load the statistics from the sqlite_stat3 table. */
77113 #ifdef SQLITE_ENABLE_STAT3
 
 
 
77114 if( rc==SQLITE_OK ){
77115 rc = loadStat3(db, sInfo.zDatabase);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77116 }
77117 #endif
77118
77119 if( rc==SQLITE_NOMEM ){
77120 db->mallocFailed = 1;
@@ -79610,11 +80827,11 @@
79610 Table *p;
79611 int n;
79612 const char *z;
79613 Token sEnd;
79614 DbFixer sFix;
79615 Token *pName;
79616 int iDb;
79617 sqlite3 *db = pParse->db;
79618
79619 if( pParse->nVar>0 ){
79620 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +81143,11 @@
79926 Parse *pParse, /* The parsing context */
79927 int iDb, /* The database number */
79928 const char *zType, /* "idx" or "tbl" */
79929 const char *zName /* Name of index or table */
79930 ){
79931 static const char *azStatTab[] = {
79932 "sqlite_stat1",
79933 "sqlite_stat2",
79934 "sqlite_stat3",
79935 };
79936 int i;
79937 const char *zDbName = pParse->db->aDb[iDb].zName;
79938 for(i=0; i<ArraySize(azStatTab); i++){
79939 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
79940 sqlite3NestedParse(pParse,
@@ -79941,81 +81154,10 @@
79941 "DELETE FROM %Q.%s WHERE %s=%Q",
79942 zDbName, azStatTab[i], zType, zName
79943 );
79944 }
79945 }
79946 }
79947
79948 /*
79949 ** Generate code to drop a table.
79950 */
79951 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
79952 Vdbe *v;
79953 sqlite3 *db = pParse->db;
79954 Trigger *pTrigger;
79955 Db *pDb = &db->aDb[iDb];
79956
79957 v = sqlite3GetVdbe(pParse);
79958 assert( v!=0 );
79959 sqlite3BeginWriteOperation(pParse, 1, iDb);
79960
79961 #ifndef SQLITE_OMIT_VIRTUALTABLE
79962 if( IsVirtual(pTab) ){
79963 sqlite3VdbeAddOp0(v, OP_VBegin);
79964 }
79965 #endif
79966
79967 /* Drop all triggers associated with the table being dropped. Code
79968 ** is generated to remove entries from sqlite_master and/or
79969 ** sqlite_temp_master if required.
79970 */
79971 pTrigger = sqlite3TriggerList(pParse, pTab);
79972 while( pTrigger ){
79973 assert( pTrigger->pSchema==pTab->pSchema ||
79974 pTrigger->pSchema==db->aDb[1].pSchema );
79975 sqlite3DropTriggerPtr(pParse, pTrigger);
79976 pTrigger = pTrigger->pNext;
79977 }
79978
79979 #ifndef SQLITE_OMIT_AUTOINCREMENT
79980 /* Remove any entries of the sqlite_sequence table associated with
79981 ** the table being dropped. This is done before the table is dropped
79982 ** at the btree level, in case the sqlite_sequence table needs to
79983 ** move as a result of the drop (can happen in auto-vacuum mode).
79984 */
79985 if( pTab->tabFlags & TF_Autoincrement ){
79986 sqlite3NestedParse(pParse,
79987 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
79988 pDb->zName, pTab->zName
79989 );
79990 }
79991 #endif
79992
79993 /* Drop all SQLITE_MASTER table and index entries that refer to the
79994 ** table. The program name loops through the master table and deletes
79995 ** every row that refers to a table of the same name as the one being
79996 ** dropped. Triggers are handled seperately because a trigger can be
79997 ** created in the temp database that refers to a table in another
79998 ** database.
79999 */
80000 sqlite3NestedParse(pParse,
80001 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
80002 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
80003 if( !isView && !IsVirtual(pTab) ){
80004 destroyTable(pParse, pTab);
80005 }
80006
80007 /* Remove the table entry from SQLite's internal schema and modify
80008 ** the schema cookie.
80009 */
80010 if( IsVirtual(pTab) ){
80011 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
80012 }
80013 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
80014 sqlite3ChangeCookie(pParse, iDb);
80015 sqliteViewResetAll(db, iDb);
80016
80017 }
80018
80019 /*
80020 ** This routine is called to do the work of a DROP TABLE statement.
80021 ** pName is the name of the table to be dropped.
@@ -80082,11 +81224,11 @@
80082 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
80083 goto exit_drop_table;
80084 }
80085 }
80086 #endif
80087 if( !pParse->nested && sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
80088 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
80089 goto exit_drop_table;
80090 }
80091
80092 #ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81248,72 @@
80106 /* Generate code to remove the table from the master table
80107 ** on disk.
80108 */
80109 v = sqlite3GetVdbe(pParse);
80110 if( v ){
 
 
80111 sqlite3BeginWriteOperation(pParse, 1, iDb);
 
 
 
 
 
 
80112 sqlite3FkDropTable(pParse, pName, pTab);
80113 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80114 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 
 
 
 
 
 
 
 
 
 
 
 
80115 }
 
80116
80117 exit_drop_table:
80118 sqlite3SrcListDelete(db, pName);
80119 }
80120
@@ -80278,11 +81477,13 @@
80278 */
80279 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
80280 Table *pTab = pIndex->pTable; /* The table that is indexed */
80281 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
80282 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
 
80283 int addr1; /* Address of top of loop */
 
80284 int tnum; /* Root page of index */
80285 Vdbe *v; /* Generate code into this virtual machine */
80286 KeyInfo *pKey; /* KeyInfo for index */
80287 int regIdxKey; /* Registers containing the index key */
80288 int regRecord; /* Register holding assemblied index record */
@@ -80311,14 +81512,45 @@
80311 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
80312 (char *)pKey, P4_KEYINFO_HANDOFF);
80313 if( memRootPage>=0 ){
80314 sqlite3VdbeChangeP5(v, 1);
80315 }
 
 
 
 
 
 
 
 
 
80316 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
80317 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 
80318 regRecord = sqlite3GetTempReg(pParse);
80319 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80320 if( pIndex->onError!=OE_None ){
80321 const int regRowid = regIdxKey + pIndex->nColumn;
80322 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
80323 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
80324
@@ -80333,17 +81565,20 @@
80333 */
80334 sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
80335 sqlite3HaltConstraint(
80336 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
80337 }
80338 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdx, regRecord);
80339 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
 
80340 sqlite3ReleaseTempReg(pParse, regRecord);
80341 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
80342 sqlite3VdbeJumpHere(v, addr1);
 
80343 sqlite3VdbeAddOp1(v, OP_Close, iTab);
80344 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
 
80345 }
80346
80347 /*
80348 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
80349 ** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81792,24 @@
80557 */
80558 nName = sqlite3Strlen30(zName);
80559 nCol = pList->nExpr;
80560 pIndex = sqlite3DbMallocZero(db,
80561 sizeof(Index) + /* Index structure */
80562 sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
80563 sizeof(int)*nCol + /* Index.aiColumn */
 
80564 sizeof(char *)*nCol + /* Index.azColl */
80565 sizeof(u8)*nCol + /* Index.aSortOrder */
80566 nName + 1 + /* Index.zName */
80567 nExtra /* Collation sequence names */
80568 );
80569 if( db->mallocFailed ){
80570 goto exit_create_index;
80571 }
80572 pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
80573 pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
80574 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
80575 pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 
80576 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
80577 zExtra = (char *)(&pIndex->zName[nName+1]);
80578 memcpy(pIndex->zName, zName, nName+1);
80579 pIndex->pTable = pTab;
80580 pIndex->nColumn = pList->nExpr;
@@ -80847,13 +82082,13 @@
80847 ** Apart from that, we have little to go on besides intuition as to
80848 ** how aiRowEst[] should be initialized. The numbers generated here
80849 ** are based on typical values found in actual indices.
80850 */
80851 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
80852 tRowcnt *a = pIdx->aiRowEst;
80853 int i;
80854 tRowcnt n;
80855 assert( a!=0 );
80856 a[0] = pIdx->pTable->nRowEst;
80857 if( a[0]<10 ) a[0] = 10;
80858 n = 10;
80859 for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82528,13 @@
81293 ** The operator is "natural cross join". The A and B operands are stored
81294 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
81295 ** operator with A. This routine shifts that operator over to B.
81296 */
81297 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
81298 if( p && p->a ){
81299 int i;
 
81300 for(i=p->nSrc-1; i>0; i--){
81301 p->a[i].jointype = p->a[i-1].jointype;
81302 }
81303 p->a[0].jointype = 0;
81304 }
@@ -82850,10 +84086,12 @@
82850 **
82851 ** There is only one exported symbol in this file - the function
82852 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
82853 ** All other code has file scope.
82854 */
 
 
82855
82856 /*
82857 ** Return the collating function associated with a function.
82858 */
82859 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86411,28 @@
85173 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
85174 }else{
85175 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
85176 }
85177 if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
 
85178 if( !isIgnoreErrors || db->mallocFailed ) return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85179 continue;
85180 }
85181 assert( pFKey->nCol==1 || (aiFree && pIdx) );
85182
85183 if( aiFree ){
@@ -88081,10 +89336,11 @@
88081
88082 #endif /* _SQLITE3EXT_H_ */
88083
88084 /************** End of sqlite3ext.h ******************************************/
88085 /************** Continuing where we left off in loadext.c ********************/
 
88086
88087 #ifndef SQLITE_OMIT_LOAD_EXTENSION
88088
88089 /*
88090 ** Some API routines are omitted when various features are
@@ -91527,16 +92783,22 @@
91527 ){
91528 Vdbe *v = pParse->pVdbe;
91529 int nExpr = pOrderBy->nExpr;
91530 int regBase = sqlite3GetTempRange(pParse, nExpr+2);
91531 int regRecord = sqlite3GetTempReg(pParse);
 
91532 sqlite3ExprCacheClear(pParse);
91533 sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
91534 sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
91535 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
91536 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
91537 sqlite3VdbeAddOp2(v, OP_IdxInsert, pOrderBy->iECursor, regRecord);
 
 
 
 
 
91538 sqlite3ReleaseTempReg(pParse, regRecord);
91539 sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
91540 if( pSelect->iLimit ){
91541 int addr1, addr2;
91542 int iLimit;
@@ -92001,13 +93263,24 @@
92001 sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
92002 regRowid = 0;
92003 }else{
92004 regRowid = sqlite3GetTempReg(pParse);
92005 }
92006 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
92007 codeOffset(v, p, addrContinue);
92008 sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr + 1, regRow);
 
 
 
 
 
 
 
 
 
 
 
92009 switch( eDest ){
92010 case SRT_Table:
92011 case SRT_EphemTab: {
92012 testcase( eDest==SRT_Table );
92013 testcase( eDest==SRT_EphemTab );
@@ -92056,11 +93329,15 @@
92056 sqlite3ReleaseTempReg(pParse, regRowid);
92057
92058 /* The bottom of the loop
92059 */
92060 sqlite3VdbeResolveLabel(v, addrContinue);
92061 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
 
 
 
 
92062 sqlite3VdbeResolveLabel(v, addrBreak);
92063 if( eDest==SRT_Output || eDest==SRT_Coroutine ){
92064 sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
92065 }
92066 }
@@ -95022,10 +96299,14 @@
95022 /* Set the limiter.
95023 */
95024 iEnd = sqlite3VdbeMakeLabel(v);
95025 p->nSelectRow = (double)LARGEST_INT64;
95026 computeLimitRegisters(pParse, p, iEnd);
 
 
 
 
95027
95028 /* Open a virtual index to use for the distinct set.
95029 */
95030 if( p->selFlags & SF_Distinct ){
95031 KeyInfo *pKeyInfo;
@@ -95057,11 +96338,11 @@
95057 }
95058
95059 if( pWInfo->eDistinct ){
95060 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
95061
95062 assert( addrDistinctIndex>0 );
95063 pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
95064
95065 assert( isDistinct );
95066 assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
95067 || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
@@ -95116,10 +96397,12 @@
95116 ** one row of the input to the aggregator has been
95117 ** processed */
95118 int iAbortFlag; /* Mem address which causes query abort if positive */
95119 int groupBySort; /* Rows come from source in GROUP BY order */
95120 int addrEnd; /* End of processing for this SELECT */
 
 
95121
95122 /* Remove any and all aliases between the result set and the
95123 ** GROUP BY clause.
95124 */
95125 if( pGroupBy ){
@@ -95177,16 +96460,16 @@
95177 int addrReset; /* Subroutine for resetting the accumulator */
95178 int regReset; /* Return address register for reset subroutine */
95179
95180 /* If there is a GROUP BY clause we might need a sorting index to
95181 ** implement it. Allocate that sorting index now. If it turns out
95182 ** that we do not need it after all, the OpenEphemeral instruction
95183 ** will be converted into a Noop.
95184 */
95185 sAggInfo.sortingIdx = pParse->nTab++;
95186 pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
95187 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
95188 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
95189 0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
95190
95191 /* Initialize memory locations used by GROUP BY aggregate processing
95192 */
@@ -95263,15 +96546,18 @@
95263 j++;
95264 }
95265 }
95266 regRecord = sqlite3GetTempReg(pParse);
95267 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
95268 sqlite3VdbeAddOp2(v, OP_IdxInsert, sAggInfo.sortingIdx, regRecord);
95269 sqlite3ReleaseTempReg(pParse, regRecord);
95270 sqlite3ReleaseTempRange(pParse, regBase, nCol);
95271 sqlite3WhereEnd(pWInfo);
95272 sqlite3VdbeAddOp2(v, OP_Sort, sAggInfo.sortingIdx, addrEnd);
 
 
 
95273 VdbeComment((v, "GROUP BY sort"));
95274 sAggInfo.useSortingIdx = 1;
95275 sqlite3ExprCacheClear(pParse);
95276 }
95277
@@ -95280,13 +96566,17 @@
95280 ** Then compare the current GROUP BY terms against the GROUP BY terms
95281 ** from the previous row currently stored in a0, a1, a2...
95282 */
95283 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
95284 sqlite3ExprCacheClear(pParse);
 
 
 
95285 for(j=0; j<pGroupBy->nExpr; j++){
95286 if( groupBySort ){
95287 sqlite3VdbeAddOp3(v, OP_Column, sAggInfo.sortingIdx, j, iBMem+j);
 
95288 }else{
95289 sAggInfo.directMode = 1;
95290 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
95291 }
95292 }
@@ -95321,11 +96611,11 @@
95321 VdbeComment((v, "indicate data in accumulator"));
95322
95323 /* End of the loop
95324 */
95325 if( groupBySort ){
95326 sqlite3VdbeAddOp2(v, OP_Next, sAggInfo.sortingIdx, addrTopOfLoop);
95327 }else{
95328 sqlite3WhereEnd(pWInfo);
95329 sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
95330 }
95331
@@ -95650,10 +96940,12 @@
95650 ** interface routine of sqlite3_exec().
95651 **
95652 ** These routines are in a separate files so that they will not be linked
95653 ** if they are not used.
95654 */
 
 
95655
95656 #ifndef SQLITE_OMIT_GET_TABLE
95657
95658 /*
95659 ** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100451,11 @@
99159 #define TERM_CODED 0x04 /* This term is already coded */
99160 #define TERM_COPIED 0x08 /* Has a child */
99161 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
99162 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
99163 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
99164 #ifdef SQLITE_ENABLE_STAT3
99165 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
99166 #else
99167 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
99168 #endif
99169
@@ -100373,11 +101665,11 @@
100373 pNewTerm->prereqAll = pTerm->prereqAll;
100374 }
100375 }
100376 #endif /* SQLITE_OMIT_VIRTUALTABLE */
100377
100378 #ifdef SQLITE_ENABLE_STAT3
100379 /* When sqlite_stat2 histogram data is available an operator of the
100380 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
100381 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
100382 ** virtual term of that form.
100383 **
@@ -100412,11 +101704,11 @@
100412 pTerm->nChild = 1;
100413 pTerm->wtFlags |= TERM_COPIED;
100414 pNewTerm->prereqAll = pTerm->prereqAll;
100415 }
100416 }
100417 #endif /* SQLITE_ENABLE_STAT */
100418
100419 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
100420 ** an index for tables to the left of the join.
100421 */
100422 pTerm->prereqRight |= extraRight;
@@ -101461,89 +102753,71 @@
101461 */
101462 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
101463 }
101464 #endif /* SQLITE_OMIT_VIRTUALTABLE */
101465
101466 #ifdef SQLITE_ENABLE_STAT3
101467 /*
101468 ** Estimate the location of a particular key among all keys in an
101469 ** index. Store the results in aStat as follows:
101470 **
101471 ** aStat[0] Est. number of rows less than pVal
101472 ** aStat[1] Est. number of rows equal to pVal
101473 **
101474 ** Return SQLITE_OK on success.
101475 */
101476 static int whereKeyStats(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101477 Parse *pParse, /* Database connection */
101478 Index *pIdx, /* Index to consider domain of */
101479 sqlite3_value *pVal, /* Value to consider */
101480 int roundUp, /* Round up if true. Round down if false */
101481 tRowcnt *aStat /* OUT: stats written here */
101482 ){
101483 tRowcnt n;
101484 IndexSample *aSample;
101485 int i, eType;
101486 int isEq = 0;
101487 i64 v;
101488 double r, rS;
101489
101490 assert( roundUp==0 || roundUp==1 );
101491 if( pVal==0 ) return SQLITE_ERROR;
101492 n = pIdx->aiRowEst[0];
101493 aSample = pIdx->aSample;
101494 i = 0;
101495 eType = sqlite3_value_type(pVal);
101496
101497 if( eType==SQLITE_INTEGER ){
101498 v = sqlite3_value_int64(pVal);
101499 r = (i64)v;
101500 for(i=0; i<pIdx->nSample; i++){
101501 if( aSample[i].eType==SQLITE_NULL ) continue;
101502 if( aSample[i].eType>=SQLITE_TEXT ) break;
101503 if( aSample[i].eType==SQLITE_INTEGER ){
101504 if( aSample[i].u.i>=v ){
101505 isEq = aSample[i].u.i==v;
101506 break;
101507 }
101508 }else{
101509 assert( aSample[i].eType==SQLITE_FLOAT );
101510 if( aSample[i].u.r>=r ){
101511 isEq = aSample[i].u.r==r;
101512 break;
101513 }
101514 }
101515 }
101516 }else if( eType==SQLITE_FLOAT ){
101517 r = sqlite3_value_double(pVal);
101518 for(i=0; i<pIdx->nSample; i++){
101519 if( aSample[i].eType==SQLITE_NULL ) continue;
101520 if( aSample[i].eType>=SQLITE_TEXT ) break;
101521 if( aSample[i].eType==SQLITE_FLOAT ){
101522 rS = aSample[i].u.r;
101523 }else{
101524 rS = aSample[i].u.i;
101525 }
101526 if( rS>=r ){
101527 isEq = rS==r;
101528 break;
101529 }
101530 }
101531 }else if( eType==SQLITE_NULL ){
101532 i = 0;
101533 if( pIdx->nSample>=1 && aSample[0].eType==SQLITE_NULL ) isEq = 1;
101534 }else{
101535 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
101536 for(i=0; i<pIdx->nSample; i++){
101537 if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
101538 break;
101539 }
101540 }
101541 if( i<pIdx->nSample ){
101542 sqlite3 *db = pParse->db;
101543 CollSeq *pColl;
101544 const u8 *z;
 
 
 
 
 
101545 if( eType==SQLITE_BLOB ){
101546 z = (const u8 *)sqlite3_value_blob(pVal);
101547 pColl = db->pDfltColl;
101548 assert( pColl->enc==SQLITE_UTF8 );
101549 }else{
@@ -101558,16 +102832,16 @@
101558 return SQLITE_NOMEM;
101559 }
101560 assert( z && pColl && pColl->xCmp );
101561 }
101562 n = sqlite3ValueBytes(pVal, pColl->enc);
101563
101564 for(; i<pIdx->nSample; i++){
101565 int c;
101566 int eSampletype = aSample[i].eType;
101567 if( eSampletype<eType ) continue;
101568 if( eSampletype!=eType ) break;
101569 #ifndef SQLITE_OMIT_UTF16
101570 if( pColl->enc!=SQLITE_UTF8 ){
101571 int nSample;
101572 char *zSample = sqlite3Utf8to16(
101573 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102855,20 @@
101581 }else
101582 #endif
101583 {
101584 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
101585 }
101586 if( c>=0 ){
101587 if( c==0 ) isEq = 1;
101588 break;
101589 }
101590 }
101591 }
101592 }
101593
101594 /* At this point, aSample[i] is the first sample that is greater than
101595 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
101596 ** than pVal. If aSample[i]==pVal, then isEq==1.
101597 */
101598 if( isEq ){
101599 assert( i<pIdx->nSample );
101600 aStat[0] = aSample[i].nLt;
101601 aStat[1] = aSample[i].nEq;
101602 }else{
101603 tRowcnt iLower, iUpper, iGap;
101604 if( i==0 ){
101605 iLower = 0;
101606 iUpper = aSample[0].nLt;
101607 }else{
101608 iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
101609 iLower = aSample[i-1].nEq + aSample[i-1].nLt;
101610 }
101611 aStat[1] = pIdx->avgEq;
101612 if( iLower>=iUpper ){
101613 iGap = 0;
101614 }else{
101615 iGap = iUpper - iLower;
101616 if( iGap>=aStat[1]/2 ) iGap -= aStat[1]/2;
101617 }
101618 if( roundUp ){
101619 iGap = (iGap*2)/3;
101620 }else{
101621 iGap = iGap/3;
101622 }
101623 aStat[0] = iLower + iGap;
101624 }
101625 return SQLITE_OK;
101626 }
101627 #endif /* SQLITE_ENABLE_STAT3 */
101628
101629 /*
101630 ** If expression pExpr represents a literal value, set *pp to point to
101631 ** an sqlite3_value structure containing the same value, with affinity
101632 ** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102882,11 @@
101640 **
101641 ** If neither of the above apply, set *pp to NULL.
101642 **
101643 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
101644 */
101645 #ifdef SQLITE_ENABLE_STAT3
101646 static int valueFromExpr(
101647 Parse *pParse,
101648 Expr *pExpr,
101649 u8 aff,
101650 sqlite3_value **pp
@@ -101688,92 +102930,106 @@
101688 **
101689 ** ... FROM t1 WHERE a > ? AND a < ? ...
101690 **
101691 ** then nEq should be passed 0.
101692 **
101693 ** The returned value is an integer divisor to reduce the estimated
101694 ** search space. A return value of 1 means that range constraints are
101695 ** no help at all. A return value of 2 means range constraints are
101696 ** expected to reduce the search space by half. And so forth...
 
 
101697 **
101698 ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
101699 ** reduces the search space by a factor of 4. Hence a single constraint (x>?)
101700 ** results in a return of 4 and a range constraint (x>? AND x<?) results
101701 ** in a return of 16.
101702 */
101703 static int whereRangeScanEst(
101704 Parse *pParse, /* Parsing & code generating context */
101705 Index *p, /* The index containing the range-compared column; "x" */
101706 int nEq, /* index into p->aCol[] of the range-compared column */
101707 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
101708 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
101709 double *pRangeDiv /* OUT: Reduce search space by this divisor */
101710 ){
101711 int rc = SQLITE_OK;
101712
101713 #ifdef SQLITE_ENABLE_STAT3
101714
101715 if( nEq==0 && p->nSample ){
101716 sqlite3_value *pRangeVal;
101717 tRowcnt iLower = 0;
101718 tRowcnt iUpper = p->aiRowEst[0];
101719 tRowcnt a[2];
 
 
 
101720 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101721
101722 if( pLower ){
101723 Expr *pExpr = pLower->pExpr->pRight;
101724 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101725 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
101726 if( rc==SQLITE_OK
101727 && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
101728 ){
101729 iLower = a[0];
101730 if( pLower->eOperator==WO_GT ) iLower += a[1];
101731 }
101732 sqlite3ValueFree(pRangeVal);
101733 }
101734 if( rc==SQLITE_OK && pUpper ){
101735 Expr *pExpr = pUpper->pExpr->pRight;
101736 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
101737 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
101738 if( rc==SQLITE_OK
101739 && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
101740 ){
101741 iUpper = a[0];
101742 if( pUpper->eOperator==WO_LE ) iUpper += a[1];
101743 }
101744 sqlite3ValueFree(pRangeVal);
101745 }
101746 if( rc==SQLITE_OK ){
101747 if( iUpper<=iLower ){
101748 *pRangeDiv = (double)p->aiRowEst[0];
101749 }else{
101750 *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
101751 }
101752 WHERETRACE(("range scan regions: %u..%u div=%g\n",
101753 (u32)iLower, (u32)iUpper, *pRangeDiv));
101754 return SQLITE_OK;
101755 }
101756 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101757 #else
101758 UNUSED_PARAMETER(pParse);
101759 UNUSED_PARAMETER(p);
101760 UNUSED_PARAMETER(nEq);
101761 #endif
101762 assert( pLower || pUpper );
101763 *pRangeDiv = (double)1;
101764 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
101765 if( pUpper ) *pRangeDiv *= (double)4;
101766 return rc;
101767 }
101768
101769 #ifdef SQLITE_ENABLE_STAT3
101770 /*
101771 ** Estimate the number of rows that will be returned based on
101772 ** an equality constraint x=VALUE and where that VALUE occurs in
101773 ** the histogram data. This only works when x is the left-most
101774 ** column of an index and sqlite_stat3 histogram data is available
101775 ** for that index. When pExpr==NULL that means the constraint is
101776 ** "x IS NULL" instead of "x=VALUE".
101777 **
101778 ** Write the estimated row count into *pnRow and return SQLITE_OK.
101779 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +103045,14 @@
101789 Index *p, /* The index whose left-most column is pTerm */
101790 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
101791 double *pnRow /* Write the revised row estimate here */
101792 ){
101793 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
 
101794 u8 aff; /* Column affinity */
101795 int rc; /* Subfunction return code */
101796 tRowcnt a[2]; /* Statistics */
101797
101798 assert( p->aSample!=0 );
101799 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
101800 if( pExpr ){
101801 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +103059,30 @@
101802 if( rc ) goto whereEqualScanEst_cancel;
101803 }else{
101804 pRhs = sqlite3ValueNew(pParse->db);
101805 }
101806 if( pRhs==0 ) return SQLITE_NOTFOUND;
101807 rc = whereKeyStats(pParse, p, pRhs, 0, a);
101808 if( rc==SQLITE_OK ){
101809 WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
101810 *pnRow = a[1];
 
 
 
 
 
 
 
101811 }
 
101812 whereEqualScanEst_cancel:
101813 sqlite3ValueFree(pRhs);
101814 return rc;
101815 }
101816 #endif /* defined(SQLITE_ENABLE_STAT3) */
101817
101818 #ifdef SQLITE_ENABLE_STAT3
101819 /*
101820 ** Estimate the number of rows that will be returned based on
101821 ** an IN constraint where the right-hand side of the IN operator
101822 ** is a list of values. Example:
101823 **
@@ -101836,29 +103101,64 @@
101836 Parse *pParse, /* Parsing & code generating context */
101837 Index *p, /* The index whose left-most column is pTerm */
101838 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
101839 double *pnRow /* Write the revised row estimate here */
101840 ){
101841 int rc = SQLITE_OK; /* Subfunction return code */
101842 double nEst; /* Number of rows for a single term */
101843 double nRowEst = (double)0; /* New estimate of the number of rows */
101844 int i; /* Loop counter */
 
 
 
 
 
 
 
101845
101846 assert( p->aSample!=0 );
101847 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
101848 nEst = p->aiRowEst[0];
101849 rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
101850 nRowEst += nEst;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101851 }
101852 if( rc==SQLITE_OK ){
 
 
 
 
 
 
 
 
 
101853 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
101854 *pnRow = nRowEst;
101855 WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
 
101856 }
 
101857 return rc;
101858 }
101859 #endif /* defined(SQLITE_ENABLE_STAT3) */
101860
101861
101862 /*
101863 ** Find the best query plan for accessing a particular table. Write the
101864 ** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +103201,11 @@
101901 Index *pProbe; /* An index we are evaluating */
101902 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
101903 int eqTermMask; /* Current mask of valid equality operators */
101904 int idxEqTermMask; /* Index mask of valid equality operators */
101905 Index sPk; /* A fake index object for the primary key */
101906 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
101907 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
101908 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
101909
101910 /* Initialize the cost to a worst-case value */
101911 memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +103256,11 @@
101956 }
101957
101958 /* Loop over all indices looking for the best one to use
101959 */
101960 for(; pProbe; pIdx=pProbe=pProbe->pNext){
101961 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
101962 double cost; /* Cost of using pProbe */
101963 double nRow; /* Estimated number of rows in result set */
101964 double log10N; /* base-10 logarithm of nRow (inexact) */
101965 int rev; /* True to scan in reverse order */
101966 int wsFlags = 0;
@@ -101999,16 +103299,18 @@
101999 ** Set to true if there was at least one "x IN (SELECT ...)" term used
102000 ** in determining the value of nInMul. Note that the RHS of the
102001 ** IN operator must be a SELECT, not a value list, for this variable
102002 ** to be true.
102003 **
102004 ** rangeDiv:
102005 ** An estimate of a divisor by which to reduce the search space due
102006 ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
102007 ** data, a single inequality reduces the search space to 1/4rd its
102008 ** original size (rangeDiv==4). Two inequalities reduce the search
102009 ** space to 1/16th of its original size (rangeDiv==16).
 
 
102010 **
102011 ** bSort:
102012 ** Boolean. True if there is an ORDER BY clause that will require an
102013 ** external sort (i.e. scanning the index being evaluated will not
102014 ** correctly order records).
@@ -102029,17 +103331,17 @@
102029 ** SELECT a, b, c FROM tbl WHERE a = 1;
102030 */
102031 int nEq; /* Number of == or IN terms matching index */
102032 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
102033 int nInMul = 1; /* Number of distinct equalities to lookup */
102034 double rangeDiv = (double)1; /* Estimated reduction in search space */
102035 int nBound = 0; /* Number of range constraints seen */
102036 int bSort = !!pOrderBy; /* True if external sort required */
102037 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
102038 int bLookup = 0; /* True if not a covering index */
102039 WhereTerm *pTerm; /* A single term of the WHERE clause */
102040 #ifdef SQLITE_ENABLE_STAT3
102041 WhereTerm *pFirstTerm = 0; /* First term matching the index */
102042 #endif
102043
102044 /* Determine the values of nEq and nInMul */
102045 for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103361,23 @@
102059 nInMul *= pExpr->x.pList->nExpr;
102060 }
102061 }else if( pTerm->eOperator & WO_ISNULL ){
102062 wsFlags |= WHERE_COLUMN_NULL;
102063 }
102064 #ifdef SQLITE_ENABLE_STAT3
102065 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
102066 #endif
102067 used |= pTerm->prereqRight;
102068 }
102069
102070 /* Determine the value of rangeDiv */
102071 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
102072 int j = pProbe->aiColumn[nEq];
102073 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
102074 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
102075 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
102076 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
102077 if( pTop ){
102078 nBound = 1;
102079 wsFlags |= WHERE_TOP_LIMIT;
102080 used |= pTop->prereqRight;
102081 }
@@ -102143,11 +103445,11 @@
102143 if( bInEst && nRow*2>aiRowEst[0] ){
102144 nRow = aiRowEst[0]/2;
102145 nInMul = (int)(nRow / aiRowEst[nEq]);
102146 }
102147
102148 #ifdef SQLITE_ENABLE_STAT3
102149 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
102150 ** and we do not think that values of x are unique and if histogram
102151 ** data is available for column x, then it might be possible
102152 ** to get a better estimate on the number of rows based on
102153 ** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103461,16 @@
102159 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
102160 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
102161 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
102162 }
102163 }
102164 #endif /* SQLITE_ENABLE_STAT3 */
102165
102166 /* Adjust the number of output rows and downward to reflect rows
102167 ** that are excluded by range constraints.
102168 */
102169 nRow = nRow/rangeDiv;
102170 if( nRow<1 ) nRow = 1;
102171
102172 /* Experiments run on real SQLite databases show that the time needed
102173 ** to do a binary search to locate a row in a table or index is roughly
102174 ** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103595,14 @@
102293 if( nRow<2 ) nRow = 2;
102294 }
102295
102296
102297 WHERETRACE((
102298 "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
102299 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
102300 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
102301 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
102302 notReady, log10N, nRow, cost, used
102303 ));
102304
102305 /* If this index is the best we have seen so far, then record this
102306 ** index and its cost in the pCost structure.
@@ -104227,10 +105529,11 @@
104227 ** LALR(1) grammar but which are always false in the
104228 ** specific grammar used by SQLite.
104229 */
104230 /* First off, code is included that follows the "include" declaration
104231 ** in the input grammar file. */
 
104232
104233
104234 /*
104235 ** Disable all error recovery processing in the parser push-down
104236 ** automaton.
@@ -105087,10 +106390,11 @@
105087 #endif
105088 };
105089 typedef struct yyParser yyParser;
105090
105091 #ifndef NDEBUG
 
105092 static FILE *yyTraceFILE = 0;
105093 static char *yyTracePrompt = 0;
105094 #endif /* NDEBUG */
105095
105096 #ifndef NDEBUG
@@ -107662,10 +108966,11 @@
107662 **
107663 ** This file contains C code that splits an SQL input string up into
107664 ** individual tokens and sends those tokens one-by-one over to the
107665 ** parser for analysis.
107666 */
 
107667
107668 /*
107669 ** The charMap() macro maps alphabetic characters into their
107670 ** lower-case ASCII equivalent. On ASCII machines, this is just
107671 ** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110358,20 @@
109053 memcpy(&y, &x, 8);
109054 assert( sqlite3IsNaN(y) );
109055 }
109056 #endif
109057 #endif
 
 
 
 
 
 
 
 
 
 
109058
109059 return rc;
109060 }
109061
109062 /*
@@ -113184,10 +114499,16 @@
113184
113185 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
113186 # define SQLITE_CORE 1
113187 #endif
113188
 
 
 
 
 
 
113189
113190 #ifndef SQLITE_CORE
113191 SQLITE_EXTENSION_INIT1
113192 #endif
113193
@@ -117705,10 +119026,12 @@
117705 ******************************************************************************
117706 **
117707 */
117708 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
117709
 
 
117710
117711 typedef struct Fts3auxTable Fts3auxTable;
117712 typedef struct Fts3auxCursor Fts3auxCursor;
117713
117714 struct Fts3auxTable {
@@ -118243,10 +119566,12 @@
118243 /*
118244 ** Default span for NEAR operators.
118245 */
118246 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
118247
 
 
118248
118249 /*
118250 ** isNot:
118251 ** This variable is used by function getNextNode(). When getNextNode() is
118252 ** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +120269,11 @@
118944 ** Everything after this point is just test code.
118945 */
118946
118947 #ifdef SQLITE_TEST
118948
 
118949
118950 /*
118951 ** Function to query the hash-table of tokenizers (see README.tokenizers).
118952 */
118953 static int queryTestTokenizer(
@@ -119154,10 +120480,13 @@
119154 ** * The FTS3 module is being built into the core of
119155 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119156 */
119157 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119158
 
 
 
119159
119160
119161 /*
119162 ** Malloc and Free functions
119163 */
@@ -119534,10 +120863,14 @@
119534 ** * The FTS3 module is being built into the core of
119535 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
119536 */
119537 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119538
 
 
 
 
119539
119540
119541 /*
119542 ** Class derived from sqlite3_tokenizer
119543 */
@@ -120177,10 +121510,12 @@
120177 ** * The FTS3 module is being built into the core of
120178 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120179 */
120180 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120181
 
 
120182
120183 /*
120184 ** Implementation of the SQL scalar function for accessing the underlying
120185 ** hash table. This function may be called as follows:
120186 **
@@ -120352,10 +121687,12 @@
120352 }
120353
120354
120355 #ifdef SQLITE_TEST
120356
 
 
120357
120358 /*
120359 ** Implementation of a special SQL scalar function for testing tokenizers
120360 ** designed to be used in concert with the Tcl testing framework. This
120361 ** function must be called with two arguments:
@@ -120663,10 +122000,14 @@
120663 ** * The FTS3 module is being built into the core of
120664 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120665 */
120666 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120667
 
 
 
 
120668
120669
120670 typedef struct simple_tokenizer {
120671 sqlite3_tokenizer base;
120672 char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +122229,13 @@
120888 ** code in fts3.c.
120889 */
120890
120891 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120892
 
 
 
120893
120894 /*
120895 ** When full-text index nodes are loaded from disk, the buffer that they
120896 ** are loaded into has the following number of bytes of padding at the end
120897 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125493,12 @@
124149 ******************************************************************************
124150 */
124151
124152 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124153
 
 
124154
124155 /*
124156 ** Characters that may appear in the second argument to matchinfo().
124157 */
124158 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +127082,12 @@
125736 #ifndef SQLITE_CORE
125737 SQLITE_EXTENSION_INIT1
125738 #else
125739 #endif
125740
 
 
125741
125742 #ifndef SQLITE_AMALGAMATION
125743 #include "sqlite3rtree.h"
125744 typedef sqlite3_int64 i64;
125745 typedef unsigned char u8;
@@ -128950,10 +130298,11 @@
128950 #include <unicode/utypes.h>
128951 #include <unicode/uregex.h>
128952 #include <unicode/ustring.h>
128953 #include <unicode/ucol.h>
128954
 
128955
128956 #ifndef SQLITE_CORE
128957 SQLITE_EXTENSION_INIT1
128958 #else
128959 #endif
@@ -129429,12 +130778,16 @@
129429 ** This file implements a tokenizer for fts3 based on the ICU library.
129430 */
129431 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
129432 #ifdef SQLITE_ENABLE_ICU
129433
 
 
129434
129435 #include <unicode/ubrk.h>
 
 
129436 #include <unicode/utf16.h>
129437
129438 typedef struct IcuTokenizer IcuTokenizer;
129439 typedef struct IcuCursor IcuCursor;
129440
129441
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -385,23 +385,29 @@
385 /*
386 ** Exactly one of the following macros must be defined in order to
387 ** specify which memory allocation subsystem to use.
388 **
389 ** SQLITE_SYSTEM_MALLOC // Use normal system malloc()
390 ** SQLITE_WIN32_MALLOC // Use Win32 native heap API
391 ** SQLITE_MEMDEBUG // Debugging version of system malloc()
392 **
393 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
394 ** assert() macro is enabled, each call into the Win32 native heap subsystem
395 ** will cause HeapValidate to be called. If heap validation should fail, an
396 ** assertion will be triggered.
397 **
398 ** (Historical note: There used to be several other options, but we've
399 ** pared it down to just these two.)
400 **
401 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402 ** the default.
403 */
404 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
405 # error "At most one of the following compile-time configuration options\
406 is allows: SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG"
407 #endif
408 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)==0
409 # define SQLITE_SYSTEM_MALLOC 1
410 #endif
411
412 /*
413 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
@@ -650,11 +656,11 @@
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.7.8"
660 #define SQLITE_VERSION_NUMBER 3007008
661 #define SQLITE_SOURCE_ID "2011-09-04 01:27:00 6b657ae75035eb10b0ad640998d3c9eadfdffa6e"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1751,20 +1757,14 @@
1757 ** also used during testing of SQLite in order to specify an alternative
1758 ** memory allocator that simulates memory out-of-memory conditions in
1759 ** order to verify that SQLite recovers gracefully from such
1760 ** conditions.
1761 **
1762 ** The xMalloc, xRealloc, and xFree methods must work like the
1763 ** malloc(), realloc() and free() functions from the standard C library.
1764 ** ^SQLite guarantees that the second argument to
 
 
 
1765 ** xRealloc is always a value returned by a prior call to xRoundup.
 
 
 
1766 **
1767 ** xSize should return the allocated size of a memory allocation
1768 ** previously obtained from xMalloc or xRealloc. The allocated size
1769 ** is always at least as big as the requested size but may be larger.
1770 **
@@ -3397,11 +3397,11 @@
3397 ** a schema change, on the first [sqlite3_step()] call following any change
3398 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3399 ** ^The specific value of WHERE-clause [parameter] might influence the
3400 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3401 ** or [GLOB] operator or if the parameter is compared to an indexed column
3402 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3403 ** the
3404 ** </li>
3405 ** </ol>
3406 */
3407 SQLITE_API int sqlite3_prepare(
@@ -7711,22 +7711,10 @@
7711 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7712 ** have to specify the value in the less intuitive manner shown:
7713 */
7714 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7715
 
 
 
 
 
 
 
 
 
 
 
 
7716 /*
7717 ** Macros to determine whether the machine is big or little endian,
7718 ** evaluated at runtime.
7719 */
7720 #ifdef SQLITE_AMALGAMATION
@@ -8188,10 +8176,11 @@
8176 ** or VDBE. The VDBE implements an abstract machine that runs a
8177 ** simple program to access and modify the underlying database.
8178 */
8179 #ifndef _SQLITE_VDBE_H_
8180 #define _SQLITE_VDBE_H_
8181 /* #include <stdio.h> */
8182
8183 /*
8184 ** A single VDBE is an opaque structure named "Vdbe". Only routines
8185 ** in the source file sqliteVdbe.c are allowed to see the insides
8186 ** of this structure.
@@ -8231,10 +8220,11 @@
8220 Mem *pMem; /* Used when p4type is P4_MEM */
8221 VTable *pVtab; /* Used when p4type is P4_VTAB */
8222 KeyInfo *pKeyInfo; /* Used when p4type is P4_KEYINFO */
8223 int *ai; /* Used when p4type is P4_INTARRAY */
8224 SubProgram *pProgram; /* Used when p4type is P4_SUBPROGRAM */
8225 int (*xAdvance)(BtCursor *, int *);
8226 } p4;
8227 #ifdef SQLITE_DEBUG
8228 char *zComment; /* Comment to improve readability */
8229 #endif
8230 #ifdef VDBE_PROFILE
@@ -8286,10 +8276,11 @@
8276 #define P4_REAL (-12) /* P4 is a 64-bit floating point value */
8277 #define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
8278 #define P4_INT32 (-14) /* P4 is a 32-bit signed integer */
8279 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
8280 #define P4_SUBPROGRAM (-18) /* P4 is a pointer to a SubProgram structure */
8281 #define P4_ADVANCE (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
8282
8283 /* When adding a P4 argument using P4_KEYINFO, a copy of the KeyInfo structure
8284 ** is made. That copy is freed when the Vdbe is finalized. But if the
8285 ** argument is P4_KEYINFO_HANDOFF, the passed in pointer is used. It still
8286 ** gets freed when the Vdbe is finalized so it still should be obtained
@@ -8401,88 +8392,90 @@
8392 #define OP_VerifyCookie 37
8393 #define OP_OpenRead 38
8394 #define OP_OpenWrite 39
8395 #define OP_OpenAutoindex 40
8396 #define OP_OpenEphemeral 41
8397 #define OP_SorterOpen 42
8398 #define OP_OpenPseudo 43
8399 #define OP_Close 44
8400 #define OP_SeekLt 45
8401 #define OP_SeekLe 46
8402 #define OP_SeekGe 47
8403 #define OP_SeekGt 48
8404 #define OP_Seek 49
8405 #define OP_NotFound 50
8406 #define OP_Found 51
8407 #define OP_IsUnique 52
8408 #define OP_NotExists 53
8409 #define OP_Sequence 54
8410 #define OP_NewRowid 55
8411 #define OP_Insert 56
8412 #define OP_InsertInt 57
8413 #define OP_Delete 58
8414 #define OP_ResetCount 59
8415 #define OP_SorterCompare 60
8416 #define OP_SorterData 61
8417 #define OP_RowKey 62
8418 #define OP_RowData 63
8419 #define OP_Rowid 64
8420 #define OP_NullRow 65
8421 #define OP_Last 66
8422 #define OP_SorterSort 67
8423 #define OP_Sort 70
8424 #define OP_Rewind 71
8425 #define OP_SorterNext 72
8426 #define OP_Prev 81
8427 #define OP_Next 92
8428 #define OP_SorterInsert 95
8429 #define OP_IdxInsert 96
8430 #define OP_IdxDelete 97
8431 #define OP_IdxRowid 98
8432 #define OP_IdxLT 99
8433 #define OP_IdxGE 100
8434 #define OP_Destroy 101
8435 #define OP_Clear 102
8436 #define OP_CreateIndex 103
8437 #define OP_CreateTable 104
8438 #define OP_ParseSchema 105
8439 #define OP_LoadAnalysis 106
8440 #define OP_DropTable 107
8441 #define OP_DropIndex 108
8442 #define OP_DropTrigger 109
8443 #define OP_IntegrityCk 110
8444 #define OP_RowSetAdd 111
8445 #define OP_RowSetRead 112
8446 #define OP_RowSetTest 113
8447 #define OP_Program 114
8448 #define OP_Param 115
8449 #define OP_FkCounter 116
8450 #define OP_FkIfZero 117
8451 #define OP_MemMax 118
8452 #define OP_IfPos 119
8453 #define OP_IfNeg 120
8454 #define OP_IfZero 121
8455 #define OP_AggStep 122
8456 #define OP_AggFinal 123
8457 #define OP_Checkpoint 124
8458 #define OP_JournalMode 125
8459 #define OP_Vacuum 126
8460 #define OP_IncrVacuum 127
8461 #define OP_Expire 128
8462 #define OP_TableLock 129
8463 #define OP_VBegin 131
8464 #define OP_VCreate 132
8465 #define OP_VDestroy 133
8466 #define OP_VOpen 134
8467 #define OP_VFilter 135
8468 #define OP_VColumn 136
8469 #define OP_VNext 137
8470 #define OP_VRename 138
8471 #define OP_VUpdate 139
8472 #define OP_Pagecount 140
8473 #define OP_MaxPgcnt 146
8474 #define OP_Trace 147
8475 #define OP_Noop 148
8476 #define OP_Explain 149
8477
8478
8479 /* Properties such as "out2" or "jump" that are specified in
8480 ** comments following the "case" for each opcode in the vdbe.c
8481 ** are encoded into bitvectors as follows:
@@ -8498,24 +8491,24 @@
8491 /* 0 */ 0x00, 0x01, 0x05, 0x04, 0x04, 0x10, 0x00, 0x02,\
8492 /* 8 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x24, 0x24,\
8493 /* 16 */ 0x00, 0x00, 0x00, 0x24, 0x04, 0x05, 0x04, 0x00,\
8494 /* 24 */ 0x00, 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02,\
8495 /* 32 */ 0x00, 0x00, 0x00, 0x02, 0x10, 0x00, 0x00, 0x00,\
8496 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11,\
8497 /* 48 */ 0x11, 0x08, 0x11, 0x11, 0x11, 0x11, 0x02, 0x02,\
8498 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
8499 /* 64 */ 0x02, 0x00, 0x01, 0x01, 0x4c, 0x4c, 0x01, 0x01,\
8500 /* 72 */ 0x01, 0x05, 0x05, 0x15, 0x15, 0x15, 0x15, 0x15,\
8501 /* 80 */ 0x15, 0x01, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c,\
8502 /* 88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x01, 0x24, 0x02, 0x08,\
8503 /* 96 */ 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00, 0x02,\
8504 /* 104 */ 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c,\
8505 /* 112 */ 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08, 0x05,\
8506 /* 120 */ 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x01,\
8507 /* 128 */ 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01,\
8508 /* 136 */ 0x00, 0x01, 0x00, 0x00, 0x02, 0x04, 0x04, 0x04,\
8509 /* 144 */ 0x04, 0x04, 0x02, 0x00, 0x00, 0x00,}
8510
8511 /************** End of opcodes.h *********************************************/
8512 /************** Continuing where we left off in vdbe.h ***********************/
8513
8514 /*
@@ -8529,13 +8522,13 @@
8522 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
8523 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
8524 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
8525 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
8526 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
8527 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
8528 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
8529 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
8530 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
8531 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
8532 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr, int N);
8533 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
8534 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
@@ -8653,10 +8646,11 @@
8646 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
8647 */
8648 #define PAGER_OMIT_JOURNAL 0x0001 /* Do not use a rollback journal */
8649 #define PAGER_NO_READLOCK 0x0002 /* Omit readlocks on readonly files */
8650 #define PAGER_MEMORY 0x0004 /* In-memory database */
8651 #define PAGER_SORTER 0x0020 /* Accumulator in external merge sort */
8652
8653 /*
8654 ** Valid values for the second argument to sqlite3PagerLockingMode().
8655 */
8656 #define PAGER_LOCKINGMODE_QUERY -1
@@ -9924,11 +9918,11 @@
9918 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9919 int nCol; /* Number of columns in this table */
9920 Column *aCol; /* Information about each column */
9921 Index *pIndex; /* List of SQL indexes on this table. */
9922 int tnum; /* Root BTree node for this table (see note above) */
9923 unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9924 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9925 u16 nRef; /* Number of pointers to this Table */
9926 u8 tabFlags; /* Mask of TF_* values */
9927 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9928 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10123,43 +10117,35 @@
10117 */
10118 struct Index {
10119 char *zName; /* Name of this index */
10120 int nColumn; /* Number of columns in the table used by this index */
10121 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10122 unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10123 Table *pTable; /* The SQL table being indexed */
10124 int tnum; /* Page containing root of this index in database file */
10125 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10126 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10127 u8 bUnordered; /* Use this index for == or IN queries only */
 
10128 char *zColAff; /* String defining the affinity of each column */
10129 Index *pNext; /* The next index associated with the same table */
10130 Schema *pSchema; /* Schema containing this index */
10131 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10132 char **azColl; /* Array of collation sequence names for index */
10133 IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
 
 
 
10134 };
10135
10136 /*
10137 ** Each sample stored in the sqlite_stat2 table is represented in memory
10138 ** using a structure of this type.
10139 */
10140 struct IndexSample {
10141 union {
10142 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10143 double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
 
10144 } u;
10145 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10146 u8 nByte; /* Size in byte of text or blob. */
 
 
 
10147 };
10148
10149 /*
10150 ** Each token coming out of the lexer is an instance of
10151 ** this structure. Tokens are also used as part of an expression.
@@ -10190,10 +10176,11 @@
10176 u8 directMode; /* Direct rendering mode means take data directly
10177 ** from source tables rather than from accumulators */
10178 u8 useSortingIdx; /* In direct mode, reference the sorting index rather
10179 ** than the source table */
10180 int sortingIdx; /* Cursor number of the sorting index */
10181 int sortingIdxPTab; /* Cursor number of pseudo-table */
10182 ExprList *pGroupBy; /* The group by clause */
10183 int nSortingColumn; /* Number of columns in the sorting index */
10184 struct AggInfo_col { /* For each column used in source tables */
10185 Table *pTab; /* Source table */
10186 int iTable; /* Cursor number of the source table */
@@ -10722,10 +10709,11 @@
10709 #define SF_Resolved 0x0002 /* Identifiers have been resolved */
10710 #define SF_Aggregate 0x0004 /* Contains aggregate functions */
10711 #define SF_UsesEphemeral 0x0008 /* Uses the OpenEphemeral opcode */
10712 #define SF_Expanded 0x0010 /* sqlite3SelectExpand() called on this */
10713 #define SF_HasTypeInfo 0x0020 /* FROM subqueries have Table metadata */
10714 #define SF_UseSorter 0x0040 /* Sort using a sorter */
10715
10716
10717 /*
10718 ** The results of a select can be distributed in several ways. The
10719 ** "SRT" prefix means "SELECT Result Type".
@@ -11361,11 +11349,10 @@
11349 #else
11350 # define sqlite3ViewGetColumnNames(A,B) 0
11351 #endif
11352
11353 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 
11354 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11355 #ifndef SQLITE_OMIT_AUTOINCREMENT
11356 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11357 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11358 #else
@@ -11618,11 +11605,11 @@
11605 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11606 void(*)(void*));
11607 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11608 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11609 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11610 #ifdef SQLITE_ENABLE_STAT2
11611 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11612 #endif
11613 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11614 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11615 #ifndef SQLITE_AMALGAMATION
@@ -12245,13 +12232,10 @@
12232 "ENABLE_RTREE",
12233 #endif
12234 #ifdef SQLITE_ENABLE_STAT2
12235 "ENABLE_STAT2",
12236 #endif
 
 
 
12237 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12238 "ENABLE_UNLOCK_NOTIFY",
12239 #endif
12240 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
12241 "ENABLE_UPDATE_DELETE_LIMIT",
@@ -12275,10 +12259,13 @@
12259 "INT64_TYPE",
12260 #endif
12261 #ifdef SQLITE_LOCK_TRACE
12262 "LOCK_TRACE",
12263 #endif
12264 #ifdef SQLITE_MAX_SCHEMA_RETRY
12265 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
12266 #endif
12267 #ifdef SQLITE_MEMDEBUG
12268 "MEMDEBUG",
12269 #endif
12270 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
12271 "MIXED_ENDIAN_64BIT_FLOAT",
@@ -12387,10 +12374,13 @@
12374 #ifdef SQLITE_OMIT_LOOKASIDE
12375 "OMIT_LOOKASIDE",
12376 #endif
12377 #ifdef SQLITE_OMIT_MEMORYDB
12378 "OMIT_MEMORYDB",
12379 #endif
12380 #ifdef SQLITE_OMIT_MERGE_SORT
12381 "OMIT_MERGE_SORT",
12382 #endif
12383 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
12384 "OMIT_OR_OPTIMIZATION",
12385 #endif
12386 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
@@ -12453,10 +12443,13 @@
12443 #ifdef SQLITE_OMIT_WSD
12444 "OMIT_WSD",
12445 #endif
12446 #ifdef SQLITE_OMIT_XFER_OPT
12447 "OMIT_XFER_OPT",
12448 #endif
12449 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
12450 "PAGECACHE_BLOCKALLOC",
12451 #endif
12452 #ifdef SQLITE_PERFORMANCE_TRACE
12453 "PERFORMANCE_TRACE",
12454 #endif
12455 #ifdef SQLITE_PROXY_DEBUG
@@ -12574,10 +12567,13 @@
12567 /*
12568 ** Boolean values
12569 */
12570 typedef unsigned char Bool;
12571
12572 /* Opaque type used by code in vdbesort.c */
12573 typedef struct VdbeSorter VdbeSorter;
12574
12575 /*
12576 ** A cursor is a pointer into a single BTree within a database file.
12577 ** The cursor can seek to a BTree entry with a particular key, or
12578 ** loop over all entries of the Btree. You can also insert new BTree
12579 ** entries or retrieve the key or data from the entry that the cursor
@@ -12600,15 +12596,17 @@
12596 Bool nullRow; /* True if pointing to a row with no data */
12597 Bool deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
12598 Bool isTable; /* True if a table requiring integer keys */
12599 Bool isIndex; /* True if an index containing keys only - no data */
12600 Bool isOrdered; /* True if the underlying table is BTREE_UNORDERED */
12601 Bool isSorter; /* True if a new-style sorter */
12602 sqlite3_vtab_cursor *pVtabCursor; /* The cursor for a virtual table */
12603 const sqlite3_module *pModule; /* Module for cursor pVtabCursor */
12604 i64 seqCount; /* Sequence counter */
12605 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
12606 i64 lastRowid; /* Last rowid from a Next or NextIdx operation */
12607 VdbeSorter *pSorter; /* Sorter object for OP_SorterOpen cursors */
12608
12609 /* Result of last sqlite3BtreeMoveto() done by an OP_NotExists or
12610 ** OP_IsUnique opcode on this cursor. */
12611 int seekResult;
12612
@@ -12924,17 +12922,38 @@
12922 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
12923 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
12924 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,int,int,int,Mem*);
12925 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
12926 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
12927 #define MemReleaseExt(X) \
12928 if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
12929 sqlite3VdbeMemReleaseExternal(X);
12930 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
12931 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
12932 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
12933 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
12934 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
12935 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
12936 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
12937
12938 #ifdef SQLITE_OMIT_MERGE_SORT
12939 # define sqlite3VdbeSorterInit(Y,Z) SQLITE_OK
12940 # define sqlite3VdbeSorterWrite(X,Y,Z) SQLITE_OK
12941 # define sqlite3VdbeSorterClose(Y,Z)
12942 # define sqlite3VdbeSorterRowkey(Y,Z) SQLITE_OK
12943 # define sqlite3VdbeSorterRewind(X,Y,Z) SQLITE_OK
12944 # define sqlite3VdbeSorterNext(X,Y,Z) SQLITE_OK
12945 # define sqlite3VdbeSorterCompare(X,Y,Z) SQLITE_OK
12946 #else
12947 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
12948 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
12949 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *, Mem *);
12950 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, VdbeCursor *, int *);
12951 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, VdbeCursor *, int *);
12952 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, VdbeCursor *, Mem *);
12953 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(VdbeCursor *, Mem *, int *);
12954 #endif
12955
12956 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
12957 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe*);
12958 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe*);
12959 #else
@@ -13225,10 +13244,12 @@
13244 ** Astronomical Algorithms, 2nd Edition, 1998
13245 ** ISBM 0-943396-61-1
13246 ** Willmann-Bell, Inc
13247 ** Richmond, Virginia (USA)
13248 */
13249 /* #include <stdlib.h> */
13250 /* #include <assert.h> */
13251 #include <time.h>
13252
13253 #ifndef SQLITE_OMIT_DATETIME_FUNCS
13254
13255
@@ -14978,10 +14999,11 @@
14999 extern void backtrace_symbols_fd(void*const*,int,int);
15000 #else
15001 # define backtrace(A,B) 1
15002 # define backtrace_symbols_fd(A,B,C)
15003 #endif
15004 /* #include <stdio.h> */
15005
15006 /*
15007 ** Each memory allocation looks like this:
15008 **
15009 ** ------------------------------------------------------------------------
@@ -18081,10 +18103,11 @@
18103 **
18104 *************************************************************************
18105 **
18106 ** Memory allocation functions used throughout sqlite.
18107 */
18108 /* #include <stdarg.h> */
18109
18110 /*
18111 ** Attempt to release up to n bytes of non-essential memory currently
18112 ** held by SQLite. An example of non-essential memory is memory used to
18113 ** cache database pages that are not currently in use.
@@ -20058,10 +20081,11 @@
20081 ** BOM or Byte Order Mark:
20082 ** 0xff 0xfe little-endian utf-16 follows
20083 ** 0xfe 0xff big-endian utf-16 follows
20084 **
20085 */
20086 /* #include <assert.h> */
20087
20088 #ifndef SQLITE_AMALGAMATION
20089 /*
20090 ** The following constant value is used by the SQLITE_BIGENDIAN and
20091 ** SQLITE_LITTLEENDIAN macros.
@@ -20486,11 +20510,11 @@
20510 ** no longer required.
20511 **
20512 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20513 ** flag set.
20514 */
20515 #ifdef SQLITE_ENABLE_STAT2
20516 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20517 Mem m;
20518 memset(&m, 0, sizeof(m));
20519 m.db = db;
20520 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -20600,10 +20624,11 @@
20624 **
20625 ** This file contains functions for allocating memory, comparing
20626 ** strings, and stuff like that.
20627 **
20628 */
20629 /* #include <stdarg.h> */
20630 #ifdef SQLITE_HAVE_ISNAN
20631 # include <math.h>
20632 #endif
20633
20634 /*
@@ -21778,10 +21803,11 @@
21803 **
21804 *************************************************************************
21805 ** This is the implementation of generic hash-tables
21806 ** used in SQLite.
21807 */
21808 /* #include <assert.h> */
21809
21810 /* Turn bulk memory into a hash table object by initializing the
21811 ** fields of the Hash structure.
21812 **
21813 ** "pNew" is a pointer to the hash table that is to be initialized.
@@ -22088,50 +22114,50 @@
22114 /* 37 */ "VerifyCookie",
22115 /* 38 */ "OpenRead",
22116 /* 39 */ "OpenWrite",
22117 /* 40 */ "OpenAutoindex",
22118 /* 41 */ "OpenEphemeral",
22119 /* 42 */ "SorterOpen",
22120 /* 43 */ "OpenPseudo",
22121 /* 44 */ "Close",
22122 /* 45 */ "SeekLt",
22123 /* 46 */ "SeekLe",
22124 /* 47 */ "SeekGe",
22125 /* 48 */ "SeekGt",
22126 /* 49 */ "Seek",
22127 /* 50 */ "NotFound",
22128 /* 51 */ "Found",
22129 /* 52 */ "IsUnique",
22130 /* 53 */ "NotExists",
22131 /* 54 */ "Sequence",
22132 /* 55 */ "NewRowid",
22133 /* 56 */ "Insert",
22134 /* 57 */ "InsertInt",
22135 /* 58 */ "Delete",
22136 /* 59 */ "ResetCount",
22137 /* 60 */ "SorterCompare",
22138 /* 61 */ "SorterData",
22139 /* 62 */ "RowKey",
22140 /* 63 */ "RowData",
22141 /* 64 */ "Rowid",
22142 /* 65 */ "NullRow",
22143 /* 66 */ "Last",
22144 /* 67 */ "SorterSort",
22145 /* 68 */ "Or",
22146 /* 69 */ "And",
22147 /* 70 */ "Sort",
22148 /* 71 */ "Rewind",
22149 /* 72 */ "SorterNext",
22150 /* 73 */ "IsNull",
22151 /* 74 */ "NotNull",
22152 /* 75 */ "Ne",
22153 /* 76 */ "Eq",
22154 /* 77 */ "Gt",
22155 /* 78 */ "Le",
22156 /* 79 */ "Lt",
22157 /* 80 */ "Ge",
22158 /* 81 */ "Prev",
22159 /* 82 */ "BitAnd",
22160 /* 83 */ "BitOr",
22161 /* 84 */ "ShiftLeft",
22162 /* 85 */ "ShiftRight",
22163 /* 86 */ "Add",
@@ -22138,64 +22164,68 @@
22164 /* 87 */ "Subtract",
22165 /* 88 */ "Multiply",
22166 /* 89 */ "Divide",
22167 /* 90 */ "Remainder",
22168 /* 91 */ "Concat",
22169 /* 92 */ "Next",
22170 /* 93 */ "BitNot",
22171 /* 94 */ "String8",
22172 /* 95 */ "SorterInsert",
22173 /* 96 */ "IdxInsert",
22174 /* 97 */ "IdxDelete",
22175 /* 98 */ "IdxRowid",
22176 /* 99 */ "IdxLT",
22177 /* 100 */ "IdxGE",
22178 /* 101 */ "Destroy",
22179 /* 102 */ "Clear",
22180 /* 103 */ "CreateIndex",
22181 /* 104 */ "CreateTable",
22182 /* 105 */ "ParseSchema",
22183 /* 106 */ "LoadAnalysis",
22184 /* 107 */ "DropTable",
22185 /* 108 */ "DropIndex",
22186 /* 109 */ "DropTrigger",
22187 /* 110 */ "IntegrityCk",
22188 /* 111 */ "RowSetAdd",
22189 /* 112 */ "RowSetRead",
22190 /* 113 */ "RowSetTest",
22191 /* 114 */ "Program",
22192 /* 115 */ "Param",
22193 /* 116 */ "FkCounter",
22194 /* 117 */ "FkIfZero",
22195 /* 118 */ "MemMax",
22196 /* 119 */ "IfPos",
22197 /* 120 */ "IfNeg",
22198 /* 121 */ "IfZero",
22199 /* 122 */ "AggStep",
22200 /* 123 */ "AggFinal",
22201 /* 124 */ "Checkpoint",
22202 /* 125 */ "JournalMode",
22203 /* 126 */ "Vacuum",
22204 /* 127 */ "IncrVacuum",
22205 /* 128 */ "Expire",
22206 /* 129 */ "TableLock",
22207 /* 130 */ "Real",
22208 /* 131 */ "VBegin",
22209 /* 132 */ "VCreate",
22210 /* 133 */ "VDestroy",
22211 /* 134 */ "VOpen",
22212 /* 135 */ "VFilter",
22213 /* 136 */ "VColumn",
22214 /* 137 */ "VNext",
22215 /* 138 */ "VRename",
22216 /* 139 */ "VUpdate",
22217 /* 140 */ "Pagecount",
22218 /* 141 */ "ToText",
22219 /* 142 */ "ToBlob",
22220 /* 143 */ "ToNumeric",
22221 /* 144 */ "ToInt",
22222 /* 145 */ "ToReal",
22223 /* 146 */ "MaxPgcnt",
22224 /* 147 */ "Trace",
22225 /* 148 */ "Noop",
22226 /* 149 */ "Explain",
22227 };
22228 return azName[i];
22229 }
22230 #endif
22231
@@ -22286,11 +22316,11 @@
22316 */
22317 #ifdef MEMORY_DEBUG
22318 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
22319 #endif
22320
22321 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
22322 # ifndef SQLITE_DEBUG_OS_TRACE
22323 # define SQLITE_DEBUG_OS_TRACE 0
22324 # endif
22325 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
22326 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -24450,10 +24480,11 @@
24480 */
24481 #include <sys/types.h>
24482 #include <sys/stat.h>
24483 #include <fcntl.h>
24484 #include <unistd.h>
24485 /* #include <time.h> */
24486 #include <sys/time.h>
24487 #include <errno.h>
24488 #ifndef SQLITE_OMIT_WAL
24489 #include <sys/mman.h>
24490 #endif
@@ -24485,10 +24516,11 @@
24516 /*
24517 ** If we are to be thread-safe, include the pthreads header and define
24518 ** the SQLITE_UNIX_THREADS macro.
24519 */
24520 #if SQLITE_THREADSAFE
24521 /* # include <pthread.h> */
24522 # define SQLITE_UNIX_THREADS 1
24523 #endif
24524
24525 /*
24526 ** Default permissions when creating a new file
@@ -24584,11 +24616,15 @@
24616 ** Allowed values for the unixFile.ctrlFlags bitmask:
24617 */
24618 #define UNIXFILE_EXCL 0x01 /* Connections from one process only */
24619 #define UNIXFILE_RDONLY 0x02 /* Connection is read only */
24620 #define UNIXFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
24621 #ifndef SQLITE_DISABLE_DIRSYNC
24622 # define UNIXFILE_DIRSYNC 0x08 /* Directory sync needed */
24623 #else
24624 # define UNIXFILE_DIRSYNC 0x00
24625 #endif
24626
24627 /*
24628 ** Include code that is common to all os_*.c files
24629 */
24630 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -24622,11 +24658,11 @@
24658 */
24659 #ifdef MEMORY_DEBUG
24660 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
24661 #endif
24662
24663 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
24664 # ifndef SQLITE_DEBUG_OS_TRACE
24665 # define SQLITE_DEBUG_OS_TRACE 0
24666 # endif
24667 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
24668 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -27061,15 +27097,16 @@
27097 */
27098 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
27099 int rc = SQLITE_OK;
27100 int reserved = 0;
27101 unixFile *pFile = (unixFile*)id;
27102 afpLockingContext *context;
27103
27104 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
27105
27106 assert( pFile );
27107 context = (afpLockingContext *) pFile->lockingContext;
27108 if( context->reserved ){
27109 *pResOut = 1;
27110 return SQLITE_OK;
27111 }
27112 unixEnterMutex(); /* Because pFile->pInode is shared across threads */
@@ -27205,11 +27242,11 @@
27242
27243 /* If control gets to this point, then actually go ahead and make
27244 ** operating system calls for the specified lock.
27245 */
27246 if( eFileLock==SHARED_LOCK ){
27247 int lrc1, lrc2, lrc1Errno = 0;
27248 long lk, mask;
27249
27250 assert( pInode->nShared==0 );
27251 assert( pInode->eFileLock==0 );
27252
@@ -27579,21 +27616,23 @@
27616 #if defined(USE_PREAD)
27617 do{ got = osPwrite(id->h, pBuf, cnt, offset); }while( got<0 && errno==EINTR );
27618 #elif defined(USE_PREAD64)
27619 do{ got = osPwrite64(id->h, pBuf, cnt, offset);}while( got<0 && errno==EINTR);
27620 #else
27621 do{
27622 newOffset = lseek(id->h, offset, SEEK_SET);
27623 SimulateIOError( newOffset-- );
27624 if( newOffset!=offset ){
27625 if( newOffset == -1 ){
27626 ((unixFile*)id)->lastErrno = errno;
27627 }else{
27628 ((unixFile*)id)->lastErrno = 0;
27629 }
27630 return -1;
27631 }
27632 got = osWrite(id->h, pBuf, cnt);
27633 }while( got<0 && errno==EINTR );
27634 #endif
27635 TIMER_END;
27636 if( got<0 ){
27637 ((unixFile*)id)->lastErrno = errno;
27638 }
@@ -27679,15 +27718,15 @@
27718 SQLITE_API int sqlite3_fullsync_count = 0;
27719 #endif
27720
27721 /*
27722 ** We do not trust systems to provide a working fdatasync(). Some do.
27723 ** Others do no. To be safe, we will stick with the (slightly slower)
27724 ** fsync(). If you know that your system does support fdatasync() correctly,
27725 ** then simply compile with -Dfdatasync=fdatasync
27726 */
27727 #if !defined(fdatasync)
27728 # define fdatasync fsync
27729 #endif
27730
27731 /*
27732 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
@@ -27888,10 +27927,12 @@
27927 HAVE_FULLFSYNC, isFullsync));
27928 rc = osOpenDirectory(pFile->zPath, &dirfd);
27929 if( rc==SQLITE_OK && dirfd>=0 ){
27930 full_fsync(dirfd, 0, 0);
27931 robust_close(pFile, dirfd, __LINE__);
27932 }else if( rc==SQLITE_CANTOPEN ){
27933 rc = SQLITE_OK;
27934 }
27935 pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27936 }
27937 return rc;
27938 }
@@ -27971,30 +28012,22 @@
28012 static int proxyFileControl(sqlite3_file*,int,void*);
28013 #endif
28014
28015 /*
28016 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
28017 ** file-control operation. Enlarge the database to nBytes in size
28018 ** (rounded up to the next chunk-size). If the database is already
28019 ** nBytes or larger, this routine is a no-op.
 
 
28020 */
28021 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
28022 if( pFile->szChunk>0 ){
28023 i64 nSize; /* Required file size */
 
28024 struct stat buf; /* Used to hold return values of fstat() */
28025
28026 if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
28027
28028 nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
 
 
 
 
 
28029 if( nSize>(i64)buf.st_size ){
28030
28031 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
28032 /* The code below is handling the return value of osFallocate()
28033 ** correctly. posix_fallocate() is defined to "returns zero on success,
@@ -28048,11 +28081,15 @@
28081 case SQLITE_FCNTL_CHUNK_SIZE: {
28082 pFile->szChunk = *(int *)pArg;
28083 return SQLITE_OK;
28084 }
28085 case SQLITE_FCNTL_SIZE_HINT: {
28086 int rc;
28087 SimulateIOErrorBenign(1);
28088 rc = fcntlSizeHint(pFile, *(i64 *)pArg);
28089 SimulateIOErrorBenign(0);
28090 return rc;
28091 }
28092 case SQLITE_FCNTL_PERSIST_WAL: {
28093 int bPersist = *(int*)pArg;
28094 if( bPersist<0 ){
28095 *(int*)pArg = (pFile->ctrlFlags & UNIXFILE_PERSIST_WAL)!=0;
@@ -28175,15 +28212,13 @@
28212 */
28213 struct unixShm {
28214 unixShmNode *pShmNode; /* The underlying unixShmNode object */
28215 unixShm *pNext; /* Next unixShm with the same unixShmNode */
28216 u8 hasMutex; /* True if holding the unixShmNode mutex */
28217 u8 id; /* Id of this connection within its unixShmNode */
28218 u16 sharedMask; /* Mask of shared locks held */
28219 u16 exclMask; /* Mask of exclusive locks held */
 
 
 
28220 };
28221
28222 /*
28223 ** Constants used for locking
28224 */
@@ -29485,10 +29520,13 @@
29520 int isReadonly = (flags & SQLITE_OPEN_READONLY);
29521 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
29522 #if SQLITE_ENABLE_LOCKING_STYLE
29523 int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY);
29524 #endif
29525 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29526 struct statfs fsInfo;
29527 #endif
29528
29529 /* If creating a master or main-file journal, this function will open
29530 ** a file-descriptor on the directory too. The first time unixSync()
29531 ** is called the directory file descriptor will be fsync()ed and close()d.
29532 */
@@ -29617,11 +29655,10 @@
29655
29656 noLock = eType!=SQLITE_OPEN_MAIN_DB;
29657
29658
29659 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
 
29660 if( fstatfs(fd, &fsInfo) == -1 ){
29661 ((unixFile*)pFile)->lastErrno = errno;
29662 robust_close(p, fd, __LINE__);
29663 return SQLITE_IOERR_ACCESS;
29664 }
@@ -29641,11 +29678,10 @@
29678 /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29679 ** never use proxy, NULL means use proxy for non-local files only. */
29680 if( envforce!=NULL ){
29681 useProxy = atoi(envforce)>0;
29682 }else{
 
29683 if( statfs(zPath, &fsInfo) == -1 ){
29684 /* In theory, the close(fd) call is sub-optimal. If the file opened
29685 ** with fd is a database file, and there are other connections open
29686 ** on that file that are currently holding advisory locks on it,
29687 ** then the call to close() will cancel those locks. In practice,
@@ -29715,10 +29751,12 @@
29751 #endif
29752 {
29753 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29754 }
29755 robust_close(0, fd, __LINE__);
29756 }else if( rc==SQLITE_CANTOPEN ){
29757 rc = SQLITE_OK;
29758 }
29759 }
29760 #endif
29761 return rc;
29762 }
@@ -30380,10 +30418,12 @@
30418 *pError = err;
30419 }
30420 return SQLITE_IOERR;
30421 }
30422 }
30423 #else
30424 UNUSED_PARAMETER(pError);
30425 #endif
30426 #ifdef SQLITE_TEST
30427 /* simulate multiple hosts by creating unique hostid file paths */
30428 if( sqlite3_hostid_num != 0){
30429 pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
@@ -30472,10 +30512,11 @@
30512 unixFile *conchFile = pCtx->conchFile;
30513 int rc = SQLITE_OK;
30514 int nTries = 0;
30515 struct timespec conchModTime;
30516
30517 memset(&conchModTime, 0, sizeof(conchModTime));
30518 do {
30519 rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30520 nTries ++;
30521 if( rc==SQLITE_BUSY ){
30522 /* If the lock failed (busy):
@@ -30703,15 +30744,16 @@
30744 conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30745
30746 end_takeconch:
30747 OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h));
30748 if( rc==SQLITE_OK && pFile->openFlags ){
30749 int fd;
30750 if( pFile->h>=0 ){
30751 robust_close(pFile, pFile->h, __LINE__);
30752 }
30753 pFile->h = -1;
30754 fd = robust_open(pCtx->dbPath, pFile->openFlags,
30755 SQLITE_DEFAULT_FILE_PERMISSIONS);
30756 OSTRACE(("TRANSPROXY: OPEN %d\n", fd));
30757 if( fd>=0 ){
30758 pFile->h = fd;
30759 }else{
@@ -31393,11 +31435,11 @@
31435 */
31436 #ifdef MEMORY_DEBUG
31437 # error "The MEMORY_DEBUG macro is obsolete. Use SQLITE_DEBUG instead."
31438 #endif
31439
31440 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31441 # ifndef SQLITE_DEBUG_OS_TRACE
31442 # define SQLITE_DEBUG_OS_TRACE 0
31443 # endif
31444 int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31445 # define OSTRACE(X) if( sqlite3OSTrace ) sqlite3DebugPrintf X
@@ -31629,10 +31671,80 @@
31671 winceLock local; /* Locks obtained by this instance of winFile */
31672 winceLock *shared; /* Global shared lock memory for the file */
31673 #endif
31674 };
31675
31676 /*
31677 * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31678 * various Win32 API heap functions instead of our own.
31679 */
31680 #ifdef SQLITE_WIN32_MALLOC
31681 /*
31682 * The initial size of the Win32-specific heap. This value may be zero.
31683 */
31684 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
31685 # define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
31686 (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
31687 #endif
31688
31689 /*
31690 * The maximum size of the Win32-specific heap. This value may be zero.
31691 */
31692 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
31693 # define SQLITE_WIN32_HEAP_MAX_SIZE (0)
31694 #endif
31695
31696 /*
31697 * The extra flags to use in calls to the Win32 heap APIs. This value may be
31698 * zero for the default behavior.
31699 */
31700 #ifndef SQLITE_WIN32_HEAP_FLAGS
31701 # define SQLITE_WIN32_HEAP_FLAGS (0)
31702 #endif
31703
31704 /*
31705 ** The winMemData structure stores information required by the Win32-specific
31706 ** sqlite3_mem_methods implementation.
31707 */
31708 typedef struct winMemData winMemData;
31709 struct winMemData {
31710 #ifndef NDEBUG
31711 u32 magic; /* Magic number to detect structure corruption. */
31712 #endif
31713 HANDLE hHeap; /* The handle to our heap. */
31714 BOOL bOwned; /* Do we own the heap (i.e. destroy it on shutdown)? */
31715 };
31716
31717 #ifndef NDEBUG
31718 #define WINMEM_MAGIC 0x42b2830b
31719 #endif
31720
31721 static struct winMemData win_mem_data = {
31722 #ifndef NDEBUG
31723 WINMEM_MAGIC,
31724 #endif
31725 NULL, FALSE
31726 };
31727
31728 #ifndef NDEBUG
31729 #define winMemAssertMagic() assert( win_mem_data.magic==WINMEM_MAGIC )
31730 #else
31731 #define winMemAssertMagic()
31732 #endif
31733
31734 #define winMemGetHeap() win_mem_data.hHeap
31735
31736 static void *winMemMalloc(int nBytes);
31737 static void winMemFree(void *pPrior);
31738 static void *winMemRealloc(void *pPrior, int nBytes);
31739 static int winMemSize(void *p);
31740 static int winMemRoundup(int n);
31741 static int winMemInit(void *pAppData);
31742 static void winMemShutdown(void *pAppData);
31743
31744 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31745 #endif /* SQLITE_WIN32_MALLOC */
31746
31747 /*
31748 ** Forward prototypes.
31749 */
31750 static int getSectorSize(
@@ -31681,10 +31793,192 @@
31793 }
31794 return sqlite3_os_type==2;
31795 }
31796 #endif /* SQLITE_OS_WINCE */
31797
31798 #ifdef SQLITE_WIN32_MALLOC
31799 /*
31800 ** Allocate nBytes of memory.
31801 */
31802 static void *winMemMalloc(int nBytes){
31803 HANDLE hHeap;
31804 void *p;
31805
31806 winMemAssertMagic();
31807 hHeap = winMemGetHeap();
31808 assert( hHeap!=0 );
31809 assert( hHeap!=INVALID_HANDLE_VALUE );
31810 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31811 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31812 #endif
31813 assert( nBytes>=0 );
31814 p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31815 if( !p ){
31816 sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%d), heap=%p",
31817 nBytes, GetLastError(), (void*)hHeap);
31818 }
31819 return p;
31820 }
31821
31822 /*
31823 ** Free memory.
31824 */
31825 static void winMemFree(void *pPrior){
31826 HANDLE hHeap;
31827
31828 winMemAssertMagic();
31829 hHeap = winMemGetHeap();
31830 assert( hHeap!=0 );
31831 assert( hHeap!=INVALID_HANDLE_VALUE );
31832 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31833 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31834 #endif
31835 if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
31836 if( !HeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
31837 sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%d), heap=%p",
31838 pPrior, GetLastError(), (void*)hHeap);
31839 }
31840 }
31841
31842 /*
31843 ** Change the size of an existing memory allocation
31844 */
31845 static void *winMemRealloc(void *pPrior, int nBytes){
31846 HANDLE hHeap;
31847 void *p;
31848
31849 winMemAssertMagic();
31850 hHeap = winMemGetHeap();
31851 assert( hHeap!=0 );
31852 assert( hHeap!=INVALID_HANDLE_VALUE );
31853 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31854 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
31855 #endif
31856 assert( nBytes>=0 );
31857 if( !pPrior ){
31858 p = HeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
31859 }else{
31860 p = HeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
31861 }
31862 if( !p ){
31863 sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%d), heap=%p",
31864 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, GetLastError(),
31865 (void*)hHeap);
31866 }
31867 return p;
31868 }
31869
31870 /*
31871 ** Return the size of an outstanding allocation, in bytes.
31872 */
31873 static int winMemSize(void *p){
31874 HANDLE hHeap;
31875 SIZE_T n;
31876
31877 winMemAssertMagic();
31878 hHeap = winMemGetHeap();
31879 assert( hHeap!=0 );
31880 assert( hHeap!=INVALID_HANDLE_VALUE );
31881 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31882 assert ( HeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31883 #endif
31884 if( !p ) return 0;
31885 n = HeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
31886 if( n==(SIZE_T)-1 ){
31887 sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%d), heap=%p",
31888 p, GetLastError(), (void*)hHeap);
31889 return 0;
31890 }
31891 return (int)n;
31892 }
31893
31894 /*
31895 ** Round up a request size to the next valid allocation size.
31896 */
31897 static int winMemRoundup(int n){
31898 return n;
31899 }
31900
31901 /*
31902 ** Initialize this module.
31903 */
31904 static int winMemInit(void *pAppData){
31905 winMemData *pWinMemData = (winMemData *)pAppData;
31906
31907 if( !pWinMemData ) return SQLITE_ERROR;
31908 assert( pWinMemData->magic==WINMEM_MAGIC );
31909 if( !pWinMemData->hHeap ){
31910 pWinMemData->hHeap = HeapCreate(SQLITE_WIN32_HEAP_FLAGS,
31911 SQLITE_WIN32_HEAP_INIT_SIZE,
31912 SQLITE_WIN32_HEAP_MAX_SIZE);
31913 if( !pWinMemData->hHeap ){
31914 sqlite3_log(SQLITE_NOMEM,
31915 "failed to HeapCreate (%d), flags=%u, initSize=%u, maxSize=%u",
31916 GetLastError(), SQLITE_WIN32_HEAP_FLAGS, SQLITE_WIN32_HEAP_INIT_SIZE,
31917 SQLITE_WIN32_HEAP_MAX_SIZE);
31918 return SQLITE_NOMEM;
31919 }
31920 pWinMemData->bOwned = TRUE;
31921 }
31922 assert( pWinMemData->hHeap!=0 );
31923 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31924 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31925 assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31926 #endif
31927 return SQLITE_OK;
31928 }
31929
31930 /*
31931 ** Deinitialize this module.
31932 */
31933 static void winMemShutdown(void *pAppData){
31934 winMemData *pWinMemData = (winMemData *)pAppData;
31935
31936 if( !pWinMemData ) return;
31937 if( pWinMemData->hHeap ){
31938 assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
31939 #ifdef SQLITE_WIN32_MALLOC_VALIDATE
31940 assert( HeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
31941 #endif
31942 if( pWinMemData->bOwned ){
31943 if( !HeapDestroy(pWinMemData->hHeap) ){
31944 sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%d), heap=%p",
31945 GetLastError(), (void*)pWinMemData->hHeap);
31946 }
31947 pWinMemData->bOwned = FALSE;
31948 }
31949 pWinMemData->hHeap = NULL;
31950 }
31951 }
31952
31953 /*
31954 ** Populate the low-level memory allocation function pointers in
31955 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
31956 ** arguments specify the block of memory to manage.
31957 **
31958 ** This routine is only called by sqlite3_config(), and therefore
31959 ** is not required to be threadsafe (it is not).
31960 */
31961 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
31962 static const sqlite3_mem_methods winMemMethods = {
31963 winMemMalloc,
31964 winMemFree,
31965 winMemRealloc,
31966 winMemSize,
31967 winMemRoundup,
31968 winMemInit,
31969 winMemShutdown,
31970 &win_mem_data
31971 };
31972 return &winMemMethods;
31973 }
31974
31975 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
31976 sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
31977 }
31978 #endif /* SQLITE_WIN32_MALLOC */
31979
31980 /*
31981 ** Convert a UTF-8 string to microsoft unicode (UTF-16?).
31982 **
31983 ** Space to hold the returned string is obtained from malloc.
31984 */
@@ -31969,10 +32263,11 @@
32263 */
32264 /*
32265 ** WindowsCE does not have a localtime() function. So create a
32266 ** substitute.
32267 */
32268 /* #include <time.h> */
32269 struct tm *__cdecl localtime(const time_t *t)
32270 {
32271 static struct tm y;
32272 FILETIME uTm, lTm;
32273 SYSTEMTIME pTm;
@@ -32473,11 +32768,11 @@
32768 /* If the user has configured a chunk-size for this file, truncate the
32769 ** file so that it consists of an integer number of chunks (i.e. the
32770 ** actual file size after the operation may be larger than the requested
32771 ** size).
32772 */
32773 if( pFile->szChunk>0 ){
32774 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
32775 }
32776
32777 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32778 if( seekWinFile(pFile, nByte) ){
@@ -32860,14 +33155,23 @@
33155 case SQLITE_FCNTL_CHUNK_SIZE: {
33156 pFile->szChunk = *(int *)pArg;
33157 return SQLITE_OK;
33158 }
33159 case SQLITE_FCNTL_SIZE_HINT: {
33160 if( pFile->szChunk>0 ){
33161 sqlite3_int64 oldSz;
33162 int rc = winFileSize(id, &oldSz);
33163 if( rc==SQLITE_OK ){
33164 sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
33165 if( newSz>oldSz ){
33166 SimulateIOErrorBenign(1);
33167 rc = winTruncate(id, newSz);
33168 SimulateIOErrorBenign(0);
33169 }
33170 }
33171 return rc;
33172 }
33173 return SQLITE_OK;
33174 }
33175 case SQLITE_FCNTL_PERSIST_WAL: {
33176 int bPersist = *(int*)pArg;
33177 if( bPersist<0 ){
@@ -35473,10 +35777,13 @@
35777 typedef struct PCache1 PCache1;
35778 typedef struct PgHdr1 PgHdr1;
35779 typedef struct PgFreeslot PgFreeslot;
35780 typedef struct PGroup PGroup;
35781
35782 typedef struct PGroupBlock PGroupBlock;
35783 typedef struct PGroupBlockList PGroupBlockList;
35784
35785 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35786 ** of one or more PCaches that are able to recycle each others unpinned
35787 ** pages when they are under memory pressure. A PGroup is an instance of
35788 ** the following object.
35789 **
@@ -35502,12 +35809,70 @@
35809 int nMaxPage; /* Sum of nMax for purgeable caches */
35810 int nMinPage; /* Sum of nMin for purgeable caches */
35811 int mxPinned; /* nMaxpage + 10 - nMinPage */
35812 int nCurrentPage; /* Number of purgeable pages allocated */
35813 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35814 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
35815 int isBusy; /* Do not run ReleaseMemory() if true */
35816 PGroupBlockList *pBlockList; /* List of block-lists for this group */
35817 #endif
35818 };
35819
35820 /*
35821 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35822 ** each PGroup structure has a linked list of the the following starting
35823 ** at PGroup.pBlockList. There is one entry for each distinct page-size
35824 ** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35825 ** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35826 ** size requested by each pcache, which is the database page-size plus
35827 ** the various header structures used by the pcache, pager and btree layers.
35828 ** Usually around (pgsz+200) bytes.
35829 **
35830 ** This size (pgsz+200) bytes is not allocated efficiently by some
35831 ** implementations of malloc. In particular, some implementations are only
35832 ** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35833 ** integer value. Since the page-size is a power of 2, this means we
35834 ** end up wasting (pgsz-200) bytes in each allocation.
35835 **
35836 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35837 ** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35838 ** are requested from malloc allocator. After a block is returned,
35839 ** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35840 ** allocations can fit in the space returned by malloc(). This value may
35841 ** be more than M.
35842 **
35843 ** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35844 ** contains the number of allocations that will fit in the aData[] space.
35845 ** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35846 ** within aData is in use, the corresponding bit in mUsed is set. Thus
35847 ** when (mUsed+1==(1 << nEntry)) the block is completely full.
35848 **
35849 ** Each time a slot within a block is freed, the block is moved to the start
35850 ** of the linked-list. And if a block becomes completely full, then it is
35851 ** moved to the end of the list. As a result, when searching for a free
35852 ** slot, only the first block in the list need be examined. If it is full,
35853 ** then it is guaranteed that all blocks are full.
35854 */
35855 struct PGroupBlockList {
35856 int nByte; /* Size of each allocation in bytes */
35857 PGroupBlock *pFirst; /* First PGroupBlock in list */
35858 PGroupBlock *pLast; /* Last PGroupBlock in list */
35859 PGroupBlockList *pNext; /* Next block-list attached to group */
35860 };
35861
35862 struct PGroupBlock {
35863 Bitmask mUsed; /* Mask of used slots */
35864 int nEntry; /* Maximum number of allocations in aData[] */
35865 u8 *aData; /* Pointer to data block */
35866 PGroupBlock *pNext; /* Next PGroupBlock in list */
35867 PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35868 PGroupBlockList *pList; /* Owner list */
35869 };
35870
35871 /* Minimum value for PGroupBlock.nEntry */
35872 #define PAGECACHE_BLOCKALLOC_MINENTRY 15
35873
35874 /* Each page cache is an instance of the following object. Every
35875 ** open database file (including each in-memory database and each
35876 ** temporary or transient database) has a single page cache which
35877 ** is an instance of this object.
35878 **
@@ -35606,10 +35971,21 @@
35971 **
35972 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35973 */
35974 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35975 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35976
35977 /*
35978 ** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35979 ** a PGroupBlock pointer based on a pointer to a page buffer.
35980 */
35981 #define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35982 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35983
35984 #define PAGE_GET_BLOCKPTR(pCache, pPg) \
35985 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35986
35987
35988 /*
35989 ** Macros to enter and leave the PCache LRU mutex.
35990 */
35991 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -35732,25 +36108,159 @@
36108 return iSize;
36109 }
36110 }
36111 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36112
36113 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36114 /*
36115 ** The block pBlock belongs to list pList but is not currently linked in.
36116 ** Insert it into the start of the list.
36117 */
36118 static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36119 pBlock->pPrev = 0;
36120 pBlock->pNext = pList->pFirst;
36121 pList->pFirst = pBlock;
36122 if( pBlock->pNext ){
36123 pBlock->pNext->pPrev = pBlock;
36124 }else{
36125 assert( pList->pLast==0 );
36126 pList->pLast = pBlock;
36127 }
36128 }
36129
36130 /*
36131 ** If there are no blocks in the list headed by pList, remove pList
36132 ** from the pGroup->pBlockList list and free it with sqlite3_free().
36133 */
36134 static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36135 assert( sqlite3_mutex_held(pGroup->mutex) );
36136 if( pList->pFirst==0 ){
36137 PGroupBlockList **pp;
36138 for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36139 *pp = (*pp)->pNext;
36140 sqlite3_free(pList);
36141 }
36142 }
36143 #endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36144
36145 /*
36146 ** Allocate a new page object initially associated with cache pCache.
36147 */
36148 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36149 int nByte = sizeof(PgHdr1) + pCache->szPage;
36150 void *pPg = 0;
36151 PgHdr1 *p;
36152
36153 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36154 PGroup *pGroup = pCache->pGroup;
36155 PGroupBlockList *pList;
36156 PGroupBlock *pBlock;
36157 int i;
36158
36159 nByte += sizeof(PGroupBlockList *);
36160 nByte = ROUND8(nByte);
36161
36162 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36163 if( pList->nByte==nByte ) break;
36164 }
36165 if( pList==0 ){
36166 PGroupBlockList *pNew;
36167 assert( pGroup->isBusy==0 );
36168 assert( sqlite3_mutex_held(pGroup->mutex) );
36169 pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36170 pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36171 pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36172 if( pNew==0 ){
36173 /* malloc() failure. Return early. */
36174 return 0;
36175 }
36176 #ifdef SQLITE_DEBUG
36177 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36178 assert( pList->nByte!=nByte );
36179 }
36180 #endif
36181 pNew->nByte = nByte;
36182 pNew->pNext = pGroup->pBlockList;
36183 pGroup->pBlockList = pNew;
36184 pList = pNew;
36185 }
36186
36187 pBlock = pList->pFirst;
36188 if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36189 int sz;
36190
36191 /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36192 ** structure and MINENTRY allocations of nByte bytes each. If the
36193 ** allocator returns more memory than requested, then more than MINENTRY
36194 ** allocations may fit in it. */
36195 assert( sqlite3_mutex_held(pGroup->mutex) );
36196 pcache1LeaveMutex(pCache->pGroup);
36197 sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36198 pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36199 pcache1EnterMutex(pCache->pGroup);
36200
36201 if( !pBlock ){
36202 freeListIfEmpty(pGroup, pList);
36203 return 0;
36204 }
36205 pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36206 if( pBlock->nEntry>=BMS ){
36207 pBlock->nEntry = BMS-1;
36208 }
36209 pBlock->pList = pList;
36210 pBlock->mUsed = 0;
36211 pBlock->aData = (u8 *)&pBlock[1];
36212 addBlockToList(pList, pBlock);
36213
36214 sz = sqlite3MallocSize(pBlock);
36215 sqlite3_mutex_enter(pcache1.mutex);
36216 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36217 sqlite3_mutex_leave(pcache1.mutex);
36218 }
36219
36220 for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36221 if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36222 pBlock->mUsed |= ((Bitmask)1<<i);
36223 pPg = (void *)&pBlock->aData[pList->nByte * i];
36224 }
36225 }
36226 assert( pPg );
36227 PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36228
36229 /* If the block is now full, shift it to the end of the list */
36230 if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36231 assert( pList->pFirst==pBlock );
36232 assert( pBlock->pPrev==0 );
36233 assert( pList->pLast->pNext==0 );
36234 pList->pFirst = pBlock->pNext;
36235 pList->pFirst->pPrev = 0;
36236 pBlock->pPrev = pList->pLast;
36237 pBlock->pNext = 0;
36238 pList->pLast->pNext = pBlock;
36239 pList->pLast = pBlock;
36240 }
36241 p = PAGE_TO_PGHDR1(pCache, pPg);
36242 if( pCache->bPurgeable ){
36243 pCache->pGroup->nCurrentPage++;
36244 }
36245 #else
36246 /* The group mutex must be released before pcache1Alloc() is called. This
36247 ** is because it may call sqlite3_release_memory(), which assumes that
36248 ** this mutex is not held. */
36249 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36250 pcache1LeaveMutex(pCache->pGroup);
36251 pPg = pcache1Alloc(nByte);
36252 pcache1EnterMutex(pCache->pGroup);
36253 if( pPg ){
36254 p = PAGE_TO_PGHDR1(pCache, pPg);
36255 if( pCache->bPurgeable ){
36256 pCache->pGroup->nCurrentPage++;
36257 }
36258 }else{
36259 p = 0;
36260 }
36261 #endif
36262 return p;
36263 }
36264
36265 /*
36266 ** Free a page object allocated by pcache1AllocPage().
@@ -35760,14 +36270,56 @@
36270 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
36271 */
36272 static void pcache1FreePage(PgHdr1 *p){
36273 if( ALWAYS(p) ){
36274 PCache1 *pCache = p->pCache;
36275 void *pPg = PGHDR1_TO_PAGE(p);
36276
36277 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36278 PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36279 PGroupBlockList *pList = pBlock->pList;
36280 int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36281
36282 assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36283 assert( pBlock->mUsed & ((Bitmask)1<<i) );
36284 pBlock->mUsed &= ~((Bitmask)1<<i);
36285
36286 /* Remove the block from the list. If it is completely empty, free it.
36287 ** Or if it is not completely empty, re-insert it at the start of the
36288 ** list. */
36289 if( pList->pFirst==pBlock ){
36290 pList->pFirst = pBlock->pNext;
36291 if( pList->pFirst ) pList->pFirst->pPrev = 0;
36292 }else{
36293 pBlock->pPrev->pNext = pBlock->pNext;
36294 }
36295 if( pList->pLast==pBlock ){
36296 pList->pLast = pBlock->pPrev;
36297 if( pList->pLast ) pList->pLast->pNext = 0;
36298 }else{
36299 pBlock->pNext->pPrev = pBlock->pPrev;
36300 }
36301
36302 if( pBlock->mUsed==0 ){
36303 PGroup *pGroup = p->pCache->pGroup;
36304
36305 int sz = sqlite3MallocSize(pBlock);
36306 sqlite3_mutex_enter(pcache1.mutex);
36307 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36308 sqlite3_mutex_leave(pcache1.mutex);
36309 freeListIfEmpty(pGroup, pList);
36310 sqlite3_free(pBlock);
36311 }else{
36312 addBlockToList(pList, pBlock);
36313 }
36314 #else
36315 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36316 pcache1Free(pPg);
36317 #endif
36318 if( pCache->bPurgeable ){
36319 pCache->pGroup->nCurrentPage--;
36320 }
 
36321 }
36322 }
36323
36324 /*
36325 ** Malloc function used by SQLite to obtain space from the buffer configured
@@ -36201,13 +36753,11 @@
36753 /* Step 5. If a usable page buffer has still not been found,
36754 ** attempt to allocate a new one.
36755 */
36756 if( !pPage ){
36757 if( createFlag==1 ) sqlite3BeginBenignMalloc();
 
36758 pPage = pcache1AllocPage(pCache);
 
36759 if( createFlag==1 ) sqlite3EndBenignMalloc();
36760 }
36761
36762 if( pPage ){
36763 unsigned int h = iKey % pCache->nHash;
@@ -36373,10 +36923,13 @@
36923 ** been released, the function returns. The return value is the total number
36924 ** of bytes of memory released.
36925 */
36926 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36927 int nFree = 0;
36928 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36929 if( pcache1.grp.isBusy ) return 0;
36930 #endif
36931 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36932 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36933 if( pcache1.pStart==0 ){
36934 PgHdr1 *p;
36935 pcache1EnterMutex(&pcache1.grp);
@@ -37585,10 +38138,11 @@
38138 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
38139 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
38140 u8 tempFile; /* zFilename is a temporary file */
38141 u8 readOnly; /* True for a read-only database */
38142 u8 memDb; /* True to inhibit all file I/O */
38143 u8 hasSeenStress; /* pagerStress() called one or more times */
38144
38145 /**************************************************************************
38146 ** The following block contains those class members that change during
38147 ** routine opertion. Class members not in this block are either fixed
38148 ** when the pager is first created or else only change when there is a
@@ -40704,10 +41258,11 @@
41258 ** to the caller.
41259 */
41260 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
41261 u8 *pTmp = (u8 *)pPager->pTmpSpace;
41262
41263 assert( assert_pager_state(pPager) );
41264 disable_simulated_io_errors();
41265 sqlite3BeginBenignMalloc();
41266 /* pPager->errCode = 0; */
41267 pPager->exclusiveMode = 0;
41268 #ifndef SQLITE_OMIT_WAL
@@ -41138,10 +41693,11 @@
41693 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41694 ** while in the error state, hence it is impossible for this routine to
41695 ** be called in the error state. Nevertheless, we include a NEVER()
41696 ** test for the error state as a safeguard against future changes.
41697 */
41698 pPager->hasSeenStress = 1;
41699 if( NEVER(pPager->errCode) ) return SQLITE_OK;
41700 if( pPager->doNotSpill ) return SQLITE_OK;
41701 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
41702 return SQLITE_OK;
41703 }
@@ -50430,15 +50986,16 @@
50986 for(i=0; i<nCell; i++){
50987 u8 *pCell = findCell(pPage, i);
50988 if( eType==PTRMAP_OVERFLOW1 ){
50989 CellInfo info;
50990 btreeParseCellPtr(pPage, pCell, &info);
50991 if( info.iOverflow
50992 && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
50993 && iFrom==get4byte(&pCell[info.iOverflow])
50994 ){
50995 put4byte(&pCell[info.iOverflow], iTo);
50996 break;
50997 }
50998 }else{
50999 if( get4byte(pCell)==iFrom ){
51000 put4byte(pCell, iTo);
51001 break;
@@ -51155,11 +51712,12 @@
51712
51713 if( NEVER(wrFlag && pBt->readOnly) ){
51714 return SQLITE_READONLY;
51715 }
51716 if( iTable==1 && btreePagecount(pBt)==0 ){
51717 assert( wrFlag==0 );
51718 iTable = 0;
51719 }
51720
51721 /* Now that no other errors can occur, finish filling in the BtCursor
51722 ** variables and link the cursor into the BtShared list. */
51723 pCur->pgnoRoot = (Pgno)iTable;
@@ -51909,10 +52467,13 @@
52467 int i;
52468 for(i=1; i<=pCur->iPage; i++){
52469 releasePage(pCur->apPage[i]);
52470 }
52471 pCur->iPage = 0;
52472 }else if( pCur->pgnoRoot==0 ){
52473 pCur->eState = CURSOR_INVALID;
52474 return SQLITE_OK;
52475 }else{
52476 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
52477 if( rc!=SQLITE_OK ){
52478 pCur->eState = CURSOR_INVALID;
52479 return rc;
@@ -52018,11 +52579,11 @@
52579 assert( cursorHoldsMutex(pCur) );
52580 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
52581 rc = moveToRoot(pCur);
52582 if( rc==SQLITE_OK ){
52583 if( pCur->eState==CURSOR_INVALID ){
52584 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52585 *pRes = 1;
52586 }else{
52587 assert( pCur->apPage[pCur->iPage]->nCell>0 );
52588 *pRes = 0;
52589 rc = moveToLeftmost(pCur);
@@ -52057,11 +52618,11 @@
52618 }
52619
52620 rc = moveToRoot(pCur);
52621 if( rc==SQLITE_OK ){
52622 if( CURSOR_INVALID==pCur->eState ){
52623 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52624 *pRes = 1;
52625 }else{
52626 assert( pCur->eState==CURSOR_VALID );
52627 *pRes = 0;
52628 rc = moveToRightmost(pCur);
@@ -52130,16 +52691,16 @@
52691
52692 rc = moveToRoot(pCur);
52693 if( rc ){
52694 return rc;
52695 }
52696 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
52697 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
52698 assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
52699 if( pCur->eState==CURSOR_INVALID ){
52700 *pRes = -1;
52701 assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
52702 return SQLITE_OK;
52703 }
52704 assert( pCur->apPage[0]->intKey || pIdxKey );
52705 for(;;){
52706 int lwr, upr, idx;
@@ -52862,10 +53423,13 @@
53423 assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53424 btreeParseCellPtr(pPage, pCell, &info);
53425 if( info.iOverflow==0 ){
53426 return SQLITE_OK; /* No overflow pages. Return without doing anything */
53427 }
53428 if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
53429 return SQLITE_CORRUPT; /* Cell extends past end of page */
53430 }
53431 ovflPgno = get4byte(&pCell[info.iOverflow]);
53432 assert( pBt->usableSize > 4 );
53433 ovflPageSize = pBt->usableSize - 4;
53434 nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
53435 assert( ovflPgno==0 || nOvfl>0 );
@@ -55045,10 +55609,15 @@
55609 ** corruption) an SQLite error code is returned.
55610 */
55611 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
55612 i64 nEntry = 0; /* Value to return in *pnEntry */
55613 int rc; /* Return code */
55614
55615 if( pCur->pgnoRoot==0 ){
55616 *pnEntry = 0;
55617 return SQLITE_OK;
55618 }
55619 rc = moveToRoot(pCur);
55620
55621 /* Unless an error occurs, the following loop runs one iteration for each
55622 ** page in the B-Tree structure (not including overflow pages).
55623 */
@@ -55829,11 +56398,10 @@
56398 */
56399 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
56400 BtShared *pBt = pBtree->pBt;
56401 int rc; /* Return code */
56402
 
56403 assert( iVersion==1 || iVersion==2 );
56404
56405 /* If setting the version fields to 1, do not automatically open the
56406 ** WAL connection, even if the version fields are currently set to 2.
56407 */
@@ -56268,106 +56836,110 @@
56836 /* Update the schema version field in the destination database. This
56837 ** is to make sure that the schema-version really does change in
56838 ** the case where the source and destination databases have the
56839 ** same schema version.
56840 */
56841 if( rc==SQLITE_DONE ){
56842 rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
56843 if( rc==SQLITE_OK ){
56844 if( p->pDestDb ){
56845 sqlite3ResetInternalSchema(p->pDestDb, -1);
56846 }
56847 if( destMode==PAGER_JOURNALMODE_WAL ){
56848 rc = sqlite3BtreeSetVersion(p->pDest, 2);
56849 }
56850 }
56851 if( rc==SQLITE_OK ){
56852 int nDestTruncate;
56853 /* Set nDestTruncate to the final number of pages in the destination
56854 ** database. The complication here is that the destination page
56855 ** size may be different to the source page size.
56856 **
56857 ** If the source page size is smaller than the destination page size,
56858 ** round up. In this case the call to sqlite3OsTruncate() below will
56859 ** fix the size of the file. However it is important to call
56860 ** sqlite3PagerTruncateImage() here so that any pages in the
56861 ** destination file that lie beyond the nDestTruncate page mark are
56862 ** journalled by PagerCommitPhaseOne() before they are destroyed
56863 ** by the file truncation.
56864 */
56865 assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
56866 assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
56867 if( pgszSrc<pgszDest ){
56868 int ratio = pgszDest/pgszSrc;
56869 nDestTruncate = (nSrcPage+ratio-1)/ratio;
56870 if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
56871 nDestTruncate--;
56872 }
56873 }else{
56874 nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
56875 }
56876 sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
56877
56878 if( pgszSrc<pgszDest ){
56879 /* If the source page-size is smaller than the destination page-size,
56880 ** two extra things may need to happen:
56881 **
56882 ** * The destination may need to be truncated, and
56883 **
56884 ** * Data stored on the pages immediately following the
56885 ** pending-byte page in the source database may need to be
56886 ** copied into the destination database.
56887 */
56888 const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
56889 sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
56890 i64 iOff;
56891 i64 iEnd;
56892
56893 assert( pFile );
56894 assert( (i64)nDestTruncate*(i64)pgszDest >= iSize || (
56895 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
56896 && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
56897 ));
56898
56899 /* This call ensures that all data required to recreate the original
56900 ** database has been stored in the journal for pDestPager and the
56901 ** journal synced to disk. So at this point we may safely modify
56902 ** the database file in any way, knowing that if a power failure
56903 ** occurs, the original database will be reconstructed from the
56904 ** journal file. */
56905 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
56906
56907 /* Write the extra pages and truncate the database file as required */
56908 iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
56909 for(
56910 iOff=PENDING_BYTE+pgszSrc;
56911 rc==SQLITE_OK && iOff<iEnd;
56912 iOff+=pgszSrc
56913 ){
56914 PgHdr *pSrcPg = 0;
56915 const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
56916 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
56917 if( rc==SQLITE_OK ){
56918 u8 *zData = sqlite3PagerGetData(pSrcPg);
56919 rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
56920 }
56921 sqlite3PagerUnref(pSrcPg);
56922 }
56923 if( rc==SQLITE_OK ){
56924 rc = backupTruncateFile(pFile, iSize);
56925 }
56926
56927 /* Sync the database file to disk. */
56928 if( rc==SQLITE_OK ){
56929 rc = sqlite3PagerSync(pDestPager);
56930 }
56931 }else{
56932 rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
56933 }
56934
56935 /* Finish committing the transaction to the destination database. */
56936 if( SQLITE_OK==rc
56937 && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
56938 ){
56939 rc = SQLITE_DONE;
56940 }
56941 }
56942 }
56943
56944 /* If bCloseTrans is true, then this function opened a read transaction
56945 ** on the source database. Close the read transaction here. There is
@@ -56831,38 +57403,32 @@
57403 ** invoking an external callback, free it now. Calling this function
57404 ** does not free any Mem.zMalloc buffer.
57405 */
57406 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
57407 assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
57408 if( p->flags&MEM_Agg ){
57409 sqlite3VdbeMemFinalize(p, p->u.pDef);
57410 assert( (p->flags & MEM_Agg)==0 );
57411 sqlite3VdbeMemRelease(p);
57412 }else if( p->flags&MEM_Dyn && p->xDel ){
57413 assert( (p->flags&MEM_RowSet)==0 );
57414 p->xDel((void *)p->z);
57415 p->xDel = 0;
57416 }else if( p->flags&MEM_RowSet ){
57417 sqlite3RowSetClear(p->u.pRowSet);
57418 }else if( p->flags&MEM_Frame ){
57419 sqlite3VdbeMemSetNull(p);
 
 
 
 
 
 
57420 }
57421 }
57422
57423 /*
57424 ** Release any memory held by the Mem. This may leave the Mem in an
57425 ** inconsistent state, for example with (Mem.z==0) and
57426 ** (Mem.type==SQLITE_TEXT).
57427 */
57428 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
57429 MemReleaseExt(p);
57430 sqlite3DbFree(p->db, p->zMalloc);
57431 p->z = 0;
57432 p->zMalloc = 0;
57433 p->xDel = 0;
57434 }
@@ -57180,11 +57746,11 @@
57746 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
57747 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
57748 */
57749 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
57750 assert( (pFrom->flags & MEM_RowSet)==0 );
57751 MemReleaseExt(pTo);
57752 memcpy(pTo, pFrom, MEMCELLSIZE);
57753 pTo->xDel = 0;
57754 if( (pFrom->flags&MEM_Static)==0 ){
57755 pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
57756 assert( srcType==MEM_Ephem || srcType==MEM_Static );
@@ -57198,11 +57764,11 @@
57764 */
57765 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
57766 int rc = SQLITE_OK;
57767
57768 assert( (pFrom->flags & MEM_RowSet)==0 );
57769 MemReleaseExt(pTo);
57770 memcpy(pTo, pFrom, MEMCELLSIZE);
57771 pTo->flags &= ~MEM_Dyn;
57772
57773 if( pTo->flags&(MEM_Str|MEM_Blob) ){
57774 if( 0==(pFrom->flags&MEM_Static) ){
@@ -57592,15 +58158,15 @@
58158 *ppVal = 0;
58159 return SQLITE_OK;
58160 }
58161 op = pExpr->op;
58162
58163 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
58164 ** The ifdef here is to enable us to achieve 100% branch test coverage even
58165 ** when SQLITE_ENABLE_STAT2 is omitted.
58166 */
58167 #ifdef SQLITE_ENABLE_STAT2
58168 if( op==TK_REGISTER ) op = pExpr->op2;
58169 #else
58170 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
58171 #endif
58172
@@ -58153,10 +58719,16 @@
58719 assert( p->nOp - i >= 3 );
58720 assert( pOp[-1].opcode==OP_Integer );
58721 n = pOp[-1].p1;
58722 if( n>nMaxArgs ) nMaxArgs = n;
58723 #endif
58724 }else if( opcode==OP_Next || opcode==OP_SorterNext ){
58725 pOp->p4.xAdvance = sqlite3BtreeNext;
58726 pOp->p4type = P4_ADVANCE;
58727 }else if( opcode==OP_Prev ){
58728 pOp->p4.xAdvance = sqlite3BtreePrevious;
58729 pOp->p4type = P4_ADVANCE;
58730 }
58731
58732 if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
58733 assert( -1-pOp->p2<p->nLabel );
58734 pOp->p2 = aLabel[-1-pOp->p2];
@@ -58244,37 +58816,34 @@
58816 ** Change the value of the P1 operand for a specific instruction.
58817 ** This routine is useful when a large program is loaded from a
58818 ** static array using sqlite3VdbeAddOpList but we want to make a
58819 ** few minor changes to the program.
58820 */
58821 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
58822 assert( p!=0 );
58823 if( ((u32)p->nOp)>addr ){
 
58824 p->aOp[addr].p1 = val;
58825 }
58826 }
58827
58828 /*
58829 ** Change the value of the P2 operand for a specific instruction.
58830 ** This routine is useful for setting a jump destination.
58831 */
58832 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
58833 assert( p!=0 );
58834 if( ((u32)p->nOp)>addr ){
 
58835 p->aOp[addr].p2 = val;
58836 }
58837 }
58838
58839 /*
58840 ** Change the value of the P3 operand for a specific instruction.
58841 */
58842 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
58843 assert( p!=0 );
58844 if( ((u32)p->nOp)>addr ){
 
58845 p->aOp[addr].p3 = val;
58846 }
58847 }
58848
58849 /*
@@ -58292,12 +58861,12 @@
58861 /*
58862 ** Change the P2 operand of instruction addr so that it points to
58863 ** the address of the next instruction to be coded.
58864 */
58865 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58866 assert( addr>=0 );
58867 sqlite3VdbeChangeP2(p, addr, p->nOp);
58868 }
58869
58870
58871 /*
58872 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -58661,10 +59230,14 @@
59230 break;
59231 }
59232 case P4_SUBPROGRAM: {
59233 sqlite3_snprintf(nTemp, zTemp, "program");
59234 break;
59235 }
59236 case P4_ADVANCE: {
59237 zTemp[0] = 0;
59238 break;
59239 }
59240 default: {
59241 zP4 = pOp->p4.z;
59242 if( zP4==0 ){
59243 zP4 = zTemp;
@@ -59285,10 +59858,11 @@
59858 */
59859 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
59860 if( pCx==0 ){
59861 return;
59862 }
59863 sqlite3VdbeSorterClose(p->db, pCx);
59864 if( pCx->pBt ){
59865 sqlite3BtreeClose(pCx->pBt);
59866 /* The pCx->pCursor will be close automatically, if it exists, by
59867 ** the call above. */
59868 }else if( pCx->pCursor ){
@@ -62585,10 +63159,17 @@
63159 ** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)
63160 ** P if required.
63161 */
63162 #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
63163
63164 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
63165 #ifdef SQLITE_OMIT_MERGE_SORT
63166 # define isSorter(x) 0
63167 #else
63168 # define isSorter(x) ((x)->pSorter!=0)
63169 #endif
63170
63171 /*
63172 ** Argument pMem points at a register that will be passed to a
63173 ** user-defined function or returned to the user as the result of a query.
63174 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
63175 ** routines.
@@ -63179,10 +63760,11 @@
63760 u8 *zEndHdr; /* Pointer to first byte after the header */
63761 u32 offset; /* Offset into the data */
63762 u32 szField; /* Number of bytes in the content of a field */
63763 int szHdr; /* Size of the header size field at start of record */
63764 int avail; /* Number of bytes of available data */
63765 u32 t; /* A type code from the record header */
63766 Mem *pReg; /* PseudoTable input register */
63767 } am;
63768 struct OP_Affinity_stack_vars {
63769 const char *zAffinity; /* The affinity to be applied */
63770 char cAff; /* A single character of affinity */
@@ -63250,55 +63832,58 @@
63832 Db *pDb;
63833 } aw;
63834 struct OP_OpenEphemeral_stack_vars {
63835 VdbeCursor *pCx;
63836 } ax;
63837 struct OP_SorterOpen_stack_vars {
63838 VdbeCursor *pCx;
63839 } ay;
63840 struct OP_OpenPseudo_stack_vars {
63841 VdbeCursor *pCx;
63842 } az;
63843 struct OP_SeekGt_stack_vars {
63844 int res;
63845 int oc;
63846 VdbeCursor *pC;
63847 UnpackedRecord r;
63848 int nField;
63849 i64 iKey; /* The rowid we are to seek to */
63850 } ba;
63851 struct OP_Seek_stack_vars {
63852 VdbeCursor *pC;
63853 } bb;
63854 struct OP_Found_stack_vars {
63855 int alreadyExists;
63856 VdbeCursor *pC;
63857 int res;
63858 UnpackedRecord *pIdxKey;
63859 UnpackedRecord r;
63860 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
63861 } bc;
63862 struct OP_IsUnique_stack_vars {
63863 u16 ii;
63864 VdbeCursor *pCx;
63865 BtCursor *pCrsr;
63866 u16 nField;
63867 Mem *aMx;
63868 UnpackedRecord r; /* B-Tree index search key */
63869 i64 R; /* Rowid stored in register P3 */
63870 } bd;
63871 struct OP_NotExists_stack_vars {
63872 VdbeCursor *pC;
63873 BtCursor *pCrsr;
63874 int res;
63875 u64 iKey;
63876 } be;
63877 struct OP_NewRowid_stack_vars {
63878 i64 v; /* The new rowid */
63879 VdbeCursor *pC; /* Cursor of table to get the new rowid */
63880 int res; /* Result of an sqlite3BtreeLast() */
63881 int cnt; /* Counter to limit the number of searches */
63882 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
63883 VdbeFrame *pFrame; /* Root frame of VDBE */
63884 } bf;
63885 struct OP_InsertInt_stack_vars {
63886 Mem *pData; /* MEM cell holding data for the record to be inserted */
63887 Mem *pKey; /* MEM cell holding key for the record */
63888 i64 iKey; /* The integer ROWID or key for the record to be inserted */
63889 VdbeCursor *pC; /* Cursor to table into which insert is written */
@@ -63305,155 +63890,161 @@
63890 int nZero; /* Number of zero-bytes to append */
63891 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
63892 const char *zDb; /* database name - used by the update hook */
63893 const char *zTbl; /* Table name - used by the opdate hook */
63894 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
63895 } bg;
63896 struct OP_Delete_stack_vars {
63897 i64 iKey;
63898 VdbeCursor *pC;
63899 } bh;
63900 struct OP_SorterCompare_stack_vars {
63901 VdbeCursor *pC;
63902 int res;
63903 } bi;
63904 struct OP_SorterData_stack_vars {
63905 VdbeCursor *pC;
63906 } bj;
63907 struct OP_RowData_stack_vars {
63908 VdbeCursor *pC;
63909 BtCursor *pCrsr;
63910 u32 n;
63911 i64 n64;
63912 } bk;
63913 struct OP_Rowid_stack_vars {
63914 VdbeCursor *pC;
63915 i64 v;
63916 sqlite3_vtab *pVtab;
63917 const sqlite3_module *pModule;
63918 } bl;
63919 struct OP_NullRow_stack_vars {
63920 VdbeCursor *pC;
63921 } bm;
63922 struct OP_Last_stack_vars {
63923 VdbeCursor *pC;
63924 BtCursor *pCrsr;
63925 int res;
63926 } bn;
63927 struct OP_Rewind_stack_vars {
63928 VdbeCursor *pC;
63929 BtCursor *pCrsr;
63930 int res;
63931 } bo;
63932 struct OP_Next_stack_vars {
63933 VdbeCursor *pC;
 
63934 int res;
63935 } bp;
63936 struct OP_IdxInsert_stack_vars {
63937 VdbeCursor *pC;
63938 BtCursor *pCrsr;
63939 int nKey;
63940 const char *zKey;
63941 } bq;
63942 struct OP_IdxDelete_stack_vars {
63943 VdbeCursor *pC;
63944 BtCursor *pCrsr;
63945 int res;
63946 UnpackedRecord r;
63947 } br;
63948 struct OP_IdxRowid_stack_vars {
63949 BtCursor *pCrsr;
63950 VdbeCursor *pC;
63951 i64 rowid;
63952 } bs;
63953 struct OP_IdxGE_stack_vars {
63954 VdbeCursor *pC;
63955 int res;
63956 UnpackedRecord r;
63957 } bt;
63958 struct OP_Destroy_stack_vars {
63959 int iMoved;
63960 int iCnt;
63961 Vdbe *pVdbe;
63962 int iDb;
63963 } bu;
63964 struct OP_Clear_stack_vars {
63965 int nChange;
63966 } bv;
63967 struct OP_CreateTable_stack_vars {
63968 int pgno;
63969 int flags;
63970 Db *pDb;
63971 } bw;
63972 struct OP_ParseSchema_stack_vars {
63973 int iDb;
63974 const char *zMaster;
63975 char *zSql;
63976 InitData initData;
63977 } bx;
63978 struct OP_IntegrityCk_stack_vars {
63979 int nRoot; /* Number of tables to check. (Number of root pages.) */
63980 int *aRoot; /* Array of rootpage numbers for tables to be checked */
63981 int j; /* Loop counter */
63982 int nErr; /* Number of errors reported */
63983 char *z; /* Text of the error report */
63984 Mem *pnErr; /* Register keeping track of errors remaining */
63985 } by;
63986 struct OP_RowSetRead_stack_vars {
63987 i64 val;
63988 } bz;
63989 struct OP_RowSetTest_stack_vars {
63990 int iSet;
63991 int exists;
63992 } ca;
63993 struct OP_Program_stack_vars {
63994 int nMem; /* Number of memory registers for sub-program */
63995 int nByte; /* Bytes of runtime space required for sub-program */
63996 Mem *pRt; /* Register to allocate runtime space */
63997 Mem *pMem; /* Used to iterate through memory cells */
63998 Mem *pEnd; /* Last memory cell in new array */
63999 VdbeFrame *pFrame; /* New vdbe frame to execute in */
64000 SubProgram *pProgram; /* Sub-program to execute */
64001 void *t; /* Token identifying trigger */
64002 } cb;
64003 struct OP_Param_stack_vars {
64004 VdbeFrame *pFrame;
64005 Mem *pIn;
64006 } cc;
64007 struct OP_MemMax_stack_vars {
64008 Mem *pIn1;
64009 VdbeFrame *pFrame;
64010 } cd;
64011 struct OP_AggStep_stack_vars {
64012 int n;
64013 int i;
64014 Mem *pMem;
64015 Mem *pRec;
64016 sqlite3_context ctx;
64017 sqlite3_value **apVal;
64018 } ce;
64019 struct OP_AggFinal_stack_vars {
64020 Mem *pMem;
64021 } cf;
64022 struct OP_Checkpoint_stack_vars {
64023 int i; /* Loop counter */
64024 int aRes[3]; /* Results */
64025 Mem *pMem; /* Write results here */
64026 } cg;
64027 struct OP_JournalMode_stack_vars {
64028 Btree *pBt; /* Btree to change journal mode of */
64029 Pager *pPager; /* Pager associated with pBt */
64030 int eNew; /* New journal mode */
64031 int eOld; /* The old journal mode */
64032 const char *zFilename; /* Name of database file for pPager */
64033 } ch;
64034 struct OP_IncrVacuum_stack_vars {
64035 Btree *pBt;
64036 } ci;
64037 struct OP_VBegin_stack_vars {
64038 VTable *pVTab;
64039 } cj;
64040 struct OP_VOpen_stack_vars {
64041 VdbeCursor *pCur;
64042 sqlite3_vtab_cursor *pVtabCursor;
64043 sqlite3_vtab *pVtab;
64044 sqlite3_module *pModule;
64045 } ck;
64046 struct OP_VFilter_stack_vars {
64047 int nArg;
64048 int iQuery;
64049 const sqlite3_module *pModule;
64050 Mem *pQuery;
@@ -63462,40 +64053,40 @@
64053 sqlite3_vtab *pVtab;
64054 VdbeCursor *pCur;
64055 int res;
64056 int i;
64057 Mem **apArg;
64058 } cl;
64059 struct OP_VColumn_stack_vars {
64060 sqlite3_vtab *pVtab;
64061 const sqlite3_module *pModule;
64062 Mem *pDest;
64063 sqlite3_context sContext;
64064 } cm;
64065 struct OP_VNext_stack_vars {
64066 sqlite3_vtab *pVtab;
64067 const sqlite3_module *pModule;
64068 int res;
64069 VdbeCursor *pCur;
64070 } cn;
64071 struct OP_VRename_stack_vars {
64072 sqlite3_vtab *pVtab;
64073 Mem *pName;
64074 } co;
64075 struct OP_VUpdate_stack_vars {
64076 sqlite3_vtab *pVtab;
64077 sqlite3_module *pModule;
64078 int nArg;
64079 int i;
64080 sqlite_int64 rowid;
64081 Mem **apArg;
64082 Mem *pX;
64083 } cp;
64084 struct OP_Trace_stack_vars {
64085 char *zTrace;
64086 char *z;
64087 } cq;
64088 } u;
64089 /* End automatically generated code
64090 ********************************************************************/
64091
64092 assert( p->magic==VDBE_MAGIC_RUN ); /* sqlite3_step() verifies this */
@@ -63591,11 +64182,11 @@
64182 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
64183 assert( pOp->p2>0 );
64184 assert( pOp->p2<=p->nMem );
64185 pOut = &aMem[pOp->p2];
64186 memAboutToChange(p, pOut);
64187 MemReleaseExt(pOut);
64188 pOut->flags = MEM_Int;
64189 }
64190
64191 /* Sanity checking on other operands */
64192 #ifdef SQLITE_DEBUG
@@ -65061,10 +65652,11 @@
65652 u8 *zEndHdr; /* Pointer to first byte after the header */
65653 u32 offset; /* Offset into the data */
65654 u32 szField; /* Number of bytes in the content of a field */
65655 int szHdr; /* Size of the header size field at start of record */
65656 int avail; /* Number of bytes of available data */
65657 u32 t; /* A type code from the record header */
65658 Mem *pReg; /* PseudoTable input register */
65659 #endif /* local variables moved into u.am */
65660
65661
65662 u.am.p1 = pOp->p1;
@@ -65073,11 +65665,10 @@
65665 memset(&u.am.sMem, 0, sizeof(u.am.sMem));
65666 assert( u.am.p1<p->nCursor );
65667 assert( pOp->p3>0 && pOp->p3<=p->nMem );
65668 u.am.pDest = &aMem[pOp->p3];
65669 memAboutToChange(p, u.am.pDest);
 
65670 u.am.zRec = 0;
65671
65672 /* This block sets the variable u.am.payloadSize to be the total number of
65673 ** bytes in the record.
65674 **
@@ -65117,11 +65708,11 @@
65708 }else{
65709 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65710 rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65711 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65712 }
65713 }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65714 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65715 assert( u.am.pReg->flags & MEM_Blob );
65716 assert( memIsValid(u.am.pReg) );
65717 u.am.payloadSize = u.am.pReg->n;
65718 u.am.zRec = u.am.pReg->z;
@@ -65130,13 +65721,14 @@
65721 }else{
65722 /* Consider the row to be NULL */
65723 u.am.payloadSize = 0;
65724 }
65725
65726 /* If u.am.payloadSize is 0, then just store a NULL. This can happen because of
65727 ** nullRow or because of a corrupt database. */
65728 if( u.am.payloadSize==0 ){
65729 MemSetTypeFlag(u.am.pDest, MEM_Null);
65730 goto op_column_out;
65731 }
65732 assert( db->aLimit[SQLITE_LIMIT_LENGTH]>=0 );
65733 if( u.am.payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
65734 goto too_big;
@@ -65239,12 +65831,18 @@
65831 ** of the record to the start of the data for the u.am.i-th column
65832 */
65833 for(u.am.i=0; u.am.i<u.am.nField; u.am.i++){
65834 if( u.am.zIdx<u.am.zEndHdr ){
65835 u.am.aOffset[u.am.i] = u.am.offset;
65836 if( u.am.zIdx[0]<0x80 ){
65837 u.am.t = u.am.zIdx[0];
65838 u.am.zIdx++;
65839 }else{
65840 u.am.zIdx += sqlite3GetVarint32(u.am.zIdx, &u.am.t);
65841 }
65842 u.am.aType[u.am.i] = u.am.t;
65843 u.am.szField = sqlite3VdbeSerialTypeLen(u.am.t);
65844 u.am.offset += u.am.szField;
65845 if( u.am.offset<u.am.szField ){ /* True if u.am.offset overflows */
65846 u.am.zIdx = &u.am.zEndHdr[1]; /* Forces SQLITE_CORRUPT return below */
65847 break;
65848 }
@@ -65281,11 +65879,11 @@
65879 ** a pointer to a Mem object.
65880 */
65881 if( u.am.aOffset[u.am.p2] ){
65882 assert( rc==SQLITE_OK );
65883 if( u.am.zRec ){
65884 MemReleaseExt(u.am.pDest);
65885 sqlite3VdbeSerialGet((u8 *)&u.am.zRec[u.am.aOffset[u.am.p2]], u.am.aType[u.am.p2], u.am.pDest);
65886 }else{
65887 u.am.len = sqlite3VdbeSerialTypeLen(u.am.aType[u.am.p2]);
65888 sqlite3VdbeMemMove(&u.am.sMem, u.am.pDest);
65889 rc = sqlite3VdbeMemFromBtree(u.am.pCrsr, u.am.aOffset[u.am.p2], u.am.len, u.am.pC->isIndex, &u.am.sMem);
@@ -65298,11 +65896,11 @@
65896 u.am.pDest->enc = encoding;
65897 }else{
65898 if( pOp->p4type==P4_MEM ){
65899 sqlite3VdbeMemShallowCopy(u.am.pDest, pOp->p4.pMem, MEM_Static);
65900 }else{
65901 MemSetTypeFlag(u.am.pDest, MEM_Null);
65902 }
65903 }
65904
65905 /* If we dynamically allocated space to hold the data (in the
65906 ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
@@ -65500,11 +66098,11 @@
66098 i64 nEntry;
66099 BtCursor *pCrsr;
66100 #endif /* local variables moved into u.ap */
66101
66102 u.ap.pCrsr = p->apCsr[pOp->p1]->pCursor;
66103 if( ALWAYS(u.ap.pCrsr) ){
66104 rc = sqlite3BtreeCount(u.ap.pCrsr, &u.ap.nEntry);
66105 }else{
66106 u.ap.nEntry = 0;
66107 }
66108 pOut->u.i = u.ap.nEntry;
@@ -66076,19 +66674,13 @@
66674 u.aw.pCur->nullRow = 1;
66675 u.aw.pCur->isOrdered = 1;
66676 rc = sqlite3BtreeCursor(u.aw.pX, u.aw.p2, u.aw.wrFlag, u.aw.pKeyInfo, u.aw.pCur->pCursor);
66677 u.aw.pCur->pKeyInfo = u.aw.pKeyInfo;
66678
66679 /* Since it performs no memory allocation or IO, the only value that
66680 ** sqlite3BtreeCursor() may return is SQLITE_OK. */
66681 assert( rc==SQLITE_OK );
 
 
 
 
 
 
66682
66683 /* Set the VdbeCursor.isTable and isIndex variables. Previous versions of
66684 ** SQLite used to check if the root-page flags were sane at this point
66685 ** and report database corruption if they were not, but this check has
66686 ** since moved into the btree layer. */
@@ -66095,11 +66687,11 @@
66687 u.aw.pCur->isTable = pOp->p4type!=P4_KEYINFO;
66688 u.aw.pCur->isIndex = !u.aw.pCur->isTable;
66689 break;
66690 }
66691
66692 /* Opcode: OpenEphemeral P1 P2 * P4 P5
66693 **
66694 ** Open a new cursor P1 to a transient table.
66695 ** The cursor is always opened read/write even if
66696 ** the main database is read-only. The ephemeral
66697 ** table is deleted automatically when the cursor is closed.
@@ -66112,10 +66704,15 @@
66704 ** This opcode was once called OpenTemp. But that created
66705 ** confusion because the term "temp table", might refer either
66706 ** to a TEMP table at the SQL level, or to a table opened by
66707 ** this opcode. Then this opcode was call OpenVirtual. But
66708 ** that created confusion with the whole virtual-table idea.
66709 **
66710 ** The P5 parameter can be a mask of the BTREE_* flags defined
66711 ** in btree.h. These flags control aspects of the operation of
66712 ** the btree. The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
66713 ** added automatically.
66714 */
66715 /* Opcode: OpenAutoindex P1 P2 * P4 *
66716 **
66717 ** This opcode works the same as OP_OpenEphemeral. It has a
66718 ** different name to distinguish its use. Tables created using
@@ -66168,10 +66765,34 @@
66765 }
66766 u.ax.pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
66767 u.ax.pCx->isIndex = !u.ax.pCx->isTable;
66768 break;
66769 }
66770
66771 /* Opcode: OpenSorter P1 P2 * P4 *
66772 **
66773 ** This opcode works like OP_OpenEphemeral except that it opens
66774 ** a transient index that is specifically designed to sort large
66775 ** tables using an external merge-sort algorithm.
66776 */
66777 case OP_SorterOpen: {
66778 #if 0 /* local variables moved into u.ay */
66779 VdbeCursor *pCx;
66780 #endif /* local variables moved into u.ay */
66781 #ifndef SQLITE_OMIT_MERGE_SORT
66782 u.ay.pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
66783 if( u.ay.pCx==0 ) goto no_mem;
66784 u.ay.pCx->pKeyInfo = pOp->p4.pKeyInfo;
66785 u.ay.pCx->pKeyInfo->enc = ENC(p->db);
66786 u.ay.pCx->isSorter = 1;
66787 rc = sqlite3VdbeSorterInit(db, u.ay.pCx);
66788 #else
66789 pOp->opcode = OP_OpenEphemeral;
66790 pc--;
66791 #endif
66792 break;
66793 }
66794
66795 /* Opcode: OpenPseudo P1 P2 P3 * *
66796 **
66797 ** Open a new cursor that points to a fake table that contains a single
66798 ** row of data. The content of that one row in the content of memory
@@ -66185,21 +66806,21 @@
66806 **
66807 ** P3 is the number of fields in the records that will be stored by
66808 ** the pseudo-table.
66809 */
66810 case OP_OpenPseudo: {
66811 #if 0 /* local variables moved into u.az */
66812 VdbeCursor *pCx;
66813 #endif /* local variables moved into u.az */
66814
66815 assert( pOp->p1>=0 );
66816 u.az.pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
66817 if( u.az.pCx==0 ) goto no_mem;
66818 u.az.pCx->nullRow = 1;
66819 u.az.pCx->pseudoTableReg = pOp->p2;
66820 u.az.pCx->isTable = 1;
66821 u.az.pCx->isIndex = 0;
66822 break;
66823 }
66824
66825 /* Opcode: Close P1 * * * *
66826 **
@@ -66267,39 +66888,39 @@
66888 */
66889 case OP_SeekLt: /* jump, in3 */
66890 case OP_SeekLe: /* jump, in3 */
66891 case OP_SeekGe: /* jump, in3 */
66892 case OP_SeekGt: { /* jump, in3 */
66893 #if 0 /* local variables moved into u.ba */
66894 int res;
66895 int oc;
66896 VdbeCursor *pC;
66897 UnpackedRecord r;
66898 int nField;
66899 i64 iKey; /* The rowid we are to seek to */
66900 #endif /* local variables moved into u.ba */
66901
66902 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
66903 assert( pOp->p2!=0 );
66904 u.ba.pC = p->apCsr[pOp->p1];
66905 assert( u.ba.pC!=0 );
66906 assert( u.ba.pC->pseudoTableReg==0 );
66907 assert( OP_SeekLe == OP_SeekLt+1 );
66908 assert( OP_SeekGe == OP_SeekLt+2 );
66909 assert( OP_SeekGt == OP_SeekLt+3 );
66910 assert( u.ba.pC->isOrdered );
66911 if( ALWAYS(u.ba.pC->pCursor!=0) ){
66912 u.ba.oc = pOp->opcode;
66913 u.ba.pC->nullRow = 0;
66914 if( u.ba.pC->isTable ){
66915 /* The input value in P3 might be of any type: integer, real, string,
66916 ** blob, or NULL. But it needs to be an integer before we can do
66917 ** the seek, so covert it. */
66918 pIn3 = &aMem[pOp->p3];
66919 applyNumericAffinity(pIn3);
66920 u.ba.iKey = sqlite3VdbeIntValue(pIn3);
66921 u.ba.pC->rowidIsValid = 0;
66922
66923 /* If the P3 value could not be converted into an integer without
66924 ** loss of information, then special processing is required... */
66925 if( (pIn3->flags & MEM_Int)==0 ){
66926 if( (pIn3->flags & MEM_Real)==0 ){
@@ -66310,105 +66931,105 @@
66931 }
66932 /* If we reach this point, then the P3 value must be a floating
66933 ** point number. */
66934 assert( (pIn3->flags & MEM_Real)!=0 );
66935
66936 if( u.ba.iKey==SMALLEST_INT64 && (pIn3->r<(double)u.ba.iKey || pIn3->r>0) ){
66937 /* The P3 value is too large in magnitude to be expressed as an
66938 ** integer. */
66939 u.ba.res = 1;
66940 if( pIn3->r<0 ){
66941 if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
66942 rc = sqlite3BtreeFirst(u.ba.pC->pCursor, &u.ba.res);
66943 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66944 }
66945 }else{
66946 if( u.ba.oc<=OP_SeekLe ){ assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
66947 rc = sqlite3BtreeLast(u.ba.pC->pCursor, &u.ba.res);
66948 if( rc!=SQLITE_OK ) goto abort_due_to_error;
66949 }
66950 }
66951 if( u.ba.res ){
66952 pc = pOp->p2 - 1;
66953 }
66954 break;
66955 }else if( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekGe ){
66956 /* Use the ceiling() function to convert real->int */
66957 if( pIn3->r > (double)u.ba.iKey ) u.ba.iKey++;
66958 }else{
66959 /* Use the floor() function to convert real->int */
66960 assert( u.ba.oc==OP_SeekLe || u.ba.oc==OP_SeekGt );
66961 if( pIn3->r < (double)u.ba.iKey ) u.ba.iKey--;
66962 }
66963 }
66964 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, 0, (u64)u.ba.iKey, 0, &u.ba.res);
66965 if( rc!=SQLITE_OK ){
66966 goto abort_due_to_error;
66967 }
66968 if( u.ba.res==0 ){
66969 u.ba.pC->rowidIsValid = 1;
66970 u.ba.pC->lastRowid = u.ba.iKey;
66971 }
66972 }else{
66973 u.ba.nField = pOp->p4.i;
66974 assert( pOp->p4type==P4_INT32 );
66975 assert( u.ba.nField>0 );
66976 u.ba.r.pKeyInfo = u.ba.pC->pKeyInfo;
66977 u.ba.r.nField = (u16)u.ba.nField;
66978
66979 /* The next line of code computes as follows, only faster:
66980 ** if( u.ba.oc==OP_SeekGt || u.ba.oc==OP_SeekLe ){
66981 ** u.ba.r.flags = UNPACKED_INCRKEY;
66982 ** }else{
66983 ** u.ba.r.flags = 0;
66984 ** }
66985 */
66986 u.ba.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.ba.oc - OP_SeekLt)));
66987 assert( u.ba.oc!=OP_SeekGt || u.ba.r.flags==UNPACKED_INCRKEY );
66988 assert( u.ba.oc!=OP_SeekLe || u.ba.r.flags==UNPACKED_INCRKEY );
66989 assert( u.ba.oc!=OP_SeekGe || u.ba.r.flags==0 );
66990 assert( u.ba.oc!=OP_SeekLt || u.ba.r.flags==0 );
66991
66992 u.ba.r.aMem = &aMem[pOp->p3];
66993 #ifdef SQLITE_DEBUG
66994 { int i; for(i=0; i<u.ba.r.nField; i++) assert( memIsValid(&u.ba.r.aMem[i]) ); }
66995 #endif
66996 ExpandBlob(u.ba.r.aMem);
66997 rc = sqlite3BtreeMovetoUnpacked(u.ba.pC->pCursor, &u.ba.r, 0, 0, &u.ba.res);
66998 if( rc!=SQLITE_OK ){
66999 goto abort_due_to_error;
67000 }
67001 u.ba.pC->rowidIsValid = 0;
67002 }
67003 u.ba.pC->deferredMoveto = 0;
67004 u.ba.pC->cacheStatus = CACHE_STALE;
67005 #ifdef SQLITE_TEST
67006 sqlite3_search_count++;
67007 #endif
67008 if( u.ba.oc>=OP_SeekGe ){ assert( u.ba.oc==OP_SeekGe || u.ba.oc==OP_SeekGt );
67009 if( u.ba.res<0 || (u.ba.res==0 && u.ba.oc==OP_SeekGt) ){
67010 rc = sqlite3BtreeNext(u.ba.pC->pCursor, &u.ba.res);
67011 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67012 u.ba.pC->rowidIsValid = 0;
67013 }else{
67014 u.ba.res = 0;
67015 }
67016 }else{
67017 assert( u.ba.oc==OP_SeekLt || u.ba.oc==OP_SeekLe );
67018 if( u.ba.res>0 || (u.ba.res==0 && u.ba.oc==OP_SeekLt) ){
67019 rc = sqlite3BtreePrevious(u.ba.pC->pCursor, &u.ba.res);
67020 if( rc!=SQLITE_OK ) goto abort_due_to_error;
67021 u.ba.pC->rowidIsValid = 0;
67022 }else{
67023 /* u.ba.res might be negative because the table is empty. Check to
67024 ** see if this is the case.
67025 */
67026 u.ba.res = sqlite3BtreeEof(u.ba.pC->pCursor);
67027 }
67028 }
67029 assert( pOp->p2>0 );
67030 if( u.ba.res ){
67031 pc = pOp->p2 - 1;
67032 }
67033 }else{
67034 /* This happens when attempting to open the sqlite3_master table
67035 ** for read access returns SQLITE_EMPTY. In this case always
@@ -66427,24 +67048,24 @@
67048 ** This is actually a deferred seek. Nothing actually happens until
67049 ** the cursor is used to read a record. That way, if no reads
67050 ** occur, no unnecessary I/O happens.
67051 */
67052 case OP_Seek: { /* in2 */
67053 #if 0 /* local variables moved into u.bb */
67054 VdbeCursor *pC;
67055 #endif /* local variables moved into u.bb */
67056
67057 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67058 u.bb.pC = p->apCsr[pOp->p1];
67059 assert( u.bb.pC!=0 );
67060 if( ALWAYS(u.bb.pC->pCursor!=0) ){
67061 assert( u.bb.pC->isTable );
67062 u.bb.pC->nullRow = 0;
67063 pIn2 = &aMem[pOp->p2];
67064 u.bb.pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
67065 u.bb.pC->rowidIsValid = 0;
67066 u.bb.pC->deferredMoveto = 1;
67067 }
67068 break;
67069 }
67070
67071
@@ -66472,66 +67093,66 @@
67093 **
67094 ** See also: Found, NotExists, IsUnique
67095 */
67096 case OP_NotFound: /* jump, in3 */
67097 case OP_Found: { /* jump, in3 */
67098 #if 0 /* local variables moved into u.bc */
67099 int alreadyExists;
67100 VdbeCursor *pC;
67101 int res;
67102 UnpackedRecord *pIdxKey;
67103 UnpackedRecord r;
67104 char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*3 + 7];
67105 #endif /* local variables moved into u.bc */
67106
67107 #ifdef SQLITE_TEST
67108 sqlite3_found_count++;
67109 #endif
67110
67111 u.bc.alreadyExists = 0;
67112 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67113 assert( pOp->p4type==P4_INT32 );
67114 u.bc.pC = p->apCsr[pOp->p1];
67115 assert( u.bc.pC!=0 );
67116 pIn3 = &aMem[pOp->p3];
67117 if( ALWAYS(u.bc.pC->pCursor!=0) ){
67118
67119 assert( u.bc.pC->isTable==0 );
67120 if( pOp->p4.i>0 ){
67121 u.bc.r.pKeyInfo = u.bc.pC->pKeyInfo;
67122 u.bc.r.nField = (u16)pOp->p4.i;
67123 u.bc.r.aMem = pIn3;
67124 #ifdef SQLITE_DEBUG
67125 { int i; for(i=0; i<u.bc.r.nField; i++) assert( memIsValid(&u.bc.r.aMem[i]) ); }
67126 #endif
67127 u.bc.r.flags = UNPACKED_PREFIX_MATCH;
67128 u.bc.pIdxKey = &u.bc.r;
67129 }else{
67130 assert( pIn3->flags & MEM_Blob );
67131 assert( (pIn3->flags & MEM_Zero)==0 ); /* zeroblobs already expanded */
67132 u.bc.pIdxKey = sqlite3VdbeRecordUnpack(u.bc.pC->pKeyInfo, pIn3->n, pIn3->z,
67133 u.bc.aTempRec, sizeof(u.bc.aTempRec));
67134 if( u.bc.pIdxKey==0 ){
67135 goto no_mem;
67136 }
67137 u.bc.pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
67138 }
67139 rc = sqlite3BtreeMovetoUnpacked(u.bc.pC->pCursor, u.bc.pIdxKey, 0, 0, &u.bc.res);
67140 if( pOp->p4.i==0 ){
67141 sqlite3VdbeDeleteUnpackedRecord(u.bc.pIdxKey);
67142 }
67143 if( rc!=SQLITE_OK ){
67144 break;
67145 }
67146 u.bc.alreadyExists = (u.bc.res==0);
67147 u.bc.pC->deferredMoveto = 0;
67148 u.bc.pC->cacheStatus = CACHE_STALE;
67149 }
67150 if( pOp->opcode==OP_Found ){
67151 if( u.bc.alreadyExists ) pc = pOp->p2 - 1;
67152 }else{
67153 if( !u.bc.alreadyExists ) pc = pOp->p2 - 1;
67154 }
67155 break;
67156 }
67157
67158 /* Opcode: IsUnique P1 P2 P3 P4 *
@@ -66559,67 +67180,67 @@
67180 ** instruction.
67181 **
67182 ** See also: NotFound, NotExists, Found
67183 */
67184 case OP_IsUnique: { /* jump, in3 */
67185 #if 0 /* local variables moved into u.bd */
67186 u16 ii;
67187 VdbeCursor *pCx;
67188 BtCursor *pCrsr;
67189 u16 nField;
67190 Mem *aMx;
67191 UnpackedRecord r; /* B-Tree index search key */
67192 i64 R; /* Rowid stored in register P3 */
67193 #endif /* local variables moved into u.bd */
67194
67195 pIn3 = &aMem[pOp->p3];
67196 u.bd.aMx = &aMem[pOp->p4.i];
67197 /* Assert that the values of parameters P1 and P4 are in range. */
67198 assert( pOp->p4type==P4_INT32 );
67199 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
67200 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67201
67202 /* Find the index cursor. */
67203 u.bd.pCx = p->apCsr[pOp->p1];
67204 assert( u.bd.pCx->deferredMoveto==0 );
67205 u.bd.pCx->seekResult = 0;
67206 u.bd.pCx->cacheStatus = CACHE_STALE;
67207 u.bd.pCrsr = u.bd.pCx->pCursor;
67208
67209 /* If any of the values are NULL, take the jump. */
67210 u.bd.nField = u.bd.pCx->pKeyInfo->nField;
67211 for(u.bd.ii=0; u.bd.ii<u.bd.nField; u.bd.ii++){
67212 if( u.bd.aMx[u.bd.ii].flags & MEM_Null ){
67213 pc = pOp->p2 - 1;
67214 u.bd.pCrsr = 0;
67215 break;
67216 }
67217 }
67218 assert( (u.bd.aMx[u.bd.nField].flags & MEM_Null)==0 );
67219
67220 if( u.bd.pCrsr!=0 ){
67221 /* Populate the index search key. */
67222 u.bd.r.pKeyInfo = u.bd.pCx->pKeyInfo;
67223 u.bd.r.nField = u.bd.nField + 1;
67224 u.bd.r.flags = UNPACKED_PREFIX_SEARCH;
67225 u.bd.r.aMem = u.bd.aMx;
67226 #ifdef SQLITE_DEBUG
67227 { int i; for(i=0; i<u.bd.r.nField; i++) assert( memIsValid(&u.bd.r.aMem[i]) ); }
67228 #endif
67229
67230 /* Extract the value of u.bd.R from register P3. */
67231 sqlite3VdbeMemIntegerify(pIn3);
67232 u.bd.R = pIn3->u.i;
67233
67234 /* Search the B-Tree index. If no conflicting record is found, jump
67235 ** to P2. Otherwise, copy the rowid of the conflicting record to
67236 ** register P3 and fall through to the next instruction. */
67237 rc = sqlite3BtreeMovetoUnpacked(u.bd.pCrsr, &u.bd.r, 0, 0, &u.bd.pCx->seekResult);
67238 if( (u.bd.r.flags & UNPACKED_PREFIX_SEARCH) || u.bd.r.rowid==u.bd.R ){
67239 pc = pOp->p2 - 1;
67240 }else{
67241 pIn3->u.i = u.bd.r.rowid;
67242 }
67243 }
67244 break;
67245 }
67246
@@ -66636,46 +67257,46 @@
67257 ** P1 is an index.
67258 **
67259 ** See also: Found, NotFound, IsUnique
67260 */
67261 case OP_NotExists: { /* jump, in3 */
67262 #if 0 /* local variables moved into u.be */
67263 VdbeCursor *pC;
67264 BtCursor *pCrsr;
67265 int res;
67266 u64 iKey;
67267 #endif /* local variables moved into u.be */
67268
67269 pIn3 = &aMem[pOp->p3];
67270 assert( pIn3->flags & MEM_Int );
67271 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67272 u.be.pC = p->apCsr[pOp->p1];
67273 assert( u.be.pC!=0 );
67274 assert( u.be.pC->isTable );
67275 assert( u.be.pC->pseudoTableReg==0 );
67276 u.be.pCrsr = u.be.pC->pCursor;
67277 if( ALWAYS(u.be.pCrsr!=0) ){
67278 u.be.res = 0;
67279 u.be.iKey = pIn3->u.i;
67280 rc = sqlite3BtreeMovetoUnpacked(u.be.pCrsr, 0, u.be.iKey, 0, &u.be.res);
67281 u.be.pC->lastRowid = pIn3->u.i;
67282 u.be.pC->rowidIsValid = u.be.res==0 ?1:0;
67283 u.be.pC->nullRow = 0;
67284 u.be.pC->cacheStatus = CACHE_STALE;
67285 u.be.pC->deferredMoveto = 0;
67286 if( u.be.res!=0 ){
67287 pc = pOp->p2 - 1;
67288 assert( u.be.pC->rowidIsValid==0 );
67289 }
67290 u.be.pC->seekResult = u.be.res;
67291 }else{
67292 /* This happens when an attempt to open a read cursor on the
67293 ** sqlite_master table returns SQLITE_EMPTY.
67294 */
67295 pc = pOp->p2 - 1;
67296 assert( u.be.pC->rowidIsValid==0 );
67297 u.be.pC->seekResult = 0;
67298 }
67299 break;
67300 }
67301
67302 /* Opcode: Sequence P1 P2 * * *
@@ -66706,25 +67327,25 @@
67327 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
67328 ** generated record number. This P3 mechanism is used to help implement the
67329 ** AUTOINCREMENT feature.
67330 */
67331 case OP_NewRowid: { /* out2-prerelease */
67332 #if 0 /* local variables moved into u.bf */
67333 i64 v; /* The new rowid */
67334 VdbeCursor *pC; /* Cursor of table to get the new rowid */
67335 int res; /* Result of an sqlite3BtreeLast() */
67336 int cnt; /* Counter to limit the number of searches */
67337 Mem *pMem; /* Register holding largest rowid for AUTOINCREMENT */
67338 VdbeFrame *pFrame; /* Root frame of VDBE */
67339 #endif /* local variables moved into u.bf */
67340
67341 u.bf.v = 0;
67342 u.bf.res = 0;
67343 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67344 u.bf.pC = p->apCsr[pOp->p1];
67345 assert( u.bf.pC!=0 );
67346 if( NEVER(u.bf.pC->pCursor==0) ){
67347 /* The zero initialization above is all that is needed */
67348 }else{
67349 /* The next rowid or record number (different terms for the same
67350 ** thing) is obtained in a two-step algorithm.
67351 **
@@ -66736,11 +67357,11 @@
67357 ** The second algorithm is to select a rowid at random and see if
67358 ** it already exists in the table. If it does not exist, we have
67359 ** succeeded. If the random rowid does exist, we select a new one
67360 ** and try again, up to 100 times.
67361 */
67362 assert( u.bf.pC->isTable );
67363
67364 #ifdef SQLITE_32BIT_ROWID
67365 # define MAX_ROWID 0x7fffffff
67366 #else
67367 /* Some compilers complain about constants of the form 0x7fffffffffffffff.
@@ -66748,101 +67369,101 @@
67369 ** to provide the constant while making all compilers happy.
67370 */
67371 # define MAX_ROWID (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
67372 #endif
67373
67374 if( !u.bf.pC->useRandomRowid ){
67375 u.bf.v = sqlite3BtreeGetCachedRowid(u.bf.pC->pCursor);
67376 if( u.bf.v==0 ){
67377 rc = sqlite3BtreeLast(u.bf.pC->pCursor, &u.bf.res);
67378 if( rc!=SQLITE_OK ){
67379 goto abort_due_to_error;
67380 }
67381 if( u.bf.res ){
67382 u.bf.v = 1; /* IMP: R-61914-48074 */
67383 }else{
67384 assert( sqlite3BtreeCursorIsValid(u.bf.pC->pCursor) );
67385 rc = sqlite3BtreeKeySize(u.bf.pC->pCursor, &u.bf.v);
67386 assert( rc==SQLITE_OK ); /* Cannot fail following BtreeLast() */
67387 if( u.bf.v==MAX_ROWID ){
67388 u.bf.pC->useRandomRowid = 1;
67389 }else{
67390 u.bf.v++; /* IMP: R-29538-34987 */
67391 }
67392 }
67393 }
67394
67395 #ifndef SQLITE_OMIT_AUTOINCREMENT
67396 if( pOp->p3 ){
67397 /* Assert that P3 is a valid memory cell. */
67398 assert( pOp->p3>0 );
67399 if( p->pFrame ){
67400 for(u.bf.pFrame=p->pFrame; u.bf.pFrame->pParent; u.bf.pFrame=u.bf.pFrame->pParent);
67401 /* Assert that P3 is a valid memory cell. */
67402 assert( pOp->p3<=u.bf.pFrame->nMem );
67403 u.bf.pMem = &u.bf.pFrame->aMem[pOp->p3];
67404 }else{
67405 /* Assert that P3 is a valid memory cell. */
67406 assert( pOp->p3<=p->nMem );
67407 u.bf.pMem = &aMem[pOp->p3];
67408 memAboutToChange(p, u.bf.pMem);
67409 }
67410 assert( memIsValid(u.bf.pMem) );
67411
67412 REGISTER_TRACE(pOp->p3, u.bf.pMem);
67413 sqlite3VdbeMemIntegerify(u.bf.pMem);
67414 assert( (u.bf.pMem->flags & MEM_Int)!=0 ); /* mem(P3) holds an integer */
67415 if( u.bf.pMem->u.i==MAX_ROWID || u.bf.pC->useRandomRowid ){
67416 rc = SQLITE_FULL; /* IMP: R-12275-61338 */
67417 goto abort_due_to_error;
67418 }
67419 if( u.bf.v<u.bf.pMem->u.i+1 ){
67420 u.bf.v = u.bf.pMem->u.i + 1;
67421 }
67422 u.bf.pMem->u.i = u.bf.v;
67423 }
67424 #endif
67425
67426 sqlite3BtreeSetCachedRowid(u.bf.pC->pCursor, u.bf.v<MAX_ROWID ? u.bf.v+1 : 0);
67427 }
67428 if( u.bf.pC->useRandomRowid ){
67429 /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
67430 ** largest possible integer (9223372036854775807) then the database
67431 ** engine starts picking positive candidate ROWIDs at random until
67432 ** it finds one that is not previously used. */
67433 assert( pOp->p3==0 ); /* We cannot be in random rowid mode if this is
67434 ** an AUTOINCREMENT table. */
67435 /* on the first attempt, simply do one more than previous */
67436 u.bf.v = lastRowid;
67437 u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67438 u.bf.v++; /* ensure non-zero */
67439 u.bf.cnt = 0;
67440 while( ((rc = sqlite3BtreeMovetoUnpacked(u.bf.pC->pCursor, 0, (u64)u.bf.v,
67441 0, &u.bf.res))==SQLITE_OK)
67442 && (u.bf.res==0)
67443 && (++u.bf.cnt<100)){
67444 /* collision - try another random rowid */
67445 sqlite3_randomness(sizeof(u.bf.v), &u.bf.v);
67446 if( u.bf.cnt<5 ){
67447 /* try "small" random rowids for the initial attempts */
67448 u.bf.v &= 0xffffff;
67449 }else{
67450 u.bf.v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
67451 }
67452 u.bf.v++; /* ensure non-zero */
67453 }
67454 if( rc==SQLITE_OK && u.bf.res==0 ){
67455 rc = SQLITE_FULL; /* IMP: R-38219-53002 */
67456 goto abort_due_to_error;
67457 }
67458 assert( u.bf.v>0 ); /* EV: R-40812-03570 */
67459 }
67460 u.bf.pC->rowidIsValid = 0;
67461 u.bf.pC->deferredMoveto = 0;
67462 u.bf.pC->cacheStatus = CACHE_STALE;
67463 }
67464 pOut->u.i = u.bf.v;
67465 break;
67466 }
67467
67468 /* Opcode: Insert P1 P2 P3 P4 P5
67469 **
@@ -66888,74 +67509,74 @@
67509 ** This works exactly like OP_Insert except that the key is the
67510 ** integer value P3, not the value of the integer stored in register P3.
67511 */
67512 case OP_Insert:
67513 case OP_InsertInt: {
67514 #if 0 /* local variables moved into u.bg */
67515 Mem *pData; /* MEM cell holding data for the record to be inserted */
67516 Mem *pKey; /* MEM cell holding key for the record */
67517 i64 iKey; /* The integer ROWID or key for the record to be inserted */
67518 VdbeCursor *pC; /* Cursor to table into which insert is written */
67519 int nZero; /* Number of zero-bytes to append */
67520 int seekResult; /* Result of prior seek or 0 if no USESEEKRESULT flag */
67521 const char *zDb; /* database name - used by the update hook */
67522 const char *zTbl; /* Table name - used by the opdate hook */
67523 int op; /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
67524 #endif /* local variables moved into u.bg */
67525
67526 u.bg.pData = &aMem[pOp->p2];
67527 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67528 assert( memIsValid(u.bg.pData) );
67529 u.bg.pC = p->apCsr[pOp->p1];
67530 assert( u.bg.pC!=0 );
67531 assert( u.bg.pC->pCursor!=0 );
67532 assert( u.bg.pC->pseudoTableReg==0 );
67533 assert( u.bg.pC->isTable );
67534 REGISTER_TRACE(pOp->p2, u.bg.pData);
67535
67536 if( pOp->opcode==OP_Insert ){
67537 u.bg.pKey = &aMem[pOp->p3];
67538 assert( u.bg.pKey->flags & MEM_Int );
67539 assert( memIsValid(u.bg.pKey) );
67540 REGISTER_TRACE(pOp->p3, u.bg.pKey);
67541 u.bg.iKey = u.bg.pKey->u.i;
67542 }else{
67543 assert( pOp->opcode==OP_InsertInt );
67544 u.bg.iKey = pOp->p3;
67545 }
67546
67547 if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
67548 if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = u.bg.iKey;
67549 if( u.bg.pData->flags & MEM_Null ){
67550 u.bg.pData->z = 0;
67551 u.bg.pData->n = 0;
67552 }else{
67553 assert( u.bg.pData->flags & (MEM_Blob|MEM_Str) );
67554 }
67555 u.bg.seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bg.pC->seekResult : 0);
67556 if( u.bg.pData->flags & MEM_Zero ){
67557 u.bg.nZero = u.bg.pData->u.nZero;
67558 }else{
67559 u.bg.nZero = 0;
67560 }
67561 sqlite3BtreeSetCachedRowid(u.bg.pC->pCursor, 0);
67562 rc = sqlite3BtreeInsert(u.bg.pC->pCursor, 0, u.bg.iKey,
67563 u.bg.pData->z, u.bg.pData->n, u.bg.nZero,
67564 pOp->p5 & OPFLAG_APPEND, u.bg.seekResult
67565 );
67566 u.bg.pC->rowidIsValid = 0;
67567 u.bg.pC->deferredMoveto = 0;
67568 u.bg.pC->cacheStatus = CACHE_STALE;
67569
67570 /* Invoke the update-hook if required. */
67571 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67572 u.bg.zDb = db->aDb[u.bg.pC->iDb].zName;
67573 u.bg.zTbl = pOp->p4.z;
67574 u.bg.op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
67575 assert( u.bg.pC->isTable );
67576 db->xUpdateCallback(db->pUpdateArg, u.bg.op, u.bg.zDb, u.bg.zTbl, u.bg.iKey);
67577 assert( u.bg.pC->iDb>=0 );
67578 }
67579 break;
67580 }
67581
67582 /* Opcode: Delete P1 P2 * P4 *
@@ -66977,51 +67598,51 @@
67598 ** pointing to. The update hook will be invoked, if it exists.
67599 ** If P4 is not NULL then the P1 cursor must have been positioned
67600 ** using OP_NotFound prior to invoking this opcode.
67601 */
67602 case OP_Delete: {
67603 #if 0 /* local variables moved into u.bh */
67604 i64 iKey;
67605 VdbeCursor *pC;
67606 #endif /* local variables moved into u.bh */
67607
67608 u.bh.iKey = 0;
67609 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67610 u.bh.pC = p->apCsr[pOp->p1];
67611 assert( u.bh.pC!=0 );
67612 assert( u.bh.pC->pCursor!=0 ); /* Only valid for real tables, no pseudotables */
67613
67614 /* If the update-hook will be invoked, set u.bh.iKey to the rowid of the
67615 ** row being deleted.
67616 */
67617 if( db->xUpdateCallback && pOp->p4.z ){
67618 assert( u.bh.pC->isTable );
67619 assert( u.bh.pC->rowidIsValid ); /* lastRowid set by previous OP_NotFound */
67620 u.bh.iKey = u.bh.pC->lastRowid;
67621 }
67622
67623 /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
67624 ** OP_Column on the same table without any intervening operations that
67625 ** might move or invalidate the cursor. Hence cursor u.bh.pC is always pointing
67626 ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
67627 ** below is always a no-op and cannot fail. We will run it anyhow, though,
67628 ** to guard against future changes to the code generator.
67629 **/
67630 assert( u.bh.pC->deferredMoveto==0 );
67631 rc = sqlite3VdbeCursorMoveto(u.bh.pC);
67632 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67633
67634 sqlite3BtreeSetCachedRowid(u.bh.pC->pCursor, 0);
67635 rc = sqlite3BtreeDelete(u.bh.pC->pCursor);
67636 u.bh.pC->cacheStatus = CACHE_STALE;
67637
67638 /* Invoke the update-hook if required. */
67639 if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
67640 const char *zDb = db->aDb[u.bh.pC->iDb].zName;
67641 const char *zTbl = pOp->p4.z;
67642 db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE, zDb, zTbl, u.bh.iKey);
67643 assert( u.bh.pC->iDb>=0 );
67644 }
67645 if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
67646 break;
67647 }
67648 /* Opcode: ResetCount * * * * *
@@ -67034,10 +67655,53 @@
67655 case OP_ResetCount: {
67656 sqlite3VdbeSetChanges(db, p->nChange);
67657 p->nChange = 0;
67658 break;
67659 }
67660
67661 /* Opcode: SorterCompare P1 P2 P3
67662 **
67663 ** P1 is a sorter cursor. This instruction compares the record blob in
67664 ** register P3 with the entry that the sorter cursor currently points to.
67665 ** If, excluding the rowid fields at the end, the two records are a match,
67666 ** fall through to the next instruction. Otherwise, jump to instruction P2.
67667 */
67668 case OP_SorterCompare: {
67669 #if 0 /* local variables moved into u.bi */
67670 VdbeCursor *pC;
67671 int res;
67672 #endif /* local variables moved into u.bi */
67673
67674 u.bi.pC = p->apCsr[pOp->p1];
67675 assert( isSorter(u.bi.pC) );
67676 pIn3 = &aMem[pOp->p3];
67677 rc = sqlite3VdbeSorterCompare(u.bi.pC, pIn3, &u.bi.res);
67678 if( u.bi.res ){
67679 pc = pOp->p2-1;
67680 }
67681 break;
67682 };
67683
67684 /* Opcode: SorterData P1 P2 * * *
67685 **
67686 ** Write into register P2 the current sorter data for sorter cursor P1.
67687 */
67688 case OP_SorterData: {
67689 #if 0 /* local variables moved into u.bj */
67690 VdbeCursor *pC;
67691 #endif /* local variables moved into u.bj */
67692 #ifndef SQLITE_OMIT_MERGE_SORT
67693 pOut = &aMem[pOp->p2];
67694 u.bj.pC = p->apCsr[pOp->p1];
67695 assert( u.bj.pC->isSorter );
67696 rc = sqlite3VdbeSorterRowkey(u.bj.pC, pOut);
67697 #else
67698 pOp->opcode = OP_RowKey;
67699 pc--;
67700 #endif
67701 break;
67702 }
67703
67704 /* Opcode: RowData P1 P2 * * *
67705 **
67706 ** Write into register P2 the complete row data for cursor P1.
67707 ** There is no interpretation of the data.
@@ -67057,65 +67721,67 @@
67721 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
67722 ** of a real table, not a pseudo-table.
67723 */
67724 case OP_RowKey:
67725 case OP_RowData: {
67726 #if 0 /* local variables moved into u.bk */
67727 VdbeCursor *pC;
67728 BtCursor *pCrsr;
67729 u32 n;
67730 i64 n64;
67731 #endif /* local variables moved into u.bk */
67732
67733 pOut = &aMem[pOp->p2];
67734 memAboutToChange(p, pOut);
67735
67736 /* Note that RowKey and RowData are really exactly the same instruction */
67737 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67738 u.bk.pC = p->apCsr[pOp->p1];
67739 assert( u.bk.pC->isSorter==0 );
67740 assert( u.bk.pC->isTable || pOp->opcode!=OP_RowData );
67741 assert( u.bk.pC->isIndex || pOp->opcode==OP_RowData );
67742 assert( u.bk.pC!=0 );
67743 assert( u.bk.pC->nullRow==0 );
67744 assert( u.bk.pC->pseudoTableReg==0 );
67745 assert( !u.bk.pC->isSorter );
67746 assert( u.bk.pC->pCursor!=0 );
67747 u.bk.pCrsr = u.bk.pC->pCursor;
67748 assert( sqlite3BtreeCursorIsValid(u.bk.pCrsr) );
67749
67750 /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
67751 ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
67752 ** the cursor. Hence the following sqlite3VdbeCursorMoveto() call is always
67753 ** a no-op and can never fail. But we leave it in place as a safety.
67754 */
67755 assert( u.bk.pC->deferredMoveto==0 );
67756 rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67757 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67758
67759 if( u.bk.pC->isIndex ){
67760 assert( !u.bk.pC->isTable );
67761 rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67762 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67763 if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67764 goto too_big;
67765 }
67766 u.bk.n = (u32)u.bk.n64;
67767 }else{
67768 rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67769 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67770 if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67771 goto too_big;
67772 }
67773 }
67774 if( sqlite3VdbeMemGrow(pOut, u.bk.n, 0) ){
67775 goto no_mem;
67776 }
67777 pOut->n = u.bk.n;
67778 MemSetTypeFlag(pOut, MEM_Blob);
67779 if( u.bk.pC->isIndex ){
67780 rc = sqlite3BtreeKey(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67781 }else{
67782 rc = sqlite3BtreeData(u.bk.pCrsr, 0, u.bk.n, pOut->z);
67783 }
67784 pOut->enc = SQLITE_UTF8; /* In case the blob is ever cast to text */
67785 UPDATE_MAX_BLOBSIZE(pOut);
67786 break;
67787 }
@@ -67128,46 +67794,46 @@
67794 ** P1 can be either an ordinary table or a virtual table. There used to
67795 ** be a separate OP_VRowid opcode for use with virtual tables, but this
67796 ** one opcode now works for both table types.
67797 */
67798 case OP_Rowid: { /* out2-prerelease */
67799 #if 0 /* local variables moved into u.bl */
67800 VdbeCursor *pC;
67801 i64 v;
67802 sqlite3_vtab *pVtab;
67803 const sqlite3_module *pModule;
67804 #endif /* local variables moved into u.bl */
67805
67806 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67807 u.bl.pC = p->apCsr[pOp->p1];
67808 assert( u.bl.pC!=0 );
67809 assert( u.bl.pC->pseudoTableReg==0 );
67810 if( u.bl.pC->nullRow ){
67811 pOut->flags = MEM_Null;
67812 break;
67813 }else if( u.bl.pC->deferredMoveto ){
67814 u.bl.v = u.bl.pC->movetoTarget;
67815 #ifndef SQLITE_OMIT_VIRTUALTABLE
67816 }else if( u.bl.pC->pVtabCursor ){
67817 u.bl.pVtab = u.bl.pC->pVtabCursor->pVtab;
67818 u.bl.pModule = u.bl.pVtab->pModule;
67819 assert( u.bl.pModule->xRowid );
67820 rc = u.bl.pModule->xRowid(u.bl.pC->pVtabCursor, &u.bl.v);
67821 importVtabErrMsg(p, u.bl.pVtab);
67822 #endif /* SQLITE_OMIT_VIRTUALTABLE */
67823 }else{
67824 assert( u.bl.pC->pCursor!=0 );
67825 rc = sqlite3VdbeCursorMoveto(u.bl.pC);
67826 if( rc ) goto abort_due_to_error;
67827 if( u.bl.pC->rowidIsValid ){
67828 u.bl.v = u.bl.pC->lastRowid;
67829 }else{
67830 rc = sqlite3BtreeKeySize(u.bl.pC->pCursor, &u.bl.v);
67831 assert( rc==SQLITE_OK ); /* Always so because of CursorMoveto() above */
67832 }
67833 }
67834 pOut->u.i = u.bl.v;
67835 break;
67836 }
67837
67838 /* Opcode: NullRow P1 * * * *
67839 **
@@ -67174,21 +67840,22 @@
67840 ** Move the cursor P1 to a null row. Any OP_Column operations
67841 ** that occur while the cursor is on the null row will always
67842 ** write a NULL.
67843 */
67844 case OP_NullRow: {
67845 #if 0 /* local variables moved into u.bm */
67846 VdbeCursor *pC;
67847 #endif /* local variables moved into u.bm */
67848
67849 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67850 u.bm.pC = p->apCsr[pOp->p1];
67851 assert( u.bm.pC!=0 );
67852 u.bm.pC->nullRow = 1;
67853 u.bm.pC->rowidIsValid = 0;
67854 assert( u.bm.pC->pCursor || u.bm.pC->pVtabCursor );
67855 if( u.bm.pC->pCursor ){
67856 sqlite3BtreeClearCursor(u.bm.pC->pCursor);
67857 }
67858 break;
67859 }
67860
67861 /* Opcode: Last P1 P2 * * *
@@ -67198,30 +67865,30 @@
67865 ** If the table or index is empty and P2>0, then jump immediately to P2.
67866 ** If P2 is 0 or if the table or index is not empty, fall through
67867 ** to the following instruction.
67868 */
67869 case OP_Last: { /* jump */
67870 #if 0 /* local variables moved into u.bn */
67871 VdbeCursor *pC;
67872 BtCursor *pCrsr;
67873 int res;
67874 #endif /* local variables moved into u.bn */
67875
67876 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67877 u.bn.pC = p->apCsr[pOp->p1];
67878 assert( u.bn.pC!=0 );
67879 u.bn.pCrsr = u.bn.pC->pCursor;
67880 if( NEVER(u.bn.pCrsr==0) ){
67881 u.bn.res = 1;
67882 }else{
67883 rc = sqlite3BtreeLast(u.bn.pCrsr, &u.bn.res);
67884 }
67885 u.bn.pC->nullRow = (u8)u.bn.res;
67886 u.bn.pC->deferredMoveto = 0;
67887 u.bn.pC->rowidIsValid = 0;
67888 u.bn.pC->cacheStatus = CACHE_STALE;
67889 if( pOp->p2>0 && u.bn.res ){
67890 pc = pOp->p2 - 1;
67891 }
67892 break;
67893 }
67894
@@ -67236,10 +67903,14 @@
67903 ** end. We use the OP_Sort opcode instead of OP_Rewind to do the
67904 ** rewinding so that the global variable will be incremented and
67905 ** regression tests can determine whether or not the optimizer is
67906 ** correctly optimizing out sorts.
67907 */
67908 case OP_SorterSort: /* jump */
67909 #ifdef SQLITE_OMIT_MERGE_SORT
67910 pOp->opcode = OP_Sort;
67911 #endif
67912 case OP_Sort: { /* jump */
67913 #ifdef SQLITE_TEST
67914 sqlite3_sort_count++;
67915 sqlite3_search_count--;
67916 #endif
@@ -67253,43 +67924,51 @@
67924 ** If the table or index is empty and P2>0, then jump immediately to P2.
67925 ** If P2 is 0 or if the table or index is not empty, fall through
67926 ** to the following instruction.
67927 */
67928 case OP_Rewind: { /* jump */
67929 #if 0 /* local variables moved into u.bo */
67930 VdbeCursor *pC;
67931 BtCursor *pCrsr;
67932 int res;
67933 #endif /* local variables moved into u.bo */
67934
67935 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
67936 u.bo.pC = p->apCsr[pOp->p1];
67937 assert( u.bo.pC!=0 );
67938 assert( u.bo.pC->isSorter==(pOp->opcode==OP_SorterSort) );
67939 u.bo.res = 1;
67940 if( isSorter(u.bo.pC) ){
67941 rc = sqlite3VdbeSorterRewind(db, u.bo.pC, &u.bo.res);
67942 }else{
67943 u.bo.pCrsr = u.bo.pC->pCursor;
67944 assert( u.bo.pCrsr );
67945 rc = sqlite3BtreeFirst(u.bo.pCrsr, &u.bo.res);
67946 u.bo.pC->atFirst = u.bo.res==0 ?1:0;
67947 u.bo.pC->deferredMoveto = 0;
67948 u.bo.pC->cacheStatus = CACHE_STALE;
67949 u.bo.pC->rowidIsValid = 0;
67950 }
67951 u.bo.pC->nullRow = (u8)u.bo.res;
67952 assert( pOp->p2>0 && pOp->p2<p->nOp );
67953 if( u.bo.res ){
67954 pc = pOp->p2 - 1;
67955 }
67956 break;
67957 }
67958
67959 /* Opcode: Next P1 P2 * P4 P5
67960 **
67961 ** Advance cursor P1 so that it points to the next key/data pair in its
67962 ** table or index. If there are no more key/value pairs then fall through
67963 ** to the following instruction. But if the cursor advance was successful,
67964 ** jump immediately to P2.
67965 **
67966 ** The P1 cursor must be for a real table, not a pseudo-table.
67967 **
67968 ** P4 is always of type P4_ADVANCE. The function pointer points to
67969 ** sqlite3BtreeNext().
67970 **
67971 ** If P5 is positive and the jump is taken, then event counter
67972 ** number P5-1 in the prepared statement is incremented.
67973 **
67974 ** See also: Prev
@@ -67300,48 +67979,57 @@
67979 ** table or index. If there is no previous key/value pairs then fall through
67980 ** to the following instruction. But if the cursor backup was successful,
67981 ** jump immediately to P2.
67982 **
67983 ** The P1 cursor must be for a real table, not a pseudo-table.
67984 **
67985 ** P4 is always of type P4_ADVANCE. The function pointer points to
67986 ** sqlite3BtreePrevious().
67987 **
67988 ** If P5 is positive and the jump is taken, then event counter
67989 ** number P5-1 in the prepared statement is incremented.
67990 */
67991 case OP_SorterNext: /* jump */
67992 #ifdef SQLITE_OMIT_MERGE_SORT
67993 pOp->opcode = OP_Next;
67994 #endif
67995 case OP_Prev: /* jump */
67996 case OP_Next: { /* jump */
67997 #if 0 /* local variables moved into u.bp */
67998 VdbeCursor *pC;
 
67999 int res;
68000 #endif /* local variables moved into u.bp */
68001
68002 CHECK_FOR_INTERRUPT;
68003 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68004 assert( pOp->p5<=ArraySize(p->aCounter) );
68005 u.bp.pC = p->apCsr[pOp->p1];
68006 if( u.bp.pC==0 ){
68007 break; /* See ticket #2273 */
68008 }
68009 assert( u.bp.pC->isSorter==(pOp->opcode==OP_SorterNext) );
68010 if( isSorter(u.bp.pC) ){
68011 assert( pOp->opcode==OP_SorterNext );
68012 rc = sqlite3VdbeSorterNext(db, u.bp.pC, &u.bp.res);
68013 }else{
68014 u.bp.res = 1;
68015 assert( u.bp.pC->deferredMoveto==0 );
68016 assert( u.bp.pC->pCursor );
68017 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
68018 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
68019 rc = pOp->p4.xAdvance(u.bp.pC->pCursor, &u.bp.res);
68020 }
68021 u.bp.pC->nullRow = (u8)u.bp.res;
68022 u.bp.pC->cacheStatus = CACHE_STALE;
68023 if( u.bp.res==0 ){
68024 pc = pOp->p2 - 1;
68025 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
68026 #ifdef SQLITE_TEST
68027 sqlite3_search_count++;
68028 #endif
68029 }
68030 u.bp.pC->rowidIsValid = 0;
68031 break;
68032 }
68033
68034 /* Opcode: IdxInsert P1 P2 P3 * P5
68035 **
@@ -67353,35 +68041,44 @@
68041 ** insert is likely to be an append.
68042 **
68043 ** This instruction only works for indices. The equivalent instruction
68044 ** for tables is OP_Insert.
68045 */
68046 case OP_SorterInsert: /* in2 */
68047 #ifdef SQLITE_OMIT_MERGE_SORT
68048 pOp->opcode = OP_IdxInsert;
68049 #endif
68050 case OP_IdxInsert: { /* in2 */
68051 #if 0 /* local variables moved into u.bq */
68052 VdbeCursor *pC;
68053 BtCursor *pCrsr;
68054 int nKey;
68055 const char *zKey;
68056 #endif /* local variables moved into u.bq */
68057
68058 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68059 u.bq.pC = p->apCsr[pOp->p1];
68060 assert( u.bq.pC!=0 );
68061 assert( u.bq.pC->isSorter==(pOp->opcode==OP_SorterInsert) );
68062 pIn2 = &aMem[pOp->p2];
68063 assert( pIn2->flags & MEM_Blob );
68064 u.bq.pCrsr = u.bq.pC->pCursor;
68065 if( ALWAYS(u.bq.pCrsr!=0) ){
68066 assert( u.bq.pC->isTable==0 );
68067 rc = ExpandBlob(pIn2);
68068 if( rc==SQLITE_OK ){
68069 if( isSorter(u.bq.pC) ){
68070 rc = sqlite3VdbeSorterWrite(db, u.bq.pC, pIn2);
68071 }else{
68072 u.bq.nKey = pIn2->n;
68073 u.bq.zKey = pIn2->z;
68074 rc = sqlite3BtreeInsert(u.bq.pCrsr, u.bq.zKey, u.bq.nKey, "", 0, 0, pOp->p3,
68075 ((pOp->p5 & OPFLAG_USESEEKRESULT) ? u.bq.pC->seekResult : 0)
68076 );
68077 assert( u.bq.pC->deferredMoveto==0 );
68078 u.bq.pC->cacheStatus = CACHE_STALE;
68079 }
68080 }
68081 }
68082 break;
68083 }
68084
@@ -67390,37 +68087,37 @@
68087 ** The content of P3 registers starting at register P2 form
68088 ** an unpacked index key. This opcode removes that entry from the
68089 ** index opened by cursor P1.
68090 */
68091 case OP_IdxDelete: {
68092 #if 0 /* local variables moved into u.br */
68093 VdbeCursor *pC;
68094 BtCursor *pCrsr;
68095 int res;
68096 UnpackedRecord r;
68097 #endif /* local variables moved into u.br */
68098
68099 assert( pOp->p3>0 );
68100 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
68101 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68102 u.br.pC = p->apCsr[pOp->p1];
68103 assert( u.br.pC!=0 );
68104 u.br.pCrsr = u.br.pC->pCursor;
68105 if( ALWAYS(u.br.pCrsr!=0) ){
68106 u.br.r.pKeyInfo = u.br.pC->pKeyInfo;
68107 u.br.r.nField = (u16)pOp->p3;
68108 u.br.r.flags = 0;
68109 u.br.r.aMem = &aMem[pOp->p2];
68110 #ifdef SQLITE_DEBUG
68111 { int i; for(i=0; i<u.br.r.nField; i++) assert( memIsValid(&u.br.r.aMem[i]) ); }
68112 #endif
68113 rc = sqlite3BtreeMovetoUnpacked(u.br.pCrsr, &u.br.r, 0, 0, &u.br.res);
68114 if( rc==SQLITE_OK && u.br.res==0 ){
68115 rc = sqlite3BtreeDelete(u.br.pCrsr);
68116 }
68117 assert( u.br.pC->deferredMoveto==0 );
68118 u.br.pC->cacheStatus = CACHE_STALE;
68119 }
68120 break;
68121 }
68122
68123 /* Opcode: IdxRowid P1 P2 * * *
@@ -67430,32 +68127,32 @@
68127 ** the rowid of the table entry to which this index entry points.
68128 **
68129 ** See also: Rowid, MakeRecord.
68130 */
68131 case OP_IdxRowid: { /* out2-prerelease */
68132 #if 0 /* local variables moved into u.bs */
68133 BtCursor *pCrsr;
68134 VdbeCursor *pC;
68135 i64 rowid;
68136 #endif /* local variables moved into u.bs */
68137
68138 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68139 u.bs.pC = p->apCsr[pOp->p1];
68140 assert( u.bs.pC!=0 );
68141 u.bs.pCrsr = u.bs.pC->pCursor;
68142 pOut->flags = MEM_Null;
68143 if( ALWAYS(u.bs.pCrsr!=0) ){
68144 rc = sqlite3VdbeCursorMoveto(u.bs.pC);
68145 if( NEVER(rc) ) goto abort_due_to_error;
68146 assert( u.bs.pC->deferredMoveto==0 );
68147 assert( u.bs.pC->isTable==0 );
68148 if( !u.bs.pC->nullRow ){
68149 rc = sqlite3VdbeIdxRowid(db, u.bs.pCrsr, &u.bs.rowid);
68150 if( rc!=SQLITE_OK ){
68151 goto abort_due_to_error;
68152 }
68153 pOut->u.i = u.bs.rowid;
68154 pOut->flags = MEM_Int;
68155 }
68156 }
68157 break;
68158 }
@@ -67486,43 +68183,43 @@
68183 ** If P5 is non-zero then the key value is increased by an epsilon prior
68184 ** to the comparison. This makes the opcode work like IdxLE.
68185 */
68186 case OP_IdxLT: /* jump */
68187 case OP_IdxGE: { /* jump */
68188 #if 0 /* local variables moved into u.bt */
68189 VdbeCursor *pC;
68190 int res;
68191 UnpackedRecord r;
68192 #endif /* local variables moved into u.bt */
68193
68194 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68195 u.bt.pC = p->apCsr[pOp->p1];
68196 assert( u.bt.pC!=0 );
68197 assert( u.bt.pC->isOrdered );
68198 if( ALWAYS(u.bt.pC->pCursor!=0) ){
68199 assert( u.bt.pC->deferredMoveto==0 );
68200 assert( pOp->p5==0 || pOp->p5==1 );
68201 assert( pOp->p4type==P4_INT32 );
68202 u.bt.r.pKeyInfo = u.bt.pC->pKeyInfo;
68203 u.bt.r.nField = (u16)pOp->p4.i;
68204 if( pOp->p5 ){
68205 u.bt.r.flags = UNPACKED_INCRKEY | UNPACKED_IGNORE_ROWID;
68206 }else{
68207 u.bt.r.flags = UNPACKED_IGNORE_ROWID;
68208 }
68209 u.bt.r.aMem = &aMem[pOp->p3];
68210 #ifdef SQLITE_DEBUG
68211 { int i; for(i=0; i<u.bt.r.nField; i++) assert( memIsValid(&u.bt.r.aMem[i]) ); }
68212 #endif
68213 rc = sqlite3VdbeIdxKeyCompare(u.bt.pC, &u.bt.r, &u.bt.res);
68214 if( pOp->opcode==OP_IdxLT ){
68215 u.bt.res = -u.bt.res;
68216 }else{
68217 assert( pOp->opcode==OP_IdxGE );
68218 u.bt.res++;
68219 }
68220 if( u.bt.res>0 ){
68221 pc = pOp->p2 - 1 ;
68222 }
68223 }
68224 break;
68225 }
@@ -67546,43 +68243,43 @@
68243 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
68244 **
68245 ** See also: Clear
68246 */
68247 case OP_Destroy: { /* out2-prerelease */
68248 #if 0 /* local variables moved into u.bu */
68249 int iMoved;
68250 int iCnt;
68251 Vdbe *pVdbe;
68252 int iDb;
68253 #endif /* local variables moved into u.bu */
68254 #ifndef SQLITE_OMIT_VIRTUALTABLE
68255 u.bu.iCnt = 0;
68256 for(u.bu.pVdbe=db->pVdbe; u.bu.pVdbe; u.bu.pVdbe = u.bu.pVdbe->pNext){
68257 if( u.bu.pVdbe->magic==VDBE_MAGIC_RUN && u.bu.pVdbe->inVtabMethod<2 && u.bu.pVdbe->pc>=0 ){
68258 u.bu.iCnt++;
68259 }
68260 }
68261 #else
68262 u.bu.iCnt = db->activeVdbeCnt;
68263 #endif
68264 pOut->flags = MEM_Null;
68265 if( u.bu.iCnt>1 ){
68266 rc = SQLITE_LOCKED;
68267 p->errorAction = OE_Abort;
68268 }else{
68269 u.bu.iDb = pOp->p3;
68270 assert( u.bu.iCnt==1 );
68271 assert( (p->btreeMask & (((yDbMask)1)<<u.bu.iDb))!=0 );
68272 rc = sqlite3BtreeDropTable(db->aDb[u.bu.iDb].pBt, pOp->p1, &u.bu.iMoved);
68273 pOut->flags = MEM_Int;
68274 pOut->u.i = u.bu.iMoved;
68275 #ifndef SQLITE_OMIT_AUTOVACUUM
68276 if( rc==SQLITE_OK && u.bu.iMoved!=0 ){
68277 sqlite3RootPageMoved(db, u.bu.iDb, u.bu.iMoved, pOp->p1);
68278 /* All OP_Destroy operations occur on the same btree */
68279 assert( resetSchemaOnFault==0 || resetSchemaOnFault==u.bu.iDb+1 );
68280 resetSchemaOnFault = u.bu.iDb+1;
68281 }
68282 #endif
68283 }
68284 break;
68285 }
@@ -67604,25 +68301,25 @@
68301 ** also incremented by the number of rows in the table being cleared.
68302 **
68303 ** See also: Destroy
68304 */
68305 case OP_Clear: {
68306 #if 0 /* local variables moved into u.bv */
68307 int nChange;
68308 #endif /* local variables moved into u.bv */
68309
68310 u.bv.nChange = 0;
68311 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
68312 rc = sqlite3BtreeClearTable(
68313 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bv.nChange : 0)
68314 );
68315 if( pOp->p3 ){
68316 p->nChange += u.bv.nChange;
68317 if( pOp->p3>0 ){
68318 assert( memIsValid(&aMem[pOp->p3]) );
68319 memAboutToChange(p, &aMem[pOp->p3]);
68320 aMem[pOp->p3].u.i += u.bv.nChange;
68321 }
68322 }
68323 break;
68324 }
68325
@@ -67648,29 +68345,29 @@
68345 **
68346 ** See documentation on OP_CreateTable for additional information.
68347 */
68348 case OP_CreateIndex: /* out2-prerelease */
68349 case OP_CreateTable: { /* out2-prerelease */
68350 #if 0 /* local variables moved into u.bw */
68351 int pgno;
68352 int flags;
68353 Db *pDb;
68354 #endif /* local variables moved into u.bw */
68355
68356 u.bw.pgno = 0;
68357 assert( pOp->p1>=0 && pOp->p1<db->nDb );
68358 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
68359 u.bw.pDb = &db->aDb[pOp->p1];
68360 assert( u.bw.pDb->pBt!=0 );
68361 if( pOp->opcode==OP_CreateTable ){
68362 /* u.bw.flags = BTREE_INTKEY; */
68363 u.bw.flags = BTREE_INTKEY;
68364 }else{
68365 u.bw.flags = BTREE_BLOBKEY;
68366 }
68367 rc = sqlite3BtreeCreateTable(u.bw.pDb->pBt, &u.bw.pgno, u.bw.flags);
68368 pOut->u.i = u.bw.pgno;
68369 break;
68370 }
68371
68372 /* Opcode: ParseSchema P1 * * P4 *
68373 **
@@ -67679,48 +68376,48 @@
68376 **
68377 ** This opcode invokes the parser to create a new virtual machine,
68378 ** then runs the new virtual machine. It is thus a re-entrant opcode.
68379 */
68380 case OP_ParseSchema: {
68381 #if 0 /* local variables moved into u.bx */
68382 int iDb;
68383 const char *zMaster;
68384 char *zSql;
68385 InitData initData;
68386 #endif /* local variables moved into u.bx */
68387
68388 /* Any prepared statement that invokes this opcode will hold mutexes
68389 ** on every btree. This is a prerequisite for invoking
68390 ** sqlite3InitCallback().
68391 */
68392 #ifdef SQLITE_DEBUG
68393 for(u.bx.iDb=0; u.bx.iDb<db->nDb; u.bx.iDb++){
68394 assert( u.bx.iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[u.bx.iDb].pBt) );
68395 }
68396 #endif
68397
68398 u.bx.iDb = pOp->p1;
68399 assert( u.bx.iDb>=0 && u.bx.iDb<db->nDb );
68400 assert( DbHasProperty(db, u.bx.iDb, DB_SchemaLoaded) );
68401 /* Used to be a conditional */ {
68402 u.bx.zMaster = SCHEMA_TABLE(u.bx.iDb);
68403 u.bx.initData.db = db;
68404 u.bx.initData.iDb = pOp->p1;
68405 u.bx.initData.pzErrMsg = &p->zErrMsg;
68406 u.bx.zSql = sqlite3MPrintf(db,
68407 "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
68408 db->aDb[u.bx.iDb].zName, u.bx.zMaster, pOp->p4.z);
68409 if( u.bx.zSql==0 ){
68410 rc = SQLITE_NOMEM;
68411 }else{
68412 assert( db->init.busy==0 );
68413 db->init.busy = 1;
68414 u.bx.initData.rc = SQLITE_OK;
68415 assert( !db->mallocFailed );
68416 rc = sqlite3_exec(db, u.bx.zSql, sqlite3InitCallback, &u.bx.initData, 0);
68417 if( rc==SQLITE_OK ) rc = u.bx.initData.rc;
68418 sqlite3DbFree(db, u.bx.zSql);
68419 db->init.busy = 0;
68420 }
68421 }
68422 if( rc==SQLITE_NOMEM ){
68423 goto no_mem;
@@ -67799,45 +68496,45 @@
68496 ** file, not the main database file.
68497 **
68498 ** This opcode is used to implement the integrity_check pragma.
68499 */
68500 case OP_IntegrityCk: {
68501 #if 0 /* local variables moved into u.by */
68502 int nRoot; /* Number of tables to check. (Number of root pages.) */
68503 int *aRoot; /* Array of rootpage numbers for tables to be checked */
68504 int j; /* Loop counter */
68505 int nErr; /* Number of errors reported */
68506 char *z; /* Text of the error report */
68507 Mem *pnErr; /* Register keeping track of errors remaining */
68508 #endif /* local variables moved into u.by */
68509
68510 u.by.nRoot = pOp->p2;
68511 assert( u.by.nRoot>0 );
68512 u.by.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.by.nRoot+1) );
68513 if( u.by.aRoot==0 ) goto no_mem;
68514 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68515 u.by.pnErr = &aMem[pOp->p3];
68516 assert( (u.by.pnErr->flags & MEM_Int)!=0 );
68517 assert( (u.by.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
68518 pIn1 = &aMem[pOp->p1];
68519 for(u.by.j=0; u.by.j<u.by.nRoot; u.by.j++){
68520 u.by.aRoot[u.by.j] = (int)sqlite3VdbeIntValue(&pIn1[u.by.j]);
68521 }
68522 u.by.aRoot[u.by.j] = 0;
68523 assert( pOp->p5<db->nDb );
68524 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
68525 u.by.z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, u.by.aRoot, u.by.nRoot,
68526 (int)u.by.pnErr->u.i, &u.by.nErr);
68527 sqlite3DbFree(db, u.by.aRoot);
68528 u.by.pnErr->u.i -= u.by.nErr;
68529 sqlite3VdbeMemSetNull(pIn1);
68530 if( u.by.nErr==0 ){
68531 assert( u.by.z==0 );
68532 }else if( u.by.z==0 ){
68533 goto no_mem;
68534 }else{
68535 sqlite3VdbeMemSetStr(pIn1, u.by.z, -1, SQLITE_UTF8, sqlite3_free);
68536 }
68537 UPDATE_MAX_BLOBSIZE(pIn1);
68538 sqlite3VdbeChangeEncoding(pIn1, encoding);
68539 break;
68540 }
@@ -67867,24 +68564,24 @@
68564 ** Extract the smallest value from boolean index P1 and put that value into
68565 ** register P3. Or, if boolean index P1 is initially empty, leave P3
68566 ** unchanged and jump to instruction P2.
68567 */
68568 case OP_RowSetRead: { /* jump, in1, out3 */
68569 #if 0 /* local variables moved into u.bz */
68570 i64 val;
68571 #endif /* local variables moved into u.bz */
68572 CHECK_FOR_INTERRUPT;
68573 pIn1 = &aMem[pOp->p1];
68574 if( (pIn1->flags & MEM_RowSet)==0
68575 || sqlite3RowSetNext(pIn1->u.pRowSet, &u.bz.val)==0
68576 ){
68577 /* The boolean index is empty */
68578 sqlite3VdbeMemSetNull(pIn1);
68579 pc = pOp->p2 - 1;
68580 }else{
68581 /* A value was pulled from the index */
68582 sqlite3VdbeMemSetInt64(&aMem[pOp->p3], u.bz.val);
68583 }
68584 break;
68585 }
68586
68587 /* Opcode: RowSetTest P1 P2 P3 P4
@@ -67909,18 +68606,18 @@
68606 ** inserted, there is no need to search to see if the same value was
68607 ** previously inserted as part of set X (only if it was previously
68608 ** inserted as part of some other set).
68609 */
68610 case OP_RowSetTest: { /* jump, in1, in3 */
68611 #if 0 /* local variables moved into u.ca */
68612 int iSet;
68613 int exists;
68614 #endif /* local variables moved into u.ca */
68615
68616 pIn1 = &aMem[pOp->p1];
68617 pIn3 = &aMem[pOp->p3];
68618 u.ca.iSet = pOp->p4.i;
68619 assert( pIn3->flags&MEM_Int );
68620
68621 /* If there is anything other than a rowset object in memory cell P1,
68622 ** delete it now and initialize P1 with an empty rowset
68623 */
@@ -67928,21 +68625,21 @@
68625 sqlite3VdbeMemSetRowSet(pIn1);
68626 if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
68627 }
68628
68629 assert( pOp->p4type==P4_INT32 );
68630 assert( u.ca.iSet==-1 || u.ca.iSet>=0 );
68631 if( u.ca.iSet ){
68632 u.ca.exists = sqlite3RowSetTest(pIn1->u.pRowSet,
68633 (u8)(u.ca.iSet>=0 ? u.ca.iSet & 0xf : 0xff),
68634 pIn3->u.i);
68635 if( u.ca.exists ){
68636 pc = pOp->p2 - 1;
68637 break;
68638 }
68639 }
68640 if( u.ca.iSet>=0 ){
68641 sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
68642 }
68643 break;
68644 }
68645
@@ -67961,25 +68658,25 @@
68658 ** memory required by the sub-vdbe at runtime.
68659 **
68660 ** P4 is a pointer to the VM containing the trigger program.
68661 */
68662 case OP_Program: { /* jump */
68663 #if 0 /* local variables moved into u.cb */
68664 int nMem; /* Number of memory registers for sub-program */
68665 int nByte; /* Bytes of runtime space required for sub-program */
68666 Mem *pRt; /* Register to allocate runtime space */
68667 Mem *pMem; /* Used to iterate through memory cells */
68668 Mem *pEnd; /* Last memory cell in new array */
68669 VdbeFrame *pFrame; /* New vdbe frame to execute in */
68670 SubProgram *pProgram; /* Sub-program to execute */
68671 void *t; /* Token identifying trigger */
68672 #endif /* local variables moved into u.cb */
68673
68674 u.cb.pProgram = pOp->p4.pProgram;
68675 u.cb.pRt = &aMem[pOp->p3];
68676 assert( memIsValid(u.cb.pRt) );
68677 assert( u.cb.pProgram->nOp>0 );
68678
68679 /* If the p5 flag is clear, then recursive invocation of triggers is
68680 ** disabled for backwards compatibility (p5 is set if this sub-program
68681 ** is really a trigger, not a foreign key action, and the flag set
68682 ** and cleared by the "PRAGMA recursive_triggers" command is clear).
@@ -67989,79 +68686,79 @@
68686 ** SubProgram (if the trigger may be executed with more than one different
68687 ** ON CONFLICT algorithm). SubProgram structures associated with a
68688 ** single trigger all have the same value for the SubProgram.token
68689 ** variable. */
68690 if( pOp->p5 ){
68691 u.cb.t = u.cb.pProgram->token;
68692 for(u.cb.pFrame=p->pFrame; u.cb.pFrame && u.cb.pFrame->token!=u.cb.t; u.cb.pFrame=u.cb.pFrame->pParent);
68693 if( u.cb.pFrame ) break;
68694 }
68695
68696 if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
68697 rc = SQLITE_ERROR;
68698 sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
68699 break;
68700 }
68701
68702 /* Register u.cb.pRt is used to store the memory required to save the state
68703 ** of the current program, and the memory required at runtime to execute
68704 ** the trigger program. If this trigger has been fired before, then u.cb.pRt
68705 ** is already allocated. Otherwise, it must be initialized. */
68706 if( (u.cb.pRt->flags&MEM_Frame)==0 ){
68707 /* SubProgram.nMem is set to the number of memory cells used by the
68708 ** program stored in SubProgram.aOp. As well as these, one memory
68709 ** cell is required for each cursor used by the program. Set local
68710 ** variable u.cb.nMem (and later, VdbeFrame.nChildMem) to this value.
68711 */
68712 u.cb.nMem = u.cb.pProgram->nMem + u.cb.pProgram->nCsr;
68713 u.cb.nByte = ROUND8(sizeof(VdbeFrame))
68714 + u.cb.nMem * sizeof(Mem)
68715 + u.cb.pProgram->nCsr * sizeof(VdbeCursor *);
68716 u.cb.pFrame = sqlite3DbMallocZero(db, u.cb.nByte);
68717 if( !u.cb.pFrame ){
68718 goto no_mem;
68719 }
68720 sqlite3VdbeMemRelease(u.cb.pRt);
68721 u.cb.pRt->flags = MEM_Frame;
68722 u.cb.pRt->u.pFrame = u.cb.pFrame;
68723
68724 u.cb.pFrame->v = p;
68725 u.cb.pFrame->nChildMem = u.cb.nMem;
68726 u.cb.pFrame->nChildCsr = u.cb.pProgram->nCsr;
68727 u.cb.pFrame->pc = pc;
68728 u.cb.pFrame->aMem = p->aMem;
68729 u.cb.pFrame->nMem = p->nMem;
68730 u.cb.pFrame->apCsr = p->apCsr;
68731 u.cb.pFrame->nCursor = p->nCursor;
68732 u.cb.pFrame->aOp = p->aOp;
68733 u.cb.pFrame->nOp = p->nOp;
68734 u.cb.pFrame->token = u.cb.pProgram->token;
68735
68736 u.cb.pEnd = &VdbeFrameMem(u.cb.pFrame)[u.cb.pFrame->nChildMem];
68737 for(u.cb.pMem=VdbeFrameMem(u.cb.pFrame); u.cb.pMem!=u.cb.pEnd; u.cb.pMem++){
68738 u.cb.pMem->flags = MEM_Null;
68739 u.cb.pMem->db = db;
68740 }
68741 }else{
68742 u.cb.pFrame = u.cb.pRt->u.pFrame;
68743 assert( u.cb.pProgram->nMem+u.cb.pProgram->nCsr==u.cb.pFrame->nChildMem );
68744 assert( u.cb.pProgram->nCsr==u.cb.pFrame->nChildCsr );
68745 assert( pc==u.cb.pFrame->pc );
68746 }
68747
68748 p->nFrame++;
68749 u.cb.pFrame->pParent = p->pFrame;
68750 u.cb.pFrame->lastRowid = lastRowid;
68751 u.cb.pFrame->nChange = p->nChange;
68752 p->nChange = 0;
68753 p->pFrame = u.cb.pFrame;
68754 p->aMem = aMem = &VdbeFrameMem(u.cb.pFrame)[-1];
68755 p->nMem = u.cb.pFrame->nChildMem;
68756 p->nCursor = (u16)u.cb.pFrame->nChildCsr;
68757 p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
68758 p->aOp = aOp = u.cb.pProgram->aOp;
68759 p->nOp = u.cb.pProgram->nOp;
68760 pc = -1;
68761
68762 break;
68763 }
68764
@@ -68076,17 +68773,17 @@
68773 ** The address of the cell in the parent frame is determined by adding
68774 ** the value of the P1 argument to the value of the P1 argument to the
68775 ** calling OP_Program instruction.
68776 */
68777 case OP_Param: { /* out2-prerelease */
68778 #if 0 /* local variables moved into u.cc */
68779 VdbeFrame *pFrame;
68780 Mem *pIn;
68781 #endif /* local variables moved into u.cc */
68782 u.cc.pFrame = p->pFrame;
68783 u.cc.pIn = &u.cc.pFrame->aMem[pOp->p1 + u.cc.pFrame->aOp[u.cc.pFrame->pc].p1];
68784 sqlite3VdbeMemShallowCopy(pOut, u.cc.pIn, MEM_Ephem);
68785 break;
68786 }
68787
68788 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
68789
@@ -68138,26 +68835,26 @@
68835 **
68836 ** This instruction throws an error if the memory cell is not initially
68837 ** an integer.
68838 */
68839 case OP_MemMax: { /* in2 */
68840 #if 0 /* local variables moved into u.cd */
68841 Mem *pIn1;
68842 VdbeFrame *pFrame;
68843 #endif /* local variables moved into u.cd */
68844 if( p->pFrame ){
68845 for(u.cd.pFrame=p->pFrame; u.cd.pFrame->pParent; u.cd.pFrame=u.cd.pFrame->pParent);
68846 u.cd.pIn1 = &u.cd.pFrame->aMem[pOp->p1];
68847 }else{
68848 u.cd.pIn1 = &aMem[pOp->p1];
68849 }
68850 assert( memIsValid(u.cd.pIn1) );
68851 sqlite3VdbeMemIntegerify(u.cd.pIn1);
68852 pIn2 = &aMem[pOp->p2];
68853 sqlite3VdbeMemIntegerify(pIn2);
68854 if( u.cd.pIn1->u.i<pIn2->u.i){
68855 u.cd.pIn1->u.i = pIn2->u.i;
68856 }
68857 break;
68858 }
68859 #endif /* SQLITE_OMIT_AUTOINCREMENT */
68860
@@ -68220,54 +68917,54 @@
68917 **
68918 ** The P5 arguments are taken from register P2 and its
68919 ** successors.
68920 */
68921 case OP_AggStep: {
68922 #if 0 /* local variables moved into u.ce */
68923 int n;
68924 int i;
68925 Mem *pMem;
68926 Mem *pRec;
68927 sqlite3_context ctx;
68928 sqlite3_value **apVal;
68929 #endif /* local variables moved into u.ce */
68930
68931 u.ce.n = pOp->p5;
68932 assert( u.ce.n>=0 );
68933 u.ce.pRec = &aMem[pOp->p2];
68934 u.ce.apVal = p->apArg;
68935 assert( u.ce.apVal || u.ce.n==0 );
68936 for(u.ce.i=0; u.ce.i<u.ce.n; u.ce.i++, u.ce.pRec++){
68937 assert( memIsValid(u.ce.pRec) );
68938 u.ce.apVal[u.ce.i] = u.ce.pRec;
68939 memAboutToChange(p, u.ce.pRec);
68940 sqlite3VdbeMemStoreType(u.ce.pRec);
68941 }
68942 u.ce.ctx.pFunc = pOp->p4.pFunc;
68943 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68944 u.ce.ctx.pMem = u.ce.pMem = &aMem[pOp->p3];
68945 u.ce.pMem->n++;
68946 u.ce.ctx.s.flags = MEM_Null;
68947 u.ce.ctx.s.z = 0;
68948 u.ce.ctx.s.zMalloc = 0;
68949 u.ce.ctx.s.xDel = 0;
68950 u.ce.ctx.s.db = db;
68951 u.ce.ctx.isError = 0;
68952 u.ce.ctx.pColl = 0;
68953 if( u.ce.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
68954 assert( pOp>p->aOp );
68955 assert( pOp[-1].p4type==P4_COLLSEQ );
68956 assert( pOp[-1].opcode==OP_CollSeq );
68957 u.ce.ctx.pColl = pOp[-1].p4.pColl;
68958 }
68959 (u.ce.ctx.pFunc->xStep)(&u.ce.ctx, u.ce.n, u.ce.apVal); /* IMP: R-24505-23230 */
68960 if( u.ce.ctx.isError ){
68961 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ce.ctx.s));
68962 rc = u.ce.ctx.isError;
68963 }
68964
68965 sqlite3VdbeMemRelease(&u.ce.ctx.s);
68966
68967 break;
68968 }
68969
68970 /* Opcode: AggFinal P1 P2 * P4 *
@@ -68281,23 +68978,23 @@
68978 ** functions that can take varying numbers of arguments. The
68979 ** P4 argument is only needed for the degenerate case where
68980 ** the step function was not previously called.
68981 */
68982 case OP_AggFinal: {
68983 #if 0 /* local variables moved into u.cf */
68984 Mem *pMem;
68985 #endif /* local variables moved into u.cf */
68986 assert( pOp->p1>0 && pOp->p1<=p->nMem );
68987 u.cf.pMem = &aMem[pOp->p1];
68988 assert( (u.cf.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
68989 rc = sqlite3VdbeMemFinalize(u.cf.pMem, pOp->p4.pFunc);
68990 if( rc ){
68991 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.cf.pMem));
68992 }
68993 sqlite3VdbeChangeEncoding(u.cf.pMem, encoding);
68994 UPDATE_MAX_BLOBSIZE(u.cf.pMem);
68995 if( sqlite3VdbeMemTooBig(u.cf.pMem) ){
68996 goto too_big;
68997 }
68998 break;
68999 }
69000
@@ -68312,29 +69009,29 @@
69009 ** in the WAL that have been checkpointed after the checkpoint
69010 ** completes into mem[P3+2]. However on an error, mem[P3+1] and
69011 ** mem[P3+2] are initialized to -1.
69012 */
69013 case OP_Checkpoint: {
69014 #if 0 /* local variables moved into u.cg */
69015 int i; /* Loop counter */
69016 int aRes[3]; /* Results */
69017 Mem *pMem; /* Write results here */
69018 #endif /* local variables moved into u.cg */
69019
69020 u.cg.aRes[0] = 0;
69021 u.cg.aRes[1] = u.cg.aRes[2] = -1;
69022 assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
69023 || pOp->p2==SQLITE_CHECKPOINT_FULL
69024 || pOp->p2==SQLITE_CHECKPOINT_RESTART
69025 );
69026 rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &u.cg.aRes[1], &u.cg.aRes[2]);
69027 if( rc==SQLITE_BUSY ){
69028 rc = SQLITE_OK;
69029 u.cg.aRes[0] = 1;
69030 }
69031 for(u.cg.i=0, u.cg.pMem = &aMem[pOp->p3]; u.cg.i<3; u.cg.i++, u.cg.pMem++){
69032 sqlite3VdbeMemSetInt64(u.cg.pMem, (i64)u.cg.aRes[u.cg.i]);
69033 }
69034 break;
69035 };
69036 #endif
69037
@@ -68349,95 +69046,95 @@
69046 ** If changing into or out of WAL mode the procedure is more complicated.
69047 **
69048 ** Write a string containing the final journal-mode to register P2.
69049 */
69050 case OP_JournalMode: { /* out2-prerelease */
69051 #if 0 /* local variables moved into u.ch */
69052 Btree *pBt; /* Btree to change journal mode of */
69053 Pager *pPager; /* Pager associated with pBt */
69054 int eNew; /* New journal mode */
69055 int eOld; /* The old journal mode */
69056 const char *zFilename; /* Name of database file for pPager */
69057 #endif /* local variables moved into u.ch */
69058
69059 u.ch.eNew = pOp->p3;
69060 assert( u.ch.eNew==PAGER_JOURNALMODE_DELETE
69061 || u.ch.eNew==PAGER_JOURNALMODE_TRUNCATE
69062 || u.ch.eNew==PAGER_JOURNALMODE_PERSIST
69063 || u.ch.eNew==PAGER_JOURNALMODE_OFF
69064 || u.ch.eNew==PAGER_JOURNALMODE_MEMORY
69065 || u.ch.eNew==PAGER_JOURNALMODE_WAL
69066 || u.ch.eNew==PAGER_JOURNALMODE_QUERY
69067 );
69068 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69069
69070 u.ch.pBt = db->aDb[pOp->p1].pBt;
69071 u.ch.pPager = sqlite3BtreePager(u.ch.pBt);
69072 u.ch.eOld = sqlite3PagerGetJournalMode(u.ch.pPager);
69073 if( u.ch.eNew==PAGER_JOURNALMODE_QUERY ) u.ch.eNew = u.ch.eOld;
69074 if( !sqlite3PagerOkToChangeJournalMode(u.ch.pPager) ) u.ch.eNew = u.ch.eOld;
69075
69076 #ifndef SQLITE_OMIT_WAL
69077 u.ch.zFilename = sqlite3PagerFilename(u.ch.pPager);
69078
69079 /* Do not allow a transition to journal_mode=WAL for a database
69080 ** in temporary storage or if the VFS does not support shared memory
69081 */
69082 if( u.ch.eNew==PAGER_JOURNALMODE_WAL
69083 && (u.ch.zFilename[0]==0 /* Temp file */
69084 || !sqlite3PagerWalSupported(u.ch.pPager)) /* No shared-memory support */
69085 ){
69086 u.ch.eNew = u.ch.eOld;
69087 }
69088
69089 if( (u.ch.eNew!=u.ch.eOld)
69090 && (u.ch.eOld==PAGER_JOURNALMODE_WAL || u.ch.eNew==PAGER_JOURNALMODE_WAL)
69091 ){
69092 if( !db->autoCommit || db->activeVdbeCnt>1 ){
69093 rc = SQLITE_ERROR;
69094 sqlite3SetString(&p->zErrMsg, db,
69095 "cannot change %s wal mode from within a transaction",
69096 (u.ch.eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
69097 );
69098 break;
69099 }else{
69100
69101 if( u.ch.eOld==PAGER_JOURNALMODE_WAL ){
69102 /* If leaving WAL mode, close the log file. If successful, the call
69103 ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
69104 ** file. An EXCLUSIVE lock may still be held on the database file
69105 ** after a successful return.
69106 */
69107 rc = sqlite3PagerCloseWal(u.ch.pPager);
69108 if( rc==SQLITE_OK ){
69109 sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69110 }
69111 }else if( u.ch.eOld==PAGER_JOURNALMODE_MEMORY ){
69112 /* Cannot transition directly from MEMORY to WAL. Use mode OFF
69113 ** as an intermediate */
69114 sqlite3PagerSetJournalMode(u.ch.pPager, PAGER_JOURNALMODE_OFF);
69115 }
69116
69117 /* Open a transaction on the database file. Regardless of the journal
69118 ** mode, this transaction always uses a rollback journal.
69119 */
69120 assert( sqlite3BtreeIsInTrans(u.ch.pBt)==0 );
69121 if( rc==SQLITE_OK ){
69122 rc = sqlite3BtreeSetVersion(u.ch.pBt, (u.ch.eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
69123 }
69124 }
69125 }
69126 #endif /* ifndef SQLITE_OMIT_WAL */
69127
69128 if( rc ){
69129 u.ch.eNew = u.ch.eOld;
69130 }
69131 u.ch.eNew = sqlite3PagerSetJournalMode(u.ch.pPager, u.ch.eNew);
69132
69133 pOut = &aMem[pOp->p2];
69134 pOut->flags = MEM_Str|MEM_Static|MEM_Term;
69135 pOut->z = (char *)sqlite3JournalModename(u.ch.eNew);
69136 pOut->n = sqlite3Strlen30(pOut->z);
69137 pOut->enc = SQLITE_UTF8;
69138 sqlite3VdbeChangeEncoding(pOut, encoding);
69139 break;
69140 };
@@ -68462,18 +69159,18 @@
69159 ** Perform a single step of the incremental vacuum procedure on
69160 ** the P1 database. If the vacuum has finished, jump to instruction
69161 ** P2. Otherwise, fall through to the next instruction.
69162 */
69163 case OP_IncrVacuum: { /* jump */
69164 #if 0 /* local variables moved into u.ci */
69165 Btree *pBt;
69166 #endif /* local variables moved into u.ci */
69167
69168 assert( pOp->p1>=0 && pOp->p1<db->nDb );
69169 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69170 u.ci.pBt = db->aDb[pOp->p1].pBt;
69171 rc = sqlite3BtreeIncrVacuum(u.ci.pBt);
69172 if( rc==SQLITE_DONE ){
69173 pc = pOp->p2 - 1;
69174 rc = SQLITE_OK;
69175 }
69176 break;
@@ -68539,16 +69236,16 @@
69236 ** Also, whether or not P4 is set, check that this is not being called from
69237 ** within a callback to a virtual table xSync() method. If it is, the error
69238 ** code will be set to SQLITE_LOCKED.
69239 */
69240 case OP_VBegin: {
69241 #if 0 /* local variables moved into u.cj */
69242 VTable *pVTab;
69243 #endif /* local variables moved into u.cj */
69244 u.cj.pVTab = pOp->p4.pVtab;
69245 rc = sqlite3VtabBegin(db, u.cj.pVTab);
69246 if( u.cj.pVTab ) importVtabErrMsg(p, u.cj.pVTab->pVtab);
69247 break;
69248 }
69249 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69250
69251 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -68583,36 +69280,36 @@
69280 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69281 ** P1 is a cursor number. This opcode opens a cursor to the virtual
69282 ** table and stores that cursor in P1.
69283 */
69284 case OP_VOpen: {
69285 #if 0 /* local variables moved into u.ck */
69286 VdbeCursor *pCur;
69287 sqlite3_vtab_cursor *pVtabCursor;
69288 sqlite3_vtab *pVtab;
69289 sqlite3_module *pModule;
69290 #endif /* local variables moved into u.ck */
69291
69292 u.ck.pCur = 0;
69293 u.ck.pVtabCursor = 0;
69294 u.ck.pVtab = pOp->p4.pVtab->pVtab;
69295 u.ck.pModule = (sqlite3_module *)u.ck.pVtab->pModule;
69296 assert(u.ck.pVtab && u.ck.pModule);
69297 rc = u.ck.pModule->xOpen(u.ck.pVtab, &u.ck.pVtabCursor);
69298 importVtabErrMsg(p, u.ck.pVtab);
69299 if( SQLITE_OK==rc ){
69300 /* Initialize sqlite3_vtab_cursor base class */
69301 u.ck.pVtabCursor->pVtab = u.ck.pVtab;
69302
69303 /* Initialise vdbe cursor object */
69304 u.ck.pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
69305 if( u.ck.pCur ){
69306 u.ck.pCur->pVtabCursor = u.ck.pVtabCursor;
69307 u.ck.pCur->pModule = u.ck.pVtabCursor->pVtab->pModule;
69308 }else{
69309 db->mallocFailed = 1;
69310 u.ck.pModule->xClose(u.ck.pVtabCursor);
69311 }
69312 }
69313 break;
69314 }
69315 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68635,11 +69332,11 @@
69332 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
69333 **
69334 ** A jump is made to P2 if the result set after filtering would be empty.
69335 */
69336 case OP_VFilter: { /* jump */
69337 #if 0 /* local variables moved into u.cl */
69338 int nArg;
69339 int iQuery;
69340 const sqlite3_module *pModule;
69341 Mem *pQuery;
69342 Mem *pArgc;
@@ -68647,49 +69344,49 @@
69344 sqlite3_vtab *pVtab;
69345 VdbeCursor *pCur;
69346 int res;
69347 int i;
69348 Mem **apArg;
69349 #endif /* local variables moved into u.cl */
69350
69351 u.cl.pQuery = &aMem[pOp->p3];
69352 u.cl.pArgc = &u.cl.pQuery[1];
69353 u.cl.pCur = p->apCsr[pOp->p1];
69354 assert( memIsValid(u.cl.pQuery) );
69355 REGISTER_TRACE(pOp->p3, u.cl.pQuery);
69356 assert( u.cl.pCur->pVtabCursor );
69357 u.cl.pVtabCursor = u.cl.pCur->pVtabCursor;
69358 u.cl.pVtab = u.cl.pVtabCursor->pVtab;
69359 u.cl.pModule = u.cl.pVtab->pModule;
69360
69361 /* Grab the index number and argc parameters */
69362 assert( (u.cl.pQuery->flags&MEM_Int)!=0 && u.cl.pArgc->flags==MEM_Int );
69363 u.cl.nArg = (int)u.cl.pArgc->u.i;
69364 u.cl.iQuery = (int)u.cl.pQuery->u.i;
69365
69366 /* Invoke the xFilter method */
69367 {
69368 u.cl.res = 0;
69369 u.cl.apArg = p->apArg;
69370 for(u.cl.i = 0; u.cl.i<u.cl.nArg; u.cl.i++){
69371 u.cl.apArg[u.cl.i] = &u.cl.pArgc[u.cl.i+1];
69372 sqlite3VdbeMemStoreType(u.cl.apArg[u.cl.i]);
69373 }
69374
69375 p->inVtabMethod = 1;
69376 rc = u.cl.pModule->xFilter(u.cl.pVtabCursor, u.cl.iQuery, pOp->p4.z, u.cl.nArg, u.cl.apArg);
69377 p->inVtabMethod = 0;
69378 importVtabErrMsg(p, u.cl.pVtab);
69379 if( rc==SQLITE_OK ){
69380 u.cl.res = u.cl.pModule->xEof(u.cl.pVtabCursor);
69381 }
69382
69383 if( u.cl.res ){
69384 pc = pOp->p2 - 1;
69385 }
69386 }
69387 u.cl.pCur->nullRow = 0;
69388
69389 break;
69390 }
69391 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69392
@@ -68699,55 +69396,55 @@
69396 ** Store the value of the P2-th column of
69397 ** the row of the virtual-table that the
69398 ** P1 cursor is pointing to into register P3.
69399 */
69400 case OP_VColumn: {
69401 #if 0 /* local variables moved into u.cm */
69402 sqlite3_vtab *pVtab;
69403 const sqlite3_module *pModule;
69404 Mem *pDest;
69405 sqlite3_context sContext;
69406 #endif /* local variables moved into u.cm */
69407
69408 VdbeCursor *pCur = p->apCsr[pOp->p1];
69409 assert( pCur->pVtabCursor );
69410 assert( pOp->p3>0 && pOp->p3<=p->nMem );
69411 u.cm.pDest = &aMem[pOp->p3];
69412 memAboutToChange(p, u.cm.pDest);
69413 if( pCur->nullRow ){
69414 sqlite3VdbeMemSetNull(u.cm.pDest);
69415 break;
69416 }
69417 u.cm.pVtab = pCur->pVtabCursor->pVtab;
69418 u.cm.pModule = u.cm.pVtab->pModule;
69419 assert( u.cm.pModule->xColumn );
69420 memset(&u.cm.sContext, 0, sizeof(u.cm.sContext));
69421
69422 /* The output cell may already have a buffer allocated. Move
69423 ** the current contents to u.cm.sContext.s so in case the user-function
69424 ** can use the already allocated buffer instead of allocating a
69425 ** new one.
69426 */
69427 sqlite3VdbeMemMove(&u.cm.sContext.s, u.cm.pDest);
69428 MemSetTypeFlag(&u.cm.sContext.s, MEM_Null);
69429
69430 rc = u.cm.pModule->xColumn(pCur->pVtabCursor, &u.cm.sContext, pOp->p2);
69431 importVtabErrMsg(p, u.cm.pVtab);
69432 if( u.cm.sContext.isError ){
69433 rc = u.cm.sContext.isError;
69434 }
69435
69436 /* Copy the result of the function to the P3 register. We
69437 ** do this regardless of whether or not an error occurred to ensure any
69438 ** dynamic allocation in u.cm.sContext.s (a Mem struct) is released.
69439 */
69440 sqlite3VdbeChangeEncoding(&u.cm.sContext.s, encoding);
69441 sqlite3VdbeMemMove(u.cm.pDest, &u.cm.sContext.s);
69442 REGISTER_TRACE(pOp->p3, u.cm.pDest);
69443 UPDATE_MAX_BLOBSIZE(u.cm.pDest);
69444
69445 if( sqlite3VdbeMemTooBig(u.cm.pDest) ){
69446 goto too_big;
69447 }
69448 break;
69449 }
69450 #endif /* SQLITE_OMIT_VIRTUALTABLE */
@@ -68758,42 +69455,42 @@
69455 ** Advance virtual table P1 to the next row in its result set and
69456 ** jump to instruction P2. Or, if the virtual table has reached
69457 ** the end of its result set, then fall through to the next instruction.
69458 */
69459 case OP_VNext: { /* jump */
69460 #if 0 /* local variables moved into u.cn */
69461 sqlite3_vtab *pVtab;
69462 const sqlite3_module *pModule;
69463 int res;
69464 VdbeCursor *pCur;
69465 #endif /* local variables moved into u.cn */
69466
69467 u.cn.res = 0;
69468 u.cn.pCur = p->apCsr[pOp->p1];
69469 assert( u.cn.pCur->pVtabCursor );
69470 if( u.cn.pCur->nullRow ){
69471 break;
69472 }
69473 u.cn.pVtab = u.cn.pCur->pVtabCursor->pVtab;
69474 u.cn.pModule = u.cn.pVtab->pModule;
69475 assert( u.cn.pModule->xNext );
69476
69477 /* Invoke the xNext() method of the module. There is no way for the
69478 ** underlying implementation to return an error if one occurs during
69479 ** xNext(). Instead, if an error occurs, true is returned (indicating that
69480 ** data is available) and the error code returned when xColumn or
69481 ** some other method is next invoked on the save virtual table cursor.
69482 */
69483 p->inVtabMethod = 1;
69484 rc = u.cn.pModule->xNext(u.cn.pCur->pVtabCursor);
69485 p->inVtabMethod = 0;
69486 importVtabErrMsg(p, u.cn.pVtab);
69487 if( rc==SQLITE_OK ){
69488 u.cn.res = u.cn.pModule->xEof(u.cn.pCur->pVtabCursor);
69489 }
69490
69491 if( !u.cn.res ){
69492 /* If there is data, jump to P2 */
69493 pc = pOp->p2 - 1;
69494 }
69495 break;
69496 }
@@ -68805,23 +69502,23 @@
69502 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
69503 ** This opcode invokes the corresponding xRename method. The value
69504 ** in register P1 is passed as the zName argument to the xRename method.
69505 */
69506 case OP_VRename: {
69507 #if 0 /* local variables moved into u.co */
69508 sqlite3_vtab *pVtab;
69509 Mem *pName;
69510 #endif /* local variables moved into u.co */
69511
69512 u.co.pVtab = pOp->p4.pVtab->pVtab;
69513 u.co.pName = &aMem[pOp->p1];
69514 assert( u.co.pVtab->pModule->xRename );
69515 assert( memIsValid(u.co.pName) );
69516 REGISTER_TRACE(pOp->p1, u.co.pName);
69517 assert( u.co.pName->flags & MEM_Str );
69518 rc = u.co.pVtab->pModule->xRename(u.co.pVtab, u.co.pName->z);
69519 importVtabErrMsg(p, u.co.pVtab);
69520 p->expired = 0;
69521
69522 break;
69523 }
69524 #endif
@@ -68849,45 +69546,45 @@
69546 ** P1 is a boolean flag. If it is set to true and the xUpdate call
69547 ** is successful, then the value returned by sqlite3_last_insert_rowid()
69548 ** is set to the value of the rowid for the row just inserted.
69549 */
69550 case OP_VUpdate: {
69551 #if 0 /* local variables moved into u.cp */
69552 sqlite3_vtab *pVtab;
69553 sqlite3_module *pModule;
69554 int nArg;
69555 int i;
69556 sqlite_int64 rowid;
69557 Mem **apArg;
69558 Mem *pX;
69559 #endif /* local variables moved into u.cp */
69560
69561 assert( pOp->p2==1 || pOp->p5==OE_Fail || pOp->p5==OE_Rollback
69562 || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
69563 );
69564 u.cp.pVtab = pOp->p4.pVtab->pVtab;
69565 u.cp.pModule = (sqlite3_module *)u.cp.pVtab->pModule;
69566 u.cp.nArg = pOp->p2;
69567 assert( pOp->p4type==P4_VTAB );
69568 if( ALWAYS(u.cp.pModule->xUpdate) ){
69569 u8 vtabOnConflict = db->vtabOnConflict;
69570 u.cp.apArg = p->apArg;
69571 u.cp.pX = &aMem[pOp->p3];
69572 for(u.cp.i=0; u.cp.i<u.cp.nArg; u.cp.i++){
69573 assert( memIsValid(u.cp.pX) );
69574 memAboutToChange(p, u.cp.pX);
69575 sqlite3VdbeMemStoreType(u.cp.pX);
69576 u.cp.apArg[u.cp.i] = u.cp.pX;
69577 u.cp.pX++;
69578 }
69579 db->vtabOnConflict = pOp->p5;
69580 rc = u.cp.pModule->xUpdate(u.cp.pVtab, u.cp.nArg, u.cp.apArg, &u.cp.rowid);
69581 db->vtabOnConflict = vtabOnConflict;
69582 importVtabErrMsg(p, u.cp.pVtab);
69583 if( rc==SQLITE_OK && pOp->p1 ){
69584 assert( u.cp.nArg>1 && u.cp.apArg[0] && (u.cp.apArg[0]->flags&MEM_Null) );
69585 db->lastRowid = lastRowid = u.cp.rowid;
69586 }
69587 if( rc==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
69588 if( pOp->p5==OE_Ignore ){
69589 rc = SQLITE_OK;
69590 }else{
@@ -68943,25 +69640,25 @@
69640 **
69641 ** If tracing is enabled (by the sqlite3_trace()) interface, then
69642 ** the UTF-8 string contained in P4 is emitted on the trace callback.
69643 */
69644 case OP_Trace: {
69645 #if 0 /* local variables moved into u.cq */
69646 char *zTrace;
69647 char *z;
69648 #endif /* local variables moved into u.cq */
69649
69650 if( db->xTrace && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0 ){
69651 u.cq.z = sqlite3VdbeExpandSql(p, u.cq.zTrace);
69652 db->xTrace(db->pTraceArg, u.cq.z);
69653 sqlite3DbFree(db, u.cq.z);
69654 }
69655 #ifdef SQLITE_DEBUG
69656 if( (db->flags & SQLITE_SqlTrace)!=0
69657 && (u.cq.zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
69658 ){
69659 sqlite3DebugPrintf("SQL-trace: %s\n", u.cq.zTrace);
69660 }
69661 #endif /* SQLITE_DEBUG */
69662 break;
69663 }
69664 #endif
@@ -69556,10 +70253,930 @@
70253 }
70254
70255 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
70256
70257 /************** End of vdbeblob.c ********************************************/
70258 /************** Begin file vdbesort.c ****************************************/
70259 /*
70260 ** 2011 July 9
70261 **
70262 ** The author disclaims copyright to this source code. In place of
70263 ** a legal notice, here is a blessing:
70264 **
70265 ** May you do good and not evil.
70266 ** May you find forgiveness for yourself and forgive others.
70267 ** May you share freely, never taking more than you give.
70268 **
70269 *************************************************************************
70270 ** This file contains code for the VdbeSorter object, used in concert with
70271 ** a VdbeCursor to sort large numbers of keys (as may be required, for
70272 ** example, by CREATE INDEX statements on tables too large to fit in main
70273 ** memory).
70274 */
70275
70276
70277 #ifndef SQLITE_OMIT_MERGE_SORT
70278
70279 typedef struct VdbeSorterIter VdbeSorterIter;
70280 typedef struct SorterRecord SorterRecord;
70281
70282 /*
70283 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
70284 **
70285 ** As keys are added to the sorter, they are written to disk in a series
70286 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
70287 ** the same as the cache-size allowed for temporary databases. In order
70288 ** to allow the caller to extract keys from the sorter in sorted order,
70289 ** all PMAs currently stored on disk must be merged together. This comment
70290 ** describes the data structure used to do so. The structure supports
70291 ** merging any number of arrays in a single pass with no redundant comparison
70292 ** operations.
70293 **
70294 ** The aIter[] array contains an iterator for each of the PMAs being merged.
70295 ** An aIter[] iterator either points to a valid key or else is at EOF. For
70296 ** the purposes of the paragraphs below, we assume that the array is actually
70297 ** N elements in size, where N is the smallest power of 2 greater to or equal
70298 ** to the number of iterators being merged. The extra aIter[] elements are
70299 ** treated as if they are empty (always at EOF).
70300 **
70301 ** The aTree[] array is also N elements in size. The value of N is stored in
70302 ** the VdbeSorter.nTree variable.
70303 **
70304 ** The final (N/2) elements of aTree[] contain the results of comparing
70305 ** pairs of iterator keys together. Element i contains the result of
70306 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
70307 ** aTree element is set to the index of it.
70308 **
70309 ** For the purposes of this comparison, EOF is considered greater than any
70310 ** other key value. If the keys are equal (only possible with two EOF
70311 ** values), it doesn't matter which index is stored.
70312 **
70313 ** The (N/4) elements of aTree[] that preceed the final (N/2) described
70314 ** above contains the index of the smallest of each block of 4 iterators.
70315 ** And so on. So that aTree[1] contains the index of the iterator that
70316 ** currently points to the smallest key value. aTree[0] is unused.
70317 **
70318 ** Example:
70319 **
70320 ** aIter[0] -> Banana
70321 ** aIter[1] -> Feijoa
70322 ** aIter[2] -> Elderberry
70323 ** aIter[3] -> Currant
70324 ** aIter[4] -> Grapefruit
70325 ** aIter[5] -> Apple
70326 ** aIter[6] -> Durian
70327 ** aIter[7] -> EOF
70328 **
70329 ** aTree[] = { X, 5 0, 5 0, 3, 5, 6 }
70330 **
70331 ** The current element is "Apple" (the value of the key indicated by
70332 ** iterator 5). When the Next() operation is invoked, iterator 5 will
70333 ** be advanced to the next key in its segment. Say the next key is
70334 ** "Eggplant":
70335 **
70336 ** aIter[5] -> Eggplant
70337 **
70338 ** The contents of aTree[] are updated first by comparing the new iterator
70339 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
70340 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
70341 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
70342 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
70343 ** so the value written into element 1 of the array is 0. As follows:
70344 **
70345 ** aTree[] = { X, 0 0, 6 0, 3, 5, 6 }
70346 **
70347 ** In other words, each time we advance to the next sorter element, log2(N)
70348 ** key comparison operations are required, where N is the number of segments
70349 ** being merged (rounded up to the next power of 2).
70350 */
70351 struct VdbeSorter {
70352 int nInMemory; /* Current size of pRecord list as PMA */
70353 int nTree; /* Used size of aTree/aIter (power of 2) */
70354 VdbeSorterIter *aIter; /* Array of iterators to merge */
70355 int *aTree; /* Current state of incremental merge */
70356 i64 iWriteOff; /* Current write offset within file pTemp1 */
70357 i64 iReadOff; /* Current read offset within file pTemp1 */
70358 sqlite3_file *pTemp1; /* PMA file 1 */
70359 int nPMA; /* Number of PMAs stored in pTemp1 */
70360 SorterRecord *pRecord; /* Head of in-memory record list */
70361 int mnPmaSize; /* Minimum PMA size, in bytes */
70362 int mxPmaSize; /* Maximum PMA size, in bytes. 0==no limit */
70363 char *aSpace; /* Space for UnpackRecord() */
70364 int nSpace; /* Size of aSpace in bytes */
70365 };
70366
70367 /*
70368 ** The following type is an iterator for a PMA. It caches the current key in
70369 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
70370 */
70371 struct VdbeSorterIter {
70372 i64 iReadOff; /* Current read offset */
70373 i64 iEof; /* 1 byte past EOF for this iterator */
70374 sqlite3_file *pFile; /* File iterator is reading from */
70375 int nAlloc; /* Bytes of space at aAlloc */
70376 u8 *aAlloc; /* Allocated space */
70377 int nKey; /* Number of bytes in key */
70378 u8 *aKey; /* Pointer to current key */
70379 };
70380
70381 /*
70382 ** A structure to store a single record. All in-memory records are connected
70383 ** together into a linked list headed at VdbeSorter.pRecord using the
70384 ** SorterRecord.pNext pointer.
70385 */
70386 struct SorterRecord {
70387 void *pVal;
70388 int nVal;
70389 SorterRecord *pNext;
70390 };
70391
70392 /* Minimum allowable value for the VdbeSorter.nWorking variable */
70393 #define SORTER_MIN_WORKING 10
70394
70395 /* Maximum number of segments to merge in a single pass. */
70396 #define SORTER_MAX_MERGE_COUNT 16
70397
70398 /*
70399 ** Free all memory belonging to the VdbeSorterIter object passed as the second
70400 ** argument. All structure fields are set to zero before returning.
70401 */
70402 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
70403 sqlite3DbFree(db, pIter->aAlloc);
70404 memset(pIter, 0, sizeof(VdbeSorterIter));
70405 }
70406
70407 /*
70408 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
70409 ** no error occurs, or an SQLite error code if one does.
70410 */
70411 static int vdbeSorterIterNext(
70412 sqlite3 *db, /* Database handle (for sqlite3DbMalloc() ) */
70413 VdbeSorterIter *pIter /* Iterator to advance */
70414 ){
70415 int rc; /* Return Code */
70416 int nRead; /* Number of bytes read */
70417 int nRec = 0; /* Size of record in bytes */
70418 int iOff = 0; /* Size of serialized size varint in bytes */
70419
70420 nRead = pIter->iEof - pIter->iReadOff;
70421 if( nRead>5 ) nRead = 5;
70422 if( nRead<=0 ){
70423 /* This is an EOF condition */
70424 vdbeSorterIterZero(db, pIter);
70425 return SQLITE_OK;
70426 }
70427
70428 rc = sqlite3OsRead(pIter->pFile, pIter->aAlloc, nRead, pIter->iReadOff);
70429 if( rc==SQLITE_OK ){
70430 iOff = getVarint32(pIter->aAlloc, nRec);
70431 if( (iOff+nRec)>nRead ){
70432 int nRead2; /* Number of extra bytes to read */
70433 if( (iOff+nRec)>pIter->nAlloc ){
70434 int nNew = pIter->nAlloc*2;
70435 while( (iOff+nRec)>nNew ) nNew = nNew*2;
70436 pIter->aAlloc = sqlite3DbReallocOrFree(db, pIter->aAlloc, nNew);
70437 if( !pIter->aAlloc ) return SQLITE_NOMEM;
70438 pIter->nAlloc = nNew;
70439 }
70440
70441 nRead2 = iOff + nRec - nRead;
70442 rc = sqlite3OsRead(
70443 pIter->pFile, &pIter->aAlloc[nRead], nRead2, pIter->iReadOff+nRead
70444 );
70445 }
70446 }
70447
70448 assert( rc!=SQLITE_OK || nRec>0 );
70449 pIter->iReadOff += iOff+nRec;
70450 pIter->nKey = nRec;
70451 pIter->aKey = &pIter->aAlloc[iOff];
70452 return rc;
70453 }
70454
70455 /*
70456 ** Write a single varint, value iVal, to file-descriptor pFile. Return
70457 ** SQLITE_OK if successful, or an SQLite error code if some error occurs.
70458 **
70459 ** The value of *piOffset when this function is called is used as the byte
70460 ** offset in file pFile to write to. Before returning, *piOffset is
70461 ** incremented by the number of bytes written.
70462 */
70463 static int vdbeSorterWriteVarint(
70464 sqlite3_file *pFile, /* File to write to */
70465 i64 iVal, /* Value to write as a varint */
70466 i64 *piOffset /* IN/OUT: Write offset in file pFile */
70467 ){
70468 u8 aVarint[9]; /* Buffer large enough for a varint */
70469 int nVarint; /* Number of used bytes in varint */
70470 int rc; /* Result of write() call */
70471
70472 nVarint = sqlite3PutVarint(aVarint, iVal);
70473 rc = sqlite3OsWrite(pFile, aVarint, nVarint, *piOffset);
70474 *piOffset += nVarint;
70475
70476 return rc;
70477 }
70478
70479 /*
70480 ** Read a single varint from file-descriptor pFile. Return SQLITE_OK if
70481 ** successful, or an SQLite error code if some error occurs.
70482 **
70483 ** The value of *piOffset when this function is called is used as the
70484 ** byte offset in file pFile from whence to read the varint. If successful
70485 ** (i.e. if no IO error occurs), then *piOffset is set to the offset of
70486 ** the first byte past the end of the varint before returning. *piVal is
70487 ** set to the integer value read. If an error occurs, the final values of
70488 ** both *piOffset and *piVal are undefined.
70489 */
70490 static int vdbeSorterReadVarint(
70491 sqlite3_file *pFile, /* File to read from */
70492 i64 *piOffset, /* IN/OUT: Read offset in pFile */
70493 i64 *piVal /* OUT: Value read from file */
70494 ){
70495 u8 aVarint[9]; /* Buffer large enough for a varint */
70496 i64 iOff = *piOffset; /* Offset in file to read from */
70497 int rc; /* Return code */
70498
70499 rc = sqlite3OsRead(pFile, aVarint, 9, iOff);
70500 if( rc==SQLITE_OK ){
70501 *piOffset += getVarint(aVarint, (u64 *)piVal);
70502 }
70503
70504 return rc;
70505 }
70506
70507 /*
70508 ** Initialize iterator pIter to scan through the PMA stored in file pFile
70509 ** starting at offset iStart and ending at offset iEof-1. This function
70510 ** leaves the iterator pointing to the first key in the PMA (or EOF if the
70511 ** PMA is empty).
70512 */
70513 static int vdbeSorterIterInit(
70514 sqlite3 *db, /* Database handle */
70515 VdbeSorter *pSorter, /* Sorter object */
70516 i64 iStart, /* Start offset in pFile */
70517 VdbeSorterIter *pIter, /* Iterator to populate */
70518 i64 *pnByte /* IN/OUT: Increment this value by PMA size */
70519 ){
70520 int rc;
70521
70522 assert( pSorter->iWriteOff>iStart );
70523 assert( pIter->aAlloc==0 );
70524 pIter->pFile = pSorter->pTemp1;
70525 pIter->iReadOff = iStart;
70526 pIter->nAlloc = 128;
70527 pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
70528 if( !pIter->aAlloc ){
70529 rc = SQLITE_NOMEM;
70530 }else{
70531 i64 nByte; /* Total size of PMA in bytes */
70532 rc = vdbeSorterReadVarint(pSorter->pTemp1, &pIter->iReadOff, &nByte);
70533 *pnByte += nByte;
70534 pIter->iEof = pIter->iReadOff + nByte;
70535 }
70536 if( rc==SQLITE_OK ){
70537 rc = vdbeSorterIterNext(db, pIter);
70538 }
70539 return rc;
70540 }
70541
70542
70543 /*
70544 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
70545 ** size nKey2 bytes). Argument pKeyInfo supplies the collation functions
70546 ** used by the comparison. If an error occurs, return an SQLite error code.
70547 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
70548 ** value, depending on whether key1 is smaller, equal to or larger than key2.
70549 **
70550 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
70551 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
70552 ** is true and key1 contains even a single NULL value, it is considered to
70553 ** be less than key2. Even if key2 also contains NULL values.
70554 **
70555 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
70556 ** has been allocated and contains an unpacked record that is used as key2.
70557 */
70558 static int vdbeSorterCompare(
70559 VdbeCursor *pCsr, /* Cursor object (for pKeyInfo) */
70560 int bOmitRowid, /* Ignore rowid field at end of keys */
70561 void *pKey1, int nKey1, /* Left side of comparison */
70562 void *pKey2, int nKey2, /* Right side of comparison */
70563 int *pRes /* OUT: Result of comparison */
70564 ){
70565 KeyInfo *pKeyInfo = pCsr->pKeyInfo;
70566 VdbeSorter *pSorter = pCsr->pSorter;
70567 char *aSpace = pSorter->aSpace;
70568 int nSpace = pSorter->nSpace;
70569 UnpackedRecord *r2;
70570 int i;
70571
70572 if( aSpace==0 ){
70573 nSpace = ROUND8(sizeof(UnpackedRecord))+(pKeyInfo->nField+1)*sizeof(Mem);
70574 aSpace = (char *)sqlite3Malloc(nSpace);
70575 if( aSpace==0 ) return SQLITE_NOMEM;
70576 pSorter->aSpace = aSpace;
70577 pSorter->nSpace = nSpace;
70578 }
70579
70580 if( pKey2 ){
70581 /* This call cannot fail. As the memory is already allocated. */
70582 r2 = sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, aSpace, nSpace);
70583 assert( r2 && (r2->flags & UNPACKED_NEED_FREE)==0 );
70584 assert( r2==(UnpackedRecord*)aSpace );
70585 }else{
70586 r2 = (UnpackedRecord *)aSpace;
70587 assert( !bOmitRowid );
70588 }
70589
70590 if( bOmitRowid ){
70591 for(i=0; i<r2->nField-1; i++){
70592 if( r2->aMem[i].flags & MEM_Null ){
70593 *pRes = -1;
70594 return SQLITE_OK;
70595 }
70596 }
70597 r2->flags |= UNPACKED_PREFIX_MATCH;
70598 r2->nField--;
70599 assert( r2->nField>0 );
70600 }
70601
70602 *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
70603 return SQLITE_OK;
70604 }
70605
70606 /*
70607 ** This function is called to compare two iterator keys when merging
70608 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
70609 ** value to recalculate.
70610 */
70611 static int vdbeSorterDoCompare(VdbeCursor *pCsr, int iOut){
70612 VdbeSorter *pSorter = pCsr->pSorter;
70613 int i1;
70614 int i2;
70615 int iRes;
70616 VdbeSorterIter *p1;
70617 VdbeSorterIter *p2;
70618
70619 assert( iOut<pSorter->nTree && iOut>0 );
70620
70621 if( iOut>=(pSorter->nTree/2) ){
70622 i1 = (iOut - pSorter->nTree/2) * 2;
70623 i2 = i1 + 1;
70624 }else{
70625 i1 = pSorter->aTree[iOut*2];
70626 i2 = pSorter->aTree[iOut*2+1];
70627 }
70628
70629 p1 = &pSorter->aIter[i1];
70630 p2 = &pSorter->aIter[i2];
70631
70632 if( p1->pFile==0 ){
70633 iRes = i2;
70634 }else if( p2->pFile==0 ){
70635 iRes = i1;
70636 }else{
70637 int res;
70638 int rc;
70639 assert( pCsr->pSorter->aSpace!=0 ); /* allocated in vdbeSorterMerge() */
70640 rc = vdbeSorterCompare(
70641 pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
70642 );
70643 /* The vdbeSorterCompare() call cannot fail since pCsr->pSorter->aSpace
70644 ** has already been allocated. */
70645 assert( rc==SQLITE_OK );
70646
70647 if( res<=0 ){
70648 iRes = i1;
70649 }else{
70650 iRes = i2;
70651 }
70652 }
70653
70654 pSorter->aTree[iOut] = iRes;
70655 return SQLITE_OK;
70656 }
70657
70658 /*
70659 ** Initialize the temporary index cursor just opened as a sorter cursor.
70660 */
70661 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
70662 int pgsz; /* Page size of main database */
70663 int mxCache; /* Cache size */
70664 VdbeSorter *pSorter; /* The new sorter */
70665
70666 assert( pCsr->pKeyInfo && pCsr->pBt==0 );
70667 pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
70668 if( pSorter==0 ){
70669 return SQLITE_NOMEM;
70670 }
70671
70672 if( !sqlite3TempInMemory(db) ){
70673 pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
70674 pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
70675 mxCache = db->aDb[0].pSchema->cache_size;
70676 if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
70677 pSorter->mxPmaSize = mxCache * pgsz;
70678 }
70679
70680 return SQLITE_OK;
70681 }
70682
70683 /*
70684 ** Free the list of sorted records starting at pRecord.
70685 */
70686 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
70687 SorterRecord *p;
70688 SorterRecord *pNext;
70689 for(p=pRecord; p; p=pNext){
70690 pNext = p->pNext;
70691 sqlite3DbFree(db, p);
70692 }
70693 }
70694
70695 /*
70696 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
70697 */
70698 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
70699 VdbeSorter *pSorter = pCsr->pSorter;
70700 if( pSorter ){
70701 if( pSorter->aIter ){
70702 int i;
70703 for(i=0; i<pSorter->nTree; i++){
70704 vdbeSorterIterZero(db, &pSorter->aIter[i]);
70705 }
70706 sqlite3DbFree(db, pSorter->aIter);
70707 }
70708 if( pSorter->pTemp1 ){
70709 sqlite3OsCloseFree(pSorter->pTemp1);
70710 }
70711 vdbeSorterRecordFree(db, pSorter->pRecord);
70712 sqlite3_free(pSorter->aSpace);
70713 sqlite3DbFree(db, pSorter);
70714 pCsr->pSorter = 0;
70715 }
70716 }
70717
70718 /*
70719 ** Allocate space for a file-handle and open a temporary file. If successful,
70720 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
70721 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
70722 */
70723 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
70724 int dummy;
70725 return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
70726 SQLITE_OPEN_TEMP_JOURNAL |
70727 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
70728 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE, &dummy
70729 );
70730 }
70731
70732 /*
70733 ** Attemp to merge the two sorted lists p1 and p2 into a single list. If no
70734 ** error occurs set *ppOut to the head of the new list and return SQLITE_OK.
70735 */
70736 static int vdbeSorterMerge(
70737 sqlite3 *db, /* Database handle */
70738 VdbeCursor *pCsr, /* For pKeyInfo */
70739 SorterRecord *p1, /* First list to merge */
70740 SorterRecord *p2, /* Second list to merge */
70741 SorterRecord **ppOut /* OUT: Head of merged list */
70742 ){
70743 int rc = SQLITE_OK;
70744 SorterRecord *pFinal = 0;
70745 SorterRecord **pp = &pFinal;
70746 void *pVal2 = p2 ? p2->pVal : 0;
70747
70748 while( p1 && p2 ){
70749 int res;
70750 rc = vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
70751 if( rc!=SQLITE_OK ){
70752 *pp = 0;
70753 vdbeSorterRecordFree(db, p1);
70754 vdbeSorterRecordFree(db, p2);
70755 vdbeSorterRecordFree(db, pFinal);
70756 *ppOut = 0;
70757 return rc;
70758 }
70759 if( res<=0 ){
70760 *pp = p1;
70761 pp = &p1->pNext;
70762 p1 = p1->pNext;
70763 pVal2 = 0;
70764 }else{
70765 *pp = p2;
70766 pp = &p2->pNext;
70767 p2 = p2->pNext;
70768 if( p2==0 ) break;
70769 pVal2 = p2->pVal;
70770 }
70771 }
70772 *pp = p1 ? p1 : p2;
70773
70774 *ppOut = pFinal;
70775 return SQLITE_OK;
70776 }
70777
70778 /*
70779 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
70780 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
70781 ** occurs.
70782 */
70783 static int vdbeSorterSort(sqlite3 *db, VdbeCursor *pCsr){
70784 int rc = SQLITE_OK;
70785 int i;
70786 SorterRecord **aSlot;
70787 SorterRecord *p;
70788 VdbeSorter *pSorter = pCsr->pSorter;
70789
70790 aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
70791 if( !aSlot ){
70792 return SQLITE_NOMEM;
70793 }
70794
70795 p = pSorter->pRecord;
70796 while( p ){
70797 SorterRecord *pNext = p->pNext;
70798 p->pNext = 0;
70799 for(i=0; rc==SQLITE_OK && aSlot[i]; i++){
70800 rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
70801 aSlot[i] = 0;
70802 }
70803 if( rc!=SQLITE_OK ){
70804 vdbeSorterRecordFree(db, pNext);
70805 break;
70806 }
70807 aSlot[i] = p;
70808 p = pNext;
70809 }
70810
70811 p = 0;
70812 for(i=0; i<64; i++){
70813 if( rc==SQLITE_OK ){
70814 rc = vdbeSorterMerge(db, pCsr, p, aSlot[i], &p);
70815 }else{
70816 vdbeSorterRecordFree(db, aSlot[i]);
70817 }
70818 }
70819 pSorter->pRecord = p;
70820
70821 sqlite3_free(aSlot);
70822 return rc;
70823 }
70824
70825
70826 /*
70827 ** Write the current contents of the in-memory linked-list to a PMA. Return
70828 ** SQLITE_OK if successful, or an SQLite error code otherwise.
70829 **
70830 ** The format of a PMA is:
70831 **
70832 ** * A varint. This varint contains the total number of bytes of content
70833 ** in the PMA (not including the varint itself).
70834 **
70835 ** * One or more records packed end-to-end in order of ascending keys.
70836 ** Each record consists of a varint followed by a blob of data (the
70837 ** key). The varint is the number of bytes in the blob of data.
70838 */
70839 static int vdbeSorterListToPMA(sqlite3 *db, VdbeCursor *pCsr){
70840 int rc = SQLITE_OK; /* Return code */
70841 VdbeSorter *pSorter = pCsr->pSorter;
70842
70843 if( pSorter->nInMemory==0 ){
70844 assert( pSorter->pRecord==0 );
70845 return rc;
70846 }
70847
70848 rc = vdbeSorterSort(db, pCsr);
70849
70850 /* If the first temporary PMA file has not been opened, open it now. */
70851 if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
70852 rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
70853 assert( rc!=SQLITE_OK || pSorter->pTemp1 );
70854 assert( pSorter->iWriteOff==0 );
70855 assert( pSorter->nPMA==0 );
70856 }
70857
70858 if( rc==SQLITE_OK ){
70859 i64 iOff = pSorter->iWriteOff;
70860 SorterRecord *p;
70861 SorterRecord *pNext = 0;
70862 static const char eightZeros[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
70863
70864 pSorter->nPMA++;
70865 rc = vdbeSorterWriteVarint(pSorter->pTemp1, pSorter->nInMemory, &iOff);
70866 for(p=pSorter->pRecord; rc==SQLITE_OK && p; p=pNext){
70867 pNext = p->pNext;
70868 rc = vdbeSorterWriteVarint(pSorter->pTemp1, p->nVal, &iOff);
70869
70870 if( rc==SQLITE_OK ){
70871 rc = sqlite3OsWrite(pSorter->pTemp1, p->pVal, p->nVal, iOff);
70872 iOff += p->nVal;
70873 }
70874
70875 sqlite3DbFree(db, p);
70876 }
70877
70878 /* This assert verifies that unless an error has occurred, the size of
70879 ** the PMA on disk is the same as the expected size stored in
70880 ** pSorter->nInMemory. */
70881 assert( rc!=SQLITE_OK || pSorter->nInMemory==(
70882 iOff-pSorter->iWriteOff-sqlite3VarintLen(pSorter->nInMemory)
70883 ));
70884
70885 pSorter->iWriteOff = iOff;
70886 if( rc==SQLITE_OK ){
70887 /* Terminate each file with 8 extra bytes so that from any offset
70888 ** in the file we can always read 9 bytes without a SHORT_READ error */
70889 rc = sqlite3OsWrite(pSorter->pTemp1, eightZeros, 8, iOff);
70890 }
70891 pSorter->pRecord = p;
70892 }
70893
70894 return rc;
70895 }
70896
70897 /*
70898 ** Add a record to the sorter.
70899 */
70900 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
70901 sqlite3 *db, /* Database handle */
70902 VdbeCursor *pCsr, /* Sorter cursor */
70903 Mem *pVal /* Memory cell containing record */
70904 ){
70905 VdbeSorter *pSorter = pCsr->pSorter;
70906 int rc = SQLITE_OK; /* Return Code */
70907 SorterRecord *pNew; /* New list element */
70908
70909 assert( pSorter );
70910 pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
70911
70912 pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
70913 if( pNew==0 ){
70914 rc = SQLITE_NOMEM;
70915 }else{
70916 pNew->pVal = (void *)&pNew[1];
70917 memcpy(pNew->pVal, pVal->z, pVal->n);
70918 pNew->nVal = pVal->n;
70919 pNew->pNext = pSorter->pRecord;
70920 pSorter->pRecord = pNew;
70921 }
70922
70923 /* See if the contents of the sorter should now be written out. They
70924 ** are written out when either of the following are true:
70925 **
70926 ** * The total memory allocated for the in-memory list is greater
70927 ** than (page-size * cache-size), or
70928 **
70929 ** * The total memory allocated for the in-memory list is greater
70930 ** than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
70931 */
70932 if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
70933 (pSorter->nInMemory>pSorter->mxPmaSize)
70934 || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
70935 )){
70936 rc = vdbeSorterListToPMA(db, pCsr);
70937 pSorter->nInMemory = 0;
70938 }
70939
70940 return rc;
70941 }
70942
70943 /*
70944 ** Helper function for sqlite3VdbeSorterRewind().
70945 */
70946 static int vdbeSorterInitMerge(
70947 sqlite3 *db, /* Database handle */
70948 VdbeCursor *pCsr, /* Cursor handle for this sorter */
70949 i64 *pnByte /* Sum of bytes in all opened PMAs */
70950 ){
70951 VdbeSorter *pSorter = pCsr->pSorter;
70952 int rc = SQLITE_OK; /* Return code */
70953 int i; /* Used to iterator through aIter[] */
70954 i64 nByte = 0; /* Total bytes in all opened PMAs */
70955
70956 /* Initialize the iterators. */
70957 for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
70958 VdbeSorterIter *pIter = &pSorter->aIter[i];
70959 rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
70960 pSorter->iReadOff = pIter->iEof;
70961 assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
70962 if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
70963 }
70964
70965 /* Initialize the aTree[] array. */
70966 for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
70967 rc = vdbeSorterDoCompare(pCsr, i);
70968 }
70969
70970 *pnByte = nByte;
70971 return rc;
70972 }
70973
70974 /*
70975 ** Once the sorter has been populated, this function is called to prepare
70976 ** for iterating through its contents in sorted order.
70977 */
70978 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
70979 VdbeSorter *pSorter = pCsr->pSorter;
70980 int rc; /* Return code */
70981 sqlite3_file *pTemp2 = 0; /* Second temp file to use */
70982 i64 iWrite2 = 0; /* Write offset for pTemp2 */
70983 int nIter; /* Number of iterators used */
70984 int nByte; /* Bytes of space required for aIter/aTree */
70985 int N = 2; /* Power of 2 >= nIter */
70986
70987 assert( pSorter );
70988
70989 /* If no data has been written to disk, then do not do so now. Instead,
70990 ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
70991 ** from the in-memory list. */
70992 if( pSorter->nPMA==0 ){
70993 *pbEof = !pSorter->pRecord;
70994 assert( pSorter->aTree==0 );
70995 return vdbeSorterSort(db, pCsr);
70996 }
70997
70998 /* Write the current b-tree to a PMA. Close the b-tree cursor. */
70999 rc = vdbeSorterListToPMA(db, pCsr);
71000 if( rc!=SQLITE_OK ) return rc;
71001
71002 /* Allocate space for aIter[] and aTree[]. */
71003 nIter = pSorter->nPMA;
71004 if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
71005 assert( nIter>0 );
71006 while( N<nIter ) N += N;
71007 nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
71008 pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
71009 if( !pSorter->aIter ) return SQLITE_NOMEM;
71010 pSorter->aTree = (int *)&pSorter->aIter[N];
71011 pSorter->nTree = N;
71012
71013 do {
71014 int iNew; /* Index of new, merged, PMA */
71015
71016 for(iNew=0;
71017 rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
71018 iNew++
71019 ){
71020 i64 nWrite; /* Number of bytes in new PMA */
71021
71022 /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
71023 ** initialize an iterator for each of them and break out of the loop.
71024 ** These iterators will be incrementally merged as the VDBE layer calls
71025 ** sqlite3VdbeSorterNext().
71026 **
71027 ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
71028 ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
71029 ** are merged into a single PMA that is written to file pTemp2.
71030 */
71031 rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
71032 assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
71033 if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71034 break;
71035 }
71036
71037 /* Open the second temp file, if it is not already open. */
71038 if( pTemp2==0 ){
71039 assert( iWrite2==0 );
71040 rc = vdbeSorterOpenTempFile(db, &pTemp2);
71041 }
71042
71043 if( rc==SQLITE_OK ){
71044 rc = vdbeSorterWriteVarint(pTemp2, nWrite, &iWrite2);
71045 }
71046
71047 if( rc==SQLITE_OK ){
71048 int bEof = 0;
71049 while( rc==SQLITE_OK && bEof==0 ){
71050 int nToWrite;
71051 VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71052 assert( pIter->pFile );
71053 nToWrite = pIter->nKey + sqlite3VarintLen(pIter->nKey);
71054 rc = sqlite3OsWrite(pTemp2, pIter->aAlloc, nToWrite, iWrite2);
71055 iWrite2 += nToWrite;
71056 if( rc==SQLITE_OK ){
71057 rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
71058 }
71059 }
71060 }
71061 }
71062
71063 if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
71064 break;
71065 }else{
71066 sqlite3_file *pTmp = pSorter->pTemp1;
71067 pSorter->nPMA = iNew;
71068 pSorter->pTemp1 = pTemp2;
71069 pTemp2 = pTmp;
71070 pSorter->iWriteOff = iWrite2;
71071 pSorter->iReadOff = 0;
71072 iWrite2 = 0;
71073 }
71074 }while( rc==SQLITE_OK );
71075
71076 if( pTemp2 ){
71077 sqlite3OsCloseFree(pTemp2);
71078 }
71079 *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71080 return rc;
71081 }
71082
71083 /*
71084 ** Advance to the next element in the sorter.
71085 */
71086 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, VdbeCursor *pCsr, int *pbEof){
71087 VdbeSorter *pSorter = pCsr->pSorter;
71088 int rc; /* Return code */
71089
71090 if( pSorter->aTree ){
71091 int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
71092 int i; /* Index of aTree[] to recalculate */
71093
71094 rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
71095 for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
71096 rc = vdbeSorterDoCompare(pCsr, i);
71097 }
71098
71099 *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
71100 }else{
71101 SorterRecord *pFree = pSorter->pRecord;
71102 pSorter->pRecord = pFree->pNext;
71103 pFree->pNext = 0;
71104 vdbeSorterRecordFree(db, pFree);
71105 *pbEof = !pSorter->pRecord;
71106 rc = SQLITE_OK;
71107 }
71108 return rc;
71109 }
71110
71111 /*
71112 ** Return a pointer to a buffer owned by the sorter that contains the
71113 ** current key.
71114 */
71115 static void *vdbeSorterRowkey(
71116 VdbeSorter *pSorter, /* Sorter object */
71117 int *pnKey /* OUT: Size of current key in bytes */
71118 ){
71119 void *pKey;
71120 if( pSorter->aTree ){
71121 VdbeSorterIter *pIter;
71122 pIter = &pSorter->aIter[ pSorter->aTree[1] ];
71123 *pnKey = pIter->nKey;
71124 pKey = pIter->aKey;
71125 }else{
71126 *pnKey = pSorter->pRecord->nVal;
71127 pKey = pSorter->pRecord->pVal;
71128 }
71129 return pKey;
71130 }
71131
71132 /*
71133 ** Copy the current sorter key into the memory cell pOut.
71134 */
71135 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(VdbeCursor *pCsr, Mem *pOut){
71136 VdbeSorter *pSorter = pCsr->pSorter;
71137 void *pKey; int nKey; /* Sorter key to copy into pOut */
71138
71139 pKey = vdbeSorterRowkey(pSorter, &nKey);
71140 if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
71141 return SQLITE_NOMEM;
71142 }
71143 pOut->n = nKey;
71144 MemSetTypeFlag(pOut, MEM_Blob);
71145 memcpy(pOut->z, pKey, nKey);
71146
71147 return SQLITE_OK;
71148 }
71149
71150 /*
71151 ** Compare the key in memory cell pVal with the key that the sorter cursor
71152 ** passed as the first argument currently points to. For the purposes of
71153 ** the comparison, ignore the rowid field at the end of each record.
71154 **
71155 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
71156 ** Otherwise, set *pRes to a negative, zero or positive value if the
71157 ** key in pVal is smaller than, equal to or larger than the current sorter
71158 ** key.
71159 */
71160 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
71161 VdbeCursor *pCsr, /* Sorter cursor */
71162 Mem *pVal, /* Value to compare to current sorter key */
71163 int *pRes /* OUT: Result of comparison */
71164 ){
71165 int rc;
71166 VdbeSorter *pSorter = pCsr->pSorter;
71167 void *pKey; int nKey; /* Sorter key to compare pVal with */
71168
71169 pKey = vdbeSorterRowkey(pSorter, &nKey);
71170 rc = vdbeSorterCompare(pCsr, 1, pVal->z, pVal->n, pKey, nKey, pRes);
71171 assert( rc!=SQLITE_OK || pVal->db->mallocFailed || (*pRes)<=0 );
71172 return rc;
71173 }
71174
71175 #endif /* #ifndef SQLITE_OMIT_MERGE_SORT */
71176
71177 /************** End of vdbesort.c ********************************************/
71178 /************** Begin file journal.c *****************************************/
71179 /*
71180 ** 2007 August 22
71181 **
71182 ** The author disclaims copyright to this source code. In place of
@@ -70072,10 +71689,12 @@
71689 **
71690 *************************************************************************
71691 ** This file contains routines used for walking the parser tree for
71692 ** an SQL statement.
71693 */
71694 /* #include <stdlib.h> */
71695 /* #include <string.h> */
71696
71697
71698 /*
71699 ** Walk an expression tree. Invoke the callback once for each node
71700 ** of the expression, while decending. (In other words, the callback
@@ -70210,10 +71829,12 @@
71829 **
71830 ** This file contains routines used for walking the parser tree and
71831 ** resolve all identifiers by associating them with a particular
71832 ** table and column.
71833 */
71834 /* #include <stdlib.h> */
71835 /* #include <string.h> */
71836
71837 /*
71838 ** Turn the pExpr expression into an alias for the iCol-th column of the
71839 ** result set in pEList.
71840 **
@@ -73700,11 +75321,11 @@
75321 if( !pAggInfo->directMode ){
75322 assert( pCol->iMem>0 );
75323 inReg = pCol->iMem;
75324 break;
75325 }else if( pAggInfo->useSortingIdx ){
75326 sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
75327 pCol->iSorterColumn, target);
75328 break;
75329 }
75330 /* Otherwise, fall thru into the TK_COLUMN case */
75331 }
@@ -76012,100 +77633,10 @@
77633 ** May you find forgiveness for yourself and forgive others.
77634 ** May you share freely, never taking more than you give.
77635 **
77636 *************************************************************************
77637 ** This file contains code associated with the ANALYZE command.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77638 */
77639 #ifndef SQLITE_OMIT_ANALYZE
77640
77641 /*
77642 ** This routine generates code that opens the sqlite_stat1 table for
@@ -76133,18 +77664,12 @@
77664 static const struct {
77665 const char *zName;
77666 const char *zCols;
77667 } aTable[] = {
77668 { "sqlite_stat1", "tbl,idx,stat" },
77669 #ifdef SQLITE_ENABLE_STAT2
77670 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
 
 
 
 
 
 
77671 #endif
77672 };
77673
77674 int aRoot[] = {0, 0};
77675 u8 aCreateTbl[] = {0, 0};
@@ -76156,21 +77681,10 @@
77681 if( v==0 ) return;
77682 assert( sqlite3BtreeHoldsAllMutexes(db) );
77683 assert( sqlite3VdbeDb(v)==db );
77684 pDb = &db->aDb[iDb];
77685
 
 
 
 
 
 
 
 
 
 
 
77686 for(i=0; i<ArraySize(aTable); i++){
77687 const char *zTab = aTable[i].zName;
77688 Table *pStat;
77689 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
77690 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -76197,238 +77711,17 @@
77711 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
77712 }
77713 }
77714 }
77715
77716 /* Open the sqlite_stat[12] tables for writing. */
77717 for(i=0; i<ArraySize(aTable); i++){
77718 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
77719 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
77720 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
77721 }
77722 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77723
77724 /*
77725 ** Generate code to do an analysis of all indices associated with
77726 ** a single table.
77727 */
@@ -76448,31 +77741,24 @@
77741 int endOfLoop; /* The end of the loop */
77742 int jZeroRows = -1; /* Jump from here if number of rows is zero */
77743 int iDb; /* Index of database containing pTab */
77744 int regTabname = iMem++; /* Register containing table name */
77745 int regIdxname = iMem++; /* Register containing index name */
77746 int regSampleno = iMem++; /* Register containing next sample number */
77747 int regCol = iMem++; /* Content of a column analyzed table */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77748 int regRec = iMem++; /* Register holding completed record */
77749 int regTemp = iMem++; /* Temporary use register */
77750 int regRowid = iMem++; /* Rowid for the inserted record */
77751
77752 #ifdef SQLITE_ENABLE_STAT2
77753 int addr = 0; /* Instruction address */
77754 int regTemp2 = iMem++; /* Temporary use register */
77755 int regSamplerecno = iMem++; /* Index of next sample to record */
77756 int regRecno = iMem++; /* Current sample index */
77757 int regLast = iMem++; /* Index of last sample to record */
77758 int regFirst = iMem++; /* Index of first sample to record */
77759 #endif
77760
77761 v = sqlite3GetVdbe(pParse);
77762 if( v==0 || NEVER(pTab==0) ){
77763 return;
77764 }
@@ -76501,22 +77787,17 @@
77787 iIdxCur = pParse->nTab++;
77788 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
77789 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
77790 int nCol;
77791 KeyInfo *pKey;
 
 
77792
77793 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 
77794 nCol = pIdx->nColumn;
77795 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
77796 if( iMem+1+(nCol*2)>pParse->nMem ){
77797 pParse->nMem = iMem+1+(nCol*2);
77798 }
 
 
77799
77800 /* Open a cursor to the index to be analyzed. */
77801 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
77802 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
77803 (char *)pKey, P4_KEYINFO_HANDOFF);
@@ -76523,24 +77804,35 @@
77804 VdbeComment((v, "%s", pIdx->zName));
77805
77806 /* Populate the register containing the index name. */
77807 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
77808
77809 #ifdef SQLITE_ENABLE_STAT2
77810
77811 /* If this iteration of the loop is generating code to analyze the
77812 ** first index in the pTab->pIndex list, then register regLast has
77813 ** not been populated. In this case populate it now. */
77814 if( pTab->pIndex==pIdx ){
77815 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77816 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77817 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77818
77819 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77820 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77821 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77822 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77823 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77824 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77825 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77826 sqlite3VdbeJumpHere(v, addr);
77827 }
77828
77829 /* Zero the regSampleno and regRecno registers. */
77830 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77831 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77832 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77833 #endif
77834
77835 /* The block of memory cells initialized here is used as follows.
77836 **
77837 ** iMem:
77838 ** The total number of rows in the table.
@@ -76566,87 +77858,79 @@
77858 /* Start the analysis loop. This loop runs through all the entries in
77859 ** the index b-tree. */
77860 endOfLoop = sqlite3VdbeMakeLabel(v);
77861 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
77862 topOfLoop = sqlite3VdbeCurrentAddr(v);
77863 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
77864
77865 for(i=0; i<nCol; i++){
77866 CollSeq *pColl;
77867 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
77868 if( i==0 ){
77869 #ifdef SQLITE_ENABLE_STAT2
77870 /* Check if the record that cursor iIdxCur points to contains a
77871 ** value that should be stored in the sqlite_stat2 table. If so,
77872 ** store it. */
77873 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77874 assert( regTabname+1==regIdxname
77875 && regTabname+2==regSampleno
77876 && regTabname+3==regCol
77877 );
77878 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77879 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77880 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77881 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77882
77883 /* Calculate new values for regSamplerecno and regSampleno.
77884 **
77885 ** sampleno = sampleno + 1
77886 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77887 */
77888 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77889 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77890 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77891 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77892 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77893 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77894 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77895
77896 sqlite3VdbeJumpHere(v, ne);
77897 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77898 #endif
77899
77900 /* Always record the very first row */
77901 sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77902 }
77903 assert( pIdx->azColl!=0 );
77904 assert( pIdx->azColl[i]!=0 );
77905 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
77906 sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77907 (char*)pColl, P4_COLLSEQ);
77908 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
77909 }
77910 if( db->mallocFailed ){
77911 /* If a malloc failure has occurred, then the result of the expression
77912 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77913 ** below may be negative. Which causes an assert() to fail (or an
77914 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77915 return;
77916 }
77917 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
77918 for(i=0; i<nCol; i++){
77919 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
77920 if( i==0 ){
77921 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
77922 }
77923 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
 
 
 
 
 
 
 
 
77924 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
77925 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
77926 }
 
77927
77928 /* End of the analysis loop. */
77929 sqlite3VdbeResolveLabel(v, endOfLoop);
 
77930 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
77931 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77932
77933 /* Store the results in sqlite_stat1.
77934 **
77935 ** The result is a single row of the sqlite_stat1 table. The first
77936 ** two columns are the names of the table and index. The third column
@@ -76662,51 +77946,50 @@
77946 **
77947 ** If K==0 then no entry is made into the sqlite_stat1 table.
77948 ** If K>0 then it is always the case the D>0 so division by zero
77949 ** is never possible.
77950 */
77951 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
77952 if( jZeroRows<0 ){
77953 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
77954 }
77955 for(i=0; i<nCol; i++){
77956 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
77957 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77958 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
77959 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77960 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
77961 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
77962 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77963 }
77964 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77965 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77966 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77967 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77968 }
77969
77970 /* If the table has no indices, create a single sqlite_stat1 entry
77971 ** containing NULL as the index name and the row count as the content.
77972 */
77973 if( pTab->pIndex==0 ){
77974 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
77975 VdbeComment((v, "%s", pTab->zName));
77976 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
77977 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77978 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
77979 }else{
77980 sqlite3VdbeJumpHere(v, jZeroRows);
77981 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
77982 }
77983 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
77984 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77985 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77986 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77987 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77988 if( pParse->nMem<regRec ) pParse->nMem = regRec;
77989 sqlite3VdbeJumpHere(v, jZeroRows);
77990 }
 
77991
77992 /*
77993 ** Generate code that will cause the most recent index analysis to
77994 ** be loaded into internal hash tables where is can be used.
77995 */
@@ -76727,11 +78010,11 @@
78010 int iStatCur;
78011 int iMem;
78012
78013 sqlite3BeginWriteOperation(pParse, 0, iDb);
78014 iStatCur = pParse->nTab;
78015 pParse->nTab += 2;
78016 openStatTable(pParse, iDb, iStatCur, 0, 0);
78017 iMem = pParse->nMem+1;
78018 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78019 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
78020 Table *pTab = (Table*)sqliteHashData(k);
@@ -76752,11 +78035,11 @@
78035 assert( pTab!=0 );
78036 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
78037 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78038 sqlite3BeginWriteOperation(pParse, 0, iDb);
78039 iStatCur = pParse->nTab;
78040 pParse->nTab += 2;
78041 if( pOnlyIdx ){
78042 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
78043 }else{
78044 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
78045 }
@@ -76857,11 +78140,11 @@
78140 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
78141 analysisInfo *pInfo = (analysisInfo*)pData;
78142 Index *pIndex;
78143 Table *pTable;
78144 int i, c, n;
78145 unsigned int v;
78146 const char *z;
78147
78148 assert( argc==3 );
78149 UNUSED_PARAMETER2(NotUsed, argc);
78150
@@ -76900,172 +78183,40 @@
78183 /*
78184 ** If the Index.aSample variable is not NULL, delete the aSample[] array
78185 ** and its contents.
78186 */
78187 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
78188 #ifdef SQLITE_ENABLE_STAT2
78189 if( pIdx->aSample ){
78190 int j;
78191 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
78192 IndexSample *p = &pIdx->aSample[j];
78193 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
78194 sqlite3DbFree(db, p->u.z);
78195 }
78196 }
78197 sqlite3DbFree(db, pIdx->aSample);
78198 }
 
 
 
78199 #else
78200 UNUSED_PARAMETER(db);
78201 UNUSED_PARAMETER(pIdx);
78202 #endif
78203 }
78204
78205 /*
78206 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78207 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
78208 ** arrays. The contents of sqlite_stat2 are used to populate the
78209 ** Index.aSample[] arrays.
78210 **
78211 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
78212 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
78213 ** during compilation and the sqlite_stat2 table is present, no data is
78214 ** read from it.
78215 **
78216 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the
78217 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
78218 ** returned. However, in this case, data is read from the sqlite_stat1
78219 ** table (if it is present) before returning.
78220 **
78221 ** If an OOM error occurs, this function always sets db->mallocFailed.
78222 ** This means if the caller does not care about other errors, the return
@@ -77083,14 +78234,12 @@
78234 /* Clear any prior statistics */
78235 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78236 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
78237 Index *pIdx = sqliteHashData(i);
78238 sqlite3DefaultRowEst(pIdx);
 
78239 sqlite3DeleteIndexSamples(db, pIdx);
78240 pIdx->aSample = 0;
 
78241 }
78242
78243 /* Check to make sure the sqlite_stat1 table exists */
78244 sInfo.db = db;
78245 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -77098,23 +78247,91 @@
78247 return SQLITE_ERROR;
78248 }
78249
78250 /* Load new statistics out of the sqlite_stat1 table */
78251 zSql = sqlite3MPrintf(db,
78252 "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78253 if( zSql==0 ){
78254 rc = SQLITE_NOMEM;
78255 }else{
78256 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
78257 sqlite3DbFree(db, zSql);
78258 }
78259
78260
78261 /* Load the statistics from the sqlite_stat2 table. */
78262 #ifdef SQLITE_ENABLE_STAT2
78263 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78264 rc = SQLITE_ERROR;
78265 }
78266 if( rc==SQLITE_OK ){
78267 sqlite3_stmt *pStmt = 0;
78268
78269 zSql = sqlite3MPrintf(db,
78270 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78271 if( !zSql ){
78272 rc = SQLITE_NOMEM;
78273 }else{
78274 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78275 sqlite3DbFree(db, zSql);
78276 }
78277
78278 if( rc==SQLITE_OK ){
78279 while( sqlite3_step(pStmt)==SQLITE_ROW ){
78280 char *zIndex; /* Index name */
78281 Index *pIdx; /* Pointer to the index object */
78282
78283 zIndex = (char *)sqlite3_column_text(pStmt, 0);
78284 pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78285 if( pIdx ){
78286 int iSample = sqlite3_column_int(pStmt, 1);
78287 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78288 int eType = sqlite3_column_type(pStmt, 2);
78289
78290 if( pIdx->aSample==0 ){
78291 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78292 pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78293 if( pIdx->aSample==0 ){
78294 db->mallocFailed = 1;
78295 break;
78296 }
78297 memset(pIdx->aSample, 0, sz);
78298 }
78299
78300 assert( pIdx->aSample );
78301 {
78302 IndexSample *pSample = &pIdx->aSample[iSample];
78303 pSample->eType = (u8)eType;
78304 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78305 pSample->u.r = sqlite3_column_double(pStmt, 2);
78306 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78307 const char *z = (const char *)(
78308 (eType==SQLITE_BLOB) ?
78309 sqlite3_column_blob(pStmt, 2):
78310 sqlite3_column_text(pStmt, 2)
78311 );
78312 int n = sqlite3_column_bytes(pStmt, 2);
78313 if( n>24 ){
78314 n = 24;
78315 }
78316 pSample->nByte = (u8)n;
78317 if( n < 1){
78318 pSample->u.z = 0;
78319 }else{
78320 pSample->u.z = sqlite3DbStrNDup(0, z, n);
78321 if( pSample->u.z==0 ){
78322 db->mallocFailed = 1;
78323 break;
78324 }
78325 }
78326 }
78327 }
78328 }
78329 }
78330 }
78331 rc = sqlite3_finalize(pStmt);
78332 }
78333 }
78334 #endif
78335
78336 if( rc==SQLITE_NOMEM ){
78337 db->mallocFailed = 1;
@@ -79610,11 +80827,11 @@
80827 Table *p;
80828 int n;
80829 const char *z;
80830 Token sEnd;
80831 DbFixer sFix;
80832 Token *pName = 0;
80833 int iDb;
80834 sqlite3 *db = pParse->db;
80835
80836 if( pParse->nVar>0 ){
80837 sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
@@ -79926,15 +81143,11 @@
81143 Parse *pParse, /* The parsing context */
81144 int iDb, /* The database number */
81145 const char *zType, /* "idx" or "tbl" */
81146 const char *zName /* Name of index or table */
81147 ){
81148 static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
 
 
 
 
81149 int i;
81150 const char *zDbName = pParse->db->aDb[iDb].zName;
81151 for(i=0; i<ArraySize(azStatTab); i++){
81152 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
81153 sqlite3NestedParse(pParse,
@@ -79941,81 +81154,10 @@
81154 "DELETE FROM %Q.%s WHERE %s=%Q",
81155 zDbName, azStatTab[i], zType, zName
81156 );
81157 }
81158 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81159 }
81160
81161 /*
81162 ** This routine is called to do the work of a DROP TABLE statement.
81163 ** pName is the name of the table to be dropped.
@@ -80082,11 +81224,11 @@
81224 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
81225 goto exit_drop_table;
81226 }
81227 }
81228 #endif
81229 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
81230 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
81231 goto exit_drop_table;
81232 }
81233
81234 #ifndef SQLITE_OMIT_VIEW
@@ -80106,15 +81248,72 @@
81248 /* Generate code to remove the table from the master table
81249 ** on disk.
81250 */
81251 v = sqlite3GetVdbe(pParse);
81252 if( v ){
81253 Trigger *pTrigger;
81254 Db *pDb = &db->aDb[iDb];
81255 sqlite3BeginWriteOperation(pParse, 1, iDb);
81256
81257 #ifndef SQLITE_OMIT_VIRTUALTABLE
81258 if( IsVirtual(pTab) ){
81259 sqlite3VdbeAddOp0(v, OP_VBegin);
81260 }
81261 #endif
81262 sqlite3FkDropTable(pParse, pName, pTab);
81263
81264 /* Drop all triggers associated with the table being dropped. Code
81265 ** is generated to remove entries from sqlite_master and/or
81266 ** sqlite_temp_master if required.
81267 */
81268 pTrigger = sqlite3TriggerList(pParse, pTab);
81269 while( pTrigger ){
81270 assert( pTrigger->pSchema==pTab->pSchema ||
81271 pTrigger->pSchema==db->aDb[1].pSchema );
81272 sqlite3DropTriggerPtr(pParse, pTrigger);
81273 pTrigger = pTrigger->pNext;
81274 }
81275
81276 #ifndef SQLITE_OMIT_AUTOINCREMENT
81277 /* Remove any entries of the sqlite_sequence table associated with
81278 ** the table being dropped. This is done before the table is dropped
81279 ** at the btree level, in case the sqlite_sequence table needs to
81280 ** move as a result of the drop (can happen in auto-vacuum mode).
81281 */
81282 if( pTab->tabFlags & TF_Autoincrement ){
81283 sqlite3NestedParse(pParse,
81284 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81285 pDb->zName, pTab->zName
81286 );
81287 }
81288 #endif
81289
81290 /* Drop all SQLITE_MASTER table and index entries that refer to the
81291 ** table. The program name loops through the master table and deletes
81292 ** every row that refers to a table of the same name as the one being
81293 ** dropped. Triggers are handled seperately because a trigger can be
81294 ** created in the temp database that refers to a table in another
81295 ** database.
81296 */
81297 sqlite3NestedParse(pParse,
81298 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81299 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81300 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81301 if( !isView && !IsVirtual(pTab) ){
81302 destroyTable(pParse, pTab);
81303 }
81304
81305 /* Remove the table entry from SQLite's internal schema and modify
81306 ** the schema cookie.
81307 */
81308 if( IsVirtual(pTab) ){
81309 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81310 }
81311 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81312 sqlite3ChangeCookie(pParse, iDb);
81313 }
81314 sqliteViewResetAll(db, iDb);
81315
81316 exit_drop_table:
81317 sqlite3SrcListDelete(db, pName);
81318 }
81319
@@ -80278,11 +81477,13 @@
81477 */
81478 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
81479 Table *pTab = pIndex->pTable; /* The table that is indexed */
81480 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
81481 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81482 int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
81483 int addr1; /* Address of top of loop */
81484 int addr2; /* Address to jump to for next iteration */
81485 int tnum; /* Root page of index */
81486 Vdbe *v; /* Generate code into this virtual machine */
81487 KeyInfo *pKey; /* KeyInfo for index */
81488 int regIdxKey; /* Registers containing the index key */
81489 int regRecord; /* Register holding assemblied index record */
@@ -80311,14 +81512,45 @@
81512 sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
81513 (char *)pKey, P4_KEYINFO_HANDOFF);
81514 if( memRootPage>=0 ){
81515 sqlite3VdbeChangeP5(v, 1);
81516 }
81517
81518 #ifndef SQLITE_OMIT_MERGE_SORT
81519 /* Open the sorter cursor if we are to use one. */
81520 iSorter = pParse->nTab++;
81521 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81522 #endif
81523
81524 /* Open the table. Loop through all rows of the table, inserting index
81525 ** records into the sorter. */
81526 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
81527 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81528 addr2 = addr1 + 1;
81529 regRecord = sqlite3GetTempReg(pParse);
81530 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81531
81532 #ifndef SQLITE_OMIT_MERGE_SORT
81533 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
81534 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81535 sqlite3VdbeJumpHere(v, addr1);
81536 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
81537 if( pIndex->onError!=OE_None ){
81538 int j2 = sqlite3VdbeCurrentAddr(v) + 3;
81539 sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
81540 addr2 = sqlite3VdbeCurrentAddr(v);
81541 sqlite3VdbeAddOp3(v, OP_SorterCompare, iSorter, j2, regRecord);
81542 sqlite3HaltConstraint(
81543 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC
81544 );
81545 }else{
81546 addr2 = sqlite3VdbeCurrentAddr(v);
81547 }
81548 sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
81549 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
81550 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81551 #else
81552 if( pIndex->onError!=OE_None ){
81553 const int regRowid = regIdxKey + pIndex->nColumn;
81554 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
81555 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
81556
@@ -80333,17 +81565,20 @@
81565 */
81566 sqlite3VdbeAddOp4(v, OP_IsUnique, iIdx, j2, regRowid, pRegKey, P4_INT32);
81567 sqlite3HaltConstraint(
81568 pParse, OE_Abort, "indexed columns are not unique", P4_STATIC);
81569 }
81570 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 0);
81571 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81572 #endif
81573 sqlite3ReleaseTempReg(pParse, regRecord);
81574 sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
81575 sqlite3VdbeJumpHere(v, addr1);
81576
81577 sqlite3VdbeAddOp1(v, OP_Close, iTab);
81578 sqlite3VdbeAddOp1(v, OP_Close, iIdx);
81579 sqlite3VdbeAddOp1(v, OP_Close, iSorter);
81580 }
81581
81582 /*
81583 ** Create a new index for an SQL table. pName1.pName2 is the name of the index
81584 ** and pTblList is the name of the table that is to be indexed. Both will
@@ -80557,24 +81792,24 @@
81792 */
81793 nName = sqlite3Strlen30(zName);
81794 nCol = pList->nExpr;
81795 pIndex = sqlite3DbMallocZero(db,
81796 sizeof(Index) + /* Index structure */
 
81797 sizeof(int)*nCol + /* Index.aiColumn */
81798 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
81799 sizeof(char *)*nCol + /* Index.azColl */
81800 sizeof(u8)*nCol + /* Index.aSortOrder */
81801 nName + 1 + /* Index.zName */
81802 nExtra /* Collation sequence names */
81803 );
81804 if( db->mallocFailed ){
81805 goto exit_create_index;
81806 }
81807 pIndex->azColl = (char**)(&pIndex[1]);
 
81808 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
81809 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81810 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
81811 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
81812 zExtra = (char *)(&pIndex->zName[nName+1]);
81813 memcpy(pIndex->zName, zName, nName+1);
81814 pIndex->pTable = pTab;
81815 pIndex->nColumn = pList->nExpr;
@@ -80847,13 +82082,13 @@
82082 ** Apart from that, we have little to go on besides intuition as to
82083 ** how aiRowEst[] should be initialized. The numbers generated here
82084 ** are based on typical values found in actual indices.
82085 */
82086 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
82087 unsigned *a = pIdx->aiRowEst;
82088 int i;
82089 unsigned n;
82090 assert( a!=0 );
82091 a[0] = pIdx->pTable->nRowEst;
82092 if( a[0]<10 ) a[0] = 10;
82093 n = 10;
82094 for(i=1; i<=pIdx->nColumn; i++){
@@ -81293,12 +82528,13 @@
82528 ** The operator is "natural cross join". The A and B operands are stored
82529 ** in p->a[0] and p->a[1], respectively. The parser initially stores the
82530 ** operator with A. This routine shifts that operator over to B.
82531 */
82532 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
82533 if( p ){
82534 int i;
82535 assert( p->a || p->nSrc==0 );
82536 for(i=p->nSrc-1; i>0; i--){
82537 p->a[i].jointype = p->a[i-1].jointype;
82538 }
82539 p->a[0].jointype = 0;
82540 }
@@ -82850,10 +84086,12 @@
84086 **
84087 ** There is only one exported symbol in this file - the function
84088 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
84089 ** All other code has file scope.
84090 */
84091 /* #include <stdlib.h> */
84092 /* #include <assert.h> */
84093
84094 /*
84095 ** Return the collating function associated with a function.
84096 */
84097 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
@@ -85173,11 +86411,28 @@
86411 pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
86412 }else{
86413 pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
86414 }
86415 if( !pTo || locateFkeyIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
86416 assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
86417 if( !isIgnoreErrors || db->mallocFailed ) return;
86418 if( pTo==0 ){
86419 /* If isIgnoreErrors is true, then a table is being dropped. In this
86420 ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
86421 ** before actually dropping it in order to check FK constraints.
86422 ** If the parent table of an FK constraint on the current table is
86423 ** missing, behave as if it is empty. i.e. decrement the relevant
86424 ** FK counter for each row of the current table with non-NULL keys.
86425 */
86426 Vdbe *v = sqlite3GetVdbe(pParse);
86427 int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
86428 for(i=0; i<pFKey->nCol; i++){
86429 int iReg = pFKey->aCol[i].iFrom + regOld + 1;
86430 sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
86431 }
86432 sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
86433 }
86434 continue;
86435 }
86436 assert( pFKey->nCol==1 || (aiFree && pIdx) );
86437
86438 if( aiFree ){
@@ -88081,10 +89336,11 @@
89336
89337 #endif /* _SQLITE3EXT_H_ */
89338
89339 /************** End of sqlite3ext.h ******************************************/
89340 /************** Continuing where we left off in loadext.c ********************/
89341 /* #include <string.h> */
89342
89343 #ifndef SQLITE_OMIT_LOAD_EXTENSION
89344
89345 /*
89346 ** Some API routines are omitted when various features are
@@ -91527,16 +92783,22 @@
92783 ){
92784 Vdbe *v = pParse->pVdbe;
92785 int nExpr = pOrderBy->nExpr;
92786 int regBase = sqlite3GetTempRange(pParse, nExpr+2);
92787 int regRecord = sqlite3GetTempReg(pParse);
92788 int op;
92789 sqlite3ExprCacheClear(pParse);
92790 sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
92791 sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
92792 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
92793 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
92794 if( pSelect->selFlags & SF_UseSorter ){
92795 op = OP_SorterInsert;
92796 }else{
92797 op = OP_IdxInsert;
92798 }
92799 sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
92800 sqlite3ReleaseTempReg(pParse, regRecord);
92801 sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
92802 if( pSelect->iLimit ){
92803 int addr1, addr2;
92804 int iLimit;
@@ -92001,13 +93263,24 @@
93263 sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
93264 regRowid = 0;
93265 }else{
93266 regRowid = sqlite3GetTempReg(pParse);
93267 }
93268 if( p->selFlags & SF_UseSorter ){
93269 int regSortOut = ++pParse->nMem;
93270 int ptab2 = pParse->nTab++;
93271 sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
93272 addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
93273 codeOffset(v, p, addrContinue);
93274 sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
93275 sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
93276 sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
93277 }else{
93278 addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
93279 codeOffset(v, p, addrContinue);
93280 sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
93281 }
93282 switch( eDest ){
93283 case SRT_Table:
93284 case SRT_EphemTab: {
93285 testcase( eDest==SRT_Table );
93286 testcase( eDest==SRT_EphemTab );
@@ -92056,11 +93329,15 @@
93329 sqlite3ReleaseTempReg(pParse, regRowid);
93330
93331 /* The bottom of the loop
93332 */
93333 sqlite3VdbeResolveLabel(v, addrContinue);
93334 if( p->selFlags & SF_UseSorter ){
93335 sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
93336 }else{
93337 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
93338 }
93339 sqlite3VdbeResolveLabel(v, addrBreak);
93340 if( eDest==SRT_Output || eDest==SRT_Coroutine ){
93341 sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
93342 }
93343 }
@@ -95022,10 +96299,14 @@
96299 /* Set the limiter.
96300 */
96301 iEnd = sqlite3VdbeMakeLabel(v);
96302 p->nSelectRow = (double)LARGEST_INT64;
96303 computeLimitRegisters(pParse, p, iEnd);
96304 if( p->iLimit==0 && addrSortIndex>=0 ){
96305 sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
96306 p->selFlags |= SF_UseSorter;
96307 }
96308
96309 /* Open a virtual index to use for the distinct set.
96310 */
96311 if( p->selFlags & SF_Distinct ){
96312 KeyInfo *pKeyInfo;
@@ -95057,11 +96338,11 @@
96338 }
96339
96340 if( pWInfo->eDistinct ){
96341 VdbeOp *pOp; /* No longer required OpenEphemeral instr. */
96342
96343 assert( addrDistinctIndex>=0 );
96344 pOp = sqlite3VdbeGetOp(v, addrDistinctIndex);
96345
96346 assert( isDistinct );
96347 assert( pWInfo->eDistinct==WHERE_DISTINCT_ORDERED
96348 || pWInfo->eDistinct==WHERE_DISTINCT_UNIQUE
@@ -95116,10 +96397,12 @@
96397 ** one row of the input to the aggregator has been
96398 ** processed */
96399 int iAbortFlag; /* Mem address which causes query abort if positive */
96400 int groupBySort; /* Rows come from source in GROUP BY order */
96401 int addrEnd; /* End of processing for this SELECT */
96402 int sortPTab = 0; /* Pseudotable used to decode sorting results */
96403 int sortOut = 0; /* Output register from the sorter */
96404
96405 /* Remove any and all aliases between the result set and the
96406 ** GROUP BY clause.
96407 */
96408 if( pGroupBy ){
@@ -95177,16 +96460,16 @@
96460 int addrReset; /* Subroutine for resetting the accumulator */
96461 int regReset; /* Return address register for reset subroutine */
96462
96463 /* If there is a GROUP BY clause we might need a sorting index to
96464 ** implement it. Allocate that sorting index now. If it turns out
96465 ** that we do not need it after all, the OP_SorterOpen instruction
96466 ** will be converted into a Noop.
96467 */
96468 sAggInfo.sortingIdx = pParse->nTab++;
96469 pKeyInfo = keyInfoFromExprList(pParse, pGroupBy);
96470 addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
96471 sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
96472 0, (char*)pKeyInfo, P4_KEYINFO_HANDOFF);
96473
96474 /* Initialize memory locations used by GROUP BY aggregate processing
96475 */
@@ -95263,15 +96546,18 @@
96546 j++;
96547 }
96548 }
96549 regRecord = sqlite3GetTempReg(pParse);
96550 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
96551 sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
96552 sqlite3ReleaseTempReg(pParse, regRecord);
96553 sqlite3ReleaseTempRange(pParse, regBase, nCol);
96554 sqlite3WhereEnd(pWInfo);
96555 sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
96556 sortOut = sqlite3GetTempReg(pParse);
96557 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
96558 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
96559 VdbeComment((v, "GROUP BY sort"));
96560 sAggInfo.useSortingIdx = 1;
96561 sqlite3ExprCacheClear(pParse);
96562 }
96563
@@ -95280,13 +96566,17 @@
96566 ** Then compare the current GROUP BY terms against the GROUP BY terms
96567 ** from the previous row currently stored in a0, a1, a2...
96568 */
96569 addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
96570 sqlite3ExprCacheClear(pParse);
96571 if( groupBySort ){
96572 sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
96573 }
96574 for(j=0; j<pGroupBy->nExpr; j++){
96575 if( groupBySort ){
96576 sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
96577 if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
96578 }else{
96579 sAggInfo.directMode = 1;
96580 sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
96581 }
96582 }
@@ -95321,11 +96611,11 @@
96611 VdbeComment((v, "indicate data in accumulator"));
96612
96613 /* End of the loop
96614 */
96615 if( groupBySort ){
96616 sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
96617 }else{
96618 sqlite3WhereEnd(pWInfo);
96619 sqlite3VdbeChangeToNoop(v, addrSortingIdx, 1);
96620 }
96621
@@ -95650,10 +96940,12 @@
96940 ** interface routine of sqlite3_exec().
96941 **
96942 ** These routines are in a separate files so that they will not be linked
96943 ** if they are not used.
96944 */
96945 /* #include <stdlib.h> */
96946 /* #include <string.h> */
96947
96948 #ifndef SQLITE_OMIT_GET_TABLE
96949
96950 /*
96951 ** This structure is used to pass data from sqlite3_get_table() through
@@ -99159,11 +100451,11 @@
100451 #define TERM_CODED 0x04 /* This term is already coded */
100452 #define TERM_COPIED 0x08 /* Has a child */
100453 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
100454 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
100455 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
100456 #ifdef SQLITE_ENABLE_STAT2
100457 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
100458 #else
100459 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
100460 #endif
100461
@@ -100373,11 +101665,11 @@
101665 pNewTerm->prereqAll = pTerm->prereqAll;
101666 }
101667 }
101668 #endif /* SQLITE_OMIT_VIRTUALTABLE */
101669
101670 #ifdef SQLITE_ENABLE_STAT2
101671 /* When sqlite_stat2 histogram data is available an operator of the
101672 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101673 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
101674 ** virtual term of that form.
101675 **
@@ -100412,11 +101704,11 @@
101704 pTerm->nChild = 1;
101705 pTerm->wtFlags |= TERM_COPIED;
101706 pNewTerm->prereqAll = pTerm->prereqAll;
101707 }
101708 }
101709 #endif /* SQLITE_ENABLE_STAT2 */
101710
101711 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
101712 ** an index for tables to the left of the join.
101713 */
101714 pTerm->prereqRight |= extraRight;
@@ -101461,89 +102753,71 @@
102753 */
102754 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102755 }
102756 #endif /* SQLITE_OMIT_VIRTUALTABLE */
102757
 
102758 /*
102759 ** Argument pIdx is a pointer to an index structure that has an array of
102760 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102761 ** stored in Index.aSample. These samples divide the domain of values stored
102762 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102763 ** Region 0 contains all values less than the first sample value. Region
102764 ** 1 contains values between the first and second samples. Region 2 contains
102765 ** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102766 ** contains values larger than the last sample.
102767 **
102768 ** If the index contains many duplicates of a single value, then it is
102769 ** possible that two or more adjacent samples can hold the same value.
102770 ** When that is the case, the smallest possible region code is returned
102771 ** when roundUp is false and the largest possible region code is returned
102772 ** when roundUp is true.
102773 **
102774 ** If successful, this function determines which of the regions value
102775 ** pVal lies in, sets *piRegion to the region index (a value between 0
102776 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102777 ** Or, if an OOM occurs while converting text values between encodings,
102778 ** SQLITE_NOMEM is returned and *piRegion is undefined.
102779 */
102780 #ifdef SQLITE_ENABLE_STAT2
102781 static int whereRangeRegion(
102782 Parse *pParse, /* Database connection */
102783 Index *pIdx, /* Index to consider domain of */
102784 sqlite3_value *pVal, /* Value to consider */
102785 int roundUp, /* Return largest valid region if true */
102786 int *piRegion /* OUT: Region of domain in which value lies */
102787 ){
 
 
 
 
 
 
 
102788 assert( roundUp==0 || roundUp==1 );
102789 if( ALWAYS(pVal) ){
102790 IndexSample *aSample = pIdx->aSample;
102791 int i = 0;
102792 int eType = sqlite3_value_type(pVal);
102793
102794 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102795 double r = sqlite3_value_double(pVal);
102796 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102797 if( aSample[i].eType==SQLITE_NULL ) continue;
102798 if( aSample[i].eType>=SQLITE_TEXT ) break;
102799 if( roundUp ){
102800 if( aSample[i].u.r>r ) break;
102801 }else{
102802 if( aSample[i].u.r>=r ) break;
102803 }
102804 }
102805 }else if( eType==SQLITE_NULL ){
102806 i = 0;
102807 if( roundUp ){
102808 while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102809 }
102810 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102811 sqlite3 *db = pParse->db;
102812 CollSeq *pColl;
102813 const u8 *z;
102814 int n;
102815
102816 /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102817 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102818
102819 if( eType==SQLITE_BLOB ){
102820 z = (const u8 *)sqlite3_value_blob(pVal);
102821 pColl = db->pDfltColl;
102822 assert( pColl->enc==SQLITE_UTF8 );
102823 }else{
@@ -101558,16 +102832,16 @@
102832 return SQLITE_NOMEM;
102833 }
102834 assert( z && pColl && pColl->xCmp );
102835 }
102836 n = sqlite3ValueBytes(pVal, pColl->enc);
102837
102838 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102839 int c;
102840 int eSampletype = aSample[i].eType;
102841 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102842 if( (eSampletype!=eType) ) break;
102843 #ifndef SQLITE_OMIT_UTF16
102844 if( pColl->enc!=SQLITE_UTF8 ){
102845 int nSample;
102846 char *zSample = sqlite3Utf8to16(
102847 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -101581,52 +102855,20 @@
102855 }else
102856 #endif
102857 {
102858 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102859 }
102860 if( c-roundUp>=0 ) break;
102861 }
102862 }
102863
102864 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102865 *piRegion = i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102866 }
102867 return SQLITE_OK;
102868 }
102869 #endif /* #ifdef SQLITE_ENABLE_STAT2 */
102870
102871 /*
102872 ** If expression pExpr represents a literal value, set *pp to point to
102873 ** an sqlite3_value structure containing the same value, with affinity
102874 ** aff applied to it, before returning. It is the responsibility of the
@@ -101640,11 +102882,11 @@
102882 **
102883 ** If neither of the above apply, set *pp to NULL.
102884 **
102885 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102886 */
102887 #ifdef SQLITE_ENABLE_STAT2
102888 static int valueFromExpr(
102889 Parse *pParse,
102890 Expr *pExpr,
102891 u8 aff,
102892 sqlite3_value **pp
@@ -101688,92 +102930,106 @@
102930 **
102931 ** ... FROM t1 WHERE a > ? AND a < ? ...
102932 **
102933 ** then nEq should be passed 0.
102934 **
102935 ** The returned value is an integer between 1 and 100, inclusive. A return
102936 ** value of 1 indicates that the proposed range scan is expected to visit
102937 ** approximately 1/100th (1%) of the rows selected by the nEq equality
102938 ** constraints (if any). A return value of 100 indicates that it is expected
102939 ** that the range scan will visit every row (100%) selected by the equality
102940 ** constraints.
102941 **
102942 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102943 ** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102944 ** results in a return of 25 and a range constraint (x>? AND x<?) results
102945 ** in a return of 6.
102946 */
102947 static int whereRangeScanEst(
102948 Parse *pParse, /* Parsing & code generating context */
102949 Index *p, /* The index containing the range-compared column; "x" */
102950 int nEq, /* index into p->aCol[] of the range-compared column */
102951 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
102952 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
102953 int *piEst /* OUT: Return value */
102954 ){
102955 int rc = SQLITE_OK;
102956
102957 #ifdef SQLITE_ENABLE_STAT2
102958
102959 if( nEq==0 && p->aSample ){
102960 sqlite3_value *pLowerVal = 0;
102961 sqlite3_value *pUpperVal = 0;
102962 int iEst;
102963 int iLower = 0;
102964 int iUpper = SQLITE_INDEX_SAMPLES;
102965 int roundUpUpper = 0;
102966 int roundUpLower = 0;
102967 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102968
102969 if( pLower ){
102970 Expr *pExpr = pLower->pExpr->pRight;
102971 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
102972 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102973 roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
 
 
 
 
 
 
102974 }
102975 if( rc==SQLITE_OK && pUpper ){
102976 Expr *pExpr = pUpper->pExpr->pRight;
102977 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
102978 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102979 roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102980 }
102981
102982 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102983 sqlite3ValueFree(pLowerVal);
102984 sqlite3ValueFree(pUpperVal);
102985 goto range_est_fallback;
102986 }else if( pLowerVal==0 ){
102987 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102988 if( pLower ) iLower = iUpper/2;
102989 }else if( pUpperVal==0 ){
102990 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102991 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
102992 }else{
102993 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
102994 if( rc==SQLITE_OK ){
102995 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
102996 }
102997 }
102998 WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
102999
103000 iEst = iUpper - iLower;
103001 testcase( iEst==SQLITE_INDEX_SAMPLES );
103002 assert( iEst<=SQLITE_INDEX_SAMPLES );
103003 if( iEst<1 ){
103004 *piEst = 50/SQLITE_INDEX_SAMPLES;
103005 }else{
103006 *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103007 }
103008 sqlite3ValueFree(pLowerVal);
103009 sqlite3ValueFree(pUpperVal);
103010 return rc;
103011 }
103012 range_est_fallback:
103013 #else
103014 UNUSED_PARAMETER(pParse);
103015 UNUSED_PARAMETER(p);
103016 UNUSED_PARAMETER(nEq);
103017 #endif
103018 assert( pLower || pUpper );
103019 *piEst = 100;
103020 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103021 if( pUpper ) *piEst /= 4;
103022 return rc;
103023 }
103024
103025 #ifdef SQLITE_ENABLE_STAT2
103026 /*
103027 ** Estimate the number of rows that will be returned based on
103028 ** an equality constraint x=VALUE and where that VALUE occurs in
103029 ** the histogram data. This only works when x is the left-most
103030 ** column of an index and sqlite_stat2 histogram data is available
103031 ** for that index. When pExpr==NULL that means the constraint is
103032 ** "x IS NULL" instead of "x=VALUE".
103033 **
103034 ** Write the estimated row count into *pnRow and return SQLITE_OK.
103035 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -101789,13 +103045,14 @@
103045 Index *p, /* The index whose left-most column is pTerm */
103046 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
103047 double *pnRow /* Write the revised row estimate here */
103048 ){
103049 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
103050 int iLower, iUpper; /* Range of histogram regions containing pRhs */
103051 u8 aff; /* Column affinity */
103052 int rc; /* Subfunction return code */
103053 double nRowEst; /* New estimate of the number of rows */
103054
103055 assert( p->aSample!=0 );
103056 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103057 if( pExpr ){
103058 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
@@ -101802,22 +103059,30 @@
103059 if( rc ) goto whereEqualScanEst_cancel;
103060 }else{
103061 pRhs = sqlite3ValueNew(pParse->db);
103062 }
103063 if( pRhs==0 ) return SQLITE_NOTFOUND;
103064 rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103065 if( rc ) goto whereEqualScanEst_cancel;
103066 rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103067 if( rc ) goto whereEqualScanEst_cancel;
103068 WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103069 if( iLower>=iUpper ){
103070 nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103071 if( nRowEst<*pnRow ) *pnRow = nRowEst;
103072 }else{
103073 nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103074 *pnRow = nRowEst;
103075 }
103076
103077 whereEqualScanEst_cancel:
103078 sqlite3ValueFree(pRhs);
103079 return rc;
103080 }
103081 #endif /* defined(SQLITE_ENABLE_STAT2) */
103082
103083 #ifdef SQLITE_ENABLE_STAT2
103084 /*
103085 ** Estimate the number of rows that will be returned based on
103086 ** an IN constraint where the right-hand side of the IN operator
103087 ** is a list of values. Example:
103088 **
@@ -101836,29 +103101,64 @@
103101 Parse *pParse, /* Parsing & code generating context */
103102 Index *p, /* The index whose left-most column is pTerm */
103103 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103104 double *pnRow /* Write the revised row estimate here */
103105 ){
103106 sqlite3_value *pVal = 0; /* One value from list */
103107 int iLower, iUpper; /* Range of histogram regions containing pRhs */
103108 u8 aff; /* Column affinity */
103109 int rc = SQLITE_OK; /* Subfunction return code */
103110 double nRowEst; /* New estimate of the number of rows */
103111 int nSpan = 0; /* Number of histogram regions spanned */
103112 int nSingle = 0; /* Histogram regions hit by a single value */
103113 int nNotFound = 0; /* Count of values that are not constants */
103114 int i; /* Loop counter */
103115 u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
103116 u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
103117
103118 assert( p->aSample!=0 );
103119 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103120 memset(aSpan, 0, sizeof(aSpan));
103121 memset(aSingle, 0, sizeof(aSingle));
103122 for(i=0; i<pList->nExpr; i++){
103123 sqlite3ValueFree(pVal);
103124 rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103125 if( rc ) break;
103126 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103127 nNotFound++;
103128 continue;
103129 }
103130 rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103131 if( rc ) break;
103132 rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103133 if( rc ) break;
103134 if( iLower>=iUpper ){
103135 aSingle[iLower] = 1;
103136 }else{
103137 assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103138 while( iLower<iUpper ) aSpan[iLower++] = 1;
103139 }
103140 }
103141 if( rc==SQLITE_OK ){
103142 for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103143 if( aSpan[i] ){
103144 nSpan++;
103145 }else if( aSingle[i] ){
103146 nSingle++;
103147 }
103148 }
103149 nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103150 + nNotFound*p->aiRowEst[1];
103151 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103152 *pnRow = nRowEst;
103153 WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103154 nSpan, nSingle, nNotFound, nRowEst));
103155 }
103156 sqlite3ValueFree(pVal);
103157 return rc;
103158 }
103159 #endif /* defined(SQLITE_ENABLE_STAT2) */
103160
103161
103162 /*
103163 ** Find the best query plan for accessing a particular table. Write the
103164 ** best query plan and its cost into the WhereCost object supplied as the
@@ -101901,11 +103201,11 @@
103201 Index *pProbe; /* An index we are evaluating */
103202 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
103203 int eqTermMask; /* Current mask of valid equality operators */
103204 int idxEqTermMask; /* Index mask of valid equality operators */
103205 Index sPk; /* A fake index object for the primary key */
103206 unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103207 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
103208 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
103209
103210 /* Initialize the cost to a worst-case value */
103211 memset(pCost, 0, sizeof(*pCost));
@@ -101956,11 +103256,11 @@
103256 }
103257
103258 /* Loop over all indices looking for the best one to use
103259 */
103260 for(; pProbe; pIdx=pProbe=pProbe->pNext){
103261 const unsigned int * const aiRowEst = pProbe->aiRowEst;
103262 double cost; /* Cost of using pProbe */
103263 double nRow; /* Estimated number of rows in result set */
103264 double log10N; /* base-10 logarithm of nRow (inexact) */
103265 int rev; /* True to scan in reverse order */
103266 int wsFlags = 0;
@@ -101999,16 +103299,18 @@
103299 ** Set to true if there was at least one "x IN (SELECT ...)" term used
103300 ** in determining the value of nInMul. Note that the RHS of the
103301 ** IN operator must be a SELECT, not a value list, for this variable
103302 ** to be true.
103303 **
103304 ** estBound:
103305 ** An estimate on the amount of the table that must be searched. A
103306 ** value of 100 means the entire table is searched. Range constraints
103307 ** might reduce this to a value less than 100 to indicate that only
103308 ** a fraction of the table needs searching. In the absence of
103309 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103310 ** space to 1/4rd its original size. So an x>? constraint reduces
103311 ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
103312 **
103313 ** bSort:
103314 ** Boolean. True if there is an ORDER BY clause that will require an
103315 ** external sort (i.e. scanning the index being evaluated will not
103316 ** correctly order records).
@@ -102029,17 +103331,17 @@
103331 ** SELECT a, b, c FROM tbl WHERE a = 1;
103332 */
103333 int nEq; /* Number of == or IN terms matching index */
103334 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
103335 int nInMul = 1; /* Number of distinct equalities to lookup */
103336 int estBound = 100; /* Estimated reduction in search space */
103337 int nBound = 0; /* Number of range constraints seen */
103338 int bSort = !!pOrderBy; /* True if external sort required */
103339 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
103340 int bLookup = 0; /* True if not a covering index */
103341 WhereTerm *pTerm; /* A single term of the WHERE clause */
103342 #ifdef SQLITE_ENABLE_STAT2
103343 WhereTerm *pFirstTerm = 0; /* First term matching the index */
103344 #endif
103345
103346 /* Determine the values of nEq and nInMul */
103347 for(nEq=0; nEq<pProbe->nColumn; nEq++){
@@ -102059,23 +103361,23 @@
103361 nInMul *= pExpr->x.pList->nExpr;
103362 }
103363 }else if( pTerm->eOperator & WO_ISNULL ){
103364 wsFlags |= WHERE_COLUMN_NULL;
103365 }
103366 #ifdef SQLITE_ENABLE_STAT2
103367 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103368 #endif
103369 used |= pTerm->prereqRight;
103370 }
103371
103372 /* Determine the value of estBound. */
103373 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103374 int j = pProbe->aiColumn[nEq];
103375 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103376 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103377 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103378 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
103379 if( pTop ){
103380 nBound = 1;
103381 wsFlags |= WHERE_TOP_LIMIT;
103382 used |= pTop->prereqRight;
103383 }
@@ -102143,11 +103445,11 @@
103445 if( bInEst && nRow*2>aiRowEst[0] ){
103446 nRow = aiRowEst[0]/2;
103447 nInMul = (int)(nRow / aiRowEst[nEq]);
103448 }
103449
103450 #ifdef SQLITE_ENABLE_STAT2
103451 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103452 ** and we do not think that values of x are unique and if histogram
103453 ** data is available for column x, then it might be possible
103454 ** to get a better estimate on the number of rows based on
103455 ** VALUE and how common that value is according to the histogram.
@@ -102159,16 +103461,16 @@
103461 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103462 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
103463 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103464 }
103465 }
103466 #endif /* SQLITE_ENABLE_STAT2 */
103467
103468 /* Adjust the number of output rows and downward to reflect rows
103469 ** that are excluded by range constraints.
103470 */
103471 nRow = (nRow * (double)estBound) / (double)100;
103472 if( nRow<1 ) nRow = 1;
103473
103474 /* Experiments run on real SQLite databases show that the time needed
103475 ** to do a binary search to locate a row in a table or index is roughly
103476 ** log10(N) times the time to move from one row to the next row within
@@ -102293,14 +103595,14 @@
103595 if( nRow<2 ) nRow = 2;
103596 }
103597
103598
103599 WHERETRACE((
103600 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103601 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103602 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103603 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
103604 notReady, log10N, nRow, cost, used
103605 ));
103606
103607 /* If this index is the best we have seen so far, then record this
103608 ** index and its cost in the pCost structure.
@@ -104227,10 +105529,11 @@
105529 ** LALR(1) grammar but which are always false in the
105530 ** specific grammar used by SQLite.
105531 */
105532 /* First off, code is included that follows the "include" declaration
105533 ** in the input grammar file. */
105534 /* #include <stdio.h> */
105535
105536
105537 /*
105538 ** Disable all error recovery processing in the parser push-down
105539 ** automaton.
@@ -105087,10 +106390,11 @@
106390 #endif
106391 };
106392 typedef struct yyParser yyParser;
106393
106394 #ifndef NDEBUG
106395 /* #include <stdio.h> */
106396 static FILE *yyTraceFILE = 0;
106397 static char *yyTracePrompt = 0;
106398 #endif /* NDEBUG */
106399
106400 #ifndef NDEBUG
@@ -107662,10 +108966,11 @@
108966 **
108967 ** This file contains C code that splits an SQL input string up into
108968 ** individual tokens and sends those tokens one-by-one over to the
108969 ** parser for analysis.
108970 */
108971 /* #include <stdlib.h> */
108972
108973 /*
108974 ** The charMap() macro maps alphabetic characters into their
108975 ** lower-case ASCII equivalent. On ASCII machines, this is just
108976 ** an upper-to-lower case map. On EBCDIC machines we also need
@@ -109053,10 +110358,20 @@
110358 memcpy(&y, &x, 8);
110359 assert( sqlite3IsNaN(y) );
110360 }
110361 #endif
110362 #endif
110363
110364 /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
110365 ** compile-time option.
110366 */
110367 #ifdef SQLITE_EXTRA_INIT
110368 if( rc==SQLITE_OK && sqlite3GlobalConfig.isInit ){
110369 int SQLITE_EXTRA_INIT(void);
110370 rc = SQLITE_EXTRA_INIT();
110371 }
110372 #endif
110373
110374 return rc;
110375 }
110376
110377 /*
@@ -113184,10 +114499,16 @@
114499
114500 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
114501 # define SQLITE_CORE 1
114502 #endif
114503
114504 /* #include <assert.h> */
114505 /* #include <stdlib.h> */
114506 /* #include <stddef.h> */
114507 /* #include <stdio.h> */
114508 /* #include <string.h> */
114509 /* #include <stdarg.h> */
114510
114511 #ifndef SQLITE_CORE
114512 SQLITE_EXTENSION_INIT1
114513 #endif
114514
@@ -117705,10 +119026,12 @@
119026 ******************************************************************************
119027 **
119028 */
119029 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
119030
119031 /* #include <string.h> */
119032 /* #include <assert.h> */
119033
119034 typedef struct Fts3auxTable Fts3auxTable;
119035 typedef struct Fts3auxCursor Fts3auxCursor;
119036
119037 struct Fts3auxTable {
@@ -118243,10 +119566,12 @@
119566 /*
119567 ** Default span for NEAR operators.
119568 */
119569 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
119570
119571 /* #include <string.h> */
119572 /* #include <assert.h> */
119573
119574 /*
119575 ** isNot:
119576 ** This variable is used by function getNextNode(). When getNextNode() is
119577 ** called, it sets ParseContext.isNot to true if the 'next node' is a
@@ -118944,10 +120269,11 @@
120269 ** Everything after this point is just test code.
120270 */
120271
120272 #ifdef SQLITE_TEST
120273
120274 /* #include <stdio.h> */
120275
120276 /*
120277 ** Function to query the hash-table of tokenizers (see README.tokenizers).
120278 */
120279 static int queryTestTokenizer(
@@ -119154,10 +120480,13 @@
120480 ** * The FTS3 module is being built into the core of
120481 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120482 */
120483 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120484
120485 /* #include <assert.h> */
120486 /* #include <stdlib.h> */
120487 /* #include <string.h> */
120488
120489
120490 /*
120491 ** Malloc and Free functions
120492 */
@@ -119534,10 +120863,14 @@
120863 ** * The FTS3 module is being built into the core of
120864 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
120865 */
120866 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
120867
120868 /* #include <assert.h> */
120869 /* #include <stdlib.h> */
120870 /* #include <stdio.h> */
120871 /* #include <string.h> */
120872
120873
120874 /*
120875 ** Class derived from sqlite3_tokenizer
120876 */
@@ -120177,10 +121510,12 @@
121510 ** * The FTS3 module is being built into the core of
121511 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
121512 */
121513 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
121514
121515 /* #include <assert.h> */
121516 /* #include <string.h> */
121517
121518 /*
121519 ** Implementation of the SQL scalar function for accessing the underlying
121520 ** hash table. This function may be called as follows:
121521 **
@@ -120352,10 +121687,12 @@
121687 }
121688
121689
121690 #ifdef SQLITE_TEST
121691
121692 /* #include <tcl.h> */
121693 /* #include <string.h> */
121694
121695 /*
121696 ** Implementation of a special SQL scalar function for testing tokenizers
121697 ** designed to be used in concert with the Tcl testing framework. This
121698 ** function must be called with two arguments:
@@ -120663,10 +122000,14 @@
122000 ** * The FTS3 module is being built into the core of
122001 ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
122002 */
122003 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
122004
122005 /* #include <assert.h> */
122006 /* #include <stdlib.h> */
122007 /* #include <stdio.h> */
122008 /* #include <string.h> */
122009
122010
122011 typedef struct simple_tokenizer {
122012 sqlite3_tokenizer base;
122013 char delim[128]; /* flag ASCII delimiters */
@@ -120888,10 +122229,13 @@
122229 ** code in fts3.c.
122230 */
122231
122232 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
122233
122234 /* #include <string.h> */
122235 /* #include <assert.h> */
122236 /* #include <stdlib.h> */
122237
122238 /*
122239 ** When full-text index nodes are loaded from disk, the buffer that they
122240 ** are loaded into has the following number of bytes of padding at the end
122241 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
@@ -124149,10 +125493,12 @@
125493 ******************************************************************************
125494 */
125495
125496 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
125497
125498 /* #include <string.h> */
125499 /* #include <assert.h> */
125500
125501 /*
125502 ** Characters that may appear in the second argument to matchinfo().
125503 */
125504 #define FTS3_MATCHINFO_NPHRASE 'p' /* 1 value */
@@ -125736,10 +127082,12 @@
127082 #ifndef SQLITE_CORE
127083 SQLITE_EXTENSION_INIT1
127084 #else
127085 #endif
127086
127087 /* #include <string.h> */
127088 /* #include <assert.h> */
127089
127090 #ifndef SQLITE_AMALGAMATION
127091 #include "sqlite3rtree.h"
127092 typedef sqlite3_int64 i64;
127093 typedef unsigned char u8;
@@ -128950,10 +130298,11 @@
130298 #include <unicode/utypes.h>
130299 #include <unicode/uregex.h>
130300 #include <unicode/ustring.h>
130301 #include <unicode/ucol.h>
130302
130303 /* #include <assert.h> */
130304
130305 #ifndef SQLITE_CORE
130306 SQLITE_EXTENSION_INIT1
130307 #else
130308 #endif
@@ -129429,12 +130778,16 @@
130778 ** This file implements a tokenizer for fts3 based on the ICU library.
130779 */
130780 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130781 #ifdef SQLITE_ENABLE_ICU
130782
130783 /* #include <assert.h> */
130784 /* #include <string.h> */
130785
130786 #include <unicode/ubrk.h>
130787 /* #include <unicode/ucol.h> */
130788 /* #include <unicode/ustring.h> */
130789 #include <unicode/utf16.h>
130790
130791 typedef struct IcuTokenizer IcuTokenizer;
130792 typedef struct IcuCursor IcuCursor;
130793
130794
+5 -11
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.7.8"
111111
#define SQLITE_VERSION_NUMBER 3007008
112
-#define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
112
+#define SQLITE_SOURCE_ID "2011-09-04 01:27:00 6b657ae75035eb10b0ad640998d3c9eadfdffa6e"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -1208,20 +1208,14 @@
12081208
** also used during testing of SQLite in order to specify an alternative
12091209
** memory allocator that simulates memory out-of-memory conditions in
12101210
** order to verify that SQLite recovers gracefully from such
12111211
** conditions.
12121212
**
1213
-** The xMalloc and xFree methods must work like the
1214
-** malloc() and free() functions from the standard C library.
1215
-** The xRealloc method must work like realloc() from the standard C library
1216
-** with the exception that if the second argument to xRealloc is zero,
1217
-** xRealloc must be a no-op - it must not perform any allocation or
1218
-** deallocation. ^SQLite guarantees that the second argument to
1213
+** The xMalloc, xRealloc, and xFree methods must work like the
1214
+** malloc(), realloc() and free() functions from the standard C library.
1215
+** ^SQLite guarantees that the second argument to
12191216
** xRealloc is always a value returned by a prior call to xRoundup.
1220
-** And so in cases where xRoundup always returns a positive number,
1221
-** xRealloc can perform exactly as the standard library realloc() and
1222
-** still be in compliance with this specification.
12231217
**
12241218
** xSize should return the allocated size of a memory allocation
12251219
** previously obtained from xMalloc or xRealloc. The allocated size
12261220
** is always at least as big as the requested size but may be larger.
12271221
**
@@ -2854,11 +2848,11 @@
28542848
** a schema change, on the first [sqlite3_step()] call following any change
28552849
** to the [sqlite3_bind_text | bindings] of that [parameter].
28562850
** ^The specific value of WHERE-clause [parameter] might influence the
28572851
** choice of query plan if the parameter is the left-hand side of a [LIKE]
28582852
** or [GLOB] operator or if the parameter is compared to an indexed column
2859
-** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
2853
+** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
28602854
** the
28612855
** </li>
28622856
** </ol>
28632857
*/
28642858
SQLITE_API int sqlite3_prepare(
28652859
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.8"
111 #define SQLITE_VERSION_NUMBER 3007008
112 #define SQLITE_SOURCE_ID "2011-08-16 02:07:04 9650d7962804d61f56cac944ff9bb2c7bc111957"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1208,20 +1208,14 @@
1208 ** also used during testing of SQLite in order to specify an alternative
1209 ** memory allocator that simulates memory out-of-memory conditions in
1210 ** order to verify that SQLite recovers gracefully from such
1211 ** conditions.
1212 **
1213 ** The xMalloc and xFree methods must work like the
1214 ** malloc() and free() functions from the standard C library.
1215 ** The xRealloc method must work like realloc() from the standard C library
1216 ** with the exception that if the second argument to xRealloc is zero,
1217 ** xRealloc must be a no-op - it must not perform any allocation or
1218 ** deallocation. ^SQLite guarantees that the second argument to
1219 ** xRealloc is always a value returned by a prior call to xRoundup.
1220 ** And so in cases where xRoundup always returns a positive number,
1221 ** xRealloc can perform exactly as the standard library realloc() and
1222 ** still be in compliance with this specification.
1223 **
1224 ** xSize should return the allocated size of a memory allocation
1225 ** previously obtained from xMalloc or xRealloc. The allocated size
1226 ** is always at least as big as the requested size but may be larger.
1227 **
@@ -2854,11 +2848,11 @@
2854 ** a schema change, on the first [sqlite3_step()] call following any change
2855 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2856 ** ^The specific value of WHERE-clause [parameter] might influence the
2857 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2858 ** or [GLOB] operator or if the parameter is compared to an indexed column
2859 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
2860 ** the
2861 ** </li>
2862 ** </ol>
2863 */
2864 SQLITE_API int sqlite3_prepare(
2865
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.8"
111 #define SQLITE_VERSION_NUMBER 3007008
112 #define SQLITE_SOURCE_ID "2011-09-04 01:27:00 6b657ae75035eb10b0ad640998d3c9eadfdffa6e"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -1208,20 +1208,14 @@
1208 ** also used during testing of SQLite in order to specify an alternative
1209 ** memory allocator that simulates memory out-of-memory conditions in
1210 ** order to verify that SQLite recovers gracefully from such
1211 ** conditions.
1212 **
1213 ** The xMalloc, xRealloc, and xFree methods must work like the
1214 ** malloc(), realloc() and free() functions from the standard C library.
1215 ** ^SQLite guarantees that the second argument to
 
 
 
1216 ** xRealloc is always a value returned by a prior call to xRoundup.
 
 
 
1217 **
1218 ** xSize should return the allocated size of a memory allocation
1219 ** previously obtained from xMalloc or xRealloc. The allocated size
1220 ** is always at least as big as the requested size but may be larger.
1221 **
@@ -2854,11 +2848,11 @@
2848 ** a schema change, on the first [sqlite3_step()] call following any change
2849 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2850 ** ^The specific value of WHERE-clause [parameter] might influence the
2851 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2852 ** or [GLOB] operator or if the parameter is compared to an indexed column
2853 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2854 ** the
2855 ** </li>
2856 ** </ol>
2857 */
2858 SQLITE_API int sqlite3_prepare(
2859
+88 -36
--- src/stash.c
+++ src/stash.c
@@ -35,10 +35,11 @@
3535
@ stashid INTEGER REFERENCES stash, -- Stash that contains this file
3636
@ rid INTEGER, -- Baseline content in BLOB table or 0.
3737
@ isAdded BOOLEAN, -- True if this is an added file
3838
@ isRemoved BOOLEAN, -- True if this file is deleted
3939
@ isExec BOOLEAN, -- True if file is executable
40
+@ isLink BOOLEAN, -- True if file is a symlink
4041
@ origname TEXT, -- Original filename
4142
@ newname TEXT, -- New name for file at next check-in
4243
@ delta BLOB, -- Delta from baseline. Content if rid=0
4344
@ PRIMARY KEY(origname, stashid)
4445
@ );
@@ -61,11 +62,11 @@
6162
zFile = mprintf("%/", zFName);
6263
file_tree_name(zFile, &fname, 1);
6364
zTreename = blob_str(&fname);
6465
blob_zero(&sql);
6566
blob_appendf(&sql,
66
- "SELECT deleted, isexe, mrid, pathname, coalesce(origname,pathname)"
67
+ "SELECT deleted, isexe, islink, mrid, pathname, coalesce(origname,pathname)"
6768
" FROM vfile"
6869
" WHERE vid=%d AND (chnged OR deleted OR origname NOT NULL OR mrid==0)",
6970
vid
7071
);
7172
if( fossil_strcmp(zTreename,".")!=0 ){
@@ -76,47 +77,60 @@
7677
);
7778
}
7879
db_prepare(&q, blob_str(&sql));
7980
blob_reset(&sql);
8081
db_prepare(&ins,
81
- "INSERT INTO stashfile(stashid, rid, isAdded, isRemoved, isExec,"
82
+ "INSERT INTO stashfile(stashid, rid, isAdded, isRemoved, isExec, isLink,"
8283
"origname, newname, delta)"
83
- "VALUES(%d,:rid,:isadd,:isrm,:isexe,:orig,:new,:content)",
84
+ "VALUES(%d,:rid,:isadd,:isrm,:isexe,:islink,:orig,:new,:content)",
8485
stashid
8586
);
8687
while( db_step(&q)==SQLITE_ROW ){
8788
int deleted = db_column_int(&q, 0);
88
- int rid = db_column_int(&q, 2);
89
- const char *zName = db_column_text(&q, 3);
90
- const char *zOrig = db_column_text(&q, 4);
89
+ int rid = db_column_int(&q, 3);
90
+ const char *zName = db_column_text(&q, 4);
91
+ const char *zOrig = db_column_text(&q, 5);
9192
char *zPath = mprintf("%s%s", g.zLocalRoot, zName);
9293
Blob content;
94
+ int isNewLink = file_islink(zPath);
9395
9496
db_bind_int(&ins, ":rid", rid);
9597
db_bind_int(&ins, ":isadd", rid==0);
9698
db_bind_int(&ins, ":isrm", deleted);
9799
db_bind_int(&ins, ":isexe", db_column_int(&q, 1));
100
+ db_bind_int(&ins, ":islink", db_column_int(&q, 2));
98101
db_bind_text(&ins, ":orig", zOrig);
99102
db_bind_text(&ins, ":new", zName);
103
+
100104
if( rid==0 ){
101105
/* A new file */
102
- blob_read_from_file(&content, zPath);
106
+ if( isNewLink ){
107
+ blob_read_link(&content, zPath);
108
+ }else{
109
+ blob_read_from_file(&content, zPath);
110
+ }
103111
db_bind_blob(&ins, ":content", &content);
104112
}else if( deleted ){
105113
blob_zero(&content);
106114
db_bind_null(&ins, ":content");
107115
}else{
108116
/* A modified file */
109117
Blob orig;
110118
Blob disk;
111
- blob_read_from_file(&disk, zPath);
119
+
120
+ if( isNewLink ){
121
+ blob_read_link(&disk, zPath);
122
+ }else{
123
+ blob_read_from_file(&disk, zPath);
124
+ }
112125
content_get(rid, &orig);
113126
blob_delta_create(&orig, &disk, &content);
114127
blob_reset(&orig);
115128
blob_reset(&disk);
116129
db_bind_blob(&ins, ":content", &content);
117130
}
131
+ db_bind_int(&ins, ":islink", isNewLink);
118132
db_step(&ins);
119133
db_reset(&ins);
120134
fossil_free(zPath);
121135
blob_reset(&content);
122136
}
@@ -167,54 +181,74 @@
167181
** Apply a stash to the current check-out.
168182
*/
169183
static void stash_apply(int stashid, int nConflict){
170184
Stmt q;
171185
db_prepare(&q,
172
- "SELECT rid, isRemoved, isExec, origname, newname, delta"
186
+ "SELECT rid, isRemoved, isExec, isLink, origname, newname, delta"
173187
" FROM stashfile WHERE stashid=%d",
174188
stashid
175189
);
176190
while( db_step(&q)==SQLITE_ROW ){
177191
int rid = db_column_int(&q, 0);
178192
int isRemoved = db_column_int(&q, 1);
179193
int isExec = db_column_int(&q, 2);
180
- const char *zOrig = db_column_text(&q, 3);
181
- const char *zNew = db_column_text(&q, 4);
194
+ int isLink = db_column_int(&q, 3);
195
+ const char *zOrig = db_column_text(&q, 4);
196
+ const char *zNew = db_column_text(&q, 5);
182197
char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
183198
char *zNPath = mprintf("%s%s", g.zLocalRoot, zNew);
184199
Blob delta;
185200
undo_save(zNew);
186201
blob_zero(&delta);
187202
if( rid==0 ){
188
- db_ephemeral_blob(&q, 5, &delta);
203
+ db_ephemeral_blob(&q, 6, &delta);
189204
blob_write_to_file(&delta, zNPath);
190205
file_setexe(zNPath, isExec);
191206
fossil_print("ADD %s\n", zNew);
192207
}else if( isRemoved ){
193208
fossil_print("DELETE %s\n", zOrig);
194209
file_delete(zOPath);
195210
}else{
196211
Blob a, b, out, disk;
197
- db_ephemeral_blob(&q, 5, &delta);
198
- blob_read_from_file(&disk, zOPath);
212
+ int isNewLink = file_islink(zOPath);
213
+ db_ephemeral_blob(&q, 6, &delta);
214
+ if( isNewLink ){
215
+ blob_read_link(&disk, zOPath);
216
+ }else{
217
+ blob_read_from_file(&disk, zOPath);
218
+ }
199219
content_get(rid, &a);
200220
blob_delta_apply(&a, &delta, &b);
201
- if( blob_compare(&disk, &a)==0 ){
202
- blob_write_to_file(&b, zNPath);
221
+ if( blob_compare(&disk, &a)==0 && isLink == isNewLink ){
222
+ if( isLink || isNewLink ){
223
+ file_delete(zNPath);
224
+ }
225
+ if( isLink ){
226
+ create_symlink(blob_str(&b), zNPath);
227
+ }else{
228
+ blob_write_to_file(&b, zNPath);
229
+ }
203230
file_setexe(zNPath, isExec);
204231
fossil_print("UPDATE %s\n", zNew);
205232
}else{
206
- int rc = merge_3way(&a, zOPath, &b, &out);
207
- blob_write_to_file(&out, zNPath);
208
- file_setexe(zNPath, isExec);
233
+ int rc;
234
+ if( isLink || isNewLink ){
235
+ rc = -1;
236
+ blob_zero(&b); /* because we reset it later */
237
+ fossil_print("***** Cannot merge symlink %s\n", zNew);
238
+ }else{
239
+ rc = merge_3way(&a, zOPath, &b, &out);
240
+ blob_write_to_file(&out, zNPath);
241
+ blob_reset(&out);
242
+ file_setexe(zNPath, isExec);
243
+ }
209244
if( rc ){
210245
fossil_print("CONFLICT %s\n", zNew);
211246
nConflict++;
212247
}else{
213248
fossil_print("MERGE %s\n", zNew);
214249
}
215
- blob_reset(&out);
216250
}
217251
blob_reset(&a);
218252
blob_reset(&b);
219253
blob_reset(&disk);
220254
}
@@ -224,11 +258,12 @@
224258
file_delete(zOPath);
225259
}
226260
}
227261
db_finalize(&q);
228262
if( nConflict ){
229
- fossil_print("WARNING: merge conflicts - see messages above for details.\n");
263
+ fossil_print("WARNING: %d merge conflicts - see messages above for details.\n",
264
+ nConflict);
230265
}
231266
}
232267
233268
/*
234269
** Show the diffs associate with a single stash.
@@ -236,50 +271,66 @@
236271
static void stash_diff(int stashid, const char *zDiffCmd){
237272
Stmt q;
238273
Blob empty;
239274
blob_zero(&empty);
240275
db_prepare(&q,
241
- "SELECT rid, isRemoved, isExec, origname, newname, delta"
276
+ "SELECT rid, isRemoved, isExec, isLink, origname, newname, delta"
242277
" FROM stashfile WHERE stashid=%d",
243278
stashid
244279
);
245280
while( db_step(&q)==SQLITE_ROW ){
246281
int rid = db_column_int(&q, 0);
247282
int isRemoved = db_column_int(&q, 1);
248
- const char *zOrig = db_column_text(&q, 3);
249
- const char *zNew = db_column_text(&q, 4);
283
+ int isLink = db_column_int(&q, 3);
284
+ const char *zOrig = db_column_text(&q, 4);
285
+ const char *zNew = db_column_text(&q, 5);
250286
char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
251287
Blob delta;
252288
if( rid==0 ){
253
- db_ephemeral_blob(&q, 5, &delta);
289
+ db_ephemeral_blob(&q, 6, &delta);
254290
fossil_print("ADDED %s\n", zNew);
255291
diff_print_index(zNew);
256292
diff_file_mem(&empty, &delta, zNew, zDiffCmd, 0);
257293
}else if( isRemoved ){
258294
fossil_print("DELETE %s\n", zOrig);
259
- blob_read_from_file(&delta, zOPath);
295
+ if( file_islink(zOPath) ){
296
+ blob_read_link(&delta, zOPath);
297
+ }else{
298
+ blob_read_from_file(&delta, zOPath);
299
+ }
260300
diff_print_index(zNew);
261301
diff_file_mem(&delta, &empty, zOrig, zDiffCmd, 0);
262302
}else{
263303
Blob a, b, disk;
264
- db_ephemeral_blob(&q, 5, &delta);
265
- blob_read_from_file(&disk, zOPath);
266
- content_get(rid, &a);
267
- blob_delta_apply(&a, &delta, &b);
304
+ int isOrigLink = file_islink(zOPath);
305
+ db_ephemeral_blob(&q, 6, &delta);
306
+ if( isOrigLink ){
307
+ blob_read_link(&disk, zOPath);
308
+ }else{
309
+ blob_read_from_file(&disk, zOPath);
310
+ }
268311
fossil_print("CHANGED %s\n", zNew);
269
- diff_file_mem(&disk, &b, zNew, zDiffCmd, 0);
270
- blob_reset(&a);
271
- blob_reset(&b);
312
+ if( !isOrigLink != !isLink ){
313
+ diff_print_index(zNew);
314
+ printf("--- %s\n+++ %s\n", zOrig, zNew);
315
+ printf("cannot compute difference between symlink and regular file\n");
316
+ }else{
317
+ content_get(rid, &a);
318
+ blob_delta_apply(&a, &delta, &b);
319
+ diff_file_mem(&disk, &b, zNew, zDiffCmd, 0);
320
+ blob_reset(&a);
321
+ blob_reset(&b);
322
+ }
272323
blob_reset(&disk);
273324
}
274325
blob_reset(&delta);
275326
}
276327
db_finalize(&q);
277328
}
278329
279330
/*
280
-** Drop the indicates stash
331
+** Drop the indicated stash
281332
*/
282333
static void stash_drop(int stashid){
283334
db_multi_exec(
284335
"DELETE FROM stash WHERE stashid=%d;"
285336
"DELETE FROM stashfile WHERE stashid=%d;",
@@ -345,17 +396,18 @@
345396
** Update to the baseline checkout for STASHID then apply the
346397
** changes of STASHID. Keep STASHID so that it can be reused
347398
** This command is undoable.
348399
**
349400
** fossil stash drop ?STASHID? ?--all?
401
+** fossil stash rm ?STASHID? ?--all?
350402
**
351403
** Forget everything about STASHID. Forget the whole stash if the
352404
** --all flag is used. Individual drops are undoable but --all is not.
353405
**
354406
** fossil stash snapshot ?-m COMMENT? ?FILES...?
355407
**
356
-** Save the current changes in the working tress as a new stash
408
+** Save the current changes in the working tree as a new stash
357409
** but, unlike "save", do not revert those changes.
358410
**
359411
** fossil stash diff ?STASHID?
360412
** fossil stash gdiff ?STASHID?
361413
**
@@ -428,11 +480,11 @@
428480
}
429481
}
430482
db_finalize(&q);
431483
if( n==0 ) fossil_print("empty stash\n");
432484
}else
433
- if( memcmp(zCmd, "drop", nCmd)==0 ){
485
+ if( memcmp(zCmd, "drop", nCmd)==0 || memcmp(zCmd, "rm", nCmd)==0 ){
434486
int allFlag = find_option("all", 0, 0)!=0;
435487
if( g.argc>4 ) usage("stash apply STASHID");
436488
if( allFlag ){
437489
db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
438490
}else{
439491
--- src/stash.c
+++ src/stash.c
@@ -35,10 +35,11 @@
35 @ stashid INTEGER REFERENCES stash, -- Stash that contains this file
36 @ rid INTEGER, -- Baseline content in BLOB table or 0.
37 @ isAdded BOOLEAN, -- True if this is an added file
38 @ isRemoved BOOLEAN, -- True if this file is deleted
39 @ isExec BOOLEAN, -- True if file is executable
 
40 @ origname TEXT, -- Original filename
41 @ newname TEXT, -- New name for file at next check-in
42 @ delta BLOB, -- Delta from baseline. Content if rid=0
43 @ PRIMARY KEY(origname, stashid)
44 @ );
@@ -61,11 +62,11 @@
61 zFile = mprintf("%/", zFName);
62 file_tree_name(zFile, &fname, 1);
63 zTreename = blob_str(&fname);
64 blob_zero(&sql);
65 blob_appendf(&sql,
66 "SELECT deleted, isexe, mrid, pathname, coalesce(origname,pathname)"
67 " FROM vfile"
68 " WHERE vid=%d AND (chnged OR deleted OR origname NOT NULL OR mrid==0)",
69 vid
70 );
71 if( fossil_strcmp(zTreename,".")!=0 ){
@@ -76,47 +77,60 @@
76 );
77 }
78 db_prepare(&q, blob_str(&sql));
79 blob_reset(&sql);
80 db_prepare(&ins,
81 "INSERT INTO stashfile(stashid, rid, isAdded, isRemoved, isExec,"
82 "origname, newname, delta)"
83 "VALUES(%d,:rid,:isadd,:isrm,:isexe,:orig,:new,:content)",
84 stashid
85 );
86 while( db_step(&q)==SQLITE_ROW ){
87 int deleted = db_column_int(&q, 0);
88 int rid = db_column_int(&q, 2);
89 const char *zName = db_column_text(&q, 3);
90 const char *zOrig = db_column_text(&q, 4);
91 char *zPath = mprintf("%s%s", g.zLocalRoot, zName);
92 Blob content;
 
93
94 db_bind_int(&ins, ":rid", rid);
95 db_bind_int(&ins, ":isadd", rid==0);
96 db_bind_int(&ins, ":isrm", deleted);
97 db_bind_int(&ins, ":isexe", db_column_int(&q, 1));
 
98 db_bind_text(&ins, ":orig", zOrig);
99 db_bind_text(&ins, ":new", zName);
 
100 if( rid==0 ){
101 /* A new file */
102 blob_read_from_file(&content, zPath);
 
 
 
 
103 db_bind_blob(&ins, ":content", &content);
104 }else if( deleted ){
105 blob_zero(&content);
106 db_bind_null(&ins, ":content");
107 }else{
108 /* A modified file */
109 Blob orig;
110 Blob disk;
111 blob_read_from_file(&disk, zPath);
 
 
 
 
 
112 content_get(rid, &orig);
113 blob_delta_create(&orig, &disk, &content);
114 blob_reset(&orig);
115 blob_reset(&disk);
116 db_bind_blob(&ins, ":content", &content);
117 }
 
118 db_step(&ins);
119 db_reset(&ins);
120 fossil_free(zPath);
121 blob_reset(&content);
122 }
@@ -167,54 +181,74 @@
167 ** Apply a stash to the current check-out.
168 */
169 static void stash_apply(int stashid, int nConflict){
170 Stmt q;
171 db_prepare(&q,
172 "SELECT rid, isRemoved, isExec, origname, newname, delta"
173 " FROM stashfile WHERE stashid=%d",
174 stashid
175 );
176 while( db_step(&q)==SQLITE_ROW ){
177 int rid = db_column_int(&q, 0);
178 int isRemoved = db_column_int(&q, 1);
179 int isExec = db_column_int(&q, 2);
180 const char *zOrig = db_column_text(&q, 3);
181 const char *zNew = db_column_text(&q, 4);
 
182 char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
183 char *zNPath = mprintf("%s%s", g.zLocalRoot, zNew);
184 Blob delta;
185 undo_save(zNew);
186 blob_zero(&delta);
187 if( rid==0 ){
188 db_ephemeral_blob(&q, 5, &delta);
189 blob_write_to_file(&delta, zNPath);
190 file_setexe(zNPath, isExec);
191 fossil_print("ADD %s\n", zNew);
192 }else if( isRemoved ){
193 fossil_print("DELETE %s\n", zOrig);
194 file_delete(zOPath);
195 }else{
196 Blob a, b, out, disk;
197 db_ephemeral_blob(&q, 5, &delta);
198 blob_read_from_file(&disk, zOPath);
 
 
 
 
 
199 content_get(rid, &a);
200 blob_delta_apply(&a, &delta, &b);
201 if( blob_compare(&disk, &a)==0 ){
202 blob_write_to_file(&b, zNPath);
 
 
 
 
 
 
 
203 file_setexe(zNPath, isExec);
204 fossil_print("UPDATE %s\n", zNew);
205 }else{
206 int rc = merge_3way(&a, zOPath, &b, &out);
207 blob_write_to_file(&out, zNPath);
208 file_setexe(zNPath, isExec);
 
 
 
 
 
 
 
 
209 if( rc ){
210 fossil_print("CONFLICT %s\n", zNew);
211 nConflict++;
212 }else{
213 fossil_print("MERGE %s\n", zNew);
214 }
215 blob_reset(&out);
216 }
217 blob_reset(&a);
218 blob_reset(&b);
219 blob_reset(&disk);
220 }
@@ -224,11 +258,12 @@
224 file_delete(zOPath);
225 }
226 }
227 db_finalize(&q);
228 if( nConflict ){
229 fossil_print("WARNING: merge conflicts - see messages above for details.\n");
 
230 }
231 }
232
233 /*
234 ** Show the diffs associate with a single stash.
@@ -236,50 +271,66 @@
236 static void stash_diff(int stashid, const char *zDiffCmd){
237 Stmt q;
238 Blob empty;
239 blob_zero(&empty);
240 db_prepare(&q,
241 "SELECT rid, isRemoved, isExec, origname, newname, delta"
242 " FROM stashfile WHERE stashid=%d",
243 stashid
244 );
245 while( db_step(&q)==SQLITE_ROW ){
246 int rid = db_column_int(&q, 0);
247 int isRemoved = db_column_int(&q, 1);
248 const char *zOrig = db_column_text(&q, 3);
249 const char *zNew = db_column_text(&q, 4);
 
250 char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
251 Blob delta;
252 if( rid==0 ){
253 db_ephemeral_blob(&q, 5, &delta);
254 fossil_print("ADDED %s\n", zNew);
255 diff_print_index(zNew);
256 diff_file_mem(&empty, &delta, zNew, zDiffCmd, 0);
257 }else if( isRemoved ){
258 fossil_print("DELETE %s\n", zOrig);
259 blob_read_from_file(&delta, zOPath);
 
 
 
 
260 diff_print_index(zNew);
261 diff_file_mem(&delta, &empty, zOrig, zDiffCmd, 0);
262 }else{
263 Blob a, b, disk;
264 db_ephemeral_blob(&q, 5, &delta);
265 blob_read_from_file(&disk, zOPath);
266 content_get(rid, &a);
267 blob_delta_apply(&a, &delta, &b);
 
 
 
268 fossil_print("CHANGED %s\n", zNew);
269 diff_file_mem(&disk, &b, zNew, zDiffCmd, 0);
270 blob_reset(&a);
271 blob_reset(&b);
 
 
 
 
 
 
 
 
272 blob_reset(&disk);
273 }
274 blob_reset(&delta);
275 }
276 db_finalize(&q);
277 }
278
279 /*
280 ** Drop the indicates stash
281 */
282 static void stash_drop(int stashid){
283 db_multi_exec(
284 "DELETE FROM stash WHERE stashid=%d;"
285 "DELETE FROM stashfile WHERE stashid=%d;",
@@ -345,17 +396,18 @@
345 ** Update to the baseline checkout for STASHID then apply the
346 ** changes of STASHID. Keep STASHID so that it can be reused
347 ** This command is undoable.
348 **
349 ** fossil stash drop ?STASHID? ?--all?
 
350 **
351 ** Forget everything about STASHID. Forget the whole stash if the
352 ** --all flag is used. Individual drops are undoable but --all is not.
353 **
354 ** fossil stash snapshot ?-m COMMENT? ?FILES...?
355 **
356 ** Save the current changes in the working tress as a new stash
357 ** but, unlike "save", do not revert those changes.
358 **
359 ** fossil stash diff ?STASHID?
360 ** fossil stash gdiff ?STASHID?
361 **
@@ -428,11 +480,11 @@
428 }
429 }
430 db_finalize(&q);
431 if( n==0 ) fossil_print("empty stash\n");
432 }else
433 if( memcmp(zCmd, "drop", nCmd)==0 ){
434 int allFlag = find_option("all", 0, 0)!=0;
435 if( g.argc>4 ) usage("stash apply STASHID");
436 if( allFlag ){
437 db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
438 }else{
439
--- src/stash.c
+++ src/stash.c
@@ -35,10 +35,11 @@
35 @ stashid INTEGER REFERENCES stash, -- Stash that contains this file
36 @ rid INTEGER, -- Baseline content in BLOB table or 0.
37 @ isAdded BOOLEAN, -- True if this is an added file
38 @ isRemoved BOOLEAN, -- True if this file is deleted
39 @ isExec BOOLEAN, -- True if file is executable
40 @ isLink BOOLEAN, -- True if file is a symlink
41 @ origname TEXT, -- Original filename
42 @ newname TEXT, -- New name for file at next check-in
43 @ delta BLOB, -- Delta from baseline. Content if rid=0
44 @ PRIMARY KEY(origname, stashid)
45 @ );
@@ -61,11 +62,11 @@
62 zFile = mprintf("%/", zFName);
63 file_tree_name(zFile, &fname, 1);
64 zTreename = blob_str(&fname);
65 blob_zero(&sql);
66 blob_appendf(&sql,
67 "SELECT deleted, isexe, islink, mrid, pathname, coalesce(origname,pathname)"
68 " FROM vfile"
69 " WHERE vid=%d AND (chnged OR deleted OR origname NOT NULL OR mrid==0)",
70 vid
71 );
72 if( fossil_strcmp(zTreename,".")!=0 ){
@@ -76,47 +77,60 @@
77 );
78 }
79 db_prepare(&q, blob_str(&sql));
80 blob_reset(&sql);
81 db_prepare(&ins,
82 "INSERT INTO stashfile(stashid, rid, isAdded, isRemoved, isExec, isLink,"
83 "origname, newname, delta)"
84 "VALUES(%d,:rid,:isadd,:isrm,:isexe,:islink,:orig,:new,:content)",
85 stashid
86 );
87 while( db_step(&q)==SQLITE_ROW ){
88 int deleted = db_column_int(&q, 0);
89 int rid = db_column_int(&q, 3);
90 const char *zName = db_column_text(&q, 4);
91 const char *zOrig = db_column_text(&q, 5);
92 char *zPath = mprintf("%s%s", g.zLocalRoot, zName);
93 Blob content;
94 int isNewLink = file_islink(zPath);
95
96 db_bind_int(&ins, ":rid", rid);
97 db_bind_int(&ins, ":isadd", rid==0);
98 db_bind_int(&ins, ":isrm", deleted);
99 db_bind_int(&ins, ":isexe", db_column_int(&q, 1));
100 db_bind_int(&ins, ":islink", db_column_int(&q, 2));
101 db_bind_text(&ins, ":orig", zOrig);
102 db_bind_text(&ins, ":new", zName);
103
104 if( rid==0 ){
105 /* A new file */
106 if( isNewLink ){
107 blob_read_link(&content, zPath);
108 }else{
109 blob_read_from_file(&content, zPath);
110 }
111 db_bind_blob(&ins, ":content", &content);
112 }else if( deleted ){
113 blob_zero(&content);
114 db_bind_null(&ins, ":content");
115 }else{
116 /* A modified file */
117 Blob orig;
118 Blob disk;
119
120 if( isNewLink ){
121 blob_read_link(&disk, zPath);
122 }else{
123 blob_read_from_file(&disk, zPath);
124 }
125 content_get(rid, &orig);
126 blob_delta_create(&orig, &disk, &content);
127 blob_reset(&orig);
128 blob_reset(&disk);
129 db_bind_blob(&ins, ":content", &content);
130 }
131 db_bind_int(&ins, ":islink", isNewLink);
132 db_step(&ins);
133 db_reset(&ins);
134 fossil_free(zPath);
135 blob_reset(&content);
136 }
@@ -167,54 +181,74 @@
181 ** Apply a stash to the current check-out.
182 */
183 static void stash_apply(int stashid, int nConflict){
184 Stmt q;
185 db_prepare(&q,
186 "SELECT rid, isRemoved, isExec, isLink, origname, newname, delta"
187 " FROM stashfile WHERE stashid=%d",
188 stashid
189 );
190 while( db_step(&q)==SQLITE_ROW ){
191 int rid = db_column_int(&q, 0);
192 int isRemoved = db_column_int(&q, 1);
193 int isExec = db_column_int(&q, 2);
194 int isLink = db_column_int(&q, 3);
195 const char *zOrig = db_column_text(&q, 4);
196 const char *zNew = db_column_text(&q, 5);
197 char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
198 char *zNPath = mprintf("%s%s", g.zLocalRoot, zNew);
199 Blob delta;
200 undo_save(zNew);
201 blob_zero(&delta);
202 if( rid==0 ){
203 db_ephemeral_blob(&q, 6, &delta);
204 blob_write_to_file(&delta, zNPath);
205 file_setexe(zNPath, isExec);
206 fossil_print("ADD %s\n", zNew);
207 }else if( isRemoved ){
208 fossil_print("DELETE %s\n", zOrig);
209 file_delete(zOPath);
210 }else{
211 Blob a, b, out, disk;
212 int isNewLink = file_islink(zOPath);
213 db_ephemeral_blob(&q, 6, &delta);
214 if( isNewLink ){
215 blob_read_link(&disk, zOPath);
216 }else{
217 blob_read_from_file(&disk, zOPath);
218 }
219 content_get(rid, &a);
220 blob_delta_apply(&a, &delta, &b);
221 if( blob_compare(&disk, &a)==0 && isLink == isNewLink ){
222 if( isLink || isNewLink ){
223 file_delete(zNPath);
224 }
225 if( isLink ){
226 create_symlink(blob_str(&b), zNPath);
227 }else{
228 blob_write_to_file(&b, zNPath);
229 }
230 file_setexe(zNPath, isExec);
231 fossil_print("UPDATE %s\n", zNew);
232 }else{
233 int rc;
234 if( isLink || isNewLink ){
235 rc = -1;
236 blob_zero(&b); /* because we reset it later */
237 fossil_print("***** Cannot merge symlink %s\n", zNew);
238 }else{
239 rc = merge_3way(&a, zOPath, &b, &out);
240 blob_write_to_file(&out, zNPath);
241 blob_reset(&out);
242 file_setexe(zNPath, isExec);
243 }
244 if( rc ){
245 fossil_print("CONFLICT %s\n", zNew);
246 nConflict++;
247 }else{
248 fossil_print("MERGE %s\n", zNew);
249 }
 
250 }
251 blob_reset(&a);
252 blob_reset(&b);
253 blob_reset(&disk);
254 }
@@ -224,11 +258,12 @@
258 file_delete(zOPath);
259 }
260 }
261 db_finalize(&q);
262 if( nConflict ){
263 fossil_print("WARNING: %d merge conflicts - see messages above for details.\n",
264 nConflict);
265 }
266 }
267
268 /*
269 ** Show the diffs associate with a single stash.
@@ -236,50 +271,66 @@
271 static void stash_diff(int stashid, const char *zDiffCmd){
272 Stmt q;
273 Blob empty;
274 blob_zero(&empty);
275 db_prepare(&q,
276 "SELECT rid, isRemoved, isExec, isLink, origname, newname, delta"
277 " FROM stashfile WHERE stashid=%d",
278 stashid
279 );
280 while( db_step(&q)==SQLITE_ROW ){
281 int rid = db_column_int(&q, 0);
282 int isRemoved = db_column_int(&q, 1);
283 int isLink = db_column_int(&q, 3);
284 const char *zOrig = db_column_text(&q, 4);
285 const char *zNew = db_column_text(&q, 5);
286 char *zOPath = mprintf("%s%s", g.zLocalRoot, zOrig);
287 Blob delta;
288 if( rid==0 ){
289 db_ephemeral_blob(&q, 6, &delta);
290 fossil_print("ADDED %s\n", zNew);
291 diff_print_index(zNew);
292 diff_file_mem(&empty, &delta, zNew, zDiffCmd, 0);
293 }else if( isRemoved ){
294 fossil_print("DELETE %s\n", zOrig);
295 if( file_islink(zOPath) ){
296 blob_read_link(&delta, zOPath);
297 }else{
298 blob_read_from_file(&delta, zOPath);
299 }
300 diff_print_index(zNew);
301 diff_file_mem(&delta, &empty, zOrig, zDiffCmd, 0);
302 }else{
303 Blob a, b, disk;
304 int isOrigLink = file_islink(zOPath);
305 db_ephemeral_blob(&q, 6, &delta);
306 if( isOrigLink ){
307 blob_read_link(&disk, zOPath);
308 }else{
309 blob_read_from_file(&disk, zOPath);
310 }
311 fossil_print("CHANGED %s\n", zNew);
312 if( !isOrigLink != !isLink ){
313 diff_print_index(zNew);
314 printf("--- %s\n+++ %s\n", zOrig, zNew);
315 printf("cannot compute difference between symlink and regular file\n");
316 }else{
317 content_get(rid, &a);
318 blob_delta_apply(&a, &delta, &b);
319 diff_file_mem(&disk, &b, zNew, zDiffCmd, 0);
320 blob_reset(&a);
321 blob_reset(&b);
322 }
323 blob_reset(&disk);
324 }
325 blob_reset(&delta);
326 }
327 db_finalize(&q);
328 }
329
330 /*
331 ** Drop the indicated stash
332 */
333 static void stash_drop(int stashid){
334 db_multi_exec(
335 "DELETE FROM stash WHERE stashid=%d;"
336 "DELETE FROM stashfile WHERE stashid=%d;",
@@ -345,17 +396,18 @@
396 ** Update to the baseline checkout for STASHID then apply the
397 ** changes of STASHID. Keep STASHID so that it can be reused
398 ** This command is undoable.
399 **
400 ** fossil stash drop ?STASHID? ?--all?
401 ** fossil stash rm ?STASHID? ?--all?
402 **
403 ** Forget everything about STASHID. Forget the whole stash if the
404 ** --all flag is used. Individual drops are undoable but --all is not.
405 **
406 ** fossil stash snapshot ?-m COMMENT? ?FILES...?
407 **
408 ** Save the current changes in the working tree as a new stash
409 ** but, unlike "save", do not revert those changes.
410 **
411 ** fossil stash diff ?STASHID?
412 ** fossil stash gdiff ?STASHID?
413 **
@@ -428,11 +480,11 @@
480 }
481 }
482 db_finalize(&q);
483 if( n==0 ) fossil_print("empty stash\n");
484 }else
485 if( memcmp(zCmd, "drop", nCmd)==0 || memcmp(zCmd, "rm", nCmd)==0 ){
486 int allFlag = find_option("all", 0, 0)!=0;
487 if( g.argc>4 ) usage("stash apply STASHID");
488 if( allFlag ){
489 db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
490 }else{
491
+21 -4
--- src/tar.c
+++ src/tar.c
@@ -280,11 +280,12 @@
280280
const char *zName, /* Name of the object */
281281
int nName, /* Number of characters in zName */
282282
int iMode, /* Mode. 0644 or 0755 */
283283
unsigned int mTime, /* File modification time */
284284
int iSize, /* Size of the object in bytes */
285
- char cType /* Type of object. '0'==file. '5'==directory */
285
+ char cType /* Type of object:
286
+ '0'==file. '2'==symlink. '5'==directory */
286287
){
287288
/* set mode and modification time */
288289
sqlite3_snprintf(8, (char*)&tball.aHdr[100], "%07o", iMode);
289290
sqlite3_snprintf(12, (char*)&tball.aHdr[136], "%011o", mTime);
290291
@@ -359,20 +360,36 @@
359360
** Add a single file to the growing tarball.
360361
*/
361362
static void tar_add_file(
362363
const char *zName, /* Name of the file. nul-terminated */
363364
Blob *pContent, /* Content of the file */
364
- int isExe, /* True for executable files */
365
+ int mPerm, /* 1: executable file, 2: symlink */
365366
unsigned int mTime /* Last modification time of the file */
366367
){
367368
int nName = strlen(zName);
368369
int n = blob_size(pContent);
369370
int lastPage;
371
+ char cType = '0';
370372
371373
/* length check moved to tar_split_path */
372374
tar_add_directory_of(zName, nName, mTime);
373
- tar_add_header(zName, nName, isExe ? 0755 : 0644, mTime, n, '0');
375
+
376
+ /*
377
+ * If we have a symlink, write its destination path (which is stored in
378
+ * pContent) into header, and set content length to 0 to avoid storing path
379
+ * as file content in the next step. Since 'linkname' header is limited to
380
+ * 100 bytes (-1 byte for terminating zero), if path is greater than that,
381
+ * store symlink as a plain-text file. (Not sure how TAR handles long links.)
382
+ */
383
+ if( mPerm == PERM_LNK && n <= 100 ){
384
+ sqlite3_snprintf(100, (char*)&tball.aHdr[157], "%s", blob_str(pContent));
385
+ cType = '2';
386
+ n = 0;
387
+ }
388
+
389
+ tar_add_header(zName, nName, ( mPerm==PERM_EXE ) ? 0755 : 0644,
390
+ mTime, n, cType);
374391
if( n ){
375392
gzip_step(blob_buffer(pContent), n);
376393
lastPage = n % 512;
377394
if( lastPage!=0 ){
378395
gzip_step(tball.zSpaces, 512 - lastPage);
@@ -415,11 +432,11 @@
415432
tar_begin();
416433
for(i=3; i<g.argc; i++){
417434
blob_zero(&file);
418435
blob_read_from_file(&file, g.argv[i]);
419436
tar_add_file(g.argv[i], &file,
420
- file_isexe(g.argv[i]), file_mtime(g.argv[i]));
437
+ file_perm(g.argv[i]), file_mtime(g.argv[i]));
421438
blob_reset(&file);
422439
}
423440
tar_finish(&zip);
424441
blob_write_to_file(&zip, g.argv[2]);
425442
}
426443
--- src/tar.c
+++ src/tar.c
@@ -280,11 +280,12 @@
280 const char *zName, /* Name of the object */
281 int nName, /* Number of characters in zName */
282 int iMode, /* Mode. 0644 or 0755 */
283 unsigned int mTime, /* File modification time */
284 int iSize, /* Size of the object in bytes */
285 char cType /* Type of object. '0'==file. '5'==directory */
 
286 ){
287 /* set mode and modification time */
288 sqlite3_snprintf(8, (char*)&tball.aHdr[100], "%07o", iMode);
289 sqlite3_snprintf(12, (char*)&tball.aHdr[136], "%011o", mTime);
290
@@ -359,20 +360,36 @@
359 ** Add a single file to the growing tarball.
360 */
361 static void tar_add_file(
362 const char *zName, /* Name of the file. nul-terminated */
363 Blob *pContent, /* Content of the file */
364 int isExe, /* True for executable files */
365 unsigned int mTime /* Last modification time of the file */
366 ){
367 int nName = strlen(zName);
368 int n = blob_size(pContent);
369 int lastPage;
 
370
371 /* length check moved to tar_split_path */
372 tar_add_directory_of(zName, nName, mTime);
373 tar_add_header(zName, nName, isExe ? 0755 : 0644, mTime, n, '0');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
374 if( n ){
375 gzip_step(blob_buffer(pContent), n);
376 lastPage = n % 512;
377 if( lastPage!=0 ){
378 gzip_step(tball.zSpaces, 512 - lastPage);
@@ -415,11 +432,11 @@
415 tar_begin();
416 for(i=3; i<g.argc; i++){
417 blob_zero(&file);
418 blob_read_from_file(&file, g.argv[i]);
419 tar_add_file(g.argv[i], &file,
420 file_isexe(g.argv[i]), file_mtime(g.argv[i]));
421 blob_reset(&file);
422 }
423 tar_finish(&zip);
424 blob_write_to_file(&zip, g.argv[2]);
425 }
426
--- src/tar.c
+++ src/tar.c
@@ -280,11 +280,12 @@
280 const char *zName, /* Name of the object */
281 int nName, /* Number of characters in zName */
282 int iMode, /* Mode. 0644 or 0755 */
283 unsigned int mTime, /* File modification time */
284 int iSize, /* Size of the object in bytes */
285 char cType /* Type of object:
286 '0'==file. '2'==symlink. '5'==directory */
287 ){
288 /* set mode and modification time */
289 sqlite3_snprintf(8, (char*)&tball.aHdr[100], "%07o", iMode);
290 sqlite3_snprintf(12, (char*)&tball.aHdr[136], "%011o", mTime);
291
@@ -359,20 +360,36 @@
360 ** Add a single file to the growing tarball.
361 */
362 static void tar_add_file(
363 const char *zName, /* Name of the file. nul-terminated */
364 Blob *pContent, /* Content of the file */
365 int mPerm, /* 1: executable file, 2: symlink */
366 unsigned int mTime /* Last modification time of the file */
367 ){
368 int nName = strlen(zName);
369 int n = blob_size(pContent);
370 int lastPage;
371 char cType = '0';
372
373 /* length check moved to tar_split_path */
374 tar_add_directory_of(zName, nName, mTime);
375
376 /*
377 * If we have a symlink, write its destination path (which is stored in
378 * pContent) into header, and set content length to 0 to avoid storing path
379 * as file content in the next step. Since 'linkname' header is limited to
380 * 100 bytes (-1 byte for terminating zero), if path is greater than that,
381 * store symlink as a plain-text file. (Not sure how TAR handles long links.)
382 */
383 if( mPerm == PERM_LNK && n <= 100 ){
384 sqlite3_snprintf(100, (char*)&tball.aHdr[157], "%s", blob_str(pContent));
385 cType = '2';
386 n = 0;
387 }
388
389 tar_add_header(zName, nName, ( mPerm==PERM_EXE ) ? 0755 : 0644,
390 mTime, n, cType);
391 if( n ){
392 gzip_step(blob_buffer(pContent), n);
393 lastPage = n % 512;
394 if( lastPage!=0 ){
395 gzip_step(tball.zSpaces, 512 - lastPage);
@@ -415,11 +432,11 @@
432 tar_begin();
433 for(i=3; i<g.argc; i++){
434 blob_zero(&file);
435 blob_read_from_file(&file, g.argv[i]);
436 tar_add_file(g.argv[i], &file,
437 file_perm(g.argv[i]), file_mtime(g.argv[i]));
438 blob_reset(&file);
439 }
440 tar_finish(&zip);
441 blob_write_to_file(&zip, g.argv[2]);
442 }
443
+36 -10
--- src/timeline.c
+++ src/timeline.c
@@ -152,10 +152,25 @@
152152
default: r = mx; g = mn, b = h2; break;
153153
}
154154
sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
155155
return zColor;
156156
}
157
+
158
+/*
159
+** COMMAND: test-hash-color
160
+**
161
+** Usage: %fossil test-hash-color TAG ...
162
+**
163
+** Print out the color names associated with each tag. Used for
164
+** testing the hash_color() function.
165
+*/
166
+void test_hash_color(void){
167
+ int i;
168
+ for(i=2; i<g.argc; i++){
169
+ fossil_print("%20s: %s\n", g.argv[i], hash_color(g.argv[i]));
170
+ }
171
+}
157172
158173
/*
159174
** Output a timeline in the web format given a query. The query
160175
** should return these columns:
161176
**
@@ -387,36 +402,46 @@
387402
db_prepare(&fchngQuery,
388403
"SELECT (pid==0) AS isnew,"
389404
" (fid==0) AS isdel,"
390405
" (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
391406
" (SELECT uuid FROM blob WHERE rid=fid),"
392
- " (SELECT uuid FROM blob WHERE rid=pid)"
407
+ " (SELECT uuid FROM blob WHERE rid=pid),"
408
+ " (SELECT name FROM filename WHERE fnid=mlink.pfnid) AS oldnm"
393409
" FROM mlink"
394
- " WHERE mid=:mid AND pid!=fid"
410
+ " WHERE mid=:mid AND (pid!=fid OR pfnid>0)"
395411
" ORDER BY 3 /*sort*/"
396412
);
397413
fchngQueryInit = 1;
398414
}
399415
db_bind_int(&fchngQuery, ":mid", rid);
400416
while( db_step(&fchngQuery)==SQLITE_ROW ){
401417
const char *zFilename = db_column_text(&fchngQuery, 2);
402418
int isNew = db_column_int(&fchngQuery, 0);
403419
int isDel = db_column_int(&fchngQuery, 1);
420
+ const char *zOldName = db_column_text(&fchngQuery, 5);
404421
const char *zOld = db_column_text(&fchngQuery, 4);
405422
const char *zNew = db_column_text(&fchngQuery, 3);
406423
if( !inUl ){
407424
@ <ul class="filelist">
408425
inUl = 1;
409426
}
410427
if( isNew ){
411428
@ <li> %h(zFilename) (new file) &nbsp;
412
- @ <a href="%s(g.zTop)/artifact/%S(zNew)" target="diffwindow">[view]
413
- @ </a></li>
429
+ @ <a href="%s(g.zTop)/artifact/%S(zNew)"
430
+ @ target="diffwindow">[view]</a></li>
414431
}else if( isDel ){
415432
@ <li> %h(zFilename) (deleted)</li>
433
+ }else if( fossil_strcmp(zOld,zNew)==0 && zOldName!=0 ){
434
+ @ <li> %h(zOldName) &rarr; %h(zFilename)
435
+ @ <a href="%s(g.zTop)/artifact/%S(zNew)"
436
+ @ target="diffwindow">[view]</a></li>
416437
}else{
417
- @ <li> %h(zFilename) &nbsp;
438
+ if( zOldName!=0 ){
439
+ @ <li> %h(zOldName) &rarr; %h(zFilename)
440
+ }else{
441
+ @ <li> %h(zFilename) &nbsp;
442
+ }
418443
@ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&v2=%S(zNew)"
419444
@ target="diffwindow">[diff]</a></li>
420445
}
421446
}
422447
db_reset(&fchngQuery);
@@ -536,11 +561,13 @@
536561
cgi_printf("]}%s", pRow->pNext ? ",\n" : "];\n");
537562
}
538563
cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
539564
graph_free(pGraph);
540565
@ var canvasDiv = document.getElementById("canvas");
566
+#if 0
541567
@ var realCanvas = null;
568
+#endif
542569
@ function drawBox(color,x0,y0,x1,y1){
543570
@ var n = document.createElement("div");
544571
@ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
545572
@ if( y0>y1 ){ var t=y0; y0=y1; y1=t; }
546573
@ var w = x1-x0+1;
@@ -665,10 +692,11 @@
665692
@ for(var i in rowinfo){
666693
@ rowinfo[i].y = absoluteY("m"+rowinfo[i].id) + 10 - canvasY;
667694
@ rowinfo[i].x = left + rowinfo[i].r*20;
668695
@ }
669696
@ var btm = absoluteY("grbtm") + 10 - canvasY;
697
+#if 0
670698
@ if( btm<32768 ){
671699
@ canvasDiv.innerHTML = '<canvas id="timeline-canvas" '+
672700
@ 'style="position:absolute;left:'+(left-5)+'px;"' +
673701
@ ' width="'+width+'" height="'+btm+'"><'+'/canvas>';
674702
@ realCanvas = document.getElementById('timeline-canvas');
@@ -685,10 +713,11 @@
685713
@ if(isNaN(x0) || isNaN(y0) || isNaN(x1) || isNaN(y1)) return;
686714
@ context.fillStyle = color;
687715
@ context.fillRect(x0-left+5,y0,x1-x0+1,y1-y0+1);
688716
@ };
689717
@ }
718
+#endif
690719
@ for(var i in rowinfo){
691720
@ drawNode(rowinfo[i], left, btm);
692721
@ }
693722
@ }
694723
@ var lastId = "m"+rowinfo[rowinfo.length-1].id;
@@ -909,11 +938,11 @@
909938
PathNode *p = 0;
910939
const char *zFrom = 0;
911940
const char *zTo = 0;
912941
913942
if( from_rid && to_rid ){
914
- p = path_shortest(from_rid, to_rid, noMerge);
943
+ p = path_shortest(from_rid, to_rid, noMerge, 0);
915944
zFrom = P("from");
916945
zTo = P("to");
917946
}else{
918947
if( path_common_ancestor(me_rid, you_rid) ){
919948
p = path_first();
@@ -1244,13 +1273,13 @@
12441273
*/
12451274
void print_timeline(Stmt *q, int mxLine, int showfiles){
12461275
int nLine = 0;
12471276
char zPrevDate[20];
12481277
const char *zCurrentUuid=0;
1249
- zPrevDate[0] = 0;
12501278
int fchngQueryInit = 0; /* True if fchngQuery is initialized */
12511279
Stmt fchngQuery; /* Query for file changes on check-ins */
1280
+ zPrevDate[0] = 0;
12521281
12531282
if( g.localOpen ){
12541283
int rid = db_lget_int("checkout", 0);
12551284
zCurrentUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
12561285
}
@@ -1297,11 +1326,10 @@
12971326
zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
12981327
nLine += comment_print(zFree, 9, 79);
12991328
sqlite3_free(zFree);
13001329
13011330
if(showfiles){
1302
- int inUl = 0;
13031331
if( !fchngQueryInit ){
13041332
db_prepare(&fchngQuery,
13051333
"SELECT (pid==0) AS isnew,"
13061334
" (fid==0) AS isdel,"
13071335
" (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1316,12 +1344,10 @@
13161344
db_bind_int(&fchngQuery, ":mid", rid);
13171345
while( db_step(&fchngQuery)==SQLITE_ROW ){
13181346
const char *zFilename = db_column_text(&fchngQuery, 2);
13191347
int isNew = db_column_int(&fchngQuery, 0);
13201348
int isDel = db_column_int(&fchngQuery, 1);
1321
- const char *zOld = db_column_text(&fchngQuery, 4);
1322
- const char *zNew = db_column_text(&fchngQuery, 3);
13231349
if( isNew ){
13241350
fossil_print(" ADDED %s\n",zFilename);
13251351
}else if( isDel ){
13261352
fossil_print(" DELETED %s\n",zFilename);
13271353
}else{
13281354
--- src/timeline.c
+++ src/timeline.c
@@ -152,10 +152,25 @@
152 default: r = mx; g = mn, b = h2; break;
153 }
154 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
155 return zColor;
156 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
158 /*
159 ** Output a timeline in the web format given a query. The query
160 ** should return these columns:
161 **
@@ -387,36 +402,46 @@
387 db_prepare(&fchngQuery,
388 "SELECT (pid==0) AS isnew,"
389 " (fid==0) AS isdel,"
390 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
391 " (SELECT uuid FROM blob WHERE rid=fid),"
392 " (SELECT uuid FROM blob WHERE rid=pid)"
 
393 " FROM mlink"
394 " WHERE mid=:mid AND pid!=fid"
395 " ORDER BY 3 /*sort*/"
396 );
397 fchngQueryInit = 1;
398 }
399 db_bind_int(&fchngQuery, ":mid", rid);
400 while( db_step(&fchngQuery)==SQLITE_ROW ){
401 const char *zFilename = db_column_text(&fchngQuery, 2);
402 int isNew = db_column_int(&fchngQuery, 0);
403 int isDel = db_column_int(&fchngQuery, 1);
 
404 const char *zOld = db_column_text(&fchngQuery, 4);
405 const char *zNew = db_column_text(&fchngQuery, 3);
406 if( !inUl ){
407 @ <ul class="filelist">
408 inUl = 1;
409 }
410 if( isNew ){
411 @ <li> %h(zFilename) (new file) &nbsp;
412 @ <a href="%s(g.zTop)/artifact/%S(zNew)" target="diffwindow">[view]
413 @ </a></li>
414 }else if( isDel ){
415 @ <li> %h(zFilename) (deleted)</li>
 
 
 
 
416 }else{
417 @ <li> %h(zFilename) &nbsp;
 
 
 
 
418 @ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&v2=%S(zNew)"
419 @ target="diffwindow">[diff]</a></li>
420 }
421 }
422 db_reset(&fchngQuery);
@@ -536,11 +561,13 @@
536 cgi_printf("]}%s", pRow->pNext ? ",\n" : "];\n");
537 }
538 cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
539 graph_free(pGraph);
540 @ var canvasDiv = document.getElementById("canvas");
 
541 @ var realCanvas = null;
 
542 @ function drawBox(color,x0,y0,x1,y1){
543 @ var n = document.createElement("div");
544 @ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
545 @ if( y0>y1 ){ var t=y0; y0=y1; y1=t; }
546 @ var w = x1-x0+1;
@@ -665,10 +692,11 @@
665 @ for(var i in rowinfo){
666 @ rowinfo[i].y = absoluteY("m"+rowinfo[i].id) + 10 - canvasY;
667 @ rowinfo[i].x = left + rowinfo[i].r*20;
668 @ }
669 @ var btm = absoluteY("grbtm") + 10 - canvasY;
 
670 @ if( btm<32768 ){
671 @ canvasDiv.innerHTML = '<canvas id="timeline-canvas" '+
672 @ 'style="position:absolute;left:'+(left-5)+'px;"' +
673 @ ' width="'+width+'" height="'+btm+'"><'+'/canvas>';
674 @ realCanvas = document.getElementById('timeline-canvas');
@@ -685,10 +713,11 @@
685 @ if(isNaN(x0) || isNaN(y0) || isNaN(x1) || isNaN(y1)) return;
686 @ context.fillStyle = color;
687 @ context.fillRect(x0-left+5,y0,x1-x0+1,y1-y0+1);
688 @ };
689 @ }
 
690 @ for(var i in rowinfo){
691 @ drawNode(rowinfo[i], left, btm);
692 @ }
693 @ }
694 @ var lastId = "m"+rowinfo[rowinfo.length-1].id;
@@ -909,11 +938,11 @@
909 PathNode *p = 0;
910 const char *zFrom = 0;
911 const char *zTo = 0;
912
913 if( from_rid && to_rid ){
914 p = path_shortest(from_rid, to_rid, noMerge);
915 zFrom = P("from");
916 zTo = P("to");
917 }else{
918 if( path_common_ancestor(me_rid, you_rid) ){
919 p = path_first();
@@ -1244,13 +1273,13 @@
1244 */
1245 void print_timeline(Stmt *q, int mxLine, int showfiles){
1246 int nLine = 0;
1247 char zPrevDate[20];
1248 const char *zCurrentUuid=0;
1249 zPrevDate[0] = 0;
1250 int fchngQueryInit = 0; /* True if fchngQuery is initialized */
1251 Stmt fchngQuery; /* Query for file changes on check-ins */
 
1252
1253 if( g.localOpen ){
1254 int rid = db_lget_int("checkout", 0);
1255 zCurrentUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
1256 }
@@ -1297,11 +1326,10 @@
1297 zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
1298 nLine += comment_print(zFree, 9, 79);
1299 sqlite3_free(zFree);
1300
1301 if(showfiles){
1302 int inUl = 0;
1303 if( !fchngQueryInit ){
1304 db_prepare(&fchngQuery,
1305 "SELECT (pid==0) AS isnew,"
1306 " (fid==0) AS isdel,"
1307 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1316,12 +1344,10 @@
1316 db_bind_int(&fchngQuery, ":mid", rid);
1317 while( db_step(&fchngQuery)==SQLITE_ROW ){
1318 const char *zFilename = db_column_text(&fchngQuery, 2);
1319 int isNew = db_column_int(&fchngQuery, 0);
1320 int isDel = db_column_int(&fchngQuery, 1);
1321 const char *zOld = db_column_text(&fchngQuery, 4);
1322 const char *zNew = db_column_text(&fchngQuery, 3);
1323 if( isNew ){
1324 fossil_print(" ADDED %s\n",zFilename);
1325 }else if( isDel ){
1326 fossil_print(" DELETED %s\n",zFilename);
1327 }else{
1328
--- src/timeline.c
+++ src/timeline.c
@@ -152,10 +152,25 @@
152 default: r = mx; g = mn, b = h2; break;
153 }
154 sqlite3_snprintf(8, zColor, "#%02x%02x%02x", r,g,b);
155 return zColor;
156 }
157
158 /*
159 ** COMMAND: test-hash-color
160 **
161 ** Usage: %fossil test-hash-color TAG ...
162 **
163 ** Print out the color names associated with each tag. Used for
164 ** testing the hash_color() function.
165 */
166 void test_hash_color(void){
167 int i;
168 for(i=2; i<g.argc; i++){
169 fossil_print("%20s: %s\n", g.argv[i], hash_color(g.argv[i]));
170 }
171 }
172
173 /*
174 ** Output a timeline in the web format given a query. The query
175 ** should return these columns:
176 **
@@ -387,36 +402,46 @@
402 db_prepare(&fchngQuery,
403 "SELECT (pid==0) AS isnew,"
404 " (fid==0) AS isdel,"
405 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
406 " (SELECT uuid FROM blob WHERE rid=fid),"
407 " (SELECT uuid FROM blob WHERE rid=pid),"
408 " (SELECT name FROM filename WHERE fnid=mlink.pfnid) AS oldnm"
409 " FROM mlink"
410 " WHERE mid=:mid AND (pid!=fid OR pfnid>0)"
411 " ORDER BY 3 /*sort*/"
412 );
413 fchngQueryInit = 1;
414 }
415 db_bind_int(&fchngQuery, ":mid", rid);
416 while( db_step(&fchngQuery)==SQLITE_ROW ){
417 const char *zFilename = db_column_text(&fchngQuery, 2);
418 int isNew = db_column_int(&fchngQuery, 0);
419 int isDel = db_column_int(&fchngQuery, 1);
420 const char *zOldName = db_column_text(&fchngQuery, 5);
421 const char *zOld = db_column_text(&fchngQuery, 4);
422 const char *zNew = db_column_text(&fchngQuery, 3);
423 if( !inUl ){
424 @ <ul class="filelist">
425 inUl = 1;
426 }
427 if( isNew ){
428 @ <li> %h(zFilename) (new file) &nbsp;
429 @ <a href="%s(g.zTop)/artifact/%S(zNew)"
430 @ target="diffwindow">[view]</a></li>
431 }else if( isDel ){
432 @ <li> %h(zFilename) (deleted)</li>
433 }else if( fossil_strcmp(zOld,zNew)==0 && zOldName!=0 ){
434 @ <li> %h(zOldName) &rarr; %h(zFilename)
435 @ <a href="%s(g.zTop)/artifact/%S(zNew)"
436 @ target="diffwindow">[view]</a></li>
437 }else{
438 if( zOldName!=0 ){
439 @ <li> %h(zOldName) &rarr; %h(zFilename)
440 }else{
441 @ <li> %h(zFilename) &nbsp;
442 }
443 @ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&v2=%S(zNew)"
444 @ target="diffwindow">[diff]</a></li>
445 }
446 }
447 db_reset(&fchngQuery);
@@ -536,11 +561,13 @@
561 cgi_printf("]}%s", pRow->pNext ? ",\n" : "];\n");
562 }
563 cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
564 graph_free(pGraph);
565 @ var canvasDiv = document.getElementById("canvas");
566 #if 0
567 @ var realCanvas = null;
568 #endif
569 @ function drawBox(color,x0,y0,x1,y1){
570 @ var n = document.createElement("div");
571 @ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
572 @ if( y0>y1 ){ var t=y0; y0=y1; y1=t; }
573 @ var w = x1-x0+1;
@@ -665,10 +692,11 @@
692 @ for(var i in rowinfo){
693 @ rowinfo[i].y = absoluteY("m"+rowinfo[i].id) + 10 - canvasY;
694 @ rowinfo[i].x = left + rowinfo[i].r*20;
695 @ }
696 @ var btm = absoluteY("grbtm") + 10 - canvasY;
697 #if 0
698 @ if( btm<32768 ){
699 @ canvasDiv.innerHTML = '<canvas id="timeline-canvas" '+
700 @ 'style="position:absolute;left:'+(left-5)+'px;"' +
701 @ ' width="'+width+'" height="'+btm+'"><'+'/canvas>';
702 @ realCanvas = document.getElementById('timeline-canvas');
@@ -685,10 +713,11 @@
713 @ if(isNaN(x0) || isNaN(y0) || isNaN(x1) || isNaN(y1)) return;
714 @ context.fillStyle = color;
715 @ context.fillRect(x0-left+5,y0,x1-x0+1,y1-y0+1);
716 @ };
717 @ }
718 #endif
719 @ for(var i in rowinfo){
720 @ drawNode(rowinfo[i], left, btm);
721 @ }
722 @ }
723 @ var lastId = "m"+rowinfo[rowinfo.length-1].id;
@@ -909,11 +938,11 @@
938 PathNode *p = 0;
939 const char *zFrom = 0;
940 const char *zTo = 0;
941
942 if( from_rid && to_rid ){
943 p = path_shortest(from_rid, to_rid, noMerge, 0);
944 zFrom = P("from");
945 zTo = P("to");
946 }else{
947 if( path_common_ancestor(me_rid, you_rid) ){
948 p = path_first();
@@ -1244,13 +1273,13 @@
1273 */
1274 void print_timeline(Stmt *q, int mxLine, int showfiles){
1275 int nLine = 0;
1276 char zPrevDate[20];
1277 const char *zCurrentUuid=0;
 
1278 int fchngQueryInit = 0; /* True if fchngQuery is initialized */
1279 Stmt fchngQuery; /* Query for file changes on check-ins */
1280 zPrevDate[0] = 0;
1281
1282 if( g.localOpen ){
1283 int rid = db_lget_int("checkout", 0);
1284 zCurrentUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
1285 }
@@ -1297,11 +1326,10 @@
1326 zFree = sqlite3_mprintf("[%.10s] %s%s", zUuid, zPrefix, zCom);
1327 nLine += comment_print(zFree, 9, 79);
1328 sqlite3_free(zFree);
1329
1330 if(showfiles){
 
1331 if( !fchngQueryInit ){
1332 db_prepare(&fchngQuery,
1333 "SELECT (pid==0) AS isnew,"
1334 " (fid==0) AS isdel,"
1335 " (SELECT name FROM filename WHERE fnid=mlink.fnid) AS name,"
@@ -1316,12 +1344,10 @@
1344 db_bind_int(&fchngQuery, ":mid", rid);
1345 while( db_step(&fchngQuery)==SQLITE_ROW ){
1346 const char *zFilename = db_column_text(&fchngQuery, 2);
1347 int isNew = db_column_int(&fchngQuery, 0);
1348 int isDel = db_column_int(&fchngQuery, 1);
 
 
1349 if( isNew ){
1350 fossil_print(" ADDED %s\n",zFilename);
1351 }else if( isDel ){
1352 fossil_print(" DELETED %s\n",zFilename);
1353 }else{
1354
+94 -8
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101101
const char *zName;
102102
Stmt q;
103103
int i, n, size, j;
104104
105105
zName = PD("name","-none-");
106
- db_prepare(&q, "SELECT datetime(tkt_mtime) AS tkt_datetime, *"
106
+ db_prepare(&q, "SELECT datetime(tkt_mtime,'localtime') AS tkt_datetime, *"
107107
" FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108108
if( db_step(&q)==SQLITE_ROW ){
109109
n = db_column_count(&q);
110110
for(i=0; i<n; i++){
111111
const char *zVal = db_column_text(&q, i);
@@ -899,10 +899,14 @@
899899
**
900900
** %fossil ticket add FIELD VALUE ?FIELD VALUE .. ? ?-q|--quote?
901901
**
902902
** like set, but create a new ticket with the given values.
903903
**
904
+** %fossil ticket history TICKETUUID
905
+**
906
+** Show the complete change history for the ticket
907
+**
904908
** The values in set|add are not validated against the definitions
905909
** given in "Ticket Common Script".
906910
*/
907911
void ticket_cmd(void){
908912
int n;
@@ -916,16 +920,16 @@
916920
if( !db_exists("SELECT 1 FROM user WHERE login=%Q", g.zLogin) ){
917921
fossil_fatal("no such user: %s", g.zLogin);
918922
}
919923
920924
if( g.argc<3 ){
921
- usage("add|fieldlist|set|show");
925
+ usage("add|fieldlist|set|show|history");
922926
}else{
923927
n = strlen(g.argv[2]);
924928
if( n==1 && g.argv[2][0]=='s' ){
925929
/* set/show cannot be distinguished, so show the usage */
926
- usage("add|fieldlist|set|show");
930
+ usage("add|fieldlist|set|show|history");
927931
}else if( strncmp(g.argv[2],"list",n)==0 ){
928932
if( g.argc==3 ){
929933
usage("list fields|reports");
930934
}else{
931935
n = strlen(g.argv[3]);
@@ -970,19 +974,24 @@
970974
rptshow( zRep, zSep, zFilterUuid, tktEncoding );
971975
972976
}
973977
}else{
974978
/* add a new ticket or update an existing ticket */
975
- enum { set,add,err } eCmd = err;
979
+ enum { set,add,history,err } eCmd = err;
976980
int i = 0;
977981
int rid;
978982
const char *zTktUuid = 0;
979983
Blob tktchng, cksum;
980984
981985
/* get command type (set/add) and get uuid, if needed for set */
982
- if( strncmp(g.argv[2],"set",n)==0 || strncmp(g.argv[2],"change",n)==0 ){
983
- eCmd = set;
986
+ if( strncmp(g.argv[2],"set",n)==0 || strncmp(g.argv[2],"change",n)==0 ||
987
+ strncmp(g.argv[2],"history",n)==0 ){
988
+ if( strncmp(g.argv[2],"history",n)==0 ){
989
+ eCmd = history;
990
+ }else{
991
+ eCmd = set;
992
+ }
984993
if( g.argc==3 ){
985994
usage("set TICKETUUID");
986995
}
987996
zTktUuid = db_text(0,
988997
"SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%s*'", g.argv[3]
@@ -996,13 +1005,90 @@
9961005
i = 3;
9971006
zTktUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
9981007
}
9991008
/* none of set/add, so show the usage! */
10001009
if( eCmd==err ){
1001
- usage("add|fieldlist|set|show");
1010
+ usage("add|fieldlist|set|show|history");
10021011
}
1003
-
1012
+
1013
+ /* we just handle history separately here, does not get out */
1014
+ if( eCmd==history ){
1015
+ Stmt q;
1016
+ char *zTitle;
1017
+ int tagid;
1018
+
1019
+ if ( i != g.argc ){
1020
+ fossil_fatal("no other parameters expected to %s!",g.argv[2]);
1021
+ }
1022
+ tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",zTktUuid);
1023
+ if( tagid==0 ){
1024
+ fossil_fatal("no such ticket %h", zTktUuid);
1025
+ }
1026
+ db_prepare(&q,
1027
+ "SELECT datetime(mtime,'localtime'), objid, uuid, NULL, NULL, NULL"
1028
+ " FROM event, blob"
1029
+ " WHERE objid IN (SELECT rid FROM tagxref WHERE tagid=%d)"
1030
+ " AND blob.rid=event.objid"
1031
+ " UNION "
1032
+ "SELECT datetime(mtime,'localtime'), attachid, uuid, src, filename, user"
1033
+ " FROM attachment, blob"
1034
+ " WHERE target=(SELECT substr(tagname,5) FROM tag WHERE tagid=%d)"
1035
+ " AND blob.rid=attachid"
1036
+ " ORDER BY 1 DESC",
1037
+ tagid, tagid
1038
+ );
1039
+ while( db_step(&q)==SQLITE_ROW ){
1040
+ Manifest *pTicket;
1041
+ char zShort[12];
1042
+ const char *zDate = db_column_text(&q, 0);
1043
+ int rid = db_column_int(&q, 1);
1044
+ const char *zChngUuid = db_column_text(&q, 2);
1045
+ const char *zFile = db_column_text(&q, 4);
1046
+ memcpy(zShort, zChngUuid, 10);
1047
+ zShort[10] = 0;
1048
+ if( zFile!=0 ){
1049
+ const char *zSrc = db_column_text(&q, 3);
1050
+ const char *zUser = db_column_text(&q, 5);
1051
+ if( zSrc==0 || zSrc[0]==0 ){
1052
+ fossil_print("Delete attachment %h\n", zFile);
1053
+ }else{
1054
+ fossil_print("Add attachment %h\n", zFile);
1055
+ }
1056
+ fossil_print(" by %h on %h\n", zUser, zDate);
1057
+ }else{
1058
+ pTicket = manifest_get(rid, CFTYPE_TICKET);
1059
+ if( pTicket ){
1060
+ int i;
1061
+
1062
+ fossil_print("Ticket Change by %h on %h:\n", pTicket->zUser, zDate);
1063
+ for(i=0; i<pTicket->nField; i++){
1064
+ Blob val;
1065
+ const char *z;
1066
+ z = pTicket->aField[i].zName;
1067
+ blob_set(&val, pTicket->aField[i].zValue);
1068
+ if( z[0]=='+' ){
1069
+ fossil_print(" Append to ");
1070
+ z++;
1071
+ }else{
1072
+ fossil_print(" Change ");
1073
+ }
1074
+ fossil_print("%h: ",z);
1075
+ if( blob_size(&val)>50 || contains_newline(&val)) {
1076
+ fossil_print("\n ",blob_str(&val));
1077
+ comment_print(blob_str(&val),4,79);
1078
+ }else{
1079
+ fossil_print("%s\n",blob_str(&val));
1080
+ }
1081
+ blob_reset(&val);
1082
+ }
1083
+ }
1084
+ manifest_destroy(pTicket);
1085
+ }
1086
+ }
1087
+ db_finalize(&q);
1088
+ return;
1089
+ }
10041090
/* read all given ticket field/value pairs from command line */
10051091
if( i==g.argc ){
10061092
fossil_fatal("empty %s command aborted!",g.argv[2]);
10071093
}
10081094
getAllTicketFields();
10091095
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101 const char *zName;
102 Stmt q;
103 int i, n, size, j;
104
105 zName = PD("name","-none-");
106 db_prepare(&q, "SELECT datetime(tkt_mtime) AS tkt_datetime, *"
107 " FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108 if( db_step(&q)==SQLITE_ROW ){
109 n = db_column_count(&q);
110 for(i=0; i<n; i++){
111 const char *zVal = db_column_text(&q, i);
@@ -899,10 +899,14 @@
899 **
900 ** %fossil ticket add FIELD VALUE ?FIELD VALUE .. ? ?-q|--quote?
901 **
902 ** like set, but create a new ticket with the given values.
903 **
 
 
 
 
904 ** The values in set|add are not validated against the definitions
905 ** given in "Ticket Common Script".
906 */
907 void ticket_cmd(void){
908 int n;
@@ -916,16 +920,16 @@
916 if( !db_exists("SELECT 1 FROM user WHERE login=%Q", g.zLogin) ){
917 fossil_fatal("no such user: %s", g.zLogin);
918 }
919
920 if( g.argc<3 ){
921 usage("add|fieldlist|set|show");
922 }else{
923 n = strlen(g.argv[2]);
924 if( n==1 && g.argv[2][0]=='s' ){
925 /* set/show cannot be distinguished, so show the usage */
926 usage("add|fieldlist|set|show");
927 }else if( strncmp(g.argv[2],"list",n)==0 ){
928 if( g.argc==3 ){
929 usage("list fields|reports");
930 }else{
931 n = strlen(g.argv[3]);
@@ -970,19 +974,24 @@
970 rptshow( zRep, zSep, zFilterUuid, tktEncoding );
971
972 }
973 }else{
974 /* add a new ticket or update an existing ticket */
975 enum { set,add,err } eCmd = err;
976 int i = 0;
977 int rid;
978 const char *zTktUuid = 0;
979 Blob tktchng, cksum;
980
981 /* get command type (set/add) and get uuid, if needed for set */
982 if( strncmp(g.argv[2],"set",n)==0 || strncmp(g.argv[2],"change",n)==0 ){
983 eCmd = set;
 
 
 
 
 
984 if( g.argc==3 ){
985 usage("set TICKETUUID");
986 }
987 zTktUuid = db_text(0,
988 "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%s*'", g.argv[3]
@@ -996,13 +1005,90 @@
996 i = 3;
997 zTktUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
998 }
999 /* none of set/add, so show the usage! */
1000 if( eCmd==err ){
1001 usage("add|fieldlist|set|show");
1002 }
1003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1004 /* read all given ticket field/value pairs from command line */
1005 if( i==g.argc ){
1006 fossil_fatal("empty %s command aborted!",g.argv[2]);
1007 }
1008 getAllTicketFields();
1009
--- src/tkt.c
+++ src/tkt.c
@@ -101,11 +101,11 @@
101 const char *zName;
102 Stmt q;
103 int i, n, size, j;
104
105 zName = PD("name","-none-");
106 db_prepare(&q, "SELECT datetime(tkt_mtime,'localtime') AS tkt_datetime, *"
107 " FROM ticket WHERE tkt_uuid GLOB '%q*'", zName);
108 if( db_step(&q)==SQLITE_ROW ){
109 n = db_column_count(&q);
110 for(i=0; i<n; i++){
111 const char *zVal = db_column_text(&q, i);
@@ -899,10 +899,14 @@
899 **
900 ** %fossil ticket add FIELD VALUE ?FIELD VALUE .. ? ?-q|--quote?
901 **
902 ** like set, but create a new ticket with the given values.
903 **
904 ** %fossil ticket history TICKETUUID
905 **
906 ** Show the complete change history for the ticket
907 **
908 ** The values in set|add are not validated against the definitions
909 ** given in "Ticket Common Script".
910 */
911 void ticket_cmd(void){
912 int n;
@@ -916,16 +920,16 @@
920 if( !db_exists("SELECT 1 FROM user WHERE login=%Q", g.zLogin) ){
921 fossil_fatal("no such user: %s", g.zLogin);
922 }
923
924 if( g.argc<3 ){
925 usage("add|fieldlist|set|show|history");
926 }else{
927 n = strlen(g.argv[2]);
928 if( n==1 && g.argv[2][0]=='s' ){
929 /* set/show cannot be distinguished, so show the usage */
930 usage("add|fieldlist|set|show|history");
931 }else if( strncmp(g.argv[2],"list",n)==0 ){
932 if( g.argc==3 ){
933 usage("list fields|reports");
934 }else{
935 n = strlen(g.argv[3]);
@@ -970,19 +974,24 @@
974 rptshow( zRep, zSep, zFilterUuid, tktEncoding );
975
976 }
977 }else{
978 /* add a new ticket or update an existing ticket */
979 enum { set,add,history,err } eCmd = err;
980 int i = 0;
981 int rid;
982 const char *zTktUuid = 0;
983 Blob tktchng, cksum;
984
985 /* get command type (set/add) and get uuid, if needed for set */
986 if( strncmp(g.argv[2],"set",n)==0 || strncmp(g.argv[2],"change",n)==0 ||
987 strncmp(g.argv[2],"history",n)==0 ){
988 if( strncmp(g.argv[2],"history",n)==0 ){
989 eCmd = history;
990 }else{
991 eCmd = set;
992 }
993 if( g.argc==3 ){
994 usage("set TICKETUUID");
995 }
996 zTktUuid = db_text(0,
997 "SELECT tkt_uuid FROM ticket WHERE tkt_uuid GLOB '%s*'", g.argv[3]
@@ -996,13 +1005,90 @@
1005 i = 3;
1006 zTktUuid = db_text(0, "SELECT lower(hex(randomblob(20)))");
1007 }
1008 /* none of set/add, so show the usage! */
1009 if( eCmd==err ){
1010 usage("add|fieldlist|set|show|history");
1011 }
1012
1013 /* we just handle history separately here, does not get out */
1014 if( eCmd==history ){
1015 Stmt q;
1016 char *zTitle;
1017 int tagid;
1018
1019 if ( i != g.argc ){
1020 fossil_fatal("no other parameters expected to %s!",g.argv[2]);
1021 }
1022 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname GLOB 'tkt-%q*'",zTktUuid);
1023 if( tagid==0 ){
1024 fossil_fatal("no such ticket %h", zTktUuid);
1025 }
1026 db_prepare(&q,
1027 "SELECT datetime(mtime,'localtime'), objid, uuid, NULL, NULL, NULL"
1028 " FROM event, blob"
1029 " WHERE objid IN (SELECT rid FROM tagxref WHERE tagid=%d)"
1030 " AND blob.rid=event.objid"
1031 " UNION "
1032 "SELECT datetime(mtime,'localtime'), attachid, uuid, src, filename, user"
1033 " FROM attachment, blob"
1034 " WHERE target=(SELECT substr(tagname,5) FROM tag WHERE tagid=%d)"
1035 " AND blob.rid=attachid"
1036 " ORDER BY 1 DESC",
1037 tagid, tagid
1038 );
1039 while( db_step(&q)==SQLITE_ROW ){
1040 Manifest *pTicket;
1041 char zShort[12];
1042 const char *zDate = db_column_text(&q, 0);
1043 int rid = db_column_int(&q, 1);
1044 const char *zChngUuid = db_column_text(&q, 2);
1045 const char *zFile = db_column_text(&q, 4);
1046 memcpy(zShort, zChngUuid, 10);
1047 zShort[10] = 0;
1048 if( zFile!=0 ){
1049 const char *zSrc = db_column_text(&q, 3);
1050 const char *zUser = db_column_text(&q, 5);
1051 if( zSrc==0 || zSrc[0]==0 ){
1052 fossil_print("Delete attachment %h\n", zFile);
1053 }else{
1054 fossil_print("Add attachment %h\n", zFile);
1055 }
1056 fossil_print(" by %h on %h\n", zUser, zDate);
1057 }else{
1058 pTicket = manifest_get(rid, CFTYPE_TICKET);
1059 if( pTicket ){
1060 int i;
1061
1062 fossil_print("Ticket Change by %h on %h:\n", pTicket->zUser, zDate);
1063 for(i=0; i<pTicket->nField; i++){
1064 Blob val;
1065 const char *z;
1066 z = pTicket->aField[i].zName;
1067 blob_set(&val, pTicket->aField[i].zValue);
1068 if( z[0]=='+' ){
1069 fossil_print(" Append to ");
1070 z++;
1071 }else{
1072 fossil_print(" Change ");
1073 }
1074 fossil_print("%h: ",z);
1075 if( blob_size(&val)>50 || contains_newline(&val)) {
1076 fossil_print("\n ",blob_str(&val));
1077 comment_print(blob_str(&val),4,79);
1078 }else{
1079 fossil_print("%s\n",blob_str(&val));
1080 }
1081 blob_reset(&val);
1082 }
1083 }
1084 manifest_destroy(pTicket);
1085 }
1086 }
1087 db_finalize(&q);
1088 return;
1089 }
1090 /* read all given ticket field/value pairs from command line */
1091 if( i==g.argc ){
1092 fossil_fatal("empty %s command aborted!",g.argv[2]);
1093 }
1094 getAllTicketFields();
1095
+32 -9
--- src/undo.c
+++ src/undo.c
@@ -30,25 +30,33 @@
3030
*/
3131
static void undo_one(const char *zPathname, int redoFlag){
3232
Stmt q;
3333
char *zFullname;
3434
db_prepare(&q,
35
- "SELECT content, existsflag, isExe FROM undo"
35
+ "SELECT content, existsflag, isExe, isLink FROM undo"
3636
" WHERE pathname=%Q AND redoflag=%d",
3737
zPathname, redoFlag
3838
);
3939
if( db_step(&q)==SQLITE_ROW ){
4040
int old_exists;
4141
int new_exists;
4242
int old_exe;
4343
int new_exe;
44
+ int new_link;
45
+ int old_link;
4446
Blob current;
4547
Blob new;
4648
zFullname = mprintf("%s/%s", g.zLocalRoot, zPathname);
49
+ old_link = db_column_int(&q, 3);
50
+ new_link = file_islink(zFullname);
4751
new_exists = file_size(zFullname)>=0;
4852
if( new_exists ){
49
- blob_read_from_file(&current, zFullname);
53
+ if( new_link ){
54
+ blob_read_link(&current, zFullname);
55
+ }else{
56
+ blob_read_from_file(&current, zFullname);
57
+ }
5058
new_exe = file_isexe(zFullname);
5159
}else{
5260
blob_zero(&current);
5361
new_exe = 0;
5462
}
@@ -62,24 +70,31 @@
6270
if( new_exists ){
6371
fossil_print("%s %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
6472
}else{
6573
fossil_print("NEW %s\n", zPathname);
6674
}
67
- blob_write_to_file(&new, zFullname);
75
+ if( new_exists && (new_link || old_link) ){
76
+ file_delete(zFullname);
77
+ }
78
+ if( old_link ){
79
+ create_symlink(blob_str(&new), zFullname);
80
+ }else{
81
+ blob_write_to_file(&new, zFullname);
82
+ }
6883
file_setexe(zFullname, old_exe);
6984
}else{
7085
fossil_print("DELETE %s\n", zPathname);
7186
file_delete(zFullname);
7287
}
7388
blob_reset(&new);
7489
free(zFullname);
7590
db_finalize(&q);
7691
db_prepare(&q,
77
- "UPDATE undo SET content=:c, existsflag=%d, isExe=%d,"
92
+ "UPDATE undo SET content=:c, existsflag=%d, isExe=%d, isLink=%d,"
7893
" redoflag=NOT redoflag"
7994
" WHERE pathname=%Q",
80
- new_exists, new_exe, zPathname
95
+ new_exists, new_exe, new_link, zPathname
8196
);
8297
if( new_exists ){
8398
db_bind_blob(&q, ":c", &current);
8499
}
85100
db_step(&q);
@@ -209,10 +224,11 @@
209224
@ CREATE TABLE %s.undo(
210225
@ pathname TEXT UNIQUE, -- Name of the file
211226
@ redoflag BOOLEAN, -- 0 for undoable. 1 for redoable
212227
@ existsflag BOOLEAN, -- True if the file exists
213228
@ isExe BOOLEAN, -- True if the file is executable
229
+ @ isLink BOOLEAN, -- True if the file is symlink
214230
@ content BLOB -- Saved content
215231
@ );
216232
@ CREATE TABLE %s.undo_vfile AS SELECT * FROM vfile;
217233
@ CREATE TABLE %s.undo_vmerge AS SELECT * FROM vmerge;
218234
;
@@ -249,22 +265,29 @@
249265
*/
250266
void undo_save(const char *zPathname){
251267
char *zFullname;
252268
Blob content;
253269
int existsFlag;
270
+ int isLink;
254271
Stmt q;
255272
256273
if( !undoActive ) return;
257274
zFullname = mprintf("%s%s", g.zLocalRoot, zPathname);
258275
existsFlag = file_size(zFullname)>=0;
276
+ isLink = file_islink(zFullname);
259277
db_prepare(&q,
260
- "INSERT OR IGNORE INTO undo(pathname,redoflag,existsflag,isExe,content)"
261
- " VALUES(%Q,0,%d,%d,:c)",
262
- zPathname, existsFlag, file_isexe(zFullname)
278
+ "INSERT OR IGNORE INTO"
279
+ " undo(pathname,redoflag,existsflag,isExe,isLink,content)"
280
+ " VALUES(%Q,0,%d,%d,%d,:c)",
281
+ zPathname, existsFlag, file_isexe(zFullname), isLink
263282
);
264283
if( existsFlag ){
265
- blob_read_from_file(&content, zFullname);
284
+ if( isLink ){
285
+ blob_read_link(&content, zFullname);
286
+ }else{
287
+ blob_read_from_file(&content, zFullname);
288
+ }
266289
db_bind_blob(&q, ":c", &content);
267290
}
268291
free(zFullname);
269292
db_step(&q);
270293
db_finalize(&q);
271294
--- src/undo.c
+++ src/undo.c
@@ -30,25 +30,33 @@
30 */
31 static void undo_one(const char *zPathname, int redoFlag){
32 Stmt q;
33 char *zFullname;
34 db_prepare(&q,
35 "SELECT content, existsflag, isExe FROM undo"
36 " WHERE pathname=%Q AND redoflag=%d",
37 zPathname, redoFlag
38 );
39 if( db_step(&q)==SQLITE_ROW ){
40 int old_exists;
41 int new_exists;
42 int old_exe;
43 int new_exe;
 
 
44 Blob current;
45 Blob new;
46 zFullname = mprintf("%s/%s", g.zLocalRoot, zPathname);
 
 
47 new_exists = file_size(zFullname)>=0;
48 if( new_exists ){
49 blob_read_from_file(&current, zFullname);
 
 
 
 
50 new_exe = file_isexe(zFullname);
51 }else{
52 blob_zero(&current);
53 new_exe = 0;
54 }
@@ -62,24 +70,31 @@
62 if( new_exists ){
63 fossil_print("%s %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
64 }else{
65 fossil_print("NEW %s\n", zPathname);
66 }
67 blob_write_to_file(&new, zFullname);
 
 
 
 
 
 
 
68 file_setexe(zFullname, old_exe);
69 }else{
70 fossil_print("DELETE %s\n", zPathname);
71 file_delete(zFullname);
72 }
73 blob_reset(&new);
74 free(zFullname);
75 db_finalize(&q);
76 db_prepare(&q,
77 "UPDATE undo SET content=:c, existsflag=%d, isExe=%d,"
78 " redoflag=NOT redoflag"
79 " WHERE pathname=%Q",
80 new_exists, new_exe, zPathname
81 );
82 if( new_exists ){
83 db_bind_blob(&q, ":c", &current);
84 }
85 db_step(&q);
@@ -209,10 +224,11 @@
209 @ CREATE TABLE %s.undo(
210 @ pathname TEXT UNIQUE, -- Name of the file
211 @ redoflag BOOLEAN, -- 0 for undoable. 1 for redoable
212 @ existsflag BOOLEAN, -- True if the file exists
213 @ isExe BOOLEAN, -- True if the file is executable
 
214 @ content BLOB -- Saved content
215 @ );
216 @ CREATE TABLE %s.undo_vfile AS SELECT * FROM vfile;
217 @ CREATE TABLE %s.undo_vmerge AS SELECT * FROM vmerge;
218 ;
@@ -249,22 +265,29 @@
249 */
250 void undo_save(const char *zPathname){
251 char *zFullname;
252 Blob content;
253 int existsFlag;
 
254 Stmt q;
255
256 if( !undoActive ) return;
257 zFullname = mprintf("%s%s", g.zLocalRoot, zPathname);
258 existsFlag = file_size(zFullname)>=0;
 
259 db_prepare(&q,
260 "INSERT OR IGNORE INTO undo(pathname,redoflag,existsflag,isExe,content)"
261 " VALUES(%Q,0,%d,%d,:c)",
262 zPathname, existsFlag, file_isexe(zFullname)
 
263 );
264 if( existsFlag ){
265 blob_read_from_file(&content, zFullname);
 
 
 
 
266 db_bind_blob(&q, ":c", &content);
267 }
268 free(zFullname);
269 db_step(&q);
270 db_finalize(&q);
271
--- src/undo.c
+++ src/undo.c
@@ -30,25 +30,33 @@
30 */
31 static void undo_one(const char *zPathname, int redoFlag){
32 Stmt q;
33 char *zFullname;
34 db_prepare(&q,
35 "SELECT content, existsflag, isExe, isLink FROM undo"
36 " WHERE pathname=%Q AND redoflag=%d",
37 zPathname, redoFlag
38 );
39 if( db_step(&q)==SQLITE_ROW ){
40 int old_exists;
41 int new_exists;
42 int old_exe;
43 int new_exe;
44 int new_link;
45 int old_link;
46 Blob current;
47 Blob new;
48 zFullname = mprintf("%s/%s", g.zLocalRoot, zPathname);
49 old_link = db_column_int(&q, 3);
50 new_link = file_islink(zFullname);
51 new_exists = file_size(zFullname)>=0;
52 if( new_exists ){
53 if( new_link ){
54 blob_read_link(&current, zFullname);
55 }else{
56 blob_read_from_file(&current, zFullname);
57 }
58 new_exe = file_isexe(zFullname);
59 }else{
60 blob_zero(&current);
61 new_exe = 0;
62 }
@@ -62,24 +70,31 @@
70 if( new_exists ){
71 fossil_print("%s %s\n", redoFlag ? "REDO" : "UNDO", zPathname);
72 }else{
73 fossil_print("NEW %s\n", zPathname);
74 }
75 if( new_exists && (new_link || old_link) ){
76 file_delete(zFullname);
77 }
78 if( old_link ){
79 create_symlink(blob_str(&new), zFullname);
80 }else{
81 blob_write_to_file(&new, zFullname);
82 }
83 file_setexe(zFullname, old_exe);
84 }else{
85 fossil_print("DELETE %s\n", zPathname);
86 file_delete(zFullname);
87 }
88 blob_reset(&new);
89 free(zFullname);
90 db_finalize(&q);
91 db_prepare(&q,
92 "UPDATE undo SET content=:c, existsflag=%d, isExe=%d, isLink=%d,"
93 " redoflag=NOT redoflag"
94 " WHERE pathname=%Q",
95 new_exists, new_exe, new_link, zPathname
96 );
97 if( new_exists ){
98 db_bind_blob(&q, ":c", &current);
99 }
100 db_step(&q);
@@ -209,10 +224,11 @@
224 @ CREATE TABLE %s.undo(
225 @ pathname TEXT UNIQUE, -- Name of the file
226 @ redoflag BOOLEAN, -- 0 for undoable. 1 for redoable
227 @ existsflag BOOLEAN, -- True if the file exists
228 @ isExe BOOLEAN, -- True if the file is executable
229 @ isLink BOOLEAN, -- True if the file is symlink
230 @ content BLOB -- Saved content
231 @ );
232 @ CREATE TABLE %s.undo_vfile AS SELECT * FROM vfile;
233 @ CREATE TABLE %s.undo_vmerge AS SELECT * FROM vmerge;
234 ;
@@ -249,22 +265,29 @@
265 */
266 void undo_save(const char *zPathname){
267 char *zFullname;
268 Blob content;
269 int existsFlag;
270 int isLink;
271 Stmt q;
272
273 if( !undoActive ) return;
274 zFullname = mprintf("%s%s", g.zLocalRoot, zPathname);
275 existsFlag = file_size(zFullname)>=0;
276 isLink = file_islink(zFullname);
277 db_prepare(&q,
278 "INSERT OR IGNORE INTO"
279 " undo(pathname,redoflag,existsflag,isExe,isLink,content)"
280 " VALUES(%Q,0,%d,%d,%d,:c)",
281 zPathname, existsFlag, file_isexe(zFullname), isLink
282 );
283 if( existsFlag ){
284 if( isLink ){
285 blob_read_link(&content, zFullname);
286 }else{
287 blob_read_from_file(&content, zFullname);
288 }
289 db_bind_blob(&q, ":c", &content);
290 }
291 free(zFullname);
292 db_step(&q);
293 db_finalize(&q);
294
+63 -31
--- src/update.c
+++ src/update.c
@@ -202,10 +202,12 @@
202202
"CREATE TEMP TABLE fv("
203203
" fn TEXT PRIMARY KEY," /* The filename relative to root */
204204
" idv INTEGER," /* VFILE entry for current version */
205205
" idt INTEGER," /* VFILE entry for target version */
206206
" chnged BOOLEAN," /* True if current version has been edited */
207
+ " islinkv BOOLEAN," /* True if current file is a link */
208
+ " islinkt BOOLEAN," /* True if target file is a link */
207209
" ridv INTEGER," /* Record ID for current version */
208210
" ridt INTEGER," /* Record ID for target */
209211
" isexe BOOLEAN," /* Does target have execute permission? */
210212
" fnt TEXT" /* Filename of same file on target version */
211213
");"
@@ -221,11 +223,11 @@
221223
);
222224
223225
/* Compute file name changes on V->T. Record name changes in files that
224226
** have changed locally.
225227
*/
226
- find_filename_changes(vid, tid, &nChng, &aChng);
228
+ find_filename_changes(vid, tid, 1, &nChng, &aChng, debugFlag ? "V->T": 0);
227229
if( nChng ){
228230
for(i=0; i<nChng; i++){
229231
db_multi_exec(
230232
"UPDATE fv"
231233
" SET fnt=(SELECT name FROM filename WHERE fnid=%d)"
@@ -254,22 +256,35 @@
254256
"UPDATE fv SET"
255257
" idt=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnt),0),"
256258
" ridt=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnt),0)",
257259
tid, tid
258260
);
261
+
262
+ /*
263
+ ** Add islink information
264
+ */
265
+ db_multi_exec(
266
+ "UPDATE fv SET"
267
+ " islinkv=coalesce((SELECT islink FROM vfile WHERE vid=%d AND pathname=fnt),0),"
268
+ " islinkt=coalesce((SELECT islink FROM vfile WHERE vid=%d AND pathname=fnt),0)",
269
+ vid, tid
270
+ );
271
+
259272
260273
if( debugFlag ){
261274
db_prepare(&q,
262
- "SELECT rowid, fn, fnt, chnged, ridv, ridt, isexe FROM fv"
275
+ "SELECT rowid, fn, fnt, chnged, ridv, ridt, isexe, islinkv, islinkt FROM fv"
263276
);
264277
while( db_step(&q)==SQLITE_ROW ){
265
- fossil_print("%3d: ridv=%-4d ridt=%-4d chnged=%d isexe=%d\n",
278
+ fossil_print("%3d: ridv=%-4d ridt=%-4d chnged=%d isexe=%d islinkv=%d islinkt=%d\n",
266279
db_column_int(&q, 0),
267280
db_column_int(&q, 4),
268281
db_column_int(&q, 5),
269282
db_column_int(&q, 3),
270
- db_column_int(&q, 6));
283
+ db_column_int(&q, 6),
284
+ db_column_int(&q, 7),
285
+ db_column_int(&q, 8));
271286
fossil_print(" fnv = [%s]\n", db_column_text(&q, 1));
272287
fossil_print(" fnt = [%s]\n", db_column_text(&q, 2));
273288
}
274289
db_finalize(&q);
275290
}
@@ -288,11 +303,11 @@
288303
blob_append(&sql, "DELETE FROM fv WHERE ", -1);
289304
zSep = "";
290305
for(i=3; i<g.argc; i++){
291306
file_tree_name(g.argv[i], &treename, 1);
292307
if( file_isdir(g.argv[i])==1 ){
293
- if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
308
+ if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
294309
blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
295310
}else{
296311
blob_reset(&sql);
297312
break;
298313
}
@@ -309,11 +324,11 @@
309324
/*
310325
** Alter the content of the checkout so that it conforms with the
311326
** target
312327
*/
313328
db_prepare(&q,
314
- "SELECT fn, idv, ridv, idt, ridt, chnged, fnt, isexe FROM fv ORDER BY 1"
329
+ "SELECT fn, idv, ridv, idt, ridt, chnged, fnt, isexe, islinkv, islinkt FROM fv ORDER BY 1"
315330
);
316331
db_prepare(&mtimeXfer,
317332
"UPDATE vfile SET mtime=(SELECT mtime FROM vfile WHERE id=:idv)"
318333
" WHERE id=:idt"
319334
);
@@ -327,10 +342,12 @@
327342
int idt = db_column_int(&q, 3); /* VFILE entry for target */
328343
int ridt = db_column_int(&q, 4); /* RecordID for target */
329344
int chnged = db_column_int(&q, 5); /* Current is edited */
330345
const char *zNewName = db_column_text(&q,6);/* New filename */
331346
int isexe = db_column_int(&q, 7); /* EXE perm for new file */
347
+ int islinkv = db_column_int(&q, 8); /* Is current file is a link */
348
+ int islinkt = db_column_int(&q, 9); /* Is target file is a link */
332349
char *zFullPath; /* Full pathname of the file */
333350
char *zFullNewPath; /* Full pathname of dest */
334351
char nameChng; /* True if the name changed */
335352
336353
zFullPath = mprintf("%s%s", g.zLocalRoot, zName);
@@ -381,30 +398,35 @@
381398
if( nameChng ){
382399
fossil_print("MERGE %s -> %s\n", zName, zNewName);
383400
}else{
384401
fossil_print("MERGE %s\n", zName);
385402
}
386
- undo_save(zName);
387
- content_get(ridt, &t);
388
- content_get(ridv, &v);
389
- rc = merge_3way(&v, zFullPath, &t, &r);
390
- if( rc>=0 ){
391
- if( !nochangeFlag ){
392
- blob_write_to_file(&r, zFullNewPath);
393
- file_setexe(zFullNewPath, isexe);
394
- }
395
- if( rc>0 ){
396
- fossil_print("***** %d merge conflicts in %s\n", rc, zNewName);
397
- nConflict++;
398
- }
399
- }else{
400
- if( !nochangeFlag ){
401
- blob_write_to_file(&t, zFullNewPath);
402
- file_setexe(zFullNewPath, isexe);
403
- }
404
- fossil_print("***** Cannot merge binary file %s\n", zNewName);
405
- nConflict++;
403
+ if( islinkv || islinkt /* || file_islink(zFullPath) */ ){
404
+ fossil_print("***** Cannot merge symlink %s\n", zNewName);
405
+ nConflict++;
406
+ }else{
407
+ undo_save(zName);
408
+ content_get(ridt, &t);
409
+ content_get(ridv, &v);
410
+ rc = merge_3way(&v, zFullPath, &t, &r);
411
+ if( rc>=0 ){
412
+ if( !nochangeFlag ){
413
+ blob_write_to_file(&r, zFullNewPath);
414
+ file_setexe(zFullNewPath, isexe);
415
+ }
416
+ if( rc>0 ){
417
+ fossil_print("***** %d merge conflicts in %s\n", rc, zNewName);
418
+ nConflict++;
419
+ }
420
+ }else{
421
+ if( !nochangeFlag ){
422
+ blob_write_to_file(&t, zFullNewPath);
423
+ file_setexe(zFullNewPath, isexe);
424
+ }
425
+ fossil_print("***** Cannot merge binary file %s\n", zNewName);
426
+ nConflict++;
427
+ }
406428
}
407429
if( nameChng && !nochangeFlag ) file_delete(zFullPath);
408430
blob_reset(&v);
409431
blob_reset(&t);
410432
blob_reset(&r);
@@ -522,10 +544,11 @@
522544
*/
523545
int historical_version_of_file(
524546
const char *revision, /* The checkin containing the file */
525547
const char *file, /* Full treename of the file */
526548
Blob *content, /* Put the content here */
549
+ int *pIsLink, /* Set to true if file is link. */
527550
int *pIsExe, /* Set to true if file is executable */
528551
int errCode /* Error code if file not found. Panic if 0. */
529552
){
530553
Manifest *pManifest;
531554
ManifestFile *pFile;
@@ -544,11 +567,12 @@
544567
545568
if( pManifest ){
546569
pFile = manifest_file_seek(pManifest, file);
547570
if( pFile ){
548571
rid = uuid_to_rid(pFile->zUuid, 0);
549
- if( pIsExe ) *pIsExe = manifest_file_mperm(pFile);
572
+ if( pIsExe ) *pIsExe = ( manifest_file_mperm(pFile)==PERM_EXE );
573
+ if( pIsLink ) *pIsLink = ( manifest_file_mperm(pFile)==PERM_LNK );
550574
manifest_destroy(pManifest);
551575
return content_get(rid, content);
552576
}
553577
manifest_destroy(pManifest);
554578
if( errCode<=0 ){
@@ -627,14 +651,15 @@
627651
int vid = db_lget_int("checkout", 0);
628652
zRevision = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
629653
}
630654
while( db_step(&q)==SQLITE_ROW ){
631655
int isExe = 0;
656
+ int isLink = 0;
632657
char *zFull;
633658
zFile = db_column_text(&q, 0);
634659
zFull = mprintf("%/%/", g.zLocalRoot, zFile);
635
- errCode = historical_version_of_file(zRevision, zFile, &record, &isExe,2);
660
+ errCode = historical_version_of_file(zRevision, zFile, &record, &isLink, &isExe,2);
636661
if( errCode==2 ){
637662
if( db_int(0, "SELECT rid FROM vfile WHERE pathname=%Q", zFile)==0 ){
638663
fossil_print("UNMANAGE: %s\n", zFile);
639664
}else{
640665
undo_save(zFile);
@@ -643,24 +668,31 @@
643668
}
644669
db_multi_exec("DELETE FROM vfile WHERE pathname=%Q", zFile);
645670
}else{
646671
sqlite3_int64 mtime;
647672
undo_save(zFile);
648
- blob_write_to_file(&record, zFull);
673
+ if( file_size(zFull)>=0 && (isLink || file_islink(zFull)) ){
674
+ file_delete(zFull);
675
+ }
676
+ if( isLink ){
677
+ create_symlink(blob_str(&record), zFull);
678
+ }else{
679
+ blob_write_to_file(&record, zFull);
680
+ }
649681
file_setexe(zFull, isExe);
650682
fossil_print("REVERTED: %s\n", zFile);
651683
mtime = file_mtime(zFull);
652684
db_multi_exec(
653685
"UPDATE vfile"
654
- " SET mtime=%lld, chnged=0, deleted=0, isexe=%d, mrid=rid,"
686
+ " SET mtime=%lld, chnged=0, deleted=0, isexe=%d, islink=%d, mrid=rid,"
655687
" pathname=coalesce(origname,pathname), origname=NULL"
656688
" WHERE pathname=%Q",
657
- mtime, isExe, zFile
689
+ mtime, isExe, isLink, zFile
658690
);
659691
}
660692
blob_reset(&record);
661693
free(zFull);
662694
}
663695
db_finalize(&q);
664696
undo_finish();
665697
db_end_transaction(0);
666698
}
667699
--- src/update.c
+++ src/update.c
@@ -202,10 +202,12 @@
202 "CREATE TEMP TABLE fv("
203 " fn TEXT PRIMARY KEY," /* The filename relative to root */
204 " idv INTEGER," /* VFILE entry for current version */
205 " idt INTEGER," /* VFILE entry for target version */
206 " chnged BOOLEAN," /* True if current version has been edited */
 
 
207 " ridv INTEGER," /* Record ID for current version */
208 " ridt INTEGER," /* Record ID for target */
209 " isexe BOOLEAN," /* Does target have execute permission? */
210 " fnt TEXT" /* Filename of same file on target version */
211 ");"
@@ -221,11 +223,11 @@
221 );
222
223 /* Compute file name changes on V->T. Record name changes in files that
224 ** have changed locally.
225 */
226 find_filename_changes(vid, tid, &nChng, &aChng);
227 if( nChng ){
228 for(i=0; i<nChng; i++){
229 db_multi_exec(
230 "UPDATE fv"
231 " SET fnt=(SELECT name FROM filename WHERE fnid=%d)"
@@ -254,22 +256,35 @@
254 "UPDATE fv SET"
255 " idt=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnt),0),"
256 " ridt=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnt),0)",
257 tid, tid
258 );
 
 
 
 
 
 
 
 
 
 
 
259
260 if( debugFlag ){
261 db_prepare(&q,
262 "SELECT rowid, fn, fnt, chnged, ridv, ridt, isexe FROM fv"
263 );
264 while( db_step(&q)==SQLITE_ROW ){
265 fossil_print("%3d: ridv=%-4d ridt=%-4d chnged=%d isexe=%d\n",
266 db_column_int(&q, 0),
267 db_column_int(&q, 4),
268 db_column_int(&q, 5),
269 db_column_int(&q, 3),
270 db_column_int(&q, 6));
 
 
271 fossil_print(" fnv = [%s]\n", db_column_text(&q, 1));
272 fossil_print(" fnt = [%s]\n", db_column_text(&q, 2));
273 }
274 db_finalize(&q);
275 }
@@ -288,11 +303,11 @@
288 blob_append(&sql, "DELETE FROM fv WHERE ", -1);
289 zSep = "";
290 for(i=3; i<g.argc; i++){
291 file_tree_name(g.argv[i], &treename, 1);
292 if( file_isdir(g.argv[i])==1 ){
293 if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
294 blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
295 }else{
296 blob_reset(&sql);
297 break;
298 }
@@ -309,11 +324,11 @@
309 /*
310 ** Alter the content of the checkout so that it conforms with the
311 ** target
312 */
313 db_prepare(&q,
314 "SELECT fn, idv, ridv, idt, ridt, chnged, fnt, isexe FROM fv ORDER BY 1"
315 );
316 db_prepare(&mtimeXfer,
317 "UPDATE vfile SET mtime=(SELECT mtime FROM vfile WHERE id=:idv)"
318 " WHERE id=:idt"
319 );
@@ -327,10 +342,12 @@
327 int idt = db_column_int(&q, 3); /* VFILE entry for target */
328 int ridt = db_column_int(&q, 4); /* RecordID for target */
329 int chnged = db_column_int(&q, 5); /* Current is edited */
330 const char *zNewName = db_column_text(&q,6);/* New filename */
331 int isexe = db_column_int(&q, 7); /* EXE perm for new file */
 
 
332 char *zFullPath; /* Full pathname of the file */
333 char *zFullNewPath; /* Full pathname of dest */
334 char nameChng; /* True if the name changed */
335
336 zFullPath = mprintf("%s%s", g.zLocalRoot, zName);
@@ -381,30 +398,35 @@
381 if( nameChng ){
382 fossil_print("MERGE %s -> %s\n", zName, zNewName);
383 }else{
384 fossil_print("MERGE %s\n", zName);
385 }
386 undo_save(zName);
387 content_get(ridt, &t);
388 content_get(ridv, &v);
389 rc = merge_3way(&v, zFullPath, &t, &r);
390 if( rc>=0 ){
391 if( !nochangeFlag ){
392 blob_write_to_file(&r, zFullNewPath);
393 file_setexe(zFullNewPath, isexe);
394 }
395 if( rc>0 ){
396 fossil_print("***** %d merge conflicts in %s\n", rc, zNewName);
397 nConflict++;
398 }
399 }else{
400 if( !nochangeFlag ){
401 blob_write_to_file(&t, zFullNewPath);
402 file_setexe(zFullNewPath, isexe);
403 }
404 fossil_print("***** Cannot merge binary file %s\n", zNewName);
405 nConflict++;
 
 
 
 
 
406 }
407 if( nameChng && !nochangeFlag ) file_delete(zFullPath);
408 blob_reset(&v);
409 blob_reset(&t);
410 blob_reset(&r);
@@ -522,10 +544,11 @@
522 */
523 int historical_version_of_file(
524 const char *revision, /* The checkin containing the file */
525 const char *file, /* Full treename of the file */
526 Blob *content, /* Put the content here */
 
527 int *pIsExe, /* Set to true if file is executable */
528 int errCode /* Error code if file not found. Panic if 0. */
529 ){
530 Manifest *pManifest;
531 ManifestFile *pFile;
@@ -544,11 +567,12 @@
544
545 if( pManifest ){
546 pFile = manifest_file_seek(pManifest, file);
547 if( pFile ){
548 rid = uuid_to_rid(pFile->zUuid, 0);
549 if( pIsExe ) *pIsExe = manifest_file_mperm(pFile);
 
550 manifest_destroy(pManifest);
551 return content_get(rid, content);
552 }
553 manifest_destroy(pManifest);
554 if( errCode<=0 ){
@@ -627,14 +651,15 @@
627 int vid = db_lget_int("checkout", 0);
628 zRevision = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
629 }
630 while( db_step(&q)==SQLITE_ROW ){
631 int isExe = 0;
 
632 char *zFull;
633 zFile = db_column_text(&q, 0);
634 zFull = mprintf("%/%/", g.zLocalRoot, zFile);
635 errCode = historical_version_of_file(zRevision, zFile, &record, &isExe,2);
636 if( errCode==2 ){
637 if( db_int(0, "SELECT rid FROM vfile WHERE pathname=%Q", zFile)==0 ){
638 fossil_print("UNMANAGE: %s\n", zFile);
639 }else{
640 undo_save(zFile);
@@ -643,24 +668,31 @@
643 }
644 db_multi_exec("DELETE FROM vfile WHERE pathname=%Q", zFile);
645 }else{
646 sqlite3_int64 mtime;
647 undo_save(zFile);
648 blob_write_to_file(&record, zFull);
 
 
 
 
 
 
 
649 file_setexe(zFull, isExe);
650 fossil_print("REVERTED: %s\n", zFile);
651 mtime = file_mtime(zFull);
652 db_multi_exec(
653 "UPDATE vfile"
654 " SET mtime=%lld, chnged=0, deleted=0, isexe=%d, mrid=rid,"
655 " pathname=coalesce(origname,pathname), origname=NULL"
656 " WHERE pathname=%Q",
657 mtime, isExe, zFile
658 );
659 }
660 blob_reset(&record);
661 free(zFull);
662 }
663 db_finalize(&q);
664 undo_finish();
665 db_end_transaction(0);
666 }
667
--- src/update.c
+++ src/update.c
@@ -202,10 +202,12 @@
202 "CREATE TEMP TABLE fv("
203 " fn TEXT PRIMARY KEY," /* The filename relative to root */
204 " idv INTEGER," /* VFILE entry for current version */
205 " idt INTEGER," /* VFILE entry for target version */
206 " chnged BOOLEAN," /* True if current version has been edited */
207 " islinkv BOOLEAN," /* True if current file is a link */
208 " islinkt BOOLEAN," /* True if target file is a link */
209 " ridv INTEGER," /* Record ID for current version */
210 " ridt INTEGER," /* Record ID for target */
211 " isexe BOOLEAN," /* Does target have execute permission? */
212 " fnt TEXT" /* Filename of same file on target version */
213 ");"
@@ -221,11 +223,11 @@
223 );
224
225 /* Compute file name changes on V->T. Record name changes in files that
226 ** have changed locally.
227 */
228 find_filename_changes(vid, tid, 1, &nChng, &aChng, debugFlag ? "V->T": 0);
229 if( nChng ){
230 for(i=0; i<nChng; i++){
231 db_multi_exec(
232 "UPDATE fv"
233 " SET fnt=(SELECT name FROM filename WHERE fnid=%d)"
@@ -254,22 +256,35 @@
256 "UPDATE fv SET"
257 " idt=coalesce((SELECT id FROM vfile WHERE vid=%d AND pathname=fnt),0),"
258 " ridt=coalesce((SELECT rid FROM vfile WHERE vid=%d AND pathname=fnt),0)",
259 tid, tid
260 );
261
262 /*
263 ** Add islink information
264 */
265 db_multi_exec(
266 "UPDATE fv SET"
267 " islinkv=coalesce((SELECT islink FROM vfile WHERE vid=%d AND pathname=fnt),0),"
268 " islinkt=coalesce((SELECT islink FROM vfile WHERE vid=%d AND pathname=fnt),0)",
269 vid, tid
270 );
271
272
273 if( debugFlag ){
274 db_prepare(&q,
275 "SELECT rowid, fn, fnt, chnged, ridv, ridt, isexe, islinkv, islinkt FROM fv"
276 );
277 while( db_step(&q)==SQLITE_ROW ){
278 fossil_print("%3d: ridv=%-4d ridt=%-4d chnged=%d isexe=%d islinkv=%d islinkt=%d\n",
279 db_column_int(&q, 0),
280 db_column_int(&q, 4),
281 db_column_int(&q, 5),
282 db_column_int(&q, 3),
283 db_column_int(&q, 6),
284 db_column_int(&q, 7),
285 db_column_int(&q, 8));
286 fossil_print(" fnv = [%s]\n", db_column_text(&q, 1));
287 fossil_print(" fnt = [%s]\n", db_column_text(&q, 2));
288 }
289 db_finalize(&q);
290 }
@@ -288,11 +303,11 @@
303 blob_append(&sql, "DELETE FROM fv WHERE ", -1);
304 zSep = "";
305 for(i=3; i<g.argc; i++){
306 file_tree_name(g.argv[i], &treename, 1);
307 if( file_isdir(g.argv[i])==1 ){
308 if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
309 blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
310 }else{
311 blob_reset(&sql);
312 break;
313 }
@@ -309,11 +324,11 @@
324 /*
325 ** Alter the content of the checkout so that it conforms with the
326 ** target
327 */
328 db_prepare(&q,
329 "SELECT fn, idv, ridv, idt, ridt, chnged, fnt, isexe, islinkv, islinkt FROM fv ORDER BY 1"
330 );
331 db_prepare(&mtimeXfer,
332 "UPDATE vfile SET mtime=(SELECT mtime FROM vfile WHERE id=:idv)"
333 " WHERE id=:idt"
334 );
@@ -327,10 +342,12 @@
342 int idt = db_column_int(&q, 3); /* VFILE entry for target */
343 int ridt = db_column_int(&q, 4); /* RecordID for target */
344 int chnged = db_column_int(&q, 5); /* Current is edited */
345 const char *zNewName = db_column_text(&q,6);/* New filename */
346 int isexe = db_column_int(&q, 7); /* EXE perm for new file */
347 int islinkv = db_column_int(&q, 8); /* Is current file is a link */
348 int islinkt = db_column_int(&q, 9); /* Is target file is a link */
349 char *zFullPath; /* Full pathname of the file */
350 char *zFullNewPath; /* Full pathname of dest */
351 char nameChng; /* True if the name changed */
352
353 zFullPath = mprintf("%s%s", g.zLocalRoot, zName);
@@ -381,30 +398,35 @@
398 if( nameChng ){
399 fossil_print("MERGE %s -> %s\n", zName, zNewName);
400 }else{
401 fossil_print("MERGE %s\n", zName);
402 }
403 if( islinkv || islinkt /* || file_islink(zFullPath) */ ){
404 fossil_print("***** Cannot merge symlink %s\n", zNewName);
405 nConflict++;
406 }else{
407 undo_save(zName);
408 content_get(ridt, &t);
409 content_get(ridv, &v);
410 rc = merge_3way(&v, zFullPath, &t, &r);
411 if( rc>=0 ){
412 if( !nochangeFlag ){
413 blob_write_to_file(&r, zFullNewPath);
414 file_setexe(zFullNewPath, isexe);
415 }
416 if( rc>0 ){
417 fossil_print("***** %d merge conflicts in %s\n", rc, zNewName);
418 nConflict++;
419 }
420 }else{
421 if( !nochangeFlag ){
422 blob_write_to_file(&t, zFullNewPath);
423 file_setexe(zFullNewPath, isexe);
424 }
425 fossil_print("***** Cannot merge binary file %s\n", zNewName);
426 nConflict++;
427 }
428 }
429 if( nameChng && !nochangeFlag ) file_delete(zFullPath);
430 blob_reset(&v);
431 blob_reset(&t);
432 blob_reset(&r);
@@ -522,10 +544,11 @@
544 */
545 int historical_version_of_file(
546 const char *revision, /* The checkin containing the file */
547 const char *file, /* Full treename of the file */
548 Blob *content, /* Put the content here */
549 int *pIsLink, /* Set to true if file is link. */
550 int *pIsExe, /* Set to true if file is executable */
551 int errCode /* Error code if file not found. Panic if 0. */
552 ){
553 Manifest *pManifest;
554 ManifestFile *pFile;
@@ -544,11 +567,12 @@
567
568 if( pManifest ){
569 pFile = manifest_file_seek(pManifest, file);
570 if( pFile ){
571 rid = uuid_to_rid(pFile->zUuid, 0);
572 if( pIsExe ) *pIsExe = ( manifest_file_mperm(pFile)==PERM_EXE );
573 if( pIsLink ) *pIsLink = ( manifest_file_mperm(pFile)==PERM_LNK );
574 manifest_destroy(pManifest);
575 return content_get(rid, content);
576 }
577 manifest_destroy(pManifest);
578 if( errCode<=0 ){
@@ -627,14 +651,15 @@
651 int vid = db_lget_int("checkout", 0);
652 zRevision = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
653 }
654 while( db_step(&q)==SQLITE_ROW ){
655 int isExe = 0;
656 int isLink = 0;
657 char *zFull;
658 zFile = db_column_text(&q, 0);
659 zFull = mprintf("%/%/", g.zLocalRoot, zFile);
660 errCode = historical_version_of_file(zRevision, zFile, &record, &isLink, &isExe,2);
661 if( errCode==2 ){
662 if( db_int(0, "SELECT rid FROM vfile WHERE pathname=%Q", zFile)==0 ){
663 fossil_print("UNMANAGE: %s\n", zFile);
664 }else{
665 undo_save(zFile);
@@ -643,24 +668,31 @@
668 }
669 db_multi_exec("DELETE FROM vfile WHERE pathname=%Q", zFile);
670 }else{
671 sqlite3_int64 mtime;
672 undo_save(zFile);
673 if( file_size(zFull)>=0 && (isLink || file_islink(zFull)) ){
674 file_delete(zFull);
675 }
676 if( isLink ){
677 create_symlink(blob_str(&record), zFull);
678 }else{
679 blob_write_to_file(&record, zFull);
680 }
681 file_setexe(zFull, isExe);
682 fossil_print("REVERTED: %s\n", zFile);
683 mtime = file_mtime(zFull);
684 db_multi_exec(
685 "UPDATE vfile"
686 " SET mtime=%lld, chnged=0, deleted=0, isexe=%d, islink=%d, mrid=rid,"
687 " pathname=coalesce(origname,pathname), origname=NULL"
688 " WHERE pathname=%Q",
689 mtime, isExe, isLink, zFile
690 );
691 }
692 blob_reset(&record);
693 free(zFull);
694 }
695 db_finalize(&q);
696 undo_finish();
697 db_end_transaction(0);
698 }
699
+7 -1
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312312
*/
313313
void user_select(void){
314314
Stmt s;
315315
316316
if( g.userUid ) return;
317
- if( attempt_user(g.zLogin) ) return;
317
+ if( g.zLogin ){
318
+ if( attempt_user(g.zLogin)==0 ){
319
+ fossil_fatal("no such user: %s", g.zLogin);
320
+ }else{
321
+ return;
322
+ }
323
+ }
318324
319325
if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
320326
321327
if( attempt_user(db_get("default-user", 0)) ) return;
322328
323329
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312 */
313 void user_select(void){
314 Stmt s;
315
316 if( g.userUid ) return;
317 if( attempt_user(g.zLogin) ) return;
 
 
 
 
 
 
318
319 if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
320
321 if( attempt_user(db_get("default-user", 0)) ) return;
322
323
--- src/user.c
+++ src/user.c
@@ -312,11 +312,17 @@
312 */
313 void user_select(void){
314 Stmt s;
315
316 if( g.userUid ) return;
317 if( g.zLogin ){
318 if( attempt_user(g.zLogin)==0 ){
319 fossil_fatal("no such user: %s", g.zLogin);
320 }else{
321 return;
322 }
323 }
324
325 if( g.localOpen && attempt_user(db_lget("default-user",0)) ) return;
326
327 if( attempt_user(db_get("default-user", 0)) ) return;
328
329
+55 -27
--- src/vfile.c
+++ src/vfile.c
@@ -90,12 +90,12 @@
9090
db_begin_transaction();
9191
p = manifest_get(vid, CFTYPE_MANIFEST);
9292
if( p==0 ) return;
9393
db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid);
9494
db_prepare(&ins,
95
- "INSERT INTO vfile(vid,isexe,rid,mrid,pathname) "
96
- " VALUES(:vid,:isexe,:id,:id,:name)");
95
+ "INSERT INTO vfile(vid,isexe,islink,rid,mrid,pathname) "
96
+ " VALUES(:vid,:isexe,:islink,:id,:id,:name)");
9797
db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
9898
db_bind_int(&ins, ":vid", vid);
9999
manifest_file_rewind(p);
100100
while( (pFile = manifest_file_next(p,0))!=0 ){
101101
if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
@@ -110,13 +110,14 @@
110110
db_reset(&ridq);
111111
if( rid==0 || size<0 ){
112112
fossil_warning("content missing for %s", pFile->zName);
113113
continue;
114114
}
115
- db_bind_int(&ins, ":isexe", manifest_file_mperm(pFile));
115
+ db_bind_int(&ins, ":isexe", ( manifest_file_mperm(pFile)==PERM_EXE ));
116116
db_bind_int(&ins, ":id", rid);
117117
db_bind_text(&ins, ":name", pFile->zName);
118
+ db_bind_int(&ins, ":islink", ( manifest_file_mperm(pFile)==PERM_LNK ));
118119
db_step(&ins);
119120
db_reset(&ins);
120121
}
121122
db_finalize(&ridq);
122123
db_finalize(&ins);
@@ -165,11 +166,11 @@
165166
isDeleted = db_column_int(&q, 3);
166167
oldChnged = db_column_int(&q, 4);
167168
oldMtime = db_column_int64(&q, 7);
168169
if( isDeleted ){
169170
chnged = 1;
170
- }else if( !file_isfile(zName) && file_size(0)>=0 ){
171
+ }else if( !file_isfile_or_link(zName) && file_size(0)>=0 ){
171172
if( notFileIsFatal ){
172173
fossil_warning("not an ordinary file: %s", zName);
173174
nErr++;
174175
}
175176
chnged = 1;
@@ -223,29 +224,30 @@
223224
Stmt q;
224225
Blob content;
225226
int nRepos = strlen(g.zLocalRoot);
226227
227228
if( vid>0 && id==0 ){
228
- db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe"
229
+ db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
229230
" FROM vfile"
230231
" WHERE vid=%d AND mrid>0",
231232
g.zLocalRoot, vid);
232233
}else{
233234
assert( vid==0 && id>0 );
234
- db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe"
235
+ db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
235236
" FROM vfile"
236237
" WHERE id=%d AND mrid>0",
237238
g.zLocalRoot, id);
238239
}
239240
while( db_step(&q)==SQLITE_ROW ){
240
- int id, rid, isExe;
241
+ int id, rid, isExe, isLink;
241242
const char *zName;
242243
243244
id = db_column_int(&q, 0);
244245
zName = db_column_text(&q, 1);
245246
rid = db_column_int(&q, 2);
246247
isExe = db_column_int(&q, 3);
248
+ isLink = db_column_int(&q, 4);
247249
content_get(rid, &content);
248250
if( file_is_the_same(&content, zName) ){
249251
blob_reset(&content);
250252
if( file_setexe(zName, isExe) ){
251253
db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
@@ -270,11 +272,22 @@
270272
blob_reset(&content);
271273
continue;
272274
}
273275
}
274276
if( verbose ) fossil_print("%s\n", &zName[nRepos]);
275
- blob_write_to_file(&content, zName);
277
+ if( file_isdir(zName) == 1 ){
278
+ /*TODO(dchest): remove directories? */
279
+ fossil_fatal("%s is directory, cannot overwrite\n", zName);
280
+ }
281
+ if( file_size(zName)>=0 && (isLink || file_islink(zName)) ){
282
+ file_delete(zName);
283
+ }
284
+ if( isLink ){
285
+ create_symlink(blob_str(&content), zName);
286
+ }else{
287
+ blob_write_to_file(&content, zName);
288
+ }
276289
file_setexe(zName, isExe);
277290
blob_reset(&content);
278291
db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
279292
file_mtime(zName), id);
280293
}
@@ -380,11 +393,11 @@
380393
/* do nothing */
381394
}else if( file_isdir(zPath)==1 ){
382395
if( !vfile_top_of_checkout(zPath) ){
383396
vfile_scan(pPath, nPrefix, allFlag, pIgnore);
384397
}
385
- }else if( file_isfile(zPath) ){
398
+ }else if( file_isfile_or_link(zPath) ){
386399
db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
387400
db_step(&ins);
388401
db_reset(&ins);
389402
}
390403
blob_resize(pPath, origSize);
@@ -439,27 +452,38 @@
439452
const char *zName = db_column_text(&q, 1);
440453
int isSelected = db_column_int(&q, 3);
441454
442455
if( isSelected ){
443456
md5sum_step_text(zName, -1);
444
- in = fossil_fopen(zFullpath,"rb");
445
- if( in==0 ){
446
- md5sum_step_text(" 0\n", -1);
447
- continue;
448
- }
449
- fseek(in, 0L, SEEK_END);
450
- sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n", ftell(in));
451
- fseek(in, 0L, SEEK_SET);
452
- md5sum_step_text(zBuf, -1);
453
- /*printf("%s %s %s",md5sum_current_state(),zName,zBuf); fflush(stdout);*/
454
- for(;;){
455
- int n;
456
- n = fread(zBuf, 1, sizeof(zBuf), in);
457
- if( n<=0 ) break;
458
- md5sum_step_text(zBuf, n);
459
- }
460
- fclose(in);
457
+ if( file_islink(zFullpath) ){
458
+ /* Instead of file content, use link destination path */
459
+ Blob pathBuf;
460
+
461
+ sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n",
462
+ blob_read_link(&pathBuf, zFullpath));
463
+ md5sum_step_text(zBuf, -1);
464
+ md5sum_step_text(blob_str(&pathBuf), -1);
465
+ blob_reset(&pathBuf);
466
+ }else{
467
+ in = fossil_fopen(zFullpath,"rb");
468
+ if( in==0 ){
469
+ md5sum_step_text(" 0\n", -1);
470
+ continue;
471
+ }
472
+ fseek(in, 0L, SEEK_END);
473
+ sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n", ftell(in));
474
+ fseek(in, 0L, SEEK_SET);
475
+ md5sum_step_text(zBuf, -1);
476
+ /*printf("%s %s %s",md5sum_current_state(),zName,zBuf); fflush(stdout);*/
477
+ for(;;){
478
+ int n;
479
+ n = fread(zBuf, 1, sizeof(zBuf), in);
480
+ if( n<=0 ) break;
481
+ md5sum_step_text(zBuf, n);
482
+ }
483
+ fclose(in);
484
+ }
461485
}else{
462486
int rid = db_column_int(&q, 4);
463487
const char *zOrigName = db_column_text(&q, 2);
464488
char zBuf[100];
465489
Blob file;
@@ -500,11 +524,15 @@
500524
const char *zFullpath = db_column_text(&q, 0);
501525
const char *zName = db_column_text(&q, 1);
502526
int rid = db_column_int(&q, 2);
503527
504528
blob_zero(&disk);
505
- rc = blob_read_from_file(&disk, zFullpath);
529
+ if( file_islink(zFullpath) ){
530
+ rc = blob_read_link(&disk, zFullpath);
531
+ }else{
532
+ rc = blob_read_from_file(&disk, zFullpath);
533
+ }
506534
if( rc<0 ){
507535
fossil_print("ERROR: cannot read file [%s]\n", zFullpath);
508536
blob_reset(&disk);
509537
continue;
510538
}
511539
--- src/vfile.c
+++ src/vfile.c
@@ -90,12 +90,12 @@
90 db_begin_transaction();
91 p = manifest_get(vid, CFTYPE_MANIFEST);
92 if( p==0 ) return;
93 db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid);
94 db_prepare(&ins,
95 "INSERT INTO vfile(vid,isexe,rid,mrid,pathname) "
96 " VALUES(:vid,:isexe,:id,:id,:name)");
97 db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
98 db_bind_int(&ins, ":vid", vid);
99 manifest_file_rewind(p);
100 while( (pFile = manifest_file_next(p,0))!=0 ){
101 if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
@@ -110,13 +110,14 @@
110 db_reset(&ridq);
111 if( rid==0 || size<0 ){
112 fossil_warning("content missing for %s", pFile->zName);
113 continue;
114 }
115 db_bind_int(&ins, ":isexe", manifest_file_mperm(pFile));
116 db_bind_int(&ins, ":id", rid);
117 db_bind_text(&ins, ":name", pFile->zName);
 
118 db_step(&ins);
119 db_reset(&ins);
120 }
121 db_finalize(&ridq);
122 db_finalize(&ins);
@@ -165,11 +166,11 @@
165 isDeleted = db_column_int(&q, 3);
166 oldChnged = db_column_int(&q, 4);
167 oldMtime = db_column_int64(&q, 7);
168 if( isDeleted ){
169 chnged = 1;
170 }else if( !file_isfile(zName) && file_size(0)>=0 ){
171 if( notFileIsFatal ){
172 fossil_warning("not an ordinary file: %s", zName);
173 nErr++;
174 }
175 chnged = 1;
@@ -223,29 +224,30 @@
223 Stmt q;
224 Blob content;
225 int nRepos = strlen(g.zLocalRoot);
226
227 if( vid>0 && id==0 ){
228 db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe"
229 " FROM vfile"
230 " WHERE vid=%d AND mrid>0",
231 g.zLocalRoot, vid);
232 }else{
233 assert( vid==0 && id>0 );
234 db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe"
235 " FROM vfile"
236 " WHERE id=%d AND mrid>0",
237 g.zLocalRoot, id);
238 }
239 while( db_step(&q)==SQLITE_ROW ){
240 int id, rid, isExe;
241 const char *zName;
242
243 id = db_column_int(&q, 0);
244 zName = db_column_text(&q, 1);
245 rid = db_column_int(&q, 2);
246 isExe = db_column_int(&q, 3);
 
247 content_get(rid, &content);
248 if( file_is_the_same(&content, zName) ){
249 blob_reset(&content);
250 if( file_setexe(zName, isExe) ){
251 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
@@ -270,11 +272,22 @@
270 blob_reset(&content);
271 continue;
272 }
273 }
274 if( verbose ) fossil_print("%s\n", &zName[nRepos]);
275 blob_write_to_file(&content, zName);
 
 
 
 
 
 
 
 
 
 
 
276 file_setexe(zName, isExe);
277 blob_reset(&content);
278 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
279 file_mtime(zName), id);
280 }
@@ -380,11 +393,11 @@
380 /* do nothing */
381 }else if( file_isdir(zPath)==1 ){
382 if( !vfile_top_of_checkout(zPath) ){
383 vfile_scan(pPath, nPrefix, allFlag, pIgnore);
384 }
385 }else if( file_isfile(zPath) ){
386 db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
387 db_step(&ins);
388 db_reset(&ins);
389 }
390 blob_resize(pPath, origSize);
@@ -439,27 +452,38 @@
439 const char *zName = db_column_text(&q, 1);
440 int isSelected = db_column_int(&q, 3);
441
442 if( isSelected ){
443 md5sum_step_text(zName, -1);
444 in = fossil_fopen(zFullpath,"rb");
445 if( in==0 ){
446 md5sum_step_text(" 0\n", -1);
447 continue;
448 }
449 fseek(in, 0L, SEEK_END);
450 sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n", ftell(in));
451 fseek(in, 0L, SEEK_SET);
452 md5sum_step_text(zBuf, -1);
453 /*printf("%s %s %s",md5sum_current_state(),zName,zBuf); fflush(stdout);*/
454 for(;;){
455 int n;
456 n = fread(zBuf, 1, sizeof(zBuf), in);
457 if( n<=0 ) break;
458 md5sum_step_text(zBuf, n);
459 }
460 fclose(in);
 
 
 
 
 
 
 
 
 
 
 
461 }else{
462 int rid = db_column_int(&q, 4);
463 const char *zOrigName = db_column_text(&q, 2);
464 char zBuf[100];
465 Blob file;
@@ -500,11 +524,15 @@
500 const char *zFullpath = db_column_text(&q, 0);
501 const char *zName = db_column_text(&q, 1);
502 int rid = db_column_int(&q, 2);
503
504 blob_zero(&disk);
505 rc = blob_read_from_file(&disk, zFullpath);
 
 
 
 
506 if( rc<0 ){
507 fossil_print("ERROR: cannot read file [%s]\n", zFullpath);
508 blob_reset(&disk);
509 continue;
510 }
511
--- src/vfile.c
+++ src/vfile.c
@@ -90,12 +90,12 @@
90 db_begin_transaction();
91 p = manifest_get(vid, CFTYPE_MANIFEST);
92 if( p==0 ) return;
93 db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid);
94 db_prepare(&ins,
95 "INSERT INTO vfile(vid,isexe,islink,rid,mrid,pathname) "
96 " VALUES(:vid,:isexe,:islink,:id,:id,:name)");
97 db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
98 db_bind_int(&ins, ":vid", vid);
99 manifest_file_rewind(p);
100 while( (pFile = manifest_file_next(p,0))!=0 ){
101 if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
@@ -110,13 +110,14 @@
110 db_reset(&ridq);
111 if( rid==0 || size<0 ){
112 fossil_warning("content missing for %s", pFile->zName);
113 continue;
114 }
115 db_bind_int(&ins, ":isexe", ( manifest_file_mperm(pFile)==PERM_EXE ));
116 db_bind_int(&ins, ":id", rid);
117 db_bind_text(&ins, ":name", pFile->zName);
118 db_bind_int(&ins, ":islink", ( manifest_file_mperm(pFile)==PERM_LNK ));
119 db_step(&ins);
120 db_reset(&ins);
121 }
122 db_finalize(&ridq);
123 db_finalize(&ins);
@@ -165,11 +166,11 @@
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_isfile_or_link(zName) && file_size(0)>=0 ){
172 if( notFileIsFatal ){
173 fossil_warning("not an ordinary file: %s", zName);
174 nErr++;
175 }
176 chnged = 1;
@@ -223,29 +224,30 @@
224 Stmt q;
225 Blob content;
226 int nRepos = strlen(g.zLocalRoot);
227
228 if( vid>0 && id==0 ){
229 db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
230 " FROM vfile"
231 " WHERE vid=%d AND mrid>0",
232 g.zLocalRoot, vid);
233 }else{
234 assert( vid==0 && id>0 );
235 db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
236 " FROM vfile"
237 " WHERE id=%d AND mrid>0",
238 g.zLocalRoot, id);
239 }
240 while( db_step(&q)==SQLITE_ROW ){
241 int id, rid, isExe, isLink;
242 const char *zName;
243
244 id = db_column_int(&q, 0);
245 zName = db_column_text(&q, 1);
246 rid = db_column_int(&q, 2);
247 isExe = db_column_int(&q, 3);
248 isLink = db_column_int(&q, 4);
249 content_get(rid, &content);
250 if( file_is_the_same(&content, zName) ){
251 blob_reset(&content);
252 if( file_setexe(zName, isExe) ){
253 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
@@ -270,11 +272,22 @@
272 blob_reset(&content);
273 continue;
274 }
275 }
276 if( verbose ) fossil_print("%s\n", &zName[nRepos]);
277 if( file_isdir(zName) == 1 ){
278 /*TODO(dchest): remove directories? */
279 fossil_fatal("%s is directory, cannot overwrite\n", zName);
280 }
281 if( file_size(zName)>=0 && (isLink || file_islink(zName)) ){
282 file_delete(zName);
283 }
284 if( isLink ){
285 create_symlink(blob_str(&content), zName);
286 }else{
287 blob_write_to_file(&content, zName);
288 }
289 file_setexe(zName, isExe);
290 blob_reset(&content);
291 db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
292 file_mtime(zName), id);
293 }
@@ -380,11 +393,11 @@
393 /* do nothing */
394 }else if( file_isdir(zPath)==1 ){
395 if( !vfile_top_of_checkout(zPath) ){
396 vfile_scan(pPath, nPrefix, allFlag, pIgnore);
397 }
398 }else if( file_isfile_or_link(zPath) ){
399 db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
400 db_step(&ins);
401 db_reset(&ins);
402 }
403 blob_resize(pPath, origSize);
@@ -439,27 +452,38 @@
452 const char *zName = db_column_text(&q, 1);
453 int isSelected = db_column_int(&q, 3);
454
455 if( isSelected ){
456 md5sum_step_text(zName, -1);
457 if( file_islink(zFullpath) ){
458 /* Instead of file content, use link destination path */
459 Blob pathBuf;
460
461 sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n",
462 blob_read_link(&pathBuf, zFullpath));
463 md5sum_step_text(zBuf, -1);
464 md5sum_step_text(blob_str(&pathBuf), -1);
465 blob_reset(&pathBuf);
466 }else{
467 in = fossil_fopen(zFullpath,"rb");
468 if( in==0 ){
469 md5sum_step_text(" 0\n", -1);
470 continue;
471 }
472 fseek(in, 0L, SEEK_END);
473 sqlite3_snprintf(sizeof(zBuf), zBuf, " %ld\n", ftell(in));
474 fseek(in, 0L, SEEK_SET);
475 md5sum_step_text(zBuf, -1);
476 /*printf("%s %s %s",md5sum_current_state(),zName,zBuf); fflush(stdout);*/
477 for(;;){
478 int n;
479 n = fread(zBuf, 1, sizeof(zBuf), in);
480 if( n<=0 ) break;
481 md5sum_step_text(zBuf, n);
482 }
483 fclose(in);
484 }
485 }else{
486 int rid = db_column_int(&q, 4);
487 const char *zOrigName = db_column_text(&q, 2);
488 char zBuf[100];
489 Blob file;
@@ -500,11 +524,15 @@
524 const char *zFullpath = db_column_text(&q, 0);
525 const char *zName = db_column_text(&q, 1);
526 int rid = db_column_int(&q, 2);
527
528 blob_zero(&disk);
529 if( file_islink(zFullpath) ){
530 rc = blob_read_link(&disk, zFullpath);
531 }else{
532 rc = blob_read_from_file(&disk, zFullpath);
533 }
534 if( rc<0 ){
535 fossil_print("ERROR: cannot read file [%s]\n", zFullpath);
536 blob_reset(&disk);
537 continue;
538 }
539
+7 -3
--- src/zip.c
+++ src/zip.c
@@ -117,11 +117,11 @@
117117
** Append a single file to a growing ZIP archive.
118118
**
119119
** pFile is the file to be appended. zName is the name
120120
** that the file should be saved as.
121121
*/
122
-void zip_add_file(const char *zName, const Blob *pFile, int isExe){
122
+void zip_add_file(const char *zName, const Blob *pFile, int mPerm){
123123
z_stream stream;
124124
int nameLen;
125125
int toOut = 0;
126126
int iStart;
127127
int iCRC = 0;
@@ -139,11 +139,15 @@
139139
/* Fill in as much of the header as we know.
140140
*/
141141
nBlob = pFile ? blob_size(pFile) : 0;
142142
if( nBlob>0 ){
143143
iMethod = 8;
144
- iMode = isExe ? 0100755 : 0100644;
144
+ switch( mPerm ){
145
+ case PERM_LNK: iMode = 0120755; break;
146
+ case PERM_EXE: iMode = 0100755; break;
147
+ default: iMode = 0100644; break;
148
+ }
145149
}else{
146150
iMethod = 0;
147151
iMode = 040755;
148152
}
149153
nameLen = strlen(zName);
@@ -287,11 +291,11 @@
287291
}
288292
zip_open();
289293
for(i=3; i<g.argc; i++){
290294
blob_zero(&file);
291295
blob_read_from_file(&file, g.argv[i]);
292
- zip_add_file(g.argv[i], &file, file_isexe(g.argv[i]));
296
+ zip_add_file(g.argv[i], &file, file_perm(g.argv[i]));
293297
blob_reset(&file);
294298
}
295299
zip_close(&zip);
296300
blob_write_to_file(&zip, g.argv[2]);
297301
}
298302
299303
ADDED test/merge_renames.test
--- src/zip.c
+++ src/zip.c
@@ -117,11 +117,11 @@
117 ** Append a single file to a growing ZIP archive.
118 **
119 ** pFile is the file to be appended. zName is the name
120 ** that the file should be saved as.
121 */
122 void zip_add_file(const char *zName, const Blob *pFile, int isExe){
123 z_stream stream;
124 int nameLen;
125 int toOut = 0;
126 int iStart;
127 int iCRC = 0;
@@ -139,11 +139,15 @@
139 /* Fill in as much of the header as we know.
140 */
141 nBlob = pFile ? blob_size(pFile) : 0;
142 if( nBlob>0 ){
143 iMethod = 8;
144 iMode = isExe ? 0100755 : 0100644;
 
 
 
 
145 }else{
146 iMethod = 0;
147 iMode = 040755;
148 }
149 nameLen = strlen(zName);
@@ -287,11 +291,11 @@
287 }
288 zip_open();
289 for(i=3; i<g.argc; i++){
290 blob_zero(&file);
291 blob_read_from_file(&file, g.argv[i]);
292 zip_add_file(g.argv[i], &file, file_isexe(g.argv[i]));
293 blob_reset(&file);
294 }
295 zip_close(&zip);
296 blob_write_to_file(&zip, g.argv[2]);
297 }
298
299 DDED test/merge_renames.test
--- src/zip.c
+++ src/zip.c
@@ -117,11 +117,11 @@
117 ** Append a single file to a growing ZIP archive.
118 **
119 ** pFile is the file to be appended. zName is the name
120 ** that the file should be saved as.
121 */
122 void zip_add_file(const char *zName, const Blob *pFile, int mPerm){
123 z_stream stream;
124 int nameLen;
125 int toOut = 0;
126 int iStart;
127 int iCRC = 0;
@@ -139,11 +139,15 @@
139 /* Fill in as much of the header as we know.
140 */
141 nBlob = pFile ? blob_size(pFile) : 0;
142 if( nBlob>0 ){
143 iMethod = 8;
144 switch( mPerm ){
145 case PERM_LNK: iMode = 0120755; break;
146 case PERM_EXE: iMode = 0100755; break;
147 default: iMode = 0100644; break;
148 }
149 }else{
150 iMethod = 0;
151 iMode = 040755;
152 }
153 nameLen = strlen(zName);
@@ -287,11 +291,11 @@
291 }
292 zip_open();
293 for(i=3; i<g.argc; i++){
294 blob_zero(&file);
295 blob_read_from_file(&file, g.argv[i]);
296 zip_add_file(g.argv[i], &file, file_perm(g.argv[i]));
297 blob_reset(&file);
298 }
299 zip_close(&zip);
300 blob_write_to_file(&zip, g.argv[2]);
301 }
302
303 DDED test/merge_renames.test
--- a/test/merge_renames.test
+++ b/test/merge_renames.test
@@ -0,0 +1,76 @@
1
+exec mv f1::fossilexe info} r #
2
+# Tests 2 "catch {exec $::fossilexe info} resnot within an open checkoutf {![regexp {use --repository} $res]} {
3
+ puts stderr "Cannot run this test within an open checkout"
4
+ return
5
+} failed
6
+ protOut "Error, the merf {$deletes!=0} {
7
+ # failed
8
+ protOut repo_iniTests 2 "f1 f2
9
+filefossil new rep.fo
10
+ protOut "Error, the merge should 1 0
11
+} else {
12
+ te1 1
13
+#
14
+# Texec rmiled
15
+ protOut "Error, the merg2RESULT
16
+
17
+# Not a nice waySULT
18
+
19
+set deletes 0e} $RESULT {
20
+ if {$status=="DELETE"} {
21
+ set deletes [expr $deletes + 1]
22
+ }
23
+}
24
+
25
+if {$deletes!=0} {
26
+ # failed
27
+ protOut "Error, the merge should 1 0
28
+} else {
29
+ te1 1
30
+#
31
+# Testputs $RESULT
32
+
33
+# Not a nice way to check, but I don't know more tcl now
34
+exec mvfile rename -force f1 f2 knownBugrepo_iniTests 2 "f1 f2
35
+fil2rotOut "Error, the merge should 3 02# TO BE WRITTEN. Test 5f14 knownBugrepo_iniTests 267176c3aa4or, the merge should SULT
36
+
37
+set deletes 0
38
+foreach {status #
39
+# Tests 2 "catch {exec $::fossilexe info} res
40
+if {![regexp {use --repositputs res=$ository} $res]} {
41
+ puts stderr "Cannot run this test within an open checkout"
42
+ returnes 0
43
+foreach {status fil1ame -force f1 f2 ULT {
44
+
45
+e merge should SULT
46
+
47
+set deletes 0e} $RESULT {
48
+ if {$status=="DELETE"} {
49
+ set deletes [expr $deletes + 1]
50
+ }
51
+}
52
+
53
+if {$deletes!=0}exec rmiled
54
+ protOut "Error, the merg$RESULT
55
+
56
+# Not a nice way to check, but I don't know more tcl now
57
+set deletes 0
58
+foreach {status filename} $RESULT {
59
+ if {$status=="DELETE"} {
60
+ set deletes [expr $deletes + 1]
61
+ }
62
+}
63
+
64
+if {$deletes!=0} {
65
+ # failed
66
+ protOut "Error, the merge should 3 0
67
+} else {
68
+ te3 1
69
+#
70
+# Test# TO BE WRITTEN. Test 5f1 f2
71
+file renaexec mvfile rename -force f1 f2 knownBugrepo_iniTests 2 "f1 f2
72
+fil2rotOut "Error, the merge should 3 02# TO BE WRITTEN. Test 5f14 knownBugrepo_iniTests 267176c3aa4or, the merge should SULT
73
+
74
+set deletes 0
75
+foreach {status #
76
+# Tests 2 "catch {exec $::fossilexe info} res
--- a/test/merge_renames.test
+++ b/test/merge_renames.test
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
--- a/test/merge_renames.test
+++ b/test/merge_renames.test
@@ -0,0 +1,76 @@
1 exec mv f1::fossilexe info} r #
2 # Tests 2 "catch {exec $::fossilexe info} resnot within an open checkoutf {![regexp {use --repository} $res]} {
3 puts stderr "Cannot run this test within an open checkout"
4 return
5 } failed
6 protOut "Error, the merf {$deletes!=0} {
7 # failed
8 protOut repo_iniTests 2 "f1 f2
9 filefossil new rep.fo
10 protOut "Error, the merge should 1 0
11 } else {
12 te1 1
13 #
14 # Texec rmiled
15 protOut "Error, the merg2RESULT
16
17 # Not a nice waySULT
18
19 set deletes 0e} $RESULT {
20 if {$status=="DELETE"} {
21 set deletes [expr $deletes + 1]
22 }
23 }
24
25 if {$deletes!=0} {
26 # failed
27 protOut "Error, the merge should 1 0
28 } else {
29 te1 1
30 #
31 # Testputs $RESULT
32
33 # Not a nice way to check, but I don't know more tcl now
34 exec mvfile rename -force f1 f2 knownBugrepo_iniTests 2 "f1 f2
35 fil2rotOut "Error, the merge should 3 02# TO BE WRITTEN. Test 5f14 knownBugrepo_iniTests 267176c3aa4or, the merge should SULT
36
37 set deletes 0
38 foreach {status #
39 # Tests 2 "catch {exec $::fossilexe info} res
40 if {![regexp {use --repositputs res=$ository} $res]} {
41 puts stderr "Cannot run this test within an open checkout"
42 returnes 0
43 foreach {status fil1ame -force f1 f2 ULT {
44
45 e merge should SULT
46
47 set deletes 0e} $RESULT {
48 if {$status=="DELETE"} {
49 set deletes [expr $deletes + 1]
50 }
51 }
52
53 if {$deletes!=0}exec rmiled
54 protOut "Error, the merg$RESULT
55
56 # Not a nice way to check, but I don't know more tcl now
57 set deletes 0
58 foreach {status filename} $RESULT {
59 if {$status=="DELETE"} {
60 set deletes [expr $deletes + 1]
61 }
62 }
63
64 if {$deletes!=0} {
65 # failed
66 protOut "Error, the merge should 3 0
67 } else {
68 te3 1
69 #
70 # Test# TO BE WRITTEN. Test 5f1 f2
71 file renaexec mvfile rename -force f1 f2 knownBugrepo_iniTests 2 "f1 f2
72 fil2rotOut "Error, the merge should 3 02# TO BE WRITTEN. Test 5f14 knownBugrepo_iniTests 267176c3aa4or, the merge should SULT
73
74 set deletes 0
75 foreach {status #
76 # Tests 2 "catch {exec $::fossilexe info} res
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -13,10 +13,16 @@
1313
<li><p>
1414
Click on each of the links in in the
1515
[./graph-test-1.wiki] document and verify that all graphs are
1616
rendered correctly.
1717
18
+<li><p>
19
+Verify correct name-change tracking behavior (no net changes) for:
20
+<blockquote><b>
21
+fossil test-name-changes --debug b120bc8b262ac 374920b20944b
22
+</b></blockquote>
23
+
1824
<li><p>
1925
Compile for all of the following platforms:
2026
<ol type="a">
2127
<li> Linux x86
2228
<li> Linux x86_64
2329
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -13,10 +13,16 @@
13 <li><p>
14 Click on each of the links in in the
15 [./graph-test-1.wiki] document and verify that all graphs are
16 rendered correctly.
17
 
 
 
 
 
 
18 <li><p>
19 Compile for all of the following platforms:
20 <ol type="a">
21 <li> Linux x86
22 <li> Linux x86_64
23
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -13,10 +13,16 @@
13 <li><p>
14 Click on each of the links in in the
15 [./graph-test-1.wiki] document and verify that all graphs are
16 rendered correctly.
17
18 <li><p>
19 Verify correct name-change tracking behavior (no net changes) for:
20 <blockquote><b>
21 fossil test-name-changes --debug b120bc8b262ac 374920b20944b
22 </b></blockquote>
23
24 <li><p>
25 Compile for all of the following platforms:
26 <ol type="a">
27 <li> Linux x86
28 <li> Linux x86_64
29
+5 -7
--- www/build.wiki
+++ www/build.wiki
@@ -28,23 +28,22 @@
2828
<p>Fossil is self-hosting, so you can obtain a ZIP archive containing
2929
a snapshot of the latest version directly from fossil's own fossil
3030
repository. Follow these steps:</p>
3131
3232
<ol>
33
-<li><p>Pointer your web browser at
33
+<li><p>Point your web browser at
3434
<a href="http://www.fossil-scm.org/">
3535
http://www.fossil-scm.org/</a>. Click on the "Login" menu button.</p></li>
3636
3737
<li><p>Log in as anonymous. The password is shown on screen.
3838
The reason for requiring this login is to prevent spiders from
3939
walking the entire website, downloading ZIP archives
4040
of every historical version, and thereby soaking up all our bandwidth.</p></li>
4141
4242
<li><p>Click on the
43
-<a href="http://www.fossil-scm.org/fossil/timeline">Timeline</a> or
44
-<a href="http://www.fossil-scm.org/fossil/leaves">Leaves</a> link at
45
-the top of the page.</p></li>
43
+<a href="http://www.fossil-scm.org/fossil/timeline">Timeline</a>
44
+link at the top of the page.</p></li>
4645
4746
<li><p>Select a version of of fossil you want to download. Click on its
4847
link. Note that you must successfully log in as "anonymous" in step 1
4948
above in order to see the link to the detailed version information.</p></li>
5049
@@ -56,13 +55,12 @@
5655
5756
<h2>2.0 Compiling</h2>
5857
5958
<ol>
6059
<li value="6">
61
-<p>Create a directory to hold the source code. Then unarchive the
62
-ZIP or tarball you downloaded into that directory. You should be
63
-in the top-level folder of that directory</p></li>
60
+<p>Unpack the ZIP or tarball you downloaded into that directory then
61
+<b>cd</b> into the directory created.</p></li>
6462
6563
<li><i>(Optional, unix only)</i>
6664
Run <b>./configure</b> to construct a makefile.
6765
6866
<ol type="a">
6967
--- www/build.wiki
+++ www/build.wiki
@@ -28,23 +28,22 @@
28 <p>Fossil is self-hosting, so you can obtain a ZIP archive containing
29 a snapshot of the latest version directly from fossil's own fossil
30 repository. Follow these steps:</p>
31
32 <ol>
33 <li><p>Pointer your web browser at
34 <a href="http://www.fossil-scm.org/">
35 http://www.fossil-scm.org/</a>. Click on the "Login" menu button.</p></li>
36
37 <li><p>Log in as anonymous. The password is shown on screen.
38 The reason for requiring this login is to prevent spiders from
39 walking the entire website, downloading ZIP archives
40 of every historical version, and thereby soaking up all our bandwidth.</p></li>
41
42 <li><p>Click on the
43 <a href="http://www.fossil-scm.org/fossil/timeline">Timeline</a> or
44 <a href="http://www.fossil-scm.org/fossil/leaves">Leaves</a> link at
45 the top of the page.</p></li>
46
47 <li><p>Select a version of of fossil you want to download. Click on its
48 link. Note that you must successfully log in as "anonymous" in step 1
49 above in order to see the link to the detailed version information.</p></li>
50
@@ -56,13 +55,12 @@
56
57 <h2>2.0 Compiling</h2>
58
59 <ol>
60 <li value="6">
61 <p>Create a directory to hold the source code. Then unarchive the
62 ZIP or tarball you downloaded into that directory. You should be
63 in the top-level folder of that directory</p></li>
64
65 <li><i>(Optional, unix only)</i>
66 Run <b>./configure</b> to construct a makefile.
67
68 <ol type="a">
69
--- www/build.wiki
+++ www/build.wiki
@@ -28,23 +28,22 @@
28 <p>Fossil is self-hosting, so you can obtain a ZIP archive containing
29 a snapshot of the latest version directly from fossil's own fossil
30 repository. Follow these steps:</p>
31
32 <ol>
33 <li><p>Point your web browser at
34 <a href="http://www.fossil-scm.org/">
35 http://www.fossil-scm.org/</a>. Click on the "Login" menu button.</p></li>
36
37 <li><p>Log in as anonymous. The password is shown on screen.
38 The reason for requiring this login is to prevent spiders from
39 walking the entire website, downloading ZIP archives
40 of every historical version, and thereby soaking up all our bandwidth.</p></li>
41
42 <li><p>Click on the
43 <a href="http://www.fossil-scm.org/fossil/timeline">Timeline</a>
44 link at the top of the page.</p></li>
 
45
46 <li><p>Select a version of of fossil you want to download. Click on its
47 link. Note that you must successfully log in as "anonymous" in step 1
48 above in order to see the link to the detailed version information.</p></li>
49
@@ -56,13 +55,12 @@
55
56 <h2>2.0 Compiling</h2>
57
58 <ol>
59 <li value="6">
60 <p>Unpack the ZIP or tarball you downloaded into that directory then
61 <b>cd</b> into the directory created.</p></li>
 
62
63 <li><i>(Optional, unix only)</i>
64 Run <b>./configure</b> to construct a makefile.
65
66 <ol type="a">
67
+10 -1
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,15 +1,17 @@
11
<title>Change Log</title>
22
3
-<h2>Changes For Version 1.19 (pending)</h2>
3
+<h2>Changes For Version 1.19 (2011-09-02)</h2>
44
55
* Added a ./configure script based on autosetup.
66
* Added the "[/help/winsrv | fossil winsrv]" command
77
for creating a Fossil service on windows systems.
88
* Added "versionable settings" where settings that affect
99
the local tree can be stored in versioned files in the
1010
.fossil-settings directory.
11
+ * Background colors for branches are choosen automatically if no
12
+ color is specified by the user.
1113
* The status, changes and extras commands now show
1214
pathnames relative to the current working directory,
1315
unless overridden by command line options or the
1416
"relative-paths" setting.<br><b>WARNING:</b> This
1517
change will break scripts which rely on the current
@@ -18,10 +20,17 @@
1820
* Added "empty-dirs" versionable setting.
1921
* Added support for client-side SSL certificates with "ssl-identity"
2022
setting and --ssl-identity option.
2123
* Added "ssl-ca-location" setting to specify trusted root
2224
SSL certificates.
25
+ * Added the --case-sensitive BOOLEAN command-line option to many commands.
26
+ Default to true for unix and false for windows.
27
+ * Added the "Color-Test" submenu button on the branch list web page.
28
+ * Compatibility improvements to the git-export feature.
29
+ * Performance improvements on SHA1 checksums
30
+ * Update to the latest SQLite version 3.7.8 alpha.
31
+ * Fix the tarball generator to work with very log pathnames
2332
2433
<h2>Changes For Version 1.18 (2011-07-14)</h2>
2534
2635
* Added this Change Log
2736
* Added sequential version numbering
2837
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,15 +1,17 @@
1 <title>Change Log</title>
2
3 <h2>Changes For Version 1.19 (pending)</h2>
4
5 * Added a ./configure script based on autosetup.
6 * Added the "[/help/winsrv | fossil winsrv]" command
7 for creating a Fossil service on windows systems.
8 * Added "versionable settings" where settings that affect
9 the local tree can be stored in versioned files in the
10 .fossil-settings directory.
 
 
11 * The status, changes and extras commands now show
12 pathnames relative to the current working directory,
13 unless overridden by command line options or the
14 "relative-paths" setting.<br><b>WARNING:</b> This
15 change will break scripts which rely on the current
@@ -18,10 +20,17 @@
18 * Added "empty-dirs" versionable setting.
19 * Added support for client-side SSL certificates with "ssl-identity"
20 setting and --ssl-identity option.
21 * Added "ssl-ca-location" setting to specify trusted root
22 SSL certificates.
 
 
 
 
 
 
 
23
24 <h2>Changes For Version 1.18 (2011-07-14)</h2>
25
26 * Added this Change Log
27 * Added sequential version numbering
28
--- www/changes.wiki
+++ www/changes.wiki
@@ -1,15 +1,17 @@
1 <title>Change Log</title>
2
3 <h2>Changes For Version 1.19 (2011-09-02)</h2>
4
5 * Added a ./configure script based on autosetup.
6 * Added the "[/help/winsrv | fossil winsrv]" command
7 for creating a Fossil service on windows systems.
8 * Added "versionable settings" where settings that affect
9 the local tree can be stored in versioned files in the
10 .fossil-settings directory.
11 * Background colors for branches are choosen automatically if no
12 color is specified by the user.
13 * The status, changes and extras commands now show
14 pathnames relative to the current working directory,
15 unless overridden by command line options or the
16 "relative-paths" setting.<br><b>WARNING:</b> This
17 change will break scripts which rely on the current
@@ -18,10 +20,17 @@
20 * Added "empty-dirs" versionable setting.
21 * Added support for client-side SSL certificates with "ssl-identity"
22 setting and --ssl-identity option.
23 * Added "ssl-ca-location" setting to specify trusted root
24 SSL certificates.
25 * Added the --case-sensitive BOOLEAN command-line option to many commands.
26 Default to true for unix and false for windows.
27 * Added the "Color-Test" submenu button on the branch list web page.
28 * Compatibility improvements to the git-export feature.
29 * Performance improvements on SHA1 checksums
30 * Update to the latest SQLite version 3.7.8 alpha.
31 * Fix the tarball generator to work with very log pathnames
32
33 <h2>Changes For Version 1.18 (2011-07-14)</h2>
34
35 * Added this Change Log
36 * Added sequential version numbering
37
--- www/mkdownload.tcl
+++ www/mkdownload.tcl
@@ -60,11 +60,10 @@
6060
puts $out "<center><b><a href=\"$hr\">$dt</a></b></center>"
6161
puts $out "</td></tr>"
6262
6363
foreach {prefix suffix img desc} {
6464
fossil-linux-x86 zip linux.gif {Linux x86}
65
- fossil-linux-amd64 zip linux64.gif {Linux x86_64}
6665
fossil-macosx-x86 zip mac.gif {Mac 10.5 x86}
6766
fossil-openbsd-x86 zip openbsd.gif {OpenBSD 4.7 x86}
6867
fossil-w32 zip win32.gif {Windows}
6968
fossil-src tar.gz src.gif {Source Tarball}
7069
} {
7170
--- www/mkdownload.tcl
+++ www/mkdownload.tcl
@@ -60,11 +60,10 @@
60 puts $out "<center><b><a href=\"$hr\">$dt</a></b></center>"
61 puts $out "</td></tr>"
62
63 foreach {prefix suffix img desc} {
64 fossil-linux-x86 zip linux.gif {Linux x86}
65 fossil-linux-amd64 zip linux64.gif {Linux x86_64}
66 fossil-macosx-x86 zip mac.gif {Mac 10.5 x86}
67 fossil-openbsd-x86 zip openbsd.gif {OpenBSD 4.7 x86}
68 fossil-w32 zip win32.gif {Windows}
69 fossil-src tar.gz src.gif {Source Tarball}
70 } {
71
--- www/mkdownload.tcl
+++ www/mkdownload.tcl
@@ -60,11 +60,10 @@
60 puts $out "<center><b><a href=\"$hr\">$dt</a></b></center>"
61 puts $out "</td></tr>"
62
63 foreach {prefix suffix img desc} {
64 fossil-linux-x86 zip linux.gif {Linux x86}
 
65 fossil-macosx-x86 zip mac.gif {Mac 10.5 x86}
66 fossil-openbsd-x86 zip openbsd.gif {OpenBSD 4.7 x86}
67 fossil-w32 zip win32.gif {Windows}
68 fossil-src tar.gz src.gif {Source Tarball}
69 } {
70

Keyboard Shortcuts

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