Fossil SCM

Merge in latest features and fixes from trunk.

andybradford 2013-08-30 14:07 UTC ssh-test-http merge
Commit ef8b9da03f051039f56f50f5553c8fc08faf9be0
+4 -4
--- src/add.c
+++ src/add.c
@@ -264,11 +264,11 @@
264264
if( zIgnoreFlag==0 ){
265265
zIgnoreFlag = db_get("ignore-glob", 0);
266266
}
267267
vid = db_lget_int("checkout",0);
268268
if( vid==0 ){
269
- fossil_panic("no checkout to add to");
269
+ fossil_fatal("no checkout to add to");
270270
}
271271
db_begin_transaction();
272272
db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
273273
filename_collation());
274274
pClean = glob_create(zCleanFlag);
@@ -331,11 +331,11 @@
331331
332332
capture_case_sensitive_option();
333333
db_must_be_within_tree();
334334
vid = db_lget_int("checkout", 0);
335335
if( vid==0 ){
336
- fossil_panic("no checkout to remove from");
336
+ fossil_fatal("no checkout to remove from");
337337
}
338338
db_begin_transaction();
339339
db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
340340
filename_collation());
341341
for(i=2; i<g.argc; i++){
@@ -506,11 +506,11 @@
506506
if( zIgnoreFlag==0 ){
507507
zIgnoreFlag = db_get("ignore-glob", 0);
508508
}
509509
vid = db_lget_int("checkout",0);
510510
if( vid==0 ){
511
- fossil_panic("no checkout to add to");
511
+ fossil_fatal("no checkout to add to");
512512
}
513513
db_begin_transaction();
514514
515515
/* step 1:
516516
** Populate the temp table "sfile" with the names of all unmanaged
@@ -611,11 +611,11 @@
611611
612612
capture_case_sensitive_option();
613613
db_must_be_within_tree();
614614
vid = db_lget_int("checkout", 0);
615615
if( vid==0 ){
616
- fossil_panic("no checkout rename files in");
616
+ fossil_fatal("no checkout rename files in");
617617
}
618618
if( g.argc<4 ){
619619
usage("OLDNAME NEWNAME");
620620
}
621621
zDest = g.argv[g.argc-1];
622622
--- src/add.c
+++ src/add.c
@@ -264,11 +264,11 @@
264 if( zIgnoreFlag==0 ){
265 zIgnoreFlag = db_get("ignore-glob", 0);
266 }
267 vid = db_lget_int("checkout",0);
268 if( vid==0 ){
269 fossil_panic("no checkout to add to");
270 }
271 db_begin_transaction();
272 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
273 filename_collation());
274 pClean = glob_create(zCleanFlag);
@@ -331,11 +331,11 @@
331
332 capture_case_sensitive_option();
333 db_must_be_within_tree();
334 vid = db_lget_int("checkout", 0);
335 if( vid==0 ){
336 fossil_panic("no checkout to remove from");
337 }
338 db_begin_transaction();
339 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
340 filename_collation());
341 for(i=2; i<g.argc; i++){
@@ -506,11 +506,11 @@
506 if( zIgnoreFlag==0 ){
507 zIgnoreFlag = db_get("ignore-glob", 0);
508 }
509 vid = db_lget_int("checkout",0);
510 if( vid==0 ){
511 fossil_panic("no checkout to add to");
512 }
513 db_begin_transaction();
514
515 /* step 1:
516 ** Populate the temp table "sfile" with the names of all unmanaged
@@ -611,11 +611,11 @@
611
612 capture_case_sensitive_option();
613 db_must_be_within_tree();
614 vid = db_lget_int("checkout", 0);
615 if( vid==0 ){
616 fossil_panic("no checkout rename files in");
617 }
618 if( g.argc<4 ){
619 usage("OLDNAME NEWNAME");
620 }
621 zDest = g.argv[g.argc-1];
622
--- src/add.c
+++ src/add.c
@@ -264,11 +264,11 @@
264 if( zIgnoreFlag==0 ){
265 zIgnoreFlag = db_get("ignore-glob", 0);
266 }
267 vid = db_lget_int("checkout",0);
268 if( vid==0 ){
269 fossil_fatal("no checkout to add to");
270 }
271 db_begin_transaction();
272 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
273 filename_collation());
274 pClean = glob_create(zCleanFlag);
@@ -331,11 +331,11 @@
331
332 capture_case_sensitive_option();
333 db_must_be_within_tree();
334 vid = db_lget_int("checkout", 0);
335 if( vid==0 ){
336 fossil_fatal("no checkout to remove from");
337 }
338 db_begin_transaction();
339 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY %s)",
340 filename_collation());
341 for(i=2; i<g.argc; i++){
@@ -506,11 +506,11 @@
506 if( zIgnoreFlag==0 ){
507 zIgnoreFlag = db_get("ignore-glob", 0);
508 }
509 vid = db_lget_int("checkout",0);
510 if( vid==0 ){
511 fossil_fatal("no checkout to add to");
512 }
513 db_begin_transaction();
514
515 /* step 1:
516 ** Populate the temp table "sfile" with the names of all unmanaged
@@ -611,11 +611,11 @@
611
612 capture_case_sensitive_option();
613 db_must_be_within_tree();
614 vid = db_lget_int("checkout", 0);
615 if( vid==0 ){
616 fossil_fatal("no checkout rename files in");
617 }
618 if( g.argc<4 ){
619 usage("OLDNAME NEWNAME");
620 }
621 zDest = g.argv[g.argc-1];
622
--- src/allrepo.c
+++ src/allrepo.c
@@ -153,10 +153,11 @@
153153
collect_argument(&extra, "stats",0);
154154
}else if( strncmp(zCmd, "sync", n)==0 ){
155155
zCmd = "sync -autourl -R";
156156
collect_argument(&extra, "verbose","v");
157157
}else if( strncmp(zCmd, "test-integrity", n)==0 ){
158
+ collect_argument(&extra, "parse", 0);
158159
zCmd = "test-integrity";
159160
}else if( strncmp(zCmd, "test-orphans", n)==0 ){
160161
zCmd = "test-orphans -R";
161162
}else if( strncmp(zCmd, "test-missing", n)==0 ){
162163
zCmd = "test-missing -q -R";
163164
--- src/allrepo.c
+++ src/allrepo.c
@@ -153,10 +153,11 @@
153 collect_argument(&extra, "stats",0);
154 }else if( strncmp(zCmd, "sync", n)==0 ){
155 zCmd = "sync -autourl -R";
156 collect_argument(&extra, "verbose","v");
157 }else if( strncmp(zCmd, "test-integrity", n)==0 ){
 
158 zCmd = "test-integrity";
159 }else if( strncmp(zCmd, "test-orphans", n)==0 ){
160 zCmd = "test-orphans -R";
161 }else if( strncmp(zCmd, "test-missing", n)==0 ){
162 zCmd = "test-missing -q -R";
163
--- src/allrepo.c
+++ src/allrepo.c
@@ -153,10 +153,11 @@
153 collect_argument(&extra, "stats",0);
154 }else if( strncmp(zCmd, "sync", n)==0 ){
155 zCmd = "sync -autourl -R";
156 collect_argument(&extra, "verbose","v");
157 }else if( strncmp(zCmd, "test-integrity", n)==0 ){
158 collect_argument(&extra, "parse", 0);
159 zCmd = "test-integrity";
160 }else if( strncmp(zCmd, "test-orphans", n)==0 ){
161 zCmd = "test-orphans -R";
162 }else if( strncmp(zCmd, "test-missing", n)==0 ){
163 zCmd = "test-missing -q -R";
164
+2 -2
--- src/attach.c
+++ src/attach.c
@@ -305,11 +305,11 @@
305305
zComment = PD("comment", "");
306306
while( fossil_isspace(zComment[0]) ) zComment++;
307307
n = strlen(zComment);
308308
while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
309309
if( n>0 ){
310
- blob_appendf(&manifest, "C %F\n", zComment);
310
+ blob_appendf(&manifest, "C %#F\n", n, zComment);
311311
}
312312
zDate = date_in_standard_format("now");
313313
blob_appendf(&manifest, "D %s\n", zDate);
314314
blob_appendf(&manifest, "U %F\n", g.zLogin ? g.zLogin : "nobody");
315315
md5sum_blob(&manifest, &cksum);
@@ -337,11 +337,11 @@
337337
}
338338
@ <input type="hidden" name="from" value="%h(zFrom)" />
339339
@ <input type="submit" name="ok" value="Add Attachment" />
340340
@ <input type="submit" name="cancel" value="Cancel" />
341341
@ </div>
342
- captcha_generate();
342
+ captcha_generate(0);
343343
@ </form>
344344
style_footer();
345345
}
346346
347347
/*
348348
--- src/attach.c
+++ src/attach.c
@@ -305,11 +305,11 @@
305 zComment = PD("comment", "");
306 while( fossil_isspace(zComment[0]) ) zComment++;
307 n = strlen(zComment);
308 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
309 if( n>0 ){
310 blob_appendf(&manifest, "C %F\n", zComment);
311 }
312 zDate = date_in_standard_format("now");
313 blob_appendf(&manifest, "D %s\n", zDate);
314 blob_appendf(&manifest, "U %F\n", g.zLogin ? g.zLogin : "nobody");
315 md5sum_blob(&manifest, &cksum);
@@ -337,11 +337,11 @@
337 }
338 @ <input type="hidden" name="from" value="%h(zFrom)" />
339 @ <input type="submit" name="ok" value="Add Attachment" />
340 @ <input type="submit" name="cancel" value="Cancel" />
341 @ </div>
342 captcha_generate();
343 @ </form>
344 style_footer();
345 }
346
347 /*
348
--- src/attach.c
+++ src/attach.c
@@ -305,11 +305,11 @@
305 zComment = PD("comment", "");
306 while( fossil_isspace(zComment[0]) ) zComment++;
307 n = strlen(zComment);
308 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
309 if( n>0 ){
310 blob_appendf(&manifest, "C %#F\n", n, zComment);
311 }
312 zDate = date_in_standard_format("now");
313 blob_appendf(&manifest, "D %s\n", zDate);
314 blob_appendf(&manifest, "U %F\n", g.zLogin ? g.zLogin : "nobody");
315 md5sum_blob(&manifest, &cksum);
@@ -337,11 +337,11 @@
337 }
338 @ <input type="hidden" name="from" value="%h(zFrom)" />
339 @ <input type="submit" name="ok" value="Add Attachment" />
340 @ <input type="submit" name="cancel" value="Cancel" />
341 @ </div>
342 captcha_generate(0);
343 @ </form>
344 style_footer();
345 }
346
347 /*
348
+19 -20
--- src/blob.c
+++ src/blob.c
@@ -700,11 +700,11 @@
700700
** Initialize a blob to be the content of a file. If the filename
701701
** is blank or "-" then read from standard input.
702702
**
703703
** Any prior content of the blob is discarded, not freed.
704704
**
705
-** Return the number of bytes read. Calls fossil_panic() error (i.e.
705
+** Return the number of bytes read. Calls fossil_fatal() error (i.e.
706706
** it exit()s and does not return).
707707
*/
708708
int blob_read_from_file(Blob *pBlob, const char *zFilename){
709709
int size, got;
710710
FILE *in;
@@ -721,11 +721,11 @@
721721
return 0;
722722
}
723723
blob_resize(pBlob, size);
724724
in = fossil_fopen(zFilename, "rb");
725725
if( in==0 ){
726
- fossil_panic("cannot open %s for reading", zFilename);
726
+ fossil_fatal("cannot open %s for reading", zFilename);
727727
}
728728
got = fread(blob_buffer(pBlob), 1, size, in);
729729
fclose(in);
730730
if( got<size ){
731731
blob_resize(pBlob, got);
@@ -744,11 +744,11 @@
744744
int blob_read_link(Blob *pBlob, const char *zFilename){
745745
#if !defined(_WIN32)
746746
char zBuf[1024];
747747
ssize_t len = readlink(zFilename, zBuf, 1023);
748748
if( len < 0 ){
749
- fossil_panic("cannot read symbolic link %s", zFilename);
749
+ fossil_fatal("cannot read symbolic link %s", zFilename);
750750
}
751751
zBuf[len] = 0; /* null-terminate */
752752
blob_zero(pBlob);
753753
blob_appendf(pBlob, "%s", zBuf);
754754
return len;
@@ -766,21 +766,20 @@
766766
**
767767
** Return the number of bytes written.
768768
*/
769769
int blob_write_to_file(Blob *pBlob, const char *zFilename){
770770
FILE *out;
771
- int wrote;
771
+ int nWrote;
772772
773773
if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
774
- int n = blob_size(pBlob);
774
+ nWrote = blob_size(pBlob);
775775
#if defined(_WIN32)
776
- if( fossil_utf8_to_console(blob_buffer(pBlob), n, 0) >= 0 ){
777
- return n;
776
+ if( fossil_utf8_to_console(blob_buffer(pBlob), nWrote, 0) >= 0 ){
777
+ return nWrote;
778778
}
779779
#endif
780
- fwrite(blob_buffer(pBlob), 1, n, stdout);
781
- return n;
780
+ fwrite(blob_buffer(pBlob), 1, nWrote, stdout);
782781
}else{
783782
int i, nName;
784783
char *zName, zBuf[1000];
785784
786785
nName = strlen(zFilename);
@@ -816,19 +815,19 @@
816815
if( out==0 ){
817816
fossil_fatal_recursive("unable to open file \"%s\" for writing", zName);
818817
return 0;
819818
}
820819
if( zName!=zBuf ) free(zName);
821
- }
822
- blob_is_init(pBlob);
823
- wrote = fwrite(blob_buffer(pBlob), 1, blob_size(pBlob), out);
824
- fclose(out);
825
- if( wrote!=blob_size(pBlob) && out!=stdout ){
826
- fossil_fatal_recursive("short write: %d of %d bytes to %s", wrote,
827
- blob_size(pBlob), zFilename);
828
- }
829
- return wrote;
820
+ blob_is_init(pBlob);
821
+ nWrote = fwrite(blob_buffer(pBlob), 1, blob_size(pBlob), out);
822
+ fclose(out);
823
+ if( nWrote!=blob_size(pBlob) ){
824
+ fossil_fatal_recursive("short write: %d of %d bytes to %s", nWrote,
825
+ blob_size(pBlob), zFilename);
826
+ }
827
+ }
828
+ return nWrote;
830829
}
831830
832831
/*
833832
** Compress a blob pIn. Store the result in pOut. It is ok for pIn and
834833
** pOut to be the same blob.
@@ -978,20 +977,20 @@
978977
for(i=2; i<g.argc; i++){
979978
blob_read_from_file(&b1, g.argv[i]);
980979
blob_compress(&b1, &b2);
981980
blob_uncompress(&b2, &b3);
982981
if( blob_compare(&b1, &b3) ){
983
- fossil_panic("compress/uncompress cycle failed for %s", g.argv[i]);
982
+ fossil_fatal("compress/uncompress cycle failed for %s", g.argv[i]);
984983
}
985984
blob_reset(&b1);
986985
blob_reset(&b2);
987986
blob_reset(&b3);
988987
}
989988
fossil_print("ok\n");
990989
}
991990
992
-#if defined(_WIN32)
991
+#if defined(_WIN32) || defined(__CYGWIN__)
993992
/*
994993
** Convert every \n character in the given blob into \r\n.
995994
*/
996995
void blob_add_cr(Blob *p){
997996
char *z = p->aData;
998997
--- src/blob.c
+++ src/blob.c
@@ -700,11 +700,11 @@
700 ** Initialize a blob to be the content of a file. If the filename
701 ** is blank or "-" then read from standard input.
702 **
703 ** Any prior content of the blob is discarded, not freed.
704 **
705 ** Return the number of bytes read. Calls fossil_panic() error (i.e.
706 ** it exit()s and does not return).
707 */
708 int blob_read_from_file(Blob *pBlob, const char *zFilename){
709 int size, got;
710 FILE *in;
@@ -721,11 +721,11 @@
721 return 0;
722 }
723 blob_resize(pBlob, size);
724 in = fossil_fopen(zFilename, "rb");
725 if( in==0 ){
726 fossil_panic("cannot open %s for reading", zFilename);
727 }
728 got = fread(blob_buffer(pBlob), 1, size, in);
729 fclose(in);
730 if( got<size ){
731 blob_resize(pBlob, got);
@@ -744,11 +744,11 @@
744 int blob_read_link(Blob *pBlob, const char *zFilename){
745 #if !defined(_WIN32)
746 char zBuf[1024];
747 ssize_t len = readlink(zFilename, zBuf, 1023);
748 if( len < 0 ){
749 fossil_panic("cannot read symbolic link %s", zFilename);
750 }
751 zBuf[len] = 0; /* null-terminate */
752 blob_zero(pBlob);
753 blob_appendf(pBlob, "%s", zBuf);
754 return len;
@@ -766,21 +766,20 @@
766 **
767 ** Return the number of bytes written.
768 */
769 int blob_write_to_file(Blob *pBlob, const char *zFilename){
770 FILE *out;
771 int wrote;
772
773 if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
774 int n = blob_size(pBlob);
775 #if defined(_WIN32)
776 if( fossil_utf8_to_console(blob_buffer(pBlob), n, 0) >= 0 ){
777 return n;
778 }
779 #endif
780 fwrite(blob_buffer(pBlob), 1, n, stdout);
781 return n;
782 }else{
783 int i, nName;
784 char *zName, zBuf[1000];
785
786 nName = strlen(zFilename);
@@ -816,19 +815,19 @@
816 if( out==0 ){
817 fossil_fatal_recursive("unable to open file \"%s\" for writing", zName);
818 return 0;
819 }
820 if( zName!=zBuf ) free(zName);
821 }
822 blob_is_init(pBlob);
823 wrote = fwrite(blob_buffer(pBlob), 1, blob_size(pBlob), out);
824 fclose(out);
825 if( wrote!=blob_size(pBlob) && out!=stdout ){
826 fossil_fatal_recursive("short write: %d of %d bytes to %s", wrote,
827 blob_size(pBlob), zFilename);
828 }
829 return wrote;
830 }
831
832 /*
833 ** Compress a blob pIn. Store the result in pOut. It is ok for pIn and
834 ** pOut to be the same blob.
@@ -978,20 +977,20 @@
978 for(i=2; i<g.argc; i++){
979 blob_read_from_file(&b1, g.argv[i]);
980 blob_compress(&b1, &b2);
981 blob_uncompress(&b2, &b3);
982 if( blob_compare(&b1, &b3) ){
983 fossil_panic("compress/uncompress cycle failed for %s", g.argv[i]);
984 }
985 blob_reset(&b1);
986 blob_reset(&b2);
987 blob_reset(&b3);
988 }
989 fossil_print("ok\n");
990 }
991
992 #if defined(_WIN32)
993 /*
994 ** Convert every \n character in the given blob into \r\n.
995 */
996 void blob_add_cr(Blob *p){
997 char *z = p->aData;
998
--- src/blob.c
+++ src/blob.c
@@ -700,11 +700,11 @@
700 ** Initialize a blob to be the content of a file. If the filename
701 ** is blank or "-" then read from standard input.
702 **
703 ** Any prior content of the blob is discarded, not freed.
704 **
705 ** Return the number of bytes read. Calls fossil_fatal() error (i.e.
706 ** it exit()s and does not return).
707 */
708 int blob_read_from_file(Blob *pBlob, const char *zFilename){
709 int size, got;
710 FILE *in;
@@ -721,11 +721,11 @@
721 return 0;
722 }
723 blob_resize(pBlob, size);
724 in = fossil_fopen(zFilename, "rb");
725 if( in==0 ){
726 fossil_fatal("cannot open %s for reading", zFilename);
727 }
728 got = fread(blob_buffer(pBlob), 1, size, in);
729 fclose(in);
730 if( got<size ){
731 blob_resize(pBlob, got);
@@ -744,11 +744,11 @@
744 int blob_read_link(Blob *pBlob, const char *zFilename){
745 #if !defined(_WIN32)
746 char zBuf[1024];
747 ssize_t len = readlink(zFilename, zBuf, 1023);
748 if( len < 0 ){
749 fossil_fatal("cannot read symbolic link %s", zFilename);
750 }
751 zBuf[len] = 0; /* null-terminate */
752 blob_zero(pBlob);
753 blob_appendf(pBlob, "%s", zBuf);
754 return len;
@@ -766,21 +766,20 @@
766 **
767 ** Return the number of bytes written.
768 */
769 int blob_write_to_file(Blob *pBlob, const char *zFilename){
770 FILE *out;
771 int nWrote;
772
773 if( zFilename[0]==0 || (zFilename[0]=='-' && zFilename[1]==0) ){
774 nWrote = blob_size(pBlob);
775 #if defined(_WIN32)
776 if( fossil_utf8_to_console(blob_buffer(pBlob), nWrote, 0) >= 0 ){
777 return nWrote;
778 }
779 #endif
780 fwrite(blob_buffer(pBlob), 1, nWrote, stdout);
 
781 }else{
782 int i, nName;
783 char *zName, zBuf[1000];
784
785 nName = strlen(zFilename);
@@ -816,19 +815,19 @@
815 if( out==0 ){
816 fossil_fatal_recursive("unable to open file \"%s\" for writing", zName);
817 return 0;
818 }
819 if( zName!=zBuf ) free(zName);
820 blob_is_init(pBlob);
821 nWrote = fwrite(blob_buffer(pBlob), 1, blob_size(pBlob), out);
822 fclose(out);
823 if( nWrote!=blob_size(pBlob) ){
824 fossil_fatal_recursive("short write: %d of %d bytes to %s", nWrote,
825 blob_size(pBlob), zFilename);
826 }
827 }
828 return nWrote;
829 }
830
831 /*
832 ** Compress a blob pIn. Store the result in pOut. It is ok for pIn and
833 ** pOut to be the same blob.
@@ -978,20 +977,20 @@
977 for(i=2; i<g.argc; i++){
978 blob_read_from_file(&b1, g.argv[i]);
979 blob_compress(&b1, &b2);
980 blob_uncompress(&b2, &b3);
981 if( blob_compare(&b1, &b3) ){
982 fossil_fatal("compress/uncompress cycle failed for %s", g.argv[i]);
983 }
984 blob_reset(&b1);
985 blob_reset(&b2);
986 blob_reset(&b3);
987 }
988 fossil_print("ok\n");
989 }
990
991 #if defined(_WIN32) || defined(__CYGWIN__)
992 /*
993 ** Convert every \n character in the given blob into \r\n.
994 */
995 void blob_add_cr(Blob *p){
996 char *z = p->aData;
997
+4 -4
--- src/branch.c
+++ src/branch.c
@@ -56,11 +56,11 @@
5656
noSign = db_get_int("omitsign", 0)|noSign;
5757
5858
/* fossil branch new name */
5959
zBranch = g.argv[3];
6060
if( zBranch==0 || zBranch[0]==0 ){
61
- fossil_panic("branch name cannot be empty");
61
+ fossil_fatal("branch name cannot be empty");
6262
}
6363
if( db_exists(
6464
"SELECT 1 FROM tagxref"
6565
" WHERE tagtype>0"
6666
" AND tagid=(SELECT tagid FROM tag WHERE tagname='sym-%q')",
@@ -150,15 +150,15 @@
150150
}
151151
}
152152
153153
brid = content_put_ex(&branch, 0, 0, 0, isPrivate);
154154
if( brid==0 ){
155
- fossil_panic("trouble committing manifest: %s", g.zErrMsg);
155
+ fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
156156
}
157157
db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
158158
if( manifest_crosslink(brid, &branch)==0 ){
159
- fossil_panic("unable to install new manifest");
159
+ fossil_fatal("unable to install new manifest");
160160
}
161161
assert( blob_is_reset(&branch) );
162162
content_deltify(rootid, brid, 0);
163163
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", brid);
164164
fossil_print("New branch: %s\n", zUuid);
@@ -279,11 +279,11 @@
279279
int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
280280
fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
281281
}
282282
db_finalize(&q);
283283
}else{
284
- fossil_panic("branch subcommand should be one of: "
284
+ fossil_fatal("branch subcommand should be one of: "
285285
"new list ls");
286286
}
287287
}
288288
289289
/*
290290
--- src/branch.c
+++ src/branch.c
@@ -56,11 +56,11 @@
56 noSign = db_get_int("omitsign", 0)|noSign;
57
58 /* fossil branch new name */
59 zBranch = g.argv[3];
60 if( zBranch==0 || zBranch[0]==0 ){
61 fossil_panic("branch name cannot be empty");
62 }
63 if( db_exists(
64 "SELECT 1 FROM tagxref"
65 " WHERE tagtype>0"
66 " AND tagid=(SELECT tagid FROM tag WHERE tagname='sym-%q')",
@@ -150,15 +150,15 @@
150 }
151 }
152
153 brid = content_put_ex(&branch, 0, 0, 0, isPrivate);
154 if( brid==0 ){
155 fossil_panic("trouble committing manifest: %s", g.zErrMsg);
156 }
157 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
158 if( manifest_crosslink(brid, &branch)==0 ){
159 fossil_panic("unable to install new manifest");
160 }
161 assert( blob_is_reset(&branch) );
162 content_deltify(rootid, brid, 0);
163 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", brid);
164 fossil_print("New branch: %s\n", zUuid);
@@ -279,11 +279,11 @@
279 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
280 fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
281 }
282 db_finalize(&q);
283 }else{
284 fossil_panic("branch subcommand should be one of: "
285 "new list ls");
286 }
287 }
288
289 /*
290
--- src/branch.c
+++ src/branch.c
@@ -56,11 +56,11 @@
56 noSign = db_get_int("omitsign", 0)|noSign;
57
58 /* fossil branch new name */
59 zBranch = g.argv[3];
60 if( zBranch==0 || zBranch[0]==0 ){
61 fossil_fatal("branch name cannot be empty");
62 }
63 if( db_exists(
64 "SELECT 1 FROM tagxref"
65 " WHERE tagtype>0"
66 " AND tagid=(SELECT tagid FROM tag WHERE tagname='sym-%q')",
@@ -150,15 +150,15 @@
150 }
151 }
152
153 brid = content_put_ex(&branch, 0, 0, 0, isPrivate);
154 if( brid==0 ){
155 fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
156 }
157 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
158 if( manifest_crosslink(brid, &branch)==0 ){
159 fossil_fatal("unable to install new manifest");
160 }
161 assert( blob_is_reset(&branch) );
162 content_deltify(rootid, brid, 0);
163 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", brid);
164 fossil_print("New branch: %s\n", zUuid);
@@ -279,11 +279,11 @@
279 int isCur = zCurrent!=0 && fossil_strcmp(zCurrent,zBr)==0;
280 fossil_print("%s%s\n", (isCur ? "* " : " "), zBr);
281 }
282 db_finalize(&q);
283 }else{
284 fossil_fatal("branch subcommand should be one of: "
285 "new list ls");
286 }
287 }
288
289 /*
290
+98 -7
--- src/captcha.c
+++ src/captcha.c
@@ -69,16 +69,16 @@
6969
** Render an 8-character hexadecimal string as ascii art.
7070
** Space to hold the result is obtained from malloc() and should be freed
7171
** by the caller.
7272
*/
7373
char *captcha_render(const char *zPw){
74
- char *z = fossil_malloc( 500 );
74
+ char *z = fossil_malloc( 9*6*strlen(zPw) + 7 );
7575
int i, j, k, m;
7676
7777
k = 0;
7878
for(i=0; i<6; i++){
79
- for(j=0; j<8; j++){
79
+ for(j=0; zPw[j]; j++){
8080
unsigned char v = hexValue(zPw[j]);
8181
v = (aFont1[v] >> ((5-i)*4)) & 0xf;
8282
for(m=8; m>=1; m = m>>1){
8383
if( v & m ){
8484
z[k++] = 'X';
@@ -202,17 +202,17 @@
202202
** Render an 8-digit hexadecimal string as ascii arg.
203203
** Space to hold the result is obtained from malloc() and should be freed
204204
** by the caller.
205205
*/
206206
char *captcha_render(const char *zPw){
207
- char *z = fossil_malloc( 300 );
207
+ char *z = fossil_malloc( 7*4*strlen(zPw) + 5 );
208208
int i, j, k, m;
209209
const char *zChar;
210210
211211
k = 0;
212212
for(i=0; i<4; i++){
213
- for(j=0; j<8; j++){
213
+ for(j=0; zPw[j]; j++){
214214
unsigned char v = hexValue(zPw[j]);
215215
zChar = azFont2[4*v + i];
216216
for(m=0; zChar[m]; m++){
217217
z[k++] = zChar[m];
218218
}
@@ -359,19 +359,52 @@
359359
** Render an 8-digit hexadecimal string as ascii arg.
360360
** Space to hold the result is obtained from malloc() and should be freed
361361
** by the caller.
362362
*/
363363
char *captcha_render(const char *zPw){
364
- char *z = fossil_malloc( 600 );
364
+ char *z = fossil_malloc( 10*6*strlen(zPw) + 7 );
365365
int i, j, k, m;
366366
const char *zChar;
367
+ unsigned char x;
368
+ int y;
367369
368370
k = 0;
369371
for(i=0; i<6; i++){
370
- for(j=0; j<8; j++){
372
+ x = 0;
373
+ for(j=0; zPw[j]; j++){
371374
unsigned char v = hexValue(zPw[j]);
375
+ x = (x<<4) + v;
376
+ switch( x ){
377
+ case 0x7a:
378
+ case 0xfa:
379
+ y = 3;
380
+ break;
381
+ case 0x47:
382
+ y = 2;
383
+ break;
384
+ case 0xf6:
385
+ case 0xa9:
386
+ case 0xa4:
387
+ case 0xa1:
388
+ case 0x9a:
389
+ case 0x76:
390
+ case 0x61:
391
+ case 0x67:
392
+ case 0x69:
393
+ case 0x41:
394
+ case 0x42:
395
+ case 0x43:
396
+ case 0x4a:
397
+ y = 1;
398
+ break;
399
+ default:
400
+ y = 0;
401
+ break;
402
+ }
372403
zChar = azFont3[6*v + i];
404
+ while( y && zChar[0]==' ' ){ y--; zChar++; }
405
+ while( y && z[k-1]==' ' ){ y--; k--; }
373406
for(m=0; zChar[m]; m++){
374407
z[k++] = zChar[m];
375408
}
376409
}
377410
z[k++] = '\n';
@@ -496,11 +529,11 @@
496529
** for the seed and the entry box into which the user will type the text of
497530
** the captcha. This is typically done at the very bottom of a form.
498531
**
499532
** This routine is a no-op if no captcha is required.
500533
*/
501
-void captcha_generate(void){
534
+void captcha_generate(int showButton){
502535
unsigned int uSeed;
503536
const char *zDecoded;
504537
char *zCaptcha;
505538
506539
if( !captcha_needed() ) return;
@@ -511,7 +544,65 @@
511544
@ %h(zCaptcha)
512545
@ </pre>
513546
@ Enter security code shown above:
514547
@ <input type="hidden" name="captchaseed" value="%u(uSeed)" />
515548
@ <input type="text" name="captcha" size=8 />
549
+ if( showButton ){
550
+ @ <input type="submit" value="Submit">
551
+ }
516552
@ </td></tr></table></div>
517553
}
554
+
555
+/*
556
+** WEBPAGE: test-captcha
557
+*/
558
+void captcha_test(void){
559
+ const char *zPw = P("name");
560
+ if( zPw==0 || zPw[0]==0 ){
561
+ u64 x;
562
+ sqlite3_randomness(sizeof(x), &x);
563
+ zPw = mprintf("%016llx", x);
564
+ }
565
+ style_header("Captcha Test");
566
+ @ <pre>
567
+ @ %s(captcha_render(zPw))
568
+ @ </pre>
569
+ style_footer();
570
+}
571
+
572
+/*
573
+** Check to see if the current request is coming from an agent that might
574
+** be a spider. If the agent is not a spider, then return 0 without doing
575
+** anything. But if the user agent appears to be a spider, offer
576
+** a captcha challenge to allow the user agent to prove that it is human
577
+** and return non-zero.
578
+*/
579
+int exclude_spiders(const char *zPage){
580
+ const char *zCookieValue;
581
+ char *zCookieName;
582
+ if( g.isHuman ) return 0;
583
+#if 0
584
+ {
585
+ const char *zReferer = P("HTTP_REFERER");
586
+ if( zReferer && strncmp(g.zBaseURL, zReferer, strlen(g.zBaseURL))==0 ){
587
+ return 0;
588
+ }
589
+ }
590
+#endif
591
+ zCookieName = mprintf("fossil-cc-%.10s", db_get("project-code","x"));
592
+ zCookieValue = P(zCookieName);
593
+ if( zCookieValue && atoi(zCookieValue)==1 ) return 0;
594
+ if( captcha_is_correct() ){
595
+ cgi_set_cookie(zCookieName, "1", login_cookie_path(), 8*3600);
596
+ return 0;
597
+ }
598
+
599
+ /* This appears to be a spider. Offer the captcha */
600
+ style_header("Verification");
601
+ form_begin(0, "%s", zPage);
602
+ cgi_query_parameters_to_hidden();
603
+ @ <p>Please demonstrate that you are human, not a spider or robot</p>
604
+ captcha_generate(1);
605
+ @ </form>
606
+ style_footer();
607
+ return 1;
608
+}
518609
--- src/captcha.c
+++ src/captcha.c
@@ -69,16 +69,16 @@
69 ** Render an 8-character hexadecimal string as ascii art.
70 ** Space to hold the result is obtained from malloc() and should be freed
71 ** by the caller.
72 */
73 char *captcha_render(const char *zPw){
74 char *z = fossil_malloc( 500 );
75 int i, j, k, m;
76
77 k = 0;
78 for(i=0; i<6; i++){
79 for(j=0; j<8; j++){
80 unsigned char v = hexValue(zPw[j]);
81 v = (aFont1[v] >> ((5-i)*4)) & 0xf;
82 for(m=8; m>=1; m = m>>1){
83 if( v & m ){
84 z[k++] = 'X';
@@ -202,17 +202,17 @@
202 ** Render an 8-digit hexadecimal string as ascii arg.
203 ** Space to hold the result is obtained from malloc() and should be freed
204 ** by the caller.
205 */
206 char *captcha_render(const char *zPw){
207 char *z = fossil_malloc( 300 );
208 int i, j, k, m;
209 const char *zChar;
210
211 k = 0;
212 for(i=0; i<4; i++){
213 for(j=0; j<8; j++){
214 unsigned char v = hexValue(zPw[j]);
215 zChar = azFont2[4*v + i];
216 for(m=0; zChar[m]; m++){
217 z[k++] = zChar[m];
218 }
@@ -359,19 +359,52 @@
359 ** Render an 8-digit hexadecimal string as ascii arg.
360 ** Space to hold the result is obtained from malloc() and should be freed
361 ** by the caller.
362 */
363 char *captcha_render(const char *zPw){
364 char *z = fossil_malloc( 600 );
365 int i, j, k, m;
366 const char *zChar;
 
 
367
368 k = 0;
369 for(i=0; i<6; i++){
370 for(j=0; j<8; j++){
 
371 unsigned char v = hexValue(zPw[j]);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372 zChar = azFont3[6*v + i];
 
 
373 for(m=0; zChar[m]; m++){
374 z[k++] = zChar[m];
375 }
376 }
377 z[k++] = '\n';
@@ -496,11 +529,11 @@
496 ** for the seed and the entry box into which the user will type the text of
497 ** the captcha. This is typically done at the very bottom of a form.
498 **
499 ** This routine is a no-op if no captcha is required.
500 */
501 void captcha_generate(void){
502 unsigned int uSeed;
503 const char *zDecoded;
504 char *zCaptcha;
505
506 if( !captcha_needed() ) return;
@@ -511,7 +544,65 @@
511 @ %h(zCaptcha)
512 @ </pre>
513 @ Enter security code shown above:
514 @ <input type="hidden" name="captchaseed" value="%u(uSeed)" />
515 @ <input type="text" name="captcha" size=8 />
 
 
 
516 @ </td></tr></table></div>
517 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
--- src/captcha.c
+++ src/captcha.c
@@ -69,16 +69,16 @@
69 ** Render an 8-character hexadecimal string as ascii art.
70 ** Space to hold the result is obtained from malloc() and should be freed
71 ** by the caller.
72 */
73 char *captcha_render(const char *zPw){
74 char *z = fossil_malloc( 9*6*strlen(zPw) + 7 );
75 int i, j, k, m;
76
77 k = 0;
78 for(i=0; i<6; i++){
79 for(j=0; zPw[j]; j++){
80 unsigned char v = hexValue(zPw[j]);
81 v = (aFont1[v] >> ((5-i)*4)) & 0xf;
82 for(m=8; m>=1; m = m>>1){
83 if( v & m ){
84 z[k++] = 'X';
@@ -202,17 +202,17 @@
202 ** Render an 8-digit hexadecimal string as ascii arg.
203 ** Space to hold the result is obtained from malloc() and should be freed
204 ** by the caller.
205 */
206 char *captcha_render(const char *zPw){
207 char *z = fossil_malloc( 7*4*strlen(zPw) + 5 );
208 int i, j, k, m;
209 const char *zChar;
210
211 k = 0;
212 for(i=0; i<4; i++){
213 for(j=0; zPw[j]; j++){
214 unsigned char v = hexValue(zPw[j]);
215 zChar = azFont2[4*v + i];
216 for(m=0; zChar[m]; m++){
217 z[k++] = zChar[m];
218 }
@@ -359,19 +359,52 @@
359 ** Render an 8-digit hexadecimal string as ascii arg.
360 ** Space to hold the result is obtained from malloc() and should be freed
361 ** by the caller.
362 */
363 char *captcha_render(const char *zPw){
364 char *z = fossil_malloc( 10*6*strlen(zPw) + 7 );
365 int i, j, k, m;
366 const char *zChar;
367 unsigned char x;
368 int y;
369
370 k = 0;
371 for(i=0; i<6; i++){
372 x = 0;
373 for(j=0; zPw[j]; j++){
374 unsigned char v = hexValue(zPw[j]);
375 x = (x<<4) + v;
376 switch( x ){
377 case 0x7a:
378 case 0xfa:
379 y = 3;
380 break;
381 case 0x47:
382 y = 2;
383 break;
384 case 0xf6:
385 case 0xa9:
386 case 0xa4:
387 case 0xa1:
388 case 0x9a:
389 case 0x76:
390 case 0x61:
391 case 0x67:
392 case 0x69:
393 case 0x41:
394 case 0x42:
395 case 0x43:
396 case 0x4a:
397 y = 1;
398 break;
399 default:
400 y = 0;
401 break;
402 }
403 zChar = azFont3[6*v + i];
404 while( y && zChar[0]==' ' ){ y--; zChar++; }
405 while( y && z[k-1]==' ' ){ y--; k--; }
406 for(m=0; zChar[m]; m++){
407 z[k++] = zChar[m];
408 }
409 }
410 z[k++] = '\n';
@@ -496,11 +529,11 @@
529 ** for the seed and the entry box into which the user will type the text of
530 ** the captcha. This is typically done at the very bottom of a form.
531 **
532 ** This routine is a no-op if no captcha is required.
533 */
534 void captcha_generate(int showButton){
535 unsigned int uSeed;
536 const char *zDecoded;
537 char *zCaptcha;
538
539 if( !captcha_needed() ) return;
@@ -511,7 +544,65 @@
544 @ %h(zCaptcha)
545 @ </pre>
546 @ Enter security code shown above:
547 @ <input type="hidden" name="captchaseed" value="%u(uSeed)" />
548 @ <input type="text" name="captcha" size=8 />
549 if( showButton ){
550 @ <input type="submit" value="Submit">
551 }
552 @ </td></tr></table></div>
553 }
554
555 /*
556 ** WEBPAGE: test-captcha
557 */
558 void captcha_test(void){
559 const char *zPw = P("name");
560 if( zPw==0 || zPw[0]==0 ){
561 u64 x;
562 sqlite3_randomness(sizeof(x), &x);
563 zPw = mprintf("%016llx", x);
564 }
565 style_header("Captcha Test");
566 @ <pre>
567 @ %s(captcha_render(zPw))
568 @ </pre>
569 style_footer();
570 }
571
572 /*
573 ** Check to see if the current request is coming from an agent that might
574 ** be a spider. If the agent is not a spider, then return 0 without doing
575 ** anything. But if the user agent appears to be a spider, offer
576 ** a captcha challenge to allow the user agent to prove that it is human
577 ** and return non-zero.
578 */
579 int exclude_spiders(const char *zPage){
580 const char *zCookieValue;
581 char *zCookieName;
582 if( g.isHuman ) return 0;
583 #if 0
584 {
585 const char *zReferer = P("HTTP_REFERER");
586 if( zReferer && strncmp(g.zBaseURL, zReferer, strlen(g.zBaseURL))==0 ){
587 return 0;
588 }
589 }
590 #endif
591 zCookieName = mprintf("fossil-cc-%.10s", db_get("project-code","x"));
592 zCookieValue = P(zCookieName);
593 if( zCookieValue && atoi(zCookieValue)==1 ) return 0;
594 if( captcha_is_correct() ){
595 cgi_set_cookie(zCookieName, "1", login_cookie_path(), 8*3600);
596 return 0;
597 }
598
599 /* This appears to be a spider. Offer the captcha */
600 style_header("Verification");
601 form_begin(0, "%s", zPage);
602 cgi_query_parameters_to_hidden();
603 @ <p>Please demonstrate that you are human, not a spider or robot</p>
604 captcha_generate(1);
605 @ </form>
606 style_footer();
607 return 1;
608 }
609
+29 -11
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411411
static int seqQP = 0; /* Sequence numbers */
412412
static struct QParam { /* One entry for each query parameter or cookie */
413413
const char *zName; /* Parameter or cookie name */
414414
const char *zValue; /* Value of the query parameter or cookie */
415415
int seq; /* Order of insertion */
416
+ int isQP; /* True for query parameters */
416417
} *aParamQP; /* An array of all parameters and cookies */
417418
418419
/*
419420
** Add another query parameter or cookie to the parameter set.
420421
** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
421422
** is its fully decoded value.
422423
**
423424
** zName and zValue are not copied and must not change or be
424425
** deallocated after this routine returns.
425426
*/
426
-void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
427
+void cgi_set_parameter_nocopy(const char *zName, const char *zValue, int isQP){
427428
if( nAllocQP<=nUsedQP ){
428429
nAllocQP = nAllocQP*2 + 10;
429430
if( nAllocQP>1000 ){
430431
/* Prevent a DOS service attack against the framework */
431432
fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
436437
aParamQP[nUsedQP].zValue = zValue;
437438
if( g.fHttpTrace ){
438439
fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
439440
}
440441
aParamQP[nUsedQP].seq = seqQP++;
442
+ aParamQP[nUsedQP].isQP = isQP;
441443
nUsedQP++;
442444
sortQP = 1;
443445
}
444446
445447
/*
@@ -448,11 +450,11 @@
448450
** is its fully decoded value.
449451
**
450452
** Copies are made of both the zName and zValue parameters.
451453
*/
452454
void cgi_set_parameter(const char *zName, const char *zValue){
453
- cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue));
455
+ cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue), 0);
454456
}
455457
456458
/*
457459
** Replace a parameter with a new value.
458460
*/
@@ -462,19 +464,19 @@
462464
if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
463465
aParamQP[i].zValue = mprintf("%s",zValue);
464466
return;
465467
}
466468
}
467
- cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
469
+ cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
468470
}
469471
470472
/*
471473
** Add a query parameter. The zName portion is fixed but a copy
472474
** must be made of zValue.
473475
*/
474476
void cgi_setenv(const char *zName, const char *zValue){
475
- cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
477
+ cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
476478
}
477479
478480
479481
/*
480482
** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
500502
** The input string "z" is modified but no copies is made. "z"
501503
** should not be deallocated or changed again after this routine
502504
** returns or it will corrupt the parameter table.
503505
*/
504506
static void add_param_list(char *z, int terminator){
507
+ int isQP = terminator=='&';
505508
while( *z ){
506509
char *zName;
507510
char *zValue;
508511
while( fossil_isspace(*z) ){ z++; }
509512
zName = z;
@@ -521,11 +524,11 @@
521524
}else{
522525
if( *z ){ *z++ = 0; }
523526
zValue = "";
524527
}
525528
if( fossil_islower(zName[0]) ){
526
- cgi_set_parameter_nocopy(zName, zValue);
529
+ cgi_set_parameter_nocopy(zName, zValue, isQP);
527530
}
528531
#ifdef FOSSIL_ENABLE_JSON
529532
json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
530533
#endif /* FOSSIL_ENABLE_JSON */
531534
}
@@ -665,14 +668,14 @@
665668
while( (zLine = get_line_from_string(&z, &len))!=0 ){
666669
if( zLine[0]==0 ){
667670
int nContent = 0;
668671
zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
669672
if( zName && zValue && fossil_islower(zName[0]) ){
670
- cgi_set_parameter_nocopy(zName, zValue);
673
+ cgi_set_parameter_nocopy(zName, zValue, 1);
671674
if( showBytes ){
672675
cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
673
- mprintf("%d",nContent));
676
+ mprintf("%d",nContent), 1);
674677
}
675678
}
676679
zName = 0;
677680
showBytes = 0;
678681
}else{
@@ -685,17 +688,17 @@
685688
}else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
686689
zName = azArg[++i];
687690
}else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
688691
char *z = azArg[++i];
689692
if( zName && z && fossil_islower(zName[0]) ){
690
- cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z);
693
+ cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z, 1);
691694
}
692695
showBytes = 1;
693696
}else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
694697
char *z = azArg[++i];
695698
if( zName && z && fossil_islower(zName[0]) ){
696
- cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z);
699
+ cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z, 1);
697700
}
698701
}
699702
}
700703
}
701704
}
@@ -867,12 +870,12 @@
867870
g.zIpAddr = mprintf("%s", z);
868871
}
869872
870873
len = atoi(PD("CONTENT_LENGTH", "0"));
871874
g.zContentType = zType = P("CONTENT_TYPE");
875
+ blob_zero(&g.cgiIn);
872876
if( len>0 && zType ){
873
- blob_zero(&g.cgiIn);
874877
if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
875878
|| strncmp(zType,"multipart/form-data",19)==0 ){
876879
z = fossil_malloc( len+1 );
877880
len = fread(z, 1, len, g.httpIn);
878881
z[len] = 0;
@@ -987,11 +990,11 @@
987990
** with the given name.
988991
*/
989992
if( fossil_isupper(zName[0]) ){
990993
const char *zValue = fossil_getenv(zName);
991994
if( zValue ){
992
- cgi_set_parameter_nocopy(zName, zValue);
995
+ cgi_set_parameter_nocopy(zName, zValue, 0);
993996
CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
994997
return zValue;
995998
}
996999
}
9971000
CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
10881091
if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
10891092
}
10901093
cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
10911094
}
10921095
}
1096
+
1097
+/*
1098
+** Export all query parameters (but not cookies or environment variables)
1099
+** as hidden values of a form.
1100
+*/
1101
+void cgi_query_parameters_to_hidden(void){
1102
+ int i;
1103
+ const char *zN, *zV;
1104
+ for(i=0; i<nUsedQP; i++){
1105
+ if( aParamQP[i].isQP==0 ) continue;
1106
+ zN = aParamQP[i].zName;
1107
+ zV = aParamQP[i].zValue;
1108
+ @ <input type="hidden" name="%h(zN)" value="%h(zV)">
1109
+ }
1110
+}
10931111
10941112
/*
10951113
** This routine works like "printf" except that it has the
10961114
** extra formatting capabilities such as %h and %t.
10971115
*/
10981116
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411 static int seqQP = 0; /* Sequence numbers */
412 static struct QParam { /* One entry for each query parameter or cookie */
413 const char *zName; /* Parameter or cookie name */
414 const char *zValue; /* Value of the query parameter or cookie */
415 int seq; /* Order of insertion */
 
416 } *aParamQP; /* An array of all parameters and cookies */
417
418 /*
419 ** Add another query parameter or cookie to the parameter set.
420 ** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
421 ** is its fully decoded value.
422 **
423 ** zName and zValue are not copied and must not change or be
424 ** deallocated after this routine returns.
425 */
426 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
427 if( nAllocQP<=nUsedQP ){
428 nAllocQP = nAllocQP*2 + 10;
429 if( nAllocQP>1000 ){
430 /* Prevent a DOS service attack against the framework */
431 fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
436 aParamQP[nUsedQP].zValue = zValue;
437 if( g.fHttpTrace ){
438 fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
439 }
440 aParamQP[nUsedQP].seq = seqQP++;
 
441 nUsedQP++;
442 sortQP = 1;
443 }
444
445 /*
@@ -448,11 +450,11 @@
448 ** is its fully decoded value.
449 **
450 ** Copies are made of both the zName and zValue parameters.
451 */
452 void cgi_set_parameter(const char *zName, const char *zValue){
453 cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue));
454 }
455
456 /*
457 ** Replace a parameter with a new value.
458 */
@@ -462,19 +464,19 @@
462 if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
463 aParamQP[i].zValue = mprintf("%s",zValue);
464 return;
465 }
466 }
467 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
468 }
469
470 /*
471 ** Add a query parameter. The zName portion is fixed but a copy
472 ** must be made of zValue.
473 */
474 void cgi_setenv(const char *zName, const char *zValue){
475 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
476 }
477
478
479 /*
480 ** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
500 ** The input string "z" is modified but no copies is made. "z"
501 ** should not be deallocated or changed again after this routine
502 ** returns or it will corrupt the parameter table.
503 */
504 static void add_param_list(char *z, int terminator){
 
505 while( *z ){
506 char *zName;
507 char *zValue;
508 while( fossil_isspace(*z) ){ z++; }
509 zName = z;
@@ -521,11 +524,11 @@
521 }else{
522 if( *z ){ *z++ = 0; }
523 zValue = "";
524 }
525 if( fossil_islower(zName[0]) ){
526 cgi_set_parameter_nocopy(zName, zValue);
527 }
528 #ifdef FOSSIL_ENABLE_JSON
529 json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
530 #endif /* FOSSIL_ENABLE_JSON */
531 }
@@ -665,14 +668,14 @@
665 while( (zLine = get_line_from_string(&z, &len))!=0 ){
666 if( zLine[0]==0 ){
667 int nContent = 0;
668 zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
669 if( zName && zValue && fossil_islower(zName[0]) ){
670 cgi_set_parameter_nocopy(zName, zValue);
671 if( showBytes ){
672 cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
673 mprintf("%d",nContent));
674 }
675 }
676 zName = 0;
677 showBytes = 0;
678 }else{
@@ -685,17 +688,17 @@
685 }else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
686 zName = azArg[++i];
687 }else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
688 char *z = azArg[++i];
689 if( zName && z && fossil_islower(zName[0]) ){
690 cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z);
691 }
692 showBytes = 1;
693 }else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
694 char *z = azArg[++i];
695 if( zName && z && fossil_islower(zName[0]) ){
696 cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z);
697 }
698 }
699 }
700 }
701 }
@@ -867,12 +870,12 @@
867 g.zIpAddr = mprintf("%s", z);
868 }
869
870 len = atoi(PD("CONTENT_LENGTH", "0"));
871 g.zContentType = zType = P("CONTENT_TYPE");
 
872 if( len>0 && zType ){
873 blob_zero(&g.cgiIn);
874 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
875 || strncmp(zType,"multipart/form-data",19)==0 ){
876 z = fossil_malloc( len+1 );
877 len = fread(z, 1, len, g.httpIn);
878 z[len] = 0;
@@ -987,11 +990,11 @@
987 ** with the given name.
988 */
989 if( fossil_isupper(zName[0]) ){
990 const char *zValue = fossil_getenv(zName);
991 if( zValue ){
992 cgi_set_parameter_nocopy(zName, zValue);
993 CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
994 return zValue;
995 }
996 }
997 CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
1088 if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
1089 }
1090 cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
1091 }
1092 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1093
1094 /*
1095 ** This routine works like "printf" except that it has the
1096 ** extra formatting capabilities such as %h and %t.
1097 */
1098
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411 static int seqQP = 0; /* Sequence numbers */
412 static struct QParam { /* One entry for each query parameter or cookie */
413 const char *zName; /* Parameter or cookie name */
414 const char *zValue; /* Value of the query parameter or cookie */
415 int seq; /* Order of insertion */
416 int isQP; /* True for query parameters */
417 } *aParamQP; /* An array of all parameters and cookies */
418
419 /*
420 ** Add another query parameter or cookie to the parameter set.
421 ** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
422 ** is its fully decoded value.
423 **
424 ** zName and zValue are not copied and must not change or be
425 ** deallocated after this routine returns.
426 */
427 void cgi_set_parameter_nocopy(const char *zName, const char *zValue, int isQP){
428 if( nAllocQP<=nUsedQP ){
429 nAllocQP = nAllocQP*2 + 10;
430 if( nAllocQP>1000 ){
431 /* Prevent a DOS service attack against the framework */
432 fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
437 aParamQP[nUsedQP].zValue = zValue;
438 if( g.fHttpTrace ){
439 fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
440 }
441 aParamQP[nUsedQP].seq = seqQP++;
442 aParamQP[nUsedQP].isQP = isQP;
443 nUsedQP++;
444 sortQP = 1;
445 }
446
447 /*
@@ -448,11 +450,11 @@
450 ** is its fully decoded value.
451 **
452 ** Copies are made of both the zName and zValue parameters.
453 */
454 void cgi_set_parameter(const char *zName, const char *zValue){
455 cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue), 0);
456 }
457
458 /*
459 ** Replace a parameter with a new value.
460 */
@@ -462,19 +464,19 @@
464 if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
465 aParamQP[i].zValue = mprintf("%s",zValue);
466 return;
467 }
468 }
469 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
470 }
471
472 /*
473 ** Add a query parameter. The zName portion is fixed but a copy
474 ** must be made of zValue.
475 */
476 void cgi_setenv(const char *zName, const char *zValue){
477 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
478 }
479
480
481 /*
482 ** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
502 ** The input string "z" is modified but no copies is made. "z"
503 ** should not be deallocated or changed again after this routine
504 ** returns or it will corrupt the parameter table.
505 */
506 static void add_param_list(char *z, int terminator){
507 int isQP = terminator=='&';
508 while( *z ){
509 char *zName;
510 char *zValue;
511 while( fossil_isspace(*z) ){ z++; }
512 zName = z;
@@ -521,11 +524,11 @@
524 }else{
525 if( *z ){ *z++ = 0; }
526 zValue = "";
527 }
528 if( fossil_islower(zName[0]) ){
529 cgi_set_parameter_nocopy(zName, zValue, isQP);
530 }
531 #ifdef FOSSIL_ENABLE_JSON
532 json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
533 #endif /* FOSSIL_ENABLE_JSON */
534 }
@@ -665,14 +668,14 @@
668 while( (zLine = get_line_from_string(&z, &len))!=0 ){
669 if( zLine[0]==0 ){
670 int nContent = 0;
671 zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
672 if( zName && zValue && fossil_islower(zName[0]) ){
673 cgi_set_parameter_nocopy(zName, zValue, 1);
674 if( showBytes ){
675 cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
676 mprintf("%d",nContent), 1);
677 }
678 }
679 zName = 0;
680 showBytes = 0;
681 }else{
@@ -685,17 +688,17 @@
688 }else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
689 zName = azArg[++i];
690 }else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
691 char *z = azArg[++i];
692 if( zName && z && fossil_islower(zName[0]) ){
693 cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z, 1);
694 }
695 showBytes = 1;
696 }else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
697 char *z = azArg[++i];
698 if( zName && z && fossil_islower(zName[0]) ){
699 cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z, 1);
700 }
701 }
702 }
703 }
704 }
@@ -867,12 +870,12 @@
870 g.zIpAddr = mprintf("%s", z);
871 }
872
873 len = atoi(PD("CONTENT_LENGTH", "0"));
874 g.zContentType = zType = P("CONTENT_TYPE");
875 blob_zero(&g.cgiIn);
876 if( len>0 && zType ){
 
877 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
878 || strncmp(zType,"multipart/form-data",19)==0 ){
879 z = fossil_malloc( len+1 );
880 len = fread(z, 1, len, g.httpIn);
881 z[len] = 0;
@@ -987,11 +990,11 @@
990 ** with the given name.
991 */
992 if( fossil_isupper(zName[0]) ){
993 const char *zValue = fossil_getenv(zName);
994 if( zValue ){
995 cgi_set_parameter_nocopy(zName, zValue, 0);
996 CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
997 return zValue;
998 }
999 }
1000 CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
1091 if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
1092 }
1093 cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
1094 }
1095 }
1096
1097 /*
1098 ** Export all query parameters (but not cookies or environment variables)
1099 ** as hidden values of a form.
1100 */
1101 void cgi_query_parameters_to_hidden(void){
1102 int i;
1103 const char *zN, *zV;
1104 for(i=0; i<nUsedQP; i++){
1105 if( aParamQP[i].isQP==0 ) continue;
1106 zN = aParamQP[i].zName;
1107 zV = aParamQP[i].zValue;
1108 @ <input type="hidden" name="%h(zN)" value="%h(zV)">
1109 }
1110 }
1111
1112 /*
1113 ** This routine works like "printf" except that it has the
1114 ** extra formatting capabilities such as %h and %t.
1115 */
1116
+29 -11
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411411
static int seqQP = 0; /* Sequence numbers */
412412
static struct QParam { /* One entry for each query parameter or cookie */
413413
const char *zName; /* Parameter or cookie name */
414414
const char *zValue; /* Value of the query parameter or cookie */
415415
int seq; /* Order of insertion */
416
+ int isQP; /* True for query parameters */
416417
} *aParamQP; /* An array of all parameters and cookies */
417418
418419
/*
419420
** Add another query parameter or cookie to the parameter set.
420421
** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
421422
** is its fully decoded value.
422423
**
423424
** zName and zValue are not copied and must not change or be
424425
** deallocated after this routine returns.
425426
*/
426
-void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
427
+void cgi_set_parameter_nocopy(const char *zName, const char *zValue, int isQP){
427428
if( nAllocQP<=nUsedQP ){
428429
nAllocQP = nAllocQP*2 + 10;
429430
if( nAllocQP>1000 ){
430431
/* Prevent a DOS service attack against the framework */
431432
fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
436437
aParamQP[nUsedQP].zValue = zValue;
437438
if( g.fHttpTrace ){
438439
fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
439440
}
440441
aParamQP[nUsedQP].seq = seqQP++;
442
+ aParamQP[nUsedQP].isQP = isQP;
441443
nUsedQP++;
442444
sortQP = 1;
443445
}
444446
445447
/*
@@ -448,11 +450,11 @@
448450
** is its fully decoded value.
449451
**
450452
** Copies are made of both the zName and zValue parameters.
451453
*/
452454
void cgi_set_parameter(const char *zName, const char *zValue){
453
- cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue));
455
+ cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue), 0);
454456
}
455457
456458
/*
457459
** Replace a parameter with a new value.
458460
*/
@@ -462,19 +464,19 @@
462464
if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
463465
aParamQP[i].zValue = mprintf("%s",zValue);
464466
return;
465467
}
466468
}
467
- cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
469
+ cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
468470
}
469471
470472
/*
471473
** Add a query parameter. The zName portion is fixed but a copy
472474
** must be made of zValue.
473475
*/
474476
void cgi_setenv(const char *zName, const char *zValue){
475
- cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
477
+ cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
476478
}
477479
478480
479481
/*
480482
** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
500502
** The input string "z" is modified but no copies is made. "z"
501503
** should not be deallocated or changed again after this routine
502504
** returns or it will corrupt the parameter table.
503505
*/
504506
static void add_param_list(char *z, int terminator){
507
+ int isQP = terminator=='&';
505508
while( *z ){
506509
char *zName;
507510
char *zValue;
508511
while( fossil_isspace(*z) ){ z++; }
509512
zName = z;
@@ -521,11 +524,11 @@
521524
}else{
522525
if( *z ){ *z++ = 0; }
523526
zValue = "";
524527
}
525528
if( fossil_islower(zName[0]) ){
526
- cgi_set_parameter_nocopy(zName, zValue);
529
+ cgi_set_parameter_nocopy(zName, zValue, isQP);
527530
}
528531
#ifdef FOSSIL_ENABLE_JSON
529532
json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
530533
#endif /* FOSSIL_ENABLE_JSON */
531534
}
@@ -665,14 +668,14 @@
665668
while( (zLine = get_line_from_string(&z, &len))!=0 ){
666669
if( zLine[0]==0 ){
667670
int nContent = 0;
668671
zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
669672
if( zName && zValue && fossil_islower(zName[0]) ){
670
- cgi_set_parameter_nocopy(zName, zValue);
673
+ cgi_set_parameter_nocopy(zName, zValue, 1);
671674
if( showBytes ){
672675
cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
673
- mprintf("%d",nContent));
676
+ mprintf("%d",nContent), 1);
674677
}
675678
}
676679
zName = 0;
677680
showBytes = 0;
678681
}else{
@@ -685,17 +688,17 @@
685688
}else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
686689
zName = azArg[++i];
687690
}else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
688691
char *z = azArg[++i];
689692
if( zName && z && fossil_islower(zName[0]) ){
690
- cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z);
693
+ cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z, 1);
691694
}
692695
showBytes = 1;
693696
}else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
694697
char *z = azArg[++i];
695698
if( zName && z && fossil_islower(zName[0]) ){
696
- cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z);
699
+ cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z, 1);
697700
}
698701
}
699702
}
700703
}
701704
}
@@ -867,12 +870,12 @@
867870
g.zIpAddr = mprintf("%s", z);
868871
}
869872
870873
len = atoi(PD("CONTENT_LENGTH", "0"));
871874
g.zContentType = zType = P("CONTENT_TYPE");
875
+ blob_zero(&g.cgiIn);
872876
if( len>0 && zType ){
873
- blob_zero(&g.cgiIn);
874877
if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
875878
|| strncmp(zType,"multipart/form-data",19)==0 ){
876879
z = fossil_malloc( len+1 );
877880
len = fread(z, 1, len, g.httpIn);
878881
z[len] = 0;
@@ -987,11 +990,11 @@
987990
** with the given name.
988991
*/
989992
if( fossil_isupper(zName[0]) ){
990993
const char *zValue = fossil_getenv(zName);
991994
if( zValue ){
992
- cgi_set_parameter_nocopy(zName, zValue);
995
+ cgi_set_parameter_nocopy(zName, zValue, 0);
993996
CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
994997
return zValue;
995998
}
996999
}
9971000
CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
10881091
if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
10891092
}
10901093
cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
10911094
}
10921095
}
1096
+
1097
+/*
1098
+** Export all query parameters (but not cookies or environment variables)
1099
+** as hidden values of a form.
1100
+*/
1101
+void cgi_query_parameters_to_hidden(void){
1102
+ int i;
1103
+ const char *zN, *zV;
1104
+ for(i=0; i<nUsedQP; i++){
1105
+ if( aParamQP[i].isQP==0 ) continue;
1106
+ zN = aParamQP[i].zName;
1107
+ zV = aParamQP[i].zValue;
1108
+ @ <input type="hidden" name="%h(zN)" value="%h(zV)">
1109
+ }
1110
+}
10931111
10941112
/*
10951113
** This routine works like "printf" except that it has the
10961114
** extra formatting capabilities such as %h and %t.
10971115
*/
10981116
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411 static int seqQP = 0; /* Sequence numbers */
412 static struct QParam { /* One entry for each query parameter or cookie */
413 const char *zName; /* Parameter or cookie name */
414 const char *zValue; /* Value of the query parameter or cookie */
415 int seq; /* Order of insertion */
 
416 } *aParamQP; /* An array of all parameters and cookies */
417
418 /*
419 ** Add another query parameter or cookie to the parameter set.
420 ** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
421 ** is its fully decoded value.
422 **
423 ** zName and zValue are not copied and must not change or be
424 ** deallocated after this routine returns.
425 */
426 void cgi_set_parameter_nocopy(const char *zName, const char *zValue){
427 if( nAllocQP<=nUsedQP ){
428 nAllocQP = nAllocQP*2 + 10;
429 if( nAllocQP>1000 ){
430 /* Prevent a DOS service attack against the framework */
431 fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
436 aParamQP[nUsedQP].zValue = zValue;
437 if( g.fHttpTrace ){
438 fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
439 }
440 aParamQP[nUsedQP].seq = seqQP++;
 
441 nUsedQP++;
442 sortQP = 1;
443 }
444
445 /*
@@ -448,11 +450,11 @@
448 ** is its fully decoded value.
449 **
450 ** Copies are made of both the zName and zValue parameters.
451 */
452 void cgi_set_parameter(const char *zName, const char *zValue){
453 cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue));
454 }
455
456 /*
457 ** Replace a parameter with a new value.
458 */
@@ -462,19 +464,19 @@
462 if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
463 aParamQP[i].zValue = mprintf("%s",zValue);
464 return;
465 }
466 }
467 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
468 }
469
470 /*
471 ** Add a query parameter. The zName portion is fixed but a copy
472 ** must be made of zValue.
473 */
474 void cgi_setenv(const char *zName, const char *zValue){
475 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue));
476 }
477
478
479 /*
480 ** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
500 ** The input string "z" is modified but no copies is made. "z"
501 ** should not be deallocated or changed again after this routine
502 ** returns or it will corrupt the parameter table.
503 */
504 static void add_param_list(char *z, int terminator){
 
505 while( *z ){
506 char *zName;
507 char *zValue;
508 while( fossil_isspace(*z) ){ z++; }
509 zName = z;
@@ -521,11 +524,11 @@
521 }else{
522 if( *z ){ *z++ = 0; }
523 zValue = "";
524 }
525 if( fossil_islower(zName[0]) ){
526 cgi_set_parameter_nocopy(zName, zValue);
527 }
528 #ifdef FOSSIL_ENABLE_JSON
529 json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
530 #endif /* FOSSIL_ENABLE_JSON */
531 }
@@ -665,14 +668,14 @@
665 while( (zLine = get_line_from_string(&z, &len))!=0 ){
666 if( zLine[0]==0 ){
667 int nContent = 0;
668 zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
669 if( zName && zValue && fossil_islower(zName[0]) ){
670 cgi_set_parameter_nocopy(zName, zValue);
671 if( showBytes ){
672 cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
673 mprintf("%d",nContent));
674 }
675 }
676 zName = 0;
677 showBytes = 0;
678 }else{
@@ -685,17 +688,17 @@
685 }else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
686 zName = azArg[++i];
687 }else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
688 char *z = azArg[++i];
689 if( zName && z && fossil_islower(zName[0]) ){
690 cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z);
691 }
692 showBytes = 1;
693 }else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
694 char *z = azArg[++i];
695 if( zName && z && fossil_islower(zName[0]) ){
696 cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z);
697 }
698 }
699 }
700 }
701 }
@@ -867,12 +870,12 @@
867 g.zIpAddr = mprintf("%s", z);
868 }
869
870 len = atoi(PD("CONTENT_LENGTH", "0"));
871 g.zContentType = zType = P("CONTENT_TYPE");
 
872 if( len>0 && zType ){
873 blob_zero(&g.cgiIn);
874 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
875 || strncmp(zType,"multipart/form-data",19)==0 ){
876 z = fossil_malloc( len+1 );
877 len = fread(z, 1, len, g.httpIn);
878 z[len] = 0;
@@ -987,11 +990,11 @@
987 ** with the given name.
988 */
989 if( fossil_isupper(zName[0]) ){
990 const char *zValue = fossil_getenv(zName);
991 if( zValue ){
992 cgi_set_parameter_nocopy(zName, zValue);
993 CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
994 return zValue;
995 }
996 }
997 CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
1088 if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
1089 }
1090 cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
1091 }
1092 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1093
1094 /*
1095 ** This routine works like "printf" except that it has the
1096 ** extra formatting capabilities such as %h and %t.
1097 */
1098
--- src/cgi.c
+++ src/cgi.c
@@ -411,10 +411,11 @@
411 static int seqQP = 0; /* Sequence numbers */
412 static struct QParam { /* One entry for each query parameter or cookie */
413 const char *zName; /* Parameter or cookie name */
414 const char *zValue; /* Value of the query parameter or cookie */
415 int seq; /* Order of insertion */
416 int isQP; /* True for query parameters */
417 } *aParamQP; /* An array of all parameters and cookies */
418
419 /*
420 ** Add another query parameter or cookie to the parameter set.
421 ** zName is the name of the query parameter or cookie and zValue
@@ -421,11 +422,11 @@
422 ** is its fully decoded value.
423 **
424 ** zName and zValue are not copied and must not change or be
425 ** deallocated after this routine returns.
426 */
427 void cgi_set_parameter_nocopy(const char *zName, const char *zValue, int isQP){
428 if( nAllocQP<=nUsedQP ){
429 nAllocQP = nAllocQP*2 + 10;
430 if( nAllocQP>1000 ){
431 /* Prevent a DOS service attack against the framework */
432 fossil_fatal("Too many query parameters");
@@ -436,10 +437,11 @@
437 aParamQP[nUsedQP].zValue = zValue;
438 if( g.fHttpTrace ){
439 fprintf(stderr, "# cgi: %s = [%s]\n", zName, zValue);
440 }
441 aParamQP[nUsedQP].seq = seqQP++;
442 aParamQP[nUsedQP].isQP = isQP;
443 nUsedQP++;
444 sortQP = 1;
445 }
446
447 /*
@@ -448,11 +450,11 @@
450 ** is its fully decoded value.
451 **
452 ** Copies are made of both the zName and zValue parameters.
453 */
454 void cgi_set_parameter(const char *zName, const char *zValue){
455 cgi_set_parameter_nocopy(mprintf("%s",zName), mprintf("%s",zValue), 0);
456 }
457
458 /*
459 ** Replace a parameter with a new value.
460 */
@@ -462,19 +464,19 @@
464 if( fossil_strcmp(aParamQP[i].zName,zName)==0 ){
465 aParamQP[i].zValue = mprintf("%s",zValue);
466 return;
467 }
468 }
469 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
470 }
471
472 /*
473 ** Add a query parameter. The zName portion is fixed but a copy
474 ** must be made of zValue.
475 */
476 void cgi_setenv(const char *zName, const char *zValue){
477 cgi_set_parameter_nocopy(zName, mprintf("%s",zValue), 0);
478 }
479
480
481 /*
482 ** Add a list of query parameters or cookies to the parameter set.
@@ -500,10 +502,11 @@
502 ** The input string "z" is modified but no copies is made. "z"
503 ** should not be deallocated or changed again after this routine
504 ** returns or it will corrupt the parameter table.
505 */
506 static void add_param_list(char *z, int terminator){
507 int isQP = terminator=='&';
508 while( *z ){
509 char *zName;
510 char *zValue;
511 while( fossil_isspace(*z) ){ z++; }
512 zName = z;
@@ -521,11 +524,11 @@
524 }else{
525 if( *z ){ *z++ = 0; }
526 zValue = "";
527 }
528 if( fossil_islower(zName[0]) ){
529 cgi_set_parameter_nocopy(zName, zValue, isQP);
530 }
531 #ifdef FOSSIL_ENABLE_JSON
532 json_setenv( zName, cson_value_new_string(zValue,strlen(zValue)) );
533 #endif /* FOSSIL_ENABLE_JSON */
534 }
@@ -665,14 +668,14 @@
668 while( (zLine = get_line_from_string(&z, &len))!=0 ){
669 if( zLine[0]==0 ){
670 int nContent = 0;
671 zValue = get_bounded_content(&z, &len, zBoundry, &nContent);
672 if( zName && zValue && fossil_islower(zName[0]) ){
673 cgi_set_parameter_nocopy(zName, zValue, 1);
674 if( showBytes ){
675 cgi_set_parameter_nocopy(mprintf("%s:bytes", zName),
676 mprintf("%d",nContent), 1);
677 }
678 }
679 zName = 0;
680 showBytes = 0;
681 }else{
@@ -685,17 +688,17 @@
688 }else if( c=='n' && sqlite3_strnicmp(azArg[i],"name=",n)==0 ){
689 zName = azArg[++i];
690 }else if( c=='f' && sqlite3_strnicmp(azArg[i],"filename=",n)==0 ){
691 char *z = azArg[++i];
692 if( zName && z && fossil_islower(zName[0]) ){
693 cgi_set_parameter_nocopy(mprintf("%s:filename",zName), z, 1);
694 }
695 showBytes = 1;
696 }else if( c=='c' && sqlite3_strnicmp(azArg[i],"content-type:",n)==0 ){
697 char *z = azArg[++i];
698 if( zName && z && fossil_islower(zName[0]) ){
699 cgi_set_parameter_nocopy(mprintf("%s:mimetype",zName), z, 1);
700 }
701 }
702 }
703 }
704 }
@@ -867,12 +870,12 @@
870 g.zIpAddr = mprintf("%s", z);
871 }
872
873 len = atoi(PD("CONTENT_LENGTH", "0"));
874 g.zContentType = zType = P("CONTENT_TYPE");
875 blob_zero(&g.cgiIn);
876 if( len>0 && zType ){
 
877 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
878 || strncmp(zType,"multipart/form-data",19)==0 ){
879 z = fossil_malloc( len+1 );
880 len = fread(z, 1, len, g.httpIn);
881 z[len] = 0;
@@ -987,11 +990,11 @@
990 ** with the given name.
991 */
992 if( fossil_isupper(zName[0]) ){
993 const char *zValue = fossil_getenv(zName);
994 if( zValue ){
995 cgi_set_parameter_nocopy(zName, zValue, 0);
996 CGIDEBUG(("env-match [%s] = [%s]\n", zName, zValue));
997 return zValue;
998 }
999 }
1000 CGIDEBUG(("no-match [%s]\n", zName));
@@ -1088,10 +1091,25 @@
1091 if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
1092 }
1093 cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
1094 }
1095 }
1096
1097 /*
1098 ** Export all query parameters (but not cookies or environment variables)
1099 ** as hidden values of a form.
1100 */
1101 void cgi_query_parameters_to_hidden(void){
1102 int i;
1103 const char *zN, *zV;
1104 for(i=0; i<nUsedQP; i++){
1105 if( aParamQP[i].isQP==0 ) continue;
1106 zN = aParamQP[i].zName;
1107 zV = aParamQP[i].zValue;
1108 @ <input type="hidden" name="%h(zN)" value="%h(zV)">
1109 }
1110 }
1111
1112 /*
1113 ** This routine works like "printf" except that it has the
1114 ** extra formatting capabilities such as %h and %t.
1115 */
1116
+59 -64
--- src/checkin.c
+++ src/checkin.c
@@ -623,13 +623,17 @@
623623
zEditor = fossil_getenv("VISUAL");
624624
}
625625
if( zEditor==0 ){
626626
zEditor = fossil_getenv("EDITOR");
627627
}
628
-#ifdef _WIN32
628
+#if defined(_WIN32) || defined(__CYGWIN__)
629629
if( zEditor==0 ){
630
- zEditor = mprintf("%s\\notepad.exe", fossil_getenv("SystemRoot"));
630
+ zEditor = mprintf("%s\\notepad.exe", fossil_getenv("SYSTEMROOT"));
631
+#if defined(__CYGWIN__)
632
+ zEditor = fossil_utf8_to_filename(zEditor);
633
+ blob_add_cr(pPrompt);
634
+#endif
631635
}
632636
#endif
633637
if( zEditor==0 ){
634638
blob_append(pPrompt,
635639
"#\n"
@@ -901,12 +905,11 @@
901905
){
902906
char *zDate; /* Date of the check-in */
903907
char *zParentUuid; /* UUID of parent check-in */
904908
Blob filename; /* A single filename */
905909
int nBasename; /* Size of base filename */
906
- Stmt q; /* Query of files changed */
907
- Stmt q2; /* Query of merge parents */
910
+ Stmt q; /* Various queries */
908911
Blob mcksum; /* Manifest checksum */
909912
ManifestFile *pFile; /* File from the baseline */
910913
int nFBcard = 0; /* Number of B-cards and F-cards */
911914
int i; /* Loop counter */
912915
const char *zColor; /* Modified value of p->zColor */
@@ -921,11 +924,15 @@
921924
pFile = manifest_file_next(pBaseline, 0);
922925
nFBcard++;
923926
}else{
924927
pFile = 0;
925928
}
926
- blob_appendf(pOut, "C %F\n", blob_str(p->pComment));
929
+ if( blob_size(p->pComment)!=0 ){
930
+ blob_appendf(pOut, "C %F\n", blob_str(p->pComment));
931
+ }else{
932
+ blob_append(pOut, "C (no\\scomment)\n", 16);
933
+ }
927934
zDate = date_in_standard_format(p->zDateOvrd ? p->zDateOvrd : "now");
928935
blob_appendf(pOut, "D %s\n", zDate);
929936
zDate[10] = ' ';
930937
db_prepare(&q,
931938
"SELECT pathname, uuid, origname, blob.rid, isexe, islink,"
@@ -1006,37 +1013,40 @@
10061013
blob_appendf(pOut, "N %F\n", p->zMimetype);
10071014
}
10081015
blob_appendf(pOut, "P %s", zParentUuid);
10091016
if( p->verifyDate ) checkin_verify_younger(vid, zParentUuid, zDate);
10101017
free(zParentUuid);
1011
- db_prepare(&q2, "SELECT merge FROM vmerge WHERE id=0 OR id<-2");
1012
- while( db_step(&q2)==SQLITE_ROW ){
1018
+ db_prepare(&q, "SELECT merge FROM vmerge WHERE id=0 OR id<-2");
1019
+ while( db_step(&q)==SQLITE_ROW ){
10131020
char *zMergeUuid;
1014
- int mid = db_column_int(&q2, 0);
1015
- if( !g.markPrivate && content_is_private(mid) ) continue;
1021
+ int mid = db_column_int(&q, 0);
1022
+ if( (!g.markPrivate && content_is_private(mid)) || (mid == vid) ) continue;
10161023
zMergeUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
10171024
if( zMergeUuid ){
10181025
blob_appendf(pOut, " %s", zMergeUuid);
10191026
if( p->verifyDate ) checkin_verify_younger(mid, zMergeUuid, zDate);
10201027
free(zMergeUuid);
10211028
}
10221029
}
1023
- db_finalize(&q2);
1030
+ db_finalize(&q);
10241031
free(zDate);
10251032
blob_appendf(pOut, "\n");
10261033
1027
- db_prepare(&q2,
1028
- "SELECT CASE vmerge.id WHEN -1 THEN '+' ELSE '-' END || blob.uuid"
1034
+ db_prepare(&q,
1035
+ "SELECT CASE vmerge.id WHEN -1 THEN '+' ELSE '-' END || blob.uuid, merge"
10291036
" FROM vmerge, blob"
10301037
" WHERE (vmerge.id=-1 OR vmerge.id=-2)"
10311038
" AND blob.rid=vmerge.merge"
10321039
" ORDER BY 1");
1033
- while( db_step(&q2)==SQLITE_ROW ){
1034
- const char *zCherrypickUuid = db_column_text(&q2, 0);
1035
- blob_appendf(pOut, "Q %s\n", zCherrypickUuid);
1040
+ while( db_step(&q)==SQLITE_ROW ){
1041
+ const char *zCherrypickUuid = db_column_text(&q, 0);
1042
+ int mid = db_column_int(&q, 1);
1043
+ if( mid != vid ){
1044
+ blob_appendf(pOut, "Q %s\n", zCherrypickUuid);
1045
+ }
10361046
}
1037
- db_finalize(&q2);
1047
+ db_finalize(&q);
10381048
10391049
if( p->pCksum ) blob_appendf(pOut, "R %b\n", p->pCksum);
10401050
zColor = p->zColor;
10411051
if( p->zBranch && p->zBranch[0] ){
10421052
/* Set tags for the new branch */
@@ -1049,10 +1059,22 @@
10491059
}
10501060
if( zColor && zColor[0] ){
10511061
/* One-time background color */
10521062
blob_appendf(pOut, "T +bgcolor * %F\n", zColor);
10531063
}
1064
+ db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
1065
+ " WHERE id=-4 ORDER BY 1");
1066
+ while( db_step(&q)==SQLITE_ROW ){
1067
+ const char *zIntegrateUuid = db_column_text(&q, 0);
1068
+ int rid = db_column_int(&q, 1);
1069
+ if( is_a_leaf(rid) && !db_exists("SELECT 1 FROM tagxref "
1070
+ " WHERE tagid=%d AND rid=%d AND tagtype>0", TAG_CLOSED, rid)){
1071
+ blob_appendf(pOut, "T +closed %s\n", zIntegrateUuid);
1072
+ }
1073
+ }
1074
+ db_finalize(&q);
1075
+
10541076
if( p->azTag ){
10551077
for(i=0; p->azTag[i]; i++){
10561078
/* Add a symbolic tag to this check-in. The tag names have already
10571079
** been sorted and converted using the %F format */
10581080
assert( i==0 || strcmp(p->azTag[i-1], p->azTag[i])<=0 );
@@ -1059,11 +1081,10 @@
10591081
blob_appendf(pOut, "T +sym-%s *\n", p->azTag[i]);
10601082
}
10611083
}
10621084
if( p->zBranch && p->zBranch[0] ){
10631085
/* For a new branch, cancel all prior propagating tags */
1064
- Stmt q;
10651086
db_prepare(&q,
10661087
"SELECT tagname FROM tagxref, tag"
10671088
" WHERE tagxref.rid=%d AND tagxref.tagid=tag.tagid"
10681089
" AND tagtype==2 AND tagname GLOB 'sym-*'"
10691090
" AND tagname!='sym-'||%Q"
@@ -1298,11 +1319,11 @@
12981319
int vid; /* blob-id of parent version */
12991320
int nrid; /* blob-id of a modified file */
13001321
int nvid; /* Blob-id of the new check-in */
13011322
Blob comment; /* Check-in comment */
13021323
const char *zComment; /* Check-in comment */
1303
- Stmt q; /* Query to find files that have been modified */
1324
+ Stmt q; /* Various queries */
13041325
char *zUuid; /* UUID of the new check-in */
13051326
int noSign = 0; /* True to omit signing the manifest using GPG */
13061327
int isAMerge = 0; /* True if checking in a merge */
13071328
int noWarningFlag = 0; /* True if skipping all warnings */
13081329
int forceFlag = 0; /* Undocumented: Disables all checks */
@@ -1451,25 +1472,24 @@
14511472
** will generate a manifest that has two fileA entries, which is illegal.
14521473
** When you think about it, the sequence above makes no sense. So detect
14531474
** it and disallow it. Ticket [0ff64b0a5fc8].
14541475
*/
14551476
if( g.aCommitFile ){
1456
- Stmt qRename;
1457
- db_prepare(&qRename,
1477
+ db_prepare(&q,
14581478
"SELECT v1.pathname, v2.pathname"
14591479
" FROM vfile AS v1, vfile AS v2"
14601480
" WHERE is_selected(v1.id)"
14611481
" AND v2.origname IS NOT NULL"
14621482
" AND v2.origname=v1.pathname"
14631483
" AND NOT is_selected(v2.id)");
1464
- if( db_step(&qRename)==SQLITE_ROW ){
1465
- const char *zFrom = db_column_text(&qRename, 0);
1466
- const char *zTo = db_column_text(&qRename, 1);
1484
+ if( db_step(&q)==SQLITE_ROW ){
1485
+ const char *zFrom = db_column_text(&q, 0);
1486
+ const char *zTo = db_column_text(&q, 1);
14671487
fossil_fatal("cannot do a partial commit of '%s' without '%s' because "
14681488
"'%s' was renamed to '%s'", zFrom, zTo, zFrom, zTo);
14691489
}
1470
- db_finalize(&qRename);
1490
+ db_finalize(&q);
14711491
}
14721492
14731493
user_select();
14741494
/*
14751495
** Check that the user exists.
@@ -1528,13 +1548,10 @@
15281548
blob_zero(&comment);
15291549
blob_read_from_file(&comment, zComFile);
15301550
blob_to_utf8_no_bom(&comment, 1);
15311551
}else if(dryRunFlag){
15321552
blob_zero(&comment);
1533
- blob_append(&comment, "Dry-run mode - no comment provided.", -1)
1534
- /* Comment needed to avoid downstream assertion. */
1535
- ;
15361553
}else{
15371554
char *zInit = db_text(0, "SELECT value FROM vvar WHERE name='ci-comment'");
15381555
prepare_commit_comment(&comment, zInit, &sCiInfo, vid);
15391556
if( zInit && zInit[0] && fossil_strcmp(zInit, blob_str(&comment))==0 ){
15401557
blob_zero(&ans);
@@ -1543,15 +1560,17 @@
15431560
if( cReply!='y' && cReply!='Y' ) fossil_exit(1);;
15441561
}
15451562
free(zInit);
15461563
}
15471564
if( blob_size(&comment)==0 ){
1548
- blob_zero(&ans);
1549
- prompt_user("empty check-in comment. continue (y/N)? ", &ans);
1550
- cReply = blob_str(&ans)[0];
1551
- if( cReply!='y' && cReply!='Y' ){
1552
- fossil_exit(1);
1565
+ if( !dryRunFlag ){
1566
+ blob_zero(&ans);
1567
+ prompt_user("empty check-in comment. continue (y/N)? ", &ans);
1568
+ cReply = blob_str(&ans)[0];
1569
+ if( cReply!='y' && cReply!='Y' ){
1570
+ fossil_exit(1);
1571
+ }
15531572
}
15541573
}else{
15551574
db_multi_exec("REPLACE INTO vvar VALUES('ci-comment',%B)", &comment);
15561575
db_end_transaction(0);
15571576
db_begin_transaction();
@@ -1620,13 +1639,10 @@
16201639
fossil_fatal("one or more files were converted on your request; "
16211640
"please re-test before committing");
16221641
}
16231642
16241643
/* Create the new manifest */
1625
- if( blob_size(&comment)==0 ){
1626
- blob_append(&comment, "(no comment)", -1);
1627
- }
16281644
sCiInfo.pComment = &comment;
16291645
sCiInfo.pCksum = useCksum ? &cksum1 : 0;
16301646
sCiInfo.verifyDate = !allowOlder && !forceFlag;
16311647
if( forceDelta ){
16321648
blob_zero(&manifest);
@@ -1674,11 +1690,11 @@
16741690
manifest = delta;
16751691
}else{
16761692
blob_reset(&delta);
16771693
}
16781694
}else if( forceDelta ){
1679
- fossil_panic("unable to find a baseline-manifest for the delta");
1695
+ fossil_fatal("unable to find a baseline-manifest for the delta");
16801696
}
16811697
}
16821698
if( !noSign && !g.markPrivate && clearsign(&manifest, &manifest) ){
16831699
blob_zero(&ans);
16841700
prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
@@ -1702,51 +1718,30 @@
17021718
free(zManifestFile);
17031719
}
17041720
17051721
nvid = content_put(&manifest);
17061722
if( nvid==0 ){
1707
- fossil_panic("trouble committing manifest: %s", g.zErrMsg);
1723
+ fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
17081724
}
17091725
db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nvid);
17101726
manifest_crosslink(nvid, &manifest);
1727
+ assert( blob_is_reset(&manifest) );
1728
+ content_deltify(vid, nvid, 0);
1729
+ zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", nvid);
17111730
17121731
db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
17131732
" WHERE id=-4");
17141733
while( db_step(&q)==SQLITE_ROW ){
17151734
const char *zIntegrateUuid = db_column_text(&q, 0);
1716
- int rid = db_column_int(&q, 1);
1717
- if( !is_a_leaf(rid) ){
1718
- fossil_print("Not_Closed: %s (not a leaf any more)\n", zIntegrateUuid);
1735
+ if( is_a_leaf(db_column_int(&q, 1)) ){
1736
+ fossil_print("Closed: %s\n", zIntegrateUuid);
17191737
}else{
1720
- if (!db_exists("SELECT 1 FROM tagxref "
1721
- " WHERE tagid=%d AND rid=%d AND tagtype>0",
1722
- TAG_CLOSED, rid)
1723
- ){
1724
- Blob ctrl;
1725
- Blob cksum;
1726
- char *zDate;
1727
- int nrid;
1728
-
1729
- blob_zero(&ctrl);
1730
- zDate = date_in_standard_format(sCiInfo.zDateOvrd ? sCiInfo.zDateOvrd : "now");
1731
- blob_appendf(&ctrl, "D %s\n", zDate);
1732
- blob_appendf(&ctrl, "T +closed %s by\\smerge\\s--integrate\n", zIntegrateUuid);
1733
- blob_appendf(&ctrl, "U %F\n", sCiInfo.zUserOvrd ? sCiInfo.zUserOvrd : g.zLogin);
1734
- md5sum_blob(&ctrl, &cksum);
1735
- blob_appendf(&ctrl, "Z %b\n", &cksum);
1736
- nrid = content_put(&ctrl);
1737
- manifest_crosslink(nrid, &ctrl);
1738
- assert( blob_is_reset(&ctrl) );
1739
- }
1740
- fossil_print("Closed: %s\n", zIntegrateUuid);
1738
+ fossil_print("Not_Closed: %s (not a leaf any more)\n", zIntegrateUuid);
17411739
}
17421740
}
17431741
db_finalize(&q);
17441742
1745
- assert( blob_is_reset(&manifest) );
1746
- content_deltify(vid, nvid, 0);
1747
- zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", nvid);
17481743
fossil_print("New_Version: %s\n", zUuid);
17491744
if( outputManifest ){
17501745
zManifestFile = mprintf("%smanifest.uuid", g.zLocalRoot);
17511746
blob_zero(&muuid);
17521747
blob_appendf(&muuid, "%s\n", zUuid);
17531748
--- src/checkin.c
+++ src/checkin.c
@@ -623,13 +623,17 @@
623 zEditor = fossil_getenv("VISUAL");
624 }
625 if( zEditor==0 ){
626 zEditor = fossil_getenv("EDITOR");
627 }
628 #ifdef _WIN32
629 if( zEditor==0 ){
630 zEditor = mprintf("%s\\notepad.exe", fossil_getenv("SystemRoot"));
 
 
 
 
631 }
632 #endif
633 if( zEditor==0 ){
634 blob_append(pPrompt,
635 "#\n"
@@ -901,12 +905,11 @@
901 ){
902 char *zDate; /* Date of the check-in */
903 char *zParentUuid; /* UUID of parent check-in */
904 Blob filename; /* A single filename */
905 int nBasename; /* Size of base filename */
906 Stmt q; /* Query of files changed */
907 Stmt q2; /* Query of merge parents */
908 Blob mcksum; /* Manifest checksum */
909 ManifestFile *pFile; /* File from the baseline */
910 int nFBcard = 0; /* Number of B-cards and F-cards */
911 int i; /* Loop counter */
912 const char *zColor; /* Modified value of p->zColor */
@@ -921,11 +924,15 @@
921 pFile = manifest_file_next(pBaseline, 0);
922 nFBcard++;
923 }else{
924 pFile = 0;
925 }
926 blob_appendf(pOut, "C %F\n", blob_str(p->pComment));
 
 
 
 
927 zDate = date_in_standard_format(p->zDateOvrd ? p->zDateOvrd : "now");
928 blob_appendf(pOut, "D %s\n", zDate);
929 zDate[10] = ' ';
930 db_prepare(&q,
931 "SELECT pathname, uuid, origname, blob.rid, isexe, islink,"
@@ -1006,37 +1013,40 @@
1006 blob_appendf(pOut, "N %F\n", p->zMimetype);
1007 }
1008 blob_appendf(pOut, "P %s", zParentUuid);
1009 if( p->verifyDate ) checkin_verify_younger(vid, zParentUuid, zDate);
1010 free(zParentUuid);
1011 db_prepare(&q2, "SELECT merge FROM vmerge WHERE id=0 OR id<-2");
1012 while( db_step(&q2)==SQLITE_ROW ){
1013 char *zMergeUuid;
1014 int mid = db_column_int(&q2, 0);
1015 if( !g.markPrivate && content_is_private(mid) ) continue;
1016 zMergeUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
1017 if( zMergeUuid ){
1018 blob_appendf(pOut, " %s", zMergeUuid);
1019 if( p->verifyDate ) checkin_verify_younger(mid, zMergeUuid, zDate);
1020 free(zMergeUuid);
1021 }
1022 }
1023 db_finalize(&q2);
1024 free(zDate);
1025 blob_appendf(pOut, "\n");
1026
1027 db_prepare(&q2,
1028 "SELECT CASE vmerge.id WHEN -1 THEN '+' ELSE '-' END || blob.uuid"
1029 " FROM vmerge, blob"
1030 " WHERE (vmerge.id=-1 OR vmerge.id=-2)"
1031 " AND blob.rid=vmerge.merge"
1032 " ORDER BY 1");
1033 while( db_step(&q2)==SQLITE_ROW ){
1034 const char *zCherrypickUuid = db_column_text(&q2, 0);
1035 blob_appendf(pOut, "Q %s\n", zCherrypickUuid);
 
 
 
1036 }
1037 db_finalize(&q2);
1038
1039 if( p->pCksum ) blob_appendf(pOut, "R %b\n", p->pCksum);
1040 zColor = p->zColor;
1041 if( p->zBranch && p->zBranch[0] ){
1042 /* Set tags for the new branch */
@@ -1049,10 +1059,22 @@
1049 }
1050 if( zColor && zColor[0] ){
1051 /* One-time background color */
1052 blob_appendf(pOut, "T +bgcolor * %F\n", zColor);
1053 }
 
 
 
 
 
 
 
 
 
 
 
 
1054 if( p->azTag ){
1055 for(i=0; p->azTag[i]; i++){
1056 /* Add a symbolic tag to this check-in. The tag names have already
1057 ** been sorted and converted using the %F format */
1058 assert( i==0 || strcmp(p->azTag[i-1], p->azTag[i])<=0 );
@@ -1059,11 +1081,10 @@
1059 blob_appendf(pOut, "T +sym-%s *\n", p->azTag[i]);
1060 }
1061 }
1062 if( p->zBranch && p->zBranch[0] ){
1063 /* For a new branch, cancel all prior propagating tags */
1064 Stmt q;
1065 db_prepare(&q,
1066 "SELECT tagname FROM tagxref, tag"
1067 " WHERE tagxref.rid=%d AND tagxref.tagid=tag.tagid"
1068 " AND tagtype==2 AND tagname GLOB 'sym-*'"
1069 " AND tagname!='sym-'||%Q"
@@ -1298,11 +1319,11 @@
1298 int vid; /* blob-id of parent version */
1299 int nrid; /* blob-id of a modified file */
1300 int nvid; /* Blob-id of the new check-in */
1301 Blob comment; /* Check-in comment */
1302 const char *zComment; /* Check-in comment */
1303 Stmt q; /* Query to find files that have been modified */
1304 char *zUuid; /* UUID of the new check-in */
1305 int noSign = 0; /* True to omit signing the manifest using GPG */
1306 int isAMerge = 0; /* True if checking in a merge */
1307 int noWarningFlag = 0; /* True if skipping all warnings */
1308 int forceFlag = 0; /* Undocumented: Disables all checks */
@@ -1451,25 +1472,24 @@
1451 ** will generate a manifest that has two fileA entries, which is illegal.
1452 ** When you think about it, the sequence above makes no sense. So detect
1453 ** it and disallow it. Ticket [0ff64b0a5fc8].
1454 */
1455 if( g.aCommitFile ){
1456 Stmt qRename;
1457 db_prepare(&qRename,
1458 "SELECT v1.pathname, v2.pathname"
1459 " FROM vfile AS v1, vfile AS v2"
1460 " WHERE is_selected(v1.id)"
1461 " AND v2.origname IS NOT NULL"
1462 " AND v2.origname=v1.pathname"
1463 " AND NOT is_selected(v2.id)");
1464 if( db_step(&qRename)==SQLITE_ROW ){
1465 const char *zFrom = db_column_text(&qRename, 0);
1466 const char *zTo = db_column_text(&qRename, 1);
1467 fossil_fatal("cannot do a partial commit of '%s' without '%s' because "
1468 "'%s' was renamed to '%s'", zFrom, zTo, zFrom, zTo);
1469 }
1470 db_finalize(&qRename);
1471 }
1472
1473 user_select();
1474 /*
1475 ** Check that the user exists.
@@ -1528,13 +1548,10 @@
1528 blob_zero(&comment);
1529 blob_read_from_file(&comment, zComFile);
1530 blob_to_utf8_no_bom(&comment, 1);
1531 }else if(dryRunFlag){
1532 blob_zero(&comment);
1533 blob_append(&comment, "Dry-run mode - no comment provided.", -1)
1534 /* Comment needed to avoid downstream assertion. */
1535 ;
1536 }else{
1537 char *zInit = db_text(0, "SELECT value FROM vvar WHERE name='ci-comment'");
1538 prepare_commit_comment(&comment, zInit, &sCiInfo, vid);
1539 if( zInit && zInit[0] && fossil_strcmp(zInit, blob_str(&comment))==0 ){
1540 blob_zero(&ans);
@@ -1543,15 +1560,17 @@
1543 if( cReply!='y' && cReply!='Y' ) fossil_exit(1);;
1544 }
1545 free(zInit);
1546 }
1547 if( blob_size(&comment)==0 ){
1548 blob_zero(&ans);
1549 prompt_user("empty check-in comment. continue (y/N)? ", &ans);
1550 cReply = blob_str(&ans)[0];
1551 if( cReply!='y' && cReply!='Y' ){
1552 fossil_exit(1);
 
 
1553 }
1554 }else{
1555 db_multi_exec("REPLACE INTO vvar VALUES('ci-comment',%B)", &comment);
1556 db_end_transaction(0);
1557 db_begin_transaction();
@@ -1620,13 +1639,10 @@
1620 fossil_fatal("one or more files were converted on your request; "
1621 "please re-test before committing");
1622 }
1623
1624 /* Create the new manifest */
1625 if( blob_size(&comment)==0 ){
1626 blob_append(&comment, "(no comment)", -1);
1627 }
1628 sCiInfo.pComment = &comment;
1629 sCiInfo.pCksum = useCksum ? &cksum1 : 0;
1630 sCiInfo.verifyDate = !allowOlder && !forceFlag;
1631 if( forceDelta ){
1632 blob_zero(&manifest);
@@ -1674,11 +1690,11 @@
1674 manifest = delta;
1675 }else{
1676 blob_reset(&delta);
1677 }
1678 }else if( forceDelta ){
1679 fossil_panic("unable to find a baseline-manifest for the delta");
1680 }
1681 }
1682 if( !noSign && !g.markPrivate && clearsign(&manifest, &manifest) ){
1683 blob_zero(&ans);
1684 prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
@@ -1702,51 +1718,30 @@
1702 free(zManifestFile);
1703 }
1704
1705 nvid = content_put(&manifest);
1706 if( nvid==0 ){
1707 fossil_panic("trouble committing manifest: %s", g.zErrMsg);
1708 }
1709 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nvid);
1710 manifest_crosslink(nvid, &manifest);
 
 
 
1711
1712 db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
1713 " WHERE id=-4");
1714 while( db_step(&q)==SQLITE_ROW ){
1715 const char *zIntegrateUuid = db_column_text(&q, 0);
1716 int rid = db_column_int(&q, 1);
1717 if( !is_a_leaf(rid) ){
1718 fossil_print("Not_Closed: %s (not a leaf any more)\n", zIntegrateUuid);
1719 }else{
1720 if (!db_exists("SELECT 1 FROM tagxref "
1721 " WHERE tagid=%d AND rid=%d AND tagtype>0",
1722 TAG_CLOSED, rid)
1723 ){
1724 Blob ctrl;
1725 Blob cksum;
1726 char *zDate;
1727 int nrid;
1728
1729 blob_zero(&ctrl);
1730 zDate = date_in_standard_format(sCiInfo.zDateOvrd ? sCiInfo.zDateOvrd : "now");
1731 blob_appendf(&ctrl, "D %s\n", zDate);
1732 blob_appendf(&ctrl, "T +closed %s by\\smerge\\s--integrate\n", zIntegrateUuid);
1733 blob_appendf(&ctrl, "U %F\n", sCiInfo.zUserOvrd ? sCiInfo.zUserOvrd : g.zLogin);
1734 md5sum_blob(&ctrl, &cksum);
1735 blob_appendf(&ctrl, "Z %b\n", &cksum);
1736 nrid = content_put(&ctrl);
1737 manifest_crosslink(nrid, &ctrl);
1738 assert( blob_is_reset(&ctrl) );
1739 }
1740 fossil_print("Closed: %s\n", zIntegrateUuid);
1741 }
1742 }
1743 db_finalize(&q);
1744
1745 assert( blob_is_reset(&manifest) );
1746 content_deltify(vid, nvid, 0);
1747 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", nvid);
1748 fossil_print("New_Version: %s\n", zUuid);
1749 if( outputManifest ){
1750 zManifestFile = mprintf("%smanifest.uuid", g.zLocalRoot);
1751 blob_zero(&muuid);
1752 blob_appendf(&muuid, "%s\n", zUuid);
1753
--- src/checkin.c
+++ src/checkin.c
@@ -623,13 +623,17 @@
623 zEditor = fossil_getenv("VISUAL");
624 }
625 if( zEditor==0 ){
626 zEditor = fossil_getenv("EDITOR");
627 }
628 #if defined(_WIN32) || defined(__CYGWIN__)
629 if( zEditor==0 ){
630 zEditor = mprintf("%s\\notepad.exe", fossil_getenv("SYSTEMROOT"));
631 #if defined(__CYGWIN__)
632 zEditor = fossil_utf8_to_filename(zEditor);
633 blob_add_cr(pPrompt);
634 #endif
635 }
636 #endif
637 if( zEditor==0 ){
638 blob_append(pPrompt,
639 "#\n"
@@ -901,12 +905,11 @@
905 ){
906 char *zDate; /* Date of the check-in */
907 char *zParentUuid; /* UUID of parent check-in */
908 Blob filename; /* A single filename */
909 int nBasename; /* Size of base filename */
910 Stmt q; /* Various queries */
 
911 Blob mcksum; /* Manifest checksum */
912 ManifestFile *pFile; /* File from the baseline */
913 int nFBcard = 0; /* Number of B-cards and F-cards */
914 int i; /* Loop counter */
915 const char *zColor; /* Modified value of p->zColor */
@@ -921,11 +924,15 @@
924 pFile = manifest_file_next(pBaseline, 0);
925 nFBcard++;
926 }else{
927 pFile = 0;
928 }
929 if( blob_size(p->pComment)!=0 ){
930 blob_appendf(pOut, "C %F\n", blob_str(p->pComment));
931 }else{
932 blob_append(pOut, "C (no\\scomment)\n", 16);
933 }
934 zDate = date_in_standard_format(p->zDateOvrd ? p->zDateOvrd : "now");
935 blob_appendf(pOut, "D %s\n", zDate);
936 zDate[10] = ' ';
937 db_prepare(&q,
938 "SELECT pathname, uuid, origname, blob.rid, isexe, islink,"
@@ -1006,37 +1013,40 @@
1013 blob_appendf(pOut, "N %F\n", p->zMimetype);
1014 }
1015 blob_appendf(pOut, "P %s", zParentUuid);
1016 if( p->verifyDate ) checkin_verify_younger(vid, zParentUuid, zDate);
1017 free(zParentUuid);
1018 db_prepare(&q, "SELECT merge FROM vmerge WHERE id=0 OR id<-2");
1019 while( db_step(&q)==SQLITE_ROW ){
1020 char *zMergeUuid;
1021 int mid = db_column_int(&q, 0);
1022 if( (!g.markPrivate && content_is_private(mid)) || (mid == vid) ) continue;
1023 zMergeUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", mid);
1024 if( zMergeUuid ){
1025 blob_appendf(pOut, " %s", zMergeUuid);
1026 if( p->verifyDate ) checkin_verify_younger(mid, zMergeUuid, zDate);
1027 free(zMergeUuid);
1028 }
1029 }
1030 db_finalize(&q);
1031 free(zDate);
1032 blob_appendf(pOut, "\n");
1033
1034 db_prepare(&q,
1035 "SELECT CASE vmerge.id WHEN -1 THEN '+' ELSE '-' END || blob.uuid, merge"
1036 " FROM vmerge, blob"
1037 " WHERE (vmerge.id=-1 OR vmerge.id=-2)"
1038 " AND blob.rid=vmerge.merge"
1039 " ORDER BY 1");
1040 while( db_step(&q)==SQLITE_ROW ){
1041 const char *zCherrypickUuid = db_column_text(&q, 0);
1042 int mid = db_column_int(&q, 1);
1043 if( mid != vid ){
1044 blob_appendf(pOut, "Q %s\n", zCherrypickUuid);
1045 }
1046 }
1047 db_finalize(&q);
1048
1049 if( p->pCksum ) blob_appendf(pOut, "R %b\n", p->pCksum);
1050 zColor = p->zColor;
1051 if( p->zBranch && p->zBranch[0] ){
1052 /* Set tags for the new branch */
@@ -1049,10 +1059,22 @@
1059 }
1060 if( zColor && zColor[0] ){
1061 /* One-time background color */
1062 blob_appendf(pOut, "T +bgcolor * %F\n", zColor);
1063 }
1064 db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
1065 " WHERE id=-4 ORDER BY 1");
1066 while( db_step(&q)==SQLITE_ROW ){
1067 const char *zIntegrateUuid = db_column_text(&q, 0);
1068 int rid = db_column_int(&q, 1);
1069 if( is_a_leaf(rid) && !db_exists("SELECT 1 FROM tagxref "
1070 " WHERE tagid=%d AND rid=%d AND tagtype>0", TAG_CLOSED, rid)){
1071 blob_appendf(pOut, "T +closed %s\n", zIntegrateUuid);
1072 }
1073 }
1074 db_finalize(&q);
1075
1076 if( p->azTag ){
1077 for(i=0; p->azTag[i]; i++){
1078 /* Add a symbolic tag to this check-in. The tag names have already
1079 ** been sorted and converted using the %F format */
1080 assert( i==0 || strcmp(p->azTag[i-1], p->azTag[i])<=0 );
@@ -1059,11 +1081,10 @@
1081 blob_appendf(pOut, "T +sym-%s *\n", p->azTag[i]);
1082 }
1083 }
1084 if( p->zBranch && p->zBranch[0] ){
1085 /* For a new branch, cancel all prior propagating tags */
 
1086 db_prepare(&q,
1087 "SELECT tagname FROM tagxref, tag"
1088 " WHERE tagxref.rid=%d AND tagxref.tagid=tag.tagid"
1089 " AND tagtype==2 AND tagname GLOB 'sym-*'"
1090 " AND tagname!='sym-'||%Q"
@@ -1298,11 +1319,11 @@
1319 int vid; /* blob-id of parent version */
1320 int nrid; /* blob-id of a modified file */
1321 int nvid; /* Blob-id of the new check-in */
1322 Blob comment; /* Check-in comment */
1323 const char *zComment; /* Check-in comment */
1324 Stmt q; /* Various queries */
1325 char *zUuid; /* UUID of the new check-in */
1326 int noSign = 0; /* True to omit signing the manifest using GPG */
1327 int isAMerge = 0; /* True if checking in a merge */
1328 int noWarningFlag = 0; /* True if skipping all warnings */
1329 int forceFlag = 0; /* Undocumented: Disables all checks */
@@ -1451,25 +1472,24 @@
1472 ** will generate a manifest that has two fileA entries, which is illegal.
1473 ** When you think about it, the sequence above makes no sense. So detect
1474 ** it and disallow it. Ticket [0ff64b0a5fc8].
1475 */
1476 if( g.aCommitFile ){
1477 db_prepare(&q,
 
1478 "SELECT v1.pathname, v2.pathname"
1479 " FROM vfile AS v1, vfile AS v2"
1480 " WHERE is_selected(v1.id)"
1481 " AND v2.origname IS NOT NULL"
1482 " AND v2.origname=v1.pathname"
1483 " AND NOT is_selected(v2.id)");
1484 if( db_step(&q)==SQLITE_ROW ){
1485 const char *zFrom = db_column_text(&q, 0);
1486 const char *zTo = db_column_text(&q, 1);
1487 fossil_fatal("cannot do a partial commit of '%s' without '%s' because "
1488 "'%s' was renamed to '%s'", zFrom, zTo, zFrom, zTo);
1489 }
1490 db_finalize(&q);
1491 }
1492
1493 user_select();
1494 /*
1495 ** Check that the user exists.
@@ -1528,13 +1548,10 @@
1548 blob_zero(&comment);
1549 blob_read_from_file(&comment, zComFile);
1550 blob_to_utf8_no_bom(&comment, 1);
1551 }else if(dryRunFlag){
1552 blob_zero(&comment);
 
 
 
1553 }else{
1554 char *zInit = db_text(0, "SELECT value FROM vvar WHERE name='ci-comment'");
1555 prepare_commit_comment(&comment, zInit, &sCiInfo, vid);
1556 if( zInit && zInit[0] && fossil_strcmp(zInit, blob_str(&comment))==0 ){
1557 blob_zero(&ans);
@@ -1543,15 +1560,17 @@
1560 if( cReply!='y' && cReply!='Y' ) fossil_exit(1);;
1561 }
1562 free(zInit);
1563 }
1564 if( blob_size(&comment)==0 ){
1565 if( !dryRunFlag ){
1566 blob_zero(&ans);
1567 prompt_user("empty check-in comment. continue (y/N)? ", &ans);
1568 cReply = blob_str(&ans)[0];
1569 if( cReply!='y' && cReply!='Y' ){
1570 fossil_exit(1);
1571 }
1572 }
1573 }else{
1574 db_multi_exec("REPLACE INTO vvar VALUES('ci-comment',%B)", &comment);
1575 db_end_transaction(0);
1576 db_begin_transaction();
@@ -1620,13 +1639,10 @@
1639 fossil_fatal("one or more files were converted on your request; "
1640 "please re-test before committing");
1641 }
1642
1643 /* Create the new manifest */
 
 
 
1644 sCiInfo.pComment = &comment;
1645 sCiInfo.pCksum = useCksum ? &cksum1 : 0;
1646 sCiInfo.verifyDate = !allowOlder && !forceFlag;
1647 if( forceDelta ){
1648 blob_zero(&manifest);
@@ -1674,11 +1690,11 @@
1690 manifest = delta;
1691 }else{
1692 blob_reset(&delta);
1693 }
1694 }else if( forceDelta ){
1695 fossil_fatal("unable to find a baseline-manifest for the delta");
1696 }
1697 }
1698 if( !noSign && !g.markPrivate && clearsign(&manifest, &manifest) ){
1699 blob_zero(&ans);
1700 prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
@@ -1702,51 +1718,30 @@
1718 free(zManifestFile);
1719 }
1720
1721 nvid = content_put(&manifest);
1722 if( nvid==0 ){
1723 fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
1724 }
1725 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nvid);
1726 manifest_crosslink(nvid, &manifest);
1727 assert( blob_is_reset(&manifest) );
1728 content_deltify(vid, nvid, 0);
1729 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", nvid);
1730
1731 db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
1732 " WHERE id=-4");
1733 while( db_step(&q)==SQLITE_ROW ){
1734 const char *zIntegrateUuid = db_column_text(&q, 0);
1735 if( is_a_leaf(db_column_int(&q, 1)) ){
1736 fossil_print("Closed: %s\n", zIntegrateUuid);
 
1737 }else{
1738 fossil_print("Not_Closed: %s (not a leaf any more)\n", zIntegrateUuid);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1739 }
1740 }
1741 db_finalize(&q);
1742
 
 
 
1743 fossil_print("New_Version: %s\n", zUuid);
1744 if( outputManifest ){
1745 zManifestFile = mprintf("%smanifest.uuid", g.zLocalRoot);
1746 blob_zero(&muuid);
1747 blob_appendf(&muuid, "%s\n", zUuid);
1748
+1 -1
--- src/checkout.c
+++ src/checkout.c
@@ -61,11 +61,11 @@
6161
Blob uuid;
6262
int vid;
6363
6464
blob_init(&uuid, zName, -1);
6565
if( name_to_uuid(&uuid, 1, "ci") ){
66
- fossil_panic(g.zErrMsg);
66
+ fossil_fatal(g.zErrMsg);
6767
}
6868
vid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &uuid);
6969
if( vid==0 ){
7070
fossil_fatal("no such check-in: %s", g.argv[2]);
7171
}
7272
--- src/checkout.c
+++ src/checkout.c
@@ -61,11 +61,11 @@
61 Blob uuid;
62 int vid;
63
64 blob_init(&uuid, zName, -1);
65 if( name_to_uuid(&uuid, 1, "ci") ){
66 fossil_panic(g.zErrMsg);
67 }
68 vid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &uuid);
69 if( vid==0 ){
70 fossil_fatal("no such check-in: %s", g.argv[2]);
71 }
72
--- src/checkout.c
+++ src/checkout.c
@@ -61,11 +61,11 @@
61 Blob uuid;
62 int vid;
63
64 blob_init(&uuid, zName, -1);
65 if( name_to_uuid(&uuid, 1, "ci") ){
66 fossil_fatal(g.zErrMsg);
67 }
68 vid = db_int(0, "SELECT rid FROM blob WHERE uuid=%B", &uuid);
69 if( vid==0 ){
70 fossil_fatal("no such check-in: %s", g.argv[2]);
71 }
72
+1 -1
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128128
if( g.argc < 4 ){
129129
usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130130
}
131131
db_open_config(0);
132132
if( file_size(g.argv[3])>0 ){
133
- fossil_panic("file already exists: %s", g.argv[3]);
133
+ fossil_fatal("file already exists: %s", g.argv[3]);
134134
}
135135
136136
zDefaultUser = find_option("admin-user","A",1);
137137
138138
url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139139
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128 if( g.argc < 4 ){
129 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130 }
131 db_open_config(0);
132 if( file_size(g.argv[3])>0 ){
133 fossil_panic("file already exists: %s", g.argv[3]);
134 }
135
136 zDefaultUser = find_option("admin-user","A",1);
137
138 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128 if( g.argc < 4 ){
129 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130 }
131 db_open_config(0);
132 if( file_size(g.argv[3])>0 ){
133 fossil_fatal("file already exists: %s", g.argv[3]);
134 }
135
136 zDefaultUser = find_option("admin-user","A",1);
137
138 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139
+1 -1
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128128
if( g.argc < 4 ){
129129
usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130130
}
131131
db_open_config(0);
132132
if( file_size(g.argv[3])>0 ){
133
- fossil_panic("file already exists: %s", g.argv[3]);
133
+ fossil_fatal("file already exists: %s", g.argv[3]);
134134
}
135135
136136
zDefaultUser = find_option("admin-user","A",1);
137137
138138
url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139139
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128 if( g.argc < 4 ){
129 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130 }
131 db_open_config(0);
132 if( file_size(g.argv[3])>0 ){
133 fossil_panic("file already exists: %s", g.argv[3]);
134 }
135
136 zDefaultUser = find_option("admin-user","A",1);
137
138 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139
--- src/clone.c
+++ src/clone.c
@@ -128,11 +128,11 @@
128 if( g.argc < 4 ){
129 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
130 }
131 db_open_config(0);
132 if( file_size(g.argv[3])>0 ){
133 fossil_fatal("file already exists: %s", g.argv[3]);
134 }
135
136 zDefaultUser = find_option("admin-user","A",1);
137
138 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
139
+61 -2
--- src/content.c
+++ src/content.c
@@ -825,25 +825,47 @@
825825
db_must_be_within_tree();
826826
content_deltify(atoi(g.argv[2]), atoi(g.argv[3]), atoi(g.argv[4]));
827827
}
828828
829829
/*
830
-** COMMAND: test-integrity
830
+** Return true if Blob p looks like it might be a parsable control artifact.
831
+*/
832
+static int looks_like_control_artifact(Blob *p){
833
+ const char *z = blob_buffer(p);
834
+ int n = blob_size(p);
835
+ if( n<10 ) return 0;
836
+ if( strncmp(z, "-----BEGIN PGP SIGNED MESSAGE-----", 34)==0 ) return 1;
837
+ if( z[0]<'A' || z[0]>'Z' || z[1]!=' ' || z[0]=='I' ) return 0;
838
+ if( z[n-1]!='\n' ) return 0;
839
+ return 1;
840
+}
841
+
842
+/*
843
+** COMMAND: test-integrity ?OPTIONS?
831844
**
832845
** Verify that all content can be extracted from the BLOB table correctly.
833846
** If the BLOB table is correct, then the repository can always be
834847
** successfully reconstructed using "fossil rebuild".
848
+**
849
+** Options:
850
+**
851
+** --parse Parse all manifests, wikis, tickets, events, and
852
+** so forth, reporting any errors found.
835853
*/
836854
void test_integrity(void){
837855
Stmt q;
838856
Blob content;
839857
Blob cksum;
840858
int n1 = 0;
841859
int n2 = 0;
842860
int nErr = 0;
843861
int total;
862
+ int nCA = 0;
863
+ int anCA[10];
864
+ int bParse = find_option("parse",0,0)!=0;
844865
db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
866
+ memset(anCA, 0, sizeof(anCA));
845867
846868
/* Make sure no public artifact is a delta from a private artifact */
847869
db_prepare(&q,
848870
"SELECT "
849871
" rid, (SELECT uuid FROM blob WHERE rid=delta.rid),"
@@ -887,17 +909,54 @@
887909
if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
888910
fossil_print("checksum mismatch on artifact %d: wanted %s but got %s\n",
889911
rid, zUuid, blob_str(&cksum));
890912
nErr++;
891913
}
914
+ if( bParse && looks_like_control_artifact(&content) ){
915
+ Blob err;
916
+ int i, n;
917
+ char *z;
918
+ Manifest *p;
919
+ char zFirstLine[400];
920
+ blob_zero(&err);
921
+
922
+ z = blob_buffer(&content);
923
+ n = blob_size(&content);
924
+ for(i=0; i<n && z[i] && z[i]!='\n' && i<sizeof(zFirstLine)-1; i++){}
925
+ memcpy(zFirstLine, z, i);
926
+ zFirstLine[i] = 0;
927
+ p = manifest_parse(&content, 0, &err);
928
+ if( p==0 ){
929
+ fossil_print("manifest_parse failed for %s:\n%s\n",
930
+ blob_str(&cksum), blob_str(&err));
931
+ if( strncmp(blob_str(&err), "line 1:", 7)==0 ){
932
+ fossil_print("\"%s\"\n", zFirstLine);
933
+ }
934
+ }else{
935
+ anCA[p->type]++;
936
+ manifest_destroy(p);
937
+ nCA++;
938
+ }
939
+ blob_reset(&err);
940
+ }else{
941
+ blob_reset(&content);
942
+ }
892943
blob_reset(&cksum);
893
- blob_reset(&content);
894944
n2++;
895945
}
896946
db_finalize(&q);
897947
fossil_print("%d non-phantom blobs (out of %d total) checked: %d errors\n",
898948
n2, n1, nErr);
949
+ if( bParse ){
950
+ const char *azType[] = { 0, "manifest", "cluster", "control", "wiki",
951
+ "ticket", "attachment", "event" };
952
+ int i;
953
+ fossil_print("%d total control artifacts\n", nCA);
954
+ for(i=1; i<count(azType); i++){
955
+ if( anCA[i] ) fossil_print(" %d %ss\n", anCA[i], azType[i]);
956
+ }
957
+ }
899958
}
900959
901960
/*
902961
** COMMAND: test-orphans
903962
**
904963
--- src/content.c
+++ src/content.c
@@ -825,25 +825,47 @@
825 db_must_be_within_tree();
826 content_deltify(atoi(g.argv[2]), atoi(g.argv[3]), atoi(g.argv[4]));
827 }
828
829 /*
830 ** COMMAND: test-integrity
 
 
 
 
 
 
 
 
 
 
 
 
 
831 **
832 ** Verify that all content can be extracted from the BLOB table correctly.
833 ** If the BLOB table is correct, then the repository can always be
834 ** successfully reconstructed using "fossil rebuild".
 
 
 
 
 
835 */
836 void test_integrity(void){
837 Stmt q;
838 Blob content;
839 Blob cksum;
840 int n1 = 0;
841 int n2 = 0;
842 int nErr = 0;
843 int total;
 
 
 
844 db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
 
845
846 /* Make sure no public artifact is a delta from a private artifact */
847 db_prepare(&q,
848 "SELECT "
849 " rid, (SELECT uuid FROM blob WHERE rid=delta.rid),"
@@ -887,17 +909,54 @@
887 if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
888 fossil_print("checksum mismatch on artifact %d: wanted %s but got %s\n",
889 rid, zUuid, blob_str(&cksum));
890 nErr++;
891 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892 blob_reset(&cksum);
893 blob_reset(&content);
894 n2++;
895 }
896 db_finalize(&q);
897 fossil_print("%d non-phantom blobs (out of %d total) checked: %d errors\n",
898 n2, n1, nErr);
 
 
 
 
 
 
 
 
 
899 }
900
901 /*
902 ** COMMAND: test-orphans
903 **
904
--- src/content.c
+++ src/content.c
@@ -825,25 +825,47 @@
825 db_must_be_within_tree();
826 content_deltify(atoi(g.argv[2]), atoi(g.argv[3]), atoi(g.argv[4]));
827 }
828
829 /*
830 ** Return true if Blob p looks like it might be a parsable control artifact.
831 */
832 static int looks_like_control_artifact(Blob *p){
833 const char *z = blob_buffer(p);
834 int n = blob_size(p);
835 if( n<10 ) return 0;
836 if( strncmp(z, "-----BEGIN PGP SIGNED MESSAGE-----", 34)==0 ) return 1;
837 if( z[0]<'A' || z[0]>'Z' || z[1]!=' ' || z[0]=='I' ) return 0;
838 if( z[n-1]!='\n' ) return 0;
839 return 1;
840 }
841
842 /*
843 ** COMMAND: test-integrity ?OPTIONS?
844 **
845 ** Verify that all content can be extracted from the BLOB table correctly.
846 ** If the BLOB table is correct, then the repository can always be
847 ** successfully reconstructed using "fossil rebuild".
848 **
849 ** Options:
850 **
851 ** --parse Parse all manifests, wikis, tickets, events, and
852 ** so forth, reporting any errors found.
853 */
854 void test_integrity(void){
855 Stmt q;
856 Blob content;
857 Blob cksum;
858 int n1 = 0;
859 int n2 = 0;
860 int nErr = 0;
861 int total;
862 int nCA = 0;
863 int anCA[10];
864 int bParse = find_option("parse",0,0)!=0;
865 db_find_and_open_repository(OPEN_ANY_SCHEMA, 2);
866 memset(anCA, 0, sizeof(anCA));
867
868 /* Make sure no public artifact is a delta from a private artifact */
869 db_prepare(&q,
870 "SELECT "
871 " rid, (SELECT uuid FROM blob WHERE rid=delta.rid),"
@@ -887,17 +909,54 @@
909 if( fossil_strcmp(blob_str(&cksum), zUuid)!=0 ){
910 fossil_print("checksum mismatch on artifact %d: wanted %s but got %s\n",
911 rid, zUuid, blob_str(&cksum));
912 nErr++;
913 }
914 if( bParse && looks_like_control_artifact(&content) ){
915 Blob err;
916 int i, n;
917 char *z;
918 Manifest *p;
919 char zFirstLine[400];
920 blob_zero(&err);
921
922 z = blob_buffer(&content);
923 n = blob_size(&content);
924 for(i=0; i<n && z[i] && z[i]!='\n' && i<sizeof(zFirstLine)-1; i++){}
925 memcpy(zFirstLine, z, i);
926 zFirstLine[i] = 0;
927 p = manifest_parse(&content, 0, &err);
928 if( p==0 ){
929 fossil_print("manifest_parse failed for %s:\n%s\n",
930 blob_str(&cksum), blob_str(&err));
931 if( strncmp(blob_str(&err), "line 1:", 7)==0 ){
932 fossil_print("\"%s\"\n", zFirstLine);
933 }
934 }else{
935 anCA[p->type]++;
936 manifest_destroy(p);
937 nCA++;
938 }
939 blob_reset(&err);
940 }else{
941 blob_reset(&content);
942 }
943 blob_reset(&cksum);
 
944 n2++;
945 }
946 db_finalize(&q);
947 fossil_print("%d non-phantom blobs (out of %d total) checked: %d errors\n",
948 n2, n1, nErr);
949 if( bParse ){
950 const char *azType[] = { 0, "manifest", "cluster", "control", "wiki",
951 "ticket", "attachment", "event" };
952 int i;
953 fossil_print("%d total control artifacts\n", nCA);
954 for(i=1; i<count(azType); i++){
955 if( anCA[i] ) fossil_print(" %d %ss\n", anCA[i], azType[i]);
956 }
957 }
958 }
959
960 /*
961 ** COMMAND: test-orphans
962 **
963
+4 -2
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
14001400
int rid;
14011401
blob_zero(&manifest);
14021402
blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
14031403
zDate = date_in_standard_format(zInitialDate);
14041404
blob_appendf(&manifest, "D %s\n", zDate);
1405
- blob_appendf(&manifest, "P\n");
14061405
md5sum_init();
1406
+ /* The R-card is necessary here because without it
1407
+ * fossil versions earlier than versions 1.27 would
1408
+ * interpret this artifact as a "control". */
14071409
blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
14081410
blob_appendf(&manifest, "T *branch * trunk\n");
14091411
blob_appendf(&manifest, "T *sym-trunk *\n");
14101412
blob_appendf(&manifest, "U %F\n", g.zLogin);
14111413
md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
19921994
allowNested = find_option("nested",0,0)!=0;
19931995
if( g.argc!=3 && g.argc!=4 ){
19941996
usage("REPOSITORY-FILENAME ?VERSION?");
19951997
}
19961998
if( !allowNested && db_open_local(0) ){
1997
- fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1999
+ fossil_fatal("already within an open tree rooted at %s", g.zLocalRoot);
19982000
}
19992001
db_open_repository(g.argv[2]);
20002002
#if defined(_WIN32) || defined(__CYGWIN__)
20012003
# define LOCALDB_NAME "./_FOSSIL_"
20022004
#else
20032005
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
1400 int rid;
1401 blob_zero(&manifest);
1402 blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
1403 zDate = date_in_standard_format(zInitialDate);
1404 blob_appendf(&manifest, "D %s\n", zDate);
1405 blob_appendf(&manifest, "P\n");
1406 md5sum_init();
 
 
 
1407 blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
1408 blob_appendf(&manifest, "T *branch * trunk\n");
1409 blob_appendf(&manifest, "T *sym-trunk *\n");
1410 blob_appendf(&manifest, "U %F\n", g.zLogin);
1411 md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
1992 allowNested = find_option("nested",0,0)!=0;
1993 if( g.argc!=3 && g.argc!=4 ){
1994 usage("REPOSITORY-FILENAME ?VERSION?");
1995 }
1996 if( !allowNested && db_open_local(0) ){
1997 fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1998 }
1999 db_open_repository(g.argv[2]);
2000 #if defined(_WIN32) || defined(__CYGWIN__)
2001 # define LOCALDB_NAME "./_FOSSIL_"
2002 #else
2003
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
1400 int rid;
1401 blob_zero(&manifest);
1402 blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
1403 zDate = date_in_standard_format(zInitialDate);
1404 blob_appendf(&manifest, "D %s\n", zDate);
 
1405 md5sum_init();
1406 /* The R-card is necessary here because without it
1407 * fossil versions earlier than versions 1.27 would
1408 * interpret this artifact as a "control". */
1409 blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
1410 blob_appendf(&manifest, "T *branch * trunk\n");
1411 blob_appendf(&manifest, "T *sym-trunk *\n");
1412 blob_appendf(&manifest, "U %F\n", g.zLogin);
1413 md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
1994 allowNested = find_option("nested",0,0)!=0;
1995 if( g.argc!=3 && g.argc!=4 ){
1996 usage("REPOSITORY-FILENAME ?VERSION?");
1997 }
1998 if( !allowNested && db_open_local(0) ){
1999 fossil_fatal("already within an open tree rooted at %s", g.zLocalRoot);
2000 }
2001 db_open_repository(g.argv[2]);
2002 #if defined(_WIN32) || defined(__CYGWIN__)
2003 # define LOCALDB_NAME "./_FOSSIL_"
2004 #else
2005
+4 -2
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
14001400
int rid;
14011401
blob_zero(&manifest);
14021402
blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
14031403
zDate = date_in_standard_format(zInitialDate);
14041404
blob_appendf(&manifest, "D %s\n", zDate);
1405
- blob_appendf(&manifest, "P\n");
14061405
md5sum_init();
1406
+ /* The R-card is necessary here because without it
1407
+ * fossil versions earlier than versions 1.27 would
1408
+ * interpret this artifact as a "control". */
14071409
blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
14081410
blob_appendf(&manifest, "T *branch * trunk\n");
14091411
blob_appendf(&manifest, "T *sym-trunk *\n");
14101412
blob_appendf(&manifest, "U %F\n", g.zLogin);
14111413
md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
19921994
allowNested = find_option("nested",0,0)!=0;
19931995
if( g.argc!=3 && g.argc!=4 ){
19941996
usage("REPOSITORY-FILENAME ?VERSION?");
19951997
}
19961998
if( !allowNested && db_open_local(0) ){
1997
- fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1999
+ fossil_fatal("already within an open tree rooted at %s", g.zLocalRoot);
19982000
}
19992001
db_open_repository(g.argv[2]);
20002002
#if defined(_WIN32) || defined(__CYGWIN__)
20012003
# define LOCALDB_NAME "./_FOSSIL_"
20022004
#else
20032005
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
1400 int rid;
1401 blob_zero(&manifest);
1402 blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
1403 zDate = date_in_standard_format(zInitialDate);
1404 blob_appendf(&manifest, "D %s\n", zDate);
1405 blob_appendf(&manifest, "P\n");
1406 md5sum_init();
 
 
 
1407 blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
1408 blob_appendf(&manifest, "T *branch * trunk\n");
1409 blob_appendf(&manifest, "T *sym-trunk *\n");
1410 blob_appendf(&manifest, "U %F\n", g.zLogin);
1411 md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
1992 allowNested = find_option("nested",0,0)!=0;
1993 if( g.argc!=3 && g.argc!=4 ){
1994 usage("REPOSITORY-FILENAME ?VERSION?");
1995 }
1996 if( !allowNested && db_open_local(0) ){
1997 fossil_panic("already within an open tree rooted at %s", g.zLocalRoot);
1998 }
1999 db_open_repository(g.argv[2]);
2000 #if defined(_WIN32) || defined(__CYGWIN__)
2001 # define LOCALDB_NAME "./_FOSSIL_"
2002 #else
2003
--- src/db.c
+++ src/db.c
@@ -1400,12 +1400,14 @@
1400 int rid;
1401 blob_zero(&manifest);
1402 blob_appendf(&manifest, "C initial\\sempty\\scheck-in\n");
1403 zDate = date_in_standard_format(zInitialDate);
1404 blob_appendf(&manifest, "D %s\n", zDate);
 
1405 md5sum_init();
1406 /* The R-card is necessary here because without it
1407 * fossil versions earlier than versions 1.27 would
1408 * interpret this artifact as a "control". */
1409 blob_appendf(&manifest, "R %s\n", md5sum_finish(0));
1410 blob_appendf(&manifest, "T *branch * trunk\n");
1411 blob_appendf(&manifest, "T *sym-trunk *\n");
1412 blob_appendf(&manifest, "U %F\n", g.zLogin);
1413 md5sum_blob(&manifest, &hash);
@@ -1992,11 +1994,11 @@
1994 allowNested = find_option("nested",0,0)!=0;
1995 if( g.argc!=3 && g.argc!=4 ){
1996 usage("REPOSITORY-FILENAME ?VERSION?");
1997 }
1998 if( !allowNested && db_open_local(0) ){
1999 fossil_fatal("already within an open tree rooted at %s", g.zLocalRoot);
2000 }
2001 db_open_repository(g.argv[2]);
2002 #if defined(_WIN32) || defined(__CYGWIN__)
2003 # define LOCALDB_NAME "./_FOSSIL_"
2004 #else
2005
+1 -1
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -144,9 +144,9 @@
144144
blob_delta_create(&f1, &f2, &d12);
145145
blob_delta_create(&f2, &f1, &d21);
146146
blob_delta_apply(&f1, &d12, &a2);
147147
blob_delta_apply(&f2, &d21, &a1);
148148
if( blob_compare(&f1,&a1) || blob_compare(&f2, &a2) ){
149
- fossil_panic("delta test failed");
149
+ fossil_fatal("delta test failed");
150150
}
151151
fossil_print("ok\n");
152152
}
153153
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -144,9 +144,9 @@
144 blob_delta_create(&f1, &f2, &d12);
145 blob_delta_create(&f2, &f1, &d21);
146 blob_delta_apply(&f1, &d12, &a2);
147 blob_delta_apply(&f2, &d21, &a1);
148 if( blob_compare(&f1,&a1) || blob_compare(&f2, &a2) ){
149 fossil_panic("delta test failed");
150 }
151 fossil_print("ok\n");
152 }
153
--- src/deltacmd.c
+++ src/deltacmd.c
@@ -144,9 +144,9 @@
144 blob_delta_create(&f1, &f2, &d12);
145 blob_delta_create(&f2, &f1, &d21);
146 blob_delta_apply(&f1, &d12, &a2);
147 blob_delta_apply(&f2, &d21, &a1);
148 if( blob_compare(&f1,&a1) || blob_compare(&f2, &a2) ){
149 fossil_fatal("delta test failed");
150 }
151 fossil_print("ok\n");
152 }
153
+4 -3
--- src/diff.c
+++ src/diff.c
@@ -2046,14 +2046,14 @@
20462046
int cnt = 0; /* Number of versions examined */
20472047
20482048
/* Initialize the annotation */
20492049
rid = db_int(0, "SELECT fid FROM mlink WHERE mid=%d AND fnid=%d",mid,fnid);
20502050
if( rid==0 ){
2051
- fossil_panic("file #%d is unchanged in manifest #%d", fnid, mid);
2051
+ fossil_fatal("file #%d is unchanged in manifest #%d", fnid, mid);
20522052
}
20532053
if( !content_get(rid, &toAnnotate) ){
2054
- fossil_panic("unable to retrieve content of artifact #%d", rid);
2054
+ fossil_fatal("unable to retrieve content of artifact #%d", rid);
20552055
}
20562056
if( iLimit<=0 ) iLimit = 1000000000;
20572057
annotation_start(p, &toAnnotate);
20582058
db_begin_transaction();
20592059
db_multi_exec(
@@ -2150,10 +2150,11 @@
21502150
21512151
/* Gather query parameters */
21522152
showLog = atoi(PD("log","1"));
21532153
login_check_credentials();
21542154
if( !g.perm.Read ){ login_needed(); return; }
2155
+ if( exclude_spiders("annotate") ) return;
21552156
mid = name_to_typed_rid(PD("checkin","0"),"ci");
21562157
zFilename = P("filename");
21572158
fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q", zFilename);
21582159
if( mid==0 || fnid==0 ){ fossil_redirect_home(); }
21592160
iLimit = atoi(PD("limit","20"));
@@ -2328,11 +2329,11 @@
23282329
mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
23292330
" WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
23302331
" ORDER BY ancestor.generation ASC LIMIT 1",
23312332
fid, fnid);
23322333
if( mid==0 ){
2333
- fossil_panic("unable to find manifest");
2334
+ fossil_fatal("unable to find manifest");
23342335
}
23352336
annFlags |= ANN_FILE_ANCEST;
23362337
annotate_file(&ann, fnid, mid, iLimit, annFlags);
23372338
if( showLog ){
23382339
struct AnnVers *p;
23392340
--- src/diff.c
+++ src/diff.c
@@ -2046,14 +2046,14 @@
2046 int cnt = 0; /* Number of versions examined */
2047
2048 /* Initialize the annotation */
2049 rid = db_int(0, "SELECT fid FROM mlink WHERE mid=%d AND fnid=%d",mid,fnid);
2050 if( rid==0 ){
2051 fossil_panic("file #%d is unchanged in manifest #%d", fnid, mid);
2052 }
2053 if( !content_get(rid, &toAnnotate) ){
2054 fossil_panic("unable to retrieve content of artifact #%d", rid);
2055 }
2056 if( iLimit<=0 ) iLimit = 1000000000;
2057 annotation_start(p, &toAnnotate);
2058 db_begin_transaction();
2059 db_multi_exec(
@@ -2150,10 +2150,11 @@
2150
2151 /* Gather query parameters */
2152 showLog = atoi(PD("log","1"));
2153 login_check_credentials();
2154 if( !g.perm.Read ){ login_needed(); return; }
 
2155 mid = name_to_typed_rid(PD("checkin","0"),"ci");
2156 zFilename = P("filename");
2157 fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q", zFilename);
2158 if( mid==0 || fnid==0 ){ fossil_redirect_home(); }
2159 iLimit = atoi(PD("limit","20"));
@@ -2328,11 +2329,11 @@
2328 mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
2329 " WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
2330 " ORDER BY ancestor.generation ASC LIMIT 1",
2331 fid, fnid);
2332 if( mid==0 ){
2333 fossil_panic("unable to find manifest");
2334 }
2335 annFlags |= ANN_FILE_ANCEST;
2336 annotate_file(&ann, fnid, mid, iLimit, annFlags);
2337 if( showLog ){
2338 struct AnnVers *p;
2339
--- src/diff.c
+++ src/diff.c
@@ -2046,14 +2046,14 @@
2046 int cnt = 0; /* Number of versions examined */
2047
2048 /* Initialize the annotation */
2049 rid = db_int(0, "SELECT fid FROM mlink WHERE mid=%d AND fnid=%d",mid,fnid);
2050 if( rid==0 ){
2051 fossil_fatal("file #%d is unchanged in manifest #%d", fnid, mid);
2052 }
2053 if( !content_get(rid, &toAnnotate) ){
2054 fossil_fatal("unable to retrieve content of artifact #%d", rid);
2055 }
2056 if( iLimit<=0 ) iLimit = 1000000000;
2057 annotation_start(p, &toAnnotate);
2058 db_begin_transaction();
2059 db_multi_exec(
@@ -2150,10 +2150,11 @@
2150
2151 /* Gather query parameters */
2152 showLog = atoi(PD("log","1"));
2153 login_check_credentials();
2154 if( !g.perm.Read ){ login_needed(); return; }
2155 if( exclude_spiders("annotate") ) return;
2156 mid = name_to_typed_rid(PD("checkin","0"),"ci");
2157 zFilename = P("filename");
2158 fnid = db_int(0, "SELECT fnid FROM filename WHERE name=%Q", zFilename);
2159 if( mid==0 || fnid==0 ){ fossil_redirect_home(); }
2160 iLimit = atoi(PD("limit","20"));
@@ -2328,11 +2329,11 @@
2329 mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
2330 " WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
2331 " ORDER BY ancestor.generation ASC LIMIT 1",
2332 fid, fnid);
2333 if( mid==0 ){
2334 fossil_fatal("unable to find manifest");
2335 }
2336 annFlags |= ANN_FILE_ANCEST;
2337 annotate_file(&ann, fnid, mid, iLimit, annFlags);
2338 if( showLog ){
2339 struct AnnVers *p;
2340
+2 -10
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -617,12 +617,10 @@
617617
@ HR_PAD_TOP 4
618618
@ HR_PAD_BTM 8
619619
@ FN_BG #444444
620620
@ FN_FG #ffffff
621621
@ FN_PAD 5
622
-@ FONTS {{DejaVu Sans Mono} Consolas Monaco}
623
-@ FONT_SIZE 9
624622
@ PADX 5
625623
@ WIDTH 80
626624
@ HEIGHT 45
627625
@ LB_HEIGHT 25
628626
@ }
@@ -868,21 +866,15 @@
868866
@ $txt tag config fn -background $CFG(FN_BG) -foreground $CFG(FN_FG) \
869867
@ -justify center
870868
@ }
871869
@ text .mkr
872870
@
873
-@ font create mono -family courier -size $CFG(FONT_SIZE)
874
-@ foreach font $CFG(FONTS) {
875
-@ if {[lsearch -exact [font families] $font] != -1} {
876
-@ font config mono -family $font
877
-@ break
878
-@ }
879
-@ }
880871
@ foreach c [cols] {
881872
@ set keyPrefix [string toupper [colType $c]]_COL_
873
+@ if {[tk windowingsystem] eq "win32"} {$c config -font {courier 9}}
882874
@ $c config -bg $CFG(${keyPrefix}BG) -fg $CFG(${keyPrefix}FG) -borderwidth 0 \
883
-@ -font mono -padx $CFG(PADX) -yscroll sync-y
875
+@ -padx $CFG(PADX) -yscroll sync-y
884876
@ $c tag config hr -spacing1 $CFG(HR_PAD_TOP) -spacing3 $CFG(HR_PAD_BTM) \
885877
@ -foreground $CFG(HR_FG)
886878
@ $c tag config fn -spacing1 $CFG(FN_PAD) -spacing3 $CFG(FN_PAD)
887879
@ bindtags $c ". $c Text all"
888880
@ bind $c <1> {focus %W}
889881
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -617,12 +617,10 @@
617 @ HR_PAD_TOP 4
618 @ HR_PAD_BTM 8
619 @ FN_BG #444444
620 @ FN_FG #ffffff
621 @ FN_PAD 5
622 @ FONTS {{DejaVu Sans Mono} Consolas Monaco}
623 @ FONT_SIZE 9
624 @ PADX 5
625 @ WIDTH 80
626 @ HEIGHT 45
627 @ LB_HEIGHT 25
628 @ }
@@ -868,21 +866,15 @@
868 @ $txt tag config fn -background $CFG(FN_BG) -foreground $CFG(FN_FG) \
869 @ -justify center
870 @ }
871 @ text .mkr
872 @
873 @ font create mono -family courier -size $CFG(FONT_SIZE)
874 @ foreach font $CFG(FONTS) {
875 @ if {[lsearch -exact [font families] $font] != -1} {
876 @ font config mono -family $font
877 @ break
878 @ }
879 @ }
880 @ foreach c [cols] {
881 @ set keyPrefix [string toupper [colType $c]]_COL_
 
882 @ $c config -bg $CFG(${keyPrefix}BG) -fg $CFG(${keyPrefix}FG) -borderwidth 0 \
883 @ -font mono -padx $CFG(PADX) -yscroll sync-y
884 @ $c tag config hr -spacing1 $CFG(HR_PAD_TOP) -spacing3 $CFG(HR_PAD_BTM) \
885 @ -foreground $CFG(HR_FG)
886 @ $c tag config fn -spacing1 $CFG(FN_PAD) -spacing3 $CFG(FN_PAD)
887 @ bindtags $c ". $c Text all"
888 @ bind $c <1> {focus %W}
889
--- src/diffcmd.c
+++ src/diffcmd.c
@@ -617,12 +617,10 @@
617 @ HR_PAD_TOP 4
618 @ HR_PAD_BTM 8
619 @ FN_BG #444444
620 @ FN_FG #ffffff
621 @ FN_PAD 5
 
 
622 @ PADX 5
623 @ WIDTH 80
624 @ HEIGHT 45
625 @ LB_HEIGHT 25
626 @ }
@@ -868,21 +866,15 @@
866 @ $txt tag config fn -background $CFG(FN_BG) -foreground $CFG(FN_FG) \
867 @ -justify center
868 @ }
869 @ text .mkr
870 @
 
 
 
 
 
 
 
871 @ foreach c [cols] {
872 @ set keyPrefix [string toupper [colType $c]]_COL_
873 @ if {[tk windowingsystem] eq "win32"} {$c config -font {courier 9}}
874 @ $c config -bg $CFG(${keyPrefix}BG) -fg $CFG(${keyPrefix}FG) -borderwidth 0 \
875 @ -padx $CFG(PADX) -yscroll sync-y
876 @ $c tag config hr -spacing1 $CFG(HR_PAD_TOP) -spacing3 $CFG(HR_PAD_BTM) \
877 @ -foreground $CFG(HR_FG)
878 @ $c tag config fn -spacing1 $CFG(FN_PAD) -spacing3 $CFG(FN_PAD)
879 @ bindtags $c ". $c Text all"
880 @ bind $c <1> {focus %W}
881
+9 -4
--- src/event.c
+++ src/event.c
@@ -115,11 +115,11 @@
115115
116116
/* Extract the event content.
117117
*/
118118
pEvent = manifest_get(rid, CFTYPE_EVENT);
119119
if( pEvent==0 ){
120
- fossil_panic("Object #%d is not an event", rid);
120
+ fossil_fatal("Object #%d is not an event", rid);
121121
}
122122
blob_init(&fullbody, pEvent->zWiki, -1);
123123
if( wiki_find_title(&fullbody, &title, &tail) ){
124124
style_header(blob_str(&title));
125125
}else{
@@ -280,15 +280,20 @@
280280
}
281281
zETime = db_text(0, "SELECT coalesce(datetime(%Q),datetime('now'))", zETime);
282282
if( P("submit")!=0 && (zBody!=0 && zComment!=0) ){
283283
char *zDate;
284284
Blob cksum;
285
- int nrid;
285
+ int nrid, n;
286286
blob_zero(&event);
287287
db_begin_transaction();
288288
login_verify_csrf_secret();
289
- blob_appendf(&event, "C %F\n", zComment);
289
+ while( fossil_isspace(zComment[0]) ) zComment++;
290
+ n = strlen(zComment);
291
+ while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
292
+ if( n>0 ){
293
+ blob_appendf(&event, "C %#F\n", n, zComment);
294
+ }
290295
zDate = date_in_standard_format("now");
291296
blob_appendf(&event, "D %s\n", zDate);
292297
free(zDate);
293298
zETime[10] = 'T';
294299
blob_appendf(&event, "E %s %s\n", zETime, zEventId);
@@ -399,11 +404,11 @@
399404
@ <form method="post" action="%s(g.zTop)/eventedit"><div>
400405
login_insert_csrf_secret();
401406
@ <input type="hidden" name="name" value="%h(zEventId)" />
402407
@ <table border="0" cellspacing="10">
403408
404
- @ <tr><th align="right" valign="top">Event&nbsp;Time:</th>
409
+ @ <tr><th align="right" valign="top">Event&nbsp;Time (UTC):</th>
405410
@ <td valign="top">
406411
@ <input type="text" name="t" size="25" value="%h(zETime)" />
407412
@ </td></tr>
408413
409414
@ <tr><th align="right" valign="top">Timeline&nbsp;Comment:</th>
410415
--- src/event.c
+++ src/event.c
@@ -115,11 +115,11 @@
115
116 /* Extract the event content.
117 */
118 pEvent = manifest_get(rid, CFTYPE_EVENT);
119 if( pEvent==0 ){
120 fossil_panic("Object #%d is not an event", rid);
121 }
122 blob_init(&fullbody, pEvent->zWiki, -1);
123 if( wiki_find_title(&fullbody, &title, &tail) ){
124 style_header(blob_str(&title));
125 }else{
@@ -280,15 +280,20 @@
280 }
281 zETime = db_text(0, "SELECT coalesce(datetime(%Q),datetime('now'))", zETime);
282 if( P("submit")!=0 && (zBody!=0 && zComment!=0) ){
283 char *zDate;
284 Blob cksum;
285 int nrid;
286 blob_zero(&event);
287 db_begin_transaction();
288 login_verify_csrf_secret();
289 blob_appendf(&event, "C %F\n", zComment);
 
 
 
 
 
290 zDate = date_in_standard_format("now");
291 blob_appendf(&event, "D %s\n", zDate);
292 free(zDate);
293 zETime[10] = 'T';
294 blob_appendf(&event, "E %s %s\n", zETime, zEventId);
@@ -399,11 +404,11 @@
399 @ <form method="post" action="%s(g.zTop)/eventedit"><div>
400 login_insert_csrf_secret();
401 @ <input type="hidden" name="name" value="%h(zEventId)" />
402 @ <table border="0" cellspacing="10">
403
404 @ <tr><th align="right" valign="top">Event&nbsp;Time:</th>
405 @ <td valign="top">
406 @ <input type="text" name="t" size="25" value="%h(zETime)" />
407 @ </td></tr>
408
409 @ <tr><th align="right" valign="top">Timeline&nbsp;Comment:</th>
410
--- src/event.c
+++ src/event.c
@@ -115,11 +115,11 @@
115
116 /* Extract the event content.
117 */
118 pEvent = manifest_get(rid, CFTYPE_EVENT);
119 if( pEvent==0 ){
120 fossil_fatal("Object #%d is not an event", rid);
121 }
122 blob_init(&fullbody, pEvent->zWiki, -1);
123 if( wiki_find_title(&fullbody, &title, &tail) ){
124 style_header(blob_str(&title));
125 }else{
@@ -280,15 +280,20 @@
280 }
281 zETime = db_text(0, "SELECT coalesce(datetime(%Q),datetime('now'))", zETime);
282 if( P("submit")!=0 && (zBody!=0 && zComment!=0) ){
283 char *zDate;
284 Blob cksum;
285 int nrid, n;
286 blob_zero(&event);
287 db_begin_transaction();
288 login_verify_csrf_secret();
289 while( fossil_isspace(zComment[0]) ) zComment++;
290 n = strlen(zComment);
291 while( n>0 && fossil_isspace(zComment[n-1]) ){ n--; }
292 if( n>0 ){
293 blob_appendf(&event, "C %#F\n", n, zComment);
294 }
295 zDate = date_in_standard_format("now");
296 blob_appendf(&event, "D %s\n", zDate);
297 free(zDate);
298 zETime[10] = 'T';
299 blob_appendf(&event, "E %s %s\n", zETime, zEventId);
@@ -399,11 +404,11 @@
404 @ <form method="post" action="%s(g.zTop)/eventedit"><div>
405 login_insert_csrf_secret();
406 @ <input type="hidden" name="name" value="%h(zEventId)" />
407 @ <table border="0" cellspacing="10">
408
409 @ <tr><th align="right" valign="top">Event&nbsp;Time (UTC):</th>
410 @ <td valign="top">
411 @ <input type="text" name="t" size="25" value="%h(zETime)" />
412 @ </td></tr>
413
414 @ <tr><th align="right" valign="top">Timeline&nbsp;Comment:</th>
415
+4 -4
--- src/export.c
+++ src/export.c
@@ -138,11 +138,11 @@
138138
char line[100];
139139
FILE *f;
140140
141141
f = fossil_fopen(markfile_in, "r");
142142
if( f==0 ){
143
- fossil_panic("cannot open %s for reading", markfile_in);
143
+ fossil_fatal("cannot open %s for reading", markfile_in);
144144
}
145145
db_prepare(&qb, "INSERT OR IGNORE INTO oldblob VALUES (:rid)");
146146
db_prepare(&qc, "INSERT OR IGNORE INTO oldcommit VALUES (:rid)");
147147
while( fgets(line, sizeof(line), f)!=0 ){
148148
if( *line == 'b' ){
@@ -154,11 +154,11 @@
154154
db_bind_text(&qc, ":rid", line + 1);
155155
db_step(&qc);
156156
db_reset(&qc);
157157
bag_insert(&vers, atoi(line + 1));
158158
}else{
159
- fossil_panic("bad input from %s: %s", markfile_in, line);
159
+ fossil_fatal("bad input from %s: %s", markfile_in, line);
160160
}
161161
}
162162
db_finalize(&qb);
163163
db_finalize(&qc);
164164
fclose(f);
@@ -335,11 +335,11 @@
335335
336336
if( markfile_out!=0 ){
337337
FILE *f;
338338
f = fossil_fopen(markfile_out, "w");
339339
if( f == 0 ){
340
- fossil_panic("cannot open %s for writing", markfile_out);
340
+ fossil_fatal("cannot open %s for writing", markfile_out);
341341
}
342342
db_prepare(&q, "SELECT rid FROM oldblob");
343343
while( db_step(&q)==SQLITE_ROW ){
344344
fprintf(f, "b%d\n", db_column_int(&q, 0));
345345
}
@@ -348,9 +348,9 @@
348348
while( db_step(&q)==SQLITE_ROW ){
349349
fprintf(f, "c%d\n", db_column_int(&q, 0));
350350
}
351351
db_finalize(&q);
352352
if( ferror(f)!=0 || fclose(f)!=0 ) {
353
- fossil_panic("error while writing %s", markfile_out);
353
+ fossil_fatal("error while writing %s", markfile_out);
354354
}
355355
}
356356
}
357357
--- src/export.c
+++ src/export.c
@@ -138,11 +138,11 @@
138 char line[100];
139 FILE *f;
140
141 f = fossil_fopen(markfile_in, "r");
142 if( f==0 ){
143 fossil_panic("cannot open %s for reading", markfile_in);
144 }
145 db_prepare(&qb, "INSERT OR IGNORE INTO oldblob VALUES (:rid)");
146 db_prepare(&qc, "INSERT OR IGNORE INTO oldcommit VALUES (:rid)");
147 while( fgets(line, sizeof(line), f)!=0 ){
148 if( *line == 'b' ){
@@ -154,11 +154,11 @@
154 db_bind_text(&qc, ":rid", line + 1);
155 db_step(&qc);
156 db_reset(&qc);
157 bag_insert(&vers, atoi(line + 1));
158 }else{
159 fossil_panic("bad input from %s: %s", markfile_in, line);
160 }
161 }
162 db_finalize(&qb);
163 db_finalize(&qc);
164 fclose(f);
@@ -335,11 +335,11 @@
335
336 if( markfile_out!=0 ){
337 FILE *f;
338 f = fossil_fopen(markfile_out, "w");
339 if( f == 0 ){
340 fossil_panic("cannot open %s for writing", markfile_out);
341 }
342 db_prepare(&q, "SELECT rid FROM oldblob");
343 while( db_step(&q)==SQLITE_ROW ){
344 fprintf(f, "b%d\n", db_column_int(&q, 0));
345 }
@@ -348,9 +348,9 @@
348 while( db_step(&q)==SQLITE_ROW ){
349 fprintf(f, "c%d\n", db_column_int(&q, 0));
350 }
351 db_finalize(&q);
352 if( ferror(f)!=0 || fclose(f)!=0 ) {
353 fossil_panic("error while writing %s", markfile_out);
354 }
355 }
356 }
357
--- src/export.c
+++ src/export.c
@@ -138,11 +138,11 @@
138 char line[100];
139 FILE *f;
140
141 f = fossil_fopen(markfile_in, "r");
142 if( f==0 ){
143 fossil_fatal("cannot open %s for reading", markfile_in);
144 }
145 db_prepare(&qb, "INSERT OR IGNORE INTO oldblob VALUES (:rid)");
146 db_prepare(&qc, "INSERT OR IGNORE INTO oldcommit VALUES (:rid)");
147 while( fgets(line, sizeof(line), f)!=0 ){
148 if( *line == 'b' ){
@@ -154,11 +154,11 @@
154 db_bind_text(&qc, ":rid", line + 1);
155 db_step(&qc);
156 db_reset(&qc);
157 bag_insert(&vers, atoi(line + 1));
158 }else{
159 fossil_fatal("bad input from %s: %s", markfile_in, line);
160 }
161 }
162 db_finalize(&qb);
163 db_finalize(&qc);
164 fclose(f);
@@ -335,11 +335,11 @@
335
336 if( markfile_out!=0 ){
337 FILE *f;
338 f = fossil_fopen(markfile_out, "w");
339 if( f == 0 ){
340 fossil_fatal("cannot open %s for writing", markfile_out);
341 }
342 db_prepare(&q, "SELECT rid FROM oldblob");
343 while( db_step(&q)==SQLITE_ROW ){
344 fprintf(f, "b%d\n", db_column_int(&q, 0));
345 }
@@ -348,9 +348,9 @@
348 while( db_step(&q)==SQLITE_ROW ){
349 fprintf(f, "c%d\n", db_column_int(&q, 0));
350 }
351 db_finalize(&q);
352 if( ferror(f)!=0 || fclose(f)!=0 ) {
353 fossil_fatal("error while writing %s", markfile_out);
354 }
355 }
356 }
357
+1 -1
--- src/finfo.c
+++ src/finfo.c
@@ -64,11 +64,11 @@
6464
int vid;
6565
6666
if( g.argc!=3 ) usage("-s|--status FILENAME");
6767
vid = db_lget_int("checkout", 0);
6868
if( vid==0 ){
69
- fossil_panic("no checkout to finfo files in");
69
+ fossil_fatal("no checkout to finfo files in");
7070
}
7171
vfile_check_signature(vid, CKSIG_ENOTFILE);
7272
file_tree_name(g.argv[2], &fname, 1);
7373
db_prepare(&q,
7474
"SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
7575
--- src/finfo.c
+++ src/finfo.c
@@ -64,11 +64,11 @@
64 int vid;
65
66 if( g.argc!=3 ) usage("-s|--status FILENAME");
67 vid = db_lget_int("checkout", 0);
68 if( vid==0 ){
69 fossil_panic("no checkout to finfo files in");
70 }
71 vfile_check_signature(vid, CKSIG_ENOTFILE);
72 file_tree_name(g.argv[2], &fname, 1);
73 db_prepare(&q,
74 "SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
75
--- src/finfo.c
+++ src/finfo.c
@@ -64,11 +64,11 @@
64 int vid;
65
66 if( g.argc!=3 ) usage("-s|--status FILENAME");
67 vid = db_lget_int("checkout", 0);
68 if( vid==0 ){
69 fossil_fatal("no checkout to finfo files in");
70 }
71 vfile_check_signature(vid, CKSIG_ENOTFILE);
72 file_tree_name(g.argv[2], &fname, 1);
73 db_prepare(&q,
74 "SELECT pathname, deleted, rid, chnged, coalesce(origname!=pathname,0)"
75
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
3737
FILE *pFile; /* File I/O for FILE: */
3838
char *zOutFile; /* Name of outbound file for FILE: */
3939
char *zInFile; /* Name of inbound file for FILE: */
4040
FILE *pLog; /* Log output here */
4141
} transport = {
42
- 0, 0, 0, 0, 0, 0, 0
42
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4343
};
4444
4545
/*
4646
** Information about the connection to the SSH subprocess when
4747
** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171171
rc = 1;
172172
#endif
173173
}else if( g.urlIsFile ){
174174
sqlite3_uint64 iRandId;
175175
sqlite3_randomness(sizeof(iRandId), &iRandId);
176
- transport.zOutFile = mprintf("%s-%llu-out.http",
176
+ transport.zOutFile = mprintf("%s-%llu-out.http",
177177
g.zRepositoryName, iRandId);
178
- transport.zInFile = mprintf("%s-%llu-in.http",
178
+ transport.zInFile = mprintf("%s-%llu-in.http",
179179
g.zRepositoryName, iRandId);
180180
transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181181
if( transport.pFile==0 ){
182182
fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183183
}
@@ -209,11 +209,11 @@
209209
}else if( g.urlIsHttps ){
210210
#ifdef FOSSIL_ENABLE_SSL
211211
ssl_close();
212212
#endif
213213
}else if( g.urlIsFile ){
214
- if( transport.pFile ){
214
+ if( transport.pFile ){
215215
fclose(transport.pFile);
216216
transport.pFile = 0;
217217
}
218218
file_delete(transport.zInFile);
219219
file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242242
while( n>0 ){
243243
sent = ssl_send(0, z, n);
244244
/* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245245
if( sent<=0 ) break;
246246
n -= sent;
247
- }
247
+ }
248248
#endif
249249
}else if( g.urlIsFile ){
250250
fwrite(z, 1, n, transport.pFile);
251251
}else{
252252
int sent;
253253
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
37 FILE *pFile; /* File I/O for FILE: */
38 char *zOutFile; /* Name of outbound file for FILE: */
39 char *zInFile; /* Name of inbound file for FILE: */
40 FILE *pLog; /* Log output here */
41 } transport = {
42 0, 0, 0, 0, 0, 0, 0
43 };
44
45 /*
46 ** Information about the connection to the SSH subprocess when
47 ** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171 rc = 1;
172 #endif
173 }else if( g.urlIsFile ){
174 sqlite3_uint64 iRandId;
175 sqlite3_randomness(sizeof(iRandId), &iRandId);
176 transport.zOutFile = mprintf("%s-%llu-out.http",
177 g.zRepositoryName, iRandId);
178 transport.zInFile = mprintf("%s-%llu-in.http",
179 g.zRepositoryName, iRandId);
180 transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181 if( transport.pFile==0 ){
182 fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183 }
@@ -209,11 +209,11 @@
209 }else if( g.urlIsHttps ){
210 #ifdef FOSSIL_ENABLE_SSL
211 ssl_close();
212 #endif
213 }else if( g.urlIsFile ){
214 if( transport.pFile ){
215 fclose(transport.pFile);
216 transport.pFile = 0;
217 }
218 file_delete(transport.zInFile);
219 file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242 while( n>0 ){
243 sent = ssl_send(0, z, n);
244 /* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245 if( sent<=0 ) break;
246 n -= sent;
247 }
248 #endif
249 }else if( g.urlIsFile ){
250 fwrite(z, 1, n, transport.pFile);
251 }else{
252 int sent;
253
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
37 FILE *pFile; /* File I/O for FILE: */
38 char *zOutFile; /* Name of outbound file for FILE: */
39 char *zInFile; /* Name of inbound file for FILE: */
40 FILE *pLog; /* Log output here */
41 } transport = {
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
43 };
44
45 /*
46 ** Information about the connection to the SSH subprocess when
47 ** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171 rc = 1;
172 #endif
173 }else if( g.urlIsFile ){
174 sqlite3_uint64 iRandId;
175 sqlite3_randomness(sizeof(iRandId), &iRandId);
176 transport.zOutFile = mprintf("%s-%llu-out.http",
177 g.zRepositoryName, iRandId);
178 transport.zInFile = mprintf("%s-%llu-in.http",
179 g.zRepositoryName, iRandId);
180 transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181 if( transport.pFile==0 ){
182 fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183 }
@@ -209,11 +209,11 @@
209 }else if( g.urlIsHttps ){
210 #ifdef FOSSIL_ENABLE_SSL
211 ssl_close();
212 #endif
213 }else if( g.urlIsFile ){
214 if( transport.pFile ){
215 fclose(transport.pFile);
216 transport.pFile = 0;
217 }
218 file_delete(transport.zInFile);
219 file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242 while( n>0 ){
243 sent = ssl_send(0, z, n);
244 /* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245 if( sent<=0 ) break;
246 n -= sent;
247 }
248 #endif
249 }else if( g.urlIsFile ){
250 fwrite(z, 1, n, transport.pFile);
251 }else{
252 int sent;
253
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
3737
FILE *pFile; /* File I/O for FILE: */
3838
char *zOutFile; /* Name of outbound file for FILE: */
3939
char *zInFile; /* Name of inbound file for FILE: */
4040
FILE *pLog; /* Log output here */
4141
} transport = {
42
- 0, 0, 0, 0, 0, 0, 0
42
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
4343
};
4444
4545
/*
4646
** Information about the connection to the SSH subprocess when
4747
** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171171
rc = 1;
172172
#endif
173173
}else if( g.urlIsFile ){
174174
sqlite3_uint64 iRandId;
175175
sqlite3_randomness(sizeof(iRandId), &iRandId);
176
- transport.zOutFile = mprintf("%s-%llu-out.http",
176
+ transport.zOutFile = mprintf("%s-%llu-out.http",
177177
g.zRepositoryName, iRandId);
178
- transport.zInFile = mprintf("%s-%llu-in.http",
178
+ transport.zInFile = mprintf("%s-%llu-in.http",
179179
g.zRepositoryName, iRandId);
180180
transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181181
if( transport.pFile==0 ){
182182
fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183183
}
@@ -209,11 +209,11 @@
209209
}else if( g.urlIsHttps ){
210210
#ifdef FOSSIL_ENABLE_SSL
211211
ssl_close();
212212
#endif
213213
}else if( g.urlIsFile ){
214
- if( transport.pFile ){
214
+ if( transport.pFile ){
215215
fclose(transport.pFile);
216216
transport.pFile = 0;
217217
}
218218
file_delete(transport.zInFile);
219219
file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242242
while( n>0 ){
243243
sent = ssl_send(0, z, n);
244244
/* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245245
if( sent<=0 ) break;
246246
n -= sent;
247
- }
247
+ }
248248
#endif
249249
}else if( g.urlIsFile ){
250250
fwrite(z, 1, n, transport.pFile);
251251
}else{
252252
int sent;
253253
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
37 FILE *pFile; /* File I/O for FILE: */
38 char *zOutFile; /* Name of outbound file for FILE: */
39 char *zInFile; /* Name of inbound file for FILE: */
40 FILE *pLog; /* Log output here */
41 } transport = {
42 0, 0, 0, 0, 0, 0, 0
43 };
44
45 /*
46 ** Information about the connection to the SSH subprocess when
47 ** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171 rc = 1;
172 #endif
173 }else if( g.urlIsFile ){
174 sqlite3_uint64 iRandId;
175 sqlite3_randomness(sizeof(iRandId), &iRandId);
176 transport.zOutFile = mprintf("%s-%llu-out.http",
177 g.zRepositoryName, iRandId);
178 transport.zInFile = mprintf("%s-%llu-in.http",
179 g.zRepositoryName, iRandId);
180 transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181 if( transport.pFile==0 ){
182 fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183 }
@@ -209,11 +209,11 @@
209 }else if( g.urlIsHttps ){
210 #ifdef FOSSIL_ENABLE_SSL
211 ssl_close();
212 #endif
213 }else if( g.urlIsFile ){
214 if( transport.pFile ){
215 fclose(transport.pFile);
216 transport.pFile = 0;
217 }
218 file_delete(transport.zInFile);
219 file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242 while( n>0 ){
243 sent = ssl_send(0, z, n);
244 /* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245 if( sent<=0 ) break;
246 n -= sent;
247 }
248 #endif
249 }else if( g.urlIsFile ){
250 fwrite(z, 1, n, transport.pFile);
251 }else{
252 int sent;
253
--- src/http_transport.c
+++ src/http_transport.c
@@ -37,11 +37,11 @@
37 FILE *pFile; /* File I/O for FILE: */
38 char *zOutFile; /* Name of outbound file for FILE: */
39 char *zInFile; /* Name of inbound file for FILE: */
40 FILE *pLog; /* Log output here */
41 } transport = {
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
43 };
44
45 /*
46 ** Information about the connection to the SSH subprocess when
47 ** using the ssh:// sync method.
@@ -171,13 +171,13 @@
171 rc = 1;
172 #endif
173 }else if( g.urlIsFile ){
174 sqlite3_uint64 iRandId;
175 sqlite3_randomness(sizeof(iRandId), &iRandId);
176 transport.zOutFile = mprintf("%s-%llu-out.http",
177 g.zRepositoryName, iRandId);
178 transport.zInFile = mprintf("%s-%llu-in.http",
179 g.zRepositoryName, iRandId);
180 transport.pFile = fossil_fopen(transport.zOutFile, "wb");
181 if( transport.pFile==0 ){
182 fossil_fatal("cannot output temporary file: %s", transport.zOutFile);
183 }
@@ -209,11 +209,11 @@
209 }else if( g.urlIsHttps ){
210 #ifdef FOSSIL_ENABLE_SSL
211 ssl_close();
212 #endif
213 }else if( g.urlIsFile ){
214 if( transport.pFile ){
215 fclose(transport.pFile);
216 transport.pFile = 0;
217 }
218 file_delete(transport.zInFile);
219 file_delete(transport.zOutFile);
@@ -242,11 +242,11 @@
242 while( n>0 ){
243 sent = ssl_send(0, z, n);
244 /* printf("Sent %d of %d bytes\n", sent, n); fflush(stdout); */
245 if( sent<=0 ) break;
246 n -= sent;
247 }
248 #endif
249 }else if( g.urlIsFile ){
250 fwrite(z, 1, n, transport.pFile);
251 }else{
252 int sent;
253
+28 -28
--- src/info.c
+++ src/info.c
@@ -215,11 +215,11 @@
215215
db_int(-1, "SELECT count(*) FROM event WHERE type='ci' /*scan*/"));
216216
}else{
217217
int rid;
218218
rid = name_to_rid(g.argv[2]);
219219
if( rid==0 ){
220
- fossil_panic("no such object: %s\n", g.argv[2]);
220
+ fossil_fatal("no such object: %s\n", g.argv[2]);
221221
}
222222
show_common_info(rid, "uuid:", 1, 1);
223223
}
224224
}
225225
@@ -471,11 +471,11 @@
471471
** With /vinfo and /info, only a list of the changed files are
472472
** shown, without diffs. This behavior is inverted if the
473473
** "show-version-diffs" setting is turned on.
474474
*/
475475
void ci_page(void){
476
- Stmt q;
476
+ Stmt q1, q2, q3;
477477
int rid;
478478
int isLeaf;
479479
int verboseFlag; /* True to show diffs */
480480
int sideBySide; /* True for side-by-side diffs */
481481
u64 diffFlags; /* Flag parameter for text_diff() */
@@ -502,21 +502,21 @@
502502
"SELECT uuid FROM plink, blob"
503503
" WHERE plink.cid=%d AND blob.rid=plink.pid AND plink.isprim",
504504
rid
505505
);
506506
isLeaf = is_a_leaf(rid);
507
- db_prepare(&q,
507
+ db_prepare(&q1,
508508
"SELECT uuid, datetime(mtime, 'localtime'), user, comment,"
509509
" datetime(omtime, 'localtime'), mtime"
510510
" FROM blob, event"
511511
" WHERE blob.rid=%d"
512512
" AND event.objid=%d",
513513
rid, rid
514514
);
515515
sideBySide = !is_false(PD("sbs","1"));
516
- if( db_step(&q)==SQLITE_ROW ){
517
- const char *zUuid = db_column_text(&q, 0);
516
+ if( db_step(&q1)==SQLITE_ROW ){
517
+ const char *zUuid = db_column_text(&q1, 0);
518518
char *zTitle = mprintf("Check-in [%.10s]", zUuid);
519519
char *zEUser, *zEComment;
520520
const char *zUser;
521521
const char *zComment;
522522
const char *zDate;
@@ -529,14 +529,14 @@
529529
"SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
530530
TAG_USER, rid);
531531
zEComment = db_text(0,
532532
"SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
533533
TAG_COMMENT, rid);
534
- zUser = db_column_text(&q, 2);
535
- zComment = db_column_text(&q, 3);
536
- zDate = db_column_text(&q,1);
537
- zOrigDate = db_column_text(&q, 4);
534
+ zUser = db_column_text(&q1, 2);
535
+ zComment = db_column_text(&q1, 3);
536
+ zDate = db_column_text(&q1,1);
537
+ zOrigDate = db_column_text(&q1, 4);
538538
@ <div class="section">Overview</div>
539539
@ <table class="label-value">
540540
@ <tr><th>SHA1&nbsp;Hash:</th><td>%s(zUuid)
541541
if( g.perm.Setup ){
542542
@ (Record ID: %d(rid))
@@ -562,25 +562,25 @@
562562
@ <tr><th>Original&nbsp;Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
563563
}else{
564564
@ <tr><th>Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
565565
}
566566
if( g.perm.Admin ){
567
- db_prepare(&q,
567
+ db_prepare(&q2,
568568
"SELECT rcvfrom.ipaddr, user.login, datetime(rcvfrom.mtime)"
569569
" FROM blob JOIN rcvfrom USING(rcvid) LEFT JOIN user USING(uid)"
570570
" WHERE blob.rid=%d",
571571
rid
572572
);
573
- if( db_step(&q)==SQLITE_ROW ){
574
- const char *zIpAddr = db_column_text(&q, 0);
575
- const char *zUser = db_column_text(&q, 1);
576
- const char *zDate = db_column_text(&q, 2);
573
+ if( db_step(&q2)==SQLITE_ROW ){
574
+ const char *zIpAddr = db_column_text(&q2, 0);
575
+ const char *zUser = db_column_text(&q2, 1);
576
+ const char *zDate = db_column_text(&q2, 2);
577577
if( zUser==0 || zUser[0]==0 ) zUser = "unknown";
578578
@ <tr><th>Received&nbsp;From:</th>
579579
@ <td>%h(zUser) @ %h(zIpAddr) on %s(zDate)</td></tr>
580580
}
581
- db_finalize(&q);
581
+ db_finalize(&q2);
582582
}
583583
if( g.perm.Hyperlink ){
584584
const char *zProjName = db_get("project-name", "unnamed");
585585
@ <tr><th>Timelines:</th><td>
586586
@ %z(href("%R/timeline?f=%S",zUuid))family</a>
@@ -591,19 +591,19 @@
591591
@ | %z(href("%R/timeline?d=%S",zUuid))descendants</a>
592592
}
593593
if( zParent && !isLeaf ){
594594
@ | %z(href("%R/timeline?dp=%S",zUuid))both</a>
595595
}
596
- db_prepare(&q, "SELECT substr(tag.tagname,5) FROM tagxref, tag "
596
+ db_prepare(&q2,"SELECT substr(tag.tagname,5) FROM tagxref, tag "
597597
" WHERE rid=%d AND tagtype>0 "
598598
" AND tag.tagid=tagxref.tagid "
599599
" AND +tag.tagname GLOB 'sym-*'", rid);
600
- while( db_step(&q)==SQLITE_ROW ){
601
- const char *zTagName = db_column_text(&q, 0);
600
+ while( db_step(&q2)==SQLITE_ROW ){
601
+ const char *zTagName = db_column_text(&q2, 0);
602602
@ | %z(href("%R/timeline?r=%T",zTagName))%h(zTagName)</a>
603603
}
604
- db_finalize(&q);
604
+ db_finalize(&q2);
605605
606606
607607
/* The Download: line */
608608
if( g.perm.Zip ){
609609
char *zUrl = mprintf("%R/tarball/%t-%S.tar.gz?uuid=%s",
@@ -630,11 +630,11 @@
630630
@ </table>
631631
}else{
632632
style_header("Check-in Information");
633633
login_anonymous_available();
634634
}
635
- db_finalize(&q);
635
+ db_finalize(&q1);
636636
showTags(rid, "");
637637
if( zParent ){
638638
@ <div class="section">Changes</div>
639639
@ <div class="sectionmenu">
640640
verboseFlag = g.zPath[0]!='c';
@@ -677,11 +677,11 @@
677677
@ patch</a></div>
678678
if( pRe ){
679679
@ <p><b>Only differences that match regular expression "%h(zRe)"
680680
@ are shown.</b></p>
681681
}
682
- db_prepare(&q,
682
+ db_prepare(&q3,
683683
"SELECT name,"
684684
" mperm,"
685685
" (SELECT uuid FROM blob WHERE rid=mlink.pid),"
686686
" (SELECT uuid FROM blob WHERE rid=mlink.fid),"
687687
" (SELECT name FROM filename WHERE filename.fnid=mlink.pfnid)"
@@ -691,19 +691,19 @@
691691
" OR mlink.fnid NOT IN (SELECT pfnid FROM mlink WHERE mid=%d))"
692692
" ORDER BY name /*sort*/",
693693
rid, rid
694694
);
695695
diffFlags = construct_diff_flags(verboseFlag, sideBySide);
696
- while( db_step(&q)==SQLITE_ROW ){
697
- const char *zName = db_column_text(&q,0);
698
- int mperm = db_column_int(&q, 1);
699
- const char *zOld = db_column_text(&q,2);
700
- const char *zNew = db_column_text(&q,3);
701
- const char *zOldName = db_column_text(&q, 4);
696
+ while( db_step(&q3)==SQLITE_ROW ){
697
+ const char *zName = db_column_text(&q3,0);
698
+ int mperm = db_column_int(&q3, 1);
699
+ const char *zOld = db_column_text(&q3,2);
700
+ const char *zNew = db_column_text(&q3,3);
701
+ const char *zOldName = db_column_text(&q3, 4);
702702
append_file_change_line(zName, zOld, zNew, zOldName, diffFlags,pRe,mperm);
703703
}
704
- db_finalize(&q);
704
+ db_finalize(&q3);
705705
}
706706
append_diff_javascript(sideBySide);
707707
style_footer();
708708
}
709709
710710
--- src/info.c
+++ src/info.c
@@ -215,11 +215,11 @@
215 db_int(-1, "SELECT count(*) FROM event WHERE type='ci' /*scan*/"));
216 }else{
217 int rid;
218 rid = name_to_rid(g.argv[2]);
219 if( rid==0 ){
220 fossil_panic("no such object: %s\n", g.argv[2]);
221 }
222 show_common_info(rid, "uuid:", 1, 1);
223 }
224 }
225
@@ -471,11 +471,11 @@
471 ** With /vinfo and /info, only a list of the changed files are
472 ** shown, without diffs. This behavior is inverted if the
473 ** "show-version-diffs" setting is turned on.
474 */
475 void ci_page(void){
476 Stmt q;
477 int rid;
478 int isLeaf;
479 int verboseFlag; /* True to show diffs */
480 int sideBySide; /* True for side-by-side diffs */
481 u64 diffFlags; /* Flag parameter for text_diff() */
@@ -502,21 +502,21 @@
502 "SELECT uuid FROM plink, blob"
503 " WHERE plink.cid=%d AND blob.rid=plink.pid AND plink.isprim",
504 rid
505 );
506 isLeaf = is_a_leaf(rid);
507 db_prepare(&q,
508 "SELECT uuid, datetime(mtime, 'localtime'), user, comment,"
509 " datetime(omtime, 'localtime'), mtime"
510 " FROM blob, event"
511 " WHERE blob.rid=%d"
512 " AND event.objid=%d",
513 rid, rid
514 );
515 sideBySide = !is_false(PD("sbs","1"));
516 if( db_step(&q)==SQLITE_ROW ){
517 const char *zUuid = db_column_text(&q, 0);
518 char *zTitle = mprintf("Check-in [%.10s]", zUuid);
519 char *zEUser, *zEComment;
520 const char *zUser;
521 const char *zComment;
522 const char *zDate;
@@ -529,14 +529,14 @@
529 "SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
530 TAG_USER, rid);
531 zEComment = db_text(0,
532 "SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
533 TAG_COMMENT, rid);
534 zUser = db_column_text(&q, 2);
535 zComment = db_column_text(&q, 3);
536 zDate = db_column_text(&q,1);
537 zOrigDate = db_column_text(&q, 4);
538 @ <div class="section">Overview</div>
539 @ <table class="label-value">
540 @ <tr><th>SHA1&nbsp;Hash:</th><td>%s(zUuid)
541 if( g.perm.Setup ){
542 @ (Record ID: %d(rid))
@@ -562,25 +562,25 @@
562 @ <tr><th>Original&nbsp;Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
563 }else{
564 @ <tr><th>Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
565 }
566 if( g.perm.Admin ){
567 db_prepare(&q,
568 "SELECT rcvfrom.ipaddr, user.login, datetime(rcvfrom.mtime)"
569 " FROM blob JOIN rcvfrom USING(rcvid) LEFT JOIN user USING(uid)"
570 " WHERE blob.rid=%d",
571 rid
572 );
573 if( db_step(&q)==SQLITE_ROW ){
574 const char *zIpAddr = db_column_text(&q, 0);
575 const char *zUser = db_column_text(&q, 1);
576 const char *zDate = db_column_text(&q, 2);
577 if( zUser==0 || zUser[0]==0 ) zUser = "unknown";
578 @ <tr><th>Received&nbsp;From:</th>
579 @ <td>%h(zUser) @ %h(zIpAddr) on %s(zDate)</td></tr>
580 }
581 db_finalize(&q);
582 }
583 if( g.perm.Hyperlink ){
584 const char *zProjName = db_get("project-name", "unnamed");
585 @ <tr><th>Timelines:</th><td>
586 @ %z(href("%R/timeline?f=%S",zUuid))family</a>
@@ -591,19 +591,19 @@
591 @ | %z(href("%R/timeline?d=%S",zUuid))descendants</a>
592 }
593 if( zParent && !isLeaf ){
594 @ | %z(href("%R/timeline?dp=%S",zUuid))both</a>
595 }
596 db_prepare(&q, "SELECT substr(tag.tagname,5) FROM tagxref, tag "
597 " WHERE rid=%d AND tagtype>0 "
598 " AND tag.tagid=tagxref.tagid "
599 " AND +tag.tagname GLOB 'sym-*'", rid);
600 while( db_step(&q)==SQLITE_ROW ){
601 const char *zTagName = db_column_text(&q, 0);
602 @ | %z(href("%R/timeline?r=%T",zTagName))%h(zTagName)</a>
603 }
604 db_finalize(&q);
605
606
607 /* The Download: line */
608 if( g.perm.Zip ){
609 char *zUrl = mprintf("%R/tarball/%t-%S.tar.gz?uuid=%s",
@@ -630,11 +630,11 @@
630 @ </table>
631 }else{
632 style_header("Check-in Information");
633 login_anonymous_available();
634 }
635 db_finalize(&q);
636 showTags(rid, "");
637 if( zParent ){
638 @ <div class="section">Changes</div>
639 @ <div class="sectionmenu">
640 verboseFlag = g.zPath[0]!='c';
@@ -677,11 +677,11 @@
677 @ patch</a></div>
678 if( pRe ){
679 @ <p><b>Only differences that match regular expression "%h(zRe)"
680 @ are shown.</b></p>
681 }
682 db_prepare(&q,
683 "SELECT name,"
684 " mperm,"
685 " (SELECT uuid FROM blob WHERE rid=mlink.pid),"
686 " (SELECT uuid FROM blob WHERE rid=mlink.fid),"
687 " (SELECT name FROM filename WHERE filename.fnid=mlink.pfnid)"
@@ -691,19 +691,19 @@
691 " OR mlink.fnid NOT IN (SELECT pfnid FROM mlink WHERE mid=%d))"
692 " ORDER BY name /*sort*/",
693 rid, rid
694 );
695 diffFlags = construct_diff_flags(verboseFlag, sideBySide);
696 while( db_step(&q)==SQLITE_ROW ){
697 const char *zName = db_column_text(&q,0);
698 int mperm = db_column_int(&q, 1);
699 const char *zOld = db_column_text(&q,2);
700 const char *zNew = db_column_text(&q,3);
701 const char *zOldName = db_column_text(&q, 4);
702 append_file_change_line(zName, zOld, zNew, zOldName, diffFlags,pRe,mperm);
703 }
704 db_finalize(&q);
705 }
706 append_diff_javascript(sideBySide);
707 style_footer();
708 }
709
710
--- src/info.c
+++ src/info.c
@@ -215,11 +215,11 @@
215 db_int(-1, "SELECT count(*) FROM event WHERE type='ci' /*scan*/"));
216 }else{
217 int rid;
218 rid = name_to_rid(g.argv[2]);
219 if( rid==0 ){
220 fossil_fatal("no such object: %s\n", g.argv[2]);
221 }
222 show_common_info(rid, "uuid:", 1, 1);
223 }
224 }
225
@@ -471,11 +471,11 @@
471 ** With /vinfo and /info, only a list of the changed files are
472 ** shown, without diffs. This behavior is inverted if the
473 ** "show-version-diffs" setting is turned on.
474 */
475 void ci_page(void){
476 Stmt q1, q2, q3;
477 int rid;
478 int isLeaf;
479 int verboseFlag; /* True to show diffs */
480 int sideBySide; /* True for side-by-side diffs */
481 u64 diffFlags; /* Flag parameter for text_diff() */
@@ -502,21 +502,21 @@
502 "SELECT uuid FROM plink, blob"
503 " WHERE plink.cid=%d AND blob.rid=plink.pid AND plink.isprim",
504 rid
505 );
506 isLeaf = is_a_leaf(rid);
507 db_prepare(&q1,
508 "SELECT uuid, datetime(mtime, 'localtime'), user, comment,"
509 " datetime(omtime, 'localtime'), mtime"
510 " FROM blob, event"
511 " WHERE blob.rid=%d"
512 " AND event.objid=%d",
513 rid, rid
514 );
515 sideBySide = !is_false(PD("sbs","1"));
516 if( db_step(&q1)==SQLITE_ROW ){
517 const char *zUuid = db_column_text(&q1, 0);
518 char *zTitle = mprintf("Check-in [%.10s]", zUuid);
519 char *zEUser, *zEComment;
520 const char *zUser;
521 const char *zComment;
522 const char *zDate;
@@ -529,14 +529,14 @@
529 "SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
530 TAG_USER, rid);
531 zEComment = db_text(0,
532 "SELECT value FROM tagxref WHERE tagid=%d AND rid=%d",
533 TAG_COMMENT, rid);
534 zUser = db_column_text(&q1, 2);
535 zComment = db_column_text(&q1, 3);
536 zDate = db_column_text(&q1,1);
537 zOrigDate = db_column_text(&q1, 4);
538 @ <div class="section">Overview</div>
539 @ <table class="label-value">
540 @ <tr><th>SHA1&nbsp;Hash:</th><td>%s(zUuid)
541 if( g.perm.Setup ){
542 @ (Record ID: %d(rid))
@@ -562,25 +562,25 @@
562 @ <tr><th>Original&nbsp;Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
563 }else{
564 @ <tr><th>Comment:</th><td class="infoComment">%!w(zComment)</td></tr>
565 }
566 if( g.perm.Admin ){
567 db_prepare(&q2,
568 "SELECT rcvfrom.ipaddr, user.login, datetime(rcvfrom.mtime)"
569 " FROM blob JOIN rcvfrom USING(rcvid) LEFT JOIN user USING(uid)"
570 " WHERE blob.rid=%d",
571 rid
572 );
573 if( db_step(&q2)==SQLITE_ROW ){
574 const char *zIpAddr = db_column_text(&q2, 0);
575 const char *zUser = db_column_text(&q2, 1);
576 const char *zDate = db_column_text(&q2, 2);
577 if( zUser==0 || zUser[0]==0 ) zUser = "unknown";
578 @ <tr><th>Received&nbsp;From:</th>
579 @ <td>%h(zUser) @ %h(zIpAddr) on %s(zDate)</td></tr>
580 }
581 db_finalize(&q2);
582 }
583 if( g.perm.Hyperlink ){
584 const char *zProjName = db_get("project-name", "unnamed");
585 @ <tr><th>Timelines:</th><td>
586 @ %z(href("%R/timeline?f=%S",zUuid))family</a>
@@ -591,19 +591,19 @@
591 @ | %z(href("%R/timeline?d=%S",zUuid))descendants</a>
592 }
593 if( zParent && !isLeaf ){
594 @ | %z(href("%R/timeline?dp=%S",zUuid))both</a>
595 }
596 db_prepare(&q2,"SELECT substr(tag.tagname,5) FROM tagxref, tag "
597 " WHERE rid=%d AND tagtype>0 "
598 " AND tag.tagid=tagxref.tagid "
599 " AND +tag.tagname GLOB 'sym-*'", rid);
600 while( db_step(&q2)==SQLITE_ROW ){
601 const char *zTagName = db_column_text(&q2, 0);
602 @ | %z(href("%R/timeline?r=%T",zTagName))%h(zTagName)</a>
603 }
604 db_finalize(&q2);
605
606
607 /* The Download: line */
608 if( g.perm.Zip ){
609 char *zUrl = mprintf("%R/tarball/%t-%S.tar.gz?uuid=%s",
@@ -630,11 +630,11 @@
630 @ </table>
631 }else{
632 style_header("Check-in Information");
633 login_anonymous_available();
634 }
635 db_finalize(&q1);
636 showTags(rid, "");
637 if( zParent ){
638 @ <div class="section">Changes</div>
639 @ <div class="sectionmenu">
640 verboseFlag = g.zPath[0]!='c';
@@ -677,11 +677,11 @@
677 @ patch</a></div>
678 if( pRe ){
679 @ <p><b>Only differences that match regular expression "%h(zRe)"
680 @ are shown.</b></p>
681 }
682 db_prepare(&q3,
683 "SELECT name,"
684 " mperm,"
685 " (SELECT uuid FROM blob WHERE rid=mlink.pid),"
686 " (SELECT uuid FROM blob WHERE rid=mlink.fid),"
687 " (SELECT name FROM filename WHERE filename.fnid=mlink.pfnid)"
@@ -691,19 +691,19 @@
691 " OR mlink.fnid NOT IN (SELECT pfnid FROM mlink WHERE mid=%d))"
692 " ORDER BY name /*sort*/",
693 rid, rid
694 );
695 diffFlags = construct_diff_flags(verboseFlag, sideBySide);
696 while( db_step(&q3)==SQLITE_ROW ){
697 const char *zName = db_column_text(&q3,0);
698 int mperm = db_column_int(&q3, 1);
699 const char *zOld = db_column_text(&q3,2);
700 const char *zNew = db_column_text(&q3,3);
701 const char *zOldName = db_column_text(&q3, 4);
702 append_file_change_line(zName, zOld, zNew, zOldName, diffFlags,pRe,mperm);
703 }
704 db_finalize(&q3);
705 }
706 append_diff_javascript(sideBySide);
707 style_footer();
708 }
709
710
--- src/json_branch.c
+++ src/json_branch.c
@@ -288,15 +288,15 @@
288288
md5sum_blob(&branch, &mcksum);
289289
blob_appendf(&branch, "Z %b\n", &mcksum);
290290
291291
brid = content_put(&branch);
292292
if( brid==0 ){
293
- fossil_panic("Problem committing manifest: %s", g.zErrMsg);
293
+ fossil_fatal("Problem committing manifest: %s", g.zErrMsg);
294294
}
295295
db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
296296
if( manifest_crosslink(brid, &branch)==0 ){
297
- fossil_panic("unable to install new manifest");
297
+ fossil_fatal("unable to install new manifest");
298298
}
299299
assert( blob_is_reset(&branch) );
300300
content_deltify(rootid, brid, 0);
301301
if( zNewRid ){
302302
*zNewRid = brid;
303303
--- src/json_branch.c
+++ src/json_branch.c
@@ -288,15 +288,15 @@
288 md5sum_blob(&branch, &mcksum);
289 blob_appendf(&branch, "Z %b\n", &mcksum);
290
291 brid = content_put(&branch);
292 if( brid==0 ){
293 fossil_panic("Problem committing manifest: %s", g.zErrMsg);
294 }
295 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
296 if( manifest_crosslink(brid, &branch)==0 ){
297 fossil_panic("unable to install new manifest");
298 }
299 assert( blob_is_reset(&branch) );
300 content_deltify(rootid, brid, 0);
301 if( zNewRid ){
302 *zNewRid = brid;
303
--- src/json_branch.c
+++ src/json_branch.c
@@ -288,15 +288,15 @@
288 md5sum_blob(&branch, &mcksum);
289 blob_appendf(&branch, "Z %b\n", &mcksum);
290
291 brid = content_put(&branch);
292 if( brid==0 ){
293 fossil_fatal("Problem committing manifest: %s", g.zErrMsg);
294 }
295 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", brid);
296 if( manifest_crosslink(brid, &branch)==0 ){
297 fossil_fatal("unable to install new manifest");
298 }
299 assert( blob_is_reset(&branch) );
300 content_deltify(rootid, brid, 0);
301 if( zNewRid ){
302 *zNewRid = brid;
303
--- src/json_status.c
+++ src/json_status.c
@@ -126,10 +126,14 @@
126126
}
127127
}else if( 2==isChnged ){
128128
zStatus = "updatedByMerge";
129129
}else if( 3==isChnged ){
130130
zStatus = "addedByMerge";
131
+ }else if( 4==isChnged ){
132
+ zStatus = "updatedByIntegrate";
133
+ }else if( 5==isChnged ){
134
+ zStatus = "addedByIntegrate";
131135
}else if( 1==isChnged ){
132136
if( file_contains_merge_marker(zFullName) ){
133137
zStatus = "conflict";
134138
}else{
135139
zStatus = "edited";
136140
--- src/json_status.c
+++ src/json_status.c
@@ -126,10 +126,14 @@
126 }
127 }else if( 2==isChnged ){
128 zStatus = "updatedByMerge";
129 }else if( 3==isChnged ){
130 zStatus = "addedByMerge";
 
 
 
 
131 }else if( 1==isChnged ){
132 if( file_contains_merge_marker(zFullName) ){
133 zStatus = "conflict";
134 }else{
135 zStatus = "edited";
136
--- src/json_status.c
+++ src/json_status.c
@@ -126,10 +126,14 @@
126 }
127 }else if( 2==isChnged ){
128 zStatus = "updatedByMerge";
129 }else if( 3==isChnged ){
130 zStatus = "addedByMerge";
131 }else if( 4==isChnged ){
132 zStatus = "updatedByIntegrate";
133 }else if( 5==isChnged ){
134 zStatus = "addedByIntegrate";
135 }else if( 1==isChnged ){
136 if( file_contains_merge_marker(zFullName) ){
137 zStatus = "conflict";
138 }else{
139 zStatus = "edited";
140
+1 -1
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
6969
** Return a path appropriate for setting a cookie.
7070
**
7171
** The path is g.zTop for single-repo cookies. It is "/" for
7272
** cookies of a login-group.
7373
*/
74
-static const char *login_cookie_path(void){
74
+const char *login_cookie_path(void){
7575
if( login_group_name()==0 ){
7676
return g.zTop;
7777
}else{
7878
return "/";
7979
}
8080
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
69 ** Return a path appropriate for setting a cookie.
70 **
71 ** The path is g.zTop for single-repo cookies. It is "/" for
72 ** cookies of a login-group.
73 */
74 static const char *login_cookie_path(void){
75 if( login_group_name()==0 ){
76 return g.zTop;
77 }else{
78 return "/";
79 }
80
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
69 ** Return a path appropriate for setting a cookie.
70 **
71 ** The path is g.zTop for single-repo cookies. It is "/" for
72 ** cookies of a login-group.
73 */
74 const char *login_cookie_path(void){
75 if( login_group_name()==0 ){
76 return g.zTop;
77 }else{
78 return "/";
79 }
80
+1 -1
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
6969
** Return a path appropriate for setting a cookie.
7070
**
7171
** The path is g.zTop for single-repo cookies. It is "/" for
7272
** cookies of a login-group.
7373
*/
74
-static const char *login_cookie_path(void){
74
+const char *login_cookie_path(void){
7575
if( login_group_name()==0 ){
7676
return g.zTop;
7777
}else{
7878
return "/";
7979
}
8080
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
69 ** Return a path appropriate for setting a cookie.
70 **
71 ** The path is g.zTop for single-repo cookies. It is "/" for
72 ** cookies of a login-group.
73 */
74 static const char *login_cookie_path(void){
75 if( login_group_name()==0 ){
76 return g.zTop;
77 }else{
78 return "/";
79 }
80
--- src/login.c
+++ src/login.c
@@ -69,11 +69,11 @@
69 ** Return a path appropriate for setting a cookie.
70 **
71 ** The path is g.zTop for single-repo cookies. It is "/" for
72 ** cookies of a login-group.
73 */
74 const char *login_cookie_path(void){
75 if( login_group_name()==0 ){
76 return g.zTop;
77 }else{
78 return "/";
79 }
80
+24 -10
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114114
** All global variables are in this structure.
115115
*/
116116
struct Global {
117117
int argc; char **argv; /* Command-line arguments to the program */
118118
char *nameOfExe; /* Full path of executable. */
119
+ const char *zErrlog; /* Log errors to this file, if not NULL */
119120
int isConst; /* True if the output is unchanging */
120121
sqlite3 *db; /* The connection to the databases */
121122
sqlite3 *dbConfig; /* Separate connection for global_config table */
122123
int useAttach; /* True if global_config is attached to repository */
123124
const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
374375
unsigned int i, j, k; /* Loop counters */
375376
int n; /* Number of bytes in one line */
376377
char *z; /* General use string pointer */
377378
char **newArgv; /* New expanded g.argv under construction */
378379
char const * zFileName; /* input file name */
379
- FILE * zInFile; /* input FILE */
380
+ FILE *inFile; /* input FILE */
380381
#if defined(_WIN32)
381382
wchar_t buf[MAX_PATH];
382383
#endif
383384
384385
g.argc = argc;
@@ -404,21 +405,21 @@
404405
if( fossil_strcmp(z, "args")==0 ) break;
405406
}
406407
if( i>=g.argc-1 ) return;
407408
408409
zFileName = g.argv[i+1];
409
- zInFile = (0==strcmp("-",zFileName))
410
+ inFile = (0==strcmp("-",zFileName))
410411
? stdin
411412
: fossil_fopen(zFileName,"rb");
412
- if(!zInFile){
413
- fossil_panic("Cannot open -args file [%s]", zFileName);
413
+ if(!inFile){
414
+ fossil_fatal("Cannot open -args file [%s]", zFileName);
414415
}else{
415
- blob_read_from_channel(&file, zInFile, -1);
416
- if(stdin != zInFile){
417
- fclose(zInFile);
416
+ blob_read_from_channel(&file, inFile, -1);
417
+ if(stdin != inFile){
418
+ fclose(inFile);
418419
}
419
- zInFile = NULL;
420
+ inFile = NULL;
420421
}
421422
blob_to_utf8_no_bom(&file, 1);
422423
z = blob_str(&file);
423424
for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
424425
newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
592593
if( g.fSqlTrace ) g.fSqlStats = 1;
593594
g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
594595
g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
595596
g.zLogin = find_option("user", "U", 1);
596597
g.zSSLIdentity = find_option("ssl-identity", 0, 1);
598
+ g.zErrlog = find_option("errorlog", 0, 1);
597599
if( find_option("utc",0,0) ) g.fTimeFormat = 1;
598600
if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
599601
if( zChdir && file_chdir(zChdir, 0) ){
600602
fossil_fatal("unable to change directories to %s", zChdir);
601603
}
@@ -611,10 +613,11 @@
611613
g.argc++;
612614
g.argv = zNewArgv;
613615
}
614616
zCmdName = g.argv[1];
615617
}
618
+ if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
616619
rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
617620
if( rc==1 ){
618621
fossil_fatal("%s: unknown command: %s\n"
619622
"%s: use \"help\" for more information\n",
620623
g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
797800
return;
798801
}else{
799802
fossil_print("Compiled on %s %s using %s (%d-bit)\n",
800803
__DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
801804
fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
805
+ fossil_print("Schema version %s\n", AUX_SCHEMA);
802806
fossil_print("zlib %s\n", ZLIB_VERSION);
803807
#if defined(FOSSIL_ENABLE_SSL)
804808
fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
805809
#endif
806810
#if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
11191123
fossil_fatal("unable to chroot into %s", zDir);
11201124
}
11211125
zRepo = "/";
11221126
}else{
11231127
for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1124
- if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1128
+ if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
11251129
if( i>0 ){
11261130
zDir[i] = 0;
11271131
if( file_chdir(zDir, 1) ){
11281132
fossil_fatal("unable to chroot into %s", zDir);
11291133
}
@@ -1373,11 +1377,11 @@
13731377
*/
13741378
#ifdef FOSSIL_ENABLE_JSON
13751379
if(!g.json.isJsonMode){
13761380
#endif
13771381
dehttpize(g.zExtra);
1378
- cgi_set_parameter_nocopy("name", g.zExtra);
1382
+ cgi_set_parameter_nocopy("name", g.zExtra, 1);
13791383
#ifdef FOSSIL_ENABLE_JSON
13801384
}
13811385
#endif
13821386
}
13831387
@@ -1510,10 +1514,14 @@
15101514
if( blob_buffer(&key)[0]=='#' ) continue;
15111515
if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
15121516
g.fDebug = fossil_fopen(blob_str(&value), "ab");
15131517
blob_reset(&value);
15141518
continue;
1519
+ }
1520
+ if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1521
+ g.zErrlog = mprintf("%s", blob_str(&value));
1522
+ continue;
15151523
}
15161524
if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
15171525
cgi_setenv("HOME", blob_str(&value));
15181526
blob_reset(&value);
15191527
continue;
@@ -1883,11 +1891,17 @@
18831891
zBrowser = db_get("web-browser", 0);
18841892
if( zBrowser==0 ){
18851893
static const char *const azBrowserProg[] =
18861894
{ "xdg-open", "gnome-open", "firefox", "google-chrome" };
18871895
int i;
1896
+#if defined(__CYGWIN__)
1897
+ const char *path = fossil_getenv("PROGRAMFILES");
1898
+ path = fossil_utf8_to_filename(path);
1899
+ zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
1900
+#else
18881901
zBrowser = "echo";
1902
+#endif
18891903
for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
18901904
if( binaryOnPath(azBrowserProg[i]) ){
18911905
zBrowser = azBrowserProg[i];
18921906
break;
18931907
}
18941908
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114 ** All global variables are in this structure.
115 */
116 struct Global {
117 int argc; char **argv; /* Command-line arguments to the program */
118 char *nameOfExe; /* Full path of executable. */
 
119 int isConst; /* True if the output is unchanging */
120 sqlite3 *db; /* The connection to the databases */
121 sqlite3 *dbConfig; /* Separate connection for global_config table */
122 int useAttach; /* True if global_config is attached to repository */
123 const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
374 unsigned int i, j, k; /* Loop counters */
375 int n; /* Number of bytes in one line */
376 char *z; /* General use string pointer */
377 char **newArgv; /* New expanded g.argv under construction */
378 char const * zFileName; /* input file name */
379 FILE * zInFile; /* input FILE */
380 #if defined(_WIN32)
381 wchar_t buf[MAX_PATH];
382 #endif
383
384 g.argc = argc;
@@ -404,21 +405,21 @@
404 if( fossil_strcmp(z, "args")==0 ) break;
405 }
406 if( i>=g.argc-1 ) return;
407
408 zFileName = g.argv[i+1];
409 zInFile = (0==strcmp("-",zFileName))
410 ? stdin
411 : fossil_fopen(zFileName,"rb");
412 if(!zInFile){
413 fossil_panic("Cannot open -args file [%s]", zFileName);
414 }else{
415 blob_read_from_channel(&file, zInFile, -1);
416 if(stdin != zInFile){
417 fclose(zInFile);
418 }
419 zInFile = NULL;
420 }
421 blob_to_utf8_no_bom(&file, 1);
422 z = blob_str(&file);
423 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
424 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
592 if( g.fSqlTrace ) g.fSqlStats = 1;
593 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
594 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
595 g.zLogin = find_option("user", "U", 1);
596 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
 
597 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
598 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
599 if( zChdir && file_chdir(zChdir, 0) ){
600 fossil_fatal("unable to change directories to %s", zChdir);
601 }
@@ -611,10 +613,11 @@
611 g.argc++;
612 g.argv = zNewArgv;
613 }
614 zCmdName = g.argv[1];
615 }
 
616 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
617 if( rc==1 ){
618 fossil_fatal("%s: unknown command: %s\n"
619 "%s: use \"help\" for more information\n",
620 g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
797 return;
798 }else{
799 fossil_print("Compiled on %s %s using %s (%d-bit)\n",
800 __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
801 fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
 
802 fossil_print("zlib %s\n", ZLIB_VERSION);
803 #if defined(FOSSIL_ENABLE_SSL)
804 fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
805 #endif
806 #if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
1119 fossil_fatal("unable to chroot into %s", zDir);
1120 }
1121 zRepo = "/";
1122 }else{
1123 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1124 if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1125 if( i>0 ){
1126 zDir[i] = 0;
1127 if( file_chdir(zDir, 1) ){
1128 fossil_fatal("unable to chroot into %s", zDir);
1129 }
@@ -1373,11 +1377,11 @@
1373 */
1374 #ifdef FOSSIL_ENABLE_JSON
1375 if(!g.json.isJsonMode){
1376 #endif
1377 dehttpize(g.zExtra);
1378 cgi_set_parameter_nocopy("name", g.zExtra);
1379 #ifdef FOSSIL_ENABLE_JSON
1380 }
1381 #endif
1382 }
1383
@@ -1510,10 +1514,14 @@
1510 if( blob_buffer(&key)[0]=='#' ) continue;
1511 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1512 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1513 blob_reset(&value);
1514 continue;
 
 
 
 
1515 }
1516 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1517 cgi_setenv("HOME", blob_str(&value));
1518 blob_reset(&value);
1519 continue;
@@ -1883,11 +1891,17 @@
1883 zBrowser = db_get("web-browser", 0);
1884 if( zBrowser==0 ){
1885 static const char *const azBrowserProg[] =
1886 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1887 int i;
 
 
 
 
 
1888 zBrowser = "echo";
 
1889 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1890 if( binaryOnPath(azBrowserProg[i]) ){
1891 zBrowser = azBrowserProg[i];
1892 break;
1893 }
1894
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114 ** All global variables are in this structure.
115 */
116 struct Global {
117 int argc; char **argv; /* Command-line arguments to the program */
118 char *nameOfExe; /* Full path of executable. */
119 const char *zErrlog; /* Log errors to this file, if not NULL */
120 int isConst; /* True if the output is unchanging */
121 sqlite3 *db; /* The connection to the databases */
122 sqlite3 *dbConfig; /* Separate connection for global_config table */
123 int useAttach; /* True if global_config is attached to repository */
124 const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
375 unsigned int i, j, k; /* Loop counters */
376 int n; /* Number of bytes in one line */
377 char *z; /* General use string pointer */
378 char **newArgv; /* New expanded g.argv under construction */
379 char const * zFileName; /* input file name */
380 FILE *inFile; /* input FILE */
381 #if defined(_WIN32)
382 wchar_t buf[MAX_PATH];
383 #endif
384
385 g.argc = argc;
@@ -404,21 +405,21 @@
405 if( fossil_strcmp(z, "args")==0 ) break;
406 }
407 if( i>=g.argc-1 ) return;
408
409 zFileName = g.argv[i+1];
410 inFile = (0==strcmp("-",zFileName))
411 ? stdin
412 : fossil_fopen(zFileName,"rb");
413 if(!inFile){
414 fossil_fatal("Cannot open -args file [%s]", zFileName);
415 }else{
416 blob_read_from_channel(&file, inFile, -1);
417 if(stdin != inFile){
418 fclose(inFile);
419 }
420 inFile = NULL;
421 }
422 blob_to_utf8_no_bom(&file, 1);
423 z = blob_str(&file);
424 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
425 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
593 if( g.fSqlTrace ) g.fSqlStats = 1;
594 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
595 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
596 g.zLogin = find_option("user", "U", 1);
597 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
598 g.zErrlog = find_option("errorlog", 0, 1);
599 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
600 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
601 if( zChdir && file_chdir(zChdir, 0) ){
602 fossil_fatal("unable to change directories to %s", zChdir);
603 }
@@ -611,10 +613,11 @@
613 g.argc++;
614 g.argv = zNewArgv;
615 }
616 zCmdName = g.argv[1];
617 }
618 if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
619 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
620 if( rc==1 ){
621 fossil_fatal("%s: unknown command: %s\n"
622 "%s: use \"help\" for more information\n",
623 g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
800 return;
801 }else{
802 fossil_print("Compiled on %s %s using %s (%d-bit)\n",
803 __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
804 fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
805 fossil_print("Schema version %s\n", AUX_SCHEMA);
806 fossil_print("zlib %s\n", ZLIB_VERSION);
807 #if defined(FOSSIL_ENABLE_SSL)
808 fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
809 #endif
810 #if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
1123 fossil_fatal("unable to chroot into %s", zDir);
1124 }
1125 zRepo = "/";
1126 }else{
1127 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1128 if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
1129 if( i>0 ){
1130 zDir[i] = 0;
1131 if( file_chdir(zDir, 1) ){
1132 fossil_fatal("unable to chroot into %s", zDir);
1133 }
@@ -1373,11 +1377,11 @@
1377 */
1378 #ifdef FOSSIL_ENABLE_JSON
1379 if(!g.json.isJsonMode){
1380 #endif
1381 dehttpize(g.zExtra);
1382 cgi_set_parameter_nocopy("name", g.zExtra, 1);
1383 #ifdef FOSSIL_ENABLE_JSON
1384 }
1385 #endif
1386 }
1387
@@ -1510,10 +1514,14 @@
1514 if( blob_buffer(&key)[0]=='#' ) continue;
1515 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1516 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1517 blob_reset(&value);
1518 continue;
1519 }
1520 if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1521 g.zErrlog = mprintf("%s", blob_str(&value));
1522 continue;
1523 }
1524 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1525 cgi_setenv("HOME", blob_str(&value));
1526 blob_reset(&value);
1527 continue;
@@ -1883,11 +1891,17 @@
1891 zBrowser = db_get("web-browser", 0);
1892 if( zBrowser==0 ){
1893 static const char *const azBrowserProg[] =
1894 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1895 int i;
1896 #if defined(__CYGWIN__)
1897 const char *path = fossil_getenv("PROGRAMFILES");
1898 path = fossil_utf8_to_filename(path);
1899 zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
1900 #else
1901 zBrowser = "echo";
1902 #endif
1903 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1904 if( binaryOnPath(azBrowserProg[i]) ){
1905 zBrowser = azBrowserProg[i];
1906 break;
1907 }
1908
+24 -10
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114114
** All global variables are in this structure.
115115
*/
116116
struct Global {
117117
int argc; char **argv; /* Command-line arguments to the program */
118118
char *nameOfExe; /* Full path of executable. */
119
+ const char *zErrlog; /* Log errors to this file, if not NULL */
119120
int isConst; /* True if the output is unchanging */
120121
sqlite3 *db; /* The connection to the databases */
121122
sqlite3 *dbConfig; /* Separate connection for global_config table */
122123
int useAttach; /* True if global_config is attached to repository */
123124
const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
374375
unsigned int i, j, k; /* Loop counters */
375376
int n; /* Number of bytes in one line */
376377
char *z; /* General use string pointer */
377378
char **newArgv; /* New expanded g.argv under construction */
378379
char const * zFileName; /* input file name */
379
- FILE * zInFile; /* input FILE */
380
+ FILE *inFile; /* input FILE */
380381
#if defined(_WIN32)
381382
wchar_t buf[MAX_PATH];
382383
#endif
383384
384385
g.argc = argc;
@@ -404,21 +405,21 @@
404405
if( fossil_strcmp(z, "args")==0 ) break;
405406
}
406407
if( i>=g.argc-1 ) return;
407408
408409
zFileName = g.argv[i+1];
409
- zInFile = (0==strcmp("-",zFileName))
410
+ inFile = (0==strcmp("-",zFileName))
410411
? stdin
411412
: fossil_fopen(zFileName,"rb");
412
- if(!zInFile){
413
- fossil_panic("Cannot open -args file [%s]", zFileName);
413
+ if(!inFile){
414
+ fossil_fatal("Cannot open -args file [%s]", zFileName);
414415
}else{
415
- blob_read_from_channel(&file, zInFile, -1);
416
- if(stdin != zInFile){
417
- fclose(zInFile);
416
+ blob_read_from_channel(&file, inFile, -1);
417
+ if(stdin != inFile){
418
+ fclose(inFile);
418419
}
419
- zInFile = NULL;
420
+ inFile = NULL;
420421
}
421422
blob_to_utf8_no_bom(&file, 1);
422423
z = blob_str(&file);
423424
for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
424425
newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
592593
if( g.fSqlTrace ) g.fSqlStats = 1;
593594
g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
594595
g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
595596
g.zLogin = find_option("user", "U", 1);
596597
g.zSSLIdentity = find_option("ssl-identity", 0, 1);
598
+ g.zErrlog = find_option("errorlog", 0, 1);
597599
if( find_option("utc",0,0) ) g.fTimeFormat = 1;
598600
if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
599601
if( zChdir && file_chdir(zChdir, 0) ){
600602
fossil_fatal("unable to change directories to %s", zChdir);
601603
}
@@ -611,10 +613,11 @@
611613
g.argc++;
612614
g.argv = zNewArgv;
613615
}
614616
zCmdName = g.argv[1];
615617
}
618
+ if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
616619
rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
617620
if( rc==1 ){
618621
fossil_fatal("%s: unknown command: %s\n"
619622
"%s: use \"help\" for more information\n",
620623
g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
797800
return;
798801
}else{
799802
fossil_print("Compiled on %s %s using %s (%d-bit)\n",
800803
__DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
801804
fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
805
+ fossil_print("Schema version %s\n", AUX_SCHEMA);
802806
fossil_print("zlib %s\n", ZLIB_VERSION);
803807
#if defined(FOSSIL_ENABLE_SSL)
804808
fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
805809
#endif
806810
#if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
11191123
fossil_fatal("unable to chroot into %s", zDir);
11201124
}
11211125
zRepo = "/";
11221126
}else{
11231127
for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1124
- if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1128
+ if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
11251129
if( i>0 ){
11261130
zDir[i] = 0;
11271131
if( file_chdir(zDir, 1) ){
11281132
fossil_fatal("unable to chroot into %s", zDir);
11291133
}
@@ -1373,11 +1377,11 @@
13731377
*/
13741378
#ifdef FOSSIL_ENABLE_JSON
13751379
if(!g.json.isJsonMode){
13761380
#endif
13771381
dehttpize(g.zExtra);
1378
- cgi_set_parameter_nocopy("name", g.zExtra);
1382
+ cgi_set_parameter_nocopy("name", g.zExtra, 1);
13791383
#ifdef FOSSIL_ENABLE_JSON
13801384
}
13811385
#endif
13821386
}
13831387
@@ -1510,10 +1514,14 @@
15101514
if( blob_buffer(&key)[0]=='#' ) continue;
15111515
if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
15121516
g.fDebug = fossil_fopen(blob_str(&value), "ab");
15131517
blob_reset(&value);
15141518
continue;
1519
+ }
1520
+ if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1521
+ g.zErrlog = mprintf("%s", blob_str(&value));
1522
+ continue;
15151523
}
15161524
if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
15171525
cgi_setenv("HOME", blob_str(&value));
15181526
blob_reset(&value);
15191527
continue;
@@ -1883,11 +1891,17 @@
18831891
zBrowser = db_get("web-browser", 0);
18841892
if( zBrowser==0 ){
18851893
static const char *const azBrowserProg[] =
18861894
{ "xdg-open", "gnome-open", "firefox", "google-chrome" };
18871895
int i;
1896
+#if defined(__CYGWIN__)
1897
+ const char *path = fossil_getenv("PROGRAMFILES");
1898
+ path = fossil_utf8_to_filename(path);
1899
+ zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
1900
+#else
18881901
zBrowser = "echo";
1902
+#endif
18891903
for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
18901904
if( binaryOnPath(azBrowserProg[i]) ){
18911905
zBrowser = azBrowserProg[i];
18921906
break;
18931907
}
18941908
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114 ** All global variables are in this structure.
115 */
116 struct Global {
117 int argc; char **argv; /* Command-line arguments to the program */
118 char *nameOfExe; /* Full path of executable. */
 
119 int isConst; /* True if the output is unchanging */
120 sqlite3 *db; /* The connection to the databases */
121 sqlite3 *dbConfig; /* Separate connection for global_config table */
122 int useAttach; /* True if global_config is attached to repository */
123 const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
374 unsigned int i, j, k; /* Loop counters */
375 int n; /* Number of bytes in one line */
376 char *z; /* General use string pointer */
377 char **newArgv; /* New expanded g.argv under construction */
378 char const * zFileName; /* input file name */
379 FILE * zInFile; /* input FILE */
380 #if defined(_WIN32)
381 wchar_t buf[MAX_PATH];
382 #endif
383
384 g.argc = argc;
@@ -404,21 +405,21 @@
404 if( fossil_strcmp(z, "args")==0 ) break;
405 }
406 if( i>=g.argc-1 ) return;
407
408 zFileName = g.argv[i+1];
409 zInFile = (0==strcmp("-",zFileName))
410 ? stdin
411 : fossil_fopen(zFileName,"rb");
412 if(!zInFile){
413 fossil_panic("Cannot open -args file [%s]", zFileName);
414 }else{
415 blob_read_from_channel(&file, zInFile, -1);
416 if(stdin != zInFile){
417 fclose(zInFile);
418 }
419 zInFile = NULL;
420 }
421 blob_to_utf8_no_bom(&file, 1);
422 z = blob_str(&file);
423 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
424 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
592 if( g.fSqlTrace ) g.fSqlStats = 1;
593 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
594 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
595 g.zLogin = find_option("user", "U", 1);
596 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
 
597 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
598 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
599 if( zChdir && file_chdir(zChdir, 0) ){
600 fossil_fatal("unable to change directories to %s", zChdir);
601 }
@@ -611,10 +613,11 @@
611 g.argc++;
612 g.argv = zNewArgv;
613 }
614 zCmdName = g.argv[1];
615 }
 
616 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
617 if( rc==1 ){
618 fossil_fatal("%s: unknown command: %s\n"
619 "%s: use \"help\" for more information\n",
620 g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
797 return;
798 }else{
799 fossil_print("Compiled on %s %s using %s (%d-bit)\n",
800 __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
801 fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
 
802 fossil_print("zlib %s\n", ZLIB_VERSION);
803 #if defined(FOSSIL_ENABLE_SSL)
804 fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
805 #endif
806 #if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
1119 fossil_fatal("unable to chroot into %s", zDir);
1120 }
1121 zRepo = "/";
1122 }else{
1123 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1124 if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1125 if( i>0 ){
1126 zDir[i] = 0;
1127 if( file_chdir(zDir, 1) ){
1128 fossil_fatal("unable to chroot into %s", zDir);
1129 }
@@ -1373,11 +1377,11 @@
1373 */
1374 #ifdef FOSSIL_ENABLE_JSON
1375 if(!g.json.isJsonMode){
1376 #endif
1377 dehttpize(g.zExtra);
1378 cgi_set_parameter_nocopy("name", g.zExtra);
1379 #ifdef FOSSIL_ENABLE_JSON
1380 }
1381 #endif
1382 }
1383
@@ -1510,10 +1514,14 @@
1510 if( blob_buffer(&key)[0]=='#' ) continue;
1511 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1512 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1513 blob_reset(&value);
1514 continue;
 
 
 
 
1515 }
1516 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1517 cgi_setenv("HOME", blob_str(&value));
1518 blob_reset(&value);
1519 continue;
@@ -1883,11 +1891,17 @@
1883 zBrowser = db_get("web-browser", 0);
1884 if( zBrowser==0 ){
1885 static const char *const azBrowserProg[] =
1886 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1887 int i;
 
 
 
 
 
1888 zBrowser = "echo";
 
1889 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1890 if( binaryOnPath(azBrowserProg[i]) ){
1891 zBrowser = azBrowserProg[i];
1892 break;
1893 }
1894
--- src/main.c
+++ src/main.c
@@ -114,10 +114,11 @@
114 ** All global variables are in this structure.
115 */
116 struct Global {
117 int argc; char **argv; /* Command-line arguments to the program */
118 char *nameOfExe; /* Full path of executable. */
119 const char *zErrlog; /* Log errors to this file, if not NULL */
120 int isConst; /* True if the output is unchanging */
121 sqlite3 *db; /* The connection to the databases */
122 sqlite3 *dbConfig; /* Separate connection for global_config table */
123 int useAttach; /* True if global_config is attached to repository */
124 const char *zConfigDbName;/* Path of the config database. NULL if not open */
@@ -374,11 +375,11 @@
375 unsigned int i, j, k; /* Loop counters */
376 int n; /* Number of bytes in one line */
377 char *z; /* General use string pointer */
378 char **newArgv; /* New expanded g.argv under construction */
379 char const * zFileName; /* input file name */
380 FILE *inFile; /* input FILE */
381 #if defined(_WIN32)
382 wchar_t buf[MAX_PATH];
383 #endif
384
385 g.argc = argc;
@@ -404,21 +405,21 @@
405 if( fossil_strcmp(z, "args")==0 ) break;
406 }
407 if( i>=g.argc-1 ) return;
408
409 zFileName = g.argv[i+1];
410 inFile = (0==strcmp("-",zFileName))
411 ? stdin
412 : fossil_fopen(zFileName,"rb");
413 if(!inFile){
414 fossil_fatal("Cannot open -args file [%s]", zFileName);
415 }else{
416 blob_read_from_channel(&file, inFile, -1);
417 if(stdin != inFile){
418 fclose(inFile);
419 }
420 inFile = NULL;
421 }
422 blob_to_utf8_no_bom(&file, 1);
423 z = blob_str(&file);
424 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
425 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -592,10 +593,11 @@
593 if( g.fSqlTrace ) g.fSqlStats = 1;
594 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
595 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
596 g.zLogin = find_option("user", "U", 1);
597 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
598 g.zErrlog = find_option("errorlog", 0, 1);
599 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
600 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
601 if( zChdir && file_chdir(zChdir, 0) ){
602 fossil_fatal("unable to change directories to %s", zChdir);
603 }
@@ -611,10 +613,11 @@
613 g.argc++;
614 g.argv = zNewArgv;
615 }
616 zCmdName = g.argv[1];
617 }
618 if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
619 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
620 if( rc==1 ){
621 fossil_fatal("%s: unknown command: %s\n"
622 "%s: use \"help\" for more information\n",
623 g.argv[0], zCmdName, g.argv[0]);
@@ -797,10 +800,11 @@
800 return;
801 }else{
802 fossil_print("Compiled on %s %s using %s (%d-bit)\n",
803 __DATE__, __TIME__, COMPILER_NAME, sizeof(void*)*8);
804 fossil_print("SQLite %s %.30s\n", SQLITE_VERSION, SQLITE_SOURCE_ID);
805 fossil_print("Schema version %s\n", AUX_SCHEMA);
806 fossil_print("zlib %s\n", ZLIB_VERSION);
807 #if defined(FOSSIL_ENABLE_SSL)
808 fossil_print("SSL (%s)\n", OPENSSL_VERSION_TEXT);
809 #endif
810 #if defined(FOSSIL_ENABLE_TCL)
@@ -1119,11 +1123,11 @@
1123 fossil_fatal("unable to chroot into %s", zDir);
1124 }
1125 zRepo = "/";
1126 }else{
1127 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1128 if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
1129 if( i>0 ){
1130 zDir[i] = 0;
1131 if( file_chdir(zDir, 1) ){
1132 fossil_fatal("unable to chroot into %s", zDir);
1133 }
@@ -1373,11 +1377,11 @@
1377 */
1378 #ifdef FOSSIL_ENABLE_JSON
1379 if(!g.json.isJsonMode){
1380 #endif
1381 dehttpize(g.zExtra);
1382 cgi_set_parameter_nocopy("name", g.zExtra, 1);
1383 #ifdef FOSSIL_ENABLE_JSON
1384 }
1385 #endif
1386 }
1387
@@ -1510,10 +1514,14 @@
1514 if( blob_buffer(&key)[0]=='#' ) continue;
1515 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1516 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1517 blob_reset(&value);
1518 continue;
1519 }
1520 if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1521 g.zErrlog = mprintf("%s", blob_str(&value));
1522 continue;
1523 }
1524 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1525 cgi_setenv("HOME", blob_str(&value));
1526 blob_reset(&value);
1527 continue;
@@ -1883,11 +1891,17 @@
1891 zBrowser = db_get("web-browser", 0);
1892 if( zBrowser==0 ){
1893 static const char *const azBrowserProg[] =
1894 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1895 int i;
1896 #if defined(__CYGWIN__)
1897 const char *path = fossil_getenv("PROGRAMFILES");
1898 path = fossil_utf8_to_filename(path);
1899 zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
1900 #else
1901 zBrowser = "echo";
1902 #endif
1903 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1904 if( binaryOnPath(azBrowserProg[i]) ){
1905 zBrowser = azBrowserProg[i];
1906 break;
1907 }
1908
+95 -79
--- src/manifest.c
+++ src/manifest.c
@@ -92,11 +92,11 @@
9292
int nCChild; /* Number of cluster children */
9393
int nCChildAlloc; /* Number of closts allocated in azCChild[] */
9494
char **azCChild; /* UUIDs of referenced objects in a cluster. M cards */
9595
int nTag; /* Number of T Cards */
9696
int nTagAlloc; /* Slots allocated in aTag[] */
97
- struct {
97
+ struct TagType {
9898
char *zName; /* Name of the tag */
9999
char *zUuid; /* UUID that the tag is applied to */
100100
char *zValue; /* Value if the tag is really a property */
101101
} *aTag; /* One for each T card */
102102
int nField; /* Number of J cards */
@@ -353,11 +353,11 @@
353353
char cType;
354354
char *z;
355355
int n;
356356
char *zUuid;
357357
int sz = 0;
358
- int isRepeat;
358
+ int isRepeat, hasSelfRefTag = 0;
359359
static Bag seen;
360360
const char *zErr = 0;
361361
362362
if( rid==0 ){
363363
isRepeat = 1;
@@ -378,27 +378,28 @@
378378
blob_reset(pContent);
379379
blob_appendf(pErr, n ? "not terminated with \\n" : "zero-length");
380380
return 0;
381381
}
382382
383
- /* Strip off the PGP signature if there is one. Then verify the
384
- ** Z-card.
383
+ /* Strip off the PGP signature if there is one.
385384
*/
386385
remove_pgp_signature(&z, &n);
387
- if( verify_z_card(z, n)==2 ){
388
- blob_reset(pContent);
389
- blob_appendf(pErr, "incorrect Z-card cksum");
390
- return 0;
391
- }
392386
393387
/* Verify that the first few characters of the artifact look like
394388
** a control artifact.
395389
*/
396390
if( n<10 || z[0]<'A' || z[0]>'Z' || z[1]!=' ' ){
397391
blob_reset(pContent);
398392
blob_appendf(pErr, "line 1 not recognized");
399393
return 0;
394
+ }
395
+ /* Then verify the Z-card.
396
+ */
397
+ if( verify_z_card(z, n)==2 ){
398
+ blob_reset(pContent);
399
+ blob_appendf(pErr, "incorrect Z-card cksum");
400
+ return 0;
400401
}
401402
402403
/* Allocate a Manifest object to hold the parsed control artifact.
403404
*/
404405
p = fossil_malloc( sizeof(*p) );
@@ -716,11 +717,11 @@
716717
**
717718
** Specify the MD5 checksum over the name and content of all files
718719
** in the manifest.
719720
*/
720721
case 'R': {
721
- if( p->zRepoCksum!=0 ) SYNTAX("more than on R-card");
722
+ if( p->zRepoCksum!=0 ) SYNTAX("more than one R-card");
722723
p->zRepoCksum = next_token(&x, &sz);
723724
if( sz!=32 ) SYNTAX("wrong size cksum on R-card");
724725
if( !validate16(p->zRepoCksum, 32) ) SYNTAX("malformed R-card cksum");
725726
break;
726727
}
@@ -748,12 +749,17 @@
748749
if( zUuid==0 ) SYNTAX("missing UUID on T-card");
749750
zValue = next_token(&x, 0);
750751
if( zValue ) defossilize(zValue);
751752
if( sz==UUID_SIZE && validate16(zUuid, UUID_SIZE) ){
752753
/* A valid uuid */
754
+ if( p->zEventId ) SYNTAX("non-self-referential T-card in event");
753755
}else if( sz==1 && zUuid[0]=='*' ){
754756
zUuid = 0;
757
+ hasSelfRefTag = 1;
758
+ if( p->zEventId && zName[0]!='+' ){
759
+ SYNTAX("propagating T-card in event");
760
+ }
755761
}else{
756762
SYNTAX("malformed UUID on T-card");
757763
}
758764
defossilize(zName);
759765
if( zName[0]!='-' && zName[0]!='+' && zName[0]!='*' ){
@@ -769,12 +775,15 @@
769775
}
770776
i = p->nTag++;
771777
p->aTag[i].zName = zName;
772778
p->aTag[i].zUuid = zUuid;
773779
p->aTag[i].zValue = zValue;
774
- if( i>0 && fossil_strcmp(p->aTag[i-1].zName, zName)>=0 ){
775
- SYNTAX("T-card in the wrong order");
780
+ if( i>0 ){
781
+ int c = fossil_strcmp(p->aTag[i-1].zName, zName);
782
+ if( c>0 || (c==0 && fossil_strcmp(p->aTag[i-1].zUuid, zUuid)>=0) ){
783
+ SYNTAX("T-card in the wrong order");
784
+ }
776785
}
777786
break;
778787
}
779788
780789
/*
@@ -849,97 +858,84 @@
849858
}
850859
}
851860
}
852861
if( x.z<x.zEnd ) SYNTAX("extra characters at end of card");
853862
854
- if( p->nFile>0 || p->zRepoCksum!=0 || p->zBaseline ){
855
- if( p->nCChild>0 ) SYNTAX("M-card in check-in");
856
- if( p->rDate<=0.0 ) SYNTAX("missing date for check-in");
857
- if( p->nField>0 ) SYNTAX("J-card in check-in");
858
- if( p->zTicketUuid ) SYNTAX("K-card in check-in");
859
- if( p->zWiki ) SYNTAX("W-card in check-in");
860
- if( p->zWikiTitle ) SYNTAX("L-card in check-in");
861
- if( p->zEventId ) SYNTAX("E-card in check-in");
862
- if( p->zTicketUuid ) SYNTAX("K-card in check-in");
863
- if( p->zAttachName ) SYNTAX("A-card in check-in");
864
- p->type = CFTYPE_MANIFEST;
865
- }else if( p->nCChild>0 ){
866
- if( p->rDate>0.0
867
- || p->zComment!=0
868
- || p->zUser!=0
869
- || p->nTag>0
870
- || p->nParent>0
863
+ if( p->nCChild>0 ){
864
+ if( p->zAttachName
865
+ || p->zBaseline
866
+ || p->zComment
867
+ || p->rDate>0.0
868
+ || p->zEventId
869
+ || p->nFile>0
871870
|| p->nField>0
872871
|| p->zTicketUuid
873
- || p->zWiki
874872
|| p->zWikiTitle
875
- || p->zEventId
876
- || p->zAttachName
877873
|| p->zMimetype
874
+ || p->nParent>0
875
+ || p->nCherrypick>0
876
+ || p->zRepoCksum
877
+ || p->nTag>0
878
+ || p->zUser
879
+ || p->zWiki
878880
){
879881
SYNTAX("cluster contains a card other than M- or Z-");
880882
}
881883
if( !seenZ ) SYNTAX("missing Z-card on cluster");
882884
p->type = CFTYPE_CLUSTER;
883
- }else if( p->nField>0 ){
884
- if( p->rDate<=0.0 ) SYNTAX("missing date for ticket");
885
+ }else if( p->zEventId ){
886
+ if( p->rDate<=0.0 ) SYNTAX("missing date on event");
887
+ if( p->nFile>0 ) SYNTAX("F-card in event");
888
+ if( p->zRepoCksum ) SYNTAX("R-card in event");
889
+ if( p->zBaseline ) SYNTAX("B-card in event");
890
+ if( p->nField>0 ) SYNTAX("J-card in event");
891
+ if( p->zTicketUuid ) SYNTAX("K-card in event");
892
+ if( p->zWikiTitle!=0 ) SYNTAX("L-card in event");
893
+ if( p->zWiki==0 ) SYNTAX("missing W-card on event");
894
+ if( p->zAttachName ) SYNTAX("A-card in event");
895
+ if( !seenZ ) SYNTAX("missing Z-card on event");
896
+ p->type = CFTYPE_EVENT;
897
+ }else if( hasSelfRefTag || p->nFile>0 || p->zRepoCksum!=0 || p->zBaseline ){
898
+ if( p->rDate<=0.0 ) SYNTAX("missing date on manifest");
899
+ if( p->nField>0 ) SYNTAX("J-card in manifest");
900
+ if( p->zTicketUuid ) SYNTAX("K-card in manifest");
901
+ if( p->zWiki ) SYNTAX("W-card in manifest");
902
+ if( p->zWikiTitle ) SYNTAX("L-card in manifest");
903
+ if( p->zTicketUuid ) SYNTAX("K-card in manifest");
904
+ if( p->zAttachName ) SYNTAX("A-card in manifest");
905
+ p->type = CFTYPE_MANIFEST;
906
+ }else if( p->nField>0 || p->zTicketUuid!=0 ){
907
+ if( p->rDate<=0.0 ) SYNTAX("missing date on ticket");
885908
if( p->zWiki ) SYNTAX("W-card in ticket");
886909
if( p->zWikiTitle ) SYNTAX("L-card in ticket");
887
- if( p->zEventId ) SYNTAX("E-card in ticket");
888
- if( p->nCChild>0 ) SYNTAX("M-card in ticket");
910
+ if( p->nField==0 ) SYNTAX("missing J-card on ticket");
889911
if( p->nTag>0 ) SYNTAX("T-card in ticket");
890
- if( p->zTicketUuid==0 ) SYNTAX("missing K-card in ticket");
891
- if( p->zUser==0 ) SYNTAX("missing U-card in ticket");
912
+ if( p->zTicketUuid==0 ) SYNTAX("missing K-card on ticket");
913
+ if( p->zUser==0 ) SYNTAX("missing U-card on ticket");
892914
if( p->zAttachName ) SYNTAX("A-card in ticket");
893915
if( p->zMimetype) SYNTAX("N-card in ticket");
894
- if( !seenZ ) SYNTAX("missing Z-card in ticket");
916
+ if( !seenZ ) SYNTAX("missing Z-card on ticket");
895917
p->type = CFTYPE_TICKET;
896
- }else if( p->zEventId ){
897
- if( p->rDate<=0.0 ) SYNTAX("missing date for event");
898
- if( p->nCChild>0 ) SYNTAX("M-card in event");
899
- if( p->zTicketUuid!=0 ) SYNTAX("K-card in event");
900
- if( p->zWikiTitle!=0 ) SYNTAX("L-card in event");
901
- if( p->zWiki==0 ) SYNTAX("W-card in event");
902
- if( p->zAttachName ) SYNTAX("A-card in event");
903
- for(i=0; i<p->nTag; i++){
904
- if( p->aTag[i].zName[0]!='+' ) SYNTAX("propagating tag in event");
905
- if( p->aTag[i].zUuid!=0 ) SYNTAX("non-self-referential tag in event");
906
- }
907
- if( !seenZ ) SYNTAX("Z-card missing in event");
908
- p->type = CFTYPE_EVENT;
909
- }else if( p->zWiki!=0 ){
910
- if( p->rDate<=0.0 ) SYNTAX("date missing on wiki");
911
- if( p->nCChild>0 ) SYNTAX("M-card in wiki");
918
+ }else if( p->zWiki!=0 || p->zWikiTitle!=0 ){
919
+ if( p->rDate<=0.0 ) SYNTAX("missing date on wiki");
912920
if( p->nTag>0 ) SYNTAX("T-card in wiki");
913
- if( p->zTicketUuid!=0 ) SYNTAX("K-card in wiki");
914
- if( p->zWikiTitle==0 ) SYNTAX("L-card in wiki");
921
+ if( p->zWiki==0 ) SYNTAX("missing W-card on wiki");
922
+ if( p->zWikiTitle==0 ) SYNTAX("missing L-card on wiki");
915923
if( p->zAttachName ) SYNTAX("A-card in wiki");
916924
if( !seenZ ) SYNTAX("missing Z-card on wiki");
917925
p->type = CFTYPE_WIKI;
918
- }else if( p->nTag>0 ){
919
- if( p->rDate<=0.0 ) SYNTAX("date missing on tag");
920
- if( p->nParent>0 ) SYNTAX("P-card on tag");
921
- if( p->zWikiTitle ) SYNTAX("L-card on tag");
922
- if( p->zTicketUuid ) SYNTAX("K-card in tag");
923
- if( p->zAttachName ) SYNTAX("A-card in tag");
924
- if( p->zMimetype ) SYNTAX("N-card in tag");
925
- if( !seenZ ) SYNTAX("missing Z-card on tag");
926
- p->type = CFTYPE_CONTROL;
927926
}else if( p->zAttachName ){
928
- if( p->nCChild>0 ) SYNTAX("M-card in attachment");
929
- if( p->rDate<=0.0 ) SYNTAX("missing date in attachment");
930
- if( p->zTicketUuid ) SYNTAX("K-card in attachment");
931
- if( p->zWikiTitle ) SYNTAX("L-card in attachment");
927
+ if( p->rDate<=0.0 ) SYNTAX("missing date on attachment");
928
+ if( p->nTag>0 ) SYNTAX("T-card in attachment");
932929
if( !seenZ ) SYNTAX("missing Z-card on attachment");
933930
p->type = CFTYPE_ATTACHMENT;
934931
}else{
935
- if( p->nCChild>0 ) SYNTAX("M-card in check-in");
936
- if( p->rDate<=0.0 ) SYNTAX("missing date in check-in");
937
- if( p->nField>0 ) SYNTAX("J-card in check-in");
938
- if( p->zTicketUuid ) SYNTAX("K-card in check-in");
939
- if( p->zWikiTitle ) SYNTAX("L-card in check-in");
940
- p->type = CFTYPE_MANIFEST;
932
+ if( p->rDate<=0.0 ) SYNTAX("missing date on control");
933
+ if( p->nParent>0 ) SYNTAX("P-card in control");
934
+ if( p->zMimetype ) SYNTAX("N-card in control");
935
+ if( !seenZ ) SYNTAX("missing Z-card on control");
936
+ p->type = CFTYPE_CONTROL;
941937
}
942938
md5sum_init();
943939
if( !isRepeat ) g.parseCnt[p->type]++;
944940
return p;
945941
@@ -1610,10 +1606,24 @@
16101606
blob_str(&comment), blob_str(&brief)
16111607
);
16121608
blob_reset(&comment);
16131609
blob_reset(&brief);
16141610
}
1611
+
1612
+/*
1613
+** This is the comparison function used to sort the tag array.
1614
+*/
1615
+static int tag_compare(const void *a, const void *b){
1616
+ struct TagType *pA = (struct TagType*)a;
1617
+ struct TagType *pB = (struct TagType*)b;
1618
+ int c;
1619
+ c = fossil_strcmp(pA->zUuid, pB->zUuid);
1620
+ if( c==0 ){
1621
+ c = fossil_strcmp(pA->zName, pB->zName);
1622
+ }
1623
+ return c;
1624
+}
16151625
16161626
/*
16171627
** Scan artifact rid/pContent to see if it is a control artifact of
16181628
** any key:
16191629
**
@@ -1924,16 +1934,21 @@
19241934
const char *zName;
19251935
const char *zValue;
19261936
const char *zUuid;
19271937
int branchMove = 0;
19281938
blob_zero(&comment);
1939
+ if( p->zComment ){
1940
+ blob_appendf(&comment, " %s.", p->zComment);
1941
+ }
1942
+ /* Next loop expects tags to be sorted on UUID, so sort it. */
1943
+ qsort(p->aTag, p->nTag, sizeof(p->aTag[0]), tag_compare);
19291944
for(i=0; i<p->nTag; i++){
19301945
zUuid = p->aTag[i].zUuid;
1946
+ if( !zUuid ) continue;
19311947
if( i==0 || fossil_strcmp(zUuid, p->aTag[i-1].zUuid)!=0 ){
1932
- if( i>0 ) blob_append(&comment, " ", 1);
19331948
blob_appendf(&comment,
1934
- "Edit [%S]:",
1949
+ " Edit [%S]:",
19351950
zUuid);
19361951
branchMove = 0;
19371952
}
19381953
zName = p->aTag[i].zName;
19391954
zValue = p->aTag[i].zValue;
@@ -1992,14 +2007,15 @@
19922007
blob_appendf(&comment, ".");
19932008
}
19942009
}
19952010
}
19962011
/*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
2012
+ if( blob_size(&comment)==0 ) blob_append(&comment, " ", 1);
19972013
db_multi_exec(
19982014
"REPLACE INTO event(type,mtime,objid,user,comment)"
19992015
"VALUES('g',%.17g,%d,%Q,%Q)",
2000
- p->rDate, rid, p->zUser, blob_str(&comment)
2016
+ p->rDate, rid, p->zUser, blob_str(&comment)+1
20012017
);
20022018
blob_reset(&comment);
20032019
}
20042020
db_end_transaction(0);
20052021
if( p->type==CFTYPE_MANIFEST ){
20062022
--- src/manifest.c
+++ src/manifest.c
@@ -92,11 +92,11 @@
92 int nCChild; /* Number of cluster children */
93 int nCChildAlloc; /* Number of closts allocated in azCChild[] */
94 char **azCChild; /* UUIDs of referenced objects in a cluster. M cards */
95 int nTag; /* Number of T Cards */
96 int nTagAlloc; /* Slots allocated in aTag[] */
97 struct {
98 char *zName; /* Name of the tag */
99 char *zUuid; /* UUID that the tag is applied to */
100 char *zValue; /* Value if the tag is really a property */
101 } *aTag; /* One for each T card */
102 int nField; /* Number of J cards */
@@ -353,11 +353,11 @@
353 char cType;
354 char *z;
355 int n;
356 char *zUuid;
357 int sz = 0;
358 int isRepeat;
359 static Bag seen;
360 const char *zErr = 0;
361
362 if( rid==0 ){
363 isRepeat = 1;
@@ -378,27 +378,28 @@
378 blob_reset(pContent);
379 blob_appendf(pErr, n ? "not terminated with \\n" : "zero-length");
380 return 0;
381 }
382
383 /* Strip off the PGP signature if there is one. Then verify the
384 ** Z-card.
385 */
386 remove_pgp_signature(&z, &n);
387 if( verify_z_card(z, n)==2 ){
388 blob_reset(pContent);
389 blob_appendf(pErr, "incorrect Z-card cksum");
390 return 0;
391 }
392
393 /* Verify that the first few characters of the artifact look like
394 ** a control artifact.
395 */
396 if( n<10 || z[0]<'A' || z[0]>'Z' || z[1]!=' ' ){
397 blob_reset(pContent);
398 blob_appendf(pErr, "line 1 not recognized");
399 return 0;
 
 
 
 
 
 
 
400 }
401
402 /* Allocate a Manifest object to hold the parsed control artifact.
403 */
404 p = fossil_malloc( sizeof(*p) );
@@ -716,11 +717,11 @@
716 **
717 ** Specify the MD5 checksum over the name and content of all files
718 ** in the manifest.
719 */
720 case 'R': {
721 if( p->zRepoCksum!=0 ) SYNTAX("more than on R-card");
722 p->zRepoCksum = next_token(&x, &sz);
723 if( sz!=32 ) SYNTAX("wrong size cksum on R-card");
724 if( !validate16(p->zRepoCksum, 32) ) SYNTAX("malformed R-card cksum");
725 break;
726 }
@@ -748,12 +749,17 @@
748 if( zUuid==0 ) SYNTAX("missing UUID on T-card");
749 zValue = next_token(&x, 0);
750 if( zValue ) defossilize(zValue);
751 if( sz==UUID_SIZE && validate16(zUuid, UUID_SIZE) ){
752 /* A valid uuid */
 
753 }else if( sz==1 && zUuid[0]=='*' ){
754 zUuid = 0;
 
 
 
 
755 }else{
756 SYNTAX("malformed UUID on T-card");
757 }
758 defossilize(zName);
759 if( zName[0]!='-' && zName[0]!='+' && zName[0]!='*' ){
@@ -769,12 +775,15 @@
769 }
770 i = p->nTag++;
771 p->aTag[i].zName = zName;
772 p->aTag[i].zUuid = zUuid;
773 p->aTag[i].zValue = zValue;
774 if( i>0 && fossil_strcmp(p->aTag[i-1].zName, zName)>=0 ){
775 SYNTAX("T-card in the wrong order");
 
 
 
776 }
777 break;
778 }
779
780 /*
@@ -849,97 +858,84 @@
849 }
850 }
851 }
852 if( x.z<x.zEnd ) SYNTAX("extra characters at end of card");
853
854 if( p->nFile>0 || p->zRepoCksum!=0 || p->zBaseline ){
855 if( p->nCChild>0 ) SYNTAX("M-card in check-in");
856 if( p->rDate<=0.0 ) SYNTAX("missing date for check-in");
857 if( p->nField>0 ) SYNTAX("J-card in check-in");
858 if( p->zTicketUuid ) SYNTAX("K-card in check-in");
859 if( p->zWiki ) SYNTAX("W-card in check-in");
860 if( p->zWikiTitle ) SYNTAX("L-card in check-in");
861 if( p->zEventId ) SYNTAX("E-card in check-in");
862 if( p->zTicketUuid ) SYNTAX("K-card in check-in");
863 if( p->zAttachName ) SYNTAX("A-card in check-in");
864 p->type = CFTYPE_MANIFEST;
865 }else if( p->nCChild>0 ){
866 if( p->rDate>0.0
867 || p->zComment!=0
868 || p->zUser!=0
869 || p->nTag>0
870 || p->nParent>0
871 || p->nField>0
872 || p->zTicketUuid
873 || p->zWiki
874 || p->zWikiTitle
875 || p->zEventId
876 || p->zAttachName
877 || p->zMimetype
 
 
 
 
 
 
878 ){
879 SYNTAX("cluster contains a card other than M- or Z-");
880 }
881 if( !seenZ ) SYNTAX("missing Z-card on cluster");
882 p->type = CFTYPE_CLUSTER;
883 }else if( p->nField>0 ){
884 if( p->rDate<=0.0 ) SYNTAX("missing date for ticket");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885 if( p->zWiki ) SYNTAX("W-card in ticket");
886 if( p->zWikiTitle ) SYNTAX("L-card in ticket");
887 if( p->zEventId ) SYNTAX("E-card in ticket");
888 if( p->nCChild>0 ) SYNTAX("M-card in ticket");
889 if( p->nTag>0 ) SYNTAX("T-card in ticket");
890 if( p->zTicketUuid==0 ) SYNTAX("missing K-card in ticket");
891 if( p->zUser==0 ) SYNTAX("missing U-card in ticket");
892 if( p->zAttachName ) SYNTAX("A-card in ticket");
893 if( p->zMimetype) SYNTAX("N-card in ticket");
894 if( !seenZ ) SYNTAX("missing Z-card in ticket");
895 p->type = CFTYPE_TICKET;
896 }else if( p->zEventId ){
897 if( p->rDate<=0.0 ) SYNTAX("missing date for event");
898 if( p->nCChild>0 ) SYNTAX("M-card in event");
899 if( p->zTicketUuid!=0 ) SYNTAX("K-card in event");
900 if( p->zWikiTitle!=0 ) SYNTAX("L-card in event");
901 if( p->zWiki==0 ) SYNTAX("W-card in event");
902 if( p->zAttachName ) SYNTAX("A-card in event");
903 for(i=0; i<p->nTag; i++){
904 if( p->aTag[i].zName[0]!='+' ) SYNTAX("propagating tag in event");
905 if( p->aTag[i].zUuid!=0 ) SYNTAX("non-self-referential tag in event");
906 }
907 if( !seenZ ) SYNTAX("Z-card missing in event");
908 p->type = CFTYPE_EVENT;
909 }else if( p->zWiki!=0 ){
910 if( p->rDate<=0.0 ) SYNTAX("date missing on wiki");
911 if( p->nCChild>0 ) SYNTAX("M-card in wiki");
912 if( p->nTag>0 ) SYNTAX("T-card in wiki");
913 if( p->zTicketUuid!=0 ) SYNTAX("K-card in wiki");
914 if( p->zWikiTitle==0 ) SYNTAX("L-card in wiki");
915 if( p->zAttachName ) SYNTAX("A-card in wiki");
916 if( !seenZ ) SYNTAX("missing Z-card on wiki");
917 p->type = CFTYPE_WIKI;
918 }else if( p->nTag>0 ){
919 if( p->rDate<=0.0 ) SYNTAX("date missing on tag");
920 if( p->nParent>0 ) SYNTAX("P-card on tag");
921 if( p->zWikiTitle ) SYNTAX("L-card on tag");
922 if( p->zTicketUuid ) SYNTAX("K-card in tag");
923 if( p->zAttachName ) SYNTAX("A-card in tag");
924 if( p->zMimetype ) SYNTAX("N-card in tag");
925 if( !seenZ ) SYNTAX("missing Z-card on tag");
926 p->type = CFTYPE_CONTROL;
927 }else if( p->zAttachName ){
928 if( p->nCChild>0 ) SYNTAX("M-card in attachment");
929 if( p->rDate<=0.0 ) SYNTAX("missing date in attachment");
930 if( p->zTicketUuid ) SYNTAX("K-card in attachment");
931 if( p->zWikiTitle ) SYNTAX("L-card in attachment");
932 if( !seenZ ) SYNTAX("missing Z-card on attachment");
933 p->type = CFTYPE_ATTACHMENT;
934 }else{
935 if( p->nCChild>0 ) SYNTAX("M-card in check-in");
936 if( p->rDate<=0.0 ) SYNTAX("missing date in check-in");
937 if( p->nField>0 ) SYNTAX("J-card in check-in");
938 if( p->zTicketUuid ) SYNTAX("K-card in check-in");
939 if( p->zWikiTitle ) SYNTAX("L-card in check-in");
940 p->type = CFTYPE_MANIFEST;
941 }
942 md5sum_init();
943 if( !isRepeat ) g.parseCnt[p->type]++;
944 return p;
945
@@ -1610,10 +1606,24 @@
1610 blob_str(&comment), blob_str(&brief)
1611 );
1612 blob_reset(&comment);
1613 blob_reset(&brief);
1614 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1615
1616 /*
1617 ** Scan artifact rid/pContent to see if it is a control artifact of
1618 ** any key:
1619 **
@@ -1924,16 +1934,21 @@
1924 const char *zName;
1925 const char *zValue;
1926 const char *zUuid;
1927 int branchMove = 0;
1928 blob_zero(&comment);
 
 
 
 
 
1929 for(i=0; i<p->nTag; i++){
1930 zUuid = p->aTag[i].zUuid;
 
1931 if( i==0 || fossil_strcmp(zUuid, p->aTag[i-1].zUuid)!=0 ){
1932 if( i>0 ) blob_append(&comment, " ", 1);
1933 blob_appendf(&comment,
1934 "Edit [%S]:",
1935 zUuid);
1936 branchMove = 0;
1937 }
1938 zName = p->aTag[i].zName;
1939 zValue = p->aTag[i].zValue;
@@ -1992,14 +2007,15 @@
1992 blob_appendf(&comment, ".");
1993 }
1994 }
1995 }
1996 /*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
 
1997 db_multi_exec(
1998 "REPLACE INTO event(type,mtime,objid,user,comment)"
1999 "VALUES('g',%.17g,%d,%Q,%Q)",
2000 p->rDate, rid, p->zUser, blob_str(&comment)
2001 );
2002 blob_reset(&comment);
2003 }
2004 db_end_transaction(0);
2005 if( p->type==CFTYPE_MANIFEST ){
2006
--- src/manifest.c
+++ src/manifest.c
@@ -92,11 +92,11 @@
92 int nCChild; /* Number of cluster children */
93 int nCChildAlloc; /* Number of closts allocated in azCChild[] */
94 char **azCChild; /* UUIDs of referenced objects in a cluster. M cards */
95 int nTag; /* Number of T Cards */
96 int nTagAlloc; /* Slots allocated in aTag[] */
97 struct TagType {
98 char *zName; /* Name of the tag */
99 char *zUuid; /* UUID that the tag is applied to */
100 char *zValue; /* Value if the tag is really a property */
101 } *aTag; /* One for each T card */
102 int nField; /* Number of J cards */
@@ -353,11 +353,11 @@
353 char cType;
354 char *z;
355 int n;
356 char *zUuid;
357 int sz = 0;
358 int isRepeat, hasSelfRefTag = 0;
359 static Bag seen;
360 const char *zErr = 0;
361
362 if( rid==0 ){
363 isRepeat = 1;
@@ -378,27 +378,28 @@
378 blob_reset(pContent);
379 blob_appendf(pErr, n ? "not terminated with \\n" : "zero-length");
380 return 0;
381 }
382
383 /* Strip off the PGP signature if there is one.
 
384 */
385 remove_pgp_signature(&z, &n);
 
 
 
 
 
386
387 /* Verify that the first few characters of the artifact look like
388 ** a control artifact.
389 */
390 if( n<10 || z[0]<'A' || z[0]>'Z' || z[1]!=' ' ){
391 blob_reset(pContent);
392 blob_appendf(pErr, "line 1 not recognized");
393 return 0;
394 }
395 /* Then verify the Z-card.
396 */
397 if( verify_z_card(z, n)==2 ){
398 blob_reset(pContent);
399 blob_appendf(pErr, "incorrect Z-card cksum");
400 return 0;
401 }
402
403 /* Allocate a Manifest object to hold the parsed control artifact.
404 */
405 p = fossil_malloc( sizeof(*p) );
@@ -716,11 +717,11 @@
717 **
718 ** Specify the MD5 checksum over the name and content of all files
719 ** in the manifest.
720 */
721 case 'R': {
722 if( p->zRepoCksum!=0 ) SYNTAX("more than one R-card");
723 p->zRepoCksum = next_token(&x, &sz);
724 if( sz!=32 ) SYNTAX("wrong size cksum on R-card");
725 if( !validate16(p->zRepoCksum, 32) ) SYNTAX("malformed R-card cksum");
726 break;
727 }
@@ -748,12 +749,17 @@
749 if( zUuid==0 ) SYNTAX("missing UUID on T-card");
750 zValue = next_token(&x, 0);
751 if( zValue ) defossilize(zValue);
752 if( sz==UUID_SIZE && validate16(zUuid, UUID_SIZE) ){
753 /* A valid uuid */
754 if( p->zEventId ) SYNTAX("non-self-referential T-card in event");
755 }else if( sz==1 && zUuid[0]=='*' ){
756 zUuid = 0;
757 hasSelfRefTag = 1;
758 if( p->zEventId && zName[0]!='+' ){
759 SYNTAX("propagating T-card in event");
760 }
761 }else{
762 SYNTAX("malformed UUID on T-card");
763 }
764 defossilize(zName);
765 if( zName[0]!='-' && zName[0]!='+' && zName[0]!='*' ){
@@ -769,12 +775,15 @@
775 }
776 i = p->nTag++;
777 p->aTag[i].zName = zName;
778 p->aTag[i].zUuid = zUuid;
779 p->aTag[i].zValue = zValue;
780 if( i>0 ){
781 int c = fossil_strcmp(p->aTag[i-1].zName, zName);
782 if( c>0 || (c==0 && fossil_strcmp(p->aTag[i-1].zUuid, zUuid)>=0) ){
783 SYNTAX("T-card in the wrong order");
784 }
785 }
786 break;
787 }
788
789 /*
@@ -849,97 +858,84 @@
858 }
859 }
860 }
861 if( x.z<x.zEnd ) SYNTAX("extra characters at end of card");
862
863 if( p->nCChild>0 ){
864 if( p->zAttachName
865 || p->zBaseline
866 || p->zComment
867 || p->rDate>0.0
868 || p->zEventId
869 || p->nFile>0
 
 
 
 
 
 
 
 
 
 
870 || p->nField>0
871 || p->zTicketUuid
 
872 || p->zWikiTitle
 
 
873 || p->zMimetype
874 || p->nParent>0
875 || p->nCherrypick>0
876 || p->zRepoCksum
877 || p->nTag>0
878 || p->zUser
879 || p->zWiki
880 ){
881 SYNTAX("cluster contains a card other than M- or Z-");
882 }
883 if( !seenZ ) SYNTAX("missing Z-card on cluster");
884 p->type = CFTYPE_CLUSTER;
885 }else if( p->zEventId ){
886 if( p->rDate<=0.0 ) SYNTAX("missing date on event");
887 if( p->nFile>0 ) SYNTAX("F-card in event");
888 if( p->zRepoCksum ) SYNTAX("R-card in event");
889 if( p->zBaseline ) SYNTAX("B-card in event");
890 if( p->nField>0 ) SYNTAX("J-card in event");
891 if( p->zTicketUuid ) SYNTAX("K-card in event");
892 if( p->zWikiTitle!=0 ) SYNTAX("L-card in event");
893 if( p->zWiki==0 ) SYNTAX("missing W-card on event");
894 if( p->zAttachName ) SYNTAX("A-card in event");
895 if( !seenZ ) SYNTAX("missing Z-card on event");
896 p->type = CFTYPE_EVENT;
897 }else if( hasSelfRefTag || p->nFile>0 || p->zRepoCksum!=0 || p->zBaseline ){
898 if( p->rDate<=0.0 ) SYNTAX("missing date on manifest");
899 if( p->nField>0 ) SYNTAX("J-card in manifest");
900 if( p->zTicketUuid ) SYNTAX("K-card in manifest");
901 if( p->zWiki ) SYNTAX("W-card in manifest");
902 if( p->zWikiTitle ) SYNTAX("L-card in manifest");
903 if( p->zTicketUuid ) SYNTAX("K-card in manifest");
904 if( p->zAttachName ) SYNTAX("A-card in manifest");
905 p->type = CFTYPE_MANIFEST;
906 }else if( p->nField>0 || p->zTicketUuid!=0 ){
907 if( p->rDate<=0.0 ) SYNTAX("missing date on ticket");
908 if( p->zWiki ) SYNTAX("W-card in ticket");
909 if( p->zWikiTitle ) SYNTAX("L-card in ticket");
910 if( p->nField==0 ) SYNTAX("missing J-card on ticket");
 
911 if( p->nTag>0 ) SYNTAX("T-card in ticket");
912 if( p->zTicketUuid==0 ) SYNTAX("missing K-card on ticket");
913 if( p->zUser==0 ) SYNTAX("missing U-card on ticket");
914 if( p->zAttachName ) SYNTAX("A-card in ticket");
915 if( p->zMimetype) SYNTAX("N-card in ticket");
916 if( !seenZ ) SYNTAX("missing Z-card on ticket");
917 p->type = CFTYPE_TICKET;
918 }else if( p->zWiki!=0 || p->zWikiTitle!=0 ){
919 if( p->rDate<=0.0 ) SYNTAX("missing date on wiki");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
920 if( p->nTag>0 ) SYNTAX("T-card in wiki");
921 if( p->zWiki==0 ) SYNTAX("missing W-card on wiki");
922 if( p->zWikiTitle==0 ) SYNTAX("missing L-card on wiki");
923 if( p->zAttachName ) SYNTAX("A-card in wiki");
924 if( !seenZ ) SYNTAX("missing Z-card on wiki");
925 p->type = CFTYPE_WIKI;
 
 
 
 
 
 
 
 
 
926 }else if( p->zAttachName ){
927 if( p->rDate<=0.0 ) SYNTAX("missing date on attachment");
928 if( p->nTag>0 ) SYNTAX("T-card in attachment");
 
 
929 if( !seenZ ) SYNTAX("missing Z-card on attachment");
930 p->type = CFTYPE_ATTACHMENT;
931 }else{
932 if( p->rDate<=0.0 ) SYNTAX("missing date on control");
933 if( p->nParent>0 ) SYNTAX("P-card in control");
934 if( p->zMimetype ) SYNTAX("N-card in control");
935 if( !seenZ ) SYNTAX("missing Z-card on control");
936 p->type = CFTYPE_CONTROL;
 
937 }
938 md5sum_init();
939 if( !isRepeat ) g.parseCnt[p->type]++;
940 return p;
941
@@ -1610,10 +1606,24 @@
1606 blob_str(&comment), blob_str(&brief)
1607 );
1608 blob_reset(&comment);
1609 blob_reset(&brief);
1610 }
1611
1612 /*
1613 ** This is the comparison function used to sort the tag array.
1614 */
1615 static int tag_compare(const void *a, const void *b){
1616 struct TagType *pA = (struct TagType*)a;
1617 struct TagType *pB = (struct TagType*)b;
1618 int c;
1619 c = fossil_strcmp(pA->zUuid, pB->zUuid);
1620 if( c==0 ){
1621 c = fossil_strcmp(pA->zName, pB->zName);
1622 }
1623 return c;
1624 }
1625
1626 /*
1627 ** Scan artifact rid/pContent to see if it is a control artifact of
1628 ** any key:
1629 **
@@ -1924,16 +1934,21 @@
1934 const char *zName;
1935 const char *zValue;
1936 const char *zUuid;
1937 int branchMove = 0;
1938 blob_zero(&comment);
1939 if( p->zComment ){
1940 blob_appendf(&comment, " %s.", p->zComment);
1941 }
1942 /* Next loop expects tags to be sorted on UUID, so sort it. */
1943 qsort(p->aTag, p->nTag, sizeof(p->aTag[0]), tag_compare);
1944 for(i=0; i<p->nTag; i++){
1945 zUuid = p->aTag[i].zUuid;
1946 if( !zUuid ) continue;
1947 if( i==0 || fossil_strcmp(zUuid, p->aTag[i-1].zUuid)!=0 ){
 
1948 blob_appendf(&comment,
1949 " Edit [%S]:",
1950 zUuid);
1951 branchMove = 0;
1952 }
1953 zName = p->aTag[i].zName;
1954 zValue = p->aTag[i].zValue;
@@ -1992,14 +2007,15 @@
2007 blob_appendf(&comment, ".");
2008 }
2009 }
2010 }
2011 /*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
2012 if( blob_size(&comment)==0 ) blob_append(&comment, " ", 1);
2013 db_multi_exec(
2014 "REPLACE INTO event(type,mtime,objid,user,comment)"
2015 "VALUES('g',%.17g,%d,%Q,%Q)",
2016 p->rDate, rid, p->zUser, blob_str(&comment)+1
2017 );
2018 blob_reset(&comment);
2019 }
2020 db_end_transaction(0);
2021 if( p->type==CFTYPE_MANIFEST ){
2022
+11 -3
--- src/merge.c
+++ src/merge.c
@@ -258,13 +258,21 @@
258258
if( !forceFlag && mid==pid ){
259259
fossil_print("Merge skipped because it is a no-op. "
260260
" Use --force to override.\n");
261261
return;
262262
}
263
- if( integrateFlag && !is_a_leaf(mid) ){
264
- fossil_warning("ignoring --integrate: %s is not a leaf", g.argv[2]);
265
- integrateFlag = 0;
263
+ if( integrateFlag ){
264
+ if( db_exists("SELECT 1 FROM vmerge WHERE id=-4")) {
265
+ /* Fossil earlier than [55cacfcace] cannot handle this,
266
+ * therefore disallow it. */
267
+ fossil_fatal("Integration of another branch already in progress."
268
+ " Commit or Undo needed first", g.argv[2]);
269
+ }
270
+ if( !is_a_leaf(mid) ){
271
+ fossil_warning("ignoring --integrate: %s is not a leaf", g.argv[2]);
272
+ integrateFlag = 0;
273
+ }
266274
}
267275
if( verboseFlag ){
268276
print_checkin_description(mid, 12, integrateFlag?"integrate:":"merge-from:");
269277
print_checkin_description(pid, 12, "baseline:");
270278
}
271279
--- src/merge.c
+++ src/merge.c
@@ -258,13 +258,21 @@
258 if( !forceFlag && mid==pid ){
259 fossil_print("Merge skipped because it is a no-op. "
260 " Use --force to override.\n");
261 return;
262 }
263 if( integrateFlag && !is_a_leaf(mid) ){
264 fossil_warning("ignoring --integrate: %s is not a leaf", g.argv[2]);
265 integrateFlag = 0;
 
 
 
 
 
 
 
 
266 }
267 if( verboseFlag ){
268 print_checkin_description(mid, 12, integrateFlag?"integrate:":"merge-from:");
269 print_checkin_description(pid, 12, "baseline:");
270 }
271
--- src/merge.c
+++ src/merge.c
@@ -258,13 +258,21 @@
258 if( !forceFlag && mid==pid ){
259 fossil_print("Merge skipped because it is a no-op. "
260 " Use --force to override.\n");
261 return;
262 }
263 if( integrateFlag ){
264 if( db_exists("SELECT 1 FROM vmerge WHERE id=-4")) {
265 /* Fossil earlier than [55cacfcace] cannot handle this,
266 * therefore disallow it. */
267 fossil_fatal("Integration of another branch already in progress."
268 " Commit or Undo needed first", g.argv[2]);
269 }
270 if( !is_a_leaf(mid) ){
271 fossil_warning("ignoring --integrate: %s is not a leaf", g.argv[2]);
272 integrateFlag = 0;
273 }
274 }
275 if( verboseFlag ){
276 print_checkin_description(mid, 12, integrateFlag?"integrate:":"merge-from:");
277 print_checkin_description(pid, 12, "baseline:");
278 }
279
+13 -10
--- src/name.c
+++ src/name.c
@@ -248,20 +248,23 @@
248248
zTag, zType
249249
);
250250
if( rid>0 ) return rid;
251251
252252
/* Undocumented: numeric tags get translated directly into the RID */
253
- for(i=0; fossil_isdigit(zTag[i]); i++){}
254
- if( zTag[i]==0 ){
255
- if( strcmp(zType,"*")==0 ){
256
- rid = atoi(zTag);
257
- }else{
258
- rid = db_int(0,
259
- "SELECT event.objid"
260
- " FROM event"
261
- " WHERE event.objid=%s"
262
- " AND event.type GLOB '%q'", zTag, zType);
253
+ if( memcmp(zTag, "rid:", 4)==0 ){
254
+ zTag += 4;
255
+ for(i=0; fossil_isdigit(zTag[i]); i++){}
256
+ if( zTag[i]==0 ){
257
+ if( strcmp(zType,"*")==0 ){
258
+ rid = atoi(zTag);
259
+ }else{
260
+ rid = db_int(0,
261
+ "SELECT event.objid"
262
+ " FROM event"
263
+ " WHERE event.objid=%s"
264
+ " AND event.type GLOB '%q'", zTag, zType);
265
+ }
263266
}
264267
}
265268
return rid;
266269
}
267270
268271
--- src/name.c
+++ src/name.c
@@ -248,20 +248,23 @@
248 zTag, zType
249 );
250 if( rid>0 ) return rid;
251
252 /* Undocumented: numeric tags get translated directly into the RID */
253 for(i=0; fossil_isdigit(zTag[i]); i++){}
254 if( zTag[i]==0 ){
255 if( strcmp(zType,"*")==0 ){
256 rid = atoi(zTag);
257 }else{
258 rid = db_int(0,
259 "SELECT event.objid"
260 " FROM event"
261 " WHERE event.objid=%s"
262 " AND event.type GLOB '%q'", zTag, zType);
 
 
 
263 }
264 }
265 return rid;
266 }
267
268
--- src/name.c
+++ src/name.c
@@ -248,20 +248,23 @@
248 zTag, zType
249 );
250 if( rid>0 ) return rid;
251
252 /* Undocumented: numeric tags get translated directly into the RID */
253 if( memcmp(zTag, "rid:", 4)==0 ){
254 zTag += 4;
255 for(i=0; fossil_isdigit(zTag[i]); i++){}
256 if( zTag[i]==0 ){
257 if( strcmp(zType,"*")==0 ){
258 rid = atoi(zTag);
259 }else{
260 rid = db_int(0,
261 "SELECT event.objid"
262 " FROM event"
263 " WHERE event.objid=%s"
264 " AND event.type GLOB '%q'", zTag, zType);
265 }
266 }
267 }
268 return rid;
269 }
270
271
+1 -1
--- src/pivot.c
+++ src/pivot.c
@@ -82,11 +82,11 @@
8282
8383
/* aqueue must contain at least one primary and one other. Otherwise
8484
** we abort early
8585
*/
8686
if( db_int(0, "SELECT count(distinct src) FROM aqueue")<2 ){
87
- fossil_panic("lack both primary and secondary files");
87
+ fossil_fatal("lack both primary and secondary files");
8888
}
8989
9090
/* Prepare queries we will be needing
9191
**
9292
** The first query finds the oldest pending version on the aqueue. This
9393
--- src/pivot.c
+++ src/pivot.c
@@ -82,11 +82,11 @@
82
83 /* aqueue must contain at least one primary and one other. Otherwise
84 ** we abort early
85 */
86 if( db_int(0, "SELECT count(distinct src) FROM aqueue")<2 ){
87 fossil_panic("lack both primary and secondary files");
88 }
89
90 /* Prepare queries we will be needing
91 **
92 ** The first query finds the oldest pending version on the aqueue. This
93
--- src/pivot.c
+++ src/pivot.c
@@ -82,11 +82,11 @@
82
83 /* aqueue must contain at least one primary and one other. Otherwise
84 ** we abort early
85 */
86 if( db_int(0, "SELECT count(distinct src) FROM aqueue")<2 ){
87 fossil_fatal("lack both primary and secondary files");
88 }
89
90 /* Prepare queries we will be needing
91 **
92 ** The first query finds the oldest pending version on the aqueue. This
93
+50 -9
--- src/printf.c
+++ src/printf.c
@@ -22,10 +22,11 @@
2222
#include "printf.h"
2323
#if defined(_WIN32)
2424
# include <io.h>
2525
# include <fcntl.h>
2626
#endif
27
+#include <time.h>
2728
2829
/*
2930
** Conversion types fall into various categories as defined by the
3031
** following enumeration.
3132
*/
@@ -906,10 +907,43 @@
906907
fossil_puts(blob_str(&b), 1);
907908
blob_reset(&b);
908909
va_end(ap);
909910
}
910911
912
+/*
913
+** Write a message to the error log, if the error log filename is
914
+** defined.
915
+*/
916
+static void fossil_errorlog(const char *zFormat, ...){
917
+ struct tm *pNow;
918
+ time_t now;
919
+ FILE *out;
920
+ const char *z;
921
+ int i;
922
+ va_list ap;
923
+ static const char *azEnv[] = { "HTTP_HOST", "HTTP_USER_AGENT",
924
+ "PATH_INFO", "QUERY_STRING", "REMOTE_ADDR", "REQUEST_METHOD",
925
+ "REQUEST_URI", "SCRIPT_NAME" };
926
+ if( g.zErrlog==0 ) return;
927
+ out = fopen(g.zErrlog, "a");
928
+ if( out==0 ) return;
929
+ now = time(0);
930
+ pNow = gmtime(&now);
931
+ fprintf(out, "------------- %04d-%02d-%02d %02d:%02d:%02d UTC ------------\n",
932
+ pNow->tm_year+1900, pNow->tm_mon+1, pNow->tm_mday+1,
933
+ pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
934
+ va_start(ap, zFormat);
935
+ vfprintf(out, zFormat, ap);
936
+ fprintf(out, "\n");
937
+ va_end(ap);
938
+ for(i=0; i<sizeof(azEnv)/sizeof(azEnv[0]); i++){
939
+ if( (z = getenv(azEnv[i]))!=0 || (z = P(azEnv[i]))!=0 ){
940
+ fprintf(out, "%s=%s\n", azEnv[i], z);
941
+ }
942
+ }
943
+ fclose(out);
944
+}
911945
912946
/*
913947
** The following variable becomes true while processing a fatal error
914948
** or a panic. If additional "recursive-fatal" errors occur while
915949
** shutting down, the recursive errors are silently ignored.
@@ -919,18 +953,23 @@
919953
/*
920954
** Print an error message, rollback all databases, and quit. These
921955
** routines never return.
922956
*/
923957
NORETURN void fossil_panic(const char *zFormat, ...){
924
- char *z;
925958
va_list ap;
926959
int rc = 1;
927
- static int once = 1;
960
+ char z[1000];
961
+ static int once = 0;
962
+
963
+ if( once ) exit(1);
964
+ once = 1;
928965
mainInFatalError = 1;
966
+ db_force_rollback();
929967
va_start(ap, zFormat);
930
- z = vmprintf(zFormat, ap);
968
+ sqlite3_vsnprintf(sizeof(z),z,zFormat, ap);
931969
va_end(ap);
970
+ fossil_errorlog("panic: %s", z);
932971
#ifdef FOSSIL_ENABLE_JSON
933972
if( g.json.isJsonMode ){
934973
json_err( 0, z, 1 );
935974
if( g.isHTTP ){
936975
rc = 0 /* avoid HTTP 500 */;
@@ -937,22 +976,21 @@
937976
}
938977
}
939978
else
940979
#endif
941980
{
942
- if( g.cgiOutput && once ){
943
- once = 0;
981
+ if( g.cgiOutput ){
944982
cgi_printf("<p class=\"generalError\">%h</p>", z);
945983
cgi_reply();
946984
}else if( !g.fQuiet ){
947985
fossil_force_newline();
948
- fossil_trace("Fossil internal error: %s\n", z);
986
+ fossil_puts("Fossil internal error: ", 1);
987
+ fossil_puts(z, 1);
988
+ fossil_puts("\n", 1);
949989
}
950990
}
951
- free(z);
952
- db_force_rollback();
953
- fossil_exit(rc);
991
+ exit(rc);
954992
}
955993
956994
NORETURN void fossil_fatal(const char *zFormat, ...){
957995
char *z;
958996
int rc = 1;
@@ -959,10 +997,11 @@
959997
va_list ap;
960998
mainInFatalError = 1;
961999
va_start(ap, zFormat);
9621000
z = vmprintf(zFormat, ap);
9631001
va_end(ap);
1002
+ fossil_errorlog("fatal: %s", z);
9641003
#ifdef FOSSIL_ENABLE_JSON
9651004
if( g.json.isJsonMode ){
9661005
json_err( g.json.resultCode, z, 1 );
9671006
if( g.isHTTP ){
9681007
rc = 0 /* avoid HTTP 500 */;
@@ -1001,10 +1040,11 @@
10011040
if( mainInFatalError ) return;
10021041
mainInFatalError = 1;
10031042
va_start(ap, zFormat);
10041043
z = vmprintf(zFormat, ap);
10051044
va_end(ap);
1045
+ fossil_errorlog("fatal: %s", z);
10061046
#ifdef FOSSIL_ENABLE_JSON
10071047
if( g.json.isJsonMode ){
10081048
json_err( g.json.resultCode, z, 1 );
10091049
if( g.isHTTP ){
10101050
rc = 0 /* avoid HTTP 500 */;
@@ -1031,10 +1071,11 @@
10311071
char *z;
10321072
va_list ap;
10331073
va_start(ap, zFormat);
10341074
z = vmprintf(zFormat, ap);
10351075
va_end(ap);
1076
+ fossil_errorlog("warning: %s", z);
10361077
#ifdef FOSSIL_ENABLE_JSON
10371078
if(g.json.isJsonMode){
10381079
json_warn( FSL_JSON_W_UNKNOWN, z );
10391080
}else
10401081
#endif
10411082
--- src/printf.c
+++ src/printf.c
@@ -22,10 +22,11 @@
22 #include "printf.h"
23 #if defined(_WIN32)
24 # include <io.h>
25 # include <fcntl.h>
26 #endif
 
27
28 /*
29 ** Conversion types fall into various categories as defined by the
30 ** following enumeration.
31 */
@@ -906,10 +907,43 @@
906 fossil_puts(blob_str(&b), 1);
907 blob_reset(&b);
908 va_end(ap);
909 }
910
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
911
912 /*
913 ** The following variable becomes true while processing a fatal error
914 ** or a panic. If additional "recursive-fatal" errors occur while
915 ** shutting down, the recursive errors are silently ignored.
@@ -919,18 +953,23 @@
919 /*
920 ** Print an error message, rollback all databases, and quit. These
921 ** routines never return.
922 */
923 NORETURN void fossil_panic(const char *zFormat, ...){
924 char *z;
925 va_list ap;
926 int rc = 1;
927 static int once = 1;
 
 
 
 
928 mainInFatalError = 1;
 
929 va_start(ap, zFormat);
930 z = vmprintf(zFormat, ap);
931 va_end(ap);
 
932 #ifdef FOSSIL_ENABLE_JSON
933 if( g.json.isJsonMode ){
934 json_err( 0, z, 1 );
935 if( g.isHTTP ){
936 rc = 0 /* avoid HTTP 500 */;
@@ -937,22 +976,21 @@
937 }
938 }
939 else
940 #endif
941 {
942 if( g.cgiOutput && once ){
943 once = 0;
944 cgi_printf("<p class=\"generalError\">%h</p>", z);
945 cgi_reply();
946 }else if( !g.fQuiet ){
947 fossil_force_newline();
948 fossil_trace("Fossil internal error: %s\n", z);
 
 
949 }
950 }
951 free(z);
952 db_force_rollback();
953 fossil_exit(rc);
954 }
955
956 NORETURN void fossil_fatal(const char *zFormat, ...){
957 char *z;
958 int rc = 1;
@@ -959,10 +997,11 @@
959 va_list ap;
960 mainInFatalError = 1;
961 va_start(ap, zFormat);
962 z = vmprintf(zFormat, ap);
963 va_end(ap);
 
964 #ifdef FOSSIL_ENABLE_JSON
965 if( g.json.isJsonMode ){
966 json_err( g.json.resultCode, z, 1 );
967 if( g.isHTTP ){
968 rc = 0 /* avoid HTTP 500 */;
@@ -1001,10 +1040,11 @@
1001 if( mainInFatalError ) return;
1002 mainInFatalError = 1;
1003 va_start(ap, zFormat);
1004 z = vmprintf(zFormat, ap);
1005 va_end(ap);
 
1006 #ifdef FOSSIL_ENABLE_JSON
1007 if( g.json.isJsonMode ){
1008 json_err( g.json.resultCode, z, 1 );
1009 if( g.isHTTP ){
1010 rc = 0 /* avoid HTTP 500 */;
@@ -1031,10 +1071,11 @@
1031 char *z;
1032 va_list ap;
1033 va_start(ap, zFormat);
1034 z = vmprintf(zFormat, ap);
1035 va_end(ap);
 
1036 #ifdef FOSSIL_ENABLE_JSON
1037 if(g.json.isJsonMode){
1038 json_warn( FSL_JSON_W_UNKNOWN, z );
1039 }else
1040 #endif
1041
--- src/printf.c
+++ src/printf.c
@@ -22,10 +22,11 @@
22 #include "printf.h"
23 #if defined(_WIN32)
24 # include <io.h>
25 # include <fcntl.h>
26 #endif
27 #include <time.h>
28
29 /*
30 ** Conversion types fall into various categories as defined by the
31 ** following enumeration.
32 */
@@ -906,10 +907,43 @@
907 fossil_puts(blob_str(&b), 1);
908 blob_reset(&b);
909 va_end(ap);
910 }
911
912 /*
913 ** Write a message to the error log, if the error log filename is
914 ** defined.
915 */
916 static void fossil_errorlog(const char *zFormat, ...){
917 struct tm *pNow;
918 time_t now;
919 FILE *out;
920 const char *z;
921 int i;
922 va_list ap;
923 static const char *azEnv[] = { "HTTP_HOST", "HTTP_USER_AGENT",
924 "PATH_INFO", "QUERY_STRING", "REMOTE_ADDR", "REQUEST_METHOD",
925 "REQUEST_URI", "SCRIPT_NAME" };
926 if( g.zErrlog==0 ) return;
927 out = fopen(g.zErrlog, "a");
928 if( out==0 ) return;
929 now = time(0);
930 pNow = gmtime(&now);
931 fprintf(out, "------------- %04d-%02d-%02d %02d:%02d:%02d UTC ------------\n",
932 pNow->tm_year+1900, pNow->tm_mon+1, pNow->tm_mday+1,
933 pNow->tm_hour, pNow->tm_min, pNow->tm_sec);
934 va_start(ap, zFormat);
935 vfprintf(out, zFormat, ap);
936 fprintf(out, "\n");
937 va_end(ap);
938 for(i=0; i<sizeof(azEnv)/sizeof(azEnv[0]); i++){
939 if( (z = getenv(azEnv[i]))!=0 || (z = P(azEnv[i]))!=0 ){
940 fprintf(out, "%s=%s\n", azEnv[i], z);
941 }
942 }
943 fclose(out);
944 }
945
946 /*
947 ** The following variable becomes true while processing a fatal error
948 ** or a panic. If additional "recursive-fatal" errors occur while
949 ** shutting down, the recursive errors are silently ignored.
@@ -919,18 +953,23 @@
953 /*
954 ** Print an error message, rollback all databases, and quit. These
955 ** routines never return.
956 */
957 NORETURN void fossil_panic(const char *zFormat, ...){
 
958 va_list ap;
959 int rc = 1;
960 char z[1000];
961 static int once = 0;
962
963 if( once ) exit(1);
964 once = 1;
965 mainInFatalError = 1;
966 db_force_rollback();
967 va_start(ap, zFormat);
968 sqlite3_vsnprintf(sizeof(z),z,zFormat, ap);
969 va_end(ap);
970 fossil_errorlog("panic: %s", z);
971 #ifdef FOSSIL_ENABLE_JSON
972 if( g.json.isJsonMode ){
973 json_err( 0, z, 1 );
974 if( g.isHTTP ){
975 rc = 0 /* avoid HTTP 500 */;
@@ -937,22 +976,21 @@
976 }
977 }
978 else
979 #endif
980 {
981 if( g.cgiOutput ){
 
982 cgi_printf("<p class=\"generalError\">%h</p>", z);
983 cgi_reply();
984 }else if( !g.fQuiet ){
985 fossil_force_newline();
986 fossil_puts("Fossil internal error: ", 1);
987 fossil_puts(z, 1);
988 fossil_puts("\n", 1);
989 }
990 }
991 exit(rc);
 
 
992 }
993
994 NORETURN void fossil_fatal(const char *zFormat, ...){
995 char *z;
996 int rc = 1;
@@ -959,10 +997,11 @@
997 va_list ap;
998 mainInFatalError = 1;
999 va_start(ap, zFormat);
1000 z = vmprintf(zFormat, ap);
1001 va_end(ap);
1002 fossil_errorlog("fatal: %s", z);
1003 #ifdef FOSSIL_ENABLE_JSON
1004 if( g.json.isJsonMode ){
1005 json_err( g.json.resultCode, z, 1 );
1006 if( g.isHTTP ){
1007 rc = 0 /* avoid HTTP 500 */;
@@ -1001,10 +1040,11 @@
1040 if( mainInFatalError ) return;
1041 mainInFatalError = 1;
1042 va_start(ap, zFormat);
1043 z = vmprintf(zFormat, ap);
1044 va_end(ap);
1045 fossil_errorlog("fatal: %s", z);
1046 #ifdef FOSSIL_ENABLE_JSON
1047 if( g.json.isJsonMode ){
1048 json_err( g.json.resultCode, z, 1 );
1049 if( g.isHTTP ){
1050 rc = 0 /* avoid HTTP 500 */;
@@ -1031,10 +1071,11 @@
1071 char *z;
1072 va_list ap;
1073 va_start(ap, zFormat);
1074 z = vmprintf(zFormat, ap);
1075 va_end(ap);
1076 fossil_errorlog("warning: %s", z);
1077 #ifdef FOSSIL_ENABLE_JSON
1078 if(g.json.isJsonMode){
1079 json_warn( FSL_JSON_W_UNKNOWN, z );
1080 }else
1081 #endif
1082
+2 -2
--- src/rebuild.c
+++ src/rebuild.c
@@ -862,11 +862,11 @@
862862
recon_read_dir(zSubpath);
863863
}
864864
blob_init(&path, 0, 0);
865865
blob_appendf(&path, "%s", zSubpath);
866866
if( blob_read_from_file(&aContent, blob_str(&path))==-1 ){
867
- fossil_panic("some unknown error occurred while reading \"%s\"",
867
+ fossil_fatal("some unknown error occurred while reading \"%s\"",
868868
blob_str(&path));
869869
}
870870
content_put(&aContent);
871871
blob_reset(&path);
872872
blob_reset(&aContent);
@@ -874,11 +874,11 @@
874874
fossil_print("\r%d", ++nFileRead);
875875
fflush(stdout);
876876
}
877877
closedir(d);
878878
}else {
879
- fossil_panic("encountered error %d while trying to open \"%s\".",
879
+ fossil_fatal("encountered error %d while trying to open \"%s\".",
880880
errno, g.argv[3]);
881881
}
882882
fossil_filename_free(zUnicodePath);
883883
}
884884
885885
--- src/rebuild.c
+++ src/rebuild.c
@@ -862,11 +862,11 @@
862 recon_read_dir(zSubpath);
863 }
864 blob_init(&path, 0, 0);
865 blob_appendf(&path, "%s", zSubpath);
866 if( blob_read_from_file(&aContent, blob_str(&path))==-1 ){
867 fossil_panic("some unknown error occurred while reading \"%s\"",
868 blob_str(&path));
869 }
870 content_put(&aContent);
871 blob_reset(&path);
872 blob_reset(&aContent);
@@ -874,11 +874,11 @@
874 fossil_print("\r%d", ++nFileRead);
875 fflush(stdout);
876 }
877 closedir(d);
878 }else {
879 fossil_panic("encountered error %d while trying to open \"%s\".",
880 errno, g.argv[3]);
881 }
882 fossil_filename_free(zUnicodePath);
883 }
884
885
--- src/rebuild.c
+++ src/rebuild.c
@@ -862,11 +862,11 @@
862 recon_read_dir(zSubpath);
863 }
864 blob_init(&path, 0, 0);
865 blob_appendf(&path, "%s", zSubpath);
866 if( blob_read_from_file(&aContent, blob_str(&path))==-1 ){
867 fossil_fatal("some unknown error occurred while reading \"%s\"",
868 blob_str(&path));
869 }
870 content_put(&aContent);
871 blob_reset(&path);
872 blob_reset(&aContent);
@@ -874,11 +874,11 @@
874 fossil_print("\r%d", ++nFileRead);
875 fflush(stdout);
876 }
877 closedir(d);
878 }else {
879 fossil_fatal("encountered error %d while trying to open \"%s\".",
880 errno, g.argv[3]);
881 }
882 fossil_filename_free(zUnicodePath);
883 }
884
885
--- src/report.c
+++ src/report.c
@@ -78,10 +78,11 @@
7878
blob_appendf(&ril, "[%zsql</a>]",
7979
href("%R/rptsql?rn=%d", rn));
8080
}
8181
blob_appendf(&ril, "</li>\n");
8282
}
83
+ db_finalize(&q);
8384
8485
Th_Store("report_items", blob_str(&ril));
8586
8687
Th_Render(zScript);
8788
8889
--- src/report.c
+++ src/report.c
@@ -78,10 +78,11 @@
78 blob_appendf(&ril, "[%zsql</a>]",
79 href("%R/rptsql?rn=%d", rn));
80 }
81 blob_appendf(&ril, "</li>\n");
82 }
 
83
84 Th_Store("report_items", blob_str(&ril));
85
86 Th_Render(zScript);
87
88
--- src/report.c
+++ src/report.c
@@ -78,10 +78,11 @@
78 blob_appendf(&ril, "[%zsql</a>]",
79 href("%R/rptsql?rn=%d", rn));
80 }
81 blob_appendf(&ril, "</li>\n");
82 }
83 db_finalize(&q);
84
85 Th_Store("report_items", blob_str(&ril));
86
87 Th_Render(zScript);
88
89
+70 -79
--- src/shell.c
+++ src/shell.c
@@ -51,11 +51,10 @@
5151
#if defined(HAVE_READLINE) && HAVE_READLINE==1
5252
# include <readline/readline.h>
5353
# include <readline/history.h>
5454
#endif
5555
#if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
56
-# define readline(p) local_getline(p,stdin,0)
5756
# define add_history(X)
5857
# define read_history(X)
5958
# define write_history(X)
6059
# define stifle_history(X)
6160
#endif
@@ -335,27 +334,17 @@
335334
** This routine reads a line of text from FILE in, stores
336335
** the text in memory obtained from malloc() and returns a pointer
337336
** to the text. NULL is returned at end of file, or if malloc()
338337
** fails.
339338
**
340
-** The interface is like "readline" but no command-line editing
341
-** is done.
339
+** If zLine is not NULL then it is a malloced buffer returned from
340
+** a previous call to this routine that may be reused.
342341
*/
343
-static char *local_getline(char *zPrompt, FILE *in, int csvFlag){
344
- char *zLine;
345
- int nLine;
346
- int n;
347
- int inQuote = 0;
348
-
349
- if( zPrompt && *zPrompt ){
350
- printf("%s",zPrompt);
351
- fflush(stdout);
352
- }
353
- nLine = 100;
354
- zLine = malloc( nLine );
355
- if( zLine==0 ) return 0;
356
- n = 0;
342
+static char *local_getline(char *zLine, FILE *in){
343
+ int nLine = zLine==0 ? 0 : 100;
344
+ int n = 0;
345
+
357346
while( 1 ){
358347
if( n+100>nLine ){
359348
nLine = nLine*2 + 100;
360349
zLine = realloc(zLine, nLine);
361350
if( zLine==0 ) return 0;
@@ -366,46 +355,52 @@
366355
return 0;
367356
}
368357
zLine[n] = 0;
369358
break;
370359
}
371
- while( zLine[n] ){
372
- if( zLine[n]=='"' ) inQuote = !inQuote;
373
- n++;
374
- }
375
- if( n>0 && zLine[n-1]=='\n' && (!inQuote || !csvFlag) ){
360
+ while( zLine[n] ) n++;
361
+ if( n>0 && zLine[n-1]=='\n' ){
376362
n--;
377363
if( n>0 && zLine[n-1]=='\r' ) n--;
378364
zLine[n] = 0;
379365
break;
380366
}
381367
}
382
- zLine = realloc( zLine, n+1 );
383368
return zLine;
384369
}
385370
386371
/*
387372
** Retrieve a single line of input text.
388373
**
389
-** zPrior is a string of prior text retrieved. If not the empty
390
-** string, then issue a continuation prompt.
374
+** If in==0 then read from standard input and prompt before each line.
375
+** If isContinuation is true, then a continuation prompt is appropriate.
376
+** If isContinuation is zero, then the main prompt should be used.
377
+**
378
+** If zPrior is not NULL then it is a buffer from a prior call to this
379
+** routine that can be reused.
380
+**
381
+** The result is stored in space obtained from malloc() and must either
382
+** be freed by the caller or else passed back into this routine via the
383
+** zPrior argument for reuse.
391384
*/
392
-static char *one_input_line(const char *zPrior, FILE *in){
385
+static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
393386
char *zPrompt;
394387
char *zResult;
395388
if( in!=0 ){
396
- return local_getline(0, in, 0);
397
- }
398
- if( zPrior && zPrior[0] ){
399
- zPrompt = continuePrompt;
389
+ zResult = local_getline(zPrior, in);
400390
}else{
401
- zPrompt = mainPrompt;
402
- }
403
- zResult = readline(zPrompt);
391
+ zPrompt = isContinuation ? continuePrompt : mainPrompt;
404392
#if defined(HAVE_READLINE) && HAVE_READLINE==1
405
- if( zResult && *zResult ) add_history(zResult);
393
+ free(zPrior);
394
+ zResult = readline(zPrompt);
395
+ if( zResult && *zResult ) add_history(zResult);
396
+#else
397
+ printf("%s", zPrompt);
398
+ fflush(stdout);
399
+ zResult = local_getline(zPrior, stdin);
406400
#endif
401
+ }
407402
return zResult;
408403
}
409404
410405
struct previous_mode_data {
411406
int valid; /* Is there legit data in here? */
@@ -1720,11 +1715,11 @@
17201715
continue;
17211716
}
17221717
}
17231718
if( (c==cSep && pc==cQuote)
17241719
|| (c=='\n' && pc==cQuote)
1725
- || (c=='\n' && pc=='\r' && p->n>2 && p->z[p->n-2]==cQuote)
1720
+ || (c=='\n' && pc=='\r' && p->n>=2 && p->z[p->n-2]==cQuote)
17261721
|| (c==EOF && pc==cQuote)
17271722
){
17281723
do{ p->n--; }while( p->z[p->n]!=cQuote );
17291724
p->cTerm = c;
17301725
break;
@@ -1993,10 +1988,11 @@
19931988
char *zFile = azArg[1]; /* Name of file to extra content from */
19941989
sqlite3_stmt *pStmt = NULL; /* A statement */
19951990
int nCol; /* Number of columns in the table */
19961991
int nByte; /* Number of bytes in an SQL string */
19971992
int i, j; /* Loop counters */
1993
+ int needCommit; /* True to COMMIT or ROLLBACK at end */
19981994
int nSep; /* Number of bytes in p->separator[] */
19991995
char *zSql; /* An SQL statement */
20001996
CSVReader sCsv; /* Reader context */
20011997
int (*xCloser)(FILE*); /* Procedure to close th3 connection */
20021998
@@ -2094,10 +2090,12 @@
20942090
fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
20952091
if (pStmt) sqlite3_finalize(pStmt);
20962092
xCloser(sCsv.in);
20972093
return 1;
20982094
}
2095
+ needCommit = sqlite3_get_autocommit(db);
2096
+ if( needCommit ) sqlite3_exec(db, "BEGIN", 0, 0, 0);
20992097
do{
21002098
int startLine = sCsv.nLine;
21012099
for(i=0; i<nCol; i++){
21022100
char *z = csv_read_one_field(&sCsv);
21032101
if( z==0 && i==0 ) break;
@@ -2130,11 +2128,11 @@
21302128
}while( sCsv.cTerm!=EOF );
21312129
21322130
xCloser(sCsv.in);
21332131
sqlite3_free(sCsv.z);
21342132
sqlite3_finalize(pStmt);
2135
- sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
2133
+ if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
21362134
}else
21372135
21382136
if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
21392137
struct callback_data data;
21402138
char *zErrMsg = 0;
@@ -2779,11 +2777,11 @@
27792777
27802778
/*
27812779
** Return TRUE if a semicolon occurs anywhere in the first N characters
27822780
** of string z[].
27832781
*/
2784
-static int _contains_semicolon(const char *z, int N){
2782
+static int line_contains_semicolon(const char *z, int N){
27852783
int i;
27862784
for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
27872785
return 0;
27882786
}
27892787
@@ -2814,11 +2812,11 @@
28142812
/*
28152813
** Return TRUE if the line typed in is an SQL command terminator other
28162814
** than a semi-colon. The SQL Server style "go" command is understood
28172815
** as is the Oracle "/".
28182816
*/
2819
-static int _is_command_terminator(const char *zLine){
2817
+static int line_is_command_terminator(const char *zLine){
28202818
while( IsSpace(zLine[0]) ){ zLine++; };
28212819
if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
28222820
return 1; /* Oracle */
28232821
}
28242822
if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
@@ -2830,11 +2828,11 @@
28302828
28312829
/*
28322830
** Return true if zSql is a complete SQL statement. Return false if it
28332831
** ends in the middle of a string literal or C-style comment.
28342832
*/
2835
-static int _is_complete(char *zSql, int nSql){
2833
+static int line_is_complete(char *zSql, int nSql){
28362834
int rc;
28372835
if( zSql==0 ) return 1;
28382836
zSql[nSql] = ';';
28392837
zSql[nSql+1] = 0;
28402838
rc = sqlite3_complete(zSql);
@@ -2850,24 +2848,25 @@
28502848
** cause this routine to exit immediately, unless input is interactive.
28512849
**
28522850
** Return the number of errors.
28532851
*/
28542852
static int process_input(struct callback_data *p, FILE *in){
2855
- char *zLine = 0;
2856
- char *zSql = 0;
2857
- int nSql = 0;
2858
- int nSqlPrior = 0;
2859
- char *zErrMsg;
2860
- int rc;
2861
- int errCnt = 0;
2862
- int lineno = 0;
2863
- int startline = 0;
2853
+ char *zLine = 0; /* A single input line */
2854
+ char *zSql = 0; /* Accumulated SQL text */
2855
+ int nLine; /* Length of current line */
2856
+ int nSql = 0; /* Bytes of zSql[] used */
2857
+ int nAlloc = 0; /* Allocated zSql[] space */
2858
+ int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
2859
+ char *zErrMsg; /* Error message returned */
2860
+ int rc; /* Error code */
2861
+ int errCnt = 0; /* Number of errors seen */
2862
+ int lineno = 0; /* Current line number */
2863
+ int startline = 0; /* Line number for start of current input */
28642864
28652865
while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
28662866
fflush(p->out);
2867
- free(zLine);
2868
- zLine = one_input_line(zSql, in);
2867
+ zLine = one_input_line(in, zLine, nSql>0);
28692868
if( zLine==0 ){
28702869
/* End of input */
28712870
if( stdin_is_interactive ) printf("\n");
28722871
break;
28732872
}
@@ -2874,11 +2873,11 @@
28742873
if( seenInterrupt ){
28752874
if( in!=0 ) break;
28762875
seenInterrupt = 0;
28772876
}
28782877
lineno++;
2879
- if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
2878
+ if( nSql==0 && _all_whitespace(zLine) ) continue;
28802879
if( zLine && zLine[0]=='.' && nSql==0 ){
28812880
if( p->echoOn ) printf("%s\n", zLine);
28822881
rc = do_meta_command(zLine, p);
28832882
if( rc==2 ){ /* exit requested */
28842883
break;
@@ -2885,39 +2884,36 @@
28852884
}else if( rc ){
28862885
errCnt++;
28872886
}
28882887
continue;
28892888
}
2890
- if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
2889
+ if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
28912890
memcpy(zLine,";",2);
2891
+ }
2892
+ nLine = strlen30(zLine);
2893
+ if( nSql+nLine+2>=nAlloc ){
2894
+ nAlloc = nSql+nLine+100;
2895
+ zSql = realloc(zSql, nAlloc);
2896
+ if( zSql==0 ){
2897
+ fprintf(stderr, "Error: out of memory\n");
2898
+ exit(1);
2899
+ }
28922900
}
28932901
nSqlPrior = nSql;
2894
- if( zSql==0 ){
2902
+ if( nSql==0 ){
28952903
int i;
28962904
for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2897
- if( zLine[i]!=0 ){
2898
- nSql = strlen30(zLine);
2899
- zSql = malloc( nSql+3 );
2900
- if( zSql==0 ){
2901
- fprintf(stderr, "Error: out of memory\n");
2902
- exit(1);
2903
- }
2904
- memcpy(zSql, zLine, nSql+1);
2905
- startline = lineno;
2906
- }
2905
+ assert( nAlloc>0 && zSql!=0 );
2906
+ memcpy(zSql, zLine+i, nLine+1-i);
2907
+ startline = lineno;
2908
+ nSql = nLine-i;
29072909
}else{
2908
- int len = strlen30(zLine);
2909
- zSql = realloc( zSql, nSql + len + 4 );
2910
- if( zSql==0 ){
2911
- fprintf(stderr,"Error: out of memory\n");
2912
- exit(1);
2913
- }
29142910
zSql[nSql++] = '\n';
2915
- memcpy(&zSql[nSql], zLine, len+1);
2916
- nSql += len;
2911
+ memcpy(zSql+nSql, zLine, nLine+1);
2912
+ nSql += nLine;
29172913
}
2918
- if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
2914
+ if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
29192915
&& sqlite3_complete(zSql) ){
29202916
p->cnt = 0;
29212917
open_db(p);
29222918
BEGIN_TIMER;
29232919
rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
@@ -2937,20 +2933,16 @@
29372933
}else{
29382934
fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
29392935
}
29402936
errCnt++;
29412937
}
2942
- free(zSql);
2943
- zSql = 0;
29442938
nSql = 0;
2945
- }else if( zSql && _all_whitespace(zSql) ){
2946
- free(zSql);
2947
- zSql = 0;
2939
+ }else if( nSql && _all_whitespace(zSql) ){
29482940
nSql = 0;
29492941
}
29502942
}
2951
- if( zSql ){
2943
+ if( nSql ){
29522944
if( !_all_whitespace(zSql) ){
29532945
fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
29542946
}
29552947
free(zSql);
29562948
}
@@ -3194,11 +3186,10 @@
31943186
** we do the actual processing of arguments later in a second pass.
31953187
*/
31963188
stdin_is_interactive = 0;
31973189
}else if( strcmp(z,"-heap")==0 ){
31983190
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3199
- int j, c;
32003191
const char *zSize;
32013192
sqlite3_int64 szHeap;
32023193
32033194
zSize = cmdline_option_value(argc, argv, ++i);
32043195
szHeap = integerValue(zSize);
32053196
--- src/shell.c
+++ src/shell.c
@@ -51,11 +51,10 @@
51 #if defined(HAVE_READLINE) && HAVE_READLINE==1
52 # include <readline/readline.h>
53 # include <readline/history.h>
54 #endif
55 #if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
56 # define readline(p) local_getline(p,stdin,0)
57 # define add_history(X)
58 # define read_history(X)
59 # define write_history(X)
60 # define stifle_history(X)
61 #endif
@@ -335,27 +334,17 @@
335 ** This routine reads a line of text from FILE in, stores
336 ** the text in memory obtained from malloc() and returns a pointer
337 ** to the text. NULL is returned at end of file, or if malloc()
338 ** fails.
339 **
340 ** The interface is like "readline" but no command-line editing
341 ** is done.
342 */
343 static char *local_getline(char *zPrompt, FILE *in, int csvFlag){
344 char *zLine;
345 int nLine;
346 int n;
347 int inQuote = 0;
348
349 if( zPrompt && *zPrompt ){
350 printf("%s",zPrompt);
351 fflush(stdout);
352 }
353 nLine = 100;
354 zLine = malloc( nLine );
355 if( zLine==0 ) return 0;
356 n = 0;
357 while( 1 ){
358 if( n+100>nLine ){
359 nLine = nLine*2 + 100;
360 zLine = realloc(zLine, nLine);
361 if( zLine==0 ) return 0;
@@ -366,46 +355,52 @@
366 return 0;
367 }
368 zLine[n] = 0;
369 break;
370 }
371 while( zLine[n] ){
372 if( zLine[n]=='"' ) inQuote = !inQuote;
373 n++;
374 }
375 if( n>0 && zLine[n-1]=='\n' && (!inQuote || !csvFlag) ){
376 n--;
377 if( n>0 && zLine[n-1]=='\r' ) n--;
378 zLine[n] = 0;
379 break;
380 }
381 }
382 zLine = realloc( zLine, n+1 );
383 return zLine;
384 }
385
386 /*
387 ** Retrieve a single line of input text.
388 **
389 ** zPrior is a string of prior text retrieved. If not the empty
390 ** string, then issue a continuation prompt.
 
 
 
 
 
 
 
 
391 */
392 static char *one_input_line(const char *zPrior, FILE *in){
393 char *zPrompt;
394 char *zResult;
395 if( in!=0 ){
396 return local_getline(0, in, 0);
397 }
398 if( zPrior && zPrior[0] ){
399 zPrompt = continuePrompt;
400 }else{
401 zPrompt = mainPrompt;
402 }
403 zResult = readline(zPrompt);
404 #if defined(HAVE_READLINE) && HAVE_READLINE==1
405 if( zResult && *zResult ) add_history(zResult);
 
 
 
 
 
 
406 #endif
 
407 return zResult;
408 }
409
410 struct previous_mode_data {
411 int valid; /* Is there legit data in here? */
@@ -1720,11 +1715,11 @@
1720 continue;
1721 }
1722 }
1723 if( (c==cSep && pc==cQuote)
1724 || (c=='\n' && pc==cQuote)
1725 || (c=='\n' && pc=='\r' && p->n>2 && p->z[p->n-2]==cQuote)
1726 || (c==EOF && pc==cQuote)
1727 ){
1728 do{ p->n--; }while( p->z[p->n]!=cQuote );
1729 p->cTerm = c;
1730 break;
@@ -1993,10 +1988,11 @@
1993 char *zFile = azArg[1]; /* Name of file to extra content from */
1994 sqlite3_stmt *pStmt = NULL; /* A statement */
1995 int nCol; /* Number of columns in the table */
1996 int nByte; /* Number of bytes in an SQL string */
1997 int i, j; /* Loop counters */
 
1998 int nSep; /* Number of bytes in p->separator[] */
1999 char *zSql; /* An SQL statement */
2000 CSVReader sCsv; /* Reader context */
2001 int (*xCloser)(FILE*); /* Procedure to close th3 connection */
2002
@@ -2094,10 +2090,12 @@
2094 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
2095 if (pStmt) sqlite3_finalize(pStmt);
2096 xCloser(sCsv.in);
2097 return 1;
2098 }
 
 
2099 do{
2100 int startLine = sCsv.nLine;
2101 for(i=0; i<nCol; i++){
2102 char *z = csv_read_one_field(&sCsv);
2103 if( z==0 && i==0 ) break;
@@ -2130,11 +2128,11 @@
2130 }while( sCsv.cTerm!=EOF );
2131
2132 xCloser(sCsv.in);
2133 sqlite3_free(sCsv.z);
2134 sqlite3_finalize(pStmt);
2135 sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
2136 }else
2137
2138 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
2139 struct callback_data data;
2140 char *zErrMsg = 0;
@@ -2779,11 +2777,11 @@
2779
2780 /*
2781 ** Return TRUE if a semicolon occurs anywhere in the first N characters
2782 ** of string z[].
2783 */
2784 static int _contains_semicolon(const char *z, int N){
2785 int i;
2786 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
2787 return 0;
2788 }
2789
@@ -2814,11 +2812,11 @@
2814 /*
2815 ** Return TRUE if the line typed in is an SQL command terminator other
2816 ** than a semi-colon. The SQL Server style "go" command is understood
2817 ** as is the Oracle "/".
2818 */
2819 static int _is_command_terminator(const char *zLine){
2820 while( IsSpace(zLine[0]) ){ zLine++; };
2821 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2822 return 1; /* Oracle */
2823 }
2824 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
@@ -2830,11 +2828,11 @@
2830
2831 /*
2832 ** Return true if zSql is a complete SQL statement. Return false if it
2833 ** ends in the middle of a string literal or C-style comment.
2834 */
2835 static int _is_complete(char *zSql, int nSql){
2836 int rc;
2837 if( zSql==0 ) return 1;
2838 zSql[nSql] = ';';
2839 zSql[nSql+1] = 0;
2840 rc = sqlite3_complete(zSql);
@@ -2850,24 +2848,25 @@
2850 ** cause this routine to exit immediately, unless input is interactive.
2851 **
2852 ** Return the number of errors.
2853 */
2854 static int process_input(struct callback_data *p, FILE *in){
2855 char *zLine = 0;
2856 char *zSql = 0;
2857 int nSql = 0;
2858 int nSqlPrior = 0;
2859 char *zErrMsg;
2860 int rc;
2861 int errCnt = 0;
2862 int lineno = 0;
2863 int startline = 0;
 
 
2864
2865 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
2866 fflush(p->out);
2867 free(zLine);
2868 zLine = one_input_line(zSql, in);
2869 if( zLine==0 ){
2870 /* End of input */
2871 if( stdin_is_interactive ) printf("\n");
2872 break;
2873 }
@@ -2874,11 +2873,11 @@
2874 if( seenInterrupt ){
2875 if( in!=0 ) break;
2876 seenInterrupt = 0;
2877 }
2878 lineno++;
2879 if( (zSql==0 || zSql[0]==0) && _all_whitespace(zLine) ) continue;
2880 if( zLine && zLine[0]=='.' && nSql==0 ){
2881 if( p->echoOn ) printf("%s\n", zLine);
2882 rc = do_meta_command(zLine, p);
2883 if( rc==2 ){ /* exit requested */
2884 break;
@@ -2885,39 +2884,36 @@
2885 }else if( rc ){
2886 errCnt++;
2887 }
2888 continue;
2889 }
2890 if( _is_command_terminator(zLine) && _is_complete(zSql, nSql) ){
2891 memcpy(zLine,";",2);
 
 
 
 
 
 
 
 
 
2892 }
2893 nSqlPrior = nSql;
2894 if( zSql==0 ){
2895 int i;
2896 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2897 if( zLine[i]!=0 ){
2898 nSql = strlen30(zLine);
2899 zSql = malloc( nSql+3 );
2900 if( zSql==0 ){
2901 fprintf(stderr, "Error: out of memory\n");
2902 exit(1);
2903 }
2904 memcpy(zSql, zLine, nSql+1);
2905 startline = lineno;
2906 }
2907 }else{
2908 int len = strlen30(zLine);
2909 zSql = realloc( zSql, nSql + len + 4 );
2910 if( zSql==0 ){
2911 fprintf(stderr,"Error: out of memory\n");
2912 exit(1);
2913 }
2914 zSql[nSql++] = '\n';
2915 memcpy(&zSql[nSql], zLine, len+1);
2916 nSql += len;
2917 }
2918 if( zSql && _contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
2919 && sqlite3_complete(zSql) ){
2920 p->cnt = 0;
2921 open_db(p);
2922 BEGIN_TIMER;
2923 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
@@ -2937,20 +2933,16 @@
2937 }else{
2938 fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
2939 }
2940 errCnt++;
2941 }
2942 free(zSql);
2943 zSql = 0;
2944 nSql = 0;
2945 }else if( zSql && _all_whitespace(zSql) ){
2946 free(zSql);
2947 zSql = 0;
2948 nSql = 0;
2949 }
2950 }
2951 if( zSql ){
2952 if( !_all_whitespace(zSql) ){
2953 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
2954 }
2955 free(zSql);
2956 }
@@ -3194,11 +3186,10 @@
3194 ** we do the actual processing of arguments later in a second pass.
3195 */
3196 stdin_is_interactive = 0;
3197 }else if( strcmp(z,"-heap")==0 ){
3198 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3199 int j, c;
3200 const char *zSize;
3201 sqlite3_int64 szHeap;
3202
3203 zSize = cmdline_option_value(argc, argv, ++i);
3204 szHeap = integerValue(zSize);
3205
--- src/shell.c
+++ src/shell.c
@@ -51,11 +51,10 @@
51 #if defined(HAVE_READLINE) && HAVE_READLINE==1
52 # include <readline/readline.h>
53 # include <readline/history.h>
54 #endif
55 #if !defined(HAVE_EDITLINE) && (!defined(HAVE_READLINE) || HAVE_READLINE!=1)
 
56 # define add_history(X)
57 # define read_history(X)
58 # define write_history(X)
59 # define stifle_history(X)
60 #endif
@@ -335,27 +334,17 @@
334 ** This routine reads a line of text from FILE in, stores
335 ** the text in memory obtained from malloc() and returns a pointer
336 ** to the text. NULL is returned at end of file, or if malloc()
337 ** fails.
338 **
339 ** If zLine is not NULL then it is a malloced buffer returned from
340 ** a previous call to this routine that may be reused.
341 */
342 static char *local_getline(char *zLine, FILE *in){
343 int nLine = zLine==0 ? 0 : 100;
344 int n = 0;
345
 
 
 
 
 
 
 
 
 
 
346 while( 1 ){
347 if( n+100>nLine ){
348 nLine = nLine*2 + 100;
349 zLine = realloc(zLine, nLine);
350 if( zLine==0 ) return 0;
@@ -366,46 +355,52 @@
355 return 0;
356 }
357 zLine[n] = 0;
358 break;
359 }
360 while( zLine[n] ) n++;
361 if( n>0 && zLine[n-1]=='\n' ){
 
 
 
362 n--;
363 if( n>0 && zLine[n-1]=='\r' ) n--;
364 zLine[n] = 0;
365 break;
366 }
367 }
 
368 return zLine;
369 }
370
371 /*
372 ** Retrieve a single line of input text.
373 **
374 ** If in==0 then read from standard input and prompt before each line.
375 ** If isContinuation is true, then a continuation prompt is appropriate.
376 ** If isContinuation is zero, then the main prompt should be used.
377 **
378 ** If zPrior is not NULL then it is a buffer from a prior call to this
379 ** routine that can be reused.
380 **
381 ** The result is stored in space obtained from malloc() and must either
382 ** be freed by the caller or else passed back into this routine via the
383 ** zPrior argument for reuse.
384 */
385 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
386 char *zPrompt;
387 char *zResult;
388 if( in!=0 ){
389 zResult = local_getline(zPrior, in);
 
 
 
390 }else{
391 zPrompt = isContinuation ? continuePrompt : mainPrompt;
 
 
392 #if defined(HAVE_READLINE) && HAVE_READLINE==1
393 free(zPrior);
394 zResult = readline(zPrompt);
395 if( zResult && *zResult ) add_history(zResult);
396 #else
397 printf("%s", zPrompt);
398 fflush(stdout);
399 zResult = local_getline(zPrior, stdin);
400 #endif
401 }
402 return zResult;
403 }
404
405 struct previous_mode_data {
406 int valid; /* Is there legit data in here? */
@@ -1720,11 +1715,11 @@
1715 continue;
1716 }
1717 }
1718 if( (c==cSep && pc==cQuote)
1719 || (c=='\n' && pc==cQuote)
1720 || (c=='\n' && pc=='\r' && p->n>=2 && p->z[p->n-2]==cQuote)
1721 || (c==EOF && pc==cQuote)
1722 ){
1723 do{ p->n--; }while( p->z[p->n]!=cQuote );
1724 p->cTerm = c;
1725 break;
@@ -1993,10 +1988,11 @@
1988 char *zFile = azArg[1]; /* Name of file to extra content from */
1989 sqlite3_stmt *pStmt = NULL; /* A statement */
1990 int nCol; /* Number of columns in the table */
1991 int nByte; /* Number of bytes in an SQL string */
1992 int i, j; /* Loop counters */
1993 int needCommit; /* True to COMMIT or ROLLBACK at end */
1994 int nSep; /* Number of bytes in p->separator[] */
1995 char *zSql; /* An SQL statement */
1996 CSVReader sCsv; /* Reader context */
1997 int (*xCloser)(FILE*); /* Procedure to close th3 connection */
1998
@@ -2094,10 +2090,12 @@
2090 fprintf(stderr, "Error: %s\n", sqlite3_errmsg(db));
2091 if (pStmt) sqlite3_finalize(pStmt);
2092 xCloser(sCsv.in);
2093 return 1;
2094 }
2095 needCommit = sqlite3_get_autocommit(db);
2096 if( needCommit ) sqlite3_exec(db, "BEGIN", 0, 0, 0);
2097 do{
2098 int startLine = sCsv.nLine;
2099 for(i=0; i<nCol; i++){
2100 char *z = csv_read_one_field(&sCsv);
2101 if( z==0 && i==0 ) break;
@@ -2130,11 +2128,11 @@
2128 }while( sCsv.cTerm!=EOF );
2129
2130 xCloser(sCsv.in);
2131 sqlite3_free(sCsv.z);
2132 sqlite3_finalize(pStmt);
2133 if( needCommit ) sqlite3_exec(db, "COMMIT", 0, 0, 0);
2134 }else
2135
2136 if( c=='i' && strncmp(azArg[0], "indices", n)==0 && nArg<3 ){
2137 struct callback_data data;
2138 char *zErrMsg = 0;
@@ -2779,11 +2777,11 @@
2777
2778 /*
2779 ** Return TRUE if a semicolon occurs anywhere in the first N characters
2780 ** of string z[].
2781 */
2782 static int line_contains_semicolon(const char *z, int N){
2783 int i;
2784 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
2785 return 0;
2786 }
2787
@@ -2814,11 +2812,11 @@
2812 /*
2813 ** Return TRUE if the line typed in is an SQL command terminator other
2814 ** than a semi-colon. The SQL Server style "go" command is understood
2815 ** as is the Oracle "/".
2816 */
2817 static int line_is_command_terminator(const char *zLine){
2818 while( IsSpace(zLine[0]) ){ zLine++; };
2819 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2820 return 1; /* Oracle */
2821 }
2822 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
@@ -2830,11 +2828,11 @@
2828
2829 /*
2830 ** Return true if zSql is a complete SQL statement. Return false if it
2831 ** ends in the middle of a string literal or C-style comment.
2832 */
2833 static int line_is_complete(char *zSql, int nSql){
2834 int rc;
2835 if( zSql==0 ) return 1;
2836 zSql[nSql] = ';';
2837 zSql[nSql+1] = 0;
2838 rc = sqlite3_complete(zSql);
@@ -2850,24 +2848,25 @@
2848 ** cause this routine to exit immediately, unless input is interactive.
2849 **
2850 ** Return the number of errors.
2851 */
2852 static int process_input(struct callback_data *p, FILE *in){
2853 char *zLine = 0; /* A single input line */
2854 char *zSql = 0; /* Accumulated SQL text */
2855 int nLine; /* Length of current line */
2856 int nSql = 0; /* Bytes of zSql[] used */
2857 int nAlloc = 0; /* Allocated zSql[] space */
2858 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
2859 char *zErrMsg; /* Error message returned */
2860 int rc; /* Error code */
2861 int errCnt = 0; /* Number of errors seen */
2862 int lineno = 0; /* Current line number */
2863 int startline = 0; /* Line number for start of current input */
2864
2865 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
2866 fflush(p->out);
2867 zLine = one_input_line(in, zLine, nSql>0);
 
2868 if( zLine==0 ){
2869 /* End of input */
2870 if( stdin_is_interactive ) printf("\n");
2871 break;
2872 }
@@ -2874,11 +2873,11 @@
2873 if( seenInterrupt ){
2874 if( in!=0 ) break;
2875 seenInterrupt = 0;
2876 }
2877 lineno++;
2878 if( nSql==0 && _all_whitespace(zLine) ) continue;
2879 if( zLine && zLine[0]=='.' && nSql==0 ){
2880 if( p->echoOn ) printf("%s\n", zLine);
2881 rc = do_meta_command(zLine, p);
2882 if( rc==2 ){ /* exit requested */
2883 break;
@@ -2885,39 +2884,36 @@
2884 }else if( rc ){
2885 errCnt++;
2886 }
2887 continue;
2888 }
2889 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
2890 memcpy(zLine,";",2);
2891 }
2892 nLine = strlen30(zLine);
2893 if( nSql+nLine+2>=nAlloc ){
2894 nAlloc = nSql+nLine+100;
2895 zSql = realloc(zSql, nAlloc);
2896 if( zSql==0 ){
2897 fprintf(stderr, "Error: out of memory\n");
2898 exit(1);
2899 }
2900 }
2901 nSqlPrior = nSql;
2902 if( nSql==0 ){
2903 int i;
2904 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2905 assert( nAlloc>0 && zSql!=0 );
2906 memcpy(zSql, zLine+i, nLine+1-i);
2907 startline = lineno;
2908 nSql = nLine-i;
 
 
 
 
 
 
2909 }else{
 
 
 
 
 
 
2910 zSql[nSql++] = '\n';
2911 memcpy(zSql+nSql, zLine, nLine+1);
2912 nSql += nLine;
2913 }
2914 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
2915 && sqlite3_complete(zSql) ){
2916 p->cnt = 0;
2917 open_db(p);
2918 BEGIN_TIMER;
2919 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
@@ -2937,20 +2933,16 @@
2933 }else{
2934 fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
2935 }
2936 errCnt++;
2937 }
 
 
2938 nSql = 0;
2939 }else if( nSql && _all_whitespace(zSql) ){
 
 
2940 nSql = 0;
2941 }
2942 }
2943 if( nSql ){
2944 if( !_all_whitespace(zSql) ){
2945 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
2946 }
2947 free(zSql);
2948 }
@@ -3194,11 +3186,10 @@
3186 ** we do the actual processing of arguments later in a second pass.
3187 */
3188 stdin_is_interactive = 0;
3189 }else if( strcmp(z,"-heap")==0 ){
3190 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
 
3191 const char *zSize;
3192 sqlite3_int64 szHeap;
3193
3194 zSize = cmdline_option_value(argc, argv, ++i);
3195 szHeap = integerValue(zSize);
3196
+2350 -1418
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.8.0. By combining all the individual C code files into this
3
+** version 3.8.1. By combining all the individual C code files into this
44
** single large file, the entire code can be compiled as a single translation
55
** unit. This allows many compilers to do optimizations that would not be
66
** possible if the files were compiled separately. Performance improvements
77
** of 5% or more are commonly seen when SQLite is compiled as a single
88
** translation unit.
@@ -654,13 +654,13 @@
654654
**
655655
** See also: [sqlite3_libversion()],
656656
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657657
** [sqlite_version()] and [sqlite_source_id()].
658658
*/
659
-#define SQLITE_VERSION "3.8.0"
660
-#define SQLITE_VERSION_NUMBER 3008000
661
-#define SQLITE_SOURCE_ID "2013-08-15 22:40:21 f2d175f975cd0be63425424ec322a98fb650019e"
659
+#define SQLITE_VERSION "3.8.1"
660
+#define SQLITE_VERSION_NUMBER 3008001
661
+#define SQLITE_SOURCE_ID "2013-08-30 06:20:23 d9c018f8155ab48df8e0e02519bba50588fe49fc"
662662
663663
/*
664664
** CAPI3REF: Run-Time Library Version Numbers
665665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
666666
**
@@ -8368,10 +8368,24 @@
83688368
#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
83698369
# undef SQLITE_DEFAULT_MMAP_SIZE
83708370
# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
83718371
#endif
83728372
8373
+/*
8374
+** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8375
+** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
8376
+** define SQLITE_ENABLE_STAT3_OR_STAT4
8377
+*/
8378
+#ifdef SQLITE_ENABLE_STAT4
8379
+# undef SQLITE_ENABLE_STAT3
8380
+# define SQLITE_ENABLE_STAT3_OR_STAT4 1
8381
+#elif SQLITE_ENABLE_STAT3
8382
+# define SQLITE_ENABLE_STAT3_OR_STAT4 1
8383
+#elif SQLITE_ENABLE_STAT3_OR_STAT4
8384
+# undef SQLITE_ENABLE_STAT3_OR_STAT4
8385
+#endif
8386
+
83738387
/*
83748388
** An instance of the following structure is used to store the busy-handler
83758389
** callback for a given sqlite handle.
83768390
**
83778391
** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8587,11 +8601,11 @@
85878601
#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
85888602
85898603
SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
85908604
SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
85918605
SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8592
-SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
8606
+SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
85938607
SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
85948608
SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
85958609
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
85968610
SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
85978611
SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
@@ -9288,12 +9302,24 @@
92889302
#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
92899303
92909304
/*
92919305
** Flags that make up the mask passed to sqlite3PagerAcquire().
92929306
*/
9293
-#define PAGER_ACQUIRE_NOCONTENT 0x01 /* Do not load data from disk */
9294
-#define PAGER_ACQUIRE_READONLY 0x02 /* Read-only page is acceptable */
9307
+#define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
9308
+#define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
9309
+
9310
+/*
9311
+** Flags for sqlite3PagerSetFlags()
9312
+*/
9313
+#define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
9314
+#define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
9315
+#define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
9316
+#define PAGER_SYNCHRONOUS_MASK 0x03 /* Mask for three values above */
9317
+#define PAGER_FULLFSYNC 0x04 /* PRAGMA fullfsync=ON */
9318
+#define PAGER_CKPT_FULLFSYNC 0x08 /* PRAGMA checkpoint_fullfsync=ON */
9319
+#define PAGER_CACHESPILL 0x10 /* PRAGMA cache_spill=ON */
9320
+#define PAGER_FLAGS_MASK 0x1c /* All above except SYNCHRONOUS */
92959321
92969322
/*
92979323
** The remainder of this file contains the declarations of the functions
92989324
** that make up the Pager sub-system API. See source code comments for
92999325
** a detailed description of each routine.
@@ -9317,11 +9343,11 @@
93179343
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
93189344
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
93199345
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
93209346
SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
93219347
SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9322
-SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
9348
+SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
93239349
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
93249350
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
93259351
SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
93269352
SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
93279353
SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
@@ -10180,36 +10206,37 @@
1018010206
/*
1018110207
** Possible values for the sqlite3.flags.
1018210208
*/
1018310209
#define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
1018410210
#define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
10185
-#define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
10186
-#define SQLITE_ShortColNames 0x00000008 /* Show short columns names */
10187
-#define SQLITE_CountRows 0x00000010 /* Count rows changed by INSERT, */
10211
+#define SQLITE_FullFSync 0x00000004 /* Use full fsync on the backend */
10212
+#define SQLITE_CkptFullFSync 0x00000008 /* Use full fsync for checkpoint */
10213
+#define SQLITE_CacheSpill 0x00000010 /* OK to spill pager cache */
10214
+#define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
10215
+#define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
10216
+#define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
1018810217
/* DELETE, or UPDATE and return */
1018910218
/* the count using a callback. */
10190
-#define SQLITE_NullCallback 0x00000020 /* Invoke the callback once if the */
10219
+#define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
1019110220
/* result set is empty */
10192
-#define SQLITE_SqlTrace 0x00000040 /* Debug print SQL as it executes */
10193
-#define SQLITE_VdbeListing 0x00000080 /* Debug listings of VDBE programs */
10194
-#define SQLITE_WriteSchema 0x00000100 /* OK to update SQLITE_MASTER */
10195
-#define SQLITE_VdbeAddopTrace 0x00000200 /* Trace sqlite3VdbeAddOp() calls */
10196
-#define SQLITE_IgnoreChecks 0x00000400 /* Do not enforce check constraints */
10197
-#define SQLITE_ReadUncommitted 0x0000800 /* For shared-cache mode */
10198
-#define SQLITE_LegacyFileFmt 0x00001000 /* Create new databases in format 1 */
10199
-#define SQLITE_FullFSync 0x00002000 /* Use full fsync on the backend */
10200
-#define SQLITE_CkptFullFSync 0x00004000 /* Use full fsync for checkpoint */
10201
-#define SQLITE_RecoveryMode 0x00008000 /* Ignore schema errors */
10202
-#define SQLITE_ReverseOrder 0x00010000 /* Reverse unordered SELECTs */
10203
-#define SQLITE_RecTriggers 0x00020000 /* Enable recursive triggers */
10204
-#define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */
10205
-#define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */
10206
-#define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */
10207
-#define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */
10208
-#define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */
10209
-#define SQLITE_DeferFKs 0x00800000 /* Defer all FK constraints */
10210
-#define SQLITE_QueryOnly 0x01000000 /* Disable database changes */
10221
+#define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */
10222
+#define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */
10223
+#define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */
10224
+#define SQLITE_VdbeAddopTrace 0x00001000 /* Trace sqlite3VdbeAddOp() calls */
10225
+#define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */
10226
+#define SQLITE_ReadUncommitted 0x0004000 /* For shared-cache mode */
10227
+#define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */
10228
+#define SQLITE_RecoveryMode 0x00010000 /* Ignore schema errors */
10229
+#define SQLITE_ReverseOrder 0x00020000 /* Reverse unordered SELECTs */
10230
+#define SQLITE_RecTriggers 0x00040000 /* Enable recursive triggers */
10231
+#define SQLITE_ForeignKeys 0x00080000 /* Enforce foreign key constraints */
10232
+#define SQLITE_AutoIndex 0x00100000 /* Enable automatic indexes */
10233
+#define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */
10234
+#define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */
10235
+#define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */
10236
+#define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
10237
+#define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
1021110238
1021210239
1021310240
/*
1021410241
** Bits of the sqlite3.dbOptFlags field that are used by the
1021510242
** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -10757,13 +10784,14 @@
1075710784
u16 nColumn; /* Number of columns in table used by this index */
1075810785
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1075910786
unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
1076010787
unsigned bUnordered:1; /* Use this index for == or IN queries only */
1076110788
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10762
-#ifdef SQLITE_ENABLE_STAT3
10789
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1076310790
int nSample; /* Number of elements in aSample[] */
10764
- tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10791
+ int nSampleCol; /* Size of IndexSample.anEq[] and so on */
10792
+ tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
1076510793
IndexSample *aSample; /* Samples of the left-most key */
1076610794
#endif
1076710795
};
1076810796
1076910797
/*
@@ -10770,20 +10798,15 @@
1077010798
** Each sample stored in the sqlite_stat3 table is represented in memory
1077110799
** using a structure of this type. See documentation at the top of the
1077210800
** analyze.c source file for additional information.
1077310801
*/
1077410802
struct IndexSample {
10775
- union {
10776
- char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10777
- double r; /* Value if eType is SQLITE_FLOAT */
10778
- i64 i; /* Value if eType is SQLITE_INTEGER */
10779
- } u;
10780
- u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10781
- int nByte; /* Size in byte of text or blob. */
10782
- tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10783
- tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10784
- tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10803
+ void *p; /* Pointer to sampled record */
10804
+ int n; /* Size of record in bytes */
10805
+ tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
10806
+ tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
10807
+ tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
1078510808
};
1078610809
1078710810
/*
1078810811
** Each token coming out of the lexer is an instance of
1078910812
** this structure. Tokens are also used as part of an expression.
@@ -11632,14 +11655,15 @@
1163211655
char *zBase; /* A base allocation. Not from malloc. */
1163311656
char *zText; /* The string collected so far */
1163411657
int nChar; /* Length of the string so far */
1163511658
int nAlloc; /* Amount of space allocated in zText */
1163611659
int mxAlloc; /* Maximum allowed string length */
11637
- u8 mallocFailed; /* Becomes true if any memory allocation fails */
1163811660
u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */
11639
- u8 tooBig; /* Becomes true if string size exceeds limits */
11661
+ u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
1164011662
};
11663
+#define STRACCUM_NOMEM 1
11664
+#define STRACCUM_TOOBIG 2
1164111665
1164211666
/*
1164311667
** A pointer to this structure is used to communicate information
1164411668
** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
1164511669
*/
@@ -12250,13 +12274,10 @@
1225012274
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1225112275
void(*)(void*));
1225212276
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1225312277
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1225412278
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12255
-#ifdef SQLITE_ENABLE_STAT3
12256
-SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
12257
-#endif
1225812279
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1225912280
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1226012281
#ifndef SQLITE_AMALGAMATION
1226112282
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
1226212283
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12319,10 +12340,16 @@
1231912340
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
1232012341
1232112342
SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
1232212343
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
1232312344
12345
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12346
+SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12347
+SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12348
+SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12349
+#endif
12350
+
1232412351
/*
1232512352
** The interface to the LEMON-generated parser
1232612353
*/
1232712354
SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
1232812355
SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
@@ -12360,17 +12387,18 @@
1236012387
# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
1236112388
# define sqlite3GetVTable(X,Y) ((VTable*)0)
1236212389
#else
1236312390
SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
1236412391
SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
12365
-SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
12392
+SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
1236612393
SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
1236712394
SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
1236812395
SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
1236912396
SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
1237012397
SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
1237112398
SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
12399
+SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
1237212400
SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
1237312401
# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
1237412402
#endif
1237512403
SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
1237612404
SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
@@ -12901,11 +12929,13 @@
1290112929
"ENABLE_OVERSIZE_CELL_CHECK",
1290212930
#endif
1290312931
#ifdef SQLITE_ENABLE_RTREE
1290412932
"ENABLE_RTREE",
1290512933
#endif
12906
-#ifdef SQLITE_ENABLE_STAT3
12934
+#if defined(SQLITE_ENABLE_STAT4)
12935
+ "ENABLE_STAT4",
12936
+#elif defined(SQLITE_ENABLE_STAT3)
1290712937
"ENABLE_STAT3",
1290812938
#endif
1290912939
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1291012940
"ENABLE_UNLOCK_NOTIFY",
1291112941
#endif
@@ -13473,14 +13503,15 @@
1347313503
struct sqlite3_context {
1347413504
FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
1347513505
Mem s; /* The return value is stored here */
1347613506
Mem *pMem; /* Memory cell used to store aggregate context */
1347713507
CollSeq *pColl; /* Collating sequence */
13478
- int isError; /* Error code returned by the function. */
13479
- int skipFlag; /* Skip skip accumulator loading if true */
13508
+ Vdbe *pVdbe; /* The VM that owns this context */
1348013509
int iOp; /* Instruction number of OP_Function */
13481
- Vdbe *pVdbe; /* The VM that owns this context */
13510
+ int isError; /* Error code returned by the function. */
13511
+ u8 skipFlag; /* Skip skip accumulator loading if true */
13512
+ u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
1348213513
};
1348313514
1348413515
/*
1348513516
** An Explain object accumulates indented output which is helpful
1348613517
** in describing recursive data structures.
@@ -13552,11 +13583,11 @@
1355213583
bft doingRerun:1; /* True if rerunning after an auto-reprepare */
1355313584
int nChange; /* Number of db changes made since last reset */
1355413585
yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
1355513586
yDbMask lockMask; /* Subset of btreeMask that requires a lock */
1355613587
int iStatement; /* Statement number (or 0 if has not opened stmt) */
13557
- int aCounter[4]; /* Counters used by sqlite3_stmt_status() */
13588
+ u32 aCounter[5]; /* Counters used by sqlite3_stmt_status() */
1355813589
#ifndef SQLITE_OMIT_TRACE
1355913590
i64 startTime; /* Time when query started - used for profiling */
1356013591
#endif
1356113592
i64 nFkConstraint; /* Number of imm. FK constraints this VM */
1356213593
i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -16059,11 +16090,11 @@
1605916090
struct MemBlockHdr *pHdr;
1606016091
if( !p ){
1606116092
return 0;
1606216093
}
1606316094
pHdr = sqlite3MemsysGetHeader(p);
16064
- return pHdr->iSize;
16095
+ return (int)pHdr->iSize;
1606516096
}
1606616097
1606716098
/*
1606816099
** Initialize the memory allocation subsystem.
1606916100
*/
@@ -16101,19 +16132,19 @@
1610116132
static void randomFill(char *pBuf, int nByte){
1610216133
unsigned int x, y, r;
1610316134
x = SQLITE_PTR_TO_INT(pBuf);
1610416135
y = nByte | 1;
1610516136
while( nByte >= 4 ){
16106
- x = (x>>1) ^ (-(x&1) & 0xd0000001);
16137
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
1610716138
y = y*1103515245 + 12345;
1610816139
r = x ^ y;
1610916140
*(int*)pBuf = r;
1611016141
pBuf += 4;
1611116142
nByte -= 4;
1611216143
}
1611316144
while( nByte-- > 0 ){
16114
- x = (x>>1) ^ (-(x&1) & 0xd0000001);
16145
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
1611516146
y = y*1103515245 + 12345;
1611616147
r = x ^ y;
1611716148
*(pBuf++) = r & 0xff;
1611816149
}
1611916150
}
@@ -16204,13 +16235,13 @@
1620416235
assert( mem.pLast==pHdr );
1620516236
mem.pLast = pHdr->pPrev;
1620616237
}
1620716238
z = (char*)pBt;
1620816239
z -= pHdr->nTitle;
16209
- adjustStats(pHdr->iSize, -1);
16240
+ adjustStats((int)pHdr->iSize, -1);
1621016241
randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16211
- pHdr->iSize + sizeof(int) + pHdr->nTitle);
16242
+ (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
1621216243
free(z);
1621316244
sqlite3_mutex_leave(mem.mutex);
1621416245
}
1621516246
1621616247
/*
@@ -16230,11 +16261,11 @@
1623016261
pOldHdr = sqlite3MemsysGetHeader(pPrior);
1623116262
pNew = sqlite3MemMalloc(nByte);
1623216263
if( pNew ){
1623316264
memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
1623416265
if( nByte>pOldHdr->iSize ){
16235
- randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
16266
+ randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
1623616267
}
1623716268
sqlite3MemFree(pPrior);
1623816269
}
1623916270
return pNew;
1624016271
}
@@ -16345,11 +16376,11 @@
1634516376
SQLITE_PRIVATE void sqlite3MemdebugSync(){
1634616377
struct MemBlockHdr *pHdr;
1634716378
for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
1634816379
void **pBt = (void**)pHdr;
1634916380
pBt -= pHdr->nBacktraceSlots;
16350
- mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16381
+ mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
1635116382
}
1635216383
}
1635316384
1635416385
/*
1635516386
** Open the file indicated and write a log of all unfreed memory
@@ -18467,11 +18498,11 @@
1846718498
GetVersionEx(&sInfo);
1846818499
osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
1846918500
}
1847018501
return osType==2;
1847118502
}
18472
-#endif /* SQLITE_OS_WINCE */
18503
+#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
1847318504
#endif
1847418505
1847518506
#ifdef SQLITE_DEBUG
1847618507
/*
1847718508
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18505,11 +18536,11 @@
1850518536
/* As winMutexInit() and winMutexEnd() are called as part
1850618537
** of the sqlite3_initialize and sqlite3_shutdown()
1850718538
** processing, the "interlocked" magic is probably not
1850818539
** strictly necessary.
1850918540
*/
18510
-static long winMutex_lock = 0;
18541
+static LONG winMutex_lock = 0;
1851118542
1851218543
SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
1851318544
1851418545
static int winMutexInit(void){
1851518546
/* The first to increment to 1 does actual initialization */
@@ -19885,11 +19916,11 @@
1988519916
zOut = buf;
1988619917
}else{
1988719918
nOut = precision + 10;
1988819919
zOut = zExtra = sqlite3Malloc( nOut );
1988919920
if( zOut==0 ){
19890
- pAccum->mallocFailed = 1;
19921
+ pAccum->accError = STRACCUM_NOMEM;
1989119922
return;
1989219923
}
1989319924
}
1989419925
bufpt = &zOut[nOut-1];
1989519926
if( xtype==etORDINAL ){
@@ -19997,11 +20028,11 @@
1999720028
e2 = exp;
1999820029
}
1999920030
if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
2000020031
bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
2000120032
if( bufpt==0 ){
20002
- pAccum->mallocFailed = 1;
20033
+ pAccum->accError = STRACCUM_NOMEM;
2000320034
return;
2000420035
}
2000520036
}
2000620037
zOut = bufpt;
2000720038
nsd = 16 + flag_altform2*10;
@@ -20132,11 +20163,11 @@
2013220163
needQuote = !isnull && xtype==etSQLESCAPE2;
2013320164
n += i + 1 + needQuote*2;
2013420165
if( n>etBUFSIZE ){
2013520166
bufpt = zExtra = sqlite3Malloc( n );
2013620167
if( bufpt==0 ){
20137
- pAccum->mallocFailed = 1;
20168
+ pAccum->accError = STRACCUM_NOMEM;
2013820169
return;
2013920170
}
2014020171
}else{
2014120172
bufpt = buf;
2014220173
}
@@ -20210,26 +20241,24 @@
2021020241
/*
2021120242
** Append N bytes of text from z to the StrAccum object.
2021220243
*/
2021320244
SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
2021420245
assert( z!=0 || N==0 );
20215
- if( p->tooBig | p->mallocFailed ){
20216
- testcase(p->tooBig);
20217
- testcase(p->mallocFailed);
20246
+ if( p->accError ){
20247
+ testcase(p->accError==STRACCUM_TOOBIG);
20248
+ testcase(p->accError==STRACCUM_NOMEM);
2021820249
return;
2021920250
}
2022020251
assert( p->zText!=0 || p->nChar==0 );
20221
- if( N<0 ){
20252
+ if( N<=0 ){
20253
+ if( N==0 || z[0]==0 ) return;
2022220254
N = sqlite3Strlen30(z);
2022320255
}
20224
- if( N==0 || NEVER(z==0) ){
20225
- return;
20226
- }
2022720256
if( p->nChar+N >= p->nAlloc ){
2022820257
char *zNew;
2022920258
if( !p->useMalloc ){
20230
- p->tooBig = 1;
20259
+ p->accError = STRACCUM_TOOBIG;
2023120260
N = p->nAlloc - p->nChar - 1;
2023220261
if( N<=0 ){
2023320262
return;
2023420263
}
2023520264
}else{
@@ -20236,11 +20265,11 @@
2023620265
char *zOld = (p->zText==p->zBase ? 0 : p->zText);
2023720266
i64 szNew = p->nChar;
2023820267
szNew += N + 1;
2023920268
if( szNew > p->mxAlloc ){
2024020269
sqlite3StrAccumReset(p);
20241
- p->tooBig = 1;
20270
+ p->accError = STRACCUM_TOOBIG;
2024220271
return;
2024320272
}else{
2024420273
p->nAlloc = (int)szNew;
2024520274
}
2024620275
if( p->useMalloc==1 ){
@@ -20250,11 +20279,11 @@
2025020279
}
2025120280
if( zNew ){
2025220281
if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
2025320282
p->zText = zNew;
2025420283
}else{
20255
- p->mallocFailed = 1;
20284
+ p->accError = STRACCUM_NOMEM;
2025620285
sqlite3StrAccumReset(p);
2025720286
return;
2025820287
}
2025920288
}
2026020289
}
@@ -20278,11 +20307,11 @@
2027820307
p->zText = sqlite3_malloc(p->nChar+1);
2027920308
}
2028020309
if( p->zText ){
2028120310
memcpy(p->zText, p->zBase, p->nChar+1);
2028220311
}else{
20283
- p->mallocFailed = 1;
20312
+ p->accError = STRACCUM_NOMEM;
2028420313
}
2028520314
}
2028620315
}
2028720316
return p->zText;
2028820317
}
@@ -20309,12 +20338,11 @@
2030920338
p->db = 0;
2031020339
p->nChar = 0;
2031120340
p->nAlloc = n;
2031220341
p->mxAlloc = mx;
2031320342
p->useMalloc = 1;
20314
- p->tooBig = 0;
20315
- p->mallocFailed = 0;
20343
+ p->accError = 0;
2031620344
}
2031720345
2031820346
/*
2031920347
** Print into memory obtained from sqliteMalloc(). Use the internal
2032020348
** %-conversion extensions.
@@ -20327,11 +20355,11 @@
2032720355
sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
2032820356
db->aLimit[SQLITE_LIMIT_LENGTH]);
2032920357
acc.db = db;
2033020358
sqlite3VXPrintf(&acc, 1, zFormat, ap);
2033120359
z = sqlite3StrAccumFinish(&acc);
20332
- if( acc.mallocFailed ){
20360
+ if( acc.accError==STRACCUM_NOMEM ){
2033320361
db->mallocFailed = 1;
2033420362
}
2033520363
return z;
2033620364
}
2033720365
@@ -20524,28 +20552,15 @@
2052420552
unsigned char i, j; /* State variables */
2052520553
unsigned char s[256]; /* State variables */
2052620554
} sqlite3Prng;
2052720555
2052820556
/*
20529
-** Get a single 8-bit random value from the RC4 PRNG. The Mutex
20530
-** must be held while executing this routine.
20531
-**
20532
-** Why not just use a library random generator like lrand48() for this?
20533
-** Because the OP_NewRowid opcode in the VDBE depends on having a very
20534
-** good source of random numbers. The lrand48() library function may
20535
-** well be good enough. But maybe not. Or maybe lrand48() has some
20536
-** subtle problems on some systems that could cause problems. It is hard
20537
-** to know. To minimize the risk of problems due to bad lrand48()
20538
-** implementations, SQLite uses this random number generator based
20539
-** on RC4, which we know works very well.
20540
-**
20541
-** (Later): Actually, OP_NewRowid does not depend on a good source of
20542
-** randomness any more. But we will leave this code in all the same.
20557
+** Return N random bytes.
2054320558
*/
20544
-static u8 randomByte(void){
20559
+SQLITE_API void sqlite3_randomness(int N, void *pBuf){
2054520560
unsigned char t;
20546
-
20561
+ unsigned char *zBuf = pBuf;
2054720562
2054820563
/* The "wsdPrng" macro will resolve to the pseudo-random number generator
2054920564
** state vector. If writable static data is unsupported on the target,
2055020565
** we have to locate the state vector at run-time. In the more common
2055120566
** case where writable static data is supported, wsdPrng can refer directly
@@ -20556,10 +20571,14 @@
2055620571
# define wsdPrng p[0]
2055720572
#else
2055820573
# define wsdPrng sqlite3Prng
2055920574
#endif
2056020575
20576
+#if SQLITE_THREADSAFE
20577
+ sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
20578
+ sqlite3_mutex_enter(mutex);
20579
+#endif
2056120580
2056220581
/* Initialize the state of the random number generator once,
2056320582
** the first time this routine is called. The seed value does
2056420583
** not need to contain a lot of randomness since we are not
2056520584
** trying to do secure encryption or anything like that...
@@ -20584,32 +20603,18 @@
2058420603
wsdPrng.s[i] = t;
2058520604
}
2058620605
wsdPrng.isInit = 1;
2058720606
}
2058820607
20589
- /* Generate and return single random byte
20590
- */
20591
- wsdPrng.i++;
20592
- t = wsdPrng.s[wsdPrng.i];
20593
- wsdPrng.j += t;
20594
- wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
20595
- wsdPrng.s[wsdPrng.j] = t;
20596
- t += wsdPrng.s[wsdPrng.i];
20597
- return wsdPrng.s[t];
20598
-}
20599
-
20600
-/*
20601
-** Return N random bytes.
20602
-*/
20603
-SQLITE_API void sqlite3_randomness(int N, void *pBuf){
20604
- unsigned char *zBuf = pBuf;
20605
-#if SQLITE_THREADSAFE
20606
- sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
20607
-#endif
20608
- sqlite3_mutex_enter(mutex);
2060920608
while( N-- ){
20610
- *(zBuf++) = randomByte();
20609
+ wsdPrng.i++;
20610
+ t = wsdPrng.s[wsdPrng.i];
20611
+ wsdPrng.j += t;
20612
+ wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
20613
+ wsdPrng.s[wsdPrng.j] = t;
20614
+ t += wsdPrng.s[wsdPrng.i];
20615
+ *(zBuf++) = wsdPrng.s[t];
2061120616
}
2061220617
sqlite3_mutex_leave(mutex);
2061320618
}
2061420619
2061520620
#ifndef SQLITE_OMIT_BUILTIN_TEST
@@ -21092,36 +21097,10 @@
2109221097
assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
2109321098
assert( m.z || db->mallocFailed );
2109421099
return m.z;
2109521100
}
2109621101
21097
-/*
21098
-** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
21099
-** enc. A pointer to the new string is returned, and the value of *pnOut
21100
-** is set to the length of the returned string in bytes. The call should
21101
-** arrange to call sqlite3DbFree() on the returned pointer when it is
21102
-** no longer required.
21103
-**
21104
-** If a malloc failure occurs, NULL is returned and the db.mallocFailed
21105
-** flag set.
21106
-*/
21107
-#ifdef SQLITE_ENABLE_STAT3
21108
-SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
21109
- Mem m;
21110
- memset(&m, 0, sizeof(m));
21111
- m.db = db;
21112
- sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
21113
- if( sqlite3VdbeMemTranslate(&m, enc) ){
21114
- assert( db->mallocFailed );
21115
- return 0;
21116
- }
21117
- assert( m.z==m.zMalloc );
21118
- *pnOut = m.n;
21119
- return m.z;
21120
-}
21121
-#endif
21122
-
2112321102
/*
2112421103
** zIn is a UTF-16 encoded unicode string at least nChar characters long.
2112521104
** Return the number of bytes in the first nChar unicode characters
2112621105
** in pZ. nChar must be non-negative.
2112721106
*/
@@ -23074,15 +23053,17 @@
2307423053
void *lockingContext; /* Locking style specific state */
2307523054
UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
2307623055
const char *zPath; /* Name of the file */
2307723056
unixShm *pShm; /* Shared memory segment information */
2307823057
int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23058
+#if SQLITE_MAX_MMAP_SIZE>0
2307923059
int nFetchOut; /* Number of outstanding xFetch refs */
2308023060
sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
2308123061
sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
2308223062
sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
2308323063
void *pMapRegion; /* Memory mapped region */
23064
+#endif
2308423065
#ifdef __QNXNTO__
2308523066
int sectorSize; /* Device sector size */
2308623067
int deviceCharacteristics; /* Precomputed device characteristics */
2308723068
#endif
2308823069
#if SQLITE_ENABLE_LOCKING_STYLE
@@ -23513,10 +23494,11 @@
2351323494
#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
2351423495
2351523496
{ "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
2351623497
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
2351723498
23499
+#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
2351823500
{ "mmap", (sqlite3_syscall_ptr)mmap, 0 },
2351923501
#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
2352023502
2352123503
{ "munmap", (sqlite3_syscall_ptr)munmap, 0 },
2352223504
#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23525,10 +23507,11 @@
2352523507
{ "mremap", (sqlite3_syscall_ptr)mremap, 0 },
2352623508
#else
2352723509
{ "mremap", (sqlite3_syscall_ptr)0, 0 },
2352823510
#endif
2352923511
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23512
+#endif
2353023513
2353123514
}; /* End of the overrideable system calls */
2353223515
2353323516
/*
2353423517
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23631,17 +23614,27 @@
2363123614
** recover the hot journals.
2363223615
*/
2363323616
static int robust_open(const char *z, int f, mode_t m){
2363423617
int fd;
2363523618
mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23636
- do{
23619
+ while(1){
2363723620
#if defined(O_CLOEXEC)
2363823621
fd = osOpen(z,f|O_CLOEXEC,m2);
2363923622
#else
2364023623
fd = osOpen(z,f,m2);
2364123624
#endif
23642
- }while( fd<0 && errno==EINTR );
23625
+ if( fd<0 ){
23626
+ if( errno==EINTR ) continue;
23627
+ break;
23628
+ }
23629
+ if( fd>2 ) break;
23630
+ osClose(fd);
23631
+ sqlite3_log(SQLITE_WARNING,
23632
+ "attempt to open \"%s\" as file descriptor %d", z, fd);
23633
+ fd = -1;
23634
+ if( osOpen("/dev/null", f, m)<0 ) break;
23635
+ }
2364323636
if( fd>=0 ){
2364423637
if( m!=0 ){
2364523638
struct stat statbuf;
2364623639
if( osFstat(fd, &statbuf)==0
2364723640
&& statbuf.st_size==0
@@ -24935,12 +24928,14 @@
2493524928
static int unixUnlock(sqlite3_file *id, int eFileLock){
2493624929
assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
2493724930
return posixUnlock(id, eFileLock, 0);
2493824931
}
2493924932
24933
+#if SQLITE_MAX_MMAP_SIZE>0
2494024934
static int unixMapfile(unixFile *pFd, i64 nByte);
2494124935
static void unixUnmapfile(unixFile *pFd);
24936
+#endif
2494224937
2494324938
/*
2494424939
** This function performs the parts of the "close file" operation
2494524940
** common to all locking schemes. It closes the directory and file
2494624941
** handles, if they are valid, and sets all fields of the unixFile
@@ -24950,11 +24945,13 @@
2495024945
** even on VxWorks. A mutex will be acquired on VxWorks by the
2495124946
** vxworksReleaseFileId() routine.
2495224947
*/
2495324948
static int closeUnixFile(sqlite3_file *id){
2495424949
unixFile *pFile = (unixFile*)id;
24950
+#if SQLITE_MAX_MMAP_SIZE>0
2495524951
unixUnmapfile(pFile);
24952
+#endif
2495624953
if( pFile->h>=0 ){
2495724954
robust_close(pFile, pFile->h, __LINE__);
2495824955
pFile->h = -1;
2495924956
}
2496024957
#if OS_VXWORKS
@@ -26155,10 +26152,11 @@
2615526152
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2615626153
i64 newOffset;
2615726154
#endif
2615826155
TIMER_START;
2615926156
assert( cnt==(cnt&0x1ffff) );
26157
+ assert( id->h>2 );
2616026158
cnt &= 0x1ffff;
2616126159
do{
2616226160
#if defined(USE_PREAD)
2616326161
got = osPread(id->h, pBuf, cnt, offset);
2616426162
SimulateIOError( got = -1 );
@@ -26269,10 +26267,11 @@
2626926267
int *piErrno /* OUT: Error number if error occurs */
2627026268
){
2627126269
int rc = 0; /* Value returned by system call */
2627226270
2627326271
assert( nBuf==(nBuf&0x1ffff) );
26272
+ assert( fd>2 );
2627426273
nBuf &= 0x1ffff;
2627526274
TIMER_START;
2627626275
2627726276
#if defined(USE_PREAD)
2627826277
do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26654,17 +26653,19 @@
2665426653
if( pFile->inNormalWrite && nByte==0 ){
2665526654
pFile->transCntrChng = 1;
2665626655
}
2665726656
#endif
2665826657
26658
+#if SQLITE_MAX_MMAP_SIZE>0
2665926659
/* If the file was just truncated to a size smaller than the currently
2666026660
** mapped region, reduce the effective mapping size as well. SQLite will
2666126661
** use read() and write() to access data beyond this point from now on.
2666226662
*/
2666326663
if( nByte<pFile->mmapSize ){
2666426664
pFile->mmapSize = nByte;
2666526665
}
26666
+#endif
2666626667
2666726668
return SQLITE_OK;
2666826669
}
2666926670
}
2667026671
@@ -26750,10 +26751,11 @@
2675026751
}
2675126752
#endif
2675226753
}
2675326754
}
2675426755
26756
+#if SQLITE_MAX_MMAP_SIZE>0
2675526757
if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
2675626758
int rc;
2675726759
if( pFile->szChunk<=0 ){
2675826760
if( robust_ftruncate(pFile->h, nByte) ){
2675926761
pFile->lastErrno = errno;
@@ -26762,10 +26764,11 @@
2676226764
}
2676326765
2676426766
rc = unixMapfile(pFile, nByte);
2676526767
return rc;
2676626768
}
26769
+#endif
2676726770
2676826771
return SQLITE_OK;
2676926772
}
2677026773
2677126774
/*
@@ -26830,10 +26833,11 @@
2683026833
unixGetTempname(pFile->pVfs->mxPathname, zTFile);
2683126834
*(char**)pArg = zTFile;
2683226835
}
2683326836
return SQLITE_OK;
2683426837
}
26838
+#if SQLITE_MAX_MMAP_SIZE>0
2683526839
case SQLITE_FCNTL_MMAP_SIZE: {
2683626840
i64 newLimit = *(i64*)pArg;
2683726841
int rc = SQLITE_OK;
2683826842
if( newLimit>sqlite3GlobalConfig.mxMmap ){
2683926843
newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26846,10 +26850,11 @@
2684626850
rc = unixMapfile(pFile, -1);
2684726851
}
2684826852
}
2684926853
return rc;
2685026854
}
26855
+#endif
2685126856
#ifdef SQLITE_DEBUG
2685226857
/* The pager calls this method to signal that it has done
2685326858
** a rollback and that the database is therefore unchanged and
2685426859
** it hence it is OK for the transaction change counter to be
2685526860
** unchanged.
@@ -27656,26 +27661,24 @@
2765627661
# define unixShmLock 0
2765727662
# define unixShmBarrier 0
2765827663
# define unixShmUnmap 0
2765927664
#endif /* #ifndef SQLITE_OMIT_WAL */
2766027665
27666
+#if SQLITE_MAX_MMAP_SIZE>0
2766127667
/*
2766227668
** If it is currently memory mapped, unmap file pFd.
2766327669
*/
2766427670
static void unixUnmapfile(unixFile *pFd){
2766527671
assert( pFd->nFetchOut==0 );
27666
-#if SQLITE_MAX_MMAP_SIZE>0
2766727672
if( pFd->pMapRegion ){
2766827673
osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
2766927674
pFd->pMapRegion = 0;
2767027675
pFd->mmapSize = 0;
2767127676
pFd->mmapSizeActual = 0;
2767227677
}
27673
-#endif
2767427678
}
2767527679
27676
-#if SQLITE_MAX_MMAP_SIZE>0
2767727680
/*
2767827681
** Return the system page size.
2767927682
*/
2768027683
static int unixGetPagesize(void){
2768127684
#if HAVE_MREMAP
@@ -27684,13 +27687,11 @@
2768427687
return getpagesize();
2768527688
#else
2768627689
return (int)sysconf(_SC_PAGESIZE);
2768727690
#endif
2768827691
}
27689
-#endif /* SQLITE_MAX_MMAP_SIZE>0 */
2769027692
27691
-#if SQLITE_MAX_MMAP_SIZE>0
2769227693
/*
2769327694
** Attempt to set the size of the memory mapping maintained by file
2769427695
** descriptor pFd to nNew bytes. Any existing mapping is discarded.
2769527696
**
2769627697
** If successful, this function sets the following variables:
@@ -27771,11 +27772,10 @@
2777127772
pFd->mmapSizeMax = 0;
2777227773
}
2777327774
pFd->pMapRegion = (void *)pNew;
2777427775
pFd->mmapSize = pFd->mmapSizeActual = nNew;
2777527776
}
27776
-#endif
2777727777
2777827778
/*
2777927779
** Memory map or remap the file opened by file-descriptor pFd (if the file
2778027780
** is already mapped, the existing mapping is replaced by the new). Or, if
2778127781
** there already exists a mapping for this file, and there are still
@@ -27790,11 +27790,10 @@
2779027790
** SQLITE_OK is returned if no error occurs (even if the mapping is not
2779127791
** recreated as a result of outstanding references) or an SQLite error
2779227792
** code otherwise.
2779327793
*/
2779427794
static int unixMapfile(unixFile *pFd, i64 nByte){
27795
-#if SQLITE_MAX_MMAP_SIZE>0
2779627795
i64 nMap = nByte;
2779727796
int rc;
2779827797
2779927798
assert( nMap>=0 || pFd->nFetchOut==0 );
2780027799
if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27816,14 +27815,14 @@
2781627815
unixRemapfile(pFd, nMap);
2781727816
}else{
2781827817
unixUnmapfile(pFd);
2781927818
}
2782027819
}
27821
-#endif
2782227820
2782327821
return SQLITE_OK;
2782427822
}
27823
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
2782527824
2782627825
/*
2782727826
** If possible, return a pointer to a mapping of file fd starting at offset
2782827827
** iOff. The mapping must be valid for at least nAmt bytes.
2782927828
**
@@ -27868,10 +27867,11 @@
2786827867
*/
2786927868
static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
2787027869
unixFile *pFd = (unixFile *)fd; /* The underlying database file */
2787127870
UNUSED_PARAMETER(iOff);
2787227871
27872
+#if SQLITE_MAX_MMAP_SIZE>0
2787327873
/* If p==0 (unmap the entire file) then there must be no outstanding
2787427874
** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
2787527875
** then there must be at least one outstanding. */
2787627876
assert( (p==0)==(pFd->nFetchOut==0) );
2787727877
@@ -27883,10 +27883,11 @@
2788327883
}else{
2788427884
unixUnmapfile(pFd);
2788527885
}
2788627886
2788727887
assert( pFd->nFetchOut>=0 );
27888
+#endif
2788827889
return SQLITE_OK;
2788927890
}
2789027891
2789127892
/*
2789227893
** Here ends the implementation of all sqlite3_file methods.
@@ -28214,11 +28215,13 @@
2821428215
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2821528216
pNew->h = h;
2821628217
pNew->pVfs = pVfs;
2821728218
pNew->zPath = zFilename;
2821828219
pNew->ctrlFlags = (u8)ctrlFlags;
28220
+#if SQLITE_MAX_MMAP_SIZE>0
2821928221
pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28222
+#endif
2822028223
if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
2822128224
"psow", SQLITE_POWERSAFE_OVERWRITE) ){
2822228225
pNew->ctrlFlags |= UNIXFILE_PSOW;
2822328226
}
2822428227
if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -30715,11 +30718,11 @@
3071530718
/*
3071630719
** Compiling and using WAL mode requires several APIs that are only
3071730720
** available in Windows platforms based on the NT kernel.
3071830721
*/
3071930722
#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30720
-# error "WAL mode requires support from the Windows NT kernel, compile\
30723
+# error "WAL mode requires support from the Windows NT kernel, compile\
3072130724
with SQLITE_OMIT_WAL."
3072230725
#endif
3072330726
3072430727
/*
3072530728
** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
@@ -30735,10 +30738,70 @@
3073530738
*/
3073630739
#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
3073730740
# define SQLITE_WIN32_HAS_WIDE
3073830741
#endif
3073930742
30743
+/*
30744
+** Maximum pathname length (in chars) for Win32. This should normally be
30745
+** MAX_PATH.
30746
+*/
30747
+#ifndef SQLITE_WIN32_MAX_PATH_CHARS
30748
+# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
30749
+#endif
30750
+
30751
+/*
30752
+** Maximum pathname length (in chars) for WinNT. This should normally be
30753
+** 32767.
30754
+*/
30755
+#ifndef SQLITE_WINNT_MAX_PATH_CHARS
30756
+# define SQLITE_WINNT_MAX_PATH_CHARS (32767)
30757
+#endif
30758
+
30759
+/*
30760
+** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
30761
+** characters, so we allocate 3 bytes per character assuming worst-case of
30762
+** 4-bytes-per-character for UTF8.
30763
+*/
30764
+#ifndef SQLITE_WIN32_MAX_PATH_BYTES
30765
+# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
30766
+#endif
30767
+
30768
+/*
30769
+** Maximum pathname length (in bytes) for WinNT. This should normally be
30770
+** 32767 * sizeof(WCHAR).
30771
+*/
30772
+#ifndef SQLITE_WINNT_MAX_PATH_BYTES
30773
+# define SQLITE_WINNT_MAX_PATH_BYTES \
30774
+ (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
30775
+#endif
30776
+
30777
+/*
30778
+** Maximum error message length (in chars) for WinRT.
30779
+*/
30780
+#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
30781
+# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
30782
+#endif
30783
+
30784
+/*
30785
+** Returns non-zero if the character should be treated as a directory
30786
+** separator.
30787
+*/
30788
+#ifndef winIsDirSep
30789
+# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
30790
+#endif
30791
+
30792
+/*
30793
+** Returns the string that should be used as the directory separator.
30794
+*/
30795
+#ifndef winGetDirDep
30796
+# ifdef __CYGWIN__
30797
+# define winGetDirDep() "/"
30798
+# else
30799
+# define winGetDirDep() "\\"
30800
+# endif
30801
+#endif
30802
+
3074030803
/*
3074130804
** Do we need to manually define the Win32 file mapping APIs for use with WAL
3074230805
** mode (e.g. these APIs are available in the Windows CE SDK; however, they
3074330806
** are not present in the header file)?
3074430807
*/
@@ -31742,15 +31805,15 @@
3174231805
** this routine is used to determine if the host is Win95/98/ME or
3174331806
** WinNT/2K/XP so that we will know whether or not we can safely call
3174431807
** the LockFileEx() API.
3174531808
*/
3174631809
#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31747
-# define isNT() (1)
31810
+# define osIsNT() (1)
3174831811
#elif !defined(SQLITE_WIN32_HAS_WIDE)
31749
-# define isNT() (0)
31812
+# define osIsNT() (0)
3175031813
#else
31751
- static int isNT(void){
31814
+ static int osIsNT(void){
3175231815
if( sqlite3_os_type==0 ){
3175331816
OSVERSIONINFOA sInfo;
3175431817
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
3175531818
osGetVersionExA(&sInfo);
3175631819
sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31957,11 +32020,11 @@
3195732020
/*
3195832021
** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
3195932022
**
3196032023
** Space to hold the returned string is obtained from malloc.
3196132024
*/
31962
-static LPWSTR utf8ToUnicode(const char *zFilename){
32025
+static LPWSTR winUtf8ToUnicode(const char *zFilename){
3196332026
int nChar;
3196432027
LPWSTR zWideFilename;
3196532028
3196632029
nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
3196732030
if( nChar==0 ){
@@ -31982,11 +32045,11 @@
3198232045
3198332046
/*
3198432047
** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
3198532048
** obtained from sqlite3_malloc().
3198632049
*/
31987
-static char *unicodeToUtf8(LPCWSTR zWideFilename){
32050
+static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
3198832051
int nByte;
3198932052
char *zFilename;
3199032053
3199132054
nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
3199232055
if( nByte == 0 ){
@@ -32010,11 +32073,11 @@
3201032073
** current codepage settings for file apis.
3201132074
**
3201232075
** Space to hold the returned string is obtained
3201332076
** from sqlite3_malloc.
3201432077
*/
32015
-static LPWSTR mbcsToUnicode(const char *zFilename){
32078
+static LPWSTR winMbcsToUnicode(const char *zFilename){
3201632079
int nByte;
3201732080
LPWSTR zMbcsFilename;
3201832081
int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3201932082
3202032083
nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32040,11 +32103,11 @@
3204032103
** user's ANSI codepage.
3204132104
**
3204232105
** Space to hold the returned string is obtained from
3204332106
** sqlite3_malloc().
3204432107
*/
32045
-static char *unicodeToMbcs(LPCWSTR zWideFilename){
32108
+static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
3204632109
int nByte;
3204732110
char *zFilename;
3204832111
int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3204932112
3205032113
nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32070,15 +32133,15 @@
3207032133
*/
3207132134
SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
3207232135
char *zFilenameUtf8;
3207332136
LPWSTR zTmpWide;
3207432137
32075
- zTmpWide = mbcsToUnicode(zFilename);
32138
+ zTmpWide = winMbcsToUnicode(zFilename);
3207632139
if( zTmpWide==0 ){
3207732140
return 0;
3207832141
}
32079
- zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32142
+ zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
3208032143
sqlite3_free(zTmpWide);
3208132144
return zFilenameUtf8;
3208232145
}
3208332146
3208432147
/*
@@ -32087,15 +32150,15 @@
3208732150
*/
3208832151
SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
3208932152
char *zFilenameMbcs;
3209032153
LPWSTR zTmpWide;
3209132154
32092
- zTmpWide = utf8ToUnicode(zFilename);
32155
+ zTmpWide = winUtf8ToUnicode(zFilename);
3209332156
if( zTmpWide==0 ){
3209432157
return 0;
3209532158
}
32096
- zFilenameMbcs = unicodeToMbcs(zTmpWide);
32159
+ zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
3209732160
sqlite3_free(zTmpWide);
3209832161
return zFilenameMbcs;
3209932162
}
3210032163
3210132164
/*
@@ -32121,11 +32184,11 @@
3212132184
);
3212232185
assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
3212332186
if( ppDirectory ){
3212432187
char *zValueUtf8 = 0;
3212532188
if( zValue && zValue[0] ){
32126
- zValueUtf8 = unicodeToUtf8(zValue);
32189
+ zValueUtf8 = winUnicodeToUtf8(zValue);
3212732190
if ( zValueUtf8==0 ){
3212832191
return SQLITE_NOMEM;
3212932192
}
3213032193
}
3213132194
sqlite3_free(*ppDirectory);
@@ -32134,32 +32197,32 @@
3213432197
}
3213532198
return SQLITE_ERROR;
3213632199
}
3213732200
3213832201
/*
32139
-** The return value of getLastErrorMsg
32202
+** The return value of winGetLastErrorMsg
3214032203
** is zero if the error message fits in the buffer, or non-zero
3214132204
** otherwise (if the message was truncated).
3214232205
*/
32143
-static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32206
+static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
3214432207
/* FormatMessage returns 0 on failure. Otherwise it
3214532208
** returns the number of TCHARs written to the output
3214632209
** buffer, excluding the terminating null char.
3214732210
*/
3214832211
DWORD dwLen = 0;
3214932212
char *zOut = 0;
3215032213
32151
- if( isNT() ){
32214
+ if( osIsNT() ){
3215232215
#if SQLITE_OS_WINRT
32153
- WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
32216
+ WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
3215432217
dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
3215532218
FORMAT_MESSAGE_IGNORE_INSERTS,
3215632219
NULL,
3215732220
lastErrno,
3215832221
0,
3215932222
zTempWide,
32160
- MAX_PATH,
32223
+ SQLITE_WIN32_MAX_ERRMSG_CHARS,
3216132224
0);
3216232225
#else
3216332226
LPWSTR zTempWide = NULL;
3216432227
dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
3216532228
FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32172,11 +32235,11 @@
3217232235
0);
3217332236
#endif
3217432237
if( dwLen > 0 ){
3217532238
/* allocate a buffer and convert to UTF8 */
3217632239
sqlite3BeginBenignMalloc();
32177
- zOut = unicodeToUtf8(zTempWide);
32240
+ zOut = winUnicodeToUtf8(zTempWide);
3217832241
sqlite3EndBenignMalloc();
3217932242
#if !SQLITE_OS_WINRT
3218032243
/* free the system buffer allocated by FormatMessage */
3218132244
osLocalFree(zTempWide);
3218232245
#endif
@@ -32240,11 +32303,11 @@
3224032303
){
3224132304
char zMsg[500]; /* Human readable error text */
3224232305
int i; /* Loop counter */
3224332306
3224432307
zMsg[0] = 0;
32245
- getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32308
+ winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
3224632309
assert( errcode!=SQLITE_OK );
3224732310
if( zPath==0 ) zPath = "";
3224832311
for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
3224932312
zMsg[i] = 0;
3225032313
sqlite3_log(errcode,
@@ -32265,30 +32328,30 @@
3226532328
# define SQLITE_WIN32_IOERR_RETRY 10
3226632329
#endif
3226732330
#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
3226832331
# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
3226932332
#endif
32270
-static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32271
-static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32333
+static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32334
+static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
3227232335
3227332336
/*
3227432337
** If a ReadFile() or WriteFile() error occurs, invoke this routine
3227532338
** to see if it should be retried. Return TRUE to retry. Return FALSE
3227632339
** to give up with an error.
3227732340
*/
32278
-static int retryIoerr(int *pnRetry, DWORD *pError){
32341
+static int winRetryIoerr(int *pnRetry, DWORD *pError){
3227932342
DWORD e = osGetLastError();
32280
- if( *pnRetry>=win32IoerrRetry ){
32343
+ if( *pnRetry>=winIoerrRetry ){
3228132344
if( pError ){
3228232345
*pError = e;
3228332346
}
3228432347
return 0;
3228532348
}
3228632349
if( e==ERROR_ACCESS_DENIED ||
3228732350
e==ERROR_LOCK_VIOLATION ||
3228832351
e==ERROR_SHARING_VIOLATION ){
32289
- sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
32352
+ sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
3229032353
++*pnRetry;
3229132354
return 1;
3229232355
}
3229332356
if( pError ){
3229432357
*pError = e;
@@ -32297,15 +32360,15 @@
3229732360
}
3229832361
3229932362
/*
3230032363
** Log a I/O error retry episode.
3230132364
*/
32302
-static void logIoerr(int nRetry){
32365
+static void winLogIoerr(int nRetry){
3230332366
if( nRetry ){
3230432367
sqlite3_log(SQLITE_IOERR,
3230532368
"delayed %dms for lock/sharing conflict",
32306
- win32IoerrRetryDelay*nRetry*(nRetry+1)/2
32369
+ winIoerrRetryDelay*nRetry*(nRetry+1)/2
3230732370
);
3230832371
}
3230932372
}
3231032373
3231132374
#if SQLITE_OS_WINCE
@@ -32366,11 +32429,11 @@
3236632429
LPWSTR zName;
3236732430
DWORD lastErrno;
3236832431
BOOL bLogged = FALSE;
3236932432
BOOL bInit = TRUE;
3237032433
32371
- zName = utf8ToUnicode(zFilename);
32434
+ zName = winUtf8ToUnicode(zFilename);
3237232435
if( zName==0 ){
3237332436
/* out of memory */
3237432437
return SQLITE_IOERR_NOMEM;
3237532438
}
3237632439
@@ -32639,11 +32702,11 @@
3263932702
** API LockFile.
3264032703
*/
3264132704
return winceLockFile(phFile, offsetLow, offsetHigh,
3264232705
numBytesLow, numBytesHigh);
3264332706
#else
32644
- if( isNT() ){
32707
+ if( osIsNT() ){
3264532708
OVERLAPPED ovlp;
3264632709
memset(&ovlp, 0, sizeof(OVERLAPPED));
3264732710
ovlp.Offset = offsetLow;
3264832711
ovlp.OffsetHigh = offsetHigh;
3264932712
return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32670,11 +32733,11 @@
3267032733
** API UnlockFile.
3267132734
*/
3267232735
return winceUnlockFile(phFile, offsetLow, offsetHigh,
3267332736
numBytesLow, numBytesHigh);
3267432737
#else
32675
- if( isNT() ){
32738
+ if( osIsNT() ){
3267632739
OVERLAPPED ovlp;
3267732740
memset(&ovlp, 0, sizeof(OVERLAPPED));
3267832741
ovlp.Offset = offsetLow;
3267932742
ovlp.OffsetHigh = offsetHigh;
3268032743
return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32700,11 +32763,11 @@
3270032763
/*
3270132764
** Move the current position of the file handle passed as the first
3270232765
** argument to offset iOffset within the file. If successful, return 0.
3270332766
** Otherwise, set pFile->lastErrno and return non-zero.
3270432767
*/
32705
-static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32768
+static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
3270632769
#if !SQLITE_OS_WINRT
3270732770
LONG upperBits; /* Most sig. 32 bits of new offset */
3270832771
LONG lowerBits; /* Least sig. 32 bits of new offset */
3270932772
DWORD dwRet; /* Value returned by SetFilePointer() */
3271032773
DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32725,11 +32788,11 @@
3272532788
3272632789
if( (dwRet==INVALID_SET_FILE_POINTER
3272732790
&& ((lastErrno = osGetLastError())!=NO_ERROR)) ){
3272832791
pFile->lastErrno = lastErrno;
3272932792
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32730
- "seekWinFile", pFile->zPath);
32793
+ "winSeekFile", pFile->zPath);
3273132794
OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
3273232795
return 1;
3273332796
}
3273432797
3273532798
OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32746,11 +32809,11 @@
3274632809
bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
3274732810
3274832811
if(!bRet){
3274932812
pFile->lastErrno = osGetLastError();
3275032813
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32751
- "seekWinFile", pFile->zPath);
32814
+ "winSeekFile", pFile->zPath);
3275232815
OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
3275332816
return 1;
3275432817
}
3275532818
3275632819
OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32861,11 +32924,11 @@
3286132924
}
3286232925
}
3286332926
#endif
3286432927
3286532928
#if SQLITE_OS_WINCE
32866
- if( seekWinFile(pFile, offset) ){
32929
+ if( winSeekFile(pFile, offset) ){
3286732930
OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
3286832931
return SQLITE_FULL;
3286932932
}
3287032933
while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
3287132934
#else
@@ -32874,17 +32937,17 @@
3287432937
overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
3287532938
while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
3287632939
osGetLastError()!=ERROR_HANDLE_EOF ){
3287732940
#endif
3287832941
DWORD lastErrno;
32879
- if( retryIoerr(&nRetry, &lastErrno) ) continue;
32942
+ if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
3288032943
pFile->lastErrno = lastErrno;
3288132944
OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
3288232945
return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
3288332946
"winRead", pFile->zPath);
3288432947
}
32885
- logIoerr(nRetry);
32948
+ winLogIoerr(nRetry);
3288632949
if( nRead<(DWORD)amt ){
3288732950
/* Unread parts of the buffer must be zero-filled */
3288832951
memset(&((char*)pBuf)[nRead], 0, amt-nRead);
3288932952
OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
3289032953
return SQLITE_IOERR_SHORT_READ;
@@ -32933,11 +32996,11 @@
3293332996
}
3293432997
}
3293532998
#endif
3293632999
3293733000
#if SQLITE_OS_WINCE
32938
- rc = seekWinFile(pFile, offset);
33001
+ rc = winSeekFile(pFile, offset);
3293933002
if( rc==0 ){
3294033003
#else
3294133004
{
3294233005
#endif
3294333006
#if !SQLITE_OS_WINCE
@@ -32958,11 +33021,11 @@
3295833021
#if SQLITE_OS_WINCE
3295933022
if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
3296033023
#else
3296133024
if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
3296233025
#endif
32963
- if( retryIoerr(&nRetry, &lastErrno) ) continue;
33026
+ if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
3296433027
break;
3296533028
}
3296633029
assert( nWrite==0 || nWrite<=(DWORD)nRem );
3296733030
if( nWrite==0 || nWrite>(DWORD)nRem ){
3296833031
lastErrno = osGetLastError();
@@ -32990,11 +33053,11 @@
3299033053
}
3299133054
OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
3299233055
return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
3299333056
"winWrite", pFile->zPath);
3299433057
}else{
32995
- logIoerr(nRetry);
33058
+ winLogIoerr(nRetry);
3299633059
}
3299733060
OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
3299833061
return SQLITE_OK;
3299933062
}
3300033063
@@ -33019,11 +33082,11 @@
3301933082
if( pFile->szChunk>0 ){
3302033083
nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3302133084
}
3302233085
3302333086
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33024
- if( seekWinFile(pFile, nByte) ){
33087
+ if( winSeekFile(pFile, nByte) ){
3302533088
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
3302633089
"winTruncate1", pFile->zPath);
3302733090
}else if( 0==osSetEndOfFile(pFile->h) &&
3302833091
((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
3302933092
pFile->lastErrno = lastErrno;
@@ -33100,10 +33163,11 @@
3310033163
3310133164
/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
3310233165
** no-op
3310333166
*/
3310433167
#ifdef SQLITE_NO_SYNC
33168
+ OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
3310533169
return SQLITE_OK;
3310633170
#else
3310733171
rc = osFlushFileBuffers(pFile->h);
3310833172
SimulateIOError( rc=FALSE );
3310933173
if( rc ){
@@ -33197,14 +33261,14 @@
3319733261
/*
3319833262
** Acquire a reader lock.
3319933263
** Different API routines are called depending on whether or not this
3320033264
** is Win9x or WinNT.
3320133265
*/
33202
-static int getReadLock(winFile *pFile){
33266
+static int winGetReadLock(winFile *pFile){
3320333267
int res;
3320433268
OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33205
- if( isNT() ){
33269
+ if( osIsNT() ){
3320633270
#if SQLITE_OS_WINCE
3320733271
/*
3320833272
** NOTE: Windows CE is handled differently here due its lack of the Win32
3320933273
** API LockFileEx.
3321033274
*/
@@ -33232,15 +33296,15 @@
3323233296
}
3323333297
3323433298
/*
3323533299
** Undo a readlock
3323633300
*/
33237
-static int unlockReadLock(winFile *pFile){
33301
+static int winUnlockReadLock(winFile *pFile){
3323833302
int res;
3323933303
DWORD lastErrno;
3324033304
OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33241
- if( isNT() ){
33305
+ if( osIsNT() ){
3324233306
res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3324333307
}
3324433308
#ifdef SQLITE_WIN32_HAS_ANSI
3324533309
else{
3324633310
res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33247,11 +33311,11 @@
3324733311
}
3324833312
#endif
3324933313
if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
3325033314
pFile->lastErrno = lastErrno;
3325133315
winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33252
- "unlockReadLock", pFile->zPath);
33316
+ "winUnlockReadLock", pFile->zPath);
3325333317
}
3325433318
OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
3325533319
return res;
3325633320
}
3325733321
@@ -33338,11 +33402,11 @@
3333833402
3333933403
/* Acquire a shared lock
3334033404
*/
3334133405
if( locktype==SHARED_LOCK && res ){
3334233406
assert( pFile->locktype==NO_LOCK );
33343
- res = getReadLock(pFile);
33407
+ res = winGetReadLock(pFile);
3334433408
if( res ){
3334533409
newLocktype = SHARED_LOCK;
3334633410
}else{
3334733411
lastErrno = osGetLastError();
3334833412
}
@@ -33369,18 +33433,18 @@
3336933433
3337033434
/* Acquire an EXCLUSIVE lock
3337133435
*/
3337233436
if( locktype==EXCLUSIVE_LOCK && res ){
3337333437
assert( pFile->locktype>=SHARED_LOCK );
33374
- res = unlockReadLock(pFile);
33438
+ res = winUnlockReadLock(pFile);
3337533439
res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
3337633440
SHARED_SIZE, 0);
3337733441
if( res ){
3337833442
newLocktype = EXCLUSIVE_LOCK;
3337933443
}else{
3338033444
lastErrno = osGetLastError();
33381
- getReadLock(pFile);
33445
+ winGetReadLock(pFile);
3338233446
}
3338333447
}
3338433448
3338533449
/* If we are holding a PENDING lock that ought to be released, then
3338633450
** release it now.
@@ -33393,14 +33457,14 @@
3339333457
** return the appropriate result code.
3339433458
*/
3339533459
if( res ){
3339633460
rc = SQLITE_OK;
3339733461
}else{
33462
+ pFile->lastErrno = lastErrno;
33463
+ rc = SQLITE_BUSY;
3339833464
OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
3339933465
pFile->h, locktype, newLocktype));
33400
- pFile->lastErrno = lastErrno;
33401
- rc = SQLITE_BUSY;
3340233466
}
3340333467
pFile->locktype = (u8)newLocktype;
3340433468
OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
3340533469
pFile->h, pFile->locktype, sqlite3ErrName(rc)));
3340633470
return rc;
@@ -33456,11 +33520,11 @@
3345633520
OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
3345733521
pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
3345833522
type = pFile->locktype;
3345933523
if( type>=EXCLUSIVE_LOCK ){
3346033524
winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33461
- if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33525
+ if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
3346233526
/* This should never happen. We should always be able to
3346333527
** reacquire the read lock */
3346433528
rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
3346533529
"winUnlock", pFile->zPath);
3346633530
}
@@ -33467,11 +33531,11 @@
3346733531
}
3346833532
if( type>=RESERVED_LOCK ){
3346933533
winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
3347033534
}
3347133535
if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33472
- unlockReadLock(pFile);
33536
+ winUnlockReadLock(pFile);
3347333537
}
3347433538
if( type>=PENDING_LOCK ){
3347533539
winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
3347633540
}
3347733541
pFile->locktype = (u8)locktype;
@@ -33495,11 +33559,11 @@
3349533559
pFile->ctrlFlags |= mask;
3349633560
}
3349733561
}
3349833562
3349933563
/* Forward declaration */
33500
-static int getTempname(int nBuf, char *zBuf);
33564
+static int winGetTempname(sqlite3_vfs *, char **);
3350133565
#if SQLITE_MAX_MMAP_SIZE>0
3350233566
static int winMapfile(winFile*, sqlite3_int64);
3350333567
#endif
3350433568
3350533569
/*
@@ -33558,30 +33622,30 @@
3355833622
return SQLITE_OK;
3355933623
}
3356033624
case SQLITE_FCNTL_WIN32_AV_RETRY: {
3356133625
int *a = (int*)pArg;
3356233626
if( a[0]>0 ){
33563
- win32IoerrRetry = a[0];
33627
+ winIoerrRetry = a[0];
3356433628
}else{
33565
- a[0] = win32IoerrRetry;
33629
+ a[0] = winIoerrRetry;
3356633630
}
3356733631
if( a[1]>0 ){
33568
- win32IoerrRetryDelay = a[1];
33632
+ winIoerrRetryDelay = a[1];
3356933633
}else{
33570
- a[1] = win32IoerrRetryDelay;
33634
+ a[1] = winIoerrRetryDelay;
3357133635
}
3357233636
OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3357333637
return SQLITE_OK;
3357433638
}
3357533639
case SQLITE_FCNTL_TEMPFILENAME: {
33576
- char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
33577
- if( zTFile ){
33578
- getTempname(pFile->pVfs->mxPathname, zTFile);
33640
+ char *zTFile = 0;
33641
+ int rc = winGetTempname(pFile->pVfs, &zTFile);
33642
+ if( rc==SQLITE_OK ){
3357933643
*(char**)pArg = zTFile;
3358033644
}
33581
- OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33582
- return SQLITE_OK;
33645
+ OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
33646
+ return rc;
3358333647
}
3358433648
#if SQLITE_MAX_MMAP_SIZE>0
3358533649
case SQLITE_FCNTL_MMAP_SIZE: {
3358633650
i64 newLimit = *(i64*)pArg;
3358733651
int rc = SQLITE_OK;
@@ -34539,14 +34603,14 @@
3453934603
** Convert a UTF-8 filename into whatever form the underlying
3454034604
** operating system wants filenames in. Space to hold the result
3454134605
** is obtained from malloc and must be freed by the calling
3454234606
** function.
3454334607
*/
34544
-static void *convertUtf8Filename(const char *zFilename){
34608
+static void *winConvertUtf8Filename(const char *zFilename){
3454534609
void *zConverted = 0;
34546
- if( isNT() ){
34547
- zConverted = utf8ToUnicode(zFilename);
34610
+ if( osIsNT() ){
34611
+ zConverted = winUtf8ToUnicode(zFilename);
3454834612
}
3454934613
#ifdef SQLITE_WIN32_HAS_ANSI
3455034614
else{
3455134615
zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
3455234616
}
@@ -34554,117 +34618,135 @@
3455434618
/* caller will handle out of memory */
3455534619
return zConverted;
3455634620
}
3455734621
3455834622
/*
34559
-** Maximum pathname length (in bytes) for windows. The MAX_PATH macro is
34560
-** in characters, so we allocate 3 bytes per character assuming worst-case
34561
-** 3-bytes-per-character UTF8.
34623
+** This function returns non-zero if the specified UTF-8 string buffer
34624
+** ends with a directory separator character.
3456234625
*/
34563
-#ifndef SQLITE_WIN32_MAX_PATH
34564
-# define SQLITE_WIN32_MAX_PATH (MAX_PATH*3)
34565
-#endif
34626
+static int winEndsInDirSep(char *zBuf){
34627
+ if( zBuf ){
34628
+ int nLen = sqlite3Strlen30(zBuf);
34629
+ return nLen>0 && winIsDirSep(zBuf[nLen-1]);
34630
+ }
34631
+ return 0;
34632
+}
3456634633
3456734634
/*
34568
-** Create a temporary file name in zBuf. zBuf must be big enough to
34569
-** hold at pVfs->mxPathname characters.
34635
+** Create a temporary file name and store the resulting pointer into pzBuf.
34636
+** The pointer returned in pzBuf must be freed via sqlite3_free().
3457034637
*/
34571
-static int getTempname(int nBuf, char *zBuf){
34638
+static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
3457234639
static char zChars[] =
3457334640
"abcdefghijklmnopqrstuvwxyz"
3457434641
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3457534642
"0123456789";
3457634643
size_t i, j;
34577
- int nTempPath;
34578
- char zTempPath[SQLITE_WIN32_MAX_PATH+2];
34644
+ int nBuf, nLen;
34645
+ char *zBuf;
3457934646
3458034647
/* It's odd to simulate an io-error here, but really this is just
3458134648
** using the io-error infrastructure to test that SQLite handles this
3458234649
** function failing.
3458334650
*/
3458434651
SimulateIOError( return SQLITE_IOERR );
3458534652
34653
+ /* Allocate a temporary buffer to store the fully qualified file
34654
+ ** name for the temporary file. If this fails, we cannot continue.
34655
+ */
34656
+ nBuf = pVfs->mxPathname;
34657
+ zBuf = sqlite3MallocZero( nBuf+2 );
34658
+ if( !zBuf ){
34659
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34660
+ return SQLITE_IOERR_NOMEM;
34661
+ }
34662
+
34663
+ /* Figure out the effective temporary directory. First, check if one
34664
+ ** has been explicitly set by the application; otherwise, use the one
34665
+ ** configured by the operating system.
34666
+ */
34667
+ assert( nBuf>30 );
3458634668
if( sqlite3_temp_directory ){
34587
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
34588
- sqlite3_temp_directory);
34669
+ sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
34670
+ winEndsInDirSep(sqlite3_temp_directory) ? "" :
34671
+ winGetDirDep());
3458934672
}
3459034673
#if !SQLITE_OS_WINRT
34591
- else if( isNT() ){
34674
+ else if( osIsNT() ){
3459234675
char *zMulti;
34593
- WCHAR zWidePath[MAX_PATH];
34594
- if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
34676
+ LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
34677
+ if( !zWidePath ){
34678
+ sqlite3_free(zBuf);
34679
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34680
+ return SQLITE_IOERR_NOMEM;
34681
+ }
34682
+ if( osGetTempPathW(nBuf, zWidePath)==0 ){
34683
+ sqlite3_free(zWidePath);
34684
+ sqlite3_free(zBuf);
3459534685
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
3459634686
return SQLITE_IOERR_GETTEMPPATH;
3459734687
}
34598
- zMulti = unicodeToUtf8(zWidePath);
34688
+ zMulti = winUnicodeToUtf8(zWidePath);
3459934689
if( zMulti ){
34600
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
34690
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
3460134691
sqlite3_free(zMulti);
34692
+ sqlite3_free(zWidePath);
3460234693
}else{
34694
+ sqlite3_free(zWidePath);
34695
+ sqlite3_free(zBuf);
3460334696
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
3460434697
return SQLITE_IOERR_NOMEM;
3460534698
}
3460634699
}
3460734700
#ifdef SQLITE_WIN32_HAS_ANSI
3460834701
else{
3460934702
char *zUtf8;
34610
- char zMbcsPath[SQLITE_WIN32_MAX_PATH];
34611
- if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
34703
+ char *zMbcsPath = sqlite3MallocZero( nBuf );
34704
+ if( !zMbcsPath ){
34705
+ sqlite3_free(zBuf);
34706
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34707
+ return SQLITE_IOERR_NOMEM;
34708
+ }
34709
+ if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
34710
+ sqlite3_free(zBuf);
3461234711
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
3461334712
return SQLITE_IOERR_GETTEMPPATH;
3461434713
}
3461534714
zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
3461634715
if( zUtf8 ){
34617
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
34716
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
3461834717
sqlite3_free(zUtf8);
3461934718
}else{
34719
+ sqlite3_free(zBuf);
3462034720
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
3462134721
return SQLITE_IOERR_NOMEM;
3462234722
}
3462334723
}
34624
-#else
34625
- else{
34626
- /*
34627
- ** Compiled without ANSI support and the current operating system
34628
- ** is not Windows NT; therefore, just zero the temporary buffer.
34629
- */
34630
- memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34631
- }
3463234724
#endif /* SQLITE_WIN32_HAS_ANSI */
34633
-#else
34634
- else{
34635
- /*
34636
- ** Compiled for WinRT and the sqlite3_temp_directory is not set;
34637
- ** therefore, just zero the temporary buffer.
34638
- */
34639
- memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34640
- }
3464134725
#endif /* !SQLITE_OS_WINRT */
3464234726
3464334727
/* Check that the output buffer is large enough for the temporary file
3464434728
** name. If it is not, return SQLITE_ERROR.
3464534729
*/
34646
- nTempPath = sqlite3Strlen30(zTempPath);
34730
+ nLen = sqlite3Strlen30(zBuf);
3464734731
34648
- if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34732
+ if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34733
+ sqlite3_free(zBuf);
3464934734
OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
3465034735
return SQLITE_ERROR;
3465134736
}
3465234737
34653
- for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
34654
- zTempPath[i] = 0;
34738
+ sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
3465534739
34656
- sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
34657
- "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
34658
- zTempPath);
3465934740
j = sqlite3Strlen30(zBuf);
3466034741
sqlite3_randomness(15, &zBuf[j]);
3466134742
for(i=0; i<15; i++, j++){
3466234743
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3466334744
}
3466434745
zBuf[j] = 0;
3466534746
zBuf[j+1] = 0;
34747
+ *pzBuf = zBuf;
3466634748
3466734749
OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
3466834750
return SQLITE_OK;
3466934751
}
3467034752
@@ -34676,17 +34758,17 @@
3467634758
static int winIsDir(const void *zConverted){
3467734759
DWORD attr;
3467834760
int rc = 0;
3467934761
DWORD lastErrno;
3468034762
34681
- if( isNT() ){
34763
+ if( osIsNT() ){
3468234764
int cnt = 0;
3468334765
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3468434766
memset(&sAttrData, 0, sizeof(sAttrData));
3468534767
while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
3468634768
GetFileExInfoStandard,
34687
- &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
34769
+ &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
3468834770
if( !rc ){
3468934771
return 0; /* Invalid name? */
3469034772
}
3469134773
attr = sAttrData.dwFileAttributes;
3469234774
#if SQLITE_OS_WINCE==0
@@ -34699,11 +34781,11 @@
3469934781
3470034782
/*
3470134783
** Open a file.
3470234784
*/
3470334785
static int winOpen(
34704
- sqlite3_vfs *pVfs, /* Not used */
34786
+ sqlite3_vfs *pVfs, /* Used to get maximum path name length */
3470534787
const char *zName, /* Name of the file (UTF-8) */
3470634788
sqlite3_file *id, /* Write the SQLite file handle here */
3470734789
int flags, /* Open mode flags */
3470834790
int *pOutFlags /* Status return flags */
3470934791
){
@@ -34722,11 +34804,11 @@
3472234804
int cnt = 0;
3472334805
3472434806
/* If argument zPath is a NULL pointer, this function is required to open
3472534807
** a temporary file. Use this buffer to store the file name in.
3472634808
*/
34727
- char zTmpname[SQLITE_WIN32_MAX_PATH+2]; /* Buffer used to create temp filename */
34809
+ char *zTmpname = 0; /* For temporary filename, if necessary. */
3472834810
3472934811
int rc = SQLITE_OK; /* Function Return Code */
3473034812
#if !defined(NDEBUG) || SQLITE_OS_WINCE
3473134813
int eType = flags&0xFFFFFF00; /* Type of file to open */
3473234814
#endif
@@ -34777,22 +34859,22 @@
3477734859
assert( pFile!=0 );
3477834860
memset(pFile, 0, sizeof(winFile));
3477934861
pFile->h = INVALID_HANDLE_VALUE;
3478034862
3478134863
#if SQLITE_OS_WINRT
34782
- if( !sqlite3_temp_directory ){
34864
+ if( !zUtf8Name && !sqlite3_temp_directory ){
3478334865
sqlite3_log(SQLITE_ERROR,
3478434866
"sqlite3_temp_directory variable should be set for WinRT");
3478534867
}
3478634868
#endif
3478734869
3478834870
/* If the second argument to this function is NULL, generate a
3478934871
** temporary file name to use
3479034872
*/
3479134873
if( !zUtf8Name ){
34792
- assert(isDelete && !isOpenJournal);
34793
- rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
34874
+ assert( isDelete && !isOpenJournal );
34875
+ rc = winGetTempname(pVfs, &zTmpname);
3479434876
if( rc!=SQLITE_OK ){
3479534877
OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
3479634878
return rc;
3479734879
}
3479834880
zUtf8Name = zTmpname;
@@ -34801,21 +34883,23 @@
3480134883
/* Database filenames are double-zero terminated if they are not
3480234884
** URIs with parameters. Hence, they can always be passed into
3480334885
** sqlite3_uri_parameter().
3480434886
*/
3480534887
assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
34806
- zUtf8Name[strlen(zUtf8Name)+1]==0 );
34888
+ zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
3480734889
3480834890
/* Convert the filename to the system encoding. */
34809
- zConverted = convertUtf8Filename(zUtf8Name);
34891
+ zConverted = winConvertUtf8Filename(zUtf8Name);
3481034892
if( zConverted==0 ){
34893
+ sqlite3_free(zTmpname);
3481134894
OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
3481234895
return SQLITE_IOERR_NOMEM;
3481334896
}
3481434897
3481534898
if( winIsDir(zConverted) ){
3481634899
sqlite3_free(zConverted);
34900
+ sqlite3_free(zTmpname);
3481734901
OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
3481834902
return SQLITE_CANTOPEN_ISDIR;
3481934903
}
3482034904
3482134905
if( isReadWrite ){
@@ -34858,11 +34942,11 @@
3485834942
** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
3485934943
#if SQLITE_OS_WINCE
3486034944
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
3486134945
#endif
3486234946
34863
- if( isNT() ){
34947
+ if( osIsNT() ){
3486434948
#if SQLITE_OS_WINRT
3486534949
CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
3486634950
extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
3486734951
extendedParameters.dwFileAttributes =
3486834952
dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34873,21 +34957,21 @@
3487334957
while( (h = osCreateFile2((LPCWSTR)zConverted,
3487434958
dwDesiredAccess,
3487534959
dwShareMode,
3487634960
dwCreationDisposition,
3487734961
&extendedParameters))==INVALID_HANDLE_VALUE &&
34878
- retryIoerr(&cnt, &lastErrno) ){
34962
+ winRetryIoerr(&cnt, &lastErrno) ){
3487934963
/* Noop */
3488034964
}
3488134965
#else
3488234966
while( (h = osCreateFileW((LPCWSTR)zConverted,
3488334967
dwDesiredAccess,
3488434968
dwShareMode, NULL,
3488534969
dwCreationDisposition,
3488634970
dwFlagsAndAttributes,
3488734971
NULL))==INVALID_HANDLE_VALUE &&
34888
- retryIoerr(&cnt, &lastErrno) ){
34972
+ winRetryIoerr(&cnt, &lastErrno) ){
3488934973
/* Noop */
3489034974
}
3489134975
#endif
3489234976
}
3489334977
#ifdef SQLITE_WIN32_HAS_ANSI
@@ -34896,24 +34980,25 @@
3489634980
dwDesiredAccess,
3489734981
dwShareMode, NULL,
3489834982
dwCreationDisposition,
3489934983
dwFlagsAndAttributes,
3490034984
NULL))==INVALID_HANDLE_VALUE &&
34901
- retryIoerr(&cnt, &lastErrno) ){
34985
+ winRetryIoerr(&cnt, &lastErrno) ){
3490234986
/* Noop */
3490334987
}
3490434988
}
3490534989
#endif
34906
- logIoerr(cnt);
34990
+ winLogIoerr(cnt);
3490734991
3490834992
OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
3490934993
dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
3491034994
3491134995
if( h==INVALID_HANDLE_VALUE ){
3491234996
pFile->lastErrno = lastErrno;
3491334997
winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
3491434998
sqlite3_free(zConverted);
34999
+ sqlite3_free(zTmpname);
3491535000
if( isReadWrite && !isExclusive ){
3491635001
return winOpen(pVfs, zName, id,
3491735002
((flags|SQLITE_OPEN_READONLY) &
3491835003
~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
3491935004
pOutFlags);
@@ -34938,19 +35023,21 @@
3493835023
if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
3493935024
&& (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
3494035025
){
3494135026
osCloseHandle(h);
3494235027
sqlite3_free(zConverted);
35028
+ sqlite3_free(zTmpname);
3494335029
OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
3494435030
return rc;
3494535031
}
3494635032
if( isTemp ){
3494735033
pFile->zDeleteOnClose = zConverted;
3494835034
}else
3494935035
#endif
3495035036
{
3495135037
sqlite3_free(zConverted);
35038
+ sqlite3_free(zTmpname);
3495235039
}
3495335040
3495435041
pFile->pMethod = &winIoMethod;
3495535042
pFile->pVfs = pVfs;
3495635043
pFile->h = h;
@@ -35000,15 +35087,16 @@
3500035087
UNUSED_PARAMETER(syncDir);
3500135088
3500235089
SimulateIOError(return SQLITE_IOERR_DELETE);
3500335090
OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
3500435091
35005
- zConverted = convertUtf8Filename(zFilename);
35092
+ zConverted = winConvertUtf8Filename(zFilename);
3500635093
if( zConverted==0 ){
35094
+ OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
3500735095
return SQLITE_IOERR_NOMEM;
3500835096
}
35009
- if( isNT() ){
35097
+ if( osIsNT() ){
3501035098
do {
3501135099
#if SQLITE_OS_WINRT
3501235100
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3501335101
memset(&sAttrData, 0, sizeof(sAttrData));
3501435102
if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35043,11 +35131,11 @@
3504335131
}
3504435132
if ( osDeleteFileW(zConverted) ){
3504535133
rc = SQLITE_OK; /* Deleted OK. */
3504635134
break;
3504735135
}
35048
- if ( !retryIoerr(&cnt, &lastErrno) ){
35136
+ if ( !winRetryIoerr(&cnt, &lastErrno) ){
3504935137
rc = SQLITE_ERROR; /* No more retries. */
3505035138
break;
3505135139
}
3505235140
} while(1);
3505335141
}
@@ -35071,11 +35159,11 @@
3507135159
}
3507235160
if ( osDeleteFileA(zConverted) ){
3507335161
rc = SQLITE_OK; /* Deleted OK. */
3507435162
break;
3507535163
}
35076
- if ( !retryIoerr(&cnt, &lastErrno) ){
35164
+ if ( !winRetryIoerr(&cnt, &lastErrno) ){
3507735165
rc = SQLITE_ERROR; /* No more retries. */
3507835166
break;
3507935167
}
3508035168
} while(1);
3508135169
}
@@ -35082,11 +35170,11 @@
3508235170
#endif
3508335171
if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
3508435172
rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
3508535173
"winDelete", zFilename);
3508635174
}else{
35087
- logIoerr(cnt);
35175
+ winLogIoerr(cnt);
3508835176
}
3508935177
sqlite3_free(zConverted);
3509035178
OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
3509135179
return rc;
3509235180
}
@@ -35108,22 +35196,22 @@
3510835196
3510935197
SimulateIOError( return SQLITE_IOERR_ACCESS; );
3511035198
OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
3511135199
zFilename, flags, pResOut));
3511235200
35113
- zConverted = convertUtf8Filename(zFilename);
35201
+ zConverted = winConvertUtf8Filename(zFilename);
3511435202
if( zConverted==0 ){
3511535203
OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
3511635204
return SQLITE_IOERR_NOMEM;
3511735205
}
35118
- if( isNT() ){
35206
+ if( osIsNT() ){
3511935207
int cnt = 0;
3512035208
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3512135209
memset(&sAttrData, 0, sizeof(sAttrData));
3512235210
while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
3512335211
GetFileExInfoStandard,
35124
- &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
35212
+ &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
3512535213
if( rc ){
3512635214
/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
3512735215
** as if it does not exist.
3512835216
*/
3512935217
if( flags==SQLITE_ACCESS_EXISTS
@@ -35132,11 +35220,11 @@
3513235220
attr = INVALID_FILE_ATTRIBUTES;
3513335221
}else{
3513435222
attr = sAttrData.dwFileAttributes;
3513535223
}
3513635224
}else{
35137
- logIoerr(cnt);
35225
+ winLogIoerr(cnt);
3513835226
if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
3513935227
winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
3514035228
sqlite3_free(zConverted);
3514135229
return SQLITE_IOERR_ACCESS;
3514235230
}else{
@@ -35183,11 +35271,11 @@
3518335271
** a legal UNC name, a volume relative path, or an absolute path name in the
3518435272
** "Unix" format on Windows. There is no easy way to differentiate between
3518535273
** the final two cases; therefore, we return the safer return value of TRUE
3518635274
** so that callers of this function will simply use it verbatim.
3518735275
*/
35188
- if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
35276
+ if ( winIsDirSep(zPathname[0]) ){
3518935277
return TRUE;
3519035278
}
3519135279
3519235280
/*
3519335281
** If the path name starts with a letter and a colon it is either a volume
@@ -35219,28 +35307,33 @@
3521935307
){
3522035308
3522135309
#if defined(__CYGWIN__)
3522235310
SimulateIOError( return SQLITE_ERROR );
3522335311
UNUSED_PARAMETER(nFull);
35224
- assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
3522535312
assert( nFull>=pVfs->mxPathname );
3522635313
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
3522735314
/*
3522835315
** NOTE: We are dealing with a relative path name and the data
3522935316
** directory has been set. Therefore, use it as the basis
3523035317
** for converting the relative path name to an absolute
3523135318
** one by prepending the data directory and a slash.
3523235319
*/
35233
- char zOut[SQLITE_WIN32_MAX_PATH+1];
35320
+ char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
35321
+ if( !zOut ){
35322
+ winLogError(SQLITE_IOERR_NOMEM, 0, "winFullPathname", zRelative);
35323
+ return SQLITE_IOERR_NOMEM;
35324
+ }
3523435325
if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35235
- SQLITE_WIN32_MAX_PATH+1)<0 ){
35326
+ pVfs->mxPathname+1)<0 ){
3523635327
winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
3523735328
zRelative);
35329
+ sqlite3_free(zOut);
3523835330
return SQLITE_CANTOPEN_FULLPATH;
3523935331
}
35240
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35241
- sqlite3_data_directory, zOut);
35332
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35333
+ sqlite3_data_directory, winGetDirDep(), zOut);
35334
+ sqlite3_free(zOut);
3524235335
}else{
3524335336
if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
3524435337
winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
3524535338
zRelative);
3524635339
return SQLITE_CANTOPEN_FULLPATH;
@@ -35258,12 +35351,12 @@
3525835351
** NOTE: We are dealing with a relative path name and the data
3525935352
** directory has been set. Therefore, use it as the basis
3526035353
** for converting the relative path name to an absolute
3526135354
** one by prepending the data directory and a backslash.
3526235355
*/
35263
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35264
- sqlite3_data_directory, zRelative);
35356
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35357
+ sqlite3_data_directory, winGetDirDep(), zRelative);
3526535358
}else{
3526635359
sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
3526735360
}
3526835361
return SQLITE_OK;
3526935362
#endif
@@ -35291,19 +35384,19 @@
3529135384
** NOTE: We are dealing with a relative path name and the data
3529235385
** directory has been set. Therefore, use it as the basis
3529335386
** for converting the relative path name to an absolute
3529435387
** one by prepending the data directory and a backslash.
3529535388
*/
35296
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35297
- sqlite3_data_directory, zRelative);
35389
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35390
+ sqlite3_data_directory, winGetDirDep(), zRelative);
3529835391
return SQLITE_OK;
3529935392
}
35300
- zConverted = convertUtf8Filename(zRelative);
35393
+ zConverted = winConvertUtf8Filename(zRelative);
3530135394
if( zConverted==0 ){
3530235395
return SQLITE_IOERR_NOMEM;
3530335396
}
35304
- if( isNT() ){
35397
+ if( osIsNT() ){
3530535398
LPWSTR zTemp;
3530635399
nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
3530735400
if( nByte==0 ){
3530835401
winLogError(SQLITE_ERROR, osGetLastError(),
3530935402
"GetFullPathNameW1", zConverted);
@@ -35323,11 +35416,11 @@
3532335416
sqlite3_free(zConverted);
3532435417
sqlite3_free(zTemp);
3532535418
return SQLITE_CANTOPEN_FULLPATH;
3532635419
}
3532735420
sqlite3_free(zConverted);
35328
- zOut = unicodeToUtf8(zTemp);
35421
+ zOut = winUnicodeToUtf8(zTemp);
3532935422
sqlite3_free(zTemp);
3533035423
}
3533135424
#ifdef SQLITE_WIN32_HAS_ANSI
3533235425
else{
3533335426
char *zTemp;
@@ -35376,16 +35469,16 @@
3537635469
** Interfaces for opening a shared library, finding entry points
3537735470
** within the shared library, and closing the shared library.
3537835471
*/
3537935472
static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
3538035473
HANDLE h;
35381
- void *zConverted = convertUtf8Filename(zFilename);
35474
+ void *zConverted = winConvertUtf8Filename(zFilename);
3538235475
UNUSED_PARAMETER(pVfs);
3538335476
if( zConverted==0 ){
3538435477
return 0;
3538535478
}
35386
- if( isNT() ){
35479
+ if( osIsNT() ){
3538735480
#if SQLITE_OS_WINRT
3538835481
h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
3538935482
#else
3539035483
h = osLoadLibraryW((LPCWSTR)zConverted);
3539135484
#endif
@@ -35398,11 +35491,11 @@
3539835491
sqlite3_free(zConverted);
3539935492
return (void*)h;
3540035493
}
3540135494
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
3540235495
UNUSED_PARAMETER(pVfs);
35403
- getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35496
+ winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
3540435497
}
3540535498
static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
3540635499
UNUSED_PARAMETER(pVfs);
3540735500
return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
3540835501
}
@@ -35574,21 +35667,21 @@
3557435667
** by sqlite into the error message available to the user using
3557535668
** sqlite3_errmsg(), possibly making IO errors easier to debug.
3557635669
*/
3557735670
static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
3557835671
UNUSED_PARAMETER(pVfs);
35579
- return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
35672
+ return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
3558035673
}
3558135674
3558235675
/*
3558335676
** Initialize and deinitialize the operating system interface.
3558435677
*/
3558535678
SQLITE_API int sqlite3_os_init(void){
3558635679
static sqlite3_vfs winVfs = {
3558735680
3, /* iVersion */
3558835681
sizeof(winFile), /* szOsFile */
35589
- SQLITE_WIN32_MAX_PATH, /* mxPathname */
35682
+ SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
3559035683
0, /* pNext */
3559135684
"win32", /* zName */
3559235685
0, /* pAppData */
3559335686
winOpen, /* xOpen */
3559435687
winDelete, /* xDelete */
@@ -35605,10 +35698,36 @@
3560535698
winCurrentTimeInt64, /* xCurrentTimeInt64 */
3560635699
winSetSystemCall, /* xSetSystemCall */
3560735700
winGetSystemCall, /* xGetSystemCall */
3560835701
winNextSystemCall, /* xNextSystemCall */
3560935702
};
35703
+#if defined(SQLITE_WIN32_HAS_WIDE)
35704
+ static sqlite3_vfs winLongPathVfs = {
35705
+ 3, /* iVersion */
35706
+ sizeof(winFile), /* szOsFile */
35707
+ SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
35708
+ 0, /* pNext */
35709
+ "win32-longpath", /* zName */
35710
+ 0, /* pAppData */
35711
+ winOpen, /* xOpen */
35712
+ winDelete, /* xDelete */
35713
+ winAccess, /* xAccess */
35714
+ winFullPathname, /* xFullPathname */
35715
+ winDlOpen, /* xDlOpen */
35716
+ winDlError, /* xDlError */
35717
+ winDlSym, /* xDlSym */
35718
+ winDlClose, /* xDlClose */
35719
+ winRandomness, /* xRandomness */
35720
+ winSleep, /* xSleep */
35721
+ winCurrentTime, /* xCurrentTime */
35722
+ winGetLastError, /* xGetLastError */
35723
+ winCurrentTimeInt64, /* xCurrentTimeInt64 */
35724
+ winSetSystemCall, /* xSetSystemCall */
35725
+ winGetSystemCall, /* xGetSystemCall */
35726
+ winNextSystemCall, /* xNextSystemCall */
35727
+ };
35728
+#endif
3561035729
3561135730
/* Double-check that the aSyscall[] array has been constructed
3561235731
** correctly. See ticket [bb3a86e890c8e96ab] */
3561335732
assert( ArraySize(aSyscall)==74 );
3561435733
@@ -35621,10 +35740,15 @@
3562135740
#endif
3562235741
assert( winSysInfo.dwAllocationGranularity>0 );
3562335742
assert( winSysInfo.dwPageSize>0 );
3562435743
3562535744
sqlite3_vfs_register(&winVfs, 1);
35745
+
35746
+#if defined(SQLITE_WIN32_HAS_WIDE)
35747
+ sqlite3_vfs_register(&winLongPathVfs, 0);
35748
+#endif
35749
+
3562635750
return SQLITE_OK;
3562735751
}
3562835752
3562935753
SQLITE_API int sqlite3_os_end(void){
3563035754
#if SQLITE_OS_WINRT
@@ -37434,10 +37558,11 @@
3743437558
}
3743537559
3743637560
if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
3743737561
goto fetch_out;
3743837562
}
37563
+ assert( pCache->nHash>0 && pCache->apHash );
3743937564
3744037565
/* Step 4. Try to recycle a page. */
3744137566
if( pCache->bPurgeable && pGroup->pLruTail && (
3744237567
(pCache->nPage+1>=pCache->nMax)
3744337568
|| pGroup->nCurrentPage>=pGroup->nMaxPage
@@ -38794,10 +38919,17 @@
3879438919
#ifndef SQLITE_OMIT_WAL
3879538920
u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
3879638921
#endif
3879738922
};
3879838923
38924
+/*
38925
+** Bits of the Pager.doNotSpill flag. See further description below.
38926
+*/
38927
+#define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
38928
+#define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
38929
+#define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
38930
+
3879938931
/*
3880038932
** A open page cache is an instance of struct Pager. A description of
3880138933
** some of the more important member variables follows:
3880238934
**
3880338935
** eState
@@ -38860,23 +38992,25 @@
3886038992
** The flag is cleared as soon as the journal file is finalized (either
3886138993
** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
3886238994
** journal file from being successfully finalized, the setMaster flag
3886338995
** is cleared anyway (and the pager will move to ERROR state).
3886438996
**
38865
-** doNotSpill, doNotSyncSpill
38866
-**
38867
-** These two boolean variables control the behavior of cache-spills
38868
-** (calls made by the pcache module to the pagerStress() routine to
38869
-** write cached data to the file-system in order to free up memory).
38870
-**
38871
-** When doNotSpill is non-zero, writing to the database from pagerStress()
38872
-** is disabled altogether. This is done in a very obscure case that
38997
+** doNotSpill
38998
+**
38999
+** This variables control the behavior of cache-spills (calls made by
39000
+** the pcache module to the pagerStress() routine to write cached data
39001
+** to the file-system in order to free up memory).
39002
+**
39003
+** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
39004
+** writing to the database from pagerStress() is disabled altogether.
39005
+** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
3887339006
** comes up during savepoint rollback that requires the pcache module
3887439007
** to allocate a new page to prevent the journal file from being written
38875
-** while it is being traversed by code in pager_playback().
39008
+** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
39009
+** case is a user preference.
3887639010
**
38877
-** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
39011
+** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
3887839012
** is permitted, but syncing the journal file is not. This flag is set
3887939013
** by sqlite3PagerWrite() when the file-system sector-size is larger than
3888039014
** the database page-size in order to prevent a journal sync from happening
3888139015
** in between the journalling of two pages on the same sector.
3888239016
**
@@ -38976,11 +39110,10 @@
3897639110
u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
3897739111
u8 eLock; /* Current lock held on database file */
3897839112
u8 changeCountDone; /* Set after incrementing the change-counter */
3897939113
u8 setMaster; /* True if a m-j name has been written to jrnl */
3898039114
u8 doNotSpill; /* Do not spill the cache when non-zero */
38981
- u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
3898239115
u8 subjInMemory; /* True to use in-memory sub-journals */
3898339116
Pgno dbSize; /* Number of pages in the database */
3898439117
Pgno dbOrigSize; /* dbSize before the current transaction */
3898539118
Pgno dbFileSize; /* Number of pages in the database file */
3898639119
Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
@@ -39355,17 +39488,21 @@
3935539488
** * The page-number is less than or equal to PagerSavepoint.nOrig, and
3935639489
** * The bit corresponding to the page-number is not set in
3935739490
** PagerSavepoint.pInSavepoint.
3935839491
*/
3935939492
static int subjRequiresPage(PgHdr *pPg){
39360
- Pgno pgno = pPg->pgno;
3936139493
Pager *pPager = pPg->pPager;
39494
+ PagerSavepoint *p;
39495
+ Pgno pgno;
3936239496
int i;
39363
- for(i=0; i<pPager->nSavepoint; i++){
39364
- PagerSavepoint *p = &pPager->aSavepoint[i];
39365
- if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
39366
- return 1;
39497
+ if( pPager->nSavepoint ){
39498
+ pgno = pPg->pgno;
39499
+ for(i=0; i<pPager->nSavepoint; i++){
39500
+ p = &pPager->aSavepoint[i];
39501
+ if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
39502
+ return 1;
39503
+ }
3936739504
}
3936839505
}
3936939506
return 0;
3937039507
}
3937139508
@@ -40636,15 +40773,15 @@
4063640773
** the data just read from the sub-journal. Mark the page as dirty
4063740774
** and if the pager requires a journal-sync, then mark the page as
4063840775
** requiring a journal-sync before it is written.
4063940776
*/
4064040777
assert( isSavepnt );
40641
- assert( pPager->doNotSpill==0 );
40642
- pPager->doNotSpill++;
40778
+ assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
40779
+ pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
4064340780
rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
40644
- assert( pPager->doNotSpill==1 );
40645
- pPager->doNotSpill--;
40781
+ assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
40782
+ pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
4064640783
if( rc!=SQLITE_OK ) return rc;
4064740784
pPg->flags &= ~PGHDR_NEED_READ;
4064840785
sqlite3PcacheMakeDirty(pPg);
4064940786
}
4065040787
if( pPg ){
@@ -41207,16 +41344,10 @@
4120741344
int pgsz = pPager->pageSize; /* Number of bytes to read */
4120841345
4120941346
assert( pPager->eState>=PAGER_READER && !MEMDB );
4121041347
assert( isOpen(pPager->fd) );
4121141348
41212
- if( NEVER(!isOpen(pPager->fd)) ){
41213
- assert( pPager->tempFile );
41214
- memset(pPg->pData, 0, pPager->pageSize);
41215
- return SQLITE_OK;
41216
- }
41217
-
4121841349
#ifndef SQLITE_OMIT_WAL
4121941350
if( iFrame ){
4122041351
/* Try to pull the page from the write-ahead log. */
4122141352
rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
4122241353
}else
@@ -41745,13 +41876,16 @@
4174541876
SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
4174641877
sqlite3PcacheShrink(pPager->pPCache);
4174741878
}
4174841879
4174941880
/*
41750
-** Adjust the robustness of the database to damage due to OS crashes
41751
-** or power failures by changing the number of syncs()s when writing
41752
-** the rollback journal. There are three levels:
41881
+** Adjust settings of the pager to those specified in the pgFlags parameter.
41882
+**
41883
+** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
41884
+** of the database to damage due to OS crashes or power failures by
41885
+** changing the number of syncs()s when writing the journals.
41886
+** There are three levels:
4175341887
**
4175441888
** OFF sqlite3OsSync() is never called. This is the default
4175541889
** for temporary and transient files.
4175641890
**
4175741891
** NORMAL The journal is synced once before writes begin on the
@@ -41788,26 +41922,25 @@
4178841922
**
4178941923
** Numeric values associated with these states are OFF==1, NORMAL=2,
4179041924
** and FULL=3.
4179141925
*/
4179241926
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
41793
-SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
41927
+SQLITE_PRIVATE void sqlite3PagerSetFlags(
4179441928
Pager *pPager, /* The pager to set safety level for */
41795
- int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
41796
- int bFullFsync, /* PRAGMA fullfsync */
41797
- int bCkptFullFsync /* PRAGMA checkpoint_fullfsync */
41929
+ unsigned pgFlags /* Various flags */
4179841930
){
41931
+ unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
4179941932
assert( level>=1 && level<=3 );
4180041933
pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
4180141934
pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
4180241935
if( pPager->noSync ){
4180341936
pPager->syncFlags = 0;
4180441937
pPager->ckptSyncFlags = 0;
41805
- }else if( bFullFsync ){
41938
+ }else if( pgFlags & PAGER_FULLFSYNC ){
4180641939
pPager->syncFlags = SQLITE_SYNC_FULL;
4180741940
pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41808
- }else if( bCkptFullFsync ){
41941
+ }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
4180941942
pPager->syncFlags = SQLITE_SYNC_NORMAL;
4181041943
pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
4181141944
}else{
4181241945
pPager->syncFlags = SQLITE_SYNC_NORMAL;
4181341946
pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
@@ -41814,10 +41947,15 @@
4181441947
}
4181541948
pPager->walSyncFlags = pPager->syncFlags;
4181641949
if( pPager->fullSync ){
4181741950
pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
4181841951
}
41952
+ if( pgFlags & PAGER_CACHESPILL ){
41953
+ pPager->doNotSpill &= ~SPILLFLAG_OFF;
41954
+ }else{
41955
+ pPager->doNotSpill |= SPILLFLAG_OFF;
41956
+ }
4181941957
}
4182041958
#endif
4182141959
4182241960
/*
4182341961
** The following global variable is incremented whenever the library
@@ -42714,28 +42852,34 @@
4271442852
int rc = SQLITE_OK;
4271542853
4271642854
assert( pPg->pPager==pPager );
4271742855
assert( pPg->flags&PGHDR_DIRTY );
4271842856
42719
- /* The doNotSyncSpill flag is set during times when doing a sync of
42857
+ /* The doNotSpill NOSYNC bit is set during times when doing a sync of
4272042858
** journal (and adding a new header) is not allowed. This occurs
4272142859
** during calls to sqlite3PagerWrite() while trying to journal multiple
4272242860
** pages belonging to the same sector.
4272342861
**
42724
- ** The doNotSpill flag inhibits all cache spilling regardless of whether
42725
- ** or not a sync is required. This is set during a rollback.
42862
+ ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
42863
+ ** regardless of whether or not a sync is required. This is set during
42864
+ ** a rollback or by user request, respectively.
4272642865
**
4272742866
** Spilling is also prohibited when in an error state since that could
4272842867
** lead to database corruption. In the current implementaton it
4272942868
** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
4273042869
** while in the error state, hence it is impossible for this routine to
4273142870
** be called in the error state. Nevertheless, we include a NEVER()
4273242871
** test for the error state as a safeguard against future changes.
4273342872
*/
4273442873
if( NEVER(pPager->errCode) ) return SQLITE_OK;
42735
- if( pPager->doNotSpill ) return SQLITE_OK;
42736
- if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
42874
+ testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
42875
+ testcase( pPager->doNotSpill & SPILLFLAG_OFF );
42876
+ testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
42877
+ if( pPager->doNotSpill
42878
+ && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
42879
+ || (pPg->flags & PGHDR_NEED_SYNC)!=0)
42880
+ ){
4273742881
return SQLITE_OK;
4273842882
}
4273942883
4274042884
pPg->pDirty = 0;
4274142885
if( pagerUseWal(pPager) ){
@@ -43553,23 +43697,23 @@
4355343697
*/
4355443698
SQLITE_PRIVATE int sqlite3PagerAcquire(
4355543699
Pager *pPager, /* The pager open on the database file */
4355643700
Pgno pgno, /* Page number to fetch */
4355743701
DbPage **ppPage, /* Write a pointer to the page here */
43558
- int flags /* PAGER_ACQUIRE_XXX flags */
43702
+ int flags /* PAGER_GET_XXX flags */
4355943703
){
4356043704
int rc = SQLITE_OK;
4356143705
PgHdr *pPg = 0;
4356243706
u32 iFrame = 0; /* Frame to read from WAL file */
43563
- const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);
43707
+ const int noContent = (flags & PAGER_GET_NOCONTENT);
4356443708
4356543709
/* It is acceptable to use a read-only (mmap) page for any page except
4356643710
** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
4356743711
** flag was specified by the caller. And so long as the db is not a
4356843712
** temporary or in-memory database. */
4356943713
const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
43570
- && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
43714
+ && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
4357143715
#ifdef SQLITE_HAS_CODEC
4357243716
&& pPager->xCodec==0
4357343717
#endif
4357443718
);
4357543719
@@ -44085,17 +44229,17 @@
4408544229
Pgno pg1; /* First page of the sector pPg is located on. */
4408644230
int nPage = 0; /* Number of pages starting at pg1 to journal */
4408744231
int ii; /* Loop counter */
4408844232
int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
4408944233
44090
- /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
44234
+ /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
4409144235
** a journal header to be written between the pages journaled by
4409244236
** this function.
4409344237
*/
4409444238
assert( !MEMDB );
44095
- assert( pPager->doNotSyncSpill==0 );
44096
- pPager->doNotSyncSpill++;
44239
+ assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
44240
+ pPager->doNotSpill |= SPILLFLAG_NOSYNC;
4409744241
4409844242
/* This trick assumes that both the page-size and sector-size are
4409944243
** an integer power of 2. It sets variable pg1 to the identifier
4410044244
** of the first page of the sector pPg is located on.
4410144245
*/
@@ -44150,12 +44294,12 @@
4415044294
sqlite3PagerUnref(pPage);
4415144295
}
4415244296
}
4415344297
}
4415444298
44155
- assert( pPager->doNotSyncSpill==1 );
44156
- pPager->doNotSyncSpill--;
44299
+ assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
44300
+ pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
4415744301
}else{
4415844302
rc = pager_write(pDbPage);
4415944303
}
4416044304
return rc;
4416144305
}
@@ -49147,18 +49291,23 @@
4914749291
};
4914849292
4914949293
/*
4915049294
** Potential values for BtCursor.eState.
4915149295
**
49152
-** CURSOR_VALID:
49153
-** Cursor points to a valid entry. getPayload() etc. may be called.
49154
-**
4915549296
** CURSOR_INVALID:
4915649297
** Cursor does not point to a valid entry. This can happen (for example)
4915749298
** because the table is empty or because BtreeCursorFirst() has not been
4915849299
** called.
4915949300
**
49301
+** CURSOR_VALID:
49302
+** Cursor points to a valid entry. getPayload() etc. may be called.
49303
+**
49304
+** CURSOR_SKIPNEXT:
49305
+** Cursor is valid except that the Cursor.skipNext field is non-zero
49306
+** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
49307
+** operation should be a no-op.
49308
+**
4916049309
** CURSOR_REQUIRESEEK:
4916149310
** The table that this cursor was opened on still exists, but has been
4916249311
** modified since the cursor was last used. The cursor position is saved
4916349312
** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
4916449313
** this state, restoreCursorPosition() can be called to attempt to
@@ -49171,12 +49320,13 @@
4917149320
** Do nothing else with this cursor. Any attempt to use the cursor
4917249321
** should return the error code stored in BtCursor.skip
4917349322
*/
4917449323
#define CURSOR_INVALID 0
4917549324
#define CURSOR_VALID 1
49176
-#define CURSOR_REQUIRESEEK 2
49177
-#define CURSOR_FAULT 3
49325
+#define CURSOR_SKIPNEXT 2
49326
+#define CURSOR_REQUIRESEEK 3
49327
+#define CURSOR_FAULT 4
4917849328
4917949329
/*
4918049330
** The database page the PENDING_BYTE occupies. This page is never used.
4918149331
*/
4918249332
# define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
@@ -50286,10 +50436,13 @@
5028650436
rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
5028750437
if( rc==SQLITE_OK ){
5028850438
sqlite3_free(pCur->pKey);
5028950439
pCur->pKey = 0;
5029050440
assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
50441
+ if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
50442
+ pCur->eState = CURSOR_SKIPNEXT;
50443
+ }
5029150444
}
5029250445
return rc;
5029350446
}
5029450447
5029550448
#define restoreCursorPosition(p) \
@@ -50311,11 +50464,11 @@
5031150464
rc = restoreCursorPosition(pCur);
5031250465
if( rc ){
5031350466
*pHasMoved = 1;
5031450467
return rc;
5031550468
}
50316
- if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
50469
+ if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
5031750470
*pHasMoved = 1;
5031850471
}else{
5031950472
*pHasMoved = 0;
5032050473
}
5032150474
return SQLITE_OK;
@@ -50499,11 +50652,12 @@
5049950652
assert( pPage->leaf==0 || pPage->leaf==1 );
5050050653
n = pPage->childPtrSize;
5050150654
assert( n==4-4*pPage->leaf );
5050250655
if( pPage->intKey ){
5050350656
if( pPage->hasData ){
50504
- n += getVarint32(&pCell[n], nPayload);
50657
+ assert( n==0 );
50658
+ n = getVarint32(pCell, nPayload);
5050550659
}else{
5050650660
nPayload = 0;
5050750661
}
5050850662
n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
5050950663
pInfo->nData = nPayload;
@@ -51143,19 +51297,16 @@
5114351297
*/
5114451298
static int btreeGetPage(
5114551299
BtShared *pBt, /* The btree */
5114651300
Pgno pgno, /* Number of the page to fetch */
5114751301
MemPage **ppPage, /* Return the page in this parameter */
51148
- int noContent, /* Do not load page content if true */
51149
- int bReadonly /* True if a read-only (mmap) page is ok */
51302
+ int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
5115051303
){
5115151304
int rc;
5115251305
DbPage *pDbPage;
51153
- int flags = (noContent ? PAGER_ACQUIRE_NOCONTENT : 0)
51154
- | (bReadonly ? PAGER_ACQUIRE_READONLY : 0);
5115551306
51156
- assert( noContent==0 || bReadonly==0 );
51307
+ assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
5115751308
assert( sqlite3_mutex_held(pBt->mutex) );
5115851309
rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
5115951310
if( rc ) return rc;
5116051311
*ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
5116151312
return SQLITE_OK;
@@ -51199,19 +51350,20 @@
5119951350
*/
5120051351
static int getAndInitPage(
5120151352
BtShared *pBt, /* The database file */
5120251353
Pgno pgno, /* Number of the page to get */
5120351354
MemPage **ppPage, /* Write the page pointer here */
51204
- int bReadonly /* True if a read-only (mmap) page is ok */
51355
+ int bReadonly /* PAGER_GET_READONLY or 0 */
5120551356
){
5120651357
int rc;
5120751358
assert( sqlite3_mutex_held(pBt->mutex) );
51359
+ assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
5120851360
5120951361
if( pgno>btreePagecount(pBt) ){
5121051362
rc = SQLITE_CORRUPT_BKPT;
5121151363
}else{
51212
- rc = btreeGetPage(pBt, pgno, ppPage, 0, bReadonly);
51364
+ rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
5121351365
if( rc==SQLITE_OK ){
5121451366
rc = btreeInitPage(*ppPage);
5121551367
if( rc!=SQLITE_OK ){
5121651368
releasePage(*ppPage);
5121751369
}
@@ -51727,21 +51879,18 @@
5172751879
** there is a high probability of damage) Level 2 is the default. There
5172851880
** is a very low but non-zero probability of damage. Level 3 reduces the
5172951881
** probability of damage to near zero but with a write performance reduction.
5173051882
*/
5173151883
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
51732
-SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
51884
+SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
5173351885
Btree *p, /* The btree to set the safety level on */
51734
- int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
51735
- int fullSync, /* PRAGMA fullfsync. */
51736
- int ckptFullSync /* PRAGMA checkpoint_fullfync */
51886
+ unsigned pgFlags /* Various PAGER_* flags */
5173751887
){
5173851888
BtShared *pBt = p->pBt;
5173951889
assert( sqlite3_mutex_held(p->db->mutex) );
51740
- assert( level>=1 && level<=3 );
5174151890
sqlite3BtreeEnter(p);
51742
- sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
51891
+ sqlite3PagerSetFlags(pBt->pPager, pgFlags);
5174351892
sqlite3BtreeLeave(p);
5174451893
return SQLITE_OK;
5174551894
}
5174651895
#endif
5174751896
@@ -51943,11 +52092,11 @@
5194352092
5194452093
assert( sqlite3_mutex_held(pBt->mutex) );
5194552094
assert( pBt->pPage1==0 );
5194652095
rc = sqlite3PagerSharedLock(pBt->pPager);
5194752096
if( rc!=SQLITE_OK ) return rc;
51948
- rc = btreeGetPage(pBt, 1, &pPage1, 0, 0);
52097
+ rc = btreeGetPage(pBt, 1, &pPage1, 0);
5194952098
if( rc!=SQLITE_OK ) return rc;
5195052099
5195152100
/* Do some checking to help insure the file we opened really is
5195252101
** a valid database file.
5195352102
*/
@@ -52071,10 +52220,11 @@
5207152220
pBt->max1bytePayload = (u8)pBt->maxLocal;
5207252221
}
5207352222
assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
5207452223
pBt->pPage1 = pPage1;
5207552224
pBt->nPage = nPage;
52225
+assert( pPage1->leaf==0 || pPage1->leaf==1 );
5207652226
return SQLITE_OK;
5207752227
5207852228
page1_init_failed:
5207952229
releasePage(pPage1);
5208052230
pBt->pPage1 = 0;
@@ -52525,11 +52675,11 @@
5252552675
/* Fix the database pointer on page iPtrPage that pointed at iDbPage so
5252652676
** that it points at iFreePage. Also fix the pointer map entry for
5252752677
** iPtrPage.
5252852678
*/
5252952679
if( eType!=PTRMAP_ROOTPAGE ){
52530
- rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0, 0);
52680
+ rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
5253152681
if( rc!=SQLITE_OK ){
5253252682
return rc;
5253352683
}
5253452684
rc = sqlite3PagerWrite(pPtrPage->pDbPage);
5253552685
if( rc!=SQLITE_OK ){
@@ -52609,11 +52759,11 @@
5260952759
Pgno iFreePg; /* Index of free page to move pLastPg to */
5261052760
MemPage *pLastPg;
5261152761
u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
5261252762
Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
5261352763
52614
- rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0, 0);
52764
+ rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
5261552765
if( rc!=SQLITE_OK ){
5261652766
return rc;
5261752767
}
5261852768
5261952769
/* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -53008,11 +53158,11 @@
5300853158
}
5300953159
5301053160
/* The rollback may have destroyed the pPage1->aData value. So
5301153161
** call btreeGetPage() on page 1 again to make
5301253162
** sure pPage1->aData is set correctly. */
53013
- if( btreeGetPage(pBt, 1, &pPage1, 0, 0)==SQLITE_OK ){
53163
+ if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
5301453164
int nPage = get4byte(28+(u8*)pPage1->aData);
5301553165
testcase( nPage==0 );
5301653166
if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
5301753167
testcase( pBt->nPage!=nPage );
5301853168
pBt->nPage = nPage;
@@ -53443,11 +53593,11 @@
5344353593
}
5344453594
#endif
5344553595
5344653596
assert( next==0 || rc==SQLITE_DONE );
5344753597
if( rc==SQLITE_OK ){
53448
- rc = btreeGetPage(pBt, ovfl, &pPage, 0, (ppPage==0));
53598
+ rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
5344953599
assert( rc==SQLITE_OK || pPage==0 );
5345053600
if( rc==SQLITE_OK ){
5345153601
next = get4byte(pPage->aData);
5345253602
}
5345353603
}
@@ -53665,11 +53815,11 @@
5366553815
#endif
5366653816
5366753817
{
5366853818
DbPage *pDbPage;
5366953819
rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
53670
- (eOp==0 ? PAGER_ACQUIRE_READONLY : 0)
53820
+ (eOp==0 ? PAGER_GET_READONLY : 0)
5367153821
);
5367253822
if( rc==SQLITE_OK ){
5367353823
aPayload = sqlite3PagerGetData(pDbPage);
5367453824
nextPage = get4byte(aPayload);
5367553825
rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
@@ -53849,11 +53999,12 @@
5384953999
assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
5385054000
assert( pCur->iPage>=0 );
5385154001
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
5385254002
return SQLITE_CORRUPT_BKPT;
5385354003
}
53854
- rc = getAndInitPage(pBt, newPgno, &pNewPage, (pCur->wrFlag==0));
54004
+ rc = getAndInitPage(pBt, newPgno, &pNewPage,
54005
+ pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
5385554006
if( rc ) return rc;
5385654007
pCur->apPage[i+1] = pNewPage;
5385754008
pCur->aiIdx[i+1] = 0;
5385854009
pCur->iPage++;
5385954010
@@ -53966,11 +54117,12 @@
5396654117
pCur->iPage = 0;
5396754118
}else if( pCur->pgnoRoot==0 ){
5396854119
pCur->eState = CURSOR_INVALID;
5396954120
return SQLITE_OK;
5397054121
}else{
53971
- rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0], pCur->wrFlag==0);
54122
+ rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0],
54123
+ pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
5397254124
if( rc!=SQLITE_OK ){
5397354125
pCur->eState = CURSOR_INVALID;
5397454126
return rc;
5397554127
}
5397654128
pCur->iPage = 0;
@@ -54361,25 +54513,33 @@
5436154513
int rc;
5436254514
int idx;
5436354515
MemPage *pPage;
5436454516
5436554517
assert( cursorHoldsMutex(pCur) );
54366
- rc = restoreCursorPosition(pCur);
54367
- if( rc!=SQLITE_OK ){
54368
- return rc;
54369
- }
5437054518
assert( pRes!=0 );
54371
- if( CURSOR_INVALID==pCur->eState ){
54372
- *pRes = 1;
54373
- return SQLITE_OK;
54374
- }
54375
- if( pCur->skipNext>0 ){
54376
- pCur->skipNext = 0;
54377
- *pRes = 0;
54378
- return SQLITE_OK;
54379
- }
54380
- pCur->skipNext = 0;
54519
+ assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
54520
+ if( pCur->eState!=CURSOR_VALID ){
54521
+ rc = restoreCursorPosition(pCur);
54522
+ if( rc!=SQLITE_OK ){
54523
+ *pRes = 0;
54524
+ return rc;
54525
+ }
54526
+ if( CURSOR_INVALID==pCur->eState ){
54527
+ *pRes = 1;
54528
+ return SQLITE_OK;
54529
+ }
54530
+ if( pCur->skipNext ){
54531
+ assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
54532
+ pCur->eState = CURSOR_VALID;
54533
+ if( pCur->skipNext>0 ){
54534
+ pCur->skipNext = 0;
54535
+ *pRes = 0;
54536
+ return SQLITE_OK;
54537
+ }
54538
+ pCur->skipNext = 0;
54539
+ }
54540
+ }
5438154541
5438254542
pPage = pCur->apPage[pCur->iPage];
5438354543
idx = ++pCur->aiIdx[pCur->iPage];
5438454544
assert( pPage->isInit );
5438554545
@@ -54393,11 +54553,14 @@
5439354553
pCur->info.nSize = 0;
5439454554
pCur->validNKey = 0;
5439554555
if( idx>=pPage->nCell ){
5439654556
if( !pPage->leaf ){
5439754557
rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
54398
- if( rc ) return rc;
54558
+ if( rc ){
54559
+ *pRes = 0;
54560
+ return rc;
54561
+ }
5439954562
rc = moveToLeftmost(pCur);
5440054563
*pRes = 0;
5440154564
return rc;
5440254565
}
5440354566
do{
@@ -54435,32 +54598,44 @@
5443554598
SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
5443654599
int rc;
5443754600
MemPage *pPage;
5443854601
5443954602
assert( cursorHoldsMutex(pCur) );
54440
- rc = restoreCursorPosition(pCur);
54441
- if( rc!=SQLITE_OK ){
54442
- return rc;
54443
- }
54603
+ assert( pRes!=0 );
54604
+ assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
5444454605
pCur->atLast = 0;
54445
- if( CURSOR_INVALID==pCur->eState ){
54446
- *pRes = 1;
54447
- return SQLITE_OK;
54448
- }
54449
- if( pCur->skipNext<0 ){
54450
- pCur->skipNext = 0;
54451
- *pRes = 0;
54452
- return SQLITE_OK;
54453
- }
54454
- pCur->skipNext = 0;
54606
+ if( pCur->eState!=CURSOR_VALID ){
54607
+ if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
54608
+ rc = btreeRestoreCursorPosition(pCur);
54609
+ if( rc!=SQLITE_OK ){
54610
+ *pRes = 0;
54611
+ return rc;
54612
+ }
54613
+ }
54614
+ if( CURSOR_INVALID==pCur->eState ){
54615
+ *pRes = 1;
54616
+ return SQLITE_OK;
54617
+ }
54618
+ if( pCur->skipNext ){
54619
+ assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
54620
+ pCur->eState = CURSOR_VALID;
54621
+ if( pCur->skipNext<0 ){
54622
+ pCur->skipNext = 0;
54623
+ *pRes = 0;
54624
+ return SQLITE_OK;
54625
+ }
54626
+ pCur->skipNext = 0;
54627
+ }
54628
+ }
5445554629
5445654630
pPage = pCur->apPage[pCur->iPage];
5445754631
assert( pPage->isInit );
5445854632
if( !pPage->leaf ){
5445954633
int idx = pCur->aiIdx[pCur->iPage];
5446054634
rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
5446154635
if( rc ){
54636
+ *pRes = 0;
5446254637
return rc;
5446354638
}
5446454639
rc = moveToRightmost(pCur);
5446554640
}else{
5446654641
while( pCur->aiIdx[pCur->iPage]==0 ){
@@ -54580,11 +54755,11 @@
5458054755
}
5458154756
testcase( iTrunk==mxPage );
5458254757
if( iTrunk>mxPage ){
5458354758
rc = SQLITE_CORRUPT_BKPT;
5458454759
}else{
54585
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
54760
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
5458654761
}
5458754762
if( rc ){
5458854763
pTrunk = 0;
5458954764
goto end_allocate_page;
5459054765
}
@@ -54644,11 +54819,11 @@
5464454819
if( iNewTrunk>mxPage ){
5464554820
rc = SQLITE_CORRUPT_BKPT;
5464654821
goto end_allocate_page;
5464754822
}
5464854823
testcase( iNewTrunk==mxPage );
54649
- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0, 0);
54824
+ rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
5465054825
if( rc!=SQLITE_OK ){
5465154826
goto end_allocate_page;
5465254827
}
5465354828
rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5465454829
if( rc!=SQLITE_OK ){
@@ -54723,12 +54898,12 @@
5472354898
if( rc ) goto end_allocate_page;
5472454899
if( closest<k-1 ){
5472554900
memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5472654901
}
5472754902
put4byte(&aData[4], k-1);
54728
- noContent = !btreeGetHasContent(pBt, *pPgno);
54729
- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent, 0);
54903
+ noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
54904
+ rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
5473054905
if( rc==SQLITE_OK ){
5473154906
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5473254907
if( rc!=SQLITE_OK ){
5473354908
releasePage(*ppPage);
5473454909
}
@@ -54756,11 +54931,11 @@
5475654931
** content for any page that really does lie past the end of the database
5475754932
** file on disk. So the effects of disabling the no-content optimization
5475854933
** here are confined to those pages that lie between the end of the
5475954934
** database image and the end of the database file.
5476054935
*/
54761
- int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate));
54936
+ int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
5476254937
5476354938
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
5476454939
if( rc ) return rc;
5476554940
pBt->nPage++;
5476654941
if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -54772,11 +54947,11 @@
5477254947
** becomes a new pointer-map page, the second is used by the caller.
5477354948
*/
5477454949
MemPage *pPg = 0;
5477554950
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
5477654951
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
54777
- rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent, 0);
54952
+ rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
5477854953
if( rc==SQLITE_OK ){
5477954954
rc = sqlite3PagerWrite(pPg->pDbPage);
5478054955
releasePage(pPg);
5478154956
}
5478254957
if( rc ) return rc;
@@ -54786,11 +54961,11 @@
5478654961
#endif
5478754962
put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
5478854963
*pPgno = pBt->nPage;
5478954964
5479054965
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
54791
- rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent, 0);
54966
+ rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
5479254967
if( rc ) return rc;
5479354968
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5479454969
if( rc!=SQLITE_OK ){
5479554970
releasePage(*ppPage);
5479654971
}
@@ -54854,11 +55029,11 @@
5485455029
5485555030
if( pBt->btsFlags & BTS_SECURE_DELETE ){
5485655031
/* If the secure_delete option is enabled, then
5485755032
** always fully overwrite deleted information with zeros.
5485855033
*/
54859
- if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0, 0))!=0) )
55034
+ if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
5486055035
|| ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
5486155036
){
5486255037
goto freepage_out;
5486355038
}
5486455039
memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -54881,11 +55056,11 @@
5488155056
*/
5488255057
if( nFree!=0 ){
5488355058
u32 nLeaf; /* Initial number of leaf cells on trunk page */
5488455059
5488555060
iTrunk = get4byte(&pPage1->aData[32]);
54886
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
55061
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
5488755062
if( rc!=SQLITE_OK ){
5488855063
goto freepage_out;
5488955064
}
5489055065
5489155066
nLeaf = get4byte(&pTrunk->aData[4]);
@@ -54927,11 +55102,11 @@
5492755102
** the page being freed as a leaf page of the first trunk in the free-list.
5492855103
** Possibly because the free-list is empty, or possibly because the
5492955104
** first trunk in the free-list is full. Either way, the page being freed
5493055105
** will become the new first trunk page in the free-list.
5493155106
*/
54932
- if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0, 0)) ){
55107
+ if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
5493355108
goto freepage_out;
5493455109
}
5493555110
rc = sqlite3PagerWrite(pPage->pDbPage);
5493655111
if( rc!=SQLITE_OK ){
5493755112
goto freepage_out;
@@ -56826,11 +57001,11 @@
5682657001
if( rc!=SQLITE_OK ){
5682757002
return rc;
5682857003
}
5682957004
5683057005
/* Move the page currently at pgnoRoot to pgnoMove. */
56831
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
57006
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
5683257007
if( rc!=SQLITE_OK ){
5683357008
return rc;
5683457009
}
5683557010
rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
5683657011
if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -56847,11 +57022,11 @@
5684757022
5684857023
/* Obtain the page at pgnoRoot */
5684957024
if( rc!=SQLITE_OK ){
5685057025
return rc;
5685157026
}
56852
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
57027
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
5685357028
if( rc!=SQLITE_OK ){
5685457029
return rc;
5685557030
}
5685657031
rc = sqlite3PagerWrite(pRoot->pDbPage);
5685757032
if( rc!=SQLITE_OK ){
@@ -57025,11 +57200,11 @@
5702557200
if( NEVER(pBt->pCursor) ){
5702657201
sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
5702757202
return SQLITE_LOCKED_SHAREDCACHE;
5702857203
}
5702957204
57030
- rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0, 0);
57205
+ rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
5703157206
if( rc ) return rc;
5703257207
rc = sqlite3BtreeClearTable(p, iTable, 0);
5703357208
if( rc ){
5703457209
releasePage(pPage);
5703557210
return rc;
@@ -57060,21 +57235,21 @@
5706057235
** number in the database. So move the page that does into the
5706157236
** gap left by the deleted root-page.
5706257237
*/
5706357238
MemPage *pMove;
5706457239
releasePage(pPage);
57065
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
57240
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
5706657241
if( rc!=SQLITE_OK ){
5706757242
return rc;
5706857243
}
5706957244
rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
5707057245
releasePage(pMove);
5707157246
if( rc!=SQLITE_OK ){
5707257247
return rc;
5707357248
}
5707457249
pMove = 0;
57075
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
57250
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
5707657251
freePage(pMove, &rc);
5707757252
releasePage(pMove);
5707857253
if( rc!=SQLITE_OK ){
5707957254
return rc;
5708057255
}
@@ -57285,11 +57460,11 @@
5728557460
if( zMsg1 ){
5728657461
sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
5728757462
}
5728857463
sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
5728957464
va_end(ap);
57290
- if( pCheck->errMsg.mallocFailed ){
57465
+ if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
5729157466
pCheck->mallocFailed = 1;
5729257467
}
5729357468
}
5729457469
#endif /* SQLITE_OMIT_INTEGRITY_CHECK */
5729557470
@@ -57482,11 +57657,11 @@
5748257657
*/
5748357658
pBt = pCheck->pBt;
5748457659
usableSize = pBt->usableSize;
5748557660
if( iPage==0 ) return 0;
5748657661
if( checkRef(pCheck, iPage, zParentContext) ) return 0;
57487
- if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0, 0))!=0 ){
57662
+ if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
5748857663
checkAppendMsg(pCheck, zContext,
5748957664
"unable to get the page. error code=%d", rc);
5749057665
return 0;
5749157666
}
5749257667
@@ -58441,11 +58616,11 @@
5844158616
for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
5844258617
const Pgno iSrcPg = p->iNext; /* Source page number */
5844358618
if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
5844458619
DbPage *pSrcPg; /* Source page object */
5844558620
rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
58446
- PAGER_ACQUIRE_READONLY);
58621
+ PAGER_GET_READONLY);
5844758622
if( rc==SQLITE_OK ){
5844858623
rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
5844958624
sqlite3PagerUnref(pSrcPg);
5845058625
}
5845158626
}
@@ -59596,38 +59771,33 @@
5959659771
/* If one value is a number and the other is not, the number is less.
5959759772
** If both are numbers, compare as reals if one is a real, or as integers
5959859773
** if both values are integers.
5959959774
*/
5960059775
if( combined_flags&(MEM_Int|MEM_Real) ){
59601
- if( !(f1&(MEM_Int|MEM_Real)) ){
59602
- return 1;
59603
- }
59604
- if( !(f2&(MEM_Int|MEM_Real)) ){
59605
- return -1;
59606
- }
59607
- if( (f1 & f2 & MEM_Int)==0 ){
59608
- double r1, r2;
59609
- if( (f1&MEM_Real)==0 ){
59610
- r1 = (double)pMem1->u.i;
59611
- }else{
59612
- r1 = pMem1->r;
59613
- }
59614
- if( (f2&MEM_Real)==0 ){
59615
- r2 = (double)pMem2->u.i;
59616
- }else{
59617
- r2 = pMem2->r;
59618
- }
59619
- if( r1<r2 ) return -1;
59620
- if( r1>r2 ) return 1;
59621
- return 0;
59622
- }else{
59623
- assert( f1&MEM_Int );
59624
- assert( f2&MEM_Int );
59776
+ double r1, r2;
59777
+ if( (f1 & f2 & MEM_Int)!=0 ){
5962559778
if( pMem1->u.i < pMem2->u.i ) return -1;
5962659779
if( pMem1->u.i > pMem2->u.i ) return 1;
5962759780
return 0;
5962859781
}
59782
+ if( (f1&MEM_Real)!=0 ){
59783
+ r1 = pMem1->r;
59784
+ }else if( (f1&MEM_Int)!=0 ){
59785
+ r1 = (double)pMem1->u.i;
59786
+ }else{
59787
+ return 1;
59788
+ }
59789
+ if( (f2&MEM_Real)!=0 ){
59790
+ r2 = pMem2->r;
59791
+ }else if( (f2&MEM_Int)!=0 ){
59792
+ r2 = (double)pMem2->u.i;
59793
+ }else{
59794
+ return -1;
59795
+ }
59796
+ if( r1<r2 ) return -1;
59797
+ if( r1>r2 ) return 1;
59798
+ return 0;
5962959799
}
5963059800
5963159801
/* If one value is a string and the other is a blob, the string is less.
5963259802
** If both are strings, compare using the collating functions.
5963359803
*/
@@ -59803,43 +59973,108 @@
5980359973
}
5980459974
return p;
5980559975
}
5980659976
5980759977
/*
59808
-** Create a new sqlite3_value object, containing the value of pExpr.
59978
+** Context object passed by sqlite3Stat4ProbeSetValue() through to
59979
+** valueNew(). See comments above valueNew() for details.
59980
+*/
59981
+struct ValueNewStat4Ctx {
59982
+ Parse *pParse;
59983
+ Index *pIdx;
59984
+ UnpackedRecord **ppRec;
59985
+ int iVal;
59986
+};
59987
+
59988
+/*
59989
+** Allocate and return a pointer to a new sqlite3_value object. If
59990
+** the second argument to this function is NULL, the object is allocated
59991
+** by calling sqlite3ValueNew().
5980959992
**
59810
-** This only works for very simple expressions that consist of one constant
59811
-** token (i.e. "5", "5.1", "'a string'"). If the expression can
59812
-** be converted directly into a value, then the value is allocated and
59813
-** a pointer written to *ppVal. The caller is responsible for deallocating
59814
-** the value by passing it to sqlite3ValueFree() later on. If the expression
59815
-** cannot be converted to a value, then *ppVal is set to NULL.
59993
+** Otherwise, if the second argument is non-zero, then this function is
59994
+** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
59995
+** already been allocated, allocate the UnpackedRecord structure that
59996
+** that function will return to its caller here. Then return a pointer
59997
+** an sqlite3_value within the UnpackedRecord.a[] array.
5981659998
*/
59817
-SQLITE_PRIVATE int sqlite3ValueFromExpr(
59818
- sqlite3 *db, /* The database connection */
59819
- Expr *pExpr, /* The expression to evaluate */
59820
- u8 enc, /* Encoding to use */
59821
- u8 affinity, /* Affinity to use */
59822
- sqlite3_value **ppVal /* Write the new value here */
59999
+static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60000
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60001
+ if( p ){
60002
+ UnpackedRecord *pRec = p->ppRec[0];
60003
+
60004
+ if( pRec==0 ){
60005
+ Index *pIdx = p->pIdx; /* Index being probed */
60006
+ int nByte; /* Bytes of space to allocate */
60007
+ int i; /* Counter variable */
60008
+ int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */
60009
+
60010
+ nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord);
60011
+ pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60012
+ if( pRec ){
60013
+ pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx);
60014
+ if( pRec->pKeyInfo ){
60015
+ assert( pRec->pKeyInfo->nField+1==nCol );
60016
+ pRec->pKeyInfo->enc = ENC(db);
60017
+ pRec->flags = UNPACKED_PREFIX_MATCH;
60018
+ pRec->aMem = (Mem *)&pRec[1];
60019
+ for(i=0; i<nCol; i++){
60020
+ pRec->aMem[i].flags = MEM_Null;
60021
+ pRec->aMem[i].type = SQLITE_NULL;
60022
+ pRec->aMem[i].db = db;
60023
+ }
60024
+ }else{
60025
+ sqlite3DbFree(db, pRec);
60026
+ pRec = 0;
60027
+ }
60028
+ }
60029
+ if( pRec==0 ) return 0;
60030
+ p->ppRec[0] = pRec;
60031
+ }
60032
+
60033
+ pRec->nField = p->iVal+1;
60034
+ return &pRec->aMem[p->iVal];
60035
+ }
60036
+#endif
60037
+ return sqlite3ValueNew(db);
60038
+}
60039
+
60040
+/*
60041
+** Extract a value from the supplied expression in the manner described
60042
+** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
60043
+** using valueNew().
60044
+**
60045
+** If pCtx is NULL and an error occurs after the sqlite3_value object
60046
+** has been allocated, it is freed before returning. Or, if pCtx is not
60047
+** NULL, it is assumed that the caller will free any allocated object
60048
+** in all cases.
60049
+*/
60050
+int valueFromExpr(
60051
+ sqlite3 *db, /* The database connection */
60052
+ Expr *pExpr, /* The expression to evaluate */
60053
+ u8 enc, /* Encoding to use */
60054
+ u8 affinity, /* Affinity to use */
60055
+ sqlite3_value **ppVal, /* Write the new value here */
60056
+ struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
5982360057
){
5982460058
int op;
5982560059
char *zVal = 0;
5982660060
sqlite3_value *pVal = 0;
5982760061
int negInt = 1;
5982860062
const char *zNeg = "";
60063
+ int rc = SQLITE_OK;
5982960064
5983060065
if( !pExpr ){
5983160066
*ppVal = 0;
5983260067
return SQLITE_OK;
5983360068
}
5983460069
op = pExpr->op;
5983560070
59836
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
60071
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
5983760072
** The ifdef here is to enable us to achieve 100% branch test coverage even
59838
- ** when SQLITE_ENABLE_STAT3 is omitted.
60073
+ ** when SQLITE_ENABLE_STAT4 is omitted.
5983960074
*/
59840
-#ifdef SQLITE_ENABLE_STAT3
60075
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
5984160076
if( op==TK_REGISTER ) op = pExpr->op2;
5984260077
#else
5984360078
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5984460079
#endif
5984560080
@@ -59853,11 +60088,11 @@
5985360088
negInt = -1;
5985460089
zNeg = "-";
5985560090
}
5985660091
5985760092
if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
59858
- pVal = sqlite3ValueNew(db);
60093
+ pVal = valueNew(db, pCtx);
5985960094
if( pVal==0 ) goto no_mem;
5986060095
if( ExprHasProperty(pExpr, EP_IntValue) ){
5986160096
sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
5986260097
}else{
5986360098
zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59870,15 +60105,17 @@
5987060105
}else{
5987160106
sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
5987260107
}
5987360108
if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
5987460109
if( enc!=SQLITE_UTF8 ){
59875
- sqlite3VdbeChangeEncoding(pVal, enc);
60110
+ rc = sqlite3VdbeChangeEncoding(pVal, enc);
5987660111
}
5987760112
}else if( op==TK_UMINUS ) {
5987860113
/* This branch happens for multiple negative signs. Ex: -(-5) */
59879
- if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
60114
+ if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
60115
+ && pVal!=0
60116
+ ){
5988060117
sqlite3VdbeMemNumerify(pVal);
5988160118
if( pVal->u.i==SMALLEST_INT64 ){
5988260119
pVal->flags &= MEM_Int;
5988360120
pVal->flags |= MEM_Real;
5988460121
pVal->r = (double)LARGEST_INT64;
@@ -59887,19 +60124,19 @@
5988760124
}
5988860125
pVal->r = -pVal->r;
5988960126
sqlite3ValueApplyAffinity(pVal, affinity, enc);
5989060127
}
5989160128
}else if( op==TK_NULL ){
59892
- pVal = sqlite3ValueNew(db);
60129
+ pVal = valueNew(db, pCtx);
5989360130
if( pVal==0 ) goto no_mem;
5989460131
}
5989560132
#ifndef SQLITE_OMIT_BLOB_LITERAL
5989660133
else if( op==TK_BLOB ){
5989760134
int nVal;
5989860135
assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
5989960136
assert( pExpr->u.zToken[1]=='\'' );
59900
- pVal = sqlite3ValueNew(db);
60137
+ pVal = valueNew(db, pCtx);
5990160138
if( !pVal ) goto no_mem;
5990260139
zVal = &pExpr->u.zToken[2];
5990360140
nVal = sqlite3Strlen30(zVal)-1;
5990460141
assert( zVal[nVal]=='\'' );
5990560142
sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59909,20 +60146,203 @@
5990960146
5991060147
if( pVal ){
5991160148
sqlite3VdbeMemStoreType(pVal);
5991260149
}
5991360150
*ppVal = pVal;
59914
- return SQLITE_OK;
60151
+ return rc;
5991560152
5991660153
no_mem:
5991760154
db->mallocFailed = 1;
5991860155
sqlite3DbFree(db, zVal);
59919
- sqlite3ValueFree(pVal);
59920
- *ppVal = 0;
60156
+ assert( *ppVal==0 );
60157
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60158
+ if( pCtx==0 ) sqlite3ValueFree(pVal);
60159
+#else
60160
+ assert( pCtx==0 ); sqlite3ValueFree(pVal);
60161
+#endif
5992160162
return SQLITE_NOMEM;
5992260163
}
5992360164
60165
+/*
60166
+** Create a new sqlite3_value object, containing the value of pExpr.
60167
+**
60168
+** This only works for very simple expressions that consist of one constant
60169
+** token (i.e. "5", "5.1", "'a string'"). If the expression can
60170
+** be converted directly into a value, then the value is allocated and
60171
+** a pointer written to *ppVal. The caller is responsible for deallocating
60172
+** the value by passing it to sqlite3ValueFree() later on. If the expression
60173
+** cannot be converted to a value, then *ppVal is set to NULL.
60174
+*/
60175
+SQLITE_PRIVATE int sqlite3ValueFromExpr(
60176
+ sqlite3 *db, /* The database connection */
60177
+ Expr *pExpr, /* The expression to evaluate */
60178
+ u8 enc, /* Encoding to use */
60179
+ u8 affinity, /* Affinity to use */
60180
+ sqlite3_value **ppVal /* Write the new value here */
60181
+){
60182
+ return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
60183
+}
60184
+
60185
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60186
+/*
60187
+** The implementation of the sqlite_record() function. This function accepts
60188
+** a single argument of any type. The return value is a formatted database
60189
+** record (a blob) containing the argument value.
60190
+**
60191
+** This is used to convert the value stored in the 'sample' column of the
60192
+** sqlite_stat3 table to the record format SQLite uses internally.
60193
+*/
60194
+static void recordFunc(
60195
+ sqlite3_context *context,
60196
+ int argc,
60197
+ sqlite3_value **argv
60198
+){
60199
+ const int file_format = 1;
60200
+ int iSerial; /* Serial type */
60201
+ int nSerial; /* Bytes of space for iSerial as varint */
60202
+ int nVal; /* Bytes of space required for argv[0] */
60203
+ int nRet;
60204
+ sqlite3 *db;
60205
+ u8 *aRet;
60206
+
60207
+ iSerial = sqlite3VdbeSerialType(argv[0], file_format);
60208
+ nSerial = sqlite3VarintLen(iSerial);
60209
+ nVal = sqlite3VdbeSerialTypeLen(iSerial);
60210
+ db = sqlite3_context_db_handle(context);
60211
+
60212
+ nRet = 1 + nSerial + nVal;
60213
+ aRet = sqlite3DbMallocRaw(db, nRet);
60214
+ if( aRet==0 ){
60215
+ sqlite3_result_error_nomem(context);
60216
+ }else{
60217
+ aRet[0] = nSerial+1;
60218
+ sqlite3PutVarint(&aRet[1], iSerial);
60219
+ sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
60220
+ sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
60221
+ sqlite3DbFree(db, aRet);
60222
+ }
60223
+}
60224
+
60225
+/*
60226
+** Register built-in functions used to help read ANALYZE data.
60227
+*/
60228
+SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
60229
+ static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
60230
+ FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
60231
+ };
60232
+ int i;
60233
+ FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
60234
+ FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
60235
+ for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
60236
+ sqlite3FuncDefInsert(pHash, &aFunc[i]);
60237
+ }
60238
+}
60239
+
60240
+/*
60241
+** This function is used to allocate and populate UnpackedRecord
60242
+** structures intended to be compared against sample index keys stored
60243
+** in the sqlite_stat4 table.
60244
+**
60245
+** A single call to this function attempts to populates field iVal (leftmost
60246
+** is 0 etc.) of the unpacked record with a value extracted from expression
60247
+** pExpr. Extraction of values is possible if:
60248
+**
60249
+** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
60250
+**
60251
+** * The expression is a bound variable, and this is a reprepare, or
60252
+**
60253
+** * The sqlite3ValueFromExpr() function is able to extract a value
60254
+** from the expression (i.e. the expression is a literal value).
60255
+**
60256
+** If a value can be extracted, the affinity passed as the 5th argument
60257
+** is applied to it before it is copied into the UnpackedRecord. Output
60258
+** parameter *pbOk is set to true if a value is extracted, or false
60259
+** otherwise.
60260
+**
60261
+** When this function is called, *ppRec must either point to an object
60262
+** allocated by an earlier call to this function, or must be NULL. If it
60263
+** is NULL and a value can be successfully extracted, a new UnpackedRecord
60264
+** is allocated (and *ppRec set to point to it) before returning.
60265
+**
60266
+** Unless an error is encountered, SQLITE_OK is returned. It is not an
60267
+** error if a value cannot be extracted from pExpr. If an error does
60268
+** occur, an SQLite error code is returned.
60269
+*/
60270
+SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
60271
+ Parse *pParse, /* Parse context */
60272
+ Index *pIdx, /* Index being probed */
60273
+ UnpackedRecord **ppRec, /* IN/OUT: Probe record */
60274
+ Expr *pExpr, /* The expression to extract a value from */
60275
+ u8 affinity, /* Affinity to use */
60276
+ int iVal, /* Array element to populate */
60277
+ int *pbOk /* OUT: True if value was extracted */
60278
+){
60279
+ int rc = SQLITE_OK;
60280
+ sqlite3_value *pVal = 0;
60281
+
60282
+ struct ValueNewStat4Ctx alloc;
60283
+ alloc.pParse = pParse;
60284
+ alloc.pIdx = pIdx;
60285
+ alloc.ppRec = ppRec;
60286
+ alloc.iVal = iVal;
60287
+
60288
+ if( !pExpr ){
60289
+ pVal = valueNew(pParse->db, &alloc);
60290
+ if( pVal ){
60291
+ sqlite3VdbeMemSetNull((Mem*)pVal);
60292
+ *pbOk = 1;
60293
+ }
60294
+ }else if( pExpr->op==TK_VARIABLE
60295
+ || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
60296
+ ){
60297
+ Vdbe *v;
60298
+ int iBindVar = pExpr->iColumn;
60299
+ sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
60300
+ if( (v = pParse->pReprepare)!=0 ){
60301
+ pVal = valueNew(pParse->db, &alloc);
60302
+ if( pVal ){
60303
+ rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
60304
+ if( rc==SQLITE_OK ){
60305
+ sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60306
+ }
60307
+ pVal->db = pParse->db;
60308
+ *pbOk = 1;
60309
+ sqlite3VdbeMemStoreType((Mem*)pVal);
60310
+ }
60311
+ }else{
60312
+ *pbOk = 0;
60313
+ }
60314
+ }else{
60315
+ sqlite3 *db = pParse->db;
60316
+ rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
60317
+ *pbOk = (pVal!=0);
60318
+ }
60319
+
60320
+ assert( pVal==0 || pVal->db==pParse->db );
60321
+ return rc;
60322
+}
60323
+
60324
+/*
60325
+** Unless it is NULL, the argument must be an UnpackedRecord object returned
60326
+** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
60327
+** the object.
60328
+*/
60329
+SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
60330
+ if( pRec ){
60331
+ int i;
60332
+ int nCol = pRec->pKeyInfo->nField+1;
60333
+ Mem *aMem = pRec->aMem;
60334
+ sqlite3 *db = aMem[0].db;
60335
+ for(i=0; i<nCol; i++){
60336
+ sqlite3DbFree(db, aMem[i].zMalloc);
60337
+ }
60338
+ sqlite3DbFree(db, pRec->pKeyInfo);
60339
+ sqlite3DbFree(db, pRec);
60340
+ }
60341
+}
60342
+#endif /* ifdef SQLITE_ENABLE_STAT4 */
60343
+
5992460344
/*
5992560345
** Change the string value of an sqlite3_value object
5992660346
*/
5992760347
SQLITE_PRIVATE void sqlite3ValueSetStr(
5992860348
sqlite3_value *v, /* Value to be set */
@@ -61724,11 +62144,11 @@
6172462144
** virtual module tables written in this transaction. This has to
6172562145
** be done before determining whether a master journal file is
6172662146
** required, as an xSync() callback may add an attached database
6172762147
** to the transaction.
6172862148
*/
61729
- rc = sqlite3VtabSync(db, &p->zErrMsg);
62149
+ rc = sqlite3VtabSync(db, p);
6173062150
6173162151
/* This loop determines (a) if the commit hook should be invoked and
6173262152
** (b) how many database files have open write transactions, not
6173362153
** including the temp database. (b) is important because if more than
6173462154
** one database file has an open write transaction, a master journal
@@ -63264,10 +63684,25 @@
6326463684
}else{
6326563685
v->expmask |= ((u32)1 << (iVar-1));
6326663686
}
6326763687
}
6326863688
63689
+#ifndef SQLITE_OMIT_VIRTUALTABLE
63690
+/*
63691
+** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
63692
+** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
63693
+** in memory obtained from sqlite3DbMalloc).
63694
+*/
63695
+SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
63696
+ sqlite3 *db = p->db;
63697
+ sqlite3DbFree(db, p->zErrMsg);
63698
+ p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
63699
+ sqlite3_free(pVtab->zErrMsg);
63700
+ pVtab->zErrMsg = 0;
63701
+}
63702
+#endif /* SQLITE_OMIT_VIRTUALTABLE */
63703
+
6326963704
/************** End of vdbeaux.c *********************************************/
6327063705
/************** Begin file vdbeapi.c *****************************************/
6327163706
/*
6327263707
** 2004 May 26
6327363708
**
@@ -63477,16 +63912,18 @@
6347763912
sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
6347863913
}
6347963914
SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
6348063915
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6348163916
pCtx->isError = SQLITE_ERROR;
63917
+ pCtx->fErrorOrAux = 1;
6348263918
sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
6348363919
}
6348463920
#ifndef SQLITE_OMIT_UTF16
6348563921
SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
6348663922
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6348763923
pCtx->isError = SQLITE_ERROR;
63924
+ pCtx->fErrorOrAux = 1;
6348863925
sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
6348963926
}
6349063927
#endif
6349163928
SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
6349263929
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
@@ -63546,10 +63983,11 @@
6354663983
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6354763984
sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
6354863985
}
6354963986
SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
6355063987
pCtx->isError = errCode;
63988
+ pCtx->fErrorOrAux = 1;
6355163989
if( pCtx->s.flags & MEM_Null ){
6355263990
sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
6355363991
SQLITE_UTF8, SQLITE_STATIC);
6355463992
}
6355563993
}
@@ -63556,19 +63994,21 @@
6355663994
6355763995
/* Force an SQLITE_TOOBIG error. */
6355863996
SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
6355963997
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6356063998
pCtx->isError = SQLITE_TOOBIG;
63999
+ pCtx->fErrorOrAux = 1;
6356164000
sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
6356264001
SQLITE_UTF8, SQLITE_STATIC);
6356364002
}
6356464003
6356564004
/* An SQLITE_NOMEM error. */
6356664005
SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
6356764006
assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
6356864007
sqlite3VdbeMemSetNull(&pCtx->s);
6356964008
pCtx->isError = SQLITE_NOMEM;
64009
+ pCtx->fErrorOrAux = 1;
6357064010
pCtx->s.db->mallocFailed = 1;
6357164011
}
6357264012
6357364013
/*
6357464014
** This function is called after a transaction has been committed. It
@@ -63887,10 +64327,14 @@
6388764327
if( !pAuxData ) goto failed;
6388864328
pAuxData->iOp = pCtx->iOp;
6388964329
pAuxData->iArg = iArg;
6389064330
pAuxData->pNext = pVdbe->pAuxData;
6389164331
pVdbe->pAuxData = pAuxData;
64332
+ if( pCtx->fErrorOrAux==0 ){
64333
+ pCtx->isError = 0;
64334
+ pCtx->fErrorOrAux = 1;
64335
+ }
6389264336
}else if( pAuxData->xDelete ){
6389364337
pAuxData->xDelete(pAuxData->pAux);
6389464338
}
6389564339
6389664340
pAuxData->pAux = pAux;
@@ -64555,13 +64999,13 @@
6455564999
/*
6455665000
** Return the value of a status counter for a prepared statement
6455765001
*/
6455865002
SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
6455965003
Vdbe *pVdbe = (Vdbe*)pStmt;
64560
- int v = pVdbe->aCounter[op-1];
64561
- if( resetFlag ) pVdbe->aCounter[op-1] = 0;
64562
- return v;
65004
+ u32 v = pVdbe->aCounter[op];
65005
+ if( resetFlag ) pVdbe->aCounter[op] = 0;
65006
+ return (int)v;
6456365007
}
6456465008
6456565009
/************** End of vdbeapi.c *********************************************/
6456665010
/************** Begin file vdbetrace.c ***************************************/
6456765011
/*
@@ -65444,23 +65888,10 @@
6544465888
assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
6544565889
return 1;
6544665890
}
6544765891
#endif
6544865892
65449
-/*
65450
-** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
65451
-** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
65452
-** in memory obtained from sqlite3DbMalloc).
65453
-*/
65454
-static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
65455
- sqlite3 *db = p->db;
65456
- sqlite3DbFree(db, p->zErrMsg);
65457
- p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
65458
- sqlite3_free(pVtab->zErrMsg);
65459
- pVtab->zErrMsg = 0;
65460
-}
65461
-
6546265893
6546365894
/*
6546465895
** Execute as much of a VDBE program as we can then return.
6546565896
**
6546665897
** sqlite3VdbeMakeReady() must be called before this routine in order to
@@ -65964,11 +66395,11 @@
6596466395
CHECK_FOR_INTERRUPT;
6596566396
sqlite3VdbeIOTraceSql(p);
6596666397
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
6596766398
if( db->xProgress ){
6596866399
assert( 0 < db->nProgressOps );
65969
- nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP-1];
66400
+ nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
6597066401
if( nProgressLimit==0 ){
6597166402
nProgressLimit = db->nProgressOps;
6597266403
}else{
6597366404
nProgressLimit %= (unsigned)db->nProgressOps;
6597466405
}
@@ -66027,11 +66458,11 @@
6602766458
** value or convert mem[p2] to a different type.
6602866459
*/
6602966460
assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
6603066461
if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
6603166462
assert( pOp->p2>0 );
66032
- assert( pOp->p2<=p->nMem );
66463
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6603366464
pOut = &aMem[pOp->p2];
6603466465
memAboutToChange(p, pOut);
6603566466
VdbeMemRelease(pOut);
6603666467
pOut->flags = MEM_Int;
6603766468
}
@@ -66038,34 +66469,34 @@
6603866469
6603966470
/* Sanity checking on other operands */
6604066471
#ifdef SQLITE_DEBUG
6604166472
if( (pOp->opflags & OPFLG_IN1)!=0 ){
6604266473
assert( pOp->p1>0 );
66043
- assert( pOp->p1<=p->nMem );
66474
+ assert( pOp->p1<=(p->nMem-p->nCursor) );
6604466475
assert( memIsValid(&aMem[pOp->p1]) );
6604566476
REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
6604666477
}
6604766478
if( (pOp->opflags & OPFLG_IN2)!=0 ){
6604866479
assert( pOp->p2>0 );
66049
- assert( pOp->p2<=p->nMem );
66480
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6605066481
assert( memIsValid(&aMem[pOp->p2]) );
6605166482
REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
6605266483
}
6605366484
if( (pOp->opflags & OPFLG_IN3)!=0 ){
6605466485
assert( pOp->p3>0 );
66055
- assert( pOp->p3<=p->nMem );
66486
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6605666487
assert( memIsValid(&aMem[pOp->p3]) );
6605766488
REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
6605866489
}
6605966490
if( (pOp->opflags & OPFLG_OUT2)!=0 ){
6606066491
assert( pOp->p2>0 );
66061
- assert( pOp->p2<=p->nMem );
66492
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6606266493
memAboutToChange(p, &aMem[pOp->p2]);
6606366494
}
6606466495
if( (pOp->opflags & OPFLG_OUT3)!=0 ){
6606566496
assert( pOp->p3>0 );
66066
- assert( pOp->p3<=p->nMem );
66497
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6606766498
memAboutToChange(p, &aMem[pOp->p3]);
6606866499
}
6606966500
#endif
6607066501
6607166502
switch( pOp->opcode ){
@@ -66154,11 +66585,11 @@
6615466585
**
6615566586
** Write the current address onto register P1
6615666587
** and then jump to address P2.
6615766588
*/
6615866589
case OP_Gosub: { /* jump */
66159
- assert( pOp->p1>0 && pOp->p1<=p->nMem );
66590
+ assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
6616066591
pIn1 = &aMem[pOp->p1];
6616166592
assert( (pIn1->flags & MEM_Dyn)==0 );
6616266593
memAboutToChange(p, pIn1);
6616366594
pIn1->flags = MEM_Int;
6616466595
pIn1->u.i = pc;
@@ -66370,11 +66801,11 @@
6637066801
#if 0 /* local variables moved into u.ab */
6637166802
int cnt;
6637266803
u16 nullFlag;
6637366804
#endif /* local variables moved into u.ab */
6637466805
u.ab.cnt = pOp->p3-pOp->p2;
66375
- assert( pOp->p3<=p->nMem );
66806
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6637666807
pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
6637766808
while( u.ab.cnt>0 ){
6637866809
pOut++;
6637966810
memAboutToChange(p, pOut);
6638066811
VdbeMemRelease(pOut);
@@ -66443,12 +66874,12 @@
6644366874
assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
6644466875
6644566876
pIn1 = &aMem[u.ad.p1];
6644666877
pOut = &aMem[u.ad.p2];
6644766878
while( u.ad.n-- ){
66448
- assert( pOut<=&aMem[p->nMem] );
66449
- assert( pIn1<=&aMem[p->nMem] );
66879
+ assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
66880
+ assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
6645066881
assert( memIsValid(pIn1) );
6645166882
memAboutToChange(p, pOut);
6645266883
u.ad.zMalloc = pOut->zMalloc;
6645366884
pOut->zMalloc = 0;
6645466885
sqlite3VdbeMemMove(pOut, pIn1);
@@ -66532,11 +66963,11 @@
6653266963
Mem *pMem;
6653366964
int i;
6653466965
#endif /* local variables moved into u.af */
6653566966
assert( p->nResColumn==pOp->p2 );
6653666967
assert( pOp->p1>0 );
66537
- assert( pOp->p1+pOp->p2<=p->nMem+1 );
66968
+ assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
6653866969
6653966970
/* If this statement has violated immediate foreign key constraints, do
6654066971
** not return the number of rows modified. And do not RELEASE the statement
6654166972
** transaction. It needs to be rolled back. */
6654266973
if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66812,15 +67243,15 @@
6681267243
#endif /* local variables moved into u.ai */
6681367244
6681467245
u.ai.n = pOp->p5;
6681567246
u.ai.apVal = p->apArg;
6681667247
assert( u.ai.apVal || u.ai.n==0 );
66817
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
67248
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6681867249
pOut = &aMem[pOp->p3];
6681967250
memAboutToChange(p, pOut);
6682067251
66821
- assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
67252
+ assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=(p->nMem-p->nCursor)+1) );
6682267253
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
6682367254
u.ai.pArg = &aMem[pOp->p2];
6682467255
for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
6682567256
assert( memIsValid(u.ai.pArg) );
6682667257
u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -66843,11 +67274,11 @@
6684367274
** the already allocated buffer instead of allocating a new one.
6684467275
*/
6684567276
sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
6684667277
MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
6684767278
66848
- u.ai.ctx.isError = 0;
67279
+ u.ai.ctx.fErrorOrAux = 0;
6684967280
if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
6685067281
assert( pOp>aOp );
6685167282
assert( pOp[-1].p4type==P4_COLLSEQ );
6685267283
assert( pOp[-1].opcode==OP_CollSeq );
6685367284
u.ai.ctx.pColl = pOp[-1].p4.pColl;
@@ -66854,15 +67285,10 @@
6685467285
}
6685567286
db->lastRowid = lastRowid;
6685667287
(*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
6685767288
lastRowid = db->lastRowid;
6685867289
66859
- /* If any auxiliary data functions have been called by this user function,
66860
- ** immediately call the destructor for any non-static values.
66861
- */
66862
- sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
66863
-
6686467290
if( db->mallocFailed ){
6686567291
/* Even though a malloc() has failed, the implementation of the
6686667292
** user function may have called an sqlite3_result_XXX() function
6686767293
** to return a value. The following call releases any resources
6686867294
** associated with such a value.
@@ -66870,13 +67296,16 @@
6687067296
sqlite3VdbeMemRelease(&u.ai.ctx.s);
6687167297
goto no_mem;
6687267298
}
6687367299
6687467300
/* If the function returned an error, throw an exception */
66875
- if( u.ai.ctx.isError ){
66876
- sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
66877
- rc = u.ai.ctx.isError;
67301
+ if( u.ai.ctx.fErrorOrAux ){
67302
+ if( u.ai.ctx.isError ){
67303
+ sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
67304
+ rc = u.ai.ctx.isError;
67305
+ }
67306
+ sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
6687867307
}
6687967308
6688067309
/* Copy the result of the function into register P3 */
6688167310
sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
6688267311
sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
@@ -67248,16 +67677,16 @@
6724867677
}else{
6724967678
/* SQLITE_NULLEQ is clear and at least one operand is NULL,
6725067679
** then the result is always NULL.
6725167680
** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
6725267681
*/
67253
- if( pOp->p5 & SQLITE_STOREP2 ){
67682
+ if( pOp->p5 & SQLITE_JUMPIFNULL ){
67683
+ pc = pOp->p2-1;
67684
+ }else if( pOp->p5 & SQLITE_STOREP2 ){
6725467685
pOut = &aMem[pOp->p2];
6725567686
MemSetTypeFlag(pOut, MEM_Null);
6725667687
REGISTER_TRACE(pOp->p2, pOut);
67257
- }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
67258
- pc = pOp->p2-1;
6725967688
}
6726067689
break;
6726167690
}
6726267691
}else{
6726367692
/* Neither operand is NULL. Do a comparison. */
@@ -67354,15 +67783,15 @@
6735467783
u.al.p2 = pOp->p2;
6735567784
#if SQLITE_DEBUG
6735667785
if( aPermute ){
6735767786
int k, mx = 0;
6735867787
for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67359
- assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
67360
- assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
67788
+ assert( u.al.p1>0 && u.al.p1+mx<=(p->nMem-p->nCursor)+1 );
67789
+ assert( u.al.p2>0 && u.al.p2+mx<=(p->nMem-p->nCursor)+1 );
6736167790
}else{
67362
- assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
67363
- assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
67791
+ assert( u.al.p1>0 && u.al.p1+u.al.n<=(p->nMem-p->nCursor)+1 );
67792
+ assert( u.al.p2>0 && u.al.p2+u.al.n<=(p->nMem-p->nCursor)+1 );
6736467793
}
6736567794
#endif /* SQLITE_DEBUG */
6736667795
for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
6736767796
u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
6736867797
assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67615,11 +68044,11 @@
6761568044
u.ao.p1 = pOp->p1;
6761668045
u.ao.p2 = pOp->p2;
6761768046
u.ao.pC = 0;
6761868047
memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
6761968048
assert( u.ao.p1<p->nCursor );
67620
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
68049
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6762168050
u.ao.pDest = &aMem[pOp->p3];
6762268051
memAboutToChange(p, u.ao.pDest);
6762368052
u.ao.zRec = 0;
6762468053
6762568054
/* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67915,11 +68344,11 @@
6791568344
u.ap.zAffinity = pOp->p4.z;
6791668345
assert( u.ap.zAffinity!=0 );
6791768346
assert( u.ap.zAffinity[pOp->p2]==0 );
6791868347
pIn1 = &aMem[pOp->p1];
6791968348
while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
67920
- assert( pIn1 <= &p->aMem[p->nMem] );
68349
+ assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
6792168350
assert( memIsValid(pIn1) );
6792268351
ExpandBlob(pIn1);
6792368352
applyAffinity(pIn1, u.ap.cAff, encoding);
6792468353
pIn1++;
6792568354
}
@@ -67978,11 +68407,11 @@
6797868407
u.aq.nData = 0; /* Number of bytes of data space */
6797968408
u.aq.nHdr = 0; /* Number of bytes of header space */
6798068409
u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
6798168410
u.aq.nField = pOp->p1;
6798268411
u.aq.zAffinity = pOp->p4.z;
67983
- assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
68412
+ assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=(p->nMem-p->nCursor)+1 );
6798468413
u.aq.pData0 = &aMem[u.aq.nField];
6798568414
u.aq.nField = pOp->p2;
6798668415
u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
6798768416
u.aq.file_format = p->minWriteFileFormat;
6798868417
@@ -68044,11 +68473,11 @@
6804468473
for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
6804568474
u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
6804668475
}
6804768476
assert( u.aq.i==u.aq.nByte );
6804868477
68049
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
68478
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6805068479
pOut->n = (int)u.aq.nByte;
6805168480
pOut->flags = MEM_Blob | MEM_Dyn;
6805268481
pOut->xDel = 0;
6805368482
if( u.aq.nZero ){
6805468483
pOut->u.nZero = u.aq.nZero;
@@ -68640,11 +69069,11 @@
6864069069
}else{
6864169070
u.ay.wrFlag = 0;
6864269071
}
6864369072
if( pOp->p5 & OPFLAG_P2ISREG ){
6864469073
assert( u.ay.p2>0 );
68645
- assert( u.ay.p2<=p->nMem );
69074
+ assert( u.ay.p2<=(p->nMem-p->nCursor) );
6864669075
pIn2 = &aMem[u.ay.p2];
6864769076
assert( memIsValid(pIn2) );
6864869077
assert( (pIn2->flags & MEM_Int)!=0 );
6864969078
sqlite3VdbeMemIntegerify(pIn2);
6865069079
u.ay.p2 = (int)pIn2->u.i;
@@ -69191,11 +69620,11 @@
6919169620
6919269621
pIn3 = &aMem[pOp->p3];
6919369622
u.bf.aMx = &aMem[pOp->p4.i];
6919469623
/* Assert that the values of parameters P1 and P4 are in range. */
6919569624
assert( pOp->p4type==P4_INT32 );
69196
- assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
69625
+ assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
6919769626
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6919869627
6919969628
/* Find the index cursor. */
6920069629
u.bf.pCx = p->apCsr[pOp->p1];
6920169630
assert( u.bf.pCx->deferredMoveto==0 );
@@ -69398,11 +69827,11 @@
6939869827
/* Assert that P3 is a valid memory cell. */
6939969828
assert( pOp->p3<=u.bh.pFrame->nMem );
6940069829
u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
6940169830
}else{
6940269831
/* Assert that P3 is a valid memory cell. */
69403
- assert( pOp->p3<=p->nMem );
69832
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6940469833
u.bh.pMem = &aMem[pOp->p3];
6940569834
memAboutToChange(p, u.bh.pMem);
6940669835
}
6940769836
assert( memIsValid(u.bh.pMem) );
6940869837
@@ -69808,11 +70237,11 @@
6980870237
}else if( u.bn.pC->pVtabCursor ){
6980970238
u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
6981070239
u.bn.pModule = u.bn.pVtab->pModule;
6981170240
assert( u.bn.pModule->xRowid );
6981270241
rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
69813
- importVtabErrMsg(p, u.bn.pVtab);
70242
+ sqlite3VtabImportErrmsg(p, u.bn.pVtab);
6981470243
#endif /* SQLITE_OMIT_VIRTUALTABLE */
6981570244
}else{
6981670245
assert( u.bn.pC->pCursor!=0 );
6981770246
rc = sqlite3VdbeCursorMoveto(u.bn.pC);
6981870247
if( rc ) goto abort_due_to_error;
@@ -69900,11 +70329,11 @@
6990070329
case OP_Sort: { /* jump */
6990170330
#ifdef SQLITE_TEST
6990270331
sqlite3_sort_count++;
6990370332
sqlite3_search_count--;
6990470333
#endif
69905
- p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
70334
+ p->aCounter[SQLITE_STMTSTATUS_SORT]++;
6990670335
/* Fall through into OP_Rewind */
6990770336
}
6990870337
/* Opcode: Rewind P1 P2 * * *
6990970338
**
6991070339
** The next use of the Rowid or Column or Next instruction for P1
@@ -69983,21 +70412,21 @@
6998370412
VdbeCursor *pC;
6998470413
int res;
6998570414
#endif /* local variables moved into u.br */
6998670415
6998770416
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69988
- assert( pOp->p5<=ArraySize(p->aCounter) );
70417
+ assert( pOp->p5<ArraySize(p->aCounter) );
6998970418
u.br.pC = p->apCsr[pOp->p1];
6999070419
if( u.br.pC==0 ){
6999170420
break; /* See ticket #2273 */
6999270421
}
6999370422
assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
6999470423
if( isSorter(u.br.pC) ){
6999570424
assert( pOp->opcode==OP_SorterNext );
6999670425
rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
6999770426
}else{
69998
- u.br.res = 1;
70427
+ /* u.br.res = 1; // Always initialized by the xAdvance() call */
6999970428
assert( u.br.pC->deferredMoveto==0 );
7000070429
assert( u.br.pC->pCursor );
7000170430
assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
7000270431
assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
7000370432
rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
@@ -70004,11 +70433,11 @@
7000470433
}
7000570434
u.br.pC->nullRow = (u8)u.br.res;
7000670435
u.br.pC->cacheStatus = CACHE_STALE;
7000770436
if( u.br.res==0 ){
7000870437
pc = pOp->p2 - 1;
70009
- if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
70438
+ p->aCounter[pOp->p5]++;
7001070439
#ifdef SQLITE_TEST
7001170440
sqlite3_search_count++;
7001270441
#endif
7001370442
}
7001470443
u.br.pC->rowidIsValid = 0;
@@ -70076,11 +70505,11 @@
7007670505
int res;
7007770506
UnpackedRecord r;
7007870507
#endif /* local variables moved into u.bt */
7007970508
7008070509
assert( pOp->p3>0 );
70081
- assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
70510
+ assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
7008270511
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7008370512
u.bt.pC = p->apCsr[pOp->p1];
7008470513
assert( u.bt.pC!=0 );
7008570514
u.bt.pCrsr = u.bt.pC->pCursor;
7008670515
if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70292,10 +70721,11 @@
7029270721
int nChange;
7029370722
#endif /* local variables moved into u.bx */
7029470723
7029570724
u.bx.nChange = 0;
7029670725
assert( p->readOnly==0 );
70726
+ assert( pOp->p1!=1 );
7029770727
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
7029870728
rc = sqlite3BtreeClearTable(
7029970729
db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
7030070730
);
7030170731
if( pOp->p3 ){
@@ -70498,11 +70928,11 @@
7049870928
assert( p->bIsReader );
7049970929
u.ca.nRoot = pOp->p2;
7050070930
assert( u.ca.nRoot>0 );
7050170931
u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
7050270932
if( u.ca.aRoot==0 ) goto no_mem;
70503
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
70933
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7050470934
u.ca.pnErr = &aMem[pOp->p3];
7050570935
assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
7050670936
assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
7050770937
pIn1 = &aMem[pOp->p1];
7050870938
for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70934,11 +71364,11 @@
7093471364
u.cg.apVal[u.cg.i] = u.cg.pRec;
7093571365
memAboutToChange(p, u.cg.pRec);
7093671366
sqlite3VdbeMemStoreType(u.cg.pRec);
7093771367
}
7093871368
u.cg.ctx.pFunc = pOp->p4.pFunc;
70939
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
71369
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7094071370
u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
7094171371
u.cg.pMem->n++;
7094271372
u.cg.ctx.s.flags = MEM_Null;
7094371373
u.cg.ctx.s.z = 0;
7094471374
u.cg.ctx.s.zMalloc = 0;
@@ -70983,11 +71413,11 @@
7098371413
*/
7098471414
case OP_AggFinal: {
7098571415
#if 0 /* local variables moved into u.ch */
7098671416
Mem *pMem;
7098771417
#endif /* local variables moved into u.ch */
70988
- assert( pOp->p1>0 && pOp->p1<=p->nMem );
71418
+ assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
7098971419
u.ch.pMem = &aMem[pOp->p1];
7099071420
assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
7099171421
rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
7099271422
if( rc ){
7099371423
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71249,11 +71679,11 @@
7124971679
#if 0 /* local variables moved into u.cl */
7125071680
VTable *pVTab;
7125171681
#endif /* local variables moved into u.cl */
7125271682
u.cl.pVTab = pOp->p4.pVtab;
7125371683
rc = sqlite3VtabBegin(db, u.cl.pVTab);
71254
- if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
71684
+ if( u.cl.pVTab ) sqlite3VtabImportErrmsg(p, u.cl.pVTab->pVtab);
7125571685
break;
7125671686
}
7125771687
#endif /* SQLITE_OMIT_VIRTUALTABLE */
7125871688
7125971689
#ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -71302,11 +71732,11 @@
7130271732
u.cm.pVtabCursor = 0;
7130371733
u.cm.pVtab = pOp->p4.pVtab->pVtab;
7130471734
u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
7130571735
assert(u.cm.pVtab && u.cm.pModule);
7130671736
rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
71307
- importVtabErrMsg(p, u.cm.pVtab);
71737
+ sqlite3VtabImportErrmsg(p, u.cm.pVtab);
7130871738
if( SQLITE_OK==rc ){
7130971739
/* Initialize sqlite3_vtab_cursor base class */
7131071740
u.cm.pVtabCursor->pVtab = u.cm.pVtab;
7131171741
7131271742
/* Initialize vdbe cursor object */
@@ -71382,11 +71812,11 @@
7138271812
}
7138371813
7138471814
p->inVtabMethod = 1;
7138571815
rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
7138671816
p->inVtabMethod = 0;
71387
- importVtabErrMsg(p, u.cn.pVtab);
71817
+ sqlite3VtabImportErrmsg(p, u.cn.pVtab);
7138871818
if( rc==SQLITE_OK ){
7138971819
u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
7139071820
}
7139171821
7139271822
if( u.cn.res ){
@@ -71414,11 +71844,11 @@
7141471844
sqlite3_context sContext;
7141571845
#endif /* local variables moved into u.co */
7141671846
7141771847
VdbeCursor *pCur = p->apCsr[pOp->p1];
7141871848
assert( pCur->pVtabCursor );
71419
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
71849
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7142071850
u.co.pDest = &aMem[pOp->p3];
7142171851
memAboutToChange(p, u.co.pDest);
7142271852
if( pCur->nullRow ){
7142371853
sqlite3VdbeMemSetNull(u.co.pDest);
7142471854
break;
@@ -71435,11 +71865,11 @@
7143571865
*/
7143671866
sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
7143771867
MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
7143871868
7143971869
rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
71440
- importVtabErrMsg(p, u.co.pVtab);
71870
+ sqlite3VtabImportErrmsg(p, u.co.pVtab);
7144171871
if( u.co.sContext.isError ){
7144271872
rc = u.co.sContext.isError;
7144371873
}
7144471874
7144571875
/* Copy the result of the function to the P3 register. We
@@ -71490,11 +71920,11 @@
7149071920
** some other method is next invoked on the save virtual table cursor.
7149171921
*/
7149271922
p->inVtabMethod = 1;
7149371923
rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
7149471924
p->inVtabMethod = 0;
71495
- importVtabErrMsg(p, u.cp.pVtab);
71925
+ sqlite3VtabImportErrmsg(p, u.cp.pVtab);
7149671926
if( rc==SQLITE_OK ){
7149771927
u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
7149871928
}
7149971929
7150071930
if( !u.cp.res ){
@@ -71529,11 +71959,11 @@
7152971959
testcase( u.cq.pName->enc==SQLITE_UTF16BE );
7153071960
testcase( u.cq.pName->enc==SQLITE_UTF16LE );
7153171961
rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
7153271962
if( rc==SQLITE_OK ){
7153371963
rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
71534
- importVtabErrMsg(p, u.cq.pVtab);
71964
+ sqlite3VtabImportErrmsg(p, u.cq.pVtab);
7153571965
p->expired = 0;
7153671966
}
7153771967
break;
7153871968
}
7153971969
#endif
@@ -71593,11 +72023,11 @@
7159372023
u.cr.pX++;
7159472024
}
7159572025
db->vtabOnConflict = pOp->p5;
7159672026
rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
7159772027
db->vtabOnConflict = vtabOnConflict;
71598
- importVtabErrMsg(p, u.cr.pVtab);
72028
+ sqlite3VtabImportErrmsg(p, u.cr.pVtab);
7159972029
if( rc==SQLITE_OK && pOp->p1 ){
7160072030
assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
7160172031
db->lastRowid = lastRowid = u.cr.rowid;
7160272032
}
7160372033
if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
@@ -71760,11 +72190,12 @@
7176072190
/* This is the only way out of this procedure. We have to
7176172191
** release the mutexes on btrees that were acquired at the
7176272192
** top. */
7176372193
vdbe_return:
7176472194
db->lastRowid = lastRowid;
71765
- p->aCounter[SQLITE_STMTSTATUS_VM_STEP-1] += (int)nVmStep;
72195
+ testcase( nVmStep>0 );
72196
+ p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
7176672197
sqlite3VdbeLeave(p);
7176772198
return rc;
7176872199
7176972200
/* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
7177072201
** is encountered.
@@ -75520,12 +75951,11 @@
7552075951
int op = p->op;
7552175952
if( op==TK_CAST || op==TK_UPLUS ){
7552275953
p = p->pLeft;
7552375954
continue;
7552475955
}
75525
- assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
75526
- if( op==TK_COLLATE ){
75956
+ if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
7552775957
pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
7552875958
break;
7552975959
}
7553075960
if( p->pTab!=0
7553175961
&& (op==TK_AGG_COLUMN || op==TK_COLUMN
@@ -76685,10 +77115,11 @@
7668577115
break;
7668677116
}
7668777117
case TK_UMINUS: {
7668877118
int v;
7668977119
if( sqlite3ExprIsInteger(p->pLeft, &v) ){
77120
+ assert( v!=(-2147483647-1) );
7669077121
*pValue = -v;
7669177122
rc = 1;
7669277123
}
7669377124
break;
7669477125
}
@@ -78910,10 +79341,11 @@
7891079341
compLeft.pRight = pExpr->x.pList->a[0].pExpr;
7891179342
compRight.op = TK_LE;
7891279343
compRight.pLeft = &exprX;
7891379344
compRight.pRight = pExpr->x.pList->a[1].pExpr;
7891479345
exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
79346
+ exprX.op2 = exprX.op;
7891579347
exprX.op = TK_REGISTER;
7891679348
if( jumpIfTrue ){
7891779349
sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
7891879350
}else{
7891979351
sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
@@ -80343,11 +80775,11 @@
8034380775
8034480776
/* Ensure the default expression is something that sqlite3ValueFromExpr()
8034580777
** can handle (i.e. not CURRENT_TIME etc.)
8034680778
*/
8034780779
if( pDflt ){
80348
- sqlite3_value *pVal;
80780
+ sqlite3_value *pVal = 0;
8034980781
if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
8035080782
db->mallocFailed = 1;
8035180783
return;
8035280784
}
8035380785
if( !pVal ){
@@ -80484,11 +80916,11 @@
8048480916
#endif /* SQLITE_ALTER_TABLE */
8048580917
8048680918
/************** End of alter.c ***********************************************/
8048780919
/************** Begin file analyze.c *****************************************/
8048880920
/*
80489
-** 2005 July 8
80921
+** 2005-07-08
8049080922
**
8049180923
** The author disclaims copyright to this source code. In place of
8049280924
** a legal notice, here is a blessing:
8049380925
**
8049480926
** May you do good and not evil.
@@ -80505,27 +80937,36 @@
8050580937
** The following system tables are or have been supported:
8050680938
**
8050780939
** CREATE TABLE sqlite_stat1(tbl, idx, stat);
8050880940
** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
8050980941
** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
80942
+** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
8051080943
**
8051180944
** Additional tables might be added in future releases of SQLite.
8051280945
** The sqlite_stat2 table is not created or used unless the SQLite version
8051380946
** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
8051480947
** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
8051580948
** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
8051680949
** created and used by SQLite versions 3.7.9 and later and with
80517
-** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
80518
-** is a superset of sqlite_stat2.
80950
+** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
80951
+** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
80952
+** version of sqlite_stat3 and is only available when compiled with
80953
+** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.0 and later. It is
80954
+** not possible to enable both STAT3 and STAT4 at the same time. If they
80955
+** are both enabled, then STAT4 is precedence.
80956
+**
80957
+** For most applications, sqlite_stat1 provides all the statisics required
80958
+** for the query planner to make good choices.
8051980959
**
8052080960
** Format of sqlite_stat1:
8052180961
**
8052280962
** There is normally one row per index, with the index identified by the
8052380963
** name in the idx column. The tbl column is the name of the table to
8052480964
** which the index belongs. In each such row, the stat column will be
8052580965
** a string consisting of a list of integers. The first integer in this
80526
-** list is the number of rows in the index and in the table. The second
80966
+** list is the number of rows in the index. (This is the same as the
80967
+** number of rows in the table, except for partial indices.) The second
8052780968
** integer is the average number of rows in the index that have the same
8052880969
** value in the first column of the index. The third integer is the average
8052980970
** number of rows in the index that have the same value for the first two
8053080971
** columns. The N-th integer (for N>1) is the average number of rows in
8053180972
** the index which have the same value for the first N-1 columns. For
@@ -80568,57 +81009,85 @@
8056881009
** writes the sqlite_stat2 table. This version of SQLite only supports
8056981010
** sqlite_stat3.
8057081011
**
8057181012
** Format for sqlite_stat3:
8057281013
**
80573
-** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
80574
-** used to avoid compatibility problems.
81014
+** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
81015
+** sqlite_stat4 format will be described first. Further information
81016
+** about sqlite_stat3 follows the sqlite_stat4 description.
8057581017
**
80576
-** The format of the sqlite_stat3 table is similar to the format of
80577
-** the sqlite_stat2 table. There are multiple entries for each index.
81018
+** Format for sqlite_stat4:
81019
+**
81020
+** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
81021
+** to aid the query planner in choosing good indices based on the values
81022
+** that indexed columns are compared against in the WHERE clauses of
81023
+** queries.
81024
+**
81025
+** The sqlite_stat4 table contains multiple entries for each index.
8057881026
** The idx column names the index and the tbl column is the table of the
8057981027
** index. If the idx and tbl columns are the same, then the sample is
80580
-** of the INTEGER PRIMARY KEY. The sample column is a value taken from
80581
-** the left-most column of the index. The nEq column is the approximate
80582
-** number of entires in the index whose left-most column exactly matches
80583
-** the sample. nLt is the approximate number of entires whose left-most
80584
-** column is less than the sample. The nDLt column is the approximate
80585
-** number of distinct left-most entries in the index that are less than
80586
-** the sample.
80587
-**
80588
-** Future versions of SQLite might change to store a string containing
80589
-** multiple integers values in the nDLt column of sqlite_stat3. The first
80590
-** integer will be the number of prior index entires that are distinct in
80591
-** the left-most column. The second integer will be the number of prior index
80592
-** entries that are distinct in the first two columns. The third integer
80593
-** will be the number of prior index entries that are distinct in the first
80594
-** three columns. And so forth. With that extension, the nDLt field is
80595
-** similar in function to the sqlite_stat1.stat field.
80596
-**
80597
-** There can be an arbitrary number of sqlite_stat3 entries per index.
80598
-** The ANALYZE command will typically generate sqlite_stat3 tables
81028
+** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
81029
+** binary encoding of a key from the index, with the trailing rowid
81030
+** omitted. The nEq column is a list of integers. The first integer
81031
+** is the approximate number of entries in the index whose left-most
81032
+** column exactly matches the left-most column of the sample. The second
81033
+** integer in nEq is the approximate number of entries in the index where
81034
+** the first two columns match the first two columns of the sample.
81035
+** And so forth. nLt is another list of integers that show the approximate
81036
+** number of entries that are strictly less than the sample. The first
81037
+** integer in nLt contains the number of entries in the index where the
81038
+** left-most column is less than the left-most column of the sample.
81039
+** The K-th integer in the nLt entry is the number of index entries
81040
+** where the first K columns are less than the first K columns of the
81041
+** sample. The nDLt column is like nLt except that it contains the
81042
+** number of distinct entries in the index that are less than the
81043
+** sample.
81044
+**
81045
+** There can be an arbitrary number of sqlite_stat4 entries per index.
81046
+** The ANALYZE command will typically generate sqlite_stat4 tables
8059981047
** that contain between 10 and 40 samples which are distributed across
8060081048
** the key space, though not uniformly, and which include samples with
80601
-** largest possible nEq values.
81049
+** large nEq values.
81050
+**
81051
+** Format for sqlite_stat3 redux:
81052
+**
81053
+** The sqlite_stat3 table is like sqlite_stat4 except that it only
81054
+** looks at the left-most column of the index. The sqlite_stat3.sample
81055
+** column contains the actual value of the left-most column instead
81056
+** of a blob encoding of the complete index key as is found in
81057
+** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
81058
+** all contain just a single integer which is the same as the first
81059
+** integer in the equivalent columns in sqlite_stat4.
8060281060
*/
8060381061
#ifndef SQLITE_OMIT_ANALYZE
8060481062
81063
+#if defined(SQLITE_ENABLE_STAT4)
81064
+# define IsStat4 1
81065
+# define IsStat3 0
81066
+#elif defined(SQLITE_ENABLE_STAT3)
81067
+# define IsStat4 0
81068
+# define IsStat3 1
81069
+#else
81070
+# define IsStat4 0
81071
+# define IsStat3 0
81072
+# undef SQLITE_STAT4_SAMPLES
81073
+# define SQLITE_STAT4_SAMPLES 1
81074
+#endif
81075
+#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
81076
+
8060581077
/*
80606
-** This routine generates code that opens the sqlite_stat1 table for
80607
-** writing with cursor iStatCur. If the library was built with the
80608
-** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
80609
-** opened for writing using cursor (iStatCur+1)
81078
+** This routine generates code that opens the sqlite_statN tables.
81079
+** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
81080
+** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
81081
+** appropriate compile-time options are provided.
8061081082
**
80611
-** If the sqlite_stat1 tables does not previously exist, it is created.
80612
-** Similarly, if the sqlite_stat3 table does not exist and the library
80613
-** is compiled with SQLITE_ENABLE_STAT3 defined, it is created.
81083
+** If the sqlite_statN tables do not previously exist, it is created.
8061481084
**
8061581085
** Argument zWhere may be a pointer to a buffer containing a table name,
8061681086
** or it may be a NULL pointer. If it is not NULL, then all entries in
80617
-** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
80618
-** with the named table are deleted. If zWhere==0, then code is generated
80619
-** to delete all stat table entries.
81087
+** the sqlite_statN tables associated with the named table are deleted.
81088
+** If zWhere==0, then code is generated to delete all stat table entries.
8062081089
*/
8062181090
static void openStatTable(
8062281091
Parse *pParse, /* Parsing context */
8062381092
int iDb, /* The database we are looking in */
8062481093
int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80628,22 +81097,28 @@
8062881097
static const struct {
8062981098
const char *zName;
8063081099
const char *zCols;
8063181100
} aTable[] = {
8063281101
{ "sqlite_stat1", "tbl,idx,stat" },
80633
-#ifdef SQLITE_ENABLE_STAT3
81102
+#if defined(SQLITE_ENABLE_STAT4)
81103
+ { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
81104
+ { "sqlite_stat3", 0 },
81105
+#elif defined(SQLITE_ENABLE_STAT3)
8063481106
{ "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
81107
+ { "sqlite_stat4", 0 },
81108
+#else
81109
+ { "sqlite_stat3", 0 },
81110
+ { "sqlite_stat4", 0 },
8063581111
#endif
8063681112
};
80637
-
80638
- int aRoot[] = {0, 0};
80639
- u8 aCreateTbl[] = {0, 0};
80640
-
8064181113
int i;
8064281114
sqlite3 *db = pParse->db;
8064381115
Db *pDb;
8064481116
Vdbe *v = sqlite3GetVdbe(pParse);
81117
+ int aRoot[ArraySize(aTable)];
81118
+ u8 aCreateTbl[ArraySize(aTable)];
81119
+
8064581120
if( v==0 ) return;
8064681121
assert( sqlite3BtreeHoldsAllMutexes(db) );
8064781122
assert( sqlite3VdbeDb(v)==db );
8064881123
pDb = &db->aDb[iDb];
8064981124
@@ -80652,262 +81127,592 @@
8065281127
*/
8065381128
for(i=0; i<ArraySize(aTable); i++){
8065481129
const char *zTab = aTable[i].zName;
8065581130
Table *pStat;
8065681131
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
80657
- /* The sqlite_stat[12] table does not exist. Create it. Note that a
80658
- ** side-effect of the CREATE TABLE statement is to leave the rootpage
80659
- ** of the new table in register pParse->regRoot. This is important
80660
- ** because the OpenWrite opcode below will be needing it. */
80661
- sqlite3NestedParse(pParse,
80662
- "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
80663
- );
80664
- aRoot[i] = pParse->regRoot;
80665
- aCreateTbl[i] = OPFLAG_P2ISREG;
81132
+ if( aTable[i].zCols ){
81133
+ /* The sqlite_statN table does not exist. Create it. Note that a
81134
+ ** side-effect of the CREATE TABLE statement is to leave the rootpage
81135
+ ** of the new table in register pParse->regRoot. This is important
81136
+ ** because the OpenWrite opcode below will be needing it. */
81137
+ sqlite3NestedParse(pParse,
81138
+ "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
81139
+ );
81140
+ aRoot[i] = pParse->regRoot;
81141
+ aCreateTbl[i] = OPFLAG_P2ISREG;
81142
+ }
8066681143
}else{
8066781144
/* The table already exists. If zWhere is not NULL, delete all entries
8066881145
** associated with the table zWhere. If zWhere is NULL, delete the
8066981146
** entire contents of the table. */
8067081147
aRoot[i] = pStat->tnum;
81148
+ aCreateTbl[i] = 0;
8067181149
sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
8067281150
if( zWhere ){
8067381151
sqlite3NestedParse(pParse,
80674
- "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
81152
+ "DELETE FROM %Q.%s WHERE %s=%Q",
81153
+ pDb->zName, zTab, zWhereType, zWhere
8067581154
);
8067681155
}else{
80677
- /* The sqlite_stat[12] table already exists. Delete all rows. */
81156
+ /* The sqlite_stat[134] table already exists. Delete all rows. */
8067881157
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
8067981158
}
8068081159
}
8068181160
}
8068281161
80683
- /* Open the sqlite_stat[13] tables for writing. */
80684
- for(i=0; i<ArraySize(aTable); i++){
81162
+ /* Open the sqlite_stat[134] tables for writing. */
81163
+ for(i=0; aTable[i].zCols; i++){
81164
+ assert( i<ArraySize(aTable) );
8068581165
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
8068681166
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
8068781167
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
8068881168
}
8068981169
}
8069081170
8069181171
/*
80692
-** Recommended number of samples for sqlite_stat3
81172
+** Recommended number of samples for sqlite_stat4
8069381173
*/
80694
-#ifndef SQLITE_STAT3_SAMPLES
80695
-# define SQLITE_STAT3_SAMPLES 24
81174
+#ifndef SQLITE_STAT4_SAMPLES
81175
+# define SQLITE_STAT4_SAMPLES 24
8069681176
#endif
8069781177
8069881178
/*
80699
-** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
81179
+** Three SQL functions - stat_init(), stat_push(), and stat_get() -
8070081180
** share an instance of the following structure to hold their state
8070181181
** information.
8070281182
*/
80703
-typedef struct Stat3Accum Stat3Accum;
80704
-struct Stat3Accum {
81183
+typedef struct Stat4Accum Stat4Accum;
81184
+typedef struct Stat4Sample Stat4Sample;
81185
+struct Stat4Sample {
81186
+ tRowcnt *anEq; /* sqlite_stat4.nEq */
81187
+ tRowcnt *anDLt; /* sqlite_stat4.nDLt */
81188
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81189
+ tRowcnt *anLt; /* sqlite_stat4.nLt */
81190
+ i64 iRowid; /* Rowid in main table of the key */
81191
+ u8 isPSample; /* True if a periodic sample */
81192
+ int iCol; /* If !isPSample, the reason for inclusion */
81193
+ u32 iHash; /* Tiebreaker hash */
81194
+#endif
81195
+};
81196
+struct Stat4Accum {
8070581197
tRowcnt nRow; /* Number of rows in the entire table */
8070681198
tRowcnt nPSample; /* How often to do a periodic sample */
80707
- int iMin; /* Index of entry with minimum nEq and hash */
81199
+ int nCol; /* Number of columns in index + rowid */
8070881200
int mxSample; /* Maximum number of samples to accumulate */
80709
- int nSample; /* Current number of samples */
81201
+ Stat4Sample current; /* Current row as a Stat4Sample */
8071081202
u32 iPrn; /* Pseudo-random number used for sampling */
80711
- struct Stat3Sample {
80712
- i64 iRowid; /* Rowid in main table of the key */
80713
- tRowcnt nEq; /* sqlite_stat3.nEq */
80714
- tRowcnt nLt; /* sqlite_stat3.nLt */
80715
- tRowcnt nDLt; /* sqlite_stat3.nDLt */
80716
- u8 isPSample; /* True if a periodic sample */
80717
- u32 iHash; /* Tiebreaker hash */
80718
- } *a; /* An array of samples */
81203
+ Stat4Sample *aBest; /* Array of (nCol-1) best samples */
81204
+ int iMin; /* Index in a[] of entry with minimum score */
81205
+ int nSample; /* Current number of samples */
81206
+ int iGet; /* Index of current sample accessed by stat_get() */
81207
+ Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
8071981208
};
8072081209
80721
-#ifdef SQLITE_ENABLE_STAT3
8072281210
/*
80723
-** Implementation of the stat3_init(C,S) SQL function. The two parameters
80724
-** are the number of rows in the table or index (C) and the number of samples
80725
-** to accumulate (S).
80726
-**
80727
-** This routine allocates the Stat3Accum object.
80728
-**
80729
-** The return value is the Stat3Accum object (P).
80730
-*/
80731
-static void stat3Init(
81211
+** Implementation of the stat_init(N,C) SQL function. The two parameters
81212
+** are the number of rows in the table or index (C) and the number of columns
81213
+** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
81214
+**
81215
+** This routine allocates the Stat4Accum object in heap memory. The return
81216
+** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
81217
+** the size of the blob is sizeof(void*) bytes).
81218
+*/
81219
+static void statInit(
8073281220
sqlite3_context *context,
8073381221
int argc,
8073481222
sqlite3_value **argv
8073581223
){
80736
- Stat3Accum *p;
80737
- tRowcnt nRow;
80738
- int mxSample;
80739
- int n;
81224
+ Stat4Accum *p;
81225
+ int nCol; /* Number of columns in index being sampled */
81226
+ int nColUp; /* nCol rounded up for alignment */
81227
+ int n; /* Bytes of space to allocate */
81228
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81229
+ int mxSample = SQLITE_STAT4_SAMPLES;
81230
+#endif
8074081231
81232
+ /* Decode the three function arguments */
8074181233
UNUSED_PARAMETER(argc);
80742
- nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
80743
- mxSample = sqlite3_value_int(argv[1]);
80744
- n = sizeof(*p) + sizeof(p->a[0])*mxSample;
80745
- p = sqlite3MallocZero( n );
81234
+ nCol = sqlite3_value_int(argv[0]);
81235
+ assert( nCol>1 ); /* >1 because it includes the rowid column */
81236
+ nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
81237
+
81238
+ /* Allocate the space required for the Stat4Accum object */
81239
+ n = sizeof(*p)
81240
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
81241
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
81242
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81243
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
81244
+ + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
81245
+ + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
81246
+#endif
81247
+ ;
81248
+ p = sqlite3MallocZero(n);
8074681249
if( p==0 ){
8074781250
sqlite3_result_error_nomem(context);
8074881251
return;
8074981252
}
80750
- p->a = (struct Stat3Sample*)&p[1];
80751
- p->nRow = nRow;
80752
- p->mxSample = mxSample;
80753
- p->nPSample = p->nRow/(mxSample/3+1) + 1;
80754
- sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81253
+
81254
+ p->nRow = 0;
81255
+ p->nCol = nCol;
81256
+ p->current.anDLt = (tRowcnt*)&p[1];
81257
+ p->current.anEq = &p->current.anDLt[nColUp];
81258
+
81259
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81260
+ {
81261
+ u8 *pSpace; /* Allocated space not yet assigned */
81262
+ int i; /* Used to iterate through p->aSample[] */
81263
+
81264
+ p->iGet = -1;
81265
+ p->mxSample = mxSample;
81266
+ p->nPSample = sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1;
81267
+ p->current.anLt = &p->current.anEq[nColUp];
81268
+ sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81269
+
81270
+ /* Set up the Stat4Accum.a[] and aBest[] arrays */
81271
+ p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
81272
+ p->aBest = &p->a[mxSample];
81273
+ pSpace = (u8*)(&p->a[mxSample+nCol]);
81274
+ for(i=0; i<(mxSample+nCol); i++){
81275
+ p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81276
+ p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81277
+ p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81278
+ }
81279
+ assert( (pSpace - (u8*)p)==n );
81280
+
81281
+ for(i=0; i<nCol; i++){
81282
+ p->aBest[i].iCol = i;
81283
+ }
81284
+ }
81285
+#endif
81286
+
81287
+ /* Return a pointer to the allocated object to the caller */
8075581288
sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
8075681289
}
80757
-static const FuncDef stat3InitFuncdef = {
80758
- 2, /* nArg */
80759
- SQLITE_UTF8, /* iPrefEnc */
80760
- 0, /* flags */
80761
- 0, /* pUserData */
80762
- 0, /* pNext */
80763
- stat3Init, /* xFunc */
80764
- 0, /* xStep */
80765
- 0, /* xFinalize */
80766
- "stat3_init", /* zName */
80767
- 0, /* pHash */
80768
- 0 /* pDestructor */
80769
-};
80770
-
80771
-
80772
-/*
80773
-** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
80774
-** arguments describe a single key instance. This routine makes the
80775
-** decision about whether or not to retain this key for the sqlite_stat3
80776
-** table.
80777
-**
80778
-** The return value is NULL.
80779
-*/
80780
-static void stat3Push(
80781
- sqlite3_context *context,
80782
- int argc,
80783
- sqlite3_value **argv
80784
-){
80785
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
80786
- tRowcnt nEq = sqlite3_value_int64(argv[0]);
80787
- tRowcnt nLt = sqlite3_value_int64(argv[1]);
80788
- tRowcnt nDLt = sqlite3_value_int64(argv[2]);
80789
- i64 rowid = sqlite3_value_int64(argv[3]);
80790
- u8 isPSample = 0;
80791
- u8 doInsert = 0;
80792
- int iMin = p->iMin;
80793
- struct Stat3Sample *pSample;
80794
- int i;
80795
- u32 h;
80796
-
80797
- UNUSED_PARAMETER(context);
80798
- UNUSED_PARAMETER(argc);
80799
- if( nEq==0 ) return;
80800
- h = p->iPrn = p->iPrn*1103515245 + 12345;
80801
- if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
80802
- doInsert = isPSample = 1;
80803
- }else if( p->nSample<p->mxSample ){
80804
- doInsert = 1;
80805
- }else{
80806
- if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
80807
- doInsert = 1;
80808
- }
80809
- }
80810
- if( !doInsert ) return;
80811
- if( p->nSample==p->mxSample ){
80812
- assert( p->nSample - iMin - 1 >= 0 );
80813
- memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
81290
+static const FuncDef statInitFuncdef = {
81291
+ 1+IsStat34, /* nArg */
81292
+ SQLITE_UTF8, /* iPrefEnc */
81293
+ 0, /* flags */
81294
+ 0, /* pUserData */
81295
+ 0, /* pNext */
81296
+ statInit, /* xFunc */
81297
+ 0, /* xStep */
81298
+ 0, /* xFinalize */
81299
+ "stat_init", /* zName */
81300
+ 0, /* pHash */
81301
+ 0 /* pDestructor */
81302
+};
81303
+
81304
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81305
+/*
81306
+** Return true if pNew is to be preferred over pOld.
81307
+*/
81308
+static int sampleIsBetter(Stat4Sample *pNew, Stat4Sample *pOld){
81309
+ tRowcnt nEqNew = pNew->anEq[pNew->iCol];
81310
+ tRowcnt nEqOld = pOld->anEq[pOld->iCol];
81311
+
81312
+ assert( pOld->isPSample==0 && pNew->isPSample==0 );
81313
+ assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
81314
+
81315
+ if( (nEqNew>nEqOld)
81316
+ || (nEqNew==nEqOld && pNew->iCol<pOld->iCol)
81317
+ || (nEqNew==nEqOld && pNew->iCol==pOld->iCol && pNew->iHash>pOld->iHash)
81318
+ ){
81319
+ return 1;
81320
+ }
81321
+ return 0;
81322
+}
81323
+
81324
+/*
81325
+** Copy the contents of object (*pFrom) into (*pTo).
81326
+*/
81327
+void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
81328
+ pTo->iRowid = pFrom->iRowid;
81329
+ pTo->isPSample = pFrom->isPSample;
81330
+ pTo->iCol = pFrom->iCol;
81331
+ pTo->iHash = pFrom->iHash;
81332
+ memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
81333
+ memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
81334
+ memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
81335
+}
81336
+
81337
+/*
81338
+** Copy the contents of sample *pNew into the p->a[] array. If necessary,
81339
+** remove the least desirable sample from p->a[] to make room.
81340
+*/
81341
+static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
81342
+ Stat4Sample *pSample;
81343
+ int i;
81344
+ i64 iSeq;
81345
+ i64 iPos;
81346
+
81347
+ assert( IsStat4 || nEqZero==0 );
81348
+
81349
+ if( pNew->isPSample==0 ){
81350
+ Stat4Sample *pUpgrade = 0;
81351
+ assert( pNew->anEq[pNew->iCol]>0 );
81352
+
81353
+ /* This sample is being added because the prefix that ends in column
81354
+ ** iCol occurs many times in the table. However, if we have already
81355
+ ** added a sample that shares this prefix, there is no need to add
81356
+ ** this one. Instead, upgrade the priority of the highest priority
81357
+ ** existing sample that shares this prefix. */
81358
+ for(i=p->nSample-1; i>=0; i--){
81359
+ Stat4Sample *pOld = &p->a[i];
81360
+ if( pOld->anEq[pNew->iCol]==0 ){
81361
+ if( pOld->isPSample ) return;
81362
+ assert( sampleIsBetter(pNew, pOld) );
81363
+ if( pUpgrade==0 || sampleIsBetter(pOld, pUpgrade) ){
81364
+ pUpgrade = pOld;
81365
+ }
81366
+ }
81367
+ }
81368
+ if( pUpgrade ){
81369
+ pUpgrade->iCol = pNew->iCol;
81370
+ pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
81371
+ goto find_new_min;
81372
+ }
81373
+ }
81374
+
81375
+ /* If necessary, remove sample iMin to make room for the new sample. */
81376
+ if( p->nSample>=p->mxSample ){
81377
+ Stat4Sample *pMin = &p->a[p->iMin];
81378
+ tRowcnt *anEq = pMin->anEq;
81379
+ tRowcnt *anLt = pMin->anLt;
81380
+ tRowcnt *anDLt = pMin->anDLt;
81381
+ memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
8081481382
pSample = &p->a[p->nSample-1];
80815
- }else{
80816
- pSample = &p->a[p->nSample++];
80817
- }
80818
- pSample->iRowid = rowid;
80819
- pSample->nEq = nEq;
80820
- pSample->nLt = nLt;
80821
- pSample->nDLt = nDLt;
80822
- pSample->iHash = h;
80823
- pSample->isPSample = isPSample;
80824
-
80825
- /* Find the new minimum */
80826
- if( p->nSample==p->mxSample ){
80827
- pSample = p->a;
80828
- i = 0;
80829
- while( pSample->isPSample ){
80830
- i++;
80831
- pSample++;
80832
- assert( i<p->nSample );
80833
- }
80834
- nEq = pSample->nEq;
80835
- h = pSample->iHash;
80836
- iMin = i;
80837
- for(i++, pSample++; i<p->nSample; i++, pSample++){
80838
- if( pSample->isPSample ) continue;
80839
- if( pSample->nEq<nEq
80840
- || (pSample->nEq==nEq && pSample->iHash<h)
80841
- ){
80842
- iMin = i;
80843
- nEq = pSample->nEq;
80844
- h = pSample->iHash;
80845
- }
80846
- }
81383
+ pSample->anEq = anEq;
81384
+ pSample->anDLt = anDLt;
81385
+ pSample->anLt = anLt;
81386
+ p->nSample = p->mxSample-1;
81387
+ }
81388
+
81389
+ /* Figure out where in the a[] array the new sample should be inserted. */
81390
+ iSeq = pNew->anLt[p->nCol-1];
81391
+ for(iPos=p->nSample; iPos>0; iPos--){
81392
+ if( iSeq>p->a[iPos-1].anLt[p->nCol-1] ) break;
81393
+ }
81394
+
81395
+ /* Insert the new sample */
81396
+ pSample = &p->a[iPos];
81397
+ if( iPos!=p->nSample ){
81398
+ Stat4Sample *pEnd = &p->a[p->nSample];
81399
+ tRowcnt *anEq = pEnd->anEq;
81400
+ tRowcnt *anLt = pEnd->anLt;
81401
+ tRowcnt *anDLt = pEnd->anDLt;
81402
+ memmove(&p->a[iPos], &p->a[iPos+1], (p->nSample-iPos)*sizeof(p->a[0]));
81403
+ pSample->anEq = anEq;
81404
+ pSample->anDLt = anDLt;
81405
+ pSample->anLt = anLt;
81406
+ }
81407
+ p->nSample++;
81408
+ sampleCopy(p, pSample, pNew);
81409
+
81410
+ /* Zero the first nEqZero entries in the anEq[] array. */
81411
+ memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
81412
+
81413
+ find_new_min:
81414
+ if( p->nSample>=p->mxSample ){
81415
+ int iMin = -1;
81416
+ for(i=0; i<p->mxSample; i++){
81417
+ if( p->a[i].isPSample ) continue;
81418
+ if( iMin<0 || sampleIsBetter(&p->a[iMin], &p->a[i]) ){
81419
+ iMin = i;
81420
+ }
81421
+ }
81422
+ assert( iMin>=0 );
8084781423
p->iMin = iMin;
8084881424
}
8084981425
}
80850
-static const FuncDef stat3PushFuncdef = {
80851
- 5, /* nArg */
80852
- SQLITE_UTF8, /* iPrefEnc */
80853
- 0, /* flags */
80854
- 0, /* pUserData */
80855
- 0, /* pNext */
80856
- stat3Push, /* xFunc */
80857
- 0, /* xStep */
80858
- 0, /* xFinalize */
80859
- "stat3_push", /* zName */
80860
- 0, /* pHash */
80861
- 0 /* pDestructor */
80862
-};
80863
-
80864
-/*
80865
-** Implementation of the stat3_get(P,N,...) SQL function. This routine is
80866
-** used to query the results. Content is returned for the Nth sqlite_stat3
80867
-** row where N is between 0 and S-1 and S is the number of samples. The
80868
-** value returned depends on the number of arguments.
80869
-**
80870
-** argc==2 result: rowid
80871
-** argc==3 result: nEq
80872
-** argc==4 result: nLt
80873
-** argc==5 result: nDLt
80874
-*/
80875
-static void stat3Get(
81426
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81427
+
81428
+/*
81429
+** Field iChng of the index being scanned has changed. So at this point
81430
+** p->current contains a sample that reflects the previous row of the
81431
+** index. The value of anEq[iChng] and subsequent anEq[] elements are
81432
+** correct at this point.
81433
+*/
81434
+static void samplePushPrevious(Stat4Accum *p, int iChng){
81435
+#ifdef SQLITE_ENABLE_STAT4
81436
+ int i;
81437
+
81438
+ /* Check if any samples from the aBest[] array should be pushed
81439
+ ** into IndexSample.a[] at this point. */
81440
+ for(i=(p->nCol-2); i>=iChng; i--){
81441
+ Stat4Sample *pBest = &p->aBest[i];
81442
+ if( p->nSample<p->mxSample
81443
+ || sampleIsBetter(pBest, &p->a[p->iMin])
81444
+ ){
81445
+ sampleInsert(p, pBest, i);
81446
+ }
81447
+ }
81448
+
81449
+ /* Update the anEq[] fields of any samples already collected. */
81450
+ for(i=p->nSample-1; i>=0; i--){
81451
+ int j;
81452
+ for(j=iChng; j<p->nCol; j++){
81453
+ if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
81454
+ }
81455
+ }
81456
+#endif
81457
+
81458
+#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
81459
+ if( iChng==0 ){
81460
+ tRowcnt nLt = p->current.anLt[0];
81461
+ tRowcnt nEq = p->current.anEq[0];
81462
+
81463
+ /* Check if this is to be a periodic sample. If so, add it. */
81464
+ if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
81465
+ p->current.isPSample = 1;
81466
+ sampleInsert(p, &p->current, 0);
81467
+ p->current.isPSample = 0;
81468
+ }else
81469
+
81470
+ /* Or if it is a non-periodic sample. Add it in this case too. */
81471
+ if( p->nSample<p->mxSample || sampleIsBetter(&p->current, &p->a[p->iMin]) ){
81472
+ sampleInsert(p, &p->current, 0);
81473
+ }
81474
+ }
81475
+#endif
81476
+}
81477
+
81478
+/*
81479
+** Implementation of the stat_push SQL function: stat_push(P,R,C)
81480
+** Arguments:
81481
+**
81482
+** P Pointer to the Stat4Accum object created by stat_init()
81483
+** C Index of left-most column to differ from previous row
81484
+** R Rowid for the current row
81485
+**
81486
+** The SQL function always returns NULL.
81487
+**
81488
+** The R parameter is only used for STAT3 and STAT4.
81489
+*/
81490
+static void statPush(
81491
+ sqlite3_context *context,
81492
+ int argc,
81493
+ sqlite3_value **argv
81494
+){
81495
+ int i;
81496
+
81497
+ /* The three function arguments */
81498
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81499
+ int iChng = sqlite3_value_int(argv[1]);
81500
+
81501
+ assert( p->nCol>1 ); /* Includes rowid field */
81502
+ assert( iChng<p->nCol );
81503
+
81504
+ if( p->nRow==0 ){
81505
+ /* anEq[0] is only zero for the very first call to this function. Do
81506
+ ** appropriate initialization */
81507
+ for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
81508
+ }else{
81509
+ /* Second and subsequent calls get processed here */
81510
+ samplePushPrevious(p, iChng);
81511
+
81512
+ /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
81513
+ ** to the current row of the index. */
81514
+ for(i=0; i<iChng; i++){
81515
+ p->current.anEq[i]++;
81516
+ }
81517
+ for(i=iChng; i<p->nCol; i++){
81518
+ p->current.anDLt[i]++;
81519
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81520
+ p->current.anLt[i] += p->current.anEq[i];
81521
+#endif
81522
+ p->current.anEq[i] = 1;
81523
+ }
81524
+ }
81525
+ p->nRow++;
81526
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81527
+ p->current.iRowid = sqlite3_value_int64(argv[2]);
81528
+ p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
81529
+#endif
81530
+
81531
+#ifdef SQLITE_ENABLE_STAT4
81532
+ {
81533
+ tRowcnt nLt = p->current.anLt[p->nCol-1];
81534
+
81535
+ /* Check if this is to be a periodic sample. If so, add it. */
81536
+ if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
81537
+ p->current.isPSample = 1;
81538
+ p->current.iCol = 0;
81539
+ sampleInsert(p, &p->current, p->nCol-1);
81540
+ p->current.isPSample = 0;
81541
+ }
81542
+
81543
+ /* Update the aBest[] array. */
81544
+ for(i=0; i<(p->nCol-1); i++){
81545
+ p->current.iCol = i;
81546
+ if( i>=iChng || sampleIsBetter(&p->current, &p->aBest[i]) ){
81547
+ sampleCopy(p, &p->aBest[i], &p->current);
81548
+ }
81549
+ }
81550
+ }
81551
+#endif
81552
+}
81553
+static const FuncDef statPushFuncdef = {
81554
+ 2+IsStat34, /* nArg */
81555
+ SQLITE_UTF8, /* iPrefEnc */
81556
+ 0, /* flags */
81557
+ 0, /* pUserData */
81558
+ 0, /* pNext */
81559
+ statPush, /* xFunc */
81560
+ 0, /* xStep */
81561
+ 0, /* xFinalize */
81562
+ "stat_push", /* zName */
81563
+ 0, /* pHash */
81564
+ 0 /* pDestructor */
81565
+};
81566
+
81567
+#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
81568
+#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
81569
+#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
81570
+#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
81571
+#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
81572
+
81573
+/*
81574
+** Implementation of the stat_get(P,J) SQL function. This routine is
81575
+** used to query the results. Content is returned for parameter J
81576
+** which is one of the STAT_GET_xxxx values defined above.
81577
+**
81578
+** If neither STAT3 nor STAT4 are enabled, then J is always
81579
+** STAT_GET_STAT1 and is hence omitted and this routine becomes
81580
+** a one-parameter function, stat_get(P), that always returns the
81581
+** stat1 table entry information.
81582
+*/
81583
+static void statGet(
8087681584
sqlite3_context *context,
8087781585
int argc,
8087881586
sqlite3_value **argv
8087981587
){
80880
- int n = sqlite3_value_int(argv[1]);
80881
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
80882
-
80883
- assert( p!=0 );
80884
- if( p->nSample<=n ) return;
80885
- switch( argc ){
80886
- case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
80887
- case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
80888
- case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
80889
- default: sqlite3_result_int64(context, p->a[n].nDLt); break;
80890
- }
80891
-}
80892
-static const FuncDef stat3GetFuncdef = {
80893
- -1, /* nArg */
80894
- SQLITE_UTF8, /* iPrefEnc */
80895
- 0, /* flags */
80896
- 0, /* pUserData */
80897
- 0, /* pNext */
80898
- stat3Get, /* xFunc */
80899
- 0, /* xStep */
80900
- 0, /* xFinalize */
80901
- "stat3_get", /* zName */
80902
- 0, /* pHash */
80903
- 0 /* pDestructor */
80904
-};
80905
-#endif /* SQLITE_ENABLE_STAT3 */
80906
-
80907
-
80908
-
81588
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81589
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81590
+ /* STAT3 and STAT4 have a parameter on this routine. */
81591
+ int eCall = sqlite3_value_int(argv[1]);
81592
+ assert( argc==2 );
81593
+ assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
81594
+ || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
81595
+ || eCall==STAT_GET_NDLT
81596
+ );
81597
+ if( eCall==STAT_GET_STAT1 )
81598
+#else
81599
+ assert( argc==1 );
81600
+#endif
81601
+ {
81602
+ /* Return the value to store in the "stat" column of the sqlite_stat1
81603
+ ** table for this index.
81604
+ **
81605
+ ** The value is a string composed of a list of integers describing
81606
+ ** the index. The first integer in the list is the total number of
81607
+ ** entries in the index. There is one additional integer in the list
81608
+ ** for each indexed column. This additional integer is an estimate of
81609
+ ** the number of rows matched by a stabbing query on the index using
81610
+ ** a key with the corresponding number of fields. In other words,
81611
+ ** if the index is on columns (a,b) and the sqlite_stat1 value is
81612
+ ** "100 10 2", then SQLite estimates that:
81613
+ **
81614
+ ** * the index contains 100 rows,
81615
+ ** * "WHERE a=?" matches 10 rows, and
81616
+ ** * "WHERE a=? AND b=?" matches 2 rows.
81617
+ **
81618
+ ** If D is the count of distinct values and K is the total number of
81619
+ ** rows, then each estimate is computed as:
81620
+ **
81621
+ ** I = (K+D-1)/D
81622
+ */
81623
+ char *z;
81624
+ int i;
81625
+
81626
+ char *zRet = sqlite3MallocZero(p->nCol * 25);
81627
+ if( zRet==0 ){
81628
+ sqlite3_result_error_nomem(context);
81629
+ return;
81630
+ }
81631
+
81632
+ sqlite3_snprintf(24, zRet, "%lld", p->nRow);
81633
+ z = zRet + sqlite3Strlen30(zRet);
81634
+ for(i=0; i<(p->nCol-1); i++){
81635
+ i64 nDistinct = p->current.anDLt[i] + 1;
81636
+ i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81637
+ sqlite3_snprintf(24, z, " %lld", iVal);
81638
+ z += sqlite3Strlen30(z);
81639
+ assert( p->current.anEq[i] );
81640
+ }
81641
+ assert( z[0]=='\0' && z>zRet );
81642
+
81643
+ sqlite3_result_text(context, zRet, -1, sqlite3_free);
81644
+ }
81645
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81646
+ else if( eCall==STAT_GET_ROWID ){
81647
+ if( p->iGet<0 ){
81648
+ samplePushPrevious(p, 0);
81649
+ p->iGet = 0;
81650
+ }
81651
+ if( p->iGet<p->nSample ){
81652
+ sqlite3_result_int64(context, p->a[p->iGet].iRowid);
81653
+ }
81654
+ }else{
81655
+ tRowcnt *aCnt = 0;
81656
+
81657
+ assert( p->iGet<p->nSample );
81658
+ switch( eCall ){
81659
+ case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
81660
+ case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
81661
+ default: {
81662
+ aCnt = p->a[p->iGet].anDLt;
81663
+ p->iGet++;
81664
+ break;
81665
+ }
81666
+ }
81667
+
81668
+ if( IsStat3 ){
81669
+ sqlite3_result_int64(context, (i64)aCnt[0]);
81670
+ }else{
81671
+ char *zRet = sqlite3MallocZero(p->nCol * 25);
81672
+ if( zRet==0 ){
81673
+ sqlite3_result_error_nomem(context);
81674
+ }else{
81675
+ int i;
81676
+ char *z = zRet;
81677
+ for(i=0; i<p->nCol; i++){
81678
+ sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
81679
+ z += sqlite3Strlen30(z);
81680
+ }
81681
+ assert( z[0]=='\0' && z>zRet );
81682
+ z[-1] = '\0';
81683
+ sqlite3_result_text(context, zRet, -1, sqlite3_free);
81684
+ }
81685
+ }
81686
+ }
81687
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81688
+}
81689
+static const FuncDef statGetFuncdef = {
81690
+ 1+IsStat34, /* nArg */
81691
+ SQLITE_UTF8, /* iPrefEnc */
81692
+ 0, /* flags */
81693
+ 0, /* pUserData */
81694
+ 0, /* pNext */
81695
+ statGet, /* xFunc */
81696
+ 0, /* xStep */
81697
+ 0, /* xFinalize */
81698
+ "stat_get", /* zName */
81699
+ 0, /* pHash */
81700
+ 0 /* pDestructor */
81701
+};
81702
+
81703
+static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
81704
+ assert( regOut!=regStat4 && regOut!=regStat4+1 );
81705
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81706
+ sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
81707
+#else
81708
+ assert( iParam==STAT_GET_STAT1 );
81709
+#endif
81710
+ sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
81711
+ sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
81712
+ sqlite3VdbeChangeP5(v, 1 + IsStat34);
81713
+}
8090981714
8091081715
/*
8091181716
** Generate code to do an analysis of all indices associated with
8091281717
** a single table.
8091381718
*/
@@ -80914,46 +81719,35 @@
8091481719
static void analyzeOneTable(
8091581720
Parse *pParse, /* Parser context */
8091681721
Table *pTab, /* Table whose indices are to be analyzed */
8091781722
Index *pOnlyIdx, /* If not NULL, only analyze this one index */
8091881723
int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
80919
- int iMem /* Available memory locations begin here */
81724
+ int iMem, /* Available memory locations begin here */
81725
+ int iTab /* Next available cursor */
8092081726
){
8092181727
sqlite3 *db = pParse->db; /* Database handle */
8092281728
Index *pIdx; /* An index to being analyzed */
8092381729
int iIdxCur; /* Cursor open on index being analyzed */
81730
+ int iTabCur; /* Table cursor */
8092481731
Vdbe *v; /* The virtual machine being built up */
8092581732
int i; /* Loop counter */
80926
- int topOfLoop; /* The top of the loop */
80927
- int endOfLoop; /* The end of the loop */
8092881733
int jZeroRows = -1; /* Jump from here if number of rows is zero */
8092981734
int iDb; /* Index of database containing pTab */
8093081735
u8 needTableCnt = 1; /* True to count the table */
81736
+ int regNewRowid = iMem++; /* Rowid for the inserted record */
81737
+ int regStat4 = iMem++; /* Register to hold Stat4Accum object */
81738
+ int regChng = iMem++; /* Index of changed index field */
81739
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81740
+ int regRowid = iMem++; /* Rowid argument passed to stat_push() */
81741
+#endif
81742
+ int regTemp = iMem++; /* Temporary use register */
8093181743
int regTabname = iMem++; /* Register containing table name */
8093281744
int regIdxname = iMem++; /* Register containing index name */
80933
- int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
80934
-#ifdef SQLITE_ENABLE_STAT3
80935
- int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
80936
- int regNumLt = iMem++; /* Number of keys less than regSample */
80937
- int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
80938
- int regSample = iMem++; /* The next sample value */
80939
- int regRowid = regSample; /* Rowid of a sample */
80940
- int regAccum = iMem++; /* Register to hold Stat3Accum object */
80941
- int regLoop = iMem++; /* Loop counter */
80942
- int regCount = iMem++; /* Number of rows in the table or index */
80943
- int regTemp1 = iMem++; /* Intermediate register */
80944
- int regTemp2 = iMem++; /* Intermediate register */
80945
- int once = 1; /* One-time initialization */
80946
- int shortJump = 0; /* Instruction address */
80947
- int iTabCur = pParse->nTab++; /* Table cursor */
80948
-#endif
80949
- int regCol = iMem++; /* Content of a column in analyzed table */
80950
- int regRec = iMem++; /* Register holding completed record */
80951
- int regTemp = iMem++; /* Temporary use register */
80952
- int regNewRowid = iMem++; /* Rowid for the inserted record */
80953
-
80954
-
81745
+ int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
81746
+ int regPrev = iMem; /* MUST BE LAST (see below) */
81747
+
81748
+ pParse->nMem = MAX(pParse->nMem, iMem);
8095581749
v = sqlite3GetVdbe(pParse);
8095681750
if( v==0 || NEVER(pTab==0) ){
8095781751
return;
8095881752
}
8095981753
if( pTab->tnum==0 ){
@@ -80973,217 +81767,230 @@
8097381767
db->aDb[iDb].zName ) ){
8097481768
return;
8097581769
}
8097681770
#endif
8097781771
80978
- /* Establish a read-lock on the table at the shared-cache level. */
81772
+ /* Establish a read-lock on the table at the shared-cache level.
81773
+ ** Open a read-only cursor on the table. Also allocate a cursor number
81774
+ ** to use for scanning indexes (iIdxCur). No index cursor is opened at
81775
+ ** this time though. */
8097981776
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
80980
-
80981
- iIdxCur = pParse->nTab++;
81777
+ iTabCur = iTab++;
81778
+ iIdxCur = iTab++;
81779
+ pParse->nTab = MAX(pParse->nTab, iTab);
81780
+ sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
8098281781
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
81782
+
8098381783
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
80984
- int nCol;
80985
- KeyInfo *pKey;
80986
- int addrIfNot = 0; /* address of OP_IfNot */
80987
- int *aChngAddr; /* Array of jump instruction addresses */
81784
+ int nCol; /* Number of columns indexed by pIdx */
81785
+ KeyInfo *pKey; /* KeyInfo structure for pIdx */
81786
+ int *aGotoChng; /* Array of jump instruction addresses */
81787
+ int addrRewind; /* Address of "OP_Rewind iIdxCur" */
81788
+ int addrGotoChng0; /* Address of "Goto addr_chng_0" */
81789
+ int addrNextRow; /* Address of "next_row:" */
8098881790
8098981791
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
8099081792
if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
8099181793
VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
8099281794
nCol = pIdx->nColumn;
80993
- aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
80994
- if( aChngAddr==0 ) continue;
81795
+ aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
81796
+ if( aGotoChng==0 ) continue;
8099581797
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
80996
- if( iMem+1+(nCol*2)>pParse->nMem ){
80997
- pParse->nMem = iMem+1+(nCol*2);
80998
- }
80999
-
81000
- /* Open a cursor to the index to be analyzed. */
81001
- assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81002
- sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
81003
- (char *)pKey, P4_KEYINFO_HANDOFF);
81004
- VdbeComment((v, "%s", pIdx->zName));
8100581798
8100681799
/* Populate the register containing the index name. */
8100781800
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
8100881801
81009
-#ifdef SQLITE_ENABLE_STAT3
81010
- if( once ){
81011
- once = 0;
81012
- sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81013
- }
81014
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
81015
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
81016
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
81017
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
81018
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
81019
- sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
81020
- sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
81021
- (char*)&stat3InitFuncdef, P4_FUNCDEF);
81022
- sqlite3VdbeChangeP5(v, 2);
81023
-#endif /* SQLITE_ENABLE_STAT3 */
81024
-
81025
- /* The block of memory cells initialized here is used as follows.
81026
- **
81027
- ** iMem:
81028
- ** The total number of rows in the table.
81029
- **
81030
- ** iMem+1 .. iMem+nCol:
81031
- ** Number of distinct entries in index considering the
81032
- ** left-most N columns only, where N is between 1 and nCol,
81033
- ** inclusive.
81034
- **
81035
- ** iMem+nCol+1 .. Mem+2*nCol:
81036
- ** Previous value of indexed columns, from left to right.
81037
- **
81038
- ** Cells iMem through iMem+nCol are initialized to 0. The others are
81039
- ** initialized to contain an SQL NULL.
81040
- */
81041
- for(i=0; i<=nCol; i++){
81042
- sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
81043
- }
81044
- for(i=0; i<nCol; i++){
81045
- sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
81046
- }
81047
-
81048
- /* Start the analysis loop. This loop runs through all the entries in
81049
- ** the index b-tree. */
81050
- endOfLoop = sqlite3VdbeMakeLabel(v);
81051
- sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
81052
- topOfLoop = sqlite3VdbeCurrentAddr(v);
81053
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
81054
-
81055
- for(i=0; i<nCol; i++){
81056
- CollSeq *pColl;
81057
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
81058
- if( i==0 ){
81059
- /* Always record the very first row */
81060
- addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
81061
- }
81062
- assert( pIdx->azColl!=0 );
81063
- assert( pIdx->azColl[i]!=0 );
81064
- pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81065
- aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
81066
- (char*)pColl, P4_COLLSEQ);
81802
+ /*
81803
+ ** Pseudo-code for loop that calls stat_push():
81804
+ **
81805
+ ** Rewind csr
81806
+ ** if eof(csr) goto end_of_scan;
81807
+ ** regChng = 0
81808
+ ** goto chng_addr_0;
81809
+ **
81810
+ ** next_row:
81811
+ ** regChng = 0
81812
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81813
+ ** regChng = 1
81814
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81815
+ ** ...
81816
+ ** regChng = N
81817
+ ** goto chng_addr_N
81818
+ **
81819
+ ** chng_addr_0:
81820
+ ** regPrev(0) = idx(0)
81821
+ ** chng_addr_1:
81822
+ ** regPrev(1) = idx(1)
81823
+ ** ...
81824
+ **
81825
+ ** chng_addr_N:
81826
+ ** regRowid = idx(rowid)
81827
+ ** stat_push(P, regChng, regRowid)
81828
+ ** Next csr
81829
+ ** if !eof(csr) goto next_row;
81830
+ **
81831
+ ** end_of_scan:
81832
+ */
81833
+
81834
+ /* Make sure there are enough memory cells allocated to accommodate
81835
+ ** the regPrev array and a trailing rowid (the rowid slot is required
81836
+ ** when building a record to insert into the sample column of
81837
+ ** the sqlite_stat4 table. */
81838
+ pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
81839
+
81840
+ /* Open a read-only cursor on the index being analyzed. */
81841
+ assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81842
+ sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
81843
+ sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
81844
+ VdbeComment((v, "%s", pIdx->zName));
81845
+
81846
+ /* Invoke the stat_init() function. The arguments are:
81847
+ **
81848
+ ** (1) the number of columns in the index including the rowid,
81849
+ ** (2) the number of rows in the index,
81850
+ **
81851
+ ** The second argument is only used for STAT3 and STAT4
81852
+ */
81853
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81854
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
81855
+#endif
81856
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
81857
+ sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
81858
+ sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
81859
+ sqlite3VdbeChangeP5(v, 1+IsStat34);
81860
+
81861
+ /* Implementation of the following:
81862
+ **
81863
+ ** Rewind csr
81864
+ ** if eof(csr) goto end_of_scan;
81865
+ ** regChng = 0
81866
+ ** goto next_push_0;
81867
+ **
81868
+ */
81869
+ addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
81870
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
81871
+ addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
81872
+
81873
+ /*
81874
+ ** next_row:
81875
+ ** regChng = 0
81876
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81877
+ ** regChng = 1
81878
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81879
+ ** ...
81880
+ ** regChng = N
81881
+ ** goto chng_addr_N
81882
+ */
81883
+ addrNextRow = sqlite3VdbeCurrentAddr(v);
81884
+ for(i=0; i<nCol; i++){
81885
+ char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81886
+ sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
81887
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
81888
+ aGotoChng[i] =
81889
+ sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
8106781890
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
81068
- VdbeComment((v, "jump if column %d changed", i));
81069
-#ifdef SQLITE_ENABLE_STAT3
81070
- if( i==0 ){
81071
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
81072
- VdbeComment((v, "incr repeat count"));
81073
- }
81074
-#endif
81075
- }
81076
- sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
81077
- for(i=0; i<nCol; i++){
81078
- sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
81079
- if( i==0 ){
81080
- sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
81081
-#ifdef SQLITE_ENABLE_STAT3
81082
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81083
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
81084
- sqlite3VdbeChangeP5(v, 5);
81085
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
81086
- sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
81087
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
81088
- sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
81089
-#endif
81090
- }
81091
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
81092
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
81093
- }
81094
- sqlite3DbFree(db, aChngAddr);
81095
-
81096
- /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
81097
- sqlite3VdbeResolveLabel(v, endOfLoop);
81098
-
81099
- sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
81100
- sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81101
-#ifdef SQLITE_ENABLE_STAT3
81102
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81103
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
81104
- sqlite3VdbeChangeP5(v, 5);
81105
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
81106
- shortJump =
81107
- sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
81108
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
81109
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81110
- sqlite3VdbeChangeP5(v, 2);
81111
- sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
81112
- sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
81113
- sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
81114
- sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
81115
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
81116
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81117
- sqlite3VdbeChangeP5(v, 3);
81118
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
81119
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81120
- sqlite3VdbeChangeP5(v, 4);
81121
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
81122
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81123
- sqlite3VdbeChangeP5(v, 5);
81124
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
81125
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81126
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
81127
- sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
81128
- sqlite3VdbeJumpHere(v, shortJump+2);
81129
-#endif
81130
-
81131
- /* Store the results in sqlite_stat1.
81132
- **
81133
- ** The result is a single row of the sqlite_stat1 table. The first
81134
- ** two columns are the names of the table and index. The third column
81135
- ** is a string composed of a list of integer statistics about the
81136
- ** index. The first integer in the list is the total number of entries
81137
- ** in the index. There is one additional integer in the list for each
81138
- ** column of the table. This additional integer is a guess of how many
81139
- ** rows of the table the index will select. If D is the count of distinct
81140
- ** values and K is the total number of rows, then the integer is computed
81141
- ** as:
81142
- **
81143
- ** I = (K+D-1)/D
81144
- **
81145
- ** If K==0 then no entry is made into the sqlite_stat1 table.
81146
- ** If K>0 then it is always the case the D>0 so division by zero
81147
- ** is never possible.
81148
- */
81149
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
81150
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
81151
- for(i=0; i<nCol; i++){
81152
- sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
81153
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81154
- sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
81155
- sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
81156
- sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
81157
- sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
81158
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81159
- }
81160
- if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81161
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81162
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81163
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81164
- sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81165
- if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81166
- }
81891
+ }
81892
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
81893
+ aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
81894
+
81895
+ /*
81896
+ ** chng_addr_0:
81897
+ ** regPrev(0) = idx(0)
81898
+ ** chng_addr_1:
81899
+ ** regPrev(1) = idx(1)
81900
+ ** ...
81901
+ */
81902
+ sqlite3VdbeJumpHere(v, addrGotoChng0);
81903
+ for(i=0; i<nCol; i++){
81904
+ sqlite3VdbeJumpHere(v, aGotoChng[i]);
81905
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
81906
+ }
81907
+
81908
+ /*
81909
+ ** chng_addr_N:
81910
+ ** regRowid = idx(rowid) // STAT34 only
81911
+ ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
81912
+ ** Next csr
81913
+ ** if !eof(csr) goto next_row;
81914
+ */
81915
+ sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
81916
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81917
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
81918
+ assert( regRowid==(regStat4+2) );
81919
+#endif
81920
+ assert( regChng==(regStat4+1) );
81921
+ sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
81922
+ sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
81923
+ sqlite3VdbeChangeP5(v, 2+IsStat34);
81924
+ sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
81925
+
81926
+ /* Add the entry to the stat1 table. */
81927
+ callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
81928
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
81929
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81930
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
81931
+ sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81932
+
81933
+ /* Add the entries to the stat3 or stat4 table. */
81934
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81935
+ {
81936
+ int regEq = regStat1;
81937
+ int regLt = regStat1+1;
81938
+ int regDLt = regStat1+2;
81939
+ int regSample = regStat1+3;
81940
+ int regCol = regStat1+4;
81941
+ int regSampleRowid = regCol + nCol;
81942
+ int addrNext;
81943
+ int addrIsNull;
81944
+
81945
+ pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
81946
+
81947
+ addrNext = sqlite3VdbeCurrentAddr(v);
81948
+ callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
81949
+ addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
81950
+ callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
81951
+ callStatGet(v, regStat4, STAT_GET_NLT, regLt);
81952
+ callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
81953
+ sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
81954
+#ifdef SQLITE_ENABLE_STAT3
81955
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
81956
+ pIdx->aiColumn[0], regSample);
81957
+#else
81958
+ for(i=0; i<nCol; i++){
81959
+ int iCol = pIdx->aiColumn[i];
81960
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
81961
+ }
81962
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
81963
+#endif
81964
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
81965
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81966
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
81967
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
81968
+ sqlite3VdbeJumpHere(v, addrIsNull);
81969
+ }
81970
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81971
+
81972
+ /* End of analysis */
81973
+ sqlite3VdbeJumpHere(v, addrRewind);
81974
+ sqlite3DbFree(db, aGotoChng);
81975
+ }
81976
+
8116781977
8116881978
/* Create a single sqlite_stat1 entry containing NULL as the index
8116981979
** name and the row count as the content.
8117081980
*/
8117181981
if( pOnlyIdx==0 && needTableCnt ){
81172
- sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
8117381982
VdbeComment((v, "%s", pTab->zName));
81174
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
81175
- sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81983
+ sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
8117681984
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
8117781985
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
81178
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81986
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
8117981987
sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81180
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81988
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
8118181989
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
8118281990
sqlite3VdbeJumpHere(v, jZeroRows);
8118381991
}
81184
- if( pParse->nMem<regRec ) pParse->nMem = regRec;
8118581992
}
8118681993
8118781994
8118881995
/*
8118981996
** Generate code that will cause the most recent index analysis to
@@ -81203,20 +82010,22 @@
8120382010
sqlite3 *db = pParse->db;
8120482011
Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
8120582012
HashElem *k;
8120682013
int iStatCur;
8120782014
int iMem;
82015
+ int iTab;
8120882016
8120982017
sqlite3BeginWriteOperation(pParse, 0, iDb);
8121082018
iStatCur = pParse->nTab;
8121182019
pParse->nTab += 3;
8121282020
openStatTable(pParse, iDb, iStatCur, 0, 0);
8121382021
iMem = pParse->nMem+1;
82022
+ iTab = pParse->nTab;
8121482023
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
8121582024
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
8121682025
Table *pTab = (Table*)sqliteHashData(k);
81217
- analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
82026
+ analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
8121882027
}
8121982028
loadAnalysis(pParse, iDb);
8122082029
}
8122182030
8122282031
/*
@@ -81237,11 +82046,11 @@
8123782046
if( pOnlyIdx ){
8123882047
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
8123982048
}else{
8124082049
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
8124182050
}
81242
- analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
82051
+ analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
8124382052
loadAnalysis(pParse, iDb);
8124482053
}
8124582054
8124682055
/*
8124782056
** Generate code for the ANALYZE command. The parser calls this routine
@@ -81319,10 +82128,47 @@
8131982128
typedef struct analysisInfo analysisInfo;
8132082129
struct analysisInfo {
8132182130
sqlite3 *db;
8132282131
const char *zDatabase;
8132382132
};
82133
+
82134
+/*
82135
+** The first argument points to a nul-terminated string containing a
82136
+** list of space separated integers. Read the first nOut of these into
82137
+** the array aOut[].
82138
+*/
82139
+static void decodeIntArray(
82140
+ char *zIntArray,
82141
+ int nOut,
82142
+ tRowcnt *aOut,
82143
+ int *pbUnordered
82144
+){
82145
+ char *z = zIntArray;
82146
+ int c;
82147
+ int i;
82148
+ tRowcnt v;
82149
+
82150
+ assert( pbUnordered==0 || *pbUnordered==0 );
82151
+
82152
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82153
+ if( z==0 ) z = "";
82154
+#else
82155
+ if( NEVER(z==0) ) z = "";
82156
+#endif
82157
+ for(i=0; *z && i<nOut; i++){
82158
+ v = 0;
82159
+ while( (c=z[0])>='0' && c<='9' ){
82160
+ v = v*10 + c - '0';
82161
+ z++;
82162
+ }
82163
+ aOut[i] = v;
82164
+ if( *z==' ' ) z++;
82165
+ }
82166
+ if( pbUnordered && strcmp(z, "unordered")==0 ){
82167
+ *pbUnordered = 1;
82168
+ }
82169
+}
8132482170
8132582171
/*
8132682172
** This callback is invoked once for each index when reading the
8132782173
** sqlite_stat1 table.
8132882174
**
@@ -81335,12 +82181,10 @@
8133582181
*/
8133682182
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
8133782183
analysisInfo *pInfo = (analysisInfo*)pData;
8133882184
Index *pIndex;
8133982185
Table *pTable;
81340
- int i, c, n;
81341
- tRowcnt v;
8134282186
const char *z;
8134382187
8134482188
assert( argc==3 );
8134582189
UNUSED_PARAMETER2(NotUsed, argc);
8134682190
@@ -81354,45 +82198,35 @@
8135482198
if( argv[1] ){
8135582199
pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
8135682200
}else{
8135782201
pIndex = 0;
8135882202
}
81359
- n = pIndex ? pIndex->nColumn : 0;
8136082203
z = argv[2];
81361
- for(i=0; *z && i<=n; i++){
81362
- v = 0;
81363
- while( (c=z[0])>='0' && c<='9' ){
81364
- v = v*10 + c - '0';
81365
- z++;
81366
- }
81367
- if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
81368
- if( v>0 ) pTable->nRowEst = v;
81369
- if( pIndex==0 ) break;
81370
- }
81371
- pIndex->aiRowEst[i] = v;
81372
- if( *z==' ' ) z++;
81373
- if( strcmp(z, "unordered")==0 ){
81374
- pIndex->bUnordered = 1;
81375
- break;
81376
- }
81377
- }
82204
+
82205
+ if( pIndex ){
82206
+ int bUnordered = 0;
82207
+ decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82208
+ if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82209
+ pIndex->bUnordered = bUnordered;
82210
+ }else{
82211
+ decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
82212
+ }
82213
+
8137882214
return 0;
8137982215
}
8138082216
8138182217
/*
8138282218
** If the Index.aSample variable is not NULL, delete the aSample[] array
8138382219
** and its contents.
8138482220
*/
8138582221
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
81386
-#ifdef SQLITE_ENABLE_STAT3
82222
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8138782223
if( pIdx->aSample ){
8138882224
int j;
8138982225
for(j=0; j<pIdx->nSample; j++){
8139082226
IndexSample *p = &pIdx->aSample[j];
81391
- if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
81392
- sqlite3DbFree(db, p->u.z);
81393
- }
82227
+ sqlite3DbFree(db, p->p);
8139482228
}
8139582229
sqlite3DbFree(db, pIdx->aSample);
8139682230
}
8139782231
if( db && db->pnBytesFreed==0 ){
8139882232
pIdx->nSample = 0;
@@ -81399,155 +82233,222 @@
8139982233
pIdx->aSample = 0;
8140082234
}
8140182235
#else
8140282236
UNUSED_PARAMETER(db);
8140382237
UNUSED_PARAMETER(pIdx);
81404
-#endif
82238
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8140582239
}
8140682240
81407
-#ifdef SQLITE_ENABLE_STAT3
82241
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8140882242
/*
81409
-** Load content from the sqlite_stat3 table into the Index.aSample[]
81410
-** arrays of all indices.
82243
+** Populate the pIdx->aAvgEq[] array based on the samples currently
82244
+** stored in pIdx->aSample[].
8141182245
*/
81412
-static int loadStat3(sqlite3 *db, const char *zDb){
82246
+static void initAvgEq(Index *pIdx){
82247
+ if( pIdx ){
82248
+ IndexSample *aSample = pIdx->aSample;
82249
+ IndexSample *pFinal = &aSample[pIdx->nSample-1];
82250
+ int iCol;
82251
+ for(iCol=0; iCol<pIdx->nColumn; iCol++){
82252
+ int i; /* Used to iterate through samples */
82253
+ tRowcnt sumEq = 0; /* Sum of the nEq values */
82254
+ int nSum = 0; /* Number of terms contributing to sumEq */
82255
+ tRowcnt avgEq = 0;
82256
+ tRowcnt nDLt = pFinal->anDLt[iCol];
82257
+
82258
+ /* Set nSum to the number of distinct (iCol+1) field prefixes that
82259
+ ** occur in the stat4 table for this index before pFinal. Set
82260
+ ** sumEq to the sum of the nEq values for column iCol for the same
82261
+ ** set (adding the value only once where there exist dupicate
82262
+ ** prefixes). */
82263
+ for(i=0; i<(pIdx->nSample-1); i++){
82264
+ if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
82265
+ sumEq += aSample[i].anEq[iCol];
82266
+ nSum++;
82267
+ }
82268
+ }
82269
+ if( nDLt>nSum ){
82270
+ avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
82271
+ }
82272
+ if( avgEq==0 ) avgEq = 1;
82273
+ pIdx->aAvgEq[iCol] = avgEq;
82274
+ if( pIdx->nSampleCol==1 ) break;
82275
+ }
82276
+ }
82277
+}
82278
+
82279
+/*
82280
+** Load the content from either the sqlite_stat4 or sqlite_stat3 table
82281
+** into the relevant Index.aSample[] arrays.
82282
+**
82283
+** Arguments zSql1 and zSql2 must point to SQL statements that return
82284
+** data equivalent to the following (statements are different for stat3,
82285
+** see the caller of this function for details):
82286
+**
82287
+** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
82288
+** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
82289
+**
82290
+** where %Q is replaced with the database name before the SQL is executed.
82291
+*/
82292
+static int loadStatTbl(
82293
+ sqlite3 *db, /* Database handle */
82294
+ int bStat3, /* Assume single column records only */
82295
+ const char *zSql1, /* SQL statement 1 (see above) */
82296
+ const char *zSql2, /* SQL statement 2 (see above) */
82297
+ const char *zDb /* Database name (e.g. "main") */
82298
+){
8141382299
int rc; /* Result codes from subroutines */
8141482300
sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
8141582301
char *zSql; /* Text of the SQL statement */
8141682302
Index *pPrevIdx = 0; /* Previous index in the loop */
81417
- int idx = 0; /* slot in pIdx->aSample[] for next sample */
81418
- int eType; /* Datatype of a sample */
8141982303
IndexSample *pSample; /* A slot in pIdx->aSample[] */
8142082304
8142182305
assert( db->lookaside.bEnabled==0 );
81422
- if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
81423
- return SQLITE_OK;
81424
- }
81425
-
81426
- zSql = sqlite3MPrintf(db,
81427
- "SELECT idx,count(*) FROM %Q.sqlite_stat3"
81428
- " GROUP BY idx", zDb);
82306
+ zSql = sqlite3MPrintf(db, zSql1, zDb);
8142982307
if( !zSql ){
8143082308
return SQLITE_NOMEM;
8143182309
}
8143282310
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
8143382311
sqlite3DbFree(db, zSql);
8143482312
if( rc ) return rc;
8143582313
8143682314
while( sqlite3_step(pStmt)==SQLITE_ROW ){
82315
+ int nIdxCol = 1; /* Number of columns in stat4 records */
82316
+ int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
82317
+
8143782318
char *zIndex; /* Index name */
8143882319
Index *pIdx; /* Pointer to the index object */
8143982320
int nSample; /* Number of samples */
82321
+ int nByte; /* Bytes of space required */
82322
+ int i; /* Bytes of space required */
82323
+ tRowcnt *pSpace;
8144082324
8144182325
zIndex = (char *)sqlite3_column_text(pStmt, 0);
8144282326
if( zIndex==0 ) continue;
8144382327
nSample = sqlite3_column_int(pStmt, 1);
8144482328
pIdx = sqlite3FindIndex(db, zIndex, zDb);
81445
- if( pIdx==0 ) continue;
81446
- assert( pIdx->nSample==0 );
81447
- pIdx->nSample = nSample;
81448
- pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
81449
- pIdx->avgEq = pIdx->aiRowEst[1];
82329
+ assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
82330
+ /* Index.nSample is non-zero at this point if data has already been
82331
+ ** loaded from the stat4 table. In this case ignore stat3 data. */
82332
+ if( pIdx==0 || pIdx->nSample ) continue;
82333
+ if( bStat3==0 ){
82334
+ nIdxCol = pIdx->nColumn+1;
82335
+ nAvgCol = pIdx->nColumn;
82336
+ }
82337
+ pIdx->nSampleCol = nIdxCol;
82338
+ nByte = sizeof(IndexSample) * nSample;
82339
+ nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
82340
+ nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
82341
+
82342
+ pIdx->aSample = sqlite3DbMallocZero(db, nByte);
8145082343
if( pIdx->aSample==0 ){
81451
- db->mallocFailed = 1;
8145282344
sqlite3_finalize(pStmt);
8145382345
return SQLITE_NOMEM;
8145482346
}
82347
+ pSpace = (tRowcnt*)&pIdx->aSample[nSample];
82348
+ pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
82349
+ for(i=0; i<nSample; i++){
82350
+ pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
82351
+ pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
82352
+ pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
82353
+ }
82354
+ assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
8145582355
}
8145682356
rc = sqlite3_finalize(pStmt);
8145782357
if( rc ) return rc;
8145882358
81459
- zSql = sqlite3MPrintf(db,
81460
- "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
82359
+ zSql = sqlite3MPrintf(db, zSql2, zDb);
8146182360
if( !zSql ){
8146282361
return SQLITE_NOMEM;
8146382362
}
8146482363
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
8146582364
sqlite3DbFree(db, zSql);
8146682365
if( rc ) return rc;
8146782366
8146882367
while( sqlite3_step(pStmt)==SQLITE_ROW ){
81469
- char *zIndex; /* Index name */
81470
- Index *pIdx; /* Pointer to the index object */
81471
- int i; /* Loop counter */
81472
- tRowcnt sumEq; /* Sum of the nEq values */
82368
+ char *zIndex; /* Index name */
82369
+ Index *pIdx; /* Pointer to the index object */
82370
+ int nCol = 1; /* Number of columns in index */
8147382371
8147482372
zIndex = (char *)sqlite3_column_text(pStmt, 0);
8147582373
if( zIndex==0 ) continue;
8147682374
pIdx = sqlite3FindIndex(db, zIndex, zDb);
8147782375
if( pIdx==0 ) continue;
81478
- if( pIdx==pPrevIdx ){
81479
- idx++;
81480
- }else{
82376
+ /* This next condition is true if data has already been loaded from
82377
+ ** the sqlite_stat4 table. In this case ignore stat3 data. */
82378
+ nCol = pIdx->nSampleCol;
82379
+ if( bStat3 && nCol>1 ) continue;
82380
+ if( pIdx!=pPrevIdx ){
82381
+ initAvgEq(pPrevIdx);
8148182382
pPrevIdx = pIdx;
81482
- idx = 0;
81483
- }
81484
- assert( idx<pIdx->nSample );
81485
- pSample = &pIdx->aSample[idx];
81486
- pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
81487
- pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
81488
- pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
81489
- if( idx==pIdx->nSample-1 ){
81490
- if( pSample->nDLt>0 ){
81491
- for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
81492
- pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
81493
- }
81494
- if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
81495
- }
81496
- eType = sqlite3_column_type(pStmt, 4);
81497
- pSample->eType = (u8)eType;
81498
- switch( eType ){
81499
- case SQLITE_INTEGER: {
81500
- pSample->u.i = sqlite3_column_int64(pStmt, 4);
81501
- break;
81502
- }
81503
- case SQLITE_FLOAT: {
81504
- pSample->u.r = sqlite3_column_double(pStmt, 4);
81505
- break;
81506
- }
81507
- case SQLITE_NULL: {
81508
- break;
81509
- }
81510
- default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
81511
- const char *z = (const char *)(
81512
- (eType==SQLITE_BLOB) ?
81513
- sqlite3_column_blob(pStmt, 4):
81514
- sqlite3_column_text(pStmt, 4)
81515
- );
81516
- int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
81517
- pSample->nByte = n;
81518
- if( n < 1){
81519
- pSample->u.z = 0;
81520
- }else{
81521
- pSample->u.z = sqlite3DbMallocRaw(db, n);
81522
- if( pSample->u.z==0 ){
81523
- db->mallocFailed = 1;
81524
- sqlite3_finalize(pStmt);
81525
- return SQLITE_NOMEM;
81526
- }
81527
- memcpy(pSample->u.z, z, n);
81528
- }
81529
- }
81530
- }
81531
- }
81532
- return sqlite3_finalize(pStmt);
81533
-}
81534
-#endif /* SQLITE_ENABLE_STAT3 */
81535
-
81536
-/*
81537
-** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
82383
+ }
82384
+ pSample = &pIdx->aSample[pIdx->nSample];
82385
+ decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
82386
+ decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
82387
+ decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
82388
+
82389
+ /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
82390
+ ** This is in case the sample record is corrupted. In that case, the
82391
+ ** sqlite3VdbeRecordCompare() may read up to two varints past the
82392
+ ** end of the allocated buffer before it realizes it is dealing with
82393
+ ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
82394
+ ** a buffer overread. */
82395
+ pSample->n = sqlite3_column_bytes(pStmt, 4);
82396
+ pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
82397
+ if( pSample->p==0 ){
82398
+ sqlite3_finalize(pStmt);
82399
+ return SQLITE_NOMEM;
82400
+ }
82401
+ memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
82402
+ pIdx->nSample++;
82403
+ }
82404
+ rc = sqlite3_finalize(pStmt);
82405
+ if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
82406
+ return rc;
82407
+}
82408
+
82409
+/*
82410
+** Load content from the sqlite_stat4 and sqlite_stat3 tables into
82411
+** the Index.aSample[] arrays of all indices.
82412
+*/
82413
+static int loadStat4(sqlite3 *db, const char *zDb){
82414
+ int rc = SQLITE_OK; /* Result codes from subroutines */
82415
+
82416
+ assert( db->lookaside.bEnabled==0 );
82417
+ if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
82418
+ rc = loadStatTbl(db, 0,
82419
+ "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
82420
+ "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
82421
+ zDb
82422
+ );
82423
+ }
82424
+
82425
+ if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
82426
+ rc = loadStatTbl(db, 1,
82427
+ "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
82428
+ "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
82429
+ zDb
82430
+ );
82431
+ }
82432
+
82433
+ return rc;
82434
+}
82435
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82436
+
82437
+/*
82438
+** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
8153882439
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
81539
-** arrays. The contents of sqlite_stat3 are used to populate the
82440
+** arrays. The contents of sqlite_stat3/4 are used to populate the
8154082441
** Index.aSample[] arrays.
8154182442
**
8154282443
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
81543
-** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
81544
-** during compilation and the sqlite_stat3 table is present, no data is
82444
+** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
82445
+** during compilation and the sqlite_stat3/4 table is present, no data is
8154582446
** read from it.
8154682447
**
81547
-** If SQLITE_ENABLE_STAT3 was defined during compilation and the
81548
-** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
82448
+** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
82449
+** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
8154982450
** returned. However, in this case, data is read from the sqlite_stat1
8155082451
** table (if it is present) before returning.
8155182452
**
8155282453
** If an OOM error occurs, this function always sets db->mallocFailed.
8155382454
** This means if the caller does not care about other errors, the return
@@ -81565,11 +82466,11 @@
8156582466
/* Clear any prior statistics */
8156682467
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
8156782468
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
8156882469
Index *pIdx = sqliteHashData(i);
8156982470
sqlite3DefaultRowEst(pIdx);
81570
-#ifdef SQLITE_ENABLE_STAT3
82471
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8157182472
sqlite3DeleteIndexSamples(db, pIdx);
8157282473
pIdx->aSample = 0;
8157382474
#endif
8157482475
}
8157582476
@@ -81589,16 +82490,16 @@
8158982490
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
8159082491
sqlite3DbFree(db, zSql);
8159182492
}
8159282493
8159382494
81594
- /* Load the statistics from the sqlite_stat3 table. */
81595
-#ifdef SQLITE_ENABLE_STAT3
82495
+ /* Load the statistics from the sqlite_stat4 table. */
82496
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8159682497
if( rc==SQLITE_OK ){
8159782498
int lookasideEnabled = db->lookaside.bEnabled;
8159882499
db->lookaside.bEnabled = 0;
81599
- rc = loadStat3(db, sInfo.zDatabase);
82500
+ rc = loadStat4(db, sInfo.zDatabase);
8160082501
db->lookaside.bEnabled = lookasideEnabled;
8160182502
}
8160282503
#endif
8160382504
8160482505
if( rc==SQLITE_NOMEM ){
@@ -81769,10 +82670,13 @@
8176982670
}
8177082671
pPager = sqlite3BtreePager(aNew->pBt);
8177182672
sqlite3PagerLockingMode(pPager, db->dfltLockMode);
8177282673
sqlite3BtreeSecureDelete(aNew->pBt,
8177382674
sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
82675
+#ifndef SQLITE_OMIT_PAGER_PRAGMAS
82676
+ sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
82677
+#endif
8177482678
}
8177582679
aNew->safety_level = 3;
8177682680
aNew->zName = sqlite3DbStrDup(db, zName);
8177782681
if( rc==SQLITE_OK && aNew->zName==0 ){
8177882682
rc = SQLITE_NOMEM;
@@ -84447,11 +85351,11 @@
8444785351
const char *zType, /* "idx" or "tbl" */
8444885352
const char *zName /* Name of index or table */
8444985353
){
8445085354
int i;
8445185355
const char *zDbName = pParse->db->aDb[iDb].zName;
84452
- for(i=1; i<=3; i++){
85356
+ for(i=1; i<=4; i++){
8445385357
char zTab[24];
8445485358
sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
8445585359
if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
8445685360
sqlite3NestedParse(pParse,
8445785361
"DELETE FROM %Q.%s WHERE %s=%Q",
@@ -85289,11 +86193,11 @@
8528986193
8529086194
/* Gather the complete text of the CREATE INDEX statement into
8529186195
** the zStmt variable
8529286196
*/
8529386197
if( pStart ){
85294
- int n = (pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
86198
+ int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
8529586199
if( pName->z[n-1]==';' ) n--;
8529686200
/* A named index with an explicit CREATE INDEX statement */
8529786201
zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
8529886202
onError==OE_None ? "" : " UNIQUE", n, pName->z);
8529986203
}else{
@@ -88926,13 +89830,13 @@
8892689830
}
8892789831
static void groupConcatFinalize(sqlite3_context *context){
8892889832
StrAccum *pAccum;
8892989833
pAccum = sqlite3_aggregate_context(context, 0);
8893089834
if( pAccum ){
88931
- if( pAccum->tooBig ){
89835
+ if( pAccum->accError==STRACCUM_TOOBIG ){
8893289836
sqlite3_result_error_toobig(context);
88933
- }else if( pAccum->mallocFailed ){
89837
+ }else if( pAccum->accError==STRACCUM_NOMEM ){
8893489838
sqlite3_result_error_nomem(context);
8893589839
}else{
8893689840
sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
8893789841
sqlite3_free);
8893889842
}
@@ -89118,10 +90022,13 @@
8911890022
sqlite3FuncDefInsert(pHash, &aFunc[i]);
8911990023
}
8912090024
sqlite3RegisterDateTimeFunctions();
8912190025
#ifndef SQLITE_OMIT_ALTERTABLE
8912290026
sqlite3AlterFunctions();
90027
+#endif
90028
+#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
90029
+ sqlite3AnalyzeFunctions();
8912390030
#endif
8912490031
}
8912590032
8912690033
/************** End of func.c ************************************************/
8912790034
/************** Begin file fkey.c ********************************************/
@@ -93834,10 +94741,40 @@
9383494741
sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
9383594742
sqlite3VdbeSetNumCols(v, 1);
9383694743
sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
9383794744
sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
9383894745
}
94746
+
94747
+
94748
+/*
94749
+** Set the safety_level and pager flags for pager iDb. Or if iDb<0
94750
+** set these values for all pagers.
94751
+*/
94752
+#ifndef SQLITE_OMIT_PAGER_PRAGMAS
94753
+static void setAllPagerFlags(sqlite3 *db){
94754
+ if( db->autoCommit ){
94755
+ Db *pDb = db->aDb;
94756
+ int n = db->nDb;
94757
+ assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
94758
+ assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
94759
+ assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
94760
+ assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
94761
+ == PAGER_FLAGS_MASK );
94762
+ assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
94763
+ while( (n--) > 0 ){
94764
+ if( pDb->pBt ){
94765
+ sqlite3BtreeSetPagerFlags(pDb->pBt,
94766
+ pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
94767
+ }
94768
+ pDb++;
94769
+ }
94770
+ }
94771
+}
94772
+#else
94773
+# define setAllPagerFlags(X) /* no-op */
94774
+#endif
94775
+
9383994776
9384094777
#ifndef SQLITE_OMIT_FLAG_PRAGMAS
9384194778
/*
9384294779
** Check to see if zRight and zLeft refer to a pragma that queries
9384394780
** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
@@ -93853,10 +94790,11 @@
9385394790
{ "count_changes", SQLITE_CountRows },
9385494791
{ "empty_result_callbacks", SQLITE_NullCallback },
9385594792
{ "legacy_file_format", SQLITE_LegacyFileFmt },
9385694793
{ "fullfsync", SQLITE_FullFSync },
9385794794
{ "checkpoint_fullfsync", SQLITE_CkptFullFSync },
94795
+ { "cache_spill", SQLITE_CacheSpill },
9385894796
{ "reverse_unordered_selects", SQLITE_ReverseOrder },
9385994797
{ "query_only", SQLITE_QueryOnly },
9386094798
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
9386194799
{ "automatic_index", SQLITE_AutoIndex },
9386294800
#endif
@@ -94307,11 +95245,11 @@
9430795245
*/
9430895246
if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
9430995247
Pager *pPager = sqlite3BtreePager(pDb->pBt);
9431095248
i64 iLimit = -2;
9431195249
if( zRight ){
94312
- sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
95250
+ sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
9431395251
if( iLimit<-1 ) iLimit = -1;
9431495252
}
9431595253
iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
9431695254
returnSingleInt(pParse, "journal_size_limit", iLimit);
9431795255
}else
@@ -94441,14 +95379,15 @@
9444195379
** as little or as much as it wants. Except, if N is set to 0 then the
9444295380
** upper layers will never invoke the xFetch interfaces to the VFS.
9444395381
*/
9444495382
if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
9444595383
sqlite3_int64 sz;
95384
+#if SQLITE_MAX_MMAP_SIZE>0
9444695385
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9444795386
if( zRight ){
9444895387
int ii;
94449
- sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
95388
+ sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
9445095389
if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
9445195390
if( pId2->n==0 ) db->szMmap = sz;
9445295391
for(ii=db->nDb-1; ii>=0; ii--){
9445395392
if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9445495393
sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94455,12 +95394,13 @@
9445595394
}
9445695395
}
9445795396
}
9445895397
sz = -1;
9445995398
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
94460
-#if SQLITE_MAX_MMAP_SIZE==0
95399
+#else
9446195400
sz = 0;
95401
+ rc = SQLITE_OK;
9446295402
#endif
9446395403
if( rc==SQLITE_OK ){
9446495404
returnSingleInt(pParse, "mmap_size", sz);
9446595405
}else if( rc!=SQLITE_NOTFOUND ){
9446695406
pParse->nErr++;
@@ -94643,19 +95583,19 @@
9464395583
if( !db->autoCommit ){
9464495584
sqlite3ErrorMsg(pParse,
9464595585
"Safety level may not be changed inside a transaction");
9464695586
}else{
9464795587
pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
95588
+ setAllPagerFlags(db);
9464895589
}
9464995590
}
9465095591
}else
9465195592
#endif /* SQLITE_OMIT_PAGER_PRAGMAS */
9465295593
9465395594
#ifndef SQLITE_OMIT_FLAG_PRAGMAS
9465495595
if( flagPragma(pParse, zLeft, zRight) ){
94655
- /* The flagPragma() subroutine also generates any necessary code
94656
- ** there is nothing more to do here */
95596
+ setAllPagerFlags(db);
9465795597
}else
9465895598
#endif /* SQLITE_OMIT_FLAG_PRAGMAS */
9465995599
9466095600
#ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
9466195601
/*
@@ -95482,21 +96422,10 @@
9548296422
#endif
9548396423
9548496424
9548596425
{/* Empty ELSE clause */}
9548696426
95487
- /*
95488
- ** Reset the safety level, in case the fullfsync flag or synchronous
95489
- ** setting changed.
95490
- */
95491
-#ifndef SQLITE_OMIT_PAGER_PRAGMAS
95492
- if( db->autoCommit ){
95493
- sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
95494
- (db->flags&SQLITE_FullFSync)!=0,
95495
- (db->flags&SQLITE_CkptFullFSync)!=0);
95496
- }
95497
-#endif
9549896427
pragma_out:
9549996428
sqlite3DbFree(db, zLeft);
9550096429
sqlite3DbFree(db, zRight);
9550196430
}
9550296431
@@ -102619,11 +103548,11 @@
102619103548
** space.
102620103549
*/
102621103550
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
102622103551
assert( pTab!=0 );
102623103552
if( !pTab->pSelect ){
102624
- sqlite3_value *pValue;
103553
+ sqlite3_value *pValue = 0;
102625103554
u8 enc = ENC(sqlite3VdbeDb(v));
102626103555
Column *pCol = &pTab->aCol[i];
102627103556
VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
102628103557
assert( i<pTab->nCol );
102629103558
sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104395,14 +105324,13 @@
104395105324
/*
104396105325
** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
104397105326
** array. Return the error code for the first error that occurs, or
104398105327
** SQLITE_OK if all xSync operations are successful.
104399105328
**
104400
-** Set *pzErrmsg to point to a buffer that should be released using
104401
-** sqlite3DbFree() containing an error message, if one is available.
105329
+** If an error message is available, leave it in p->zErrMsg.
104402105330
*/
104403
-SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
105331
+SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
104404105332
int i;
104405105333
int rc = SQLITE_OK;
104406105334
VTable **aVTrans = db->aVTrans;
104407105335
104408105336
db->aVTrans = 0;
@@ -104409,13 +105337,11 @@
104409105337
for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
104410105338
int (*x)(sqlite3_vtab *);
104411105339
sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
104412105340
if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
104413105341
rc = x(pVtab);
104414
- sqlite3DbFree(db, *pzErrmsg);
104415
- *pzErrmsg = pVtab->zErrMsg;
104416
- pVtab->zErrMsg = 0;
105342
+ sqlite3VtabImportErrmsg(p, pVtab);
104417105343
}
104418105344
}
104419105345
db->aVTrans = aVTrans;
104420105346
return rc;
104421105347
}
@@ -104776,10 +105702,11 @@
104776105702
int iIdxCur; /* The VDBE cursor used to access pIdx */
104777105703
int addrBrk; /* Jump here to break out of the loop */
104778105704
int addrNxt; /* Jump here to start the next IN combination */
104779105705
int addrCont; /* Jump here to continue with the next loop cycle */
104780105706
int addrFirst; /* First instruction of interior of the loop */
105707
+ int addrBody; /* Beginning of the body of this loop */
104781105708
u8 iFrom; /* Which entry in the FROM clause */
104782105709
u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
104783105710
int p1, p2; /* Operands of the opcode used to ends the loop */
104784105711
union { /* Information that depends on pWLoop->wsFlags */
104785105712
struct {
@@ -104971,11 +105898,11 @@
104971105898
#define TERM_CODED 0x04 /* This term is already coded */
104972105899
#define TERM_COPIED 0x08 /* Has a child */
104973105900
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
104974105901
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
104975105902
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
104976
-#ifdef SQLITE_ENABLE_STAT3
105903
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
104977105904
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
104978105905
#else
104979105906
# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
104980105907
#endif
104981105908
@@ -105077,10 +106004,14 @@
105077106004
WhereInfo *pWInfo; /* Information about this WHERE */
105078106005
WhereClause *pWC; /* WHERE clause terms */
105079106006
ExprList *pOrderBy; /* ORDER BY clause */
105080106007
WhereLoop *pNew; /* Template WhereLoop */
105081106008
WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
106009
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106010
+ UnpackedRecord *pRec; /* Probe for stat4 (if required) */
106011
+ int nRecValid; /* Number of valid fields currently in pRec */
106012
+#endif
105082106013
};
105083106014
105084106015
/*
105085106016
** The WHERE clause processing routine has two halves. The
105086106017
** first part does the start of the WHERE loop and the second
@@ -105890,12 +106821,14 @@
105890106821
/*
105891106822
** If the pBase expression originated in the ON or USING clause of
105892106823
** a join, then transfer the appropriate markings over to derived.
105893106824
*/
105894106825
static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
105895
- pDerived->flags |= pBase->flags & EP_FromJoin;
105896
- pDerived->iRightJoinTable = pBase->iRightJoinTable;
106826
+ if( pDerived ){
106827
+ pDerived->flags |= pBase->flags & EP_FromJoin;
106828
+ pDerived->iRightJoinTable = pBase->iRightJoinTable;
106829
+ }
105897106830
}
105898106831
105899106832
#if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
105900106833
/*
105901106834
** Analyze a term that consists of two or more OR-connected
@@ -106348,10 +107281,11 @@
106348107281
Expr *pNewExpr;
106349107282
int idxNew;
106350107283
pNewExpr = sqlite3PExpr(pParse, ops[i],
106351107284
sqlite3ExprDup(db, pExpr->pLeft, 0),
106352107285
sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
107286
+ transferJoinMarkings(pNewExpr, pExpr);
106353107287
idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
106354107288
testcase( idxNew==0 );
106355107289
exprAnalyze(pSrc, pWC, idxNew);
106356107290
pTerm = &pWC->a[idxTerm];
106357107291
pWC->a[idxNew].iParent = idxTerm;
@@ -106415,17 +107349,19 @@
106415107349
sCollSeqName.n = 6;
106416107350
pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
106417107351
pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
106418107352
sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
106419107353
pStr1, 0);
107354
+ transferJoinMarkings(pNewExpr1, pExpr);
106420107355
idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
106421107356
testcase( idxNew1==0 );
106422107357
exprAnalyze(pSrc, pWC, idxNew1);
106423107358
pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
106424107359
pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
106425107360
sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
106426107361
pStr2, 0);
107362
+ transferJoinMarkings(pNewExpr2, pExpr);
106427107363
idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
106428107364
testcase( idxNew2==0 );
106429107365
exprAnalyze(pSrc, pWC, idxNew2);
106430107366
pTerm = &pWC->a[idxTerm];
106431107367
if( isComplete ){
@@ -106471,11 +107407,11 @@
106471107407
pNewTerm->prereqAll = pTerm->prereqAll;
106472107408
}
106473107409
}
106474107410
#endif /* SQLITE_OMIT_VIRTUALTABLE */
106475107411
106476
-#ifdef SQLITE_ENABLE_STAT3
107412
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106477107413
/* When sqlite_stat3 histogram data is available an operator of the
106478107414
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
106479107415
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
106480107416
** virtual term of that form.
106481107417
**
@@ -106511,11 +107447,11 @@
106511107447
pTerm->nChild = 1;
106512107448
pTerm->wtFlags |= TERM_COPIED;
106513107449
pNewTerm->prereqAll = pTerm->prereqAll;
106514107450
}
106515107451
}
106516
-#endif /* SQLITE_ENABLE_STAT */
107452
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
106517107453
106518107454
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
106519107455
** an index for tables to the left of the join.
106520107456
*/
106521107457
pTerm->prereqRight |= extraRight;
@@ -107079,155 +108015,84 @@
107079108015
return pParse->nErr;
107080108016
}
107081108017
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
107082108018
107083108019
107084
-#ifdef SQLITE_ENABLE_STAT3
108020
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107085108021
/*
107086108022
** Estimate the location of a particular key among all keys in an
107087108023
** index. Store the results in aStat as follows:
107088108024
**
107089108025
** aStat[0] Est. number of rows less than pVal
107090108026
** aStat[1] Est. number of rows equal to pVal
107091108027
**
107092108028
** Return SQLITE_OK on success.
107093108029
*/
107094
-static int whereKeyStats(
108030
+static void whereKeyStats(
107095108031
Parse *pParse, /* Database connection */
107096108032
Index *pIdx, /* Index to consider domain of */
107097
- sqlite3_value *pVal, /* Value to consider */
108033
+ UnpackedRecord *pRec, /* Vector of values to consider */
107098108034
int roundUp, /* Round up if true. Round down if false */
107099108035
tRowcnt *aStat /* OUT: stats written here */
107100108036
){
107101
- tRowcnt n;
107102
- IndexSample *aSample;
107103
- int i, eType;
107104
- int isEq = 0;
107105
- i64 v;
107106
- double r, rS;
107107
-
107108
- assert( roundUp==0 || roundUp==1 );
108037
+ IndexSample *aSample = pIdx->aSample;
108038
+ int iCol = pRec->nField-1; /* Index of required stats in anEq[] etc. */
108039
+ int iMin = 0; /* Smallest sample not yet tested */
108040
+ int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
108041
+ int iTest; /* Next sample to test */
108042
+ int res; /* Result of comparison operation */
108043
+
107109108044
assert( pIdx->nSample>0 );
107110
- if( pVal==0 ) return SQLITE_ERROR;
107111
- n = pIdx->aiRowEst[0];
107112
- aSample = pIdx->aSample;
107113
- eType = sqlite3_value_type(pVal);
107114
-
107115
- if( eType==SQLITE_INTEGER ){
107116
- v = sqlite3_value_int64(pVal);
107117
- r = (i64)v;
107118
- for(i=0; i<pIdx->nSample; i++){
107119
- if( aSample[i].eType==SQLITE_NULL ) continue;
107120
- if( aSample[i].eType>=SQLITE_TEXT ) break;
107121
- if( aSample[i].eType==SQLITE_INTEGER ){
107122
- if( aSample[i].u.i>=v ){
107123
- isEq = aSample[i].u.i==v;
107124
- break;
107125
- }
107126
- }else{
107127
- assert( aSample[i].eType==SQLITE_FLOAT );
107128
- if( aSample[i].u.r>=r ){
107129
- isEq = aSample[i].u.r==r;
107130
- break;
107131
- }
107132
- }
107133
- }
107134
- }else if( eType==SQLITE_FLOAT ){
107135
- r = sqlite3_value_double(pVal);
107136
- for(i=0; i<pIdx->nSample; i++){
107137
- if( aSample[i].eType==SQLITE_NULL ) continue;
107138
- if( aSample[i].eType>=SQLITE_TEXT ) break;
107139
- if( aSample[i].eType==SQLITE_FLOAT ){
107140
- rS = aSample[i].u.r;
107141
- }else{
107142
- rS = aSample[i].u.i;
107143
- }
107144
- if( rS>=r ){
107145
- isEq = rS==r;
107146
- break;
107147
- }
107148
- }
107149
- }else if( eType==SQLITE_NULL ){
107150
- i = 0;
107151
- if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
107152
- }else{
107153
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
107154
- for(i=0; i<pIdx->nSample; i++){
107155
- if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
107156
- break;
107157
- }
107158
- }
107159
- if( i<pIdx->nSample ){
107160
- sqlite3 *db = pParse->db;
107161
- CollSeq *pColl;
107162
- const u8 *z;
107163
- if( eType==SQLITE_BLOB ){
107164
- z = (const u8 *)sqlite3_value_blob(pVal);
107165
- pColl = db->pDfltColl;
107166
- assert( pColl->enc==SQLITE_UTF8 );
107167
- }else{
107168
- pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
107169
- /* If the collating sequence was unavailable, we should have failed
107170
- ** long ago and never reached this point. But we'll check just to
107171
- ** be doubly sure. */
107172
- if( NEVER(pColl==0) ) return SQLITE_ERROR;
107173
- z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
107174
- if( !z ){
107175
- return SQLITE_NOMEM;
107176
- }
107177
- assert( z && pColl && pColl->xCmp );
107178
- }
107179
- n = sqlite3ValueBytes(pVal, pColl->enc);
107180
-
107181
- for(; i<pIdx->nSample; i++){
107182
- int c;
107183
- int eSampletype = aSample[i].eType;
107184
- if( eSampletype<eType ) continue;
107185
- if( eSampletype!=eType ) break;
107186
-#ifndef SQLITE_OMIT_UTF16
107187
- if( pColl->enc!=SQLITE_UTF8 ){
107188
- int nSample;
107189
- char *zSample = sqlite3Utf8to16(
107190
- db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
107191
- );
107192
- if( !zSample ){
107193
- assert( db->mallocFailed );
107194
- return SQLITE_NOMEM;
107195
- }
107196
- c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
107197
- sqlite3DbFree(db, zSample);
107198
- }else
107199
-#endif
107200
- {
107201
- c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
107202
- }
107203
- if( c>=0 ){
107204
- if( c==0 ) isEq = 1;
107205
- break;
107206
- }
107207
- }
107208
- }
107209
- }
108045
+ assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
108046
+ do{
108047
+ iTest = (iMin+i)/2;
108048
+ res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
108049
+ if( res<0 ){
108050
+ iMin = iTest+1;
108051
+ }else{
108052
+ i = iTest;
108053
+ }
108054
+ }while( res && iMin<i );
108055
+
108056
+#ifdef SQLITE_DEBUG
108057
+ /* The following assert statements check that the binary search code
108058
+ ** above found the right answer. This block serves no purpose other
108059
+ ** than to invoke the asserts. */
108060
+ if( res==0 ){
108061
+ /* If (res==0) is true, then sample $i must be equal to pRec */
108062
+ assert( i<pIdx->nSample );
108063
+ assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
108064
+ || pParse->db->mallocFailed );
108065
+ }else{
108066
+ /* Otherwise, pRec must be smaller than sample $i and larger than
108067
+ ** sample ($i-1). */
108068
+ assert( i==pIdx->nSample
108069
+ || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
108070
+ || pParse->db->mallocFailed );
108071
+ assert( i==0
108072
+ || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
108073
+ || pParse->db->mallocFailed );
108074
+ }
108075
+#endif /* ifdef SQLITE_DEBUG */
107210108076
107211108077
/* At this point, aSample[i] is the first sample that is greater than
107212108078
** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
107213
- ** than pVal. If aSample[i]==pVal, then isEq==1.
108079
+ ** than pVal. If aSample[i]==pVal, then res==0.
107214108080
*/
107215
- if( isEq ){
107216
- assert( i<pIdx->nSample );
107217
- aStat[0] = aSample[i].nLt;
107218
- aStat[1] = aSample[i].nEq;
108081
+ if( res==0 ){
108082
+ aStat[0] = aSample[i].anLt[iCol];
108083
+ aStat[1] = aSample[i].anEq[iCol];
107219108084
}else{
107220108085
tRowcnt iLower, iUpper, iGap;
107221108086
if( i==0 ){
107222108087
iLower = 0;
107223
- iUpper = aSample[0].nLt;
108088
+ iUpper = aSample[0].anLt[iCol];
107224108089
}else{
107225
- iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
107226
- iLower = aSample[i-1].nEq + aSample[i-1].nLt;
108090
+ iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
108091
+ iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
107227108092
}
107228
- aStat[1] = pIdx->avgEq;
108093
+ aStat[1] = (pIdx->nColumn>iCol ? pIdx->aAvgEq[iCol] : 1);
107229108094
if( iLower>=iUpper ){
107230108095
iGap = 0;
107231108096
}else{
107232108097
iGap = iUpper - iLower;
107233108098
}
@@ -107236,48 +108101,12 @@
107236108101
}else{
107237108102
iGap = iGap/3;
107238108103
}
107239108104
aStat[0] = iLower + iGap;
107240108105
}
107241
- return SQLITE_OK;
107242
-}
107243
-#endif /* SQLITE_ENABLE_STAT3 */
107244
-
107245
-/*
107246
-** If expression pExpr represents a literal value, set *pp to point to
107247
-** an sqlite3_value structure containing the same value, with affinity
107248
-** aff applied to it, before returning. It is the responsibility of the
107249
-** caller to eventually release this structure by passing it to
107250
-** sqlite3ValueFree().
107251
-**
107252
-** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
107253
-** is an SQL variable that currently has a non-NULL value bound to it,
107254
-** create an sqlite3_value structure containing this value, again with
107255
-** affinity aff applied to it, instead.
107256
-**
107257
-** If neither of the above apply, set *pp to NULL.
107258
-**
107259
-** If an error occurs, return an error code. Otherwise, SQLITE_OK.
107260
-*/
107261
-#ifdef SQLITE_ENABLE_STAT3
107262
-static int valueFromExpr(
107263
- Parse *pParse,
107264
- Expr *pExpr,
107265
- u8 aff,
107266
- sqlite3_value **pp
107267
-){
107268
- if( pExpr->op==TK_VARIABLE
107269
- || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
107270
- ){
107271
- int iVar = pExpr->iColumn;
107272
- sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
107273
- *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff);
107274
- return SQLITE_OK;
107275
- }
107276
- return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
107277
-}
107278
-#endif
108106
+}
108107
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107279108108
107280108109
/*
107281108110
** This function is used to estimate the number of rows that will be visited
107282108111
** by scanning an index for a range of values. The range may have an upper
107283108112
** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107290,107 +108119,154 @@
107290108119
** pLower pUpper
107291108120
**
107292108121
** If either of the upper or lower bound is not present, then NULL is passed in
107293108122
** place of the corresponding WhereTerm.
107294108123
**
107295
-** The nEq parameter is passed the index of the index column subject to the
107296
-** range constraint. Or, equivalently, the number of equality constraints
107297
-** optimized by the proposed index scan. For example, assuming index p is
107298
-** on t1(a, b), and the SQL query is:
108124
+** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
108125
+** column subject to the range constraint. Or, equivalently, the number of
108126
+** equality constraints optimized by the proposed index scan. For example,
108127
+** assuming index p is on t1(a, b), and the SQL query is:
107299108128
**
107300108129
** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
107301108130
**
107302
-** then nEq should be passed the value 1 (as the range restricted column,
107303
-** b, is the second left-most column of the index). Or, if the query is:
108131
+** then nEq is set to 1 (as the range restricted column, b, is the second
108132
+** left-most column of the index). Or, if the query is:
107304108133
**
107305108134
** ... FROM t1 WHERE a > ? AND a < ? ...
107306108135
**
107307
-** then nEq should be passed 0.
107308
-**
107309
-** The returned value is an integer divisor to reduce the estimated
107310
-** search space. A return value of 1 means that range constraints are
107311
-** no help at all. A return value of 2 means range constraints are
107312
-** expected to reduce the search space by half. And so forth...
107313
-**
107314
-** In the absence of sqlite_stat3 ANALYZE data, each range inequality
107315
-** reduces the search space by a factor of 4. Hence a single constraint (x>?)
107316
-** results in a return of 4 and a range constraint (x>? AND x<?) results
107317
-** in a return of 16.
108136
+** then nEq is set to 0.
108137
+**
108138
+** When this function is called, *pnOut is set to the whereCost() of the
108139
+** number of rows that the index scan is expected to visit without
108140
+** considering the range constraints. If nEq is 0, this is the number of
108141
+** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108142
+** to account for the range contraints pLower and pUpper.
108143
+**
108144
+** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
108145
+** used, each range inequality reduces the search space by a factor of 4.
108146
+** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
108147
+** rows visited by a factor of 16.
107318108148
*/
107319108149
static int whereRangeScanEst(
107320108150
Parse *pParse, /* Parsing & code generating context */
107321
- Index *p, /* The index containing the range-compared column; "x" */
107322
- int nEq, /* index into p->aCol[] of the range-compared column */
108151
+ WhereLoopBuilder *pBuilder,
107323108152
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
107324108153
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
107325
- WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
108154
+ WhereCost *pnOut /* IN/OUT: Number of rows visited */
107326108155
){
107327108156
int rc = SQLITE_OK;
108157
+ int nOut = (int)*pnOut;
107328108158
107329
-#ifdef SQLITE_ENABLE_STAT3
108159
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108160
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108161
+ int nEq = pBuilder->pNew->u.btree.nEq;
107330108162
107331
- if( nEq==0 && p->nSample && OptimizationEnabled(pParse->db, SQLITE_Stat3) ){
107332
- sqlite3_value *pRangeVal;
107333
- tRowcnt iLower = 0;
107334
- tRowcnt iUpper = p->aiRowEst[0];
108163
+ if( nEq==pBuilder->nRecValid
108164
+ && nEq<p->nSampleCol
108165
+ && p->nSample
108166
+ && OptimizationEnabled(pParse->db, SQLITE_Stat3)
108167
+ ){
108168
+ UnpackedRecord *pRec = pBuilder->pRec;
107335108169
tRowcnt a[2];
107336108170
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107337108171
108172
+ /* Variable iLower will be set to the estimate of the number of rows in
108173
+ ** the index that are less than the lower bound of the range query. The
108174
+ ** lower bound being the concatenation of $P and $L, where $P is the
108175
+ ** key-prefix formed by the nEq values matched against the nEq left-most
108176
+ ** columns of the index, and $L is the value in pLower.
108177
+ **
108178
+ ** Or, if pLower is NULL or $L cannot be extracted from it (because it
108179
+ ** is not a simple variable or literal value), the lower bound of the
108180
+ ** range is $P. Due to a quirk in the way whereKeyStats() works, even
108181
+ ** if $L is available, whereKeyStats() is called for both ($P) and
108182
+ ** ($P:$L) and the larger of the two returned values used.
108183
+ **
108184
+ ** Similarly, iUpper is to be set to the estimate of the number of rows
108185
+ ** less than the upper bound of the range query. Where the upper bound
108186
+ ** is either ($P) or ($P:$U). Again, even if $U is available, both values
108187
+ ** of iUpper are requested of whereKeyStats() and the smaller used.
108188
+ */
108189
+ tRowcnt iLower;
108190
+ tRowcnt iUpper;
108191
+
108192
+ /* Determine iLower and iUpper using ($P) only. */
108193
+ if( nEq==0 ){
108194
+ iLower = 0;
108195
+ iUpper = p->aiRowEst[0];
108196
+ }else{
108197
+ /* Note: this call could be optimized away - since the same values must
108198
+ ** have been requested when testing key $P in whereEqualScanEst(). */
108199
+ whereKeyStats(pParse, p, pRec, 0, a);
108200
+ iLower = a[0];
108201
+ iUpper = a[0] + a[1];
108202
+ }
108203
+
108204
+ /* If possible, improve on the iLower estimate using ($P:$L). */
107338108205
if( pLower ){
108206
+ int bOk; /* True if value is extracted from pExpr */
107339108207
Expr *pExpr = pLower->pExpr->pRight;
107340
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
107341108208
assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
107342
- if( rc==SQLITE_OK
107343
- && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
107344
- ){
107345
- iLower = a[0];
107346
- if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1];
107347
- }
107348
- sqlite3ValueFree(pRangeVal);
107349
- }
107350
- if( rc==SQLITE_OK && pUpper ){
107351
- Expr *pExpr = pUpper->pExpr->pRight;
107352
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
108209
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108210
+ if( rc==SQLITE_OK && bOk ){
108211
+ tRowcnt iNew;
108212
+ whereKeyStats(pParse, p, pRec, 0, a);
108213
+ iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
108214
+ if( iNew>iLower ) iLower = iNew;
108215
+ }
108216
+ }
108217
+
108218
+ /* If possible, improve on the iUpper estimate using ($P:$U). */
108219
+ if( pUpper ){
108220
+ int bOk; /* True if value is extracted from pExpr */
108221
+ Expr *pExpr = pUpper->pExpr->pRight;
107353108222
assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
107354
- if( rc==SQLITE_OK
107355
- && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
107356
- ){
107357
- iUpper = a[0];
107358
- if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
107359
- }
107360
- sqlite3ValueFree(pRangeVal);
107361
- }
108223
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108224
+ if( rc==SQLITE_OK && bOk ){
108225
+ tRowcnt iNew;
108226
+ whereKeyStats(pParse, p, pRec, 1, a);
108227
+ iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
108228
+ if( iNew<iUpper ) iUpper = iNew;
108229
+ }
108230
+ }
108231
+
108232
+ pBuilder->pRec = pRec;
107362108233
if( rc==SQLITE_OK ){
107363
- WhereCost iBase = whereCost(p->aiRowEst[0]);
108234
+ WhereCost nNew;
107364108235
if( iUpper>iLower ){
107365
- iBase -= whereCost(iUpper - iLower);
108236
+ nNew = whereCost(iUpper - iLower);
108237
+ }else{
108238
+ nNew = 10; assert( 10==whereCost(2) );
107366108239
}
107367
- *pRangeDiv = iBase;
107368
- WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n",
107369
- (u32)iLower, (u32)iUpper, *pRangeDiv));
108240
+ if( nNew<nOut ){
108241
+ nOut = nNew;
108242
+ }
108243
+ *pnOut = (WhereCost)nOut;
108244
+ WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
108245
+ (u32)iLower, (u32)iUpper, nOut));
107370108246
return SQLITE_OK;
107371108247
}
107372108248
}
107373108249
#else
107374108250
UNUSED_PARAMETER(pParse);
107375
- UNUSED_PARAMETER(p);
107376
- UNUSED_PARAMETER(nEq);
108251
+ UNUSED_PARAMETER(pBuilder);
107377108252
#endif
107378108253
assert( pLower || pUpper );
107379
- *pRangeDiv = 0;
107380108254
/* TUNING: Each inequality constraint reduces the search space 4-fold.
107381108255
** A BETWEEN operator, therefore, reduces the search space 16-fold */
107382108256
if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
107383
- *pRangeDiv += 20; assert( 20==whereCost(4) );
108257
+ nOut -= 20; assert( 20==whereCost(4) );
107384108258
}
107385108259
if( pUpper ){
107386
- *pRangeDiv += 20; assert( 20==whereCost(4) );
108260
+ nOut -= 20; assert( 20==whereCost(4) );
107387108261
}
108262
+ if( nOut<10 ) nOut = 10;
108263
+ *pnOut = (WhereCost)nOut;
107388108264
return rc;
107389108265
}
107390108266
107391
-#ifdef SQLITE_ENABLE_STAT3
108267
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107392108268
/*
107393108269
** Estimate the number of rows that will be returned based on
107394108270
** an equality constraint x=VALUE and where that VALUE occurs in
107395108271
** the histogram data. This only works when x is the left-most
107396108272
** column of an index and sqlite_stat3 histogram data is available
@@ -107406,41 +108282,57 @@
107406108282
** for a UTF conversion required for comparison. The error is stored
107407108283
** in the pParse structure.
107408108284
*/
107409108285
static int whereEqualScanEst(
107410108286
Parse *pParse, /* Parsing & code generating context */
107411
- Index *p, /* The index whose left-most column is pTerm */
108287
+ WhereLoopBuilder *pBuilder,
107412108288
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
107413108289
tRowcnt *pnRow /* Write the revised row estimate here */
107414108290
){
107415
- sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
108291
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108292
+ int nEq = pBuilder->pNew->u.btree.nEq;
108293
+ UnpackedRecord *pRec = pBuilder->pRec;
107416108294
u8 aff; /* Column affinity */
107417108295
int rc; /* Subfunction return code */
107418108296
tRowcnt a[2]; /* Statistics */
108297
+ int bOk;
107419108298
108299
+ assert( nEq>=1 );
108300
+ assert( nEq<=(p->nColumn+1) );
107420108301
assert( p->aSample!=0 );
107421108302
assert( p->nSample>0 );
107422
- aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107423
- if( pExpr ){
107424
- rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
107425
- if( rc ) goto whereEqualScanEst_cancel;
107426
- }else{
107427
- pRhs = sqlite3ValueNew(pParse->db);
107428
- }
107429
- if( pRhs==0 ) return SQLITE_NOTFOUND;
107430
- rc = whereKeyStats(pParse, p, pRhs, 0, a);
107431
- if( rc==SQLITE_OK ){
107432
- WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
107433
- *pnRow = a[1];
107434
- }
107435
-whereEqualScanEst_cancel:
107436
- sqlite3ValueFree(pRhs);
108303
+ assert( pBuilder->nRecValid<nEq );
108304
+
108305
+ /* If values are not available for all fields of the index to the left
108306
+ ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
108307
+ if( pBuilder->nRecValid<(nEq-1) ){
108308
+ return SQLITE_NOTFOUND;
108309
+ }
108310
+
108311
+ /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
108312
+ ** below would return the same value. */
108313
+ if( nEq>p->nColumn ){
108314
+ *pnRow = 1;
108315
+ return SQLITE_OK;
108316
+ }
108317
+
108318
+ aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
108319
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
108320
+ pBuilder->pRec = pRec;
108321
+ if( rc!=SQLITE_OK ) return rc;
108322
+ if( bOk==0 ) return SQLITE_NOTFOUND;
108323
+ pBuilder->nRecValid = nEq;
108324
+
108325
+ whereKeyStats(pParse, p, pRec, 0, a);
108326
+ WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
108327
+ *pnRow = a[1];
108328
+
107437108329
return rc;
107438108330
}
107439
-#endif /* defined(SQLITE_ENABLE_STAT3) */
108331
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107440108332
107441
-#ifdef SQLITE_ENABLE_STAT3
108333
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107442108334
/*
107443108335
** Estimate the number of rows that will be returned based on
107444108336
** an IN constraint where the right-hand side of the IN operator
107445108337
** is a list of values. Example:
107446108338
**
@@ -107455,33 +108347,38 @@
107455108347
** for a UTF conversion required for comparison. The error is stored
107456108348
** in the pParse structure.
107457108349
*/
107458108350
static int whereInScanEst(
107459108351
Parse *pParse, /* Parsing & code generating context */
107460
- Index *p, /* The index whose left-most column is pTerm */
108352
+ WhereLoopBuilder *pBuilder,
107461108353
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
107462108354
tRowcnt *pnRow /* Write the revised row estimate here */
107463108355
){
108356
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108357
+ int nRecValid = pBuilder->nRecValid;
107464108358
int rc = SQLITE_OK; /* Subfunction return code */
107465108359
tRowcnt nEst; /* Number of rows for a single term */
107466108360
tRowcnt nRowEst = 0; /* New estimate of the number of rows */
107467108361
int i; /* Loop counter */
107468108362
107469108363
assert( p->aSample!=0 );
107470108364
for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
107471108365
nEst = p->aiRowEst[0];
107472
- rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
108366
+ rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
107473108367
nRowEst += nEst;
108368
+ pBuilder->nRecValid = nRecValid;
107474108369
}
108370
+
107475108371
if( rc==SQLITE_OK ){
107476108372
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
107477108373
*pnRow = nRowEst;
107478108374
WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
107479108375
}
108376
+ assert( pBuilder->nRecValid==nRecValid );
107480108377
return rc;
107481108378
}
107482
-#endif /* defined(SQLITE_ENABLE_STAT3) */
108379
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107483108380
107484108381
/*
107485108382
** Disable a term in the WHERE clause. Except, do not disable the term
107486108383
** if it controls a LEFT OUTER JOIN and it did not originate in the ON
107487108384
** or USING clause of that join.
@@ -107715,11 +108612,11 @@
107715108612
pParse->db->mallocFailed = 1;
107716108613
}
107717108614
107718108615
/* Evaluate the equality constraints
107719108616
*/
107720
- assert( pIdx->nColumn>=nEq );
108617
+ assert( zAff==0 || (int)strlen(zAff)>=nEq );
107721108618
for(j=0; j<nEq; j++){
107722108619
int r1;
107723108620
pTerm = pLoop->aLTerm[j];
107724108621
assert( pTerm!=0 );
107725108622
/* The following true for indices with redundant columns.
@@ -107807,11 +108704,12 @@
107807108704
}
107808108705
sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
107809108706
txt.db = db;
107810108707
sqlite3StrAccumAppend(&txt, " (", 2);
107811108708
for(i=0; i<nEq; i++){
107812
- explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
108709
+ char *z = (i==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[i]].zName;
108710
+ explainAppendTerm(&txt, i, z, "=");
107813108711
}
107814108712
107815108713
j = i;
107816108714
if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
107817108715
char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
@@ -109021,16 +109919,22 @@
109021109919
saved_nOut = pNew->nOut;
109022109920
pNew->rSetup = 0;
109023109921
rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
109024109922
for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
109025109923
int nIn = 0;
109924
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109925
+ int nRecValid = pBuilder->nRecValid;
109926
+#endif
109927
+ if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
109928
+ && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
109929
+ ){
109930
+ continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
109931
+ }
109026109932
if( pTerm->prereqRight & pNew->maskSelf ) continue;
109027
-#ifdef SQLITE_ENABLE_STAT3
109028
- if( (pTerm->wtFlags & TERM_VNULL)!=0 && pSrc->pTab->aCol[iCol].notNull ){
109029
- continue; /* skip IS NOT NULL constraints on a NOT NULL column */
109030
- }
109031
-#endif
109933
+
109934
+ assert( pNew->nOut==saved_nOut );
109935
+
109032109936
pNew->wsFlags = saved_wsFlags;
109033109937
pNew->u.btree.nEq = saved_nEq;
109034109938
pNew->nLTerm = saved_nLTerm;
109035109939
if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
109036109940
pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109083,29 +109987,34 @@
109083109987
pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
109084109988
pNew->aLTerm[pNew->nLTerm-2] : 0;
109085109989
}
109086109990
if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
109087109991
/* Adjust nOut and rRun for STAT3 range values */
109088
- WhereCost rDiv;
109089
- whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
109090
- pBtm, pTop, &rDiv);
109091
- pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
109092
- }
109093
-#ifdef SQLITE_ENABLE_STAT3
109094
- if( pNew->u.btree.nEq==1 && pProbe->nSample
109095
- && OptimizationEnabled(db, SQLITE_Stat3) ){
109992
+ assert( pNew->nOut==saved_nOut );
109993
+ whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
109994
+ }
109995
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109996
+ if( nInMul==0
109997
+ && pProbe->nSample
109998
+ && pNew->u.btree.nEq<=pProbe->nSampleCol
109999
+ && OptimizationEnabled(db, SQLITE_Stat3)
110000
+ ){
110001
+ Expr *pExpr = pTerm->pExpr;
109096110002
tRowcnt nOut = 0;
109097110003
if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
109098110004
testcase( pTerm->eOperator & WO_EQ );
109099110005
testcase( pTerm->eOperator & WO_ISNULL );
109100
- rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
110006
+ rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
109101110007
}else if( (pTerm->eOperator & WO_IN)
109102
- && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){
109103
- rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
110008
+ && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110009
+ rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
109104110010
}
109105110011
assert( nOut==0 || rc==SQLITE_OK );
109106
- if( nOut ) pNew->nOut = whereCost(nOut);
110012
+ if( nOut ){
110013
+ nOut = whereCost(nOut);
110014
+ pNew->nOut = MIN(nOut, saved_nOut);
110015
+ }
109107110016
}
109108110017
#endif
109109110018
if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
109110110019
/* Each row involves a step of the index, then a binary search of
109111110020
** the main table */
@@ -109118,10 +110027,14 @@
109118110027
if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
109119110028
&& pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
109120110029
){
109121110030
whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
109122110031
}
110032
+ pNew->nOut = saved_nOut;
110033
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110034
+ pBuilder->nRecValid = nRecValid;
110035
+#endif
109123110036
}
109124110037
pNew->prereq = saved_prereq;
109125110038
pNew->u.btree.nEq = saved_nEq;
109126110039
pNew->wsFlags = saved_wsFlags;
109127110040
pNew->nOut = saved_nOut;
@@ -109166,10 +110079,11 @@
109166110079
static Bitmask columnsInIndex(Index *pIdx){
109167110080
Bitmask m = 0;
109168110081
int j;
109169110082
for(j=pIdx->nColumn-1; j>=0; j--){
109170110083
int x = pIdx->aiColumn[j];
110084
+ assert( x>=0 );
109171110085
testcase( x==BMS-1 );
109172110086
testcase( x==BMS-2 );
109173110087
if( x<BMS-1 ) m |= MASKBIT(x);
109174110088
}
109175110089
return m;
@@ -109244,10 +110158,11 @@
109244110158
pProbe = &sPk;
109245110159
}
109246110160
rSize = whereCost(pSrc->pTab->nRowEst);
109247110161
rLogSize = estLog(rSize);
109248110162
110163
+#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
109249110164
/* Automatic indexes */
109250110165
if( !pBuilder->pOrSet
109251110166
&& (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
109252110167
&& pSrc->pIndex==0
109253110168
&& !pSrc->viaCoroutine
@@ -109278,10 +110193,11 @@
109278110193
pNew->prereq = mExtra | pTerm->prereqRight;
109279110194
rc = whereLoopInsert(pBuilder, pNew);
109280110195
}
109281110196
}
109282110197
}
110198
+#endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
109283110199
109284110200
/* Loop over all indices
109285110201
*/
109286110202
for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
109287110203
if( pProbe->pPartIdxWhere!=0
@@ -109344,11 +110260,17 @@
109344110260
}
109345110261
rc = whereLoopInsert(pBuilder, pNew);
109346110262
if( rc ) break;
109347110263
}
109348110264
}
110265
+
109349110266
rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
110267
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110268
+ sqlite3Stat4ProbeFree(pBuilder->pRec);
110269
+ pBuilder->nRecValid = 0;
110270
+ pBuilder->pRec = 0;
110271
+#endif
109350110272
109351110273
/* If there was an INDEXED BY clause, then only that one index is
109352110274
** considered. */
109353110275
if( pSrc->pIndex ) break;
109354110276
}
@@ -109541,10 +110463,11 @@
109541110463
109542110464
pWC = pBuilder->pWC;
109543110465
if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
109544110466
pWCEnd = pWC->a + pWC->nTerm;
109545110467
pNew = pBuilder->pNew;
110468
+ memset(&sSum, 0, sizeof(sSum));
109546110469
109547110470
for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
109548110471
if( (pTerm->eOperator & WO_OR)!=0
109549110472
&& (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
109550110473
){
@@ -110226,15 +111149,19 @@
110226111149
pLoop->u.btree.nEq = 1;
110227111150
/* TUNING: Cost of a rowid lookup is 10 */
110228111151
pLoop->rRun = 33; /* 33==whereCost(10) */
110229111152
}else{
110230111153
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110231
- if( pIdx->onError==OE_None || pIdx->pPartIdxWhere!=0 ) continue;
111154
+ assert( pLoop->aLTermSpace==pLoop->aLTerm );
111155
+ assert( ArraySize(pLoop->aLTermSpace)==4 );
111156
+ if( pIdx->onError==OE_None
111157
+ || pIdx->pPartIdxWhere!=0
111158
+ || pIdx->nColumn>ArraySize(pLoop->aLTermSpace)
111159
+ ) continue;
110232111160
for(j=0; j<pIdx->nColumn; j++){
110233111161
pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
110234111162
if( pTerm==0 ) break;
110235
- whereLoopResize(pWInfo->pParse->db, pLoop, j);
110236111163
pLoop->aLTerm[j] = pTerm;
110237111164
}
110238111165
if( j!=pIdx->nColumn ) continue;
110239111166
pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
110240111167
if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -110667,15 +111594,10 @@
110667111594
assert( n<=pTab->nCol );
110668111595
}
110669111596
}else{
110670111597
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
110671111598
}
110672
-#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110673
- if( (pLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
110674
- constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel);
110675
- }else
110676
-#endif
110677111599
if( pLoop->wsFlags & WHERE_INDEXED ){
110678111600
Index *pIx = pLoop->u.btree.pIndex;
110679111601
KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
110680111602
/* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
110681111603
int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
@@ -110696,11 +111618,19 @@
110696111618
** program.
110697111619
*/
110698111620
notReady = ~(Bitmask)0;
110699111621
for(ii=0; ii<nTabList; ii++){
110700111622
pLevel = &pWInfo->a[ii];
111623
+#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
111624
+ if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
111625
+ constructAutomaticIndex(pParse, &pWInfo->sWC,
111626
+ &pTabList->a[pLevel->iFrom], notReady, pLevel);
111627
+ if( db->mallocFailed ) goto whereBeginError;
111628
+ }
111629
+#endif
110701111630
explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
111631
+ pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
110702111632
notReady = codeOneLoopStart(pWInfo, ii, notReady);
110703111633
pWInfo->iContinue = pLevel->addrCont;
110704111634
}
110705111635
110706111636
/* Done. */
@@ -110816,13 +111746,14 @@
110816111746
}
110817111747
if( pIdx && !db->mallocFailed ){
110818111748
int k, j, last;
110819111749
VdbeOp *pOp;
110820111750
110821
- pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
110822111751
last = sqlite3VdbeCurrentAddr(v);
110823
- for(k=pWInfo->iTop; k<last; k++, pOp++){
111752
+ k = pLevel->addrBody;
111753
+ pOp = sqlite3VdbeGetOp(v, k);
111754
+ for(; k<last; k++, pOp++){
110824111755
if( pOp->p1!=pLevel->iTabCur ) continue;
110825111756
if( pOp->opcode==OP_Column ){
110826111757
for(j=0; j<pIdx->nColumn; j++){
110827111758
if( pOp->p2==pIdx->aiColumn[j] ){
110828111759
pOp->p2 = j;
@@ -117967,11 +118898,11 @@
117967118898
memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
117968118899
db->autoCommit = 1;
117969118900
db->nextAutovac = -1;
117970118901
db->szMmap = sqlite3GlobalConfig.szMmap;
117971118902
db->nextPagesize = 0;
117972
- db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger
118903
+ db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
117973118904
#if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
117974118905
| SQLITE_AutoIndex
117975118906
#endif
117976118907
#if SQLITE_DEFAULT_FILE_FORMAT<4
117977118908
| SQLITE_LegacyFileFmt
@@ -130880,11 +131811,11 @@
130880131811
while( 1 ){
130881131812
130882131813
/* The following line of code (and the "p++" below the while() loop) is
130883131814
** normally all that is required to move pointer p to the desired
130884131815
** position. The exception is if this node is being loaded from disk
130885
- ** incrementally and pointer "p" now points to the first byte passed
131816
+ ** incrementally and pointer "p" now points to the first byte past
130886131817
** the populated part of pReader->aNode[].
130887131818
*/
130888131819
while( *p | c ) c = *p++ & 0x80;
130889131820
assert( *p==0 );
130890131821
@@ -132267,12 +133198,12 @@
132267133198
fts3SegReaderFirstDocid(p, apSegment[i]);
132268133199
}
132269133200
fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
132270133201
while( apSegment[0]->pOffsetList ){
132271133202
int j; /* Number of segments that share a docid */
132272
- char *pList;
132273
- int nList;
133203
+ char *pList = 0;
133204
+ int nList = 0;
132274133205
int nByte;
132275133206
sqlite3_int64 iDocid = apSegment[0]->iDocid;
132276133207
fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
132277133208
j = 1;
132278133209
while( j<nMerge
@@ -134554,15 +135485,15 @@
134554135485
}
134555135486
}
134556135487
if( pTC ) pModule->xClose(pTC);
134557135488
if( rc==SQLITE_DONE ) rc = SQLITE_OK;
134558135489
}
135490
+ }
134559135491
134560
- for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
134561
- if( pDef->pList ){
134562
- rc = fts3PendingListAppendVarint(&pDef->pList, 0);
134563
- }
135492
+ for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
135493
+ if( pDef->pList ){
135494
+ rc = fts3PendingListAppendVarint(&pDef->pList, 0);
134564135495
}
134565135496
}
134566135497
}
134567135498
134568135499
return rc;
@@ -135341,10 +136272,11 @@
135341136272
return SQLITE_NOMEM;
135342136273
}
135343136274
pStr->z = zNew;
135344136275
pStr->nAlloc = nAlloc;
135345136276
}
136277
+ assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
135346136278
135347136279
/* Append the data to the string buffer. */
135348136280
memcpy(&pStr->z[pStr->n], zAppend, nAppend);
135349136281
pStr->n += nAppend;
135350136282
pStr->z[pStr->n] = '\0';
135351136283
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.0. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -654,13 +654,13 @@
654 **
655 ** See also: [sqlite3_libversion()],
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.8.0"
660 #define SQLITE_VERSION_NUMBER 3008000
661 #define SQLITE_SOURCE_ID "2013-08-15 22:40:21 f2d175f975cd0be63425424ec322a98fb650019e"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -8368,10 +8368,24 @@
8368 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8369 # undef SQLITE_DEFAULT_MMAP_SIZE
8370 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8371 #endif
8372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8373 /*
8374 ** An instance of the following structure is used to store the busy-handler
8375 ** callback for a given sqlite handle.
8376 **
8377 ** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8587,11 +8601,11 @@
8587 #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
8588
8589 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8590 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8591 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8592 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
8593 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8594 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8595 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8596 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8597 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
@@ -9288,12 +9302,24 @@
9288 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
9289
9290 /*
9291 ** Flags that make up the mask passed to sqlite3PagerAcquire().
9292 */
9293 #define PAGER_ACQUIRE_NOCONTENT 0x01 /* Do not load data from disk */
9294 #define PAGER_ACQUIRE_READONLY 0x02 /* Read-only page is acceptable */
 
 
 
 
 
 
 
 
 
 
 
 
9295
9296 /*
9297 ** The remainder of this file contains the declarations of the functions
9298 ** that make up the Pager sub-system API. See source code comments for
9299 ** a detailed description of each routine.
@@ -9317,11 +9343,11 @@
9317 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9318 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9319 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9320 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
9321 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9322 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
9323 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9324 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9325 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
9326 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
9327 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
@@ -10180,36 +10206,37 @@
10180 /*
10181 ** Possible values for the sqlite3.flags.
10182 */
10183 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
10184 #define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
10185 #define SQLITE_FullColNames 0x00000004 /* Show full column names on SELECT */
10186 #define SQLITE_ShortColNames 0x00000008 /* Show short columns names */
10187 #define SQLITE_CountRows 0x00000010 /* Count rows changed by INSERT, */
 
 
 
10188 /* DELETE, or UPDATE and return */
10189 /* the count using a callback. */
10190 #define SQLITE_NullCallback 0x00000020 /* Invoke the callback once if the */
10191 /* result set is empty */
10192 #define SQLITE_SqlTrace 0x00000040 /* Debug print SQL as it executes */
10193 #define SQLITE_VdbeListing 0x00000080 /* Debug listings of VDBE programs */
10194 #define SQLITE_WriteSchema 0x00000100 /* OK to update SQLITE_MASTER */
10195 #define SQLITE_VdbeAddopTrace 0x00000200 /* Trace sqlite3VdbeAddOp() calls */
10196 #define SQLITE_IgnoreChecks 0x00000400 /* Do not enforce check constraints */
10197 #define SQLITE_ReadUncommitted 0x0000800 /* For shared-cache mode */
10198 #define SQLITE_LegacyFileFmt 0x00001000 /* Create new databases in format 1 */
10199 #define SQLITE_FullFSync 0x00002000 /* Use full fsync on the backend */
10200 #define SQLITE_CkptFullFSync 0x00004000 /* Use full fsync for checkpoint */
10201 #define SQLITE_RecoveryMode 0x00008000 /* Ignore schema errors */
10202 #define SQLITE_ReverseOrder 0x00010000 /* Reverse unordered SELECTs */
10203 #define SQLITE_RecTriggers 0x00020000 /* Enable recursive triggers */
10204 #define SQLITE_ForeignKeys 0x00040000 /* Enforce foreign key constraints */
10205 #define SQLITE_AutoIndex 0x00080000 /* Enable automatic indexes */
10206 #define SQLITE_PreferBuiltin 0x00100000 /* Preference to built-in funcs */
10207 #define SQLITE_LoadExtension 0x00200000 /* Enable load_extension */
10208 #define SQLITE_EnableTrigger 0x00400000 /* True to enable triggers */
10209 #define SQLITE_DeferFKs 0x00800000 /* Defer all FK constraints */
10210 #define SQLITE_QueryOnly 0x01000000 /* Disable database changes */
10211
10212
10213 /*
10214 ** Bits of the sqlite3.dbOptFlags field that are used by the
10215 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -10757,13 +10784,14 @@
10757 u16 nColumn; /* Number of columns in table used by this index */
10758 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10759 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10760 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10761 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10762 #ifdef SQLITE_ENABLE_STAT3
10763 int nSample; /* Number of elements in aSample[] */
10764 tRowcnt avgEq; /* Average nEq value for key values not in aSample */
 
10765 IndexSample *aSample; /* Samples of the left-most key */
10766 #endif
10767 };
10768
10769 /*
@@ -10770,20 +10798,15 @@
10770 ** Each sample stored in the sqlite_stat3 table is represented in memory
10771 ** using a structure of this type. See documentation at the top of the
10772 ** analyze.c source file for additional information.
10773 */
10774 struct IndexSample {
10775 union {
10776 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10777 double r; /* Value if eType is SQLITE_FLOAT */
10778 i64 i; /* Value if eType is SQLITE_INTEGER */
10779 } u;
10780 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10781 int nByte; /* Size in byte of text or blob. */
10782 tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10783 tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10784 tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10785 };
10786
10787 /*
10788 ** Each token coming out of the lexer is an instance of
10789 ** this structure. Tokens are also used as part of an expression.
@@ -11632,14 +11655,15 @@
11632 char *zBase; /* A base allocation. Not from malloc. */
11633 char *zText; /* The string collected so far */
11634 int nChar; /* Length of the string so far */
11635 int nAlloc; /* Amount of space allocated in zText */
11636 int mxAlloc; /* Maximum allowed string length */
11637 u8 mallocFailed; /* Becomes true if any memory allocation fails */
11638 u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */
11639 u8 tooBig; /* Becomes true if string size exceeds limits */
11640 };
 
 
11641
11642 /*
11643 ** A pointer to this structure is used to communicate information
11644 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
11645 */
@@ -12250,13 +12274,10 @@
12250 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12251 void(*)(void*));
12252 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12253 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12254 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12255 #ifdef SQLITE_ENABLE_STAT3
12256 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
12257 #endif
12258 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12259 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12260 #ifndef SQLITE_AMALGAMATION
12261 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12262 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12319,10 +12340,16 @@
12319 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12320
12321 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12322 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12323
 
 
 
 
 
 
12324 /*
12325 ** The interface to the LEMON-generated parser
12326 */
12327 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
12328 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
@@ -12360,17 +12387,18 @@
12360 # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
12361 # define sqlite3GetVTable(X,Y) ((VTable*)0)
12362 #else
12363 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
12364 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
12365 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
12366 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
12367 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
12368 SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
12369 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
12370 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
12371 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
 
12372 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
12373 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
12374 #endif
12375 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
12376 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
@@ -12901,11 +12929,13 @@
12901 "ENABLE_OVERSIZE_CELL_CHECK",
12902 #endif
12903 #ifdef SQLITE_ENABLE_RTREE
12904 "ENABLE_RTREE",
12905 #endif
12906 #ifdef SQLITE_ENABLE_STAT3
 
 
12907 "ENABLE_STAT3",
12908 #endif
12909 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12910 "ENABLE_UNLOCK_NOTIFY",
12911 #endif
@@ -13473,14 +13503,15 @@
13473 struct sqlite3_context {
13474 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
13475 Mem s; /* The return value is stored here */
13476 Mem *pMem; /* Memory cell used to store aggregate context */
13477 CollSeq *pColl; /* Collating sequence */
13478 int isError; /* Error code returned by the function. */
13479 int skipFlag; /* Skip skip accumulator loading if true */
13480 int iOp; /* Instruction number of OP_Function */
13481 Vdbe *pVdbe; /* The VM that owns this context */
 
 
13482 };
13483
13484 /*
13485 ** An Explain object accumulates indented output which is helpful
13486 ** in describing recursive data structures.
@@ -13552,11 +13583,11 @@
13552 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
13553 int nChange; /* Number of db changes made since last reset */
13554 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
13555 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
13556 int iStatement; /* Statement number (or 0 if has not opened stmt) */
13557 int aCounter[4]; /* Counters used by sqlite3_stmt_status() */
13558 #ifndef SQLITE_OMIT_TRACE
13559 i64 startTime; /* Time when query started - used for profiling */
13560 #endif
13561 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
13562 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -16059,11 +16090,11 @@
16059 struct MemBlockHdr *pHdr;
16060 if( !p ){
16061 return 0;
16062 }
16063 pHdr = sqlite3MemsysGetHeader(p);
16064 return pHdr->iSize;
16065 }
16066
16067 /*
16068 ** Initialize the memory allocation subsystem.
16069 */
@@ -16101,19 +16132,19 @@
16101 static void randomFill(char *pBuf, int nByte){
16102 unsigned int x, y, r;
16103 x = SQLITE_PTR_TO_INT(pBuf);
16104 y = nByte | 1;
16105 while( nByte >= 4 ){
16106 x = (x>>1) ^ (-(x&1) & 0xd0000001);
16107 y = y*1103515245 + 12345;
16108 r = x ^ y;
16109 *(int*)pBuf = r;
16110 pBuf += 4;
16111 nByte -= 4;
16112 }
16113 while( nByte-- > 0 ){
16114 x = (x>>1) ^ (-(x&1) & 0xd0000001);
16115 y = y*1103515245 + 12345;
16116 r = x ^ y;
16117 *(pBuf++) = r & 0xff;
16118 }
16119 }
@@ -16204,13 +16235,13 @@
16204 assert( mem.pLast==pHdr );
16205 mem.pLast = pHdr->pPrev;
16206 }
16207 z = (char*)pBt;
16208 z -= pHdr->nTitle;
16209 adjustStats(pHdr->iSize, -1);
16210 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16211 pHdr->iSize + sizeof(int) + pHdr->nTitle);
16212 free(z);
16213 sqlite3_mutex_leave(mem.mutex);
16214 }
16215
16216 /*
@@ -16230,11 +16261,11 @@
16230 pOldHdr = sqlite3MemsysGetHeader(pPrior);
16231 pNew = sqlite3MemMalloc(nByte);
16232 if( pNew ){
16233 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
16234 if( nByte>pOldHdr->iSize ){
16235 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
16236 }
16237 sqlite3MemFree(pPrior);
16238 }
16239 return pNew;
16240 }
@@ -16345,11 +16376,11 @@
16345 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16346 struct MemBlockHdr *pHdr;
16347 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16348 void **pBt = (void**)pHdr;
16349 pBt -= pHdr->nBacktraceSlots;
16350 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16351 }
16352 }
16353
16354 /*
16355 ** Open the file indicated and write a log of all unfreed memory
@@ -18467,11 +18498,11 @@
18467 GetVersionEx(&sInfo);
18468 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18469 }
18470 return osType==2;
18471 }
18472 #endif /* SQLITE_OS_WINCE */
18473 #endif
18474
18475 #ifdef SQLITE_DEBUG
18476 /*
18477 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18505,11 +18536,11 @@
18505 /* As winMutexInit() and winMutexEnd() are called as part
18506 ** of the sqlite3_initialize and sqlite3_shutdown()
18507 ** processing, the "interlocked" magic is probably not
18508 ** strictly necessary.
18509 */
18510 static long winMutex_lock = 0;
18511
18512 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18513
18514 static int winMutexInit(void){
18515 /* The first to increment to 1 does actual initialization */
@@ -19885,11 +19916,11 @@
19885 zOut = buf;
19886 }else{
19887 nOut = precision + 10;
19888 zOut = zExtra = sqlite3Malloc( nOut );
19889 if( zOut==0 ){
19890 pAccum->mallocFailed = 1;
19891 return;
19892 }
19893 }
19894 bufpt = &zOut[nOut-1];
19895 if( xtype==etORDINAL ){
@@ -19997,11 +20028,11 @@
19997 e2 = exp;
19998 }
19999 if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
20000 bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
20001 if( bufpt==0 ){
20002 pAccum->mallocFailed = 1;
20003 return;
20004 }
20005 }
20006 zOut = bufpt;
20007 nsd = 16 + flag_altform2*10;
@@ -20132,11 +20163,11 @@
20132 needQuote = !isnull && xtype==etSQLESCAPE2;
20133 n += i + 1 + needQuote*2;
20134 if( n>etBUFSIZE ){
20135 bufpt = zExtra = sqlite3Malloc( n );
20136 if( bufpt==0 ){
20137 pAccum->mallocFailed = 1;
20138 return;
20139 }
20140 }else{
20141 bufpt = buf;
20142 }
@@ -20210,26 +20241,24 @@
20210 /*
20211 ** Append N bytes of text from z to the StrAccum object.
20212 */
20213 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
20214 assert( z!=0 || N==0 );
20215 if( p->tooBig | p->mallocFailed ){
20216 testcase(p->tooBig);
20217 testcase(p->mallocFailed);
20218 return;
20219 }
20220 assert( p->zText!=0 || p->nChar==0 );
20221 if( N<0 ){
 
20222 N = sqlite3Strlen30(z);
20223 }
20224 if( N==0 || NEVER(z==0) ){
20225 return;
20226 }
20227 if( p->nChar+N >= p->nAlloc ){
20228 char *zNew;
20229 if( !p->useMalloc ){
20230 p->tooBig = 1;
20231 N = p->nAlloc - p->nChar - 1;
20232 if( N<=0 ){
20233 return;
20234 }
20235 }else{
@@ -20236,11 +20265,11 @@
20236 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
20237 i64 szNew = p->nChar;
20238 szNew += N + 1;
20239 if( szNew > p->mxAlloc ){
20240 sqlite3StrAccumReset(p);
20241 p->tooBig = 1;
20242 return;
20243 }else{
20244 p->nAlloc = (int)szNew;
20245 }
20246 if( p->useMalloc==1 ){
@@ -20250,11 +20279,11 @@
20250 }
20251 if( zNew ){
20252 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
20253 p->zText = zNew;
20254 }else{
20255 p->mallocFailed = 1;
20256 sqlite3StrAccumReset(p);
20257 return;
20258 }
20259 }
20260 }
@@ -20278,11 +20307,11 @@
20278 p->zText = sqlite3_malloc(p->nChar+1);
20279 }
20280 if( p->zText ){
20281 memcpy(p->zText, p->zBase, p->nChar+1);
20282 }else{
20283 p->mallocFailed = 1;
20284 }
20285 }
20286 }
20287 return p->zText;
20288 }
@@ -20309,12 +20338,11 @@
20309 p->db = 0;
20310 p->nChar = 0;
20311 p->nAlloc = n;
20312 p->mxAlloc = mx;
20313 p->useMalloc = 1;
20314 p->tooBig = 0;
20315 p->mallocFailed = 0;
20316 }
20317
20318 /*
20319 ** Print into memory obtained from sqliteMalloc(). Use the internal
20320 ** %-conversion extensions.
@@ -20327,11 +20355,11 @@
20327 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
20328 db->aLimit[SQLITE_LIMIT_LENGTH]);
20329 acc.db = db;
20330 sqlite3VXPrintf(&acc, 1, zFormat, ap);
20331 z = sqlite3StrAccumFinish(&acc);
20332 if( acc.mallocFailed ){
20333 db->mallocFailed = 1;
20334 }
20335 return z;
20336 }
20337
@@ -20524,28 +20552,15 @@
20524 unsigned char i, j; /* State variables */
20525 unsigned char s[256]; /* State variables */
20526 } sqlite3Prng;
20527
20528 /*
20529 ** Get a single 8-bit random value from the RC4 PRNG. The Mutex
20530 ** must be held while executing this routine.
20531 **
20532 ** Why not just use a library random generator like lrand48() for this?
20533 ** Because the OP_NewRowid opcode in the VDBE depends on having a very
20534 ** good source of random numbers. The lrand48() library function may
20535 ** well be good enough. But maybe not. Or maybe lrand48() has some
20536 ** subtle problems on some systems that could cause problems. It is hard
20537 ** to know. To minimize the risk of problems due to bad lrand48()
20538 ** implementations, SQLite uses this random number generator based
20539 ** on RC4, which we know works very well.
20540 **
20541 ** (Later): Actually, OP_NewRowid does not depend on a good source of
20542 ** randomness any more. But we will leave this code in all the same.
20543 */
20544 static u8 randomByte(void){
20545 unsigned char t;
20546
20547
20548 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
20549 ** state vector. If writable static data is unsupported on the target,
20550 ** we have to locate the state vector at run-time. In the more common
20551 ** case where writable static data is supported, wsdPrng can refer directly
@@ -20556,10 +20571,14 @@
20556 # define wsdPrng p[0]
20557 #else
20558 # define wsdPrng sqlite3Prng
20559 #endif
20560
 
 
 
 
20561
20562 /* Initialize the state of the random number generator once,
20563 ** the first time this routine is called. The seed value does
20564 ** not need to contain a lot of randomness since we are not
20565 ** trying to do secure encryption or anything like that...
@@ -20584,32 +20603,18 @@
20584 wsdPrng.s[i] = t;
20585 }
20586 wsdPrng.isInit = 1;
20587 }
20588
20589 /* Generate and return single random byte
20590 */
20591 wsdPrng.i++;
20592 t = wsdPrng.s[wsdPrng.i];
20593 wsdPrng.j += t;
20594 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
20595 wsdPrng.s[wsdPrng.j] = t;
20596 t += wsdPrng.s[wsdPrng.i];
20597 return wsdPrng.s[t];
20598 }
20599
20600 /*
20601 ** Return N random bytes.
20602 */
20603 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
20604 unsigned char *zBuf = pBuf;
20605 #if SQLITE_THREADSAFE
20606 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
20607 #endif
20608 sqlite3_mutex_enter(mutex);
20609 while( N-- ){
20610 *(zBuf++) = randomByte();
 
 
 
 
 
 
20611 }
20612 sqlite3_mutex_leave(mutex);
20613 }
20614
20615 #ifndef SQLITE_OMIT_BUILTIN_TEST
@@ -21092,36 +21097,10 @@
21092 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21093 assert( m.z || db->mallocFailed );
21094 return m.z;
21095 }
21096
21097 /*
21098 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
21099 ** enc. A pointer to the new string is returned, and the value of *pnOut
21100 ** is set to the length of the returned string in bytes. The call should
21101 ** arrange to call sqlite3DbFree() on the returned pointer when it is
21102 ** no longer required.
21103 **
21104 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
21105 ** flag set.
21106 */
21107 #ifdef SQLITE_ENABLE_STAT3
21108 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
21109 Mem m;
21110 memset(&m, 0, sizeof(m));
21111 m.db = db;
21112 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
21113 if( sqlite3VdbeMemTranslate(&m, enc) ){
21114 assert( db->mallocFailed );
21115 return 0;
21116 }
21117 assert( m.z==m.zMalloc );
21118 *pnOut = m.n;
21119 return m.z;
21120 }
21121 #endif
21122
21123 /*
21124 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21125 ** Return the number of bytes in the first nChar unicode characters
21126 ** in pZ. nChar must be non-negative.
21127 */
@@ -23074,15 +23053,17 @@
23074 void *lockingContext; /* Locking style specific state */
23075 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
23076 const char *zPath; /* Name of the file */
23077 unixShm *pShm; /* Shared memory segment information */
23078 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
 
23079 int nFetchOut; /* Number of outstanding xFetch refs */
23080 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23081 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23082 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23083 void *pMapRegion; /* Memory mapped region */
 
23084 #ifdef __QNXNTO__
23085 int sectorSize; /* Device sector size */
23086 int deviceCharacteristics; /* Precomputed device characteristics */
23087 #endif
23088 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -23513,10 +23494,11 @@
23513 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23514
23515 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23516 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23517
 
23518 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23519 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23520
23521 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23522 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23525,10 +23507,11 @@
23525 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23526 #else
23527 { "mremap", (sqlite3_syscall_ptr)0, 0 },
23528 #endif
23529 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
 
23530
23531 }; /* End of the overrideable system calls */
23532
23533 /*
23534 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23631,17 +23614,27 @@
23631 ** recover the hot journals.
23632 */
23633 static int robust_open(const char *z, int f, mode_t m){
23634 int fd;
23635 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23636 do{
23637 #if defined(O_CLOEXEC)
23638 fd = osOpen(z,f|O_CLOEXEC,m2);
23639 #else
23640 fd = osOpen(z,f,m2);
23641 #endif
23642 }while( fd<0 && errno==EINTR );
 
 
 
 
 
 
 
 
 
 
23643 if( fd>=0 ){
23644 if( m!=0 ){
23645 struct stat statbuf;
23646 if( osFstat(fd, &statbuf)==0
23647 && statbuf.st_size==0
@@ -24935,12 +24928,14 @@
24935 static int unixUnlock(sqlite3_file *id, int eFileLock){
24936 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
24937 return posixUnlock(id, eFileLock, 0);
24938 }
24939
 
24940 static int unixMapfile(unixFile *pFd, i64 nByte);
24941 static void unixUnmapfile(unixFile *pFd);
 
24942
24943 /*
24944 ** This function performs the parts of the "close file" operation
24945 ** common to all locking schemes. It closes the directory and file
24946 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24950,11 +24945,13 @@
24950 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24951 ** vxworksReleaseFileId() routine.
24952 */
24953 static int closeUnixFile(sqlite3_file *id){
24954 unixFile *pFile = (unixFile*)id;
 
24955 unixUnmapfile(pFile);
 
24956 if( pFile->h>=0 ){
24957 robust_close(pFile, pFile->h, __LINE__);
24958 pFile->h = -1;
24959 }
24960 #if OS_VXWORKS
@@ -26155,10 +26152,11 @@
26155 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26156 i64 newOffset;
26157 #endif
26158 TIMER_START;
26159 assert( cnt==(cnt&0x1ffff) );
 
26160 cnt &= 0x1ffff;
26161 do{
26162 #if defined(USE_PREAD)
26163 got = osPread(id->h, pBuf, cnt, offset);
26164 SimulateIOError( got = -1 );
@@ -26269,10 +26267,11 @@
26269 int *piErrno /* OUT: Error number if error occurs */
26270 ){
26271 int rc = 0; /* Value returned by system call */
26272
26273 assert( nBuf==(nBuf&0x1ffff) );
 
26274 nBuf &= 0x1ffff;
26275 TIMER_START;
26276
26277 #if defined(USE_PREAD)
26278 do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26654,17 +26653,19 @@
26654 if( pFile->inNormalWrite && nByte==0 ){
26655 pFile->transCntrChng = 1;
26656 }
26657 #endif
26658
 
26659 /* If the file was just truncated to a size smaller than the currently
26660 ** mapped region, reduce the effective mapping size as well. SQLite will
26661 ** use read() and write() to access data beyond this point from now on.
26662 */
26663 if( nByte<pFile->mmapSize ){
26664 pFile->mmapSize = nByte;
26665 }
 
26666
26667 return SQLITE_OK;
26668 }
26669 }
26670
@@ -26750,10 +26751,11 @@
26750 }
26751 #endif
26752 }
26753 }
26754
 
26755 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26756 int rc;
26757 if( pFile->szChunk<=0 ){
26758 if( robust_ftruncate(pFile->h, nByte) ){
26759 pFile->lastErrno = errno;
@@ -26762,10 +26764,11 @@
26762 }
26763
26764 rc = unixMapfile(pFile, nByte);
26765 return rc;
26766 }
 
26767
26768 return SQLITE_OK;
26769 }
26770
26771 /*
@@ -26830,10 +26833,11 @@
26830 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26831 *(char**)pArg = zTFile;
26832 }
26833 return SQLITE_OK;
26834 }
 
26835 case SQLITE_FCNTL_MMAP_SIZE: {
26836 i64 newLimit = *(i64*)pArg;
26837 int rc = SQLITE_OK;
26838 if( newLimit>sqlite3GlobalConfig.mxMmap ){
26839 newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26846,10 +26850,11 @@
26846 rc = unixMapfile(pFile, -1);
26847 }
26848 }
26849 return rc;
26850 }
 
26851 #ifdef SQLITE_DEBUG
26852 /* The pager calls this method to signal that it has done
26853 ** a rollback and that the database is therefore unchanged and
26854 ** it hence it is OK for the transaction change counter to be
26855 ** unchanged.
@@ -27656,26 +27661,24 @@
27656 # define unixShmLock 0
27657 # define unixShmBarrier 0
27658 # define unixShmUnmap 0
27659 #endif /* #ifndef SQLITE_OMIT_WAL */
27660
 
27661 /*
27662 ** If it is currently memory mapped, unmap file pFd.
27663 */
27664 static void unixUnmapfile(unixFile *pFd){
27665 assert( pFd->nFetchOut==0 );
27666 #if SQLITE_MAX_MMAP_SIZE>0
27667 if( pFd->pMapRegion ){
27668 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27669 pFd->pMapRegion = 0;
27670 pFd->mmapSize = 0;
27671 pFd->mmapSizeActual = 0;
27672 }
27673 #endif
27674 }
27675
27676 #if SQLITE_MAX_MMAP_SIZE>0
27677 /*
27678 ** Return the system page size.
27679 */
27680 static int unixGetPagesize(void){
27681 #if HAVE_MREMAP
@@ -27684,13 +27687,11 @@
27684 return getpagesize();
27685 #else
27686 return (int)sysconf(_SC_PAGESIZE);
27687 #endif
27688 }
27689 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
27690
27691 #if SQLITE_MAX_MMAP_SIZE>0
27692 /*
27693 ** Attempt to set the size of the memory mapping maintained by file
27694 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27695 **
27696 ** If successful, this function sets the following variables:
@@ -27771,11 +27772,10 @@
27771 pFd->mmapSizeMax = 0;
27772 }
27773 pFd->pMapRegion = (void *)pNew;
27774 pFd->mmapSize = pFd->mmapSizeActual = nNew;
27775 }
27776 #endif
27777
27778 /*
27779 ** Memory map or remap the file opened by file-descriptor pFd (if the file
27780 ** is already mapped, the existing mapping is replaced by the new). Or, if
27781 ** there already exists a mapping for this file, and there are still
@@ -27790,11 +27790,10 @@
27790 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
27791 ** recreated as a result of outstanding references) or an SQLite error
27792 ** code otherwise.
27793 */
27794 static int unixMapfile(unixFile *pFd, i64 nByte){
27795 #if SQLITE_MAX_MMAP_SIZE>0
27796 i64 nMap = nByte;
27797 int rc;
27798
27799 assert( nMap>=0 || pFd->nFetchOut==0 );
27800 if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27816,14 +27815,14 @@
27816 unixRemapfile(pFd, nMap);
27817 }else{
27818 unixUnmapfile(pFd);
27819 }
27820 }
27821 #endif
27822
27823 return SQLITE_OK;
27824 }
 
27825
27826 /*
27827 ** If possible, return a pointer to a mapping of file fd starting at offset
27828 ** iOff. The mapping must be valid for at least nAmt bytes.
27829 **
@@ -27868,10 +27867,11 @@
27868 */
27869 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27870 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27871 UNUSED_PARAMETER(iOff);
27872
 
27873 /* If p==0 (unmap the entire file) then there must be no outstanding
27874 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27875 ** then there must be at least one outstanding. */
27876 assert( (p==0)==(pFd->nFetchOut==0) );
27877
@@ -27883,10 +27883,11 @@
27883 }else{
27884 unixUnmapfile(pFd);
27885 }
27886
27887 assert( pFd->nFetchOut>=0 );
 
27888 return SQLITE_OK;
27889 }
27890
27891 /*
27892 ** Here ends the implementation of all sqlite3_file methods.
@@ -28214,11 +28215,13 @@
28214 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28215 pNew->h = h;
28216 pNew->pVfs = pVfs;
28217 pNew->zPath = zFilename;
28218 pNew->ctrlFlags = (u8)ctrlFlags;
 
28219 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 
28220 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28221 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28222 pNew->ctrlFlags |= UNIXFILE_PSOW;
28223 }
28224 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -30715,11 +30718,11 @@
30715 /*
30716 ** Compiling and using WAL mode requires several APIs that are only
30717 ** available in Windows platforms based on the NT kernel.
30718 */
30719 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30720 # error "WAL mode requires support from the Windows NT kernel, compile\
30721 with SQLITE_OMIT_WAL."
30722 #endif
30723
30724 /*
30725 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
@@ -30735,10 +30738,70 @@
30735 */
30736 #if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
30737 # define SQLITE_WIN32_HAS_WIDE
30738 #endif
30739
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30740 /*
30741 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
30742 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
30743 ** are not present in the header file)?
30744 */
@@ -31742,15 +31805,15 @@
31742 ** this routine is used to determine if the host is Win95/98/ME or
31743 ** WinNT/2K/XP so that we will know whether or not we can safely call
31744 ** the LockFileEx() API.
31745 */
31746 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31747 # define isNT() (1)
31748 #elif !defined(SQLITE_WIN32_HAS_WIDE)
31749 # define isNT() (0)
31750 #else
31751 static int isNT(void){
31752 if( sqlite3_os_type==0 ){
31753 OSVERSIONINFOA sInfo;
31754 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31755 osGetVersionExA(&sInfo);
31756 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31957,11 +32020,11 @@
31957 /*
31958 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
31959 **
31960 ** Space to hold the returned string is obtained from malloc.
31961 */
31962 static LPWSTR utf8ToUnicode(const char *zFilename){
31963 int nChar;
31964 LPWSTR zWideFilename;
31965
31966 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
31967 if( nChar==0 ){
@@ -31982,11 +32045,11 @@
31982
31983 /*
31984 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
31985 ** obtained from sqlite3_malloc().
31986 */
31987 static char *unicodeToUtf8(LPCWSTR zWideFilename){
31988 int nByte;
31989 char *zFilename;
31990
31991 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
31992 if( nByte == 0 ){
@@ -32010,11 +32073,11 @@
32010 ** current codepage settings for file apis.
32011 **
32012 ** Space to hold the returned string is obtained
32013 ** from sqlite3_malloc.
32014 */
32015 static LPWSTR mbcsToUnicode(const char *zFilename){
32016 int nByte;
32017 LPWSTR zMbcsFilename;
32018 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32019
32020 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32040,11 +32103,11 @@
32040 ** user's ANSI codepage.
32041 **
32042 ** Space to hold the returned string is obtained from
32043 ** sqlite3_malloc().
32044 */
32045 static char *unicodeToMbcs(LPCWSTR zWideFilename){
32046 int nByte;
32047 char *zFilename;
32048 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32049
32050 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32070,15 +32133,15 @@
32070 */
32071 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32072 char *zFilenameUtf8;
32073 LPWSTR zTmpWide;
32074
32075 zTmpWide = mbcsToUnicode(zFilename);
32076 if( zTmpWide==0 ){
32077 return 0;
32078 }
32079 zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32080 sqlite3_free(zTmpWide);
32081 return zFilenameUtf8;
32082 }
32083
32084 /*
@@ -32087,15 +32150,15 @@
32087 */
32088 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32089 char *zFilenameMbcs;
32090 LPWSTR zTmpWide;
32091
32092 zTmpWide = utf8ToUnicode(zFilename);
32093 if( zTmpWide==0 ){
32094 return 0;
32095 }
32096 zFilenameMbcs = unicodeToMbcs(zTmpWide);
32097 sqlite3_free(zTmpWide);
32098 return zFilenameMbcs;
32099 }
32100
32101 /*
@@ -32121,11 +32184,11 @@
32121 );
32122 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
32123 if( ppDirectory ){
32124 char *zValueUtf8 = 0;
32125 if( zValue && zValue[0] ){
32126 zValueUtf8 = unicodeToUtf8(zValue);
32127 if ( zValueUtf8==0 ){
32128 return SQLITE_NOMEM;
32129 }
32130 }
32131 sqlite3_free(*ppDirectory);
@@ -32134,32 +32197,32 @@
32134 }
32135 return SQLITE_ERROR;
32136 }
32137
32138 /*
32139 ** The return value of getLastErrorMsg
32140 ** is zero if the error message fits in the buffer, or non-zero
32141 ** otherwise (if the message was truncated).
32142 */
32143 static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32144 /* FormatMessage returns 0 on failure. Otherwise it
32145 ** returns the number of TCHARs written to the output
32146 ** buffer, excluding the terminating null char.
32147 */
32148 DWORD dwLen = 0;
32149 char *zOut = 0;
32150
32151 if( isNT() ){
32152 #if SQLITE_OS_WINRT
32153 WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
32154 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
32155 FORMAT_MESSAGE_IGNORE_INSERTS,
32156 NULL,
32157 lastErrno,
32158 0,
32159 zTempWide,
32160 MAX_PATH,
32161 0);
32162 #else
32163 LPWSTR zTempWide = NULL;
32164 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
32165 FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32172,11 +32235,11 @@
32172 0);
32173 #endif
32174 if( dwLen > 0 ){
32175 /* allocate a buffer and convert to UTF8 */
32176 sqlite3BeginBenignMalloc();
32177 zOut = unicodeToUtf8(zTempWide);
32178 sqlite3EndBenignMalloc();
32179 #if !SQLITE_OS_WINRT
32180 /* free the system buffer allocated by FormatMessage */
32181 osLocalFree(zTempWide);
32182 #endif
@@ -32240,11 +32303,11 @@
32240 ){
32241 char zMsg[500]; /* Human readable error text */
32242 int i; /* Loop counter */
32243
32244 zMsg[0] = 0;
32245 getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32246 assert( errcode!=SQLITE_OK );
32247 if( zPath==0 ) zPath = "";
32248 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32249 zMsg[i] = 0;
32250 sqlite3_log(errcode,
@@ -32265,30 +32328,30 @@
32265 # define SQLITE_WIN32_IOERR_RETRY 10
32266 #endif
32267 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
32268 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
32269 #endif
32270 static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32271 static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32272
32273 /*
32274 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
32275 ** to see if it should be retried. Return TRUE to retry. Return FALSE
32276 ** to give up with an error.
32277 */
32278 static int retryIoerr(int *pnRetry, DWORD *pError){
32279 DWORD e = osGetLastError();
32280 if( *pnRetry>=win32IoerrRetry ){
32281 if( pError ){
32282 *pError = e;
32283 }
32284 return 0;
32285 }
32286 if( e==ERROR_ACCESS_DENIED ||
32287 e==ERROR_LOCK_VIOLATION ||
32288 e==ERROR_SHARING_VIOLATION ){
32289 sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
32290 ++*pnRetry;
32291 return 1;
32292 }
32293 if( pError ){
32294 *pError = e;
@@ -32297,15 +32360,15 @@
32297 }
32298
32299 /*
32300 ** Log a I/O error retry episode.
32301 */
32302 static void logIoerr(int nRetry){
32303 if( nRetry ){
32304 sqlite3_log(SQLITE_IOERR,
32305 "delayed %dms for lock/sharing conflict",
32306 win32IoerrRetryDelay*nRetry*(nRetry+1)/2
32307 );
32308 }
32309 }
32310
32311 #if SQLITE_OS_WINCE
@@ -32366,11 +32429,11 @@
32366 LPWSTR zName;
32367 DWORD lastErrno;
32368 BOOL bLogged = FALSE;
32369 BOOL bInit = TRUE;
32370
32371 zName = utf8ToUnicode(zFilename);
32372 if( zName==0 ){
32373 /* out of memory */
32374 return SQLITE_IOERR_NOMEM;
32375 }
32376
@@ -32639,11 +32702,11 @@
32639 ** API LockFile.
32640 */
32641 return winceLockFile(phFile, offsetLow, offsetHigh,
32642 numBytesLow, numBytesHigh);
32643 #else
32644 if( isNT() ){
32645 OVERLAPPED ovlp;
32646 memset(&ovlp, 0, sizeof(OVERLAPPED));
32647 ovlp.Offset = offsetLow;
32648 ovlp.OffsetHigh = offsetHigh;
32649 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32670,11 +32733,11 @@
32670 ** API UnlockFile.
32671 */
32672 return winceUnlockFile(phFile, offsetLow, offsetHigh,
32673 numBytesLow, numBytesHigh);
32674 #else
32675 if( isNT() ){
32676 OVERLAPPED ovlp;
32677 memset(&ovlp, 0, sizeof(OVERLAPPED));
32678 ovlp.Offset = offsetLow;
32679 ovlp.OffsetHigh = offsetHigh;
32680 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32700,11 +32763,11 @@
32700 /*
32701 ** Move the current position of the file handle passed as the first
32702 ** argument to offset iOffset within the file. If successful, return 0.
32703 ** Otherwise, set pFile->lastErrno and return non-zero.
32704 */
32705 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32706 #if !SQLITE_OS_WINRT
32707 LONG upperBits; /* Most sig. 32 bits of new offset */
32708 LONG lowerBits; /* Least sig. 32 bits of new offset */
32709 DWORD dwRet; /* Value returned by SetFilePointer() */
32710 DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32725,11 +32788,11 @@
32725
32726 if( (dwRet==INVALID_SET_FILE_POINTER
32727 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
32728 pFile->lastErrno = lastErrno;
32729 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32730 "seekWinFile", pFile->zPath);
32731 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32732 return 1;
32733 }
32734
32735 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32746,11 +32809,11 @@
32746 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
32747
32748 if(!bRet){
32749 pFile->lastErrno = osGetLastError();
32750 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32751 "seekWinFile", pFile->zPath);
32752 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32753 return 1;
32754 }
32755
32756 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32861,11 +32924,11 @@
32861 }
32862 }
32863 #endif
32864
32865 #if SQLITE_OS_WINCE
32866 if( seekWinFile(pFile, offset) ){
32867 OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
32868 return SQLITE_FULL;
32869 }
32870 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32871 #else
@@ -32874,17 +32937,17 @@
32874 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
32875 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
32876 osGetLastError()!=ERROR_HANDLE_EOF ){
32877 #endif
32878 DWORD lastErrno;
32879 if( retryIoerr(&nRetry, &lastErrno) ) continue;
32880 pFile->lastErrno = lastErrno;
32881 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
32882 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
32883 "winRead", pFile->zPath);
32884 }
32885 logIoerr(nRetry);
32886 if( nRead<(DWORD)amt ){
32887 /* Unread parts of the buffer must be zero-filled */
32888 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32889 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
32890 return SQLITE_IOERR_SHORT_READ;
@@ -32933,11 +32996,11 @@
32933 }
32934 }
32935 #endif
32936
32937 #if SQLITE_OS_WINCE
32938 rc = seekWinFile(pFile, offset);
32939 if( rc==0 ){
32940 #else
32941 {
32942 #endif
32943 #if !SQLITE_OS_WINCE
@@ -32958,11 +33021,11 @@
32958 #if SQLITE_OS_WINCE
32959 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
32960 #else
32961 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
32962 #endif
32963 if( retryIoerr(&nRetry, &lastErrno) ) continue;
32964 break;
32965 }
32966 assert( nWrite==0 || nWrite<=(DWORD)nRem );
32967 if( nWrite==0 || nWrite>(DWORD)nRem ){
32968 lastErrno = osGetLastError();
@@ -32990,11 +33053,11 @@
32990 }
32991 OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
32992 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
32993 "winWrite", pFile->zPath);
32994 }else{
32995 logIoerr(nRetry);
32996 }
32997 OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
32998 return SQLITE_OK;
32999 }
33000
@@ -33019,11 +33082,11 @@
33019 if( pFile->szChunk>0 ){
33020 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33021 }
33022
33023 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33024 if( seekWinFile(pFile, nByte) ){
33025 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33026 "winTruncate1", pFile->zPath);
33027 }else if( 0==osSetEndOfFile(pFile->h) &&
33028 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33029 pFile->lastErrno = lastErrno;
@@ -33100,10 +33163,11 @@
33100
33101 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33102 ** no-op
33103 */
33104 #ifdef SQLITE_NO_SYNC
 
33105 return SQLITE_OK;
33106 #else
33107 rc = osFlushFileBuffers(pFile->h);
33108 SimulateIOError( rc=FALSE );
33109 if( rc ){
@@ -33197,14 +33261,14 @@
33197 /*
33198 ** Acquire a reader lock.
33199 ** Different API routines are called depending on whether or not this
33200 ** is Win9x or WinNT.
33201 */
33202 static int getReadLock(winFile *pFile){
33203 int res;
33204 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33205 if( isNT() ){
33206 #if SQLITE_OS_WINCE
33207 /*
33208 ** NOTE: Windows CE is handled differently here due its lack of the Win32
33209 ** API LockFileEx.
33210 */
@@ -33232,15 +33296,15 @@
33232 }
33233
33234 /*
33235 ** Undo a readlock
33236 */
33237 static int unlockReadLock(winFile *pFile){
33238 int res;
33239 DWORD lastErrno;
33240 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33241 if( isNT() ){
33242 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33243 }
33244 #ifdef SQLITE_WIN32_HAS_ANSI
33245 else{
33246 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33247,11 +33311,11 @@
33247 }
33248 #endif
33249 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33250 pFile->lastErrno = lastErrno;
33251 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33252 "unlockReadLock", pFile->zPath);
33253 }
33254 OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
33255 return res;
33256 }
33257
@@ -33338,11 +33402,11 @@
33338
33339 /* Acquire a shared lock
33340 */
33341 if( locktype==SHARED_LOCK && res ){
33342 assert( pFile->locktype==NO_LOCK );
33343 res = getReadLock(pFile);
33344 if( res ){
33345 newLocktype = SHARED_LOCK;
33346 }else{
33347 lastErrno = osGetLastError();
33348 }
@@ -33369,18 +33433,18 @@
33369
33370 /* Acquire an EXCLUSIVE lock
33371 */
33372 if( locktype==EXCLUSIVE_LOCK && res ){
33373 assert( pFile->locktype>=SHARED_LOCK );
33374 res = unlockReadLock(pFile);
33375 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
33376 SHARED_SIZE, 0);
33377 if( res ){
33378 newLocktype = EXCLUSIVE_LOCK;
33379 }else{
33380 lastErrno = osGetLastError();
33381 getReadLock(pFile);
33382 }
33383 }
33384
33385 /* If we are holding a PENDING lock that ought to be released, then
33386 ** release it now.
@@ -33393,14 +33457,14 @@
33393 ** return the appropriate result code.
33394 */
33395 if( res ){
33396 rc = SQLITE_OK;
33397 }else{
 
 
33398 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
33399 pFile->h, locktype, newLocktype));
33400 pFile->lastErrno = lastErrno;
33401 rc = SQLITE_BUSY;
33402 }
33403 pFile->locktype = (u8)newLocktype;
33404 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
33405 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
33406 return rc;
@@ -33456,11 +33520,11 @@
33456 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
33457 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
33458 type = pFile->locktype;
33459 if( type>=EXCLUSIVE_LOCK ){
33460 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33461 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33462 /* This should never happen. We should always be able to
33463 ** reacquire the read lock */
33464 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
33465 "winUnlock", pFile->zPath);
33466 }
@@ -33467,11 +33531,11 @@
33467 }
33468 if( type>=RESERVED_LOCK ){
33469 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
33470 }
33471 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33472 unlockReadLock(pFile);
33473 }
33474 if( type>=PENDING_LOCK ){
33475 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
33476 }
33477 pFile->locktype = (u8)locktype;
@@ -33495,11 +33559,11 @@
33495 pFile->ctrlFlags |= mask;
33496 }
33497 }
33498
33499 /* Forward declaration */
33500 static int getTempname(int nBuf, char *zBuf);
33501 #if SQLITE_MAX_MMAP_SIZE>0
33502 static int winMapfile(winFile*, sqlite3_int64);
33503 #endif
33504
33505 /*
@@ -33558,30 +33622,30 @@
33558 return SQLITE_OK;
33559 }
33560 case SQLITE_FCNTL_WIN32_AV_RETRY: {
33561 int *a = (int*)pArg;
33562 if( a[0]>0 ){
33563 win32IoerrRetry = a[0];
33564 }else{
33565 a[0] = win32IoerrRetry;
33566 }
33567 if( a[1]>0 ){
33568 win32IoerrRetryDelay = a[1];
33569 }else{
33570 a[1] = win32IoerrRetryDelay;
33571 }
33572 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33573 return SQLITE_OK;
33574 }
33575 case SQLITE_FCNTL_TEMPFILENAME: {
33576 char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
33577 if( zTFile ){
33578 getTempname(pFile->pVfs->mxPathname, zTFile);
33579 *(char**)pArg = zTFile;
33580 }
33581 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33582 return SQLITE_OK;
33583 }
33584 #if SQLITE_MAX_MMAP_SIZE>0
33585 case SQLITE_FCNTL_MMAP_SIZE: {
33586 i64 newLimit = *(i64*)pArg;
33587 int rc = SQLITE_OK;
@@ -34539,14 +34603,14 @@
34539 ** Convert a UTF-8 filename into whatever form the underlying
34540 ** operating system wants filenames in. Space to hold the result
34541 ** is obtained from malloc and must be freed by the calling
34542 ** function.
34543 */
34544 static void *convertUtf8Filename(const char *zFilename){
34545 void *zConverted = 0;
34546 if( isNT() ){
34547 zConverted = utf8ToUnicode(zFilename);
34548 }
34549 #ifdef SQLITE_WIN32_HAS_ANSI
34550 else{
34551 zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
34552 }
@@ -34554,117 +34618,135 @@
34554 /* caller will handle out of memory */
34555 return zConverted;
34556 }
34557
34558 /*
34559 ** Maximum pathname length (in bytes) for windows. The MAX_PATH macro is
34560 ** in characters, so we allocate 3 bytes per character assuming worst-case
34561 ** 3-bytes-per-character UTF8.
34562 */
34563 #ifndef SQLITE_WIN32_MAX_PATH
34564 # define SQLITE_WIN32_MAX_PATH (MAX_PATH*3)
34565 #endif
 
 
 
 
34566
34567 /*
34568 ** Create a temporary file name in zBuf. zBuf must be big enough to
34569 ** hold at pVfs->mxPathname characters.
34570 */
34571 static int getTempname(int nBuf, char *zBuf){
34572 static char zChars[] =
34573 "abcdefghijklmnopqrstuvwxyz"
34574 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34575 "0123456789";
34576 size_t i, j;
34577 int nTempPath;
34578 char zTempPath[SQLITE_WIN32_MAX_PATH+2];
34579
34580 /* It's odd to simulate an io-error here, but really this is just
34581 ** using the io-error infrastructure to test that SQLite handles this
34582 ** function failing.
34583 */
34584 SimulateIOError( return SQLITE_IOERR );
34585
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34586 if( sqlite3_temp_directory ){
34587 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
34588 sqlite3_temp_directory);
 
34589 }
34590 #if !SQLITE_OS_WINRT
34591 else if( isNT() ){
34592 char *zMulti;
34593 WCHAR zWidePath[MAX_PATH];
34594 if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
 
 
 
 
 
 
 
34595 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34596 return SQLITE_IOERR_GETTEMPPATH;
34597 }
34598 zMulti = unicodeToUtf8(zWidePath);
34599 if( zMulti ){
34600 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
34601 sqlite3_free(zMulti);
 
34602 }else{
 
 
34603 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34604 return SQLITE_IOERR_NOMEM;
34605 }
34606 }
34607 #ifdef SQLITE_WIN32_HAS_ANSI
34608 else{
34609 char *zUtf8;
34610 char zMbcsPath[SQLITE_WIN32_MAX_PATH];
34611 if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
 
 
 
 
 
 
34612 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34613 return SQLITE_IOERR_GETTEMPPATH;
34614 }
34615 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34616 if( zUtf8 ){
34617 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
34618 sqlite3_free(zUtf8);
34619 }else{
 
34620 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34621 return SQLITE_IOERR_NOMEM;
34622 }
34623 }
34624 #else
34625 else{
34626 /*
34627 ** Compiled without ANSI support and the current operating system
34628 ** is not Windows NT; therefore, just zero the temporary buffer.
34629 */
34630 memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34631 }
34632 #endif /* SQLITE_WIN32_HAS_ANSI */
34633 #else
34634 else{
34635 /*
34636 ** Compiled for WinRT and the sqlite3_temp_directory is not set;
34637 ** therefore, just zero the temporary buffer.
34638 */
34639 memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34640 }
34641 #endif /* !SQLITE_OS_WINRT */
34642
34643 /* Check that the output buffer is large enough for the temporary file
34644 ** name. If it is not, return SQLITE_ERROR.
34645 */
34646 nTempPath = sqlite3Strlen30(zTempPath);
34647
34648 if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
 
34649 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
34650 return SQLITE_ERROR;
34651 }
34652
34653 for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
34654 zTempPath[i] = 0;
34655
34656 sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
34657 "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
34658 zTempPath);
34659 j = sqlite3Strlen30(zBuf);
34660 sqlite3_randomness(15, &zBuf[j]);
34661 for(i=0; i<15; i++, j++){
34662 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34663 }
34664 zBuf[j] = 0;
34665 zBuf[j+1] = 0;
 
34666
34667 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
34668 return SQLITE_OK;
34669 }
34670
@@ -34676,17 +34758,17 @@
34676 static int winIsDir(const void *zConverted){
34677 DWORD attr;
34678 int rc = 0;
34679 DWORD lastErrno;
34680
34681 if( isNT() ){
34682 int cnt = 0;
34683 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34684 memset(&sAttrData, 0, sizeof(sAttrData));
34685 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
34686 GetFileExInfoStandard,
34687 &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
34688 if( !rc ){
34689 return 0; /* Invalid name? */
34690 }
34691 attr = sAttrData.dwFileAttributes;
34692 #if SQLITE_OS_WINCE==0
@@ -34699,11 +34781,11 @@
34699
34700 /*
34701 ** Open a file.
34702 */
34703 static int winOpen(
34704 sqlite3_vfs *pVfs, /* Not used */
34705 const char *zName, /* Name of the file (UTF-8) */
34706 sqlite3_file *id, /* Write the SQLite file handle here */
34707 int flags, /* Open mode flags */
34708 int *pOutFlags /* Status return flags */
34709 ){
@@ -34722,11 +34804,11 @@
34722 int cnt = 0;
34723
34724 /* If argument zPath is a NULL pointer, this function is required to open
34725 ** a temporary file. Use this buffer to store the file name in.
34726 */
34727 char zTmpname[SQLITE_WIN32_MAX_PATH+2]; /* Buffer used to create temp filename */
34728
34729 int rc = SQLITE_OK; /* Function Return Code */
34730 #if !defined(NDEBUG) || SQLITE_OS_WINCE
34731 int eType = flags&0xFFFFFF00; /* Type of file to open */
34732 #endif
@@ -34777,22 +34859,22 @@
34777 assert( pFile!=0 );
34778 memset(pFile, 0, sizeof(winFile));
34779 pFile->h = INVALID_HANDLE_VALUE;
34780
34781 #if SQLITE_OS_WINRT
34782 if( !sqlite3_temp_directory ){
34783 sqlite3_log(SQLITE_ERROR,
34784 "sqlite3_temp_directory variable should be set for WinRT");
34785 }
34786 #endif
34787
34788 /* If the second argument to this function is NULL, generate a
34789 ** temporary file name to use
34790 */
34791 if( !zUtf8Name ){
34792 assert(isDelete && !isOpenJournal);
34793 rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
34794 if( rc!=SQLITE_OK ){
34795 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
34796 return rc;
34797 }
34798 zUtf8Name = zTmpname;
@@ -34801,21 +34883,23 @@
34801 /* Database filenames are double-zero terminated if they are not
34802 ** URIs with parameters. Hence, they can always be passed into
34803 ** sqlite3_uri_parameter().
34804 */
34805 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
34806 zUtf8Name[strlen(zUtf8Name)+1]==0 );
34807
34808 /* Convert the filename to the system encoding. */
34809 zConverted = convertUtf8Filename(zUtf8Name);
34810 if( zConverted==0 ){
 
34811 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
34812 return SQLITE_IOERR_NOMEM;
34813 }
34814
34815 if( winIsDir(zConverted) ){
34816 sqlite3_free(zConverted);
 
34817 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
34818 return SQLITE_CANTOPEN_ISDIR;
34819 }
34820
34821 if( isReadWrite ){
@@ -34858,11 +34942,11 @@
34858 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
34859 #if SQLITE_OS_WINCE
34860 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
34861 #endif
34862
34863 if( isNT() ){
34864 #if SQLITE_OS_WINRT
34865 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
34866 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
34867 extendedParameters.dwFileAttributes =
34868 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34873,21 +34957,21 @@
34873 while( (h = osCreateFile2((LPCWSTR)zConverted,
34874 dwDesiredAccess,
34875 dwShareMode,
34876 dwCreationDisposition,
34877 &extendedParameters))==INVALID_HANDLE_VALUE &&
34878 retryIoerr(&cnt, &lastErrno) ){
34879 /* Noop */
34880 }
34881 #else
34882 while( (h = osCreateFileW((LPCWSTR)zConverted,
34883 dwDesiredAccess,
34884 dwShareMode, NULL,
34885 dwCreationDisposition,
34886 dwFlagsAndAttributes,
34887 NULL))==INVALID_HANDLE_VALUE &&
34888 retryIoerr(&cnt, &lastErrno) ){
34889 /* Noop */
34890 }
34891 #endif
34892 }
34893 #ifdef SQLITE_WIN32_HAS_ANSI
@@ -34896,24 +34980,25 @@
34896 dwDesiredAccess,
34897 dwShareMode, NULL,
34898 dwCreationDisposition,
34899 dwFlagsAndAttributes,
34900 NULL))==INVALID_HANDLE_VALUE &&
34901 retryIoerr(&cnt, &lastErrno) ){
34902 /* Noop */
34903 }
34904 }
34905 #endif
34906 logIoerr(cnt);
34907
34908 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
34909 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
34910
34911 if( h==INVALID_HANDLE_VALUE ){
34912 pFile->lastErrno = lastErrno;
34913 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
34914 sqlite3_free(zConverted);
 
34915 if( isReadWrite && !isExclusive ){
34916 return winOpen(pVfs, zName, id,
34917 ((flags|SQLITE_OPEN_READONLY) &
34918 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
34919 pOutFlags);
@@ -34938,19 +35023,21 @@
34938 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
34939 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
34940 ){
34941 osCloseHandle(h);
34942 sqlite3_free(zConverted);
 
34943 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
34944 return rc;
34945 }
34946 if( isTemp ){
34947 pFile->zDeleteOnClose = zConverted;
34948 }else
34949 #endif
34950 {
34951 sqlite3_free(zConverted);
 
34952 }
34953
34954 pFile->pMethod = &winIoMethod;
34955 pFile->pVfs = pVfs;
34956 pFile->h = h;
@@ -35000,15 +35087,16 @@
35000 UNUSED_PARAMETER(syncDir);
35001
35002 SimulateIOError(return SQLITE_IOERR_DELETE);
35003 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
35004
35005 zConverted = convertUtf8Filename(zFilename);
35006 if( zConverted==0 ){
 
35007 return SQLITE_IOERR_NOMEM;
35008 }
35009 if( isNT() ){
35010 do {
35011 #if SQLITE_OS_WINRT
35012 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35013 memset(&sAttrData, 0, sizeof(sAttrData));
35014 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35043,11 +35131,11 @@
35043 }
35044 if ( osDeleteFileW(zConverted) ){
35045 rc = SQLITE_OK; /* Deleted OK. */
35046 break;
35047 }
35048 if ( !retryIoerr(&cnt, &lastErrno) ){
35049 rc = SQLITE_ERROR; /* No more retries. */
35050 break;
35051 }
35052 } while(1);
35053 }
@@ -35071,11 +35159,11 @@
35071 }
35072 if ( osDeleteFileA(zConverted) ){
35073 rc = SQLITE_OK; /* Deleted OK. */
35074 break;
35075 }
35076 if ( !retryIoerr(&cnt, &lastErrno) ){
35077 rc = SQLITE_ERROR; /* No more retries. */
35078 break;
35079 }
35080 } while(1);
35081 }
@@ -35082,11 +35170,11 @@
35082 #endif
35083 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
35084 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35085 "winDelete", zFilename);
35086 }else{
35087 logIoerr(cnt);
35088 }
35089 sqlite3_free(zConverted);
35090 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
35091 return rc;
35092 }
@@ -35108,22 +35196,22 @@
35108
35109 SimulateIOError( return SQLITE_IOERR_ACCESS; );
35110 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
35111 zFilename, flags, pResOut));
35112
35113 zConverted = convertUtf8Filename(zFilename);
35114 if( zConverted==0 ){
35115 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35116 return SQLITE_IOERR_NOMEM;
35117 }
35118 if( isNT() ){
35119 int cnt = 0;
35120 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35121 memset(&sAttrData, 0, sizeof(sAttrData));
35122 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35123 GetFileExInfoStandard,
35124 &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
35125 if( rc ){
35126 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
35127 ** as if it does not exist.
35128 */
35129 if( flags==SQLITE_ACCESS_EXISTS
@@ -35132,11 +35220,11 @@
35132 attr = INVALID_FILE_ATTRIBUTES;
35133 }else{
35134 attr = sAttrData.dwFileAttributes;
35135 }
35136 }else{
35137 logIoerr(cnt);
35138 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
35139 winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
35140 sqlite3_free(zConverted);
35141 return SQLITE_IOERR_ACCESS;
35142 }else{
@@ -35183,11 +35271,11 @@
35183 ** a legal UNC name, a volume relative path, or an absolute path name in the
35184 ** "Unix" format on Windows. There is no easy way to differentiate between
35185 ** the final two cases; therefore, we return the safer return value of TRUE
35186 ** so that callers of this function will simply use it verbatim.
35187 */
35188 if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
35189 return TRUE;
35190 }
35191
35192 /*
35193 ** If the path name starts with a letter and a colon it is either a volume
@@ -35219,28 +35307,33 @@
35219 ){
35220
35221 #if defined(__CYGWIN__)
35222 SimulateIOError( return SQLITE_ERROR );
35223 UNUSED_PARAMETER(nFull);
35224 assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
35225 assert( nFull>=pVfs->mxPathname );
35226 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
35227 /*
35228 ** NOTE: We are dealing with a relative path name and the data
35229 ** directory has been set. Therefore, use it as the basis
35230 ** for converting the relative path name to an absolute
35231 ** one by prepending the data directory and a slash.
35232 */
35233 char zOut[SQLITE_WIN32_MAX_PATH+1];
 
 
 
 
35234 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35235 SQLITE_WIN32_MAX_PATH+1)<0 ){
35236 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35237 zRelative);
 
35238 return SQLITE_CANTOPEN_FULLPATH;
35239 }
35240 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35241 sqlite3_data_directory, zOut);
 
35242 }else{
35243 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
35244 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35245 zRelative);
35246 return SQLITE_CANTOPEN_FULLPATH;
@@ -35258,12 +35351,12 @@
35258 ** NOTE: We are dealing with a relative path name and the data
35259 ** directory has been set. Therefore, use it as the basis
35260 ** for converting the relative path name to an absolute
35261 ** one by prepending the data directory and a backslash.
35262 */
35263 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35264 sqlite3_data_directory, zRelative);
35265 }else{
35266 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
35267 }
35268 return SQLITE_OK;
35269 #endif
@@ -35291,19 +35384,19 @@
35291 ** NOTE: We are dealing with a relative path name and the data
35292 ** directory has been set. Therefore, use it as the basis
35293 ** for converting the relative path name to an absolute
35294 ** one by prepending the data directory and a backslash.
35295 */
35296 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35297 sqlite3_data_directory, zRelative);
35298 return SQLITE_OK;
35299 }
35300 zConverted = convertUtf8Filename(zRelative);
35301 if( zConverted==0 ){
35302 return SQLITE_IOERR_NOMEM;
35303 }
35304 if( isNT() ){
35305 LPWSTR zTemp;
35306 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
35307 if( nByte==0 ){
35308 winLogError(SQLITE_ERROR, osGetLastError(),
35309 "GetFullPathNameW1", zConverted);
@@ -35323,11 +35416,11 @@
35323 sqlite3_free(zConverted);
35324 sqlite3_free(zTemp);
35325 return SQLITE_CANTOPEN_FULLPATH;
35326 }
35327 sqlite3_free(zConverted);
35328 zOut = unicodeToUtf8(zTemp);
35329 sqlite3_free(zTemp);
35330 }
35331 #ifdef SQLITE_WIN32_HAS_ANSI
35332 else{
35333 char *zTemp;
@@ -35376,16 +35469,16 @@
35376 ** Interfaces for opening a shared library, finding entry points
35377 ** within the shared library, and closing the shared library.
35378 */
35379 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
35380 HANDLE h;
35381 void *zConverted = convertUtf8Filename(zFilename);
35382 UNUSED_PARAMETER(pVfs);
35383 if( zConverted==0 ){
35384 return 0;
35385 }
35386 if( isNT() ){
35387 #if SQLITE_OS_WINRT
35388 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
35389 #else
35390 h = osLoadLibraryW((LPCWSTR)zConverted);
35391 #endif
@@ -35398,11 +35491,11 @@
35398 sqlite3_free(zConverted);
35399 return (void*)h;
35400 }
35401 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
35402 UNUSED_PARAMETER(pVfs);
35403 getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35404 }
35405 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
35406 UNUSED_PARAMETER(pVfs);
35407 return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
35408 }
@@ -35574,21 +35667,21 @@
35574 ** by sqlite into the error message available to the user using
35575 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
35576 */
35577 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
35578 UNUSED_PARAMETER(pVfs);
35579 return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
35580 }
35581
35582 /*
35583 ** Initialize and deinitialize the operating system interface.
35584 */
35585 SQLITE_API int sqlite3_os_init(void){
35586 static sqlite3_vfs winVfs = {
35587 3, /* iVersion */
35588 sizeof(winFile), /* szOsFile */
35589 SQLITE_WIN32_MAX_PATH, /* mxPathname */
35590 0, /* pNext */
35591 "win32", /* zName */
35592 0, /* pAppData */
35593 winOpen, /* xOpen */
35594 winDelete, /* xDelete */
@@ -35605,10 +35698,36 @@
35605 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35606 winSetSystemCall, /* xSetSystemCall */
35607 winGetSystemCall, /* xGetSystemCall */
35608 winNextSystemCall, /* xNextSystemCall */
35609 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35610
35611 /* Double-check that the aSyscall[] array has been constructed
35612 ** correctly. See ticket [bb3a86e890c8e96ab] */
35613 assert( ArraySize(aSyscall)==74 );
35614
@@ -35621,10 +35740,15 @@
35621 #endif
35622 assert( winSysInfo.dwAllocationGranularity>0 );
35623 assert( winSysInfo.dwPageSize>0 );
35624
35625 sqlite3_vfs_register(&winVfs, 1);
 
 
 
 
 
35626 return SQLITE_OK;
35627 }
35628
35629 SQLITE_API int sqlite3_os_end(void){
35630 #if SQLITE_OS_WINRT
@@ -37434,10 +37558,11 @@
37434 }
37435
37436 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
37437 goto fetch_out;
37438 }
 
37439
37440 /* Step 4. Try to recycle a page. */
37441 if( pCache->bPurgeable && pGroup->pLruTail && (
37442 (pCache->nPage+1>=pCache->nMax)
37443 || pGroup->nCurrentPage>=pGroup->nMaxPage
@@ -38794,10 +38919,17 @@
38794 #ifndef SQLITE_OMIT_WAL
38795 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
38796 #endif
38797 };
38798
 
 
 
 
 
 
 
38799 /*
38800 ** A open page cache is an instance of struct Pager. A description of
38801 ** some of the more important member variables follows:
38802 **
38803 ** eState
@@ -38860,23 +38992,25 @@
38860 ** The flag is cleared as soon as the journal file is finalized (either
38861 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
38862 ** journal file from being successfully finalized, the setMaster flag
38863 ** is cleared anyway (and the pager will move to ERROR state).
38864 **
38865 ** doNotSpill, doNotSyncSpill
38866 **
38867 ** These two boolean variables control the behavior of cache-spills
38868 ** (calls made by the pcache module to the pagerStress() routine to
38869 ** write cached data to the file-system in order to free up memory).
38870 **
38871 ** When doNotSpill is non-zero, writing to the database from pagerStress()
38872 ** is disabled altogether. This is done in a very obscure case that
 
38873 ** comes up during savepoint rollback that requires the pcache module
38874 ** to allocate a new page to prevent the journal file from being written
38875 ** while it is being traversed by code in pager_playback().
 
38876 **
38877 ** If doNotSyncSpill is non-zero, writing to the database from pagerStress()
38878 ** is permitted, but syncing the journal file is not. This flag is set
38879 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
38880 ** the database page-size in order to prevent a journal sync from happening
38881 ** in between the journalling of two pages on the same sector.
38882 **
@@ -38976,11 +39110,10 @@
38976 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
38977 u8 eLock; /* Current lock held on database file */
38978 u8 changeCountDone; /* Set after incrementing the change-counter */
38979 u8 setMaster; /* True if a m-j name has been written to jrnl */
38980 u8 doNotSpill; /* Do not spill the cache when non-zero */
38981 u8 doNotSyncSpill; /* Do not do a spill that requires jrnl sync */
38982 u8 subjInMemory; /* True to use in-memory sub-journals */
38983 Pgno dbSize; /* Number of pages in the database */
38984 Pgno dbOrigSize; /* dbSize before the current transaction */
38985 Pgno dbFileSize; /* Number of pages in the database file */
38986 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
@@ -39355,17 +39488,21 @@
39355 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
39356 ** * The bit corresponding to the page-number is not set in
39357 ** PagerSavepoint.pInSavepoint.
39358 */
39359 static int subjRequiresPage(PgHdr *pPg){
39360 Pgno pgno = pPg->pgno;
39361 Pager *pPager = pPg->pPager;
 
 
39362 int i;
39363 for(i=0; i<pPager->nSavepoint; i++){
39364 PagerSavepoint *p = &pPager->aSavepoint[i];
39365 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
39366 return 1;
 
 
 
39367 }
39368 }
39369 return 0;
39370 }
39371
@@ -40636,15 +40773,15 @@
40636 ** the data just read from the sub-journal. Mark the page as dirty
40637 ** and if the pager requires a journal-sync, then mark the page as
40638 ** requiring a journal-sync before it is written.
40639 */
40640 assert( isSavepnt );
40641 assert( pPager->doNotSpill==0 );
40642 pPager->doNotSpill++;
40643 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
40644 assert( pPager->doNotSpill==1 );
40645 pPager->doNotSpill--;
40646 if( rc!=SQLITE_OK ) return rc;
40647 pPg->flags &= ~PGHDR_NEED_READ;
40648 sqlite3PcacheMakeDirty(pPg);
40649 }
40650 if( pPg ){
@@ -41207,16 +41344,10 @@
41207 int pgsz = pPager->pageSize; /* Number of bytes to read */
41208
41209 assert( pPager->eState>=PAGER_READER && !MEMDB );
41210 assert( isOpen(pPager->fd) );
41211
41212 if( NEVER(!isOpen(pPager->fd)) ){
41213 assert( pPager->tempFile );
41214 memset(pPg->pData, 0, pPager->pageSize);
41215 return SQLITE_OK;
41216 }
41217
41218 #ifndef SQLITE_OMIT_WAL
41219 if( iFrame ){
41220 /* Try to pull the page from the write-ahead log. */
41221 rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
41222 }else
@@ -41745,13 +41876,16 @@
41745 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41746 sqlite3PcacheShrink(pPager->pPCache);
41747 }
41748
41749 /*
41750 ** Adjust the robustness of the database to damage due to OS crashes
41751 ** or power failures by changing the number of syncs()s when writing
41752 ** the rollback journal. There are three levels:
 
 
 
41753 **
41754 ** OFF sqlite3OsSync() is never called. This is the default
41755 ** for temporary and transient files.
41756 **
41757 ** NORMAL The journal is synced once before writes begin on the
@@ -41788,26 +41922,25 @@
41788 **
41789 ** Numeric values associated with these states are OFF==1, NORMAL=2,
41790 ** and FULL=3.
41791 */
41792 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
41793 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(
41794 Pager *pPager, /* The pager to set safety level for */
41795 int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
41796 int bFullFsync, /* PRAGMA fullfsync */
41797 int bCkptFullFsync /* PRAGMA checkpoint_fullfsync */
41798 ){
 
41799 assert( level>=1 && level<=3 );
41800 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
41801 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
41802 if( pPager->noSync ){
41803 pPager->syncFlags = 0;
41804 pPager->ckptSyncFlags = 0;
41805 }else if( bFullFsync ){
41806 pPager->syncFlags = SQLITE_SYNC_FULL;
41807 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41808 }else if( bCkptFullFsync ){
41809 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41810 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41811 }else{
41812 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41813 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
@@ -41814,10 +41947,15 @@
41814 }
41815 pPager->walSyncFlags = pPager->syncFlags;
41816 if( pPager->fullSync ){
41817 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41818 }
 
 
 
 
 
41819 }
41820 #endif
41821
41822 /*
41823 ** The following global variable is incremented whenever the library
@@ -42714,28 +42852,34 @@
42714 int rc = SQLITE_OK;
42715
42716 assert( pPg->pPager==pPager );
42717 assert( pPg->flags&PGHDR_DIRTY );
42718
42719 /* The doNotSyncSpill flag is set during times when doing a sync of
42720 ** journal (and adding a new header) is not allowed. This occurs
42721 ** during calls to sqlite3PagerWrite() while trying to journal multiple
42722 ** pages belonging to the same sector.
42723 **
42724 ** The doNotSpill flag inhibits all cache spilling regardless of whether
42725 ** or not a sync is required. This is set during a rollback.
 
42726 **
42727 ** Spilling is also prohibited when in an error state since that could
42728 ** lead to database corruption. In the current implementaton it
42729 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
42730 ** while in the error state, hence it is impossible for this routine to
42731 ** be called in the error state. Nevertheless, we include a NEVER()
42732 ** test for the error state as a safeguard against future changes.
42733 */
42734 if( NEVER(pPager->errCode) ) return SQLITE_OK;
42735 if( pPager->doNotSpill ) return SQLITE_OK;
42736 if( pPager->doNotSyncSpill && (pPg->flags & PGHDR_NEED_SYNC)!=0 ){
 
 
 
 
 
42737 return SQLITE_OK;
42738 }
42739
42740 pPg->pDirty = 0;
42741 if( pagerUseWal(pPager) ){
@@ -43553,23 +43697,23 @@
43553 */
43554 SQLITE_PRIVATE int sqlite3PagerAcquire(
43555 Pager *pPager, /* The pager open on the database file */
43556 Pgno pgno, /* Page number to fetch */
43557 DbPage **ppPage, /* Write a pointer to the page here */
43558 int flags /* PAGER_ACQUIRE_XXX flags */
43559 ){
43560 int rc = SQLITE_OK;
43561 PgHdr *pPg = 0;
43562 u32 iFrame = 0; /* Frame to read from WAL file */
43563 const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);
43564
43565 /* It is acceptable to use a read-only (mmap) page for any page except
43566 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
43567 ** flag was specified by the caller. And so long as the db is not a
43568 ** temporary or in-memory database. */
43569 const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
43570 && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
43571 #ifdef SQLITE_HAS_CODEC
43572 && pPager->xCodec==0
43573 #endif
43574 );
43575
@@ -44085,17 +44229,17 @@
44085 Pgno pg1; /* First page of the sector pPg is located on. */
44086 int nPage = 0; /* Number of pages starting at pg1 to journal */
44087 int ii; /* Loop counter */
44088 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
44089
44090 /* Set the doNotSyncSpill flag to 1. This is because we cannot allow
44091 ** a journal header to be written between the pages journaled by
44092 ** this function.
44093 */
44094 assert( !MEMDB );
44095 assert( pPager->doNotSyncSpill==0 );
44096 pPager->doNotSyncSpill++;
44097
44098 /* This trick assumes that both the page-size and sector-size are
44099 ** an integer power of 2. It sets variable pg1 to the identifier
44100 ** of the first page of the sector pPg is located on.
44101 */
@@ -44150,12 +44294,12 @@
44150 sqlite3PagerUnref(pPage);
44151 }
44152 }
44153 }
44154
44155 assert( pPager->doNotSyncSpill==1 );
44156 pPager->doNotSyncSpill--;
44157 }else{
44158 rc = pager_write(pDbPage);
44159 }
44160 return rc;
44161 }
@@ -49147,18 +49291,23 @@
49147 };
49148
49149 /*
49150 ** Potential values for BtCursor.eState.
49151 **
49152 ** CURSOR_VALID:
49153 ** Cursor points to a valid entry. getPayload() etc. may be called.
49154 **
49155 ** CURSOR_INVALID:
49156 ** Cursor does not point to a valid entry. This can happen (for example)
49157 ** because the table is empty or because BtreeCursorFirst() has not been
49158 ** called.
49159 **
 
 
 
 
 
 
 
 
49160 ** CURSOR_REQUIRESEEK:
49161 ** The table that this cursor was opened on still exists, but has been
49162 ** modified since the cursor was last used. The cursor position is saved
49163 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
49164 ** this state, restoreCursorPosition() can be called to attempt to
@@ -49171,12 +49320,13 @@
49171 ** Do nothing else with this cursor. Any attempt to use the cursor
49172 ** should return the error code stored in BtCursor.skip
49173 */
49174 #define CURSOR_INVALID 0
49175 #define CURSOR_VALID 1
49176 #define CURSOR_REQUIRESEEK 2
49177 #define CURSOR_FAULT 3
 
49178
49179 /*
49180 ** The database page the PENDING_BYTE occupies. This page is never used.
49181 */
49182 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
@@ -50286,10 +50436,13 @@
50286 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
50287 if( rc==SQLITE_OK ){
50288 sqlite3_free(pCur->pKey);
50289 pCur->pKey = 0;
50290 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
 
 
 
50291 }
50292 return rc;
50293 }
50294
50295 #define restoreCursorPosition(p) \
@@ -50311,11 +50464,11 @@
50311 rc = restoreCursorPosition(pCur);
50312 if( rc ){
50313 *pHasMoved = 1;
50314 return rc;
50315 }
50316 if( pCur->eState!=CURSOR_VALID || pCur->skipNext!=0 ){
50317 *pHasMoved = 1;
50318 }else{
50319 *pHasMoved = 0;
50320 }
50321 return SQLITE_OK;
@@ -50499,11 +50652,12 @@
50499 assert( pPage->leaf==0 || pPage->leaf==1 );
50500 n = pPage->childPtrSize;
50501 assert( n==4-4*pPage->leaf );
50502 if( pPage->intKey ){
50503 if( pPage->hasData ){
50504 n += getVarint32(&pCell[n], nPayload);
 
50505 }else{
50506 nPayload = 0;
50507 }
50508 n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
50509 pInfo->nData = nPayload;
@@ -51143,19 +51297,16 @@
51143 */
51144 static int btreeGetPage(
51145 BtShared *pBt, /* The btree */
51146 Pgno pgno, /* Number of the page to fetch */
51147 MemPage **ppPage, /* Return the page in this parameter */
51148 int noContent, /* Do not load page content if true */
51149 int bReadonly /* True if a read-only (mmap) page is ok */
51150 ){
51151 int rc;
51152 DbPage *pDbPage;
51153 int flags = (noContent ? PAGER_ACQUIRE_NOCONTENT : 0)
51154 | (bReadonly ? PAGER_ACQUIRE_READONLY : 0);
51155
51156 assert( noContent==0 || bReadonly==0 );
51157 assert( sqlite3_mutex_held(pBt->mutex) );
51158 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
51159 if( rc ) return rc;
51160 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
51161 return SQLITE_OK;
@@ -51199,19 +51350,20 @@
51199 */
51200 static int getAndInitPage(
51201 BtShared *pBt, /* The database file */
51202 Pgno pgno, /* Number of the page to get */
51203 MemPage **ppPage, /* Write the page pointer here */
51204 int bReadonly /* True if a read-only (mmap) page is ok */
51205 ){
51206 int rc;
51207 assert( sqlite3_mutex_held(pBt->mutex) );
 
51208
51209 if( pgno>btreePagecount(pBt) ){
51210 rc = SQLITE_CORRUPT_BKPT;
51211 }else{
51212 rc = btreeGetPage(pBt, pgno, ppPage, 0, bReadonly);
51213 if( rc==SQLITE_OK ){
51214 rc = btreeInitPage(*ppPage);
51215 if( rc!=SQLITE_OK ){
51216 releasePage(*ppPage);
51217 }
@@ -51727,21 +51879,18 @@
51727 ** there is a high probability of damage) Level 2 is the default. There
51728 ** is a very low but non-zero probability of damage. Level 3 reduces the
51729 ** probability of damage to near zero but with a write performance reduction.
51730 */
51731 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
51732 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(
51733 Btree *p, /* The btree to set the safety level on */
51734 int level, /* PRAGMA synchronous. 1=OFF, 2=NORMAL, 3=FULL */
51735 int fullSync, /* PRAGMA fullfsync. */
51736 int ckptFullSync /* PRAGMA checkpoint_fullfync */
51737 ){
51738 BtShared *pBt = p->pBt;
51739 assert( sqlite3_mutex_held(p->db->mutex) );
51740 assert( level>=1 && level<=3 );
51741 sqlite3BtreeEnter(p);
51742 sqlite3PagerSetSafetyLevel(pBt->pPager, level, fullSync, ckptFullSync);
51743 sqlite3BtreeLeave(p);
51744 return SQLITE_OK;
51745 }
51746 #endif
51747
@@ -51943,11 +52092,11 @@
51943
51944 assert( sqlite3_mutex_held(pBt->mutex) );
51945 assert( pBt->pPage1==0 );
51946 rc = sqlite3PagerSharedLock(pBt->pPager);
51947 if( rc!=SQLITE_OK ) return rc;
51948 rc = btreeGetPage(pBt, 1, &pPage1, 0, 0);
51949 if( rc!=SQLITE_OK ) return rc;
51950
51951 /* Do some checking to help insure the file we opened really is
51952 ** a valid database file.
51953 */
@@ -52071,10 +52220,11 @@
52071 pBt->max1bytePayload = (u8)pBt->maxLocal;
52072 }
52073 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
52074 pBt->pPage1 = pPage1;
52075 pBt->nPage = nPage;
 
52076 return SQLITE_OK;
52077
52078 page1_init_failed:
52079 releasePage(pPage1);
52080 pBt->pPage1 = 0;
@@ -52525,11 +52675,11 @@
52525 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
52526 ** that it points at iFreePage. Also fix the pointer map entry for
52527 ** iPtrPage.
52528 */
52529 if( eType!=PTRMAP_ROOTPAGE ){
52530 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0, 0);
52531 if( rc!=SQLITE_OK ){
52532 return rc;
52533 }
52534 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
52535 if( rc!=SQLITE_OK ){
@@ -52609,11 +52759,11 @@
52609 Pgno iFreePg; /* Index of free page to move pLastPg to */
52610 MemPage *pLastPg;
52611 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
52612 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
52613
52614 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0, 0);
52615 if( rc!=SQLITE_OK ){
52616 return rc;
52617 }
52618
52619 /* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -53008,11 +53158,11 @@
53008 }
53009
53010 /* The rollback may have destroyed the pPage1->aData value. So
53011 ** call btreeGetPage() on page 1 again to make
53012 ** sure pPage1->aData is set correctly. */
53013 if( btreeGetPage(pBt, 1, &pPage1, 0, 0)==SQLITE_OK ){
53014 int nPage = get4byte(28+(u8*)pPage1->aData);
53015 testcase( nPage==0 );
53016 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
53017 testcase( pBt->nPage!=nPage );
53018 pBt->nPage = nPage;
@@ -53443,11 +53593,11 @@
53443 }
53444 #endif
53445
53446 assert( next==0 || rc==SQLITE_DONE );
53447 if( rc==SQLITE_OK ){
53448 rc = btreeGetPage(pBt, ovfl, &pPage, 0, (ppPage==0));
53449 assert( rc==SQLITE_OK || pPage==0 );
53450 if( rc==SQLITE_OK ){
53451 next = get4byte(pPage->aData);
53452 }
53453 }
@@ -53665,11 +53815,11 @@
53665 #endif
53666
53667 {
53668 DbPage *pDbPage;
53669 rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
53670 (eOp==0 ? PAGER_ACQUIRE_READONLY : 0)
53671 );
53672 if( rc==SQLITE_OK ){
53673 aPayload = sqlite3PagerGetData(pDbPage);
53674 nextPage = get4byte(aPayload);
53675 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
@@ -53849,11 +53999,12 @@
53849 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
53850 assert( pCur->iPage>=0 );
53851 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
53852 return SQLITE_CORRUPT_BKPT;
53853 }
53854 rc = getAndInitPage(pBt, newPgno, &pNewPage, (pCur->wrFlag==0));
 
53855 if( rc ) return rc;
53856 pCur->apPage[i+1] = pNewPage;
53857 pCur->aiIdx[i+1] = 0;
53858 pCur->iPage++;
53859
@@ -53966,11 +54117,12 @@
53966 pCur->iPage = 0;
53967 }else if( pCur->pgnoRoot==0 ){
53968 pCur->eState = CURSOR_INVALID;
53969 return SQLITE_OK;
53970 }else{
53971 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0], pCur->wrFlag==0);
 
53972 if( rc!=SQLITE_OK ){
53973 pCur->eState = CURSOR_INVALID;
53974 return rc;
53975 }
53976 pCur->iPage = 0;
@@ -54361,25 +54513,33 @@
54361 int rc;
54362 int idx;
54363 MemPage *pPage;
54364
54365 assert( cursorHoldsMutex(pCur) );
54366 rc = restoreCursorPosition(pCur);
54367 if( rc!=SQLITE_OK ){
54368 return rc;
54369 }
54370 assert( pRes!=0 );
54371 if( CURSOR_INVALID==pCur->eState ){
54372 *pRes = 1;
54373 return SQLITE_OK;
54374 }
54375 if( pCur->skipNext>0 ){
54376 pCur->skipNext = 0;
54377 *pRes = 0;
54378 return SQLITE_OK;
54379 }
54380 pCur->skipNext = 0;
 
 
 
 
 
 
 
 
 
 
 
 
54381
54382 pPage = pCur->apPage[pCur->iPage];
54383 idx = ++pCur->aiIdx[pCur->iPage];
54384 assert( pPage->isInit );
54385
@@ -54393,11 +54553,14 @@
54393 pCur->info.nSize = 0;
54394 pCur->validNKey = 0;
54395 if( idx>=pPage->nCell ){
54396 if( !pPage->leaf ){
54397 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
54398 if( rc ) return rc;
 
 
 
54399 rc = moveToLeftmost(pCur);
54400 *pRes = 0;
54401 return rc;
54402 }
54403 do{
@@ -54435,32 +54598,44 @@
54435 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
54436 int rc;
54437 MemPage *pPage;
54438
54439 assert( cursorHoldsMutex(pCur) );
54440 rc = restoreCursorPosition(pCur);
54441 if( rc!=SQLITE_OK ){
54442 return rc;
54443 }
54444 pCur->atLast = 0;
54445 if( CURSOR_INVALID==pCur->eState ){
54446 *pRes = 1;
54447 return SQLITE_OK;
54448 }
54449 if( pCur->skipNext<0 ){
54450 pCur->skipNext = 0;
54451 *pRes = 0;
54452 return SQLITE_OK;
54453 }
54454 pCur->skipNext = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
54455
54456 pPage = pCur->apPage[pCur->iPage];
54457 assert( pPage->isInit );
54458 if( !pPage->leaf ){
54459 int idx = pCur->aiIdx[pCur->iPage];
54460 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
54461 if( rc ){
 
54462 return rc;
54463 }
54464 rc = moveToRightmost(pCur);
54465 }else{
54466 while( pCur->aiIdx[pCur->iPage]==0 ){
@@ -54580,11 +54755,11 @@
54580 }
54581 testcase( iTrunk==mxPage );
54582 if( iTrunk>mxPage ){
54583 rc = SQLITE_CORRUPT_BKPT;
54584 }else{
54585 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
54586 }
54587 if( rc ){
54588 pTrunk = 0;
54589 goto end_allocate_page;
54590 }
@@ -54644,11 +54819,11 @@
54644 if( iNewTrunk>mxPage ){
54645 rc = SQLITE_CORRUPT_BKPT;
54646 goto end_allocate_page;
54647 }
54648 testcase( iNewTrunk==mxPage );
54649 rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0, 0);
54650 if( rc!=SQLITE_OK ){
54651 goto end_allocate_page;
54652 }
54653 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
54654 if( rc!=SQLITE_OK ){
@@ -54723,12 +54898,12 @@
54723 if( rc ) goto end_allocate_page;
54724 if( closest<k-1 ){
54725 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
54726 }
54727 put4byte(&aData[4], k-1);
54728 noContent = !btreeGetHasContent(pBt, *pPgno);
54729 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent, 0);
54730 if( rc==SQLITE_OK ){
54731 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54732 if( rc!=SQLITE_OK ){
54733 releasePage(*ppPage);
54734 }
@@ -54756,11 +54931,11 @@
54756 ** content for any page that really does lie past the end of the database
54757 ** file on disk. So the effects of disabling the no-content optimization
54758 ** here are confined to those pages that lie between the end of the
54759 ** database image and the end of the database file.
54760 */
54761 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate));
54762
54763 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
54764 if( rc ) return rc;
54765 pBt->nPage++;
54766 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -54772,11 +54947,11 @@
54772 ** becomes a new pointer-map page, the second is used by the caller.
54773 */
54774 MemPage *pPg = 0;
54775 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
54776 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
54777 rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent, 0);
54778 if( rc==SQLITE_OK ){
54779 rc = sqlite3PagerWrite(pPg->pDbPage);
54780 releasePage(pPg);
54781 }
54782 if( rc ) return rc;
@@ -54786,11 +54961,11 @@
54786 #endif
54787 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
54788 *pPgno = pBt->nPage;
54789
54790 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
54791 rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent, 0);
54792 if( rc ) return rc;
54793 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54794 if( rc!=SQLITE_OK ){
54795 releasePage(*ppPage);
54796 }
@@ -54854,11 +55029,11 @@
54854
54855 if( pBt->btsFlags & BTS_SECURE_DELETE ){
54856 /* If the secure_delete option is enabled, then
54857 ** always fully overwrite deleted information with zeros.
54858 */
54859 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0, 0))!=0) )
54860 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
54861 ){
54862 goto freepage_out;
54863 }
54864 memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -54881,11 +55056,11 @@
54881 */
54882 if( nFree!=0 ){
54883 u32 nLeaf; /* Initial number of leaf cells on trunk page */
54884
54885 iTrunk = get4byte(&pPage1->aData[32]);
54886 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
54887 if( rc!=SQLITE_OK ){
54888 goto freepage_out;
54889 }
54890
54891 nLeaf = get4byte(&pTrunk->aData[4]);
@@ -54927,11 +55102,11 @@
54927 ** the page being freed as a leaf page of the first trunk in the free-list.
54928 ** Possibly because the free-list is empty, or possibly because the
54929 ** first trunk in the free-list is full. Either way, the page being freed
54930 ** will become the new first trunk page in the free-list.
54931 */
54932 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0, 0)) ){
54933 goto freepage_out;
54934 }
54935 rc = sqlite3PagerWrite(pPage->pDbPage);
54936 if( rc!=SQLITE_OK ){
54937 goto freepage_out;
@@ -56826,11 +57001,11 @@
56826 if( rc!=SQLITE_OK ){
56827 return rc;
56828 }
56829
56830 /* Move the page currently at pgnoRoot to pgnoMove. */
56831 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
56832 if( rc!=SQLITE_OK ){
56833 return rc;
56834 }
56835 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
56836 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -56847,11 +57022,11 @@
56847
56848 /* Obtain the page at pgnoRoot */
56849 if( rc!=SQLITE_OK ){
56850 return rc;
56851 }
56852 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
56853 if( rc!=SQLITE_OK ){
56854 return rc;
56855 }
56856 rc = sqlite3PagerWrite(pRoot->pDbPage);
56857 if( rc!=SQLITE_OK ){
@@ -57025,11 +57200,11 @@
57025 if( NEVER(pBt->pCursor) ){
57026 sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
57027 return SQLITE_LOCKED_SHAREDCACHE;
57028 }
57029
57030 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0, 0);
57031 if( rc ) return rc;
57032 rc = sqlite3BtreeClearTable(p, iTable, 0);
57033 if( rc ){
57034 releasePage(pPage);
57035 return rc;
@@ -57060,21 +57235,21 @@
57060 ** number in the database. So move the page that does into the
57061 ** gap left by the deleted root-page.
57062 */
57063 MemPage *pMove;
57064 releasePage(pPage);
57065 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
57066 if( rc!=SQLITE_OK ){
57067 return rc;
57068 }
57069 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
57070 releasePage(pMove);
57071 if( rc!=SQLITE_OK ){
57072 return rc;
57073 }
57074 pMove = 0;
57075 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
57076 freePage(pMove, &rc);
57077 releasePage(pMove);
57078 if( rc!=SQLITE_OK ){
57079 return rc;
57080 }
@@ -57285,11 +57460,11 @@
57285 if( zMsg1 ){
57286 sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
57287 }
57288 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
57289 va_end(ap);
57290 if( pCheck->errMsg.mallocFailed ){
57291 pCheck->mallocFailed = 1;
57292 }
57293 }
57294 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
57295
@@ -57482,11 +57657,11 @@
57482 */
57483 pBt = pCheck->pBt;
57484 usableSize = pBt->usableSize;
57485 if( iPage==0 ) return 0;
57486 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
57487 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0, 0))!=0 ){
57488 checkAppendMsg(pCheck, zContext,
57489 "unable to get the page. error code=%d", rc);
57490 return 0;
57491 }
57492
@@ -58441,11 +58616,11 @@
58441 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
58442 const Pgno iSrcPg = p->iNext; /* Source page number */
58443 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
58444 DbPage *pSrcPg; /* Source page object */
58445 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
58446 PAGER_ACQUIRE_READONLY);
58447 if( rc==SQLITE_OK ){
58448 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
58449 sqlite3PagerUnref(pSrcPg);
58450 }
58451 }
@@ -59596,38 +59771,33 @@
59596 /* If one value is a number and the other is not, the number is less.
59597 ** If both are numbers, compare as reals if one is a real, or as integers
59598 ** if both values are integers.
59599 */
59600 if( combined_flags&(MEM_Int|MEM_Real) ){
59601 if( !(f1&(MEM_Int|MEM_Real)) ){
59602 return 1;
59603 }
59604 if( !(f2&(MEM_Int|MEM_Real)) ){
59605 return -1;
59606 }
59607 if( (f1 & f2 & MEM_Int)==0 ){
59608 double r1, r2;
59609 if( (f1&MEM_Real)==0 ){
59610 r1 = (double)pMem1->u.i;
59611 }else{
59612 r1 = pMem1->r;
59613 }
59614 if( (f2&MEM_Real)==0 ){
59615 r2 = (double)pMem2->u.i;
59616 }else{
59617 r2 = pMem2->r;
59618 }
59619 if( r1<r2 ) return -1;
59620 if( r1>r2 ) return 1;
59621 return 0;
59622 }else{
59623 assert( f1&MEM_Int );
59624 assert( f2&MEM_Int );
59625 if( pMem1->u.i < pMem2->u.i ) return -1;
59626 if( pMem1->u.i > pMem2->u.i ) return 1;
59627 return 0;
59628 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59629 }
59630
59631 /* If one value is a string and the other is a blob, the string is less.
59632 ** If both are strings, compare using the collating functions.
59633 */
@@ -59803,43 +59973,108 @@
59803 }
59804 return p;
59805 }
59806
59807 /*
59808 ** Create a new sqlite3_value object, containing the value of pExpr.
 
 
 
 
 
 
 
 
 
 
 
 
 
59809 **
59810 ** This only works for very simple expressions that consist of one constant
59811 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
59812 ** be converted directly into a value, then the value is allocated and
59813 ** a pointer written to *ppVal. The caller is responsible for deallocating
59814 ** the value by passing it to sqlite3ValueFree() later on. If the expression
59815 ** cannot be converted to a value, then *ppVal is set to NULL.
59816 */
59817 SQLITE_PRIVATE int sqlite3ValueFromExpr(
59818 sqlite3 *db, /* The database connection */
59819 Expr *pExpr, /* The expression to evaluate */
59820 u8 enc, /* Encoding to use */
59821 u8 affinity, /* Affinity to use */
59822 sqlite3_value **ppVal /* Write the new value here */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59823 ){
59824 int op;
59825 char *zVal = 0;
59826 sqlite3_value *pVal = 0;
59827 int negInt = 1;
59828 const char *zNeg = "";
 
59829
59830 if( !pExpr ){
59831 *ppVal = 0;
59832 return SQLITE_OK;
59833 }
59834 op = pExpr->op;
59835
59836 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
59837 ** The ifdef here is to enable us to achieve 100% branch test coverage even
59838 ** when SQLITE_ENABLE_STAT3 is omitted.
59839 */
59840 #ifdef SQLITE_ENABLE_STAT3
59841 if( op==TK_REGISTER ) op = pExpr->op2;
59842 #else
59843 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
59844 #endif
59845
@@ -59853,11 +60088,11 @@
59853 negInt = -1;
59854 zNeg = "-";
59855 }
59856
59857 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
59858 pVal = sqlite3ValueNew(db);
59859 if( pVal==0 ) goto no_mem;
59860 if( ExprHasProperty(pExpr, EP_IntValue) ){
59861 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
59862 }else{
59863 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59870,15 +60105,17 @@
59870 }else{
59871 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
59872 }
59873 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
59874 if( enc!=SQLITE_UTF8 ){
59875 sqlite3VdbeChangeEncoding(pVal, enc);
59876 }
59877 }else if( op==TK_UMINUS ) {
59878 /* This branch happens for multiple negative signs. Ex: -(-5) */
59879 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
 
 
59880 sqlite3VdbeMemNumerify(pVal);
59881 if( pVal->u.i==SMALLEST_INT64 ){
59882 pVal->flags &= MEM_Int;
59883 pVal->flags |= MEM_Real;
59884 pVal->r = (double)LARGEST_INT64;
@@ -59887,19 +60124,19 @@
59887 }
59888 pVal->r = -pVal->r;
59889 sqlite3ValueApplyAffinity(pVal, affinity, enc);
59890 }
59891 }else if( op==TK_NULL ){
59892 pVal = sqlite3ValueNew(db);
59893 if( pVal==0 ) goto no_mem;
59894 }
59895 #ifndef SQLITE_OMIT_BLOB_LITERAL
59896 else if( op==TK_BLOB ){
59897 int nVal;
59898 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
59899 assert( pExpr->u.zToken[1]=='\'' );
59900 pVal = sqlite3ValueNew(db);
59901 if( !pVal ) goto no_mem;
59902 zVal = &pExpr->u.zToken[2];
59903 nVal = sqlite3Strlen30(zVal)-1;
59904 assert( zVal[nVal]=='\'' );
59905 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59909,20 +60146,203 @@
59909
59910 if( pVal ){
59911 sqlite3VdbeMemStoreType(pVal);
59912 }
59913 *ppVal = pVal;
59914 return SQLITE_OK;
59915
59916 no_mem:
59917 db->mallocFailed = 1;
59918 sqlite3DbFree(db, zVal);
59919 sqlite3ValueFree(pVal);
59920 *ppVal = 0;
 
 
 
 
59921 return SQLITE_NOMEM;
59922 }
59923
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59924 /*
59925 ** Change the string value of an sqlite3_value object
59926 */
59927 SQLITE_PRIVATE void sqlite3ValueSetStr(
59928 sqlite3_value *v, /* Value to be set */
@@ -61724,11 +62144,11 @@
61724 ** virtual module tables written in this transaction. This has to
61725 ** be done before determining whether a master journal file is
61726 ** required, as an xSync() callback may add an attached database
61727 ** to the transaction.
61728 */
61729 rc = sqlite3VtabSync(db, &p->zErrMsg);
61730
61731 /* This loop determines (a) if the commit hook should be invoked and
61732 ** (b) how many database files have open write transactions, not
61733 ** including the temp database. (b) is important because if more than
61734 ** one database file has an open write transaction, a master journal
@@ -63264,10 +63684,25 @@
63264 }else{
63265 v->expmask |= ((u32)1 << (iVar-1));
63266 }
63267 }
63268
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63269 /************** End of vdbeaux.c *********************************************/
63270 /************** Begin file vdbeapi.c *****************************************/
63271 /*
63272 ** 2004 May 26
63273 **
@@ -63477,16 +63912,18 @@
63477 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
63478 }
63479 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
63480 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63481 pCtx->isError = SQLITE_ERROR;
 
63482 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
63483 }
63484 #ifndef SQLITE_OMIT_UTF16
63485 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
63486 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63487 pCtx->isError = SQLITE_ERROR;
 
63488 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
63489 }
63490 #endif
63491 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
63492 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
@@ -63546,10 +63983,11 @@
63546 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63547 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
63548 }
63549 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
63550 pCtx->isError = errCode;
 
63551 if( pCtx->s.flags & MEM_Null ){
63552 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
63553 SQLITE_UTF8, SQLITE_STATIC);
63554 }
63555 }
@@ -63556,19 +63994,21 @@
63556
63557 /* Force an SQLITE_TOOBIG error. */
63558 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
63559 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63560 pCtx->isError = SQLITE_TOOBIG;
 
63561 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
63562 SQLITE_UTF8, SQLITE_STATIC);
63563 }
63564
63565 /* An SQLITE_NOMEM error. */
63566 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
63567 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63568 sqlite3VdbeMemSetNull(&pCtx->s);
63569 pCtx->isError = SQLITE_NOMEM;
 
63570 pCtx->s.db->mallocFailed = 1;
63571 }
63572
63573 /*
63574 ** This function is called after a transaction has been committed. It
@@ -63887,10 +64327,14 @@
63887 if( !pAuxData ) goto failed;
63888 pAuxData->iOp = pCtx->iOp;
63889 pAuxData->iArg = iArg;
63890 pAuxData->pNext = pVdbe->pAuxData;
63891 pVdbe->pAuxData = pAuxData;
 
 
 
 
63892 }else if( pAuxData->xDelete ){
63893 pAuxData->xDelete(pAuxData->pAux);
63894 }
63895
63896 pAuxData->pAux = pAux;
@@ -64555,13 +64999,13 @@
64555 /*
64556 ** Return the value of a status counter for a prepared statement
64557 */
64558 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
64559 Vdbe *pVdbe = (Vdbe*)pStmt;
64560 int v = pVdbe->aCounter[op-1];
64561 if( resetFlag ) pVdbe->aCounter[op-1] = 0;
64562 return v;
64563 }
64564
64565 /************** End of vdbeapi.c *********************************************/
64566 /************** Begin file vdbetrace.c ***************************************/
64567 /*
@@ -65444,23 +65888,10 @@
65444 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
65445 return 1;
65446 }
65447 #endif
65448
65449 /*
65450 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
65451 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
65452 ** in memory obtained from sqlite3DbMalloc).
65453 */
65454 static void importVtabErrMsg(Vdbe *p, sqlite3_vtab *pVtab){
65455 sqlite3 *db = p->db;
65456 sqlite3DbFree(db, p->zErrMsg);
65457 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
65458 sqlite3_free(pVtab->zErrMsg);
65459 pVtab->zErrMsg = 0;
65460 }
65461
65462
65463 /*
65464 ** Execute as much of a VDBE program as we can then return.
65465 **
65466 ** sqlite3VdbeMakeReady() must be called before this routine in order to
@@ -65964,11 +66395,11 @@
65964 CHECK_FOR_INTERRUPT;
65965 sqlite3VdbeIOTraceSql(p);
65966 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
65967 if( db->xProgress ){
65968 assert( 0 < db->nProgressOps );
65969 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP-1];
65970 if( nProgressLimit==0 ){
65971 nProgressLimit = db->nProgressOps;
65972 }else{
65973 nProgressLimit %= (unsigned)db->nProgressOps;
65974 }
@@ -66027,11 +66458,11 @@
66027 ** value or convert mem[p2] to a different type.
66028 */
66029 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
66030 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
66031 assert( pOp->p2>0 );
66032 assert( pOp->p2<=p->nMem );
66033 pOut = &aMem[pOp->p2];
66034 memAboutToChange(p, pOut);
66035 VdbeMemRelease(pOut);
66036 pOut->flags = MEM_Int;
66037 }
@@ -66038,34 +66469,34 @@
66038
66039 /* Sanity checking on other operands */
66040 #ifdef SQLITE_DEBUG
66041 if( (pOp->opflags & OPFLG_IN1)!=0 ){
66042 assert( pOp->p1>0 );
66043 assert( pOp->p1<=p->nMem );
66044 assert( memIsValid(&aMem[pOp->p1]) );
66045 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
66046 }
66047 if( (pOp->opflags & OPFLG_IN2)!=0 ){
66048 assert( pOp->p2>0 );
66049 assert( pOp->p2<=p->nMem );
66050 assert( memIsValid(&aMem[pOp->p2]) );
66051 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
66052 }
66053 if( (pOp->opflags & OPFLG_IN3)!=0 ){
66054 assert( pOp->p3>0 );
66055 assert( pOp->p3<=p->nMem );
66056 assert( memIsValid(&aMem[pOp->p3]) );
66057 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
66058 }
66059 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
66060 assert( pOp->p2>0 );
66061 assert( pOp->p2<=p->nMem );
66062 memAboutToChange(p, &aMem[pOp->p2]);
66063 }
66064 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
66065 assert( pOp->p3>0 );
66066 assert( pOp->p3<=p->nMem );
66067 memAboutToChange(p, &aMem[pOp->p3]);
66068 }
66069 #endif
66070
66071 switch( pOp->opcode ){
@@ -66154,11 +66585,11 @@
66154 **
66155 ** Write the current address onto register P1
66156 ** and then jump to address P2.
66157 */
66158 case OP_Gosub: { /* jump */
66159 assert( pOp->p1>0 && pOp->p1<=p->nMem );
66160 pIn1 = &aMem[pOp->p1];
66161 assert( (pIn1->flags & MEM_Dyn)==0 );
66162 memAboutToChange(p, pIn1);
66163 pIn1->flags = MEM_Int;
66164 pIn1->u.i = pc;
@@ -66370,11 +66801,11 @@
66370 #if 0 /* local variables moved into u.ab */
66371 int cnt;
66372 u16 nullFlag;
66373 #endif /* local variables moved into u.ab */
66374 u.ab.cnt = pOp->p3-pOp->p2;
66375 assert( pOp->p3<=p->nMem );
66376 pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
66377 while( u.ab.cnt>0 ){
66378 pOut++;
66379 memAboutToChange(p, pOut);
66380 VdbeMemRelease(pOut);
@@ -66443,12 +66874,12 @@
66443 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
66444
66445 pIn1 = &aMem[u.ad.p1];
66446 pOut = &aMem[u.ad.p2];
66447 while( u.ad.n-- ){
66448 assert( pOut<=&aMem[p->nMem] );
66449 assert( pIn1<=&aMem[p->nMem] );
66450 assert( memIsValid(pIn1) );
66451 memAboutToChange(p, pOut);
66452 u.ad.zMalloc = pOut->zMalloc;
66453 pOut->zMalloc = 0;
66454 sqlite3VdbeMemMove(pOut, pIn1);
@@ -66532,11 +66963,11 @@
66532 Mem *pMem;
66533 int i;
66534 #endif /* local variables moved into u.af */
66535 assert( p->nResColumn==pOp->p2 );
66536 assert( pOp->p1>0 );
66537 assert( pOp->p1+pOp->p2<=p->nMem+1 );
66538
66539 /* If this statement has violated immediate foreign key constraints, do
66540 ** not return the number of rows modified. And do not RELEASE the statement
66541 ** transaction. It needs to be rolled back. */
66542 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66812,15 +67243,15 @@
66812 #endif /* local variables moved into u.ai */
66813
66814 u.ai.n = pOp->p5;
66815 u.ai.apVal = p->apArg;
66816 assert( u.ai.apVal || u.ai.n==0 );
66817 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66818 pOut = &aMem[pOp->p3];
66819 memAboutToChange(p, pOut);
66820
66821 assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
66822 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
66823 u.ai.pArg = &aMem[pOp->p2];
66824 for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
66825 assert( memIsValid(u.ai.pArg) );
66826 u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -66843,11 +67274,11 @@
66843 ** the already allocated buffer instead of allocating a new one.
66844 */
66845 sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
66846 MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
66847
66848 u.ai.ctx.isError = 0;
66849 if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
66850 assert( pOp>aOp );
66851 assert( pOp[-1].p4type==P4_COLLSEQ );
66852 assert( pOp[-1].opcode==OP_CollSeq );
66853 u.ai.ctx.pColl = pOp[-1].p4.pColl;
@@ -66854,15 +67285,10 @@
66854 }
66855 db->lastRowid = lastRowid;
66856 (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
66857 lastRowid = db->lastRowid;
66858
66859 /* If any auxiliary data functions have been called by this user function,
66860 ** immediately call the destructor for any non-static values.
66861 */
66862 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
66863
66864 if( db->mallocFailed ){
66865 /* Even though a malloc() has failed, the implementation of the
66866 ** user function may have called an sqlite3_result_XXX() function
66867 ** to return a value. The following call releases any resources
66868 ** associated with such a value.
@@ -66870,13 +67296,16 @@
66870 sqlite3VdbeMemRelease(&u.ai.ctx.s);
66871 goto no_mem;
66872 }
66873
66874 /* If the function returned an error, throw an exception */
66875 if( u.ai.ctx.isError ){
66876 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
66877 rc = u.ai.ctx.isError;
 
 
 
66878 }
66879
66880 /* Copy the result of the function into register P3 */
66881 sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
66882 sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
@@ -67248,16 +67677,16 @@
67248 }else{
67249 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
67250 ** then the result is always NULL.
67251 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
67252 */
67253 if( pOp->p5 & SQLITE_STOREP2 ){
 
 
67254 pOut = &aMem[pOp->p2];
67255 MemSetTypeFlag(pOut, MEM_Null);
67256 REGISTER_TRACE(pOp->p2, pOut);
67257 }else if( pOp->p5 & SQLITE_JUMPIFNULL ){
67258 pc = pOp->p2-1;
67259 }
67260 break;
67261 }
67262 }else{
67263 /* Neither operand is NULL. Do a comparison. */
@@ -67354,15 +67783,15 @@
67354 u.al.p2 = pOp->p2;
67355 #if SQLITE_DEBUG
67356 if( aPermute ){
67357 int k, mx = 0;
67358 for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67359 assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
67360 assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
67361 }else{
67362 assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
67363 assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
67364 }
67365 #endif /* SQLITE_DEBUG */
67366 for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
67367 u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
67368 assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67615,11 +68044,11 @@
67615 u.ao.p1 = pOp->p1;
67616 u.ao.p2 = pOp->p2;
67617 u.ao.pC = 0;
67618 memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
67619 assert( u.ao.p1<p->nCursor );
67620 assert( pOp->p3>0 && pOp->p3<=p->nMem );
67621 u.ao.pDest = &aMem[pOp->p3];
67622 memAboutToChange(p, u.ao.pDest);
67623 u.ao.zRec = 0;
67624
67625 /* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67915,11 +68344,11 @@
67915 u.ap.zAffinity = pOp->p4.z;
67916 assert( u.ap.zAffinity!=0 );
67917 assert( u.ap.zAffinity[pOp->p2]==0 );
67918 pIn1 = &aMem[pOp->p1];
67919 while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
67920 assert( pIn1 <= &p->aMem[p->nMem] );
67921 assert( memIsValid(pIn1) );
67922 ExpandBlob(pIn1);
67923 applyAffinity(pIn1, u.ap.cAff, encoding);
67924 pIn1++;
67925 }
@@ -67978,11 +68407,11 @@
67978 u.aq.nData = 0; /* Number of bytes of data space */
67979 u.aq.nHdr = 0; /* Number of bytes of header space */
67980 u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
67981 u.aq.nField = pOp->p1;
67982 u.aq.zAffinity = pOp->p4.z;
67983 assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
67984 u.aq.pData0 = &aMem[u.aq.nField];
67985 u.aq.nField = pOp->p2;
67986 u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
67987 u.aq.file_format = p->minWriteFileFormat;
67988
@@ -68044,11 +68473,11 @@
68044 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
68045 u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
68046 }
68047 assert( u.aq.i==u.aq.nByte );
68048
68049 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68050 pOut->n = (int)u.aq.nByte;
68051 pOut->flags = MEM_Blob | MEM_Dyn;
68052 pOut->xDel = 0;
68053 if( u.aq.nZero ){
68054 pOut->u.nZero = u.aq.nZero;
@@ -68640,11 +69069,11 @@
68640 }else{
68641 u.ay.wrFlag = 0;
68642 }
68643 if( pOp->p5 & OPFLAG_P2ISREG ){
68644 assert( u.ay.p2>0 );
68645 assert( u.ay.p2<=p->nMem );
68646 pIn2 = &aMem[u.ay.p2];
68647 assert( memIsValid(pIn2) );
68648 assert( (pIn2->flags & MEM_Int)!=0 );
68649 sqlite3VdbeMemIntegerify(pIn2);
68650 u.ay.p2 = (int)pIn2->u.i;
@@ -69191,11 +69620,11 @@
69191
69192 pIn3 = &aMem[pOp->p3];
69193 u.bf.aMx = &aMem[pOp->p4.i];
69194 /* Assert that the values of parameters P1 and P4 are in range. */
69195 assert( pOp->p4type==P4_INT32 );
69196 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
69197 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69198
69199 /* Find the index cursor. */
69200 u.bf.pCx = p->apCsr[pOp->p1];
69201 assert( u.bf.pCx->deferredMoveto==0 );
@@ -69398,11 +69827,11 @@
69398 /* Assert that P3 is a valid memory cell. */
69399 assert( pOp->p3<=u.bh.pFrame->nMem );
69400 u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
69401 }else{
69402 /* Assert that P3 is a valid memory cell. */
69403 assert( pOp->p3<=p->nMem );
69404 u.bh.pMem = &aMem[pOp->p3];
69405 memAboutToChange(p, u.bh.pMem);
69406 }
69407 assert( memIsValid(u.bh.pMem) );
69408
@@ -69808,11 +70237,11 @@
69808 }else if( u.bn.pC->pVtabCursor ){
69809 u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
69810 u.bn.pModule = u.bn.pVtab->pModule;
69811 assert( u.bn.pModule->xRowid );
69812 rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
69813 importVtabErrMsg(p, u.bn.pVtab);
69814 #endif /* SQLITE_OMIT_VIRTUALTABLE */
69815 }else{
69816 assert( u.bn.pC->pCursor!=0 );
69817 rc = sqlite3VdbeCursorMoveto(u.bn.pC);
69818 if( rc ) goto abort_due_to_error;
@@ -69900,11 +70329,11 @@
69900 case OP_Sort: { /* jump */
69901 #ifdef SQLITE_TEST
69902 sqlite3_sort_count++;
69903 sqlite3_search_count--;
69904 #endif
69905 p->aCounter[SQLITE_STMTSTATUS_SORT-1]++;
69906 /* Fall through into OP_Rewind */
69907 }
69908 /* Opcode: Rewind P1 P2 * * *
69909 **
69910 ** The next use of the Rowid or Column or Next instruction for P1
@@ -69983,21 +70412,21 @@
69983 VdbeCursor *pC;
69984 int res;
69985 #endif /* local variables moved into u.br */
69986
69987 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69988 assert( pOp->p5<=ArraySize(p->aCounter) );
69989 u.br.pC = p->apCsr[pOp->p1];
69990 if( u.br.pC==0 ){
69991 break; /* See ticket #2273 */
69992 }
69993 assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
69994 if( isSorter(u.br.pC) ){
69995 assert( pOp->opcode==OP_SorterNext );
69996 rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
69997 }else{
69998 u.br.res = 1;
69999 assert( u.br.pC->deferredMoveto==0 );
70000 assert( u.br.pC->pCursor );
70001 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
70002 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
70003 rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
@@ -70004,11 +70433,11 @@
70004 }
70005 u.br.pC->nullRow = (u8)u.br.res;
70006 u.br.pC->cacheStatus = CACHE_STALE;
70007 if( u.br.res==0 ){
70008 pc = pOp->p2 - 1;
70009 if( pOp->p5 ) p->aCounter[pOp->p5-1]++;
70010 #ifdef SQLITE_TEST
70011 sqlite3_search_count++;
70012 #endif
70013 }
70014 u.br.pC->rowidIsValid = 0;
@@ -70076,11 +70505,11 @@
70076 int res;
70077 UnpackedRecord r;
70078 #endif /* local variables moved into u.bt */
70079
70080 assert( pOp->p3>0 );
70081 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
70082 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70083 u.bt.pC = p->apCsr[pOp->p1];
70084 assert( u.bt.pC!=0 );
70085 u.bt.pCrsr = u.bt.pC->pCursor;
70086 if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70292,10 +70721,11 @@
70292 int nChange;
70293 #endif /* local variables moved into u.bx */
70294
70295 u.bx.nChange = 0;
70296 assert( p->readOnly==0 );
 
70297 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
70298 rc = sqlite3BtreeClearTable(
70299 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
70300 );
70301 if( pOp->p3 ){
@@ -70498,11 +70928,11 @@
70498 assert( p->bIsReader );
70499 u.ca.nRoot = pOp->p2;
70500 assert( u.ca.nRoot>0 );
70501 u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
70502 if( u.ca.aRoot==0 ) goto no_mem;
70503 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70504 u.ca.pnErr = &aMem[pOp->p3];
70505 assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
70506 assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
70507 pIn1 = &aMem[pOp->p1];
70508 for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70934,11 +71364,11 @@
70934 u.cg.apVal[u.cg.i] = u.cg.pRec;
70935 memAboutToChange(p, u.cg.pRec);
70936 sqlite3VdbeMemStoreType(u.cg.pRec);
70937 }
70938 u.cg.ctx.pFunc = pOp->p4.pFunc;
70939 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70940 u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
70941 u.cg.pMem->n++;
70942 u.cg.ctx.s.flags = MEM_Null;
70943 u.cg.ctx.s.z = 0;
70944 u.cg.ctx.s.zMalloc = 0;
@@ -70983,11 +71413,11 @@
70983 */
70984 case OP_AggFinal: {
70985 #if 0 /* local variables moved into u.ch */
70986 Mem *pMem;
70987 #endif /* local variables moved into u.ch */
70988 assert( pOp->p1>0 && pOp->p1<=p->nMem );
70989 u.ch.pMem = &aMem[pOp->p1];
70990 assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
70991 rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
70992 if( rc ){
70993 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71249,11 +71679,11 @@
71249 #if 0 /* local variables moved into u.cl */
71250 VTable *pVTab;
71251 #endif /* local variables moved into u.cl */
71252 u.cl.pVTab = pOp->p4.pVtab;
71253 rc = sqlite3VtabBegin(db, u.cl.pVTab);
71254 if( u.cl.pVTab ) importVtabErrMsg(p, u.cl.pVTab->pVtab);
71255 break;
71256 }
71257 #endif /* SQLITE_OMIT_VIRTUALTABLE */
71258
71259 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -71302,11 +71732,11 @@
71302 u.cm.pVtabCursor = 0;
71303 u.cm.pVtab = pOp->p4.pVtab->pVtab;
71304 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
71305 assert(u.cm.pVtab && u.cm.pModule);
71306 rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
71307 importVtabErrMsg(p, u.cm.pVtab);
71308 if( SQLITE_OK==rc ){
71309 /* Initialize sqlite3_vtab_cursor base class */
71310 u.cm.pVtabCursor->pVtab = u.cm.pVtab;
71311
71312 /* Initialize vdbe cursor object */
@@ -71382,11 +71812,11 @@
71382 }
71383
71384 p->inVtabMethod = 1;
71385 rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
71386 p->inVtabMethod = 0;
71387 importVtabErrMsg(p, u.cn.pVtab);
71388 if( rc==SQLITE_OK ){
71389 u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
71390 }
71391
71392 if( u.cn.res ){
@@ -71414,11 +71844,11 @@
71414 sqlite3_context sContext;
71415 #endif /* local variables moved into u.co */
71416
71417 VdbeCursor *pCur = p->apCsr[pOp->p1];
71418 assert( pCur->pVtabCursor );
71419 assert( pOp->p3>0 && pOp->p3<=p->nMem );
71420 u.co.pDest = &aMem[pOp->p3];
71421 memAboutToChange(p, u.co.pDest);
71422 if( pCur->nullRow ){
71423 sqlite3VdbeMemSetNull(u.co.pDest);
71424 break;
@@ -71435,11 +71865,11 @@
71435 */
71436 sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
71437 MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
71438
71439 rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
71440 importVtabErrMsg(p, u.co.pVtab);
71441 if( u.co.sContext.isError ){
71442 rc = u.co.sContext.isError;
71443 }
71444
71445 /* Copy the result of the function to the P3 register. We
@@ -71490,11 +71920,11 @@
71490 ** some other method is next invoked on the save virtual table cursor.
71491 */
71492 p->inVtabMethod = 1;
71493 rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
71494 p->inVtabMethod = 0;
71495 importVtabErrMsg(p, u.cp.pVtab);
71496 if( rc==SQLITE_OK ){
71497 u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
71498 }
71499
71500 if( !u.cp.res ){
@@ -71529,11 +71959,11 @@
71529 testcase( u.cq.pName->enc==SQLITE_UTF16BE );
71530 testcase( u.cq.pName->enc==SQLITE_UTF16LE );
71531 rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
71532 if( rc==SQLITE_OK ){
71533 rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
71534 importVtabErrMsg(p, u.cq.pVtab);
71535 p->expired = 0;
71536 }
71537 break;
71538 }
71539 #endif
@@ -71593,11 +72023,11 @@
71593 u.cr.pX++;
71594 }
71595 db->vtabOnConflict = pOp->p5;
71596 rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
71597 db->vtabOnConflict = vtabOnConflict;
71598 importVtabErrMsg(p, u.cr.pVtab);
71599 if( rc==SQLITE_OK && pOp->p1 ){
71600 assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
71601 db->lastRowid = lastRowid = u.cr.rowid;
71602 }
71603 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
@@ -71760,11 +72190,12 @@
71760 /* This is the only way out of this procedure. We have to
71761 ** release the mutexes on btrees that were acquired at the
71762 ** top. */
71763 vdbe_return:
71764 db->lastRowid = lastRowid;
71765 p->aCounter[SQLITE_STMTSTATUS_VM_STEP-1] += (int)nVmStep;
 
71766 sqlite3VdbeLeave(p);
71767 return rc;
71768
71769 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
71770 ** is encountered.
@@ -75520,12 +75951,11 @@
75520 int op = p->op;
75521 if( op==TK_CAST || op==TK_UPLUS ){
75522 p = p->pLeft;
75523 continue;
75524 }
75525 assert( op!=TK_REGISTER || p->op2!=TK_COLLATE );
75526 if( op==TK_COLLATE ){
75527 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
75528 break;
75529 }
75530 if( p->pTab!=0
75531 && (op==TK_AGG_COLUMN || op==TK_COLUMN
@@ -76685,10 +77115,11 @@
76685 break;
76686 }
76687 case TK_UMINUS: {
76688 int v;
76689 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
 
76690 *pValue = -v;
76691 rc = 1;
76692 }
76693 break;
76694 }
@@ -78910,10 +79341,11 @@
78910 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
78911 compRight.op = TK_LE;
78912 compRight.pLeft = &exprX;
78913 compRight.pRight = pExpr->x.pList->a[1].pExpr;
78914 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
 
78915 exprX.op = TK_REGISTER;
78916 if( jumpIfTrue ){
78917 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
78918 }else{
78919 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
@@ -80343,11 +80775,11 @@
80343
80344 /* Ensure the default expression is something that sqlite3ValueFromExpr()
80345 ** can handle (i.e. not CURRENT_TIME etc.)
80346 */
80347 if( pDflt ){
80348 sqlite3_value *pVal;
80349 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
80350 db->mallocFailed = 1;
80351 return;
80352 }
80353 if( !pVal ){
@@ -80484,11 +80916,11 @@
80484 #endif /* SQLITE_ALTER_TABLE */
80485
80486 /************** End of alter.c ***********************************************/
80487 /************** Begin file analyze.c *****************************************/
80488 /*
80489 ** 2005 July 8
80490 **
80491 ** The author disclaims copyright to this source code. In place of
80492 ** a legal notice, here is a blessing:
80493 **
80494 ** May you do good and not evil.
@@ -80505,27 +80937,36 @@
80505 ** The following system tables are or have been supported:
80506 **
80507 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
80508 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
80509 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 
80510 **
80511 ** Additional tables might be added in future releases of SQLite.
80512 ** The sqlite_stat2 table is not created or used unless the SQLite version
80513 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
80514 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
80515 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
80516 ** created and used by SQLite versions 3.7.9 and later and with
80517 ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
80518 ** is a superset of sqlite_stat2.
 
 
 
 
 
 
 
80519 **
80520 ** Format of sqlite_stat1:
80521 **
80522 ** There is normally one row per index, with the index identified by the
80523 ** name in the idx column. The tbl column is the name of the table to
80524 ** which the index belongs. In each such row, the stat column will be
80525 ** a string consisting of a list of integers. The first integer in this
80526 ** list is the number of rows in the index and in the table. The second
 
80527 ** integer is the average number of rows in the index that have the same
80528 ** value in the first column of the index. The third integer is the average
80529 ** number of rows in the index that have the same value for the first two
80530 ** columns. The N-th integer (for N>1) is the average number of rows in
80531 ** the index which have the same value for the first N-1 columns. For
@@ -80568,57 +81009,85 @@
80568 ** writes the sqlite_stat2 table. This version of SQLite only supports
80569 ** sqlite_stat3.
80570 **
80571 ** Format for sqlite_stat3:
80572 **
80573 ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
80574 ** used to avoid compatibility problems.
 
80575 **
80576 ** The format of the sqlite_stat3 table is similar to the format of
80577 ** the sqlite_stat2 table. There are multiple entries for each index.
 
 
 
 
 
 
80578 ** The idx column names the index and the tbl column is the table of the
80579 ** index. If the idx and tbl columns are the same, then the sample is
80580 ** of the INTEGER PRIMARY KEY. The sample column is a value taken from
80581 ** the left-most column of the index. The nEq column is the approximate
80582 ** number of entires in the index whose left-most column exactly matches
80583 ** the sample. nLt is the approximate number of entires whose left-most
80584 ** column is less than the sample. The nDLt column is the approximate
80585 ** number of distinct left-most entries in the index that are less than
80586 ** the sample.
80587 **
80588 ** Future versions of SQLite might change to store a string containing
80589 ** multiple integers values in the nDLt column of sqlite_stat3. The first
80590 ** integer will be the number of prior index entires that are distinct in
80591 ** the left-most column. The second integer will be the number of prior index
80592 ** entries that are distinct in the first two columns. The third integer
80593 ** will be the number of prior index entries that are distinct in the first
80594 ** three columns. And so forth. With that extension, the nDLt field is
80595 ** similar in function to the sqlite_stat1.stat field.
80596 **
80597 ** There can be an arbitrary number of sqlite_stat3 entries per index.
80598 ** The ANALYZE command will typically generate sqlite_stat3 tables
80599 ** that contain between 10 and 40 samples which are distributed across
80600 ** the key space, though not uniformly, and which include samples with
80601 ** largest possible nEq values.
 
 
 
 
 
 
 
 
 
 
80602 */
80603 #ifndef SQLITE_OMIT_ANALYZE
80604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80605 /*
80606 ** This routine generates code that opens the sqlite_stat1 table for
80607 ** writing with cursor iStatCur. If the library was built with the
80608 ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
80609 ** opened for writing using cursor (iStatCur+1)
80610 **
80611 ** If the sqlite_stat1 tables does not previously exist, it is created.
80612 ** Similarly, if the sqlite_stat3 table does not exist and the library
80613 ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created.
80614 **
80615 ** Argument zWhere may be a pointer to a buffer containing a table name,
80616 ** or it may be a NULL pointer. If it is not NULL, then all entries in
80617 ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
80618 ** with the named table are deleted. If zWhere==0, then code is generated
80619 ** to delete all stat table entries.
80620 */
80621 static void openStatTable(
80622 Parse *pParse, /* Parsing context */
80623 int iDb, /* The database we are looking in */
80624 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80628,22 +81097,28 @@
80628 static const struct {
80629 const char *zName;
80630 const char *zCols;
80631 } aTable[] = {
80632 { "sqlite_stat1", "tbl,idx,stat" },
80633 #ifdef SQLITE_ENABLE_STAT3
 
 
 
80634 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 
 
 
 
80635 #endif
80636 };
80637
80638 int aRoot[] = {0, 0};
80639 u8 aCreateTbl[] = {0, 0};
80640
80641 int i;
80642 sqlite3 *db = pParse->db;
80643 Db *pDb;
80644 Vdbe *v = sqlite3GetVdbe(pParse);
 
 
 
80645 if( v==0 ) return;
80646 assert( sqlite3BtreeHoldsAllMutexes(db) );
80647 assert( sqlite3VdbeDb(v)==db );
80648 pDb = &db->aDb[iDb];
80649
@@ -80652,262 +81127,592 @@
80652 */
80653 for(i=0; i<ArraySize(aTable); i++){
80654 const char *zTab = aTable[i].zName;
80655 Table *pStat;
80656 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
80657 /* The sqlite_stat[12] table does not exist. Create it. Note that a
80658 ** side-effect of the CREATE TABLE statement is to leave the rootpage
80659 ** of the new table in register pParse->regRoot. This is important
80660 ** because the OpenWrite opcode below will be needing it. */
80661 sqlite3NestedParse(pParse,
80662 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
80663 );
80664 aRoot[i] = pParse->regRoot;
80665 aCreateTbl[i] = OPFLAG_P2ISREG;
 
 
80666 }else{
80667 /* The table already exists. If zWhere is not NULL, delete all entries
80668 ** associated with the table zWhere. If zWhere is NULL, delete the
80669 ** entire contents of the table. */
80670 aRoot[i] = pStat->tnum;
 
80671 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
80672 if( zWhere ){
80673 sqlite3NestedParse(pParse,
80674 "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
 
80675 );
80676 }else{
80677 /* The sqlite_stat[12] table already exists. Delete all rows. */
80678 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
80679 }
80680 }
80681 }
80682
80683 /* Open the sqlite_stat[13] tables for writing. */
80684 for(i=0; i<ArraySize(aTable); i++){
 
80685 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
80686 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
80687 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
80688 }
80689 }
80690
80691 /*
80692 ** Recommended number of samples for sqlite_stat3
80693 */
80694 #ifndef SQLITE_STAT3_SAMPLES
80695 # define SQLITE_STAT3_SAMPLES 24
80696 #endif
80697
80698 /*
80699 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
80700 ** share an instance of the following structure to hold their state
80701 ** information.
80702 */
80703 typedef struct Stat3Accum Stat3Accum;
80704 struct Stat3Accum {
 
 
 
 
 
 
 
 
 
 
 
 
80705 tRowcnt nRow; /* Number of rows in the entire table */
80706 tRowcnt nPSample; /* How often to do a periodic sample */
80707 int iMin; /* Index of entry with minimum nEq and hash */
80708 int mxSample; /* Maximum number of samples to accumulate */
80709 int nSample; /* Current number of samples */
80710 u32 iPrn; /* Pseudo-random number used for sampling */
80711 struct Stat3Sample {
80712 i64 iRowid; /* Rowid in main table of the key */
80713 tRowcnt nEq; /* sqlite_stat3.nEq */
80714 tRowcnt nLt; /* sqlite_stat3.nLt */
80715 tRowcnt nDLt; /* sqlite_stat3.nDLt */
80716 u8 isPSample; /* True if a periodic sample */
80717 u32 iHash; /* Tiebreaker hash */
80718 } *a; /* An array of samples */
80719 };
80720
80721 #ifdef SQLITE_ENABLE_STAT3
80722 /*
80723 ** Implementation of the stat3_init(C,S) SQL function. The two parameters
80724 ** are the number of rows in the table or index (C) and the number of samples
80725 ** to accumulate (S).
80726 **
80727 ** This routine allocates the Stat3Accum object.
80728 **
80729 ** The return value is the Stat3Accum object (P).
80730 */
80731 static void stat3Init(
80732 sqlite3_context *context,
80733 int argc,
80734 sqlite3_value **argv
80735 ){
80736 Stat3Accum *p;
80737 tRowcnt nRow;
80738 int mxSample;
80739 int n;
 
 
 
80740
 
80741 UNUSED_PARAMETER(argc);
80742 nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
80743 mxSample = sqlite3_value_int(argv[1]);
80744 n = sizeof(*p) + sizeof(p->a[0])*mxSample;
80745 p = sqlite3MallocZero( n );
 
 
 
 
 
 
 
 
 
 
 
80746 if( p==0 ){
80747 sqlite3_result_error_nomem(context);
80748 return;
80749 }
80750 p->a = (struct Stat3Sample*)&p[1];
80751 p->nRow = nRow;
80752 p->mxSample = mxSample;
80753 p->nPSample = p->nRow/(mxSample/3+1) + 1;
80754 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80755 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
80756 }
80757 static const FuncDef stat3InitFuncdef = {
80758 2, /* nArg */
80759 SQLITE_UTF8, /* iPrefEnc */
80760 0, /* flags */
80761 0, /* pUserData */
80762 0, /* pNext */
80763 stat3Init, /* xFunc */
80764 0, /* xStep */
80765 0, /* xFinalize */
80766 "stat3_init", /* zName */
80767 0, /* pHash */
80768 0 /* pDestructor */
80769 };
80770
80771
80772 /*
80773 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
80774 ** arguments describe a single key instance. This routine makes the
80775 ** decision about whether or not to retain this key for the sqlite_stat3
80776 ** table.
80777 **
80778 ** The return value is NULL.
80779 */
80780 static void stat3Push(
80781 sqlite3_context *context,
80782 int argc,
80783 sqlite3_value **argv
80784 ){
80785 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
80786 tRowcnt nEq = sqlite3_value_int64(argv[0]);
80787 tRowcnt nLt = sqlite3_value_int64(argv[1]);
80788 tRowcnt nDLt = sqlite3_value_int64(argv[2]);
80789 i64 rowid = sqlite3_value_int64(argv[3]);
80790 u8 isPSample = 0;
80791 u8 doInsert = 0;
80792 int iMin = p->iMin;
80793 struct Stat3Sample *pSample;
80794 int i;
80795 u32 h;
80796
80797 UNUSED_PARAMETER(context);
80798 UNUSED_PARAMETER(argc);
80799 if( nEq==0 ) return;
80800 h = p->iPrn = p->iPrn*1103515245 + 12345;
80801 if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
80802 doInsert = isPSample = 1;
80803 }else if( p->nSample<p->mxSample ){
80804 doInsert = 1;
80805 }else{
80806 if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
80807 doInsert = 1;
80808 }
80809 }
80810 if( !doInsert ) return;
80811 if( p->nSample==p->mxSample ){
80812 assert( p->nSample - iMin - 1 >= 0 );
80813 memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80814 pSample = &p->a[p->nSample-1];
80815 }else{
80816 pSample = &p->a[p->nSample++];
80817 }
80818 pSample->iRowid = rowid;
80819 pSample->nEq = nEq;
80820 pSample->nLt = nLt;
80821 pSample->nDLt = nDLt;
80822 pSample->iHash = h;
80823 pSample->isPSample = isPSample;
80824
80825 /* Find the new minimum */
80826 if( p->nSample==p->mxSample ){
80827 pSample = p->a;
80828 i = 0;
80829 while( pSample->isPSample ){
80830 i++;
80831 pSample++;
80832 assert( i<p->nSample );
80833 }
80834 nEq = pSample->nEq;
80835 h = pSample->iHash;
80836 iMin = i;
80837 for(i++, pSample++; i<p->nSample; i++, pSample++){
80838 if( pSample->isPSample ) continue;
80839 if( pSample->nEq<nEq
80840 || (pSample->nEq==nEq && pSample->iHash<h)
80841 ){
80842 iMin = i;
80843 nEq = pSample->nEq;
80844 h = pSample->iHash;
80845 }
80846 }
 
 
 
 
 
 
 
 
80847 p->iMin = iMin;
80848 }
80849 }
80850 static const FuncDef stat3PushFuncdef = {
80851 5, /* nArg */
80852 SQLITE_UTF8, /* iPrefEnc */
80853 0, /* flags */
80854 0, /* pUserData */
80855 0, /* pNext */
80856 stat3Push, /* xFunc */
80857 0, /* xStep */
80858 0, /* xFinalize */
80859 "stat3_push", /* zName */
80860 0, /* pHash */
80861 0 /* pDestructor */
80862 };
80863
80864 /*
80865 ** Implementation of the stat3_get(P,N,...) SQL function. This routine is
80866 ** used to query the results. Content is returned for the Nth sqlite_stat3
80867 ** row where N is between 0 and S-1 and S is the number of samples. The
80868 ** value returned depends on the number of arguments.
80869 **
80870 ** argc==2 result: rowid
80871 ** argc==3 result: nEq
80872 ** argc==4 result: nLt
80873 ** argc==5 result: nDLt
80874 */
80875 static void stat3Get(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80876 sqlite3_context *context,
80877 int argc,
80878 sqlite3_value **argv
80879 ){
80880 int n = sqlite3_value_int(argv[1]);
80881 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
80882
80883 assert( p!=0 );
80884 if( p->nSample<=n ) return;
80885 switch( argc ){
80886 case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
80887 case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
80888 case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
80889 default: sqlite3_result_int64(context, p->a[n].nDLt); break;
80890 }
80891 }
80892 static const FuncDef stat3GetFuncdef = {
80893 -1, /* nArg */
80894 SQLITE_UTF8, /* iPrefEnc */
80895 0, /* flags */
80896 0, /* pUserData */
80897 0, /* pNext */
80898 stat3Get, /* xFunc */
80899 0, /* xStep */
80900 0, /* xFinalize */
80901 "stat3_get", /* zName */
80902 0, /* pHash */
80903 0 /* pDestructor */
80904 };
80905 #endif /* SQLITE_ENABLE_STAT3 */
80906
80907
80908
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80909
80910 /*
80911 ** Generate code to do an analysis of all indices associated with
80912 ** a single table.
80913 */
@@ -80914,46 +81719,35 @@
80914 static void analyzeOneTable(
80915 Parse *pParse, /* Parser context */
80916 Table *pTab, /* Table whose indices are to be analyzed */
80917 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
80918 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
80919 int iMem /* Available memory locations begin here */
 
80920 ){
80921 sqlite3 *db = pParse->db; /* Database handle */
80922 Index *pIdx; /* An index to being analyzed */
80923 int iIdxCur; /* Cursor open on index being analyzed */
 
80924 Vdbe *v; /* The virtual machine being built up */
80925 int i; /* Loop counter */
80926 int topOfLoop; /* The top of the loop */
80927 int endOfLoop; /* The end of the loop */
80928 int jZeroRows = -1; /* Jump from here if number of rows is zero */
80929 int iDb; /* Index of database containing pTab */
80930 u8 needTableCnt = 1; /* True to count the table */
 
 
 
 
 
 
 
80931 int regTabname = iMem++; /* Register containing table name */
80932 int regIdxname = iMem++; /* Register containing index name */
80933 int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
80934 #ifdef SQLITE_ENABLE_STAT3
80935 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
80936 int regNumLt = iMem++; /* Number of keys less than regSample */
80937 int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
80938 int regSample = iMem++; /* The next sample value */
80939 int regRowid = regSample; /* Rowid of a sample */
80940 int regAccum = iMem++; /* Register to hold Stat3Accum object */
80941 int regLoop = iMem++; /* Loop counter */
80942 int regCount = iMem++; /* Number of rows in the table or index */
80943 int regTemp1 = iMem++; /* Intermediate register */
80944 int regTemp2 = iMem++; /* Intermediate register */
80945 int once = 1; /* One-time initialization */
80946 int shortJump = 0; /* Instruction address */
80947 int iTabCur = pParse->nTab++; /* Table cursor */
80948 #endif
80949 int regCol = iMem++; /* Content of a column in analyzed table */
80950 int regRec = iMem++; /* Register holding completed record */
80951 int regTemp = iMem++; /* Temporary use register */
80952 int regNewRowid = iMem++; /* Rowid for the inserted record */
80953
80954
80955 v = sqlite3GetVdbe(pParse);
80956 if( v==0 || NEVER(pTab==0) ){
80957 return;
80958 }
80959 if( pTab->tnum==0 ){
@@ -80973,217 +81767,230 @@
80973 db->aDb[iDb].zName ) ){
80974 return;
80975 }
80976 #endif
80977
80978 /* Establish a read-lock on the table at the shared-cache level. */
 
 
 
80979 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
80980
80981 iIdxCur = pParse->nTab++;
 
 
80982 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 
80983 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
80984 int nCol;
80985 KeyInfo *pKey;
80986 int addrIfNot = 0; /* address of OP_IfNot */
80987 int *aChngAddr; /* Array of jump instruction addresses */
 
 
80988
80989 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
80990 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
80991 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
80992 nCol = pIdx->nColumn;
80993 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
80994 if( aChngAddr==0 ) continue;
80995 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
80996 if( iMem+1+(nCol*2)>pParse->nMem ){
80997 pParse->nMem = iMem+1+(nCol*2);
80998 }
80999
81000 /* Open a cursor to the index to be analyzed. */
81001 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81002 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
81003 (char *)pKey, P4_KEYINFO_HANDOFF);
81004 VdbeComment((v, "%s", pIdx->zName));
81005
81006 /* Populate the register containing the index name. */
81007 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
81008
81009 #ifdef SQLITE_ENABLE_STAT3
81010 if( once ){
81011 once = 0;
81012 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81013 }
81014 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
81015 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
81016 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
81017 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
81018 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
81019 sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
81020 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
81021 (char*)&stat3InitFuncdef, P4_FUNCDEF);
81022 sqlite3VdbeChangeP5(v, 2);
81023 #endif /* SQLITE_ENABLE_STAT3 */
81024
81025 /* The block of memory cells initialized here is used as follows.
81026 **
81027 ** iMem:
81028 ** The total number of rows in the table.
81029 **
81030 ** iMem+1 .. iMem+nCol:
81031 ** Number of distinct entries in index considering the
81032 ** left-most N columns only, where N is between 1 and nCol,
81033 ** inclusive.
81034 **
81035 ** iMem+nCol+1 .. Mem+2*nCol:
81036 ** Previous value of indexed columns, from left to right.
81037 **
81038 ** Cells iMem through iMem+nCol are initialized to 0. The others are
81039 ** initialized to contain an SQL NULL.
81040 */
81041 for(i=0; i<=nCol; i++){
81042 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
81043 }
81044 for(i=0; i<nCol; i++){
81045 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
81046 }
81047
81048 /* Start the analysis loop. This loop runs through all the entries in
81049 ** the index b-tree. */
81050 endOfLoop = sqlite3VdbeMakeLabel(v);
81051 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
81052 topOfLoop = sqlite3VdbeCurrentAddr(v);
81053 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
81054
81055 for(i=0; i<nCol; i++){
81056 CollSeq *pColl;
81057 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
81058 if( i==0 ){
81059 /* Always record the very first row */
81060 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
81061 }
81062 assert( pIdx->azColl!=0 );
81063 assert( pIdx->azColl[i]!=0 );
81064 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81065 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
81066 (char*)pColl, P4_COLLSEQ);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81067 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
81068 VdbeComment((v, "jump if column %d changed", i));
81069 #ifdef SQLITE_ENABLE_STAT3
81070 if( i==0 ){
81071 sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
81072 VdbeComment((v, "incr repeat count"));
81073 }
81074 #endif
81075 }
81076 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
81077 for(i=0; i<nCol; i++){
81078 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
81079 if( i==0 ){
81080 sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
81081 #ifdef SQLITE_ENABLE_STAT3
81082 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81083 (char*)&stat3PushFuncdef, P4_FUNCDEF);
81084 sqlite3VdbeChangeP5(v, 5);
81085 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
81086 sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
81087 sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
81088 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
81089 #endif
81090 }
81091 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
81092 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
81093 }
81094 sqlite3DbFree(db, aChngAddr);
81095
81096 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
81097 sqlite3VdbeResolveLabel(v, endOfLoop);
81098
81099 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
81100 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81101 #ifdef SQLITE_ENABLE_STAT3
81102 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81103 (char*)&stat3PushFuncdef, P4_FUNCDEF);
81104 sqlite3VdbeChangeP5(v, 5);
81105 sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
81106 shortJump =
81107 sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
81108 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
81109 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81110 sqlite3VdbeChangeP5(v, 2);
81111 sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
81112 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
81113 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
81114 sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
81115 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
81116 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81117 sqlite3VdbeChangeP5(v, 3);
81118 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
81119 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81120 sqlite3VdbeChangeP5(v, 4);
81121 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
81122 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81123 sqlite3VdbeChangeP5(v, 5);
81124 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
81125 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81126 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
81127 sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
81128 sqlite3VdbeJumpHere(v, shortJump+2);
81129 #endif
81130
81131 /* Store the results in sqlite_stat1.
81132 **
81133 ** The result is a single row of the sqlite_stat1 table. The first
81134 ** two columns are the names of the table and index. The third column
81135 ** is a string composed of a list of integer statistics about the
81136 ** index. The first integer in the list is the total number of entries
81137 ** in the index. There is one additional integer in the list for each
81138 ** column of the table. This additional integer is a guess of how many
81139 ** rows of the table the index will select. If D is the count of distinct
81140 ** values and K is the total number of rows, then the integer is computed
81141 ** as:
81142 **
81143 ** I = (K+D-1)/D
81144 **
81145 ** If K==0 then no entry is made into the sqlite_stat1 table.
81146 ** If K>0 then it is always the case the D>0 so division by zero
81147 ** is never possible.
81148 */
81149 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
81150 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
81151 for(i=0; i<nCol; i++){
81152 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
81153 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81154 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
81155 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
81156 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
81157 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
81158 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81159 }
81160 if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81161 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81162 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81163 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81164 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81165 if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81166 }
81167
81168 /* Create a single sqlite_stat1 entry containing NULL as the index
81169 ** name and the row count as the content.
81170 */
81171 if( pOnlyIdx==0 && needTableCnt ){
81172 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
81173 VdbeComment((v, "%s", pTab->zName));
81174 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
81175 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81176 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
81177 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
81178 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81179 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81180 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81181 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81182 sqlite3VdbeJumpHere(v, jZeroRows);
81183 }
81184 if( pParse->nMem<regRec ) pParse->nMem = regRec;
81185 }
81186
81187
81188 /*
81189 ** Generate code that will cause the most recent index analysis to
@@ -81203,20 +82010,22 @@
81203 sqlite3 *db = pParse->db;
81204 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
81205 HashElem *k;
81206 int iStatCur;
81207 int iMem;
 
81208
81209 sqlite3BeginWriteOperation(pParse, 0, iDb);
81210 iStatCur = pParse->nTab;
81211 pParse->nTab += 3;
81212 openStatTable(pParse, iDb, iStatCur, 0, 0);
81213 iMem = pParse->nMem+1;
 
81214 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81215 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
81216 Table *pTab = (Table*)sqliteHashData(k);
81217 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
81218 }
81219 loadAnalysis(pParse, iDb);
81220 }
81221
81222 /*
@@ -81237,11 +82046,11 @@
81237 if( pOnlyIdx ){
81238 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
81239 }else{
81240 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
81241 }
81242 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
81243 loadAnalysis(pParse, iDb);
81244 }
81245
81246 /*
81247 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -81319,10 +82128,47 @@
81319 typedef struct analysisInfo analysisInfo;
81320 struct analysisInfo {
81321 sqlite3 *db;
81322 const char *zDatabase;
81323 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81324
81325 /*
81326 ** This callback is invoked once for each index when reading the
81327 ** sqlite_stat1 table.
81328 **
@@ -81335,12 +82181,10 @@
81335 */
81336 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
81337 analysisInfo *pInfo = (analysisInfo*)pData;
81338 Index *pIndex;
81339 Table *pTable;
81340 int i, c, n;
81341 tRowcnt v;
81342 const char *z;
81343
81344 assert( argc==3 );
81345 UNUSED_PARAMETER2(NotUsed, argc);
81346
@@ -81354,45 +82198,35 @@
81354 if( argv[1] ){
81355 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
81356 }else{
81357 pIndex = 0;
81358 }
81359 n = pIndex ? pIndex->nColumn : 0;
81360 z = argv[2];
81361 for(i=0; *z && i<=n; i++){
81362 v = 0;
81363 while( (c=z[0])>='0' && c<='9' ){
81364 v = v*10 + c - '0';
81365 z++;
81366 }
81367 if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
81368 if( v>0 ) pTable->nRowEst = v;
81369 if( pIndex==0 ) break;
81370 }
81371 pIndex->aiRowEst[i] = v;
81372 if( *z==' ' ) z++;
81373 if( strcmp(z, "unordered")==0 ){
81374 pIndex->bUnordered = 1;
81375 break;
81376 }
81377 }
81378 return 0;
81379 }
81380
81381 /*
81382 ** If the Index.aSample variable is not NULL, delete the aSample[] array
81383 ** and its contents.
81384 */
81385 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
81386 #ifdef SQLITE_ENABLE_STAT3
81387 if( pIdx->aSample ){
81388 int j;
81389 for(j=0; j<pIdx->nSample; j++){
81390 IndexSample *p = &pIdx->aSample[j];
81391 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
81392 sqlite3DbFree(db, p->u.z);
81393 }
81394 }
81395 sqlite3DbFree(db, pIdx->aSample);
81396 }
81397 if( db && db->pnBytesFreed==0 ){
81398 pIdx->nSample = 0;
@@ -81399,155 +82233,222 @@
81399 pIdx->aSample = 0;
81400 }
81401 #else
81402 UNUSED_PARAMETER(db);
81403 UNUSED_PARAMETER(pIdx);
81404 #endif
81405 }
81406
81407 #ifdef SQLITE_ENABLE_STAT3
81408 /*
81409 ** Load content from the sqlite_stat3 table into the Index.aSample[]
81410 ** arrays of all indices.
81411 */
81412 static int loadStat3(sqlite3 *db, const char *zDb){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81413 int rc; /* Result codes from subroutines */
81414 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
81415 char *zSql; /* Text of the SQL statement */
81416 Index *pPrevIdx = 0; /* Previous index in the loop */
81417 int idx = 0; /* slot in pIdx->aSample[] for next sample */
81418 int eType; /* Datatype of a sample */
81419 IndexSample *pSample; /* A slot in pIdx->aSample[] */
81420
81421 assert( db->lookaside.bEnabled==0 );
81422 if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
81423 return SQLITE_OK;
81424 }
81425
81426 zSql = sqlite3MPrintf(db,
81427 "SELECT idx,count(*) FROM %Q.sqlite_stat3"
81428 " GROUP BY idx", zDb);
81429 if( !zSql ){
81430 return SQLITE_NOMEM;
81431 }
81432 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
81433 sqlite3DbFree(db, zSql);
81434 if( rc ) return rc;
81435
81436 while( sqlite3_step(pStmt)==SQLITE_ROW ){
 
 
 
81437 char *zIndex; /* Index name */
81438 Index *pIdx; /* Pointer to the index object */
81439 int nSample; /* Number of samples */
 
 
 
81440
81441 zIndex = (char *)sqlite3_column_text(pStmt, 0);
81442 if( zIndex==0 ) continue;
81443 nSample = sqlite3_column_int(pStmt, 1);
81444 pIdx = sqlite3FindIndex(db, zIndex, zDb);
81445 if( pIdx==0 ) continue;
81446 assert( pIdx->nSample==0 );
81447 pIdx->nSample = nSample;
81448 pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
81449 pIdx->avgEq = pIdx->aiRowEst[1];
 
 
 
 
 
 
 
 
 
81450 if( pIdx->aSample==0 ){
81451 db->mallocFailed = 1;
81452 sqlite3_finalize(pStmt);
81453 return SQLITE_NOMEM;
81454 }
 
 
 
 
 
 
 
 
81455 }
81456 rc = sqlite3_finalize(pStmt);
81457 if( rc ) return rc;
81458
81459 zSql = sqlite3MPrintf(db,
81460 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
81461 if( !zSql ){
81462 return SQLITE_NOMEM;
81463 }
81464 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
81465 sqlite3DbFree(db, zSql);
81466 if( rc ) return rc;
81467
81468 while( sqlite3_step(pStmt)==SQLITE_ROW ){
81469 char *zIndex; /* Index name */
81470 Index *pIdx; /* Pointer to the index object */
81471 int i; /* Loop counter */
81472 tRowcnt sumEq; /* Sum of the nEq values */
81473
81474 zIndex = (char *)sqlite3_column_text(pStmt, 0);
81475 if( zIndex==0 ) continue;
81476 pIdx = sqlite3FindIndex(db, zIndex, zDb);
81477 if( pIdx==0 ) continue;
81478 if( pIdx==pPrevIdx ){
81479 idx++;
81480 }else{
 
 
 
81481 pPrevIdx = pIdx;
81482 idx = 0;
81483 }
81484 assert( idx<pIdx->nSample );
81485 pSample = &pIdx->aSample[idx];
81486 pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
81487 pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
81488 pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
81489 if( idx==pIdx->nSample-1 ){
81490 if( pSample->nDLt>0 ){
81491 for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
81492 pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
81493 }
81494 if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
81495 }
81496 eType = sqlite3_column_type(pStmt, 4);
81497 pSample->eType = (u8)eType;
81498 switch( eType ){
81499 case SQLITE_INTEGER: {
81500 pSample->u.i = sqlite3_column_int64(pStmt, 4);
81501 break;
81502 }
81503 case SQLITE_FLOAT: {
81504 pSample->u.r = sqlite3_column_double(pStmt, 4);
81505 break;
81506 }
81507 case SQLITE_NULL: {
81508 break;
81509 }
81510 default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
81511 const char *z = (const char *)(
81512 (eType==SQLITE_BLOB) ?
81513 sqlite3_column_blob(pStmt, 4):
81514 sqlite3_column_text(pStmt, 4)
81515 );
81516 int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
81517 pSample->nByte = n;
81518 if( n < 1){
81519 pSample->u.z = 0;
81520 }else{
81521 pSample->u.z = sqlite3DbMallocRaw(db, n);
81522 if( pSample->u.z==0 ){
81523 db->mallocFailed = 1;
81524 sqlite3_finalize(pStmt);
81525 return SQLITE_NOMEM;
81526 }
81527 memcpy(pSample->u.z, z, n);
81528 }
81529 }
81530 }
81531 }
81532 return sqlite3_finalize(pStmt);
81533 }
81534 #endif /* SQLITE_ENABLE_STAT3 */
81535
81536 /*
81537 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
81538 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
81539 ** arrays. The contents of sqlite_stat3 are used to populate the
81540 ** Index.aSample[] arrays.
81541 **
81542 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
81543 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
81544 ** during compilation and the sqlite_stat3 table is present, no data is
81545 ** read from it.
81546 **
81547 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
81548 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
81549 ** returned. However, in this case, data is read from the sqlite_stat1
81550 ** table (if it is present) before returning.
81551 **
81552 ** If an OOM error occurs, this function always sets db->mallocFailed.
81553 ** This means if the caller does not care about other errors, the return
@@ -81565,11 +82466,11 @@
81565 /* Clear any prior statistics */
81566 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81567 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
81568 Index *pIdx = sqliteHashData(i);
81569 sqlite3DefaultRowEst(pIdx);
81570 #ifdef SQLITE_ENABLE_STAT3
81571 sqlite3DeleteIndexSamples(db, pIdx);
81572 pIdx->aSample = 0;
81573 #endif
81574 }
81575
@@ -81589,16 +82490,16 @@
81589 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
81590 sqlite3DbFree(db, zSql);
81591 }
81592
81593
81594 /* Load the statistics from the sqlite_stat3 table. */
81595 #ifdef SQLITE_ENABLE_STAT3
81596 if( rc==SQLITE_OK ){
81597 int lookasideEnabled = db->lookaside.bEnabled;
81598 db->lookaside.bEnabled = 0;
81599 rc = loadStat3(db, sInfo.zDatabase);
81600 db->lookaside.bEnabled = lookasideEnabled;
81601 }
81602 #endif
81603
81604 if( rc==SQLITE_NOMEM ){
@@ -81769,10 +82670,13 @@
81769 }
81770 pPager = sqlite3BtreePager(aNew->pBt);
81771 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
81772 sqlite3BtreeSecureDelete(aNew->pBt,
81773 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
 
 
 
81774 }
81775 aNew->safety_level = 3;
81776 aNew->zName = sqlite3DbStrDup(db, zName);
81777 if( rc==SQLITE_OK && aNew->zName==0 ){
81778 rc = SQLITE_NOMEM;
@@ -84447,11 +85351,11 @@
84447 const char *zType, /* "idx" or "tbl" */
84448 const char *zName /* Name of index or table */
84449 ){
84450 int i;
84451 const char *zDbName = pParse->db->aDb[iDb].zName;
84452 for(i=1; i<=3; i++){
84453 char zTab[24];
84454 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
84455 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
84456 sqlite3NestedParse(pParse,
84457 "DELETE FROM %Q.%s WHERE %s=%Q",
@@ -85289,11 +86193,11 @@
85289
85290 /* Gather the complete text of the CREATE INDEX statement into
85291 ** the zStmt variable
85292 */
85293 if( pStart ){
85294 int n = (pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
85295 if( pName->z[n-1]==';' ) n--;
85296 /* A named index with an explicit CREATE INDEX statement */
85297 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
85298 onError==OE_None ? "" : " UNIQUE", n, pName->z);
85299 }else{
@@ -88926,13 +89830,13 @@
88926 }
88927 static void groupConcatFinalize(sqlite3_context *context){
88928 StrAccum *pAccum;
88929 pAccum = sqlite3_aggregate_context(context, 0);
88930 if( pAccum ){
88931 if( pAccum->tooBig ){
88932 sqlite3_result_error_toobig(context);
88933 }else if( pAccum->mallocFailed ){
88934 sqlite3_result_error_nomem(context);
88935 }else{
88936 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
88937 sqlite3_free);
88938 }
@@ -89118,10 +90022,13 @@
89118 sqlite3FuncDefInsert(pHash, &aFunc[i]);
89119 }
89120 sqlite3RegisterDateTimeFunctions();
89121 #ifndef SQLITE_OMIT_ALTERTABLE
89122 sqlite3AlterFunctions();
 
 
 
89123 #endif
89124 }
89125
89126 /************** End of func.c ************************************************/
89127 /************** Begin file fkey.c ********************************************/
@@ -93834,10 +94741,40 @@
93834 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
93835 sqlite3VdbeSetNumCols(v, 1);
93836 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
93837 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
93838 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93839
93840 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
93841 /*
93842 ** Check to see if zRight and zLeft refer to a pragma that queries
93843 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
@@ -93853,10 +94790,11 @@
93853 { "count_changes", SQLITE_CountRows },
93854 { "empty_result_callbacks", SQLITE_NullCallback },
93855 { "legacy_file_format", SQLITE_LegacyFileFmt },
93856 { "fullfsync", SQLITE_FullFSync },
93857 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
 
93858 { "reverse_unordered_selects", SQLITE_ReverseOrder },
93859 { "query_only", SQLITE_QueryOnly },
93860 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
93861 { "automatic_index", SQLITE_AutoIndex },
93862 #endif
@@ -94307,11 +95245,11 @@
94307 */
94308 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
94309 Pager *pPager = sqlite3BtreePager(pDb->pBt);
94310 i64 iLimit = -2;
94311 if( zRight ){
94312 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
94313 if( iLimit<-1 ) iLimit = -1;
94314 }
94315 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
94316 returnSingleInt(pParse, "journal_size_limit", iLimit);
94317 }else
@@ -94441,14 +95379,15 @@
94441 ** as little or as much as it wants. Except, if N is set to 0 then the
94442 ** upper layers will never invoke the xFetch interfaces to the VFS.
94443 */
94444 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
94445 sqlite3_int64 sz;
 
94446 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94447 if( zRight ){
94448 int ii;
94449 sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
94450 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
94451 if( pId2->n==0 ) db->szMmap = sz;
94452 for(ii=db->nDb-1; ii>=0; ii--){
94453 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
94454 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94455,12 +95394,13 @@
94455 }
94456 }
94457 }
94458 sz = -1;
94459 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
94460 #if SQLITE_MAX_MMAP_SIZE==0
94461 sz = 0;
 
94462 #endif
94463 if( rc==SQLITE_OK ){
94464 returnSingleInt(pParse, "mmap_size", sz);
94465 }else if( rc!=SQLITE_NOTFOUND ){
94466 pParse->nErr++;
@@ -94643,19 +95583,19 @@
94643 if( !db->autoCommit ){
94644 sqlite3ErrorMsg(pParse,
94645 "Safety level may not be changed inside a transaction");
94646 }else{
94647 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
 
94648 }
94649 }
94650 }else
94651 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
94652
94653 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
94654 if( flagPragma(pParse, zLeft, zRight) ){
94655 /* The flagPragma() subroutine also generates any necessary code
94656 ** there is nothing more to do here */
94657 }else
94658 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
94659
94660 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
94661 /*
@@ -95482,21 +96422,10 @@
95482 #endif
95483
95484
95485 {/* Empty ELSE clause */}
95486
95487 /*
95488 ** Reset the safety level, in case the fullfsync flag or synchronous
95489 ** setting changed.
95490 */
95491 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
95492 if( db->autoCommit ){
95493 sqlite3BtreeSetSafetyLevel(pDb->pBt, pDb->safety_level,
95494 (db->flags&SQLITE_FullFSync)!=0,
95495 (db->flags&SQLITE_CkptFullFSync)!=0);
95496 }
95497 #endif
95498 pragma_out:
95499 sqlite3DbFree(db, zLeft);
95500 sqlite3DbFree(db, zRight);
95501 }
95502
@@ -102619,11 +103548,11 @@
102619 ** space.
102620 */
102621 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
102622 assert( pTab!=0 );
102623 if( !pTab->pSelect ){
102624 sqlite3_value *pValue;
102625 u8 enc = ENC(sqlite3VdbeDb(v));
102626 Column *pCol = &pTab->aCol[i];
102627 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
102628 assert( i<pTab->nCol );
102629 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104395,14 +105324,13 @@
104395 /*
104396 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
104397 ** array. Return the error code for the first error that occurs, or
104398 ** SQLITE_OK if all xSync operations are successful.
104399 **
104400 ** Set *pzErrmsg to point to a buffer that should be released using
104401 ** sqlite3DbFree() containing an error message, if one is available.
104402 */
104403 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **pzErrmsg){
104404 int i;
104405 int rc = SQLITE_OK;
104406 VTable **aVTrans = db->aVTrans;
104407
104408 db->aVTrans = 0;
@@ -104409,13 +105337,11 @@
104409 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
104410 int (*x)(sqlite3_vtab *);
104411 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
104412 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
104413 rc = x(pVtab);
104414 sqlite3DbFree(db, *pzErrmsg);
104415 *pzErrmsg = pVtab->zErrMsg;
104416 pVtab->zErrMsg = 0;
104417 }
104418 }
104419 db->aVTrans = aVTrans;
104420 return rc;
104421 }
@@ -104776,10 +105702,11 @@
104776 int iIdxCur; /* The VDBE cursor used to access pIdx */
104777 int addrBrk; /* Jump here to break out of the loop */
104778 int addrNxt; /* Jump here to start the next IN combination */
104779 int addrCont; /* Jump here to continue with the next loop cycle */
104780 int addrFirst; /* First instruction of interior of the loop */
 
104781 u8 iFrom; /* Which entry in the FROM clause */
104782 u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
104783 int p1, p2; /* Operands of the opcode used to ends the loop */
104784 union { /* Information that depends on pWLoop->wsFlags */
104785 struct {
@@ -104971,11 +105898,11 @@
104971 #define TERM_CODED 0x04 /* This term is already coded */
104972 #define TERM_COPIED 0x08 /* Has a child */
104973 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
104974 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
104975 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
104976 #ifdef SQLITE_ENABLE_STAT3
104977 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
104978 #else
104979 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
104980 #endif
104981
@@ -105077,10 +106004,14 @@
105077 WhereInfo *pWInfo; /* Information about this WHERE */
105078 WhereClause *pWC; /* WHERE clause terms */
105079 ExprList *pOrderBy; /* ORDER BY clause */
105080 WhereLoop *pNew; /* Template WhereLoop */
105081 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
 
 
 
 
105082 };
105083
105084 /*
105085 ** The WHERE clause processing routine has two halves. The
105086 ** first part does the start of the WHERE loop and the second
@@ -105890,12 +106821,14 @@
105890 /*
105891 ** If the pBase expression originated in the ON or USING clause of
105892 ** a join, then transfer the appropriate markings over to derived.
105893 */
105894 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
105895 pDerived->flags |= pBase->flags & EP_FromJoin;
105896 pDerived->iRightJoinTable = pBase->iRightJoinTable;
 
 
105897 }
105898
105899 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
105900 /*
105901 ** Analyze a term that consists of two or more OR-connected
@@ -106348,10 +107281,11 @@
106348 Expr *pNewExpr;
106349 int idxNew;
106350 pNewExpr = sqlite3PExpr(pParse, ops[i],
106351 sqlite3ExprDup(db, pExpr->pLeft, 0),
106352 sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
 
106353 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
106354 testcase( idxNew==0 );
106355 exprAnalyze(pSrc, pWC, idxNew);
106356 pTerm = &pWC->a[idxTerm];
106357 pWC->a[idxNew].iParent = idxTerm;
@@ -106415,17 +107349,19 @@
106415 sCollSeqName.n = 6;
106416 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
106417 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
106418 sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
106419 pStr1, 0);
 
106420 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
106421 testcase( idxNew1==0 );
106422 exprAnalyze(pSrc, pWC, idxNew1);
106423 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
106424 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
106425 sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
106426 pStr2, 0);
 
106427 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
106428 testcase( idxNew2==0 );
106429 exprAnalyze(pSrc, pWC, idxNew2);
106430 pTerm = &pWC->a[idxTerm];
106431 if( isComplete ){
@@ -106471,11 +107407,11 @@
106471 pNewTerm->prereqAll = pTerm->prereqAll;
106472 }
106473 }
106474 #endif /* SQLITE_OMIT_VIRTUALTABLE */
106475
106476 #ifdef SQLITE_ENABLE_STAT3
106477 /* When sqlite_stat3 histogram data is available an operator of the
106478 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
106479 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
106480 ** virtual term of that form.
106481 **
@@ -106511,11 +107447,11 @@
106511 pTerm->nChild = 1;
106512 pTerm->wtFlags |= TERM_COPIED;
106513 pNewTerm->prereqAll = pTerm->prereqAll;
106514 }
106515 }
106516 #endif /* SQLITE_ENABLE_STAT */
106517
106518 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
106519 ** an index for tables to the left of the join.
106520 */
106521 pTerm->prereqRight |= extraRight;
@@ -107079,155 +108015,84 @@
107079 return pParse->nErr;
107080 }
107081 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
107082
107083
107084 #ifdef SQLITE_ENABLE_STAT3
107085 /*
107086 ** Estimate the location of a particular key among all keys in an
107087 ** index. Store the results in aStat as follows:
107088 **
107089 ** aStat[0] Est. number of rows less than pVal
107090 ** aStat[1] Est. number of rows equal to pVal
107091 **
107092 ** Return SQLITE_OK on success.
107093 */
107094 static int whereKeyStats(
107095 Parse *pParse, /* Database connection */
107096 Index *pIdx, /* Index to consider domain of */
107097 sqlite3_value *pVal, /* Value to consider */
107098 int roundUp, /* Round up if true. Round down if false */
107099 tRowcnt *aStat /* OUT: stats written here */
107100 ){
107101 tRowcnt n;
107102 IndexSample *aSample;
107103 int i, eType;
107104 int isEq = 0;
107105 i64 v;
107106 double r, rS;
107107
107108 assert( roundUp==0 || roundUp==1 );
107109 assert( pIdx->nSample>0 );
107110 if( pVal==0 ) return SQLITE_ERROR;
107111 n = pIdx->aiRowEst[0];
107112 aSample = pIdx->aSample;
107113 eType = sqlite3_value_type(pVal);
107114
107115 if( eType==SQLITE_INTEGER ){
107116 v = sqlite3_value_int64(pVal);
107117 r = (i64)v;
107118 for(i=0; i<pIdx->nSample; i++){
107119 if( aSample[i].eType==SQLITE_NULL ) continue;
107120 if( aSample[i].eType>=SQLITE_TEXT ) break;
107121 if( aSample[i].eType==SQLITE_INTEGER ){
107122 if( aSample[i].u.i>=v ){
107123 isEq = aSample[i].u.i==v;
107124 break;
107125 }
107126 }else{
107127 assert( aSample[i].eType==SQLITE_FLOAT );
107128 if( aSample[i].u.r>=r ){
107129 isEq = aSample[i].u.r==r;
107130 break;
107131 }
107132 }
107133 }
107134 }else if( eType==SQLITE_FLOAT ){
107135 r = sqlite3_value_double(pVal);
107136 for(i=0; i<pIdx->nSample; i++){
107137 if( aSample[i].eType==SQLITE_NULL ) continue;
107138 if( aSample[i].eType>=SQLITE_TEXT ) break;
107139 if( aSample[i].eType==SQLITE_FLOAT ){
107140 rS = aSample[i].u.r;
107141 }else{
107142 rS = aSample[i].u.i;
107143 }
107144 if( rS>=r ){
107145 isEq = rS==r;
107146 break;
107147 }
107148 }
107149 }else if( eType==SQLITE_NULL ){
107150 i = 0;
107151 if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
107152 }else{
107153 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
107154 for(i=0; i<pIdx->nSample; i++){
107155 if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
107156 break;
107157 }
107158 }
107159 if( i<pIdx->nSample ){
107160 sqlite3 *db = pParse->db;
107161 CollSeq *pColl;
107162 const u8 *z;
107163 if( eType==SQLITE_BLOB ){
107164 z = (const u8 *)sqlite3_value_blob(pVal);
107165 pColl = db->pDfltColl;
107166 assert( pColl->enc==SQLITE_UTF8 );
107167 }else{
107168 pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
107169 /* If the collating sequence was unavailable, we should have failed
107170 ** long ago and never reached this point. But we'll check just to
107171 ** be doubly sure. */
107172 if( NEVER(pColl==0) ) return SQLITE_ERROR;
107173 z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
107174 if( !z ){
107175 return SQLITE_NOMEM;
107176 }
107177 assert( z && pColl && pColl->xCmp );
107178 }
107179 n = sqlite3ValueBytes(pVal, pColl->enc);
107180
107181 for(; i<pIdx->nSample; i++){
107182 int c;
107183 int eSampletype = aSample[i].eType;
107184 if( eSampletype<eType ) continue;
107185 if( eSampletype!=eType ) break;
107186 #ifndef SQLITE_OMIT_UTF16
107187 if( pColl->enc!=SQLITE_UTF8 ){
107188 int nSample;
107189 char *zSample = sqlite3Utf8to16(
107190 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
107191 );
107192 if( !zSample ){
107193 assert( db->mallocFailed );
107194 return SQLITE_NOMEM;
107195 }
107196 c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
107197 sqlite3DbFree(db, zSample);
107198 }else
107199 #endif
107200 {
107201 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
107202 }
107203 if( c>=0 ){
107204 if( c==0 ) isEq = 1;
107205 break;
107206 }
107207 }
107208 }
107209 }
107210
107211 /* At this point, aSample[i] is the first sample that is greater than
107212 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
107213 ** than pVal. If aSample[i]==pVal, then isEq==1.
107214 */
107215 if( isEq ){
107216 assert( i<pIdx->nSample );
107217 aStat[0] = aSample[i].nLt;
107218 aStat[1] = aSample[i].nEq;
107219 }else{
107220 tRowcnt iLower, iUpper, iGap;
107221 if( i==0 ){
107222 iLower = 0;
107223 iUpper = aSample[0].nLt;
107224 }else{
107225 iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
107226 iLower = aSample[i-1].nEq + aSample[i-1].nLt;
107227 }
107228 aStat[1] = pIdx->avgEq;
107229 if( iLower>=iUpper ){
107230 iGap = 0;
107231 }else{
107232 iGap = iUpper - iLower;
107233 }
@@ -107236,48 +108101,12 @@
107236 }else{
107237 iGap = iGap/3;
107238 }
107239 aStat[0] = iLower + iGap;
107240 }
107241 return SQLITE_OK;
107242 }
107243 #endif /* SQLITE_ENABLE_STAT3 */
107244
107245 /*
107246 ** If expression pExpr represents a literal value, set *pp to point to
107247 ** an sqlite3_value structure containing the same value, with affinity
107248 ** aff applied to it, before returning. It is the responsibility of the
107249 ** caller to eventually release this structure by passing it to
107250 ** sqlite3ValueFree().
107251 **
107252 ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
107253 ** is an SQL variable that currently has a non-NULL value bound to it,
107254 ** create an sqlite3_value structure containing this value, again with
107255 ** affinity aff applied to it, instead.
107256 **
107257 ** If neither of the above apply, set *pp to NULL.
107258 **
107259 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
107260 */
107261 #ifdef SQLITE_ENABLE_STAT3
107262 static int valueFromExpr(
107263 Parse *pParse,
107264 Expr *pExpr,
107265 u8 aff,
107266 sqlite3_value **pp
107267 ){
107268 if( pExpr->op==TK_VARIABLE
107269 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
107270 ){
107271 int iVar = pExpr->iColumn;
107272 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
107273 *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff);
107274 return SQLITE_OK;
107275 }
107276 return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
107277 }
107278 #endif
107279
107280 /*
107281 ** This function is used to estimate the number of rows that will be visited
107282 ** by scanning an index for a range of values. The range may have an upper
107283 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107290,107 +108119,154 @@
107290 ** pLower pUpper
107291 **
107292 ** If either of the upper or lower bound is not present, then NULL is passed in
107293 ** place of the corresponding WhereTerm.
107294 **
107295 ** The nEq parameter is passed the index of the index column subject to the
107296 ** range constraint. Or, equivalently, the number of equality constraints
107297 ** optimized by the proposed index scan. For example, assuming index p is
107298 ** on t1(a, b), and the SQL query is:
107299 **
107300 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
107301 **
107302 ** then nEq should be passed the value 1 (as the range restricted column,
107303 ** b, is the second left-most column of the index). Or, if the query is:
107304 **
107305 ** ... FROM t1 WHERE a > ? AND a < ? ...
107306 **
107307 ** then nEq should be passed 0.
107308 **
107309 ** The returned value is an integer divisor to reduce the estimated
107310 ** search space. A return value of 1 means that range constraints are
107311 ** no help at all. A return value of 2 means range constraints are
107312 ** expected to reduce the search space by half. And so forth...
107313 **
107314 ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
107315 ** reduces the search space by a factor of 4. Hence a single constraint (x>?)
107316 ** results in a return of 4 and a range constraint (x>? AND x<?) results
107317 ** in a return of 16.
 
107318 */
107319 static int whereRangeScanEst(
107320 Parse *pParse, /* Parsing & code generating context */
107321 Index *p, /* The index containing the range-compared column; "x" */
107322 int nEq, /* index into p->aCol[] of the range-compared column */
107323 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
107324 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
107325 WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
107326 ){
107327 int rc = SQLITE_OK;
 
107328
107329 #ifdef SQLITE_ENABLE_STAT3
 
 
107330
107331 if( nEq==0 && p->nSample && OptimizationEnabled(pParse->db, SQLITE_Stat3) ){
107332 sqlite3_value *pRangeVal;
107333 tRowcnt iLower = 0;
107334 tRowcnt iUpper = p->aiRowEst[0];
 
 
107335 tRowcnt a[2];
107336 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107337
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107338 if( pLower ){
 
107339 Expr *pExpr = pLower->pExpr->pRight;
107340 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
107341 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
107342 if( rc==SQLITE_OK
107343 && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
107344 ){
107345 iLower = a[0];
107346 if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1];
107347 }
107348 sqlite3ValueFree(pRangeVal);
107349 }
107350 if( rc==SQLITE_OK && pUpper ){
107351 Expr *pExpr = pUpper->pExpr->pRight;
107352 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
 
 
107353 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
107354 if( rc==SQLITE_OK
107355 && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
107356 ){
107357 iUpper = a[0];
107358 if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
107359 }
107360 sqlite3ValueFree(pRangeVal);
107361 }
 
 
107362 if( rc==SQLITE_OK ){
107363 WhereCost iBase = whereCost(p->aiRowEst[0]);
107364 if( iUpper>iLower ){
107365 iBase -= whereCost(iUpper - iLower);
 
 
107366 }
107367 *pRangeDiv = iBase;
107368 WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n",
107369 (u32)iLower, (u32)iUpper, *pRangeDiv));
 
 
 
107370 return SQLITE_OK;
107371 }
107372 }
107373 #else
107374 UNUSED_PARAMETER(pParse);
107375 UNUSED_PARAMETER(p);
107376 UNUSED_PARAMETER(nEq);
107377 #endif
107378 assert( pLower || pUpper );
107379 *pRangeDiv = 0;
107380 /* TUNING: Each inequality constraint reduces the search space 4-fold.
107381 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
107382 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
107383 *pRangeDiv += 20; assert( 20==whereCost(4) );
107384 }
107385 if( pUpper ){
107386 *pRangeDiv += 20; assert( 20==whereCost(4) );
107387 }
 
 
107388 return rc;
107389 }
107390
107391 #ifdef SQLITE_ENABLE_STAT3
107392 /*
107393 ** Estimate the number of rows that will be returned based on
107394 ** an equality constraint x=VALUE and where that VALUE occurs in
107395 ** the histogram data. This only works when x is the left-most
107396 ** column of an index and sqlite_stat3 histogram data is available
@@ -107406,41 +108282,57 @@
107406 ** for a UTF conversion required for comparison. The error is stored
107407 ** in the pParse structure.
107408 */
107409 static int whereEqualScanEst(
107410 Parse *pParse, /* Parsing & code generating context */
107411 Index *p, /* The index whose left-most column is pTerm */
107412 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
107413 tRowcnt *pnRow /* Write the revised row estimate here */
107414 ){
107415 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
 
 
107416 u8 aff; /* Column affinity */
107417 int rc; /* Subfunction return code */
107418 tRowcnt a[2]; /* Statistics */
 
107419
 
 
107420 assert( p->aSample!=0 );
107421 assert( p->nSample>0 );
107422 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107423 if( pExpr ){
107424 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
107425 if( rc ) goto whereEqualScanEst_cancel;
107426 }else{
107427 pRhs = sqlite3ValueNew(pParse->db);
107428 }
107429 if( pRhs==0 ) return SQLITE_NOTFOUND;
107430 rc = whereKeyStats(pParse, p, pRhs, 0, a);
107431 if( rc==SQLITE_OK ){
107432 WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
107433 *pnRow = a[1];
107434 }
107435 whereEqualScanEst_cancel:
107436 sqlite3ValueFree(pRhs);
 
 
 
 
 
 
 
 
 
 
 
107437 return rc;
107438 }
107439 #endif /* defined(SQLITE_ENABLE_STAT3) */
107440
107441 #ifdef SQLITE_ENABLE_STAT3
107442 /*
107443 ** Estimate the number of rows that will be returned based on
107444 ** an IN constraint where the right-hand side of the IN operator
107445 ** is a list of values. Example:
107446 **
@@ -107455,33 +108347,38 @@
107455 ** for a UTF conversion required for comparison. The error is stored
107456 ** in the pParse structure.
107457 */
107458 static int whereInScanEst(
107459 Parse *pParse, /* Parsing & code generating context */
107460 Index *p, /* The index whose left-most column is pTerm */
107461 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
107462 tRowcnt *pnRow /* Write the revised row estimate here */
107463 ){
 
 
107464 int rc = SQLITE_OK; /* Subfunction return code */
107465 tRowcnt nEst; /* Number of rows for a single term */
107466 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
107467 int i; /* Loop counter */
107468
107469 assert( p->aSample!=0 );
107470 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
107471 nEst = p->aiRowEst[0];
107472 rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
107473 nRowEst += nEst;
 
107474 }
 
107475 if( rc==SQLITE_OK ){
107476 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
107477 *pnRow = nRowEst;
107478 WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
107479 }
 
107480 return rc;
107481 }
107482 #endif /* defined(SQLITE_ENABLE_STAT3) */
107483
107484 /*
107485 ** Disable a term in the WHERE clause. Except, do not disable the term
107486 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
107487 ** or USING clause of that join.
@@ -107715,11 +108612,11 @@
107715 pParse->db->mallocFailed = 1;
107716 }
107717
107718 /* Evaluate the equality constraints
107719 */
107720 assert( pIdx->nColumn>=nEq );
107721 for(j=0; j<nEq; j++){
107722 int r1;
107723 pTerm = pLoop->aLTerm[j];
107724 assert( pTerm!=0 );
107725 /* The following true for indices with redundant columns.
@@ -107807,11 +108704,12 @@
107807 }
107808 sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
107809 txt.db = db;
107810 sqlite3StrAccumAppend(&txt, " (", 2);
107811 for(i=0; i<nEq; i++){
107812 explainAppendTerm(&txt, i, aCol[aiColumn[i]].zName, "=");
 
107813 }
107814
107815 j = i;
107816 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
107817 char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
@@ -109021,16 +109919,22 @@
109021 saved_nOut = pNew->nOut;
109022 pNew->rSetup = 0;
109023 rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
109024 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
109025 int nIn = 0;
 
 
 
 
 
 
 
 
109026 if( pTerm->prereqRight & pNew->maskSelf ) continue;
109027 #ifdef SQLITE_ENABLE_STAT3
109028 if( (pTerm->wtFlags & TERM_VNULL)!=0 && pSrc->pTab->aCol[iCol].notNull ){
109029 continue; /* skip IS NOT NULL constraints on a NOT NULL column */
109030 }
109031 #endif
109032 pNew->wsFlags = saved_wsFlags;
109033 pNew->u.btree.nEq = saved_nEq;
109034 pNew->nLTerm = saved_nLTerm;
109035 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
109036 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109083,29 +109987,34 @@
109083 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
109084 pNew->aLTerm[pNew->nLTerm-2] : 0;
109085 }
109086 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
109087 /* Adjust nOut and rRun for STAT3 range values */
109088 WhereCost rDiv;
109089 whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
109090 pBtm, pTop, &rDiv);
109091 pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
109092 }
109093 #ifdef SQLITE_ENABLE_STAT3
109094 if( pNew->u.btree.nEq==1 && pProbe->nSample
109095 && OptimizationEnabled(db, SQLITE_Stat3) ){
 
 
109096 tRowcnt nOut = 0;
109097 if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
109098 testcase( pTerm->eOperator & WO_EQ );
109099 testcase( pTerm->eOperator & WO_ISNULL );
109100 rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
109101 }else if( (pTerm->eOperator & WO_IN)
109102 && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){
109103 rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
109104 }
109105 assert( nOut==0 || rc==SQLITE_OK );
109106 if( nOut ) pNew->nOut = whereCost(nOut);
 
 
 
109107 }
109108 #endif
109109 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
109110 /* Each row involves a step of the index, then a binary search of
109111 ** the main table */
@@ -109118,10 +110027,14 @@
109118 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
109119 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
109120 ){
109121 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
109122 }
 
 
 
 
109123 }
109124 pNew->prereq = saved_prereq;
109125 pNew->u.btree.nEq = saved_nEq;
109126 pNew->wsFlags = saved_wsFlags;
109127 pNew->nOut = saved_nOut;
@@ -109166,10 +110079,11 @@
109166 static Bitmask columnsInIndex(Index *pIdx){
109167 Bitmask m = 0;
109168 int j;
109169 for(j=pIdx->nColumn-1; j>=0; j--){
109170 int x = pIdx->aiColumn[j];
 
109171 testcase( x==BMS-1 );
109172 testcase( x==BMS-2 );
109173 if( x<BMS-1 ) m |= MASKBIT(x);
109174 }
109175 return m;
@@ -109244,10 +110158,11 @@
109244 pProbe = &sPk;
109245 }
109246 rSize = whereCost(pSrc->pTab->nRowEst);
109247 rLogSize = estLog(rSize);
109248
 
109249 /* Automatic indexes */
109250 if( !pBuilder->pOrSet
109251 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
109252 && pSrc->pIndex==0
109253 && !pSrc->viaCoroutine
@@ -109278,10 +110193,11 @@
109278 pNew->prereq = mExtra | pTerm->prereqRight;
109279 rc = whereLoopInsert(pBuilder, pNew);
109280 }
109281 }
109282 }
 
109283
109284 /* Loop over all indices
109285 */
109286 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
109287 if( pProbe->pPartIdxWhere!=0
@@ -109344,11 +110260,17 @@
109344 }
109345 rc = whereLoopInsert(pBuilder, pNew);
109346 if( rc ) break;
109347 }
109348 }
 
109349 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
 
 
 
 
 
109350
109351 /* If there was an INDEXED BY clause, then only that one index is
109352 ** considered. */
109353 if( pSrc->pIndex ) break;
109354 }
@@ -109541,10 +110463,11 @@
109541
109542 pWC = pBuilder->pWC;
109543 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
109544 pWCEnd = pWC->a + pWC->nTerm;
109545 pNew = pBuilder->pNew;
 
109546
109547 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
109548 if( (pTerm->eOperator & WO_OR)!=0
109549 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
109550 ){
@@ -110226,15 +111149,19 @@
110226 pLoop->u.btree.nEq = 1;
110227 /* TUNING: Cost of a rowid lookup is 10 */
110228 pLoop->rRun = 33; /* 33==whereCost(10) */
110229 }else{
110230 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110231 if( pIdx->onError==OE_None || pIdx->pPartIdxWhere!=0 ) continue;
 
 
 
 
 
110232 for(j=0; j<pIdx->nColumn; j++){
110233 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
110234 if( pTerm==0 ) break;
110235 whereLoopResize(pWInfo->pParse->db, pLoop, j);
110236 pLoop->aLTerm[j] = pTerm;
110237 }
110238 if( j!=pIdx->nColumn ) continue;
110239 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
110240 if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -110667,15 +111594,10 @@
110667 assert( n<=pTab->nCol );
110668 }
110669 }else{
110670 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
110671 }
110672 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110673 if( (pLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
110674 constructAutomaticIndex(pParse, &pWInfo->sWC, pTabItem, notReady, pLevel);
110675 }else
110676 #endif
110677 if( pLoop->wsFlags & WHERE_INDEXED ){
110678 Index *pIx = pLoop->u.btree.pIndex;
110679 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
110680 /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
110681 int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
@@ -110696,11 +111618,19 @@
110696 ** program.
110697 */
110698 notReady = ~(Bitmask)0;
110699 for(ii=0; ii<nTabList; ii++){
110700 pLevel = &pWInfo->a[ii];
 
 
 
 
 
 
 
110701 explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
 
110702 notReady = codeOneLoopStart(pWInfo, ii, notReady);
110703 pWInfo->iContinue = pLevel->addrCont;
110704 }
110705
110706 /* Done. */
@@ -110816,13 +111746,14 @@
110816 }
110817 if( pIdx && !db->mallocFailed ){
110818 int k, j, last;
110819 VdbeOp *pOp;
110820
110821 pOp = sqlite3VdbeGetOp(v, pWInfo->iTop);
110822 last = sqlite3VdbeCurrentAddr(v);
110823 for(k=pWInfo->iTop; k<last; k++, pOp++){
 
 
110824 if( pOp->p1!=pLevel->iTabCur ) continue;
110825 if( pOp->opcode==OP_Column ){
110826 for(j=0; j<pIdx->nColumn; j++){
110827 if( pOp->p2==pIdx->aiColumn[j] ){
110828 pOp->p2 = j;
@@ -117967,11 +118898,11 @@
117967 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
117968 db->autoCommit = 1;
117969 db->nextAutovac = -1;
117970 db->szMmap = sqlite3GlobalConfig.szMmap;
117971 db->nextPagesize = 0;
117972 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger
117973 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
117974 | SQLITE_AutoIndex
117975 #endif
117976 #if SQLITE_DEFAULT_FILE_FORMAT<4
117977 | SQLITE_LegacyFileFmt
@@ -130880,11 +131811,11 @@
130880 while( 1 ){
130881
130882 /* The following line of code (and the "p++" below the while() loop) is
130883 ** normally all that is required to move pointer p to the desired
130884 ** position. The exception is if this node is being loaded from disk
130885 ** incrementally and pointer "p" now points to the first byte passed
130886 ** the populated part of pReader->aNode[].
130887 */
130888 while( *p | c ) c = *p++ & 0x80;
130889 assert( *p==0 );
130890
@@ -132267,12 +133198,12 @@
132267 fts3SegReaderFirstDocid(p, apSegment[i]);
132268 }
132269 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
132270 while( apSegment[0]->pOffsetList ){
132271 int j; /* Number of segments that share a docid */
132272 char *pList;
132273 int nList;
132274 int nByte;
132275 sqlite3_int64 iDocid = apSegment[0]->iDocid;
132276 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
132277 j = 1;
132278 while( j<nMerge
@@ -134554,15 +135485,15 @@
134554 }
134555 }
134556 if( pTC ) pModule->xClose(pTC);
134557 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
134558 }
 
134559
134560 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
134561 if( pDef->pList ){
134562 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
134563 }
134564 }
134565 }
134566 }
134567
134568 return rc;
@@ -135341,10 +136272,11 @@
135341 return SQLITE_NOMEM;
135342 }
135343 pStr->z = zNew;
135344 pStr->nAlloc = nAlloc;
135345 }
 
135346
135347 /* Append the data to the string buffer. */
135348 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
135349 pStr->n += nAppend;
135350 pStr->z[pStr->n] = '\0';
135351
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.1. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -654,13 +654,13 @@
654 **
655 ** See also: [sqlite3_libversion()],
656 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
657 ** [sqlite_version()] and [sqlite_source_id()].
658 */
659 #define SQLITE_VERSION "3.8.1"
660 #define SQLITE_VERSION_NUMBER 3008001
661 #define SQLITE_SOURCE_ID "2013-08-30 06:20:23 d9c018f8155ab48df8e0e02519bba50588fe49fc"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -8368,10 +8368,24 @@
8368 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8369 # undef SQLITE_DEFAULT_MMAP_SIZE
8370 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8371 #endif
8372
8373 /*
8374 ** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8375 ** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
8376 ** define SQLITE_ENABLE_STAT3_OR_STAT4
8377 */
8378 #ifdef SQLITE_ENABLE_STAT4
8379 # undef SQLITE_ENABLE_STAT3
8380 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8381 #elif SQLITE_ENABLE_STAT3
8382 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8383 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8384 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8385 #endif
8386
8387 /*
8388 ** An instance of the following structure is used to store the busy-handler
8389 ** callback for a given sqlite handle.
8390 **
8391 ** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -8587,11 +8601,11 @@
8601 #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
8602
8603 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8604 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8605 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8606 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
8607 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8608 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8609 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8610 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8611 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
@@ -9288,12 +9302,24 @@
9302 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
9303
9304 /*
9305 ** Flags that make up the mask passed to sqlite3PagerAcquire().
9306 */
9307 #define PAGER_GET_NOCONTENT 0x01 /* Do not load data from disk */
9308 #define PAGER_GET_READONLY 0x02 /* Read-only page is acceptable */
9309
9310 /*
9311 ** Flags for sqlite3PagerSetFlags()
9312 */
9313 #define PAGER_SYNCHRONOUS_OFF 0x01 /* PRAGMA synchronous=OFF */
9314 #define PAGER_SYNCHRONOUS_NORMAL 0x02 /* PRAGMA synchronous=NORMAL */
9315 #define PAGER_SYNCHRONOUS_FULL 0x03 /* PRAGMA synchronous=FULL */
9316 #define PAGER_SYNCHRONOUS_MASK 0x03 /* Mask for three values above */
9317 #define PAGER_FULLFSYNC 0x04 /* PRAGMA fullfsync=ON */
9318 #define PAGER_CKPT_FULLFSYNC 0x08 /* PRAGMA checkpoint_fullfsync=ON */
9319 #define PAGER_CACHESPILL 0x10 /* PRAGMA cache_spill=ON */
9320 #define PAGER_FLAGS_MASK 0x1c /* All above except SYNCHRONOUS */
9321
9322 /*
9323 ** The remainder of this file contains the declarations of the functions
9324 ** that make up the Pager sub-system API. See source code comments for
9325 ** a detailed description of each routine.
@@ -9317,11 +9343,11 @@
9343 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9344 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9345 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9346 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
9347 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9348 SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
9349 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9350 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9351 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
9352 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
9353 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
@@ -10180,36 +10206,37 @@
10206 /*
10207 ** Possible values for the sqlite3.flags.
10208 */
10209 #define SQLITE_VdbeTrace 0x00000001 /* True to trace VDBE execution */
10210 #define SQLITE_InternChanges 0x00000002 /* Uncommitted Hash table changes */
10211 #define SQLITE_FullFSync 0x00000004 /* Use full fsync on the backend */
10212 #define SQLITE_CkptFullFSync 0x00000008 /* Use full fsync for checkpoint */
10213 #define SQLITE_CacheSpill 0x00000010 /* OK to spill pager cache */
10214 #define SQLITE_FullColNames 0x00000020 /* Show full column names on SELECT */
10215 #define SQLITE_ShortColNames 0x00000040 /* Show short columns names */
10216 #define SQLITE_CountRows 0x00000080 /* Count rows changed by INSERT, */
10217 /* DELETE, or UPDATE and return */
10218 /* the count using a callback. */
10219 #define SQLITE_NullCallback 0x00000100 /* Invoke the callback once if the */
10220 /* result set is empty */
10221 #define SQLITE_SqlTrace 0x00000200 /* Debug print SQL as it executes */
10222 #define SQLITE_VdbeListing 0x00000400 /* Debug listings of VDBE programs */
10223 #define SQLITE_WriteSchema 0x00000800 /* OK to update SQLITE_MASTER */
10224 #define SQLITE_VdbeAddopTrace 0x00001000 /* Trace sqlite3VdbeAddOp() calls */
10225 #define SQLITE_IgnoreChecks 0x00002000 /* Do not enforce check constraints */
10226 #define SQLITE_ReadUncommitted 0x0004000 /* For shared-cache mode */
10227 #define SQLITE_LegacyFileFmt 0x00008000 /* Create new databases in format 1 */
10228 #define SQLITE_RecoveryMode 0x00010000 /* Ignore schema errors */
10229 #define SQLITE_ReverseOrder 0x00020000 /* Reverse unordered SELECTs */
10230 #define SQLITE_RecTriggers 0x00040000 /* Enable recursive triggers */
10231 #define SQLITE_ForeignKeys 0x00080000 /* Enforce foreign key constraints */
10232 #define SQLITE_AutoIndex 0x00100000 /* Enable automatic indexes */
10233 #define SQLITE_PreferBuiltin 0x00200000 /* Preference to built-in funcs */
10234 #define SQLITE_LoadExtension 0x00400000 /* Enable load_extension */
10235 #define SQLITE_EnableTrigger 0x00800000 /* True to enable triggers */
10236 #define SQLITE_DeferFKs 0x01000000 /* Defer all FK constraints */
10237 #define SQLITE_QueryOnly 0x02000000 /* Disable database changes */
 
 
10238
10239
10240 /*
10241 ** Bits of the sqlite3.dbOptFlags field that are used by the
10242 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
@@ -10757,13 +10784,14 @@
10784 u16 nColumn; /* Number of columns in table used by this index */
10785 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10786 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10787 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10788 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10789 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
10790 int nSample; /* Number of elements in aSample[] */
10791 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
10792 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
10793 IndexSample *aSample; /* Samples of the left-most key */
10794 #endif
10795 };
10796
10797 /*
@@ -10770,20 +10798,15 @@
10798 ** Each sample stored in the sqlite_stat3 table is represented in memory
10799 ** using a structure of this type. See documentation at the top of the
10800 ** analyze.c source file for additional information.
10801 */
10802 struct IndexSample {
10803 void *p; /* Pointer to sampled record */
10804 int n; /* Size of record in bytes */
10805 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
10806 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
10807 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
 
 
 
 
 
10808 };
10809
10810 /*
10811 ** Each token coming out of the lexer is an instance of
10812 ** this structure. Tokens are also used as part of an expression.
@@ -11632,14 +11655,15 @@
11655 char *zBase; /* A base allocation. Not from malloc. */
11656 char *zText; /* The string collected so far */
11657 int nChar; /* Length of the string so far */
11658 int nAlloc; /* Amount of space allocated in zText */
11659 int mxAlloc; /* Maximum allowed string length */
 
11660 u8 useMalloc; /* 0: none, 1: sqlite3DbMalloc, 2: sqlite3_malloc */
11661 u8 accError; /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
11662 };
11663 #define STRACCUM_NOMEM 1
11664 #define STRACCUM_TOOBIG 2
11665
11666 /*
11667 ** A pointer to this structure is used to communicate information
11668 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
11669 */
@@ -12250,13 +12274,10 @@
12274 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12275 void(*)(void*));
12276 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12277 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12278 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 
 
 
12279 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12280 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12281 #ifndef SQLITE_AMALGAMATION
12282 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12283 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12319,10 +12340,16 @@
12340 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12341
12342 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12343 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12344
12345 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12346 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12347 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12348 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12349 #endif
12350
12351 /*
12352 ** The interface to the LEMON-generated parser
12353 */
12354 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
12355 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
@@ -12360,17 +12387,18 @@
12387 # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
12388 # define sqlite3GetVTable(X,Y) ((VTable*)0)
12389 #else
12390 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
12391 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
12392 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe*);
12393 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
12394 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
12395 SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
12396 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
12397 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
12398 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
12399 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
12400 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
12401 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
12402 #endif
12403 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
12404 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
@@ -12901,11 +12929,13 @@
12929 "ENABLE_OVERSIZE_CELL_CHECK",
12930 #endif
12931 #ifdef SQLITE_ENABLE_RTREE
12932 "ENABLE_RTREE",
12933 #endif
12934 #if defined(SQLITE_ENABLE_STAT4)
12935 "ENABLE_STAT4",
12936 #elif defined(SQLITE_ENABLE_STAT3)
12937 "ENABLE_STAT3",
12938 #endif
12939 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12940 "ENABLE_UNLOCK_NOTIFY",
12941 #endif
@@ -13473,14 +13503,15 @@
13503 struct sqlite3_context {
13504 FuncDef *pFunc; /* Pointer to function information. MUST BE FIRST */
13505 Mem s; /* The return value is stored here */
13506 Mem *pMem; /* Memory cell used to store aggregate context */
13507 CollSeq *pColl; /* Collating sequence */
13508 Vdbe *pVdbe; /* The VM that owns this context */
 
13509 int iOp; /* Instruction number of OP_Function */
13510 int isError; /* Error code returned by the function. */
13511 u8 skipFlag; /* Skip skip accumulator loading if true */
13512 u8 fErrorOrAux; /* isError!=0 or pVdbe->pAuxData modified */
13513 };
13514
13515 /*
13516 ** An Explain object accumulates indented output which is helpful
13517 ** in describing recursive data structures.
@@ -13552,11 +13583,11 @@
13583 bft doingRerun:1; /* True if rerunning after an auto-reprepare */
13584 int nChange; /* Number of db changes made since last reset */
13585 yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
13586 yDbMask lockMask; /* Subset of btreeMask that requires a lock */
13587 int iStatement; /* Statement number (or 0 if has not opened stmt) */
13588 u32 aCounter[5]; /* Counters used by sqlite3_stmt_status() */
13589 #ifndef SQLITE_OMIT_TRACE
13590 i64 startTime; /* Time when query started - used for profiling */
13591 #endif
13592 i64 nFkConstraint; /* Number of imm. FK constraints this VM */
13593 i64 nStmtDefCons; /* Number of def. constraints when stmt started */
@@ -16059,11 +16090,11 @@
16090 struct MemBlockHdr *pHdr;
16091 if( !p ){
16092 return 0;
16093 }
16094 pHdr = sqlite3MemsysGetHeader(p);
16095 return (int)pHdr->iSize;
16096 }
16097
16098 /*
16099 ** Initialize the memory allocation subsystem.
16100 */
@@ -16101,19 +16132,19 @@
16132 static void randomFill(char *pBuf, int nByte){
16133 unsigned int x, y, r;
16134 x = SQLITE_PTR_TO_INT(pBuf);
16135 y = nByte | 1;
16136 while( nByte >= 4 ){
16137 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16138 y = y*1103515245 + 12345;
16139 r = x ^ y;
16140 *(int*)pBuf = r;
16141 pBuf += 4;
16142 nByte -= 4;
16143 }
16144 while( nByte-- > 0 ){
16145 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16146 y = y*1103515245 + 12345;
16147 r = x ^ y;
16148 *(pBuf++) = r & 0xff;
16149 }
16150 }
@@ -16204,13 +16235,13 @@
16235 assert( mem.pLast==pHdr );
16236 mem.pLast = pHdr->pPrev;
16237 }
16238 z = (char*)pBt;
16239 z -= pHdr->nTitle;
16240 adjustStats((int)pHdr->iSize, -1);
16241 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16242 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
16243 free(z);
16244 sqlite3_mutex_leave(mem.mutex);
16245 }
16246
16247 /*
@@ -16230,11 +16261,11 @@
16261 pOldHdr = sqlite3MemsysGetHeader(pPrior);
16262 pNew = sqlite3MemMalloc(nByte);
16263 if( pNew ){
16264 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
16265 if( nByte>pOldHdr->iSize ){
16266 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
16267 }
16268 sqlite3MemFree(pPrior);
16269 }
16270 return pNew;
16271 }
@@ -16345,11 +16376,11 @@
16376 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16377 struct MemBlockHdr *pHdr;
16378 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16379 void **pBt = (void**)pHdr;
16380 pBt -= pHdr->nBacktraceSlots;
16381 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16382 }
16383 }
16384
16385 /*
16386 ** Open the file indicated and write a log of all unfreed memory
@@ -18467,11 +18498,11 @@
18498 GetVersionEx(&sInfo);
18499 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18500 }
18501 return osType==2;
18502 }
18503 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
18504 #endif
18505
18506 #ifdef SQLITE_DEBUG
18507 /*
18508 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18505,11 +18536,11 @@
18536 /* As winMutexInit() and winMutexEnd() are called as part
18537 ** of the sqlite3_initialize and sqlite3_shutdown()
18538 ** processing, the "interlocked" magic is probably not
18539 ** strictly necessary.
18540 */
18541 static LONG winMutex_lock = 0;
18542
18543 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18544
18545 static int winMutexInit(void){
18546 /* The first to increment to 1 does actual initialization */
@@ -19885,11 +19916,11 @@
19916 zOut = buf;
19917 }else{
19918 nOut = precision + 10;
19919 zOut = zExtra = sqlite3Malloc( nOut );
19920 if( zOut==0 ){
19921 pAccum->accError = STRACCUM_NOMEM;
19922 return;
19923 }
19924 }
19925 bufpt = &zOut[nOut-1];
19926 if( xtype==etORDINAL ){
@@ -19997,11 +20028,11 @@
20028 e2 = exp;
20029 }
20030 if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
20031 bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
20032 if( bufpt==0 ){
20033 pAccum->accError = STRACCUM_NOMEM;
20034 return;
20035 }
20036 }
20037 zOut = bufpt;
20038 nsd = 16 + flag_altform2*10;
@@ -20132,11 +20163,11 @@
20163 needQuote = !isnull && xtype==etSQLESCAPE2;
20164 n += i + 1 + needQuote*2;
20165 if( n>etBUFSIZE ){
20166 bufpt = zExtra = sqlite3Malloc( n );
20167 if( bufpt==0 ){
20168 pAccum->accError = STRACCUM_NOMEM;
20169 return;
20170 }
20171 }else{
20172 bufpt = buf;
20173 }
@@ -20210,26 +20241,24 @@
20241 /*
20242 ** Append N bytes of text from z to the StrAccum object.
20243 */
20244 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
20245 assert( z!=0 || N==0 );
20246 if( p->accError ){
20247 testcase(p->accError==STRACCUM_TOOBIG);
20248 testcase(p->accError==STRACCUM_NOMEM);
20249 return;
20250 }
20251 assert( p->zText!=0 || p->nChar==0 );
20252 if( N<=0 ){
20253 if( N==0 || z[0]==0 ) return;
20254 N = sqlite3Strlen30(z);
20255 }
 
 
 
20256 if( p->nChar+N >= p->nAlloc ){
20257 char *zNew;
20258 if( !p->useMalloc ){
20259 p->accError = STRACCUM_TOOBIG;
20260 N = p->nAlloc - p->nChar - 1;
20261 if( N<=0 ){
20262 return;
20263 }
20264 }else{
@@ -20236,11 +20265,11 @@
20265 char *zOld = (p->zText==p->zBase ? 0 : p->zText);
20266 i64 szNew = p->nChar;
20267 szNew += N + 1;
20268 if( szNew > p->mxAlloc ){
20269 sqlite3StrAccumReset(p);
20270 p->accError = STRACCUM_TOOBIG;
20271 return;
20272 }else{
20273 p->nAlloc = (int)szNew;
20274 }
20275 if( p->useMalloc==1 ){
@@ -20250,11 +20279,11 @@
20279 }
20280 if( zNew ){
20281 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
20282 p->zText = zNew;
20283 }else{
20284 p->accError = STRACCUM_NOMEM;
20285 sqlite3StrAccumReset(p);
20286 return;
20287 }
20288 }
20289 }
@@ -20278,11 +20307,11 @@
20307 p->zText = sqlite3_malloc(p->nChar+1);
20308 }
20309 if( p->zText ){
20310 memcpy(p->zText, p->zBase, p->nChar+1);
20311 }else{
20312 p->accError = STRACCUM_NOMEM;
20313 }
20314 }
20315 }
20316 return p->zText;
20317 }
@@ -20309,12 +20338,11 @@
20338 p->db = 0;
20339 p->nChar = 0;
20340 p->nAlloc = n;
20341 p->mxAlloc = mx;
20342 p->useMalloc = 1;
20343 p->accError = 0;
 
20344 }
20345
20346 /*
20347 ** Print into memory obtained from sqliteMalloc(). Use the internal
20348 ** %-conversion extensions.
@@ -20327,11 +20355,11 @@
20355 sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
20356 db->aLimit[SQLITE_LIMIT_LENGTH]);
20357 acc.db = db;
20358 sqlite3VXPrintf(&acc, 1, zFormat, ap);
20359 z = sqlite3StrAccumFinish(&acc);
20360 if( acc.accError==STRACCUM_NOMEM ){
20361 db->mallocFailed = 1;
20362 }
20363 return z;
20364 }
20365
@@ -20524,28 +20552,15 @@
20552 unsigned char i, j; /* State variables */
20553 unsigned char s[256]; /* State variables */
20554 } sqlite3Prng;
20555
20556 /*
20557 ** Return N random bytes.
 
 
 
 
 
 
 
 
 
 
 
 
 
20558 */
20559 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
20560 unsigned char t;
20561 unsigned char *zBuf = pBuf;
20562
20563 /* The "wsdPrng" macro will resolve to the pseudo-random number generator
20564 ** state vector. If writable static data is unsupported on the target,
20565 ** we have to locate the state vector at run-time. In the more common
20566 ** case where writable static data is supported, wsdPrng can refer directly
@@ -20556,10 +20571,14 @@
20571 # define wsdPrng p[0]
20572 #else
20573 # define wsdPrng sqlite3Prng
20574 #endif
20575
20576 #if SQLITE_THREADSAFE
20577 sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
20578 sqlite3_mutex_enter(mutex);
20579 #endif
20580
20581 /* Initialize the state of the random number generator once,
20582 ** the first time this routine is called. The seed value does
20583 ** not need to contain a lot of randomness since we are not
20584 ** trying to do secure encryption or anything like that...
@@ -20584,32 +20603,18 @@
20603 wsdPrng.s[i] = t;
20604 }
20605 wsdPrng.isInit = 1;
20606 }
20607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20608 while( N-- ){
20609 wsdPrng.i++;
20610 t = wsdPrng.s[wsdPrng.i];
20611 wsdPrng.j += t;
20612 wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
20613 wsdPrng.s[wsdPrng.j] = t;
20614 t += wsdPrng.s[wsdPrng.i];
20615 *(zBuf++) = wsdPrng.s[t];
20616 }
20617 sqlite3_mutex_leave(mutex);
20618 }
20619
20620 #ifndef SQLITE_OMIT_BUILTIN_TEST
@@ -21092,36 +21097,10 @@
21097 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21098 assert( m.z || db->mallocFailed );
21099 return m.z;
21100 }
21101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21102 /*
21103 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21104 ** Return the number of bytes in the first nChar unicode characters
21105 ** in pZ. nChar must be non-negative.
21106 */
@@ -23074,15 +23053,17 @@
23053 void *lockingContext; /* Locking style specific state */
23054 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
23055 const char *zPath; /* Name of the file */
23056 unixShm *pShm; /* Shared memory segment information */
23057 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23058 #if SQLITE_MAX_MMAP_SIZE>0
23059 int nFetchOut; /* Number of outstanding xFetch refs */
23060 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23061 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23062 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23063 void *pMapRegion; /* Memory mapped region */
23064 #endif
23065 #ifdef __QNXNTO__
23066 int sectorSize; /* Device sector size */
23067 int deviceCharacteristics; /* Precomputed device characteristics */
23068 #endif
23069 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -23513,10 +23494,11 @@
23494 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23495
23496 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23497 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23498
23499 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23500 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23501 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23502
23503 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23504 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23525,10 +23507,11 @@
23507 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23508 #else
23509 { "mremap", (sqlite3_syscall_ptr)0, 0 },
23510 #endif
23511 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23512 #endif
23513
23514 }; /* End of the overrideable system calls */
23515
23516 /*
23517 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23631,17 +23614,27 @@
23614 ** recover the hot journals.
23615 */
23616 static int robust_open(const char *z, int f, mode_t m){
23617 int fd;
23618 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23619 while(1){
23620 #if defined(O_CLOEXEC)
23621 fd = osOpen(z,f|O_CLOEXEC,m2);
23622 #else
23623 fd = osOpen(z,f,m2);
23624 #endif
23625 if( fd<0 ){
23626 if( errno==EINTR ) continue;
23627 break;
23628 }
23629 if( fd>2 ) break;
23630 osClose(fd);
23631 sqlite3_log(SQLITE_WARNING,
23632 "attempt to open \"%s\" as file descriptor %d", z, fd);
23633 fd = -1;
23634 if( osOpen("/dev/null", f, m)<0 ) break;
23635 }
23636 if( fd>=0 ){
23637 if( m!=0 ){
23638 struct stat statbuf;
23639 if( osFstat(fd, &statbuf)==0
23640 && statbuf.st_size==0
@@ -24935,12 +24928,14 @@
24928 static int unixUnlock(sqlite3_file *id, int eFileLock){
24929 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
24930 return posixUnlock(id, eFileLock, 0);
24931 }
24932
24933 #if SQLITE_MAX_MMAP_SIZE>0
24934 static int unixMapfile(unixFile *pFd, i64 nByte);
24935 static void unixUnmapfile(unixFile *pFd);
24936 #endif
24937
24938 /*
24939 ** This function performs the parts of the "close file" operation
24940 ** common to all locking schemes. It closes the directory and file
24941 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24950,11 +24945,13 @@
24945 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24946 ** vxworksReleaseFileId() routine.
24947 */
24948 static int closeUnixFile(sqlite3_file *id){
24949 unixFile *pFile = (unixFile*)id;
24950 #if SQLITE_MAX_MMAP_SIZE>0
24951 unixUnmapfile(pFile);
24952 #endif
24953 if( pFile->h>=0 ){
24954 robust_close(pFile, pFile->h, __LINE__);
24955 pFile->h = -1;
24956 }
24957 #if OS_VXWORKS
@@ -26155,10 +26152,11 @@
26152 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26153 i64 newOffset;
26154 #endif
26155 TIMER_START;
26156 assert( cnt==(cnt&0x1ffff) );
26157 assert( id->h>2 );
26158 cnt &= 0x1ffff;
26159 do{
26160 #if defined(USE_PREAD)
26161 got = osPread(id->h, pBuf, cnt, offset);
26162 SimulateIOError( got = -1 );
@@ -26269,10 +26267,11 @@
26267 int *piErrno /* OUT: Error number if error occurs */
26268 ){
26269 int rc = 0; /* Value returned by system call */
26270
26271 assert( nBuf==(nBuf&0x1ffff) );
26272 assert( fd>2 );
26273 nBuf &= 0x1ffff;
26274 TIMER_START;
26275
26276 #if defined(USE_PREAD)
26277 do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26654,17 +26653,19 @@
26653 if( pFile->inNormalWrite && nByte==0 ){
26654 pFile->transCntrChng = 1;
26655 }
26656 #endif
26657
26658 #if SQLITE_MAX_MMAP_SIZE>0
26659 /* If the file was just truncated to a size smaller than the currently
26660 ** mapped region, reduce the effective mapping size as well. SQLite will
26661 ** use read() and write() to access data beyond this point from now on.
26662 */
26663 if( nByte<pFile->mmapSize ){
26664 pFile->mmapSize = nByte;
26665 }
26666 #endif
26667
26668 return SQLITE_OK;
26669 }
26670 }
26671
@@ -26750,10 +26751,11 @@
26751 }
26752 #endif
26753 }
26754 }
26755
26756 #if SQLITE_MAX_MMAP_SIZE>0
26757 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26758 int rc;
26759 if( pFile->szChunk<=0 ){
26760 if( robust_ftruncate(pFile->h, nByte) ){
26761 pFile->lastErrno = errno;
@@ -26762,10 +26764,11 @@
26764 }
26765
26766 rc = unixMapfile(pFile, nByte);
26767 return rc;
26768 }
26769 #endif
26770
26771 return SQLITE_OK;
26772 }
26773
26774 /*
@@ -26830,10 +26833,11 @@
26833 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26834 *(char**)pArg = zTFile;
26835 }
26836 return SQLITE_OK;
26837 }
26838 #if SQLITE_MAX_MMAP_SIZE>0
26839 case SQLITE_FCNTL_MMAP_SIZE: {
26840 i64 newLimit = *(i64*)pArg;
26841 int rc = SQLITE_OK;
26842 if( newLimit>sqlite3GlobalConfig.mxMmap ){
26843 newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26846,10 +26850,11 @@
26850 rc = unixMapfile(pFile, -1);
26851 }
26852 }
26853 return rc;
26854 }
26855 #endif
26856 #ifdef SQLITE_DEBUG
26857 /* The pager calls this method to signal that it has done
26858 ** a rollback and that the database is therefore unchanged and
26859 ** it hence it is OK for the transaction change counter to be
26860 ** unchanged.
@@ -27656,26 +27661,24 @@
27661 # define unixShmLock 0
27662 # define unixShmBarrier 0
27663 # define unixShmUnmap 0
27664 #endif /* #ifndef SQLITE_OMIT_WAL */
27665
27666 #if SQLITE_MAX_MMAP_SIZE>0
27667 /*
27668 ** If it is currently memory mapped, unmap file pFd.
27669 */
27670 static void unixUnmapfile(unixFile *pFd){
27671 assert( pFd->nFetchOut==0 );
 
27672 if( pFd->pMapRegion ){
27673 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27674 pFd->pMapRegion = 0;
27675 pFd->mmapSize = 0;
27676 pFd->mmapSizeActual = 0;
27677 }
 
27678 }
27679
 
27680 /*
27681 ** Return the system page size.
27682 */
27683 static int unixGetPagesize(void){
27684 #if HAVE_MREMAP
@@ -27684,13 +27687,11 @@
27687 return getpagesize();
27688 #else
27689 return (int)sysconf(_SC_PAGESIZE);
27690 #endif
27691 }
 
27692
 
27693 /*
27694 ** Attempt to set the size of the memory mapping maintained by file
27695 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27696 **
27697 ** If successful, this function sets the following variables:
@@ -27771,11 +27772,10 @@
27772 pFd->mmapSizeMax = 0;
27773 }
27774 pFd->pMapRegion = (void *)pNew;
27775 pFd->mmapSize = pFd->mmapSizeActual = nNew;
27776 }
 
27777
27778 /*
27779 ** Memory map or remap the file opened by file-descriptor pFd (if the file
27780 ** is already mapped, the existing mapping is replaced by the new). Or, if
27781 ** there already exists a mapping for this file, and there are still
@@ -27790,11 +27790,10 @@
27790 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
27791 ** recreated as a result of outstanding references) or an SQLite error
27792 ** code otherwise.
27793 */
27794 static int unixMapfile(unixFile *pFd, i64 nByte){
 
27795 i64 nMap = nByte;
27796 int rc;
27797
27798 assert( nMap>=0 || pFd->nFetchOut==0 );
27799 if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27816,14 +27815,14 @@
27815 unixRemapfile(pFd, nMap);
27816 }else{
27817 unixUnmapfile(pFd);
27818 }
27819 }
 
27820
27821 return SQLITE_OK;
27822 }
27823 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
27824
27825 /*
27826 ** If possible, return a pointer to a mapping of file fd starting at offset
27827 ** iOff. The mapping must be valid for at least nAmt bytes.
27828 **
@@ -27868,10 +27867,11 @@
27867 */
27868 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27869 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27870 UNUSED_PARAMETER(iOff);
27871
27872 #if SQLITE_MAX_MMAP_SIZE>0
27873 /* If p==0 (unmap the entire file) then there must be no outstanding
27874 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27875 ** then there must be at least one outstanding. */
27876 assert( (p==0)==(pFd->nFetchOut==0) );
27877
@@ -27883,10 +27883,11 @@
27883 }else{
27884 unixUnmapfile(pFd);
27885 }
27886
27887 assert( pFd->nFetchOut>=0 );
27888 #endif
27889 return SQLITE_OK;
27890 }
27891
27892 /*
27893 ** Here ends the implementation of all sqlite3_file methods.
@@ -28214,11 +28215,13 @@
28215 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28216 pNew->h = h;
28217 pNew->pVfs = pVfs;
28218 pNew->zPath = zFilename;
28219 pNew->ctrlFlags = (u8)ctrlFlags;
28220 #if SQLITE_MAX_MMAP_SIZE>0
28221 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28222 #endif
28223 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28224 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28225 pNew->ctrlFlags |= UNIXFILE_PSOW;
28226 }
28227 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -30715,11 +30718,11 @@
30718 /*
30719 ** Compiling and using WAL mode requires several APIs that are only
30720 ** available in Windows platforms based on the NT kernel.
30721 */
30722 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30723 # error "WAL mode requires support from the Windows NT kernel, compile\
30724 with SQLITE_OMIT_WAL."
30725 #endif
30726
30727 /*
30728 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
@@ -30735,10 +30738,70 @@
30738 */
30739 #if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
30740 # define SQLITE_WIN32_HAS_WIDE
30741 #endif
30742
30743 /*
30744 ** Maximum pathname length (in chars) for Win32. This should normally be
30745 ** MAX_PATH.
30746 */
30747 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
30748 # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
30749 #endif
30750
30751 /*
30752 ** Maximum pathname length (in chars) for WinNT. This should normally be
30753 ** 32767.
30754 */
30755 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
30756 # define SQLITE_WINNT_MAX_PATH_CHARS (32767)
30757 #endif
30758
30759 /*
30760 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
30761 ** characters, so we allocate 3 bytes per character assuming worst-case of
30762 ** 4-bytes-per-character for UTF8.
30763 */
30764 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
30765 # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
30766 #endif
30767
30768 /*
30769 ** Maximum pathname length (in bytes) for WinNT. This should normally be
30770 ** 32767 * sizeof(WCHAR).
30771 */
30772 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
30773 # define SQLITE_WINNT_MAX_PATH_BYTES \
30774 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
30775 #endif
30776
30777 /*
30778 ** Maximum error message length (in chars) for WinRT.
30779 */
30780 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
30781 # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
30782 #endif
30783
30784 /*
30785 ** Returns non-zero if the character should be treated as a directory
30786 ** separator.
30787 */
30788 #ifndef winIsDirSep
30789 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
30790 #endif
30791
30792 /*
30793 ** Returns the string that should be used as the directory separator.
30794 */
30795 #ifndef winGetDirDep
30796 # ifdef __CYGWIN__
30797 # define winGetDirDep() "/"
30798 # else
30799 # define winGetDirDep() "\\"
30800 # endif
30801 #endif
30802
30803 /*
30804 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
30805 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
30806 ** are not present in the header file)?
30807 */
@@ -31742,15 +31805,15 @@
31805 ** this routine is used to determine if the host is Win95/98/ME or
31806 ** WinNT/2K/XP so that we will know whether or not we can safely call
31807 ** the LockFileEx() API.
31808 */
31809 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31810 # define osIsNT() (1)
31811 #elif !defined(SQLITE_WIN32_HAS_WIDE)
31812 # define osIsNT() (0)
31813 #else
31814 static int osIsNT(void){
31815 if( sqlite3_os_type==0 ){
31816 OSVERSIONINFOA sInfo;
31817 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31818 osGetVersionExA(&sInfo);
31819 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31957,11 +32020,11 @@
32020 /*
32021 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
32022 **
32023 ** Space to hold the returned string is obtained from malloc.
32024 */
32025 static LPWSTR winUtf8ToUnicode(const char *zFilename){
32026 int nChar;
32027 LPWSTR zWideFilename;
32028
32029 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32030 if( nChar==0 ){
@@ -31982,11 +32045,11 @@
32045
32046 /*
32047 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
32048 ** obtained from sqlite3_malloc().
32049 */
32050 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
32051 int nByte;
32052 char *zFilename;
32053
32054 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32055 if( nByte == 0 ){
@@ -32010,11 +32073,11 @@
32073 ** current codepage settings for file apis.
32074 **
32075 ** Space to hold the returned string is obtained
32076 ** from sqlite3_malloc.
32077 */
32078 static LPWSTR winMbcsToUnicode(const char *zFilename){
32079 int nByte;
32080 LPWSTR zMbcsFilename;
32081 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32082
32083 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32040,11 +32103,11 @@
32103 ** user's ANSI codepage.
32104 **
32105 ** Space to hold the returned string is obtained from
32106 ** sqlite3_malloc().
32107 */
32108 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
32109 int nByte;
32110 char *zFilename;
32111 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32112
32113 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32070,15 +32133,15 @@
32133 */
32134 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32135 char *zFilenameUtf8;
32136 LPWSTR zTmpWide;
32137
32138 zTmpWide = winMbcsToUnicode(zFilename);
32139 if( zTmpWide==0 ){
32140 return 0;
32141 }
32142 zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
32143 sqlite3_free(zTmpWide);
32144 return zFilenameUtf8;
32145 }
32146
32147 /*
@@ -32087,15 +32150,15 @@
32150 */
32151 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32152 char *zFilenameMbcs;
32153 LPWSTR zTmpWide;
32154
32155 zTmpWide = winUtf8ToUnicode(zFilename);
32156 if( zTmpWide==0 ){
32157 return 0;
32158 }
32159 zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
32160 sqlite3_free(zTmpWide);
32161 return zFilenameMbcs;
32162 }
32163
32164 /*
@@ -32121,11 +32184,11 @@
32184 );
32185 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
32186 if( ppDirectory ){
32187 char *zValueUtf8 = 0;
32188 if( zValue && zValue[0] ){
32189 zValueUtf8 = winUnicodeToUtf8(zValue);
32190 if ( zValueUtf8==0 ){
32191 return SQLITE_NOMEM;
32192 }
32193 }
32194 sqlite3_free(*ppDirectory);
@@ -32134,32 +32197,32 @@
32197 }
32198 return SQLITE_ERROR;
32199 }
32200
32201 /*
32202 ** The return value of winGetLastErrorMsg
32203 ** is zero if the error message fits in the buffer, or non-zero
32204 ** otherwise (if the message was truncated).
32205 */
32206 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32207 /* FormatMessage returns 0 on failure. Otherwise it
32208 ** returns the number of TCHARs written to the output
32209 ** buffer, excluding the terminating null char.
32210 */
32211 DWORD dwLen = 0;
32212 char *zOut = 0;
32213
32214 if( osIsNT() ){
32215 #if SQLITE_OS_WINRT
32216 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
32217 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
32218 FORMAT_MESSAGE_IGNORE_INSERTS,
32219 NULL,
32220 lastErrno,
32221 0,
32222 zTempWide,
32223 SQLITE_WIN32_MAX_ERRMSG_CHARS,
32224 0);
32225 #else
32226 LPWSTR zTempWide = NULL;
32227 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
32228 FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32172,11 +32235,11 @@
32235 0);
32236 #endif
32237 if( dwLen > 0 ){
32238 /* allocate a buffer and convert to UTF8 */
32239 sqlite3BeginBenignMalloc();
32240 zOut = winUnicodeToUtf8(zTempWide);
32241 sqlite3EndBenignMalloc();
32242 #if !SQLITE_OS_WINRT
32243 /* free the system buffer allocated by FormatMessage */
32244 osLocalFree(zTempWide);
32245 #endif
@@ -32240,11 +32303,11 @@
32303 ){
32304 char zMsg[500]; /* Human readable error text */
32305 int i; /* Loop counter */
32306
32307 zMsg[0] = 0;
32308 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32309 assert( errcode!=SQLITE_OK );
32310 if( zPath==0 ) zPath = "";
32311 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32312 zMsg[i] = 0;
32313 sqlite3_log(errcode,
@@ -32265,30 +32328,30 @@
32328 # define SQLITE_WIN32_IOERR_RETRY 10
32329 #endif
32330 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
32331 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
32332 #endif
32333 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32334 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32335
32336 /*
32337 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
32338 ** to see if it should be retried. Return TRUE to retry. Return FALSE
32339 ** to give up with an error.
32340 */
32341 static int winRetryIoerr(int *pnRetry, DWORD *pError){
32342 DWORD e = osGetLastError();
32343 if( *pnRetry>=winIoerrRetry ){
32344 if( pError ){
32345 *pError = e;
32346 }
32347 return 0;
32348 }
32349 if( e==ERROR_ACCESS_DENIED ||
32350 e==ERROR_LOCK_VIOLATION ||
32351 e==ERROR_SHARING_VIOLATION ){
32352 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
32353 ++*pnRetry;
32354 return 1;
32355 }
32356 if( pError ){
32357 *pError = e;
@@ -32297,15 +32360,15 @@
32360 }
32361
32362 /*
32363 ** Log a I/O error retry episode.
32364 */
32365 static void winLogIoerr(int nRetry){
32366 if( nRetry ){
32367 sqlite3_log(SQLITE_IOERR,
32368 "delayed %dms for lock/sharing conflict",
32369 winIoerrRetryDelay*nRetry*(nRetry+1)/2
32370 );
32371 }
32372 }
32373
32374 #if SQLITE_OS_WINCE
@@ -32366,11 +32429,11 @@
32429 LPWSTR zName;
32430 DWORD lastErrno;
32431 BOOL bLogged = FALSE;
32432 BOOL bInit = TRUE;
32433
32434 zName = winUtf8ToUnicode(zFilename);
32435 if( zName==0 ){
32436 /* out of memory */
32437 return SQLITE_IOERR_NOMEM;
32438 }
32439
@@ -32639,11 +32702,11 @@
32702 ** API LockFile.
32703 */
32704 return winceLockFile(phFile, offsetLow, offsetHigh,
32705 numBytesLow, numBytesHigh);
32706 #else
32707 if( osIsNT() ){
32708 OVERLAPPED ovlp;
32709 memset(&ovlp, 0, sizeof(OVERLAPPED));
32710 ovlp.Offset = offsetLow;
32711 ovlp.OffsetHigh = offsetHigh;
32712 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32670,11 +32733,11 @@
32733 ** API UnlockFile.
32734 */
32735 return winceUnlockFile(phFile, offsetLow, offsetHigh,
32736 numBytesLow, numBytesHigh);
32737 #else
32738 if( osIsNT() ){
32739 OVERLAPPED ovlp;
32740 memset(&ovlp, 0, sizeof(OVERLAPPED));
32741 ovlp.Offset = offsetLow;
32742 ovlp.OffsetHigh = offsetHigh;
32743 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32700,11 +32763,11 @@
32763 /*
32764 ** Move the current position of the file handle passed as the first
32765 ** argument to offset iOffset within the file. If successful, return 0.
32766 ** Otherwise, set pFile->lastErrno and return non-zero.
32767 */
32768 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
32769 #if !SQLITE_OS_WINRT
32770 LONG upperBits; /* Most sig. 32 bits of new offset */
32771 LONG lowerBits; /* Least sig. 32 bits of new offset */
32772 DWORD dwRet; /* Value returned by SetFilePointer() */
32773 DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32725,11 +32788,11 @@
32788
32789 if( (dwRet==INVALID_SET_FILE_POINTER
32790 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
32791 pFile->lastErrno = lastErrno;
32792 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32793 "winSeekFile", pFile->zPath);
32794 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32795 return 1;
32796 }
32797
32798 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32746,11 +32809,11 @@
32809 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
32810
32811 if(!bRet){
32812 pFile->lastErrno = osGetLastError();
32813 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32814 "winSeekFile", pFile->zPath);
32815 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32816 return 1;
32817 }
32818
32819 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32861,11 +32924,11 @@
32924 }
32925 }
32926 #endif
32927
32928 #if SQLITE_OS_WINCE
32929 if( winSeekFile(pFile, offset) ){
32930 OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
32931 return SQLITE_FULL;
32932 }
32933 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32934 #else
@@ -32874,17 +32937,17 @@
32937 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
32938 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
32939 osGetLastError()!=ERROR_HANDLE_EOF ){
32940 #endif
32941 DWORD lastErrno;
32942 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
32943 pFile->lastErrno = lastErrno;
32944 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
32945 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
32946 "winRead", pFile->zPath);
32947 }
32948 winLogIoerr(nRetry);
32949 if( nRead<(DWORD)amt ){
32950 /* Unread parts of the buffer must be zero-filled */
32951 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32952 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
32953 return SQLITE_IOERR_SHORT_READ;
@@ -32933,11 +32996,11 @@
32996 }
32997 }
32998 #endif
32999
33000 #if SQLITE_OS_WINCE
33001 rc = winSeekFile(pFile, offset);
33002 if( rc==0 ){
33003 #else
33004 {
33005 #endif
33006 #if !SQLITE_OS_WINCE
@@ -32958,11 +33021,11 @@
33021 #if SQLITE_OS_WINCE
33022 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33023 #else
33024 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
33025 #endif
33026 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33027 break;
33028 }
33029 assert( nWrite==0 || nWrite<=(DWORD)nRem );
33030 if( nWrite==0 || nWrite>(DWORD)nRem ){
33031 lastErrno = osGetLastError();
@@ -32990,11 +33053,11 @@
33053 }
33054 OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
33055 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33056 "winWrite", pFile->zPath);
33057 }else{
33058 winLogIoerr(nRetry);
33059 }
33060 OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
33061 return SQLITE_OK;
33062 }
33063
@@ -33019,11 +33082,11 @@
33082 if( pFile->szChunk>0 ){
33083 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33084 }
33085
33086 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33087 if( winSeekFile(pFile, nByte) ){
33088 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33089 "winTruncate1", pFile->zPath);
33090 }else if( 0==osSetEndOfFile(pFile->h) &&
33091 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33092 pFile->lastErrno = lastErrno;
@@ -33100,10 +33163,11 @@
33163
33164 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33165 ** no-op
33166 */
33167 #ifdef SQLITE_NO_SYNC
33168 OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
33169 return SQLITE_OK;
33170 #else
33171 rc = osFlushFileBuffers(pFile->h);
33172 SimulateIOError( rc=FALSE );
33173 if( rc ){
@@ -33197,14 +33261,14 @@
33261 /*
33262 ** Acquire a reader lock.
33263 ** Different API routines are called depending on whether or not this
33264 ** is Win9x or WinNT.
33265 */
33266 static int winGetReadLock(winFile *pFile){
33267 int res;
33268 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33269 if( osIsNT() ){
33270 #if SQLITE_OS_WINCE
33271 /*
33272 ** NOTE: Windows CE is handled differently here due its lack of the Win32
33273 ** API LockFileEx.
33274 */
@@ -33232,15 +33296,15 @@
33296 }
33297
33298 /*
33299 ** Undo a readlock
33300 */
33301 static int winUnlockReadLock(winFile *pFile){
33302 int res;
33303 DWORD lastErrno;
33304 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33305 if( osIsNT() ){
33306 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33307 }
33308 #ifdef SQLITE_WIN32_HAS_ANSI
33309 else{
33310 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33247,11 +33311,11 @@
33311 }
33312 #endif
33313 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33314 pFile->lastErrno = lastErrno;
33315 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33316 "winUnlockReadLock", pFile->zPath);
33317 }
33318 OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
33319 return res;
33320 }
33321
@@ -33338,11 +33402,11 @@
33402
33403 /* Acquire a shared lock
33404 */
33405 if( locktype==SHARED_LOCK && res ){
33406 assert( pFile->locktype==NO_LOCK );
33407 res = winGetReadLock(pFile);
33408 if( res ){
33409 newLocktype = SHARED_LOCK;
33410 }else{
33411 lastErrno = osGetLastError();
33412 }
@@ -33369,18 +33433,18 @@
33433
33434 /* Acquire an EXCLUSIVE lock
33435 */
33436 if( locktype==EXCLUSIVE_LOCK && res ){
33437 assert( pFile->locktype>=SHARED_LOCK );
33438 res = winUnlockReadLock(pFile);
33439 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
33440 SHARED_SIZE, 0);
33441 if( res ){
33442 newLocktype = EXCLUSIVE_LOCK;
33443 }else{
33444 lastErrno = osGetLastError();
33445 winGetReadLock(pFile);
33446 }
33447 }
33448
33449 /* If we are holding a PENDING lock that ought to be released, then
33450 ** release it now.
@@ -33393,14 +33457,14 @@
33457 ** return the appropriate result code.
33458 */
33459 if( res ){
33460 rc = SQLITE_OK;
33461 }else{
33462 pFile->lastErrno = lastErrno;
33463 rc = SQLITE_BUSY;
33464 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
33465 pFile->h, locktype, newLocktype));
 
 
33466 }
33467 pFile->locktype = (u8)newLocktype;
33468 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
33469 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
33470 return rc;
@@ -33456,11 +33520,11 @@
33520 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
33521 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
33522 type = pFile->locktype;
33523 if( type>=EXCLUSIVE_LOCK ){
33524 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33525 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
33526 /* This should never happen. We should always be able to
33527 ** reacquire the read lock */
33528 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
33529 "winUnlock", pFile->zPath);
33530 }
@@ -33467,11 +33531,11 @@
33531 }
33532 if( type>=RESERVED_LOCK ){
33533 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
33534 }
33535 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33536 winUnlockReadLock(pFile);
33537 }
33538 if( type>=PENDING_LOCK ){
33539 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
33540 }
33541 pFile->locktype = (u8)locktype;
@@ -33495,11 +33559,11 @@
33559 pFile->ctrlFlags |= mask;
33560 }
33561 }
33562
33563 /* Forward declaration */
33564 static int winGetTempname(sqlite3_vfs *, char **);
33565 #if SQLITE_MAX_MMAP_SIZE>0
33566 static int winMapfile(winFile*, sqlite3_int64);
33567 #endif
33568
33569 /*
@@ -33558,30 +33622,30 @@
33622 return SQLITE_OK;
33623 }
33624 case SQLITE_FCNTL_WIN32_AV_RETRY: {
33625 int *a = (int*)pArg;
33626 if( a[0]>0 ){
33627 winIoerrRetry = a[0];
33628 }else{
33629 a[0] = winIoerrRetry;
33630 }
33631 if( a[1]>0 ){
33632 winIoerrRetryDelay = a[1];
33633 }else{
33634 a[1] = winIoerrRetryDelay;
33635 }
33636 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33637 return SQLITE_OK;
33638 }
33639 case SQLITE_FCNTL_TEMPFILENAME: {
33640 char *zTFile = 0;
33641 int rc = winGetTempname(pFile->pVfs, &zTFile);
33642 if( rc==SQLITE_OK ){
33643 *(char**)pArg = zTFile;
33644 }
33645 OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
33646 return rc;
33647 }
33648 #if SQLITE_MAX_MMAP_SIZE>0
33649 case SQLITE_FCNTL_MMAP_SIZE: {
33650 i64 newLimit = *(i64*)pArg;
33651 int rc = SQLITE_OK;
@@ -34539,14 +34603,14 @@
34603 ** Convert a UTF-8 filename into whatever form the underlying
34604 ** operating system wants filenames in. Space to hold the result
34605 ** is obtained from malloc and must be freed by the calling
34606 ** function.
34607 */
34608 static void *winConvertUtf8Filename(const char *zFilename){
34609 void *zConverted = 0;
34610 if( osIsNT() ){
34611 zConverted = winUtf8ToUnicode(zFilename);
34612 }
34613 #ifdef SQLITE_WIN32_HAS_ANSI
34614 else{
34615 zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
34616 }
@@ -34554,117 +34618,135 @@
34618 /* caller will handle out of memory */
34619 return zConverted;
34620 }
34621
34622 /*
34623 ** This function returns non-zero if the specified UTF-8 string buffer
34624 ** ends with a directory separator character.
 
34625 */
34626 static int winEndsInDirSep(char *zBuf){
34627 if( zBuf ){
34628 int nLen = sqlite3Strlen30(zBuf);
34629 return nLen>0 && winIsDirSep(zBuf[nLen-1]);
34630 }
34631 return 0;
34632 }
34633
34634 /*
34635 ** Create a temporary file name and store the resulting pointer into pzBuf.
34636 ** The pointer returned in pzBuf must be freed via sqlite3_free().
34637 */
34638 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
34639 static char zChars[] =
34640 "abcdefghijklmnopqrstuvwxyz"
34641 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34642 "0123456789";
34643 size_t i, j;
34644 int nBuf, nLen;
34645 char *zBuf;
34646
34647 /* It's odd to simulate an io-error here, but really this is just
34648 ** using the io-error infrastructure to test that SQLite handles this
34649 ** function failing.
34650 */
34651 SimulateIOError( return SQLITE_IOERR );
34652
34653 /* Allocate a temporary buffer to store the fully qualified file
34654 ** name for the temporary file. If this fails, we cannot continue.
34655 */
34656 nBuf = pVfs->mxPathname;
34657 zBuf = sqlite3MallocZero( nBuf+2 );
34658 if( !zBuf ){
34659 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34660 return SQLITE_IOERR_NOMEM;
34661 }
34662
34663 /* Figure out the effective temporary directory. First, check if one
34664 ** has been explicitly set by the application; otherwise, use the one
34665 ** configured by the operating system.
34666 */
34667 assert( nBuf>30 );
34668 if( sqlite3_temp_directory ){
34669 sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
34670 winEndsInDirSep(sqlite3_temp_directory) ? "" :
34671 winGetDirDep());
34672 }
34673 #if !SQLITE_OS_WINRT
34674 else if( osIsNT() ){
34675 char *zMulti;
34676 LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
34677 if( !zWidePath ){
34678 sqlite3_free(zBuf);
34679 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34680 return SQLITE_IOERR_NOMEM;
34681 }
34682 if( osGetTempPathW(nBuf, zWidePath)==0 ){
34683 sqlite3_free(zWidePath);
34684 sqlite3_free(zBuf);
34685 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34686 return SQLITE_IOERR_GETTEMPPATH;
34687 }
34688 zMulti = winUnicodeToUtf8(zWidePath);
34689 if( zMulti ){
34690 sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
34691 sqlite3_free(zMulti);
34692 sqlite3_free(zWidePath);
34693 }else{
34694 sqlite3_free(zWidePath);
34695 sqlite3_free(zBuf);
34696 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34697 return SQLITE_IOERR_NOMEM;
34698 }
34699 }
34700 #ifdef SQLITE_WIN32_HAS_ANSI
34701 else{
34702 char *zUtf8;
34703 char *zMbcsPath = sqlite3MallocZero( nBuf );
34704 if( !zMbcsPath ){
34705 sqlite3_free(zBuf);
34706 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34707 return SQLITE_IOERR_NOMEM;
34708 }
34709 if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
34710 sqlite3_free(zBuf);
34711 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34712 return SQLITE_IOERR_GETTEMPPATH;
34713 }
34714 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34715 if( zUtf8 ){
34716 sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
34717 sqlite3_free(zUtf8);
34718 }else{
34719 sqlite3_free(zBuf);
34720 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34721 return SQLITE_IOERR_NOMEM;
34722 }
34723 }
 
 
 
 
 
 
 
 
34724 #endif /* SQLITE_WIN32_HAS_ANSI */
 
 
 
 
 
 
 
 
34725 #endif /* !SQLITE_OS_WINRT */
34726
34727 /* Check that the output buffer is large enough for the temporary file
34728 ** name. If it is not, return SQLITE_ERROR.
34729 */
34730 nLen = sqlite3Strlen30(zBuf);
34731
34732 if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34733 sqlite3_free(zBuf);
34734 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
34735 return SQLITE_ERROR;
34736 }
34737
34738 sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
 
34739
 
 
 
34740 j = sqlite3Strlen30(zBuf);
34741 sqlite3_randomness(15, &zBuf[j]);
34742 for(i=0; i<15; i++, j++){
34743 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34744 }
34745 zBuf[j] = 0;
34746 zBuf[j+1] = 0;
34747 *pzBuf = zBuf;
34748
34749 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
34750 return SQLITE_OK;
34751 }
34752
@@ -34676,17 +34758,17 @@
34758 static int winIsDir(const void *zConverted){
34759 DWORD attr;
34760 int rc = 0;
34761 DWORD lastErrno;
34762
34763 if( osIsNT() ){
34764 int cnt = 0;
34765 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34766 memset(&sAttrData, 0, sizeof(sAttrData));
34767 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
34768 GetFileExInfoStandard,
34769 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
34770 if( !rc ){
34771 return 0; /* Invalid name? */
34772 }
34773 attr = sAttrData.dwFileAttributes;
34774 #if SQLITE_OS_WINCE==0
@@ -34699,11 +34781,11 @@
34781
34782 /*
34783 ** Open a file.
34784 */
34785 static int winOpen(
34786 sqlite3_vfs *pVfs, /* Used to get maximum path name length */
34787 const char *zName, /* Name of the file (UTF-8) */
34788 sqlite3_file *id, /* Write the SQLite file handle here */
34789 int flags, /* Open mode flags */
34790 int *pOutFlags /* Status return flags */
34791 ){
@@ -34722,11 +34804,11 @@
34804 int cnt = 0;
34805
34806 /* If argument zPath is a NULL pointer, this function is required to open
34807 ** a temporary file. Use this buffer to store the file name in.
34808 */
34809 char *zTmpname = 0; /* For temporary filename, if necessary. */
34810
34811 int rc = SQLITE_OK; /* Function Return Code */
34812 #if !defined(NDEBUG) || SQLITE_OS_WINCE
34813 int eType = flags&0xFFFFFF00; /* Type of file to open */
34814 #endif
@@ -34777,22 +34859,22 @@
34859 assert( pFile!=0 );
34860 memset(pFile, 0, sizeof(winFile));
34861 pFile->h = INVALID_HANDLE_VALUE;
34862
34863 #if SQLITE_OS_WINRT
34864 if( !zUtf8Name && !sqlite3_temp_directory ){
34865 sqlite3_log(SQLITE_ERROR,
34866 "sqlite3_temp_directory variable should be set for WinRT");
34867 }
34868 #endif
34869
34870 /* If the second argument to this function is NULL, generate a
34871 ** temporary file name to use
34872 */
34873 if( !zUtf8Name ){
34874 assert( isDelete && !isOpenJournal );
34875 rc = winGetTempname(pVfs, &zTmpname);
34876 if( rc!=SQLITE_OK ){
34877 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
34878 return rc;
34879 }
34880 zUtf8Name = zTmpname;
@@ -34801,21 +34883,23 @@
34883 /* Database filenames are double-zero terminated if they are not
34884 ** URIs with parameters. Hence, they can always be passed into
34885 ** sqlite3_uri_parameter().
34886 */
34887 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
34888 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
34889
34890 /* Convert the filename to the system encoding. */
34891 zConverted = winConvertUtf8Filename(zUtf8Name);
34892 if( zConverted==0 ){
34893 sqlite3_free(zTmpname);
34894 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
34895 return SQLITE_IOERR_NOMEM;
34896 }
34897
34898 if( winIsDir(zConverted) ){
34899 sqlite3_free(zConverted);
34900 sqlite3_free(zTmpname);
34901 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
34902 return SQLITE_CANTOPEN_ISDIR;
34903 }
34904
34905 if( isReadWrite ){
@@ -34858,11 +34942,11 @@
34942 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
34943 #if SQLITE_OS_WINCE
34944 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
34945 #endif
34946
34947 if( osIsNT() ){
34948 #if SQLITE_OS_WINRT
34949 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
34950 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
34951 extendedParameters.dwFileAttributes =
34952 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34873,21 +34957,21 @@
34957 while( (h = osCreateFile2((LPCWSTR)zConverted,
34958 dwDesiredAccess,
34959 dwShareMode,
34960 dwCreationDisposition,
34961 &extendedParameters))==INVALID_HANDLE_VALUE &&
34962 winRetryIoerr(&cnt, &lastErrno) ){
34963 /* Noop */
34964 }
34965 #else
34966 while( (h = osCreateFileW((LPCWSTR)zConverted,
34967 dwDesiredAccess,
34968 dwShareMode, NULL,
34969 dwCreationDisposition,
34970 dwFlagsAndAttributes,
34971 NULL))==INVALID_HANDLE_VALUE &&
34972 winRetryIoerr(&cnt, &lastErrno) ){
34973 /* Noop */
34974 }
34975 #endif
34976 }
34977 #ifdef SQLITE_WIN32_HAS_ANSI
@@ -34896,24 +34980,25 @@
34980 dwDesiredAccess,
34981 dwShareMode, NULL,
34982 dwCreationDisposition,
34983 dwFlagsAndAttributes,
34984 NULL))==INVALID_HANDLE_VALUE &&
34985 winRetryIoerr(&cnt, &lastErrno) ){
34986 /* Noop */
34987 }
34988 }
34989 #endif
34990 winLogIoerr(cnt);
34991
34992 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
34993 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
34994
34995 if( h==INVALID_HANDLE_VALUE ){
34996 pFile->lastErrno = lastErrno;
34997 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
34998 sqlite3_free(zConverted);
34999 sqlite3_free(zTmpname);
35000 if( isReadWrite && !isExclusive ){
35001 return winOpen(pVfs, zName, id,
35002 ((flags|SQLITE_OPEN_READONLY) &
35003 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
35004 pOutFlags);
@@ -34938,19 +35023,21 @@
35023 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
35024 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
35025 ){
35026 osCloseHandle(h);
35027 sqlite3_free(zConverted);
35028 sqlite3_free(zTmpname);
35029 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
35030 return rc;
35031 }
35032 if( isTemp ){
35033 pFile->zDeleteOnClose = zConverted;
35034 }else
35035 #endif
35036 {
35037 sqlite3_free(zConverted);
35038 sqlite3_free(zTmpname);
35039 }
35040
35041 pFile->pMethod = &winIoMethod;
35042 pFile->pVfs = pVfs;
35043 pFile->h = h;
@@ -35000,15 +35087,16 @@
35087 UNUSED_PARAMETER(syncDir);
35088
35089 SimulateIOError(return SQLITE_IOERR_DELETE);
35090 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
35091
35092 zConverted = winConvertUtf8Filename(zFilename);
35093 if( zConverted==0 ){
35094 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35095 return SQLITE_IOERR_NOMEM;
35096 }
35097 if( osIsNT() ){
35098 do {
35099 #if SQLITE_OS_WINRT
35100 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35101 memset(&sAttrData, 0, sizeof(sAttrData));
35102 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35043,11 +35131,11 @@
35131 }
35132 if ( osDeleteFileW(zConverted) ){
35133 rc = SQLITE_OK; /* Deleted OK. */
35134 break;
35135 }
35136 if ( !winRetryIoerr(&cnt, &lastErrno) ){
35137 rc = SQLITE_ERROR; /* No more retries. */
35138 break;
35139 }
35140 } while(1);
35141 }
@@ -35071,11 +35159,11 @@
35159 }
35160 if ( osDeleteFileA(zConverted) ){
35161 rc = SQLITE_OK; /* Deleted OK. */
35162 break;
35163 }
35164 if ( !winRetryIoerr(&cnt, &lastErrno) ){
35165 rc = SQLITE_ERROR; /* No more retries. */
35166 break;
35167 }
35168 } while(1);
35169 }
@@ -35082,11 +35170,11 @@
35170 #endif
35171 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
35172 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35173 "winDelete", zFilename);
35174 }else{
35175 winLogIoerr(cnt);
35176 }
35177 sqlite3_free(zConverted);
35178 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
35179 return rc;
35180 }
@@ -35108,22 +35196,22 @@
35196
35197 SimulateIOError( return SQLITE_IOERR_ACCESS; );
35198 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
35199 zFilename, flags, pResOut));
35200
35201 zConverted = winConvertUtf8Filename(zFilename);
35202 if( zConverted==0 ){
35203 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35204 return SQLITE_IOERR_NOMEM;
35205 }
35206 if( osIsNT() ){
35207 int cnt = 0;
35208 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35209 memset(&sAttrData, 0, sizeof(sAttrData));
35210 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35211 GetFileExInfoStandard,
35212 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
35213 if( rc ){
35214 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
35215 ** as if it does not exist.
35216 */
35217 if( flags==SQLITE_ACCESS_EXISTS
@@ -35132,11 +35220,11 @@
35220 attr = INVALID_FILE_ATTRIBUTES;
35221 }else{
35222 attr = sAttrData.dwFileAttributes;
35223 }
35224 }else{
35225 winLogIoerr(cnt);
35226 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
35227 winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
35228 sqlite3_free(zConverted);
35229 return SQLITE_IOERR_ACCESS;
35230 }else{
@@ -35183,11 +35271,11 @@
35271 ** a legal UNC name, a volume relative path, or an absolute path name in the
35272 ** "Unix" format on Windows. There is no easy way to differentiate between
35273 ** the final two cases; therefore, we return the safer return value of TRUE
35274 ** so that callers of this function will simply use it verbatim.
35275 */
35276 if ( winIsDirSep(zPathname[0]) ){
35277 return TRUE;
35278 }
35279
35280 /*
35281 ** If the path name starts with a letter and a colon it is either a volume
@@ -35219,28 +35307,33 @@
35307 ){
35308
35309 #if defined(__CYGWIN__)
35310 SimulateIOError( return SQLITE_ERROR );
35311 UNUSED_PARAMETER(nFull);
 
35312 assert( nFull>=pVfs->mxPathname );
35313 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
35314 /*
35315 ** NOTE: We are dealing with a relative path name and the data
35316 ** directory has been set. Therefore, use it as the basis
35317 ** for converting the relative path name to an absolute
35318 ** one by prepending the data directory and a slash.
35319 */
35320 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
35321 if( !zOut ){
35322 winLogError(SQLITE_IOERR_NOMEM, 0, "winFullPathname", zRelative);
35323 return SQLITE_IOERR_NOMEM;
35324 }
35325 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35326 pVfs->mxPathname+1)<0 ){
35327 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35328 zRelative);
35329 sqlite3_free(zOut);
35330 return SQLITE_CANTOPEN_FULLPATH;
35331 }
35332 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35333 sqlite3_data_directory, winGetDirDep(), zOut);
35334 sqlite3_free(zOut);
35335 }else{
35336 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
35337 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35338 zRelative);
35339 return SQLITE_CANTOPEN_FULLPATH;
@@ -35258,12 +35351,12 @@
35351 ** NOTE: We are dealing with a relative path name and the data
35352 ** directory has been set. Therefore, use it as the basis
35353 ** for converting the relative path name to an absolute
35354 ** one by prepending the data directory and a backslash.
35355 */
35356 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35357 sqlite3_data_directory, winGetDirDep(), zRelative);
35358 }else{
35359 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
35360 }
35361 return SQLITE_OK;
35362 #endif
@@ -35291,19 +35384,19 @@
35384 ** NOTE: We are dealing with a relative path name and the data
35385 ** directory has been set. Therefore, use it as the basis
35386 ** for converting the relative path name to an absolute
35387 ** one by prepending the data directory and a backslash.
35388 */
35389 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35390 sqlite3_data_directory, winGetDirDep(), zRelative);
35391 return SQLITE_OK;
35392 }
35393 zConverted = winConvertUtf8Filename(zRelative);
35394 if( zConverted==0 ){
35395 return SQLITE_IOERR_NOMEM;
35396 }
35397 if( osIsNT() ){
35398 LPWSTR zTemp;
35399 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
35400 if( nByte==0 ){
35401 winLogError(SQLITE_ERROR, osGetLastError(),
35402 "GetFullPathNameW1", zConverted);
@@ -35323,11 +35416,11 @@
35416 sqlite3_free(zConverted);
35417 sqlite3_free(zTemp);
35418 return SQLITE_CANTOPEN_FULLPATH;
35419 }
35420 sqlite3_free(zConverted);
35421 zOut = winUnicodeToUtf8(zTemp);
35422 sqlite3_free(zTemp);
35423 }
35424 #ifdef SQLITE_WIN32_HAS_ANSI
35425 else{
35426 char *zTemp;
@@ -35376,16 +35469,16 @@
35469 ** Interfaces for opening a shared library, finding entry points
35470 ** within the shared library, and closing the shared library.
35471 */
35472 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
35473 HANDLE h;
35474 void *zConverted = winConvertUtf8Filename(zFilename);
35475 UNUSED_PARAMETER(pVfs);
35476 if( zConverted==0 ){
35477 return 0;
35478 }
35479 if( osIsNT() ){
35480 #if SQLITE_OS_WINRT
35481 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
35482 #else
35483 h = osLoadLibraryW((LPCWSTR)zConverted);
35484 #endif
@@ -35398,11 +35491,11 @@
35491 sqlite3_free(zConverted);
35492 return (void*)h;
35493 }
35494 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
35495 UNUSED_PARAMETER(pVfs);
35496 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35497 }
35498 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
35499 UNUSED_PARAMETER(pVfs);
35500 return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
35501 }
@@ -35574,21 +35667,21 @@
35667 ** by sqlite into the error message available to the user using
35668 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
35669 */
35670 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
35671 UNUSED_PARAMETER(pVfs);
35672 return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
35673 }
35674
35675 /*
35676 ** Initialize and deinitialize the operating system interface.
35677 */
35678 SQLITE_API int sqlite3_os_init(void){
35679 static sqlite3_vfs winVfs = {
35680 3, /* iVersion */
35681 sizeof(winFile), /* szOsFile */
35682 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
35683 0, /* pNext */
35684 "win32", /* zName */
35685 0, /* pAppData */
35686 winOpen, /* xOpen */
35687 winDelete, /* xDelete */
@@ -35605,10 +35698,36 @@
35698 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35699 winSetSystemCall, /* xSetSystemCall */
35700 winGetSystemCall, /* xGetSystemCall */
35701 winNextSystemCall, /* xNextSystemCall */
35702 };
35703 #if defined(SQLITE_WIN32_HAS_WIDE)
35704 static sqlite3_vfs winLongPathVfs = {
35705 3, /* iVersion */
35706 sizeof(winFile), /* szOsFile */
35707 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
35708 0, /* pNext */
35709 "win32-longpath", /* zName */
35710 0, /* pAppData */
35711 winOpen, /* xOpen */
35712 winDelete, /* xDelete */
35713 winAccess, /* xAccess */
35714 winFullPathname, /* xFullPathname */
35715 winDlOpen, /* xDlOpen */
35716 winDlError, /* xDlError */
35717 winDlSym, /* xDlSym */
35718 winDlClose, /* xDlClose */
35719 winRandomness, /* xRandomness */
35720 winSleep, /* xSleep */
35721 winCurrentTime, /* xCurrentTime */
35722 winGetLastError, /* xGetLastError */
35723 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35724 winSetSystemCall, /* xSetSystemCall */
35725 winGetSystemCall, /* xGetSystemCall */
35726 winNextSystemCall, /* xNextSystemCall */
35727 };
35728 #endif
35729
35730 /* Double-check that the aSyscall[] array has been constructed
35731 ** correctly. See ticket [bb3a86e890c8e96ab] */
35732 assert( ArraySize(aSyscall)==74 );
35733
@@ -35621,10 +35740,15 @@
35740 #endif
35741 assert( winSysInfo.dwAllocationGranularity>0 );
35742 assert( winSysInfo.dwPageSize>0 );
35743
35744 sqlite3_vfs_register(&winVfs, 1);
35745
35746 #if defined(SQLITE_WIN32_HAS_WIDE)
35747 sqlite3_vfs_register(&winLongPathVfs, 0);
35748 #endif
35749
35750 return SQLITE_OK;
35751 }
35752
35753 SQLITE_API int sqlite3_os_end(void){
35754 #if SQLITE_OS_WINRT
@@ -37434,10 +37558,11 @@
37558 }
37559
37560 if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
37561 goto fetch_out;
37562 }
37563 assert( pCache->nHash>0 && pCache->apHash );
37564
37565 /* Step 4. Try to recycle a page. */
37566 if( pCache->bPurgeable && pGroup->pLruTail && (
37567 (pCache->nPage+1>=pCache->nMax)
37568 || pGroup->nCurrentPage>=pGroup->nMaxPage
@@ -38794,10 +38919,17 @@
38919 #ifndef SQLITE_OMIT_WAL
38920 u32 aWalData[WAL_SAVEPOINT_NDATA]; /* WAL savepoint context */
38921 #endif
38922 };
38923
38924 /*
38925 ** Bits of the Pager.doNotSpill flag. See further description below.
38926 */
38927 #define SPILLFLAG_OFF 0x01 /* Never spill cache. Set via pragma */
38928 #define SPILLFLAG_ROLLBACK 0x02 /* Current rolling back, so do not spill */
38929 #define SPILLFLAG_NOSYNC 0x04 /* Spill is ok, but do not sync */
38930
38931 /*
38932 ** A open page cache is an instance of struct Pager. A description of
38933 ** some of the more important member variables follows:
38934 **
38935 ** eState
@@ -38860,23 +38992,25 @@
38992 ** The flag is cleared as soon as the journal file is finalized (either
38993 ** by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
38994 ** journal file from being successfully finalized, the setMaster flag
38995 ** is cleared anyway (and the pager will move to ERROR state).
38996 **
38997 ** doNotSpill
38998 **
38999 ** This variables control the behavior of cache-spills (calls made by
39000 ** the pcache module to the pagerStress() routine to write cached data
39001 ** to the file-system in order to free up memory).
39002 **
39003 ** When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
39004 ** writing to the database from pagerStress() is disabled altogether.
39005 ** The SPILLFLAG_ROLLBACK case is done in a very obscure case that
39006 ** comes up during savepoint rollback that requires the pcache module
39007 ** to allocate a new page to prevent the journal file from being written
39008 ** while it is being traversed by code in pager_playback(). The SPILLFLAG_OFF
39009 ** case is a user preference.
39010 **
39011 ** If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
39012 ** is permitted, but syncing the journal file is not. This flag is set
39013 ** by sqlite3PagerWrite() when the file-system sector-size is larger than
39014 ** the database page-size in order to prevent a journal sync from happening
39015 ** in between the journalling of two pages on the same sector.
39016 **
@@ -38976,11 +39110,10 @@
39110 u8 eState; /* Pager state (OPEN, READER, WRITER_LOCKED..) */
39111 u8 eLock; /* Current lock held on database file */
39112 u8 changeCountDone; /* Set after incrementing the change-counter */
39113 u8 setMaster; /* True if a m-j name has been written to jrnl */
39114 u8 doNotSpill; /* Do not spill the cache when non-zero */
 
39115 u8 subjInMemory; /* True to use in-memory sub-journals */
39116 Pgno dbSize; /* Number of pages in the database */
39117 Pgno dbOrigSize; /* dbSize before the current transaction */
39118 Pgno dbFileSize; /* Number of pages in the database file */
39119 Pgno dbHintSize; /* Value passed to FCNTL_SIZE_HINT call */
@@ -39355,17 +39488,21 @@
39488 ** * The page-number is less than or equal to PagerSavepoint.nOrig, and
39489 ** * The bit corresponding to the page-number is not set in
39490 ** PagerSavepoint.pInSavepoint.
39491 */
39492 static int subjRequiresPage(PgHdr *pPg){
 
39493 Pager *pPager = pPg->pPager;
39494 PagerSavepoint *p;
39495 Pgno pgno;
39496 int i;
39497 if( pPager->nSavepoint ){
39498 pgno = pPg->pgno;
39499 for(i=0; i<pPager->nSavepoint; i++){
39500 p = &pPager->aSavepoint[i];
39501 if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
39502 return 1;
39503 }
39504 }
39505 }
39506 return 0;
39507 }
39508
@@ -40636,15 +40773,15 @@
40773 ** the data just read from the sub-journal. Mark the page as dirty
40774 ** and if the pager requires a journal-sync, then mark the page as
40775 ** requiring a journal-sync before it is written.
40776 */
40777 assert( isSavepnt );
40778 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
40779 pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
40780 rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
40781 assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
40782 pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
40783 if( rc!=SQLITE_OK ) return rc;
40784 pPg->flags &= ~PGHDR_NEED_READ;
40785 sqlite3PcacheMakeDirty(pPg);
40786 }
40787 if( pPg ){
@@ -41207,16 +41344,10 @@
41344 int pgsz = pPager->pageSize; /* Number of bytes to read */
41345
41346 assert( pPager->eState>=PAGER_READER && !MEMDB );
41347 assert( isOpen(pPager->fd) );
41348
 
 
 
 
 
 
41349 #ifndef SQLITE_OMIT_WAL
41350 if( iFrame ){
41351 /* Try to pull the page from the write-ahead log. */
41352 rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
41353 }else
@@ -41745,13 +41876,16 @@
41876 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
41877 sqlite3PcacheShrink(pPager->pPCache);
41878 }
41879
41880 /*
41881 ** Adjust settings of the pager to those specified in the pgFlags parameter.
41882 **
41883 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
41884 ** of the database to damage due to OS crashes or power failures by
41885 ** changing the number of syncs()s when writing the journals.
41886 ** There are three levels:
41887 **
41888 ** OFF sqlite3OsSync() is never called. This is the default
41889 ** for temporary and transient files.
41890 **
41891 ** NORMAL The journal is synced once before writes begin on the
@@ -41788,26 +41922,25 @@
41922 **
41923 ** Numeric values associated with these states are OFF==1, NORMAL=2,
41924 ** and FULL=3.
41925 */
41926 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
41927 SQLITE_PRIVATE void sqlite3PagerSetFlags(
41928 Pager *pPager, /* The pager to set safety level for */
41929 unsigned pgFlags /* Various flags */
 
 
41930 ){
41931 unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
41932 assert( level>=1 && level<=3 );
41933 pPager->noSync = (level==1 || pPager->tempFile) ?1:0;
41934 pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
41935 if( pPager->noSync ){
41936 pPager->syncFlags = 0;
41937 pPager->ckptSyncFlags = 0;
41938 }else if( pgFlags & PAGER_FULLFSYNC ){
41939 pPager->syncFlags = SQLITE_SYNC_FULL;
41940 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41941 }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
41942 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41943 pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
41944 }else{
41945 pPager->syncFlags = SQLITE_SYNC_NORMAL;
41946 pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
@@ -41814,10 +41947,15 @@
41947 }
41948 pPager->walSyncFlags = pPager->syncFlags;
41949 if( pPager->fullSync ){
41950 pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
41951 }
41952 if( pgFlags & PAGER_CACHESPILL ){
41953 pPager->doNotSpill &= ~SPILLFLAG_OFF;
41954 }else{
41955 pPager->doNotSpill |= SPILLFLAG_OFF;
41956 }
41957 }
41958 #endif
41959
41960 /*
41961 ** The following global variable is incremented whenever the library
@@ -42714,28 +42852,34 @@
42852 int rc = SQLITE_OK;
42853
42854 assert( pPg->pPager==pPager );
42855 assert( pPg->flags&PGHDR_DIRTY );
42856
42857 /* The doNotSpill NOSYNC bit is set during times when doing a sync of
42858 ** journal (and adding a new header) is not allowed. This occurs
42859 ** during calls to sqlite3PagerWrite() while trying to journal multiple
42860 ** pages belonging to the same sector.
42861 **
42862 ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
42863 ** regardless of whether or not a sync is required. This is set during
42864 ** a rollback or by user request, respectively.
42865 **
42866 ** Spilling is also prohibited when in an error state since that could
42867 ** lead to database corruption. In the current implementaton it
42868 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
42869 ** while in the error state, hence it is impossible for this routine to
42870 ** be called in the error state. Nevertheless, we include a NEVER()
42871 ** test for the error state as a safeguard against future changes.
42872 */
42873 if( NEVER(pPager->errCode) ) return SQLITE_OK;
42874 testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
42875 testcase( pPager->doNotSpill & SPILLFLAG_OFF );
42876 testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
42877 if( pPager->doNotSpill
42878 && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
42879 || (pPg->flags & PGHDR_NEED_SYNC)!=0)
42880 ){
42881 return SQLITE_OK;
42882 }
42883
42884 pPg->pDirty = 0;
42885 if( pagerUseWal(pPager) ){
@@ -43553,23 +43697,23 @@
43697 */
43698 SQLITE_PRIVATE int sqlite3PagerAcquire(
43699 Pager *pPager, /* The pager open on the database file */
43700 Pgno pgno, /* Page number to fetch */
43701 DbPage **ppPage, /* Write a pointer to the page here */
43702 int flags /* PAGER_GET_XXX flags */
43703 ){
43704 int rc = SQLITE_OK;
43705 PgHdr *pPg = 0;
43706 u32 iFrame = 0; /* Frame to read from WAL file */
43707 const int noContent = (flags & PAGER_GET_NOCONTENT);
43708
43709 /* It is acceptable to use a read-only (mmap) page for any page except
43710 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
43711 ** flag was specified by the caller. And so long as the db is not a
43712 ** temporary or in-memory database. */
43713 const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
43714 && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
43715 #ifdef SQLITE_HAS_CODEC
43716 && pPager->xCodec==0
43717 #endif
43718 );
43719
@@ -44085,17 +44229,17 @@
44229 Pgno pg1; /* First page of the sector pPg is located on. */
44230 int nPage = 0; /* Number of pages starting at pg1 to journal */
44231 int ii; /* Loop counter */
44232 int needSync = 0; /* True if any page has PGHDR_NEED_SYNC */
44233
44234 /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
44235 ** a journal header to be written between the pages journaled by
44236 ** this function.
44237 */
44238 assert( !MEMDB );
44239 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
44240 pPager->doNotSpill |= SPILLFLAG_NOSYNC;
44241
44242 /* This trick assumes that both the page-size and sector-size are
44243 ** an integer power of 2. It sets variable pg1 to the identifier
44244 ** of the first page of the sector pPg is located on.
44245 */
@@ -44150,12 +44294,12 @@
44294 sqlite3PagerUnref(pPage);
44295 }
44296 }
44297 }
44298
44299 assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
44300 pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
44301 }else{
44302 rc = pager_write(pDbPage);
44303 }
44304 return rc;
44305 }
@@ -49147,18 +49291,23 @@
49291 };
49292
49293 /*
49294 ** Potential values for BtCursor.eState.
49295 **
 
 
 
49296 ** CURSOR_INVALID:
49297 ** Cursor does not point to a valid entry. This can happen (for example)
49298 ** because the table is empty or because BtreeCursorFirst() has not been
49299 ** called.
49300 **
49301 ** CURSOR_VALID:
49302 ** Cursor points to a valid entry. getPayload() etc. may be called.
49303 **
49304 ** CURSOR_SKIPNEXT:
49305 ** Cursor is valid except that the Cursor.skipNext field is non-zero
49306 ** indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
49307 ** operation should be a no-op.
49308 **
49309 ** CURSOR_REQUIRESEEK:
49310 ** The table that this cursor was opened on still exists, but has been
49311 ** modified since the cursor was last used. The cursor position is saved
49312 ** in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
49313 ** this state, restoreCursorPosition() can be called to attempt to
@@ -49171,12 +49320,13 @@
49320 ** Do nothing else with this cursor. Any attempt to use the cursor
49321 ** should return the error code stored in BtCursor.skip
49322 */
49323 #define CURSOR_INVALID 0
49324 #define CURSOR_VALID 1
49325 #define CURSOR_SKIPNEXT 2
49326 #define CURSOR_REQUIRESEEK 3
49327 #define CURSOR_FAULT 4
49328
49329 /*
49330 ** The database page the PENDING_BYTE occupies. This page is never used.
49331 */
49332 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
@@ -50286,10 +50436,13 @@
50436 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
50437 if( rc==SQLITE_OK ){
50438 sqlite3_free(pCur->pKey);
50439 pCur->pKey = 0;
50440 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
50441 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
50442 pCur->eState = CURSOR_SKIPNEXT;
50443 }
50444 }
50445 return rc;
50446 }
50447
50448 #define restoreCursorPosition(p) \
@@ -50311,11 +50464,11 @@
50464 rc = restoreCursorPosition(pCur);
50465 if( rc ){
50466 *pHasMoved = 1;
50467 return rc;
50468 }
50469 if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
50470 *pHasMoved = 1;
50471 }else{
50472 *pHasMoved = 0;
50473 }
50474 return SQLITE_OK;
@@ -50499,11 +50652,12 @@
50652 assert( pPage->leaf==0 || pPage->leaf==1 );
50653 n = pPage->childPtrSize;
50654 assert( n==4-4*pPage->leaf );
50655 if( pPage->intKey ){
50656 if( pPage->hasData ){
50657 assert( n==0 );
50658 n = getVarint32(pCell, nPayload);
50659 }else{
50660 nPayload = 0;
50661 }
50662 n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
50663 pInfo->nData = nPayload;
@@ -51143,19 +51297,16 @@
51297 */
51298 static int btreeGetPage(
51299 BtShared *pBt, /* The btree */
51300 Pgno pgno, /* Number of the page to fetch */
51301 MemPage **ppPage, /* Return the page in this parameter */
51302 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
 
51303 ){
51304 int rc;
51305 DbPage *pDbPage;
 
 
51306
51307 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
51308 assert( sqlite3_mutex_held(pBt->mutex) );
51309 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
51310 if( rc ) return rc;
51311 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
51312 return SQLITE_OK;
@@ -51199,19 +51350,20 @@
51350 */
51351 static int getAndInitPage(
51352 BtShared *pBt, /* The database file */
51353 Pgno pgno, /* Number of the page to get */
51354 MemPage **ppPage, /* Write the page pointer here */
51355 int bReadonly /* PAGER_GET_READONLY or 0 */
51356 ){
51357 int rc;
51358 assert( sqlite3_mutex_held(pBt->mutex) );
51359 assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
51360
51361 if( pgno>btreePagecount(pBt) ){
51362 rc = SQLITE_CORRUPT_BKPT;
51363 }else{
51364 rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
51365 if( rc==SQLITE_OK ){
51366 rc = btreeInitPage(*ppPage);
51367 if( rc!=SQLITE_OK ){
51368 releasePage(*ppPage);
51369 }
@@ -51727,21 +51879,18 @@
51879 ** there is a high probability of damage) Level 2 is the default. There
51880 ** is a very low but non-zero probability of damage. Level 3 reduces the
51881 ** probability of damage to near zero but with a write performance reduction.
51882 */
51883 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
51884 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
51885 Btree *p, /* The btree to set the safety level on */
51886 unsigned pgFlags /* Various PAGER_* flags */
 
 
51887 ){
51888 BtShared *pBt = p->pBt;
51889 assert( sqlite3_mutex_held(p->db->mutex) );
 
51890 sqlite3BtreeEnter(p);
51891 sqlite3PagerSetFlags(pBt->pPager, pgFlags);
51892 sqlite3BtreeLeave(p);
51893 return SQLITE_OK;
51894 }
51895 #endif
51896
@@ -51943,11 +52092,11 @@
52092
52093 assert( sqlite3_mutex_held(pBt->mutex) );
52094 assert( pBt->pPage1==0 );
52095 rc = sqlite3PagerSharedLock(pBt->pPager);
52096 if( rc!=SQLITE_OK ) return rc;
52097 rc = btreeGetPage(pBt, 1, &pPage1, 0);
52098 if( rc!=SQLITE_OK ) return rc;
52099
52100 /* Do some checking to help insure the file we opened really is
52101 ** a valid database file.
52102 */
@@ -52071,10 +52220,11 @@
52220 pBt->max1bytePayload = (u8)pBt->maxLocal;
52221 }
52222 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
52223 pBt->pPage1 = pPage1;
52224 pBt->nPage = nPage;
52225 assert( pPage1->leaf==0 || pPage1->leaf==1 );
52226 return SQLITE_OK;
52227
52228 page1_init_failed:
52229 releasePage(pPage1);
52230 pBt->pPage1 = 0;
@@ -52525,11 +52675,11 @@
52675 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
52676 ** that it points at iFreePage. Also fix the pointer map entry for
52677 ** iPtrPage.
52678 */
52679 if( eType!=PTRMAP_ROOTPAGE ){
52680 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
52681 if( rc!=SQLITE_OK ){
52682 return rc;
52683 }
52684 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
52685 if( rc!=SQLITE_OK ){
@@ -52609,11 +52759,11 @@
52759 Pgno iFreePg; /* Index of free page to move pLastPg to */
52760 MemPage *pLastPg;
52761 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
52762 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
52763
52764 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
52765 if( rc!=SQLITE_OK ){
52766 return rc;
52767 }
52768
52769 /* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -53008,11 +53158,11 @@
53158 }
53159
53160 /* The rollback may have destroyed the pPage1->aData value. So
53161 ** call btreeGetPage() on page 1 again to make
53162 ** sure pPage1->aData is set correctly. */
53163 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
53164 int nPage = get4byte(28+(u8*)pPage1->aData);
53165 testcase( nPage==0 );
53166 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
53167 testcase( pBt->nPage!=nPage );
53168 pBt->nPage = nPage;
@@ -53443,11 +53593,11 @@
53593 }
53594 #endif
53595
53596 assert( next==0 || rc==SQLITE_DONE );
53597 if( rc==SQLITE_OK ){
53598 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
53599 assert( rc==SQLITE_OK || pPage==0 );
53600 if( rc==SQLITE_OK ){
53601 next = get4byte(pPage->aData);
53602 }
53603 }
@@ -53665,11 +53815,11 @@
53815 #endif
53816
53817 {
53818 DbPage *pDbPage;
53819 rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
53820 (eOp==0 ? PAGER_GET_READONLY : 0)
53821 );
53822 if( rc==SQLITE_OK ){
53823 aPayload = sqlite3PagerGetData(pDbPage);
53824 nextPage = get4byte(aPayload);
53825 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
@@ -53849,11 +53999,12 @@
53999 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
54000 assert( pCur->iPage>=0 );
54001 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
54002 return SQLITE_CORRUPT_BKPT;
54003 }
54004 rc = getAndInitPage(pBt, newPgno, &pNewPage,
54005 pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
54006 if( rc ) return rc;
54007 pCur->apPage[i+1] = pNewPage;
54008 pCur->aiIdx[i+1] = 0;
54009 pCur->iPage++;
54010
@@ -53966,11 +54117,12 @@
54117 pCur->iPage = 0;
54118 }else if( pCur->pgnoRoot==0 ){
54119 pCur->eState = CURSOR_INVALID;
54120 return SQLITE_OK;
54121 }else{
54122 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0],
54123 pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
54124 if( rc!=SQLITE_OK ){
54125 pCur->eState = CURSOR_INVALID;
54126 return rc;
54127 }
54128 pCur->iPage = 0;
@@ -54361,25 +54513,33 @@
54513 int rc;
54514 int idx;
54515 MemPage *pPage;
54516
54517 assert( cursorHoldsMutex(pCur) );
 
 
 
 
54518 assert( pRes!=0 );
54519 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
54520 if( pCur->eState!=CURSOR_VALID ){
54521 rc = restoreCursorPosition(pCur);
54522 if( rc!=SQLITE_OK ){
54523 *pRes = 0;
54524 return rc;
54525 }
54526 if( CURSOR_INVALID==pCur->eState ){
54527 *pRes = 1;
54528 return SQLITE_OK;
54529 }
54530 if( pCur->skipNext ){
54531 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
54532 pCur->eState = CURSOR_VALID;
54533 if( pCur->skipNext>0 ){
54534 pCur->skipNext = 0;
54535 *pRes = 0;
54536 return SQLITE_OK;
54537 }
54538 pCur->skipNext = 0;
54539 }
54540 }
54541
54542 pPage = pCur->apPage[pCur->iPage];
54543 idx = ++pCur->aiIdx[pCur->iPage];
54544 assert( pPage->isInit );
54545
@@ -54393,11 +54553,14 @@
54553 pCur->info.nSize = 0;
54554 pCur->validNKey = 0;
54555 if( idx>=pPage->nCell ){
54556 if( !pPage->leaf ){
54557 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
54558 if( rc ){
54559 *pRes = 0;
54560 return rc;
54561 }
54562 rc = moveToLeftmost(pCur);
54563 *pRes = 0;
54564 return rc;
54565 }
54566 do{
@@ -54435,32 +54598,44 @@
54598 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
54599 int rc;
54600 MemPage *pPage;
54601
54602 assert( cursorHoldsMutex(pCur) );
54603 assert( pRes!=0 );
54604 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
 
 
54605 pCur->atLast = 0;
54606 if( pCur->eState!=CURSOR_VALID ){
54607 if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
54608 rc = btreeRestoreCursorPosition(pCur);
54609 if( rc!=SQLITE_OK ){
54610 *pRes = 0;
54611 return rc;
54612 }
54613 }
54614 if( CURSOR_INVALID==pCur->eState ){
54615 *pRes = 1;
54616 return SQLITE_OK;
54617 }
54618 if( pCur->skipNext ){
54619 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
54620 pCur->eState = CURSOR_VALID;
54621 if( pCur->skipNext<0 ){
54622 pCur->skipNext = 0;
54623 *pRes = 0;
54624 return SQLITE_OK;
54625 }
54626 pCur->skipNext = 0;
54627 }
54628 }
54629
54630 pPage = pCur->apPage[pCur->iPage];
54631 assert( pPage->isInit );
54632 if( !pPage->leaf ){
54633 int idx = pCur->aiIdx[pCur->iPage];
54634 rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
54635 if( rc ){
54636 *pRes = 0;
54637 return rc;
54638 }
54639 rc = moveToRightmost(pCur);
54640 }else{
54641 while( pCur->aiIdx[pCur->iPage]==0 ){
@@ -54580,11 +54755,11 @@
54755 }
54756 testcase( iTrunk==mxPage );
54757 if( iTrunk>mxPage ){
54758 rc = SQLITE_CORRUPT_BKPT;
54759 }else{
54760 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
54761 }
54762 if( rc ){
54763 pTrunk = 0;
54764 goto end_allocate_page;
54765 }
@@ -54644,11 +54819,11 @@
54819 if( iNewTrunk>mxPage ){
54820 rc = SQLITE_CORRUPT_BKPT;
54821 goto end_allocate_page;
54822 }
54823 testcase( iNewTrunk==mxPage );
54824 rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
54825 if( rc!=SQLITE_OK ){
54826 goto end_allocate_page;
54827 }
54828 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
54829 if( rc!=SQLITE_OK ){
@@ -54723,12 +54898,12 @@
54898 if( rc ) goto end_allocate_page;
54899 if( closest<k-1 ){
54900 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
54901 }
54902 put4byte(&aData[4], k-1);
54903 noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
54904 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
54905 if( rc==SQLITE_OK ){
54906 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54907 if( rc!=SQLITE_OK ){
54908 releasePage(*ppPage);
54909 }
@@ -54756,11 +54931,11 @@
54931 ** content for any page that really does lie past the end of the database
54932 ** file on disk. So the effects of disabling the no-content optimization
54933 ** here are confined to those pages that lie between the end of the
54934 ** database image and the end of the database file.
54935 */
54936 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
54937
54938 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
54939 if( rc ) return rc;
54940 pBt->nPage++;
54941 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
@@ -54772,11 +54947,11 @@
54947 ** becomes a new pointer-map page, the second is used by the caller.
54948 */
54949 MemPage *pPg = 0;
54950 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
54951 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
54952 rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
54953 if( rc==SQLITE_OK ){
54954 rc = sqlite3PagerWrite(pPg->pDbPage);
54955 releasePage(pPg);
54956 }
54957 if( rc ) return rc;
@@ -54786,11 +54961,11 @@
54961 #endif
54962 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
54963 *pPgno = pBt->nPage;
54964
54965 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
54966 rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
54967 if( rc ) return rc;
54968 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54969 if( rc!=SQLITE_OK ){
54970 releasePage(*ppPage);
54971 }
@@ -54854,11 +55029,11 @@
55029
55030 if( pBt->btsFlags & BTS_SECURE_DELETE ){
55031 /* If the secure_delete option is enabled, then
55032 ** always fully overwrite deleted information with zeros.
55033 */
55034 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
55035 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
55036 ){
55037 goto freepage_out;
55038 }
55039 memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -54881,11 +55056,11 @@
55056 */
55057 if( nFree!=0 ){
55058 u32 nLeaf; /* Initial number of leaf cells on trunk page */
55059
55060 iTrunk = get4byte(&pPage1->aData[32]);
55061 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
55062 if( rc!=SQLITE_OK ){
55063 goto freepage_out;
55064 }
55065
55066 nLeaf = get4byte(&pTrunk->aData[4]);
@@ -54927,11 +55102,11 @@
55102 ** the page being freed as a leaf page of the first trunk in the free-list.
55103 ** Possibly because the free-list is empty, or possibly because the
55104 ** first trunk in the free-list is full. Either way, the page being freed
55105 ** will become the new first trunk page in the free-list.
55106 */
55107 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
55108 goto freepage_out;
55109 }
55110 rc = sqlite3PagerWrite(pPage->pDbPage);
55111 if( rc!=SQLITE_OK ){
55112 goto freepage_out;
@@ -56826,11 +57001,11 @@
57001 if( rc!=SQLITE_OK ){
57002 return rc;
57003 }
57004
57005 /* Move the page currently at pgnoRoot to pgnoMove. */
57006 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
57007 if( rc!=SQLITE_OK ){
57008 return rc;
57009 }
57010 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
57011 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -56847,11 +57022,11 @@
57022
57023 /* Obtain the page at pgnoRoot */
57024 if( rc!=SQLITE_OK ){
57025 return rc;
57026 }
57027 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
57028 if( rc!=SQLITE_OK ){
57029 return rc;
57030 }
57031 rc = sqlite3PagerWrite(pRoot->pDbPage);
57032 if( rc!=SQLITE_OK ){
@@ -57025,11 +57200,11 @@
57200 if( NEVER(pBt->pCursor) ){
57201 sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
57202 return SQLITE_LOCKED_SHAREDCACHE;
57203 }
57204
57205 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
57206 if( rc ) return rc;
57207 rc = sqlite3BtreeClearTable(p, iTable, 0);
57208 if( rc ){
57209 releasePage(pPage);
57210 return rc;
@@ -57060,21 +57235,21 @@
57235 ** number in the database. So move the page that does into the
57236 ** gap left by the deleted root-page.
57237 */
57238 MemPage *pMove;
57239 releasePage(pPage);
57240 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
57241 if( rc!=SQLITE_OK ){
57242 return rc;
57243 }
57244 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
57245 releasePage(pMove);
57246 if( rc!=SQLITE_OK ){
57247 return rc;
57248 }
57249 pMove = 0;
57250 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
57251 freePage(pMove, &rc);
57252 releasePage(pMove);
57253 if( rc!=SQLITE_OK ){
57254 return rc;
57255 }
@@ -57285,11 +57460,11 @@
57460 if( zMsg1 ){
57461 sqlite3StrAccumAppend(&pCheck->errMsg, zMsg1, -1);
57462 }
57463 sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
57464 va_end(ap);
57465 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
57466 pCheck->mallocFailed = 1;
57467 }
57468 }
57469 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
57470
@@ -57482,11 +57657,11 @@
57657 */
57658 pBt = pCheck->pBt;
57659 usableSize = pBt->usableSize;
57660 if( iPage==0 ) return 0;
57661 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
57662 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
57663 checkAppendMsg(pCheck, zContext,
57664 "unable to get the page. error code=%d", rc);
57665 return 0;
57666 }
57667
@@ -58441,11 +58616,11 @@
58616 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
58617 const Pgno iSrcPg = p->iNext; /* Source page number */
58618 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
58619 DbPage *pSrcPg; /* Source page object */
58620 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
58621 PAGER_GET_READONLY);
58622 if( rc==SQLITE_OK ){
58623 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
58624 sqlite3PagerUnref(pSrcPg);
58625 }
58626 }
@@ -59596,38 +59771,33 @@
59771 /* If one value is a number and the other is not, the number is less.
59772 ** If both are numbers, compare as reals if one is a real, or as integers
59773 ** if both values are integers.
59774 */
59775 if( combined_flags&(MEM_Int|MEM_Real) ){
59776 double r1, r2;
59777 if( (f1 & f2 & MEM_Int)!=0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59778 if( pMem1->u.i < pMem2->u.i ) return -1;
59779 if( pMem1->u.i > pMem2->u.i ) return 1;
59780 return 0;
59781 }
59782 if( (f1&MEM_Real)!=0 ){
59783 r1 = pMem1->r;
59784 }else if( (f1&MEM_Int)!=0 ){
59785 r1 = (double)pMem1->u.i;
59786 }else{
59787 return 1;
59788 }
59789 if( (f2&MEM_Real)!=0 ){
59790 r2 = pMem2->r;
59791 }else if( (f2&MEM_Int)!=0 ){
59792 r2 = (double)pMem2->u.i;
59793 }else{
59794 return -1;
59795 }
59796 if( r1<r2 ) return -1;
59797 if( r1>r2 ) return 1;
59798 return 0;
59799 }
59800
59801 /* If one value is a string and the other is a blob, the string is less.
59802 ** If both are strings, compare using the collating functions.
59803 */
@@ -59803,43 +59973,108 @@
59973 }
59974 return p;
59975 }
59976
59977 /*
59978 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
59979 ** valueNew(). See comments above valueNew() for details.
59980 */
59981 struct ValueNewStat4Ctx {
59982 Parse *pParse;
59983 Index *pIdx;
59984 UnpackedRecord **ppRec;
59985 int iVal;
59986 };
59987
59988 /*
59989 ** Allocate and return a pointer to a new sqlite3_value object. If
59990 ** the second argument to this function is NULL, the object is allocated
59991 ** by calling sqlite3ValueNew().
59992 **
59993 ** Otherwise, if the second argument is non-zero, then this function is
59994 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
59995 ** already been allocated, allocate the UnpackedRecord structure that
59996 ** that function will return to its caller here. Then return a pointer
59997 ** an sqlite3_value within the UnpackedRecord.a[] array.
 
59998 */
59999 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60000 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60001 if( p ){
60002 UnpackedRecord *pRec = p->ppRec[0];
60003
60004 if( pRec==0 ){
60005 Index *pIdx = p->pIdx; /* Index being probed */
60006 int nByte; /* Bytes of space to allocate */
60007 int i; /* Counter variable */
60008 int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */
60009
60010 nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord);
60011 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60012 if( pRec ){
60013 pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx);
60014 if( pRec->pKeyInfo ){
60015 assert( pRec->pKeyInfo->nField+1==nCol );
60016 pRec->pKeyInfo->enc = ENC(db);
60017 pRec->flags = UNPACKED_PREFIX_MATCH;
60018 pRec->aMem = (Mem *)&pRec[1];
60019 for(i=0; i<nCol; i++){
60020 pRec->aMem[i].flags = MEM_Null;
60021 pRec->aMem[i].type = SQLITE_NULL;
60022 pRec->aMem[i].db = db;
60023 }
60024 }else{
60025 sqlite3DbFree(db, pRec);
60026 pRec = 0;
60027 }
60028 }
60029 if( pRec==0 ) return 0;
60030 p->ppRec[0] = pRec;
60031 }
60032
60033 pRec->nField = p->iVal+1;
60034 return &pRec->aMem[p->iVal];
60035 }
60036 #endif
60037 return sqlite3ValueNew(db);
60038 }
60039
60040 /*
60041 ** Extract a value from the supplied expression in the manner described
60042 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
60043 ** using valueNew().
60044 **
60045 ** If pCtx is NULL and an error occurs after the sqlite3_value object
60046 ** has been allocated, it is freed before returning. Or, if pCtx is not
60047 ** NULL, it is assumed that the caller will free any allocated object
60048 ** in all cases.
60049 */
60050 int valueFromExpr(
60051 sqlite3 *db, /* The database connection */
60052 Expr *pExpr, /* The expression to evaluate */
60053 u8 enc, /* Encoding to use */
60054 u8 affinity, /* Affinity to use */
60055 sqlite3_value **ppVal, /* Write the new value here */
60056 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
60057 ){
60058 int op;
60059 char *zVal = 0;
60060 sqlite3_value *pVal = 0;
60061 int negInt = 1;
60062 const char *zNeg = "";
60063 int rc = SQLITE_OK;
60064
60065 if( !pExpr ){
60066 *ppVal = 0;
60067 return SQLITE_OK;
60068 }
60069 op = pExpr->op;
60070
60071 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
60072 ** The ifdef here is to enable us to achieve 100% branch test coverage even
60073 ** when SQLITE_ENABLE_STAT4 is omitted.
60074 */
60075 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60076 if( op==TK_REGISTER ) op = pExpr->op2;
60077 #else
60078 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
60079 #endif
60080
@@ -59853,11 +60088,11 @@
60088 negInt = -1;
60089 zNeg = "-";
60090 }
60091
60092 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
60093 pVal = valueNew(db, pCtx);
60094 if( pVal==0 ) goto no_mem;
60095 if( ExprHasProperty(pExpr, EP_IntValue) ){
60096 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
60097 }else{
60098 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59870,15 +60105,17 @@
60105 }else{
60106 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60107 }
60108 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
60109 if( enc!=SQLITE_UTF8 ){
60110 rc = sqlite3VdbeChangeEncoding(pVal, enc);
60111 }
60112 }else if( op==TK_UMINUS ) {
60113 /* This branch happens for multiple negative signs. Ex: -(-5) */
60114 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
60115 && pVal!=0
60116 ){
60117 sqlite3VdbeMemNumerify(pVal);
60118 if( pVal->u.i==SMALLEST_INT64 ){
60119 pVal->flags &= MEM_Int;
60120 pVal->flags |= MEM_Real;
60121 pVal->r = (double)LARGEST_INT64;
@@ -59887,19 +60124,19 @@
60124 }
60125 pVal->r = -pVal->r;
60126 sqlite3ValueApplyAffinity(pVal, affinity, enc);
60127 }
60128 }else if( op==TK_NULL ){
60129 pVal = valueNew(db, pCtx);
60130 if( pVal==0 ) goto no_mem;
60131 }
60132 #ifndef SQLITE_OMIT_BLOB_LITERAL
60133 else if( op==TK_BLOB ){
60134 int nVal;
60135 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
60136 assert( pExpr->u.zToken[1]=='\'' );
60137 pVal = valueNew(db, pCtx);
60138 if( !pVal ) goto no_mem;
60139 zVal = &pExpr->u.zToken[2];
60140 nVal = sqlite3Strlen30(zVal)-1;
60141 assert( zVal[nVal]=='\'' );
60142 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59909,20 +60146,203 @@
60146
60147 if( pVal ){
60148 sqlite3VdbeMemStoreType(pVal);
60149 }
60150 *ppVal = pVal;
60151 return rc;
60152
60153 no_mem:
60154 db->mallocFailed = 1;
60155 sqlite3DbFree(db, zVal);
60156 assert( *ppVal==0 );
60157 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60158 if( pCtx==0 ) sqlite3ValueFree(pVal);
60159 #else
60160 assert( pCtx==0 ); sqlite3ValueFree(pVal);
60161 #endif
60162 return SQLITE_NOMEM;
60163 }
60164
60165 /*
60166 ** Create a new sqlite3_value object, containing the value of pExpr.
60167 **
60168 ** This only works for very simple expressions that consist of one constant
60169 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
60170 ** be converted directly into a value, then the value is allocated and
60171 ** a pointer written to *ppVal. The caller is responsible for deallocating
60172 ** the value by passing it to sqlite3ValueFree() later on. If the expression
60173 ** cannot be converted to a value, then *ppVal is set to NULL.
60174 */
60175 SQLITE_PRIVATE int sqlite3ValueFromExpr(
60176 sqlite3 *db, /* The database connection */
60177 Expr *pExpr, /* The expression to evaluate */
60178 u8 enc, /* Encoding to use */
60179 u8 affinity, /* Affinity to use */
60180 sqlite3_value **ppVal /* Write the new value here */
60181 ){
60182 return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
60183 }
60184
60185 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60186 /*
60187 ** The implementation of the sqlite_record() function. This function accepts
60188 ** a single argument of any type. The return value is a formatted database
60189 ** record (a blob) containing the argument value.
60190 **
60191 ** This is used to convert the value stored in the 'sample' column of the
60192 ** sqlite_stat3 table to the record format SQLite uses internally.
60193 */
60194 static void recordFunc(
60195 sqlite3_context *context,
60196 int argc,
60197 sqlite3_value **argv
60198 ){
60199 const int file_format = 1;
60200 int iSerial; /* Serial type */
60201 int nSerial; /* Bytes of space for iSerial as varint */
60202 int nVal; /* Bytes of space required for argv[0] */
60203 int nRet;
60204 sqlite3 *db;
60205 u8 *aRet;
60206
60207 iSerial = sqlite3VdbeSerialType(argv[0], file_format);
60208 nSerial = sqlite3VarintLen(iSerial);
60209 nVal = sqlite3VdbeSerialTypeLen(iSerial);
60210 db = sqlite3_context_db_handle(context);
60211
60212 nRet = 1 + nSerial + nVal;
60213 aRet = sqlite3DbMallocRaw(db, nRet);
60214 if( aRet==0 ){
60215 sqlite3_result_error_nomem(context);
60216 }else{
60217 aRet[0] = nSerial+1;
60218 sqlite3PutVarint(&aRet[1], iSerial);
60219 sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
60220 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
60221 sqlite3DbFree(db, aRet);
60222 }
60223 }
60224
60225 /*
60226 ** Register built-in functions used to help read ANALYZE data.
60227 */
60228 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
60229 static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
60230 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
60231 };
60232 int i;
60233 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
60234 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
60235 for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
60236 sqlite3FuncDefInsert(pHash, &aFunc[i]);
60237 }
60238 }
60239
60240 /*
60241 ** This function is used to allocate and populate UnpackedRecord
60242 ** structures intended to be compared against sample index keys stored
60243 ** in the sqlite_stat4 table.
60244 **
60245 ** A single call to this function attempts to populates field iVal (leftmost
60246 ** is 0 etc.) of the unpacked record with a value extracted from expression
60247 ** pExpr. Extraction of values is possible if:
60248 **
60249 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
60250 **
60251 ** * The expression is a bound variable, and this is a reprepare, or
60252 **
60253 ** * The sqlite3ValueFromExpr() function is able to extract a value
60254 ** from the expression (i.e. the expression is a literal value).
60255 **
60256 ** If a value can be extracted, the affinity passed as the 5th argument
60257 ** is applied to it before it is copied into the UnpackedRecord. Output
60258 ** parameter *pbOk is set to true if a value is extracted, or false
60259 ** otherwise.
60260 **
60261 ** When this function is called, *ppRec must either point to an object
60262 ** allocated by an earlier call to this function, or must be NULL. If it
60263 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
60264 ** is allocated (and *ppRec set to point to it) before returning.
60265 **
60266 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
60267 ** error if a value cannot be extracted from pExpr. If an error does
60268 ** occur, an SQLite error code is returned.
60269 */
60270 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
60271 Parse *pParse, /* Parse context */
60272 Index *pIdx, /* Index being probed */
60273 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
60274 Expr *pExpr, /* The expression to extract a value from */
60275 u8 affinity, /* Affinity to use */
60276 int iVal, /* Array element to populate */
60277 int *pbOk /* OUT: True if value was extracted */
60278 ){
60279 int rc = SQLITE_OK;
60280 sqlite3_value *pVal = 0;
60281
60282 struct ValueNewStat4Ctx alloc;
60283 alloc.pParse = pParse;
60284 alloc.pIdx = pIdx;
60285 alloc.ppRec = ppRec;
60286 alloc.iVal = iVal;
60287
60288 if( !pExpr ){
60289 pVal = valueNew(pParse->db, &alloc);
60290 if( pVal ){
60291 sqlite3VdbeMemSetNull((Mem*)pVal);
60292 *pbOk = 1;
60293 }
60294 }else if( pExpr->op==TK_VARIABLE
60295 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
60296 ){
60297 Vdbe *v;
60298 int iBindVar = pExpr->iColumn;
60299 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
60300 if( (v = pParse->pReprepare)!=0 ){
60301 pVal = valueNew(pParse->db, &alloc);
60302 if( pVal ){
60303 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
60304 if( rc==SQLITE_OK ){
60305 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60306 }
60307 pVal->db = pParse->db;
60308 *pbOk = 1;
60309 sqlite3VdbeMemStoreType((Mem*)pVal);
60310 }
60311 }else{
60312 *pbOk = 0;
60313 }
60314 }else{
60315 sqlite3 *db = pParse->db;
60316 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
60317 *pbOk = (pVal!=0);
60318 }
60319
60320 assert( pVal==0 || pVal->db==pParse->db );
60321 return rc;
60322 }
60323
60324 /*
60325 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
60326 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
60327 ** the object.
60328 */
60329 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
60330 if( pRec ){
60331 int i;
60332 int nCol = pRec->pKeyInfo->nField+1;
60333 Mem *aMem = pRec->aMem;
60334 sqlite3 *db = aMem[0].db;
60335 for(i=0; i<nCol; i++){
60336 sqlite3DbFree(db, aMem[i].zMalloc);
60337 }
60338 sqlite3DbFree(db, pRec->pKeyInfo);
60339 sqlite3DbFree(db, pRec);
60340 }
60341 }
60342 #endif /* ifdef SQLITE_ENABLE_STAT4 */
60343
60344 /*
60345 ** Change the string value of an sqlite3_value object
60346 */
60347 SQLITE_PRIVATE void sqlite3ValueSetStr(
60348 sqlite3_value *v, /* Value to be set */
@@ -61724,11 +62144,11 @@
62144 ** virtual module tables written in this transaction. This has to
62145 ** be done before determining whether a master journal file is
62146 ** required, as an xSync() callback may add an attached database
62147 ** to the transaction.
62148 */
62149 rc = sqlite3VtabSync(db, p);
62150
62151 /* This loop determines (a) if the commit hook should be invoked and
62152 ** (b) how many database files have open write transactions, not
62153 ** including the temp database. (b) is important because if more than
62154 ** one database file has an open write transaction, a master journal
@@ -63264,10 +63684,25 @@
63684 }else{
63685 v->expmask |= ((u32)1 << (iVar-1));
63686 }
63687 }
63688
63689 #ifndef SQLITE_OMIT_VIRTUALTABLE
63690 /*
63691 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
63692 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
63693 ** in memory obtained from sqlite3DbMalloc).
63694 */
63695 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
63696 sqlite3 *db = p->db;
63697 sqlite3DbFree(db, p->zErrMsg);
63698 p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
63699 sqlite3_free(pVtab->zErrMsg);
63700 pVtab->zErrMsg = 0;
63701 }
63702 #endif /* SQLITE_OMIT_VIRTUALTABLE */
63703
63704 /************** End of vdbeaux.c *********************************************/
63705 /************** Begin file vdbeapi.c *****************************************/
63706 /*
63707 ** 2004 May 26
63708 **
@@ -63477,16 +63912,18 @@
63912 sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
63913 }
63914 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
63915 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63916 pCtx->isError = SQLITE_ERROR;
63917 pCtx->fErrorOrAux = 1;
63918 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
63919 }
63920 #ifndef SQLITE_OMIT_UTF16
63921 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
63922 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63923 pCtx->isError = SQLITE_ERROR;
63924 pCtx->fErrorOrAux = 1;
63925 sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
63926 }
63927 #endif
63928 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
63929 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
@@ -63546,10 +63983,11 @@
63983 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63984 sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
63985 }
63986 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
63987 pCtx->isError = errCode;
63988 pCtx->fErrorOrAux = 1;
63989 if( pCtx->s.flags & MEM_Null ){
63990 sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
63991 SQLITE_UTF8, SQLITE_STATIC);
63992 }
63993 }
@@ -63556,19 +63994,21 @@
63994
63995 /* Force an SQLITE_TOOBIG error. */
63996 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
63997 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
63998 pCtx->isError = SQLITE_TOOBIG;
63999 pCtx->fErrorOrAux = 1;
64000 sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
64001 SQLITE_UTF8, SQLITE_STATIC);
64002 }
64003
64004 /* An SQLITE_NOMEM error. */
64005 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
64006 assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64007 sqlite3VdbeMemSetNull(&pCtx->s);
64008 pCtx->isError = SQLITE_NOMEM;
64009 pCtx->fErrorOrAux = 1;
64010 pCtx->s.db->mallocFailed = 1;
64011 }
64012
64013 /*
64014 ** This function is called after a transaction has been committed. It
@@ -63887,10 +64327,14 @@
64327 if( !pAuxData ) goto failed;
64328 pAuxData->iOp = pCtx->iOp;
64329 pAuxData->iArg = iArg;
64330 pAuxData->pNext = pVdbe->pAuxData;
64331 pVdbe->pAuxData = pAuxData;
64332 if( pCtx->fErrorOrAux==0 ){
64333 pCtx->isError = 0;
64334 pCtx->fErrorOrAux = 1;
64335 }
64336 }else if( pAuxData->xDelete ){
64337 pAuxData->xDelete(pAuxData->pAux);
64338 }
64339
64340 pAuxData->pAux = pAux;
@@ -64555,13 +64999,13 @@
64999 /*
65000 ** Return the value of a status counter for a prepared statement
65001 */
65002 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
65003 Vdbe *pVdbe = (Vdbe*)pStmt;
65004 u32 v = pVdbe->aCounter[op];
65005 if( resetFlag ) pVdbe->aCounter[op] = 0;
65006 return (int)v;
65007 }
65008
65009 /************** End of vdbeapi.c *********************************************/
65010 /************** Begin file vdbetrace.c ***************************************/
65011 /*
@@ -65444,23 +65888,10 @@
65888 assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
65889 return 1;
65890 }
65891 #endif
65892
 
 
 
 
 
 
 
 
 
 
 
 
 
65893
65894 /*
65895 ** Execute as much of a VDBE program as we can then return.
65896 **
65897 ** sqlite3VdbeMakeReady() must be called before this routine in order to
@@ -65964,11 +66395,11 @@
66395 CHECK_FOR_INTERRUPT;
66396 sqlite3VdbeIOTraceSql(p);
66397 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
66398 if( db->xProgress ){
66399 assert( 0 < db->nProgressOps );
66400 nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
66401 if( nProgressLimit==0 ){
66402 nProgressLimit = db->nProgressOps;
66403 }else{
66404 nProgressLimit %= (unsigned)db->nProgressOps;
66405 }
@@ -66027,11 +66458,11 @@
66458 ** value or convert mem[p2] to a different type.
66459 */
66460 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
66461 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
66462 assert( pOp->p2>0 );
66463 assert( pOp->p2<=(p->nMem-p->nCursor) );
66464 pOut = &aMem[pOp->p2];
66465 memAboutToChange(p, pOut);
66466 VdbeMemRelease(pOut);
66467 pOut->flags = MEM_Int;
66468 }
@@ -66038,34 +66469,34 @@
66469
66470 /* Sanity checking on other operands */
66471 #ifdef SQLITE_DEBUG
66472 if( (pOp->opflags & OPFLG_IN1)!=0 ){
66473 assert( pOp->p1>0 );
66474 assert( pOp->p1<=(p->nMem-p->nCursor) );
66475 assert( memIsValid(&aMem[pOp->p1]) );
66476 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
66477 }
66478 if( (pOp->opflags & OPFLG_IN2)!=0 ){
66479 assert( pOp->p2>0 );
66480 assert( pOp->p2<=(p->nMem-p->nCursor) );
66481 assert( memIsValid(&aMem[pOp->p2]) );
66482 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
66483 }
66484 if( (pOp->opflags & OPFLG_IN3)!=0 ){
66485 assert( pOp->p3>0 );
66486 assert( pOp->p3<=(p->nMem-p->nCursor) );
66487 assert( memIsValid(&aMem[pOp->p3]) );
66488 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
66489 }
66490 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
66491 assert( pOp->p2>0 );
66492 assert( pOp->p2<=(p->nMem-p->nCursor) );
66493 memAboutToChange(p, &aMem[pOp->p2]);
66494 }
66495 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
66496 assert( pOp->p3>0 );
66497 assert( pOp->p3<=(p->nMem-p->nCursor) );
66498 memAboutToChange(p, &aMem[pOp->p3]);
66499 }
66500 #endif
66501
66502 switch( pOp->opcode ){
@@ -66154,11 +66585,11 @@
66585 **
66586 ** Write the current address onto register P1
66587 ** and then jump to address P2.
66588 */
66589 case OP_Gosub: { /* jump */
66590 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
66591 pIn1 = &aMem[pOp->p1];
66592 assert( (pIn1->flags & MEM_Dyn)==0 );
66593 memAboutToChange(p, pIn1);
66594 pIn1->flags = MEM_Int;
66595 pIn1->u.i = pc;
@@ -66370,11 +66801,11 @@
66801 #if 0 /* local variables moved into u.ab */
66802 int cnt;
66803 u16 nullFlag;
66804 #endif /* local variables moved into u.ab */
66805 u.ab.cnt = pOp->p3-pOp->p2;
66806 assert( pOp->p3<=(p->nMem-p->nCursor) );
66807 pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
66808 while( u.ab.cnt>0 ){
66809 pOut++;
66810 memAboutToChange(p, pOut);
66811 VdbeMemRelease(pOut);
@@ -66443,12 +66874,12 @@
66874 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
66875
66876 pIn1 = &aMem[u.ad.p1];
66877 pOut = &aMem[u.ad.p2];
66878 while( u.ad.n-- ){
66879 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
66880 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
66881 assert( memIsValid(pIn1) );
66882 memAboutToChange(p, pOut);
66883 u.ad.zMalloc = pOut->zMalloc;
66884 pOut->zMalloc = 0;
66885 sqlite3VdbeMemMove(pOut, pIn1);
@@ -66532,11 +66963,11 @@
66963 Mem *pMem;
66964 int i;
66965 #endif /* local variables moved into u.af */
66966 assert( p->nResColumn==pOp->p2 );
66967 assert( pOp->p1>0 );
66968 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
66969
66970 /* If this statement has violated immediate foreign key constraints, do
66971 ** not return the number of rows modified. And do not RELEASE the statement
66972 ** transaction. It needs to be rolled back. */
66973 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66812,15 +67243,15 @@
67243 #endif /* local variables moved into u.ai */
67244
67245 u.ai.n = pOp->p5;
67246 u.ai.apVal = p->apArg;
67247 assert( u.ai.apVal || u.ai.n==0 );
67248 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
67249 pOut = &aMem[pOp->p3];
67250 memAboutToChange(p, pOut);
67251
67252 assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=(p->nMem-p->nCursor)+1) );
67253 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
67254 u.ai.pArg = &aMem[pOp->p2];
67255 for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
67256 assert( memIsValid(u.ai.pArg) );
67257 u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -66843,11 +67274,11 @@
67274 ** the already allocated buffer instead of allocating a new one.
67275 */
67276 sqlite3VdbeMemMove(&u.ai.ctx.s, pOut);
67277 MemSetTypeFlag(&u.ai.ctx.s, MEM_Null);
67278
67279 u.ai.ctx.fErrorOrAux = 0;
67280 if( u.ai.ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
67281 assert( pOp>aOp );
67282 assert( pOp[-1].p4type==P4_COLLSEQ );
67283 assert( pOp[-1].opcode==OP_CollSeq );
67284 u.ai.ctx.pColl = pOp[-1].p4.pColl;
@@ -66854,15 +67285,10 @@
67285 }
67286 db->lastRowid = lastRowid;
67287 (*u.ai.ctx.pFunc->xFunc)(&u.ai.ctx, u.ai.n, u.ai.apVal); /* IMP: R-24505-23230 */
67288 lastRowid = db->lastRowid;
67289
 
 
 
 
 
67290 if( db->mallocFailed ){
67291 /* Even though a malloc() has failed, the implementation of the
67292 ** user function may have called an sqlite3_result_XXX() function
67293 ** to return a value. The following call releases any resources
67294 ** associated with such a value.
@@ -66870,13 +67296,16 @@
67296 sqlite3VdbeMemRelease(&u.ai.ctx.s);
67297 goto no_mem;
67298 }
67299
67300 /* If the function returned an error, throw an exception */
67301 if( u.ai.ctx.fErrorOrAux ){
67302 if( u.ai.ctx.isError ){
67303 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&u.ai.ctx.s));
67304 rc = u.ai.ctx.isError;
67305 }
67306 sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
67307 }
67308
67309 /* Copy the result of the function into register P3 */
67310 sqlite3VdbeChangeEncoding(&u.ai.ctx.s, encoding);
67311 sqlite3VdbeMemMove(pOut, &u.ai.ctx.s);
@@ -67248,16 +67677,16 @@
67677 }else{
67678 /* SQLITE_NULLEQ is clear and at least one operand is NULL,
67679 ** then the result is always NULL.
67680 ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
67681 */
67682 if( pOp->p5 & SQLITE_JUMPIFNULL ){
67683 pc = pOp->p2-1;
67684 }else if( pOp->p5 & SQLITE_STOREP2 ){
67685 pOut = &aMem[pOp->p2];
67686 MemSetTypeFlag(pOut, MEM_Null);
67687 REGISTER_TRACE(pOp->p2, pOut);
 
 
67688 }
67689 break;
67690 }
67691 }else{
67692 /* Neither operand is NULL. Do a comparison. */
@@ -67354,15 +67783,15 @@
67783 u.al.p2 = pOp->p2;
67784 #if SQLITE_DEBUG
67785 if( aPermute ){
67786 int k, mx = 0;
67787 for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67788 assert( u.al.p1>0 && u.al.p1+mx<=(p->nMem-p->nCursor)+1 );
67789 assert( u.al.p2>0 && u.al.p2+mx<=(p->nMem-p->nCursor)+1 );
67790 }else{
67791 assert( u.al.p1>0 && u.al.p1+u.al.n<=(p->nMem-p->nCursor)+1 );
67792 assert( u.al.p2>0 && u.al.p2+u.al.n<=(p->nMem-p->nCursor)+1 );
67793 }
67794 #endif /* SQLITE_DEBUG */
67795 for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
67796 u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
67797 assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67615,11 +68044,11 @@
68044 u.ao.p1 = pOp->p1;
68045 u.ao.p2 = pOp->p2;
68046 u.ao.pC = 0;
68047 memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
68048 assert( u.ao.p1<p->nCursor );
68049 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68050 u.ao.pDest = &aMem[pOp->p3];
68051 memAboutToChange(p, u.ao.pDest);
68052 u.ao.zRec = 0;
68053
68054 /* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67915,11 +68344,11 @@
68344 u.ap.zAffinity = pOp->p4.z;
68345 assert( u.ap.zAffinity!=0 );
68346 assert( u.ap.zAffinity[pOp->p2]==0 );
68347 pIn1 = &aMem[pOp->p1];
68348 while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
68349 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
68350 assert( memIsValid(pIn1) );
68351 ExpandBlob(pIn1);
68352 applyAffinity(pIn1, u.ap.cAff, encoding);
68353 pIn1++;
68354 }
@@ -67978,11 +68407,11 @@
68407 u.aq.nData = 0; /* Number of bytes of data space */
68408 u.aq.nHdr = 0; /* Number of bytes of header space */
68409 u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
68410 u.aq.nField = pOp->p1;
68411 u.aq.zAffinity = pOp->p4.z;
68412 assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=(p->nMem-p->nCursor)+1 );
68413 u.aq.pData0 = &aMem[u.aq.nField];
68414 u.aq.nField = pOp->p2;
68415 u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
68416 u.aq.file_format = p->minWriteFileFormat;
68417
@@ -68044,11 +68473,11 @@
68473 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
68474 u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
68475 }
68476 assert( u.aq.i==u.aq.nByte );
68477
68478 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68479 pOut->n = (int)u.aq.nByte;
68480 pOut->flags = MEM_Blob | MEM_Dyn;
68481 pOut->xDel = 0;
68482 if( u.aq.nZero ){
68483 pOut->u.nZero = u.aq.nZero;
@@ -68640,11 +69069,11 @@
69069 }else{
69070 u.ay.wrFlag = 0;
69071 }
69072 if( pOp->p5 & OPFLAG_P2ISREG ){
69073 assert( u.ay.p2>0 );
69074 assert( u.ay.p2<=(p->nMem-p->nCursor) );
69075 pIn2 = &aMem[u.ay.p2];
69076 assert( memIsValid(pIn2) );
69077 assert( (pIn2->flags & MEM_Int)!=0 );
69078 sqlite3VdbeMemIntegerify(pIn2);
69079 u.ay.p2 = (int)pIn2->u.i;
@@ -69191,11 +69620,11 @@
69620
69621 pIn3 = &aMem[pOp->p3];
69622 u.bf.aMx = &aMem[pOp->p4.i];
69623 /* Assert that the values of parameters P1 and P4 are in range. */
69624 assert( pOp->p4type==P4_INT32 );
69625 assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
69626 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69627
69628 /* Find the index cursor. */
69629 u.bf.pCx = p->apCsr[pOp->p1];
69630 assert( u.bf.pCx->deferredMoveto==0 );
@@ -69398,11 +69827,11 @@
69827 /* Assert that P3 is a valid memory cell. */
69828 assert( pOp->p3<=u.bh.pFrame->nMem );
69829 u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
69830 }else{
69831 /* Assert that P3 is a valid memory cell. */
69832 assert( pOp->p3<=(p->nMem-p->nCursor) );
69833 u.bh.pMem = &aMem[pOp->p3];
69834 memAboutToChange(p, u.bh.pMem);
69835 }
69836 assert( memIsValid(u.bh.pMem) );
69837
@@ -69808,11 +70237,11 @@
70237 }else if( u.bn.pC->pVtabCursor ){
70238 u.bn.pVtab = u.bn.pC->pVtabCursor->pVtab;
70239 u.bn.pModule = u.bn.pVtab->pModule;
70240 assert( u.bn.pModule->xRowid );
70241 rc = u.bn.pModule->xRowid(u.bn.pC->pVtabCursor, &u.bn.v);
70242 sqlite3VtabImportErrmsg(p, u.bn.pVtab);
70243 #endif /* SQLITE_OMIT_VIRTUALTABLE */
70244 }else{
70245 assert( u.bn.pC->pCursor!=0 );
70246 rc = sqlite3VdbeCursorMoveto(u.bn.pC);
70247 if( rc ) goto abort_due_to_error;
@@ -69900,11 +70329,11 @@
70329 case OP_Sort: { /* jump */
70330 #ifdef SQLITE_TEST
70331 sqlite3_sort_count++;
70332 sqlite3_search_count--;
70333 #endif
70334 p->aCounter[SQLITE_STMTSTATUS_SORT]++;
70335 /* Fall through into OP_Rewind */
70336 }
70337 /* Opcode: Rewind P1 P2 * * *
70338 **
70339 ** The next use of the Rowid or Column or Next instruction for P1
@@ -69983,21 +70412,21 @@
70412 VdbeCursor *pC;
70413 int res;
70414 #endif /* local variables moved into u.br */
70415
70416 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70417 assert( pOp->p5<ArraySize(p->aCounter) );
70418 u.br.pC = p->apCsr[pOp->p1];
70419 if( u.br.pC==0 ){
70420 break; /* See ticket #2273 */
70421 }
70422 assert( u.br.pC->isSorter==(pOp->opcode==OP_SorterNext) );
70423 if( isSorter(u.br.pC) ){
70424 assert( pOp->opcode==OP_SorterNext );
70425 rc = sqlite3VdbeSorterNext(db, u.br.pC, &u.br.res);
70426 }else{
70427 /* u.br.res = 1; // Always initialized by the xAdvance() call */
70428 assert( u.br.pC->deferredMoveto==0 );
70429 assert( u.br.pC->pCursor );
70430 assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
70431 assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
70432 rc = pOp->p4.xAdvance(u.br.pC->pCursor, &u.br.res);
@@ -70004,11 +70433,11 @@
70433 }
70434 u.br.pC->nullRow = (u8)u.br.res;
70435 u.br.pC->cacheStatus = CACHE_STALE;
70436 if( u.br.res==0 ){
70437 pc = pOp->p2 - 1;
70438 p->aCounter[pOp->p5]++;
70439 #ifdef SQLITE_TEST
70440 sqlite3_search_count++;
70441 #endif
70442 }
70443 u.br.pC->rowidIsValid = 0;
@@ -70076,11 +70505,11 @@
70505 int res;
70506 UnpackedRecord r;
70507 #endif /* local variables moved into u.bt */
70508
70509 assert( pOp->p3>0 );
70510 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
70511 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70512 u.bt.pC = p->apCsr[pOp->p1];
70513 assert( u.bt.pC!=0 );
70514 u.bt.pCrsr = u.bt.pC->pCursor;
70515 if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70292,10 +70721,11 @@
70721 int nChange;
70722 #endif /* local variables moved into u.bx */
70723
70724 u.bx.nChange = 0;
70725 assert( p->readOnly==0 );
70726 assert( pOp->p1!=1 );
70727 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
70728 rc = sqlite3BtreeClearTable(
70729 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
70730 );
70731 if( pOp->p3 ){
@@ -70498,11 +70928,11 @@
70928 assert( p->bIsReader );
70929 u.ca.nRoot = pOp->p2;
70930 assert( u.ca.nRoot>0 );
70931 u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
70932 if( u.ca.aRoot==0 ) goto no_mem;
70933 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
70934 u.ca.pnErr = &aMem[pOp->p3];
70935 assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
70936 assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
70937 pIn1 = &aMem[pOp->p1];
70938 for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70934,11 +71364,11 @@
71364 u.cg.apVal[u.cg.i] = u.cg.pRec;
71365 memAboutToChange(p, u.cg.pRec);
71366 sqlite3VdbeMemStoreType(u.cg.pRec);
71367 }
71368 u.cg.ctx.pFunc = pOp->p4.pFunc;
71369 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71370 u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
71371 u.cg.pMem->n++;
71372 u.cg.ctx.s.flags = MEM_Null;
71373 u.cg.ctx.s.z = 0;
71374 u.cg.ctx.s.zMalloc = 0;
@@ -70983,11 +71413,11 @@
71413 */
71414 case OP_AggFinal: {
71415 #if 0 /* local variables moved into u.ch */
71416 Mem *pMem;
71417 #endif /* local variables moved into u.ch */
71418 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
71419 u.ch.pMem = &aMem[pOp->p1];
71420 assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
71421 rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
71422 if( rc ){
71423 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71249,11 +71679,11 @@
71679 #if 0 /* local variables moved into u.cl */
71680 VTable *pVTab;
71681 #endif /* local variables moved into u.cl */
71682 u.cl.pVTab = pOp->p4.pVtab;
71683 rc = sqlite3VtabBegin(db, u.cl.pVTab);
71684 if( u.cl.pVTab ) sqlite3VtabImportErrmsg(p, u.cl.pVTab->pVtab);
71685 break;
71686 }
71687 #endif /* SQLITE_OMIT_VIRTUALTABLE */
71688
71689 #ifndef SQLITE_OMIT_VIRTUALTABLE
@@ -71302,11 +71732,11 @@
71732 u.cm.pVtabCursor = 0;
71733 u.cm.pVtab = pOp->p4.pVtab->pVtab;
71734 u.cm.pModule = (sqlite3_module *)u.cm.pVtab->pModule;
71735 assert(u.cm.pVtab && u.cm.pModule);
71736 rc = u.cm.pModule->xOpen(u.cm.pVtab, &u.cm.pVtabCursor);
71737 sqlite3VtabImportErrmsg(p, u.cm.pVtab);
71738 if( SQLITE_OK==rc ){
71739 /* Initialize sqlite3_vtab_cursor base class */
71740 u.cm.pVtabCursor->pVtab = u.cm.pVtab;
71741
71742 /* Initialize vdbe cursor object */
@@ -71382,11 +71812,11 @@
71812 }
71813
71814 p->inVtabMethod = 1;
71815 rc = u.cn.pModule->xFilter(u.cn.pVtabCursor, u.cn.iQuery, pOp->p4.z, u.cn.nArg, u.cn.apArg);
71816 p->inVtabMethod = 0;
71817 sqlite3VtabImportErrmsg(p, u.cn.pVtab);
71818 if( rc==SQLITE_OK ){
71819 u.cn.res = u.cn.pModule->xEof(u.cn.pVtabCursor);
71820 }
71821
71822 if( u.cn.res ){
@@ -71414,11 +71844,11 @@
71844 sqlite3_context sContext;
71845 #endif /* local variables moved into u.co */
71846
71847 VdbeCursor *pCur = p->apCsr[pOp->p1];
71848 assert( pCur->pVtabCursor );
71849 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71850 u.co.pDest = &aMem[pOp->p3];
71851 memAboutToChange(p, u.co.pDest);
71852 if( pCur->nullRow ){
71853 sqlite3VdbeMemSetNull(u.co.pDest);
71854 break;
@@ -71435,11 +71865,11 @@
71865 */
71866 sqlite3VdbeMemMove(&u.co.sContext.s, u.co.pDest);
71867 MemSetTypeFlag(&u.co.sContext.s, MEM_Null);
71868
71869 rc = u.co.pModule->xColumn(pCur->pVtabCursor, &u.co.sContext, pOp->p2);
71870 sqlite3VtabImportErrmsg(p, u.co.pVtab);
71871 if( u.co.sContext.isError ){
71872 rc = u.co.sContext.isError;
71873 }
71874
71875 /* Copy the result of the function to the P3 register. We
@@ -71490,11 +71920,11 @@
71920 ** some other method is next invoked on the save virtual table cursor.
71921 */
71922 p->inVtabMethod = 1;
71923 rc = u.cp.pModule->xNext(u.cp.pCur->pVtabCursor);
71924 p->inVtabMethod = 0;
71925 sqlite3VtabImportErrmsg(p, u.cp.pVtab);
71926 if( rc==SQLITE_OK ){
71927 u.cp.res = u.cp.pModule->xEof(u.cp.pCur->pVtabCursor);
71928 }
71929
71930 if( !u.cp.res ){
@@ -71529,11 +71959,11 @@
71959 testcase( u.cq.pName->enc==SQLITE_UTF16BE );
71960 testcase( u.cq.pName->enc==SQLITE_UTF16LE );
71961 rc = sqlite3VdbeChangeEncoding(u.cq.pName, SQLITE_UTF8);
71962 if( rc==SQLITE_OK ){
71963 rc = u.cq.pVtab->pModule->xRename(u.cq.pVtab, u.cq.pName->z);
71964 sqlite3VtabImportErrmsg(p, u.cq.pVtab);
71965 p->expired = 0;
71966 }
71967 break;
71968 }
71969 #endif
@@ -71593,11 +72023,11 @@
72023 u.cr.pX++;
72024 }
72025 db->vtabOnConflict = pOp->p5;
72026 rc = u.cr.pModule->xUpdate(u.cr.pVtab, u.cr.nArg, u.cr.apArg, &u.cr.rowid);
72027 db->vtabOnConflict = vtabOnConflict;
72028 sqlite3VtabImportErrmsg(p, u.cr.pVtab);
72029 if( rc==SQLITE_OK && pOp->p1 ){
72030 assert( u.cr.nArg>1 && u.cr.apArg[0] && (u.cr.apArg[0]->flags&MEM_Null) );
72031 db->lastRowid = lastRowid = u.cr.rowid;
72032 }
72033 if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
@@ -71760,11 +72190,12 @@
72190 /* This is the only way out of this procedure. We have to
72191 ** release the mutexes on btrees that were acquired at the
72192 ** top. */
72193 vdbe_return:
72194 db->lastRowid = lastRowid;
72195 testcase( nVmStep>0 );
72196 p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
72197 sqlite3VdbeLeave(p);
72198 return rc;
72199
72200 /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
72201 ** is encountered.
@@ -75520,12 +75951,11 @@
75951 int op = p->op;
75952 if( op==TK_CAST || op==TK_UPLUS ){
75953 p = p->pLeft;
75954 continue;
75955 }
75956 if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
 
75957 pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
75958 break;
75959 }
75960 if( p->pTab!=0
75961 && (op==TK_AGG_COLUMN || op==TK_COLUMN
@@ -76685,10 +77115,11 @@
77115 break;
77116 }
77117 case TK_UMINUS: {
77118 int v;
77119 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
77120 assert( v!=(-2147483647-1) );
77121 *pValue = -v;
77122 rc = 1;
77123 }
77124 break;
77125 }
@@ -78910,10 +79341,11 @@
79341 compLeft.pRight = pExpr->x.pList->a[0].pExpr;
79342 compRight.op = TK_LE;
79343 compRight.pLeft = &exprX;
79344 compRight.pRight = pExpr->x.pList->a[1].pExpr;
79345 exprX.iTable = sqlite3ExprCodeTemp(pParse, &exprX, &regFree1);
79346 exprX.op2 = exprX.op;
79347 exprX.op = TK_REGISTER;
79348 if( jumpIfTrue ){
79349 sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
79350 }else{
79351 sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
@@ -80343,11 +80775,11 @@
80775
80776 /* Ensure the default expression is something that sqlite3ValueFromExpr()
80777 ** can handle (i.e. not CURRENT_TIME etc.)
80778 */
80779 if( pDflt ){
80780 sqlite3_value *pVal = 0;
80781 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
80782 db->mallocFailed = 1;
80783 return;
80784 }
80785 if( !pVal ){
@@ -80484,11 +80916,11 @@
80916 #endif /* SQLITE_ALTER_TABLE */
80917
80918 /************** End of alter.c ***********************************************/
80919 /************** Begin file analyze.c *****************************************/
80920 /*
80921 ** 2005-07-08
80922 **
80923 ** The author disclaims copyright to this source code. In place of
80924 ** a legal notice, here is a blessing:
80925 **
80926 ** May you do good and not evil.
@@ -80505,27 +80937,36 @@
80937 ** The following system tables are or have been supported:
80938 **
80939 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
80940 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
80941 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
80942 ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
80943 **
80944 ** Additional tables might be added in future releases of SQLite.
80945 ** The sqlite_stat2 table is not created or used unless the SQLite version
80946 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
80947 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
80948 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
80949 ** created and used by SQLite versions 3.7.9 and later and with
80950 ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
80951 ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
80952 ** version of sqlite_stat3 and is only available when compiled with
80953 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.0 and later. It is
80954 ** not possible to enable both STAT3 and STAT4 at the same time. If they
80955 ** are both enabled, then STAT4 is precedence.
80956 **
80957 ** For most applications, sqlite_stat1 provides all the statisics required
80958 ** for the query planner to make good choices.
80959 **
80960 ** Format of sqlite_stat1:
80961 **
80962 ** There is normally one row per index, with the index identified by the
80963 ** name in the idx column. The tbl column is the name of the table to
80964 ** which the index belongs. In each such row, the stat column will be
80965 ** a string consisting of a list of integers. The first integer in this
80966 ** list is the number of rows in the index. (This is the same as the
80967 ** number of rows in the table, except for partial indices.) The second
80968 ** integer is the average number of rows in the index that have the same
80969 ** value in the first column of the index. The third integer is the average
80970 ** number of rows in the index that have the same value for the first two
80971 ** columns. The N-th integer (for N>1) is the average number of rows in
80972 ** the index which have the same value for the first N-1 columns. For
@@ -80568,57 +81009,85 @@
81009 ** writes the sqlite_stat2 table. This version of SQLite only supports
81010 ** sqlite_stat3.
81011 **
81012 ** Format for sqlite_stat3:
81013 **
81014 ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
81015 ** sqlite_stat4 format will be described first. Further information
81016 ** about sqlite_stat3 follows the sqlite_stat4 description.
81017 **
81018 ** Format for sqlite_stat4:
81019 **
81020 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
81021 ** to aid the query planner in choosing good indices based on the values
81022 ** that indexed columns are compared against in the WHERE clauses of
81023 ** queries.
81024 **
81025 ** The sqlite_stat4 table contains multiple entries for each index.
81026 ** The idx column names the index and the tbl column is the table of the
81027 ** index. If the idx and tbl columns are the same, then the sample is
81028 ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
81029 ** binary encoding of a key from the index, with the trailing rowid
81030 ** omitted. The nEq column is a list of integers. The first integer
81031 ** is the approximate number of entries in the index whose left-most
81032 ** column exactly matches the left-most column of the sample. The second
81033 ** integer in nEq is the approximate number of entries in the index where
81034 ** the first two columns match the first two columns of the sample.
81035 ** And so forth. nLt is another list of integers that show the approximate
81036 ** number of entries that are strictly less than the sample. The first
81037 ** integer in nLt contains the number of entries in the index where the
81038 ** left-most column is less than the left-most column of the sample.
81039 ** The K-th integer in the nLt entry is the number of index entries
81040 ** where the first K columns are less than the first K columns of the
81041 ** sample. The nDLt column is like nLt except that it contains the
81042 ** number of distinct entries in the index that are less than the
81043 ** sample.
81044 **
81045 ** There can be an arbitrary number of sqlite_stat4 entries per index.
81046 ** The ANALYZE command will typically generate sqlite_stat4 tables
81047 ** that contain between 10 and 40 samples which are distributed across
81048 ** the key space, though not uniformly, and which include samples with
81049 ** large nEq values.
81050 **
81051 ** Format for sqlite_stat3 redux:
81052 **
81053 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
81054 ** looks at the left-most column of the index. The sqlite_stat3.sample
81055 ** column contains the actual value of the left-most column instead
81056 ** of a blob encoding of the complete index key as is found in
81057 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
81058 ** all contain just a single integer which is the same as the first
81059 ** integer in the equivalent columns in sqlite_stat4.
81060 */
81061 #ifndef SQLITE_OMIT_ANALYZE
81062
81063 #if defined(SQLITE_ENABLE_STAT4)
81064 # define IsStat4 1
81065 # define IsStat3 0
81066 #elif defined(SQLITE_ENABLE_STAT3)
81067 # define IsStat4 0
81068 # define IsStat3 1
81069 #else
81070 # define IsStat4 0
81071 # define IsStat3 0
81072 # undef SQLITE_STAT4_SAMPLES
81073 # define SQLITE_STAT4_SAMPLES 1
81074 #endif
81075 #define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
81076
81077 /*
81078 ** This routine generates code that opens the sqlite_statN tables.
81079 ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
81080 ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
81081 ** appropriate compile-time options are provided.
81082 **
81083 ** If the sqlite_statN tables do not previously exist, it is created.
 
 
81084 **
81085 ** Argument zWhere may be a pointer to a buffer containing a table name,
81086 ** or it may be a NULL pointer. If it is not NULL, then all entries in
81087 ** the sqlite_statN tables associated with the named table are deleted.
81088 ** If zWhere==0, then code is generated to delete all stat table entries.
 
81089 */
81090 static void openStatTable(
81091 Parse *pParse, /* Parsing context */
81092 int iDb, /* The database we are looking in */
81093 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80628,22 +81097,28 @@
81097 static const struct {
81098 const char *zName;
81099 const char *zCols;
81100 } aTable[] = {
81101 { "sqlite_stat1", "tbl,idx,stat" },
81102 #if defined(SQLITE_ENABLE_STAT4)
81103 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
81104 { "sqlite_stat3", 0 },
81105 #elif defined(SQLITE_ENABLE_STAT3)
81106 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
81107 { "sqlite_stat4", 0 },
81108 #else
81109 { "sqlite_stat3", 0 },
81110 { "sqlite_stat4", 0 },
81111 #endif
81112 };
 
 
 
 
81113 int i;
81114 sqlite3 *db = pParse->db;
81115 Db *pDb;
81116 Vdbe *v = sqlite3GetVdbe(pParse);
81117 int aRoot[ArraySize(aTable)];
81118 u8 aCreateTbl[ArraySize(aTable)];
81119
81120 if( v==0 ) return;
81121 assert( sqlite3BtreeHoldsAllMutexes(db) );
81122 assert( sqlite3VdbeDb(v)==db );
81123 pDb = &db->aDb[iDb];
81124
@@ -80652,262 +81127,592 @@
81127 */
81128 for(i=0; i<ArraySize(aTable); i++){
81129 const char *zTab = aTable[i].zName;
81130 Table *pStat;
81131 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
81132 if( aTable[i].zCols ){
81133 /* The sqlite_statN table does not exist. Create it. Note that a
81134 ** side-effect of the CREATE TABLE statement is to leave the rootpage
81135 ** of the new table in register pParse->regRoot. This is important
81136 ** because the OpenWrite opcode below will be needing it. */
81137 sqlite3NestedParse(pParse,
81138 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
81139 );
81140 aRoot[i] = pParse->regRoot;
81141 aCreateTbl[i] = OPFLAG_P2ISREG;
81142 }
81143 }else{
81144 /* The table already exists. If zWhere is not NULL, delete all entries
81145 ** associated with the table zWhere. If zWhere is NULL, delete the
81146 ** entire contents of the table. */
81147 aRoot[i] = pStat->tnum;
81148 aCreateTbl[i] = 0;
81149 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
81150 if( zWhere ){
81151 sqlite3NestedParse(pParse,
81152 "DELETE FROM %Q.%s WHERE %s=%Q",
81153 pDb->zName, zTab, zWhereType, zWhere
81154 );
81155 }else{
81156 /* The sqlite_stat[134] table already exists. Delete all rows. */
81157 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
81158 }
81159 }
81160 }
81161
81162 /* Open the sqlite_stat[134] tables for writing. */
81163 for(i=0; aTable[i].zCols; i++){
81164 assert( i<ArraySize(aTable) );
81165 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
81166 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
81167 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
81168 }
81169 }
81170
81171 /*
81172 ** Recommended number of samples for sqlite_stat4
81173 */
81174 #ifndef SQLITE_STAT4_SAMPLES
81175 # define SQLITE_STAT4_SAMPLES 24
81176 #endif
81177
81178 /*
81179 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
81180 ** share an instance of the following structure to hold their state
81181 ** information.
81182 */
81183 typedef struct Stat4Accum Stat4Accum;
81184 typedef struct Stat4Sample Stat4Sample;
81185 struct Stat4Sample {
81186 tRowcnt *anEq; /* sqlite_stat4.nEq */
81187 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
81188 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81189 tRowcnt *anLt; /* sqlite_stat4.nLt */
81190 i64 iRowid; /* Rowid in main table of the key */
81191 u8 isPSample; /* True if a periodic sample */
81192 int iCol; /* If !isPSample, the reason for inclusion */
81193 u32 iHash; /* Tiebreaker hash */
81194 #endif
81195 };
81196 struct Stat4Accum {
81197 tRowcnt nRow; /* Number of rows in the entire table */
81198 tRowcnt nPSample; /* How often to do a periodic sample */
81199 int nCol; /* Number of columns in index + rowid */
81200 int mxSample; /* Maximum number of samples to accumulate */
81201 Stat4Sample current; /* Current row as a Stat4Sample */
81202 u32 iPrn; /* Pseudo-random number used for sampling */
81203 Stat4Sample *aBest; /* Array of (nCol-1) best samples */
81204 int iMin; /* Index in a[] of entry with minimum score */
81205 int nSample; /* Current number of samples */
81206 int iGet; /* Index of current sample accessed by stat_get() */
81207 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
 
 
 
81208 };
81209
 
81210 /*
81211 ** Implementation of the stat_init(N,C) SQL function. The two parameters
81212 ** are the number of rows in the table or index (C) and the number of columns
81213 ** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
81214 **
81215 ** This routine allocates the Stat4Accum object in heap memory. The return
81216 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
81217 ** the size of the blob is sizeof(void*) bytes).
81218 */
81219 static void statInit(
81220 sqlite3_context *context,
81221 int argc,
81222 sqlite3_value **argv
81223 ){
81224 Stat4Accum *p;
81225 int nCol; /* Number of columns in index being sampled */
81226 int nColUp; /* nCol rounded up for alignment */
81227 int n; /* Bytes of space to allocate */
81228 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81229 int mxSample = SQLITE_STAT4_SAMPLES;
81230 #endif
81231
81232 /* Decode the three function arguments */
81233 UNUSED_PARAMETER(argc);
81234 nCol = sqlite3_value_int(argv[0]);
81235 assert( nCol>1 ); /* >1 because it includes the rowid column */
81236 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
81237
81238 /* Allocate the space required for the Stat4Accum object */
81239 n = sizeof(*p)
81240 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
81241 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
81242 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81243 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
81244 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
81245 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
81246 #endif
81247 ;
81248 p = sqlite3MallocZero(n);
81249 if( p==0 ){
81250 sqlite3_result_error_nomem(context);
81251 return;
81252 }
81253
81254 p->nRow = 0;
81255 p->nCol = nCol;
81256 p->current.anDLt = (tRowcnt*)&p[1];
81257 p->current.anEq = &p->current.anDLt[nColUp];
81258
81259 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81260 {
81261 u8 *pSpace; /* Allocated space not yet assigned */
81262 int i; /* Used to iterate through p->aSample[] */
81263
81264 p->iGet = -1;
81265 p->mxSample = mxSample;
81266 p->nPSample = sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1;
81267 p->current.anLt = &p->current.anEq[nColUp];
81268 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81269
81270 /* Set up the Stat4Accum.a[] and aBest[] arrays */
81271 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
81272 p->aBest = &p->a[mxSample];
81273 pSpace = (u8*)(&p->a[mxSample+nCol]);
81274 for(i=0; i<(mxSample+nCol); i++){
81275 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81276 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81277 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81278 }
81279 assert( (pSpace - (u8*)p)==n );
81280
81281 for(i=0; i<nCol; i++){
81282 p->aBest[i].iCol = i;
81283 }
81284 }
81285 #endif
81286
81287 /* Return a pointer to the allocated object to the caller */
81288 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
81289 }
81290 static const FuncDef statInitFuncdef = {
81291 1+IsStat34, /* nArg */
81292 SQLITE_UTF8, /* iPrefEnc */
81293 0, /* flags */
81294 0, /* pUserData */
81295 0, /* pNext */
81296 statInit, /* xFunc */
81297 0, /* xStep */
81298 0, /* xFinalize */
81299 "stat_init", /* zName */
81300 0, /* pHash */
81301 0 /* pDestructor */
81302 };
81303
81304 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81305 /*
81306 ** Return true if pNew is to be preferred over pOld.
81307 */
81308 static int sampleIsBetter(Stat4Sample *pNew, Stat4Sample *pOld){
81309 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
81310 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
81311
81312 assert( pOld->isPSample==0 && pNew->isPSample==0 );
81313 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
81314
81315 if( (nEqNew>nEqOld)
81316 || (nEqNew==nEqOld && pNew->iCol<pOld->iCol)
81317 || (nEqNew==nEqOld && pNew->iCol==pOld->iCol && pNew->iHash>pOld->iHash)
81318 ){
81319 return 1;
81320 }
81321 return 0;
81322 }
81323
81324 /*
81325 ** Copy the contents of object (*pFrom) into (*pTo).
81326 */
81327 void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
81328 pTo->iRowid = pFrom->iRowid;
81329 pTo->isPSample = pFrom->isPSample;
81330 pTo->iCol = pFrom->iCol;
81331 pTo->iHash = pFrom->iHash;
81332 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
81333 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
81334 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
81335 }
81336
81337 /*
81338 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
81339 ** remove the least desirable sample from p->a[] to make room.
81340 */
81341 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
81342 Stat4Sample *pSample;
81343 int i;
81344 i64 iSeq;
81345 i64 iPos;
81346
81347 assert( IsStat4 || nEqZero==0 );
81348
81349 if( pNew->isPSample==0 ){
81350 Stat4Sample *pUpgrade = 0;
81351 assert( pNew->anEq[pNew->iCol]>0 );
81352
81353 /* This sample is being added because the prefix that ends in column
81354 ** iCol occurs many times in the table. However, if we have already
81355 ** added a sample that shares this prefix, there is no need to add
81356 ** this one. Instead, upgrade the priority of the highest priority
81357 ** existing sample that shares this prefix. */
81358 for(i=p->nSample-1; i>=0; i--){
81359 Stat4Sample *pOld = &p->a[i];
81360 if( pOld->anEq[pNew->iCol]==0 ){
81361 if( pOld->isPSample ) return;
81362 assert( sampleIsBetter(pNew, pOld) );
81363 if( pUpgrade==0 || sampleIsBetter(pOld, pUpgrade) ){
81364 pUpgrade = pOld;
81365 }
81366 }
81367 }
81368 if( pUpgrade ){
81369 pUpgrade->iCol = pNew->iCol;
81370 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
81371 goto find_new_min;
81372 }
81373 }
81374
81375 /* If necessary, remove sample iMin to make room for the new sample. */
81376 if( p->nSample>=p->mxSample ){
81377 Stat4Sample *pMin = &p->a[p->iMin];
81378 tRowcnt *anEq = pMin->anEq;
81379 tRowcnt *anLt = pMin->anLt;
81380 tRowcnt *anDLt = pMin->anDLt;
81381 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
81382 pSample = &p->a[p->nSample-1];
81383 pSample->anEq = anEq;
81384 pSample->anDLt = anDLt;
81385 pSample->anLt = anLt;
81386 p->nSample = p->mxSample-1;
81387 }
81388
81389 /* Figure out where in the a[] array the new sample should be inserted. */
81390 iSeq = pNew->anLt[p->nCol-1];
81391 for(iPos=p->nSample; iPos>0; iPos--){
81392 if( iSeq>p->a[iPos-1].anLt[p->nCol-1] ) break;
81393 }
81394
81395 /* Insert the new sample */
81396 pSample = &p->a[iPos];
81397 if( iPos!=p->nSample ){
81398 Stat4Sample *pEnd = &p->a[p->nSample];
81399 tRowcnt *anEq = pEnd->anEq;
81400 tRowcnt *anLt = pEnd->anLt;
81401 tRowcnt *anDLt = pEnd->anDLt;
81402 memmove(&p->a[iPos], &p->a[iPos+1], (p->nSample-iPos)*sizeof(p->a[0]));
81403 pSample->anEq = anEq;
81404 pSample->anDLt = anDLt;
81405 pSample->anLt = anLt;
81406 }
81407 p->nSample++;
81408 sampleCopy(p, pSample, pNew);
81409
81410 /* Zero the first nEqZero entries in the anEq[] array. */
81411 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
81412
81413 find_new_min:
81414 if( p->nSample>=p->mxSample ){
81415 int iMin = -1;
81416 for(i=0; i<p->mxSample; i++){
81417 if( p->a[i].isPSample ) continue;
81418 if( iMin<0 || sampleIsBetter(&p->a[iMin], &p->a[i]) ){
81419 iMin = i;
81420 }
81421 }
81422 assert( iMin>=0 );
81423 p->iMin = iMin;
81424 }
81425 }
81426 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81427
81428 /*
81429 ** Field iChng of the index being scanned has changed. So at this point
81430 ** p->current contains a sample that reflects the previous row of the
81431 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
81432 ** correct at this point.
81433 */
81434 static void samplePushPrevious(Stat4Accum *p, int iChng){
81435 #ifdef SQLITE_ENABLE_STAT4
81436 int i;
81437
81438 /* Check if any samples from the aBest[] array should be pushed
81439 ** into IndexSample.a[] at this point. */
81440 for(i=(p->nCol-2); i>=iChng; i--){
81441 Stat4Sample *pBest = &p->aBest[i];
81442 if( p->nSample<p->mxSample
81443 || sampleIsBetter(pBest, &p->a[p->iMin])
81444 ){
81445 sampleInsert(p, pBest, i);
81446 }
81447 }
81448
81449 /* Update the anEq[] fields of any samples already collected. */
81450 for(i=p->nSample-1; i>=0; i--){
81451 int j;
81452 for(j=iChng; j<p->nCol; j++){
81453 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
81454 }
81455 }
81456 #endif
81457
81458 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
81459 if( iChng==0 ){
81460 tRowcnt nLt = p->current.anLt[0];
81461 tRowcnt nEq = p->current.anEq[0];
81462
81463 /* Check if this is to be a periodic sample. If so, add it. */
81464 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
81465 p->current.isPSample = 1;
81466 sampleInsert(p, &p->current, 0);
81467 p->current.isPSample = 0;
81468 }else
81469
81470 /* Or if it is a non-periodic sample. Add it in this case too. */
81471 if( p->nSample<p->mxSample || sampleIsBetter(&p->current, &p->a[p->iMin]) ){
81472 sampleInsert(p, &p->current, 0);
81473 }
81474 }
81475 #endif
81476 }
81477
81478 /*
81479 ** Implementation of the stat_push SQL function: stat_push(P,R,C)
81480 ** Arguments:
81481 **
81482 ** P Pointer to the Stat4Accum object created by stat_init()
81483 ** C Index of left-most column to differ from previous row
81484 ** R Rowid for the current row
81485 **
81486 ** The SQL function always returns NULL.
81487 **
81488 ** The R parameter is only used for STAT3 and STAT4.
81489 */
81490 static void statPush(
81491 sqlite3_context *context,
81492 int argc,
81493 sqlite3_value **argv
81494 ){
81495 int i;
81496
81497 /* The three function arguments */
81498 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81499 int iChng = sqlite3_value_int(argv[1]);
81500
81501 assert( p->nCol>1 ); /* Includes rowid field */
81502 assert( iChng<p->nCol );
81503
81504 if( p->nRow==0 ){
81505 /* anEq[0] is only zero for the very first call to this function. Do
81506 ** appropriate initialization */
81507 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
81508 }else{
81509 /* Second and subsequent calls get processed here */
81510 samplePushPrevious(p, iChng);
81511
81512 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
81513 ** to the current row of the index. */
81514 for(i=0; i<iChng; i++){
81515 p->current.anEq[i]++;
81516 }
81517 for(i=iChng; i<p->nCol; i++){
81518 p->current.anDLt[i]++;
81519 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81520 p->current.anLt[i] += p->current.anEq[i];
81521 #endif
81522 p->current.anEq[i] = 1;
81523 }
81524 }
81525 p->nRow++;
81526 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81527 p->current.iRowid = sqlite3_value_int64(argv[2]);
81528 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
81529 #endif
81530
81531 #ifdef SQLITE_ENABLE_STAT4
81532 {
81533 tRowcnt nLt = p->current.anLt[p->nCol-1];
81534
81535 /* Check if this is to be a periodic sample. If so, add it. */
81536 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
81537 p->current.isPSample = 1;
81538 p->current.iCol = 0;
81539 sampleInsert(p, &p->current, p->nCol-1);
81540 p->current.isPSample = 0;
81541 }
81542
81543 /* Update the aBest[] array. */
81544 for(i=0; i<(p->nCol-1); i++){
81545 p->current.iCol = i;
81546 if( i>=iChng || sampleIsBetter(&p->current, &p->aBest[i]) ){
81547 sampleCopy(p, &p->aBest[i], &p->current);
81548 }
81549 }
81550 }
81551 #endif
81552 }
81553 static const FuncDef statPushFuncdef = {
81554 2+IsStat34, /* nArg */
81555 SQLITE_UTF8, /* iPrefEnc */
81556 0, /* flags */
81557 0, /* pUserData */
81558 0, /* pNext */
81559 statPush, /* xFunc */
81560 0, /* xStep */
81561 0, /* xFinalize */
81562 "stat_push", /* zName */
81563 0, /* pHash */
81564 0 /* pDestructor */
81565 };
81566
81567 #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
81568 #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
81569 #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
81570 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
81571 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
81572
81573 /*
81574 ** Implementation of the stat_get(P,J) SQL function. This routine is
81575 ** used to query the results. Content is returned for parameter J
81576 ** which is one of the STAT_GET_xxxx values defined above.
81577 **
81578 ** If neither STAT3 nor STAT4 are enabled, then J is always
81579 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
81580 ** a one-parameter function, stat_get(P), that always returns the
81581 ** stat1 table entry information.
81582 */
81583 static void statGet(
81584 sqlite3_context *context,
81585 int argc,
81586 sqlite3_value **argv
81587 ){
81588 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81589 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81590 /* STAT3 and STAT4 have a parameter on this routine. */
81591 int eCall = sqlite3_value_int(argv[1]);
81592 assert( argc==2 );
81593 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
81594 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
81595 || eCall==STAT_GET_NDLT
81596 );
81597 if( eCall==STAT_GET_STAT1 )
81598 #else
81599 assert( argc==1 );
81600 #endif
81601 {
81602 /* Return the value to store in the "stat" column of the sqlite_stat1
81603 ** table for this index.
81604 **
81605 ** The value is a string composed of a list of integers describing
81606 ** the index. The first integer in the list is the total number of
81607 ** entries in the index. There is one additional integer in the list
81608 ** for each indexed column. This additional integer is an estimate of
81609 ** the number of rows matched by a stabbing query on the index using
81610 ** a key with the corresponding number of fields. In other words,
81611 ** if the index is on columns (a,b) and the sqlite_stat1 value is
81612 ** "100 10 2", then SQLite estimates that:
81613 **
81614 ** * the index contains 100 rows,
81615 ** * "WHERE a=?" matches 10 rows, and
81616 ** * "WHERE a=? AND b=?" matches 2 rows.
81617 **
81618 ** If D is the count of distinct values and K is the total number of
81619 ** rows, then each estimate is computed as:
81620 **
81621 ** I = (K+D-1)/D
81622 */
81623 char *z;
81624 int i;
81625
81626 char *zRet = sqlite3MallocZero(p->nCol * 25);
81627 if( zRet==0 ){
81628 sqlite3_result_error_nomem(context);
81629 return;
81630 }
81631
81632 sqlite3_snprintf(24, zRet, "%lld", p->nRow);
81633 z = zRet + sqlite3Strlen30(zRet);
81634 for(i=0; i<(p->nCol-1); i++){
81635 i64 nDistinct = p->current.anDLt[i] + 1;
81636 i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81637 sqlite3_snprintf(24, z, " %lld", iVal);
81638 z += sqlite3Strlen30(z);
81639 assert( p->current.anEq[i] );
81640 }
81641 assert( z[0]=='\0' && z>zRet );
81642
81643 sqlite3_result_text(context, zRet, -1, sqlite3_free);
81644 }
81645 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81646 else if( eCall==STAT_GET_ROWID ){
81647 if( p->iGet<0 ){
81648 samplePushPrevious(p, 0);
81649 p->iGet = 0;
81650 }
81651 if( p->iGet<p->nSample ){
81652 sqlite3_result_int64(context, p->a[p->iGet].iRowid);
81653 }
81654 }else{
81655 tRowcnt *aCnt = 0;
81656
81657 assert( p->iGet<p->nSample );
81658 switch( eCall ){
81659 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
81660 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
81661 default: {
81662 aCnt = p->a[p->iGet].anDLt;
81663 p->iGet++;
81664 break;
81665 }
81666 }
81667
81668 if( IsStat3 ){
81669 sqlite3_result_int64(context, (i64)aCnt[0]);
81670 }else{
81671 char *zRet = sqlite3MallocZero(p->nCol * 25);
81672 if( zRet==0 ){
81673 sqlite3_result_error_nomem(context);
81674 }else{
81675 int i;
81676 char *z = zRet;
81677 for(i=0; i<p->nCol; i++){
81678 sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
81679 z += sqlite3Strlen30(z);
81680 }
81681 assert( z[0]=='\0' && z>zRet );
81682 z[-1] = '\0';
81683 sqlite3_result_text(context, zRet, -1, sqlite3_free);
81684 }
81685 }
81686 }
81687 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81688 }
81689 static const FuncDef statGetFuncdef = {
81690 1+IsStat34, /* nArg */
81691 SQLITE_UTF8, /* iPrefEnc */
81692 0, /* flags */
81693 0, /* pUserData */
81694 0, /* pNext */
81695 statGet, /* xFunc */
81696 0, /* xStep */
81697 0, /* xFinalize */
81698 "stat_get", /* zName */
81699 0, /* pHash */
81700 0 /* pDestructor */
81701 };
81702
81703 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
81704 assert( regOut!=regStat4 && regOut!=regStat4+1 );
81705 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81706 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
81707 #else
81708 assert( iParam==STAT_GET_STAT1 );
81709 #endif
81710 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
81711 sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
81712 sqlite3VdbeChangeP5(v, 1 + IsStat34);
81713 }
81714
81715 /*
81716 ** Generate code to do an analysis of all indices associated with
81717 ** a single table.
81718 */
@@ -80914,46 +81719,35 @@
81719 static void analyzeOneTable(
81720 Parse *pParse, /* Parser context */
81721 Table *pTab, /* Table whose indices are to be analyzed */
81722 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
81723 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
81724 int iMem, /* Available memory locations begin here */
81725 int iTab /* Next available cursor */
81726 ){
81727 sqlite3 *db = pParse->db; /* Database handle */
81728 Index *pIdx; /* An index to being analyzed */
81729 int iIdxCur; /* Cursor open on index being analyzed */
81730 int iTabCur; /* Table cursor */
81731 Vdbe *v; /* The virtual machine being built up */
81732 int i; /* Loop counter */
 
 
81733 int jZeroRows = -1; /* Jump from here if number of rows is zero */
81734 int iDb; /* Index of database containing pTab */
81735 u8 needTableCnt = 1; /* True to count the table */
81736 int regNewRowid = iMem++; /* Rowid for the inserted record */
81737 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
81738 int regChng = iMem++; /* Index of changed index field */
81739 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81740 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
81741 #endif
81742 int regTemp = iMem++; /* Temporary use register */
81743 int regTabname = iMem++; /* Register containing table name */
81744 int regIdxname = iMem++; /* Register containing index name */
81745 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
81746 int regPrev = iMem; /* MUST BE LAST (see below) */
81747
81748 pParse->nMem = MAX(pParse->nMem, iMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81749 v = sqlite3GetVdbe(pParse);
81750 if( v==0 || NEVER(pTab==0) ){
81751 return;
81752 }
81753 if( pTab->tnum==0 ){
@@ -80973,217 +81767,230 @@
81767 db->aDb[iDb].zName ) ){
81768 return;
81769 }
81770 #endif
81771
81772 /* Establish a read-lock on the table at the shared-cache level.
81773 ** Open a read-only cursor on the table. Also allocate a cursor number
81774 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
81775 ** this time though. */
81776 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
81777 iTabCur = iTab++;
81778 iIdxCur = iTab++;
81779 pParse->nTab = MAX(pParse->nTab, iTab);
81780 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81781 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
81782
81783 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81784 int nCol; /* Number of columns indexed by pIdx */
81785 KeyInfo *pKey; /* KeyInfo structure for pIdx */
81786 int *aGotoChng; /* Array of jump instruction addresses */
81787 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
81788 int addrGotoChng0; /* Address of "Goto addr_chng_0" */
81789 int addrNextRow; /* Address of "next_row:" */
81790
81791 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
81792 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
81793 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
81794 nCol = pIdx->nColumn;
81795 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
81796 if( aGotoChng==0 ) continue;
81797 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 
 
 
 
 
 
 
 
 
81798
81799 /* Populate the register containing the index name. */
81800 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
81801
81802 /*
81803 ** Pseudo-code for loop that calls stat_push():
81804 **
81805 ** Rewind csr
81806 ** if eof(csr) goto end_of_scan;
81807 ** regChng = 0
81808 ** goto chng_addr_0;
81809 **
81810 ** next_row:
81811 ** regChng = 0
81812 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81813 ** regChng = 1
81814 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81815 ** ...
81816 ** regChng = N
81817 ** goto chng_addr_N
81818 **
81819 ** chng_addr_0:
81820 ** regPrev(0) = idx(0)
81821 ** chng_addr_1:
81822 ** regPrev(1) = idx(1)
81823 ** ...
81824 **
81825 ** chng_addr_N:
81826 ** regRowid = idx(rowid)
81827 ** stat_push(P, regChng, regRowid)
81828 ** Next csr
81829 ** if !eof(csr) goto next_row;
81830 **
81831 ** end_of_scan:
81832 */
81833
81834 /* Make sure there are enough memory cells allocated to accommodate
81835 ** the regPrev array and a trailing rowid (the rowid slot is required
81836 ** when building a record to insert into the sample column of
81837 ** the sqlite_stat4 table. */
81838 pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
81839
81840 /* Open a read-only cursor on the index being analyzed. */
81841 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81842 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
81843 sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
81844 VdbeComment((v, "%s", pIdx->zName));
81845
81846 /* Invoke the stat_init() function. The arguments are:
81847 **
81848 ** (1) the number of columns in the index including the rowid,
81849 ** (2) the number of rows in the index,
81850 **
81851 ** The second argument is only used for STAT3 and STAT4
81852 */
81853 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81854 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
81855 #endif
81856 sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
81857 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
81858 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
81859 sqlite3VdbeChangeP5(v, 1+IsStat34);
81860
81861 /* Implementation of the following:
81862 **
81863 ** Rewind csr
81864 ** if eof(csr) goto end_of_scan;
81865 ** regChng = 0
81866 ** goto next_push_0;
81867 **
81868 */
81869 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
81870 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
81871 addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
81872
81873 /*
81874 ** next_row:
81875 ** regChng = 0
81876 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81877 ** regChng = 1
81878 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81879 ** ...
81880 ** regChng = N
81881 ** goto chng_addr_N
81882 */
81883 addrNextRow = sqlite3VdbeCurrentAddr(v);
81884 for(i=0; i<nCol; i++){
81885 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81886 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
81887 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
81888 aGotoChng[i] =
81889 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
81890 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
81891 }
81892 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
81893 aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
81894
81895 /*
81896 ** chng_addr_0:
81897 ** regPrev(0) = idx(0)
81898 ** chng_addr_1:
81899 ** regPrev(1) = idx(1)
81900 ** ...
81901 */
81902 sqlite3VdbeJumpHere(v, addrGotoChng0);
81903 for(i=0; i<nCol; i++){
81904 sqlite3VdbeJumpHere(v, aGotoChng[i]);
81905 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
81906 }
81907
81908 /*
81909 ** chng_addr_N:
81910 ** regRowid = idx(rowid) // STAT34 only
81911 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
81912 ** Next csr
81913 ** if !eof(csr) goto next_row;
81914 */
81915 sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
81916 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81917 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
81918 assert( regRowid==(regStat4+2) );
81919 #endif
81920 assert( regChng==(regStat4+1) );
81921 sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
81922 sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
81923 sqlite3VdbeChangeP5(v, 2+IsStat34);
81924 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
81925
81926 /* Add the entry to the stat1 table. */
81927 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
81928 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
81929 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81930 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
81931 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81932
81933 /* Add the entries to the stat3 or stat4 table. */
81934 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81935 {
81936 int regEq = regStat1;
81937 int regLt = regStat1+1;
81938 int regDLt = regStat1+2;
81939 int regSample = regStat1+3;
81940 int regCol = regStat1+4;
81941 int regSampleRowid = regCol + nCol;
81942 int addrNext;
81943 int addrIsNull;
81944
81945 pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
81946
81947 addrNext = sqlite3VdbeCurrentAddr(v);
81948 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
81949 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
81950 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
81951 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
81952 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
81953 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
81954 #ifdef SQLITE_ENABLE_STAT3
81955 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
81956 pIdx->aiColumn[0], regSample);
81957 #else
81958 for(i=0; i<nCol; i++){
81959 int iCol = pIdx->aiColumn[i];
81960 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
81961 }
81962 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
81963 #endif
81964 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
81965 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81966 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
81967 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
81968 sqlite3VdbeJumpHere(v, addrIsNull);
81969 }
81970 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81971
81972 /* End of analysis */
81973 sqlite3VdbeJumpHere(v, addrRewind);
81974 sqlite3DbFree(db, aGotoChng);
81975 }
81976
 
 
 
 
 
 
 
 
 
 
 
 
 
81977
81978 /* Create a single sqlite_stat1 entry containing NULL as the index
81979 ** name and the row count as the content.
81980 */
81981 if( pOnlyIdx==0 && needTableCnt ){
 
81982 VdbeComment((v, "%s", pTab->zName));
81983 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
 
81984 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
81985 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
81986 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
81987 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81988 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
81989 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81990 sqlite3VdbeJumpHere(v, jZeroRows);
81991 }
 
81992 }
81993
81994
81995 /*
81996 ** Generate code that will cause the most recent index analysis to
@@ -81203,20 +82010,22 @@
82010 sqlite3 *db = pParse->db;
82011 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
82012 HashElem *k;
82013 int iStatCur;
82014 int iMem;
82015 int iTab;
82016
82017 sqlite3BeginWriteOperation(pParse, 0, iDb);
82018 iStatCur = pParse->nTab;
82019 pParse->nTab += 3;
82020 openStatTable(pParse, iDb, iStatCur, 0, 0);
82021 iMem = pParse->nMem+1;
82022 iTab = pParse->nTab;
82023 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82024 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
82025 Table *pTab = (Table*)sqliteHashData(k);
82026 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
82027 }
82028 loadAnalysis(pParse, iDb);
82029 }
82030
82031 /*
@@ -81237,11 +82046,11 @@
82046 if( pOnlyIdx ){
82047 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
82048 }else{
82049 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
82050 }
82051 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
82052 loadAnalysis(pParse, iDb);
82053 }
82054
82055 /*
82056 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -81319,10 +82128,47 @@
82128 typedef struct analysisInfo analysisInfo;
82129 struct analysisInfo {
82130 sqlite3 *db;
82131 const char *zDatabase;
82132 };
82133
82134 /*
82135 ** The first argument points to a nul-terminated string containing a
82136 ** list of space separated integers. Read the first nOut of these into
82137 ** the array aOut[].
82138 */
82139 static void decodeIntArray(
82140 char *zIntArray,
82141 int nOut,
82142 tRowcnt *aOut,
82143 int *pbUnordered
82144 ){
82145 char *z = zIntArray;
82146 int c;
82147 int i;
82148 tRowcnt v;
82149
82150 assert( pbUnordered==0 || *pbUnordered==0 );
82151
82152 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82153 if( z==0 ) z = "";
82154 #else
82155 if( NEVER(z==0) ) z = "";
82156 #endif
82157 for(i=0; *z && i<nOut; i++){
82158 v = 0;
82159 while( (c=z[0])>='0' && c<='9' ){
82160 v = v*10 + c - '0';
82161 z++;
82162 }
82163 aOut[i] = v;
82164 if( *z==' ' ) z++;
82165 }
82166 if( pbUnordered && strcmp(z, "unordered")==0 ){
82167 *pbUnordered = 1;
82168 }
82169 }
82170
82171 /*
82172 ** This callback is invoked once for each index when reading the
82173 ** sqlite_stat1 table.
82174 **
@@ -81335,12 +82181,10 @@
82181 */
82182 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
82183 analysisInfo *pInfo = (analysisInfo*)pData;
82184 Index *pIndex;
82185 Table *pTable;
 
 
82186 const char *z;
82187
82188 assert( argc==3 );
82189 UNUSED_PARAMETER2(NotUsed, argc);
82190
@@ -81354,45 +82198,35 @@
82198 if( argv[1] ){
82199 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
82200 }else{
82201 pIndex = 0;
82202 }
 
82203 z = argv[2];
82204
82205 if( pIndex ){
82206 int bUnordered = 0;
82207 decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82208 if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82209 pIndex->bUnordered = bUnordered;
82210 }else{
82211 decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
82212 }
82213
 
 
 
 
 
 
 
82214 return 0;
82215 }
82216
82217 /*
82218 ** If the Index.aSample variable is not NULL, delete the aSample[] array
82219 ** and its contents.
82220 */
82221 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
82222 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82223 if( pIdx->aSample ){
82224 int j;
82225 for(j=0; j<pIdx->nSample; j++){
82226 IndexSample *p = &pIdx->aSample[j];
82227 sqlite3DbFree(db, p->p);
 
 
82228 }
82229 sqlite3DbFree(db, pIdx->aSample);
82230 }
82231 if( db && db->pnBytesFreed==0 ){
82232 pIdx->nSample = 0;
@@ -81399,155 +82233,222 @@
82233 pIdx->aSample = 0;
82234 }
82235 #else
82236 UNUSED_PARAMETER(db);
82237 UNUSED_PARAMETER(pIdx);
82238 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82239 }
82240
82241 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82242 /*
82243 ** Populate the pIdx->aAvgEq[] array based on the samples currently
82244 ** stored in pIdx->aSample[].
82245 */
82246 static void initAvgEq(Index *pIdx){
82247 if( pIdx ){
82248 IndexSample *aSample = pIdx->aSample;
82249 IndexSample *pFinal = &aSample[pIdx->nSample-1];
82250 int iCol;
82251 for(iCol=0; iCol<pIdx->nColumn; iCol++){
82252 int i; /* Used to iterate through samples */
82253 tRowcnt sumEq = 0; /* Sum of the nEq values */
82254 int nSum = 0; /* Number of terms contributing to sumEq */
82255 tRowcnt avgEq = 0;
82256 tRowcnt nDLt = pFinal->anDLt[iCol];
82257
82258 /* Set nSum to the number of distinct (iCol+1) field prefixes that
82259 ** occur in the stat4 table for this index before pFinal. Set
82260 ** sumEq to the sum of the nEq values for column iCol for the same
82261 ** set (adding the value only once where there exist dupicate
82262 ** prefixes). */
82263 for(i=0; i<(pIdx->nSample-1); i++){
82264 if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
82265 sumEq += aSample[i].anEq[iCol];
82266 nSum++;
82267 }
82268 }
82269 if( nDLt>nSum ){
82270 avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
82271 }
82272 if( avgEq==0 ) avgEq = 1;
82273 pIdx->aAvgEq[iCol] = avgEq;
82274 if( pIdx->nSampleCol==1 ) break;
82275 }
82276 }
82277 }
82278
82279 /*
82280 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
82281 ** into the relevant Index.aSample[] arrays.
82282 **
82283 ** Arguments zSql1 and zSql2 must point to SQL statements that return
82284 ** data equivalent to the following (statements are different for stat3,
82285 ** see the caller of this function for details):
82286 **
82287 ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
82288 ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
82289 **
82290 ** where %Q is replaced with the database name before the SQL is executed.
82291 */
82292 static int loadStatTbl(
82293 sqlite3 *db, /* Database handle */
82294 int bStat3, /* Assume single column records only */
82295 const char *zSql1, /* SQL statement 1 (see above) */
82296 const char *zSql2, /* SQL statement 2 (see above) */
82297 const char *zDb /* Database name (e.g. "main") */
82298 ){
82299 int rc; /* Result codes from subroutines */
82300 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
82301 char *zSql; /* Text of the SQL statement */
82302 Index *pPrevIdx = 0; /* Previous index in the loop */
 
 
82303 IndexSample *pSample; /* A slot in pIdx->aSample[] */
82304
82305 assert( db->lookaside.bEnabled==0 );
82306 zSql = sqlite3MPrintf(db, zSql1, zDb);
 
 
 
 
 
 
82307 if( !zSql ){
82308 return SQLITE_NOMEM;
82309 }
82310 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
82311 sqlite3DbFree(db, zSql);
82312 if( rc ) return rc;
82313
82314 while( sqlite3_step(pStmt)==SQLITE_ROW ){
82315 int nIdxCol = 1; /* Number of columns in stat4 records */
82316 int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
82317
82318 char *zIndex; /* Index name */
82319 Index *pIdx; /* Pointer to the index object */
82320 int nSample; /* Number of samples */
82321 int nByte; /* Bytes of space required */
82322 int i; /* Bytes of space required */
82323 tRowcnt *pSpace;
82324
82325 zIndex = (char *)sqlite3_column_text(pStmt, 0);
82326 if( zIndex==0 ) continue;
82327 nSample = sqlite3_column_int(pStmt, 1);
82328 pIdx = sqlite3FindIndex(db, zIndex, zDb);
82329 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
82330 /* Index.nSample is non-zero at this point if data has already been
82331 ** loaded from the stat4 table. In this case ignore stat3 data. */
82332 if( pIdx==0 || pIdx->nSample ) continue;
82333 if( bStat3==0 ){
82334 nIdxCol = pIdx->nColumn+1;
82335 nAvgCol = pIdx->nColumn;
82336 }
82337 pIdx->nSampleCol = nIdxCol;
82338 nByte = sizeof(IndexSample) * nSample;
82339 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
82340 nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
82341
82342 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
82343 if( pIdx->aSample==0 ){
 
82344 sqlite3_finalize(pStmt);
82345 return SQLITE_NOMEM;
82346 }
82347 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
82348 pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
82349 for(i=0; i<nSample; i++){
82350 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
82351 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
82352 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
82353 }
82354 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
82355 }
82356 rc = sqlite3_finalize(pStmt);
82357 if( rc ) return rc;
82358
82359 zSql = sqlite3MPrintf(db, zSql2, zDb);
 
82360 if( !zSql ){
82361 return SQLITE_NOMEM;
82362 }
82363 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
82364 sqlite3DbFree(db, zSql);
82365 if( rc ) return rc;
82366
82367 while( sqlite3_step(pStmt)==SQLITE_ROW ){
82368 char *zIndex; /* Index name */
82369 Index *pIdx; /* Pointer to the index object */
82370 int nCol = 1; /* Number of columns in index */
 
82371
82372 zIndex = (char *)sqlite3_column_text(pStmt, 0);
82373 if( zIndex==0 ) continue;
82374 pIdx = sqlite3FindIndex(db, zIndex, zDb);
82375 if( pIdx==0 ) continue;
82376 /* This next condition is true if data has already been loaded from
82377 ** the sqlite_stat4 table. In this case ignore stat3 data. */
82378 nCol = pIdx->nSampleCol;
82379 if( bStat3 && nCol>1 ) continue;
82380 if( pIdx!=pPrevIdx ){
82381 initAvgEq(pPrevIdx);
82382 pPrevIdx = pIdx;
82383 }
82384 pSample = &pIdx->aSample[pIdx->nSample];
82385 decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
82386 decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
82387 decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
82388
82389 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
82390 ** This is in case the sample record is corrupted. In that case, the
82391 ** sqlite3VdbeRecordCompare() may read up to two varints past the
82392 ** end of the allocated buffer before it realizes it is dealing with
82393 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
82394 ** a buffer overread. */
82395 pSample->n = sqlite3_column_bytes(pStmt, 4);
82396 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
82397 if( pSample->p==0 ){
82398 sqlite3_finalize(pStmt);
82399 return SQLITE_NOMEM;
82400 }
82401 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
82402 pIdx->nSample++;
82403 }
82404 rc = sqlite3_finalize(pStmt);
82405 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
82406 return rc;
82407 }
82408
82409 /*
82410 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
82411 ** the Index.aSample[] arrays of all indices.
82412 */
82413 static int loadStat4(sqlite3 *db, const char *zDb){
82414 int rc = SQLITE_OK; /* Result codes from subroutines */
82415
82416 assert( db->lookaside.bEnabled==0 );
82417 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
82418 rc = loadStatTbl(db, 0,
82419 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
82420 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
82421 zDb
82422 );
82423 }
82424
82425 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
82426 rc = loadStatTbl(db, 1,
82427 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
82428 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
82429 zDb
82430 );
82431 }
82432
82433 return rc;
82434 }
82435 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82436
82437 /*
82438 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
82439 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
82440 ** arrays. The contents of sqlite_stat3/4 are used to populate the
82441 ** Index.aSample[] arrays.
82442 **
82443 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
82444 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
82445 ** during compilation and the sqlite_stat3/4 table is present, no data is
82446 ** read from it.
82447 **
82448 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
82449 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
82450 ** returned. However, in this case, data is read from the sqlite_stat1
82451 ** table (if it is present) before returning.
82452 **
82453 ** If an OOM error occurs, this function always sets db->mallocFailed.
82454 ** This means if the caller does not care about other errors, the return
@@ -81565,11 +82466,11 @@
82466 /* Clear any prior statistics */
82467 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82468 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
82469 Index *pIdx = sqliteHashData(i);
82470 sqlite3DefaultRowEst(pIdx);
82471 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82472 sqlite3DeleteIndexSamples(db, pIdx);
82473 pIdx->aSample = 0;
82474 #endif
82475 }
82476
@@ -81589,16 +82490,16 @@
82490 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
82491 sqlite3DbFree(db, zSql);
82492 }
82493
82494
82495 /* Load the statistics from the sqlite_stat4 table. */
82496 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82497 if( rc==SQLITE_OK ){
82498 int lookasideEnabled = db->lookaside.bEnabled;
82499 db->lookaside.bEnabled = 0;
82500 rc = loadStat4(db, sInfo.zDatabase);
82501 db->lookaside.bEnabled = lookasideEnabled;
82502 }
82503 #endif
82504
82505 if( rc==SQLITE_NOMEM ){
@@ -81769,10 +82670,13 @@
82670 }
82671 pPager = sqlite3BtreePager(aNew->pBt);
82672 sqlite3PagerLockingMode(pPager, db->dfltLockMode);
82673 sqlite3BtreeSecureDelete(aNew->pBt,
82674 sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
82675 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
82676 sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
82677 #endif
82678 }
82679 aNew->safety_level = 3;
82680 aNew->zName = sqlite3DbStrDup(db, zName);
82681 if( rc==SQLITE_OK && aNew->zName==0 ){
82682 rc = SQLITE_NOMEM;
@@ -84447,11 +85351,11 @@
85351 const char *zType, /* "idx" or "tbl" */
85352 const char *zName /* Name of index or table */
85353 ){
85354 int i;
85355 const char *zDbName = pParse->db->aDb[iDb].zName;
85356 for(i=1; i<=4; i++){
85357 char zTab[24];
85358 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
85359 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
85360 sqlite3NestedParse(pParse,
85361 "DELETE FROM %Q.%s WHERE %s=%Q",
@@ -85289,11 +86193,11 @@
86193
86194 /* Gather the complete text of the CREATE INDEX statement into
86195 ** the zStmt variable
86196 */
86197 if( pStart ){
86198 int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
86199 if( pName->z[n-1]==';' ) n--;
86200 /* A named index with an explicit CREATE INDEX statement */
86201 zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
86202 onError==OE_None ? "" : " UNIQUE", n, pName->z);
86203 }else{
@@ -88926,13 +89830,13 @@
89830 }
89831 static void groupConcatFinalize(sqlite3_context *context){
89832 StrAccum *pAccum;
89833 pAccum = sqlite3_aggregate_context(context, 0);
89834 if( pAccum ){
89835 if( pAccum->accError==STRACCUM_TOOBIG ){
89836 sqlite3_result_error_toobig(context);
89837 }else if( pAccum->accError==STRACCUM_NOMEM ){
89838 sqlite3_result_error_nomem(context);
89839 }else{
89840 sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
89841 sqlite3_free);
89842 }
@@ -89118,10 +90022,13 @@
90022 sqlite3FuncDefInsert(pHash, &aFunc[i]);
90023 }
90024 sqlite3RegisterDateTimeFunctions();
90025 #ifndef SQLITE_OMIT_ALTERTABLE
90026 sqlite3AlterFunctions();
90027 #endif
90028 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
90029 sqlite3AnalyzeFunctions();
90030 #endif
90031 }
90032
90033 /************** End of func.c ************************************************/
90034 /************** Begin file fkey.c ********************************************/
@@ -93834,10 +94741,40 @@
94741 sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
94742 sqlite3VdbeSetNumCols(v, 1);
94743 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
94744 sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
94745 }
94746
94747
94748 /*
94749 ** Set the safety_level and pager flags for pager iDb. Or if iDb<0
94750 ** set these values for all pagers.
94751 */
94752 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
94753 static void setAllPagerFlags(sqlite3 *db){
94754 if( db->autoCommit ){
94755 Db *pDb = db->aDb;
94756 int n = db->nDb;
94757 assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
94758 assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
94759 assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
94760 assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
94761 == PAGER_FLAGS_MASK );
94762 assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
94763 while( (n--) > 0 ){
94764 if( pDb->pBt ){
94765 sqlite3BtreeSetPagerFlags(pDb->pBt,
94766 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
94767 }
94768 pDb++;
94769 }
94770 }
94771 }
94772 #else
94773 # define setAllPagerFlags(X) /* no-op */
94774 #endif
94775
94776
94777 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
94778 /*
94779 ** Check to see if zRight and zLeft refer to a pragma that queries
94780 ** or changes one of the flags in db->flags. Return 1 if so and 0 if not.
@@ -93853,10 +94790,11 @@
94790 { "count_changes", SQLITE_CountRows },
94791 { "empty_result_callbacks", SQLITE_NullCallback },
94792 { "legacy_file_format", SQLITE_LegacyFileFmt },
94793 { "fullfsync", SQLITE_FullFSync },
94794 { "checkpoint_fullfsync", SQLITE_CkptFullFSync },
94795 { "cache_spill", SQLITE_CacheSpill },
94796 { "reverse_unordered_selects", SQLITE_ReverseOrder },
94797 { "query_only", SQLITE_QueryOnly },
94798 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
94799 { "automatic_index", SQLITE_AutoIndex },
94800 #endif
@@ -94307,11 +95245,11 @@
95245 */
95246 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
95247 Pager *pPager = sqlite3BtreePager(pDb->pBt);
95248 i64 iLimit = -2;
95249 if( zRight ){
95250 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
95251 if( iLimit<-1 ) iLimit = -1;
95252 }
95253 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
95254 returnSingleInt(pParse, "journal_size_limit", iLimit);
95255 }else
@@ -94441,14 +95379,15 @@
95379 ** as little or as much as it wants. Except, if N is set to 0 then the
95380 ** upper layers will never invoke the xFetch interfaces to the VFS.
95381 */
95382 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
95383 sqlite3_int64 sz;
95384 #if SQLITE_MAX_MMAP_SIZE>0
95385 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95386 if( zRight ){
95387 int ii;
95388 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
95389 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
95390 if( pId2->n==0 ) db->szMmap = sz;
95391 for(ii=db->nDb-1; ii>=0; ii--){
95392 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
95393 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94455,12 +95394,13 @@
95394 }
95395 }
95396 }
95397 sz = -1;
95398 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
95399 #else
95400 sz = 0;
95401 rc = SQLITE_OK;
95402 #endif
95403 if( rc==SQLITE_OK ){
95404 returnSingleInt(pParse, "mmap_size", sz);
95405 }else if( rc!=SQLITE_NOTFOUND ){
95406 pParse->nErr++;
@@ -94643,19 +95583,19 @@
95583 if( !db->autoCommit ){
95584 sqlite3ErrorMsg(pParse,
95585 "Safety level may not be changed inside a transaction");
95586 }else{
95587 pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
95588 setAllPagerFlags(db);
95589 }
95590 }
95591 }else
95592 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
95593
95594 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
95595 if( flagPragma(pParse, zLeft, zRight) ){
95596 setAllPagerFlags(db);
 
95597 }else
95598 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
95599
95600 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
95601 /*
@@ -95482,21 +96422,10 @@
96422 #endif
96423
96424
96425 {/* Empty ELSE clause */}
96426
 
 
 
 
 
 
 
 
 
 
 
96427 pragma_out:
96428 sqlite3DbFree(db, zLeft);
96429 sqlite3DbFree(db, zRight);
96430 }
96431
@@ -102619,11 +103548,11 @@
103548 ** space.
103549 */
103550 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
103551 assert( pTab!=0 );
103552 if( !pTab->pSelect ){
103553 sqlite3_value *pValue = 0;
103554 u8 enc = ENC(sqlite3VdbeDb(v));
103555 Column *pCol = &pTab->aCol[i];
103556 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
103557 assert( i<pTab->nCol );
103558 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104395,14 +105324,13 @@
105324 /*
105325 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
105326 ** array. Return the error code for the first error that occurs, or
105327 ** SQLITE_OK if all xSync operations are successful.
105328 **
105329 ** If an error message is available, leave it in p->zErrMsg.
 
105330 */
105331 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
105332 int i;
105333 int rc = SQLITE_OK;
105334 VTable **aVTrans = db->aVTrans;
105335
105336 db->aVTrans = 0;
@@ -104409,13 +105337,11 @@
105337 for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
105338 int (*x)(sqlite3_vtab *);
105339 sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
105340 if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
105341 rc = x(pVtab);
105342 sqlite3VtabImportErrmsg(p, pVtab);
 
 
105343 }
105344 }
105345 db->aVTrans = aVTrans;
105346 return rc;
105347 }
@@ -104776,10 +105702,11 @@
105702 int iIdxCur; /* The VDBE cursor used to access pIdx */
105703 int addrBrk; /* Jump here to break out of the loop */
105704 int addrNxt; /* Jump here to start the next IN combination */
105705 int addrCont; /* Jump here to continue with the next loop cycle */
105706 int addrFirst; /* First instruction of interior of the loop */
105707 int addrBody; /* Beginning of the body of this loop */
105708 u8 iFrom; /* Which entry in the FROM clause */
105709 u8 op, p5; /* Opcode and P5 of the opcode that ends the loop */
105710 int p1, p2; /* Operands of the opcode used to ends the loop */
105711 union { /* Information that depends on pWLoop->wsFlags */
105712 struct {
@@ -104971,11 +105898,11 @@
105898 #define TERM_CODED 0x04 /* This term is already coded */
105899 #define TERM_COPIED 0x08 /* Has a child */
105900 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
105901 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
105902 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
105903 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105904 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
105905 #else
105906 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
105907 #endif
105908
@@ -105077,10 +106004,14 @@
106004 WhereInfo *pWInfo; /* Information about this WHERE */
106005 WhereClause *pWC; /* WHERE clause terms */
106006 ExprList *pOrderBy; /* ORDER BY clause */
106007 WhereLoop *pNew; /* Template WhereLoop */
106008 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
106009 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106010 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
106011 int nRecValid; /* Number of valid fields currently in pRec */
106012 #endif
106013 };
106014
106015 /*
106016 ** The WHERE clause processing routine has two halves. The
106017 ** first part does the start of the WHERE loop and the second
@@ -105890,12 +106821,14 @@
106821 /*
106822 ** If the pBase expression originated in the ON or USING clause of
106823 ** a join, then transfer the appropriate markings over to derived.
106824 */
106825 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
106826 if( pDerived ){
106827 pDerived->flags |= pBase->flags & EP_FromJoin;
106828 pDerived->iRightJoinTable = pBase->iRightJoinTable;
106829 }
106830 }
106831
106832 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
106833 /*
106834 ** Analyze a term that consists of two or more OR-connected
@@ -106348,10 +107281,11 @@
107281 Expr *pNewExpr;
107282 int idxNew;
107283 pNewExpr = sqlite3PExpr(pParse, ops[i],
107284 sqlite3ExprDup(db, pExpr->pLeft, 0),
107285 sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
107286 transferJoinMarkings(pNewExpr, pExpr);
107287 idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
107288 testcase( idxNew==0 );
107289 exprAnalyze(pSrc, pWC, idxNew);
107290 pTerm = &pWC->a[idxTerm];
107291 pWC->a[idxNew].iParent = idxTerm;
@@ -106415,17 +107349,19 @@
107349 sCollSeqName.n = 6;
107350 pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
107351 pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
107352 sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
107353 pStr1, 0);
107354 transferJoinMarkings(pNewExpr1, pExpr);
107355 idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
107356 testcase( idxNew1==0 );
107357 exprAnalyze(pSrc, pWC, idxNew1);
107358 pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
107359 pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
107360 sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
107361 pStr2, 0);
107362 transferJoinMarkings(pNewExpr2, pExpr);
107363 idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
107364 testcase( idxNew2==0 );
107365 exprAnalyze(pSrc, pWC, idxNew2);
107366 pTerm = &pWC->a[idxTerm];
107367 if( isComplete ){
@@ -106471,11 +107407,11 @@
107407 pNewTerm->prereqAll = pTerm->prereqAll;
107408 }
107409 }
107410 #endif /* SQLITE_OMIT_VIRTUALTABLE */
107411
107412 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107413 /* When sqlite_stat3 histogram data is available an operator of the
107414 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
107415 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
107416 ** virtual term of that form.
107417 **
@@ -106511,11 +107447,11 @@
107447 pTerm->nChild = 1;
107448 pTerm->wtFlags |= TERM_COPIED;
107449 pNewTerm->prereqAll = pTerm->prereqAll;
107450 }
107451 }
107452 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107453
107454 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
107455 ** an index for tables to the left of the join.
107456 */
107457 pTerm->prereqRight |= extraRight;
@@ -107079,155 +108015,84 @@
108015 return pParse->nErr;
108016 }
108017 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
108018
108019
108020 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108021 /*
108022 ** Estimate the location of a particular key among all keys in an
108023 ** index. Store the results in aStat as follows:
108024 **
108025 ** aStat[0] Est. number of rows less than pVal
108026 ** aStat[1] Est. number of rows equal to pVal
108027 **
108028 ** Return SQLITE_OK on success.
108029 */
108030 static void whereKeyStats(
108031 Parse *pParse, /* Database connection */
108032 Index *pIdx, /* Index to consider domain of */
108033 UnpackedRecord *pRec, /* Vector of values to consider */
108034 int roundUp, /* Round up if true. Round down if false */
108035 tRowcnt *aStat /* OUT: stats written here */
108036 ){
108037 IndexSample *aSample = pIdx->aSample;
108038 int iCol = pRec->nField-1; /* Index of required stats in anEq[] etc. */
108039 int iMin = 0; /* Smallest sample not yet tested */
108040 int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
108041 int iTest; /* Next sample to test */
108042 int res; /* Result of comparison operation */
108043
 
108044 assert( pIdx->nSample>0 );
108045 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
108046 do{
108047 iTest = (iMin+i)/2;
108048 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
108049 if( res<0 ){
108050 iMin = iTest+1;
108051 }else{
108052 i = iTest;
108053 }
108054 }while( res && iMin<i );
108055
108056 #ifdef SQLITE_DEBUG
108057 /* The following assert statements check that the binary search code
108058 ** above found the right answer. This block serves no purpose other
108059 ** than to invoke the asserts. */
108060 if( res==0 ){
108061 /* If (res==0) is true, then sample $i must be equal to pRec */
108062 assert( i<pIdx->nSample );
108063 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
108064 || pParse->db->mallocFailed );
108065 }else{
108066 /* Otherwise, pRec must be smaller than sample $i and larger than
108067 ** sample ($i-1). */
108068 assert( i==pIdx->nSample
108069 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
108070 || pParse->db->mallocFailed );
108071 assert( i==0
108072 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
108073 || pParse->db->mallocFailed );
108074 }
108075 #endif /* ifdef SQLITE_DEBUG */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108076
108077 /* At this point, aSample[i] is the first sample that is greater than
108078 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
108079 ** than pVal. If aSample[i]==pVal, then res==0.
108080 */
108081 if( res==0 ){
108082 aStat[0] = aSample[i].anLt[iCol];
108083 aStat[1] = aSample[i].anEq[iCol];
 
108084 }else{
108085 tRowcnt iLower, iUpper, iGap;
108086 if( i==0 ){
108087 iLower = 0;
108088 iUpper = aSample[0].anLt[iCol];
108089 }else{
108090 iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
108091 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
108092 }
108093 aStat[1] = (pIdx->nColumn>iCol ? pIdx->aAvgEq[iCol] : 1);
108094 if( iLower>=iUpper ){
108095 iGap = 0;
108096 }else{
108097 iGap = iUpper - iLower;
108098 }
@@ -107236,48 +108101,12 @@
108101 }else{
108102 iGap = iGap/3;
108103 }
108104 aStat[0] = iLower + iGap;
108105 }
108106 }
108107 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108108
108109 /*
108110 ** This function is used to estimate the number of rows that will be visited
108111 ** by scanning an index for a range of values. The range may have an upper
108112 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107290,107 +108119,154 @@
108119 ** pLower pUpper
108120 **
108121 ** If either of the upper or lower bound is not present, then NULL is passed in
108122 ** place of the corresponding WhereTerm.
108123 **
108124 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
108125 ** column subject to the range constraint. Or, equivalently, the number of
108126 ** equality constraints optimized by the proposed index scan. For example,
108127 ** assuming index p is on t1(a, b), and the SQL query is:
108128 **
108129 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
108130 **
108131 ** then nEq is set to 1 (as the range restricted column, b, is the second
108132 ** left-most column of the index). Or, if the query is:
108133 **
108134 ** ... FROM t1 WHERE a > ? AND a < ? ...
108135 **
108136 ** then nEq is set to 0.
108137 **
108138 ** When this function is called, *pnOut is set to the whereCost() of the
108139 ** number of rows that the index scan is expected to visit without
108140 ** considering the range constraints. If nEq is 0, this is the number of
108141 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108142 ** to account for the range contraints pLower and pUpper.
108143 **
108144 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
108145 ** used, each range inequality reduces the search space by a factor of 4.
108146 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
108147 ** rows visited by a factor of 16.
108148 */
108149 static int whereRangeScanEst(
108150 Parse *pParse, /* Parsing & code generating context */
108151 WhereLoopBuilder *pBuilder,
 
108152 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
108153 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
108154 WhereCost *pnOut /* IN/OUT: Number of rows visited */
108155 ){
108156 int rc = SQLITE_OK;
108157 int nOut = (int)*pnOut;
108158
108159 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108160 Index *p = pBuilder->pNew->u.btree.pIndex;
108161 int nEq = pBuilder->pNew->u.btree.nEq;
108162
108163 if( nEq==pBuilder->nRecValid
108164 && nEq<p->nSampleCol
108165 && p->nSample
108166 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
108167 ){
108168 UnpackedRecord *pRec = pBuilder->pRec;
108169 tRowcnt a[2];
108170 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
108171
108172 /* Variable iLower will be set to the estimate of the number of rows in
108173 ** the index that are less than the lower bound of the range query. The
108174 ** lower bound being the concatenation of $P and $L, where $P is the
108175 ** key-prefix formed by the nEq values matched against the nEq left-most
108176 ** columns of the index, and $L is the value in pLower.
108177 **
108178 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
108179 ** is not a simple variable or literal value), the lower bound of the
108180 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
108181 ** if $L is available, whereKeyStats() is called for both ($P) and
108182 ** ($P:$L) and the larger of the two returned values used.
108183 **
108184 ** Similarly, iUpper is to be set to the estimate of the number of rows
108185 ** less than the upper bound of the range query. Where the upper bound
108186 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
108187 ** of iUpper are requested of whereKeyStats() and the smaller used.
108188 */
108189 tRowcnt iLower;
108190 tRowcnt iUpper;
108191
108192 /* Determine iLower and iUpper using ($P) only. */
108193 if( nEq==0 ){
108194 iLower = 0;
108195 iUpper = p->aiRowEst[0];
108196 }else{
108197 /* Note: this call could be optimized away - since the same values must
108198 ** have been requested when testing key $P in whereEqualScanEst(). */
108199 whereKeyStats(pParse, p, pRec, 0, a);
108200 iLower = a[0];
108201 iUpper = a[0] + a[1];
108202 }
108203
108204 /* If possible, improve on the iLower estimate using ($P:$L). */
108205 if( pLower ){
108206 int bOk; /* True if value is extracted from pExpr */
108207 Expr *pExpr = pLower->pExpr->pRight;
 
108208 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
108209 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108210 if( rc==SQLITE_OK && bOk ){
108211 tRowcnt iNew;
108212 whereKeyStats(pParse, p, pRec, 0, a);
108213 iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
108214 if( iNew>iLower ) iLower = iNew;
108215 }
108216 }
108217
108218 /* If possible, improve on the iUpper estimate using ($P:$U). */
108219 if( pUpper ){
108220 int bOk; /* True if value is extracted from pExpr */
108221 Expr *pExpr = pUpper->pExpr->pRight;
108222 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
108223 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108224 if( rc==SQLITE_OK && bOk ){
108225 tRowcnt iNew;
108226 whereKeyStats(pParse, p, pRec, 1, a);
108227 iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
108228 if( iNew<iUpper ) iUpper = iNew;
108229 }
108230 }
108231
108232 pBuilder->pRec = pRec;
108233 if( rc==SQLITE_OK ){
108234 WhereCost nNew;
108235 if( iUpper>iLower ){
108236 nNew = whereCost(iUpper - iLower);
108237 }else{
108238 nNew = 10; assert( 10==whereCost(2) );
108239 }
108240 if( nNew<nOut ){
108241 nOut = nNew;
108242 }
108243 *pnOut = (WhereCost)nOut;
108244 WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
108245 (u32)iLower, (u32)iUpper, nOut));
108246 return SQLITE_OK;
108247 }
108248 }
108249 #else
108250 UNUSED_PARAMETER(pParse);
108251 UNUSED_PARAMETER(pBuilder);
 
108252 #endif
108253 assert( pLower || pUpper );
 
108254 /* TUNING: Each inequality constraint reduces the search space 4-fold.
108255 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
108256 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
108257 nOut -= 20; assert( 20==whereCost(4) );
108258 }
108259 if( pUpper ){
108260 nOut -= 20; assert( 20==whereCost(4) );
108261 }
108262 if( nOut<10 ) nOut = 10;
108263 *pnOut = (WhereCost)nOut;
108264 return rc;
108265 }
108266
108267 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108268 /*
108269 ** Estimate the number of rows that will be returned based on
108270 ** an equality constraint x=VALUE and where that VALUE occurs in
108271 ** the histogram data. This only works when x is the left-most
108272 ** column of an index and sqlite_stat3 histogram data is available
@@ -107406,41 +108282,57 @@
108282 ** for a UTF conversion required for comparison. The error is stored
108283 ** in the pParse structure.
108284 */
108285 static int whereEqualScanEst(
108286 Parse *pParse, /* Parsing & code generating context */
108287 WhereLoopBuilder *pBuilder,
108288 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
108289 tRowcnt *pnRow /* Write the revised row estimate here */
108290 ){
108291 Index *p = pBuilder->pNew->u.btree.pIndex;
108292 int nEq = pBuilder->pNew->u.btree.nEq;
108293 UnpackedRecord *pRec = pBuilder->pRec;
108294 u8 aff; /* Column affinity */
108295 int rc; /* Subfunction return code */
108296 tRowcnt a[2]; /* Statistics */
108297 int bOk;
108298
108299 assert( nEq>=1 );
108300 assert( nEq<=(p->nColumn+1) );
108301 assert( p->aSample!=0 );
108302 assert( p->nSample>0 );
108303 assert( pBuilder->nRecValid<nEq );
108304
108305 /* If values are not available for all fields of the index to the left
108306 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
108307 if( pBuilder->nRecValid<(nEq-1) ){
108308 return SQLITE_NOTFOUND;
108309 }
108310
108311 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
108312 ** below would return the same value. */
108313 if( nEq>p->nColumn ){
108314 *pnRow = 1;
108315 return SQLITE_OK;
108316 }
108317
108318 aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
108319 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
108320 pBuilder->pRec = pRec;
108321 if( rc!=SQLITE_OK ) return rc;
108322 if( bOk==0 ) return SQLITE_NOTFOUND;
108323 pBuilder->nRecValid = nEq;
108324
108325 whereKeyStats(pParse, p, pRec, 0, a);
108326 WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
108327 *pnRow = a[1];
108328
108329 return rc;
108330 }
108331 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
108332
108333 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108334 /*
108335 ** Estimate the number of rows that will be returned based on
108336 ** an IN constraint where the right-hand side of the IN operator
108337 ** is a list of values. Example:
108338 **
@@ -107455,33 +108347,38 @@
108347 ** for a UTF conversion required for comparison. The error is stored
108348 ** in the pParse structure.
108349 */
108350 static int whereInScanEst(
108351 Parse *pParse, /* Parsing & code generating context */
108352 WhereLoopBuilder *pBuilder,
108353 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
108354 tRowcnt *pnRow /* Write the revised row estimate here */
108355 ){
108356 Index *p = pBuilder->pNew->u.btree.pIndex;
108357 int nRecValid = pBuilder->nRecValid;
108358 int rc = SQLITE_OK; /* Subfunction return code */
108359 tRowcnt nEst; /* Number of rows for a single term */
108360 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
108361 int i; /* Loop counter */
108362
108363 assert( p->aSample!=0 );
108364 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
108365 nEst = p->aiRowEst[0];
108366 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
108367 nRowEst += nEst;
108368 pBuilder->nRecValid = nRecValid;
108369 }
108370
108371 if( rc==SQLITE_OK ){
108372 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
108373 *pnRow = nRowEst;
108374 WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
108375 }
108376 assert( pBuilder->nRecValid==nRecValid );
108377 return rc;
108378 }
108379 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
108380
108381 /*
108382 ** Disable a term in the WHERE clause. Except, do not disable the term
108383 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
108384 ** or USING clause of that join.
@@ -107715,11 +108612,11 @@
108612 pParse->db->mallocFailed = 1;
108613 }
108614
108615 /* Evaluate the equality constraints
108616 */
108617 assert( zAff==0 || (int)strlen(zAff)>=nEq );
108618 for(j=0; j<nEq; j++){
108619 int r1;
108620 pTerm = pLoop->aLTerm[j];
108621 assert( pTerm!=0 );
108622 /* The following true for indices with redundant columns.
@@ -107807,11 +108704,12 @@
108704 }
108705 sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
108706 txt.db = db;
108707 sqlite3StrAccumAppend(&txt, " (", 2);
108708 for(i=0; i<nEq; i++){
108709 char *z = (i==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[i]].zName;
108710 explainAppendTerm(&txt, i, z, "=");
108711 }
108712
108713 j = i;
108714 if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
108715 char *z = (j==pIndex->nColumn ) ? "rowid" : aCol[aiColumn[j]].zName;
@@ -109021,16 +109919,22 @@
109919 saved_nOut = pNew->nOut;
109920 pNew->rSetup = 0;
109921 rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
109922 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
109923 int nIn = 0;
109924 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109925 int nRecValid = pBuilder->nRecValid;
109926 #endif
109927 if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
109928 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
109929 ){
109930 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
109931 }
109932 if( pTerm->prereqRight & pNew->maskSelf ) continue;
109933
109934 assert( pNew->nOut==saved_nOut );
109935
 
 
109936 pNew->wsFlags = saved_wsFlags;
109937 pNew->u.btree.nEq = saved_nEq;
109938 pNew->nLTerm = saved_nLTerm;
109939 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
109940 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109083,29 +109987,34 @@
109987 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
109988 pNew->aLTerm[pNew->nLTerm-2] : 0;
109989 }
109990 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
109991 /* Adjust nOut and rRun for STAT3 range values */
109992 assert( pNew->nOut==saved_nOut );
109993 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
109994 }
109995 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
109996 if( nInMul==0
109997 && pProbe->nSample
109998 && pNew->u.btree.nEq<=pProbe->nSampleCol
109999 && OptimizationEnabled(db, SQLITE_Stat3)
110000 ){
110001 Expr *pExpr = pTerm->pExpr;
110002 tRowcnt nOut = 0;
110003 if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
110004 testcase( pTerm->eOperator & WO_EQ );
110005 testcase( pTerm->eOperator & WO_ISNULL );
110006 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
110007 }else if( (pTerm->eOperator & WO_IN)
110008 && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110009 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
110010 }
110011 assert( nOut==0 || rc==SQLITE_OK );
110012 if( nOut ){
110013 nOut = whereCost(nOut);
110014 pNew->nOut = MIN(nOut, saved_nOut);
110015 }
110016 }
110017 #endif
110018 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
110019 /* Each row involves a step of the index, then a binary search of
110020 ** the main table */
@@ -109118,10 +110027,14 @@
110027 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
110028 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
110029 ){
110030 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
110031 }
110032 pNew->nOut = saved_nOut;
110033 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110034 pBuilder->nRecValid = nRecValid;
110035 #endif
110036 }
110037 pNew->prereq = saved_prereq;
110038 pNew->u.btree.nEq = saved_nEq;
110039 pNew->wsFlags = saved_wsFlags;
110040 pNew->nOut = saved_nOut;
@@ -109166,10 +110079,11 @@
110079 static Bitmask columnsInIndex(Index *pIdx){
110080 Bitmask m = 0;
110081 int j;
110082 for(j=pIdx->nColumn-1; j>=0; j--){
110083 int x = pIdx->aiColumn[j];
110084 assert( x>=0 );
110085 testcase( x==BMS-1 );
110086 testcase( x==BMS-2 );
110087 if( x<BMS-1 ) m |= MASKBIT(x);
110088 }
110089 return m;
@@ -109244,10 +110158,11 @@
110158 pProbe = &sPk;
110159 }
110160 rSize = whereCost(pSrc->pTab->nRowEst);
110161 rLogSize = estLog(rSize);
110162
110163 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110164 /* Automatic indexes */
110165 if( !pBuilder->pOrSet
110166 && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
110167 && pSrc->pIndex==0
110168 && !pSrc->viaCoroutine
@@ -109278,10 +110193,11 @@
110193 pNew->prereq = mExtra | pTerm->prereqRight;
110194 rc = whereLoopInsert(pBuilder, pNew);
110195 }
110196 }
110197 }
110198 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
110199
110200 /* Loop over all indices
110201 */
110202 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
110203 if( pProbe->pPartIdxWhere!=0
@@ -109344,11 +110260,17 @@
110260 }
110261 rc = whereLoopInsert(pBuilder, pNew);
110262 if( rc ) break;
110263 }
110264 }
110265
110266 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
110267 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110268 sqlite3Stat4ProbeFree(pBuilder->pRec);
110269 pBuilder->nRecValid = 0;
110270 pBuilder->pRec = 0;
110271 #endif
110272
110273 /* If there was an INDEXED BY clause, then only that one index is
110274 ** considered. */
110275 if( pSrc->pIndex ) break;
110276 }
@@ -109541,10 +110463,11 @@
110463
110464 pWC = pBuilder->pWC;
110465 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
110466 pWCEnd = pWC->a + pWC->nTerm;
110467 pNew = pBuilder->pNew;
110468 memset(&sSum, 0, sizeof(sSum));
110469
110470 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
110471 if( (pTerm->eOperator & WO_OR)!=0
110472 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
110473 ){
@@ -110226,15 +111149,19 @@
111149 pLoop->u.btree.nEq = 1;
111150 /* TUNING: Cost of a rowid lookup is 10 */
111151 pLoop->rRun = 33; /* 33==whereCost(10) */
111152 }else{
111153 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
111154 assert( pLoop->aLTermSpace==pLoop->aLTerm );
111155 assert( ArraySize(pLoop->aLTermSpace)==4 );
111156 if( pIdx->onError==OE_None
111157 || pIdx->pPartIdxWhere!=0
111158 || pIdx->nColumn>ArraySize(pLoop->aLTermSpace)
111159 ) continue;
111160 for(j=0; j<pIdx->nColumn; j++){
111161 pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
111162 if( pTerm==0 ) break;
 
111163 pLoop->aLTerm[j] = pTerm;
111164 }
111165 if( j!=pIdx->nColumn ) continue;
111166 pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
111167 if( (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
@@ -110667,15 +111594,10 @@
111594 assert( n<=pTab->nCol );
111595 }
111596 }else{
111597 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
111598 }
 
 
 
 
 
111599 if( pLoop->wsFlags & WHERE_INDEXED ){
111600 Index *pIx = pLoop->u.btree.pIndex;
111601 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIx);
111602 /* FIXME: As an optimization use pTabItem->iCursor if WHERE_IDX_ONLY */
111603 int iIndexCur = pLevel->iIdxCur = iIdxCur ? iIdxCur : pParse->nTab++;
@@ -110696,11 +111618,19 @@
111618 ** program.
111619 */
111620 notReady = ~(Bitmask)0;
111621 for(ii=0; ii<nTabList; ii++){
111622 pLevel = &pWInfo->a[ii];
111623 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
111624 if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
111625 constructAutomaticIndex(pParse, &pWInfo->sWC,
111626 &pTabList->a[pLevel->iFrom], notReady, pLevel);
111627 if( db->mallocFailed ) goto whereBeginError;
111628 }
111629 #endif
111630 explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
111631 pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
111632 notReady = codeOneLoopStart(pWInfo, ii, notReady);
111633 pWInfo->iContinue = pLevel->addrCont;
111634 }
111635
111636 /* Done. */
@@ -110816,13 +111746,14 @@
111746 }
111747 if( pIdx && !db->mallocFailed ){
111748 int k, j, last;
111749 VdbeOp *pOp;
111750
 
111751 last = sqlite3VdbeCurrentAddr(v);
111752 k = pLevel->addrBody;
111753 pOp = sqlite3VdbeGetOp(v, k);
111754 for(; k<last; k++, pOp++){
111755 if( pOp->p1!=pLevel->iTabCur ) continue;
111756 if( pOp->opcode==OP_Column ){
111757 for(j=0; j<pIdx->nColumn; j++){
111758 if( pOp->p2==pIdx->aiColumn[j] ){
111759 pOp->p2 = j;
@@ -117967,11 +118898,11 @@
118898 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
118899 db->autoCommit = 1;
118900 db->nextAutovac = -1;
118901 db->szMmap = sqlite3GlobalConfig.szMmap;
118902 db->nextPagesize = 0;
118903 db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
118904 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
118905 | SQLITE_AutoIndex
118906 #endif
118907 #if SQLITE_DEFAULT_FILE_FORMAT<4
118908 | SQLITE_LegacyFileFmt
@@ -130880,11 +131811,11 @@
131811 while( 1 ){
131812
131813 /* The following line of code (and the "p++" below the while() loop) is
131814 ** normally all that is required to move pointer p to the desired
131815 ** position. The exception is if this node is being loaded from disk
131816 ** incrementally and pointer "p" now points to the first byte past
131817 ** the populated part of pReader->aNode[].
131818 */
131819 while( *p | c ) c = *p++ & 0x80;
131820 assert( *p==0 );
131821
@@ -132267,12 +133198,12 @@
133198 fts3SegReaderFirstDocid(p, apSegment[i]);
133199 }
133200 fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
133201 while( apSegment[0]->pOffsetList ){
133202 int j; /* Number of segments that share a docid */
133203 char *pList = 0;
133204 int nList = 0;
133205 int nByte;
133206 sqlite3_int64 iDocid = apSegment[0]->iDocid;
133207 fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
133208 j = 1;
133209 while( j<nMerge
@@ -134554,15 +135485,15 @@
135485 }
135486 }
135487 if( pTC ) pModule->xClose(pTC);
135488 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
135489 }
135490 }
135491
135492 for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
135493 if( pDef->pList ){
135494 rc = fts3PendingListAppendVarint(&pDef->pList, 0);
 
135495 }
135496 }
135497 }
135498
135499 return rc;
@@ -135341,10 +136272,11 @@
136272 return SQLITE_NOMEM;
136273 }
136274 pStr->z = zNew;
136275 pStr->nAlloc = nAlloc;
136276 }
136277 assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
136278
136279 /* Append the data to the string buffer. */
136280 memcpy(&pStr->z[pStr->n], zAppend, nAppend);
136281 pStr->n += nAppend;
136282 pStr->z[pStr->n] = '\0';
136283
+3 -3
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.8.0"
111
-#define SQLITE_VERSION_NUMBER 3008000
112
-#define SQLITE_SOURCE_ID "2013-08-15 22:40:21 f2d175f975cd0be63425424ec322a98fb650019e"
110
+#define SQLITE_VERSION "3.8.1"
111
+#define SQLITE_VERSION_NUMBER 3008001
112
+#define SQLITE_SOURCE_ID "2013-08-30 06:20:23 d9c018f8155ab48df8e0e02519bba50588fe49fc"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
118118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.0"
111 #define SQLITE_VERSION_NUMBER 3008000
112 #define SQLITE_SOURCE_ID "2013-08-15 22:40:21 f2d175f975cd0be63425424ec322a98fb650019e"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.1"
111 #define SQLITE_VERSION_NUMBER 3008001
112 #define SQLITE_SOURCE_ID "2013-08-30 06:20:23 d9c018f8155ab48df8e0e02519bba50588fe49fc"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
118
+18 -3
--- src/style.c
+++ src/style.c
@@ -69,11 +69,11 @@
6969
** If g.javascriptHyperlink is true then the
7070
** id="ID" form is used and javascript is generated in the footer to cause
7171
** href values to be inserted after the page has loaded. If
7272
** g.perm.History is false, then the <a id="ID"> form is still
7373
** generated but the javascript is not generated so the links never
74
-** activate.
74
+** activate.
7575
**
7676
** If the user lacks the Hyperlink (h) property and the "auto-hyperlink"
7777
** setting is true, then g.perm.Hyperlink is changed from 0 to 1 and
7878
** g.javascriptHyperlink is set to 1. The g.javascriptHyperlink defaults
7979
** to 0 and only changes to one if the user lacks the Hyperlink (h) property
@@ -1053,11 +1053,11 @@
10531053
},
10541054
{ ".statistics-report-graph-line",
10551055
"for the /stats_report views",
10561056
@ background-color: #446979;
10571057
},
1058
- { ".statistics-report-table-events th"
1058
+ { ".statistics-report-table-events th",
10591059
"",
10601060
@ padding: 0 1em 0 1em;
10611061
},
10621062
{ ".statistics-report-table-events td",
10631063
"",
@@ -1085,10 +1085,14 @@
10851085
@ /* use default */
10861086
},
10871087
{ "tr.row1",
10881088
"odd table row color",
10891089
@ /* Use default */
1090
+ },
1091
+ { "#canvas", "timeline graph node colors",
1092
+ @ color: black;
1093
+ @ background-color: white;
10901094
},
10911095
{ 0,
10921096
0,
10931097
0
10941098
}
@@ -1152,15 +1156,25 @@
11521156
void page_test_env(void){
11531157
char c;
11541158
int i;
11551159
int showAll;
11561160
char zCap[30];
1161
+ static const char *azCgiVars[] = {
1162
+ "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
1163
+ "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET", "HTTP_ACCEPT_ENCODING",
1164
+ "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", "HTTP_HOST",
1165
+ "HTTP_USER_AGENT", "HTTP_REFERER", "PATH_INFO", "PATH_TRANSLATED",
1166
+ "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT", "REQUEST_METHOD",
1167
+ "REQUEST_URI", "SCRIPT_FILENAME", "SCRIPT_NAME", "SERVER_PROTOCOL",
1168
+ };
1169
+
11571170
login_check_credentials();
11581171
if( !g.perm.Admin && !g.perm.Setup && !db_get_boolean("test_env_enable",0) ){
11591172
login_needed();
11601173
return;
11611174
}
1175
+ for(i=0; i<count(azCgiVars); i++) (void)P(azCgiVars[i]);
11621176
style_header("Environment Test");
11631177
showAll = atoi(PD("showall","0"));
11641178
if( !showAll ){
11651179
style_submenu_element("Show Cookies", "Show Cookies",
11661180
"%s/test_env?showall=1", g.zTop);
@@ -1193,16 +1207,17 @@
11931207
if( g.perm.Setup ){
11941208
const char *zRedir = P("redirect");
11951209
if( zRedir ) cgi_redirect(zRedir);
11961210
}
11971211
style_footer();
1212
+ if( g.perm.Admin && P("err") ) fossil_fatal("%s", P("err"));
11981213
}
11991214
12001215
/*
1201
-** This page is a honeypot for spiders and bots.
1216
+** This page is a honeypot for spiders and bots.
12021217
**
12031218
** WEBPAGE: honeypot
12041219
*/
12051220
void honeypot_page(void){
12061221
cgi_set_status(403, "Forbidden");
12071222
@ <p>Access by spiders and robots is forbidden</p>
12081223
}
12091224
--- src/style.c
+++ src/style.c
@@ -69,11 +69,11 @@
69 ** If g.javascriptHyperlink is true then the
70 ** id="ID" form is used and javascript is generated in the footer to cause
71 ** href values to be inserted after the page has loaded. If
72 ** g.perm.History is false, then the <a id="ID"> form is still
73 ** generated but the javascript is not generated so the links never
74 ** activate.
75 **
76 ** If the user lacks the Hyperlink (h) property and the "auto-hyperlink"
77 ** setting is true, then g.perm.Hyperlink is changed from 0 to 1 and
78 ** g.javascriptHyperlink is set to 1. The g.javascriptHyperlink defaults
79 ** to 0 and only changes to one if the user lacks the Hyperlink (h) property
@@ -1053,11 +1053,11 @@
1053 },
1054 { ".statistics-report-graph-line",
1055 "for the /stats_report views",
1056 @ background-color: #446979;
1057 },
1058 { ".statistics-report-table-events th"
1059 "",
1060 @ padding: 0 1em 0 1em;
1061 },
1062 { ".statistics-report-table-events td",
1063 "",
@@ -1085,10 +1085,14 @@
1085 @ /* use default */
1086 },
1087 { "tr.row1",
1088 "odd table row color",
1089 @ /* Use default */
 
 
 
 
1090 },
1091 { 0,
1092 0,
1093 0
1094 }
@@ -1152,15 +1156,25 @@
1152 void page_test_env(void){
1153 char c;
1154 int i;
1155 int showAll;
1156 char zCap[30];
 
 
 
 
 
 
 
 
 
1157 login_check_credentials();
1158 if( !g.perm.Admin && !g.perm.Setup && !db_get_boolean("test_env_enable",0) ){
1159 login_needed();
1160 return;
1161 }
 
1162 style_header("Environment Test");
1163 showAll = atoi(PD("showall","0"));
1164 if( !showAll ){
1165 style_submenu_element("Show Cookies", "Show Cookies",
1166 "%s/test_env?showall=1", g.zTop);
@@ -1193,16 +1207,17 @@
1193 if( g.perm.Setup ){
1194 const char *zRedir = P("redirect");
1195 if( zRedir ) cgi_redirect(zRedir);
1196 }
1197 style_footer();
 
1198 }
1199
1200 /*
1201 ** This page is a honeypot for spiders and bots.
1202 **
1203 ** WEBPAGE: honeypot
1204 */
1205 void honeypot_page(void){
1206 cgi_set_status(403, "Forbidden");
1207 @ <p>Access by spiders and robots is forbidden</p>
1208 }
1209
--- src/style.c
+++ src/style.c
@@ -69,11 +69,11 @@
69 ** If g.javascriptHyperlink is true then the
70 ** id="ID" form is used and javascript is generated in the footer to cause
71 ** href values to be inserted after the page has loaded. If
72 ** g.perm.History is false, then the <a id="ID"> form is still
73 ** generated but the javascript is not generated so the links never
74 ** activate.
75 **
76 ** If the user lacks the Hyperlink (h) property and the "auto-hyperlink"
77 ** setting is true, then g.perm.Hyperlink is changed from 0 to 1 and
78 ** g.javascriptHyperlink is set to 1. The g.javascriptHyperlink defaults
79 ** to 0 and only changes to one if the user lacks the Hyperlink (h) property
@@ -1053,11 +1053,11 @@
1053 },
1054 { ".statistics-report-graph-line",
1055 "for the /stats_report views",
1056 @ background-color: #446979;
1057 },
1058 { ".statistics-report-table-events th",
1059 "",
1060 @ padding: 0 1em 0 1em;
1061 },
1062 { ".statistics-report-table-events td",
1063 "",
@@ -1085,10 +1085,14 @@
1085 @ /* use default */
1086 },
1087 { "tr.row1",
1088 "odd table row color",
1089 @ /* Use default */
1090 },
1091 { "#canvas", "timeline graph node colors",
1092 @ color: black;
1093 @ background-color: white;
1094 },
1095 { 0,
1096 0,
1097 0
1098 }
@@ -1152,15 +1156,25 @@
1156 void page_test_env(void){
1157 char c;
1158 int i;
1159 int showAll;
1160 char zCap[30];
1161 static const char *azCgiVars[] = {
1162 "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
1163 "HTTP_ACCEPT", "HTTP_ACCEPT_CHARSET", "HTTP_ACCEPT_ENCODING",
1164 "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION", "HTTP_HOST",
1165 "HTTP_USER_AGENT", "HTTP_REFERER", "PATH_INFO", "PATH_TRANSLATED",
1166 "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT", "REQUEST_METHOD",
1167 "REQUEST_URI", "SCRIPT_FILENAME", "SCRIPT_NAME", "SERVER_PROTOCOL",
1168 };
1169
1170 login_check_credentials();
1171 if( !g.perm.Admin && !g.perm.Setup && !db_get_boolean("test_env_enable",0) ){
1172 login_needed();
1173 return;
1174 }
1175 for(i=0; i<count(azCgiVars); i++) (void)P(azCgiVars[i]);
1176 style_header("Environment Test");
1177 showAll = atoi(PD("showall","0"));
1178 if( !showAll ){
1179 style_submenu_element("Show Cookies", "Show Cookies",
1180 "%s/test_env?showall=1", g.zTop);
@@ -1193,16 +1207,17 @@
1207 if( g.perm.Setup ){
1208 const char *zRedir = P("redirect");
1209 if( zRedir ) cgi_redirect(zRedir);
1210 }
1211 style_footer();
1212 if( g.perm.Admin && P("err") ) fossil_fatal("%s", P("err"));
1213 }
1214
1215 /*
1216 ** This page is a honeypot for spiders and bots.
1217 **
1218 ** WEBPAGE: honeypot
1219 */
1220 void honeypot_page(void){
1221 cgi_set_status(403, "Forbidden");
1222 @ <p>Access by spiders and robots is forbidden</p>
1223 }
1224
+1 -1
--- src/th.c
+++ src/th.c
@@ -1775,11 +1775,11 @@
17751775
{">", OP_GT, 5, ARG_NUMBER},
17761776
{"&", OP_BITWISE_AND, 8, ARG_INTEGER},
17771777
{"^", OP_BITWISE_XOR, 9, ARG_INTEGER},
17781778
{"|", OP_BITWISE_OR, 10, ARG_INTEGER},
17791779
1780
- {0,0,0}
1780
+ {0,0,0,0}
17811781
};
17821782
17831783
/*
17841784
** The first part of the string (zInput,nInput) contains a number.
17851785
** Set *pnVarname to the number of bytes in the numeric string.
17861786
--- src/th.c
+++ src/th.c
@@ -1775,11 +1775,11 @@
1775 {">", OP_GT, 5, ARG_NUMBER},
1776 {"&", OP_BITWISE_AND, 8, ARG_INTEGER},
1777 {"^", OP_BITWISE_XOR, 9, ARG_INTEGER},
1778 {"|", OP_BITWISE_OR, 10, ARG_INTEGER},
1779
1780 {0,0,0}
1781 };
1782
1783 /*
1784 ** The first part of the string (zInput,nInput) contains a number.
1785 ** Set *pnVarname to the number of bytes in the numeric string.
1786
--- src/th.c
+++ src/th.c
@@ -1775,11 +1775,11 @@
1775 {">", OP_GT, 5, ARG_NUMBER},
1776 {"&", OP_BITWISE_AND, 8, ARG_INTEGER},
1777 {"^", OP_BITWISE_XOR, 9, ARG_INTEGER},
1778 {"|", OP_BITWISE_OR, 10, ARG_INTEGER},
1779
1780 {0,0,0,0}
1781 };
1782
1783 /*
1784 ** The first part of the string (zInput,nInput) contains a number.
1785 ** Set *pnVarname to the number of bytes in the numeric string.
1786
+3 -2
--- src/th_main.c
+++ src/th_main.c
@@ -430,12 +430,13 @@
430430
431431
if( Th_ToInt(interp, argv[3], argl[3], &height) ) return TH_ERROR;
432432
Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem);
433433
blob_init(&name, (char*)argv[1], argl[1]);
434434
zValue = Th_Fetch(blob_str(&name), &nValue);
435
- z = mprintf("<select name=\"%z\" size=\"%d\">",
436
- htmlize(blob_buffer(&name), blob_size(&name)), height);
435
+ zH = htmlize(blob_buffer(&name), blob_size(&name));
436
+ z = mprintf("<select id=\"%s\" name=\"%s\" size=\"%d\">", zH, zH, height);
437
+ free(zH);
437438
sendText(z, -1, 0);
438439
free(z);
439440
blob_reset(&name);
440441
for(i=0; i<nElem; i++){
441442
zH = htmlize((char*)azElem[i], aszElem[i]);
442443
--- src/th_main.c
+++ src/th_main.c
@@ -430,12 +430,13 @@
430
431 if( Th_ToInt(interp, argv[3], argl[3], &height) ) return TH_ERROR;
432 Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem);
433 blob_init(&name, (char*)argv[1], argl[1]);
434 zValue = Th_Fetch(blob_str(&name), &nValue);
435 z = mprintf("<select name=\"%z\" size=\"%d\">",
436 htmlize(blob_buffer(&name), blob_size(&name)), height);
 
437 sendText(z, -1, 0);
438 free(z);
439 blob_reset(&name);
440 for(i=0; i<nElem; i++){
441 zH = htmlize((char*)azElem[i], aszElem[i]);
442
--- src/th_main.c
+++ src/th_main.c
@@ -430,12 +430,13 @@
430
431 if( Th_ToInt(interp, argv[3], argl[3], &height) ) return TH_ERROR;
432 Th_SplitList(interp, argv[2], argl[2], &azElem, &aszElem, &nElem);
433 blob_init(&name, (char*)argv[1], argl[1]);
434 zValue = Th_Fetch(blob_str(&name), &nValue);
435 zH = htmlize(blob_buffer(&name), blob_size(&name));
436 z = mprintf("<select id=\"%s\" name=\"%s\" size=\"%d\">", zH, zH, height);
437 free(zH);
438 sendText(z, -1, 0);
439 free(z);
440 blob_reset(&name);
441 for(i=0; i<nElem; i++){
442 zH = htmlize((char*)azElem[i], aszElem[i]);
443
+25 -12
--- src/timeline.c
+++ src/timeline.c
@@ -649,13 +649,13 @@
649649
cgi_printf("],h:\"%s\"}%s", pRow->zUuid, pRow->pNext ? ",\n" : "];\n");
650650
}
651651
cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
652652
graph_free(pGraph);
653653
@ var canvasDiv = gebi("canvas");
654
- @ var canvasStyle = window.getComputedStyle(canvasDiv,null);
655
- @ var lineColor = canvasStyle.getPropertyValue('color') || 'black';
656
- @ var bgColor = canvasStyle.getPropertyValue('background-color') || 'white';
654
+ @ var canvasStyle = window.getComputedStyle && window.getComputedStyle(canvasDiv,null);
655
+ @ var lineColor = (canvasStyle && canvasStyle.getPropertyValue('color')) || 'black';
656
+ @ var bgColor = (canvasStyle && canvasStyle.getPropertyValue('background-color')) || 'white';
657657
@ if( bgColor=='transparent' ) bgColor = 'white';
658658
@ var boxColor = lineColor;
659659
@ function drawBox(color,x0,y0,x1,y1){
660660
@ var n = document.createElement("div");
661661
@ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
@@ -667,11 +667,10 @@
667667
@ n.style.left = x0+"px";
668668
@ n.style.top = y0+"px";
669669
@ n.style.width = w+"px";
670670
@ n.style.height = h+"px";
671671
@ n.style.backgroundColor = color;
672
- @ n.style.cursor = "pointer";
673672
@ canvasDiv.appendChild(n);
674673
@ return n;
675674
@ }
676675
@ function absoluteY(id){
677676
@ var obj = gebi(id);
@@ -717,15 +716,18 @@
717716
@ }
718717
@ }
719718
@ function drawThinLine(x0,y0,x1,y1){
720719
@ drawBox(lineColor,x0,y0,x1,y1);
721720
@ }
721
+ @ function drawNodeBox(color,x0,y0,x1,y1){
722
+ @ drawBox(color,x0,y0,x1,y1).style.cursor = "pointer";
723
+ @ }
722724
@ function drawNode(p, left, btm){
723
- @ drawBox(boxColor,p.x-5,p.y-5,p.x+6,p.y+6);
724
- @ drawBox(p.bg||bgColor,p.x-4,p.y-4,p.x+5,p.y+5);
725
+ @ drawNodeBox(boxColor,p.x-5,p.y-5,p.x+6,p.y+6);
726
+ @ drawNodeBox(p.bg||bgColor,p.x-4,p.y-4,p.x+5,p.y+5);
725727
@ if( p.u>0 ) drawUpArrow(p.x, rowinfo[p.u-1].y+6, p.y-5);
726
- @ if( p.f&1 ) drawBox(boxColor,p.x-1,p.y-1,p.x+2,p.y+2);
728
+ @ if( p.f&1 ) drawNodeBox(boxColor,p.x-1,p.y-1,p.x+2,p.y+2);
727729
if( !omitDescenders ){
728730
@ if( p.u==0 ) drawUpArrow(p.x, 0, p.y-5);
729731
@ if( p.d ) drawUpArrow(p.x, p.y+6, btm);
730732
}
731733
@ if( p.mo>0 ){
@@ -1902,12 +1904,14 @@
19021904
int nEventsPerYear = 0; /* Total event count for the
19031905
current year */
19041906
char showYearTotal = 0; /* Flag telling us when to show
19051907
the per-year event totals */
19061908
Blob header = empty_blob; /* Page header text */
1907
- int nMaxEvents = 1; /* for calculating length of graph bars. */
1908
-
1909
+ int nMaxEvents = 1; /* for calculating length of graph
1910
+ bars. */
1911
+ int iterations = 0; /* number of weeks/months we iterate
1912
+ over */
19091913
blob_appendf(&header, "Timeline Events by year%s",
19101914
(includeMonth ? "/month" : ""));
19111915
blob_appendf(&sql,
19121916
"SELECT substr(date(mtime),1,%d) AS timeframe, "
19131917
"count(*) AS eventCount "
@@ -1940,10 +1944,11 @@
19401944
while( SQLITE_ROW == db_step(&query) ){
19411945
const int nCount = db_column_int(&query, 1);
19421946
if(nCount>nMaxEvents){
19431947
nMaxEvents = nCount;
19441948
}
1949
+ ++iterations;
19451950
}
19461951
db_reset(&query);
19471952
while( SQLITE_ROW == db_step(&query) ){
19481953
const char * zTimeframe = db_column_text(&query, 0);
19491954
const int nCount = db_column_int(&query, 1);
@@ -2025,11 +2030,15 @@
20252030
@ <td colspan='2'>Yearly total: %d(nEventsPerYear)</td>
20262031
@</tr>
20272032
}
20282033
@ </tbody></table>
20292034
if(nEventTotal){
2030
- @ <br><div>Total events: %d(nEventTotal)</div>
2035
+ char const * zAvgLabel = includeMonth ? "month" : "year";
2036
+ int nAvg = iterations ? (nEventTotal/iterations) : 0;
2037
+ @ <br><div>Total events: %d(nEventTotal)
2038
+ @ <br>Average per active %s(zAvgLabel): %d(nAvg)
2039
+ @ </div>
20312040
}
20322041
if( !includeMonth ){
20332042
output_table_sorting_javascript("statsTable","tnx");
20342043
}
20352044
}
@@ -2110,10 +2119,11 @@
21102119
Stmt qYears = empty_Stmt;
21112120
char * zDefaultYear = NULL;
21122121
Blob sql = empty_blob;
21132122
int nMaxEvents = 1; /* max number of events for
21142123
all rows. */
2124
+ int iterations = 0; /* # of active time periods. */
21152125
21162126
cgi_printf("Select year: ");
21172127
blob_append(&sql,
21182128
"SELECT DISTINCT substr(date(mtime),1,4) AS y "
21192129
"FROM event WHERE 1 ", -1);
@@ -2176,10 +2186,11 @@
21762186
while( SQLITE_ROW == db_step(&stWeek) ){
21772187
const int nCount = db_column_int(&stWeek, 1);
21782188
if(nCount>nMaxEvents){
21792189
nMaxEvents = nCount;
21802190
}
2191
+ ++iterations;
21812192
}
21822193
db_reset(&stWeek);
21832194
while( SQLITE_ROW == db_step(&stWeek) ){
21842195
const char * zWeek = db_column_text(&stWeek,0);
21852196
const int nCount = db_column_int(&stWeek,1);
@@ -2206,12 +2217,14 @@
22062217
}
22072218
db_finalize(&stWeek);
22082219
free(zDefaultYear);
22092220
cgi_printf("</tbody></table>");
22102221
if(total){
2211
- cgi_printf("<br><div>Total events: %d</div>",
2212
- total);
2222
+ int nAvg = iterations ? (total/iterations) : 0;
2223
+ cgi_printf("<br><div>Total events: %d<br>"
2224
+ "Average per active week: %d</div>",
2225
+ total, nAvg);
22132226
}
22142227
output_table_sorting_javascript("statsTable","tnx");
22152228
}
22162229
}
22172230
22182231
--- src/timeline.c
+++ src/timeline.c
@@ -649,13 +649,13 @@
649 cgi_printf("],h:\"%s\"}%s", pRow->zUuid, pRow->pNext ? ",\n" : "];\n");
650 }
651 cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
652 graph_free(pGraph);
653 @ var canvasDiv = gebi("canvas");
654 @ var canvasStyle = window.getComputedStyle(canvasDiv,null);
655 @ var lineColor = canvasStyle.getPropertyValue('color') || 'black';
656 @ var bgColor = canvasStyle.getPropertyValue('background-color') || 'white';
657 @ if( bgColor=='transparent' ) bgColor = 'white';
658 @ var boxColor = lineColor;
659 @ function drawBox(color,x0,y0,x1,y1){
660 @ var n = document.createElement("div");
661 @ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
@@ -667,11 +667,10 @@
667 @ n.style.left = x0+"px";
668 @ n.style.top = y0+"px";
669 @ n.style.width = w+"px";
670 @ n.style.height = h+"px";
671 @ n.style.backgroundColor = color;
672 @ n.style.cursor = "pointer";
673 @ canvasDiv.appendChild(n);
674 @ return n;
675 @ }
676 @ function absoluteY(id){
677 @ var obj = gebi(id);
@@ -717,15 +716,18 @@
717 @ }
718 @ }
719 @ function drawThinLine(x0,y0,x1,y1){
720 @ drawBox(lineColor,x0,y0,x1,y1);
721 @ }
 
 
 
722 @ function drawNode(p, left, btm){
723 @ drawBox(boxColor,p.x-5,p.y-5,p.x+6,p.y+6);
724 @ drawBox(p.bg||bgColor,p.x-4,p.y-4,p.x+5,p.y+5);
725 @ if( p.u>0 ) drawUpArrow(p.x, rowinfo[p.u-1].y+6, p.y-5);
726 @ if( p.f&1 ) drawBox(boxColor,p.x-1,p.y-1,p.x+2,p.y+2);
727 if( !omitDescenders ){
728 @ if( p.u==0 ) drawUpArrow(p.x, 0, p.y-5);
729 @ if( p.d ) drawUpArrow(p.x, p.y+6, btm);
730 }
731 @ if( p.mo>0 ){
@@ -1902,12 +1904,14 @@
1902 int nEventsPerYear = 0; /* Total event count for the
1903 current year */
1904 char showYearTotal = 0; /* Flag telling us when to show
1905 the per-year event totals */
1906 Blob header = empty_blob; /* Page header text */
1907 int nMaxEvents = 1; /* for calculating length of graph bars. */
1908
 
 
1909 blob_appendf(&header, "Timeline Events by year%s",
1910 (includeMonth ? "/month" : ""));
1911 blob_appendf(&sql,
1912 "SELECT substr(date(mtime),1,%d) AS timeframe, "
1913 "count(*) AS eventCount "
@@ -1940,10 +1944,11 @@
1940 while( SQLITE_ROW == db_step(&query) ){
1941 const int nCount = db_column_int(&query, 1);
1942 if(nCount>nMaxEvents){
1943 nMaxEvents = nCount;
1944 }
 
1945 }
1946 db_reset(&query);
1947 while( SQLITE_ROW == db_step(&query) ){
1948 const char * zTimeframe = db_column_text(&query, 0);
1949 const int nCount = db_column_int(&query, 1);
@@ -2025,11 +2030,15 @@
2025 @ <td colspan='2'>Yearly total: %d(nEventsPerYear)</td>
2026 @</tr>
2027 }
2028 @ </tbody></table>
2029 if(nEventTotal){
2030 @ <br><div>Total events: %d(nEventTotal)</div>
 
 
 
 
2031 }
2032 if( !includeMonth ){
2033 output_table_sorting_javascript("statsTable","tnx");
2034 }
2035 }
@@ -2110,10 +2119,11 @@
2110 Stmt qYears = empty_Stmt;
2111 char * zDefaultYear = NULL;
2112 Blob sql = empty_blob;
2113 int nMaxEvents = 1; /* max number of events for
2114 all rows. */
 
2115
2116 cgi_printf("Select year: ");
2117 blob_append(&sql,
2118 "SELECT DISTINCT substr(date(mtime),1,4) AS y "
2119 "FROM event WHERE 1 ", -1);
@@ -2176,10 +2186,11 @@
2176 while( SQLITE_ROW == db_step(&stWeek) ){
2177 const int nCount = db_column_int(&stWeek, 1);
2178 if(nCount>nMaxEvents){
2179 nMaxEvents = nCount;
2180 }
 
2181 }
2182 db_reset(&stWeek);
2183 while( SQLITE_ROW == db_step(&stWeek) ){
2184 const char * zWeek = db_column_text(&stWeek,0);
2185 const int nCount = db_column_int(&stWeek,1);
@@ -2206,12 +2217,14 @@
2206 }
2207 db_finalize(&stWeek);
2208 free(zDefaultYear);
2209 cgi_printf("</tbody></table>");
2210 if(total){
2211 cgi_printf("<br><div>Total events: %d</div>",
2212 total);
 
 
2213 }
2214 output_table_sorting_javascript("statsTable","tnx");
2215 }
2216 }
2217
2218
--- src/timeline.c
+++ src/timeline.c
@@ -649,13 +649,13 @@
649 cgi_printf("],h:\"%s\"}%s", pRow->zUuid, pRow->pNext ? ",\n" : "];\n");
650 }
651 cgi_printf("var nrail = %d\n", pGraph->mxRail+1);
652 graph_free(pGraph);
653 @ var canvasDiv = gebi("canvas");
654 @ var canvasStyle = window.getComputedStyle && window.getComputedStyle(canvasDiv,null);
655 @ var lineColor = (canvasStyle && canvasStyle.getPropertyValue('color')) || 'black';
656 @ var bgColor = (canvasStyle && canvasStyle.getPropertyValue('background-color')) || 'white';
657 @ if( bgColor=='transparent' ) bgColor = 'white';
658 @ var boxColor = lineColor;
659 @ function drawBox(color,x0,y0,x1,y1){
660 @ var n = document.createElement("div");
661 @ if( x0>x1 ){ var t=x0; x0=x1; x1=t; }
@@ -667,11 +667,10 @@
667 @ n.style.left = x0+"px";
668 @ n.style.top = y0+"px";
669 @ n.style.width = w+"px";
670 @ n.style.height = h+"px";
671 @ n.style.backgroundColor = color;
 
672 @ canvasDiv.appendChild(n);
673 @ return n;
674 @ }
675 @ function absoluteY(id){
676 @ var obj = gebi(id);
@@ -717,15 +716,18 @@
716 @ }
717 @ }
718 @ function drawThinLine(x0,y0,x1,y1){
719 @ drawBox(lineColor,x0,y0,x1,y1);
720 @ }
721 @ function drawNodeBox(color,x0,y0,x1,y1){
722 @ drawBox(color,x0,y0,x1,y1).style.cursor = "pointer";
723 @ }
724 @ function drawNode(p, left, btm){
725 @ drawNodeBox(boxColor,p.x-5,p.y-5,p.x+6,p.y+6);
726 @ drawNodeBox(p.bg||bgColor,p.x-4,p.y-4,p.x+5,p.y+5);
727 @ if( p.u>0 ) drawUpArrow(p.x, rowinfo[p.u-1].y+6, p.y-5);
728 @ if( p.f&1 ) drawNodeBox(boxColor,p.x-1,p.y-1,p.x+2,p.y+2);
729 if( !omitDescenders ){
730 @ if( p.u==0 ) drawUpArrow(p.x, 0, p.y-5);
731 @ if( p.d ) drawUpArrow(p.x, p.y+6, btm);
732 }
733 @ if( p.mo>0 ){
@@ -1902,12 +1904,14 @@
1904 int nEventsPerYear = 0; /* Total event count for the
1905 current year */
1906 char showYearTotal = 0; /* Flag telling us when to show
1907 the per-year event totals */
1908 Blob header = empty_blob; /* Page header text */
1909 int nMaxEvents = 1; /* for calculating length of graph
1910 bars. */
1911 int iterations = 0; /* number of weeks/months we iterate
1912 over */
1913 blob_appendf(&header, "Timeline Events by year%s",
1914 (includeMonth ? "/month" : ""));
1915 blob_appendf(&sql,
1916 "SELECT substr(date(mtime),1,%d) AS timeframe, "
1917 "count(*) AS eventCount "
@@ -1940,10 +1944,11 @@
1944 while( SQLITE_ROW == db_step(&query) ){
1945 const int nCount = db_column_int(&query, 1);
1946 if(nCount>nMaxEvents){
1947 nMaxEvents = nCount;
1948 }
1949 ++iterations;
1950 }
1951 db_reset(&query);
1952 while( SQLITE_ROW == db_step(&query) ){
1953 const char * zTimeframe = db_column_text(&query, 0);
1954 const int nCount = db_column_int(&query, 1);
@@ -2025,11 +2030,15 @@
2030 @ <td colspan='2'>Yearly total: %d(nEventsPerYear)</td>
2031 @</tr>
2032 }
2033 @ </tbody></table>
2034 if(nEventTotal){
2035 char const * zAvgLabel = includeMonth ? "month" : "year";
2036 int nAvg = iterations ? (nEventTotal/iterations) : 0;
2037 @ <br><div>Total events: %d(nEventTotal)
2038 @ <br>Average per active %s(zAvgLabel): %d(nAvg)
2039 @ </div>
2040 }
2041 if( !includeMonth ){
2042 output_table_sorting_javascript("statsTable","tnx");
2043 }
2044 }
@@ -2110,10 +2119,11 @@
2119 Stmt qYears = empty_Stmt;
2120 char * zDefaultYear = NULL;
2121 Blob sql = empty_blob;
2122 int nMaxEvents = 1; /* max number of events for
2123 all rows. */
2124 int iterations = 0; /* # of active time periods. */
2125
2126 cgi_printf("Select year: ");
2127 blob_append(&sql,
2128 "SELECT DISTINCT substr(date(mtime),1,4) AS y "
2129 "FROM event WHERE 1 ", -1);
@@ -2176,10 +2186,11 @@
2186 while( SQLITE_ROW == db_step(&stWeek) ){
2187 const int nCount = db_column_int(&stWeek, 1);
2188 if(nCount>nMaxEvents){
2189 nMaxEvents = nCount;
2190 }
2191 ++iterations;
2192 }
2193 db_reset(&stWeek);
2194 while( SQLITE_ROW == db_step(&stWeek) ){
2195 const char * zWeek = db_column_text(&stWeek,0);
2196 const int nCount = db_column_int(&stWeek,1);
@@ -2206,12 +2217,14 @@
2217 }
2218 db_finalize(&stWeek);
2219 free(zDefaultYear);
2220 cgi_printf("</tbody></table>");
2221 if(total){
2222 int nAvg = iterations ? (total/iterations) : 0;
2223 cgi_printf("<br><div>Total events: %d<br>"
2224 "Average per active week: %d</div>",
2225 total, nAvg);
2226 }
2227 output_table_sorting_javascript("statsTable","tnx");
2228 }
2229 }
2230
2231
+3 -3
--- src/tkt.c
+++ src/tkt.c
@@ -520,11 +520,11 @@
520520
const char *zTktId, /* The ticket to which this change is applied */
521521
int needMod /* True if moderation is needed */
522522
){
523523
int rid = content_put_ex(pTicket, 0, 0, 0, needMod);
524524
if( rid==0 ){
525
- fossil_panic("trouble committing ticket: %s", g.zErrMsg);
525
+ fossil_fatal("trouble committing ticket: %s", g.zErrMsg);
526526
}
527527
if( needMod ){
528528
moderation_table_create();
529529
db_multi_exec(
530530
"INSERT INTO modreq(objid, tktid) VALUES(%d,'%s')",
@@ -676,11 +676,11 @@
676676
if( g.thTrace ) Th_Trace("BEGIN_TKTNEW_SCRIPT<br />\n", -1);
677677
if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zNewUuid ){
678678
cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zNewUuid));
679679
return;
680680
}
681
- captcha_generate();
681
+ captcha_generate(0);
682682
@ </form>
683683
if( g.thTrace ) Th_Trace("END_TKTVIEW<br />\n", -1);
684684
style_footer();
685685
}
686686
@@ -744,11 +744,11 @@
744744
if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT_SCRIPT<br />\n", -1);
745745
if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zName ){
746746
cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zName));
747747
return;
748748
}
749
- captcha_generate();
749
+ captcha_generate(0);
750750
@ </form>
751751
if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br />\n", -1);
752752
style_footer();
753753
}
754754
755755
--- src/tkt.c
+++ src/tkt.c
@@ -520,11 +520,11 @@
520 const char *zTktId, /* The ticket to which this change is applied */
521 int needMod /* True if moderation is needed */
522 ){
523 int rid = content_put_ex(pTicket, 0, 0, 0, needMod);
524 if( rid==0 ){
525 fossil_panic("trouble committing ticket: %s", g.zErrMsg);
526 }
527 if( needMod ){
528 moderation_table_create();
529 db_multi_exec(
530 "INSERT INTO modreq(objid, tktid) VALUES(%d,'%s')",
@@ -676,11 +676,11 @@
676 if( g.thTrace ) Th_Trace("BEGIN_TKTNEW_SCRIPT<br />\n", -1);
677 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zNewUuid ){
678 cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zNewUuid));
679 return;
680 }
681 captcha_generate();
682 @ </form>
683 if( g.thTrace ) Th_Trace("END_TKTVIEW<br />\n", -1);
684 style_footer();
685 }
686
@@ -744,11 +744,11 @@
744 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT_SCRIPT<br />\n", -1);
745 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zName ){
746 cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zName));
747 return;
748 }
749 captcha_generate();
750 @ </form>
751 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br />\n", -1);
752 style_footer();
753 }
754
755
--- src/tkt.c
+++ src/tkt.c
@@ -520,11 +520,11 @@
520 const char *zTktId, /* The ticket to which this change is applied */
521 int needMod /* True if moderation is needed */
522 ){
523 int rid = content_put_ex(pTicket, 0, 0, 0, needMod);
524 if( rid==0 ){
525 fossil_fatal("trouble committing ticket: %s", g.zErrMsg);
526 }
527 if( needMod ){
528 moderation_table_create();
529 db_multi_exec(
530 "INSERT INTO modreq(objid, tktid) VALUES(%d,'%s')",
@@ -676,11 +676,11 @@
676 if( g.thTrace ) Th_Trace("BEGIN_TKTNEW_SCRIPT<br />\n", -1);
677 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zNewUuid ){
678 cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zNewUuid));
679 return;
680 }
681 captcha_generate(0);
682 @ </form>
683 if( g.thTrace ) Th_Trace("END_TKTVIEW<br />\n", -1);
684 style_footer();
685 }
686
@@ -744,11 +744,11 @@
744 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT_SCRIPT<br />\n", -1);
745 if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zName ){
746 cgi_redirect(mprintf("%s/tktview/%s", g.zTop, zName));
747 return;
748 }
749 captcha_generate(0);
750 @ </form>
751 if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br />\n", -1);
752 style_footer();
753 }
754
755
+1 -1
--- src/unicode.c
+++ src/unicode.c
@@ -38,11 +38,11 @@
3838
** the size of the range (always at least 1). In other words, the value
3939
** ((C<<22) + N) represents a range of N codepoints starting with codepoint
4040
** C. It is not possible to represent a range larger than 1023 codepoints
4141
** using this format.
4242
*/
43
- const static unsigned int aEntry[] = {
43
+ static const unsigned int aEntry[] = {
4444
0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
4545
0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
4646
0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
4747
0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
4848
0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
4949
--- src/unicode.c
+++ src/unicode.c
@@ -38,11 +38,11 @@
38 ** the size of the range (always at least 1). In other words, the value
39 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
40 ** C. It is not possible to represent a range larger than 1023 codepoints
41 ** using this format.
42 */
43 const static unsigned int aEntry[] = {
44 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
45 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
46 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
47 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
48 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
49
--- src/unicode.c
+++ src/unicode.c
@@ -38,11 +38,11 @@
38 ** the size of the range (always at least 1). In other words, the value
39 ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
40 ** C. It is not possible to represent a range larger than 1023 codepoints
41 ** using this format.
42 */
43 static const unsigned int aEntry[] = {
44 0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
45 0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
46 0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
47 0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
48 0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
49
+6 -7
--- src/update.c
+++ src/update.c
@@ -199,11 +199,11 @@
199199
" ORDER BY event.mtime DESC");
200200
if( tid==0 ) tid = vid;
201201
}
202202
203203
if( tid==0 ){
204
- fossil_panic("Internal Error: unable to find a version to update to.");
204
+ fossil_panic("unable to find a version to update to.");
205205
}
206206
207207
db_begin_transaction();
208208
vfile_check_signature(vid, CKSIG_ENOTFILE);
209209
if( !dryRunFlag && !internalUpdate ) undo_begin();
@@ -224,11 +224,11 @@
224224
" islinkv BOOLEAN," /* True if current file is a link */
225225
" islinkt BOOLEAN," /* True if target file is a link */
226226
" ridv INTEGER," /* Record ID for current version */
227227
" ridt INTEGER," /* Record ID for target */
228228
" isexe BOOLEAN," /* Does target have execute permission? */
229
- " deleted BOOLEAN DEFAULT 0,"/* File marke by "rm" to become unmanaged */
229
+ " deleted BOOLEAN DEFAULT 0,"/* File marked by "rm" to become unmanaged */
230230
" fnt TEXT %s" /* Filename of same file on target version */
231231
");",
232232
filename_collation(), filename_collation()
233233
);
234234
@@ -400,20 +400,19 @@
400400
if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
401401
}else if( idt>0 && idv>0 && ridt!=ridv && (chnged==0 || deleted) ){
402402
/* The file is unedited. Change it to the target version */
403403
undo_save(zName);
404404
if( deleted ){
405
- fossil_print("UPDATE %s - change to unmanged file\n", zName);
405
+ fossil_print("UPDATE %s - change to unmanaged file\n", zName);
406406
}else{
407407
fossil_print("UPDATE %s\n", zName);
408408
}
409409
if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
410
- }else if( idt>0 && idv>0 && file_wd_size(zFullPath)<0 ){
410
+ }else if( idt>0 && idv>0 && !deleted && file_wd_size(zFullPath)<0 ){
411411
/* The file missing from the local check-out. Restore it to the
412412
** version that appears in the target. */
413
- fossil_print("UPDATE %s%s\n", zName,
414
- deleted?" - change to unmanaged file":"");
413
+ fossil_print("UPDATE %s\n", zName);
415414
undo_save(zName);
416415
if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
417416
}else if( idt==0 && idv>0 ){
418417
if( ridv==0 ){
419418
/* Added in current checkout. Continue to hold the file as
@@ -659,11 +658,11 @@
659658
}
660659
}else if( errCode<=0 ){
661660
if( revision==0 ){
662661
revision = db_text("current", "SELECT uuid FROM blob WHERE rid=%d", rid);
663662
}
664
- fossil_panic("could not parse manifest for checkin: %s", revision);
663
+ fossil_fatal("could not parse manifest for checkin: %s", revision);
665664
}
666665
return errCode;
667666
}
668667
669668
670669
--- src/update.c
+++ src/update.c
@@ -199,11 +199,11 @@
199 " ORDER BY event.mtime DESC");
200 if( tid==0 ) tid = vid;
201 }
202
203 if( tid==0 ){
204 fossil_panic("Internal Error: unable to find a version to update to.");
205 }
206
207 db_begin_transaction();
208 vfile_check_signature(vid, CKSIG_ENOTFILE);
209 if( !dryRunFlag && !internalUpdate ) undo_begin();
@@ -224,11 +224,11 @@
224 " islinkv BOOLEAN," /* True if current file is a link */
225 " islinkt BOOLEAN," /* True if target file is a link */
226 " ridv INTEGER," /* Record ID for current version */
227 " ridt INTEGER," /* Record ID for target */
228 " isexe BOOLEAN," /* Does target have execute permission? */
229 " deleted BOOLEAN DEFAULT 0,"/* File marke by "rm" to become unmanaged */
230 " fnt TEXT %s" /* Filename of same file on target version */
231 ");",
232 filename_collation(), filename_collation()
233 );
234
@@ -400,20 +400,19 @@
400 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
401 }else if( idt>0 && idv>0 && ridt!=ridv && (chnged==0 || deleted) ){
402 /* The file is unedited. Change it to the target version */
403 undo_save(zName);
404 if( deleted ){
405 fossil_print("UPDATE %s - change to unmanged file\n", zName);
406 }else{
407 fossil_print("UPDATE %s\n", zName);
408 }
409 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
410 }else if( idt>0 && idv>0 && file_wd_size(zFullPath)<0 ){
411 /* The file missing from the local check-out. Restore it to the
412 ** version that appears in the target. */
413 fossil_print("UPDATE %s%s\n", zName,
414 deleted?" - change to unmanaged file":"");
415 undo_save(zName);
416 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
417 }else if( idt==0 && idv>0 ){
418 if( ridv==0 ){
419 /* Added in current checkout. Continue to hold the file as
@@ -659,11 +658,11 @@
659 }
660 }else if( errCode<=0 ){
661 if( revision==0 ){
662 revision = db_text("current", "SELECT uuid FROM blob WHERE rid=%d", rid);
663 }
664 fossil_panic("could not parse manifest for checkin: %s", revision);
665 }
666 return errCode;
667 }
668
669
670
--- src/update.c
+++ src/update.c
@@ -199,11 +199,11 @@
199 " ORDER BY event.mtime DESC");
200 if( tid==0 ) tid = vid;
201 }
202
203 if( tid==0 ){
204 fossil_panic("unable to find a version to update to.");
205 }
206
207 db_begin_transaction();
208 vfile_check_signature(vid, CKSIG_ENOTFILE);
209 if( !dryRunFlag && !internalUpdate ) undo_begin();
@@ -224,11 +224,11 @@
224 " islinkv BOOLEAN," /* True if current file is a link */
225 " islinkt BOOLEAN," /* True if target file is a link */
226 " ridv INTEGER," /* Record ID for current version */
227 " ridt INTEGER," /* Record ID for target */
228 " isexe BOOLEAN," /* Does target have execute permission? */
229 " deleted BOOLEAN DEFAULT 0,"/* File marked by "rm" to become unmanaged */
230 " fnt TEXT %s" /* Filename of same file on target version */
231 ");",
232 filename_collation(), filename_collation()
233 );
234
@@ -400,20 +400,19 @@
400 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
401 }else if( idt>0 && idv>0 && ridt!=ridv && (chnged==0 || deleted) ){
402 /* The file is unedited. Change it to the target version */
403 undo_save(zName);
404 if( deleted ){
405 fossil_print("UPDATE %s - change to unmanaged file\n", zName);
406 }else{
407 fossil_print("UPDATE %s\n", zName);
408 }
409 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
410 }else if( idt>0 && idv>0 && !deleted && file_wd_size(zFullPath)<0 ){
411 /* The file missing from the local check-out. Restore it to the
412 ** version that appears in the target. */
413 fossil_print("UPDATE %s\n", zName);
 
414 undo_save(zName);
415 if( !dryRunFlag ) vfile_to_disk(0, idt, 0, 0);
416 }else if( idt==0 && idv>0 ){
417 if( ridv==0 ){
418 /* Added in current checkout. Continue to hold the file as
@@ -659,11 +658,11 @@
658 }
659 }else if( errCode<=0 ){
660 if( revision==0 ){
661 revision = db_text("current", "SELECT uuid FROM blob WHERE rid=%d", rid);
662 }
663 fossil_fatal("could not parse manifest for checkin: %s", revision);
664 }
665 return errCode;
666 }
667
668
669
+1 -1
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328328
g.argv[4], uid
329329
);
330330
}
331331
fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332332
}else{
333
- fossil_panic("user subcommand should be one of: "
333
+ fossil_fatal("user subcommand should be one of: "
334334
"capabilities default list new password");
335335
}
336336
}
337337
338338
/*
339339
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328 g.argv[4], uid
329 );
330 }
331 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332 }else{
333 fossil_panic("user subcommand should be one of: "
334 "capabilities default list new password");
335 }
336 }
337
338 /*
339
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328 g.argv[4], uid
329 );
330 }
331 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332 }else{
333 fossil_fatal("user subcommand should be one of: "
334 "capabilities default list new password");
335 }
336 }
337
338 /*
339
+1 -1
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328328
g.argv[4], uid
329329
);
330330
}
331331
fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332332
}else{
333
- fossil_panic("user subcommand should be one of: "
333
+ fossil_fatal("user subcommand should be one of: "
334334
"capabilities default list new password");
335335
}
336336
}
337337
338338
/*
339339
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328 g.argv[4], uid
329 );
330 }
331 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332 }else{
333 fossil_panic("user subcommand should be one of: "
334 "capabilities default list new password");
335 }
336 }
337
338 /*
339
--- src/user.c
+++ src/user.c
@@ -328,11 +328,11 @@
328 g.argv[4], uid
329 );
330 }
331 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
332 }else{
333 fossil_fatal("user subcommand should be one of: "
334 "capabilities default list new password");
335 }
336 }
337
338 /*
339
+15
--- src/util.c
+++ src/util.c
@@ -26,10 +26,13 @@
2626
#ifdef _WIN32
2727
# include <windows.h>
2828
#else
2929
# include <sys/time.h>
3030
# include <sys/resource.h>
31
+# include <unistd.h>
32
+# include <fcntl.h>
33
+# include <errno.h>
3134
#endif
3235
3336
3437
/*
3538
** Exit. Take care to close the database first.
@@ -299,5 +302,17 @@
299302
int const rc = fossilTimerList[timerId-1].id;
300303
assert(!rc || (rc == timerId));
301304
return fossilTimerList[timerId-1].id;
302305
}
303306
}
307
+
308
+/*
309
+** Return TRUE if fd is a valid open file descriptor. This only
310
+** works on unix. The function always returns true on Windows.
311
+*/
312
+int is_valid_fd(int fd){
313
+#ifdef _WIN32
314
+ return 1;
315
+#else
316
+ return fcntl(fd, F_GETFL)!=(-1) || errno!=EBADF;
317
+#endif
318
+}
304319
--- src/util.c
+++ src/util.c
@@ -26,10 +26,13 @@
26 #ifdef _WIN32
27 # include <windows.h>
28 #else
29 # include <sys/time.h>
30 # include <sys/resource.h>
 
 
 
31 #endif
32
33
34 /*
35 ** Exit. Take care to close the database first.
@@ -299,5 +302,17 @@
299 int const rc = fossilTimerList[timerId-1].id;
300 assert(!rc || (rc == timerId));
301 return fossilTimerList[timerId-1].id;
302 }
303 }
 
 
 
 
 
 
 
 
 
 
 
 
304
--- src/util.c
+++ src/util.c
@@ -26,10 +26,13 @@
26 #ifdef _WIN32
27 # include <windows.h>
28 #else
29 # include <sys/time.h>
30 # include <sys/resource.h>
31 # include <unistd.h>
32 # include <fcntl.h>
33 # include <errno.h>
34 #endif
35
36
37 /*
38 ** Exit. Take care to close the database first.
@@ -299,5 +302,17 @@
302 int const rc = fossilTimerList[timerId-1].id;
303 assert(!rc || (rc == timerId));
304 return fossilTimerList[timerId-1].id;
305 }
306 }
307
308 /*
309 ** Return TRUE if fd is a valid open file descriptor. This only
310 ** works on unix. The function always returns true on Windows.
311 */
312 int is_valid_fd(int fd){
313 #ifdef _WIN32
314 return 1;
315 #else
316 return fcntl(fd, F_GETFL)!=(-1) || errno!=EBADF;
317 #endif
318 }
319
+1 -1
--- src/verify.c
+++ src/verify.c
@@ -41,11 +41,11 @@
4141
return; /* No way to verify phantoms */
4242
}
4343
blob_zero(&uuid);
4444
db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d", rid);
4545
if( blob_size(&uuid)!=UUID_SIZE ){
46
- fossil_panic("not a valid rid: %d", rid);
46
+ fossil_fatal("not a valid rid: %d", rid);
4747
}
4848
if( content_get(rid, &content) ){
4949
sha1sum_blob(&content, &hash);
5050
blob_reset(&content);
5151
if( blob_compare(&uuid, &hash) ){
5252
--- src/verify.c
+++ src/verify.c
@@ -41,11 +41,11 @@
41 return; /* No way to verify phantoms */
42 }
43 blob_zero(&uuid);
44 db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d", rid);
45 if( blob_size(&uuid)!=UUID_SIZE ){
46 fossil_panic("not a valid rid: %d", rid);
47 }
48 if( content_get(rid, &content) ){
49 sha1sum_blob(&content, &hash);
50 blob_reset(&content);
51 if( blob_compare(&uuid, &hash) ){
52
--- src/verify.c
+++ src/verify.c
@@ -41,11 +41,11 @@
41 return; /* No way to verify phantoms */
42 }
43 blob_zero(&uuid);
44 db_blob(&uuid, "SELECT uuid FROM blob WHERE rid=%d", rid);
45 if( blob_size(&uuid)!=UUID_SIZE ){
46 fossil_fatal("not a valid rid: %d", rid);
47 }
48 if( content_get(rid, &content) ){
49 sha1sum_blob(&content, &hash);
50 blob_reset(&content);
51 if( blob_compare(&uuid, &hash) ){
52
+1 -1
--- src/vfile.c
+++ src/vfile.c
@@ -744,11 +744,11 @@
744744
blob_zero(pManOut);
745745
}
746746
db_must_be_within_tree();
747747
pManifest = manifest_get(vid, CFTYPE_MANIFEST);
748748
if( pManifest==0 ){
749
- fossil_panic("manifest file (%d) is malformed", vid);
749
+ fossil_fatal("manifest file (%d) is malformed", vid);
750750
}
751751
manifest_file_rewind(pManifest);
752752
while( (pFile = manifest_file_next(pManifest,0))!=0 ){
753753
if( pFile->zUuid==0 ) continue;
754754
fid = uuid_to_rid(pFile->zUuid, 0);
755755
--- src/vfile.c
+++ src/vfile.c
@@ -744,11 +744,11 @@
744 blob_zero(pManOut);
745 }
746 db_must_be_within_tree();
747 pManifest = manifest_get(vid, CFTYPE_MANIFEST);
748 if( pManifest==0 ){
749 fossil_panic("manifest file (%d) is malformed", vid);
750 }
751 manifest_file_rewind(pManifest);
752 while( (pFile = manifest_file_next(pManifest,0))!=0 ){
753 if( pFile->zUuid==0 ) continue;
754 fid = uuid_to_rid(pFile->zUuid, 0);
755
--- src/vfile.c
+++ src/vfile.c
@@ -744,11 +744,11 @@
744 blob_zero(pManOut);
745 }
746 db_must_be_within_tree();
747 pManifest = manifest_get(vid, CFTYPE_MANIFEST);
748 if( pManifest==0 ){
749 fossil_fatal("manifest file (%d) is malformed", vid);
750 }
751 manifest_file_rewind(pManifest);
752 while( (pFile = manifest_file_next(pManifest,0))!=0 ){
753 if( pFile->zUuid==0 ) continue;
754 fid = uuid_to_rid(pFile->zUuid, 0);
755
+3 -3
--- src/wiki.c
+++ src/wiki.c
@@ -96,11 +96,11 @@
9696
cgi_redirectf("%s/login?g=%s/home", g.zTop, g.zTop);
9797
}
9898
if( zPageName ){
9999
login_check_credentials();
100100
g.zExtra = zPageName;
101
- cgi_set_parameter_nocopy("name", g.zExtra);
101
+ cgi_set_parameter_nocopy("name", g.zExtra, 1);
102102
g.isHome = 1;
103103
wiki_page();
104104
return;
105105
}
106106
style_header("Home");
@@ -493,11 +493,11 @@
493493
@ <input type="submit" name="submit" value="Apply These Changes" />
494494
@ <input type="hidden" name="name" value="%h(zPageName)" />
495495
@ <input type="submit" name="cancel" value="Cancel"
496496
@ onclick='confirm("Abandon your changes?")' />
497497
@ </div>
498
- captcha_generate();
498
+ captcha_generate(0);
499499
@ </form>
500500
manifest_destroy(pWiki);
501501
blob_reset(&wiki);
502502
style_footer();
503503
}
@@ -696,11 +696,11 @@
696696
@ rows="10" wrap="virtual">%h(PD("r",""))</textarea>
697697
@ <br />
698698
@ <input type="submit" name="preview" value="Preview Your Comment" />
699699
@ <input type="submit" name="submit" value="Append Your Changes" />
700700
@ <input type="submit" name="cancel" value="Cancel" />
701
- captcha_generate();
701
+ captcha_generate(0);
702702
@ </form>
703703
style_footer();
704704
}
705705
706706
/*
707707
--- src/wiki.c
+++ src/wiki.c
@@ -96,11 +96,11 @@
96 cgi_redirectf("%s/login?g=%s/home", g.zTop, g.zTop);
97 }
98 if( zPageName ){
99 login_check_credentials();
100 g.zExtra = zPageName;
101 cgi_set_parameter_nocopy("name", g.zExtra);
102 g.isHome = 1;
103 wiki_page();
104 return;
105 }
106 style_header("Home");
@@ -493,11 +493,11 @@
493 @ <input type="submit" name="submit" value="Apply These Changes" />
494 @ <input type="hidden" name="name" value="%h(zPageName)" />
495 @ <input type="submit" name="cancel" value="Cancel"
496 @ onclick='confirm("Abandon your changes?")' />
497 @ </div>
498 captcha_generate();
499 @ </form>
500 manifest_destroy(pWiki);
501 blob_reset(&wiki);
502 style_footer();
503 }
@@ -696,11 +696,11 @@
696 @ rows="10" wrap="virtual">%h(PD("r",""))</textarea>
697 @ <br />
698 @ <input type="submit" name="preview" value="Preview Your Comment" />
699 @ <input type="submit" name="submit" value="Append Your Changes" />
700 @ <input type="submit" name="cancel" value="Cancel" />
701 captcha_generate();
702 @ </form>
703 style_footer();
704 }
705
706 /*
707
--- src/wiki.c
+++ src/wiki.c
@@ -96,11 +96,11 @@
96 cgi_redirectf("%s/login?g=%s/home", g.zTop, g.zTop);
97 }
98 if( zPageName ){
99 login_check_credentials();
100 g.zExtra = zPageName;
101 cgi_set_parameter_nocopy("name", g.zExtra, 1);
102 g.isHome = 1;
103 wiki_page();
104 return;
105 }
106 style_header("Home");
@@ -493,11 +493,11 @@
493 @ <input type="submit" name="submit" value="Apply These Changes" />
494 @ <input type="hidden" name="name" value="%h(zPageName)" />
495 @ <input type="submit" name="cancel" value="Cancel"
496 @ onclick='confirm("Abandon your changes?")' />
497 @ </div>
498 captcha_generate(0);
499 @ </form>
500 manifest_destroy(pWiki);
501 blob_reset(&wiki);
502 style_footer();
503 }
@@ -696,11 +696,11 @@
696 @ rows="10" wrap="virtual">%h(PD("r",""))</textarea>
697 @ <br />
698 @ <input type="submit" name="preview" value="Preview Your Comment" />
699 @ <input type="submit" name="submit" value="Append Your Changes" />
700 @ <input type="submit" name="cancel" value="Cancel" />
701 captcha_generate(0);
702 @ </form>
703 style_footer();
704 }
705
706 /*
707
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -211,11 +211,11 @@
211211
212212
A manifest might contain one or more T-cards used to set
213213
[./branching.wiki#tags | tags or properties]
214214
on the check-in. The format of the T-card is the same as
215215
described in <i>Control Artifacts</i> section below, except that the
216
-second argument is the single characcter "<b>*</b>" instead of an
216
+second argument is the single character "<b>*</b>" instead of an
217217
artifact ID. The <b>*</b> in place of the artifact ID indicates that
218218
the tag or property applies to the current artifact. It is not
219219
possible to encode the current artifact ID as part of an artifact,
220220
since the act of inserting the artifact ID would change the artifact ID,
221221
hence a <b>*</b> is used to represent "self". T-cards are typically
@@ -482,11 +482,11 @@
482482
<b>W</b> <i>size</i> <b>\n</b> <i>text</i> <b>\n</b><br />
483483
<b>Z</b> <i>checksum</i>
484484
</blockquote>
485485
486486
The C card contains text that is displayed on the timeline for the
487
-event. Exactly one C card is required on an event artifact.
487
+event. The C card is optional, but there can only be one.
488488
489489
A single D card is required to give the date and time when the
490490
event artifact was created. This is different from the time at which
491491
the event occurs.
492492
@@ -508,11 +508,11 @@
508508
509509
An event might contain one or more T-cards used to set
510510
[./branching.wiki#tags | tags or properties]
511511
on the event. The format of the T-card is the same as
512512
described in [#ctrl | Control Artifacts] section above, except that the
513
-second argument is the single characcter "<b>*</b>" instead of an
513
+second argument is the single character "<b>*</b>" instead of an
514514
artifact ID and the name is always prefaced by "<b>+</b>".
515515
The <b>*</b> in place of the artifact ID indicates that
516516
the tag or property applies to the current artifact. It is not
517517
possible to encode the current artifact ID as part of an artifact,
518518
since the act of inserting the artifact ID would change the artifact ID,
@@ -581,11 +581,11 @@
581581
<td>&nbsp;</td>
582582
<td>&nbsp;</td>
583583
<td>&nbsp;</td>
584584
<td>&nbsp;</td>
585585
<td align=center><b>0-1</b></td>
586
-<td align=center><b>1</b></td>
586
+<td align=center><b>0-1</b></td>
587587
</tr>
588588
<tr>
589589
<td><b>D</b> <i>date-time-stamp</i></td>
590590
<td align=center><b>1</b></td>
591591
<td align=center>&nbsp;</td>
@@ -685,18 +685,17 @@
685685
<td align=center>&nbsp;</td>
686686
<td align=center>&nbsp;</td>
687687
</tr>
688688
<tr>
689689
<td><b>R</b> <i>md5sum</i></td>
690
-<td align=center><b>0-1*</b></td>
690
+<td align=center><b>0-1</b></td>
691691
<td align=center>&nbsp;</td>
692692
<td align=center>&nbsp;</td>
693693
<td align=center>&nbsp;</td>
694694
<td align=center>&nbsp;</td>
695695
<td align=center>&nbsp;</td>
696696
<td align=center>&nbsp;</td>
697
-<tr><td>&nbsp;</td><td colspan='7'>* = Required when using F cards</td></tr>
698697
<tr>
699698
<td><b>T</b> (<b>+</b>|<b>*</b>|<b>-</b>)<i>tagname</i> <i>uuid</i> ?<i>value</i>?</td>
700699
<td align=center><b>0+</b></td>
701700
<td align=center>&nbsp;</td>
702701
<td align=center><b>1+</b></td>
@@ -771,5 +770,14 @@
771770
echo $i $(stat -c '%s' $i); \
772771
cat $i; \
773772
done | md5sum
774773
c0788982781981c96a0d81465fec7192 -
775774
</pre></nowiki>
775
+
776
+Minor caveats: the above demonstration will work only when none of the
777
+filenames in the manifest are "fossilized" (encoded) because they contain
778
+spaces. In that case the shell-generated hash would differ because the
779
+<tt>stat</tt> calls will fail to find such files (which are output in encoded
780
+form here). That approach also won't work for delta manifests. Calculating
781
+the R-card for delta manifests requires traversing both the delta and its baseline in
782
+lexical order of the files, prefering the delta's copy if both contain
783
+a given file.
776784
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -211,11 +211,11 @@
211
212 A manifest might contain one or more T-cards used to set
213 [./branching.wiki#tags | tags or properties]
214 on the check-in. The format of the T-card is the same as
215 described in <i>Control Artifacts</i> section below, except that the
216 second argument is the single characcter "<b>*</b>" instead of an
217 artifact ID. The <b>*</b> in place of the artifact ID indicates that
218 the tag or property applies to the current artifact. It is not
219 possible to encode the current artifact ID as part of an artifact,
220 since the act of inserting the artifact ID would change the artifact ID,
221 hence a <b>*</b> is used to represent "self". T-cards are typically
@@ -482,11 +482,11 @@
482 <b>W</b> <i>size</i> <b>\n</b> <i>text</i> <b>\n</b><br />
483 <b>Z</b> <i>checksum</i>
484 </blockquote>
485
486 The C card contains text that is displayed on the timeline for the
487 event. Exactly one C card is required on an event artifact.
488
489 A single D card is required to give the date and time when the
490 event artifact was created. This is different from the time at which
491 the event occurs.
492
@@ -508,11 +508,11 @@
508
509 An event might contain one or more T-cards used to set
510 [./branching.wiki#tags | tags or properties]
511 on the event. The format of the T-card is the same as
512 described in [#ctrl | Control Artifacts] section above, except that the
513 second argument is the single characcter "<b>*</b>" instead of an
514 artifact ID and the name is always prefaced by "<b>+</b>".
515 The <b>*</b> in place of the artifact ID indicates that
516 the tag or property applies to the current artifact. It is not
517 possible to encode the current artifact ID as part of an artifact,
518 since the act of inserting the artifact ID would change the artifact ID,
@@ -581,11 +581,11 @@
581 <td>&nbsp;</td>
582 <td>&nbsp;</td>
583 <td>&nbsp;</td>
584 <td>&nbsp;</td>
585 <td align=center><b>0-1</b></td>
586 <td align=center><b>1</b></td>
587 </tr>
588 <tr>
589 <td><b>D</b> <i>date-time-stamp</i></td>
590 <td align=center><b>1</b></td>
591 <td align=center>&nbsp;</td>
@@ -685,18 +685,17 @@
685 <td align=center>&nbsp;</td>
686 <td align=center>&nbsp;</td>
687 </tr>
688 <tr>
689 <td><b>R</b> <i>md5sum</i></td>
690 <td align=center><b>0-1*</b></td>
691 <td align=center>&nbsp;</td>
692 <td align=center>&nbsp;</td>
693 <td align=center>&nbsp;</td>
694 <td align=center>&nbsp;</td>
695 <td align=center>&nbsp;</td>
696 <td align=center>&nbsp;</td>
697 <tr><td>&nbsp;</td><td colspan='7'>* = Required when using F cards</td></tr>
698 <tr>
699 <td><b>T</b> (<b>+</b>|<b>*</b>|<b>-</b>)<i>tagname</i> <i>uuid</i> ?<i>value</i>?</td>
700 <td align=center><b>0+</b></td>
701 <td align=center>&nbsp;</td>
702 <td align=center><b>1+</b></td>
@@ -771,5 +770,14 @@
771 echo $i $(stat -c '%s' $i); \
772 cat $i; \
773 done | md5sum
774 c0788982781981c96a0d81465fec7192 -
775 </pre></nowiki>
 
 
 
 
 
 
 
 
 
776
--- www/fileformat.wiki
+++ www/fileformat.wiki
@@ -211,11 +211,11 @@
211
212 A manifest might contain one or more T-cards used to set
213 [./branching.wiki#tags | tags or properties]
214 on the check-in. The format of the T-card is the same as
215 described in <i>Control Artifacts</i> section below, except that the
216 second argument is the single character "<b>*</b>" instead of an
217 artifact ID. The <b>*</b> in place of the artifact ID indicates that
218 the tag or property applies to the current artifact. It is not
219 possible to encode the current artifact ID as part of an artifact,
220 since the act of inserting the artifact ID would change the artifact ID,
221 hence a <b>*</b> is used to represent "self". T-cards are typically
@@ -482,11 +482,11 @@
482 <b>W</b> <i>size</i> <b>\n</b> <i>text</i> <b>\n</b><br />
483 <b>Z</b> <i>checksum</i>
484 </blockquote>
485
486 The C card contains text that is displayed on the timeline for the
487 event. The C card is optional, but there can only be one.
488
489 A single D card is required to give the date and time when the
490 event artifact was created. This is different from the time at which
491 the event occurs.
492
@@ -508,11 +508,11 @@
508
509 An event might contain one or more T-cards used to set
510 [./branching.wiki#tags | tags or properties]
511 on the event. The format of the T-card is the same as
512 described in [#ctrl | Control Artifacts] section above, except that the
513 second argument is the single character "<b>*</b>" instead of an
514 artifact ID and the name is always prefaced by "<b>+</b>".
515 The <b>*</b> in place of the artifact ID indicates that
516 the tag or property applies to the current artifact. It is not
517 possible to encode the current artifact ID as part of an artifact,
518 since the act of inserting the artifact ID would change the artifact ID,
@@ -581,11 +581,11 @@
581 <td>&nbsp;</td>
582 <td>&nbsp;</td>
583 <td>&nbsp;</td>
584 <td>&nbsp;</td>
585 <td align=center><b>0-1</b></td>
586 <td align=center><b>0-1</b></td>
587 </tr>
588 <tr>
589 <td><b>D</b> <i>date-time-stamp</i></td>
590 <td align=center><b>1</b></td>
591 <td align=center>&nbsp;</td>
@@ -685,18 +685,17 @@
685 <td align=center>&nbsp;</td>
686 <td align=center>&nbsp;</td>
687 </tr>
688 <tr>
689 <td><b>R</b> <i>md5sum</i></td>
690 <td align=center><b>0-1</b></td>
691 <td align=center>&nbsp;</td>
692 <td align=center>&nbsp;</td>
693 <td align=center>&nbsp;</td>
694 <td align=center>&nbsp;</td>
695 <td align=center>&nbsp;</td>
696 <td align=center>&nbsp;</td>
 
697 <tr>
698 <td><b>T</b> (<b>+</b>|<b>*</b>|<b>-</b>)<i>tagname</i> <i>uuid</i> ?<i>value</i>?</td>
699 <td align=center><b>0+</b></td>
700 <td align=center>&nbsp;</td>
701 <td align=center><b>1+</b></td>
@@ -771,5 +770,14 @@
770 echo $i $(stat -c '%s' $i); \
771 cat $i; \
772 done | md5sum
773 c0788982781981c96a0d81465fec7192 -
774 </pre></nowiki>
775
776 Minor caveats: the above demonstration will work only when none of the
777 filenames in the manifest are "fossilized" (encoded) because they contain
778 spaces. In that case the shell-generated hash would differ because the
779 <tt>stat</tt> calls will fail to find such files (which are output in encoded
780 form here). That approach also won't work for delta manifests. Calculating
781 the R-card for delta manifests requires traversing both the delta and its baseline in
782 lexical order of the files, prefering the delta's copy if both contain
783 a given file.
784

Keyboard Shortcuts

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