Fossil SCM

Merge updates from trunk.

mistachkin 2016-03-06 06:28 altBaseUrlRepoDir merge
Commit 45ed23d94624b44abacb66b4a88abec3721dd250
+7 -5
--- auto.def
+++ auto.def
@@ -37,10 +37,11 @@
3737
3838
define EXTRA_CFLAGS ""
3939
define EXTRA_LDFLAGS ""
4040
define USE_SYSTEM_SQLITE 0
4141
define USE_LINENOISE 0
42
+define FOSSIL_ENABLE_MINIZ 0
4243
4344
# This procedure is a customized version of "cc-check-function-in-lib",
4445
# that does not modify the LIBS variable. Its use prevents prematurely
4546
# pulling in libraries that will be added later anyhow (e.g. "-ldl").
4647
proc check-function-in-lib {function libs {otherlibs {}}} {
@@ -85,17 +86,17 @@
8586
# the code below will append -ldl to LIBS.
8687
#
8788
foreach extralibs {{} {-ldl}} {
8889
8990
# Locate the system SQLite by searching for sqlite3_open(). Then check
90
- # if sqlite3_strglob() can be found as well. If we can find open() but
91
- # not strglob(), then the system SQLite is too old to link against
91
+ # if sqlite3_strlike() can be found as well. If we can find open() but
92
+ # not strlike(), then the system SQLite is too old to link against
9293
# fossil.
9394
#
9495
if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
95
- if {![check-function-in-lib sqlite3_malloc64 sqlite3 $extralibs]} {
96
- user-error "system sqlite3 too old (require >= 3.8.7)"
96
+ if {![check-function-in-lib sqlite3_strlike sqlite3 $extralibs]} {
97
+ user-error "system sqlite3 too old (require >= 3.10.0)"
9798
}
9899
99100
# Success. Update symbols and return.
100101
#
101102
define USE_SYSTEM_SQLITE 1
@@ -269,11 +270,12 @@
269270
set cflags [exec pkg-config openssl --cflags-only-I]
270271
set ldflags [exec pkg-config openssl --libs-only-L]
271272
set found [check-for-openssl "ssl via pkg-config" "$cflags $ldflags"]
272273
} msg
273274
if {!$found} {
274
- set ssldirs "{} /usr/sfw /usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
275
+ set ssldirs "{} /usr/sfw /usr/local/ssl /usr/lib/ssl /usr/ssl \
276
+ /usr/pkg /usr/local /usr /usr/local/opt/openssl"
275277
}
276278
}
277279
if {!$found} {
278280
foreach dir $ssldirs {
279281
if {$dir eq ""} {
280282
--- auto.def
+++ auto.def
@@ -37,10 +37,11 @@
37
38 define EXTRA_CFLAGS ""
39 define EXTRA_LDFLAGS ""
40 define USE_SYSTEM_SQLITE 0
41 define USE_LINENOISE 0
 
42
43 # This procedure is a customized version of "cc-check-function-in-lib",
44 # that does not modify the LIBS variable. Its use prevents prematurely
45 # pulling in libraries that will be added later anyhow (e.g. "-ldl").
46 proc check-function-in-lib {function libs {otherlibs {}}} {
@@ -85,17 +86,17 @@
85 # the code below will append -ldl to LIBS.
86 #
87 foreach extralibs {{} {-ldl}} {
88
89 # Locate the system SQLite by searching for sqlite3_open(). Then check
90 # if sqlite3_strglob() can be found as well. If we can find open() but
91 # not strglob(), then the system SQLite is too old to link against
92 # fossil.
93 #
94 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
95 if {![check-function-in-lib sqlite3_malloc64 sqlite3 $extralibs]} {
96 user-error "system sqlite3 too old (require >= 3.8.7)"
97 }
98
99 # Success. Update symbols and return.
100 #
101 define USE_SYSTEM_SQLITE 1
@@ -269,11 +270,12 @@
269 set cflags [exec pkg-config openssl --cflags-only-I]
270 set ldflags [exec pkg-config openssl --libs-only-L]
271 set found [check-for-openssl "ssl via pkg-config" "$cflags $ldflags"]
272 } msg
273 if {!$found} {
274 set ssldirs "{} /usr/sfw /usr/local/ssl /usr/lib/ssl /usr/ssl /usr/pkg /usr/local /usr"
 
275 }
276 }
277 if {!$found} {
278 foreach dir $ssldirs {
279 if {$dir eq ""} {
280
--- auto.def
+++ auto.def
@@ -37,10 +37,11 @@
37
38 define EXTRA_CFLAGS ""
39 define EXTRA_LDFLAGS ""
40 define USE_SYSTEM_SQLITE 0
41 define USE_LINENOISE 0
42 define FOSSIL_ENABLE_MINIZ 0
43
44 # This procedure is a customized version of "cc-check-function-in-lib",
45 # that does not modify the LIBS variable. Its use prevents prematurely
46 # pulling in libraries that will be added later anyhow (e.g. "-ldl").
47 proc check-function-in-lib {function libs {otherlibs {}}} {
@@ -85,17 +86,17 @@
86 # the code below will append -ldl to LIBS.
87 #
88 foreach extralibs {{} {-ldl}} {
89
90 # Locate the system SQLite by searching for sqlite3_open(). Then check
91 # if sqlite3_strlike() can be found as well. If we can find open() but
92 # not strlike(), then the system SQLite is too old to link against
93 # fossil.
94 #
95 if {[check-function-in-lib sqlite3_open sqlite3 $extralibs]} {
96 if {![check-function-in-lib sqlite3_strlike sqlite3 $extralibs]} {
97 user-error "system sqlite3 too old (require >= 3.10.0)"
98 }
99
100 # Success. Update symbols and return.
101 #
102 define USE_SYSTEM_SQLITE 1
@@ -269,11 +270,12 @@
270 set cflags [exec pkg-config openssl --cflags-only-I]
271 set ldflags [exec pkg-config openssl --libs-only-L]
272 set found [check-for-openssl "ssl via pkg-config" "$cflags $ldflags"]
273 } msg
274 if {!$found} {
275 set ssldirs "{} /usr/sfw /usr/local/ssl /usr/lib/ssl /usr/ssl \
276 /usr/pkg /usr/local /usr /usr/local/opt/openssl"
277 }
278 }
279 if {!$found} {
280 foreach dir $ssldirs {
281 if {$dir eq ""} {
282
+2 -2
--- src/allrepo.c
+++ src/allrepo.c
@@ -185,11 +185,11 @@
185185
186186
if( g.argc<3 ){
187187
usage("SUBCOMMAND ...");
188188
}
189189
n = strlen(g.argv[2]);
190
- db_open_config(1);
190
+ db_open_config(1, 0);
191191
blob_zero(&extra);
192192
zCmd = g.argv[2];
193193
if( !login_is_nobody() ) blob_appendf(&extra, " -U %s", g.zLogin);
194194
if( strncmp(zCmd, "list", n)==0 || strncmp(zCmd,"ls",n)==0 ){
195195
zCmd = "list";
@@ -292,11 +292,11 @@
292292
Blob sql = BLOB_INITIALIZER;
293293
useCheckouts = find_option("ckout","c",0)!=0;
294294
verify_all_options();
295295
db_begin_transaction();
296296
for(j=3; j<g.argc; j++, blob_reset(&sql), blob_reset(&fn)){
297
- file_canonical_name(g.argv[j], &fn, 0);
297
+ file_canonical_name(g.argv[j], &fn, useCheckouts?1:0);
298298
blob_append_sql(&sql,
299299
"DELETE FROM global_config WHERE name GLOB '%s:%q'",
300300
useCheckouts?"ckout":"repo", blob_str(&fn)
301301
);
302302
if( dryRunFlag ){
303303
--- src/allrepo.c
+++ src/allrepo.c
@@ -185,11 +185,11 @@
185
186 if( g.argc<3 ){
187 usage("SUBCOMMAND ...");
188 }
189 n = strlen(g.argv[2]);
190 db_open_config(1);
191 blob_zero(&extra);
192 zCmd = g.argv[2];
193 if( !login_is_nobody() ) blob_appendf(&extra, " -U %s", g.zLogin);
194 if( strncmp(zCmd, "list", n)==0 || strncmp(zCmd,"ls",n)==0 ){
195 zCmd = "list";
@@ -292,11 +292,11 @@
292 Blob sql = BLOB_INITIALIZER;
293 useCheckouts = find_option("ckout","c",0)!=0;
294 verify_all_options();
295 db_begin_transaction();
296 for(j=3; j<g.argc; j++, blob_reset(&sql), blob_reset(&fn)){
297 file_canonical_name(g.argv[j], &fn, 0);
298 blob_append_sql(&sql,
299 "DELETE FROM global_config WHERE name GLOB '%s:%q'",
300 useCheckouts?"ckout":"repo", blob_str(&fn)
301 );
302 if( dryRunFlag ){
303
--- src/allrepo.c
+++ src/allrepo.c
@@ -185,11 +185,11 @@
185
186 if( g.argc<3 ){
187 usage("SUBCOMMAND ...");
188 }
189 n = strlen(g.argv[2]);
190 db_open_config(1, 0);
191 blob_zero(&extra);
192 zCmd = g.argv[2];
193 if( !login_is_nobody() ) blob_appendf(&extra, " -U %s", g.zLogin);
194 if( strncmp(zCmd, "list", n)==0 || strncmp(zCmd,"ls",n)==0 ){
195 zCmd = "list";
@@ -292,11 +292,11 @@
292 Blob sql = BLOB_INITIALIZER;
293 useCheckouts = find_option("ckout","c",0)!=0;
294 verify_all_options();
295 db_begin_transaction();
296 for(j=3; j<g.argc; j++, blob_reset(&sql), blob_reset(&fn)){
297 file_canonical_name(g.argv[j], &fn, useCheckouts?1:0);
298 blob_append_sql(&sql,
299 "DELETE FROM global_config WHERE name GLOB '%s:%q'",
300 useCheckouts?"ckout":"repo", blob_str(&fn)
301 );
302 if( dryRunFlag ){
303
--- src/attach.c
+++ src/attach.c
@@ -615,10 +615,12 @@
615615
@ <pre>
616616
@ %h(z)
617617
@ </pre>
618618
}
619619
}else if( strncmp(zMime, "image/", 6)==0 ){
620
+ int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
621
+ @ <i>(file is %d(sz) bytes of image data)</i><br>
620622
@ <img src="%R/raw/%s(zSrc)?m=%s(zMime)"></img>
621623
style_submenu_element("Image", "Image", "%R/raw/%s?m=%s", zSrc, zMime);
622624
}else{
623625
int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
624626
@ <i>(file is %d(sz) bytes of binary data)</i>
625627
--- src/attach.c
+++ src/attach.c
@@ -615,10 +615,12 @@
615 @ <pre>
616 @ %h(z)
617 @ </pre>
618 }
619 }else if( strncmp(zMime, "image/", 6)==0 ){
 
 
620 @ <img src="%R/raw/%s(zSrc)?m=%s(zMime)"></img>
621 style_submenu_element("Image", "Image", "%R/raw/%s?m=%s", zSrc, zMime);
622 }else{
623 int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
624 @ <i>(file is %d(sz) bytes of binary data)</i>
625
--- src/attach.c
+++ src/attach.c
@@ -615,10 +615,12 @@
615 @ <pre>
616 @ %h(z)
617 @ </pre>
618 }
619 }else if( strncmp(zMime, "image/", 6)==0 ){
620 int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
621 @ <i>(file is %d(sz) bytes of image data)</i><br>
622 @ <img src="%R/raw/%s(zSrc)?m=%s(zMime)"></img>
623 style_submenu_element("Image", "Image", "%R/raw/%s?m=%s", zSrc, zMime);
624 }else{
625 int sz = db_int(0, "SELECT size FROM blob WHERE rid=%d", ridSrc);
626 @ <i>(file is %d(sz) bytes of binary data)</i>
627
+1
--- src/cgi.c
+++ src/cgi.c
@@ -796,10 +796,11 @@
796796
cson_value * jv = NULL;
797797
int rc;
798798
CgiPostReadState state;
799799
cson_parse_opt popt = cson_parse_opt_empty;
800800
cson_parse_info pinfo = cson_parse_info_empty;
801
+ assert(g.json.gc.a && "json_main_bootstrap() was not called!");
801802
popt.maxDepth = 15;
802803
state.fh = zIn;
803804
state.len = contentLen;
804805
state.pos = 0;
805806
rc = cson_parse( &jv,
806807
--- src/cgi.c
+++ src/cgi.c
@@ -796,10 +796,11 @@
796 cson_value * jv = NULL;
797 int rc;
798 CgiPostReadState state;
799 cson_parse_opt popt = cson_parse_opt_empty;
800 cson_parse_info pinfo = cson_parse_info_empty;
 
801 popt.maxDepth = 15;
802 state.fh = zIn;
803 state.len = contentLen;
804 state.pos = 0;
805 rc = cson_parse( &jv,
806
--- src/cgi.c
+++ src/cgi.c
@@ -796,10 +796,11 @@
796 cson_value * jv = NULL;
797 int rc;
798 CgiPostReadState state;
799 cson_parse_opt popt = cson_parse_opt_empty;
800 cson_parse_info pinfo = cson_parse_info_empty;
801 assert(g.json.gc.a && "json_main_bootstrap() was not called!");
802 popt.maxDepth = 15;
803 state.fh = zIn;
804 state.len = contentLen;
805 state.pos = 0;
806 rc = cson_parse( &jv,
807
+1 -1
--- src/checkout.c
+++ src/checkout.c
@@ -243,11 +243,11 @@
243243
if( !keepFlag ){
244244
uncheckout(prior);
245245
}
246246
db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
247247
if( !keepFlag ){
248
- vfile_to_disk(vid, 0, 1, promptFlag);
248
+ vfile_to_disk(vid, 0, !g.fQuiet, promptFlag);
249249
}
250250
checkout_set_all_exe(vid);
251251
manifest_to_disk(vid);
252252
ensure_empty_dirs_created();
253253
db_lset_int("checkout", vid);
254254
--- src/checkout.c
+++ src/checkout.c
@@ -243,11 +243,11 @@
243 if( !keepFlag ){
244 uncheckout(prior);
245 }
246 db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
247 if( !keepFlag ){
248 vfile_to_disk(vid, 0, 1, promptFlag);
249 }
250 checkout_set_all_exe(vid);
251 manifest_to_disk(vid);
252 ensure_empty_dirs_created();
253 db_lset_int("checkout", vid);
254
--- src/checkout.c
+++ src/checkout.c
@@ -243,11 +243,11 @@
243 if( !keepFlag ){
244 uncheckout(prior);
245 }
246 db_multi_exec("DELETE FROM vfile WHERE vid!=%d", vid);
247 if( !keepFlag ){
248 vfile_to_disk(vid, 0, !g.fQuiet, promptFlag);
249 }
250 checkout_set_all_exe(vid);
251 manifest_to_disk(vid);
252 ensure_empty_dirs_created();
253 db_lset_int("checkout", vid);
254
+1 -1
--- src/clone.c
+++ src/clone.c
@@ -141,11 +141,11 @@
141141
verify_all_options();
142142
143143
if( g.argc < 4 ){
144144
usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
145145
}
146
- db_open_config(0);
146
+ db_open_config(0, 0);
147147
if( -1 != file_size(g.argv[3]) ){
148148
fossil_fatal("file already exists: %s", g.argv[3]);
149149
}
150150
151151
url_parse(g.argv[2], urlFlags);
152152
--- src/clone.c
+++ src/clone.c
@@ -141,11 +141,11 @@
141 verify_all_options();
142
143 if( g.argc < 4 ){
144 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
145 }
146 db_open_config(0);
147 if( -1 != file_size(g.argv[3]) ){
148 fossil_fatal("file already exists: %s", g.argv[3]);
149 }
150
151 url_parse(g.argv[2], urlFlags);
152
--- src/clone.c
+++ src/clone.c
@@ -141,11 +141,11 @@
141 verify_all_options();
142
143 if( g.argc < 4 ){
144 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
145 }
146 db_open_config(0, 0);
147 if( -1 != file_size(g.argv[3]) ){
148 fossil_fatal("file already exists: %s", g.argv[3]);
149 }
150
151 url_parse(g.argv[2], urlFlags);
152
+1 -1
--- src/configure.c
+++ src/configure.c
@@ -883,11 +883,11 @@
883883
*/
884884
void configuration_cmd(void){
885885
int n;
886886
const char *zMethod;
887887
db_find_and_open_repository(0, 0);
888
- db_open_config(0);
888
+ db_open_config(0, 0);
889889
if( g.argc<3 ){
890890
usage("SUBCOMMAND ...");
891891
}
892892
zMethod = g.argv[2];
893893
n = strlen(zMethod);
894894
--- src/configure.c
+++ src/configure.c
@@ -883,11 +883,11 @@
883 */
884 void configuration_cmd(void){
885 int n;
886 const char *zMethod;
887 db_find_and_open_repository(0, 0);
888 db_open_config(0);
889 if( g.argc<3 ){
890 usage("SUBCOMMAND ...");
891 }
892 zMethod = g.argv[2];
893 n = strlen(zMethod);
894
--- src/configure.c
+++ src/configure.c
@@ -883,11 +883,11 @@
883 */
884 void configuration_cmd(void){
885 int n;
886 const char *zMethod;
887 db_find_and_open_repository(0, 0);
888 db_open_config(0, 0);
889 if( g.argc<3 ){
890 usage("SUBCOMMAND ...");
891 }
892 zMethod = g.argv[2];
893 n = strlen(zMethod);
894
+13 -5
--- src/db.c
+++ src/db.c
@@ -981,15 +981,15 @@
981981
** operations which hold an exclusive transaction. In a few cases, though,
982982
** it is convenient for the ~/.fossil to be attached to the main database
983983
** connection so that we can join between the various databases. In that
984984
** case, invoke this routine with useAttach as 1.
985985
*/
986
-void db_open_config(int useAttach){
986
+int db_open_config(int useAttach, int isOptional){
987987
char *zDbName;
988988
char *zHome;
989989
if( g.zConfigDbName ){
990
- if( useAttach==g.useAttach ) return;
990
+ if( useAttach==g.useAttach ) return 1; /* Already open. */
991991
db_close_config();
992992
}
993993
zHome = fossil_getenv("FOSSIL_HOME");
994994
#if defined(_WIN32) || defined(__CYGWIN__)
995995
if( zHome==0 ){
@@ -1002,24 +1002,27 @@
10021002
if( zDrive && zPath ) zHome = mprintf("%s%s", zDrive, zPath);
10031003
}
10041004
}
10051005
}
10061006
if( zHome==0 ){
1007
+ if( isOptional ) return 0;
10071008
fossil_fatal("cannot locate home directory - please set the "
10081009
"FOSSIL_HOME, LOCALAPPDATA, APPDATA, or HOMEPATH "
10091010
"environment variables");
10101011
}
10111012
#else
10121013
if( zHome==0 ){
10131014
zHome = fossil_getenv("HOME");
10141015
}
10151016
if( zHome==0 ){
1017
+ if( isOptional ) return 0;
10161018
fossil_fatal("cannot locate home directory - please set the "
10171019
"FOSSIL_HOME or HOME environment variables");
10181020
}
10191021
#endif
10201022
if( file_isdir(zHome)!=1 ){
1023
+ if( isOptional ) return 0;
10211024
fossil_fatal("invalid home directory: %s", zHome);
10221025
}
10231026
#if defined(_WIN32) || defined(__CYGWIN__)
10241027
/* . filenames give some window systems problems and many apps problems */
10251028
zDbName = mprintf("%//_fossil", zHome);
@@ -1026,15 +1029,17 @@
10261029
#else
10271030
zDbName = mprintf("%s/.fossil", zHome);
10281031
#endif
10291032
if( file_size(zDbName)<1024*3 ){
10301033
if( file_access(zHome, W_OK) ){
1034
+ if( isOptional ) return 0;
10311035
fossil_fatal("home directory %s must be writeable", zHome);
10321036
}
10331037
db_init_database(zDbName, zConfigSchema, (char*)0);
10341038
}
10351039
if( file_access(zDbName, W_OK) ){
1040
+ if( isOptional ) return 0;
10361041
fossil_fatal("configuration file %s must be writeable", zDbName);
10371042
}
10381043
if( useAttach ){
10391044
db_open_or_attach(zDbName, "configdb", &g.useAttach);
10401045
g.dbConfig = 0;
@@ -1043,10 +1048,11 @@
10431048
g.useAttach = 0;
10441049
g.dbConfig = db_open(zDbName);
10451050
g.zConfigDbType = "configdb";
10461051
}
10471052
g.zConfigDbName = zDbName;
1053
+ return 1;
10481054
}
10491055
10501056
/*
10511057
** Return TRUE if zTable exists.
10521058
*/
@@ -1156,20 +1162,22 @@
11561162
n = strlen(zPwd);
11571163
while( n>0 ){
11581164
for(i=0; i<count(aDbName); i++){
11591165
sqlite3_snprintf(sizeof(zPwd)-n, &zPwd[n], "/%s", aDbName[i]);
11601166
if( isValidLocalDb(zPwd) ){
1167
+ if( db_open_config(0, 1)==0 ){
1168
+ return 0; /* Configuration could not be opened */
1169
+ }
11611170
/* Found a valid checkout database file */
11621171
g.zLocalDbName = mprintf("%s", zPwd);
11631172
zPwd[n] = 0;
11641173
while( n>0 && zPwd[n-1]=='/' ){
11651174
n--;
11661175
zPwd[n] = 0;
11671176
}
11681177
g.zLocalRoot = mprintf("%s/", zPwd);
11691178
g.localOpen = 1;
1170
- db_open_config(0);
11711179
db_open_repository(zDbName);
11721180
return 1;
11731181
}
11741182
}
11751183
n--;
@@ -1730,11 +1738,11 @@
17301738
fossil_fatal("file already exists: %s", g.argv[2]);
17311739
}
17321740
17331741
db_create_repository(g.argv[2]);
17341742
db_open_repository(g.argv[2]);
1735
- db_open_config(0);
1743
+ db_open_config(0, 0);
17361744
if( zTemplate ) db_attach(zTemplate, "settingSrc");
17371745
db_begin_transaction();
17381746
if( zDate==0 ) zDate = "now";
17391747
db_initial_setup(zTemplate, zDate, zDefaultUser);
17401748
db_end_transaction(0);
@@ -2823,11 +2831,11 @@
28232831
*/
28242832
void setting_cmd(void){
28252833
int i;
28262834
int globalFlag = find_option("global","g",0)!=0;
28272835
int unsetFlag = g.argv[1][0]=='u';
2828
- db_open_config(1);
2836
+ db_open_config(1, 0);
28292837
if( !globalFlag ){
28302838
db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0);
28312839
}
28322840
if( !g.repositoryOpen ){
28332841
globalFlag = 1;
28342842
--- src/db.c
+++ src/db.c
@@ -981,15 +981,15 @@
981 ** operations which hold an exclusive transaction. In a few cases, though,
982 ** it is convenient for the ~/.fossil to be attached to the main database
983 ** connection so that we can join between the various databases. In that
984 ** case, invoke this routine with useAttach as 1.
985 */
986 void db_open_config(int useAttach){
987 char *zDbName;
988 char *zHome;
989 if( g.zConfigDbName ){
990 if( useAttach==g.useAttach ) return;
991 db_close_config();
992 }
993 zHome = fossil_getenv("FOSSIL_HOME");
994 #if defined(_WIN32) || defined(__CYGWIN__)
995 if( zHome==0 ){
@@ -1002,24 +1002,27 @@
1002 if( zDrive && zPath ) zHome = mprintf("%s%s", zDrive, zPath);
1003 }
1004 }
1005 }
1006 if( zHome==0 ){
 
1007 fossil_fatal("cannot locate home directory - please set the "
1008 "FOSSIL_HOME, LOCALAPPDATA, APPDATA, or HOMEPATH "
1009 "environment variables");
1010 }
1011 #else
1012 if( zHome==0 ){
1013 zHome = fossil_getenv("HOME");
1014 }
1015 if( zHome==0 ){
 
1016 fossil_fatal("cannot locate home directory - please set the "
1017 "FOSSIL_HOME or HOME environment variables");
1018 }
1019 #endif
1020 if( file_isdir(zHome)!=1 ){
 
1021 fossil_fatal("invalid home directory: %s", zHome);
1022 }
1023 #if defined(_WIN32) || defined(__CYGWIN__)
1024 /* . filenames give some window systems problems and many apps problems */
1025 zDbName = mprintf("%//_fossil", zHome);
@@ -1026,15 +1029,17 @@
1026 #else
1027 zDbName = mprintf("%s/.fossil", zHome);
1028 #endif
1029 if( file_size(zDbName)<1024*3 ){
1030 if( file_access(zHome, W_OK) ){
 
1031 fossil_fatal("home directory %s must be writeable", zHome);
1032 }
1033 db_init_database(zDbName, zConfigSchema, (char*)0);
1034 }
1035 if( file_access(zDbName, W_OK) ){
 
1036 fossil_fatal("configuration file %s must be writeable", zDbName);
1037 }
1038 if( useAttach ){
1039 db_open_or_attach(zDbName, "configdb", &g.useAttach);
1040 g.dbConfig = 0;
@@ -1043,10 +1048,11 @@
1043 g.useAttach = 0;
1044 g.dbConfig = db_open(zDbName);
1045 g.zConfigDbType = "configdb";
1046 }
1047 g.zConfigDbName = zDbName;
 
1048 }
1049
1050 /*
1051 ** Return TRUE if zTable exists.
1052 */
@@ -1156,20 +1162,22 @@
1156 n = strlen(zPwd);
1157 while( n>0 ){
1158 for(i=0; i<count(aDbName); i++){
1159 sqlite3_snprintf(sizeof(zPwd)-n, &zPwd[n], "/%s", aDbName[i]);
1160 if( isValidLocalDb(zPwd) ){
 
 
 
1161 /* Found a valid checkout database file */
1162 g.zLocalDbName = mprintf("%s", zPwd);
1163 zPwd[n] = 0;
1164 while( n>0 && zPwd[n-1]=='/' ){
1165 n--;
1166 zPwd[n] = 0;
1167 }
1168 g.zLocalRoot = mprintf("%s/", zPwd);
1169 g.localOpen = 1;
1170 db_open_config(0);
1171 db_open_repository(zDbName);
1172 return 1;
1173 }
1174 }
1175 n--;
@@ -1730,11 +1738,11 @@
1730 fossil_fatal("file already exists: %s", g.argv[2]);
1731 }
1732
1733 db_create_repository(g.argv[2]);
1734 db_open_repository(g.argv[2]);
1735 db_open_config(0);
1736 if( zTemplate ) db_attach(zTemplate, "settingSrc");
1737 db_begin_transaction();
1738 if( zDate==0 ) zDate = "now";
1739 db_initial_setup(zTemplate, zDate, zDefaultUser);
1740 db_end_transaction(0);
@@ -2823,11 +2831,11 @@
2823 */
2824 void setting_cmd(void){
2825 int i;
2826 int globalFlag = find_option("global","g",0)!=0;
2827 int unsetFlag = g.argv[1][0]=='u';
2828 db_open_config(1);
2829 if( !globalFlag ){
2830 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0);
2831 }
2832 if( !g.repositoryOpen ){
2833 globalFlag = 1;
2834
--- src/db.c
+++ src/db.c
@@ -981,15 +981,15 @@
981 ** operations which hold an exclusive transaction. In a few cases, though,
982 ** it is convenient for the ~/.fossil to be attached to the main database
983 ** connection so that we can join between the various databases. In that
984 ** case, invoke this routine with useAttach as 1.
985 */
986 int db_open_config(int useAttach, int isOptional){
987 char *zDbName;
988 char *zHome;
989 if( g.zConfigDbName ){
990 if( useAttach==g.useAttach ) return 1; /* Already open. */
991 db_close_config();
992 }
993 zHome = fossil_getenv("FOSSIL_HOME");
994 #if defined(_WIN32) || defined(__CYGWIN__)
995 if( zHome==0 ){
@@ -1002,24 +1002,27 @@
1002 if( zDrive && zPath ) zHome = mprintf("%s%s", zDrive, zPath);
1003 }
1004 }
1005 }
1006 if( zHome==0 ){
1007 if( isOptional ) return 0;
1008 fossil_fatal("cannot locate home directory - please set the "
1009 "FOSSIL_HOME, LOCALAPPDATA, APPDATA, or HOMEPATH "
1010 "environment variables");
1011 }
1012 #else
1013 if( zHome==0 ){
1014 zHome = fossil_getenv("HOME");
1015 }
1016 if( zHome==0 ){
1017 if( isOptional ) return 0;
1018 fossil_fatal("cannot locate home directory - please set the "
1019 "FOSSIL_HOME or HOME environment variables");
1020 }
1021 #endif
1022 if( file_isdir(zHome)!=1 ){
1023 if( isOptional ) return 0;
1024 fossil_fatal("invalid home directory: %s", zHome);
1025 }
1026 #if defined(_WIN32) || defined(__CYGWIN__)
1027 /* . filenames give some window systems problems and many apps problems */
1028 zDbName = mprintf("%//_fossil", zHome);
@@ -1026,15 +1029,17 @@
1029 #else
1030 zDbName = mprintf("%s/.fossil", zHome);
1031 #endif
1032 if( file_size(zDbName)<1024*3 ){
1033 if( file_access(zHome, W_OK) ){
1034 if( isOptional ) return 0;
1035 fossil_fatal("home directory %s must be writeable", zHome);
1036 }
1037 db_init_database(zDbName, zConfigSchema, (char*)0);
1038 }
1039 if( file_access(zDbName, W_OK) ){
1040 if( isOptional ) return 0;
1041 fossil_fatal("configuration file %s must be writeable", zDbName);
1042 }
1043 if( useAttach ){
1044 db_open_or_attach(zDbName, "configdb", &g.useAttach);
1045 g.dbConfig = 0;
@@ -1043,10 +1048,11 @@
1048 g.useAttach = 0;
1049 g.dbConfig = db_open(zDbName);
1050 g.zConfigDbType = "configdb";
1051 }
1052 g.zConfigDbName = zDbName;
1053 return 1;
1054 }
1055
1056 /*
1057 ** Return TRUE if zTable exists.
1058 */
@@ -1156,20 +1162,22 @@
1162 n = strlen(zPwd);
1163 while( n>0 ){
1164 for(i=0; i<count(aDbName); i++){
1165 sqlite3_snprintf(sizeof(zPwd)-n, &zPwd[n], "/%s", aDbName[i]);
1166 if( isValidLocalDb(zPwd) ){
1167 if( db_open_config(0, 1)==0 ){
1168 return 0; /* Configuration could not be opened */
1169 }
1170 /* Found a valid checkout database file */
1171 g.zLocalDbName = mprintf("%s", zPwd);
1172 zPwd[n] = 0;
1173 while( n>0 && zPwd[n-1]=='/' ){
1174 n--;
1175 zPwd[n] = 0;
1176 }
1177 g.zLocalRoot = mprintf("%s/", zPwd);
1178 g.localOpen = 1;
 
1179 db_open_repository(zDbName);
1180 return 1;
1181 }
1182 }
1183 n--;
@@ -1730,11 +1738,11 @@
1738 fossil_fatal("file already exists: %s", g.argv[2]);
1739 }
1740
1741 db_create_repository(g.argv[2]);
1742 db_open_repository(g.argv[2]);
1743 db_open_config(0, 0);
1744 if( zTemplate ) db_attach(zTemplate, "settingSrc");
1745 db_begin_transaction();
1746 if( zDate==0 ) zDate = "now";
1747 db_initial_setup(zTemplate, zDate, zDefaultUser);
1748 db_end_transaction(0);
@@ -2823,11 +2831,11 @@
2831 */
2832 void setting_cmd(void){
2833 int i;
2834 int globalFlag = find_option("global","g",0)!=0;
2835 int unsetFlag = g.argv[1][0]=='u';
2836 db_open_config(1, 0);
2837 if( !globalFlag ){
2838 db_find_and_open_repository(OPEN_ANY_SCHEMA | OPEN_OK_NOT_FOUND, 0);
2839 }
2840 if( !g.repositoryOpen ){
2841 globalFlag = 1;
2842
+2 -2
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -99,13 +99,13 @@
9999
sz3 = blob_size(&delta);
100100
blob_reset(&orig);
101101
blob_reset(&target);
102102
blob_reset(&delta);
103103
fossil_print("original size: %8d\n", sz1);
104
- fossil_print("bytes copied: %8d (%.1f%% of target)\n",
104
+ fossil_print("bytes copied: %8d (%.2f%% of target)\n",
105105
nCopy, (100.0*nCopy)/sz2);
106
- fossil_print("bytes inserted: %8d (%.1f%% of target)\n",
106
+ fossil_print("bytes inserted: %8d (%.2f%% of target)\n",
107107
nInsert, (100.0*nInsert)/sz2);
108108
fossil_print("final size: %8d\n", sz2);
109109
fossil_print("delta size: %8d\n", sz3);
110110
}
111111
112112
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -99,13 +99,13 @@
99 sz3 = blob_size(&delta);
100 blob_reset(&orig);
101 blob_reset(&target);
102 blob_reset(&delta);
103 fossil_print("original size: %8d\n", sz1);
104 fossil_print("bytes copied: %8d (%.1f%% of target)\n",
105 nCopy, (100.0*nCopy)/sz2);
106 fossil_print("bytes inserted: %8d (%.1f%% of target)\n",
107 nInsert, (100.0*nInsert)/sz2);
108 fossil_print("final size: %8d\n", sz2);
109 fossil_print("delta size: %8d\n", sz3);
110 }
111
112
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -99,13 +99,13 @@
99 sz3 = blob_size(&delta);
100 blob_reset(&orig);
101 blob_reset(&target);
102 blob_reset(&delta);
103 fossil_print("original size: %8d\n", sz1);
104 fossil_print("bytes copied: %8d (%.2f%% of target)\n",
105 nCopy, (100.0*nCopy)/sz2);
106 fossil_print("bytes inserted: %8d (%.2f%% of target)\n",
107 nInsert, (100.0*nInsert)/sz2);
108 fossil_print("final size: %8d\n", sz2);
109 fossil_print("delta size: %8d\n", sz3);
110 }
111
112
+1 -1
--- src/import.c
+++ src/import.c
@@ -1564,11 +1564,11 @@
15641564
if( !incrFlag ){
15651565
if( forceFlag ) file_delete(g.argv[2]);
15661566
db_create_repository(g.argv[2]);
15671567
}
15681568
db_open_repository(g.argv[2]);
1569
- db_open_config(0);
1569
+ db_open_config(0, 0);
15701570
15711571
db_begin_transaction();
15721572
if( !incrFlag ) db_initial_setup(0, 0, 0);
15731573
15741574
if( svnFlag ){
15751575
--- src/import.c
+++ src/import.c
@@ -1564,11 +1564,11 @@
1564 if( !incrFlag ){
1565 if( forceFlag ) file_delete(g.argv[2]);
1566 db_create_repository(g.argv[2]);
1567 }
1568 db_open_repository(g.argv[2]);
1569 db_open_config(0);
1570
1571 db_begin_transaction();
1572 if( !incrFlag ) db_initial_setup(0, 0, 0);
1573
1574 if( svnFlag ){
1575
--- src/import.c
+++ src/import.c
@@ -1564,11 +1564,11 @@
1564 if( !incrFlag ){
1565 if( forceFlag ) file_delete(g.argv[2]);
1566 db_create_repository(g.argv[2]);
1567 }
1568 db_open_repository(g.argv[2]);
1569 db_open_config(0, 0);
1570
1571 db_begin_transaction();
1572 if( !incrFlag ) db_initial_setup(0, 0, 0);
1573
1574 if( svnFlag ){
1575
+4 -3
--- src/info.c
+++ src/info.c
@@ -184,11 +184,11 @@
184184
if( !verboseFlag ){
185185
verboseFlag = find_option("detail","l",0)!=0; /* deprecated */
186186
}
187187
188188
if( g.argc==3 && (fsize = file_size(g.argv[2]))>0 && (fsize&0x1ff)==0 ){
189
- db_open_config(0);
189
+ db_open_config(0, 0);
190190
db_open_repository(g.argv[2]);
191191
db_record_repository_filename(g.argv[2]);
192192
fossil_print("project-name: %s\n", db_get("project-name", "<unnamed>"));
193193
fossil_print("project-code: %s\n", db_get("project-code", "<none>"));
194194
extraRepoInfo();
@@ -1953,10 +1953,11 @@
19531953
@ <pre>
19541954
@ %h(z)
19551955
@ </pre>
19561956
}
19571957
}else if( strncmp(zMime, "image/", 6)==0 ){
1958
+ @ <i>(file is %d(blob_size(&content)) bytes of image data)</i><br>
19581959
@ <img src="%R/raw/%s(zUuid)?m=%s(zMime)" />
19591960
style_submenu_element("Image", "Image",
19601961
"%R/raw/%s?m=%s", zUuid, zMime);
19611962
}else{
19621963
@ <i>(file is %d(blob_size(&content)) bytes of binary data)</i>
@@ -2799,11 +2800,11 @@
27992800
** Options:
28002801
**
28012802
** --author USER Make USER the author for check-in
28022803
** -m|--comment COMMENT Make COMMENT the check-in comment
28032804
** -M|--message-file FILE Read the amended comment from FILE
2804
-** --edit-comment Launch editor to revise comment
2805
+** -e|--edit-comment Launch editor to revise comment
28052806
** --date DATE Make DATE the check-in time
28062807
** --bgcolor COLOR Apply COLOR to this check-in
28072808
** --branchcolor COLOR Apply and propagate COLOR to the branch
28082809
** --tag TAG Add new TAG to this check-in
28092810
** --cancel TAG Cancel TAG from this check-in
@@ -2841,11 +2842,11 @@
28412842
int nTags, nCancels;
28422843
int i;
28432844
Stmt q;
28442845
28452846
if( g.argc==3 ) usage(AMEND_USAGE_STMT);
2846
- fEditComment = find_option("edit-comment",0,0)!=0;
2847
+ fEditComment = find_option("edit-comment","e",0)!=0;
28472848
zNewComment = find_option("comment","m",1);
28482849
zComFile = find_option("message-file","M",1);
28492850
zNewBranch = find_option("branch",0,1);
28502851
zNewColor = find_option("bgcolor",0,1);
28512852
zNewBrColor = find_option("branchcolor",0,1);
28522853
--- src/info.c
+++ src/info.c
@@ -184,11 +184,11 @@
184 if( !verboseFlag ){
185 verboseFlag = find_option("detail","l",0)!=0; /* deprecated */
186 }
187
188 if( g.argc==3 && (fsize = file_size(g.argv[2]))>0 && (fsize&0x1ff)==0 ){
189 db_open_config(0);
190 db_open_repository(g.argv[2]);
191 db_record_repository_filename(g.argv[2]);
192 fossil_print("project-name: %s\n", db_get("project-name", "<unnamed>"));
193 fossil_print("project-code: %s\n", db_get("project-code", "<none>"));
194 extraRepoInfo();
@@ -1953,10 +1953,11 @@
1953 @ <pre>
1954 @ %h(z)
1955 @ </pre>
1956 }
1957 }else if( strncmp(zMime, "image/", 6)==0 ){
 
1958 @ <img src="%R/raw/%s(zUuid)?m=%s(zMime)" />
1959 style_submenu_element("Image", "Image",
1960 "%R/raw/%s?m=%s", zUuid, zMime);
1961 }else{
1962 @ <i>(file is %d(blob_size(&content)) bytes of binary data)</i>
@@ -2799,11 +2800,11 @@
2799 ** Options:
2800 **
2801 ** --author USER Make USER the author for check-in
2802 ** -m|--comment COMMENT Make COMMENT the check-in comment
2803 ** -M|--message-file FILE Read the amended comment from FILE
2804 ** --edit-comment Launch editor to revise comment
2805 ** --date DATE Make DATE the check-in time
2806 ** --bgcolor COLOR Apply COLOR to this check-in
2807 ** --branchcolor COLOR Apply and propagate COLOR to the branch
2808 ** --tag TAG Add new TAG to this check-in
2809 ** --cancel TAG Cancel TAG from this check-in
@@ -2841,11 +2842,11 @@
2841 int nTags, nCancels;
2842 int i;
2843 Stmt q;
2844
2845 if( g.argc==3 ) usage(AMEND_USAGE_STMT);
2846 fEditComment = find_option("edit-comment",0,0)!=0;
2847 zNewComment = find_option("comment","m",1);
2848 zComFile = find_option("message-file","M",1);
2849 zNewBranch = find_option("branch",0,1);
2850 zNewColor = find_option("bgcolor",0,1);
2851 zNewBrColor = find_option("branchcolor",0,1);
2852
--- src/info.c
+++ src/info.c
@@ -184,11 +184,11 @@
184 if( !verboseFlag ){
185 verboseFlag = find_option("detail","l",0)!=0; /* deprecated */
186 }
187
188 if( g.argc==3 && (fsize = file_size(g.argv[2]))>0 && (fsize&0x1ff)==0 ){
189 db_open_config(0, 0);
190 db_open_repository(g.argv[2]);
191 db_record_repository_filename(g.argv[2]);
192 fossil_print("project-name: %s\n", db_get("project-name", "<unnamed>"));
193 fossil_print("project-code: %s\n", db_get("project-code", "<none>"));
194 extraRepoInfo();
@@ -1953,10 +1953,11 @@
1953 @ <pre>
1954 @ %h(z)
1955 @ </pre>
1956 }
1957 }else if( strncmp(zMime, "image/", 6)==0 ){
1958 @ <i>(file is %d(blob_size(&content)) bytes of image data)</i><br>
1959 @ <img src="%R/raw/%s(zUuid)?m=%s(zMime)" />
1960 style_submenu_element("Image", "Image",
1961 "%R/raw/%s?m=%s", zUuid, zMime);
1962 }else{
1963 @ <i>(file is %d(blob_size(&content)) bytes of binary data)</i>
@@ -2799,11 +2800,11 @@
2800 ** Options:
2801 **
2802 ** --author USER Make USER the author for check-in
2803 ** -m|--comment COMMENT Make COMMENT the check-in comment
2804 ** -M|--message-file FILE Read the amended comment from FILE
2805 ** -e|--edit-comment Launch editor to revise comment
2806 ** --date DATE Make DATE the check-in time
2807 ** --bgcolor COLOR Apply COLOR to this check-in
2808 ** --branchcolor COLOR Apply and propagate COLOR to the branch
2809 ** --tag TAG Add new TAG to this check-in
2810 ** --cancel TAG Cancel TAG from this check-in
@@ -2841,11 +2842,11 @@
2842 int nTags, nCancels;
2843 int i;
2844 Stmt q;
2845
2846 if( g.argc==3 ) usage(AMEND_USAGE_STMT);
2847 fEditComment = find_option("edit-comment","e",0)!=0;
2848 zNewComment = find_option("comment","m",1);
2849 zComFile = find_option("message-file","M",1);
2850 zNewBranch = find_option("branch",0,1);
2851 zNewColor = find_option("bgcolor",0,1);
2852 zNewBrColor = find_option("branchcolor",0,1);
2853
+15 -8
--- src/json.c
+++ src/json.c
@@ -222,10 +222,11 @@
222222
** allocation error (more likely than not) or a serious internal error
223223
** such as numeric overflow).
224224
*/
225225
void json_gc_add( char const * key, cson_value * v ){
226226
int const rc = cson_array_append( g.json.gc.a, v );
227
+
227228
assert( NULL != g.json.gc.a );
228229
if( 0 != rc ){
229230
cson_value_free( v );
230231
}
231232
assert( (0==rc) && "Adding item to GC failed." );
@@ -478,11 +479,11 @@
478479
** If the option is not found, dftl is returned.
479480
*/
480481
int json_find_option_bool(char const * zKey,
481482
char const * zCLILong,
482483
char const * zCLIShort,
483
- char dflt ){
484
+ int dflt ){
484485
int rc = -1;
485486
if(!g.isHTTP){
486487
if(NULL != find_option(zCLILong ? zCLILong : zKey,
487488
zCLIShort, 0)){
488489
rc = 1;
@@ -631,11 +632,12 @@
631632
** results).
632633
**
633634
** The result of this call are cached for future calls.
634635
*/
635636
cson_value * json_auth_token(){
636
- if( !g.json.authToken ){
637
+ assert(g.json.gc.a && "json_main_bootstrap() was not called!");
638
+ if( !g.json.authToken ){
637639
/* Try to get an authorization token from GET parameter, POSTed
638640
JSON, or fossil cookie (in that order). */
639641
g.json.authToken = json_getenv(FossilJsonKeys.authToken);
640642
if(g.json.authToken
641643
&& cson_value_is_string(g.json.authToken)
@@ -704,11 +706,13 @@
704706
which need a long lifetime but don't have a logical parent to put
705707
them in.
706708
*/
707709
v = cson_value_new_array();
708710
g.json.gc.v = v;
711
+ assert(0 != g.json.gc.v);
709712
g.json.gc.a = cson_value_get_array(v);
713
+ assert(0 != g.json.gc.a);
710714
cson_value_add_reference(v)
711715
/* Needed to allow us to include this value in other JSON
712716
containers without transferring ownership to those containers.
713717
All other persistent g.json.XXX.v values get appended to
714718
g.json.gc.a, and therefore already have a live reference
@@ -755,10 +759,11 @@
755759
void json_warn( int code, char const * fmt, ... ){
756760
cson_object * obj = NULL;
757761
assert( (code>FSL_JSON_W_START)
758762
&& (code<FSL_JSON_W_END)
759763
&& "Invalid warning code.");
764
+ assert(g.json.gc.a && "json_main_bootstrap() was not called!");
760765
if(!g.json.warnings){
761766
g.json.warnings = cson_new_array();
762767
assert((NULL != g.json.warnings) && "Alloc error.");
763768
json_gc_add("$WARNINGS",cson_array_value(g.json.warnings));
764769
}
@@ -801,11 +806,11 @@
801806
** Achtung: empty elements will be skipped, meaning consecutive empty
802807
** elements are collapsed.
803808
*/
804809
int json_string_split( char const * zStr,
805810
char separator,
806
- char doDeHttp,
811
+ int doDeHttp,
807812
cson_array * target ){
808813
char const * p = zStr /* current byte */;
809814
char const * head /* current start-of-token */;
810815
unsigned int len = 0 /* current token's length */;
811816
int rc = 0 /* return code (number of added elements)*/;
@@ -876,11 +881,11 @@
876881
** The returned value is owned by the caller. If not NULL then it
877882
** _will_ have a JSON type of Array.
878883
*/
879884
cson_value * json_string_split2( char const * zStr,
880885
char separator,
881
- char doDeHttp ){
886
+ int doDeHttp ){
882887
cson_array * a = cson_new_array();
883888
int rc = json_string_split( zStr, separator, doDeHttp, a );
884889
if( 0>=rc ){
885890
cson_free_array(a);
886891
a = NULL;
@@ -904,10 +909,11 @@
904909
** This must only be called once, or an assertion may be triggered.
905910
*/
906911
static void json_mode_bootstrap(){
907912
static char once = 0 /* guard against multiple runs */;
908913
char const * zPath = P("PATH_INFO");
914
+ assert(g.json.gc.a && "json_main_bootstrap() was not called!");
909915
assert( (0==once) && "json_mode_bootstrap() called too many times!");
910916
if( once ){
911917
return;
912918
}else{
913919
once = 1;
@@ -1082,11 +1088,11 @@
10821088
**
10831089
** Note that CLI options are not included in the command path. Use
10841090
** find_option() to get those.
10851091
**
10861092
*/
1087
-char const * json_command_arg(unsigned char ndx){
1093
+char const * json_command_arg(unsigned short ndx){
10881094
cson_array * ar = g.json.cmd.a;
10891095
assert((NULL!=ar) && "Internal error. Was json_mode_bootstrap() called?");
10901096
assert((g.argc>1) && "Internal error - we never should have gotten this far.");
10911097
if( g.json.cmd.offset < 0 ){
10921098
/* first-time setup. */
@@ -1497,11 +1503,11 @@
14971503
**
14981504
** For generating the resultText property: if msg is not NULL then it
14991505
** is used as-is. If it is NULL then g.zErrMsg is checked, and if that
15001506
** is NULL then json_err_cstr(code) is used.
15011507
*/
1502
-void json_err( int code, char const * msg, char alsoOutput ){
1508
+void json_err( int code, char const * msg, int alsoOutput ){
15031509
int rc = code ? code : (g.json.resultCode
15041510
? g.json.resultCode
15051511
: FSL_JSON_E_UNKNOWN);
15061512
cson_value * resp = NULL;
15071513
rc = json_dumbdown_rc(rc);
@@ -1662,11 +1668,11 @@
16621668
**
16631669
** FIXME: change this to take a (char const *) instead of a blob,
16641670
** to simplify the trivial use-cases (which don't need a Blob).
16651671
*/
16661672
cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
1667
- char resetBlob){
1673
+ int resetBlob){
16681674
Stmt q = empty_Stmt;
16691675
cson_value * pay = NULL;
16701676
assert( blob_size(pSql) > 0 );
16711677
db_prepare(&q, "%s", blob_str(pSql) /*safe-for-%s*/);
16721678
if(resetBlob){
@@ -1687,11 +1693,11 @@
16871693
** wrapper for that function).
16881694
**
16891695
** If there are no tags then this function returns NULL, not an empty
16901696
** Array.
16911697
*/
1692
-cson_value * json_tags_for_checkin_rid(int rid, char propagatingOnly){
1698
+cson_value * json_tags_for_checkin_rid(int rid, int propagatingOnly){
16931699
cson_value * v = NULL;
16941700
char * tags = info_tags_of_checkin(rid, propagatingOnly);
16951701
if(tags){
16961702
if(*tags){
16971703
v = json_string_split2(tags,',',0);
@@ -2221,10 +2227,11 @@
22212227
** This function dispatches them, and is the HTTP equivalent of
22222228
** json_cmd_top().
22232229
*/
22242230
void json_page_top(void){
22252231
char const * zCommand;
2232
+ assert(g.json.gc.a && "json_main_bootstrap() was not called!");
22262233
json_mode_bootstrap();
22272234
zCommand = json_command_arg(1);
22282235
if(!zCommand || !*zCommand){
22292236
json_dispatch_missing_args_err( JsonPageDefs,
22302237
"No command (sub-path) specified."
22312238
--- src/json.c
+++ src/json.c
@@ -222,10 +222,11 @@
222 ** allocation error (more likely than not) or a serious internal error
223 ** such as numeric overflow).
224 */
225 void json_gc_add( char const * key, cson_value * v ){
226 int const rc = cson_array_append( g.json.gc.a, v );
 
227 assert( NULL != g.json.gc.a );
228 if( 0 != rc ){
229 cson_value_free( v );
230 }
231 assert( (0==rc) && "Adding item to GC failed." );
@@ -478,11 +479,11 @@
478 ** If the option is not found, dftl is returned.
479 */
480 int json_find_option_bool(char const * zKey,
481 char const * zCLILong,
482 char const * zCLIShort,
483 char dflt ){
484 int rc = -1;
485 if(!g.isHTTP){
486 if(NULL != find_option(zCLILong ? zCLILong : zKey,
487 zCLIShort, 0)){
488 rc = 1;
@@ -631,11 +632,12 @@
631 ** results).
632 **
633 ** The result of this call are cached for future calls.
634 */
635 cson_value * json_auth_token(){
636 if( !g.json.authToken ){
 
637 /* Try to get an authorization token from GET parameter, POSTed
638 JSON, or fossil cookie (in that order). */
639 g.json.authToken = json_getenv(FossilJsonKeys.authToken);
640 if(g.json.authToken
641 && cson_value_is_string(g.json.authToken)
@@ -704,11 +706,13 @@
704 which need a long lifetime but don't have a logical parent to put
705 them in.
706 */
707 v = cson_value_new_array();
708 g.json.gc.v = v;
 
709 g.json.gc.a = cson_value_get_array(v);
 
710 cson_value_add_reference(v)
711 /* Needed to allow us to include this value in other JSON
712 containers without transferring ownership to those containers.
713 All other persistent g.json.XXX.v values get appended to
714 g.json.gc.a, and therefore already have a live reference
@@ -755,10 +759,11 @@
755 void json_warn( int code, char const * fmt, ... ){
756 cson_object * obj = NULL;
757 assert( (code>FSL_JSON_W_START)
758 && (code<FSL_JSON_W_END)
759 && "Invalid warning code.");
 
760 if(!g.json.warnings){
761 g.json.warnings = cson_new_array();
762 assert((NULL != g.json.warnings) && "Alloc error.");
763 json_gc_add("$WARNINGS",cson_array_value(g.json.warnings));
764 }
@@ -801,11 +806,11 @@
801 ** Achtung: empty elements will be skipped, meaning consecutive empty
802 ** elements are collapsed.
803 */
804 int json_string_split( char const * zStr,
805 char separator,
806 char doDeHttp,
807 cson_array * target ){
808 char const * p = zStr /* current byte */;
809 char const * head /* current start-of-token */;
810 unsigned int len = 0 /* current token's length */;
811 int rc = 0 /* return code (number of added elements)*/;
@@ -876,11 +881,11 @@
876 ** The returned value is owned by the caller. If not NULL then it
877 ** _will_ have a JSON type of Array.
878 */
879 cson_value * json_string_split2( char const * zStr,
880 char separator,
881 char doDeHttp ){
882 cson_array * a = cson_new_array();
883 int rc = json_string_split( zStr, separator, doDeHttp, a );
884 if( 0>=rc ){
885 cson_free_array(a);
886 a = NULL;
@@ -904,10 +909,11 @@
904 ** This must only be called once, or an assertion may be triggered.
905 */
906 static void json_mode_bootstrap(){
907 static char once = 0 /* guard against multiple runs */;
908 char const * zPath = P("PATH_INFO");
 
909 assert( (0==once) && "json_mode_bootstrap() called too many times!");
910 if( once ){
911 return;
912 }else{
913 once = 1;
@@ -1082,11 +1088,11 @@
1082 **
1083 ** Note that CLI options are not included in the command path. Use
1084 ** find_option() to get those.
1085 **
1086 */
1087 char const * json_command_arg(unsigned char ndx){
1088 cson_array * ar = g.json.cmd.a;
1089 assert((NULL!=ar) && "Internal error. Was json_mode_bootstrap() called?");
1090 assert((g.argc>1) && "Internal error - we never should have gotten this far.");
1091 if( g.json.cmd.offset < 0 ){
1092 /* first-time setup. */
@@ -1497,11 +1503,11 @@
1497 **
1498 ** For generating the resultText property: if msg is not NULL then it
1499 ** is used as-is. If it is NULL then g.zErrMsg is checked, and if that
1500 ** is NULL then json_err_cstr(code) is used.
1501 */
1502 void json_err( int code, char const * msg, char alsoOutput ){
1503 int rc = code ? code : (g.json.resultCode
1504 ? g.json.resultCode
1505 : FSL_JSON_E_UNKNOWN);
1506 cson_value * resp = NULL;
1507 rc = json_dumbdown_rc(rc);
@@ -1662,11 +1668,11 @@
1662 **
1663 ** FIXME: change this to take a (char const *) instead of a blob,
1664 ** to simplify the trivial use-cases (which don't need a Blob).
1665 */
1666 cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
1667 char resetBlob){
1668 Stmt q = empty_Stmt;
1669 cson_value * pay = NULL;
1670 assert( blob_size(pSql) > 0 );
1671 db_prepare(&q, "%s", blob_str(pSql) /*safe-for-%s*/);
1672 if(resetBlob){
@@ -1687,11 +1693,11 @@
1687 ** wrapper for that function).
1688 **
1689 ** If there are no tags then this function returns NULL, not an empty
1690 ** Array.
1691 */
1692 cson_value * json_tags_for_checkin_rid(int rid, char propagatingOnly){
1693 cson_value * v = NULL;
1694 char * tags = info_tags_of_checkin(rid, propagatingOnly);
1695 if(tags){
1696 if(*tags){
1697 v = json_string_split2(tags,',',0);
@@ -2221,10 +2227,11 @@
2221 ** This function dispatches them, and is the HTTP equivalent of
2222 ** json_cmd_top().
2223 */
2224 void json_page_top(void){
2225 char const * zCommand;
 
2226 json_mode_bootstrap();
2227 zCommand = json_command_arg(1);
2228 if(!zCommand || !*zCommand){
2229 json_dispatch_missing_args_err( JsonPageDefs,
2230 "No command (sub-path) specified."
2231
--- src/json.c
+++ src/json.c
@@ -222,10 +222,11 @@
222 ** allocation error (more likely than not) or a serious internal error
223 ** such as numeric overflow).
224 */
225 void json_gc_add( char const * key, cson_value * v ){
226 int const rc = cson_array_append( g.json.gc.a, v );
227
228 assert( NULL != g.json.gc.a );
229 if( 0 != rc ){
230 cson_value_free( v );
231 }
232 assert( (0==rc) && "Adding item to GC failed." );
@@ -478,11 +479,11 @@
479 ** If the option is not found, dftl is returned.
480 */
481 int json_find_option_bool(char const * zKey,
482 char const * zCLILong,
483 char const * zCLIShort,
484 int dflt ){
485 int rc = -1;
486 if(!g.isHTTP){
487 if(NULL != find_option(zCLILong ? zCLILong : zKey,
488 zCLIShort, 0)){
489 rc = 1;
@@ -631,11 +632,12 @@
632 ** results).
633 **
634 ** The result of this call are cached for future calls.
635 */
636 cson_value * json_auth_token(){
637 assert(g.json.gc.a && "json_main_bootstrap() was not called!");
638 if( !g.json.authToken ){
639 /* Try to get an authorization token from GET parameter, POSTed
640 JSON, or fossil cookie (in that order). */
641 g.json.authToken = json_getenv(FossilJsonKeys.authToken);
642 if(g.json.authToken
643 && cson_value_is_string(g.json.authToken)
@@ -704,11 +706,13 @@
706 which need a long lifetime but don't have a logical parent to put
707 them in.
708 */
709 v = cson_value_new_array();
710 g.json.gc.v = v;
711 assert(0 != g.json.gc.v);
712 g.json.gc.a = cson_value_get_array(v);
713 assert(0 != g.json.gc.a);
714 cson_value_add_reference(v)
715 /* Needed to allow us to include this value in other JSON
716 containers without transferring ownership to those containers.
717 All other persistent g.json.XXX.v values get appended to
718 g.json.gc.a, and therefore already have a live reference
@@ -755,10 +759,11 @@
759 void json_warn( int code, char const * fmt, ... ){
760 cson_object * obj = NULL;
761 assert( (code>FSL_JSON_W_START)
762 && (code<FSL_JSON_W_END)
763 && "Invalid warning code.");
764 assert(g.json.gc.a && "json_main_bootstrap() was not called!");
765 if(!g.json.warnings){
766 g.json.warnings = cson_new_array();
767 assert((NULL != g.json.warnings) && "Alloc error.");
768 json_gc_add("$WARNINGS",cson_array_value(g.json.warnings));
769 }
@@ -801,11 +806,11 @@
806 ** Achtung: empty elements will be skipped, meaning consecutive empty
807 ** elements are collapsed.
808 */
809 int json_string_split( char const * zStr,
810 char separator,
811 int doDeHttp,
812 cson_array * target ){
813 char const * p = zStr /* current byte */;
814 char const * head /* current start-of-token */;
815 unsigned int len = 0 /* current token's length */;
816 int rc = 0 /* return code (number of added elements)*/;
@@ -876,11 +881,11 @@
881 ** The returned value is owned by the caller. If not NULL then it
882 ** _will_ have a JSON type of Array.
883 */
884 cson_value * json_string_split2( char const * zStr,
885 char separator,
886 int doDeHttp ){
887 cson_array * a = cson_new_array();
888 int rc = json_string_split( zStr, separator, doDeHttp, a );
889 if( 0>=rc ){
890 cson_free_array(a);
891 a = NULL;
@@ -904,10 +909,11 @@
909 ** This must only be called once, or an assertion may be triggered.
910 */
911 static void json_mode_bootstrap(){
912 static char once = 0 /* guard against multiple runs */;
913 char const * zPath = P("PATH_INFO");
914 assert(g.json.gc.a && "json_main_bootstrap() was not called!");
915 assert( (0==once) && "json_mode_bootstrap() called too many times!");
916 if( once ){
917 return;
918 }else{
919 once = 1;
@@ -1082,11 +1088,11 @@
1088 **
1089 ** Note that CLI options are not included in the command path. Use
1090 ** find_option() to get those.
1091 **
1092 */
1093 char const * json_command_arg(unsigned short ndx){
1094 cson_array * ar = g.json.cmd.a;
1095 assert((NULL!=ar) && "Internal error. Was json_mode_bootstrap() called?");
1096 assert((g.argc>1) && "Internal error - we never should have gotten this far.");
1097 if( g.json.cmd.offset < 0 ){
1098 /* first-time setup. */
@@ -1497,11 +1503,11 @@
1503 **
1504 ** For generating the resultText property: if msg is not NULL then it
1505 ** is used as-is. If it is NULL then g.zErrMsg is checked, and if that
1506 ** is NULL then json_err_cstr(code) is used.
1507 */
1508 void json_err( int code, char const * msg, int alsoOutput ){
1509 int rc = code ? code : (g.json.resultCode
1510 ? g.json.resultCode
1511 : FSL_JSON_E_UNKNOWN);
1512 cson_value * resp = NULL;
1513 rc = json_dumbdown_rc(rc);
@@ -1662,11 +1668,11 @@
1668 **
1669 ** FIXME: change this to take a (char const *) instead of a blob,
1670 ** to simplify the trivial use-cases (which don't need a Blob).
1671 */
1672 cson_value * json_sql_to_array_of_obj(Blob * pSql, cson_array * pTgt,
1673 int resetBlob){
1674 Stmt q = empty_Stmt;
1675 cson_value * pay = NULL;
1676 assert( blob_size(pSql) > 0 );
1677 db_prepare(&q, "%s", blob_str(pSql) /*safe-for-%s*/);
1678 if(resetBlob){
@@ -1687,11 +1693,11 @@
1693 ** wrapper for that function).
1694 **
1695 ** If there are no tags then this function returns NULL, not an empty
1696 ** Array.
1697 */
1698 cson_value * json_tags_for_checkin_rid(int rid, int propagatingOnly){
1699 cson_value * v = NULL;
1700 char * tags = info_tags_of_checkin(rid, propagatingOnly);
1701 if(tags){
1702 if(*tags){
1703 v = json_string_split2(tags,',',0);
@@ -2221,10 +2227,11 @@
2227 ** This function dispatches them, and is the HTTP equivalent of
2228 ** json_cmd_top().
2229 */
2230 void json_page_top(void){
2231 char const * zCommand;
2232 assert(g.json.gc.a && "json_main_bootstrap() was not called!");
2233 json_mode_bootstrap();
2234 zCommand = json_command_arg(1);
2235 if(!zCommand || !*zCommand){
2236 json_dispatch_missing_args_err( JsonPageDefs,
2237 "No command (sub-path) specified."
2238
+1 -1
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -546,11 +546,11 @@
546546
blob_init(&w1, pW1->zWiki, -1);
547547
blob_zero(&w2);
548548
blob_init(&w2, pW2->zWiki, -1);
549549
blob_zero(&d);
550550
diffFlags = DIFF_IGNORE_EOLWS | DIFF_STRIP_EOLCR;
551
- text_diff(&w2, &w1, &d, 0, diffFlags);
551
+ text_diff(&w1, &w2, &d, 0, diffFlags);
552552
blob_reset(&w1);
553553
blob_reset(&w2);
554554
555555
pay = cson_new_object();
556556
557557
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -546,11 +546,11 @@
546 blob_init(&w1, pW1->zWiki, -1);
547 blob_zero(&w2);
548 blob_init(&w2, pW2->zWiki, -1);
549 blob_zero(&d);
550 diffFlags = DIFF_IGNORE_EOLWS | DIFF_STRIP_EOLCR;
551 text_diff(&w2, &w1, &d, 0, diffFlags);
552 blob_reset(&w1);
553 blob_reset(&w2);
554
555 pay = cson_new_object();
556
557
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -546,11 +546,11 @@
546 blob_init(&w1, pW1->zWiki, -1);
547 blob_zero(&w2);
548 blob_init(&w2, pW2->zWiki, -1);
549 blob_zero(&d);
550 diffFlags = DIFF_IGNORE_EOLWS | DIFF_STRIP_EOLCR;
551 text_diff(&w1, &w2, &d, 0, diffFlags);
552 blob_reset(&w1);
553 blob_reset(&w2);
554
555 pay = cson_new_object();
556
557
+2 -2
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592592
#endif
593593
{
594594
const char *zCmdName = "unknown";
595595
int idx;
596596
int rc;
597
- if( sqlite3_libversion_number()<3008007 ){
598
- fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.7",
597
+ if( sqlite3_libversion_number()<3010000 ){
598
+ fossil_fatal("Unsuitable SQLite version %s, must be at least 3.10.0",
599599
sqlite3_libversion());
600600
}
601601
sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602602
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603603
memset(&g, 0, sizeof(g));
604604
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592 #endif
593 {
594 const char *zCmdName = "unknown";
595 int idx;
596 int rc;
597 if( sqlite3_libversion_number()<3008007 ){
598 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.7",
599 sqlite3_libversion());
600 }
601 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603 memset(&g, 0, sizeof(g));
604
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592 #endif
593 {
594 const char *zCmdName = "unknown";
595 int idx;
596 int rc;
597 if( sqlite3_libversion_number()<3010000 ){
598 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.10.0",
599 sqlite3_libversion());
600 }
601 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603 memset(&g, 0, sizeof(g));
604
+2 -2
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592592
#endif
593593
{
594594
const char *zCmdName = "unknown";
595595
int idx;
596596
int rc;
597
- if( sqlite3_libversion_number()<3008007 ){
598
- fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.7",
597
+ if( sqlite3_libversion_number()<3010000 ){
598
+ fossil_fatal("Unsuitable SQLite version %s, must be at least 3.10.0",
599599
sqlite3_libversion());
600600
}
601601
sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602602
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603603
memset(&g, 0, sizeof(g));
604604
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592 #endif
593 {
594 const char *zCmdName = "unknown";
595 int idx;
596 int rc;
597 if( sqlite3_libversion_number()<3008007 ){
598 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.8.7",
599 sqlite3_libversion());
600 }
601 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603 memset(&g, 0, sizeof(g));
604
--- src/main.c
+++ src/main.c
@@ -592,12 +592,12 @@
592 #endif
593 {
594 const char *zCmdName = "unknown";
595 int idx;
596 int rc;
597 if( sqlite3_libversion_number()<3010000 ){
598 fossil_fatal("Unsuitable SQLite version %s, must be at least 3.10.0",
599 sqlite3_libversion());
600 }
601 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
602 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
603 memset(&g, 0, sizeof(g));
604
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -617,11 +617,11 @@
617617
#### The directories where the OpenSSL include and library files are located.
618618
# The recommended usage here is to use the Sysinternals junction tool
619619
# to create a hard link between an "openssl-1.x" sub-directory of the
620620
# Fossil source code directory and the target OpenSSL source directory.
621621
#
622
-OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
622
+OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
623623
OPENSSLINCDIR = $(OPENSSLDIR)/include
624624
OPENSSLLIBDIR = $(OPENSSLDIR)
625625
626626
#### Either the directory where the Tcl library is installed or the Tcl
627627
# source code directory resides (depending on the value of the macro
@@ -1406,11 +1406,11 @@
14061406
!ifndef FOSSIL_ENABLE_WINXP
14071407
FOSSIL_ENABLE_WINXP = 0
14081408
!endif
14091409
14101410
!if $(FOSSIL_ENABLE_SSL)!=0
1411
-SSLDIR = $(B)\compat\openssl-1.0.2f
1411
+SSLDIR = $(B)\compat\openssl-1.0.2g
14121412
SSLINCDIR = $(SSLDIR)\inc32
14131413
!if $(FOSSIL_DYNAMIC_BUILD)!=0
14141414
SSLLIBDIR = $(SSLDIR)\out32dll
14151415
!else
14161416
SSLLIBDIR = $(SSLDIR)\out32
14171417
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -617,11 +617,11 @@
617 #### The directories where the OpenSSL include and library files are located.
618 # The recommended usage here is to use the Sysinternals junction tool
619 # to create a hard link between an "openssl-1.x" sub-directory of the
620 # Fossil source code directory and the target OpenSSL source directory.
621 #
622 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
623 OPENSSLINCDIR = $(OPENSSLDIR)/include
624 OPENSSLLIBDIR = $(OPENSSLDIR)
625
626 #### Either the directory where the Tcl library is installed or the Tcl
627 # source code directory resides (depending on the value of the macro
@@ -1406,11 +1406,11 @@
1406 !ifndef FOSSIL_ENABLE_WINXP
1407 FOSSIL_ENABLE_WINXP = 0
1408 !endif
1409
1410 !if $(FOSSIL_ENABLE_SSL)!=0
1411 SSLDIR = $(B)\compat\openssl-1.0.2f
1412 SSLINCDIR = $(SSLDIR)\inc32
1413 !if $(FOSSIL_DYNAMIC_BUILD)!=0
1414 SSLLIBDIR = $(SSLDIR)\out32dll
1415 !else
1416 SSLLIBDIR = $(SSLDIR)\out32
1417
--- src/makemake.tcl
+++ src/makemake.tcl
@@ -617,11 +617,11 @@
617 #### The directories where the OpenSSL include and library files are located.
618 # The recommended usage here is to use the Sysinternals junction tool
619 # to create a hard link between an "openssl-1.x" sub-directory of the
620 # Fossil source code directory and the target OpenSSL source directory.
621 #
622 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
623 OPENSSLINCDIR = $(OPENSSLDIR)/include
624 OPENSSLLIBDIR = $(OPENSSLDIR)
625
626 #### Either the directory where the Tcl library is installed or the Tcl
627 # source code directory resides (depending on the value of the macro
@@ -1406,11 +1406,11 @@
1406 !ifndef FOSSIL_ENABLE_WINXP
1407 FOSSIL_ENABLE_WINXP = 0
1408 !endif
1409
1410 !if $(FOSSIL_ENABLE_SSL)!=0
1411 SSLDIR = $(B)\compat\openssl-1.0.2g
1412 SSLINCDIR = $(SSLDIR)\inc32
1413 !if $(FOSSIL_DYNAMIC_BUILD)!=0
1414 SSLLIBDIR = $(SSLDIR)\out32dll
1415 !else
1416 SSLLIBDIR = $(SSLDIR)\out32
1417
+1 -1
--- src/rebuild.c
+++ src/rebuild.c
@@ -958,11 +958,11 @@
958958
db_open_repository(g.argv[2]);
959959
960960
/* We should be done with options.. */
961961
verify_all_options();
962962
963
- db_open_config(0);
963
+ db_open_config(0, 0);
964964
db_begin_transaction();
965965
db_initial_setup(0, 0, 0);
966966
967967
fossil_print("Reading files from directory \"%s\"...\n", g.argv[3]);
968968
recon_read_dir(g.argv[3]);
969969
--- src/rebuild.c
+++ src/rebuild.c
@@ -958,11 +958,11 @@
958 db_open_repository(g.argv[2]);
959
960 /* We should be done with options.. */
961 verify_all_options();
962
963 db_open_config(0);
964 db_begin_transaction();
965 db_initial_setup(0, 0, 0);
966
967 fossil_print("Reading files from directory \"%s\"...\n", g.argv[3]);
968 recon_read_dir(g.argv[3]);
969
--- src/rebuild.c
+++ src/rebuild.c
@@ -958,11 +958,11 @@
958 db_open_repository(g.argv[2]);
959
960 /* We should be done with options.. */
961 verify_all_options();
962
963 db_open_config(0, 0);
964 db_begin_transaction();
965 db_initial_setup(0, 0, 0);
966
967 fossil_print("Reading files from directory \"%s\"...\n", g.argv[3]);
968 recon_read_dir(g.argv[3]);
969
+12 -14
--- src/report.c
+++ src/report.c
@@ -955,11 +955,11 @@
955955
** k Sort by the data-sortkey property
956956
** x This column is not sortable
957957
**
958958
** Capital letters mean sort in reverse order.
959959
** If there are fewer characters in zColumnTypes[] than their are columns,
960
-** the all extra columns assume type "t" (text).
960
+** then all extra columns assume type "t" (text).
961961
**
962962
** The third parameter is the column that was initially sorted (using 1-based
963963
** column numbers, like SQL). Make this value 0 if none of the columns are
964964
** initially sorted. Make the value negative if the column is initially sorted
965965
** in reverse order.
@@ -973,10 +973,12 @@
973973
){
974974
@ <script>
975975
@ function SortableTable(tableEl,columnTypes,initSort){
976976
@ this.tbody = tableEl.getElementsByTagName('tbody');
977977
@ this.columnTypes = columnTypes;
978
+ @ var ncols = tableEl.rows[0].cells.length;
979
+ @ for(var i = columnTypes.length; i<=ncols; i++){this.columnTypes += 't';}
978980
@ this.sort = function (cell) {
979981
@ var column = cell.cellIndex;
980982
@ var sortFn;
981983
@ switch( cell.sortType ){
982984
if( strchr(zColumnTypes,'n') ){
@@ -983,13 +985,11 @@
983985
@ case "n": sortFn = this.sortNumeric; break;
984986
}
985987
if( strchr(zColumnTypes,'N') ){
986988
@ case "N": sortFn = this.sortReverseNumeric; break;
987989
}
988
- if( strchr(zColumnTypes,'t') ){
989
- @ case "t": sortFn = this.sortText; break;
990
- }
990
+ @ case "t": sortFn = this.sortText; break;
991991
if( strchr(zColumnTypes,'T') ){
992992
@ case "T": sortFn = this.sortReverseText; break;
993993
}
994994
if( strchr(zColumnTypes,'k') ){
995995
@ case "k": sortFn = this.sortKey; break;
@@ -1031,20 +1031,18 @@
10311031
@ var clsName = hdrCell.className.replace(/\s*\bsort\s*\w+/, '');
10321032
@ clsName += ' sort ' + sortType;
10331033
@ hdrCell.className = clsName;
10341034
@ }
10351035
@ }
1036
- if( strchr(zColumnTypes,'t') ){
1037
- @ this.sortText = function(a,b) {
1038
- @ var i = thisObject.sortIndex;
1039
- @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1040
- @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1041
- @ if(aa<bb) return -1;
1042
- @ if(aa==bb) return a.rowIndex-b.rowIndex;
1043
- @ return 1;
1044
- @ }
1045
- }
1036
+ @ this.sortText = function(a,b) {
1037
+ @ var i = thisObject.sortIndex;
1038
+ @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1039
+ @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1040
+ @ if(aa<bb) return -1;
1041
+ @ if(aa==bb) return a.rowIndex-b.rowIndex;
1042
+ @ return 1;
1043
+ @ }
10461044
if( strchr(zColumnTypes,'T') ){
10471045
@ this.sortReverseText = function(a,b) {
10481046
@ var i = thisObject.sortIndex;
10491047
@ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
10501048
@ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
10511049
--- src/report.c
+++ src/report.c
@@ -955,11 +955,11 @@
955 ** k Sort by the data-sortkey property
956 ** x This column is not sortable
957 **
958 ** Capital letters mean sort in reverse order.
959 ** If there are fewer characters in zColumnTypes[] than their are columns,
960 ** the all extra columns assume type "t" (text).
961 **
962 ** The third parameter is the column that was initially sorted (using 1-based
963 ** column numbers, like SQL). Make this value 0 if none of the columns are
964 ** initially sorted. Make the value negative if the column is initially sorted
965 ** in reverse order.
@@ -973,10 +973,12 @@
973 ){
974 @ <script>
975 @ function SortableTable(tableEl,columnTypes,initSort){
976 @ this.tbody = tableEl.getElementsByTagName('tbody');
977 @ this.columnTypes = columnTypes;
 
 
978 @ this.sort = function (cell) {
979 @ var column = cell.cellIndex;
980 @ var sortFn;
981 @ switch( cell.sortType ){
982 if( strchr(zColumnTypes,'n') ){
@@ -983,13 +985,11 @@
983 @ case "n": sortFn = this.sortNumeric; break;
984 }
985 if( strchr(zColumnTypes,'N') ){
986 @ case "N": sortFn = this.sortReverseNumeric; break;
987 }
988 if( strchr(zColumnTypes,'t') ){
989 @ case "t": sortFn = this.sortText; break;
990 }
991 if( strchr(zColumnTypes,'T') ){
992 @ case "T": sortFn = this.sortReverseText; break;
993 }
994 if( strchr(zColumnTypes,'k') ){
995 @ case "k": sortFn = this.sortKey; break;
@@ -1031,20 +1031,18 @@
1031 @ var clsName = hdrCell.className.replace(/\s*\bsort\s*\w+/, '');
1032 @ clsName += ' sort ' + sortType;
1033 @ hdrCell.className = clsName;
1034 @ }
1035 @ }
1036 if( strchr(zColumnTypes,'t') ){
1037 @ this.sortText = function(a,b) {
1038 @ var i = thisObject.sortIndex;
1039 @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1040 @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1041 @ if(aa<bb) return -1;
1042 @ if(aa==bb) return a.rowIndex-b.rowIndex;
1043 @ return 1;
1044 @ }
1045 }
1046 if( strchr(zColumnTypes,'T') ){
1047 @ this.sortReverseText = function(a,b) {
1048 @ var i = thisObject.sortIndex;
1049 @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1050 @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1051
--- src/report.c
+++ src/report.c
@@ -955,11 +955,11 @@
955 ** k Sort by the data-sortkey property
956 ** x This column is not sortable
957 **
958 ** Capital letters mean sort in reverse order.
959 ** If there are fewer characters in zColumnTypes[] than their are columns,
960 ** then all extra columns assume type "t" (text).
961 **
962 ** The third parameter is the column that was initially sorted (using 1-based
963 ** column numbers, like SQL). Make this value 0 if none of the columns are
964 ** initially sorted. Make the value negative if the column is initially sorted
965 ** in reverse order.
@@ -973,10 +973,12 @@
973 ){
974 @ <script>
975 @ function SortableTable(tableEl,columnTypes,initSort){
976 @ this.tbody = tableEl.getElementsByTagName('tbody');
977 @ this.columnTypes = columnTypes;
978 @ var ncols = tableEl.rows[0].cells.length;
979 @ for(var i = columnTypes.length; i<=ncols; i++){this.columnTypes += 't';}
980 @ this.sort = function (cell) {
981 @ var column = cell.cellIndex;
982 @ var sortFn;
983 @ switch( cell.sortType ){
984 if( strchr(zColumnTypes,'n') ){
@@ -983,13 +985,11 @@
985 @ case "n": sortFn = this.sortNumeric; break;
986 }
987 if( strchr(zColumnTypes,'N') ){
988 @ case "N": sortFn = this.sortReverseNumeric; break;
989 }
990 @ case "t": sortFn = this.sortText; break;
 
 
991 if( strchr(zColumnTypes,'T') ){
992 @ case "T": sortFn = this.sortReverseText; break;
993 }
994 if( strchr(zColumnTypes,'k') ){
995 @ case "k": sortFn = this.sortKey; break;
@@ -1031,20 +1031,18 @@
1031 @ var clsName = hdrCell.className.replace(/\s*\bsort\s*\w+/, '');
1032 @ clsName += ' sort ' + sortType;
1033 @ hdrCell.className = clsName;
1034 @ }
1035 @ }
1036 @ this.sortText = function(a,b) {
1037 @ var i = thisObject.sortIndex;
1038 @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1039 @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1040 @ if(aa<bb) return -1;
1041 @ if(aa==bb) return a.rowIndex-b.rowIndex;
1042 @ return 1;
1043 @ }
 
 
1044 if( strchr(zColumnTypes,'T') ){
1045 @ this.sortReverseText = function(a,b) {
1046 @ var i = thisObject.sortIndex;
1047 @ aa = a.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1048 @ bb = b.cells[i].textContent.replace(/^\W+/,'').toLowerCase();
1049
--- src/setup.c
+++ src/setup.c
@@ -1882,10 +1882,11 @@
18821882
login_check_credentials();
18831883
if( !g.perm.Setup ){
18841884
login_needed(0);
18851885
return;
18861886
}
1887
+ add_content_sql_commands(g.db);
18871888
db_begin_transaction();
18881889
style_header("Raw SQL Commands");
18891890
@ <p><b>Caution:</b> There are no restrictions on the SQL that can be
18901891
@ run by this page. You can do serious and irrepairable damage to the
18911892
@ repository. Proceed with extreme caution.</p>
18921893
--- src/setup.c
+++ src/setup.c
@@ -1882,10 +1882,11 @@
1882 login_check_credentials();
1883 if( !g.perm.Setup ){
1884 login_needed(0);
1885 return;
1886 }
 
1887 db_begin_transaction();
1888 style_header("Raw SQL Commands");
1889 @ <p><b>Caution:</b> There are no restrictions on the SQL that can be
1890 @ run by this page. You can do serious and irrepairable damage to the
1891 @ repository. Proceed with extreme caution.</p>
1892
--- src/setup.c
+++ src/setup.c
@@ -1882,10 +1882,11 @@
1882 login_check_credentials();
1883 if( !g.perm.Setup ){
1884 login_needed(0);
1885 return;
1886 }
1887 add_content_sql_commands(g.db);
1888 db_begin_transaction();
1889 style_header("Raw SQL Commands");
1890 @ <p><b>Caution:</b> There are no restrictions on the SQL that can be
1891 @ run by this page. You can do serious and irrepairable damage to the
1892 @ repository. Proceed with extreme caution.</p>
1893
+93 -57
--- src/shell.c
+++ src/shell.c
@@ -590,10 +590,11 @@
590590
*/
591591
typedef struct ShellState ShellState;
592592
struct ShellState {
593593
sqlite3 *db; /* The database */
594594
int echoOn; /* True to echo input commands */
595
+ int autoExplain; /* Automatically turn on .explain mode */
595596
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
596597
int statsOn; /* True to display memory stats before each finalize */
597598
int scanstatsOn; /* True to display scan stats before each finalize */
598599
int countChanges; /* True to display change counts */
599600
int backslashOn; /* Resolve C-style \x escapes in SQL input text */
@@ -601,10 +602,12 @@
601602
int cnt; /* Number of records displayed so far */
602603
FILE *out; /* Write results here */
603604
FILE *traceOut; /* Output for sqlite3_trace() */
604605
int nErr; /* Number of errors seen */
605606
int mode; /* An output mode setting */
607
+ int cMode; /* temporary output mode for the current query */
608
+ int normalMode; /* Output mode before ".explain on" */
606609
int writableSchema; /* True if PRAGMA writable_schema=ON */
607610
int showHeader; /* True to show column names in List or Column mode */
608611
unsigned shellFlgs; /* Various flags */
609612
char *zDestTable; /* Name of destination table when MODE_Insert */
610613
char colSeparator[20]; /* Column separator character for several modes */
@@ -611,11 +614,10 @@
611614
char rowSeparator[20]; /* Row separator character for MODE_Ascii */
612615
int colWidth[100]; /* Requested width of each column when in column mode*/
613616
int actualWidth[100]; /* Actual width of each column */
614617
char nullValue[20]; /* The text to print when a NULL comes back from
615618
** the database */
616
- SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
617619
char outfile[FILENAME_MAX]; /* Filename for *out */
618620
const char *zDbFilename; /* name of the database file */
619621
char *zFreeOnClose; /* Filename to free when closing */
620622
const char *zVfs; /* Name of VFS to use */
621623
sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -880,11 +882,11 @@
880882
int *aiType /* Column types */
881883
){
882884
int i;
883885
ShellState *p = (ShellState*)pArg;
884886
885
- switch( p->mode ){
887
+ switch( p->cMode ){
886888
case MODE_Line: {
887889
int w = 5;
888890
if( azArg==0 ) break;
889891
for(i=0; i<nArg; i++){
890892
int len = strlen30(azCol[i] ? azCol[i] : "");
@@ -897,15 +899,28 @@
897899
}
898900
break;
899901
}
900902
case MODE_Explain:
901903
case MODE_Column: {
904
+ static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
905
+ const int *colWidth;
906
+ int showHdr;
907
+ char *rowSep;
908
+ if( p->cMode==MODE_Column ){
909
+ colWidth = p->colWidth;
910
+ showHdr = p->showHeader;
911
+ rowSep = p->rowSeparator;
912
+ }else{
913
+ colWidth = aExplainWidths;
914
+ showHdr = 1;
915
+ rowSep = SEP_Row;
916
+ }
902917
if( p->cnt++==0 ){
903918
for(i=0; i<nArg; i++){
904919
int w, n;
905920
if( i<ArraySize(p->colWidth) ){
906
- w = p->colWidth[i];
921
+ w = colWidth[i];
907922
}else{
908923
w = 0;
909924
}
910925
if( w==0 ){
911926
w = strlen30(azCol[i] ? azCol[i] : "");
@@ -914,21 +929,21 @@
914929
if( w<n ) w = n;
915930
}
916931
if( i<ArraySize(p->actualWidth) ){
917932
p->actualWidth[i] = w;
918933
}
919
- if( p->showHeader ){
934
+ if( showHdr ){
920935
if( w<0 ){
921936
utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i],
922
- i==nArg-1 ? p->rowSeparator : " ");
937
+ i==nArg-1 ? rowSep : " ");
923938
}else{
924939
utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i],
925
- i==nArg-1 ? p->rowSeparator : " ");
940
+ i==nArg-1 ? rowSep : " ");
926941
}
927942
}
928943
}
929
- if( p->showHeader ){
944
+ if( showHdr ){
930945
for(i=0; i<nArg; i++){
931946
int w;
932947
if( i<ArraySize(p->actualWidth) ){
933948
w = p->actualWidth[i];
934949
if( w<0 ) w = -w;
@@ -936,11 +951,11 @@
936951
w = 10;
937952
}
938953
utf8_printf(p->out,"%-*.*s%s",w,w,
939954
"----------------------------------------------------------"
940955
"----------------------------------------------------------",
941
- i==nArg-1 ? p->rowSeparator : " ");
956
+ i==nArg-1 ? rowSep : " ");
942957
}
943958
}
944959
}
945960
if( azArg==0 ) break;
946961
for(i=0; i<nArg; i++){
@@ -948,11 +963,11 @@
948963
if( i<ArraySize(p->actualWidth) ){
949964
w = p->actualWidth[i];
950965
}else{
951966
w = 10;
952967
}
953
- if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
968
+ if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
954969
w = strlen30(azArg[i]);
955970
}
956971
if( i==1 && p->aiIndent && p->pStmt ){
957972
if( p->iIndent<p->nIndent ){
958973
utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
@@ -960,15 +975,15 @@
960975
p->iIndent++;
961976
}
962977
if( w<0 ){
963978
utf8_printf(p->out,"%*.*s%s",-w,-w,
964979
azArg[i] ? azArg[i] : p->nullValue,
965
- i==nArg-1 ? p->rowSeparator : " ");
980
+ i==nArg-1 ? rowSep : " ");
966981
}else{
967982
utf8_printf(p->out,"%-*.*s%s",w,w,
968983
azArg[i] ? azArg[i] : p->nullValue,
969
- i==nArg-1 ? p->rowSeparator : " ");
984
+ i==nArg-1 ? rowSep : " ");
970985
}
971986
}
972987
break;
973988
}
974989
case MODE_Semi:
@@ -984,11 +999,11 @@
984999
char *z = azArg[i];
9851000
if( z==0 ) z = p->nullValue;
9861001
utf8_printf(p->out, "%s", z);
9871002
if( i<nArg-1 ){
9881003
utf8_printf(p->out, "%s", p->colSeparator);
989
- }else if( p->mode==MODE_Semi ){
1004
+ }else if( p->cMode==MODE_Semi ){
9901005
utf8_printf(p->out, ";%s", p->rowSeparator);
9911006
}else{
9921007
utf8_printf(p->out, "%s", p->rowSeparator);
9931008
}
9941009
}
@@ -1489,14 +1504,21 @@
14891504
"Rewind", 0 };
14901505
const char *azGoto[] = { "Goto", 0 };
14911506
14921507
/* Try to figure out if this is really an EXPLAIN statement. If this
14931508
** cannot be verified, return early. */
1509
+ if( sqlite3_column_count(pSql)!=8 ){
1510
+ p->cMode = p->mode;
1511
+ return;
1512
+ }
14941513
zSql = sqlite3_sql(pSql);
14951514
if( zSql==0 ) return;
14961515
for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1497
- if( sqlite3_strnicmp(z, "explain", 7) ) return;
1516
+ if( sqlite3_strnicmp(z, "explain", 7) ){
1517
+ p->cMode = p->mode;
1518
+ return;
1519
+ }
14981520
14991521
for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
15001522
int i;
15011523
int iAddr = sqlite3_column_int(pSql, 0);
15021524
const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
@@ -1509,10 +1531,24 @@
15091531
int p2 = sqlite3_column_int(pSql, 3);
15101532
int p2op = (p2 + (iOp-iAddr));
15111533
15121534
/* Grow the p->aiIndent array as required */
15131535
if( iOp>=nAlloc ){
1536
+ if( iOp==0 ){
1537
+ /* Do further verfication that this is explain output. Abort if
1538
+ ** it is not */
1539
+ static const char *explainCols[] = {
1540
+ "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
1541
+ int jj;
1542
+ for(jj=0; jj<ArraySize(explainCols); jj++){
1543
+ if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
1544
+ p->cMode = p->mode;
1545
+ sqlite3_reset(pSql);
1546
+ return;
1547
+ }
1548
+ }
1549
+ }
15141550
nAlloc += 100;
15151551
p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
15161552
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
15171553
}
15181554
abYield[iOp] = str_in_array(zOp, azYield);
@@ -1612,14 +1648,24 @@
16121648
}
16131649
sqlite3_finalize(pExplain);
16141650
sqlite3_free(zEQP);
16151651
}
16161652
1617
- /* If the shell is currently in ".explain" mode, gather the extra
1618
- ** data required to add indents to the output.*/
1619
- if( pArg && pArg->mode==MODE_Explain ){
1620
- explain_data_prepare(pArg, pStmt);
1653
+ if( pArg ){
1654
+ pArg->cMode = pArg->mode;
1655
+ if( pArg->autoExplain
1656
+ && sqlite3_column_count(pStmt)==8
1657
+ && sqlite3_strlike("%EXPLAIN%", sqlite3_sql(pStmt),0)==0
1658
+ ){
1659
+ pArg->cMode = MODE_Explain;
1660
+ }
1661
+
1662
+ /* If the shell is currently in ".explain" mode, gather the extra
1663
+ ** data required to add indents to the output.*/
1664
+ if( pArg->cMode==MODE_Explain ){
1665
+ explain_data_prepare(pArg, pStmt);
1666
+ }
16211667
}
16221668
16231669
/* perform the first step. this will tell us if we
16241670
** have a result set or not and how wide it is.
16251671
*/
@@ -1645,11 +1691,11 @@
16451691
}
16461692
do{
16471693
/* extract the data and data types */
16481694
for(i=0; i<nCol; i++){
16491695
aiTypes[i] = x = sqlite3_column_type(pStmt, i);
1650
- if( x==SQLITE_BLOB && pArg && pArg->mode==MODE_Insert ){
1696
+ if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
16511697
azVals[i] = "";
16521698
}else{
16531699
azVals[i] = (char*)sqlite3_column_text(pStmt, i);
16541700
}
16551701
if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
@@ -1865,12 +1911,11 @@
18651911
" If TABLE specified, only dump tables matching\n"
18661912
" LIKE pattern TABLE.\n"
18671913
".echo on|off Turn command echo on or off\n"
18681914
".eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN\n"
18691915
".exit Exit this program\n"
1870
- ".explain ?on|off? Turn output mode suitable for EXPLAIN on or off.\n"
1871
- " With no args, it turns EXPLAIN on.\n"
1916
+ ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"
18721917
".fullschema Show schema and the content of sqlite_stat tables\n"
18731918
".headers on|off Turn display of headers on or off\n"
18741919
".help Show this message\n"
18751920
".import FILE TABLE Import data from FILE into TABLE\n"
18761921
".indexes ?TABLE? Show names of all indexes\n"
@@ -2853,11 +2898,11 @@
28532898
ShellState data;
28542899
char *zErrMsg = 0;
28552900
open_db(p, 0);
28562901
memcpy(&data, p, sizeof(data));
28572902
data.showHeader = 1;
2858
- data.mode = MODE_Column;
2903
+ data.cMode = data.mode = MODE_Column;
28592904
data.colWidth[0] = 3;
28602905
data.colWidth[1] = 15;
28612906
data.colWidth[2] = 58;
28622907
data.cnt = 0;
28632908
sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
@@ -2948,41 +2993,28 @@
29482993
if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
29492994
rc = 2;
29502995
}else
29512996
29522997
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2953
- int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2954
- if(val == 1) {
2955
- if(!p->normalMode.valid) {
2956
- p->normalMode.valid = 1;
2957
- p->normalMode.mode = p->mode;
2958
- p->normalMode.showHeader = p->showHeader;
2959
- memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
2960
- }
2961
- /* We could put this code under the !p->explainValid
2962
- ** condition so that it does not execute if we are already in
2963
- ** explain mode. However, always executing it allows us an easy
2964
- ** was to reset to explain mode in case the user previously
2965
- ** did an .explain followed by a .width, .mode or .header
2966
- ** command.
2967
- */
2998
+ int val = 1;
2999
+ if( nArg>=2 ){
3000
+ if( strcmp(azArg[1],"auto")==0 ){
3001
+ val = 99;
3002
+ }else{
3003
+ val = booleanValue(azArg[1]);
3004
+ }
3005
+ }
3006
+ if( val==1 && p->mode!=MODE_Explain ){
3007
+ p->normalMode = p->mode;
29683008
p->mode = MODE_Explain;
2969
- p->showHeader = 1;
2970
- memset(p->colWidth,0,sizeof(p->colWidth));
2971
- p->colWidth[0] = 4; /* addr */
2972
- p->colWidth[1] = 13; /* opcode */
2973
- p->colWidth[2] = 4; /* P1 */
2974
- p->colWidth[3] = 4; /* P2 */
2975
- p->colWidth[4] = 4; /* P3 */
2976
- p->colWidth[5] = 13; /* P4 */
2977
- p->colWidth[6] = 2; /* P5 */
2978
- p->colWidth[7] = 13; /* Comment */
2979
- }else if (p->normalMode.valid) {
2980
- p->normalMode.valid = 0;
2981
- p->mode = p->normalMode.mode;
2982
- p->showHeader = p->normalMode.showHeader;
2983
- memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
3009
+ p->autoExplain = 0;
3010
+ }else if( val==0 ){
3011
+ if( p->mode==MODE_Explain ) p->mode = p->normalMode;
3012
+ p->autoExplain = 0;
3013
+ }else if( val==99 ){
3014
+ if( p->mode==MODE_Explain ) p->mode = p->normalMode;
3015
+ p->autoExplain = 1;
29843016
}
29853017
}else
29863018
29873019
if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
29883020
ShellState data;
@@ -2994,11 +3026,11 @@
29943026
goto meta_command_exit;
29953027
}
29963028
open_db(p, 0);
29973029
memcpy(&data, p, sizeof(data));
29983030
data.showHeader = 0;
2999
- data.mode = MODE_Semi;
3031
+ data.cMode = data.mode = MODE_Semi;
30003032
rc = sqlite3_exec(p->db,
30013033
"SELECT sql FROM"
30023034
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
30033035
" FROM sqlite_master UNION ALL"
30043036
" SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
@@ -3019,11 +3051,11 @@
30193051
raw_printf(p->out, "/* No STAT tables available */\n");
30203052
}else{
30213053
raw_printf(p->out, "ANALYZE sqlite_master;\n");
30223054
sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
30233055
callback, &data, &zErrMsg);
3024
- data.mode = MODE_Insert;
3056
+ data.cMode = data.mode = MODE_Insert;
30253057
data.zDestTable = "sqlite_stat1";
30263058
shell_exec(p->db, "SELECT * FROM sqlite_stat1",
30273059
shell_callback, &data,&zErrMsg);
30283060
data.zDestTable = "sqlite_stat3";
30293061
shell_exec(p->db, "SELECT * FROM sqlite_stat3",
@@ -3251,11 +3283,11 @@
32513283
ShellState data;
32523284
char *zErrMsg = 0;
32533285
open_db(p, 0);
32543286
memcpy(&data, p, sizeof(data));
32553287
data.showHeader = 0;
3256
- data.mode = MODE_List;
3288
+ data.cMode = data.mode = MODE_List;
32573289
if( nArg==1 ){
32583290
rc = sqlite3_exec(p->db,
32593291
"SELECT name FROM sqlite_master "
32603292
"WHERE type='index' AND name NOT LIKE 'sqlite_%' "
32613293
"UNION ALL "
@@ -3437,10 +3469,11 @@
34373469
}else {
34383470
raw_printf(stderr, "Error: mode should be one of: "
34393471
"ascii column csv html insert line list tabs tcl\n");
34403472
rc = 1;
34413473
}
3474
+ p->cMode = p->mode;
34423475
}else
34433476
34443477
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
34453478
if( nArg==2 ){
34463479
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
@@ -3626,11 +3659,11 @@
36263659
ShellState data;
36273660
char *zErrMsg = 0;
36283661
open_db(p, 0);
36293662
memcpy(&data, p, sizeof(data));
36303663
data.showHeader = 0;
3631
- data.mode = MODE_Semi;
3664
+ data.cMode = data.mode = MODE_Semi;
36323665
if( nArg==2 ){
36333666
int i;
36343667
for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
36353668
if( strcmp(azArg[1],"sqlite_master")==0 ){
36363669
char *new_argv[2], *new_colv[2];
@@ -3774,11 +3807,12 @@
37743807
rc = 1;
37753808
goto meta_command_exit;
37763809
}
37773810
utf8_printf(p->out, "%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
37783811
utf8_printf(p->out, "%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
3779
- utf8_printf(p->out,"%9.9s: %s\n","explain",p->normalMode.valid?"on":"off");
3812
+ utf8_printf(p->out, "%12.12s: %s\n","explain",
3813
+ p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
37803814
utf8_printf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
37813815
utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
37823816
utf8_printf(p->out, "%12.12s: ", "nullvalue");
37833817
output_c_string(p->out, p->nullValue);
37843818
raw_printf(p->out, "\n");
@@ -4573,11 +4607,12 @@
45734607
/*
45744608
** Initialize the state information in data
45754609
*/
45764610
static void main_init(ShellState *data) {
45774611
memset(data, 0, sizeof(*data));
4578
- data->mode = MODE_List;
4612
+ data->normalMode = data->cMode = data->mode = MODE_List;
4613
+ data->autoExplain = 1;
45794614
memcpy(data->colSeparator,SEP_Column, 2);
45804615
memcpy(data->rowSeparator,SEP_Row, 2);
45814616
data->showHeader = 0;
45824617
data->shellFlgs = SHFLG_Lookaside;
45834618
sqlite3_config(SQLITE_CONFIG_URI, 1);
@@ -4906,10 +4941,11 @@
49064941
}else{
49074942
utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
49084943
raw_printf(stderr,"Use -help for a list of options.\n");
49094944
return 1;
49104945
}
4946
+ data.cMode = data.mode;
49114947
}
49124948
49134949
if( !readStdin ){
49144950
/* Run all arguments that do not begin with '-' as if they were separate
49154951
** command-line inputs, except for the argToSkip argument which contains
49164952
--- src/shell.c
+++ src/shell.c
@@ -590,10 +590,11 @@
590 */
591 typedef struct ShellState ShellState;
592 struct ShellState {
593 sqlite3 *db; /* The database */
594 int echoOn; /* True to echo input commands */
 
595 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
596 int statsOn; /* True to display memory stats before each finalize */
597 int scanstatsOn; /* True to display scan stats before each finalize */
598 int countChanges; /* True to display change counts */
599 int backslashOn; /* Resolve C-style \x escapes in SQL input text */
@@ -601,10 +602,12 @@
601 int cnt; /* Number of records displayed so far */
602 FILE *out; /* Write results here */
603 FILE *traceOut; /* Output for sqlite3_trace() */
604 int nErr; /* Number of errors seen */
605 int mode; /* An output mode setting */
 
 
606 int writableSchema; /* True if PRAGMA writable_schema=ON */
607 int showHeader; /* True to show column names in List or Column mode */
608 unsigned shellFlgs; /* Various flags */
609 char *zDestTable; /* Name of destination table when MODE_Insert */
610 char colSeparator[20]; /* Column separator character for several modes */
@@ -611,11 +614,10 @@
611 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
612 int colWidth[100]; /* Requested width of each column when in column mode*/
613 int actualWidth[100]; /* Actual width of each column */
614 char nullValue[20]; /* The text to print when a NULL comes back from
615 ** the database */
616 SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
617 char outfile[FILENAME_MAX]; /* Filename for *out */
618 const char *zDbFilename; /* name of the database file */
619 char *zFreeOnClose; /* Filename to free when closing */
620 const char *zVfs; /* Name of VFS to use */
621 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -880,11 +882,11 @@
880 int *aiType /* Column types */
881 ){
882 int i;
883 ShellState *p = (ShellState*)pArg;
884
885 switch( p->mode ){
886 case MODE_Line: {
887 int w = 5;
888 if( azArg==0 ) break;
889 for(i=0; i<nArg; i++){
890 int len = strlen30(azCol[i] ? azCol[i] : "");
@@ -897,15 +899,28 @@
897 }
898 break;
899 }
900 case MODE_Explain:
901 case MODE_Column: {
 
 
 
 
 
 
 
 
 
 
 
 
 
902 if( p->cnt++==0 ){
903 for(i=0; i<nArg; i++){
904 int w, n;
905 if( i<ArraySize(p->colWidth) ){
906 w = p->colWidth[i];
907 }else{
908 w = 0;
909 }
910 if( w==0 ){
911 w = strlen30(azCol[i] ? azCol[i] : "");
@@ -914,21 +929,21 @@
914 if( w<n ) w = n;
915 }
916 if( i<ArraySize(p->actualWidth) ){
917 p->actualWidth[i] = w;
918 }
919 if( p->showHeader ){
920 if( w<0 ){
921 utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i],
922 i==nArg-1 ? p->rowSeparator : " ");
923 }else{
924 utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i],
925 i==nArg-1 ? p->rowSeparator : " ");
926 }
927 }
928 }
929 if( p->showHeader ){
930 for(i=0; i<nArg; i++){
931 int w;
932 if( i<ArraySize(p->actualWidth) ){
933 w = p->actualWidth[i];
934 if( w<0 ) w = -w;
@@ -936,11 +951,11 @@
936 w = 10;
937 }
938 utf8_printf(p->out,"%-*.*s%s",w,w,
939 "----------------------------------------------------------"
940 "----------------------------------------------------------",
941 i==nArg-1 ? p->rowSeparator : " ");
942 }
943 }
944 }
945 if( azArg==0 ) break;
946 for(i=0; i<nArg; i++){
@@ -948,11 +963,11 @@
948 if( i<ArraySize(p->actualWidth) ){
949 w = p->actualWidth[i];
950 }else{
951 w = 10;
952 }
953 if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
954 w = strlen30(azArg[i]);
955 }
956 if( i==1 && p->aiIndent && p->pStmt ){
957 if( p->iIndent<p->nIndent ){
958 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
@@ -960,15 +975,15 @@
960 p->iIndent++;
961 }
962 if( w<0 ){
963 utf8_printf(p->out,"%*.*s%s",-w,-w,
964 azArg[i] ? azArg[i] : p->nullValue,
965 i==nArg-1 ? p->rowSeparator : " ");
966 }else{
967 utf8_printf(p->out,"%-*.*s%s",w,w,
968 azArg[i] ? azArg[i] : p->nullValue,
969 i==nArg-1 ? p->rowSeparator : " ");
970 }
971 }
972 break;
973 }
974 case MODE_Semi:
@@ -984,11 +999,11 @@
984 char *z = azArg[i];
985 if( z==0 ) z = p->nullValue;
986 utf8_printf(p->out, "%s", z);
987 if( i<nArg-1 ){
988 utf8_printf(p->out, "%s", p->colSeparator);
989 }else if( p->mode==MODE_Semi ){
990 utf8_printf(p->out, ";%s", p->rowSeparator);
991 }else{
992 utf8_printf(p->out, "%s", p->rowSeparator);
993 }
994 }
@@ -1489,14 +1504,21 @@
1489 "Rewind", 0 };
1490 const char *azGoto[] = { "Goto", 0 };
1491
1492 /* Try to figure out if this is really an EXPLAIN statement. If this
1493 ** cannot be verified, return early. */
 
 
 
 
1494 zSql = sqlite3_sql(pSql);
1495 if( zSql==0 ) return;
1496 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1497 if( sqlite3_strnicmp(z, "explain", 7) ) return;
 
 
 
1498
1499 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
1500 int i;
1501 int iAddr = sqlite3_column_int(pSql, 0);
1502 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
@@ -1509,10 +1531,24 @@
1509 int p2 = sqlite3_column_int(pSql, 3);
1510 int p2op = (p2 + (iOp-iAddr));
1511
1512 /* Grow the p->aiIndent array as required */
1513 if( iOp>=nAlloc ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1514 nAlloc += 100;
1515 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
1516 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
1517 }
1518 abYield[iOp] = str_in_array(zOp, azYield);
@@ -1612,14 +1648,24 @@
1612 }
1613 sqlite3_finalize(pExplain);
1614 sqlite3_free(zEQP);
1615 }
1616
1617 /* If the shell is currently in ".explain" mode, gather the extra
1618 ** data required to add indents to the output.*/
1619 if( pArg && pArg->mode==MODE_Explain ){
1620 explain_data_prepare(pArg, pStmt);
 
 
 
 
 
 
 
 
 
 
1621 }
1622
1623 /* perform the first step. this will tell us if we
1624 ** have a result set or not and how wide it is.
1625 */
@@ -1645,11 +1691,11 @@
1645 }
1646 do{
1647 /* extract the data and data types */
1648 for(i=0; i<nCol; i++){
1649 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
1650 if( x==SQLITE_BLOB && pArg && pArg->mode==MODE_Insert ){
1651 azVals[i] = "";
1652 }else{
1653 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
1654 }
1655 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
@@ -1865,12 +1911,11 @@
1865 " If TABLE specified, only dump tables matching\n"
1866 " LIKE pattern TABLE.\n"
1867 ".echo on|off Turn command echo on or off\n"
1868 ".eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN\n"
1869 ".exit Exit this program\n"
1870 ".explain ?on|off? Turn output mode suitable for EXPLAIN on or off.\n"
1871 " With no args, it turns EXPLAIN on.\n"
1872 ".fullschema Show schema and the content of sqlite_stat tables\n"
1873 ".headers on|off Turn display of headers on or off\n"
1874 ".help Show this message\n"
1875 ".import FILE TABLE Import data from FILE into TABLE\n"
1876 ".indexes ?TABLE? Show names of all indexes\n"
@@ -2853,11 +2898,11 @@
2853 ShellState data;
2854 char *zErrMsg = 0;
2855 open_db(p, 0);
2856 memcpy(&data, p, sizeof(data));
2857 data.showHeader = 1;
2858 data.mode = MODE_Column;
2859 data.colWidth[0] = 3;
2860 data.colWidth[1] = 15;
2861 data.colWidth[2] = 58;
2862 data.cnt = 0;
2863 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
@@ -2948,41 +2993,28 @@
2948 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
2949 rc = 2;
2950 }else
2951
2952 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2953 int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
2954 if(val == 1) {
2955 if(!p->normalMode.valid) {
2956 p->normalMode.valid = 1;
2957 p->normalMode.mode = p->mode;
2958 p->normalMode.showHeader = p->showHeader;
2959 memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
2960 }
2961 /* We could put this code under the !p->explainValid
2962 ** condition so that it does not execute if we are already in
2963 ** explain mode. However, always executing it allows us an easy
2964 ** was to reset to explain mode in case the user previously
2965 ** did an .explain followed by a .width, .mode or .header
2966 ** command.
2967 */
2968 p->mode = MODE_Explain;
2969 p->showHeader = 1;
2970 memset(p->colWidth,0,sizeof(p->colWidth));
2971 p->colWidth[0] = 4; /* addr */
2972 p->colWidth[1] = 13; /* opcode */
2973 p->colWidth[2] = 4; /* P1 */
2974 p->colWidth[3] = 4; /* P2 */
2975 p->colWidth[4] = 4; /* P3 */
2976 p->colWidth[5] = 13; /* P4 */
2977 p->colWidth[6] = 2; /* P5 */
2978 p->colWidth[7] = 13; /* Comment */
2979 }else if (p->normalMode.valid) {
2980 p->normalMode.valid = 0;
2981 p->mode = p->normalMode.mode;
2982 p->showHeader = p->normalMode.showHeader;
2983 memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
2984 }
2985 }else
2986
2987 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
2988 ShellState data;
@@ -2994,11 +3026,11 @@
2994 goto meta_command_exit;
2995 }
2996 open_db(p, 0);
2997 memcpy(&data, p, sizeof(data));
2998 data.showHeader = 0;
2999 data.mode = MODE_Semi;
3000 rc = sqlite3_exec(p->db,
3001 "SELECT sql FROM"
3002 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3003 " FROM sqlite_master UNION ALL"
3004 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
@@ -3019,11 +3051,11 @@
3019 raw_printf(p->out, "/* No STAT tables available */\n");
3020 }else{
3021 raw_printf(p->out, "ANALYZE sqlite_master;\n");
3022 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
3023 callback, &data, &zErrMsg);
3024 data.mode = MODE_Insert;
3025 data.zDestTable = "sqlite_stat1";
3026 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
3027 shell_callback, &data,&zErrMsg);
3028 data.zDestTable = "sqlite_stat3";
3029 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
@@ -3251,11 +3283,11 @@
3251 ShellState data;
3252 char *zErrMsg = 0;
3253 open_db(p, 0);
3254 memcpy(&data, p, sizeof(data));
3255 data.showHeader = 0;
3256 data.mode = MODE_List;
3257 if( nArg==1 ){
3258 rc = sqlite3_exec(p->db,
3259 "SELECT name FROM sqlite_master "
3260 "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
3261 "UNION ALL "
@@ -3437,10 +3469,11 @@
3437 }else {
3438 raw_printf(stderr, "Error: mode should be one of: "
3439 "ascii column csv html insert line list tabs tcl\n");
3440 rc = 1;
3441 }
 
3442 }else
3443
3444 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
3445 if( nArg==2 ){
3446 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
@@ -3626,11 +3659,11 @@
3626 ShellState data;
3627 char *zErrMsg = 0;
3628 open_db(p, 0);
3629 memcpy(&data, p, sizeof(data));
3630 data.showHeader = 0;
3631 data.mode = MODE_Semi;
3632 if( nArg==2 ){
3633 int i;
3634 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
3635 if( strcmp(azArg[1],"sqlite_master")==0 ){
3636 char *new_argv[2], *new_colv[2];
@@ -3774,11 +3807,12 @@
3774 rc = 1;
3775 goto meta_command_exit;
3776 }
3777 utf8_printf(p->out, "%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
3778 utf8_printf(p->out, "%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
3779 utf8_printf(p->out,"%9.9s: %s\n","explain",p->normalMode.valid?"on":"off");
 
3780 utf8_printf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
3781 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
3782 utf8_printf(p->out, "%12.12s: ", "nullvalue");
3783 output_c_string(p->out, p->nullValue);
3784 raw_printf(p->out, "\n");
@@ -4573,11 +4607,12 @@
4573 /*
4574 ** Initialize the state information in data
4575 */
4576 static void main_init(ShellState *data) {
4577 memset(data, 0, sizeof(*data));
4578 data->mode = MODE_List;
 
4579 memcpy(data->colSeparator,SEP_Column, 2);
4580 memcpy(data->rowSeparator,SEP_Row, 2);
4581 data->showHeader = 0;
4582 data->shellFlgs = SHFLG_Lookaside;
4583 sqlite3_config(SQLITE_CONFIG_URI, 1);
@@ -4906,10 +4941,11 @@
4906 }else{
4907 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
4908 raw_printf(stderr,"Use -help for a list of options.\n");
4909 return 1;
4910 }
 
4911 }
4912
4913 if( !readStdin ){
4914 /* Run all arguments that do not begin with '-' as if they were separate
4915 ** command-line inputs, except for the argToSkip argument which contains
4916
--- src/shell.c
+++ src/shell.c
@@ -590,10 +590,11 @@
590 */
591 typedef struct ShellState ShellState;
592 struct ShellState {
593 sqlite3 *db; /* The database */
594 int echoOn; /* True to echo input commands */
595 int autoExplain; /* Automatically turn on .explain mode */
596 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
597 int statsOn; /* True to display memory stats before each finalize */
598 int scanstatsOn; /* True to display scan stats before each finalize */
599 int countChanges; /* True to display change counts */
600 int backslashOn; /* Resolve C-style \x escapes in SQL input text */
@@ -601,10 +602,12 @@
602 int cnt; /* Number of records displayed so far */
603 FILE *out; /* Write results here */
604 FILE *traceOut; /* Output for sqlite3_trace() */
605 int nErr; /* Number of errors seen */
606 int mode; /* An output mode setting */
607 int cMode; /* temporary output mode for the current query */
608 int normalMode; /* Output mode before ".explain on" */
609 int writableSchema; /* True if PRAGMA writable_schema=ON */
610 int showHeader; /* True to show column names in List or Column mode */
611 unsigned shellFlgs; /* Various flags */
612 char *zDestTable; /* Name of destination table when MODE_Insert */
613 char colSeparator[20]; /* Column separator character for several modes */
@@ -611,11 +614,10 @@
614 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
615 int colWidth[100]; /* Requested width of each column when in column mode*/
616 int actualWidth[100]; /* Actual width of each column */
617 char nullValue[20]; /* The text to print when a NULL comes back from
618 ** the database */
 
619 char outfile[FILENAME_MAX]; /* Filename for *out */
620 const char *zDbFilename; /* name of the database file */
621 char *zFreeOnClose; /* Filename to free when closing */
622 const char *zVfs; /* Name of VFS to use */
623 sqlite3_stmt *pStmt; /* Current statement if any. */
@@ -880,11 +882,11 @@
882 int *aiType /* Column types */
883 ){
884 int i;
885 ShellState *p = (ShellState*)pArg;
886
887 switch( p->cMode ){
888 case MODE_Line: {
889 int w = 5;
890 if( azArg==0 ) break;
891 for(i=0; i<nArg; i++){
892 int len = strlen30(azCol[i] ? azCol[i] : "");
@@ -897,15 +899,28 @@
899 }
900 break;
901 }
902 case MODE_Explain:
903 case MODE_Column: {
904 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
905 const int *colWidth;
906 int showHdr;
907 char *rowSep;
908 if( p->cMode==MODE_Column ){
909 colWidth = p->colWidth;
910 showHdr = p->showHeader;
911 rowSep = p->rowSeparator;
912 }else{
913 colWidth = aExplainWidths;
914 showHdr = 1;
915 rowSep = SEP_Row;
916 }
917 if( p->cnt++==0 ){
918 for(i=0; i<nArg; i++){
919 int w, n;
920 if( i<ArraySize(p->colWidth) ){
921 w = colWidth[i];
922 }else{
923 w = 0;
924 }
925 if( w==0 ){
926 w = strlen30(azCol[i] ? azCol[i] : "");
@@ -914,21 +929,21 @@
929 if( w<n ) w = n;
930 }
931 if( i<ArraySize(p->actualWidth) ){
932 p->actualWidth[i] = w;
933 }
934 if( showHdr ){
935 if( w<0 ){
936 utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i],
937 i==nArg-1 ? rowSep : " ");
938 }else{
939 utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i],
940 i==nArg-1 ? rowSep : " ");
941 }
942 }
943 }
944 if( showHdr ){
945 for(i=0; i<nArg; i++){
946 int w;
947 if( i<ArraySize(p->actualWidth) ){
948 w = p->actualWidth[i];
949 if( w<0 ) w = -w;
@@ -936,11 +951,11 @@
951 w = 10;
952 }
953 utf8_printf(p->out,"%-*.*s%s",w,w,
954 "----------------------------------------------------------"
955 "----------------------------------------------------------",
956 i==nArg-1 ? rowSep : " ");
957 }
958 }
959 }
960 if( azArg==0 ) break;
961 for(i=0; i<nArg; i++){
@@ -948,11 +963,11 @@
963 if( i<ArraySize(p->actualWidth) ){
964 w = p->actualWidth[i];
965 }else{
966 w = 10;
967 }
968 if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
969 w = strlen30(azArg[i]);
970 }
971 if( i==1 && p->aiIndent && p->pStmt ){
972 if( p->iIndent<p->nIndent ){
973 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
@@ -960,15 +975,15 @@
975 p->iIndent++;
976 }
977 if( w<0 ){
978 utf8_printf(p->out,"%*.*s%s",-w,-w,
979 azArg[i] ? azArg[i] : p->nullValue,
980 i==nArg-1 ? rowSep : " ");
981 }else{
982 utf8_printf(p->out,"%-*.*s%s",w,w,
983 azArg[i] ? azArg[i] : p->nullValue,
984 i==nArg-1 ? rowSep : " ");
985 }
986 }
987 break;
988 }
989 case MODE_Semi:
@@ -984,11 +999,11 @@
999 char *z = azArg[i];
1000 if( z==0 ) z = p->nullValue;
1001 utf8_printf(p->out, "%s", z);
1002 if( i<nArg-1 ){
1003 utf8_printf(p->out, "%s", p->colSeparator);
1004 }else if( p->cMode==MODE_Semi ){
1005 utf8_printf(p->out, ";%s", p->rowSeparator);
1006 }else{
1007 utf8_printf(p->out, "%s", p->rowSeparator);
1008 }
1009 }
@@ -1489,14 +1504,21 @@
1504 "Rewind", 0 };
1505 const char *azGoto[] = { "Goto", 0 };
1506
1507 /* Try to figure out if this is really an EXPLAIN statement. If this
1508 ** cannot be verified, return early. */
1509 if( sqlite3_column_count(pSql)!=8 ){
1510 p->cMode = p->mode;
1511 return;
1512 }
1513 zSql = sqlite3_sql(pSql);
1514 if( zSql==0 ) return;
1515 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
1516 if( sqlite3_strnicmp(z, "explain", 7) ){
1517 p->cMode = p->mode;
1518 return;
1519 }
1520
1521 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
1522 int i;
1523 int iAddr = sqlite3_column_int(pSql, 0);
1524 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
@@ -1509,10 +1531,24 @@
1531 int p2 = sqlite3_column_int(pSql, 3);
1532 int p2op = (p2 + (iOp-iAddr));
1533
1534 /* Grow the p->aiIndent array as required */
1535 if( iOp>=nAlloc ){
1536 if( iOp==0 ){
1537 /* Do further verfication that this is explain output. Abort if
1538 ** it is not */
1539 static const char *explainCols[] = {
1540 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
1541 int jj;
1542 for(jj=0; jj<ArraySize(explainCols); jj++){
1543 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
1544 p->cMode = p->mode;
1545 sqlite3_reset(pSql);
1546 return;
1547 }
1548 }
1549 }
1550 nAlloc += 100;
1551 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
1552 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
1553 }
1554 abYield[iOp] = str_in_array(zOp, azYield);
@@ -1612,14 +1648,24 @@
1648 }
1649 sqlite3_finalize(pExplain);
1650 sqlite3_free(zEQP);
1651 }
1652
1653 if( pArg ){
1654 pArg->cMode = pArg->mode;
1655 if( pArg->autoExplain
1656 && sqlite3_column_count(pStmt)==8
1657 && sqlite3_strlike("%EXPLAIN%", sqlite3_sql(pStmt),0)==0
1658 ){
1659 pArg->cMode = MODE_Explain;
1660 }
1661
1662 /* If the shell is currently in ".explain" mode, gather the extra
1663 ** data required to add indents to the output.*/
1664 if( pArg->cMode==MODE_Explain ){
1665 explain_data_prepare(pArg, pStmt);
1666 }
1667 }
1668
1669 /* perform the first step. this will tell us if we
1670 ** have a result set or not and how wide it is.
1671 */
@@ -1645,11 +1691,11 @@
1691 }
1692 do{
1693 /* extract the data and data types */
1694 for(i=0; i<nCol; i++){
1695 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
1696 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
1697 azVals[i] = "";
1698 }else{
1699 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
1700 }
1701 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
@@ -1865,12 +1911,11 @@
1911 " If TABLE specified, only dump tables matching\n"
1912 " LIKE pattern TABLE.\n"
1913 ".echo on|off Turn command echo on or off\n"
1914 ".eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN\n"
1915 ".exit Exit this program\n"
1916 ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"
 
1917 ".fullschema Show schema and the content of sqlite_stat tables\n"
1918 ".headers on|off Turn display of headers on or off\n"
1919 ".help Show this message\n"
1920 ".import FILE TABLE Import data from FILE into TABLE\n"
1921 ".indexes ?TABLE? Show names of all indexes\n"
@@ -2853,11 +2898,11 @@
2898 ShellState data;
2899 char *zErrMsg = 0;
2900 open_db(p, 0);
2901 memcpy(&data, p, sizeof(data));
2902 data.showHeader = 1;
2903 data.cMode = data.mode = MODE_Column;
2904 data.colWidth[0] = 3;
2905 data.colWidth[1] = 15;
2906 data.colWidth[2] = 58;
2907 data.cnt = 0;
2908 sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
@@ -2948,41 +2993,28 @@
2993 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
2994 rc = 2;
2995 }else
2996
2997 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
2998 int val = 1;
2999 if( nArg>=2 ){
3000 if( strcmp(azArg[1],"auto")==0 ){
3001 val = 99;
3002 }else{
3003 val = booleanValue(azArg[1]);
3004 }
3005 }
3006 if( val==1 && p->mode!=MODE_Explain ){
3007 p->normalMode = p->mode;
 
 
 
 
 
3008 p->mode = MODE_Explain;
3009 p->autoExplain = 0;
3010 }else if( val==0 ){
3011 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
3012 p->autoExplain = 0;
3013 }else if( val==99 ){
3014 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
3015 p->autoExplain = 1;
 
 
 
 
 
 
 
 
3016 }
3017 }else
3018
3019 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
3020 ShellState data;
@@ -2994,11 +3026,11 @@
3026 goto meta_command_exit;
3027 }
3028 open_db(p, 0);
3029 memcpy(&data, p, sizeof(data));
3030 data.showHeader = 0;
3031 data.cMode = data.mode = MODE_Semi;
3032 rc = sqlite3_exec(p->db,
3033 "SELECT sql FROM"
3034 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
3035 " FROM sqlite_master UNION ALL"
3036 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
@@ -3019,11 +3051,11 @@
3051 raw_printf(p->out, "/* No STAT tables available */\n");
3052 }else{
3053 raw_printf(p->out, "ANALYZE sqlite_master;\n");
3054 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
3055 callback, &data, &zErrMsg);
3056 data.cMode = data.mode = MODE_Insert;
3057 data.zDestTable = "sqlite_stat1";
3058 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
3059 shell_callback, &data,&zErrMsg);
3060 data.zDestTable = "sqlite_stat3";
3061 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
@@ -3251,11 +3283,11 @@
3283 ShellState data;
3284 char *zErrMsg = 0;
3285 open_db(p, 0);
3286 memcpy(&data, p, sizeof(data));
3287 data.showHeader = 0;
3288 data.cMode = data.mode = MODE_List;
3289 if( nArg==1 ){
3290 rc = sqlite3_exec(p->db,
3291 "SELECT name FROM sqlite_master "
3292 "WHERE type='index' AND name NOT LIKE 'sqlite_%' "
3293 "UNION ALL "
@@ -3437,10 +3469,11 @@
3469 }else {
3470 raw_printf(stderr, "Error: mode should be one of: "
3471 "ascii column csv html insert line list tabs tcl\n");
3472 rc = 1;
3473 }
3474 p->cMode = p->mode;
3475 }else
3476
3477 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
3478 if( nArg==2 ){
3479 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
@@ -3626,11 +3659,11 @@
3659 ShellState data;
3660 char *zErrMsg = 0;
3661 open_db(p, 0);
3662 memcpy(&data, p, sizeof(data));
3663 data.showHeader = 0;
3664 data.cMode = data.mode = MODE_Semi;
3665 if( nArg==2 ){
3666 int i;
3667 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
3668 if( strcmp(azArg[1],"sqlite_master")==0 ){
3669 char *new_argv[2], *new_colv[2];
@@ -3774,11 +3807,12 @@
3807 rc = 1;
3808 goto meta_command_exit;
3809 }
3810 utf8_printf(p->out, "%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
3811 utf8_printf(p->out, "%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
3812 utf8_printf(p->out, "%12.12s: %s\n","explain",
3813 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
3814 utf8_printf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
3815 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
3816 utf8_printf(p->out, "%12.12s: ", "nullvalue");
3817 output_c_string(p->out, p->nullValue);
3818 raw_printf(p->out, "\n");
@@ -4573,11 +4607,12 @@
4607 /*
4608 ** Initialize the state information in data
4609 */
4610 static void main_init(ShellState *data) {
4611 memset(data, 0, sizeof(*data));
4612 data->normalMode = data->cMode = data->mode = MODE_List;
4613 data->autoExplain = 1;
4614 memcpy(data->colSeparator,SEP_Column, 2);
4615 memcpy(data->rowSeparator,SEP_Row, 2);
4616 data->showHeader = 0;
4617 data->shellFlgs = SHFLG_Lookaside;
4618 sqlite3_config(SQLITE_CONFIG_URI, 1);
@@ -4906,10 +4941,11 @@
4941 }else{
4942 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
4943 raw_printf(stderr,"Use -help for a list of options.\n");
4944 return 1;
4945 }
4946 data.cMode = data.mode;
4947 }
4948
4949 if( !readStdin ){
4950 /* Run all arguments that do not begin with '-' as if they were separate
4951 ** command-line inputs, except for the argToSkip argument which contains
4952
+18 -6
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -197,22 +197,21 @@
197197
**
198198
** CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;
199199
** SELECT * FROM foci WHERE checkinID=symbolic_name_to_rid('trunk');
200200
*/
201201
void cmd_sqlite3(void){
202
+ int noRepository;
202203
extern int sqlite3_shell(int, char**);
203
- if( find_option("no-repository", 0, 0)==0 ){
204
+ noRepository = find_option("no-repository", 0, 0)!=0;
205
+ if( !noRepository ){
204206
db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
205
- db_close(1);
206207
}
208
+ fossil_close(1, noRepository);
207209
sqlite3_shutdown();
208210
sqlite3_shell(g.argc-1, g.argv+1);
209211
sqlite3_cancel_auto_extension((void(*)(void))sqlcmd_autoinit);
210
- g.db = 0;
211
- g.zMainDbType = 0;
212
- g.repositoryOpen = 0;
213
- g.localOpen = 0;
212
+ fossil_close(0, noRepository);
214213
}
215214
216215
/*
217216
** This routine is called by the patched sqlite3 command-line shell in order
218217
** to load the name and database connection for the open Fossil database.
@@ -219,5 +218,18 @@
219218
*/
220219
void fossil_open(const char **pzRepoName){
221220
sqlite3_auto_extension((void(*)(void))sqlcmd_autoinit);
222221
*pzRepoName = g.zRepositoryName;
223222
}
223
+
224
+/*
225
+** This routine closes the Fossil databases and/or invalidates the global
226
+** state variables that keep track of them.
227
+*/
228
+void fossil_close(int bDb, int noRepository){
229
+ if( bDb ) db_close(1);
230
+ if( noRepository ) g.zRepositoryName = 0;
231
+ g.db = 0;
232
+ g.zMainDbType = 0;
233
+ g.repositoryOpen = 0;
234
+ g.localOpen = 0;
235
+}
224236
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -197,22 +197,21 @@
197 **
198 ** CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;
199 ** SELECT * FROM foci WHERE checkinID=symbolic_name_to_rid('trunk');
200 */
201 void cmd_sqlite3(void){
 
202 extern int sqlite3_shell(int, char**);
203 if( find_option("no-repository", 0, 0)==0 ){
 
204 db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
205 db_close(1);
206 }
 
207 sqlite3_shutdown();
208 sqlite3_shell(g.argc-1, g.argv+1);
209 sqlite3_cancel_auto_extension((void(*)(void))sqlcmd_autoinit);
210 g.db = 0;
211 g.zMainDbType = 0;
212 g.repositoryOpen = 0;
213 g.localOpen = 0;
214 }
215
216 /*
217 ** This routine is called by the patched sqlite3 command-line shell in order
218 ** to load the name and database connection for the open Fossil database.
@@ -219,5 +218,18 @@
219 */
220 void fossil_open(const char **pzRepoName){
221 sqlite3_auto_extension((void(*)(void))sqlcmd_autoinit);
222 *pzRepoName = g.zRepositoryName;
223 }
 
 
 
 
 
 
 
 
 
 
 
 
 
224
--- src/sqlcmd.c
+++ src/sqlcmd.c
@@ -197,22 +197,21 @@
197 **
198 ** CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;
199 ** SELECT * FROM foci WHERE checkinID=symbolic_name_to_rid('trunk');
200 */
201 void cmd_sqlite3(void){
202 int noRepository;
203 extern int sqlite3_shell(int, char**);
204 noRepository = find_option("no-repository", 0, 0)!=0;
205 if( !noRepository ){
206 db_find_and_open_repository(OPEN_ANY_SCHEMA, 0);
 
207 }
208 fossil_close(1, noRepository);
209 sqlite3_shutdown();
210 sqlite3_shell(g.argc-1, g.argv+1);
211 sqlite3_cancel_auto_extension((void(*)(void))sqlcmd_autoinit);
212 fossil_close(0, noRepository);
 
 
 
213 }
214
215 /*
216 ** This routine is called by the patched sqlite3 command-line shell in order
217 ** to load the name and database connection for the open Fossil database.
@@ -219,5 +218,18 @@
218 */
219 void fossil_open(const char **pzRepoName){
220 sqlite3_auto_extension((void(*)(void))sqlcmd_autoinit);
221 *pzRepoName = g.zRepositoryName;
222 }
223
224 /*
225 ** This routine closes the Fossil databases and/or invalidates the global
226 ** state variables that keep track of them.
227 */
228 void fossil_close(int bDb, int noRepository){
229 if( bDb ) db_close(1);
230 if( noRepository ) g.zRepositoryName = 0;
231 g.db = 0;
232 g.zMainDbType = 0;
233 g.repositoryOpen = 0;
234 g.localOpen = 0;
235 }
236
+3341 -2581
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -121,10 +121,11 @@
121121
#else
122122
/* This is not VxWorks. */
123123
#define OS_VXWORKS 0
124124
#define HAVE_FCHOWN 1
125125
#define HAVE_READLINK 1
126
+#define HAVE_LSTAT 1
126127
#endif /* defined(_WRS_KERNEL) */
127128
128129
/************** End of vxworks.h *********************************************/
129130
/************** Continuing where we left off in sqliteInt.h ******************/
130131
@@ -327,11 +328,11 @@
327328
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
328329
** [sqlite_version()] and [sqlite_source_id()].
329330
*/
330331
#define SQLITE_VERSION "3.11.0"
331332
#define SQLITE_VERSION_NUMBER 3011000
332
-#define SQLITE_SOURCE_ID "2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1"
333
+#define SQLITE_SOURCE_ID "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
333334
334335
/*
335336
** CAPI3REF: Run-Time Library Version Numbers
336337
** KEYWORDS: sqlite3_version, sqlite3_sourceid
337338
**
@@ -561,11 +562,11 @@
561562
** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
562563
** is not NULL then any error message is written into memory obtained
563564
** from [sqlite3_malloc()] and passed back through the 5th parameter.
564565
** To avoid memory leaks, the application should invoke [sqlite3_free()]
565566
** on error message strings returned through the 5th parameter of
566
-** of sqlite3_exec() after the error message string is no longer needed.
567
+** sqlite3_exec() after the error message string is no longer needed.
567568
** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
568569
** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
569570
** NULL before returning.
570571
**
571572
** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5911,11 +5912,11 @@
59115912
*/
59125913
struct sqlite3_index_info {
59135914
/* Inputs */
59145915
int nConstraint; /* Number of entries in aConstraint */
59155916
struct sqlite3_index_constraint {
5916
- int iColumn; /* Column on left-hand side of constraint */
5917
+ int iColumn; /* Column constrained. -1 for ROWID */
59175918
unsigned char op; /* Constraint operator */
59185919
unsigned char usable; /* True if this constraint is usable */
59195920
int iTermOffset; /* Used internally - xBestIndex should ignore */
59205921
} *aConstraint; /* Table of WHERE clause constraints */
59215922
int nOrderBy; /* Number of terms in the ORDER BY clause */
@@ -10468,18 +10469,28 @@
1046810469
1046910470
/*
1047010471
** Flags passed as the third argument to sqlite3BtreeCursor().
1047110472
**
1047210473
** For read-only cursors the wrFlag argument is always zero. For read-write
10473
-** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or
10474
-** (BTREE_WRCSR). If the BTREE_FORDELETE flag is set, then the cursor will
10474
+** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
10475
+** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
1047510476
** only be used by SQLite for the following:
1047610477
**
10477
-** * to seek to and delete specific entries, and/or
10478
+** * to seek to and then delete specific entries, and/or
1047810479
**
1047910480
** * to read values that will be used to create keys that other
1048010481
** BTREE_FORDELETE cursors will seek to and delete.
10482
+**
10483
+** The BTREE_FORDELETE flag is an optimization hint. It is not used by
10484
+** by this, the native b-tree engine of SQLite, but it is available to
10485
+** alternative storage engines that might be substituted in place of this
10486
+** b-tree system. For alternative storage engines in which a delete of
10487
+** the main table row automatically deletes corresponding index rows,
10488
+** the FORDELETE flag hint allows those alternative storage engines to
10489
+** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
10490
+** and DELETE operations as no-ops, and any READ operation against a
10491
+** FORDELETE cursor may return a null row: 0x01 0x00.
1048110492
*/
1048210493
#define BTREE_WRCSR 0x00000004 /* read-write cursor */
1048310494
#define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
1048410495
1048510496
SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -10504,11 +10515,16 @@
1050410515
int bias,
1050510516
int *pRes
1050610517
);
1050710518
SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
1050810519
SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
10509
-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, int);
10520
+SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
10521
+
10522
+/* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
10523
+#define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
10524
+#define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
10525
+
1051010526
SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
1051110527
const void *pData, int nData,
1051210528
int nZero, int bias, int seekResult);
1051310529
SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
1051410530
SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
@@ -10762,94 +10778,95 @@
1076210778
*/
1076310779
/************** Include opcodes.h in the middle of vdbe.h ********************/
1076410780
/************** Begin file opcodes.h *****************************************/
1076510781
/* Automatically generated. Do not edit */
1076610782
/* See the tool/mkopcodeh.tcl script for details */
10767
-#define OP_Savepoint 1
10768
-#define OP_AutoCommit 2
10769
-#define OP_Transaction 3
10770
-#define OP_SorterNext 4
10771
-#define OP_PrevIfOpen 5
10772
-#define OP_NextIfOpen 6
10773
-#define OP_Prev 7
10774
-#define OP_Next 8
10775
-#define OP_Checkpoint 9
10776
-#define OP_JournalMode 10
10777
-#define OP_Vacuum 11
10778
-#define OP_VFilter 12 /* synopsis: iplan=r[P3] zplan='P4' */
10779
-#define OP_VUpdate 13 /* synopsis: data=r[P3@P2] */
10780
-#define OP_Goto 14
10781
-#define OP_Gosub 15
10782
-#define OP_Return 16
10783
-#define OP_InitCoroutine 17
10784
-#define OP_EndCoroutine 18
10783
+#define OP_Savepoint 0
10784
+#define OP_AutoCommit 1
10785
+#define OP_Transaction 2
10786
+#define OP_SorterNext 3
10787
+#define OP_PrevIfOpen 4
10788
+#define OP_NextIfOpen 5
10789
+#define OP_Prev 6
10790
+#define OP_Next 7
10791
+#define OP_Checkpoint 8
10792
+#define OP_JournalMode 9
10793
+#define OP_Vacuum 10
10794
+#define OP_VFilter 11 /* synopsis: iplan=r[P3] zplan='P4' */
10795
+#define OP_VUpdate 12 /* synopsis: data=r[P3@P2] */
10796
+#define OP_Goto 13
10797
+#define OP_Gosub 14
10798
+#define OP_Return 15
10799
+#define OP_InitCoroutine 16
10800
+#define OP_EndCoroutine 17
10801
+#define OP_Yield 18
1078510802
#define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
10786
-#define OP_Yield 20
10787
-#define OP_HaltIfNull 21 /* synopsis: if r[P3]=null halt */
10788
-#define OP_Halt 22
10789
-#define OP_Integer 23 /* synopsis: r[P2]=P1 */
10790
-#define OP_Int64 24 /* synopsis: r[P2]=P4 */
10791
-#define OP_String 25 /* synopsis: r[P2]='P4' (len=P1) */
10792
-#define OP_Null 26 /* synopsis: r[P2..P3]=NULL */
10793
-#define OP_SoftNull 27 /* synopsis: r[P1]=NULL */
10794
-#define OP_Blob 28 /* synopsis: r[P2]=P4 (len=P1) */
10795
-#define OP_Variable 29 /* synopsis: r[P2]=parameter(P1,P4) */
10796
-#define OP_Move 30 /* synopsis: r[P2@P3]=r[P1@P3] */
10797
-#define OP_Copy 31 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
10798
-#define OP_SCopy 32 /* synopsis: r[P2]=r[P1] */
10799
-#define OP_IntCopy 33 /* synopsis: r[P2]=r[P1] */
10800
-#define OP_ResultRow 34 /* synopsis: output=r[P1@P2] */
10801
-#define OP_CollSeq 35
10802
-#define OP_Function0 36 /* synopsis: r[P3]=func(r[P2@P5]) */
10803
-#define OP_Function 37 /* synopsis: r[P3]=func(r[P2@P5]) */
10804
-#define OP_AddImm 38 /* synopsis: r[P1]=r[P1]+P2 */
10805
-#define OP_MustBeInt 39
10806
-#define OP_RealAffinity 40
10807
-#define OP_Cast 41 /* synopsis: affinity(r[P1]) */
10808
-#define OP_Permutation 42
10809
-#define OP_Compare 43 /* synopsis: r[P1@P3] <-> r[P2@P3] */
10810
-#define OP_Jump 44
10811
-#define OP_Once 45
10812
-#define OP_If 46
10813
-#define OP_IfNot 47
10814
-#define OP_Column 48 /* synopsis: r[P3]=PX */
10815
-#define OP_Affinity 49 /* synopsis: affinity(r[P1@P2]) */
10816
-#define OP_MakeRecord 50 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
10817
-#define OP_Count 51 /* synopsis: r[P2]=count() */
10818
-#define OP_ReadCookie 52
10819
-#define OP_SetCookie 53
10820
-#define OP_ReopenIdx 54 /* synopsis: root=P2 iDb=P3 */
10821
-#define OP_OpenRead 55 /* synopsis: root=P2 iDb=P3 */
10822
-#define OP_OpenWrite 56 /* synopsis: root=P2 iDb=P3 */
10823
-#define OP_OpenAutoindex 57 /* synopsis: nColumn=P2 */
10824
-#define OP_OpenEphemeral 58 /* synopsis: nColumn=P2 */
10825
-#define OP_SorterOpen 59
10826
-#define OP_SequenceTest 60 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
10827
-#define OP_OpenPseudo 61 /* synopsis: P3 columns in r[P2] */
10828
-#define OP_Close 62
10829
-#define OP_ColumnsUsed 63
10830
-#define OP_SeekLT 64 /* synopsis: key=r[P3@P4] */
10831
-#define OP_SeekLE 65 /* synopsis: key=r[P3@P4] */
10832
-#define OP_SeekGE 66 /* synopsis: key=r[P3@P4] */
10833
-#define OP_SeekGT 67 /* synopsis: key=r[P3@P4] */
10834
-#define OP_Seek 68 /* synopsis: intkey=r[P2] */
10835
-#define OP_NoConflict 69 /* synopsis: key=r[P3@P4] */
10836
-#define OP_NotFound 70 /* synopsis: key=r[P3@P4] */
10803
+#define OP_HaltIfNull 20 /* synopsis: if r[P3]=null halt */
10804
+#define OP_Halt 21
10805
+#define OP_Integer 22 /* synopsis: r[P2]=P1 */
10806
+#define OP_Int64 23 /* synopsis: r[P2]=P4 */
10807
+#define OP_String 24 /* synopsis: r[P2]='P4' (len=P1) */
10808
+#define OP_Null 25 /* synopsis: r[P2..P3]=NULL */
10809
+#define OP_SoftNull 26 /* synopsis: r[P1]=NULL */
10810
+#define OP_Blob 27 /* synopsis: r[P2]=P4 (len=P1) */
10811
+#define OP_Variable 28 /* synopsis: r[P2]=parameter(P1,P4) */
10812
+#define OP_Move 29 /* synopsis: r[P2@P3]=r[P1@P3] */
10813
+#define OP_Copy 30 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
10814
+#define OP_SCopy 31 /* synopsis: r[P2]=r[P1] */
10815
+#define OP_IntCopy 32 /* synopsis: r[P2]=r[P1] */
10816
+#define OP_ResultRow 33 /* synopsis: output=r[P1@P2] */
10817
+#define OP_CollSeq 34
10818
+#define OP_Function0 35 /* synopsis: r[P3]=func(r[P2@P5]) */
10819
+#define OP_Function 36 /* synopsis: r[P3]=func(r[P2@P5]) */
10820
+#define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */
10821
+#define OP_MustBeInt 38
10822
+#define OP_RealAffinity 39
10823
+#define OP_Cast 40 /* synopsis: affinity(r[P1]) */
10824
+#define OP_Permutation 41
10825
+#define OP_Compare 42 /* synopsis: r[P1@P3] <-> r[P2@P3] */
10826
+#define OP_Jump 43
10827
+#define OP_Once 44
10828
+#define OP_If 45
10829
+#define OP_IfNot 46
10830
+#define OP_Column 47 /* synopsis: r[P3]=PX */
10831
+#define OP_Affinity 48 /* synopsis: affinity(r[P1@P2]) */
10832
+#define OP_MakeRecord 49 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
10833
+#define OP_Count 50 /* synopsis: r[P2]=count() */
10834
+#define OP_ReadCookie 51
10835
+#define OP_SetCookie 52
10836
+#define OP_ReopenIdx 53 /* synopsis: root=P2 iDb=P3 */
10837
+#define OP_OpenRead 54 /* synopsis: root=P2 iDb=P3 */
10838
+#define OP_OpenWrite 55 /* synopsis: root=P2 iDb=P3 */
10839
+#define OP_OpenAutoindex 56 /* synopsis: nColumn=P2 */
10840
+#define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */
10841
+#define OP_SorterOpen 58
10842
+#define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
10843
+#define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */
10844
+#define OP_Close 61
10845
+#define OP_ColumnsUsed 62
10846
+#define OP_SeekLT 63 /* synopsis: key=r[P3@P4] */
10847
+#define OP_SeekLE 64 /* synopsis: key=r[P3@P4] */
10848
+#define OP_SeekGE 65 /* synopsis: key=r[P3@P4] */
10849
+#define OP_SeekGT 66 /* synopsis: key=r[P3@P4] */
10850
+#define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */
10851
+#define OP_NotFound 68 /* synopsis: key=r[P3@P4] */
10852
+#define OP_Found 69 /* synopsis: key=r[P3@P4] */
10853
+#define OP_NotExists 70 /* synopsis: intkey=r[P3] */
1083710854
#define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
1083810855
#define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
10839
-#define OP_Found 73 /* synopsis: key=r[P3@P4] */
10840
-#define OP_NotExists 74 /* synopsis: intkey=r[P3] */
10841
-#define OP_Sequence 75 /* synopsis: r[P2]=cursor[P1].ctr++ */
10856
+#define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */
10857
+#define OP_NewRowid 74 /* synopsis: r[P2]=rowid */
10858
+#define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */
1084210859
#define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
1084310860
#define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
1084410861
#define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
1084510862
#define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
1084610863
#define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
1084710864
#define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
1084810865
#define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
1084910866
#define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
10850
-#define OP_NewRowid 84 /* synopsis: r[P2]=rowid */
10867
+#define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */
1085110868
#define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
1085210869
#define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
1085310870
#define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
1085410871
#define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
1085510872
#define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -10856,111 +10873,110 @@
1085610873
#define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
1085710874
#define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
1085810875
#define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
1085910876
#define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
1086010877
#define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
10861
-#define OP_Insert 95 /* synopsis: intkey=r[P3] data=r[P2] */
10878
+#define OP_Delete 95
1086210879
#define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
1086310880
#define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
10864
-#define OP_InsertInt 98 /* synopsis: intkey=P3 data=r[P2] */
10865
-#define OP_Delete 99
10866
-#define OP_ResetCount 100
10867
-#define OP_SorterCompare 101 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
10868
-#define OP_SorterData 102 /* synopsis: r[P2]=data */
10869
-#define OP_RowKey 103 /* synopsis: r[P2]=key */
10870
-#define OP_RowData 104 /* synopsis: r[P2]=data */
10871
-#define OP_Rowid 105 /* synopsis: r[P2]=rowid */
10872
-#define OP_NullRow 106
10873
-#define OP_Last 107
10874
-#define OP_SorterSort 108
10875
-#define OP_Sort 109
10876
-#define OP_Rewind 110
10877
-#define OP_SorterInsert 111
10878
-#define OP_IdxInsert 112 /* synopsis: key=r[P2] */
10879
-#define OP_IdxDelete 113 /* synopsis: key=r[P2@P3] */
10880
-#define OP_IdxRowid 114 /* synopsis: r[P2]=rowid */
10881
-#define OP_IdxLE 115 /* synopsis: key=r[P3@P4] */
10882
-#define OP_IdxGT 116 /* synopsis: key=r[P3@P4] */
10883
-#define OP_IdxLT 117 /* synopsis: key=r[P3@P4] */
10884
-#define OP_IdxGE 118 /* synopsis: key=r[P3@P4] */
10885
-#define OP_Destroy 119
10886
-#define OP_Clear 120
10887
-#define OP_ResetSorter 121
10888
-#define OP_CreateIndex 122 /* synopsis: r[P2]=root iDb=P1 */
10889
-#define OP_CreateTable 123 /* synopsis: r[P2]=root iDb=P1 */
10890
-#define OP_ParseSchema 124
10891
-#define OP_LoadAnalysis 125
10892
-#define OP_DropTable 126
10893
-#define OP_DropIndex 127
10894
-#define OP_DropTrigger 128
10895
-#define OP_IntegrityCk 129
10896
-#define OP_RowSetAdd 130 /* synopsis: rowset(P1)=r[P2] */
10897
-#define OP_RowSetRead 131 /* synopsis: r[P3]=rowset(P1) */
10898
-#define OP_RowSetTest 132 /* synopsis: if r[P3] in rowset(P1) goto P2 */
10881
+#define OP_ResetCount 98
10882
+#define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
10883
+#define OP_SorterData 100 /* synopsis: r[P2]=data */
10884
+#define OP_RowKey 101 /* synopsis: r[P2]=key */
10885
+#define OP_RowData 102 /* synopsis: r[P2]=data */
10886
+#define OP_Rowid 103 /* synopsis: r[P2]=rowid */
10887
+#define OP_NullRow 104
10888
+#define OP_Last 105
10889
+#define OP_SorterSort 106
10890
+#define OP_Sort 107
10891
+#define OP_Rewind 108
10892
+#define OP_SorterInsert 109
10893
+#define OP_IdxInsert 110 /* synopsis: key=r[P2] */
10894
+#define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */
10895
+#define OP_Seek 112 /* synopsis: Move P3 to P1.rowid */
10896
+#define OP_IdxRowid 113 /* synopsis: r[P2]=rowid */
10897
+#define OP_IdxLE 114 /* synopsis: key=r[P3@P4] */
10898
+#define OP_IdxGT 115 /* synopsis: key=r[P3@P4] */
10899
+#define OP_IdxLT 116 /* synopsis: key=r[P3@P4] */
10900
+#define OP_IdxGE 117 /* synopsis: key=r[P3@P4] */
10901
+#define OP_Destroy 118
10902
+#define OP_Clear 119
10903
+#define OP_ResetSorter 120
10904
+#define OP_CreateIndex 121 /* synopsis: r[P2]=root iDb=P1 */
10905
+#define OP_CreateTable 122 /* synopsis: r[P2]=root iDb=P1 */
10906
+#define OP_ParseSchema 123
10907
+#define OP_LoadAnalysis 124
10908
+#define OP_DropTable 125
10909
+#define OP_DropIndex 126
10910
+#define OP_DropTrigger 127
10911
+#define OP_IntegrityCk 128
10912
+#define OP_RowSetAdd 129 /* synopsis: rowset(P1)=r[P2] */
10913
+#define OP_RowSetRead 130 /* synopsis: r[P3]=rowset(P1) */
10914
+#define OP_RowSetTest 131 /* synopsis: if r[P3] in rowset(P1) goto P2 */
10915
+#define OP_Program 132
1089910916
#define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
10900
-#define OP_Program 134
10901
-#define OP_Param 135
10902
-#define OP_FkCounter 136 /* synopsis: fkctr[P1]+=P2 */
10903
-#define OP_FkIfZero 137 /* synopsis: if fkctr[P1]==0 goto P2 */
10904
-#define OP_MemMax 138 /* synopsis: r[P1]=max(r[P1],r[P2]) */
10905
-#define OP_IfPos 139 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
10906
-#define OP_SetIfNotPos 140 /* synopsis: if r[P1]<=0 then r[P2]=P3 */
10907
-#define OP_IfNotZero 141 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
10908
-#define OP_DecrJumpZero 142 /* synopsis: if (--r[P1])==0 goto P2 */
10909
-#define OP_JumpZeroIncr 143 /* synopsis: if (r[P1]++)==0 ) goto P2 */
10910
-#define OP_AggStep0 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10911
-#define OP_AggStep 145 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10912
-#define OP_AggFinal 146 /* synopsis: accum=r[P1] N=P2 */
10913
-#define OP_IncrVacuum 147
10914
-#define OP_Expire 148
10915
-#define OP_TableLock 149 /* synopsis: iDb=P1 root=P2 write=P3 */
10916
-#define OP_VBegin 150
10917
-#define OP_VCreate 151
10918
-#define OP_VDestroy 152
10919
-#define OP_VOpen 153
10920
-#define OP_VColumn 154 /* synopsis: r[P3]=vcolumn(P2) */
10921
-#define OP_VNext 155
10922
-#define OP_VRename 156
10923
-#define OP_Pagecount 157
10924
-#define OP_MaxPgcnt 158
10925
-#define OP_Init 159 /* synopsis: Start at P2 */
10926
-#define OP_CursorHint 160
10927
-#define OP_Noop 161
10928
-#define OP_Explain 162
10917
+#define OP_Param 134
10918
+#define OP_FkCounter 135 /* synopsis: fkctr[P1]+=P2 */
10919
+#define OP_FkIfZero 136 /* synopsis: if fkctr[P1]==0 goto P2 */
10920
+#define OP_MemMax 137 /* synopsis: r[P1]=max(r[P1],r[P2]) */
10921
+#define OP_IfPos 138 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
10922
+#define OP_OffsetLimit 139 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
10923
+#define OP_IfNotZero 140 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
10924
+#define OP_DecrJumpZero 141 /* synopsis: if (--r[P1])==0 goto P2 */
10925
+#define OP_JumpZeroIncr 142 /* synopsis: if (r[P1]++)==0 ) goto P2 */
10926
+#define OP_AggStep0 143 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10927
+#define OP_AggStep 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10928
+#define OP_AggFinal 145 /* synopsis: accum=r[P1] N=P2 */
10929
+#define OP_IncrVacuum 146
10930
+#define OP_Expire 147
10931
+#define OP_TableLock 148 /* synopsis: iDb=P1 root=P2 write=P3 */
10932
+#define OP_VBegin 149
10933
+#define OP_VCreate 150
10934
+#define OP_VDestroy 151
10935
+#define OP_VOpen 152
10936
+#define OP_VColumn 153 /* synopsis: r[P3]=vcolumn(P2) */
10937
+#define OP_VNext 154
10938
+#define OP_VRename 155
10939
+#define OP_Pagecount 156
10940
+#define OP_MaxPgcnt 157
10941
+#define OP_Init 158 /* synopsis: Start at P2 */
10942
+#define OP_CursorHint 159
10943
+#define OP_Noop 160
10944
+#define OP_Explain 161
1092910945
1093010946
/* Properties such as "out2" or "jump" that are specified in
1093110947
** comments following the "case" for each opcode in the vdbe.c
1093210948
** are encoded into bitvectors as follows:
1093310949
*/
10934
-#define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */
10935
-#define OPFLG_IN1 0x0002 /* in1: P1 is an input */
10936
-#define OPFLG_IN2 0x0004 /* in2: P2 is an input */
10937
-#define OPFLG_IN3 0x0008 /* in3: P3 is an input */
10938
-#define OPFLG_OUT2 0x0010 /* out2: P2 is an output */
10939
-#define OPFLG_OUT3 0x0020 /* out3: P3 is an output */
10950
+#define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
10951
+#define OPFLG_IN1 0x02 /* in1: P1 is an input */
10952
+#define OPFLG_IN2 0x04 /* in2: P2 is an input */
10953
+#define OPFLG_IN3 0x08 /* in3: P3 is an input */
10954
+#define OPFLG_OUT2 0x10 /* out2: P2 is an output */
10955
+#define OPFLG_OUT3 0x20 /* out3: P3 is an output */
1094010956
#define OPFLG_INITIALIZER {\
10941
-/* 0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\
10942
-/* 8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\
10943
-/* 16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\
10944
-/* 24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\
10945
-/* 32 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03,\
10946
-/* 40 */ 0x02, 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03,\
10947
-/* 48 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00,\
10948
-/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10949
-/* 64 */ 0x09, 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x26,\
10950
-/* 72 */ 0x26, 0x09, 0x09, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
10951
-/* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x10, 0x26, 0x26, 0x26,\
10957
+/* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
10958
+/* 8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02,\
10959
+/* 16 */ 0x01, 0x02, 0x03, 0x12, 0x08, 0x00, 0x10, 0x10,\
10960
+/* 24 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
10961
+/* 32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
10962
+/* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
10963
+/* 48 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
10964
+/* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
10965
+/* 64 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x26,\
10966
+/* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
10967
+/* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
1095210968
/* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
10953
-/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10954
-/* 104 */ 0x00, 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04,\
10955
-/* 112 */ 0x04, 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10,\
10956
-/* 120 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
10957
-/* 128 */ 0x00, 0x00, 0x06, 0x23, 0x0b, 0x10, 0x01, 0x10,\
10958
-/* 136 */ 0x00, 0x01, 0x04, 0x03, 0x06, 0x03, 0x03, 0x03,\
10959
-/* 144 */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,\
10960
-/* 152 */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01,\
10961
-/* 160 */ 0x00, 0x00, 0x00,}
10969
+/* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
10970
+/* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
10971
+/* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
10972
+/* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
10973
+/* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
10974
+/* 136 */ 0x01, 0x04, 0x03, 0x1a, 0x03, 0x03, 0x03, 0x00,\
10975
+/* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
10976
+/* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
10977
+/* 160 */ 0x00, 0x00,}
1096210978
1096310979
/************** End of opcodes.h *********************************************/
1096410980
/************** Continuing where we left off in vdbe.h ***********************/
1096510981
1096610982
/*
@@ -10976,10 +10992,11 @@
1097610992
SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
1097710993
SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
1097810994
SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
1097910995
SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
1098010996
SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
10997
+SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
1098110998
#if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
1098210999
SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
1098311000
#else
1098411001
# define sqlite3VdbeVerifyNoMallocRequired(A,B)
1098511002
#endif
@@ -11199,15 +11216,16 @@
1119911216
** Flags for sqlite3PagerSetFlags()
1120011217
*/
1120111218
#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
1120211219
#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
1120311220
#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
11204
-#define PAGER_SYNCHRONOUS_MASK 0x03 /* Mask for three values above */
11205
-#define PAGER_FULLFSYNC 0x04 /* PRAGMA fullfsync=ON */
11206
-#define PAGER_CKPT_FULLFSYNC 0x08 /* PRAGMA checkpoint_fullfsync=ON */
11207
-#define PAGER_CACHESPILL 0x10 /* PRAGMA cache_spill=ON */
11208
-#define PAGER_FLAGS_MASK 0x1c /* All above except SYNCHRONOUS */
11221
+#define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
11222
+#define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
11223
+#define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
11224
+#define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
11225
+#define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
11226
+#define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
1120911227
1121011228
/*
1121111229
** The remainder of this file contains the declarations of the functions
1121211230
** that make up the Pager sub-system API. See source code comments for
1121311231
** a detailed description of each routine.
@@ -11963,12 +11981,12 @@
1196311981
** is shared by multiple database connections. Therefore, while parsing
1196411982
** schema information, the Lookaside.bEnabled flag is cleared so that
1196511983
** lookaside allocations are not used to construct the schema objects.
1196611984
*/
1196711985
struct Lookaside {
11986
+ u32 bDisable; /* Only operate the lookaside when zero */
1196811987
u16 sz; /* Size of each buffer in bytes */
11969
- u8 bEnabled; /* False to disable new lookaside allocations */
1197011988
u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
1197111989
int nOut; /* Number of buffers currently checked out */
1197211990
int mxOut; /* Highwater mark for nOut */
1197311991
int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
1197411992
LookasideSlot *pFree; /* List of available buffers */
@@ -12047,10 +12065,11 @@
1204712065
u16 dbOptFlags; /* Flags to enable/disable optimizations */
1204812066
u8 enc; /* Text encoding */
1204912067
u8 autoCommit; /* The auto-commit flag. */
1205012068
u8 temp_store; /* 1: file 2: memory 0: default */
1205112069
u8 mallocFailed; /* True if we have seen a malloc failure */
12070
+ u8 bBenignMalloc; /* Do not require OOMs if true */
1205212071
u8 dfltLockMode; /* Default locking-mode for attached dbs */
1205312072
signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
1205412073
u8 suppressErr; /* Do not issue error messages if true */
1205512074
u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
1205612075
u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
@@ -12155,14 +12174,14 @@
1215512174
/*
1215612175
** Possible values for the sqlite3.flags.
1215712176
*/
1215812177
#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
1215912178
#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
12160
-#define SQLITE_FullFSync 0x00000004 /* Use full fsync on the backend */
12161
-#define SQLITE_CkptFullFSync 0x00000008 /* Use full fsync for checkpoint */
12162
-#define SQLITE_CacheSpill 0x00000010 /* OK to spill pager cache */
12163
-#define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
12179
+#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
12180
+#define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
12181
+#define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
12182
+#define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
1216412183
#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
1216512184
#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
1216612185
/* DELETE, or UPDATE and return */
1216712186
/* the count using a callback. */
1216812187
#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
@@ -13510,11 +13529,11 @@
1351013529
/*
1351113530
** During code generation of statements that do inserts into AUTOINCREMENT
1351213531
** tables, the following information is attached to the Table.u.autoInc.p
1351313532
** pointer of each autoincrement table to record some side information that
1351413533
** the code generator needs. We have to keep per-table autoincrement
13515
-** information in case inserts are down within triggers. Triggers do not
13534
+** information in case inserts are done within triggers. Triggers do not
1351613535
** normally coordinate their activities, but we do need to coordinate the
1351713536
** loading and saving of autoincrement information.
1351813537
*/
1351913538
struct AutoincInfo {
1352013539
AutoincInfo *pNext; /* Next info block in a list of them all */
@@ -13602,10 +13621,11 @@
1360213621
u8 nTempReg; /* Number of temporary registers in aTempReg[] */
1360313622
u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
1360413623
u8 mayAbort; /* True if statement may throw an ABORT exception */
1360513624
u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
1360613625
u8 okConstFactor; /* OK to factor out constants */
13626
+ u8 disableLookaside; /* Number of times lookaside has been disabled */
1360713627
int aTempReg[8]; /* Holding area for temporary registers */
1360813628
int nRangeReg; /* Size of the temporary register block */
1360913629
int iRangeReg; /* First register in temporary register block */
1361013630
int nErr; /* Number of errors seen */
1361113631
int nTab; /* Number of previously allocated VDBE cursors */
@@ -13716,23 +13736,26 @@
1371613736
};
1371713737
1371813738
/*
1371913739
** Bitfield flags for P5 value in various opcodes.
1372013740
*/
13721
-#define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
13741
+#define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
13742
+ /* Also used in P2 (not P5) of OP_Delete */
1372213743
#define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
1372313744
#define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
1372413745
#define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
1372513746
#define OPFLAG_APPEND 0x08 /* This is likely to be an append */
1372613747
#define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
1372713748
#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
1372813749
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
1372913750
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
1373013751
#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
13731
-#define OPFLAG_FORDELETE 0x08 /* OP_Open is opening for-delete csr */
13752
+#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
1373213753
#define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
1373313754
#define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
13755
+#define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */
13756
+#define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
1373413757
1373513758
/*
1373613759
* Each trigger present in the database schema is stored as an instance of
1373713760
* struct Trigger.
1373813761
*
@@ -13847,14 +13870,20 @@
1384713870
char *zText; /* The string collected so far */
1384813871
u32 nChar; /* Length of the string so far */
1384913872
u32 nAlloc; /* Amount of space allocated in zText */
1385013873
u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
1385113874
u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
13852
- u8 bMalloced; /* zText points to allocated space */
13875
+ u8 printfFlags; /* SQLITE_PRINTF flags below */
1385313876
};
1385413877
#define STRACCUM_NOMEM 1
1385513878
#define STRACCUM_TOOBIG 2
13879
+#define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
13880
+#define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
13881
+#define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
13882
+
13883
+#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
13884
+
1385613885
1385713886
/*
1385813887
** A pointer to this structure is used to communicate information
1385913888
** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
1386013889
*/
@@ -13958,10 +13987,11 @@
1395813987
int n; /* A counter */
1395913988
int iCur; /* A cursor number */
1396013989
SrcList *pSrcList; /* FROM clause */
1396113990
struct SrcCount *pSrcCount; /* Counting column references */
1396213991
struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
13992
+ int *aiCol; /* array of column indexes */
1396313993
} u;
1396413994
};
1396513995
1396613996
/* Forward declarations */
1396713997
SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -14027,10 +14057,17 @@
1402714057
SQLITE_PRIVATE int sqlite3CantopenError(int);
1402814058
#define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
1402914059
#define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
1403014060
#define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
1403114061
14062
+/*
14063
+** FTS3 and FTS4 both require virtual table support
14064
+*/
14065
+#if defined(SQLITE_OMIT_VIRTUALTABLE)
14066
+# undef SQLITE_ENABLE_FTS3
14067
+# undef SQLITE_ENABLE_FTS4
14068
+#endif
1403214069
1403314070
/*
1403414071
** FTS4 is really an extension for FTS3. It is enabled using the
1403514072
** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
1403614073
** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
@@ -14085,10 +14122,11 @@
1408514122
SQLITE_PRIVATE void sqlite3MallocEnd(void);
1408614123
SQLITE_PRIVATE void *sqlite3Malloc(u64);
1408714124
SQLITE_PRIVATE void *sqlite3MallocZero(u64);
1408814125
SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
1408914126
SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
14127
+SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
1409014128
SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
1409114129
SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
1409214130
SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
1409314131
SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
1409414132
SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
@@ -14167,14 +14205,12 @@
1416714205
int nArg; /* Total number of arguments */
1416814206
int nUsed; /* Number of arguments used so far */
1416914207
sqlite3_value **apArg; /* The argument values */
1417014208
};
1417114209
14172
-#define SQLITE_PRINTF_INTERNAL 0x01
14173
-#define SQLITE_PRINTF_SQLFUNC 0x02
14174
-SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
14175
-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
14210
+SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, const char*, va_list);
14211
+SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
1417614212
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
1417714213
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
1417814214
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
1417914215
SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
1418014216
#endif
@@ -14191,10 +14227,11 @@
1419114227
1419214228
1419314229
SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
1419414230
SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
1419514231
SQLITE_PRIVATE int sqlite3Dequote(char*);
14232
+SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
1419614233
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
1419714234
SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
1419814235
SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
1419914236
SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
1420014237
SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
@@ -14403,11 +14440,11 @@
1440314440
Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
1440414441
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
1440514442
SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
1440614443
SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
1440714444
SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
14408
- u8,u8,int,int*);
14445
+ u8,u8,int,int*,int*);
1440914446
SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
1441014447
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
1441114448
SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
1441214449
SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
1441314450
SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -14622,11 +14659,10 @@
1462214659
SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
1462314660
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
1462414661
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
1462514662
SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
1462614663
SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
14627
-SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
1462814664
SQLITE_PRIVATE void sqlite3SchemaClear(void *);
1462914665
SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
1463014666
SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
1463114667
SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
1463214668
SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
@@ -14638,10 +14674,12 @@
1463814674
SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
1463914675
void (*)(sqlite3_context*,int,sqlite3_value **),
1464014676
void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
1464114677
FuncDestructor *pDestructor
1464214678
);
14679
+SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
14680
+SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
1464314681
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
1464414682
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
1464514683
1464614684
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
1464714685
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
@@ -15734,18 +15772,20 @@
1573415772
** - On either an index or a table
1573515773
** * A sorter
1573615774
** * A virtual table
1573715775
** * A one-row "pseudotable" stored in a single register
1573815776
*/
15777
+typedef struct VdbeCursor VdbeCursor;
1573915778
struct VdbeCursor {
1574015779
u8 eCurType; /* One of the CURTYPE_* values above */
1574115780
i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
1574215781
u8 nullRow; /* True if pointing to a row with no data */
1574315782
u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
1574415783
u8 isTable; /* True for rowid tables. False for indexes */
1574515784
#ifdef SQLITE_DEBUG
1574615785
u8 seekOp; /* Most recent seek operation on this cursor */
15786
+ u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
1574715787
#endif
1574815788
Bool isEphemeral:1; /* True for an ephemeral table */
1574915789
Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
1575015790
Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
1575115791
Pgno pgnoRoot; /* Root page of the open btree cursor */
@@ -15760,10 +15800,12 @@
1576015800
Btree *pBt; /* Separate file holding temporary table */
1576115801
KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
1576215802
int seekResult; /* Result of previous sqlite3BtreeMoveto() */
1576315803
i64 seqCount; /* Sequence counter */
1576415804
i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
15805
+ VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
15806
+ int *aAltMap; /* Mapping from table to index column numbers */
1576515807
#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
1576615808
u64 maskUsed; /* Mask of columns used by this cursor */
1576715809
#endif
1576815810
1576915811
/* Cached information about the header for the data record that the
@@ -15784,11 +15826,10 @@
1578415826
u32 aType[1]; /* Type values for all entries in the record */
1578515827
/* 2*nField extra array elements allocated for aType[], beyond the one
1578615828
** static element declared in the structure. nField total array slots for
1578715829
** aType[] and nField+1 array slots for aOffset[] */
1578815830
};
15789
-typedef struct VdbeCursor VdbeCursor;
1579015831
1579115832
/*
1579215833
** When a sub-program is executed (OP_Program), a structure of this type
1579315834
** is allocated to store the current value of the program counter, as
1579415835
** well as the current memory cell array and various other frame specific
@@ -15895,11 +15936,11 @@
1589515936
#define MEM_AffMask 0x001f /* Mask of affinity bits */
1589615937
#define MEM_RowSet 0x0020 /* Value is a RowSet object */
1589715938
#define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
1589815939
#define MEM_Undefined 0x0080 /* Value is undefined */
1589915940
#define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
15900
-#define MEM_TypeMask 0x01ff /* Mask of type bits */
15941
+#define MEM_TypeMask 0x81ff /* Mask of type bits */
1590115942
1590215943
1590315944
/* Whenever Mem contains a valid string or blob representation, one of
1590415945
** the following flags must be set to determine the memory management
1590515946
** policy for Mem.z. The MEM_Term flag tells us whether or not the
@@ -15909,14 +15950,21 @@
1590915950
#define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
1591015951
#define MEM_Static 0x0800 /* Mem.z points to a static string */
1591115952
#define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
1591215953
#define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
1591315954
#define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
15955
+#define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
1591415956
#ifdef SQLITE_OMIT_INCRBLOB
1591515957
#undef MEM_Zero
1591615958
#define MEM_Zero 0x0000
1591715959
#endif
15960
+
15961
+/* Return TRUE if Mem X contains dynamically allocated content - anything
15962
+** that needs to be deallocated to avoid a leak.
15963
+*/
15964
+#define VdbeMemDynamic(X) \
15965
+ (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
1591815966
1591915967
/*
1592015968
** Clear any existing type flags from a Mem and replace them with f
1592115969
*/
1592215970
#define MemSetTypeFlag(p, f) \
@@ -16083,11 +16131,11 @@
1608316131
** Function prototypes
1608416132
*/
1608516133
SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
1608616134
SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
1608716135
void sqliteVdbePopStack(Vdbe*,int);
16088
-SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
16136
+SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
1608916137
SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
1609016138
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
1609116139
SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
1609216140
#endif
1609316141
SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
@@ -16129,12 +16177,10 @@
1612916177
SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
1613016178
SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
1613116179
SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
1613216180
SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
1613316181
SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
16134
-#define VdbeMemDynamic(X) \
16135
- (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
1613616182
SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
1613716183
SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
1613816184
SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
1613916185
SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
1614016186
SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
@@ -17492,11 +17538,11 @@
1749217538
z = zBuf;
1749317539
}else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
1749417540
sqlite3_result_error_toobig(context);
1749517541
return;
1749617542
}else{
17497
- z = sqlite3DbMallocRaw(db, (int)n);
17543
+ z = sqlite3DbMallocRawNN(db, (int)n);
1749817544
if( z==0 ){
1749917545
sqlite3_result_error_nomem(context);
1750017546
return;
1750117547
}
1750217548
}
@@ -17707,18 +17753,40 @@
1770717753
*/
1770817754
#define _SQLITE_OS_C_ 1
1770917755
/* #include "sqliteInt.h" */
1771017756
#undef _SQLITE_OS_C_
1771117757
17758
+/*
17759
+** If we compile with the SQLITE_TEST macro set, then the following block
17760
+** of code will give us the ability to simulate a disk I/O error. This
17761
+** is used for testing the I/O recovery logic.
17762
+*/
17763
+#if defined(SQLITE_TEST)
17764
+SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
17765
+SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
17766
+SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
17767
+SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
17768
+SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
17769
+SQLITE_API int sqlite3_diskfull_pending = 0;
17770
+SQLITE_API int sqlite3_diskfull = 0;
17771
+#endif /* defined(SQLITE_TEST) */
17772
+
17773
+/*
17774
+** When testing, also keep a count of the number of open files.
17775
+*/
17776
+#if defined(SQLITE_TEST)
17777
+SQLITE_API int sqlite3_open_file_count = 0;
17778
+#endif /* defined(SQLITE_TEST) */
17779
+
1771217780
/*
1771317781
** The default SQLite sqlite3_vfs implementations do not allocate
1771417782
** memory (actually, os_unix.c allocates a small amount of memory
1771517783
** from within OsOpen()), but some third-party implementations may.
1771617784
** So we test the effects of a malloc() failing and the sqlite3OsXXX()
1771717785
** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
1771817786
**
17719
-** The following functions are instrumented for malloc() failure
17787
+** The following functions are instrumented for malloc() failure
1772017788
** testing:
1772117789
**
1772217790
** sqlite3OsRead()
1772317791
** sqlite3OsWrite()
1772417792
** sqlite3OsSync()
@@ -17800,12 +17868,12 @@
1780017868
*/
1780117869
SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
1780217870
#ifdef SQLITE_TEST
1780317871
if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
1780417872
/* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
17805
- ** is using a regular VFS, it is called after the corresponding
17806
- ** transaction has been committed. Injecting a fault at this point
17873
+ ** is using a regular VFS, it is called after the corresponding
17874
+ ** transaction has been committed. Injecting a fault at this point
1780717875
** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
1780817876
** but the transaction is committed anyway.
1780917877
**
1781017878
** The core must call OsFileControl() though, not OsFileControlHint(),
1781117879
** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
@@ -17870,14 +17938,14 @@
1787017938
/*
1787117939
** The next group of routines are convenience wrappers around the
1787217940
** VFS methods.
1787317941
*/
1787417942
SQLITE_PRIVATE int sqlite3OsOpen(
17875
- sqlite3_vfs *pVfs,
17876
- const char *zPath,
17877
- sqlite3_file *pFile,
17878
- int flags,
17943
+ sqlite3_vfs *pVfs,
17944
+ const char *zPath,
17945
+ sqlite3_file *pFile,
17946
+ int flags,
1787917947
int *pFlagsOut
1788017948
){
1788117949
int rc;
1788217950
DO_OS_MALLOC_TEST(0);
1788317951
/* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
@@ -17892,22 +17960,22 @@
1789217960
DO_OS_MALLOC_TEST(0);
1789317961
assert( dirSync==0 || dirSync==1 );
1789417962
return pVfs->xDelete(pVfs, zPath, dirSync);
1789517963
}
1789617964
SQLITE_PRIVATE int sqlite3OsAccess(
17897
- sqlite3_vfs *pVfs,
17898
- const char *zPath,
17899
- int flags,
17965
+ sqlite3_vfs *pVfs,
17966
+ const char *zPath,
17967
+ int flags,
1790017968
int *pResOut
1790117969
){
1790217970
DO_OS_MALLOC_TEST(0);
1790317971
return pVfs->xAccess(pVfs, zPath, flags, pResOut);
1790417972
}
1790517973
SQLITE_PRIVATE int sqlite3OsFullPathname(
17906
- sqlite3_vfs *pVfs,
17907
- const char *zPath,
17908
- int nPathOut,
17974
+ sqlite3_vfs *pVfs,
17975
+ const char *zPath,
17976
+ int nPathOut,
1790917977
char *zPathOut
1791017978
){
1791117979
DO_OS_MALLOC_TEST(0);
1791217980
zPathOut[0] = 0;
1791317981
return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
@@ -17949,13 +18017,13 @@
1794918017
}
1795018018
return rc;
1795118019
}
1795218020
1795318021
SQLITE_PRIVATE int sqlite3OsOpenMalloc(
17954
- sqlite3_vfs *pVfs,
17955
- const char *zFile,
17956
- sqlite3_file **ppFile,
18022
+ sqlite3_vfs *pVfs,
18023
+ const char *zFile,
18024
+ sqlite3_file **ppFile,
1795718025
int flags,
1795818026
int *pOutFlags
1795918027
){
1796018028
int rc = SQLITE_NOMEM;
1796118029
sqlite3_file *pFile;
@@ -20081,15 +20149,15 @@
2008120149
mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
2008220150
while( ALWAYS(iLogsize<LOGMAX) ){
2008320151
int iBuddy;
2008420152
if( (iBlock>>iLogsize) & 1 ){
2008520153
iBuddy = iBlock - size;
20154
+ assert( iBuddy>=0 );
2008620155
}else{
2008720156
iBuddy = iBlock + size;
20157
+ if( iBuddy>=mem5.nBlock ) break;
2008820158
}
20089
- assert( iBuddy>=0 );
20090
- if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
2009120159
if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
2009220160
memsys5Unlink(iBuddy, iLogsize);
2009320161
iLogsize++;
2009420162
if( iBuddy<iBlock ){
2009520163
mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
@@ -21174,12 +21242,12 @@
2117421242
** Macros for performance tracing. Normally turned off. Only works
2117521243
** on i486 hardware.
2117621244
*/
2117721245
#ifdef SQLITE_PERFORMANCE_TRACE
2117821246
21179
-/*
21180
-** hwtime.h contains inline assembler code for implementing
21247
+/*
21248
+** hwtime.h contains inline assembler code for implementing
2118121249
** high-performance timing routines.
2118221250
*/
2118321251
/************** Include hwtime.h in the middle of os_common.h ****************/
2118421252
/************** Begin file hwtime.h ******************************************/
2118521253
/*
@@ -21285,18 +21353,18 @@
2128521353
/*
2128621354
** If we compile with the SQLITE_TEST macro set, then the following block
2128721355
** of code will give us the ability to simulate a disk I/O error. This
2128821356
** is used for testing the I/O recovery logic.
2128921357
*/
21290
-#ifdef SQLITE_TEST
21291
-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
21292
-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
21293
-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
21294
-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
21295
-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
21296
-SQLITE_API int sqlite3_diskfull_pending = 0;
21297
-SQLITE_API int sqlite3_diskfull = 0;
21358
+#if defined(SQLITE_TEST)
21359
+SQLITE_API extern int sqlite3_io_error_hit;
21360
+SQLITE_API extern int sqlite3_io_error_hardhit;
21361
+SQLITE_API extern int sqlite3_io_error_pending;
21362
+SQLITE_API extern int sqlite3_io_error_persist;
21363
+SQLITE_API extern int sqlite3_io_error_benign;
21364
+SQLITE_API extern int sqlite3_diskfull_pending;
21365
+SQLITE_API extern int sqlite3_diskfull;
2129821366
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
2129921367
#define SimulateIOError(CODE) \
2130021368
if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
2130121369
|| sqlite3_io_error_pending-- == 1 ) \
2130221370
{ local_ioerr(); CODE; }
@@ -21318,21 +21386,21 @@
2131821386
}
2131921387
#else
2132021388
#define SimulateIOErrorBenign(X)
2132121389
#define SimulateIOError(A)
2132221390
#define SimulateDiskfullError(A)
21323
-#endif
21391
+#endif /* defined(SQLITE_TEST) */
2132421392
2132521393
/*
2132621394
** When testing, keep a count of the number of open files.
2132721395
*/
21328
-#ifdef SQLITE_TEST
21329
-SQLITE_API int sqlite3_open_file_count = 0;
21396
+#if defined(SQLITE_TEST)
21397
+SQLITE_API extern int sqlite3_open_file_count;
2133021398
#define OpenCounter(X) sqlite3_open_file_count+=(X)
2133121399
#else
2133221400
#define OpenCounter(X)
21333
-#endif
21401
+#endif /* defined(SQLITE_TEST) */
2133421402
2133521403
#endif /* !defined(_OS_COMMON_H_) */
2133621404
2133721405
/************** End of os_common.h *******************************************/
2133821406
/************** Continuing where we left off in mutex_w32.c ******************/
@@ -22386,14 +22454,28 @@
2238622454
/*
2238722455
** Allocate and zero memory. If the allocation fails, make
2238822456
** the mallocFailed flag in the connection pointer.
2238922457
*/
2239022458
SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
22391
- void *p = sqlite3DbMallocRaw(db, n);
22392
- if( p ){
22393
- memset(p, 0, (size_t)n);
22394
- }
22459
+ void *p;
22460
+ testcase( db==0 );
22461
+ p = sqlite3DbMallocRaw(db, n);
22462
+ if( p ) memset(p, 0, (size_t)n);
22463
+ return p;
22464
+}
22465
+
22466
+
22467
+/* Finish the work of sqlite3DbMallocRawNN for the unusual and
22468
+** slower case when the allocation cannot be fulfilled using lookaside.
22469
+*/
22470
+static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
22471
+ void *p;
22472
+ assert( db!=0 );
22473
+ p = sqlite3Malloc(n);
22474
+ if( !p ) sqlite3OomFault(db);
22475
+ sqlite3MemdebugSetType(p,
22476
+ (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
2239522477
return p;
2239622478
}
2239722479
2239822480
/*
2239922481
** Allocate memory, either lookaside (if possible) or heap.
@@ -22411,71 +22493,77 @@
2241122493
** int *b = (int*)sqlite3DbMallocRaw(db, 200);
2241222494
** if( b ) a[10] = 9;
2241322495
**
2241422496
** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
2241522497
** that all prior mallocs (ex: "a") worked too.
22498
+**
22499
+** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
22500
+** not a NULL pointer.
2241622501
*/
22417
-static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n);
2241822502
SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
22419
- assert( db==0 || sqlite3_mutex_held(db->mutex) );
22420
- assert( db==0 || db->pnBytesFreed==0 );
22503
+ void *p;
22504
+ if( db ) return sqlite3DbMallocRawNN(db, n);
22505
+ p = sqlite3Malloc(n);
22506
+ sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
22507
+ return p;
22508
+}
22509
+SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
2242122510
#ifndef SQLITE_OMIT_LOOKASIDE
22422
- if( db ){
22423
- LookasideSlot *pBuf;
22424
- if( db->mallocFailed ){
22425
- return 0;
22426
- }
22427
- if( db->lookaside.bEnabled ){
22428
- if( n>db->lookaside.sz ){
22429
- db->lookaside.anStat[1]++;
22430
- }else if( (pBuf = db->lookaside.pFree)==0 ){
22431
- db->lookaside.anStat[2]++;
22432
- }else{
22433
- db->lookaside.pFree = pBuf->pNext;
22434
- db->lookaside.nOut++;
22435
- db->lookaside.anStat[0]++;
22436
- if( db->lookaside.nOut>db->lookaside.mxOut ){
22437
- db->lookaside.mxOut = db->lookaside.nOut;
22438
- }
22439
- return (void*)pBuf;
22440
- }
22441
- }
22511
+ LookasideSlot *pBuf;
22512
+ assert( db!=0 );
22513
+ assert( sqlite3_mutex_held(db->mutex) );
22514
+ assert( db->pnBytesFreed==0 );
22515
+ if( db->lookaside.bDisable==0 ){
22516
+ assert( db->mallocFailed==0 );
22517
+ if( n>db->lookaside.sz ){
22518
+ db->lookaside.anStat[1]++;
22519
+ }else if( (pBuf = db->lookaside.pFree)==0 ){
22520
+ db->lookaside.anStat[2]++;
22521
+ }else{
22522
+ db->lookaside.pFree = pBuf->pNext;
22523
+ db->lookaside.nOut++;
22524
+ db->lookaside.anStat[0]++;
22525
+ if( db->lookaside.nOut>db->lookaside.mxOut ){
22526
+ db->lookaside.mxOut = db->lookaside.nOut;
22527
+ }
22528
+ return (void*)pBuf;
22529
+ }
22530
+ }else if( db->mallocFailed ){
22531
+ return 0;
2244222532
}
2244322533
#else
22444
- if( db && db->mallocFailed ){
22534
+ assert( db!=0 );
22535
+ assert( sqlite3_mutex_held(db->mutex) );
22536
+ assert( db->pnBytesFreed==0 );
22537
+ if( db->mallocFailed ){
2244522538
return 0;
2244622539
}
2244722540
#endif
2244822541
return dbMallocRawFinish(db, n);
2244922542
}
22450
-static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
22451
- void *p = sqlite3Malloc(n);
22452
- if( !p && db ){
22453
- db->mallocFailed = 1;
22454
- }
22455
- sqlite3MemdebugSetType(p,
22456
- (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
22457
- return p;
22458
-}
22543
+
22544
+/* Forward declaration */
22545
+static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
2245922546
2246022547
/*
2246122548
** Resize the block of memory pointed to by p to n bytes. If the
2246222549
** resize fails, set the mallocFailed flag in the connection object.
2246322550
*/
2246422551
SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
22552
+ assert( db!=0 );
22553
+ if( p==0 ) return sqlite3DbMallocRawNN(db, n);
22554
+ assert( sqlite3_mutex_held(db->mutex) );
22555
+ if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
22556
+ return dbReallocFinish(db, p, n);
22557
+}
22558
+static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
2246522559
void *pNew = 0;
2246622560
assert( db!=0 );
22467
- assert( sqlite3_mutex_held(db->mutex) );
22561
+ assert( p!=0 );
2246822562
if( db->mallocFailed==0 ){
22469
- if( p==0 ){
22470
- return sqlite3DbMallocRaw(db, n);
22471
- }
2247222563
if( isLookaside(db, p) ){
22473
- if( n<=db->lookaside.sz ){
22474
- return p;
22475
- }
22476
- pNew = sqlite3DbMallocRaw(db, n);
22564
+ pNew = sqlite3DbMallocRawNN(db, n);
2247722565
if( pNew ){
2247822566
memcpy(pNew, p, db->lookaside.sz);
2247922567
sqlite3DbFree(db, p);
2248022568
}
2248122569
}else{
@@ -22482,14 +22570,14 @@
2248222570
assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2248322571
assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
2248422572
sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
2248522573
pNew = sqlite3_realloc64(p, n);
2248622574
if( !pNew ){
22487
- db->mallocFailed = 1;
22575
+ sqlite3OomFault(db);
2248822576
}
2248922577
sqlite3MemdebugSetType(pNew,
22490
- (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
22578
+ (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
2249122579
}
2249222580
}
2249322581
return pNew;
2249422582
}
2249522583
@@ -22527,15 +22615,16 @@
2252722615
}
2252822616
return zNew;
2252922617
}
2253022618
SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
2253122619
char *zNew;
22620
+ assert( db!=0 );
2253222621
if( z==0 ){
2253322622
return 0;
2253422623
}
2253522624
assert( (n&0x7fffffff)==n );
22536
- zNew = sqlite3DbMallocRaw(db, n+1);
22625
+ zNew = sqlite3DbMallocRawNN(db, n+1);
2253722626
if( zNew ){
2253822627
memcpy(zNew, z, (size_t)n);
2253922628
zNew[n] = 0;
2254022629
}
2254122630
return zNew;
@@ -22546,16 +22635,48 @@
2254622635
*/
2254722636
SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
2254822637
sqlite3DbFree(db, *pz);
2254922638
*pz = sqlite3DbStrDup(db, zNew);
2255022639
}
22640
+
22641
+/*
22642
+** Call this routine to record the fact that an OOM (out-of-memory) error
22643
+** has happened. This routine will set db->mallocFailed, and also
22644
+** temporarily disable the lookaside memory allocator and interrupt
22645
+** any running VDBEs.
22646
+*/
22647
+SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
22648
+ if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
22649
+ db->mallocFailed = 1;
22650
+ if( db->nVdbeExec>0 ){
22651
+ db->u1.isInterrupted = 1;
22652
+ }
22653
+ db->lookaside.bDisable++;
22654
+ }
22655
+}
22656
+
22657
+/*
22658
+** This routine reactivates the memory allocator and clears the
22659
+** db->mallocFailed flag as necessary.
22660
+**
22661
+** The memory allocator is not restarted if there are running
22662
+** VDBEs.
22663
+*/
22664
+SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
22665
+ if( db->mallocFailed && db->nVdbeExec==0 ){
22666
+ db->mallocFailed = 0;
22667
+ db->u1.isInterrupted = 0;
22668
+ assert( db->lookaside.bDisable>0 );
22669
+ db->lookaside.bDisable--;
22670
+ }
22671
+}
2255122672
2255222673
/*
2255322674
** Take actions at the end of an API call to indicate an OOM error
2255422675
*/
2255522676
static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
22556
- db->mallocFailed = 0;
22677
+ sqlite3OomClear(db);
2255722678
sqlite3Error(db, SQLITE_NOMEM);
2255822679
return SQLITE_NOMEM;
2255922680
}
2256022681
2256122682
/*
@@ -22756,11 +22877,10 @@
2275622877
/*
2275722878
** Render a string given by "fmt" into the StrAccum object.
2275822879
*/
2275922880
SQLITE_PRIVATE void sqlite3VXPrintf(
2276022881
StrAccum *pAccum, /* Accumulate results here */
22761
- u32 bFlags, /* SQLITE_PRINTF_* flags */
2276222882
const char *fmt, /* Format string */
2276322883
va_list ap /* arguments */
2276422884
){
2276522885
int c; /* Next character in the format string */
2276622886
char *bufpt; /* Pointer to the conversion buffer */
@@ -22796,15 +22916,15 @@
2279622916
#endif
2279722917
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
2279822918
char buf[etBUFSIZE]; /* Conversion buffer */
2279922919
2280022920
bufpt = 0;
22801
- if( bFlags ){
22802
- if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
22921
+ if( pAccum->printfFlags ){
22922
+ if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
2280322923
pArgList = va_arg(ap, PrintfArguments*);
2280422924
}
22805
- useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
22925
+ useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL;
2280622926
}else{
2280722927
bArgList = useIntern = 0;
2280822928
}
2280922929
for(; (c=(*fmt))!=0; ++fmt){
2281022930
if( c!='%' ){
@@ -23351,13 +23471,13 @@
2335123471
if( p->mxAlloc==0 ){
2335223472
N = p->nAlloc - p->nChar - 1;
2335323473
setStrAccumError(p, STRACCUM_TOOBIG);
2335423474
return N;
2335523475
}else{
23356
- char *zOld = p->bMalloced ? p->zText : 0;
23476
+ char *zOld = isMalloced(p) ? p->zText : 0;
2335723477
i64 szNew = p->nChar;
23358
- assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23478
+ assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
2335923479
szNew += N + 1;
2336023480
if( szNew+p->nChar<=p->mxAlloc ){
2336123481
/* Force exponential buffer size growth as long as it does not overflow,
2336223482
** to avoid having to call this routine too often */
2336323483
szNew += p->nChar;
@@ -23374,14 +23494,14 @@
2337423494
}else{
2337523495
zNew = sqlite3_realloc64(zOld, p->nAlloc);
2337623496
}
2337723497
if( zNew ){
2337823498
assert( p->zText!=0 || p->nChar==0 );
23379
- if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
23499
+ if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
2338023500
p->zText = zNew;
2338123501
p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
23382
- p->bMalloced = 1;
23502
+ p->printfFlags |= SQLITE_PRINTF_MALLOCED;
2338323503
}else{
2338423504
sqlite3StrAccumReset(p);
2338523505
setStrAccumError(p, STRACCUM_NOMEM);
2338623506
return 0;
2338723507
}
@@ -23395,11 +23515,11 @@
2339523515
SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
2339623516
testcase( p->nChar + (i64)N > 0x7fffffff );
2339723517
if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
2339823518
return;
2339923519
}
23400
- assert( (p->zText==p->zBase)==(p->bMalloced==0) );
23520
+ assert( (p->zText==p->zBase)==!isMalloced(p) );
2340123521
while( (N--)>0 ) p->zText[p->nChar++] = c;
2340223522
}
2340323523
2340423524
/*
2340523525
** The StrAccum "p" is not large enough to accept N new bytes of z[].
@@ -23413,11 +23533,11 @@
2341323533
N = sqlite3StrAccumEnlarge(p, N);
2341423534
if( N>0 ){
2341523535
memcpy(&p->zText[p->nChar], z, N);
2341623536
p->nChar += N;
2341723537
}
23418
- assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23538
+ assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
2341923539
}
2342023540
2342123541
/*
2342223542
** Append N bytes of text from z to the StrAccum object. Increase the
2342323543
** size of the memory allocation for StrAccum if necessary.
@@ -23449,17 +23569,17 @@
2344923569
** Return a pointer to the resulting string. Return a NULL
2345023570
** pointer if any kind of error was encountered.
2345123571
*/
2345223572
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
2345323573
if( p->zText ){
23454
- assert( (p->zText==p->zBase)==(p->bMalloced==0) );
23574
+ assert( (p->zText==p->zBase)==!isMalloced(p) );
2345523575
p->zText[p->nChar] = 0;
23456
- if( p->mxAlloc>0 && p->bMalloced==0 ){
23576
+ if( p->mxAlloc>0 && !isMalloced(p) ){
2345723577
p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
2345823578
if( p->zText ){
2345923579
memcpy(p->zText, p->zBase, p->nChar+1);
23460
- p->bMalloced = 1;
23580
+ p->printfFlags |= SQLITE_PRINTF_MALLOCED;
2346123581
}else{
2346223582
setStrAccumError(p, STRACCUM_NOMEM);
2346323583
}
2346423584
}
2346523585
}
@@ -23468,14 +23588,14 @@
2346823588
2346923589
/*
2347023590
** Reset an StrAccum string. Reclaim all malloced memory.
2347123591
*/
2347223592
SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
23473
- assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23474
- if( p->bMalloced ){
23593
+ assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
23594
+ if( isMalloced(p) ){
2347523595
sqlite3DbFree(p->db, p->zText);
23476
- p->bMalloced = 0;
23596
+ p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
2347723597
}
2347823598
p->zText = 0;
2347923599
}
2348023600
2348123601
/*
@@ -23497,11 +23617,11 @@
2349723617
p->db = db;
2349823618
p->nChar = 0;
2349923619
p->nAlloc = n;
2350023620
p->mxAlloc = mx;
2350123621
p->accError = 0;
23502
- p->bMalloced = 0;
23622
+ p->printfFlags = 0;
2350323623
}
2350423624
2350523625
/*
2350623626
** Print into memory obtained from sqliteMalloc(). Use the internal
2350723627
** %-conversion extensions.
@@ -23511,14 +23631,15 @@
2351123631
char zBase[SQLITE_PRINT_BUF_SIZE];
2351223632
StrAccum acc;
2351323633
assert( db!=0 );
2351423634
sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
2351523635
db->aLimit[SQLITE_LIMIT_LENGTH]);
23516
- sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
23636
+ acc.printfFlags = SQLITE_PRINTF_INTERNAL;
23637
+ sqlite3VXPrintf(&acc, zFormat, ap);
2351723638
z = sqlite3StrAccumFinish(&acc);
2351823639
if( acc.accError==STRACCUM_NOMEM ){
23519
- db->mallocFailed = 1;
23640
+ sqlite3OomFault(db);
2352023641
}
2352123642
return z;
2352223643
}
2352323644
2352423645
/*
@@ -23551,11 +23672,11 @@
2355123672
#endif
2355223673
#ifndef SQLITE_OMIT_AUTOINIT
2355323674
if( sqlite3_initialize() ) return 0;
2355423675
#endif
2355523676
sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
23556
- sqlite3VXPrintf(&acc, 0, zFormat, ap);
23677
+ sqlite3VXPrintf(&acc, zFormat, ap);
2355723678
z = sqlite3StrAccumFinish(&acc);
2355823679
return z;
2355923680
}
2356023681
2356123682
/*
@@ -23596,11 +23717,11 @@
2359623717
if( zBuf ) zBuf[0] = 0;
2359723718
return zBuf;
2359823719
}
2359923720
#endif
2360023721
sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
23601
- sqlite3VXPrintf(&acc, 0, zFormat, ap);
23722
+ sqlite3VXPrintf(&acc, zFormat, ap);
2360223723
return sqlite3StrAccumFinish(&acc);
2360323724
}
2360423725
SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
2360523726
char *z;
2360623727
va_list ap;
@@ -23627,11 +23748,11 @@
2362723748
static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
2362823749
StrAccum acc; /* String accumulator */
2362923750
char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
2363023751
2363123752
sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
23632
- sqlite3VXPrintf(&acc, 0, zFormat, ap);
23753
+ sqlite3VXPrintf(&acc, zFormat, ap);
2363323754
sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
2363423755
sqlite3StrAccumFinish(&acc));
2363523756
}
2363623757
2363723758
/*
@@ -23656,11 +23777,11 @@
2365623777
va_list ap;
2365723778
StrAccum acc;
2365823779
char zBuf[500];
2365923780
sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
2366023781
va_start(ap,zFormat);
23661
- sqlite3VXPrintf(&acc, 0, zFormat, ap);
23782
+ sqlite3VXPrintf(&acc, zFormat, ap);
2366223783
va_end(ap);
2366323784
sqlite3StrAccumFinish(&acc);
2366423785
fprintf(stdout,"%s", zBuf);
2366523786
fflush(stdout);
2366623787
}
@@ -23669,14 +23790,14 @@
2366923790
2367023791
/*
2367123792
** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
2367223793
** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
2367323794
*/
23674
-SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
23795
+SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
2367523796
va_list ap;
2367623797
va_start(ap,zFormat);
23677
- sqlite3VXPrintf(p, bFlags, zFormat, ap);
23798
+ sqlite3VXPrintf(p, zFormat, ap);
2367823799
va_end(ap);
2367923800
}
2368023801
2368123802
/************** End of printf.c **********************************************/
2368223803
/************** Begin file treeview.c ****************************************/
@@ -23743,11 +23864,11 @@
2374323864
sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
2374423865
}
2374523866
sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
2374623867
}
2374723868
va_start(ap, zFormat);
23748
- sqlite3VXPrintf(&acc, 0, zFormat, ap);
23869
+ sqlite3VXPrintf(&acc, zFormat, ap);
2374923870
va_end(ap);
2375023871
if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
2375123872
sqlite3StrAccumFinish(&acc);
2375223873
fprintf(stdout,"%s", zBuf);
2375323874
fflush(stdout);
@@ -23778,21 +23899,21 @@
2377823899
for(i=0; i<pWith->nCte; i++){
2377923900
StrAccum x;
2378023901
char zLine[1000];
2378123902
const struct Cte *pCte = &pWith->a[i];
2378223903
sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23783
- sqlite3XPrintf(&x, 0, "%s", pCte->zName);
23904
+ sqlite3XPrintf(&x, "%s", pCte->zName);
2378423905
if( pCte->pCols && pCte->pCols->nExpr>0 ){
2378523906
char cSep = '(';
2378623907
int j;
2378723908
for(j=0; j<pCte->pCols->nExpr; j++){
23788
- sqlite3XPrintf(&x, 0, "%c%s", cSep, pCte->pCols->a[j].zName);
23909
+ sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
2378923910
cSep = ',';
2379023911
}
23791
- sqlite3XPrintf(&x, 0, ")");
23912
+ sqlite3XPrintf(&x, ")");
2379223913
}
23793
- sqlite3XPrintf(&x, 0, " AS");
23914
+ sqlite3XPrintf(&x, " AS");
2379423915
sqlite3StrAccumFinish(&x);
2379523916
sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
2379623917
sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
2379723918
sqlite3TreeViewPop(pView);
2379823919
}
@@ -23839,24 +23960,24 @@
2383923960
for(i=0; i<p->pSrc->nSrc; i++){
2384023961
struct SrcList_item *pItem = &p->pSrc->a[i];
2384123962
StrAccum x;
2384223963
char zLine[100];
2384323964
sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23844
- sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
23965
+ sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
2384523966
if( pItem->zDatabase ){
23846
- sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
23967
+ sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
2384723968
}else if( pItem->zName ){
23848
- sqlite3XPrintf(&x, 0, " %s", pItem->zName);
23969
+ sqlite3XPrintf(&x, " %s", pItem->zName);
2384923970
}
2385023971
if( pItem->pTab ){
23851
- sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
23972
+ sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
2385223973
}
2385323974
if( pItem->zAlias ){
23854
- sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
23975
+ sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
2385523976
}
2385623977
if( pItem->fg.jointype & JT_LEFT ){
23857
- sqlite3XPrintf(&x, 0, " LEFT-JOIN");
23978
+ sqlite3XPrintf(&x, " LEFT-JOIN");
2385823979
}
2385923980
sqlite3StrAccumFinish(&x);
2386023981
sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
2386123982
if( pItem->pSelect ){
2386223983
sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
@@ -24899,11 +25020,11 @@
2489925020
*z = 0;
2490025021
assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
2490125022
2490225023
c = pMem->flags;
2490325024
sqlite3VdbeMemRelease(pMem);
24904
- pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
25025
+ pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
2490525026
pMem->enc = desiredEnc;
2490625027
pMem->z = (char*)zOut;
2490725028
pMem->zMalloc = pMem->z;
2490825029
pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
2490925030
@@ -25349,10 +25470,18 @@
2534925470
}
2535025471
}
2535125472
z[j] = 0;
2535225473
return j;
2535325474
}
25475
+
25476
+/*
25477
+** Generate a Token object from a string
25478
+*/
25479
+SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
25480
+ p->z = z;
25481
+ p->n = sqlite3Strlen30(z);
25482
+}
2535425483
2535525484
/* Convenient short-hand */
2535625485
#define UpperToLower sqlite3UpperToLower
2535725486
2535825487
/*
@@ -26258,11 +26387,11 @@
2625826387
*/
2625926388
SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
2626026389
char *zBlob;
2626126390
int i;
2626226391
26263
- zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
26392
+ zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
2626426393
n--;
2626526394
if( zBlob ){
2626626395
for(i=0; i<n; i+=2){
2626726396
zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
2626826397
}
@@ -26796,95 +26925,96 @@
2679626925
# define OpHelp(X) "\0" X
2679726926
#else
2679826927
# define OpHelp(X)
2679926928
#endif
2680026929
SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
26801
- static const char *const azName[] = { "?",
26802
- /* 1 */ "Savepoint" OpHelp(""),
26803
- /* 2 */ "AutoCommit" OpHelp(""),
26804
- /* 3 */ "Transaction" OpHelp(""),
26805
- /* 4 */ "SorterNext" OpHelp(""),
26806
- /* 5 */ "PrevIfOpen" OpHelp(""),
26807
- /* 6 */ "NextIfOpen" OpHelp(""),
26808
- /* 7 */ "Prev" OpHelp(""),
26809
- /* 8 */ "Next" OpHelp(""),
26810
- /* 9 */ "Checkpoint" OpHelp(""),
26811
- /* 10 */ "JournalMode" OpHelp(""),
26812
- /* 11 */ "Vacuum" OpHelp(""),
26813
- /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
26814
- /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"),
26815
- /* 14 */ "Goto" OpHelp(""),
26816
- /* 15 */ "Gosub" OpHelp(""),
26817
- /* 16 */ "Return" OpHelp(""),
26818
- /* 17 */ "InitCoroutine" OpHelp(""),
26819
- /* 18 */ "EndCoroutine" OpHelp(""),
26930
+ static const char *const azName[] = {
26931
+ /* 0 */ "Savepoint" OpHelp(""),
26932
+ /* 1 */ "AutoCommit" OpHelp(""),
26933
+ /* 2 */ "Transaction" OpHelp(""),
26934
+ /* 3 */ "SorterNext" OpHelp(""),
26935
+ /* 4 */ "PrevIfOpen" OpHelp(""),
26936
+ /* 5 */ "NextIfOpen" OpHelp(""),
26937
+ /* 6 */ "Prev" OpHelp(""),
26938
+ /* 7 */ "Next" OpHelp(""),
26939
+ /* 8 */ "Checkpoint" OpHelp(""),
26940
+ /* 9 */ "JournalMode" OpHelp(""),
26941
+ /* 10 */ "Vacuum" OpHelp(""),
26942
+ /* 11 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
26943
+ /* 12 */ "VUpdate" OpHelp("data=r[P3@P2]"),
26944
+ /* 13 */ "Goto" OpHelp(""),
26945
+ /* 14 */ "Gosub" OpHelp(""),
26946
+ /* 15 */ "Return" OpHelp(""),
26947
+ /* 16 */ "InitCoroutine" OpHelp(""),
26948
+ /* 17 */ "EndCoroutine" OpHelp(""),
26949
+ /* 18 */ "Yield" OpHelp(""),
2682026950
/* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
26821
- /* 20 */ "Yield" OpHelp(""),
26822
- /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
26823
- /* 22 */ "Halt" OpHelp(""),
26824
- /* 23 */ "Integer" OpHelp("r[P2]=P1"),
26825
- /* 24 */ "Int64" OpHelp("r[P2]=P4"),
26826
- /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
26827
- /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"),
26828
- /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"),
26829
- /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
26830
- /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
26831
- /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
26832
- /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
26833
- /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"),
26834
- /* 33 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
26835
- /* 34 */ "ResultRow" OpHelp("output=r[P1@P2]"),
26836
- /* 35 */ "CollSeq" OpHelp(""),
26837
- /* 36 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
26838
- /* 37 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
26839
- /* 38 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
26840
- /* 39 */ "MustBeInt" OpHelp(""),
26841
- /* 40 */ "RealAffinity" OpHelp(""),
26842
- /* 41 */ "Cast" OpHelp("affinity(r[P1])"),
26843
- /* 42 */ "Permutation" OpHelp(""),
26844
- /* 43 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
26845
- /* 44 */ "Jump" OpHelp(""),
26846
- /* 45 */ "Once" OpHelp(""),
26847
- /* 46 */ "If" OpHelp(""),
26848
- /* 47 */ "IfNot" OpHelp(""),
26849
- /* 48 */ "Column" OpHelp("r[P3]=PX"),
26850
- /* 49 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
26851
- /* 50 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
26852
- /* 51 */ "Count" OpHelp("r[P2]=count()"),
26853
- /* 52 */ "ReadCookie" OpHelp(""),
26854
- /* 53 */ "SetCookie" OpHelp(""),
26855
- /* 54 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
26856
- /* 55 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
26857
- /* 56 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
26858
- /* 57 */ "OpenAutoindex" OpHelp("nColumn=P2"),
26859
- /* 58 */ "OpenEphemeral" OpHelp("nColumn=P2"),
26860
- /* 59 */ "SorterOpen" OpHelp(""),
26861
- /* 60 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
26862
- /* 61 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
26863
- /* 62 */ "Close" OpHelp(""),
26864
- /* 63 */ "ColumnsUsed" OpHelp(""),
26865
- /* 64 */ "SeekLT" OpHelp("key=r[P3@P4]"),
26866
- /* 65 */ "SeekLE" OpHelp("key=r[P3@P4]"),
26867
- /* 66 */ "SeekGE" OpHelp("key=r[P3@P4]"),
26868
- /* 67 */ "SeekGT" OpHelp("key=r[P3@P4]"),
26869
- /* 68 */ "Seek" OpHelp("intkey=r[P2]"),
26870
- /* 69 */ "NoConflict" OpHelp("key=r[P3@P4]"),
26871
- /* 70 */ "NotFound" OpHelp("key=r[P3@P4]"),
26951
+ /* 20 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
26952
+ /* 21 */ "Halt" OpHelp(""),
26953
+ /* 22 */ "Integer" OpHelp("r[P2]=P1"),
26954
+ /* 23 */ "Int64" OpHelp("r[P2]=P4"),
26955
+ /* 24 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
26956
+ /* 25 */ "Null" OpHelp("r[P2..P3]=NULL"),
26957
+ /* 26 */ "SoftNull" OpHelp("r[P1]=NULL"),
26958
+ /* 27 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
26959
+ /* 28 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
26960
+ /* 29 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
26961
+ /* 30 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
26962
+ /* 31 */ "SCopy" OpHelp("r[P2]=r[P1]"),
26963
+ /* 32 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
26964
+ /* 33 */ "ResultRow" OpHelp("output=r[P1@P2]"),
26965
+ /* 34 */ "CollSeq" OpHelp(""),
26966
+ /* 35 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
26967
+ /* 36 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
26968
+ /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
26969
+ /* 38 */ "MustBeInt" OpHelp(""),
26970
+ /* 39 */ "RealAffinity" OpHelp(""),
26971
+ /* 40 */ "Cast" OpHelp("affinity(r[P1])"),
26972
+ /* 41 */ "Permutation" OpHelp(""),
26973
+ /* 42 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
26974
+ /* 43 */ "Jump" OpHelp(""),
26975
+ /* 44 */ "Once" OpHelp(""),
26976
+ /* 45 */ "If" OpHelp(""),
26977
+ /* 46 */ "IfNot" OpHelp(""),
26978
+ /* 47 */ "Column" OpHelp("r[P3]=PX"),
26979
+ /* 48 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
26980
+ /* 49 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
26981
+ /* 50 */ "Count" OpHelp("r[P2]=count()"),
26982
+ /* 51 */ "ReadCookie" OpHelp(""),
26983
+ /* 52 */ "SetCookie" OpHelp(""),
26984
+ /* 53 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
26985
+ /* 54 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
26986
+ /* 55 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
26987
+ /* 56 */ "OpenAutoindex" OpHelp("nColumn=P2"),
26988
+ /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"),
26989
+ /* 58 */ "SorterOpen" OpHelp(""),
26990
+ /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
26991
+ /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
26992
+ /* 61 */ "Close" OpHelp(""),
26993
+ /* 62 */ "ColumnsUsed" OpHelp(""),
26994
+ /* 63 */ "SeekLT" OpHelp("key=r[P3@P4]"),
26995
+ /* 64 */ "SeekLE" OpHelp("key=r[P3@P4]"),
26996
+ /* 65 */ "SeekGE" OpHelp("key=r[P3@P4]"),
26997
+ /* 66 */ "SeekGT" OpHelp("key=r[P3@P4]"),
26998
+ /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"),
26999
+ /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"),
27000
+ /* 69 */ "Found" OpHelp("key=r[P3@P4]"),
27001
+ /* 70 */ "NotExists" OpHelp("intkey=r[P3]"),
2687227002
/* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
2687327003
/* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
26874
- /* 73 */ "Found" OpHelp("key=r[P3@P4]"),
26875
- /* 74 */ "NotExists" OpHelp("intkey=r[P3]"),
26876
- /* 75 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
27004
+ /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
27005
+ /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"),
27006
+ /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
2687727007
/* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
2687827008
/* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
2687927009
/* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
2688027010
/* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
2688127011
/* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
2688227012
/* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
2688327013
/* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
2688427014
/* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
26885
- /* 84 */ "NewRowid" OpHelp("r[P2]=rowid"),
27015
+ /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
2688627016
/* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
2688727017
/* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
2688827018
/* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
2688927019
/* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
2689027020
/* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -26891,78 +27021,77 @@
2689127021
/* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
2689227022
/* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
2689327023
/* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
2689427024
/* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
2689527025
/* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
26896
- /* 95 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
27026
+ /* 95 */ "Delete" OpHelp(""),
2689727027
/* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
2689827028
/* 97 */ "String8" OpHelp("r[P2]='P4'"),
26899
- /* 98 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
26900
- /* 99 */ "Delete" OpHelp(""),
26901
- /* 100 */ "ResetCount" OpHelp(""),
26902
- /* 101 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
26903
- /* 102 */ "SorterData" OpHelp("r[P2]=data"),
26904
- /* 103 */ "RowKey" OpHelp("r[P2]=key"),
26905
- /* 104 */ "RowData" OpHelp("r[P2]=data"),
26906
- /* 105 */ "Rowid" OpHelp("r[P2]=rowid"),
26907
- /* 106 */ "NullRow" OpHelp(""),
26908
- /* 107 */ "Last" OpHelp(""),
26909
- /* 108 */ "SorterSort" OpHelp(""),
26910
- /* 109 */ "Sort" OpHelp(""),
26911
- /* 110 */ "Rewind" OpHelp(""),
26912
- /* 111 */ "SorterInsert" OpHelp(""),
26913
- /* 112 */ "IdxInsert" OpHelp("key=r[P2]"),
26914
- /* 113 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
26915
- /* 114 */ "IdxRowid" OpHelp("r[P2]=rowid"),
26916
- /* 115 */ "IdxLE" OpHelp("key=r[P3@P4]"),
26917
- /* 116 */ "IdxGT" OpHelp("key=r[P3@P4]"),
26918
- /* 117 */ "IdxLT" OpHelp("key=r[P3@P4]"),
26919
- /* 118 */ "IdxGE" OpHelp("key=r[P3@P4]"),
26920
- /* 119 */ "Destroy" OpHelp(""),
26921
- /* 120 */ "Clear" OpHelp(""),
26922
- /* 121 */ "ResetSorter" OpHelp(""),
26923
- /* 122 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
26924
- /* 123 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
26925
- /* 124 */ "ParseSchema" OpHelp(""),
26926
- /* 125 */ "LoadAnalysis" OpHelp(""),
26927
- /* 126 */ "DropTable" OpHelp(""),
26928
- /* 127 */ "DropIndex" OpHelp(""),
26929
- /* 128 */ "DropTrigger" OpHelp(""),
26930
- /* 129 */ "IntegrityCk" OpHelp(""),
26931
- /* 130 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
26932
- /* 131 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
26933
- /* 132 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
27029
+ /* 98 */ "ResetCount" OpHelp(""),
27030
+ /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
27031
+ /* 100 */ "SorterData" OpHelp("r[P2]=data"),
27032
+ /* 101 */ "RowKey" OpHelp("r[P2]=key"),
27033
+ /* 102 */ "RowData" OpHelp("r[P2]=data"),
27034
+ /* 103 */ "Rowid" OpHelp("r[P2]=rowid"),
27035
+ /* 104 */ "NullRow" OpHelp(""),
27036
+ /* 105 */ "Last" OpHelp(""),
27037
+ /* 106 */ "SorterSort" OpHelp(""),
27038
+ /* 107 */ "Sort" OpHelp(""),
27039
+ /* 108 */ "Rewind" OpHelp(""),
27040
+ /* 109 */ "SorterInsert" OpHelp(""),
27041
+ /* 110 */ "IdxInsert" OpHelp("key=r[P2]"),
27042
+ /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
27043
+ /* 112 */ "Seek" OpHelp("Move P3 to P1.rowid"),
27044
+ /* 113 */ "IdxRowid" OpHelp("r[P2]=rowid"),
27045
+ /* 114 */ "IdxLE" OpHelp("key=r[P3@P4]"),
27046
+ /* 115 */ "IdxGT" OpHelp("key=r[P3@P4]"),
27047
+ /* 116 */ "IdxLT" OpHelp("key=r[P3@P4]"),
27048
+ /* 117 */ "IdxGE" OpHelp("key=r[P3@P4]"),
27049
+ /* 118 */ "Destroy" OpHelp(""),
27050
+ /* 119 */ "Clear" OpHelp(""),
27051
+ /* 120 */ "ResetSorter" OpHelp(""),
27052
+ /* 121 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
27053
+ /* 122 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
27054
+ /* 123 */ "ParseSchema" OpHelp(""),
27055
+ /* 124 */ "LoadAnalysis" OpHelp(""),
27056
+ /* 125 */ "DropTable" OpHelp(""),
27057
+ /* 126 */ "DropIndex" OpHelp(""),
27058
+ /* 127 */ "DropTrigger" OpHelp(""),
27059
+ /* 128 */ "IntegrityCk" OpHelp(""),
27060
+ /* 129 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
27061
+ /* 130 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
27062
+ /* 131 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
27063
+ /* 132 */ "Program" OpHelp(""),
2693427064
/* 133 */ "Real" OpHelp("r[P2]=P4"),
26935
- /* 134 */ "Program" OpHelp(""),
26936
- /* 135 */ "Param" OpHelp(""),
26937
- /* 136 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
26938
- /* 137 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
26939
- /* 138 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
26940
- /* 139 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
26941
- /* 140 */ "SetIfNotPos" OpHelp("if r[P1]<=0 then r[P2]=P3"),
26942
- /* 141 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
26943
- /* 142 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
26944
- /* 143 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
26945
- /* 144 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
26946
- /* 145 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
26947
- /* 146 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
26948
- /* 147 */ "IncrVacuum" OpHelp(""),
26949
- /* 148 */ "Expire" OpHelp(""),
26950
- /* 149 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
26951
- /* 150 */ "VBegin" OpHelp(""),
26952
- /* 151 */ "VCreate" OpHelp(""),
26953
- /* 152 */ "VDestroy" OpHelp(""),
26954
- /* 153 */ "VOpen" OpHelp(""),
26955
- /* 154 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
26956
- /* 155 */ "VNext" OpHelp(""),
26957
- /* 156 */ "VRename" OpHelp(""),
26958
- /* 157 */ "Pagecount" OpHelp(""),
26959
- /* 158 */ "MaxPgcnt" OpHelp(""),
26960
- /* 159 */ "Init" OpHelp("Start at P2"),
26961
- /* 160 */ "CursorHint" OpHelp(""),
26962
- /* 161 */ "Noop" OpHelp(""),
26963
- /* 162 */ "Explain" OpHelp(""),
27065
+ /* 134 */ "Param" OpHelp(""),
27066
+ /* 135 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
27067
+ /* 136 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
27068
+ /* 137 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
27069
+ /* 138 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
27070
+ /* 139 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
27071
+ /* 140 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
27072
+ /* 141 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
27073
+ /* 142 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
27074
+ /* 143 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
27075
+ /* 144 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
27076
+ /* 145 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
27077
+ /* 146 */ "IncrVacuum" OpHelp(""),
27078
+ /* 147 */ "Expire" OpHelp(""),
27079
+ /* 148 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
27080
+ /* 149 */ "VBegin" OpHelp(""),
27081
+ /* 150 */ "VCreate" OpHelp(""),
27082
+ /* 151 */ "VDestroy" OpHelp(""),
27083
+ /* 152 */ "VOpen" OpHelp(""),
27084
+ /* 153 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
27085
+ /* 154 */ "VNext" OpHelp(""),
27086
+ /* 155 */ "VRename" OpHelp(""),
27087
+ /* 156 */ "Pagecount" OpHelp(""),
27088
+ /* 157 */ "MaxPgcnt" OpHelp(""),
27089
+ /* 158 */ "Init" OpHelp("Start at P2"),
27090
+ /* 159 */ "CursorHint" OpHelp(""),
27091
+ /* 160 */ "Noop" OpHelp(""),
27092
+ /* 161 */ "Explain" OpHelp(""),
2696427093
};
2696527094
return azName[i];
2696627095
}
2696727096
#endif
2696827097
@@ -27117,10 +27246,15 @@
2711727246
/*
2711827247
** Maximum supported path-length.
2711927248
*/
2712027249
#define MAX_PATHNAME 512
2712127250
27251
+/*
27252
+** Maximum supported symbolic links
27253
+*/
27254
+#define SQLITE_MAX_SYMLINKS 100
27255
+
2712227256
/* Always cast the getpid() return type for compatibility with
2712327257
** kernel modules in VxWorks. */
2712427258
#define osGetpid(X) (pid_t)getpid()
2712527259
2712627260
/*
@@ -27269,12 +27403,12 @@
2726927403
** Macros for performance tracing. Normally turned off. Only works
2727027404
** on i486 hardware.
2727127405
*/
2727227406
#ifdef SQLITE_PERFORMANCE_TRACE
2727327407
27274
-/*
27275
-** hwtime.h contains inline assembler code for implementing
27408
+/*
27409
+** hwtime.h contains inline assembler code for implementing
2727627410
** high-performance timing routines.
2727727411
*/
2727827412
/************** Include hwtime.h in the middle of os_common.h ****************/
2727927413
/************** Begin file hwtime.h ******************************************/
2728027414
/*
@@ -27380,18 +27514,18 @@
2738027514
/*
2738127515
** If we compile with the SQLITE_TEST macro set, then the following block
2738227516
** of code will give us the ability to simulate a disk I/O error. This
2738327517
** is used for testing the I/O recovery logic.
2738427518
*/
27385
-#ifdef SQLITE_TEST
27386
-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
27387
-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
27388
-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
27389
-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
27390
-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
27391
-SQLITE_API int sqlite3_diskfull_pending = 0;
27392
-SQLITE_API int sqlite3_diskfull = 0;
27519
+#if defined(SQLITE_TEST)
27520
+SQLITE_API extern int sqlite3_io_error_hit;
27521
+SQLITE_API extern int sqlite3_io_error_hardhit;
27522
+SQLITE_API extern int sqlite3_io_error_pending;
27523
+SQLITE_API extern int sqlite3_io_error_persist;
27524
+SQLITE_API extern int sqlite3_io_error_benign;
27525
+SQLITE_API extern int sqlite3_diskfull_pending;
27526
+SQLITE_API extern int sqlite3_diskfull;
2739327527
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
2739427528
#define SimulateIOError(CODE) \
2739527529
if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
2739627530
|| sqlite3_io_error_pending-- == 1 ) \
2739727531
{ local_ioerr(); CODE; }
@@ -27413,21 +27547,21 @@
2741327547
}
2741427548
#else
2741527549
#define SimulateIOErrorBenign(X)
2741627550
#define SimulateIOError(A)
2741727551
#define SimulateDiskfullError(A)
27418
-#endif
27552
+#endif /* defined(SQLITE_TEST) */
2741927553
2742027554
/*
2742127555
** When testing, keep a count of the number of open files.
2742227556
*/
27423
-#ifdef SQLITE_TEST
27424
-SQLITE_API int sqlite3_open_file_count = 0;
27557
+#if defined(SQLITE_TEST)
27558
+SQLITE_API extern int sqlite3_open_file_count;
2742527559
#define OpenCounter(X) sqlite3_open_file_count+=(X)
2742627560
#else
2742727561
#define OpenCounter(X)
27428
-#endif
27562
+#endif /* defined(SQLITE_TEST) */
2742927563
2743027564
#endif /* !defined(_OS_COMMON_H_) */
2743127565
2743227566
/************** End of os_common.h *******************************************/
2743327567
/************** Continuing where we left off in os_unix.c ********************/
@@ -27641,10 +27775,16 @@
2764127775
#else
2764227776
{ "readlink", (sqlite3_syscall_ptr)0, 0 },
2764327777
#endif
2764427778
#define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
2764527779
27780
+#if defined(HAVE_LSTAT)
27781
+ { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
27782
+#else
27783
+ { "lstat", (sqlite3_syscall_ptr)0, 0 },
27784
+#endif
27785
+#define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
2764627786
2764727787
}; /* End of the overrideable system calls */
2764827788
2764927789
2765027790
/*
@@ -33042,16 +33182,11 @@
3304233182
#ifndef SQLITE_DISABLE_DIRSYNC
3304333183
if( (dirSync & 1)!=0 ){
3304433184
int fd;
3304533185
rc = osOpenDirectory(zPath, &fd);
3304633186
if( rc==SQLITE_OK ){
33047
-#if OS_VXWORKS
33048
- if( fsync(fd)==-1 )
33049
-#else
33050
- if( fsync(fd) )
33051
-#endif
33052
- {
33187
+ if( full_fsync(fd,0,0) ){
3305333188
rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
3305433189
}
3305533190
robust_close(0, fd, __LINE__);
3305633191
}else{
3305733192
assert( rc==SQLITE_CANTOPEN );
@@ -33093,10 +33228,36 @@
3309333228
*pResOut = osAccess(zPath, W_OK|R_OK)==0;
3309433229
}
3309533230
return SQLITE_OK;
3309633231
}
3309733232
33233
+/*
33234
+**
33235
+*/
33236
+static int mkFullPathname(
33237
+ const char *zPath, /* Input path */
33238
+ char *zOut, /* Output buffer */
33239
+ int nOut /* Allocated size of buffer zOut */
33240
+){
33241
+ int nPath = sqlite3Strlen30(zPath);
33242
+ int iOff = 0;
33243
+ if( zPath[0]!='/' ){
33244
+ if( osGetcwd(zOut, nOut-2)==0 ){
33245
+ return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
33246
+ }
33247
+ iOff = sqlite3Strlen30(zOut);
33248
+ zOut[iOff++] = '/';
33249
+ }
33250
+ if( (iOff+nPath+1)>nOut ){
33251
+ /* SQLite assumes that xFullPathname() nul-terminates the output buffer
33252
+ ** even if it returns an error. */
33253
+ zOut[iOff] = '\0';
33254
+ return SQLITE_CANTOPEN_BKPT;
33255
+ }
33256
+ sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
33257
+ return SQLITE_OK;
33258
+}
3309833259
3309933260
/*
3310033261
** Turn a relative pathname into a full pathname. The relative path
3310133262
** is stored as a nul-terminated string in the buffer pointed to by
3310233263
** zPath.
@@ -33109,73 +33270,85 @@
3310933270
sqlite3_vfs *pVfs, /* Pointer to vfs object */
3311033271
const char *zPath, /* Possibly relative input path */
3311133272
int nOut, /* Size of output buffer in bytes */
3311233273
char *zOut /* Output buffer */
3311333274
){
33275
+#if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
33276
+ return mkFullPathname(zPath, zOut, nOut);
33277
+#else
33278
+ int rc = SQLITE_OK;
3311433279
int nByte;
33280
+ int nLink = 1; /* Number of symbolic links followed so far */
33281
+ const char *zIn = zPath; /* Input path for each iteration of loop */
33282
+ char *zDel = 0;
33283
+
33284
+ assert( pVfs->mxPathname==MAX_PATHNAME );
33285
+ UNUSED_PARAMETER(pVfs);
3311533286
3311633287
/* It's odd to simulate an io-error here, but really this is just
3311733288
** using the io-error infrastructure to test that SQLite handles this
3311833289
** function failing. This function could fail if, for example, the
3311933290
** current working directory has been unlinked.
3312033291
*/
3312133292
SimulateIOError( return SQLITE_ERROR );
3312233293
33123
- assert( pVfs->mxPathname==MAX_PATHNAME );
33124
- UNUSED_PARAMETER(pVfs);
33125
-
33126
-#if defined(HAVE_READLINK)
33127
- /* Attempt to resolve the path as if it were a symbolic link. If it is
33128
- ** a symbolic link, the resolved path is stored in buffer zOut[]. Or, if
33129
- ** the identified file is not a symbolic link or does not exist, then
33130
- ** zPath is copied directly into zOut. Either way, nByte is left set to
33131
- ** the size of the string copied into zOut[] in bytes. */
33132
- nByte = osReadlink(zPath, zOut, nOut-1);
33133
- if( nByte<0 ){
33134
- if( errno!=EINVAL && errno!=ENOENT ){
33135
- return unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zPath);
33136
- }
33137
- sqlite3_snprintf(nOut, zOut, "%s", zPath);
33138
- nByte = sqlite3Strlen30(zOut);
33139
- }else{
33140
- zOut[nByte] = '\0';
33141
- }
33142
-#endif
33143
-
33144
- /* If buffer zOut[] now contains an absolute path there is nothing more
33145
- ** to do. If it contains a relative path, do the following:
33146
- **
33147
- ** * move the relative path string so that it is at the end of th
33148
- ** zOut[] buffer.
33149
- ** * Call getcwd() to read the path of the current working directory
33150
- ** into the start of the zOut[] buffer.
33151
- ** * Append a '/' character to the cwd string and move the
33152
- ** relative path back within the buffer so that it immediately
33153
- ** follows the '/'.
33154
- **
33155
- ** This code is written so that if the combination of the CWD and relative
33156
- ** path are larger than the allocated size of zOut[] the CWD is silently
33157
- ** truncated to make it fit. This is Ok, as SQLite refuses to open any
33158
- ** file for which this function returns a full path larger than (nOut-8)
33159
- ** bytes in size. */
33160
- testcase( nByte==nOut-5 );
33161
- testcase( nByte==nOut-4 );
33162
- if( zOut[0]!='/' && nByte<nOut-4 ){
33163
- int nCwd;
33164
- int nRem = nOut-nByte-1;
33165
- memmove(&zOut[nRem], zOut, nByte+1);
33166
- zOut[nRem-1] = '\0';
33167
- if( osGetcwd(zOut, nRem-1)==0 ){
33168
- return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
33169
- }
33170
- nCwd = sqlite3Strlen30(zOut);
33171
- assert( nCwd<=nRem-1 );
33172
- zOut[nCwd] = '/';
33173
- memmove(&zOut[nCwd+1], &zOut[nRem], nByte+1);
33174
- }
33175
-
33176
- return SQLITE_OK;
33294
+ do {
33295
+
33296
+ /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
33297
+ ** link, or false otherwise. */
33298
+ int bLink = 0;
33299
+ struct stat buf;
33300
+ if( osLstat(zIn, &buf)!=0 ){
33301
+ if( errno!=ENOENT ){
33302
+ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
33303
+ }
33304
+ }else{
33305
+ bLink = S_ISLNK(buf.st_mode);
33306
+ }
33307
+
33308
+ if( bLink ){
33309
+ if( zDel==0 ){
33310
+ zDel = sqlite3_malloc(nOut);
33311
+ if( zDel==0 ) rc = SQLITE_NOMEM;
33312
+ }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
33313
+ rc = SQLITE_CANTOPEN_BKPT;
33314
+ }
33315
+
33316
+ if( rc==SQLITE_OK ){
33317
+ nByte = osReadlink(zIn, zDel, nOut-1);
33318
+ if( nByte<0 ){
33319
+ rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
33320
+ }else{
33321
+ if( zDel[0]!='/' ){
33322
+ int n;
33323
+ for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
33324
+ if( nByte+n+1>nOut ){
33325
+ rc = SQLITE_CANTOPEN_BKPT;
33326
+ }else{
33327
+ memmove(&zDel[n], zDel, nByte+1);
33328
+ memcpy(zDel, zIn, n);
33329
+ nByte += n;
33330
+ }
33331
+ }
33332
+ zDel[nByte] = '\0';
33333
+ }
33334
+ }
33335
+
33336
+ zIn = zDel;
33337
+ }
33338
+
33339
+ assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
33340
+ if( rc==SQLITE_OK && zIn!=zOut ){
33341
+ rc = mkFullPathname(zIn, zOut, nOut);
33342
+ }
33343
+ if( bLink==0 ) break;
33344
+ zIn = zOut;
33345
+ }while( rc==SQLITE_OK );
33346
+
33347
+ sqlite3_free(zDel);
33348
+ return rc;
33349
+#endif /* HAVE_READLINK && HAVE_LSTAT */
3317733350
}
3317833351
3317933352
3318033353
#ifndef SQLITE_OMIT_LOAD_EXTENSION
3318133354
/*
@@ -33353,11 +33526,11 @@
3335333526
#endif
3335433527
UNUSED_PARAMETER(NotUsed);
3335533528
return rc;
3335633529
}
3335733530
33358
-#if 0 /* Not used */
33531
+#ifndef SQLITE_OMIT_DEPRECATED
3335933532
/*
3336033533
** Find the current time (in Universal Coordinated Time). Write the
3336133534
** current time and date as a Julian Day number into *prNow and
3336233535
** return 0. Return 1 if the time and date cannot be found.
3336333536
*/
@@ -33371,11 +33544,11 @@
3337133544
}
3337233545
#else
3337333546
# define unixCurrentTime 0
3337433547
#endif
3337533548
33376
-#if 0 /* Not used */
33549
+#ifndef SQLITE_OMIT_DEPRECATED
3337733550
/*
3337833551
** We added the xGetLastError() method with the intention of providing
3337933552
** better low-level error messages when operating-system problems come up
3338033553
** during SQLite operation. But so far, none of that has been implemented
3338133554
** in the core. So this routine is never called. For now, it is merely
@@ -34053,11 +34226,11 @@
3405334226
strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
3405434227
}
3405534228
writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
3405634229
robust_ftruncate(conchFile->h, writeSize);
3405734230
rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
34058
- fsync(conchFile->h);
34231
+ full_fsync(conchFile->h,0,0);
3405934232
/* If we created a new conch file (not just updated the contents of a
3406034233
** valid conch file), try to match the permissions of the database
3406134234
*/
3406234235
if( rc==SQLITE_OK && createConch ){
3406334236
struct stat buf;
@@ -34670,11 +34843,11 @@
3467034843
};
3467134844
unsigned int i; /* Loop counter */
3467234845
3467334846
/* Double-check that the aSyscall[] array has been constructed
3467434847
** correctly. See ticket [bb3a86e890c8e96ab] */
34675
- assert( ArraySize(aSyscall)==27 );
34848
+ assert( ArraySize(aSyscall)==28 );
3467634849
3467734850
/* Register all VFSes defined in the aVfs[] array */
3467834851
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
3467934852
sqlite3_vfs_register(&aVfs[i], i==0);
3468034853
}
@@ -34753,12 +34926,12 @@
3475334926
** Macros for performance tracing. Normally turned off. Only works
3475434927
** on i486 hardware.
3475534928
*/
3475634929
#ifdef SQLITE_PERFORMANCE_TRACE
3475734930
34758
-/*
34759
-** hwtime.h contains inline assembler code for implementing
34931
+/*
34932
+** hwtime.h contains inline assembler code for implementing
3476034933
** high-performance timing routines.
3476134934
*/
3476234935
/************** Include hwtime.h in the middle of os_common.h ****************/
3476334936
/************** Begin file hwtime.h ******************************************/
3476434937
/*
@@ -34864,18 +35037,18 @@
3486435037
/*
3486535038
** If we compile with the SQLITE_TEST macro set, then the following block
3486635039
** of code will give us the ability to simulate a disk I/O error. This
3486735040
** is used for testing the I/O recovery logic.
3486835041
*/
34869
-#ifdef SQLITE_TEST
34870
-SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
34871
-SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
34872
-SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
34873
-SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
34874
-SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
34875
-SQLITE_API int sqlite3_diskfull_pending = 0;
34876
-SQLITE_API int sqlite3_diskfull = 0;
35042
+#if defined(SQLITE_TEST)
35043
+SQLITE_API extern int sqlite3_io_error_hit;
35044
+SQLITE_API extern int sqlite3_io_error_hardhit;
35045
+SQLITE_API extern int sqlite3_io_error_pending;
35046
+SQLITE_API extern int sqlite3_io_error_persist;
35047
+SQLITE_API extern int sqlite3_io_error_benign;
35048
+SQLITE_API extern int sqlite3_diskfull_pending;
35049
+SQLITE_API extern int sqlite3_diskfull;
3487735050
#define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
3487835051
#define SimulateIOError(CODE) \
3487935052
if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
3488035053
|| sqlite3_io_error_pending-- == 1 ) \
3488135054
{ local_ioerr(); CODE; }
@@ -34897,21 +35070,21 @@
3489735070
}
3489835071
#else
3489935072
#define SimulateIOErrorBenign(X)
3490035073
#define SimulateIOError(A)
3490135074
#define SimulateDiskfullError(A)
34902
-#endif
35075
+#endif /* defined(SQLITE_TEST) */
3490335076
3490435077
/*
3490535078
** When testing, keep a count of the number of open files.
3490635079
*/
34907
-#ifdef SQLITE_TEST
34908
-SQLITE_API int sqlite3_open_file_count = 0;
35080
+#if defined(SQLITE_TEST)
35081
+SQLITE_API extern int sqlite3_open_file_count;
3490935082
#define OpenCounter(X) sqlite3_open_file_count+=(X)
3491035083
#else
3491135084
#define OpenCounter(X)
34912
-#endif
35085
+#endif /* defined(SQLITE_TEST) */
3491335086
3491435087
#endif /* !defined(_OS_COMMON_H_) */
3491535088
3491635089
/************** End of os_common.h *******************************************/
3491735090
/************** Continuing where we left off in os_win.c *********************/
@@ -34970,10 +35143,14 @@
3497035143
3497135144
#ifndef NTDDI_WINBLUE
3497235145
# define NTDDI_WINBLUE 0x06030000
3497335146
#endif
3497435147
35148
+#ifndef NTDDI_WINTHRESHOLD
35149
+# define NTDDI_WINTHRESHOLD 0x06040000
35150
+#endif
35151
+
3497535152
/*
3497635153
** Check to see if the GetVersionEx[AW] functions are deprecated on the
3497735154
** target system. GetVersionEx was first deprecated in Win8.1.
3497835155
*/
3497935156
#ifndef SQLITE_WIN32_GETVERSIONEX
@@ -34982,10 +35159,23 @@
3498235159
# else
3498335160
# define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
3498435161
# endif
3498535162
#endif
3498635163
35164
+/*
35165
+** Check to see if the CreateFileMappingA function is supported on the
35166
+** target system. It is unavailable when using "mincore.lib" on Win10.
35167
+** When compiling for Windows 10, always assume "mincore.lib" is in use.
35168
+*/
35169
+#ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
35170
+# if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
35171
+# define SQLITE_WIN32_CREATEFILEMAPPINGA 0
35172
+# else
35173
+# define SQLITE_WIN32_CREATEFILEMAPPINGA 1
35174
+# endif
35175
+#endif
35176
+
3498735177
/*
3498835178
** This constant should already be defined (in the "WinDef.h" SDK file).
3498935179
*/
3499035180
#ifndef MAX_PATH
3499135181
# define MAX_PATH (260)
@@ -35388,12 +35578,13 @@
3538835578
#endif
3538935579
3539035580
#define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
3539135581
LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
3539235582
35393
-#if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
35394
- (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
35583
+#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
35584
+ (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
35585
+ SQLITE_WIN32_CREATEFILEMAPPINGA
3539535586
{ "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
3539635587
#else
3539735588
{ "CreateFileMappingA", (SYSCALL)0, 0 },
3539835589
#endif
3539935590
@@ -35619,22 +35810,21 @@
3561935810
{ "GetTickCount", (SYSCALL)0, 0 },
3562035811
#endif
3562135812
3562235813
#define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
3562335814
35624
-#if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
35625
- SQLITE_WIN32_GETVERSIONEX
35815
+#if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
3562635816
{ "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
3562735817
#else
3562835818
{ "GetVersionExA", (SYSCALL)0, 0 },
3562935819
#endif
3563035820
3563135821
#define osGetVersionExA ((BOOL(WINAPI*)( \
3563235822
LPOSVERSIONINFOA))aSyscall[34].pCurrent)
3563335823
3563435824
#if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
35635
- defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
35825
+ SQLITE_WIN32_GETVERSIONEX
3563635826
{ "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
3563735827
#else
3563835828
{ "GetVersionExW", (SYSCALL)0, 0 },
3563935829
#endif
3564035830
@@ -36241,11 +36431,11 @@
3624136431
** this routine is used to determine if the host is Win95/98/ME or
3624236432
** WinNT/2K/XP so that we will know whether or not we can safely call
3624336433
** the LockFileEx() API.
3624436434
*/
3624536435
36246
-#if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
36436
+#if !SQLITE_WIN32_GETVERSIONEX
3624736437
# define osIsNT() (1)
3624836438
#elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
3624936439
# define osIsNT() (1)
3625036440
#elif !defined(SQLITE_WIN32_HAS_WIDE)
3625136441
# define osIsNT() (0)
@@ -36262,11 +36452,11 @@
3626236452
/*
3626336453
** NOTE: The WinRT sub-platform is always assumed to be based on the NT
3626436454
** kernel.
3626536455
*/
3626636456
return 1;
36267
-#elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
36457
+#elif SQLITE_WIN32_GETVERSIONEX
3626836458
if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
3626936459
#if defined(SQLITE_WIN32_HAS_ANSI)
3627036460
OSVERSIONINFOA sInfo;
3627136461
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
3627236462
osGetVersionExA(&sInfo);
@@ -38846,11 +39036,11 @@
3884639036
);
3884739037
#elif defined(SQLITE_WIN32_HAS_WIDE)
3884839038
hMap = osCreateFileMappingW(pShmNode->hFile.h,
3884939039
NULL, PAGE_READWRITE, 0, nByte, NULL
3885039040
);
38851
-#elif defined(SQLITE_WIN32_HAS_ANSI)
39041
+#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
3885239042
hMap = osCreateFileMappingA(pShmNode->hFile.h,
3885339043
NULL, PAGE_READWRITE, 0, nByte, NULL
3885439044
);
3885539045
#endif
3885639046
OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
@@ -39002,11 +39192,11 @@
3900239192
pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
3900339193
#elif defined(SQLITE_WIN32_HAS_WIDE)
3900439194
pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
3900539195
(DWORD)((nMap>>32) & 0xffffffff),
3900639196
(DWORD)(nMap & 0xffffffff), NULL);
39007
-#elif defined(SQLITE_WIN32_HAS_ANSI)
39197
+#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
3900839198
pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
3900939199
(DWORD)((nMap>>32) & 0xffffffff),
3901039200
(DWORD)(nMap & 0xffffffff), NULL);
3901139201
#endif
3901239202
if( pFd->hMap==NULL ){
@@ -43066,11 +43256,11 @@
4306643256
*/
4306743257
static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
4306843258
assert( p!=0 );
4306943259
if( p->nFresh==0 ){
4307043260
struct RowSetChunk *pNew;
43071
- pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
43261
+ pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
4307243262
if( pNew==0 ){
4307343263
return 0;
4307443264
}
4307543265
pNew->pNextChunk = p->pChunk;
4307643266
p->pChunk = pNew;
@@ -43973,10 +44163,24 @@
4397344163
** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
4397444164
** This could conceivably cause corruption following a power failure on
4397544165
** such a system. This is currently an undocumented limit.
4397644166
*/
4397744167
#define MAX_SECTOR_SIZE 0x10000
44168
+
44169
+/*
44170
+** If the option SQLITE_EXTRA_DURABLE option is set at compile-time, then
44171
+** SQLite will do extra fsync() operations when synchronous==FULL to help
44172
+** ensure that transactions are durable across a power failure. Most
44173
+** applications are happy as long as transactions are consistent across
44174
+** a power failure, and are perfectly willing to lose the last transaction
44175
+** in exchange for the extra performance of avoiding directory syncs.
44176
+** And so the default SQLITE_EXTRA_DURABLE setting is off.
44177
+*/
44178
+#ifndef SQLITE_EXTRA_DURABLE
44179
+# define SQLITE_EXTRA_DURABLE 0
44180
+#endif
44181
+
4397844182
4397944183
/*
4398044184
** An instance of the following structure is allocated for each active
4398144185
** savepoint and statement transaction in the system. All such structures
4398244186
** are stored in the Pager.aSavepoint[] array, which is allocated and
@@ -44169,10 +44373,11 @@
4416944373
u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
4417044374
u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
4417144375
u8 useJournal; /* Use a rollback journal on this file */
4417244376
u8 noSync; /* Do not sync the journal if true */
4417344377
u8 fullSync; /* Do extra syncs of the journal for robustness */
44378
+ u8 extraSync; /* sync directory after journal delete */
4417444379
u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
4417544380
u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
4417644381
u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
4417744382
u8 tempFile; /* zFilename is a temporary or immutable file */
4417844383
u8 noLock; /* Do not lock (except in WAL mode) */
@@ -45529,11 +45734,11 @@
4552945734
|| pPager->journalMode==PAGER_JOURNALMODE_MEMORY
4553045735
|| pPager->journalMode==PAGER_JOURNALMODE_WAL
4553145736
);
4553245737
sqlite3OsClose(pPager->jfd);
4553345738
if( bDelete ){
45534
- rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45739
+ rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
4553545740
}
4553645741
}
4553745742
}
4553845743
4553945744
#ifdef SQLITE_CHECK_PAGES
@@ -47035,13 +47240,19 @@
4703547240
SQLITE_PRIVATE void sqlite3PagerSetFlags(
4703647241
Pager *pPager, /* The pager to set safety level for */
4703747242
unsigned pgFlags /* Various flags */
4703847243
){
4703947244
unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
47040
- assert( level>=1 && level<=3 );
47041
- pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
47042
- pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
47245
+ if( pPager->tempFile ){
47246
+ pPager->noSync = 1;
47247
+ pPager->fullSync = 0;
47248
+ pPager->extraSync = 0;
47249
+ }else{
47250
+ pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
47251
+ pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
47252
+ pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
47253
+ }
4704347254
if( pPager->noSync ){
4704447255
pPager->syncFlags = 0;
4704547256
pPager->ckptSyncFlags = 0;
4704647257
}else if( pgFlags & PAGER_FULLFSYNC ){
4704747258
pPager->syncFlags = SQLITE_SYNC_FULL;
@@ -48342,15 +48553,21 @@
4834248553
pPager->readOnly = (u8)readOnly;
4834348554
assert( useJournal || pPager->tempFile );
4834448555
pPager->noSync = pPager->tempFile;
4834548556
if( pPager->noSync ){
4834648557
assert( pPager->fullSync==0 );
48558
+ assert( pPager->extraSync==0 );
4834748559
assert( pPager->syncFlags==0 );
4834848560
assert( pPager->walSyncFlags==0 );
4834948561
assert( pPager->ckptSyncFlags==0 );
4835048562
}else{
4835148563
pPager->fullSync = 1;
48564
+#if SQLITE_EXTRA_DURABLE
48565
+ pPager->extraSync = 1;
48566
+#else
48567
+ pPager->extraSync = 0;
48568
+#endif
4835248569
pPager->syncFlags = SQLITE_SYNC_NORMAL;
4835348570
pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
4835448571
pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
4835548572
}
4835648573
/* pPager->pFirst = 0; */
@@ -53972,14 +54189,20 @@
5397254189
u32 iWrite = 0;
5397354190
VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
5397454191
assert( rc==SQLITE_OK || iWrite==0 );
5397554192
if( iWrite>=iFirst ){
5397654193
i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
54194
+ void *pData;
5397754195
if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
5397854196
pWal->iReCksum = iWrite;
5397954197
}
53980
- rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOff);
54198
+#if defined(SQLITE_HAS_CODEC)
54199
+ if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
54200
+#else
54201
+ pData = p->pData;
54202
+#endif
54203
+ rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
5398154204
if( rc ) return rc;
5398254205
p->flags &= ~PGHDR_WAL_APPEND;
5398354206
continue;
5398454207
}
5398554208
}
@@ -57680,11 +57903,10 @@
5768057903
MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
5768157904
if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
5768257905
pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
5768357906
if( pBt->mutex==0 ){
5768457907
rc = SQLITE_NOMEM;
57685
- db->mallocFailed = 0;
5768657908
goto btree_open_out;
5768757909
}
5768857910
}
5768957911
sqlite3_mutex_enter(mutexShared);
5769057912
pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
@@ -59391,17 +59613,17 @@
5939159613
** iTable. If a read-only cursor is requested, it is assumed that
5939259614
** the caller already has at least a read-only transaction open
5939359615
** on the database already. If a write-cursor is requested, then
5939459616
** the caller is assumed to have an open write transaction.
5939559617
**
59396
-** If wrFlag==0, then the cursor can only be used for reading.
59397
-** If wrFlag==1, then the cursor can be used for reading or for
59398
-** writing if other conditions for writing are also met. These
59399
-** are the conditions that must be met in order for writing to
59400
-** be allowed:
59618
+** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
59619
+** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
59620
+** can be used for reading or for writing if other conditions for writing
59621
+** are also met. These are the conditions that must be met in order
59622
+** for writing to be allowed:
5940159623
**
59402
-** 1: The cursor must have been opened with wrFlag==1
59624
+** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
5940359625
**
5940459626
** 2: Other database connections that share the same pager cache
5940559627
** but which are not in the READ_UNCOMMITTED state may not have
5940659628
** cursors open with wrFlag==0 on the same table. Otherwise
5940759629
** the changes made by this write cursor would be visible to
@@ -59408,10 +59630,20 @@
5940859630
** the read cursors in the other database connection.
5940959631
**
5941059632
** 3: The database must be writable (not on read-only media)
5941159633
**
5941259634
** 4: There must be an active transaction.
59635
+**
59636
+** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
59637
+** is set. If FORDELETE is set, that is a hint to the implementation that
59638
+** this cursor will only be used to seek to and delete entries of an index
59639
+** as part of a larger DELETE statement. The FORDELETE hint is not used by
59640
+** this implementation. But in a hypothetical alternative storage engine
59641
+** in which index entries are automatically deleted when corresponding table
59642
+** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
59643
+** operations on this cursor can be no-ops and all READ operations can
59644
+** return a null row (2-bytes: 0x01 0x00).
5941359645
**
5941459646
** No checking is done to make sure that page iTable really is the
5941559647
** root page of a b-tree. If it is not, then the cursor acquired
5941659648
** will not work correctly.
5941759649
**
@@ -61481,11 +61713,11 @@
6148161713
*/
6148261714
#if SQLITE_DEBUG
6148361715
{
6148461716
CellInfo info;
6148561717
pPage->xParseCell(pPage, pCell, &info);
61486
- assert( nHeader=(int)(info.pPayload - pCell) );
61718
+ assert( nHeader==(int)(info.pPayload - pCell) );
6148761719
assert( info.nKey==nKey );
6148861720
assert( *pnSize == info.nSize );
6148961721
assert( spaceLeft == info.nLocal );
6149061722
}
6149161723
#endif
@@ -63140,12 +63372,12 @@
6314063372
int rc = SQLITE_OK;
6314163373
const int nMin = pCur->pBt->usableSize * 2 / 3;
6314263374
u8 aBalanceQuickSpace[13];
6314363375
u8 *pFree = 0;
6314463376
63145
- TESTONLY( int balance_quick_called = 0 );
63146
- TESTONLY( int balance_deeper_called = 0 );
63377
+ VVA_ONLY( int balance_quick_called = 0 );
63378
+ VVA_ONLY( int balance_deeper_called = 0 );
6314763379
6314863380
do {
6314963381
int iPage = pCur->iPage;
6315063382
MemPage *pPage = pCur->apPage[iPage];
6315163383
@@ -63154,11 +63386,12 @@
6315463386
/* The root page of the b-tree is overfull. In this case call the
6315563387
** balance_deeper() function to create a new child for the root-page
6315663388
** and copy the current contents of the root-page to it. The
6315763389
** next iteration of the do-loop will balance the child page.
6315863390
*/
63159
- assert( (balance_deeper_called++)==0 );
63391
+ assert( balance_deeper_called==0 );
63392
+ VVA_ONLY( balance_deeper_called++ );
6316063393
rc = balance_deeper(pPage, &pCur->apPage[1]);
6316163394
if( rc==SQLITE_OK ){
6316263395
pCur->iPage = 1;
6316363396
pCur->aiIdx[0] = 0;
6316463397
pCur->aiIdx[1] = 0;
@@ -63193,11 +63426,12 @@
6319363426
** The purpose of the following assert() is to check that only a
6319463427
** single call to balance_quick() is made for each call to this
6319563428
** function. If this were not verified, a subtle bug involving reuse
6319663429
** of the aBalanceQuickSpace[] might sneak in.
6319763430
*/
63198
- assert( (balance_quick_called++)==0 );
63431
+ assert( balance_quick_called==0 );
63432
+ VVA_ONLY( balance_quick_called++ );
6319963433
rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
6320063434
}else
6320163435
#endif
6320263436
{
6320363437
/* In this case, call balance_nonroot() to redistribute cells
@@ -63424,35 +63658,45 @@
6342463658
}
6342563659
6342663660
/*
6342763661
** Delete the entry that the cursor is pointing to.
6342863662
**
63429
-** If the second parameter is zero, then the cursor is left pointing at an
63430
-** arbitrary location after the delete. If it is non-zero, then the cursor
63431
-** is left in a state such that the next call to BtreeNext() or BtreePrev()
63432
-** moves it to the same row as it would if the call to BtreeDelete() had
63433
-** been omitted.
63663
+** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
63664
+** the cursor is left pointing at an arbitrary location after the delete.
63665
+** But if that bit is set, then the cursor is left in a state such that
63666
+** the next call to BtreeNext() or BtreePrev() moves it to the same row
63667
+** as it would have been on if the call to BtreeDelete() had been omitted.
63668
+**
63669
+** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
63670
+** associated with a single table entry and its indexes. Only one of those
63671
+** deletes is considered the "primary" delete. The primary delete occurs
63672
+** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
63673
+** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
63674
+** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
63675
+** but which might be used by alternative storage engines.
6343463676
*/
63435
-SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, int bPreserve){
63677
+SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
6343663678
Btree *p = pCur->pBtree;
6343763679
BtShared *pBt = p->pBt;
6343863680
int rc; /* Return code */
6343963681
MemPage *pPage; /* Page to delete cell from */
6344063682
unsigned char *pCell; /* Pointer to cell to delete */
6344163683
int iCellIdx; /* Index of cell to delete */
6344263684
int iCellDepth; /* Depth of node containing pCell */
6344363685
u16 szCell; /* Size of the cell being deleted */
6344463686
int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
63687
+ u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
6344563688
6344663689
assert( cursorOwnsBtShared(pCur) );
6344763690
assert( pBt->inTransaction==TRANS_WRITE );
6344863691
assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
6344963692
assert( pCur->curFlags & BTCF_WriteFlag );
6345063693
assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
6345163694
assert( !hasReadConflicts(p, pCur->pgnoRoot) );
6345263695
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
6345363696
assert( pCur->eState==CURSOR_VALID );
63697
+ assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
6345463698
6345563699
iCellDepth = pCur->iPage;
6345663700
iCellIdx = pCur->aiIdx[iCellDepth];
6345763701
pPage = pCur->apPage[iCellDepth];
6345863702
pCell = findCell(pPage, iCellIdx);
@@ -63561,11 +63805,11 @@
6356163805
}
6356263806
6356363807
if( rc==SQLITE_OK ){
6356463808
if( bSkipnext ){
6356563809
assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
63566
- assert( pPage==pCur->apPage[pCur->iPage] );
63810
+ assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
6356763811
assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
6356863812
pCur->eState = CURSOR_SKIPNEXT;
6356963813
if( iCellIdx>=pPage->nCell ){
6357063814
pCur->skipNext = -1;
6357163815
pCur->aiIdx[iCellDepth] = pPage->nCell-1;
@@ -64147,13 +64391,13 @@
6414764391
va_start(ap, zFormat);
6414864392
if( pCheck->errMsg.nChar ){
6414964393
sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
6415064394
}
6415164395
if( pCheck->zPfx ){
64152
- sqlite3XPrintf(&pCheck->errMsg, 0, pCheck->zPfx, pCheck->v1, pCheck->v2);
64396
+ sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
6415364397
}
64154
- sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
64398
+ sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
6415564399
va_end(ap);
6415664400
if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
6415764401
pCheck->mallocFailed = 1;
6415864402
}
6415964403
}
@@ -64650,11 +64894,12 @@
6465064894
char zErr[100];
6465164895
VVA_ONLY( int nRef );
6465264896
6465364897
sqlite3BtreeEnter(p);
6465464898
assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
64655
- assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 );
64899
+ VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
64900
+ assert( nRef>=0 );
6465664901
sCheck.pBt = pBt;
6465764902
sCheck.pPager = pBt->pPager;
6465864903
sCheck.nPage = btreePagecount(sCheck.pBt);
6465964904
sCheck.mxErr = mxErr;
6466064905
sCheck.nErr = 0;
@@ -64663,10 +64908,11 @@
6466364908
sCheck.v1 = 0;
6466464909
sCheck.v2 = 0;
6466564910
sCheck.aPgRef = 0;
6466664911
sCheck.heap = 0;
6466764912
sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
64913
+ sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
6466864914
if( sCheck.nPage==0 ){
6466964915
goto integrity_ck_cleanup;
6467064916
}
6467164917
6467264918
sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
@@ -65939,10 +66185,11 @@
6593966185
** in pMem->z is discarded.
6594066186
*/
6594166187
SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
6594266188
assert( sqlite3VdbeCheckMemInvariants(pMem) );
6594366189
assert( (pMem->flags&MEM_RowSet)==0 );
66190
+ testcase( pMem->db==0 );
6594466191
6594566192
/* If the bPreserve flag is set to true, then the memory cell must already
6594666193
** contain a valid string or blob value. */
6594766194
assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
6594866195
testcase( bPreserve && pMem->z==0 );
@@ -66542,11 +66789,11 @@
6654266789
SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
6654366790
sqlite3 *db = pMem->db;
6654466791
assert( db!=0 );
6654566792
assert( (pMem->flags & MEM_RowSet)==0 );
6654666793
sqlite3VdbeMemRelease(pMem);
66547
- pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
66794
+ pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
6654866795
if( db->mallocFailed ){
6654966796
pMem->flags = MEM_Null;
6655066797
pMem->szMalloc = 0;
6655166798
}else{
6655266799
assert( pMem->zMalloc );
@@ -67204,11 +67451,11 @@
6720467451
6720567452
*ppVal = pVal;
6720667453
return rc;
6720767454
6720867455
no_mem:
67209
- db->mallocFailed = 1;
67456
+ sqlite3OomFault(db);
6721067457
sqlite3DbFree(db, zVal);
6721167458
assert( *ppVal==0 );
6721267459
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
6721367460
if( pCtx==0 ) sqlite3ValueFree(pVal);
6721467461
#else
@@ -67263,11 +67510,11 @@
6726367510
iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
6726467511
nSerial = sqlite3VarintLen(iSerial);
6726567512
db = sqlite3_context_db_handle(context);
6726667513
6726767514
nRet = 1 + nSerial + nVal;
67268
- aRet = sqlite3DbMallocRaw(db, nRet);
67515
+ aRet = sqlite3DbMallocRawNN(db, nRet);
6726967516
if( aRet==0 ){
6727067517
sqlite3_result_error_nomem(context);
6727167518
}else{
6727267519
aRet[0] = nSerial+1;
6727367520
putVarint32(&aRet[1], iSerial);
@@ -67715,11 +67962,11 @@
6771567962
int i;
6771667963
VdbeOp *pOp;
6771767964
6771867965
i = p->nOp;
6771967966
assert( p->magic==VDBE_MAGIC_INIT );
67720
- assert( op>0 && op<0xff );
67967
+ assert( op>=0 && op<0xff );
6772167968
if( p->pParse->nOpAlloc<=i ){
6772267969
return growOp3(p, op, p1, p2, p3);
6772367970
}
6772467971
p->nOp++;
6772567972
pOp = &p->aOp[i];
@@ -67833,11 +68080,11 @@
6783368080
int p2, /* The P2 operand */
6783468081
int p3, /* The P3 operand */
6783568082
const u8 *zP4, /* The P4 operand */
6783668083
int p4type /* P4 operand type */
6783768084
){
67838
- char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
68085
+ char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
6783968086
if( p4copy ) memcpy(p4copy, zP4, 8);
6784068087
return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
6784168088
}
6784268089
6784368090
/*
@@ -67867,10 +68114,25 @@
6786768114
){
6786868115
int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
6786968116
sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
6787068117
return addr;
6787168118
}
68119
+
68120
+/* Insert the end of a co-routine
68121
+*/
68122
+SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
68123
+ sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
68124
+
68125
+ /* Clear the temporary register cache, thereby ensuring that each
68126
+ ** co-routine has its own independent set of registers, because co-routines
68127
+ ** might expect their registers to be preserved across an OP_Yield, and
68128
+ ** that could cause problems if two or more co-routines are using the same
68129
+ ** temporary register.
68130
+ */
68131
+ v->pParse->nTempReg = 0;
68132
+ v->pParse->nRangeReg = 0;
68133
+}
6787268134
6787368135
/*
6787468136
** Create a new symbolic label for an instruction that has yet to be
6787568137
** coded. The symbolic label is really just a negative number. The
6787668138
** label can be used as the P2 value of an operation. Later, when
@@ -68078,11 +68340,11 @@
6807868340
p->readOnly = 1;
6807968341
p->bIsReader = 0;
6808068342
for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
6808168343
u8 opcode = pOp->opcode;
6808268344
68083
- /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
68345
+ /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
6808468346
** cases from this switch! */
6808568347
switch( opcode ){
6808668348
case OP_Transaction: {
6808768349
if( pOp->p2!=0 ) p->readOnly = 0;
6808868350
/* fall thru */
@@ -68190,10 +68452,13 @@
6819068452
}
6819168453
6819268454
/*
6819368455
** Add a whole list of operations to the operation stack. Return a
6819468456
** pointer to the first operation inserted.
68457
+**
68458
+** Non-zero P2 arguments to jump instructions are automatically adjusted
68459
+** so that the jump target is relative to the first operation inserted.
6819568460
*/
6819668461
SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
6819768462
Vdbe *p, /* Add opcodes to the prepared statement */
6819868463
int nOp, /* Number of opcodes to add */
6819968464
VdbeOpList const *aOp, /* The opcodes to be added */
@@ -68210,10 +68475,13 @@
6821068475
for(i=0; i<nOp; i++, aOp++, pOut++){
6821168476
pOut->opcode = aOp->opcode;
6821268477
pOut->p1 = aOp->p1;
6821368478
pOut->p2 = aOp->p2;
6821468479
assert( aOp->p2>=0 );
68480
+ if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
68481
+ pOut->p2 += p->nOp;
68482
+ }
6821568483
pOut->p3 = aOp->p3;
6821668484
pOut->p4type = P4_NOTUSED;
6821768485
pOut->p4.p = 0;
6821868486
pOut->p5 = 0;
6821968487
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -68661,32 +68929,31 @@
6866168929
#if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
6866268930
/*
6866368931
** Translate the P4.pExpr value for an OP_CursorHint opcode into text
6866468932
** that can be displayed in the P4 column of EXPLAIN output.
6866568933
*/
68666
-static int displayP4Expr(int nTemp, char *zTemp, Expr *pExpr){
68934
+static void displayP4Expr(StrAccum *p, Expr *pExpr){
6866768935
const char *zOp = 0;
68668
- int n;
6866968936
switch( pExpr->op ){
6867068937
case TK_STRING:
68671
- sqlite3_snprintf(nTemp, zTemp, "%Q", pExpr->u.zToken);
68938
+ sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
6867268939
break;
6867368940
case TK_INTEGER:
68674
- sqlite3_snprintf(nTemp, zTemp, "%d", pExpr->u.iValue);
68941
+ sqlite3XPrintf(p, "%d", pExpr->u.iValue);
6867568942
break;
6867668943
case TK_NULL:
68677
- sqlite3_snprintf(nTemp, zTemp, "NULL");
68944
+ sqlite3XPrintf(p, "NULL");
6867868945
break;
6867968946
case TK_REGISTER: {
68680
- sqlite3_snprintf(nTemp, zTemp, "r[%d]", pExpr->iTable);
68947
+ sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
6868168948
break;
6868268949
}
6868368950
case TK_COLUMN: {
6868468951
if( pExpr->iColumn<0 ){
68685
- sqlite3_snprintf(nTemp, zTemp, "rowid");
68952
+ sqlite3XPrintf(p, "rowid");
6868668953
}else{
68687
- sqlite3_snprintf(nTemp, zTemp, "c%d", (int)pExpr->iColumn);
68954
+ sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
6868868955
}
6868968956
break;
6869068957
}
6869168958
case TK_LT: zOp = "LT"; break;
6869268959
case TK_LE: zOp = "LE"; break;
@@ -68714,25 +68981,23 @@
6871468981
case TK_NOT: zOp = "NOT"; break;
6871568982
case TK_ISNULL: zOp = "ISNULL"; break;
6871668983
case TK_NOTNULL: zOp = "NOTNULL"; break;
6871768984
6871868985
default:
68719
- sqlite3_snprintf(nTemp, zTemp, "%s", "expr");
68986
+ sqlite3XPrintf(p, "%s", "expr");
6872068987
break;
6872168988
}
6872268989
6872368990
if( zOp ){
68724
- sqlite3_snprintf(nTemp, zTemp, "%s(", zOp);
68725
- n = sqlite3Strlen30(zTemp);
68726
- n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pLeft);
68727
- if( n<nTemp-1 && pExpr->pRight ){
68728
- zTemp[n++] = ',';
68729
- n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pRight);
68730
- }
68731
- sqlite3_snprintf(nTemp-n, zTemp+n, ")");
68732
- }
68733
- return sqlite3Strlen30(zTemp);
68991
+ sqlite3XPrintf(p, "%s(", zOp);
68992
+ displayP4Expr(p, pExpr->pLeft);
68993
+ if( pExpr->pRight ){
68994
+ sqlite3StrAccumAppend(p, ",", 1);
68995
+ displayP4Expr(p, pExpr->pRight);
68996
+ }
68997
+ sqlite3StrAccumAppend(p, ")", 1);
68998
+ }
6873468999
}
6873569000
#endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
6873669001
6873769002
6873869003
#if VDBE_DISPLAY_P4
@@ -68740,107 +69005,100 @@
6874069005
** Compute a string that describes the P4 parameter for an opcode.
6874169006
** Use zTemp for any required temporary buffer space.
6874269007
*/
6874369008
static char *displayP4(Op *pOp, char *zTemp, int nTemp){
6874469009
char *zP4 = zTemp;
69010
+ StrAccum x;
6874569011
assert( nTemp>=20 );
69012
+ sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
6874669013
switch( pOp->p4type ){
6874769014
case P4_KEYINFO: {
68748
- int i, j;
69015
+ int j;
6874969016
KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
6875069017
assert( pKeyInfo->aSortOrder!=0 );
68751
- sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
68752
- i = sqlite3Strlen30(zTemp);
69018
+ sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
6875369019
for(j=0; j<pKeyInfo->nField; j++){
6875469020
CollSeq *pColl = pKeyInfo->aColl[j];
68755
- const char *zColl = pColl ? pColl->zName : "nil";
68756
- int n = sqlite3Strlen30(zColl);
68757
- if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
68758
- zColl = "B";
68759
- n = 1;
68760
- }
68761
- if( i+n>nTemp-7 ){
68762
- memcpy(&zTemp[i],",...",4);
68763
- i += 4;
68764
- break;
68765
- }
68766
- zTemp[i++] = ',';
68767
- if( pKeyInfo->aSortOrder[j] ){
68768
- zTemp[i++] = '-';
68769
- }
68770
- memcpy(&zTemp[i], zColl, n+1);
68771
- i += n;
68772
- }
68773
- zTemp[i++] = ')';
68774
- zTemp[i] = 0;
68775
- assert( i<nTemp );
69021
+ const char *zColl = pColl ? pColl->zName : "";
69022
+ if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
69023
+ sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
69024
+ }
69025
+ sqlite3StrAccumAppend(&x, ")", 1);
6877669026
break;
6877769027
}
6877869028
#ifdef SQLITE_ENABLE_CURSOR_HINTS
6877969029
case P4_EXPR: {
68780
- displayP4Expr(nTemp, zTemp, pOp->p4.pExpr);
69030
+ displayP4Expr(&x, pOp->p4.pExpr);
6878169031
break;
6878269032
}
6878369033
#endif
6878469034
case P4_COLLSEQ: {
6878569035
CollSeq *pColl = pOp->p4.pColl;
68786
- sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
69036
+ sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
6878769037
break;
6878869038
}
6878969039
case P4_FUNCDEF: {
6879069040
FuncDef *pDef = pOp->p4.pFunc;
68791
- sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
69041
+ sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
6879269042
break;
6879369043
}
6879469044
#ifdef SQLITE_DEBUG
6879569045
case P4_FUNCCTX: {
6879669046
FuncDef *pDef = pOp->p4.pCtx->pFunc;
68797
- sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
69047
+ sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
6879869048
break;
6879969049
}
6880069050
#endif
6880169051
case P4_INT64: {
68802
- sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
69052
+ sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
6880369053
break;
6880469054
}
6880569055
case P4_INT32: {
68806
- sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
69056
+ sqlite3XPrintf(&x, "%d", pOp->p4.i);
6880769057
break;
6880869058
}
6880969059
case P4_REAL: {
68810
- sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
69060
+ sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
6881169061
break;
6881269062
}
6881369063
case P4_MEM: {
6881469064
Mem *pMem = pOp->p4.pMem;
6881569065
if( pMem->flags & MEM_Str ){
6881669066
zP4 = pMem->z;
6881769067
}else if( pMem->flags & MEM_Int ){
68818
- sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
69068
+ sqlite3XPrintf(&x, "%lld", pMem->u.i);
6881969069
}else if( pMem->flags & MEM_Real ){
68820
- sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
69070
+ sqlite3XPrintf(&x, "%.16g", pMem->u.r);
6882169071
}else if( pMem->flags & MEM_Null ){
68822
- sqlite3_snprintf(nTemp, zTemp, "NULL");
69072
+ zP4 = "NULL";
6882369073
}else{
6882469074
assert( pMem->flags & MEM_Blob );
6882569075
zP4 = "(blob)";
6882669076
}
6882769077
break;
6882869078
}
6882969079
#ifndef SQLITE_OMIT_VIRTUALTABLE
6883069080
case P4_VTAB: {
6883169081
sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
68832
- sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
69082
+ sqlite3XPrintf(&x, "vtab:%p", pVtab);
6883369083
break;
6883469084
}
6883569085
#endif
6883669086
case P4_INTARRAY: {
68837
- sqlite3_snprintf(nTemp, zTemp, "intarray");
69087
+ int i;
69088
+ int *ai = pOp->p4.ai;
69089
+ int n = ai[0]; /* The first element of an INTARRAY is always the
69090
+ ** count of the number of elements to follow */
69091
+ for(i=1; i<n; i++){
69092
+ sqlite3XPrintf(&x, ",%d", ai[i]);
69093
+ }
69094
+ zTemp[0] = '[';
69095
+ sqlite3StrAccumAppend(&x, "]", 1);
6883869096
break;
6883969097
}
6884069098
case P4_SUBPROGRAM: {
68841
- sqlite3_snprintf(nTemp, zTemp, "program");
69099
+ sqlite3XPrintf(&x, "program");
6884269100
break;
6884369101
}
6884469102
case P4_ADVANCE: {
6884569103
zTemp[0] = 0;
6884669104
break;
@@ -68851,10 +69109,11 @@
6885169109
zP4 = zTemp;
6885269110
zTemp[0] = 0;
6885369111
}
6885469112
}
6885569113
}
69114
+ sqlite3StrAccumFinish(&x);
6885669115
assert( zP4!=0 );
6885769116
return zP4;
6885869117
}
6885969118
#endif /* VDBE_DISPLAY_P4 */
6886069119
@@ -68970,11 +69229,10 @@
6897069229
*/
6897169230
static void releaseMemArray(Mem *p, int N){
6897269231
if( p && N ){
6897369232
Mem *pEnd = &p[N];
6897469233
sqlite3 *db = p->db;
68975
- u8 malloc_failed = db->mallocFailed;
6897669234
if( db->pnBytesFreed ){
6897769235
do{
6897869236
if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
6897969237
}while( (++p)<pEnd );
6898069238
return;
@@ -69006,11 +69264,10 @@
6900669264
p->szMalloc = 0;
6900769265
}
6900869266
6900969267
p->flags = MEM_Undefined;
6901069268
}while( (++p)<pEnd );
69011
- db->mallocFailed = malloc_failed;
6901269269
}
6901369270
}
6901469271
6901569272
/*
6901669273
** Delete a VdbeFrame object and its contents. VdbeFrame objects are
@@ -69067,11 +69324,11 @@
6906769324
p->pResultSet = 0;
6906869325
6906969326
if( p->rc==SQLITE_NOMEM ){
6907069327
/* This happens if a malloc() inside a call to sqlite3_column_text() or
6907169328
** sqlite3_column_text16() failed. */
69072
- db->mallocFailed = 1;
69329
+ sqlite3OomFault(db);
6907369330
return SQLITE_ERROR;
6907469331
}
6907569332
6907669333
/* When the number of output rows reaches nRow, that means the
6907769334
** listing has finished and sqlite3_step() should return SQLITE_DONE.
@@ -69265,45 +69522,47 @@
6926569522
sqlite3IoTrace("SQL %s\n", z);
6926669523
}
6926769524
}
6926869525
#endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
6926969526
69270
-/*
69271
-** Allocate space from a fixed size buffer and return a pointer to
69272
-** that space. If insufficient space is available, return NULL.
69273
-**
69274
-** The pBuf parameter is the initial value of a pointer which will
69275
-** receive the new memory. pBuf is normally NULL. If pBuf is not
69276
-** NULL, it means that memory space has already been allocated and that
69277
-** this routine should not allocate any new memory. When pBuf is not
69278
-** NULL simply return pBuf. Only allocate new memory space when pBuf
69279
-** is NULL.
69280
-**
69281
-** nByte is the number of bytes of space needed.
69282
-**
69283
-** pFrom points to *pnFrom bytes of available space. New space is allocated
69284
-** from the end of the pFrom buffer and *pnFrom is decremented.
69285
-**
69286
-** *pnNeeded is a counter of the number of bytes of space that have failed
69287
-** to allocate. If there is insufficient space in pFrom to satisfy the
69288
-** request, then increment *pnNeeded by the amount of the request.
69527
+/* An instance of this object describes bulk memory available for use
69528
+** by subcomponents of a prepared statement. Space is allocated out
69529
+** of a ReusableSpace object by the allocSpace() routine below.
69530
+*/
69531
+struct ReusableSpace {
69532
+ u8 *pSpace; /* Available memory */
69533
+ int nFree; /* Bytes of available memory */
69534
+ int nNeeded; /* Total bytes that could not be allocated */
69535
+};
69536
+
69537
+/* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
69538
+** from the ReusableSpace object. Return a pointer to the allocated
69539
+** memory on success. If insufficient memory is available in the
69540
+** ReusableSpace object, increase the ReusableSpace.nNeeded
69541
+** value by the amount needed and return NULL.
69542
+**
69543
+** If pBuf is not initially NULL, that means that the memory has already
69544
+** been allocated by a prior call to this routine, so just return a copy
69545
+** of pBuf and leave ReusableSpace unchanged.
69546
+**
69547
+** This allocator is employed to repurpose unused slots at the end of the
69548
+** opcode array of prepared state for other memory needs of the prepared
69549
+** statement.
6928969550
*/
6929069551
static void *allocSpace(
69291
- void *pBuf, /* Where return pointer will be stored */
69292
- int nByte, /* Number of bytes to allocate */
69293
- u8 *pFrom, /* Memory available for allocation */
69294
- int *pnFrom, /* IN/OUT: Space available at pFrom */
69295
- int *pnNeeded /* If allocation cannot be made, increment *pnByte */
69552
+ struct ReusableSpace *p, /* Bulk memory available for allocation */
69553
+ void *pBuf, /* Pointer to a prior allocation */
69554
+ int nByte /* Bytes of memory needed */
6929669555
){
69297
- assert( EIGHT_BYTE_ALIGNMENT(pFrom) );
69556
+ assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
6929869557
if( pBuf==0 ){
6929969558
nByte = ROUND8(nByte);
69300
- if( nByte <= *pnFrom ){
69301
- *pnFrom -= nByte;
69302
- pBuf = &pFrom[*pnFrom];
69559
+ if( nByte <= p->nFree ){
69560
+ p->nFree -= nByte;
69561
+ pBuf = &p->pSpace[p->nFree];
6930369562
}else{
69304
- *pnNeeded += nByte;
69563
+ p->nNeeded += nByte;
6930569564
}
6930669565
}
6930769566
assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
6930869567
return pBuf;
6930969568
}
@@ -69332,11 +69591,10 @@
6933269591
}
6933369592
#endif
6933469593
p->pc = -1;
6933569594
p->rc = SQLITE_OK;
6933669595
p->errorAction = OE_Abort;
69337
- p->magic = VDBE_MAGIC_RUN;
6933869596
p->nChange = 0;
6933969597
p->cacheCtr = 1;
6934069598
p->minWriteFileFormat = 255;
6934169599
p->iStatement = 0;
6934269600
p->nFkConstraint = 0;
@@ -69375,13 +69633,11 @@
6937569633
int nMem; /* Number of VM memory registers */
6937669634
int nCursor; /* Number of cursors required */
6937769635
int nArg; /* Number of arguments in subprograms */
6937869636
int nOnce; /* Number of OP_Once instructions */
6937969637
int n; /* Loop counter */
69380
- int nFree; /* Available free space */
69381
- u8 *zCsr; /* Memory available for allocation */
69382
- int nByte; /* How much extra memory is needed */
69638
+ struct ReusableSpace x; /* Reusable bulk memory */
6938369639
6938469640
assert( p!=0 );
6938569641
assert( p->nOp>0 );
6938669642
assert( pParse!=0 );
6938769643
assert( p->magic==VDBE_MAGIC_INIT );
@@ -69395,69 +69651,64 @@
6939569651
nOnce = pParse->nOnce;
6939669652
if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
6939769653
6939869654
/* For each cursor required, also allocate a memory cell. Memory
6939969655
** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
69400
- ** the vdbe program. Instead they are used to allocate space for
69656
+ ** the vdbe program. Instead they are used to allocate memory for
6940169657
** VdbeCursor/BtCursor structures. The blob of memory associated with
6940269658
** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
6940369659
** stores the blob of memory associated with cursor 1, etc.
6940469660
**
6940569661
** See also: allocateCursor().
6940669662
*/
6940769663
nMem += nCursor;
6940869664
69409
- /* zCsr will initially point to nFree bytes of unused space at the
69410
- ** end of the opcode array, p->aOp. The computation of nFree is
69411
- ** conservative - it might be smaller than the true number of free
69412
- ** bytes, but never larger. nFree must be a multiple of 8 - it is
69413
- ** rounded down if is not.
69665
+ /* Figure out how much reusable memory is available at the end of the
69666
+ ** opcode array. This extra memory will be reallocated for other elements
69667
+ ** of the prepared statement.
6941469668
*/
69415
- n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode space used */
69416
- zCsr = &((u8*)p->aOp)[n]; /* Unused opcode space */
69417
- assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
69418
- nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused space */
69419
- assert( nFree>=0 );
69420
- if( nFree>0 ){
69421
- memset(zCsr, 0, nFree);
69422
- assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) );
69669
+ n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
69670
+ x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
69671
+ assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
69672
+ x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
69673
+ assert( x.nFree>=0 );
69674
+ if( x.nFree>0 ){
69675
+ memset(x.pSpace, 0, x.nFree);
69676
+ assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
6942369677
}
6942469678
6942569679
resolveP2Values(p, &nArg);
6942669680
p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
6942769681
if( pParse->explain && nMem<10 ){
6942869682
nMem = 10;
6942969683
}
6943069684
p->expired = 0;
6943169685
69432
- /* Memory for registers, parameters, cursor, etc, is allocated in two
69433
- ** passes. On the first pass, we try to reuse unused space at the
69686
+ /* Memory for registers, parameters, cursor, etc, is allocated in one or two
69687
+ ** passes. On the first pass, we try to reuse unused memory at the
6943469688
** end of the opcode array. If we are unable to satisfy all memory
6943569689
** requirements by reusing the opcode array tail, then the second
69436
- ** pass will fill in the rest using a fresh allocation.
69690
+ ** pass will fill in the remainder using a fresh memory allocation.
6943769691
**
6943869692
** This two-pass approach that reuses as much memory as possible from
69439
- ** the leftover space at the end of the opcode array can significantly
69693
+ ** the leftover memory at the end of the opcode array. This can significantly
6944069694
** reduce the amount of memory held by a prepared statement.
6944169695
*/
6944269696
do {
69443
- nByte = 0;
69444
- p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte);
69445
- p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte);
69446
- p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte);
69447
- p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
69448
- zCsr, &nFree, &nByte);
69449
- p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte);
69697
+ x.nNeeded = 0;
69698
+ p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
69699
+ p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
69700
+ p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
69701
+ p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
69702
+ p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce);
6945069703
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69451
- p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte);
69704
+ p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
6945269705
#endif
69453
- if( nByte ){
69454
- p->pFree = sqlite3DbMallocZero(db, nByte);
69455
- }
69456
- zCsr = p->pFree;
69457
- nFree = nByte;
69458
- }while( nByte && !db->mallocFailed );
69706
+ if( x.nNeeded==0 ) break;
69707
+ x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded);
69708
+ x.nFree = x.nNeeded;
69709
+ }while( !db->mallocFailed );
6945969710
6946069711
p->nCursor = nCursor;
6946169712
p->nOnceFlag = nOnce;
6946269713
if( p->aVar ){
6946369714
p->nVar = (ynVar)nVar;
@@ -70066,11 +70317,11 @@
7006670317
** Then the internal cache might have been left in an inconsistent
7006770318
** state. We need to rollback the statement transaction, if there is
7006870319
** one, or the complete transaction if there is no statement transaction.
7006970320
*/
7007070321
70071
- if( p->db->mallocFailed ){
70322
+ if( db->mallocFailed ){
7007270323
p->rc = SQLITE_NOMEM;
7007370324
}
7007470325
if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
7007570326
closeAllCursors(p);
7007670327
if( p->magic!=VDBE_MAGIC_RUN ){
@@ -70227,11 +70478,11 @@
7022770478
assert( db->nVdbeRead>=db->nVdbeWrite );
7022870479
assert( db->nVdbeWrite>=0 );
7022970480
}
7023070481
p->magic = VDBE_MAGIC_HALT;
7023170482
checkActiveVdbeCnt(db);
70232
- if( p->db->mallocFailed ){
70483
+ if( db->mallocFailed ){
7023370484
p->rc = SQLITE_NOMEM;
7023470485
}
7023570486
7023670487
/* If the auto-commit flag is set to true, then any locks that were held
7023770488
** by connection db have now been released. Call sqlite3ConnectionUnlocked()
@@ -70264,16 +70515,16 @@
7026470515
*/
7026570516
SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
7026670517
sqlite3 *db = p->db;
7026770518
int rc = p->rc;
7026870519
if( p->zErrMsg ){
70269
- u8 mallocFailed = db->mallocFailed;
70520
+ db->bBenignMalloc++;
7027070521
sqlite3BeginBenignMalloc();
7027170522
if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
7027270523
sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
7027370524
sqlite3EndBenignMalloc();
70274
- db->mallocFailed = mallocFailed;
70525
+ db->bBenignMalloc--;
7027570526
db->errCode = rc;
7027670527
}else{
7027770528
sqlite3Error(db, rc);
7027870529
}
7027970530
return rc;
@@ -70558,13 +70809,20 @@
7055870809
** a NULL row.
7055970810
**
7056070811
** If the cursor is already pointing to the correct row and that row has
7056170812
** not been deleted out from under the cursor, then this routine is a no-op.
7056270813
*/
70563
-SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
70814
+SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
70815
+ VdbeCursor *p = *pp;
7056470816
if( p->eCurType==CURTYPE_BTREE ){
7056570817
if( p->deferredMoveto ){
70818
+ int iMap;
70819
+ if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
70820
+ *pp = p->pAltCursor;
70821
+ *piCol = iMap - 1;
70822
+ return SQLITE_OK;
70823
+ }
7056670824
return handleDeferredMoveto(p);
7056770825
}
7056870826
if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
7056970827
return handleMovedCursor(p);
7057070828
}
@@ -72191,11 +72449,12 @@
7219172449
}
7219272450
SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
7219372451
return sqlite3VdbeIntValue((Mem*)pVal);
7219472452
}
7219572453
SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value *pVal){
72196
- return ((Mem*)pVal)->eSubtype;
72454
+ Mem *pMem = (Mem*)pVal;
72455
+ return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
7219772456
}
7219872457
SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
7219972458
return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
7220072459
}
7220172460
#ifndef SQLITE_OMIT_UTF16
@@ -72372,12 +72631,14 @@
7237272631
SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
7237372632
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7237472633
sqlite3VdbeMemSetNull(pCtx->pOut);
7237572634
}
7237672635
SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
72377
- assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72378
- pCtx->pOut->eSubtype = eSubtype & 0xff;
72636
+ Mem *pOut = pCtx->pOut;
72637
+ assert( sqlite3_mutex_held(pOut->db->mutex) );
72638
+ pOut->eSubtype = eSubtype & 0xff;
72639
+ pOut->flags |= MEM_Subtype;
7237972640
}
7238072641
SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
7238172642
sqlite3_context *pCtx,
7238272643
const char *z,
7238372644
int n,
@@ -72473,11 +72734,11 @@
7247372734
SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){
7247472735
assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
7247572736
sqlite3VdbeMemSetNull(pCtx->pOut);
7247672737
pCtx->isError = SQLITE_NOMEM;
7247772738
pCtx->fErrorOrAux = 1;
72478
- pCtx->pOut->db->mallocFailed = 1;
72739
+ sqlite3OomFault(pCtx->pOut->db);
7247972740
}
7248072741
7248172742
/*
7248272743
** This function is called after a transaction has been committed. It
7248372744
** invokes callbacks registered with sqlite3_wal_hook() as required.
@@ -73101,11 +73362,11 @@
7310173362
ret = xFunc(&p->aColName[N]);
7310273363
/* A malloc may have failed inside of the xFunc() call. If this
7310373364
** is the case, clear the mallocFailed flag and return NULL.
7310473365
*/
7310573366
if( db->mallocFailed ){
73106
- db->mallocFailed = 0;
73367
+ sqlite3OomClear(db);
7310773368
ret = 0;
7310873369
}
7310973370
sqlite3_mutex_leave(db->mutex);
7311073371
}
7311173372
return ret;
@@ -73802,13 +74063,13 @@
7380274063
assert( idx>0 && idx<=p->nVar );
7380374064
pVar = &p->aVar[idx-1];
7380474065
if( pVar->flags & MEM_Null ){
7380574066
sqlite3StrAccumAppend(&out, "NULL", 4);
7380674067
}else if( pVar->flags & MEM_Int ){
73807
- sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
74068
+ sqlite3XPrintf(&out, "%lld", pVar->u.i);
7380874069
}else if( pVar->flags & MEM_Real ){
73809
- sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
74070
+ sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
7381074071
}else if( pVar->flags & MEM_Str ){
7381174072
int nOut; /* Number of bytes of the string text to include in output */
7381274073
#ifndef SQLITE_OMIT_UTF16
7381374074
u8 enc = ENC(db);
7381474075
Mem utf8;
@@ -73825,36 +74086,36 @@
7382574086
if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
7382674087
nOut = SQLITE_TRACE_SIZE_LIMIT;
7382774088
while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
7382874089
}
7382974090
#endif
73830
- sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
74091
+ sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
7383174092
#ifdef SQLITE_TRACE_SIZE_LIMIT
7383274093
if( nOut<pVar->n ){
73833
- sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
74094
+ sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
7383474095
}
7383574096
#endif
7383674097
#ifndef SQLITE_OMIT_UTF16
7383774098
if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
7383874099
#endif
7383974100
}else if( pVar->flags & MEM_Zero ){
73840
- sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
74101
+ sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
7384174102
}else{
7384274103
int nOut; /* Number of bytes of the blob to include in output */
7384374104
assert( pVar->flags & MEM_Blob );
7384474105
sqlite3StrAccumAppend(&out, "x'", 2);
7384574106
nOut = pVar->n;
7384674107
#ifdef SQLITE_TRACE_SIZE_LIMIT
7384774108
if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
7384874109
#endif
7384974110
for(i=0; i<nOut; i++){
73850
- sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
74111
+ sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
7385174112
}
7385274113
sqlite3StrAccumAppend(&out, "'", 1);
7385374114
#ifdef SQLITE_TRACE_SIZE_LIMIT
7385474115
if( nOut<pVar->n ){
73855
- sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
74116
+ sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
7385674117
}
7385774118
#endif
7385874119
}
7385974120
}
7386074121
}
@@ -74336,10 +74597,11 @@
7433674597
}else{
7433774598
char zBuf[200];
7433874599
sqlite3VdbeMemPrettyPrint(p, zBuf);
7433974600
printf(" %s", zBuf);
7434074601
}
74602
+ if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
7434174603
}
7434274604
static void registerTrace(int iReg, Mem *p){
7434374605
printf("REG[%d] = ", iReg);
7434474606
memTracePrint(p);
7434574607
printf("\n");
@@ -74506,10 +74768,13 @@
7450674768
Op *aOp = p->aOp; /* Copy of p->aOp */
7450774769
Op *pOp = aOp; /* Current operation */
7450874770
#if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
7450974771
Op *pOrigOp; /* Value of pOp at the top of the loop */
7451074772
#endif
74773
+#ifdef SQLITE_DEBUG
74774
+ int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
74775
+#endif
7451174776
int rc = SQLITE_OK; /* Value to return */
7451274777
sqlite3 *db = p->db; /* The database */
7451374778
u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
7451474779
u8 encoding = ENC(db); /* The database encoding */
7451574780
int iCompare = 0; /* Result of last OP_Compare operation */
@@ -74579,11 +74844,10 @@
7457974844
}
7458074845
sqlite3EndBenignMalloc();
7458174846
#endif
7458274847
for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
7458374848
assert( pOp>=aOp && pOp<&aOp[p->nOp]);
74584
- if( db->mallocFailed ) goto no_mem;
7458574849
#ifdef VDBE_PROFILE
7458674850
start = sqlite3Hwtime();
7458774851
#endif
7458874852
nVmStep++;
7458974853
#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -75577,11 +75841,11 @@
7557775841
assert( pOp->p4type==P4_FUNCDEF );
7557875842
n = pOp->p5;
7557975843
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7558075844
assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
7558175845
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
75582
- pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
75846
+ pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7558375847
if( pCtx==0 ) goto no_mem;
7558475848
pCtx->pOut = 0;
7558575849
pCtx->pFunc = pOp->p4.pFunc;
7558675850
pCtx->iOp = (int)(pOp - aOp);
7558775851
pCtx->pVdbe = p;
@@ -76021,15 +76285,18 @@
7602176285
** of integers in P4.
7602276286
**
7602376287
** The permutation is only valid until the next OP_Compare that has
7602476288
** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
7602576289
** occur immediately prior to the OP_Compare.
76290
+**
76291
+** The first integer in the P4 integer array is the length of the array
76292
+** and does not become part of the permutation.
7602676293
*/
7602776294
case OP_Permutation: {
7602876295
assert( pOp->p4type==P4_INTARRAY );
7602976296
assert( pOp->p4.ai );
76030
- aPermute = pOp->p4.ai;
76297
+ aPermute = pOp->p4.ai + 1;
7603176298
break;
7603276299
}
7603376300
7603476301
/* Opcode: Compare P1 P2 P3 P4 P5
7603576302
** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -76330,26 +76597,28 @@
7633076597
u64 offset64; /* 64-bit offset */
7633176598
u32 avail; /* Number of bytes of available data */
7633276599
u32 t; /* A type code from the record header */
7633376600
Mem *pReg; /* PseudoTable input register */
7633476601
76602
+ pC = p->apCsr[pOp->p1];
7633576603
p2 = pOp->p2;
76604
+
76605
+ /* If the cursor cache is stale, bring it up-to-date */
76606
+ rc = sqlite3VdbeCursorMoveto(&pC, &p2);
76607
+
7633676608
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7633776609
pDest = &aMem[pOp->p3];
7633876610
memAboutToChange(p, pDest);
7633976611
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76340
- pC = p->apCsr[pOp->p1];
7634176612
assert( pC!=0 );
7634276613
assert( p2<pC->nField );
7634376614
aOffset = pC->aOffset;
7634476615
assert( pC->eCurType!=CURTYPE_VTAB );
7634576616
assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
7634676617
assert( pC->eCurType!=CURTYPE_SORTER );
7634776618
pCrsr = pC->uc.pCursor;
7634876619
76349
- /* If the cursor cache is stale, bring it up-to-date */
76350
- rc = sqlite3VdbeCursorMoveto(pC);
7635176620
if( rc ) goto abort_due_to_error;
7635276621
if( pC->cacheStatus!=p->cacheCtr ){
7635376622
if( pC->nullRow ){
7635476623
if( pC->eCurType==CURTYPE_PSEUDO ){
7635576624
assert( pC->uc.pseudoTableReg>0 );
@@ -76816,11 +77085,11 @@
7681677085
db->nStatement+db->nSavepoint);
7681777086
if( rc!=SQLITE_OK ) goto abort_due_to_error;
7681877087
#endif
7681977088
7682077089
/* Create a new savepoint structure. */
76821
- pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
77090
+ pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
7682277091
if( pNew ){
7682377092
pNew->zName = (char *)&pNew[1];
7682477093
memcpy(pNew->zName, zName, nName+1);
7682577094
7682677095
/* If there is no open transaction, then mark this as a special
@@ -76953,32 +77222,31 @@
7695377222
** This instruction causes the VM to halt.
7695477223
*/
7695577224
case OP_AutoCommit: {
7695677225
int desiredAutoCommit;
7695777226
int iRollback;
76958
- int turnOnAC;
7695977227
7696077228
desiredAutoCommit = pOp->p1;
7696177229
iRollback = pOp->p2;
76962
- turnOnAC = desiredAutoCommit && !db->autoCommit;
7696377230
assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
7696477231
assert( desiredAutoCommit==1 || iRollback==0 );
7696577232
assert( db->nVdbeActive>0 ); /* At least this one VM is active */
7696677233
assert( p->bIsReader );
7696777234
76968
- if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
76969
- /* If this instruction implements a COMMIT and other VMs are writing
76970
- ** return an error indicating that the other VMs must complete first.
76971
- */
76972
- sqlite3VdbeError(p, "cannot commit transaction - "
76973
- "SQL statements in progress");
76974
- rc = SQLITE_BUSY;
76975
- }else if( desiredAutoCommit!=db->autoCommit ){
77235
+ if( desiredAutoCommit!=db->autoCommit ){
7697677236
if( iRollback ){
7697777237
assert( desiredAutoCommit==1 );
7697877238
sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
7697977239
db->autoCommit = 1;
77240
+ }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
77241
+ /* If this instruction implements a COMMIT and other VMs are writing
77242
+ ** return an error indicating that the other VMs must complete first.
77243
+ */
77244
+ sqlite3VdbeError(p, "cannot commit transaction - "
77245
+ "SQL statements in progress");
77246
+ rc = SQLITE_BUSY;
77247
+ break;
7698077248
}else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
7698177249
goto vdbe_return;
7698277250
}else{
7698377251
db->autoCommit = (u8)desiredAutoCommit;
7698477252
}
@@ -77159,38 +77427,36 @@
7715977427
break;
7716077428
}
7716177429
7716277430
/* Opcode: SetCookie P1 P2 P3 * *
7716377431
**
77164
-** Write the content of register P3 (interpreted as an integer)
77165
-** into cookie number P2 of database P1. P2==1 is the schema version.
77166
-** P2==2 is the database format. P2==3 is the recommended pager cache
77432
+** Write the integer value P3 into cookie number P2 of database P1.
77433
+** P2==1 is the schema version. P2==2 is the database format.
77434
+** P2==3 is the recommended pager cache
7716777435
** size, and so forth. P1==0 is the main database file and P1==1 is the
7716877436
** database file used to store temporary tables.
7716977437
**
7717077438
** A transaction must be started before executing this opcode.
7717177439
*/
77172
-case OP_SetCookie: { /* in3 */
77440
+case OP_SetCookie: {
7717377441
Db *pDb;
7717477442
assert( pOp->p2<SQLITE_N_BTREE_META );
7717577443
assert( pOp->p1>=0 && pOp->p1<db->nDb );
7717677444
assert( DbMaskTest(p->btreeMask, pOp->p1) );
7717777445
assert( p->readOnly==0 );
7717877446
pDb = &db->aDb[pOp->p1];
7717977447
assert( pDb->pBt!=0 );
7718077448
assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
77181
- pIn3 = &aMem[pOp->p3];
77182
- sqlite3VdbeMemIntegerify(pIn3);
7718377449
/* See note about index shifting on OP_ReadCookie */
77184
- rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
77450
+ rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
7718577451
if( pOp->p2==BTREE_SCHEMA_VERSION ){
7718677452
/* When the schema cookie changes, record the new cookie internally */
77187
- pDb->pSchema->schema_cookie = (int)pIn3->u.i;
77453
+ pDb->pSchema->schema_cookie = pOp->p3;
7718877454
db->flags |= SQLITE_InternChanges;
7718977455
}else if( pOp->p2==BTREE_FILE_FORMAT ){
7719077456
/* Record changes in the file format */
77191
- pDb->pSchema->file_format = (u8)pIn3->u.i;
77457
+ pDb->pSchema->file_format = pOp->p3;
7719277458
}
7719377459
if( pOp->p1==1 ){
7719477460
/* Invalidate all prepared statements whenever the TEMP database
7719577461
** schema is changed. Ticket #1644 */
7719677462
sqlite3ExpirePreparedStatements(db);
@@ -77346,10 +77612,13 @@
7734677612
pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
7734777613
if( pCur==0 ) goto no_mem;
7734877614
pCur->nullRow = 1;
7734977615
pCur->isOrdered = 1;
7735077616
pCur->pgnoRoot = p2;
77617
+#ifdef SQLITE_DEBUG
77618
+ pCur->wrFlag = wrFlag;
77619
+#endif
7735177620
rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
7735277621
pCur->pKeyInfo = pKeyInfo;
7735377622
/* Set the VdbeCursor.isTable variable. Previous versions of
7735477623
** SQLite used to check if the root-page flags were sane at this point
7735577624
** and report database corruption if they were not, but this check has
@@ -77799,36 +78068,10 @@
7779978068
assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
7780078069
pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
7780178070
}
7780278071
break;
7780378072
}
77804
-
77805
-/* Opcode: Seek P1 P2 * * *
77806
-** Synopsis: intkey=r[P2]
77807
-**
77808
-** P1 is an open table cursor and P2 is a rowid integer. Arrange
77809
-** for P1 to move so that it points to the rowid given by P2.
77810
-**
77811
-** This is actually a deferred seek. Nothing actually happens until
77812
-** the cursor is used to read a record. That way, if no reads
77813
-** occur, no unnecessary I/O happens.
77814
-*/
77815
-case OP_Seek: { /* in2 */
77816
- VdbeCursor *pC;
77817
-
77818
- assert( pOp->p1>=0 && pOp->p1<p->nCursor );
77819
- pC = p->apCsr[pOp->p1];
77820
- assert( pC!=0 );
77821
- assert( pC->eCurType==CURTYPE_BTREE );
77822
- assert( pC->uc.pCursor!=0 );
77823
- assert( pC->isTable );
77824
- pC->nullRow = 0;
77825
- pIn2 = &aMem[pOp->p2];
77826
- pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
77827
- pC->deferredMoveto = 1;
77828
- break;
77829
-}
7783078073
7783178074
7783278075
/* Opcode: Found P1 P2 P3 P4 *
7783378076
** Synopsis: key=r[P3@P4]
7783478077
**
@@ -78295,18 +78538,26 @@
7829578538
7829678539
/* Opcode: Delete P1 P2 * P4 P5
7829778540
**
7829878541
** Delete the record at which the P1 cursor is currently pointing.
7829978542
**
78300
-** If the P5 parameter is non-zero, the cursor will be left pointing at
78301
-** either the next or the previous record in the table. If it is left
78302
-** pointing at the next record, then the next Next instruction will be a
78303
-** no-op. As a result, in this case it is OK to delete a record from within a
78304
-** Next loop. If P5 is zero, then the cursor is left in an undefined state.
78543
+** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
78544
+** the cursor will be left pointing at either the next or the previous
78545
+** record in the table. If it is left pointing at the next record, then
78546
+** the next Next instruction will be a no-op. As a result, in this case
78547
+** it is ok to delete a record from within a Next loop. If
78548
+** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
78549
+** left in an undefined state.
7830578550
**
78306
-** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
78307
-** incremented (otherwise not).
78551
+** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
78552
+** delete one of several associated with deleting a table row and all its
78553
+** associated index entries. Exactly one of those deletes is the "primary"
78554
+** delete. The others are all on OPFLAG_FORDELETE cursors or else are
78555
+** marked with the AUXDELETE flag.
78556
+**
78557
+** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
78558
+** change count is incremented (otherwise not).
7830878559
**
7830978560
** P1 must not be pseudo-table. It has to be a real table with
7831078561
** multiple rows.
7831178562
**
7831278563
** If P4 is not NULL, then it is the name of the table that P1 is
@@ -78338,11 +78589,30 @@
7833878589
i64 iKey = 0;
7833978590
sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
7834078591
assert( pC->movetoTarget==iKey );
7834178592
}
7834278593
#endif
78343
-
78594
+
78595
+ /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
78596
+ assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
78597
+ assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
78598
+ assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
78599
+
78600
+#ifdef SQLITE_DEBUG
78601
+ if( p->pFrame==0 ){
78602
+ if( pC->isEphemeral==0
78603
+ && (pOp->p5 & OPFLAG_AUXDELETE)==0
78604
+ && (pC->wrFlag & OPFLAG_FORDELETE)==0
78605
+ ){
78606
+ nExtraDelete++;
78607
+ }
78608
+ if( pOp->p2 & OPFLAG_NCHANGE ){
78609
+ nExtraDelete--;
78610
+ }
78611
+ }
78612
+#endif
78613
+
7834478614
rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
7834578615
pC->cacheStatus = CACHE_STALE;
7834678616
7834778617
/* Invoke the update-hook if required. */
7834878618
if( rc==SQLITE_OK && hasUpdateCallback ){
@@ -78883,62 +79153,98 @@
7888379153
assert( pOp->p5==0 );
7888479154
r.pKeyInfo = pC->pKeyInfo;
7888579155
r.nField = (u16)pOp->p3;
7888679156
r.default_rc = 0;
7888779157
r.aMem = &aMem[pOp->p2];
78888
-#ifdef SQLITE_DEBUG
78889
- { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
78890
-#endif
7889179158
rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
7889279159
if( rc==SQLITE_OK && res==0 ){
78893
- rc = sqlite3BtreeDelete(pCrsr, 0);
79160
+ rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
7889479161
}
7889579162
assert( pC->deferredMoveto==0 );
7889679163
pC->cacheStatus = CACHE_STALE;
7889779164
break;
7889879165
}
7889979166
79167
+/* Opcode: Seek P1 * P3 P4 *
79168
+** Synopsis: Move P3 to P1.rowid
79169
+**
79170
+** P1 is an open index cursor and P3 is a cursor on the corresponding
79171
+** table. This opcode does a deferred seek of the P3 table cursor
79172
+** to the row that corresponds to the current row of P1.
79173
+**
79174
+** This is a deferred seek. Nothing actually happens until
79175
+** the cursor is used to read a record. That way, if no reads
79176
+** occur, no unnecessary I/O happens.
79177
+**
79178
+** P4 may be an array of integers (type P4_INTARRAY) containing
79179
+** one entry for each column in the P3 table. If array entry a(i)
79180
+** is non-zero, then reading column a(i)-1 from cursor P3 is
79181
+** equivalent to performing the deferred seek and then reading column i
79182
+** from P1. This information is stored in P3 and used to redirect
79183
+** reads against P3 over to P1, thus possibly avoiding the need to
79184
+** seek and read cursor P3.
79185
+*/
7890079186
/* Opcode: IdxRowid P1 P2 * * *
7890179187
** Synopsis: r[P2]=rowid
7890279188
**
7890379189
** Write into register P2 an integer which is the last entry in the record at
7890479190
** the end of the index key pointed to by cursor P1. This integer should be
7890579191
** the rowid of the table entry to which this index entry points.
7890679192
**
7890779193
** See also: Rowid, MakeRecord.
7890879194
*/
79195
+case OP_Seek:
7890979196
case OP_IdxRowid: { /* out2 */
78910
- BtCursor *pCrsr;
78911
- VdbeCursor *pC;
78912
- i64 rowid;
79197
+ VdbeCursor *pC; /* The P1 index cursor */
79198
+ VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
79199
+ i64 rowid; /* Rowid that P1 current points to */
7891379200
78914
- pOut = out2Prerelease(p, pOp);
7891579201
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7891679202
pC = p->apCsr[pOp->p1];
7891779203
assert( pC!=0 );
7891879204
assert( pC->eCurType==CURTYPE_BTREE );
78919
- pCrsr = pC->uc.pCursor;
78920
- assert( pCrsr!=0 );
78921
- pOut->flags = MEM_Null;
79205
+ assert( pC->uc.pCursor!=0 );
7892279206
assert( pC->isTable==0 );
7892379207
assert( pC->deferredMoveto==0 );
79208
+ assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
79209
+
79210
+ /* The IdxRowid and Seek opcodes are combined because of the commonality
79211
+ ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
79212
+ rc = sqlite3VdbeCursorRestore(pC);
7892479213
7892579214
/* sqlite3VbeCursorRestore() can only fail if the record has been deleted
78926
- ** out from under the cursor. That will never happend for an IdxRowid
78927
- ** opcode, hence the NEVER() arround the check of the return value.
78928
- */
78929
- rc = sqlite3VdbeCursorRestore(pC);
79215
+ ** out from under the cursor. That will never happens for an IdxRowid
79216
+ ** or Seek opcode */
7893079217
if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
7893179218
7893279219
if( !pC->nullRow ){
7893379220
rowid = 0; /* Not needed. Only used to silence a warning. */
78934
- rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
79221
+ rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
7893579222
if( rc!=SQLITE_OK ){
7893679223
goto abort_due_to_error;
7893779224
}
78938
- pOut->u.i = rowid;
78939
- pOut->flags = MEM_Int;
79225
+ if( pOp->opcode==OP_Seek ){
79226
+ assert( pOp->p3>=0 && pOp->p3<p->nCursor );
79227
+ pTabCur = p->apCsr[pOp->p3];
79228
+ assert( pTabCur!=0 );
79229
+ assert( pTabCur->eCurType==CURTYPE_BTREE );
79230
+ assert( pTabCur->uc.pCursor!=0 );
79231
+ assert( pTabCur->isTable );
79232
+ pTabCur->nullRow = 0;
79233
+ pTabCur->movetoTarget = rowid;
79234
+ pTabCur->deferredMoveto = 1;
79235
+ assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
79236
+ pTabCur->aAltMap = pOp->p4.ai;
79237
+ pTabCur->pAltCursor = pC;
79238
+ }else{
79239
+ pOut = out2Prerelease(p, pOp);
79240
+ pOut->u.i = rowid;
79241
+ pOut->flags = MEM_Int;
79242
+ }
79243
+ }else{
79244
+ assert( pOp->opcode==OP_IdxRowid );
79245
+ sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
7894079246
}
7894179247
break;
7894279248
}
7894379249
7894479250
/* Opcode: IdxGE P1 P2 P3 P4 P5
@@ -79329,11 +79635,11 @@
7932979635
Mem *pnErr; /* Register keeping track of errors remaining */
7933079636
7933179637
assert( p->bIsReader );
7933279638
nRoot = pOp->p2;
7933379639
assert( nRoot>0 );
79334
- aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
79640
+ aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(nRoot+1) );
7933579641
if( aRoot==0 ) goto no_mem;
7933679642
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7933779643
pnErr = &aMem[pOp->p3];
7933879644
assert( (pnErr->flags & MEM_Int)!=0 );
7933979645
assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
@@ -79711,24 +80017,35 @@
7971180017
goto jump_to_p2;
7971280018
}
7971380019
break;
7971480020
}
7971580021
79716
-/* Opcode: SetIfNotPos P1 P2 P3 * *
79717
-** Synopsis: if r[P1]<=0 then r[P2]=P3
80022
+/* Opcode: OffsetLimit P1 P2 P3 * *
80023
+** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
7971880024
**
79719
-** Register P1 must contain an integer.
79720
-** If the value of register P1 is not positive (if it is less than 1) then
79721
-** set the value of register P2 to be the integer P3.
80025
+** This opcode performs a commonly used computation associated with
80026
+** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
80027
+** holds the offset counter. The opcode computes the combined value
80028
+** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
80029
+** value computed is the total number of rows that will need to be
80030
+** visited in order to complete the query.
80031
+**
80032
+** If r[P3] is zero or negative, that means there is no OFFSET
80033
+** and r[P2] is set to be the value of the LIMIT, r[P1].
80034
+**
80035
+** if r[P1] is zero or negative, that means there is no LIMIT
80036
+** and r[P2] is set to -1.
80037
+**
80038
+** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
7972280039
*/
79723
-case OP_SetIfNotPos: { /* in1, in2 */
80040
+case OP_OffsetLimit: { /* in1, out2, in3 */
7972480041
pIn1 = &aMem[pOp->p1];
79725
- assert( pIn1->flags&MEM_Int );
79726
- if( pIn1->u.i<=0 ){
79727
- pOut = out2Prerelease(p, pOp);
79728
- pOut->u.i = pOp->p3;
79729
- }
80042
+ pIn3 = &aMem[pOp->p3];
80043
+ pOut = out2Prerelease(p, pOp);
80044
+ assert( pIn1->flags & MEM_Int );
80045
+ assert( pIn3->flags & MEM_Int );
80046
+ pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0);
7973080047
break;
7973180048
}
7973280049
7973380050
/* Opcode: IfNotZero P1 P2 P3 * *
7973480051
** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
@@ -79815,11 +80132,11 @@
7981580132
assert( pOp->p4type==P4_FUNCDEF );
7981680133
n = pOp->p5;
7981780134
assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7981880135
assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
7981980136
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
79820
- pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
80137
+ pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
7982180138
if( pCtx==0 ) goto no_mem;
7982280139
pCtx->pMem = 0;
7982380140
pCtx->pFunc = pOp->p4.pFunc;
7982480141
pCtx->iOp = (int)(pOp - aOp);
7982580142
pCtx->pVdbe = p;
@@ -80682,11 +80999,11 @@
8068280999
p->rc = rc;
8068381000
testcase( sqlite3GlobalConfig.xLog!=0 );
8068481001
sqlite3_log(rc, "statement aborts at %d: [%s] %s",
8068581002
(int)(pOp - aOp), p->zSql, p->zErrMsg);
8068681003
sqlite3VdbeHalt(p);
80687
- if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
81004
+ if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
8068881005
rc = SQLITE_ERROR;
8068981006
if( resetSchemaOnFault>0 ){
8069081007
sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
8069181008
}
8069281009
@@ -80696,10 +81013,13 @@
8069681013
vdbe_return:
8069781014
db->lastRowid = lastRowid;
8069881015
testcase( nVmStep>0 );
8069981016
p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
8070081017
sqlite3VdbeLeave(p);
81018
+ assert( rc!=SQLITE_OK || nExtraDelete==0
81019
+ || sqlite3_strlike("DELETE%",p->zSql,0)!=0
81020
+ );
8070181021
return rc;
8070281022
8070381023
/* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
8070481024
** is encountered.
8070581025
*/
@@ -80709,11 +81029,11 @@
8070981029
goto vdbe_error_halt;
8071081030
8071181031
/* Jump to here if a malloc() fails.
8071281032
*/
8071381033
no_mem:
80714
- db->mallocFailed = 1;
81034
+ sqlite3OomFault(db);
8071581035
sqlite3VdbeError(p, "out of memory");
8071681036
rc = SQLITE_NOMEM;
8071781037
goto vdbe_error_halt;
8071881038
8071981039
/* Jump to here for any other kind of fatal error. The "rc" variable
@@ -80730,11 +81050,11 @@
8073081050
/* Jump to here if the sqlite3_interrupt() API sets the interrupt
8073181051
** flag.
8073281052
*/
8073381053
abort_due_to_interrupt:
8073481054
assert( db->u1.isInterrupted );
80735
- rc = SQLITE_INTERRUPT;
81055
+ rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_INTERRUPT;
8073681056
p->rc = rc;
8073781057
sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
8073881058
goto vdbe_error_halt;
8073981059
}
8074081060
@@ -80990,23 +81310,21 @@
8099081310
**
8099181311
** The sqlite3_blob_close() function finalizes the vdbe program,
8099281312
** which closes the b-tree cursor and (possibly) commits the
8099381313
** transaction.
8099481314
*/
80995
- static const int iLn = VDBE_OFFSET_LINENO(4);
81315
+ static const int iLn = VDBE_OFFSET_LINENO(2);
8099681316
static const VdbeOpList openBlob[] = {
80997
- /* addr/ofst */
80998
- /* {OP_Transaction, 0, 0, 0}, // 0/ inserted separately */
80999
- {OP_TableLock, 0, 0, 0}, /* 1/0: Acquire a read or write lock */
81000
- {OP_OpenRead, 0, 0, 0}, /* 2/1: Open a cursor */
81001
- {OP_Variable, 1, 1, 0}, /* 3/2: Move ?1 into reg[1] */
81002
- {OP_NotExists, 0, 8, 1}, /* 4/3: Seek the cursor */
81003
- {OP_Column, 0, 0, 1}, /* 5/4 */
81004
- {OP_ResultRow, 1, 0, 0}, /* 6/5 */
81005
- {OP_Goto, 0, 3, 0}, /* 7/6 */
81006
- {OP_Close, 0, 0, 0}, /* 8/7 */
81007
- {OP_Halt, 0, 0, 0}, /* 9/8 */
81317
+ {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
81318
+ {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
81319
+ {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */
81320
+ {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */
81321
+ {OP_Column, 0, 0, 1}, /* 4 */
81322
+ {OP_ResultRow, 1, 0, 0}, /* 5 */
81323
+ {OP_Goto, 0, 2, 0}, /* 6 */
81324
+ {OP_Close, 0, 0, 0}, /* 7 */
81325
+ {OP_Halt, 0, 0, 0}, /* 8 */
8100881326
};
8100981327
Vdbe *v = (Vdbe *)pBlob->pStmt;
8101081328
int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
8101181329
VdbeOp *aOp;
8101281330
@@ -83044,27 +83362,28 @@
8304483362
if( pSorter->list.aMemory ){
8304583363
int nMin = pSorter->iMemory + nReq;
8304683364
8304783365
if( nMin>pSorter->nMemory ){
8304883366
u8 *aNew;
83367
+ int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
8304983368
int nNew = pSorter->nMemory * 2;
8305083369
while( nNew < nMin ) nNew = nNew*2;
8305183370
if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
8305283371
if( nNew < nMin ) nNew = nMin;
8305383372
8305483373
aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
8305583374
if( !aNew ) return SQLITE_NOMEM;
83056
- pSorter->list.pList = (SorterRecord*)(
83057
- aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
83058
- );
83375
+ pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
8305983376
pSorter->list.aMemory = aNew;
8306083377
pSorter->nMemory = nNew;
8306183378
}
8306283379
8306383380
pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
8306483381
pSorter->iMemory += ROUND8(nReq);
83065
- pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
83382
+ if( pSorter->list.pList ){
83383
+ pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
83384
+ }
8306683385
}else{
8306783386
pNew = (SorterRecord *)sqlite3Malloc(nReq);
8306883387
if( pNew==0 ){
8306983388
return SQLITE_NOMEM;
8307083389
}
@@ -86230,12 +86549,11 @@
8623086549
return pExpr;
8623186550
}
8623286551
SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
8623386552
Token s;
8623486553
assert( zC!=0 );
86235
- s.z = zC;
86236
- s.n = sqlite3Strlen30(s.z);
86554
+ sqlite3TokenInit(&s, (char*)zC);
8623786555
return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
8623886556
}
8623986557
8624086558
/*
8624186559
** Skip over any TK_COLLATE operators and any unlikely()
@@ -86599,18 +86917,19 @@
8659986917
){
8660086918
Expr *pNew;
8660186919
int nExtra = 0;
8660286920
int iValue = 0;
8660386921
86922
+ assert( db!=0 );
8660486923
if( pToken ){
8660586924
if( op!=TK_INTEGER || pToken->z==0
8660686925
|| sqlite3GetInt32(pToken->z, &iValue)==0 ){
8660786926
nExtra = pToken->n+1;
8660886927
assert( iValue>=0 );
8660986928
}
8661086929
}
86611
- pNew = sqlite3DbMallocRaw(db, sizeof(Expr)+nExtra);
86930
+ pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
8661286931
if( pNew ){
8661386932
memset(pNew, 0, sizeof(Expr));
8661486933
pNew->op = (u8)op;
8661586934
pNew->iAgg = -1;
8661686935
if( pToken ){
@@ -86845,11 +87164,14 @@
8684587164
}
8684687165
if( x>0 ){
8684787166
if( x>pParse->nzVar ){
8684887167
char **a;
8684987168
a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
86850
- if( a==0 ) return; /* Error reported through db->mallocFailed */
87169
+ if( a==0 ){
87170
+ assert( db->mallocFailed ); /* Error reported through mallocFailed */
87171
+ return;
87172
+ }
8685187173
pParse->azVar = a;
8685287174
memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
8685387175
pParse->nzVar = x;
8685487176
}
8685587177
if( z[0]!='?' || pParse->azVar[x-1]==0 ){
@@ -87000,10 +87322,11 @@
8700087322
** portion of the buffer copied into by this function.
8700187323
*/
8700287324
static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
8700387325
Expr *pNew = 0; /* Value to return */
8700487326
assert( flags==0 || flags==EXPRDUP_REDUCE );
87327
+ assert( db!=0 );
8700587328
if( p ){
8700687329
const int isReduced = (flags&EXPRDUP_REDUCE);
8700787330
u8 *zAlloc;
8700887331
u32 staticFlag = 0;
8700987332
@@ -87012,11 +87335,11 @@
8701287335
/* Figure out where to write the new Expr structure. */
8701387336
if( pzBuffer ){
8701487337
zAlloc = *pzBuffer;
8701587338
staticFlag = EP_Static;
8701687339
}else{
87017
- zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
87340
+ zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, flags));
8701887341
}
8701987342
pNew = (Expr *)zAlloc;
8702087343
8702187344
if( pNew ){
8702287345
/* Set nNewSize to the size allocated for the structure pointed to
@@ -87135,16 +87458,17 @@
8713587458
}
8713687459
SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
8713787460
ExprList *pNew;
8713887461
struct ExprList_item *pItem, *pOldItem;
8713987462
int i;
87463
+ assert( db!=0 );
8714087464
if( p==0 ) return 0;
87141
- pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
87465
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
8714287466
if( pNew==0 ) return 0;
8714387467
pNew->nExpr = i = p->nExpr;
8714487468
if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
87145
- pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) );
87469
+ pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) );
8714687470
if( pItem==0 ){
8714787471
sqlite3DbFree(db, pNew);
8714887472
return 0;
8714987473
}
8715087474
pOldItem = p->a;
@@ -87171,13 +87495,14 @@
8717187495
|| !defined(SQLITE_OMIT_SUBQUERY)
8717287496
SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
8717387497
SrcList *pNew;
8717487498
int i;
8717587499
int nByte;
87500
+ assert( db!=0 );
8717687501
if( p==0 ) return 0;
8717787502
nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
87178
- pNew = sqlite3DbMallocRaw(db, nByte );
87503
+ pNew = sqlite3DbMallocRawNN(db, nByte );
8717987504
if( pNew==0 ) return 0;
8718087505
pNew->nSrc = pNew->nAlloc = p->nSrc;
8718187506
for(i=0; i<p->nSrc; i++){
8718287507
struct SrcList_item *pNewItem = &pNew->a[i];
8718387508
struct SrcList_item *pOldItem = &p->a[i];
@@ -87210,15 +87535,16 @@
8721087535
return pNew;
8721187536
}
8721287537
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
8721387538
IdList *pNew;
8721487539
int i;
87540
+ assert( db!=0 );
8721587541
if( p==0 ) return 0;
87216
- pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
87542
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
8721787543
if( pNew==0 ) return 0;
8721887544
pNew->nId = p->nId;
87219
- pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
87545
+ pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
8722087546
if( pNew->a==0 ){
8722187547
sqlite3DbFree(db, pNew);
8722287548
return 0;
8722387549
}
8722487550
/* Note that because the size of the allocation for p->a[] is not
@@ -87232,12 +87558,13 @@
8723287558
}
8723387559
return pNew;
8723487560
}
8723587561
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
8723687562
Select *pNew, *pPrior;
87563
+ assert( db!=0 );
8723787564
if( p==0 ) return 0;
87238
- pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
87565
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
8723987566
if( pNew==0 ) return 0;
8724087567
pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
8724187568
pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
8724287569
pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
8724387570
pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
@@ -87279,17 +87606,18 @@
8727987606
Parse *pParse, /* Parsing context */
8728087607
ExprList *pList, /* List to which to append. Might be NULL */
8728187608
Expr *pExpr /* Expression to be appended. Might be NULL */
8728287609
){
8728387610
sqlite3 *db = pParse->db;
87611
+ assert( db!=0 );
8728487612
if( pList==0 ){
87285
- pList = sqlite3DbMallocRaw(db, sizeof(ExprList) );
87613
+ pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
8728687614
if( pList==0 ){
8728787615
goto no_mem;
8728887616
}
8728987617
pList->nExpr = 0;
87290
- pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
87618
+ pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
8729187619
if( pList->a==0 ) goto no_mem;
8729287620
}else if( (pList->nExpr & (pList->nExpr-1))==0 ){
8729387621
struct ExprList_item *a;
8729487622
assert( pList->nExpr>0 );
8729587623
a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
@@ -90989,37 +91317,10 @@
9098991317
sqlite3SrcListDelete(db, pSrc);
9099091318
sqlite3DbFree(db, zName);
9099191319
db->flags = savedDbFlags;
9099291320
}
9099391321
90994
-
90995
-/*
90996
-** Generate code to make sure the file format number is at least minFormat.
90997
-** The generated code will increase the file format number if necessary.
90998
-*/
90999
-SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
91000
- Vdbe *v;
91001
- v = sqlite3GetVdbe(pParse);
91002
- /* The VDBE should have been allocated before this routine is called.
91003
- ** If that allocation failed, we would have quit before reaching this
91004
- ** point */
91005
- if( ALWAYS(v) ){
91006
- int r1 = sqlite3GetTempReg(pParse);
91007
- int r2 = sqlite3GetTempReg(pParse);
91008
- int addr1;
91009
- sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
91010
- sqlite3VdbeUsesBtree(v, iDb);
91011
- sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
91012
- addr1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
91013
- sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
91014
- sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
91015
- sqlite3VdbeJumpHere(v, addr1);
91016
- sqlite3ReleaseTempReg(pParse, r1);
91017
- sqlite3ReleaseTempReg(pParse, r2);
91018
- }
91019
-}
91020
-
9102191322
/*
9102291323
** This function is called after an "ALTER TABLE ... ADD" statement
9102391324
** has been parsed. Argument pColDef contains the text of the new
9102491325
** column definition.
9102591326
**
@@ -91034,13 +91335,15 @@
9103491335
const char *zTab; /* Table name */
9103591336
char *zCol; /* Null-terminated column definition */
9103691337
Column *pCol; /* The new column */
9103791338
Expr *pDflt; /* Default value for the new column */
9103891339
sqlite3 *db; /* The database connection; */
91340
+ Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
9103991341
9104091342
db = pParse->db;
9104191343
if( pParse->nErr || db->mallocFailed ) return;
91344
+ assert( v!=0 );
9104291345
pNew = pParse->pNewTable;
9104391346
assert( pNew );
9104491347
9104591348
assert( sqlite3BtreeHoldsAllMutexes(db) );
9104691349
iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
@@ -91096,11 +91399,11 @@
9109691399
sqlite3_value *pVal = 0;
9109791400
int rc;
9109891401
rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
9109991402
assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
9110091403
if( rc!=SQLITE_OK ){
91101
- db->mallocFailed = 1;
91404
+ assert( db->mallocFailed == 1 );
9110291405
return;
9110391406
}
9110491407
if( !pVal ){
9110591408
sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
9110691409
return;
@@ -91126,15 +91429,20 @@
9112691429
);
9112791430
sqlite3DbFree(db, zCol);
9112891431
db->flags = savedDbFlags;
9112991432
}
9113091433
91131
- /* If the default value of the new column is NULL, then set the file
91434
+ /* If the default value of the new column is NULL, then the file
9113291435
** format to 2. If the default value of the new column is not NULL,
91133
- ** the file format becomes 3.
91436
+ ** the file format be 3. Back when this feature was first added
91437
+ ** in 2006, we went to the trouble to upgrade the file format to the
91438
+ ** minimum support values. But 10-years on, we can assume that all
91439
+ ** extent versions of SQLite support file-format 4, so we always and
91440
+ ** unconditionally upgrade to 4.
9113491441
*/
91135
- sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
91442
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT,
91443
+ SQLITE_MAX_FILE_FORMAT);
9113691444
9113791445
/* Reload the schema of the modified table. */
9113891446
reloadTableSchema(pParse, pTab, pTab->zName);
9113991447
}
9114091448
@@ -91204,11 +91512,11 @@
9120491512
nAlloc = (((pNew->nCol-1)/8)*8)+8;
9120591513
assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
9120691514
pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
9120791515
pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
9120891516
if( !pNew->aCol || !pNew->zName ){
91209
- db->mallocFailed = 1;
91517
+ assert( db->mallocFailed );
9121091518
goto exit_begin_add_column;
9121191519
}
9121291520
memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
9121391521
for(i=0; i<pNew->nCol; i++){
9121491522
Column *pCol = &pNew->aCol[i];
@@ -91549,11 +91857,11 @@
9154991857
*/
9155091858
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9155191859
static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
9155291860
assert( db!=0 );
9155391861
if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
91554
- p->u.aRowid = sqlite3DbMallocRaw(db, n);
91862
+ p->u.aRowid = sqlite3DbMallocRawNN(db, n);
9155591863
if( p->u.aRowid ){
9155691864
p->nRowid = n;
9155791865
memcpy(p->u.aRowid, pData, n);
9155891866
}else{
9155991867
p->nRowid = 0;
@@ -92351,11 +92659,11 @@
9235192659
addrNextRow = sqlite3VdbeCurrentAddr(v);
9235292660
9235392661
if( nColTest>0 ){
9235492662
int endDistinctTest = sqlite3VdbeMakeLabel(v);
9235592663
int *aGotoChng; /* Array of jump instruction addresses */
92356
- aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
92664
+ aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
9235792665
if( aGotoChng==0 ) continue;
9235892666
9235992667
/*
9236092668
** next_row:
9236192669
** regChng = 0
@@ -92759,11 +93067,11 @@
9275993067
/* Index.aiRowEst may already be set here if there are duplicate
9276093068
** sqlite_stat1 entries for this index. In that case just clobber
9276193069
** the old data with the new instead of allocating a new array. */
9276293070
if( pIndex->aiRowEst==0 ){
9276393071
pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
92764
- if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
93072
+ if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
9276593073
}
9276693074
aiRowEst = pIndex->aiRowEst;
9276793075
#endif
9276893076
pIndex->bUnordered = 0;
9276993077
decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
@@ -92906,11 +93214,11 @@
9290693214
sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
9290793215
char *zSql; /* Text of the SQL statement */
9290893216
Index *pPrevIdx = 0; /* Previous index in the loop */
9290993217
IndexSample *pSample; /* A slot in pIdx->aSample[] */
9291093218
92911
- assert( db->lookaside.bEnabled==0 );
93219
+ assert( db->lookaside.bDisable );
9291293220
zSql = sqlite3MPrintf(db, zSql1, zDb);
9291393221
if( !zSql ){
9291493222
return SQLITE_NOMEM;
9291593223
}
9291693224
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
@@ -93020,11 +93328,11 @@
9302093328
** the Index.aSample[] arrays of all indices.
9302193329
*/
9302293330
static int loadStat4(sqlite3 *db, const char *zDb){
9302393331
int rc = SQLITE_OK; /* Result codes from subroutines */
9302493332
93025
- assert( db->lookaside.bEnabled==0 );
93333
+ assert( db->lookaside.bDisable );
9302693334
if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
9302793335
rc = loadStatTbl(db, 0,
9302893336
"SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
9302993337
"SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
9303093338
zDb
@@ -93102,24 +93410,23 @@
9310293410
9310393411
9310493412
/* Load the statistics from the sqlite_stat4 table. */
9310593413
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
9310693414
if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
93107
- int lookasideEnabled = db->lookaside.bEnabled;
93108
- db->lookaside.bEnabled = 0;
93415
+ db->lookaside.bDisable++;
9310993416
rc = loadStat4(db, sInfo.zDatabase);
93110
- db->lookaside.bEnabled = lookasideEnabled;
93417
+ db->lookaside.bDisable--;
9311193418
}
9311293419
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
9311393420
Index *pIdx = sqliteHashData(i);
9311493421
sqlite3_free(pIdx->aiRowEst);
9311593422
pIdx->aiRowEst = 0;
9311693423
}
9311793424
#endif
9311893425
9311993426
if( rc==SQLITE_NOMEM ){
93120
- db->mallocFailed = 1;
93427
+ sqlite3OomFault(db);
9312193428
}
9312293429
return rc;
9312393430
}
9312493431
9312593432
@@ -93236,11 +93543,11 @@
9323693543
9323793544
/* Allocate the new entry in the db->aDb[] array and initialize the schema
9323893545
** hash tables.
9323993546
*/
9324093547
if( db->aDb==db->aDbStatic ){
93241
- aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
93548
+ aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
9324293549
if( aNew==0 ) return;
9324393550
memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
9324493551
}else{
9324593552
aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
9324693553
if( aNew==0 ) return;
@@ -93254,11 +93561,11 @@
9325493561
** or may not be initialized.
9325593562
*/
9325693563
flags = db->openFlags;
9325793564
rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
9325893565
if( rc!=SQLITE_OK ){
93259
- if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
93566
+ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
9326093567
sqlite3_result_error(context, zErr, -1);
9326193568
sqlite3_free(zErr);
9326293569
return;
9326393570
}
9326493571
assert( pVfs );
@@ -93283,11 +93590,12 @@
9328393590
pPager = sqlite3BtreePager(aNew->pBt);
9328493591
sqlite3PagerLockingMode(pPager, db->dfltLockMode);
9328593592
sqlite3BtreeSecureDelete(aNew->pBt,
9328693593
sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
9328793594
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
93288
- sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
93595
+ sqlite3BtreeSetPagerFlags(aNew->pBt,
93596
+ PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
9328993597
#endif
9329093598
sqlite3BtreeLeave(aNew->pBt);
9329193599
}
9329293600
aNew->safety_level = 3;
9329393601
aNew->zName = sqlite3DbStrDup(db, zName);
@@ -93356,11 +93664,11 @@
9335693664
db->aDb[iDb].pSchema = 0;
9335793665
}
9335893666
sqlite3ResetAllSchemasOfConnection(db);
9335993667
db->nDb = iDb;
9336093668
if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
93361
- db->mallocFailed = 1;
93669
+ sqlite3OomFault(db);
9336293670
sqlite3DbFree(db, zErrDyn);
9336393671
zErrDyn = sqlite3MPrintf(db, "out of memory");
9336493672
}else if( zErrDyn==0 ){
9336593673
zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
9336693674
}
@@ -94053,11 +94361,11 @@
9405394361
p->iTab = iTab;
9405494362
p->isWriteLock = isWriteLock;
9405594363
p->zName = zName;
9405694364
}else{
9405794365
pToplevel->nTableLock = 0;
94058
- pToplevel->db->mallocFailed = 1;
94366
+ sqlite3OomFault(pToplevel->db);
9405994367
}
9406094368
}
9406194369
9406294370
/*
9406394371
** Code an OP_TableLock instruction for each table locked by the
@@ -94901,11 +95209,11 @@
9490195209
}
9490295210
}
9490395211
9490495212
pTable = sqlite3DbMallocZero(db, sizeof(Table));
9490595213
if( pTable==0 ){
94906
- db->mallocFailed = 1;
95214
+ assert( db->mallocFailed );
9490795215
pParse->rc = SQLITE_NOMEM;
9490895216
pParse->nErr++;
9490995217
goto begin_table_error;
9491095218
}
9491195219
pTable->zName = zName;
@@ -94958,14 +95266,12 @@
9495895266
sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
9495995267
sqlite3VdbeUsesBtree(v, iDb);
9496095268
addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
9496195269
fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
9496295270
1 : SQLITE_MAX_FILE_FORMAT;
94963
- sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
94964
- sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
94965
- sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
94966
- sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
95271
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
95272
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
9496795273
sqlite3VdbeJumpHere(v, addr1);
9496895274
9496995275
/* This just creates a place-holder record in the sqlite_master table.
9497095276
** The record created does not contain anything yet. It will be replaced
9497195277
** by the real entry in code generated at sqlite3EndTable().
@@ -95446,17 +95752,15 @@
9544695752
** set back to prior value. But schema changes are infrequent
9544795753
** and the probability of hitting the same cookie value is only
9544895754
** 1 chance in 2^32. So we're safe enough.
9544995755
*/
9545095756
SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
95451
- int r1 = sqlite3GetTempReg(pParse);
9545295757
sqlite3 *db = pParse->db;
9545395758
Vdbe *v = pParse->pVdbe;
9545495759
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95455
- sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
95456
- sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
95457
- sqlite3ReleaseTempReg(pParse, r1);
95760
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
95761
+ db->aDb[iDb].pSchema->schema_cookie+1);
9545895762
}
9545995763
9546095764
/*
9546195765
** Measure the number of characters needed to output the given
9546295766
** identifier. The number returned includes any quotes used
@@ -95534,11 +95838,11 @@
9553495838
zEnd = "\n)";
9553595839
}
9553695840
n += 35 + 6*p->nCol;
9553795841
zStmt = sqlite3DbMallocRaw(0, n);
9553895842
if( zStmt==0 ){
95539
- db->mallocFailed = 1;
95843
+ sqlite3OomFault(db);
9554095844
return 0;
9554195845
}
9554295846
sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
9554395847
k = sqlite3Strlen30(zStmt);
9554495848
identPut(zStmt, &k, p->zName);
@@ -95683,12 +95987,11 @@
9568395987
** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
9568495988
*/
9568595989
if( pTab->iPKey>=0 ){
9568695990
ExprList *pList;
9568795991
Token ipkToken;
95688
- ipkToken.z = pTab->aCol[pTab->iPKey].zName;
95689
- ipkToken.n = sqlite3Strlen30(ipkToken.z);
95992
+ sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
9569095993
pList = sqlite3ExprListAppend(pParse, 0,
9569195994
sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
9569295995
if( pList==0 ) return;
9569395996
pList->a[0].sortOrder = pParse->iPkSortOrder;
9569495997
assert( pParse->pNewTable==pTab );
@@ -95934,11 +96237,11 @@
9593496237
pParse->nTab = 2;
9593596238
addrTop = sqlite3VdbeCurrentAddr(v) + 1;
9593696239
sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
9593796240
sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
9593896241
sqlite3Select(pParse, pSelect, &dest);
95939
- sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
96242
+ sqlite3VdbeEndCoroutine(v, regYield);
9594096243
sqlite3VdbeJumpHere(v, addrTop - 1);
9594196244
if( pParse->nErr ) return;
9594296245
pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
9594396246
if( pSelTab==0 ) return;
9594496247
assert( p->aCol==0 );
@@ -96018,11 +96321,11 @@
9601896321
Schema *pSchema = p->pSchema;
9601996322
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9602096323
pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
9602196324
if( pOld ){
9602296325
assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
96023
- db->mallocFailed = 1;
96326
+ sqlite3OomFault(db);
9602496327
return;
9602596328
}
9602696329
pParse->pNewTable = 0;
9602796330
db->flags |= SQLITE_InternChanges;
9602896331
@@ -96122,11 +96425,10 @@
9612296425
Select *pSel; /* Copy of the SELECT that implements the view */
9612396426
int nErr = 0; /* Number of errors encountered */
9612496427
int n; /* Temporarily holds the number of cursors assigned */
9612596428
sqlite3 *db = pParse->db; /* Database connection for malloc errors */
9612696429
sqlite3_xauth xAuth; /* Saved xAuth pointer */
96127
- u8 bEnabledLA; /* Saved db->lookaside.bEnabled state */
9612896430
9612996431
assert( pTable );
9613096432
9613196433
#ifndef SQLITE_OMIT_VIRTUALTABLE
9613296434
if( sqlite3VtabCallConnect(pParse, pTable) ){
@@ -96168,30 +96470,31 @@
9616896470
** to the elements of the FROM clause. But we do not want these changes
9616996471
** to be permanent. So the computation is done on a copy of the SELECT
9617096472
** statement that defines the view.
9617196473
*/
9617296474
assert( pTable->pSelect );
96173
- bEnabledLA = db->lookaside.bEnabled;
9617496475
if( pTable->pCheck ){
96175
- db->lookaside.bEnabled = 0;
96476
+ db->lookaside.bDisable++;
9617696477
sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
9617796478
&pTable->nCol, &pTable->aCol);
96479
+ db->lookaside.bDisable--;
9617896480
}else{
9617996481
pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
9618096482
if( pSel ){
9618196483
n = pParse->nTab;
9618296484
sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
9618396485
pTable->nCol = -1;
96184
- db->lookaside.bEnabled = 0;
96486
+ db->lookaside.bDisable++;
9618596487
#ifndef SQLITE_OMIT_AUTHORIZATION
9618696488
xAuth = db->xAuth;
9618796489
db->xAuth = 0;
9618896490
pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
9618996491
db->xAuth = xAuth;
9619096492
#else
9619196493
pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
9619296494
#endif
96495
+ db->lookaside.bDisable--;
9619396496
pParse->nTab = n;
9619496497
if( pSelTab ){
9619596498
assert( pTable->aCol==0 );
9619696499
pTable->nCol = pSelTab->nCol;
9619796500
pTable->aCol = pSelTab->aCol;
@@ -96206,11 +96509,10 @@
9620696509
sqlite3SelectDelete(db, pSel);
9620796510
} else {
9620896511
nErr++;
9620996512
}
9621096513
}
96211
- db->lookaside.bEnabled = bEnabledLA;
9621296514
pTable->pSchema->schemaFlags |= DB_UnresetViews;
9621396515
#endif /* SQLITE_OMIT_VIEW */
9621496516
return nErr;
9621596517
}
9621696518
#endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
@@ -96672,11 +96974,11 @@
9667296974
assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
9667396975
pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
9667496976
pFKey->zTo, (void *)pFKey
9667596977
);
9667696978
if( pNextTo==pFKey ){
96677
- db->mallocFailed = 1;
96979
+ sqlite3OomFault(db);
9667896980
goto fk_end;
9667996981
}
9668096982
if( pNextTo ){
9668196983
assert( pNextTo->pPrevTo==0 );
9668296984
pFKey->pNextTo = pNextTo;
@@ -97032,12 +97334,11 @@
9703297334
** key out of the last column added to the table under construction.
9703397335
** So create a fake list to simulate this.
9703497336
*/
9703597337
if( pList==0 ){
9703697338
Token prevCol;
97037
- prevCol.z = pTab->aCol[pTab->nCol-1].zName;
97038
- prevCol.n = sqlite3Strlen30(prevCol.z);
97339
+ sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
9703997340
pList = sqlite3ExprListAppend(pParse, 0,
9704097341
sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
9704197342
if( pList==0 ) goto exit_create_index;
9704297343
assert( pList->nExpr==1 );
9704397344
sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -97255,11 +97556,11 @@
9725597556
assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
9725697557
p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
9725797558
pIndex->zName, pIndex);
9725897559
if( p ){
9725997560
assert( p==pIndex ); /* Malloc must have failed */
97260
- db->mallocFailed = 1;
97561
+ sqlite3OomFault(db);
9726197562
goto exit_create_index;
9726297563
}
9726397564
db->flags |= SQLITE_InternChanges;
9726497565
if( pTblName!=0 ){
9726597566
pIndex->tnum = db->init.newTnum;
@@ -97684,12 +97985,13 @@
9768497985
Token *pTable, /* Table to append */
9768597986
Token *pDatabase /* Database of the table */
9768697987
){
9768797988
struct SrcList_item *pItem;
9768897989
assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
97990
+ assert( db!=0 );
9768997991
if( pList==0 ){
97690
- pList = sqlite3DbMallocRaw(db, sizeof(SrcList) );
97992
+ pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
9769197993
if( pList==0 ) return 0;
9769297994
pList->nAlloc = 1;
9769397995
pList->nSrc = 0;
9769497996
}
9769597997
pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
@@ -97869,21 +98171,20 @@
9786998171
p->a[0].fg.jointype = 0;
9787098172
}
9787198173
}
9787298174
9787398175
/*
97874
-** Begin a transaction
98176
+** Generate VDBE code for a BEGIN statement.
9787598177
*/
9787698178
SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
9787798179
sqlite3 *db;
9787898180
Vdbe *v;
9787998181
int i;
9788098182
9788198183
assert( pParse!=0 );
9788298184
db = pParse->db;
9788398185
assert( db!=0 );
97884
-/* if( db->aDb[0].pBt==0 ) return; */
9788598186
if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
9788698187
return;
9788798188
}
9788898189
v = sqlite3GetVdbe(pParse);
9788998190
if( !v ) return;
@@ -97891,15 +98192,15 @@
9789198192
for(i=0; i<db->nDb; i++){
9789298193
sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
9789398194
sqlite3VdbeUsesBtree(v, i);
9789498195
}
9789598196
}
97896
- sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
98197
+ sqlite3VdbeAddOp0(v, OP_AutoCommit);
9789798198
}
9789898199
9789998200
/*
97900
-** Commit a transaction
98201
+** Generate VDBE code for a COMMIT statement.
9790198202
*/
9790298203
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
9790398204
Vdbe *v;
9790498205
9790598206
assert( pParse!=0 );
@@ -97907,16 +98208,16 @@
9790798208
if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
9790898209
return;
9790998210
}
9791098211
v = sqlite3GetVdbe(pParse);
9791198212
if( v ){
97912
- sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
98213
+ sqlite3VdbeAddOp1(v, OP_AutoCommit, 1);
9791398214
}
9791498215
}
9791598216
9791698217
/*
97917
-** Rollback a transaction
98218
+** Generate VDBE code for a ROLLBACK statement.
9791898219
*/
9791998220
SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
9792098221
Vdbe *v;
9792198222
9792298223
assert( pParse!=0 );
@@ -97974,11 +98275,11 @@
9797498275
return 1;
9797598276
}
9797698277
db->aDb[1].pBt = pBt;
9797798278
assert( db->aDb[1].pSchema );
9797898279
if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
97979
- db->mallocFailed = 1;
98280
+ sqlite3OomFault(db);
9798098281
return 1;
9798198282
}
9798298283
}
9798398284
return 0;
9798498285
}
@@ -98109,18 +98410,18 @@
9810998410
StrAccum errMsg;
9811098411
Table *pTab = pIdx->pTable;
9811198412
9811298413
sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
9811398414
if( pIdx->aColExpr ){
98114
- sqlite3XPrintf(&errMsg, 0, "index '%q'", pIdx->zName);
98415
+ sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
9811598416
}else{
9811698417
for(j=0; j<pIdx->nKeyCol; j++){
9811798418
char *zCol;
9811898419
assert( pIdx->aiColumn[j]>=0 );
9811998420
zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
9812098421
if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
98121
- sqlite3XPrintf(&errMsg, 0, "%s.%s", pTab->zName, zCol);
98422
+ sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol);
9812298423
}
9812398424
}
9812498425
zErr = sqlite3StrAccumFinish(&errMsg);
9812598426
sqlite3HaltConstraint(pParse,
9812698427
IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
@@ -98349,14 +98650,13 @@
9834998650
int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
9835098651
pNew = sqlite3DbRealloc(db, pWith, nByte);
9835198652
}else{
9835298653
pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
9835398654
}
98354
- assert( zName!=0 || pNew==0 );
98355
- assert( db->mallocFailed==0 || pNew==0 );
98655
+ assert( (pNew!=0 && zName!=0) || db->mallocFailed );
9835698656
98357
- if( pNew==0 ){
98657
+ if( db->mallocFailed ){
9835898658
sqlite3ExprListDelete(db, pArglist);
9835998659
sqlite3SelectDelete(db, pQuery);
9836098660
sqlite3DbFree(db, zName);
9836198661
pNew = pWith;
9836298662
}else{
@@ -98566,11 +98866,11 @@
9856698866
** return the pColl pointer to be deleted (because it wasn't added
9856798867
** to the hash table).
9856898868
*/
9856998869
assert( pDel==0 || pDel==pColl );
9857098870
if( pDel!=0 ){
98571
- db->mallocFailed = 1;
98871
+ sqlite3OomFault(db);
9857298872
sqlite3DbFree(db, pDel);
9857398873
pColl = 0;
9857498874
}
9857598875
}
9857698876
}
@@ -98854,11 +99154,11 @@
9885499154
p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
9885599155
}else{
9885699156
p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
9885799157
}
9885899158
if( !p ){
98859
- db->mallocFailed = 1;
99159
+ sqlite3OomFault(db);
9886099160
}else if ( 0==p->file_format ){
9886199161
sqlite3HashInit(&p->tblHash);
9886299162
sqlite3HashInit(&p->idxHash);
9886399163
sqlite3HashInit(&p->trigHash);
9886499164
sqlite3HashInit(&p->fkeyHash);
@@ -99308,11 +99608,11 @@
9930899608
if( eOnePass!=ONEPASS_OFF ){
9930999609
/* For ONEPASS, no need to store the rowid/primary-key. There is only
9931099610
** one, so just keep it in its register(s) and fall through to the
9931199611
** delete code. */
9931299612
nKey = nPk; /* OP_Found will use an unpacked key */
99313
- aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
99613
+ aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
9931499614
if( aToOpen==0 ){
9931599615
sqlite3WhereEnd(pWInfo);
9931699616
goto delete_from_cleanup;
9931799617
}
9931899618
memset(aToOpen, 1, nIdx+1);
@@ -99348,17 +99648,16 @@
9934899648
** only effect this statement has is to fire the INSTEAD OF
9934999649
** triggers.
9935099650
*/
9935199651
if( !isView ){
9935299652
int iAddrOnce = 0;
99353
- u8 p5 = (eOnePass==ONEPASS_OFF ? 0 : OPFLAG_FORDELETE);
9935499653
if( eOnePass==ONEPASS_MULTI ){
9935599654
iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
9935699655
}
9935799656
testcase( IsVirtual(pTab) );
99358
- sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, p5, iTabCur,
99359
- aToOpen, &iDataCur, &iIdxCur);
99657
+ sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
99658
+ iTabCur, aToOpen, &iDataCur, &iIdxCur);
9936099659
assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
9936199660
assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
9936299661
if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
9936399662
}
9936499663
@@ -99587,19 +99886,24 @@
9958799886
9958899887
/* Delete the index and table entries. Skip this step if pTab is really
9958999888
** a view (in which case the only effect of the DELETE statement is to
9959099889
** fire the INSTEAD OF triggers). */
9959199890
if( pTab->pSelect==0 ){
99891
+ u8 p5 = 0;
9959299892
sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
9959399893
sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
9959499894
if( count ){
9959599895
sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
9959699896
}
99897
+ if( eMode!=ONEPASS_OFF ){
99898
+ sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
99899
+ }
9959799900
if( iIdxNoSeek>=0 ){
9959899901
sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
9959999902
}
99600
- sqlite3VdbeChangeP5(v, eMode==ONEPASS_MULTI);
99903
+ if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
99904
+ sqlite3VdbeChangeP5(v, p5);
9960199905
}
9960299906
9960399907
/* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
9960499908
** handle rows (possibly in other tables) that refer via a foreign key
9960599909
** to the row just deleted. */
@@ -100005,11 +100309,12 @@
100005100309
if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
100006100310
x.nArg = argc-1;
100007100311
x.nUsed = 0;
100008100312
x.apArg = argv+1;
100009100313
sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
100010
- sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
100314
+ str.printfFlags = SQLITE_PRINTF_SQLFUNC;
100315
+ sqlite3XPrintf(&str, zFormat, &x);
100011100316
n = str.nChar;
100012100317
sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
100013100318
SQLITE_DYNAMIC);
100014100319
}
100015100320
}
@@ -101380,11 +101685,11 @@
101380101685
*/
101381101686
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
101382101687
int rc = sqlite3_overload_function(db, "MATCH", 2);
101383101688
assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
101384101689
if( rc==SQLITE_NOMEM ){
101385
- db->mallocFailed = 1;
101690
+ sqlite3OomFault(db);
101386101691
}
101387101692
}
101388101693
101389101694
/*
101390101695
** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -101795,11 +102100,11 @@
101795102100
if( !zKey ) return 0;
101796102101
if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
101797102102
}
101798102103
}else if( paiCol ){
101799102104
assert( nCol>1 );
101800
- aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
102105
+ aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
101801102106
if( !aiCol ) return 1;
101802102107
*paiCol = aiCol;
101803102108
}
101804102109
101805102110
for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
@@ -102741,11 +103046,10 @@
102741103046
102742103047
action = pFKey->aAction[iAction];
102743103048
pTrigger = pFKey->apTrigger[iAction];
102744103049
102745103050
if( action!=OE_None && !pTrigger ){
102746
- u8 enableLookaside; /* Copy of db->lookaside.bEnabled */
102747103051
char const *zFrom; /* Name of child table */
102748103052
int nFrom; /* Length in bytes of zFrom */
102749103053
Index *pIdx = 0; /* Parent key index for this FK */
102750103054
int *aiCol = 0; /* child table cols -> parent key cols */
102751103055
TriggerStep *pStep = 0; /* First (only) step of trigger program */
@@ -102768,15 +103072,13 @@
102768103072
102769103073
iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
102770103074
assert( iFromCol>=0 );
102771103075
assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
102772103076
assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
102773
- tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
102774
- tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
102775
-
102776
- tToCol.n = sqlite3Strlen30(tToCol.z);
102777
- tFromCol.n = sqlite3Strlen30(tFromCol.z);
103077
+ sqlite3TokenInit(&tToCol,
103078
+ pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
103079
+ sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
102778103080
102779103081
/* Create the expression "OLD.zToCol = zFromCol". It is important
102780103082
** that the "OLD.zToCol" term is on the LHS of the = operator, so
102781103083
** that the affinity and collation sequence associated with the
102782103084
** parent table are used for the comparison. */
@@ -102852,12 +103154,11 @@
102852103154
);
102853103155
pWhere = 0;
102854103156
}
102855103157
102856103158
/* Disable lookaside memory allocation */
102857
- enableLookaside = db->lookaside.bEnabled;
102858
- db->lookaside.bEnabled = 0;
103159
+ db->lookaside.bDisable++;
102859103160
102860103161
pTrigger = (Trigger *)sqlite3DbMallocZero(db,
102861103162
sizeof(Trigger) + /* struct Trigger */
102862103163
sizeof(TriggerStep) + /* Single step in trigger program */
102863103164
nFrom + 1 /* Space for pStep->zTarget */
@@ -102875,11 +103176,11 @@
102875103176
pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
102876103177
}
102877103178
}
102878103179
102879103180
/* Re-enable the lookaside buffer, if it was disabled earlier. */
102880
- db->lookaside.bEnabled = enableLookaside;
103181
+ db->lookaside.bDisable--;
102881103182
102882103183
sqlite3ExprDelete(db, pWhere);
102883103184
sqlite3ExprDelete(db, pWhen);
102884103185
sqlite3ExprListDelete(db, pList);
102885103186
sqlite3SelectDelete(db, pSelect);
@@ -103070,11 +103371,11 @@
103070103371
*/
103071103372
int n;
103072103373
Table *pTab = pIdx->pTable;
103073103374
pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
103074103375
if( !pIdx->zColAff ){
103075
- db->mallocFailed = 1;
103376
+ sqlite3OomFault(db);
103076103377
return 0;
103077103378
}
103078103379
for(n=0; n<pIdx->nColumn; n++){
103079103380
i16 x = pIdx->aiColumn[n];
103080103381
if( x>=0 ){
@@ -103121,11 +103422,11 @@
103121103422
char *zColAff = pTab->zColAff;
103122103423
if( zColAff==0 ){
103123103424
sqlite3 *db = sqlite3VdbeDb(v);
103124103425
zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
103125103426
if( !zColAff ){
103126
- db->mallocFailed = 1;
103427
+ sqlite3OomFault(db);
103127103428
return;
103128103429
}
103129103430
103130103431
for(i=0; i<pTab->nCol; i++){
103131103432
zColAff[i] = pTab->aCol[i].affinity;
@@ -103217,11 +103518,11 @@
103217103518
AutoincInfo *pInfo;
103218103519
103219103520
pInfo = pToplevel->pAinc;
103220103521
while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
103221103522
if( pInfo==0 ){
103222
- pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
103523
+ pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
103223103524
if( pInfo==0 ) return 0;
103224103525
pInfo->pNext = pToplevel->pAinc;
103225103526
pToplevel->pAinc = pInfo;
103226103527
pInfo->pTab = pTab;
103227103528
pInfo->iDb = iDb;
@@ -103241,47 +103542,59 @@
103241103542
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
103242103543
AutoincInfo *p; /* Information about an AUTOINCREMENT */
103243103544
sqlite3 *db = pParse->db; /* The database connection */
103244103545
Db *pDb; /* Database only autoinc table */
103245103546
int memId; /* Register holding max rowid */
103246
- int addr; /* A VDBE address */
103247103547
Vdbe *v = pParse->pVdbe; /* VDBE under construction */
103248103548
103249103549
/* This routine is never called during trigger-generation. It is
103250103550
** only called from the top-level */
103251103551
assert( pParse->pTriggerTab==0 );
103252103552
assert( sqlite3IsToplevel(pParse) );
103253103553
103254103554
assert( v ); /* We failed long ago if this is not so */
103255103555
for(p = pParse->pAinc; p; p = p->pNext){
103556
+ static const int iLn = VDBE_OFFSET_LINENO(2);
103557
+ static const VdbeOpList autoInc[] = {
103558
+ /* 0 */ {OP_Null, 0, 0, 0},
103559
+ /* 1 */ {OP_Rewind, 0, 9, 0},
103560
+ /* 2 */ {OP_Column, 0, 0, 0},
103561
+ /* 3 */ {OP_Ne, 0, 7, 0},
103562
+ /* 4 */ {OP_Rowid, 0, 0, 0},
103563
+ /* 5 */ {OP_Column, 0, 1, 0},
103564
+ /* 6 */ {OP_Goto, 0, 9, 0},
103565
+ /* 7 */ {OP_Next, 0, 2, 0},
103566
+ /* 8 */ {OP_Integer, 0, 0, 0},
103567
+ /* 9 */ {OP_Close, 0, 0, 0}
103568
+ };
103569
+ VdbeOp *aOp;
103256103570
pDb = &db->aDb[p->iDb];
103257103571
memId = p->regCtr;
103258103572
assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103259103573
sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
103260
- sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
103261
- addr = sqlite3VdbeCurrentAddr(v);
103262103574
sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
103263
- sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
103264
- sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
103265
- sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
103266
- sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
103267
- sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
103268
- sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
103269
- sqlite3VdbeGoto(v, addr+9);
103270
- sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
103271
- sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
103272
- sqlite3VdbeAddOp0(v, OP_Close);
103575
+ aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
103576
+ if( aOp==0 ) break;
103577
+ aOp[0].p2 = memId;
103578
+ aOp[0].p3 = memId+1;
103579
+ aOp[2].p3 = memId;
103580
+ aOp[3].p1 = memId-1;
103581
+ aOp[3].p3 = memId;
103582
+ aOp[3].p5 = SQLITE_JUMPIFNULL;
103583
+ aOp[4].p2 = memId+1;
103584
+ aOp[5].p3 = memId;
103585
+ aOp[8].p2 = memId;
103273103586
}
103274103587
}
103275103588
103276103589
/*
103277103590
** Update the maximum rowid for an autoincrement calculation.
103278103591
**
103279
-** This routine should be called when the top of the stack holds a
103592
+** This routine should be called when the regRowid register holds a
103280103593
** new rowid that is about to be inserted. If that new rowid is
103281103594
** larger than the maximum rowid in the memId memory cell, then the
103282
-** memory cell is updated. The stack is unchanged.
103595
+** memory cell is updated.
103283103596
*/
103284103597
static void autoIncStep(Parse *pParse, int memId, int regRowid){
103285103598
if( memId>0 ){
103286103599
sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
103287103600
}
@@ -103292,34 +103605,47 @@
103292103605
** maximum rowid values back into the sqlite_sequence register.
103293103606
** Every statement that might do an INSERT into an autoincrement
103294103607
** table (either directly or through triggers) needs to call this
103295103608
** routine just before the "exit" code.
103296103609
*/
103297
-SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
103610
+static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
103298103611
AutoincInfo *p;
103299103612
Vdbe *v = pParse->pVdbe;
103300103613
sqlite3 *db = pParse->db;
103301103614
103302103615
assert( v );
103303103616
for(p = pParse->pAinc; p; p = p->pNext){
103617
+ static const int iLn = VDBE_OFFSET_LINENO(2);
103618
+ static const VdbeOpList autoIncEnd[] = {
103619
+ /* 0 */ {OP_NotNull, 0, 2, 0},
103620
+ /* 1 */ {OP_NewRowid, 0, 0, 0},
103621
+ /* 2 */ {OP_MakeRecord, 0, 2, 0},
103622
+ /* 3 */ {OP_Insert, 0, 0, 0},
103623
+ /* 4 */ {OP_Close, 0, 0, 0}
103624
+ };
103625
+ VdbeOp *aOp;
103304103626
Db *pDb = &db->aDb[p->iDb];
103305
- int addr1;
103306103627
int iRec;
103307103628
int memId = p->regCtr;
103308103629
103309103630
iRec = sqlite3GetTempReg(pParse);
103310103631
assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103311103632
sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
103312
- addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
103313
- sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
103314
- sqlite3VdbeJumpHere(v, addr1);
103315
- sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
103316
- sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
103317
- sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
103318
- sqlite3VdbeAddOp0(v, OP_Close);
103633
+ aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
103634
+ if( aOp==0 ) break;
103635
+ aOp[0].p1 = memId+1;
103636
+ aOp[1].p2 = memId+1;
103637
+ aOp[2].p1 = memId-1;
103638
+ aOp[2].p3 = iRec;
103639
+ aOp[3].p2 = iRec;
103640
+ aOp[3].p3 = memId+1;
103641
+ aOp[3].p5 = OPFLAG_APPEND;
103319103642
sqlite3ReleaseTempReg(pParse, iRec);
103320103643
}
103644
+}
103645
+SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
103646
+ if( pParse->pAinc ) autoIncrementEnd(pParse);
103321103647
}
103322103648
#else
103323103649
/*
103324103650
** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
103325103651
** above are all no-ops
@@ -103647,11 +103973,11 @@
103647103973
dest.iSdst = bIdListInOrder ? regData : 0;
103648103974
dest.nSdst = pTab->nCol;
103649103975
rc = sqlite3Select(pParse, pSelect, &dest);
103650103976
regFromSelect = dest.iSdst;
103651103977
if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
103652
- sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
103978
+ sqlite3VdbeEndCoroutine(v, regYield);
103653103979
sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
103654103980
assert( pSelect->pEList );
103655103981
nColumn = pSelect->pEList->nExpr;
103656103982
103657103983
/* Set useTempTable to TRUE if the result of the SELECT statement
@@ -103749,11 +104075,11 @@
103749104075
/* If this is not a view, open the table and and all indices */
103750104076
if( !isView ){
103751104077
int nIdx;
103752104078
nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
103753104079
&iDataCur, &iIdxCur);
103754
- aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
104080
+ aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
103755104081
if( aRegIdx==0 ){
103756104082
goto insert_cleanup;
103757104083
}
103758104084
for(i=0; i<nIdx; i++){
103759104085
aRegIdx[i] = ++pParse->nMem;
@@ -103957,11 +104283,11 @@
103957104283
}else
103958104284
#endif
103959104285
{
103960104286
int isReplace; /* Set to true if constraints may cause a replace */
103961104287
sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
103962
- regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
104288
+ regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
103963104289
);
103964104290
sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
103965104291
sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
103966104292
regIns, aRegIdx, 0, appendFlag, isReplace==0);
103967104293
}
@@ -104038,10 +104364,63 @@
104038104364
#undef pTrigger
104039104365
#endif
104040104366
#ifdef tmask
104041104367
#undef tmask
104042104368
#endif
104369
+
104370
+/*
104371
+** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
104372
+*/
104373
+#define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
104374
+#define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
104375
+
104376
+/* This is the Walker callback from checkConstraintUnchanged(). Set
104377
+** bit 0x01 of pWalker->eCode if
104378
+** pWalker->eCode to 0 if this expression node references any of the
104379
+** columns that are being modifed by an UPDATE statement.
104380
+*/
104381
+static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
104382
+ if( pExpr->op==TK_COLUMN ){
104383
+ assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
104384
+ if( pExpr->iColumn>=0 ){
104385
+ if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
104386
+ pWalker->eCode |= CKCNSTRNT_COLUMN;
104387
+ }
104388
+ }else{
104389
+ pWalker->eCode |= CKCNSTRNT_ROWID;
104390
+ }
104391
+ }
104392
+ return WRC_Continue;
104393
+}
104394
+
104395
+/*
104396
+** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
104397
+** only columns that are modified by the UPDATE are those for which
104398
+** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
104399
+**
104400
+** Return true if CHECK constraint pExpr does not use any of the
104401
+** changing columns (or the rowid if it is changing). In other words,
104402
+** return true if this CHECK constraint can be skipped when validating
104403
+** the new row in the UPDATE statement.
104404
+*/
104405
+static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
104406
+ Walker w;
104407
+ memset(&w, 0, sizeof(w));
104408
+ w.eCode = 0;
104409
+ w.xExprCallback = checkConstraintExprNode;
104410
+ w.u.aiCol = aiChng;
104411
+ sqlite3WalkExpr(&w, pExpr);
104412
+ if( !chngRowid ){
104413
+ testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
104414
+ w.eCode &= ~CKCNSTRNT_ROWID;
104415
+ }
104416
+ testcase( w.eCode==0 );
104417
+ testcase( w.eCode==CKCNSTRNT_COLUMN );
104418
+ testcase( w.eCode==CKCNSTRNT_ROWID );
104419
+ testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
104420
+ return !w.eCode;
104421
+}
104043104422
104044104423
/*
104045104424
** Generate code to do constraint checks prior to an INSERT or an UPDATE
104046104425
** on table pTab.
104047104426
**
@@ -104133,11 +104512,12 @@
104133104512
int regNewData, /* First register in a range holding values to insert */
104134104513
int regOldData, /* Previous content. 0 for INSERTs */
104135104514
u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
104136104515
u8 overrideError, /* Override onError to this if not OE_Default */
104137104516
int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
104138
- int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
104517
+ int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
104518
+ int *aiChng /* column i is unchanged if aiChng[i]<0 */
104139104519
){
104140104520
Vdbe *v; /* VDBE under constrution */
104141104521
Index *pIdx; /* Pointer to one of the indices */
104142104522
Index *pPk = 0; /* The PRIMARY KEY index */
104143104523
sqlite3 *db; /* Database connection */
@@ -104179,14 +104559,18 @@
104179104559
104180104560
/* Test all NOT NULL constraints.
104181104561
*/
104182104562
for(i=0; i<nCol; i++){
104183104563
if( i==pTab->iPKey ){
104564
+ continue; /* ROWID is never NULL */
104565
+ }
104566
+ if( aiChng && aiChng[i]<0 ){
104567
+ /* Don't bother checking for NOT NULL on columns that do not change */
104184104568
continue;
104185104569
}
104186104570
onError = pTab->aCol[i].notNull;
104187
- if( onError==OE_None ) continue;
104571
+ if( onError==OE_None ) continue; /* This column is allowed to be NULL */
104188104572
if( overrideError!=OE_Default ){
104189104573
onError = overrideError;
104190104574
}else if( onError==OE_Default ){
104191104575
onError = OE_Abort;
104192104576
}
@@ -104231,12 +104615,15 @@
104231104615
if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
104232104616
ExprList *pCheck = pTab->pCheck;
104233104617
pParse->ckBase = regNewData+1;
104234104618
onError = overrideError!=OE_Default ? overrideError : OE_Abort;
104235104619
for(i=0; i<pCheck->nExpr; i++){
104236
- int allOk = sqlite3VdbeMakeLabel(v);
104237
- sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
104620
+ int allOk;
104621
+ Expr *pExpr = pCheck->a[i].pExpr;
104622
+ if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
104623
+ allOk = sqlite3VdbeMakeLabel(v);
104624
+ sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
104238104625
if( onError==OE_Ignore ){
104239104626
sqlite3VdbeGoto(v, ignoreDest);
104240104627
}else{
104241104628
char *zName = pCheck->a[i].zName;
104242104629
if( zName==0 ) zName = pTab->zName;
@@ -104634,11 +105021,11 @@
104634105021
*/
104635105022
SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
104636105023
Parse *pParse, /* Parsing context */
104637105024
Table *pTab, /* Table to be opened */
104638105025
int op, /* OP_OpenRead or OP_OpenWrite */
104639
- u8 p5, /* P5 value for OP_Open* instructions */
105026
+ u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
104640105027
int iBase, /* Use this for the table cursor, if there is one */
104641105028
u8 *aToOpen, /* If not NULL: boolean for each table and index */
104642105029
int *piDataCur, /* Write the database source cursor number here */
104643105030
int *piIdxCur /* Write the first index cursor number here */
104644105031
){
@@ -104669,18 +105056,19 @@
104669105056
}
104670105057
if( piIdxCur ) *piIdxCur = iBase;
104671105058
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
104672105059
int iIdxCur = iBase++;
104673105060
assert( pIdx->pSchema==pTab->pSchema );
104674
- if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
104675
- *piDataCur = iIdxCur;
104676
- }
104677105061
if( aToOpen==0 || aToOpen[i+1] ){
104678105062
sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
104679105063
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
104680
- sqlite3VdbeChangeP5(v, p5);
104681105064
VdbeComment((v, "%s", pIdx->zName));
105065
+ }
105066
+ if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
105067
+ if( piDataCur ) *piDataCur = iIdxCur;
105068
+ }else{
105069
+ sqlite3VdbeChangeP5(v, p5);
104682105070
}
104683105071
}
104684105072
if( iBase>pParse->nTab ) pParse->nTab = iBase;
104685105073
return i;
104686105074
}
@@ -105167,11 +105555,11 @@
105167105555
if( rc==SQLITE_ROW ){
105168105556
azVals = &azCols[nCol];
105169105557
for(i=0; i<nCol; i++){
105170105558
azVals[i] = (char *)sqlite3_column_text(pStmt, i);
105171105559
if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
105172
- db->mallocFailed = 1;
105560
+ sqlite3OomFault(db);
105173105561
goto exec_out;
105174105562
}
105175105563
}
105176105564
}
105177105565
if( xCallback(pArg, nCol, azVals, azCols) ){
@@ -107055,32 +107443,35 @@
107055107443
/************** End of pragma.h **********************************************/
107056107444
/************** Continuing where we left off in pragma.c *********************/
107057107445
107058107446
/*
107059107447
** Interpret the given string as a safety level. Return 0 for OFF,
107060
-** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
107061
-** unrecognized string argument. The FULL option is disallowed
107448
+** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
107449
+** unrecognized string argument. The FULL and EXTRA option is disallowed
107062107450
** if the omitFull parameter it 1.
107063107451
**
107064107452
** Note that the values returned are one less that the values that
107065107453
** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
107066107454
** to support legacy SQL code. The safety level used to be boolean
107067107455
** and older scripts may have used numbers 0 for OFF and 1 for ON.
107068107456
*/
107069107457
static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
107070
- /* 123456789 123456789 */
107071
- static const char zText[] = "onoffalseyestruefull";
107072
- static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
107073
- static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
107074
- static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
107458
+ /* 123456789 123456789 123 */
107459
+ static const char zText[] = "onoffalseyestruextrafull";
107460
+ static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
107461
+ static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
107462
+ static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
107463
+ /* on no off false yes true extra full */
107075107464
int i, n;
107076107465
if( sqlite3Isdigit(*z) ){
107077107466
return (u8)sqlite3Atoi(z);
107078107467
}
107079107468
n = sqlite3Strlen30(z);
107080
- for(i=0; i<ArraySize(iLength)-omitFull; i++){
107081
- if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
107469
+ for(i=0; i<ArraySize(iLength); i++){
107470
+ if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
107471
+ && (!omitFull || iValue[i]<=1)
107472
+ ){
107082107473
return iValue[i];
107083107474
}
107084107475
}
107085107476
return dflt;
107086107477
}
@@ -107467,12 +107858,11 @@
107467107858
aOp[1].p1 = iDb;
107468107859
aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
107469107860
}else{
107470107861
int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
107471107862
sqlite3BeginWriteOperation(pParse, 0, iDb);
107472
- sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
107473
- sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
107863
+ sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
107474107864
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107475107865
pDb->pSchema->cache_size = size;
107476107866
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
107477107867
}
107478107868
break;
@@ -107499,11 +107889,11 @@
107499107889
/* Malloc may fail when setting the page-size, as there is an internal
107500107890
** buffer that the pager module resizes using sqlite3_realloc().
107501107891
*/
107502107892
db->nextPagesize = sqlite3Atoi(zRight);
107503107893
if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
107504
- db->mallocFailed = 1;
107894
+ sqlite3OomFault(db);
107505107895
}
107506107896
}
107507107897
break;
107508107898
}
107509107899
@@ -107706,23 +108096,22 @@
107706108096
static const VdbeOpList setMeta6[] = {
107707108097
{ OP_Transaction, 0, 1, 0}, /* 0 */
107708108098
{ OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
107709108099
{ OP_If, 1, 0, 0}, /* 2 */
107710108100
{ OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
107711
- { OP_Integer, 0, 1, 0}, /* 4 */
107712
- { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
108101
+ { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
107713108102
};
107714108103
VdbeOp *aOp;
107715108104
int iAddr = sqlite3VdbeCurrentAddr(v);
107716108105
sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
107717108106
aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
107718108107
if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
107719108108
aOp[0].p1 = iDb;
107720108109
aOp[1].p1 = iDb;
107721108110
aOp[2].p2 = iAddr+4;
107722
- aOp[4].p1 = eAuto - 1;
107723
- aOp[5].p1 = iDb;
108111
+ aOp[4].p1 = iDb;
108112
+ aOp[4].p3 = eAuto - 1;
107724108113
sqlite3VdbeUsesBtree(v, iDb);
107725108114
}
107726108115
}
107727108116
break;
107728108117
}
@@ -107997,11 +108386,11 @@
107997108386
}
107998108387
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
107999108388
108000108389
/*
108001108390
** PRAGMA [schema.]synchronous
108002
- ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
108391
+ ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
108003108392
**
108004108393
** Return or set the local value of the synchronous flag. Changing
108005108394
** the local value does not make changes to the disk file and the
108006108395
** default value will be restored the next time the database is
108007108396
** opened.
@@ -108624,20 +109013,19 @@
108624109013
}
108625109014
{
108626109015
static const int iLn = VDBE_OFFSET_LINENO(2);
108627109016
static const VdbeOpList endCode[] = {
108628109017
{ OP_AddImm, 1, 0, 0}, /* 0 */
108629
- { OP_If, 1, 0, 0}, /* 1 */
109018
+ { OP_If, 1, 4, 0}, /* 1 */
108630109019
{ OP_String8, 0, 3, 0}, /* 2 */
108631
- { OP_ResultRow, 3, 1, 0},
109020
+ { OP_ResultRow, 3, 1, 0}, /* 3 */
108632109021
};
108633109022
VdbeOp *aOp;
108634109023
108635109024
aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
108636109025
if( aOp ){
108637109026
aOp[0].p2 = -mxErr;
108638
- aOp[1].p2 = sqlite3VdbeCurrentAddr(v);
108639109027
aOp[2].p4type = P4_STATIC;
108640109028
aOp[2].p4.z = "ok";
108641109029
}
108642109030
}
108643109031
}
@@ -108751,21 +109139,20 @@
108751109139
sqlite3VdbeUsesBtree(v, iDb);
108752109140
if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
108753109141
/* Write the specified cookie value */
108754109142
static const VdbeOpList setCookie[] = {
108755109143
{ OP_Transaction, 0, 1, 0}, /* 0 */
108756
- { OP_Integer, 0, 1, 0}, /* 1 */
108757
- { OP_SetCookie, 0, 0, 1}, /* 2 */
109144
+ { OP_SetCookie, 0, 0, 0}, /* 1 */
108758109145
};
108759109146
VdbeOp *aOp;
108760109147
sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
108761109148
aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
108762109149
if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108763109150
aOp[0].p1 = iDb;
108764
- aOp[1].p1 = sqlite3Atoi(zRight);
108765
- aOp[2].p1 = iDb;
108766
- aOp[2].p2 = iCookie;
109151
+ aOp[1].p1 = iDb;
109152
+ aOp[1].p2 = iCookie;
109153
+ aOp[1].p3 = sqlite3Atoi(zRight);
108767109154
}else{
108768109155
/* Read the specified cookie value */
108769109156
static const VdbeOpList readCookie[] = {
108770109157
{ OP_Transaction, 0, 0, 0}, /* 0 */
108771109158
{ OP_ReadCookie, 0, 1, 0}, /* 1 */
@@ -109033,15 +109420,14 @@
109033109420
){
109034109421
sqlite3 *db = pData->db;
109035109422
if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
109036109423
char *z;
109037109424
if( zObj==0 ) zObj = "?";
109038
- z = sqlite3_mprintf("malformed database schema (%s)", zObj);
109039
- if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
109425
+ z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
109426
+ if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
109040109427
sqlite3DbFree(db, *pData->pzErrMsg);
109041109428
*pData->pzErrMsg = z;
109042
- if( z==0 ) db->mallocFailed = 1;
109043109429
}
109044109430
pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
109045109431
}
109046109432
109047109433
/*
@@ -109096,11 +109482,11 @@
109096109482
if( db->init.orphanTrigger ){
109097109483
assert( iDb==1 );
109098109484
}else{
109099109485
pData->rc = rc;
109100109486
if( rc==SQLITE_NOMEM ){
109101
- db->mallocFailed = 1;
109487
+ sqlite3OomFault(db);
109102109488
}else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
109103109489
corruptSchema(pData, argv[0], sqlite3_errmsg(db));
109104109490
}
109105109491
}
109106109492
}
@@ -109341,11 +109727,11 @@
109341109727
}
109342109728
sqlite3BtreeLeave(pDb->pBt);
109343109729
109344109730
error_out:
109345109731
if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109346
- db->mallocFailed = 1;
109732
+ sqlite3OomFault(db);
109347109733
}
109348109734
return rc;
109349109735
}
109350109736
109351109737
/*
@@ -109439,11 +109825,11 @@
109439109825
** on the b-tree database, open one now. If a transaction is opened, it
109440109826
** will be closed immediately after reading the meta-value. */
109441109827
if( !sqlite3BtreeIsInReadTrans(pBt) ){
109442109828
rc = sqlite3BtreeBeginTrans(pBt, 0);
109443109829
if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109444
- db->mallocFailed = 1;
109830
+ sqlite3OomFault(db);
109445109831
}
109446109832
if( rc!=SQLITE_OK ) return;
109447109833
openedTransaction = 1;
109448109834
}
109449109835
@@ -109502,10 +109888,15 @@
109502109888
SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
109503109889
if( pParse ){
109504109890
sqlite3 *db = pParse->db;
109505109891
sqlite3DbFree(db, pParse->aLabel);
109506109892
sqlite3ExprListDelete(db, pParse->pConstExpr);
109893
+ if( db ){
109894
+ assert( db->lookaside.bDisable >= pParse->disableLookaside );
109895
+ db->lookaside.bDisable -= pParse->disableLookaside;
109896
+ }
109897
+ pParse->disableLookaside = 0;
109507109898
}
109508109899
}
109509109900
109510109901
/*
109511109902
** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
@@ -109530,11 +109921,11 @@
109530109921
rc = SQLITE_NOMEM;
109531109922
goto end_prepare;
109532109923
}
109533109924
pParse->pReprepare = pReprepare;
109534109925
assert( ppStmt && *ppStmt==0 );
109535
- assert( !db->mallocFailed );
109926
+ /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
109536109927
assert( sqlite3_mutex_held(db->mutex) );
109537109928
109538109929
/* Check to verify that it is possible to get a read lock on all
109539109930
** database schemas. The inability to get a read lock indicates that
109540109931
** some other database connection is holding a write-lock, which in
@@ -109587,23 +109978,20 @@
109587109978
goto end_prepare;
109588109979
}
109589109980
zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
109590109981
if( zSqlCopy ){
109591109982
sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
109592
- sqlite3DbFree(db, zSqlCopy);
109593109983
pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
109984
+ sqlite3DbFree(db, zSqlCopy);
109594109985
}else{
109595109986
pParse->zTail = &zSql[nBytes];
109596109987
}
109597109988
}else{
109598109989
sqlite3RunParser(pParse, zSql, &zErrMsg);
109599109990
}
109600109991
assert( 0==pParse->nQueryLoop );
109601109992
109602
- if( db->mallocFailed ){
109603
- pParse->rc = SQLITE_NOMEM;
109604
- }
109605109993
if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
109606109994
if( pParse->checkSchema ){
109607109995
schemaIsValid(pParse);
109608109996
}
109609109997
if( db->mallocFailed ){
@@ -109721,11 +110109,11 @@
109721110109
db = sqlite3VdbeDb(p);
109722110110
assert( sqlite3_mutex_held(db->mutex) );
109723110111
rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
109724110112
if( rc ){
109725110113
if( rc==SQLITE_NOMEM ){
109726
- db->mallocFailed = 1;
110114
+ sqlite3OomFault(db);
109727110115
}
109728110116
assert( pNew==0 );
109729110117
return rc;
109730110118
}else{
109731110119
assert( pNew!=0 );
@@ -109975,11 +110363,11 @@
109975110363
Expr *pOffset /* OFFSET value. NULL means no offset */
109976110364
){
109977110365
Select *pNew;
109978110366
Select standin;
109979110367
sqlite3 *db = pParse->db;
109980
- pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
110368
+ pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
109981110369
if( pNew==0 ){
109982110370
assert( db->mallocFailed );
109983110371
pNew = &standin;
109984110372
}
109985110373
if( pEList==0 ){
@@ -110879,11 +111267,11 @@
110879111267
p->enc = ENC(db);
110880111268
p->db = db;
110881111269
p->nRef = 1;
110882111270
memset(&p[1], 0, nExtra);
110883111271
}else{
110884
- db->mallocFailed = 1;
111272
+ sqlite3OomFault(db);
110885111273
}
110886111274
return p;
110887111275
}
110888111276
110889111277
/*
@@ -111540,11 +111928,11 @@
111540111928
if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
111541111929
}
111542111930
pCol->zName = zName;
111543111931
sqlite3ColumnPropertiesFromName(0, pCol);
111544111932
if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
111545
- db->mallocFailed = 1;
111933
+ sqlite3OomFault(db);
111546111934
}
111547111935
}
111548111936
sqlite3HashClear(&ht);
111549111937
if( db->mallocFailed ){
111550111938
for(j=0; j<i; j++){
@@ -111627,11 +112015,11 @@
111627112015
if( pTab==0 ){
111628112016
return 0;
111629112017
}
111630112018
/* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
111631112019
** is disabled */
111632
- assert( db->lookaside.bEnabled==0 );
112020
+ assert( db->lookaside.bDisable );
111633112021
pTab->nRef = 1;
111634112022
pTab->zName = 0;
111635112023
pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111636112024
sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
111637112025
selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
@@ -111723,14 +112111,12 @@
111723112111
p->iOffset = iOffset = ++pParse->nMem;
111724112112
pParse->nMem++; /* Allocate an extra register for limit+offset */
111725112113
sqlite3ExprCode(pParse, p->pOffset, iOffset);
111726112114
sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
111727112115
VdbeComment((v, "OFFSET counter"));
111728
- sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iOffset, iOffset, 0);
111729
- sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
112116
+ sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
111730112117
VdbeComment((v, "LIMIT+OFFSET"));
111731
- sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iLimit, iOffset+1, -1);
111732112118
}
111733112119
}
111734112120
}
111735112121
111736112122
#ifndef SQLITE_OMIT_COMPOUND_SELECT
@@ -112143,13 +112529,12 @@
112143112529
p->iOffset = pPrior->iOffset;
112144112530
if( p->iLimit ){
112145112531
addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
112146112532
VdbeComment((v, "Jump ahead if LIMIT reached"));
112147112533
if( p->iOffset ){
112148
- sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iOffset, p->iOffset, 0);
112149
- sqlite3VdbeAddOp3(v, OP_Add, p->iLimit, p->iOffset, p->iOffset+1);
112150
- sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iLimit, p->iOffset+1, -1);
112534
+ sqlite3VdbeAddOp3(v, OP_OffsetLimit,
112535
+ p->iLimit, p->iOffset+1, p->iOffset);
112151112536
}
112152112537
}
112153112538
explainSetInteger(iSub2, pParse->iNextSelectId);
112154112539
rc = sqlite3Select(pParse, p, &dest);
112155112540
testcase( rc!=SQLITE_OK );
@@ -112736,14 +113121,15 @@
112736113121
** row of results comes from selectA or selectB. Also add explicit
112737113122
** collations to the ORDER BY clause terms so that when the subqueries
112738113123
** to the right and the left are evaluated, they use the correct
112739113124
** collation.
112740113125
*/
112741
- aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
113126
+ aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
112742113127
if( aPermute ){
112743113128
struct ExprList_item *pItem;
112744
- for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
113129
+ aPermute[0] = nOrderBy;
113130
+ for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
112745113131
assert( pItem->u.x.iOrderByCol>0 );
112746113132
assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
112747113133
aPermute[i] = pItem->u.x.iOrderByCol - 1;
112748113134
}
112749113135
pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
@@ -112817,11 +113203,11 @@
112817113203
addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
112818113204
VdbeComment((v, "left SELECT"));
112819113205
pPrior->iLimit = regLimitA;
112820113206
explainSetInteger(iSub1, pParse->iNextSelectId);
112821113207
sqlite3Select(pParse, pPrior, &destA);
112822
- sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
113208
+ sqlite3VdbeEndCoroutine(v, regAddrA);
112823113209
sqlite3VdbeJumpHere(v, addr1);
112824113210
112825113211
/* Generate a coroutine to evaluate the SELECT statement on
112826113212
** the right - the "B" select
112827113213
*/
@@ -112834,11 +113220,11 @@
112834113220
p->iOffset = 0;
112835113221
explainSetInteger(iSub2, pParse->iNextSelectId);
112836113222
sqlite3Select(pParse, p, &destB);
112837113223
p->iLimit = savedLimit;
112838113224
p->iOffset = savedOffset;
112839
- sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
113225
+ sqlite3VdbeEndCoroutine(v, regAddrB);
112840113226
112841113227
/* Generate a subroutine that outputs the current row of the A
112842113228
** select as the next output row of the compound select.
112843113229
*/
112844113230
VdbeNoopComment((v, "Output routine for A"));
@@ -114301,12 +114687,11 @@
114301114687
}
114302114688
}else{
114303114689
pExpr = pRight;
114304114690
}
114305114691
pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
114306
- sColname.z = zColname;
114307
- sColname.n = sqlite3Strlen30(zColname);
114692
+ sqlite3TokenInit(&sColname, zColname);
114308114693
sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
114309114694
if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
114310114695
struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
114311114696
if( pSub ){
114312114697
pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
@@ -114856,11 +115241,11 @@
114856115241
explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
114857115242
sqlite3Select(pParse, pSub, &dest);
114858115243
pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
114859115244
pItem->fg.viaCoroutine = 1;
114860115245
pItem->regResult = dest.iSdst;
114861
- sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
115246
+ sqlite3VdbeEndCoroutine(v, pItem->regReturn);
114862115247
sqlite3VdbeJumpHere(v, addrTop-1);
114863115248
sqlite3ClearTempRegCache(pParse);
114864115249
}else{
114865115250
/* Generate a subroutine that will fill an ephemeral table with
114866115251
** the content of this subquery. pItem->addrFillSub will point
@@ -115428,11 +115813,12 @@
115428115813
assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
115429115814
115430115815
if( flag ){
115431115816
pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
115432115817
pDel = pMinMax;
115433
- if( pMinMax && !db->mallocFailed ){
115818
+ assert( db->mallocFailed || pMinMax!=0 );
115819
+ if( !db->mallocFailed ){
115434115820
pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
115435115821
pMinMax->a[0].pExpr->op = TK_COLUMN;
115436115822
}
115437115823
}
115438115824
@@ -116001,12 +116387,11 @@
116001116387
pTrig->step_list = pStepList;
116002116388
while( pStepList ){
116003116389
pStepList->pTrig = pTrig;
116004116390
pStepList = pStepList->pNext;
116005116391
}
116006
- nameToken.z = pTrig->zName;
116007
- nameToken.n = sqlite3Strlen30(nameToken.z);
116392
+ sqlite3TokenInit(&nameToken, pTrig->zName);
116008116393
sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
116009116394
if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
116010116395
|| sqlite3FixExpr(&sFix, pTrig->pWhen)
116011116396
){
116012116397
goto triggerfinish_cleanup;
@@ -116038,11 +116423,11 @@
116038116423
Trigger *pLink = pTrig;
116039116424
Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
116040116425
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
116041116426
pTrig = sqlite3HashInsert(pHash, zName, pTrig);
116042116427
if( pTrig ){
116043
- db->mallocFailed = 1;
116428
+ sqlite3OomFault(db);
116044116429
}else if( pLink->pSchema==pLink->pTabSchema ){
116045116430
Table *pTab;
116046116431
pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
116047116432
assert( pTab!=0 );
116048116433
pLink->pNext = pTab->pTrigger;
@@ -117014,11 +117399,11 @@
117014117399
}
117015117400
117016117401
/* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
117017117402
** Initialize aXRef[] and aToOpen[] to their default values.
117018117403
*/
117019
- aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
117404
+ aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
117020117405
if( aXRef==0 ) goto update_cleanup;
117021117406
aRegIdx = aXRef+pTab->nCol;
117022117407
aToOpen = (u8*)(aRegIdx+nIdx);
117023117408
memset(aToOpen, 1, nIdx+1);
117024117409
aToOpen[nIdx+1] = 0;
@@ -117389,11 +117774,12 @@
117389117774
int bReplace = 0; /* True if REPLACE conflict resolution might happen */
117390117775
117391117776
/* Do constraint checks. */
117392117777
assert( regOldRowid>0 );
117393117778
sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
117394
- regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
117779
+ regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
117780
+ aXRef);
117395117781
117396117782
/* Do FK constraint checks. */
117397117783
if( hasFK ){
117398117784
sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
117399117785
}
@@ -118054,11 +118440,11 @@
118054118440
nName = sqlite3Strlen30(zName);
118055118441
if( sqlite3HashFind(&db->aModule, zName) ){
118056118442
rc = SQLITE_MISUSE_BKPT;
118057118443
}else{
118058118444
Module *pMod;
118059
- pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
118445
+ pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1);
118060118446
if( pMod ){
118061118447
Module *pDel;
118062118448
char *zCopy = (char *)(&pMod[1]);
118063118449
memcpy(zCopy, zName, nName+1);
118064118450
pMod->zName = zCopy;
@@ -118067,11 +118453,11 @@
118067118453
pMod->xDestroy = xDestroy;
118068118454
pMod->pEpoTab = 0;
118069118455
pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
118070118456
assert( pDel==0 || pDel==pMod );
118071118457
if( pDel ){
118072
- db->mallocFailed = 1;
118458
+ sqlite3OomFault(db);
118073118459
sqlite3DbFree(db, pDel);
118074118460
}
118075118461
}
118076118462
}
118077118463
rc = sqlite3ApiExit(db, rc);
@@ -118444,11 +118830,11 @@
118444118830
Schema *pSchema = pTab->pSchema;
118445118831
const char *zName = pTab->zName;
118446118832
assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
118447118833
pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
118448118834
if( pOld ){
118449
- db->mallocFailed = 1;
118835
+ sqlite3OomFault(db);
118450118836
assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
118451118837
return;
118452118838
}
118453118839
pParse->pNewTable = 0;
118454118840
}
@@ -118535,11 +118921,11 @@
118535118921
sCtx.pPrior = db->pVtabCtx;
118536118922
sCtx.bDeclared = 0;
118537118923
db->pVtabCtx = &sCtx;
118538118924
rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
118539118925
db->pVtabCtx = sCtx.pPrior;
118540
- if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
118926
+ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
118541118927
assert( sCtx.pTab==pTab );
118542118928
118543118929
if( SQLITE_OK!=rc ){
118544118930
if( zErr==0 ){
118545118931
*pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
@@ -119093,11 +119479,11 @@
119093119479
apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
119094119480
if( apVtabLock ){
119095119481
pToplevel->apVtabLock = apVtabLock;
119096119482
pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
119097119483
}else{
119098
- pToplevel->db->mallocFailed = 1;
119484
+ sqlite3OomFault(pToplevel->db);
119099119485
}
119100119486
}
119101119487
119102119488
/*
119103119489
** Check to see if virtual tale module pMod can be have an eponymous
@@ -119835,11 +120221,11 @@
119835120221
if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
119836120222
sqlite3StrAccumAppend(pStr, " (", 2);
119837120223
for(i=0; i<nEq; i++){
119838120224
const char *z = explainIndexColumnName(pIndex, i);
119839120225
if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119840
- sqlite3XPrintf(pStr, 0, i>=nSkip ? "%s=?" : "ANY(%s)", z);
120226
+ sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
119841120227
}
119842120228
119843120229
j = i;
119844120230
if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
119845120231
const char *z = explainIndexColumnName(pIndex, i);
@@ -119894,17 +120280,17 @@
119894120280
|| (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
119895120281
119896120282
sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
119897120283
sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
119898120284
if( pItem->pSelect ){
119899
- sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
120285
+ sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
119900120286
}else{
119901
- sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
120287
+ sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
119902120288
}
119903120289
119904120290
if( pItem->zAlias ){
119905
- sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
120291
+ sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
119906120292
}
119907120293
if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
119908120294
const char *zFmt = 0;
119909120295
Index *pIdx;
119910120296
@@ -119924,11 +120310,11 @@
119924120310
}else{
119925120311
zFmt = "INDEX %s";
119926120312
}
119927120313
if( zFmt ){
119928120314
sqlite3StrAccumAppend(&str, " USING ", 7);
119929
- sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
120315
+ sqlite3XPrintf(&str, zFmt, pIdx->zName);
119930120316
explainIndexRange(&str, pLoop);
119931120317
}
119932120318
}else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
119933120319
const char *zRangeOp;
119934120320
if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -119939,21 +120325,21 @@
119939120325
zRangeOp = ">";
119940120326
}else{
119941120327
assert( flags&WHERE_TOP_LIMIT);
119942120328
zRangeOp = "<";
119943120329
}
119944
- sqlite3XPrintf(&str, 0, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
120330
+ sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
119945120331
}
119946120332
#ifndef SQLITE_OMIT_VIRTUALTABLE
119947120333
else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
119948
- sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
120334
+ sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s",
119949120335
pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
119950120336
}
119951120337
#endif
119952120338
#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
119953120339
if( pLoop->nOut>=10 ){
119954
- sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
120340
+ sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
119955120341
}else{
119956120342
sqlite3StrAccumAppend(&str, " (~1 row)", 9);
119957120343
}
119958120344
#endif
119959120345
zMsg = sqlite3StrAccumFinish(&str);
@@ -120254,13 +120640,11 @@
120254120640
regBase = pParse->nMem + 1;
120255120641
nReg = pLoop->u.btree.nEq + nExtraReg;
120256120642
pParse->nMem += nReg;
120257120643
120258120644
zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
120259
- if( !zAff ){
120260
- pParse->db->mallocFailed = 1;
120261
- }
120645
+ assert( zAff!=0 || pParse->db->mallocFailed );
120262120646
120263120647
if( nSkip ){
120264120648
int iIdxCur = pLevel->iIdxCur;
120265120649
sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
120266120650
VdbeCoverageIf(v, bRev==0);
@@ -120504,10 +120888,58 @@
120504120888
}
120505120889
}
120506120890
#else
120507120891
# define codeCursorHint(A,B,C) /* No-op */
120508120892
#endif /* SQLITE_ENABLE_CURSOR_HINTS */
120893
+
120894
+/*
120895
+** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
120896
+** a rowid value just read from cursor iIdxCur, open on index pIdx. This
120897
+** function generates code to do a deferred seek of cursor iCur to the
120898
+** rowid stored in register iRowid.
120899
+**
120900
+** Normally, this is just:
120901
+**
120902
+** OP_Seek $iCur $iRowid
120903
+**
120904
+** However, if the scan currently being coded is a branch of an OR-loop and
120905
+** the statement currently being coded is a SELECT, then P3 of the OP_Seek
120906
+** is set to iIdxCur and P4 is set to point to an array of integers
120907
+** containing one entry for each column of the table cursor iCur is open
120908
+** on. For each table column, if the column is the i'th column of the
120909
+** index, then the corresponding array entry is set to (i+1). If the column
120910
+** does not appear in the index at all, the array entry is set to 0.
120911
+*/
120912
+static void codeDeferredSeek(
120913
+ WhereInfo *pWInfo, /* Where clause context */
120914
+ Index *pIdx, /* Index scan is using */
120915
+ int iCur, /* Cursor for IPK b-tree */
120916
+ int iIdxCur /* Index cursor */
120917
+){
120918
+ Parse *pParse = pWInfo->pParse; /* Parse context */
120919
+ Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
120920
+
120921
+ assert( iIdxCur>0 );
120922
+ assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
120923
+
120924
+ sqlite3VdbeAddOp3(v, OP_Seek, iIdxCur, 0, iCur);
120925
+ if( (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)
120926
+ && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
120927
+ ){
120928
+ int i;
120929
+ Table *pTab = pIdx->pTable;
120930
+ int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
120931
+ if( ai ){
120932
+ ai[0] = pTab->nCol;
120933
+ for(i=0; i<pIdx->nColumn-1; i++){
120934
+ assert( pIdx->aiColumn[i]<pTab->nCol );
120935
+ if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
120936
+ }
120937
+ sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
120938
+ }
120939
+ }
120940
+}
120509120941
120510120942
/*
120511120943
** Generate code for the start of the iLevel-th loop in the WHERE clause
120512120944
** implementation described by pWInfo.
120513120945
*/
@@ -120984,18 +121416,18 @@
120984121416
disableTerm(pLevel, pRangeStart);
120985121417
disableTerm(pLevel, pRangeEnd);
120986121418
if( omitTable ){
120987121419
/* pIdx is a covering index. No need to access the main table. */
120988121420
}else if( HasRowid(pIdx->pTable) ){
120989
- iRowidReg = ++pParse->nMem;
120990
- sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
120991
- sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120992121421
if( pWInfo->eOnePass!=ONEPASS_OFF ){
121422
+ iRowidReg = ++pParse->nMem;
121423
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
121424
+ sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120993121425
sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
120994121426
VdbeCoverage(v);
120995121427
}else{
120996
- sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
121428
+ codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
120997121429
}
120998121430
}else if( iCur!=iIdxCur ){
120999121431
Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
121000121432
iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
121001121433
for(j=0; j<pPk->nKeyCol; j++){
@@ -121160,11 +121592,13 @@
121160121592
int iTerm;
121161121593
for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
121162121594
Expr *pExpr = pWC->a[iTerm].pExpr;
121163121595
if( &pWC->a[iTerm] == pTerm ) continue;
121164121596
if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
121165
- if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
121597
+ testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
121598
+ testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
121599
+ if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
121166121600
if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
121167121601
testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
121168121602
pExpr = sqlite3ExprDup(db, pExpr, 0);
121169121603
pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
121170121604
}
@@ -121500,11 +121934,11 @@
121500121934
int idx;
121501121935
testcase( wtFlags & TERM_VIRTUAL );
121502121936
if( pWC->nTerm>=pWC->nSlot ){
121503121937
WhereTerm *pOld = pWC->a;
121504121938
sqlite3 *db = pWC->pWInfo->pParse->db;
121505
- pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
121939
+ pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
121506121940
if( pWC->a==0 ){
121507121941
if( wtFlags & TERM_DYNAMIC ){
121508121942
sqlite3ExprDelete(db, p);
121509121943
}
121510121944
pWC->a = pOld;
@@ -121985,11 +122419,11 @@
121985122419
for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
121986122420
if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
121987122421
WhereAndInfo *pAndInfo;
121988122422
assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
121989122423
chngToIN = 0;
121990
- pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
122424
+ pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
121991122425
if( pAndInfo ){
121992122426
WhereClause *pAndWC;
121993122427
WhereTerm *pAndTerm;
121994122428
int j;
121995122429
Bitmask b = 0;
@@ -121999,11 +122433,10 @@
121999122433
pAndWC = &pAndInfo->wc;
122000122434
sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
122001122435
sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
122002122436
sqlite3WhereExprAnalyze(pSrc, pAndWC);
122003122437
pAndWC->pOuter = pWC;
122004
- testcase( db->mallocFailed );
122005122438
if( !db->mallocFailed ){
122006122439
for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
122007122440
assert( pAndTerm->pExpr );
122008122441
if( allowedOp(pAndTerm->pExpr->op) ){
122009122442
b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
@@ -123742,11 +124175,11 @@
123742124175
rc = pVtab->pModule->xBestIndex(pVtab, p);
123743124176
TRACE_IDX_OUTPUTS(p);
123744124177
123745124178
if( rc!=SQLITE_OK ){
123746124179
if( rc==SQLITE_NOMEM ){
123747
- pParse->db->mallocFailed = 1;
124180
+ sqlite3OomFault(pParse->db);
123748124181
}else if( !pVtab->zErrMsg ){
123749124182
sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
123750124183
}else{
123751124184
sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
123752124185
}
@@ -124534,11 +124967,11 @@
124534124967
*/
124535124968
static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
124536124969
WhereTerm **paNew;
124537124970
if( p->nLSlot>=n ) return SQLITE_OK;
124538124971
n = (n+7)&~7;
124539
- paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
124972
+ paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
124540124973
if( paNew==0 ) return SQLITE_NOMEM;
124541124974
memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
124542124975
if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
124543124976
p->aLTerm = paNew;
124544124977
p->nLSlot = n;
@@ -124831,11 +125264,11 @@
124831125264
whereLoopPrint(pTemplate, pBuilder->pWC);
124832125265
}
124833125266
#endif
124834125267
if( p==0 ){
124835125268
/* Allocate a new WhereLoop to add to the end of the list */
124836
- *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
125269
+ *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
124837125270
if( p==0 ) return SQLITE_NOMEM;
124838125271
whereLoopInit(p);
124839125272
p->pNextLoop = 0;
124840125273
}else{
124841125274
/* We will be overwriting WhereLoop p[]. But before we do, first
@@ -126237,11 +126670,10 @@
126237126670
** Return the cost of sorting nRow rows, assuming that the keys have
126238126671
** nOrderby columns and that the first nSorted columns are already in
126239126672
** order.
126240126673
*/
126241126674
static LogEst whereSortingCost(
126242
- WhereInfo *pWInfo,
126243126675
LogEst nRow,
126244126676
int nOrderBy,
126245126677
int nSorted
126246126678
){
126247126679
/* TUNING: Estimated cost of a full external sort, where N is
@@ -126259,18 +126691,10 @@
126259126691
** below. */
126260126692
LogEst rScale, rSortCost;
126261126693
assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
126262126694
rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
126263126695
rSortCost = nRow + estLog(nRow) + rScale + 16;
126264
-
126265
- /* TUNING: The cost of implementing DISTINCT using a B-TREE is
126266
- ** similar but with a larger constant of proportionality.
126267
- ** Multiply by an additional factor of 3.0. */
126268
- if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
126269
- rSortCost += 16;
126270
- }
126271
-
126272126696
return rSortCost;
126273126697
}
126274126698
126275126699
/*
126276126700
** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
@@ -126328,11 +126752,11 @@
126328126752
}
126329126753
126330126754
/* Allocate and initialize space for aTo, aFrom and aSortCost[] */
126331126755
nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
126332126756
nSpace += sizeof(LogEst) * nOrderBy;
126333
- pSpace = sqlite3DbMallocRaw(db, nSpace);
126757
+ pSpace = sqlite3DbMallocRawNN(db, nSpace);
126334126758
if( pSpace==0 ) return SQLITE_NOMEM;
126335126759
aTo = (WherePath*)pSpace;
126336126760
aFrom = aTo+mxChoice;
126337126761
memset(aFrom, 0, sizeof(aFrom[0]));
126338126762
pX = (WhereLoop**)(aFrom+mxChoice);
@@ -126400,11 +126824,11 @@
126400126824
revMask = pFrom->revLoop;
126401126825
}
126402126826
if( isOrdered>=0 && isOrdered<nOrderBy ){
126403126827
if( aSortCost[isOrdered]==0 ){
126404126828
aSortCost[isOrdered] = whereSortingCost(
126405
- pWInfo, nRowEst, nOrderBy, isOrdered
126829
+ nRowEst, nOrderBy, isOrdered
126406126830
);
126407126831
}
126408126832
rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
126409126833
126410126834
WHERETRACE(0x002,
@@ -126813,11 +127237,11 @@
126813127237
WhereLevel *pLevel; /* A single level in pWInfo->a[] */
126814127238
WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
126815127239
int ii; /* Loop counter */
126816127240
sqlite3 *db; /* Database connection */
126817127241
int rc; /* Return code */
126818
- u8 bFordelete = 0;
127242
+ u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
126819127243
126820127244
assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
126821127245
(wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
126822127246
&& (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
126823127247
));
@@ -127058,20 +127482,19 @@
127058127482
WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
127059127483
pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
127060127484
127061127485
/* If the caller is an UPDATE or DELETE statement that is requesting
127062127486
** to use a one-pass algorithm, determine if this is appropriate.
127063
- ** The one-pass algorithm only works if the WHERE clause constrains
127064
- ** the statement to update or delete a single row.
127065127487
*/
127066127488
assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
127067127489
if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
127068127490
int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
127069127491
int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
127070
- if( bOnerow || ( (wctrlFlags & WHERE_ONEPASS_MULTIROW)
127071
- && 0==(wsFlags & WHERE_VIRTUALTABLE)
127072
- )){
127492
+ if( bOnerow
127493
+ || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0
127494
+ && 0==(wsFlags & WHERE_VIRTUALTABLE))
127495
+ ){
127073127496
pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
127074127497
if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
127075127498
if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
127076127499
bFordelete = OPFLAG_FORDELETE;
127077127500
}
@@ -127511,10 +127934,19 @@
127511127934
/*
127512127935
** An instance of this structure holds the ATTACH key and the key type.
127513127936
*/
127514127937
struct AttachKey { int type; Token key; };
127515127938
127939
+/*
127940
+** Disable lookaside memory allocation for objects that might be
127941
+** shared across database connections.
127942
+*/
127943
+static void disableLookaside(Parse *pParse){
127944
+ pParse->disableLookaside++;
127945
+ pParse->db->lookaside.bDisable++;
127946
+}
127947
+
127516127948
127517127949
/*
127518127950
** For a compound SELECT statement, make sure p->pPrior->pNext==p for
127519127951
** all elements in the list. And make sure list length does not exceed
127520127952
** SQLITE_LIMIT_COMPOUND_SELECT.
@@ -127593,11 +128025,11 @@
127593128025
127594128026
/* A routine to convert a binary TK_IS or TK_ISNOT expression into a
127595128027
** unary TK_ISNULL or TK_NOTNULL expression. */
127596128028
static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
127597128029
sqlite3 *db = pParse->db;
127598
- if( pY && pA && pY->op==TK_NULL ){
128030
+ if( pA && pY && pY->op==TK_NULL ){
127599128031
pA->op = (u8)op;
127600128032
sqlite3ExprDelete(db, pA->pRight);
127601128033
pA->pRight = 0;
127602128034
}
127603128035
}
@@ -129635,11 +130067,11 @@
129635130067
sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
129636130068
}
129637130069
break;
129638130070
case 27: /* createkw ::= CREATE */
129639130071
{
129640
- pParse->db->lookaside.bEnabled = 0;
130072
+ disableLookaside(pParse);
129641130073
yygotominor.yy0 = yymsp[0].minor.yy0;
129642130074
}
129643130075
break;
129644130076
case 28: /* ifnotexists ::= */
129645130077
case 31: /* temp ::= */ yytestcase(yyruleno==31);
@@ -130717,11 +131149,11 @@
130717131149
sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
130718131150
}
130719131151
break;
130720131152
case 307: /* add_column_fullname ::= fullname */
130721131153
{
130722
- pParse->db->lookaside.bEnabled = 0;
131154
+ disableLookaside(pParse);
130723131155
sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
130724131156
}
130725131157
break;
130726131158
case 310: /* cmd ::= create_vtab */
130727131159
{sqlite3VtabFinishParse(pParse,0);}
@@ -131097,16 +131529,96 @@
131097131529
** parser for analysis.
131098131530
*/
131099131531
/* #include "sqliteInt.h" */
131100131532
/* #include <stdlib.h> */
131101131533
131534
+/* Character classes for tokenizing
131535
+**
131536
+** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
131537
+** using a lookup table, whereas a switch() directly on c uses a binary search.
131538
+** The lookup table is much faster. To maximize speed, and to ensure that
131539
+** a lookup table is used, all of the classes need to be small integers and
131540
+** all of them need to be used within the switch.
131541
+*/
131542
+#define CC_X 0 /* The letter 'x', or start of BLOB literal */
131543
+#define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
131544
+#define CC_ID 2 /* unicode characters usable in IDs */
131545
+#define CC_DIGIT 3 /* Digits */
131546
+#define CC_DOLLAR 4 /* '$' */
131547
+#define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
131548
+#define CC_VARNUM 6 /* '?'. Numeric SQL variables */
131549
+#define CC_SPACE 7 /* Space characters */
131550
+#define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
131551
+#define CC_QUOTE2 9 /* '['. [...] style quoted ids */
131552
+#define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
131553
+#define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
131554
+#define CC_LT 12 /* '<'. Part of < or <= or <> */
131555
+#define CC_GT 13 /* '>'. Part of > or >= */
131556
+#define CC_EQ 14 /* '='. Part of = or == */
131557
+#define CC_BANG 15 /* '!'. Part of != */
131558
+#define CC_SLASH 16 /* '/'. / or c-style comment */
131559
+#define CC_LP 17 /* '(' */
131560
+#define CC_RP 18 /* ')' */
131561
+#define CC_SEMI 19 /* ';' */
131562
+#define CC_PLUS 20 /* '+' */
131563
+#define CC_STAR 21 /* '*' */
131564
+#define CC_PERCENT 22 /* '%' */
131565
+#define CC_COMMA 23 /* ',' */
131566
+#define CC_AND 24 /* '&' */
131567
+#define CC_TILDA 25 /* '~' */
131568
+#define CC_DOT 26 /* '.' */
131569
+#define CC_ILLEGAL 27 /* Illegal character */
131570
+
131571
+static const unsigned char aiClass[] = {
131572
+#ifdef SQLITE_ASCII
131573
+/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
131574
+/* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
131575
+/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131576
+/* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
131577
+/* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
131578
+/* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131579
+/* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
131580
+/* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131581
+/* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
131582
+/* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131583
+/* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131584
+/* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131585
+/* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131586
+/* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131587
+/* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131588
+/* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131589
+/* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
131590
+#endif
131591
+#ifdef SQLITE_EBCDIC
131592
+/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
131593
+/* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
131594
+/* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131595
+/* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131596
+/* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131597
+/* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10,
131598
+/* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
131599
+/* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7,
131600
+/* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
131601
+/* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131602
+/* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131603
+/* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
131604
+/* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
131605
+/* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131606
+/* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131607
+/* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
131608
+/* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
131609
+#endif
131610
+};
131611
+
131102131612
/*
131103
-** The charMap() macro maps alphabetic characters into their
131613
+** The charMap() macro maps alphabetic characters (only) into their
131104131614
** lower-case ASCII equivalent. On ASCII machines, this is just
131105131615
** an upper-to-lower case map. On EBCDIC machines we also need
131106
-** to adjust the encoding. Only alphabetic characters and underscores
131107
-** need to be translated.
131616
+** to adjust the encoding. The mapping is only valid for alphabetics
131617
+** which are the only characters for which this feature is used.
131618
+**
131619
+** Used by keywordhash.h
131108131620
*/
131109131621
#ifdef SQLITE_ASCII
131110131622
# define charMap(X) sqlite3UpperToLower[(unsigned char)X]
131111131623
#endif
131112131624
#ifdef SQLITE_EBCDIC
@@ -131136,11 +131648,11 @@
131136131648
** The sqlite3KeywordCode function looks up an identifier to determine if
131137131649
** it is a keyword. If it is a keyword, the token code of that keyword is
131138131650
** returned. If the input is not a keyword, TK_ID is returned.
131139131651
**
131140131652
** The implementation of this routine was generated by a program,
131141
-** mkkeywordhash.h, located in the tool subdirectory of the distribution.
131653
+** mkkeywordhash.c, located in the tool subdirectory of the distribution.
131142131654
** The output of the mkkeywordhash.c program is written into a file
131143131655
** named keywordhash.h and then included into this source file by
131144131656
** the #include below.
131145131657
*/
131146131658
/************** Include keywordhash.h in the middle of tokenize.c ************/
@@ -131277,142 +131789,151 @@
131277131789
TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW,
131278131790
TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT,
131279131791
TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING,
131280131792
TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL,
131281131793
};
131282
- int h, i;
131794
+ int i, j;
131795
+ const char *zKW;
131283131796
if( n>=2 ){
131284
- h = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
131285
- for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
131286
- if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
131287
- testcase( i==0 ); /* REINDEX */
131288
- testcase( i==1 ); /* INDEXED */
131289
- testcase( i==2 ); /* INDEX */
131290
- testcase( i==3 ); /* DESC */
131291
- testcase( i==4 ); /* ESCAPE */
131292
- testcase( i==5 ); /* EACH */
131293
- testcase( i==6 ); /* CHECK */
131294
- testcase( i==7 ); /* KEY */
131295
- testcase( i==8 ); /* BEFORE */
131296
- testcase( i==9 ); /* FOREIGN */
131297
- testcase( i==10 ); /* FOR */
131298
- testcase( i==11 ); /* IGNORE */
131299
- testcase( i==12 ); /* REGEXP */
131300
- testcase( i==13 ); /* EXPLAIN */
131301
- testcase( i==14 ); /* INSTEAD */
131302
- testcase( i==15 ); /* ADD */
131303
- testcase( i==16 ); /* DATABASE */
131304
- testcase( i==17 ); /* AS */
131305
- testcase( i==18 ); /* SELECT */
131306
- testcase( i==19 ); /* TABLE */
131307
- testcase( i==20 ); /* LEFT */
131308
- testcase( i==21 ); /* THEN */
131309
- testcase( i==22 ); /* END */
131310
- testcase( i==23 ); /* DEFERRABLE */
131311
- testcase( i==24 ); /* ELSE */
131312
- testcase( i==25 ); /* EXCEPT */
131313
- testcase( i==26 ); /* TRANSACTION */
131314
- testcase( i==27 ); /* ACTION */
131315
- testcase( i==28 ); /* ON */
131316
- testcase( i==29 ); /* NATURAL */
131317
- testcase( i==30 ); /* ALTER */
131318
- testcase( i==31 ); /* RAISE */
131319
- testcase( i==32 ); /* EXCLUSIVE */
131320
- testcase( i==33 ); /* EXISTS */
131321
- testcase( i==34 ); /* SAVEPOINT */
131322
- testcase( i==35 ); /* INTERSECT */
131323
- testcase( i==36 ); /* TRIGGER */
131324
- testcase( i==37 ); /* REFERENCES */
131325
- testcase( i==38 ); /* CONSTRAINT */
131326
- testcase( i==39 ); /* INTO */
131327
- testcase( i==40 ); /* OFFSET */
131328
- testcase( i==41 ); /* OF */
131329
- testcase( i==42 ); /* SET */
131330
- testcase( i==43 ); /* TEMPORARY */
131331
- testcase( i==44 ); /* TEMP */
131332
- testcase( i==45 ); /* OR */
131333
- testcase( i==46 ); /* UNIQUE */
131334
- testcase( i==47 ); /* QUERY */
131335
- testcase( i==48 ); /* WITHOUT */
131336
- testcase( i==49 ); /* WITH */
131337
- testcase( i==50 ); /* OUTER */
131338
- testcase( i==51 ); /* RELEASE */
131339
- testcase( i==52 ); /* ATTACH */
131340
- testcase( i==53 ); /* HAVING */
131341
- testcase( i==54 ); /* GROUP */
131342
- testcase( i==55 ); /* UPDATE */
131343
- testcase( i==56 ); /* BEGIN */
131344
- testcase( i==57 ); /* INNER */
131345
- testcase( i==58 ); /* RECURSIVE */
131346
- testcase( i==59 ); /* BETWEEN */
131347
- testcase( i==60 ); /* NOTNULL */
131348
- testcase( i==61 ); /* NOT */
131349
- testcase( i==62 ); /* NO */
131350
- testcase( i==63 ); /* NULL */
131351
- testcase( i==64 ); /* LIKE */
131352
- testcase( i==65 ); /* CASCADE */
131353
- testcase( i==66 ); /* ASC */
131354
- testcase( i==67 ); /* DELETE */
131355
- testcase( i==68 ); /* CASE */
131356
- testcase( i==69 ); /* COLLATE */
131357
- testcase( i==70 ); /* CREATE */
131358
- testcase( i==71 ); /* CURRENT_DATE */
131359
- testcase( i==72 ); /* DETACH */
131360
- testcase( i==73 ); /* IMMEDIATE */
131361
- testcase( i==74 ); /* JOIN */
131362
- testcase( i==75 ); /* INSERT */
131363
- testcase( i==76 ); /* MATCH */
131364
- testcase( i==77 ); /* PLAN */
131365
- testcase( i==78 ); /* ANALYZE */
131366
- testcase( i==79 ); /* PRAGMA */
131367
- testcase( i==80 ); /* ABORT */
131368
- testcase( i==81 ); /* VALUES */
131369
- testcase( i==82 ); /* VIRTUAL */
131370
- testcase( i==83 ); /* LIMIT */
131371
- testcase( i==84 ); /* WHEN */
131372
- testcase( i==85 ); /* WHERE */
131373
- testcase( i==86 ); /* RENAME */
131374
- testcase( i==87 ); /* AFTER */
131375
- testcase( i==88 ); /* REPLACE */
131376
- testcase( i==89 ); /* AND */
131377
- testcase( i==90 ); /* DEFAULT */
131378
- testcase( i==91 ); /* AUTOINCREMENT */
131379
- testcase( i==92 ); /* TO */
131380
- testcase( i==93 ); /* IN */
131381
- testcase( i==94 ); /* CAST */
131382
- testcase( i==95 ); /* COLUMN */
131383
- testcase( i==96 ); /* COMMIT */
131384
- testcase( i==97 ); /* CONFLICT */
131385
- testcase( i==98 ); /* CROSS */
131386
- testcase( i==99 ); /* CURRENT_TIMESTAMP */
131387
- testcase( i==100 ); /* CURRENT_TIME */
131388
- testcase( i==101 ); /* PRIMARY */
131389
- testcase( i==102 ); /* DEFERRED */
131390
- testcase( i==103 ); /* DISTINCT */
131391
- testcase( i==104 ); /* IS */
131392
- testcase( i==105 ); /* DROP */
131393
- testcase( i==106 ); /* FAIL */
131394
- testcase( i==107 ); /* FROM */
131395
- testcase( i==108 ); /* FULL */
131396
- testcase( i==109 ); /* GLOB */
131397
- testcase( i==110 ); /* BY */
131398
- testcase( i==111 ); /* IF */
131399
- testcase( i==112 ); /* ISNULL */
131400
- testcase( i==113 ); /* ORDER */
131401
- testcase( i==114 ); /* RESTRICT */
131402
- testcase( i==115 ); /* RIGHT */
131403
- testcase( i==116 ); /* ROLLBACK */
131404
- testcase( i==117 ); /* ROW */
131405
- testcase( i==118 ); /* UNION */
131406
- testcase( i==119 ); /* USING */
131407
- testcase( i==120 ); /* VACUUM */
131408
- testcase( i==121 ); /* VIEW */
131409
- testcase( i==122 ); /* INITIALLY */
131410
- testcase( i==123 ); /* ALL */
131411
- *pType = aCode[i];
131412
- break;
131413
- }
131797
+ i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
131798
+ for(i=((int)aHash[i])-1; i>=0; i=((int)aNext[i])-1){
131799
+ if( aLen[i]!=n ) continue;
131800
+ j = 0;
131801
+ zKW = &zText[aOffset[i]];
131802
+#ifdef SQLITE_ASCII
131803
+ while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
131804
+#endif
131805
+#ifdef SQLITE_EBCDIC
131806
+ while( j<n && toupper(z[j])==zKW[j] ){ j++; }
131807
+#endif
131808
+ if( j<n ) continue;
131809
+ testcase( i==0 ); /* REINDEX */
131810
+ testcase( i==1 ); /* INDEXED */
131811
+ testcase( i==2 ); /* INDEX */
131812
+ testcase( i==3 ); /* DESC */
131813
+ testcase( i==4 ); /* ESCAPE */
131814
+ testcase( i==5 ); /* EACH */
131815
+ testcase( i==6 ); /* CHECK */
131816
+ testcase( i==7 ); /* KEY */
131817
+ testcase( i==8 ); /* BEFORE */
131818
+ testcase( i==9 ); /* FOREIGN */
131819
+ testcase( i==10 ); /* FOR */
131820
+ testcase( i==11 ); /* IGNORE */
131821
+ testcase( i==12 ); /* REGEXP */
131822
+ testcase( i==13 ); /* EXPLAIN */
131823
+ testcase( i==14 ); /* INSTEAD */
131824
+ testcase( i==15 ); /* ADD */
131825
+ testcase( i==16 ); /* DATABASE */
131826
+ testcase( i==17 ); /* AS */
131827
+ testcase( i==18 ); /* SELECT */
131828
+ testcase( i==19 ); /* TABLE */
131829
+ testcase( i==20 ); /* LEFT */
131830
+ testcase( i==21 ); /* THEN */
131831
+ testcase( i==22 ); /* END */
131832
+ testcase( i==23 ); /* DEFERRABLE */
131833
+ testcase( i==24 ); /* ELSE */
131834
+ testcase( i==25 ); /* EXCEPT */
131835
+ testcase( i==26 ); /* TRANSACTION */
131836
+ testcase( i==27 ); /* ACTION */
131837
+ testcase( i==28 ); /* ON */
131838
+ testcase( i==29 ); /* NATURAL */
131839
+ testcase( i==30 ); /* ALTER */
131840
+ testcase( i==31 ); /* RAISE */
131841
+ testcase( i==32 ); /* EXCLUSIVE */
131842
+ testcase( i==33 ); /* EXISTS */
131843
+ testcase( i==34 ); /* SAVEPOINT */
131844
+ testcase( i==35 ); /* INTERSECT */
131845
+ testcase( i==36 ); /* TRIGGER */
131846
+ testcase( i==37 ); /* REFERENCES */
131847
+ testcase( i==38 ); /* CONSTRAINT */
131848
+ testcase( i==39 ); /* INTO */
131849
+ testcase( i==40 ); /* OFFSET */
131850
+ testcase( i==41 ); /* OF */
131851
+ testcase( i==42 ); /* SET */
131852
+ testcase( i==43 ); /* TEMPORARY */
131853
+ testcase( i==44 ); /* TEMP */
131854
+ testcase( i==45 ); /* OR */
131855
+ testcase( i==46 ); /* UNIQUE */
131856
+ testcase( i==47 ); /* QUERY */
131857
+ testcase( i==48 ); /* WITHOUT */
131858
+ testcase( i==49 ); /* WITH */
131859
+ testcase( i==50 ); /* OUTER */
131860
+ testcase( i==51 ); /* RELEASE */
131861
+ testcase( i==52 ); /* ATTACH */
131862
+ testcase( i==53 ); /* HAVING */
131863
+ testcase( i==54 ); /* GROUP */
131864
+ testcase( i==55 ); /* UPDATE */
131865
+ testcase( i==56 ); /* BEGIN */
131866
+ testcase( i==57 ); /* INNER */
131867
+ testcase( i==58 ); /* RECURSIVE */
131868
+ testcase( i==59 ); /* BETWEEN */
131869
+ testcase( i==60 ); /* NOTNULL */
131870
+ testcase( i==61 ); /* NOT */
131871
+ testcase( i==62 ); /* NO */
131872
+ testcase( i==63 ); /* NULL */
131873
+ testcase( i==64 ); /* LIKE */
131874
+ testcase( i==65 ); /* CASCADE */
131875
+ testcase( i==66 ); /* ASC */
131876
+ testcase( i==67 ); /* DELETE */
131877
+ testcase( i==68 ); /* CASE */
131878
+ testcase( i==69 ); /* COLLATE */
131879
+ testcase( i==70 ); /* CREATE */
131880
+ testcase( i==71 ); /* CURRENT_DATE */
131881
+ testcase( i==72 ); /* DETACH */
131882
+ testcase( i==73 ); /* IMMEDIATE */
131883
+ testcase( i==74 ); /* JOIN */
131884
+ testcase( i==75 ); /* INSERT */
131885
+ testcase( i==76 ); /* MATCH */
131886
+ testcase( i==77 ); /* PLAN */
131887
+ testcase( i==78 ); /* ANALYZE */
131888
+ testcase( i==79 ); /* PRAGMA */
131889
+ testcase( i==80 ); /* ABORT */
131890
+ testcase( i==81 ); /* VALUES */
131891
+ testcase( i==82 ); /* VIRTUAL */
131892
+ testcase( i==83 ); /* LIMIT */
131893
+ testcase( i==84 ); /* WHEN */
131894
+ testcase( i==85 ); /* WHERE */
131895
+ testcase( i==86 ); /* RENAME */
131896
+ testcase( i==87 ); /* AFTER */
131897
+ testcase( i==88 ); /* REPLACE */
131898
+ testcase( i==89 ); /* AND */
131899
+ testcase( i==90 ); /* DEFAULT */
131900
+ testcase( i==91 ); /* AUTOINCREMENT */
131901
+ testcase( i==92 ); /* TO */
131902
+ testcase( i==93 ); /* IN */
131903
+ testcase( i==94 ); /* CAST */
131904
+ testcase( i==95 ); /* COLUMN */
131905
+ testcase( i==96 ); /* COMMIT */
131906
+ testcase( i==97 ); /* CONFLICT */
131907
+ testcase( i==98 ); /* CROSS */
131908
+ testcase( i==99 ); /* CURRENT_TIMESTAMP */
131909
+ testcase( i==100 ); /* CURRENT_TIME */
131910
+ testcase( i==101 ); /* PRIMARY */
131911
+ testcase( i==102 ); /* DEFERRED */
131912
+ testcase( i==103 ); /* DISTINCT */
131913
+ testcase( i==104 ); /* IS */
131914
+ testcase( i==105 ); /* DROP */
131915
+ testcase( i==106 ); /* FAIL */
131916
+ testcase( i==107 ); /* FROM */
131917
+ testcase( i==108 ); /* FULL */
131918
+ testcase( i==109 ); /* GLOB */
131919
+ testcase( i==110 ); /* BY */
131920
+ testcase( i==111 ); /* IF */
131921
+ testcase( i==112 ); /* ISNULL */
131922
+ testcase( i==113 ); /* ORDER */
131923
+ testcase( i==114 ); /* RESTRICT */
131924
+ testcase( i==115 ); /* RIGHT */
131925
+ testcase( i==116 ); /* ROLLBACK */
131926
+ testcase( i==117 ); /* ROW */
131927
+ testcase( i==118 ); /* UNION */
131928
+ testcase( i==119 ); /* USING */
131929
+ testcase( i==120 ); /* VACUUM */
131930
+ testcase( i==121 ); /* VIEW */
131931
+ testcase( i==122 ); /* INITIALLY */
131932
+ testcase( i==123 ); /* ALL */
131933
+ *pType = aCode[i];
131934
+ break;
131414131935
}
131415131936
}
131416131937
return n;
131417131938
}
131418131939
SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
@@ -131469,74 +131990,76 @@
131469131990
SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
131470131991
#endif
131471131992
131472131993
131473131994
/*
131474
-** Return the length of the token that begins at z[0].
131995
+** Return the length (in bytes) of the token that begins at z[0].
131475131996
** Store the token type in *tokenType before returning.
131476131997
*/
131477131998
SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
131478131999
int i, c;
131479
- switch( *z ){
131480
- case ' ': case '\t': case '\n': case '\f': case '\r': {
132000
+ switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
132001
+ ** of the token. See the comment on the CC_ defines
132002
+ ** above. */
132003
+ case CC_SPACE: {
131481132004
testcase( z[0]==' ' );
131482132005
testcase( z[0]=='\t' );
131483132006
testcase( z[0]=='\n' );
131484132007
testcase( z[0]=='\f' );
131485132008
testcase( z[0]=='\r' );
131486132009
for(i=1; sqlite3Isspace(z[i]); i++){}
131487132010
*tokenType = TK_SPACE;
131488132011
return i;
131489132012
}
131490
- case '-': {
132013
+ case CC_MINUS: {
131491132014
if( z[1]=='-' ){
131492132015
for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
131493132016
*tokenType = TK_SPACE; /* IMP: R-22934-25134 */
131494132017
return i;
131495132018
}
131496132019
*tokenType = TK_MINUS;
131497132020
return 1;
131498132021
}
131499
- case '(': {
132022
+ case CC_LP: {
131500132023
*tokenType = TK_LP;
131501132024
return 1;
131502132025
}
131503
- case ')': {
132026
+ case CC_RP: {
131504132027
*tokenType = TK_RP;
131505132028
return 1;
131506132029
}
131507
- case ';': {
132030
+ case CC_SEMI: {
131508132031
*tokenType = TK_SEMI;
131509132032
return 1;
131510132033
}
131511
- case '+': {
132034
+ case CC_PLUS: {
131512132035
*tokenType = TK_PLUS;
131513132036
return 1;
131514132037
}
131515
- case '*': {
132038
+ case CC_STAR: {
131516132039
*tokenType = TK_STAR;
131517132040
return 1;
131518132041
}
131519
- case '/': {
132042
+ case CC_SLASH: {
131520132043
if( z[1]!='*' || z[2]==0 ){
131521132044
*tokenType = TK_SLASH;
131522132045
return 1;
131523132046
}
131524132047
for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
131525132048
if( c ) i++;
131526132049
*tokenType = TK_SPACE; /* IMP: R-22934-25134 */
131527132050
return i;
131528132051
}
131529
- case '%': {
132052
+ case CC_PERCENT: {
131530132053
*tokenType = TK_REM;
131531132054
return 1;
131532132055
}
131533
- case '=': {
132056
+ case CC_EQ: {
131534132057
*tokenType = TK_EQ;
131535132058
return 1 + (z[1]=='=');
131536132059
}
131537
- case '<': {
132060
+ case CC_LT: {
131538132061
if( (c=z[1])=='=' ){
131539132062
*tokenType = TK_LE;
131540132063
return 2;
131541132064
}else if( c=='>' ){
131542132065
*tokenType = TK_NE;
@@ -131547,11 +132070,11 @@
131547132070
}else{
131548132071
*tokenType = TK_LT;
131549132072
return 1;
131550132073
}
131551132074
}
131552
- case '>': {
132075
+ case CC_GT: {
131553132076
if( (c=z[1])=='=' ){
131554132077
*tokenType = TK_GE;
131555132078
return 2;
131556132079
}else if( c=='>' ){
131557132080
*tokenType = TK_RSHIFT;
@@ -131559,43 +132082,41 @@
131559132082
}else{
131560132083
*tokenType = TK_GT;
131561132084
return 1;
131562132085
}
131563132086
}
131564
- case '!': {
132087
+ case CC_BANG: {
131565132088
if( z[1]!='=' ){
131566132089
*tokenType = TK_ILLEGAL;
131567132090
return 2;
131568132091
}else{
131569132092
*tokenType = TK_NE;
131570132093
return 2;
131571132094
}
131572132095
}
131573
- case '|': {
132096
+ case CC_PIPE: {
131574132097
if( z[1]!='|' ){
131575132098
*tokenType = TK_BITOR;
131576132099
return 1;
131577132100
}else{
131578132101
*tokenType = TK_CONCAT;
131579132102
return 2;
131580132103
}
131581132104
}
131582
- case ',': {
132105
+ case CC_COMMA: {
131583132106
*tokenType = TK_COMMA;
131584132107
return 1;
131585132108
}
131586
- case '&': {
132109
+ case CC_AND: {
131587132110
*tokenType = TK_BITAND;
131588132111
return 1;
131589132112
}
131590
- case '~': {
132113
+ case CC_TILDA: {
131591132114
*tokenType = TK_BITNOT;
131592132115
return 1;
131593132116
}
131594
- case '`':
131595
- case '\'':
131596
- case '"': {
132117
+ case CC_QUOTE: {
131597132118
int delim = z[0];
131598132119
testcase( delim=='`' );
131599132120
testcase( delim=='\'' );
131600132121
testcase( delim=='"' );
131601132122
for(i=1; (c=z[i])!=0; i++){
@@ -131616,11 +132137,11 @@
131616132137
}else{
131617132138
*tokenType = TK_ILLEGAL;
131618132139
return i;
131619132140
}
131620132141
}
131621
- case '.': {
132142
+ case CC_DOT: {
131622132143
#ifndef SQLITE_OMIT_FLOATING_POINT
131623132144
if( !sqlite3Isdigit(z[1]) )
131624132145
#endif
131625132146
{
131626132147
*tokenType = TK_DOT;
@@ -131627,12 +132148,11 @@
131627132148
return 1;
131628132149
}
131629132150
/* If the next character is a digit, this is a floating point
131630132151
** number that begins with ".". Fall thru into the next case */
131631132152
}
131632
- case '0': case '1': case '2': case '3': case '4':
131633
- case '5': case '6': case '7': case '8': case '9': {
132153
+ case CC_DIGIT: {
131634132154
testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
131635132155
testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
131636132156
testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
131637132157
testcase( z[0]=='9' );
131638132158
*tokenType = TK_INTEGER;
@@ -131663,26 +132183,22 @@
131663132183
*tokenType = TK_ILLEGAL;
131664132184
i++;
131665132185
}
131666132186
return i;
131667132187
}
131668
- case '[': {
132188
+ case CC_QUOTE2: {
131669132189
for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
131670132190
*tokenType = c==']' ? TK_ID : TK_ILLEGAL;
131671132191
return i;
131672132192
}
131673
- case '?': {
132193
+ case CC_VARNUM: {
131674132194
*tokenType = TK_VARIABLE;
131675132195
for(i=1; sqlite3Isdigit(z[i]); i++){}
131676132196
return i;
131677132197
}
131678
-#ifndef SQLITE_OMIT_TCL_VARIABLE
131679
- case '$':
131680
-#endif
131681
- case '@': /* For compatibility with MS SQL Server */
131682
- case '#':
131683
- case ':': {
132198
+ case CC_DOLLAR:
132199
+ case CC_VARALPHA: {
131684132200
int n = 0;
131685132201
testcase( z[0]=='$' ); testcase( z[0]=='@' );
131686132202
testcase( z[0]==':' ); testcase( z[0]=='#' );
131687132203
*tokenType = TK_VARIABLE;
131688132204
for(i=1; (c=z[i])!=0; i++){
@@ -131707,12 +132223,24 @@
131707132223
}
131708132224
}
131709132225
if( n==0 ) *tokenType = TK_ILLEGAL;
131710132226
return i;
131711132227
}
132228
+ case CC_KYWD: {
132229
+ for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
132230
+ if( IdChar(z[i]) ){
132231
+ /* This token started out using characters that can appear in keywords,
132232
+ ** but z[i] is a character not allowed within keywords, so this must
132233
+ ** be an identifier instead */
132234
+ i++;
132235
+ break;
132236
+ }
132237
+ *tokenType = TK_ID;
132238
+ return keywordCode((char*)z, i, tokenType);
132239
+ }
131712132240
#ifndef SQLITE_OMIT_BLOB_LITERAL
131713
- case 'x': case 'X': {
132241
+ case CC_X: {
131714132242
testcase( z[0]=='x' ); testcase( z[0]=='X' );
131715132243
if( z[1]=='\'' ){
131716132244
*tokenType = TK_BLOB;
131717132245
for(i=2; sqlite3Isxdigit(z[i]); i++){}
131718132246
if( z[i]!='\'' || i%2 ){
@@ -131720,24 +132248,26 @@
131720132248
while( z[i] && z[i]!='\'' ){ i++; }
131721132249
}
131722132250
if( z[i] ) i++;
131723132251
return i;
131724132252
}
131725
- /* Otherwise fall through to the next case */
132253
+ /* If it is not a BLOB literal, then it must be an ID, since no
132254
+ ** SQL keywords start with the letter 'x'. Fall through */
131726132255
}
131727132256
#endif
132257
+ case CC_ID: {
132258
+ i = 1;
132259
+ break;
132260
+ }
131728132261
default: {
131729
- if( !IdChar(*z) ){
131730
- break;
131731
- }
131732
- for(i=1; IdChar(z[i]); i++){}
131733
- *tokenType = TK_ID;
131734
- return keywordCode((char*)z, i, tokenType);
132262
+ *tokenType = TK_ILLEGAL;
132263
+ return 1;
131735132264
}
131736132265
}
131737
- *tokenType = TK_ILLEGAL;
131738
- return 1;
132266
+ while( IdChar(z[i]) ){ i++; }
132267
+ *tokenType = TK_ID;
132268
+ return i;
131739132269
}
131740132270
131741132271
/*
131742132272
** Run the parser on the given SQL string. The parser structure is
131743132273
** passed in. An SQLITE_ status code is returned. If an error occurs
@@ -131749,11 +132279,10 @@
131749132279
int nErr = 0; /* Number of errors encountered */
131750132280
int i; /* Loop counter */
131751132281
void *pEngine; /* The LEMON-generated LALR(1) parser */
131752132282
int tokenType; /* type of the next token */
131753132283
int lastTokenParsed = -1; /* type of the previous token */
131754
- u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
131755132284
sqlite3 *db = pParse->db; /* The database connection */
131756132285
int mxSqlLen; /* Max length of an SQL string */
131757132286
131758132287
assert( zSql!=0 );
131759132288
mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
@@ -131765,20 +132294,18 @@
131765132294
i = 0;
131766132295
assert( pzErrMsg!=0 );
131767132296
/* sqlite3ParserTrace(stdout, "parser: "); */
131768132297
pEngine = sqlite3ParserAlloc(sqlite3Malloc);
131769132298
if( pEngine==0 ){
131770
- db->mallocFailed = 1;
132299
+ sqlite3OomFault(db);
131771132300
return SQLITE_NOMEM;
131772132301
}
131773132302
assert( pParse->pNewTable==0 );
131774132303
assert( pParse->pNewTrigger==0 );
131775132304
assert( pParse->nVar==0 );
131776132305
assert( pParse->nzVar==0 );
131777132306
assert( pParse->azVar==0 );
131778
- enableLookaside = db->lookaside.bEnabled;
131779
- if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
131780132307
while( zSql[i]!=0 ){
131781132308
assert( i>=0 );
131782132309
pParse->sLastToken.z = &zSql[i];
131783132310
pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
131784132311
i += pParse->sLastToken.n;
@@ -131787,11 +132314,10 @@
131787132314
break;
131788132315
}
131789132316
if( tokenType>=TK_SPACE ){
131790132317
assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
131791132318
if( db->u1.isInterrupted ){
131792
- sqlite3ErrorMsg(pParse, "interrupt");
131793132319
pParse->rc = SQLITE_INTERRUPT;
131794132320
break;
131795132321
}
131796132322
if( tokenType==TK_ILLEGAL ){
131797132323
sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
@@ -131822,11 +132348,10 @@
131822132348
sqlite3ParserStackPeak(pEngine)
131823132349
);
131824132350
sqlite3_mutex_leave(sqlite3MallocMutex());
131825132351
#endif /* YYDEBUG */
131826132352
sqlite3ParserFree(pEngine, sqlite3_free);
131827
- db->lookaside.bEnabled = enableLookaside;
131828132353
if( db->mallocFailed ){
131829132354
pParse->rc = SQLITE_NOMEM;
131830132355
}
131831132356
if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
131832132357
pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -132963,16 +133488,16 @@
132963133488
p->pNext = db->lookaside.pFree;
132964133489
db->lookaside.pFree = p;
132965133490
p = (LookasideSlot*)&((u8*)p)[sz];
132966133491
}
132967133492
db->lookaside.pEnd = p;
132968
- db->lookaside.bEnabled = 1;
133493
+ db->lookaside.bDisable = 0;
132969133494
db->lookaside.bMalloced = pBuf==0 ?1:0;
132970133495
}else{
132971133496
db->lookaside.pStart = db;
132972133497
db->lookaside.pEnd = db;
132973
- db->lookaside.bEnabled = 0;
133498
+ db->lookaside.bDisable = 1;
132974133499
db->lookaside.bMalloced = 0;
132975133500
}
132976133501
#endif /* SQLITE_OMIT_LOOKASIDE */
132977133502
return SQLITE_OK;
132978133503
}
@@ -134473,11 +134998,11 @@
134473134998
/* A malloc() may have failed within the call to sqlite3_value_text16()
134474134999
** above. If this is the case, then the db->mallocFailed flag needs to
134475135000
** be cleared before returning. Do this directly, instead of via
134476135001
** sqlite3ApiExit(), to avoid setting the database handle error message.
134477135002
*/
134478
- db->mallocFailed = 0;
135003
+ sqlite3OomClear(db);
134479135004
}
134480135005
sqlite3_mutex_leave(db->mutex);
134481135006
return z;
134482135007
}
134483135008
#endif /* SQLITE_OMIT_UTF16 */
@@ -135111,11 +135636,11 @@
135111135636
135112135637
/* Parse the filename/URI argument. */
135113135638
db->openFlags = flags;
135114135639
rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
135115135640
if( rc!=SQLITE_OK ){
135116
- if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
135641
+ if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
135117135642
sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
135118135643
sqlite3_free(zErrMsg);
135119135644
goto opendb_out;
135120135645
}
135121135646
@@ -135831,11 +136356,11 @@
135831136356
** process aborts. If X is false and assert() is disabled, then the
135832136357
** return value is zero.
135833136358
*/
135834136359
case SQLITE_TESTCTRL_ASSERT: {
135835136360
volatile int x = 0;
135836
- assert( (x = va_arg(ap,int))!=0 );
136361
+ assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
135837136362
rc = x;
135838136363
break;
135839136364
}
135840136365
135841136366
@@ -136864,10 +137389,16 @@
136864137389
#define _FTSINT_H
136865137390
136866137391
#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
136867137392
# define NDEBUG 1
136868137393
#endif
137394
+
137395
+/* FTS3/FTS4 require virtual tables */
137396
+#ifdef SQLITE_OMIT_VIRTUALTABLE
137397
+# undef SQLITE_ENABLE_FTS3
137398
+# undef SQLITE_ENABLE_FTS4
137399
+#endif
136869137400
136870137401
/*
136871137402
** FTS4 is really an extension for FTS3. It is enabled using the
136872137403
** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
136873137404
** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
@@ -146361,10 +146892,11 @@
146361146892
146362146893
zName = sqlite3_value_text(argv[0]);
146363146894
nName = sqlite3_value_bytes(argv[0])+1;
146364146895
146365146896
if( argc==2 ){
146897
+#ifdef SQLITE_ENABLE_FTS3_TOKENIZER
146366146898
void *pOld;
146367146899
int n = sqlite3_value_bytes(argv[1]);
146368146900
if( zName==0 || n!=sizeof(pPtr) ){
146369146901
sqlite3_result_error(context, "argument type mismatch", -1);
146370146902
return;
@@ -146373,11 +146905,18 @@
146373146905
pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
146374146906
if( pOld==pPtr ){
146375146907
sqlite3_result_error(context, "out of memory", -1);
146376146908
return;
146377146909
}
146378
- }else{
146910
+#else
146911
+ sqlite3_result_error(context, "fts3tokenize: "
146912
+ "disabled - rebuild with -DSQLITE_ENABLE_FTS3_TOKENIZER", -1
146913
+ );
146914
+ return;
146915
+#endif /* SQLITE_ENABLE_FTS3_TOKENIZER */
146916
+ }else
146917
+ {
146379146918
if( zName ){
146380146919
pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
146381146920
}
146382146921
if( !pPtr ){
146383146922
char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -146622,10 +147161,11 @@
146622147161
sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
146623147162
}
146624147163
Tcl_DecrRefCount(pRet);
146625147164
}
146626147165
147166
+#ifdef SQLITE_ENABLE_FTS3_TOKENIZER
146627147167
static
146628147168
int registerTokenizer(
146629147169
sqlite3 *db,
146630147170
char *zName,
146631147171
const sqlite3_tokenizer_module *p
@@ -146643,10 +147183,12 @@
146643147183
sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
146644147184
sqlite3_step(pStmt);
146645147185
146646147186
return sqlite3_finalize(pStmt);
146647147187
}
147188
+#endif /* SQLITE_ENABLE_FTS3_TOKENIZER */
147189
+
146648147190
146649147191
static
146650147192
int queryTokenizer(
146651147193
sqlite3 *db,
146652147194
char *zName,
@@ -146714,15 +147256,17 @@
146714147256
assert( rc==SQLITE_ERROR );
146715147257
assert( p2==0 );
146716147258
assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
146717147259
146718147260
/* Test the storage function */
147261
+#ifdef SQLITE_ENABLE_FTS3_TOKENIZER
146719147262
rc = registerTokenizer(db, "nosuchtokenizer", p1);
146720147263
assert( rc==SQLITE_OK );
146721147264
rc = queryTokenizer(db, "nosuchtokenizer", &p2);
146722147265
assert( rc==SQLITE_OK );
146723147266
assert( p2==p1 );
147267
+#endif
146724147268
146725147269
sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
146726147270
}
146727147271
146728147272
#endif
@@ -164601,11 +165145,13 @@
164601165145
StatTable *pTab = 0;
164602165146
int rc = SQLITE_OK;
164603165147
int iDb;
164604165148
164605165149
if( argc>=4 ){
164606
- iDb = sqlite3FindDbName(db, argv[3]);
165150
+ Token nm;
165151
+ sqlite3TokenInit(&nm, (char*)argv[3]);
165152
+ iDb = sqlite3FindDb(db, &nm);
164607165153
if( iDb<0 ){
164608165154
*pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
164609165155
return SQLITE_ERROR;
164610165156
}
164611165157
}else{
@@ -165186,11 +165732,15 @@
165186165732
/* #include <assert.h> */
165187165733
/* #include <string.h> */
165188165734
/* #include <stdlib.h> */
165189165735
/* #include <stdarg.h> */
165190165736
165191
-#define UNUSED_PARAM(X) (void)(X)
165737
+/* Mark a function parameter as unused, to suppress nuisance compiler
165738
+** warnings. */
165739
+#ifndef UNUSED_PARAM
165740
+# define UNUSED_PARAM(X) (void)(X)
165741
+#endif
165192165742
165193165743
#ifndef LARGEST_INT64
165194165744
# define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
165195165745
# define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
165196165746
#endif
@@ -165431,14 +165981,37 @@
165431165981
static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
165432165982
u32 i;
165433165983
if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
165434165984
p->zBuf[p->nUsed++] = '"';
165435165985
for(i=0; i<N; i++){
165436
- char c = zIn[i];
165986
+ unsigned char c = ((unsigned const char*)zIn)[i];
165437165987
if( c=='"' || c=='\\' ){
165988
+ json_simple_escape:
165438165989
if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
165439165990
p->zBuf[p->nUsed++] = '\\';
165991
+ }else if( c<=0x1f ){
165992
+ static const char aSpecial[] = {
165993
+ 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
165994
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
165995
+ };
165996
+ assert( sizeof(aSpecial)==32 );
165997
+ assert( aSpecial['\b']=='b' );
165998
+ assert( aSpecial['\f']=='f' );
165999
+ assert( aSpecial['\n']=='n' );
166000
+ assert( aSpecial['\r']=='r' );
166001
+ assert( aSpecial['\t']=='t' );
166002
+ if( aSpecial[c] ){
166003
+ c = aSpecial[c];
166004
+ goto json_simple_escape;
166005
+ }
166006
+ if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
166007
+ p->zBuf[p->nUsed++] = '\\';
166008
+ p->zBuf[p->nUsed++] = 'u';
166009
+ p->zBuf[p->nUsed++] = '0';
166010
+ p->zBuf[p->nUsed++] = '0';
166011
+ p->zBuf[p->nUsed++] = '0' + (c>>4);
166012
+ c = "0123456789abcdef"[c&0xf];
165440166013
}
165441166014
p->zBuf[p->nUsed++] = c;
165442166015
}
165443166016
p->zBuf[p->nUsed++] = '"';
165444166017
assert( p->nUsed<p->nAlloc );
@@ -165475,11 +166048,11 @@
165475166048
break;
165476166049
}
165477166050
default: {
165478166051
if( p->bErr==0 ){
165479166052
sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
165480
- p->bErr = 1;
166053
+ p->bErr = 2;
165481166054
jsonReset(p);
165482166055
}
165483166056
break;
165484166057
}
165485166058
}
@@ -166684,10 +167257,11 @@
166684167257
sqlite3_context *ctx,
166685167258
int argc,
166686167259
sqlite3_value **argv
166687167260
){
166688167261
JsonString *pStr;
167262
+ UNUSED_PARAM(argc);
166689167263
pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
166690167264
if( pStr ){
166691167265
if( pStr->zBuf==0 ){
166692167266
jsonInit(pStr, ctx);
166693167267
jsonAppendChar(pStr, '[');
@@ -166703,11 +167277,11 @@
166703167277
pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
166704167278
if( pStr ){
166705167279
pStr->pCtx = ctx;
166706167280
jsonAppendChar(pStr, ']');
166707167281
if( pStr->bErr ){
166708
- sqlite3_result_error_nomem(ctx);
167282
+ if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
166709167283
assert( pStr->bStatic );
166710167284
}else{
166711167285
sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
166712167286
pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
166713167287
pStr->bStatic = 1;
@@ -166729,10 +167303,11 @@
166729167303
sqlite3_value **argv
166730167304
){
166731167305
JsonString *pStr;
166732167306
const char *z;
166733167307
u32 n;
167308
+ UNUSED_PARAM(argc);
166734167309
pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
166735167310
if( pStr ){
166736167311
if( pStr->zBuf==0 ){
166737167312
jsonInit(pStr, ctx);
166738167313
jsonAppendChar(pStr, '{');
@@ -166751,11 +167326,11 @@
166751167326
JsonString *pStr;
166752167327
pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
166753167328
if( pStr ){
166754167329
jsonAppendChar(pStr, '}');
166755167330
if( pStr->bErr ){
166756
- sqlite3_result_error_nomem(ctx);
167331
+ if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx);
166757167332
assert( pStr->bStatic );
166758167333
}else{
166759167334
sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
166760167335
pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
166761167336
pStr->bStatic = 1;
@@ -167937,14 +168512,15 @@
167937168512
#ifndef SQLITE_AMALGAMATION
167938168513
167939168514
typedef unsigned char u8;
167940168515
typedef unsigned int u32;
167941168516
typedef unsigned short u16;
168517
+typedef short i16;
167942168518
typedef sqlite3_int64 i64;
167943168519
typedef sqlite3_uint64 u64;
167944168520
167945
-#define ArraySize(x) (sizeof(x) / sizeof(x[0]))
168521
+#define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
167946168522
167947168523
#define testcase(x)
167948168524
#define ALWAYS(x) 1
167949168525
#define NEVER(x) 0
167950168526
@@ -167990,10 +168566,20 @@
167990168566
SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
167991168567
# define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
167992168568
#else
167993168569
# define assert_nc(x) assert(x)
167994168570
#endif
168571
+
168572
+/* Mark a function parameter as unused, to suppress nuisance compiler
168573
+** warnings. */
168574
+#ifndef UNUSED_PARAM
168575
+# define UNUSED_PARAM(X) (void)(X)
168576
+#endif
168577
+
168578
+#ifndef UNUSED_PARAM2
168579
+# define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
168580
+#endif
167995168581
167996168582
typedef struct Fts5Global Fts5Global;
167997168583
typedef struct Fts5Colset Fts5Colset;
167998168584
167999168585
/* If a NEAR() clump or phrase may only match a specific set of columns,
@@ -168136,12 +168722,12 @@
168136168722
** Buffer object for the incremental building of string data.
168137168723
*/
168138168724
typedef struct Fts5Buffer Fts5Buffer;
168139168725
struct Fts5Buffer {
168140168726
u8 *p;
168141
- u32 n;
168142
- u32 nSpace;
168727
+ int n;
168728
+ int nSpace;
168143168729
};
168144168730
168145168731
static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
168146168732
static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
168147168733
static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
@@ -168158,11 +168744,11 @@
168158168744
#define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
168159168745
#define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
168160168746
#define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
168161168747
168162168748
#define fts5BufferGrow(pRc,pBuf,nn) ( \
168163
- (pBuf)->n + (nn) <= (pBuf)->nSpace ? 0 : \
168749
+ (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
168164168750
sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
168165168751
)
168166168752
168167168753
/* Write and decode big-endian 32-bit integer values */
168168168754
static void sqlite3Fts5Put32(u8*, int);
@@ -168193,10 +168779,11 @@
168193168779
typedef struct Fts5PoslistWriter Fts5PoslistWriter;
168194168780
struct Fts5PoslistWriter {
168195168781
i64 iPrev;
168196168782
};
168197168783
static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
168784
+static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
168198168785
168199168786
static int sqlite3Fts5PoslistNext64(
168200168787
const u8 *a, int n, /* Buffer containing poslist */
168201168788
int *pi, /* IN/OUT: Offset within a[] */
168202168789
i64 *piOff /* IN/OUT: Current offset */
@@ -168226,34 +168813,39 @@
168226168813
*/
168227168814
168228168815
typedef struct Fts5Index Fts5Index;
168229168816
typedef struct Fts5IndexIter Fts5IndexIter;
168230168817
168818
+struct Fts5IndexIter {
168819
+ i64 iRowid;
168820
+ const u8 *pData;
168821
+ int nData;
168822
+ u8 bEof;
168823
+};
168824
+
168825
+#define sqlite3Fts5IterEof(x) ((x)->bEof)
168826
+
168231168827
/*
168232168828
** Values used as part of the flags argument passed to IndexQuery().
168233168829
*/
168234168830
#define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
168235168831
#define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
168236168832
#define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
168237168833
#define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
168238168834
168835
+/* The following are used internally by the fts5_index.c module. They are
168836
+** defined here only to make it easier to avoid clashes with the flags
168837
+** above. */
168838
+#define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
168839
+#define FTS5INDEX_QUERY_NOOUTPUT 0x0020
168840
+
168239168841
/*
168240168842
** Create/destroy an Fts5Index object.
168241168843
*/
168242168844
static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
168243168845
static int sqlite3Fts5IndexClose(Fts5Index *p);
168244168846
168245
-/*
168246
-** for(
168247
-** sqlite3Fts5IndexQuery(p, "token", 5, 0, 0, &pIter);
168248
-** 0==sqlite3Fts5IterEof(pIter);
168249
-** sqlite3Fts5IterNext(pIter)
168250
-** ){
168251
-** i64 iRowid = sqlite3Fts5IterRowid(pIter);
168252
-** }
168253
-*/
168254
-
168255168847
/*
168256168848
** Return a simple checksum value based on the arguments.
168257168849
*/
168258168850
static u64 sqlite3Fts5IndexEntryCksum(
168259168851
i64 iRowid,
@@ -168289,16 +168881,12 @@
168289168881
168290168882
/*
168291168883
** The various operations on open token or token prefix iterators opened
168292168884
** using sqlite3Fts5IndexQuery().
168293168885
*/
168294
-static int sqlite3Fts5IterEof(Fts5IndexIter*);
168295168886
static int sqlite3Fts5IterNext(Fts5IndexIter*);
168296168887
static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
168297
-static i64 sqlite3Fts5IterRowid(Fts5IndexIter*);
168298
-static int sqlite3Fts5IterPoslist(Fts5IndexIter*,Fts5Colset*, const u8**, int*, i64*);
168299
-static int sqlite3Fts5IterPoslistBuffer(Fts5IndexIter *pIter, Fts5Buffer *pBuf);
168300168888
168301168889
/*
168302168890
** Close an iterator opened by sqlite3Fts5IndexQuery().
168303168891
*/
168304168892
static void sqlite3Fts5IterClose(Fts5IndexIter*);
@@ -168380,12 +168968,10 @@
168380168968
static int sqlite3Fts5IndexOptimize(Fts5Index *p);
168381168969
static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
168382168970
168383168971
static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
168384168972
168385
-static int sqlite3Fts5IterCollist(Fts5IndexIter*, const u8 **, int*);
168386
-
168387168973
/*
168388168974
** End of interface to code in fts5_index.c.
168389168975
**************************************************************************/
168390168976
168391168977
/**************************************************************************
@@ -168581,11 +169167,11 @@
168581169167
Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
168582169168
);
168583169169
static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
168584169170
static void sqlite3Fts5ExprClearEof(Fts5Expr*);
168585169171
168586
-static int sqlite3Fts5ExprClonePhrase(Fts5Config*, Fts5Expr*, int, Fts5Expr**);
169172
+static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
168587169173
168588169174
static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
168589169175
168590169176
/*******************************************
168591169177
** The fts5_expr.c API above this point is used by the other hand-written
@@ -169372,11 +169958,12 @@
169372169958
while( fts5yypParser->fts5yyidx>=0 ) fts5yy_pop_parser_stack(fts5yypParser);
169373169959
/* Here code is inserted which will execute if the parser
169374169960
** stack every overflows */
169375169961
/******** Begin %stack_overflow code ******************************************/
169376169962
169377
- assert( 0 );
169963
+ UNUSED_PARAM(fts5yypMinor); /* Silence a compiler warning */
169964
+ sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
169378169965
/******** End %stack_overflow code ********************************************/
169379169966
sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
169380169967
}
169381169968
169382169969
/*
@@ -169669,10 +170256,11 @@
169669170256
){
169670170257
sqlite3Fts5ParserARG_FETCH;
169671170258
#define FTS5TOKEN (fts5yyminor.fts5yy0)
169672170259
/************ Begin %syntax_error code ****************************************/
169673170260
170261
+ UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
169674170262
sqlite3Fts5ParseError(
169675170263
pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
169676170264
);
169677170265
/************ End %syntax_error code ******************************************/
169678170266
sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
@@ -170045,10 +170633,12 @@
170045170633
int iEndOff /* End offset of token */
170046170634
){
170047170635
HighlightContext *p = (HighlightContext*)pContext;
170048170636
int rc = SQLITE_OK;
170049170637
int iPos;
170638
+
170639
+ UNUSED_PARAM2(pToken, nToken);
170050170640
170051170641
if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
170052170642
iPos = p->iPos++;
170053170643
170054170644
if( p->iRangeEnd>0 ){
@@ -170279,10 +170869,11 @@
170279170869
const Fts5ExtensionApi *pApi,
170280170870
Fts5Context *pFts,
170281170871
void *pUserData /* Pointer to sqlite3_int64 variable */
170282170872
){
170283170873
sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
170874
+ UNUSED_PARAM2(pApi, pFts);
170284170875
(*pn)++;
170285170876
return SQLITE_OK;
170286170877
}
170287170878
170288170879
/*
@@ -170432,11 +171023,11 @@
170432171023
{ "bm25", 0, fts5Bm25Function, 0 },
170433171024
};
170434171025
int rc = SQLITE_OK; /* Return code */
170435171026
int i; /* To iterate through builtin functions */
170436171027
170437
- for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aBuiltin); i++){
171028
+ for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
170438171029
rc = pApi->xCreateFunction(pApi,
170439171030
aBuiltin[i].zFunc,
170440171031
aBuiltin[i].pUserData,
170441171032
aBuiltin[i].xFunc,
170442171033
aBuiltin[i].xDestroy
@@ -170464,22 +171055,24 @@
170464171055
170465171056
170466171057
/* #include "fts5Int.h" */
170467171058
170468171059
static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
170469
- u32 nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64;
170470
- u8 *pNew;
170471
- while( nNew<nByte ){
170472
- nNew = nNew * 2;
170473
- }
170474
- pNew = sqlite3_realloc(pBuf->p, nNew);
170475
- if( pNew==0 ){
170476
- *pRc = SQLITE_NOMEM;
170477
- return 1;
170478
- }else{
170479
- pBuf->nSpace = nNew;
170480
- pBuf->p = pNew;
171060
+ if( (u32)pBuf->nSpace<nByte ){
171061
+ u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
171062
+ u8 *pNew;
171063
+ while( nNew<nByte ){
171064
+ nNew = nNew * 2;
171065
+ }
171066
+ pNew = sqlite3_realloc(pBuf->p, nNew);
171067
+ if( pNew==0 ){
171068
+ *pRc = SQLITE_NOMEM;
171069
+ return 1;
171070
+ }else{
171071
+ pBuf->nSpace = nNew;
171072
+ pBuf->p = pNew;
171073
+ }
170481171074
}
170482171075
return 0;
170483171076
}
170484171077
170485171078
@@ -170655,28 +171248,41 @@
170655171248
pIter->a = a;
170656171249
pIter->n = n;
170657171250
sqlite3Fts5PoslistReaderNext(pIter);
170658171251
return pIter->bEof;
170659171252
}
171253
+
171254
+/*
171255
+** Append position iPos to the position list being accumulated in buffer
171256
+** pBuf, which must be already be large enough to hold the new data.
171257
+** The previous position written to this list is *piPrev. *piPrev is set
171258
+** to iPos before returning.
171259
+*/
171260
+static void sqlite3Fts5PoslistSafeAppend(
171261
+ Fts5Buffer *pBuf,
171262
+ i64 *piPrev,
171263
+ i64 iPos
171264
+){
171265
+ static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
171266
+ if( (iPos & colmask) != (*piPrev & colmask) ){
171267
+ pBuf->p[pBuf->n++] = 1;
171268
+ pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
171269
+ *piPrev = (iPos & colmask);
171270
+ }
171271
+ pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
171272
+ *piPrev = iPos;
171273
+}
170660171274
170661171275
static int sqlite3Fts5PoslistWriterAppend(
170662171276
Fts5Buffer *pBuf,
170663171277
Fts5PoslistWriter *pWriter,
170664171278
i64 iPos
170665171279
){
170666
- static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
170667
- int rc = SQLITE_OK;
170668
- if( 0==fts5BufferGrow(&rc, pBuf, 5+5+5) ){
170669
- if( (iPos & colmask) != (pWriter->iPrev & colmask) ){
170670
- pBuf->p[pBuf->n++] = 1;
170671
- pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
170672
- pWriter->iPrev = (iPos & colmask);
170673
- }
170674
- pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-pWriter->iPrev)+2);
170675
- pWriter->iPrev = iPos;
170676
- }
170677
- return rc;
171280
+ int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
171281
+ if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
171282
+ sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
171283
+ return SQLITE_OK;
170678171284
}
170679171285
170680171286
static void *sqlite3Fts5MallocZero(int *pRc, int nByte){
170681171287
void *pRet = 0;
170682171288
if( *pRc==SQLITE_OK ){
@@ -170770,11 +171376,11 @@
170770171376
){
170771171377
int rc = SQLITE_OK;
170772171378
*pbPresent = 0;
170773171379
if( p ){
170774171380
int i;
170775
- int hash = 13;
171381
+ u32 hash = 13;
170776171382
Fts5TermsetEntry *pEntry;
170777171383
170778171384
/* Calculate a hash value for this term. This is the same hash checksum
170779171385
** used by the fts5_hash.c module. This is not important for correct
170780171386
** operation of the module, but is necessary to ensure that some tests
@@ -170787,11 +171393,11 @@
170787171393
170788171394
for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
170789171395
if( pEntry->iIdx==iIdx
170790171396
&& pEntry->nTerm==nTerm
170791171397
&& memcmp(pEntry->pTerm, pTerm, nTerm)==0
170792
- ){
171398
+ ){
170793171399
*pbPresent = 1;
170794171400
break;
170795171401
}
170796171402
}
170797171403
@@ -170811,11 +171417,11 @@
170811171417
return rc;
170812171418
}
170813171419
170814171420
static void sqlite3Fts5TermsetFree(Fts5Termset *p){
170815171421
if( p ){
170816
- int i;
171422
+ u32 i;
170817171423
for(i=0; i<ArraySize(p->apHash); i++){
170818171424
Fts5TermsetEntry *pEntry = p->apHash[i];
170819171425
while( pEntry ){
170820171426
Fts5TermsetEntry *pDel = pEntry;
170821171427
pEntry = pEntry->pNext;
@@ -170823,13 +171429,10 @@
170823171429
}
170824171430
}
170825171431
sqlite3_free(p);
170826171432
}
170827171433
}
170828
-
170829
-
170830
-
170831171434
170832171435
/*
170833171436
** 2014 Jun 09
170834171437
**
170835171438
** The author disclaims copyright to this source code. In place of
@@ -171035,11 +171638,11 @@
171035171638
static int fts5ConfigSetEnum(
171036171639
const Fts5Enum *aEnum,
171037171640
const char *zEnum,
171038171641
int *peVal
171039171642
){
171040
- int nEnum = strlen(zEnum);
171643
+ int nEnum = (int)strlen(zEnum);
171041171644
int i;
171042171645
int iVal = -1;
171043171646
171044171647
for(i=0; aEnum[i].zName; i++){
171045171648
if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
@@ -171773,11 +172376,10 @@
171773172376
pConfig->iCookie = iCookie;
171774172377
}
171775172378
return rc;
171776172379
}
171777172380
171778
-
171779172381
/*
171780172382
** 2014 May 31
171781172383
**
171782172384
** The author disclaims copyright to this source code. In place of
171783172385
** a legal notice, here is a blessing:
@@ -171838,10 +172440,13 @@
171838172440
struct Fts5ExprNode {
171839172441
int eType; /* Node type */
171840172442
int bEof; /* True at EOF */
171841172443
int bNomatch; /* True if entry is not a match */
171842172444
172445
+ /* Next method for this node. */
172446
+ int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
172447
+
171843172448
i64 iRowid; /* Current rowid */
171844172449
Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
171845172450
171846172451
/* Child nodes. For a NOT node, this array always contains 2 entries. For
171847172452
** AND or OR nodes, it contains 2 or more entries. */
@@ -171849,10 +172454,16 @@
171849172454
Fts5ExprNode *apChild[1]; /* Array of child nodes */
171850172455
};
171851172456
171852172457
#define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
171853172458
172459
+/*
172460
+** Invoke the xNext method of an Fts5ExprNode object. This macro should be
172461
+** used as if it has the same signature as the xNext() methods themselves.
172462
+*/
172463
+#define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
172464
+
171854172465
/*
171855172466
** An instance of the following structure represents a single search term
171856172467
** or term prefix.
171857172468
*/
171858172469
struct Fts5ExprTerm {
@@ -172010,11 +172621,19 @@
172010172621
*ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
172011172622
if( pNew==0 ){
172012172623
sParse.rc = SQLITE_NOMEM;
172013172624
sqlite3Fts5ParseNodeFree(sParse.pExpr);
172014172625
}else{
172015
- pNew->pRoot = sParse.pExpr;
172626
+ if( !sParse.pExpr ){
172627
+ const int nByte = sizeof(Fts5ExprNode);
172628
+ pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
172629
+ if( pNew->pRoot ){
172630
+ pNew->pRoot->bEof = 1;
172631
+ }
172632
+ }else{
172633
+ pNew->pRoot = sParse.pExpr;
172634
+ }
172016172635
pNew->pIndex = 0;
172017172636
pNew->pConfig = pConfig;
172018172637
pNew->apExprPhrase = sParse.apPhrase;
172019172638
pNew->nPhrase = sParse.nPhrase;
172020172639
sParse.apPhrase = 0;
@@ -172062,11 +172681,11 @@
172062172681
172063172682
assert( pTerm->pSynonym );
172064172683
assert( bDesc==0 || bDesc==1 );
172065172684
for(p=pTerm; p; p=p->pSynonym){
172066172685
if( 0==sqlite3Fts5IterEof(p->pIter) ){
172067
- i64 iRowid = sqlite3Fts5IterRowid(p->pIter);
172686
+ i64 iRowid = p->pIter->iRowid;
172068172687
if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
172069172688
iRet = iRowid;
172070172689
bRetValid = 1;
172071172690
}
172072172691
}
@@ -172079,14 +172698,12 @@
172079172698
/*
172080172699
** Argument pTerm must be a synonym iterator.
172081172700
*/
172082172701
static int fts5ExprSynonymList(
172083172702
Fts5ExprTerm *pTerm,
172084
- int bCollist,
172085
- Fts5Colset *pColset,
172086172703
i64 iRowid,
172087
- int *pbDel, /* OUT: Caller should sqlite3_free(*pa) */
172704
+ Fts5Buffer *pBuf, /* Use this buffer for space if required */
172088172705
u8 **pa, int *pn
172089172706
){
172090172707
Fts5PoslistReader aStatic[4];
172091172708
Fts5PoslistReader *aIter = aStatic;
172092172709
int nIter = 0;
@@ -172095,23 +172712,12 @@
172095172712
Fts5ExprTerm *p;
172096172713
172097172714
assert( pTerm->pSynonym );
172098172715
for(p=pTerm; p; p=p->pSynonym){
172099172716
Fts5IndexIter *pIter = p->pIter;
172100
- if( sqlite3Fts5IterEof(pIter)==0 && sqlite3Fts5IterRowid(pIter)==iRowid ){
172101
- const u8 *a;
172102
- int n;
172103
-
172104
- if( bCollist ){
172105
- rc = sqlite3Fts5IterCollist(pIter, &a, &n);
172106
- }else{
172107
- i64 dummy;
172108
- rc = sqlite3Fts5IterPoslist(pIter, pColset, &a, &n, &dummy);
172109
- }
172110
-
172111
- if( rc!=SQLITE_OK ) goto synonym_poslist_out;
172112
- if( n==0 ) continue;
172717
+ if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
172718
+ if( pIter->nData==0 ) continue;
172113172719
if( nIter==nAlloc ){
172114172720
int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
172115172721
Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172116172722
if( aNew==0 ){
172117172723
rc = SQLITE_NOMEM;
@@ -172120,24 +172726,23 @@
172120172726
memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
172121172727
nAlloc = nAlloc*2;
172122172728
if( aIter!=aStatic ) sqlite3_free(aIter);
172123172729
aIter = aNew;
172124172730
}
172125
- sqlite3Fts5PoslistReaderInit(a, n, &aIter[nIter]);
172731
+ sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
172126172732
assert( aIter[nIter].bEof==0 );
172127172733
nIter++;
172128172734
}
172129172735
}
172130172736
172131
- assert( *pbDel==0 );
172132172737
if( nIter==1 ){
172133172738
*pa = (u8*)aIter[0].a;
172134172739
*pn = aIter[0].n;
172135172740
}else{
172136172741
Fts5PoslistWriter writer = {0};
172137
- Fts5Buffer buf = {0,0,0};
172138172742
i64 iPrev = -1;
172743
+ fts5BufferZero(pBuf);
172139172744
while( 1 ){
172140172745
int i;
172141172746
i64 iMin = FTS5_LARGEST_INT64;
172142172747
for(i=0; i<nIter; i++){
172143172748
if( aIter[i].bEof==0 ){
@@ -172148,19 +172753,16 @@
172148172753
iMin = aIter[i].iPos;
172149172754
}
172150172755
}
172151172756
}
172152172757
if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
172153
- rc = sqlite3Fts5PoslistWriterAppend(&buf, &writer, iMin);
172758
+ rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
172154172759
iPrev = iMin;
172155172760
}
172156
- if( rc ){
172157
- sqlite3_free(buf.p);
172158
- }else{
172159
- *pa = buf.p;
172160
- *pn = buf.n;
172161
- *pbDel = 1;
172761
+ if( rc==SQLITE_OK ){
172762
+ *pa = pBuf->p;
172763
+ *pn = pBuf->n;
172162172764
}
172163172765
}
172164172766
172165172767
synonym_poslist_out:
172166172768
if( aIter!=aStatic ) sqlite3_free(aIter);
@@ -172179,11 +172781,10 @@
172179172781
** otherwise. It is not considered an error code if the current rowid is
172180172782
** not a match.
172181172783
*/
172182172784
static int fts5ExprPhraseIsMatch(
172183172785
Fts5ExprNode *pNode, /* Node pPhrase belongs to */
172184
- Fts5Colset *pColset, /* Restrict matches to these columns */
172185172786
Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
172186172787
int *pbMatch /* OUT: Set to true if really a match */
172187172788
){
172188172789
Fts5PoslistWriter writer = {0};
172189172790
Fts5PoslistReader aStatic[4];
@@ -172193,32 +172794,35 @@
172193172794
172194172795
fts5BufferZero(&pPhrase->poslist);
172195172796
172196172797
/* If the aStatic[] array is not large enough, allocate a large array
172197172798
** using sqlite3_malloc(). This approach could be improved upon. */
172198
- if( pPhrase->nTerm>(int)ArraySize(aStatic) ){
172799
+ if( pPhrase->nTerm>ArraySize(aStatic) ){
172199172800
int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
172200172801
aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172201172802
if( !aIter ) return SQLITE_NOMEM;
172202172803
}
172203172804
memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
172204172805
172205172806
/* Initialize a term iterator for each term in the phrase */
172206172807
for(i=0; i<pPhrase->nTerm; i++){
172207172808
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
172208
- i64 dummy;
172209172809
int n = 0;
172210172810
int bFlag = 0;
172211
- const u8 *a = 0;
172811
+ u8 *a = 0;
172212172812
if( pTerm->pSynonym ){
172213
- rc = fts5ExprSynonymList(
172214
- pTerm, 0, pColset, pNode->iRowid, &bFlag, (u8**)&a, &n
172215
- );
172813
+ Fts5Buffer buf = {0, 0, 0};
172814
+ rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
172815
+ if( rc ){
172816
+ sqlite3_free(a);
172817
+ goto ismatch_out;
172818
+ }
172819
+ if( a==buf.p ) bFlag = 1;
172216172820
}else{
172217
- rc = sqlite3Fts5IterPoslist(pTerm->pIter, pColset, &a, &n, &dummy);
172821
+ a = (u8*)pTerm->pIter->pData;
172822
+ n = pTerm->pIter->nData;
172218172823
}
172219
- if( rc!=SQLITE_OK ) goto ismatch_out;
172220172824
sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
172221172825
aIter[i].bFlag = (u8)bFlag;
172222172826
if( aIter[i].bEof ) goto ismatch_out;
172223172827
}
172224172828
@@ -172286,16 +172890,10 @@
172286172890
p->n = n;
172287172891
fts5LookaheadReaderNext(p);
172288172892
return fts5LookaheadReaderNext(p);
172289172893
}
172290172894
172291
-#if 0
172292
-static int fts5LookaheadReaderEof(Fts5LookaheadReader *p){
172293
- return (p->iPos==FTS5_LOOKAHEAD_EOF);
172294
-}
172295
-#endif
172296
-
172297172895
typedef struct Fts5NearTrimmer Fts5NearTrimmer;
172298172896
struct Fts5NearTrimmer {
172299172897
Fts5LookaheadReader reader; /* Input iterator */
172300172898
Fts5PoslistWriter writer; /* Writer context */
172301172899
Fts5Buffer *pOut; /* Output poslist */
@@ -172329,11 +172927,11 @@
172329172927
172330172928
assert( pNear->nPhrase>1 );
172331172929
172332172930
/* If the aStatic[] array is not large enough, allocate a large array
172333172931
** using sqlite3_malloc(). This approach could be improved upon. */
172334
- if( pNear->nPhrase>(int)ArraySize(aStatic) ){
172932
+ if( pNear->nPhrase>ArraySize(aStatic) ){
172335172933
int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
172336172934
a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
172337172935
}else{
172338172936
memset(aStatic, 0, sizeof(aStatic));
172339172937
}
@@ -172406,75 +173004,10 @@
172406173004
if( a!=aStatic ) sqlite3_free(a);
172407173005
return bRet;
172408173006
}
172409173007
}
172410173008
172411
-/*
172412
-** Advance the first term iterator in the first phrase of pNear. Set output
172413
-** variable *pbEof to true if it reaches EOF or if an error occurs.
172414
-**
172415
-** Return SQLITE_OK if successful, or an SQLite error code if an error
172416
-** occurs.
172417
-*/
172418
-static int fts5ExprNearAdvanceFirst(
172419
- Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172420
- Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
172421
- int bFromValid,
172422
- i64 iFrom
172423
-){
172424
- Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
172425
- int rc = SQLITE_OK;
172426
-
172427
- if( pTerm->pSynonym ){
172428
- int bEof = 1;
172429
- Fts5ExprTerm *p;
172430
-
172431
- /* Find the firstest rowid any synonym points to. */
172432
- i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
172433
-
172434
- /* Advance each iterator that currently points to iRowid. Or, if iFrom
172435
- ** is valid - each iterator that points to a rowid before iFrom. */
172436
- for(p=pTerm; p; p=p->pSynonym){
172437
- if( sqlite3Fts5IterEof(p->pIter)==0 ){
172438
- i64 ii = sqlite3Fts5IterRowid(p->pIter);
172439
- if( ii==iRowid
172440
- || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
172441
- ){
172442
- if( bFromValid ){
172443
- rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
172444
- }else{
172445
- rc = sqlite3Fts5IterNext(p->pIter);
172446
- }
172447
- if( rc!=SQLITE_OK ) break;
172448
- if( sqlite3Fts5IterEof(p->pIter)==0 ){
172449
- bEof = 0;
172450
- }
172451
- }else{
172452
- bEof = 0;
172453
- }
172454
- }
172455
- }
172456
-
172457
- /* Set the EOF flag if either all synonym iterators are at EOF or an
172458
- ** error has occurred. */
172459
- pNode->bEof = (rc || bEof);
172460
- }else{
172461
- Fts5IndexIter *pIter = pTerm->pIter;
172462
-
172463
- assert( Fts5NodeIsString(pNode) );
172464
- if( bFromValid ){
172465
- rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
172466
- }else{
172467
- rc = sqlite3Fts5IterNext(pIter);
172468
- }
172469
-
172470
- pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
172471
- }
172472
-
172473
- return rc;
172474
-}
172475
-
172476173009
/*
172477173010
** Advance iterator pIter until it points to a value equal to or laster
172478173011
** than the initial value of *piLast. If this means the iterator points
172479173012
** to a value laster than *piLast, update *piLast to the new lastest value.
172480173013
**
@@ -172490,19 +173023,19 @@
172490173023
int *pbEof /* OUT: Set to true if EOF */
172491173024
){
172492173025
i64 iLast = *piLast;
172493173026
i64 iRowid;
172494173027
172495
- iRowid = sqlite3Fts5IterRowid(pIter);
173028
+ iRowid = pIter->iRowid;
172496173029
if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
172497173030
int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
172498173031
if( rc || sqlite3Fts5IterEof(pIter) ){
172499173032
*pRc = rc;
172500173033
*pbEof = 1;
172501173034
return 1;
172502173035
}
172503
- iRowid = sqlite3Fts5IterRowid(pIter);
173036
+ iRowid = pIter->iRowid;
172504173037
assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
172505173038
}
172506173039
*piLast = iRowid;
172507173040
172508173041
return 0;
@@ -172519,11 +173052,11 @@
172519173052
Fts5ExprTerm *p;
172520173053
int bEof = 0;
172521173054
172522173055
for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
172523173056
if( sqlite3Fts5IterEof(p->pIter)==0 ){
172524
- i64 iRowid = sqlite3Fts5IterRowid(p->pIter);
173057
+ i64 iRowid = p->pIter->iRowid;
172525173058
if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
172526173059
rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
172527173060
}
172528173061
}
172529173062
}
@@ -172551,17 +173084,11 @@
172551173084
Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
172552173085
pPhrase->poslist.n = 0;
172553173086
for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
172554173087
Fts5IndexIter *pIter = pTerm->pIter;
172555173088
if( sqlite3Fts5IterEof(pIter)==0 ){
172556
- int n;
172557
- i64 iRowid;
172558
- rc = sqlite3Fts5IterPoslist(pIter, pNear->pColset, 0, &n, &iRowid);
172559
- if( rc!=SQLITE_OK ){
172560
- *pRc = rc;
172561
- return 0;
172562
- }else if( iRowid==pNode->iRowid && n>0 ){
173089
+ if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
172563173090
pPhrase->poslist.n = 1;
172564173091
}
172565173092
}
172566173093
}
172567173094
return pPhrase->poslist.n;
@@ -172573,16 +173100,15 @@
172573173100
** phrase is not a match, break out of the loop early. */
172574173101
for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
172575173102
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172576173103
if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){
172577173104
int bMatch = 0;
172578
- rc = fts5ExprPhraseIsMatch(pNode, pNear->pColset, pPhrase, &bMatch);
173105
+ rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
172579173106
if( bMatch==0 ) break;
172580173107
}else{
172581
- rc = sqlite3Fts5IterPoslistBuffer(
172582
- pPhrase->aTerm[0].pIter, &pPhrase->poslist
172583
- );
173108
+ Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
173109
+ fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
172584173110
}
172585173111
}
172586173112
172587173113
*pRc = rc;
172588173114
if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
@@ -172590,107 +173116,10 @@
172590173116
}
172591173117
return 0;
172592173118
}
172593173119
}
172594173120
172595
-static int fts5ExprTokenTest(
172596
- Fts5Expr *pExpr, /* Expression that pNear is a part of */
172597
- Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
172598
-){
172599
- /* As this "NEAR" object is actually a single phrase that consists
172600
- ** of a single term only, grab pointers into the poslist managed by the
172601
- ** fts5_index.c iterator object. This is much faster than synthesizing
172602
- ** a new poslist the way we have to for more complicated phrase or NEAR
172603
- ** expressions. */
172604
- Fts5ExprNearset *pNear = pNode->pNear;
172605
- Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
172606
- Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
172607
- Fts5Colset *pColset = pNear->pColset;
172608
- int rc;
172609
-
172610
- assert( pNode->eType==FTS5_TERM );
172611
- assert( pNear->nPhrase==1 && pPhrase->nTerm==1 );
172612
- assert( pPhrase->aTerm[0].pSynonym==0 );
172613
-
172614
- rc = sqlite3Fts5IterPoslist(pIter, pColset,
172615
- (const u8**)&pPhrase->poslist.p, (int*)&pPhrase->poslist.n, &pNode->iRowid
172616
- );
172617
- pNode->bNomatch = (pPhrase->poslist.n==0);
172618
- return rc;
172619
-}
172620
-
172621
-/*
172622
-** All individual term iterators in pNear are guaranteed to be valid when
172623
-** this function is called. This function checks if all term iterators
172624
-** point to the same rowid, and if not, advances them until they do.
172625
-** If an EOF is reached before this happens, *pbEof is set to true before
172626
-** returning.
172627
-**
172628
-** SQLITE_OK is returned if an error occurs, or an SQLite error code
172629
-** otherwise. It is not considered an error code if an iterator reaches
172630
-** EOF.
172631
-*/
172632
-static int fts5ExprNearNextMatch(
172633
- Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172634
- Fts5ExprNode *pNode
172635
-){
172636
- Fts5ExprNearset *pNear = pNode->pNear;
172637
- Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
172638
- int rc = SQLITE_OK;
172639
- i64 iLast; /* Lastest rowid any iterator points to */
172640
- int i, j; /* Phrase and token index, respectively */
172641
- int bMatch; /* True if all terms are at the same rowid */
172642
- const int bDesc = pExpr->bDesc;
172643
-
172644
- /* Check that this node should not be FTS5_TERM */
172645
- assert( pNear->nPhrase>1
172646
- || pNear->apPhrase[0]->nTerm>1
172647
- || pNear->apPhrase[0]->aTerm[0].pSynonym
172648
- );
172649
-
172650
- /* Initialize iLast, the "lastest" rowid any iterator points to. If the
172651
- ** iterator skips through rowids in the default ascending order, this means
172652
- ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
172653
- ** means the minimum rowid. */
172654
- if( pLeft->aTerm[0].pSynonym ){
172655
- iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
172656
- }else{
172657
- iLast = sqlite3Fts5IterRowid(pLeft->aTerm[0].pIter);
172658
- }
172659
-
172660
- do {
172661
- bMatch = 1;
172662
- for(i=0; i<pNear->nPhrase; i++){
172663
- Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172664
- for(j=0; j<pPhrase->nTerm; j++){
172665
- Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
172666
- if( pTerm->pSynonym ){
172667
- i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
172668
- if( iRowid==iLast ) continue;
172669
- bMatch = 0;
172670
- if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
172671
- pNode->bEof = 1;
172672
- return rc;
172673
- }
172674
- }else{
172675
- Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
172676
- i64 iRowid = sqlite3Fts5IterRowid(pIter);
172677
- if( iRowid==iLast ) continue;
172678
- bMatch = 0;
172679
- if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
172680
- return rc;
172681
- }
172682
- }
172683
- }
172684
- }
172685
- }while( bMatch==0 );
172686
-
172687
- pNode->iRowid = iLast;
172688
- pNode->bNomatch = (0==fts5ExprNearTest(&rc, pExpr, pNode));
172689
-
172690
- return rc;
172691
-}
172692173121
172693173122
/*
172694173123
** Initialize all term iterators in the pNear object. If any term is found
172695173124
** to match no documents at all, return immediately without initializing any
172696173125
** further iterators.
@@ -172701,10 +173130,11 @@
172701173130
){
172702173131
Fts5ExprNearset *pNear = pNode->pNear;
172703173132
int i, j;
172704173133
int rc = SQLITE_OK;
172705173134
173135
+ assert( pNode->bNomatch==0 );
172706173136
for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
172707173137
Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172708173138
for(j=0; j<pPhrase->nTerm; j++){
172709173139
Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
172710173140
Fts5ExprTerm *p;
@@ -172735,14 +173165,10 @@
172735173165
}
172736173166
}
172737173167
172738173168
return rc;
172739173169
}
172740
-
172741
-/* fts5ExprNodeNext() calls fts5ExprNodeNextMatch(). And vice-versa. */
172742
-static int fts5ExprNodeNextMatch(Fts5Expr*, Fts5ExprNode*);
172743
-
172744173170
172745173171
/*
172746173172
** If pExpr is an ASC iterator, this function returns a value with the
172747173173
** same sign as:
172748173174
**
@@ -172768,10 +173194,11 @@
172768173194
}
172769173195
172770173196
static void fts5ExprSetEof(Fts5ExprNode *pNode){
172771173197
int i;
172772173198
pNode->bEof = 1;
173199
+ pNode->bNomatch = 0;
172773173200
for(i=0; i<pNode->nChild; i++){
172774173201
fts5ExprSetEof(pNode->apChild[i]);
172775173202
}
172776173203
}
172777173204
@@ -172790,16 +173217,279 @@
172790173217
}
172791173218
}
172792173219
}
172793173220
172794173221
172795
-static int fts5ExprNodeNext(Fts5Expr*, Fts5ExprNode*, int, i64);
173222
+
173223
+/*
173224
+** Compare the values currently indicated by the two nodes as follows:
173225
+**
173226
+** res = (*p1) - (*p2)
173227
+**
173228
+** Nodes that point to values that come later in the iteration order are
173229
+** considered to be larger. Nodes at EOF are the largest of all.
173230
+**
173231
+** This means that if the iteration order is ASC, then numerically larger
173232
+** rowids are considered larger. Or if it is the default DESC, numerically
173233
+** smaller rowids are larger.
173234
+*/
173235
+static int fts5NodeCompare(
173236
+ Fts5Expr *pExpr,
173237
+ Fts5ExprNode *p1,
173238
+ Fts5ExprNode *p2
173239
+){
173240
+ if( p2->bEof ) return -1;
173241
+ if( p1->bEof ) return +1;
173242
+ return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
173243
+}
173244
+
173245
+/*
173246
+** All individual term iterators in pNear are guaranteed to be valid when
173247
+** this function is called. This function checks if all term iterators
173248
+** point to the same rowid, and if not, advances them until they do.
173249
+** If an EOF is reached before this happens, *pbEof is set to true before
173250
+** returning.
173251
+**
173252
+** SQLITE_OK is returned if an error occurs, or an SQLite error code
173253
+** otherwise. It is not considered an error code if an iterator reaches
173254
+** EOF.
173255
+*/
173256
+static int fts5ExprNodeTest_STRING(
173257
+ Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173258
+ Fts5ExprNode *pNode
173259
+){
173260
+ Fts5ExprNearset *pNear = pNode->pNear;
173261
+ Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
173262
+ int rc = SQLITE_OK;
173263
+ i64 iLast; /* Lastest rowid any iterator points to */
173264
+ int i, j; /* Phrase and token index, respectively */
173265
+ int bMatch; /* True if all terms are at the same rowid */
173266
+ const int bDesc = pExpr->bDesc;
173267
+
173268
+ /* Check that this node should not be FTS5_TERM */
173269
+ assert( pNear->nPhrase>1
173270
+ || pNear->apPhrase[0]->nTerm>1
173271
+ || pNear->apPhrase[0]->aTerm[0].pSynonym
173272
+ );
173273
+
173274
+ /* Initialize iLast, the "lastest" rowid any iterator points to. If the
173275
+ ** iterator skips through rowids in the default ascending order, this means
173276
+ ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
173277
+ ** means the minimum rowid. */
173278
+ if( pLeft->aTerm[0].pSynonym ){
173279
+ iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
173280
+ }else{
173281
+ iLast = pLeft->aTerm[0].pIter->iRowid;
173282
+ }
173283
+
173284
+ do {
173285
+ bMatch = 1;
173286
+ for(i=0; i<pNear->nPhrase; i++){
173287
+ Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
173288
+ for(j=0; j<pPhrase->nTerm; j++){
173289
+ Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
173290
+ if( pTerm->pSynonym ){
173291
+ i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
173292
+ if( iRowid==iLast ) continue;
173293
+ bMatch = 0;
173294
+ if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
173295
+ pNode->bNomatch = 0;
173296
+ pNode->bEof = 1;
173297
+ return rc;
173298
+ }
173299
+ }else{
173300
+ Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
173301
+ if( pIter->iRowid==iLast ) continue;
173302
+ bMatch = 0;
173303
+ if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
173304
+ return rc;
173305
+ }
173306
+ }
173307
+ }
173308
+ }
173309
+ }while( bMatch==0 );
173310
+
173311
+ pNode->iRowid = iLast;
173312
+ pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
173313
+ assert( pNode->bEof==0 || pNode->bNomatch==0 );
173314
+
173315
+ return rc;
173316
+}
173317
+
173318
+/*
173319
+** Advance the first term iterator in the first phrase of pNear. Set output
173320
+** variable *pbEof to true if it reaches EOF or if an error occurs.
173321
+**
173322
+** Return SQLITE_OK if successful, or an SQLite error code if an error
173323
+** occurs.
173324
+*/
173325
+static int fts5ExprNodeNext_STRING(
173326
+ Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173327
+ Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
173328
+ int bFromValid,
173329
+ i64 iFrom
173330
+){
173331
+ Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
173332
+ int rc = SQLITE_OK;
173333
+
173334
+ pNode->bNomatch = 0;
173335
+ if( pTerm->pSynonym ){
173336
+ int bEof = 1;
173337
+ Fts5ExprTerm *p;
173338
+
173339
+ /* Find the firstest rowid any synonym points to. */
173340
+ i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
173341
+
173342
+ /* Advance each iterator that currently points to iRowid. Or, if iFrom
173343
+ ** is valid - each iterator that points to a rowid before iFrom. */
173344
+ for(p=pTerm; p; p=p->pSynonym){
173345
+ if( sqlite3Fts5IterEof(p->pIter)==0 ){
173346
+ i64 ii = p->pIter->iRowid;
173347
+ if( ii==iRowid
173348
+ || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
173349
+ ){
173350
+ if( bFromValid ){
173351
+ rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
173352
+ }else{
173353
+ rc = sqlite3Fts5IterNext(p->pIter);
173354
+ }
173355
+ if( rc!=SQLITE_OK ) break;
173356
+ if( sqlite3Fts5IterEof(p->pIter)==0 ){
173357
+ bEof = 0;
173358
+ }
173359
+ }else{
173360
+ bEof = 0;
173361
+ }
173362
+ }
173363
+ }
173364
+
173365
+ /* Set the EOF flag if either all synonym iterators are at EOF or an
173366
+ ** error has occurred. */
173367
+ pNode->bEof = (rc || bEof);
173368
+ }else{
173369
+ Fts5IndexIter *pIter = pTerm->pIter;
173370
+
173371
+ assert( Fts5NodeIsString(pNode) );
173372
+ if( bFromValid ){
173373
+ rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
173374
+ }else{
173375
+ rc = sqlite3Fts5IterNext(pIter);
173376
+ }
173377
+
173378
+ pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
173379
+ }
173380
+
173381
+ if( pNode->bEof==0 ){
173382
+ assert( rc==SQLITE_OK );
173383
+ rc = fts5ExprNodeTest_STRING(pExpr, pNode);
173384
+ }
173385
+
173386
+ return rc;
173387
+}
173388
+
173389
+
173390
+static int fts5ExprNodeTest_TERM(
173391
+ Fts5Expr *pExpr, /* Expression that pNear is a part of */
173392
+ Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
173393
+){
173394
+ /* As this "NEAR" object is actually a single phrase that consists
173395
+ ** of a single term only, grab pointers into the poslist managed by the
173396
+ ** fts5_index.c iterator object. This is much faster than synthesizing
173397
+ ** a new poslist the way we have to for more complicated phrase or NEAR
173398
+ ** expressions. */
173399
+ Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
173400
+ Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
173401
+
173402
+ assert( pNode->eType==FTS5_TERM );
173403
+ assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
173404
+ assert( pPhrase->aTerm[0].pSynonym==0 );
173405
+
173406
+ pPhrase->poslist.n = pIter->nData;
173407
+ if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
173408
+ pPhrase->poslist.p = (u8*)pIter->pData;
173409
+ }
173410
+ pNode->iRowid = pIter->iRowid;
173411
+ pNode->bNomatch = (pPhrase->poslist.n==0);
173412
+ return SQLITE_OK;
173413
+}
173414
+
173415
+/*
173416
+** xNext() method for a node of type FTS5_TERM.
173417
+*/
173418
+static int fts5ExprNodeNext_TERM(
173419
+ Fts5Expr *pExpr,
173420
+ Fts5ExprNode *pNode,
173421
+ int bFromValid,
173422
+ i64 iFrom
173423
+){
173424
+ int rc;
173425
+ Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
173426
+
173427
+ assert( pNode->bEof==0 );
173428
+ if( bFromValid ){
173429
+ rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
173430
+ }else{
173431
+ rc = sqlite3Fts5IterNext(pIter);
173432
+ }
173433
+ if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
173434
+ rc = fts5ExprNodeTest_TERM(pExpr, pNode);
173435
+ }else{
173436
+ pNode->bEof = 1;
173437
+ pNode->bNomatch = 0;
173438
+ }
173439
+ return rc;
173440
+}
173441
+
173442
+static void fts5ExprNodeTest_OR(
173443
+ Fts5Expr *pExpr, /* Expression of which pNode is a part */
173444
+ Fts5ExprNode *pNode /* Expression node to test */
173445
+){
173446
+ Fts5ExprNode *pNext = pNode->apChild[0];
173447
+ int i;
173448
+
173449
+ for(i=1; i<pNode->nChild; i++){
173450
+ Fts5ExprNode *pChild = pNode->apChild[i];
173451
+ int cmp = fts5NodeCompare(pExpr, pNext, pChild);
173452
+ if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
173453
+ pNext = pChild;
173454
+ }
173455
+ }
173456
+ pNode->iRowid = pNext->iRowid;
173457
+ pNode->bEof = pNext->bEof;
173458
+ pNode->bNomatch = pNext->bNomatch;
173459
+}
173460
+
173461
+static int fts5ExprNodeNext_OR(
173462
+ Fts5Expr *pExpr,
173463
+ Fts5ExprNode *pNode,
173464
+ int bFromValid,
173465
+ i64 iFrom
173466
+){
173467
+ int i;
173468
+ i64 iLast = pNode->iRowid;
173469
+
173470
+ for(i=0; i<pNode->nChild; i++){
173471
+ Fts5ExprNode *p1 = pNode->apChild[i];
173472
+ assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
173473
+ if( p1->bEof==0 ){
173474
+ if( (p1->iRowid==iLast)
173475
+ || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
173476
+ ){
173477
+ int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
173478
+ if( rc!=SQLITE_OK ) return rc;
173479
+ }
173480
+ }
173481
+ }
173482
+
173483
+ fts5ExprNodeTest_OR(pExpr, pNode);
173484
+ return SQLITE_OK;
173485
+}
172796173486
172797173487
/*
172798173488
** Argument pNode is an FTS5_AND node.
172799173489
*/
172800
-static int fts5ExprAndNextRowid(
173490
+static int fts5ExprNodeTest_AND(
172801173491
Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172802173492
Fts5ExprNode *pAnd /* FTS5_AND node to advance */
172803173493
){
172804173494
int iChild;
172805173495
i64 iLast = pAnd->iRowid;
@@ -172810,19 +173500,15 @@
172810173500
do {
172811173501
pAnd->bNomatch = 0;
172812173502
bMatch = 1;
172813173503
for(iChild=0; iChild<pAnd->nChild; iChild++){
172814173504
Fts5ExprNode *pChild = pAnd->apChild[iChild];
172815
- if( 0 && pChild->eType==FTS5_STRING ){
172816
- /* TODO */
172817
- }else{
172818
- int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
172819
- if( cmp>0 ){
172820
- /* Advance pChild until it points to iLast or laster */
172821
- rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
172822
- if( rc!=SQLITE_OK ) return rc;
172823
- }
173505
+ int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
173506
+ if( cmp>0 ){
173507
+ /* Advance pChild until it points to iLast or laster */
173508
+ rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
173509
+ if( rc!=SQLITE_OK ) return rc;
172824173510
}
172825173511
172826173512
/* If the child node is now at EOF, so is the parent AND node. Otherwise,
172827173513
** the child node is guaranteed to have advanced at least as far as
172828173514
** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
@@ -172848,190 +173534,99 @@
172848173534
}
172849173535
pAnd->iRowid = iLast;
172850173536
return SQLITE_OK;
172851173537
}
172852173538
172853
-
172854
-/*
172855
-** Compare the values currently indicated by the two nodes as follows:
172856
-**
172857
-** res = (*p1) - (*p2)
172858
-**
172859
-** Nodes that point to values that come later in the iteration order are
172860
-** considered to be larger. Nodes at EOF are the largest of all.
172861
-**
172862
-** This means that if the iteration order is ASC, then numerically larger
172863
-** rowids are considered larger. Or if it is the default DESC, numerically
172864
-** smaller rowids are larger.
172865
-*/
172866
-static int fts5NodeCompare(
172867
- Fts5Expr *pExpr,
172868
- Fts5ExprNode *p1,
172869
- Fts5ExprNode *p2
172870
-){
172871
- if( p2->bEof ) return -1;
172872
- if( p1->bEof ) return +1;
172873
- return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
172874
-}
172875
-
172876
-/*
172877
-** Advance node iterator pNode, part of expression pExpr. If argument
172878
-** bFromValid is zero, then pNode is advanced exactly once. Or, if argument
172879
-** bFromValid is non-zero, then pNode is advanced until it is at or past
172880
-** rowid value iFrom. Whether "past" means "less than" or "greater than"
172881
-** depends on whether this is an ASC or DESC iterator.
172882
-*/
172883
-static int fts5ExprNodeNext(
173539
+static int fts5ExprNodeNext_AND(
173540
+ Fts5Expr *pExpr,
173541
+ Fts5ExprNode *pNode,
173542
+ int bFromValid,
173543
+ i64 iFrom
173544
+){
173545
+ int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
173546
+ if( rc==SQLITE_OK ){
173547
+ rc = fts5ExprNodeTest_AND(pExpr, pNode);
173548
+ }
173549
+ return rc;
173550
+}
173551
+
173552
+static int fts5ExprNodeTest_NOT(
173553
+ Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173554
+ Fts5ExprNode *pNode /* FTS5_NOT node to advance */
173555
+){
173556
+ int rc = SQLITE_OK;
173557
+ Fts5ExprNode *p1 = pNode->apChild[0];
173558
+ Fts5ExprNode *p2 = pNode->apChild[1];
173559
+ assert( pNode->nChild==2 );
173560
+
173561
+ while( rc==SQLITE_OK && p1->bEof==0 ){
173562
+ int cmp = fts5NodeCompare(pExpr, p1, p2);
173563
+ if( cmp>0 ){
173564
+ rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
173565
+ cmp = fts5NodeCompare(pExpr, p1, p2);
173566
+ }
173567
+ assert( rc!=SQLITE_OK || cmp<=0 );
173568
+ if( cmp || p2->bNomatch ) break;
173569
+ rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
173570
+ }
173571
+ pNode->bEof = p1->bEof;
173572
+ pNode->bNomatch = p1->bNomatch;
173573
+ pNode->iRowid = p1->iRowid;
173574
+ if( p1->bEof ){
173575
+ fts5ExprNodeZeroPoslist(p2);
173576
+ }
173577
+ return rc;
173578
+}
173579
+
173580
+static int fts5ExprNodeNext_NOT(
172884173581
Fts5Expr *pExpr,
172885173582
Fts5ExprNode *pNode,
172886173583
int bFromValid,
172887173584
i64 iFrom
172888173585
){
172889
- int rc = SQLITE_OK;
172890
-
172891
- if( pNode->bEof==0 ){
172892
- switch( pNode->eType ){
172893
- case FTS5_STRING: {
172894
- rc = fts5ExprNearAdvanceFirst(pExpr, pNode, bFromValid, iFrom);
172895
- break;
172896
- };
172897
-
172898
- case FTS5_TERM: {
172899
- Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
172900
- if( bFromValid ){
172901
- rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
172902
- }else{
172903
- rc = sqlite3Fts5IterNext(pIter);
172904
- }
172905
- if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
172906
- assert( rc==SQLITE_OK );
172907
- rc = fts5ExprTokenTest(pExpr, pNode);
172908
- }else{
172909
- pNode->bEof = 1;
172910
- }
172911
- return rc;
172912
- };
172913
-
172914
- case FTS5_AND: {
172915
- Fts5ExprNode *pLeft = pNode->apChild[0];
172916
- rc = fts5ExprNodeNext(pExpr, pLeft, bFromValid, iFrom);
172917
- break;
172918
- }
172919
-
172920
- case FTS5_OR: {
172921
- int i;
172922
- i64 iLast = pNode->iRowid;
172923
-
172924
- for(i=0; rc==SQLITE_OK && i<pNode->nChild; i++){
172925
- Fts5ExprNode *p1 = pNode->apChild[i];
172926
- assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
172927
- if( p1->bEof==0 ){
172928
- if( (p1->iRowid==iLast)
172929
- || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
172930
- ){
172931
- rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
172932
- }
172933
- }
172934
- }
172935
-
172936
- break;
172937
- }
172938
-
172939
- default: assert( pNode->eType==FTS5_NOT ); {
172940
- assert( pNode->nChild==2 );
172941
- rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
172942
- break;
172943
- }
172944
- }
172945
-
172946
- if( rc==SQLITE_OK ){
172947
- rc = fts5ExprNodeNextMatch(pExpr, pNode);
172948
- }
172949
- }
172950
-
172951
- /* Assert that if bFromValid was true, either:
172952
- **
172953
- ** a) an error occurred, or
172954
- ** b) the node is now at EOF, or
172955
- ** c) the node is now at or past rowid iFrom.
172956
- */
172957
- assert( bFromValid==0
172958
- || rc!=SQLITE_OK /* a */
172959
- || pNode->bEof /* b */
172960
- || pNode->iRowid==iFrom || pExpr->bDesc==(pNode->iRowid<iFrom) /* c */
172961
- );
172962
-
172963
- return rc;
172964
-}
172965
-
173586
+ int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
173587
+ if( rc==SQLITE_OK ){
173588
+ rc = fts5ExprNodeTest_NOT(pExpr, pNode);
173589
+ }
173590
+ return rc;
173591
+}
172966173592
172967173593
/*
172968173594
** If pNode currently points to a match, this function returns SQLITE_OK
172969173595
** without modifying it. Otherwise, pNode is advanced until it does point
172970173596
** to a match or EOF is reached.
172971173597
*/
172972
-static int fts5ExprNodeNextMatch(
173598
+static int fts5ExprNodeTest(
172973173599
Fts5Expr *pExpr, /* Expression of which pNode is a part */
172974173600
Fts5ExprNode *pNode /* Expression node to test */
172975173601
){
172976173602
int rc = SQLITE_OK;
172977173603
if( pNode->bEof==0 ){
172978173604
switch( pNode->eType ){
172979173605
172980173606
case FTS5_STRING: {
172981
- /* Advance the iterators until they all point to the same rowid */
172982
- rc = fts5ExprNearNextMatch(pExpr, pNode);
173607
+ rc = fts5ExprNodeTest_STRING(pExpr, pNode);
172983173608
break;
172984173609
}
172985173610
172986173611
case FTS5_TERM: {
172987
- rc = fts5ExprTokenTest(pExpr, pNode);
173612
+ rc = fts5ExprNodeTest_TERM(pExpr, pNode);
172988173613
break;
172989173614
}
172990173615
172991173616
case FTS5_AND: {
172992
- rc = fts5ExprAndNextRowid(pExpr, pNode);
173617
+ rc = fts5ExprNodeTest_AND(pExpr, pNode);
172993173618
break;
172994173619
}
172995173620
172996173621
case FTS5_OR: {
172997
- Fts5ExprNode *pNext = pNode->apChild[0];
172998
- int i;
172999
-
173000
- for(i=1; i<pNode->nChild; i++){
173001
- Fts5ExprNode *pChild = pNode->apChild[i];
173002
- int cmp = fts5NodeCompare(pExpr, pNext, pChild);
173003
- if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
173004
- pNext = pChild;
173005
- }
173006
- }
173007
- pNode->iRowid = pNext->iRowid;
173008
- pNode->bEof = pNext->bEof;
173009
- pNode->bNomatch = pNext->bNomatch;
173622
+ fts5ExprNodeTest_OR(pExpr, pNode);
173010173623
break;
173011173624
}
173012173625
173013173626
default: assert( pNode->eType==FTS5_NOT ); {
173014
- Fts5ExprNode *p1 = pNode->apChild[0];
173015
- Fts5ExprNode *p2 = pNode->apChild[1];
173016
- assert( pNode->nChild==2 );
173017
-
173018
- while( rc==SQLITE_OK && p1->bEof==0 ){
173019
- int cmp = fts5NodeCompare(pExpr, p1, p2);
173020
- if( cmp>0 ){
173021
- rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
173022
- cmp = fts5NodeCompare(pExpr, p1, p2);
173023
- }
173024
- assert( rc!=SQLITE_OK || cmp<=0 );
173025
- if( cmp || p2->bNomatch ) break;
173026
- rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
173027
- }
173028
- pNode->bEof = p1->bEof;
173029
- pNode->iRowid = p1->iRowid;
173030
- if( p1->bEof ){
173031
- fts5ExprNodeZeroPoslist(p2);
173032
- }
173627
+ rc = fts5ExprNodeTest_NOT(pExpr, pNode);
173033173628
break;
173034173629
}
173035173630
}
173036173631
}
173037173632
return rc;
@@ -173046,24 +173641,44 @@
173046173641
** It is not an error if there are no matches.
173047173642
*/
173048173643
static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
173049173644
int rc = SQLITE_OK;
173050173645
pNode->bEof = 0;
173646
+ pNode->bNomatch = 0;
173051173647
173052173648
if( Fts5NodeIsString(pNode) ){
173053173649
/* Initialize all term iterators in the NEAR object. */
173054173650
rc = fts5ExprNearInitAll(pExpr, pNode);
173055173651
}else{
173056173652
int i;
173653
+ int nEof = 0;
173057173654
for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
173655
+ Fts5ExprNode *pChild = pNode->apChild[i];
173058173656
rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
173657
+ assert( pChild->bEof==0 || pChild->bEof==1 );
173658
+ nEof += pChild->bEof;
173059173659
}
173060173660
pNode->iRowid = pNode->apChild[0]->iRowid;
173661
+
173662
+ switch( pNode->eType ){
173663
+ case FTS5_AND:
173664
+ if( nEof>0 ) fts5ExprSetEof(pNode);
173665
+ break;
173666
+
173667
+ case FTS5_OR:
173668
+ if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
173669
+ break;
173670
+
173671
+ default:
173672
+ assert( pNode->eType==FTS5_NOT );
173673
+ pNode->bEof = pNode->apChild[0]->bEof;
173674
+ break;
173675
+ }
173061173676
}
173062173677
173063173678
if( rc==SQLITE_OK ){
173064
- rc = fts5ExprNodeNextMatch(pExpr, pNode);
173679
+ rc = fts5ExprNodeTest(pExpr, pNode);
173065173680
}
173066173681
return rc;
173067173682
}
173068173683
173069173684
@@ -173083,11 +173698,11 @@
173083173698
** is not considered an error if the query does not match any documents.
173084173699
*/
173085173700
static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
173086173701
Fts5ExprNode *pRoot = p->pRoot;
173087173702
int rc = SQLITE_OK;
173088
- if( pRoot ){
173703
+ if( pRoot->xNext ){
173089173704
p->pIndex = pIdx;
173090173705
p->bDesc = bDesc;
173091173706
rc = fts5ExprNodeFirst(p, pRoot);
173092173707
173093173708
/* If not at EOF but the current rowid occurs earlier than iFirst in
@@ -173095,11 +173710,12 @@
173095173710
if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){
173096173711
rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
173097173712
}
173098173713
173099173714
/* If the iterator is not at a real match, skip forward until it is. */
173100
- while( pRoot->bNomatch && rc==SQLITE_OK && pRoot->bEof==0 ){
173715
+ while( pRoot->bNomatch ){
173716
+ assert( pRoot->bEof==0 && rc==SQLITE_OK );
173101173717
rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173102173718
}
173103173719
}
173104173720
return rc;
173105173721
}
@@ -173111,21 +173727,23 @@
173111173727
** is not considered an error if the query does not match any documents.
173112173728
*/
173113173729
static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
173114173730
int rc;
173115173731
Fts5ExprNode *pRoot = p->pRoot;
173732
+ assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
173116173733
do {
173117173734
rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173118
- }while( pRoot->bNomatch && pRoot->bEof==0 && rc==SQLITE_OK );
173735
+ assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
173736
+ }while( pRoot->bNomatch );
173119173737
if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
173120173738
pRoot->bEof = 1;
173121173739
}
173122173740
return rc;
173123173741
}
173124173742
173125173743
static int sqlite3Fts5ExprEof(Fts5Expr *p){
173126
- return (p->pRoot==0 || p->pRoot->bEof);
173744
+ return p->pRoot->bEof;
173127173745
}
173128173746
173129173747
static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
173130173748
return p->pRoot->iRowid;
173131173749
}
@@ -173146,14 +173764,14 @@
173146173764
Fts5ExprTerm *pSyn;
173147173765
Fts5ExprTerm *pNext;
173148173766
Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
173149173767
sqlite3_free(pTerm->zTerm);
173150173768
sqlite3Fts5IterClose(pTerm->pIter);
173151
-
173152173769
for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
173153173770
pNext = pSyn->pSynonym;
173154173771
sqlite3Fts5IterClose(pSyn->pIter);
173772
+ fts5BufferFree((Fts5Buffer*)&pSyn[1]);
173155173773
sqlite3_free(pSyn);
173156173774
}
173157173775
}
173158173776
if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
173159173777
sqlite3_free(pPhrase);
@@ -173230,24 +173848,26 @@
173230173848
){
173231173849
int rc = SQLITE_OK;
173232173850
const int SZALLOC = 8;
173233173851
TokenCtx *pCtx = (TokenCtx*)pContext;
173234173852
Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
173853
+
173854
+ UNUSED_PARAM2(iUnused1, iUnused2);
173235173855
173236173856
/* If an error has already occurred, this is a no-op */
173237173857
if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
173238173858
173239173859
assert( pPhrase==0 || pPhrase->nTerm>0 );
173240173860
if( pPhrase && (tflags & FTS5_TOKEN_COLOCATED) ){
173241173861
Fts5ExprTerm *pSyn;
173242
- int nByte = sizeof(Fts5ExprTerm) + nToken+1;
173862
+ int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
173243173863
pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
173244173864
if( pSyn==0 ){
173245173865
rc = SQLITE_NOMEM;
173246173866
}else{
173247173867
memset(pSyn, 0, nByte);
173248
- pSyn->zTerm = (char*)&pSyn[1];
173868
+ pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
173249173869
memcpy(pSyn->zTerm, pToken, nToken);
173250173870
pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
173251173871
pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
173252173872
}
173253173873
}else{
@@ -173366,26 +173986,21 @@
173366173986
/*
173367173987
** Create a new FTS5 expression by cloning phrase iPhrase of the
173368173988
** expression passed as the second argument.
173369173989
*/
173370173990
static int sqlite3Fts5ExprClonePhrase(
173371
- Fts5Config *pConfig,
173372173991
Fts5Expr *pExpr,
173373173992
int iPhrase,
173374173993
Fts5Expr **ppNew
173375173994
){
173376173995
int rc = SQLITE_OK; /* Return code */
173377173996
Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
173378173997
int i; /* Used to iterate through phrase terms */
173379
-
173380173998
Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
173381
-
173382173999
TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
173383174000
173384
-
173385174001
pOrig = pExpr->apExprPhrase[iPhrase];
173386
-
173387174002
pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
173388174003
if( rc==SQLITE_OK ){
173389174004
pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
173390174005
sizeof(Fts5ExprPhrase*));
173391174006
}
@@ -173422,12 +174037,14 @@
173422174037
pNew->pRoot->pNear->nPhrase = 1;
173423174038
sCtx.pPhrase->pNode = pNew->pRoot;
173424174039
173425174040
if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){
173426174041
pNew->pRoot->eType = FTS5_TERM;
174042
+ pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
173427174043
}else{
173428174044
pNew->pRoot->eType = FTS5_STRING;
174045
+ pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
173429174046
}
173430174047
}else{
173431174048
sqlite3Fts5ExprFree(pNew);
173432174049
fts5ExprPhraseFree(sCtx.pPhrase);
173433174050
pNew = 0;
@@ -173570,10 +174187,42 @@
173570174187
pNear->pColset = pColset;
173571174188
}else{
173572174189
sqlite3_free(pColset);
173573174190
}
173574174191
}
174192
+
174193
+static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
174194
+ switch( pNode->eType ){
174195
+ case FTS5_STRING: {
174196
+ Fts5ExprNearset *pNear = pNode->pNear;
174197
+ if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
174198
+ && pNear->apPhrase[0]->aTerm[0].pSynonym==0
174199
+ ){
174200
+ pNode->eType = FTS5_TERM;
174201
+ pNode->xNext = fts5ExprNodeNext_TERM;
174202
+ }else{
174203
+ pNode->xNext = fts5ExprNodeNext_STRING;
174204
+ }
174205
+ break;
174206
+ };
174207
+
174208
+ case FTS5_OR: {
174209
+ pNode->xNext = fts5ExprNodeNext_OR;
174210
+ break;
174211
+ };
174212
+
174213
+ case FTS5_AND: {
174214
+ pNode->xNext = fts5ExprNodeNext_AND;
174215
+ break;
174216
+ };
174217
+
174218
+ default: assert( pNode->eType==FTS5_NOT ); {
174219
+ pNode->xNext = fts5ExprNodeNext_NOT;
174220
+ break;
174221
+ };
174222
+ }
174223
+}
173575174224
173576174225
static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
173577174226
if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
173578174227
int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
173579174228
memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
@@ -173620,20 +174269,20 @@
173620174269
pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
173621174270
173622174271
if( pRet ){
173623174272
pRet->eType = eType;
173624174273
pRet->pNear = pNear;
174274
+ fts5ExprAssignXNext(pRet);
173625174275
if( eType==FTS5_STRING ){
173626174276
int iPhrase;
173627174277
for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
173628174278
pNear->apPhrase[iPhrase]->pNode = pRet;
173629174279
}
173630
- if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 ){
173631
- if( pNear->apPhrase[0]->aTerm[0].pSynonym==0 ){
173632
- pRet->eType = FTS5_TERM;
173633
- }
173634
- }else if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
174280
+
174281
+ if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL
174282
+ && (pNear->nPhrase!=1 || pNear->apPhrase[0]->nTerm!=1)
174283
+ ){
173635174284
assert( pParse->rc==SQLITE_OK );
173636174285
pParse->rc = SQLITE_ERROR;
173637174286
assert( pParse->zErr==0 );
173638174287
pParse->zErr = sqlite3_mprintf(
173639174288
"fts5: %s queries are not supported (detail!=full)",
@@ -173640,10 +174289,11 @@
173640174289
pNear->nPhrase==1 ? "phrase": "NEAR"
173641174290
);
173642174291
sqlite3_free(pRet);
173643174292
pRet = 0;
173644174293
}
174294
+
173645174295
}else{
173646174296
fts5ExprAddChildren(pRet, pLeft);
173647174297
fts5ExprAddChildren(pRet, pRight);
173648174298
}
173649174299
}
@@ -173922,11 +174572,11 @@
173922174572
if( rc==SQLITE_OK ){
173923174573
rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr);
173924174574
}
173925174575
if( rc==SQLITE_OK ){
173926174576
char *zText;
173927
- if( pExpr->pRoot==0 ){
174577
+ if( pExpr->pRoot->xNext==0 ){
173928174578
zText = sqlite3_mprintf("");
173929174579
}else if( bTcl ){
173930174580
zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
173931174581
}else{
173932174582
zText = fts5ExprPrint(pConfig, pExpr->pRoot);
@@ -174022,11 +174672,11 @@
174022174672
};
174023174673
int i;
174024174674
int rc = SQLITE_OK;
174025174675
void *pCtx = (void*)pGlobal;
174026174676
174027
- for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aFunc); i++){
174677
+ for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
174028174678
struct Fts5ExprFunc *p = &aFunc[i];
174029174679
rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
174030174680
}
174031174681
174032174682
/* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */
@@ -174119,16 +174769,18 @@
174119174769
static int fts5ExprPopulatePoslistsCb(
174120174770
void *pCtx, /* Copy of 2nd argument to xTokenize() */
174121174771
int tflags, /* Mask of FTS5_TOKEN_* flags */
174122174772
const char *pToken, /* Pointer to buffer containing token */
174123174773
int nToken, /* Size of token in bytes */
174124
- int iStart, /* Byte offset of token within input text */
174125
- int iEnd /* Byte offset of end of token within input text */
174774
+ int iUnused1, /* Byte offset of token within input text */
174775
+ int iUnused2 /* Byte offset of end of token within input text */
174126174776
){
174127174777
Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
174128174778
Fts5Expr *pExpr = p->pExpr;
174129174779
int i;
174780
+
174781
+ UNUSED_PARAM2(iUnused1, iUnused2);
174130174782
174131174783
if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
174132174784
for(i=0; i<pExpr->nPhrase; i++){
174133174785
Fts5ExprTerm *pTerm;
174134174786
if( p->aPopulator[i].bOk==0 ) continue;
@@ -174260,30 +174912,25 @@
174260174912
Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
174261174913
Fts5ExprNode *pNode = pPhrase->pNode;
174262174914
int rc = SQLITE_OK;
174263174915
174264174916
assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
174917
+ assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
174918
+
174265174919
if( pNode->bEof==0
174266174920
&& pNode->iRowid==pExpr->pRoot->iRowid
174267174921
&& pPhrase->poslist.n>0
174268174922
){
174269174923
Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
174270174924
if( pTerm->pSynonym ){
174271
- int bDel = 0;
174272
- u8 *a;
174925
+ Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
174273174926
rc = fts5ExprSynonymList(
174274
- pTerm, 1, 0, pNode->iRowid, &bDel, &a, pnCollist
174927
+ pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
174275174928
);
174276
- if( bDel ){
174277
- sqlite3Fts5BufferSet(&rc, &pPhrase->poslist, *pnCollist, a);
174278
- *ppCollist = pPhrase->poslist.p;
174279
- sqlite3_free(a);
174280
- }else{
174281
- *ppCollist = a;
174282
- }
174283
- }else{
174284
- sqlite3Fts5IterCollist(pPhrase->aTerm[0].pIter, ppCollist, pnCollist);
174929
+ }else{
174930
+ *ppCollist = pPhrase->aTerm[0].pIter->pData;
174931
+ *pnCollist = pPhrase->aTerm[0].pIter->nData;
174285174932
}
174286174933
}else{
174287174934
*ppCollist = 0;
174288174935
*pnCollist = 0;
174289174936
}
@@ -174354,14 +175001,14 @@
174354175001
Fts5HashEntry *pScanNext; /* Next entry in sorted order */
174355175002
174356175003
int nAlloc; /* Total size of allocation */
174357175004
int iSzPoslist; /* Offset of space for 4-byte poslist size */
174358175005
int nData; /* Total bytes of data (incl. structure) */
175006
+ int nKey; /* Length of zKey[] in bytes */
174359175007
u8 bDel; /* Set delete-flag @ iSzPoslist */
174360175008
u8 bContent; /* Set content-flag (detail=none mode) */
174361
-
174362
- int iCol; /* Column of last value written */
175009
+ i16 iCol; /* Column of last value written */
174363175010
int iPos; /* Position of last value written */
174364175011
i64 iRowid; /* Rowid of last value written */
174365175012
char zKey[8]; /* Nul-terminated entry key */
174366175013
};
174367175014
@@ -174537,12 +175184,12 @@
174537175184
174538175185
/* Attempt to locate an existing hash entry */
174539175186
iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
174540175187
for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
174541175188
if( p->zKey[0]==bByte
175189
+ && p->nKey==nToken
174542175190
&& memcmp(&p->zKey[1], pToken, nToken)==0
174543
- && p->zKey[nToken+1]==0
174544175191
){
174545175192
break;
174546175193
}
174547175194
}
174548175195
@@ -174565,10 +175212,11 @@
174565175212
memset(p, 0, FTS5_HASHENTRYSIZE);
174566175213
p->nAlloc = nByte;
174567175214
p->zKey[0] = bByte;
174568175215
memcpy(&p->zKey[1], pToken, nToken);
174569175216
assert( iHash==fts5HashKey(pHash->nSlot, (u8*)p->zKey, nToken+1) );
175217
+ p->nKey = nToken;
174570175218
p->zKey[nToken+1] = '\0';
174571175219
p->nData = nToken+1 + 1 + FTS5_HASHENTRYSIZE;
174572175220
p->pHashNext = pHash->aSlot[iHash];
174573175221
pHash->aSlot[iHash] = p;
174574175222
pHash->nEntry++;
@@ -175080,10 +175728,11 @@
175080175728
175081175729
typedef struct Fts5Data Fts5Data;
175082175730
typedef struct Fts5DlidxIter Fts5DlidxIter;
175083175731
typedef struct Fts5DlidxLvl Fts5DlidxLvl;
175084175732
typedef struct Fts5DlidxWriter Fts5DlidxWriter;
175733
+typedef struct Fts5Iter Fts5Iter;
175085175734
typedef struct Fts5PageWriter Fts5PageWriter;
175086175735
typedef struct Fts5SegIter Fts5SegIter;
175087175736
typedef struct Fts5DoclistIter Fts5DoclistIter;
175088175737
typedef struct Fts5SegWriter Fts5SegWriter;
175089175738
typedef struct Fts5Structure Fts5Structure;
@@ -175322,20 +175971,24 @@
175322175971
**
175323175972
** poslist:
175324175973
** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
175325175974
** There is no way to tell if this is populated or not.
175326175975
*/
175327
-struct Fts5IndexIter {
175976
+struct Fts5Iter {
175977
+ Fts5IndexIter base; /* Base class containing output vars */
175978
+
175328175979
Fts5Index *pIndex; /* Index that owns this iterator */
175329175980
Fts5Structure *pStruct; /* Database structure for this iterator */
175330175981
Fts5Buffer poslist; /* Buffer containing current poslist */
175982
+ Fts5Colset *pColset; /* Restrict matches to these columns */
175983
+
175984
+ /* Invoked to set output variables. */
175985
+ void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
175331175986
175332175987
int nSeg; /* Size of aSeg[] array */
175333175988
int bRev; /* True to iterate in reverse order */
175334175989
u8 bSkipEmpty; /* True to skip deleted entries */
175335
- u8 bEof; /* True at EOF */
175336
- u8 bFiltered; /* True if column-filter already applied */
175337175990
175338175991
i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
175339175992
Fts5CResult *aFirst; /* Current merge state (see above) */
175340175993
Fts5SegIter aSeg[1]; /* Array of segment iterators */
175341175994
};
@@ -175421,21 +176074,10 @@
175421176074
int nCmp = MIN(pLeft->n, pRight->n);
175422176075
int res = memcmp(pLeft->p, pRight->p, nCmp);
175423176076
return (res==0 ? (pLeft->n - pRight->n) : res);
175424176077
}
175425176078
175426
-#ifdef SQLITE_DEBUG
175427
-static int fts5BlobCompare(
175428
- const u8 *pLeft, int nLeft,
175429
- const u8 *pRight, int nRight
175430
-){
175431
- int nCmp = MIN(nLeft, nRight);
175432
- int res = memcmp(pLeft, pRight, nCmp);
175433
- return (res==0 ? (nLeft - nRight) : res);
175434
-}
175435
-#endif
175436
-
175437176079
static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
175438176080
int ret;
175439176081
fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
175440176082
return ret;
175441176083
}
@@ -175693,28 +176335,37 @@
175693176335
for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
175694176336
Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
175695176337
int nTotal;
175696176338
int iSeg;
175697176339
175698
- i += fts5GetVarint32(&pData[i], pLvl->nMerge);
175699
- i += fts5GetVarint32(&pData[i], nTotal);
175700
- assert( nTotal>=pLvl->nMerge );
175701
- pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
175702
- nTotal * sizeof(Fts5StructureSegment)
175703
- );
176340
+ if( i>=nData ){
176341
+ rc = FTS5_CORRUPT;
176342
+ }else{
176343
+ i += fts5GetVarint32(&pData[i], pLvl->nMerge);
176344
+ i += fts5GetVarint32(&pData[i], nTotal);
176345
+ assert( nTotal>=pLvl->nMerge );
176346
+ pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
176347
+ nTotal * sizeof(Fts5StructureSegment)
176348
+ );
176349
+ }
175704176350
175705176351
if( rc==SQLITE_OK ){
175706176352
pLvl->nSeg = nTotal;
175707176353
for(iSeg=0; iSeg<nTotal; iSeg++){
176354
+ if( i>=nData ){
176355
+ rc = FTS5_CORRUPT;
176356
+ break;
176357
+ }
175708176358
i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid);
175709176359
i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst);
175710176360
i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast);
175711176361
}
175712
- }else{
175713
- fts5StructureRelease(pRet);
175714
- pRet = 0;
175715176362
}
176363
+ }
176364
+ if( rc!=SQLITE_OK ){
176365
+ fts5StructureRelease(pRet);
176366
+ pRet = 0;
175716176367
}
175717176368
}
175718176369
175719176370
*ppOut = pRet;
175720176371
return rc;
@@ -176378,10 +177029,14 @@
176378177029
u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
176379177030
int iOff = pIter->iLeafOffset; /* Offset to read at */
176380177031
int nNew; /* Bytes of new data */
176381177032
176382177033
iOff += fts5GetVarint32(&a[iOff], nNew);
177034
+ if( iOff+nNew>pIter->pLeaf->nn ){
177035
+ p->rc = FTS5_CORRUPT;
177036
+ return;
177037
+ }
176383177038
pIter->term.n = nKeep;
176384177039
fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
176385177040
iOff += nNew;
176386177041
pIter->iTermLeafOffset = iOff;
176387177042
pIter->iTermLeafPgno = pIter->iLeafPgno;
@@ -176571,11 +177226,11 @@
176571177226
/*
176572177227
** Return true if the iterator passed as the second argument currently
176573177228
** points to a delete marker. A delete marker is an entry with a 0 byte
176574177229
** position-list.
176575177230
*/
176576
-static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5IndexIter *pIter){
177231
+static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
176577177232
Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
176578177233
return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
176579177234
}
176580177235
176581177236
/*
@@ -176584,14 +177239,16 @@
176584177239
** This version of fts5SegIterNext() is only used by reverse iterators.
176585177240
*/
176586177241
static void fts5SegIterNext_Reverse(
176587177242
Fts5Index *p, /* FTS5 backend object */
176588177243
Fts5SegIter *pIter, /* Iterator to advance */
176589
- int *pbNewTerm /* OUT: Set for new term */
177244
+ int *pbUnused /* Unused */
176590177245
){
176591177246
assert( pIter->flags & FTS5_SEGITER_REVERSE );
176592177247
assert( pIter->pNextLeaf==0 );
177248
+ UNUSED_PARAM(pbUnused);
177249
+
176593177250
if( pIter->iRowidOffset>0 ){
176594177251
u8 *a = pIter->pLeaf->p;
176595177252
int iOff;
176596177253
i64 iDelta;
176597177254
@@ -176843,13 +177500,10 @@
176843177500
iPoslist = pIter->iTermLeafOffset;
176844177501
}else{
176845177502
iPoslist = 4;
176846177503
}
176847177504
fts5IndexSkipVarint(pLeaf->p, iPoslist);
176848
- assert( p->pConfig->eDetail==FTS5_DETAIL_NONE || iPoslist==(
176849
- pIter->iLeafOffset - sqlite3Fts5GetVarintLen(pIter->nPos*2+pIter->bDel)
176850
- ));
176851177505
pIter->iLeafOffset = iPoslist;
176852177506
176853177507
/* If this condition is true then the largest rowid for the current
176854177508
** term may not be stored on the current page. So search forward to
176855177509
** see where said rowid really is. */
@@ -177067,23 +177721,19 @@
177067177721
** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
177068177722
** an error has already occurred when this function is called, it is a no-op.
177069177723
*/
177070177724
static void fts5SegIterSeekInit(
177071177725
Fts5Index *p, /* FTS5 backend */
177072
- Fts5Buffer *pBuf, /* Buffer to use for loading pages */
177073177726
const u8 *pTerm, int nTerm, /* Term to seek to */
177074177727
int flags, /* Mask of FTS5INDEX_XXX flags */
177075177728
Fts5StructureSegment *pSeg, /* Description of segment */
177076177729
Fts5SegIter *pIter /* Object to populate */
177077177730
){
177078177731
int iPg = 1;
177079177732
int bGe = (flags & FTS5INDEX_QUERY_SCAN);
177080177733
int bDlidx = 0; /* True if there is a doclist-index */
177081177734
177082
- static int nCall = 0;
177083
- nCall++;
177084
-
177085177735
assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
177086177736
assert( pTerm && nTerm );
177087177737
memset(pIter, 0, sizeof(*pIter));
177088177738
pIter->pSeg = pSeg;
177089177739
@@ -177225,11 +177875,11 @@
177225177875
** fts5AssertMultiIterSetup(). It ensures that the result currently stored
177226177876
** in *pRes is the correct result of comparing the current positions of the
177227177877
** two iterators.
177228177878
*/
177229177879
static void fts5AssertComparisonResult(
177230
- Fts5IndexIter *pIter,
177880
+ Fts5Iter *pIter,
177231177881
Fts5SegIter *p1,
177232177882
Fts5SegIter *p2,
177233177883
Fts5CResult *pRes
177234177884
){
177235177885
int i1 = p1 - pIter->aSeg;
@@ -177266,16 +177916,16 @@
177266177916
** This function is a no-op unless SQLITE_DEBUG is defined when this module
177267177917
** is compiled. In that case, this function is essentially an assert()
177268177918
** statement used to verify that the contents of the pIter->aFirst[] array
177269177919
** are correct.
177270177920
*/
177271
-static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5IndexIter *pIter){
177921
+static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
177272177922
if( p->rc==SQLITE_OK ){
177273177923
Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177274177924
int i;
177275177925
177276
- assert( (pFirst->pLeaf==0)==pIter->bEof );
177926
+ assert( (pFirst->pLeaf==0)==pIter->base.bEof );
177277177927
177278177928
/* Check that pIter->iSwitchRowid is set correctly. */
177279177929
for(i=0; i<pIter->nSeg; i++){
177280177930
Fts5SegIter *p1 = &pIter->aSeg[i];
177281177931
assert( p1==pFirst
@@ -177311,11 +177961,11 @@
177311177961
** If the returned value is non-zero, then it is the index of an entry
177312177962
** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
177313177963
** to a key that is a duplicate of another, higher priority,
177314177964
** segment-iterator in the pSeg->aSeg[] array.
177315177965
*/
177316
-static int fts5MultiIterDoCompare(Fts5IndexIter *pIter, int iOut){
177966
+static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
177317177967
int i1; /* Index of left-hand Fts5SegIter */
177318177968
int i2; /* Index of right-hand Fts5SegIter */
177319177969
int iRes;
177320177970
Fts5SegIter *p1; /* Left-hand Fts5SegIter */
177321177971
Fts5SegIter *p2; /* Right-hand Fts5SegIter */
@@ -177457,11 +178107,11 @@
177457178107
177458178108
177459178109
/*
177460178110
** Free the iterator object passed as the second argument.
177461178111
*/
177462
-static void fts5MultiIterFree(Fts5Index *p, Fts5IndexIter *pIter){
178112
+static void fts5MultiIterFree(Fts5Iter *pIter){
177463178113
if( pIter ){
177464178114
int i;
177465178115
for(i=0; i<pIter->nSeg; i++){
177466178116
fts5SegIterClear(&pIter->aSeg[i]);
177467178117
}
@@ -177471,11 +178121,11 @@
177471178121
}
177472178122
}
177473178123
177474178124
static void fts5MultiIterAdvanced(
177475178125
Fts5Index *p, /* FTS5 backend to iterate within */
177476
- Fts5IndexIter *pIter, /* Iterator to update aFirst[] array for */
178126
+ Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
177477178127
int iChanged, /* Index of sub-iterator just advanced */
177478178128
int iMinset /* Minimum entry in aFirst[] to set */
177479178129
){
177480178130
int i;
177481178131
for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
@@ -177498,13 +178148,13 @@
177498178148
** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
177499178149
** on the iterator instead. That function does the same as this one, except
177500178150
** that it deals with more complicated cases as well.
177501178151
*/
177502178152
static int fts5MultiIterAdvanceRowid(
177503
- Fts5Index *p, /* FTS5 backend to iterate within */
177504
- Fts5IndexIter *pIter, /* Iterator to update aFirst[] array for */
177505
- int iChanged /* Index of sub-iterator just advanced */
178153
+ Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
178154
+ int iChanged, /* Index of sub-iterator just advanced */
178155
+ Fts5SegIter **ppFirst
177506178156
){
177507178157
Fts5SegIter *pNew = &pIter->aSeg[iChanged];
177508178158
177509178159
if( pNew->iRowid==pIter->iSwitchRowid
177510178160
|| (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
@@ -177533,19 +178183,20 @@
177533178183
177534178184
pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
177535178185
}
177536178186
}
177537178187
178188
+ *ppFirst = pNew;
177538178189
return 0;
177539178190
}
177540178191
177541178192
/*
177542178193
** Set the pIter->bEof variable based on the state of the sub-iterators.
177543178194
*/
177544
-static void fts5MultiIterSetEof(Fts5IndexIter *pIter){
178195
+static void fts5MultiIterSetEof(Fts5Iter *pIter){
177545178196
Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177546
- pIter->bEof = pSeg->pLeaf==0;
178197
+ pIter->base.bEof = pSeg->pLeaf==0;
177547178198
pIter->iSwitchRowid = pSeg->iRowid;
177548178199
}
177549178200
177550178201
/*
177551178202
** Move the iterator to the next entry.
@@ -177554,43 +178205,48 @@
177554178205
** considered an error if the iterator reaches EOF, or if it is already at
177555178206
** EOF when this function is called.
177556178207
*/
177557178208
static void fts5MultiIterNext(
177558178209
Fts5Index *p,
177559
- Fts5IndexIter *pIter,
178210
+ Fts5Iter *pIter,
177560178211
int bFrom, /* True if argument iFrom is valid */
177561178212
i64 iFrom /* Advance at least as far as this */
177562178213
){
177563
- if( p->rc==SQLITE_OK ){
177564
- int bUseFrom = bFrom;
177565
- do {
177566
- int iFirst = pIter->aFirst[1].iFirst;
177567
- int bNewTerm = 0;
177568
- Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
177569
- assert( p->rc==SQLITE_OK );
177570
- if( bUseFrom && pSeg->pDlidx ){
177571
- fts5SegIterNextFrom(p, pSeg, iFrom);
177572
- }else{
177573
- pSeg->xNext(p, pSeg, &bNewTerm);
177574
- }
177575
-
177576
- if( pSeg->pLeaf==0 || bNewTerm
177577
- || fts5MultiIterAdvanceRowid(p, pIter, iFirst)
177578
- ){
177579
- fts5MultiIterAdvanced(p, pIter, iFirst, 1);
177580
- fts5MultiIterSetEof(pIter);
177581
- }
177582
- fts5AssertMultiIterSetup(p, pIter);
177583
-
177584
- bUseFrom = 0;
177585
- }while( pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter) );
178214
+ int bUseFrom = bFrom;
178215
+ while( p->rc==SQLITE_OK ){
178216
+ int iFirst = pIter->aFirst[1].iFirst;
178217
+ int bNewTerm = 0;
178218
+ Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
178219
+ assert( p->rc==SQLITE_OK );
178220
+ if( bUseFrom && pSeg->pDlidx ){
178221
+ fts5SegIterNextFrom(p, pSeg, iFrom);
178222
+ }else{
178223
+ pSeg->xNext(p, pSeg, &bNewTerm);
178224
+ }
178225
+
178226
+ if( pSeg->pLeaf==0 || bNewTerm
178227
+ || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
178228
+ ){
178229
+ fts5MultiIterAdvanced(p, pIter, iFirst, 1);
178230
+ fts5MultiIterSetEof(pIter);
178231
+ pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
178232
+ if( pSeg->pLeaf==0 ) return;
178233
+ }
178234
+
178235
+ fts5AssertMultiIterSetup(p, pIter);
178236
+ assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
178237
+ if( pIter->bSkipEmpty==0 || pSeg->nPos ){
178238
+ pIter->xSetOutputs(pIter, pSeg);
178239
+ return;
178240
+ }
178241
+ bUseFrom = 0;
177586178242
}
177587178243
}
177588178244
177589178245
static void fts5MultiIterNext2(
177590178246
Fts5Index *p,
177591
- Fts5IndexIter *pIter,
178247
+ Fts5Iter *pIter,
177592178248
int *pbNewTerm /* OUT: True if *might* be new term */
177593178249
){
177594178250
assert( pIter->bSkipEmpty );
177595178251
if( p->rc==SQLITE_OK ){
177596178252
do {
@@ -177599,11 +178255,11 @@
177599178255
int bNewTerm = 0;
177600178256
177601178257
assert( p->rc==SQLITE_OK );
177602178258
pSeg->xNext(p, pSeg, &bNewTerm);
177603178259
if( pSeg->pLeaf==0 || bNewTerm
177604
- || fts5MultiIterAdvanceRowid(p, pIter, iFirst)
178260
+ || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
177605178261
){
177606178262
fts5MultiIterAdvanced(p, pIter, iFirst, 1);
177607178263
fts5MultiIterSetEof(pIter);
177608178264
*pbNewTerm = 1;
177609178265
}else{
@@ -177613,220 +178269,147 @@
177613178269
177614178270
}while( fts5MultiIterIsEmpty(p, pIter) );
177615178271
}
177616178272
}
177617178273
178274
+static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
178275
+ UNUSED_PARAM2(pUnused1, pUnused2);
178276
+}
177618178277
177619
-static Fts5IndexIter *fts5MultiIterAlloc(
178278
+static Fts5Iter *fts5MultiIterAlloc(
177620178279
Fts5Index *p, /* FTS5 backend to iterate within */
177621178280
int nSeg
177622178281
){
177623
- Fts5IndexIter *pNew;
178282
+ Fts5Iter *pNew;
177624178283
int nSlot; /* Power of two >= nSeg */
177625178284
177626178285
for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
177627178286
pNew = fts5IdxMalloc(p,
177628
- sizeof(Fts5IndexIter) + /* pNew */
178287
+ sizeof(Fts5Iter) + /* pNew */
177629178288
sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
177630178289
sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
177631178290
);
177632178291
if( pNew ){
177633178292
pNew->nSeg = nSlot;
177634178293
pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
177635178294
pNew->pIndex = p;
178295
+ pNew->xSetOutputs = fts5IterSetOutputs_Noop;
177636178296
}
177637178297
return pNew;
177638178298
}
177639178299
177640
-/*
177641
-** Allocate a new Fts5IndexIter object.
177642
-**
177643
-** The new object will be used to iterate through data in structure pStruct.
177644
-** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
177645
-** is zero or greater, data from the first nSegment segments on level iLevel
177646
-** is merged.
177647
-**
177648
-** The iterator initially points to the first term/rowid entry in the
177649
-** iterated data.
177650
-*/
177651
-static void fts5MultiIterNew(
177652
- Fts5Index *p, /* FTS5 backend to iterate within */
177653
- Fts5Structure *pStruct, /* Structure of specific index */
177654
- int bSkipEmpty, /* True to ignore delete-keys */
177655
- int flags, /* FTS5INDEX_QUERY_XXX flags */
177656
- const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
177657
- int iLevel, /* Level to iterate (-1 for all) */
177658
- int nSegment, /* Number of segments to merge (iLevel>=0) */
177659
- Fts5IndexIter **ppOut /* New object */
177660
-){
177661
- int nSeg = 0; /* Number of segment-iters in use */
177662
- int iIter = 0; /* */
177663
- int iSeg; /* Used to iterate through segments */
177664
- Fts5Buffer buf = {0,0,0}; /* Buffer used by fts5SegIterSeekInit() */
177665
- Fts5StructureLevel *pLvl;
177666
- Fts5IndexIter *pNew;
177667
-
177668
- assert( (pTerm==0 && nTerm==0) || iLevel<0 );
177669
-
177670
- /* Allocate space for the new multi-seg-iterator. */
177671
- if( p->rc==SQLITE_OK ){
177672
- if( iLevel<0 ){
177673
- assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
177674
- nSeg = pStruct->nSegment;
177675
- nSeg += (p->pHash ? 1 : 0);
177676
- }else{
177677
- nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
177678
- }
177679
- }
177680
- *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
177681
- if( pNew==0 ) return;
177682
- pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
177683
- pNew->bSkipEmpty = (u8)bSkipEmpty;
177684
- pNew->pStruct = pStruct;
177685
- fts5StructureRef(pStruct);
177686
-
177687
- /* Initialize each of the component segment iterators. */
177688
- if( iLevel<0 ){
177689
- Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
177690
- if( p->pHash ){
177691
- /* Add a segment iterator for the current contents of the hash table. */
177692
- Fts5SegIter *pIter = &pNew->aSeg[iIter++];
177693
- fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
177694
- }
177695
- for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
177696
- for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
177697
- Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
177698
- Fts5SegIter *pIter = &pNew->aSeg[iIter++];
177699
- if( pTerm==0 ){
177700
- fts5SegIterInit(p, pSeg, pIter);
177701
- }else{
177702
- fts5SegIterSeekInit(p, &buf, pTerm, nTerm, flags, pSeg, pIter);
177703
- }
177704
- }
177705
- }
177706
- }else{
177707
- pLvl = &pStruct->aLevel[iLevel];
177708
- for(iSeg=nSeg-1; iSeg>=0; iSeg--){
177709
- fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
177710
- }
177711
- }
177712
- assert( iIter==nSeg );
177713
-
177714
- /* If the above was successful, each component iterators now points
177715
- ** to the first entry in its segment. In this case initialize the
177716
- ** aFirst[] array. Or, if an error has occurred, free the iterator
177717
- ** object and set the output variable to NULL. */
177718
- if( p->rc==SQLITE_OK ){
177719
- for(iIter=pNew->nSeg-1; iIter>0; iIter--){
177720
- int iEq;
177721
- if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
177722
- Fts5SegIter *pSeg = &pNew->aSeg[iEq];
177723
- if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
177724
- fts5MultiIterAdvanced(p, pNew, iEq, iIter);
177725
- }
177726
- }
177727
- fts5MultiIterSetEof(pNew);
177728
- fts5AssertMultiIterSetup(p, pNew);
177729
-
177730
- if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
177731
- fts5MultiIterNext(p, pNew, 0, 0);
177732
- }
177733
- }else{
177734
- fts5MultiIterFree(p, pNew);
177735
- *ppOut = 0;
177736
- }
177737
- fts5BufferFree(&buf);
177738
-}
177739
-
177740
-/*
177741
-** Create an Fts5IndexIter that iterates through the doclist provided
177742
-** as the second argument.
177743
-*/
177744
-static void fts5MultiIterNew2(
177745
- Fts5Index *p, /* FTS5 backend to iterate within */
177746
- Fts5Data *pData, /* Doclist to iterate through */
177747
- int bDesc, /* True for descending rowid order */
177748
- Fts5IndexIter **ppOut /* New object */
177749
-){
177750
- Fts5IndexIter *pNew;
177751
- pNew = fts5MultiIterAlloc(p, 2);
177752
- if( pNew ){
177753
- Fts5SegIter *pIter = &pNew->aSeg[1];
177754
-
177755
- pNew->bFiltered = 1;
177756
- pIter->flags = FTS5_SEGITER_ONETERM;
177757
- if( pData->szLeaf>0 ){
177758
- pIter->pLeaf = pData;
177759
- pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
177760
- pIter->iEndofDoclist = pData->nn;
177761
- pNew->aFirst[1].iFirst = 1;
177762
- if( bDesc ){
177763
- pNew->bRev = 1;
177764
- pIter->flags |= FTS5_SEGITER_REVERSE;
177765
- fts5SegIterReverseInitPage(p, pIter);
177766
- }else{
177767
- fts5SegIterLoadNPos(p, pIter);
177768
- }
177769
- pData = 0;
177770
- }else{
177771
- pNew->bEof = 1;
177772
- }
177773
- fts5SegIterSetNext(p, pIter);
177774
-
177775
- *ppOut = pNew;
177776
- }
177777
-
177778
- fts5DataRelease(pData);
177779
-}
177780
-
177781
-/*
177782
-** Return true if the iterator is at EOF or if an error has occurred.
177783
-** False otherwise.
177784
-*/
177785
-static int fts5MultiIterEof(Fts5Index *p, Fts5IndexIter *pIter){
177786
- assert( p->rc
177787
- || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->bEof
177788
- );
177789
- return (p->rc || pIter->bEof);
177790
-}
177791
-
177792
-/*
177793
-** Return the rowid of the entry that the iterator currently points
177794
-** to. If the iterator points to EOF when this function is called the
177795
-** results are undefined.
177796
-*/
177797
-static i64 fts5MultiIterRowid(Fts5IndexIter *pIter){
177798
- assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
177799
- return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
177800
-}
177801
-
177802
-/*
177803
-** Move the iterator to the next entry at or following iMatch.
177804
-*/
177805
-static void fts5MultiIterNextFrom(
177806
- Fts5Index *p,
177807
- Fts5IndexIter *pIter,
177808
- i64 iMatch
177809
-){
177810
- while( 1 ){
177811
- i64 iRowid;
177812
- fts5MultiIterNext(p, pIter, 1, iMatch);
177813
- if( fts5MultiIterEof(p, pIter) ) break;
177814
- iRowid = fts5MultiIterRowid(pIter);
177815
- if( pIter->bRev==0 && iRowid>=iMatch ) break;
177816
- if( pIter->bRev!=0 && iRowid<=iMatch ) break;
177817
- }
177818
-}
177819
-
177820
-/*
177821
-** Return a pointer to a buffer containing the term associated with the
177822
-** entry that the iterator currently points to.
177823
-*/
177824
-static const u8 *fts5MultiIterTerm(Fts5IndexIter *pIter, int *pn){
177825
- Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177826
- *pn = p->term.n;
177827
- return p->term.p;
178300
+static void fts5PoslistCallback(
178301
+ Fts5Index *pUnused,
178302
+ void *pContext,
178303
+ const u8 *pChunk, int nChunk
178304
+){
178305
+ UNUSED_PARAM(pUnused);
178306
+ assert_nc( nChunk>=0 );
178307
+ if( nChunk>0 ){
178308
+ fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
178309
+ }
178310
+}
178311
+
178312
+typedef struct PoslistCallbackCtx PoslistCallbackCtx;
178313
+struct PoslistCallbackCtx {
178314
+ Fts5Buffer *pBuf; /* Append to this buffer */
178315
+ Fts5Colset *pColset; /* Restrict matches to this column */
178316
+ int eState; /* See above */
178317
+};
178318
+
178319
+typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
178320
+struct PoslistOffsetsCtx {
178321
+ Fts5Buffer *pBuf; /* Append to this buffer */
178322
+ Fts5Colset *pColset; /* Restrict matches to this column */
178323
+ int iRead;
178324
+ int iWrite;
178325
+};
178326
+
178327
+/*
178328
+** TODO: Make this more efficient!
178329
+*/
178330
+static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
178331
+ int i;
178332
+ for(i=0; i<pColset->nCol; i++){
178333
+ if( pColset->aiCol[i]==iCol ) return 1;
178334
+ }
178335
+ return 0;
178336
+}
178337
+
178338
+static void fts5PoslistOffsetsCallback(
178339
+ Fts5Index *pUnused,
178340
+ void *pContext,
178341
+ const u8 *pChunk, int nChunk
178342
+){
178343
+ PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
178344
+ UNUSED_PARAM(pUnused);
178345
+ assert_nc( nChunk>=0 );
178346
+ if( nChunk>0 ){
178347
+ int i = 0;
178348
+ while( i<nChunk ){
178349
+ int iVal;
178350
+ i += fts5GetVarint32(&pChunk[i], iVal);
178351
+ iVal += pCtx->iRead - 2;
178352
+ pCtx->iRead = iVal;
178353
+ if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
178354
+ fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
178355
+ pCtx->iWrite = iVal;
178356
+ }
178357
+ }
178358
+ }
178359
+}
178360
+
178361
+static void fts5PoslistFilterCallback(
178362
+ Fts5Index *pUnused,
178363
+ void *pContext,
178364
+ const u8 *pChunk, int nChunk
178365
+){
178366
+ PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
178367
+ UNUSED_PARAM(pUnused);
178368
+ assert_nc( nChunk>=0 );
178369
+ if( nChunk>0 ){
178370
+ /* Search through to find the first varint with value 1. This is the
178371
+ ** start of the next columns hits. */
178372
+ int i = 0;
178373
+ int iStart = 0;
178374
+
178375
+ if( pCtx->eState==2 ){
178376
+ int iCol;
178377
+ fts5FastGetVarint32(pChunk, i, iCol);
178378
+ if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
178379
+ pCtx->eState = 1;
178380
+ fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
178381
+ }else{
178382
+ pCtx->eState = 0;
178383
+ }
178384
+ }
178385
+
178386
+ do {
178387
+ while( i<nChunk && pChunk[i]!=0x01 ){
178388
+ while( pChunk[i] & 0x80 ) i++;
178389
+ i++;
178390
+ }
178391
+ if( pCtx->eState ){
178392
+ fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
178393
+ }
178394
+ if( i<nChunk ){
178395
+ int iCol;
178396
+ iStart = i;
178397
+ i++;
178398
+ if( i>=nChunk ){
178399
+ pCtx->eState = 2;
178400
+ }else{
178401
+ fts5FastGetVarint32(pChunk, i, iCol);
178402
+ pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
178403
+ if( pCtx->eState ){
178404
+ fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
178405
+ iStart = i;
178406
+ }
178407
+ }
178408
+ }
178409
+ }while( i<nChunk );
178410
+ }
177828178411
}
177829178412
177830178413
static void fts5ChunkIterate(
177831178414
Fts5Index *p, /* Index object */
177832178415
Fts5SegIter *pSeg, /* Poslist of this iterator */
@@ -177866,11 +178449,459 @@
177866178449
}
177867178450
}
177868178451
}
177869178452
}
177870178453
178454
+/*
178455
+** Iterator pIter currently points to a valid entry (not EOF). This
178456
+** function appends the position list data for the current entry to
178457
+** buffer pBuf. It does not make a copy of the position-list size
178458
+** field.
178459
+*/
178460
+static void fts5SegiterPoslist(
178461
+ Fts5Index *p,
178462
+ Fts5SegIter *pSeg,
178463
+ Fts5Colset *pColset,
178464
+ Fts5Buffer *pBuf
178465
+){
178466
+ if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
178467
+ if( pColset==0 ){
178468
+ fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
178469
+ }else{
178470
+ if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
178471
+ PoslistCallbackCtx sCtx;
178472
+ sCtx.pBuf = pBuf;
178473
+ sCtx.pColset = pColset;
178474
+ sCtx.eState = fts5IndexColsetTest(pColset, 0);
178475
+ assert( sCtx.eState==0 || sCtx.eState==1 );
178476
+ fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
178477
+ }else{
178478
+ PoslistOffsetsCtx sCtx;
178479
+ memset(&sCtx, 0, sizeof(sCtx));
178480
+ sCtx.pBuf = pBuf;
178481
+ sCtx.pColset = pColset;
178482
+ fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
178483
+ }
178484
+ }
178485
+ }
178486
+}
177871178487
178488
+/*
178489
+** IN/OUT parameter (*pa) points to a position list n bytes in size. If
178490
+** the position list contains entries for column iCol, then (*pa) is set
178491
+** to point to the sub-position-list for that column and the number of
178492
+** bytes in it returned. Or, if the argument position list does not
178493
+** contain any entries for column iCol, return 0.
178494
+*/
178495
+static int fts5IndexExtractCol(
178496
+ const u8 **pa, /* IN/OUT: Pointer to poslist */
178497
+ int n, /* IN: Size of poslist in bytes */
178498
+ int iCol /* Column to extract from poslist */
178499
+){
178500
+ int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
178501
+ const u8 *p = *pa;
178502
+ const u8 *pEnd = &p[n]; /* One byte past end of position list */
178503
+
178504
+ while( iCol>iCurrent ){
178505
+ /* Advance pointer p until it points to pEnd or an 0x01 byte that is
178506
+ ** not part of a varint. Note that it is not possible for a negative
178507
+ ** or extremely large varint to occur within an uncorrupted position
178508
+ ** list. So the last byte of each varint may be assumed to have a clear
178509
+ ** 0x80 bit. */
178510
+ while( *p!=0x01 ){
178511
+ while( *p++ & 0x80 );
178512
+ if( p>=pEnd ) return 0;
178513
+ }
178514
+ *pa = p++;
178515
+ iCurrent = *p++;
178516
+ if( iCurrent & 0x80 ){
178517
+ p--;
178518
+ p += fts5GetVarint32(p, iCurrent);
178519
+ }
178520
+ }
178521
+ if( iCol!=iCurrent ) return 0;
178522
+
178523
+ /* Advance pointer p until it points to pEnd or an 0x01 byte that is
178524
+ ** not part of a varint */
178525
+ while( p<pEnd && *p!=0x01 ){
178526
+ while( *p++ & 0x80 );
178527
+ }
178528
+
178529
+ return p - (*pa);
178530
+}
178531
+
178532
+static int fts5IndexExtractColset (
178533
+ Fts5Colset *pColset, /* Colset to filter on */
178534
+ const u8 *pPos, int nPos, /* Position list */
178535
+ Fts5Buffer *pBuf /* Output buffer */
178536
+){
178537
+ int rc = SQLITE_OK;
178538
+ int i;
178539
+
178540
+ fts5BufferZero(pBuf);
178541
+ for(i=0; i<pColset->nCol; i++){
178542
+ const u8 *pSub = pPos;
178543
+ int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
178544
+ if( nSub ){
178545
+ fts5BufferAppendBlob(&rc, pBuf, nSub, pSub);
178546
+ }
178547
+ }
178548
+ return rc;
178549
+}
178550
+
178551
+/*
178552
+** xSetOutputs callback used by detail=none tables.
178553
+*/
178554
+static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
178555
+ assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
178556
+ pIter->base.iRowid = pSeg->iRowid;
178557
+ pIter->base.nData = pSeg->nPos;
178558
+}
178559
+
178560
+/*
178561
+** xSetOutputs callback used by detail=full and detail=col tables when no
178562
+** column filters are specified.
178563
+*/
178564
+static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
178565
+ pIter->base.iRowid = pSeg->iRowid;
178566
+ pIter->base.nData = pSeg->nPos;
178567
+
178568
+ assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
178569
+ assert( pIter->pColset==0 );
178570
+
178571
+ if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
178572
+ /* All data is stored on the current page. Populate the output
178573
+ ** variables to point into the body of the page object. */
178574
+ pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
178575
+ }else{
178576
+ /* The data is distributed over two or more pages. Copy it into the
178577
+ ** Fts5Iter.poslist buffer and then set the output pointer to point
178578
+ ** to this buffer. */
178579
+ fts5BufferZero(&pIter->poslist);
178580
+ fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
178581
+ pIter->base.pData = pIter->poslist.p;
178582
+ }
178583
+}
178584
+
178585
+/*
178586
+** xSetOutputs callback used by detail=col when there is a column filter
178587
+** and there are 100 or more columns. Also called as a fallback from
178588
+** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
178589
+*/
178590
+static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
178591
+ fts5BufferZero(&pIter->poslist);
178592
+ fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
178593
+ pIter->base.iRowid = pSeg->iRowid;
178594
+ pIter->base.pData = pIter->poslist.p;
178595
+ pIter->base.nData = pIter->poslist.n;
178596
+}
178597
+
178598
+/*
178599
+** xSetOutputs callback used when:
178600
+**
178601
+** * detail=col,
178602
+** * there is a column filter, and
178603
+** * the table contains 100 or fewer columns.
178604
+**
178605
+** The last point is to ensure all column numbers are stored as
178606
+** single-byte varints.
178607
+*/
178608
+static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
178609
+
178610
+ assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
178611
+ assert( pIter->pColset );
178612
+
178613
+ if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
178614
+ fts5IterSetOutputs_Col(pIter, pSeg);
178615
+ }else{
178616
+ u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
178617
+ u8 *pEnd = (u8*)&a[pSeg->nPos];
178618
+ int iPrev = 0;
178619
+ int *aiCol = pIter->pColset->aiCol;
178620
+ int *aiColEnd = &aiCol[pIter->pColset->nCol];
178621
+
178622
+ u8 *aOut = pIter->poslist.p;
178623
+ int iPrevOut = 0;
178624
+
178625
+ pIter->base.iRowid = pSeg->iRowid;
178626
+
178627
+ while( a<pEnd ){
178628
+ iPrev += (int)a++[0] - 2;
178629
+ while( *aiCol<iPrev ){
178630
+ aiCol++;
178631
+ if( aiCol==aiColEnd ) goto setoutputs_col_out;
178632
+ }
178633
+ if( *aiCol==iPrev ){
178634
+ *aOut++ = (iPrev - iPrevOut) + 2;
178635
+ iPrevOut = iPrev;
178636
+ }
178637
+ }
178638
+
178639
+setoutputs_col_out:
178640
+ pIter->base.pData = pIter->poslist.p;
178641
+ pIter->base.nData = aOut - pIter->poslist.p;
178642
+ }
178643
+}
178644
+
178645
+/*
178646
+** xSetOutputs callback used by detail=full when there is a column filter.
178647
+*/
178648
+static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
178649
+ Fts5Colset *pColset = pIter->pColset;
178650
+ pIter->base.iRowid = pSeg->iRowid;
178651
+
178652
+ assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
178653
+ assert( pColset );
178654
+
178655
+ if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
178656
+ /* All data is stored on the current page. Populate the output
178657
+ ** variables to point into the body of the page object. */
178658
+ const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
178659
+ if( pColset->nCol==1 ){
178660
+ pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
178661
+ pIter->base.pData = a;
178662
+ }else{
178663
+ fts5BufferZero(&pIter->poslist);
178664
+ fts5IndexExtractColset(pColset, a, pSeg->nPos, &pIter->poslist);
178665
+ pIter->base.pData = pIter->poslist.p;
178666
+ pIter->base.nData = pIter->poslist.n;
178667
+ }
178668
+ }else{
178669
+ /* The data is distributed over two or more pages. Copy it into the
178670
+ ** Fts5Iter.poslist buffer and then set the output pointer to point
178671
+ ** to this buffer. */
178672
+ fts5BufferZero(&pIter->poslist);
178673
+ fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
178674
+ pIter->base.pData = pIter->poslist.p;
178675
+ pIter->base.nData = pIter->poslist.n;
178676
+ }
178677
+}
178678
+
178679
+static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
178680
+ if( *pRc==SQLITE_OK ){
178681
+ Fts5Config *pConfig = pIter->pIndex->pConfig;
178682
+ if( pConfig->eDetail==FTS5_DETAIL_NONE ){
178683
+ pIter->xSetOutputs = fts5IterSetOutputs_None;
178684
+ }
178685
+
178686
+ else if( pIter->pColset==0 ){
178687
+ pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
178688
+ }
178689
+
178690
+ else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
178691
+ pIter->xSetOutputs = fts5IterSetOutputs_Full;
178692
+ }
178693
+
178694
+ else{
178695
+ assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
178696
+ if( pConfig->nCol<=100 ){
178697
+ pIter->xSetOutputs = fts5IterSetOutputs_Col100;
178698
+ sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
178699
+ }else{
178700
+ pIter->xSetOutputs = fts5IterSetOutputs_Col;
178701
+ }
178702
+ }
178703
+ }
178704
+}
178705
+
178706
+
178707
+/*
178708
+** Allocate a new Fts5Iter object.
178709
+**
178710
+** The new object will be used to iterate through data in structure pStruct.
178711
+** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
178712
+** is zero or greater, data from the first nSegment segments on level iLevel
178713
+** is merged.
178714
+**
178715
+** The iterator initially points to the first term/rowid entry in the
178716
+** iterated data.
178717
+*/
178718
+static void fts5MultiIterNew(
178719
+ Fts5Index *p, /* FTS5 backend to iterate within */
178720
+ Fts5Structure *pStruct, /* Structure of specific index */
178721
+ int flags, /* FTS5INDEX_QUERY_XXX flags */
178722
+ Fts5Colset *pColset, /* Colset to filter on (or NULL) */
178723
+ const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
178724
+ int iLevel, /* Level to iterate (-1 for all) */
178725
+ int nSegment, /* Number of segments to merge (iLevel>=0) */
178726
+ Fts5Iter **ppOut /* New object */
178727
+){
178728
+ int nSeg = 0; /* Number of segment-iters in use */
178729
+ int iIter = 0; /* */
178730
+ int iSeg; /* Used to iterate through segments */
178731
+ Fts5StructureLevel *pLvl;
178732
+ Fts5Iter *pNew;
178733
+
178734
+ assert( (pTerm==0 && nTerm==0) || iLevel<0 );
178735
+
178736
+ /* Allocate space for the new multi-seg-iterator. */
178737
+ if( p->rc==SQLITE_OK ){
178738
+ if( iLevel<0 ){
178739
+ assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
178740
+ nSeg = pStruct->nSegment;
178741
+ nSeg += (p->pHash ? 1 : 0);
178742
+ }else{
178743
+ nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
178744
+ }
178745
+ }
178746
+ *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
178747
+ if( pNew==0 ) return;
178748
+ pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
178749
+ pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
178750
+ pNew->pStruct = pStruct;
178751
+ pNew->pColset = pColset;
178752
+ fts5StructureRef(pStruct);
178753
+ if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
178754
+ fts5IterSetOutputCb(&p->rc, pNew);
178755
+ }
178756
+
178757
+ /* Initialize each of the component segment iterators. */
178758
+ if( p->rc==SQLITE_OK ){
178759
+ if( iLevel<0 ){
178760
+ Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
178761
+ if( p->pHash ){
178762
+ /* Add a segment iterator for the current contents of the hash table. */
178763
+ Fts5SegIter *pIter = &pNew->aSeg[iIter++];
178764
+ fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
178765
+ }
178766
+ for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
178767
+ for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
178768
+ Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
178769
+ Fts5SegIter *pIter = &pNew->aSeg[iIter++];
178770
+ if( pTerm==0 ){
178771
+ fts5SegIterInit(p, pSeg, pIter);
178772
+ }else{
178773
+ fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
178774
+ }
178775
+ }
178776
+ }
178777
+ }else{
178778
+ pLvl = &pStruct->aLevel[iLevel];
178779
+ for(iSeg=nSeg-1; iSeg>=0; iSeg--){
178780
+ fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
178781
+ }
178782
+ }
178783
+ assert( iIter==nSeg );
178784
+ }
178785
+
178786
+ /* If the above was successful, each component iterators now points
178787
+ ** to the first entry in its segment. In this case initialize the
178788
+ ** aFirst[] array. Or, if an error has occurred, free the iterator
178789
+ ** object and set the output variable to NULL. */
178790
+ if( p->rc==SQLITE_OK ){
178791
+ for(iIter=pNew->nSeg-1; iIter>0; iIter--){
178792
+ int iEq;
178793
+ if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
178794
+ Fts5SegIter *pSeg = &pNew->aSeg[iEq];
178795
+ if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
178796
+ fts5MultiIterAdvanced(p, pNew, iEq, iIter);
178797
+ }
178798
+ }
178799
+ fts5MultiIterSetEof(pNew);
178800
+ fts5AssertMultiIterSetup(p, pNew);
178801
+
178802
+ if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
178803
+ fts5MultiIterNext(p, pNew, 0, 0);
178804
+ }else if( pNew->base.bEof==0 ){
178805
+ Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
178806
+ pNew->xSetOutputs(pNew, pSeg);
178807
+ }
178808
+
178809
+ }else{
178810
+ fts5MultiIterFree(pNew);
178811
+ *ppOut = 0;
178812
+ }
178813
+}
178814
+
178815
+/*
178816
+** Create an Fts5Iter that iterates through the doclist provided
178817
+** as the second argument.
178818
+*/
178819
+static void fts5MultiIterNew2(
178820
+ Fts5Index *p, /* FTS5 backend to iterate within */
178821
+ Fts5Data *pData, /* Doclist to iterate through */
178822
+ int bDesc, /* True for descending rowid order */
178823
+ Fts5Iter **ppOut /* New object */
178824
+){
178825
+ Fts5Iter *pNew;
178826
+ pNew = fts5MultiIterAlloc(p, 2);
178827
+ if( pNew ){
178828
+ Fts5SegIter *pIter = &pNew->aSeg[1];
178829
+
178830
+ pIter->flags = FTS5_SEGITER_ONETERM;
178831
+ if( pData->szLeaf>0 ){
178832
+ pIter->pLeaf = pData;
178833
+ pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
178834
+ pIter->iEndofDoclist = pData->nn;
178835
+ pNew->aFirst[1].iFirst = 1;
178836
+ if( bDesc ){
178837
+ pNew->bRev = 1;
178838
+ pIter->flags |= FTS5_SEGITER_REVERSE;
178839
+ fts5SegIterReverseInitPage(p, pIter);
178840
+ }else{
178841
+ fts5SegIterLoadNPos(p, pIter);
178842
+ }
178843
+ pData = 0;
178844
+ }else{
178845
+ pNew->base.bEof = 1;
178846
+ }
178847
+ fts5SegIterSetNext(p, pIter);
178848
+
178849
+ *ppOut = pNew;
178850
+ }
178851
+
178852
+ fts5DataRelease(pData);
178853
+}
178854
+
178855
+/*
178856
+** Return true if the iterator is at EOF or if an error has occurred.
178857
+** False otherwise.
178858
+*/
178859
+static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
178860
+ assert( p->rc
178861
+ || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
178862
+ );
178863
+ return (p->rc || pIter->base.bEof);
178864
+}
178865
+
178866
+/*
178867
+** Return the rowid of the entry that the iterator currently points
178868
+** to. If the iterator points to EOF when this function is called the
178869
+** results are undefined.
178870
+*/
178871
+static i64 fts5MultiIterRowid(Fts5Iter *pIter){
178872
+ assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
178873
+ return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
178874
+}
178875
+
178876
+/*
178877
+** Move the iterator to the next entry at or following iMatch.
178878
+*/
178879
+static void fts5MultiIterNextFrom(
178880
+ Fts5Index *p,
178881
+ Fts5Iter *pIter,
178882
+ i64 iMatch
178883
+){
178884
+ while( 1 ){
178885
+ i64 iRowid;
178886
+ fts5MultiIterNext(p, pIter, 1, iMatch);
178887
+ if( fts5MultiIterEof(p, pIter) ) break;
178888
+ iRowid = fts5MultiIterRowid(pIter);
178889
+ if( pIter->bRev==0 && iRowid>=iMatch ) break;
178890
+ if( pIter->bRev!=0 && iRowid<=iMatch ) break;
178891
+ }
178892
+}
178893
+
178894
+/*
178895
+** Return a pointer to a buffer containing the term associated with the
178896
+** entry that the iterator currently points to.
178897
+*/
178898
+static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
178899
+ Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
178900
+ *pn = p->term.n;
178901
+ return p->term.p;
178902
+}
177872178903
177873178904
/*
177874178905
** Allocate a new segment-id for the structure pStruct. The new segment
177875178906
** id must be between 1 and 65335 inclusive, and must not be used by
177876178907
** any currently existing segment. If a free segment id cannot be found,
@@ -177914,19 +178945,18 @@
177914178945
p->nPendingData = 0;
177915178946
}
177916178947
}
177917178948
177918178949
/*
177919
-** Return the size of the prefix, in bytes, that buffer (nNew/pNew) shares
177920
-** with buffer (nOld/pOld).
178950
+** Return the size of the prefix, in bytes, that buffer
178951
+** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
178952
+**
178953
+** Buffer (pNew/<length-unknown>) is guaranteed to be greater
178954
+** than buffer (pOld/nOld).
177921178955
*/
177922
-static int fts5PrefixCompress(
177923
- int nOld, const u8 *pOld,
177924
- int nNew, const u8 *pNew
177925
-){
178956
+static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
177926178957
int i;
177927
- assert( fts5BlobCompare(pOld, nOld, pNew, nNew)<0 );
177928178958
for(i=0; i<nOld; i++){
177929178959
if( pOld[i]!=pNew[i] ) break;
177930178960
}
177931178961
return i;
177932178962
}
@@ -178232,17 +179262,17 @@
178232179262
** In this case the previous term is not available, so just write a
178233179263
** copy of (pTerm/nTerm) into the parent node. This is slightly
178234179264
** inefficient, but still correct. */
178235179265
int n = nTerm;
178236179266
if( pPage->term.n ){
178237
- n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, nTerm, pTerm);
179267
+ n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
178238179268
}
178239179269
fts5WriteBtreeTerm(p, pWriter, n, pTerm);
178240179270
pPage = &pWriter->writer;
178241179271
}
178242179272
}else{
178243
- nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, nTerm, pTerm);
179273
+ nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
178244179274
fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
178245179275
}
178246179276
178247179277
/* Append the number of bytes of new data, then the term data itself
178248179278
** to the page. */
@@ -178401,11 +179431,11 @@
178401179431
/*
178402179432
** Iterator pIter was used to iterate through the input segments of on an
178403179433
** incremental merge operation. This function is called if the incremental
178404179434
** merge step has finished but the input has not been completely exhausted.
178405179435
*/
178406
-static void fts5TrimSegments(Fts5Index *p, Fts5IndexIter *pIter){
179436
+static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
178407179437
int i;
178408179438
Fts5Buffer buf;
178409179439
memset(&buf, 0, sizeof(Fts5Buffer));
178410179440
for(i=0; i<pIter->nSeg; i++){
178411179441
Fts5SegIter *pSeg = &pIter->aSeg[i];
@@ -178479,18 +179509,19 @@
178479179509
int *pnRem /* Write up to this many output leaves */
178480179510
){
178481179511
Fts5Structure *pStruct = *ppStruct;
178482179512
Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
178483179513
Fts5StructureLevel *pLvlOut;
178484
- Fts5IndexIter *pIter = 0; /* Iterator to read input data */
179514
+ Fts5Iter *pIter = 0; /* Iterator to read input data */
178485179515
int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
178486179516
int nInput; /* Number of input segments */
178487179517
Fts5SegWriter writer; /* Writer object */
178488179518
Fts5StructureSegment *pSeg; /* Output segment */
178489179519
Fts5Buffer term;
178490179520
int bOldest; /* True if the output segment is the oldest */
178491179521
int eDetail = p->pConfig->eDetail;
179522
+ const int flags = FTS5INDEX_QUERY_NOOUTPUT;
178492179523
178493179524
assert( iLvl<pStruct->nLevel );
178494179525
assert( pLvl->nMerge<=pLvl->nSeg );
178495179526
178496179527
memset(&writer, 0, sizeof(Fts5SegWriter));
@@ -178531,11 +179562,11 @@
178531179562
nInput = pLvl->nSeg;
178532179563
}
178533179564
bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
178534179565
178535179566
assert( iLvl>=0 );
178536
- for(fts5MultiIterNew(p, pStruct, 0, 0, 0, 0, iLvl, nInput, &pIter);
179567
+ for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
178537179568
fts5MultiIterEof(p, pIter)==0;
178538179569
fts5MultiIterNext(p, pIter, 0, 0)
178539179570
){
178540179571
Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
178541179572
int nPos; /* position-list size field value */
@@ -178603,11 +179634,11 @@
178603179634
assert( pSeg->pgnoLast>0 );
178604179635
fts5TrimSegments(p, pIter);
178605179636
pLvl->nMerge = nInput;
178606179637
}
178607179638
178608
- fts5MultiIterFree(p, pIter);
179639
+ fts5MultiIterFree(pIter);
178609179640
fts5BufferFree(&term);
178610179641
if( pnRem ) *pnRem -= writer.nLeafWritten;
178611179642
}
178612179643
178613179644
/*
@@ -178976,279 +180007,33 @@
178976180007
fts5StructureRelease(pStruct);
178977180008
178978180009
return fts5IndexReturn(p);
178979180010
}
178980180011
178981
-static void fts5PoslistCallback(
178982
- Fts5Index *p,
178983
- void *pContext,
178984
- const u8 *pChunk, int nChunk
178985
-){
178986
- assert_nc( nChunk>=0 );
178987
- if( nChunk>0 ){
178988
- fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
178989
- }
178990
-}
178991
-
178992
-typedef struct PoslistCallbackCtx PoslistCallbackCtx;
178993
-struct PoslistCallbackCtx {
178994
- Fts5Buffer *pBuf; /* Append to this buffer */
178995
- Fts5Colset *pColset; /* Restrict matches to this column */
178996
- int eState; /* See above */
178997
-};
178998
-
178999
-typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
179000
-struct PoslistOffsetsCtx {
179001
- Fts5Buffer *pBuf; /* Append to this buffer */
179002
- Fts5Colset *pColset; /* Restrict matches to this column */
179003
- int iRead;
179004
- int iWrite;
179005
-};
179006
-
179007
-/*
179008
-** TODO: Make this more efficient!
179009
-*/
179010
-static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
179011
- int i;
179012
- for(i=0; i<pColset->nCol; i++){
179013
- if( pColset->aiCol[i]==iCol ) return 1;
179014
- }
179015
- return 0;
179016
-}
179017
-
179018
-static void fts5PoslistOffsetsCallback(
179019
- Fts5Index *p,
179020
- void *pContext,
179021
- const u8 *pChunk, int nChunk
179022
-){
179023
- PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
179024
- assert_nc( nChunk>=0 );
179025
- if( nChunk>0 ){
179026
- int i = 0;
179027
- while( i<nChunk ){
179028
- int iVal;
179029
- i += fts5GetVarint32(&pChunk[i], iVal);
179030
- iVal += pCtx->iRead - 2;
179031
- pCtx->iRead = iVal;
179032
- if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
179033
- fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
179034
- pCtx->iWrite = iVal;
179035
- }
179036
- }
179037
- }
179038
-}
179039
-
179040
-static void fts5PoslistFilterCallback(
179041
- Fts5Index *p,
179042
- void *pContext,
179043
- const u8 *pChunk, int nChunk
179044
-){
179045
- PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
179046
- assert_nc( nChunk>=0 );
179047
- if( nChunk>0 ){
179048
- /* Search through to find the first varint with value 1. This is the
179049
- ** start of the next columns hits. */
179050
- int i = 0;
179051
- int iStart = 0;
179052
-
179053
- if( pCtx->eState==2 ){
179054
- int iCol;
179055
- fts5FastGetVarint32(pChunk, i, iCol);
179056
- if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
179057
- pCtx->eState = 1;
179058
- fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
179059
- }else{
179060
- pCtx->eState = 0;
179061
- }
179062
- }
179063
-
179064
- do {
179065
- while( i<nChunk && pChunk[i]!=0x01 ){
179066
- while( pChunk[i] & 0x80 ) i++;
179067
- i++;
179068
- }
179069
- if( pCtx->eState ){
179070
- fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
179071
- }
179072
- if( i<nChunk ){
179073
- int iCol;
179074
- iStart = i;
179075
- i++;
179076
- if( i>=nChunk ){
179077
- pCtx->eState = 2;
179078
- }else{
179079
- fts5FastGetVarint32(pChunk, i, iCol);
179080
- pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
179081
- if( pCtx->eState ){
179082
- fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
179083
- iStart = i;
179084
- }
179085
- }
179086
- }
179087
- }while( i<nChunk );
179088
- }
179089
-}
179090
-
179091
-/*
179092
-** Iterator pIter currently points to a valid entry (not EOF). This
179093
-** function appends the position list data for the current entry to
179094
-** buffer pBuf. It does not make a copy of the position-list size
179095
-** field.
179096
-*/
179097
-static void fts5SegiterPoslist(
179098
- Fts5Index *p,
179099
- Fts5SegIter *pSeg,
179100
- Fts5Colset *pColset,
179101
- Fts5Buffer *pBuf
179102
-){
179103
- if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
179104
- if( pColset==0 ){
179105
- fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
179106
- }else{
179107
- if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
179108
- PoslistCallbackCtx sCtx;
179109
- sCtx.pBuf = pBuf;
179110
- sCtx.pColset = pColset;
179111
- sCtx.eState = fts5IndexColsetTest(pColset, 0);
179112
- assert( sCtx.eState==0 || sCtx.eState==1 );
179113
- fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
179114
- }else{
179115
- PoslistOffsetsCtx sCtx;
179116
- memset(&sCtx, 0, sizeof(sCtx));
179117
- sCtx.pBuf = pBuf;
179118
- sCtx.pColset = pColset;
179119
- fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
179120
- }
179121
- }
179122
- }
179123
-}
179124
-
179125
-/*
179126
-** IN/OUT parameter (*pa) points to a position list n bytes in size. If
179127
-** the position list contains entries for column iCol, then (*pa) is set
179128
-** to point to the sub-position-list for that column and the number of
179129
-** bytes in it returned. Or, if the argument position list does not
179130
-** contain any entries for column iCol, return 0.
179131
-*/
179132
-static int fts5IndexExtractCol(
179133
- const u8 **pa, /* IN/OUT: Pointer to poslist */
179134
- int n, /* IN: Size of poslist in bytes */
179135
- int iCol /* Column to extract from poslist */
179136
-){
179137
- int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
179138
- const u8 *p = *pa;
179139
- const u8 *pEnd = &p[n]; /* One byte past end of position list */
179140
- u8 prev = 0;
179141
-
179142
- while( iCol>iCurrent ){
179143
- /* Advance pointer p until it points to pEnd or an 0x01 byte that is
179144
- ** not part of a varint */
179145
- while( (prev & 0x80) || *p!=0x01 ){
179146
- prev = *p++;
179147
- if( p==pEnd ) return 0;
179148
- }
179149
- *pa = p++;
179150
- p += fts5GetVarint32(p, iCurrent);
179151
- }
179152
- if( iCol!=iCurrent ) return 0;
179153
-
179154
- /* Advance pointer p until it points to pEnd or an 0x01 byte that is
179155
- ** not part of a varint */
179156
- assert( (prev & 0x80)==0 );
179157
- while( p<pEnd && ((prev & 0x80) || *p!=0x01) ){
179158
- prev = *p++;
179159
- }
179160
- return p - (*pa);
179161
-}
179162
-
179163
-static int fts5AppendRowid(
179164
- Fts5Index *p,
179165
- i64 iDelta,
179166
- Fts5IndexIter *pMulti,
179167
- Fts5Colset *pColset,
179168
- Fts5Buffer *pBuf
179169
-){
179170
- fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
179171
- return 0;
179172
-}
179173
-
179174
-/*
179175
-** Iterator pMulti currently points to a valid entry (not EOF). This
179176
-** function appends the following to buffer pBuf:
179177
-**
179178
-** * The varint iDelta, and
179179
-** * the position list that currently points to, including the size field.
179180
-**
179181
-** If argument pColset is NULL, then the position list is filtered according
179182
-** to pColset before being appended to the buffer. If this means there are
179183
-** no entries in the position list, nothing is appended to the buffer (not
179184
-** even iDelta).
179185
-**
179186
-** If an error occurs, an error code is left in p->rc.
179187
-*/
179188
-static int fts5AppendPoslist(
179189
- Fts5Index *p,
179190
- i64 iDelta,
179191
- Fts5IndexIter *pMulti,
179192
- Fts5Colset *pColset,
179193
- Fts5Buffer *pBuf
179194
-){
179195
- if( p->rc==SQLITE_OK ){
179196
- Fts5SegIter *pSeg = &pMulti->aSeg[ pMulti->aFirst[1].iFirst ];
179197
- assert( fts5MultiIterEof(p, pMulti)==0 );
179198
- assert( pSeg->nPos>0 );
179199
- if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+9+9) ){
179200
- if( p->pConfig->eDetail==FTS5_DETAIL_FULL
179201
- && pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf
179202
- && (pColset==0 || pColset->nCol==1)
179203
- ){
179204
- const u8 *pPos = &pSeg->pLeaf->p[pSeg->iLeafOffset];
179205
- int nPos;
179206
- if( pColset ){
179207
- nPos = fts5IndexExtractCol(&pPos, pSeg->nPos, pColset->aiCol[0]);
179208
- if( nPos==0 ) return 1;
179209
- }else{
179210
- nPos = pSeg->nPos;
179211
- }
179212
- assert( nPos>0 );
179213
- fts5BufferSafeAppendVarint(pBuf, iDelta);
179214
- fts5BufferSafeAppendVarint(pBuf, nPos*2);
179215
- fts5BufferSafeAppendBlob(pBuf, pPos, nPos);
179216
- }else{
179217
- int iSv1;
179218
- int iSv2;
179219
- int iData;
179220
-
179221
- /* Append iDelta */
179222
- iSv1 = pBuf->n;
179223
- fts5BufferSafeAppendVarint(pBuf, iDelta);
179224
-
179225
- /* WRITEPOSLISTSIZE */
179226
- iSv2 = pBuf->n;
179227
- fts5BufferSafeAppendVarint(pBuf, pSeg->nPos*2);
179228
- iData = pBuf->n;
179229
-
179230
- fts5SegiterPoslist(p, pSeg, pColset, pBuf);
179231
-
179232
- if( pColset ){
179233
- int nActual = pBuf->n - iData;
179234
- if( nActual!=pSeg->nPos ){
179235
- if( nActual==0 ){
179236
- pBuf->n = iSv1;
179237
- return 1;
179238
- }else{
179239
- int nReq = sqlite3Fts5GetVarintLen((u32)(nActual*2));
179240
- while( iSv2<(iData-nReq) ){ pBuf->p[iSv2++] = 0x80; }
179241
- sqlite3Fts5PutVarint(&pBuf->p[iSv2], nActual*2);
179242
- }
179243
- }
179244
- }
179245
- }
179246
- }
179247
- }
179248
-
179249
- return 0;
180012
+static void fts5AppendRowid(
180013
+ Fts5Index *p,
180014
+ i64 iDelta,
180015
+ Fts5Iter *pUnused,
180016
+ Fts5Buffer *pBuf
180017
+){
180018
+ UNUSED_PARAM(pUnused);
180019
+ fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
180020
+}
180021
+
180022
+static void fts5AppendPoslist(
180023
+ Fts5Index *p,
180024
+ i64 iDelta,
180025
+ Fts5Iter *pMulti,
180026
+ Fts5Buffer *pBuf
180027
+){
180028
+ int nData = pMulti->base.nData;
180029
+ assert( nData>0 );
180030
+ if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){
180031
+ fts5BufferSafeAppendVarint(pBuf, iDelta);
180032
+ fts5BufferSafeAppendVarint(pBuf, nData*2);
180033
+ fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
180034
+ }
179250180035
}
179251180036
179252180037
179253180038
static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
179254180039
u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
@@ -179388,73 +180173,107 @@
179388180173
){
179389180174
if( p2->n ){
179390180175
i64 iLastRowid = 0;
179391180176
Fts5DoclistIter i1;
179392180177
Fts5DoclistIter i2;
179393
- Fts5Buffer out;
179394
- Fts5Buffer tmp;
179395
- memset(&out, 0, sizeof(out));
179396
- memset(&tmp, 0, sizeof(tmp));
180178
+ Fts5Buffer out = {0, 0, 0};
180179
+ Fts5Buffer tmp = {0, 0, 0};
179397180180
179398
- sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
180181
+ if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n) ) return;
179399180182
fts5DoclistIterInit(p1, &i1);
179400180183
fts5DoclistIterInit(p2, &i2);
179401
- while( p->rc==SQLITE_OK && (i1.aPoslist!=0 || i2.aPoslist!=0) ){
179402
- if( i2.aPoslist==0 || (i1.aPoslist && i1.iRowid<i2.iRowid) ){
180184
+
180185
+ while( 1 ){
180186
+ if( i1.iRowid<i2.iRowid ){
179403180187
/* Copy entry from i1 */
179404180188
fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
179405180189
fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
179406180190
fts5DoclistIterNext(&i1);
180191
+ if( i1.aPoslist==0 ) break;
179407180192
}
179408
- else if( i1.aPoslist==0 || i2.iRowid!=i1.iRowid ){
180193
+ else if( i2.iRowid!=i1.iRowid ){
179409180194
/* Copy entry from i2 */
179410180195
fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
179411180196
fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
179412180197
fts5DoclistIterNext(&i2);
180198
+ if( i2.aPoslist==0 ) break;
179413180199
}
179414180200
else{
180201
+ /* Merge the two position lists. */
179415180202
i64 iPos1 = 0;
179416180203
i64 iPos2 = 0;
179417180204
int iOff1 = 0;
179418180205
int iOff2 = 0;
179419180206
u8 *a1 = &i1.aPoslist[i1.nSize];
179420180207
u8 *a2 = &i2.aPoslist[i2.nSize];
179421180208
179422
- Fts5PoslistWriter writer;
179423
- memset(&writer, 0, sizeof(writer));
179424
-
179425
- /* Merge the two position lists. */
179426
- fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
179427
- fts5BufferZero(&tmp);
179428
-
179429
- sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
179430
- sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
179431
-
179432
- while( p->rc==SQLITE_OK && (iPos1>=0 || iPos2>=0) ){
179433
- i64 iNew;
179434
- if( iPos2<0 || (iPos1>=0 && iPos1<iPos2) ){
179435
- iNew = iPos1;
179436
- sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
179437
- }else{
179438
- iNew = iPos2;
179439
- sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
179440
- if( iPos1==iPos2 ){
179441
- sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1,&iPos1);
179442
- }
179443
- }
179444
- if( iNew!=writer.iPrev || tmp.n==0 ){
179445
- p->rc = sqlite3Fts5PoslistWriterAppend(&tmp, &writer, iNew);
179446
- }
180209
+ i64 iPrev = 0;
180210
+ Fts5PoslistWriter writer;
180211
+ memset(&writer, 0, sizeof(writer));
180212
+
180213
+ fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
180214
+ fts5BufferZero(&tmp);
180215
+ sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
180216
+ if( p->rc ) break;
180217
+
180218
+ sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180219
+ sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180220
+ assert( iPos1>=0 && iPos2>=0 );
180221
+
180222
+ if( iPos1<iPos2 ){
180223
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180224
+ sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180225
+ }else{
180226
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180227
+ sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180228
+ }
180229
+
180230
+ if( iPos1>=0 && iPos2>=0 ){
180231
+ while( 1 ){
180232
+ if( iPos1<iPos2 ){
180233
+ if( iPos1!=iPrev ){
180234
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180235
+ }
180236
+ sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180237
+ if( iPos1<0 ) break;
180238
+ }else{
180239
+ assert( iPos2!=iPrev );
180240
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180241
+ sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180242
+ if( iPos2<0 ) break;
180243
+ }
180244
+ }
180245
+ }
180246
+
180247
+ if( iPos1>=0 ){
180248
+ if( iPos1!=iPrev ){
180249
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180250
+ }
180251
+ fts5BufferSafeAppendBlob(&tmp, &a1[iOff1], i1.nPoslist-iOff1);
180252
+ }else{
180253
+ assert( iPos2>=0 && iPos2!=iPrev );
180254
+ sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180255
+ fts5BufferSafeAppendBlob(&tmp, &a2[iOff2], i2.nPoslist-iOff2);
179447180256
}
179448180257
179449180258
/* WRITEPOSLISTSIZE */
179450180259
fts5BufferSafeAppendVarint(&out, tmp.n * 2);
179451180260
fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
179452180261
fts5DoclistIterNext(&i1);
179453180262
fts5DoclistIterNext(&i2);
180263
+ if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
179454180264
}
179455180265
}
180266
+
180267
+ if( i1.aPoslist ){
180268
+ fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
180269
+ fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
180270
+ }
180271
+ else if( i2.aPoslist ){
180272
+ fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
180273
+ fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
180274
+ }
179456180275
179457180276
fts5BufferSet(&p->rc, p1, out.n, out.p);
179458180277
fts5BufferFree(&tmp);
179459180278
fts5BufferFree(&out);
179460180279
}
@@ -179464,18 +180283,18 @@
179464180283
Fts5Index *p, /* Index to read from */
179465180284
int bDesc, /* True for "ORDER BY rowid DESC" */
179466180285
const u8 *pToken, /* Buffer containing prefix to match */
179467180286
int nToken, /* Size of buffer pToken in bytes */
179468180287
Fts5Colset *pColset, /* Restrict matches to these columns */
179469
- Fts5IndexIter **ppIter /* OUT: New iterator */
180288
+ Fts5Iter **ppIter /* OUT: New iterator */
179470180289
){
179471180290
Fts5Structure *pStruct;
179472180291
Fts5Buffer *aBuf;
179473180292
const int nBuf = 32;
179474180293
179475180294
void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
179476
- int (*xAppend)(Fts5Index*, i64, Fts5IndexIter*, Fts5Colset*, Fts5Buffer*);
180295
+ void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
179477180296
if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
179478180297
xMerge = fts5MergeRowidLists;
179479180298
xAppend = fts5AppendRowid;
179480180299
}else{
179481180300
xMerge = fts5MergePrefixLists;
@@ -179484,32 +180303,40 @@
179484180303
179485180304
aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
179486180305
pStruct = fts5StructureRead(p);
179487180306
179488180307
if( aBuf && pStruct ){
179489
- const int flags = FTS5INDEX_QUERY_SCAN;
180308
+ const int flags = FTS5INDEX_QUERY_SCAN
180309
+ | FTS5INDEX_QUERY_SKIPEMPTY
180310
+ | FTS5INDEX_QUERY_NOOUTPUT;
179490180311
int i;
179491180312
i64 iLastRowid = 0;
179492
- Fts5IndexIter *p1 = 0; /* Iterator used to gather data from index */
180313
+ Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
179493180314
Fts5Data *pData;
179494180315
Fts5Buffer doclist;
179495180316
int bNewTerm = 1;
179496180317
179497180318
memset(&doclist, 0, sizeof(doclist));
179498
- for(fts5MultiIterNew(p, pStruct, 1, flags, pToken, nToken, -1, 0, &p1);
180319
+ fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
180320
+ fts5IterSetOutputCb(&p->rc, p1);
180321
+ for( /* no-op */ ;
179499180322
fts5MultiIterEof(p, p1)==0;
179500180323
fts5MultiIterNext2(p, p1, &bNewTerm)
179501180324
){
179502
- i64 iRowid = fts5MultiIterRowid(p1);
179503
- int nTerm;
179504
- const u8 *pTerm = fts5MultiIterTerm(p1, &nTerm);
180325
+ Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
180326
+ int nTerm = pSeg->term.n;
180327
+ const u8 *pTerm = pSeg->term.p;
180328
+ p1->xSetOutputs(p1, pSeg);
180329
+
179505180330
assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
179506180331
if( bNewTerm ){
179507180332
if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
179508180333
}
179509180334
179510
- if( doclist.n>0 && iRowid<=iLastRowid ){
180335
+ if( p1->base.nData==0 ) continue;
180336
+
180337
+ if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
179511180338
for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
179512180339
assert( i<nBuf );
179513180340
if( aBuf[i].n==0 ){
179514180341
fts5BufferSwap(&doclist, &aBuf[i]);
179515180342
fts5BufferZero(&doclist);
@@ -179519,22 +180346,21 @@
179519180346
}
179520180347
}
179521180348
iLastRowid = 0;
179522180349
}
179523180350
179524
- if( !xAppend(p, iRowid-iLastRowid, p1, pColset, &doclist) ){
179525
- iLastRowid = iRowid;
179526
- }
180351
+ xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
180352
+ iLastRowid = p1->base.iRowid;
179527180353
}
179528180354
179529180355
for(i=0; i<nBuf; i++){
179530180356
if( p->rc==SQLITE_OK ){
179531180357
xMerge(p, &doclist, &aBuf[i]);
179532180358
}
179533180359
fts5BufferFree(&aBuf[i]);
179534180360
}
179535
- fts5MultiIterFree(p, p1);
180361
+ fts5MultiIterFree(p1);
179536180362
179537180363
pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n);
179538180364
if( pData ){
179539180365
pData->p = (u8*)&pData[1];
179540180366
pData->nn = pData->szLeaf = doclist.n;
@@ -179591,11 +180417,11 @@
179591180417
** records must be invalidated.
179592180418
*/
179593180419
static int sqlite3Fts5IndexRollback(Fts5Index *p){
179594180420
fts5CloseReader(p);
179595180421
fts5IndexDiscardData(p);
179596
- assert( p->rc==SQLITE_OK );
180422
+ /* assert( p->rc==SQLITE_OK ); */
179597180423
return SQLITE_OK;
179598180424
}
179599180425
179600180426
/*
179601180427
** The %_data table is completely empty when this function is called. This
@@ -179763,26 +180589,31 @@
179763180589
int flags, /* Mask of FTS5INDEX_QUERY_X flags */
179764180590
Fts5Colset *pColset, /* Match these columns only */
179765180591
Fts5IndexIter **ppIter /* OUT: New iterator object */
179766180592
){
179767180593
Fts5Config *pConfig = p->pConfig;
179768
- Fts5IndexIter *pRet = 0;
179769
- int iIdx = 0;
180594
+ Fts5Iter *pRet = 0;
179770180595
Fts5Buffer buf = {0, 0, 0};
179771180596
179772180597
/* If the QUERY_SCAN flag is set, all other flags must be clear. */
179773180598
assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
179774180599
179775180600
if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
180601
+ int iIdx = 0; /* Index to search */
179776180602
memcpy(&buf.p[1], pToken, nToken);
179777180603
179778
-#ifdef SQLITE_DEBUG
179779
- /* If the QUERY_TEST_NOIDX flag was specified, then this must be a
180604
+ /* Figure out which index to search and set iIdx accordingly. If this
180605
+ ** is a prefix query for which there is no prefix index, set iIdx to
180606
+ ** greater than pConfig->nPrefix to indicate that the query will be
180607
+ ** satisfied by scanning multiple terms in the main index.
180608
+ **
180609
+ ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
179780180610
** prefix-query. Instead of using a prefix-index (if one exists),
179781180611
** evaluate the prefix query using the main FTS index. This is used
179782180612
** for internal sanity checking by the integrity-check in debug
179783180613
** mode only. */
180614
+#ifdef SQLITE_DEBUG
179784180615
if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
179785180616
assert( flags & FTS5INDEX_QUERY_PREFIX );
179786180617
iIdx = 1+pConfig->nPrefix;
179787180618
}else
179788180619
#endif
@@ -179792,54 +180623,62 @@
179792180623
if( pConfig->aPrefix[iIdx-1]==nChar ) break;
179793180624
}
179794180625
}
179795180626
179796180627
if( iIdx<=pConfig->nPrefix ){
180628
+ /* Straight index lookup */
179797180629
Fts5Structure *pStruct = fts5StructureRead(p);
179798180630
buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
179799180631
if( pStruct ){
179800
- fts5MultiIterNew(p, pStruct, 1, flags, buf.p, nToken+1, -1, 0, &pRet);
180632
+ fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
180633
+ pColset, buf.p, nToken+1, -1, 0, &pRet
180634
+ );
179801180635
fts5StructureRelease(pStruct);
179802180636
}
179803180637
}else{
180638
+ /* Scan multiple terms in the main index */
179804180639
int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
179805180640
buf.p[0] = FTS5_MAIN_PREFIX;
179806180641
fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
180642
+ assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
180643
+ fts5IterSetOutputCb(&p->rc, pRet);
180644
+ if( p->rc==SQLITE_OK ){
180645
+ Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
180646
+ if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
180647
+ }
179807180648
}
179808180649
179809180650
if( p->rc ){
179810
- sqlite3Fts5IterClose(pRet);
180651
+ sqlite3Fts5IterClose(&pRet->base);
179811180652
pRet = 0;
179812180653
fts5CloseReader(p);
179813180654
}
179814
- *ppIter = pRet;
180655
+
180656
+ *ppIter = &pRet->base;
179815180657
sqlite3Fts5BufferFree(&buf);
179816180658
}
179817180659
return fts5IndexReturn(p);
179818180660
}
179819180661
179820180662
/*
179821180663
** Return true if the iterator passed as the only argument is at EOF.
179822180664
*/
179823
-static int sqlite3Fts5IterEof(Fts5IndexIter *pIter){
179824
- assert( pIter->pIndex->rc==SQLITE_OK );
179825
- return pIter->bEof;
179826
-}
179827
-
179828180665
/*
179829180666
** Move to the next matching rowid.
179830180667
*/
179831
-static int sqlite3Fts5IterNext(Fts5IndexIter *pIter){
180668
+static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
180669
+ Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
179832180670
assert( pIter->pIndex->rc==SQLITE_OK );
179833180671
fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
179834180672
return fts5IndexReturn(pIter->pIndex);
179835180673
}
179836180674
179837180675
/*
179838180676
** Move to the next matching term/rowid. Used by the fts5vocab module.
179839180677
*/
179840
-static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIter){
180678
+static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
180679
+ Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
179841180680
Fts5Index *p = pIter->pIndex;
179842180681
179843180682
assert( pIter->pIndex->rc==SQLITE_OK );
179844180683
179845180684
fts5MultiIterNext(p, pIter, 0, 0);
@@ -179846,11 +180685,11 @@
179846180685
if( p->rc==SQLITE_OK ){
179847180686
Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179848180687
if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
179849180688
fts5DataRelease(pSeg->pLeaf);
179850180689
pSeg->pLeaf = 0;
179851
- pIter->bEof = 1;
180690
+ pIter->base.bEof = 1;
179852180691
}
179853180692
}
179854180693
179855180694
return fts5IndexReturn(pIter->pIndex);
179856180695
}
@@ -179858,135 +180697,34 @@
179858180697
/*
179859180698
** Move to the next matching rowid that occurs at or after iMatch. The
179860180699
** definition of "at or after" depends on whether this iterator iterates
179861180700
** in ascending or descending rowid order.
179862180701
*/
179863
-static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIter, i64 iMatch){
180702
+static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
180703
+ Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
179864180704
fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
179865180705
return fts5IndexReturn(pIter->pIndex);
179866180706
}
179867180707
179868
-/*
179869
-** Return the current rowid.
179870
-*/
179871
-static i64 sqlite3Fts5IterRowid(Fts5IndexIter *pIter){
179872
- return fts5MultiIterRowid(pIter);
179873
-}
179874
-
179875180708
/*
179876180709
** Return the current term.
179877180710
*/
179878
-static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIter, int *pn){
180711
+static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
179879180712
int n;
179880
- const char *z = (const char*)fts5MultiIterTerm(pIter, &n);
180713
+ const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
179881180714
*pn = n-1;
179882180715
return &z[1];
179883180716
}
179884180717
179885
-
179886
-static int fts5IndexExtractColset (
179887
- Fts5Colset *pColset, /* Colset to filter on */
179888
- const u8 *pPos, int nPos, /* Position list */
179889
- Fts5Buffer *pBuf /* Output buffer */
179890
-){
179891
- int rc = SQLITE_OK;
179892
- int i;
179893
-
179894
- fts5BufferZero(pBuf);
179895
- for(i=0; i<pColset->nCol; i++){
179896
- const u8 *pSub = pPos;
179897
- int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
179898
- if( nSub ){
179899
- fts5BufferAppendBlob(&rc, pBuf, nSub, pSub);
179900
- }
179901
- }
179902
- return rc;
179903
-}
179904
-
179905
-
179906
-/*
179907
-** Return a pointer to a buffer containing a copy of the position list for
179908
-** the current entry. Output variable *pn is set to the size of the buffer
179909
-** in bytes before returning.
179910
-**
179911
-** The returned position list does not include the "number of bytes" varint
179912
-** field that starts the position list on disk.
179913
-*/
179914
-static int sqlite3Fts5IterPoslist(
179915
- Fts5IndexIter *pIter,
179916
- Fts5Colset *pColset, /* Column filter (or NULL) */
179917
- const u8 **pp, /* OUT: Pointer to position-list data */
179918
- int *pn, /* OUT: Size of position-list in bytes */
179919
- i64 *piRowid /* OUT: Current rowid */
179920
-){
179921
- Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179922
- int eDetail = pIter->pIndex->pConfig->eDetail;
179923
-
179924
- assert( pIter->pIndex->rc==SQLITE_OK );
179925
- *piRowid = pSeg->iRowid;
179926
- if( eDetail==FTS5_DETAIL_NONE ){
179927
- *pn = pSeg->nPos;
179928
- }else
179929
- if( eDetail==FTS5_DETAIL_FULL
179930
- && pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf
179931
- ){
179932
- u8 *pPos = &pSeg->pLeaf->p[pSeg->iLeafOffset];
179933
- if( pColset==0 || pIter->bFiltered ){
179934
- *pn = pSeg->nPos;
179935
- *pp = pPos;
179936
- }else if( pColset->nCol==1 ){
179937
- *pp = pPos;
179938
- *pn = fts5IndexExtractCol(pp, pSeg->nPos, pColset->aiCol[0]);
179939
- }else{
179940
- fts5BufferZero(&pIter->poslist);
179941
- fts5IndexExtractColset(pColset, pPos, pSeg->nPos, &pIter->poslist);
179942
- *pp = pIter->poslist.p;
179943
- *pn = pIter->poslist.n;
179944
- }
179945
- }else{
179946
- fts5BufferZero(&pIter->poslist);
179947
- fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
179948
- if( eDetail==FTS5_DETAIL_FULL ){
179949
- *pp = pIter->poslist.p;
179950
- }
179951
- *pn = pIter->poslist.n;
179952
- }
179953
- return fts5IndexReturn(pIter->pIndex);
179954
-}
179955
-
179956
-static int sqlite3Fts5IterCollist(
179957
- Fts5IndexIter *pIter,
179958
- const u8 **pp, /* OUT: Pointer to position-list data */
179959
- int *pn /* OUT: Size of position-list in bytes */
179960
-){
179961
- assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
179962
- *pp = pIter->poslist.p;
179963
- *pn = pIter->poslist.n;
179964
- return SQLITE_OK;
179965
-}
179966
-
179967
-/*
179968
-** This function is similar to sqlite3Fts5IterPoslist(), except that it
179969
-** copies the position list into the buffer supplied as the second
179970
-** argument.
179971
-*/
179972
-static int sqlite3Fts5IterPoslistBuffer(Fts5IndexIter *pIter, Fts5Buffer *pBuf){
179973
- Fts5Index *p = pIter->pIndex;
179974
- Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179975
- assert( p->rc==SQLITE_OK );
179976
- fts5BufferZero(pBuf);
179977
- fts5SegiterPoslist(p, pSeg, 0, pBuf);
179978
- return fts5IndexReturn(p);
179979
-}
179980
-
179981180718
/*
179982180719
** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
179983180720
*/
179984
-static void sqlite3Fts5IterClose(Fts5IndexIter *pIter){
179985
- if( pIter ){
180721
+static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
180722
+ if( pIndexIter ){
180723
+ Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
179986180724
Fts5Index *pIndex = pIter->pIndex;
179987
- fts5MultiIterFree(pIter->pIndex, pIter);
180725
+ fts5MultiIterFree(pIter);
179988180726
fts5CloseReader(pIndex);
179989180727
}
179990180728
}
179991180729
179992180730
/*
@@ -180147,39 +180885,34 @@
180147180885
int flags, /* Flags for Fts5IndexQuery */
180148180886
u64 *pCksum /* IN/OUT: Checksum value */
180149180887
){
180150180888
int eDetail = p->pConfig->eDetail;
180151180889
u64 cksum = *pCksum;
180152
- Fts5IndexIter *pIdxIter = 0;
180153
- Fts5Buffer buf = {0, 0, 0};
180154
- int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIdxIter);
180890
+ Fts5IndexIter *pIter = 0;
180891
+ int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
180155180892
180156
- while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIdxIter) ){
180157
- i64 rowid = sqlite3Fts5IterRowid(pIdxIter);
180893
+ while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
180894
+ i64 rowid = pIter->iRowid;
180158180895
180159180896
if( eDetail==FTS5_DETAIL_NONE ){
180160180897
cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
180161180898
}else{
180162
- rc = sqlite3Fts5IterPoslistBuffer(pIdxIter, &buf);
180163
- if( rc==SQLITE_OK ){
180164
- Fts5PoslistReader sReader;
180165
- for(sqlite3Fts5PoslistReaderInit(buf.p, buf.n, &sReader);
180166
- sReader.bEof==0;
180167
- sqlite3Fts5PoslistReaderNext(&sReader)
180168
- ){
180169
- int iCol = FTS5_POS2COLUMN(sReader.iPos);
180170
- int iOff = FTS5_POS2OFFSET(sReader.iPos);
180171
- cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
180172
- }
180899
+ Fts5PoslistReader sReader;
180900
+ for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
180901
+ sReader.bEof==0;
180902
+ sqlite3Fts5PoslistReaderNext(&sReader)
180903
+ ){
180904
+ int iCol = FTS5_POS2COLUMN(sReader.iPos);
180905
+ int iOff = FTS5_POS2OFFSET(sReader.iPos);
180906
+ cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
180173180907
}
180174180908
}
180175180909
if( rc==SQLITE_OK ){
180176
- rc = sqlite3Fts5IterNext(pIdxIter);
180910
+ rc = sqlite3Fts5IterNext(pIter);
180177180911
}
180178180912
}
180179
- sqlite3Fts5IterClose(pIdxIter);
180180
- fts5BufferFree(&buf);
180913
+ sqlite3Fts5IterClose(pIter);
180181180914
180182180915
*pCksum = cksum;
180183180916
return rc;
180184180917
}
180185180918
@@ -180480,18 +181213,19 @@
180480181213
*/
180481181214
static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
180482181215
int eDetail = p->pConfig->eDetail;
180483181216
u64 cksum2 = 0; /* Checksum based on contents of indexes */
180484181217
Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
180485
- Fts5IndexIter *pIter; /* Used to iterate through entire index */
181218
+ Fts5Iter *pIter; /* Used to iterate through entire index */
180486181219
Fts5Structure *pStruct; /* Index structure */
180487181220
180488181221
#ifdef SQLITE_DEBUG
180489181222
/* Used by extra internal tests only run if NDEBUG is not defined */
180490181223
u64 cksum3 = 0; /* Checksum based on contents of indexes */
180491181224
Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
180492181225
#endif
181226
+ const int flags = FTS5INDEX_QUERY_NOOUTPUT;
180493181227
180494181228
/* Load the FTS index structure */
180495181229
pStruct = fts5StructureRead(p);
180496181230
180497181231
/* Check that the internal nodes of each segment match the leaves */
@@ -180516,11 +181250,11 @@
180516181250
**
180517181251
** As each term visited by the linear scans, a separate query for the
180518181252
** same term is performed. cksum3 is calculated based on the entries
180519181253
** extracted by these queries.
180520181254
*/
180521
- for(fts5MultiIterNew(p, pStruct, 0, 0, 0, 0, -1, 0, &pIter);
181255
+ for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
180522181256
fts5MultiIterEof(p, pIter)==0;
180523181257
fts5MultiIterNext(p, pIter, 0, 0)
180524181258
){
180525181259
int n; /* Size of term in bytes */
180526181260
i64 iPos = 0; /* Position read from poslist */
@@ -180545,11 +181279,11 @@
180545181279
}
180546181280
}
180547181281
}
180548181282
fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
180549181283
180550
- fts5MultiIterFree(p, pIter);
181284
+ fts5MultiIterFree(pIter);
180551181285
if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
180552181286
180553181287
fts5StructureRelease(pStruct);
180554181288
#ifdef SQLITE_DEBUG
180555181289
fts5BufferFree(&term);
@@ -180782,10 +181516,11 @@
180782181516
int rc = SQLITE_OK; /* Return code */
180783181517
int nSpace = 0;
180784181518
int eDetailNone = (sqlite3_user_data(pCtx)!=0);
180785181519
180786181520
assert( nArg==2 );
181521
+ UNUSED_PARAM(nArg);
180787181522
memset(&s, 0, sizeof(Fts5Buffer));
180788181523
iRowid = sqlite3_value_int64(apVal[0]);
180789181524
180790181525
/* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
180791181526
** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
@@ -181234,14 +181969,14 @@
181234181969
#define FTS5_BI_ORDER_DESC 0x0080
181235181970
181236181971
/*
181237181972
** Values for Fts5Cursor.csrflags
181238181973
*/
181239
-#define FTS5CSR_REQUIRE_CONTENT 0x01
181240
-#define FTS5CSR_REQUIRE_DOCSIZE 0x02
181241
-#define FTS5CSR_REQUIRE_INST 0x04
181242
-#define FTS5CSR_EOF 0x08
181974
+#define FTS5CSR_EOF 0x01
181975
+#define FTS5CSR_REQUIRE_CONTENT 0x02
181976
+#define FTS5CSR_REQUIRE_DOCSIZE 0x04
181977
+#define FTS5CSR_REQUIRE_INST 0x08
181243181978
#define FTS5CSR_FREE_ZRANK 0x10
181244181979
#define FTS5CSR_REQUIRE_RESEEK 0x20
181245181980
#define FTS5CSR_REQUIRE_POSLIST 0x40
181246181981
181247181982
#define BitFlagAllTest(x,y) (((x) & (y))==(y))
@@ -181552,11 +182287,11 @@
181552182287
181553182288
/* Set idxFlags flags for all WHERE clause terms that will be used. */
181554182289
for(i=0; i<pInfo->nConstraint; i++){
181555182290
struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
181556182291
int j;
181557
- for(j=0; j<(int)ArraySize(aConstraint); j++){
182292
+ for(j=0; j<ArraySize(aConstraint); j++){
181558182293
struct Constraint *pC = &aConstraint[j];
181559182294
if( p->iColumn==aColMap[pC->iCol] && p->op & pC->op ){
181560182295
if( p->usable ){
181561182296
pC->iConsIndex = i;
181562182297
idxFlags |= pC->fts5op;
@@ -181599,11 +182334,11 @@
181599182334
pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
181600182335
}
181601182336
181602182337
/* Assign argvIndex values to each constraint in use. */
181603182338
iNext = 1;
181604
- for(i=0; i<(int)ArraySize(aConstraint); i++){
182339
+ for(i=0; i<ArraySize(aConstraint); i++){
181605182340
struct Constraint *pC = &aConstraint[i];
181606182341
if( pC->iConsIndex>=0 ){
181607182342
pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
181608182343
pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
181609182344
}
@@ -181792,18 +182527,19 @@
181792182527
Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
181793182528
int bDesc = pCsr->bDesc;
181794182529
i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
181795182530
181796182531
rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc);
181797
- if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
182532
+ if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
181798182533
*pbSkip = 1;
181799182534
}
181800182535
181801182536
CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
181802182537
fts5CsrNewrow(pCsr);
181803182538
if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
181804182539
CsrFlagSet(pCsr, FTS5CSR_EOF);
182540
+ *pbSkip = 1;
181805182541
}
181806182542
}
181807182543
return rc;
181808182544
}
181809182545
@@ -181816,28 +182552,28 @@
181816182552
** even if we reach end-of-file. The fts5EofMethod() will be called
181817182553
** subsequently to determine whether or not an EOF was hit.
181818182554
*/
181819182555
static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
181820182556
Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
181821
- int rc = SQLITE_OK;
182557
+ int rc;
181822182558
181823182559
assert( (pCsr->ePlan<3)==
181824182560
(pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
181825182561
);
182562
+ assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
181826182563
181827182564
if( pCsr->ePlan<3 ){
181828182565
int bSkip = 0;
181829182566
if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
181830182567
rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
181831
- if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
181832
- CsrFlagSet(pCsr, FTS5CSR_EOF);
181833
- }
182568
+ CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
181834182569
fts5CsrNewrow(pCsr);
181835182570
}else{
181836182571
switch( pCsr->ePlan ){
181837182572
case FTS5_PLAN_SPECIAL: {
181838182573
CsrFlagSet(pCsr, FTS5CSR_EOF);
182574
+ rc = SQLITE_OK;
181839182575
break;
181840182576
}
181841182577
181842182578
case FTS5_PLAN_SORTED_MATCH: {
181843182579
rc = fts5SorterNext(pCsr);
@@ -182109,11 +182845,11 @@
182109182845
** 3. A full-table scan.
182110182846
*/
182111182847
static int fts5FilterMethod(
182112182848
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
182113182849
int idxNum, /* Strategy index */
182114
- const char *idxStr, /* Unused */
182850
+ const char *zUnused, /* Unused */
182115182851
int nVal, /* Number of elements in apVal */
182116182852
sqlite3_value **apVal /* Arguments for the indexing scheme */
182117182853
){
182118182854
Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
182119182855
Fts5Config *pConfig = pTab->pConfig;
@@ -182126,10 +182862,13 @@
182126182862
sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
182127182863
sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
182128182864
sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
182129182865
sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
182130182866
char **pzErrmsg = pConfig->pzErrmsg;
182867
+
182868
+ UNUSED_PARAM(zUnused);
182869
+ UNUSED_PARAM(nVal);
182131182870
182132182871
if( pCsr->ePlan ){
182133182872
fts5FreeCursorComponents(pCsr);
182134182873
memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
182135182874
}
@@ -182411,12 +183150,11 @@
182411183150
return rc;
182412183151
}
182413183152
182414183153
static int fts5SpecialDelete(
182415183154
Fts5Table *pTab,
182416
- sqlite3_value **apVal,
182417
- sqlite3_int64 *piRowid
183155
+ sqlite3_value **apVal
182418183156
){
182419183157
int rc = SQLITE_OK;
182420183158
int eType1 = sqlite3_value_type(apVal[1]);
182421183159
if( eType1==SQLITE_INTEGER ){
182422183160
sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
@@ -182488,11 +183226,11 @@
182488183226
/* A "special" INSERT op. These are handled separately. */
182489183227
const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
182490183228
if( pConfig->eContent!=FTS5_CONTENT_NORMAL
182491183229
&& 0==sqlite3_stricmp("delete", z)
182492183230
){
182493
- rc = fts5SpecialDelete(pTab, apVal, pRowid);
183231
+ rc = fts5SpecialDelete(pTab, apVal);
182494183232
}else{
182495183233
rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
182496183234
}
182497183235
}else{
182498183236
/* A regular INSERT, UPDATE or DELETE statement. The trick here is that
@@ -182589,10 +183327,11 @@
182589183327
182590183328
/*
182591183329
** Implementation of xBegin() method.
182592183330
*/
182593183331
static int fts5BeginMethod(sqlite3_vtab *pVtab){
183332
+ UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
182594183333
fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0);
182595183334
return SQLITE_OK;
182596183335
}
182597183336
182598183337
/*
@@ -182599,10 +183338,11 @@
182599183338
** Implementation of xCommit() method. This is a no-op. The contents of
182600183339
** the pending-terms hash-table have already been flushed into the database
182601183340
** by fts5SyncMethod().
182602183341
*/
182603183342
static int fts5CommitMethod(sqlite3_vtab *pVtab){
183343
+ UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
182604183344
fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0);
182605183345
return SQLITE_OK;
182606183346
}
182607183347
182608183348
/*
@@ -182852,16 +183592,18 @@
182852183592
}
182853183593
182854183594
static int fts5ColumnSizeCb(
182855183595
void *pContext, /* Pointer to int */
182856183596
int tflags,
182857
- const char *pToken, /* Buffer containing token */
182858
- int nToken, /* Size of token in bytes */
182859
- int iStart, /* Start offset of token */
182860
- int iEnd /* End offset of token */
183597
+ const char *pUnused, /* Buffer containing token */
183598
+ int nUnused, /* Size of token in bytes */
183599
+ int iUnused1, /* Start offset of token */
183600
+ int iUnused2 /* End offset of token */
182861183601
){
182862183602
int *pCnt = (int*)pContext;
183603
+ UNUSED_PARAM2(pUnused, nUnused);
183604
+ UNUSED_PARAM2(iUnused1, iUnused2);
182863183605
if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
182864183606
(*pCnt)++;
182865183607
}
182866183608
return SQLITE_OK;
182867183609
}
@@ -182973,14 +183715,15 @@
182973183715
182974183716
return pRet;
182975183717
}
182976183718
182977183719
static void fts5ApiPhraseNext(
182978
- Fts5Context *pCtx,
183720
+ Fts5Context *pUnused,
182979183721
Fts5PhraseIter *pIter,
182980183722
int *piCol, int *piOff
182981183723
){
183724
+ UNUSED_PARAM(pUnused);
182982183725
if( pIter->a>=pIter->b ){
182983183726
*piCol = -1;
182984183727
*piOff = -1;
182985183728
}else{
182986183729
int iVal;
@@ -183128,16 +183871,15 @@
183128183871
int rc;
183129183872
Fts5Cursor *pNew = 0;
183130183873
183131183874
rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
183132183875
if( rc==SQLITE_OK ){
183133
- Fts5Config *pConf = pTab->pConfig;
183134183876
pNew->ePlan = FTS5_PLAN_MATCH;
183135183877
pNew->iFirstRowid = SMALLEST_INT64;
183136183878
pNew->iLastRowid = LARGEST_INT64;
183137183879
pNew->base.pVtab = (sqlite3_vtab*)pTab;
183138
- rc = sqlite3Fts5ExprClonePhrase(pConf, pCsr->pExpr, iPhrase, &pNew->pExpr);
183880
+ rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
183139183881
}
183140183882
183141183883
if( rc==SQLITE_OK ){
183142183884
for(rc = fts5CursorFirst(pTab, pNew, 0);
183143183885
rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
@@ -183346,18 +184088,19 @@
183346184088
** This routine implements the xFindFunction method for the FTS3
183347184089
** virtual table.
183348184090
*/
183349184091
static int fts5FindFunctionMethod(
183350184092
sqlite3_vtab *pVtab, /* Virtual table handle */
183351
- int nArg, /* Number of SQL function arguments */
184093
+ int nUnused, /* Number of SQL function arguments */
183352184094
const char *zName, /* Name of SQL function */
183353184095
void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
183354184096
void **ppArg /* OUT: User data for *pxFunc */
183355184097
){
183356184098
Fts5Table *pTab = (Fts5Table*)pVtab;
183357184099
Fts5Auxiliary *pAux;
183358184100
184101
+ UNUSED_PARAM(nUnused);
183359184102
pAux = fts5FindAuxiliary(pTab, zName);
183360184103
if( pAux ){
183361184104
*pxFunc = fts5ApiCallback;
183362184105
*ppArg = (void*)pAux;
183363184106
return 1;
@@ -183383,10 +184126,11 @@
183383184126
**
183384184127
** Flush the contents of the pending-terms table to disk.
183385184128
*/
183386184129
static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
183387184130
Fts5Table *pTab = (Fts5Table*)pVtab;
184131
+ UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
183388184132
fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
183389184133
fts5TripCursors(pTab);
183390184134
return sqlite3Fts5StorageSync(pTab->pStorage, 0);
183391184135
}
183392184136
@@ -183395,10 +184139,11 @@
183395184139
**
183396184140
** This is a no-op.
183397184141
*/
183398184142
static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
183399184143
Fts5Table *pTab = (Fts5Table*)pVtab;
184144
+ UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
183400184145
fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
183401184146
fts5TripCursors(pTab);
183402184147
return sqlite3Fts5StorageSync(pTab->pStorage, 0);
183403184148
}
183404184149
@@ -183407,10 +184152,11 @@
183407184152
**
183408184153
** Discard the contents of the pending terms table.
183409184154
*/
183410184155
static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
183411184156
Fts5Table *pTab = (Fts5Table*)pVtab;
184157
+ UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
183412184158
fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
183413184159
fts5TripCursors(pTab);
183414184160
return sqlite3Fts5StorageRollback(pTab->pStorage);
183415184161
}
183416184162
@@ -183586,14 +184332,15 @@
183586184332
}
183587184333
183588184334
static void fts5Fts5Func(
183589184335
sqlite3_context *pCtx, /* Function call context */
183590184336
int nArg, /* Number of args */
183591
- sqlite3_value **apVal /* Function arguments */
184337
+ sqlite3_value **apUnused /* Function arguments */
183592184338
){
183593184339
Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
183594184340
char buf[8];
184341
+ UNUSED_PARAM2(nArg, apUnused);
183595184342
assert( nArg==0 );
183596184343
assert( sizeof(buf)>=sizeof(pGlobal) );
183597184344
memcpy(buf, (void*)&pGlobal, sizeof(pGlobal));
183598184345
sqlite3_result_blob(pCtx, buf, sizeof(pGlobal), SQLITE_TRANSIENT);
183599184346
}
@@ -183602,14 +184349,15 @@
183602184349
** Implementation of fts5_source_id() function.
183603184350
*/
183604184351
static void fts5SourceIdFunc(
183605184352
sqlite3_context *pCtx, /* Function call context */
183606184353
int nArg, /* Number of args */
183607
- sqlite3_value **apVal /* Function arguments */
184354
+ sqlite3_value **apUnused /* Function arguments */
183608184355
){
183609184356
assert( nArg==0 );
183610
- sqlite3_result_text(pCtx, "fts5: 2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1", -1, SQLITE_TRANSIENT);
184357
+ UNUSED_PARAM2(nArg, apUnused);
184358
+ sqlite3_result_text(pCtx, "fts5: 2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f", -1, SQLITE_TRANSIENT);
183611184359
}
183612184360
183613184361
static int fts5Init(sqlite3 *db){
183614184362
static const sqlite3_module fts5Mod = {
183615184363
/* iVersion */ 2,
@@ -184050,11 +184798,11 @@
184050184798
int rc = SQLITE_OK;
184051184799
if( p ){
184052184800
int i;
184053184801
184054184802
/* Finalize all SQL statements */
184055
- for(i=0; i<(int)ArraySize(p->aStmt); i++){
184803
+ for(i=0; i<ArraySize(p->aStmt); i++){
184056184804
sqlite3_finalize(p->aStmt[i]);
184057184805
}
184058184806
184059184807
sqlite3_free(p);
184060184808
}
@@ -184074,15 +184822,16 @@
184074184822
static int fts5StorageInsertCallback(
184075184823
void *pContext, /* Pointer to Fts5InsertCtx object */
184076184824
int tflags,
184077184825
const char *pToken, /* Buffer containing token */
184078184826
int nToken, /* Size of token in bytes */
184079
- int iStart, /* Start offset of token */
184080
- int iEnd /* End offset of token */
184827
+ int iUnused1, /* Start offset of token */
184828
+ int iUnused2 /* End offset of token */
184081184829
){
184082184830
Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
184083184831
Fts5Index *pIdx = pCtx->pStorage->pIndex;
184832
+ UNUSED_PARAM2(iUnused1, iUnused2);
184084184833
if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
184085184834
pCtx->szCol++;
184086184835
}
184087184836
return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
184088184837
}
@@ -184509,20 +185258,22 @@
184509185258
static int fts5StorageIntegrityCallback(
184510185259
void *pContext, /* Pointer to Fts5IntegrityCtx object */
184511185260
int tflags,
184512185261
const char *pToken, /* Buffer containing token */
184513185262
int nToken, /* Size of token in bytes */
184514
- int iStart, /* Start offset of token */
184515
- int iEnd /* End offset of token */
185263
+ int iUnused1, /* Start offset of token */
185264
+ int iUnused2 /* End offset of token */
184516185265
){
184517185266
Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
184518185267
Fts5Termset *pTermset = pCtx->pTermset;
184519185268
int bPresent;
184520185269
int ii;
184521185270
int rc = SQLITE_OK;
184522185271
int iPos;
184523185272
int iCol;
185273
+
185274
+ UNUSED_PARAM2(iUnused1, iUnused2);
184524185275
184525185276
if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
184526185277
pCtx->szCol++;
184527185278
}
184528185279
@@ -184897,16 +185648,17 @@
184897185648
184898185649
/*
184899185650
** Create an "ascii" tokenizer.
184900185651
*/
184901185652
static int fts5AsciiCreate(
184902
- void *pCtx,
185653
+ void *pUnused,
184903185654
const char **azArg, int nArg,
184904185655
Fts5Tokenizer **ppOut
184905185656
){
184906185657
int rc = SQLITE_OK;
184907185658
AsciiTokenizer *p = 0;
185659
+ UNUSED_PARAM(pUnused);
184908185660
if( nArg%2 ){
184909185661
rc = SQLITE_ERROR;
184910185662
}else{
184911185663
p = sqlite3_malloc(sizeof(AsciiTokenizer));
184912185664
if( p==0 ){
@@ -184951,11 +185703,11 @@
184951185703
** Tokenize some text using the ascii tokenizer.
184952185704
*/
184953185705
static int fts5AsciiTokenize(
184954185706
Fts5Tokenizer *pTokenizer,
184955185707
void *pCtx,
184956
- int flags,
185708
+ int iUnused,
184957185709
const char *pText, int nText,
184958185710
int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
184959185711
){
184960185712
AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
184961185713
int rc = SQLITE_OK;
@@ -184964,10 +185716,12 @@
184964185716
184965185717
char aFold[64];
184966185718
int nFold = sizeof(aFold);
184967185719
char *pFold = aFold;
184968185720
unsigned char *a = p->aTokenChar;
185721
+
185722
+ UNUSED_PARAM(iUnused);
184969185723
184970185724
while( is<nText && rc==SQLITE_OK ){
184971185725
int nByte;
184972185726
184973185727
/* Skip any leading divider characters. */
@@ -185158,16 +185912,18 @@
185158185912
185159185913
/*
185160185914
** Create a "unicode61" tokenizer.
185161185915
*/
185162185916
static int fts5UnicodeCreate(
185163
- void *pCtx,
185917
+ void *pUnused,
185164185918
const char **azArg, int nArg,
185165185919
Fts5Tokenizer **ppOut
185166185920
){
185167185921
int rc = SQLITE_OK; /* Return code */
185168185922
Unicode61Tokenizer *p = 0; /* New tokenizer object */
185923
+
185924
+ UNUSED_PARAM(pUnused);
185169185925
185170185926
if( nArg%2 ){
185171185927
rc = SQLITE_ERROR;
185172185928
}else{
185173185929
p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
@@ -185221,11 +185977,11 @@
185221185977
}
185222185978
185223185979
static int fts5UnicodeTokenize(
185224185980
Fts5Tokenizer *pTokenizer,
185225185981
void *pCtx,
185226
- int flags,
185982
+ int iUnused,
185227185983
const char *pText, int nText,
185228185984
int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
185229185985
){
185230185986
Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
185231185987
int rc = SQLITE_OK;
@@ -185236,10 +185992,12 @@
185236185992
185237185993
/* Output buffer */
185238185994
char *aFold = p->aFold;
185239185995
int nFold = p->nFold;
185240185996
const char *pEnd = &aFold[nFold-6];
185997
+
185998
+ UNUSED_PARAM(iUnused);
185241185999
185242186000
/* Each iteration of this loop gobbles up a contiguous run of separators,
185243186001
** then the next token. */
185244186002
while( rc==SQLITE_OK ){
185245186003
int iCode; /* non-ASCII codepoint read from input */
@@ -186055,11 +186813,11 @@
186055186813
};
186056186814
186057186815
int rc = SQLITE_OK; /* Return code */
186058186816
int i; /* To iterate through builtin functions */
186059186817
186060
- for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aBuiltin); i++){
186818
+ for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
186061186819
rc = pApi->xCreateTokenizer(pApi,
186062186820
aBuiltin[i].zName,
186063186821
(void*)pApi,
186064186822
&aBuiltin[i].x,
186065186823
0
@@ -186196,13 +186954,13 @@
186196186954
};
186197186955
static const unsigned int aAscii[4] = {
186198186956
0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
186199186957
};
186200186958
186201
- if( c<128 ){
186959
+ if( (unsigned int)c<128 ){
186202186960
return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
186203
- }else if( c<(1<<22) ){
186961
+ }else if( (unsigned int)c<(1<<22) ){
186204186962
unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
186205186963
int iRes = 0;
186206186964
int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
186207186965
int iLo = 0;
186208186966
while( iHi>=iLo ){
@@ -186765,11 +187523,14 @@
186765187523
return fts5PutVarint64(p,v);
186766187524
}
186767187525
186768187526
186769187527
static int sqlite3Fts5GetVarintLen(u32 iVal){
187528
+#if 0
186770187529
if( iVal<(1 << 7 ) ) return 1;
187530
+#endif
187531
+ assert( iVal>=(1 << 7) );
186771187532
if( iVal<(1 << 14) ) return 2;
186772187533
if( iVal<(1 << 21) ) return 3;
186773187534
if( iVal<(1 << 28) ) return 4;
186774187535
return 5;
186775187536
}
@@ -186959,11 +187720,11 @@
186959187720
int nTab = (int)strlen(zTab)+1;
186960187721
int eType = 0;
186961187722
186962187723
rc = fts5VocabTableType(zType, pzErr, &eType);
186963187724
if( rc==SQLITE_OK ){
186964
- assert( eType>=0 && eType<sizeof(azSchema)/sizeof(azSchema[0]) );
187725
+ assert( eType>=0 && eType<ArraySize(azSchema) );
186965187726
rc = sqlite3_declare_vtab(db, azSchema[eType]);
186966187727
}
186967187728
186968187729
nByte = sizeof(Fts5VocabTable) + nDb + nTab;
186969187730
pRet = sqlite3Fts5MallocZero(&rc, nByte);
@@ -187012,19 +187773,21 @@
187012187773
187013187774
/*
187014187775
** Implementation of the xBestIndex method.
187015187776
*/
187016187777
static int fts5VocabBestIndexMethod(
187017
- sqlite3_vtab *pVTab,
187778
+ sqlite3_vtab *pUnused,
187018187779
sqlite3_index_info *pInfo
187019187780
){
187020187781
int i;
187021187782
int iTermEq = -1;
187022187783
int iTermGe = -1;
187023187784
int iTermLe = -1;
187024187785
int idxNum = 0;
187025187786
int nArg = 0;
187787
+
187788
+ UNUSED_PARAM(pUnused);
187026187789
187027187790
for(i=0; i<pInfo->nConstraint; i++){
187028187791
struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
187029187792
if( p->usable==0 ) continue;
187030187793
if( p->iColumn==0 ){ /* term column */
@@ -187182,59 +187945,54 @@
187182187945
memset(pCsr->aDoc, 0, nCol * sizeof(i64));
187183187946
pCsr->iCol = 0;
187184187947
187185187948
assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
187186187949
while( rc==SQLITE_OK ){
187187
- i64 dummy;
187188187950
const u8 *pPos; int nPos; /* Position list */
187189187951
i64 iPos = 0; /* 64-bit position read from poslist */
187190187952
int iOff = 0; /* Current offset within position list */
187191187953
187954
+ pPos = pCsr->pIter->pData;
187955
+ nPos = pCsr->pIter->nData;
187192187956
switch( pCsr->pConfig->eDetail ){
187193187957
case FTS5_DETAIL_FULL:
187194
- rc = sqlite3Fts5IterPoslist(pCsr->pIter, 0, &pPos, &nPos, &dummy);
187195
- if( rc==SQLITE_OK ){
187196
- if( pTab->eType==FTS5_VOCAB_ROW ){
187197
- while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187198
- pCsr->aCnt[0]++;
187199
- }
187200
- pCsr->aDoc[0]++;
187201
- }else{
187202
- int iCol = -1;
187203
- while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187204
- int ii = FTS5_POS2COLUMN(iPos);
187205
- pCsr->aCnt[ii]++;
187206
- if( iCol!=ii ){
187207
- if( ii>=nCol ){
187208
- rc = FTS5_CORRUPT;
187209
- break;
187210
- }
187211
- pCsr->aDoc[ii]++;
187212
- iCol = ii;
187213
- }
187958
+ pPos = pCsr->pIter->pData;
187959
+ nPos = pCsr->pIter->nData;
187960
+ if( pTab->eType==FTS5_VOCAB_ROW ){
187961
+ while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187962
+ pCsr->aCnt[0]++;
187963
+ }
187964
+ pCsr->aDoc[0]++;
187965
+ }else{
187966
+ int iCol = -1;
187967
+ while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187968
+ int ii = FTS5_POS2COLUMN(iPos);
187969
+ pCsr->aCnt[ii]++;
187970
+ if( iCol!=ii ){
187971
+ if( ii>=nCol ){
187972
+ rc = FTS5_CORRUPT;
187973
+ break;
187974
+ }
187975
+ pCsr->aDoc[ii]++;
187976
+ iCol = ii;
187214187977
}
187215187978
}
187216187979
}
187217187980
break;
187218187981
187219187982
case FTS5_DETAIL_COLUMNS:
187220187983
if( pTab->eType==FTS5_VOCAB_ROW ){
187221187984
pCsr->aDoc[0]++;
187222187985
}else{
187223
- Fts5Buffer buf = {0, 0, 0};
187224
- rc = sqlite3Fts5IterPoslistBuffer(pCsr->pIter, &buf);
187225
- if( rc==SQLITE_OK ){
187226
- while( 0==sqlite3Fts5PoslistNext64(buf.p, buf.n, &iOff,&iPos) ){
187227
- assert_nc( iPos>=0 && iPos<nCol );
187228
- if( iPos>=nCol ){
187229
- rc = FTS5_CORRUPT;
187230
- break;
187231
- }
187232
- pCsr->aDoc[iPos]++;
187233
- }
187234
- }
187235
- sqlite3Fts5BufferFree(&buf);
187986
+ while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
187987
+ assert_nc( iPos>=0 && iPos<nCol );
187988
+ if( iPos>=nCol ){
187989
+ rc = FTS5_CORRUPT;
187990
+ break;
187991
+ }
187992
+ pCsr->aDoc[iPos]++;
187993
+ }
187236187994
}
187237187995
break;
187238187996
187239187997
default:
187240187998
assert( pCsr->pConfig->eDetail==FTS5_DETAIL_NONE );
@@ -187268,12 +188026,12 @@
187268188026
** This is the xFilter implementation for the virtual table.
187269188027
*/
187270188028
static int fts5VocabFilterMethod(
187271188029
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
187272188030
int idxNum, /* Strategy index */
187273
- const char *idxStr, /* Unused */
187274
- int nVal, /* Number of elements in apVal */
188031
+ const char *zUnused, /* Unused */
188032
+ int nUnused, /* Number of elements in apVal */
187275188033
sqlite3_value **apVal /* Arguments for the indexing scheme */
187276188034
){
187277188035
Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
187278188036
int rc = SQLITE_OK;
187279188037
@@ -187283,10 +188041,12 @@
187283188041
int nTerm = 0;
187284188042
187285188043
sqlite3_value *pEq = 0;
187286188044
sqlite3_value *pGe = 0;
187287188045
sqlite3_value *pLe = 0;
188046
+
188047
+ UNUSED_PARAM2(zUnused, nUnused);
187288188048
187289188049
fts5VocabResetCursor(pCsr);
187290188050
if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
187291188051
if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
187292188052
if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
187293188053
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -121,10 +121,11 @@
121 #else
122 /* This is not VxWorks. */
123 #define OS_VXWORKS 0
124 #define HAVE_FCHOWN 1
125 #define HAVE_READLINK 1
 
126 #endif /* defined(_WRS_KERNEL) */
127
128 /************** End of vxworks.h *********************************************/
129 /************** Continuing where we left off in sqliteInt.h ******************/
130
@@ -327,11 +328,11 @@
327 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
328 ** [sqlite_version()] and [sqlite_source_id()].
329 */
330 #define SQLITE_VERSION "3.11.0"
331 #define SQLITE_VERSION_NUMBER 3011000
332 #define SQLITE_SOURCE_ID "2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1"
333
334 /*
335 ** CAPI3REF: Run-Time Library Version Numbers
336 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
337 **
@@ -561,11 +562,11 @@
561 ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
562 ** is not NULL then any error message is written into memory obtained
563 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
564 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
565 ** on error message strings returned through the 5th parameter of
566 ** of sqlite3_exec() after the error message string is no longer needed.
567 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
568 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
569 ** NULL before returning.
570 **
571 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5911,11 +5912,11 @@
5911 */
5912 struct sqlite3_index_info {
5913 /* Inputs */
5914 int nConstraint; /* Number of entries in aConstraint */
5915 struct sqlite3_index_constraint {
5916 int iColumn; /* Column on left-hand side of constraint */
5917 unsigned char op; /* Constraint operator */
5918 unsigned char usable; /* True if this constraint is usable */
5919 int iTermOffset; /* Used internally - xBestIndex should ignore */
5920 } *aConstraint; /* Table of WHERE clause constraints */
5921 int nOrderBy; /* Number of terms in the ORDER BY clause */
@@ -10468,18 +10469,28 @@
10468
10469 /*
10470 ** Flags passed as the third argument to sqlite3BtreeCursor().
10471 **
10472 ** For read-only cursors the wrFlag argument is always zero. For read-write
10473 ** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or
10474 ** (BTREE_WRCSR). If the BTREE_FORDELETE flag is set, then the cursor will
10475 ** only be used by SQLite for the following:
10476 **
10477 ** * to seek to and delete specific entries, and/or
10478 **
10479 ** * to read values that will be used to create keys that other
10480 ** BTREE_FORDELETE cursors will seek to and delete.
 
 
 
 
 
 
 
 
 
 
10481 */
10482 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
10483 #define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
10484
10485 SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -10504,11 +10515,16 @@
10504 int bias,
10505 int *pRes
10506 );
10507 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
10508 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
10509 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, int);
 
 
 
 
 
10510 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
10511 const void *pData, int nData,
10512 int nZero, int bias, int seekResult);
10513 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
10514 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
@@ -10762,94 +10778,95 @@
10762 */
10763 /************** Include opcodes.h in the middle of vdbe.h ********************/
10764 /************** Begin file opcodes.h *****************************************/
10765 /* Automatically generated. Do not edit */
10766 /* See the tool/mkopcodeh.tcl script for details */
10767 #define OP_Savepoint 1
10768 #define OP_AutoCommit 2
10769 #define OP_Transaction 3
10770 #define OP_SorterNext 4
10771 #define OP_PrevIfOpen 5
10772 #define OP_NextIfOpen 6
10773 #define OP_Prev 7
10774 #define OP_Next 8
10775 #define OP_Checkpoint 9
10776 #define OP_JournalMode 10
10777 #define OP_Vacuum 11
10778 #define OP_VFilter 12 /* synopsis: iplan=r[P3] zplan='P4' */
10779 #define OP_VUpdate 13 /* synopsis: data=r[P3@P2] */
10780 #define OP_Goto 14
10781 #define OP_Gosub 15
10782 #define OP_Return 16
10783 #define OP_InitCoroutine 17
10784 #define OP_EndCoroutine 18
 
10785 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
10786 #define OP_Yield 20
10787 #define OP_HaltIfNull 21 /* synopsis: if r[P3]=null halt */
10788 #define OP_Halt 22
10789 #define OP_Integer 23 /* synopsis: r[P2]=P1 */
10790 #define OP_Int64 24 /* synopsis: r[P2]=P4 */
10791 #define OP_String 25 /* synopsis: r[P2]='P4' (len=P1) */
10792 #define OP_Null 26 /* synopsis: r[P2..P3]=NULL */
10793 #define OP_SoftNull 27 /* synopsis: r[P1]=NULL */
10794 #define OP_Blob 28 /* synopsis: r[P2]=P4 (len=P1) */
10795 #define OP_Variable 29 /* synopsis: r[P2]=parameter(P1,P4) */
10796 #define OP_Move 30 /* synopsis: r[P2@P3]=r[P1@P3] */
10797 #define OP_Copy 31 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
10798 #define OP_SCopy 32 /* synopsis: r[P2]=r[P1] */
10799 #define OP_IntCopy 33 /* synopsis: r[P2]=r[P1] */
10800 #define OP_ResultRow 34 /* synopsis: output=r[P1@P2] */
10801 #define OP_CollSeq 35
10802 #define OP_Function0 36 /* synopsis: r[P3]=func(r[P2@P5]) */
10803 #define OP_Function 37 /* synopsis: r[P3]=func(r[P2@P5]) */
10804 #define OP_AddImm 38 /* synopsis: r[P1]=r[P1]+P2 */
10805 #define OP_MustBeInt 39
10806 #define OP_RealAffinity 40
10807 #define OP_Cast 41 /* synopsis: affinity(r[P1]) */
10808 #define OP_Permutation 42
10809 #define OP_Compare 43 /* synopsis: r[P1@P3] <-> r[P2@P3] */
10810 #define OP_Jump 44
10811 #define OP_Once 45
10812 #define OP_If 46
10813 #define OP_IfNot 47
10814 #define OP_Column 48 /* synopsis: r[P3]=PX */
10815 #define OP_Affinity 49 /* synopsis: affinity(r[P1@P2]) */
10816 #define OP_MakeRecord 50 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
10817 #define OP_Count 51 /* synopsis: r[P2]=count() */
10818 #define OP_ReadCookie 52
10819 #define OP_SetCookie 53
10820 #define OP_ReopenIdx 54 /* synopsis: root=P2 iDb=P3 */
10821 #define OP_OpenRead 55 /* synopsis: root=P2 iDb=P3 */
10822 #define OP_OpenWrite 56 /* synopsis: root=P2 iDb=P3 */
10823 #define OP_OpenAutoindex 57 /* synopsis: nColumn=P2 */
10824 #define OP_OpenEphemeral 58 /* synopsis: nColumn=P2 */
10825 #define OP_SorterOpen 59
10826 #define OP_SequenceTest 60 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
10827 #define OP_OpenPseudo 61 /* synopsis: P3 columns in r[P2] */
10828 #define OP_Close 62
10829 #define OP_ColumnsUsed 63
10830 #define OP_SeekLT 64 /* synopsis: key=r[P3@P4] */
10831 #define OP_SeekLE 65 /* synopsis: key=r[P3@P4] */
10832 #define OP_SeekGE 66 /* synopsis: key=r[P3@P4] */
10833 #define OP_SeekGT 67 /* synopsis: key=r[P3@P4] */
10834 #define OP_Seek 68 /* synopsis: intkey=r[P2] */
10835 #define OP_NoConflict 69 /* synopsis: key=r[P3@P4] */
10836 #define OP_NotFound 70 /* synopsis: key=r[P3@P4] */
10837 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
10838 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
10839 #define OP_Found 73 /* synopsis: key=r[P3@P4] */
10840 #define OP_NotExists 74 /* synopsis: intkey=r[P3] */
10841 #define OP_Sequence 75 /* synopsis: r[P2]=cursor[P1].ctr++ */
10842 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
10843 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
10844 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
10845 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
10846 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
10847 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
10848 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
10849 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
10850 #define OP_NewRowid 84 /* synopsis: r[P2]=rowid */
10851 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
10852 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
10853 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
10854 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
10855 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -10856,111 +10873,110 @@
10856 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
10857 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
10858 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
10859 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
10860 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
10861 #define OP_Insert 95 /* synopsis: intkey=r[P3] data=r[P2] */
10862 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
10863 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
10864 #define OP_InsertInt 98 /* synopsis: intkey=P3 data=r[P2] */
10865 #define OP_Delete 99
10866 #define OP_ResetCount 100
10867 #define OP_SorterCompare 101 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
10868 #define OP_SorterData 102 /* synopsis: r[P2]=data */
10869 #define OP_RowKey 103 /* synopsis: r[P2]=key */
10870 #define OP_RowData 104 /* synopsis: r[P2]=data */
10871 #define OP_Rowid 105 /* synopsis: r[P2]=rowid */
10872 #define OP_NullRow 106
10873 #define OP_Last 107
10874 #define OP_SorterSort 108
10875 #define OP_Sort 109
10876 #define OP_Rewind 110
10877 #define OP_SorterInsert 111
10878 #define OP_IdxInsert 112 /* synopsis: key=r[P2] */
10879 #define OP_IdxDelete 113 /* synopsis: key=r[P2@P3] */
10880 #define OP_IdxRowid 114 /* synopsis: r[P2]=rowid */
10881 #define OP_IdxLE 115 /* synopsis: key=r[P3@P4] */
10882 #define OP_IdxGT 116 /* synopsis: key=r[P3@P4] */
10883 #define OP_IdxLT 117 /* synopsis: key=r[P3@P4] */
10884 #define OP_IdxGE 118 /* synopsis: key=r[P3@P4] */
10885 #define OP_Destroy 119
10886 #define OP_Clear 120
10887 #define OP_ResetSorter 121
10888 #define OP_CreateIndex 122 /* synopsis: r[P2]=root iDb=P1 */
10889 #define OP_CreateTable 123 /* synopsis: r[P2]=root iDb=P1 */
10890 #define OP_ParseSchema 124
10891 #define OP_LoadAnalysis 125
10892 #define OP_DropTable 126
10893 #define OP_DropIndex 127
10894 #define OP_DropTrigger 128
10895 #define OP_IntegrityCk 129
10896 #define OP_RowSetAdd 130 /* synopsis: rowset(P1)=r[P2] */
10897 #define OP_RowSetRead 131 /* synopsis: r[P3]=rowset(P1) */
10898 #define OP_RowSetTest 132 /* synopsis: if r[P3] in rowset(P1) goto P2 */
10899 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
10900 #define OP_Program 134
10901 #define OP_Param 135
10902 #define OP_FkCounter 136 /* synopsis: fkctr[P1]+=P2 */
10903 #define OP_FkIfZero 137 /* synopsis: if fkctr[P1]==0 goto P2 */
10904 #define OP_MemMax 138 /* synopsis: r[P1]=max(r[P1],r[P2]) */
10905 #define OP_IfPos 139 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
10906 #define OP_SetIfNotPos 140 /* synopsis: if r[P1]<=0 then r[P2]=P3 */
10907 #define OP_IfNotZero 141 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
10908 #define OP_DecrJumpZero 142 /* synopsis: if (--r[P1])==0 goto P2 */
10909 #define OP_JumpZeroIncr 143 /* synopsis: if (r[P1]++)==0 ) goto P2 */
10910 #define OP_AggStep0 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10911 #define OP_AggStep 145 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10912 #define OP_AggFinal 146 /* synopsis: accum=r[P1] N=P2 */
10913 #define OP_IncrVacuum 147
10914 #define OP_Expire 148
10915 #define OP_TableLock 149 /* synopsis: iDb=P1 root=P2 write=P3 */
10916 #define OP_VBegin 150
10917 #define OP_VCreate 151
10918 #define OP_VDestroy 152
10919 #define OP_VOpen 153
10920 #define OP_VColumn 154 /* synopsis: r[P3]=vcolumn(P2) */
10921 #define OP_VNext 155
10922 #define OP_VRename 156
10923 #define OP_Pagecount 157
10924 #define OP_MaxPgcnt 158
10925 #define OP_Init 159 /* synopsis: Start at P2 */
10926 #define OP_CursorHint 160
10927 #define OP_Noop 161
10928 #define OP_Explain 162
10929
10930 /* Properties such as "out2" or "jump" that are specified in
10931 ** comments following the "case" for each opcode in the vdbe.c
10932 ** are encoded into bitvectors as follows:
10933 */
10934 #define OPFLG_JUMP 0x0001 /* jump: P2 holds jmp target */
10935 #define OPFLG_IN1 0x0002 /* in1: P1 is an input */
10936 #define OPFLG_IN2 0x0004 /* in2: P2 is an input */
10937 #define OPFLG_IN3 0x0008 /* in3: P3 is an input */
10938 #define OPFLG_OUT2 0x0010 /* out2: P2 is an output */
10939 #define OPFLG_OUT3 0x0020 /* out3: P3 is an output */
10940 #define OPFLG_INITIALIZER {\
10941 /* 0 */ 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01,\
10942 /* 8 */ 0x01, 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01,\
10943 /* 16 */ 0x02, 0x01, 0x02, 0x12, 0x03, 0x08, 0x00, 0x10,\
10944 /* 24 */ 0x10, 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00,\
10945 /* 32 */ 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03,\
10946 /* 40 */ 0x02, 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03,\
10947 /* 48 */ 0x00, 0x00, 0x00, 0x10, 0x10, 0x08, 0x00, 0x00,\
10948 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10949 /* 64 */ 0x09, 0x09, 0x09, 0x09, 0x04, 0x09, 0x09, 0x26,\
10950 /* 72 */ 0x26, 0x09, 0x09, 0x10, 0x03, 0x03, 0x0b, 0x0b,\
10951 /* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x10, 0x26, 0x26, 0x26,\
10952 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
10953 /* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
10954 /* 104 */ 0x00, 0x10, 0x00, 0x01, 0x01, 0x01, 0x01, 0x04,\
10955 /* 112 */ 0x04, 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10,\
10956 /* 120 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
10957 /* 128 */ 0x00, 0x00, 0x06, 0x23, 0x0b, 0x10, 0x01, 0x10,\
10958 /* 136 */ 0x00, 0x01, 0x04, 0x03, 0x06, 0x03, 0x03, 0x03,\
10959 /* 144 */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,\
10960 /* 152 */ 0x00, 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01,\
10961 /* 160 */ 0x00, 0x00, 0x00,}
10962
10963 /************** End of opcodes.h *********************************************/
10964 /************** Continuing where we left off in vdbe.h ***********************/
10965
10966 /*
@@ -10976,10 +10992,11 @@
10976 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
10977 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
10978 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
10979 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
10980 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
 
10981 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
10982 SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
10983 #else
10984 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
10985 #endif
@@ -11199,15 +11216,16 @@
11199 ** Flags for sqlite3PagerSetFlags()
11200 */
11201 #define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
11202 #define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
11203 #define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
11204 #define PAGER_SYNCHRONOUS_MASK 0x03 /* Mask for three values above */
11205 #define PAGER_FULLFSYNC 0x04 /* PRAGMA fullfsync=ON */
11206 #define PAGER_CKPT_FULLFSYNC 0x08 /* PRAGMA checkpoint_fullfsync=ON */
11207 #define PAGER_CACHESPILL 0x10 /* PRAGMA cache_spill=ON */
11208 #define PAGER_FLAGS_MASK 0x1c /* All above except SYNCHRONOUS */
 
11209
11210 /*
11211 ** The remainder of this file contains the declarations of the functions
11212 ** that make up the Pager sub-system API. See source code comments for
11213 ** a detailed description of each routine.
@@ -11963,12 +11981,12 @@
11963 ** is shared by multiple database connections. Therefore, while parsing
11964 ** schema information, the Lookaside.bEnabled flag is cleared so that
11965 ** lookaside allocations are not used to construct the schema objects.
11966 */
11967 struct Lookaside {
 
11968 u16 sz; /* Size of each buffer in bytes */
11969 u8 bEnabled; /* False to disable new lookaside allocations */
11970 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
11971 int nOut; /* Number of buffers currently checked out */
11972 int mxOut; /* Highwater mark for nOut */
11973 int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
11974 LookasideSlot *pFree; /* List of available buffers */
@@ -12047,10 +12065,11 @@
12047 u16 dbOptFlags; /* Flags to enable/disable optimizations */
12048 u8 enc; /* Text encoding */
12049 u8 autoCommit; /* The auto-commit flag. */
12050 u8 temp_store; /* 1: file 2: memory 0: default */
12051 u8 mallocFailed; /* True if we have seen a malloc failure */
 
12052 u8 dfltLockMode; /* Default locking-mode for attached dbs */
12053 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
12054 u8 suppressErr; /* Do not issue error messages if true */
12055 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
12056 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
@@ -12155,14 +12174,14 @@
12155 /*
12156 ** Possible values for the sqlite3.flags.
12157 */
12158 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
12159 #define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
12160 #define SQLITE_FullFSync 0x00000004 /* Use full fsync on the backend */
12161 #define SQLITE_CkptFullFSync 0x00000008 /* Use full fsync for checkpoint */
12162 #define SQLITE_CacheSpill 0x00000010 /* OK to spill pager cache */
12163 #define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
12164 #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
12165 #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
12166 /* DELETE, or UPDATE and return */
12167 /* the count using a callback. */
12168 #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
@@ -13510,11 +13529,11 @@
13510 /*
13511 ** During code generation of statements that do inserts into AUTOINCREMENT
13512 ** tables, the following information is attached to the Table.u.autoInc.p
13513 ** pointer of each autoincrement table to record some side information that
13514 ** the code generator needs. We have to keep per-table autoincrement
13515 ** information in case inserts are down within triggers. Triggers do not
13516 ** normally coordinate their activities, but we do need to coordinate the
13517 ** loading and saving of autoincrement information.
13518 */
13519 struct AutoincInfo {
13520 AutoincInfo *pNext; /* Next info block in a list of them all */
@@ -13602,10 +13621,11 @@
13602 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
13603 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
13604 u8 mayAbort; /* True if statement may throw an ABORT exception */
13605 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
13606 u8 okConstFactor; /* OK to factor out constants */
 
13607 int aTempReg[8]; /* Holding area for temporary registers */
13608 int nRangeReg; /* Size of the temporary register block */
13609 int iRangeReg; /* First register in temporary register block */
13610 int nErr; /* Number of errors seen */
13611 int nTab; /* Number of previously allocated VDBE cursors */
@@ -13716,23 +13736,26 @@
13716 };
13717
13718 /*
13719 ** Bitfield flags for P5 value in various opcodes.
13720 */
13721 #define OPFLAG_NCHANGE 0x01 /* Set to update db->nChange */
 
13722 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
13723 #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
13724 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
13725 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
13726 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
13727 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
13728 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
13729 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
13730 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
13731 #define OPFLAG_FORDELETE 0x08 /* OP_Open is opening for-delete csr */
13732 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
13733 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
 
 
13734
13735 /*
13736 * Each trigger present in the database schema is stored as an instance of
13737 * struct Trigger.
13738 *
@@ -13847,14 +13870,20 @@
13847 char *zText; /* The string collected so far */
13848 u32 nChar; /* Length of the string so far */
13849 u32 nAlloc; /* Amount of space allocated in zText */
13850 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
13851 u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
13852 u8 bMalloced; /* zText points to allocated space */
13853 };
13854 #define STRACCUM_NOMEM 1
13855 #define STRACCUM_TOOBIG 2
 
 
 
 
 
 
13856
13857 /*
13858 ** A pointer to this structure is used to communicate information
13859 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
13860 */
@@ -13958,10 +13987,11 @@
13958 int n; /* A counter */
13959 int iCur; /* A cursor number */
13960 SrcList *pSrcList; /* FROM clause */
13961 struct SrcCount *pSrcCount; /* Counting column references */
13962 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
 
13963 } u;
13964 };
13965
13966 /* Forward declarations */
13967 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -14027,10 +14057,17 @@
14027 SQLITE_PRIVATE int sqlite3CantopenError(int);
14028 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
14029 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
14030 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
14031
 
 
 
 
 
 
 
14032
14033 /*
14034 ** FTS4 is really an extension for FTS3. It is enabled using the
14035 ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
14036 ** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
@@ -14085,10 +14122,11 @@
14085 SQLITE_PRIVATE void sqlite3MallocEnd(void);
14086 SQLITE_PRIVATE void *sqlite3Malloc(u64);
14087 SQLITE_PRIVATE void *sqlite3MallocZero(u64);
14088 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
14089 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
 
14090 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
14091 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
14092 SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
14093 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
14094 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
@@ -14167,14 +14205,12 @@
14167 int nArg; /* Total number of arguments */
14168 int nUsed; /* Number of arguments used so far */
14169 sqlite3_value **apArg; /* The argument values */
14170 };
14171
14172 #define SQLITE_PRINTF_INTERNAL 0x01
14173 #define SQLITE_PRINTF_SQLFUNC 0x02
14174 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
14175 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
14176 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
14177 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
14178 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
14179 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
14180 #endif
@@ -14191,10 +14227,11 @@
14191
14192
14193 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
14194 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
14195 SQLITE_PRIVATE int sqlite3Dequote(char*);
 
14196 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
14197 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
14198 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
14199 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
14200 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
@@ -14403,11 +14440,11 @@
14403 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
14404 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
14405 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
14406 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
14407 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
14408 u8,u8,int,int*);
14409 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
14410 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
14411 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
14412 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
14413 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -14622,11 +14659,10 @@
14622 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
14623 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
14624 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
14625 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
14626 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
14627 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
14628 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
14629 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
14630 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
14631 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
14632 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
@@ -14638,10 +14674,12 @@
14638 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
14639 void (*)(sqlite3_context*,int,sqlite3_value **),
14640 void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
14641 FuncDestructor *pDestructor
14642 );
 
 
14643 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
14644 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
14645
14646 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
14647 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
@@ -15734,18 +15772,20 @@
15734 ** - On either an index or a table
15735 ** * A sorter
15736 ** * A virtual table
15737 ** * A one-row "pseudotable" stored in a single register
15738 */
 
15739 struct VdbeCursor {
15740 u8 eCurType; /* One of the CURTYPE_* values above */
15741 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
15742 u8 nullRow; /* True if pointing to a row with no data */
15743 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
15744 u8 isTable; /* True for rowid tables. False for indexes */
15745 #ifdef SQLITE_DEBUG
15746 u8 seekOp; /* Most recent seek operation on this cursor */
 
15747 #endif
15748 Bool isEphemeral:1; /* True for an ephemeral table */
15749 Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
15750 Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
15751 Pgno pgnoRoot; /* Root page of the open btree cursor */
@@ -15760,10 +15800,12 @@
15760 Btree *pBt; /* Separate file holding temporary table */
15761 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
15762 int seekResult; /* Result of previous sqlite3BtreeMoveto() */
15763 i64 seqCount; /* Sequence counter */
15764 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
 
 
15765 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
15766 u64 maskUsed; /* Mask of columns used by this cursor */
15767 #endif
15768
15769 /* Cached information about the header for the data record that the
@@ -15784,11 +15826,10 @@
15784 u32 aType[1]; /* Type values for all entries in the record */
15785 /* 2*nField extra array elements allocated for aType[], beyond the one
15786 ** static element declared in the structure. nField total array slots for
15787 ** aType[] and nField+1 array slots for aOffset[] */
15788 };
15789 typedef struct VdbeCursor VdbeCursor;
15790
15791 /*
15792 ** When a sub-program is executed (OP_Program), a structure of this type
15793 ** is allocated to store the current value of the program counter, as
15794 ** well as the current memory cell array and various other frame specific
@@ -15895,11 +15936,11 @@
15895 #define MEM_AffMask 0x001f /* Mask of affinity bits */
15896 #define MEM_RowSet 0x0020 /* Value is a RowSet object */
15897 #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
15898 #define MEM_Undefined 0x0080 /* Value is undefined */
15899 #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
15900 #define MEM_TypeMask 0x01ff /* Mask of type bits */
15901
15902
15903 /* Whenever Mem contains a valid string or blob representation, one of
15904 ** the following flags must be set to determine the memory management
15905 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
@@ -15909,14 +15950,21 @@
15909 #define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
15910 #define MEM_Static 0x0800 /* Mem.z points to a static string */
15911 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
15912 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
15913 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
 
15914 #ifdef SQLITE_OMIT_INCRBLOB
15915 #undef MEM_Zero
15916 #define MEM_Zero 0x0000
15917 #endif
 
 
 
 
 
 
15918
15919 /*
15920 ** Clear any existing type flags from a Mem and replace them with f
15921 */
15922 #define MemSetTypeFlag(p, f) \
@@ -16083,11 +16131,11 @@
16083 ** Function prototypes
16084 */
16085 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
16086 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
16087 void sqliteVdbePopStack(Vdbe*,int);
16088 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
16089 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
16090 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
16091 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
16092 #endif
16093 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
@@ -16129,12 +16177,10 @@
16129 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
16130 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
16131 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
16132 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
16133 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
16134 #define VdbeMemDynamic(X) \
16135 (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
16136 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
16137 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
16138 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
16139 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
16140 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
@@ -17492,11 +17538,11 @@
17492 z = zBuf;
17493 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
17494 sqlite3_result_error_toobig(context);
17495 return;
17496 }else{
17497 z = sqlite3DbMallocRaw(db, (int)n);
17498 if( z==0 ){
17499 sqlite3_result_error_nomem(context);
17500 return;
17501 }
17502 }
@@ -17707,18 +17753,40 @@
17707 */
17708 #define _SQLITE_OS_C_ 1
17709 /* #include "sqliteInt.h" */
17710 #undef _SQLITE_OS_C_
17711
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17712 /*
17713 ** The default SQLite sqlite3_vfs implementations do not allocate
17714 ** memory (actually, os_unix.c allocates a small amount of memory
17715 ** from within OsOpen()), but some third-party implementations may.
17716 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
17717 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
17718 **
17719 ** The following functions are instrumented for malloc() failure
17720 ** testing:
17721 **
17722 ** sqlite3OsRead()
17723 ** sqlite3OsWrite()
17724 ** sqlite3OsSync()
@@ -17800,12 +17868,12 @@
17800 */
17801 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
17802 #ifdef SQLITE_TEST
17803 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
17804 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
17805 ** is using a regular VFS, it is called after the corresponding
17806 ** transaction has been committed. Injecting a fault at this point
17807 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
17808 ** but the transaction is committed anyway.
17809 **
17810 ** The core must call OsFileControl() though, not OsFileControlHint(),
17811 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
@@ -17870,14 +17938,14 @@
17870 /*
17871 ** The next group of routines are convenience wrappers around the
17872 ** VFS methods.
17873 */
17874 SQLITE_PRIVATE int sqlite3OsOpen(
17875 sqlite3_vfs *pVfs,
17876 const char *zPath,
17877 sqlite3_file *pFile,
17878 int flags,
17879 int *pFlagsOut
17880 ){
17881 int rc;
17882 DO_OS_MALLOC_TEST(0);
17883 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
@@ -17892,22 +17960,22 @@
17892 DO_OS_MALLOC_TEST(0);
17893 assert( dirSync==0 || dirSync==1 );
17894 return pVfs->xDelete(pVfs, zPath, dirSync);
17895 }
17896 SQLITE_PRIVATE int sqlite3OsAccess(
17897 sqlite3_vfs *pVfs,
17898 const char *zPath,
17899 int flags,
17900 int *pResOut
17901 ){
17902 DO_OS_MALLOC_TEST(0);
17903 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
17904 }
17905 SQLITE_PRIVATE int sqlite3OsFullPathname(
17906 sqlite3_vfs *pVfs,
17907 const char *zPath,
17908 int nPathOut,
17909 char *zPathOut
17910 ){
17911 DO_OS_MALLOC_TEST(0);
17912 zPathOut[0] = 0;
17913 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
@@ -17949,13 +18017,13 @@
17949 }
17950 return rc;
17951 }
17952
17953 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
17954 sqlite3_vfs *pVfs,
17955 const char *zFile,
17956 sqlite3_file **ppFile,
17957 int flags,
17958 int *pOutFlags
17959 ){
17960 int rc = SQLITE_NOMEM;
17961 sqlite3_file *pFile;
@@ -20081,15 +20149,15 @@
20081 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
20082 while( ALWAYS(iLogsize<LOGMAX) ){
20083 int iBuddy;
20084 if( (iBlock>>iLogsize) & 1 ){
20085 iBuddy = iBlock - size;
 
20086 }else{
20087 iBuddy = iBlock + size;
 
20088 }
20089 assert( iBuddy>=0 );
20090 if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
20091 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
20092 memsys5Unlink(iBuddy, iLogsize);
20093 iLogsize++;
20094 if( iBuddy<iBlock ){
20095 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
@@ -21174,12 +21242,12 @@
21174 ** Macros for performance tracing. Normally turned off. Only works
21175 ** on i486 hardware.
21176 */
21177 #ifdef SQLITE_PERFORMANCE_TRACE
21178
21179 /*
21180 ** hwtime.h contains inline assembler code for implementing
21181 ** high-performance timing routines.
21182 */
21183 /************** Include hwtime.h in the middle of os_common.h ****************/
21184 /************** Begin file hwtime.h ******************************************/
21185 /*
@@ -21285,18 +21353,18 @@
21285 /*
21286 ** If we compile with the SQLITE_TEST macro set, then the following block
21287 ** of code will give us the ability to simulate a disk I/O error. This
21288 ** is used for testing the I/O recovery logic.
21289 */
21290 #ifdef SQLITE_TEST
21291 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
21292 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
21293 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
21294 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
21295 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
21296 SQLITE_API int sqlite3_diskfull_pending = 0;
21297 SQLITE_API int sqlite3_diskfull = 0;
21298 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
21299 #define SimulateIOError(CODE) \
21300 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
21301 || sqlite3_io_error_pending-- == 1 ) \
21302 { local_ioerr(); CODE; }
@@ -21318,21 +21386,21 @@
21318 }
21319 #else
21320 #define SimulateIOErrorBenign(X)
21321 #define SimulateIOError(A)
21322 #define SimulateDiskfullError(A)
21323 #endif
21324
21325 /*
21326 ** When testing, keep a count of the number of open files.
21327 */
21328 #ifdef SQLITE_TEST
21329 SQLITE_API int sqlite3_open_file_count = 0;
21330 #define OpenCounter(X) sqlite3_open_file_count+=(X)
21331 #else
21332 #define OpenCounter(X)
21333 #endif
21334
21335 #endif /* !defined(_OS_COMMON_H_) */
21336
21337 /************** End of os_common.h *******************************************/
21338 /************** Continuing where we left off in mutex_w32.c ******************/
@@ -22386,14 +22454,28 @@
22386 /*
22387 ** Allocate and zero memory. If the allocation fails, make
22388 ** the mallocFailed flag in the connection pointer.
22389 */
22390 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
22391 void *p = sqlite3DbMallocRaw(db, n);
22392 if( p ){
22393 memset(p, 0, (size_t)n);
22394 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22395 return p;
22396 }
22397
22398 /*
22399 ** Allocate memory, either lookaside (if possible) or heap.
@@ -22411,71 +22493,77 @@
22411 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
22412 ** if( b ) a[10] = 9;
22413 **
22414 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
22415 ** that all prior mallocs (ex: "a") worked too.
 
 
 
22416 */
22417 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n);
22418 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
22419 assert( db==0 || sqlite3_mutex_held(db->mutex) );
22420 assert( db==0 || db->pnBytesFreed==0 );
 
 
 
 
 
22421 #ifndef SQLITE_OMIT_LOOKASIDE
22422 if( db ){
22423 LookasideSlot *pBuf;
22424 if( db->mallocFailed ){
22425 return 0;
22426 }
22427 if( db->lookaside.bEnabled ){
22428 if( n>db->lookaside.sz ){
22429 db->lookaside.anStat[1]++;
22430 }else if( (pBuf = db->lookaside.pFree)==0 ){
22431 db->lookaside.anStat[2]++;
22432 }else{
22433 db->lookaside.pFree = pBuf->pNext;
22434 db->lookaside.nOut++;
22435 db->lookaside.anStat[0]++;
22436 if( db->lookaside.nOut>db->lookaside.mxOut ){
22437 db->lookaside.mxOut = db->lookaside.nOut;
22438 }
22439 return (void*)pBuf;
22440 }
22441 }
 
22442 }
22443 #else
22444 if( db && db->mallocFailed ){
 
 
 
22445 return 0;
22446 }
22447 #endif
22448 return dbMallocRawFinish(db, n);
22449 }
22450 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
22451 void *p = sqlite3Malloc(n);
22452 if( !p && db ){
22453 db->mallocFailed = 1;
22454 }
22455 sqlite3MemdebugSetType(p,
22456 (db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
22457 return p;
22458 }
22459
22460 /*
22461 ** Resize the block of memory pointed to by p to n bytes. If the
22462 ** resize fails, set the mallocFailed flag in the connection object.
22463 */
22464 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
 
 
 
 
 
 
 
22465 void *pNew = 0;
22466 assert( db!=0 );
22467 assert( sqlite3_mutex_held(db->mutex) );
22468 if( db->mallocFailed==0 ){
22469 if( p==0 ){
22470 return sqlite3DbMallocRaw(db, n);
22471 }
22472 if( isLookaside(db, p) ){
22473 if( n<=db->lookaside.sz ){
22474 return p;
22475 }
22476 pNew = sqlite3DbMallocRaw(db, n);
22477 if( pNew ){
22478 memcpy(pNew, p, db->lookaside.sz);
22479 sqlite3DbFree(db, p);
22480 }
22481 }else{
@@ -22482,14 +22570,14 @@
22482 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
22483 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
22484 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
22485 pNew = sqlite3_realloc64(p, n);
22486 if( !pNew ){
22487 db->mallocFailed = 1;
22488 }
22489 sqlite3MemdebugSetType(pNew,
22490 (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
22491 }
22492 }
22493 return pNew;
22494 }
22495
@@ -22527,15 +22615,16 @@
22527 }
22528 return zNew;
22529 }
22530 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
22531 char *zNew;
 
22532 if( z==0 ){
22533 return 0;
22534 }
22535 assert( (n&0x7fffffff)==n );
22536 zNew = sqlite3DbMallocRaw(db, n+1);
22537 if( zNew ){
22538 memcpy(zNew, z, (size_t)n);
22539 zNew[n] = 0;
22540 }
22541 return zNew;
@@ -22546,16 +22635,48 @@
22546 */
22547 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
22548 sqlite3DbFree(db, *pz);
22549 *pz = sqlite3DbStrDup(db, zNew);
22550 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22551
22552 /*
22553 ** Take actions at the end of an API call to indicate an OOM error
22554 */
22555 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
22556 db->mallocFailed = 0;
22557 sqlite3Error(db, SQLITE_NOMEM);
22558 return SQLITE_NOMEM;
22559 }
22560
22561 /*
@@ -22756,11 +22877,10 @@
22756 /*
22757 ** Render a string given by "fmt" into the StrAccum object.
22758 */
22759 SQLITE_PRIVATE void sqlite3VXPrintf(
22760 StrAccum *pAccum, /* Accumulate results here */
22761 u32 bFlags, /* SQLITE_PRINTF_* flags */
22762 const char *fmt, /* Format string */
22763 va_list ap /* arguments */
22764 ){
22765 int c; /* Next character in the format string */
22766 char *bufpt; /* Pointer to the conversion buffer */
@@ -22796,15 +22916,15 @@
22796 #endif
22797 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
22798 char buf[etBUFSIZE]; /* Conversion buffer */
22799
22800 bufpt = 0;
22801 if( bFlags ){
22802 if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
22803 pArgList = va_arg(ap, PrintfArguments*);
22804 }
22805 useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
22806 }else{
22807 bArgList = useIntern = 0;
22808 }
22809 for(; (c=(*fmt))!=0; ++fmt){
22810 if( c!='%' ){
@@ -23351,13 +23471,13 @@
23351 if( p->mxAlloc==0 ){
23352 N = p->nAlloc - p->nChar - 1;
23353 setStrAccumError(p, STRACCUM_TOOBIG);
23354 return N;
23355 }else{
23356 char *zOld = p->bMalloced ? p->zText : 0;
23357 i64 szNew = p->nChar;
23358 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23359 szNew += N + 1;
23360 if( szNew+p->nChar<=p->mxAlloc ){
23361 /* Force exponential buffer size growth as long as it does not overflow,
23362 ** to avoid having to call this routine too often */
23363 szNew += p->nChar;
@@ -23374,14 +23494,14 @@
23374 }else{
23375 zNew = sqlite3_realloc64(zOld, p->nAlloc);
23376 }
23377 if( zNew ){
23378 assert( p->zText!=0 || p->nChar==0 );
23379 if( !p->bMalloced && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
23380 p->zText = zNew;
23381 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
23382 p->bMalloced = 1;
23383 }else{
23384 sqlite3StrAccumReset(p);
23385 setStrAccumError(p, STRACCUM_NOMEM);
23386 return 0;
23387 }
@@ -23395,11 +23515,11 @@
23395 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
23396 testcase( p->nChar + (i64)N > 0x7fffffff );
23397 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
23398 return;
23399 }
23400 assert( (p->zText==p->zBase)==(p->bMalloced==0) );
23401 while( (N--)>0 ) p->zText[p->nChar++] = c;
23402 }
23403
23404 /*
23405 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
@@ -23413,11 +23533,11 @@
23413 N = sqlite3StrAccumEnlarge(p, N);
23414 if( N>0 ){
23415 memcpy(&p->zText[p->nChar], z, N);
23416 p->nChar += N;
23417 }
23418 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23419 }
23420
23421 /*
23422 ** Append N bytes of text from z to the StrAccum object. Increase the
23423 ** size of the memory allocation for StrAccum if necessary.
@@ -23449,17 +23569,17 @@
23449 ** Return a pointer to the resulting string. Return a NULL
23450 ** pointer if any kind of error was encountered.
23451 */
23452 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
23453 if( p->zText ){
23454 assert( (p->zText==p->zBase)==(p->bMalloced==0) );
23455 p->zText[p->nChar] = 0;
23456 if( p->mxAlloc>0 && p->bMalloced==0 ){
23457 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
23458 if( p->zText ){
23459 memcpy(p->zText, p->zBase, p->nChar+1);
23460 p->bMalloced = 1;
23461 }else{
23462 setStrAccumError(p, STRACCUM_NOMEM);
23463 }
23464 }
23465 }
@@ -23468,14 +23588,14 @@
23468
23469 /*
23470 ** Reset an StrAccum string. Reclaim all malloced memory.
23471 */
23472 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
23473 assert( (p->zText==0 || p->zText==p->zBase)==(p->bMalloced==0) );
23474 if( p->bMalloced ){
23475 sqlite3DbFree(p->db, p->zText);
23476 p->bMalloced = 0;
23477 }
23478 p->zText = 0;
23479 }
23480
23481 /*
@@ -23497,11 +23617,11 @@
23497 p->db = db;
23498 p->nChar = 0;
23499 p->nAlloc = n;
23500 p->mxAlloc = mx;
23501 p->accError = 0;
23502 p->bMalloced = 0;
23503 }
23504
23505 /*
23506 ** Print into memory obtained from sqliteMalloc(). Use the internal
23507 ** %-conversion extensions.
@@ -23511,14 +23631,15 @@
23511 char zBase[SQLITE_PRINT_BUF_SIZE];
23512 StrAccum acc;
23513 assert( db!=0 );
23514 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
23515 db->aLimit[SQLITE_LIMIT_LENGTH]);
23516 sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
 
23517 z = sqlite3StrAccumFinish(&acc);
23518 if( acc.accError==STRACCUM_NOMEM ){
23519 db->mallocFailed = 1;
23520 }
23521 return z;
23522 }
23523
23524 /*
@@ -23551,11 +23672,11 @@
23551 #endif
23552 #ifndef SQLITE_OMIT_AUTOINIT
23553 if( sqlite3_initialize() ) return 0;
23554 #endif
23555 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
23556 sqlite3VXPrintf(&acc, 0, zFormat, ap);
23557 z = sqlite3StrAccumFinish(&acc);
23558 return z;
23559 }
23560
23561 /*
@@ -23596,11 +23717,11 @@
23596 if( zBuf ) zBuf[0] = 0;
23597 return zBuf;
23598 }
23599 #endif
23600 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
23601 sqlite3VXPrintf(&acc, 0, zFormat, ap);
23602 return sqlite3StrAccumFinish(&acc);
23603 }
23604 SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
23605 char *z;
23606 va_list ap;
@@ -23627,11 +23748,11 @@
23627 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
23628 StrAccum acc; /* String accumulator */
23629 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
23630
23631 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
23632 sqlite3VXPrintf(&acc, 0, zFormat, ap);
23633 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
23634 sqlite3StrAccumFinish(&acc));
23635 }
23636
23637 /*
@@ -23656,11 +23777,11 @@
23656 va_list ap;
23657 StrAccum acc;
23658 char zBuf[500];
23659 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
23660 va_start(ap,zFormat);
23661 sqlite3VXPrintf(&acc, 0, zFormat, ap);
23662 va_end(ap);
23663 sqlite3StrAccumFinish(&acc);
23664 fprintf(stdout,"%s", zBuf);
23665 fflush(stdout);
23666 }
@@ -23669,14 +23790,14 @@
23669
23670 /*
23671 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
23672 ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
23673 */
23674 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
23675 va_list ap;
23676 va_start(ap,zFormat);
23677 sqlite3VXPrintf(p, bFlags, zFormat, ap);
23678 va_end(ap);
23679 }
23680
23681 /************** End of printf.c **********************************************/
23682 /************** Begin file treeview.c ****************************************/
@@ -23743,11 +23864,11 @@
23743 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
23744 }
23745 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
23746 }
23747 va_start(ap, zFormat);
23748 sqlite3VXPrintf(&acc, 0, zFormat, ap);
23749 va_end(ap);
23750 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
23751 sqlite3StrAccumFinish(&acc);
23752 fprintf(stdout,"%s", zBuf);
23753 fflush(stdout);
@@ -23778,21 +23899,21 @@
23778 for(i=0; i<pWith->nCte; i++){
23779 StrAccum x;
23780 char zLine[1000];
23781 const struct Cte *pCte = &pWith->a[i];
23782 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23783 sqlite3XPrintf(&x, 0, "%s", pCte->zName);
23784 if( pCte->pCols && pCte->pCols->nExpr>0 ){
23785 char cSep = '(';
23786 int j;
23787 for(j=0; j<pCte->pCols->nExpr; j++){
23788 sqlite3XPrintf(&x, 0, "%c%s", cSep, pCte->pCols->a[j].zName);
23789 cSep = ',';
23790 }
23791 sqlite3XPrintf(&x, 0, ")");
23792 }
23793 sqlite3XPrintf(&x, 0, " AS");
23794 sqlite3StrAccumFinish(&x);
23795 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
23796 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
23797 sqlite3TreeViewPop(pView);
23798 }
@@ -23839,24 +23960,24 @@
23839 for(i=0; i<p->pSrc->nSrc; i++){
23840 struct SrcList_item *pItem = &p->pSrc->a[i];
23841 StrAccum x;
23842 char zLine[100];
23843 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23844 sqlite3XPrintf(&x, 0, "{%d,*}", pItem->iCursor);
23845 if( pItem->zDatabase ){
23846 sqlite3XPrintf(&x, 0, " %s.%s", pItem->zDatabase, pItem->zName);
23847 }else if( pItem->zName ){
23848 sqlite3XPrintf(&x, 0, " %s", pItem->zName);
23849 }
23850 if( pItem->pTab ){
23851 sqlite3XPrintf(&x, 0, " tabname=%Q", pItem->pTab->zName);
23852 }
23853 if( pItem->zAlias ){
23854 sqlite3XPrintf(&x, 0, " (AS %s)", pItem->zAlias);
23855 }
23856 if( pItem->fg.jointype & JT_LEFT ){
23857 sqlite3XPrintf(&x, 0, " LEFT-JOIN");
23858 }
23859 sqlite3StrAccumFinish(&x);
23860 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
23861 if( pItem->pSelect ){
23862 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
@@ -24899,11 +25020,11 @@
24899 *z = 0;
24900 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
24901
24902 c = pMem->flags;
24903 sqlite3VdbeMemRelease(pMem);
24904 pMem->flags = MEM_Str|MEM_Term|(c&MEM_AffMask);
24905 pMem->enc = desiredEnc;
24906 pMem->z = (char*)zOut;
24907 pMem->zMalloc = pMem->z;
24908 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
24909
@@ -25349,10 +25470,18 @@
25349 }
25350 }
25351 z[j] = 0;
25352 return j;
25353 }
 
 
 
 
 
 
 
 
25354
25355 /* Convenient short-hand */
25356 #define UpperToLower sqlite3UpperToLower
25357
25358 /*
@@ -26258,11 +26387,11 @@
26258 */
26259 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
26260 char *zBlob;
26261 int i;
26262
26263 zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
26264 n--;
26265 if( zBlob ){
26266 for(i=0; i<n; i+=2){
26267 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
26268 }
@@ -26796,95 +26925,96 @@
26796 # define OpHelp(X) "\0" X
26797 #else
26798 # define OpHelp(X)
26799 #endif
26800 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
26801 static const char *const azName[] = { "?",
26802 /* 1 */ "Savepoint" OpHelp(""),
26803 /* 2 */ "AutoCommit" OpHelp(""),
26804 /* 3 */ "Transaction" OpHelp(""),
26805 /* 4 */ "SorterNext" OpHelp(""),
26806 /* 5 */ "PrevIfOpen" OpHelp(""),
26807 /* 6 */ "NextIfOpen" OpHelp(""),
26808 /* 7 */ "Prev" OpHelp(""),
26809 /* 8 */ "Next" OpHelp(""),
26810 /* 9 */ "Checkpoint" OpHelp(""),
26811 /* 10 */ "JournalMode" OpHelp(""),
26812 /* 11 */ "Vacuum" OpHelp(""),
26813 /* 12 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
26814 /* 13 */ "VUpdate" OpHelp("data=r[P3@P2]"),
26815 /* 14 */ "Goto" OpHelp(""),
26816 /* 15 */ "Gosub" OpHelp(""),
26817 /* 16 */ "Return" OpHelp(""),
26818 /* 17 */ "InitCoroutine" OpHelp(""),
26819 /* 18 */ "EndCoroutine" OpHelp(""),
 
26820 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
26821 /* 20 */ "Yield" OpHelp(""),
26822 /* 21 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
26823 /* 22 */ "Halt" OpHelp(""),
26824 /* 23 */ "Integer" OpHelp("r[P2]=P1"),
26825 /* 24 */ "Int64" OpHelp("r[P2]=P4"),
26826 /* 25 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
26827 /* 26 */ "Null" OpHelp("r[P2..P3]=NULL"),
26828 /* 27 */ "SoftNull" OpHelp("r[P1]=NULL"),
26829 /* 28 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
26830 /* 29 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
26831 /* 30 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
26832 /* 31 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
26833 /* 32 */ "SCopy" OpHelp("r[P2]=r[P1]"),
26834 /* 33 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
26835 /* 34 */ "ResultRow" OpHelp("output=r[P1@P2]"),
26836 /* 35 */ "CollSeq" OpHelp(""),
26837 /* 36 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
26838 /* 37 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
26839 /* 38 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
26840 /* 39 */ "MustBeInt" OpHelp(""),
26841 /* 40 */ "RealAffinity" OpHelp(""),
26842 /* 41 */ "Cast" OpHelp("affinity(r[P1])"),
26843 /* 42 */ "Permutation" OpHelp(""),
26844 /* 43 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
26845 /* 44 */ "Jump" OpHelp(""),
26846 /* 45 */ "Once" OpHelp(""),
26847 /* 46 */ "If" OpHelp(""),
26848 /* 47 */ "IfNot" OpHelp(""),
26849 /* 48 */ "Column" OpHelp("r[P3]=PX"),
26850 /* 49 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
26851 /* 50 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
26852 /* 51 */ "Count" OpHelp("r[P2]=count()"),
26853 /* 52 */ "ReadCookie" OpHelp(""),
26854 /* 53 */ "SetCookie" OpHelp(""),
26855 /* 54 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
26856 /* 55 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
26857 /* 56 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
26858 /* 57 */ "OpenAutoindex" OpHelp("nColumn=P2"),
26859 /* 58 */ "OpenEphemeral" OpHelp("nColumn=P2"),
26860 /* 59 */ "SorterOpen" OpHelp(""),
26861 /* 60 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
26862 /* 61 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
26863 /* 62 */ "Close" OpHelp(""),
26864 /* 63 */ "ColumnsUsed" OpHelp(""),
26865 /* 64 */ "SeekLT" OpHelp("key=r[P3@P4]"),
26866 /* 65 */ "SeekLE" OpHelp("key=r[P3@P4]"),
26867 /* 66 */ "SeekGE" OpHelp("key=r[P3@P4]"),
26868 /* 67 */ "SeekGT" OpHelp("key=r[P3@P4]"),
26869 /* 68 */ "Seek" OpHelp("intkey=r[P2]"),
26870 /* 69 */ "NoConflict" OpHelp("key=r[P3@P4]"),
26871 /* 70 */ "NotFound" OpHelp("key=r[P3@P4]"),
26872 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
26873 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
26874 /* 73 */ "Found" OpHelp("key=r[P3@P4]"),
26875 /* 74 */ "NotExists" OpHelp("intkey=r[P3]"),
26876 /* 75 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
26877 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
26878 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
26879 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
26880 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
26881 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
26882 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
26883 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
26884 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
26885 /* 84 */ "NewRowid" OpHelp("r[P2]=rowid"),
26886 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
26887 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
26888 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
26889 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
26890 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -26891,78 +27021,77 @@
26891 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
26892 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
26893 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
26894 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
26895 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
26896 /* 95 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
26897 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
26898 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
26899 /* 98 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
26900 /* 99 */ "Delete" OpHelp(""),
26901 /* 100 */ "ResetCount" OpHelp(""),
26902 /* 101 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
26903 /* 102 */ "SorterData" OpHelp("r[P2]=data"),
26904 /* 103 */ "RowKey" OpHelp("r[P2]=key"),
26905 /* 104 */ "RowData" OpHelp("r[P2]=data"),
26906 /* 105 */ "Rowid" OpHelp("r[P2]=rowid"),
26907 /* 106 */ "NullRow" OpHelp(""),
26908 /* 107 */ "Last" OpHelp(""),
26909 /* 108 */ "SorterSort" OpHelp(""),
26910 /* 109 */ "Sort" OpHelp(""),
26911 /* 110 */ "Rewind" OpHelp(""),
26912 /* 111 */ "SorterInsert" OpHelp(""),
26913 /* 112 */ "IdxInsert" OpHelp("key=r[P2]"),
26914 /* 113 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
26915 /* 114 */ "IdxRowid" OpHelp("r[P2]=rowid"),
26916 /* 115 */ "IdxLE" OpHelp("key=r[P3@P4]"),
26917 /* 116 */ "IdxGT" OpHelp("key=r[P3@P4]"),
26918 /* 117 */ "IdxLT" OpHelp("key=r[P3@P4]"),
26919 /* 118 */ "IdxGE" OpHelp("key=r[P3@P4]"),
26920 /* 119 */ "Destroy" OpHelp(""),
26921 /* 120 */ "Clear" OpHelp(""),
26922 /* 121 */ "ResetSorter" OpHelp(""),
26923 /* 122 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
26924 /* 123 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
26925 /* 124 */ "ParseSchema" OpHelp(""),
26926 /* 125 */ "LoadAnalysis" OpHelp(""),
26927 /* 126 */ "DropTable" OpHelp(""),
26928 /* 127 */ "DropIndex" OpHelp(""),
26929 /* 128 */ "DropTrigger" OpHelp(""),
26930 /* 129 */ "IntegrityCk" OpHelp(""),
26931 /* 130 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
26932 /* 131 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
26933 /* 132 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
26934 /* 133 */ "Real" OpHelp("r[P2]=P4"),
26935 /* 134 */ "Program" OpHelp(""),
26936 /* 135 */ "Param" OpHelp(""),
26937 /* 136 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
26938 /* 137 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
26939 /* 138 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
26940 /* 139 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
26941 /* 140 */ "SetIfNotPos" OpHelp("if r[P1]<=0 then r[P2]=P3"),
26942 /* 141 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
26943 /* 142 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
26944 /* 143 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
26945 /* 144 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
26946 /* 145 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
26947 /* 146 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
26948 /* 147 */ "IncrVacuum" OpHelp(""),
26949 /* 148 */ "Expire" OpHelp(""),
26950 /* 149 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
26951 /* 150 */ "VBegin" OpHelp(""),
26952 /* 151 */ "VCreate" OpHelp(""),
26953 /* 152 */ "VDestroy" OpHelp(""),
26954 /* 153 */ "VOpen" OpHelp(""),
26955 /* 154 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
26956 /* 155 */ "VNext" OpHelp(""),
26957 /* 156 */ "VRename" OpHelp(""),
26958 /* 157 */ "Pagecount" OpHelp(""),
26959 /* 158 */ "MaxPgcnt" OpHelp(""),
26960 /* 159 */ "Init" OpHelp("Start at P2"),
26961 /* 160 */ "CursorHint" OpHelp(""),
26962 /* 161 */ "Noop" OpHelp(""),
26963 /* 162 */ "Explain" OpHelp(""),
26964 };
26965 return azName[i];
26966 }
26967 #endif
26968
@@ -27117,10 +27246,15 @@
27117 /*
27118 ** Maximum supported path-length.
27119 */
27120 #define MAX_PATHNAME 512
27121
 
 
 
 
 
27122 /* Always cast the getpid() return type for compatibility with
27123 ** kernel modules in VxWorks. */
27124 #define osGetpid(X) (pid_t)getpid()
27125
27126 /*
@@ -27269,12 +27403,12 @@
27269 ** Macros for performance tracing. Normally turned off. Only works
27270 ** on i486 hardware.
27271 */
27272 #ifdef SQLITE_PERFORMANCE_TRACE
27273
27274 /*
27275 ** hwtime.h contains inline assembler code for implementing
27276 ** high-performance timing routines.
27277 */
27278 /************** Include hwtime.h in the middle of os_common.h ****************/
27279 /************** Begin file hwtime.h ******************************************/
27280 /*
@@ -27380,18 +27514,18 @@
27380 /*
27381 ** If we compile with the SQLITE_TEST macro set, then the following block
27382 ** of code will give us the ability to simulate a disk I/O error. This
27383 ** is used for testing the I/O recovery logic.
27384 */
27385 #ifdef SQLITE_TEST
27386 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
27387 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
27388 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
27389 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
27390 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
27391 SQLITE_API int sqlite3_diskfull_pending = 0;
27392 SQLITE_API int sqlite3_diskfull = 0;
27393 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
27394 #define SimulateIOError(CODE) \
27395 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
27396 || sqlite3_io_error_pending-- == 1 ) \
27397 { local_ioerr(); CODE; }
@@ -27413,21 +27547,21 @@
27413 }
27414 #else
27415 #define SimulateIOErrorBenign(X)
27416 #define SimulateIOError(A)
27417 #define SimulateDiskfullError(A)
27418 #endif
27419
27420 /*
27421 ** When testing, keep a count of the number of open files.
27422 */
27423 #ifdef SQLITE_TEST
27424 SQLITE_API int sqlite3_open_file_count = 0;
27425 #define OpenCounter(X) sqlite3_open_file_count+=(X)
27426 #else
27427 #define OpenCounter(X)
27428 #endif
27429
27430 #endif /* !defined(_OS_COMMON_H_) */
27431
27432 /************** End of os_common.h *******************************************/
27433 /************** Continuing where we left off in os_unix.c ********************/
@@ -27641,10 +27775,16 @@
27641 #else
27642 { "readlink", (sqlite3_syscall_ptr)0, 0 },
27643 #endif
27644 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
27645
 
 
 
 
 
 
27646
27647 }; /* End of the overrideable system calls */
27648
27649
27650 /*
@@ -33042,16 +33182,11 @@
33042 #ifndef SQLITE_DISABLE_DIRSYNC
33043 if( (dirSync & 1)!=0 ){
33044 int fd;
33045 rc = osOpenDirectory(zPath, &fd);
33046 if( rc==SQLITE_OK ){
33047 #if OS_VXWORKS
33048 if( fsync(fd)==-1 )
33049 #else
33050 if( fsync(fd) )
33051 #endif
33052 {
33053 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
33054 }
33055 robust_close(0, fd, __LINE__);
33056 }else{
33057 assert( rc==SQLITE_CANTOPEN );
@@ -33093,10 +33228,36 @@
33093 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
33094 }
33095 return SQLITE_OK;
33096 }
33097
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33098
33099 /*
33100 ** Turn a relative pathname into a full pathname. The relative path
33101 ** is stored as a nul-terminated string in the buffer pointed to by
33102 ** zPath.
@@ -33109,73 +33270,85 @@
33109 sqlite3_vfs *pVfs, /* Pointer to vfs object */
33110 const char *zPath, /* Possibly relative input path */
33111 int nOut, /* Size of output buffer in bytes */
33112 char *zOut /* Output buffer */
33113 ){
 
 
 
 
33114 int nByte;
 
 
 
 
 
 
33115
33116 /* It's odd to simulate an io-error here, but really this is just
33117 ** using the io-error infrastructure to test that SQLite handles this
33118 ** function failing. This function could fail if, for example, the
33119 ** current working directory has been unlinked.
33120 */
33121 SimulateIOError( return SQLITE_ERROR );
33122
33123 assert( pVfs->mxPathname==MAX_PATHNAME );
33124 UNUSED_PARAMETER(pVfs);
33125
33126 #if defined(HAVE_READLINK)
33127 /* Attempt to resolve the path as if it were a symbolic link. If it is
33128 ** a symbolic link, the resolved path is stored in buffer zOut[]. Or, if
33129 ** the identified file is not a symbolic link or does not exist, then
33130 ** zPath is copied directly into zOut. Either way, nByte is left set to
33131 ** the size of the string copied into zOut[] in bytes. */
33132 nByte = osReadlink(zPath, zOut, nOut-1);
33133 if( nByte<0 ){
33134 if( errno!=EINVAL && errno!=ENOENT ){
33135 return unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zPath);
33136 }
33137 sqlite3_snprintf(nOut, zOut, "%s", zPath);
33138 nByte = sqlite3Strlen30(zOut);
33139 }else{
33140 zOut[nByte] = '\0';
33141 }
33142 #endif
33143
33144 /* If buffer zOut[] now contains an absolute path there is nothing more
33145 ** to do. If it contains a relative path, do the following:
33146 **
33147 ** * move the relative path string so that it is at the end of th
33148 ** zOut[] buffer.
33149 ** * Call getcwd() to read the path of the current working directory
33150 ** into the start of the zOut[] buffer.
33151 ** * Append a '/' character to the cwd string and move the
33152 ** relative path back within the buffer so that it immediately
33153 ** follows the '/'.
33154 **
33155 ** This code is written so that if the combination of the CWD and relative
33156 ** path are larger than the allocated size of zOut[] the CWD is silently
33157 ** truncated to make it fit. This is Ok, as SQLite refuses to open any
33158 ** file for which this function returns a full path larger than (nOut-8)
33159 ** bytes in size. */
33160 testcase( nByte==nOut-5 );
33161 testcase( nByte==nOut-4 );
33162 if( zOut[0]!='/' && nByte<nOut-4 ){
33163 int nCwd;
33164 int nRem = nOut-nByte-1;
33165 memmove(&zOut[nRem], zOut, nByte+1);
33166 zOut[nRem-1] = '\0';
33167 if( osGetcwd(zOut, nRem-1)==0 ){
33168 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
33169 }
33170 nCwd = sqlite3Strlen30(zOut);
33171 assert( nCwd<=nRem-1 );
33172 zOut[nCwd] = '/';
33173 memmove(&zOut[nCwd+1], &zOut[nRem], nByte+1);
33174 }
33175
33176 return SQLITE_OK;
 
 
33177 }
33178
33179
33180 #ifndef SQLITE_OMIT_LOAD_EXTENSION
33181 /*
@@ -33353,11 +33526,11 @@
33353 #endif
33354 UNUSED_PARAMETER(NotUsed);
33355 return rc;
33356 }
33357
33358 #if 0 /* Not used */
33359 /*
33360 ** Find the current time (in Universal Coordinated Time). Write the
33361 ** current time and date as a Julian Day number into *prNow and
33362 ** return 0. Return 1 if the time and date cannot be found.
33363 */
@@ -33371,11 +33544,11 @@
33371 }
33372 #else
33373 # define unixCurrentTime 0
33374 #endif
33375
33376 #if 0 /* Not used */
33377 /*
33378 ** We added the xGetLastError() method with the intention of providing
33379 ** better low-level error messages when operating-system problems come up
33380 ** during SQLite operation. But so far, none of that has been implemented
33381 ** in the core. So this routine is never called. For now, it is merely
@@ -34053,11 +34226,11 @@
34053 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
34054 }
34055 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
34056 robust_ftruncate(conchFile->h, writeSize);
34057 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
34058 fsync(conchFile->h);
34059 /* If we created a new conch file (not just updated the contents of a
34060 ** valid conch file), try to match the permissions of the database
34061 */
34062 if( rc==SQLITE_OK && createConch ){
34063 struct stat buf;
@@ -34670,11 +34843,11 @@
34670 };
34671 unsigned int i; /* Loop counter */
34672
34673 /* Double-check that the aSyscall[] array has been constructed
34674 ** correctly. See ticket [bb3a86e890c8e96ab] */
34675 assert( ArraySize(aSyscall)==27 );
34676
34677 /* Register all VFSes defined in the aVfs[] array */
34678 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
34679 sqlite3_vfs_register(&aVfs[i], i==0);
34680 }
@@ -34753,12 +34926,12 @@
34753 ** Macros for performance tracing. Normally turned off. Only works
34754 ** on i486 hardware.
34755 */
34756 #ifdef SQLITE_PERFORMANCE_TRACE
34757
34758 /*
34759 ** hwtime.h contains inline assembler code for implementing
34760 ** high-performance timing routines.
34761 */
34762 /************** Include hwtime.h in the middle of os_common.h ****************/
34763 /************** Begin file hwtime.h ******************************************/
34764 /*
@@ -34864,18 +35037,18 @@
34864 /*
34865 ** If we compile with the SQLITE_TEST macro set, then the following block
34866 ** of code will give us the ability to simulate a disk I/O error. This
34867 ** is used for testing the I/O recovery logic.
34868 */
34869 #ifdef SQLITE_TEST
34870 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
34871 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
34872 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
34873 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
34874 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
34875 SQLITE_API int sqlite3_diskfull_pending = 0;
34876 SQLITE_API int sqlite3_diskfull = 0;
34877 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
34878 #define SimulateIOError(CODE) \
34879 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
34880 || sqlite3_io_error_pending-- == 1 ) \
34881 { local_ioerr(); CODE; }
@@ -34897,21 +35070,21 @@
34897 }
34898 #else
34899 #define SimulateIOErrorBenign(X)
34900 #define SimulateIOError(A)
34901 #define SimulateDiskfullError(A)
34902 #endif
34903
34904 /*
34905 ** When testing, keep a count of the number of open files.
34906 */
34907 #ifdef SQLITE_TEST
34908 SQLITE_API int sqlite3_open_file_count = 0;
34909 #define OpenCounter(X) sqlite3_open_file_count+=(X)
34910 #else
34911 #define OpenCounter(X)
34912 #endif
34913
34914 #endif /* !defined(_OS_COMMON_H_) */
34915
34916 /************** End of os_common.h *******************************************/
34917 /************** Continuing where we left off in os_win.c *********************/
@@ -34970,10 +35143,14 @@
34970
34971 #ifndef NTDDI_WINBLUE
34972 # define NTDDI_WINBLUE 0x06030000
34973 #endif
34974
 
 
 
 
34975 /*
34976 ** Check to see if the GetVersionEx[AW] functions are deprecated on the
34977 ** target system. GetVersionEx was first deprecated in Win8.1.
34978 */
34979 #ifndef SQLITE_WIN32_GETVERSIONEX
@@ -34982,10 +35159,23 @@
34982 # else
34983 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
34984 # endif
34985 #endif
34986
 
 
 
 
 
 
 
 
 
 
 
 
 
34987 /*
34988 ** This constant should already be defined (in the "WinDef.h" SDK file).
34989 */
34990 #ifndef MAX_PATH
34991 # define MAX_PATH (260)
@@ -35388,12 +35578,13 @@
35388 #endif
35389
35390 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
35391 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
35392
35393 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
35394 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0))
 
35395 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
35396 #else
35397 { "CreateFileMappingA", (SYSCALL)0, 0 },
35398 #endif
35399
@@ -35619,22 +35810,21 @@
35619 { "GetTickCount", (SYSCALL)0, 0 },
35620 #endif
35621
35622 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
35623
35624 #if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
35625 SQLITE_WIN32_GETVERSIONEX
35626 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
35627 #else
35628 { "GetVersionExA", (SYSCALL)0, 0 },
35629 #endif
35630
35631 #define osGetVersionExA ((BOOL(WINAPI*)( \
35632 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
35633
35634 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
35635 defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
35636 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
35637 #else
35638 { "GetVersionExW", (SYSCALL)0, 0 },
35639 #endif
35640
@@ -36241,11 +36431,11 @@
36241 ** this routine is used to determine if the host is Win95/98/ME or
36242 ** WinNT/2K/XP so that we will know whether or not we can safely call
36243 ** the LockFileEx() API.
36244 */
36245
36246 #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
36247 # define osIsNT() (1)
36248 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
36249 # define osIsNT() (1)
36250 #elif !defined(SQLITE_WIN32_HAS_WIDE)
36251 # define osIsNT() (0)
@@ -36262,11 +36452,11 @@
36262 /*
36263 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
36264 ** kernel.
36265 */
36266 return 1;
36267 #elif defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
36268 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
36269 #if defined(SQLITE_WIN32_HAS_ANSI)
36270 OSVERSIONINFOA sInfo;
36271 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
36272 osGetVersionExA(&sInfo);
@@ -38846,11 +39036,11 @@
38846 );
38847 #elif defined(SQLITE_WIN32_HAS_WIDE)
38848 hMap = osCreateFileMappingW(pShmNode->hFile.h,
38849 NULL, PAGE_READWRITE, 0, nByte, NULL
38850 );
38851 #elif defined(SQLITE_WIN32_HAS_ANSI)
38852 hMap = osCreateFileMappingA(pShmNode->hFile.h,
38853 NULL, PAGE_READWRITE, 0, nByte, NULL
38854 );
38855 #endif
38856 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
@@ -39002,11 +39192,11 @@
39002 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
39003 #elif defined(SQLITE_WIN32_HAS_WIDE)
39004 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
39005 (DWORD)((nMap>>32) & 0xffffffff),
39006 (DWORD)(nMap & 0xffffffff), NULL);
39007 #elif defined(SQLITE_WIN32_HAS_ANSI)
39008 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
39009 (DWORD)((nMap>>32) & 0xffffffff),
39010 (DWORD)(nMap & 0xffffffff), NULL);
39011 #endif
39012 if( pFd->hMap==NULL ){
@@ -43066,11 +43256,11 @@
43066 */
43067 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
43068 assert( p!=0 );
43069 if( p->nFresh==0 ){
43070 struct RowSetChunk *pNew;
43071 pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
43072 if( pNew==0 ){
43073 return 0;
43074 }
43075 pNew->pNextChunk = p->pChunk;
43076 p->pChunk = pNew;
@@ -43973,10 +44163,24 @@
43973 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
43974 ** This could conceivably cause corruption following a power failure on
43975 ** such a system. This is currently an undocumented limit.
43976 */
43977 #define MAX_SECTOR_SIZE 0x10000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43978
43979 /*
43980 ** An instance of the following structure is allocated for each active
43981 ** savepoint and statement transaction in the system. All such structures
43982 ** are stored in the Pager.aSavepoint[] array, which is allocated and
@@ -44169,10 +44373,11 @@
44169 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
44170 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
44171 u8 useJournal; /* Use a rollback journal on this file */
44172 u8 noSync; /* Do not sync the journal if true */
44173 u8 fullSync; /* Do extra syncs of the journal for robustness */
 
44174 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
44175 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
44176 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
44177 u8 tempFile; /* zFilename is a temporary or immutable file */
44178 u8 noLock; /* Do not lock (except in WAL mode) */
@@ -45529,11 +45734,11 @@
45529 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
45530 || pPager->journalMode==PAGER_JOURNALMODE_WAL
45531 );
45532 sqlite3OsClose(pPager->jfd);
45533 if( bDelete ){
45534 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
45535 }
45536 }
45537 }
45538
45539 #ifdef SQLITE_CHECK_PAGES
@@ -47035,13 +47240,19 @@
47035 SQLITE_PRIVATE void sqlite3PagerSetFlags(
47036 Pager *pPager, /* The pager to set safety level for */
47037 unsigned pgFlags /* Various flags */
47038 ){
47039 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
47040 assert( level>=1 && level<=3 );
47041 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
47042 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
 
 
 
 
 
 
47043 if( pPager->noSync ){
47044 pPager->syncFlags = 0;
47045 pPager->ckptSyncFlags = 0;
47046 }else if( pgFlags & PAGER_FULLFSYNC ){
47047 pPager->syncFlags = SQLITE_SYNC_FULL;
@@ -48342,15 +48553,21 @@
48342 pPager->readOnly = (u8)readOnly;
48343 assert( useJournal || pPager->tempFile );
48344 pPager->noSync = pPager->tempFile;
48345 if( pPager->noSync ){
48346 assert( pPager->fullSync==0 );
 
48347 assert( pPager->syncFlags==0 );
48348 assert( pPager->walSyncFlags==0 );
48349 assert( pPager->ckptSyncFlags==0 );
48350 }else{
48351 pPager->fullSync = 1;
 
 
 
 
 
48352 pPager->syncFlags = SQLITE_SYNC_NORMAL;
48353 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
48354 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
48355 }
48356 /* pPager->pFirst = 0; */
@@ -53972,14 +54189,20 @@
53972 u32 iWrite = 0;
53973 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
53974 assert( rc==SQLITE_OK || iWrite==0 );
53975 if( iWrite>=iFirst ){
53976 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
 
53977 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
53978 pWal->iReCksum = iWrite;
53979 }
53980 rc = sqlite3OsWrite(pWal->pWalFd, p->pData, szPage, iOff);
 
 
 
 
 
53981 if( rc ) return rc;
53982 p->flags &= ~PGHDR_WAL_APPEND;
53983 continue;
53984 }
53985 }
@@ -57680,11 +57903,10 @@
57680 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
57681 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
57682 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
57683 if( pBt->mutex==0 ){
57684 rc = SQLITE_NOMEM;
57685 db->mallocFailed = 0;
57686 goto btree_open_out;
57687 }
57688 }
57689 sqlite3_mutex_enter(mutexShared);
57690 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
@@ -59391,17 +59613,17 @@
59391 ** iTable. If a read-only cursor is requested, it is assumed that
59392 ** the caller already has at least a read-only transaction open
59393 ** on the database already. If a write-cursor is requested, then
59394 ** the caller is assumed to have an open write transaction.
59395 **
59396 ** If wrFlag==0, then the cursor can only be used for reading.
59397 ** If wrFlag==1, then the cursor can be used for reading or for
59398 ** writing if other conditions for writing are also met. These
59399 ** are the conditions that must be met in order for writing to
59400 ** be allowed:
59401 **
59402 ** 1: The cursor must have been opened with wrFlag==1
59403 **
59404 ** 2: Other database connections that share the same pager cache
59405 ** but which are not in the READ_UNCOMMITTED state may not have
59406 ** cursors open with wrFlag==0 on the same table. Otherwise
59407 ** the changes made by this write cursor would be visible to
@@ -59408,10 +59630,20 @@
59408 ** the read cursors in the other database connection.
59409 **
59410 ** 3: The database must be writable (not on read-only media)
59411 **
59412 ** 4: There must be an active transaction.
 
 
 
 
 
 
 
 
 
 
59413 **
59414 ** No checking is done to make sure that page iTable really is the
59415 ** root page of a b-tree. If it is not, then the cursor acquired
59416 ** will not work correctly.
59417 **
@@ -61481,11 +61713,11 @@
61481 */
61482 #if SQLITE_DEBUG
61483 {
61484 CellInfo info;
61485 pPage->xParseCell(pPage, pCell, &info);
61486 assert( nHeader=(int)(info.pPayload - pCell) );
61487 assert( info.nKey==nKey );
61488 assert( *pnSize == info.nSize );
61489 assert( spaceLeft == info.nLocal );
61490 }
61491 #endif
@@ -63140,12 +63372,12 @@
63140 int rc = SQLITE_OK;
63141 const int nMin = pCur->pBt->usableSize * 2 / 3;
63142 u8 aBalanceQuickSpace[13];
63143 u8 *pFree = 0;
63144
63145 TESTONLY( int balance_quick_called = 0 );
63146 TESTONLY( int balance_deeper_called = 0 );
63147
63148 do {
63149 int iPage = pCur->iPage;
63150 MemPage *pPage = pCur->apPage[iPage];
63151
@@ -63154,11 +63386,12 @@
63154 /* The root page of the b-tree is overfull. In this case call the
63155 ** balance_deeper() function to create a new child for the root-page
63156 ** and copy the current contents of the root-page to it. The
63157 ** next iteration of the do-loop will balance the child page.
63158 */
63159 assert( (balance_deeper_called++)==0 );
 
63160 rc = balance_deeper(pPage, &pCur->apPage[1]);
63161 if( rc==SQLITE_OK ){
63162 pCur->iPage = 1;
63163 pCur->aiIdx[0] = 0;
63164 pCur->aiIdx[1] = 0;
@@ -63193,11 +63426,12 @@
63193 ** The purpose of the following assert() is to check that only a
63194 ** single call to balance_quick() is made for each call to this
63195 ** function. If this were not verified, a subtle bug involving reuse
63196 ** of the aBalanceQuickSpace[] might sneak in.
63197 */
63198 assert( (balance_quick_called++)==0 );
 
63199 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
63200 }else
63201 #endif
63202 {
63203 /* In this case, call balance_nonroot() to redistribute cells
@@ -63424,35 +63658,45 @@
63424 }
63425
63426 /*
63427 ** Delete the entry that the cursor is pointing to.
63428 **
63429 ** If the second parameter is zero, then the cursor is left pointing at an
63430 ** arbitrary location after the delete. If it is non-zero, then the cursor
63431 ** is left in a state such that the next call to BtreeNext() or BtreePrev()
63432 ** moves it to the same row as it would if the call to BtreeDelete() had
63433 ** been omitted.
 
 
 
 
 
 
 
 
63434 */
63435 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, int bPreserve){
63436 Btree *p = pCur->pBtree;
63437 BtShared *pBt = p->pBt;
63438 int rc; /* Return code */
63439 MemPage *pPage; /* Page to delete cell from */
63440 unsigned char *pCell; /* Pointer to cell to delete */
63441 int iCellIdx; /* Index of cell to delete */
63442 int iCellDepth; /* Depth of node containing pCell */
63443 u16 szCell; /* Size of the cell being deleted */
63444 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
 
63445
63446 assert( cursorOwnsBtShared(pCur) );
63447 assert( pBt->inTransaction==TRANS_WRITE );
63448 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
63449 assert( pCur->curFlags & BTCF_WriteFlag );
63450 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
63451 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
63452 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63453 assert( pCur->eState==CURSOR_VALID );
 
63454
63455 iCellDepth = pCur->iPage;
63456 iCellIdx = pCur->aiIdx[iCellDepth];
63457 pPage = pCur->apPage[iCellDepth];
63458 pCell = findCell(pPage, iCellIdx);
@@ -63561,11 +63805,11 @@
63561 }
63562
63563 if( rc==SQLITE_OK ){
63564 if( bSkipnext ){
63565 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
63566 assert( pPage==pCur->apPage[pCur->iPage] );
63567 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
63568 pCur->eState = CURSOR_SKIPNEXT;
63569 if( iCellIdx>=pPage->nCell ){
63570 pCur->skipNext = -1;
63571 pCur->aiIdx[iCellDepth] = pPage->nCell-1;
@@ -64147,13 +64391,13 @@
64147 va_start(ap, zFormat);
64148 if( pCheck->errMsg.nChar ){
64149 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
64150 }
64151 if( pCheck->zPfx ){
64152 sqlite3XPrintf(&pCheck->errMsg, 0, pCheck->zPfx, pCheck->v1, pCheck->v2);
64153 }
64154 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
64155 va_end(ap);
64156 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
64157 pCheck->mallocFailed = 1;
64158 }
64159 }
@@ -64650,11 +64894,12 @@
64650 char zErr[100];
64651 VVA_ONLY( int nRef );
64652
64653 sqlite3BtreeEnter(p);
64654 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
64655 assert( (nRef = sqlite3PagerRefcount(pBt->pPager))>=0 );
 
64656 sCheck.pBt = pBt;
64657 sCheck.pPager = pBt->pPager;
64658 sCheck.nPage = btreePagecount(sCheck.pBt);
64659 sCheck.mxErr = mxErr;
64660 sCheck.nErr = 0;
@@ -64663,10 +64908,11 @@
64663 sCheck.v1 = 0;
64664 sCheck.v2 = 0;
64665 sCheck.aPgRef = 0;
64666 sCheck.heap = 0;
64667 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
 
64668 if( sCheck.nPage==0 ){
64669 goto integrity_ck_cleanup;
64670 }
64671
64672 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
@@ -65939,10 +66185,11 @@
65939 ** in pMem->z is discarded.
65940 */
65941 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
65942 assert( sqlite3VdbeCheckMemInvariants(pMem) );
65943 assert( (pMem->flags&MEM_RowSet)==0 );
 
65944
65945 /* If the bPreserve flag is set to true, then the memory cell must already
65946 ** contain a valid string or blob value. */
65947 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
65948 testcase( bPreserve && pMem->z==0 );
@@ -66542,11 +66789,11 @@
66542 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
66543 sqlite3 *db = pMem->db;
66544 assert( db!=0 );
66545 assert( (pMem->flags & MEM_RowSet)==0 );
66546 sqlite3VdbeMemRelease(pMem);
66547 pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
66548 if( db->mallocFailed ){
66549 pMem->flags = MEM_Null;
66550 pMem->szMalloc = 0;
66551 }else{
66552 assert( pMem->zMalloc );
@@ -67204,11 +67451,11 @@
67204
67205 *ppVal = pVal;
67206 return rc;
67207
67208 no_mem:
67209 db->mallocFailed = 1;
67210 sqlite3DbFree(db, zVal);
67211 assert( *ppVal==0 );
67212 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
67213 if( pCtx==0 ) sqlite3ValueFree(pVal);
67214 #else
@@ -67263,11 +67510,11 @@
67263 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
67264 nSerial = sqlite3VarintLen(iSerial);
67265 db = sqlite3_context_db_handle(context);
67266
67267 nRet = 1 + nSerial + nVal;
67268 aRet = sqlite3DbMallocRaw(db, nRet);
67269 if( aRet==0 ){
67270 sqlite3_result_error_nomem(context);
67271 }else{
67272 aRet[0] = nSerial+1;
67273 putVarint32(&aRet[1], iSerial);
@@ -67715,11 +67962,11 @@
67715 int i;
67716 VdbeOp *pOp;
67717
67718 i = p->nOp;
67719 assert( p->magic==VDBE_MAGIC_INIT );
67720 assert( op>0 && op<0xff );
67721 if( p->pParse->nOpAlloc<=i ){
67722 return growOp3(p, op, p1, p2, p3);
67723 }
67724 p->nOp++;
67725 pOp = &p->aOp[i];
@@ -67833,11 +68080,11 @@
67833 int p2, /* The P2 operand */
67834 int p3, /* The P3 operand */
67835 const u8 *zP4, /* The P4 operand */
67836 int p4type /* P4 operand type */
67837 ){
67838 char *p4copy = sqlite3DbMallocRaw(sqlite3VdbeDb(p), 8);
67839 if( p4copy ) memcpy(p4copy, zP4, 8);
67840 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
67841 }
67842
67843 /*
@@ -67867,10 +68114,25 @@
67867 ){
67868 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
67869 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
67870 return addr;
67871 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67872
67873 /*
67874 ** Create a new symbolic label for an instruction that has yet to be
67875 ** coded. The symbolic label is really just a negative number. The
67876 ** label can be used as the P2 value of an operation. Later, when
@@ -68078,11 +68340,11 @@
68078 p->readOnly = 1;
68079 p->bIsReader = 0;
68080 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
68081 u8 opcode = pOp->opcode;
68082
68083 /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
68084 ** cases from this switch! */
68085 switch( opcode ){
68086 case OP_Transaction: {
68087 if( pOp->p2!=0 ) p->readOnly = 0;
68088 /* fall thru */
@@ -68190,10 +68452,13 @@
68190 }
68191
68192 /*
68193 ** Add a whole list of operations to the operation stack. Return a
68194 ** pointer to the first operation inserted.
 
 
 
68195 */
68196 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
68197 Vdbe *p, /* Add opcodes to the prepared statement */
68198 int nOp, /* Number of opcodes to add */
68199 VdbeOpList const *aOp, /* The opcodes to be added */
@@ -68210,10 +68475,13 @@
68210 for(i=0; i<nOp; i++, aOp++, pOut++){
68211 pOut->opcode = aOp->opcode;
68212 pOut->p1 = aOp->p1;
68213 pOut->p2 = aOp->p2;
68214 assert( aOp->p2>=0 );
 
 
 
68215 pOut->p3 = aOp->p3;
68216 pOut->p4type = P4_NOTUSED;
68217 pOut->p4.p = 0;
68218 pOut->p5 = 0;
68219 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -68661,32 +68929,31 @@
68661 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
68662 /*
68663 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
68664 ** that can be displayed in the P4 column of EXPLAIN output.
68665 */
68666 static int displayP4Expr(int nTemp, char *zTemp, Expr *pExpr){
68667 const char *zOp = 0;
68668 int n;
68669 switch( pExpr->op ){
68670 case TK_STRING:
68671 sqlite3_snprintf(nTemp, zTemp, "%Q", pExpr->u.zToken);
68672 break;
68673 case TK_INTEGER:
68674 sqlite3_snprintf(nTemp, zTemp, "%d", pExpr->u.iValue);
68675 break;
68676 case TK_NULL:
68677 sqlite3_snprintf(nTemp, zTemp, "NULL");
68678 break;
68679 case TK_REGISTER: {
68680 sqlite3_snprintf(nTemp, zTemp, "r[%d]", pExpr->iTable);
68681 break;
68682 }
68683 case TK_COLUMN: {
68684 if( pExpr->iColumn<0 ){
68685 sqlite3_snprintf(nTemp, zTemp, "rowid");
68686 }else{
68687 sqlite3_snprintf(nTemp, zTemp, "c%d", (int)pExpr->iColumn);
68688 }
68689 break;
68690 }
68691 case TK_LT: zOp = "LT"; break;
68692 case TK_LE: zOp = "LE"; break;
@@ -68714,25 +68981,23 @@
68714 case TK_NOT: zOp = "NOT"; break;
68715 case TK_ISNULL: zOp = "ISNULL"; break;
68716 case TK_NOTNULL: zOp = "NOTNULL"; break;
68717
68718 default:
68719 sqlite3_snprintf(nTemp, zTemp, "%s", "expr");
68720 break;
68721 }
68722
68723 if( zOp ){
68724 sqlite3_snprintf(nTemp, zTemp, "%s(", zOp);
68725 n = sqlite3Strlen30(zTemp);
68726 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pLeft);
68727 if( n<nTemp-1 && pExpr->pRight ){
68728 zTemp[n++] = ',';
68729 n += displayP4Expr(nTemp-n, zTemp+n, pExpr->pRight);
68730 }
68731 sqlite3_snprintf(nTemp-n, zTemp+n, ")");
68732 }
68733 return sqlite3Strlen30(zTemp);
68734 }
68735 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
68736
68737
68738 #if VDBE_DISPLAY_P4
@@ -68740,107 +69005,100 @@
68740 ** Compute a string that describes the P4 parameter for an opcode.
68741 ** Use zTemp for any required temporary buffer space.
68742 */
68743 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
68744 char *zP4 = zTemp;
 
68745 assert( nTemp>=20 );
 
68746 switch( pOp->p4type ){
68747 case P4_KEYINFO: {
68748 int i, j;
68749 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
68750 assert( pKeyInfo->aSortOrder!=0 );
68751 sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
68752 i = sqlite3Strlen30(zTemp);
68753 for(j=0; j<pKeyInfo->nField; j++){
68754 CollSeq *pColl = pKeyInfo->aColl[j];
68755 const char *zColl = pColl ? pColl->zName : "nil";
68756 int n = sqlite3Strlen30(zColl);
68757 if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
68758 zColl = "B";
68759 n = 1;
68760 }
68761 if( i+n>nTemp-7 ){
68762 memcpy(&zTemp[i],",...",4);
68763 i += 4;
68764 break;
68765 }
68766 zTemp[i++] = ',';
68767 if( pKeyInfo->aSortOrder[j] ){
68768 zTemp[i++] = '-';
68769 }
68770 memcpy(&zTemp[i], zColl, n+1);
68771 i += n;
68772 }
68773 zTemp[i++] = ')';
68774 zTemp[i] = 0;
68775 assert( i<nTemp );
68776 break;
68777 }
68778 #ifdef SQLITE_ENABLE_CURSOR_HINTS
68779 case P4_EXPR: {
68780 displayP4Expr(nTemp, zTemp, pOp->p4.pExpr);
68781 break;
68782 }
68783 #endif
68784 case P4_COLLSEQ: {
68785 CollSeq *pColl = pOp->p4.pColl;
68786 sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
68787 break;
68788 }
68789 case P4_FUNCDEF: {
68790 FuncDef *pDef = pOp->p4.pFunc;
68791 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
68792 break;
68793 }
68794 #ifdef SQLITE_DEBUG
68795 case P4_FUNCCTX: {
68796 FuncDef *pDef = pOp->p4.pCtx->pFunc;
68797 sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
68798 break;
68799 }
68800 #endif
68801 case P4_INT64: {
68802 sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
68803 break;
68804 }
68805 case P4_INT32: {
68806 sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
68807 break;
68808 }
68809 case P4_REAL: {
68810 sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
68811 break;
68812 }
68813 case P4_MEM: {
68814 Mem *pMem = pOp->p4.pMem;
68815 if( pMem->flags & MEM_Str ){
68816 zP4 = pMem->z;
68817 }else if( pMem->flags & MEM_Int ){
68818 sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
68819 }else if( pMem->flags & MEM_Real ){
68820 sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->u.r);
68821 }else if( pMem->flags & MEM_Null ){
68822 sqlite3_snprintf(nTemp, zTemp, "NULL");
68823 }else{
68824 assert( pMem->flags & MEM_Blob );
68825 zP4 = "(blob)";
68826 }
68827 break;
68828 }
68829 #ifndef SQLITE_OMIT_VIRTUALTABLE
68830 case P4_VTAB: {
68831 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
68832 sqlite3_snprintf(nTemp, zTemp, "vtab:%p", pVtab);
68833 break;
68834 }
68835 #endif
68836 case P4_INTARRAY: {
68837 sqlite3_snprintf(nTemp, zTemp, "intarray");
 
 
 
 
 
 
 
 
68838 break;
68839 }
68840 case P4_SUBPROGRAM: {
68841 sqlite3_snprintf(nTemp, zTemp, "program");
68842 break;
68843 }
68844 case P4_ADVANCE: {
68845 zTemp[0] = 0;
68846 break;
@@ -68851,10 +69109,11 @@
68851 zP4 = zTemp;
68852 zTemp[0] = 0;
68853 }
68854 }
68855 }
 
68856 assert( zP4!=0 );
68857 return zP4;
68858 }
68859 #endif /* VDBE_DISPLAY_P4 */
68860
@@ -68970,11 +69229,10 @@
68970 */
68971 static void releaseMemArray(Mem *p, int N){
68972 if( p && N ){
68973 Mem *pEnd = &p[N];
68974 sqlite3 *db = p->db;
68975 u8 malloc_failed = db->mallocFailed;
68976 if( db->pnBytesFreed ){
68977 do{
68978 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
68979 }while( (++p)<pEnd );
68980 return;
@@ -69006,11 +69264,10 @@
69006 p->szMalloc = 0;
69007 }
69008
69009 p->flags = MEM_Undefined;
69010 }while( (++p)<pEnd );
69011 db->mallocFailed = malloc_failed;
69012 }
69013 }
69014
69015 /*
69016 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
@@ -69067,11 +69324,11 @@
69067 p->pResultSet = 0;
69068
69069 if( p->rc==SQLITE_NOMEM ){
69070 /* This happens if a malloc() inside a call to sqlite3_column_text() or
69071 ** sqlite3_column_text16() failed. */
69072 db->mallocFailed = 1;
69073 return SQLITE_ERROR;
69074 }
69075
69076 /* When the number of output rows reaches nRow, that means the
69077 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
@@ -69265,45 +69522,47 @@
69265 sqlite3IoTrace("SQL %s\n", z);
69266 }
69267 }
69268 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
69269
69270 /*
69271 ** Allocate space from a fixed size buffer and return a pointer to
69272 ** that space. If insufficient space is available, return NULL.
69273 **
69274 ** The pBuf parameter is the initial value of a pointer which will
69275 ** receive the new memory. pBuf is normally NULL. If pBuf is not
69276 ** NULL, it means that memory space has already been allocated and that
69277 ** this routine should not allocate any new memory. When pBuf is not
69278 ** NULL simply return pBuf. Only allocate new memory space when pBuf
69279 ** is NULL.
69280 **
69281 ** nByte is the number of bytes of space needed.
69282 **
69283 ** pFrom points to *pnFrom bytes of available space. New space is allocated
69284 ** from the end of the pFrom buffer and *pnFrom is decremented.
69285 **
69286 ** *pnNeeded is a counter of the number of bytes of space that have failed
69287 ** to allocate. If there is insufficient space in pFrom to satisfy the
69288 ** request, then increment *pnNeeded by the amount of the request.
 
 
 
 
69289 */
69290 static void *allocSpace(
69291 void *pBuf, /* Where return pointer will be stored */
69292 int nByte, /* Number of bytes to allocate */
69293 u8 *pFrom, /* Memory available for allocation */
69294 int *pnFrom, /* IN/OUT: Space available at pFrom */
69295 int *pnNeeded /* If allocation cannot be made, increment *pnByte */
69296 ){
69297 assert( EIGHT_BYTE_ALIGNMENT(pFrom) );
69298 if( pBuf==0 ){
69299 nByte = ROUND8(nByte);
69300 if( nByte <= *pnFrom ){
69301 *pnFrom -= nByte;
69302 pBuf = &pFrom[*pnFrom];
69303 }else{
69304 *pnNeeded += nByte;
69305 }
69306 }
69307 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
69308 return pBuf;
69309 }
@@ -69332,11 +69591,10 @@
69332 }
69333 #endif
69334 p->pc = -1;
69335 p->rc = SQLITE_OK;
69336 p->errorAction = OE_Abort;
69337 p->magic = VDBE_MAGIC_RUN;
69338 p->nChange = 0;
69339 p->cacheCtr = 1;
69340 p->minWriteFileFormat = 255;
69341 p->iStatement = 0;
69342 p->nFkConstraint = 0;
@@ -69375,13 +69633,11 @@
69375 int nMem; /* Number of VM memory registers */
69376 int nCursor; /* Number of cursors required */
69377 int nArg; /* Number of arguments in subprograms */
69378 int nOnce; /* Number of OP_Once instructions */
69379 int n; /* Loop counter */
69380 int nFree; /* Available free space */
69381 u8 *zCsr; /* Memory available for allocation */
69382 int nByte; /* How much extra memory is needed */
69383
69384 assert( p!=0 );
69385 assert( p->nOp>0 );
69386 assert( pParse!=0 );
69387 assert( p->magic==VDBE_MAGIC_INIT );
@@ -69395,69 +69651,64 @@
69395 nOnce = pParse->nOnce;
69396 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
69397
69398 /* For each cursor required, also allocate a memory cell. Memory
69399 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
69400 ** the vdbe program. Instead they are used to allocate space for
69401 ** VdbeCursor/BtCursor structures. The blob of memory associated with
69402 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
69403 ** stores the blob of memory associated with cursor 1, etc.
69404 **
69405 ** See also: allocateCursor().
69406 */
69407 nMem += nCursor;
69408
69409 /* zCsr will initially point to nFree bytes of unused space at the
69410 ** end of the opcode array, p->aOp. The computation of nFree is
69411 ** conservative - it might be smaller than the true number of free
69412 ** bytes, but never larger. nFree must be a multiple of 8 - it is
69413 ** rounded down if is not.
69414 */
69415 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode space used */
69416 zCsr = &((u8*)p->aOp)[n]; /* Unused opcode space */
69417 assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
69418 nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused space */
69419 assert( nFree>=0 );
69420 if( nFree>0 ){
69421 memset(zCsr, 0, nFree);
69422 assert( EIGHT_BYTE_ALIGNMENT(&zCsr[nFree]) );
69423 }
69424
69425 resolveP2Values(p, &nArg);
69426 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
69427 if( pParse->explain && nMem<10 ){
69428 nMem = 10;
69429 }
69430 p->expired = 0;
69431
69432 /* Memory for registers, parameters, cursor, etc, is allocated in two
69433 ** passes. On the first pass, we try to reuse unused space at the
69434 ** end of the opcode array. If we are unable to satisfy all memory
69435 ** requirements by reusing the opcode array tail, then the second
69436 ** pass will fill in the rest using a fresh allocation.
69437 **
69438 ** This two-pass approach that reuses as much memory as possible from
69439 ** the leftover space at the end of the opcode array can significantly
69440 ** reduce the amount of memory held by a prepared statement.
69441 */
69442 do {
69443 nByte = 0;
69444 p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), zCsr, &nFree, &nByte);
69445 p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), zCsr, &nFree, &nByte);
69446 p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), zCsr, &nFree, &nByte);
69447 p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
69448 zCsr, &nFree, &nByte);
69449 p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, zCsr, &nFree, &nByte);
69450 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69451 p->anExec = allocSpace(p->anExec, p->nOp*sizeof(i64), zCsr, &nFree, &nByte);
69452 #endif
69453 if( nByte ){
69454 p->pFree = sqlite3DbMallocZero(db, nByte);
69455 }
69456 zCsr = p->pFree;
69457 nFree = nByte;
69458 }while( nByte && !db->mallocFailed );
69459
69460 p->nCursor = nCursor;
69461 p->nOnceFlag = nOnce;
69462 if( p->aVar ){
69463 p->nVar = (ynVar)nVar;
@@ -70066,11 +70317,11 @@
70066 ** Then the internal cache might have been left in an inconsistent
70067 ** state. We need to rollback the statement transaction, if there is
70068 ** one, or the complete transaction if there is no statement transaction.
70069 */
70070
70071 if( p->db->mallocFailed ){
70072 p->rc = SQLITE_NOMEM;
70073 }
70074 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
70075 closeAllCursors(p);
70076 if( p->magic!=VDBE_MAGIC_RUN ){
@@ -70227,11 +70478,11 @@
70227 assert( db->nVdbeRead>=db->nVdbeWrite );
70228 assert( db->nVdbeWrite>=0 );
70229 }
70230 p->magic = VDBE_MAGIC_HALT;
70231 checkActiveVdbeCnt(db);
70232 if( p->db->mallocFailed ){
70233 p->rc = SQLITE_NOMEM;
70234 }
70235
70236 /* If the auto-commit flag is set to true, then any locks that were held
70237 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
@@ -70264,16 +70515,16 @@
70264 */
70265 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
70266 sqlite3 *db = p->db;
70267 int rc = p->rc;
70268 if( p->zErrMsg ){
70269 u8 mallocFailed = db->mallocFailed;
70270 sqlite3BeginBenignMalloc();
70271 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
70272 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
70273 sqlite3EndBenignMalloc();
70274 db->mallocFailed = mallocFailed;
70275 db->errCode = rc;
70276 }else{
70277 sqlite3Error(db, rc);
70278 }
70279 return rc;
@@ -70558,13 +70809,20 @@
70558 ** a NULL row.
70559 **
70560 ** If the cursor is already pointing to the correct row and that row has
70561 ** not been deleted out from under the cursor, then this routine is a no-op.
70562 */
70563 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
 
70564 if( p->eCurType==CURTYPE_BTREE ){
70565 if( p->deferredMoveto ){
 
 
 
 
 
 
70566 return handleDeferredMoveto(p);
70567 }
70568 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
70569 return handleMovedCursor(p);
70570 }
@@ -72191,11 +72449,12 @@
72191 }
72192 SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
72193 return sqlite3VdbeIntValue((Mem*)pVal);
72194 }
72195 SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value *pVal){
72196 return ((Mem*)pVal)->eSubtype;
 
72197 }
72198 SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
72199 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
72200 }
72201 #ifndef SQLITE_OMIT_UTF16
@@ -72372,12 +72631,14 @@
72372 SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
72373 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72374 sqlite3VdbeMemSetNull(pCtx->pOut);
72375 }
72376 SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
72377 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72378 pCtx->pOut->eSubtype = eSubtype & 0xff;
 
 
72379 }
72380 SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
72381 sqlite3_context *pCtx,
72382 const char *z,
72383 int n,
@@ -72473,11 +72734,11 @@
72473 SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){
72474 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72475 sqlite3VdbeMemSetNull(pCtx->pOut);
72476 pCtx->isError = SQLITE_NOMEM;
72477 pCtx->fErrorOrAux = 1;
72478 pCtx->pOut->db->mallocFailed = 1;
72479 }
72480
72481 /*
72482 ** This function is called after a transaction has been committed. It
72483 ** invokes callbacks registered with sqlite3_wal_hook() as required.
@@ -73101,11 +73362,11 @@
73101 ret = xFunc(&p->aColName[N]);
73102 /* A malloc may have failed inside of the xFunc() call. If this
73103 ** is the case, clear the mallocFailed flag and return NULL.
73104 */
73105 if( db->mallocFailed ){
73106 db->mallocFailed = 0;
73107 ret = 0;
73108 }
73109 sqlite3_mutex_leave(db->mutex);
73110 }
73111 return ret;
@@ -73802,13 +74063,13 @@
73802 assert( idx>0 && idx<=p->nVar );
73803 pVar = &p->aVar[idx-1];
73804 if( pVar->flags & MEM_Null ){
73805 sqlite3StrAccumAppend(&out, "NULL", 4);
73806 }else if( pVar->flags & MEM_Int ){
73807 sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
73808 }else if( pVar->flags & MEM_Real ){
73809 sqlite3XPrintf(&out, 0, "%!.15g", pVar->u.r);
73810 }else if( pVar->flags & MEM_Str ){
73811 int nOut; /* Number of bytes of the string text to include in output */
73812 #ifndef SQLITE_OMIT_UTF16
73813 u8 enc = ENC(db);
73814 Mem utf8;
@@ -73825,36 +74086,36 @@
73825 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
73826 nOut = SQLITE_TRACE_SIZE_LIMIT;
73827 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
73828 }
73829 #endif
73830 sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
73831 #ifdef SQLITE_TRACE_SIZE_LIMIT
73832 if( nOut<pVar->n ){
73833 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
73834 }
73835 #endif
73836 #ifndef SQLITE_OMIT_UTF16
73837 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
73838 #endif
73839 }else if( pVar->flags & MEM_Zero ){
73840 sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
73841 }else{
73842 int nOut; /* Number of bytes of the blob to include in output */
73843 assert( pVar->flags & MEM_Blob );
73844 sqlite3StrAccumAppend(&out, "x'", 2);
73845 nOut = pVar->n;
73846 #ifdef SQLITE_TRACE_SIZE_LIMIT
73847 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
73848 #endif
73849 for(i=0; i<nOut; i++){
73850 sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
73851 }
73852 sqlite3StrAccumAppend(&out, "'", 1);
73853 #ifdef SQLITE_TRACE_SIZE_LIMIT
73854 if( nOut<pVar->n ){
73855 sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
73856 }
73857 #endif
73858 }
73859 }
73860 }
@@ -74336,10 +74597,11 @@
74336 }else{
74337 char zBuf[200];
74338 sqlite3VdbeMemPrettyPrint(p, zBuf);
74339 printf(" %s", zBuf);
74340 }
 
74341 }
74342 static void registerTrace(int iReg, Mem *p){
74343 printf("REG[%d] = ", iReg);
74344 memTracePrint(p);
74345 printf("\n");
@@ -74506,10 +74768,13 @@
74506 Op *aOp = p->aOp; /* Copy of p->aOp */
74507 Op *pOp = aOp; /* Current operation */
74508 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
74509 Op *pOrigOp; /* Value of pOp at the top of the loop */
74510 #endif
 
 
 
74511 int rc = SQLITE_OK; /* Value to return */
74512 sqlite3 *db = p->db; /* The database */
74513 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
74514 u8 encoding = ENC(db); /* The database encoding */
74515 int iCompare = 0; /* Result of last OP_Compare operation */
@@ -74579,11 +74844,10 @@
74579 }
74580 sqlite3EndBenignMalloc();
74581 #endif
74582 for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
74583 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
74584 if( db->mallocFailed ) goto no_mem;
74585 #ifdef VDBE_PROFILE
74586 start = sqlite3Hwtime();
74587 #endif
74588 nVmStep++;
74589 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -75577,11 +75841,11 @@
75577 assert( pOp->p4type==P4_FUNCDEF );
75578 n = pOp->p5;
75579 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
75580 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
75581 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
75582 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
75583 if( pCtx==0 ) goto no_mem;
75584 pCtx->pOut = 0;
75585 pCtx->pFunc = pOp->p4.pFunc;
75586 pCtx->iOp = (int)(pOp - aOp);
75587 pCtx->pVdbe = p;
@@ -76021,15 +76285,18 @@
76021 ** of integers in P4.
76022 **
76023 ** The permutation is only valid until the next OP_Compare that has
76024 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
76025 ** occur immediately prior to the OP_Compare.
 
 
 
76026 */
76027 case OP_Permutation: {
76028 assert( pOp->p4type==P4_INTARRAY );
76029 assert( pOp->p4.ai );
76030 aPermute = pOp->p4.ai;
76031 break;
76032 }
76033
76034 /* Opcode: Compare P1 P2 P3 P4 P5
76035 ** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -76330,26 +76597,28 @@
76330 u64 offset64; /* 64-bit offset */
76331 u32 avail; /* Number of bytes of available data */
76332 u32 t; /* A type code from the record header */
76333 Mem *pReg; /* PseudoTable input register */
76334
 
76335 p2 = pOp->p2;
 
 
 
 
76336 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
76337 pDest = &aMem[pOp->p3];
76338 memAboutToChange(p, pDest);
76339 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
76340 pC = p->apCsr[pOp->p1];
76341 assert( pC!=0 );
76342 assert( p2<pC->nField );
76343 aOffset = pC->aOffset;
76344 assert( pC->eCurType!=CURTYPE_VTAB );
76345 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
76346 assert( pC->eCurType!=CURTYPE_SORTER );
76347 pCrsr = pC->uc.pCursor;
76348
76349 /* If the cursor cache is stale, bring it up-to-date */
76350 rc = sqlite3VdbeCursorMoveto(pC);
76351 if( rc ) goto abort_due_to_error;
76352 if( pC->cacheStatus!=p->cacheCtr ){
76353 if( pC->nullRow ){
76354 if( pC->eCurType==CURTYPE_PSEUDO ){
76355 assert( pC->uc.pseudoTableReg>0 );
@@ -76816,11 +77085,11 @@
76816 db->nStatement+db->nSavepoint);
76817 if( rc!=SQLITE_OK ) goto abort_due_to_error;
76818 #endif
76819
76820 /* Create a new savepoint structure. */
76821 pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
76822 if( pNew ){
76823 pNew->zName = (char *)&pNew[1];
76824 memcpy(pNew->zName, zName, nName+1);
76825
76826 /* If there is no open transaction, then mark this as a special
@@ -76953,32 +77222,31 @@
76953 ** This instruction causes the VM to halt.
76954 */
76955 case OP_AutoCommit: {
76956 int desiredAutoCommit;
76957 int iRollback;
76958 int turnOnAC;
76959
76960 desiredAutoCommit = pOp->p1;
76961 iRollback = pOp->p2;
76962 turnOnAC = desiredAutoCommit && !db->autoCommit;
76963 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
76964 assert( desiredAutoCommit==1 || iRollback==0 );
76965 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
76966 assert( p->bIsReader );
76967
76968 if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
76969 /* If this instruction implements a COMMIT and other VMs are writing
76970 ** return an error indicating that the other VMs must complete first.
76971 */
76972 sqlite3VdbeError(p, "cannot commit transaction - "
76973 "SQL statements in progress");
76974 rc = SQLITE_BUSY;
76975 }else if( desiredAutoCommit!=db->autoCommit ){
76976 if( iRollback ){
76977 assert( desiredAutoCommit==1 );
76978 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
76979 db->autoCommit = 1;
 
 
 
 
 
 
 
 
76980 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
76981 goto vdbe_return;
76982 }else{
76983 db->autoCommit = (u8)desiredAutoCommit;
76984 }
@@ -77159,38 +77427,36 @@
77159 break;
77160 }
77161
77162 /* Opcode: SetCookie P1 P2 P3 * *
77163 **
77164 ** Write the content of register P3 (interpreted as an integer)
77165 ** into cookie number P2 of database P1. P2==1 is the schema version.
77166 ** P2==2 is the database format. P2==3 is the recommended pager cache
77167 ** size, and so forth. P1==0 is the main database file and P1==1 is the
77168 ** database file used to store temporary tables.
77169 **
77170 ** A transaction must be started before executing this opcode.
77171 */
77172 case OP_SetCookie: { /* in3 */
77173 Db *pDb;
77174 assert( pOp->p2<SQLITE_N_BTREE_META );
77175 assert( pOp->p1>=0 && pOp->p1<db->nDb );
77176 assert( DbMaskTest(p->btreeMask, pOp->p1) );
77177 assert( p->readOnly==0 );
77178 pDb = &db->aDb[pOp->p1];
77179 assert( pDb->pBt!=0 );
77180 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
77181 pIn3 = &aMem[pOp->p3];
77182 sqlite3VdbeMemIntegerify(pIn3);
77183 /* See note about index shifting on OP_ReadCookie */
77184 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
77185 if( pOp->p2==BTREE_SCHEMA_VERSION ){
77186 /* When the schema cookie changes, record the new cookie internally */
77187 pDb->pSchema->schema_cookie = (int)pIn3->u.i;
77188 db->flags |= SQLITE_InternChanges;
77189 }else if( pOp->p2==BTREE_FILE_FORMAT ){
77190 /* Record changes in the file format */
77191 pDb->pSchema->file_format = (u8)pIn3->u.i;
77192 }
77193 if( pOp->p1==1 ){
77194 /* Invalidate all prepared statements whenever the TEMP database
77195 ** schema is changed. Ticket #1644 */
77196 sqlite3ExpirePreparedStatements(db);
@@ -77346,10 +77612,13 @@
77346 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
77347 if( pCur==0 ) goto no_mem;
77348 pCur->nullRow = 1;
77349 pCur->isOrdered = 1;
77350 pCur->pgnoRoot = p2;
 
 
 
77351 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
77352 pCur->pKeyInfo = pKeyInfo;
77353 /* Set the VdbeCursor.isTable variable. Previous versions of
77354 ** SQLite used to check if the root-page flags were sane at this point
77355 ** and report database corruption if they were not, but this check has
@@ -77799,36 +78068,10 @@
77799 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
77800 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
77801 }
77802 break;
77803 }
77804
77805 /* Opcode: Seek P1 P2 * * *
77806 ** Synopsis: intkey=r[P2]
77807 **
77808 ** P1 is an open table cursor and P2 is a rowid integer. Arrange
77809 ** for P1 to move so that it points to the rowid given by P2.
77810 **
77811 ** This is actually a deferred seek. Nothing actually happens until
77812 ** the cursor is used to read a record. That way, if no reads
77813 ** occur, no unnecessary I/O happens.
77814 */
77815 case OP_Seek: { /* in2 */
77816 VdbeCursor *pC;
77817
77818 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
77819 pC = p->apCsr[pOp->p1];
77820 assert( pC!=0 );
77821 assert( pC->eCurType==CURTYPE_BTREE );
77822 assert( pC->uc.pCursor!=0 );
77823 assert( pC->isTable );
77824 pC->nullRow = 0;
77825 pIn2 = &aMem[pOp->p2];
77826 pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
77827 pC->deferredMoveto = 1;
77828 break;
77829 }
77830
77831
77832 /* Opcode: Found P1 P2 P3 P4 *
77833 ** Synopsis: key=r[P3@P4]
77834 **
@@ -78295,18 +78538,26 @@
78295
78296 /* Opcode: Delete P1 P2 * P4 P5
78297 **
78298 ** Delete the record at which the P1 cursor is currently pointing.
78299 **
78300 ** If the P5 parameter is non-zero, the cursor will be left pointing at
78301 ** either the next or the previous record in the table. If it is left
78302 ** pointing at the next record, then the next Next instruction will be a
78303 ** no-op. As a result, in this case it is OK to delete a record from within a
78304 ** Next loop. If P5 is zero, then the cursor is left in an undefined state.
 
 
78305 **
78306 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
78307 ** incremented (otherwise not).
 
 
 
 
 
 
78308 **
78309 ** P1 must not be pseudo-table. It has to be a real table with
78310 ** multiple rows.
78311 **
78312 ** If P4 is not NULL, then it is the name of the table that P1 is
@@ -78338,11 +78589,30 @@
78338 i64 iKey = 0;
78339 sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
78340 assert( pC->movetoTarget==iKey );
78341 }
78342 #endif
78343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78344 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
78345 pC->cacheStatus = CACHE_STALE;
78346
78347 /* Invoke the update-hook if required. */
78348 if( rc==SQLITE_OK && hasUpdateCallback ){
@@ -78883,62 +79153,98 @@
78883 assert( pOp->p5==0 );
78884 r.pKeyInfo = pC->pKeyInfo;
78885 r.nField = (u16)pOp->p3;
78886 r.default_rc = 0;
78887 r.aMem = &aMem[pOp->p2];
78888 #ifdef SQLITE_DEBUG
78889 { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
78890 #endif
78891 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
78892 if( rc==SQLITE_OK && res==0 ){
78893 rc = sqlite3BtreeDelete(pCrsr, 0);
78894 }
78895 assert( pC->deferredMoveto==0 );
78896 pC->cacheStatus = CACHE_STALE;
78897 break;
78898 }
78899
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78900 /* Opcode: IdxRowid P1 P2 * * *
78901 ** Synopsis: r[P2]=rowid
78902 **
78903 ** Write into register P2 an integer which is the last entry in the record at
78904 ** the end of the index key pointed to by cursor P1. This integer should be
78905 ** the rowid of the table entry to which this index entry points.
78906 **
78907 ** See also: Rowid, MakeRecord.
78908 */
 
78909 case OP_IdxRowid: { /* out2 */
78910 BtCursor *pCrsr;
78911 VdbeCursor *pC;
78912 i64 rowid;
78913
78914 pOut = out2Prerelease(p, pOp);
78915 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
78916 pC = p->apCsr[pOp->p1];
78917 assert( pC!=0 );
78918 assert( pC->eCurType==CURTYPE_BTREE );
78919 pCrsr = pC->uc.pCursor;
78920 assert( pCrsr!=0 );
78921 pOut->flags = MEM_Null;
78922 assert( pC->isTable==0 );
78923 assert( pC->deferredMoveto==0 );
 
 
 
 
 
78924
78925 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
78926 ** out from under the cursor. That will never happend for an IdxRowid
78927 ** opcode, hence the NEVER() arround the check of the return value.
78928 */
78929 rc = sqlite3VdbeCursorRestore(pC);
78930 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
78931
78932 if( !pC->nullRow ){
78933 rowid = 0; /* Not needed. Only used to silence a warning. */
78934 rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
78935 if( rc!=SQLITE_OK ){
78936 goto abort_due_to_error;
78937 }
78938 pOut->u.i = rowid;
78939 pOut->flags = MEM_Int;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78940 }
78941 break;
78942 }
78943
78944 /* Opcode: IdxGE P1 P2 P3 P4 P5
@@ -79329,11 +79635,11 @@
79329 Mem *pnErr; /* Register keeping track of errors remaining */
79330
79331 assert( p->bIsReader );
79332 nRoot = pOp->p2;
79333 assert( nRoot>0 );
79334 aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
79335 if( aRoot==0 ) goto no_mem;
79336 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
79337 pnErr = &aMem[pOp->p3];
79338 assert( (pnErr->flags & MEM_Int)!=0 );
79339 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
@@ -79711,24 +80017,35 @@
79711 goto jump_to_p2;
79712 }
79713 break;
79714 }
79715
79716 /* Opcode: SetIfNotPos P1 P2 P3 * *
79717 ** Synopsis: if r[P1]<=0 then r[P2]=P3
79718 **
79719 ** Register P1 must contain an integer.
79720 ** If the value of register P1 is not positive (if it is less than 1) then
79721 ** set the value of register P2 to be the integer P3.
 
 
 
 
 
 
 
 
 
 
 
79722 */
79723 case OP_SetIfNotPos: { /* in1, in2 */
79724 pIn1 = &aMem[pOp->p1];
79725 assert( pIn1->flags&MEM_Int );
79726 if( pIn1->u.i<=0 ){
79727 pOut = out2Prerelease(p, pOp);
79728 pOut->u.i = pOp->p3;
79729 }
79730 break;
79731 }
79732
79733 /* Opcode: IfNotZero P1 P2 P3 * *
79734 ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
@@ -79815,11 +80132,11 @@
79815 assert( pOp->p4type==P4_FUNCDEF );
79816 n = pOp->p5;
79817 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
79818 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
79819 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
79820 pCtx = sqlite3DbMallocRaw(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
79821 if( pCtx==0 ) goto no_mem;
79822 pCtx->pMem = 0;
79823 pCtx->pFunc = pOp->p4.pFunc;
79824 pCtx->iOp = (int)(pOp - aOp);
79825 pCtx->pVdbe = p;
@@ -80682,11 +80999,11 @@
80682 p->rc = rc;
80683 testcase( sqlite3GlobalConfig.xLog!=0 );
80684 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
80685 (int)(pOp - aOp), p->zSql, p->zErrMsg);
80686 sqlite3VdbeHalt(p);
80687 if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
80688 rc = SQLITE_ERROR;
80689 if( resetSchemaOnFault>0 ){
80690 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
80691 }
80692
@@ -80696,10 +81013,13 @@
80696 vdbe_return:
80697 db->lastRowid = lastRowid;
80698 testcase( nVmStep>0 );
80699 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
80700 sqlite3VdbeLeave(p);
 
 
 
80701 return rc;
80702
80703 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
80704 ** is encountered.
80705 */
@@ -80709,11 +81029,11 @@
80709 goto vdbe_error_halt;
80710
80711 /* Jump to here if a malloc() fails.
80712 */
80713 no_mem:
80714 db->mallocFailed = 1;
80715 sqlite3VdbeError(p, "out of memory");
80716 rc = SQLITE_NOMEM;
80717 goto vdbe_error_halt;
80718
80719 /* Jump to here for any other kind of fatal error. The "rc" variable
@@ -80730,11 +81050,11 @@
80730 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
80731 ** flag.
80732 */
80733 abort_due_to_interrupt:
80734 assert( db->u1.isInterrupted );
80735 rc = SQLITE_INTERRUPT;
80736 p->rc = rc;
80737 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
80738 goto vdbe_error_halt;
80739 }
80740
@@ -80990,23 +81310,21 @@
80990 **
80991 ** The sqlite3_blob_close() function finalizes the vdbe program,
80992 ** which closes the b-tree cursor and (possibly) commits the
80993 ** transaction.
80994 */
80995 static const int iLn = VDBE_OFFSET_LINENO(4);
80996 static const VdbeOpList openBlob[] = {
80997 /* addr/ofst */
80998 /* {OP_Transaction, 0, 0, 0}, // 0/ inserted separately */
80999 {OP_TableLock, 0, 0, 0}, /* 1/0: Acquire a read or write lock */
81000 {OP_OpenRead, 0, 0, 0}, /* 2/1: Open a cursor */
81001 {OP_Variable, 1, 1, 0}, /* 3/2: Move ?1 into reg[1] */
81002 {OP_NotExists, 0, 8, 1}, /* 4/3: Seek the cursor */
81003 {OP_Column, 0, 0, 1}, /* 5/4 */
81004 {OP_ResultRow, 1, 0, 0}, /* 6/5 */
81005 {OP_Goto, 0, 3, 0}, /* 7/6 */
81006 {OP_Close, 0, 0, 0}, /* 8/7 */
81007 {OP_Halt, 0, 0, 0}, /* 9/8 */
81008 };
81009 Vdbe *v = (Vdbe *)pBlob->pStmt;
81010 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81011 VdbeOp *aOp;
81012
@@ -83044,27 +83362,28 @@
83044 if( pSorter->list.aMemory ){
83045 int nMin = pSorter->iMemory + nReq;
83046
83047 if( nMin>pSorter->nMemory ){
83048 u8 *aNew;
 
83049 int nNew = pSorter->nMemory * 2;
83050 while( nNew < nMin ) nNew = nNew*2;
83051 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
83052 if( nNew < nMin ) nNew = nMin;
83053
83054 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
83055 if( !aNew ) return SQLITE_NOMEM;
83056 pSorter->list.pList = (SorterRecord*)(
83057 aNew + ((u8*)pSorter->list.pList - pSorter->list.aMemory)
83058 );
83059 pSorter->list.aMemory = aNew;
83060 pSorter->nMemory = nNew;
83061 }
83062
83063 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
83064 pSorter->iMemory += ROUND8(nReq);
83065 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
 
 
83066 }else{
83067 pNew = (SorterRecord *)sqlite3Malloc(nReq);
83068 if( pNew==0 ){
83069 return SQLITE_NOMEM;
83070 }
@@ -86230,12 +86549,11 @@
86230 return pExpr;
86231 }
86232 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
86233 Token s;
86234 assert( zC!=0 );
86235 s.z = zC;
86236 s.n = sqlite3Strlen30(s.z);
86237 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
86238 }
86239
86240 /*
86241 ** Skip over any TK_COLLATE operators and any unlikely()
@@ -86599,18 +86917,19 @@
86599 ){
86600 Expr *pNew;
86601 int nExtra = 0;
86602 int iValue = 0;
86603
 
86604 if( pToken ){
86605 if( op!=TK_INTEGER || pToken->z==0
86606 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
86607 nExtra = pToken->n+1;
86608 assert( iValue>=0 );
86609 }
86610 }
86611 pNew = sqlite3DbMallocRaw(db, sizeof(Expr)+nExtra);
86612 if( pNew ){
86613 memset(pNew, 0, sizeof(Expr));
86614 pNew->op = (u8)op;
86615 pNew->iAgg = -1;
86616 if( pToken ){
@@ -86845,11 +87164,14 @@
86845 }
86846 if( x>0 ){
86847 if( x>pParse->nzVar ){
86848 char **a;
86849 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
86850 if( a==0 ) return; /* Error reported through db->mallocFailed */
 
 
 
86851 pParse->azVar = a;
86852 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
86853 pParse->nzVar = x;
86854 }
86855 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
@@ -87000,10 +87322,11 @@
87000 ** portion of the buffer copied into by this function.
87001 */
87002 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
87003 Expr *pNew = 0; /* Value to return */
87004 assert( flags==0 || flags==EXPRDUP_REDUCE );
 
87005 if( p ){
87006 const int isReduced = (flags&EXPRDUP_REDUCE);
87007 u8 *zAlloc;
87008 u32 staticFlag = 0;
87009
@@ -87012,11 +87335,11 @@
87012 /* Figure out where to write the new Expr structure. */
87013 if( pzBuffer ){
87014 zAlloc = *pzBuffer;
87015 staticFlag = EP_Static;
87016 }else{
87017 zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
87018 }
87019 pNew = (Expr *)zAlloc;
87020
87021 if( pNew ){
87022 /* Set nNewSize to the size allocated for the structure pointed to
@@ -87135,16 +87458,17 @@
87135 }
87136 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
87137 ExprList *pNew;
87138 struct ExprList_item *pItem, *pOldItem;
87139 int i;
 
87140 if( p==0 ) return 0;
87141 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
87142 if( pNew==0 ) return 0;
87143 pNew->nExpr = i = p->nExpr;
87144 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
87145 pNew->a = pItem = sqlite3DbMallocRaw(db, i*sizeof(p->a[0]) );
87146 if( pItem==0 ){
87147 sqlite3DbFree(db, pNew);
87148 return 0;
87149 }
87150 pOldItem = p->a;
@@ -87171,13 +87495,14 @@
87171 || !defined(SQLITE_OMIT_SUBQUERY)
87172 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
87173 SrcList *pNew;
87174 int i;
87175 int nByte;
 
87176 if( p==0 ) return 0;
87177 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
87178 pNew = sqlite3DbMallocRaw(db, nByte );
87179 if( pNew==0 ) return 0;
87180 pNew->nSrc = pNew->nAlloc = p->nSrc;
87181 for(i=0; i<p->nSrc; i++){
87182 struct SrcList_item *pNewItem = &pNew->a[i];
87183 struct SrcList_item *pOldItem = &p->a[i];
@@ -87210,15 +87535,16 @@
87210 return pNew;
87211 }
87212 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
87213 IdList *pNew;
87214 int i;
 
87215 if( p==0 ) return 0;
87216 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
87217 if( pNew==0 ) return 0;
87218 pNew->nId = p->nId;
87219 pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
87220 if( pNew->a==0 ){
87221 sqlite3DbFree(db, pNew);
87222 return 0;
87223 }
87224 /* Note that because the size of the allocation for p->a[] is not
@@ -87232,12 +87558,13 @@
87232 }
87233 return pNew;
87234 }
87235 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
87236 Select *pNew, *pPrior;
 
87237 if( p==0 ) return 0;
87238 pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
87239 if( pNew==0 ) return 0;
87240 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
87241 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
87242 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
87243 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
@@ -87279,17 +87606,18 @@
87279 Parse *pParse, /* Parsing context */
87280 ExprList *pList, /* List to which to append. Might be NULL */
87281 Expr *pExpr /* Expression to be appended. Might be NULL */
87282 ){
87283 sqlite3 *db = pParse->db;
 
87284 if( pList==0 ){
87285 pList = sqlite3DbMallocRaw(db, sizeof(ExprList) );
87286 if( pList==0 ){
87287 goto no_mem;
87288 }
87289 pList->nExpr = 0;
87290 pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
87291 if( pList->a==0 ) goto no_mem;
87292 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
87293 struct ExprList_item *a;
87294 assert( pList->nExpr>0 );
87295 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
@@ -90989,37 +91317,10 @@
90989 sqlite3SrcListDelete(db, pSrc);
90990 sqlite3DbFree(db, zName);
90991 db->flags = savedDbFlags;
90992 }
90993
90994
90995 /*
90996 ** Generate code to make sure the file format number is at least minFormat.
90997 ** The generated code will increase the file format number if necessary.
90998 */
90999 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
91000 Vdbe *v;
91001 v = sqlite3GetVdbe(pParse);
91002 /* The VDBE should have been allocated before this routine is called.
91003 ** If that allocation failed, we would have quit before reaching this
91004 ** point */
91005 if( ALWAYS(v) ){
91006 int r1 = sqlite3GetTempReg(pParse);
91007 int r2 = sqlite3GetTempReg(pParse);
91008 int addr1;
91009 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
91010 sqlite3VdbeUsesBtree(v, iDb);
91011 sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
91012 addr1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
91013 sqlite3VdbeChangeP5(v, SQLITE_NOTNULL); VdbeCoverage(v);
91014 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
91015 sqlite3VdbeJumpHere(v, addr1);
91016 sqlite3ReleaseTempReg(pParse, r1);
91017 sqlite3ReleaseTempReg(pParse, r2);
91018 }
91019 }
91020
91021 /*
91022 ** This function is called after an "ALTER TABLE ... ADD" statement
91023 ** has been parsed. Argument pColDef contains the text of the new
91024 ** column definition.
91025 **
@@ -91034,13 +91335,15 @@
91034 const char *zTab; /* Table name */
91035 char *zCol; /* Null-terminated column definition */
91036 Column *pCol; /* The new column */
91037 Expr *pDflt; /* Default value for the new column */
91038 sqlite3 *db; /* The database connection; */
 
91039
91040 db = pParse->db;
91041 if( pParse->nErr || db->mallocFailed ) return;
 
91042 pNew = pParse->pNewTable;
91043 assert( pNew );
91044
91045 assert( sqlite3BtreeHoldsAllMutexes(db) );
91046 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
@@ -91096,11 +91399,11 @@
91096 sqlite3_value *pVal = 0;
91097 int rc;
91098 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
91099 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
91100 if( rc!=SQLITE_OK ){
91101 db->mallocFailed = 1;
91102 return;
91103 }
91104 if( !pVal ){
91105 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
91106 return;
@@ -91126,15 +91429,20 @@
91126 );
91127 sqlite3DbFree(db, zCol);
91128 db->flags = savedDbFlags;
91129 }
91130
91131 /* If the default value of the new column is NULL, then set the file
91132 ** format to 2. If the default value of the new column is not NULL,
91133 ** the file format becomes 3.
 
 
 
 
91134 */
91135 sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
 
91136
91137 /* Reload the schema of the modified table. */
91138 reloadTableSchema(pParse, pTab, pTab->zName);
91139 }
91140
@@ -91204,11 +91512,11 @@
91204 nAlloc = (((pNew->nCol-1)/8)*8)+8;
91205 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
91206 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
91207 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
91208 if( !pNew->aCol || !pNew->zName ){
91209 db->mallocFailed = 1;
91210 goto exit_begin_add_column;
91211 }
91212 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
91213 for(i=0; i<pNew->nCol; i++){
91214 Column *pCol = &pNew->aCol[i];
@@ -91549,11 +91857,11 @@
91549 */
91550 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
91551 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
91552 assert( db!=0 );
91553 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
91554 p->u.aRowid = sqlite3DbMallocRaw(db, n);
91555 if( p->u.aRowid ){
91556 p->nRowid = n;
91557 memcpy(p->u.aRowid, pData, n);
91558 }else{
91559 p->nRowid = 0;
@@ -92351,11 +92659,11 @@
92351 addrNextRow = sqlite3VdbeCurrentAddr(v);
92352
92353 if( nColTest>0 ){
92354 int endDistinctTest = sqlite3VdbeMakeLabel(v);
92355 int *aGotoChng; /* Array of jump instruction addresses */
92356 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*nColTest);
92357 if( aGotoChng==0 ) continue;
92358
92359 /*
92360 ** next_row:
92361 ** regChng = 0
@@ -92759,11 +93067,11 @@
92759 /* Index.aiRowEst may already be set here if there are duplicate
92760 ** sqlite_stat1 entries for this index. In that case just clobber
92761 ** the old data with the new instead of allocating a new array. */
92762 if( pIndex->aiRowEst==0 ){
92763 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
92764 if( pIndex->aiRowEst==0 ) pInfo->db->mallocFailed = 1;
92765 }
92766 aiRowEst = pIndex->aiRowEst;
92767 #endif
92768 pIndex->bUnordered = 0;
92769 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
@@ -92906,11 +93214,11 @@
92906 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
92907 char *zSql; /* Text of the SQL statement */
92908 Index *pPrevIdx = 0; /* Previous index in the loop */
92909 IndexSample *pSample; /* A slot in pIdx->aSample[] */
92910
92911 assert( db->lookaside.bEnabled==0 );
92912 zSql = sqlite3MPrintf(db, zSql1, zDb);
92913 if( !zSql ){
92914 return SQLITE_NOMEM;
92915 }
92916 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
@@ -93020,11 +93328,11 @@
93020 ** the Index.aSample[] arrays of all indices.
93021 */
93022 static int loadStat4(sqlite3 *db, const char *zDb){
93023 int rc = SQLITE_OK; /* Result codes from subroutines */
93024
93025 assert( db->lookaside.bEnabled==0 );
93026 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
93027 rc = loadStatTbl(db, 0,
93028 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
93029 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
93030 zDb
@@ -93102,24 +93410,23 @@
93102
93103
93104 /* Load the statistics from the sqlite_stat4 table. */
93105 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
93106 if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
93107 int lookasideEnabled = db->lookaside.bEnabled;
93108 db->lookaside.bEnabled = 0;
93109 rc = loadStat4(db, sInfo.zDatabase);
93110 db->lookaside.bEnabled = lookasideEnabled;
93111 }
93112 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
93113 Index *pIdx = sqliteHashData(i);
93114 sqlite3_free(pIdx->aiRowEst);
93115 pIdx->aiRowEst = 0;
93116 }
93117 #endif
93118
93119 if( rc==SQLITE_NOMEM ){
93120 db->mallocFailed = 1;
93121 }
93122 return rc;
93123 }
93124
93125
@@ -93236,11 +93543,11 @@
93236
93237 /* Allocate the new entry in the db->aDb[] array and initialize the schema
93238 ** hash tables.
93239 */
93240 if( db->aDb==db->aDbStatic ){
93241 aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
93242 if( aNew==0 ) return;
93243 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
93244 }else{
93245 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
93246 if( aNew==0 ) return;
@@ -93254,11 +93561,11 @@
93254 ** or may not be initialized.
93255 */
93256 flags = db->openFlags;
93257 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
93258 if( rc!=SQLITE_OK ){
93259 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
93260 sqlite3_result_error(context, zErr, -1);
93261 sqlite3_free(zErr);
93262 return;
93263 }
93264 assert( pVfs );
@@ -93283,11 +93590,12 @@
93283 pPager = sqlite3BtreePager(aNew->pBt);
93284 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
93285 sqlite3BtreeSecureDelete(aNew->pBt,
93286 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
93287 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
93288 sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
 
93289 #endif
93290 sqlite3BtreeLeave(aNew->pBt);
93291 }
93292 aNew->safety_level = 3;
93293 aNew->zName = sqlite3DbStrDup(db, zName);
@@ -93356,11 +93664,11 @@
93356 db->aDb[iDb].pSchema = 0;
93357 }
93358 sqlite3ResetAllSchemasOfConnection(db);
93359 db->nDb = iDb;
93360 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
93361 db->mallocFailed = 1;
93362 sqlite3DbFree(db, zErrDyn);
93363 zErrDyn = sqlite3MPrintf(db, "out of memory");
93364 }else if( zErrDyn==0 ){
93365 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
93366 }
@@ -94053,11 +94361,11 @@
94053 p->iTab = iTab;
94054 p->isWriteLock = isWriteLock;
94055 p->zName = zName;
94056 }else{
94057 pToplevel->nTableLock = 0;
94058 pToplevel->db->mallocFailed = 1;
94059 }
94060 }
94061
94062 /*
94063 ** Code an OP_TableLock instruction for each table locked by the
@@ -94901,11 +95209,11 @@
94901 }
94902 }
94903
94904 pTable = sqlite3DbMallocZero(db, sizeof(Table));
94905 if( pTable==0 ){
94906 db->mallocFailed = 1;
94907 pParse->rc = SQLITE_NOMEM;
94908 pParse->nErr++;
94909 goto begin_table_error;
94910 }
94911 pTable->zName = zName;
@@ -94958,14 +95266,12 @@
94958 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
94959 sqlite3VdbeUsesBtree(v, iDb);
94960 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
94961 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
94962 1 : SQLITE_MAX_FILE_FORMAT;
94963 sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
94964 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
94965 sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
94966 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
94967 sqlite3VdbeJumpHere(v, addr1);
94968
94969 /* This just creates a place-holder record in the sqlite_master table.
94970 ** The record created does not contain anything yet. It will be replaced
94971 ** by the real entry in code generated at sqlite3EndTable().
@@ -95446,17 +95752,15 @@
95446 ** set back to prior value. But schema changes are infrequent
95447 ** and the probability of hitting the same cookie value is only
95448 ** 1 chance in 2^32. So we're safe enough.
95449 */
95450 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
95451 int r1 = sqlite3GetTempReg(pParse);
95452 sqlite3 *db = pParse->db;
95453 Vdbe *v = pParse->pVdbe;
95454 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95455 sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
95456 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
95457 sqlite3ReleaseTempReg(pParse, r1);
95458 }
95459
95460 /*
95461 ** Measure the number of characters needed to output the given
95462 ** identifier. The number returned includes any quotes used
@@ -95534,11 +95838,11 @@
95534 zEnd = "\n)";
95535 }
95536 n += 35 + 6*p->nCol;
95537 zStmt = sqlite3DbMallocRaw(0, n);
95538 if( zStmt==0 ){
95539 db->mallocFailed = 1;
95540 return 0;
95541 }
95542 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
95543 k = sqlite3Strlen30(zStmt);
95544 identPut(zStmt, &k, p->zName);
@@ -95683,12 +95987,11 @@
95683 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
95684 */
95685 if( pTab->iPKey>=0 ){
95686 ExprList *pList;
95687 Token ipkToken;
95688 ipkToken.z = pTab->aCol[pTab->iPKey].zName;
95689 ipkToken.n = sqlite3Strlen30(ipkToken.z);
95690 pList = sqlite3ExprListAppend(pParse, 0,
95691 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
95692 if( pList==0 ) return;
95693 pList->a[0].sortOrder = pParse->iPkSortOrder;
95694 assert( pParse->pNewTable==pTab );
@@ -95934,11 +96237,11 @@
95934 pParse->nTab = 2;
95935 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
95936 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
95937 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
95938 sqlite3Select(pParse, pSelect, &dest);
95939 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
95940 sqlite3VdbeJumpHere(v, addrTop - 1);
95941 if( pParse->nErr ) return;
95942 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
95943 if( pSelTab==0 ) return;
95944 assert( p->aCol==0 );
@@ -96018,11 +96321,11 @@
96018 Schema *pSchema = p->pSchema;
96019 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
96020 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
96021 if( pOld ){
96022 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
96023 db->mallocFailed = 1;
96024 return;
96025 }
96026 pParse->pNewTable = 0;
96027 db->flags |= SQLITE_InternChanges;
96028
@@ -96122,11 +96425,10 @@
96122 Select *pSel; /* Copy of the SELECT that implements the view */
96123 int nErr = 0; /* Number of errors encountered */
96124 int n; /* Temporarily holds the number of cursors assigned */
96125 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
96126 sqlite3_xauth xAuth; /* Saved xAuth pointer */
96127 u8 bEnabledLA; /* Saved db->lookaside.bEnabled state */
96128
96129 assert( pTable );
96130
96131 #ifndef SQLITE_OMIT_VIRTUALTABLE
96132 if( sqlite3VtabCallConnect(pParse, pTable) ){
@@ -96168,30 +96470,31 @@
96168 ** to the elements of the FROM clause. But we do not want these changes
96169 ** to be permanent. So the computation is done on a copy of the SELECT
96170 ** statement that defines the view.
96171 */
96172 assert( pTable->pSelect );
96173 bEnabledLA = db->lookaside.bEnabled;
96174 if( pTable->pCheck ){
96175 db->lookaside.bEnabled = 0;
96176 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
96177 &pTable->nCol, &pTable->aCol);
 
96178 }else{
96179 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
96180 if( pSel ){
96181 n = pParse->nTab;
96182 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
96183 pTable->nCol = -1;
96184 db->lookaside.bEnabled = 0;
96185 #ifndef SQLITE_OMIT_AUTHORIZATION
96186 xAuth = db->xAuth;
96187 db->xAuth = 0;
96188 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
96189 db->xAuth = xAuth;
96190 #else
96191 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
96192 #endif
 
96193 pParse->nTab = n;
96194 if( pSelTab ){
96195 assert( pTable->aCol==0 );
96196 pTable->nCol = pSelTab->nCol;
96197 pTable->aCol = pSelTab->aCol;
@@ -96206,11 +96509,10 @@
96206 sqlite3SelectDelete(db, pSel);
96207 } else {
96208 nErr++;
96209 }
96210 }
96211 db->lookaside.bEnabled = bEnabledLA;
96212 pTable->pSchema->schemaFlags |= DB_UnresetViews;
96213 #endif /* SQLITE_OMIT_VIEW */
96214 return nErr;
96215 }
96216 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
@@ -96672,11 +96974,11 @@
96672 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
96673 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
96674 pFKey->zTo, (void *)pFKey
96675 );
96676 if( pNextTo==pFKey ){
96677 db->mallocFailed = 1;
96678 goto fk_end;
96679 }
96680 if( pNextTo ){
96681 assert( pNextTo->pPrevTo==0 );
96682 pFKey->pNextTo = pNextTo;
@@ -97032,12 +97334,11 @@
97032 ** key out of the last column added to the table under construction.
97033 ** So create a fake list to simulate this.
97034 */
97035 if( pList==0 ){
97036 Token prevCol;
97037 prevCol.z = pTab->aCol[pTab->nCol-1].zName;
97038 prevCol.n = sqlite3Strlen30(prevCol.z);
97039 pList = sqlite3ExprListAppend(pParse, 0,
97040 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
97041 if( pList==0 ) goto exit_create_index;
97042 assert( pList->nExpr==1 );
97043 sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -97255,11 +97556,11 @@
97255 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
97256 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
97257 pIndex->zName, pIndex);
97258 if( p ){
97259 assert( p==pIndex ); /* Malloc must have failed */
97260 db->mallocFailed = 1;
97261 goto exit_create_index;
97262 }
97263 db->flags |= SQLITE_InternChanges;
97264 if( pTblName!=0 ){
97265 pIndex->tnum = db->init.newTnum;
@@ -97684,12 +97985,13 @@
97684 Token *pTable, /* Table to append */
97685 Token *pDatabase /* Database of the table */
97686 ){
97687 struct SrcList_item *pItem;
97688 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
 
97689 if( pList==0 ){
97690 pList = sqlite3DbMallocRaw(db, sizeof(SrcList) );
97691 if( pList==0 ) return 0;
97692 pList->nAlloc = 1;
97693 pList->nSrc = 0;
97694 }
97695 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
@@ -97869,21 +98171,20 @@
97869 p->a[0].fg.jointype = 0;
97870 }
97871 }
97872
97873 /*
97874 ** Begin a transaction
97875 */
97876 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
97877 sqlite3 *db;
97878 Vdbe *v;
97879 int i;
97880
97881 assert( pParse!=0 );
97882 db = pParse->db;
97883 assert( db!=0 );
97884 /* if( db->aDb[0].pBt==0 ) return; */
97885 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
97886 return;
97887 }
97888 v = sqlite3GetVdbe(pParse);
97889 if( !v ) return;
@@ -97891,15 +98192,15 @@
97891 for(i=0; i<db->nDb; i++){
97892 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
97893 sqlite3VdbeUsesBtree(v, i);
97894 }
97895 }
97896 sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
97897 }
97898
97899 /*
97900 ** Commit a transaction
97901 */
97902 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
97903 Vdbe *v;
97904
97905 assert( pParse!=0 );
@@ -97907,16 +98208,16 @@
97907 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
97908 return;
97909 }
97910 v = sqlite3GetVdbe(pParse);
97911 if( v ){
97912 sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
97913 }
97914 }
97915
97916 /*
97917 ** Rollback a transaction
97918 */
97919 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
97920 Vdbe *v;
97921
97922 assert( pParse!=0 );
@@ -97974,11 +98275,11 @@
97974 return 1;
97975 }
97976 db->aDb[1].pBt = pBt;
97977 assert( db->aDb[1].pSchema );
97978 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
97979 db->mallocFailed = 1;
97980 return 1;
97981 }
97982 }
97983 return 0;
97984 }
@@ -98109,18 +98410,18 @@
98109 StrAccum errMsg;
98110 Table *pTab = pIdx->pTable;
98111
98112 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
98113 if( pIdx->aColExpr ){
98114 sqlite3XPrintf(&errMsg, 0, "index '%q'", pIdx->zName);
98115 }else{
98116 for(j=0; j<pIdx->nKeyCol; j++){
98117 char *zCol;
98118 assert( pIdx->aiColumn[j]>=0 );
98119 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
98120 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
98121 sqlite3XPrintf(&errMsg, 0, "%s.%s", pTab->zName, zCol);
98122 }
98123 }
98124 zErr = sqlite3StrAccumFinish(&errMsg);
98125 sqlite3HaltConstraint(pParse,
98126 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
@@ -98349,14 +98650,13 @@
98349 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
98350 pNew = sqlite3DbRealloc(db, pWith, nByte);
98351 }else{
98352 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
98353 }
98354 assert( zName!=0 || pNew==0 );
98355 assert( db->mallocFailed==0 || pNew==0 );
98356
98357 if( pNew==0 ){
98358 sqlite3ExprListDelete(db, pArglist);
98359 sqlite3SelectDelete(db, pQuery);
98360 sqlite3DbFree(db, zName);
98361 pNew = pWith;
98362 }else{
@@ -98566,11 +98866,11 @@
98566 ** return the pColl pointer to be deleted (because it wasn't added
98567 ** to the hash table).
98568 */
98569 assert( pDel==0 || pDel==pColl );
98570 if( pDel!=0 ){
98571 db->mallocFailed = 1;
98572 sqlite3DbFree(db, pDel);
98573 pColl = 0;
98574 }
98575 }
98576 }
@@ -98854,11 +99154,11 @@
98854 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
98855 }else{
98856 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
98857 }
98858 if( !p ){
98859 db->mallocFailed = 1;
98860 }else if ( 0==p->file_format ){
98861 sqlite3HashInit(&p->tblHash);
98862 sqlite3HashInit(&p->idxHash);
98863 sqlite3HashInit(&p->trigHash);
98864 sqlite3HashInit(&p->fkeyHash);
@@ -99308,11 +99608,11 @@
99308 if( eOnePass!=ONEPASS_OFF ){
99309 /* For ONEPASS, no need to store the rowid/primary-key. There is only
99310 ** one, so just keep it in its register(s) and fall through to the
99311 ** delete code. */
99312 nKey = nPk; /* OP_Found will use an unpacked key */
99313 aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
99314 if( aToOpen==0 ){
99315 sqlite3WhereEnd(pWInfo);
99316 goto delete_from_cleanup;
99317 }
99318 memset(aToOpen, 1, nIdx+1);
@@ -99348,17 +99648,16 @@
99348 ** only effect this statement has is to fire the INSTEAD OF
99349 ** triggers.
99350 */
99351 if( !isView ){
99352 int iAddrOnce = 0;
99353 u8 p5 = (eOnePass==ONEPASS_OFF ? 0 : OPFLAG_FORDELETE);
99354 if( eOnePass==ONEPASS_MULTI ){
99355 iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
99356 }
99357 testcase( IsVirtual(pTab) );
99358 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, p5, iTabCur,
99359 aToOpen, &iDataCur, &iIdxCur);
99360 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
99361 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
99362 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
99363 }
99364
@@ -99587,19 +99886,24 @@
99587
99588 /* Delete the index and table entries. Skip this step if pTab is really
99589 ** a view (in which case the only effect of the DELETE statement is to
99590 ** fire the INSTEAD OF triggers). */
99591 if( pTab->pSelect==0 ){
 
99592 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
99593 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
99594 if( count ){
99595 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
99596 }
 
 
 
99597 if( iIdxNoSeek>=0 ){
99598 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
99599 }
99600 sqlite3VdbeChangeP5(v, eMode==ONEPASS_MULTI);
 
99601 }
99602
99603 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
99604 ** handle rows (possibly in other tables) that refer via a foreign key
99605 ** to the row just deleted. */
@@ -100005,11 +100309,12 @@
100005 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
100006 x.nArg = argc-1;
100007 x.nUsed = 0;
100008 x.apArg = argv+1;
100009 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
100010 sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
 
100011 n = str.nChar;
100012 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
100013 SQLITE_DYNAMIC);
100014 }
100015 }
@@ -101380,11 +101685,11 @@
101380 */
101381 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
101382 int rc = sqlite3_overload_function(db, "MATCH", 2);
101383 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
101384 if( rc==SQLITE_NOMEM ){
101385 db->mallocFailed = 1;
101386 }
101387 }
101388
101389 /*
101390 ** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -101795,11 +102100,11 @@
101795 if( !zKey ) return 0;
101796 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
101797 }
101798 }else if( paiCol ){
101799 assert( nCol>1 );
101800 aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
101801 if( !aiCol ) return 1;
101802 *paiCol = aiCol;
101803 }
101804
101805 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
@@ -102741,11 +103046,10 @@
102741
102742 action = pFKey->aAction[iAction];
102743 pTrigger = pFKey->apTrigger[iAction];
102744
102745 if( action!=OE_None && !pTrigger ){
102746 u8 enableLookaside; /* Copy of db->lookaside.bEnabled */
102747 char const *zFrom; /* Name of child table */
102748 int nFrom; /* Length in bytes of zFrom */
102749 Index *pIdx = 0; /* Parent key index for this FK */
102750 int *aiCol = 0; /* child table cols -> parent key cols */
102751 TriggerStep *pStep = 0; /* First (only) step of trigger program */
@@ -102768,15 +103072,13 @@
102768
102769 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
102770 assert( iFromCol>=0 );
102771 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
102772 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
102773 tToCol.z = pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName;
102774 tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
102775
102776 tToCol.n = sqlite3Strlen30(tToCol.z);
102777 tFromCol.n = sqlite3Strlen30(tFromCol.z);
102778
102779 /* Create the expression "OLD.zToCol = zFromCol". It is important
102780 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
102781 ** that the affinity and collation sequence associated with the
102782 ** parent table are used for the comparison. */
@@ -102852,12 +103154,11 @@
102852 );
102853 pWhere = 0;
102854 }
102855
102856 /* Disable lookaside memory allocation */
102857 enableLookaside = db->lookaside.bEnabled;
102858 db->lookaside.bEnabled = 0;
102859
102860 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
102861 sizeof(Trigger) + /* struct Trigger */
102862 sizeof(TriggerStep) + /* Single step in trigger program */
102863 nFrom + 1 /* Space for pStep->zTarget */
@@ -102875,11 +103176,11 @@
102875 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
102876 }
102877 }
102878
102879 /* Re-enable the lookaside buffer, if it was disabled earlier. */
102880 db->lookaside.bEnabled = enableLookaside;
102881
102882 sqlite3ExprDelete(db, pWhere);
102883 sqlite3ExprDelete(db, pWhen);
102884 sqlite3ExprListDelete(db, pList);
102885 sqlite3SelectDelete(db, pSelect);
@@ -103070,11 +103371,11 @@
103070 */
103071 int n;
103072 Table *pTab = pIdx->pTable;
103073 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
103074 if( !pIdx->zColAff ){
103075 db->mallocFailed = 1;
103076 return 0;
103077 }
103078 for(n=0; n<pIdx->nColumn; n++){
103079 i16 x = pIdx->aiColumn[n];
103080 if( x>=0 ){
@@ -103121,11 +103422,11 @@
103121 char *zColAff = pTab->zColAff;
103122 if( zColAff==0 ){
103123 sqlite3 *db = sqlite3VdbeDb(v);
103124 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
103125 if( !zColAff ){
103126 db->mallocFailed = 1;
103127 return;
103128 }
103129
103130 for(i=0; i<pTab->nCol; i++){
103131 zColAff[i] = pTab->aCol[i].affinity;
@@ -103217,11 +103518,11 @@
103217 AutoincInfo *pInfo;
103218
103219 pInfo = pToplevel->pAinc;
103220 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
103221 if( pInfo==0 ){
103222 pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
103223 if( pInfo==0 ) return 0;
103224 pInfo->pNext = pToplevel->pAinc;
103225 pToplevel->pAinc = pInfo;
103226 pInfo->pTab = pTab;
103227 pInfo->iDb = iDb;
@@ -103241,47 +103542,59 @@
103241 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
103242 AutoincInfo *p; /* Information about an AUTOINCREMENT */
103243 sqlite3 *db = pParse->db; /* The database connection */
103244 Db *pDb; /* Database only autoinc table */
103245 int memId; /* Register holding max rowid */
103246 int addr; /* A VDBE address */
103247 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
103248
103249 /* This routine is never called during trigger-generation. It is
103250 ** only called from the top-level */
103251 assert( pParse->pTriggerTab==0 );
103252 assert( sqlite3IsToplevel(pParse) );
103253
103254 assert( v ); /* We failed long ago if this is not so */
103255 for(p = pParse->pAinc; p; p = p->pNext){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103256 pDb = &db->aDb[p->iDb];
103257 memId = p->regCtr;
103258 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103259 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
103260 sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
103261 addr = sqlite3VdbeCurrentAddr(v);
103262 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
103263 sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9); VdbeCoverage(v);
103264 sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
103265 sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId); VdbeCoverage(v);
103266 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
103267 sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
103268 sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
103269 sqlite3VdbeGoto(v, addr+9);
103270 sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2); VdbeCoverage(v);
103271 sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
103272 sqlite3VdbeAddOp0(v, OP_Close);
 
103273 }
103274 }
103275
103276 /*
103277 ** Update the maximum rowid for an autoincrement calculation.
103278 **
103279 ** This routine should be called when the top of the stack holds a
103280 ** new rowid that is about to be inserted. If that new rowid is
103281 ** larger than the maximum rowid in the memId memory cell, then the
103282 ** memory cell is updated. The stack is unchanged.
103283 */
103284 static void autoIncStep(Parse *pParse, int memId, int regRowid){
103285 if( memId>0 ){
103286 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
103287 }
@@ -103292,34 +103605,47 @@
103292 ** maximum rowid values back into the sqlite_sequence register.
103293 ** Every statement that might do an INSERT into an autoincrement
103294 ** table (either directly or through triggers) needs to call this
103295 ** routine just before the "exit" code.
103296 */
103297 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
103298 AutoincInfo *p;
103299 Vdbe *v = pParse->pVdbe;
103300 sqlite3 *db = pParse->db;
103301
103302 assert( v );
103303 for(p = pParse->pAinc; p; p = p->pNext){
 
 
 
 
 
 
 
 
 
103304 Db *pDb = &db->aDb[p->iDb];
103305 int addr1;
103306 int iRec;
103307 int memId = p->regCtr;
103308
103309 iRec = sqlite3GetTempReg(pParse);
103310 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103311 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
103312 addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1); VdbeCoverage(v);
103313 sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
103314 sqlite3VdbeJumpHere(v, addr1);
103315 sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
103316 sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
103317 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
103318 sqlite3VdbeAddOp0(v, OP_Close);
 
 
103319 sqlite3ReleaseTempReg(pParse, iRec);
103320 }
 
 
 
103321 }
103322 #else
103323 /*
103324 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
103325 ** above are all no-ops
@@ -103647,11 +103973,11 @@
103647 dest.iSdst = bIdListInOrder ? regData : 0;
103648 dest.nSdst = pTab->nCol;
103649 rc = sqlite3Select(pParse, pSelect, &dest);
103650 regFromSelect = dest.iSdst;
103651 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
103652 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
103653 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
103654 assert( pSelect->pEList );
103655 nColumn = pSelect->pEList->nExpr;
103656
103657 /* Set useTempTable to TRUE if the result of the SELECT statement
@@ -103749,11 +104075,11 @@
103749 /* If this is not a view, open the table and and all indices */
103750 if( !isView ){
103751 int nIdx;
103752 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
103753 &iDataCur, &iIdxCur);
103754 aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
103755 if( aRegIdx==0 ){
103756 goto insert_cleanup;
103757 }
103758 for(i=0; i<nIdx; i++){
103759 aRegIdx[i] = ++pParse->nMem;
@@ -103957,11 +104283,11 @@
103957 }else
103958 #endif
103959 {
103960 int isReplace; /* Set to true if constraints may cause a replace */
103961 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
103962 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
103963 );
103964 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
103965 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
103966 regIns, aRegIdx, 0, appendFlag, isReplace==0);
103967 }
@@ -104038,10 +104364,63 @@
104038 #undef pTrigger
104039 #endif
104040 #ifdef tmask
104041 #undef tmask
104042 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104043
104044 /*
104045 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
104046 ** on table pTab.
104047 **
@@ -104133,11 +104512,12 @@
104133 int regNewData, /* First register in a range holding values to insert */
104134 int regOldData, /* Previous content. 0 for INSERTs */
104135 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
104136 u8 overrideError, /* Override onError to this if not OE_Default */
104137 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
104138 int *pbMayReplace /* OUT: Set to true if constraint may cause a replace */
 
104139 ){
104140 Vdbe *v; /* VDBE under constrution */
104141 Index *pIdx; /* Pointer to one of the indices */
104142 Index *pPk = 0; /* The PRIMARY KEY index */
104143 sqlite3 *db; /* Database connection */
@@ -104179,14 +104559,18 @@
104179
104180 /* Test all NOT NULL constraints.
104181 */
104182 for(i=0; i<nCol; i++){
104183 if( i==pTab->iPKey ){
 
 
 
 
104184 continue;
104185 }
104186 onError = pTab->aCol[i].notNull;
104187 if( onError==OE_None ) continue;
104188 if( overrideError!=OE_Default ){
104189 onError = overrideError;
104190 }else if( onError==OE_Default ){
104191 onError = OE_Abort;
104192 }
@@ -104231,12 +104615,15 @@
104231 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
104232 ExprList *pCheck = pTab->pCheck;
104233 pParse->ckBase = regNewData+1;
104234 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
104235 for(i=0; i<pCheck->nExpr; i++){
104236 int allOk = sqlite3VdbeMakeLabel(v);
104237 sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
 
 
 
104238 if( onError==OE_Ignore ){
104239 sqlite3VdbeGoto(v, ignoreDest);
104240 }else{
104241 char *zName = pCheck->a[i].zName;
104242 if( zName==0 ) zName = pTab->zName;
@@ -104634,11 +105021,11 @@
104634 */
104635 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
104636 Parse *pParse, /* Parsing context */
104637 Table *pTab, /* Table to be opened */
104638 int op, /* OP_OpenRead or OP_OpenWrite */
104639 u8 p5, /* P5 value for OP_Open* instructions */
104640 int iBase, /* Use this for the table cursor, if there is one */
104641 u8 *aToOpen, /* If not NULL: boolean for each table and index */
104642 int *piDataCur, /* Write the database source cursor number here */
104643 int *piIdxCur /* Write the first index cursor number here */
104644 ){
@@ -104669,18 +105056,19 @@
104669 }
104670 if( piIdxCur ) *piIdxCur = iBase;
104671 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
104672 int iIdxCur = iBase++;
104673 assert( pIdx->pSchema==pTab->pSchema );
104674 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
104675 *piDataCur = iIdxCur;
104676 }
104677 if( aToOpen==0 || aToOpen[i+1] ){
104678 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
104679 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
104680 sqlite3VdbeChangeP5(v, p5);
104681 VdbeComment((v, "%s", pIdx->zName));
 
 
 
 
 
104682 }
104683 }
104684 if( iBase>pParse->nTab ) pParse->nTab = iBase;
104685 return i;
104686 }
@@ -105167,11 +105555,11 @@
105167 if( rc==SQLITE_ROW ){
105168 azVals = &azCols[nCol];
105169 for(i=0; i<nCol; i++){
105170 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
105171 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
105172 db->mallocFailed = 1;
105173 goto exec_out;
105174 }
105175 }
105176 }
105177 if( xCallback(pArg, nCol, azVals, azCols) ){
@@ -107055,32 +107443,35 @@
107055 /************** End of pragma.h **********************************************/
107056 /************** Continuing where we left off in pragma.c *********************/
107057
107058 /*
107059 ** Interpret the given string as a safety level. Return 0 for OFF,
107060 ** 1 for ON or NORMAL and 2 for FULL. Return 1 for an empty or
107061 ** unrecognized string argument. The FULL option is disallowed
107062 ** if the omitFull parameter it 1.
107063 **
107064 ** Note that the values returned are one less that the values that
107065 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
107066 ** to support legacy SQL code. The safety level used to be boolean
107067 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
107068 */
107069 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
107070 /* 123456789 123456789 */
107071 static const char zText[] = "onoffalseyestruefull";
107072 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
107073 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
107074 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 2};
 
107075 int i, n;
107076 if( sqlite3Isdigit(*z) ){
107077 return (u8)sqlite3Atoi(z);
107078 }
107079 n = sqlite3Strlen30(z);
107080 for(i=0; i<ArraySize(iLength)-omitFull; i++){
107081 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
 
 
107082 return iValue[i];
107083 }
107084 }
107085 return dflt;
107086 }
@@ -107467,12 +107858,11 @@
107467 aOp[1].p1 = iDb;
107468 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
107469 }else{
107470 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
107471 sqlite3BeginWriteOperation(pParse, 0, iDb);
107472 sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
107473 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
107474 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107475 pDb->pSchema->cache_size = size;
107476 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
107477 }
107478 break;
@@ -107499,11 +107889,11 @@
107499 /* Malloc may fail when setting the page-size, as there is an internal
107500 ** buffer that the pager module resizes using sqlite3_realloc().
107501 */
107502 db->nextPagesize = sqlite3Atoi(zRight);
107503 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
107504 db->mallocFailed = 1;
107505 }
107506 }
107507 break;
107508 }
107509
@@ -107706,23 +108096,22 @@
107706 static const VdbeOpList setMeta6[] = {
107707 { OP_Transaction, 0, 1, 0}, /* 0 */
107708 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
107709 { OP_If, 1, 0, 0}, /* 2 */
107710 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
107711 { OP_Integer, 0, 1, 0}, /* 4 */
107712 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 1}, /* 5 */
107713 };
107714 VdbeOp *aOp;
107715 int iAddr = sqlite3VdbeCurrentAddr(v);
107716 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
107717 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
107718 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
107719 aOp[0].p1 = iDb;
107720 aOp[1].p1 = iDb;
107721 aOp[2].p2 = iAddr+4;
107722 aOp[4].p1 = eAuto - 1;
107723 aOp[5].p1 = iDb;
107724 sqlite3VdbeUsesBtree(v, iDb);
107725 }
107726 }
107727 break;
107728 }
@@ -107997,11 +108386,11 @@
107997 }
107998 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
107999
108000 /*
108001 ** PRAGMA [schema.]synchronous
108002 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL
108003 **
108004 ** Return or set the local value of the synchronous flag. Changing
108005 ** the local value does not make changes to the disk file and the
108006 ** default value will be restored the next time the database is
108007 ** opened.
@@ -108624,20 +109013,19 @@
108624 }
108625 {
108626 static const int iLn = VDBE_OFFSET_LINENO(2);
108627 static const VdbeOpList endCode[] = {
108628 { OP_AddImm, 1, 0, 0}, /* 0 */
108629 { OP_If, 1, 0, 0}, /* 1 */
108630 { OP_String8, 0, 3, 0}, /* 2 */
108631 { OP_ResultRow, 3, 1, 0},
108632 };
108633 VdbeOp *aOp;
108634
108635 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
108636 if( aOp ){
108637 aOp[0].p2 = -mxErr;
108638 aOp[1].p2 = sqlite3VdbeCurrentAddr(v);
108639 aOp[2].p4type = P4_STATIC;
108640 aOp[2].p4.z = "ok";
108641 }
108642 }
108643 }
@@ -108751,21 +109139,20 @@
108751 sqlite3VdbeUsesBtree(v, iDb);
108752 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
108753 /* Write the specified cookie value */
108754 static const VdbeOpList setCookie[] = {
108755 { OP_Transaction, 0, 1, 0}, /* 0 */
108756 { OP_Integer, 0, 1, 0}, /* 1 */
108757 { OP_SetCookie, 0, 0, 1}, /* 2 */
108758 };
108759 VdbeOp *aOp;
108760 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
108761 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
108762 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108763 aOp[0].p1 = iDb;
108764 aOp[1].p1 = sqlite3Atoi(zRight);
108765 aOp[2].p1 = iDb;
108766 aOp[2].p2 = iCookie;
108767 }else{
108768 /* Read the specified cookie value */
108769 static const VdbeOpList readCookie[] = {
108770 { OP_Transaction, 0, 0, 0}, /* 0 */
108771 { OP_ReadCookie, 0, 1, 0}, /* 1 */
@@ -109033,15 +109420,14 @@
109033 ){
109034 sqlite3 *db = pData->db;
109035 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
109036 char *z;
109037 if( zObj==0 ) zObj = "?";
109038 z = sqlite3_mprintf("malformed database schema (%s)", zObj);
109039 if( z && zExtra ) z = sqlite3_mprintf("%z - %s", z, zExtra);
109040 sqlite3DbFree(db, *pData->pzErrMsg);
109041 *pData->pzErrMsg = z;
109042 if( z==0 ) db->mallocFailed = 1;
109043 }
109044 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
109045 }
109046
109047 /*
@@ -109096,11 +109482,11 @@
109096 if( db->init.orphanTrigger ){
109097 assert( iDb==1 );
109098 }else{
109099 pData->rc = rc;
109100 if( rc==SQLITE_NOMEM ){
109101 db->mallocFailed = 1;
109102 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
109103 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
109104 }
109105 }
109106 }
@@ -109341,11 +109727,11 @@
109341 }
109342 sqlite3BtreeLeave(pDb->pBt);
109343
109344 error_out:
109345 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109346 db->mallocFailed = 1;
109347 }
109348 return rc;
109349 }
109350
109351 /*
@@ -109439,11 +109825,11 @@
109439 ** on the b-tree database, open one now. If a transaction is opened, it
109440 ** will be closed immediately after reading the meta-value. */
109441 if( !sqlite3BtreeIsInReadTrans(pBt) ){
109442 rc = sqlite3BtreeBeginTrans(pBt, 0);
109443 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109444 db->mallocFailed = 1;
109445 }
109446 if( rc!=SQLITE_OK ) return;
109447 openedTransaction = 1;
109448 }
109449
@@ -109502,10 +109888,15 @@
109502 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
109503 if( pParse ){
109504 sqlite3 *db = pParse->db;
109505 sqlite3DbFree(db, pParse->aLabel);
109506 sqlite3ExprListDelete(db, pParse->pConstExpr);
 
 
 
 
 
109507 }
109508 }
109509
109510 /*
109511 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
@@ -109530,11 +109921,11 @@
109530 rc = SQLITE_NOMEM;
109531 goto end_prepare;
109532 }
109533 pParse->pReprepare = pReprepare;
109534 assert( ppStmt && *ppStmt==0 );
109535 assert( !db->mallocFailed );
109536 assert( sqlite3_mutex_held(db->mutex) );
109537
109538 /* Check to verify that it is possible to get a read lock on all
109539 ** database schemas. The inability to get a read lock indicates that
109540 ** some other database connection is holding a write-lock, which in
@@ -109587,23 +109978,20 @@
109587 goto end_prepare;
109588 }
109589 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
109590 if( zSqlCopy ){
109591 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
109592 sqlite3DbFree(db, zSqlCopy);
109593 pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
 
109594 }else{
109595 pParse->zTail = &zSql[nBytes];
109596 }
109597 }else{
109598 sqlite3RunParser(pParse, zSql, &zErrMsg);
109599 }
109600 assert( 0==pParse->nQueryLoop );
109601
109602 if( db->mallocFailed ){
109603 pParse->rc = SQLITE_NOMEM;
109604 }
109605 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
109606 if( pParse->checkSchema ){
109607 schemaIsValid(pParse);
109608 }
109609 if( db->mallocFailed ){
@@ -109721,11 +110109,11 @@
109721 db = sqlite3VdbeDb(p);
109722 assert( sqlite3_mutex_held(db->mutex) );
109723 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
109724 if( rc ){
109725 if( rc==SQLITE_NOMEM ){
109726 db->mallocFailed = 1;
109727 }
109728 assert( pNew==0 );
109729 return rc;
109730 }else{
109731 assert( pNew!=0 );
@@ -109975,11 +110363,11 @@
109975 Expr *pOffset /* OFFSET value. NULL means no offset */
109976 ){
109977 Select *pNew;
109978 Select standin;
109979 sqlite3 *db = pParse->db;
109980 pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
109981 if( pNew==0 ){
109982 assert( db->mallocFailed );
109983 pNew = &standin;
109984 }
109985 if( pEList==0 ){
@@ -110879,11 +111267,11 @@
110879 p->enc = ENC(db);
110880 p->db = db;
110881 p->nRef = 1;
110882 memset(&p[1], 0, nExtra);
110883 }else{
110884 db->mallocFailed = 1;
110885 }
110886 return p;
110887 }
110888
110889 /*
@@ -111540,11 +111928,11 @@
111540 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
111541 }
111542 pCol->zName = zName;
111543 sqlite3ColumnPropertiesFromName(0, pCol);
111544 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
111545 db->mallocFailed = 1;
111546 }
111547 }
111548 sqlite3HashClear(&ht);
111549 if( db->mallocFailed ){
111550 for(j=0; j<i; j++){
@@ -111627,11 +112015,11 @@
111627 if( pTab==0 ){
111628 return 0;
111629 }
111630 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
111631 ** is disabled */
111632 assert( db->lookaside.bEnabled==0 );
111633 pTab->nRef = 1;
111634 pTab->zName = 0;
111635 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
111636 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
111637 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
@@ -111723,14 +112111,12 @@
111723 p->iOffset = iOffset = ++pParse->nMem;
111724 pParse->nMem++; /* Allocate an extra register for limit+offset */
111725 sqlite3ExprCode(pParse, p->pOffset, iOffset);
111726 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
111727 VdbeComment((v, "OFFSET counter"));
111728 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iOffset, iOffset, 0);
111729 sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
111730 VdbeComment((v, "LIMIT+OFFSET"));
111731 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, iLimit, iOffset+1, -1);
111732 }
111733 }
111734 }
111735
111736 #ifndef SQLITE_OMIT_COMPOUND_SELECT
@@ -112143,13 +112529,12 @@
112143 p->iOffset = pPrior->iOffset;
112144 if( p->iLimit ){
112145 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
112146 VdbeComment((v, "Jump ahead if LIMIT reached"));
112147 if( p->iOffset ){
112148 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iOffset, p->iOffset, 0);
112149 sqlite3VdbeAddOp3(v, OP_Add, p->iLimit, p->iOffset, p->iOffset+1);
112150 sqlite3VdbeAddOp3(v, OP_SetIfNotPos, p->iLimit, p->iOffset+1, -1);
112151 }
112152 }
112153 explainSetInteger(iSub2, pParse->iNextSelectId);
112154 rc = sqlite3Select(pParse, p, &dest);
112155 testcase( rc!=SQLITE_OK );
@@ -112736,14 +113121,15 @@
112736 ** row of results comes from selectA or selectB. Also add explicit
112737 ** collations to the ORDER BY clause terms so that when the subqueries
112738 ** to the right and the left are evaluated, they use the correct
112739 ** collation.
112740 */
112741 aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
112742 if( aPermute ){
112743 struct ExprList_item *pItem;
112744 for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
 
112745 assert( pItem->u.x.iOrderByCol>0 );
112746 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
112747 aPermute[i] = pItem->u.x.iOrderByCol - 1;
112748 }
112749 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
@@ -112817,11 +113203,11 @@
112817 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
112818 VdbeComment((v, "left SELECT"));
112819 pPrior->iLimit = regLimitA;
112820 explainSetInteger(iSub1, pParse->iNextSelectId);
112821 sqlite3Select(pParse, pPrior, &destA);
112822 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrA);
112823 sqlite3VdbeJumpHere(v, addr1);
112824
112825 /* Generate a coroutine to evaluate the SELECT statement on
112826 ** the right - the "B" select
112827 */
@@ -112834,11 +113220,11 @@
112834 p->iOffset = 0;
112835 explainSetInteger(iSub2, pParse->iNextSelectId);
112836 sqlite3Select(pParse, p, &destB);
112837 p->iLimit = savedLimit;
112838 p->iOffset = savedOffset;
112839 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regAddrB);
112840
112841 /* Generate a subroutine that outputs the current row of the A
112842 ** select as the next output row of the compound select.
112843 */
112844 VdbeNoopComment((v, "Output routine for A"));
@@ -114301,12 +114687,11 @@
114301 }
114302 }else{
114303 pExpr = pRight;
114304 }
114305 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
114306 sColname.z = zColname;
114307 sColname.n = sqlite3Strlen30(zColname);
114308 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
114309 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
114310 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
114311 if( pSub ){
114312 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
@@ -114856,11 +115241,11 @@
114856 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
114857 sqlite3Select(pParse, pSub, &dest);
114858 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
114859 pItem->fg.viaCoroutine = 1;
114860 pItem->regResult = dest.iSdst;
114861 sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
114862 sqlite3VdbeJumpHere(v, addrTop-1);
114863 sqlite3ClearTempRegCache(pParse);
114864 }else{
114865 /* Generate a subroutine that will fill an ephemeral table with
114866 ** the content of this subquery. pItem->addrFillSub will point
@@ -115428,11 +115813,12 @@
115428 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
115429
115430 if( flag ){
115431 pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
115432 pDel = pMinMax;
115433 if( pMinMax && !db->mallocFailed ){
 
115434 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
115435 pMinMax->a[0].pExpr->op = TK_COLUMN;
115436 }
115437 }
115438
@@ -116001,12 +116387,11 @@
116001 pTrig->step_list = pStepList;
116002 while( pStepList ){
116003 pStepList->pTrig = pTrig;
116004 pStepList = pStepList->pNext;
116005 }
116006 nameToken.z = pTrig->zName;
116007 nameToken.n = sqlite3Strlen30(nameToken.z);
116008 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
116009 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
116010 || sqlite3FixExpr(&sFix, pTrig->pWhen)
116011 ){
116012 goto triggerfinish_cleanup;
@@ -116038,11 +116423,11 @@
116038 Trigger *pLink = pTrig;
116039 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
116040 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
116041 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
116042 if( pTrig ){
116043 db->mallocFailed = 1;
116044 }else if( pLink->pSchema==pLink->pTabSchema ){
116045 Table *pTab;
116046 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
116047 assert( pTab!=0 );
116048 pLink->pNext = pTab->pTrigger;
@@ -117014,11 +117399,11 @@
117014 }
117015
117016 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
117017 ** Initialize aXRef[] and aToOpen[] to their default values.
117018 */
117019 aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
117020 if( aXRef==0 ) goto update_cleanup;
117021 aRegIdx = aXRef+pTab->nCol;
117022 aToOpen = (u8*)(aRegIdx+nIdx);
117023 memset(aToOpen, 1, nIdx+1);
117024 aToOpen[nIdx+1] = 0;
@@ -117389,11 +117774,12 @@
117389 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
117390
117391 /* Do constraint checks. */
117392 assert( regOldRowid>0 );
117393 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
117394 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
 
117395
117396 /* Do FK constraint checks. */
117397 if( hasFK ){
117398 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
117399 }
@@ -118054,11 +118440,11 @@
118054 nName = sqlite3Strlen30(zName);
118055 if( sqlite3HashFind(&db->aModule, zName) ){
118056 rc = SQLITE_MISUSE_BKPT;
118057 }else{
118058 Module *pMod;
118059 pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
118060 if( pMod ){
118061 Module *pDel;
118062 char *zCopy = (char *)(&pMod[1]);
118063 memcpy(zCopy, zName, nName+1);
118064 pMod->zName = zCopy;
@@ -118067,11 +118453,11 @@
118067 pMod->xDestroy = xDestroy;
118068 pMod->pEpoTab = 0;
118069 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
118070 assert( pDel==0 || pDel==pMod );
118071 if( pDel ){
118072 db->mallocFailed = 1;
118073 sqlite3DbFree(db, pDel);
118074 }
118075 }
118076 }
118077 rc = sqlite3ApiExit(db, rc);
@@ -118444,11 +118830,11 @@
118444 Schema *pSchema = pTab->pSchema;
118445 const char *zName = pTab->zName;
118446 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
118447 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
118448 if( pOld ){
118449 db->mallocFailed = 1;
118450 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
118451 return;
118452 }
118453 pParse->pNewTable = 0;
118454 }
@@ -118535,11 +118921,11 @@
118535 sCtx.pPrior = db->pVtabCtx;
118536 sCtx.bDeclared = 0;
118537 db->pVtabCtx = &sCtx;
118538 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
118539 db->pVtabCtx = sCtx.pPrior;
118540 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
118541 assert( sCtx.pTab==pTab );
118542
118543 if( SQLITE_OK!=rc ){
118544 if( zErr==0 ){
118545 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
@@ -119093,11 +119479,11 @@
119093 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
119094 if( apVtabLock ){
119095 pToplevel->apVtabLock = apVtabLock;
119096 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
119097 }else{
119098 pToplevel->db->mallocFailed = 1;
119099 }
119100 }
119101
119102 /*
119103 ** Check to see if virtual tale module pMod can be have an eponymous
@@ -119835,11 +120221,11 @@
119835 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
119836 sqlite3StrAccumAppend(pStr, " (", 2);
119837 for(i=0; i<nEq; i++){
119838 const char *z = explainIndexColumnName(pIndex, i);
119839 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
119840 sqlite3XPrintf(pStr, 0, i>=nSkip ? "%s=?" : "ANY(%s)", z);
119841 }
119842
119843 j = i;
119844 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
119845 const char *z = explainIndexColumnName(pIndex, i);
@@ -119894,17 +120280,17 @@
119894 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
119895
119896 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
119897 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
119898 if( pItem->pSelect ){
119899 sqlite3XPrintf(&str, 0, " SUBQUERY %d", pItem->iSelectId);
119900 }else{
119901 sqlite3XPrintf(&str, 0, " TABLE %s", pItem->zName);
119902 }
119903
119904 if( pItem->zAlias ){
119905 sqlite3XPrintf(&str, 0, " AS %s", pItem->zAlias);
119906 }
119907 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
119908 const char *zFmt = 0;
119909 Index *pIdx;
119910
@@ -119924,11 +120310,11 @@
119924 }else{
119925 zFmt = "INDEX %s";
119926 }
119927 if( zFmt ){
119928 sqlite3StrAccumAppend(&str, " USING ", 7);
119929 sqlite3XPrintf(&str, 0, zFmt, pIdx->zName);
119930 explainIndexRange(&str, pLoop);
119931 }
119932 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
119933 const char *zRangeOp;
119934 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -119939,21 +120325,21 @@
119939 zRangeOp = ">";
119940 }else{
119941 assert( flags&WHERE_TOP_LIMIT);
119942 zRangeOp = "<";
119943 }
119944 sqlite3XPrintf(&str, 0, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
119945 }
119946 #ifndef SQLITE_OMIT_VIRTUALTABLE
119947 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
119948 sqlite3XPrintf(&str, 0, " VIRTUAL TABLE INDEX %d:%s",
119949 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
119950 }
119951 #endif
119952 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
119953 if( pLoop->nOut>=10 ){
119954 sqlite3XPrintf(&str, 0, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
119955 }else{
119956 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
119957 }
119958 #endif
119959 zMsg = sqlite3StrAccumFinish(&str);
@@ -120254,13 +120640,11 @@
120254 regBase = pParse->nMem + 1;
120255 nReg = pLoop->u.btree.nEq + nExtraReg;
120256 pParse->nMem += nReg;
120257
120258 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
120259 if( !zAff ){
120260 pParse->db->mallocFailed = 1;
120261 }
120262
120263 if( nSkip ){
120264 int iIdxCur = pLevel->iIdxCur;
120265 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
120266 VdbeCoverageIf(v, bRev==0);
@@ -120504,10 +120888,58 @@
120504 }
120505 }
120506 #else
120507 # define codeCursorHint(A,B,C) /* No-op */
120508 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120509
120510 /*
120511 ** Generate code for the start of the iLevel-th loop in the WHERE clause
120512 ** implementation described by pWInfo.
120513 */
@@ -120984,18 +121416,18 @@
120984 disableTerm(pLevel, pRangeStart);
120985 disableTerm(pLevel, pRangeEnd);
120986 if( omitTable ){
120987 /* pIdx is a covering index. No need to access the main table. */
120988 }else if( HasRowid(pIdx->pTable) ){
120989 iRowidReg = ++pParse->nMem;
120990 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
120991 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
120992 if( pWInfo->eOnePass!=ONEPASS_OFF ){
 
 
 
120993 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
120994 VdbeCoverage(v);
120995 }else{
120996 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
120997 }
120998 }else if( iCur!=iIdxCur ){
120999 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
121000 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
121001 for(j=0; j<pPk->nKeyCol; j++){
@@ -121160,11 +121592,13 @@
121160 int iTerm;
121161 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
121162 Expr *pExpr = pWC->a[iTerm].pExpr;
121163 if( &pWC->a[iTerm] == pTerm ) continue;
121164 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
121165 if( (pWC->a[iTerm].wtFlags & TERM_VIRTUAL)!=0 ) continue;
 
 
121166 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
121167 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
121168 pExpr = sqlite3ExprDup(db, pExpr, 0);
121169 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
121170 }
@@ -121500,11 +121934,11 @@
121500 int idx;
121501 testcase( wtFlags & TERM_VIRTUAL );
121502 if( pWC->nTerm>=pWC->nSlot ){
121503 WhereTerm *pOld = pWC->a;
121504 sqlite3 *db = pWC->pWInfo->pParse->db;
121505 pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
121506 if( pWC->a==0 ){
121507 if( wtFlags & TERM_DYNAMIC ){
121508 sqlite3ExprDelete(db, p);
121509 }
121510 pWC->a = pOld;
@@ -121985,11 +122419,11 @@
121985 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
121986 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
121987 WhereAndInfo *pAndInfo;
121988 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
121989 chngToIN = 0;
121990 pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
121991 if( pAndInfo ){
121992 WhereClause *pAndWC;
121993 WhereTerm *pAndTerm;
121994 int j;
121995 Bitmask b = 0;
@@ -121999,11 +122433,10 @@
121999 pAndWC = &pAndInfo->wc;
122000 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
122001 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
122002 sqlite3WhereExprAnalyze(pSrc, pAndWC);
122003 pAndWC->pOuter = pWC;
122004 testcase( db->mallocFailed );
122005 if( !db->mallocFailed ){
122006 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
122007 assert( pAndTerm->pExpr );
122008 if( allowedOp(pAndTerm->pExpr->op) ){
122009 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
@@ -123742,11 +124175,11 @@
123742 rc = pVtab->pModule->xBestIndex(pVtab, p);
123743 TRACE_IDX_OUTPUTS(p);
123744
123745 if( rc!=SQLITE_OK ){
123746 if( rc==SQLITE_NOMEM ){
123747 pParse->db->mallocFailed = 1;
123748 }else if( !pVtab->zErrMsg ){
123749 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
123750 }else{
123751 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
123752 }
@@ -124534,11 +124967,11 @@
124534 */
124535 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
124536 WhereTerm **paNew;
124537 if( p->nLSlot>=n ) return SQLITE_OK;
124538 n = (n+7)&~7;
124539 paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
124540 if( paNew==0 ) return SQLITE_NOMEM;
124541 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
124542 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
124543 p->aLTerm = paNew;
124544 p->nLSlot = n;
@@ -124831,11 +125264,11 @@
124831 whereLoopPrint(pTemplate, pBuilder->pWC);
124832 }
124833 #endif
124834 if( p==0 ){
124835 /* Allocate a new WhereLoop to add to the end of the list */
124836 *ppPrev = p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
124837 if( p==0 ) return SQLITE_NOMEM;
124838 whereLoopInit(p);
124839 p->pNextLoop = 0;
124840 }else{
124841 /* We will be overwriting WhereLoop p[]. But before we do, first
@@ -126237,11 +126670,10 @@
126237 ** Return the cost of sorting nRow rows, assuming that the keys have
126238 ** nOrderby columns and that the first nSorted columns are already in
126239 ** order.
126240 */
126241 static LogEst whereSortingCost(
126242 WhereInfo *pWInfo,
126243 LogEst nRow,
126244 int nOrderBy,
126245 int nSorted
126246 ){
126247 /* TUNING: Estimated cost of a full external sort, where N is
@@ -126259,18 +126691,10 @@
126259 ** below. */
126260 LogEst rScale, rSortCost;
126261 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
126262 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
126263 rSortCost = nRow + estLog(nRow) + rScale + 16;
126264
126265 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
126266 ** similar but with a larger constant of proportionality.
126267 ** Multiply by an additional factor of 3.0. */
126268 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
126269 rSortCost += 16;
126270 }
126271
126272 return rSortCost;
126273 }
126274
126275 /*
126276 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
@@ -126328,11 +126752,11 @@
126328 }
126329
126330 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
126331 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
126332 nSpace += sizeof(LogEst) * nOrderBy;
126333 pSpace = sqlite3DbMallocRaw(db, nSpace);
126334 if( pSpace==0 ) return SQLITE_NOMEM;
126335 aTo = (WherePath*)pSpace;
126336 aFrom = aTo+mxChoice;
126337 memset(aFrom, 0, sizeof(aFrom[0]));
126338 pX = (WhereLoop**)(aFrom+mxChoice);
@@ -126400,11 +126824,11 @@
126400 revMask = pFrom->revLoop;
126401 }
126402 if( isOrdered>=0 && isOrdered<nOrderBy ){
126403 if( aSortCost[isOrdered]==0 ){
126404 aSortCost[isOrdered] = whereSortingCost(
126405 pWInfo, nRowEst, nOrderBy, isOrdered
126406 );
126407 }
126408 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
126409
126410 WHERETRACE(0x002,
@@ -126813,11 +127237,11 @@
126813 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
126814 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
126815 int ii; /* Loop counter */
126816 sqlite3 *db; /* Database connection */
126817 int rc; /* Return code */
126818 u8 bFordelete = 0;
126819
126820 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
126821 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
126822 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
126823 ));
@@ -127058,20 +127482,19 @@
127058 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
127059 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
127060
127061 /* If the caller is an UPDATE or DELETE statement that is requesting
127062 ** to use a one-pass algorithm, determine if this is appropriate.
127063 ** The one-pass algorithm only works if the WHERE clause constrains
127064 ** the statement to update or delete a single row.
127065 */
127066 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
127067 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
127068 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
127069 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
127070 if( bOnerow || ( (wctrlFlags & WHERE_ONEPASS_MULTIROW)
127071 && 0==(wsFlags & WHERE_VIRTUALTABLE)
127072 )){
 
127073 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
127074 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
127075 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
127076 bFordelete = OPFLAG_FORDELETE;
127077 }
@@ -127511,10 +127934,19 @@
127511 /*
127512 ** An instance of this structure holds the ATTACH key and the key type.
127513 */
127514 struct AttachKey { int type; Token key; };
127515
 
 
 
 
 
 
 
 
 
127516
127517 /*
127518 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
127519 ** all elements in the list. And make sure list length does not exceed
127520 ** SQLITE_LIMIT_COMPOUND_SELECT.
@@ -127593,11 +128025,11 @@
127593
127594 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
127595 ** unary TK_ISNULL or TK_NOTNULL expression. */
127596 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
127597 sqlite3 *db = pParse->db;
127598 if( pY && pA && pY->op==TK_NULL ){
127599 pA->op = (u8)op;
127600 sqlite3ExprDelete(db, pA->pRight);
127601 pA->pRight = 0;
127602 }
127603 }
@@ -129635,11 +130067,11 @@
129635 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
129636 }
129637 break;
129638 case 27: /* createkw ::= CREATE */
129639 {
129640 pParse->db->lookaside.bEnabled = 0;
129641 yygotominor.yy0 = yymsp[0].minor.yy0;
129642 }
129643 break;
129644 case 28: /* ifnotexists ::= */
129645 case 31: /* temp ::= */ yytestcase(yyruleno==31);
@@ -130717,11 +131149,11 @@
130717 sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
130718 }
130719 break;
130720 case 307: /* add_column_fullname ::= fullname */
130721 {
130722 pParse->db->lookaside.bEnabled = 0;
130723 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
130724 }
130725 break;
130726 case 310: /* cmd ::= create_vtab */
130727 {sqlite3VtabFinishParse(pParse,0);}
@@ -131097,16 +131529,96 @@
131097 ** parser for analysis.
131098 */
131099 /* #include "sqliteInt.h" */
131100 /* #include <stdlib.h> */
131101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
131102 /*
131103 ** The charMap() macro maps alphabetic characters into their
131104 ** lower-case ASCII equivalent. On ASCII machines, this is just
131105 ** an upper-to-lower case map. On EBCDIC machines we also need
131106 ** to adjust the encoding. Only alphabetic characters and underscores
131107 ** need to be translated.
 
 
131108 */
131109 #ifdef SQLITE_ASCII
131110 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
131111 #endif
131112 #ifdef SQLITE_EBCDIC
@@ -131136,11 +131648,11 @@
131136 ** The sqlite3KeywordCode function looks up an identifier to determine if
131137 ** it is a keyword. If it is a keyword, the token code of that keyword is
131138 ** returned. If the input is not a keyword, TK_ID is returned.
131139 **
131140 ** The implementation of this routine was generated by a program,
131141 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
131142 ** The output of the mkkeywordhash.c program is written into a file
131143 ** named keywordhash.h and then included into this source file by
131144 ** the #include below.
131145 */
131146 /************** Include keywordhash.h in the middle of tokenize.c ************/
@@ -131277,142 +131789,151 @@
131277 TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW,
131278 TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT,
131279 TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING,
131280 TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL,
131281 };
131282 int h, i;
 
131283 if( n>=2 ){
131284 h = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
131285 for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
131286 if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
131287 testcase( i==0 ); /* REINDEX */
131288 testcase( i==1 ); /* INDEXED */
131289 testcase( i==2 ); /* INDEX */
131290 testcase( i==3 ); /* DESC */
131291 testcase( i==4 ); /* ESCAPE */
131292 testcase( i==5 ); /* EACH */
131293 testcase( i==6 ); /* CHECK */
131294 testcase( i==7 ); /* KEY */
131295 testcase( i==8 ); /* BEFORE */
131296 testcase( i==9 ); /* FOREIGN */
131297 testcase( i==10 ); /* FOR */
131298 testcase( i==11 ); /* IGNORE */
131299 testcase( i==12 ); /* REGEXP */
131300 testcase( i==13 ); /* EXPLAIN */
131301 testcase( i==14 ); /* INSTEAD */
131302 testcase( i==15 ); /* ADD */
131303 testcase( i==16 ); /* DATABASE */
131304 testcase( i==17 ); /* AS */
131305 testcase( i==18 ); /* SELECT */
131306 testcase( i==19 ); /* TABLE */
131307 testcase( i==20 ); /* LEFT */
131308 testcase( i==21 ); /* THEN */
131309 testcase( i==22 ); /* END */
131310 testcase( i==23 ); /* DEFERRABLE */
131311 testcase( i==24 ); /* ELSE */
131312 testcase( i==25 ); /* EXCEPT */
131313 testcase( i==26 ); /* TRANSACTION */
131314 testcase( i==27 ); /* ACTION */
131315 testcase( i==28 ); /* ON */
131316 testcase( i==29 ); /* NATURAL */
131317 testcase( i==30 ); /* ALTER */
131318 testcase( i==31 ); /* RAISE */
131319 testcase( i==32 ); /* EXCLUSIVE */
131320 testcase( i==33 ); /* EXISTS */
131321 testcase( i==34 ); /* SAVEPOINT */
131322 testcase( i==35 ); /* INTERSECT */
131323 testcase( i==36 ); /* TRIGGER */
131324 testcase( i==37 ); /* REFERENCES */
131325 testcase( i==38 ); /* CONSTRAINT */
131326 testcase( i==39 ); /* INTO */
131327 testcase( i==40 ); /* OFFSET */
131328 testcase( i==41 ); /* OF */
131329 testcase( i==42 ); /* SET */
131330 testcase( i==43 ); /* TEMPORARY */
131331 testcase( i==44 ); /* TEMP */
131332 testcase( i==45 ); /* OR */
131333 testcase( i==46 ); /* UNIQUE */
131334 testcase( i==47 ); /* QUERY */
131335 testcase( i==48 ); /* WITHOUT */
131336 testcase( i==49 ); /* WITH */
131337 testcase( i==50 ); /* OUTER */
131338 testcase( i==51 ); /* RELEASE */
131339 testcase( i==52 ); /* ATTACH */
131340 testcase( i==53 ); /* HAVING */
131341 testcase( i==54 ); /* GROUP */
131342 testcase( i==55 ); /* UPDATE */
131343 testcase( i==56 ); /* BEGIN */
131344 testcase( i==57 ); /* INNER */
131345 testcase( i==58 ); /* RECURSIVE */
131346 testcase( i==59 ); /* BETWEEN */
131347 testcase( i==60 ); /* NOTNULL */
131348 testcase( i==61 ); /* NOT */
131349 testcase( i==62 ); /* NO */
131350 testcase( i==63 ); /* NULL */
131351 testcase( i==64 ); /* LIKE */
131352 testcase( i==65 ); /* CASCADE */
131353 testcase( i==66 ); /* ASC */
131354 testcase( i==67 ); /* DELETE */
131355 testcase( i==68 ); /* CASE */
131356 testcase( i==69 ); /* COLLATE */
131357 testcase( i==70 ); /* CREATE */
131358 testcase( i==71 ); /* CURRENT_DATE */
131359 testcase( i==72 ); /* DETACH */
131360 testcase( i==73 ); /* IMMEDIATE */
131361 testcase( i==74 ); /* JOIN */
131362 testcase( i==75 ); /* INSERT */
131363 testcase( i==76 ); /* MATCH */
131364 testcase( i==77 ); /* PLAN */
131365 testcase( i==78 ); /* ANALYZE */
131366 testcase( i==79 ); /* PRAGMA */
131367 testcase( i==80 ); /* ABORT */
131368 testcase( i==81 ); /* VALUES */
131369 testcase( i==82 ); /* VIRTUAL */
131370 testcase( i==83 ); /* LIMIT */
131371 testcase( i==84 ); /* WHEN */
131372 testcase( i==85 ); /* WHERE */
131373 testcase( i==86 ); /* RENAME */
131374 testcase( i==87 ); /* AFTER */
131375 testcase( i==88 ); /* REPLACE */
131376 testcase( i==89 ); /* AND */
131377 testcase( i==90 ); /* DEFAULT */
131378 testcase( i==91 ); /* AUTOINCREMENT */
131379 testcase( i==92 ); /* TO */
131380 testcase( i==93 ); /* IN */
131381 testcase( i==94 ); /* CAST */
131382 testcase( i==95 ); /* COLUMN */
131383 testcase( i==96 ); /* COMMIT */
131384 testcase( i==97 ); /* CONFLICT */
131385 testcase( i==98 ); /* CROSS */
131386 testcase( i==99 ); /* CURRENT_TIMESTAMP */
131387 testcase( i==100 ); /* CURRENT_TIME */
131388 testcase( i==101 ); /* PRIMARY */
131389 testcase( i==102 ); /* DEFERRED */
131390 testcase( i==103 ); /* DISTINCT */
131391 testcase( i==104 ); /* IS */
131392 testcase( i==105 ); /* DROP */
131393 testcase( i==106 ); /* FAIL */
131394 testcase( i==107 ); /* FROM */
131395 testcase( i==108 ); /* FULL */
131396 testcase( i==109 ); /* GLOB */
131397 testcase( i==110 ); /* BY */
131398 testcase( i==111 ); /* IF */
131399 testcase( i==112 ); /* ISNULL */
131400 testcase( i==113 ); /* ORDER */
131401 testcase( i==114 ); /* RESTRICT */
131402 testcase( i==115 ); /* RIGHT */
131403 testcase( i==116 ); /* ROLLBACK */
131404 testcase( i==117 ); /* ROW */
131405 testcase( i==118 ); /* UNION */
131406 testcase( i==119 ); /* USING */
131407 testcase( i==120 ); /* VACUUM */
131408 testcase( i==121 ); /* VIEW */
131409 testcase( i==122 ); /* INITIALLY */
131410 testcase( i==123 ); /* ALL */
131411 *pType = aCode[i];
131412 break;
131413 }
 
 
 
 
 
 
 
 
131414 }
131415 }
131416 return n;
131417 }
131418 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
@@ -131469,74 +131990,76 @@
131469 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
131470 #endif
131471
131472
131473 /*
131474 ** Return the length of the token that begins at z[0].
131475 ** Store the token type in *tokenType before returning.
131476 */
131477 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
131478 int i, c;
131479 switch( *z ){
131480 case ' ': case '\t': case '\n': case '\f': case '\r': {
 
 
131481 testcase( z[0]==' ' );
131482 testcase( z[0]=='\t' );
131483 testcase( z[0]=='\n' );
131484 testcase( z[0]=='\f' );
131485 testcase( z[0]=='\r' );
131486 for(i=1; sqlite3Isspace(z[i]); i++){}
131487 *tokenType = TK_SPACE;
131488 return i;
131489 }
131490 case '-': {
131491 if( z[1]=='-' ){
131492 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
131493 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
131494 return i;
131495 }
131496 *tokenType = TK_MINUS;
131497 return 1;
131498 }
131499 case '(': {
131500 *tokenType = TK_LP;
131501 return 1;
131502 }
131503 case ')': {
131504 *tokenType = TK_RP;
131505 return 1;
131506 }
131507 case ';': {
131508 *tokenType = TK_SEMI;
131509 return 1;
131510 }
131511 case '+': {
131512 *tokenType = TK_PLUS;
131513 return 1;
131514 }
131515 case '*': {
131516 *tokenType = TK_STAR;
131517 return 1;
131518 }
131519 case '/': {
131520 if( z[1]!='*' || z[2]==0 ){
131521 *tokenType = TK_SLASH;
131522 return 1;
131523 }
131524 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
131525 if( c ) i++;
131526 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
131527 return i;
131528 }
131529 case '%': {
131530 *tokenType = TK_REM;
131531 return 1;
131532 }
131533 case '=': {
131534 *tokenType = TK_EQ;
131535 return 1 + (z[1]=='=');
131536 }
131537 case '<': {
131538 if( (c=z[1])=='=' ){
131539 *tokenType = TK_LE;
131540 return 2;
131541 }else if( c=='>' ){
131542 *tokenType = TK_NE;
@@ -131547,11 +132070,11 @@
131547 }else{
131548 *tokenType = TK_LT;
131549 return 1;
131550 }
131551 }
131552 case '>': {
131553 if( (c=z[1])=='=' ){
131554 *tokenType = TK_GE;
131555 return 2;
131556 }else if( c=='>' ){
131557 *tokenType = TK_RSHIFT;
@@ -131559,43 +132082,41 @@
131559 }else{
131560 *tokenType = TK_GT;
131561 return 1;
131562 }
131563 }
131564 case '!': {
131565 if( z[1]!='=' ){
131566 *tokenType = TK_ILLEGAL;
131567 return 2;
131568 }else{
131569 *tokenType = TK_NE;
131570 return 2;
131571 }
131572 }
131573 case '|': {
131574 if( z[1]!='|' ){
131575 *tokenType = TK_BITOR;
131576 return 1;
131577 }else{
131578 *tokenType = TK_CONCAT;
131579 return 2;
131580 }
131581 }
131582 case ',': {
131583 *tokenType = TK_COMMA;
131584 return 1;
131585 }
131586 case '&': {
131587 *tokenType = TK_BITAND;
131588 return 1;
131589 }
131590 case '~': {
131591 *tokenType = TK_BITNOT;
131592 return 1;
131593 }
131594 case '`':
131595 case '\'':
131596 case '"': {
131597 int delim = z[0];
131598 testcase( delim=='`' );
131599 testcase( delim=='\'' );
131600 testcase( delim=='"' );
131601 for(i=1; (c=z[i])!=0; i++){
@@ -131616,11 +132137,11 @@
131616 }else{
131617 *tokenType = TK_ILLEGAL;
131618 return i;
131619 }
131620 }
131621 case '.': {
131622 #ifndef SQLITE_OMIT_FLOATING_POINT
131623 if( !sqlite3Isdigit(z[1]) )
131624 #endif
131625 {
131626 *tokenType = TK_DOT;
@@ -131627,12 +132148,11 @@
131627 return 1;
131628 }
131629 /* If the next character is a digit, this is a floating point
131630 ** number that begins with ".". Fall thru into the next case */
131631 }
131632 case '0': case '1': case '2': case '3': case '4':
131633 case '5': case '6': case '7': case '8': case '9': {
131634 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
131635 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
131636 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
131637 testcase( z[0]=='9' );
131638 *tokenType = TK_INTEGER;
@@ -131663,26 +132183,22 @@
131663 *tokenType = TK_ILLEGAL;
131664 i++;
131665 }
131666 return i;
131667 }
131668 case '[': {
131669 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
131670 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
131671 return i;
131672 }
131673 case '?': {
131674 *tokenType = TK_VARIABLE;
131675 for(i=1; sqlite3Isdigit(z[i]); i++){}
131676 return i;
131677 }
131678 #ifndef SQLITE_OMIT_TCL_VARIABLE
131679 case '$':
131680 #endif
131681 case '@': /* For compatibility with MS SQL Server */
131682 case '#':
131683 case ':': {
131684 int n = 0;
131685 testcase( z[0]=='$' ); testcase( z[0]=='@' );
131686 testcase( z[0]==':' ); testcase( z[0]=='#' );
131687 *tokenType = TK_VARIABLE;
131688 for(i=1; (c=z[i])!=0; i++){
@@ -131707,12 +132223,24 @@
131707 }
131708 }
131709 if( n==0 ) *tokenType = TK_ILLEGAL;
131710 return i;
131711 }
 
 
 
 
 
 
 
 
 
 
 
 
131712 #ifndef SQLITE_OMIT_BLOB_LITERAL
131713 case 'x': case 'X': {
131714 testcase( z[0]=='x' ); testcase( z[0]=='X' );
131715 if( z[1]=='\'' ){
131716 *tokenType = TK_BLOB;
131717 for(i=2; sqlite3Isxdigit(z[i]); i++){}
131718 if( z[i]!='\'' || i%2 ){
@@ -131720,24 +132248,26 @@
131720 while( z[i] && z[i]!='\'' ){ i++; }
131721 }
131722 if( z[i] ) i++;
131723 return i;
131724 }
131725 /* Otherwise fall through to the next case */
 
131726 }
131727 #endif
 
 
 
 
131728 default: {
131729 if( !IdChar(*z) ){
131730 break;
131731 }
131732 for(i=1; IdChar(z[i]); i++){}
131733 *tokenType = TK_ID;
131734 return keywordCode((char*)z, i, tokenType);
131735 }
131736 }
131737 *tokenType = TK_ILLEGAL;
131738 return 1;
 
131739 }
131740
131741 /*
131742 ** Run the parser on the given SQL string. The parser structure is
131743 ** passed in. An SQLITE_ status code is returned. If an error occurs
@@ -131749,11 +132279,10 @@
131749 int nErr = 0; /* Number of errors encountered */
131750 int i; /* Loop counter */
131751 void *pEngine; /* The LEMON-generated LALR(1) parser */
131752 int tokenType; /* type of the next token */
131753 int lastTokenParsed = -1; /* type of the previous token */
131754 u8 enableLookaside; /* Saved value of db->lookaside.bEnabled */
131755 sqlite3 *db = pParse->db; /* The database connection */
131756 int mxSqlLen; /* Max length of an SQL string */
131757
131758 assert( zSql!=0 );
131759 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
@@ -131765,20 +132294,18 @@
131765 i = 0;
131766 assert( pzErrMsg!=0 );
131767 /* sqlite3ParserTrace(stdout, "parser: "); */
131768 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
131769 if( pEngine==0 ){
131770 db->mallocFailed = 1;
131771 return SQLITE_NOMEM;
131772 }
131773 assert( pParse->pNewTable==0 );
131774 assert( pParse->pNewTrigger==0 );
131775 assert( pParse->nVar==0 );
131776 assert( pParse->nzVar==0 );
131777 assert( pParse->azVar==0 );
131778 enableLookaside = db->lookaside.bEnabled;
131779 if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
131780 while( zSql[i]!=0 ){
131781 assert( i>=0 );
131782 pParse->sLastToken.z = &zSql[i];
131783 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
131784 i += pParse->sLastToken.n;
@@ -131787,11 +132314,10 @@
131787 break;
131788 }
131789 if( tokenType>=TK_SPACE ){
131790 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
131791 if( db->u1.isInterrupted ){
131792 sqlite3ErrorMsg(pParse, "interrupt");
131793 pParse->rc = SQLITE_INTERRUPT;
131794 break;
131795 }
131796 if( tokenType==TK_ILLEGAL ){
131797 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
@@ -131822,11 +132348,10 @@
131822 sqlite3ParserStackPeak(pEngine)
131823 );
131824 sqlite3_mutex_leave(sqlite3MallocMutex());
131825 #endif /* YYDEBUG */
131826 sqlite3ParserFree(pEngine, sqlite3_free);
131827 db->lookaside.bEnabled = enableLookaside;
131828 if( db->mallocFailed ){
131829 pParse->rc = SQLITE_NOMEM;
131830 }
131831 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
131832 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -132963,16 +133488,16 @@
132963 p->pNext = db->lookaside.pFree;
132964 db->lookaside.pFree = p;
132965 p = (LookasideSlot*)&((u8*)p)[sz];
132966 }
132967 db->lookaside.pEnd = p;
132968 db->lookaside.bEnabled = 1;
132969 db->lookaside.bMalloced = pBuf==0 ?1:0;
132970 }else{
132971 db->lookaside.pStart = db;
132972 db->lookaside.pEnd = db;
132973 db->lookaside.bEnabled = 0;
132974 db->lookaside.bMalloced = 0;
132975 }
132976 #endif /* SQLITE_OMIT_LOOKASIDE */
132977 return SQLITE_OK;
132978 }
@@ -134473,11 +134998,11 @@
134473 /* A malloc() may have failed within the call to sqlite3_value_text16()
134474 ** above. If this is the case, then the db->mallocFailed flag needs to
134475 ** be cleared before returning. Do this directly, instead of via
134476 ** sqlite3ApiExit(), to avoid setting the database handle error message.
134477 */
134478 db->mallocFailed = 0;
134479 }
134480 sqlite3_mutex_leave(db->mutex);
134481 return z;
134482 }
134483 #endif /* SQLITE_OMIT_UTF16 */
@@ -135111,11 +135636,11 @@
135111
135112 /* Parse the filename/URI argument. */
135113 db->openFlags = flags;
135114 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
135115 if( rc!=SQLITE_OK ){
135116 if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
135117 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
135118 sqlite3_free(zErrMsg);
135119 goto opendb_out;
135120 }
135121
@@ -135831,11 +136356,11 @@
135831 ** process aborts. If X is false and assert() is disabled, then the
135832 ** return value is zero.
135833 */
135834 case SQLITE_TESTCTRL_ASSERT: {
135835 volatile int x = 0;
135836 assert( (x = va_arg(ap,int))!=0 );
135837 rc = x;
135838 break;
135839 }
135840
135841
@@ -136864,10 +137389,16 @@
136864 #define _FTSINT_H
136865
136866 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
136867 # define NDEBUG 1
136868 #endif
 
 
 
 
 
 
136869
136870 /*
136871 ** FTS4 is really an extension for FTS3. It is enabled using the
136872 ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
136873 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
@@ -146361,10 +146892,11 @@
146361
146362 zName = sqlite3_value_text(argv[0]);
146363 nName = sqlite3_value_bytes(argv[0])+1;
146364
146365 if( argc==2 ){
 
146366 void *pOld;
146367 int n = sqlite3_value_bytes(argv[1]);
146368 if( zName==0 || n!=sizeof(pPtr) ){
146369 sqlite3_result_error(context, "argument type mismatch", -1);
146370 return;
@@ -146373,11 +146905,18 @@
146373 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
146374 if( pOld==pPtr ){
146375 sqlite3_result_error(context, "out of memory", -1);
146376 return;
146377 }
146378 }else{
 
 
 
 
 
 
 
146379 if( zName ){
146380 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
146381 }
146382 if( !pPtr ){
146383 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -146622,10 +147161,11 @@
146622 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
146623 }
146624 Tcl_DecrRefCount(pRet);
146625 }
146626
 
146627 static
146628 int registerTokenizer(
146629 sqlite3 *db,
146630 char *zName,
146631 const sqlite3_tokenizer_module *p
@@ -146643,10 +147183,12 @@
146643 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
146644 sqlite3_step(pStmt);
146645
146646 return sqlite3_finalize(pStmt);
146647 }
 
 
146648
146649 static
146650 int queryTokenizer(
146651 sqlite3 *db,
146652 char *zName,
@@ -146714,15 +147256,17 @@
146714 assert( rc==SQLITE_ERROR );
146715 assert( p2==0 );
146716 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
146717
146718 /* Test the storage function */
 
146719 rc = registerTokenizer(db, "nosuchtokenizer", p1);
146720 assert( rc==SQLITE_OK );
146721 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
146722 assert( rc==SQLITE_OK );
146723 assert( p2==p1 );
 
146724
146725 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
146726 }
146727
146728 #endif
@@ -164601,11 +165145,13 @@
164601 StatTable *pTab = 0;
164602 int rc = SQLITE_OK;
164603 int iDb;
164604
164605 if( argc>=4 ){
164606 iDb = sqlite3FindDbName(db, argv[3]);
 
 
164607 if( iDb<0 ){
164608 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
164609 return SQLITE_ERROR;
164610 }
164611 }else{
@@ -165186,11 +165732,15 @@
165186 /* #include <assert.h> */
165187 /* #include <string.h> */
165188 /* #include <stdlib.h> */
165189 /* #include <stdarg.h> */
165190
165191 #define UNUSED_PARAM(X) (void)(X)
 
 
 
 
165192
165193 #ifndef LARGEST_INT64
165194 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
165195 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
165196 #endif
@@ -165431,14 +165981,37 @@
165431 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
165432 u32 i;
165433 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
165434 p->zBuf[p->nUsed++] = '"';
165435 for(i=0; i<N; i++){
165436 char c = zIn[i];
165437 if( c=='"' || c=='\\' ){
 
165438 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
165439 p->zBuf[p->nUsed++] = '\\';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165440 }
165441 p->zBuf[p->nUsed++] = c;
165442 }
165443 p->zBuf[p->nUsed++] = '"';
165444 assert( p->nUsed<p->nAlloc );
@@ -165475,11 +166048,11 @@
165475 break;
165476 }
165477 default: {
165478 if( p->bErr==0 ){
165479 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
165480 p->bErr = 1;
165481 jsonReset(p);
165482 }
165483 break;
165484 }
165485 }
@@ -166684,10 +167257,11 @@
166684 sqlite3_context *ctx,
166685 int argc,
166686 sqlite3_value **argv
166687 ){
166688 JsonString *pStr;
 
166689 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
166690 if( pStr ){
166691 if( pStr->zBuf==0 ){
166692 jsonInit(pStr, ctx);
166693 jsonAppendChar(pStr, '[');
@@ -166703,11 +167277,11 @@
166703 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
166704 if( pStr ){
166705 pStr->pCtx = ctx;
166706 jsonAppendChar(pStr, ']');
166707 if( pStr->bErr ){
166708 sqlite3_result_error_nomem(ctx);
166709 assert( pStr->bStatic );
166710 }else{
166711 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
166712 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
166713 pStr->bStatic = 1;
@@ -166729,10 +167303,11 @@
166729 sqlite3_value **argv
166730 ){
166731 JsonString *pStr;
166732 const char *z;
166733 u32 n;
 
166734 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
166735 if( pStr ){
166736 if( pStr->zBuf==0 ){
166737 jsonInit(pStr, ctx);
166738 jsonAppendChar(pStr, '{');
@@ -166751,11 +167326,11 @@
166751 JsonString *pStr;
166752 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
166753 if( pStr ){
166754 jsonAppendChar(pStr, '}');
166755 if( pStr->bErr ){
166756 sqlite3_result_error_nomem(ctx);
166757 assert( pStr->bStatic );
166758 }else{
166759 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
166760 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
166761 pStr->bStatic = 1;
@@ -167937,14 +168512,15 @@
167937 #ifndef SQLITE_AMALGAMATION
167938
167939 typedef unsigned char u8;
167940 typedef unsigned int u32;
167941 typedef unsigned short u16;
 
167942 typedef sqlite3_int64 i64;
167943 typedef sqlite3_uint64 u64;
167944
167945 #define ArraySize(x) (sizeof(x) / sizeof(x[0]))
167946
167947 #define testcase(x)
167948 #define ALWAYS(x) 1
167949 #define NEVER(x) 0
167950
@@ -167990,10 +168566,20 @@
167990 SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
167991 # define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
167992 #else
167993 # define assert_nc(x) assert(x)
167994 #endif
 
 
 
 
 
 
 
 
 
 
167995
167996 typedef struct Fts5Global Fts5Global;
167997 typedef struct Fts5Colset Fts5Colset;
167998
167999 /* If a NEAR() clump or phrase may only match a specific set of columns,
@@ -168136,12 +168722,12 @@
168136 ** Buffer object for the incremental building of string data.
168137 */
168138 typedef struct Fts5Buffer Fts5Buffer;
168139 struct Fts5Buffer {
168140 u8 *p;
168141 u32 n;
168142 u32 nSpace;
168143 };
168144
168145 static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
168146 static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
168147 static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
@@ -168158,11 +168744,11 @@
168158 #define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
168159 #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
168160 #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
168161
168162 #define fts5BufferGrow(pRc,pBuf,nn) ( \
168163 (pBuf)->n + (nn) <= (pBuf)->nSpace ? 0 : \
168164 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
168165 )
168166
168167 /* Write and decode big-endian 32-bit integer values */
168168 static void sqlite3Fts5Put32(u8*, int);
@@ -168193,10 +168779,11 @@
168193 typedef struct Fts5PoslistWriter Fts5PoslistWriter;
168194 struct Fts5PoslistWriter {
168195 i64 iPrev;
168196 };
168197 static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
 
168198
168199 static int sqlite3Fts5PoslistNext64(
168200 const u8 *a, int n, /* Buffer containing poslist */
168201 int *pi, /* IN/OUT: Offset within a[] */
168202 i64 *piOff /* IN/OUT: Current offset */
@@ -168226,34 +168813,39 @@
168226 */
168227
168228 typedef struct Fts5Index Fts5Index;
168229 typedef struct Fts5IndexIter Fts5IndexIter;
168230
 
 
 
 
 
 
 
 
 
168231 /*
168232 ** Values used as part of the flags argument passed to IndexQuery().
168233 */
168234 #define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
168235 #define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
168236 #define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
168237 #define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
168238
 
 
 
 
 
 
168239 /*
168240 ** Create/destroy an Fts5Index object.
168241 */
168242 static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
168243 static int sqlite3Fts5IndexClose(Fts5Index *p);
168244
168245 /*
168246 ** for(
168247 ** sqlite3Fts5IndexQuery(p, "token", 5, 0, 0, &pIter);
168248 ** 0==sqlite3Fts5IterEof(pIter);
168249 ** sqlite3Fts5IterNext(pIter)
168250 ** ){
168251 ** i64 iRowid = sqlite3Fts5IterRowid(pIter);
168252 ** }
168253 */
168254
168255 /*
168256 ** Return a simple checksum value based on the arguments.
168257 */
168258 static u64 sqlite3Fts5IndexEntryCksum(
168259 i64 iRowid,
@@ -168289,16 +168881,12 @@
168289
168290 /*
168291 ** The various operations on open token or token prefix iterators opened
168292 ** using sqlite3Fts5IndexQuery().
168293 */
168294 static int sqlite3Fts5IterEof(Fts5IndexIter*);
168295 static int sqlite3Fts5IterNext(Fts5IndexIter*);
168296 static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
168297 static i64 sqlite3Fts5IterRowid(Fts5IndexIter*);
168298 static int sqlite3Fts5IterPoslist(Fts5IndexIter*,Fts5Colset*, const u8**, int*, i64*);
168299 static int sqlite3Fts5IterPoslistBuffer(Fts5IndexIter *pIter, Fts5Buffer *pBuf);
168300
168301 /*
168302 ** Close an iterator opened by sqlite3Fts5IndexQuery().
168303 */
168304 static void sqlite3Fts5IterClose(Fts5IndexIter*);
@@ -168380,12 +168968,10 @@
168380 static int sqlite3Fts5IndexOptimize(Fts5Index *p);
168381 static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
168382
168383 static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
168384
168385 static int sqlite3Fts5IterCollist(Fts5IndexIter*, const u8 **, int*);
168386
168387 /*
168388 ** End of interface to code in fts5_index.c.
168389 **************************************************************************/
168390
168391 /**************************************************************************
@@ -168581,11 +169167,11 @@
168581 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
168582 );
168583 static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
168584 static void sqlite3Fts5ExprClearEof(Fts5Expr*);
168585
168586 static int sqlite3Fts5ExprClonePhrase(Fts5Config*, Fts5Expr*, int, Fts5Expr**);
168587
168588 static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
168589
168590 /*******************************************
168591 ** The fts5_expr.c API above this point is used by the other hand-written
@@ -169372,11 +169958,12 @@
169372 while( fts5yypParser->fts5yyidx>=0 ) fts5yy_pop_parser_stack(fts5yypParser);
169373 /* Here code is inserted which will execute if the parser
169374 ** stack every overflows */
169375 /******** Begin %stack_overflow code ******************************************/
169376
169377 assert( 0 );
 
169378 /******** End %stack_overflow code ********************************************/
169379 sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
169380 }
169381
169382 /*
@@ -169669,10 +170256,11 @@
169669 ){
169670 sqlite3Fts5ParserARG_FETCH;
169671 #define FTS5TOKEN (fts5yyminor.fts5yy0)
169672 /************ Begin %syntax_error code ****************************************/
169673
 
169674 sqlite3Fts5ParseError(
169675 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
169676 );
169677 /************ End %syntax_error code ******************************************/
169678 sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
@@ -170045,10 +170633,12 @@
170045 int iEndOff /* End offset of token */
170046 ){
170047 HighlightContext *p = (HighlightContext*)pContext;
170048 int rc = SQLITE_OK;
170049 int iPos;
 
 
170050
170051 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
170052 iPos = p->iPos++;
170053
170054 if( p->iRangeEnd>0 ){
@@ -170279,10 +170869,11 @@
170279 const Fts5ExtensionApi *pApi,
170280 Fts5Context *pFts,
170281 void *pUserData /* Pointer to sqlite3_int64 variable */
170282 ){
170283 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
 
170284 (*pn)++;
170285 return SQLITE_OK;
170286 }
170287
170288 /*
@@ -170432,11 +171023,11 @@
170432 { "bm25", 0, fts5Bm25Function, 0 },
170433 };
170434 int rc = SQLITE_OK; /* Return code */
170435 int i; /* To iterate through builtin functions */
170436
170437 for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aBuiltin); i++){
170438 rc = pApi->xCreateFunction(pApi,
170439 aBuiltin[i].zFunc,
170440 aBuiltin[i].pUserData,
170441 aBuiltin[i].xFunc,
170442 aBuiltin[i].xDestroy
@@ -170464,22 +171055,24 @@
170464
170465
170466 /* #include "fts5Int.h" */
170467
170468 static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
170469 u32 nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64;
170470 u8 *pNew;
170471 while( nNew<nByte ){
170472 nNew = nNew * 2;
170473 }
170474 pNew = sqlite3_realloc(pBuf->p, nNew);
170475 if( pNew==0 ){
170476 *pRc = SQLITE_NOMEM;
170477 return 1;
170478 }else{
170479 pBuf->nSpace = nNew;
170480 pBuf->p = pNew;
 
 
170481 }
170482 return 0;
170483 }
170484
170485
@@ -170655,28 +171248,41 @@
170655 pIter->a = a;
170656 pIter->n = n;
170657 sqlite3Fts5PoslistReaderNext(pIter);
170658 return pIter->bEof;
170659 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170660
170661 static int sqlite3Fts5PoslistWriterAppend(
170662 Fts5Buffer *pBuf,
170663 Fts5PoslistWriter *pWriter,
170664 i64 iPos
170665 ){
170666 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
170667 int rc = SQLITE_OK;
170668 if( 0==fts5BufferGrow(&rc, pBuf, 5+5+5) ){
170669 if( (iPos & colmask) != (pWriter->iPrev & colmask) ){
170670 pBuf->p[pBuf->n++] = 1;
170671 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
170672 pWriter->iPrev = (iPos & colmask);
170673 }
170674 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-pWriter->iPrev)+2);
170675 pWriter->iPrev = iPos;
170676 }
170677 return rc;
170678 }
170679
170680 static void *sqlite3Fts5MallocZero(int *pRc, int nByte){
170681 void *pRet = 0;
170682 if( *pRc==SQLITE_OK ){
@@ -170770,11 +171376,11 @@
170770 ){
170771 int rc = SQLITE_OK;
170772 *pbPresent = 0;
170773 if( p ){
170774 int i;
170775 int hash = 13;
170776 Fts5TermsetEntry *pEntry;
170777
170778 /* Calculate a hash value for this term. This is the same hash checksum
170779 ** used by the fts5_hash.c module. This is not important for correct
170780 ** operation of the module, but is necessary to ensure that some tests
@@ -170787,11 +171393,11 @@
170787
170788 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
170789 if( pEntry->iIdx==iIdx
170790 && pEntry->nTerm==nTerm
170791 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
170792 ){
170793 *pbPresent = 1;
170794 break;
170795 }
170796 }
170797
@@ -170811,11 +171417,11 @@
170811 return rc;
170812 }
170813
170814 static void sqlite3Fts5TermsetFree(Fts5Termset *p){
170815 if( p ){
170816 int i;
170817 for(i=0; i<ArraySize(p->apHash); i++){
170818 Fts5TermsetEntry *pEntry = p->apHash[i];
170819 while( pEntry ){
170820 Fts5TermsetEntry *pDel = pEntry;
170821 pEntry = pEntry->pNext;
@@ -170823,13 +171429,10 @@
170823 }
170824 }
170825 sqlite3_free(p);
170826 }
170827 }
170828
170829
170830
170831
170832 /*
170833 ** 2014 Jun 09
170834 **
170835 ** The author disclaims copyright to this source code. In place of
@@ -171035,11 +171638,11 @@
171035 static int fts5ConfigSetEnum(
171036 const Fts5Enum *aEnum,
171037 const char *zEnum,
171038 int *peVal
171039 ){
171040 int nEnum = strlen(zEnum);
171041 int i;
171042 int iVal = -1;
171043
171044 for(i=0; aEnum[i].zName; i++){
171045 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
@@ -171773,11 +172376,10 @@
171773 pConfig->iCookie = iCookie;
171774 }
171775 return rc;
171776 }
171777
171778
171779 /*
171780 ** 2014 May 31
171781 **
171782 ** The author disclaims copyright to this source code. In place of
171783 ** a legal notice, here is a blessing:
@@ -171838,10 +172440,13 @@
171838 struct Fts5ExprNode {
171839 int eType; /* Node type */
171840 int bEof; /* True at EOF */
171841 int bNomatch; /* True if entry is not a match */
171842
 
 
 
171843 i64 iRowid; /* Current rowid */
171844 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
171845
171846 /* Child nodes. For a NOT node, this array always contains 2 entries. For
171847 ** AND or OR nodes, it contains 2 or more entries. */
@@ -171849,10 +172454,16 @@
171849 Fts5ExprNode *apChild[1]; /* Array of child nodes */
171850 };
171851
171852 #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
171853
 
 
 
 
 
 
171854 /*
171855 ** An instance of the following structure represents a single search term
171856 ** or term prefix.
171857 */
171858 struct Fts5ExprTerm {
@@ -172010,11 +172621,19 @@
172010 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
172011 if( pNew==0 ){
172012 sParse.rc = SQLITE_NOMEM;
172013 sqlite3Fts5ParseNodeFree(sParse.pExpr);
172014 }else{
172015 pNew->pRoot = sParse.pExpr;
 
 
 
 
 
 
 
 
172016 pNew->pIndex = 0;
172017 pNew->pConfig = pConfig;
172018 pNew->apExprPhrase = sParse.apPhrase;
172019 pNew->nPhrase = sParse.nPhrase;
172020 sParse.apPhrase = 0;
@@ -172062,11 +172681,11 @@
172062
172063 assert( pTerm->pSynonym );
172064 assert( bDesc==0 || bDesc==1 );
172065 for(p=pTerm; p; p=p->pSynonym){
172066 if( 0==sqlite3Fts5IterEof(p->pIter) ){
172067 i64 iRowid = sqlite3Fts5IterRowid(p->pIter);
172068 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
172069 iRet = iRowid;
172070 bRetValid = 1;
172071 }
172072 }
@@ -172079,14 +172698,12 @@
172079 /*
172080 ** Argument pTerm must be a synonym iterator.
172081 */
172082 static int fts5ExprSynonymList(
172083 Fts5ExprTerm *pTerm,
172084 int bCollist,
172085 Fts5Colset *pColset,
172086 i64 iRowid,
172087 int *pbDel, /* OUT: Caller should sqlite3_free(*pa) */
172088 u8 **pa, int *pn
172089 ){
172090 Fts5PoslistReader aStatic[4];
172091 Fts5PoslistReader *aIter = aStatic;
172092 int nIter = 0;
@@ -172095,23 +172712,12 @@
172095 Fts5ExprTerm *p;
172096
172097 assert( pTerm->pSynonym );
172098 for(p=pTerm; p; p=p->pSynonym){
172099 Fts5IndexIter *pIter = p->pIter;
172100 if( sqlite3Fts5IterEof(pIter)==0 && sqlite3Fts5IterRowid(pIter)==iRowid ){
172101 const u8 *a;
172102 int n;
172103
172104 if( bCollist ){
172105 rc = sqlite3Fts5IterCollist(pIter, &a, &n);
172106 }else{
172107 i64 dummy;
172108 rc = sqlite3Fts5IterPoslist(pIter, pColset, &a, &n, &dummy);
172109 }
172110
172111 if( rc!=SQLITE_OK ) goto synonym_poslist_out;
172112 if( n==0 ) continue;
172113 if( nIter==nAlloc ){
172114 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
172115 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172116 if( aNew==0 ){
172117 rc = SQLITE_NOMEM;
@@ -172120,24 +172726,23 @@
172120 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
172121 nAlloc = nAlloc*2;
172122 if( aIter!=aStatic ) sqlite3_free(aIter);
172123 aIter = aNew;
172124 }
172125 sqlite3Fts5PoslistReaderInit(a, n, &aIter[nIter]);
172126 assert( aIter[nIter].bEof==0 );
172127 nIter++;
172128 }
172129 }
172130
172131 assert( *pbDel==0 );
172132 if( nIter==1 ){
172133 *pa = (u8*)aIter[0].a;
172134 *pn = aIter[0].n;
172135 }else{
172136 Fts5PoslistWriter writer = {0};
172137 Fts5Buffer buf = {0,0,0};
172138 i64 iPrev = -1;
 
172139 while( 1 ){
172140 int i;
172141 i64 iMin = FTS5_LARGEST_INT64;
172142 for(i=0; i<nIter; i++){
172143 if( aIter[i].bEof==0 ){
@@ -172148,19 +172753,16 @@
172148 iMin = aIter[i].iPos;
172149 }
172150 }
172151 }
172152 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
172153 rc = sqlite3Fts5PoslistWriterAppend(&buf, &writer, iMin);
172154 iPrev = iMin;
172155 }
172156 if( rc ){
172157 sqlite3_free(buf.p);
172158 }else{
172159 *pa = buf.p;
172160 *pn = buf.n;
172161 *pbDel = 1;
172162 }
172163 }
172164
172165 synonym_poslist_out:
172166 if( aIter!=aStatic ) sqlite3_free(aIter);
@@ -172179,11 +172781,10 @@
172179 ** otherwise. It is not considered an error code if the current rowid is
172180 ** not a match.
172181 */
172182 static int fts5ExprPhraseIsMatch(
172183 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
172184 Fts5Colset *pColset, /* Restrict matches to these columns */
172185 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
172186 int *pbMatch /* OUT: Set to true if really a match */
172187 ){
172188 Fts5PoslistWriter writer = {0};
172189 Fts5PoslistReader aStatic[4];
@@ -172193,32 +172794,35 @@
172193
172194 fts5BufferZero(&pPhrase->poslist);
172195
172196 /* If the aStatic[] array is not large enough, allocate a large array
172197 ** using sqlite3_malloc(). This approach could be improved upon. */
172198 if( pPhrase->nTerm>(int)ArraySize(aStatic) ){
172199 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
172200 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172201 if( !aIter ) return SQLITE_NOMEM;
172202 }
172203 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
172204
172205 /* Initialize a term iterator for each term in the phrase */
172206 for(i=0; i<pPhrase->nTerm; i++){
172207 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
172208 i64 dummy;
172209 int n = 0;
172210 int bFlag = 0;
172211 const u8 *a = 0;
172212 if( pTerm->pSynonym ){
172213 rc = fts5ExprSynonymList(
172214 pTerm, 0, pColset, pNode->iRowid, &bFlag, (u8**)&a, &n
172215 );
 
 
 
 
172216 }else{
172217 rc = sqlite3Fts5IterPoslist(pTerm->pIter, pColset, &a, &n, &dummy);
 
172218 }
172219 if( rc!=SQLITE_OK ) goto ismatch_out;
172220 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
172221 aIter[i].bFlag = (u8)bFlag;
172222 if( aIter[i].bEof ) goto ismatch_out;
172223 }
172224
@@ -172286,16 +172890,10 @@
172286 p->n = n;
172287 fts5LookaheadReaderNext(p);
172288 return fts5LookaheadReaderNext(p);
172289 }
172290
172291 #if 0
172292 static int fts5LookaheadReaderEof(Fts5LookaheadReader *p){
172293 return (p->iPos==FTS5_LOOKAHEAD_EOF);
172294 }
172295 #endif
172296
172297 typedef struct Fts5NearTrimmer Fts5NearTrimmer;
172298 struct Fts5NearTrimmer {
172299 Fts5LookaheadReader reader; /* Input iterator */
172300 Fts5PoslistWriter writer; /* Writer context */
172301 Fts5Buffer *pOut; /* Output poslist */
@@ -172329,11 +172927,11 @@
172329
172330 assert( pNear->nPhrase>1 );
172331
172332 /* If the aStatic[] array is not large enough, allocate a large array
172333 ** using sqlite3_malloc(). This approach could be improved upon. */
172334 if( pNear->nPhrase>(int)ArraySize(aStatic) ){
172335 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
172336 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
172337 }else{
172338 memset(aStatic, 0, sizeof(aStatic));
172339 }
@@ -172406,75 +173004,10 @@
172406 if( a!=aStatic ) sqlite3_free(a);
172407 return bRet;
172408 }
172409 }
172410
172411 /*
172412 ** Advance the first term iterator in the first phrase of pNear. Set output
172413 ** variable *pbEof to true if it reaches EOF or if an error occurs.
172414 **
172415 ** Return SQLITE_OK if successful, or an SQLite error code if an error
172416 ** occurs.
172417 */
172418 static int fts5ExprNearAdvanceFirst(
172419 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172420 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
172421 int bFromValid,
172422 i64 iFrom
172423 ){
172424 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
172425 int rc = SQLITE_OK;
172426
172427 if( pTerm->pSynonym ){
172428 int bEof = 1;
172429 Fts5ExprTerm *p;
172430
172431 /* Find the firstest rowid any synonym points to. */
172432 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
172433
172434 /* Advance each iterator that currently points to iRowid. Or, if iFrom
172435 ** is valid - each iterator that points to a rowid before iFrom. */
172436 for(p=pTerm; p; p=p->pSynonym){
172437 if( sqlite3Fts5IterEof(p->pIter)==0 ){
172438 i64 ii = sqlite3Fts5IterRowid(p->pIter);
172439 if( ii==iRowid
172440 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
172441 ){
172442 if( bFromValid ){
172443 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
172444 }else{
172445 rc = sqlite3Fts5IterNext(p->pIter);
172446 }
172447 if( rc!=SQLITE_OK ) break;
172448 if( sqlite3Fts5IterEof(p->pIter)==0 ){
172449 bEof = 0;
172450 }
172451 }else{
172452 bEof = 0;
172453 }
172454 }
172455 }
172456
172457 /* Set the EOF flag if either all synonym iterators are at EOF or an
172458 ** error has occurred. */
172459 pNode->bEof = (rc || bEof);
172460 }else{
172461 Fts5IndexIter *pIter = pTerm->pIter;
172462
172463 assert( Fts5NodeIsString(pNode) );
172464 if( bFromValid ){
172465 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
172466 }else{
172467 rc = sqlite3Fts5IterNext(pIter);
172468 }
172469
172470 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
172471 }
172472
172473 return rc;
172474 }
172475
172476 /*
172477 ** Advance iterator pIter until it points to a value equal to or laster
172478 ** than the initial value of *piLast. If this means the iterator points
172479 ** to a value laster than *piLast, update *piLast to the new lastest value.
172480 **
@@ -172490,19 +173023,19 @@
172490 int *pbEof /* OUT: Set to true if EOF */
172491 ){
172492 i64 iLast = *piLast;
172493 i64 iRowid;
172494
172495 iRowid = sqlite3Fts5IterRowid(pIter);
172496 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
172497 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
172498 if( rc || sqlite3Fts5IterEof(pIter) ){
172499 *pRc = rc;
172500 *pbEof = 1;
172501 return 1;
172502 }
172503 iRowid = sqlite3Fts5IterRowid(pIter);
172504 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
172505 }
172506 *piLast = iRowid;
172507
172508 return 0;
@@ -172519,11 +173052,11 @@
172519 Fts5ExprTerm *p;
172520 int bEof = 0;
172521
172522 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
172523 if( sqlite3Fts5IterEof(p->pIter)==0 ){
172524 i64 iRowid = sqlite3Fts5IterRowid(p->pIter);
172525 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
172526 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
172527 }
172528 }
172529 }
@@ -172551,17 +173084,11 @@
172551 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
172552 pPhrase->poslist.n = 0;
172553 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
172554 Fts5IndexIter *pIter = pTerm->pIter;
172555 if( sqlite3Fts5IterEof(pIter)==0 ){
172556 int n;
172557 i64 iRowid;
172558 rc = sqlite3Fts5IterPoslist(pIter, pNear->pColset, 0, &n, &iRowid);
172559 if( rc!=SQLITE_OK ){
172560 *pRc = rc;
172561 return 0;
172562 }else if( iRowid==pNode->iRowid && n>0 ){
172563 pPhrase->poslist.n = 1;
172564 }
172565 }
172566 }
172567 return pPhrase->poslist.n;
@@ -172573,16 +173100,15 @@
172573 ** phrase is not a match, break out of the loop early. */
172574 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
172575 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172576 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){
172577 int bMatch = 0;
172578 rc = fts5ExprPhraseIsMatch(pNode, pNear->pColset, pPhrase, &bMatch);
172579 if( bMatch==0 ) break;
172580 }else{
172581 rc = sqlite3Fts5IterPoslistBuffer(
172582 pPhrase->aTerm[0].pIter, &pPhrase->poslist
172583 );
172584 }
172585 }
172586
172587 *pRc = rc;
172588 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
@@ -172590,107 +173116,10 @@
172590 }
172591 return 0;
172592 }
172593 }
172594
172595 static int fts5ExprTokenTest(
172596 Fts5Expr *pExpr, /* Expression that pNear is a part of */
172597 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
172598 ){
172599 /* As this "NEAR" object is actually a single phrase that consists
172600 ** of a single term only, grab pointers into the poslist managed by the
172601 ** fts5_index.c iterator object. This is much faster than synthesizing
172602 ** a new poslist the way we have to for more complicated phrase or NEAR
172603 ** expressions. */
172604 Fts5ExprNearset *pNear = pNode->pNear;
172605 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
172606 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
172607 Fts5Colset *pColset = pNear->pColset;
172608 int rc;
172609
172610 assert( pNode->eType==FTS5_TERM );
172611 assert( pNear->nPhrase==1 && pPhrase->nTerm==1 );
172612 assert( pPhrase->aTerm[0].pSynonym==0 );
172613
172614 rc = sqlite3Fts5IterPoslist(pIter, pColset,
172615 (const u8**)&pPhrase->poslist.p, (int*)&pPhrase->poslist.n, &pNode->iRowid
172616 );
172617 pNode->bNomatch = (pPhrase->poslist.n==0);
172618 return rc;
172619 }
172620
172621 /*
172622 ** All individual term iterators in pNear are guaranteed to be valid when
172623 ** this function is called. This function checks if all term iterators
172624 ** point to the same rowid, and if not, advances them until they do.
172625 ** If an EOF is reached before this happens, *pbEof is set to true before
172626 ** returning.
172627 **
172628 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
172629 ** otherwise. It is not considered an error code if an iterator reaches
172630 ** EOF.
172631 */
172632 static int fts5ExprNearNextMatch(
172633 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172634 Fts5ExprNode *pNode
172635 ){
172636 Fts5ExprNearset *pNear = pNode->pNear;
172637 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
172638 int rc = SQLITE_OK;
172639 i64 iLast; /* Lastest rowid any iterator points to */
172640 int i, j; /* Phrase and token index, respectively */
172641 int bMatch; /* True if all terms are at the same rowid */
172642 const int bDesc = pExpr->bDesc;
172643
172644 /* Check that this node should not be FTS5_TERM */
172645 assert( pNear->nPhrase>1
172646 || pNear->apPhrase[0]->nTerm>1
172647 || pNear->apPhrase[0]->aTerm[0].pSynonym
172648 );
172649
172650 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
172651 ** iterator skips through rowids in the default ascending order, this means
172652 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
172653 ** means the minimum rowid. */
172654 if( pLeft->aTerm[0].pSynonym ){
172655 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
172656 }else{
172657 iLast = sqlite3Fts5IterRowid(pLeft->aTerm[0].pIter);
172658 }
172659
172660 do {
172661 bMatch = 1;
172662 for(i=0; i<pNear->nPhrase; i++){
172663 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172664 for(j=0; j<pPhrase->nTerm; j++){
172665 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
172666 if( pTerm->pSynonym ){
172667 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
172668 if( iRowid==iLast ) continue;
172669 bMatch = 0;
172670 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
172671 pNode->bEof = 1;
172672 return rc;
172673 }
172674 }else{
172675 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
172676 i64 iRowid = sqlite3Fts5IterRowid(pIter);
172677 if( iRowid==iLast ) continue;
172678 bMatch = 0;
172679 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
172680 return rc;
172681 }
172682 }
172683 }
172684 }
172685 }while( bMatch==0 );
172686
172687 pNode->iRowid = iLast;
172688 pNode->bNomatch = (0==fts5ExprNearTest(&rc, pExpr, pNode));
172689
172690 return rc;
172691 }
172692
172693 /*
172694 ** Initialize all term iterators in the pNear object. If any term is found
172695 ** to match no documents at all, return immediately without initializing any
172696 ** further iterators.
@@ -172701,10 +173130,11 @@
172701 ){
172702 Fts5ExprNearset *pNear = pNode->pNear;
172703 int i, j;
172704 int rc = SQLITE_OK;
172705
 
172706 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
172707 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
172708 for(j=0; j<pPhrase->nTerm; j++){
172709 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
172710 Fts5ExprTerm *p;
@@ -172735,14 +173165,10 @@
172735 }
172736 }
172737
172738 return rc;
172739 }
172740
172741 /* fts5ExprNodeNext() calls fts5ExprNodeNextMatch(). And vice-versa. */
172742 static int fts5ExprNodeNextMatch(Fts5Expr*, Fts5ExprNode*);
172743
172744
172745 /*
172746 ** If pExpr is an ASC iterator, this function returns a value with the
172747 ** same sign as:
172748 **
@@ -172768,10 +173194,11 @@
172768 }
172769
172770 static void fts5ExprSetEof(Fts5ExprNode *pNode){
172771 int i;
172772 pNode->bEof = 1;
 
172773 for(i=0; i<pNode->nChild; i++){
172774 fts5ExprSetEof(pNode->apChild[i]);
172775 }
172776 }
172777
@@ -172790,16 +173217,279 @@
172790 }
172791 }
172792 }
172793
172794
172795 static int fts5ExprNodeNext(Fts5Expr*, Fts5ExprNode*, int, i64);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172796
172797 /*
172798 ** Argument pNode is an FTS5_AND node.
172799 */
172800 static int fts5ExprAndNextRowid(
172801 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
172802 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
172803 ){
172804 int iChild;
172805 i64 iLast = pAnd->iRowid;
@@ -172810,19 +173500,15 @@
172810 do {
172811 pAnd->bNomatch = 0;
172812 bMatch = 1;
172813 for(iChild=0; iChild<pAnd->nChild; iChild++){
172814 Fts5ExprNode *pChild = pAnd->apChild[iChild];
172815 if( 0 && pChild->eType==FTS5_STRING ){
172816 /* TODO */
172817 }else{
172818 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
172819 if( cmp>0 ){
172820 /* Advance pChild until it points to iLast or laster */
172821 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
172822 if( rc!=SQLITE_OK ) return rc;
172823 }
172824 }
172825
172826 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
172827 ** the child node is guaranteed to have advanced at least as far as
172828 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
@@ -172848,190 +173534,99 @@
172848 }
172849 pAnd->iRowid = iLast;
172850 return SQLITE_OK;
172851 }
172852
172853
172854 /*
172855 ** Compare the values currently indicated by the two nodes as follows:
172856 **
172857 ** res = (*p1) - (*p2)
172858 **
172859 ** Nodes that point to values that come later in the iteration order are
172860 ** considered to be larger. Nodes at EOF are the largest of all.
172861 **
172862 ** This means that if the iteration order is ASC, then numerically larger
172863 ** rowids are considered larger. Or if it is the default DESC, numerically
172864 ** smaller rowids are larger.
172865 */
172866 static int fts5NodeCompare(
172867 Fts5Expr *pExpr,
172868 Fts5ExprNode *p1,
172869 Fts5ExprNode *p2
172870 ){
172871 if( p2->bEof ) return -1;
172872 if( p1->bEof ) return +1;
172873 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
172874 }
172875
172876 /*
172877 ** Advance node iterator pNode, part of expression pExpr. If argument
172878 ** bFromValid is zero, then pNode is advanced exactly once. Or, if argument
172879 ** bFromValid is non-zero, then pNode is advanced until it is at or past
172880 ** rowid value iFrom. Whether "past" means "less than" or "greater than"
172881 ** depends on whether this is an ASC or DESC iterator.
172882 */
172883 static int fts5ExprNodeNext(
 
 
 
 
 
 
 
 
 
 
 
172884 Fts5Expr *pExpr,
172885 Fts5ExprNode *pNode,
172886 int bFromValid,
172887 i64 iFrom
172888 ){
172889 int rc = SQLITE_OK;
172890
172891 if( pNode->bEof==0 ){
172892 switch( pNode->eType ){
172893 case FTS5_STRING: {
172894 rc = fts5ExprNearAdvanceFirst(pExpr, pNode, bFromValid, iFrom);
172895 break;
172896 };
172897
172898 case FTS5_TERM: {
172899 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
172900 if( bFromValid ){
172901 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
172902 }else{
172903 rc = sqlite3Fts5IterNext(pIter);
172904 }
172905 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
172906 assert( rc==SQLITE_OK );
172907 rc = fts5ExprTokenTest(pExpr, pNode);
172908 }else{
172909 pNode->bEof = 1;
172910 }
172911 return rc;
172912 };
172913
172914 case FTS5_AND: {
172915 Fts5ExprNode *pLeft = pNode->apChild[0];
172916 rc = fts5ExprNodeNext(pExpr, pLeft, bFromValid, iFrom);
172917 break;
172918 }
172919
172920 case FTS5_OR: {
172921 int i;
172922 i64 iLast = pNode->iRowid;
172923
172924 for(i=0; rc==SQLITE_OK && i<pNode->nChild; i++){
172925 Fts5ExprNode *p1 = pNode->apChild[i];
172926 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
172927 if( p1->bEof==0 ){
172928 if( (p1->iRowid==iLast)
172929 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
172930 ){
172931 rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
172932 }
172933 }
172934 }
172935
172936 break;
172937 }
172938
172939 default: assert( pNode->eType==FTS5_NOT ); {
172940 assert( pNode->nChild==2 );
172941 rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
172942 break;
172943 }
172944 }
172945
172946 if( rc==SQLITE_OK ){
172947 rc = fts5ExprNodeNextMatch(pExpr, pNode);
172948 }
172949 }
172950
172951 /* Assert that if bFromValid was true, either:
172952 **
172953 ** a) an error occurred, or
172954 ** b) the node is now at EOF, or
172955 ** c) the node is now at or past rowid iFrom.
172956 */
172957 assert( bFromValid==0
172958 || rc!=SQLITE_OK /* a */
172959 || pNode->bEof /* b */
172960 || pNode->iRowid==iFrom || pExpr->bDesc==(pNode->iRowid<iFrom) /* c */
172961 );
172962
172963 return rc;
172964 }
172965
172966
172967 /*
172968 ** If pNode currently points to a match, this function returns SQLITE_OK
172969 ** without modifying it. Otherwise, pNode is advanced until it does point
172970 ** to a match or EOF is reached.
172971 */
172972 static int fts5ExprNodeNextMatch(
172973 Fts5Expr *pExpr, /* Expression of which pNode is a part */
172974 Fts5ExprNode *pNode /* Expression node to test */
172975 ){
172976 int rc = SQLITE_OK;
172977 if( pNode->bEof==0 ){
172978 switch( pNode->eType ){
172979
172980 case FTS5_STRING: {
172981 /* Advance the iterators until they all point to the same rowid */
172982 rc = fts5ExprNearNextMatch(pExpr, pNode);
172983 break;
172984 }
172985
172986 case FTS5_TERM: {
172987 rc = fts5ExprTokenTest(pExpr, pNode);
172988 break;
172989 }
172990
172991 case FTS5_AND: {
172992 rc = fts5ExprAndNextRowid(pExpr, pNode);
172993 break;
172994 }
172995
172996 case FTS5_OR: {
172997 Fts5ExprNode *pNext = pNode->apChild[0];
172998 int i;
172999
173000 for(i=1; i<pNode->nChild; i++){
173001 Fts5ExprNode *pChild = pNode->apChild[i];
173002 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
173003 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
173004 pNext = pChild;
173005 }
173006 }
173007 pNode->iRowid = pNext->iRowid;
173008 pNode->bEof = pNext->bEof;
173009 pNode->bNomatch = pNext->bNomatch;
173010 break;
173011 }
173012
173013 default: assert( pNode->eType==FTS5_NOT ); {
173014 Fts5ExprNode *p1 = pNode->apChild[0];
173015 Fts5ExprNode *p2 = pNode->apChild[1];
173016 assert( pNode->nChild==2 );
173017
173018 while( rc==SQLITE_OK && p1->bEof==0 ){
173019 int cmp = fts5NodeCompare(pExpr, p1, p2);
173020 if( cmp>0 ){
173021 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
173022 cmp = fts5NodeCompare(pExpr, p1, p2);
173023 }
173024 assert( rc!=SQLITE_OK || cmp<=0 );
173025 if( cmp || p2->bNomatch ) break;
173026 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
173027 }
173028 pNode->bEof = p1->bEof;
173029 pNode->iRowid = p1->iRowid;
173030 if( p1->bEof ){
173031 fts5ExprNodeZeroPoslist(p2);
173032 }
173033 break;
173034 }
173035 }
173036 }
173037 return rc;
@@ -173046,24 +173641,44 @@
173046 ** It is not an error if there are no matches.
173047 */
173048 static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
173049 int rc = SQLITE_OK;
173050 pNode->bEof = 0;
 
173051
173052 if( Fts5NodeIsString(pNode) ){
173053 /* Initialize all term iterators in the NEAR object. */
173054 rc = fts5ExprNearInitAll(pExpr, pNode);
173055 }else{
173056 int i;
 
173057 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
 
173058 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
 
 
173059 }
173060 pNode->iRowid = pNode->apChild[0]->iRowid;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173061 }
173062
173063 if( rc==SQLITE_OK ){
173064 rc = fts5ExprNodeNextMatch(pExpr, pNode);
173065 }
173066 return rc;
173067 }
173068
173069
@@ -173083,11 +173698,11 @@
173083 ** is not considered an error if the query does not match any documents.
173084 */
173085 static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
173086 Fts5ExprNode *pRoot = p->pRoot;
173087 int rc = SQLITE_OK;
173088 if( pRoot ){
173089 p->pIndex = pIdx;
173090 p->bDesc = bDesc;
173091 rc = fts5ExprNodeFirst(p, pRoot);
173092
173093 /* If not at EOF but the current rowid occurs earlier than iFirst in
@@ -173095,11 +173710,12 @@
173095 if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){
173096 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
173097 }
173098
173099 /* If the iterator is not at a real match, skip forward until it is. */
173100 while( pRoot->bNomatch && rc==SQLITE_OK && pRoot->bEof==0 ){
 
173101 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173102 }
173103 }
173104 return rc;
173105 }
@@ -173111,21 +173727,23 @@
173111 ** is not considered an error if the query does not match any documents.
173112 */
173113 static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
173114 int rc;
173115 Fts5ExprNode *pRoot = p->pRoot;
 
173116 do {
173117 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173118 }while( pRoot->bNomatch && pRoot->bEof==0 && rc==SQLITE_OK );
 
173119 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
173120 pRoot->bEof = 1;
173121 }
173122 return rc;
173123 }
173124
173125 static int sqlite3Fts5ExprEof(Fts5Expr *p){
173126 return (p->pRoot==0 || p->pRoot->bEof);
173127 }
173128
173129 static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
173130 return p->pRoot->iRowid;
173131 }
@@ -173146,14 +173764,14 @@
173146 Fts5ExprTerm *pSyn;
173147 Fts5ExprTerm *pNext;
173148 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
173149 sqlite3_free(pTerm->zTerm);
173150 sqlite3Fts5IterClose(pTerm->pIter);
173151
173152 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
173153 pNext = pSyn->pSynonym;
173154 sqlite3Fts5IterClose(pSyn->pIter);
 
173155 sqlite3_free(pSyn);
173156 }
173157 }
173158 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
173159 sqlite3_free(pPhrase);
@@ -173230,24 +173848,26 @@
173230 ){
173231 int rc = SQLITE_OK;
173232 const int SZALLOC = 8;
173233 TokenCtx *pCtx = (TokenCtx*)pContext;
173234 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
 
 
173235
173236 /* If an error has already occurred, this is a no-op */
173237 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
173238
173239 assert( pPhrase==0 || pPhrase->nTerm>0 );
173240 if( pPhrase && (tflags & FTS5_TOKEN_COLOCATED) ){
173241 Fts5ExprTerm *pSyn;
173242 int nByte = sizeof(Fts5ExprTerm) + nToken+1;
173243 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
173244 if( pSyn==0 ){
173245 rc = SQLITE_NOMEM;
173246 }else{
173247 memset(pSyn, 0, nByte);
173248 pSyn->zTerm = (char*)&pSyn[1];
173249 memcpy(pSyn->zTerm, pToken, nToken);
173250 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
173251 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
173252 }
173253 }else{
@@ -173366,26 +173986,21 @@
173366 /*
173367 ** Create a new FTS5 expression by cloning phrase iPhrase of the
173368 ** expression passed as the second argument.
173369 */
173370 static int sqlite3Fts5ExprClonePhrase(
173371 Fts5Config *pConfig,
173372 Fts5Expr *pExpr,
173373 int iPhrase,
173374 Fts5Expr **ppNew
173375 ){
173376 int rc = SQLITE_OK; /* Return code */
173377 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
173378 int i; /* Used to iterate through phrase terms */
173379
173380 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
173381
173382 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
173383
173384
173385 pOrig = pExpr->apExprPhrase[iPhrase];
173386
173387 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
173388 if( rc==SQLITE_OK ){
173389 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
173390 sizeof(Fts5ExprPhrase*));
173391 }
@@ -173422,12 +174037,14 @@
173422 pNew->pRoot->pNear->nPhrase = 1;
173423 sCtx.pPhrase->pNode = pNew->pRoot;
173424
173425 if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){
173426 pNew->pRoot->eType = FTS5_TERM;
 
173427 }else{
173428 pNew->pRoot->eType = FTS5_STRING;
 
173429 }
173430 }else{
173431 sqlite3Fts5ExprFree(pNew);
173432 fts5ExprPhraseFree(sCtx.pPhrase);
173433 pNew = 0;
@@ -173570,10 +174187,42 @@
173570 pNear->pColset = pColset;
173571 }else{
173572 sqlite3_free(pColset);
173573 }
173574 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173575
173576 static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
173577 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
173578 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
173579 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
@@ -173620,20 +174269,20 @@
173620 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
173621
173622 if( pRet ){
173623 pRet->eType = eType;
173624 pRet->pNear = pNear;
 
173625 if( eType==FTS5_STRING ){
173626 int iPhrase;
173627 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
173628 pNear->apPhrase[iPhrase]->pNode = pRet;
173629 }
173630 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1 ){
173631 if( pNear->apPhrase[0]->aTerm[0].pSynonym==0 ){
173632 pRet->eType = FTS5_TERM;
173633 }
173634 }else if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL ){
173635 assert( pParse->rc==SQLITE_OK );
173636 pParse->rc = SQLITE_ERROR;
173637 assert( pParse->zErr==0 );
173638 pParse->zErr = sqlite3_mprintf(
173639 "fts5: %s queries are not supported (detail!=full)",
@@ -173640,10 +174289,11 @@
173640 pNear->nPhrase==1 ? "phrase": "NEAR"
173641 );
173642 sqlite3_free(pRet);
173643 pRet = 0;
173644 }
 
173645 }else{
173646 fts5ExprAddChildren(pRet, pLeft);
173647 fts5ExprAddChildren(pRet, pRight);
173648 }
173649 }
@@ -173922,11 +174572,11 @@
173922 if( rc==SQLITE_OK ){
173923 rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr);
173924 }
173925 if( rc==SQLITE_OK ){
173926 char *zText;
173927 if( pExpr->pRoot==0 ){
173928 zText = sqlite3_mprintf("");
173929 }else if( bTcl ){
173930 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
173931 }else{
173932 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
@@ -174022,11 +174672,11 @@
174022 };
174023 int i;
174024 int rc = SQLITE_OK;
174025 void *pCtx = (void*)pGlobal;
174026
174027 for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aFunc); i++){
174028 struct Fts5ExprFunc *p = &aFunc[i];
174029 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
174030 }
174031
174032 /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */
@@ -174119,16 +174769,18 @@
174119 static int fts5ExprPopulatePoslistsCb(
174120 void *pCtx, /* Copy of 2nd argument to xTokenize() */
174121 int tflags, /* Mask of FTS5_TOKEN_* flags */
174122 const char *pToken, /* Pointer to buffer containing token */
174123 int nToken, /* Size of token in bytes */
174124 int iStart, /* Byte offset of token within input text */
174125 int iEnd /* Byte offset of end of token within input text */
174126 ){
174127 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
174128 Fts5Expr *pExpr = p->pExpr;
174129 int i;
 
 
174130
174131 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
174132 for(i=0; i<pExpr->nPhrase; i++){
174133 Fts5ExprTerm *pTerm;
174134 if( p->aPopulator[i].bOk==0 ) continue;
@@ -174260,30 +174912,25 @@
174260 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
174261 Fts5ExprNode *pNode = pPhrase->pNode;
174262 int rc = SQLITE_OK;
174263
174264 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
 
 
174265 if( pNode->bEof==0
174266 && pNode->iRowid==pExpr->pRoot->iRowid
174267 && pPhrase->poslist.n>0
174268 ){
174269 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
174270 if( pTerm->pSynonym ){
174271 int bDel = 0;
174272 u8 *a;
174273 rc = fts5ExprSynonymList(
174274 pTerm, 1, 0, pNode->iRowid, &bDel, &a, pnCollist
174275 );
174276 if( bDel ){
174277 sqlite3Fts5BufferSet(&rc, &pPhrase->poslist, *pnCollist, a);
174278 *ppCollist = pPhrase->poslist.p;
174279 sqlite3_free(a);
174280 }else{
174281 *ppCollist = a;
174282 }
174283 }else{
174284 sqlite3Fts5IterCollist(pPhrase->aTerm[0].pIter, ppCollist, pnCollist);
174285 }
174286 }else{
174287 *ppCollist = 0;
174288 *pnCollist = 0;
174289 }
@@ -174354,14 +175001,14 @@
174354 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
174355
174356 int nAlloc; /* Total size of allocation */
174357 int iSzPoslist; /* Offset of space for 4-byte poslist size */
174358 int nData; /* Total bytes of data (incl. structure) */
 
174359 u8 bDel; /* Set delete-flag @ iSzPoslist */
174360 u8 bContent; /* Set content-flag (detail=none mode) */
174361
174362 int iCol; /* Column of last value written */
174363 int iPos; /* Position of last value written */
174364 i64 iRowid; /* Rowid of last value written */
174365 char zKey[8]; /* Nul-terminated entry key */
174366 };
174367
@@ -174537,12 +175184,12 @@
174537
174538 /* Attempt to locate an existing hash entry */
174539 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
174540 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
174541 if( p->zKey[0]==bByte
 
174542 && memcmp(&p->zKey[1], pToken, nToken)==0
174543 && p->zKey[nToken+1]==0
174544 ){
174545 break;
174546 }
174547 }
174548
@@ -174565,10 +175212,11 @@
174565 memset(p, 0, FTS5_HASHENTRYSIZE);
174566 p->nAlloc = nByte;
174567 p->zKey[0] = bByte;
174568 memcpy(&p->zKey[1], pToken, nToken);
174569 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)p->zKey, nToken+1) );
 
174570 p->zKey[nToken+1] = '\0';
174571 p->nData = nToken+1 + 1 + FTS5_HASHENTRYSIZE;
174572 p->pHashNext = pHash->aSlot[iHash];
174573 pHash->aSlot[iHash] = p;
174574 pHash->nEntry++;
@@ -175080,10 +175728,11 @@
175080
175081 typedef struct Fts5Data Fts5Data;
175082 typedef struct Fts5DlidxIter Fts5DlidxIter;
175083 typedef struct Fts5DlidxLvl Fts5DlidxLvl;
175084 typedef struct Fts5DlidxWriter Fts5DlidxWriter;
 
175085 typedef struct Fts5PageWriter Fts5PageWriter;
175086 typedef struct Fts5SegIter Fts5SegIter;
175087 typedef struct Fts5DoclistIter Fts5DoclistIter;
175088 typedef struct Fts5SegWriter Fts5SegWriter;
175089 typedef struct Fts5Structure Fts5Structure;
@@ -175322,20 +175971,24 @@
175322 **
175323 ** poslist:
175324 ** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
175325 ** There is no way to tell if this is populated or not.
175326 */
175327 struct Fts5IndexIter {
 
 
175328 Fts5Index *pIndex; /* Index that owns this iterator */
175329 Fts5Structure *pStruct; /* Database structure for this iterator */
175330 Fts5Buffer poslist; /* Buffer containing current poslist */
 
 
 
 
175331
175332 int nSeg; /* Size of aSeg[] array */
175333 int bRev; /* True to iterate in reverse order */
175334 u8 bSkipEmpty; /* True to skip deleted entries */
175335 u8 bEof; /* True at EOF */
175336 u8 bFiltered; /* True if column-filter already applied */
175337
175338 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
175339 Fts5CResult *aFirst; /* Current merge state (see above) */
175340 Fts5SegIter aSeg[1]; /* Array of segment iterators */
175341 };
@@ -175421,21 +176074,10 @@
175421 int nCmp = MIN(pLeft->n, pRight->n);
175422 int res = memcmp(pLeft->p, pRight->p, nCmp);
175423 return (res==0 ? (pLeft->n - pRight->n) : res);
175424 }
175425
175426 #ifdef SQLITE_DEBUG
175427 static int fts5BlobCompare(
175428 const u8 *pLeft, int nLeft,
175429 const u8 *pRight, int nRight
175430 ){
175431 int nCmp = MIN(nLeft, nRight);
175432 int res = memcmp(pLeft, pRight, nCmp);
175433 return (res==0 ? (nLeft - nRight) : res);
175434 }
175435 #endif
175436
175437 static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
175438 int ret;
175439 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
175440 return ret;
175441 }
@@ -175693,28 +176335,37 @@
175693 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
175694 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
175695 int nTotal;
175696 int iSeg;
175697
175698 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
175699 i += fts5GetVarint32(&pData[i], nTotal);
175700 assert( nTotal>=pLvl->nMerge );
175701 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
175702 nTotal * sizeof(Fts5StructureSegment)
175703 );
 
 
 
 
175704
175705 if( rc==SQLITE_OK ){
175706 pLvl->nSeg = nTotal;
175707 for(iSeg=0; iSeg<nTotal; iSeg++){
 
 
 
 
175708 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid);
175709 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst);
175710 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast);
175711 }
175712 }else{
175713 fts5StructureRelease(pRet);
175714 pRet = 0;
175715 }
 
 
 
 
175716 }
175717 }
175718
175719 *ppOut = pRet;
175720 return rc;
@@ -176378,10 +177029,14 @@
176378 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
176379 int iOff = pIter->iLeafOffset; /* Offset to read at */
176380 int nNew; /* Bytes of new data */
176381
176382 iOff += fts5GetVarint32(&a[iOff], nNew);
 
 
 
 
176383 pIter->term.n = nKeep;
176384 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
176385 iOff += nNew;
176386 pIter->iTermLeafOffset = iOff;
176387 pIter->iTermLeafPgno = pIter->iLeafPgno;
@@ -176571,11 +177226,11 @@
176571 /*
176572 ** Return true if the iterator passed as the second argument currently
176573 ** points to a delete marker. A delete marker is an entry with a 0 byte
176574 ** position-list.
176575 */
176576 static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5IndexIter *pIter){
176577 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
176578 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
176579 }
176580
176581 /*
@@ -176584,14 +177239,16 @@
176584 ** This version of fts5SegIterNext() is only used by reverse iterators.
176585 */
176586 static void fts5SegIterNext_Reverse(
176587 Fts5Index *p, /* FTS5 backend object */
176588 Fts5SegIter *pIter, /* Iterator to advance */
176589 int *pbNewTerm /* OUT: Set for new term */
176590 ){
176591 assert( pIter->flags & FTS5_SEGITER_REVERSE );
176592 assert( pIter->pNextLeaf==0 );
 
 
176593 if( pIter->iRowidOffset>0 ){
176594 u8 *a = pIter->pLeaf->p;
176595 int iOff;
176596 i64 iDelta;
176597
@@ -176843,13 +177500,10 @@
176843 iPoslist = pIter->iTermLeafOffset;
176844 }else{
176845 iPoslist = 4;
176846 }
176847 fts5IndexSkipVarint(pLeaf->p, iPoslist);
176848 assert( p->pConfig->eDetail==FTS5_DETAIL_NONE || iPoslist==(
176849 pIter->iLeafOffset - sqlite3Fts5GetVarintLen(pIter->nPos*2+pIter->bDel)
176850 ));
176851 pIter->iLeafOffset = iPoslist;
176852
176853 /* If this condition is true then the largest rowid for the current
176854 ** term may not be stored on the current page. So search forward to
176855 ** see where said rowid really is. */
@@ -177067,23 +177721,19 @@
177067 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
177068 ** an error has already occurred when this function is called, it is a no-op.
177069 */
177070 static void fts5SegIterSeekInit(
177071 Fts5Index *p, /* FTS5 backend */
177072 Fts5Buffer *pBuf, /* Buffer to use for loading pages */
177073 const u8 *pTerm, int nTerm, /* Term to seek to */
177074 int flags, /* Mask of FTS5INDEX_XXX flags */
177075 Fts5StructureSegment *pSeg, /* Description of segment */
177076 Fts5SegIter *pIter /* Object to populate */
177077 ){
177078 int iPg = 1;
177079 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
177080 int bDlidx = 0; /* True if there is a doclist-index */
177081
177082 static int nCall = 0;
177083 nCall++;
177084
177085 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
177086 assert( pTerm && nTerm );
177087 memset(pIter, 0, sizeof(*pIter));
177088 pIter->pSeg = pSeg;
177089
@@ -177225,11 +177875,11 @@
177225 ** fts5AssertMultiIterSetup(). It ensures that the result currently stored
177226 ** in *pRes is the correct result of comparing the current positions of the
177227 ** two iterators.
177228 */
177229 static void fts5AssertComparisonResult(
177230 Fts5IndexIter *pIter,
177231 Fts5SegIter *p1,
177232 Fts5SegIter *p2,
177233 Fts5CResult *pRes
177234 ){
177235 int i1 = p1 - pIter->aSeg;
@@ -177266,16 +177916,16 @@
177266 ** This function is a no-op unless SQLITE_DEBUG is defined when this module
177267 ** is compiled. In that case, this function is essentially an assert()
177268 ** statement used to verify that the contents of the pIter->aFirst[] array
177269 ** are correct.
177270 */
177271 static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5IndexIter *pIter){
177272 if( p->rc==SQLITE_OK ){
177273 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177274 int i;
177275
177276 assert( (pFirst->pLeaf==0)==pIter->bEof );
177277
177278 /* Check that pIter->iSwitchRowid is set correctly. */
177279 for(i=0; i<pIter->nSeg; i++){
177280 Fts5SegIter *p1 = &pIter->aSeg[i];
177281 assert( p1==pFirst
@@ -177311,11 +177961,11 @@
177311 ** If the returned value is non-zero, then it is the index of an entry
177312 ** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
177313 ** to a key that is a duplicate of another, higher priority,
177314 ** segment-iterator in the pSeg->aSeg[] array.
177315 */
177316 static int fts5MultiIterDoCompare(Fts5IndexIter *pIter, int iOut){
177317 int i1; /* Index of left-hand Fts5SegIter */
177318 int i2; /* Index of right-hand Fts5SegIter */
177319 int iRes;
177320 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
177321 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
@@ -177457,11 +178107,11 @@
177457
177458
177459 /*
177460 ** Free the iterator object passed as the second argument.
177461 */
177462 static void fts5MultiIterFree(Fts5Index *p, Fts5IndexIter *pIter){
177463 if( pIter ){
177464 int i;
177465 for(i=0; i<pIter->nSeg; i++){
177466 fts5SegIterClear(&pIter->aSeg[i]);
177467 }
@@ -177471,11 +178121,11 @@
177471 }
177472 }
177473
177474 static void fts5MultiIterAdvanced(
177475 Fts5Index *p, /* FTS5 backend to iterate within */
177476 Fts5IndexIter *pIter, /* Iterator to update aFirst[] array for */
177477 int iChanged, /* Index of sub-iterator just advanced */
177478 int iMinset /* Minimum entry in aFirst[] to set */
177479 ){
177480 int i;
177481 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
@@ -177498,13 +178148,13 @@
177498 ** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
177499 ** on the iterator instead. That function does the same as this one, except
177500 ** that it deals with more complicated cases as well.
177501 */
177502 static int fts5MultiIterAdvanceRowid(
177503 Fts5Index *p, /* FTS5 backend to iterate within */
177504 Fts5IndexIter *pIter, /* Iterator to update aFirst[] array for */
177505 int iChanged /* Index of sub-iterator just advanced */
177506 ){
177507 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
177508
177509 if( pNew->iRowid==pIter->iSwitchRowid
177510 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
@@ -177533,19 +178183,20 @@
177533
177534 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
177535 }
177536 }
177537
 
177538 return 0;
177539 }
177540
177541 /*
177542 ** Set the pIter->bEof variable based on the state of the sub-iterators.
177543 */
177544 static void fts5MultiIterSetEof(Fts5IndexIter *pIter){
177545 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177546 pIter->bEof = pSeg->pLeaf==0;
177547 pIter->iSwitchRowid = pSeg->iRowid;
177548 }
177549
177550 /*
177551 ** Move the iterator to the next entry.
@@ -177554,43 +178205,48 @@
177554 ** considered an error if the iterator reaches EOF, or if it is already at
177555 ** EOF when this function is called.
177556 */
177557 static void fts5MultiIterNext(
177558 Fts5Index *p,
177559 Fts5IndexIter *pIter,
177560 int bFrom, /* True if argument iFrom is valid */
177561 i64 iFrom /* Advance at least as far as this */
177562 ){
177563 if( p->rc==SQLITE_OK ){
177564 int bUseFrom = bFrom;
177565 do {
177566 int iFirst = pIter->aFirst[1].iFirst;
177567 int bNewTerm = 0;
177568 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
177569 assert( p->rc==SQLITE_OK );
177570 if( bUseFrom && pSeg->pDlidx ){
177571 fts5SegIterNextFrom(p, pSeg, iFrom);
177572 }else{
177573 pSeg->xNext(p, pSeg, &bNewTerm);
177574 }
177575
177576 if( pSeg->pLeaf==0 || bNewTerm
177577 || fts5MultiIterAdvanceRowid(p, pIter, iFirst)
177578 ){
177579 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
177580 fts5MultiIterSetEof(pIter);
177581 }
177582 fts5AssertMultiIterSetup(p, pIter);
177583
177584 bUseFrom = 0;
177585 }while( pIter->bSkipEmpty && fts5MultiIterIsEmpty(p, pIter) );
 
 
 
 
 
177586 }
177587 }
177588
177589 static void fts5MultiIterNext2(
177590 Fts5Index *p,
177591 Fts5IndexIter *pIter,
177592 int *pbNewTerm /* OUT: True if *might* be new term */
177593 ){
177594 assert( pIter->bSkipEmpty );
177595 if( p->rc==SQLITE_OK ){
177596 do {
@@ -177599,11 +178255,11 @@
177599 int bNewTerm = 0;
177600
177601 assert( p->rc==SQLITE_OK );
177602 pSeg->xNext(p, pSeg, &bNewTerm);
177603 if( pSeg->pLeaf==0 || bNewTerm
177604 || fts5MultiIterAdvanceRowid(p, pIter, iFirst)
177605 ){
177606 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
177607 fts5MultiIterSetEof(pIter);
177608 *pbNewTerm = 1;
177609 }else{
@@ -177613,220 +178269,147 @@
177613
177614 }while( fts5MultiIterIsEmpty(p, pIter) );
177615 }
177616 }
177617
 
 
 
177618
177619 static Fts5IndexIter *fts5MultiIterAlloc(
177620 Fts5Index *p, /* FTS5 backend to iterate within */
177621 int nSeg
177622 ){
177623 Fts5IndexIter *pNew;
177624 int nSlot; /* Power of two >= nSeg */
177625
177626 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
177627 pNew = fts5IdxMalloc(p,
177628 sizeof(Fts5IndexIter) + /* pNew */
177629 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
177630 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
177631 );
177632 if( pNew ){
177633 pNew->nSeg = nSlot;
177634 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
177635 pNew->pIndex = p;
 
177636 }
177637 return pNew;
177638 }
177639
177640 /*
177641 ** Allocate a new Fts5IndexIter object.
177642 **
177643 ** The new object will be used to iterate through data in structure pStruct.
177644 ** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
177645 ** is zero or greater, data from the first nSegment segments on level iLevel
177646 ** is merged.
177647 **
177648 ** The iterator initially points to the first term/rowid entry in the
177649 ** iterated data.
177650 */
177651 static void fts5MultiIterNew(
177652 Fts5Index *p, /* FTS5 backend to iterate within */
177653 Fts5Structure *pStruct, /* Structure of specific index */
177654 int bSkipEmpty, /* True to ignore delete-keys */
177655 int flags, /* FTS5INDEX_QUERY_XXX flags */
177656 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
177657 int iLevel, /* Level to iterate (-1 for all) */
177658 int nSegment, /* Number of segments to merge (iLevel>=0) */
177659 Fts5IndexIter **ppOut /* New object */
177660 ){
177661 int nSeg = 0; /* Number of segment-iters in use */
177662 int iIter = 0; /* */
177663 int iSeg; /* Used to iterate through segments */
177664 Fts5Buffer buf = {0,0,0}; /* Buffer used by fts5SegIterSeekInit() */
177665 Fts5StructureLevel *pLvl;
177666 Fts5IndexIter *pNew;
177667
177668 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
177669
177670 /* Allocate space for the new multi-seg-iterator. */
177671 if( p->rc==SQLITE_OK ){
177672 if( iLevel<0 ){
177673 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
177674 nSeg = pStruct->nSegment;
177675 nSeg += (p->pHash ? 1 : 0);
177676 }else{
177677 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
177678 }
177679 }
177680 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
177681 if( pNew==0 ) return;
177682 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
177683 pNew->bSkipEmpty = (u8)bSkipEmpty;
177684 pNew->pStruct = pStruct;
177685 fts5StructureRef(pStruct);
177686
177687 /* Initialize each of the component segment iterators. */
177688 if( iLevel<0 ){
177689 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
177690 if( p->pHash ){
177691 /* Add a segment iterator for the current contents of the hash table. */
177692 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
177693 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
177694 }
177695 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
177696 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
177697 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
177698 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
177699 if( pTerm==0 ){
177700 fts5SegIterInit(p, pSeg, pIter);
177701 }else{
177702 fts5SegIterSeekInit(p, &buf, pTerm, nTerm, flags, pSeg, pIter);
177703 }
177704 }
177705 }
177706 }else{
177707 pLvl = &pStruct->aLevel[iLevel];
177708 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
177709 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
177710 }
177711 }
177712 assert( iIter==nSeg );
177713
177714 /* If the above was successful, each component iterators now points
177715 ** to the first entry in its segment. In this case initialize the
177716 ** aFirst[] array. Or, if an error has occurred, free the iterator
177717 ** object and set the output variable to NULL. */
177718 if( p->rc==SQLITE_OK ){
177719 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
177720 int iEq;
177721 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
177722 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
177723 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
177724 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
177725 }
177726 }
177727 fts5MultiIterSetEof(pNew);
177728 fts5AssertMultiIterSetup(p, pNew);
177729
177730 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
177731 fts5MultiIterNext(p, pNew, 0, 0);
177732 }
177733 }else{
177734 fts5MultiIterFree(p, pNew);
177735 *ppOut = 0;
177736 }
177737 fts5BufferFree(&buf);
177738 }
177739
177740 /*
177741 ** Create an Fts5IndexIter that iterates through the doclist provided
177742 ** as the second argument.
177743 */
177744 static void fts5MultiIterNew2(
177745 Fts5Index *p, /* FTS5 backend to iterate within */
177746 Fts5Data *pData, /* Doclist to iterate through */
177747 int bDesc, /* True for descending rowid order */
177748 Fts5IndexIter **ppOut /* New object */
177749 ){
177750 Fts5IndexIter *pNew;
177751 pNew = fts5MultiIterAlloc(p, 2);
177752 if( pNew ){
177753 Fts5SegIter *pIter = &pNew->aSeg[1];
177754
177755 pNew->bFiltered = 1;
177756 pIter->flags = FTS5_SEGITER_ONETERM;
177757 if( pData->szLeaf>0 ){
177758 pIter->pLeaf = pData;
177759 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
177760 pIter->iEndofDoclist = pData->nn;
177761 pNew->aFirst[1].iFirst = 1;
177762 if( bDesc ){
177763 pNew->bRev = 1;
177764 pIter->flags |= FTS5_SEGITER_REVERSE;
177765 fts5SegIterReverseInitPage(p, pIter);
177766 }else{
177767 fts5SegIterLoadNPos(p, pIter);
177768 }
177769 pData = 0;
177770 }else{
177771 pNew->bEof = 1;
177772 }
177773 fts5SegIterSetNext(p, pIter);
177774
177775 *ppOut = pNew;
177776 }
177777
177778 fts5DataRelease(pData);
177779 }
177780
177781 /*
177782 ** Return true if the iterator is at EOF or if an error has occurred.
177783 ** False otherwise.
177784 */
177785 static int fts5MultiIterEof(Fts5Index *p, Fts5IndexIter *pIter){
177786 assert( p->rc
177787 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->bEof
177788 );
177789 return (p->rc || pIter->bEof);
177790 }
177791
177792 /*
177793 ** Return the rowid of the entry that the iterator currently points
177794 ** to. If the iterator points to EOF when this function is called the
177795 ** results are undefined.
177796 */
177797 static i64 fts5MultiIterRowid(Fts5IndexIter *pIter){
177798 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
177799 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
177800 }
177801
177802 /*
177803 ** Move the iterator to the next entry at or following iMatch.
177804 */
177805 static void fts5MultiIterNextFrom(
177806 Fts5Index *p,
177807 Fts5IndexIter *pIter,
177808 i64 iMatch
177809 ){
177810 while( 1 ){
177811 i64 iRowid;
177812 fts5MultiIterNext(p, pIter, 1, iMatch);
177813 if( fts5MultiIterEof(p, pIter) ) break;
177814 iRowid = fts5MultiIterRowid(pIter);
177815 if( pIter->bRev==0 && iRowid>=iMatch ) break;
177816 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
177817 }
177818 }
177819
177820 /*
177821 ** Return a pointer to a buffer containing the term associated with the
177822 ** entry that the iterator currently points to.
177823 */
177824 static const u8 *fts5MultiIterTerm(Fts5IndexIter *pIter, int *pn){
177825 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177826 *pn = p->term.n;
177827 return p->term.p;
177828 }
177829
177830 static void fts5ChunkIterate(
177831 Fts5Index *p, /* Index object */
177832 Fts5SegIter *pSeg, /* Poslist of this iterator */
@@ -177866,11 +178449,459 @@
177866 }
177867 }
177868 }
177869 }
177870
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177871
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177872
177873 /*
177874 ** Allocate a new segment-id for the structure pStruct. The new segment
177875 ** id must be between 1 and 65335 inclusive, and must not be used by
177876 ** any currently existing segment. If a free segment id cannot be found,
@@ -177914,19 +178945,18 @@
177914 p->nPendingData = 0;
177915 }
177916 }
177917
177918 /*
177919 ** Return the size of the prefix, in bytes, that buffer (nNew/pNew) shares
177920 ** with buffer (nOld/pOld).
 
 
 
177921 */
177922 static int fts5PrefixCompress(
177923 int nOld, const u8 *pOld,
177924 int nNew, const u8 *pNew
177925 ){
177926 int i;
177927 assert( fts5BlobCompare(pOld, nOld, pNew, nNew)<0 );
177928 for(i=0; i<nOld; i++){
177929 if( pOld[i]!=pNew[i] ) break;
177930 }
177931 return i;
177932 }
@@ -178232,17 +179262,17 @@
178232 ** In this case the previous term is not available, so just write a
178233 ** copy of (pTerm/nTerm) into the parent node. This is slightly
178234 ** inefficient, but still correct. */
178235 int n = nTerm;
178236 if( pPage->term.n ){
178237 n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, nTerm, pTerm);
178238 }
178239 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
178240 pPage = &pWriter->writer;
178241 }
178242 }else{
178243 nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, nTerm, pTerm);
178244 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
178245 }
178246
178247 /* Append the number of bytes of new data, then the term data itself
178248 ** to the page. */
@@ -178401,11 +179431,11 @@
178401 /*
178402 ** Iterator pIter was used to iterate through the input segments of on an
178403 ** incremental merge operation. This function is called if the incremental
178404 ** merge step has finished but the input has not been completely exhausted.
178405 */
178406 static void fts5TrimSegments(Fts5Index *p, Fts5IndexIter *pIter){
178407 int i;
178408 Fts5Buffer buf;
178409 memset(&buf, 0, sizeof(Fts5Buffer));
178410 for(i=0; i<pIter->nSeg; i++){
178411 Fts5SegIter *pSeg = &pIter->aSeg[i];
@@ -178479,18 +179509,19 @@
178479 int *pnRem /* Write up to this many output leaves */
178480 ){
178481 Fts5Structure *pStruct = *ppStruct;
178482 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
178483 Fts5StructureLevel *pLvlOut;
178484 Fts5IndexIter *pIter = 0; /* Iterator to read input data */
178485 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
178486 int nInput; /* Number of input segments */
178487 Fts5SegWriter writer; /* Writer object */
178488 Fts5StructureSegment *pSeg; /* Output segment */
178489 Fts5Buffer term;
178490 int bOldest; /* True if the output segment is the oldest */
178491 int eDetail = p->pConfig->eDetail;
 
178492
178493 assert( iLvl<pStruct->nLevel );
178494 assert( pLvl->nMerge<=pLvl->nSeg );
178495
178496 memset(&writer, 0, sizeof(Fts5SegWriter));
@@ -178531,11 +179562,11 @@
178531 nInput = pLvl->nSeg;
178532 }
178533 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
178534
178535 assert( iLvl>=0 );
178536 for(fts5MultiIterNew(p, pStruct, 0, 0, 0, 0, iLvl, nInput, &pIter);
178537 fts5MultiIterEof(p, pIter)==0;
178538 fts5MultiIterNext(p, pIter, 0, 0)
178539 ){
178540 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
178541 int nPos; /* position-list size field value */
@@ -178603,11 +179634,11 @@
178603 assert( pSeg->pgnoLast>0 );
178604 fts5TrimSegments(p, pIter);
178605 pLvl->nMerge = nInput;
178606 }
178607
178608 fts5MultiIterFree(p, pIter);
178609 fts5BufferFree(&term);
178610 if( pnRem ) *pnRem -= writer.nLeafWritten;
178611 }
178612
178613 /*
@@ -178976,279 +180007,33 @@
178976 fts5StructureRelease(pStruct);
178977
178978 return fts5IndexReturn(p);
178979 }
178980
178981 static void fts5PoslistCallback(
178982 Fts5Index *p,
178983 void *pContext,
178984 const u8 *pChunk, int nChunk
178985 ){
178986 assert_nc( nChunk>=0 );
178987 if( nChunk>0 ){
178988 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
178989 }
178990 }
178991
178992 typedef struct PoslistCallbackCtx PoslistCallbackCtx;
178993 struct PoslistCallbackCtx {
178994 Fts5Buffer *pBuf; /* Append to this buffer */
178995 Fts5Colset *pColset; /* Restrict matches to this column */
178996 int eState; /* See above */
178997 };
178998
178999 typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
179000 struct PoslistOffsetsCtx {
179001 Fts5Buffer *pBuf; /* Append to this buffer */
179002 Fts5Colset *pColset; /* Restrict matches to this column */
179003 int iRead;
179004 int iWrite;
179005 };
179006
179007 /*
179008 ** TODO: Make this more efficient!
179009 */
179010 static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
179011 int i;
179012 for(i=0; i<pColset->nCol; i++){
179013 if( pColset->aiCol[i]==iCol ) return 1;
179014 }
179015 return 0;
179016 }
179017
179018 static void fts5PoslistOffsetsCallback(
179019 Fts5Index *p,
179020 void *pContext,
179021 const u8 *pChunk, int nChunk
179022 ){
179023 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
179024 assert_nc( nChunk>=0 );
179025 if( nChunk>0 ){
179026 int i = 0;
179027 while( i<nChunk ){
179028 int iVal;
179029 i += fts5GetVarint32(&pChunk[i], iVal);
179030 iVal += pCtx->iRead - 2;
179031 pCtx->iRead = iVal;
179032 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
179033 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
179034 pCtx->iWrite = iVal;
179035 }
179036 }
179037 }
179038 }
179039
179040 static void fts5PoslistFilterCallback(
179041 Fts5Index *p,
179042 void *pContext,
179043 const u8 *pChunk, int nChunk
179044 ){
179045 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
179046 assert_nc( nChunk>=0 );
179047 if( nChunk>0 ){
179048 /* Search through to find the first varint with value 1. This is the
179049 ** start of the next columns hits. */
179050 int i = 0;
179051 int iStart = 0;
179052
179053 if( pCtx->eState==2 ){
179054 int iCol;
179055 fts5FastGetVarint32(pChunk, i, iCol);
179056 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
179057 pCtx->eState = 1;
179058 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
179059 }else{
179060 pCtx->eState = 0;
179061 }
179062 }
179063
179064 do {
179065 while( i<nChunk && pChunk[i]!=0x01 ){
179066 while( pChunk[i] & 0x80 ) i++;
179067 i++;
179068 }
179069 if( pCtx->eState ){
179070 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
179071 }
179072 if( i<nChunk ){
179073 int iCol;
179074 iStart = i;
179075 i++;
179076 if( i>=nChunk ){
179077 pCtx->eState = 2;
179078 }else{
179079 fts5FastGetVarint32(pChunk, i, iCol);
179080 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
179081 if( pCtx->eState ){
179082 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
179083 iStart = i;
179084 }
179085 }
179086 }
179087 }while( i<nChunk );
179088 }
179089 }
179090
179091 /*
179092 ** Iterator pIter currently points to a valid entry (not EOF). This
179093 ** function appends the position list data for the current entry to
179094 ** buffer pBuf. It does not make a copy of the position-list size
179095 ** field.
179096 */
179097 static void fts5SegiterPoslist(
179098 Fts5Index *p,
179099 Fts5SegIter *pSeg,
179100 Fts5Colset *pColset,
179101 Fts5Buffer *pBuf
179102 ){
179103 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
179104 if( pColset==0 ){
179105 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
179106 }else{
179107 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
179108 PoslistCallbackCtx sCtx;
179109 sCtx.pBuf = pBuf;
179110 sCtx.pColset = pColset;
179111 sCtx.eState = fts5IndexColsetTest(pColset, 0);
179112 assert( sCtx.eState==0 || sCtx.eState==1 );
179113 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
179114 }else{
179115 PoslistOffsetsCtx sCtx;
179116 memset(&sCtx, 0, sizeof(sCtx));
179117 sCtx.pBuf = pBuf;
179118 sCtx.pColset = pColset;
179119 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
179120 }
179121 }
179122 }
179123 }
179124
179125 /*
179126 ** IN/OUT parameter (*pa) points to a position list n bytes in size. If
179127 ** the position list contains entries for column iCol, then (*pa) is set
179128 ** to point to the sub-position-list for that column and the number of
179129 ** bytes in it returned. Or, if the argument position list does not
179130 ** contain any entries for column iCol, return 0.
179131 */
179132 static int fts5IndexExtractCol(
179133 const u8 **pa, /* IN/OUT: Pointer to poslist */
179134 int n, /* IN: Size of poslist in bytes */
179135 int iCol /* Column to extract from poslist */
179136 ){
179137 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
179138 const u8 *p = *pa;
179139 const u8 *pEnd = &p[n]; /* One byte past end of position list */
179140 u8 prev = 0;
179141
179142 while( iCol>iCurrent ){
179143 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
179144 ** not part of a varint */
179145 while( (prev & 0x80) || *p!=0x01 ){
179146 prev = *p++;
179147 if( p==pEnd ) return 0;
179148 }
179149 *pa = p++;
179150 p += fts5GetVarint32(p, iCurrent);
179151 }
179152 if( iCol!=iCurrent ) return 0;
179153
179154 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
179155 ** not part of a varint */
179156 assert( (prev & 0x80)==0 );
179157 while( p<pEnd && ((prev & 0x80) || *p!=0x01) ){
179158 prev = *p++;
179159 }
179160 return p - (*pa);
179161 }
179162
179163 static int fts5AppendRowid(
179164 Fts5Index *p,
179165 i64 iDelta,
179166 Fts5IndexIter *pMulti,
179167 Fts5Colset *pColset,
179168 Fts5Buffer *pBuf
179169 ){
179170 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
179171 return 0;
179172 }
179173
179174 /*
179175 ** Iterator pMulti currently points to a valid entry (not EOF). This
179176 ** function appends the following to buffer pBuf:
179177 **
179178 ** * The varint iDelta, and
179179 ** * the position list that currently points to, including the size field.
179180 **
179181 ** If argument pColset is NULL, then the position list is filtered according
179182 ** to pColset before being appended to the buffer. If this means there are
179183 ** no entries in the position list, nothing is appended to the buffer (not
179184 ** even iDelta).
179185 **
179186 ** If an error occurs, an error code is left in p->rc.
179187 */
179188 static int fts5AppendPoslist(
179189 Fts5Index *p,
179190 i64 iDelta,
179191 Fts5IndexIter *pMulti,
179192 Fts5Colset *pColset,
179193 Fts5Buffer *pBuf
179194 ){
179195 if( p->rc==SQLITE_OK ){
179196 Fts5SegIter *pSeg = &pMulti->aSeg[ pMulti->aFirst[1].iFirst ];
179197 assert( fts5MultiIterEof(p, pMulti)==0 );
179198 assert( pSeg->nPos>0 );
179199 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos+9+9) ){
179200 if( p->pConfig->eDetail==FTS5_DETAIL_FULL
179201 && pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf
179202 && (pColset==0 || pColset->nCol==1)
179203 ){
179204 const u8 *pPos = &pSeg->pLeaf->p[pSeg->iLeafOffset];
179205 int nPos;
179206 if( pColset ){
179207 nPos = fts5IndexExtractCol(&pPos, pSeg->nPos, pColset->aiCol[0]);
179208 if( nPos==0 ) return 1;
179209 }else{
179210 nPos = pSeg->nPos;
179211 }
179212 assert( nPos>0 );
179213 fts5BufferSafeAppendVarint(pBuf, iDelta);
179214 fts5BufferSafeAppendVarint(pBuf, nPos*2);
179215 fts5BufferSafeAppendBlob(pBuf, pPos, nPos);
179216 }else{
179217 int iSv1;
179218 int iSv2;
179219 int iData;
179220
179221 /* Append iDelta */
179222 iSv1 = pBuf->n;
179223 fts5BufferSafeAppendVarint(pBuf, iDelta);
179224
179225 /* WRITEPOSLISTSIZE */
179226 iSv2 = pBuf->n;
179227 fts5BufferSafeAppendVarint(pBuf, pSeg->nPos*2);
179228 iData = pBuf->n;
179229
179230 fts5SegiterPoslist(p, pSeg, pColset, pBuf);
179231
179232 if( pColset ){
179233 int nActual = pBuf->n - iData;
179234 if( nActual!=pSeg->nPos ){
179235 if( nActual==0 ){
179236 pBuf->n = iSv1;
179237 return 1;
179238 }else{
179239 int nReq = sqlite3Fts5GetVarintLen((u32)(nActual*2));
179240 while( iSv2<(iData-nReq) ){ pBuf->p[iSv2++] = 0x80; }
179241 sqlite3Fts5PutVarint(&pBuf->p[iSv2], nActual*2);
179242 }
179243 }
179244 }
179245 }
179246 }
179247 }
179248
179249 return 0;
179250 }
179251
179252
179253 static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
179254 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
@@ -179388,73 +180173,107 @@
179388 ){
179389 if( p2->n ){
179390 i64 iLastRowid = 0;
179391 Fts5DoclistIter i1;
179392 Fts5DoclistIter i2;
179393 Fts5Buffer out;
179394 Fts5Buffer tmp;
179395 memset(&out, 0, sizeof(out));
179396 memset(&tmp, 0, sizeof(tmp));
179397
179398 sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n);
179399 fts5DoclistIterInit(p1, &i1);
179400 fts5DoclistIterInit(p2, &i2);
179401 while( p->rc==SQLITE_OK && (i1.aPoslist!=0 || i2.aPoslist!=0) ){
179402 if( i2.aPoslist==0 || (i1.aPoslist && i1.iRowid<i2.iRowid) ){
 
179403 /* Copy entry from i1 */
179404 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
179405 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
179406 fts5DoclistIterNext(&i1);
 
179407 }
179408 else if( i1.aPoslist==0 || i2.iRowid!=i1.iRowid ){
179409 /* Copy entry from i2 */
179410 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
179411 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
179412 fts5DoclistIterNext(&i2);
 
179413 }
179414 else{
 
179415 i64 iPos1 = 0;
179416 i64 iPos2 = 0;
179417 int iOff1 = 0;
179418 int iOff2 = 0;
179419 u8 *a1 = &i1.aPoslist[i1.nSize];
179420 u8 *a2 = &i2.aPoslist[i2.nSize];
179421
179422 Fts5PoslistWriter writer;
179423 memset(&writer, 0, sizeof(writer));
179424
179425 /* Merge the two position lists. */
179426 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
179427 fts5BufferZero(&tmp);
179428
179429 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
179430 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
179431
179432 while( p->rc==SQLITE_OK && (iPos1>=0 || iPos2>=0) ){
179433 i64 iNew;
179434 if( iPos2<0 || (iPos1>=0 && iPos1<iPos2) ){
179435 iNew = iPos1;
179436 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
179437 }else{
179438 iNew = iPos2;
179439 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
179440 if( iPos1==iPos2 ){
179441 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1,&iPos1);
179442 }
179443 }
179444 if( iNew!=writer.iPrev || tmp.n==0 ){
179445 p->rc = sqlite3Fts5PoslistWriterAppend(&tmp, &writer, iNew);
179446 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179447 }
179448
179449 /* WRITEPOSLISTSIZE */
179450 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
179451 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
179452 fts5DoclistIterNext(&i1);
179453 fts5DoclistIterNext(&i2);
 
179454 }
179455 }
 
 
 
 
 
 
 
 
 
179456
179457 fts5BufferSet(&p->rc, p1, out.n, out.p);
179458 fts5BufferFree(&tmp);
179459 fts5BufferFree(&out);
179460 }
@@ -179464,18 +180283,18 @@
179464 Fts5Index *p, /* Index to read from */
179465 int bDesc, /* True for "ORDER BY rowid DESC" */
179466 const u8 *pToken, /* Buffer containing prefix to match */
179467 int nToken, /* Size of buffer pToken in bytes */
179468 Fts5Colset *pColset, /* Restrict matches to these columns */
179469 Fts5IndexIter **ppIter /* OUT: New iterator */
179470 ){
179471 Fts5Structure *pStruct;
179472 Fts5Buffer *aBuf;
179473 const int nBuf = 32;
179474
179475 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
179476 int (*xAppend)(Fts5Index*, i64, Fts5IndexIter*, Fts5Colset*, Fts5Buffer*);
179477 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
179478 xMerge = fts5MergeRowidLists;
179479 xAppend = fts5AppendRowid;
179480 }else{
179481 xMerge = fts5MergePrefixLists;
@@ -179484,32 +180303,40 @@
179484
179485 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
179486 pStruct = fts5StructureRead(p);
179487
179488 if( aBuf && pStruct ){
179489 const int flags = FTS5INDEX_QUERY_SCAN;
 
 
179490 int i;
179491 i64 iLastRowid = 0;
179492 Fts5IndexIter *p1 = 0; /* Iterator used to gather data from index */
179493 Fts5Data *pData;
179494 Fts5Buffer doclist;
179495 int bNewTerm = 1;
179496
179497 memset(&doclist, 0, sizeof(doclist));
179498 for(fts5MultiIterNew(p, pStruct, 1, flags, pToken, nToken, -1, 0, &p1);
 
 
179499 fts5MultiIterEof(p, p1)==0;
179500 fts5MultiIterNext2(p, p1, &bNewTerm)
179501 ){
179502 i64 iRowid = fts5MultiIterRowid(p1);
179503 int nTerm;
179504 const u8 *pTerm = fts5MultiIterTerm(p1, &nTerm);
 
 
179505 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
179506 if( bNewTerm ){
179507 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
179508 }
179509
179510 if( doclist.n>0 && iRowid<=iLastRowid ){
 
 
179511 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
179512 assert( i<nBuf );
179513 if( aBuf[i].n==0 ){
179514 fts5BufferSwap(&doclist, &aBuf[i]);
179515 fts5BufferZero(&doclist);
@@ -179519,22 +180346,21 @@
179519 }
179520 }
179521 iLastRowid = 0;
179522 }
179523
179524 if( !xAppend(p, iRowid-iLastRowid, p1, pColset, &doclist) ){
179525 iLastRowid = iRowid;
179526 }
179527 }
179528
179529 for(i=0; i<nBuf; i++){
179530 if( p->rc==SQLITE_OK ){
179531 xMerge(p, &doclist, &aBuf[i]);
179532 }
179533 fts5BufferFree(&aBuf[i]);
179534 }
179535 fts5MultiIterFree(p, p1);
179536
179537 pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n);
179538 if( pData ){
179539 pData->p = (u8*)&pData[1];
179540 pData->nn = pData->szLeaf = doclist.n;
@@ -179591,11 +180417,11 @@
179591 ** records must be invalidated.
179592 */
179593 static int sqlite3Fts5IndexRollback(Fts5Index *p){
179594 fts5CloseReader(p);
179595 fts5IndexDiscardData(p);
179596 assert( p->rc==SQLITE_OK );
179597 return SQLITE_OK;
179598 }
179599
179600 /*
179601 ** The %_data table is completely empty when this function is called. This
@@ -179763,26 +180589,31 @@
179763 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
179764 Fts5Colset *pColset, /* Match these columns only */
179765 Fts5IndexIter **ppIter /* OUT: New iterator object */
179766 ){
179767 Fts5Config *pConfig = p->pConfig;
179768 Fts5IndexIter *pRet = 0;
179769 int iIdx = 0;
179770 Fts5Buffer buf = {0, 0, 0};
179771
179772 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
179773 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
179774
179775 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
 
179776 memcpy(&buf.p[1], pToken, nToken);
179777
179778 #ifdef SQLITE_DEBUG
179779 /* If the QUERY_TEST_NOIDX flag was specified, then this must be a
 
 
 
 
179780 ** prefix-query. Instead of using a prefix-index (if one exists),
179781 ** evaluate the prefix query using the main FTS index. This is used
179782 ** for internal sanity checking by the integrity-check in debug
179783 ** mode only. */
 
179784 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
179785 assert( flags & FTS5INDEX_QUERY_PREFIX );
179786 iIdx = 1+pConfig->nPrefix;
179787 }else
179788 #endif
@@ -179792,54 +180623,62 @@
179792 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
179793 }
179794 }
179795
179796 if( iIdx<=pConfig->nPrefix ){
 
179797 Fts5Structure *pStruct = fts5StructureRead(p);
179798 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
179799 if( pStruct ){
179800 fts5MultiIterNew(p, pStruct, 1, flags, buf.p, nToken+1, -1, 0, &pRet);
 
 
179801 fts5StructureRelease(pStruct);
179802 }
179803 }else{
 
179804 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
179805 buf.p[0] = FTS5_MAIN_PREFIX;
179806 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
 
 
 
 
 
 
179807 }
179808
179809 if( p->rc ){
179810 sqlite3Fts5IterClose(pRet);
179811 pRet = 0;
179812 fts5CloseReader(p);
179813 }
179814 *ppIter = pRet;
 
179815 sqlite3Fts5BufferFree(&buf);
179816 }
179817 return fts5IndexReturn(p);
179818 }
179819
179820 /*
179821 ** Return true if the iterator passed as the only argument is at EOF.
179822 */
179823 static int sqlite3Fts5IterEof(Fts5IndexIter *pIter){
179824 assert( pIter->pIndex->rc==SQLITE_OK );
179825 return pIter->bEof;
179826 }
179827
179828 /*
179829 ** Move to the next matching rowid.
179830 */
179831 static int sqlite3Fts5IterNext(Fts5IndexIter *pIter){
 
179832 assert( pIter->pIndex->rc==SQLITE_OK );
179833 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
179834 return fts5IndexReturn(pIter->pIndex);
179835 }
179836
179837 /*
179838 ** Move to the next matching term/rowid. Used by the fts5vocab module.
179839 */
179840 static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIter){
 
179841 Fts5Index *p = pIter->pIndex;
179842
179843 assert( pIter->pIndex->rc==SQLITE_OK );
179844
179845 fts5MultiIterNext(p, pIter, 0, 0);
@@ -179846,11 +180685,11 @@
179846 if( p->rc==SQLITE_OK ){
179847 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179848 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
179849 fts5DataRelease(pSeg->pLeaf);
179850 pSeg->pLeaf = 0;
179851 pIter->bEof = 1;
179852 }
179853 }
179854
179855 return fts5IndexReturn(pIter->pIndex);
179856 }
@@ -179858,135 +180697,34 @@
179858 /*
179859 ** Move to the next matching rowid that occurs at or after iMatch. The
179860 ** definition of "at or after" depends on whether this iterator iterates
179861 ** in ascending or descending rowid order.
179862 */
179863 static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIter, i64 iMatch){
 
179864 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
179865 return fts5IndexReturn(pIter->pIndex);
179866 }
179867
179868 /*
179869 ** Return the current rowid.
179870 */
179871 static i64 sqlite3Fts5IterRowid(Fts5IndexIter *pIter){
179872 return fts5MultiIterRowid(pIter);
179873 }
179874
179875 /*
179876 ** Return the current term.
179877 */
179878 static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIter, int *pn){
179879 int n;
179880 const char *z = (const char*)fts5MultiIterTerm(pIter, &n);
179881 *pn = n-1;
179882 return &z[1];
179883 }
179884
179885
179886 static int fts5IndexExtractColset (
179887 Fts5Colset *pColset, /* Colset to filter on */
179888 const u8 *pPos, int nPos, /* Position list */
179889 Fts5Buffer *pBuf /* Output buffer */
179890 ){
179891 int rc = SQLITE_OK;
179892 int i;
179893
179894 fts5BufferZero(pBuf);
179895 for(i=0; i<pColset->nCol; i++){
179896 const u8 *pSub = pPos;
179897 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
179898 if( nSub ){
179899 fts5BufferAppendBlob(&rc, pBuf, nSub, pSub);
179900 }
179901 }
179902 return rc;
179903 }
179904
179905
179906 /*
179907 ** Return a pointer to a buffer containing a copy of the position list for
179908 ** the current entry. Output variable *pn is set to the size of the buffer
179909 ** in bytes before returning.
179910 **
179911 ** The returned position list does not include the "number of bytes" varint
179912 ** field that starts the position list on disk.
179913 */
179914 static int sqlite3Fts5IterPoslist(
179915 Fts5IndexIter *pIter,
179916 Fts5Colset *pColset, /* Column filter (or NULL) */
179917 const u8 **pp, /* OUT: Pointer to position-list data */
179918 int *pn, /* OUT: Size of position-list in bytes */
179919 i64 *piRowid /* OUT: Current rowid */
179920 ){
179921 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179922 int eDetail = pIter->pIndex->pConfig->eDetail;
179923
179924 assert( pIter->pIndex->rc==SQLITE_OK );
179925 *piRowid = pSeg->iRowid;
179926 if( eDetail==FTS5_DETAIL_NONE ){
179927 *pn = pSeg->nPos;
179928 }else
179929 if( eDetail==FTS5_DETAIL_FULL
179930 && pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf
179931 ){
179932 u8 *pPos = &pSeg->pLeaf->p[pSeg->iLeafOffset];
179933 if( pColset==0 || pIter->bFiltered ){
179934 *pn = pSeg->nPos;
179935 *pp = pPos;
179936 }else if( pColset->nCol==1 ){
179937 *pp = pPos;
179938 *pn = fts5IndexExtractCol(pp, pSeg->nPos, pColset->aiCol[0]);
179939 }else{
179940 fts5BufferZero(&pIter->poslist);
179941 fts5IndexExtractColset(pColset, pPos, pSeg->nPos, &pIter->poslist);
179942 *pp = pIter->poslist.p;
179943 *pn = pIter->poslist.n;
179944 }
179945 }else{
179946 fts5BufferZero(&pIter->poslist);
179947 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
179948 if( eDetail==FTS5_DETAIL_FULL ){
179949 *pp = pIter->poslist.p;
179950 }
179951 *pn = pIter->poslist.n;
179952 }
179953 return fts5IndexReturn(pIter->pIndex);
179954 }
179955
179956 static int sqlite3Fts5IterCollist(
179957 Fts5IndexIter *pIter,
179958 const u8 **pp, /* OUT: Pointer to position-list data */
179959 int *pn /* OUT: Size of position-list in bytes */
179960 ){
179961 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
179962 *pp = pIter->poslist.p;
179963 *pn = pIter->poslist.n;
179964 return SQLITE_OK;
179965 }
179966
179967 /*
179968 ** This function is similar to sqlite3Fts5IterPoslist(), except that it
179969 ** copies the position list into the buffer supplied as the second
179970 ** argument.
179971 */
179972 static int sqlite3Fts5IterPoslistBuffer(Fts5IndexIter *pIter, Fts5Buffer *pBuf){
179973 Fts5Index *p = pIter->pIndex;
179974 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179975 assert( p->rc==SQLITE_OK );
179976 fts5BufferZero(pBuf);
179977 fts5SegiterPoslist(p, pSeg, 0, pBuf);
179978 return fts5IndexReturn(p);
179979 }
179980
179981 /*
179982 ** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
179983 */
179984 static void sqlite3Fts5IterClose(Fts5IndexIter *pIter){
179985 if( pIter ){
 
179986 Fts5Index *pIndex = pIter->pIndex;
179987 fts5MultiIterFree(pIter->pIndex, pIter);
179988 fts5CloseReader(pIndex);
179989 }
179990 }
179991
179992 /*
@@ -180147,39 +180885,34 @@
180147 int flags, /* Flags for Fts5IndexQuery */
180148 u64 *pCksum /* IN/OUT: Checksum value */
180149 ){
180150 int eDetail = p->pConfig->eDetail;
180151 u64 cksum = *pCksum;
180152 Fts5IndexIter *pIdxIter = 0;
180153 Fts5Buffer buf = {0, 0, 0};
180154 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIdxIter);
180155
180156 while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIdxIter) ){
180157 i64 rowid = sqlite3Fts5IterRowid(pIdxIter);
180158
180159 if( eDetail==FTS5_DETAIL_NONE ){
180160 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
180161 }else{
180162 rc = sqlite3Fts5IterPoslistBuffer(pIdxIter, &buf);
180163 if( rc==SQLITE_OK ){
180164 Fts5PoslistReader sReader;
180165 for(sqlite3Fts5PoslistReaderInit(buf.p, buf.n, &sReader);
180166 sReader.bEof==0;
180167 sqlite3Fts5PoslistReaderNext(&sReader)
180168 ){
180169 int iCol = FTS5_POS2COLUMN(sReader.iPos);
180170 int iOff = FTS5_POS2OFFSET(sReader.iPos);
180171 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
180172 }
180173 }
180174 }
180175 if( rc==SQLITE_OK ){
180176 rc = sqlite3Fts5IterNext(pIdxIter);
180177 }
180178 }
180179 sqlite3Fts5IterClose(pIdxIter);
180180 fts5BufferFree(&buf);
180181
180182 *pCksum = cksum;
180183 return rc;
180184 }
180185
@@ -180480,18 +181213,19 @@
180480 */
180481 static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
180482 int eDetail = p->pConfig->eDetail;
180483 u64 cksum2 = 0; /* Checksum based on contents of indexes */
180484 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
180485 Fts5IndexIter *pIter; /* Used to iterate through entire index */
180486 Fts5Structure *pStruct; /* Index structure */
180487
180488 #ifdef SQLITE_DEBUG
180489 /* Used by extra internal tests only run if NDEBUG is not defined */
180490 u64 cksum3 = 0; /* Checksum based on contents of indexes */
180491 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
180492 #endif
 
180493
180494 /* Load the FTS index structure */
180495 pStruct = fts5StructureRead(p);
180496
180497 /* Check that the internal nodes of each segment match the leaves */
@@ -180516,11 +181250,11 @@
180516 **
180517 ** As each term visited by the linear scans, a separate query for the
180518 ** same term is performed. cksum3 is calculated based on the entries
180519 ** extracted by these queries.
180520 */
180521 for(fts5MultiIterNew(p, pStruct, 0, 0, 0, 0, -1, 0, &pIter);
180522 fts5MultiIterEof(p, pIter)==0;
180523 fts5MultiIterNext(p, pIter, 0, 0)
180524 ){
180525 int n; /* Size of term in bytes */
180526 i64 iPos = 0; /* Position read from poslist */
@@ -180545,11 +181279,11 @@
180545 }
180546 }
180547 }
180548 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
180549
180550 fts5MultiIterFree(p, pIter);
180551 if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
180552
180553 fts5StructureRelease(pStruct);
180554 #ifdef SQLITE_DEBUG
180555 fts5BufferFree(&term);
@@ -180782,10 +181516,11 @@
180782 int rc = SQLITE_OK; /* Return code */
180783 int nSpace = 0;
180784 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
180785
180786 assert( nArg==2 );
 
180787 memset(&s, 0, sizeof(Fts5Buffer));
180788 iRowid = sqlite3_value_int64(apVal[0]);
180789
180790 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
180791 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
@@ -181234,14 +181969,14 @@
181234 #define FTS5_BI_ORDER_DESC 0x0080
181235
181236 /*
181237 ** Values for Fts5Cursor.csrflags
181238 */
181239 #define FTS5CSR_REQUIRE_CONTENT 0x01
181240 #define FTS5CSR_REQUIRE_DOCSIZE 0x02
181241 #define FTS5CSR_REQUIRE_INST 0x04
181242 #define FTS5CSR_EOF 0x08
181243 #define FTS5CSR_FREE_ZRANK 0x10
181244 #define FTS5CSR_REQUIRE_RESEEK 0x20
181245 #define FTS5CSR_REQUIRE_POSLIST 0x40
181246
181247 #define BitFlagAllTest(x,y) (((x) & (y))==(y))
@@ -181552,11 +182287,11 @@
181552
181553 /* Set idxFlags flags for all WHERE clause terms that will be used. */
181554 for(i=0; i<pInfo->nConstraint; i++){
181555 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
181556 int j;
181557 for(j=0; j<(int)ArraySize(aConstraint); j++){
181558 struct Constraint *pC = &aConstraint[j];
181559 if( p->iColumn==aColMap[pC->iCol] && p->op & pC->op ){
181560 if( p->usable ){
181561 pC->iConsIndex = i;
181562 idxFlags |= pC->fts5op;
@@ -181599,11 +182334,11 @@
181599 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
181600 }
181601
181602 /* Assign argvIndex values to each constraint in use. */
181603 iNext = 1;
181604 for(i=0; i<(int)ArraySize(aConstraint); i++){
181605 struct Constraint *pC = &aConstraint[i];
181606 if( pC->iConsIndex>=0 ){
181607 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
181608 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
181609 }
@@ -181792,18 +182527,19 @@
181792 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
181793 int bDesc = pCsr->bDesc;
181794 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
181795
181796 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc);
181797 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
181798 *pbSkip = 1;
181799 }
181800
181801 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
181802 fts5CsrNewrow(pCsr);
181803 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
181804 CsrFlagSet(pCsr, FTS5CSR_EOF);
 
181805 }
181806 }
181807 return rc;
181808 }
181809
@@ -181816,28 +182552,28 @@
181816 ** even if we reach end-of-file. The fts5EofMethod() will be called
181817 ** subsequently to determine whether or not an EOF was hit.
181818 */
181819 static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
181820 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
181821 int rc = SQLITE_OK;
181822
181823 assert( (pCsr->ePlan<3)==
181824 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
181825 );
 
181826
181827 if( pCsr->ePlan<3 ){
181828 int bSkip = 0;
181829 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
181830 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
181831 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
181832 CsrFlagSet(pCsr, FTS5CSR_EOF);
181833 }
181834 fts5CsrNewrow(pCsr);
181835 }else{
181836 switch( pCsr->ePlan ){
181837 case FTS5_PLAN_SPECIAL: {
181838 CsrFlagSet(pCsr, FTS5CSR_EOF);
 
181839 break;
181840 }
181841
181842 case FTS5_PLAN_SORTED_MATCH: {
181843 rc = fts5SorterNext(pCsr);
@@ -182109,11 +182845,11 @@
182109 ** 3. A full-table scan.
182110 */
182111 static int fts5FilterMethod(
182112 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
182113 int idxNum, /* Strategy index */
182114 const char *idxStr, /* Unused */
182115 int nVal, /* Number of elements in apVal */
182116 sqlite3_value **apVal /* Arguments for the indexing scheme */
182117 ){
182118 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
182119 Fts5Config *pConfig = pTab->pConfig;
@@ -182126,10 +182862,13 @@
182126 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
182127 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
182128 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
182129 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
182130 char **pzErrmsg = pConfig->pzErrmsg;
 
 
 
182131
182132 if( pCsr->ePlan ){
182133 fts5FreeCursorComponents(pCsr);
182134 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
182135 }
@@ -182411,12 +183150,11 @@
182411 return rc;
182412 }
182413
182414 static int fts5SpecialDelete(
182415 Fts5Table *pTab,
182416 sqlite3_value **apVal,
182417 sqlite3_int64 *piRowid
182418 ){
182419 int rc = SQLITE_OK;
182420 int eType1 = sqlite3_value_type(apVal[1]);
182421 if( eType1==SQLITE_INTEGER ){
182422 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
@@ -182488,11 +183226,11 @@
182488 /* A "special" INSERT op. These are handled separately. */
182489 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
182490 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
182491 && 0==sqlite3_stricmp("delete", z)
182492 ){
182493 rc = fts5SpecialDelete(pTab, apVal, pRowid);
182494 }else{
182495 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
182496 }
182497 }else{
182498 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
@@ -182589,10 +183327,11 @@
182589
182590 /*
182591 ** Implementation of xBegin() method.
182592 */
182593 static int fts5BeginMethod(sqlite3_vtab *pVtab){
 
182594 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0);
182595 return SQLITE_OK;
182596 }
182597
182598 /*
@@ -182599,10 +183338,11 @@
182599 ** Implementation of xCommit() method. This is a no-op. The contents of
182600 ** the pending-terms hash-table have already been flushed into the database
182601 ** by fts5SyncMethod().
182602 */
182603 static int fts5CommitMethod(sqlite3_vtab *pVtab){
 
182604 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0);
182605 return SQLITE_OK;
182606 }
182607
182608 /*
@@ -182852,16 +183592,18 @@
182852 }
182853
182854 static int fts5ColumnSizeCb(
182855 void *pContext, /* Pointer to int */
182856 int tflags,
182857 const char *pToken, /* Buffer containing token */
182858 int nToken, /* Size of token in bytes */
182859 int iStart, /* Start offset of token */
182860 int iEnd /* End offset of token */
182861 ){
182862 int *pCnt = (int*)pContext;
 
 
182863 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
182864 (*pCnt)++;
182865 }
182866 return SQLITE_OK;
182867 }
@@ -182973,14 +183715,15 @@
182973
182974 return pRet;
182975 }
182976
182977 static void fts5ApiPhraseNext(
182978 Fts5Context *pCtx,
182979 Fts5PhraseIter *pIter,
182980 int *piCol, int *piOff
182981 ){
 
182982 if( pIter->a>=pIter->b ){
182983 *piCol = -1;
182984 *piOff = -1;
182985 }else{
182986 int iVal;
@@ -183128,16 +183871,15 @@
183128 int rc;
183129 Fts5Cursor *pNew = 0;
183130
183131 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
183132 if( rc==SQLITE_OK ){
183133 Fts5Config *pConf = pTab->pConfig;
183134 pNew->ePlan = FTS5_PLAN_MATCH;
183135 pNew->iFirstRowid = SMALLEST_INT64;
183136 pNew->iLastRowid = LARGEST_INT64;
183137 pNew->base.pVtab = (sqlite3_vtab*)pTab;
183138 rc = sqlite3Fts5ExprClonePhrase(pConf, pCsr->pExpr, iPhrase, &pNew->pExpr);
183139 }
183140
183141 if( rc==SQLITE_OK ){
183142 for(rc = fts5CursorFirst(pTab, pNew, 0);
183143 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
@@ -183346,18 +184088,19 @@
183346 ** This routine implements the xFindFunction method for the FTS3
183347 ** virtual table.
183348 */
183349 static int fts5FindFunctionMethod(
183350 sqlite3_vtab *pVtab, /* Virtual table handle */
183351 int nArg, /* Number of SQL function arguments */
183352 const char *zName, /* Name of SQL function */
183353 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
183354 void **ppArg /* OUT: User data for *pxFunc */
183355 ){
183356 Fts5Table *pTab = (Fts5Table*)pVtab;
183357 Fts5Auxiliary *pAux;
183358
 
183359 pAux = fts5FindAuxiliary(pTab, zName);
183360 if( pAux ){
183361 *pxFunc = fts5ApiCallback;
183362 *ppArg = (void*)pAux;
183363 return 1;
@@ -183383,10 +184126,11 @@
183383 **
183384 ** Flush the contents of the pending-terms table to disk.
183385 */
183386 static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
183387 Fts5Table *pTab = (Fts5Table*)pVtab;
 
183388 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
183389 fts5TripCursors(pTab);
183390 return sqlite3Fts5StorageSync(pTab->pStorage, 0);
183391 }
183392
@@ -183395,10 +184139,11 @@
183395 **
183396 ** This is a no-op.
183397 */
183398 static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
183399 Fts5Table *pTab = (Fts5Table*)pVtab;
 
183400 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
183401 fts5TripCursors(pTab);
183402 return sqlite3Fts5StorageSync(pTab->pStorage, 0);
183403 }
183404
@@ -183407,10 +184152,11 @@
183407 **
183408 ** Discard the contents of the pending terms table.
183409 */
183410 static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
183411 Fts5Table *pTab = (Fts5Table*)pVtab;
 
183412 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
183413 fts5TripCursors(pTab);
183414 return sqlite3Fts5StorageRollback(pTab->pStorage);
183415 }
183416
@@ -183586,14 +184332,15 @@
183586 }
183587
183588 static void fts5Fts5Func(
183589 sqlite3_context *pCtx, /* Function call context */
183590 int nArg, /* Number of args */
183591 sqlite3_value **apVal /* Function arguments */
183592 ){
183593 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
183594 char buf[8];
 
183595 assert( nArg==0 );
183596 assert( sizeof(buf)>=sizeof(pGlobal) );
183597 memcpy(buf, (void*)&pGlobal, sizeof(pGlobal));
183598 sqlite3_result_blob(pCtx, buf, sizeof(pGlobal), SQLITE_TRANSIENT);
183599 }
@@ -183602,14 +184349,15 @@
183602 ** Implementation of fts5_source_id() function.
183603 */
183604 static void fts5SourceIdFunc(
183605 sqlite3_context *pCtx, /* Function call context */
183606 int nArg, /* Number of args */
183607 sqlite3_value **apVal /* Function arguments */
183608 ){
183609 assert( nArg==0 );
183610 sqlite3_result_text(pCtx, "fts5: 2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1", -1, SQLITE_TRANSIENT);
 
183611 }
183612
183613 static int fts5Init(sqlite3 *db){
183614 static const sqlite3_module fts5Mod = {
183615 /* iVersion */ 2,
@@ -184050,11 +184798,11 @@
184050 int rc = SQLITE_OK;
184051 if( p ){
184052 int i;
184053
184054 /* Finalize all SQL statements */
184055 for(i=0; i<(int)ArraySize(p->aStmt); i++){
184056 sqlite3_finalize(p->aStmt[i]);
184057 }
184058
184059 sqlite3_free(p);
184060 }
@@ -184074,15 +184822,16 @@
184074 static int fts5StorageInsertCallback(
184075 void *pContext, /* Pointer to Fts5InsertCtx object */
184076 int tflags,
184077 const char *pToken, /* Buffer containing token */
184078 int nToken, /* Size of token in bytes */
184079 int iStart, /* Start offset of token */
184080 int iEnd /* End offset of token */
184081 ){
184082 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
184083 Fts5Index *pIdx = pCtx->pStorage->pIndex;
 
184084 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
184085 pCtx->szCol++;
184086 }
184087 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
184088 }
@@ -184509,20 +185258,22 @@
184509 static int fts5StorageIntegrityCallback(
184510 void *pContext, /* Pointer to Fts5IntegrityCtx object */
184511 int tflags,
184512 const char *pToken, /* Buffer containing token */
184513 int nToken, /* Size of token in bytes */
184514 int iStart, /* Start offset of token */
184515 int iEnd /* End offset of token */
184516 ){
184517 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
184518 Fts5Termset *pTermset = pCtx->pTermset;
184519 int bPresent;
184520 int ii;
184521 int rc = SQLITE_OK;
184522 int iPos;
184523 int iCol;
 
 
184524
184525 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
184526 pCtx->szCol++;
184527 }
184528
@@ -184897,16 +185648,17 @@
184897
184898 /*
184899 ** Create an "ascii" tokenizer.
184900 */
184901 static int fts5AsciiCreate(
184902 void *pCtx,
184903 const char **azArg, int nArg,
184904 Fts5Tokenizer **ppOut
184905 ){
184906 int rc = SQLITE_OK;
184907 AsciiTokenizer *p = 0;
 
184908 if( nArg%2 ){
184909 rc = SQLITE_ERROR;
184910 }else{
184911 p = sqlite3_malloc(sizeof(AsciiTokenizer));
184912 if( p==0 ){
@@ -184951,11 +185703,11 @@
184951 ** Tokenize some text using the ascii tokenizer.
184952 */
184953 static int fts5AsciiTokenize(
184954 Fts5Tokenizer *pTokenizer,
184955 void *pCtx,
184956 int flags,
184957 const char *pText, int nText,
184958 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
184959 ){
184960 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
184961 int rc = SQLITE_OK;
@@ -184964,10 +185716,12 @@
184964
184965 char aFold[64];
184966 int nFold = sizeof(aFold);
184967 char *pFold = aFold;
184968 unsigned char *a = p->aTokenChar;
 
 
184969
184970 while( is<nText && rc==SQLITE_OK ){
184971 int nByte;
184972
184973 /* Skip any leading divider characters. */
@@ -185158,16 +185912,18 @@
185158
185159 /*
185160 ** Create a "unicode61" tokenizer.
185161 */
185162 static int fts5UnicodeCreate(
185163 void *pCtx,
185164 const char **azArg, int nArg,
185165 Fts5Tokenizer **ppOut
185166 ){
185167 int rc = SQLITE_OK; /* Return code */
185168 Unicode61Tokenizer *p = 0; /* New tokenizer object */
 
 
185169
185170 if( nArg%2 ){
185171 rc = SQLITE_ERROR;
185172 }else{
185173 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
@@ -185221,11 +185977,11 @@
185221 }
185222
185223 static int fts5UnicodeTokenize(
185224 Fts5Tokenizer *pTokenizer,
185225 void *pCtx,
185226 int flags,
185227 const char *pText, int nText,
185228 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
185229 ){
185230 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
185231 int rc = SQLITE_OK;
@@ -185236,10 +185992,12 @@
185236
185237 /* Output buffer */
185238 char *aFold = p->aFold;
185239 int nFold = p->nFold;
185240 const char *pEnd = &aFold[nFold-6];
 
 
185241
185242 /* Each iteration of this loop gobbles up a contiguous run of separators,
185243 ** then the next token. */
185244 while( rc==SQLITE_OK ){
185245 int iCode; /* non-ASCII codepoint read from input */
@@ -186055,11 +186813,11 @@
186055 };
186056
186057 int rc = SQLITE_OK; /* Return code */
186058 int i; /* To iterate through builtin functions */
186059
186060 for(i=0; rc==SQLITE_OK && i<(int)ArraySize(aBuiltin); i++){
186061 rc = pApi->xCreateTokenizer(pApi,
186062 aBuiltin[i].zName,
186063 (void*)pApi,
186064 &aBuiltin[i].x,
186065 0
@@ -186196,13 +186954,13 @@
186196 };
186197 static const unsigned int aAscii[4] = {
186198 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
186199 };
186200
186201 if( c<128 ){
186202 return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
186203 }else if( c<(1<<22) ){
186204 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
186205 int iRes = 0;
186206 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
186207 int iLo = 0;
186208 while( iHi>=iLo ){
@@ -186765,11 +187523,14 @@
186765 return fts5PutVarint64(p,v);
186766 }
186767
186768
186769 static int sqlite3Fts5GetVarintLen(u32 iVal){
 
186770 if( iVal<(1 << 7 ) ) return 1;
 
 
186771 if( iVal<(1 << 14) ) return 2;
186772 if( iVal<(1 << 21) ) return 3;
186773 if( iVal<(1 << 28) ) return 4;
186774 return 5;
186775 }
@@ -186959,11 +187720,11 @@
186959 int nTab = (int)strlen(zTab)+1;
186960 int eType = 0;
186961
186962 rc = fts5VocabTableType(zType, pzErr, &eType);
186963 if( rc==SQLITE_OK ){
186964 assert( eType>=0 && eType<sizeof(azSchema)/sizeof(azSchema[0]) );
186965 rc = sqlite3_declare_vtab(db, azSchema[eType]);
186966 }
186967
186968 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
186969 pRet = sqlite3Fts5MallocZero(&rc, nByte);
@@ -187012,19 +187773,21 @@
187012
187013 /*
187014 ** Implementation of the xBestIndex method.
187015 */
187016 static int fts5VocabBestIndexMethod(
187017 sqlite3_vtab *pVTab,
187018 sqlite3_index_info *pInfo
187019 ){
187020 int i;
187021 int iTermEq = -1;
187022 int iTermGe = -1;
187023 int iTermLe = -1;
187024 int idxNum = 0;
187025 int nArg = 0;
 
 
187026
187027 for(i=0; i<pInfo->nConstraint; i++){
187028 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
187029 if( p->usable==0 ) continue;
187030 if( p->iColumn==0 ){ /* term column */
@@ -187182,59 +187945,54 @@
187182 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
187183 pCsr->iCol = 0;
187184
187185 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
187186 while( rc==SQLITE_OK ){
187187 i64 dummy;
187188 const u8 *pPos; int nPos; /* Position list */
187189 i64 iPos = 0; /* 64-bit position read from poslist */
187190 int iOff = 0; /* Current offset within position list */
187191
 
 
187192 switch( pCsr->pConfig->eDetail ){
187193 case FTS5_DETAIL_FULL:
187194 rc = sqlite3Fts5IterPoslist(pCsr->pIter, 0, &pPos, &nPos, &dummy);
187195 if( rc==SQLITE_OK ){
187196 if( pTab->eType==FTS5_VOCAB_ROW ){
187197 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187198 pCsr->aCnt[0]++;
187199 }
187200 pCsr->aDoc[0]++;
187201 }else{
187202 int iCol = -1;
187203 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187204 int ii = FTS5_POS2COLUMN(iPos);
187205 pCsr->aCnt[ii]++;
187206 if( iCol!=ii ){
187207 if( ii>=nCol ){
187208 rc = FTS5_CORRUPT;
187209 break;
187210 }
187211 pCsr->aDoc[ii]++;
187212 iCol = ii;
187213 }
187214 }
187215 }
187216 }
187217 break;
187218
187219 case FTS5_DETAIL_COLUMNS:
187220 if( pTab->eType==FTS5_VOCAB_ROW ){
187221 pCsr->aDoc[0]++;
187222 }else{
187223 Fts5Buffer buf = {0, 0, 0};
187224 rc = sqlite3Fts5IterPoslistBuffer(pCsr->pIter, &buf);
187225 if( rc==SQLITE_OK ){
187226 while( 0==sqlite3Fts5PoslistNext64(buf.p, buf.n, &iOff,&iPos) ){
187227 assert_nc( iPos>=0 && iPos<nCol );
187228 if( iPos>=nCol ){
187229 rc = FTS5_CORRUPT;
187230 break;
187231 }
187232 pCsr->aDoc[iPos]++;
187233 }
187234 }
187235 sqlite3Fts5BufferFree(&buf);
187236 }
187237 break;
187238
187239 default:
187240 assert( pCsr->pConfig->eDetail==FTS5_DETAIL_NONE );
@@ -187268,12 +188026,12 @@
187268 ** This is the xFilter implementation for the virtual table.
187269 */
187270 static int fts5VocabFilterMethod(
187271 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
187272 int idxNum, /* Strategy index */
187273 const char *idxStr, /* Unused */
187274 int nVal, /* Number of elements in apVal */
187275 sqlite3_value **apVal /* Arguments for the indexing scheme */
187276 ){
187277 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
187278 int rc = SQLITE_OK;
187279
@@ -187283,10 +188041,12 @@
187283 int nTerm = 0;
187284
187285 sqlite3_value *pEq = 0;
187286 sqlite3_value *pGe = 0;
187287 sqlite3_value *pLe = 0;
 
 
187288
187289 fts5VocabResetCursor(pCsr);
187290 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
187291 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
187292 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
187293
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -121,10 +121,11 @@
121 #else
122 /* This is not VxWorks. */
123 #define OS_VXWORKS 0
124 #define HAVE_FCHOWN 1
125 #define HAVE_READLINK 1
126 #define HAVE_LSTAT 1
127 #endif /* defined(_WRS_KERNEL) */
128
129 /************** End of vxworks.h *********************************************/
130 /************** Continuing where we left off in sqliteInt.h ******************/
131
@@ -327,11 +328,11 @@
328 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
329 ** [sqlite_version()] and [sqlite_source_id()].
330 */
331 #define SQLITE_VERSION "3.11.0"
332 #define SQLITE_VERSION_NUMBER 3011000
333 #define SQLITE_SOURCE_ID "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
334
335 /*
336 ** CAPI3REF: Run-Time Library Version Numbers
337 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
338 **
@@ -561,11 +562,11 @@
562 ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
563 ** is not NULL then any error message is written into memory obtained
564 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
565 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
566 ** on error message strings returned through the 5th parameter of
567 ** sqlite3_exec() after the error message string is no longer needed.
568 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
569 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
570 ** NULL before returning.
571 **
572 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5911,11 +5912,11 @@
5912 */
5913 struct sqlite3_index_info {
5914 /* Inputs */
5915 int nConstraint; /* Number of entries in aConstraint */
5916 struct sqlite3_index_constraint {
5917 int iColumn; /* Column constrained. -1 for ROWID */
5918 unsigned char op; /* Constraint operator */
5919 unsigned char usable; /* True if this constraint is usable */
5920 int iTermOffset; /* Used internally - xBestIndex should ignore */
5921 } *aConstraint; /* Table of WHERE clause constraints */
5922 int nOrderBy; /* Number of terms in the ORDER BY clause */
@@ -10468,18 +10469,28 @@
10469
10470 /*
10471 ** Flags passed as the third argument to sqlite3BtreeCursor().
10472 **
10473 ** For read-only cursors the wrFlag argument is always zero. For read-write
10474 ** cursors it may be set to either (BTREE_WRCSR|BTREE_FORDELETE) or just
10475 ** (BTREE_WRCSR). If the BTREE_FORDELETE bit is set, then the cursor will
10476 ** only be used by SQLite for the following:
10477 **
10478 ** * to seek to and then delete specific entries, and/or
10479 **
10480 ** * to read values that will be used to create keys that other
10481 ** BTREE_FORDELETE cursors will seek to and delete.
10482 **
10483 ** The BTREE_FORDELETE flag is an optimization hint. It is not used by
10484 ** by this, the native b-tree engine of SQLite, but it is available to
10485 ** alternative storage engines that might be substituted in place of this
10486 ** b-tree system. For alternative storage engines in which a delete of
10487 ** the main table row automatically deletes corresponding index rows,
10488 ** the FORDELETE flag hint allows those alternative storage engines to
10489 ** skip a lot of work. Namely: FORDELETE cursors may treat all SEEK
10490 ** and DELETE operations as no-ops, and any READ operation against a
10491 ** FORDELETE cursor may return a null row: 0x01 0x00.
10492 */
10493 #define BTREE_WRCSR 0x00000004 /* read-write cursor */
10494 #define BTREE_FORDELETE 0x00000008 /* Cursor is for seek/delete only */
10495
10496 SQLITE_PRIVATE int sqlite3BtreeCursor(
@@ -10504,11 +10515,16 @@
10515 int bias,
10516 int *pRes
10517 );
10518 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*);
10519 SQLITE_PRIVATE int sqlite3BtreeCursorRestore(BtCursor*, int*);
10520 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*, u8 flags);
10521
10522 /* Allowed flags for the 2nd argument to sqlite3BtreeDelete() */
10523 #define BTREE_SAVEPOSITION 0x02 /* Leave cursor pointing at NEXT or PREV */
10524 #define BTREE_AUXDELETE 0x04 /* not the primary delete operation */
10525
10526 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
10527 const void *pData, int nData,
10528 int nZero, int bias, int seekResult);
10529 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
10530 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
@@ -10762,94 +10778,95 @@
10778 */
10779 /************** Include opcodes.h in the middle of vdbe.h ********************/
10780 /************** Begin file opcodes.h *****************************************/
10781 /* Automatically generated. Do not edit */
10782 /* See the tool/mkopcodeh.tcl script for details */
10783 #define OP_Savepoint 0
10784 #define OP_AutoCommit 1
10785 #define OP_Transaction 2
10786 #define OP_SorterNext 3
10787 #define OP_PrevIfOpen 4
10788 #define OP_NextIfOpen 5
10789 #define OP_Prev 6
10790 #define OP_Next 7
10791 #define OP_Checkpoint 8
10792 #define OP_JournalMode 9
10793 #define OP_Vacuum 10
10794 #define OP_VFilter 11 /* synopsis: iplan=r[P3] zplan='P4' */
10795 #define OP_VUpdate 12 /* synopsis: data=r[P3@P2] */
10796 #define OP_Goto 13
10797 #define OP_Gosub 14
10798 #define OP_Return 15
10799 #define OP_InitCoroutine 16
10800 #define OP_EndCoroutine 17
10801 #define OP_Yield 18
10802 #define OP_Not 19 /* same as TK_NOT, synopsis: r[P2]= !r[P1] */
10803 #define OP_HaltIfNull 20 /* synopsis: if r[P3]=null halt */
10804 #define OP_Halt 21
10805 #define OP_Integer 22 /* synopsis: r[P2]=P1 */
10806 #define OP_Int64 23 /* synopsis: r[P2]=P4 */
10807 #define OP_String 24 /* synopsis: r[P2]='P4' (len=P1) */
10808 #define OP_Null 25 /* synopsis: r[P2..P3]=NULL */
10809 #define OP_SoftNull 26 /* synopsis: r[P1]=NULL */
10810 #define OP_Blob 27 /* synopsis: r[P2]=P4 (len=P1) */
10811 #define OP_Variable 28 /* synopsis: r[P2]=parameter(P1,P4) */
10812 #define OP_Move 29 /* synopsis: r[P2@P3]=r[P1@P3] */
10813 #define OP_Copy 30 /* synopsis: r[P2@P3+1]=r[P1@P3+1] */
10814 #define OP_SCopy 31 /* synopsis: r[P2]=r[P1] */
10815 #define OP_IntCopy 32 /* synopsis: r[P2]=r[P1] */
10816 #define OP_ResultRow 33 /* synopsis: output=r[P1@P2] */
10817 #define OP_CollSeq 34
10818 #define OP_Function0 35 /* synopsis: r[P3]=func(r[P2@P5]) */
10819 #define OP_Function 36 /* synopsis: r[P3]=func(r[P2@P5]) */
10820 #define OP_AddImm 37 /* synopsis: r[P1]=r[P1]+P2 */
10821 #define OP_MustBeInt 38
10822 #define OP_RealAffinity 39
10823 #define OP_Cast 40 /* synopsis: affinity(r[P1]) */
10824 #define OP_Permutation 41
10825 #define OP_Compare 42 /* synopsis: r[P1@P3] <-> r[P2@P3] */
10826 #define OP_Jump 43
10827 #define OP_Once 44
10828 #define OP_If 45
10829 #define OP_IfNot 46
10830 #define OP_Column 47 /* synopsis: r[P3]=PX */
10831 #define OP_Affinity 48 /* synopsis: affinity(r[P1@P2]) */
10832 #define OP_MakeRecord 49 /* synopsis: r[P3]=mkrec(r[P1@P2]) */
10833 #define OP_Count 50 /* synopsis: r[P2]=count() */
10834 #define OP_ReadCookie 51
10835 #define OP_SetCookie 52
10836 #define OP_ReopenIdx 53 /* synopsis: root=P2 iDb=P3 */
10837 #define OP_OpenRead 54 /* synopsis: root=P2 iDb=P3 */
10838 #define OP_OpenWrite 55 /* synopsis: root=P2 iDb=P3 */
10839 #define OP_OpenAutoindex 56 /* synopsis: nColumn=P2 */
10840 #define OP_OpenEphemeral 57 /* synopsis: nColumn=P2 */
10841 #define OP_SorterOpen 58
10842 #define OP_SequenceTest 59 /* synopsis: if( cursor[P1].ctr++ ) pc = P2 */
10843 #define OP_OpenPseudo 60 /* synopsis: P3 columns in r[P2] */
10844 #define OP_Close 61
10845 #define OP_ColumnsUsed 62
10846 #define OP_SeekLT 63 /* synopsis: key=r[P3@P4] */
10847 #define OP_SeekLE 64 /* synopsis: key=r[P3@P4] */
10848 #define OP_SeekGE 65 /* synopsis: key=r[P3@P4] */
10849 #define OP_SeekGT 66 /* synopsis: key=r[P3@P4] */
10850 #define OP_NoConflict 67 /* synopsis: key=r[P3@P4] */
10851 #define OP_NotFound 68 /* synopsis: key=r[P3@P4] */
10852 #define OP_Found 69 /* synopsis: key=r[P3@P4] */
10853 #define OP_NotExists 70 /* synopsis: intkey=r[P3] */
10854 #define OP_Or 71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
10855 #define OP_And 72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
10856 #define OP_Sequence 73 /* synopsis: r[P2]=cursor[P1].ctr++ */
10857 #define OP_NewRowid 74 /* synopsis: r[P2]=rowid */
10858 #define OP_Insert 75 /* synopsis: intkey=r[P3] data=r[P2] */
10859 #define OP_IsNull 76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
10860 #define OP_NotNull 77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
10861 #define OP_Ne 78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
10862 #define OP_Eq 79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
10863 #define OP_Gt 80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
10864 #define OP_Le 81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
10865 #define OP_Lt 82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
10866 #define OP_Ge 83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
10867 #define OP_InsertInt 84 /* synopsis: intkey=P3 data=r[P2] */
10868 #define OP_BitAnd 85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
10869 #define OP_BitOr 86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
10870 #define OP_ShiftLeft 87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
10871 #define OP_ShiftRight 88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
10872 #define OP_Add 89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
@@ -10856,111 +10873,110 @@
10873 #define OP_Subtract 90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
10874 #define OP_Multiply 91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
10875 #define OP_Divide 92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
10876 #define OP_Remainder 93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
10877 #define OP_Concat 94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
10878 #define OP_Delete 95
10879 #define OP_BitNot 96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
10880 #define OP_String8 97 /* same as TK_STRING, synopsis: r[P2]='P4' */
10881 #define OP_ResetCount 98
10882 #define OP_SorterCompare 99 /* synopsis: if key(P1)!=trim(r[P3],P4) goto P2 */
10883 #define OP_SorterData 100 /* synopsis: r[P2]=data */
10884 #define OP_RowKey 101 /* synopsis: r[P2]=key */
10885 #define OP_RowData 102 /* synopsis: r[P2]=data */
10886 #define OP_Rowid 103 /* synopsis: r[P2]=rowid */
10887 #define OP_NullRow 104
10888 #define OP_Last 105
10889 #define OP_SorterSort 106
10890 #define OP_Sort 107
10891 #define OP_Rewind 108
10892 #define OP_SorterInsert 109
10893 #define OP_IdxInsert 110 /* synopsis: key=r[P2] */
10894 #define OP_IdxDelete 111 /* synopsis: key=r[P2@P3] */
10895 #define OP_Seek 112 /* synopsis: Move P3 to P1.rowid */
10896 #define OP_IdxRowid 113 /* synopsis: r[P2]=rowid */
10897 #define OP_IdxLE 114 /* synopsis: key=r[P3@P4] */
10898 #define OP_IdxGT 115 /* synopsis: key=r[P3@P4] */
10899 #define OP_IdxLT 116 /* synopsis: key=r[P3@P4] */
10900 #define OP_IdxGE 117 /* synopsis: key=r[P3@P4] */
10901 #define OP_Destroy 118
10902 #define OP_Clear 119
10903 #define OP_ResetSorter 120
10904 #define OP_CreateIndex 121 /* synopsis: r[P2]=root iDb=P1 */
10905 #define OP_CreateTable 122 /* synopsis: r[P2]=root iDb=P1 */
10906 #define OP_ParseSchema 123
10907 #define OP_LoadAnalysis 124
10908 #define OP_DropTable 125
10909 #define OP_DropIndex 126
10910 #define OP_DropTrigger 127
10911 #define OP_IntegrityCk 128
10912 #define OP_RowSetAdd 129 /* synopsis: rowset(P1)=r[P2] */
10913 #define OP_RowSetRead 130 /* synopsis: r[P3]=rowset(P1) */
10914 #define OP_RowSetTest 131 /* synopsis: if r[P3] in rowset(P1) goto P2 */
10915 #define OP_Program 132
10916 #define OP_Real 133 /* same as TK_FLOAT, synopsis: r[P2]=P4 */
10917 #define OP_Param 134
10918 #define OP_FkCounter 135 /* synopsis: fkctr[P1]+=P2 */
10919 #define OP_FkIfZero 136 /* synopsis: if fkctr[P1]==0 goto P2 */
10920 #define OP_MemMax 137 /* synopsis: r[P1]=max(r[P1],r[P2]) */
10921 #define OP_IfPos 138 /* synopsis: if r[P1]>0 then r[P1]-=P3, goto P2 */
10922 #define OP_OffsetLimit 139 /* synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1) */
10923 #define OP_IfNotZero 140 /* synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2 */
10924 #define OP_DecrJumpZero 141 /* synopsis: if (--r[P1])==0 goto P2 */
10925 #define OP_JumpZeroIncr 142 /* synopsis: if (r[P1]++)==0 ) goto P2 */
10926 #define OP_AggStep0 143 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10927 #define OP_AggStep 144 /* synopsis: accum=r[P3] step(r[P2@P5]) */
10928 #define OP_AggFinal 145 /* synopsis: accum=r[P1] N=P2 */
10929 #define OP_IncrVacuum 146
10930 #define OP_Expire 147
10931 #define OP_TableLock 148 /* synopsis: iDb=P1 root=P2 write=P3 */
10932 #define OP_VBegin 149
10933 #define OP_VCreate 150
10934 #define OP_VDestroy 151
10935 #define OP_VOpen 152
10936 #define OP_VColumn 153 /* synopsis: r[P3]=vcolumn(P2) */
10937 #define OP_VNext 154
10938 #define OP_VRename 155
10939 #define OP_Pagecount 156
10940 #define OP_MaxPgcnt 157
10941 #define OP_Init 158 /* synopsis: Start at P2 */
10942 #define OP_CursorHint 159
10943 #define OP_Noop 160
10944 #define OP_Explain 161
 
10945
10946 /* Properties such as "out2" or "jump" that are specified in
10947 ** comments following the "case" for each opcode in the vdbe.c
10948 ** are encoded into bitvectors as follows:
10949 */
10950 #define OPFLG_JUMP 0x01 /* jump: P2 holds jmp target */
10951 #define OPFLG_IN1 0x02 /* in1: P1 is an input */
10952 #define OPFLG_IN2 0x04 /* in2: P2 is an input */
10953 #define OPFLG_IN3 0x08 /* in3: P3 is an input */
10954 #define OPFLG_OUT2 0x10 /* out2: P2 is an output */
10955 #define OPFLG_OUT3 0x20 /* out3: P3 is an output */
10956 #define OPFLG_INITIALIZER {\
10957 /* 0 */ 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01,\
10958 /* 8 */ 0x00, 0x10, 0x00, 0x01, 0x00, 0x01, 0x01, 0x02,\
10959 /* 16 */ 0x01, 0x02, 0x03, 0x12, 0x08, 0x00, 0x10, 0x10,\
10960 /* 24 */ 0x10, 0x10, 0x00, 0x10, 0x10, 0x00, 0x00, 0x10,\
10961 /* 32 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x02, 0x03, 0x02,\
10962 /* 40 */ 0x02, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x00,\
10963 /* 48 */ 0x00, 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,\
10964 /* 56 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,\
10965 /* 64 */ 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x26,\
10966 /* 72 */ 0x26, 0x10, 0x10, 0x00, 0x03, 0x03, 0x0b, 0x0b,\
10967 /* 80 */ 0x0b, 0x0b, 0x0b, 0x0b, 0x00, 0x26, 0x26, 0x26,\
10968 /* 88 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x00,\
10969 /* 96 */ 0x12, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10,\
10970 /* 104 */ 0x00, 0x01, 0x01, 0x01, 0x01, 0x04, 0x04, 0x00,\
10971 /* 112 */ 0x00, 0x10, 0x01, 0x01, 0x01, 0x01, 0x10, 0x00,\
10972 /* 120 */ 0x00, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,\
10973 /* 128 */ 0x00, 0x06, 0x23, 0x0b, 0x01, 0x10, 0x10, 0x00,\
10974 /* 136 */ 0x01, 0x04, 0x03, 0x1a, 0x03, 0x03, 0x03, 0x00,\
10975 /* 144 */ 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,\
10976 /* 152 */ 0x00, 0x00, 0x01, 0x00, 0x10, 0x10, 0x01, 0x00,\
10977 /* 160 */ 0x00, 0x00,}
10978
10979 /************** End of opcodes.h *********************************************/
10980 /************** Continuing where we left off in vdbe.h ***********************/
10981
10982 /*
@@ -10976,10 +10992,11 @@
10992 SQLITE_PRIVATE void sqlite3VdbeMultiLoad(Vdbe*,int,const char*,...);
10993 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
10994 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
10995 SQLITE_PRIVATE int sqlite3VdbeAddOp4Dup8(Vdbe*,int,int,int,int,const u8*,int);
10996 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
10997 SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe*,int);
10998 #if defined(SQLITE_DEBUG) && !defined(SQLITE_TEST_REALLOC_STRESS)
10999 SQLITE_PRIVATE void sqlite3VdbeVerifyNoMallocRequired(Vdbe *p, int N);
11000 #else
11001 # define sqlite3VdbeVerifyNoMallocRequired(A,B)
11002 #endif
@@ -11199,15 +11216,16 @@
11216 ** Flags for sqlite3PagerSetFlags()
11217 */
11218 #define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
11219 #define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
11220 #define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
11221 #define PAGER_SYNCHRONOUS_EXTRA 0x04 /* PRAGMA synchronous=EXTRA */
11222 #define PAGER_SYNCHRONOUS_MASK 0x07 /* Mask for four values above */
11223 #define PAGER_FULLFSYNC 0x08 /* PRAGMA fullfsync=ON */
11224 #define PAGER_CKPT_FULLFSYNC 0x10 /* PRAGMA checkpoint_fullfsync=ON */
11225 #define PAGER_CACHESPILL 0x20 /* PRAGMA cache_spill=ON */
11226 #define PAGER_FLAGS_MASK 0x38 /* All above except SYNCHRONOUS */
11227
11228 /*
11229 ** The remainder of this file contains the declarations of the functions
11230 ** that make up the Pager sub-system API. See source code comments for
11231 ** a detailed description of each routine.
@@ -11963,12 +11981,12 @@
11981 ** is shared by multiple database connections. Therefore, while parsing
11982 ** schema information, the Lookaside.bEnabled flag is cleared so that
11983 ** lookaside allocations are not used to construct the schema objects.
11984 */
11985 struct Lookaside {
11986 u32 bDisable; /* Only operate the lookaside when zero */
11987 u16 sz; /* Size of each buffer in bytes */
 
11988 u8 bMalloced; /* True if pStart obtained from sqlite3_malloc() */
11989 int nOut; /* Number of buffers currently checked out */
11990 int mxOut; /* Highwater mark for nOut */
11991 int anStat[3]; /* 0: hits. 1: size misses. 2: full misses */
11992 LookasideSlot *pFree; /* List of available buffers */
@@ -12047,10 +12065,11 @@
12065 u16 dbOptFlags; /* Flags to enable/disable optimizations */
12066 u8 enc; /* Text encoding */
12067 u8 autoCommit; /* The auto-commit flag. */
12068 u8 temp_store; /* 1: file 2: memory 0: default */
12069 u8 mallocFailed; /* True if we have seen a malloc failure */
12070 u8 bBenignMalloc; /* Do not require OOMs if true */
12071 u8 dfltLockMode; /* Default locking-mode for attached dbs */
12072 signed char nextAutovac; /* Autovac setting after VACUUM if >=0 */
12073 u8 suppressErr; /* Do not issue error messages if true */
12074 u8 vtabOnConflict; /* Value to return for s3_vtab_on_conflict() */
12075 u8 isTransactionSavepoint; /* True if the outermost savepoint is a TS */
@@ -12155,14 +12174,14 @@
12174 /*
12175 ** Possible values for the sqlite3.flags.
12176 */
12177 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
12178 #define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
12179 #define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
12180 #define SQLITE_FullFSync 0x00000008 /* Use full fsync on the backend */
12181 #define SQLITE_CkptFullFSync 0x00000010 /* Use full fsync for checkpoint */
12182 #define SQLITE_CacheSpill 0x00000020 /* OK to spill pager cache */
12183 #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
12184 #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
12185 /* DELETE, or UPDATE and return */
12186 /* the count using a callback. */
12187 #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
@@ -13510,11 +13529,11 @@
13529 /*
13530 ** During code generation of statements that do inserts into AUTOINCREMENT
13531 ** tables, the following information is attached to the Table.u.autoInc.p
13532 ** pointer of each autoincrement table to record some side information that
13533 ** the code generator needs. We have to keep per-table autoincrement
13534 ** information in case inserts are done within triggers. Triggers do not
13535 ** normally coordinate their activities, but we do need to coordinate the
13536 ** loading and saving of autoincrement information.
13537 */
13538 struct AutoincInfo {
13539 AutoincInfo *pNext; /* Next info block in a list of them all */
@@ -13602,10 +13621,11 @@
13621 u8 nTempReg; /* Number of temporary registers in aTempReg[] */
13622 u8 isMultiWrite; /* True if statement may modify/insert multiple rows */
13623 u8 mayAbort; /* True if statement may throw an ABORT exception */
13624 u8 hasCompound; /* Need to invoke convertCompoundSelectToSubquery() */
13625 u8 okConstFactor; /* OK to factor out constants */
13626 u8 disableLookaside; /* Number of times lookaside has been disabled */
13627 int aTempReg[8]; /* Holding area for temporary registers */
13628 int nRangeReg; /* Size of the temporary register block */
13629 int iRangeReg; /* First register in temporary register block */
13630 int nErr; /* Number of errors seen */
13631 int nTab; /* Number of previously allocated VDBE cursors */
@@ -13716,23 +13736,26 @@
13736 };
13737
13738 /*
13739 ** Bitfield flags for P5 value in various opcodes.
13740 */
13741 #define OPFLAG_NCHANGE 0x01 /* OP_Insert: Set to update db->nChange */
13742 /* Also used in P2 (not P5) of OP_Delete */
13743 #define OPFLAG_EPHEM 0x01 /* OP_Column: Ephemeral output is ok */
13744 #define OPFLAG_LASTROWID 0x02 /* Set to update db->lastRowid */
13745 #define OPFLAG_ISUPDATE 0x04 /* This OP_Insert is an sql UPDATE */
13746 #define OPFLAG_APPEND 0x08 /* This is likely to be an append */
13747 #define OPFLAG_USESEEKRESULT 0x10 /* Try to avoid a seek in BtreeInsert() */
13748 #define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
13749 #define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
13750 #define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
13751 #define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
13752 #define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
13753 #define OPFLAG_P2ISREG 0x10 /* P2 to OP_Open** is a register number */
13754 #define OPFLAG_PERMUTE 0x01 /* OP_Compare: use the permutation */
13755 #define OPFLAG_SAVEPOSITION 0x02 /* OP_Delete: keep cursor position */
13756 #define OPFLAG_AUXDELETE 0x04 /* OP_Delete: index in a DELETE op */
13757
13758 /*
13759 * Each trigger present in the database schema is stored as an instance of
13760 * struct Trigger.
13761 *
@@ -13847,14 +13870,20 @@
13870 char *zText; /* The string collected so far */
13871 u32 nChar; /* Length of the string so far */
13872 u32 nAlloc; /* Amount of space allocated in zText */
13873 u32 mxAlloc; /* Maximum allowed allocation. 0 for no malloc usage */
13874 u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
13875 u8 printfFlags; /* SQLITE_PRINTF flags below */
13876 };
13877 #define STRACCUM_NOMEM 1
13878 #define STRACCUM_TOOBIG 2
13879 #define SQLITE_PRINTF_INTERNAL 0x01 /* Internal-use-only converters allowed */
13880 #define SQLITE_PRINTF_SQLFUNC 0x02 /* SQL function arguments to VXPrintf */
13881 #define SQLITE_PRINTF_MALLOCED 0x04 /* True if xText is allocated space */
13882
13883 #define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
13884
13885
13886 /*
13887 ** A pointer to this structure is used to communicate information
13888 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
13889 */
@@ -13958,10 +13987,11 @@
13987 int n; /* A counter */
13988 int iCur; /* A cursor number */
13989 SrcList *pSrcList; /* FROM clause */
13990 struct SrcCount *pSrcCount; /* Counting column references */
13991 struct CCurHint *pCCurHint; /* Used by codeCursorHint() */
13992 int *aiCol; /* array of column indexes */
13993 } u;
13994 };
13995
13996 /* Forward declarations */
13997 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
@@ -14027,10 +14057,17 @@
14057 SQLITE_PRIVATE int sqlite3CantopenError(int);
14058 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
14059 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
14060 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
14061
14062 /*
14063 ** FTS3 and FTS4 both require virtual table support
14064 */
14065 #if defined(SQLITE_OMIT_VIRTUALTABLE)
14066 # undef SQLITE_ENABLE_FTS3
14067 # undef SQLITE_ENABLE_FTS4
14068 #endif
14069
14070 /*
14071 ** FTS4 is really an extension for FTS3. It is enabled using the
14072 ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also call
14073 ** the SQLITE_ENABLE_FTS4 macro to serve as an alias for SQLITE_ENABLE_FTS3.
@@ -14085,10 +14122,11 @@
14122 SQLITE_PRIVATE void sqlite3MallocEnd(void);
14123 SQLITE_PRIVATE void *sqlite3Malloc(u64);
14124 SQLITE_PRIVATE void *sqlite3MallocZero(u64);
14125 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, u64);
14126 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, u64);
14127 SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3*, u64);
14128 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
14129 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, u64);
14130 SQLITE_PRIVATE void *sqlite3Realloc(void*, u64);
14131 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, u64);
14132 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, u64);
@@ -14167,14 +14205,12 @@
14205 int nArg; /* Total number of arguments */
14206 int nUsed; /* Number of arguments used so far */
14207 sqlite3_value **apArg; /* The argument values */
14208 };
14209
14210 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, const char*, va_list);
14211 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, const char*, ...);
 
 
14212 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
14213 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
14214 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
14215 SQLITE_PRIVATE void sqlite3DebugPrintf(const char*, ...);
14216 #endif
@@ -14191,10 +14227,11 @@
14227
14228
14229 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*);
14230 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
14231 SQLITE_PRIVATE int sqlite3Dequote(char*);
14232 SQLITE_PRIVATE void sqlite3TokenInit(Token*,char*);
14233 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
14234 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
14235 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
14236 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
14237 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
@@ -14403,11 +14440,11 @@
14440 Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
14441 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
14442 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
14443 SQLITE_PRIVATE void sqlite3ResolvePartIdxLabel(Parse*,int);
14444 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
14445 u8,u8,int,int*,int*);
14446 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
14447 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, u8, int, u8*, int*, int*);
14448 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
14449 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
14450 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
@@ -14622,11 +14659,10 @@
14659 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
14660 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
14661 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
14662 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
14663 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
 
14664 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
14665 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
14666 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
14667 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
14668 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
@@ -14638,10 +14674,12 @@
14674 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
14675 void (*)(sqlite3_context*,int,sqlite3_value **),
14676 void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
14677 FuncDestructor *pDestructor
14678 );
14679 SQLITE_PRIVATE void sqlite3OomFault(sqlite3*);
14680 SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
14681 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
14682 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
14683
14684 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
14685 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
@@ -15734,18 +15772,20 @@
15772 ** - On either an index or a table
15773 ** * A sorter
15774 ** * A virtual table
15775 ** * A one-row "pseudotable" stored in a single register
15776 */
15777 typedef struct VdbeCursor VdbeCursor;
15778 struct VdbeCursor {
15779 u8 eCurType; /* One of the CURTYPE_* values above */
15780 i8 iDb; /* Index of cursor database in db->aDb[] (or -1) */
15781 u8 nullRow; /* True if pointing to a row with no data */
15782 u8 deferredMoveto; /* A call to sqlite3BtreeMoveto() is needed */
15783 u8 isTable; /* True for rowid tables. False for indexes */
15784 #ifdef SQLITE_DEBUG
15785 u8 seekOp; /* Most recent seek operation on this cursor */
15786 u8 wrFlag; /* The wrFlag argument to sqlite3BtreeCursor() */
15787 #endif
15788 Bool isEphemeral:1; /* True for an ephemeral table */
15789 Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
15790 Bool isOrdered:1; /* True if the underlying table is BTREE_UNORDERED */
15791 Pgno pgnoRoot; /* Root page of the open btree cursor */
@@ -15760,10 +15800,12 @@
15800 Btree *pBt; /* Separate file holding temporary table */
15801 KeyInfo *pKeyInfo; /* Info about index keys needed by index cursors */
15802 int seekResult; /* Result of previous sqlite3BtreeMoveto() */
15803 i64 seqCount; /* Sequence counter */
15804 i64 movetoTarget; /* Argument to the deferred sqlite3BtreeMoveto() */
15805 VdbeCursor *pAltCursor; /* Associated index cursor from which to read */
15806 int *aAltMap; /* Mapping from table to index column numbers */
15807 #ifdef SQLITE_ENABLE_COLUMN_USED_MASK
15808 u64 maskUsed; /* Mask of columns used by this cursor */
15809 #endif
15810
15811 /* Cached information about the header for the data record that the
@@ -15784,11 +15826,10 @@
15826 u32 aType[1]; /* Type values for all entries in the record */
15827 /* 2*nField extra array elements allocated for aType[], beyond the one
15828 ** static element declared in the structure. nField total array slots for
15829 ** aType[] and nField+1 array slots for aOffset[] */
15830 };
 
15831
15832 /*
15833 ** When a sub-program is executed (OP_Program), a structure of this type
15834 ** is allocated to store the current value of the program counter, as
15835 ** well as the current memory cell array and various other frame specific
@@ -15895,11 +15936,11 @@
15936 #define MEM_AffMask 0x001f /* Mask of affinity bits */
15937 #define MEM_RowSet 0x0020 /* Value is a RowSet object */
15938 #define MEM_Frame 0x0040 /* Value is a VdbeFrame object */
15939 #define MEM_Undefined 0x0080 /* Value is undefined */
15940 #define MEM_Cleared 0x0100 /* NULL set by OP_Null, not from data */
15941 #define MEM_TypeMask 0x81ff /* Mask of type bits */
15942
15943
15944 /* Whenever Mem contains a valid string or blob representation, one of
15945 ** the following flags must be set to determine the memory management
15946 ** policy for Mem.z. The MEM_Term flag tells us whether or not the
@@ -15909,14 +15950,21 @@
15950 #define MEM_Dyn 0x0400 /* Need to call Mem.xDel() on Mem.z */
15951 #define MEM_Static 0x0800 /* Mem.z points to a static string */
15952 #define MEM_Ephem 0x1000 /* Mem.z points to an ephemeral string */
15953 #define MEM_Agg 0x2000 /* Mem.z points to an agg function context */
15954 #define MEM_Zero 0x4000 /* Mem.i contains count of 0s appended to blob */
15955 #define MEM_Subtype 0x8000 /* Mem.eSubtype is valid */
15956 #ifdef SQLITE_OMIT_INCRBLOB
15957 #undef MEM_Zero
15958 #define MEM_Zero 0x0000
15959 #endif
15960
15961 /* Return TRUE if Mem X contains dynamically allocated content - anything
15962 ** that needs to be deallocated to avoid a leak.
15963 */
15964 #define VdbeMemDynamic(X) \
15965 (((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame))!=0)
15966
15967 /*
15968 ** Clear any existing type flags from a Mem and replace them with f
15969 */
15970 #define MemSetTypeFlag(p, f) \
@@ -16083,11 +16131,11 @@
16131 ** Function prototypes
16132 */
16133 SQLITE_PRIVATE void sqlite3VdbeError(Vdbe*, const char *, ...);
16134 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
16135 void sqliteVdbePopStack(Vdbe*,int);
16136 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor**, int*);
16137 SQLITE_PRIVATE int sqlite3VdbeCursorRestore(VdbeCursor*);
16138 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
16139 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
16140 #endif
16141 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
@@ -16129,12 +16177,10 @@
16177 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
16178 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
16179 SQLITE_PRIVATE void sqlite3VdbeMemCast(Mem*,u8,u8);
16180 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
16181 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
 
 
16182 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
16183 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
16184 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
16185 SQLITE_PRIVATE int sqlite3VdbeMemClearAndResize(Mem *pMem, int n);
16186 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
@@ -17492,11 +17538,11 @@
17538 z = zBuf;
17539 }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
17540 sqlite3_result_error_toobig(context);
17541 return;
17542 }else{
17543 z = sqlite3DbMallocRawNN(db, (int)n);
17544 if( z==0 ){
17545 sqlite3_result_error_nomem(context);
17546 return;
17547 }
17548 }
@@ -17707,18 +17753,40 @@
17753 */
17754 #define _SQLITE_OS_C_ 1
17755 /* #include "sqliteInt.h" */
17756 #undef _SQLITE_OS_C_
17757
17758 /*
17759 ** If we compile with the SQLITE_TEST macro set, then the following block
17760 ** of code will give us the ability to simulate a disk I/O error. This
17761 ** is used for testing the I/O recovery logic.
17762 */
17763 #if defined(SQLITE_TEST)
17764 SQLITE_API int sqlite3_io_error_hit = 0; /* Total number of I/O Errors */
17765 SQLITE_API int sqlite3_io_error_hardhit = 0; /* Number of non-benign errors */
17766 SQLITE_API int sqlite3_io_error_pending = 0; /* Count down to first I/O error */
17767 SQLITE_API int sqlite3_io_error_persist = 0; /* True if I/O errors persist */
17768 SQLITE_API int sqlite3_io_error_benign = 0; /* True if errors are benign */
17769 SQLITE_API int sqlite3_diskfull_pending = 0;
17770 SQLITE_API int sqlite3_diskfull = 0;
17771 #endif /* defined(SQLITE_TEST) */
17772
17773 /*
17774 ** When testing, also keep a count of the number of open files.
17775 */
17776 #if defined(SQLITE_TEST)
17777 SQLITE_API int sqlite3_open_file_count = 0;
17778 #endif /* defined(SQLITE_TEST) */
17779
17780 /*
17781 ** The default SQLite sqlite3_vfs implementations do not allocate
17782 ** memory (actually, os_unix.c allocates a small amount of memory
17783 ** from within OsOpen()), but some third-party implementations may.
17784 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
17785 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
17786 **
17787 ** The following functions are instrumented for malloc() failure
17788 ** testing:
17789 **
17790 ** sqlite3OsRead()
17791 ** sqlite3OsWrite()
17792 ** sqlite3OsSync()
@@ -17800,12 +17868,12 @@
17868 */
17869 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
17870 #ifdef SQLITE_TEST
17871 if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
17872 /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
17873 ** is using a regular VFS, it is called after the corresponding
17874 ** transaction has been committed. Injecting a fault at this point
17875 ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
17876 ** but the transaction is committed anyway.
17877 **
17878 ** The core must call OsFileControl() though, not OsFileControlHint(),
17879 ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
@@ -17870,14 +17938,14 @@
17938 /*
17939 ** The next group of routines are convenience wrappers around the
17940 ** VFS methods.
17941 */
17942 SQLITE_PRIVATE int sqlite3OsOpen(
17943 sqlite3_vfs *pVfs,
17944 const char *zPath,
17945 sqlite3_file *pFile,
17946 int flags,
17947 int *pFlagsOut
17948 ){
17949 int rc;
17950 DO_OS_MALLOC_TEST(0);
17951 /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
@@ -17892,22 +17960,22 @@
17960 DO_OS_MALLOC_TEST(0);
17961 assert( dirSync==0 || dirSync==1 );
17962 return pVfs->xDelete(pVfs, zPath, dirSync);
17963 }
17964 SQLITE_PRIVATE int sqlite3OsAccess(
17965 sqlite3_vfs *pVfs,
17966 const char *zPath,
17967 int flags,
17968 int *pResOut
17969 ){
17970 DO_OS_MALLOC_TEST(0);
17971 return pVfs->xAccess(pVfs, zPath, flags, pResOut);
17972 }
17973 SQLITE_PRIVATE int sqlite3OsFullPathname(
17974 sqlite3_vfs *pVfs,
17975 const char *zPath,
17976 int nPathOut,
17977 char *zPathOut
17978 ){
17979 DO_OS_MALLOC_TEST(0);
17980 zPathOut[0] = 0;
17981 return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
@@ -17949,13 +18017,13 @@
18017 }
18018 return rc;
18019 }
18020
18021 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
18022 sqlite3_vfs *pVfs,
18023 const char *zFile,
18024 sqlite3_file **ppFile,
18025 int flags,
18026 int *pOutFlags
18027 ){
18028 int rc = SQLITE_NOMEM;
18029 sqlite3_file *pFile;
@@ -20081,15 +20149,15 @@
20149 mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
20150 while( ALWAYS(iLogsize<LOGMAX) ){
20151 int iBuddy;
20152 if( (iBlock>>iLogsize) & 1 ){
20153 iBuddy = iBlock - size;
20154 assert( iBuddy>=0 );
20155 }else{
20156 iBuddy = iBlock + size;
20157 if( iBuddy>=mem5.nBlock ) break;
20158 }
 
 
20159 if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
20160 memsys5Unlink(iBuddy, iLogsize);
20161 iLogsize++;
20162 if( iBuddy<iBlock ){
20163 mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
@@ -21174,12 +21242,12 @@
21242 ** Macros for performance tracing. Normally turned off. Only works
21243 ** on i486 hardware.
21244 */
21245 #ifdef SQLITE_PERFORMANCE_TRACE
21246
21247 /*
21248 ** hwtime.h contains inline assembler code for implementing
21249 ** high-performance timing routines.
21250 */
21251 /************** Include hwtime.h in the middle of os_common.h ****************/
21252 /************** Begin file hwtime.h ******************************************/
21253 /*
@@ -21285,18 +21353,18 @@
21353 /*
21354 ** If we compile with the SQLITE_TEST macro set, then the following block
21355 ** of code will give us the ability to simulate a disk I/O error. This
21356 ** is used for testing the I/O recovery logic.
21357 */
21358 #if defined(SQLITE_TEST)
21359 SQLITE_API extern int sqlite3_io_error_hit;
21360 SQLITE_API extern int sqlite3_io_error_hardhit;
21361 SQLITE_API extern int sqlite3_io_error_pending;
21362 SQLITE_API extern int sqlite3_io_error_persist;
21363 SQLITE_API extern int sqlite3_io_error_benign;
21364 SQLITE_API extern int sqlite3_diskfull_pending;
21365 SQLITE_API extern int sqlite3_diskfull;
21366 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
21367 #define SimulateIOError(CODE) \
21368 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
21369 || sqlite3_io_error_pending-- == 1 ) \
21370 { local_ioerr(); CODE; }
@@ -21318,21 +21386,21 @@
21386 }
21387 #else
21388 #define SimulateIOErrorBenign(X)
21389 #define SimulateIOError(A)
21390 #define SimulateDiskfullError(A)
21391 #endif /* defined(SQLITE_TEST) */
21392
21393 /*
21394 ** When testing, keep a count of the number of open files.
21395 */
21396 #if defined(SQLITE_TEST)
21397 SQLITE_API extern int sqlite3_open_file_count;
21398 #define OpenCounter(X) sqlite3_open_file_count+=(X)
21399 #else
21400 #define OpenCounter(X)
21401 #endif /* defined(SQLITE_TEST) */
21402
21403 #endif /* !defined(_OS_COMMON_H_) */
21404
21405 /************** End of os_common.h *******************************************/
21406 /************** Continuing where we left off in mutex_w32.c ******************/
@@ -22386,14 +22454,28 @@
22454 /*
22455 ** Allocate and zero memory. If the allocation fails, make
22456 ** the mallocFailed flag in the connection pointer.
22457 */
22458 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, u64 n){
22459 void *p;
22460 testcase( db==0 );
22461 p = sqlite3DbMallocRaw(db, n);
22462 if( p ) memset(p, 0, (size_t)n);
22463 return p;
22464 }
22465
22466
22467 /* Finish the work of sqlite3DbMallocRawNN for the unusual and
22468 ** slower case when the allocation cannot be fulfilled using lookaside.
22469 */
22470 static SQLITE_NOINLINE void *dbMallocRawFinish(sqlite3 *db, u64 n){
22471 void *p;
22472 assert( db!=0 );
22473 p = sqlite3Malloc(n);
22474 if( !p ) sqlite3OomFault(db);
22475 sqlite3MemdebugSetType(p,
22476 (db->lookaside.bDisable==0) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP);
22477 return p;
22478 }
22479
22480 /*
22481 ** Allocate memory, either lookaside (if possible) or heap.
@@ -22411,71 +22493,77 @@
22493 ** int *b = (int*)sqlite3DbMallocRaw(db, 200);
22494 ** if( b ) a[10] = 9;
22495 **
22496 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
22497 ** that all prior mallocs (ex: "a") worked too.
22498 **
22499 ** The sqlite3MallocRawNN() variant guarantees that the "db" parameter is
22500 ** not a NULL pointer.
22501 */
 
22502 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, u64 n){
22503 void *p;
22504 if( db ) return sqlite3DbMallocRawNN(db, n);
22505 p = sqlite3Malloc(n);
22506 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
22507 return p;
22508 }
22509 SQLITE_PRIVATE void *sqlite3DbMallocRawNN(sqlite3 *db, u64 n){
22510 #ifndef SQLITE_OMIT_LOOKASIDE
22511 LookasideSlot *pBuf;
22512 assert( db!=0 );
22513 assert( sqlite3_mutex_held(db->mutex) );
22514 assert( db->pnBytesFreed==0 );
22515 if( db->lookaside.bDisable==0 ){
22516 assert( db->mallocFailed==0 );
22517 if( n>db->lookaside.sz ){
22518 db->lookaside.anStat[1]++;
22519 }else if( (pBuf = db->lookaside.pFree)==0 ){
22520 db->lookaside.anStat[2]++;
22521 }else{
22522 db->lookaside.pFree = pBuf->pNext;
22523 db->lookaside.nOut++;
22524 db->lookaside.anStat[0]++;
22525 if( db->lookaside.nOut>db->lookaside.mxOut ){
22526 db->lookaside.mxOut = db->lookaside.nOut;
22527 }
22528 return (void*)pBuf;
22529 }
22530 }else if( db->mallocFailed ){
22531 return 0;
22532 }
22533 #else
22534 assert( db!=0 );
22535 assert( sqlite3_mutex_held(db->mutex) );
22536 assert( db->pnBytesFreed==0 );
22537 if( db->mallocFailed ){
22538 return 0;
22539 }
22540 #endif
22541 return dbMallocRawFinish(db, n);
22542 }
22543
22544 /* Forward declaration */
22545 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n);
 
 
 
 
 
 
22546
22547 /*
22548 ** Resize the block of memory pointed to by p to n bytes. If the
22549 ** resize fails, set the mallocFailed flag in the connection object.
22550 */
22551 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, u64 n){
22552 assert( db!=0 );
22553 if( p==0 ) return sqlite3DbMallocRawNN(db, n);
22554 assert( sqlite3_mutex_held(db->mutex) );
22555 if( isLookaside(db,p) && n<=db->lookaside.sz ) return p;
22556 return dbReallocFinish(db, p, n);
22557 }
22558 static SQLITE_NOINLINE void *dbReallocFinish(sqlite3 *db, void *p, u64 n){
22559 void *pNew = 0;
22560 assert( db!=0 );
22561 assert( p!=0 );
22562 if( db->mallocFailed==0 ){
 
 
 
22563 if( isLookaside(db, p) ){
22564 pNew = sqlite3DbMallocRawNN(db, n);
 
 
 
22565 if( pNew ){
22566 memcpy(pNew, p, db->lookaside.sz);
22567 sqlite3DbFree(db, p);
22568 }
22569 }else{
@@ -22482,14 +22570,14 @@
22570 assert( sqlite3MemdebugHasType(p, (MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
22571 assert( sqlite3MemdebugNoType(p, (u8)~(MEMTYPE_LOOKASIDE|MEMTYPE_HEAP)) );
22572 sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
22573 pNew = sqlite3_realloc64(p, n);
22574 if( !pNew ){
22575 sqlite3OomFault(db);
22576 }
22577 sqlite3MemdebugSetType(pNew,
22578 (db->lookaside.bDisable==0 ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
22579 }
22580 }
22581 return pNew;
22582 }
22583
@@ -22527,15 +22615,16 @@
22615 }
22616 return zNew;
22617 }
22618 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, u64 n){
22619 char *zNew;
22620 assert( db!=0 );
22621 if( z==0 ){
22622 return 0;
22623 }
22624 assert( (n&0x7fffffff)==n );
22625 zNew = sqlite3DbMallocRawNN(db, n+1);
22626 if( zNew ){
22627 memcpy(zNew, z, (size_t)n);
22628 zNew[n] = 0;
22629 }
22630 return zNew;
@@ -22546,16 +22635,48 @@
22635 */
22636 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zNew){
22637 sqlite3DbFree(db, *pz);
22638 *pz = sqlite3DbStrDup(db, zNew);
22639 }
22640
22641 /*
22642 ** Call this routine to record the fact that an OOM (out-of-memory) error
22643 ** has happened. This routine will set db->mallocFailed, and also
22644 ** temporarily disable the lookaside memory allocator and interrupt
22645 ** any running VDBEs.
22646 */
22647 SQLITE_PRIVATE void sqlite3OomFault(sqlite3 *db){
22648 if( db->mallocFailed==0 && db->bBenignMalloc==0 ){
22649 db->mallocFailed = 1;
22650 if( db->nVdbeExec>0 ){
22651 db->u1.isInterrupted = 1;
22652 }
22653 db->lookaside.bDisable++;
22654 }
22655 }
22656
22657 /*
22658 ** This routine reactivates the memory allocator and clears the
22659 ** db->mallocFailed flag as necessary.
22660 **
22661 ** The memory allocator is not restarted if there are running
22662 ** VDBEs.
22663 */
22664 SQLITE_PRIVATE void sqlite3OomClear(sqlite3 *db){
22665 if( db->mallocFailed && db->nVdbeExec==0 ){
22666 db->mallocFailed = 0;
22667 db->u1.isInterrupted = 0;
22668 assert( db->lookaside.bDisable>0 );
22669 db->lookaside.bDisable--;
22670 }
22671 }
22672
22673 /*
22674 ** Take actions at the end of an API call to indicate an OOM error
22675 */
22676 static SQLITE_NOINLINE int apiOomError(sqlite3 *db){
22677 sqlite3OomClear(db);
22678 sqlite3Error(db, SQLITE_NOMEM);
22679 return SQLITE_NOMEM;
22680 }
22681
22682 /*
@@ -22756,11 +22877,10 @@
22877 /*
22878 ** Render a string given by "fmt" into the StrAccum object.
22879 */
22880 SQLITE_PRIVATE void sqlite3VXPrintf(
22881 StrAccum *pAccum, /* Accumulate results here */
 
22882 const char *fmt, /* Format string */
22883 va_list ap /* arguments */
22884 ){
22885 int c; /* Next character in the format string */
22886 char *bufpt; /* Pointer to the conversion buffer */
@@ -22796,15 +22916,15 @@
22916 #endif
22917 PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
22918 char buf[etBUFSIZE]; /* Conversion buffer */
22919
22920 bufpt = 0;
22921 if( pAccum->printfFlags ){
22922 if( (bArgList = (pAccum->printfFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
22923 pArgList = va_arg(ap, PrintfArguments*);
22924 }
22925 useIntern = pAccum->printfFlags & SQLITE_PRINTF_INTERNAL;
22926 }else{
22927 bArgList = useIntern = 0;
22928 }
22929 for(; (c=(*fmt))!=0; ++fmt){
22930 if( c!='%' ){
@@ -23351,13 +23471,13 @@
23471 if( p->mxAlloc==0 ){
23472 N = p->nAlloc - p->nChar - 1;
23473 setStrAccumError(p, STRACCUM_TOOBIG);
23474 return N;
23475 }else{
23476 char *zOld = isMalloced(p) ? p->zText : 0;
23477 i64 szNew = p->nChar;
23478 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
23479 szNew += N + 1;
23480 if( szNew+p->nChar<=p->mxAlloc ){
23481 /* Force exponential buffer size growth as long as it does not overflow,
23482 ** to avoid having to call this routine too often */
23483 szNew += p->nChar;
@@ -23374,14 +23494,14 @@
23494 }else{
23495 zNew = sqlite3_realloc64(zOld, p->nAlloc);
23496 }
23497 if( zNew ){
23498 assert( p->zText!=0 || p->nChar==0 );
23499 if( !isMalloced(p) && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
23500 p->zText = zNew;
23501 p->nAlloc = sqlite3DbMallocSize(p->db, zNew);
23502 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
23503 }else{
23504 sqlite3StrAccumReset(p);
23505 setStrAccumError(p, STRACCUM_NOMEM);
23506 return 0;
23507 }
@@ -23395,11 +23515,11 @@
23515 SQLITE_PRIVATE void sqlite3AppendChar(StrAccum *p, int N, char c){
23516 testcase( p->nChar + (i64)N > 0x7fffffff );
23517 if( p->nChar+(i64)N >= p->nAlloc && (N = sqlite3StrAccumEnlarge(p, N))<=0 ){
23518 return;
23519 }
23520 assert( (p->zText==p->zBase)==!isMalloced(p) );
23521 while( (N--)>0 ) p->zText[p->nChar++] = c;
23522 }
23523
23524 /*
23525 ** The StrAccum "p" is not large enough to accept N new bytes of z[].
@@ -23413,11 +23533,11 @@
23533 N = sqlite3StrAccumEnlarge(p, N);
23534 if( N>0 ){
23535 memcpy(&p->zText[p->nChar], z, N);
23536 p->nChar += N;
23537 }
23538 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
23539 }
23540
23541 /*
23542 ** Append N bytes of text from z to the StrAccum object. Increase the
23543 ** size of the memory allocation for StrAccum if necessary.
@@ -23449,17 +23569,17 @@
23569 ** Return a pointer to the resulting string. Return a NULL
23570 ** pointer if any kind of error was encountered.
23571 */
23572 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
23573 if( p->zText ){
23574 assert( (p->zText==p->zBase)==!isMalloced(p) );
23575 p->zText[p->nChar] = 0;
23576 if( p->mxAlloc>0 && !isMalloced(p) ){
23577 p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
23578 if( p->zText ){
23579 memcpy(p->zText, p->zBase, p->nChar+1);
23580 p->printfFlags |= SQLITE_PRINTF_MALLOCED;
23581 }else{
23582 setStrAccumError(p, STRACCUM_NOMEM);
23583 }
23584 }
23585 }
@@ -23468,14 +23588,14 @@
23588
23589 /*
23590 ** Reset an StrAccum string. Reclaim all malloced memory.
23591 */
23592 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
23593 assert( (p->zText==0 || p->zText==p->zBase)==!isMalloced(p) );
23594 if( isMalloced(p) ){
23595 sqlite3DbFree(p->db, p->zText);
23596 p->printfFlags &= ~SQLITE_PRINTF_MALLOCED;
23597 }
23598 p->zText = 0;
23599 }
23600
23601 /*
@@ -23497,11 +23617,11 @@
23617 p->db = db;
23618 p->nChar = 0;
23619 p->nAlloc = n;
23620 p->mxAlloc = mx;
23621 p->accError = 0;
23622 p->printfFlags = 0;
23623 }
23624
23625 /*
23626 ** Print into memory obtained from sqliteMalloc(). Use the internal
23627 ** %-conversion extensions.
@@ -23511,14 +23631,15 @@
23631 char zBase[SQLITE_PRINT_BUF_SIZE];
23632 StrAccum acc;
23633 assert( db!=0 );
23634 sqlite3StrAccumInit(&acc, db, zBase, sizeof(zBase),
23635 db->aLimit[SQLITE_LIMIT_LENGTH]);
23636 acc.printfFlags = SQLITE_PRINTF_INTERNAL;
23637 sqlite3VXPrintf(&acc, zFormat, ap);
23638 z = sqlite3StrAccumFinish(&acc);
23639 if( acc.accError==STRACCUM_NOMEM ){
23640 sqlite3OomFault(db);
23641 }
23642 return z;
23643 }
23644
23645 /*
@@ -23551,11 +23672,11 @@
23672 #endif
23673 #ifndef SQLITE_OMIT_AUTOINIT
23674 if( sqlite3_initialize() ) return 0;
23675 #endif
23676 sqlite3StrAccumInit(&acc, 0, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
23677 sqlite3VXPrintf(&acc, zFormat, ap);
23678 z = sqlite3StrAccumFinish(&acc);
23679 return z;
23680 }
23681
23682 /*
@@ -23596,11 +23717,11 @@
23717 if( zBuf ) zBuf[0] = 0;
23718 return zBuf;
23719 }
23720 #endif
23721 sqlite3StrAccumInit(&acc, 0, zBuf, n, 0);
23722 sqlite3VXPrintf(&acc, zFormat, ap);
23723 return sqlite3StrAccumFinish(&acc);
23724 }
23725 SQLITE_API char *SQLITE_CDECL sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
23726 char *z;
23727 va_list ap;
@@ -23627,11 +23748,11 @@
23748 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
23749 StrAccum acc; /* String accumulator */
23750 char zMsg[SQLITE_PRINT_BUF_SIZE*3]; /* Complete log message */
23751
23752 sqlite3StrAccumInit(&acc, 0, zMsg, sizeof(zMsg), 0);
23753 sqlite3VXPrintf(&acc, zFormat, ap);
23754 sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
23755 sqlite3StrAccumFinish(&acc));
23756 }
23757
23758 /*
@@ -23656,11 +23777,11 @@
23777 va_list ap;
23778 StrAccum acc;
23779 char zBuf[500];
23780 sqlite3StrAccumInit(&acc, 0, zBuf, sizeof(zBuf), 0);
23781 va_start(ap,zFormat);
23782 sqlite3VXPrintf(&acc, zFormat, ap);
23783 va_end(ap);
23784 sqlite3StrAccumFinish(&acc);
23785 fprintf(stdout,"%s", zBuf);
23786 fflush(stdout);
23787 }
@@ -23669,14 +23790,14 @@
23790
23791 /*
23792 ** variable-argument wrapper around sqlite3VXPrintf(). The bFlags argument
23793 ** can contain the bit SQLITE_PRINTF_INTERNAL enable internal formats.
23794 */
23795 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, const char *zFormat, ...){
23796 va_list ap;
23797 va_start(ap,zFormat);
23798 sqlite3VXPrintf(p, zFormat, ap);
23799 va_end(ap);
23800 }
23801
23802 /************** End of printf.c **********************************************/
23803 /************** Begin file treeview.c ****************************************/
@@ -23743,11 +23864,11 @@
23864 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "| " : " ", 4);
23865 }
23866 sqlite3StrAccumAppend(&acc, p->bLine[i] ? "|-- " : "'-- ", 4);
23867 }
23868 va_start(ap, zFormat);
23869 sqlite3VXPrintf(&acc, zFormat, ap);
23870 va_end(ap);
23871 if( zBuf[acc.nChar-1]!='\n' ) sqlite3StrAccumAppend(&acc, "\n", 1);
23872 sqlite3StrAccumFinish(&acc);
23873 fprintf(stdout,"%s", zBuf);
23874 fflush(stdout);
@@ -23778,21 +23899,21 @@
23899 for(i=0; i<pWith->nCte; i++){
23900 StrAccum x;
23901 char zLine[1000];
23902 const struct Cte *pCte = &pWith->a[i];
23903 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23904 sqlite3XPrintf(&x, "%s", pCte->zName);
23905 if( pCte->pCols && pCte->pCols->nExpr>0 ){
23906 char cSep = '(';
23907 int j;
23908 for(j=0; j<pCte->pCols->nExpr; j++){
23909 sqlite3XPrintf(&x, "%c%s", cSep, pCte->pCols->a[j].zName);
23910 cSep = ',';
23911 }
23912 sqlite3XPrintf(&x, ")");
23913 }
23914 sqlite3XPrintf(&x, " AS");
23915 sqlite3StrAccumFinish(&x);
23916 sqlite3TreeViewItem(pView, zLine, i<pWith->nCte-1);
23917 sqlite3TreeViewSelect(pView, pCte->pSelect, 0);
23918 sqlite3TreeViewPop(pView);
23919 }
@@ -23839,24 +23960,24 @@
23960 for(i=0; i<p->pSrc->nSrc; i++){
23961 struct SrcList_item *pItem = &p->pSrc->a[i];
23962 StrAccum x;
23963 char zLine[100];
23964 sqlite3StrAccumInit(&x, 0, zLine, sizeof(zLine), 0);
23965 sqlite3XPrintf(&x, "{%d,*}", pItem->iCursor);
23966 if( pItem->zDatabase ){
23967 sqlite3XPrintf(&x, " %s.%s", pItem->zDatabase, pItem->zName);
23968 }else if( pItem->zName ){
23969 sqlite3XPrintf(&x, " %s", pItem->zName);
23970 }
23971 if( pItem->pTab ){
23972 sqlite3XPrintf(&x, " tabname=%Q", pItem->pTab->zName);
23973 }
23974 if( pItem->zAlias ){
23975 sqlite3XPrintf(&x, " (AS %s)", pItem->zAlias);
23976 }
23977 if( pItem->fg.jointype & JT_LEFT ){
23978 sqlite3XPrintf(&x, " LEFT-JOIN");
23979 }
23980 sqlite3StrAccumFinish(&x);
23981 sqlite3TreeViewItem(pView, zLine, i<p->pSrc->nSrc-1);
23982 if( pItem->pSelect ){
23983 sqlite3TreeViewSelect(pView, pItem->pSelect, 0);
@@ -24899,11 +25020,11 @@
25020 *z = 0;
25021 assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
25022
25023 c = pMem->flags;
25024 sqlite3VdbeMemRelease(pMem);
25025 pMem->flags = MEM_Str|MEM_Term|(c&(MEM_AffMask|MEM_Subtype));
25026 pMem->enc = desiredEnc;
25027 pMem->z = (char*)zOut;
25028 pMem->zMalloc = pMem->z;
25029 pMem->szMalloc = sqlite3DbMallocSize(pMem->db, pMem->z);
25030
@@ -25349,10 +25470,18 @@
25470 }
25471 }
25472 z[j] = 0;
25473 return j;
25474 }
25475
25476 /*
25477 ** Generate a Token object from a string
25478 */
25479 SQLITE_PRIVATE void sqlite3TokenInit(Token *p, char *z){
25480 p->z = z;
25481 p->n = sqlite3Strlen30(z);
25482 }
25483
25484 /* Convenient short-hand */
25485 #define UpperToLower sqlite3UpperToLower
25486
25487 /*
@@ -26258,11 +26387,11 @@
26387 */
26388 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
26389 char *zBlob;
26390 int i;
26391
26392 zBlob = (char *)sqlite3DbMallocRawNN(db, n/2 + 1);
26393 n--;
26394 if( zBlob ){
26395 for(i=0; i<n; i+=2){
26396 zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
26397 }
@@ -26796,95 +26925,96 @@
26925 # define OpHelp(X) "\0" X
26926 #else
26927 # define OpHelp(X)
26928 #endif
26929 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
26930 static const char *const azName[] = {
26931 /* 0 */ "Savepoint" OpHelp(""),
26932 /* 1 */ "AutoCommit" OpHelp(""),
26933 /* 2 */ "Transaction" OpHelp(""),
26934 /* 3 */ "SorterNext" OpHelp(""),
26935 /* 4 */ "PrevIfOpen" OpHelp(""),
26936 /* 5 */ "NextIfOpen" OpHelp(""),
26937 /* 6 */ "Prev" OpHelp(""),
26938 /* 7 */ "Next" OpHelp(""),
26939 /* 8 */ "Checkpoint" OpHelp(""),
26940 /* 9 */ "JournalMode" OpHelp(""),
26941 /* 10 */ "Vacuum" OpHelp(""),
26942 /* 11 */ "VFilter" OpHelp("iplan=r[P3] zplan='P4'"),
26943 /* 12 */ "VUpdate" OpHelp("data=r[P3@P2]"),
26944 /* 13 */ "Goto" OpHelp(""),
26945 /* 14 */ "Gosub" OpHelp(""),
26946 /* 15 */ "Return" OpHelp(""),
26947 /* 16 */ "InitCoroutine" OpHelp(""),
26948 /* 17 */ "EndCoroutine" OpHelp(""),
26949 /* 18 */ "Yield" OpHelp(""),
26950 /* 19 */ "Not" OpHelp("r[P2]= !r[P1]"),
26951 /* 20 */ "HaltIfNull" OpHelp("if r[P3]=null halt"),
26952 /* 21 */ "Halt" OpHelp(""),
26953 /* 22 */ "Integer" OpHelp("r[P2]=P1"),
26954 /* 23 */ "Int64" OpHelp("r[P2]=P4"),
26955 /* 24 */ "String" OpHelp("r[P2]='P4' (len=P1)"),
26956 /* 25 */ "Null" OpHelp("r[P2..P3]=NULL"),
26957 /* 26 */ "SoftNull" OpHelp("r[P1]=NULL"),
26958 /* 27 */ "Blob" OpHelp("r[P2]=P4 (len=P1)"),
26959 /* 28 */ "Variable" OpHelp("r[P2]=parameter(P1,P4)"),
26960 /* 29 */ "Move" OpHelp("r[P2@P3]=r[P1@P3]"),
26961 /* 30 */ "Copy" OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
26962 /* 31 */ "SCopy" OpHelp("r[P2]=r[P1]"),
26963 /* 32 */ "IntCopy" OpHelp("r[P2]=r[P1]"),
26964 /* 33 */ "ResultRow" OpHelp("output=r[P1@P2]"),
26965 /* 34 */ "CollSeq" OpHelp(""),
26966 /* 35 */ "Function0" OpHelp("r[P3]=func(r[P2@P5])"),
26967 /* 36 */ "Function" OpHelp("r[P3]=func(r[P2@P5])"),
26968 /* 37 */ "AddImm" OpHelp("r[P1]=r[P1]+P2"),
26969 /* 38 */ "MustBeInt" OpHelp(""),
26970 /* 39 */ "RealAffinity" OpHelp(""),
26971 /* 40 */ "Cast" OpHelp("affinity(r[P1])"),
26972 /* 41 */ "Permutation" OpHelp(""),
26973 /* 42 */ "Compare" OpHelp("r[P1@P3] <-> r[P2@P3]"),
26974 /* 43 */ "Jump" OpHelp(""),
26975 /* 44 */ "Once" OpHelp(""),
26976 /* 45 */ "If" OpHelp(""),
26977 /* 46 */ "IfNot" OpHelp(""),
26978 /* 47 */ "Column" OpHelp("r[P3]=PX"),
26979 /* 48 */ "Affinity" OpHelp("affinity(r[P1@P2])"),
26980 /* 49 */ "MakeRecord" OpHelp("r[P3]=mkrec(r[P1@P2])"),
26981 /* 50 */ "Count" OpHelp("r[P2]=count()"),
26982 /* 51 */ "ReadCookie" OpHelp(""),
26983 /* 52 */ "SetCookie" OpHelp(""),
26984 /* 53 */ "ReopenIdx" OpHelp("root=P2 iDb=P3"),
26985 /* 54 */ "OpenRead" OpHelp("root=P2 iDb=P3"),
26986 /* 55 */ "OpenWrite" OpHelp("root=P2 iDb=P3"),
26987 /* 56 */ "OpenAutoindex" OpHelp("nColumn=P2"),
26988 /* 57 */ "OpenEphemeral" OpHelp("nColumn=P2"),
26989 /* 58 */ "SorterOpen" OpHelp(""),
26990 /* 59 */ "SequenceTest" OpHelp("if( cursor[P1].ctr++ ) pc = P2"),
26991 /* 60 */ "OpenPseudo" OpHelp("P3 columns in r[P2]"),
26992 /* 61 */ "Close" OpHelp(""),
26993 /* 62 */ "ColumnsUsed" OpHelp(""),
26994 /* 63 */ "SeekLT" OpHelp("key=r[P3@P4]"),
26995 /* 64 */ "SeekLE" OpHelp("key=r[P3@P4]"),
26996 /* 65 */ "SeekGE" OpHelp("key=r[P3@P4]"),
26997 /* 66 */ "SeekGT" OpHelp("key=r[P3@P4]"),
26998 /* 67 */ "NoConflict" OpHelp("key=r[P3@P4]"),
26999 /* 68 */ "NotFound" OpHelp("key=r[P3@P4]"),
27000 /* 69 */ "Found" OpHelp("key=r[P3@P4]"),
27001 /* 70 */ "NotExists" OpHelp("intkey=r[P3]"),
27002 /* 71 */ "Or" OpHelp("r[P3]=(r[P1] || r[P2])"),
27003 /* 72 */ "And" OpHelp("r[P3]=(r[P1] && r[P2])"),
27004 /* 73 */ "Sequence" OpHelp("r[P2]=cursor[P1].ctr++"),
27005 /* 74 */ "NewRowid" OpHelp("r[P2]=rowid"),
27006 /* 75 */ "Insert" OpHelp("intkey=r[P3] data=r[P2]"),
27007 /* 76 */ "IsNull" OpHelp("if r[P1]==NULL goto P2"),
27008 /* 77 */ "NotNull" OpHelp("if r[P1]!=NULL goto P2"),
27009 /* 78 */ "Ne" OpHelp("if r[P1]!=r[P3] goto P2"),
27010 /* 79 */ "Eq" OpHelp("if r[P1]==r[P3] goto P2"),
27011 /* 80 */ "Gt" OpHelp("if r[P1]>r[P3] goto P2"),
27012 /* 81 */ "Le" OpHelp("if r[P1]<=r[P3] goto P2"),
27013 /* 82 */ "Lt" OpHelp("if r[P1]<r[P3] goto P2"),
27014 /* 83 */ "Ge" OpHelp("if r[P1]>=r[P3] goto P2"),
27015 /* 84 */ "InsertInt" OpHelp("intkey=P3 data=r[P2]"),
27016 /* 85 */ "BitAnd" OpHelp("r[P3]=r[P1]&r[P2]"),
27017 /* 86 */ "BitOr" OpHelp("r[P3]=r[P1]|r[P2]"),
27018 /* 87 */ "ShiftLeft" OpHelp("r[P3]=r[P2]<<r[P1]"),
27019 /* 88 */ "ShiftRight" OpHelp("r[P3]=r[P2]>>r[P1]"),
27020 /* 89 */ "Add" OpHelp("r[P3]=r[P1]+r[P2]"),
@@ -26891,78 +27021,77 @@
27021 /* 90 */ "Subtract" OpHelp("r[P3]=r[P2]-r[P1]"),
27022 /* 91 */ "Multiply" OpHelp("r[P3]=r[P1]*r[P2]"),
27023 /* 92 */ "Divide" OpHelp("r[P3]=r[P2]/r[P1]"),
27024 /* 93 */ "Remainder" OpHelp("r[P3]=r[P2]%r[P1]"),
27025 /* 94 */ "Concat" OpHelp("r[P3]=r[P2]+r[P1]"),
27026 /* 95 */ "Delete" OpHelp(""),
27027 /* 96 */ "BitNot" OpHelp("r[P1]= ~r[P1]"),
27028 /* 97 */ "String8" OpHelp("r[P2]='P4'"),
27029 /* 98 */ "ResetCount" OpHelp(""),
27030 /* 99 */ "SorterCompare" OpHelp("if key(P1)!=trim(r[P3],P4) goto P2"),
27031 /* 100 */ "SorterData" OpHelp("r[P2]=data"),
27032 /* 101 */ "RowKey" OpHelp("r[P2]=key"),
27033 /* 102 */ "RowData" OpHelp("r[P2]=data"),
27034 /* 103 */ "Rowid" OpHelp("r[P2]=rowid"),
27035 /* 104 */ "NullRow" OpHelp(""),
27036 /* 105 */ "Last" OpHelp(""),
27037 /* 106 */ "SorterSort" OpHelp(""),
27038 /* 107 */ "Sort" OpHelp(""),
27039 /* 108 */ "Rewind" OpHelp(""),
27040 /* 109 */ "SorterInsert" OpHelp(""),
27041 /* 110 */ "IdxInsert" OpHelp("key=r[P2]"),
27042 /* 111 */ "IdxDelete" OpHelp("key=r[P2@P3]"),
27043 /* 112 */ "Seek" OpHelp("Move P3 to P1.rowid"),
27044 /* 113 */ "IdxRowid" OpHelp("r[P2]=rowid"),
27045 /* 114 */ "IdxLE" OpHelp("key=r[P3@P4]"),
27046 /* 115 */ "IdxGT" OpHelp("key=r[P3@P4]"),
27047 /* 116 */ "IdxLT" OpHelp("key=r[P3@P4]"),
27048 /* 117 */ "IdxGE" OpHelp("key=r[P3@P4]"),
27049 /* 118 */ "Destroy" OpHelp(""),
27050 /* 119 */ "Clear" OpHelp(""),
27051 /* 120 */ "ResetSorter" OpHelp(""),
27052 /* 121 */ "CreateIndex" OpHelp("r[P2]=root iDb=P1"),
27053 /* 122 */ "CreateTable" OpHelp("r[P2]=root iDb=P1"),
27054 /* 123 */ "ParseSchema" OpHelp(""),
27055 /* 124 */ "LoadAnalysis" OpHelp(""),
27056 /* 125 */ "DropTable" OpHelp(""),
27057 /* 126 */ "DropIndex" OpHelp(""),
27058 /* 127 */ "DropTrigger" OpHelp(""),
27059 /* 128 */ "IntegrityCk" OpHelp(""),
27060 /* 129 */ "RowSetAdd" OpHelp("rowset(P1)=r[P2]"),
27061 /* 130 */ "RowSetRead" OpHelp("r[P3]=rowset(P1)"),
27062 /* 131 */ "RowSetTest" OpHelp("if r[P3] in rowset(P1) goto P2"),
27063 /* 132 */ "Program" OpHelp(""),
27064 /* 133 */ "Real" OpHelp("r[P2]=P4"),
27065 /* 134 */ "Param" OpHelp(""),
27066 /* 135 */ "FkCounter" OpHelp("fkctr[P1]+=P2"),
27067 /* 136 */ "FkIfZero" OpHelp("if fkctr[P1]==0 goto P2"),
27068 /* 137 */ "MemMax" OpHelp("r[P1]=max(r[P1],r[P2])"),
27069 /* 138 */ "IfPos" OpHelp("if r[P1]>0 then r[P1]-=P3, goto P2"),
27070 /* 139 */ "OffsetLimit" OpHelp("if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)"),
27071 /* 140 */ "IfNotZero" OpHelp("if r[P1]!=0 then r[P1]-=P3, goto P2"),
27072 /* 141 */ "DecrJumpZero" OpHelp("if (--r[P1])==0 goto P2"),
27073 /* 142 */ "JumpZeroIncr" OpHelp("if (r[P1]++)==0 ) goto P2"),
27074 /* 143 */ "AggStep0" OpHelp("accum=r[P3] step(r[P2@P5])"),
27075 /* 144 */ "AggStep" OpHelp("accum=r[P3] step(r[P2@P5])"),
27076 /* 145 */ "AggFinal" OpHelp("accum=r[P1] N=P2"),
27077 /* 146 */ "IncrVacuum" OpHelp(""),
27078 /* 147 */ "Expire" OpHelp(""),
27079 /* 148 */ "TableLock" OpHelp("iDb=P1 root=P2 write=P3"),
27080 /* 149 */ "VBegin" OpHelp(""),
27081 /* 150 */ "VCreate" OpHelp(""),
27082 /* 151 */ "VDestroy" OpHelp(""),
27083 /* 152 */ "VOpen" OpHelp(""),
27084 /* 153 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
27085 /* 154 */ "VNext" OpHelp(""),
27086 /* 155 */ "VRename" OpHelp(""),
27087 /* 156 */ "Pagecount" OpHelp(""),
27088 /* 157 */ "MaxPgcnt" OpHelp(""),
27089 /* 158 */ "Init" OpHelp("Start at P2"),
27090 /* 159 */ "CursorHint" OpHelp(""),
27091 /* 160 */ "Noop" OpHelp(""),
27092 /* 161 */ "Explain" OpHelp(""),
 
27093 };
27094 return azName[i];
27095 }
27096 #endif
27097
@@ -27117,10 +27246,15 @@
27246 /*
27247 ** Maximum supported path-length.
27248 */
27249 #define MAX_PATHNAME 512
27250
27251 /*
27252 ** Maximum supported symbolic links
27253 */
27254 #define SQLITE_MAX_SYMLINKS 100
27255
27256 /* Always cast the getpid() return type for compatibility with
27257 ** kernel modules in VxWorks. */
27258 #define osGetpid(X) (pid_t)getpid()
27259
27260 /*
@@ -27269,12 +27403,12 @@
27403 ** Macros for performance tracing. Normally turned off. Only works
27404 ** on i486 hardware.
27405 */
27406 #ifdef SQLITE_PERFORMANCE_TRACE
27407
27408 /*
27409 ** hwtime.h contains inline assembler code for implementing
27410 ** high-performance timing routines.
27411 */
27412 /************** Include hwtime.h in the middle of os_common.h ****************/
27413 /************** Begin file hwtime.h ******************************************/
27414 /*
@@ -27380,18 +27514,18 @@
27514 /*
27515 ** If we compile with the SQLITE_TEST macro set, then the following block
27516 ** of code will give us the ability to simulate a disk I/O error. This
27517 ** is used for testing the I/O recovery logic.
27518 */
27519 #if defined(SQLITE_TEST)
27520 SQLITE_API extern int sqlite3_io_error_hit;
27521 SQLITE_API extern int sqlite3_io_error_hardhit;
27522 SQLITE_API extern int sqlite3_io_error_pending;
27523 SQLITE_API extern int sqlite3_io_error_persist;
27524 SQLITE_API extern int sqlite3_io_error_benign;
27525 SQLITE_API extern int sqlite3_diskfull_pending;
27526 SQLITE_API extern int sqlite3_diskfull;
27527 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
27528 #define SimulateIOError(CODE) \
27529 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
27530 || sqlite3_io_error_pending-- == 1 ) \
27531 { local_ioerr(); CODE; }
@@ -27413,21 +27547,21 @@
27547 }
27548 #else
27549 #define SimulateIOErrorBenign(X)
27550 #define SimulateIOError(A)
27551 #define SimulateDiskfullError(A)
27552 #endif /* defined(SQLITE_TEST) */
27553
27554 /*
27555 ** When testing, keep a count of the number of open files.
27556 */
27557 #if defined(SQLITE_TEST)
27558 SQLITE_API extern int sqlite3_open_file_count;
27559 #define OpenCounter(X) sqlite3_open_file_count+=(X)
27560 #else
27561 #define OpenCounter(X)
27562 #endif /* defined(SQLITE_TEST) */
27563
27564 #endif /* !defined(_OS_COMMON_H_) */
27565
27566 /************** End of os_common.h *******************************************/
27567 /************** Continuing where we left off in os_unix.c ********************/
@@ -27641,10 +27775,16 @@
27775 #else
27776 { "readlink", (sqlite3_syscall_ptr)0, 0 },
27777 #endif
27778 #define osReadlink ((ssize_t(*)(const char*,char*,size_t))aSyscall[26].pCurrent)
27779
27780 #if defined(HAVE_LSTAT)
27781 { "lstat", (sqlite3_syscall_ptr)lstat, 0 },
27782 #else
27783 { "lstat", (sqlite3_syscall_ptr)0, 0 },
27784 #endif
27785 #define osLstat ((int(*)(const char*,struct stat*))aSyscall[27].pCurrent)
27786
27787 }; /* End of the overrideable system calls */
27788
27789
27790 /*
@@ -33042,16 +33182,11 @@
33182 #ifndef SQLITE_DISABLE_DIRSYNC
33183 if( (dirSync & 1)!=0 ){
33184 int fd;
33185 rc = osOpenDirectory(zPath, &fd);
33186 if( rc==SQLITE_OK ){
33187 if( full_fsync(fd,0,0) ){
 
 
 
 
 
33188 rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
33189 }
33190 robust_close(0, fd, __LINE__);
33191 }else{
33192 assert( rc==SQLITE_CANTOPEN );
@@ -33093,10 +33228,36 @@
33228 *pResOut = osAccess(zPath, W_OK|R_OK)==0;
33229 }
33230 return SQLITE_OK;
33231 }
33232
33233 /*
33234 **
33235 */
33236 static int mkFullPathname(
33237 const char *zPath, /* Input path */
33238 char *zOut, /* Output buffer */
33239 int nOut /* Allocated size of buffer zOut */
33240 ){
33241 int nPath = sqlite3Strlen30(zPath);
33242 int iOff = 0;
33243 if( zPath[0]!='/' ){
33244 if( osGetcwd(zOut, nOut-2)==0 ){
33245 return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
33246 }
33247 iOff = sqlite3Strlen30(zOut);
33248 zOut[iOff++] = '/';
33249 }
33250 if( (iOff+nPath+1)>nOut ){
33251 /* SQLite assumes that xFullPathname() nul-terminates the output buffer
33252 ** even if it returns an error. */
33253 zOut[iOff] = '\0';
33254 return SQLITE_CANTOPEN_BKPT;
33255 }
33256 sqlite3_snprintf(nOut-iOff, &zOut[iOff], "%s", zPath);
33257 return SQLITE_OK;
33258 }
33259
33260 /*
33261 ** Turn a relative pathname into a full pathname. The relative path
33262 ** is stored as a nul-terminated string in the buffer pointed to by
33263 ** zPath.
@@ -33109,73 +33270,85 @@
33270 sqlite3_vfs *pVfs, /* Pointer to vfs object */
33271 const char *zPath, /* Possibly relative input path */
33272 int nOut, /* Size of output buffer in bytes */
33273 char *zOut /* Output buffer */
33274 ){
33275 #if !defined(HAVE_READLINK) || !defined(HAVE_LSTAT)
33276 return mkFullPathname(zPath, zOut, nOut);
33277 #else
33278 int rc = SQLITE_OK;
33279 int nByte;
33280 int nLink = 1; /* Number of symbolic links followed so far */
33281 const char *zIn = zPath; /* Input path for each iteration of loop */
33282 char *zDel = 0;
33283
33284 assert( pVfs->mxPathname==MAX_PATHNAME );
33285 UNUSED_PARAMETER(pVfs);
33286
33287 /* It's odd to simulate an io-error here, but really this is just
33288 ** using the io-error infrastructure to test that SQLite handles this
33289 ** function failing. This function could fail if, for example, the
33290 ** current working directory has been unlinked.
33291 */
33292 SimulateIOError( return SQLITE_ERROR );
33293
33294 do {
33295
33296 /* Call stat() on path zIn. Set bLink to true if the path is a symbolic
33297 ** link, or false otherwise. */
33298 int bLink = 0;
33299 struct stat buf;
33300 if( osLstat(zIn, &buf)!=0 ){
33301 if( errno!=ENOENT ){
33302 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "lstat", zIn);
33303 }
33304 }else{
33305 bLink = S_ISLNK(buf.st_mode);
33306 }
33307
33308 if( bLink ){
33309 if( zDel==0 ){
33310 zDel = sqlite3_malloc(nOut);
33311 if( zDel==0 ) rc = SQLITE_NOMEM;
33312 }else if( ++nLink>SQLITE_MAX_SYMLINKS ){
33313 rc = SQLITE_CANTOPEN_BKPT;
33314 }
33315
33316 if( rc==SQLITE_OK ){
33317 nByte = osReadlink(zIn, zDel, nOut-1);
33318 if( nByte<0 ){
33319 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "readlink", zIn);
33320 }else{
33321 if( zDel[0]!='/' ){
33322 int n;
33323 for(n = sqlite3Strlen30(zIn); n>0 && zIn[n-1]!='/'; n--);
33324 if( nByte+n+1>nOut ){
33325 rc = SQLITE_CANTOPEN_BKPT;
33326 }else{
33327 memmove(&zDel[n], zDel, nByte+1);
33328 memcpy(zDel, zIn, n);
33329 nByte += n;
33330 }
33331 }
33332 zDel[nByte] = '\0';
33333 }
33334 }
33335
33336 zIn = zDel;
33337 }
33338
33339 assert( rc!=SQLITE_OK || zIn!=zOut || zIn[0]=='/' );
33340 if( rc==SQLITE_OK && zIn!=zOut ){
33341 rc = mkFullPathname(zIn, zOut, nOut);
33342 }
33343 if( bLink==0 ) break;
33344 zIn = zOut;
33345 }while( rc==SQLITE_OK );
33346
33347 sqlite3_free(zDel);
33348 return rc;
33349 #endif /* HAVE_READLINK && HAVE_LSTAT */
33350 }
33351
33352
33353 #ifndef SQLITE_OMIT_LOAD_EXTENSION
33354 /*
@@ -33353,11 +33526,11 @@
33526 #endif
33527 UNUSED_PARAMETER(NotUsed);
33528 return rc;
33529 }
33530
33531 #ifndef SQLITE_OMIT_DEPRECATED
33532 /*
33533 ** Find the current time (in Universal Coordinated Time). Write the
33534 ** current time and date as a Julian Day number into *prNow and
33535 ** return 0. Return 1 if the time and date cannot be found.
33536 */
@@ -33371,11 +33544,11 @@
33544 }
33545 #else
33546 # define unixCurrentTime 0
33547 #endif
33548
33549 #ifndef SQLITE_OMIT_DEPRECATED
33550 /*
33551 ** We added the xGetLastError() method with the intention of providing
33552 ** better low-level error messages when operating-system problems come up
33553 ** during SQLite operation. But so far, none of that has been implemented
33554 ** in the core. So this routine is never called. For now, it is merely
@@ -34053,11 +34226,11 @@
34226 strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
34227 }
34228 writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
34229 robust_ftruncate(conchFile->h, writeSize);
34230 rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
34231 full_fsync(conchFile->h,0,0);
34232 /* If we created a new conch file (not just updated the contents of a
34233 ** valid conch file), try to match the permissions of the database
34234 */
34235 if( rc==SQLITE_OK && createConch ){
34236 struct stat buf;
@@ -34670,11 +34843,11 @@
34843 };
34844 unsigned int i; /* Loop counter */
34845
34846 /* Double-check that the aSyscall[] array has been constructed
34847 ** correctly. See ticket [bb3a86e890c8e96ab] */
34848 assert( ArraySize(aSyscall)==28 );
34849
34850 /* Register all VFSes defined in the aVfs[] array */
34851 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
34852 sqlite3_vfs_register(&aVfs[i], i==0);
34853 }
@@ -34753,12 +34926,12 @@
34926 ** Macros for performance tracing. Normally turned off. Only works
34927 ** on i486 hardware.
34928 */
34929 #ifdef SQLITE_PERFORMANCE_TRACE
34930
34931 /*
34932 ** hwtime.h contains inline assembler code for implementing
34933 ** high-performance timing routines.
34934 */
34935 /************** Include hwtime.h in the middle of os_common.h ****************/
34936 /************** Begin file hwtime.h ******************************************/
34937 /*
@@ -34864,18 +35037,18 @@
35037 /*
35038 ** If we compile with the SQLITE_TEST macro set, then the following block
35039 ** of code will give us the ability to simulate a disk I/O error. This
35040 ** is used for testing the I/O recovery logic.
35041 */
35042 #if defined(SQLITE_TEST)
35043 SQLITE_API extern int sqlite3_io_error_hit;
35044 SQLITE_API extern int sqlite3_io_error_hardhit;
35045 SQLITE_API extern int sqlite3_io_error_pending;
35046 SQLITE_API extern int sqlite3_io_error_persist;
35047 SQLITE_API extern int sqlite3_io_error_benign;
35048 SQLITE_API extern int sqlite3_diskfull_pending;
35049 SQLITE_API extern int sqlite3_diskfull;
35050 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
35051 #define SimulateIOError(CODE) \
35052 if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
35053 || sqlite3_io_error_pending-- == 1 ) \
35054 { local_ioerr(); CODE; }
@@ -34897,21 +35070,21 @@
35070 }
35071 #else
35072 #define SimulateIOErrorBenign(X)
35073 #define SimulateIOError(A)
35074 #define SimulateDiskfullError(A)
35075 #endif /* defined(SQLITE_TEST) */
35076
35077 /*
35078 ** When testing, keep a count of the number of open files.
35079 */
35080 #if defined(SQLITE_TEST)
35081 SQLITE_API extern int sqlite3_open_file_count;
35082 #define OpenCounter(X) sqlite3_open_file_count+=(X)
35083 #else
35084 #define OpenCounter(X)
35085 #endif /* defined(SQLITE_TEST) */
35086
35087 #endif /* !defined(_OS_COMMON_H_) */
35088
35089 /************** End of os_common.h *******************************************/
35090 /************** Continuing where we left off in os_win.c *********************/
@@ -34970,10 +35143,14 @@
35143
35144 #ifndef NTDDI_WINBLUE
35145 # define NTDDI_WINBLUE 0x06030000
35146 #endif
35147
35148 #ifndef NTDDI_WINTHRESHOLD
35149 # define NTDDI_WINTHRESHOLD 0x06040000
35150 #endif
35151
35152 /*
35153 ** Check to see if the GetVersionEx[AW] functions are deprecated on the
35154 ** target system. GetVersionEx was first deprecated in Win8.1.
35155 */
35156 #ifndef SQLITE_WIN32_GETVERSIONEX
@@ -34982,10 +35159,23 @@
35159 # else
35160 # define SQLITE_WIN32_GETVERSIONEX 1 /* GetVersionEx() is current */
35161 # endif
35162 #endif
35163
35164 /*
35165 ** Check to see if the CreateFileMappingA function is supported on the
35166 ** target system. It is unavailable when using "mincore.lib" on Win10.
35167 ** When compiling for Windows 10, always assume "mincore.lib" is in use.
35168 */
35169 #ifndef SQLITE_WIN32_CREATEFILEMAPPINGA
35170 # if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINTHRESHOLD
35171 # define SQLITE_WIN32_CREATEFILEMAPPINGA 0
35172 # else
35173 # define SQLITE_WIN32_CREATEFILEMAPPINGA 1
35174 # endif
35175 #endif
35176
35177 /*
35178 ** This constant should already be defined (in the "WinDef.h" SDK file).
35179 */
35180 #ifndef MAX_PATH
35181 # define MAX_PATH (260)
@@ -35388,12 +35578,13 @@
35578 #endif
35579
35580 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
35581 LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
35582
35583 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
35584 (!defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0) && \
35585 SQLITE_WIN32_CREATEFILEMAPPINGA
35586 { "CreateFileMappingA", (SYSCALL)CreateFileMappingA, 0 },
35587 #else
35588 { "CreateFileMappingA", (SYSCALL)0, 0 },
35589 #endif
35590
@@ -35619,22 +35810,21 @@
35810 { "GetTickCount", (SYSCALL)0, 0 },
35811 #endif
35812
35813 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
35814
35815 #if defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_GETVERSIONEX
 
35816 { "GetVersionExA", (SYSCALL)GetVersionExA, 0 },
35817 #else
35818 { "GetVersionExA", (SYSCALL)0, 0 },
35819 #endif
35820
35821 #define osGetVersionExA ((BOOL(WINAPI*)( \
35822 LPOSVERSIONINFOA))aSyscall[34].pCurrent)
35823
35824 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
35825 SQLITE_WIN32_GETVERSIONEX
35826 { "GetVersionExW", (SYSCALL)GetVersionExW, 0 },
35827 #else
35828 { "GetVersionExW", (SYSCALL)0, 0 },
35829 #endif
35830
@@ -36241,11 +36431,11 @@
36431 ** this routine is used to determine if the host is Win95/98/ME or
36432 ** WinNT/2K/XP so that we will know whether or not we can safely call
36433 ** the LockFileEx() API.
36434 */
36435
36436 #if !SQLITE_WIN32_GETVERSIONEX
36437 # define osIsNT() (1)
36438 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
36439 # define osIsNT() (1)
36440 #elif !defined(SQLITE_WIN32_HAS_WIDE)
36441 # define osIsNT() (0)
@@ -36262,11 +36452,11 @@
36452 /*
36453 ** NOTE: The WinRT sub-platform is always assumed to be based on the NT
36454 ** kernel.
36455 */
36456 return 1;
36457 #elif SQLITE_WIN32_GETVERSIONEX
36458 if( osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0 ){
36459 #if defined(SQLITE_WIN32_HAS_ANSI)
36460 OSVERSIONINFOA sInfo;
36461 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
36462 osGetVersionExA(&sInfo);
@@ -38846,11 +39036,11 @@
39036 );
39037 #elif defined(SQLITE_WIN32_HAS_WIDE)
39038 hMap = osCreateFileMappingW(pShmNode->hFile.h,
39039 NULL, PAGE_READWRITE, 0, nByte, NULL
39040 );
39041 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
39042 hMap = osCreateFileMappingA(pShmNode->hFile.h,
39043 NULL, PAGE_READWRITE, 0, nByte, NULL
39044 );
39045 #endif
39046 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
@@ -39002,11 +39192,11 @@
39192 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
39193 #elif defined(SQLITE_WIN32_HAS_WIDE)
39194 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
39195 (DWORD)((nMap>>32) & 0xffffffff),
39196 (DWORD)(nMap & 0xffffffff), NULL);
39197 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
39198 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
39199 (DWORD)((nMap>>32) & 0xffffffff),
39200 (DWORD)(nMap & 0xffffffff), NULL);
39201 #endif
39202 if( pFd->hMap==NULL ){
@@ -43066,11 +43256,11 @@
43256 */
43257 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
43258 assert( p!=0 );
43259 if( p->nFresh==0 ){
43260 struct RowSetChunk *pNew;
43261 pNew = sqlite3DbMallocRawNN(p->db, sizeof(*pNew));
43262 if( pNew==0 ){
43263 return 0;
43264 }
43265 pNew->pNextChunk = p->pChunk;
43266 p->pChunk = pNew;
@@ -43973,10 +44163,24 @@
44163 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
44164 ** This could conceivably cause corruption following a power failure on
44165 ** such a system. This is currently an undocumented limit.
44166 */
44167 #define MAX_SECTOR_SIZE 0x10000
44168
44169 /*
44170 ** If the option SQLITE_EXTRA_DURABLE option is set at compile-time, then
44171 ** SQLite will do extra fsync() operations when synchronous==FULL to help
44172 ** ensure that transactions are durable across a power failure. Most
44173 ** applications are happy as long as transactions are consistent across
44174 ** a power failure, and are perfectly willing to lose the last transaction
44175 ** in exchange for the extra performance of avoiding directory syncs.
44176 ** And so the default SQLITE_EXTRA_DURABLE setting is off.
44177 */
44178 #ifndef SQLITE_EXTRA_DURABLE
44179 # define SQLITE_EXTRA_DURABLE 0
44180 #endif
44181
44182
44183 /*
44184 ** An instance of the following structure is allocated for each active
44185 ** savepoint and statement transaction in the system. All such structures
44186 ** are stored in the Pager.aSavepoint[] array, which is allocated and
@@ -44169,10 +44373,11 @@
44373 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
44374 u8 journalMode; /* One of the PAGER_JOURNALMODE_* values */
44375 u8 useJournal; /* Use a rollback journal on this file */
44376 u8 noSync; /* Do not sync the journal if true */
44377 u8 fullSync; /* Do extra syncs of the journal for robustness */
44378 u8 extraSync; /* sync directory after journal delete */
44379 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
44380 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
44381 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
44382 u8 tempFile; /* zFilename is a temporary or immutable file */
44383 u8 noLock; /* Do not lock (except in WAL mode) */
@@ -45529,11 +45734,11 @@
45734 || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
45735 || pPager->journalMode==PAGER_JOURNALMODE_WAL
45736 );
45737 sqlite3OsClose(pPager->jfd);
45738 if( bDelete ){
45739 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, pPager->extraSync);
45740 }
45741 }
45742 }
45743
45744 #ifdef SQLITE_CHECK_PAGES
@@ -47035,13 +47240,19 @@
47240 SQLITE_PRIVATE void sqlite3PagerSetFlags(
47241 Pager *pPager, /* The pager to set safety level for */
47242 unsigned pgFlags /* Various flags */
47243 ){
47244 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
47245 if( pPager->tempFile ){
47246 pPager->noSync = 1;
47247 pPager->fullSync = 0;
47248 pPager->extraSync = 0;
47249 }else{
47250 pPager->noSync = level==PAGER_SYNCHRONOUS_OFF ?1:0;
47251 pPager->fullSync = level>=PAGER_SYNCHRONOUS_FULL ?1:0;
47252 pPager->extraSync = level==PAGER_SYNCHRONOUS_EXTRA ?1:0;
47253 }
47254 if( pPager->noSync ){
47255 pPager->syncFlags = 0;
47256 pPager->ckptSyncFlags = 0;
47257 }else if( pgFlags & PAGER_FULLFSYNC ){
47258 pPager->syncFlags = SQLITE_SYNC_FULL;
@@ -48342,15 +48553,21 @@
48553 pPager->readOnly = (u8)readOnly;
48554 assert( useJournal || pPager->tempFile );
48555 pPager->noSync = pPager->tempFile;
48556 if( pPager->noSync ){
48557 assert( pPager->fullSync==0 );
48558 assert( pPager->extraSync==0 );
48559 assert( pPager->syncFlags==0 );
48560 assert( pPager->walSyncFlags==0 );
48561 assert( pPager->ckptSyncFlags==0 );
48562 }else{
48563 pPager->fullSync = 1;
48564 #if SQLITE_EXTRA_DURABLE
48565 pPager->extraSync = 1;
48566 #else
48567 pPager->extraSync = 0;
48568 #endif
48569 pPager->syncFlags = SQLITE_SYNC_NORMAL;
48570 pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
48571 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
48572 }
48573 /* pPager->pFirst = 0; */
@@ -53972,14 +54189,20 @@
54189 u32 iWrite = 0;
54190 VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite);
54191 assert( rc==SQLITE_OK || iWrite==0 );
54192 if( iWrite>=iFirst ){
54193 i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE;
54194 void *pData;
54195 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
54196 pWal->iReCksum = iWrite;
54197 }
54198 #if defined(SQLITE_HAS_CODEC)
54199 if( (pData = sqlite3PagerCodec(p))==0 ) return SQLITE_NOMEM;
54200 #else
54201 pData = p->pData;
54202 #endif
54203 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
54204 if( rc ) return rc;
54205 p->flags &= ~PGHDR_WAL_APPEND;
54206 continue;
54207 }
54208 }
@@ -57680,11 +57903,10 @@
57903 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
57904 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
57905 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
57906 if( pBt->mutex==0 ){
57907 rc = SQLITE_NOMEM;
 
57908 goto btree_open_out;
57909 }
57910 }
57911 sqlite3_mutex_enter(mutexShared);
57912 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
@@ -59391,17 +59613,17 @@
59613 ** iTable. If a read-only cursor is requested, it is assumed that
59614 ** the caller already has at least a read-only transaction open
59615 ** on the database already. If a write-cursor is requested, then
59616 ** the caller is assumed to have an open write transaction.
59617 **
59618 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only
59619 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor
59620 ** can be used for reading or for writing if other conditions for writing
59621 ** are also met. These are the conditions that must be met in order
59622 ** for writing to be allowed:
59623 **
59624 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR
59625 **
59626 ** 2: Other database connections that share the same pager cache
59627 ** but which are not in the READ_UNCOMMITTED state may not have
59628 ** cursors open with wrFlag==0 on the same table. Otherwise
59629 ** the changes made by this write cursor would be visible to
@@ -59408,10 +59630,20 @@
59630 ** the read cursors in the other database connection.
59631 **
59632 ** 3: The database must be writable (not on read-only media)
59633 **
59634 ** 4: There must be an active transaction.
59635 **
59636 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR
59637 ** is set. If FORDELETE is set, that is a hint to the implementation that
59638 ** this cursor will only be used to seek to and delete entries of an index
59639 ** as part of a larger DELETE statement. The FORDELETE hint is not used by
59640 ** this implementation. But in a hypothetical alternative storage engine
59641 ** in which index entries are automatically deleted when corresponding table
59642 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE
59643 ** operations on this cursor can be no-ops and all READ operations can
59644 ** return a null row (2-bytes: 0x01 0x00).
59645 **
59646 ** No checking is done to make sure that page iTable really is the
59647 ** root page of a b-tree. If it is not, then the cursor acquired
59648 ** will not work correctly.
59649 **
@@ -61481,11 +61713,11 @@
61713 */
61714 #if SQLITE_DEBUG
61715 {
61716 CellInfo info;
61717 pPage->xParseCell(pPage, pCell, &info);
61718 assert( nHeader==(int)(info.pPayload - pCell) );
61719 assert( info.nKey==nKey );
61720 assert( *pnSize == info.nSize );
61721 assert( spaceLeft == info.nLocal );
61722 }
61723 #endif
@@ -63140,12 +63372,12 @@
63372 int rc = SQLITE_OK;
63373 const int nMin = pCur->pBt->usableSize * 2 / 3;
63374 u8 aBalanceQuickSpace[13];
63375 u8 *pFree = 0;
63376
63377 VVA_ONLY( int balance_quick_called = 0 );
63378 VVA_ONLY( int balance_deeper_called = 0 );
63379
63380 do {
63381 int iPage = pCur->iPage;
63382 MemPage *pPage = pCur->apPage[iPage];
63383
@@ -63154,11 +63386,12 @@
63386 /* The root page of the b-tree is overfull. In this case call the
63387 ** balance_deeper() function to create a new child for the root-page
63388 ** and copy the current contents of the root-page to it. The
63389 ** next iteration of the do-loop will balance the child page.
63390 */
63391 assert( balance_deeper_called==0 );
63392 VVA_ONLY( balance_deeper_called++ );
63393 rc = balance_deeper(pPage, &pCur->apPage[1]);
63394 if( rc==SQLITE_OK ){
63395 pCur->iPage = 1;
63396 pCur->aiIdx[0] = 0;
63397 pCur->aiIdx[1] = 0;
@@ -63193,11 +63426,12 @@
63426 ** The purpose of the following assert() is to check that only a
63427 ** single call to balance_quick() is made for each call to this
63428 ** function. If this were not verified, a subtle bug involving reuse
63429 ** of the aBalanceQuickSpace[] might sneak in.
63430 */
63431 assert( balance_quick_called==0 );
63432 VVA_ONLY( balance_quick_called++ );
63433 rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
63434 }else
63435 #endif
63436 {
63437 /* In this case, call balance_nonroot() to redistribute cells
@@ -63424,35 +63658,45 @@
63658 }
63659
63660 /*
63661 ** Delete the entry that the cursor is pointing to.
63662 **
63663 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then
63664 ** the cursor is left pointing at an arbitrary location after the delete.
63665 ** But if that bit is set, then the cursor is left in a state such that
63666 ** the next call to BtreeNext() or BtreePrev() moves it to the same row
63667 ** as it would have been on if the call to BtreeDelete() had been omitted.
63668 **
63669 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes
63670 ** associated with a single table entry and its indexes. Only one of those
63671 ** deletes is considered the "primary" delete. The primary delete occurs
63672 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete
63673 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag.
63674 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation,
63675 ** but which might be used by alternative storage engines.
63676 */
63677 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){
63678 Btree *p = pCur->pBtree;
63679 BtShared *pBt = p->pBt;
63680 int rc; /* Return code */
63681 MemPage *pPage; /* Page to delete cell from */
63682 unsigned char *pCell; /* Pointer to cell to delete */
63683 int iCellIdx; /* Index of cell to delete */
63684 int iCellDepth; /* Depth of node containing pCell */
63685 u16 szCell; /* Size of the cell being deleted */
63686 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */
63687 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */
63688
63689 assert( cursorOwnsBtShared(pCur) );
63690 assert( pBt->inTransaction==TRANS_WRITE );
63691 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
63692 assert( pCur->curFlags & BTCF_WriteFlag );
63693 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
63694 assert( !hasReadConflicts(p, pCur->pgnoRoot) );
63695 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
63696 assert( pCur->eState==CURSOR_VALID );
63697 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 );
63698
63699 iCellDepth = pCur->iPage;
63700 iCellIdx = pCur->aiIdx[iCellDepth];
63701 pPage = pCur->apPage[iCellDepth];
63702 pCell = findCell(pPage, iCellIdx);
@@ -63561,11 +63805,11 @@
63805 }
63806
63807 if( rc==SQLITE_OK ){
63808 if( bSkipnext ){
63809 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) );
63810 assert( pPage==pCur->apPage[pCur->iPage] || CORRUPT_DB );
63811 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell );
63812 pCur->eState = CURSOR_SKIPNEXT;
63813 if( iCellIdx>=pPage->nCell ){
63814 pCur->skipNext = -1;
63815 pCur->aiIdx[iCellDepth] = pPage->nCell-1;
@@ -64147,13 +64391,13 @@
64391 va_start(ap, zFormat);
64392 if( pCheck->errMsg.nChar ){
64393 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
64394 }
64395 if( pCheck->zPfx ){
64396 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2);
64397 }
64398 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap);
64399 va_end(ap);
64400 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
64401 pCheck->mallocFailed = 1;
64402 }
64403 }
@@ -64650,11 +64894,12 @@
64894 char zErr[100];
64895 VVA_ONLY( int nRef );
64896
64897 sqlite3BtreeEnter(p);
64898 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
64899 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) );
64900 assert( nRef>=0 );
64901 sCheck.pBt = pBt;
64902 sCheck.pPager = pBt->pPager;
64903 sCheck.nPage = btreePagecount(sCheck.pBt);
64904 sCheck.mxErr = mxErr;
64905 sCheck.nErr = 0;
@@ -64663,10 +64908,11 @@
64908 sCheck.v1 = 0;
64909 sCheck.v2 = 0;
64910 sCheck.aPgRef = 0;
64911 sCheck.heap = 0;
64912 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
64913 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL;
64914 if( sCheck.nPage==0 ){
64915 goto integrity_ck_cleanup;
64916 }
64917
64918 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
@@ -65939,10 +66185,11 @@
66185 ** in pMem->z is discarded.
66186 */
66187 SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
66188 assert( sqlite3VdbeCheckMemInvariants(pMem) );
66189 assert( (pMem->flags&MEM_RowSet)==0 );
66190 testcase( pMem->db==0 );
66191
66192 /* If the bPreserve flag is set to true, then the memory cell must already
66193 ** contain a valid string or blob value. */
66194 assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
66195 testcase( bPreserve && pMem->z==0 );
@@ -66542,11 +66789,11 @@
66789 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
66790 sqlite3 *db = pMem->db;
66791 assert( db!=0 );
66792 assert( (pMem->flags & MEM_RowSet)==0 );
66793 sqlite3VdbeMemRelease(pMem);
66794 pMem->zMalloc = sqlite3DbMallocRawNN(db, 64);
66795 if( db->mallocFailed ){
66796 pMem->flags = MEM_Null;
66797 pMem->szMalloc = 0;
66798 }else{
66799 assert( pMem->zMalloc );
@@ -67204,11 +67451,11 @@
67451
67452 *ppVal = pVal;
67453 return rc;
67454
67455 no_mem:
67456 sqlite3OomFault(db);
67457 sqlite3DbFree(db, zVal);
67458 assert( *ppVal==0 );
67459 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
67460 if( pCtx==0 ) sqlite3ValueFree(pVal);
67461 #else
@@ -67263,11 +67510,11 @@
67510 iSerial = sqlite3VdbeSerialType(argv[0], file_format, &nVal);
67511 nSerial = sqlite3VarintLen(iSerial);
67512 db = sqlite3_context_db_handle(context);
67513
67514 nRet = 1 + nSerial + nVal;
67515 aRet = sqlite3DbMallocRawNN(db, nRet);
67516 if( aRet==0 ){
67517 sqlite3_result_error_nomem(context);
67518 }else{
67519 aRet[0] = nSerial+1;
67520 putVarint32(&aRet[1], iSerial);
@@ -67715,11 +67962,11 @@
67962 int i;
67963 VdbeOp *pOp;
67964
67965 i = p->nOp;
67966 assert( p->magic==VDBE_MAGIC_INIT );
67967 assert( op>=0 && op<0xff );
67968 if( p->pParse->nOpAlloc<=i ){
67969 return growOp3(p, op, p1, p2, p3);
67970 }
67971 p->nOp++;
67972 pOp = &p->aOp[i];
@@ -67833,11 +68080,11 @@
68080 int p2, /* The P2 operand */
68081 int p3, /* The P3 operand */
68082 const u8 *zP4, /* The P4 operand */
68083 int p4type /* P4 operand type */
68084 ){
68085 char *p4copy = sqlite3DbMallocRawNN(sqlite3VdbeDb(p), 8);
68086 if( p4copy ) memcpy(p4copy, zP4, 8);
68087 return sqlite3VdbeAddOp4(p, op, p1, p2, p3, p4copy, p4type);
68088 }
68089
68090 /*
@@ -67867,10 +68114,25 @@
68114 ){
68115 int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
68116 sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
68117 return addr;
68118 }
68119
68120 /* Insert the end of a co-routine
68121 */
68122 SQLITE_PRIVATE void sqlite3VdbeEndCoroutine(Vdbe *v, int regYield){
68123 sqlite3VdbeAddOp1(v, OP_EndCoroutine, regYield);
68124
68125 /* Clear the temporary register cache, thereby ensuring that each
68126 ** co-routine has its own independent set of registers, because co-routines
68127 ** might expect their registers to be preserved across an OP_Yield, and
68128 ** that could cause problems if two or more co-routines are using the same
68129 ** temporary register.
68130 */
68131 v->pParse->nTempReg = 0;
68132 v->pParse->nRangeReg = 0;
68133 }
68134
68135 /*
68136 ** Create a new symbolic label for an instruction that has yet to be
68137 ** coded. The symbolic label is really just a negative number. The
68138 ** label can be used as the P2 value of an operation. Later, when
@@ -68078,11 +68340,11 @@
68340 p->readOnly = 1;
68341 p->bIsReader = 0;
68342 for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
68343 u8 opcode = pOp->opcode;
68344
68345 /* NOTE: Be sure to update mkopcodeh.tcl when adding or removing
68346 ** cases from this switch! */
68347 switch( opcode ){
68348 case OP_Transaction: {
68349 if( pOp->p2!=0 ) p->readOnly = 0;
68350 /* fall thru */
@@ -68190,10 +68452,13 @@
68452 }
68453
68454 /*
68455 ** Add a whole list of operations to the operation stack. Return a
68456 ** pointer to the first operation inserted.
68457 **
68458 ** Non-zero P2 arguments to jump instructions are automatically adjusted
68459 ** so that the jump target is relative to the first operation inserted.
68460 */
68461 SQLITE_PRIVATE VdbeOp *sqlite3VdbeAddOpList(
68462 Vdbe *p, /* Add opcodes to the prepared statement */
68463 int nOp, /* Number of opcodes to add */
68464 VdbeOpList const *aOp, /* The opcodes to be added */
@@ -68210,10 +68475,13 @@
68475 for(i=0; i<nOp; i++, aOp++, pOut++){
68476 pOut->opcode = aOp->opcode;
68477 pOut->p1 = aOp->p1;
68478 pOut->p2 = aOp->p2;
68479 assert( aOp->p2>=0 );
68480 if( (sqlite3OpcodeProperty[aOp->opcode] & OPFLG_JUMP)!=0 && aOp->p2>0 ){
68481 pOut->p2 += p->nOp;
68482 }
68483 pOut->p3 = aOp->p3;
68484 pOut->p4type = P4_NOTUSED;
68485 pOut->p4.p = 0;
68486 pOut->p5 = 0;
68487 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
@@ -68661,32 +68929,31 @@
68929 #if VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS)
68930 /*
68931 ** Translate the P4.pExpr value for an OP_CursorHint opcode into text
68932 ** that can be displayed in the P4 column of EXPLAIN output.
68933 */
68934 static void displayP4Expr(StrAccum *p, Expr *pExpr){
68935 const char *zOp = 0;
 
68936 switch( pExpr->op ){
68937 case TK_STRING:
68938 sqlite3XPrintf(p, "%Q", pExpr->u.zToken);
68939 break;
68940 case TK_INTEGER:
68941 sqlite3XPrintf(p, "%d", pExpr->u.iValue);
68942 break;
68943 case TK_NULL:
68944 sqlite3XPrintf(p, "NULL");
68945 break;
68946 case TK_REGISTER: {
68947 sqlite3XPrintf(p, "r[%d]", pExpr->iTable);
68948 break;
68949 }
68950 case TK_COLUMN: {
68951 if( pExpr->iColumn<0 ){
68952 sqlite3XPrintf(p, "rowid");
68953 }else{
68954 sqlite3XPrintf(p, "c%d", (int)pExpr->iColumn);
68955 }
68956 break;
68957 }
68958 case TK_LT: zOp = "LT"; break;
68959 case TK_LE: zOp = "LE"; break;
@@ -68714,25 +68981,23 @@
68981 case TK_NOT: zOp = "NOT"; break;
68982 case TK_ISNULL: zOp = "ISNULL"; break;
68983 case TK_NOTNULL: zOp = "NOTNULL"; break;
68984
68985 default:
68986 sqlite3XPrintf(p, "%s", "expr");
68987 break;
68988 }
68989
68990 if( zOp ){
68991 sqlite3XPrintf(p, "%s(", zOp);
68992 displayP4Expr(p, pExpr->pLeft);
68993 if( pExpr->pRight ){
68994 sqlite3StrAccumAppend(p, ",", 1);
68995 displayP4Expr(p, pExpr->pRight);
68996 }
68997 sqlite3StrAccumAppend(p, ")", 1);
68998 }
 
 
68999 }
69000 #endif /* VDBE_DISPLAY_P4 && defined(SQLITE_ENABLE_CURSOR_HINTS) */
69001
69002
69003 #if VDBE_DISPLAY_P4
@@ -68740,107 +69005,100 @@
69005 ** Compute a string that describes the P4 parameter for an opcode.
69006 ** Use zTemp for any required temporary buffer space.
69007 */
69008 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
69009 char *zP4 = zTemp;
69010 StrAccum x;
69011 assert( nTemp>=20 );
69012 sqlite3StrAccumInit(&x, 0, zTemp, nTemp, 0);
69013 switch( pOp->p4type ){
69014 case P4_KEYINFO: {
69015 int j;
69016 KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
69017 assert( pKeyInfo->aSortOrder!=0 );
69018 sqlite3XPrintf(&x, "k(%d", pKeyInfo->nField);
 
69019 for(j=0; j<pKeyInfo->nField; j++){
69020 CollSeq *pColl = pKeyInfo->aColl[j];
69021 const char *zColl = pColl ? pColl->zName : "";
69022 if( strcmp(zColl, "BINARY")==0 ) zColl = "B";
69023 sqlite3XPrintf(&x, ",%s%s", pKeyInfo->aSortOrder[j] ? "-" : "", zColl);
69024 }
69025 sqlite3StrAccumAppend(&x, ")", 1);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69026 break;
69027 }
69028 #ifdef SQLITE_ENABLE_CURSOR_HINTS
69029 case P4_EXPR: {
69030 displayP4Expr(&x, pOp->p4.pExpr);
69031 break;
69032 }
69033 #endif
69034 case P4_COLLSEQ: {
69035 CollSeq *pColl = pOp->p4.pColl;
69036 sqlite3XPrintf(&x, "(%.20s)", pColl->zName);
69037 break;
69038 }
69039 case P4_FUNCDEF: {
69040 FuncDef *pDef = pOp->p4.pFunc;
69041 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
69042 break;
69043 }
69044 #ifdef SQLITE_DEBUG
69045 case P4_FUNCCTX: {
69046 FuncDef *pDef = pOp->p4.pCtx->pFunc;
69047 sqlite3XPrintf(&x, "%s(%d)", pDef->zName, pDef->nArg);
69048 break;
69049 }
69050 #endif
69051 case P4_INT64: {
69052 sqlite3XPrintf(&x, "%lld", *pOp->p4.pI64);
69053 break;
69054 }
69055 case P4_INT32: {
69056 sqlite3XPrintf(&x, "%d", pOp->p4.i);
69057 break;
69058 }
69059 case P4_REAL: {
69060 sqlite3XPrintf(&x, "%.16g", *pOp->p4.pReal);
69061 break;
69062 }
69063 case P4_MEM: {
69064 Mem *pMem = pOp->p4.pMem;
69065 if( pMem->flags & MEM_Str ){
69066 zP4 = pMem->z;
69067 }else if( pMem->flags & MEM_Int ){
69068 sqlite3XPrintf(&x, "%lld", pMem->u.i);
69069 }else if( pMem->flags & MEM_Real ){
69070 sqlite3XPrintf(&x, "%.16g", pMem->u.r);
69071 }else if( pMem->flags & MEM_Null ){
69072 zP4 = "NULL";
69073 }else{
69074 assert( pMem->flags & MEM_Blob );
69075 zP4 = "(blob)";
69076 }
69077 break;
69078 }
69079 #ifndef SQLITE_OMIT_VIRTUALTABLE
69080 case P4_VTAB: {
69081 sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
69082 sqlite3XPrintf(&x, "vtab:%p", pVtab);
69083 break;
69084 }
69085 #endif
69086 case P4_INTARRAY: {
69087 int i;
69088 int *ai = pOp->p4.ai;
69089 int n = ai[0]; /* The first element of an INTARRAY is always the
69090 ** count of the number of elements to follow */
69091 for(i=1; i<n; i++){
69092 sqlite3XPrintf(&x, ",%d", ai[i]);
69093 }
69094 zTemp[0] = '[';
69095 sqlite3StrAccumAppend(&x, "]", 1);
69096 break;
69097 }
69098 case P4_SUBPROGRAM: {
69099 sqlite3XPrintf(&x, "program");
69100 break;
69101 }
69102 case P4_ADVANCE: {
69103 zTemp[0] = 0;
69104 break;
@@ -68851,10 +69109,11 @@
69109 zP4 = zTemp;
69110 zTemp[0] = 0;
69111 }
69112 }
69113 }
69114 sqlite3StrAccumFinish(&x);
69115 assert( zP4!=0 );
69116 return zP4;
69117 }
69118 #endif /* VDBE_DISPLAY_P4 */
69119
@@ -68970,11 +69229,10 @@
69229 */
69230 static void releaseMemArray(Mem *p, int N){
69231 if( p && N ){
69232 Mem *pEnd = &p[N];
69233 sqlite3 *db = p->db;
 
69234 if( db->pnBytesFreed ){
69235 do{
69236 if( p->szMalloc ) sqlite3DbFree(db, p->zMalloc);
69237 }while( (++p)<pEnd );
69238 return;
@@ -69006,11 +69264,10 @@
69264 p->szMalloc = 0;
69265 }
69266
69267 p->flags = MEM_Undefined;
69268 }while( (++p)<pEnd );
 
69269 }
69270 }
69271
69272 /*
69273 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
@@ -69067,11 +69324,11 @@
69324 p->pResultSet = 0;
69325
69326 if( p->rc==SQLITE_NOMEM ){
69327 /* This happens if a malloc() inside a call to sqlite3_column_text() or
69328 ** sqlite3_column_text16() failed. */
69329 sqlite3OomFault(db);
69330 return SQLITE_ERROR;
69331 }
69332
69333 /* When the number of output rows reaches nRow, that means the
69334 ** listing has finished and sqlite3_step() should return SQLITE_DONE.
@@ -69265,45 +69522,47 @@
69522 sqlite3IoTrace("SQL %s\n", z);
69523 }
69524 }
69525 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
69526
69527 /* An instance of this object describes bulk memory available for use
69528 ** by subcomponents of a prepared statement. Space is allocated out
69529 ** of a ReusableSpace object by the allocSpace() routine below.
69530 */
69531 struct ReusableSpace {
69532 u8 *pSpace; /* Available memory */
69533 int nFree; /* Bytes of available memory */
69534 int nNeeded; /* Total bytes that could not be allocated */
69535 };
69536
69537 /* Try to allocate nByte bytes of 8-byte aligned bulk memory for pBuf
69538 ** from the ReusableSpace object. Return a pointer to the allocated
69539 ** memory on success. If insufficient memory is available in the
69540 ** ReusableSpace object, increase the ReusableSpace.nNeeded
69541 ** value by the amount needed and return NULL.
69542 **
69543 ** If pBuf is not initially NULL, that means that the memory has already
69544 ** been allocated by a prior call to this routine, so just return a copy
69545 ** of pBuf and leave ReusableSpace unchanged.
69546 **
69547 ** This allocator is employed to repurpose unused slots at the end of the
69548 ** opcode array of prepared state for other memory needs of the prepared
69549 ** statement.
69550 */
69551 static void *allocSpace(
69552 struct ReusableSpace *p, /* Bulk memory available for allocation */
69553 void *pBuf, /* Pointer to a prior allocation */
69554 int nByte /* Bytes of memory needed */
 
 
69555 ){
69556 assert( EIGHT_BYTE_ALIGNMENT(p->pSpace) );
69557 if( pBuf==0 ){
69558 nByte = ROUND8(nByte);
69559 if( nByte <= p->nFree ){
69560 p->nFree -= nByte;
69561 pBuf = &p->pSpace[p->nFree];
69562 }else{
69563 p->nNeeded += nByte;
69564 }
69565 }
69566 assert( EIGHT_BYTE_ALIGNMENT(pBuf) );
69567 return pBuf;
69568 }
@@ -69332,11 +69591,10 @@
69591 }
69592 #endif
69593 p->pc = -1;
69594 p->rc = SQLITE_OK;
69595 p->errorAction = OE_Abort;
 
69596 p->nChange = 0;
69597 p->cacheCtr = 1;
69598 p->minWriteFileFormat = 255;
69599 p->iStatement = 0;
69600 p->nFkConstraint = 0;
@@ -69375,13 +69633,11 @@
69633 int nMem; /* Number of VM memory registers */
69634 int nCursor; /* Number of cursors required */
69635 int nArg; /* Number of arguments in subprograms */
69636 int nOnce; /* Number of OP_Once instructions */
69637 int n; /* Loop counter */
69638 struct ReusableSpace x; /* Reusable bulk memory */
 
 
69639
69640 assert( p!=0 );
69641 assert( p->nOp>0 );
69642 assert( pParse!=0 );
69643 assert( p->magic==VDBE_MAGIC_INIT );
@@ -69395,69 +69651,64 @@
69651 nOnce = pParse->nOnce;
69652 if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
69653
69654 /* For each cursor required, also allocate a memory cell. Memory
69655 ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
69656 ** the vdbe program. Instead they are used to allocate memory for
69657 ** VdbeCursor/BtCursor structures. The blob of memory associated with
69658 ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
69659 ** stores the blob of memory associated with cursor 1, etc.
69660 **
69661 ** See also: allocateCursor().
69662 */
69663 nMem += nCursor;
69664
69665 /* Figure out how much reusable memory is available at the end of the
69666 ** opcode array. This extra memory will be reallocated for other elements
69667 ** of the prepared statement.
 
 
69668 */
69669 n = ROUND8(sizeof(Op)*p->nOp); /* Bytes of opcode memory used */
69670 x.pSpace = &((u8*)p->aOp)[n]; /* Unused opcode memory */
69671 assert( EIGHT_BYTE_ALIGNMENT(x.pSpace) );
69672 x.nFree = ROUNDDOWN8(pParse->szOpAlloc - n); /* Bytes of unused memory */
69673 assert( x.nFree>=0 );
69674 if( x.nFree>0 ){
69675 memset(x.pSpace, 0, x.nFree);
69676 assert( EIGHT_BYTE_ALIGNMENT(&x.pSpace[x.nFree]) );
69677 }
69678
69679 resolveP2Values(p, &nArg);
69680 p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
69681 if( pParse->explain && nMem<10 ){
69682 nMem = 10;
69683 }
69684 p->expired = 0;
69685
69686 /* Memory for registers, parameters, cursor, etc, is allocated in one or two
69687 ** passes. On the first pass, we try to reuse unused memory at the
69688 ** end of the opcode array. If we are unable to satisfy all memory
69689 ** requirements by reusing the opcode array tail, then the second
69690 ** pass will fill in the remainder using a fresh memory allocation.
69691 **
69692 ** This two-pass approach that reuses as much memory as possible from
69693 ** the leftover memory at the end of the opcode array. This can significantly
69694 ** reduce the amount of memory held by a prepared statement.
69695 */
69696 do {
69697 x.nNeeded = 0;
69698 p->aMem = allocSpace(&x, p->aMem, nMem*sizeof(Mem));
69699 p->aVar = allocSpace(&x, p->aVar, nVar*sizeof(Mem));
69700 p->apArg = allocSpace(&x, p->apArg, nArg*sizeof(Mem*));
69701 p->apCsr = allocSpace(&x, p->apCsr, nCursor*sizeof(VdbeCursor*));
69702 p->aOnceFlag = allocSpace(&x, p->aOnceFlag, nOnce);
 
69703 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
69704 p->anExec = allocSpace(&x, p->anExec, p->nOp*sizeof(i64));
69705 #endif
69706 if( x.nNeeded==0 ) break;
69707 x.pSpace = p->pFree = sqlite3DbMallocZero(db, x.nNeeded);
69708 x.nFree = x.nNeeded;
69709 }while( !db->mallocFailed );
 
 
69710
69711 p->nCursor = nCursor;
69712 p->nOnceFlag = nOnce;
69713 if( p->aVar ){
69714 p->nVar = (ynVar)nVar;
@@ -70066,11 +70317,11 @@
70317 ** Then the internal cache might have been left in an inconsistent
70318 ** state. We need to rollback the statement transaction, if there is
70319 ** one, or the complete transaction if there is no statement transaction.
70320 */
70321
70322 if( db->mallocFailed ){
70323 p->rc = SQLITE_NOMEM;
70324 }
70325 if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
70326 closeAllCursors(p);
70327 if( p->magic!=VDBE_MAGIC_RUN ){
@@ -70227,11 +70478,11 @@
70478 assert( db->nVdbeRead>=db->nVdbeWrite );
70479 assert( db->nVdbeWrite>=0 );
70480 }
70481 p->magic = VDBE_MAGIC_HALT;
70482 checkActiveVdbeCnt(db);
70483 if( db->mallocFailed ){
70484 p->rc = SQLITE_NOMEM;
70485 }
70486
70487 /* If the auto-commit flag is set to true, then any locks that were held
70488 ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
@@ -70264,16 +70515,16 @@
70515 */
70516 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
70517 sqlite3 *db = p->db;
70518 int rc = p->rc;
70519 if( p->zErrMsg ){
70520 db->bBenignMalloc++;
70521 sqlite3BeginBenignMalloc();
70522 if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
70523 sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
70524 sqlite3EndBenignMalloc();
70525 db->bBenignMalloc--;
70526 db->errCode = rc;
70527 }else{
70528 sqlite3Error(db, rc);
70529 }
70530 return rc;
@@ -70558,13 +70809,20 @@
70809 ** a NULL row.
70810 **
70811 ** If the cursor is already pointing to the correct row and that row has
70812 ** not been deleted out from under the cursor, then this routine is a no-op.
70813 */
70814 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor **pp, int *piCol){
70815 VdbeCursor *p = *pp;
70816 if( p->eCurType==CURTYPE_BTREE ){
70817 if( p->deferredMoveto ){
70818 int iMap;
70819 if( p->aAltMap && (iMap = p->aAltMap[1+*piCol])>0 ){
70820 *pp = p->pAltCursor;
70821 *piCol = iMap - 1;
70822 return SQLITE_OK;
70823 }
70824 return handleDeferredMoveto(p);
70825 }
70826 if( sqlite3BtreeCursorHasMoved(p->uc.pCursor) ){
70827 return handleMovedCursor(p);
70828 }
@@ -72191,11 +72449,12 @@
72449 }
72450 SQLITE_API sqlite_int64 SQLITE_STDCALL sqlite3_value_int64(sqlite3_value *pVal){
72451 return sqlite3VdbeIntValue((Mem*)pVal);
72452 }
72453 SQLITE_API unsigned int SQLITE_STDCALL sqlite3_value_subtype(sqlite3_value *pVal){
72454 Mem *pMem = (Mem*)pVal;
72455 return ((pMem->flags & MEM_Subtype) ? pMem->eSubtype : 0);
72456 }
72457 SQLITE_API const unsigned char *SQLITE_STDCALL sqlite3_value_text(sqlite3_value *pVal){
72458 return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
72459 }
72460 #ifndef SQLITE_OMIT_UTF16
@@ -72372,12 +72631,14 @@
72631 SQLITE_API void SQLITE_STDCALL sqlite3_result_null(sqlite3_context *pCtx){
72632 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72633 sqlite3VdbeMemSetNull(pCtx->pOut);
72634 }
72635 SQLITE_API void SQLITE_STDCALL sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubtype){
72636 Mem *pOut = pCtx->pOut;
72637 assert( sqlite3_mutex_held(pOut->db->mutex) );
72638 pOut->eSubtype = eSubtype & 0xff;
72639 pOut->flags |= MEM_Subtype;
72640 }
72641 SQLITE_API void SQLITE_STDCALL sqlite3_result_text(
72642 sqlite3_context *pCtx,
72643 const char *z,
72644 int n,
@@ -72473,11 +72734,11 @@
72734 SQLITE_API void SQLITE_STDCALL sqlite3_result_error_nomem(sqlite3_context *pCtx){
72735 assert( sqlite3_mutex_held(pCtx->pOut->db->mutex) );
72736 sqlite3VdbeMemSetNull(pCtx->pOut);
72737 pCtx->isError = SQLITE_NOMEM;
72738 pCtx->fErrorOrAux = 1;
72739 sqlite3OomFault(pCtx->pOut->db);
72740 }
72741
72742 /*
72743 ** This function is called after a transaction has been committed. It
72744 ** invokes callbacks registered with sqlite3_wal_hook() as required.
@@ -73101,11 +73362,11 @@
73362 ret = xFunc(&p->aColName[N]);
73363 /* A malloc may have failed inside of the xFunc() call. If this
73364 ** is the case, clear the mallocFailed flag and return NULL.
73365 */
73366 if( db->mallocFailed ){
73367 sqlite3OomClear(db);
73368 ret = 0;
73369 }
73370 sqlite3_mutex_leave(db->mutex);
73371 }
73372 return ret;
@@ -73802,13 +74063,13 @@
74063 assert( idx>0 && idx<=p->nVar );
74064 pVar = &p->aVar[idx-1];
74065 if( pVar->flags & MEM_Null ){
74066 sqlite3StrAccumAppend(&out, "NULL", 4);
74067 }else if( pVar->flags & MEM_Int ){
74068 sqlite3XPrintf(&out, "%lld", pVar->u.i);
74069 }else if( pVar->flags & MEM_Real ){
74070 sqlite3XPrintf(&out, "%!.15g", pVar->u.r);
74071 }else if( pVar->flags & MEM_Str ){
74072 int nOut; /* Number of bytes of the string text to include in output */
74073 #ifndef SQLITE_OMIT_UTF16
74074 u8 enc = ENC(db);
74075 Mem utf8;
@@ -73825,36 +74086,36 @@
74086 if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
74087 nOut = SQLITE_TRACE_SIZE_LIMIT;
74088 while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
74089 }
74090 #endif
74091 sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
74092 #ifdef SQLITE_TRACE_SIZE_LIMIT
74093 if( nOut<pVar->n ){
74094 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
74095 }
74096 #endif
74097 #ifndef SQLITE_OMIT_UTF16
74098 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
74099 #endif
74100 }else if( pVar->flags & MEM_Zero ){
74101 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
74102 }else{
74103 int nOut; /* Number of bytes of the blob to include in output */
74104 assert( pVar->flags & MEM_Blob );
74105 sqlite3StrAccumAppend(&out, "x'", 2);
74106 nOut = pVar->n;
74107 #ifdef SQLITE_TRACE_SIZE_LIMIT
74108 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
74109 #endif
74110 for(i=0; i<nOut; i++){
74111 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
74112 }
74113 sqlite3StrAccumAppend(&out, "'", 1);
74114 #ifdef SQLITE_TRACE_SIZE_LIMIT
74115 if( nOut<pVar->n ){
74116 sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-nOut);
74117 }
74118 #endif
74119 }
74120 }
74121 }
@@ -74336,10 +74597,11 @@
74597 }else{
74598 char zBuf[200];
74599 sqlite3VdbeMemPrettyPrint(p, zBuf);
74600 printf(" %s", zBuf);
74601 }
74602 if( p->flags & MEM_Subtype ) printf(" subtype=0x%02x", p->eSubtype);
74603 }
74604 static void registerTrace(int iReg, Mem *p){
74605 printf("REG[%d] = ", iReg);
74606 memTracePrint(p);
74607 printf("\n");
@@ -74506,10 +74768,13 @@
74768 Op *aOp = p->aOp; /* Copy of p->aOp */
74769 Op *pOp = aOp; /* Current operation */
74770 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
74771 Op *pOrigOp; /* Value of pOp at the top of the loop */
74772 #endif
74773 #ifdef SQLITE_DEBUG
74774 int nExtraDelete = 0; /* Verifies FORDELETE and AUXDELETE flags */
74775 #endif
74776 int rc = SQLITE_OK; /* Value to return */
74777 sqlite3 *db = p->db; /* The database */
74778 u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
74779 u8 encoding = ENC(db); /* The database encoding */
74780 int iCompare = 0; /* Result of last OP_Compare operation */
@@ -74579,11 +74844,10 @@
74844 }
74845 sqlite3EndBenignMalloc();
74846 #endif
74847 for(pOp=&aOp[p->pc]; rc==SQLITE_OK; pOp++){
74848 assert( pOp>=aOp && pOp<&aOp[p->nOp]);
 
74849 #ifdef VDBE_PROFILE
74850 start = sqlite3Hwtime();
74851 #endif
74852 nVmStep++;
74853 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
@@ -75577,11 +75841,11 @@
75841 assert( pOp->p4type==P4_FUNCDEF );
75842 n = pOp->p5;
75843 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
75844 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
75845 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
75846 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
75847 if( pCtx==0 ) goto no_mem;
75848 pCtx->pOut = 0;
75849 pCtx->pFunc = pOp->p4.pFunc;
75850 pCtx->iOp = (int)(pOp - aOp);
75851 pCtx->pVdbe = p;
@@ -76021,15 +76285,18 @@
76285 ** of integers in P4.
76286 **
76287 ** The permutation is only valid until the next OP_Compare that has
76288 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
76289 ** occur immediately prior to the OP_Compare.
76290 **
76291 ** The first integer in the P4 integer array is the length of the array
76292 ** and does not become part of the permutation.
76293 */
76294 case OP_Permutation: {
76295 assert( pOp->p4type==P4_INTARRAY );
76296 assert( pOp->p4.ai );
76297 aPermute = pOp->p4.ai + 1;
76298 break;
76299 }
76300
76301 /* Opcode: Compare P1 P2 P3 P4 P5
76302 ** Synopsis: r[P1@P3] <-> r[P2@P3]
@@ -76330,26 +76597,28 @@
76597 u64 offset64; /* 64-bit offset */
76598 u32 avail; /* Number of bytes of available data */
76599 u32 t; /* A type code from the record header */
76600 Mem *pReg; /* PseudoTable input register */
76601
76602 pC = p->apCsr[pOp->p1];
76603 p2 = pOp->p2;
76604
76605 /* If the cursor cache is stale, bring it up-to-date */
76606 rc = sqlite3VdbeCursorMoveto(&pC, &p2);
76607
76608 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
76609 pDest = &aMem[pOp->p3];
76610 memAboutToChange(p, pDest);
76611 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
 
76612 assert( pC!=0 );
76613 assert( p2<pC->nField );
76614 aOffset = pC->aOffset;
76615 assert( pC->eCurType!=CURTYPE_VTAB );
76616 assert( pC->eCurType!=CURTYPE_PSEUDO || pC->nullRow );
76617 assert( pC->eCurType!=CURTYPE_SORTER );
76618 pCrsr = pC->uc.pCursor;
76619
 
 
76620 if( rc ) goto abort_due_to_error;
76621 if( pC->cacheStatus!=p->cacheCtr ){
76622 if( pC->nullRow ){
76623 if( pC->eCurType==CURTYPE_PSEUDO ){
76624 assert( pC->uc.pseudoTableReg>0 );
@@ -76816,11 +77085,11 @@
77085 db->nStatement+db->nSavepoint);
77086 if( rc!=SQLITE_OK ) goto abort_due_to_error;
77087 #endif
77088
77089 /* Create a new savepoint structure. */
77090 pNew = sqlite3DbMallocRawNN(db, sizeof(Savepoint)+nName+1);
77091 if( pNew ){
77092 pNew->zName = (char *)&pNew[1];
77093 memcpy(pNew->zName, zName, nName+1);
77094
77095 /* If there is no open transaction, then mark this as a special
@@ -76953,32 +77222,31 @@
77222 ** This instruction causes the VM to halt.
77223 */
77224 case OP_AutoCommit: {
77225 int desiredAutoCommit;
77226 int iRollback;
 
77227
77228 desiredAutoCommit = pOp->p1;
77229 iRollback = pOp->p2;
 
77230 assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
77231 assert( desiredAutoCommit==1 || iRollback==0 );
77232 assert( db->nVdbeActive>0 ); /* At least this one VM is active */
77233 assert( p->bIsReader );
77234
77235 if( desiredAutoCommit!=db->autoCommit ){
 
 
 
 
 
 
 
77236 if( iRollback ){
77237 assert( desiredAutoCommit==1 );
77238 sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
77239 db->autoCommit = 1;
77240 }else if( desiredAutoCommit && db->nVdbeWrite>0 ){
77241 /* If this instruction implements a COMMIT and other VMs are writing
77242 ** return an error indicating that the other VMs must complete first.
77243 */
77244 sqlite3VdbeError(p, "cannot commit transaction - "
77245 "SQL statements in progress");
77246 rc = SQLITE_BUSY;
77247 break;
77248 }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
77249 goto vdbe_return;
77250 }else{
77251 db->autoCommit = (u8)desiredAutoCommit;
77252 }
@@ -77159,38 +77427,36 @@
77427 break;
77428 }
77429
77430 /* Opcode: SetCookie P1 P2 P3 * *
77431 **
77432 ** Write the integer value P3 into cookie number P2 of database P1.
77433 ** P2==1 is the schema version. P2==2 is the database format.
77434 ** P2==3 is the recommended pager cache
77435 ** size, and so forth. P1==0 is the main database file and P1==1 is the
77436 ** database file used to store temporary tables.
77437 **
77438 ** A transaction must be started before executing this opcode.
77439 */
77440 case OP_SetCookie: {
77441 Db *pDb;
77442 assert( pOp->p2<SQLITE_N_BTREE_META );
77443 assert( pOp->p1>=0 && pOp->p1<db->nDb );
77444 assert( DbMaskTest(p->btreeMask, pOp->p1) );
77445 assert( p->readOnly==0 );
77446 pDb = &db->aDb[pOp->p1];
77447 assert( pDb->pBt!=0 );
77448 assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
 
 
77449 /* See note about index shifting on OP_ReadCookie */
77450 rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, pOp->p3);
77451 if( pOp->p2==BTREE_SCHEMA_VERSION ){
77452 /* When the schema cookie changes, record the new cookie internally */
77453 pDb->pSchema->schema_cookie = pOp->p3;
77454 db->flags |= SQLITE_InternChanges;
77455 }else if( pOp->p2==BTREE_FILE_FORMAT ){
77456 /* Record changes in the file format */
77457 pDb->pSchema->file_format = pOp->p3;
77458 }
77459 if( pOp->p1==1 ){
77460 /* Invalidate all prepared statements whenever the TEMP database
77461 ** schema is changed. Ticket #1644 */
77462 sqlite3ExpirePreparedStatements(db);
@@ -77346,10 +77612,13 @@
77612 pCur = allocateCursor(p, pOp->p1, nField, iDb, CURTYPE_BTREE);
77613 if( pCur==0 ) goto no_mem;
77614 pCur->nullRow = 1;
77615 pCur->isOrdered = 1;
77616 pCur->pgnoRoot = p2;
77617 #ifdef SQLITE_DEBUG
77618 pCur->wrFlag = wrFlag;
77619 #endif
77620 rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->uc.pCursor);
77621 pCur->pKeyInfo = pKeyInfo;
77622 /* Set the VdbeCursor.isTable variable. Previous versions of
77623 ** SQLite used to check if the root-page flags were sane at this point
77624 ** and report database corruption if they were not, but this check has
@@ -77799,36 +78068,10 @@
78068 assert( pOp[1].opcode==OP_IdxLT || pOp[1].opcode==OP_IdxGT );
78069 pOp++; /* Skip the OP_IdxLt or OP_IdxGT that follows */
78070 }
78071 break;
78072 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78073
78074
78075 /* Opcode: Found P1 P2 P3 P4 *
78076 ** Synopsis: key=r[P3@P4]
78077 **
@@ -78295,18 +78538,26 @@
78538
78539 /* Opcode: Delete P1 P2 * P4 P5
78540 **
78541 ** Delete the record at which the P1 cursor is currently pointing.
78542 **
78543 ** If the OPFLAG_SAVEPOSITION bit of the P5 parameter is set, then
78544 ** the cursor will be left pointing at either the next or the previous
78545 ** record in the table. If it is left pointing at the next record, then
78546 ** the next Next instruction will be a no-op. As a result, in this case
78547 ** it is ok to delete a record from within a Next loop. If
78548 ** OPFLAG_SAVEPOSITION bit of P5 is clear, then the cursor will be
78549 ** left in an undefined state.
78550 **
78551 ** If the OPFLAG_AUXDELETE bit is set on P5, that indicates that this
78552 ** delete one of several associated with deleting a table row and all its
78553 ** associated index entries. Exactly one of those deletes is the "primary"
78554 ** delete. The others are all on OPFLAG_FORDELETE cursors or else are
78555 ** marked with the AUXDELETE flag.
78556 **
78557 ** If the OPFLAG_NCHANGE flag of P2 (NB: P2 not P5) is set, then the row
78558 ** change count is incremented (otherwise not).
78559 **
78560 ** P1 must not be pseudo-table. It has to be a real table with
78561 ** multiple rows.
78562 **
78563 ** If P4 is not NULL, then it is the name of the table that P1 is
@@ -78338,11 +78589,30 @@
78589 i64 iKey = 0;
78590 sqlite3BtreeKeySize(pC->uc.pCursor, &iKey);
78591 assert( pC->movetoTarget==iKey );
78592 }
78593 #endif
78594
78595 /* Only flags that can be set are SAVEPOISTION and AUXDELETE */
78596 assert( (pOp->p5 & ~(OPFLAG_SAVEPOSITION|OPFLAG_AUXDELETE))==0 );
78597 assert( OPFLAG_SAVEPOSITION==BTREE_SAVEPOSITION );
78598 assert( OPFLAG_AUXDELETE==BTREE_AUXDELETE );
78599
78600 #ifdef SQLITE_DEBUG
78601 if( p->pFrame==0 ){
78602 if( pC->isEphemeral==0
78603 && (pOp->p5 & OPFLAG_AUXDELETE)==0
78604 && (pC->wrFlag & OPFLAG_FORDELETE)==0
78605 ){
78606 nExtraDelete++;
78607 }
78608 if( pOp->p2 & OPFLAG_NCHANGE ){
78609 nExtraDelete--;
78610 }
78611 }
78612 #endif
78613
78614 rc = sqlite3BtreeDelete(pC->uc.pCursor, pOp->p5);
78615 pC->cacheStatus = CACHE_STALE;
78616
78617 /* Invoke the update-hook if required. */
78618 if( rc==SQLITE_OK && hasUpdateCallback ){
@@ -78883,62 +79153,98 @@
79153 assert( pOp->p5==0 );
79154 r.pKeyInfo = pC->pKeyInfo;
79155 r.nField = (u16)pOp->p3;
79156 r.default_rc = 0;
79157 r.aMem = &aMem[pOp->p2];
 
 
 
79158 rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
79159 if( rc==SQLITE_OK && res==0 ){
79160 rc = sqlite3BtreeDelete(pCrsr, BTREE_AUXDELETE);
79161 }
79162 assert( pC->deferredMoveto==0 );
79163 pC->cacheStatus = CACHE_STALE;
79164 break;
79165 }
79166
79167 /* Opcode: Seek P1 * P3 P4 *
79168 ** Synopsis: Move P3 to P1.rowid
79169 **
79170 ** P1 is an open index cursor and P3 is a cursor on the corresponding
79171 ** table. This opcode does a deferred seek of the P3 table cursor
79172 ** to the row that corresponds to the current row of P1.
79173 **
79174 ** This is a deferred seek. Nothing actually happens until
79175 ** the cursor is used to read a record. That way, if no reads
79176 ** occur, no unnecessary I/O happens.
79177 **
79178 ** P4 may be an array of integers (type P4_INTARRAY) containing
79179 ** one entry for each column in the P3 table. If array entry a(i)
79180 ** is non-zero, then reading column a(i)-1 from cursor P3 is
79181 ** equivalent to performing the deferred seek and then reading column i
79182 ** from P1. This information is stored in P3 and used to redirect
79183 ** reads against P3 over to P1, thus possibly avoiding the need to
79184 ** seek and read cursor P3.
79185 */
79186 /* Opcode: IdxRowid P1 P2 * * *
79187 ** Synopsis: r[P2]=rowid
79188 **
79189 ** Write into register P2 an integer which is the last entry in the record at
79190 ** the end of the index key pointed to by cursor P1. This integer should be
79191 ** the rowid of the table entry to which this index entry points.
79192 **
79193 ** See also: Rowid, MakeRecord.
79194 */
79195 case OP_Seek:
79196 case OP_IdxRowid: { /* out2 */
79197 VdbeCursor *pC; /* The P1 index cursor */
79198 VdbeCursor *pTabCur; /* The P2 table cursor (OP_Seek only) */
79199 i64 rowid; /* Rowid that P1 current points to */
79200
 
79201 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
79202 pC = p->apCsr[pOp->p1];
79203 assert( pC!=0 );
79204 assert( pC->eCurType==CURTYPE_BTREE );
79205 assert( pC->uc.pCursor!=0 );
 
 
79206 assert( pC->isTable==0 );
79207 assert( pC->deferredMoveto==0 );
79208 assert( !pC->nullRow || pOp->opcode==OP_IdxRowid );
79209
79210 /* The IdxRowid and Seek opcodes are combined because of the commonality
79211 ** of sqlite3VdbeCursorRestore() and sqlite3VdbeIdxRowid(). */
79212 rc = sqlite3VdbeCursorRestore(pC);
79213
79214 /* sqlite3VbeCursorRestore() can only fail if the record has been deleted
79215 ** out from under the cursor. That will never happens for an IdxRowid
79216 ** or Seek opcode */
 
 
79217 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
79218
79219 if( !pC->nullRow ){
79220 rowid = 0; /* Not needed. Only used to silence a warning. */
79221 rc = sqlite3VdbeIdxRowid(db, pC->uc.pCursor, &rowid);
79222 if( rc!=SQLITE_OK ){
79223 goto abort_due_to_error;
79224 }
79225 if( pOp->opcode==OP_Seek ){
79226 assert( pOp->p3>=0 && pOp->p3<p->nCursor );
79227 pTabCur = p->apCsr[pOp->p3];
79228 assert( pTabCur!=0 );
79229 assert( pTabCur->eCurType==CURTYPE_BTREE );
79230 assert( pTabCur->uc.pCursor!=0 );
79231 assert( pTabCur->isTable );
79232 pTabCur->nullRow = 0;
79233 pTabCur->movetoTarget = rowid;
79234 pTabCur->deferredMoveto = 1;
79235 assert( pOp->p4type==P4_INTARRAY || pOp->p4.ai==0 );
79236 pTabCur->aAltMap = pOp->p4.ai;
79237 pTabCur->pAltCursor = pC;
79238 }else{
79239 pOut = out2Prerelease(p, pOp);
79240 pOut->u.i = rowid;
79241 pOut->flags = MEM_Int;
79242 }
79243 }else{
79244 assert( pOp->opcode==OP_IdxRowid );
79245 sqlite3VdbeMemSetNull(&aMem[pOp->p2]);
79246 }
79247 break;
79248 }
79249
79250 /* Opcode: IdxGE P1 P2 P3 P4 P5
@@ -79329,11 +79635,11 @@
79635 Mem *pnErr; /* Register keeping track of errors remaining */
79636
79637 assert( p->bIsReader );
79638 nRoot = pOp->p2;
79639 assert( nRoot>0 );
79640 aRoot = sqlite3DbMallocRawNN(db, sizeof(int)*(nRoot+1) );
79641 if( aRoot==0 ) goto no_mem;
79642 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
79643 pnErr = &aMem[pOp->p3];
79644 assert( (pnErr->flags & MEM_Int)!=0 );
79645 assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
@@ -79711,24 +80017,35 @@
80017 goto jump_to_p2;
80018 }
80019 break;
80020 }
80021
80022 /* Opcode: OffsetLimit P1 P2 P3 * *
80023 ** Synopsis: if r[P1]>0 then r[P2]=r[P1]+max(0,r[P3]) else r[P2]=(-1)
80024 **
80025 ** This opcode performs a commonly used computation associated with
80026 ** LIMIT and OFFSET process. r[P1] holds the limit counter. r[P3]
80027 ** holds the offset counter. The opcode computes the combined value
80028 ** of the LIMIT and OFFSET and stores that value in r[P2]. The r[P2]
80029 ** value computed is the total number of rows that will need to be
80030 ** visited in order to complete the query.
80031 **
80032 ** If r[P3] is zero or negative, that means there is no OFFSET
80033 ** and r[P2] is set to be the value of the LIMIT, r[P1].
80034 **
80035 ** if r[P1] is zero or negative, that means there is no LIMIT
80036 ** and r[P2] is set to -1.
80037 **
80038 ** Otherwise, r[P2] is set to the sum of r[P1] and r[P3].
80039 */
80040 case OP_OffsetLimit: { /* in1, out2, in3 */
80041 pIn1 = &aMem[pOp->p1];
80042 pIn3 = &aMem[pOp->p3];
80043 pOut = out2Prerelease(p, pOp);
80044 assert( pIn1->flags & MEM_Int );
80045 assert( pIn3->flags & MEM_Int );
80046 pOut->u.i = pIn1->u.i<=0 ? -1 : pIn1->u.i+(pIn3->u.i>0?pIn3->u.i:0);
80047 break;
80048 }
80049
80050 /* Opcode: IfNotZero P1 P2 P3 * *
80051 ** Synopsis: if r[P1]!=0 then r[P1]-=P3, goto P2
@@ -79815,11 +80132,11 @@
80132 assert( pOp->p4type==P4_FUNCDEF );
80133 n = pOp->p5;
80134 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
80135 assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
80136 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
80137 pCtx = sqlite3DbMallocRawNN(db, sizeof(*pCtx) + (n-1)*sizeof(sqlite3_value*));
80138 if( pCtx==0 ) goto no_mem;
80139 pCtx->pMem = 0;
80140 pCtx->pFunc = pOp->p4.pFunc;
80141 pCtx->iOp = (int)(pOp - aOp);
80142 pCtx->pVdbe = p;
@@ -80682,11 +80999,11 @@
80999 p->rc = rc;
81000 testcase( sqlite3GlobalConfig.xLog!=0 );
81001 sqlite3_log(rc, "statement aborts at %d: [%s] %s",
81002 (int)(pOp - aOp), p->zSql, p->zErrMsg);
81003 sqlite3VdbeHalt(p);
81004 if( rc==SQLITE_IOERR_NOMEM ) sqlite3OomFault(db);
81005 rc = SQLITE_ERROR;
81006 if( resetSchemaOnFault>0 ){
81007 sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
81008 }
81009
@@ -80696,10 +81013,13 @@
81013 vdbe_return:
81014 db->lastRowid = lastRowid;
81015 testcase( nVmStep>0 );
81016 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
81017 sqlite3VdbeLeave(p);
81018 assert( rc!=SQLITE_OK || nExtraDelete==0
81019 || sqlite3_strlike("DELETE%",p->zSql,0)!=0
81020 );
81021 return rc;
81022
81023 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
81024 ** is encountered.
81025 */
@@ -80709,11 +81029,11 @@
81029 goto vdbe_error_halt;
81030
81031 /* Jump to here if a malloc() fails.
81032 */
81033 no_mem:
81034 sqlite3OomFault(db);
81035 sqlite3VdbeError(p, "out of memory");
81036 rc = SQLITE_NOMEM;
81037 goto vdbe_error_halt;
81038
81039 /* Jump to here for any other kind of fatal error. The "rc" variable
@@ -80730,11 +81050,11 @@
81050 /* Jump to here if the sqlite3_interrupt() API sets the interrupt
81051 ** flag.
81052 */
81053 abort_due_to_interrupt:
81054 assert( db->u1.isInterrupted );
81055 rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_INTERRUPT;
81056 p->rc = rc;
81057 sqlite3VdbeError(p, "%s", sqlite3ErrStr(rc));
81058 goto vdbe_error_halt;
81059 }
81060
@@ -80990,23 +81310,21 @@
81310 **
81311 ** The sqlite3_blob_close() function finalizes the vdbe program,
81312 ** which closes the b-tree cursor and (possibly) commits the
81313 ** transaction.
81314 */
81315 static const int iLn = VDBE_OFFSET_LINENO(2);
81316 static const VdbeOpList openBlob[] = {
81317 {OP_TableLock, 0, 0, 0}, /* 0: Acquire a read or write lock */
81318 {OP_OpenRead, 0, 0, 0}, /* 1: Open a cursor */
81319 {OP_Variable, 1, 1, 0}, /* 2: Move ?1 into reg[1] */
81320 {OP_NotExists, 0, 7, 1}, /* 3: Seek the cursor */
81321 {OP_Column, 0, 0, 1}, /* 4 */
81322 {OP_ResultRow, 1, 0, 0}, /* 5 */
81323 {OP_Goto, 0, 2, 0}, /* 6 */
81324 {OP_Close, 0, 0, 0}, /* 7 */
81325 {OP_Halt, 0, 0, 0}, /* 8 */
 
 
81326 };
81327 Vdbe *v = (Vdbe *)pBlob->pStmt;
81328 int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81329 VdbeOp *aOp;
81330
@@ -83044,27 +83362,28 @@
83362 if( pSorter->list.aMemory ){
83363 int nMin = pSorter->iMemory + nReq;
83364
83365 if( nMin>pSorter->nMemory ){
83366 u8 *aNew;
83367 int iListOff = (u8*)pSorter->list.pList - pSorter->list.aMemory;
83368 int nNew = pSorter->nMemory * 2;
83369 while( nNew < nMin ) nNew = nNew*2;
83370 if( nNew > pSorter->mxPmaSize ) nNew = pSorter->mxPmaSize;
83371 if( nNew < nMin ) nNew = nMin;
83372
83373 aNew = sqlite3Realloc(pSorter->list.aMemory, nNew);
83374 if( !aNew ) return SQLITE_NOMEM;
83375 pSorter->list.pList = (SorterRecord*)&aNew[iListOff];
 
 
83376 pSorter->list.aMemory = aNew;
83377 pSorter->nMemory = nNew;
83378 }
83379
83380 pNew = (SorterRecord*)&pSorter->list.aMemory[pSorter->iMemory];
83381 pSorter->iMemory += ROUND8(nReq);
83382 if( pSorter->list.pList ){
83383 pNew->u.iNext = (int)((u8*)(pSorter->list.pList) - pSorter->list.aMemory);
83384 }
83385 }else{
83386 pNew = (SorterRecord *)sqlite3Malloc(nReq);
83387 if( pNew==0 ){
83388 return SQLITE_NOMEM;
83389 }
@@ -86230,12 +86549,11 @@
86549 return pExpr;
86550 }
86551 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
86552 Token s;
86553 assert( zC!=0 );
86554 sqlite3TokenInit(&s, (char*)zC);
 
86555 return sqlite3ExprAddCollateToken(pParse, pExpr, &s, 0);
86556 }
86557
86558 /*
86559 ** Skip over any TK_COLLATE operators and any unlikely()
@@ -86599,18 +86917,19 @@
86917 ){
86918 Expr *pNew;
86919 int nExtra = 0;
86920 int iValue = 0;
86921
86922 assert( db!=0 );
86923 if( pToken ){
86924 if( op!=TK_INTEGER || pToken->z==0
86925 || sqlite3GetInt32(pToken->z, &iValue)==0 ){
86926 nExtra = pToken->n+1;
86927 assert( iValue>=0 );
86928 }
86929 }
86930 pNew = sqlite3DbMallocRawNN(db, sizeof(Expr)+nExtra);
86931 if( pNew ){
86932 memset(pNew, 0, sizeof(Expr));
86933 pNew->op = (u8)op;
86934 pNew->iAgg = -1;
86935 if( pToken ){
@@ -86845,11 +87164,14 @@
87164 }
87165 if( x>0 ){
87166 if( x>pParse->nzVar ){
87167 char **a;
87168 a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
87169 if( a==0 ){
87170 assert( db->mallocFailed ); /* Error reported through mallocFailed */
87171 return;
87172 }
87173 pParse->azVar = a;
87174 memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
87175 pParse->nzVar = x;
87176 }
87177 if( z[0]!='?' || pParse->azVar[x-1]==0 ){
@@ -87000,10 +87322,11 @@
87322 ** portion of the buffer copied into by this function.
87323 */
87324 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
87325 Expr *pNew = 0; /* Value to return */
87326 assert( flags==0 || flags==EXPRDUP_REDUCE );
87327 assert( db!=0 );
87328 if( p ){
87329 const int isReduced = (flags&EXPRDUP_REDUCE);
87330 u8 *zAlloc;
87331 u32 staticFlag = 0;
87332
@@ -87012,11 +87335,11 @@
87335 /* Figure out where to write the new Expr structure. */
87336 if( pzBuffer ){
87337 zAlloc = *pzBuffer;
87338 staticFlag = EP_Static;
87339 }else{
87340 zAlloc = sqlite3DbMallocRawNN(db, dupedExprSize(p, flags));
87341 }
87342 pNew = (Expr *)zAlloc;
87343
87344 if( pNew ){
87345 /* Set nNewSize to the size allocated for the structure pointed to
@@ -87135,16 +87458,17 @@
87458 }
87459 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
87460 ExprList *pNew;
87461 struct ExprList_item *pItem, *pOldItem;
87462 int i;
87463 assert( db!=0 );
87464 if( p==0 ) return 0;
87465 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
87466 if( pNew==0 ) return 0;
87467 pNew->nExpr = i = p->nExpr;
87468 if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
87469 pNew->a = pItem = sqlite3DbMallocRawNN(db, i*sizeof(p->a[0]) );
87470 if( pItem==0 ){
87471 sqlite3DbFree(db, pNew);
87472 return 0;
87473 }
87474 pOldItem = p->a;
@@ -87171,13 +87495,14 @@
87495 || !defined(SQLITE_OMIT_SUBQUERY)
87496 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
87497 SrcList *pNew;
87498 int i;
87499 int nByte;
87500 assert( db!=0 );
87501 if( p==0 ) return 0;
87502 nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
87503 pNew = sqlite3DbMallocRawNN(db, nByte );
87504 if( pNew==0 ) return 0;
87505 pNew->nSrc = pNew->nAlloc = p->nSrc;
87506 for(i=0; i<p->nSrc; i++){
87507 struct SrcList_item *pNewItem = &pNew->a[i];
87508 struct SrcList_item *pOldItem = &p->a[i];
@@ -87210,15 +87535,16 @@
87535 return pNew;
87536 }
87537 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
87538 IdList *pNew;
87539 int i;
87540 assert( db!=0 );
87541 if( p==0 ) return 0;
87542 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
87543 if( pNew==0 ) return 0;
87544 pNew->nId = p->nId;
87545 pNew->a = sqlite3DbMallocRawNN(db, p->nId*sizeof(p->a[0]) );
87546 if( pNew->a==0 ){
87547 sqlite3DbFree(db, pNew);
87548 return 0;
87549 }
87550 /* Note that because the size of the allocation for p->a[] is not
@@ -87232,12 +87558,13 @@
87558 }
87559 return pNew;
87560 }
87561 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
87562 Select *pNew, *pPrior;
87563 assert( db!=0 );
87564 if( p==0 ) return 0;
87565 pNew = sqlite3DbMallocRawNN(db, sizeof(*p) );
87566 if( pNew==0 ) return 0;
87567 pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
87568 pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
87569 pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
87570 pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
@@ -87279,17 +87606,18 @@
87606 Parse *pParse, /* Parsing context */
87607 ExprList *pList, /* List to which to append. Might be NULL */
87608 Expr *pExpr /* Expression to be appended. Might be NULL */
87609 ){
87610 sqlite3 *db = pParse->db;
87611 assert( db!=0 );
87612 if( pList==0 ){
87613 pList = sqlite3DbMallocRawNN(db, sizeof(ExprList) );
87614 if( pList==0 ){
87615 goto no_mem;
87616 }
87617 pList->nExpr = 0;
87618 pList->a = sqlite3DbMallocRawNN(db, sizeof(pList->a[0]));
87619 if( pList->a==0 ) goto no_mem;
87620 }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
87621 struct ExprList_item *a;
87622 assert( pList->nExpr>0 );
87623 a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
@@ -90989,37 +91317,10 @@
91317 sqlite3SrcListDelete(db, pSrc);
91318 sqlite3DbFree(db, zName);
91319 db->flags = savedDbFlags;
91320 }
91321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91322 /*
91323 ** This function is called after an "ALTER TABLE ... ADD" statement
91324 ** has been parsed. Argument pColDef contains the text of the new
91325 ** column definition.
91326 **
@@ -91034,13 +91335,15 @@
91335 const char *zTab; /* Table name */
91336 char *zCol; /* Null-terminated column definition */
91337 Column *pCol; /* The new column */
91338 Expr *pDflt; /* Default value for the new column */
91339 sqlite3 *db; /* The database connection; */
91340 Vdbe *v = pParse->pVdbe; /* The prepared statement under construction */
91341
91342 db = pParse->db;
91343 if( pParse->nErr || db->mallocFailed ) return;
91344 assert( v!=0 );
91345 pNew = pParse->pNewTable;
91346 assert( pNew );
91347
91348 assert( sqlite3BtreeHoldsAllMutexes(db) );
91349 iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
@@ -91096,11 +91399,11 @@
91399 sqlite3_value *pVal = 0;
91400 int rc;
91401 rc = sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_BLOB, &pVal);
91402 assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
91403 if( rc!=SQLITE_OK ){
91404 assert( db->mallocFailed == 1 );
91405 return;
91406 }
91407 if( !pVal ){
91408 sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
91409 return;
@@ -91126,15 +91429,20 @@
91429 );
91430 sqlite3DbFree(db, zCol);
91431 db->flags = savedDbFlags;
91432 }
91433
91434 /* If the default value of the new column is NULL, then the file
91435 ** format to 2. If the default value of the new column is not NULL,
91436 ** the file format be 3. Back when this feature was first added
91437 ** in 2006, we went to the trouble to upgrade the file format to the
91438 ** minimum support values. But 10-years on, we can assume that all
91439 ** extent versions of SQLite support file-format 4, so we always and
91440 ** unconditionally upgrade to 4.
91441 */
91442 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT,
91443 SQLITE_MAX_FILE_FORMAT);
91444
91445 /* Reload the schema of the modified table. */
91446 reloadTableSchema(pParse, pTab, pTab->zName);
91447 }
91448
@@ -91204,11 +91512,11 @@
91512 nAlloc = (((pNew->nCol-1)/8)*8)+8;
91513 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
91514 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
91515 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
91516 if( !pNew->aCol || !pNew->zName ){
91517 assert( db->mallocFailed );
91518 goto exit_begin_add_column;
91519 }
91520 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
91521 for(i=0; i<pNew->nCol; i++){
91522 Column *pCol = &pNew->aCol[i];
@@ -91549,11 +91857,11 @@
91857 */
91858 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
91859 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
91860 assert( db!=0 );
91861 if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
91862 p->u.aRowid = sqlite3DbMallocRawNN(db, n);
91863 if( p->u.aRowid ){
91864 p->nRowid = n;
91865 memcpy(p->u.aRowid, pData, n);
91866 }else{
91867 p->nRowid = 0;
@@ -92351,11 +92659,11 @@
92659 addrNextRow = sqlite3VdbeCurrentAddr(v);
92660
92661 if( nColTest>0 ){
92662 int endDistinctTest = sqlite3VdbeMakeLabel(v);
92663 int *aGotoChng; /* Array of jump instruction addresses */
92664 aGotoChng = sqlite3DbMallocRawNN(db, sizeof(int)*nColTest);
92665 if( aGotoChng==0 ) continue;
92666
92667 /*
92668 ** next_row:
92669 ** regChng = 0
@@ -92759,11 +93067,11 @@
93067 /* Index.aiRowEst may already be set here if there are duplicate
93068 ** sqlite_stat1 entries for this index. In that case just clobber
93069 ** the old data with the new instead of allocating a new array. */
93070 if( pIndex->aiRowEst==0 ){
93071 pIndex->aiRowEst = (tRowcnt*)sqlite3MallocZero(sizeof(tRowcnt) * nCol);
93072 if( pIndex->aiRowEst==0 ) sqlite3OomFault(pInfo->db);
93073 }
93074 aiRowEst = pIndex->aiRowEst;
93075 #endif
93076 pIndex->bUnordered = 0;
93077 decodeIntArray((char*)z, nCol, aiRowEst, pIndex->aiRowLogEst, pIndex);
@@ -92906,11 +93214,11 @@
93214 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
93215 char *zSql; /* Text of the SQL statement */
93216 Index *pPrevIdx = 0; /* Previous index in the loop */
93217 IndexSample *pSample; /* A slot in pIdx->aSample[] */
93218
93219 assert( db->lookaside.bDisable );
93220 zSql = sqlite3MPrintf(db, zSql1, zDb);
93221 if( !zSql ){
93222 return SQLITE_NOMEM;
93223 }
93224 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
@@ -93020,11 +93328,11 @@
93328 ** the Index.aSample[] arrays of all indices.
93329 */
93330 static int loadStat4(sqlite3 *db, const char *zDb){
93331 int rc = SQLITE_OK; /* Result codes from subroutines */
93332
93333 assert( db->lookaside.bDisable );
93334 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
93335 rc = loadStatTbl(db, 0,
93336 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
93337 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
93338 zDb
@@ -93102,24 +93410,23 @@
93410
93411
93412 /* Load the statistics from the sqlite_stat4 table. */
93413 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
93414 if( rc==SQLITE_OK && OptimizationEnabled(db, SQLITE_Stat34) ){
93415 db->lookaside.bDisable++;
 
93416 rc = loadStat4(db, sInfo.zDatabase);
93417 db->lookaside.bDisable--;
93418 }
93419 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
93420 Index *pIdx = sqliteHashData(i);
93421 sqlite3_free(pIdx->aiRowEst);
93422 pIdx->aiRowEst = 0;
93423 }
93424 #endif
93425
93426 if( rc==SQLITE_NOMEM ){
93427 sqlite3OomFault(db);
93428 }
93429 return rc;
93430 }
93431
93432
@@ -93236,11 +93543,11 @@
93543
93544 /* Allocate the new entry in the db->aDb[] array and initialize the schema
93545 ** hash tables.
93546 */
93547 if( db->aDb==db->aDbStatic ){
93548 aNew = sqlite3DbMallocRawNN(db, sizeof(db->aDb[0])*3 );
93549 if( aNew==0 ) return;
93550 memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
93551 }else{
93552 aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
93553 if( aNew==0 ) return;
@@ -93254,11 +93561,11 @@
93561 ** or may not be initialized.
93562 */
93563 flags = db->openFlags;
93564 rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
93565 if( rc!=SQLITE_OK ){
93566 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
93567 sqlite3_result_error(context, zErr, -1);
93568 sqlite3_free(zErr);
93569 return;
93570 }
93571 assert( pVfs );
@@ -93283,11 +93590,12 @@
93590 pPager = sqlite3BtreePager(aNew->pBt);
93591 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
93592 sqlite3BtreeSecureDelete(aNew->pBt,
93593 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
93594 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
93595 sqlite3BtreeSetPagerFlags(aNew->pBt,
93596 PAGER_SYNCHRONOUS_FULL | (db->flags & PAGER_FLAGS_MASK));
93597 #endif
93598 sqlite3BtreeLeave(aNew->pBt);
93599 }
93600 aNew->safety_level = 3;
93601 aNew->zName = sqlite3DbStrDup(db, zName);
@@ -93356,11 +93664,11 @@
93664 db->aDb[iDb].pSchema = 0;
93665 }
93666 sqlite3ResetAllSchemasOfConnection(db);
93667 db->nDb = iDb;
93668 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
93669 sqlite3OomFault(db);
93670 sqlite3DbFree(db, zErrDyn);
93671 zErrDyn = sqlite3MPrintf(db, "out of memory");
93672 }else if( zErrDyn==0 ){
93673 zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
93674 }
@@ -94053,11 +94361,11 @@
94361 p->iTab = iTab;
94362 p->isWriteLock = isWriteLock;
94363 p->zName = zName;
94364 }else{
94365 pToplevel->nTableLock = 0;
94366 sqlite3OomFault(pToplevel->db);
94367 }
94368 }
94369
94370 /*
94371 ** Code an OP_TableLock instruction for each table locked by the
@@ -94901,11 +95209,11 @@
95209 }
95210 }
95211
95212 pTable = sqlite3DbMallocZero(db, sizeof(Table));
95213 if( pTable==0 ){
95214 assert( db->mallocFailed );
95215 pParse->rc = SQLITE_NOMEM;
95216 pParse->nErr++;
95217 goto begin_table_error;
95218 }
95219 pTable->zName = zName;
@@ -94958,14 +95266,12 @@
95266 sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
95267 sqlite3VdbeUsesBtree(v, iDb);
95268 addr1 = sqlite3VdbeAddOp1(v, OP_If, reg3); VdbeCoverage(v);
95269 fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
95270 1 : SQLITE_MAX_FILE_FORMAT;
95271 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, fileFormat);
95272 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, ENC(db));
 
 
95273 sqlite3VdbeJumpHere(v, addr1);
95274
95275 /* This just creates a place-holder record in the sqlite_master table.
95276 ** The record created does not contain anything yet. It will be replaced
95277 ** by the real entry in code generated at sqlite3EndTable().
@@ -95446,17 +95752,15 @@
95752 ** set back to prior value. But schema changes are infrequent
95753 ** and the probability of hitting the same cookie value is only
95754 ** 1 chance in 2^32. So we're safe enough.
95755 */
95756 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
 
95757 sqlite3 *db = pParse->db;
95758 Vdbe *v = pParse->pVdbe;
95759 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95760 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION,
95761 db->aDb[iDb].pSchema->schema_cookie+1);
 
95762 }
95763
95764 /*
95765 ** Measure the number of characters needed to output the given
95766 ** identifier. The number returned includes any quotes used
@@ -95534,11 +95838,11 @@
95838 zEnd = "\n)";
95839 }
95840 n += 35 + 6*p->nCol;
95841 zStmt = sqlite3DbMallocRaw(0, n);
95842 if( zStmt==0 ){
95843 sqlite3OomFault(db);
95844 return 0;
95845 }
95846 sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
95847 k = sqlite3Strlen30(zStmt);
95848 identPut(zStmt, &k, p->zName);
@@ -95683,12 +95987,11 @@
95987 ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
95988 */
95989 if( pTab->iPKey>=0 ){
95990 ExprList *pList;
95991 Token ipkToken;
95992 sqlite3TokenInit(&ipkToken, pTab->aCol[pTab->iPKey].zName);
 
95993 pList = sqlite3ExprListAppend(pParse, 0,
95994 sqlite3ExprAlloc(db, TK_ID, &ipkToken, 0));
95995 if( pList==0 ) return;
95996 pList->a[0].sortOrder = pParse->iPkSortOrder;
95997 assert( pParse->pNewTable==pTab );
@@ -95934,11 +96237,11 @@
96237 pParse->nTab = 2;
96238 addrTop = sqlite3VdbeCurrentAddr(v) + 1;
96239 sqlite3VdbeAddOp3(v, OP_InitCoroutine, regYield, 0, addrTop);
96240 sqlite3SelectDestInit(&dest, SRT_Coroutine, regYield);
96241 sqlite3Select(pParse, pSelect, &dest);
96242 sqlite3VdbeEndCoroutine(v, regYield);
96243 sqlite3VdbeJumpHere(v, addrTop - 1);
96244 if( pParse->nErr ) return;
96245 pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
96246 if( pSelTab==0 ) return;
96247 assert( p->aCol==0 );
@@ -96018,11 +96321,11 @@
96321 Schema *pSchema = p->pSchema;
96322 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
96323 pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName, p);
96324 if( pOld ){
96325 assert( p==pOld ); /* Malloc must have failed inside HashInsert() */
96326 sqlite3OomFault(db);
96327 return;
96328 }
96329 pParse->pNewTable = 0;
96330 db->flags |= SQLITE_InternChanges;
96331
@@ -96122,11 +96425,10 @@
96425 Select *pSel; /* Copy of the SELECT that implements the view */
96426 int nErr = 0; /* Number of errors encountered */
96427 int n; /* Temporarily holds the number of cursors assigned */
96428 sqlite3 *db = pParse->db; /* Database connection for malloc errors */
96429 sqlite3_xauth xAuth; /* Saved xAuth pointer */
 
96430
96431 assert( pTable );
96432
96433 #ifndef SQLITE_OMIT_VIRTUALTABLE
96434 if( sqlite3VtabCallConnect(pParse, pTable) ){
@@ -96168,30 +96470,31 @@
96470 ** to the elements of the FROM clause. But we do not want these changes
96471 ** to be permanent. So the computation is done on a copy of the SELECT
96472 ** statement that defines the view.
96473 */
96474 assert( pTable->pSelect );
 
96475 if( pTable->pCheck ){
96476 db->lookaside.bDisable++;
96477 sqlite3ColumnsFromExprList(pParse, pTable->pCheck,
96478 &pTable->nCol, &pTable->aCol);
96479 db->lookaside.bDisable--;
96480 }else{
96481 pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
96482 if( pSel ){
96483 n = pParse->nTab;
96484 sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
96485 pTable->nCol = -1;
96486 db->lookaside.bDisable++;
96487 #ifndef SQLITE_OMIT_AUTHORIZATION
96488 xAuth = db->xAuth;
96489 db->xAuth = 0;
96490 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
96491 db->xAuth = xAuth;
96492 #else
96493 pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
96494 #endif
96495 db->lookaside.bDisable--;
96496 pParse->nTab = n;
96497 if( pSelTab ){
96498 assert( pTable->aCol==0 );
96499 pTable->nCol = pSelTab->nCol;
96500 pTable->aCol = pSelTab->aCol;
@@ -96206,11 +96509,10 @@
96509 sqlite3SelectDelete(db, pSel);
96510 } else {
96511 nErr++;
96512 }
96513 }
 
96514 pTable->pSchema->schemaFlags |= DB_UnresetViews;
96515 #endif /* SQLITE_OMIT_VIEW */
96516 return nErr;
96517 }
96518 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
@@ -96672,11 +96974,11 @@
96974 assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
96975 pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
96976 pFKey->zTo, (void *)pFKey
96977 );
96978 if( pNextTo==pFKey ){
96979 sqlite3OomFault(db);
96980 goto fk_end;
96981 }
96982 if( pNextTo ){
96983 assert( pNextTo->pPrevTo==0 );
96984 pFKey->pNextTo = pNextTo;
@@ -97032,12 +97334,11 @@
97334 ** key out of the last column added to the table under construction.
97335 ** So create a fake list to simulate this.
97336 */
97337 if( pList==0 ){
97338 Token prevCol;
97339 sqlite3TokenInit(&prevCol, pTab->aCol[pTab->nCol-1].zName);
 
97340 pList = sqlite3ExprListAppend(pParse, 0,
97341 sqlite3ExprAlloc(db, TK_ID, &prevCol, 0));
97342 if( pList==0 ) goto exit_create_index;
97343 assert( pList->nExpr==1 );
97344 sqlite3ExprListSetSortOrder(pList, sortOrder);
@@ -97255,11 +97556,11 @@
97556 assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
97557 p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
97558 pIndex->zName, pIndex);
97559 if( p ){
97560 assert( p==pIndex ); /* Malloc must have failed */
97561 sqlite3OomFault(db);
97562 goto exit_create_index;
97563 }
97564 db->flags |= SQLITE_InternChanges;
97565 if( pTblName!=0 ){
97566 pIndex->tnum = db->init.newTnum;
@@ -97684,12 +97985,13 @@
97985 Token *pTable, /* Table to append */
97986 Token *pDatabase /* Database of the table */
97987 ){
97988 struct SrcList_item *pItem;
97989 assert( pDatabase==0 || pTable!=0 ); /* Cannot have C without B */
97990 assert( db!=0 );
97991 if( pList==0 ){
97992 pList = sqlite3DbMallocRawNN(db, sizeof(SrcList) );
97993 if( pList==0 ) return 0;
97994 pList->nAlloc = 1;
97995 pList->nSrc = 0;
97996 }
97997 pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
@@ -97869,21 +98171,20 @@
98171 p->a[0].fg.jointype = 0;
98172 }
98173 }
98174
98175 /*
98176 ** Generate VDBE code for a BEGIN statement.
98177 */
98178 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
98179 sqlite3 *db;
98180 Vdbe *v;
98181 int i;
98182
98183 assert( pParse!=0 );
98184 db = pParse->db;
98185 assert( db!=0 );
 
98186 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
98187 return;
98188 }
98189 v = sqlite3GetVdbe(pParse);
98190 if( !v ) return;
@@ -97891,15 +98192,15 @@
98192 for(i=0; i<db->nDb; i++){
98193 sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
98194 sqlite3VdbeUsesBtree(v, i);
98195 }
98196 }
98197 sqlite3VdbeAddOp0(v, OP_AutoCommit);
98198 }
98199
98200 /*
98201 ** Generate VDBE code for a COMMIT statement.
98202 */
98203 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
98204 Vdbe *v;
98205
98206 assert( pParse!=0 );
@@ -97907,16 +98208,16 @@
98208 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
98209 return;
98210 }
98211 v = sqlite3GetVdbe(pParse);
98212 if( v ){
98213 sqlite3VdbeAddOp1(v, OP_AutoCommit, 1);
98214 }
98215 }
98216
98217 /*
98218 ** Generate VDBE code for a ROLLBACK statement.
98219 */
98220 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
98221 Vdbe *v;
98222
98223 assert( pParse!=0 );
@@ -97974,11 +98275,11 @@
98275 return 1;
98276 }
98277 db->aDb[1].pBt = pBt;
98278 assert( db->aDb[1].pSchema );
98279 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
98280 sqlite3OomFault(db);
98281 return 1;
98282 }
98283 }
98284 return 0;
98285 }
@@ -98109,18 +98410,18 @@
98410 StrAccum errMsg;
98411 Table *pTab = pIdx->pTable;
98412
98413 sqlite3StrAccumInit(&errMsg, pParse->db, 0, 0, 200);
98414 if( pIdx->aColExpr ){
98415 sqlite3XPrintf(&errMsg, "index '%q'", pIdx->zName);
98416 }else{
98417 for(j=0; j<pIdx->nKeyCol; j++){
98418 char *zCol;
98419 assert( pIdx->aiColumn[j]>=0 );
98420 zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
98421 if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
98422 sqlite3XPrintf(&errMsg, "%s.%s", pTab->zName, zCol);
98423 }
98424 }
98425 zErr = sqlite3StrAccumFinish(&errMsg);
98426 sqlite3HaltConstraint(pParse,
98427 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
@@ -98349,14 +98650,13 @@
98650 int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
98651 pNew = sqlite3DbRealloc(db, pWith, nByte);
98652 }else{
98653 pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
98654 }
98655 assert( (pNew!=0 && zName!=0) || db->mallocFailed );
 
98656
98657 if( db->mallocFailed ){
98658 sqlite3ExprListDelete(db, pArglist);
98659 sqlite3SelectDelete(db, pQuery);
98660 sqlite3DbFree(db, zName);
98661 pNew = pWith;
98662 }else{
@@ -98566,11 +98866,11 @@
98866 ** return the pColl pointer to be deleted (because it wasn't added
98867 ** to the hash table).
98868 */
98869 assert( pDel==0 || pDel==pColl );
98870 if( pDel!=0 ){
98871 sqlite3OomFault(db);
98872 sqlite3DbFree(db, pDel);
98873 pColl = 0;
98874 }
98875 }
98876 }
@@ -98854,11 +99154,11 @@
99154 p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
99155 }else{
99156 p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
99157 }
99158 if( !p ){
99159 sqlite3OomFault(db);
99160 }else if ( 0==p->file_format ){
99161 sqlite3HashInit(&p->tblHash);
99162 sqlite3HashInit(&p->idxHash);
99163 sqlite3HashInit(&p->trigHash);
99164 sqlite3HashInit(&p->fkeyHash);
@@ -99308,11 +99608,11 @@
99608 if( eOnePass!=ONEPASS_OFF ){
99609 /* For ONEPASS, no need to store the rowid/primary-key. There is only
99610 ** one, so just keep it in its register(s) and fall through to the
99611 ** delete code. */
99612 nKey = nPk; /* OP_Found will use an unpacked key */
99613 aToOpen = sqlite3DbMallocRawNN(db, nIdx+2);
99614 if( aToOpen==0 ){
99615 sqlite3WhereEnd(pWInfo);
99616 goto delete_from_cleanup;
99617 }
99618 memset(aToOpen, 1, nIdx+1);
@@ -99348,17 +99648,16 @@
99648 ** only effect this statement has is to fire the INSTEAD OF
99649 ** triggers.
99650 */
99651 if( !isView ){
99652 int iAddrOnce = 0;
 
99653 if( eOnePass==ONEPASS_MULTI ){
99654 iAddrOnce = sqlite3CodeOnce(pParse); VdbeCoverage(v);
99655 }
99656 testcase( IsVirtual(pTab) );
99657 sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, OPFLAG_FORDELETE,
99658 iTabCur, aToOpen, &iDataCur, &iIdxCur);
99659 assert( pPk || IsVirtual(pTab) || iDataCur==iTabCur );
99660 assert( pPk || IsVirtual(pTab) || iIdxCur==iDataCur+1 );
99661 if( eOnePass==ONEPASS_MULTI ) sqlite3VdbeJumpHere(v, iAddrOnce);
99662 }
99663
@@ -99587,19 +99886,24 @@
99886
99887 /* Delete the index and table entries. Skip this step if pTab is really
99888 ** a view (in which case the only effect of the DELETE statement is to
99889 ** fire the INSTEAD OF triggers). */
99890 if( pTab->pSelect==0 ){
99891 u8 p5 = 0;
99892 sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur,0,iIdxNoSeek);
99893 sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
99894 if( count ){
99895 sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
99896 }
99897 if( eMode!=ONEPASS_OFF ){
99898 sqlite3VdbeChangeP5(v, OPFLAG_AUXDELETE);
99899 }
99900 if( iIdxNoSeek>=0 ){
99901 sqlite3VdbeAddOp1(v, OP_Delete, iIdxNoSeek);
99902 }
99903 if( eMode==ONEPASS_MULTI ) p5 |= OPFLAG_SAVEPOSITION;
99904 sqlite3VdbeChangeP5(v, p5);
99905 }
99906
99907 /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
99908 ** handle rows (possibly in other tables) that refer via a foreign key
99909 ** to the row just deleted. */
@@ -100005,11 +100309,12 @@
100309 if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
100310 x.nArg = argc-1;
100311 x.nUsed = 0;
100312 x.apArg = argv+1;
100313 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
100314 str.printfFlags = SQLITE_PRINTF_SQLFUNC;
100315 sqlite3XPrintf(&str, zFormat, &x);
100316 n = str.nChar;
100317 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
100318 SQLITE_DYNAMIC);
100319 }
100320 }
@@ -101380,11 +101685,11 @@
101685 */
101686 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
101687 int rc = sqlite3_overload_function(db, "MATCH", 2);
101688 assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
101689 if( rc==SQLITE_NOMEM ){
101690 sqlite3OomFault(db);
101691 }
101692 }
101693
101694 /*
101695 ** Set the LIKEOPT flag on the 2-argument function with the given name.
@@ -101795,11 +102100,11 @@
102100 if( !zKey ) return 0;
102101 if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
102102 }
102103 }else if( paiCol ){
102104 assert( nCol>1 );
102105 aiCol = (int *)sqlite3DbMallocRawNN(pParse->db, nCol*sizeof(int));
102106 if( !aiCol ) return 1;
102107 *paiCol = aiCol;
102108 }
102109
102110 for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
@@ -102741,11 +103046,10 @@
103046
103047 action = pFKey->aAction[iAction];
103048 pTrigger = pFKey->apTrigger[iAction];
103049
103050 if( action!=OE_None && !pTrigger ){
 
103051 char const *zFrom; /* Name of child table */
103052 int nFrom; /* Length in bytes of zFrom */
103053 Index *pIdx = 0; /* Parent key index for this FK */
103054 int *aiCol = 0; /* child table cols -> parent key cols */
103055 TriggerStep *pStep = 0; /* First (only) step of trigger program */
@@ -102768,15 +103072,13 @@
103072
103073 iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
103074 assert( iFromCol>=0 );
103075 assert( pIdx!=0 || (pTab->iPKey>=0 && pTab->iPKey<pTab->nCol) );
103076 assert( pIdx==0 || pIdx->aiColumn[i]>=0 );
103077 sqlite3TokenInit(&tToCol,
103078 pTab->aCol[pIdx ? pIdx->aiColumn[i] : pTab->iPKey].zName);
103079 sqlite3TokenInit(&tFromCol, pFKey->pFrom->aCol[iFromCol].zName);
 
 
103080
103081 /* Create the expression "OLD.zToCol = zFromCol". It is important
103082 ** that the "OLD.zToCol" term is on the LHS of the = operator, so
103083 ** that the affinity and collation sequence associated with the
103084 ** parent table are used for the comparison. */
@@ -102852,12 +103154,11 @@
103154 );
103155 pWhere = 0;
103156 }
103157
103158 /* Disable lookaside memory allocation */
103159 db->lookaside.bDisable++;
 
103160
103161 pTrigger = (Trigger *)sqlite3DbMallocZero(db,
103162 sizeof(Trigger) + /* struct Trigger */
103163 sizeof(TriggerStep) + /* Single step in trigger program */
103164 nFrom + 1 /* Space for pStep->zTarget */
@@ -102875,11 +103176,11 @@
103176 pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
103177 }
103178 }
103179
103180 /* Re-enable the lookaside buffer, if it was disabled earlier. */
103181 db->lookaside.bDisable--;
103182
103183 sqlite3ExprDelete(db, pWhere);
103184 sqlite3ExprDelete(db, pWhen);
103185 sqlite3ExprListDelete(db, pList);
103186 sqlite3SelectDelete(db, pSelect);
@@ -103070,11 +103371,11 @@
103371 */
103372 int n;
103373 Table *pTab = pIdx->pTable;
103374 pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
103375 if( !pIdx->zColAff ){
103376 sqlite3OomFault(db);
103377 return 0;
103378 }
103379 for(n=0; n<pIdx->nColumn; n++){
103380 i16 x = pIdx->aiColumn[n];
103381 if( x>=0 ){
@@ -103121,11 +103422,11 @@
103422 char *zColAff = pTab->zColAff;
103423 if( zColAff==0 ){
103424 sqlite3 *db = sqlite3VdbeDb(v);
103425 zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
103426 if( !zColAff ){
103427 sqlite3OomFault(db);
103428 return;
103429 }
103430
103431 for(i=0; i<pTab->nCol; i++){
103432 zColAff[i] = pTab->aCol[i].affinity;
@@ -103217,11 +103518,11 @@
103518 AutoincInfo *pInfo;
103519
103520 pInfo = pToplevel->pAinc;
103521 while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
103522 if( pInfo==0 ){
103523 pInfo = sqlite3DbMallocRawNN(pParse->db, sizeof(*pInfo));
103524 if( pInfo==0 ) return 0;
103525 pInfo->pNext = pToplevel->pAinc;
103526 pToplevel->pAinc = pInfo;
103527 pInfo->pTab = pTab;
103528 pInfo->iDb = iDb;
@@ -103241,47 +103542,59 @@
103542 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
103543 AutoincInfo *p; /* Information about an AUTOINCREMENT */
103544 sqlite3 *db = pParse->db; /* The database connection */
103545 Db *pDb; /* Database only autoinc table */
103546 int memId; /* Register holding max rowid */
 
103547 Vdbe *v = pParse->pVdbe; /* VDBE under construction */
103548
103549 /* This routine is never called during trigger-generation. It is
103550 ** only called from the top-level */
103551 assert( pParse->pTriggerTab==0 );
103552 assert( sqlite3IsToplevel(pParse) );
103553
103554 assert( v ); /* We failed long ago if this is not so */
103555 for(p = pParse->pAinc; p; p = p->pNext){
103556 static const int iLn = VDBE_OFFSET_LINENO(2);
103557 static const VdbeOpList autoInc[] = {
103558 /* 0 */ {OP_Null, 0, 0, 0},
103559 /* 1 */ {OP_Rewind, 0, 9, 0},
103560 /* 2 */ {OP_Column, 0, 0, 0},
103561 /* 3 */ {OP_Ne, 0, 7, 0},
103562 /* 4 */ {OP_Rowid, 0, 0, 0},
103563 /* 5 */ {OP_Column, 0, 1, 0},
103564 /* 6 */ {OP_Goto, 0, 9, 0},
103565 /* 7 */ {OP_Next, 0, 2, 0},
103566 /* 8 */ {OP_Integer, 0, 0, 0},
103567 /* 9 */ {OP_Close, 0, 0, 0}
103568 };
103569 VdbeOp *aOp;
103570 pDb = &db->aDb[p->iDb];
103571 memId = p->regCtr;
103572 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103573 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
 
 
103574 sqlite3VdbeLoadString(v, memId-1, p->pTab->zName);
103575 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoInc), autoInc, iLn);
103576 if( aOp==0 ) break;
103577 aOp[0].p2 = memId;
103578 aOp[0].p3 = memId+1;
103579 aOp[2].p3 = memId;
103580 aOp[3].p1 = memId-1;
103581 aOp[3].p3 = memId;
103582 aOp[3].p5 = SQLITE_JUMPIFNULL;
103583 aOp[4].p2 = memId+1;
103584 aOp[5].p3 = memId;
103585 aOp[8].p2 = memId;
103586 }
103587 }
103588
103589 /*
103590 ** Update the maximum rowid for an autoincrement calculation.
103591 **
103592 ** This routine should be called when the regRowid register holds a
103593 ** new rowid that is about to be inserted. If that new rowid is
103594 ** larger than the maximum rowid in the memId memory cell, then the
103595 ** memory cell is updated.
103596 */
103597 static void autoIncStep(Parse *pParse, int memId, int regRowid){
103598 if( memId>0 ){
103599 sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
103600 }
@@ -103292,34 +103605,47 @@
103605 ** maximum rowid values back into the sqlite_sequence register.
103606 ** Every statement that might do an INSERT into an autoincrement
103607 ** table (either directly or through triggers) needs to call this
103608 ** routine just before the "exit" code.
103609 */
103610 static SQLITE_NOINLINE void autoIncrementEnd(Parse *pParse){
103611 AutoincInfo *p;
103612 Vdbe *v = pParse->pVdbe;
103613 sqlite3 *db = pParse->db;
103614
103615 assert( v );
103616 for(p = pParse->pAinc; p; p = p->pNext){
103617 static const int iLn = VDBE_OFFSET_LINENO(2);
103618 static const VdbeOpList autoIncEnd[] = {
103619 /* 0 */ {OP_NotNull, 0, 2, 0},
103620 /* 1 */ {OP_NewRowid, 0, 0, 0},
103621 /* 2 */ {OP_MakeRecord, 0, 2, 0},
103622 /* 3 */ {OP_Insert, 0, 0, 0},
103623 /* 4 */ {OP_Close, 0, 0, 0}
103624 };
103625 VdbeOp *aOp;
103626 Db *pDb = &db->aDb[p->iDb];
 
103627 int iRec;
103628 int memId = p->regCtr;
103629
103630 iRec = sqlite3GetTempReg(pParse);
103631 assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
103632 sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
103633 aOp = sqlite3VdbeAddOpList(v, ArraySize(autoIncEnd), autoIncEnd, iLn);
103634 if( aOp==0 ) break;
103635 aOp[0].p1 = memId+1;
103636 aOp[1].p2 = memId+1;
103637 aOp[2].p1 = memId-1;
103638 aOp[2].p3 = iRec;
103639 aOp[3].p2 = iRec;
103640 aOp[3].p3 = memId+1;
103641 aOp[3].p5 = OPFLAG_APPEND;
103642 sqlite3ReleaseTempReg(pParse, iRec);
103643 }
103644 }
103645 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
103646 if( pParse->pAinc ) autoIncrementEnd(pParse);
103647 }
103648 #else
103649 /*
103650 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
103651 ** above are all no-ops
@@ -103647,11 +103973,11 @@
103973 dest.iSdst = bIdListInOrder ? regData : 0;
103974 dest.nSdst = pTab->nCol;
103975 rc = sqlite3Select(pParse, pSelect, &dest);
103976 regFromSelect = dest.iSdst;
103977 if( rc || db->mallocFailed || pParse->nErr ) goto insert_cleanup;
103978 sqlite3VdbeEndCoroutine(v, regYield);
103979 sqlite3VdbeJumpHere(v, addrTop - 1); /* label B: */
103980 assert( pSelect->pEList );
103981 nColumn = pSelect->pEList->nExpr;
103982
103983 /* Set useTempTable to TRUE if the result of the SELECT statement
@@ -103749,11 +104075,11 @@
104075 /* If this is not a view, open the table and and all indices */
104076 if( !isView ){
104077 int nIdx;
104078 nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, 0, -1, 0,
104079 &iDataCur, &iIdxCur);
104080 aRegIdx = sqlite3DbMallocRawNN(db, sizeof(int)*(nIdx+1));
104081 if( aRegIdx==0 ){
104082 goto insert_cleanup;
104083 }
104084 for(i=0; i<nIdx; i++){
104085 aRegIdx[i] = ++pParse->nMem;
@@ -103957,11 +104283,11 @@
104283 }else
104284 #endif
104285 {
104286 int isReplace; /* Set to true if constraints may cause a replace */
104287 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
104288 regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace, 0
104289 );
104290 sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
104291 sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
104292 regIns, aRegIdx, 0, appendFlag, isReplace==0);
104293 }
@@ -104038,10 +104364,63 @@
104364 #undef pTrigger
104365 #endif
104366 #ifdef tmask
104367 #undef tmask
104368 #endif
104369
104370 /*
104371 ** Meanings of bits in of pWalker->eCode for checkConstraintUnchanged()
104372 */
104373 #define CKCNSTRNT_COLUMN 0x01 /* CHECK constraint uses a changing column */
104374 #define CKCNSTRNT_ROWID 0x02 /* CHECK constraint references the ROWID */
104375
104376 /* This is the Walker callback from checkConstraintUnchanged(). Set
104377 ** bit 0x01 of pWalker->eCode if
104378 ** pWalker->eCode to 0 if this expression node references any of the
104379 ** columns that are being modifed by an UPDATE statement.
104380 */
104381 static int checkConstraintExprNode(Walker *pWalker, Expr *pExpr){
104382 if( pExpr->op==TK_COLUMN ){
104383 assert( pExpr->iColumn>=0 || pExpr->iColumn==-1 );
104384 if( pExpr->iColumn>=0 ){
104385 if( pWalker->u.aiCol[pExpr->iColumn]>=0 ){
104386 pWalker->eCode |= CKCNSTRNT_COLUMN;
104387 }
104388 }else{
104389 pWalker->eCode |= CKCNSTRNT_ROWID;
104390 }
104391 }
104392 return WRC_Continue;
104393 }
104394
104395 /*
104396 ** pExpr is a CHECK constraint on a row that is being UPDATE-ed. The
104397 ** only columns that are modified by the UPDATE are those for which
104398 ** aiChng[i]>=0, and also the ROWID is modified if chngRowid is true.
104399 **
104400 ** Return true if CHECK constraint pExpr does not use any of the
104401 ** changing columns (or the rowid if it is changing). In other words,
104402 ** return true if this CHECK constraint can be skipped when validating
104403 ** the new row in the UPDATE statement.
104404 */
104405 static int checkConstraintUnchanged(Expr *pExpr, int *aiChng, int chngRowid){
104406 Walker w;
104407 memset(&w, 0, sizeof(w));
104408 w.eCode = 0;
104409 w.xExprCallback = checkConstraintExprNode;
104410 w.u.aiCol = aiChng;
104411 sqlite3WalkExpr(&w, pExpr);
104412 if( !chngRowid ){
104413 testcase( (w.eCode & CKCNSTRNT_ROWID)!=0 );
104414 w.eCode &= ~CKCNSTRNT_ROWID;
104415 }
104416 testcase( w.eCode==0 );
104417 testcase( w.eCode==CKCNSTRNT_COLUMN );
104418 testcase( w.eCode==CKCNSTRNT_ROWID );
104419 testcase( w.eCode==(CKCNSTRNT_ROWID|CKCNSTRNT_COLUMN) );
104420 return !w.eCode;
104421 }
104422
104423 /*
104424 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
104425 ** on table pTab.
104426 **
@@ -104133,11 +104512,12 @@
104512 int regNewData, /* First register in a range holding values to insert */
104513 int regOldData, /* Previous content. 0 for INSERTs */
104514 u8 pkChng, /* Non-zero if the rowid or PRIMARY KEY changed */
104515 u8 overrideError, /* Override onError to this if not OE_Default */
104516 int ignoreDest, /* Jump to this label on an OE_Ignore resolution */
104517 int *pbMayReplace, /* OUT: Set to true if constraint may cause a replace */
104518 int *aiChng /* column i is unchanged if aiChng[i]<0 */
104519 ){
104520 Vdbe *v; /* VDBE under constrution */
104521 Index *pIdx; /* Pointer to one of the indices */
104522 Index *pPk = 0; /* The PRIMARY KEY index */
104523 sqlite3 *db; /* Database connection */
@@ -104179,14 +104559,18 @@
104559
104560 /* Test all NOT NULL constraints.
104561 */
104562 for(i=0; i<nCol; i++){
104563 if( i==pTab->iPKey ){
104564 continue; /* ROWID is never NULL */
104565 }
104566 if( aiChng && aiChng[i]<0 ){
104567 /* Don't bother checking for NOT NULL on columns that do not change */
104568 continue;
104569 }
104570 onError = pTab->aCol[i].notNull;
104571 if( onError==OE_None ) continue; /* This column is allowed to be NULL */
104572 if( overrideError!=OE_Default ){
104573 onError = overrideError;
104574 }else if( onError==OE_Default ){
104575 onError = OE_Abort;
104576 }
@@ -104231,12 +104615,15 @@
104615 if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
104616 ExprList *pCheck = pTab->pCheck;
104617 pParse->ckBase = regNewData+1;
104618 onError = overrideError!=OE_Default ? overrideError : OE_Abort;
104619 for(i=0; i<pCheck->nExpr; i++){
104620 int allOk;
104621 Expr *pExpr = pCheck->a[i].pExpr;
104622 if( aiChng && checkConstraintUnchanged(pExpr, aiChng, pkChng) ) continue;
104623 allOk = sqlite3VdbeMakeLabel(v);
104624 sqlite3ExprIfTrue(pParse, pExpr, allOk, SQLITE_JUMPIFNULL);
104625 if( onError==OE_Ignore ){
104626 sqlite3VdbeGoto(v, ignoreDest);
104627 }else{
104628 char *zName = pCheck->a[i].zName;
104629 if( zName==0 ) zName = pTab->zName;
@@ -104634,11 +105021,11 @@
105021 */
105022 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
105023 Parse *pParse, /* Parsing context */
105024 Table *pTab, /* Table to be opened */
105025 int op, /* OP_OpenRead or OP_OpenWrite */
105026 u8 p5, /* P5 value for OP_Open* opcodes (except on WITHOUT ROWID) */
105027 int iBase, /* Use this for the table cursor, if there is one */
105028 u8 *aToOpen, /* If not NULL: boolean for each table and index */
105029 int *piDataCur, /* Write the database source cursor number here */
105030 int *piIdxCur /* Write the first index cursor number here */
105031 ){
@@ -104669,18 +105056,19 @@
105056 }
105057 if( piIdxCur ) *piIdxCur = iBase;
105058 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
105059 int iIdxCur = iBase++;
105060 assert( pIdx->pSchema==pTab->pSchema );
 
 
 
105061 if( aToOpen==0 || aToOpen[i+1] ){
105062 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
105063 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
 
105064 VdbeComment((v, "%s", pIdx->zName));
105065 }
105066 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
105067 if( piDataCur ) *piDataCur = iIdxCur;
105068 }else{
105069 sqlite3VdbeChangeP5(v, p5);
105070 }
105071 }
105072 if( iBase>pParse->nTab ) pParse->nTab = iBase;
105073 return i;
105074 }
@@ -105167,11 +105555,11 @@
105555 if( rc==SQLITE_ROW ){
105556 azVals = &azCols[nCol];
105557 for(i=0; i<nCol; i++){
105558 azVals[i] = (char *)sqlite3_column_text(pStmt, i);
105559 if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
105560 sqlite3OomFault(db);
105561 goto exec_out;
105562 }
105563 }
105564 }
105565 if( xCallback(pArg, nCol, azVals, azCols) ){
@@ -107055,32 +107443,35 @@
107443 /************** End of pragma.h **********************************************/
107444 /************** Continuing where we left off in pragma.c *********************/
107445
107446 /*
107447 ** Interpret the given string as a safety level. Return 0 for OFF,
107448 ** 1 for ON or NORMAL, 2 for FULL, and 3 for EXTRA. Return 1 for an empty or
107449 ** unrecognized string argument. The FULL and EXTRA option is disallowed
107450 ** if the omitFull parameter it 1.
107451 **
107452 ** Note that the values returned are one less that the values that
107453 ** should be passed into sqlite3BtreeSetSafetyLevel(). The is done
107454 ** to support legacy SQL code. The safety level used to be boolean
107455 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
107456 */
107457 static u8 getSafetyLevel(const char *z, int omitFull, u8 dflt){
107458 /* 123456789 123456789 123 */
107459 static const char zText[] = "onoffalseyestruextrafull";
107460 static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 15, 20};
107461 static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 5, 4};
107462 static const u8 iValue[] = {1, 0, 0, 0, 1, 1, 3, 2};
107463 /* on no off false yes true extra full */
107464 int i, n;
107465 if( sqlite3Isdigit(*z) ){
107466 return (u8)sqlite3Atoi(z);
107467 }
107468 n = sqlite3Strlen30(z);
107469 for(i=0; i<ArraySize(iLength); i++){
107470 if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0
107471 && (!omitFull || iValue[i]<=1)
107472 ){
107473 return iValue[i];
107474 }
107475 }
107476 return dflt;
107477 }
@@ -107467,12 +107858,11 @@
107858 aOp[1].p1 = iDb;
107859 aOp[6].p1 = SQLITE_DEFAULT_CACHE_SIZE;
107860 }else{
107861 int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
107862 sqlite3BeginWriteOperation(pParse, 0, iDb);
107863 sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, size);
 
107864 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
107865 pDb->pSchema->cache_size = size;
107866 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
107867 }
107868 break;
@@ -107499,11 +107889,11 @@
107889 /* Malloc may fail when setting the page-size, as there is an internal
107890 ** buffer that the pager module resizes using sqlite3_realloc().
107891 */
107892 db->nextPagesize = sqlite3Atoi(zRight);
107893 if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
107894 sqlite3OomFault(db);
107895 }
107896 }
107897 break;
107898 }
107899
@@ -107706,23 +108096,22 @@
108096 static const VdbeOpList setMeta6[] = {
108097 { OP_Transaction, 0, 1, 0}, /* 0 */
108098 { OP_ReadCookie, 0, 1, BTREE_LARGEST_ROOT_PAGE},
108099 { OP_If, 1, 0, 0}, /* 2 */
108100 { OP_Halt, SQLITE_OK, OE_Abort, 0}, /* 3 */
108101 { OP_SetCookie, 0, BTREE_INCR_VACUUM, 0}, /* 4 */
 
108102 };
108103 VdbeOp *aOp;
108104 int iAddr = sqlite3VdbeCurrentAddr(v);
108105 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setMeta6));
108106 aOp = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6, iLn);
108107 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
108108 aOp[0].p1 = iDb;
108109 aOp[1].p1 = iDb;
108110 aOp[2].p2 = iAddr+4;
108111 aOp[4].p1 = iDb;
108112 aOp[4].p3 = eAuto - 1;
108113 sqlite3VdbeUsesBtree(v, iDb);
108114 }
108115 }
108116 break;
108117 }
@@ -107997,11 +108386,11 @@
108386 }
108387 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
108388
108389 /*
108390 ** PRAGMA [schema.]synchronous
108391 ** PRAGMA [schema.]synchronous=OFF|ON|NORMAL|FULL|EXTRA
108392 **
108393 ** Return or set the local value of the synchronous flag. Changing
108394 ** the local value does not make changes to the disk file and the
108395 ** default value will be restored the next time the database is
108396 ** opened.
@@ -108624,20 +109013,19 @@
109013 }
109014 {
109015 static const int iLn = VDBE_OFFSET_LINENO(2);
109016 static const VdbeOpList endCode[] = {
109017 { OP_AddImm, 1, 0, 0}, /* 0 */
109018 { OP_If, 1, 4, 0}, /* 1 */
109019 { OP_String8, 0, 3, 0}, /* 2 */
109020 { OP_ResultRow, 3, 1, 0}, /* 3 */
109021 };
109022 VdbeOp *aOp;
109023
109024 aOp = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode, iLn);
109025 if( aOp ){
109026 aOp[0].p2 = -mxErr;
 
109027 aOp[2].p4type = P4_STATIC;
109028 aOp[2].p4.z = "ok";
109029 }
109030 }
109031 }
@@ -108751,21 +109139,20 @@
109139 sqlite3VdbeUsesBtree(v, iDb);
109140 if( zRight && (pPragma->mPragFlag & PragFlag_ReadOnly)==0 ){
109141 /* Write the specified cookie value */
109142 static const VdbeOpList setCookie[] = {
109143 { OP_Transaction, 0, 1, 0}, /* 0 */
109144 { OP_SetCookie, 0, 0, 0}, /* 1 */
 
109145 };
109146 VdbeOp *aOp;
109147 sqlite3VdbeVerifyNoMallocRequired(v, ArraySize(setCookie));
109148 aOp = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie, 0);
109149 if( ONLY_IF_REALLOC_STRESS(aOp==0) ) break;
109150 aOp[0].p1 = iDb;
109151 aOp[1].p1 = iDb;
109152 aOp[1].p2 = iCookie;
109153 aOp[1].p3 = sqlite3Atoi(zRight);
109154 }else{
109155 /* Read the specified cookie value */
109156 static const VdbeOpList readCookie[] = {
109157 { OP_Transaction, 0, 0, 0}, /* 0 */
109158 { OP_ReadCookie, 0, 1, 0}, /* 1 */
@@ -109033,15 +109420,14 @@
109420 ){
109421 sqlite3 *db = pData->db;
109422 if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
109423 char *z;
109424 if( zObj==0 ) zObj = "?";
109425 z = sqlite3MPrintf(db, "malformed database schema (%s)", zObj);
109426 if( zExtra ) z = sqlite3MPrintf(db, "%z - %s", z, zExtra);
109427 sqlite3DbFree(db, *pData->pzErrMsg);
109428 *pData->pzErrMsg = z;
 
109429 }
109430 pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
109431 }
109432
109433 /*
@@ -109096,11 +109482,11 @@
109482 if( db->init.orphanTrigger ){
109483 assert( iDb==1 );
109484 }else{
109485 pData->rc = rc;
109486 if( rc==SQLITE_NOMEM ){
109487 sqlite3OomFault(db);
109488 }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
109489 corruptSchema(pData, argv[0], sqlite3_errmsg(db));
109490 }
109491 }
109492 }
@@ -109341,11 +109727,11 @@
109727 }
109728 sqlite3BtreeLeave(pDb->pBt);
109729
109730 error_out:
109731 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109732 sqlite3OomFault(db);
109733 }
109734 return rc;
109735 }
109736
109737 /*
@@ -109439,11 +109825,11 @@
109825 ** on the b-tree database, open one now. If a transaction is opened, it
109826 ** will be closed immediately after reading the meta-value. */
109827 if( !sqlite3BtreeIsInReadTrans(pBt) ){
109828 rc = sqlite3BtreeBeginTrans(pBt, 0);
109829 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
109830 sqlite3OomFault(db);
109831 }
109832 if( rc!=SQLITE_OK ) return;
109833 openedTransaction = 1;
109834 }
109835
@@ -109502,10 +109888,15 @@
109888 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
109889 if( pParse ){
109890 sqlite3 *db = pParse->db;
109891 sqlite3DbFree(db, pParse->aLabel);
109892 sqlite3ExprListDelete(db, pParse->pConstExpr);
109893 if( db ){
109894 assert( db->lookaside.bDisable >= pParse->disableLookaside );
109895 db->lookaside.bDisable -= pParse->disableLookaside;
109896 }
109897 pParse->disableLookaside = 0;
109898 }
109899 }
109900
109901 /*
109902 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
@@ -109530,11 +109921,11 @@
109921 rc = SQLITE_NOMEM;
109922 goto end_prepare;
109923 }
109924 pParse->pReprepare = pReprepare;
109925 assert( ppStmt && *ppStmt==0 );
109926 /* assert( !db->mallocFailed ); // not true with SQLITE_USE_ALLOCA */
109927 assert( sqlite3_mutex_held(db->mutex) );
109928
109929 /* Check to verify that it is possible to get a read lock on all
109930 ** database schemas. The inability to get a read lock indicates that
109931 ** some other database connection is holding a write-lock, which in
@@ -109587,23 +109978,20 @@
109978 goto end_prepare;
109979 }
109980 zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
109981 if( zSqlCopy ){
109982 sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
 
109983 pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
109984 sqlite3DbFree(db, zSqlCopy);
109985 }else{
109986 pParse->zTail = &zSql[nBytes];
109987 }
109988 }else{
109989 sqlite3RunParser(pParse, zSql, &zErrMsg);
109990 }
109991 assert( 0==pParse->nQueryLoop );
109992
 
 
 
109993 if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
109994 if( pParse->checkSchema ){
109995 schemaIsValid(pParse);
109996 }
109997 if( db->mallocFailed ){
@@ -109721,11 +110109,11 @@
110109 db = sqlite3VdbeDb(p);
110110 assert( sqlite3_mutex_held(db->mutex) );
110111 rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
110112 if( rc ){
110113 if( rc==SQLITE_NOMEM ){
110114 sqlite3OomFault(db);
110115 }
110116 assert( pNew==0 );
110117 return rc;
110118 }else{
110119 assert( pNew!=0 );
@@ -109975,11 +110363,11 @@
110363 Expr *pOffset /* OFFSET value. NULL means no offset */
110364 ){
110365 Select *pNew;
110366 Select standin;
110367 sqlite3 *db = pParse->db;
110368 pNew = sqlite3DbMallocRawNN(db, sizeof(*pNew) );
110369 if( pNew==0 ){
110370 assert( db->mallocFailed );
110371 pNew = &standin;
110372 }
110373 if( pEList==0 ){
@@ -110879,11 +111267,11 @@
111267 p->enc = ENC(db);
111268 p->db = db;
111269 p->nRef = 1;
111270 memset(&p[1], 0, nExtra);
111271 }else{
111272 sqlite3OomFault(db);
111273 }
111274 return p;
111275 }
111276
111277 /*
@@ -111540,11 +111928,11 @@
111928 if( cnt>3 ) sqlite3_randomness(sizeof(cnt), &cnt);
111929 }
111930 pCol->zName = zName;
111931 sqlite3ColumnPropertiesFromName(0, pCol);
111932 if( zName && sqlite3HashInsert(&ht, zName, pCol)==pCol ){
111933 sqlite3OomFault(db);
111934 }
111935 }
111936 sqlite3HashClear(&ht);
111937 if( db->mallocFailed ){
111938 for(j=0; j<i; j++){
@@ -111627,11 +112015,11 @@
112015 if( pTab==0 ){
112016 return 0;
112017 }
112018 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
112019 ** is disabled */
112020 assert( db->lookaside.bDisable );
112021 pTab->nRef = 1;
112022 pTab->zName = 0;
112023 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
112024 sqlite3ColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
112025 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
@@ -111723,14 +112111,12 @@
112111 p->iOffset = iOffset = ++pParse->nMem;
112112 pParse->nMem++; /* Allocate an extra register for limit+offset */
112113 sqlite3ExprCode(pParse, p->pOffset, iOffset);
112114 sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset); VdbeCoverage(v);
112115 VdbeComment((v, "OFFSET counter"));
112116 sqlite3VdbeAddOp3(v, OP_OffsetLimit, iLimit, iOffset+1, iOffset);
 
112117 VdbeComment((v, "LIMIT+OFFSET"));
 
112118 }
112119 }
112120 }
112121
112122 #ifndef SQLITE_OMIT_COMPOUND_SELECT
@@ -112143,13 +112529,12 @@
112529 p->iOffset = pPrior->iOffset;
112530 if( p->iLimit ){
112531 addr = sqlite3VdbeAddOp1(v, OP_IfNot, p->iLimit); VdbeCoverage(v);
112532 VdbeComment((v, "Jump ahead if LIMIT reached"));
112533 if( p->iOffset ){
112534 sqlite3VdbeAddOp3(v, OP_OffsetLimit,
112535 p->iLimit, p->iOffset+1, p->iOffset);
 
112536 }
112537 }
112538 explainSetInteger(iSub2, pParse->iNextSelectId);
112539 rc = sqlite3Select(pParse, p, &dest);
112540 testcase( rc!=SQLITE_OK );
@@ -112736,14 +113121,15 @@
113121 ** row of results comes from selectA or selectB. Also add explicit
113122 ** collations to the ORDER BY clause terms so that when the subqueries
113123 ** to the right and the left are evaluated, they use the correct
113124 ** collation.
113125 */
113126 aPermute = sqlite3DbMallocRawNN(db, sizeof(int)*(nOrderBy + 1));
113127 if( aPermute ){
113128 struct ExprList_item *pItem;
113129 aPermute[0] = nOrderBy;
113130 for(i=1, pItem=pOrderBy->a; i<=nOrderBy; i++, pItem++){
113131 assert( pItem->u.x.iOrderByCol>0 );
113132 assert( pItem->u.x.iOrderByCol<=p->pEList->nExpr );
113133 aPermute[i] = pItem->u.x.iOrderByCol - 1;
113134 }
113135 pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
@@ -112817,11 +113203,11 @@
113203 addr1 = sqlite3VdbeAddOp3(v, OP_InitCoroutine, regAddrA, 0, addrSelectA);
113204 VdbeComment((v, "left SELECT"));
113205 pPrior->iLimit = regLimitA;
113206 explainSetInteger(iSub1, pParse->iNextSelectId);
113207 sqlite3Select(pParse, pPrior, &destA);
113208 sqlite3VdbeEndCoroutine(v, regAddrA);
113209 sqlite3VdbeJumpHere(v, addr1);
113210
113211 /* Generate a coroutine to evaluate the SELECT statement on
113212 ** the right - the "B" select
113213 */
@@ -112834,11 +113220,11 @@
113220 p->iOffset = 0;
113221 explainSetInteger(iSub2, pParse->iNextSelectId);
113222 sqlite3Select(pParse, p, &destB);
113223 p->iLimit = savedLimit;
113224 p->iOffset = savedOffset;
113225 sqlite3VdbeEndCoroutine(v, regAddrB);
113226
113227 /* Generate a subroutine that outputs the current row of the A
113228 ** select as the next output row of the compound select.
113229 */
113230 VdbeNoopComment((v, "Output routine for A"));
@@ -114301,12 +114687,11 @@
114687 }
114688 }else{
114689 pExpr = pRight;
114690 }
114691 pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
114692 sqlite3TokenInit(&sColname, zColname);
 
114693 sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
114694 if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
114695 struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
114696 if( pSub ){
114697 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
@@ -114856,11 +115241,11 @@
115241 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
115242 sqlite3Select(pParse, pSub, &dest);
115243 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
115244 pItem->fg.viaCoroutine = 1;
115245 pItem->regResult = dest.iSdst;
115246 sqlite3VdbeEndCoroutine(v, pItem->regReturn);
115247 sqlite3VdbeJumpHere(v, addrTop-1);
115248 sqlite3ClearTempRegCache(pParse);
115249 }else{
115250 /* Generate a subroutine that will fill an ephemeral table with
115251 ** the content of this subquery. pItem->addrFillSub will point
@@ -115428,11 +115813,12 @@
115813 assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
115814
115815 if( flag ){
115816 pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
115817 pDel = pMinMax;
115818 assert( db->mallocFailed || pMinMax!=0 );
115819 if( !db->mallocFailed ){
115820 pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
115821 pMinMax->a[0].pExpr->op = TK_COLUMN;
115822 }
115823 }
115824
@@ -116001,12 +116387,11 @@
116387 pTrig->step_list = pStepList;
116388 while( pStepList ){
116389 pStepList->pTrig = pTrig;
116390 pStepList = pStepList->pNext;
116391 }
116392 sqlite3TokenInit(&nameToken, pTrig->zName);
 
116393 sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
116394 if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
116395 || sqlite3FixExpr(&sFix, pTrig->pWhen)
116396 ){
116397 goto triggerfinish_cleanup;
@@ -116038,11 +116423,11 @@
116423 Trigger *pLink = pTrig;
116424 Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
116425 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
116426 pTrig = sqlite3HashInsert(pHash, zName, pTrig);
116427 if( pTrig ){
116428 sqlite3OomFault(db);
116429 }else if( pLink->pSchema==pLink->pTabSchema ){
116430 Table *pTab;
116431 pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table);
116432 assert( pTab!=0 );
116433 pLink->pNext = pTab->pTrigger;
@@ -117014,11 +117399,11 @@
117399 }
117400
117401 /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
117402 ** Initialize aXRef[] and aToOpen[] to their default values.
117403 */
117404 aXRef = sqlite3DbMallocRawNN(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
117405 if( aXRef==0 ) goto update_cleanup;
117406 aRegIdx = aXRef+pTab->nCol;
117407 aToOpen = (u8*)(aRegIdx+nIdx);
117408 memset(aToOpen, 1, nIdx+1);
117409 aToOpen[nIdx+1] = 0;
@@ -117389,11 +117774,12 @@
117774 int bReplace = 0; /* True if REPLACE conflict resolution might happen */
117775
117776 /* Do constraint checks. */
117777 assert( regOldRowid>0 );
117778 sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
117779 regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace,
117780 aXRef);
117781
117782 /* Do FK constraint checks. */
117783 if( hasFK ){
117784 sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
117785 }
@@ -118054,11 +118440,11 @@
118440 nName = sqlite3Strlen30(zName);
118441 if( sqlite3HashFind(&db->aModule, zName) ){
118442 rc = SQLITE_MISUSE_BKPT;
118443 }else{
118444 Module *pMod;
118445 pMod = (Module *)sqlite3DbMallocRawNN(db, sizeof(Module) + nName + 1);
118446 if( pMod ){
118447 Module *pDel;
118448 char *zCopy = (char *)(&pMod[1]);
118449 memcpy(zCopy, zName, nName+1);
118450 pMod->zName = zCopy;
@@ -118067,11 +118453,11 @@
118453 pMod->xDestroy = xDestroy;
118454 pMod->pEpoTab = 0;
118455 pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,(void*)pMod);
118456 assert( pDel==0 || pDel==pMod );
118457 if( pDel ){
118458 sqlite3OomFault(db);
118459 sqlite3DbFree(db, pDel);
118460 }
118461 }
118462 }
118463 rc = sqlite3ApiExit(db, rc);
@@ -118444,11 +118830,11 @@
118830 Schema *pSchema = pTab->pSchema;
118831 const char *zName = pTab->zName;
118832 assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
118833 pOld = sqlite3HashInsert(&pSchema->tblHash, zName, pTab);
118834 if( pOld ){
118835 sqlite3OomFault(db);
118836 assert( pTab==pOld ); /* Malloc must have failed inside HashInsert() */
118837 return;
118838 }
118839 pParse->pNewTable = 0;
118840 }
@@ -118535,11 +118921,11 @@
118921 sCtx.pPrior = db->pVtabCtx;
118922 sCtx.bDeclared = 0;
118923 db->pVtabCtx = &sCtx;
118924 rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
118925 db->pVtabCtx = sCtx.pPrior;
118926 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
118927 assert( sCtx.pTab==pTab );
118928
118929 if( SQLITE_OK!=rc ){
118930 if( zErr==0 ){
118931 *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
@@ -119093,11 +119479,11 @@
119479 apVtabLock = sqlite3_realloc64(pToplevel->apVtabLock, n);
119480 if( apVtabLock ){
119481 pToplevel->apVtabLock = apVtabLock;
119482 pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
119483 }else{
119484 sqlite3OomFault(pToplevel->db);
119485 }
119486 }
119487
119488 /*
119489 ** Check to see if virtual tale module pMod can be have an eponymous
@@ -119835,11 +120221,11 @@
120221 if( nEq==0 && (pLoop->wsFlags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ) return;
120222 sqlite3StrAccumAppend(pStr, " (", 2);
120223 for(i=0; i<nEq; i++){
120224 const char *z = explainIndexColumnName(pIndex, i);
120225 if( i ) sqlite3StrAccumAppend(pStr, " AND ", 5);
120226 sqlite3XPrintf(pStr, i>=nSkip ? "%s=?" : "ANY(%s)", z);
120227 }
120228
120229 j = i;
120230 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
120231 const char *z = explainIndexColumnName(pIndex, i);
@@ -119894,17 +120280,17 @@
120280 || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
120281
120282 sqlite3StrAccumInit(&str, db, zBuf, sizeof(zBuf), SQLITE_MAX_LENGTH);
120283 sqlite3StrAccumAppendAll(&str, isSearch ? "SEARCH" : "SCAN");
120284 if( pItem->pSelect ){
120285 sqlite3XPrintf(&str, " SUBQUERY %d", pItem->iSelectId);
120286 }else{
120287 sqlite3XPrintf(&str, " TABLE %s", pItem->zName);
120288 }
120289
120290 if( pItem->zAlias ){
120291 sqlite3XPrintf(&str, " AS %s", pItem->zAlias);
120292 }
120293 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0 ){
120294 const char *zFmt = 0;
120295 Index *pIdx;
120296
@@ -119924,11 +120310,11 @@
120310 }else{
120311 zFmt = "INDEX %s";
120312 }
120313 if( zFmt ){
120314 sqlite3StrAccumAppend(&str, " USING ", 7);
120315 sqlite3XPrintf(&str, zFmt, pIdx->zName);
120316 explainIndexRange(&str, pLoop);
120317 }
120318 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
120319 const char *zRangeOp;
120320 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -119939,21 +120325,21 @@
120325 zRangeOp = ">";
120326 }else{
120327 assert( flags&WHERE_TOP_LIMIT);
120328 zRangeOp = "<";
120329 }
120330 sqlite3XPrintf(&str, " USING INTEGER PRIMARY KEY (rowid%s?)",zRangeOp);
120331 }
120332 #ifndef SQLITE_OMIT_VIRTUALTABLE
120333 else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
120334 sqlite3XPrintf(&str, " VIRTUAL TABLE INDEX %d:%s",
120335 pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
120336 }
120337 #endif
120338 #ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
120339 if( pLoop->nOut>=10 ){
120340 sqlite3XPrintf(&str, " (~%llu rows)", sqlite3LogEstToInt(pLoop->nOut));
120341 }else{
120342 sqlite3StrAccumAppend(&str, " (~1 row)", 9);
120343 }
120344 #endif
120345 zMsg = sqlite3StrAccumFinish(&str);
@@ -120254,13 +120640,11 @@
120640 regBase = pParse->nMem + 1;
120641 nReg = pLoop->u.btree.nEq + nExtraReg;
120642 pParse->nMem += nReg;
120643
120644 zAff = sqlite3DbStrDup(pParse->db,sqlite3IndexAffinityStr(pParse->db,pIdx));
120645 assert( zAff!=0 || pParse->db->mallocFailed );
 
 
120646
120647 if( nSkip ){
120648 int iIdxCur = pLevel->iIdxCur;
120649 sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
120650 VdbeCoverageIf(v, bRev==0);
@@ -120504,10 +120888,58 @@
120888 }
120889 }
120890 #else
120891 # define codeCursorHint(A,B,C) /* No-op */
120892 #endif /* SQLITE_ENABLE_CURSOR_HINTS */
120893
120894 /*
120895 ** Cursor iCur is open on an intkey b-tree (a table). Register iRowid contains
120896 ** a rowid value just read from cursor iIdxCur, open on index pIdx. This
120897 ** function generates code to do a deferred seek of cursor iCur to the
120898 ** rowid stored in register iRowid.
120899 **
120900 ** Normally, this is just:
120901 **
120902 ** OP_Seek $iCur $iRowid
120903 **
120904 ** However, if the scan currently being coded is a branch of an OR-loop and
120905 ** the statement currently being coded is a SELECT, then P3 of the OP_Seek
120906 ** is set to iIdxCur and P4 is set to point to an array of integers
120907 ** containing one entry for each column of the table cursor iCur is open
120908 ** on. For each table column, if the column is the i'th column of the
120909 ** index, then the corresponding array entry is set to (i+1). If the column
120910 ** does not appear in the index at all, the array entry is set to 0.
120911 */
120912 static void codeDeferredSeek(
120913 WhereInfo *pWInfo, /* Where clause context */
120914 Index *pIdx, /* Index scan is using */
120915 int iCur, /* Cursor for IPK b-tree */
120916 int iIdxCur /* Index cursor */
120917 ){
120918 Parse *pParse = pWInfo->pParse; /* Parse context */
120919 Vdbe *v = pParse->pVdbe; /* Vdbe to generate code within */
120920
120921 assert( iIdxCur>0 );
120922 assert( pIdx->aiColumn[pIdx->nColumn-1]==-1 );
120923
120924 sqlite3VdbeAddOp3(v, OP_Seek, iIdxCur, 0, iCur);
120925 if( (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)
120926 && DbMaskAllZero(sqlite3ParseToplevel(pParse)->writeMask)
120927 ){
120928 int i;
120929 Table *pTab = pIdx->pTable;
120930 int *ai = (int*)sqlite3DbMallocZero(pParse->db, sizeof(int)*(pTab->nCol+1));
120931 if( ai ){
120932 ai[0] = pTab->nCol;
120933 for(i=0; i<pIdx->nColumn-1; i++){
120934 assert( pIdx->aiColumn[i]<pTab->nCol );
120935 if( pIdx->aiColumn[i]>=0 ) ai[pIdx->aiColumn[i]+1] = i+1;
120936 }
120937 sqlite3VdbeChangeP4(v, -1, (char*)ai, P4_INTARRAY);
120938 }
120939 }
120940 }
120941
120942 /*
120943 ** Generate code for the start of the iLevel-th loop in the WHERE clause
120944 ** implementation described by pWInfo.
120945 */
@@ -120984,18 +121416,18 @@
121416 disableTerm(pLevel, pRangeStart);
121417 disableTerm(pLevel, pRangeEnd);
121418 if( omitTable ){
121419 /* pIdx is a covering index. No need to access the main table. */
121420 }else if( HasRowid(pIdx->pTable) ){
 
 
 
121421 if( pWInfo->eOnePass!=ONEPASS_OFF ){
121422 iRowidReg = ++pParse->nMem;
121423 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
121424 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
121425 sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, iRowidReg);
121426 VdbeCoverage(v);
121427 }else{
121428 codeDeferredSeek(pWInfo, pIdx, iCur, iIdxCur);
121429 }
121430 }else if( iCur!=iIdxCur ){
121431 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
121432 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
121433 for(j=0; j<pPk->nKeyCol; j++){
@@ -121160,11 +121592,13 @@
121592 int iTerm;
121593 for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
121594 Expr *pExpr = pWC->a[iTerm].pExpr;
121595 if( &pWC->a[iTerm] == pTerm ) continue;
121596 if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
121597 testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
121598 testcase( pWC->a[iTerm].wtFlags & TERM_CODED );
121599 if( (pWC->a[iTerm].wtFlags & (TERM_VIRTUAL|TERM_CODED))!=0 ) continue;
121600 if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
121601 testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
121602 pExpr = sqlite3ExprDup(db, pExpr, 0);
121603 pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
121604 }
@@ -121500,11 +121934,11 @@
121934 int idx;
121935 testcase( wtFlags & TERM_VIRTUAL );
121936 if( pWC->nTerm>=pWC->nSlot ){
121937 WhereTerm *pOld = pWC->a;
121938 sqlite3 *db = pWC->pWInfo->pParse->db;
121939 pWC->a = sqlite3DbMallocRawNN(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
121940 if( pWC->a==0 ){
121941 if( wtFlags & TERM_DYNAMIC ){
121942 sqlite3ExprDelete(db, p);
121943 }
121944 pWC->a = pOld;
@@ -121985,11 +122419,11 @@
122419 for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
122420 if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
122421 WhereAndInfo *pAndInfo;
122422 assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
122423 chngToIN = 0;
122424 pAndInfo = sqlite3DbMallocRawNN(db, sizeof(*pAndInfo));
122425 if( pAndInfo ){
122426 WhereClause *pAndWC;
122427 WhereTerm *pAndTerm;
122428 int j;
122429 Bitmask b = 0;
@@ -121999,11 +122433,10 @@
122433 pAndWC = &pAndInfo->wc;
122434 sqlite3WhereClauseInit(pAndWC, pWC->pWInfo);
122435 sqlite3WhereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
122436 sqlite3WhereExprAnalyze(pSrc, pAndWC);
122437 pAndWC->pOuter = pWC;
 
122438 if( !db->mallocFailed ){
122439 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
122440 assert( pAndTerm->pExpr );
122441 if( allowedOp(pAndTerm->pExpr->op) ){
122442 b |= sqlite3WhereGetMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
@@ -123742,11 +124175,11 @@
124175 rc = pVtab->pModule->xBestIndex(pVtab, p);
124176 TRACE_IDX_OUTPUTS(p);
124177
124178 if( rc!=SQLITE_OK ){
124179 if( rc==SQLITE_NOMEM ){
124180 sqlite3OomFault(pParse->db);
124181 }else if( !pVtab->zErrMsg ){
124182 sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
124183 }else{
124184 sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
124185 }
@@ -124534,11 +124967,11 @@
124967 */
124968 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
124969 WhereTerm **paNew;
124970 if( p->nLSlot>=n ) return SQLITE_OK;
124971 n = (n+7)&~7;
124972 paNew = sqlite3DbMallocRawNN(db, sizeof(p->aLTerm[0])*n);
124973 if( paNew==0 ) return SQLITE_NOMEM;
124974 memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
124975 if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
124976 p->aLTerm = paNew;
124977 p->nLSlot = n;
@@ -124831,11 +125264,11 @@
125264 whereLoopPrint(pTemplate, pBuilder->pWC);
125265 }
125266 #endif
125267 if( p==0 ){
125268 /* Allocate a new WhereLoop to add to the end of the list */
125269 *ppPrev = p = sqlite3DbMallocRawNN(db, sizeof(WhereLoop));
125270 if( p==0 ) return SQLITE_NOMEM;
125271 whereLoopInit(p);
125272 p->pNextLoop = 0;
125273 }else{
125274 /* We will be overwriting WhereLoop p[]. But before we do, first
@@ -126237,11 +126670,10 @@
126670 ** Return the cost of sorting nRow rows, assuming that the keys have
126671 ** nOrderby columns and that the first nSorted columns are already in
126672 ** order.
126673 */
126674 static LogEst whereSortingCost(
 
126675 LogEst nRow,
126676 int nOrderBy,
126677 int nSorted
126678 ){
126679 /* TUNING: Estimated cost of a full external sort, where N is
@@ -126259,18 +126691,10 @@
126691 ** below. */
126692 LogEst rScale, rSortCost;
126693 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
126694 rScale = sqlite3LogEst((nOrderBy-nSorted)*100/nOrderBy) - 66;
126695 rSortCost = nRow + estLog(nRow) + rScale + 16;
 
 
 
 
 
 
 
 
126696 return rSortCost;
126697 }
126698
126699 /*
126700 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
@@ -126328,11 +126752,11 @@
126752 }
126753
126754 /* Allocate and initialize space for aTo, aFrom and aSortCost[] */
126755 nSpace = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
126756 nSpace += sizeof(LogEst) * nOrderBy;
126757 pSpace = sqlite3DbMallocRawNN(db, nSpace);
126758 if( pSpace==0 ) return SQLITE_NOMEM;
126759 aTo = (WherePath*)pSpace;
126760 aFrom = aTo+mxChoice;
126761 memset(aFrom, 0, sizeof(aFrom[0]));
126762 pX = (WhereLoop**)(aFrom+mxChoice);
@@ -126400,11 +126824,11 @@
126824 revMask = pFrom->revLoop;
126825 }
126826 if( isOrdered>=0 && isOrdered<nOrderBy ){
126827 if( aSortCost[isOrdered]==0 ){
126828 aSortCost[isOrdered] = whereSortingCost(
126829 nRowEst, nOrderBy, isOrdered
126830 );
126831 }
126832 rCost = sqlite3LogEstAdd(rUnsorted, aSortCost[isOrdered]);
126833
126834 WHERETRACE(0x002,
@@ -126813,11 +127237,11 @@
127237 WhereLevel *pLevel; /* A single level in pWInfo->a[] */
127238 WhereLoop *pLoop; /* Pointer to a single WhereLoop object */
127239 int ii; /* Loop counter */
127240 sqlite3 *db; /* Database connection */
127241 int rc; /* Return code */
127242 u8 bFordelete = 0; /* OPFLAG_FORDELETE or zero, as appropriate */
127243
127244 assert( (wctrlFlags & WHERE_ONEPASS_MULTIROW)==0 || (
127245 (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
127246 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
127247 ));
@@ -127058,20 +127482,19 @@
127482 WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
127483 pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
127484
127485 /* If the caller is an UPDATE or DELETE statement that is requesting
127486 ** to use a one-pass algorithm, determine if this is appropriate.
 
 
127487 */
127488 assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
127489 if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0 ){
127490 int wsFlags = pWInfo->a[0].pWLoop->wsFlags;
127491 int bOnerow = (wsFlags & WHERE_ONEROW)!=0;
127492 if( bOnerow
127493 || ((wctrlFlags & WHERE_ONEPASS_MULTIROW)!=0
127494 && 0==(wsFlags & WHERE_VIRTUALTABLE))
127495 ){
127496 pWInfo->eOnePass = bOnerow ? ONEPASS_SINGLE : ONEPASS_MULTI;
127497 if( HasRowid(pTabList->a[0].pTab) && (wsFlags & WHERE_IDX_ONLY) ){
127498 if( wctrlFlags & WHERE_ONEPASS_MULTIROW ){
127499 bFordelete = OPFLAG_FORDELETE;
127500 }
@@ -127511,10 +127934,19 @@
127934 /*
127935 ** An instance of this structure holds the ATTACH key and the key type.
127936 */
127937 struct AttachKey { int type; Token key; };
127938
127939 /*
127940 ** Disable lookaside memory allocation for objects that might be
127941 ** shared across database connections.
127942 */
127943 static void disableLookaside(Parse *pParse){
127944 pParse->disableLookaside++;
127945 pParse->db->lookaside.bDisable++;
127946 }
127947
127948
127949 /*
127950 ** For a compound SELECT statement, make sure p->pPrior->pNext==p for
127951 ** all elements in the list. And make sure list length does not exceed
127952 ** SQLITE_LIMIT_COMPOUND_SELECT.
@@ -127593,11 +128025,11 @@
128025
128026 /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
128027 ** unary TK_ISNULL or TK_NOTNULL expression. */
128028 static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
128029 sqlite3 *db = pParse->db;
128030 if( pA && pY && pY->op==TK_NULL ){
128031 pA->op = (u8)op;
128032 sqlite3ExprDelete(db, pA->pRight);
128033 pA->pRight = 0;
128034 }
128035 }
@@ -129635,11 +130067,11 @@
130067 sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy4,0,0,yymsp[-2].minor.yy4);
130068 }
130069 break;
130070 case 27: /* createkw ::= CREATE */
130071 {
130072 disableLookaside(pParse);
130073 yygotominor.yy0 = yymsp[0].minor.yy0;
130074 }
130075 break;
130076 case 28: /* ifnotexists ::= */
130077 case 31: /* temp ::= */ yytestcase(yyruleno==31);
@@ -130717,11 +131149,11 @@
131149 sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
131150 }
131151 break;
131152 case 307: /* add_column_fullname ::= fullname */
131153 {
131154 disableLookaside(pParse);
131155 sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy259);
131156 }
131157 break;
131158 case 310: /* cmd ::= create_vtab */
131159 {sqlite3VtabFinishParse(pParse,0);}
@@ -131097,16 +131529,96 @@
131529 ** parser for analysis.
131530 */
131531 /* #include "sqliteInt.h" */
131532 /* #include <stdlib.h> */
131533
131534 /* Character classes for tokenizing
131535 **
131536 ** In the sqlite3GetToken() function, a switch() on aiClass[c] is implemented
131537 ** using a lookup table, whereas a switch() directly on c uses a binary search.
131538 ** The lookup table is much faster. To maximize speed, and to ensure that
131539 ** a lookup table is used, all of the classes need to be small integers and
131540 ** all of them need to be used within the switch.
131541 */
131542 #define CC_X 0 /* The letter 'x', or start of BLOB literal */
131543 #define CC_KYWD 1 /* Alphabetics or '_'. Usable in a keyword */
131544 #define CC_ID 2 /* unicode characters usable in IDs */
131545 #define CC_DIGIT 3 /* Digits */
131546 #define CC_DOLLAR 4 /* '$' */
131547 #define CC_VARALPHA 5 /* '@', '#', ':'. Alphabetic SQL variables */
131548 #define CC_VARNUM 6 /* '?'. Numeric SQL variables */
131549 #define CC_SPACE 7 /* Space characters */
131550 #define CC_QUOTE 8 /* '"', '\'', or '`'. String literals, quoted ids */
131551 #define CC_QUOTE2 9 /* '['. [...] style quoted ids */
131552 #define CC_PIPE 10 /* '|'. Bitwise OR or concatenate */
131553 #define CC_MINUS 11 /* '-'. Minus or SQL-style comment */
131554 #define CC_LT 12 /* '<'. Part of < or <= or <> */
131555 #define CC_GT 13 /* '>'. Part of > or >= */
131556 #define CC_EQ 14 /* '='. Part of = or == */
131557 #define CC_BANG 15 /* '!'. Part of != */
131558 #define CC_SLASH 16 /* '/'. / or c-style comment */
131559 #define CC_LP 17 /* '(' */
131560 #define CC_RP 18 /* ')' */
131561 #define CC_SEMI 19 /* ';' */
131562 #define CC_PLUS 20 /* '+' */
131563 #define CC_STAR 21 /* '*' */
131564 #define CC_PERCENT 22 /* '%' */
131565 #define CC_COMMA 23 /* ',' */
131566 #define CC_AND 24 /* '&' */
131567 #define CC_TILDA 25 /* '~' */
131568 #define CC_DOT 26 /* '.' */
131569 #define CC_ILLEGAL 27 /* Illegal character */
131570
131571 static const unsigned char aiClass[] = {
131572 #ifdef SQLITE_ASCII
131573 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
131574 /* 0x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 7, 7, 27, 7, 7, 27, 27,
131575 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131576 /* 2x */ 7, 15, 8, 5, 4, 22, 24, 8, 17, 18, 21, 20, 23, 11, 26, 16,
131577 /* 3x */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 5, 19, 12, 14, 13, 6,
131578 /* 4x */ 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131579 /* 5x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 9, 27, 27, 27, 1,
131580 /* 6x */ 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
131581 /* 7x */ 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 10, 27, 25, 27,
131582 /* 8x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131583 /* 9x */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131584 /* Ax */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131585 /* Bx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131586 /* Cx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131587 /* Dx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131588 /* Ex */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
131589 /* Fx */ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
131590 #endif
131591 #ifdef SQLITE_EBCDIC
131592 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
131593 /* 0x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 7, 7, 27, 27,
131594 /* 1x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131595 /* 2x */ 27, 27, 27, 27, 27, 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131596 /* 3x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
131597 /* 4x */ 7, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 12, 17, 20, 10,
131598 /* 5x */ 24, 27, 27, 27, 27, 27, 27, 27, 27, 27, 15, 4, 21, 18, 19, 27,
131599 /* 6x */ 11, 16, 27, 27, 27, 27, 27, 27, 27, 27, 27, 23, 22, 1, 13, 7,
131600 /* 7x */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 8, 5, 5, 5, 8, 14, 8,
131601 /* 8x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131602 /* 9x */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131603 /* 9x */ 25, 1, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
131604 /* Bx */ 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 9, 27, 27, 27, 27, 27,
131605 /* Cx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131606 /* Dx */ 27, 1, 1, 1, 1, 1, 1, 1, 1, 1, 27, 27, 27, 27, 27, 27,
131607 /* Ex */ 27, 27, 1, 1, 1, 1, 1, 0, 1, 1, 27, 27, 27, 27, 27, 27,
131608 /* Fx */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 27, 27, 27, 27, 27, 27,
131609 #endif
131610 };
131611
131612 /*
131613 ** The charMap() macro maps alphabetic characters (only) into their
131614 ** lower-case ASCII equivalent. On ASCII machines, this is just
131615 ** an upper-to-lower case map. On EBCDIC machines we also need
131616 ** to adjust the encoding. The mapping is only valid for alphabetics
131617 ** which are the only characters for which this feature is used.
131618 **
131619 ** Used by keywordhash.h
131620 */
131621 #ifdef SQLITE_ASCII
131622 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
131623 #endif
131624 #ifdef SQLITE_EBCDIC
@@ -131136,11 +131648,11 @@
131648 ** The sqlite3KeywordCode function looks up an identifier to determine if
131649 ** it is a keyword. If it is a keyword, the token code of that keyword is
131650 ** returned. If the input is not a keyword, TK_ID is returned.
131651 **
131652 ** The implementation of this routine was generated by a program,
131653 ** mkkeywordhash.c, located in the tool subdirectory of the distribution.
131654 ** The output of the mkkeywordhash.c program is written into a file
131655 ** named keywordhash.h and then included into this source file by
131656 ** the #include below.
131657 */
131658 /************** Include keywordhash.h in the middle of tokenize.c ************/
@@ -131277,142 +131789,151 @@
131789 TK_DROP, TK_FAIL, TK_FROM, TK_JOIN_KW, TK_LIKE_KW,
131790 TK_BY, TK_IF, TK_ISNULL, TK_ORDER, TK_RESTRICT,
131791 TK_JOIN_KW, TK_ROLLBACK, TK_ROW, TK_UNION, TK_USING,
131792 TK_VACUUM, TK_VIEW, TK_INITIALLY, TK_ALL,
131793 };
131794 int i, j;
131795 const char *zKW;
131796 if( n>=2 ){
131797 i = ((charMap(z[0])*4) ^ (charMap(z[n-1])*3) ^ n) % 127;
131798 for(i=((int)aHash[i])-1; i>=0; i=((int)aNext[i])-1){
131799 if( aLen[i]!=n ) continue;
131800 j = 0;
131801 zKW = &zText[aOffset[i]];
131802 #ifdef SQLITE_ASCII
131803 while( j<n && (z[j]&~0x20)==zKW[j] ){ j++; }
131804 #endif
131805 #ifdef SQLITE_EBCDIC
131806 while( j<n && toupper(z[j])==zKW[j] ){ j++; }
131807 #endif
131808 if( j<n ) continue;
131809 testcase( i==0 ); /* REINDEX */
131810 testcase( i==1 ); /* INDEXED */
131811 testcase( i==2 ); /* INDEX */
131812 testcase( i==3 ); /* DESC */
131813 testcase( i==4 ); /* ESCAPE */
131814 testcase( i==5 ); /* EACH */
131815 testcase( i==6 ); /* CHECK */
131816 testcase( i==7 ); /* KEY */
131817 testcase( i==8 ); /* BEFORE */
131818 testcase( i==9 ); /* FOREIGN */
131819 testcase( i==10 ); /* FOR */
131820 testcase( i==11 ); /* IGNORE */
131821 testcase( i==12 ); /* REGEXP */
131822 testcase( i==13 ); /* EXPLAIN */
131823 testcase( i==14 ); /* INSTEAD */
131824 testcase( i==15 ); /* ADD */
131825 testcase( i==16 ); /* DATABASE */
131826 testcase( i==17 ); /* AS */
131827 testcase( i==18 ); /* SELECT */
131828 testcase( i==19 ); /* TABLE */
131829 testcase( i==20 ); /* LEFT */
131830 testcase( i==21 ); /* THEN */
131831 testcase( i==22 ); /* END */
131832 testcase( i==23 ); /* DEFERRABLE */
131833 testcase( i==24 ); /* ELSE */
131834 testcase( i==25 ); /* EXCEPT */
131835 testcase( i==26 ); /* TRANSACTION */
131836 testcase( i==27 ); /* ACTION */
131837 testcase( i==28 ); /* ON */
131838 testcase( i==29 ); /* NATURAL */
131839 testcase( i==30 ); /* ALTER */
131840 testcase( i==31 ); /* RAISE */
131841 testcase( i==32 ); /* EXCLUSIVE */
131842 testcase( i==33 ); /* EXISTS */
131843 testcase( i==34 ); /* SAVEPOINT */
131844 testcase( i==35 ); /* INTERSECT */
131845 testcase( i==36 ); /* TRIGGER */
131846 testcase( i==37 ); /* REFERENCES */
131847 testcase( i==38 ); /* CONSTRAINT */
131848 testcase( i==39 ); /* INTO */
131849 testcase( i==40 ); /* OFFSET */
131850 testcase( i==41 ); /* OF */
131851 testcase( i==42 ); /* SET */
131852 testcase( i==43 ); /* TEMPORARY */
131853 testcase( i==44 ); /* TEMP */
131854 testcase( i==45 ); /* OR */
131855 testcase( i==46 ); /* UNIQUE */
131856 testcase( i==47 ); /* QUERY */
131857 testcase( i==48 ); /* WITHOUT */
131858 testcase( i==49 ); /* WITH */
131859 testcase( i==50 ); /* OUTER */
131860 testcase( i==51 ); /* RELEASE */
131861 testcase( i==52 ); /* ATTACH */
131862 testcase( i==53 ); /* HAVING */
131863 testcase( i==54 ); /* GROUP */
131864 testcase( i==55 ); /* UPDATE */
131865 testcase( i==56 ); /* BEGIN */
131866 testcase( i==57 ); /* INNER */
131867 testcase( i==58 ); /* RECURSIVE */
131868 testcase( i==59 ); /* BETWEEN */
131869 testcase( i==60 ); /* NOTNULL */
131870 testcase( i==61 ); /* NOT */
131871 testcase( i==62 ); /* NO */
131872 testcase( i==63 ); /* NULL */
131873 testcase( i==64 ); /* LIKE */
131874 testcase( i==65 ); /* CASCADE */
131875 testcase( i==66 ); /* ASC */
131876 testcase( i==67 ); /* DELETE */
131877 testcase( i==68 ); /* CASE */
131878 testcase( i==69 ); /* COLLATE */
131879 testcase( i==70 ); /* CREATE */
131880 testcase( i==71 ); /* CURRENT_DATE */
131881 testcase( i==72 ); /* DETACH */
131882 testcase( i==73 ); /* IMMEDIATE */
131883 testcase( i==74 ); /* JOIN */
131884 testcase( i==75 ); /* INSERT */
131885 testcase( i==76 ); /* MATCH */
131886 testcase( i==77 ); /* PLAN */
131887 testcase( i==78 ); /* ANALYZE */
131888 testcase( i==79 ); /* PRAGMA */
131889 testcase( i==80 ); /* ABORT */
131890 testcase( i==81 ); /* VALUES */
131891 testcase( i==82 ); /* VIRTUAL */
131892 testcase( i==83 ); /* LIMIT */
131893 testcase( i==84 ); /* WHEN */
131894 testcase( i==85 ); /* WHERE */
131895 testcase( i==86 ); /* RENAME */
131896 testcase( i==87 ); /* AFTER */
131897 testcase( i==88 ); /* REPLACE */
131898 testcase( i==89 ); /* AND */
131899 testcase( i==90 ); /* DEFAULT */
131900 testcase( i==91 ); /* AUTOINCREMENT */
131901 testcase( i==92 ); /* TO */
131902 testcase( i==93 ); /* IN */
131903 testcase( i==94 ); /* CAST */
131904 testcase( i==95 ); /* COLUMN */
131905 testcase( i==96 ); /* COMMIT */
131906 testcase( i==97 ); /* CONFLICT */
131907 testcase( i==98 ); /* CROSS */
131908 testcase( i==99 ); /* CURRENT_TIMESTAMP */
131909 testcase( i==100 ); /* CURRENT_TIME */
131910 testcase( i==101 ); /* PRIMARY */
131911 testcase( i==102 ); /* DEFERRED */
131912 testcase( i==103 ); /* DISTINCT */
131913 testcase( i==104 ); /* IS */
131914 testcase( i==105 ); /* DROP */
131915 testcase( i==106 ); /* FAIL */
131916 testcase( i==107 ); /* FROM */
131917 testcase( i==108 ); /* FULL */
131918 testcase( i==109 ); /* GLOB */
131919 testcase( i==110 ); /* BY */
131920 testcase( i==111 ); /* IF */
131921 testcase( i==112 ); /* ISNULL */
131922 testcase( i==113 ); /* ORDER */
131923 testcase( i==114 ); /* RESTRICT */
131924 testcase( i==115 ); /* RIGHT */
131925 testcase( i==116 ); /* ROLLBACK */
131926 testcase( i==117 ); /* ROW */
131927 testcase( i==118 ); /* UNION */
131928 testcase( i==119 ); /* USING */
131929 testcase( i==120 ); /* VACUUM */
131930 testcase( i==121 ); /* VIEW */
131931 testcase( i==122 ); /* INITIALLY */
131932 testcase( i==123 ); /* ALL */
131933 *pType = aCode[i];
131934 break;
131935 }
131936 }
131937 return n;
131938 }
131939 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
@@ -131469,74 +131990,76 @@
131990 SQLITE_PRIVATE int sqlite3IsIdChar(u8 c){ return IdChar(c); }
131991 #endif
131992
131993
131994 /*
131995 ** Return the length (in bytes) of the token that begins at z[0].
131996 ** Store the token type in *tokenType before returning.
131997 */
131998 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
131999 int i, c;
132000 switch( aiClass[*z] ){ /* Switch on the character-class of the first byte
132001 ** of the token. See the comment on the CC_ defines
132002 ** above. */
132003 case CC_SPACE: {
132004 testcase( z[0]==' ' );
132005 testcase( z[0]=='\t' );
132006 testcase( z[0]=='\n' );
132007 testcase( z[0]=='\f' );
132008 testcase( z[0]=='\r' );
132009 for(i=1; sqlite3Isspace(z[i]); i++){}
132010 *tokenType = TK_SPACE;
132011 return i;
132012 }
132013 case CC_MINUS: {
132014 if( z[1]=='-' ){
132015 for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
132016 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
132017 return i;
132018 }
132019 *tokenType = TK_MINUS;
132020 return 1;
132021 }
132022 case CC_LP: {
132023 *tokenType = TK_LP;
132024 return 1;
132025 }
132026 case CC_RP: {
132027 *tokenType = TK_RP;
132028 return 1;
132029 }
132030 case CC_SEMI: {
132031 *tokenType = TK_SEMI;
132032 return 1;
132033 }
132034 case CC_PLUS: {
132035 *tokenType = TK_PLUS;
132036 return 1;
132037 }
132038 case CC_STAR: {
132039 *tokenType = TK_STAR;
132040 return 1;
132041 }
132042 case CC_SLASH: {
132043 if( z[1]!='*' || z[2]==0 ){
132044 *tokenType = TK_SLASH;
132045 return 1;
132046 }
132047 for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
132048 if( c ) i++;
132049 *tokenType = TK_SPACE; /* IMP: R-22934-25134 */
132050 return i;
132051 }
132052 case CC_PERCENT: {
132053 *tokenType = TK_REM;
132054 return 1;
132055 }
132056 case CC_EQ: {
132057 *tokenType = TK_EQ;
132058 return 1 + (z[1]=='=');
132059 }
132060 case CC_LT: {
132061 if( (c=z[1])=='=' ){
132062 *tokenType = TK_LE;
132063 return 2;
132064 }else if( c=='>' ){
132065 *tokenType = TK_NE;
@@ -131547,11 +132070,11 @@
132070 }else{
132071 *tokenType = TK_LT;
132072 return 1;
132073 }
132074 }
132075 case CC_GT: {
132076 if( (c=z[1])=='=' ){
132077 *tokenType = TK_GE;
132078 return 2;
132079 }else if( c=='>' ){
132080 *tokenType = TK_RSHIFT;
@@ -131559,43 +132082,41 @@
132082 }else{
132083 *tokenType = TK_GT;
132084 return 1;
132085 }
132086 }
132087 case CC_BANG: {
132088 if( z[1]!='=' ){
132089 *tokenType = TK_ILLEGAL;
132090 return 2;
132091 }else{
132092 *tokenType = TK_NE;
132093 return 2;
132094 }
132095 }
132096 case CC_PIPE: {
132097 if( z[1]!='|' ){
132098 *tokenType = TK_BITOR;
132099 return 1;
132100 }else{
132101 *tokenType = TK_CONCAT;
132102 return 2;
132103 }
132104 }
132105 case CC_COMMA: {
132106 *tokenType = TK_COMMA;
132107 return 1;
132108 }
132109 case CC_AND: {
132110 *tokenType = TK_BITAND;
132111 return 1;
132112 }
132113 case CC_TILDA: {
132114 *tokenType = TK_BITNOT;
132115 return 1;
132116 }
132117 case CC_QUOTE: {
 
 
132118 int delim = z[0];
132119 testcase( delim=='`' );
132120 testcase( delim=='\'' );
132121 testcase( delim=='"' );
132122 for(i=1; (c=z[i])!=0; i++){
@@ -131616,11 +132137,11 @@
132137 }else{
132138 *tokenType = TK_ILLEGAL;
132139 return i;
132140 }
132141 }
132142 case CC_DOT: {
132143 #ifndef SQLITE_OMIT_FLOATING_POINT
132144 if( !sqlite3Isdigit(z[1]) )
132145 #endif
132146 {
132147 *tokenType = TK_DOT;
@@ -131627,12 +132148,11 @@
132148 return 1;
132149 }
132150 /* If the next character is a digit, this is a floating point
132151 ** number that begins with ".". Fall thru into the next case */
132152 }
132153 case CC_DIGIT: {
 
132154 testcase( z[0]=='0' ); testcase( z[0]=='1' ); testcase( z[0]=='2' );
132155 testcase( z[0]=='3' ); testcase( z[0]=='4' ); testcase( z[0]=='5' );
132156 testcase( z[0]=='6' ); testcase( z[0]=='7' ); testcase( z[0]=='8' );
132157 testcase( z[0]=='9' );
132158 *tokenType = TK_INTEGER;
@@ -131663,26 +132183,22 @@
132183 *tokenType = TK_ILLEGAL;
132184 i++;
132185 }
132186 return i;
132187 }
132188 case CC_QUOTE2: {
132189 for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
132190 *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
132191 return i;
132192 }
132193 case CC_VARNUM: {
132194 *tokenType = TK_VARIABLE;
132195 for(i=1; sqlite3Isdigit(z[i]); i++){}
132196 return i;
132197 }
132198 case CC_DOLLAR:
132199 case CC_VARALPHA: {
 
 
 
 
132200 int n = 0;
132201 testcase( z[0]=='$' ); testcase( z[0]=='@' );
132202 testcase( z[0]==':' ); testcase( z[0]=='#' );
132203 *tokenType = TK_VARIABLE;
132204 for(i=1; (c=z[i])!=0; i++){
@@ -131707,12 +132223,24 @@
132223 }
132224 }
132225 if( n==0 ) *tokenType = TK_ILLEGAL;
132226 return i;
132227 }
132228 case CC_KYWD: {
132229 for(i=1; aiClass[z[i]]<=CC_KYWD; i++){}
132230 if( IdChar(z[i]) ){
132231 /* This token started out using characters that can appear in keywords,
132232 ** but z[i] is a character not allowed within keywords, so this must
132233 ** be an identifier instead */
132234 i++;
132235 break;
132236 }
132237 *tokenType = TK_ID;
132238 return keywordCode((char*)z, i, tokenType);
132239 }
132240 #ifndef SQLITE_OMIT_BLOB_LITERAL
132241 case CC_X: {
132242 testcase( z[0]=='x' ); testcase( z[0]=='X' );
132243 if( z[1]=='\'' ){
132244 *tokenType = TK_BLOB;
132245 for(i=2; sqlite3Isxdigit(z[i]); i++){}
132246 if( z[i]!='\'' || i%2 ){
@@ -131720,24 +132248,26 @@
132248 while( z[i] && z[i]!='\'' ){ i++; }
132249 }
132250 if( z[i] ) i++;
132251 return i;
132252 }
132253 /* If it is not a BLOB literal, then it must be an ID, since no
132254 ** SQL keywords start with the letter 'x'. Fall through */
132255 }
132256 #endif
132257 case CC_ID: {
132258 i = 1;
132259 break;
132260 }
132261 default: {
132262 *tokenType = TK_ILLEGAL;
132263 return 1;
 
 
 
 
132264 }
132265 }
132266 while( IdChar(z[i]) ){ i++; }
132267 *tokenType = TK_ID;
132268 return i;
132269 }
132270
132271 /*
132272 ** Run the parser on the given SQL string. The parser structure is
132273 ** passed in. An SQLITE_ status code is returned. If an error occurs
@@ -131749,11 +132279,10 @@
132279 int nErr = 0; /* Number of errors encountered */
132280 int i; /* Loop counter */
132281 void *pEngine; /* The LEMON-generated LALR(1) parser */
132282 int tokenType; /* type of the next token */
132283 int lastTokenParsed = -1; /* type of the previous token */
 
132284 sqlite3 *db = pParse->db; /* The database connection */
132285 int mxSqlLen; /* Max length of an SQL string */
132286
132287 assert( zSql!=0 );
132288 mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
@@ -131765,20 +132294,18 @@
132294 i = 0;
132295 assert( pzErrMsg!=0 );
132296 /* sqlite3ParserTrace(stdout, "parser: "); */
132297 pEngine = sqlite3ParserAlloc(sqlite3Malloc);
132298 if( pEngine==0 ){
132299 sqlite3OomFault(db);
132300 return SQLITE_NOMEM;
132301 }
132302 assert( pParse->pNewTable==0 );
132303 assert( pParse->pNewTrigger==0 );
132304 assert( pParse->nVar==0 );
132305 assert( pParse->nzVar==0 );
132306 assert( pParse->azVar==0 );
 
 
132307 while( zSql[i]!=0 ){
132308 assert( i>=0 );
132309 pParse->sLastToken.z = &zSql[i];
132310 pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
132311 i += pParse->sLastToken.n;
@@ -131787,11 +132314,10 @@
132314 break;
132315 }
132316 if( tokenType>=TK_SPACE ){
132317 assert( tokenType==TK_SPACE || tokenType==TK_ILLEGAL );
132318 if( db->u1.isInterrupted ){
 
132319 pParse->rc = SQLITE_INTERRUPT;
132320 break;
132321 }
132322 if( tokenType==TK_ILLEGAL ){
132323 sqlite3ErrorMsg(pParse, "unrecognized token: \"%T\"",
@@ -131822,11 +132348,10 @@
132348 sqlite3ParserStackPeak(pEngine)
132349 );
132350 sqlite3_mutex_leave(sqlite3MallocMutex());
132351 #endif /* YYDEBUG */
132352 sqlite3ParserFree(pEngine, sqlite3_free);
 
132353 if( db->mallocFailed ){
132354 pParse->rc = SQLITE_NOMEM;
132355 }
132356 if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
132357 pParse->zErrMsg = sqlite3MPrintf(db, "%s", sqlite3ErrStr(pParse->rc));
@@ -132963,16 +133488,16 @@
133488 p->pNext = db->lookaside.pFree;
133489 db->lookaside.pFree = p;
133490 p = (LookasideSlot*)&((u8*)p)[sz];
133491 }
133492 db->lookaside.pEnd = p;
133493 db->lookaside.bDisable = 0;
133494 db->lookaside.bMalloced = pBuf==0 ?1:0;
133495 }else{
133496 db->lookaside.pStart = db;
133497 db->lookaside.pEnd = db;
133498 db->lookaside.bDisable = 1;
133499 db->lookaside.bMalloced = 0;
133500 }
133501 #endif /* SQLITE_OMIT_LOOKASIDE */
133502 return SQLITE_OK;
133503 }
@@ -134473,11 +134998,11 @@
134998 /* A malloc() may have failed within the call to sqlite3_value_text16()
134999 ** above. If this is the case, then the db->mallocFailed flag needs to
135000 ** be cleared before returning. Do this directly, instead of via
135001 ** sqlite3ApiExit(), to avoid setting the database handle error message.
135002 */
135003 sqlite3OomClear(db);
135004 }
135005 sqlite3_mutex_leave(db->mutex);
135006 return z;
135007 }
135008 #endif /* SQLITE_OMIT_UTF16 */
@@ -135111,11 +135636,11 @@
135636
135637 /* Parse the filename/URI argument. */
135638 db->openFlags = flags;
135639 rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
135640 if( rc!=SQLITE_OK ){
135641 if( rc==SQLITE_NOMEM ) sqlite3OomFault(db);
135642 sqlite3ErrorWithMsg(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
135643 sqlite3_free(zErrMsg);
135644 goto opendb_out;
135645 }
135646
@@ -135831,11 +136356,11 @@
136356 ** process aborts. If X is false and assert() is disabled, then the
136357 ** return value is zero.
136358 */
136359 case SQLITE_TESTCTRL_ASSERT: {
136360 volatile int x = 0;
136361 assert( /*side-effects-ok*/ (x = va_arg(ap,int))!=0 );
136362 rc = x;
136363 break;
136364 }
136365
136366
@@ -136864,10 +137389,16 @@
137389 #define _FTSINT_H
137390
137391 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
137392 # define NDEBUG 1
137393 #endif
137394
137395 /* FTS3/FTS4 require virtual tables */
137396 #ifdef SQLITE_OMIT_VIRTUALTABLE
137397 # undef SQLITE_ENABLE_FTS3
137398 # undef SQLITE_ENABLE_FTS4
137399 #endif
137400
137401 /*
137402 ** FTS4 is really an extension for FTS3. It is enabled using the
137403 ** SQLITE_ENABLE_FTS3 macro. But to avoid confusion we also all
137404 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
@@ -146361,10 +146892,11 @@
146892
146893 zName = sqlite3_value_text(argv[0]);
146894 nName = sqlite3_value_bytes(argv[0])+1;
146895
146896 if( argc==2 ){
146897 #ifdef SQLITE_ENABLE_FTS3_TOKENIZER
146898 void *pOld;
146899 int n = sqlite3_value_bytes(argv[1]);
146900 if( zName==0 || n!=sizeof(pPtr) ){
146901 sqlite3_result_error(context, "argument type mismatch", -1);
146902 return;
@@ -146373,11 +146905,18 @@
146905 pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
146906 if( pOld==pPtr ){
146907 sqlite3_result_error(context, "out of memory", -1);
146908 return;
146909 }
146910 #else
146911 sqlite3_result_error(context, "fts3tokenize: "
146912 "disabled - rebuild with -DSQLITE_ENABLE_FTS3_TOKENIZER", -1
146913 );
146914 return;
146915 #endif /* SQLITE_ENABLE_FTS3_TOKENIZER */
146916 }else
146917 {
146918 if( zName ){
146919 pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
146920 }
146921 if( !pPtr ){
146922 char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
@@ -146622,10 +147161,11 @@
147161 sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
147162 }
147163 Tcl_DecrRefCount(pRet);
147164 }
147165
147166 #ifdef SQLITE_ENABLE_FTS3_TOKENIZER
147167 static
147168 int registerTokenizer(
147169 sqlite3 *db,
147170 char *zName,
147171 const sqlite3_tokenizer_module *p
@@ -146643,10 +147183,12 @@
147183 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
147184 sqlite3_step(pStmt);
147185
147186 return sqlite3_finalize(pStmt);
147187 }
147188 #endif /* SQLITE_ENABLE_FTS3_TOKENIZER */
147189
147190
147191 static
147192 int queryTokenizer(
147193 sqlite3 *db,
147194 char *zName,
@@ -146714,15 +147256,17 @@
147256 assert( rc==SQLITE_ERROR );
147257 assert( p2==0 );
147258 assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
147259
147260 /* Test the storage function */
147261 #ifdef SQLITE_ENABLE_FTS3_TOKENIZER
147262 rc = registerTokenizer(db, "nosuchtokenizer", p1);
147263 assert( rc==SQLITE_OK );
147264 rc = queryTokenizer(db, "nosuchtokenizer", &p2);
147265 assert( rc==SQLITE_OK );
147266 assert( p2==p1 );
147267 #endif
147268
147269 sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
147270 }
147271
147272 #endif
@@ -164601,11 +165145,13 @@
165145 StatTable *pTab = 0;
165146 int rc = SQLITE_OK;
165147 int iDb;
165148
165149 if( argc>=4 ){
165150 Token nm;
165151 sqlite3TokenInit(&nm, (char*)argv[3]);
165152 iDb = sqlite3FindDb(db, &nm);
165153 if( iDb<0 ){
165154 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
165155 return SQLITE_ERROR;
165156 }
165157 }else{
@@ -165186,11 +165732,15 @@
165732 /* #include <assert.h> */
165733 /* #include <string.h> */
165734 /* #include <stdlib.h> */
165735 /* #include <stdarg.h> */
165736
165737 /* Mark a function parameter as unused, to suppress nuisance compiler
165738 ** warnings. */
165739 #ifndef UNUSED_PARAM
165740 # define UNUSED_PARAM(X) (void)(X)
165741 #endif
165742
165743 #ifndef LARGEST_INT64
165744 # define LARGEST_INT64 (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
165745 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
165746 #endif
@@ -165431,14 +165981,37 @@
165981 static void jsonAppendString(JsonString *p, const char *zIn, u32 N){
165982 u32 i;
165983 if( (N+p->nUsed+2 >= p->nAlloc) && jsonGrow(p,N+2)!=0 ) return;
165984 p->zBuf[p->nUsed++] = '"';
165985 for(i=0; i<N; i++){
165986 unsigned char c = ((unsigned const char*)zIn)[i];
165987 if( c=='"' || c=='\\' ){
165988 json_simple_escape:
165989 if( (p->nUsed+N+3-i > p->nAlloc) && jsonGrow(p,N+3-i)!=0 ) return;
165990 p->zBuf[p->nUsed++] = '\\';
165991 }else if( c<=0x1f ){
165992 static const char aSpecial[] = {
165993 0, 0, 0, 0, 0, 0, 0, 0, 'b', 't', 'n', 0, 'f', 'r', 0, 0,
165994 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
165995 };
165996 assert( sizeof(aSpecial)==32 );
165997 assert( aSpecial['\b']=='b' );
165998 assert( aSpecial['\f']=='f' );
165999 assert( aSpecial['\n']=='n' );
166000 assert( aSpecial['\r']=='r' );
166001 assert( aSpecial['\t']=='t' );
166002 if( aSpecial[c] ){
166003 c = aSpecial[c];
166004 goto json_simple_escape;
166005 }
166006 if( (p->nUsed+N+7+i > p->nAlloc) && jsonGrow(p,N+7-i)!=0 ) return;
166007 p->zBuf[p->nUsed++] = '\\';
166008 p->zBuf[p->nUsed++] = 'u';
166009 p->zBuf[p->nUsed++] = '0';
166010 p->zBuf[p->nUsed++] = '0';
166011 p->zBuf[p->nUsed++] = '0' + (c>>4);
166012 c = "0123456789abcdef"[c&0xf];
166013 }
166014 p->zBuf[p->nUsed++] = c;
166015 }
166016 p->zBuf[p->nUsed++] = '"';
166017 assert( p->nUsed<p->nAlloc );
@@ -165475,11 +166048,11 @@
166048 break;
166049 }
166050 default: {
166051 if( p->bErr==0 ){
166052 sqlite3_result_error(p->pCtx, "JSON cannot hold BLOB values", -1);
166053 p->bErr = 2;
166054 jsonReset(p);
166055 }
166056 break;
166057 }
166058 }
@@ -166684,10 +167257,11 @@
167257 sqlite3_context *ctx,
167258 int argc,
167259 sqlite3_value **argv
167260 ){
167261 JsonString *pStr;
167262 UNUSED_PARAM(argc);
167263 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
167264 if( pStr ){
167265 if( pStr->zBuf==0 ){
167266 jsonInit(pStr, ctx);
167267 jsonAppendChar(pStr, '[');
@@ -166703,11 +167277,11 @@
167277 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
167278 if( pStr ){
167279 pStr->pCtx = ctx;
167280 jsonAppendChar(pStr, ']');
167281 if( pStr->bErr ){
167282 if( pStr->bErr==1 ) sqlite3_result_error_nomem(ctx);
167283 assert( pStr->bStatic );
167284 }else{
167285 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
167286 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
167287 pStr->bStatic = 1;
@@ -166729,10 +167303,11 @@
167303 sqlite3_value **argv
167304 ){
167305 JsonString *pStr;
167306 const char *z;
167307 u32 n;
167308 UNUSED_PARAM(argc);
167309 pStr = (JsonString*)sqlite3_aggregate_context(ctx, sizeof(*pStr));
167310 if( pStr ){
167311 if( pStr->zBuf==0 ){
167312 jsonInit(pStr, ctx);
167313 jsonAppendChar(pStr, '{');
@@ -166751,11 +167326,11 @@
167326 JsonString *pStr;
167327 pStr = (JsonString*)sqlite3_aggregate_context(ctx, 0);
167328 if( pStr ){
167329 jsonAppendChar(pStr, '}');
167330 if( pStr->bErr ){
167331 if( pStr->bErr==0 ) sqlite3_result_error_nomem(ctx);
167332 assert( pStr->bStatic );
167333 }else{
167334 sqlite3_result_text(ctx, pStr->zBuf, pStr->nUsed,
167335 pStr->bStatic ? SQLITE_TRANSIENT : sqlite3_free);
167336 pStr->bStatic = 1;
@@ -167937,14 +168512,15 @@
168512 #ifndef SQLITE_AMALGAMATION
168513
168514 typedef unsigned char u8;
168515 typedef unsigned int u32;
168516 typedef unsigned short u16;
168517 typedef short i16;
168518 typedef sqlite3_int64 i64;
168519 typedef sqlite3_uint64 u64;
168520
168521 #define ArraySize(x) ((int)(sizeof(x) / sizeof(x[0])))
168522
168523 #define testcase(x)
168524 #define ALWAYS(x) 1
168525 #define NEVER(x) 0
168526
@@ -167990,10 +168566,20 @@
168566 SQLITE_API extern int sqlite3_fts5_may_be_corrupt;
168567 # define assert_nc(x) assert(sqlite3_fts5_may_be_corrupt || (x))
168568 #else
168569 # define assert_nc(x) assert(x)
168570 #endif
168571
168572 /* Mark a function parameter as unused, to suppress nuisance compiler
168573 ** warnings. */
168574 #ifndef UNUSED_PARAM
168575 # define UNUSED_PARAM(X) (void)(X)
168576 #endif
168577
168578 #ifndef UNUSED_PARAM2
168579 # define UNUSED_PARAM2(X, Y) (void)(X), (void)(Y)
168580 #endif
168581
168582 typedef struct Fts5Global Fts5Global;
168583 typedef struct Fts5Colset Fts5Colset;
168584
168585 /* If a NEAR() clump or phrase may only match a specific set of columns,
@@ -168136,12 +168722,12 @@
168722 ** Buffer object for the incremental building of string data.
168723 */
168724 typedef struct Fts5Buffer Fts5Buffer;
168725 struct Fts5Buffer {
168726 u8 *p;
168727 int n;
168728 int nSpace;
168729 };
168730
168731 static int sqlite3Fts5BufferSize(int*, Fts5Buffer*, u32);
168732 static void sqlite3Fts5BufferAppendVarint(int*, Fts5Buffer*, i64);
168733 static void sqlite3Fts5BufferAppendBlob(int*, Fts5Buffer*, u32, const u8*);
@@ -168158,11 +168744,11 @@
168744 #define fts5BufferFree(a) sqlite3Fts5BufferFree(a)
168745 #define fts5BufferAppendBlob(a,b,c,d) sqlite3Fts5BufferAppendBlob(a,b,c,d)
168746 #define fts5BufferSet(a,b,c,d) sqlite3Fts5BufferSet(a,b,c,d)
168747
168748 #define fts5BufferGrow(pRc,pBuf,nn) ( \
168749 (u32)((pBuf)->n) + (u32)(nn) <= (u32)((pBuf)->nSpace) ? 0 : \
168750 sqlite3Fts5BufferSize((pRc),(pBuf),(nn)+(pBuf)->n) \
168751 )
168752
168753 /* Write and decode big-endian 32-bit integer values */
168754 static void sqlite3Fts5Put32(u8*, int);
@@ -168193,10 +168779,11 @@
168779 typedef struct Fts5PoslistWriter Fts5PoslistWriter;
168780 struct Fts5PoslistWriter {
168781 i64 iPrev;
168782 };
168783 static int sqlite3Fts5PoslistWriterAppend(Fts5Buffer*, Fts5PoslistWriter*, i64);
168784 static void sqlite3Fts5PoslistSafeAppend(Fts5Buffer*, i64*, i64);
168785
168786 static int sqlite3Fts5PoslistNext64(
168787 const u8 *a, int n, /* Buffer containing poslist */
168788 int *pi, /* IN/OUT: Offset within a[] */
168789 i64 *piOff /* IN/OUT: Current offset */
@@ -168226,34 +168813,39 @@
168813 */
168814
168815 typedef struct Fts5Index Fts5Index;
168816 typedef struct Fts5IndexIter Fts5IndexIter;
168817
168818 struct Fts5IndexIter {
168819 i64 iRowid;
168820 const u8 *pData;
168821 int nData;
168822 u8 bEof;
168823 };
168824
168825 #define sqlite3Fts5IterEof(x) ((x)->bEof)
168826
168827 /*
168828 ** Values used as part of the flags argument passed to IndexQuery().
168829 */
168830 #define FTS5INDEX_QUERY_PREFIX 0x0001 /* Prefix query */
168831 #define FTS5INDEX_QUERY_DESC 0x0002 /* Docs in descending rowid order */
168832 #define FTS5INDEX_QUERY_TEST_NOIDX 0x0004 /* Do not use prefix index */
168833 #define FTS5INDEX_QUERY_SCAN 0x0008 /* Scan query (fts5vocab) */
168834
168835 /* The following are used internally by the fts5_index.c module. They are
168836 ** defined here only to make it easier to avoid clashes with the flags
168837 ** above. */
168838 #define FTS5INDEX_QUERY_SKIPEMPTY 0x0010
168839 #define FTS5INDEX_QUERY_NOOUTPUT 0x0020
168840
168841 /*
168842 ** Create/destroy an Fts5Index object.
168843 */
168844 static int sqlite3Fts5IndexOpen(Fts5Config *pConfig, int bCreate, Fts5Index**, char**);
168845 static int sqlite3Fts5IndexClose(Fts5Index *p);
168846
 
 
 
 
 
 
 
 
 
 
168847 /*
168848 ** Return a simple checksum value based on the arguments.
168849 */
168850 static u64 sqlite3Fts5IndexEntryCksum(
168851 i64 iRowid,
@@ -168289,16 +168881,12 @@
168881
168882 /*
168883 ** The various operations on open token or token prefix iterators opened
168884 ** using sqlite3Fts5IndexQuery().
168885 */
 
168886 static int sqlite3Fts5IterNext(Fts5IndexIter*);
168887 static int sqlite3Fts5IterNextFrom(Fts5IndexIter*, i64 iMatch);
 
 
 
168888
168889 /*
168890 ** Close an iterator opened by sqlite3Fts5IndexQuery().
168891 */
168892 static void sqlite3Fts5IterClose(Fts5IndexIter*);
@@ -168380,12 +168968,10 @@
168968 static int sqlite3Fts5IndexOptimize(Fts5Index *p);
168969 static int sqlite3Fts5IndexMerge(Fts5Index *p, int nMerge);
168970
168971 static int sqlite3Fts5IndexLoadConfig(Fts5Index *p);
168972
 
 
168973 /*
168974 ** End of interface to code in fts5_index.c.
168975 **************************************************************************/
168976
168977 /**************************************************************************
@@ -168581,11 +169167,11 @@
169167 Fts5Config*, Fts5Expr*, Fts5PoslistPopulator*, int, const char*, int
169168 );
169169 static void sqlite3Fts5ExprCheckPoslists(Fts5Expr*, i64);
169170 static void sqlite3Fts5ExprClearEof(Fts5Expr*);
169171
169172 static int sqlite3Fts5ExprClonePhrase(Fts5Expr*, int, Fts5Expr**);
169173
169174 static int sqlite3Fts5ExprPhraseCollist(Fts5Expr *, int, const u8 **, int *);
169175
169176 /*******************************************
169177 ** The fts5_expr.c API above this point is used by the other hand-written
@@ -169372,11 +169958,12 @@
169958 while( fts5yypParser->fts5yyidx>=0 ) fts5yy_pop_parser_stack(fts5yypParser);
169959 /* Here code is inserted which will execute if the parser
169960 ** stack every overflows */
169961 /******** Begin %stack_overflow code ******************************************/
169962
169963 UNUSED_PARAM(fts5yypMinor); /* Silence a compiler warning */
169964 sqlite3Fts5ParseError(pParse, "fts5: parser stack overflow");
169965 /******** End %stack_overflow code ********************************************/
169966 sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
169967 }
169968
169969 /*
@@ -169669,10 +170256,11 @@
170256 ){
170257 sqlite3Fts5ParserARG_FETCH;
170258 #define FTS5TOKEN (fts5yyminor.fts5yy0)
170259 /************ Begin %syntax_error code ****************************************/
170260
170261 UNUSED_PARAM(fts5yymajor); /* Silence a compiler warning */
170262 sqlite3Fts5ParseError(
170263 pParse, "fts5: syntax error near \"%.*s\"",FTS5TOKEN.n,FTS5TOKEN.p
170264 );
170265 /************ End %syntax_error code ******************************************/
170266 sqlite3Fts5ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
@@ -170045,10 +170633,12 @@
170633 int iEndOff /* End offset of token */
170634 ){
170635 HighlightContext *p = (HighlightContext*)pContext;
170636 int rc = SQLITE_OK;
170637 int iPos;
170638
170639 UNUSED_PARAM2(pToken, nToken);
170640
170641 if( tflags & FTS5_TOKEN_COLOCATED ) return SQLITE_OK;
170642 iPos = p->iPos++;
170643
170644 if( p->iRangeEnd>0 ){
@@ -170279,10 +170869,11 @@
170869 const Fts5ExtensionApi *pApi,
170870 Fts5Context *pFts,
170871 void *pUserData /* Pointer to sqlite3_int64 variable */
170872 ){
170873 sqlite3_int64 *pn = (sqlite3_int64*)pUserData;
170874 UNUSED_PARAM2(pApi, pFts);
170875 (*pn)++;
170876 return SQLITE_OK;
170877 }
170878
170879 /*
@@ -170432,11 +171023,11 @@
171023 { "bm25", 0, fts5Bm25Function, 0 },
171024 };
171025 int rc = SQLITE_OK; /* Return code */
171026 int i; /* To iterate through builtin functions */
171027
171028 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
171029 rc = pApi->xCreateFunction(pApi,
171030 aBuiltin[i].zFunc,
171031 aBuiltin[i].pUserData,
171032 aBuiltin[i].xFunc,
171033 aBuiltin[i].xDestroy
@@ -170464,22 +171055,24 @@
171055
171056
171057 /* #include "fts5Int.h" */
171058
171059 static int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, u32 nByte){
171060 if( (u32)pBuf->nSpace<nByte ){
171061 u32 nNew = pBuf->nSpace ? pBuf->nSpace : 64;
171062 u8 *pNew;
171063 while( nNew<nByte ){
171064 nNew = nNew * 2;
171065 }
171066 pNew = sqlite3_realloc(pBuf->p, nNew);
171067 if( pNew==0 ){
171068 *pRc = SQLITE_NOMEM;
171069 return 1;
171070 }else{
171071 pBuf->nSpace = nNew;
171072 pBuf->p = pNew;
171073 }
171074 }
171075 return 0;
171076 }
171077
171078
@@ -170655,28 +171248,41 @@
171248 pIter->a = a;
171249 pIter->n = n;
171250 sqlite3Fts5PoslistReaderNext(pIter);
171251 return pIter->bEof;
171252 }
171253
171254 /*
171255 ** Append position iPos to the position list being accumulated in buffer
171256 ** pBuf, which must be already be large enough to hold the new data.
171257 ** The previous position written to this list is *piPrev. *piPrev is set
171258 ** to iPos before returning.
171259 */
171260 static void sqlite3Fts5PoslistSafeAppend(
171261 Fts5Buffer *pBuf,
171262 i64 *piPrev,
171263 i64 iPos
171264 ){
171265 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32;
171266 if( (iPos & colmask) != (*piPrev & colmask) ){
171267 pBuf->p[pBuf->n++] = 1;
171268 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32));
171269 *piPrev = (iPos & colmask);
171270 }
171271 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-*piPrev)+2);
171272 *piPrev = iPos;
171273 }
171274
171275 static int sqlite3Fts5PoslistWriterAppend(
171276 Fts5Buffer *pBuf,
171277 Fts5PoslistWriter *pWriter,
171278 i64 iPos
171279 ){
171280 int rc = 0; /* Initialized only to suppress erroneous warning from Clang */
171281 if( fts5BufferGrow(&rc, pBuf, 5+5+5) ) return rc;
171282 sqlite3Fts5PoslistSafeAppend(pBuf, &pWriter->iPrev, iPos);
171283 return SQLITE_OK;
 
 
 
 
 
 
 
 
171284 }
171285
171286 static void *sqlite3Fts5MallocZero(int *pRc, int nByte){
171287 void *pRet = 0;
171288 if( *pRc==SQLITE_OK ){
@@ -170770,11 +171376,11 @@
171376 ){
171377 int rc = SQLITE_OK;
171378 *pbPresent = 0;
171379 if( p ){
171380 int i;
171381 u32 hash = 13;
171382 Fts5TermsetEntry *pEntry;
171383
171384 /* Calculate a hash value for this term. This is the same hash checksum
171385 ** used by the fts5_hash.c module. This is not important for correct
171386 ** operation of the module, but is necessary to ensure that some tests
@@ -170787,11 +171393,11 @@
171393
171394 for(pEntry=p->apHash[hash]; pEntry; pEntry=pEntry->pNext){
171395 if( pEntry->iIdx==iIdx
171396 && pEntry->nTerm==nTerm
171397 && memcmp(pEntry->pTerm, pTerm, nTerm)==0
171398 ){
171399 *pbPresent = 1;
171400 break;
171401 }
171402 }
171403
@@ -170811,11 +171417,11 @@
171417 return rc;
171418 }
171419
171420 static void sqlite3Fts5TermsetFree(Fts5Termset *p){
171421 if( p ){
171422 u32 i;
171423 for(i=0; i<ArraySize(p->apHash); i++){
171424 Fts5TermsetEntry *pEntry = p->apHash[i];
171425 while( pEntry ){
171426 Fts5TermsetEntry *pDel = pEntry;
171427 pEntry = pEntry->pNext;
@@ -170823,13 +171429,10 @@
171429 }
171430 }
171431 sqlite3_free(p);
171432 }
171433 }
 
 
 
171434
171435 /*
171436 ** 2014 Jun 09
171437 **
171438 ** The author disclaims copyright to this source code. In place of
@@ -171035,11 +171638,11 @@
171638 static int fts5ConfigSetEnum(
171639 const Fts5Enum *aEnum,
171640 const char *zEnum,
171641 int *peVal
171642 ){
171643 int nEnum = (int)strlen(zEnum);
171644 int i;
171645 int iVal = -1;
171646
171647 for(i=0; aEnum[i].zName; i++){
171648 if( sqlite3_strnicmp(aEnum[i].zName, zEnum, nEnum)==0 ){
@@ -171773,11 +172376,10 @@
172376 pConfig->iCookie = iCookie;
172377 }
172378 return rc;
172379 }
172380
 
172381 /*
172382 ** 2014 May 31
172383 **
172384 ** The author disclaims copyright to this source code. In place of
172385 ** a legal notice, here is a blessing:
@@ -171838,10 +172440,13 @@
172440 struct Fts5ExprNode {
172441 int eType; /* Node type */
172442 int bEof; /* True at EOF */
172443 int bNomatch; /* True if entry is not a match */
172444
172445 /* Next method for this node. */
172446 int (*xNext)(Fts5Expr*, Fts5ExprNode*, int, i64);
172447
172448 i64 iRowid; /* Current rowid */
172449 Fts5ExprNearset *pNear; /* For FTS5_STRING - cluster of phrases */
172450
172451 /* Child nodes. For a NOT node, this array always contains 2 entries. For
172452 ** AND or OR nodes, it contains 2 or more entries. */
@@ -171849,10 +172454,16 @@
172454 Fts5ExprNode *apChild[1]; /* Array of child nodes */
172455 };
172456
172457 #define Fts5NodeIsString(p) ((p)->eType==FTS5_TERM || (p)->eType==FTS5_STRING)
172458
172459 /*
172460 ** Invoke the xNext method of an Fts5ExprNode object. This macro should be
172461 ** used as if it has the same signature as the xNext() methods themselves.
172462 */
172463 #define fts5ExprNodeNext(a,b,c,d) (b)->xNext((a), (b), (c), (d))
172464
172465 /*
172466 ** An instance of the following structure represents a single search term
172467 ** or term prefix.
172468 */
172469 struct Fts5ExprTerm {
@@ -172010,11 +172621,19 @@
172621 *ppNew = pNew = sqlite3_malloc(sizeof(Fts5Expr));
172622 if( pNew==0 ){
172623 sParse.rc = SQLITE_NOMEM;
172624 sqlite3Fts5ParseNodeFree(sParse.pExpr);
172625 }else{
172626 if( !sParse.pExpr ){
172627 const int nByte = sizeof(Fts5ExprNode);
172628 pNew->pRoot = (Fts5ExprNode*)sqlite3Fts5MallocZero(&sParse.rc, nByte);
172629 if( pNew->pRoot ){
172630 pNew->pRoot->bEof = 1;
172631 }
172632 }else{
172633 pNew->pRoot = sParse.pExpr;
172634 }
172635 pNew->pIndex = 0;
172636 pNew->pConfig = pConfig;
172637 pNew->apExprPhrase = sParse.apPhrase;
172638 pNew->nPhrase = sParse.nPhrase;
172639 sParse.apPhrase = 0;
@@ -172062,11 +172681,11 @@
172681
172682 assert( pTerm->pSynonym );
172683 assert( bDesc==0 || bDesc==1 );
172684 for(p=pTerm; p; p=p->pSynonym){
172685 if( 0==sqlite3Fts5IterEof(p->pIter) ){
172686 i64 iRowid = p->pIter->iRowid;
172687 if( bRetValid==0 || (bDesc!=(iRowid<iRet)) ){
172688 iRet = iRowid;
172689 bRetValid = 1;
172690 }
172691 }
@@ -172079,14 +172698,12 @@
172698 /*
172699 ** Argument pTerm must be a synonym iterator.
172700 */
172701 static int fts5ExprSynonymList(
172702 Fts5ExprTerm *pTerm,
 
 
172703 i64 iRowid,
172704 Fts5Buffer *pBuf, /* Use this buffer for space if required */
172705 u8 **pa, int *pn
172706 ){
172707 Fts5PoslistReader aStatic[4];
172708 Fts5PoslistReader *aIter = aStatic;
172709 int nIter = 0;
@@ -172095,23 +172712,12 @@
172712 Fts5ExprTerm *p;
172713
172714 assert( pTerm->pSynonym );
172715 for(p=pTerm; p; p=p->pSynonym){
172716 Fts5IndexIter *pIter = p->pIter;
172717 if( sqlite3Fts5IterEof(pIter)==0 && pIter->iRowid==iRowid ){
172718 if( pIter->nData==0 ) continue;
 
 
 
 
 
 
 
 
 
 
 
172719 if( nIter==nAlloc ){
172720 int nByte = sizeof(Fts5PoslistReader) * nAlloc * 2;
172721 Fts5PoslistReader *aNew = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172722 if( aNew==0 ){
172723 rc = SQLITE_NOMEM;
@@ -172120,24 +172726,23 @@
172726 memcpy(aNew, aIter, sizeof(Fts5PoslistReader) * nIter);
172727 nAlloc = nAlloc*2;
172728 if( aIter!=aStatic ) sqlite3_free(aIter);
172729 aIter = aNew;
172730 }
172731 sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &aIter[nIter]);
172732 assert( aIter[nIter].bEof==0 );
172733 nIter++;
172734 }
172735 }
172736
 
172737 if( nIter==1 ){
172738 *pa = (u8*)aIter[0].a;
172739 *pn = aIter[0].n;
172740 }else{
172741 Fts5PoslistWriter writer = {0};
 
172742 i64 iPrev = -1;
172743 fts5BufferZero(pBuf);
172744 while( 1 ){
172745 int i;
172746 i64 iMin = FTS5_LARGEST_INT64;
172747 for(i=0; i<nIter; i++){
172748 if( aIter[i].bEof==0 ){
@@ -172148,19 +172753,16 @@
172753 iMin = aIter[i].iPos;
172754 }
172755 }
172756 }
172757 if( iMin==FTS5_LARGEST_INT64 || rc!=SQLITE_OK ) break;
172758 rc = sqlite3Fts5PoslistWriterAppend(pBuf, &writer, iMin);
172759 iPrev = iMin;
172760 }
172761 if( rc==SQLITE_OK ){
172762 *pa = pBuf->p;
172763 *pn = pBuf->n;
 
 
 
172764 }
172765 }
172766
172767 synonym_poslist_out:
172768 if( aIter!=aStatic ) sqlite3_free(aIter);
@@ -172179,11 +172781,10 @@
172781 ** otherwise. It is not considered an error code if the current rowid is
172782 ** not a match.
172783 */
172784 static int fts5ExprPhraseIsMatch(
172785 Fts5ExprNode *pNode, /* Node pPhrase belongs to */
 
172786 Fts5ExprPhrase *pPhrase, /* Phrase object to initialize */
172787 int *pbMatch /* OUT: Set to true if really a match */
172788 ){
172789 Fts5PoslistWriter writer = {0};
172790 Fts5PoslistReader aStatic[4];
@@ -172193,32 +172794,35 @@
172794
172795 fts5BufferZero(&pPhrase->poslist);
172796
172797 /* If the aStatic[] array is not large enough, allocate a large array
172798 ** using sqlite3_malloc(). This approach could be improved upon. */
172799 if( pPhrase->nTerm>ArraySize(aStatic) ){
172800 int nByte = sizeof(Fts5PoslistReader) * pPhrase->nTerm;
172801 aIter = (Fts5PoslistReader*)sqlite3_malloc(nByte);
172802 if( !aIter ) return SQLITE_NOMEM;
172803 }
172804 memset(aIter, 0, sizeof(Fts5PoslistReader) * pPhrase->nTerm);
172805
172806 /* Initialize a term iterator for each term in the phrase */
172807 for(i=0; i<pPhrase->nTerm; i++){
172808 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
 
172809 int n = 0;
172810 int bFlag = 0;
172811 u8 *a = 0;
172812 if( pTerm->pSynonym ){
172813 Fts5Buffer buf = {0, 0, 0};
172814 rc = fts5ExprSynonymList(pTerm, pNode->iRowid, &buf, &a, &n);
172815 if( rc ){
172816 sqlite3_free(a);
172817 goto ismatch_out;
172818 }
172819 if( a==buf.p ) bFlag = 1;
172820 }else{
172821 a = (u8*)pTerm->pIter->pData;
172822 n = pTerm->pIter->nData;
172823 }
 
172824 sqlite3Fts5PoslistReaderInit(a, n, &aIter[i]);
172825 aIter[i].bFlag = (u8)bFlag;
172826 if( aIter[i].bEof ) goto ismatch_out;
172827 }
172828
@@ -172286,16 +172890,10 @@
172890 p->n = n;
172891 fts5LookaheadReaderNext(p);
172892 return fts5LookaheadReaderNext(p);
172893 }
172894
 
 
 
 
 
 
172895 typedef struct Fts5NearTrimmer Fts5NearTrimmer;
172896 struct Fts5NearTrimmer {
172897 Fts5LookaheadReader reader; /* Input iterator */
172898 Fts5PoslistWriter writer; /* Writer context */
172899 Fts5Buffer *pOut; /* Output poslist */
@@ -172329,11 +172927,11 @@
172927
172928 assert( pNear->nPhrase>1 );
172929
172930 /* If the aStatic[] array is not large enough, allocate a large array
172931 ** using sqlite3_malloc(). This approach could be improved upon. */
172932 if( pNear->nPhrase>ArraySize(aStatic) ){
172933 int nByte = sizeof(Fts5NearTrimmer) * pNear->nPhrase;
172934 a = (Fts5NearTrimmer*)sqlite3Fts5MallocZero(&rc, nByte);
172935 }else{
172936 memset(aStatic, 0, sizeof(aStatic));
172937 }
@@ -172406,75 +173004,10 @@
173004 if( a!=aStatic ) sqlite3_free(a);
173005 return bRet;
173006 }
173007 }
173008
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173009 /*
173010 ** Advance iterator pIter until it points to a value equal to or laster
173011 ** than the initial value of *piLast. If this means the iterator points
173012 ** to a value laster than *piLast, update *piLast to the new lastest value.
173013 **
@@ -172490,19 +173023,19 @@
173023 int *pbEof /* OUT: Set to true if EOF */
173024 ){
173025 i64 iLast = *piLast;
173026 i64 iRowid;
173027
173028 iRowid = pIter->iRowid;
173029 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
173030 int rc = sqlite3Fts5IterNextFrom(pIter, iLast);
173031 if( rc || sqlite3Fts5IterEof(pIter) ){
173032 *pRc = rc;
173033 *pbEof = 1;
173034 return 1;
173035 }
173036 iRowid = pIter->iRowid;
173037 assert( (bDesc==0 && iRowid>=iLast) || (bDesc==1 && iRowid<=iLast) );
173038 }
173039 *piLast = iRowid;
173040
173041 return 0;
@@ -172519,11 +173052,11 @@
173052 Fts5ExprTerm *p;
173053 int bEof = 0;
173054
173055 for(p=pTerm; rc==SQLITE_OK && p; p=p->pSynonym){
173056 if( sqlite3Fts5IterEof(p->pIter)==0 ){
173057 i64 iRowid = p->pIter->iRowid;
173058 if( (bDesc==0 && iLast>iRowid) || (bDesc && iLast<iRowid) ){
173059 rc = sqlite3Fts5IterNextFrom(p->pIter, iLast);
173060 }
173061 }
173062 }
@@ -172551,17 +173084,11 @@
173084 Fts5ExprPhrase *pPhrase = pNear->apPhrase[0];
173085 pPhrase->poslist.n = 0;
173086 for(pTerm=&pPhrase->aTerm[0]; pTerm; pTerm=pTerm->pSynonym){
173087 Fts5IndexIter *pIter = pTerm->pIter;
173088 if( sqlite3Fts5IterEof(pIter)==0 ){
173089 if( pIter->iRowid==pNode->iRowid && pIter->nData>0 ){
 
 
 
 
 
 
173090 pPhrase->poslist.n = 1;
173091 }
173092 }
173093 }
173094 return pPhrase->poslist.n;
@@ -172573,16 +173100,15 @@
173100 ** phrase is not a match, break out of the loop early. */
173101 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
173102 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
173103 if( pPhrase->nTerm>1 || pPhrase->aTerm[0].pSynonym || pNear->pColset ){
173104 int bMatch = 0;
173105 rc = fts5ExprPhraseIsMatch(pNode, pPhrase, &bMatch);
173106 if( bMatch==0 ) break;
173107 }else{
173108 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
173109 fts5BufferSet(&rc, &pPhrase->poslist, pIter->nData, pIter->pData);
 
173110 }
173111 }
173112
173113 *pRc = rc;
173114 if( i==pNear->nPhrase && (i==1 || fts5ExprNearIsMatch(pRc, pNear)) ){
@@ -172590,107 +173116,10 @@
173116 }
173117 return 0;
173118 }
173119 }
173120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173121
173122 /*
173123 ** Initialize all term iterators in the pNear object. If any term is found
173124 ** to match no documents at all, return immediately without initializing any
173125 ** further iterators.
@@ -172701,10 +173130,11 @@
173130 ){
173131 Fts5ExprNearset *pNear = pNode->pNear;
173132 int i, j;
173133 int rc = SQLITE_OK;
173134
173135 assert( pNode->bNomatch==0 );
173136 for(i=0; rc==SQLITE_OK && i<pNear->nPhrase; i++){
173137 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
173138 for(j=0; j<pPhrase->nTerm; j++){
173139 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
173140 Fts5ExprTerm *p;
@@ -172735,14 +173165,10 @@
173165 }
173166 }
173167
173168 return rc;
173169 }
 
 
 
 
173170
173171 /*
173172 ** If pExpr is an ASC iterator, this function returns a value with the
173173 ** same sign as:
173174 **
@@ -172768,10 +173194,11 @@
173194 }
173195
173196 static void fts5ExprSetEof(Fts5ExprNode *pNode){
173197 int i;
173198 pNode->bEof = 1;
173199 pNode->bNomatch = 0;
173200 for(i=0; i<pNode->nChild; i++){
173201 fts5ExprSetEof(pNode->apChild[i]);
173202 }
173203 }
173204
@@ -172790,16 +173217,279 @@
173217 }
173218 }
173219 }
173220
173221
173222
173223 /*
173224 ** Compare the values currently indicated by the two nodes as follows:
173225 **
173226 ** res = (*p1) - (*p2)
173227 **
173228 ** Nodes that point to values that come later in the iteration order are
173229 ** considered to be larger. Nodes at EOF are the largest of all.
173230 **
173231 ** This means that if the iteration order is ASC, then numerically larger
173232 ** rowids are considered larger. Or if it is the default DESC, numerically
173233 ** smaller rowids are larger.
173234 */
173235 static int fts5NodeCompare(
173236 Fts5Expr *pExpr,
173237 Fts5ExprNode *p1,
173238 Fts5ExprNode *p2
173239 ){
173240 if( p2->bEof ) return -1;
173241 if( p1->bEof ) return +1;
173242 return fts5RowidCmp(pExpr, p1->iRowid, p2->iRowid);
173243 }
173244
173245 /*
173246 ** All individual term iterators in pNear are guaranteed to be valid when
173247 ** this function is called. This function checks if all term iterators
173248 ** point to the same rowid, and if not, advances them until they do.
173249 ** If an EOF is reached before this happens, *pbEof is set to true before
173250 ** returning.
173251 **
173252 ** SQLITE_OK is returned if an error occurs, or an SQLite error code
173253 ** otherwise. It is not considered an error code if an iterator reaches
173254 ** EOF.
173255 */
173256 static int fts5ExprNodeTest_STRING(
173257 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173258 Fts5ExprNode *pNode
173259 ){
173260 Fts5ExprNearset *pNear = pNode->pNear;
173261 Fts5ExprPhrase *pLeft = pNear->apPhrase[0];
173262 int rc = SQLITE_OK;
173263 i64 iLast; /* Lastest rowid any iterator points to */
173264 int i, j; /* Phrase and token index, respectively */
173265 int bMatch; /* True if all terms are at the same rowid */
173266 const int bDesc = pExpr->bDesc;
173267
173268 /* Check that this node should not be FTS5_TERM */
173269 assert( pNear->nPhrase>1
173270 || pNear->apPhrase[0]->nTerm>1
173271 || pNear->apPhrase[0]->aTerm[0].pSynonym
173272 );
173273
173274 /* Initialize iLast, the "lastest" rowid any iterator points to. If the
173275 ** iterator skips through rowids in the default ascending order, this means
173276 ** the maximum rowid. Or, if the iterator is "ORDER BY rowid DESC", then it
173277 ** means the minimum rowid. */
173278 if( pLeft->aTerm[0].pSynonym ){
173279 iLast = fts5ExprSynonymRowid(&pLeft->aTerm[0], bDesc, 0);
173280 }else{
173281 iLast = pLeft->aTerm[0].pIter->iRowid;
173282 }
173283
173284 do {
173285 bMatch = 1;
173286 for(i=0; i<pNear->nPhrase; i++){
173287 Fts5ExprPhrase *pPhrase = pNear->apPhrase[i];
173288 for(j=0; j<pPhrase->nTerm; j++){
173289 Fts5ExprTerm *pTerm = &pPhrase->aTerm[j];
173290 if( pTerm->pSynonym ){
173291 i64 iRowid = fts5ExprSynonymRowid(pTerm, bDesc, 0);
173292 if( iRowid==iLast ) continue;
173293 bMatch = 0;
173294 if( fts5ExprSynonymAdvanceto(pTerm, bDesc, &iLast, &rc) ){
173295 pNode->bNomatch = 0;
173296 pNode->bEof = 1;
173297 return rc;
173298 }
173299 }else{
173300 Fts5IndexIter *pIter = pPhrase->aTerm[j].pIter;
173301 if( pIter->iRowid==iLast ) continue;
173302 bMatch = 0;
173303 if( fts5ExprAdvanceto(pIter, bDesc, &iLast, &rc, &pNode->bEof) ){
173304 return rc;
173305 }
173306 }
173307 }
173308 }
173309 }while( bMatch==0 );
173310
173311 pNode->iRowid = iLast;
173312 pNode->bNomatch = ((0==fts5ExprNearTest(&rc, pExpr, pNode)) && rc==SQLITE_OK);
173313 assert( pNode->bEof==0 || pNode->bNomatch==0 );
173314
173315 return rc;
173316 }
173317
173318 /*
173319 ** Advance the first term iterator in the first phrase of pNear. Set output
173320 ** variable *pbEof to true if it reaches EOF or if an error occurs.
173321 **
173322 ** Return SQLITE_OK if successful, or an SQLite error code if an error
173323 ** occurs.
173324 */
173325 static int fts5ExprNodeNext_STRING(
173326 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173327 Fts5ExprNode *pNode, /* FTS5_STRING or FTS5_TERM node */
173328 int bFromValid,
173329 i64 iFrom
173330 ){
173331 Fts5ExprTerm *pTerm = &pNode->pNear->apPhrase[0]->aTerm[0];
173332 int rc = SQLITE_OK;
173333
173334 pNode->bNomatch = 0;
173335 if( pTerm->pSynonym ){
173336 int bEof = 1;
173337 Fts5ExprTerm *p;
173338
173339 /* Find the firstest rowid any synonym points to. */
173340 i64 iRowid = fts5ExprSynonymRowid(pTerm, pExpr->bDesc, 0);
173341
173342 /* Advance each iterator that currently points to iRowid. Or, if iFrom
173343 ** is valid - each iterator that points to a rowid before iFrom. */
173344 for(p=pTerm; p; p=p->pSynonym){
173345 if( sqlite3Fts5IterEof(p->pIter)==0 ){
173346 i64 ii = p->pIter->iRowid;
173347 if( ii==iRowid
173348 || (bFromValid && ii!=iFrom && (ii>iFrom)==pExpr->bDesc)
173349 ){
173350 if( bFromValid ){
173351 rc = sqlite3Fts5IterNextFrom(p->pIter, iFrom);
173352 }else{
173353 rc = sqlite3Fts5IterNext(p->pIter);
173354 }
173355 if( rc!=SQLITE_OK ) break;
173356 if( sqlite3Fts5IterEof(p->pIter)==0 ){
173357 bEof = 0;
173358 }
173359 }else{
173360 bEof = 0;
173361 }
173362 }
173363 }
173364
173365 /* Set the EOF flag if either all synonym iterators are at EOF or an
173366 ** error has occurred. */
173367 pNode->bEof = (rc || bEof);
173368 }else{
173369 Fts5IndexIter *pIter = pTerm->pIter;
173370
173371 assert( Fts5NodeIsString(pNode) );
173372 if( bFromValid ){
173373 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
173374 }else{
173375 rc = sqlite3Fts5IterNext(pIter);
173376 }
173377
173378 pNode->bEof = (rc || sqlite3Fts5IterEof(pIter));
173379 }
173380
173381 if( pNode->bEof==0 ){
173382 assert( rc==SQLITE_OK );
173383 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
173384 }
173385
173386 return rc;
173387 }
173388
173389
173390 static int fts5ExprNodeTest_TERM(
173391 Fts5Expr *pExpr, /* Expression that pNear is a part of */
173392 Fts5ExprNode *pNode /* The "NEAR" node (FTS5_TERM) */
173393 ){
173394 /* As this "NEAR" object is actually a single phrase that consists
173395 ** of a single term only, grab pointers into the poslist managed by the
173396 ** fts5_index.c iterator object. This is much faster than synthesizing
173397 ** a new poslist the way we have to for more complicated phrase or NEAR
173398 ** expressions. */
173399 Fts5ExprPhrase *pPhrase = pNode->pNear->apPhrase[0];
173400 Fts5IndexIter *pIter = pPhrase->aTerm[0].pIter;
173401
173402 assert( pNode->eType==FTS5_TERM );
173403 assert( pNode->pNear->nPhrase==1 && pPhrase->nTerm==1 );
173404 assert( pPhrase->aTerm[0].pSynonym==0 );
173405
173406 pPhrase->poslist.n = pIter->nData;
173407 if( pExpr->pConfig->eDetail==FTS5_DETAIL_FULL ){
173408 pPhrase->poslist.p = (u8*)pIter->pData;
173409 }
173410 pNode->iRowid = pIter->iRowid;
173411 pNode->bNomatch = (pPhrase->poslist.n==0);
173412 return SQLITE_OK;
173413 }
173414
173415 /*
173416 ** xNext() method for a node of type FTS5_TERM.
173417 */
173418 static int fts5ExprNodeNext_TERM(
173419 Fts5Expr *pExpr,
173420 Fts5ExprNode *pNode,
173421 int bFromValid,
173422 i64 iFrom
173423 ){
173424 int rc;
173425 Fts5IndexIter *pIter = pNode->pNear->apPhrase[0]->aTerm[0].pIter;
173426
173427 assert( pNode->bEof==0 );
173428 if( bFromValid ){
173429 rc = sqlite3Fts5IterNextFrom(pIter, iFrom);
173430 }else{
173431 rc = sqlite3Fts5IterNext(pIter);
173432 }
173433 if( rc==SQLITE_OK && sqlite3Fts5IterEof(pIter)==0 ){
173434 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
173435 }else{
173436 pNode->bEof = 1;
173437 pNode->bNomatch = 0;
173438 }
173439 return rc;
173440 }
173441
173442 static void fts5ExprNodeTest_OR(
173443 Fts5Expr *pExpr, /* Expression of which pNode is a part */
173444 Fts5ExprNode *pNode /* Expression node to test */
173445 ){
173446 Fts5ExprNode *pNext = pNode->apChild[0];
173447 int i;
173448
173449 for(i=1; i<pNode->nChild; i++){
173450 Fts5ExprNode *pChild = pNode->apChild[i];
173451 int cmp = fts5NodeCompare(pExpr, pNext, pChild);
173452 if( cmp>0 || (cmp==0 && pChild->bNomatch==0) ){
173453 pNext = pChild;
173454 }
173455 }
173456 pNode->iRowid = pNext->iRowid;
173457 pNode->bEof = pNext->bEof;
173458 pNode->bNomatch = pNext->bNomatch;
173459 }
173460
173461 static int fts5ExprNodeNext_OR(
173462 Fts5Expr *pExpr,
173463 Fts5ExprNode *pNode,
173464 int bFromValid,
173465 i64 iFrom
173466 ){
173467 int i;
173468 i64 iLast = pNode->iRowid;
173469
173470 for(i=0; i<pNode->nChild; i++){
173471 Fts5ExprNode *p1 = pNode->apChild[i];
173472 assert( p1->bEof || fts5RowidCmp(pExpr, p1->iRowid, iLast)>=0 );
173473 if( p1->bEof==0 ){
173474 if( (p1->iRowid==iLast)
173475 || (bFromValid && fts5RowidCmp(pExpr, p1->iRowid, iFrom)<0)
173476 ){
173477 int rc = fts5ExprNodeNext(pExpr, p1, bFromValid, iFrom);
173478 if( rc!=SQLITE_OK ) return rc;
173479 }
173480 }
173481 }
173482
173483 fts5ExprNodeTest_OR(pExpr, pNode);
173484 return SQLITE_OK;
173485 }
173486
173487 /*
173488 ** Argument pNode is an FTS5_AND node.
173489 */
173490 static int fts5ExprNodeTest_AND(
173491 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173492 Fts5ExprNode *pAnd /* FTS5_AND node to advance */
173493 ){
173494 int iChild;
173495 i64 iLast = pAnd->iRowid;
@@ -172810,19 +173500,15 @@
173500 do {
173501 pAnd->bNomatch = 0;
173502 bMatch = 1;
173503 for(iChild=0; iChild<pAnd->nChild; iChild++){
173504 Fts5ExprNode *pChild = pAnd->apChild[iChild];
173505 int cmp = fts5RowidCmp(pExpr, iLast, pChild->iRowid);
173506 if( cmp>0 ){
173507 /* Advance pChild until it points to iLast or laster */
173508 rc = fts5ExprNodeNext(pExpr, pChild, 1, iLast);
173509 if( rc!=SQLITE_OK ) return rc;
 
 
 
 
173510 }
173511
173512 /* If the child node is now at EOF, so is the parent AND node. Otherwise,
173513 ** the child node is guaranteed to have advanced at least as far as
173514 ** rowid iLast. So if it is not at exactly iLast, pChild->iRowid is the
@@ -172848,190 +173534,99 @@
173534 }
173535 pAnd->iRowid = iLast;
173536 return SQLITE_OK;
173537 }
173538
173539 static int fts5ExprNodeNext_AND(
173540 Fts5Expr *pExpr,
173541 Fts5ExprNode *pNode,
173542 int bFromValid,
173543 i64 iFrom
173544 ){
173545 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
173546 if( rc==SQLITE_OK ){
173547 rc = fts5ExprNodeTest_AND(pExpr, pNode);
173548 }
173549 return rc;
173550 }
173551
173552 static int fts5ExprNodeTest_NOT(
173553 Fts5Expr *pExpr, /* Expression pPhrase belongs to */
173554 Fts5ExprNode *pNode /* FTS5_NOT node to advance */
173555 ){
173556 int rc = SQLITE_OK;
173557 Fts5ExprNode *p1 = pNode->apChild[0];
173558 Fts5ExprNode *p2 = pNode->apChild[1];
173559 assert( pNode->nChild==2 );
173560
173561 while( rc==SQLITE_OK && p1->bEof==0 ){
173562 int cmp = fts5NodeCompare(pExpr, p1, p2);
173563 if( cmp>0 ){
173564 rc = fts5ExprNodeNext(pExpr, p2, 1, p1->iRowid);
173565 cmp = fts5NodeCompare(pExpr, p1, p2);
173566 }
173567 assert( rc!=SQLITE_OK || cmp<=0 );
173568 if( cmp || p2->bNomatch ) break;
173569 rc = fts5ExprNodeNext(pExpr, p1, 0, 0);
173570 }
173571 pNode->bEof = p1->bEof;
173572 pNode->bNomatch = p1->bNomatch;
173573 pNode->iRowid = p1->iRowid;
173574 if( p1->bEof ){
173575 fts5ExprNodeZeroPoslist(p2);
173576 }
173577 return rc;
173578 }
173579
173580 static int fts5ExprNodeNext_NOT(
173581 Fts5Expr *pExpr,
173582 Fts5ExprNode *pNode,
173583 int bFromValid,
173584 i64 iFrom
173585 ){
173586 int rc = fts5ExprNodeNext(pExpr, pNode->apChild[0], bFromValid, iFrom);
173587 if( rc==SQLITE_OK ){
173588 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
173589 }
173590 return rc;
173591 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173592
173593 /*
173594 ** If pNode currently points to a match, this function returns SQLITE_OK
173595 ** without modifying it. Otherwise, pNode is advanced until it does point
173596 ** to a match or EOF is reached.
173597 */
173598 static int fts5ExprNodeTest(
173599 Fts5Expr *pExpr, /* Expression of which pNode is a part */
173600 Fts5ExprNode *pNode /* Expression node to test */
173601 ){
173602 int rc = SQLITE_OK;
173603 if( pNode->bEof==0 ){
173604 switch( pNode->eType ){
173605
173606 case FTS5_STRING: {
173607 rc = fts5ExprNodeTest_STRING(pExpr, pNode);
 
173608 break;
173609 }
173610
173611 case FTS5_TERM: {
173612 rc = fts5ExprNodeTest_TERM(pExpr, pNode);
173613 break;
173614 }
173615
173616 case FTS5_AND: {
173617 rc = fts5ExprNodeTest_AND(pExpr, pNode);
173618 break;
173619 }
173620
173621 case FTS5_OR: {
173622 fts5ExprNodeTest_OR(pExpr, pNode);
 
 
 
 
 
 
 
 
 
 
 
 
173623 break;
173624 }
173625
173626 default: assert( pNode->eType==FTS5_NOT ); {
173627 rc = fts5ExprNodeTest_NOT(pExpr, pNode);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173628 break;
173629 }
173630 }
173631 }
173632 return rc;
@@ -173046,24 +173641,44 @@
173641 ** It is not an error if there are no matches.
173642 */
173643 static int fts5ExprNodeFirst(Fts5Expr *pExpr, Fts5ExprNode *pNode){
173644 int rc = SQLITE_OK;
173645 pNode->bEof = 0;
173646 pNode->bNomatch = 0;
173647
173648 if( Fts5NodeIsString(pNode) ){
173649 /* Initialize all term iterators in the NEAR object. */
173650 rc = fts5ExprNearInitAll(pExpr, pNode);
173651 }else{
173652 int i;
173653 int nEof = 0;
173654 for(i=0; i<pNode->nChild && rc==SQLITE_OK; i++){
173655 Fts5ExprNode *pChild = pNode->apChild[i];
173656 rc = fts5ExprNodeFirst(pExpr, pNode->apChild[i]);
173657 assert( pChild->bEof==0 || pChild->bEof==1 );
173658 nEof += pChild->bEof;
173659 }
173660 pNode->iRowid = pNode->apChild[0]->iRowid;
173661
173662 switch( pNode->eType ){
173663 case FTS5_AND:
173664 if( nEof>0 ) fts5ExprSetEof(pNode);
173665 break;
173666
173667 case FTS5_OR:
173668 if( pNode->nChild==nEof ) fts5ExprSetEof(pNode);
173669 break;
173670
173671 default:
173672 assert( pNode->eType==FTS5_NOT );
173673 pNode->bEof = pNode->apChild[0]->bEof;
173674 break;
173675 }
173676 }
173677
173678 if( rc==SQLITE_OK ){
173679 rc = fts5ExprNodeTest(pExpr, pNode);
173680 }
173681 return rc;
173682 }
173683
173684
@@ -173083,11 +173698,11 @@
173698 ** is not considered an error if the query does not match any documents.
173699 */
173700 static int sqlite3Fts5ExprFirst(Fts5Expr *p, Fts5Index *pIdx, i64 iFirst, int bDesc){
173701 Fts5ExprNode *pRoot = p->pRoot;
173702 int rc = SQLITE_OK;
173703 if( pRoot->xNext ){
173704 p->pIndex = pIdx;
173705 p->bDesc = bDesc;
173706 rc = fts5ExprNodeFirst(p, pRoot);
173707
173708 /* If not at EOF but the current rowid occurs earlier than iFirst in
@@ -173095,11 +173710,12 @@
173710 if( pRoot->bEof==0 && fts5RowidCmp(p, pRoot->iRowid, iFirst)<0 ){
173711 rc = fts5ExprNodeNext(p, pRoot, 1, iFirst);
173712 }
173713
173714 /* If the iterator is not at a real match, skip forward until it is. */
173715 while( pRoot->bNomatch ){
173716 assert( pRoot->bEof==0 && rc==SQLITE_OK );
173717 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173718 }
173719 }
173720 return rc;
173721 }
@@ -173111,21 +173727,23 @@
173727 ** is not considered an error if the query does not match any documents.
173728 */
173729 static int sqlite3Fts5ExprNext(Fts5Expr *p, i64 iLast){
173730 int rc;
173731 Fts5ExprNode *pRoot = p->pRoot;
173732 assert( pRoot->bEof==0 && pRoot->bNomatch==0 );
173733 do {
173734 rc = fts5ExprNodeNext(p, pRoot, 0, 0);
173735 assert( pRoot->bNomatch==0 || (rc==SQLITE_OK && pRoot->bEof==0) );
173736 }while( pRoot->bNomatch );
173737 if( fts5RowidCmp(p, pRoot->iRowid, iLast)>0 ){
173738 pRoot->bEof = 1;
173739 }
173740 return rc;
173741 }
173742
173743 static int sqlite3Fts5ExprEof(Fts5Expr *p){
173744 return p->pRoot->bEof;
173745 }
173746
173747 static i64 sqlite3Fts5ExprRowid(Fts5Expr *p){
173748 return p->pRoot->iRowid;
173749 }
@@ -173146,14 +173764,14 @@
173764 Fts5ExprTerm *pSyn;
173765 Fts5ExprTerm *pNext;
173766 Fts5ExprTerm *pTerm = &pPhrase->aTerm[i];
173767 sqlite3_free(pTerm->zTerm);
173768 sqlite3Fts5IterClose(pTerm->pIter);
 
173769 for(pSyn=pTerm->pSynonym; pSyn; pSyn=pNext){
173770 pNext = pSyn->pSynonym;
173771 sqlite3Fts5IterClose(pSyn->pIter);
173772 fts5BufferFree((Fts5Buffer*)&pSyn[1]);
173773 sqlite3_free(pSyn);
173774 }
173775 }
173776 if( pPhrase->poslist.nSpace>0 ) fts5BufferFree(&pPhrase->poslist);
173777 sqlite3_free(pPhrase);
@@ -173230,24 +173848,26 @@
173848 ){
173849 int rc = SQLITE_OK;
173850 const int SZALLOC = 8;
173851 TokenCtx *pCtx = (TokenCtx*)pContext;
173852 Fts5ExprPhrase *pPhrase = pCtx->pPhrase;
173853
173854 UNUSED_PARAM2(iUnused1, iUnused2);
173855
173856 /* If an error has already occurred, this is a no-op */
173857 if( pCtx->rc!=SQLITE_OK ) return pCtx->rc;
173858
173859 assert( pPhrase==0 || pPhrase->nTerm>0 );
173860 if( pPhrase && (tflags & FTS5_TOKEN_COLOCATED) ){
173861 Fts5ExprTerm *pSyn;
173862 int nByte = sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer) + nToken+1;
173863 pSyn = (Fts5ExprTerm*)sqlite3_malloc(nByte);
173864 if( pSyn==0 ){
173865 rc = SQLITE_NOMEM;
173866 }else{
173867 memset(pSyn, 0, nByte);
173868 pSyn->zTerm = ((char*)pSyn) + sizeof(Fts5ExprTerm) + sizeof(Fts5Buffer);
173869 memcpy(pSyn->zTerm, pToken, nToken);
173870 pSyn->pSynonym = pPhrase->aTerm[pPhrase->nTerm-1].pSynonym;
173871 pPhrase->aTerm[pPhrase->nTerm-1].pSynonym = pSyn;
173872 }
173873 }else{
@@ -173366,26 +173986,21 @@
173986 /*
173987 ** Create a new FTS5 expression by cloning phrase iPhrase of the
173988 ** expression passed as the second argument.
173989 */
173990 static int sqlite3Fts5ExprClonePhrase(
 
173991 Fts5Expr *pExpr,
173992 int iPhrase,
173993 Fts5Expr **ppNew
173994 ){
173995 int rc = SQLITE_OK; /* Return code */
173996 Fts5ExprPhrase *pOrig; /* The phrase extracted from pExpr */
173997 int i; /* Used to iterate through phrase terms */
 
173998 Fts5Expr *pNew = 0; /* Expression to return via *ppNew */
 
173999 TokenCtx sCtx = {0,0}; /* Context object for fts5ParseTokenize */
174000
 
174001 pOrig = pExpr->apExprPhrase[iPhrase];
 
174002 pNew = (Fts5Expr*)sqlite3Fts5MallocZero(&rc, sizeof(Fts5Expr));
174003 if( rc==SQLITE_OK ){
174004 pNew->apExprPhrase = (Fts5ExprPhrase**)sqlite3Fts5MallocZero(&rc,
174005 sizeof(Fts5ExprPhrase*));
174006 }
@@ -173422,12 +174037,14 @@
174037 pNew->pRoot->pNear->nPhrase = 1;
174038 sCtx.pPhrase->pNode = pNew->pRoot;
174039
174040 if( pOrig->nTerm==1 && pOrig->aTerm[0].pSynonym==0 ){
174041 pNew->pRoot->eType = FTS5_TERM;
174042 pNew->pRoot->xNext = fts5ExprNodeNext_TERM;
174043 }else{
174044 pNew->pRoot->eType = FTS5_STRING;
174045 pNew->pRoot->xNext = fts5ExprNodeNext_STRING;
174046 }
174047 }else{
174048 sqlite3Fts5ExprFree(pNew);
174049 fts5ExprPhraseFree(sCtx.pPhrase);
174050 pNew = 0;
@@ -173570,10 +174187,42 @@
174187 pNear->pColset = pColset;
174188 }else{
174189 sqlite3_free(pColset);
174190 }
174191 }
174192
174193 static void fts5ExprAssignXNext(Fts5ExprNode *pNode){
174194 switch( pNode->eType ){
174195 case FTS5_STRING: {
174196 Fts5ExprNearset *pNear = pNode->pNear;
174197 if( pNear->nPhrase==1 && pNear->apPhrase[0]->nTerm==1
174198 && pNear->apPhrase[0]->aTerm[0].pSynonym==0
174199 ){
174200 pNode->eType = FTS5_TERM;
174201 pNode->xNext = fts5ExprNodeNext_TERM;
174202 }else{
174203 pNode->xNext = fts5ExprNodeNext_STRING;
174204 }
174205 break;
174206 };
174207
174208 case FTS5_OR: {
174209 pNode->xNext = fts5ExprNodeNext_OR;
174210 break;
174211 };
174212
174213 case FTS5_AND: {
174214 pNode->xNext = fts5ExprNodeNext_AND;
174215 break;
174216 };
174217
174218 default: assert( pNode->eType==FTS5_NOT ); {
174219 pNode->xNext = fts5ExprNodeNext_NOT;
174220 break;
174221 };
174222 }
174223 }
174224
174225 static void fts5ExprAddChildren(Fts5ExprNode *p, Fts5ExprNode *pSub){
174226 if( p->eType!=FTS5_NOT && pSub->eType==p->eType ){
174227 int nByte = sizeof(Fts5ExprNode*) * pSub->nChild;
174228 memcpy(&p->apChild[p->nChild], pSub->apChild, nByte);
@@ -173620,20 +174269,20 @@
174269 pRet = (Fts5ExprNode*)sqlite3Fts5MallocZero(&pParse->rc, nByte);
174270
174271 if( pRet ){
174272 pRet->eType = eType;
174273 pRet->pNear = pNear;
174274 fts5ExprAssignXNext(pRet);
174275 if( eType==FTS5_STRING ){
174276 int iPhrase;
174277 for(iPhrase=0; iPhrase<pNear->nPhrase; iPhrase++){
174278 pNear->apPhrase[iPhrase]->pNode = pRet;
174279 }
174280
174281 if( pParse->pConfig->eDetail!=FTS5_DETAIL_FULL
174282 && (pNear->nPhrase!=1 || pNear->apPhrase[0]->nTerm!=1)
174283 ){
 
174284 assert( pParse->rc==SQLITE_OK );
174285 pParse->rc = SQLITE_ERROR;
174286 assert( pParse->zErr==0 );
174287 pParse->zErr = sqlite3_mprintf(
174288 "fts5: %s queries are not supported (detail!=full)",
@@ -173640,10 +174289,11 @@
174289 pNear->nPhrase==1 ? "phrase": "NEAR"
174290 );
174291 sqlite3_free(pRet);
174292 pRet = 0;
174293 }
174294
174295 }else{
174296 fts5ExprAddChildren(pRet, pLeft);
174297 fts5ExprAddChildren(pRet, pRight);
174298 }
174299 }
@@ -173922,11 +174572,11 @@
174572 if( rc==SQLITE_OK ){
174573 rc = sqlite3Fts5ExprNew(pConfig, zExpr, &pExpr, &zErr);
174574 }
174575 if( rc==SQLITE_OK ){
174576 char *zText;
174577 if( pExpr->pRoot->xNext==0 ){
174578 zText = sqlite3_mprintf("");
174579 }else if( bTcl ){
174580 zText = fts5ExprPrintTcl(pConfig, zNearsetCmd, pExpr->pRoot);
174581 }else{
174582 zText = fts5ExprPrint(pConfig, pExpr->pRoot);
@@ -174022,11 +174672,11 @@
174672 };
174673 int i;
174674 int rc = SQLITE_OK;
174675 void *pCtx = (void*)pGlobal;
174676
174677 for(i=0; rc==SQLITE_OK && i<ArraySize(aFunc); i++){
174678 struct Fts5ExprFunc *p = &aFunc[i];
174679 rc = sqlite3_create_function(db, p->z, -1, SQLITE_UTF8, pCtx, p->x, 0, 0);
174680 }
174681
174682 /* Avoid a warning indicating that sqlite3Fts5ParserTrace() is unused */
@@ -174119,16 +174769,18 @@
174769 static int fts5ExprPopulatePoslistsCb(
174770 void *pCtx, /* Copy of 2nd argument to xTokenize() */
174771 int tflags, /* Mask of FTS5_TOKEN_* flags */
174772 const char *pToken, /* Pointer to buffer containing token */
174773 int nToken, /* Size of token in bytes */
174774 int iUnused1, /* Byte offset of token within input text */
174775 int iUnused2 /* Byte offset of end of token within input text */
174776 ){
174777 Fts5ExprCtx *p = (Fts5ExprCtx*)pCtx;
174778 Fts5Expr *pExpr = p->pExpr;
174779 int i;
174780
174781 UNUSED_PARAM2(iUnused1, iUnused2);
174782
174783 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ) p->iOff++;
174784 for(i=0; i<pExpr->nPhrase; i++){
174785 Fts5ExprTerm *pTerm;
174786 if( p->aPopulator[i].bOk==0 ) continue;
@@ -174260,30 +174912,25 @@
174912 Fts5ExprPhrase *pPhrase = pExpr->apExprPhrase[iPhrase];
174913 Fts5ExprNode *pNode = pPhrase->pNode;
174914 int rc = SQLITE_OK;
174915
174916 assert( iPhrase>=0 && iPhrase<pExpr->nPhrase );
174917 assert( pExpr->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
174918
174919 if( pNode->bEof==0
174920 && pNode->iRowid==pExpr->pRoot->iRowid
174921 && pPhrase->poslist.n>0
174922 ){
174923 Fts5ExprTerm *pTerm = &pPhrase->aTerm[0];
174924 if( pTerm->pSynonym ){
174925 Fts5Buffer *pBuf = (Fts5Buffer*)&pTerm->pSynonym[1];
 
174926 rc = fts5ExprSynonymList(
174927 pTerm, pNode->iRowid, pBuf, (u8**)ppCollist, pnCollist
174928 );
174929 }else{
174930 *ppCollist = pPhrase->aTerm[0].pIter->pData;
174931 *pnCollist = pPhrase->aTerm[0].pIter->nData;
 
 
 
 
 
 
174932 }
174933 }else{
174934 *ppCollist = 0;
174935 *pnCollist = 0;
174936 }
@@ -174354,14 +175001,14 @@
175001 Fts5HashEntry *pScanNext; /* Next entry in sorted order */
175002
175003 int nAlloc; /* Total size of allocation */
175004 int iSzPoslist; /* Offset of space for 4-byte poslist size */
175005 int nData; /* Total bytes of data (incl. structure) */
175006 int nKey; /* Length of zKey[] in bytes */
175007 u8 bDel; /* Set delete-flag @ iSzPoslist */
175008 u8 bContent; /* Set content-flag (detail=none mode) */
175009 i16 iCol; /* Column of last value written */
 
175010 int iPos; /* Position of last value written */
175011 i64 iRowid; /* Rowid of last value written */
175012 char zKey[8]; /* Nul-terminated entry key */
175013 };
175014
@@ -174537,12 +175184,12 @@
175184
175185 /* Attempt to locate an existing hash entry */
175186 iHash = fts5HashKey2(pHash->nSlot, (u8)bByte, (const u8*)pToken, nToken);
175187 for(p=pHash->aSlot[iHash]; p; p=p->pHashNext){
175188 if( p->zKey[0]==bByte
175189 && p->nKey==nToken
175190 && memcmp(&p->zKey[1], pToken, nToken)==0
 
175191 ){
175192 break;
175193 }
175194 }
175195
@@ -174565,10 +175212,11 @@
175212 memset(p, 0, FTS5_HASHENTRYSIZE);
175213 p->nAlloc = nByte;
175214 p->zKey[0] = bByte;
175215 memcpy(&p->zKey[1], pToken, nToken);
175216 assert( iHash==fts5HashKey(pHash->nSlot, (u8*)p->zKey, nToken+1) );
175217 p->nKey = nToken;
175218 p->zKey[nToken+1] = '\0';
175219 p->nData = nToken+1 + 1 + FTS5_HASHENTRYSIZE;
175220 p->pHashNext = pHash->aSlot[iHash];
175221 pHash->aSlot[iHash] = p;
175222 pHash->nEntry++;
@@ -175080,10 +175728,11 @@
175728
175729 typedef struct Fts5Data Fts5Data;
175730 typedef struct Fts5DlidxIter Fts5DlidxIter;
175731 typedef struct Fts5DlidxLvl Fts5DlidxLvl;
175732 typedef struct Fts5DlidxWriter Fts5DlidxWriter;
175733 typedef struct Fts5Iter Fts5Iter;
175734 typedef struct Fts5PageWriter Fts5PageWriter;
175735 typedef struct Fts5SegIter Fts5SegIter;
175736 typedef struct Fts5DoclistIter Fts5DoclistIter;
175737 typedef struct Fts5SegWriter Fts5SegWriter;
175738 typedef struct Fts5Structure Fts5Structure;
@@ -175322,20 +175971,24 @@
175971 **
175972 ** poslist:
175973 ** Used by sqlite3Fts5IterPoslist() when the poslist needs to be buffered.
175974 ** There is no way to tell if this is populated or not.
175975 */
175976 struct Fts5Iter {
175977 Fts5IndexIter base; /* Base class containing output vars */
175978
175979 Fts5Index *pIndex; /* Index that owns this iterator */
175980 Fts5Structure *pStruct; /* Database structure for this iterator */
175981 Fts5Buffer poslist; /* Buffer containing current poslist */
175982 Fts5Colset *pColset; /* Restrict matches to these columns */
175983
175984 /* Invoked to set output variables. */
175985 void (*xSetOutputs)(Fts5Iter*, Fts5SegIter*);
175986
175987 int nSeg; /* Size of aSeg[] array */
175988 int bRev; /* True to iterate in reverse order */
175989 u8 bSkipEmpty; /* True to skip deleted entries */
 
 
175990
175991 i64 iSwitchRowid; /* Firstest rowid of other than aFirst[1] */
175992 Fts5CResult *aFirst; /* Current merge state (see above) */
175993 Fts5SegIter aSeg[1]; /* Array of segment iterators */
175994 };
@@ -175421,21 +176074,10 @@
176074 int nCmp = MIN(pLeft->n, pRight->n);
176075 int res = memcmp(pLeft->p, pRight->p, nCmp);
176076 return (res==0 ? (pLeft->n - pRight->n) : res);
176077 }
176078
 
 
 
 
 
 
 
 
 
 
 
176079 static int fts5LeafFirstTermOff(Fts5Data *pLeaf){
176080 int ret;
176081 fts5GetVarint32(&pLeaf->p[pLeaf->szLeaf], ret);
176082 return ret;
176083 }
@@ -175693,28 +176335,37 @@
176335 for(iLvl=0; rc==SQLITE_OK && iLvl<nLevel; iLvl++){
176336 Fts5StructureLevel *pLvl = &pRet->aLevel[iLvl];
176337 int nTotal;
176338 int iSeg;
176339
176340 if( i>=nData ){
176341 rc = FTS5_CORRUPT;
176342 }else{
176343 i += fts5GetVarint32(&pData[i], pLvl->nMerge);
176344 i += fts5GetVarint32(&pData[i], nTotal);
176345 assert( nTotal>=pLvl->nMerge );
176346 pLvl->aSeg = (Fts5StructureSegment*)sqlite3Fts5MallocZero(&rc,
176347 nTotal * sizeof(Fts5StructureSegment)
176348 );
176349 }
176350
176351 if( rc==SQLITE_OK ){
176352 pLvl->nSeg = nTotal;
176353 for(iSeg=0; iSeg<nTotal; iSeg++){
176354 if( i>=nData ){
176355 rc = FTS5_CORRUPT;
176356 break;
176357 }
176358 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].iSegid);
176359 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoFirst);
176360 i += fts5GetVarint32(&pData[i], pLvl->aSeg[iSeg].pgnoLast);
176361 }
 
 
 
176362 }
176363 }
176364 if( rc!=SQLITE_OK ){
176365 fts5StructureRelease(pRet);
176366 pRet = 0;
176367 }
176368 }
176369
176370 *ppOut = pRet;
176371 return rc;
@@ -176378,10 +177029,14 @@
177029 u8 *a = pIter->pLeaf->p; /* Buffer to read data from */
177030 int iOff = pIter->iLeafOffset; /* Offset to read at */
177031 int nNew; /* Bytes of new data */
177032
177033 iOff += fts5GetVarint32(&a[iOff], nNew);
177034 if( iOff+nNew>pIter->pLeaf->nn ){
177035 p->rc = FTS5_CORRUPT;
177036 return;
177037 }
177038 pIter->term.n = nKeep;
177039 fts5BufferAppendBlob(&p->rc, &pIter->term, nNew, &a[iOff]);
177040 iOff += nNew;
177041 pIter->iTermLeafOffset = iOff;
177042 pIter->iTermLeafPgno = pIter->iLeafPgno;
@@ -176571,11 +177226,11 @@
177226 /*
177227 ** Return true if the iterator passed as the second argument currently
177228 ** points to a delete marker. A delete marker is an entry with a 0 byte
177229 ** position-list.
177230 */
177231 static int fts5MultiIterIsEmpty(Fts5Index *p, Fts5Iter *pIter){
177232 Fts5SegIter *pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
177233 return (p->rc==SQLITE_OK && pSeg->pLeaf && pSeg->nPos==0);
177234 }
177235
177236 /*
@@ -176584,14 +177239,16 @@
177239 ** This version of fts5SegIterNext() is only used by reverse iterators.
177240 */
177241 static void fts5SegIterNext_Reverse(
177242 Fts5Index *p, /* FTS5 backend object */
177243 Fts5SegIter *pIter, /* Iterator to advance */
177244 int *pbUnused /* Unused */
177245 ){
177246 assert( pIter->flags & FTS5_SEGITER_REVERSE );
177247 assert( pIter->pNextLeaf==0 );
177248 UNUSED_PARAM(pbUnused);
177249
177250 if( pIter->iRowidOffset>0 ){
177251 u8 *a = pIter->pLeaf->p;
177252 int iOff;
177253 i64 iDelta;
177254
@@ -176843,13 +177500,10 @@
177500 iPoslist = pIter->iTermLeafOffset;
177501 }else{
177502 iPoslist = 4;
177503 }
177504 fts5IndexSkipVarint(pLeaf->p, iPoslist);
 
 
 
177505 pIter->iLeafOffset = iPoslist;
177506
177507 /* If this condition is true then the largest rowid for the current
177508 ** term may not be stored on the current page. So search forward to
177509 ** see where said rowid really is. */
@@ -177067,23 +177721,19 @@
177721 ** If an error occurs, Fts5Index.rc is set to an appropriate error code. If
177722 ** an error has already occurred when this function is called, it is a no-op.
177723 */
177724 static void fts5SegIterSeekInit(
177725 Fts5Index *p, /* FTS5 backend */
 
177726 const u8 *pTerm, int nTerm, /* Term to seek to */
177727 int flags, /* Mask of FTS5INDEX_XXX flags */
177728 Fts5StructureSegment *pSeg, /* Description of segment */
177729 Fts5SegIter *pIter /* Object to populate */
177730 ){
177731 int iPg = 1;
177732 int bGe = (flags & FTS5INDEX_QUERY_SCAN);
177733 int bDlidx = 0; /* True if there is a doclist-index */
177734
 
 
 
177735 assert( bGe==0 || (flags & FTS5INDEX_QUERY_DESC)==0 );
177736 assert( pTerm && nTerm );
177737 memset(pIter, 0, sizeof(*pIter));
177738 pIter->pSeg = pSeg;
177739
@@ -177225,11 +177875,11 @@
177875 ** fts5AssertMultiIterSetup(). It ensures that the result currently stored
177876 ** in *pRes is the correct result of comparing the current positions of the
177877 ** two iterators.
177878 */
177879 static void fts5AssertComparisonResult(
177880 Fts5Iter *pIter,
177881 Fts5SegIter *p1,
177882 Fts5SegIter *p2,
177883 Fts5CResult *pRes
177884 ){
177885 int i1 = p1 - pIter->aSeg;
@@ -177266,16 +177916,16 @@
177916 ** This function is a no-op unless SQLITE_DEBUG is defined when this module
177917 ** is compiled. In that case, this function is essentially an assert()
177918 ** statement used to verify that the contents of the pIter->aFirst[] array
177919 ** are correct.
177920 */
177921 static void fts5AssertMultiIterSetup(Fts5Index *p, Fts5Iter *pIter){
177922 if( p->rc==SQLITE_OK ){
177923 Fts5SegIter *pFirst = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
177924 int i;
177925
177926 assert( (pFirst->pLeaf==0)==pIter->base.bEof );
177927
177928 /* Check that pIter->iSwitchRowid is set correctly. */
177929 for(i=0; i<pIter->nSeg; i++){
177930 Fts5SegIter *p1 = &pIter->aSeg[i];
177931 assert( p1==pFirst
@@ -177311,11 +177961,11 @@
177961 ** If the returned value is non-zero, then it is the index of an entry
177962 ** in the pIter->aSeg[] array that is (a) not at EOF, and (b) pointing
177963 ** to a key that is a duplicate of another, higher priority,
177964 ** segment-iterator in the pSeg->aSeg[] array.
177965 */
177966 static int fts5MultiIterDoCompare(Fts5Iter *pIter, int iOut){
177967 int i1; /* Index of left-hand Fts5SegIter */
177968 int i2; /* Index of right-hand Fts5SegIter */
177969 int iRes;
177970 Fts5SegIter *p1; /* Left-hand Fts5SegIter */
177971 Fts5SegIter *p2; /* Right-hand Fts5SegIter */
@@ -177457,11 +178107,11 @@
178107
178108
178109 /*
178110 ** Free the iterator object passed as the second argument.
178111 */
178112 static void fts5MultiIterFree(Fts5Iter *pIter){
178113 if( pIter ){
178114 int i;
178115 for(i=0; i<pIter->nSeg; i++){
178116 fts5SegIterClear(&pIter->aSeg[i]);
178117 }
@@ -177471,11 +178121,11 @@
178121 }
178122 }
178123
178124 static void fts5MultiIterAdvanced(
178125 Fts5Index *p, /* FTS5 backend to iterate within */
178126 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
178127 int iChanged, /* Index of sub-iterator just advanced */
178128 int iMinset /* Minimum entry in aFirst[] to set */
178129 ){
178130 int i;
178131 for(i=(pIter->nSeg+iChanged)/2; i>=iMinset && p->rc==SQLITE_OK; i=i/2){
@@ -177498,13 +178148,13 @@
178148 ** If non-zero is returned, the caller should call fts5MultiIterAdvanced()
178149 ** on the iterator instead. That function does the same as this one, except
178150 ** that it deals with more complicated cases as well.
178151 */
178152 static int fts5MultiIterAdvanceRowid(
178153 Fts5Iter *pIter, /* Iterator to update aFirst[] array for */
178154 int iChanged, /* Index of sub-iterator just advanced */
178155 Fts5SegIter **ppFirst
178156 ){
178157 Fts5SegIter *pNew = &pIter->aSeg[iChanged];
178158
178159 if( pNew->iRowid==pIter->iSwitchRowid
178160 || (pNew->iRowid<pIter->iSwitchRowid)==pIter->bRev
@@ -177533,19 +178183,20 @@
178183
178184 pOther = &pIter->aSeg[ pIter->aFirst[i ^ 0x0001].iFirst ];
178185 }
178186 }
178187
178188 *ppFirst = pNew;
178189 return 0;
178190 }
178191
178192 /*
178193 ** Set the pIter->bEof variable based on the state of the sub-iterators.
178194 */
178195 static void fts5MultiIterSetEof(Fts5Iter *pIter){
178196 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
178197 pIter->base.bEof = pSeg->pLeaf==0;
178198 pIter->iSwitchRowid = pSeg->iRowid;
178199 }
178200
178201 /*
178202 ** Move the iterator to the next entry.
@@ -177554,43 +178205,48 @@
178205 ** considered an error if the iterator reaches EOF, or if it is already at
178206 ** EOF when this function is called.
178207 */
178208 static void fts5MultiIterNext(
178209 Fts5Index *p,
178210 Fts5Iter *pIter,
178211 int bFrom, /* True if argument iFrom is valid */
178212 i64 iFrom /* Advance at least as far as this */
178213 ){
178214 int bUseFrom = bFrom;
178215 while( p->rc==SQLITE_OK ){
178216 int iFirst = pIter->aFirst[1].iFirst;
178217 int bNewTerm = 0;
178218 Fts5SegIter *pSeg = &pIter->aSeg[iFirst];
178219 assert( p->rc==SQLITE_OK );
178220 if( bUseFrom && pSeg->pDlidx ){
178221 fts5SegIterNextFrom(p, pSeg, iFrom);
178222 }else{
178223 pSeg->xNext(p, pSeg, &bNewTerm);
178224 }
178225
178226 if( pSeg->pLeaf==0 || bNewTerm
178227 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
178228 ){
178229 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
178230 fts5MultiIterSetEof(pIter);
178231 pSeg = &pIter->aSeg[pIter->aFirst[1].iFirst];
178232 if( pSeg->pLeaf==0 ) return;
178233 }
178234
178235 fts5AssertMultiIterSetup(p, pIter);
178236 assert( pSeg==&pIter->aSeg[pIter->aFirst[1].iFirst] && pSeg->pLeaf );
178237 if( pIter->bSkipEmpty==0 || pSeg->nPos ){
178238 pIter->xSetOutputs(pIter, pSeg);
178239 return;
178240 }
178241 bUseFrom = 0;
178242 }
178243 }
178244
178245 static void fts5MultiIterNext2(
178246 Fts5Index *p,
178247 Fts5Iter *pIter,
178248 int *pbNewTerm /* OUT: True if *might* be new term */
178249 ){
178250 assert( pIter->bSkipEmpty );
178251 if( p->rc==SQLITE_OK ){
178252 do {
@@ -177599,11 +178255,11 @@
178255 int bNewTerm = 0;
178256
178257 assert( p->rc==SQLITE_OK );
178258 pSeg->xNext(p, pSeg, &bNewTerm);
178259 if( pSeg->pLeaf==0 || bNewTerm
178260 || fts5MultiIterAdvanceRowid(pIter, iFirst, &pSeg)
178261 ){
178262 fts5MultiIterAdvanced(p, pIter, iFirst, 1);
178263 fts5MultiIterSetEof(pIter);
178264 *pbNewTerm = 1;
178265 }else{
@@ -177613,220 +178269,147 @@
178269
178270 }while( fts5MultiIterIsEmpty(p, pIter) );
178271 }
178272 }
178273
178274 static void fts5IterSetOutputs_Noop(Fts5Iter *pUnused1, Fts5SegIter *pUnused2){
178275 UNUSED_PARAM2(pUnused1, pUnused2);
178276 }
178277
178278 static Fts5Iter *fts5MultiIterAlloc(
178279 Fts5Index *p, /* FTS5 backend to iterate within */
178280 int nSeg
178281 ){
178282 Fts5Iter *pNew;
178283 int nSlot; /* Power of two >= nSeg */
178284
178285 for(nSlot=2; nSlot<nSeg; nSlot=nSlot*2);
178286 pNew = fts5IdxMalloc(p,
178287 sizeof(Fts5Iter) + /* pNew */
178288 sizeof(Fts5SegIter) * (nSlot-1) + /* pNew->aSeg[] */
178289 sizeof(Fts5CResult) * nSlot /* pNew->aFirst[] */
178290 );
178291 if( pNew ){
178292 pNew->nSeg = nSlot;
178293 pNew->aFirst = (Fts5CResult*)&pNew->aSeg[nSlot];
178294 pNew->pIndex = p;
178295 pNew->xSetOutputs = fts5IterSetOutputs_Noop;
178296 }
178297 return pNew;
178298 }
178299
178300 static void fts5PoslistCallback(
178301 Fts5Index *pUnused,
178302 void *pContext,
178303 const u8 *pChunk, int nChunk
178304 ){
178305 UNUSED_PARAM(pUnused);
178306 assert_nc( nChunk>=0 );
178307 if( nChunk>0 ){
178308 fts5BufferSafeAppendBlob((Fts5Buffer*)pContext, pChunk, nChunk);
178309 }
178310 }
178311
178312 typedef struct PoslistCallbackCtx PoslistCallbackCtx;
178313 struct PoslistCallbackCtx {
178314 Fts5Buffer *pBuf; /* Append to this buffer */
178315 Fts5Colset *pColset; /* Restrict matches to this column */
178316 int eState; /* See above */
178317 };
178318
178319 typedef struct PoslistOffsetsCtx PoslistOffsetsCtx;
178320 struct PoslistOffsetsCtx {
178321 Fts5Buffer *pBuf; /* Append to this buffer */
178322 Fts5Colset *pColset; /* Restrict matches to this column */
178323 int iRead;
178324 int iWrite;
178325 };
178326
178327 /*
178328 ** TODO: Make this more efficient!
178329 */
178330 static int fts5IndexColsetTest(Fts5Colset *pColset, int iCol){
178331 int i;
178332 for(i=0; i<pColset->nCol; i++){
178333 if( pColset->aiCol[i]==iCol ) return 1;
178334 }
178335 return 0;
178336 }
178337
178338 static void fts5PoslistOffsetsCallback(
178339 Fts5Index *pUnused,
178340 void *pContext,
178341 const u8 *pChunk, int nChunk
178342 ){
178343 PoslistOffsetsCtx *pCtx = (PoslistOffsetsCtx*)pContext;
178344 UNUSED_PARAM(pUnused);
178345 assert_nc( nChunk>=0 );
178346 if( nChunk>0 ){
178347 int i = 0;
178348 while( i<nChunk ){
178349 int iVal;
178350 i += fts5GetVarint32(&pChunk[i], iVal);
178351 iVal += pCtx->iRead - 2;
178352 pCtx->iRead = iVal;
178353 if( fts5IndexColsetTest(pCtx->pColset, iVal) ){
178354 fts5BufferSafeAppendVarint(pCtx->pBuf, iVal + 2 - pCtx->iWrite);
178355 pCtx->iWrite = iVal;
178356 }
178357 }
178358 }
178359 }
178360
178361 static void fts5PoslistFilterCallback(
178362 Fts5Index *pUnused,
178363 void *pContext,
178364 const u8 *pChunk, int nChunk
178365 ){
178366 PoslistCallbackCtx *pCtx = (PoslistCallbackCtx*)pContext;
178367 UNUSED_PARAM(pUnused);
178368 assert_nc( nChunk>=0 );
178369 if( nChunk>0 ){
178370 /* Search through to find the first varint with value 1. This is the
178371 ** start of the next columns hits. */
178372 int i = 0;
178373 int iStart = 0;
178374
178375 if( pCtx->eState==2 ){
178376 int iCol;
178377 fts5FastGetVarint32(pChunk, i, iCol);
178378 if( fts5IndexColsetTest(pCtx->pColset, iCol) ){
178379 pCtx->eState = 1;
178380 fts5BufferSafeAppendVarint(pCtx->pBuf, 1);
178381 }else{
178382 pCtx->eState = 0;
178383 }
178384 }
178385
178386 do {
178387 while( i<nChunk && pChunk[i]!=0x01 ){
178388 while( pChunk[i] & 0x80 ) i++;
178389 i++;
178390 }
178391 if( pCtx->eState ){
178392 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
178393 }
178394 if( i<nChunk ){
178395 int iCol;
178396 iStart = i;
178397 i++;
178398 if( i>=nChunk ){
178399 pCtx->eState = 2;
178400 }else{
178401 fts5FastGetVarint32(pChunk, i, iCol);
178402 pCtx->eState = fts5IndexColsetTest(pCtx->pColset, iCol);
178403 if( pCtx->eState ){
178404 fts5BufferSafeAppendBlob(pCtx->pBuf, &pChunk[iStart], i-iStart);
178405 iStart = i;
178406 }
178407 }
178408 }
178409 }while( i<nChunk );
178410 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178411 }
178412
178413 static void fts5ChunkIterate(
178414 Fts5Index *p, /* Index object */
178415 Fts5SegIter *pSeg, /* Poslist of this iterator */
@@ -177866,11 +178449,459 @@
178449 }
178450 }
178451 }
178452 }
178453
178454 /*
178455 ** Iterator pIter currently points to a valid entry (not EOF). This
178456 ** function appends the position list data for the current entry to
178457 ** buffer pBuf. It does not make a copy of the position-list size
178458 ** field.
178459 */
178460 static void fts5SegiterPoslist(
178461 Fts5Index *p,
178462 Fts5SegIter *pSeg,
178463 Fts5Colset *pColset,
178464 Fts5Buffer *pBuf
178465 ){
178466 if( 0==fts5BufferGrow(&p->rc, pBuf, pSeg->nPos) ){
178467 if( pColset==0 ){
178468 fts5ChunkIterate(p, pSeg, (void*)pBuf, fts5PoslistCallback);
178469 }else{
178470 if( p->pConfig->eDetail==FTS5_DETAIL_FULL ){
178471 PoslistCallbackCtx sCtx;
178472 sCtx.pBuf = pBuf;
178473 sCtx.pColset = pColset;
178474 sCtx.eState = fts5IndexColsetTest(pColset, 0);
178475 assert( sCtx.eState==0 || sCtx.eState==1 );
178476 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistFilterCallback);
178477 }else{
178478 PoslistOffsetsCtx sCtx;
178479 memset(&sCtx, 0, sizeof(sCtx));
178480 sCtx.pBuf = pBuf;
178481 sCtx.pColset = pColset;
178482 fts5ChunkIterate(p, pSeg, (void*)&sCtx, fts5PoslistOffsetsCallback);
178483 }
178484 }
178485 }
178486 }
178487
178488 /*
178489 ** IN/OUT parameter (*pa) points to a position list n bytes in size. If
178490 ** the position list contains entries for column iCol, then (*pa) is set
178491 ** to point to the sub-position-list for that column and the number of
178492 ** bytes in it returned. Or, if the argument position list does not
178493 ** contain any entries for column iCol, return 0.
178494 */
178495 static int fts5IndexExtractCol(
178496 const u8 **pa, /* IN/OUT: Pointer to poslist */
178497 int n, /* IN: Size of poslist in bytes */
178498 int iCol /* Column to extract from poslist */
178499 ){
178500 int iCurrent = 0; /* Anything before the first 0x01 is col 0 */
178501 const u8 *p = *pa;
178502 const u8 *pEnd = &p[n]; /* One byte past end of position list */
178503
178504 while( iCol>iCurrent ){
178505 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
178506 ** not part of a varint. Note that it is not possible for a negative
178507 ** or extremely large varint to occur within an uncorrupted position
178508 ** list. So the last byte of each varint may be assumed to have a clear
178509 ** 0x80 bit. */
178510 while( *p!=0x01 ){
178511 while( *p++ & 0x80 );
178512 if( p>=pEnd ) return 0;
178513 }
178514 *pa = p++;
178515 iCurrent = *p++;
178516 if( iCurrent & 0x80 ){
178517 p--;
178518 p += fts5GetVarint32(p, iCurrent);
178519 }
178520 }
178521 if( iCol!=iCurrent ) return 0;
178522
178523 /* Advance pointer p until it points to pEnd or an 0x01 byte that is
178524 ** not part of a varint */
178525 while( p<pEnd && *p!=0x01 ){
178526 while( *p++ & 0x80 );
178527 }
178528
178529 return p - (*pa);
178530 }
178531
178532 static int fts5IndexExtractColset (
178533 Fts5Colset *pColset, /* Colset to filter on */
178534 const u8 *pPos, int nPos, /* Position list */
178535 Fts5Buffer *pBuf /* Output buffer */
178536 ){
178537 int rc = SQLITE_OK;
178538 int i;
178539
178540 fts5BufferZero(pBuf);
178541 for(i=0; i<pColset->nCol; i++){
178542 const u8 *pSub = pPos;
178543 int nSub = fts5IndexExtractCol(&pSub, nPos, pColset->aiCol[i]);
178544 if( nSub ){
178545 fts5BufferAppendBlob(&rc, pBuf, nSub, pSub);
178546 }
178547 }
178548 return rc;
178549 }
178550
178551 /*
178552 ** xSetOutputs callback used by detail=none tables.
178553 */
178554 static void fts5IterSetOutputs_None(Fts5Iter *pIter, Fts5SegIter *pSeg){
178555 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_NONE );
178556 pIter->base.iRowid = pSeg->iRowid;
178557 pIter->base.nData = pSeg->nPos;
178558 }
178559
178560 /*
178561 ** xSetOutputs callback used by detail=full and detail=col tables when no
178562 ** column filters are specified.
178563 */
178564 static void fts5IterSetOutputs_Nocolset(Fts5Iter *pIter, Fts5SegIter *pSeg){
178565 pIter->base.iRowid = pSeg->iRowid;
178566 pIter->base.nData = pSeg->nPos;
178567
178568 assert( pIter->pIndex->pConfig->eDetail!=FTS5_DETAIL_NONE );
178569 assert( pIter->pColset==0 );
178570
178571 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
178572 /* All data is stored on the current page. Populate the output
178573 ** variables to point into the body of the page object. */
178574 pIter->base.pData = &pSeg->pLeaf->p[pSeg->iLeafOffset];
178575 }else{
178576 /* The data is distributed over two or more pages. Copy it into the
178577 ** Fts5Iter.poslist buffer and then set the output pointer to point
178578 ** to this buffer. */
178579 fts5BufferZero(&pIter->poslist);
178580 fts5SegiterPoslist(pIter->pIndex, pSeg, 0, &pIter->poslist);
178581 pIter->base.pData = pIter->poslist.p;
178582 }
178583 }
178584
178585 /*
178586 ** xSetOutputs callback used by detail=col when there is a column filter
178587 ** and there are 100 or more columns. Also called as a fallback from
178588 ** fts5IterSetOutputs_Col100 if the column-list spans more than one page.
178589 */
178590 static void fts5IterSetOutputs_Col(Fts5Iter *pIter, Fts5SegIter *pSeg){
178591 fts5BufferZero(&pIter->poslist);
178592 fts5SegiterPoslist(pIter->pIndex, pSeg, pIter->pColset, &pIter->poslist);
178593 pIter->base.iRowid = pSeg->iRowid;
178594 pIter->base.pData = pIter->poslist.p;
178595 pIter->base.nData = pIter->poslist.n;
178596 }
178597
178598 /*
178599 ** xSetOutputs callback used when:
178600 **
178601 ** * detail=col,
178602 ** * there is a column filter, and
178603 ** * the table contains 100 or fewer columns.
178604 **
178605 ** The last point is to ensure all column numbers are stored as
178606 ** single-byte varints.
178607 */
178608 static void fts5IterSetOutputs_Col100(Fts5Iter *pIter, Fts5SegIter *pSeg){
178609
178610 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_COLUMNS );
178611 assert( pIter->pColset );
178612
178613 if( pSeg->iLeafOffset+pSeg->nPos>pSeg->pLeaf->szLeaf ){
178614 fts5IterSetOutputs_Col(pIter, pSeg);
178615 }else{
178616 u8 *a = (u8*)&pSeg->pLeaf->p[pSeg->iLeafOffset];
178617 u8 *pEnd = (u8*)&a[pSeg->nPos];
178618 int iPrev = 0;
178619 int *aiCol = pIter->pColset->aiCol;
178620 int *aiColEnd = &aiCol[pIter->pColset->nCol];
178621
178622 u8 *aOut = pIter->poslist.p;
178623 int iPrevOut = 0;
178624
178625 pIter->base.iRowid = pSeg->iRowid;
178626
178627 while( a<pEnd ){
178628 iPrev += (int)a++[0] - 2;
178629 while( *aiCol<iPrev ){
178630 aiCol++;
178631 if( aiCol==aiColEnd ) goto setoutputs_col_out;
178632 }
178633 if( *aiCol==iPrev ){
178634 *aOut++ = (iPrev - iPrevOut) + 2;
178635 iPrevOut = iPrev;
178636 }
178637 }
178638
178639 setoutputs_col_out:
178640 pIter->base.pData = pIter->poslist.p;
178641 pIter->base.nData = aOut - pIter->poslist.p;
178642 }
178643 }
178644
178645 /*
178646 ** xSetOutputs callback used by detail=full when there is a column filter.
178647 */
178648 static void fts5IterSetOutputs_Full(Fts5Iter *pIter, Fts5SegIter *pSeg){
178649 Fts5Colset *pColset = pIter->pColset;
178650 pIter->base.iRowid = pSeg->iRowid;
178651
178652 assert( pIter->pIndex->pConfig->eDetail==FTS5_DETAIL_FULL );
178653 assert( pColset );
178654
178655 if( pSeg->iLeafOffset+pSeg->nPos<=pSeg->pLeaf->szLeaf ){
178656 /* All data is stored on the current page. Populate the output
178657 ** variables to point into the body of the page object. */
178658 const u8 *a = &pSeg->pLeaf->p[pSeg->iLeafOffset];
178659 if( pColset->nCol==1 ){
178660 pIter->base.nData = fts5IndexExtractCol(&a, pSeg->nPos,pColset->aiCol[0]);
178661 pIter->base.pData = a;
178662 }else{
178663 fts5BufferZero(&pIter->poslist);
178664 fts5IndexExtractColset(pColset, a, pSeg->nPos, &pIter->poslist);
178665 pIter->base.pData = pIter->poslist.p;
178666 pIter->base.nData = pIter->poslist.n;
178667 }
178668 }else{
178669 /* The data is distributed over two or more pages. Copy it into the
178670 ** Fts5Iter.poslist buffer and then set the output pointer to point
178671 ** to this buffer. */
178672 fts5BufferZero(&pIter->poslist);
178673 fts5SegiterPoslist(pIter->pIndex, pSeg, pColset, &pIter->poslist);
178674 pIter->base.pData = pIter->poslist.p;
178675 pIter->base.nData = pIter->poslist.n;
178676 }
178677 }
178678
178679 static void fts5IterSetOutputCb(int *pRc, Fts5Iter *pIter){
178680 if( *pRc==SQLITE_OK ){
178681 Fts5Config *pConfig = pIter->pIndex->pConfig;
178682 if( pConfig->eDetail==FTS5_DETAIL_NONE ){
178683 pIter->xSetOutputs = fts5IterSetOutputs_None;
178684 }
178685
178686 else if( pIter->pColset==0 ){
178687 pIter->xSetOutputs = fts5IterSetOutputs_Nocolset;
178688 }
178689
178690 else if( pConfig->eDetail==FTS5_DETAIL_FULL ){
178691 pIter->xSetOutputs = fts5IterSetOutputs_Full;
178692 }
178693
178694 else{
178695 assert( pConfig->eDetail==FTS5_DETAIL_COLUMNS );
178696 if( pConfig->nCol<=100 ){
178697 pIter->xSetOutputs = fts5IterSetOutputs_Col100;
178698 sqlite3Fts5BufferSize(pRc, &pIter->poslist, pConfig->nCol);
178699 }else{
178700 pIter->xSetOutputs = fts5IterSetOutputs_Col;
178701 }
178702 }
178703 }
178704 }
178705
178706
178707 /*
178708 ** Allocate a new Fts5Iter object.
178709 **
178710 ** The new object will be used to iterate through data in structure pStruct.
178711 ** If iLevel is -ve, then all data in all segments is merged. Or, if iLevel
178712 ** is zero or greater, data from the first nSegment segments on level iLevel
178713 ** is merged.
178714 **
178715 ** The iterator initially points to the first term/rowid entry in the
178716 ** iterated data.
178717 */
178718 static void fts5MultiIterNew(
178719 Fts5Index *p, /* FTS5 backend to iterate within */
178720 Fts5Structure *pStruct, /* Structure of specific index */
178721 int flags, /* FTS5INDEX_QUERY_XXX flags */
178722 Fts5Colset *pColset, /* Colset to filter on (or NULL) */
178723 const u8 *pTerm, int nTerm, /* Term to seek to (or NULL/0) */
178724 int iLevel, /* Level to iterate (-1 for all) */
178725 int nSegment, /* Number of segments to merge (iLevel>=0) */
178726 Fts5Iter **ppOut /* New object */
178727 ){
178728 int nSeg = 0; /* Number of segment-iters in use */
178729 int iIter = 0; /* */
178730 int iSeg; /* Used to iterate through segments */
178731 Fts5StructureLevel *pLvl;
178732 Fts5Iter *pNew;
178733
178734 assert( (pTerm==0 && nTerm==0) || iLevel<0 );
178735
178736 /* Allocate space for the new multi-seg-iterator. */
178737 if( p->rc==SQLITE_OK ){
178738 if( iLevel<0 ){
178739 assert( pStruct->nSegment==fts5StructureCountSegments(pStruct) );
178740 nSeg = pStruct->nSegment;
178741 nSeg += (p->pHash ? 1 : 0);
178742 }else{
178743 nSeg = MIN(pStruct->aLevel[iLevel].nSeg, nSegment);
178744 }
178745 }
178746 *ppOut = pNew = fts5MultiIterAlloc(p, nSeg);
178747 if( pNew==0 ) return;
178748 pNew->bRev = (0!=(flags & FTS5INDEX_QUERY_DESC));
178749 pNew->bSkipEmpty = (0!=(flags & FTS5INDEX_QUERY_SKIPEMPTY));
178750 pNew->pStruct = pStruct;
178751 pNew->pColset = pColset;
178752 fts5StructureRef(pStruct);
178753 if( (flags & FTS5INDEX_QUERY_NOOUTPUT)==0 ){
178754 fts5IterSetOutputCb(&p->rc, pNew);
178755 }
178756
178757 /* Initialize each of the component segment iterators. */
178758 if( p->rc==SQLITE_OK ){
178759 if( iLevel<0 ){
178760 Fts5StructureLevel *pEnd = &pStruct->aLevel[pStruct->nLevel];
178761 if( p->pHash ){
178762 /* Add a segment iterator for the current contents of the hash table. */
178763 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
178764 fts5SegIterHashInit(p, pTerm, nTerm, flags, pIter);
178765 }
178766 for(pLvl=&pStruct->aLevel[0]; pLvl<pEnd; pLvl++){
178767 for(iSeg=pLvl->nSeg-1; iSeg>=0; iSeg--){
178768 Fts5StructureSegment *pSeg = &pLvl->aSeg[iSeg];
178769 Fts5SegIter *pIter = &pNew->aSeg[iIter++];
178770 if( pTerm==0 ){
178771 fts5SegIterInit(p, pSeg, pIter);
178772 }else{
178773 fts5SegIterSeekInit(p, pTerm, nTerm, flags, pSeg, pIter);
178774 }
178775 }
178776 }
178777 }else{
178778 pLvl = &pStruct->aLevel[iLevel];
178779 for(iSeg=nSeg-1; iSeg>=0; iSeg--){
178780 fts5SegIterInit(p, &pLvl->aSeg[iSeg], &pNew->aSeg[iIter++]);
178781 }
178782 }
178783 assert( iIter==nSeg );
178784 }
178785
178786 /* If the above was successful, each component iterators now points
178787 ** to the first entry in its segment. In this case initialize the
178788 ** aFirst[] array. Or, if an error has occurred, free the iterator
178789 ** object and set the output variable to NULL. */
178790 if( p->rc==SQLITE_OK ){
178791 for(iIter=pNew->nSeg-1; iIter>0; iIter--){
178792 int iEq;
178793 if( (iEq = fts5MultiIterDoCompare(pNew, iIter)) ){
178794 Fts5SegIter *pSeg = &pNew->aSeg[iEq];
178795 if( p->rc==SQLITE_OK ) pSeg->xNext(p, pSeg, 0);
178796 fts5MultiIterAdvanced(p, pNew, iEq, iIter);
178797 }
178798 }
178799 fts5MultiIterSetEof(pNew);
178800 fts5AssertMultiIterSetup(p, pNew);
178801
178802 if( pNew->bSkipEmpty && fts5MultiIterIsEmpty(p, pNew) ){
178803 fts5MultiIterNext(p, pNew, 0, 0);
178804 }else if( pNew->base.bEof==0 ){
178805 Fts5SegIter *pSeg = &pNew->aSeg[pNew->aFirst[1].iFirst];
178806 pNew->xSetOutputs(pNew, pSeg);
178807 }
178808
178809 }else{
178810 fts5MultiIterFree(pNew);
178811 *ppOut = 0;
178812 }
178813 }
178814
178815 /*
178816 ** Create an Fts5Iter that iterates through the doclist provided
178817 ** as the second argument.
178818 */
178819 static void fts5MultiIterNew2(
178820 Fts5Index *p, /* FTS5 backend to iterate within */
178821 Fts5Data *pData, /* Doclist to iterate through */
178822 int bDesc, /* True for descending rowid order */
178823 Fts5Iter **ppOut /* New object */
178824 ){
178825 Fts5Iter *pNew;
178826 pNew = fts5MultiIterAlloc(p, 2);
178827 if( pNew ){
178828 Fts5SegIter *pIter = &pNew->aSeg[1];
178829
178830 pIter->flags = FTS5_SEGITER_ONETERM;
178831 if( pData->szLeaf>0 ){
178832 pIter->pLeaf = pData;
178833 pIter->iLeafOffset = fts5GetVarint(pData->p, (u64*)&pIter->iRowid);
178834 pIter->iEndofDoclist = pData->nn;
178835 pNew->aFirst[1].iFirst = 1;
178836 if( bDesc ){
178837 pNew->bRev = 1;
178838 pIter->flags |= FTS5_SEGITER_REVERSE;
178839 fts5SegIterReverseInitPage(p, pIter);
178840 }else{
178841 fts5SegIterLoadNPos(p, pIter);
178842 }
178843 pData = 0;
178844 }else{
178845 pNew->base.bEof = 1;
178846 }
178847 fts5SegIterSetNext(p, pIter);
178848
178849 *ppOut = pNew;
178850 }
178851
178852 fts5DataRelease(pData);
178853 }
178854
178855 /*
178856 ** Return true if the iterator is at EOF or if an error has occurred.
178857 ** False otherwise.
178858 */
178859 static int fts5MultiIterEof(Fts5Index *p, Fts5Iter *pIter){
178860 assert( p->rc
178861 || (pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf==0)==pIter->base.bEof
178862 );
178863 return (p->rc || pIter->base.bEof);
178864 }
178865
178866 /*
178867 ** Return the rowid of the entry that the iterator currently points
178868 ** to. If the iterator points to EOF when this function is called the
178869 ** results are undefined.
178870 */
178871 static i64 fts5MultiIterRowid(Fts5Iter *pIter){
178872 assert( pIter->aSeg[ pIter->aFirst[1].iFirst ].pLeaf );
178873 return pIter->aSeg[ pIter->aFirst[1].iFirst ].iRowid;
178874 }
178875
178876 /*
178877 ** Move the iterator to the next entry at or following iMatch.
178878 */
178879 static void fts5MultiIterNextFrom(
178880 Fts5Index *p,
178881 Fts5Iter *pIter,
178882 i64 iMatch
178883 ){
178884 while( 1 ){
178885 i64 iRowid;
178886 fts5MultiIterNext(p, pIter, 1, iMatch);
178887 if( fts5MultiIterEof(p, pIter) ) break;
178888 iRowid = fts5MultiIterRowid(pIter);
178889 if( pIter->bRev==0 && iRowid>=iMatch ) break;
178890 if( pIter->bRev!=0 && iRowid<=iMatch ) break;
178891 }
178892 }
178893
178894 /*
178895 ** Return a pointer to a buffer containing the term associated with the
178896 ** entry that the iterator currently points to.
178897 */
178898 static const u8 *fts5MultiIterTerm(Fts5Iter *pIter, int *pn){
178899 Fts5SegIter *p = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
178900 *pn = p->term.n;
178901 return p->term.p;
178902 }
178903
178904 /*
178905 ** Allocate a new segment-id for the structure pStruct. The new segment
178906 ** id must be between 1 and 65335 inclusive, and must not be used by
178907 ** any currently existing segment. If a free segment id cannot be found,
@@ -177914,19 +178945,18 @@
178945 p->nPendingData = 0;
178946 }
178947 }
178948
178949 /*
178950 ** Return the size of the prefix, in bytes, that buffer
178951 ** (pNew/<length-unknown>) shares with buffer (pOld/nOld).
178952 **
178953 ** Buffer (pNew/<length-unknown>) is guaranteed to be greater
178954 ** than buffer (pOld/nOld).
178955 */
178956 static int fts5PrefixCompress(int nOld, const u8 *pOld, const u8 *pNew){
 
 
 
178957 int i;
 
178958 for(i=0; i<nOld; i++){
178959 if( pOld[i]!=pNew[i] ) break;
178960 }
178961 return i;
178962 }
@@ -178232,17 +179262,17 @@
179262 ** In this case the previous term is not available, so just write a
179263 ** copy of (pTerm/nTerm) into the parent node. This is slightly
179264 ** inefficient, but still correct. */
179265 int n = nTerm;
179266 if( pPage->term.n ){
179267 n = 1 + fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
179268 }
179269 fts5WriteBtreeTerm(p, pWriter, n, pTerm);
179270 pPage = &pWriter->writer;
179271 }
179272 }else{
179273 nPrefix = fts5PrefixCompress(pPage->term.n, pPage->term.p, pTerm);
179274 fts5BufferAppendVarint(&p->rc, &pPage->buf, nPrefix);
179275 }
179276
179277 /* Append the number of bytes of new data, then the term data itself
179278 ** to the page. */
@@ -178401,11 +179431,11 @@
179431 /*
179432 ** Iterator pIter was used to iterate through the input segments of on an
179433 ** incremental merge operation. This function is called if the incremental
179434 ** merge step has finished but the input has not been completely exhausted.
179435 */
179436 static void fts5TrimSegments(Fts5Index *p, Fts5Iter *pIter){
179437 int i;
179438 Fts5Buffer buf;
179439 memset(&buf, 0, sizeof(Fts5Buffer));
179440 for(i=0; i<pIter->nSeg; i++){
179441 Fts5SegIter *pSeg = &pIter->aSeg[i];
@@ -178479,18 +179509,19 @@
179509 int *pnRem /* Write up to this many output leaves */
179510 ){
179511 Fts5Structure *pStruct = *ppStruct;
179512 Fts5StructureLevel *pLvl = &pStruct->aLevel[iLvl];
179513 Fts5StructureLevel *pLvlOut;
179514 Fts5Iter *pIter = 0; /* Iterator to read input data */
179515 int nRem = pnRem ? *pnRem : 0; /* Output leaf pages left to write */
179516 int nInput; /* Number of input segments */
179517 Fts5SegWriter writer; /* Writer object */
179518 Fts5StructureSegment *pSeg; /* Output segment */
179519 Fts5Buffer term;
179520 int bOldest; /* True if the output segment is the oldest */
179521 int eDetail = p->pConfig->eDetail;
179522 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
179523
179524 assert( iLvl<pStruct->nLevel );
179525 assert( pLvl->nMerge<=pLvl->nSeg );
179526
179527 memset(&writer, 0, sizeof(Fts5SegWriter));
@@ -178531,11 +179562,11 @@
179562 nInput = pLvl->nSeg;
179563 }
179564 bOldest = (pLvlOut->nSeg==1 && pStruct->nLevel==iLvl+2);
179565
179566 assert( iLvl>=0 );
179567 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, iLvl, nInput, &pIter);
179568 fts5MultiIterEof(p, pIter)==0;
179569 fts5MultiIterNext(p, pIter, 0, 0)
179570 ){
179571 Fts5SegIter *pSegIter = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
179572 int nPos; /* position-list size field value */
@@ -178603,11 +179634,11 @@
179634 assert( pSeg->pgnoLast>0 );
179635 fts5TrimSegments(p, pIter);
179636 pLvl->nMerge = nInput;
179637 }
179638
179639 fts5MultiIterFree(pIter);
179640 fts5BufferFree(&term);
179641 if( pnRem ) *pnRem -= writer.nLeafWritten;
179642 }
179643
179644 /*
@@ -178976,279 +180007,33 @@
180007 fts5StructureRelease(pStruct);
180008
180009 return fts5IndexReturn(p);
180010 }
180011
180012 static void fts5AppendRowid(
180013 Fts5Index *p,
180014 i64 iDelta,
180015 Fts5Iter *pUnused,
180016 Fts5Buffer *pBuf
180017 ){
180018 UNUSED_PARAM(pUnused);
180019 fts5BufferAppendVarint(&p->rc, pBuf, iDelta);
180020 }
180021
180022 static void fts5AppendPoslist(
180023 Fts5Index *p,
180024 i64 iDelta,
180025 Fts5Iter *pMulti,
180026 Fts5Buffer *pBuf
180027 ){
180028 int nData = pMulti->base.nData;
180029 assert( nData>0 );
180030 if( p->rc==SQLITE_OK && 0==fts5BufferGrow(&p->rc, pBuf, nData+9+9) ){
180031 fts5BufferSafeAppendVarint(pBuf, iDelta);
180032 fts5BufferSafeAppendVarint(pBuf, nData*2);
180033 fts5BufferSafeAppendBlob(pBuf, pMulti->base.pData, nData);
180034 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180035 }
180036
180037
180038 static void fts5DoclistIterNext(Fts5DoclistIter *pIter){
180039 u8 *p = pIter->aPoslist + pIter->nSize + pIter->nPoslist;
@@ -179388,73 +180173,107 @@
180173 ){
180174 if( p2->n ){
180175 i64 iLastRowid = 0;
180176 Fts5DoclistIter i1;
180177 Fts5DoclistIter i2;
180178 Fts5Buffer out = {0, 0, 0};
180179 Fts5Buffer tmp = {0, 0, 0};
 
 
180180
180181 if( sqlite3Fts5BufferSize(&p->rc, &out, p1->n + p2->n) ) return;
180182 fts5DoclistIterInit(p1, &i1);
180183 fts5DoclistIterInit(p2, &i2);
180184
180185 while( 1 ){
180186 if( i1.iRowid<i2.iRowid ){
180187 /* Copy entry from i1 */
180188 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
180189 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.nPoslist+i1.nSize);
180190 fts5DoclistIterNext(&i1);
180191 if( i1.aPoslist==0 ) break;
180192 }
180193 else if( i2.iRowid!=i1.iRowid ){
180194 /* Copy entry from i2 */
180195 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
180196 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.nPoslist+i2.nSize);
180197 fts5DoclistIterNext(&i2);
180198 if( i2.aPoslist==0 ) break;
180199 }
180200 else{
180201 /* Merge the two position lists. */
180202 i64 iPos1 = 0;
180203 i64 iPos2 = 0;
180204 int iOff1 = 0;
180205 int iOff2 = 0;
180206 u8 *a1 = &i1.aPoslist[i1.nSize];
180207 u8 *a2 = &i2.aPoslist[i2.nSize];
180208
180209 i64 iPrev = 0;
180210 Fts5PoslistWriter writer;
180211 memset(&writer, 0, sizeof(writer));
180212
180213 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
180214 fts5BufferZero(&tmp);
180215 sqlite3Fts5BufferSize(&p->rc, &tmp, i1.nPoslist + i2.nPoslist);
180216 if( p->rc ) break;
180217
180218 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180219 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180220 assert( iPos1>=0 && iPos2>=0 );
180221
180222 if( iPos1<iPos2 ){
180223 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180224 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180225 }else{
180226 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180227 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180228 }
180229
180230 if( iPos1>=0 && iPos2>=0 ){
180231 while( 1 ){
180232 if( iPos1<iPos2 ){
180233 if( iPos1!=iPrev ){
180234 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180235 }
180236 sqlite3Fts5PoslistNext64(a1, i1.nPoslist, &iOff1, &iPos1);
180237 if( iPos1<0 ) break;
180238 }else{
180239 assert( iPos2!=iPrev );
180240 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180241 sqlite3Fts5PoslistNext64(a2, i2.nPoslist, &iOff2, &iPos2);
180242 if( iPos2<0 ) break;
180243 }
180244 }
180245 }
180246
180247 if( iPos1>=0 ){
180248 if( iPos1!=iPrev ){
180249 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos1);
180250 }
180251 fts5BufferSafeAppendBlob(&tmp, &a1[iOff1], i1.nPoslist-iOff1);
180252 }else{
180253 assert( iPos2>=0 && iPos2!=iPrev );
180254 sqlite3Fts5PoslistSafeAppend(&tmp, &iPrev, iPos2);
180255 fts5BufferSafeAppendBlob(&tmp, &a2[iOff2], i2.nPoslist-iOff2);
180256 }
180257
180258 /* WRITEPOSLISTSIZE */
180259 fts5BufferSafeAppendVarint(&out, tmp.n * 2);
180260 fts5BufferSafeAppendBlob(&out, tmp.p, tmp.n);
180261 fts5DoclistIterNext(&i1);
180262 fts5DoclistIterNext(&i2);
180263 if( i1.aPoslist==0 || i2.aPoslist==0 ) break;
180264 }
180265 }
180266
180267 if( i1.aPoslist ){
180268 fts5MergeAppendDocid(&out, iLastRowid, i1.iRowid);
180269 fts5BufferSafeAppendBlob(&out, i1.aPoslist, i1.aEof - i1.aPoslist);
180270 }
180271 else if( i2.aPoslist ){
180272 fts5MergeAppendDocid(&out, iLastRowid, i2.iRowid);
180273 fts5BufferSafeAppendBlob(&out, i2.aPoslist, i2.aEof - i2.aPoslist);
180274 }
180275
180276 fts5BufferSet(&p->rc, p1, out.n, out.p);
180277 fts5BufferFree(&tmp);
180278 fts5BufferFree(&out);
180279 }
@@ -179464,18 +180283,18 @@
180283 Fts5Index *p, /* Index to read from */
180284 int bDesc, /* True for "ORDER BY rowid DESC" */
180285 const u8 *pToken, /* Buffer containing prefix to match */
180286 int nToken, /* Size of buffer pToken in bytes */
180287 Fts5Colset *pColset, /* Restrict matches to these columns */
180288 Fts5Iter **ppIter /* OUT: New iterator */
180289 ){
180290 Fts5Structure *pStruct;
180291 Fts5Buffer *aBuf;
180292 const int nBuf = 32;
180293
180294 void (*xMerge)(Fts5Index*, Fts5Buffer*, Fts5Buffer*);
180295 void (*xAppend)(Fts5Index*, i64, Fts5Iter*, Fts5Buffer*);
180296 if( p->pConfig->eDetail==FTS5_DETAIL_NONE ){
180297 xMerge = fts5MergeRowidLists;
180298 xAppend = fts5AppendRowid;
180299 }else{
180300 xMerge = fts5MergePrefixLists;
@@ -179484,32 +180303,40 @@
180303
180304 aBuf = (Fts5Buffer*)fts5IdxMalloc(p, sizeof(Fts5Buffer)*nBuf);
180305 pStruct = fts5StructureRead(p);
180306
180307 if( aBuf && pStruct ){
180308 const int flags = FTS5INDEX_QUERY_SCAN
180309 | FTS5INDEX_QUERY_SKIPEMPTY
180310 | FTS5INDEX_QUERY_NOOUTPUT;
180311 int i;
180312 i64 iLastRowid = 0;
180313 Fts5Iter *p1 = 0; /* Iterator used to gather data from index */
180314 Fts5Data *pData;
180315 Fts5Buffer doclist;
180316 int bNewTerm = 1;
180317
180318 memset(&doclist, 0, sizeof(doclist));
180319 fts5MultiIterNew(p, pStruct, flags, pColset, pToken, nToken, -1, 0, &p1);
180320 fts5IterSetOutputCb(&p->rc, p1);
180321 for( /* no-op */ ;
180322 fts5MultiIterEof(p, p1)==0;
180323 fts5MultiIterNext2(p, p1, &bNewTerm)
180324 ){
180325 Fts5SegIter *pSeg = &p1->aSeg[ p1->aFirst[1].iFirst ];
180326 int nTerm = pSeg->term.n;
180327 const u8 *pTerm = pSeg->term.p;
180328 p1->xSetOutputs(p1, pSeg);
180329
180330 assert_nc( memcmp(pToken, pTerm, MIN(nToken, nTerm))<=0 );
180331 if( bNewTerm ){
180332 if( nTerm<nToken || memcmp(pToken, pTerm, nToken) ) break;
180333 }
180334
180335 if( p1->base.nData==0 ) continue;
180336
180337 if( p1->base.iRowid<=iLastRowid && doclist.n>0 ){
180338 for(i=0; p->rc==SQLITE_OK && doclist.n; i++){
180339 assert( i<nBuf );
180340 if( aBuf[i].n==0 ){
180341 fts5BufferSwap(&doclist, &aBuf[i]);
180342 fts5BufferZero(&doclist);
@@ -179519,22 +180346,21 @@
180346 }
180347 }
180348 iLastRowid = 0;
180349 }
180350
180351 xAppend(p, p1->base.iRowid-iLastRowid, p1, &doclist);
180352 iLastRowid = p1->base.iRowid;
 
180353 }
180354
180355 for(i=0; i<nBuf; i++){
180356 if( p->rc==SQLITE_OK ){
180357 xMerge(p, &doclist, &aBuf[i]);
180358 }
180359 fts5BufferFree(&aBuf[i]);
180360 }
180361 fts5MultiIterFree(p1);
180362
180363 pData = fts5IdxMalloc(p, sizeof(Fts5Data) + doclist.n);
180364 if( pData ){
180365 pData->p = (u8*)&pData[1];
180366 pData->nn = pData->szLeaf = doclist.n;
@@ -179591,11 +180417,11 @@
180417 ** records must be invalidated.
180418 */
180419 static int sqlite3Fts5IndexRollback(Fts5Index *p){
180420 fts5CloseReader(p);
180421 fts5IndexDiscardData(p);
180422 /* assert( p->rc==SQLITE_OK ); */
180423 return SQLITE_OK;
180424 }
180425
180426 /*
180427 ** The %_data table is completely empty when this function is called. This
@@ -179763,26 +180589,31 @@
180589 int flags, /* Mask of FTS5INDEX_QUERY_X flags */
180590 Fts5Colset *pColset, /* Match these columns only */
180591 Fts5IndexIter **ppIter /* OUT: New iterator object */
180592 ){
180593 Fts5Config *pConfig = p->pConfig;
180594 Fts5Iter *pRet = 0;
 
180595 Fts5Buffer buf = {0, 0, 0};
180596
180597 /* If the QUERY_SCAN flag is set, all other flags must be clear. */
180598 assert( (flags & FTS5INDEX_QUERY_SCAN)==0 || flags==FTS5INDEX_QUERY_SCAN );
180599
180600 if( sqlite3Fts5BufferSize(&p->rc, &buf, nToken+1)==0 ){
180601 int iIdx = 0; /* Index to search */
180602 memcpy(&buf.p[1], pToken, nToken);
180603
180604 /* Figure out which index to search and set iIdx accordingly. If this
180605 ** is a prefix query for which there is no prefix index, set iIdx to
180606 ** greater than pConfig->nPrefix to indicate that the query will be
180607 ** satisfied by scanning multiple terms in the main index.
180608 **
180609 ** If the QUERY_TEST_NOIDX flag was specified, then this must be a
180610 ** prefix-query. Instead of using a prefix-index (if one exists),
180611 ** evaluate the prefix query using the main FTS index. This is used
180612 ** for internal sanity checking by the integrity-check in debug
180613 ** mode only. */
180614 #ifdef SQLITE_DEBUG
180615 if( pConfig->bPrefixIndex==0 || (flags & FTS5INDEX_QUERY_TEST_NOIDX) ){
180616 assert( flags & FTS5INDEX_QUERY_PREFIX );
180617 iIdx = 1+pConfig->nPrefix;
180618 }else
180619 #endif
@@ -179792,54 +180623,62 @@
180623 if( pConfig->aPrefix[iIdx-1]==nChar ) break;
180624 }
180625 }
180626
180627 if( iIdx<=pConfig->nPrefix ){
180628 /* Straight index lookup */
180629 Fts5Structure *pStruct = fts5StructureRead(p);
180630 buf.p[0] = (u8)(FTS5_MAIN_PREFIX + iIdx);
180631 if( pStruct ){
180632 fts5MultiIterNew(p, pStruct, flags | FTS5INDEX_QUERY_SKIPEMPTY,
180633 pColset, buf.p, nToken+1, -1, 0, &pRet
180634 );
180635 fts5StructureRelease(pStruct);
180636 }
180637 }else{
180638 /* Scan multiple terms in the main index */
180639 int bDesc = (flags & FTS5INDEX_QUERY_DESC)!=0;
180640 buf.p[0] = FTS5_MAIN_PREFIX;
180641 fts5SetupPrefixIter(p, bDesc, buf.p, nToken+1, pColset, &pRet);
180642 assert( p->rc!=SQLITE_OK || pRet->pColset==0 );
180643 fts5IterSetOutputCb(&p->rc, pRet);
180644 if( p->rc==SQLITE_OK ){
180645 Fts5SegIter *pSeg = &pRet->aSeg[pRet->aFirst[1].iFirst];
180646 if( pSeg->pLeaf ) pRet->xSetOutputs(pRet, pSeg);
180647 }
180648 }
180649
180650 if( p->rc ){
180651 sqlite3Fts5IterClose(&pRet->base);
180652 pRet = 0;
180653 fts5CloseReader(p);
180654 }
180655
180656 *ppIter = &pRet->base;
180657 sqlite3Fts5BufferFree(&buf);
180658 }
180659 return fts5IndexReturn(p);
180660 }
180661
180662 /*
180663 ** Return true if the iterator passed as the only argument is at EOF.
180664 */
 
 
 
 
 
180665 /*
180666 ** Move to the next matching rowid.
180667 */
180668 static int sqlite3Fts5IterNext(Fts5IndexIter *pIndexIter){
180669 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
180670 assert( pIter->pIndex->rc==SQLITE_OK );
180671 fts5MultiIterNext(pIter->pIndex, pIter, 0, 0);
180672 return fts5IndexReturn(pIter->pIndex);
180673 }
180674
180675 /*
180676 ** Move to the next matching term/rowid. Used by the fts5vocab module.
180677 */
180678 static int sqlite3Fts5IterNextScan(Fts5IndexIter *pIndexIter){
180679 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
180680 Fts5Index *p = pIter->pIndex;
180681
180682 assert( pIter->pIndex->rc==SQLITE_OK );
180683
180684 fts5MultiIterNext(p, pIter, 0, 0);
@@ -179846,11 +180685,11 @@
180685 if( p->rc==SQLITE_OK ){
180686 Fts5SegIter *pSeg = &pIter->aSeg[ pIter->aFirst[1].iFirst ];
180687 if( pSeg->pLeaf && pSeg->term.p[0]!=FTS5_MAIN_PREFIX ){
180688 fts5DataRelease(pSeg->pLeaf);
180689 pSeg->pLeaf = 0;
180690 pIter->base.bEof = 1;
180691 }
180692 }
180693
180694 return fts5IndexReturn(pIter->pIndex);
180695 }
@@ -179858,135 +180697,34 @@
180697 /*
180698 ** Move to the next matching rowid that occurs at or after iMatch. The
180699 ** definition of "at or after" depends on whether this iterator iterates
180700 ** in ascending or descending rowid order.
180701 */
180702 static int sqlite3Fts5IterNextFrom(Fts5IndexIter *pIndexIter, i64 iMatch){
180703 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
180704 fts5MultiIterNextFrom(pIter->pIndex, pIter, iMatch);
180705 return fts5IndexReturn(pIter->pIndex);
180706 }
180707
 
 
 
 
 
 
 
180708 /*
180709 ** Return the current term.
180710 */
180711 static const char *sqlite3Fts5IterTerm(Fts5IndexIter *pIndexIter, int *pn){
180712 int n;
180713 const char *z = (const char*)fts5MultiIterTerm((Fts5Iter*)pIndexIter, &n);
180714 *pn = n-1;
180715 return &z[1];
180716 }
180717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180718 /*
180719 ** Close an iterator opened by an earlier call to sqlite3Fts5IndexQuery().
180720 */
180721 static void sqlite3Fts5IterClose(Fts5IndexIter *pIndexIter){
180722 if( pIndexIter ){
180723 Fts5Iter *pIter = (Fts5Iter*)pIndexIter;
180724 Fts5Index *pIndex = pIter->pIndex;
180725 fts5MultiIterFree(pIter);
180726 fts5CloseReader(pIndex);
180727 }
180728 }
180729
180730 /*
@@ -180147,39 +180885,34 @@
180885 int flags, /* Flags for Fts5IndexQuery */
180886 u64 *pCksum /* IN/OUT: Checksum value */
180887 ){
180888 int eDetail = p->pConfig->eDetail;
180889 u64 cksum = *pCksum;
180890 Fts5IndexIter *pIter = 0;
180891 int rc = sqlite3Fts5IndexQuery(p, z, n, flags, 0, &pIter);
 
180892
180893 while( rc==SQLITE_OK && 0==sqlite3Fts5IterEof(pIter) ){
180894 i64 rowid = pIter->iRowid;
180895
180896 if( eDetail==FTS5_DETAIL_NONE ){
180897 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, 0, 0, iIdx, z, n);
180898 }else{
180899 Fts5PoslistReader sReader;
180900 for(sqlite3Fts5PoslistReaderInit(pIter->pData, pIter->nData, &sReader);
180901 sReader.bEof==0;
180902 sqlite3Fts5PoslistReaderNext(&sReader)
180903 ){
180904 int iCol = FTS5_POS2COLUMN(sReader.iPos);
180905 int iOff = FTS5_POS2OFFSET(sReader.iPos);
180906 cksum ^= sqlite3Fts5IndexEntryCksum(rowid, iCol, iOff, iIdx, z, n);
 
 
 
180907 }
180908 }
180909 if( rc==SQLITE_OK ){
180910 rc = sqlite3Fts5IterNext(pIter);
180911 }
180912 }
180913 sqlite3Fts5IterClose(pIter);
 
180914
180915 *pCksum = cksum;
180916 return rc;
180917 }
180918
@@ -180480,18 +181213,19 @@
181213 */
181214 static int sqlite3Fts5IndexIntegrityCheck(Fts5Index *p, u64 cksum){
181215 int eDetail = p->pConfig->eDetail;
181216 u64 cksum2 = 0; /* Checksum based on contents of indexes */
181217 Fts5Buffer poslist = {0,0,0}; /* Buffer used to hold a poslist */
181218 Fts5Iter *pIter; /* Used to iterate through entire index */
181219 Fts5Structure *pStruct; /* Index structure */
181220
181221 #ifdef SQLITE_DEBUG
181222 /* Used by extra internal tests only run if NDEBUG is not defined */
181223 u64 cksum3 = 0; /* Checksum based on contents of indexes */
181224 Fts5Buffer term = {0,0,0}; /* Buffer used to hold most recent term */
181225 #endif
181226 const int flags = FTS5INDEX_QUERY_NOOUTPUT;
181227
181228 /* Load the FTS index structure */
181229 pStruct = fts5StructureRead(p);
181230
181231 /* Check that the internal nodes of each segment match the leaves */
@@ -180516,11 +181250,11 @@
181250 **
181251 ** As each term visited by the linear scans, a separate query for the
181252 ** same term is performed. cksum3 is calculated based on the entries
181253 ** extracted by these queries.
181254 */
181255 for(fts5MultiIterNew(p, pStruct, flags, 0, 0, 0, -1, 0, &pIter);
181256 fts5MultiIterEof(p, pIter)==0;
181257 fts5MultiIterNext(p, pIter, 0, 0)
181258 ){
181259 int n; /* Size of term in bytes */
181260 i64 iPos = 0; /* Position read from poslist */
@@ -180545,11 +181279,11 @@
181279 }
181280 }
181281 }
181282 fts5TestTerm(p, &term, 0, 0, cksum2, &cksum3);
181283
181284 fts5MultiIterFree(pIter);
181285 if( p->rc==SQLITE_OK && cksum!=cksum2 ) p->rc = FTS5_CORRUPT;
181286
181287 fts5StructureRelease(pStruct);
181288 #ifdef SQLITE_DEBUG
181289 fts5BufferFree(&term);
@@ -180782,10 +181516,11 @@
181516 int rc = SQLITE_OK; /* Return code */
181517 int nSpace = 0;
181518 int eDetailNone = (sqlite3_user_data(pCtx)!=0);
181519
181520 assert( nArg==2 );
181521 UNUSED_PARAM(nArg);
181522 memset(&s, 0, sizeof(Fts5Buffer));
181523 iRowid = sqlite3_value_int64(apVal[0]);
181524
181525 /* Make a copy of the second argument (a blob) in aBlob[]. The aBlob[]
181526 ** copy is followed by FTS5_DATA_ZERO_PADDING 0x00 bytes, which prevents
@@ -181234,14 +181969,14 @@
181969 #define FTS5_BI_ORDER_DESC 0x0080
181970
181971 /*
181972 ** Values for Fts5Cursor.csrflags
181973 */
181974 #define FTS5CSR_EOF 0x01
181975 #define FTS5CSR_REQUIRE_CONTENT 0x02
181976 #define FTS5CSR_REQUIRE_DOCSIZE 0x04
181977 #define FTS5CSR_REQUIRE_INST 0x08
181978 #define FTS5CSR_FREE_ZRANK 0x10
181979 #define FTS5CSR_REQUIRE_RESEEK 0x20
181980 #define FTS5CSR_REQUIRE_POSLIST 0x40
181981
181982 #define BitFlagAllTest(x,y) (((x) & (y))==(y))
@@ -181552,11 +182287,11 @@
182287
182288 /* Set idxFlags flags for all WHERE clause terms that will be used. */
182289 for(i=0; i<pInfo->nConstraint; i++){
182290 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
182291 int j;
182292 for(j=0; j<ArraySize(aConstraint); j++){
182293 struct Constraint *pC = &aConstraint[j];
182294 if( p->iColumn==aColMap[pC->iCol] && p->op & pC->op ){
182295 if( p->usable ){
182296 pC->iConsIndex = i;
182297 idxFlags |= pC->fts5op;
@@ -181599,11 +182334,11 @@
182334 pInfo->estimatedCost = bHasMatch ? 1000.0 : 1000000.0;
182335 }
182336
182337 /* Assign argvIndex values to each constraint in use. */
182338 iNext = 1;
182339 for(i=0; i<ArraySize(aConstraint); i++){
182340 struct Constraint *pC = &aConstraint[i];
182341 if( pC->iConsIndex>=0 ){
182342 pInfo->aConstraintUsage[pC->iConsIndex].argvIndex = iNext++;
182343 pInfo->aConstraintUsage[pC->iConsIndex].omit = (unsigned char)pC->omit;
182344 }
@@ -181792,18 +182527,19 @@
182527 Fts5Table *pTab = (Fts5Table*)(pCsr->base.pVtab);
182528 int bDesc = pCsr->bDesc;
182529 i64 iRowid = sqlite3Fts5ExprRowid(pCsr->pExpr);
182530
182531 rc = sqlite3Fts5ExprFirst(pCsr->pExpr, pTab->pIndex, iRowid, bDesc);
182532 if( rc==SQLITE_OK && iRowid!=sqlite3Fts5ExprRowid(pCsr->pExpr) ){
182533 *pbSkip = 1;
182534 }
182535
182536 CsrFlagClear(pCsr, FTS5CSR_REQUIRE_RESEEK);
182537 fts5CsrNewrow(pCsr);
182538 if( sqlite3Fts5ExprEof(pCsr->pExpr) ){
182539 CsrFlagSet(pCsr, FTS5CSR_EOF);
182540 *pbSkip = 1;
182541 }
182542 }
182543 return rc;
182544 }
182545
@@ -181816,28 +182552,28 @@
182552 ** even if we reach end-of-file. The fts5EofMethod() will be called
182553 ** subsequently to determine whether or not an EOF was hit.
182554 */
182555 static int fts5NextMethod(sqlite3_vtab_cursor *pCursor){
182556 Fts5Cursor *pCsr = (Fts5Cursor*)pCursor;
182557 int rc;
182558
182559 assert( (pCsr->ePlan<3)==
182560 (pCsr->ePlan==FTS5_PLAN_MATCH || pCsr->ePlan==FTS5_PLAN_SOURCE)
182561 );
182562 assert( !CsrFlagTest(pCsr, FTS5CSR_EOF) );
182563
182564 if( pCsr->ePlan<3 ){
182565 int bSkip = 0;
182566 if( (rc = fts5CursorReseek(pCsr, &bSkip)) || bSkip ) return rc;
182567 rc = sqlite3Fts5ExprNext(pCsr->pExpr, pCsr->iLastRowid);
182568 CsrFlagSet(pCsr, sqlite3Fts5ExprEof(pCsr->pExpr));
 
 
182569 fts5CsrNewrow(pCsr);
182570 }else{
182571 switch( pCsr->ePlan ){
182572 case FTS5_PLAN_SPECIAL: {
182573 CsrFlagSet(pCsr, FTS5CSR_EOF);
182574 rc = SQLITE_OK;
182575 break;
182576 }
182577
182578 case FTS5_PLAN_SORTED_MATCH: {
182579 rc = fts5SorterNext(pCsr);
@@ -182109,11 +182845,11 @@
182845 ** 3. A full-table scan.
182846 */
182847 static int fts5FilterMethod(
182848 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
182849 int idxNum, /* Strategy index */
182850 const char *zUnused, /* Unused */
182851 int nVal, /* Number of elements in apVal */
182852 sqlite3_value **apVal /* Arguments for the indexing scheme */
182853 ){
182854 Fts5Table *pTab = (Fts5Table*)(pCursor->pVtab);
182855 Fts5Config *pConfig = pTab->pConfig;
@@ -182126,10 +182862,13 @@
182862 sqlite3_value *pRank = 0; /* rank MATCH ? expression (or NULL) */
182863 sqlite3_value *pRowidEq = 0; /* rowid = ? expression (or NULL) */
182864 sqlite3_value *pRowidLe = 0; /* rowid <= ? expression (or NULL) */
182865 sqlite3_value *pRowidGe = 0; /* rowid >= ? expression (or NULL) */
182866 char **pzErrmsg = pConfig->pzErrmsg;
182867
182868 UNUSED_PARAM(zUnused);
182869 UNUSED_PARAM(nVal);
182870
182871 if( pCsr->ePlan ){
182872 fts5FreeCursorComponents(pCsr);
182873 memset(&pCsr->ePlan, 0, sizeof(Fts5Cursor) - ((u8*)&pCsr->ePlan-(u8*)pCsr));
182874 }
@@ -182411,12 +183150,11 @@
183150 return rc;
183151 }
183152
183153 static int fts5SpecialDelete(
183154 Fts5Table *pTab,
183155 sqlite3_value **apVal
 
183156 ){
183157 int rc = SQLITE_OK;
183158 int eType1 = sqlite3_value_type(apVal[1]);
183159 if( eType1==SQLITE_INTEGER ){
183160 sqlite3_int64 iDel = sqlite3_value_int64(apVal[1]);
@@ -182488,11 +183226,11 @@
183226 /* A "special" INSERT op. These are handled separately. */
183227 const char *z = (const char*)sqlite3_value_text(apVal[2+pConfig->nCol]);
183228 if( pConfig->eContent!=FTS5_CONTENT_NORMAL
183229 && 0==sqlite3_stricmp("delete", z)
183230 ){
183231 rc = fts5SpecialDelete(pTab, apVal);
183232 }else{
183233 rc = fts5SpecialInsert(pTab, z, apVal[2 + pConfig->nCol + 1]);
183234 }
183235 }else{
183236 /* A regular INSERT, UPDATE or DELETE statement. The trick here is that
@@ -182589,10 +183327,11 @@
183327
183328 /*
183329 ** Implementation of xBegin() method.
183330 */
183331 static int fts5BeginMethod(sqlite3_vtab *pVtab){
183332 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
183333 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_BEGIN, 0);
183334 return SQLITE_OK;
183335 }
183336
183337 /*
@@ -182599,10 +183338,11 @@
183338 ** Implementation of xCommit() method. This is a no-op. The contents of
183339 ** the pending-terms hash-table have already been flushed into the database
183340 ** by fts5SyncMethod().
183341 */
183342 static int fts5CommitMethod(sqlite3_vtab *pVtab){
183343 UNUSED_PARAM(pVtab); /* Call below is a no-op for NDEBUG builds */
183344 fts5CheckTransactionState((Fts5Table*)pVtab, FTS5_COMMIT, 0);
183345 return SQLITE_OK;
183346 }
183347
183348 /*
@@ -182852,16 +183592,18 @@
183592 }
183593
183594 static int fts5ColumnSizeCb(
183595 void *pContext, /* Pointer to int */
183596 int tflags,
183597 const char *pUnused, /* Buffer containing token */
183598 int nUnused, /* Size of token in bytes */
183599 int iUnused1, /* Start offset of token */
183600 int iUnused2 /* End offset of token */
183601 ){
183602 int *pCnt = (int*)pContext;
183603 UNUSED_PARAM2(pUnused, nUnused);
183604 UNUSED_PARAM2(iUnused1, iUnused2);
183605 if( (tflags & FTS5_TOKEN_COLOCATED)==0 ){
183606 (*pCnt)++;
183607 }
183608 return SQLITE_OK;
183609 }
@@ -182973,14 +183715,15 @@
183715
183716 return pRet;
183717 }
183718
183719 static void fts5ApiPhraseNext(
183720 Fts5Context *pUnused,
183721 Fts5PhraseIter *pIter,
183722 int *piCol, int *piOff
183723 ){
183724 UNUSED_PARAM(pUnused);
183725 if( pIter->a>=pIter->b ){
183726 *piCol = -1;
183727 *piOff = -1;
183728 }else{
183729 int iVal;
@@ -183128,16 +183871,15 @@
183871 int rc;
183872 Fts5Cursor *pNew = 0;
183873
183874 rc = fts5OpenMethod(pCsr->base.pVtab, (sqlite3_vtab_cursor**)&pNew);
183875 if( rc==SQLITE_OK ){
 
183876 pNew->ePlan = FTS5_PLAN_MATCH;
183877 pNew->iFirstRowid = SMALLEST_INT64;
183878 pNew->iLastRowid = LARGEST_INT64;
183879 pNew->base.pVtab = (sqlite3_vtab*)pTab;
183880 rc = sqlite3Fts5ExprClonePhrase(pCsr->pExpr, iPhrase, &pNew->pExpr);
183881 }
183882
183883 if( rc==SQLITE_OK ){
183884 for(rc = fts5CursorFirst(pTab, pNew, 0);
183885 rc==SQLITE_OK && CsrFlagTest(pNew, FTS5CSR_EOF)==0;
@@ -183346,18 +184088,19 @@
184088 ** This routine implements the xFindFunction method for the FTS3
184089 ** virtual table.
184090 */
184091 static int fts5FindFunctionMethod(
184092 sqlite3_vtab *pVtab, /* Virtual table handle */
184093 int nUnused, /* Number of SQL function arguments */
184094 const char *zName, /* Name of SQL function */
184095 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
184096 void **ppArg /* OUT: User data for *pxFunc */
184097 ){
184098 Fts5Table *pTab = (Fts5Table*)pVtab;
184099 Fts5Auxiliary *pAux;
184100
184101 UNUSED_PARAM(nUnused);
184102 pAux = fts5FindAuxiliary(pTab, zName);
184103 if( pAux ){
184104 *pxFunc = fts5ApiCallback;
184105 *ppArg = (void*)pAux;
184106 return 1;
@@ -183383,10 +184126,11 @@
184126 **
184127 ** Flush the contents of the pending-terms table to disk.
184128 */
184129 static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
184130 Fts5Table *pTab = (Fts5Table*)pVtab;
184131 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
184132 fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
184133 fts5TripCursors(pTab);
184134 return sqlite3Fts5StorageSync(pTab->pStorage, 0);
184135 }
184136
@@ -183395,10 +184139,11 @@
184139 **
184140 ** This is a no-op.
184141 */
184142 static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
184143 Fts5Table *pTab = (Fts5Table*)pVtab;
184144 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
184145 fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
184146 fts5TripCursors(pTab);
184147 return sqlite3Fts5StorageSync(pTab->pStorage, 0);
184148 }
184149
@@ -183407,10 +184152,11 @@
184152 **
184153 ** Discard the contents of the pending terms table.
184154 */
184155 static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
184156 Fts5Table *pTab = (Fts5Table*)pVtab;
184157 UNUSED_PARAM(iSavepoint); /* Call below is a no-op for NDEBUG builds */
184158 fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
184159 fts5TripCursors(pTab);
184160 return sqlite3Fts5StorageRollback(pTab->pStorage);
184161 }
184162
@@ -183586,14 +184332,15 @@
184332 }
184333
184334 static void fts5Fts5Func(
184335 sqlite3_context *pCtx, /* Function call context */
184336 int nArg, /* Number of args */
184337 sqlite3_value **apUnused /* Function arguments */
184338 ){
184339 Fts5Global *pGlobal = (Fts5Global*)sqlite3_user_data(pCtx);
184340 char buf[8];
184341 UNUSED_PARAM2(nArg, apUnused);
184342 assert( nArg==0 );
184343 assert( sizeof(buf)>=sizeof(pGlobal) );
184344 memcpy(buf, (void*)&pGlobal, sizeof(pGlobal));
184345 sqlite3_result_blob(pCtx, buf, sizeof(pGlobal), SQLITE_TRANSIENT);
184346 }
@@ -183602,14 +184349,15 @@
184349 ** Implementation of fts5_source_id() function.
184350 */
184351 static void fts5SourceIdFunc(
184352 sqlite3_context *pCtx, /* Function call context */
184353 int nArg, /* Number of args */
184354 sqlite3_value **apUnused /* Function arguments */
184355 ){
184356 assert( nArg==0 );
184357 UNUSED_PARAM2(nArg, apUnused);
184358 sqlite3_result_text(pCtx, "fts5: 2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f", -1, SQLITE_TRANSIENT);
184359 }
184360
184361 static int fts5Init(sqlite3 *db){
184362 static const sqlite3_module fts5Mod = {
184363 /* iVersion */ 2,
@@ -184050,11 +184798,11 @@
184798 int rc = SQLITE_OK;
184799 if( p ){
184800 int i;
184801
184802 /* Finalize all SQL statements */
184803 for(i=0; i<ArraySize(p->aStmt); i++){
184804 sqlite3_finalize(p->aStmt[i]);
184805 }
184806
184807 sqlite3_free(p);
184808 }
@@ -184074,15 +184822,16 @@
184822 static int fts5StorageInsertCallback(
184823 void *pContext, /* Pointer to Fts5InsertCtx object */
184824 int tflags,
184825 const char *pToken, /* Buffer containing token */
184826 int nToken, /* Size of token in bytes */
184827 int iUnused1, /* Start offset of token */
184828 int iUnused2 /* End offset of token */
184829 ){
184830 Fts5InsertCtx *pCtx = (Fts5InsertCtx*)pContext;
184831 Fts5Index *pIdx = pCtx->pStorage->pIndex;
184832 UNUSED_PARAM2(iUnused1, iUnused2);
184833 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
184834 pCtx->szCol++;
184835 }
184836 return sqlite3Fts5IndexWrite(pIdx, pCtx->iCol, pCtx->szCol-1, pToken, nToken);
184837 }
@@ -184509,20 +185258,22 @@
185258 static int fts5StorageIntegrityCallback(
185259 void *pContext, /* Pointer to Fts5IntegrityCtx object */
185260 int tflags,
185261 const char *pToken, /* Buffer containing token */
185262 int nToken, /* Size of token in bytes */
185263 int iUnused1, /* Start offset of token */
185264 int iUnused2 /* End offset of token */
185265 ){
185266 Fts5IntegrityCtx *pCtx = (Fts5IntegrityCtx*)pContext;
185267 Fts5Termset *pTermset = pCtx->pTermset;
185268 int bPresent;
185269 int ii;
185270 int rc = SQLITE_OK;
185271 int iPos;
185272 int iCol;
185273
185274 UNUSED_PARAM2(iUnused1, iUnused2);
185275
185276 if( (tflags & FTS5_TOKEN_COLOCATED)==0 || pCtx->szCol==0 ){
185277 pCtx->szCol++;
185278 }
185279
@@ -184897,16 +185648,17 @@
185648
185649 /*
185650 ** Create an "ascii" tokenizer.
185651 */
185652 static int fts5AsciiCreate(
185653 void *pUnused,
185654 const char **azArg, int nArg,
185655 Fts5Tokenizer **ppOut
185656 ){
185657 int rc = SQLITE_OK;
185658 AsciiTokenizer *p = 0;
185659 UNUSED_PARAM(pUnused);
185660 if( nArg%2 ){
185661 rc = SQLITE_ERROR;
185662 }else{
185663 p = sqlite3_malloc(sizeof(AsciiTokenizer));
185664 if( p==0 ){
@@ -184951,11 +185703,11 @@
185703 ** Tokenize some text using the ascii tokenizer.
185704 */
185705 static int fts5AsciiTokenize(
185706 Fts5Tokenizer *pTokenizer,
185707 void *pCtx,
185708 int iUnused,
185709 const char *pText, int nText,
185710 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
185711 ){
185712 AsciiTokenizer *p = (AsciiTokenizer*)pTokenizer;
185713 int rc = SQLITE_OK;
@@ -184964,10 +185716,12 @@
185716
185717 char aFold[64];
185718 int nFold = sizeof(aFold);
185719 char *pFold = aFold;
185720 unsigned char *a = p->aTokenChar;
185721
185722 UNUSED_PARAM(iUnused);
185723
185724 while( is<nText && rc==SQLITE_OK ){
185725 int nByte;
185726
185727 /* Skip any leading divider characters. */
@@ -185158,16 +185912,18 @@
185912
185913 /*
185914 ** Create a "unicode61" tokenizer.
185915 */
185916 static int fts5UnicodeCreate(
185917 void *pUnused,
185918 const char **azArg, int nArg,
185919 Fts5Tokenizer **ppOut
185920 ){
185921 int rc = SQLITE_OK; /* Return code */
185922 Unicode61Tokenizer *p = 0; /* New tokenizer object */
185923
185924 UNUSED_PARAM(pUnused);
185925
185926 if( nArg%2 ){
185927 rc = SQLITE_ERROR;
185928 }else{
185929 p = (Unicode61Tokenizer*)sqlite3_malloc(sizeof(Unicode61Tokenizer));
@@ -185221,11 +185977,11 @@
185977 }
185978
185979 static int fts5UnicodeTokenize(
185980 Fts5Tokenizer *pTokenizer,
185981 void *pCtx,
185982 int iUnused,
185983 const char *pText, int nText,
185984 int (*xToken)(void*, int, const char*, int nToken, int iStart, int iEnd)
185985 ){
185986 Unicode61Tokenizer *p = (Unicode61Tokenizer*)pTokenizer;
185987 int rc = SQLITE_OK;
@@ -185236,10 +185992,12 @@
185992
185993 /* Output buffer */
185994 char *aFold = p->aFold;
185995 int nFold = p->nFold;
185996 const char *pEnd = &aFold[nFold-6];
185997
185998 UNUSED_PARAM(iUnused);
185999
186000 /* Each iteration of this loop gobbles up a contiguous run of separators,
186001 ** then the next token. */
186002 while( rc==SQLITE_OK ){
186003 int iCode; /* non-ASCII codepoint read from input */
@@ -186055,11 +186813,11 @@
186813 };
186814
186815 int rc = SQLITE_OK; /* Return code */
186816 int i; /* To iterate through builtin functions */
186817
186818 for(i=0; rc==SQLITE_OK && i<ArraySize(aBuiltin); i++){
186819 rc = pApi->xCreateTokenizer(pApi,
186820 aBuiltin[i].zName,
186821 (void*)pApi,
186822 &aBuiltin[i].x,
186823 0
@@ -186196,13 +186954,13 @@
186954 };
186955 static const unsigned int aAscii[4] = {
186956 0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
186957 };
186958
186959 if( (unsigned int)c<128 ){
186960 return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
186961 }else if( (unsigned int)c<(1<<22) ){
186962 unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
186963 int iRes = 0;
186964 int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
186965 int iLo = 0;
186966 while( iHi>=iLo ){
@@ -186765,11 +187523,14 @@
187523 return fts5PutVarint64(p,v);
187524 }
187525
187526
187527 static int sqlite3Fts5GetVarintLen(u32 iVal){
187528 #if 0
187529 if( iVal<(1 << 7 ) ) return 1;
187530 #endif
187531 assert( iVal>=(1 << 7) );
187532 if( iVal<(1 << 14) ) return 2;
187533 if( iVal<(1 << 21) ) return 3;
187534 if( iVal<(1 << 28) ) return 4;
187535 return 5;
187536 }
@@ -186959,11 +187720,11 @@
187720 int nTab = (int)strlen(zTab)+1;
187721 int eType = 0;
187722
187723 rc = fts5VocabTableType(zType, pzErr, &eType);
187724 if( rc==SQLITE_OK ){
187725 assert( eType>=0 && eType<ArraySize(azSchema) );
187726 rc = sqlite3_declare_vtab(db, azSchema[eType]);
187727 }
187728
187729 nByte = sizeof(Fts5VocabTable) + nDb + nTab;
187730 pRet = sqlite3Fts5MallocZero(&rc, nByte);
@@ -187012,19 +187773,21 @@
187773
187774 /*
187775 ** Implementation of the xBestIndex method.
187776 */
187777 static int fts5VocabBestIndexMethod(
187778 sqlite3_vtab *pUnused,
187779 sqlite3_index_info *pInfo
187780 ){
187781 int i;
187782 int iTermEq = -1;
187783 int iTermGe = -1;
187784 int iTermLe = -1;
187785 int idxNum = 0;
187786 int nArg = 0;
187787
187788 UNUSED_PARAM(pUnused);
187789
187790 for(i=0; i<pInfo->nConstraint; i++){
187791 struct sqlite3_index_constraint *p = &pInfo->aConstraint[i];
187792 if( p->usable==0 ) continue;
187793 if( p->iColumn==0 ){ /* term column */
@@ -187182,59 +187945,54 @@
187945 memset(pCsr->aDoc, 0, nCol * sizeof(i64));
187946 pCsr->iCol = 0;
187947
187948 assert( pTab->eType==FTS5_VOCAB_COL || pTab->eType==FTS5_VOCAB_ROW );
187949 while( rc==SQLITE_OK ){
 
187950 const u8 *pPos; int nPos; /* Position list */
187951 i64 iPos = 0; /* 64-bit position read from poslist */
187952 int iOff = 0; /* Current offset within position list */
187953
187954 pPos = pCsr->pIter->pData;
187955 nPos = pCsr->pIter->nData;
187956 switch( pCsr->pConfig->eDetail ){
187957 case FTS5_DETAIL_FULL:
187958 pPos = pCsr->pIter->pData;
187959 nPos = pCsr->pIter->nData;
187960 if( pTab->eType==FTS5_VOCAB_ROW ){
187961 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187962 pCsr->aCnt[0]++;
187963 }
187964 pCsr->aDoc[0]++;
187965 }else{
187966 int iCol = -1;
187967 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff, &iPos) ){
187968 int ii = FTS5_POS2COLUMN(iPos);
187969 pCsr->aCnt[ii]++;
187970 if( iCol!=ii ){
187971 if( ii>=nCol ){
187972 rc = FTS5_CORRUPT;
187973 break;
187974 }
187975 pCsr->aDoc[ii]++;
187976 iCol = ii;
 
187977 }
187978 }
187979 }
187980 break;
187981
187982 case FTS5_DETAIL_COLUMNS:
187983 if( pTab->eType==FTS5_VOCAB_ROW ){
187984 pCsr->aDoc[0]++;
187985 }else{
187986 while( 0==sqlite3Fts5PoslistNext64(pPos, nPos, &iOff,&iPos) ){
187987 assert_nc( iPos>=0 && iPos<nCol );
187988 if( iPos>=nCol ){
187989 rc = FTS5_CORRUPT;
187990 break;
187991 }
187992 pCsr->aDoc[iPos]++;
187993 }
 
 
 
 
 
187994 }
187995 break;
187996
187997 default:
187998 assert( pCsr->pConfig->eDetail==FTS5_DETAIL_NONE );
@@ -187268,12 +188026,12 @@
188026 ** This is the xFilter implementation for the virtual table.
188027 */
188028 static int fts5VocabFilterMethod(
188029 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
188030 int idxNum, /* Strategy index */
188031 const char *zUnused, /* Unused */
188032 int nUnused, /* Number of elements in apVal */
188033 sqlite3_value **apVal /* Arguments for the indexing scheme */
188034 ){
188035 Fts5VocabCursor *pCsr = (Fts5VocabCursor*)pCursor;
188036 int rc = SQLITE_OK;
188037
@@ -187283,10 +188041,12 @@
188041 int nTerm = 0;
188042
188043 sqlite3_value *pEq = 0;
188044 sqlite3_value *pGe = 0;
188045 sqlite3_value *pLe = 0;
188046
188047 UNUSED_PARAM2(zUnused, nUnused);
188048
188049 fts5VocabResetCursor(pCsr);
188050 if( idxNum & FTS5_VOCAB_TERM_EQ ) pEq = apVal[iVal++];
188051 if( idxNum & FTS5_VOCAB_TERM_GE ) pGe = apVal[iVal++];
188052 if( idxNum & FTS5_VOCAB_TERM_LE ) pLe = apVal[iVal++];
188053
+3 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111111
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112112
** [sqlite_version()] and [sqlite_source_id()].
113113
*/
114114
#define SQLITE_VERSION "3.11.0"
115115
#define SQLITE_VERSION_NUMBER 3011000
116
-#define SQLITE_SOURCE_ID "2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1"
116
+#define SQLITE_SOURCE_ID "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
117117
118118
/*
119119
** CAPI3REF: Run-Time Library Version Numbers
120120
** KEYWORDS: sqlite3_version, sqlite3_sourceid
121121
**
@@ -345,11 +345,11 @@
345345
** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
346346
** is not NULL then any error message is written into memory obtained
347347
** from [sqlite3_malloc()] and passed back through the 5th parameter.
348348
** To avoid memory leaks, the application should invoke [sqlite3_free()]
349349
** on error message strings returned through the 5th parameter of
350
-** of sqlite3_exec() after the error message string is no longer needed.
350
+** sqlite3_exec() after the error message string is no longer needed.
351351
** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
352352
** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
353353
** NULL before returning.
354354
**
355355
** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5695,11 +5695,11 @@
56955695
*/
56965696
struct sqlite3_index_info {
56975697
/* Inputs */
56985698
int nConstraint; /* Number of entries in aConstraint */
56995699
struct sqlite3_index_constraint {
5700
- int iColumn; /* Column on left-hand side of constraint */
5700
+ int iColumn; /* Column constrained. -1 for ROWID */
57015701
unsigned char op; /* Constraint operator */
57025702
unsigned char usable; /* True if this constraint is usable */
57035703
int iTermOffset; /* Used internally - xBestIndex should ignore */
57045704
} *aConstraint; /* Table of WHERE clause constraints */
57055705
int nOrderBy; /* Number of terms in the ORDER BY clause */
57065706
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.11.0"
115 #define SQLITE_VERSION_NUMBER 3011000
116 #define SQLITE_SOURCE_ID "2016-01-20 14:22:41 204432ee72fda8e82d244c4aa18de7ec4811b8e1"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -345,11 +345,11 @@
345 ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
346 ** is not NULL then any error message is written into memory obtained
347 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
348 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
349 ** on error message strings returned through the 5th parameter of
350 ** of sqlite3_exec() after the error message string is no longer needed.
351 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
352 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
353 ** NULL before returning.
354 **
355 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5695,11 +5695,11 @@
5695 */
5696 struct sqlite3_index_info {
5697 /* Inputs */
5698 int nConstraint; /* Number of entries in aConstraint */
5699 struct sqlite3_index_constraint {
5700 int iColumn; /* Column on left-hand side of constraint */
5701 unsigned char op; /* Constraint operator */
5702 unsigned char usable; /* True if this constraint is usable */
5703 int iTermOffset; /* Used internally - xBestIndex should ignore */
5704 } *aConstraint; /* Table of WHERE clause constraints */
5705 int nOrderBy; /* Number of terms in the ORDER BY clause */
5706
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -111,11 +111,11 @@
111 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
112 ** [sqlite_version()] and [sqlite_source_id()].
113 */
114 #define SQLITE_VERSION "3.11.0"
115 #define SQLITE_VERSION_NUMBER 3011000
116 #define SQLITE_SOURCE_ID "2016-02-15 17:29:24 3d862f207e3adc00f78066799ac5a8c282430a5f"
117
118 /*
119 ** CAPI3REF: Run-Time Library Version Numbers
120 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
121 **
@@ -345,11 +345,11 @@
345 ** subsequent statements are skipped. ^If the 5th parameter to sqlite3_exec()
346 ** is not NULL then any error message is written into memory obtained
347 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
348 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
349 ** on error message strings returned through the 5th parameter of
350 ** sqlite3_exec() after the error message string is no longer needed.
351 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
352 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
353 ** NULL before returning.
354 **
355 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
@@ -5695,11 +5695,11 @@
5695 */
5696 struct sqlite3_index_info {
5697 /* Inputs */
5698 int nConstraint; /* Number of entries in aConstraint */
5699 struct sqlite3_index_constraint {
5700 int iColumn; /* Column constrained. -1 for ROWID */
5701 unsigned char op; /* Constraint operator */
5702 unsigned char usable; /* True if this constraint is usable */
5703 int iTermOffset; /* Used internally - xBestIndex should ignore */
5704 } *aConstraint; /* Table of WHERE clause constraints */
5705 int nOrderBy; /* Number of terms in the ORDER BY clause */
5706
+1 -1
--- src/stash.c
+++ src/stash.c
@@ -478,11 +478,11 @@
478478
const char *zCmd;
479479
int nCmd;
480480
int stashid = 0;
481481
undo_capture_command_line();
482482
db_must_be_within_tree();
483
- db_open_config(0);
483
+ db_open_config(0, 0);
484484
db_begin_transaction();
485485
zDb = db_name("localdb");
486486
db_multi_exec(zStashInit /*works-like:"%w,%w"*/, zDb, zDb);
487487
if( g.argc<=2 ){
488488
zCmd = "save";
489489
--- src/stash.c
+++ src/stash.c
@@ -478,11 +478,11 @@
478 const char *zCmd;
479 int nCmd;
480 int stashid = 0;
481 undo_capture_command_line();
482 db_must_be_within_tree();
483 db_open_config(0);
484 db_begin_transaction();
485 zDb = db_name("localdb");
486 db_multi_exec(zStashInit /*works-like:"%w,%w"*/, zDb, zDb);
487 if( g.argc<=2 ){
488 zCmd = "save";
489
--- src/stash.c
+++ src/stash.c
@@ -478,11 +478,11 @@
478 const char *zCmd;
479 int nCmd;
480 int stashid = 0;
481 undo_capture_command_line();
482 db_must_be_within_tree();
483 db_open_config(0, 0);
484 db_begin_transaction();
485 zDb = db_name("localdb");
486 db_multi_exec(zStashInit /*works-like:"%w,%w"*/, zDb, zDb);
487 if( g.argc<=2 ){
488 zCmd = "save";
489
+1 -1
--- src/sync.c
+++ src/sync.c
@@ -128,11 +128,11 @@
128128
*pSyncFlags |= SYNC_RESYNC;
129129
}
130130
url_proxy_options();
131131
clone_ssh_find_options();
132132
db_find_and_open_repository(0, 0);
133
- db_open_config(0);
133
+ db_open_config(0, 0);
134134
if( g.argc==2 ){
135135
if( db_get_boolean("auto-shun",1) ) configSync = CONFIGSET_SHUN;
136136
}else if( g.argc==3 ){
137137
zUrl = g.argv[2];
138138
}
139139
--- src/sync.c
+++ src/sync.c
@@ -128,11 +128,11 @@
128 *pSyncFlags |= SYNC_RESYNC;
129 }
130 url_proxy_options();
131 clone_ssh_find_options();
132 db_find_and_open_repository(0, 0);
133 db_open_config(0);
134 if( g.argc==2 ){
135 if( db_get_boolean("auto-shun",1) ) configSync = CONFIGSET_SHUN;
136 }else if( g.argc==3 ){
137 zUrl = g.argv[2];
138 }
139
--- src/sync.c
+++ src/sync.c
@@ -128,11 +128,11 @@
128 *pSyncFlags |= SYNC_RESYNC;
129 }
130 url_proxy_options();
131 clone_ssh_find_options();
132 db_find_and_open_repository(0, 0);
133 db_open_config(0, 0);
134 if( g.argc==2 ){
135 if( db_get_boolean("auto-shun",1) ) configSync = CONFIGSET_SHUN;
136 }else if( g.argc==3 ){
137 zUrl = g.argv[2];
138 }
139
+1 -1
--- src/th_main.c
+++ src/th_main.c
@@ -1766,11 +1766,11 @@
17661766
}else{
17671767
g.th1Flags &= ~TH_STATE_REPOSITORY;
17681768
}
17691769
}
17701770
if( !Th_IsConfigOpen() ){
1771
- db_open_config(0);
1771
+ db_open_config(0, 1);
17721772
if( Th_IsConfigOpen() ){
17731773
g.th1Flags |= TH_STATE_CONFIG;
17741774
}else{
17751775
g.th1Flags &= ~TH_STATE_CONFIG;
17761776
}
17771777
--- src/th_main.c
+++ src/th_main.c
@@ -1766,11 +1766,11 @@
1766 }else{
1767 g.th1Flags &= ~TH_STATE_REPOSITORY;
1768 }
1769 }
1770 if( !Th_IsConfigOpen() ){
1771 db_open_config(0);
1772 if( Th_IsConfigOpen() ){
1773 g.th1Flags |= TH_STATE_CONFIG;
1774 }else{
1775 g.th1Flags &= ~TH_STATE_CONFIG;
1776 }
1777
--- src/th_main.c
+++ src/th_main.c
@@ -1766,11 +1766,11 @@
1766 }else{
1767 g.th1Flags &= ~TH_STATE_REPOSITORY;
1768 }
1769 }
1770 if( !Th_IsConfigOpen() ){
1771 db_open_config(0, 1);
1772 if( Th_IsConfigOpen() ){
1773 g.th1Flags |= TH_STATE_CONFIG;
1774 }else{
1775 g.th1Flags &= ~TH_STATE_CONFIG;
1776 }
1777
+4 -4
--- test/amend.test
+++ test/amend.test
@@ -221,11 +221,11 @@
221221
}
222222
set sc 0
223223
foreach badformat $badformats {
224224
incr sc
225225
set datetime [clock format $timestamp -format $badformat -gmt 1]
226
- fossil amend $UUIDINIT -date $datetime
226
+ fossil amend $UUIDINIT -date $datetime -expectError
227227
test amend-date-2.$sc {[string first "YYYY-MM-DD HH:MM:SS" $RESULT] != -1}
228228
}
229229
230230
########################################
231231
# Test: -hide #
@@ -261,11 +261,11 @@
261261
fossil tag ls --raw $UUIDC
262262
test amend-close-1.2 {[string first "closed" $RESULT] != -1}
263263
fossil timeline -n 1
264264
test amend-close-1.3 {[string match {*Marked*"Closed".*} $RESULT]}
265265
write_file datafile "cllf"
266
-fossil commit -m "should fail"
266
+fossil commit -m "should fail" -expectError
267267
test amend-close-2 {[string first "closed leaf" $RESULT] != -1}
268268
269269
set UUID3 UUID3
270270
fossil revert
271271
fossil update trunk
@@ -285,11 +285,11 @@
285285
fossil timeline -n 1
286286
test amend-close-3.3 {
287287
[string match "*Add*propagating*\"closed\".*" $RESULT]
288288
}
289289
write_file datafile "changed"
290
-fossil commit -m "should fail"
290
+fossil commit -m "should fail" -expectError
291291
test amend-close-3.4 {[string first "closed leaf" $RESULT] != -1}
292292
293293
########################################
294294
# Test: -tag/-cancel #
295295
########################################
@@ -399,7 +399,7 @@
399399
}
400400
401401
########################################
402402
# Test: NULL UUID #
403403
########################################
404
-fossil amend {} -close
404
+fossil amend {} -close -expectError
405405
test amend-null-uuid {$CODE && [string first "no such check-in" $RESULT] != -1}
406406
--- test/amend.test
+++ test/amend.test
@@ -221,11 +221,11 @@
221 }
222 set sc 0
223 foreach badformat $badformats {
224 incr sc
225 set datetime [clock format $timestamp -format $badformat -gmt 1]
226 fossil amend $UUIDINIT -date $datetime
227 test amend-date-2.$sc {[string first "YYYY-MM-DD HH:MM:SS" $RESULT] != -1}
228 }
229
230 ########################################
231 # Test: -hide #
@@ -261,11 +261,11 @@
261 fossil tag ls --raw $UUIDC
262 test amend-close-1.2 {[string first "closed" $RESULT] != -1}
263 fossil timeline -n 1
264 test amend-close-1.3 {[string match {*Marked*"Closed".*} $RESULT]}
265 write_file datafile "cllf"
266 fossil commit -m "should fail"
267 test amend-close-2 {[string first "closed leaf" $RESULT] != -1}
268
269 set UUID3 UUID3
270 fossil revert
271 fossil update trunk
@@ -285,11 +285,11 @@
285 fossil timeline -n 1
286 test amend-close-3.3 {
287 [string match "*Add*propagating*\"closed\".*" $RESULT]
288 }
289 write_file datafile "changed"
290 fossil commit -m "should fail"
291 test amend-close-3.4 {[string first "closed leaf" $RESULT] != -1}
292
293 ########################################
294 # Test: -tag/-cancel #
295 ########################################
@@ -399,7 +399,7 @@
399 }
400
401 ########################################
402 # Test: NULL UUID #
403 ########################################
404 fossil amend {} -close
405 test amend-null-uuid {$CODE && [string first "no such check-in" $RESULT] != -1}
406
--- test/amend.test
+++ test/amend.test
@@ -221,11 +221,11 @@
221 }
222 set sc 0
223 foreach badformat $badformats {
224 incr sc
225 set datetime [clock format $timestamp -format $badformat -gmt 1]
226 fossil amend $UUIDINIT -date $datetime -expectError
227 test amend-date-2.$sc {[string first "YYYY-MM-DD HH:MM:SS" $RESULT] != -1}
228 }
229
230 ########################################
231 # Test: -hide #
@@ -261,11 +261,11 @@
261 fossil tag ls --raw $UUIDC
262 test amend-close-1.2 {[string first "closed" $RESULT] != -1}
263 fossil timeline -n 1
264 test amend-close-1.3 {[string match {*Marked*"Closed".*} $RESULT]}
265 write_file datafile "cllf"
266 fossil commit -m "should fail" -expectError
267 test amend-close-2 {[string first "closed leaf" $RESULT] != -1}
268
269 set UUID3 UUID3
270 fossil revert
271 fossil update trunk
@@ -285,11 +285,11 @@
285 fossil timeline -n 1
286 test amend-close-3.3 {
287 [string match "*Add*propagating*\"closed\".*" $RESULT]
288 }
289 write_file datafile "changed"
290 fossil commit -m "should fail" -expectError
291 test amend-close-3.4 {[string first "closed leaf" $RESULT] != -1}
292
293 ########################################
294 # Test: -tag/-cancel #
295 ########################################
@@ -399,7 +399,7 @@
399 }
400
401 ########################################
402 # Test: NULL UUID #
403 ########################################
404 fossil amend {} -close -expectError
405 test amend-null-uuid {$CODE && [string first "no such check-in" $RESULT] != -1}
406
+3 -3
--- test/clean.test
+++ test/clean.test
@@ -136,11 +136,11 @@
136136
test clean-22 {[normalize_result] eq {f2
137137
f4}}
138138
139139
###############################################################################
140140
141
-fossil undo
141
+fossil undo -expectError
142142
test clean-23 {[normalize_result] eq {nothing to undo}}
143143
144144
###############################################################################
145145
146146
# clean w/undo disabled, force, 1 file < 10MiB, 1 file > 10MiB
@@ -152,11 +152,11 @@
152152
fossil extra
153153
test clean-25 {[normalize_result] eq {}}
154154
155155
###############################################################################
156156
157
-fossil undo
157
+fossil undo -expectError
158158
test clean-26 {[normalize_result] eq {nothing to undo}}
159159
160160
###############################################################################
161161
162162
write_file f5 "f5 line"
@@ -178,12 +178,12 @@
178178
fossil extra
179179
test clean-29 {[normalize_result] eq {}}
180180
181181
###############################################################################
182182
183
-fossil undo
183
+fossil undo -expectError
184184
test clean-30 {[normalize_result] eq {nothing to undo}}
185185
186186
###############################################################################
187187
188188
fossil extra
189189
test clean-31 {[normalize_result] eq {}}
190190
191191
ADDED test/json.test
--- test/clean.test
+++ test/clean.test
@@ -136,11 +136,11 @@
136 test clean-22 {[normalize_result] eq {f2
137 f4}}
138
139 ###############################################################################
140
141 fossil undo
142 test clean-23 {[normalize_result] eq {nothing to undo}}
143
144 ###############################################################################
145
146 # clean w/undo disabled, force, 1 file < 10MiB, 1 file > 10MiB
@@ -152,11 +152,11 @@
152 fossil extra
153 test clean-25 {[normalize_result] eq {}}
154
155 ###############################################################################
156
157 fossil undo
158 test clean-26 {[normalize_result] eq {nothing to undo}}
159
160 ###############################################################################
161
162 write_file f5 "f5 line"
@@ -178,12 +178,12 @@
178 fossil extra
179 test clean-29 {[normalize_result] eq {}}
180
181 ###############################################################################
182
183 fossil undo
184 test clean-30 {[normalize_result] eq {nothing to undo}}
185
186 ###############################################################################
187
188 fossil extra
189 test clean-31 {[normalize_result] eq {}}
190
191 DDED test/json.test
--- test/clean.test
+++ test/clean.test
@@ -136,11 +136,11 @@
136 test clean-22 {[normalize_result] eq {f2
137 f4}}
138
139 ###############################################################################
140
141 fossil undo -expectError
142 test clean-23 {[normalize_result] eq {nothing to undo}}
143
144 ###############################################################################
145
146 # clean w/undo disabled, force, 1 file < 10MiB, 1 file > 10MiB
@@ -152,11 +152,11 @@
152 fossil extra
153 test clean-25 {[normalize_result] eq {}}
154
155 ###############################################################################
156
157 fossil undo -expectError
158 test clean-26 {[normalize_result] eq {nothing to undo}}
159
160 ###############################################################################
161
162 write_file f5 "f5 line"
@@ -178,12 +178,12 @@
178 fossil extra
179 test clean-29 {[normalize_result] eq {}}
180
181 ###############################################################################
182
183 fossil undo -expectError
184 test clean-30 {[normalize_result] eq {nothing to undo}}
185
186 ###############################################################################
187
188 fossil extra
189 test clean-31 {[normalize_result] eq {}}
190
191 DDED test/json.test
--- a/test/json.test
+++ b/test/json.test
@@ -0,0 +1,16 @@
1
+#
2
+env-RC-1102env#write_file bad.sql {
3
+##}
4
+##1102#
5
+# Co#. What about Win?timeline/checkin $U1Cookie1 {$CODE =11http-timeline#
6
+# Cotimeline/checkin $U1Cookie knownBughttp-timeline2
7
+#
8
+# Co
9
+env-RC-1102env#write_file bad.sql {
10
+##}
11
+##1102#
12
+# Co#. What about Win?timeline/checkin $U1Cookie1 {$CODE =11http-timeline#
13
+# Cotimeline/checkin $U1Cookie knownBughttp-timeline2
14
+#
15
+# CoC}
16
+$::RESULT; ; repo_initrepo_init
--- a/test/json.test
+++ b/test/json.test
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
--- a/test/json.test
+++ b/test/json.test
@@ -0,0 +1,16 @@
1 #
2 env-RC-1102env#write_file bad.sql {
3 ##}
4 ##1102#
5 # Co#. What about Win?timeline/checkin $U1Cookie1 {$CODE =11http-timeline#
6 # Cotimeline/checkin $U1Cookie knownBughttp-timeline2
7 #
8 # Co
9 env-RC-1102env#write_file bad.sql {
10 ##}
11 ##1102#
12 # Co#. What about Win?timeline/checkin $U1Cookie1 {$CODE =11http-timeline#
13 # Cotimeline/checkin $U1Cookie knownBughttp-timeline2
14 #
15 # CoC}
16 $::RESULT; ; repo_initrepo_init
--- test/merge5.test
+++ test/merge5.test
@@ -222,11 +222,11 @@
222222
35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
223223
da5c8346496f3421cb58f84b6e59e9531d9d424d one.txt
224224
ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
225225
278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
226226
}
227
-fossil merge br4
227
+fossil merge br4 -expectError
228228
checkout-test 121 {
229229
35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
230230
ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
231231
278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
232232
}
@@ -235,11 +235,11 @@
235235
checkout-test 122 {
236236
6e167b139c294bed560e2e30b352361b101e1f39 four.txt
237237
ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
238238
278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
239239
}
240
-fossil merge br1
240
+fossil merge br1 -expectError
241241
checkout-test 123 {
242242
6e167b139c294bed560e2e30b352361b101e1f39 four.txt
243243
ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
244244
278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
245245
}
246246
--- test/merge5.test
+++ test/merge5.test
@@ -222,11 +222,11 @@
222 35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
223 da5c8346496f3421cb58f84b6e59e9531d9d424d one.txt
224 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
225 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
226 }
227 fossil merge br4
228 checkout-test 121 {
229 35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
230 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
231 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
232 }
@@ -235,11 +235,11 @@
235 checkout-test 122 {
236 6e167b139c294bed560e2e30b352361b101e1f39 four.txt
237 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
238 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
239 }
240 fossil merge br1
241 checkout-test 123 {
242 6e167b139c294bed560e2e30b352361b101e1f39 four.txt
243 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
244 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
245 }
246
--- test/merge5.test
+++ test/merge5.test
@@ -222,11 +222,11 @@
222 35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
223 da5c8346496f3421cb58f84b6e59e9531d9d424d one.txt
224 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
225 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
226 }
227 fossil merge br4 -expectError
228 checkout-test 121 {
229 35815cf5804e8933eab64ae34e00bbb381be72c5 four.txt
230 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
231 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
232 }
@@ -235,11 +235,11 @@
235 checkout-test 122 {
236 6e167b139c294bed560e2e30b352361b101e1f39 four.txt
237 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
238 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
239 }
240 fossil merge br1 -expectError
241 checkout-test 123 {
242 6e167b139c294bed560e2e30b352361b101e1f39 four.txt
243 ed24d19d726d173f18dbf4a9a0f8514daa3e3ca4 three.txt
244 278a402316510f6ae4a77186796a6bde78c7dbc1 two.txt
245 }
246
--- test/revert.test
+++ test/revert.test
@@ -176,5 +176,20 @@
176176
177177
revert-test 2-1 {} {
178178
REVERT f1
179179
DELETE f1n
180180
} -exists {f1} -notexists {f1n}
181
+
182
+
183
+# Test reverting a rename in the repo but not completed in the file
184
+# system
185
+repo_init
186
+write_file f1 "f1"
187
+fossil add f1
188
+fossil commit -m "add f1"
189
+fossil mv --soft f1 f1new
190
+test 3-mv-1 {[file exists f1]}
191
+test 3-mv-2 {![file exists f1new]}
192
+revert-test 3-1 {} {
193
+ REVERT f1
194
+ DELETE f1new
195
+} -exists {f1} -notexists {f1n}
181196
182197
ADDED test/stash.test
--- test/revert.test
+++ test/revert.test
@@ -176,5 +176,20 @@
176
177 revert-test 2-1 {} {
178 REVERT f1
179 DELETE f1n
180 } -exists {f1} -notexists {f1n}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
182 DDED test/stash.test
--- test/revert.test
+++ test/revert.test
@@ -176,5 +176,20 @@
176
177 revert-test 2-1 {} {
178 REVERT f1
179 DELETE f1n
180 } -exists {f1} -notexists {f1n}
181
182
183 # Test reverting a rename in the repo but not completed in the file
184 # system
185 repo_init
186 write_file f1 "f1"
187 fossil add f1
188 fossil commit -m "add f1"
189 fossil mv --soft f1 f1new
190 test 3-mv-1 {[file exists f1]}
191 test 3-mv-2 {![file exists f1new]}
192 revert-test 3-1 {} {
193 REVERT f1
194 DELETE f1new
195 } -exists {f1} -notexists {f1n}
196
197 DDED test/stash.test
--- a/test/stash.test
+++ b/test/stash.test
@@ -0,0 +1,130 @@
1
+f1
2
+@@ -1,1 +0,0 @@
3
+-f1
4
+
5
+CHANGED f2
6
+--- f2
++++ f2
7
+@@ -1,1 +1,1 @@
8
+-f2
9
++f2.1
10
+
11
+CHANGED f3n
12
+--- f3n
++++ f3n
13
+
14
+ADDED f0
15
+Index: f0
16
+==================================================================
17
+--- f0
++++ f0
18
+@@ -0,0 +1,1 @@
19
++f0}
20
+
21
+########
22
+# fossil stash show|cat ?STASHID? ?DIFF-OPTIONS?
23
+# fossil stash [g]diff ?STASHID? ?DIFF-OPTIONS?
24
+
25
+fossil stash show
26
+test stash- Should be triggered by this s$diff_stash_ts {f0 f2 f3} -notexists {f1}
27
+fo1-diff {[normaliz knownBug+++ f0
28
+@@ -0,0 +1,1 @@
29
++f0pop
30
+
31
+stash-test 2 pop {
32
+ DELETE f1
33
+ UPDATE f2
34
+ UPDATE f3n
35
+ ADDED f0
36
+} -changes {
37
+ ADDED f0
38
+ MISSING f 1
39
+ EDITED f2
40
+ RENAMED f3 -> f3n
41
+} -addremove {
42
+ DELETED f1
43
+} -exists {f0 f2 f3n} -notexists {f1 f3}
44
+
45
+# Confirm there is no longer a stash saved
46
+fossil stash list
47
+test stash-2-list {[first_data_line] eq "empty stash"}
48
+
49
+
50
+# Test stashed mv without touching the file system
51
+# Issue reported by email to fossil-users
52
+# from Warren Young, dated Tue, 9 Feb 2016 01:22:54 -0700
53
+# with checkin [b8c7af5bd9] plus a local patch on CentOS 5
54
+# 64 bit intel, 8-byte pointer, 4-byte integer
55
+# Stashed renamed file said:
56
+# fossil: ./src/delta.c:231: checksum: Assertion '...' failed.
57
+# Should be triggered by this stash-WY-1 test.
58
+fossil checkout --force c1
59
+fossil clean REVERT f1
60
+ {
61
+ RENAMED f2 -> f2n
62
+} -a
63
+# fossil: ./src/f1newsf3 -knownbugs {-changescatch {exec $::fossilexe info} res
64
+if {![regexp {use --repository} $res]} {
65
+ puts stderr "Cannot run this test within an open checkout"
66
+ return
67
+}
68
+
69
+repo_init a 0,0 @@
70
+-f1
71
+
72
+CHANGED f2
73
+--- f2
++++ f2
74
+@@ -1,1 +1,1 @@
75
+-f2
76
++f2.1
77
+
78
+CHANGED f3n
79
+--- f3n
++++ f3n
80
+
81
+ADDED f0
82
+Index: f0
83
+==================================================================
84
+--- f0
++++ f0
85
+@@ -0,0 +1,1 @@
86
++f0}
87
+
88
+########
89
+# fossil stash show|cat ?STASHID? ?DIFF-OPTIONS?
90
+# fossil stash [g]diff ?STASHID? ?DIFF-OPTIONS?
91
+
92
+fossil stash show
93
+test stash- Sf1 checksum: Assertion '...' failed.
94
+# Should be triggered by this stash-WY-1 test.
95
+fossil checkout --force c1
96
+fossil clean REVERT f1
97
+ {
98
+ RENAMED f2 -> f2n
99
+} -a
100
+# fossil: ./src/f1newsf3 -known bugs {-changes}
101
+
102
+ MISSING f3 ADDED f3n
103
+ ADDED f2n
104
+ DELETED f2 DELETED f1repo_initrepo_init2
105
+--- f2
++++repo_init2
106
+--- f2
++++\
107
+ "RENAME f2 f2n\n MOVED_Frepo_init2
108
+--- f2
++++ f2
109
+@@ -1,1 +1,1 @@
110
+-f2
111
++f2.1
112
+
113
+CHANGED f3n
114
+--- f3n
++++ f3n
115
+
116
+ADDED f0
117
+Ind;#;#MISSING f3 ADDED f3n
118
+4-mv "mv --hard f2 f2n" {
119
+ RENAM ADDED f2n
120
+ DELETED f2
--- a/test/stash.test
+++ b/test/stash.test
@@ -0,0 +1,130 @@
 
 
 
 
 
 
++++ f2
 
 
 
 
 
 
++++ f3n
 
 
 
 
 
++++ f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
++++ f2
 
 
 
 
 
 
++++ f3n
 
 
 
 
 
++++ f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
++++repo_init2
 
++++\
 
 
++++ f2
 
 
 
 
 
 
++++ f3n
 
 
 
 
 
 
--- a/test/stash.test
+++ b/test/stash.test
@@ -0,0 +1,130 @@
1 f1
2 @@ -1,1 +0,0 @@
3 -f1
4
5 CHANGED f2
6 --- f2
++++ f2
7 @@ -1,1 +1,1 @@
8 -f2
9 +f2.1
10
11 CHANGED f3n
12 --- f3n
++++ f3n
13
14 ADDED f0
15 Index: f0
16 ==================================================================
17 --- f0
++++ f0
18 @@ -0,0 +1,1 @@
19 +f0}
20
21 ########
22 # fossil stash show|cat ?STASHID? ?DIFF-OPTIONS?
23 # fossil stash [g]diff ?STASHID? ?DIFF-OPTIONS?
24
25 fossil stash show
26 test stash- Should be triggered by this s$diff_stash_ts {f0 f2 f3} -notexists {f1}
27 fo1-diff {[normaliz knownBug+++ f0
28 @@ -0,0 +1,1 @@
29 +f0pop
30
31 stash-test 2 pop {
32 DELETE f1
33 UPDATE f2
34 UPDATE f3n
35 ADDED f0
36 } -changes {
37 ADDED f0
38 MISSING f 1
39 EDITED f2
40 RENAMED f3 -> f3n
41 } -addremove {
42 DELETED f1
43 } -exists {f0 f2 f3n} -notexists {f1 f3}
44
45 # Confirm there is no longer a stash saved
46 fossil stash list
47 test stash-2-list {[first_data_line] eq "empty stash"}
48
49
50 # Test stashed mv without touching the file system
51 # Issue reported by email to fossil-users
52 # from Warren Young, dated Tue, 9 Feb 2016 01:22:54 -0700
53 # with checkin [b8c7af5bd9] plus a local patch on CentOS 5
54 # 64 bit intel, 8-byte pointer, 4-byte integer
55 # Stashed renamed file said:
56 # fossil: ./src/delta.c:231: checksum: Assertion '...' failed.
57 # Should be triggered by this stash-WY-1 test.
58 fossil checkout --force c1
59 fossil clean REVERT f1
60 {
61 RENAMED f2 -> f2n
62 } -a
63 # fossil: ./src/f1newsf3 -knownbugs {-changescatch {exec $::fossilexe info} res
64 if {![regexp {use --repository} $res]} {
65 puts stderr "Cannot run this test within an open checkout"
66 return
67 }
68
69 repo_init a 0,0 @@
70 -f1
71
72 CHANGED f2
73 --- f2
++++ f2
74 @@ -1,1 +1,1 @@
75 -f2
76 +f2.1
77
78 CHANGED f3n
79 --- f3n
++++ f3n
80
81 ADDED f0
82 Index: f0
83 ==================================================================
84 --- f0
++++ f0
85 @@ -0,0 +1,1 @@
86 +f0}
87
88 ########
89 # fossil stash show|cat ?STASHID? ?DIFF-OPTIONS?
90 # fossil stash [g]diff ?STASHID? ?DIFF-OPTIONS?
91
92 fossil stash show
93 test stash- Sf1 checksum: Assertion '...' failed.
94 # Should be triggered by this stash-WY-1 test.
95 fossil checkout --force c1
96 fossil clean REVERT f1
97 {
98 RENAMED f2 -> f2n
99 } -a
100 # fossil: ./src/f1newsf3 -known bugs {-changes}
101
102 MISSING f3 ADDED f3n
103 ADDED f2n
104 DELETED f2 DELETED f1repo_initrepo_init2
105 --- f2
++++repo_init2
106 --- f2
++++\
107 "RENAME f2 f2n\n MOVED_Frepo_init2
108 --- f2
++++ f2
109 @@ -1,1 +1,1 @@
110 -f2
111 +f2.1
112
113 CHANGED f3n
114 --- f3n
++++ f3n
115
116 ADDED f0
117 Ind;#;#MISSING f3 ADDED f3n
118 4-mv "mv --hard f2 f2n" {
119 RENAM ADDED f2n
120 DELETED f2
+20 -5
--- test/tester.tcl
+++ test/tester.tcl
@@ -25,10 +25,16 @@
2525
2626
set testfiledir [file normalize [file dirname [info script]]]
2727
set testrundir [pwd]
2828
set testdir [file normalize [file dirname $argv0]]
2929
set fossilexe [file normalize [lindex $argv 0]]
30
+
31
+if {$tcl_platform(platform) eq "windows" && \
32
+ [string length [file extension $fossilexe]] == 0} {
33
+ append fossilexe .exe
34
+}
35
+
3036
set argv [lrange $argv 1 end]
3137
3238
set i [lsearch $argv -halt]
3339
if {$i>=0} {
3440
set HALT 1
@@ -121,27 +127,33 @@
121127
# test expressions.
122128
#
123129
proc fossil_maybe_answer {answer args} {
124130
global fossilexe
125131
set cmd $fossilexe
132
+ set expectError 0
133
+ if {[lindex $args end] eq "-expectError"} {
134
+ set expectError 1
135
+ set args [lrange $args 0 end-1]
136
+ }
126137
foreach a $args {
127138
lappend cmd $a
128139
}
129140
protOut $cmd
130141
131142
flush stdout
132143
if {[string length $answer] > 0} {
144
+ protOut $answer
133145
set prompt_file [file join $::tempPath fossil_prompt_answer]
134146
write_file $prompt_file $answer\n
135147
set rc [catch {eval exec $cmd <$prompt_file} result]
136148
file delete $prompt_file
137149
} else {
138150
set rc [catch {eval exec $cmd} result]
139151
}
140152
global RESULT CODE
141153
set CODE $rc
142
- if {$rc} {
154
+ if {($rc && !$expectError) || (!$rc && $expectError)} {
143155
protOut "ERROR: $result" 1
144156
} elseif {$::VERBOSE} {
145157
protOut "RESULT: $result"
146158
}
147159
set RESULT $result
@@ -185,13 +197,14 @@
185197
if {$::env(HOME) ne [pwd]} {
186198
catch {exec $::fossilexe info} res
187199
if {![regexp {use --repository} $res]} {
188200
error "In an open checkout: cannot initialize a new repository here."
189201
}
190
- # Fossil will write data on $HOME, running 'fossil new' here.
202
+ # Fossil will write data on $FOSSIL_HOME, running 'fossil new' here.
191203
# We need not to clutter the $HOME of the test caller.
192204
#
205
+ set ::env(FOSSIL_HOME) [pwd]
193206
set ::env(HOME) [pwd]
194207
}
195208
catch {exec $::fossilexe close -f}
196209
file delete $filename
197210
exec $::fossilexe new $filename
@@ -254,19 +267,19 @@
254267
return $normalized
255268
}
256269
257270
# Perform a test comparing two status lists
258271
#
259
-proc test_status_list {name result expected} {
272
+proc test_status_list {name result expected {constraints ""}} {
260273
set expected [normalize_status_list $expected]
261274
set result [normalize_status_list $result]
262275
if {$result eq $expected} {
263
- test $name 1
276
+ test $name 1 $constraints
264277
} else {
265278
protOut " Expected:\n [join $expected "\n "]" 1
266279
protOut " Got:\n [join $result "\n "]" 1
267
- test $name 0
280
+ test $name 0 $constraints
268281
}
269282
}
270283
271284
# Append all arguments into a single value and then returns it.
272285
#
@@ -491,10 +504,12 @@
491504
$env(TEMP) : [file dirname [info script]]}]
492505
493506
if {$tcl_platform(platform) eq "windows"} {
494507
set tempPath [string map [list \\ /] $tempPath]
495508
}
509
+
510
+set tempPath [file normalize $tempPath]
496511
497512
if {[catch {
498513
write_file [file join $tempPath temporary.txt] [clock seconds]
499514
} error] != 0} {
500515
error "could not write file to directory \"$tempPath\",\
501516
--- test/tester.tcl
+++ test/tester.tcl
@@ -25,10 +25,16 @@
25
26 set testfiledir [file normalize [file dirname [info script]]]
27 set testrundir [pwd]
28 set testdir [file normalize [file dirname $argv0]]
29 set fossilexe [file normalize [lindex $argv 0]]
 
 
 
 
 
 
30 set argv [lrange $argv 1 end]
31
32 set i [lsearch $argv -halt]
33 if {$i>=0} {
34 set HALT 1
@@ -121,27 +127,33 @@
121 # test expressions.
122 #
123 proc fossil_maybe_answer {answer args} {
124 global fossilexe
125 set cmd $fossilexe
 
 
 
 
 
126 foreach a $args {
127 lappend cmd $a
128 }
129 protOut $cmd
130
131 flush stdout
132 if {[string length $answer] > 0} {
 
133 set prompt_file [file join $::tempPath fossil_prompt_answer]
134 write_file $prompt_file $answer\n
135 set rc [catch {eval exec $cmd <$prompt_file} result]
136 file delete $prompt_file
137 } else {
138 set rc [catch {eval exec $cmd} result]
139 }
140 global RESULT CODE
141 set CODE $rc
142 if {$rc} {
143 protOut "ERROR: $result" 1
144 } elseif {$::VERBOSE} {
145 protOut "RESULT: $result"
146 }
147 set RESULT $result
@@ -185,13 +197,14 @@
185 if {$::env(HOME) ne [pwd]} {
186 catch {exec $::fossilexe info} res
187 if {![regexp {use --repository} $res]} {
188 error "In an open checkout: cannot initialize a new repository here."
189 }
190 # Fossil will write data on $HOME, running 'fossil new' here.
191 # We need not to clutter the $HOME of the test caller.
192 #
 
193 set ::env(HOME) [pwd]
194 }
195 catch {exec $::fossilexe close -f}
196 file delete $filename
197 exec $::fossilexe new $filename
@@ -254,19 +267,19 @@
254 return $normalized
255 }
256
257 # Perform a test comparing two status lists
258 #
259 proc test_status_list {name result expected} {
260 set expected [normalize_status_list $expected]
261 set result [normalize_status_list $result]
262 if {$result eq $expected} {
263 test $name 1
264 } else {
265 protOut " Expected:\n [join $expected "\n "]" 1
266 protOut " Got:\n [join $result "\n "]" 1
267 test $name 0
268 }
269 }
270
271 # Append all arguments into a single value and then returns it.
272 #
@@ -491,10 +504,12 @@
491 $env(TEMP) : [file dirname [info script]]}]
492
493 if {$tcl_platform(platform) eq "windows"} {
494 set tempPath [string map [list \\ /] $tempPath]
495 }
 
 
496
497 if {[catch {
498 write_file [file join $tempPath temporary.txt] [clock seconds]
499 } error] != 0} {
500 error "could not write file to directory \"$tempPath\",\
501
--- test/tester.tcl
+++ test/tester.tcl
@@ -25,10 +25,16 @@
25
26 set testfiledir [file normalize [file dirname [info script]]]
27 set testrundir [pwd]
28 set testdir [file normalize [file dirname $argv0]]
29 set fossilexe [file normalize [lindex $argv 0]]
30
31 if {$tcl_platform(platform) eq "windows" && \
32 [string length [file extension $fossilexe]] == 0} {
33 append fossilexe .exe
34 }
35
36 set argv [lrange $argv 1 end]
37
38 set i [lsearch $argv -halt]
39 if {$i>=0} {
40 set HALT 1
@@ -121,27 +127,33 @@
127 # test expressions.
128 #
129 proc fossil_maybe_answer {answer args} {
130 global fossilexe
131 set cmd $fossilexe
132 set expectError 0
133 if {[lindex $args end] eq "-expectError"} {
134 set expectError 1
135 set args [lrange $args 0 end-1]
136 }
137 foreach a $args {
138 lappend cmd $a
139 }
140 protOut $cmd
141
142 flush stdout
143 if {[string length $answer] > 0} {
144 protOut $answer
145 set prompt_file [file join $::tempPath fossil_prompt_answer]
146 write_file $prompt_file $answer\n
147 set rc [catch {eval exec $cmd <$prompt_file} result]
148 file delete $prompt_file
149 } else {
150 set rc [catch {eval exec $cmd} result]
151 }
152 global RESULT CODE
153 set CODE $rc
154 if {($rc && !$expectError) || (!$rc && $expectError)} {
155 protOut "ERROR: $result" 1
156 } elseif {$::VERBOSE} {
157 protOut "RESULT: $result"
158 }
159 set RESULT $result
@@ -185,13 +197,14 @@
197 if {$::env(HOME) ne [pwd]} {
198 catch {exec $::fossilexe info} res
199 if {![regexp {use --repository} $res]} {
200 error "In an open checkout: cannot initialize a new repository here."
201 }
202 # Fossil will write data on $FOSSIL_HOME, running 'fossil new' here.
203 # We need not to clutter the $HOME of the test caller.
204 #
205 set ::env(FOSSIL_HOME) [pwd]
206 set ::env(HOME) [pwd]
207 }
208 catch {exec $::fossilexe close -f}
209 file delete $filename
210 exec $::fossilexe new $filename
@@ -254,19 +267,19 @@
267 return $normalized
268 }
269
270 # Perform a test comparing two status lists
271 #
272 proc test_status_list {name result expected {constraints ""}} {
273 set expected [normalize_status_list $expected]
274 set result [normalize_status_list $result]
275 if {$result eq $expected} {
276 test $name 1 $constraints
277 } else {
278 protOut " Expected:\n [join $expected "\n "]" 1
279 protOut " Got:\n [join $result "\n "]" 1
280 test $name 0 $constraints
281 }
282 }
283
284 # Append all arguments into a single value and then returns it.
285 #
@@ -491,10 +504,12 @@
504 $env(TEMP) : [file dirname [info script]]}]
505
506 if {$tcl_platform(platform) eq "windows"} {
507 set tempPath [string map [list \\ /] $tempPath]
508 }
509
510 set tempPath [file normalize $tempPath]
511
512 if {[catch {
513 write_file [file join $tempPath temporary.txt] [clock seconds]
514 } error] != 0} {
515 error "could not write file to directory \"$tempPath\",\
516
+28 -30
--- test/th1.test
+++ test/th1.test
@@ -913,64 +913,62 @@
913913
test th1-reinitialize-2 {$RESULT ne ""}
914914
915915
###############################################################################
916916
917917
#
918
-# NOTE: This test may fail if the command names do not always come out in a
919
-# deterministic order from TH1.
918
+# NOTE: This test will fail if the command names are added to TH1, or
919
+# moved from Tcl builds to plain or the reverse. Sorting the
920
+# command lists eliminates a dependence on order.
920921
#
921922
fossil test-th-eval "info commands"
922
-
923
+set sorted_result [lsort $RESULT]
924
+protOut "Sorted: $sorted_result"
925
+set base_commands {anoncap anycap array artifact break breakpoint catch\
926
+ checkout combobox continue date decorate dir enable_output encode64\
927
+ error expr for getParameter glob_match globalState hascap hasfeature\
928
+ html htmlize http httpize if info insertCsrf lindex linecount list\
929
+ llength lsearch markdown proc puts query randhex redirect regexp\
930
+ reinitialize rename render repository return searchable set\
931
+ setParameter setting stime string styleFooter styleHeader tclReady\
932
+ trace unset uplevel upvar utime verifyCsrf wiki}
933
+set tcl_commands {tclEval tclExpr tclInvoke tclIsSafe tclMakeSafe}
923934
if {$th1Tcl} {
924
- test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
925
- enable_output uplevel dir http expr glob_match utime styleFooter encode64\
926
- catch if tclReady searchable reinitialize combobox lindex tclIsSafe query\
927
- html anoncap randhex llength for set break regexp markdown styleHeader\
928
- puts return checkout decorate artifact trace wiki proc tclInvoke hascap\
929
- globalState continue getParameter hasfeature setting lsearch breakpoint\
930
- upvar render repository string unset setParameter list error info rename\
931
- tclExpr array anycap tclEval httpize tclMakeSafe}}
935
+ test th1-info-commands-1 {$sorted_result eq [lsort "$base_commands $tcl_commands"]}
932936
} else {
933
- test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
934
- enable_output uplevel dir http expr glob_match utime styleFooter encode64\
935
- catch if tclReady searchable reinitialize combobox lindex query html\
936
- anoncap randhex llength for set break regexp markdown styleHeader puts\
937
- return checkout decorate artifact trace wiki proc hascap globalState\
938
- continue getParameter hasfeature setting lsearch breakpoint upvar render\
939
- repository string unset setParameter list error info rename array anycap\
940
- httpize}}
941
-}
937
+ test th1-info-commands-1 {$sorted_result eq [lsort "$base_commands"]}
938
+}
939
+
942940
943941
###############################################################################
944942
945943
fossil test-th-eval "info vars"
946944
947945
if {$th1Hooks} {
948
- test th1-info-vars-1 {$RESULT eq \
949
- "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
946
+ test th1-info-vars-1 {[lsort $RESULT] eq \
947
+ [lsort "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
950948
} else {
951949
test th1-info-vars-1 {$RESULT eq "tcl_platform"}
952950
}
953951
954952
###############################################################################
955953
956954
fossil test-th-eval "set x 1; info vars"
957955
958956
if {$th1Hooks} {
959
- test th1-info-vars-2 {$RESULT eq \
960
- "x th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
957
+ test th1-info-vars-2 {[lsort $RESULT] eq \
958
+ [lsort "x th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
961959
} else {
962
- test th1-info-vars-2 {$RESULT eq "x tcl_platform"}
960
+ test th1-info-vars-2 {[lsort $RESULT] eq [lsort "x tcl_platform"]}
963961
}
964962
965963
###############################################################################
966964
967965
fossil test-th-eval "set x 1; unset x; info vars"
968966
969967
if {$th1Hooks} {
970
- test th1-info-vars-3 {$RESULT eq \
971
- "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
968
+ test th1-info-vars-3 {[lsort $RESULT] eq \
969
+ [lsort "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
972970
} else {
973971
test th1-info-vars-3 {$RESULT eq "tcl_platform"}
974972
}
975973
976974
###############################################################################
@@ -981,14 +979,14 @@
981979
###############################################################################
982980
983981
fossil test-th-eval "set y 1; proc foo {} {set x 1; uplevel 1 {info vars}}; foo"
984982
985983
if {$th1Hooks} {
986
- test th1-info-vars-5 {$RESULT eq \
987
- "th_stack_trace y cmd_flags tcl_platform cmd_name cmd_args"}
984
+ test th1-info-vars-5 {[lsort $RESULT] eq \
985
+ [lsort "th_stack_trace y cmd_flags tcl_platform cmd_name cmd_args"]}
988986
} else {
989
- test th1-info-vars-5 {$RESULT eq "y tcl_platform"}
987
+ test th1-info-vars-5 {[lsort $RESULT] eq [lsort "y tcl_platform"]}
990988
}
991989
992990
###############################################################################
993991
994992
fossil test-th-eval "array exists foo"
995993
--- test/th1.test
+++ test/th1.test
@@ -913,64 +913,62 @@
913 test th1-reinitialize-2 {$RESULT ne ""}
914
915 ###############################################################################
916
917 #
918 # NOTE: This test may fail if the command names do not always come out in a
919 # deterministic order from TH1.
 
920 #
921 fossil test-th-eval "info commands"
922
 
 
 
 
 
 
 
 
 
 
923 if {$th1Tcl} {
924 test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
925 enable_output uplevel dir http expr glob_match utime styleFooter encode64\
926 catch if tclReady searchable reinitialize combobox lindex tclIsSafe query\
927 html anoncap randhex llength for set break regexp markdown styleHeader\
928 puts return checkout decorate artifact trace wiki proc tclInvoke hascap\
929 globalState continue getParameter hasfeature setting lsearch breakpoint\
930 upvar render repository string unset setParameter list error info rename\
931 tclExpr array anycap tclEval httpize tclMakeSafe}}
932 } else {
933 test th1-info-commands-1 {$RESULT eq {linecount htmlize date stime\
934 enable_output uplevel dir http expr glob_match utime styleFooter encode64\
935 catch if tclReady searchable reinitialize combobox lindex query html\
936 anoncap randhex llength for set break regexp markdown styleHeader puts\
937 return checkout decorate artifact trace wiki proc hascap globalState\
938 continue getParameter hasfeature setting lsearch breakpoint upvar render\
939 repository string unset setParameter list error info rename array anycap\
940 httpize}}
941 }
942
943 ###############################################################################
944
945 fossil test-th-eval "info vars"
946
947 if {$th1Hooks} {
948 test th1-info-vars-1 {$RESULT eq \
949 "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
950 } else {
951 test th1-info-vars-1 {$RESULT eq "tcl_platform"}
952 }
953
954 ###############################################################################
955
956 fossil test-th-eval "set x 1; info vars"
957
958 if {$th1Hooks} {
959 test th1-info-vars-2 {$RESULT eq \
960 "x th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
961 } else {
962 test th1-info-vars-2 {$RESULT eq "x tcl_platform"}
963 }
964
965 ###############################################################################
966
967 fossil test-th-eval "set x 1; unset x; info vars"
968
969 if {$th1Hooks} {
970 test th1-info-vars-3 {$RESULT eq \
971 "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"}
972 } else {
973 test th1-info-vars-3 {$RESULT eq "tcl_platform"}
974 }
975
976 ###############################################################################
@@ -981,14 +979,14 @@
981 ###############################################################################
982
983 fossil test-th-eval "set y 1; proc foo {} {set x 1; uplevel 1 {info vars}}; foo"
984
985 if {$th1Hooks} {
986 test th1-info-vars-5 {$RESULT eq \
987 "th_stack_trace y cmd_flags tcl_platform cmd_name cmd_args"}
988 } else {
989 test th1-info-vars-5 {$RESULT eq "y tcl_platform"}
990 }
991
992 ###############################################################################
993
994 fossil test-th-eval "array exists foo"
995
--- test/th1.test
+++ test/th1.test
@@ -913,64 +913,62 @@
913 test th1-reinitialize-2 {$RESULT ne ""}
914
915 ###############################################################################
916
917 #
918 # NOTE: This test will fail if the command names are added to TH1, or
919 # moved from Tcl builds to plain or the reverse. Sorting the
920 # command lists eliminates a dependence on order.
921 #
922 fossil test-th-eval "info commands"
923 set sorted_result [lsort $RESULT]
924 protOut "Sorted: $sorted_result"
925 set base_commands {anoncap anycap array artifact break breakpoint catch\
926 checkout combobox continue date decorate dir enable_output encode64\
927 error expr for getParameter glob_match globalState hascap hasfeature\
928 html htmlize http httpize if info insertCsrf lindex linecount list\
929 llength lsearch markdown proc puts query randhex redirect regexp\
930 reinitialize rename render repository return searchable set\
931 setParameter setting stime string styleFooter styleHeader tclReady\
932 trace unset uplevel upvar utime verifyCsrf wiki}
933 set tcl_commands {tclEval tclExpr tclInvoke tclIsSafe tclMakeSafe}
934 if {$th1Tcl} {
935 test th1-info-commands-1 {$sorted_result eq [lsort "$base_commands $tcl_commands"]}
 
 
 
 
 
 
 
936 } else {
937 test th1-info-commands-1 {$sorted_result eq [lsort "$base_commands"]}
938 }
939
 
 
 
 
 
 
940
941 ###############################################################################
942
943 fossil test-th-eval "info vars"
944
945 if {$th1Hooks} {
946 test th1-info-vars-1 {[lsort $RESULT] eq \
947 [lsort "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
948 } else {
949 test th1-info-vars-1 {$RESULT eq "tcl_platform"}
950 }
951
952 ###############################################################################
953
954 fossil test-th-eval "set x 1; info vars"
955
956 if {$th1Hooks} {
957 test th1-info-vars-2 {[lsort $RESULT] eq \
958 [lsort "x th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
959 } else {
960 test th1-info-vars-2 {[lsort $RESULT] eq [lsort "x tcl_platform"]}
961 }
962
963 ###############################################################################
964
965 fossil test-th-eval "set x 1; unset x; info vars"
966
967 if {$th1Hooks} {
968 test th1-info-vars-3 {[lsort $RESULT] eq \
969 [lsort "th_stack_trace cmd_flags tcl_platform cmd_name cmd_args"]}
970 } else {
971 test th1-info-vars-3 {$RESULT eq "tcl_platform"}
972 }
973
974 ###############################################################################
@@ -981,14 +979,14 @@
979 ###############################################################################
980
981 fossil test-th-eval "set y 1; proc foo {} {set x 1; uplevel 1 {info vars}}; foo"
982
983 if {$th1Hooks} {
984 test th1-info-vars-5 {[lsort $RESULT] eq \
985 [lsort "th_stack_trace y cmd_flags tcl_platform cmd_name cmd_args"]}
986 } else {
987 test th1-info-vars-5 {[lsort $RESULT] eq [lsort "y tcl_platform"]}
988 }
989
990 ###############################################################################
991
992 fossil test-th-eval "array exists foo"
993
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -158,11 +158,11 @@
158158
#### The directories where the OpenSSL include and library files are located.
159159
# The recommended usage here is to use the Sysinternals junction tool
160160
# to create a hard link between an "openssl-1.x" sub-directory of the
161161
# Fossil source code directory and the target OpenSSL source directory.
162162
#
163
-OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
163
+OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
164164
OPENSSLINCDIR = $(OPENSSLDIR)/include
165165
OPENSSLLIBDIR = $(OPENSSLDIR)
166166
167167
#### Either the directory where the Tcl library is installed or the Tcl
168168
# source code directory resides (depending on the value of the macro
169169
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -158,11 +158,11 @@
158 #### The directories where the OpenSSL include and library files are located.
159 # The recommended usage here is to use the Sysinternals junction tool
160 # to create a hard link between an "openssl-1.x" sub-directory of the
161 # Fossil source code directory and the target OpenSSL source directory.
162 #
163 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
164 OPENSSLINCDIR = $(OPENSSLDIR)/include
165 OPENSSLLIBDIR = $(OPENSSLDIR)
166
167 #### Either the directory where the Tcl library is installed or the Tcl
168 # source code directory resides (depending on the value of the macro
169
--- win/Makefile.mingw
+++ win/Makefile.mingw
@@ -158,11 +158,11 @@
158 #### The directories where the OpenSSL include and library files are located.
159 # The recommended usage here is to use the Sysinternals junction tool
160 # to create a hard link between an "openssl-1.x" sub-directory of the
161 # Fossil source code directory and the target OpenSSL source directory.
162 #
163 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
164 OPENSSLINCDIR = $(OPENSSLDIR)/include
165 OPENSSLLIBDIR = $(OPENSSLDIR)
166
167 #### Either the directory where the Tcl library is installed or the Tcl
168 # source code directory resides (depending on the value of the macro
169
--- win/Makefile.mingw.mistachkin
+++ win/Makefile.mingw.mistachkin
@@ -158,11 +158,11 @@
158158
#### The directories where the OpenSSL include and library files are located.
159159
# The recommended usage here is to use the Sysinternals junction tool
160160
# to create a hard link between an "openssl-1.x" sub-directory of the
161161
# Fossil source code directory and the target OpenSSL source directory.
162162
#
163
-OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
163
+OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
164164
OPENSSLINCDIR = $(OPENSSLDIR)/include
165165
OPENSSLLIBDIR = $(OPENSSLDIR)
166166
167167
#### Either the directory where the Tcl library is installed or the Tcl
168168
# source code directory resides (depending on the value of the macro
169169
--- win/Makefile.mingw.mistachkin
+++ win/Makefile.mingw.mistachkin
@@ -158,11 +158,11 @@
158 #### The directories where the OpenSSL include and library files are located.
159 # The recommended usage here is to use the Sysinternals junction tool
160 # to create a hard link between an "openssl-1.x" sub-directory of the
161 # Fossil source code directory and the target OpenSSL source directory.
162 #
163 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2f
164 OPENSSLINCDIR = $(OPENSSLDIR)/include
165 OPENSSLLIBDIR = $(OPENSSLDIR)
166
167 #### Either the directory where the Tcl library is installed or the Tcl
168 # source code directory resides (depending on the value of the macro
169
--- win/Makefile.mingw.mistachkin
+++ win/Makefile.mingw.mistachkin
@@ -158,11 +158,11 @@
158 #### The directories where the OpenSSL include and library files are located.
159 # The recommended usage here is to use the Sysinternals junction tool
160 # to create a hard link between an "openssl-1.x" sub-directory of the
161 # Fossil source code directory and the target OpenSSL source directory.
162 #
163 OPENSSLDIR = $(SRCDIR)/../compat/openssl-1.0.2g
164 OPENSSLINCDIR = $(OPENSSLDIR)/include
165 OPENSSLLIBDIR = $(OPENSSLDIR)
166
167 #### Either the directory where the Tcl library is installed or the Tcl
168 # source code directory resides (depending on the value of the macro
169
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -93,11 +93,11 @@
9393
!ifndef FOSSIL_ENABLE_WINXP
9494
FOSSIL_ENABLE_WINXP = 0
9595
!endif
9696
9797
!if $(FOSSIL_ENABLE_SSL)!=0
98
-SSLDIR = $(B)\compat\openssl-1.0.2f
98
+SSLDIR = $(B)\compat\openssl-1.0.2g
9999
SSLINCDIR = $(SSLDIR)\inc32
100100
!if $(FOSSIL_DYNAMIC_BUILD)!=0
101101
SSLLIBDIR = $(SSLDIR)\out32dll
102102
!else
103103
SSLLIBDIR = $(SSLDIR)\out32
104104
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -93,11 +93,11 @@
93 !ifndef FOSSIL_ENABLE_WINXP
94 FOSSIL_ENABLE_WINXP = 0
95 !endif
96
97 !if $(FOSSIL_ENABLE_SSL)!=0
98 SSLDIR = $(B)\compat\openssl-1.0.2f
99 SSLINCDIR = $(SSLDIR)\inc32
100 !if $(FOSSIL_DYNAMIC_BUILD)!=0
101 SSLLIBDIR = $(SSLDIR)\out32dll
102 !else
103 SSLLIBDIR = $(SSLDIR)\out32
104
--- win/Makefile.msc
+++ win/Makefile.msc
@@ -93,11 +93,11 @@
93 !ifndef FOSSIL_ENABLE_WINXP
94 FOSSIL_ENABLE_WINXP = 0
95 !endif
96
97 !if $(FOSSIL_ENABLE_SSL)!=0
98 SSLDIR = $(B)\compat\openssl-1.0.2g
99 SSLINCDIR = $(SSLDIR)\inc32
100 !if $(FOSSIL_DYNAMIC_BUILD)!=0
101 SSLLIBDIR = $(SSLDIR)\out32dll
102 !else
103 SSLLIBDIR = $(SSLDIR)\out32
104
--- win/buildmsvc.bat
+++ win/buildmsvc.bat
@@ -259,16 +259,20 @@
259259
CALL :fn_UnsetVariable PFILES_SDK71A
260260
SET NMAKE_ARGS=%NMAKE_ARGS% FOSSIL_ENABLE_WINXP=1
261261
GOTO :EOF
262262
263263
:fn_UnsetVariable
264
+ SETLOCAL
264265
SET VALUE=%1
265266
IF DEFINED VALUE (
266
- SET %VALUE%=
267267
SET VALUE=
268
- CALL :fn_ResetErrorLevel
268
+ ENDLOCAL
269
+ SET %VALUE%=
270
+ ) ELSE (
271
+ ENDLOCAL
269272
)
273
+ CALL :fn_ResetErrorLevel
270274
GOTO :EOF
271275
272276
:fn_ResetErrorLevel
273277
VERIFY > NUL
274278
GOTO :EOF
275279
--- win/buildmsvc.bat
+++ win/buildmsvc.bat
@@ -259,16 +259,20 @@
259 CALL :fn_UnsetVariable PFILES_SDK71A
260 SET NMAKE_ARGS=%NMAKE_ARGS% FOSSIL_ENABLE_WINXP=1
261 GOTO :EOF
262
263 :fn_UnsetVariable
 
264 SET VALUE=%1
265 IF DEFINED VALUE (
266 SET %VALUE%=
267 SET VALUE=
268 CALL :fn_ResetErrorLevel
 
 
 
269 )
 
270 GOTO :EOF
271
272 :fn_ResetErrorLevel
273 VERIFY > NUL
274 GOTO :EOF
275
--- win/buildmsvc.bat
+++ win/buildmsvc.bat
@@ -259,16 +259,20 @@
259 CALL :fn_UnsetVariable PFILES_SDK71A
260 SET NMAKE_ARGS=%NMAKE_ARGS% FOSSIL_ENABLE_WINXP=1
261 GOTO :EOF
262
263 :fn_UnsetVariable
264 SETLOCAL
265 SET VALUE=%1
266 IF DEFINED VALUE (
 
267 SET VALUE=
268 ENDLOCAL
269 SET %VALUE%=
270 ) ELSE (
271 ENDLOCAL
272 )
273 CALL :fn_ResetErrorLevel
274 GOTO :EOF
275
276 :fn_ResetErrorLevel
277 VERIFY > NUL
278 GOTO :EOF
279
+1 -1
--- www/build.wiki
+++ www/build.wiki
@@ -141,11 +141,11 @@
141141
the optional <a href="https://www.openssl.org/">OpenSSL</a> support,
142142
first <a href="https://www.openssl.org/source/">download the official
143143
source code for OpenSSL</a> and extract it to an appropriately named
144144
"<b>openssl-X.Y.ZA</b>" subdirectory within the local
145145
[/tree?ci=trunk&name=compat | compat] directory (e.g.
146
-"<b>compat/openssl-1.0.2f</b>"), then make sure that some recent
146
+"<b>compat/openssl-1.0.2g</b>"), then make sure that some recent
147147
<a href="http://www.perl.org/">Perl</a> binaries are installed locally,
148148
and finally run one of the following commands:
149149
<blockquote><pre>
150150
nmake /f Makefile.msc FOSSIL_ENABLE_SSL=1 FOSSIL_BUILD_SSL=1 PERLDIR=C:\full\path\to\Perl\bin
151151
</pre></blockquote>
152152
--- www/build.wiki
+++ www/build.wiki
@@ -141,11 +141,11 @@
141 the optional <a href="https://www.openssl.org/">OpenSSL</a> support,
142 first <a href="https://www.openssl.org/source/">download the official
143 source code for OpenSSL</a> and extract it to an appropriately named
144 "<b>openssl-X.Y.ZA</b>" subdirectory within the local
145 [/tree?ci=trunk&name=compat | compat] directory (e.g.
146 "<b>compat/openssl-1.0.2f</b>"), then make sure that some recent
147 <a href="http://www.perl.org/">Perl</a> binaries are installed locally,
148 and finally run one of the following commands:
149 <blockquote><pre>
150 nmake /f Makefile.msc FOSSIL_ENABLE_SSL=1 FOSSIL_BUILD_SSL=1 PERLDIR=C:\full\path\to\Perl\bin
151 </pre></blockquote>
152
--- www/build.wiki
+++ www/build.wiki
@@ -141,11 +141,11 @@
141 the optional <a href="https://www.openssl.org/">OpenSSL</a> support,
142 first <a href="https://www.openssl.org/source/">download the official
143 source code for OpenSSL</a> and extract it to an appropriately named
144 "<b>openssl-X.Y.ZA</b>" subdirectory within the local
145 [/tree?ci=trunk&name=compat | compat] directory (e.g.
146 "<b>compat/openssl-1.0.2g</b>"), then make sure that some recent
147 <a href="http://www.perl.org/">Perl</a> binaries are installed locally,
148 and finally run one of the following commands:
149 <blockquote><pre>
150 nmake /f Makefile.msc FOSSIL_ENABLE_SSL=1 FOSSIL_BUILD_SSL=1 PERLDIR=C:\full\path\to\Perl\bin
151 </pre></blockquote>
152
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -174,11 +174,12 @@
174174
to the P-card must be unique to that line.
175175
The first predecessor is the direct ancestor of the manifest.
176176
Other arguments define manifests with which the first was
177177
merged to yield the current manifest. Most manifests have
178178
a P-card with a single argument. The first manifest in the
179
-project has no ancestors and thus has no P-card.
179
+project has no ancestors and thus has no P-card or (depending
180
+on the Fossil version) an empty P-card (no arguments).
180181
181182
A manifest has zero or more Q-cards. A Q-card is similar to a P-card
182183
in that it defines a predecessor to the current check-in. But
183184
whereas a P-card defines the immediate ancestor or a merge
184185
ancestor, the Q-card is used to identify a single check-in or a small
185186
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -174,11 +174,12 @@
174 to the P-card must be unique to that line.
175 The first predecessor is the direct ancestor of the manifest.
176 Other arguments define manifests with which the first was
177 merged to yield the current manifest. Most manifests have
178 a P-card with a single argument. The first manifest in the
179 project has no ancestors and thus has no P-card.
 
180
181 A manifest has zero or more Q-cards. A Q-card is similar to a P-card
182 in that it defines a predecessor to the current check-in. But
183 whereas a P-card defines the immediate ancestor or a merge
184 ancestor, the Q-card is used to identify a single check-in or a small
185
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -174,11 +174,12 @@
174 to the P-card must be unique to that line.
175 The first predecessor is the direct ancestor of the manifest.
176 Other arguments define manifests with which the first was
177 merged to yield the current manifest. Most manifests have
178 a P-card with a single argument. The first manifest in the
179 project has no ancestors and thus has no P-card or (depending
180 on the Fossil version) an empty P-card (no arguments).
181
182 A manifest has zero or more Q-cards. A Q-card is similar to a P-card
183 in that it defines a predecessor to the current check-in. But
184 whereas a P-card defines the immediate ancestor or a merge
185 ancestor, the Q-card is used to identify a single check-in or a small
186

Keyboard Shortcuts

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