Fossil SCM

Merge from trunk.

dg 2013-09-04 20:44 dg-codenames merge
Commit 15090b9450aa34e93ae4bb999da915597d7dc2a4
+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
+18 -19
--- 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,11 +977,11 @@
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
--- 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,11 +977,11 @@
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
--- 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,11 +977,11 @@
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
+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
+1 -1
--- src/cgi.c
+++ src/cgi.c
@@ -863,12 +863,12 @@
863863
g.zIpAddr = mprintf("%s", z);
864864
}
865865
866866
len = atoi(PD("CONTENT_LENGTH", "0"));
867867
g.zContentType = zType = P("CONTENT_TYPE");
868
+ blob_zero(&g.cgiIn);
868869
if( len>0 && zType ){
869
- blob_zero(&g.cgiIn);
870870
if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
871871
|| strncmp(zType,"multipart/form-data",19)==0 ){
872872
z = fossil_malloc( len+1 );
873873
len = fread(z, 1, len, g.httpIn);
874874
z[len] = 0;
875875
--- src/cgi.c
+++ src/cgi.c
@@ -863,12 +863,12 @@
863 g.zIpAddr = mprintf("%s", z);
864 }
865
866 len = atoi(PD("CONTENT_LENGTH", "0"));
867 g.zContentType = zType = P("CONTENT_TYPE");
 
868 if( len>0 && zType ){
869 blob_zero(&g.cgiIn);
870 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
871 || strncmp(zType,"multipart/form-data",19)==0 ){
872 z = fossil_malloc( len+1 );
873 len = fread(z, 1, len, g.httpIn);
874 z[len] = 0;
875
--- src/cgi.c
+++ src/cgi.c
@@ -863,12 +863,12 @@
863 g.zIpAddr = mprintf("%s", z);
864 }
865
866 len = atoi(PD("CONTENT_LENGTH", "0"));
867 g.zContentType = zType = P("CONTENT_TYPE");
868 blob_zero(&g.cgiIn);
869 if( len>0 && zType ){
 
870 if( fossil_strcmp(zType,"application/x-www-form-urlencoded")==0
871 || strncmp(zType,"multipart/form-data",19)==0 ){
872 z = fossil_malloc( len+1 );
873 len = fread(z, 1, len, g.httpIn);
874 z[len] = 0;
875
+8 -2
--- src/checkin.c
+++ src/checkin.c
@@ -880,10 +880,11 @@
880880
*/
881881
struct CheckinInfo {
882882
Blob *pComment; /* Check-in comment text */
883883
const char *zMimetype; /* Mimetype of check-in command. May be NULL */
884884
int verifyDate; /* Verify that child is younger */
885
+ int closeFlag; /* Close the branch being committed */
885886
Blob *pCksum; /* Repository checksum. May be 0 */
886887
const char *zDateOvrd; /* Date override. If 0 then use 'now' */
887888
const char *zUserOvrd; /* User override. If 0 then use g.zLogin */
888889
const char *zBranch; /* Branch name. May be 0 */
889890
const char *zColor; /* One-time background color. May be 0 */
@@ -1059,10 +1060,13 @@
10591060
}
10601061
if( zColor && zColor[0] ){
10611062
/* One-time background color */
10621063
blob_appendf(pOut, "T +bgcolor * %F\n", zColor);
10631064
}
1065
+ if( p->closeFlag ){
1066
+ blob_appendf(pOut, "T +closed *\n");
1067
+ }
10641068
db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
10651069
" WHERE id=-4 ORDER BY 1");
10661070
while( db_step(&q)==SQLITE_ROW ){
10671071
const char *zIntegrateUuid = db_column_text(&q, 0);
10681072
int rid = db_column_int(&q, 1);
@@ -1300,10 +1304,11 @@
13001304
** --allow-older allow a commit older than its ancestor
13011305
** --baseline use a baseline manifest in the commit process
13021306
** --bgcolor COLOR apply COLOR to this one check-in only
13031307
** --branch NEW-BRANCH-NAME check in to this new branch
13041308
** --branchcolor COLOR apply given COLOR to the branch
1309
+** --close close the branch being committed
13051310
** --delta use a delta manifest in the commit process
13061311
** -m|--comment COMMENT-TEXT use COMMENT-TEXT as commit comment
13071312
** -M|--message-file FILE read the commit comment from given file
13081313
** --mimetype MIMETYPE mimetype of check-in comment
13091314
** -n|--dry-run If given, display instead of run actions
@@ -1372,10 +1377,11 @@
13721377
allowOlder = find_option("allow-older",0,0)!=0;
13731378
noWarningFlag = find_option("no-warnings", 0, 0)!=0;
13741379
sCiInfo.zBranch = find_option("branch","b",1);
13751380
sCiInfo.zColor = find_option("bgcolor",0,1);
13761381
sCiInfo.zBrClr = find_option("branchcolor",0,1);
1382
+ sCiInfo.closeFlag = find_option("close",0,0)!=0;
13771383
sCiInfo.zMimetype = find_option("mimetype",0,1);
13781384
while( (zTag = find_option("tag",0,1))!=0 ){
13791385
if( zTag[0]==0 ) continue;
13801386
sCiInfo.azTag = fossil_realloc((void*)sCiInfo.azTag, sizeof(char*)*(nTag+2));
13811387
sCiInfo.azTag[nTag++] = zTag;
@@ -1690,11 +1696,11 @@
16901696
manifest = delta;
16911697
}else{
16921698
blob_reset(&delta);
16931699
}
16941700
}else if( forceDelta ){
1695
- fossil_panic("unable to find a baseline-manifest for the delta");
1701
+ fossil_fatal("unable to find a baseline-manifest for the delta");
16961702
}
16971703
}
16981704
if( !noSign && !g.markPrivate && clearsign(&manifest, &manifest) ){
16991705
blob_zero(&ans);
17001706
prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
@@ -1718,11 +1724,11 @@
17181724
free(zManifestFile);
17191725
}
17201726
17211727
nvid = content_put(&manifest);
17221728
if( nvid==0 ){
1723
- fossil_panic("trouble committing manifest: %s", g.zErrMsg);
1729
+ fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
17241730
}
17251731
db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nvid);
17261732
manifest_crosslink(nvid, &manifest);
17271733
assert( blob_is_reset(&manifest) );
17281734
content_deltify(vid, nvid, 0);
17291735
--- src/checkin.c
+++ src/checkin.c
@@ -880,10 +880,11 @@
880 */
881 struct CheckinInfo {
882 Blob *pComment; /* Check-in comment text */
883 const char *zMimetype; /* Mimetype of check-in command. May be NULL */
884 int verifyDate; /* Verify that child is younger */
 
885 Blob *pCksum; /* Repository checksum. May be 0 */
886 const char *zDateOvrd; /* Date override. If 0 then use 'now' */
887 const char *zUserOvrd; /* User override. If 0 then use g.zLogin */
888 const char *zBranch; /* Branch name. May be 0 */
889 const char *zColor; /* One-time background color. May be 0 */
@@ -1059,10 +1060,13 @@
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);
@@ -1300,10 +1304,11 @@
1300 ** --allow-older allow a commit older than its ancestor
1301 ** --baseline use a baseline manifest in the commit process
1302 ** --bgcolor COLOR apply COLOR to this one check-in only
1303 ** --branch NEW-BRANCH-NAME check in to this new branch
1304 ** --branchcolor COLOR apply given COLOR to the branch
 
1305 ** --delta use a delta manifest in the commit process
1306 ** -m|--comment COMMENT-TEXT use COMMENT-TEXT as commit comment
1307 ** -M|--message-file FILE read the commit comment from given file
1308 ** --mimetype MIMETYPE mimetype of check-in comment
1309 ** -n|--dry-run If given, display instead of run actions
@@ -1372,10 +1377,11 @@
1372 allowOlder = find_option("allow-older",0,0)!=0;
1373 noWarningFlag = find_option("no-warnings", 0, 0)!=0;
1374 sCiInfo.zBranch = find_option("branch","b",1);
1375 sCiInfo.zColor = find_option("bgcolor",0,1);
1376 sCiInfo.zBrClr = find_option("branchcolor",0,1);
 
1377 sCiInfo.zMimetype = find_option("mimetype",0,1);
1378 while( (zTag = find_option("tag",0,1))!=0 ){
1379 if( zTag[0]==0 ) continue;
1380 sCiInfo.azTag = fossil_realloc((void*)sCiInfo.azTag, sizeof(char*)*(nTag+2));
1381 sCiInfo.azTag[nTag++] = zTag;
@@ -1690,11 +1696,11 @@
1690 manifest = delta;
1691 }else{
1692 blob_reset(&delta);
1693 }
1694 }else if( forceDelta ){
1695 fossil_panic("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);
@@ -1718,11 +1724,11 @@
1718 free(zManifestFile);
1719 }
1720
1721 nvid = content_put(&manifest);
1722 if( nvid==0 ){
1723 fossil_panic("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
--- src/checkin.c
+++ src/checkin.c
@@ -880,10 +880,11 @@
880 */
881 struct CheckinInfo {
882 Blob *pComment; /* Check-in comment text */
883 const char *zMimetype; /* Mimetype of check-in command. May be NULL */
884 int verifyDate; /* Verify that child is younger */
885 int closeFlag; /* Close the branch being committed */
886 Blob *pCksum; /* Repository checksum. May be 0 */
887 const char *zDateOvrd; /* Date override. If 0 then use 'now' */
888 const char *zUserOvrd; /* User override. If 0 then use g.zLogin */
889 const char *zBranch; /* Branch name. May be 0 */
890 const char *zColor; /* One-time background color. May be 0 */
@@ -1059,10 +1060,13 @@
1060 }
1061 if( zColor && zColor[0] ){
1062 /* One-time background color */
1063 blob_appendf(pOut, "T +bgcolor * %F\n", zColor);
1064 }
1065 if( p->closeFlag ){
1066 blob_appendf(pOut, "T +closed *\n");
1067 }
1068 db_prepare(&q, "SELECT uuid,merge FROM vmerge JOIN blob ON merge=rid"
1069 " WHERE id=-4 ORDER BY 1");
1070 while( db_step(&q)==SQLITE_ROW ){
1071 const char *zIntegrateUuid = db_column_text(&q, 0);
1072 int rid = db_column_int(&q, 1);
@@ -1300,10 +1304,11 @@
1304 ** --allow-older allow a commit older than its ancestor
1305 ** --baseline use a baseline manifest in the commit process
1306 ** --bgcolor COLOR apply COLOR to this one check-in only
1307 ** --branch NEW-BRANCH-NAME check in to this new branch
1308 ** --branchcolor COLOR apply given COLOR to the branch
1309 ** --close close the branch being committed
1310 ** --delta use a delta manifest in the commit process
1311 ** -m|--comment COMMENT-TEXT use COMMENT-TEXT as commit comment
1312 ** -M|--message-file FILE read the commit comment from given file
1313 ** --mimetype MIMETYPE mimetype of check-in comment
1314 ** -n|--dry-run If given, display instead of run actions
@@ -1372,10 +1377,11 @@
1377 allowOlder = find_option("allow-older",0,0)!=0;
1378 noWarningFlag = find_option("no-warnings", 0, 0)!=0;
1379 sCiInfo.zBranch = find_option("branch","b",1);
1380 sCiInfo.zColor = find_option("bgcolor",0,1);
1381 sCiInfo.zBrClr = find_option("branchcolor",0,1);
1382 sCiInfo.closeFlag = find_option("close",0,0)!=0;
1383 sCiInfo.zMimetype = find_option("mimetype",0,1);
1384 while( (zTag = find_option("tag",0,1))!=0 ){
1385 if( zTag[0]==0 ) continue;
1386 sCiInfo.azTag = fossil_realloc((void*)sCiInfo.azTag, sizeof(char*)*(nTag+2));
1387 sCiInfo.azTag[nTag++] = zTag;
@@ -1690,11 +1696,11 @@
1696 manifest = delta;
1697 }else{
1698 blob_reset(&delta);
1699 }
1700 }else if( forceDelta ){
1701 fossil_fatal("unable to find a baseline-manifest for the delta");
1702 }
1703 }
1704 if( !noSign && !g.markPrivate && clearsign(&manifest, &manifest) ){
1705 blob_zero(&ans);
1706 prompt_user("unable to sign manifest. continue (y/N)? ", &ans);
@@ -1718,11 +1724,11 @@
1724 free(zManifestFile);
1725 }
1726
1727 nvid = content_put(&manifest);
1728 if( nvid==0 ){
1729 fossil_fatal("trouble committing manifest: %s", g.zErrMsg);
1730 }
1731 db_multi_exec("INSERT OR IGNORE INTO unsent VALUES(%d)", nvid);
1732 manifest_crosslink(nvid, &manifest);
1733 assert( blob_is_reset(&manifest) );
1734 content_deltify(vid, nvid, 0);
1735
+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
@@ -123,11 +123,11 @@
123123
if( g.argc < 4 ){
124124
usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
125125
}
126126
db_open_config(0);
127127
if( file_size(g.argv[3])>0 ){
128
- fossil_panic("file already exists: %s", g.argv[3]);
128
+ fossil_fatal("file already exists: %s", g.argv[3]);
129129
}
130130
131131
zDefaultUser = find_option("admin-user","A",1);
132132
133133
url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
134134
--- src/clone.c
+++ src/clone.c
@@ -123,11 +123,11 @@
123 if( g.argc < 4 ){
124 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
125 }
126 db_open_config(0);
127 if( file_size(g.argv[3])>0 ){
128 fossil_panic("file already exists: %s", g.argv[3]);
129 }
130
131 zDefaultUser = find_option("admin-user","A",1);
132
133 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
134
--- src/clone.c
+++ src/clone.c
@@ -123,11 +123,11 @@
123 if( g.argc < 4 ){
124 usage("?OPTIONS? FILE-OR-URL NEW-REPOSITORY");
125 }
126 db_open_config(0);
127 if( file_size(g.argv[3])>0 ){
128 fossil_fatal("file already exists: %s", g.argv[3]);
129 }
130
131 zDefaultUser = find_option("admin-user","A",1);
132
133 url_parse(g.argv[2], URL_PROMPT_PW|URL_ASK_REMEMBER_PW);
134
+1 -1
--- src/db.c
+++ src/db.c
@@ -1994,11 +1994,11 @@
19941994
allowNested = find_option("nested",0,0)!=0;
19951995
if( g.argc!=3 && g.argc!=4 ){
19961996
usage("REPOSITORY-FILENAME ?VERSION?");
19971997
}
19981998
if( !allowNested && db_open_local(0) ){
1999
- 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);
20002000
}
20012001
db_open_repository(g.argv[2]);
20022002
#if defined(_WIN32) || defined(__CYGWIN__)
20032003
# define LOCALDB_NAME "./_FOSSIL_"
20042004
#else
20052005
--- src/db.c
+++ src/db.c
@@ -1994,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_panic("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
--- src/db.c
+++ src/db.c
@@ -1994,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/db.c
+++ src/db.c
@@ -1994,11 +1994,11 @@
19941994
allowNested = find_option("nested",0,0)!=0;
19951995
if( g.argc!=3 && g.argc!=4 ){
19961996
usage("REPOSITORY-FILENAME ?VERSION?");
19971997
}
19981998
if( !allowNested && db_open_local(0) ){
1999
- 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);
20002000
}
20012001
db_open_repository(g.argv[2]);
20022002
#if defined(_WIN32) || defined(__CYGWIN__)
20032003
# define LOCALDB_NAME "./_FOSSIL_"
20042004
#else
20052005
--- src/db.c
+++ src/db.c
@@ -1994,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_panic("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
--- src/db.c
+++ src/db.c
@@ -1994,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 -7
--- src/diff.c
+++ src/diff.c
@@ -214,21 +214,18 @@
214214
int html, /* True if generating HTML. False for plain text */
215215
ReCompiled *pRe /* Colorize only if line matches this Regex */
216216
){
217217
blob_append(pOut, &cPrefix, 1);
218218
if( html ){
219
- char *zHtml;
220219
if( pRe && re_dline_match(pRe, pLine, 1)==0 ){
221220
cPrefix = ' ';
222221
}else if( cPrefix=='+' ){
223222
blob_append(pOut, "<span class=\"diffadd\">", -1);
224223
}else if( cPrefix=='-' ){
225224
blob_append(pOut, "<span class=\"diffrm\">", -1);
226225
}
227
- zHtml = htmlize(pLine->z, (pLine->h & LENGTH_MASK));
228
- blob_append(pOut, zHtml, -1);
229
- fossil_free(zHtml);
226
+ htmlize_to_blob(pOut, pLine->z, (pLine->h & LENGTH_MASK));
230227
if( cPrefix!=' ' ){
231228
blob_append(pOut, "</span>", -1);
232229
}
233230
}else{
234231
blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
@@ -2046,14 +2043,14 @@
20462043
int cnt = 0; /* Number of versions examined */
20472044
20482045
/* Initialize the annotation */
20492046
rid = db_int(0, "SELECT fid FROM mlink WHERE mid=%d AND fnid=%d",mid,fnid);
20502047
if( rid==0 ){
2051
- fossil_panic("file #%d is unchanged in manifest #%d", fnid, mid);
2048
+ fossil_fatal("file #%d is unchanged in manifest #%d", fnid, mid);
20522049
}
20532050
if( !content_get(rid, &toAnnotate) ){
2054
- fossil_panic("unable to retrieve content of artifact #%d", rid);
2051
+ fossil_fatal("unable to retrieve content of artifact #%d", rid);
20552052
}
20562053
if( iLimit<=0 ) iLimit = 1000000000;
20572054
annotation_start(p, &toAnnotate);
20582055
db_begin_transaction();
20592056
db_multi_exec(
@@ -2329,11 +2326,11 @@
23292326
mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
23302327
" WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
23312328
" ORDER BY ancestor.generation ASC LIMIT 1",
23322329
fid, fnid);
23332330
if( mid==0 ){
2334
- fossil_panic("unable to find manifest");
2331
+ fossil_fatal("unable to find manifest");
23352332
}
23362333
annFlags |= ANN_FILE_ANCEST;
23372334
annotate_file(&ann, fnid, mid, iLimit, annFlags);
23382335
if( showLog ){
23392336
struct AnnVers *p;
23402337
--- src/diff.c
+++ src/diff.c
@@ -214,21 +214,18 @@
214 int html, /* True if generating HTML. False for plain text */
215 ReCompiled *pRe /* Colorize only if line matches this Regex */
216 ){
217 blob_append(pOut, &cPrefix, 1);
218 if( html ){
219 char *zHtml;
220 if( pRe && re_dline_match(pRe, pLine, 1)==0 ){
221 cPrefix = ' ';
222 }else if( cPrefix=='+' ){
223 blob_append(pOut, "<span class=\"diffadd\">", -1);
224 }else if( cPrefix=='-' ){
225 blob_append(pOut, "<span class=\"diffrm\">", -1);
226 }
227 zHtml = htmlize(pLine->z, (pLine->h & LENGTH_MASK));
228 blob_append(pOut, zHtml, -1);
229 fossil_free(zHtml);
230 if( cPrefix!=' ' ){
231 blob_append(pOut, "</span>", -1);
232 }
233 }else{
234 blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
@@ -2046,14 +2043,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(
@@ -2329,11 +2326,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_panic("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
--- src/diff.c
+++ src/diff.c
@@ -214,21 +214,18 @@
214 int html, /* True if generating HTML. False for plain text */
215 ReCompiled *pRe /* Colorize only if line matches this Regex */
216 ){
217 blob_append(pOut, &cPrefix, 1);
218 if( html ){
 
219 if( pRe && re_dline_match(pRe, pLine, 1)==0 ){
220 cPrefix = ' ';
221 }else if( cPrefix=='+' ){
222 blob_append(pOut, "<span class=\"diffadd\">", -1);
223 }else if( cPrefix=='-' ){
224 blob_append(pOut, "<span class=\"diffrm\">", -1);
225 }
226 htmlize_to_blob(pOut, pLine->z, (pLine->h & LENGTH_MASK));
 
 
227 if( cPrefix!=' ' ){
228 blob_append(pOut, "</span>", -1);
229 }
230 }else{
231 blob_append(pOut, pLine->z, pLine->h & LENGTH_MASK);
@@ -2046,14 +2043,14 @@
2043 int cnt = 0; /* Number of versions examined */
2044
2045 /* Initialize the annotation */
2046 rid = db_int(0, "SELECT fid FROM mlink WHERE mid=%d AND fnid=%d",mid,fnid);
2047 if( rid==0 ){
2048 fossil_fatal("file #%d is unchanged in manifest #%d", fnid, mid);
2049 }
2050 if( !content_get(rid, &toAnnotate) ){
2051 fossil_fatal("unable to retrieve content of artifact #%d", rid);
2052 }
2053 if( iLimit<=0 ) iLimit = 1000000000;
2054 annotation_start(p, &toAnnotate);
2055 db_begin_transaction();
2056 db_multi_exec(
@@ -2329,11 +2326,11 @@
2326 mid = db_int(0, "SELECT mlink.mid FROM mlink, ancestor "
2327 " WHERE mlink.fid=%d AND mlink.fnid=%d AND mlink.mid=ancestor.rid"
2328 " ORDER BY ancestor.generation ASC LIMIT 1",
2329 fid, fnid);
2330 if( mid==0 ){
2331 fossil_fatal("unable to find manifest");
2332 }
2333 annFlags |= ANN_FILE_ANCEST;
2334 annotate_file(&ann, fnid, mid, iLimit, annFlags);
2335 if( showLog ){
2336 struct AnnVers *p;
2337
--- src/encode.c
+++ src/encode.c
@@ -161,10 +161,11 @@
161161
zOut[i++] = "0123456789ABCDEF"[c&0xf];
162162
}
163163
zIn++;
164164
}
165165
zOut[i] = 0;
166
+#undef IsSafeChar
166167
return zOut;
167168
}
168169
169170
/*
170171
** Convert the input string into a form that is suitable for use as
171172
--- src/encode.c
+++ src/encode.c
@@ -161,10 +161,11 @@
161 zOut[i++] = "0123456789ABCDEF"[c&0xf];
162 }
163 zIn++;
164 }
165 zOut[i] = 0;
 
166 return zOut;
167 }
168
169 /*
170 ** Convert the input string into a form that is suitable for use as
171
--- src/encode.c
+++ src/encode.c
@@ -161,10 +161,11 @@
161 zOut[i++] = "0123456789ABCDEF"[c&0xf];
162 }
163 zIn++;
164 }
165 zOut[i] = 0;
166 #undef IsSafeChar
167 return zOut;
168 }
169
170 /*
171 ** Convert the input string into a form that is suitable for use as
172
+1 -1
--- 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{
126126
--- 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{
126
--- 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{
126
+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
+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
+33 -11
--- 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 */
@@ -372,11 +373,11 @@
372373
unsigned int i, j, k; /* Loop counters */
373374
int n; /* Number of bytes in one line */
374375
char *z; /* General use string pointer */
375376
char **newArgv; /* New expanded g.argv under construction */
376377
char const * zFileName; /* input file name */
377
- FILE * zInFile; /* input FILE */
378
+ FILE *inFile; /* input FILE */
378379
#if defined(_WIN32)
379380
wchar_t buf[MAX_PATH];
380381
#endif
381382
382383
g.argc = argc;
@@ -402,21 +403,21 @@
402403
if( fossil_strcmp(z, "args")==0 ) break;
403404
}
404405
if( i>=g.argc-1 ) return;
405406
406407
zFileName = g.argv[i+1];
407
- zInFile = (0==strcmp("-",zFileName))
408
+ inFile = (0==strcmp("-",zFileName))
408409
? stdin
409410
: fossil_fopen(zFileName,"rb");
410
- if(!zInFile){
411
- fossil_panic("Cannot open -args file [%s]", zFileName);
411
+ if(!inFile){
412
+ fossil_fatal("Cannot open -args file [%s]", zFileName);
412413
}else{
413
- blob_read_from_channel(&file, zInFile, -1);
414
- if(stdin != zInFile){
415
- fclose(zInFile);
414
+ blob_read_from_channel(&file, inFile, -1);
415
+ if(stdin != inFile){
416
+ fclose(inFile);
416417
}
417
- zInFile = NULL;
418
+ inFile = NULL;
418419
}
419420
blob_to_utf8_no_bom(&file, 1);
420421
z = blob_str(&file);
421422
for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
422423
newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -512,10 +513,11 @@
512513
#ifdef __APPLE__
513514
/* Disable the file alias warning on apple products because Time Machine
514515
** creates lots of aliases and the warning alarms people. */
515516
if( iCode==SQLITE_WARNING ) return;
516517
#endif
518
+ if( iCode==SQLITE_SCHEMA ) return;
517519
fossil_warning("%s: %s", sqlite_error_code_name(iCode), zErrmsg);
518520
}
519521
520522
/*
521523
** This procedure runs first.
@@ -586,10 +588,11 @@
586588
if( g.fSqlTrace ) g.fSqlStats = 1;
587589
g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
588590
g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
589591
g.zLogin = find_option("user", "U", 1);
590592
g.zSSLIdentity = find_option("ssl-identity", 0, 1);
593
+ g.zErrlog = find_option("errorlog", 0, 1);
591594
if( find_option("utc",0,0) ) g.fTimeFormat = 1;
592595
if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
593596
if( zChdir && file_chdir(zChdir, 0) ){
594597
fossil_fatal("unable to change directories to %s", zChdir);
595598
}
@@ -605,10 +608,14 @@
605608
g.argc++;
606609
g.argv = zNewArgv;
607610
}
608611
zCmdName = g.argv[1];
609612
}
613
+#ifndef _WIN32
614
+ if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
615
+ /* if( is_valid_fd(3) ) fossil_warning("file descriptor 3 is open"); */
616
+#endif
610617
rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
611618
if( rc==1 ){
612619
fossil_fatal("%s: unknown command: %s\n"
613620
"%s: use \"help\" for more information\n",
614621
g.argv[0], zCmdName, g.argv[0]);
@@ -1114,11 +1121,11 @@
11141121
fossil_fatal("unable to chroot into %s", zDir);
11151122
}
11161123
zRepo = "/";
11171124
}else{
11181125
for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1119
- if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1126
+ if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
11201127
if( i>0 ){
11211128
zDir[i] = 0;
11221129
if( file_chdir(zDir, 1) ){
11231130
fossil_fatal("unable to chroot into %s", zDir);
11241131
}
@@ -1504,10 +1511,14 @@
15041511
if( blob_buffer(&key)[0]=='#' ) continue;
15051512
if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
15061513
g.fDebug = fossil_fopen(blob_str(&value), "ab");
15071514
blob_reset(&value);
15081515
continue;
1516
+ }
1517
+ if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1518
+ g.zErrlog = mprintf("%s", blob_str(&value));
1519
+ continue;
15091520
}
15101521
if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
15111522
cgi_setenv("HOME", blob_str(&value));
15121523
blob_reset(&value);
15131524
continue;
@@ -1849,13 +1860,24 @@
18491860
if( zBrowser==0 ){
18501861
static const char *const azBrowserProg[] =
18511862
{ "xdg-open", "gnome-open", "firefox", "google-chrome" };
18521863
int i;
18531864
#if defined(__CYGWIN__)
1854
- const char *path = fossil_getenv("PROGRAMFILES");
1865
+ const char *path = fossil_getenv("ProgramFiles(x86)");
1866
+ if( !path ){
1867
+ path = fossil_getenv("PROGRAMFILES");
1868
+ }
18551869
path = fossil_utf8_to_filename(path);
1856
- zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
1870
+ zBrowser = mprintf("%s/Google/Chrome/Application/chrome.exe", path);
1871
+ if( file_access(zBrowser, X_OK) ){
1872
+ zBrowser = mprintf("%s/Mozilla Firefox/firefox.exe", path);
1873
+ }
1874
+ if( file_access(zBrowser, X_OK) ){
1875
+ path = fossil_utf8_to_filename(fossil_getenv("PROGRAMFILES"));
1876
+ zBrowser = mprintf("%s/Internet Explorer/iexplore.exe", path);
1877
+ }
1878
+ zBrowser = mprintf("\"%s\"", zBrowser);
18571879
#else
18581880
zBrowser = "echo";
18591881
#endif
18601882
for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
18611883
if( binaryOnPath(azBrowserProg[i]) ){
18621884
--- 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 */
@@ -372,11 +373,11 @@
372 unsigned int i, j, k; /* Loop counters */
373 int n; /* Number of bytes in one line */
374 char *z; /* General use string pointer */
375 char **newArgv; /* New expanded g.argv under construction */
376 char const * zFileName; /* input file name */
377 FILE * zInFile; /* input FILE */
378 #if defined(_WIN32)
379 wchar_t buf[MAX_PATH];
380 #endif
381
382 g.argc = argc;
@@ -402,21 +403,21 @@
402 if( fossil_strcmp(z, "args")==0 ) break;
403 }
404 if( i>=g.argc-1 ) return;
405
406 zFileName = g.argv[i+1];
407 zInFile = (0==strcmp("-",zFileName))
408 ? stdin
409 : fossil_fopen(zFileName,"rb");
410 if(!zInFile){
411 fossil_panic("Cannot open -args file [%s]", zFileName);
412 }else{
413 blob_read_from_channel(&file, zInFile, -1);
414 if(stdin != zInFile){
415 fclose(zInFile);
416 }
417 zInFile = NULL;
418 }
419 blob_to_utf8_no_bom(&file, 1);
420 z = blob_str(&file);
421 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
422 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -512,10 +513,11 @@
512 #ifdef __APPLE__
513 /* Disable the file alias warning on apple products because Time Machine
514 ** creates lots of aliases and the warning alarms people. */
515 if( iCode==SQLITE_WARNING ) return;
516 #endif
 
517 fossil_warning("%s: %s", sqlite_error_code_name(iCode), zErrmsg);
518 }
519
520 /*
521 ** This procedure runs first.
@@ -586,10 +588,11 @@
586 if( g.fSqlTrace ) g.fSqlStats = 1;
587 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
588 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
589 g.zLogin = find_option("user", "U", 1);
590 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
 
591 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
592 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
593 if( zChdir && file_chdir(zChdir, 0) ){
594 fossil_fatal("unable to change directories to %s", zChdir);
595 }
@@ -605,10 +608,14 @@
605 g.argc++;
606 g.argv = zNewArgv;
607 }
608 zCmdName = g.argv[1];
609 }
 
 
 
 
610 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
611 if( rc==1 ){
612 fossil_fatal("%s: unknown command: %s\n"
613 "%s: use \"help\" for more information\n",
614 g.argv[0], zCmdName, g.argv[0]);
@@ -1114,11 +1121,11 @@
1114 fossil_fatal("unable to chroot into %s", zDir);
1115 }
1116 zRepo = "/";
1117 }else{
1118 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1119 if( zDir[i]!='/' ) fossil_panic("bad repository name: %s", zRepo);
1120 if( i>0 ){
1121 zDir[i] = 0;
1122 if( file_chdir(zDir, 1) ){
1123 fossil_fatal("unable to chroot into %s", zDir);
1124 }
@@ -1504,10 +1511,14 @@
1504 if( blob_buffer(&key)[0]=='#' ) continue;
1505 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1506 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1507 blob_reset(&value);
1508 continue;
 
 
 
 
1509 }
1510 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1511 cgi_setenv("HOME", blob_str(&value));
1512 blob_reset(&value);
1513 continue;
@@ -1849,13 +1860,24 @@
1849 if( zBrowser==0 ){
1850 static const char *const azBrowserProg[] =
1851 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1852 int i;
1853 #if defined(__CYGWIN__)
1854 const char *path = fossil_getenv("PROGRAMFILES");
 
 
 
1855 path = fossil_utf8_to_filename(path);
1856 zBrowser = mprintf("\"%s/Internet Explorer/iexplore.exe\"", path);
 
 
 
 
 
 
 
 
1857 #else
1858 zBrowser = "echo";
1859 #endif
1860 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1861 if( binaryOnPath(azBrowserProg[i]) ){
1862
--- 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 */
@@ -372,11 +373,11 @@
373 unsigned int i, j, k; /* Loop counters */
374 int n; /* Number of bytes in one line */
375 char *z; /* General use string pointer */
376 char **newArgv; /* New expanded g.argv under construction */
377 char const * zFileName; /* input file name */
378 FILE *inFile; /* input FILE */
379 #if defined(_WIN32)
380 wchar_t buf[MAX_PATH];
381 #endif
382
383 g.argc = argc;
@@ -402,21 +403,21 @@
403 if( fossil_strcmp(z, "args")==0 ) break;
404 }
405 if( i>=g.argc-1 ) return;
406
407 zFileName = g.argv[i+1];
408 inFile = (0==strcmp("-",zFileName))
409 ? stdin
410 : fossil_fopen(zFileName,"rb");
411 if(!inFile){
412 fossil_fatal("Cannot open -args file [%s]", zFileName);
413 }else{
414 blob_read_from_channel(&file, inFile, -1);
415 if(stdin != inFile){
416 fclose(inFile);
417 }
418 inFile = NULL;
419 }
420 blob_to_utf8_no_bom(&file, 1);
421 z = blob_str(&file);
422 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
423 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
@@ -512,10 +513,11 @@
513 #ifdef __APPLE__
514 /* Disable the file alias warning on apple products because Time Machine
515 ** creates lots of aliases and the warning alarms people. */
516 if( iCode==SQLITE_WARNING ) return;
517 #endif
518 if( iCode==SQLITE_SCHEMA ) return;
519 fossil_warning("%s: %s", sqlite_error_code_name(iCode), zErrmsg);
520 }
521
522 /*
523 ** This procedure runs first.
@@ -586,10 +588,11 @@
588 if( g.fSqlTrace ) g.fSqlStats = 1;
589 g.fSqlPrint = find_option("sqlprint", 0, 0)!=0;
590 g.fHttpTrace = find_option("httptrace", 0, 0)!=0;
591 g.zLogin = find_option("user", "U", 1);
592 g.zSSLIdentity = find_option("ssl-identity", 0, 1);
593 g.zErrlog = find_option("errorlog", 0, 1);
594 if( find_option("utc",0,0) ) g.fTimeFormat = 1;
595 if( find_option("localtime",0,0) ) g.fTimeFormat = 2;
596 if( zChdir && file_chdir(zChdir, 0) ){
597 fossil_fatal("unable to change directories to %s", zChdir);
598 }
@@ -605,10 +608,14 @@
608 g.argc++;
609 g.argv = zNewArgv;
610 }
611 zCmdName = g.argv[1];
612 }
613 #ifndef _WIN32
614 if( !is_valid_fd(2) ) fossil_panic("file descriptor 2 not open");
615 /* if( is_valid_fd(3) ) fossil_warning("file descriptor 3 is open"); */
616 #endif
617 rc = name_search(zCmdName, aCommand, count(aCommand), &idx);
618 if( rc==1 ){
619 fossil_fatal("%s: unknown command: %s\n"
620 "%s: use \"help\" for more information\n",
621 g.argv[0], zCmdName, g.argv[0]);
@@ -1114,11 +1121,11 @@
1121 fossil_fatal("unable to chroot into %s", zDir);
1122 }
1123 zRepo = "/";
1124 }else{
1125 for(i=strlen(zDir)-1; i>0 && zDir[i]!='/'; i--){}
1126 if( zDir[i]!='/' ) fossil_fatal("bad repository name: %s", zRepo);
1127 if( i>0 ){
1128 zDir[i] = 0;
1129 if( file_chdir(zDir, 1) ){
1130 fossil_fatal("unable to chroot into %s", zDir);
1131 }
@@ -1504,10 +1511,14 @@
1511 if( blob_buffer(&key)[0]=='#' ) continue;
1512 if( blob_eq(&key, "debug:") && blob_token(&line, &value) ){
1513 g.fDebug = fossil_fopen(blob_str(&value), "ab");
1514 blob_reset(&value);
1515 continue;
1516 }
1517 if( blob_eq(&key, "errorlog:") && blob_token(&line, &value) ){
1518 g.zErrlog = mprintf("%s", blob_str(&value));
1519 continue;
1520 }
1521 if( blob_eq(&key, "HOME:") && blob_token(&line, &value) ){
1522 cgi_setenv("HOME", blob_str(&value));
1523 blob_reset(&value);
1524 continue;
@@ -1849,13 +1860,24 @@
1860 if( zBrowser==0 ){
1861 static const char *const azBrowserProg[] =
1862 { "xdg-open", "gnome-open", "firefox", "google-chrome" };
1863 int i;
1864 #if defined(__CYGWIN__)
1865 const char *path = fossil_getenv("ProgramFiles(x86)");
1866 if( !path ){
1867 path = fossil_getenv("PROGRAMFILES");
1868 }
1869 path = fossil_utf8_to_filename(path);
1870 zBrowser = mprintf("%s/Google/Chrome/Application/chrome.exe", path);
1871 if( file_access(zBrowser, X_OK) ){
1872 zBrowser = mprintf("%s/Mozilla Firefox/firefox.exe", path);
1873 }
1874 if( file_access(zBrowser, X_OK) ){
1875 path = fossil_utf8_to_filename(fossil_getenv("PROGRAMFILES"));
1876 zBrowser = mprintf("%s/Internet Explorer/iexplore.exe", path);
1877 }
1878 zBrowser = mprintf("\"%s\"", zBrowser);
1879 #else
1880 zBrowser = "echo";
1881 #endif
1882 for(i=0; i<sizeof(azBrowserProg)/sizeof(azBrowserProg[0]); i++){
1883 if( binaryOnPath(azBrowserProg[i]) ){
1884
+22 -17
--- src/manifest.c
+++ src/manifest.c
@@ -469,12 +469,13 @@
469469
470470
/*
471471
** C <comment>
472472
**
473473
** Comment text is fossil-encoded. There may be no more than
474
- ** one C line. C lines are required for manifests and are
475
- ** disallowed on all other control files.
474
+ ** one C line. C lines are required for manifests, are optional
475
+ ** for Events and Attachments, and are disallowed on all other
476
+ ** control files.
476477
*/
477478
case 'C': {
478479
if( p->zComment!=0 ) SYNTAX("more than one C-card");
479480
p->zComment = next_token(&x, 0);
480481
if( p->zComment==0 ) SYNTAX("missing comment text on C-card");
@@ -663,11 +664,13 @@
663664
/*
664665
** P <uuid> ...
665666
**
666667
** Specify one or more other artifacts which are the parents of
667668
** this artifact. The first parent is the primary parent. All
668
- ** others are parents by merge.
669
+ ** others are parents by merge. Note that the initial empty
670
+ ** checkin historically has an empty P-card, so empty P-cards
671
+ ** must be accepted.
669672
*/
670673
case 'P': {
671674
while( (zUuid = next_token(&x, &sz))!=0 ){
672675
if( sz!=UUID_SIZE ) SYNTAX("wrong size UUID on P-card");
673676
if( !validate16(zUuid, UUID_SIZE) )SYNTAX("invalid UUID on P-card");
@@ -1955,46 +1958,42 @@
19551958
if( strcmp(zName, "*branch")==0 ){
19561959
blob_appendf(&comment,
19571960
" Move to branch [/timeline?r=%h&nd&dp=%S | %h].",
19581961
zValue, zUuid, zValue);
19591962
branchMove = 1;
1963
+ continue;
19601964
}else if( strcmp(zName, "*bgcolor")==0 ){
19611965
blob_appendf(&comment,
19621966
" Change branch background color to \"%h\".", zValue);
1967
+ continue;
19631968
}else if( strcmp(zName, "+bgcolor")==0 ){
19641969
blob_appendf(&comment,
19651970
" Change background color to \"%h\".", zValue);
1971
+ continue;
19661972
}else if( strcmp(zName, "-bgcolor")==0 ){
1967
- blob_appendf(&comment, " Cancel background color.");
1973
+ blob_appendf(&comment, " Cancel background color");
19681974
}else if( strcmp(zName, "+comment")==0 ){
19691975
blob_appendf(&comment, " Edit check-in comment.");
1976
+ continue;
19701977
}else if( strcmp(zName, "+user")==0 ){
19711978
blob_appendf(&comment, " Change user to \"%h\".", zValue);
1979
+ continue;
19721980
}else if( strcmp(zName, "+date")==0 ){
19731981
blob_appendf(&comment, " Timestamp %h.", zValue);
1982
+ continue;
19741983
}else if( memcmp(zName, "-sym-",5)==0 ){
1975
- if( !branchMove ) blob_appendf(&comment, " Cancel tag %h.", &zName[5]);
1984
+ if( !branchMove ) blob_appendf(&comment, " Cancel tag \"%h\"", &zName[5]);
19761985
}else if( memcmp(zName, "*sym-",5)==0 ){
19771986
if( !branchMove ){
1978
- blob_appendf(&comment, " Add propagating tag \"%h\".", &zName[5]);
1987
+ blob_appendf(&comment, " Add propagating tag \"%h\"", &zName[5]);
19791988
}
19801989
}else if( memcmp(zName, "+sym-",5)==0 ){
1981
- blob_appendf(&comment, " Add tag \"%h\".", &zName[5]);
1982
- }else if( memcmp(zName, "-sym-",5)==0 ){
1983
- blob_appendf(&comment, " Cancel tag \"%h\".", &zName[5]);
1990
+ blob_appendf(&comment, " Add tag \"%h\"", &zName[5]);
19841991
}else if( strcmp(zName, "+closed")==0 ){
19851992
blob_append(&comment, " Marked \"Closed\"", -1);
1986
- if( zValue && *zValue ){
1987
- blob_appendf(&comment, " with note \"%h\"", zValue);
1988
- }
1989
- blob_append(&comment, ".", 1);
19901993
}else if( strcmp(zName, "-closed")==0 ){
19911994
blob_append(&comment, " Removed the \"Closed\" mark", -1);
1992
- if( zValue && *zValue ){
1993
- blob_appendf(&comment, " with note \"%h\"", zValue);
1994
- }
1995
- blob_append(&comment, ".", 1);
19961995
}else {
19971996
if( zName[0]=='-' ){
19981997
blob_appendf(&comment, " Cancel \"%h\"", &zName[1]);
19991998
}else if( zName[0]=='+' ){
20001999
blob_appendf(&comment, " Add \"%h\"", &zName[1]);
@@ -2004,10 +2003,16 @@
20042003
if( zValue && zValue[0] ){
20052004
blob_appendf(&comment, " with value \"%h\".", zValue);
20062005
}else{
20072006
blob_appendf(&comment, ".");
20082007
}
2008
+ continue;
2009
+ }
2010
+ if( zValue && zValue[0] ){
2011
+ blob_appendf(&comment, " with note \"%h\".", zValue);
2012
+ }else{
2013
+ blob_appendf(&comment, ".");
20092014
}
20102015
}
20112016
/*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
20122017
if( blob_size(&comment)==0 ) blob_append(&comment, " ", 1);
20132018
db_multi_exec(
20142019
--- src/manifest.c
+++ src/manifest.c
@@ -469,12 +469,13 @@
469
470 /*
471 ** C <comment>
472 **
473 ** Comment text is fossil-encoded. There may be no more than
474 ** one C line. C lines are required for manifests and are
475 ** disallowed on all other control files.
 
476 */
477 case 'C': {
478 if( p->zComment!=0 ) SYNTAX("more than one C-card");
479 p->zComment = next_token(&x, 0);
480 if( p->zComment==0 ) SYNTAX("missing comment text on C-card");
@@ -663,11 +664,13 @@
663 /*
664 ** P <uuid> ...
665 **
666 ** Specify one or more other artifacts which are the parents of
667 ** this artifact. The first parent is the primary parent. All
668 ** others are parents by merge.
 
 
669 */
670 case 'P': {
671 while( (zUuid = next_token(&x, &sz))!=0 ){
672 if( sz!=UUID_SIZE ) SYNTAX("wrong size UUID on P-card");
673 if( !validate16(zUuid, UUID_SIZE) )SYNTAX("invalid UUID on P-card");
@@ -1955,46 +1958,42 @@
1955 if( strcmp(zName, "*branch")==0 ){
1956 blob_appendf(&comment,
1957 " Move to branch [/timeline?r=%h&nd&dp=%S | %h].",
1958 zValue, zUuid, zValue);
1959 branchMove = 1;
 
1960 }else if( strcmp(zName, "*bgcolor")==0 ){
1961 blob_appendf(&comment,
1962 " Change branch background color to \"%h\".", zValue);
 
1963 }else if( strcmp(zName, "+bgcolor")==0 ){
1964 blob_appendf(&comment,
1965 " Change background color to \"%h\".", zValue);
 
1966 }else if( strcmp(zName, "-bgcolor")==0 ){
1967 blob_appendf(&comment, " Cancel background color.");
1968 }else if( strcmp(zName, "+comment")==0 ){
1969 blob_appendf(&comment, " Edit check-in comment.");
 
1970 }else if( strcmp(zName, "+user")==0 ){
1971 blob_appendf(&comment, " Change user to \"%h\".", zValue);
 
1972 }else if( strcmp(zName, "+date")==0 ){
1973 blob_appendf(&comment, " Timestamp %h.", zValue);
 
1974 }else if( memcmp(zName, "-sym-",5)==0 ){
1975 if( !branchMove ) blob_appendf(&comment, " Cancel tag %h.", &zName[5]);
1976 }else if( memcmp(zName, "*sym-",5)==0 ){
1977 if( !branchMove ){
1978 blob_appendf(&comment, " Add propagating tag \"%h\".", &zName[5]);
1979 }
1980 }else if( memcmp(zName, "+sym-",5)==0 ){
1981 blob_appendf(&comment, " Add tag \"%h\".", &zName[5]);
1982 }else if( memcmp(zName, "-sym-",5)==0 ){
1983 blob_appendf(&comment, " Cancel tag \"%h\".", &zName[5]);
1984 }else if( strcmp(zName, "+closed")==0 ){
1985 blob_append(&comment, " Marked \"Closed\"", -1);
1986 if( zValue && *zValue ){
1987 blob_appendf(&comment, " with note \"%h\"", zValue);
1988 }
1989 blob_append(&comment, ".", 1);
1990 }else if( strcmp(zName, "-closed")==0 ){
1991 blob_append(&comment, " Removed the \"Closed\" mark", -1);
1992 if( zValue && *zValue ){
1993 blob_appendf(&comment, " with note \"%h\"", zValue);
1994 }
1995 blob_append(&comment, ".", 1);
1996 }else {
1997 if( zName[0]=='-' ){
1998 blob_appendf(&comment, " Cancel \"%h\"", &zName[1]);
1999 }else if( zName[0]=='+' ){
2000 blob_appendf(&comment, " Add \"%h\"", &zName[1]);
@@ -2004,10 +2003,16 @@
2004 if( zValue && zValue[0] ){
2005 blob_appendf(&comment, " with value \"%h\".", zValue);
2006 }else{
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
--- src/manifest.c
+++ src/manifest.c
@@ -469,12 +469,13 @@
469
470 /*
471 ** C <comment>
472 **
473 ** Comment text is fossil-encoded. There may be no more than
474 ** one C line. C lines are required for manifests, are optional
475 ** for Events and Attachments, and are disallowed on all other
476 ** control files.
477 */
478 case 'C': {
479 if( p->zComment!=0 ) SYNTAX("more than one C-card");
480 p->zComment = next_token(&x, 0);
481 if( p->zComment==0 ) SYNTAX("missing comment text on C-card");
@@ -663,11 +664,13 @@
664 /*
665 ** P <uuid> ...
666 **
667 ** Specify one or more other artifacts which are the parents of
668 ** this artifact. The first parent is the primary parent. All
669 ** others are parents by merge. Note that the initial empty
670 ** checkin historically has an empty P-card, so empty P-cards
671 ** must be accepted.
672 */
673 case 'P': {
674 while( (zUuid = next_token(&x, &sz))!=0 ){
675 if( sz!=UUID_SIZE ) SYNTAX("wrong size UUID on P-card");
676 if( !validate16(zUuid, UUID_SIZE) )SYNTAX("invalid UUID on P-card");
@@ -1955,46 +1958,42 @@
1958 if( strcmp(zName, "*branch")==0 ){
1959 blob_appendf(&comment,
1960 " Move to branch [/timeline?r=%h&nd&dp=%S | %h].",
1961 zValue, zUuid, zValue);
1962 branchMove = 1;
1963 continue;
1964 }else if( strcmp(zName, "*bgcolor")==0 ){
1965 blob_appendf(&comment,
1966 " Change branch background color to \"%h\".", zValue);
1967 continue;
1968 }else if( strcmp(zName, "+bgcolor")==0 ){
1969 blob_appendf(&comment,
1970 " Change background color to \"%h\".", zValue);
1971 continue;
1972 }else if( strcmp(zName, "-bgcolor")==0 ){
1973 blob_appendf(&comment, " Cancel background color");
1974 }else if( strcmp(zName, "+comment")==0 ){
1975 blob_appendf(&comment, " Edit check-in comment.");
1976 continue;
1977 }else if( strcmp(zName, "+user")==0 ){
1978 blob_appendf(&comment, " Change user to \"%h\".", zValue);
1979 continue;
1980 }else if( strcmp(zName, "+date")==0 ){
1981 blob_appendf(&comment, " Timestamp %h.", zValue);
1982 continue;
1983 }else if( memcmp(zName, "-sym-",5)==0 ){
1984 if( !branchMove ) blob_appendf(&comment, " Cancel tag \"%h\"", &zName[5]);
1985 }else if( memcmp(zName, "*sym-",5)==0 ){
1986 if( !branchMove ){
1987 blob_appendf(&comment, " Add propagating tag \"%h\"", &zName[5]);
1988 }
1989 }else if( memcmp(zName, "+sym-",5)==0 ){
1990 blob_appendf(&comment, " Add tag \"%h\"", &zName[5]);
 
 
1991 }else if( strcmp(zName, "+closed")==0 ){
1992 blob_append(&comment, " Marked \"Closed\"", -1);
 
 
 
 
1993 }else if( strcmp(zName, "-closed")==0 ){
1994 blob_append(&comment, " Removed the \"Closed\" mark", -1);
 
 
 
 
1995 }else {
1996 if( zName[0]=='-' ){
1997 blob_appendf(&comment, " Cancel \"%h\"", &zName[1]);
1998 }else if( zName[0]=='+' ){
1999 blob_appendf(&comment, " Add \"%h\"", &zName[1]);
@@ -2004,10 +2003,16 @@
2003 if( zValue && zValue[0] ){
2004 blob_appendf(&comment, " with value \"%h\".", zValue);
2005 }else{
2006 blob_appendf(&comment, ".");
2007 }
2008 continue;
2009 }
2010 if( zValue && zValue[0] ){
2011 blob_appendf(&comment, " with note \"%h\".", zValue);
2012 }else{
2013 blob_appendf(&comment, ".");
2014 }
2015 }
2016 /*blob_appendf(&comment, " &#91;[/info/%S | details]&#93;");*/
2017 if( blob_size(&comment)==0 ) blob_append(&comment, " ", 1);
2018 db_multi_exec(
2019
+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
+5 -5
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
6969
#define pclose _pclose
7070
#else
7171
/* Make sure isatty() has a prototype.
7272
*/
7373
extern int isatty(int);
74
-#endif
7574
7675
/* popen and pclose are not C89 functions and so are sometimes omitted from
7776
** the <stdio.h> header */
78
-FILE *popen(const char*,const char*);
79
-int pclose(FILE*);
77
+extern FILE *popen(const char*,const char*);
78
+extern int pclose(FILE*);
79
+#endif
8080
8181
#if defined(_WIN32_WCE)
8282
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
8383
* thus we always assume that we have a console. That can be
8484
* overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552552
fputc('\\', out);
553553
fputc('n', out);
554554
}else if( c=='\r' ){
555555
fputc('\\', out);
556556
fputc('r', out);
557
- }else if( !isprint(c) ){
557
+ }else if( !isprint(c&0xff) ){
558558
fprintf(out, "\\%03o", c&0xff);
559559
}else{
560560
fputc(c, out);
561561
}
562562
}
@@ -1278,11 +1278,11 @@
12781278
zType = azArg[1];
12791279
zSql = azArg[2];
12801280
12811281
if( strcmp(zTable, "sqlite_sequence")==0 ){
12821282
zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283
- }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1283
+ }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
12841284
fprintf(p->out, "ANALYZE sqlite_master;\n");
12851285
}else if( strncmp(zTable, "sqlite_", 7)==0 ){
12861286
return 0;
12871287
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
12881288
char *zIns;
12891289
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
69 #define pclose _pclose
70 #else
71 /* Make sure isatty() has a prototype.
72 */
73 extern int isatty(int);
74 #endif
75
76 /* popen and pclose are not C89 functions and so are sometimes omitted from
77 ** the <stdio.h> header */
78 FILE *popen(const char*,const char*);
79 int pclose(FILE*);
 
80
81 #if defined(_WIN32_WCE)
82 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
83 * thus we always assume that we have a console. That can be
84 * overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552 fputc('\\', out);
553 fputc('n', out);
554 }else if( c=='\r' ){
555 fputc('\\', out);
556 fputc('r', out);
557 }else if( !isprint(c) ){
558 fprintf(out, "\\%03o", c&0xff);
559 }else{
560 fputc(c, out);
561 }
562 }
@@ -1278,11 +1278,11 @@
1278 zType = azArg[1];
1279 zSql = azArg[2];
1280
1281 if( strcmp(zTable, "sqlite_sequence")==0 ){
1282 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1284 fprintf(p->out, "ANALYZE sqlite_master;\n");
1285 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1286 return 0;
1287 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1288 char *zIns;
1289
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
69 #define pclose _pclose
70 #else
71 /* Make sure isatty() has a prototype.
72 */
73 extern int isatty(int);
 
74
75 /* popen and pclose are not C89 functions and so are sometimes omitted from
76 ** the <stdio.h> header */
77 extern FILE *popen(const char*,const char*);
78 extern int pclose(FILE*);
79 #endif
80
81 #if defined(_WIN32_WCE)
82 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
83 * thus we always assume that we have a console. That can be
84 * overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552 fputc('\\', out);
553 fputc('n', out);
554 }else if( c=='\r' ){
555 fputc('\\', out);
556 fputc('r', out);
557 }else if( !isprint(c&0xff) ){
558 fprintf(out, "\\%03o", c&0xff);
559 }else{
560 fputc(c, out);
561 }
562 }
@@ -1278,11 +1278,11 @@
1278 zType = azArg[1];
1279 zSql = azArg[2];
1280
1281 if( strcmp(zTable, "sqlite_sequence")==0 ){
1282 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
1284 fprintf(p->out, "ANALYZE sqlite_master;\n");
1285 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1286 return 0;
1287 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1288 char *zIns;
1289
+5 -5
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
6969
#define pclose _pclose
7070
#else
7171
/* Make sure isatty() has a prototype.
7272
*/
7373
extern int isatty(int);
74
-#endif
7574
7675
/* popen and pclose are not C89 functions and so are sometimes omitted from
7776
** the <stdio.h> header */
78
-FILE *popen(const char*,const char*);
79
-int pclose(FILE*);
77
+extern FILE *popen(const char*,const char*);
78
+extern int pclose(FILE*);
79
+#endif
8080
8181
#if defined(_WIN32_WCE)
8282
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
8383
* thus we always assume that we have a console. That can be
8484
* overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552552
fputc('\\', out);
553553
fputc('n', out);
554554
}else if( c=='\r' ){
555555
fputc('\\', out);
556556
fputc('r', out);
557
- }else if( !isprint(c) ){
557
+ }else if( !isprint(c&0xff) ){
558558
fprintf(out, "\\%03o", c&0xff);
559559
}else{
560560
fputc(c, out);
561561
}
562562
}
@@ -1278,11 +1278,11 @@
12781278
zType = azArg[1];
12791279
zSql = azArg[2];
12801280
12811281
if( strcmp(zTable, "sqlite_sequence")==0 ){
12821282
zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283
- }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1283
+ }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
12841284
fprintf(p->out, "ANALYZE sqlite_master;\n");
12851285
}else if( strncmp(zTable, "sqlite_", 7)==0 ){
12861286
return 0;
12871287
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
12881288
char *zIns;
12891289
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
69 #define pclose _pclose
70 #else
71 /* Make sure isatty() has a prototype.
72 */
73 extern int isatty(int);
74 #endif
75
76 /* popen and pclose are not C89 functions and so are sometimes omitted from
77 ** the <stdio.h> header */
78 FILE *popen(const char*,const char*);
79 int pclose(FILE*);
 
80
81 #if defined(_WIN32_WCE)
82 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
83 * thus we always assume that we have a console. That can be
84 * overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552 fputc('\\', out);
553 fputc('n', out);
554 }else if( c=='\r' ){
555 fputc('\\', out);
556 fputc('r', out);
557 }else if( !isprint(c) ){
558 fprintf(out, "\\%03o", c&0xff);
559 }else{
560 fputc(c, out);
561 }
562 }
@@ -1278,11 +1278,11 @@
1278 zType = azArg[1];
1279 zSql = azArg[2];
1280
1281 if( strcmp(zTable, "sqlite_sequence")==0 ){
1282 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283 }else if( strcmp(zTable, "sqlite_stat1")==0 ){
1284 fprintf(p->out, "ANALYZE sqlite_master;\n");
1285 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1286 return 0;
1287 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1288 char *zIns;
1289
--- src/shell.c
+++ src/shell.c
@@ -69,16 +69,16 @@
69 #define pclose _pclose
70 #else
71 /* Make sure isatty() has a prototype.
72 */
73 extern int isatty(int);
 
74
75 /* popen and pclose are not C89 functions and so are sometimes omitted from
76 ** the <stdio.h> header */
77 extern FILE *popen(const char*,const char*);
78 extern int pclose(FILE*);
79 #endif
80
81 #if defined(_WIN32_WCE)
82 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
83 * thus we always assume that we have a console. That can be
84 * overridden with the -batch command line option.
@@ -552,11 +552,11 @@
552 fputc('\\', out);
553 fputc('n', out);
554 }else if( c=='\r' ){
555 fputc('\\', out);
556 fputc('r', out);
557 }else if( !isprint(c&0xff) ){
558 fprintf(out, "\\%03o", c&0xff);
559 }else{
560 fputc(c, out);
561 }
562 }
@@ -1278,11 +1278,11 @@
1278 zType = azArg[1];
1279 zSql = azArg[2];
1280
1281 if( strcmp(zTable, "sqlite_sequence")==0 ){
1282 zPrepStmt = "DELETE FROM sqlite_sequence;\n";
1283 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
1284 fprintf(p->out, "ANALYZE sqlite_master;\n");
1285 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
1286 return 0;
1287 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
1288 char *zIns;
1289
+2190 -1165
--- 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.1. 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.1"
660
-#define SQLITE_VERSION_NUMBER 3008000
661
-#define SQLITE_SOURCE_ID "2013-08-29 13:47:05 c5857808c0707baa30994dd6aa3b9c93a74c0073"
659
+#define SQLITE_VERSION "3.8.1"
660
+#define SQLITE_VERSION_NUMBER 3008001
661
+#define SQLITE_SOURCE_ID "2013-09-04 04:04:08 8df95bb0b3f72222cf262174247a467c234f9939"
662662
663663
/*
664664
** CAPI3REF: Run-Time Library Version Numbers
665665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
666666
**
@@ -1026,16 +1026,18 @@
10261026
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
10271027
#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
10281028
#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
10291029
#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
10301030
#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
1031
+#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
10311032
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
10321033
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
10331034
#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
10341035
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
10351036
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
10361037
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1038
+#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
10371039
#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
10381040
#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
10391041
#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
10401042
#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
10411043
#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
@@ -8368,10 +8370,24 @@
83688370
#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
83698371
# undef SQLITE_DEFAULT_MMAP_SIZE
83708372
# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
83718373
#endif
83728374
8375
+/*
8376
+** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8377
+** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
8378
+** define SQLITE_ENABLE_STAT3_OR_STAT4
8379
+*/
8380
+#ifdef SQLITE_ENABLE_STAT4
8381
+# undef SQLITE_ENABLE_STAT3
8382
+# define SQLITE_ENABLE_STAT3_OR_STAT4 1
8383
+#elif SQLITE_ENABLE_STAT3
8384
+# define SQLITE_ENABLE_STAT3_OR_STAT4 1
8385
+#elif SQLITE_ENABLE_STAT3_OR_STAT4
8386
+# undef SQLITE_ENABLE_STAT3_OR_STAT4
8387
+#endif
8388
+
83738389
/*
83748390
** An instance of the following structure is used to store the busy-handler
83758391
** callback for a given sqlite handle.
83768392
**
83778393
** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -10770,13 +10786,14 @@
1077010786
u16 nColumn; /* Number of columns in table used by this index */
1077110787
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1077210788
unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
1077310789
unsigned bUnordered:1; /* Use this index for == or IN queries only */
1077410790
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10775
-#ifdef SQLITE_ENABLE_STAT3
10791
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
1077610792
int nSample; /* Number of elements in aSample[] */
10777
- tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10793
+ int nSampleCol; /* Size of IndexSample.anEq[] and so on */
10794
+ tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
1077810795
IndexSample *aSample; /* Samples of the left-most key */
1077910796
#endif
1078010797
};
1078110798
1078210799
/*
@@ -10783,20 +10800,15 @@
1078310800
** Each sample stored in the sqlite_stat3 table is represented in memory
1078410801
** using a structure of this type. See documentation at the top of the
1078510802
** analyze.c source file for additional information.
1078610803
*/
1078710804
struct IndexSample {
10788
- union {
10789
- char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10790
- double r; /* Value if eType is SQLITE_FLOAT */
10791
- i64 i; /* Value if eType is SQLITE_INTEGER */
10792
- } u;
10793
- u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10794
- int nByte; /* Size in byte of text or blob. */
10795
- tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10796
- tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10797
- tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10805
+ void *p; /* Pointer to sampled record */
10806
+ int n; /* Size of record in bytes */
10807
+ tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
10808
+ tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
10809
+ tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
1079810810
};
1079910811
1080010812
/*
1080110813
** Each token coming out of the lexer is an instance of
1080210814
** this structure. Tokens are also used as part of an expression.
@@ -12264,13 +12276,10 @@
1226412276
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1226512277
void(*)(void*));
1226612278
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1226712279
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1226812280
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12269
-#ifdef SQLITE_ENABLE_STAT3
12270
-SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
12271
-#endif
1227212281
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1227312282
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1227412283
#ifndef SQLITE_AMALGAMATION
1227512284
SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
1227612285
SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12332,10 +12341,16 @@
1233212341
SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
1233312342
SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
1233412343
1233512344
SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
1233612345
SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12346
+
12347
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12348
+SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12349
+SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12350
+SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12351
+#endif
1233712352
1233812353
/*
1233912354
** The interface to the LEMON-generated parser
1234012355
*/
1234112356
SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
@@ -12916,11 +12931,13 @@
1291612931
"ENABLE_OVERSIZE_CELL_CHECK",
1291712932
#endif
1291812933
#ifdef SQLITE_ENABLE_RTREE
1291912934
"ENABLE_RTREE",
1292012935
#endif
12921
-#ifdef SQLITE_ENABLE_STAT3
12936
+#if defined(SQLITE_ENABLE_STAT4)
12937
+ "ENABLE_STAT4",
12938
+#elif defined(SQLITE_ENABLE_STAT3)
1292212939
"ENABLE_STAT3",
1292312940
#endif
1292412941
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1292512942
"ENABLE_UNLOCK_NOTIFY",
1292612943
#endif
@@ -16075,11 +16092,11 @@
1607516092
struct MemBlockHdr *pHdr;
1607616093
if( !p ){
1607716094
return 0;
1607816095
}
1607916096
pHdr = sqlite3MemsysGetHeader(p);
16080
- return pHdr->iSize;
16097
+ return (int)pHdr->iSize;
1608116098
}
1608216099
1608316100
/*
1608416101
** Initialize the memory allocation subsystem.
1608516102
*/
@@ -16117,19 +16134,19 @@
1611716134
static void randomFill(char *pBuf, int nByte){
1611816135
unsigned int x, y, r;
1611916136
x = SQLITE_PTR_TO_INT(pBuf);
1612016137
y = nByte | 1;
1612116138
while( nByte >= 4 ){
16122
- x = (x>>1) ^ (-(x&1) & 0xd0000001);
16139
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
1612316140
y = y*1103515245 + 12345;
1612416141
r = x ^ y;
1612516142
*(int*)pBuf = r;
1612616143
pBuf += 4;
1612716144
nByte -= 4;
1612816145
}
1612916146
while( nByte-- > 0 ){
16130
- x = (x>>1) ^ (-(x&1) & 0xd0000001);
16147
+ x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
1613116148
y = y*1103515245 + 12345;
1613216149
r = x ^ y;
1613316150
*(pBuf++) = r & 0xff;
1613416151
}
1613516152
}
@@ -16220,13 +16237,13 @@
1622016237
assert( mem.pLast==pHdr );
1622116238
mem.pLast = pHdr->pPrev;
1622216239
}
1622316240
z = (char*)pBt;
1622416241
z -= pHdr->nTitle;
16225
- adjustStats(pHdr->iSize, -1);
16242
+ adjustStats((int)pHdr->iSize, -1);
1622616243
randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16227
- pHdr->iSize + sizeof(int) + pHdr->nTitle);
16244
+ (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
1622816245
free(z);
1622916246
sqlite3_mutex_leave(mem.mutex);
1623016247
}
1623116248
1623216249
/*
@@ -16244,13 +16261,13 @@
1624416261
assert( mem.disallow==0 );
1624516262
assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
1624616263
pOldHdr = sqlite3MemsysGetHeader(pPrior);
1624716264
pNew = sqlite3MemMalloc(nByte);
1624816265
if( pNew ){
16249
- memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
16266
+ memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
1625016267
if( nByte>pOldHdr->iSize ){
16251
- randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
16268
+ randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
1625216269
}
1625316270
sqlite3MemFree(pPrior);
1625416271
}
1625516272
return pNew;
1625616273
}
@@ -16361,11 +16378,11 @@
1636116378
SQLITE_PRIVATE void sqlite3MemdebugSync(){
1636216379
struct MemBlockHdr *pHdr;
1636316380
for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
1636416381
void **pBt = (void**)pHdr;
1636516382
pBt -= pHdr->nBacktraceSlots;
16366
- mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16383
+ mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
1636716384
}
1636816385
}
1636916386
1637016387
/*
1637116388
** Open the file indicated and write a log of all unfreed memory
@@ -18483,11 +18500,11 @@
1848318500
GetVersionEx(&sInfo);
1848418501
osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
1848518502
}
1848618503
return osType==2;
1848718504
}
18488
-#endif /* SQLITE_OS_WINCE */
18505
+#endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
1848918506
#endif
1849018507
1849118508
#ifdef SQLITE_DEBUG
1849218509
/*
1849318510
** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18521,11 +18538,11 @@
1852118538
/* As winMutexInit() and winMutexEnd() are called as part
1852218539
** of the sqlite3_initialize and sqlite3_shutdown()
1852318540
** processing, the "interlocked" magic is probably not
1852418541
** strictly necessary.
1852518542
*/
18526
-static long winMutex_lock = 0;
18543
+static LONG winMutex_lock = 0;
1852718544
1852818545
SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
1852918546
1853018547
static int winMutexInit(void){
1853118548
/* The first to increment to 1 does actual initialization */
@@ -21082,36 +21099,10 @@
2108221099
assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
2108321100
assert( m.z || db->mallocFailed );
2108421101
return m.z;
2108521102
}
2108621103
21087
-/*
21088
-** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
21089
-** enc. A pointer to the new string is returned, and the value of *pnOut
21090
-** is set to the length of the returned string in bytes. The call should
21091
-** arrange to call sqlite3DbFree() on the returned pointer when it is
21092
-** no longer required.
21093
-**
21094
-** If a malloc failure occurs, NULL is returned and the db.mallocFailed
21095
-** flag set.
21096
-*/
21097
-#ifdef SQLITE_ENABLE_STAT3
21098
-SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
21099
- Mem m;
21100
- memset(&m, 0, sizeof(m));
21101
- m.db = db;
21102
- sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
21103
- if( sqlite3VdbeMemTranslate(&m, enc) ){
21104
- assert( db->mallocFailed );
21105
- return 0;
21106
- }
21107
- assert( m.z==m.zMalloc );
21108
- *pnOut = m.n;
21109
- return m.z;
21110
-}
21111
-#endif
21112
-
2111321104
/*
2111421105
** zIn is a UTF-16 encoded unicode string at least nChar characters long.
2111521106
** Return the number of bytes in the first nChar unicode characters
2111621107
** in pZ. nChar must be non-negative.
2111721108
*/
@@ -23064,15 +23055,17 @@
2306423055
void *lockingContext; /* Locking style specific state */
2306523056
UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
2306623057
const char *zPath; /* Name of the file */
2306723058
unixShm *pShm; /* Shared memory segment information */
2306823059
int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23060
+#if SQLITE_MAX_MMAP_SIZE>0
2306923061
int nFetchOut; /* Number of outstanding xFetch refs */
2307023062
sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
2307123063
sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
2307223064
sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
2307323065
void *pMapRegion; /* Memory mapped region */
23066
+#endif
2307423067
#ifdef __QNXNTO__
2307523068
int sectorSize; /* Device sector size */
2307623069
int deviceCharacteristics; /* Precomputed device characteristics */
2307723070
#endif
2307823071
#if SQLITE_ENABLE_LOCKING_STYLE
@@ -23503,10 +23496,11 @@
2350323496
#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
2350423497
2350523498
{ "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
2350623499
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
2350723500
23501
+#if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
2350823502
{ "mmap", (sqlite3_syscall_ptr)mmap, 0 },
2350923503
#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
2351023504
2351123505
{ "munmap", (sqlite3_syscall_ptr)munmap, 0 },
2351223506
#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23515,10 +23509,11 @@
2351523509
{ "mremap", (sqlite3_syscall_ptr)mremap, 0 },
2351623510
#else
2351723511
{ "mremap", (sqlite3_syscall_ptr)0, 0 },
2351823512
#endif
2351923513
#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23514
+#endif
2352023515
2352123516
}; /* End of the overrideable system calls */
2352223517
2352323518
/*
2352423519
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23601,10 +23596,19 @@
2360123596
if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
2360223597
}
2360323598
return 0;
2360423599
}
2360523600
23601
+/*
23602
+** Do not accept any file descriptor less than this value, in order to avoid
23603
+** opening database file using file descriptors that are commonly used for
23604
+** standard input, output, and error.
23605
+*/
23606
+#ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
23607
+# define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
23608
+#endif
23609
+
2360623610
/*
2360723611
** Invoke open(). Do so multiple times, until it either succeeds or
2360823612
** fails for some reason other than EINTR.
2360923613
**
2361023614
** If the file creation mode "m" is 0 then set it to the default for
@@ -23621,17 +23625,27 @@
2362123625
** recover the hot journals.
2362223626
*/
2362323627
static int robust_open(const char *z, int f, mode_t m){
2362423628
int fd;
2362523629
mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23626
- do{
23630
+ while(1){
2362723631
#if defined(O_CLOEXEC)
2362823632
fd = osOpen(z,f|O_CLOEXEC,m2);
2362923633
#else
2363023634
fd = osOpen(z,f,m2);
2363123635
#endif
23632
- }while( fd<0 && errno==EINTR );
23636
+ if( fd<0 ){
23637
+ if( errno==EINTR ) continue;
23638
+ break;
23639
+ }
23640
+ if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
23641
+ osClose(fd);
23642
+ sqlite3_log(SQLITE_WARNING,
23643
+ "attempt to open \"%s\" as file descriptor %d", z, fd);
23644
+ fd = -1;
23645
+ if( osOpen("/dev/null", f, m)<0 ) break;
23646
+ }
2363323647
if( fd>=0 ){
2363423648
if( m!=0 ){
2363523649
struct stat statbuf;
2363623650
if( osFstat(fd, &statbuf)==0
2363723651
&& statbuf.st_size==0
@@ -24925,12 +24939,14 @@
2492524939
static int unixUnlock(sqlite3_file *id, int eFileLock){
2492624940
assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
2492724941
return posixUnlock(id, eFileLock, 0);
2492824942
}
2492924943
24944
+#if SQLITE_MAX_MMAP_SIZE>0
2493024945
static int unixMapfile(unixFile *pFd, i64 nByte);
2493124946
static void unixUnmapfile(unixFile *pFd);
24947
+#endif
2493224948
2493324949
/*
2493424950
** This function performs the parts of the "close file" operation
2493524951
** common to all locking schemes. It closes the directory and file
2493624952
** handles, if they are valid, and sets all fields of the unixFile
@@ -24940,11 +24956,13 @@
2494024956
** even on VxWorks. A mutex will be acquired on VxWorks by the
2494124957
** vxworksReleaseFileId() routine.
2494224958
*/
2494324959
static int closeUnixFile(sqlite3_file *id){
2494424960
unixFile *pFile = (unixFile*)id;
24961
+#if SQLITE_MAX_MMAP_SIZE>0
2494524962
unixUnmapfile(pFile);
24963
+#endif
2494624964
if( pFile->h>=0 ){
2494724965
robust_close(pFile, pFile->h, __LINE__);
2494824966
pFile->h = -1;
2494924967
}
2495024968
#if OS_VXWORKS
@@ -26145,10 +26163,11 @@
2614526163
#if (!defined(USE_PREAD) && !defined(USE_PREAD64))
2614626164
i64 newOffset;
2614726165
#endif
2614826166
TIMER_START;
2614926167
assert( cnt==(cnt&0x1ffff) );
26168
+ assert( id->h>2 );
2615026169
cnt &= 0x1ffff;
2615126170
do{
2615226171
#if defined(USE_PREAD)
2615326172
got = osPread(id->h, pBuf, cnt, offset);
2615426173
SimulateIOError( got = -1 );
@@ -26259,10 +26278,11 @@
2625926278
int *piErrno /* OUT: Error number if error occurs */
2626026279
){
2626126280
int rc = 0; /* Value returned by system call */
2626226281
2626326282
assert( nBuf==(nBuf&0x1ffff) );
26283
+ assert( fd>2 );
2626426284
nBuf &= 0x1ffff;
2626526285
TIMER_START;
2626626286
2626726287
#if defined(USE_PREAD)
2626826288
do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26644,17 +26664,19 @@
2664426664
if( pFile->inNormalWrite && nByte==0 ){
2664526665
pFile->transCntrChng = 1;
2664626666
}
2664726667
#endif
2664826668
26669
+#if SQLITE_MAX_MMAP_SIZE>0
2664926670
/* If the file was just truncated to a size smaller than the currently
2665026671
** mapped region, reduce the effective mapping size as well. SQLite will
2665126672
** use read() and write() to access data beyond this point from now on.
2665226673
*/
2665326674
if( nByte<pFile->mmapSize ){
2665426675
pFile->mmapSize = nByte;
2665526676
}
26677
+#endif
2665626678
2665726679
return SQLITE_OK;
2665826680
}
2665926681
}
2666026682
@@ -26740,10 +26762,11 @@
2674026762
}
2674126763
#endif
2674226764
}
2674326765
}
2674426766
26767
+#if SQLITE_MAX_MMAP_SIZE>0
2674526768
if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
2674626769
int rc;
2674726770
if( pFile->szChunk<=0 ){
2674826771
if( robust_ftruncate(pFile->h, nByte) ){
2674926772
pFile->lastErrno = errno;
@@ -26752,10 +26775,11 @@
2675226775
}
2675326776
2675426777
rc = unixMapfile(pFile, nByte);
2675526778
return rc;
2675626779
}
26780
+#endif
2675726781
2675826782
return SQLITE_OK;
2675926783
}
2676026784
2676126785
/*
@@ -26820,10 +26844,11 @@
2682026844
unixGetTempname(pFile->pVfs->mxPathname, zTFile);
2682126845
*(char**)pArg = zTFile;
2682226846
}
2682326847
return SQLITE_OK;
2682426848
}
26849
+#if SQLITE_MAX_MMAP_SIZE>0
2682526850
case SQLITE_FCNTL_MMAP_SIZE: {
2682626851
i64 newLimit = *(i64*)pArg;
2682726852
int rc = SQLITE_OK;
2682826853
if( newLimit>sqlite3GlobalConfig.mxMmap ){
2682926854
newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26836,10 +26861,11 @@
2683626861
rc = unixMapfile(pFile, -1);
2683726862
}
2683826863
}
2683926864
return rc;
2684026865
}
26866
+#endif
2684126867
#ifdef SQLITE_DEBUG
2684226868
/* The pager calls this method to signal that it has done
2684326869
** a rollback and that the database is therefore unchanged and
2684426870
** it hence it is OK for the transaction change counter to be
2684526871
** unchanged.
@@ -27646,26 +27672,24 @@
2764627672
# define unixShmLock 0
2764727673
# define unixShmBarrier 0
2764827674
# define unixShmUnmap 0
2764927675
#endif /* #ifndef SQLITE_OMIT_WAL */
2765027676
27677
+#if SQLITE_MAX_MMAP_SIZE>0
2765127678
/*
2765227679
** If it is currently memory mapped, unmap file pFd.
2765327680
*/
2765427681
static void unixUnmapfile(unixFile *pFd){
2765527682
assert( pFd->nFetchOut==0 );
27656
-#if SQLITE_MAX_MMAP_SIZE>0
2765727683
if( pFd->pMapRegion ){
2765827684
osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
2765927685
pFd->pMapRegion = 0;
2766027686
pFd->mmapSize = 0;
2766127687
pFd->mmapSizeActual = 0;
2766227688
}
27663
-#endif
2766427689
}
2766527690
27666
-#if SQLITE_MAX_MMAP_SIZE>0
2766727691
/*
2766827692
** Return the system page size.
2766927693
*/
2767027694
static int unixGetPagesize(void){
2767127695
#if HAVE_MREMAP
@@ -27674,13 +27698,11 @@
2767427698
return getpagesize();
2767527699
#else
2767627700
return (int)sysconf(_SC_PAGESIZE);
2767727701
#endif
2767827702
}
27679
-#endif /* SQLITE_MAX_MMAP_SIZE>0 */
2768027703
27681
-#if SQLITE_MAX_MMAP_SIZE>0
2768227704
/*
2768327705
** Attempt to set the size of the memory mapping maintained by file
2768427706
** descriptor pFd to nNew bytes. Any existing mapping is discarded.
2768527707
**
2768627708
** If successful, this function sets the following variables:
@@ -27761,11 +27783,10 @@
2776127783
pFd->mmapSizeMax = 0;
2776227784
}
2776327785
pFd->pMapRegion = (void *)pNew;
2776427786
pFd->mmapSize = pFd->mmapSizeActual = nNew;
2776527787
}
27766
-#endif
2776727788
2776827789
/*
2776927790
** Memory map or remap the file opened by file-descriptor pFd (if the file
2777027791
** is already mapped, the existing mapping is replaced by the new). Or, if
2777127792
** there already exists a mapping for this file, and there are still
@@ -27780,11 +27801,10 @@
2778027801
** SQLITE_OK is returned if no error occurs (even if the mapping is not
2778127802
** recreated as a result of outstanding references) or an SQLite error
2778227803
** code otherwise.
2778327804
*/
2778427805
static int unixMapfile(unixFile *pFd, i64 nByte){
27785
-#if SQLITE_MAX_MMAP_SIZE>0
2778627806
i64 nMap = nByte;
2778727807
int rc;
2778827808
2778927809
assert( nMap>=0 || pFd->nFetchOut==0 );
2779027810
if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27806,14 +27826,14 @@
2780627826
unixRemapfile(pFd, nMap);
2780727827
}else{
2780827828
unixUnmapfile(pFd);
2780927829
}
2781027830
}
27811
-#endif
2781227831
2781327832
return SQLITE_OK;
2781427833
}
27834
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
2781527835
2781627836
/*
2781727837
** If possible, return a pointer to a mapping of file fd starting at offset
2781827838
** iOff. The mapping must be valid for at least nAmt bytes.
2781927839
**
@@ -27858,10 +27878,11 @@
2785827878
*/
2785927879
static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
2786027880
unixFile *pFd = (unixFile *)fd; /* The underlying database file */
2786127881
UNUSED_PARAMETER(iOff);
2786227882
27883
+#if SQLITE_MAX_MMAP_SIZE>0
2786327884
/* If p==0 (unmap the entire file) then there must be no outstanding
2786427885
** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
2786527886
** then there must be at least one outstanding. */
2786627887
assert( (p==0)==(pFd->nFetchOut==0) );
2786727888
@@ -27873,10 +27894,11 @@
2787327894
}else{
2787427895
unixUnmapfile(pFd);
2787527896
}
2787627897
2787727898
assert( pFd->nFetchOut>=0 );
27899
+#endif
2787827900
return SQLITE_OK;
2787927901
}
2788027902
2788127903
/*
2788227904
** Here ends the implementation of all sqlite3_file methods.
@@ -28204,11 +28226,13 @@
2820428226
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2820528227
pNew->h = h;
2820628228
pNew->pVfs = pVfs;
2820728229
pNew->zPath = zFilename;
2820828230
pNew->ctrlFlags = (u8)ctrlFlags;
28231
+#if SQLITE_MAX_MMAP_SIZE>0
2820928232
pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28233
+#endif
2821028234
if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
2821128235
"psow", SQLITE_POWERSAFE_OVERWRITE) ){
2821228236
pNew->ctrlFlags |= UNIXFILE_PSOW;
2821328237
}
2821428238
if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -28360,10 +28384,11 @@
2836028384
** If no suitable temporary file directory can be found, return NULL.
2836128385
*/
2836228386
static const char *unixTempFileDir(void){
2836328387
static const char *azDirs[] = {
2836428388
0,
28389
+ 0,
2836528390
0,
2836628391
"/var/tmp",
2836728392
"/usr/tmp",
2836828393
"/tmp",
2836928394
0 /* List terminator */
@@ -28371,11 +28396,12 @@
2837128396
unsigned int i;
2837228397
struct stat buf;
2837328398
const char *zDir = 0;
2837428399
2837528400
azDirs[0] = sqlite3_temp_directory;
28376
- if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
28401
+ if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
28402
+ if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
2837728403
for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
2837828404
if( zDir==0 ) continue;
2837928405
if( osStat(zDir, &buf) ) continue;
2838028406
if( !S_ISDIR(buf.st_mode) ) continue;
2838128407
if( osAccess(zDir, 07) ) continue;
@@ -30484,11 +30510,11 @@
3048430510
*/
3048530511
#if SQLITE_OS_WIN /* This file is used for Windows only */
3048630512
3048730513
#ifdef __CYGWIN__
3048830514
# include <sys/cygwin.h>
30489
-/* # include <errno.h> */
30515
+# include <errno.h> /* amalgamator: keep */
3049030516
#endif
3049130517
3049230518
/*
3049330519
** Include code that is common to all os_*.c files
3049430520
*/
@@ -30705,30 +30731,99 @@
3070530731
/*
3070630732
** Compiling and using WAL mode requires several APIs that are only
3070730733
** available in Windows platforms based on the NT kernel.
3070830734
*/
3070930735
#if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30710
-# error "WAL mode requires support from the Windows NT kernel, compile\
30736
+# error "WAL mode requires support from the Windows NT kernel, compile\
3071130737
with SQLITE_OMIT_WAL."
3071230738
#endif
3071330739
3071430740
/*
3071530741
** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
3071630742
** based on the sub-platform)?
3071730743
*/
30718
-#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
30744
+#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
3071930745
# define SQLITE_WIN32_HAS_ANSI
3072030746
#endif
3072130747
3072230748
/*
3072330749
** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
3072430750
** based on the sub-platform)?
3072530751
*/
30726
-#if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
30752
+#if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
30753
+ !defined(SQLITE_WIN32_NO_WIDE)
3072730754
# define SQLITE_WIN32_HAS_WIDE
3072830755
#endif
3072930756
30757
+/*
30758
+** Make sure at least one set of Win32 APIs is available.
30759
+*/
30760
+#if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
30761
+# error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
30762
+ must be defined."
30763
+#endif
30764
+
30765
+/*
30766
+** Maximum pathname length (in chars) for Win32. This should normally be
30767
+** MAX_PATH.
30768
+*/
30769
+#ifndef SQLITE_WIN32_MAX_PATH_CHARS
30770
+# define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
30771
+#endif
30772
+
30773
+/*
30774
+** Maximum pathname length (in chars) for WinNT. This should normally be
30775
+** 32767.
30776
+*/
30777
+#ifndef SQLITE_WINNT_MAX_PATH_CHARS
30778
+# define SQLITE_WINNT_MAX_PATH_CHARS (32767)
30779
+#endif
30780
+
30781
+/*
30782
+** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
30783
+** characters, so we allocate 3 bytes per character assuming worst-case of
30784
+** 4-bytes-per-character for UTF8.
30785
+*/
30786
+#ifndef SQLITE_WIN32_MAX_PATH_BYTES
30787
+# define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
30788
+#endif
30789
+
30790
+/*
30791
+** Maximum pathname length (in bytes) for WinNT. This should normally be
30792
+** 32767 * sizeof(WCHAR).
30793
+*/
30794
+#ifndef SQLITE_WINNT_MAX_PATH_BYTES
30795
+# define SQLITE_WINNT_MAX_PATH_BYTES \
30796
+ (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
30797
+#endif
30798
+
30799
+/*
30800
+** Maximum error message length (in chars) for WinRT.
30801
+*/
30802
+#ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
30803
+# define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
30804
+#endif
30805
+
30806
+/*
30807
+** Returns non-zero if the character should be treated as a directory
30808
+** separator.
30809
+*/
30810
+#ifndef winIsDirSep
30811
+# define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
30812
+#endif
30813
+
30814
+/*
30815
+** Returns the string that should be used as the directory separator.
30816
+*/
30817
+#ifndef winGetDirDep
30818
+# ifdef __CYGWIN__
30819
+# define winGetDirDep() "/"
30820
+# else
30821
+# define winGetDirDep() "\\"
30822
+# endif
30823
+#endif
30824
+
3073030825
/*
3073130826
** Do we need to manually define the Win32 file mapping APIs for use with WAL
3073230827
** mode (e.g. these APIs are available in the Windows CE SDK; however, they
3073330828
** are not present in the header file)?
3073430829
*/
@@ -30776,11 +30871,11 @@
3077630871
#ifndef FILE_ATTRIBUTE_MASK
3077730872
# define FILE_ATTRIBUTE_MASK (0x0003FFF7)
3077830873
#endif
3077930874
3078030875
#ifndef SQLITE_OMIT_WAL
30781
-/* Forward references */
30876
+/* Forward references to structures used for WAL */
3078230877
typedef struct winShm winShm; /* A connection to shared-memory */
3078330878
typedef struct winShmNode winShmNode; /* A region of shared-memory */
3078430879
#endif
3078530880
3078630881
/*
@@ -31731,16 +31826,16 @@
3173131826
** API as long as we don't call it when running Win95/98/ME. A call to
3173231827
** this routine is used to determine if the host is Win95/98/ME or
3173331828
** WinNT/2K/XP so that we will know whether or not we can safely call
3173431829
** the LockFileEx() API.
3173531830
*/
31736
-#if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31737
-# define isNT() (1)
31831
+#if SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
31832
+# define osIsNT() (1)
3173831833
#elif !defined(SQLITE_WIN32_HAS_WIDE)
31739
-# define isNT() (0)
31834
+# define osIsNT() (0)
3174031835
#else
31741
- static int isNT(void){
31836
+ static int osIsNT(void){
3174231837
if( sqlite3_os_type==0 ){
3174331838
OSVERSIONINFOA sInfo;
3174431839
sInfo.dwOSVersionInfoSize = sizeof(sInfo);
3174531840
osGetVersionExA(&sInfo);
3174631841
sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31947,11 +32042,11 @@
3194732042
/*
3194832043
** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
3194932044
**
3195032045
** Space to hold the returned string is obtained from malloc.
3195132046
*/
31952
-static LPWSTR utf8ToUnicode(const char *zFilename){
32047
+static LPWSTR winUtf8ToUnicode(const char *zFilename){
3195332048
int nChar;
3195432049
LPWSTR zWideFilename;
3195532050
3195632051
nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
3195732052
if( nChar==0 ){
@@ -31972,11 +32067,11 @@
3197232067
3197332068
/*
3197432069
** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
3197532070
** obtained from sqlite3_malloc().
3197632071
*/
31977
-static char *unicodeToUtf8(LPCWSTR zWideFilename){
32072
+static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
3197832073
int nByte;
3197932074
char *zFilename;
3198032075
3198132076
nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
3198232077
if( nByte == 0 ){
@@ -32000,11 +32095,11 @@
3200032095
** current codepage settings for file apis.
3200132096
**
3200232097
** Space to hold the returned string is obtained
3200332098
** from sqlite3_malloc.
3200432099
*/
32005
-static LPWSTR mbcsToUnicode(const char *zFilename){
32100
+static LPWSTR winMbcsToUnicode(const char *zFilename){
3200632101
int nByte;
3200732102
LPWSTR zMbcsFilename;
3200832103
int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3200932104
3201032105
nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32030,11 +32125,11 @@
3203032125
** user's ANSI codepage.
3203132126
**
3203232127
** Space to hold the returned string is obtained from
3203332128
** sqlite3_malloc().
3203432129
*/
32035
-static char *unicodeToMbcs(LPCWSTR zWideFilename){
32130
+static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
3203632131
int nByte;
3203732132
char *zFilename;
3203832133
int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
3203932134
3204032135
nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32060,15 +32155,15 @@
3206032155
*/
3206132156
SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
3206232157
char *zFilenameUtf8;
3206332158
LPWSTR zTmpWide;
3206432159
32065
- zTmpWide = mbcsToUnicode(zFilename);
32160
+ zTmpWide = winMbcsToUnicode(zFilename);
3206632161
if( zTmpWide==0 ){
3206732162
return 0;
3206832163
}
32069
- zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32164
+ zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
3207032165
sqlite3_free(zTmpWide);
3207132166
return zFilenameUtf8;
3207232167
}
3207332168
3207432169
/*
@@ -32077,15 +32172,15 @@
3207732172
*/
3207832173
SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
3207932174
char *zFilenameMbcs;
3208032175
LPWSTR zTmpWide;
3208132176
32082
- zTmpWide = utf8ToUnicode(zFilename);
32177
+ zTmpWide = winUtf8ToUnicode(zFilename);
3208332178
if( zTmpWide==0 ){
3208432179
return 0;
3208532180
}
32086
- zFilenameMbcs = unicodeToMbcs(zTmpWide);
32181
+ zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
3208732182
sqlite3_free(zTmpWide);
3208832183
return zFilenameMbcs;
3208932184
}
3209032185
3209132186
/*
@@ -32111,11 +32206,11 @@
3211132206
);
3211232207
assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
3211332208
if( ppDirectory ){
3211432209
char *zValueUtf8 = 0;
3211532210
if( zValue && zValue[0] ){
32116
- zValueUtf8 = unicodeToUtf8(zValue);
32211
+ zValueUtf8 = winUnicodeToUtf8(zValue);
3211732212
if ( zValueUtf8==0 ){
3211832213
return SQLITE_NOMEM;
3211932214
}
3212032215
}
3212132216
sqlite3_free(*ppDirectory);
@@ -32124,32 +32219,32 @@
3212432219
}
3212532220
return SQLITE_ERROR;
3212632221
}
3212732222
3212832223
/*
32129
-** The return value of getLastErrorMsg
32224
+** The return value of winGetLastErrorMsg
3213032225
** is zero if the error message fits in the buffer, or non-zero
3213132226
** otherwise (if the message was truncated).
3213232227
*/
32133
-static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32228
+static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
3213432229
/* FormatMessage returns 0 on failure. Otherwise it
3213532230
** returns the number of TCHARs written to the output
3213632231
** buffer, excluding the terminating null char.
3213732232
*/
3213832233
DWORD dwLen = 0;
3213932234
char *zOut = 0;
3214032235
32141
- if( isNT() ){
32236
+ if( osIsNT() ){
3214232237
#if SQLITE_OS_WINRT
32143
- WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
32238
+ WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
3214432239
dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
3214532240
FORMAT_MESSAGE_IGNORE_INSERTS,
3214632241
NULL,
3214732242
lastErrno,
3214832243
0,
3214932244
zTempWide,
32150
- MAX_PATH,
32245
+ SQLITE_WIN32_MAX_ERRMSG_CHARS,
3215132246
0);
3215232247
#else
3215332248
LPWSTR zTempWide = NULL;
3215432249
dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
3215532250
FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32162,11 +32257,11 @@
3216232257
0);
3216332258
#endif
3216432259
if( dwLen > 0 ){
3216532260
/* allocate a buffer and convert to UTF8 */
3216632261
sqlite3BeginBenignMalloc();
32167
- zOut = unicodeToUtf8(zTempWide);
32262
+ zOut = winUnicodeToUtf8(zTempWide);
3216832263
sqlite3EndBenignMalloc();
3216932264
#if !SQLITE_OS_WINRT
3217032265
/* free the system buffer allocated by FormatMessage */
3217132266
osLocalFree(zTempWide);
3217232267
#endif
@@ -32230,11 +32325,11 @@
3223032325
){
3223132326
char zMsg[500]; /* Human readable error text */
3223232327
int i; /* Loop counter */
3223332328
3223432329
zMsg[0] = 0;
32235
- getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32330
+ winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
3223632331
assert( errcode!=SQLITE_OK );
3223732332
if( zPath==0 ) zPath = "";
3223832333
for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
3223932334
zMsg[i] = 0;
3224032335
sqlite3_log(errcode,
@@ -32255,30 +32350,30 @@
3225532350
# define SQLITE_WIN32_IOERR_RETRY 10
3225632351
#endif
3225732352
#ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
3225832353
# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
3225932354
#endif
32260
-static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32261
-static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32355
+static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32356
+static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
3226232357
3226332358
/*
3226432359
** If a ReadFile() or WriteFile() error occurs, invoke this routine
3226532360
** to see if it should be retried. Return TRUE to retry. Return FALSE
3226632361
** to give up with an error.
3226732362
*/
32268
-static int retryIoerr(int *pnRetry, DWORD *pError){
32363
+static int winRetryIoerr(int *pnRetry, DWORD *pError){
3226932364
DWORD e = osGetLastError();
32270
- if( *pnRetry>=win32IoerrRetry ){
32365
+ if( *pnRetry>=winIoerrRetry ){
3227132366
if( pError ){
3227232367
*pError = e;
3227332368
}
3227432369
return 0;
3227532370
}
3227632371
if( e==ERROR_ACCESS_DENIED ||
3227732372
e==ERROR_LOCK_VIOLATION ||
3227832373
e==ERROR_SHARING_VIOLATION ){
32279
- sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
32374
+ sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
3228032375
++*pnRetry;
3228132376
return 1;
3228232377
}
3228332378
if( pError ){
3228432379
*pError = e;
@@ -32287,15 +32382,15 @@
3228732382
}
3228832383
3228932384
/*
3229032385
** Log a I/O error retry episode.
3229132386
*/
32292
-static void logIoerr(int nRetry){
32387
+static void winLogIoerr(int nRetry){
3229332388
if( nRetry ){
3229432389
sqlite3_log(SQLITE_IOERR,
3229532390
"delayed %dms for lock/sharing conflict",
32296
- win32IoerrRetryDelay*nRetry*(nRetry+1)/2
32391
+ winIoerrRetryDelay*nRetry*(nRetry+1)/2
3229732392
);
3229832393
}
3229932394
}
3230032395
3230132396
#if SQLITE_OS_WINCE
@@ -32356,11 +32451,11 @@
3235632451
LPWSTR zName;
3235732452
DWORD lastErrno;
3235832453
BOOL bLogged = FALSE;
3235932454
BOOL bInit = TRUE;
3236032455
32361
- zName = utf8ToUnicode(zFilename);
32456
+ zName = winUtf8ToUnicode(zFilename);
3236232457
if( zName==0 ){
3236332458
/* out of memory */
3236432459
return SQLITE_IOERR_NOMEM;
3236532460
}
3236632461
@@ -32376,14 +32471,13 @@
3237632471
3237732472
/* Create/open the named mutex */
3237832473
pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
3237932474
if (!pFile->hMutex){
3238032475
pFile->lastErrno = osGetLastError();
32381
- winLogError(SQLITE_IOERR, pFile->lastErrno,
32382
- "winceCreateLock1", zFilename);
3238332476
sqlite3_free(zName);
32384
- return SQLITE_IOERR;
32477
+ return winLogError(SQLITE_IOERR, pFile->lastErrno,
32478
+ "winceCreateLock1", zFilename);
3238532479
}
3238632480
3238732481
/* Acquire the mutex before continuing */
3238832482
winceMutexAcquire(pFile->hMutex);
3238932483
@@ -32629,11 +32723,11 @@
3262932723
** API LockFile.
3263032724
*/
3263132725
return winceLockFile(phFile, offsetLow, offsetHigh,
3263232726
numBytesLow, numBytesHigh);
3263332727
#else
32634
- if( isNT() ){
32728
+ if( osIsNT() ){
3263532729
OVERLAPPED ovlp;
3263632730
memset(&ovlp, 0, sizeof(OVERLAPPED));
3263732731
ovlp.Offset = offsetLow;
3263832732
ovlp.OffsetHigh = offsetHigh;
3263932733
return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32660,11 +32754,11 @@
3266032754
** API UnlockFile.
3266132755
*/
3266232756
return winceUnlockFile(phFile, offsetLow, offsetHigh,
3266332757
numBytesLow, numBytesHigh);
3266432758
#else
32665
- if( isNT() ){
32759
+ if( osIsNT() ){
3266632760
OVERLAPPED ovlp;
3266732761
memset(&ovlp, 0, sizeof(OVERLAPPED));
3266832762
ovlp.Offset = offsetLow;
3266932763
ovlp.OffsetHigh = offsetHigh;
3267032764
return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32690,11 +32784,11 @@
3269032784
/*
3269132785
** Move the current position of the file handle passed as the first
3269232786
** argument to offset iOffset within the file. If successful, return 0.
3269332787
** Otherwise, set pFile->lastErrno and return non-zero.
3269432788
*/
32695
-static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32789
+static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
3269632790
#if !SQLITE_OS_WINRT
3269732791
LONG upperBits; /* Most sig. 32 bits of new offset */
3269832792
LONG lowerBits; /* Least sig. 32 bits of new offset */
3269932793
DWORD dwRet; /* Value returned by SetFilePointer() */
3270032794
DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32715,11 +32809,11 @@
3271532809
3271632810
if( (dwRet==INVALID_SET_FILE_POINTER
3271732811
&& ((lastErrno = osGetLastError())!=NO_ERROR)) ){
3271832812
pFile->lastErrno = lastErrno;
3271932813
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32720
- "seekWinFile", pFile->zPath);
32814
+ "winSeekFile", pFile->zPath);
3272132815
OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
3272232816
return 1;
3272332817
}
3272432818
3272532819
OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32736,11 +32830,11 @@
3273632830
bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
3273732831
3273832832
if(!bRet){
3273932833
pFile->lastErrno = osGetLastError();
3274032834
winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32741
- "seekWinFile", pFile->zPath);
32835
+ "winSeekFile", pFile->zPath);
3274232836
OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
3274332837
return 1;
3274432838
}
3274532839
3274632840
OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32747,11 +32841,12 @@
3274732841
return 0;
3274832842
#endif
3274932843
}
3275032844
3275132845
#if SQLITE_MAX_MMAP_SIZE>0
32752
-/* Forward references to VFS methods */
32846
+/* Forward references to VFS helper methods used for memory mapped files */
32847
+static int winMapfile(winFile*, sqlite3_int64);
3275332848
static int winUnmapfile(winFile*);
3275432849
#endif
3275532850
3275632851
/*
3275732852
** Close a file.
@@ -32851,11 +32946,11 @@
3285132946
}
3285232947
}
3285332948
#endif
3285432949
3285532950
#if SQLITE_OS_WINCE
32856
- if( seekWinFile(pFile, offset) ){
32951
+ if( winSeekFile(pFile, offset) ){
3285732952
OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
3285832953
return SQLITE_FULL;
3285932954
}
3286032955
while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
3286132956
#else
@@ -32864,17 +32959,17 @@
3286432959
overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
3286532960
while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
3286632961
osGetLastError()!=ERROR_HANDLE_EOF ){
3286732962
#endif
3286832963
DWORD lastErrno;
32869
- if( retryIoerr(&nRetry, &lastErrno) ) continue;
32964
+ if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
3287032965
pFile->lastErrno = lastErrno;
3287132966
OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
3287232967
return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
32873
- "winRead", pFile->zPath);
32968
+ "winRead", pFile->zPath);
3287432969
}
32875
- logIoerr(nRetry);
32970
+ winLogIoerr(nRetry);
3287632971
if( nRead<(DWORD)amt ){
3287732972
/* Unread parts of the buffer must be zero-filled */
3287832973
memset(&((char*)pBuf)[nRead], 0, amt-nRead);
3287932974
OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
3288032975
return SQLITE_IOERR_SHORT_READ;
@@ -32923,11 +33018,11 @@
3292333018
}
3292433019
}
3292533020
#endif
3292633021
3292733022
#if SQLITE_OS_WINCE
32928
- rc = seekWinFile(pFile, offset);
33023
+ rc = winSeekFile(pFile, offset);
3292933024
if( rc==0 ){
3293033025
#else
3293133026
{
3293233027
#endif
3293333028
#if !SQLITE_OS_WINCE
@@ -32948,11 +33043,11 @@
3294833043
#if SQLITE_OS_WINCE
3294933044
if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
3295033045
#else
3295133046
if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
3295233047
#endif
32953
- if( retryIoerr(&nRetry, &lastErrno) ) continue;
33048
+ if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
3295433049
break;
3295533050
}
3295633051
assert( nWrite==0 || nWrite<=(DWORD)nRem );
3295733052
if( nWrite==0 || nWrite>(DWORD)nRem ){
3295833053
lastErrno = osGetLastError();
@@ -32974,17 +33069,18 @@
3297433069
3297533070
if( rc ){
3297633071
if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
3297733072
|| ( pFile->lastErrno==ERROR_DISK_FULL )){
3297833073
OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
32979
- return SQLITE_FULL;
33074
+ return winLogError(SQLITE_FULL, pFile->lastErrno,
33075
+ "winWrite1", pFile->zPath);
3298033076
}
3298133077
OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
3298233078
return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
32983
- "winWrite", pFile->zPath);
33079
+ "winWrite2", pFile->zPath);
3298433080
}else{
32985
- logIoerr(nRetry);
33081
+ winLogIoerr(nRetry);
3298633082
}
3298733083
OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
3298833084
return SQLITE_OK;
3298933085
}
3299033086
@@ -33009,11 +33105,11 @@
3300933105
if( pFile->szChunk>0 ){
3301033106
nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
3301133107
}
3301233108
3301333109
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33014
- if( seekWinFile(pFile, nByte) ){
33110
+ if( winSeekFile(pFile, nByte) ){
3301533111
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
3301633112
"winTruncate1", pFile->zPath);
3301733113
}else if( 0==osSetEndOfFile(pFile->h) &&
3301833114
((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
3301933115
pFile->lastErrno = lastErrno;
@@ -33090,10 +33186,11 @@
3309033186
3309133187
/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
3309233188
** no-op
3309333189
*/
3309433190
#ifdef SQLITE_NO_SYNC
33191
+ OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
3309533192
return SQLITE_OK;
3309633193
#else
3309733194
rc = osFlushFileBuffers(pFile->h);
3309833195
SimulateIOError( rc=FALSE );
3309933196
if( rc ){
@@ -33101,11 +33198,11 @@
3310133198
return SQLITE_OK;
3310233199
}else{
3310333200
pFile->lastErrno = osGetLastError();
3310433201
OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
3310533202
return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33106
- "winSync", pFile->zPath);
33203
+ "winSync", pFile->zPath);
3310733204
}
3310833205
#endif
3310933206
}
3311033207
3311133208
/*
@@ -33142,11 +33239,11 @@
3314233239
*pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
3314333240
if( (lowerBits == INVALID_FILE_SIZE)
3314433241
&& ((lastErrno = osGetLastError())!=NO_ERROR) ){
3314533242
pFile->lastErrno = lastErrno;
3314633243
rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33147
- "winFileSize", pFile->zPath);
33244
+ "winFileSize", pFile->zPath);
3314833245
}
3314933246
}
3315033247
#endif
3315133248
OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
3315233249
pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
@@ -33187,14 +33284,14 @@
3318733284
/*
3318833285
** Acquire a reader lock.
3318933286
** Different API routines are called depending on whether or not this
3319033287
** is Win9x or WinNT.
3319133288
*/
33192
-static int getReadLock(winFile *pFile){
33289
+static int winGetReadLock(winFile *pFile){
3319333290
int res;
3319433291
OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33195
- if( isNT() ){
33292
+ if( osIsNT() ){
3319633293
#if SQLITE_OS_WINCE
3319733294
/*
3319833295
** NOTE: Windows CE is handled differently here due its lack of the Win32
3319933296
** API LockFileEx.
3320033297
*/
@@ -33222,15 +33319,15 @@
3322233319
}
3322333320
3322433321
/*
3322533322
** Undo a readlock
3322633323
*/
33227
-static int unlockReadLock(winFile *pFile){
33324
+static int winUnlockReadLock(winFile *pFile){
3322833325
int res;
3322933326
DWORD lastErrno;
3323033327
OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33231
- if( isNT() ){
33328
+ if( osIsNT() ){
3323233329
res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
3323333330
}
3323433331
#ifdef SQLITE_WIN32_HAS_ANSI
3323533332
else{
3323633333
res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33237,11 +33334,11 @@
3323733334
}
3323833335
#endif
3323933336
if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
3324033337
pFile->lastErrno = lastErrno;
3324133338
winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33242
- "unlockReadLock", pFile->zPath);
33339
+ "winUnlockReadLock", pFile->zPath);
3324333340
}
3324433341
OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
3324533342
return res;
3324633343
}
3324733344
@@ -33328,11 +33425,11 @@
3332833425
3332933426
/* Acquire a shared lock
3333033427
*/
3333133428
if( locktype==SHARED_LOCK && res ){
3333233429
assert( pFile->locktype==NO_LOCK );
33333
- res = getReadLock(pFile);
33430
+ res = winGetReadLock(pFile);
3333433431
if( res ){
3333533432
newLocktype = SHARED_LOCK;
3333633433
}else{
3333733434
lastErrno = osGetLastError();
3333833435
}
@@ -33359,18 +33456,18 @@
3335933456
3336033457
/* Acquire an EXCLUSIVE lock
3336133458
*/
3336233459
if( locktype==EXCLUSIVE_LOCK && res ){
3336333460
assert( pFile->locktype>=SHARED_LOCK );
33364
- res = unlockReadLock(pFile);
33461
+ res = winUnlockReadLock(pFile);
3336533462
res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
3336633463
SHARED_SIZE, 0);
3336733464
if( res ){
3336833465
newLocktype = EXCLUSIVE_LOCK;
3336933466
}else{
3337033467
lastErrno = osGetLastError();
33371
- getReadLock(pFile);
33468
+ winGetReadLock(pFile);
3337233469
}
3337333470
}
3337433471
3337533472
/* If we are holding a PENDING lock that ought to be released, then
3337633473
** release it now.
@@ -33383,14 +33480,14 @@
3338333480
** return the appropriate result code.
3338433481
*/
3338533482
if( res ){
3338633483
rc = SQLITE_OK;
3338733484
}else{
33485
+ pFile->lastErrno = lastErrno;
33486
+ rc = SQLITE_BUSY;
3338833487
OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
3338933488
pFile->h, locktype, newLocktype));
33390
- pFile->lastErrno = lastErrno;
33391
- rc = SQLITE_BUSY;
3339233489
}
3339333490
pFile->locktype = (u8)newLocktype;
3339433491
OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
3339533492
pFile->h, pFile->locktype, sqlite3ErrName(rc)));
3339633493
return rc;
@@ -33446,22 +33543,22 @@
3344633543
OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
3344733544
pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
3344833545
type = pFile->locktype;
3344933546
if( type>=EXCLUSIVE_LOCK ){
3345033547
winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33451
- if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33548
+ if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
3345233549
/* This should never happen. We should always be able to
3345333550
** reacquire the read lock */
3345433551
rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
33455
- "winUnlock", pFile->zPath);
33552
+ "winUnlock", pFile->zPath);
3345633553
}
3345733554
}
3345833555
if( type>=RESERVED_LOCK ){
3345933556
winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
3346033557
}
3346133558
if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33462
- unlockReadLock(pFile);
33559
+ winUnlockReadLock(pFile);
3346333560
}
3346433561
if( type>=PENDING_LOCK ){
3346533562
winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
3346633563
}
3346733564
pFile->locktype = (u8)locktype;
@@ -33484,15 +33581,14 @@
3348433581
}else{
3348533582
pFile->ctrlFlags |= mask;
3348633583
}
3348733584
}
3348833585
33489
-/* Forward declaration */
33490
-static int getTempname(int nBuf, char *zBuf);
33491
-#if SQLITE_MAX_MMAP_SIZE>0
33492
-static int winMapfile(winFile*, sqlite3_int64);
33493
-#endif
33586
+/* Forward references to VFS helper methods used for temporary files */
33587
+static int winGetTempname(sqlite3_vfs *, char **);
33588
+static int winIsDir(const void *);
33589
+static BOOL winIsDriveLetterAndColon(const char *);
3349433590
3349533591
/*
3349633592
** Control and query of the open file handle.
3349733593
*/
3349833594
static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33548,30 +33644,30 @@
3354833644
return SQLITE_OK;
3354933645
}
3355033646
case SQLITE_FCNTL_WIN32_AV_RETRY: {
3355133647
int *a = (int*)pArg;
3355233648
if( a[0]>0 ){
33553
- win32IoerrRetry = a[0];
33649
+ winIoerrRetry = a[0];
3355433650
}else{
33555
- a[0] = win32IoerrRetry;
33651
+ a[0] = winIoerrRetry;
3355633652
}
3355733653
if( a[1]>0 ){
33558
- win32IoerrRetryDelay = a[1];
33654
+ winIoerrRetryDelay = a[1];
3355933655
}else{
33560
- a[1] = win32IoerrRetryDelay;
33656
+ a[1] = winIoerrRetryDelay;
3356133657
}
3356233658
OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
3356333659
return SQLITE_OK;
3356433660
}
3356533661
case SQLITE_FCNTL_TEMPFILENAME: {
33566
- char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
33567
- if( zTFile ){
33568
- getTempname(pFile->pVfs->mxPathname, zTFile);
33662
+ char *zTFile = 0;
33663
+ int rc = winGetTempname(pFile->pVfs, &zTFile);
33664
+ if( rc==SQLITE_OK ){
3356933665
*(char**)pArg = zTFile;
3357033666
}
33571
- OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33572
- return SQLITE_OK;
33667
+ OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
33668
+ return rc;
3357333669
}
3357433670
#if SQLITE_MAX_MMAP_SIZE>0
3357533671
case SQLITE_FCNTL_MMAP_SIZE: {
3357633672
i64 newLimit = *(i64*)pArg;
3357733673
int rc = SQLITE_OK;
@@ -33584,11 +33680,11 @@
3358433680
if( pFile->mmapSize>0 ){
3358533681
(void)winUnmapfile(pFile);
3358633682
rc = winMapfile(pFile, -1);
3358733683
}
3358833684
}
33589
- OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
33685
+ OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
3359033686
return rc;
3359133687
}
3359233688
#endif
3359333689
}
3359433690
OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
@@ -33900,11 +33996,11 @@
3390033996
*/
3390133997
if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
3390233998
rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
3390333999
if( rc!=SQLITE_OK ){
3390434000
rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
33905
- "winOpenShm", pDbFd->zPath);
34001
+ "winOpenShm", pDbFd->zPath);
3390634002
}
3390734003
}
3390834004
if( rc==SQLITE_OK ){
3390934005
winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
3391034006
rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -34160,11 +34256,11 @@
3416034256
** large enough to contain the requested region).
3416134257
*/
3416234258
rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
3416334259
if( rc!=SQLITE_OK ){
3416434260
rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34165
- "winShmMap1", pDbFd->zPath);
34261
+ "winShmMap1", pDbFd->zPath);
3416634262
goto shmpage_out;
3416734263
}
3416834264
3416934265
if( sz<nByte ){
3417034266
/* The requested memory region does not exist. If isWrite is set to
@@ -34175,11 +34271,11 @@
3417534271
*/
3417634272
if( !isWrite ) goto shmpage_out;
3417734273
rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
3417834274
if( rc!=SQLITE_OK ){
3417934275
rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34180
- "winShmMap2", pDbFd->zPath);
34276
+ "winShmMap2", pDbFd->zPath);
3418134277
goto shmpage_out;
3418234278
}
3418334279
}
3418434280
3418534281
/* Map the requested memory region into this processes address space. */
@@ -34229,11 +34325,11 @@
3422934325
szRegion, pMap ? "ok" : "failed"));
3423034326
}
3423134327
if( !pMap ){
3423234328
pShmNode->lastErrno = osGetLastError();
3423334329
rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
34234
- "winShmMap3", pDbFd->zPath);
34330
+ "winShmMap3", pDbFd->zPath);
3423534331
if( hMap ) osCloseHandle(hMap);
3423634332
goto shmpage_out;
3423734333
}
3423834334
3423934335
pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
@@ -34277,11 +34373,11 @@
3427734373
pFile->lastErrno = osGetLastError();
3427834374
OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
3427934375
"rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
3428034376
pFile->pMapRegion));
3428134377
return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34282
- "winUnmap1", pFile->zPath);
34378
+ "winUnmapfile1", pFile->zPath);
3428334379
}
3428434380
pFile->pMapRegion = 0;
3428534381
pFile->mmapSize = 0;
3428634382
pFile->mmapSizeActual = 0;
3428734383
}
@@ -34289,11 +34385,11 @@
3428934385
if( !osCloseHandle(pFile->hMap) ){
3429034386
pFile->lastErrno = osGetLastError();
3429134387
OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
3429234388
osGetCurrentProcessId(), pFile, pFile->hMap));
3429334389
return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34294
- "winUnmap2", pFile->zPath);
34390
+ "winUnmapfile2", pFile->zPath);
3429534391
}
3429634392
pFile->hMap = NULL;
3429734393
}
3429834394
OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
3429934395
osGetCurrentProcessId(), pFile));
@@ -34364,14 +34460,14 @@
3436434460
(DWORD)(nMap & 0xffffffff), NULL);
3436534461
#endif
3436634462
if( pFd->hMap==NULL ){
3436734463
pFd->lastErrno = osGetLastError();
3436834464
rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34369
- "winMapfile", pFd->zPath);
34465
+ "winMapfile1", pFd->zPath);
3437034466
/* Log the error, but continue normal operation using xRead/xWrite */
34371
- OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
34372
- osGetCurrentProcessId(), pFd));
34467
+ OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
34468
+ osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
3437334469
return SQLITE_OK;
3437434470
}
3437534471
assert( (nMap % winSysInfo.dwPageSize)==0 );
3437634472
assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
3437734473
#if SQLITE_OS_WINRT
@@ -34381,14 +34477,15 @@
3438134477
#endif
3438234478
if( pNew==NULL ){
3438334479
osCloseHandle(pFd->hMap);
3438434480
pFd->hMap = NULL;
3438534481
pFd->lastErrno = osGetLastError();
34386
- winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34387
- "winMapfile", pFd->zPath);
34388
- OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
34389
- osGetCurrentProcessId(), pFd));
34482
+ rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34483
+ "winMapfile2", pFd->zPath);
34484
+ /* Log the error, but continue normal operation using xRead/xWrite */
34485
+ OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
34486
+ osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
3439034487
return SQLITE_OK;
3439134488
}
3439234489
pFd->pMapRegion = pNew;
3439334490
pFd->mmapSize = nMap;
3439434491
pFd->mmapSizeActual = nMap;
@@ -34522,21 +34619,40 @@
3452234619
**************************** sqlite3_vfs methods ****************************
3452334620
**
3452434621
** This division contains the implementation of methods on the
3452534622
** sqlite3_vfs object.
3452634623
*/
34624
+
34625
+/*
34626
+** Convert a filename from whatever the underlying operating system
34627
+** supports for filenames into UTF-8. Space to hold the result is
34628
+** obtained from malloc and must be freed by the calling function.
34629
+*/
34630
+static char *winConvertToUtf8Filename(const void *zFilename){
34631
+ char *zConverted = 0;
34632
+ if( osIsNT() ){
34633
+ zConverted = winUnicodeToUtf8(zFilename);
34634
+ }
34635
+#ifdef SQLITE_WIN32_HAS_ANSI
34636
+ else{
34637
+ zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
34638
+ }
34639
+#endif
34640
+ /* caller will handle out of memory */
34641
+ return zConverted;
34642
+}
3452734643
3452834644
/*
3452934645
** Convert a UTF-8 filename into whatever form the underlying
3453034646
** operating system wants filenames in. Space to hold the result
3453134647
** is obtained from malloc and must be freed by the calling
3453234648
** function.
3453334649
*/
34534
-static void *convertUtf8Filename(const char *zFilename){
34650
+static void *winConvertFromUtf8Filename(const char *zFilename){
3453534651
void *zConverted = 0;
34536
- if( isNT() ){
34537
- zConverted = utf8ToUnicode(zFilename);
34652
+ if( osIsNT() ){
34653
+ zConverted = winUtf8ToUnicode(zFilename);
3453834654
}
3453934655
#ifdef SQLITE_WIN32_HAS_ANSI
3454034656
else{
3454134657
zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
3454234658
}
@@ -34544,117 +34660,220 @@
3454434660
/* caller will handle out of memory */
3454534661
return zConverted;
3454634662
}
3454734663
3454834664
/*
34549
-** Maximum pathname length (in bytes) for windows. The MAX_PATH macro is
34550
-** in characters, so we allocate 3 bytes per character assuming worst-case
34551
-** 3-bytes-per-character UTF8.
34665
+** This function returns non-zero if the specified UTF-8 string buffer
34666
+** ends with a directory separator character.
3455234667
*/
34553
-#ifndef SQLITE_WIN32_MAX_PATH
34554
-# define SQLITE_WIN32_MAX_PATH (MAX_PATH*3)
34555
-#endif
34668
+static int winEndsInDirSep(char *zBuf){
34669
+ if( zBuf ){
34670
+ int nLen = sqlite3Strlen30(zBuf);
34671
+ return nLen>0 && winIsDirSep(zBuf[nLen-1]);
34672
+ }
34673
+ return 0;
34674
+}
3455634675
3455734676
/*
34558
-** Create a temporary file name in zBuf. zBuf must be big enough to
34559
-** hold at pVfs->mxPathname characters.
34677
+** Create a temporary file name and store the resulting pointer into pzBuf.
34678
+** The pointer returned in pzBuf must be freed via sqlite3_free().
3456034679
*/
34561
-static int getTempname(int nBuf, char *zBuf){
34680
+static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
3456234681
static char zChars[] =
3456334682
"abcdefghijklmnopqrstuvwxyz"
3456434683
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
3456534684
"0123456789";
3456634685
size_t i, j;
34567
- int nTempPath;
34568
- char zTempPath[SQLITE_WIN32_MAX_PATH+2];
34686
+ int nBuf, nLen;
34687
+ char *zBuf;
3456934688
3457034689
/* It's odd to simulate an io-error here, but really this is just
3457134690
** using the io-error infrastructure to test that SQLite handles this
3457234691
** function failing.
3457334692
*/
3457434693
SimulateIOError( return SQLITE_IOERR );
3457534694
34695
+ /* Allocate a temporary buffer to store the fully qualified file
34696
+ ** name for the temporary file. If this fails, we cannot continue.
34697
+ */
34698
+ nBuf = pVfs->mxPathname;
34699
+ zBuf = sqlite3MallocZero( nBuf+2 );
34700
+ if( !zBuf ){
34701
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34702
+ return SQLITE_IOERR_NOMEM;
34703
+ }
34704
+
34705
+ /* Figure out the effective temporary directory. First, check if one
34706
+ ** has been explicitly set by the application; otherwise, use the one
34707
+ ** configured by the operating system.
34708
+ */
34709
+ assert( nBuf>30 );
3457634710
if( sqlite3_temp_directory ){
34577
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
34578
- sqlite3_temp_directory);
34711
+ sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
34712
+ winEndsInDirSep(sqlite3_temp_directory) ? "" :
34713
+ winGetDirDep());
3457934714
}
34580
-#if !SQLITE_OS_WINRT
34581
- else if( isNT() ){
34715
+#if defined(__CYGWIN__)
34716
+ else{
34717
+ static const char *azDirs[] = {
34718
+ 0, /* getenv("SQLITE_TMPDIR") */
34719
+ 0, /* getenv("TMPDIR") */
34720
+ 0, /* getenv("TMP") */
34721
+ 0, /* getenv("TEMP") */
34722
+ 0, /* getenv("USERPROFILE") */
34723
+ "/var/tmp",
34724
+ "/usr/tmp",
34725
+ "/tmp",
34726
+ ".",
34727
+ 0 /* List terminator */
34728
+ };
34729
+ unsigned int i;
34730
+ const char *zDir = 0;
34731
+
34732
+ if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
34733
+ if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
34734
+ if( !azDirs[2] ) azDirs[2] = getenv("TMP");
34735
+ if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
34736
+ if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
34737
+ for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
34738
+ void *zConverted;
34739
+ if( zDir==0 ) continue;
34740
+ /* If the path starts with a drive letter followed by the colon
34741
+ ** character, assume it is already a native Win32 path; otherwise,
34742
+ ** it must be converted to a native Win32 path prior via the Cygwin
34743
+ ** API prior to using it.
34744
+ */
34745
+ if( winIsDriveLetterAndColon(zDir) ){
34746
+ zConverted = winConvertFromUtf8Filename(zDir);
34747
+ if( !zConverted ){
34748
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34749
+ return SQLITE_IOERR_NOMEM;
34750
+ }
34751
+ if( winIsDir(zConverted) ){
34752
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zDir);
34753
+ sqlite3_free(zConverted);
34754
+ break;
34755
+ }
34756
+ sqlite3_free(zConverted);
34757
+ }else{
34758
+ zConverted = sqlite3MallocZero( nBuf+1 );
34759
+ if( !zConverted ){
34760
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34761
+ return SQLITE_IOERR_NOMEM;
34762
+ }
34763
+ if( cygwin_conv_path(
34764
+ osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
34765
+ zConverted, nBuf+1)<0 ){
34766
+ sqlite3_free(zConverted);
34767
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
34768
+ return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
34769
+ "winGetTempname1", zDir);
34770
+ }
34771
+ if( winIsDir(zConverted) ){
34772
+ /* At this point, we know the candidate directory exists and should
34773
+ ** be used. However, we may need to convert the string containing
34774
+ ** its name into UTF-8 (i.e. if it is UTF-16 right now).
34775
+ */
34776
+ if( osIsNT() ){
34777
+ char *zUtf8 = winUnicodeToUtf8(zConverted);
34778
+ if( !zUtf8 ){
34779
+ sqlite3_free(zConverted);
34780
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34781
+ return SQLITE_IOERR_NOMEM;
34782
+ }
34783
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
34784
+ sqlite3_free(zUtf8);
34785
+ sqlite3_free(zConverted);
34786
+ break;
34787
+ }else{
34788
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zConverted);
34789
+ sqlite3_free(zConverted);
34790
+ break;
34791
+ }
34792
+ }
34793
+ sqlite3_free(zConverted);
34794
+ }
34795
+ break;
34796
+ }
34797
+ }
34798
+#elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
34799
+ else if( osIsNT() ){
3458234800
char *zMulti;
34583
- WCHAR zWidePath[MAX_PATH];
34584
- if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
34801
+ LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
34802
+ if( !zWidePath ){
34803
+ sqlite3_free(zBuf);
34804
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34805
+ return SQLITE_IOERR_NOMEM;
34806
+ }
34807
+ if( osGetTempPathW(nBuf, zWidePath)==0 ){
34808
+ sqlite3_free(zWidePath);
34809
+ sqlite3_free(zBuf);
3458534810
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34586
- return SQLITE_IOERR_GETTEMPPATH;
34811
+ return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
34812
+ "winGetTempname1", 0);
3458734813
}
34588
- zMulti = unicodeToUtf8(zWidePath);
34814
+ zMulti = winUnicodeToUtf8(zWidePath);
3458934815
if( zMulti ){
34590
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
34816
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
3459134817
sqlite3_free(zMulti);
34818
+ sqlite3_free(zWidePath);
3459234819
}else{
34820
+ sqlite3_free(zWidePath);
34821
+ sqlite3_free(zBuf);
3459334822
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
3459434823
return SQLITE_IOERR_NOMEM;
3459534824
}
3459634825
}
3459734826
#ifdef SQLITE_WIN32_HAS_ANSI
3459834827
else{
3459934828
char *zUtf8;
34600
- char zMbcsPath[SQLITE_WIN32_MAX_PATH];
34601
- if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
34829
+ char *zMbcsPath = sqlite3MallocZero( nBuf );
34830
+ if( !zMbcsPath ){
34831
+ sqlite3_free(zBuf);
34832
+ OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34833
+ return SQLITE_IOERR_NOMEM;
34834
+ }
34835
+ if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
34836
+ sqlite3_free(zBuf);
3460234837
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34603
- return SQLITE_IOERR_GETTEMPPATH;
34838
+ return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
34839
+ "winGetTempname2", 0);
3460434840
}
3460534841
zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
3460634842
if( zUtf8 ){
34607
- sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
34843
+ sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
3460834844
sqlite3_free(zUtf8);
3460934845
}else{
34846
+ sqlite3_free(zBuf);
3461034847
OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
3461134848
return SQLITE_IOERR_NOMEM;
3461234849
}
3461334850
}
34614
-#else
34615
- else{
34616
- /*
34617
- ** Compiled without ANSI support and the current operating system
34618
- ** is not Windows NT; therefore, just zero the temporary buffer.
34619
- */
34620
- memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34621
- }
3462234851
#endif /* SQLITE_WIN32_HAS_ANSI */
34623
-#else
34624
- else{
34625
- /*
34626
- ** Compiled for WinRT and the sqlite3_temp_directory is not set;
34627
- ** therefore, just zero the temporary buffer.
34628
- */
34629
- memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34630
- }
3463134852
#endif /* !SQLITE_OS_WINRT */
3463234853
3463334854
/* Check that the output buffer is large enough for the temporary file
3463434855
** name. If it is not, return SQLITE_ERROR.
3463534856
*/
34636
- nTempPath = sqlite3Strlen30(zTempPath);
34857
+ nLen = sqlite3Strlen30(zBuf);
3463734858
34638
- if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34859
+ if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34860
+ sqlite3_free(zBuf);
3463934861
OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
34640
- return SQLITE_ERROR;
34862
+ return winLogError(SQLITE_ERROR, 0, "winGetTempname3", 0);
3464134863
}
3464234864
34643
- for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
34644
- zTempPath[i] = 0;
34865
+ sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
3464534866
34646
- sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
34647
- "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
34648
- zTempPath);
3464934867
j = sqlite3Strlen30(zBuf);
3465034868
sqlite3_randomness(15, &zBuf[j]);
3465134869
for(i=0; i<15; i++, j++){
3465234870
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
3465334871
}
3465434872
zBuf[j] = 0;
3465534873
zBuf[j+1] = 0;
34874
+ *pzBuf = zBuf;
3465634875
3465734876
OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
3465834877
return SQLITE_OK;
3465934878
}
3466034879
@@ -34666,17 +34885,17 @@
3466634885
static int winIsDir(const void *zConverted){
3466734886
DWORD attr;
3466834887
int rc = 0;
3466934888
DWORD lastErrno;
3467034889
34671
- if( isNT() ){
34890
+ if( osIsNT() ){
3467234891
int cnt = 0;
3467334892
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3467434893
memset(&sAttrData, 0, sizeof(sAttrData));
3467534894
while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
3467634895
GetFileExInfoStandard,
34677
- &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
34896
+ &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
3467834897
if( !rc ){
3467934898
return 0; /* Invalid name? */
3468034899
}
3468134900
attr = sAttrData.dwFileAttributes;
3468234901
#if SQLITE_OS_WINCE==0
@@ -34689,11 +34908,11 @@
3468934908
3469034909
/*
3469134910
** Open a file.
3469234911
*/
3469334912
static int winOpen(
34694
- sqlite3_vfs *pVfs, /* Not used */
34913
+ sqlite3_vfs *pVfs, /* Used to get maximum path name length */
3469534914
const char *zName, /* Name of the file (UTF-8) */
3469634915
sqlite3_file *id, /* Write the SQLite file handle here */
3469734916
int flags, /* Open mode flags */
3469834917
int *pOutFlags /* Status return flags */
3469934918
){
@@ -34712,11 +34931,11 @@
3471234931
int cnt = 0;
3471334932
3471434933
/* If argument zPath is a NULL pointer, this function is required to open
3471534934
** a temporary file. Use this buffer to store the file name in.
3471634935
*/
34717
- char zTmpname[SQLITE_WIN32_MAX_PATH+2]; /* Buffer used to create temp filename */
34936
+ char *zTmpname = 0; /* For temporary filename, if necessary. */
3471834937
3471934938
int rc = SQLITE_OK; /* Function Return Code */
3472034939
#if !defined(NDEBUG) || SQLITE_OS_WINCE
3472134940
int eType = flags&0xFFFFFF00; /* Type of file to open */
3472234941
#endif
@@ -34767,22 +34986,22 @@
3476734986
assert( pFile!=0 );
3476834987
memset(pFile, 0, sizeof(winFile));
3476934988
pFile->h = INVALID_HANDLE_VALUE;
3477034989
3477134990
#if SQLITE_OS_WINRT
34772
- if( !sqlite3_temp_directory ){
34991
+ if( !zUtf8Name && !sqlite3_temp_directory ){
3477334992
sqlite3_log(SQLITE_ERROR,
3477434993
"sqlite3_temp_directory variable should be set for WinRT");
3477534994
}
3477634995
#endif
3477734996
3477834997
/* If the second argument to this function is NULL, generate a
3477934998
** temporary file name to use
3478034999
*/
3478135000
if( !zUtf8Name ){
34782
- assert(isDelete && !isOpenJournal);
34783
- rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
35001
+ assert( isDelete && !isOpenJournal );
35002
+ rc = winGetTempname(pVfs, &zTmpname);
3478435003
if( rc!=SQLITE_OK ){
3478535004
OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
3478635005
return rc;
3478735006
}
3478835007
zUtf8Name = zTmpname;
@@ -34791,21 +35010,23 @@
3479135010
/* Database filenames are double-zero terminated if they are not
3479235011
** URIs with parameters. Hence, they can always be passed into
3479335012
** sqlite3_uri_parameter().
3479435013
*/
3479535014
assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
34796
- zUtf8Name[strlen(zUtf8Name)+1]==0 );
35015
+ zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
3479735016
3479835017
/* Convert the filename to the system encoding. */
34799
- zConverted = convertUtf8Filename(zUtf8Name);
35018
+ zConverted = winConvertFromUtf8Filename(zUtf8Name);
3480035019
if( zConverted==0 ){
35020
+ sqlite3_free(zTmpname);
3480135021
OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
3480235022
return SQLITE_IOERR_NOMEM;
3480335023
}
3480435024
3480535025
if( winIsDir(zConverted) ){
3480635026
sqlite3_free(zConverted);
35027
+ sqlite3_free(zTmpname);
3480735028
OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
3480835029
return SQLITE_CANTOPEN_ISDIR;
3480935030
}
3481035031
3481135032
if( isReadWrite ){
@@ -34848,11 +35069,11 @@
3484835069
** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
3484935070
#if SQLITE_OS_WINCE
3485035071
dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
3485135072
#endif
3485235073
34853
- if( isNT() ){
35074
+ if( osIsNT() ){
3485435075
#if SQLITE_OS_WINRT
3485535076
CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
3485635077
extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
3485735078
extendedParameters.dwFileAttributes =
3485835079
dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34863,21 +35084,21 @@
3486335084
while( (h = osCreateFile2((LPCWSTR)zConverted,
3486435085
dwDesiredAccess,
3486535086
dwShareMode,
3486635087
dwCreationDisposition,
3486735088
&extendedParameters))==INVALID_HANDLE_VALUE &&
34868
- retryIoerr(&cnt, &lastErrno) ){
35089
+ winRetryIoerr(&cnt, &lastErrno) ){
3486935090
/* Noop */
3487035091
}
3487135092
#else
3487235093
while( (h = osCreateFileW((LPCWSTR)zConverted,
3487335094
dwDesiredAccess,
3487435095
dwShareMode, NULL,
3487535096
dwCreationDisposition,
3487635097
dwFlagsAndAttributes,
3487735098
NULL))==INVALID_HANDLE_VALUE &&
34878
- retryIoerr(&cnt, &lastErrno) ){
35099
+ winRetryIoerr(&cnt, &lastErrno) ){
3487935100
/* Noop */
3488035101
}
3488135102
#endif
3488235103
}
3488335104
#ifdef SQLITE_WIN32_HAS_ANSI
@@ -34886,24 +35107,25 @@
3488635107
dwDesiredAccess,
3488735108
dwShareMode, NULL,
3488835109
dwCreationDisposition,
3488935110
dwFlagsAndAttributes,
3489035111
NULL))==INVALID_HANDLE_VALUE &&
34891
- retryIoerr(&cnt, &lastErrno) ){
35112
+ winRetryIoerr(&cnt, &lastErrno) ){
3489235113
/* Noop */
3489335114
}
3489435115
}
3489535116
#endif
34896
- logIoerr(cnt);
35117
+ winLogIoerr(cnt);
3489735118
3489835119
OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
3489935120
dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
3490035121
3490135122
if( h==INVALID_HANDLE_VALUE ){
3490235123
pFile->lastErrno = lastErrno;
3490335124
winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
3490435125
sqlite3_free(zConverted);
35126
+ sqlite3_free(zTmpname);
3490535127
if( isReadWrite && !isExclusive ){
3490635128
return winOpen(pVfs, zName, id,
3490735129
((flags|SQLITE_OPEN_READONLY) &
3490835130
~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
3490935131
pOutFlags);
@@ -34928,19 +35150,21 @@
3492835150
if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
3492935151
&& (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
3493035152
){
3493135153
osCloseHandle(h);
3493235154
sqlite3_free(zConverted);
35155
+ sqlite3_free(zTmpname);
3493335156
OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
3493435157
return rc;
3493535158
}
3493635159
if( isTemp ){
3493735160
pFile->zDeleteOnClose = zConverted;
3493835161
}else
3493935162
#endif
3494035163
{
3494135164
sqlite3_free(zConverted);
35165
+ sqlite3_free(zTmpname);
3494235166
}
3494335167
3494435168
pFile->pMethod = &winIoMethod;
3494535169
pFile->pVfs = pVfs;
3494635170
pFile->h = h;
@@ -34990,15 +35214,16 @@
3499035214
UNUSED_PARAMETER(syncDir);
3499135215
3499235216
SimulateIOError(return SQLITE_IOERR_DELETE);
3499335217
OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
3499435218
34995
- zConverted = convertUtf8Filename(zFilename);
35219
+ zConverted = winConvertFromUtf8Filename(zFilename);
3499635220
if( zConverted==0 ){
35221
+ OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
3499735222
return SQLITE_IOERR_NOMEM;
3499835223
}
34999
- if( isNT() ){
35224
+ if( osIsNT() ){
3500035225
do {
3500135226
#if SQLITE_OS_WINRT
3500235227
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3500335228
memset(&sAttrData, 0, sizeof(sAttrData));
3500435229
if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35033,11 +35258,11 @@
3503335258
}
3503435259
if ( osDeleteFileW(zConverted) ){
3503535260
rc = SQLITE_OK; /* Deleted OK. */
3503635261
break;
3503735262
}
35038
- if ( !retryIoerr(&cnt, &lastErrno) ){
35263
+ if ( !winRetryIoerr(&cnt, &lastErrno) ){
3503935264
rc = SQLITE_ERROR; /* No more retries. */
3504035265
break;
3504135266
}
3504235267
} while(1);
3504335268
}
@@ -35061,22 +35286,21 @@
3506135286
}
3506235287
if ( osDeleteFileA(zConverted) ){
3506335288
rc = SQLITE_OK; /* Deleted OK. */
3506435289
break;
3506535290
}
35066
- if ( !retryIoerr(&cnt, &lastErrno) ){
35291
+ if ( !winRetryIoerr(&cnt, &lastErrno) ){
3506735292
rc = SQLITE_ERROR; /* No more retries. */
3506835293
break;
3506935294
}
3507035295
} while(1);
3507135296
}
3507235297
#endif
3507335298
if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
35074
- rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35075
- "winDelete", zFilename);
35299
+ rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
3507635300
}else{
35077
- logIoerr(cnt);
35301
+ winLogIoerr(cnt);
3507835302
}
3507935303
sqlite3_free(zConverted);
3508035304
OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
3508135305
return rc;
3508235306
}
@@ -35098,22 +35322,22 @@
3509835322
3509935323
SimulateIOError( return SQLITE_IOERR_ACCESS; );
3510035324
OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
3510135325
zFilename, flags, pResOut));
3510235326
35103
- zConverted = convertUtf8Filename(zFilename);
35327
+ zConverted = winConvertFromUtf8Filename(zFilename);
3510435328
if( zConverted==0 ){
3510535329
OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
3510635330
return SQLITE_IOERR_NOMEM;
3510735331
}
35108
- if( isNT() ){
35332
+ if( osIsNT() ){
3510935333
int cnt = 0;
3511035334
WIN32_FILE_ATTRIBUTE_DATA sAttrData;
3511135335
memset(&sAttrData, 0, sizeof(sAttrData));
3511235336
while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
3511335337
GetFileExInfoStandard,
35114
- &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
35338
+ &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
3511535339
if( rc ){
3511635340
/* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
3511735341
** as if it does not exist.
3511835342
*/
3511935343
if( flags==SQLITE_ACCESS_EXISTS
@@ -35122,15 +35346,15 @@
3512235346
attr = INVALID_FILE_ATTRIBUTES;
3512335347
}else{
3512435348
attr = sAttrData.dwFileAttributes;
3512535349
}
3512635350
}else{
35127
- logIoerr(cnt);
35351
+ winLogIoerr(cnt);
3512835352
if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
35129
- winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
3513035353
sqlite3_free(zConverted);
35131
- return SQLITE_IOERR_ACCESS;
35354
+ return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
35355
+ zFilename);
3513235356
}else{
3513335357
attr = INVALID_FILE_ATTRIBUTES;
3513435358
}
3513535359
}
3513635360
}
@@ -35156,10 +35380,19 @@
3515635380
OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
3515735381
zFilename, pResOut, *pResOut));
3515835382
return SQLITE_OK;
3515935383
}
3516035384
35385
+/*
35386
+** Returns non-zero if the specified path name starts with a drive letter
35387
+** followed by a colon character.
35388
+*/
35389
+static BOOL winIsDriveLetterAndColon(
35390
+ const char *zPathname
35391
+){
35392
+ return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
35393
+}
3516135394
3516235395
/*
3516335396
** Returns non-zero if the specified path name should be used verbatim. If
3516435397
** non-zero is returned from this function, the calling function must simply
3516535398
** use the provided path name verbatim -OR- resolve it into a full path name
@@ -35173,21 +35406,21 @@
3517335406
** a legal UNC name, a volume relative path, or an absolute path name in the
3517435407
** "Unix" format on Windows. There is no easy way to differentiate between
3517535408
** the final two cases; therefore, we return the safer return value of TRUE
3517635409
** so that callers of this function will simply use it verbatim.
3517735410
*/
35178
- if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
35411
+ if ( winIsDirSep(zPathname[0]) ){
3517935412
return TRUE;
3518035413
}
3518135414
3518235415
/*
3518335416
** If the path name starts with a letter and a colon it is either a volume
3518435417
** relative path or an absolute path. Callers of this function must not
3518535418
** attempt to treat it as a relative path name (i.e. they should simply use
3518635419
** it verbatim).
3518735420
*/
35188
- if ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ){
35421
+ if ( winIsDriveLetterAndColon(zPathname) ){
3518935422
return TRUE;
3519035423
}
3519135424
3519235425
/*
3519335426
** If we get to this point, the path name should almost certainly be a purely
@@ -35209,33 +35442,35 @@
3520935442
){
3521035443
3521135444
#if defined(__CYGWIN__)
3521235445
SimulateIOError( return SQLITE_ERROR );
3521335446
UNUSED_PARAMETER(nFull);
35214
- assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
3521535447
assert( nFull>=pVfs->mxPathname );
3521635448
if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
3521735449
/*
3521835450
** NOTE: We are dealing with a relative path name and the data
3521935451
** directory has been set. Therefore, use it as the basis
3522035452
** for converting the relative path name to an absolute
3522135453
** one by prepending the data directory and a slash.
3522235454
*/
35223
- char zOut[SQLITE_WIN32_MAX_PATH+1];
35455
+ char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
35456
+ if( !zOut ){
35457
+ return SQLITE_IOERR_NOMEM;
35458
+ }
3522435459
if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35225
- SQLITE_WIN32_MAX_PATH+1)<0 ){
35226
- winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35227
- zRelative);
35228
- return SQLITE_CANTOPEN_FULLPATH;
35460
+ pVfs->mxPathname+1)<0 ){
35461
+ sqlite3_free(zOut);
35462
+ return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
35463
+ "winFullPathname1", zRelative);
3522935464
}
35230
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35231
- sqlite3_data_directory, zOut);
35465
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35466
+ sqlite3_data_directory, winGetDirDep(), zOut);
35467
+ sqlite3_free(zOut);
3523235468
}else{
3523335469
if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
35234
- winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35235
- zRelative);
35236
- return SQLITE_CANTOPEN_FULLPATH;
35470
+ return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
35471
+ "winFullPathname2", zRelative);
3523735472
}
3523835473
}
3523935474
return SQLITE_OK;
3524035475
#endif
3524135476
@@ -35248,12 +35483,12 @@
3524835483
** NOTE: We are dealing with a relative path name and the data
3524935484
** directory has been set. Therefore, use it as the basis
3525035485
** for converting the relative path name to an absolute
3525135486
** one by prepending the data directory and a backslash.
3525235487
*/
35253
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35254
- sqlite3_data_directory, zRelative);
35488
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35489
+ sqlite3_data_directory, winGetDirDep(), zRelative);
3525535490
}else{
3525635491
sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
3525735492
}
3525835493
return SQLITE_OK;
3525935494
#endif
@@ -35264,11 +35499,11 @@
3526435499
char *zOut;
3526535500
3526635501
/* If this path name begins with "/X:", where "X" is any alphabetic
3526735502
** character, discard the initial "/" from the pathname.
3526835503
*/
35269
- if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
35504
+ if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
3527035505
zRelative++;
3527135506
}
3527235507
3527335508
/* It's odd to simulate an io-error here, but really this is just
3527435509
** using the io-error infrastructure to test that SQLite handles this
@@ -35281,68 +35516,64 @@
3528135516
** NOTE: We are dealing with a relative path name and the data
3528235517
** directory has been set. Therefore, use it as the basis
3528335518
** for converting the relative path name to an absolute
3528435519
** one by prepending the data directory and a backslash.
3528535520
*/
35286
- sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35287
- sqlite3_data_directory, zRelative);
35521
+ sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35522
+ sqlite3_data_directory, winGetDirDep(), zRelative);
3528835523
return SQLITE_OK;
3528935524
}
35290
- zConverted = convertUtf8Filename(zRelative);
35525
+ zConverted = winConvertFromUtf8Filename(zRelative);
3529135526
if( zConverted==0 ){
3529235527
return SQLITE_IOERR_NOMEM;
3529335528
}
35294
- if( isNT() ){
35529
+ if( osIsNT() ){
3529535530
LPWSTR zTemp;
3529635531
nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
3529735532
if( nByte==0 ){
35298
- winLogError(SQLITE_ERROR, osGetLastError(),
35299
- "GetFullPathNameW1", zConverted);
3530035533
sqlite3_free(zConverted);
35301
- return SQLITE_CANTOPEN_FULLPATH;
35534
+ return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35535
+ "winFullPathname1", zRelative);
3530235536
}
3530335537
nByte += 3;
3530435538
zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
3530535539
if( zTemp==0 ){
3530635540
sqlite3_free(zConverted);
3530735541
return SQLITE_IOERR_NOMEM;
3530835542
}
3530935543
nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
3531035544
if( nByte==0 ){
35311
- winLogError(SQLITE_ERROR, osGetLastError(),
35312
- "GetFullPathNameW2", zConverted);
3531335545
sqlite3_free(zConverted);
3531435546
sqlite3_free(zTemp);
35315
- return SQLITE_CANTOPEN_FULLPATH;
35547
+ return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35548
+ "winFullPathname2", zRelative);
3531635549
}
3531735550
sqlite3_free(zConverted);
35318
- zOut = unicodeToUtf8(zTemp);
35551
+ zOut = winUnicodeToUtf8(zTemp);
3531935552
sqlite3_free(zTemp);
3532035553
}
3532135554
#ifdef SQLITE_WIN32_HAS_ANSI
3532235555
else{
3532335556
char *zTemp;
3532435557
nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
3532535558
if( nByte==0 ){
35326
- winLogError(SQLITE_ERROR, osGetLastError(),
35327
- "GetFullPathNameA1", zConverted);
3532835559
sqlite3_free(zConverted);
35329
- return SQLITE_CANTOPEN_FULLPATH;
35560
+ return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35561
+ "winFullPathname3", zRelative);
3533035562
}
3533135563
nByte += 3;
3533235564
zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
3533335565
if( zTemp==0 ){
3533435566
sqlite3_free(zConverted);
3533535567
return SQLITE_IOERR_NOMEM;
3533635568
}
3533735569
nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
3533835570
if( nByte==0 ){
35339
- winLogError(SQLITE_ERROR, osGetLastError(),
35340
- "GetFullPathNameA2", zConverted);
3534135571
sqlite3_free(zConverted);
3534235572
sqlite3_free(zTemp);
35343
- return SQLITE_CANTOPEN_FULLPATH;
35573
+ return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35574
+ "winFullPathname4", zRelative);
3534435575
}
3534535576
sqlite3_free(zConverted);
3534635577
zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
3534735578
sqlite3_free(zTemp);
3534835579
}
@@ -35366,16 +35597,16 @@
3536635597
** Interfaces for opening a shared library, finding entry points
3536735598
** within the shared library, and closing the shared library.
3536835599
*/
3536935600
static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
3537035601
HANDLE h;
35371
- void *zConverted = convertUtf8Filename(zFilename);
35602
+ void *zConverted = winConvertFromUtf8Filename(zFilename);
3537235603
UNUSED_PARAMETER(pVfs);
3537335604
if( zConverted==0 ){
3537435605
return 0;
3537535606
}
35376
- if( isNT() ){
35607
+ if( osIsNT() ){
3537735608
#if SQLITE_OS_WINRT
3537835609
h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
3537935610
#else
3538035611
h = osLoadLibraryW((LPCWSTR)zConverted);
3538135612
#endif
@@ -35388,11 +35619,11 @@
3538835619
sqlite3_free(zConverted);
3538935620
return (void*)h;
3539035621
}
3539135622
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
3539235623
UNUSED_PARAMETER(pVfs);
35393
- getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35624
+ winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
3539435625
}
3539535626
static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
3539635627
UNUSED_PARAMETER(pVfs);
3539735628
return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
3539835629
}
@@ -35564,21 +35795,21 @@
3556435795
** by sqlite into the error message available to the user using
3556535796
** sqlite3_errmsg(), possibly making IO errors easier to debug.
3556635797
*/
3556735798
static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
3556835799
UNUSED_PARAMETER(pVfs);
35569
- return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
35800
+ return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
3557035801
}
3557135802
3557235803
/*
3557335804
** Initialize and deinitialize the operating system interface.
3557435805
*/
3557535806
SQLITE_API int sqlite3_os_init(void){
3557635807
static sqlite3_vfs winVfs = {
3557735808
3, /* iVersion */
3557835809
sizeof(winFile), /* szOsFile */
35579
- SQLITE_WIN32_MAX_PATH, /* mxPathname */
35810
+ SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
3558035811
0, /* pNext */
3558135812
"win32", /* zName */
3558235813
0, /* pAppData */
3558335814
winOpen, /* xOpen */
3558435815
winDelete, /* xDelete */
@@ -35595,10 +35826,36 @@
3559535826
winCurrentTimeInt64, /* xCurrentTimeInt64 */
3559635827
winSetSystemCall, /* xSetSystemCall */
3559735828
winGetSystemCall, /* xGetSystemCall */
3559835829
winNextSystemCall, /* xNextSystemCall */
3559935830
};
35831
+#if defined(SQLITE_WIN32_HAS_WIDE)
35832
+ static sqlite3_vfs winLongPathVfs = {
35833
+ 3, /* iVersion */
35834
+ sizeof(winFile), /* szOsFile */
35835
+ SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
35836
+ 0, /* pNext */
35837
+ "win32-longpath", /* zName */
35838
+ 0, /* pAppData */
35839
+ winOpen, /* xOpen */
35840
+ winDelete, /* xDelete */
35841
+ winAccess, /* xAccess */
35842
+ winFullPathname, /* xFullPathname */
35843
+ winDlOpen, /* xDlOpen */
35844
+ winDlError, /* xDlError */
35845
+ winDlSym, /* xDlSym */
35846
+ winDlClose, /* xDlClose */
35847
+ winRandomness, /* xRandomness */
35848
+ winSleep, /* xSleep */
35849
+ winCurrentTime, /* xCurrentTime */
35850
+ winGetLastError, /* xGetLastError */
35851
+ winCurrentTimeInt64, /* xCurrentTimeInt64 */
35852
+ winSetSystemCall, /* xSetSystemCall */
35853
+ winGetSystemCall, /* xGetSystemCall */
35854
+ winNextSystemCall, /* xNextSystemCall */
35855
+ };
35856
+#endif
3560035857
3560135858
/* Double-check that the aSyscall[] array has been constructed
3560235859
** correctly. See ticket [bb3a86e890c8e96ab] */
3560335860
assert( ArraySize(aSyscall)==74 );
3560435861
@@ -35611,10 +35868,15 @@
3561135868
#endif
3561235869
assert( winSysInfo.dwAllocationGranularity>0 );
3561335870
assert( winSysInfo.dwPageSize>0 );
3561435871
3561535872
sqlite3_vfs_register(&winVfs, 1);
35873
+
35874
+#if defined(SQLITE_WIN32_HAS_WIDE)
35875
+ sqlite3_vfs_register(&winLongPathVfs, 0);
35876
+#endif
35877
+
3561635878
return SQLITE_OK;
3561735879
}
3561835880
3561935881
SQLITE_API int sqlite3_os_end(void){
3562035882
#if SQLITE_OS_WINRT
@@ -52086,10 +52348,11 @@
5208652348
pBt->max1bytePayload = (u8)pBt->maxLocal;
5208752349
}
5208852350
assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
5208952351
pBt->pPage1 = pPage1;
5209052352
pBt->nPage = nPage;
52353
+assert( pPage1->leaf==0 || pPage1->leaf==1 );
5209152354
return SQLITE_OK;
5209252355
5209352356
page1_init_failed:
5209452357
releasePage(pPage1);
5209552358
pBt->pPage1 = 0;
@@ -59838,43 +60101,108 @@
5983860101
}
5983960102
return p;
5984060103
}
5984160104
5984260105
/*
59843
-** Create a new sqlite3_value object, containing the value of pExpr.
60106
+** Context object passed by sqlite3Stat4ProbeSetValue() through to
60107
+** valueNew(). See comments above valueNew() for details.
60108
+*/
60109
+struct ValueNewStat4Ctx {
60110
+ Parse *pParse;
60111
+ Index *pIdx;
60112
+ UnpackedRecord **ppRec;
60113
+ int iVal;
60114
+};
60115
+
60116
+/*
60117
+** Allocate and return a pointer to a new sqlite3_value object. If
60118
+** the second argument to this function is NULL, the object is allocated
60119
+** by calling sqlite3ValueNew().
5984460120
**
59845
-** This only works for very simple expressions that consist of one constant
59846
-** token (i.e. "5", "5.1", "'a string'"). If the expression can
59847
-** be converted directly into a value, then the value is allocated and
59848
-** a pointer written to *ppVal. The caller is responsible for deallocating
59849
-** the value by passing it to sqlite3ValueFree() later on. If the expression
59850
-** cannot be converted to a value, then *ppVal is set to NULL.
60121
+** Otherwise, if the second argument is non-zero, then this function is
60122
+** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
60123
+** already been allocated, allocate the UnpackedRecord structure that
60124
+** that function will return to its caller here. Then return a pointer
60125
+** an sqlite3_value within the UnpackedRecord.a[] array.
5985160126
*/
59852
-SQLITE_PRIVATE int sqlite3ValueFromExpr(
59853
- sqlite3 *db, /* The database connection */
59854
- Expr *pExpr, /* The expression to evaluate */
59855
- u8 enc, /* Encoding to use */
59856
- u8 affinity, /* Affinity to use */
59857
- sqlite3_value **ppVal /* Write the new value here */
60127
+static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60128
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60129
+ if( p ){
60130
+ UnpackedRecord *pRec = p->ppRec[0];
60131
+
60132
+ if( pRec==0 ){
60133
+ Index *pIdx = p->pIdx; /* Index being probed */
60134
+ int nByte; /* Bytes of space to allocate */
60135
+ int i; /* Counter variable */
60136
+ int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */
60137
+
60138
+ nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord);
60139
+ pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60140
+ if( pRec ){
60141
+ pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx);
60142
+ if( pRec->pKeyInfo ){
60143
+ assert( pRec->pKeyInfo->nField+1==nCol );
60144
+ pRec->pKeyInfo->enc = ENC(db);
60145
+ pRec->flags = UNPACKED_PREFIX_MATCH;
60146
+ pRec->aMem = (Mem *)&pRec[1];
60147
+ for(i=0; i<nCol; i++){
60148
+ pRec->aMem[i].flags = MEM_Null;
60149
+ pRec->aMem[i].type = SQLITE_NULL;
60150
+ pRec->aMem[i].db = db;
60151
+ }
60152
+ }else{
60153
+ sqlite3DbFree(db, pRec);
60154
+ pRec = 0;
60155
+ }
60156
+ }
60157
+ if( pRec==0 ) return 0;
60158
+ p->ppRec[0] = pRec;
60159
+ }
60160
+
60161
+ pRec->nField = p->iVal+1;
60162
+ return &pRec->aMem[p->iVal];
60163
+ }
60164
+#endif
60165
+ return sqlite3ValueNew(db);
60166
+}
60167
+
60168
+/*
60169
+** Extract a value from the supplied expression in the manner described
60170
+** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
60171
+** using valueNew().
60172
+**
60173
+** If pCtx is NULL and an error occurs after the sqlite3_value object
60174
+** has been allocated, it is freed before returning. Or, if pCtx is not
60175
+** NULL, it is assumed that the caller will free any allocated object
60176
+** in all cases.
60177
+*/
60178
+int valueFromExpr(
60179
+ sqlite3 *db, /* The database connection */
60180
+ Expr *pExpr, /* The expression to evaluate */
60181
+ u8 enc, /* Encoding to use */
60182
+ u8 affinity, /* Affinity to use */
60183
+ sqlite3_value **ppVal, /* Write the new value here */
60184
+ struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
5985860185
){
5985960186
int op;
5986060187
char *zVal = 0;
5986160188
sqlite3_value *pVal = 0;
5986260189
int negInt = 1;
5986360190
const char *zNeg = "";
60191
+ int rc = SQLITE_OK;
5986460192
5986560193
if( !pExpr ){
5986660194
*ppVal = 0;
5986760195
return SQLITE_OK;
5986860196
}
5986960197
op = pExpr->op;
5987060198
59871
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
60199
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
5987260200
** The ifdef here is to enable us to achieve 100% branch test coverage even
59873
- ** when SQLITE_ENABLE_STAT3 is omitted.
60201
+ ** when SQLITE_ENABLE_STAT4 is omitted.
5987460202
*/
59875
-#ifdef SQLITE_ENABLE_STAT3
60203
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
5987660204
if( op==TK_REGISTER ) op = pExpr->op2;
5987760205
#else
5987860206
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5987960207
#endif
5988060208
@@ -59888,11 +60216,11 @@
5988860216
negInt = -1;
5988960217
zNeg = "-";
5989060218
}
5989160219
5989260220
if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
59893
- pVal = sqlite3ValueNew(db);
60221
+ pVal = valueNew(db, pCtx);
5989460222
if( pVal==0 ) goto no_mem;
5989560223
if( ExprHasProperty(pExpr, EP_IntValue) ){
5989660224
sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
5989760225
}else{
5989860226
zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59905,15 +60233,17 @@
5990560233
}else{
5990660234
sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
5990760235
}
5990860236
if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
5990960237
if( enc!=SQLITE_UTF8 ){
59910
- sqlite3VdbeChangeEncoding(pVal, enc);
60238
+ rc = sqlite3VdbeChangeEncoding(pVal, enc);
5991160239
}
5991260240
}else if( op==TK_UMINUS ) {
5991360241
/* This branch happens for multiple negative signs. Ex: -(-5) */
59914
- if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
60242
+ if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
60243
+ && pVal!=0
60244
+ ){
5991560245
sqlite3VdbeMemNumerify(pVal);
5991660246
if( pVal->u.i==SMALLEST_INT64 ){
5991760247
pVal->flags &= MEM_Int;
5991860248
pVal->flags |= MEM_Real;
5991960249
pVal->r = (double)LARGEST_INT64;
@@ -59922,19 +60252,19 @@
5992260252
}
5992360253
pVal->r = -pVal->r;
5992460254
sqlite3ValueApplyAffinity(pVal, affinity, enc);
5992560255
}
5992660256
}else if( op==TK_NULL ){
59927
- pVal = sqlite3ValueNew(db);
60257
+ pVal = valueNew(db, pCtx);
5992860258
if( pVal==0 ) goto no_mem;
5992960259
}
5993060260
#ifndef SQLITE_OMIT_BLOB_LITERAL
5993160261
else if( op==TK_BLOB ){
5993260262
int nVal;
5993360263
assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
5993460264
assert( pExpr->u.zToken[1]=='\'' );
59935
- pVal = sqlite3ValueNew(db);
60265
+ pVal = valueNew(db, pCtx);
5993660266
if( !pVal ) goto no_mem;
5993760267
zVal = &pExpr->u.zToken[2];
5993860268
nVal = sqlite3Strlen30(zVal)-1;
5993960269
assert( zVal[nVal]=='\'' );
5994060270
sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59944,20 +60274,206 @@
5994460274
5994560275
if( pVal ){
5994660276
sqlite3VdbeMemStoreType(pVal);
5994760277
}
5994860278
*ppVal = pVal;
59949
- return SQLITE_OK;
60279
+ return rc;
5995060280
5995160281
no_mem:
5995260282
db->mallocFailed = 1;
5995360283
sqlite3DbFree(db, zVal);
59954
- sqlite3ValueFree(pVal);
59955
- *ppVal = 0;
60284
+ assert( *ppVal==0 );
60285
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60286
+ if( pCtx==0 ) sqlite3ValueFree(pVal);
60287
+#else
60288
+ assert( pCtx==0 ); sqlite3ValueFree(pVal);
60289
+#endif
5995660290
return SQLITE_NOMEM;
5995760291
}
5995860292
60293
+/*
60294
+** Create a new sqlite3_value object, containing the value of pExpr.
60295
+**
60296
+** This only works for very simple expressions that consist of one constant
60297
+** token (i.e. "5", "5.1", "'a string'"). If the expression can
60298
+** be converted directly into a value, then the value is allocated and
60299
+** a pointer written to *ppVal. The caller is responsible for deallocating
60300
+** the value by passing it to sqlite3ValueFree() later on. If the expression
60301
+** cannot be converted to a value, then *ppVal is set to NULL.
60302
+*/
60303
+SQLITE_PRIVATE int sqlite3ValueFromExpr(
60304
+ sqlite3 *db, /* The database connection */
60305
+ Expr *pExpr, /* The expression to evaluate */
60306
+ u8 enc, /* Encoding to use */
60307
+ u8 affinity, /* Affinity to use */
60308
+ sqlite3_value **ppVal /* Write the new value here */
60309
+){
60310
+ return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
60311
+}
60312
+
60313
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60314
+/*
60315
+** The implementation of the sqlite_record() function. This function accepts
60316
+** a single argument of any type. The return value is a formatted database
60317
+** record (a blob) containing the argument value.
60318
+**
60319
+** This is used to convert the value stored in the 'sample' column of the
60320
+** sqlite_stat3 table to the record format SQLite uses internally.
60321
+*/
60322
+static void recordFunc(
60323
+ sqlite3_context *context,
60324
+ int argc,
60325
+ sqlite3_value **argv
60326
+){
60327
+ const int file_format = 1;
60328
+ int iSerial; /* Serial type */
60329
+ int nSerial; /* Bytes of space for iSerial as varint */
60330
+ int nVal; /* Bytes of space required for argv[0] */
60331
+ int nRet;
60332
+ sqlite3 *db;
60333
+ u8 *aRet;
60334
+
60335
+ iSerial = sqlite3VdbeSerialType(argv[0], file_format);
60336
+ nSerial = sqlite3VarintLen(iSerial);
60337
+ nVal = sqlite3VdbeSerialTypeLen(iSerial);
60338
+ db = sqlite3_context_db_handle(context);
60339
+
60340
+ nRet = 1 + nSerial + nVal;
60341
+ aRet = sqlite3DbMallocRaw(db, nRet);
60342
+ if( aRet==0 ){
60343
+ sqlite3_result_error_nomem(context);
60344
+ }else{
60345
+ aRet[0] = nSerial+1;
60346
+ sqlite3PutVarint(&aRet[1], iSerial);
60347
+ sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
60348
+ sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
60349
+ sqlite3DbFree(db, aRet);
60350
+ }
60351
+}
60352
+
60353
+/*
60354
+** Register built-in functions used to help read ANALYZE data.
60355
+*/
60356
+SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
60357
+ static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
60358
+ FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
60359
+ };
60360
+ int i;
60361
+ FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
60362
+ FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
60363
+ for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
60364
+ sqlite3FuncDefInsert(pHash, &aFunc[i]);
60365
+ }
60366
+}
60367
+
60368
+/*
60369
+** This function is used to allocate and populate UnpackedRecord
60370
+** structures intended to be compared against sample index keys stored
60371
+** in the sqlite_stat4 table.
60372
+**
60373
+** A single call to this function attempts to populates field iVal (leftmost
60374
+** is 0 etc.) of the unpacked record with a value extracted from expression
60375
+** pExpr. Extraction of values is possible if:
60376
+**
60377
+** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
60378
+**
60379
+** * The expression is a bound variable, and this is a reprepare, or
60380
+**
60381
+** * The sqlite3ValueFromExpr() function is able to extract a value
60382
+** from the expression (i.e. the expression is a literal value).
60383
+**
60384
+** If a value can be extracted, the affinity passed as the 5th argument
60385
+** is applied to it before it is copied into the UnpackedRecord. Output
60386
+** parameter *pbOk is set to true if a value is extracted, or false
60387
+** otherwise.
60388
+**
60389
+** When this function is called, *ppRec must either point to an object
60390
+** allocated by an earlier call to this function, or must be NULL. If it
60391
+** is NULL and a value can be successfully extracted, a new UnpackedRecord
60392
+** is allocated (and *ppRec set to point to it) before returning.
60393
+**
60394
+** Unless an error is encountered, SQLITE_OK is returned. It is not an
60395
+** error if a value cannot be extracted from pExpr. If an error does
60396
+** occur, an SQLite error code is returned.
60397
+*/
60398
+SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
60399
+ Parse *pParse, /* Parse context */
60400
+ Index *pIdx, /* Index being probed */
60401
+ UnpackedRecord **ppRec, /* IN/OUT: Probe record */
60402
+ Expr *pExpr, /* The expression to extract a value from */
60403
+ u8 affinity, /* Affinity to use */
60404
+ int iVal, /* Array element to populate */
60405
+ int *pbOk /* OUT: True if value was extracted */
60406
+){
60407
+ int rc = SQLITE_OK;
60408
+ sqlite3_value *pVal = 0;
60409
+
60410
+ struct ValueNewStat4Ctx alloc;
60411
+ alloc.pParse = pParse;
60412
+ alloc.pIdx = pIdx;
60413
+ alloc.ppRec = ppRec;
60414
+ alloc.iVal = iVal;
60415
+
60416
+ /* Skip over any TK_COLLATE nodes */
60417
+ pExpr = sqlite3ExprSkipCollate(pExpr);
60418
+
60419
+ if( !pExpr ){
60420
+ pVal = valueNew(pParse->db, &alloc);
60421
+ if( pVal ){
60422
+ sqlite3VdbeMemSetNull((Mem*)pVal);
60423
+ *pbOk = 1;
60424
+ }
60425
+ }else if( pExpr->op==TK_VARIABLE
60426
+ || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
60427
+ ){
60428
+ Vdbe *v;
60429
+ int iBindVar = pExpr->iColumn;
60430
+ sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
60431
+ if( (v = pParse->pReprepare)!=0 ){
60432
+ pVal = valueNew(pParse->db, &alloc);
60433
+ if( pVal ){
60434
+ rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
60435
+ if( rc==SQLITE_OK ){
60436
+ sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60437
+ }
60438
+ pVal->db = pParse->db;
60439
+ *pbOk = 1;
60440
+ sqlite3VdbeMemStoreType((Mem*)pVal);
60441
+ }
60442
+ }else{
60443
+ *pbOk = 0;
60444
+ }
60445
+ }else{
60446
+ sqlite3 *db = pParse->db;
60447
+ rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
60448
+ *pbOk = (pVal!=0);
60449
+ }
60450
+
60451
+ assert( pVal==0 || pVal->db==pParse->db );
60452
+ return rc;
60453
+}
60454
+
60455
+/*
60456
+** Unless it is NULL, the argument must be an UnpackedRecord object returned
60457
+** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
60458
+** the object.
60459
+*/
60460
+SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
60461
+ if( pRec ){
60462
+ int i;
60463
+ int nCol = pRec->pKeyInfo->nField+1;
60464
+ Mem *aMem = pRec->aMem;
60465
+ sqlite3 *db = aMem[0].db;
60466
+ for(i=0; i<nCol; i++){
60467
+ sqlite3DbFree(db, aMem[i].zMalloc);
60468
+ }
60469
+ sqlite3DbFree(db, pRec->pKeyInfo);
60470
+ sqlite3DbFree(db, pRec);
60471
+ }
60472
+}
60473
+#endif /* ifdef SQLITE_ENABLE_STAT4 */
60474
+
5995960475
/*
5996060476
** Change the string value of an sqlite3_value object
5996160477
*/
5996260478
SQLITE_PRIVATE void sqlite3ValueSetStr(
5996360479
sqlite3_value *v, /* Value to be set */
@@ -66073,11 +66589,11 @@
6607366589
** value or convert mem[p2] to a different type.
6607466590
*/
6607566591
assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
6607666592
if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
6607766593
assert( pOp->p2>0 );
66078
- assert( pOp->p2<=p->nMem );
66594
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6607966595
pOut = &aMem[pOp->p2];
6608066596
memAboutToChange(p, pOut);
6608166597
VdbeMemRelease(pOut);
6608266598
pOut->flags = MEM_Int;
6608366599
}
@@ -66084,34 +66600,34 @@
6608466600
6608566601
/* Sanity checking on other operands */
6608666602
#ifdef SQLITE_DEBUG
6608766603
if( (pOp->opflags & OPFLG_IN1)!=0 ){
6608866604
assert( pOp->p1>0 );
66089
- assert( pOp->p1<=p->nMem );
66605
+ assert( pOp->p1<=(p->nMem-p->nCursor) );
6609066606
assert( memIsValid(&aMem[pOp->p1]) );
6609166607
REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
6609266608
}
6609366609
if( (pOp->opflags & OPFLG_IN2)!=0 ){
6609466610
assert( pOp->p2>0 );
66095
- assert( pOp->p2<=p->nMem );
66611
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6609666612
assert( memIsValid(&aMem[pOp->p2]) );
6609766613
REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
6609866614
}
6609966615
if( (pOp->opflags & OPFLG_IN3)!=0 ){
6610066616
assert( pOp->p3>0 );
66101
- assert( pOp->p3<=p->nMem );
66617
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6610266618
assert( memIsValid(&aMem[pOp->p3]) );
6610366619
REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
6610466620
}
6610566621
if( (pOp->opflags & OPFLG_OUT2)!=0 ){
6610666622
assert( pOp->p2>0 );
66107
- assert( pOp->p2<=p->nMem );
66623
+ assert( pOp->p2<=(p->nMem-p->nCursor) );
6610866624
memAboutToChange(p, &aMem[pOp->p2]);
6610966625
}
6611066626
if( (pOp->opflags & OPFLG_OUT3)!=0 ){
6611166627
assert( pOp->p3>0 );
66112
- assert( pOp->p3<=p->nMem );
66628
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6611366629
memAboutToChange(p, &aMem[pOp->p3]);
6611466630
}
6611566631
#endif
6611666632
6611766633
switch( pOp->opcode ){
@@ -66200,11 +66716,11 @@
6620066716
**
6620166717
** Write the current address onto register P1
6620266718
** and then jump to address P2.
6620366719
*/
6620466720
case OP_Gosub: { /* jump */
66205
- assert( pOp->p1>0 && pOp->p1<=p->nMem );
66721
+ assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
6620666722
pIn1 = &aMem[pOp->p1];
6620766723
assert( (pIn1->flags & MEM_Dyn)==0 );
6620866724
memAboutToChange(p, pIn1);
6620966725
pIn1->flags = MEM_Int;
6621066726
pIn1->u.i = pc;
@@ -66416,11 +66932,11 @@
6641666932
#if 0 /* local variables moved into u.ab */
6641766933
int cnt;
6641866934
u16 nullFlag;
6641966935
#endif /* local variables moved into u.ab */
6642066936
u.ab.cnt = pOp->p3-pOp->p2;
66421
- assert( pOp->p3<=p->nMem );
66937
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6642266938
pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
6642366939
while( u.ab.cnt>0 ){
6642466940
pOut++;
6642566941
memAboutToChange(p, pOut);
6642666942
VdbeMemRelease(pOut);
@@ -66489,12 +67005,12 @@
6648967005
assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
6649067006
6649167007
pIn1 = &aMem[u.ad.p1];
6649267008
pOut = &aMem[u.ad.p2];
6649367009
while( u.ad.n-- ){
66494
- assert( pOut<=&aMem[p->nMem] );
66495
- assert( pIn1<=&aMem[p->nMem] );
67010
+ assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
67011
+ assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
6649667012
assert( memIsValid(pIn1) );
6649767013
memAboutToChange(p, pOut);
6649867014
u.ad.zMalloc = pOut->zMalloc;
6649967015
pOut->zMalloc = 0;
6650067016
sqlite3VdbeMemMove(pOut, pIn1);
@@ -66578,11 +67094,11 @@
6657867094
Mem *pMem;
6657967095
int i;
6658067096
#endif /* local variables moved into u.af */
6658167097
assert( p->nResColumn==pOp->p2 );
6658267098
assert( pOp->p1>0 );
66583
- assert( pOp->p1+pOp->p2<=p->nMem+1 );
67099
+ assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
6658467100
6658567101
/* If this statement has violated immediate foreign key constraints, do
6658667102
** not return the number of rows modified. And do not RELEASE the statement
6658767103
** transaction. It needs to be rolled back. */
6658867104
if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66858,15 +67374,15 @@
6685867374
#endif /* local variables moved into u.ai */
6685967375
6686067376
u.ai.n = pOp->p5;
6686167377
u.ai.apVal = p->apArg;
6686267378
assert( u.ai.apVal || u.ai.n==0 );
66863
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
67379
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6686467380
pOut = &aMem[pOp->p3];
6686567381
memAboutToChange(p, pOut);
6686667382
66867
- assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
67383
+ assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=(p->nMem-p->nCursor)+1) );
6686867384
assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
6686967385
u.ai.pArg = &aMem[pOp->p2];
6687067386
for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
6687167387
assert( memIsValid(u.ai.pArg) );
6687267388
u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -67398,15 +67914,15 @@
6739867914
u.al.p2 = pOp->p2;
6739967915
#if SQLITE_DEBUG
6740067916
if( aPermute ){
6740167917
int k, mx = 0;
6740267918
for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67403
- assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
67404
- assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
67919
+ assert( u.al.p1>0 && u.al.p1+mx<=(p->nMem-p->nCursor)+1 );
67920
+ assert( u.al.p2>0 && u.al.p2+mx<=(p->nMem-p->nCursor)+1 );
6740567921
}else{
67406
- assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
67407
- assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
67922
+ assert( u.al.p1>0 && u.al.p1+u.al.n<=(p->nMem-p->nCursor)+1 );
67923
+ assert( u.al.p2>0 && u.al.p2+u.al.n<=(p->nMem-p->nCursor)+1 );
6740867924
}
6740967925
#endif /* SQLITE_DEBUG */
6741067926
for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
6741167927
u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
6741267928
assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67659,11 +68175,11 @@
6765968175
u.ao.p1 = pOp->p1;
6766068176
u.ao.p2 = pOp->p2;
6766168177
u.ao.pC = 0;
6766268178
memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
6766368179
assert( u.ao.p1<p->nCursor );
67664
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
68180
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6766568181
u.ao.pDest = &aMem[pOp->p3];
6766668182
memAboutToChange(p, u.ao.pDest);
6766768183
u.ao.zRec = 0;
6766868184
6766968185
/* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67959,11 +68475,11 @@
6795968475
u.ap.zAffinity = pOp->p4.z;
6796068476
assert( u.ap.zAffinity!=0 );
6796168477
assert( u.ap.zAffinity[pOp->p2]==0 );
6796268478
pIn1 = &aMem[pOp->p1];
6796368479
while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
67964
- assert( pIn1 <= &p->aMem[p->nMem] );
68480
+ assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
6796568481
assert( memIsValid(pIn1) );
6796668482
ExpandBlob(pIn1);
6796768483
applyAffinity(pIn1, u.ap.cAff, encoding);
6796868484
pIn1++;
6796968485
}
@@ -68022,11 +68538,11 @@
6802268538
u.aq.nData = 0; /* Number of bytes of data space */
6802368539
u.aq.nHdr = 0; /* Number of bytes of header space */
6802468540
u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
6802568541
u.aq.nField = pOp->p1;
6802668542
u.aq.zAffinity = pOp->p4.z;
68027
- assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
68543
+ assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=(p->nMem-p->nCursor)+1 );
6802868544
u.aq.pData0 = &aMem[u.aq.nField];
6802968545
u.aq.nField = pOp->p2;
6803068546
u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
6803168547
u.aq.file_format = p->minWriteFileFormat;
6803268548
@@ -68088,11 +68604,11 @@
6808868604
for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
6808968605
u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
6809068606
}
6809168607
assert( u.aq.i==u.aq.nByte );
6809268608
68093
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
68609
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
6809468610
pOut->n = (int)u.aq.nByte;
6809568611
pOut->flags = MEM_Blob | MEM_Dyn;
6809668612
pOut->xDel = 0;
6809768613
if( u.aq.nZero ){
6809868614
pOut->u.nZero = u.aq.nZero;
@@ -68684,11 +69200,11 @@
6868469200
}else{
6868569201
u.ay.wrFlag = 0;
6868669202
}
6868769203
if( pOp->p5 & OPFLAG_P2ISREG ){
6868869204
assert( u.ay.p2>0 );
68689
- assert( u.ay.p2<=p->nMem );
69205
+ assert( u.ay.p2<=(p->nMem-p->nCursor) );
6869069206
pIn2 = &aMem[u.ay.p2];
6869169207
assert( memIsValid(pIn2) );
6869269208
assert( (pIn2->flags & MEM_Int)!=0 );
6869369209
sqlite3VdbeMemIntegerify(pIn2);
6869469210
u.ay.p2 = (int)pIn2->u.i;
@@ -69235,11 +69751,11 @@
6923569751
6923669752
pIn3 = &aMem[pOp->p3];
6923769753
u.bf.aMx = &aMem[pOp->p4.i];
6923869754
/* Assert that the values of parameters P1 and P4 are in range. */
6923969755
assert( pOp->p4type==P4_INT32 );
69240
- assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
69756
+ assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
6924169757
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
6924269758
6924369759
/* Find the index cursor. */
6924469760
u.bf.pCx = p->apCsr[pOp->p1];
6924569761
assert( u.bf.pCx->deferredMoveto==0 );
@@ -69442,11 +69958,11 @@
6944269958
/* Assert that P3 is a valid memory cell. */
6944369959
assert( pOp->p3<=u.bh.pFrame->nMem );
6944469960
u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
6944569961
}else{
6944669962
/* Assert that P3 is a valid memory cell. */
69447
- assert( pOp->p3<=p->nMem );
69963
+ assert( pOp->p3<=(p->nMem-p->nCursor) );
6944869964
u.bh.pMem = &aMem[pOp->p3];
6944969965
memAboutToChange(p, u.bh.pMem);
6945069966
}
6945169967
assert( memIsValid(u.bh.pMem) );
6945269968
@@ -70120,11 +70636,11 @@
7012070636
int res;
7012170637
UnpackedRecord r;
7012270638
#endif /* local variables moved into u.bt */
7012370639
7012470640
assert( pOp->p3>0 );
70125
- assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
70641
+ assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
7012670642
assert( pOp->p1>=0 && pOp->p1<p->nCursor );
7012770643
u.bt.pC = p->apCsr[pOp->p1];
7012870644
assert( u.bt.pC!=0 );
7012970645
u.bt.pCrsr = u.bt.pC->pCursor;
7013070646
if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70336,10 +70852,11 @@
7033670852
int nChange;
7033770853
#endif /* local variables moved into u.bx */
7033870854
7033970855
u.bx.nChange = 0;
7034070856
assert( p->readOnly==0 );
70857
+ assert( pOp->p1!=1 );
7034170858
assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
7034270859
rc = sqlite3BtreeClearTable(
7034370860
db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
7034470861
);
7034570862
if( pOp->p3 ){
@@ -70542,11 +71059,11 @@
7054271059
assert( p->bIsReader );
7054371060
u.ca.nRoot = pOp->p2;
7054471061
assert( u.ca.nRoot>0 );
7054571062
u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
7054671063
if( u.ca.aRoot==0 ) goto no_mem;
70547
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
71064
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7054871065
u.ca.pnErr = &aMem[pOp->p3];
7054971066
assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
7055071067
assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
7055171068
pIn1 = &aMem[pOp->p1];
7055271069
for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70978,11 +71495,11 @@
7097871495
u.cg.apVal[u.cg.i] = u.cg.pRec;
7097971496
memAboutToChange(p, u.cg.pRec);
7098071497
sqlite3VdbeMemStoreType(u.cg.pRec);
7098171498
}
7098271499
u.cg.ctx.pFunc = pOp->p4.pFunc;
70983
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
71500
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7098471501
u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
7098571502
u.cg.pMem->n++;
7098671503
u.cg.ctx.s.flags = MEM_Null;
7098771504
u.cg.ctx.s.z = 0;
7098871505
u.cg.ctx.s.zMalloc = 0;
@@ -71027,11 +71544,11 @@
7102771544
*/
7102871545
case OP_AggFinal: {
7102971546
#if 0 /* local variables moved into u.ch */
7103071547
Mem *pMem;
7103171548
#endif /* local variables moved into u.ch */
71032
- assert( pOp->p1>0 && pOp->p1<=p->nMem );
71549
+ assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
7103371550
u.ch.pMem = &aMem[pOp->p1];
7103471551
assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
7103571552
rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
7103671553
if( rc ){
7103771554
sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71458,11 +71975,11 @@
7145871975
sqlite3_context sContext;
7145971976
#endif /* local variables moved into u.co */
7146071977
7146171978
VdbeCursor *pCur = p->apCsr[pOp->p1];
7146271979
assert( pCur->pVtabCursor );
71463
- assert( pOp->p3>0 && pOp->p3<=p->nMem );
71980
+ assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
7146471981
u.co.pDest = &aMem[pOp->p3];
7146571982
memAboutToChange(p, u.co.pDest);
7146671983
if( pCur->nullRow ){
7146771984
sqlite3VdbeMemSetNull(u.co.pDest);
7146871985
break;
@@ -76729,11 +77246,11 @@
7672977246
break;
7673077247
}
7673177248
case TK_UMINUS: {
7673277249
int v;
7673377250
if( sqlite3ExprIsInteger(p->pLeft, &v) ){
76734
- assert( v!=-2147483648 );
77251
+ assert( v!=(-2147483647-1) );
7673577252
*pValue = -v;
7673677253
rc = 1;
7673777254
}
7673877255
break;
7673977256
}
@@ -80389,11 +80906,11 @@
8038980906
8039080907
/* Ensure the default expression is something that sqlite3ValueFromExpr()
8039180908
** can handle (i.e. not CURRENT_TIME etc.)
8039280909
*/
8039380910
if( pDflt ){
80394
- sqlite3_value *pVal;
80911
+ sqlite3_value *pVal = 0;
8039580912
if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
8039680913
db->mallocFailed = 1;
8039780914
return;
8039880915
}
8039980916
if( !pVal ){
@@ -80530,11 +81047,11 @@
8053081047
#endif /* SQLITE_ALTER_TABLE */
8053181048
8053281049
/************** End of alter.c ***********************************************/
8053381050
/************** Begin file analyze.c *****************************************/
8053481051
/*
80535
-** 2005 July 8
81052
+** 2005-07-08
8053681053
**
8053781054
** The author disclaims copyright to this source code. In place of
8053881055
** a legal notice, here is a blessing:
8053981056
**
8054081057
** May you do good and not evil.
@@ -80551,27 +81068,36 @@
8055181068
** The following system tables are or have been supported:
8055281069
**
8055381070
** CREATE TABLE sqlite_stat1(tbl, idx, stat);
8055481071
** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
8055581072
** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
81073
+** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
8055681074
**
8055781075
** Additional tables might be added in future releases of SQLite.
8055881076
** The sqlite_stat2 table is not created or used unless the SQLite version
8055981077
** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
8056081078
** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
8056181079
** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
8056281080
** created and used by SQLite versions 3.7.9 and later and with
80563
-** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
80564
-** is a superset of sqlite_stat2.
81081
+** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
81082
+** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
81083
+** version of sqlite_stat3 and is only available when compiled with
81084
+** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.0 and later. It is
81085
+** not possible to enable both STAT3 and STAT4 at the same time. If they
81086
+** are both enabled, then STAT4 takes precedence.
81087
+**
81088
+** For most applications, sqlite_stat1 provides all the statisics required
81089
+** for the query planner to make good choices.
8056581090
**
8056681091
** Format of sqlite_stat1:
8056781092
**
8056881093
** There is normally one row per index, with the index identified by the
8056981094
** name in the idx column. The tbl column is the name of the table to
8057081095
** which the index belongs. In each such row, the stat column will be
8057181096
** a string consisting of a list of integers. The first integer in this
80572
-** list is the number of rows in the index and in the table. The second
81097
+** list is the number of rows in the index. (This is the same as the
81098
+** number of rows in the table, except for partial indices.) The second
8057381099
** integer is the average number of rows in the index that have the same
8057481100
** value in the first column of the index. The third integer is the average
8057581101
** number of rows in the index that have the same value for the first two
8057681102
** columns. The N-th integer (for N>1) is the average number of rows in
8057781103
** the index which have the same value for the first N-1 columns. For
@@ -80614,57 +81140,85 @@
8061481140
** writes the sqlite_stat2 table. This version of SQLite only supports
8061581141
** sqlite_stat3.
8061681142
**
8061781143
** Format for sqlite_stat3:
8061881144
**
80619
-** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
80620
-** used to avoid compatibility problems.
81145
+** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
81146
+** sqlite_stat4 format will be described first. Further information
81147
+** about sqlite_stat3 follows the sqlite_stat4 description.
8062181148
**
80622
-** The format of the sqlite_stat3 table is similar to the format of
80623
-** the sqlite_stat2 table. There are multiple entries for each index.
81149
+** Format for sqlite_stat4:
81150
+**
81151
+** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
81152
+** to aid the query planner in choosing good indices based on the values
81153
+** that indexed columns are compared against in the WHERE clauses of
81154
+** queries.
81155
+**
81156
+** The sqlite_stat4 table contains multiple entries for each index.
8062481157
** The idx column names the index and the tbl column is the table of the
8062581158
** index. If the idx and tbl columns are the same, then the sample is
80626
-** of the INTEGER PRIMARY KEY. The sample column is a value taken from
80627
-** the left-most column of the index. The nEq column is the approximate
80628
-** number of entires in the index whose left-most column exactly matches
80629
-** the sample. nLt is the approximate number of entires whose left-most
80630
-** column is less than the sample. The nDLt column is the approximate
80631
-** number of distinct left-most entries in the index that are less than
80632
-** the sample.
80633
-**
80634
-** Future versions of SQLite might change to store a string containing
80635
-** multiple integers values in the nDLt column of sqlite_stat3. The first
80636
-** integer will be the number of prior index entires that are distinct in
80637
-** the left-most column. The second integer will be the number of prior index
80638
-** entries that are distinct in the first two columns. The third integer
80639
-** will be the number of prior index entries that are distinct in the first
80640
-** three columns. And so forth. With that extension, the nDLt field is
80641
-** similar in function to the sqlite_stat1.stat field.
80642
-**
80643
-** There can be an arbitrary number of sqlite_stat3 entries per index.
80644
-** The ANALYZE command will typically generate sqlite_stat3 tables
81159
+** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
81160
+** binary encoding of a key from the index, with the trailing rowid
81161
+** omitted. The nEq column is a list of integers. The first integer
81162
+** is the approximate number of entries in the index whose left-most
81163
+** column exactly matches the left-most column of the sample. The second
81164
+** integer in nEq is the approximate number of entries in the index where
81165
+** the first two columns match the first two columns of the sample.
81166
+** And so forth. nLt is another list of integers that show the approximate
81167
+** number of entries that are strictly less than the sample. The first
81168
+** integer in nLt contains the number of entries in the index where the
81169
+** left-most column is less than the left-most column of the sample.
81170
+** The K-th integer in the nLt entry is the number of index entries
81171
+** where the first K columns are less than the first K columns of the
81172
+** sample. The nDLt column is like nLt except that it contains the
81173
+** number of distinct entries in the index that are less than the
81174
+** sample.
81175
+**
81176
+** There can be an arbitrary number of sqlite_stat4 entries per index.
81177
+** The ANALYZE command will typically generate sqlite_stat4 tables
8064581178
** that contain between 10 and 40 samples which are distributed across
8064681179
** the key space, though not uniformly, and which include samples with
80647
-** largest possible nEq values.
81180
+** large nEq values.
81181
+**
81182
+** Format for sqlite_stat3 redux:
81183
+**
81184
+** The sqlite_stat3 table is like sqlite_stat4 except that it only
81185
+** looks at the left-most column of the index. The sqlite_stat3.sample
81186
+** column contains the actual value of the left-most column instead
81187
+** of a blob encoding of the complete index key as is found in
81188
+** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
81189
+** all contain just a single integer which is the same as the first
81190
+** integer in the equivalent columns in sqlite_stat4.
8064881191
*/
8064981192
#ifndef SQLITE_OMIT_ANALYZE
8065081193
81194
+#if defined(SQLITE_ENABLE_STAT4)
81195
+# define IsStat4 1
81196
+# define IsStat3 0
81197
+#elif defined(SQLITE_ENABLE_STAT3)
81198
+# define IsStat4 0
81199
+# define IsStat3 1
81200
+#else
81201
+# define IsStat4 0
81202
+# define IsStat3 0
81203
+# undef SQLITE_STAT4_SAMPLES
81204
+# define SQLITE_STAT4_SAMPLES 1
81205
+#endif
81206
+#define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
81207
+
8065181208
/*
80652
-** This routine generates code that opens the sqlite_stat1 table for
80653
-** writing with cursor iStatCur. If the library was built with the
80654
-** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
80655
-** opened for writing using cursor (iStatCur+1)
81209
+** This routine generates code that opens the sqlite_statN tables.
81210
+** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
81211
+** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
81212
+** appropriate compile-time options are provided.
8065681213
**
80657
-** If the sqlite_stat1 tables does not previously exist, it is created.
80658
-** Similarly, if the sqlite_stat3 table does not exist and the library
80659
-** is compiled with SQLITE_ENABLE_STAT3 defined, it is created.
81214
+** If the sqlite_statN tables do not previously exist, it is created.
8066081215
**
8066181216
** Argument zWhere may be a pointer to a buffer containing a table name,
8066281217
** or it may be a NULL pointer. If it is not NULL, then all entries in
80663
-** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
80664
-** with the named table are deleted. If zWhere==0, then code is generated
80665
-** to delete all stat table entries.
81218
+** the sqlite_statN tables associated with the named table are deleted.
81219
+** If zWhere==0, then code is generated to delete all stat table entries.
8066681220
*/
8066781221
static void openStatTable(
8066881222
Parse *pParse, /* Parsing context */
8066981223
int iDb, /* The database we are looking in */
8067081224
int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80674,22 +81228,28 @@
8067481228
static const struct {
8067581229
const char *zName;
8067681230
const char *zCols;
8067781231
} aTable[] = {
8067881232
{ "sqlite_stat1", "tbl,idx,stat" },
80679
-#ifdef SQLITE_ENABLE_STAT3
81233
+#if defined(SQLITE_ENABLE_STAT4)
81234
+ { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
81235
+ { "sqlite_stat3", 0 },
81236
+#elif defined(SQLITE_ENABLE_STAT3)
8068081237
{ "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
81238
+ { "sqlite_stat4", 0 },
81239
+#else
81240
+ { "sqlite_stat3", 0 },
81241
+ { "sqlite_stat4", 0 },
8068181242
#endif
8068281243
};
80683
-
80684
- int aRoot[] = {0, 0};
80685
- u8 aCreateTbl[] = {0, 0};
80686
-
8068781244
int i;
8068881245
sqlite3 *db = pParse->db;
8068981246
Db *pDb;
8069081247
Vdbe *v = sqlite3GetVdbe(pParse);
81248
+ int aRoot[ArraySize(aTable)];
81249
+ u8 aCreateTbl[ArraySize(aTable)];
81250
+
8069181251
if( v==0 ) return;
8069281252
assert( sqlite3BtreeHoldsAllMutexes(db) );
8069381253
assert( sqlite3VdbeDb(v)==db );
8069481254
pDb = &db->aDb[iDb];
8069581255
@@ -80698,262 +81258,626 @@
8069881258
*/
8069981259
for(i=0; i<ArraySize(aTable); i++){
8070081260
const char *zTab = aTable[i].zName;
8070181261
Table *pStat;
8070281262
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
80703
- /* The sqlite_stat[12] table does not exist. Create it. Note that a
80704
- ** side-effect of the CREATE TABLE statement is to leave the rootpage
80705
- ** of the new table in register pParse->regRoot. This is important
80706
- ** because the OpenWrite opcode below will be needing it. */
80707
- sqlite3NestedParse(pParse,
80708
- "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
80709
- );
80710
- aRoot[i] = pParse->regRoot;
80711
- aCreateTbl[i] = OPFLAG_P2ISREG;
81263
+ if( aTable[i].zCols ){
81264
+ /* The sqlite_statN table does not exist. Create it. Note that a
81265
+ ** side-effect of the CREATE TABLE statement is to leave the rootpage
81266
+ ** of the new table in register pParse->regRoot. This is important
81267
+ ** because the OpenWrite opcode below will be needing it. */
81268
+ sqlite3NestedParse(pParse,
81269
+ "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
81270
+ );
81271
+ aRoot[i] = pParse->regRoot;
81272
+ aCreateTbl[i] = OPFLAG_P2ISREG;
81273
+ }
8071281274
}else{
8071381275
/* The table already exists. If zWhere is not NULL, delete all entries
8071481276
** associated with the table zWhere. If zWhere is NULL, delete the
8071581277
** entire contents of the table. */
8071681278
aRoot[i] = pStat->tnum;
81279
+ aCreateTbl[i] = 0;
8071781280
sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
8071881281
if( zWhere ){
8071981282
sqlite3NestedParse(pParse,
80720
- "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
81283
+ "DELETE FROM %Q.%s WHERE %s=%Q",
81284
+ pDb->zName, zTab, zWhereType, zWhere
8072181285
);
8072281286
}else{
80723
- /* The sqlite_stat[12] table already exists. Delete all rows. */
81287
+ /* The sqlite_stat[134] table already exists. Delete all rows. */
8072481288
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
8072581289
}
8072681290
}
8072781291
}
8072881292
80729
- /* Open the sqlite_stat[13] tables for writing. */
80730
- for(i=0; i<ArraySize(aTable); i++){
81293
+ /* Open the sqlite_stat[134] tables for writing. */
81294
+ for(i=0; aTable[i].zCols; i++){
81295
+ assert( i<ArraySize(aTable) );
8073181296
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
8073281297
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
8073381298
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
8073481299
}
8073581300
}
8073681301
8073781302
/*
80738
-** Recommended number of samples for sqlite_stat3
81303
+** Recommended number of samples for sqlite_stat4
8073981304
*/
80740
-#ifndef SQLITE_STAT3_SAMPLES
80741
-# define SQLITE_STAT3_SAMPLES 24
81305
+#ifndef SQLITE_STAT4_SAMPLES
81306
+# define SQLITE_STAT4_SAMPLES 24
8074281307
#endif
8074381308
8074481309
/*
80745
-** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
81310
+** Three SQL functions - stat_init(), stat_push(), and stat_get() -
8074681311
** share an instance of the following structure to hold their state
8074781312
** information.
8074881313
*/
80749
-typedef struct Stat3Accum Stat3Accum;
80750
-struct Stat3Accum {
81314
+typedef struct Stat4Accum Stat4Accum;
81315
+typedef struct Stat4Sample Stat4Sample;
81316
+struct Stat4Sample {
81317
+ tRowcnt *anEq; /* sqlite_stat4.nEq */
81318
+ tRowcnt *anDLt; /* sqlite_stat4.nDLt */
81319
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81320
+ tRowcnt *anLt; /* sqlite_stat4.nLt */
81321
+ i64 iRowid; /* Rowid in main table of the key */
81322
+ u8 isPSample; /* True if a periodic sample */
81323
+ int iCol; /* If !isPSample, the reason for inclusion */
81324
+ u32 iHash; /* Tiebreaker hash */
81325
+#endif
81326
+};
81327
+struct Stat4Accum {
8075181328
tRowcnt nRow; /* Number of rows in the entire table */
8075281329
tRowcnt nPSample; /* How often to do a periodic sample */
80753
- int iMin; /* Index of entry with minimum nEq and hash */
81330
+ int nCol; /* Number of columns in index + rowid */
8075481331
int mxSample; /* Maximum number of samples to accumulate */
80755
- int nSample; /* Current number of samples */
81332
+ Stat4Sample current; /* Current row as a Stat4Sample */
8075681333
u32 iPrn; /* Pseudo-random number used for sampling */
80757
- struct Stat3Sample {
80758
- i64 iRowid; /* Rowid in main table of the key */
80759
- tRowcnt nEq; /* sqlite_stat3.nEq */
80760
- tRowcnt nLt; /* sqlite_stat3.nLt */
80761
- tRowcnt nDLt; /* sqlite_stat3.nDLt */
80762
- u8 isPSample; /* True if a periodic sample */
80763
- u32 iHash; /* Tiebreaker hash */
80764
- } *a; /* An array of samples */
81334
+ Stat4Sample *aBest; /* Array of (nCol-1) best samples */
81335
+ int iMin; /* Index in a[] of entry with minimum score */
81336
+ int nSample; /* Current number of samples */
81337
+ int iGet; /* Index of current sample accessed by stat_get() */
81338
+ Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
8076581339
};
8076681340
80767
-#ifdef SQLITE_ENABLE_STAT3
8076881341
/*
80769
-** Implementation of the stat3_init(C,S) SQL function. The two parameters
80770
-** are the number of rows in the table or index (C) and the number of samples
80771
-** to accumulate (S).
80772
-**
80773
-** This routine allocates the Stat3Accum object.
80774
-**
80775
-** The return value is the Stat3Accum object (P).
80776
-*/
80777
-static void stat3Init(
81342
+** Implementation of the stat_init(N,C) SQL function. The two parameters
81343
+** are the number of rows in the table or index (C) and the number of columns
81344
+** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
81345
+**
81346
+** This routine allocates the Stat4Accum object in heap memory. The return
81347
+** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
81348
+** the size of the blob is sizeof(void*) bytes).
81349
+*/
81350
+static void statInit(
8077881351
sqlite3_context *context,
8077981352
int argc,
8078081353
sqlite3_value **argv
8078181354
){
80782
- Stat3Accum *p;
80783
- tRowcnt nRow;
80784
- int mxSample;
80785
- int n;
81355
+ Stat4Accum *p;
81356
+ int nCol; /* Number of columns in index being sampled */
81357
+ int nColUp; /* nCol rounded up for alignment */
81358
+ int n; /* Bytes of space to allocate */
81359
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81360
+ int mxSample = SQLITE_STAT4_SAMPLES;
81361
+#endif
8078681362
81363
+ /* Decode the three function arguments */
8078781364
UNUSED_PARAMETER(argc);
80788
- nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
80789
- mxSample = sqlite3_value_int(argv[1]);
80790
- n = sizeof(*p) + sizeof(p->a[0])*mxSample;
80791
- p = sqlite3MallocZero( n );
81365
+ nCol = sqlite3_value_int(argv[0]);
81366
+ assert( nCol>1 ); /* >1 because it includes the rowid column */
81367
+ nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
81368
+
81369
+ /* Allocate the space required for the Stat4Accum object */
81370
+ n = sizeof(*p)
81371
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
81372
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
81373
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81374
+ + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
81375
+ + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
81376
+ + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
81377
+#endif
81378
+ ;
81379
+ p = sqlite3MallocZero(n);
8079281380
if( p==0 ){
8079381381
sqlite3_result_error_nomem(context);
8079481382
return;
8079581383
}
80796
- p->a = (struct Stat3Sample*)&p[1];
80797
- p->nRow = nRow;
80798
- p->mxSample = mxSample;
80799
- p->nPSample = p->nRow/(mxSample/3+1) + 1;
80800
- sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81384
+
81385
+ p->nRow = 0;
81386
+ p->nCol = nCol;
81387
+ p->current.anDLt = (tRowcnt*)&p[1];
81388
+ p->current.anEq = &p->current.anDLt[nColUp];
81389
+
81390
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81391
+ {
81392
+ u8 *pSpace; /* Allocated space not yet assigned */
81393
+ int i; /* Used to iterate through p->aSample[] */
81394
+
81395
+ p->iGet = -1;
81396
+ p->mxSample = mxSample;
81397
+ p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
81398
+ p->current.anLt = &p->current.anEq[nColUp];
81399
+ sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81400
+
81401
+ /* Set up the Stat4Accum.a[] and aBest[] arrays */
81402
+ p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
81403
+ p->aBest = &p->a[mxSample];
81404
+ pSpace = (u8*)(&p->a[mxSample+nCol]);
81405
+ for(i=0; i<(mxSample+nCol); i++){
81406
+ p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81407
+ p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81408
+ p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81409
+ }
81410
+ assert( (pSpace - (u8*)p)==n );
81411
+
81412
+ for(i=0; i<nCol; i++){
81413
+ p->aBest[i].iCol = i;
81414
+ }
81415
+ }
81416
+#endif
81417
+
81418
+ /* Return a pointer to the allocated object to the caller */
8080181419
sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
8080281420
}
80803
-static const FuncDef stat3InitFuncdef = {
80804
- 2, /* nArg */
80805
- SQLITE_UTF8, /* iPrefEnc */
80806
- 0, /* flags */
80807
- 0, /* pUserData */
80808
- 0, /* pNext */
80809
- stat3Init, /* xFunc */
80810
- 0, /* xStep */
80811
- 0, /* xFinalize */
80812
- "stat3_init", /* zName */
80813
- 0, /* pHash */
80814
- 0 /* pDestructor */
80815
-};
80816
-
80817
-
80818
-/*
80819
-** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
80820
-** arguments describe a single key instance. This routine makes the
80821
-** decision about whether or not to retain this key for the sqlite_stat3
80822
-** table.
80823
-**
80824
-** The return value is NULL.
80825
-*/
80826
-static void stat3Push(
80827
- sqlite3_context *context,
80828
- int argc,
80829
- sqlite3_value **argv
80830
-){
80831
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
80832
- tRowcnt nEq = sqlite3_value_int64(argv[0]);
80833
- tRowcnt nLt = sqlite3_value_int64(argv[1]);
80834
- tRowcnt nDLt = sqlite3_value_int64(argv[2]);
80835
- i64 rowid = sqlite3_value_int64(argv[3]);
80836
- u8 isPSample = 0;
80837
- u8 doInsert = 0;
80838
- int iMin = p->iMin;
80839
- struct Stat3Sample *pSample;
80840
- int i;
80841
- u32 h;
80842
-
80843
- UNUSED_PARAMETER(context);
80844
- UNUSED_PARAMETER(argc);
80845
- if( nEq==0 ) return;
80846
- h = p->iPrn = p->iPrn*1103515245 + 12345;
80847
- if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
80848
- doInsert = isPSample = 1;
80849
- }else if( p->nSample<p->mxSample ){
80850
- doInsert = 1;
80851
- }else{
80852
- if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
80853
- doInsert = 1;
80854
- }
80855
- }
80856
- if( !doInsert ) return;
80857
- if( p->nSample==p->mxSample ){
80858
- assert( p->nSample - iMin - 1 >= 0 );
80859
- memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
81421
+static const FuncDef statInitFuncdef = {
81422
+ 1+IsStat34, /* nArg */
81423
+ SQLITE_UTF8, /* iPrefEnc */
81424
+ 0, /* flags */
81425
+ 0, /* pUserData */
81426
+ 0, /* pNext */
81427
+ statInit, /* xFunc */
81428
+ 0, /* xStep */
81429
+ 0, /* xFinalize */
81430
+ "stat_init", /* zName */
81431
+ 0, /* pHash */
81432
+ 0 /* pDestructor */
81433
+};
81434
+
81435
+#ifdef SQLITE_ENABLE_STAT4
81436
+/*
81437
+** pNew and pOld are both candidate non-periodic samples selected for
81438
+** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
81439
+** considering only any trailing columns and the sample hash value, this
81440
+** function returns true if sample pNew is to be preferred over pOld.
81441
+** In other words, if we assume that the cardinalities of the selected
81442
+** column for pNew and pOld are equal, is pNew to be preferred over pOld.
81443
+**
81444
+** This function assumes that for each argument sample, the contents of
81445
+** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
81446
+*/
81447
+static int sampleIsBetterPost(
81448
+ Stat4Accum *pAccum,
81449
+ Stat4Sample *pNew,
81450
+ Stat4Sample *pOld
81451
+){
81452
+ int nCol = pAccum->nCol;
81453
+ int i;
81454
+ assert( pNew->iCol==pOld->iCol );
81455
+ for(i=pNew->iCol+1; i<nCol; i++){
81456
+ if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
81457
+ if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
81458
+ }
81459
+ if( pNew->iHash>pOld->iHash ) return 1;
81460
+ return 0;
81461
+}
81462
+#endif
81463
+
81464
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81465
+/*
81466
+** Return true if pNew is to be preferred over pOld.
81467
+**
81468
+** This function assumes that for each argument sample, the contents of
81469
+** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
81470
+*/
81471
+static int sampleIsBetter(
81472
+ Stat4Accum *pAccum,
81473
+ Stat4Sample *pNew,
81474
+ Stat4Sample *pOld
81475
+){
81476
+ tRowcnt nEqNew = pNew->anEq[pNew->iCol];
81477
+ tRowcnt nEqOld = pOld->anEq[pOld->iCol];
81478
+
81479
+ assert( pOld->isPSample==0 && pNew->isPSample==0 );
81480
+ assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
81481
+
81482
+ if( (nEqNew>nEqOld) ) return 1;
81483
+#ifdef SQLITE_ENABLE_STAT4
81484
+ if( nEqNew==nEqOld ){
81485
+ if( pNew->iCol<pOld->iCol ) return 1;
81486
+ return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
81487
+ }
81488
+ return 0;
81489
+#else
81490
+ return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
81491
+#endif
81492
+}
81493
+
81494
+/*
81495
+** Copy the contents of object (*pFrom) into (*pTo).
81496
+*/
81497
+void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
81498
+ pTo->iRowid = pFrom->iRowid;
81499
+ pTo->isPSample = pFrom->isPSample;
81500
+ pTo->iCol = pFrom->iCol;
81501
+ pTo->iHash = pFrom->iHash;
81502
+ memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
81503
+ memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
81504
+ memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
81505
+}
81506
+
81507
+/*
81508
+** Copy the contents of sample *pNew into the p->a[] array. If necessary,
81509
+** remove the least desirable sample from p->a[] to make room.
81510
+*/
81511
+static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
81512
+ Stat4Sample *pSample;
81513
+ int i;
81514
+
81515
+ assert( IsStat4 || nEqZero==0 );
81516
+
81517
+#ifdef SQLITE_ENABLE_STAT4
81518
+ if( pNew->isPSample==0 ){
81519
+ Stat4Sample *pUpgrade = 0;
81520
+ assert( pNew->anEq[pNew->iCol]>0 );
81521
+
81522
+ /* This sample is being added because the prefix that ends in column
81523
+ ** iCol occurs many times in the table. However, if we have already
81524
+ ** added a sample that shares this prefix, there is no need to add
81525
+ ** this one. Instead, upgrade the priority of the highest priority
81526
+ ** existing sample that shares this prefix. */
81527
+ for(i=p->nSample-1; i>=0; i--){
81528
+ Stat4Sample *pOld = &p->a[i];
81529
+ if( pOld->anEq[pNew->iCol]==0 ){
81530
+ if( pOld->isPSample ) return;
81531
+ assert( pOld->iCol>pNew->iCol );
81532
+ assert( sampleIsBetter(p, pNew, pOld) );
81533
+ if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
81534
+ pUpgrade = pOld;
81535
+ }
81536
+ }
81537
+ }
81538
+ if( pUpgrade ){
81539
+ pUpgrade->iCol = pNew->iCol;
81540
+ pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
81541
+ goto find_new_min;
81542
+ }
81543
+ }
81544
+#endif
81545
+
81546
+ /* If necessary, remove sample iMin to make room for the new sample. */
81547
+ if( p->nSample>=p->mxSample ){
81548
+ Stat4Sample *pMin = &p->a[p->iMin];
81549
+ tRowcnt *anEq = pMin->anEq;
81550
+ tRowcnt *anLt = pMin->anLt;
81551
+ tRowcnt *anDLt = pMin->anDLt;
81552
+ memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
8086081553
pSample = &p->a[p->nSample-1];
80861
- }else{
80862
- pSample = &p->a[p->nSample++];
80863
- }
80864
- pSample->iRowid = rowid;
80865
- pSample->nEq = nEq;
80866
- pSample->nLt = nLt;
80867
- pSample->nDLt = nDLt;
80868
- pSample->iHash = h;
80869
- pSample->isPSample = isPSample;
80870
-
80871
- /* Find the new minimum */
80872
- if( p->nSample==p->mxSample ){
80873
- pSample = p->a;
80874
- i = 0;
80875
- while( pSample->isPSample ){
80876
- i++;
80877
- pSample++;
80878
- assert( i<p->nSample );
80879
- }
80880
- nEq = pSample->nEq;
80881
- h = pSample->iHash;
80882
- iMin = i;
80883
- for(i++, pSample++; i<p->nSample; i++, pSample++){
80884
- if( pSample->isPSample ) continue;
80885
- if( pSample->nEq<nEq
80886
- || (pSample->nEq==nEq && pSample->iHash<h)
80887
- ){
80888
- iMin = i;
80889
- nEq = pSample->nEq;
80890
- h = pSample->iHash;
80891
- }
80892
- }
81554
+ pSample->anEq = anEq;
81555
+ pSample->anDLt = anDLt;
81556
+ pSample->anLt = anLt;
81557
+ p->nSample = p->mxSample-1;
81558
+ }
81559
+
81560
+ /* The "rows less-than" for the rowid column must be greater than that
81561
+ ** for the last sample in the p->a[] array. Otherwise, the samples would
81562
+ ** be out of order. */
81563
+#ifdef SQLITE_ENABLE_STAT4
81564
+ assert( p->nSample==0
81565
+ || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
81566
+#endif
81567
+
81568
+ /* Insert the new sample */
81569
+ pSample = &p->a[p->nSample];
81570
+ sampleCopy(p, pSample, pNew);
81571
+ p->nSample++;
81572
+
81573
+ /* Zero the first nEqZero entries in the anEq[] array. */
81574
+ memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
81575
+
81576
+#ifdef SQLITE_ENABLE_STAT4
81577
+ find_new_min:
81578
+#endif
81579
+ if( p->nSample>=p->mxSample ){
81580
+ int iMin = -1;
81581
+ for(i=0; i<p->mxSample; i++){
81582
+ if( p->a[i].isPSample ) continue;
81583
+ if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
81584
+ iMin = i;
81585
+ }
81586
+ }
81587
+ assert( iMin>=0 );
8089381588
p->iMin = iMin;
8089481589
}
8089581590
}
80896
-static const FuncDef stat3PushFuncdef = {
80897
- 5, /* nArg */
80898
- SQLITE_UTF8, /* iPrefEnc */
80899
- 0, /* flags */
80900
- 0, /* pUserData */
80901
- 0, /* pNext */
80902
- stat3Push, /* xFunc */
80903
- 0, /* xStep */
80904
- 0, /* xFinalize */
80905
- "stat3_push", /* zName */
80906
- 0, /* pHash */
80907
- 0 /* pDestructor */
80908
-};
80909
-
80910
-/*
80911
-** Implementation of the stat3_get(P,N,...) SQL function. This routine is
80912
-** used to query the results. Content is returned for the Nth sqlite_stat3
80913
-** row where N is between 0 and S-1 and S is the number of samples. The
80914
-** value returned depends on the number of arguments.
80915
-**
80916
-** argc==2 result: rowid
80917
-** argc==3 result: nEq
80918
-** argc==4 result: nLt
80919
-** argc==5 result: nDLt
80920
-*/
80921
-static void stat3Get(
81591
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81592
+
81593
+/*
81594
+** Field iChng of the index being scanned has changed. So at this point
81595
+** p->current contains a sample that reflects the previous row of the
81596
+** index. The value of anEq[iChng] and subsequent anEq[] elements are
81597
+** correct at this point.
81598
+*/
81599
+static void samplePushPrevious(Stat4Accum *p, int iChng){
81600
+#ifdef SQLITE_ENABLE_STAT4
81601
+ int i;
81602
+
81603
+ /* Check if any samples from the aBest[] array should be pushed
81604
+ ** into IndexSample.a[] at this point. */
81605
+ for(i=(p->nCol-2); i>=iChng; i--){
81606
+ Stat4Sample *pBest = &p->aBest[i];
81607
+ pBest->anEq[i] = p->current.anEq[i];
81608
+ if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
81609
+ sampleInsert(p, pBest, i);
81610
+ }
81611
+ }
81612
+
81613
+ /* Update the anEq[] fields of any samples already collected. */
81614
+ for(i=p->nSample-1; i>=0; i--){
81615
+ int j;
81616
+ for(j=iChng; j<p->nCol; j++){
81617
+ if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
81618
+ }
81619
+ }
81620
+#endif
81621
+
81622
+#if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
81623
+ if( iChng==0 ){
81624
+ tRowcnt nLt = p->current.anLt[0];
81625
+ tRowcnt nEq = p->current.anEq[0];
81626
+
81627
+ /* Check if this is to be a periodic sample. If so, add it. */
81628
+ if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
81629
+ p->current.isPSample = 1;
81630
+ sampleInsert(p, &p->current, 0);
81631
+ p->current.isPSample = 0;
81632
+ }else
81633
+
81634
+ /* Or if it is a non-periodic sample. Add it in this case too. */
81635
+ if( p->nSample<p->mxSample
81636
+ || sampleIsBetter(p, &p->current, &p->a[p->iMin])
81637
+ ){
81638
+ sampleInsert(p, &p->current, 0);
81639
+ }
81640
+ }
81641
+#endif
81642
+}
81643
+
81644
+/*
81645
+** Implementation of the stat_push SQL function: stat_push(P,R,C)
81646
+** Arguments:
81647
+**
81648
+** P Pointer to the Stat4Accum object created by stat_init()
81649
+** C Index of left-most column to differ from previous row
81650
+** R Rowid for the current row
81651
+**
81652
+** The SQL function always returns NULL.
81653
+**
81654
+** The R parameter is only used for STAT3 and STAT4.
81655
+*/
81656
+static void statPush(
81657
+ sqlite3_context *context,
81658
+ int argc,
81659
+ sqlite3_value **argv
81660
+){
81661
+ int i;
81662
+
81663
+ /* The three function arguments */
81664
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81665
+ int iChng = sqlite3_value_int(argv[1]);
81666
+
81667
+ assert( p->nCol>1 ); /* Includes rowid field */
81668
+ assert( iChng<p->nCol );
81669
+
81670
+ if( p->nRow==0 ){
81671
+ /* This is the first call to this function. Do initialization. */
81672
+ for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
81673
+ }else{
81674
+ /* Second and subsequent calls get processed here */
81675
+ samplePushPrevious(p, iChng);
81676
+
81677
+ /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
81678
+ ** to the current row of the index. */
81679
+ for(i=0; i<iChng; i++){
81680
+ p->current.anEq[i]++;
81681
+ }
81682
+ for(i=iChng; i<p->nCol; i++){
81683
+ p->current.anDLt[i]++;
81684
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81685
+ p->current.anLt[i] += p->current.anEq[i];
81686
+#endif
81687
+ p->current.anEq[i] = 1;
81688
+ }
81689
+ }
81690
+ p->nRow++;
81691
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81692
+ p->current.iRowid = sqlite3_value_int64(argv[2]);
81693
+ p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
81694
+#endif
81695
+
81696
+#ifdef SQLITE_ENABLE_STAT4
81697
+ {
81698
+ tRowcnt nLt = p->current.anLt[p->nCol-1];
81699
+
81700
+ /* Check if this is to be a periodic sample. If so, add it. */
81701
+ if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
81702
+ p->current.isPSample = 1;
81703
+ p->current.iCol = 0;
81704
+ sampleInsert(p, &p->current, p->nCol-1);
81705
+ p->current.isPSample = 0;
81706
+ }
81707
+
81708
+ /* Update the aBest[] array. */
81709
+ for(i=0; i<(p->nCol-1); i++){
81710
+ p->current.iCol = i;
81711
+ if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
81712
+ sampleCopy(p, &p->aBest[i], &p->current);
81713
+ }
81714
+ }
81715
+ }
81716
+#endif
81717
+}
81718
+static const FuncDef statPushFuncdef = {
81719
+ 2+IsStat34, /* nArg */
81720
+ SQLITE_UTF8, /* iPrefEnc */
81721
+ 0, /* flags */
81722
+ 0, /* pUserData */
81723
+ 0, /* pNext */
81724
+ statPush, /* xFunc */
81725
+ 0, /* xStep */
81726
+ 0, /* xFinalize */
81727
+ "stat_push", /* zName */
81728
+ 0, /* pHash */
81729
+ 0 /* pDestructor */
81730
+};
81731
+
81732
+#define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
81733
+#define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
81734
+#define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
81735
+#define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
81736
+#define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
81737
+
81738
+/*
81739
+** Implementation of the stat_get(P,J) SQL function. This routine is
81740
+** used to query the results. Content is returned for parameter J
81741
+** which is one of the STAT_GET_xxxx values defined above.
81742
+**
81743
+** If neither STAT3 nor STAT4 are enabled, then J is always
81744
+** STAT_GET_STAT1 and is hence omitted and this routine becomes
81745
+** a one-parameter function, stat_get(P), that always returns the
81746
+** stat1 table entry information.
81747
+*/
81748
+static void statGet(
8092281749
sqlite3_context *context,
8092381750
int argc,
8092481751
sqlite3_value **argv
8092581752
){
80926
- int n = sqlite3_value_int(argv[1]);
80927
- Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
80928
-
80929
- assert( p!=0 );
80930
- if( p->nSample<=n ) return;
80931
- switch( argc ){
80932
- case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
80933
- case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
80934
- case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
80935
- default: sqlite3_result_int64(context, p->a[n].nDLt); break;
80936
- }
80937
-}
80938
-static const FuncDef stat3GetFuncdef = {
80939
- -1, /* nArg */
80940
- SQLITE_UTF8, /* iPrefEnc */
80941
- 0, /* flags */
80942
- 0, /* pUserData */
80943
- 0, /* pNext */
80944
- stat3Get, /* xFunc */
80945
- 0, /* xStep */
80946
- 0, /* xFinalize */
80947
- "stat3_get", /* zName */
80948
- 0, /* pHash */
80949
- 0 /* pDestructor */
80950
-};
80951
-#endif /* SQLITE_ENABLE_STAT3 */
80952
-
80953
-
80954
-
81753
+ Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81754
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81755
+ /* STAT3 and STAT4 have a parameter on this routine. */
81756
+ int eCall = sqlite3_value_int(argv[1]);
81757
+ assert( argc==2 );
81758
+ assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
81759
+ || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
81760
+ || eCall==STAT_GET_NDLT
81761
+ );
81762
+ if( eCall==STAT_GET_STAT1 )
81763
+#else
81764
+ assert( argc==1 );
81765
+#endif
81766
+ {
81767
+ /* Return the value to store in the "stat" column of the sqlite_stat1
81768
+ ** table for this index.
81769
+ **
81770
+ ** The value is a string composed of a list of integers describing
81771
+ ** the index. The first integer in the list is the total number of
81772
+ ** entries in the index. There is one additional integer in the list
81773
+ ** for each indexed column. This additional integer is an estimate of
81774
+ ** the number of rows matched by a stabbing query on the index using
81775
+ ** a key with the corresponding number of fields. In other words,
81776
+ ** if the index is on columns (a,b) and the sqlite_stat1 value is
81777
+ ** "100 10 2", then SQLite estimates that:
81778
+ **
81779
+ ** * the index contains 100 rows,
81780
+ ** * "WHERE a=?" matches 10 rows, and
81781
+ ** * "WHERE a=? AND b=?" matches 2 rows.
81782
+ **
81783
+ ** If D is the count of distinct values and K is the total number of
81784
+ ** rows, then each estimate is computed as:
81785
+ **
81786
+ ** I = (K+D-1)/D
81787
+ */
81788
+ char *z;
81789
+ int i;
81790
+
81791
+ char *zRet = sqlite3MallocZero(p->nCol * 25);
81792
+ if( zRet==0 ){
81793
+ sqlite3_result_error_nomem(context);
81794
+ return;
81795
+ }
81796
+
81797
+ sqlite3_snprintf(24, zRet, "%lld", p->nRow);
81798
+ z = zRet + sqlite3Strlen30(zRet);
81799
+ for(i=0; i<(p->nCol-1); i++){
81800
+ i64 nDistinct = p->current.anDLt[i] + 1;
81801
+ i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81802
+ sqlite3_snprintf(24, z, " %lld", iVal);
81803
+ z += sqlite3Strlen30(z);
81804
+ assert( p->current.anEq[i] );
81805
+ }
81806
+ assert( z[0]=='\0' && z>zRet );
81807
+
81808
+ sqlite3_result_text(context, zRet, -1, sqlite3_free);
81809
+ }
81810
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81811
+ else if( eCall==STAT_GET_ROWID ){
81812
+ if( p->iGet<0 ){
81813
+ samplePushPrevious(p, 0);
81814
+ p->iGet = 0;
81815
+ }
81816
+ if( p->iGet<p->nSample ){
81817
+ sqlite3_result_int64(context, p->a[p->iGet].iRowid);
81818
+ }
81819
+ }else{
81820
+ tRowcnt *aCnt = 0;
81821
+
81822
+ assert( p->iGet<p->nSample );
81823
+ switch( eCall ){
81824
+ case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
81825
+ case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
81826
+ default: {
81827
+ aCnt = p->a[p->iGet].anDLt;
81828
+ p->iGet++;
81829
+ break;
81830
+ }
81831
+ }
81832
+
81833
+ if( IsStat3 ){
81834
+ sqlite3_result_int64(context, (i64)aCnt[0]);
81835
+ }else{
81836
+ char *zRet = sqlite3MallocZero(p->nCol * 25);
81837
+ if( zRet==0 ){
81838
+ sqlite3_result_error_nomem(context);
81839
+ }else{
81840
+ int i;
81841
+ char *z = zRet;
81842
+ for(i=0; i<p->nCol; i++){
81843
+ sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
81844
+ z += sqlite3Strlen30(z);
81845
+ }
81846
+ assert( z[0]=='\0' && z>zRet );
81847
+ z[-1] = '\0';
81848
+ sqlite3_result_text(context, zRet, -1, sqlite3_free);
81849
+ }
81850
+ }
81851
+ }
81852
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81853
+}
81854
+static const FuncDef statGetFuncdef = {
81855
+ 1+IsStat34, /* nArg */
81856
+ SQLITE_UTF8, /* iPrefEnc */
81857
+ 0, /* flags */
81858
+ 0, /* pUserData */
81859
+ 0, /* pNext */
81860
+ statGet, /* xFunc */
81861
+ 0, /* xStep */
81862
+ 0, /* xFinalize */
81863
+ "stat_get", /* zName */
81864
+ 0, /* pHash */
81865
+ 0 /* pDestructor */
81866
+};
81867
+
81868
+static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
81869
+ assert( regOut!=regStat4 && regOut!=regStat4+1 );
81870
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81871
+ sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
81872
+#else
81873
+ assert( iParam==STAT_GET_STAT1 );
81874
+#endif
81875
+ sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
81876
+ sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
81877
+ sqlite3VdbeChangeP5(v, 1 + IsStat34);
81878
+}
8095581879
8095681880
/*
8095781881
** Generate code to do an analysis of all indices associated with
8095881882
** a single table.
8095981883
*/
@@ -80960,46 +81884,35 @@
8096081884
static void analyzeOneTable(
8096181885
Parse *pParse, /* Parser context */
8096281886
Table *pTab, /* Table whose indices are to be analyzed */
8096381887
Index *pOnlyIdx, /* If not NULL, only analyze this one index */
8096481888
int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
80965
- int iMem /* Available memory locations begin here */
81889
+ int iMem, /* Available memory locations begin here */
81890
+ int iTab /* Next available cursor */
8096681891
){
8096781892
sqlite3 *db = pParse->db; /* Database handle */
8096881893
Index *pIdx; /* An index to being analyzed */
8096981894
int iIdxCur; /* Cursor open on index being analyzed */
81895
+ int iTabCur; /* Table cursor */
8097081896
Vdbe *v; /* The virtual machine being built up */
8097181897
int i; /* Loop counter */
80972
- int topOfLoop; /* The top of the loop */
80973
- int endOfLoop; /* The end of the loop */
8097481898
int jZeroRows = -1; /* Jump from here if number of rows is zero */
8097581899
int iDb; /* Index of database containing pTab */
8097681900
u8 needTableCnt = 1; /* True to count the table */
81901
+ int regNewRowid = iMem++; /* Rowid for the inserted record */
81902
+ int regStat4 = iMem++; /* Register to hold Stat4Accum object */
81903
+ int regChng = iMem++; /* Index of changed index field */
81904
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81905
+ int regRowid = iMem++; /* Rowid argument passed to stat_push() */
81906
+#endif
81907
+ int regTemp = iMem++; /* Temporary use register */
8097781908
int regTabname = iMem++; /* Register containing table name */
8097881909
int regIdxname = iMem++; /* Register containing index name */
80979
- int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
80980
-#ifdef SQLITE_ENABLE_STAT3
80981
- int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
80982
- int regNumLt = iMem++; /* Number of keys less than regSample */
80983
- int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
80984
- int regSample = iMem++; /* The next sample value */
80985
- int regRowid = regSample; /* Rowid of a sample */
80986
- int regAccum = iMem++; /* Register to hold Stat3Accum object */
80987
- int regLoop = iMem++; /* Loop counter */
80988
- int regCount = iMem++; /* Number of rows in the table or index */
80989
- int regTemp1 = iMem++; /* Intermediate register */
80990
- int regTemp2 = iMem++; /* Intermediate register */
80991
- int once = 1; /* One-time initialization */
80992
- int shortJump = 0; /* Instruction address */
80993
- int iTabCur = pParse->nTab++; /* Table cursor */
80994
-#endif
80995
- int regCol = iMem++; /* Content of a column in analyzed table */
80996
- int regRec = iMem++; /* Register holding completed record */
80997
- int regTemp = iMem++; /* Temporary use register */
80998
- int regNewRowid = iMem++; /* Rowid for the inserted record */
80999
-
81000
-
81910
+ int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
81911
+ int regPrev = iMem; /* MUST BE LAST (see below) */
81912
+
81913
+ pParse->nMem = MAX(pParse->nMem, iMem);
8100181914
v = sqlite3GetVdbe(pParse);
8100281915
if( v==0 || NEVER(pTab==0) ){
8100381916
return;
8100481917
}
8100581918
if( pTab->tnum==0 ){
@@ -81019,217 +81932,230 @@
8101981932
db->aDb[iDb].zName ) ){
8102081933
return;
8102181934
}
8102281935
#endif
8102381936
81024
- /* Establish a read-lock on the table at the shared-cache level. */
81937
+ /* Establish a read-lock on the table at the shared-cache level.
81938
+ ** Open a read-only cursor on the table. Also allocate a cursor number
81939
+ ** to use for scanning indexes (iIdxCur). No index cursor is opened at
81940
+ ** this time though. */
8102581941
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
81026
-
81027
- iIdxCur = pParse->nTab++;
81942
+ iTabCur = iTab++;
81943
+ iIdxCur = iTab++;
81944
+ pParse->nTab = MAX(pParse->nTab, iTab);
81945
+ sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
8102881946
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
81947
+
8102981948
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81030
- int nCol;
81031
- KeyInfo *pKey;
81032
- int addrIfNot = 0; /* address of OP_IfNot */
81033
- int *aChngAddr; /* Array of jump instruction addresses */
81949
+ int nCol; /* Number of columns indexed by pIdx */
81950
+ KeyInfo *pKey; /* KeyInfo structure for pIdx */
81951
+ int *aGotoChng; /* Array of jump instruction addresses */
81952
+ int addrRewind; /* Address of "OP_Rewind iIdxCur" */
81953
+ int addrGotoChng0; /* Address of "Goto addr_chng_0" */
81954
+ int addrNextRow; /* Address of "next_row:" */
8103481955
8103581956
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
8103681957
if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
8103781958
VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
8103881959
nCol = pIdx->nColumn;
81039
- aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
81040
- if( aChngAddr==0 ) continue;
81960
+ aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
81961
+ if( aGotoChng==0 ) continue;
8104181962
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
81042
- if( iMem+1+(nCol*2)>pParse->nMem ){
81043
- pParse->nMem = iMem+1+(nCol*2);
81044
- }
81045
-
81046
- /* Open a cursor to the index to be analyzed. */
81047
- assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81048
- sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
81049
- (char *)pKey, P4_KEYINFO_HANDOFF);
81050
- VdbeComment((v, "%s", pIdx->zName));
8105181963
8105281964
/* Populate the register containing the index name. */
8105381965
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
8105481966
81055
-#ifdef SQLITE_ENABLE_STAT3
81056
- if( once ){
81057
- once = 0;
81058
- sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81059
- }
81060
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
81061
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
81062
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
81063
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
81064
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
81065
- sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
81066
- sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
81067
- (char*)&stat3InitFuncdef, P4_FUNCDEF);
81068
- sqlite3VdbeChangeP5(v, 2);
81069
-#endif /* SQLITE_ENABLE_STAT3 */
81070
-
81071
- /* The block of memory cells initialized here is used as follows.
81072
- **
81073
- ** iMem:
81074
- ** The total number of rows in the table.
81075
- **
81076
- ** iMem+1 .. iMem+nCol:
81077
- ** Number of distinct entries in index considering the
81078
- ** left-most N columns only, where N is between 1 and nCol,
81079
- ** inclusive.
81080
- **
81081
- ** iMem+nCol+1 .. Mem+2*nCol:
81082
- ** Previous value of indexed columns, from left to right.
81083
- **
81084
- ** Cells iMem through iMem+nCol are initialized to 0. The others are
81085
- ** initialized to contain an SQL NULL.
81086
- */
81087
- for(i=0; i<=nCol; i++){
81088
- sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
81089
- }
81090
- for(i=0; i<nCol; i++){
81091
- sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
81092
- }
81093
-
81094
- /* Start the analysis loop. This loop runs through all the entries in
81095
- ** the index b-tree. */
81096
- endOfLoop = sqlite3VdbeMakeLabel(v);
81097
- sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
81098
- topOfLoop = sqlite3VdbeCurrentAddr(v);
81099
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
81100
-
81101
- for(i=0; i<nCol; i++){
81102
- CollSeq *pColl;
81103
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
81104
- if( i==0 ){
81105
- /* Always record the very first row */
81106
- addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
81107
- }
81108
- assert( pIdx->azColl!=0 );
81109
- assert( pIdx->azColl[i]!=0 );
81110
- pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81111
- aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
81112
- (char*)pColl, P4_COLLSEQ);
81967
+ /*
81968
+ ** Pseudo-code for loop that calls stat_push():
81969
+ **
81970
+ ** Rewind csr
81971
+ ** if eof(csr) goto end_of_scan;
81972
+ ** regChng = 0
81973
+ ** goto chng_addr_0;
81974
+ **
81975
+ ** next_row:
81976
+ ** regChng = 0
81977
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81978
+ ** regChng = 1
81979
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81980
+ ** ...
81981
+ ** regChng = N
81982
+ ** goto chng_addr_N
81983
+ **
81984
+ ** chng_addr_0:
81985
+ ** regPrev(0) = idx(0)
81986
+ ** chng_addr_1:
81987
+ ** regPrev(1) = idx(1)
81988
+ ** ...
81989
+ **
81990
+ ** chng_addr_N:
81991
+ ** regRowid = idx(rowid)
81992
+ ** stat_push(P, regChng, regRowid)
81993
+ ** Next csr
81994
+ ** if !eof(csr) goto next_row;
81995
+ **
81996
+ ** end_of_scan:
81997
+ */
81998
+
81999
+ /* Make sure there are enough memory cells allocated to accommodate
82000
+ ** the regPrev array and a trailing rowid (the rowid slot is required
82001
+ ** when building a record to insert into the sample column of
82002
+ ** the sqlite_stat4 table. */
82003
+ pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
82004
+
82005
+ /* Open a read-only cursor on the index being analyzed. */
82006
+ assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
82007
+ sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
82008
+ sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
82009
+ VdbeComment((v, "%s", pIdx->zName));
82010
+
82011
+ /* Invoke the stat_init() function. The arguments are:
82012
+ **
82013
+ ** (1) the number of columns in the index including the rowid,
82014
+ ** (2) the number of rows in the index,
82015
+ **
82016
+ ** The second argument is only used for STAT3 and STAT4
82017
+ */
82018
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82019
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
82020
+#endif
82021
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
82022
+ sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
82023
+ sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
82024
+ sqlite3VdbeChangeP5(v, 1+IsStat34);
82025
+
82026
+ /* Implementation of the following:
82027
+ **
82028
+ ** Rewind csr
82029
+ ** if eof(csr) goto end_of_scan;
82030
+ ** regChng = 0
82031
+ ** goto next_push_0;
82032
+ **
82033
+ */
82034
+ addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
82035
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
82036
+ addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
82037
+
82038
+ /*
82039
+ ** next_row:
82040
+ ** regChng = 0
82041
+ ** if( idx(0) != regPrev(0) ) goto chng_addr_0
82042
+ ** regChng = 1
82043
+ ** if( idx(1) != regPrev(1) ) goto chng_addr_1
82044
+ ** ...
82045
+ ** regChng = N
82046
+ ** goto chng_addr_N
82047
+ */
82048
+ addrNextRow = sqlite3VdbeCurrentAddr(v);
82049
+ for(i=0; i<nCol; i++){
82050
+ char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
82051
+ sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
82052
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
82053
+ aGotoChng[i] =
82054
+ sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
8111382055
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
81114
- VdbeComment((v, "jump if column %d changed", i));
81115
-#ifdef SQLITE_ENABLE_STAT3
81116
- if( i==0 ){
81117
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
81118
- VdbeComment((v, "incr repeat count"));
81119
- }
81120
-#endif
81121
- }
81122
- sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
81123
- for(i=0; i<nCol; i++){
81124
- sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
81125
- if( i==0 ){
81126
- sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
81127
-#ifdef SQLITE_ENABLE_STAT3
81128
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81129
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
81130
- sqlite3VdbeChangeP5(v, 5);
81131
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
81132
- sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
81133
- sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
81134
- sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
81135
-#endif
81136
- }
81137
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
81138
- sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
81139
- }
81140
- sqlite3DbFree(db, aChngAddr);
81141
-
81142
- /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
81143
- sqlite3VdbeResolveLabel(v, endOfLoop);
81144
-
81145
- sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
81146
- sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81147
-#ifdef SQLITE_ENABLE_STAT3
81148
- sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81149
- (char*)&stat3PushFuncdef, P4_FUNCDEF);
81150
- sqlite3VdbeChangeP5(v, 5);
81151
- sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
81152
- shortJump =
81153
- sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
81154
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
81155
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81156
- sqlite3VdbeChangeP5(v, 2);
81157
- sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
81158
- sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
81159
- sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
81160
- sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
81161
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
81162
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81163
- sqlite3VdbeChangeP5(v, 3);
81164
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
81165
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81166
- sqlite3VdbeChangeP5(v, 4);
81167
- sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
81168
- (char*)&stat3GetFuncdef, P4_FUNCDEF);
81169
- sqlite3VdbeChangeP5(v, 5);
81170
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
81171
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81172
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
81173
- sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
81174
- sqlite3VdbeJumpHere(v, shortJump+2);
81175
-#endif
81176
-
81177
- /* Store the results in sqlite_stat1.
81178
- **
81179
- ** The result is a single row of the sqlite_stat1 table. The first
81180
- ** two columns are the names of the table and index. The third column
81181
- ** is a string composed of a list of integer statistics about the
81182
- ** index. The first integer in the list is the total number of entries
81183
- ** in the index. There is one additional integer in the list for each
81184
- ** column of the table. This additional integer is a guess of how many
81185
- ** rows of the table the index will select. If D is the count of distinct
81186
- ** values and K is the total number of rows, then the integer is computed
81187
- ** as:
81188
- **
81189
- ** I = (K+D-1)/D
81190
- **
81191
- ** If K==0 then no entry is made into the sqlite_stat1 table.
81192
- ** If K>0 then it is always the case the D>0 so division by zero
81193
- ** is never possible.
81194
- */
81195
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
81196
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
81197
- for(i=0; i<nCol; i++){
81198
- sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
81199
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81200
- sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
81201
- sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
81202
- sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
81203
- sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
81204
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81205
- }
81206
- if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81207
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81208
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81209
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81210
- sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81211
- if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81212
- }
82056
+ }
82057
+ sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
82058
+ aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
82059
+
82060
+ /*
82061
+ ** chng_addr_0:
82062
+ ** regPrev(0) = idx(0)
82063
+ ** chng_addr_1:
82064
+ ** regPrev(1) = idx(1)
82065
+ ** ...
82066
+ */
82067
+ sqlite3VdbeJumpHere(v, addrGotoChng0);
82068
+ for(i=0; i<nCol; i++){
82069
+ sqlite3VdbeJumpHere(v, aGotoChng[i]);
82070
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
82071
+ }
82072
+
82073
+ /*
82074
+ ** chng_addr_N:
82075
+ ** regRowid = idx(rowid) // STAT34 only
82076
+ ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
82077
+ ** Next csr
82078
+ ** if !eof(csr) goto next_row;
82079
+ */
82080
+ sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
82081
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82082
+ sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
82083
+ assert( regRowid==(regStat4+2) );
82084
+#endif
82085
+ assert( regChng==(regStat4+1) );
82086
+ sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
82087
+ sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
82088
+ sqlite3VdbeChangeP5(v, 2+IsStat34);
82089
+ sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
82090
+
82091
+ /* Add the entry to the stat1 table. */
82092
+ callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
82093
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
82094
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
82095
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
82096
+ sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
82097
+
82098
+ /* Add the entries to the stat3 or stat4 table. */
82099
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82100
+ {
82101
+ int regEq = regStat1;
82102
+ int regLt = regStat1+1;
82103
+ int regDLt = regStat1+2;
82104
+ int regSample = regStat1+3;
82105
+ int regCol = regStat1+4;
82106
+ int regSampleRowid = regCol + nCol;
82107
+ int addrNext;
82108
+ int addrIsNull;
82109
+
82110
+ pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
82111
+
82112
+ addrNext = sqlite3VdbeCurrentAddr(v);
82113
+ callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
82114
+ addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
82115
+ callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
82116
+ callStatGet(v, regStat4, STAT_GET_NLT, regLt);
82117
+ callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
82118
+ sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
82119
+#ifdef SQLITE_ENABLE_STAT3
82120
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
82121
+ pIdx->aiColumn[0], regSample);
82122
+#else
82123
+ for(i=0; i<nCol; i++){
82124
+ int iCol = pIdx->aiColumn[i];
82125
+ sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
82126
+ }
82127
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
82128
+#endif
82129
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
82130
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
82131
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
82132
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
82133
+ sqlite3VdbeJumpHere(v, addrIsNull);
82134
+ }
82135
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82136
+
82137
+ /* End of analysis */
82138
+ sqlite3VdbeJumpHere(v, addrRewind);
82139
+ sqlite3DbFree(db, aGotoChng);
82140
+ }
82141
+
8121382142
8121482143
/* Create a single sqlite_stat1 entry containing NULL as the index
8121582144
** name and the row count as the content.
8121682145
*/
8121782146
if( pOnlyIdx==0 && needTableCnt ){
81218
- sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
8121982147
VdbeComment((v, "%s", pTab->zName));
81220
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
81221
- sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
82148
+ sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
8122282149
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
8122382150
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
81224
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
82151
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
8122582152
sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81226
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
82153
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
8122782154
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
8122882155
sqlite3VdbeJumpHere(v, jZeroRows);
8122982156
}
81230
- if( pParse->nMem<regRec ) pParse->nMem = regRec;
8123182157
}
8123282158
8123382159
8123482160
/*
8123582161
** Generate code that will cause the most recent index analysis to
@@ -81249,20 +82175,22 @@
8124982175
sqlite3 *db = pParse->db;
8125082176
Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
8125182177
HashElem *k;
8125282178
int iStatCur;
8125382179
int iMem;
82180
+ int iTab;
8125482181
8125582182
sqlite3BeginWriteOperation(pParse, 0, iDb);
8125682183
iStatCur = pParse->nTab;
8125782184
pParse->nTab += 3;
8125882185
openStatTable(pParse, iDb, iStatCur, 0, 0);
8125982186
iMem = pParse->nMem+1;
82187
+ iTab = pParse->nTab;
8126082188
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
8126182189
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
8126282190
Table *pTab = (Table*)sqliteHashData(k);
81263
- analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
82191
+ analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
8126482192
}
8126582193
loadAnalysis(pParse, iDb);
8126682194
}
8126782195
8126882196
/*
@@ -81283,11 +82211,11 @@
8128382211
if( pOnlyIdx ){
8128482212
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
8128582213
}else{
8128682214
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
8128782215
}
81288
- analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
82216
+ analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
8128982217
loadAnalysis(pParse, iDb);
8129082218
}
8129182219
8129282220
/*
8129382221
** Generate code for the ANALYZE command. The parser calls this routine
@@ -81365,10 +82293,47 @@
8136582293
typedef struct analysisInfo analysisInfo;
8136682294
struct analysisInfo {
8136782295
sqlite3 *db;
8136882296
const char *zDatabase;
8136982297
};
82298
+
82299
+/*
82300
+** The first argument points to a nul-terminated string containing a
82301
+** list of space separated integers. Read the first nOut of these into
82302
+** the array aOut[].
82303
+*/
82304
+static void decodeIntArray(
82305
+ char *zIntArray,
82306
+ int nOut,
82307
+ tRowcnt *aOut,
82308
+ int *pbUnordered
82309
+){
82310
+ char *z = zIntArray;
82311
+ int c;
82312
+ int i;
82313
+ tRowcnt v;
82314
+
82315
+ assert( pbUnordered==0 || *pbUnordered==0 );
82316
+
82317
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82318
+ if( z==0 ) z = "";
82319
+#else
82320
+ if( NEVER(z==0) ) z = "";
82321
+#endif
82322
+ for(i=0; *z && i<nOut; i++){
82323
+ v = 0;
82324
+ while( (c=z[0])>='0' && c<='9' ){
82325
+ v = v*10 + c - '0';
82326
+ z++;
82327
+ }
82328
+ aOut[i] = v;
82329
+ if( *z==' ' ) z++;
82330
+ }
82331
+ if( pbUnordered && strcmp(z, "unordered")==0 ){
82332
+ *pbUnordered = 1;
82333
+ }
82334
+}
8137082335
8137182336
/*
8137282337
** This callback is invoked once for each index when reading the
8137382338
** sqlite_stat1 table.
8137482339
**
@@ -81381,12 +82346,10 @@
8138182346
*/
8138282347
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
8138382348
analysisInfo *pInfo = (analysisInfo*)pData;
8138482349
Index *pIndex;
8138582350
Table *pTable;
81386
- int i, c, n;
81387
- tRowcnt v;
8138882351
const char *z;
8138982352
8139082353
assert( argc==3 );
8139182354
UNUSED_PARAMETER2(NotUsed, argc);
8139282355
@@ -81400,45 +82363,35 @@
8140082363
if( argv[1] ){
8140182364
pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
8140282365
}else{
8140382366
pIndex = 0;
8140482367
}
81405
- n = pIndex ? pIndex->nColumn : 0;
8140682368
z = argv[2];
81407
- for(i=0; *z && i<=n; i++){
81408
- v = 0;
81409
- while( (c=z[0])>='0' && c<='9' ){
81410
- v = v*10 + c - '0';
81411
- z++;
81412
- }
81413
- if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
81414
- if( v>0 ) pTable->nRowEst = v;
81415
- if( pIndex==0 ) break;
81416
- }
81417
- pIndex->aiRowEst[i] = v;
81418
- if( *z==' ' ) z++;
81419
- if( strcmp(z, "unordered")==0 ){
81420
- pIndex->bUnordered = 1;
81421
- break;
81422
- }
81423
- }
82369
+
82370
+ if( pIndex ){
82371
+ int bUnordered = 0;
82372
+ decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82373
+ if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82374
+ pIndex->bUnordered = bUnordered;
82375
+ }else{
82376
+ decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
82377
+ }
82378
+
8142482379
return 0;
8142582380
}
8142682381
8142782382
/*
8142882383
** If the Index.aSample variable is not NULL, delete the aSample[] array
8142982384
** and its contents.
8143082385
*/
8143182386
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
81432
-#ifdef SQLITE_ENABLE_STAT3
82387
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8143382388
if( pIdx->aSample ){
8143482389
int j;
8143582390
for(j=0; j<pIdx->nSample; j++){
8143682391
IndexSample *p = &pIdx->aSample[j];
81437
- if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
81438
- sqlite3DbFree(db, p->u.z);
81439
- }
82392
+ sqlite3DbFree(db, p->p);
8144082393
}
8144182394
sqlite3DbFree(db, pIdx->aSample);
8144282395
}
8144382396
if( db && db->pnBytesFreed==0 ){
8144482397
pIdx->nSample = 0;
@@ -81445,155 +82398,222 @@
8144582398
pIdx->aSample = 0;
8144682399
}
8144782400
#else
8144882401
UNUSED_PARAMETER(db);
8144982402
UNUSED_PARAMETER(pIdx);
81450
-#endif
82403
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
8145182404
}
8145282405
81453
-#ifdef SQLITE_ENABLE_STAT3
82406
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8145482407
/*
81455
-** Load content from the sqlite_stat3 table into the Index.aSample[]
81456
-** arrays of all indices.
82408
+** Populate the pIdx->aAvgEq[] array based on the samples currently
82409
+** stored in pIdx->aSample[].
8145782410
*/
81458
-static int loadStat3(sqlite3 *db, const char *zDb){
82411
+static void initAvgEq(Index *pIdx){
82412
+ if( pIdx ){
82413
+ IndexSample *aSample = pIdx->aSample;
82414
+ IndexSample *pFinal = &aSample[pIdx->nSample-1];
82415
+ int iCol;
82416
+ for(iCol=0; iCol<pIdx->nColumn; iCol++){
82417
+ int i; /* Used to iterate through samples */
82418
+ tRowcnt sumEq = 0; /* Sum of the nEq values */
82419
+ tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
82420
+ tRowcnt avgEq = 0;
82421
+ tRowcnt nDLt = pFinal->anDLt[iCol];
82422
+
82423
+ /* Set nSum to the number of distinct (iCol+1) field prefixes that
82424
+ ** occur in the stat4 table for this index before pFinal. Set
82425
+ ** sumEq to the sum of the nEq values for column iCol for the same
82426
+ ** set (adding the value only once where there exist dupicate
82427
+ ** prefixes). */
82428
+ for(i=0; i<(pIdx->nSample-1); i++){
82429
+ if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
82430
+ sumEq += aSample[i].anEq[iCol];
82431
+ nSum++;
82432
+ }
82433
+ }
82434
+ if( nDLt>nSum ){
82435
+ avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
82436
+ }
82437
+ if( avgEq==0 ) avgEq = 1;
82438
+ pIdx->aAvgEq[iCol] = avgEq;
82439
+ if( pIdx->nSampleCol==1 ) break;
82440
+ }
82441
+ }
82442
+}
82443
+
82444
+/*
82445
+** Load the content from either the sqlite_stat4 or sqlite_stat3 table
82446
+** into the relevant Index.aSample[] arrays.
82447
+**
82448
+** Arguments zSql1 and zSql2 must point to SQL statements that return
82449
+** data equivalent to the following (statements are different for stat3,
82450
+** see the caller of this function for details):
82451
+**
82452
+** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
82453
+** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
82454
+**
82455
+** where %Q is replaced with the database name before the SQL is executed.
82456
+*/
82457
+static int loadStatTbl(
82458
+ sqlite3 *db, /* Database handle */
82459
+ int bStat3, /* Assume single column records only */
82460
+ const char *zSql1, /* SQL statement 1 (see above) */
82461
+ const char *zSql2, /* SQL statement 2 (see above) */
82462
+ const char *zDb /* Database name (e.g. "main") */
82463
+){
8145982464
int rc; /* Result codes from subroutines */
8146082465
sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
8146182466
char *zSql; /* Text of the SQL statement */
8146282467
Index *pPrevIdx = 0; /* Previous index in the loop */
81463
- int idx = 0; /* slot in pIdx->aSample[] for next sample */
81464
- int eType; /* Datatype of a sample */
8146582468
IndexSample *pSample; /* A slot in pIdx->aSample[] */
8146682469
8146782470
assert( db->lookaside.bEnabled==0 );
81468
- if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
81469
- return SQLITE_OK;
81470
- }
81471
-
81472
- zSql = sqlite3MPrintf(db,
81473
- "SELECT idx,count(*) FROM %Q.sqlite_stat3"
81474
- " GROUP BY idx", zDb);
82471
+ zSql = sqlite3MPrintf(db, zSql1, zDb);
8147582472
if( !zSql ){
8147682473
return SQLITE_NOMEM;
8147782474
}
8147882475
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
8147982476
sqlite3DbFree(db, zSql);
8148082477
if( rc ) return rc;
8148182478
8148282479
while( sqlite3_step(pStmt)==SQLITE_ROW ){
82480
+ int nIdxCol = 1; /* Number of columns in stat4 records */
82481
+ int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
82482
+
8148382483
char *zIndex; /* Index name */
8148482484
Index *pIdx; /* Pointer to the index object */
8148582485
int nSample; /* Number of samples */
82486
+ int nByte; /* Bytes of space required */
82487
+ int i; /* Bytes of space required */
82488
+ tRowcnt *pSpace;
8148682489
8148782490
zIndex = (char *)sqlite3_column_text(pStmt, 0);
8148882491
if( zIndex==0 ) continue;
8148982492
nSample = sqlite3_column_int(pStmt, 1);
8149082493
pIdx = sqlite3FindIndex(db, zIndex, zDb);
81491
- if( pIdx==0 ) continue;
81492
- assert( pIdx->nSample==0 );
81493
- pIdx->nSample = nSample;
81494
- pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
81495
- pIdx->avgEq = pIdx->aiRowEst[1];
82494
+ assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
82495
+ /* Index.nSample is non-zero at this point if data has already been
82496
+ ** loaded from the stat4 table. In this case ignore stat3 data. */
82497
+ if( pIdx==0 || pIdx->nSample ) continue;
82498
+ if( bStat3==0 ){
82499
+ nIdxCol = pIdx->nColumn+1;
82500
+ nAvgCol = pIdx->nColumn;
82501
+ }
82502
+ pIdx->nSampleCol = nIdxCol;
82503
+ nByte = sizeof(IndexSample) * nSample;
82504
+ nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
82505
+ nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
82506
+
82507
+ pIdx->aSample = sqlite3DbMallocZero(db, nByte);
8149682508
if( pIdx->aSample==0 ){
81497
- db->mallocFailed = 1;
8149882509
sqlite3_finalize(pStmt);
8149982510
return SQLITE_NOMEM;
8150082511
}
82512
+ pSpace = (tRowcnt*)&pIdx->aSample[nSample];
82513
+ pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
82514
+ for(i=0; i<nSample; i++){
82515
+ pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
82516
+ pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
82517
+ pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
82518
+ }
82519
+ assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
8150182520
}
8150282521
rc = sqlite3_finalize(pStmt);
8150382522
if( rc ) return rc;
8150482523
81505
- zSql = sqlite3MPrintf(db,
81506
- "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
82524
+ zSql = sqlite3MPrintf(db, zSql2, zDb);
8150782525
if( !zSql ){
8150882526
return SQLITE_NOMEM;
8150982527
}
8151082528
rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
8151182529
sqlite3DbFree(db, zSql);
8151282530
if( rc ) return rc;
8151382531
8151482532
while( sqlite3_step(pStmt)==SQLITE_ROW ){
81515
- char *zIndex; /* Index name */
81516
- Index *pIdx; /* Pointer to the index object */
81517
- int i; /* Loop counter */
81518
- tRowcnt sumEq; /* Sum of the nEq values */
82533
+ char *zIndex; /* Index name */
82534
+ Index *pIdx; /* Pointer to the index object */
82535
+ int nCol = 1; /* Number of columns in index */
8151982536
8152082537
zIndex = (char *)sqlite3_column_text(pStmt, 0);
8152182538
if( zIndex==0 ) continue;
8152282539
pIdx = sqlite3FindIndex(db, zIndex, zDb);
8152382540
if( pIdx==0 ) continue;
81524
- if( pIdx==pPrevIdx ){
81525
- idx++;
81526
- }else{
82541
+ /* This next condition is true if data has already been loaded from
82542
+ ** the sqlite_stat4 table. In this case ignore stat3 data. */
82543
+ nCol = pIdx->nSampleCol;
82544
+ if( bStat3 && nCol>1 ) continue;
82545
+ if( pIdx!=pPrevIdx ){
82546
+ initAvgEq(pPrevIdx);
8152782547
pPrevIdx = pIdx;
81528
- idx = 0;
81529
- }
81530
- assert( idx<pIdx->nSample );
81531
- pSample = &pIdx->aSample[idx];
81532
- pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
81533
- pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
81534
- pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
81535
- if( idx==pIdx->nSample-1 ){
81536
- if( pSample->nDLt>0 ){
81537
- for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
81538
- pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
81539
- }
81540
- if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
81541
- }
81542
- eType = sqlite3_column_type(pStmt, 4);
81543
- pSample->eType = (u8)eType;
81544
- switch( eType ){
81545
- case SQLITE_INTEGER: {
81546
- pSample->u.i = sqlite3_column_int64(pStmt, 4);
81547
- break;
81548
- }
81549
- case SQLITE_FLOAT: {
81550
- pSample->u.r = sqlite3_column_double(pStmt, 4);
81551
- break;
81552
- }
81553
- case SQLITE_NULL: {
81554
- break;
81555
- }
81556
- default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
81557
- const char *z = (const char *)(
81558
- (eType==SQLITE_BLOB) ?
81559
- sqlite3_column_blob(pStmt, 4):
81560
- sqlite3_column_text(pStmt, 4)
81561
- );
81562
- int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
81563
- pSample->nByte = n;
81564
- if( n < 1){
81565
- pSample->u.z = 0;
81566
- }else{
81567
- pSample->u.z = sqlite3DbMallocRaw(db, n);
81568
- if( pSample->u.z==0 ){
81569
- db->mallocFailed = 1;
81570
- sqlite3_finalize(pStmt);
81571
- return SQLITE_NOMEM;
81572
- }
81573
- memcpy(pSample->u.z, z, n);
81574
- }
81575
- }
81576
- }
81577
- }
81578
- return sqlite3_finalize(pStmt);
81579
-}
81580
-#endif /* SQLITE_ENABLE_STAT3 */
81581
-
81582
-/*
81583
-** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
82548
+ }
82549
+ pSample = &pIdx->aSample[pIdx->nSample];
82550
+ decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
82551
+ decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
82552
+ decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
82553
+
82554
+ /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
82555
+ ** This is in case the sample record is corrupted. In that case, the
82556
+ ** sqlite3VdbeRecordCompare() may read up to two varints past the
82557
+ ** end of the allocated buffer before it realizes it is dealing with
82558
+ ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
82559
+ ** a buffer overread. */
82560
+ pSample->n = sqlite3_column_bytes(pStmt, 4);
82561
+ pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
82562
+ if( pSample->p==0 ){
82563
+ sqlite3_finalize(pStmt);
82564
+ return SQLITE_NOMEM;
82565
+ }
82566
+ memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
82567
+ pIdx->nSample++;
82568
+ }
82569
+ rc = sqlite3_finalize(pStmt);
82570
+ if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
82571
+ return rc;
82572
+}
82573
+
82574
+/*
82575
+** Load content from the sqlite_stat4 and sqlite_stat3 tables into
82576
+** the Index.aSample[] arrays of all indices.
82577
+*/
82578
+static int loadStat4(sqlite3 *db, const char *zDb){
82579
+ int rc = SQLITE_OK; /* Result codes from subroutines */
82580
+
82581
+ assert( db->lookaside.bEnabled==0 );
82582
+ if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
82583
+ rc = loadStatTbl(db, 0,
82584
+ "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
82585
+ "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
82586
+ zDb
82587
+ );
82588
+ }
82589
+
82590
+ if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
82591
+ rc = loadStatTbl(db, 1,
82592
+ "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
82593
+ "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
82594
+ zDb
82595
+ );
82596
+ }
82597
+
82598
+ return rc;
82599
+}
82600
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82601
+
82602
+/*
82603
+** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
8158482604
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
81585
-** arrays. The contents of sqlite_stat3 are used to populate the
82605
+** arrays. The contents of sqlite_stat3/4 are used to populate the
8158682606
** Index.aSample[] arrays.
8158782607
**
8158882608
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
81589
-** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
81590
-** during compilation and the sqlite_stat3 table is present, no data is
82609
+** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
82610
+** during compilation and the sqlite_stat3/4 table is present, no data is
8159182611
** read from it.
8159282612
**
81593
-** If SQLITE_ENABLE_STAT3 was defined during compilation and the
81594
-** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
82613
+** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
82614
+** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
8159582615
** returned. However, in this case, data is read from the sqlite_stat1
8159682616
** table (if it is present) before returning.
8159782617
**
8159882618
** If an OOM error occurs, this function always sets db->mallocFailed.
8159982619
** This means if the caller does not care about other errors, the return
@@ -81611,11 +82631,11 @@
8161182631
/* Clear any prior statistics */
8161282632
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
8161382633
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
8161482634
Index *pIdx = sqliteHashData(i);
8161582635
sqlite3DefaultRowEst(pIdx);
81616
-#ifdef SQLITE_ENABLE_STAT3
82636
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8161782637
sqlite3DeleteIndexSamples(db, pIdx);
8161882638
pIdx->aSample = 0;
8161982639
#endif
8162082640
}
8162182641
@@ -81635,16 +82655,16 @@
8163582655
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
8163682656
sqlite3DbFree(db, zSql);
8163782657
}
8163882658
8163982659
81640
- /* Load the statistics from the sqlite_stat3 table. */
81641
-#ifdef SQLITE_ENABLE_STAT3
82660
+ /* Load the statistics from the sqlite_stat4 table. */
82661
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
8164282662
if( rc==SQLITE_OK ){
8164382663
int lookasideEnabled = db->lookaside.bEnabled;
8164482664
db->lookaside.bEnabled = 0;
81645
- rc = loadStat3(db, sInfo.zDatabase);
82665
+ rc = loadStat4(db, sInfo.zDatabase);
8164682666
db->lookaside.bEnabled = lookasideEnabled;
8164782667
}
8164882668
#endif
8164982669
8165082670
if( rc==SQLITE_NOMEM ){
@@ -84496,11 +85516,11 @@
8449685516
const char *zType, /* "idx" or "tbl" */
8449785517
const char *zName /* Name of index or table */
8449885518
){
8449985519
int i;
8450085520
const char *zDbName = pParse->db->aDb[iDb].zName;
84501
- for(i=1; i<=3; i++){
85521
+ for(i=1; i<=4; i++){
8450285522
char zTab[24];
8450385523
sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
8450485524
if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
8450585525
sqlite3NestedParse(pParse,
8450685526
"DELETE FROM %Q.%s WHERE %s=%Q",
@@ -89167,10 +90187,13 @@
8916790187
sqlite3FuncDefInsert(pHash, &aFunc[i]);
8916890188
}
8916990189
sqlite3RegisterDateTimeFunctions();
8917090190
#ifndef SQLITE_OMIT_ALTERTABLE
8917190191
sqlite3AlterFunctions();
90192
+#endif
90193
+#if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
90194
+ sqlite3AnalyzeFunctions();
8917290195
#endif
8917390196
}
8917490197
8917590198
/************** End of func.c ************************************************/
8917690199
/************** Begin file fkey.c ********************************************/
@@ -94387,11 +95410,11 @@
9438795410
*/
9438895411
if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
9438995412
Pager *pPager = sqlite3BtreePager(pDb->pBt);
9439095413
i64 iLimit = -2;
9439195414
if( zRight ){
94392
- sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
95415
+ sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
9439395416
if( iLimit<-1 ) iLimit = -1;
9439495417
}
9439595418
iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
9439695419
returnSingleInt(pParse, "journal_size_limit", iLimit);
9439795420
}else
@@ -94521,14 +95544,15 @@
9452195544
** as little or as much as it wants. Except, if N is set to 0 then the
9452295545
** upper layers will never invoke the xFetch interfaces to the VFS.
9452395546
*/
9452495547
if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
9452595548
sqlite3_int64 sz;
95549
+#if SQLITE_MAX_MMAP_SIZE>0
9452695550
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
9452795551
if( zRight ){
9452895552
int ii;
94529
- sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
95553
+ sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
9453095554
if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
9453195555
if( pId2->n==0 ) db->szMmap = sz;
9453295556
for(ii=db->nDb-1; ii>=0; ii--){
9453395557
if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
9453495558
sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94535,12 +95559,13 @@
9453595559
}
9453695560
}
9453795561
}
9453895562
sz = -1;
9453995563
rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
94540
-#if SQLITE_MAX_MMAP_SIZE==0
95564
+#else
9454195565
sz = 0;
95566
+ rc = SQLITE_OK;
9454295567
#endif
9454395568
if( rc==SQLITE_OK ){
9454495569
returnSingleInt(pParse, "mmap_size", sz);
9454595570
}else if( rc!=SQLITE_NOTFOUND ){
9454695571
pParse->nErr++;
@@ -102688,11 +103713,11 @@
102688103713
** space.
102689103714
*/
102690103715
SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
102691103716
assert( pTab!=0 );
102692103717
if( !pTab->pSelect ){
102693
- sqlite3_value *pValue;
103718
+ sqlite3_value *pValue = 0;
102694103719
u8 enc = ENC(sqlite3VdbeDb(v));
102695103720
Column *pCol = &pTab->aCol[i];
102696103721
VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
102697103722
assert( i<pTab->nCol );
102698103723
sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104937,13 +105962,13 @@
104937105962
/*
104938105963
** Each instance of this object holds a sequence of WhereLoop objects
104939105964
** that implement some or all of a query plan.
104940105965
**
104941105966
** Think of each WhereLoop object as a node in a graph with arcs
104942
-** showing dependences and costs for travelling between nodes. (That is
105967
+** showing dependencies and costs for travelling between nodes. (That is
104943105968
** not a completely accurate description because WhereLoop costs are a
104944
-** vector, not a scalar, and because dependences are many-to-one, not
105969
+** vector, not a scalar, and because dependencies are many-to-one, not
104945105970
** one-to-one as are graph nodes. But it is a useful visualization aid.)
104946105971
** Then a WherePath object is a path through the graph that visits some
104947105972
** or all of the WhereLoop objects once.
104948105973
**
104949105974
** The "solver" works by creating the N best WherePath objects of length
@@ -105038,11 +106063,11 @@
105038106063
#define TERM_CODED 0x04 /* This term is already coded */
105039106064
#define TERM_COPIED 0x08 /* Has a child */
105040106065
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
105041106066
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
105042106067
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
105043
-#ifdef SQLITE_ENABLE_STAT3
106068
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
105044106069
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
105045106070
#else
105046106071
# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
105047106072
#endif
105048106073
@@ -105144,10 +106169,14 @@
105144106169
WhereInfo *pWInfo; /* Information about this WHERE */
105145106170
WhereClause *pWC; /* WHERE clause terms */
105146106171
ExprList *pOrderBy; /* ORDER BY clause */
105147106172
WhereLoop *pNew; /* Template WhereLoop */
105148106173
WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
106174
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106175
+ UnpackedRecord *pRec; /* Probe for stat4 (if required) */
106176
+ int nRecValid; /* Number of valid fields currently in pRec */
106177
+#endif
105149106178
};
105150106179
105151106180
/*
105152106181
** The WHERE clause processing routine has two halves. The
105153106182
** first part does the start of the WHERE loop and the second
@@ -106543,11 +107572,11 @@
106543107572
pNewTerm->prereqAll = pTerm->prereqAll;
106544107573
}
106545107574
}
106546107575
#endif /* SQLITE_OMIT_VIRTUALTABLE */
106547107576
106548
-#ifdef SQLITE_ENABLE_STAT3
107577
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106549107578
/* When sqlite_stat3 histogram data is available an operator of the
106550107579
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
106551107580
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
106552107581
** virtual term of that form.
106553107582
**
@@ -106583,11 +107612,11 @@
106583107612
pTerm->nChild = 1;
106584107613
pTerm->wtFlags |= TERM_COPIED;
106585107614
pNewTerm->prereqAll = pTerm->prereqAll;
106586107615
}
106587107616
}
106588
-#endif /* SQLITE_ENABLE_STAT */
107617
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
106589107618
106590107619
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
106591107620
** an index for tables to the left of the join.
106592107621
*/
106593107622
pTerm->prereqRight |= extraRight;
@@ -107151,155 +108180,87 @@
107151108180
return pParse->nErr;
107152108181
}
107153108182
#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
107154108183
107155108184
107156
-#ifdef SQLITE_ENABLE_STAT3
108185
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107157108186
/*
107158108187
** Estimate the location of a particular key among all keys in an
107159108188
** index. Store the results in aStat as follows:
107160108189
**
107161108190
** aStat[0] Est. number of rows less than pVal
107162108191
** aStat[1] Est. number of rows equal to pVal
107163108192
**
107164108193
** Return SQLITE_OK on success.
107165108194
*/
107166
-static int whereKeyStats(
108195
+static void whereKeyStats(
107167108196
Parse *pParse, /* Database connection */
107168108197
Index *pIdx, /* Index to consider domain of */
107169
- sqlite3_value *pVal, /* Value to consider */
108198
+ UnpackedRecord *pRec, /* Vector of values to consider */
107170108199
int roundUp, /* Round up if true. Round down if false */
107171108200
tRowcnt *aStat /* OUT: stats written here */
107172108201
){
107173
- tRowcnt n;
107174
- IndexSample *aSample;
107175
- int i, eType;
107176
- int isEq = 0;
107177
- i64 v;
107178
- double r, rS;
107179
-
107180
- assert( roundUp==0 || roundUp==1 );
108202
+ IndexSample *aSample = pIdx->aSample;
108203
+ int iCol; /* Index of required stats in anEq[] etc. */
108204
+ int iMin = 0; /* Smallest sample not yet tested */
108205
+ int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
108206
+ int iTest; /* Next sample to test */
108207
+ int res; /* Result of comparison operation */
108208
+
108209
+ assert( pRec!=0 || pParse->db->mallocFailed );
108210
+ if( pRec==0 ) return;
108211
+ iCol = pRec->nField - 1;
107181108212
assert( pIdx->nSample>0 );
107182
- if( pVal==0 ) return SQLITE_ERROR;
107183
- n = pIdx->aiRowEst[0];
107184
- aSample = pIdx->aSample;
107185
- eType = sqlite3_value_type(pVal);
107186
-
107187
- if( eType==SQLITE_INTEGER ){
107188
- v = sqlite3_value_int64(pVal);
107189
- r = (i64)v;
107190
- for(i=0; i<pIdx->nSample; i++){
107191
- if( aSample[i].eType==SQLITE_NULL ) continue;
107192
- if( aSample[i].eType>=SQLITE_TEXT ) break;
107193
- if( aSample[i].eType==SQLITE_INTEGER ){
107194
- if( aSample[i].u.i>=v ){
107195
- isEq = aSample[i].u.i==v;
107196
- break;
107197
- }
107198
- }else{
107199
- assert( aSample[i].eType==SQLITE_FLOAT );
107200
- if( aSample[i].u.r>=r ){
107201
- isEq = aSample[i].u.r==r;
107202
- break;
107203
- }
107204
- }
107205
- }
107206
- }else if( eType==SQLITE_FLOAT ){
107207
- r = sqlite3_value_double(pVal);
107208
- for(i=0; i<pIdx->nSample; i++){
107209
- if( aSample[i].eType==SQLITE_NULL ) continue;
107210
- if( aSample[i].eType>=SQLITE_TEXT ) break;
107211
- if( aSample[i].eType==SQLITE_FLOAT ){
107212
- rS = aSample[i].u.r;
107213
- }else{
107214
- rS = aSample[i].u.i;
107215
- }
107216
- if( rS>=r ){
107217
- isEq = rS==r;
107218
- break;
107219
- }
107220
- }
107221
- }else if( eType==SQLITE_NULL ){
107222
- i = 0;
107223
- if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
107224
- }else{
107225
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
107226
- for(i=0; i<pIdx->nSample; i++){
107227
- if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
107228
- break;
107229
- }
107230
- }
107231
- if( i<pIdx->nSample ){
107232
- sqlite3 *db = pParse->db;
107233
- CollSeq *pColl;
107234
- const u8 *z;
107235
- if( eType==SQLITE_BLOB ){
107236
- z = (const u8 *)sqlite3_value_blob(pVal);
107237
- pColl = db->pDfltColl;
107238
- assert( pColl->enc==SQLITE_UTF8 );
107239
- }else{
107240
- pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
107241
- /* If the collating sequence was unavailable, we should have failed
107242
- ** long ago and never reached this point. But we'll check just to
107243
- ** be doubly sure. */
107244
- if( NEVER(pColl==0) ) return SQLITE_ERROR;
107245
- z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
107246
- if( !z ){
107247
- return SQLITE_NOMEM;
107248
- }
107249
- assert( z && pColl && pColl->xCmp );
107250
- }
107251
- n = sqlite3ValueBytes(pVal, pColl->enc);
107252
-
107253
- for(; i<pIdx->nSample; i++){
107254
- int c;
107255
- int eSampletype = aSample[i].eType;
107256
- if( eSampletype<eType ) continue;
107257
- if( eSampletype!=eType ) break;
107258
-#ifndef SQLITE_OMIT_UTF16
107259
- if( pColl->enc!=SQLITE_UTF8 ){
107260
- int nSample;
107261
- char *zSample = sqlite3Utf8to16(
107262
- db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
107263
- );
107264
- if( !zSample ){
107265
- assert( db->mallocFailed );
107266
- return SQLITE_NOMEM;
107267
- }
107268
- c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
107269
- sqlite3DbFree(db, zSample);
107270
- }else
107271
-#endif
107272
- {
107273
- c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
107274
- }
107275
- if( c>=0 ){
107276
- if( c==0 ) isEq = 1;
107277
- break;
107278
- }
107279
- }
107280
- }
107281
- }
108213
+ assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
108214
+ do{
108215
+ iTest = (iMin+i)/2;
108216
+ res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
108217
+ if( res<0 ){
108218
+ iMin = iTest+1;
108219
+ }else{
108220
+ i = iTest;
108221
+ }
108222
+ }while( res && iMin<i );
108223
+
108224
+#ifdef SQLITE_DEBUG
108225
+ /* The following assert statements check that the binary search code
108226
+ ** above found the right answer. This block serves no purpose other
108227
+ ** than to invoke the asserts. */
108228
+ if( res==0 ){
108229
+ /* If (res==0) is true, then sample $i must be equal to pRec */
108230
+ assert( i<pIdx->nSample );
108231
+ assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
108232
+ || pParse->db->mallocFailed );
108233
+ }else{
108234
+ /* Otherwise, pRec must be smaller than sample $i and larger than
108235
+ ** sample ($i-1). */
108236
+ assert( i==pIdx->nSample
108237
+ || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
108238
+ || pParse->db->mallocFailed );
108239
+ assert( i==0
108240
+ || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
108241
+ || pParse->db->mallocFailed );
108242
+ }
108243
+#endif /* ifdef SQLITE_DEBUG */
107282108244
107283108245
/* At this point, aSample[i] is the first sample that is greater than
107284108246
** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
107285
- ** than pVal. If aSample[i]==pVal, then isEq==1.
108247
+ ** than pVal. If aSample[i]==pVal, then res==0.
107286108248
*/
107287
- if( isEq ){
107288
- assert( i<pIdx->nSample );
107289
- aStat[0] = aSample[i].nLt;
107290
- aStat[1] = aSample[i].nEq;
108249
+ if( res==0 ){
108250
+ aStat[0] = aSample[i].anLt[iCol];
108251
+ aStat[1] = aSample[i].anEq[iCol];
107291108252
}else{
107292108253
tRowcnt iLower, iUpper, iGap;
107293108254
if( i==0 ){
107294108255
iLower = 0;
107295
- iUpper = aSample[0].nLt;
108256
+ iUpper = aSample[0].anLt[iCol];
107296108257
}else{
107297
- iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
107298
- iLower = aSample[i-1].nEq + aSample[i-1].nLt;
108258
+ iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
108259
+ iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
107299108260
}
107300
- aStat[1] = pIdx->avgEq;
108261
+ aStat[1] = (pIdx->nColumn>iCol ? pIdx->aAvgEq[iCol] : 1);
107301108262
if( iLower>=iUpper ){
107302108263
iGap = 0;
107303108264
}else{
107304108265
iGap = iUpper - iLower;
107305108266
}
@@ -107308,48 +108269,12 @@
107308108269
}else{
107309108270
iGap = iGap/3;
107310108271
}
107311108272
aStat[0] = iLower + iGap;
107312108273
}
107313
- return SQLITE_OK;
107314
-}
107315
-#endif /* SQLITE_ENABLE_STAT3 */
107316
-
107317
-/*
107318
-** If expression pExpr represents a literal value, set *pp to point to
107319
-** an sqlite3_value structure containing the same value, with affinity
107320
-** aff applied to it, before returning. It is the responsibility of the
107321
-** caller to eventually release this structure by passing it to
107322
-** sqlite3ValueFree().
107323
-**
107324
-** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
107325
-** is an SQL variable that currently has a non-NULL value bound to it,
107326
-** create an sqlite3_value structure containing this value, again with
107327
-** affinity aff applied to it, instead.
107328
-**
107329
-** If neither of the above apply, set *pp to NULL.
107330
-**
107331
-** If an error occurs, return an error code. Otherwise, SQLITE_OK.
107332
-*/
107333
-#ifdef SQLITE_ENABLE_STAT3
107334
-static int valueFromExpr(
107335
- Parse *pParse,
107336
- Expr *pExpr,
107337
- u8 aff,
107338
- sqlite3_value **pp
107339
-){
107340
- if( pExpr->op==TK_VARIABLE
107341
- || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
107342
- ){
107343
- int iVar = pExpr->iColumn;
107344
- sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
107345
- *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff);
107346
- return SQLITE_OK;
107347
- }
107348
- return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
107349
-}
107350
-#endif
108274
+}
108275
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107351108276
107352108277
/*
107353108278
** This function is used to estimate the number of rows that will be visited
107354108279
** by scanning an index for a range of values. The range may have an upper
107355108280
** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107362,107 +108287,159 @@
107362108287
** pLower pUpper
107363108288
**
107364108289
** If either of the upper or lower bound is not present, then NULL is passed in
107365108290
** place of the corresponding WhereTerm.
107366108291
**
107367
-** The nEq parameter is passed the index of the index column subject to the
107368
-** range constraint. Or, equivalently, the number of equality constraints
107369
-** optimized by the proposed index scan. For example, assuming index p is
107370
-** on t1(a, b), and the SQL query is:
108292
+** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
108293
+** column subject to the range constraint. Or, equivalently, the number of
108294
+** equality constraints optimized by the proposed index scan. For example,
108295
+** assuming index p is on t1(a, b), and the SQL query is:
107371108296
**
107372108297
** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
107373108298
**
107374
-** then nEq should be passed the value 1 (as the range restricted column,
107375
-** b, is the second left-most column of the index). Or, if the query is:
108299
+** then nEq is set to 1 (as the range restricted column, b, is the second
108300
+** left-most column of the index). Or, if the query is:
107376108301
**
107377108302
** ... FROM t1 WHERE a > ? AND a < ? ...
107378108303
**
107379
-** then nEq should be passed 0.
107380
-**
107381
-** The returned value is an integer divisor to reduce the estimated
107382
-** search space. A return value of 1 means that range constraints are
107383
-** no help at all. A return value of 2 means range constraints are
107384
-** expected to reduce the search space by half. And so forth...
107385
-**
107386
-** In the absence of sqlite_stat3 ANALYZE data, each range inequality
107387
-** reduces the search space by a factor of 4. Hence a single constraint (x>?)
107388
-** results in a return of 4 and a range constraint (x>? AND x<?) results
107389
-** in a return of 16.
108304
+** then nEq is set to 0.
108305
+**
108306
+** When this function is called, *pnOut is set to the whereCost() of the
108307
+** number of rows that the index scan is expected to visit without
108308
+** considering the range constraints. If nEq is 0, this is the number of
108309
+** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108310
+** to account for the range contraints pLower and pUpper.
108311
+**
108312
+** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
108313
+** used, each range inequality reduces the search space by a factor of 4.
108314
+** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
108315
+** rows visited by a factor of 16.
107390108316
*/
107391108317
static int whereRangeScanEst(
107392108318
Parse *pParse, /* Parsing & code generating context */
107393
- Index *p, /* The index containing the range-compared column; "x" */
107394
- int nEq, /* index into p->aCol[] of the range-compared column */
108319
+ WhereLoopBuilder *pBuilder,
107395108320
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
107396108321
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
107397
- WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
108322
+ WhereCost *pnOut /* IN/OUT: Number of rows visited */
107398108323
){
107399108324
int rc = SQLITE_OK;
108325
+ int nOut = (int)*pnOut;
107400108326
107401
-#ifdef SQLITE_ENABLE_STAT3
108327
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108328
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108329
+ int nEq = pBuilder->pNew->u.btree.nEq;
107402108330
107403
- if( nEq==0 && p->nSample && OptimizationEnabled(pParse->db, SQLITE_Stat3) ){
107404
- sqlite3_value *pRangeVal;
107405
- tRowcnt iLower = 0;
107406
- tRowcnt iUpper = p->aiRowEst[0];
108331
+ if( nEq==pBuilder->nRecValid
108332
+ && nEq<p->nSampleCol
108333
+ && p->nSample
108334
+ && OptimizationEnabled(pParse->db, SQLITE_Stat3)
108335
+ ){
108336
+ UnpackedRecord *pRec = pBuilder->pRec;
107407108337
tRowcnt a[2];
107408
- u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
108338
+ u8 aff;
107409108339
108340
+ /* Variable iLower will be set to the estimate of the number of rows in
108341
+ ** the index that are less than the lower bound of the range query. The
108342
+ ** lower bound being the concatenation of $P and $L, where $P is the
108343
+ ** key-prefix formed by the nEq values matched against the nEq left-most
108344
+ ** columns of the index, and $L is the value in pLower.
108345
+ **
108346
+ ** Or, if pLower is NULL or $L cannot be extracted from it (because it
108347
+ ** is not a simple variable or literal value), the lower bound of the
108348
+ ** range is $P. Due to a quirk in the way whereKeyStats() works, even
108349
+ ** if $L is available, whereKeyStats() is called for both ($P) and
108350
+ ** ($P:$L) and the larger of the two returned values used.
108351
+ **
108352
+ ** Similarly, iUpper is to be set to the estimate of the number of rows
108353
+ ** less than the upper bound of the range query. Where the upper bound
108354
+ ** is either ($P) or ($P:$U). Again, even if $U is available, both values
108355
+ ** of iUpper are requested of whereKeyStats() and the smaller used.
108356
+ */
108357
+ tRowcnt iLower;
108358
+ tRowcnt iUpper;
108359
+
108360
+ if( nEq==p->nColumn ){
108361
+ aff = SQLITE_AFF_INTEGER;
108362
+ }else{
108363
+ aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
108364
+ }
108365
+ /* Determine iLower and iUpper using ($P) only. */
108366
+ if( nEq==0 ){
108367
+ iLower = 0;
108368
+ iUpper = p->aiRowEst[0];
108369
+ }else{
108370
+ /* Note: this call could be optimized away - since the same values must
108371
+ ** have been requested when testing key $P in whereEqualScanEst(). */
108372
+ whereKeyStats(pParse, p, pRec, 0, a);
108373
+ iLower = a[0];
108374
+ iUpper = a[0] + a[1];
108375
+ }
108376
+
108377
+ /* If possible, improve on the iLower estimate using ($P:$L). */
107410108378
if( pLower ){
108379
+ int bOk; /* True if value is extracted from pExpr */
107411108380
Expr *pExpr = pLower->pExpr->pRight;
107412
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
107413108381
assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
107414
- if( rc==SQLITE_OK
107415
- && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
107416
- ){
107417
- iLower = a[0];
107418
- if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1];
107419
- }
107420
- sqlite3ValueFree(pRangeVal);
107421
- }
107422
- if( rc==SQLITE_OK && pUpper ){
107423
- Expr *pExpr = pUpper->pExpr->pRight;
107424
- rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
108382
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108383
+ if( rc==SQLITE_OK && bOk ){
108384
+ tRowcnt iNew;
108385
+ whereKeyStats(pParse, p, pRec, 0, a);
108386
+ iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
108387
+ if( iNew>iLower ) iLower = iNew;
108388
+ }
108389
+ }
108390
+
108391
+ /* If possible, improve on the iUpper estimate using ($P:$U). */
108392
+ if( pUpper ){
108393
+ int bOk; /* True if value is extracted from pExpr */
108394
+ Expr *pExpr = pUpper->pExpr->pRight;
107425108395
assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
107426
- if( rc==SQLITE_OK
107427
- && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
107428
- ){
107429
- iUpper = a[0];
107430
- if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
107431
- }
107432
- sqlite3ValueFree(pRangeVal);
107433
- }
108396
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108397
+ if( rc==SQLITE_OK && bOk ){
108398
+ tRowcnt iNew;
108399
+ whereKeyStats(pParse, p, pRec, 1, a);
108400
+ iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
108401
+ if( iNew<iUpper ) iUpper = iNew;
108402
+ }
108403
+ }
108404
+
108405
+ pBuilder->pRec = pRec;
107434108406
if( rc==SQLITE_OK ){
107435
- WhereCost iBase = whereCost(p->aiRowEst[0]);
108407
+ WhereCost nNew;
107436108408
if( iUpper>iLower ){
107437
- iBase -= whereCost(iUpper - iLower);
108409
+ nNew = whereCost(iUpper - iLower);
108410
+ }else{
108411
+ nNew = 10; assert( 10==whereCost(2) );
107438108412
}
107439
- *pRangeDiv = iBase;
107440
- WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n",
107441
- (u32)iLower, (u32)iUpper, *pRangeDiv));
108413
+ if( nNew<nOut ){
108414
+ nOut = nNew;
108415
+ }
108416
+ *pnOut = (WhereCost)nOut;
108417
+ WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
108418
+ (u32)iLower, (u32)iUpper, nOut));
107442108419
return SQLITE_OK;
107443108420
}
107444108421
}
107445108422
#else
107446108423
UNUSED_PARAMETER(pParse);
107447
- UNUSED_PARAMETER(p);
107448
- UNUSED_PARAMETER(nEq);
108424
+ UNUSED_PARAMETER(pBuilder);
107449108425
#endif
107450108426
assert( pLower || pUpper );
107451
- *pRangeDiv = 0;
107452108427
/* TUNING: Each inequality constraint reduces the search space 4-fold.
107453108428
** A BETWEEN operator, therefore, reduces the search space 16-fold */
107454108429
if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
107455
- *pRangeDiv += 20; assert( 20==whereCost(4) );
108430
+ nOut -= 20; assert( 20==whereCost(4) );
107456108431
}
107457108432
if( pUpper ){
107458
- *pRangeDiv += 20; assert( 20==whereCost(4) );
108433
+ nOut -= 20; assert( 20==whereCost(4) );
107459108434
}
108435
+ if( nOut<10 ) nOut = 10;
108436
+ *pnOut = (WhereCost)nOut;
107460108437
return rc;
107461108438
}
107462108439
107463
-#ifdef SQLITE_ENABLE_STAT3
108440
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107464108441
/*
107465108442
** Estimate the number of rows that will be returned based on
107466108443
** an equality constraint x=VALUE and where that VALUE occurs in
107467108444
** the histogram data. This only works when x is the left-most
107468108445
** column of an index and sqlite_stat3 histogram data is available
@@ -107478,41 +108455,57 @@
107478108455
** for a UTF conversion required for comparison. The error is stored
107479108456
** in the pParse structure.
107480108457
*/
107481108458
static int whereEqualScanEst(
107482108459
Parse *pParse, /* Parsing & code generating context */
107483
- Index *p, /* The index whose left-most column is pTerm */
108460
+ WhereLoopBuilder *pBuilder,
107484108461
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
107485108462
tRowcnt *pnRow /* Write the revised row estimate here */
107486108463
){
107487
- sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
108464
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108465
+ int nEq = pBuilder->pNew->u.btree.nEq;
108466
+ UnpackedRecord *pRec = pBuilder->pRec;
107488108467
u8 aff; /* Column affinity */
107489108468
int rc; /* Subfunction return code */
107490108469
tRowcnt a[2]; /* Statistics */
108470
+ int bOk;
107491108471
108472
+ assert( nEq>=1 );
108473
+ assert( nEq<=(p->nColumn+1) );
107492108474
assert( p->aSample!=0 );
107493108475
assert( p->nSample>0 );
107494
- aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107495
- if( pExpr ){
107496
- rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
107497
- if( rc ) goto whereEqualScanEst_cancel;
107498
- }else{
107499
- pRhs = sqlite3ValueNew(pParse->db);
107500
- }
107501
- if( pRhs==0 ) return SQLITE_NOTFOUND;
107502
- rc = whereKeyStats(pParse, p, pRhs, 0, a);
107503
- if( rc==SQLITE_OK ){
107504
- WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
107505
- *pnRow = a[1];
107506
- }
107507
-whereEqualScanEst_cancel:
107508
- sqlite3ValueFree(pRhs);
108476
+ assert( pBuilder->nRecValid<nEq );
108477
+
108478
+ /* If values are not available for all fields of the index to the left
108479
+ ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
108480
+ if( pBuilder->nRecValid<(nEq-1) ){
108481
+ return SQLITE_NOTFOUND;
108482
+ }
108483
+
108484
+ /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
108485
+ ** below would return the same value. */
108486
+ if( nEq>p->nColumn ){
108487
+ *pnRow = 1;
108488
+ return SQLITE_OK;
108489
+ }
108490
+
108491
+ aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
108492
+ rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
108493
+ pBuilder->pRec = pRec;
108494
+ if( rc!=SQLITE_OK ) return rc;
108495
+ if( bOk==0 ) return SQLITE_NOTFOUND;
108496
+ pBuilder->nRecValid = nEq;
108497
+
108498
+ whereKeyStats(pParse, p, pRec, 0, a);
108499
+ WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
108500
+ *pnRow = a[1];
108501
+
107509108502
return rc;
107510108503
}
107511
-#endif /* defined(SQLITE_ENABLE_STAT3) */
108504
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107512108505
107513
-#ifdef SQLITE_ENABLE_STAT3
108506
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107514108507
/*
107515108508
** Estimate the number of rows that will be returned based on
107516108509
** an IN constraint where the right-hand side of the IN operator
107517108510
** is a list of values. Example:
107518108511
**
@@ -107527,33 +108520,38 @@
107527108520
** for a UTF conversion required for comparison. The error is stored
107528108521
** in the pParse structure.
107529108522
*/
107530108523
static int whereInScanEst(
107531108524
Parse *pParse, /* Parsing & code generating context */
107532
- Index *p, /* The index whose left-most column is pTerm */
108525
+ WhereLoopBuilder *pBuilder,
107533108526
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
107534108527
tRowcnt *pnRow /* Write the revised row estimate here */
107535108528
){
108529
+ Index *p = pBuilder->pNew->u.btree.pIndex;
108530
+ int nRecValid = pBuilder->nRecValid;
107536108531
int rc = SQLITE_OK; /* Subfunction return code */
107537108532
tRowcnt nEst; /* Number of rows for a single term */
107538108533
tRowcnt nRowEst = 0; /* New estimate of the number of rows */
107539108534
int i; /* Loop counter */
107540108535
107541108536
assert( p->aSample!=0 );
107542108537
for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
107543108538
nEst = p->aiRowEst[0];
107544
- rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
108539
+ rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
107545108540
nRowEst += nEst;
108541
+ pBuilder->nRecValid = nRecValid;
107546108542
}
108543
+
107547108544
if( rc==SQLITE_OK ){
107548108545
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
107549108546
*pnRow = nRowEst;
107550108547
WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
107551108548
}
108549
+ assert( pBuilder->nRecValid==nRecValid );
107552108550
return rc;
107553108551
}
107554
-#endif /* defined(SQLITE_ENABLE_STAT3) */
108552
+#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107555108553
107556108554
/*
107557108555
** Disable a term in the WHERE clause. Except, do not disable the term
107558108556
** if it controls a LEFT OUTER JOIN and it did not originate in the ON
107559108557
** or USING clause of that join.
@@ -107787,11 +108785,11 @@
107787108785
pParse->db->mallocFailed = 1;
107788108786
}
107789108787
107790108788
/* Evaluate the equality constraints
107791108789
*/
107792
- assert( zAff==0 || strlen(zAff)>=nEq );
108790
+ assert( zAff==0 || (int)strlen(zAff)>=nEq );
107793108791
for(j=0; j<nEq; j++){
107794108792
int r1;
107795108793
pTerm = pLoop->aLTerm[j];
107796108794
assert( pTerm!=0 );
107797108795
/* The following true for indices with redundant columns.
@@ -108956,19 +109954,21 @@
108956109954
assert( p->rSetup>=pTemplate->rSetup );
108957109955
108958109956
if( (p->prereq & pTemplate->prereq)==p->prereq
108959109957
&& p->rSetup<=pTemplate->rSetup
108960109958
&& p->rRun<=pTemplate->rRun
109959
+ && p->nOut<=pTemplate->nOut
108961109960
){
108962109961
/* This branch taken when p is equal or better than pTemplate in
108963
- ** all of (1) dependences (2) setup-cost, and (3) run-cost. */
109962
+ ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
109963
+ ** (4) number of output rows. */
108964109964
assert( p->rSetup==pTemplate->rSetup );
108965
- if( p->nLTerm<pTemplate->nLTerm
109965
+ if( p->prereq==pTemplate->prereq
109966
+ && p->nLTerm<pTemplate->nLTerm
108966109967
&& (p->wsFlags & WHERE_INDEXED)!=0
108967109968
&& (pTemplate->wsFlags & WHERE_INDEXED)!=0
108968109969
&& p->u.btree.pIndex==pTemplate->u.btree.pIndex
108969
- && p->prereq==pTemplate->prereq
108970109970
){
108971109971
/* Overwrite an existing WhereLoop with an similar one that uses
108972109972
** more terms of the index */
108973109973
pNext = p->pNextLoop;
108974109974
break;
@@ -108978,15 +109978,17 @@
108978109978
goto whereLoopInsert_noop;
108979109979
}
108980109980
}
108981109981
if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
108982109982
&& p->rRun>=pTemplate->rRun
109983
+ && p->nOut>=pTemplate->nOut
108983109984
&& ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */
108984109985
){
108985109986
/* Overwrite an existing WhereLoop with a better one: one that is
108986
- ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost
108987
- ** and is no worse in any of those categories. */
109987
+ ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
109988
+ ** or (4) number of output rows, and is no worse in any of those
109989
+ ** categories. */
108988109990
pNext = p->pNextLoop;
108989109991
break;
108990109992
}
108991109993
}
108992109994
@@ -109094,16 +110096,22 @@
109094110096
saved_nOut = pNew->nOut;
109095110097
pNew->rSetup = 0;
109096110098
rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
109097110099
for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
109098110100
int nIn = 0;
109099
- if( pTerm->prereqRight & pNew->maskSelf ) continue;
110101
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110102
+ int nRecValid = pBuilder->nRecValid;
110103
+#endif
109100110104
if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
109101110105
&& (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
109102110106
){
109103110107
continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
109104110108
}
110109
+ if( pTerm->prereqRight & pNew->maskSelf ) continue;
110110
+
110111
+ assert( pNew->nOut==saved_nOut );
110112
+
109105110113
pNew->wsFlags = saved_wsFlags;
109106110114
pNew->u.btree.nEq = saved_nEq;
109107110115
pNew->nLTerm = saved_nLTerm;
109108110116
if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
109109110117
pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109156,29 +110164,34 @@
109156110164
pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
109157110165
pNew->aLTerm[pNew->nLTerm-2] : 0;
109158110166
}
109159110167
if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
109160110168
/* Adjust nOut and rRun for STAT3 range values */
109161
- WhereCost rDiv;
109162
- whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
109163
- pBtm, pTop, &rDiv);
109164
- pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
109165
- }
109166
-#ifdef SQLITE_ENABLE_STAT3
109167
- if( pNew->u.btree.nEq==1 && pProbe->nSample
109168
- && OptimizationEnabled(db, SQLITE_Stat3) ){
110169
+ assert( pNew->nOut==saved_nOut );
110170
+ whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
110171
+ }
110172
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110173
+ if( nInMul==0
110174
+ && pProbe->nSample
110175
+ && pNew->u.btree.nEq<=pProbe->nSampleCol
110176
+ && OptimizationEnabled(db, SQLITE_Stat3)
110177
+ ){
110178
+ Expr *pExpr = pTerm->pExpr;
109169110179
tRowcnt nOut = 0;
109170110180
if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
109171110181
testcase( pTerm->eOperator & WO_EQ );
109172110182
testcase( pTerm->eOperator & WO_ISNULL );
109173
- rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
110183
+ rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
109174110184
}else if( (pTerm->eOperator & WO_IN)
109175
- && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){
109176
- rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
110185
+ && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110186
+ rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
109177110187
}
109178110188
assert( nOut==0 || rc==SQLITE_OK );
109179
- if( nOut ) pNew->nOut = whereCost(nOut);
110189
+ if( nOut ){
110190
+ nOut = whereCost(nOut);
110191
+ pNew->nOut = MIN(nOut, saved_nOut);
110192
+ }
109180110193
}
109181110194
#endif
109182110195
if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
109183110196
/* Each row involves a step of the index, then a binary search of
109184110197
** the main table */
@@ -109191,10 +110204,14 @@
109191110204
if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
109192110205
&& pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
109193110206
){
109194110207
whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
109195110208
}
110209
+ pNew->nOut = saved_nOut;
110210
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110211
+ pBuilder->nRecValid = nRecValid;
110212
+#endif
109196110213
}
109197110214
pNew->prereq = saved_prereq;
109198110215
pNew->u.btree.nEq = saved_nEq;
109199110216
pNew->wsFlags = saved_wsFlags;
109200110217
pNew->nOut = saved_nOut;
@@ -109420,11 +110437,17 @@
109420110437
}
109421110438
rc = whereLoopInsert(pBuilder, pNew);
109422110439
if( rc ) break;
109423110440
}
109424110441
}
110442
+
109425110443
rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
110444
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110445
+ sqlite3Stat4ProbeFree(pBuilder->pRec);
110446
+ pBuilder->nRecValid = 0;
110447
+ pBuilder->pRec = 0;
110448
+#endif
109426110449
109427110450
/* If there was an INDEXED BY clause, then only that one index is
109428110451
** considered. */
109429110452
if( pSrc->pIndex ) break;
109430110453
}
@@ -110666,11 +111689,11 @@
110666111689
if( pWInfo->nLevel>=2
110667111690
&& pResultSet!=0
110668111691
&& OptimizationEnabled(db, SQLITE_OmitNoopJoin)
110669111692
){
110670111693
Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
110671
- if( pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, pOrderBy);
111694
+ if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
110672111695
while( pWInfo->nLevel>=2 ){
110673111696
WhereTerm *pTerm, *pEnd;
110674111697
pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
110675111698
if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
110676111699
if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -116698,18 +117721,20 @@
116698117721
case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
116699117722
case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
116700117723
case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
116701117724
case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
116702117725
case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
117726
+ case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
116703117727
case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
116704117728
case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
116705117729
case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
116706117730
case SQLITE_FULL: zName = "SQLITE_FULL"; break;
116707117731
case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
116708117732
case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
116709117733
case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
116710117734
case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
117735
+ case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
116711117736
case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
116712117737
case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
116713117738
case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
116714117739
case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
116715117740
case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
116716117741
--- 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.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.0.1"
660 #define SQLITE_VERSION_NUMBER 3008000
661 #define SQLITE_SOURCE_ID "2013-08-29 13:47:05 c5857808c0707baa30994dd6aa3b9c93a74c0073"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1026,16 +1026,18 @@
1026 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1027 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1028 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1029 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1030 #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
 
1031 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1032 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1033 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1034 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1035 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1036 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
 
1037 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1038 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1039 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1040 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1041 #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
@@ -8368,10 +8370,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
@@ -10770,13 +10786,14 @@
10770 u16 nColumn; /* Number of columns in table used by this index */
10771 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10772 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10773 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10774 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10775 #ifdef SQLITE_ENABLE_STAT3
10776 int nSample; /* Number of elements in aSample[] */
10777 tRowcnt avgEq; /* Average nEq value for key values not in aSample */
 
10778 IndexSample *aSample; /* Samples of the left-most key */
10779 #endif
10780 };
10781
10782 /*
@@ -10783,20 +10800,15 @@
10783 ** Each sample stored in the sqlite_stat3 table is represented in memory
10784 ** using a structure of this type. See documentation at the top of the
10785 ** analyze.c source file for additional information.
10786 */
10787 struct IndexSample {
10788 union {
10789 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10790 double r; /* Value if eType is SQLITE_FLOAT */
10791 i64 i; /* Value if eType is SQLITE_INTEGER */
10792 } u;
10793 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10794 int nByte; /* Size in byte of text or blob. */
10795 tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10796 tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10797 tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10798 };
10799
10800 /*
10801 ** Each token coming out of the lexer is an instance of
10802 ** this structure. Tokens are also used as part of an expression.
@@ -12264,13 +12276,10 @@
12264 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12265 void(*)(void*));
12266 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12267 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12268 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12269 #ifdef SQLITE_ENABLE_STAT3
12270 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
12271 #endif
12272 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12273 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12274 #ifndef SQLITE_AMALGAMATION
12275 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12276 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12332,10 +12341,16 @@
12332 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
12333 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12334
12335 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12336 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
 
 
 
 
 
 
12337
12338 /*
12339 ** The interface to the LEMON-generated parser
12340 */
12341 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
@@ -12916,11 +12931,13 @@
12916 "ENABLE_OVERSIZE_CELL_CHECK",
12917 #endif
12918 #ifdef SQLITE_ENABLE_RTREE
12919 "ENABLE_RTREE",
12920 #endif
12921 #ifdef SQLITE_ENABLE_STAT3
 
 
12922 "ENABLE_STAT3",
12923 #endif
12924 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12925 "ENABLE_UNLOCK_NOTIFY",
12926 #endif
@@ -16075,11 +16092,11 @@
16075 struct MemBlockHdr *pHdr;
16076 if( !p ){
16077 return 0;
16078 }
16079 pHdr = sqlite3MemsysGetHeader(p);
16080 return pHdr->iSize;
16081 }
16082
16083 /*
16084 ** Initialize the memory allocation subsystem.
16085 */
@@ -16117,19 +16134,19 @@
16117 static void randomFill(char *pBuf, int nByte){
16118 unsigned int x, y, r;
16119 x = SQLITE_PTR_TO_INT(pBuf);
16120 y = nByte | 1;
16121 while( nByte >= 4 ){
16122 x = (x>>1) ^ (-(x&1) & 0xd0000001);
16123 y = y*1103515245 + 12345;
16124 r = x ^ y;
16125 *(int*)pBuf = r;
16126 pBuf += 4;
16127 nByte -= 4;
16128 }
16129 while( nByte-- > 0 ){
16130 x = (x>>1) ^ (-(x&1) & 0xd0000001);
16131 y = y*1103515245 + 12345;
16132 r = x ^ y;
16133 *(pBuf++) = r & 0xff;
16134 }
16135 }
@@ -16220,13 +16237,13 @@
16220 assert( mem.pLast==pHdr );
16221 mem.pLast = pHdr->pPrev;
16222 }
16223 z = (char*)pBt;
16224 z -= pHdr->nTitle;
16225 adjustStats(pHdr->iSize, -1);
16226 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16227 pHdr->iSize + sizeof(int) + pHdr->nTitle);
16228 free(z);
16229 sqlite3_mutex_leave(mem.mutex);
16230 }
16231
16232 /*
@@ -16244,13 +16261,13 @@
16244 assert( mem.disallow==0 );
16245 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
16246 pOldHdr = sqlite3MemsysGetHeader(pPrior);
16247 pNew = sqlite3MemMalloc(nByte);
16248 if( pNew ){
16249 memcpy(pNew, pPrior, nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize);
16250 if( nByte>pOldHdr->iSize ){
16251 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - pOldHdr->iSize);
16252 }
16253 sqlite3MemFree(pPrior);
16254 }
16255 return pNew;
16256 }
@@ -16361,11 +16378,11 @@
16361 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16362 struct MemBlockHdr *pHdr;
16363 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16364 void **pBt = (void**)pHdr;
16365 pBt -= pHdr->nBacktraceSlots;
16366 mem.xBacktrace(pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16367 }
16368 }
16369
16370 /*
16371 ** Open the file indicated and write a log of all unfreed memory
@@ -18483,11 +18500,11 @@
18483 GetVersionEx(&sInfo);
18484 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18485 }
18486 return osType==2;
18487 }
18488 #endif /* SQLITE_OS_WINCE */
18489 #endif
18490
18491 #ifdef SQLITE_DEBUG
18492 /*
18493 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18521,11 +18538,11 @@
18521 /* As winMutexInit() and winMutexEnd() are called as part
18522 ** of the sqlite3_initialize and sqlite3_shutdown()
18523 ** processing, the "interlocked" magic is probably not
18524 ** strictly necessary.
18525 */
18526 static long winMutex_lock = 0;
18527
18528 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18529
18530 static int winMutexInit(void){
18531 /* The first to increment to 1 does actual initialization */
@@ -21082,36 +21099,10 @@
21082 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21083 assert( m.z || db->mallocFailed );
21084 return m.z;
21085 }
21086
21087 /*
21088 ** Convert a UTF-8 string to the UTF-16 encoding specified by parameter
21089 ** enc. A pointer to the new string is returned, and the value of *pnOut
21090 ** is set to the length of the returned string in bytes. The call should
21091 ** arrange to call sqlite3DbFree() on the returned pointer when it is
21092 ** no longer required.
21093 **
21094 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
21095 ** flag set.
21096 */
21097 #ifdef SQLITE_ENABLE_STAT3
21098 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
21099 Mem m;
21100 memset(&m, 0, sizeof(m));
21101 m.db = db;
21102 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
21103 if( sqlite3VdbeMemTranslate(&m, enc) ){
21104 assert( db->mallocFailed );
21105 return 0;
21106 }
21107 assert( m.z==m.zMalloc );
21108 *pnOut = m.n;
21109 return m.z;
21110 }
21111 #endif
21112
21113 /*
21114 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21115 ** Return the number of bytes in the first nChar unicode characters
21116 ** in pZ. nChar must be non-negative.
21117 */
@@ -23064,15 +23055,17 @@
23064 void *lockingContext; /* Locking style specific state */
23065 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
23066 const char *zPath; /* Name of the file */
23067 unixShm *pShm; /* Shared memory segment information */
23068 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
 
23069 int nFetchOut; /* Number of outstanding xFetch refs */
23070 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23071 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23072 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23073 void *pMapRegion; /* Memory mapped region */
 
23074 #ifdef __QNXNTO__
23075 int sectorSize; /* Device sector size */
23076 int deviceCharacteristics; /* Precomputed device characteristics */
23077 #endif
23078 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -23503,10 +23496,11 @@
23503 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23504
23505 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23506 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23507
 
23508 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23509 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23510
23511 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23512 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23515,10 +23509,11 @@
23515 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23516 #else
23517 { "mremap", (sqlite3_syscall_ptr)0, 0 },
23518 #endif
23519 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
 
23520
23521 }; /* End of the overrideable system calls */
23522
23523 /*
23524 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23601,10 +23596,19 @@
23601 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
23602 }
23603 return 0;
23604 }
23605
 
 
 
 
 
 
 
 
 
23606 /*
23607 ** Invoke open(). Do so multiple times, until it either succeeds or
23608 ** fails for some reason other than EINTR.
23609 **
23610 ** If the file creation mode "m" is 0 then set it to the default for
@@ -23621,17 +23625,27 @@
23621 ** recover the hot journals.
23622 */
23623 static int robust_open(const char *z, int f, mode_t m){
23624 int fd;
23625 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23626 do{
23627 #if defined(O_CLOEXEC)
23628 fd = osOpen(z,f|O_CLOEXEC,m2);
23629 #else
23630 fd = osOpen(z,f,m2);
23631 #endif
23632 }while( fd<0 && errno==EINTR );
 
 
 
 
 
 
 
 
 
 
23633 if( fd>=0 ){
23634 if( m!=0 ){
23635 struct stat statbuf;
23636 if( osFstat(fd, &statbuf)==0
23637 && statbuf.st_size==0
@@ -24925,12 +24939,14 @@
24925 static int unixUnlock(sqlite3_file *id, int eFileLock){
24926 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
24927 return posixUnlock(id, eFileLock, 0);
24928 }
24929
 
24930 static int unixMapfile(unixFile *pFd, i64 nByte);
24931 static void unixUnmapfile(unixFile *pFd);
 
24932
24933 /*
24934 ** This function performs the parts of the "close file" operation
24935 ** common to all locking schemes. It closes the directory and file
24936 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24940,11 +24956,13 @@
24940 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24941 ** vxworksReleaseFileId() routine.
24942 */
24943 static int closeUnixFile(sqlite3_file *id){
24944 unixFile *pFile = (unixFile*)id;
 
24945 unixUnmapfile(pFile);
 
24946 if( pFile->h>=0 ){
24947 robust_close(pFile, pFile->h, __LINE__);
24948 pFile->h = -1;
24949 }
24950 #if OS_VXWORKS
@@ -26145,10 +26163,11 @@
26145 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26146 i64 newOffset;
26147 #endif
26148 TIMER_START;
26149 assert( cnt==(cnt&0x1ffff) );
 
26150 cnt &= 0x1ffff;
26151 do{
26152 #if defined(USE_PREAD)
26153 got = osPread(id->h, pBuf, cnt, offset);
26154 SimulateIOError( got = -1 );
@@ -26259,10 +26278,11 @@
26259 int *piErrno /* OUT: Error number if error occurs */
26260 ){
26261 int rc = 0; /* Value returned by system call */
26262
26263 assert( nBuf==(nBuf&0x1ffff) );
 
26264 nBuf &= 0x1ffff;
26265 TIMER_START;
26266
26267 #if defined(USE_PREAD)
26268 do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26644,17 +26664,19 @@
26644 if( pFile->inNormalWrite && nByte==0 ){
26645 pFile->transCntrChng = 1;
26646 }
26647 #endif
26648
 
26649 /* If the file was just truncated to a size smaller than the currently
26650 ** mapped region, reduce the effective mapping size as well. SQLite will
26651 ** use read() and write() to access data beyond this point from now on.
26652 */
26653 if( nByte<pFile->mmapSize ){
26654 pFile->mmapSize = nByte;
26655 }
 
26656
26657 return SQLITE_OK;
26658 }
26659 }
26660
@@ -26740,10 +26762,11 @@
26740 }
26741 #endif
26742 }
26743 }
26744
 
26745 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26746 int rc;
26747 if( pFile->szChunk<=0 ){
26748 if( robust_ftruncate(pFile->h, nByte) ){
26749 pFile->lastErrno = errno;
@@ -26752,10 +26775,11 @@
26752 }
26753
26754 rc = unixMapfile(pFile, nByte);
26755 return rc;
26756 }
 
26757
26758 return SQLITE_OK;
26759 }
26760
26761 /*
@@ -26820,10 +26844,11 @@
26820 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26821 *(char**)pArg = zTFile;
26822 }
26823 return SQLITE_OK;
26824 }
 
26825 case SQLITE_FCNTL_MMAP_SIZE: {
26826 i64 newLimit = *(i64*)pArg;
26827 int rc = SQLITE_OK;
26828 if( newLimit>sqlite3GlobalConfig.mxMmap ){
26829 newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26836,10 +26861,11 @@
26836 rc = unixMapfile(pFile, -1);
26837 }
26838 }
26839 return rc;
26840 }
 
26841 #ifdef SQLITE_DEBUG
26842 /* The pager calls this method to signal that it has done
26843 ** a rollback and that the database is therefore unchanged and
26844 ** it hence it is OK for the transaction change counter to be
26845 ** unchanged.
@@ -27646,26 +27672,24 @@
27646 # define unixShmLock 0
27647 # define unixShmBarrier 0
27648 # define unixShmUnmap 0
27649 #endif /* #ifndef SQLITE_OMIT_WAL */
27650
 
27651 /*
27652 ** If it is currently memory mapped, unmap file pFd.
27653 */
27654 static void unixUnmapfile(unixFile *pFd){
27655 assert( pFd->nFetchOut==0 );
27656 #if SQLITE_MAX_MMAP_SIZE>0
27657 if( pFd->pMapRegion ){
27658 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27659 pFd->pMapRegion = 0;
27660 pFd->mmapSize = 0;
27661 pFd->mmapSizeActual = 0;
27662 }
27663 #endif
27664 }
27665
27666 #if SQLITE_MAX_MMAP_SIZE>0
27667 /*
27668 ** Return the system page size.
27669 */
27670 static int unixGetPagesize(void){
27671 #if HAVE_MREMAP
@@ -27674,13 +27698,11 @@
27674 return getpagesize();
27675 #else
27676 return (int)sysconf(_SC_PAGESIZE);
27677 #endif
27678 }
27679 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
27680
27681 #if SQLITE_MAX_MMAP_SIZE>0
27682 /*
27683 ** Attempt to set the size of the memory mapping maintained by file
27684 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27685 **
27686 ** If successful, this function sets the following variables:
@@ -27761,11 +27783,10 @@
27761 pFd->mmapSizeMax = 0;
27762 }
27763 pFd->pMapRegion = (void *)pNew;
27764 pFd->mmapSize = pFd->mmapSizeActual = nNew;
27765 }
27766 #endif
27767
27768 /*
27769 ** Memory map or remap the file opened by file-descriptor pFd (if the file
27770 ** is already mapped, the existing mapping is replaced by the new). Or, if
27771 ** there already exists a mapping for this file, and there are still
@@ -27780,11 +27801,10 @@
27780 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
27781 ** recreated as a result of outstanding references) or an SQLite error
27782 ** code otherwise.
27783 */
27784 static int unixMapfile(unixFile *pFd, i64 nByte){
27785 #if SQLITE_MAX_MMAP_SIZE>0
27786 i64 nMap = nByte;
27787 int rc;
27788
27789 assert( nMap>=0 || pFd->nFetchOut==0 );
27790 if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27806,14 +27826,14 @@
27806 unixRemapfile(pFd, nMap);
27807 }else{
27808 unixUnmapfile(pFd);
27809 }
27810 }
27811 #endif
27812
27813 return SQLITE_OK;
27814 }
 
27815
27816 /*
27817 ** If possible, return a pointer to a mapping of file fd starting at offset
27818 ** iOff. The mapping must be valid for at least nAmt bytes.
27819 **
@@ -27858,10 +27878,11 @@
27858 */
27859 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27860 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27861 UNUSED_PARAMETER(iOff);
27862
 
27863 /* If p==0 (unmap the entire file) then there must be no outstanding
27864 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27865 ** then there must be at least one outstanding. */
27866 assert( (p==0)==(pFd->nFetchOut==0) );
27867
@@ -27873,10 +27894,11 @@
27873 }else{
27874 unixUnmapfile(pFd);
27875 }
27876
27877 assert( pFd->nFetchOut>=0 );
 
27878 return SQLITE_OK;
27879 }
27880
27881 /*
27882 ** Here ends the implementation of all sqlite3_file methods.
@@ -28204,11 +28226,13 @@
28204 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28205 pNew->h = h;
28206 pNew->pVfs = pVfs;
28207 pNew->zPath = zFilename;
28208 pNew->ctrlFlags = (u8)ctrlFlags;
 
28209 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
 
28210 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28211 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28212 pNew->ctrlFlags |= UNIXFILE_PSOW;
28213 }
28214 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -28360,10 +28384,11 @@
28360 ** If no suitable temporary file directory can be found, return NULL.
28361 */
28362 static const char *unixTempFileDir(void){
28363 static const char *azDirs[] = {
28364 0,
 
28365 0,
28366 "/var/tmp",
28367 "/usr/tmp",
28368 "/tmp",
28369 0 /* List terminator */
@@ -28371,11 +28396,12 @@
28371 unsigned int i;
28372 struct stat buf;
28373 const char *zDir = 0;
28374
28375 azDirs[0] = sqlite3_temp_directory;
28376 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
 
28377 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
28378 if( zDir==0 ) continue;
28379 if( osStat(zDir, &buf) ) continue;
28380 if( !S_ISDIR(buf.st_mode) ) continue;
28381 if( osAccess(zDir, 07) ) continue;
@@ -30484,11 +30510,11 @@
30484 */
30485 #if SQLITE_OS_WIN /* This file is used for Windows only */
30486
30487 #ifdef __CYGWIN__
30488 # include <sys/cygwin.h>
30489 /* # include <errno.h> */
30490 #endif
30491
30492 /*
30493 ** Include code that is common to all os_*.c files
30494 */
@@ -30705,30 +30731,99 @@
30705 /*
30706 ** Compiling and using WAL mode requires several APIs that are only
30707 ** available in Windows platforms based on the NT kernel.
30708 */
30709 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30710 # error "WAL mode requires support from the Windows NT kernel, compile\
30711 with SQLITE_OMIT_WAL."
30712 #endif
30713
30714 /*
30715 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
30716 ** based on the sub-platform)?
30717 */
30718 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
30719 # define SQLITE_WIN32_HAS_ANSI
30720 #endif
30721
30722 /*
30723 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
30724 ** based on the sub-platform)?
30725 */
30726 #if SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT
 
30727 # define SQLITE_WIN32_HAS_WIDE
30728 #endif
30729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30730 /*
30731 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
30732 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
30733 ** are not present in the header file)?
30734 */
@@ -30776,11 +30871,11 @@
30776 #ifndef FILE_ATTRIBUTE_MASK
30777 # define FILE_ATTRIBUTE_MASK (0x0003FFF7)
30778 #endif
30779
30780 #ifndef SQLITE_OMIT_WAL
30781 /* Forward references */
30782 typedef struct winShm winShm; /* A connection to shared-memory */
30783 typedef struct winShmNode winShmNode; /* A region of shared-memory */
30784 #endif
30785
30786 /*
@@ -31731,16 +31826,16 @@
31731 ** API as long as we don't call it when running Win95/98/ME. A call to
31732 ** this routine is used to determine if the host is Win95/98/ME or
31733 ** WinNT/2K/XP so that we will know whether or not we can safely call
31734 ** the LockFileEx() API.
31735 */
31736 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31737 # define isNT() (1)
31738 #elif !defined(SQLITE_WIN32_HAS_WIDE)
31739 # define isNT() (0)
31740 #else
31741 static int isNT(void){
31742 if( sqlite3_os_type==0 ){
31743 OSVERSIONINFOA sInfo;
31744 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31745 osGetVersionExA(&sInfo);
31746 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31947,11 +32042,11 @@
31947 /*
31948 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
31949 **
31950 ** Space to hold the returned string is obtained from malloc.
31951 */
31952 static LPWSTR utf8ToUnicode(const char *zFilename){
31953 int nChar;
31954 LPWSTR zWideFilename;
31955
31956 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
31957 if( nChar==0 ){
@@ -31972,11 +32067,11 @@
31972
31973 /*
31974 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
31975 ** obtained from sqlite3_malloc().
31976 */
31977 static char *unicodeToUtf8(LPCWSTR zWideFilename){
31978 int nByte;
31979 char *zFilename;
31980
31981 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
31982 if( nByte == 0 ){
@@ -32000,11 +32095,11 @@
32000 ** current codepage settings for file apis.
32001 **
32002 ** Space to hold the returned string is obtained
32003 ** from sqlite3_malloc.
32004 */
32005 static LPWSTR mbcsToUnicode(const char *zFilename){
32006 int nByte;
32007 LPWSTR zMbcsFilename;
32008 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32009
32010 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32030,11 +32125,11 @@
32030 ** user's ANSI codepage.
32031 **
32032 ** Space to hold the returned string is obtained from
32033 ** sqlite3_malloc().
32034 */
32035 static char *unicodeToMbcs(LPCWSTR zWideFilename){
32036 int nByte;
32037 char *zFilename;
32038 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32039
32040 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32060,15 +32155,15 @@
32060 */
32061 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32062 char *zFilenameUtf8;
32063 LPWSTR zTmpWide;
32064
32065 zTmpWide = mbcsToUnicode(zFilename);
32066 if( zTmpWide==0 ){
32067 return 0;
32068 }
32069 zFilenameUtf8 = unicodeToUtf8(zTmpWide);
32070 sqlite3_free(zTmpWide);
32071 return zFilenameUtf8;
32072 }
32073
32074 /*
@@ -32077,15 +32172,15 @@
32077 */
32078 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32079 char *zFilenameMbcs;
32080 LPWSTR zTmpWide;
32081
32082 zTmpWide = utf8ToUnicode(zFilename);
32083 if( zTmpWide==0 ){
32084 return 0;
32085 }
32086 zFilenameMbcs = unicodeToMbcs(zTmpWide);
32087 sqlite3_free(zTmpWide);
32088 return zFilenameMbcs;
32089 }
32090
32091 /*
@@ -32111,11 +32206,11 @@
32111 );
32112 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
32113 if( ppDirectory ){
32114 char *zValueUtf8 = 0;
32115 if( zValue && zValue[0] ){
32116 zValueUtf8 = unicodeToUtf8(zValue);
32117 if ( zValueUtf8==0 ){
32118 return SQLITE_NOMEM;
32119 }
32120 }
32121 sqlite3_free(*ppDirectory);
@@ -32124,32 +32219,32 @@
32124 }
32125 return SQLITE_ERROR;
32126 }
32127
32128 /*
32129 ** The return value of getLastErrorMsg
32130 ** is zero if the error message fits in the buffer, or non-zero
32131 ** otherwise (if the message was truncated).
32132 */
32133 static int getLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32134 /* FormatMessage returns 0 on failure. Otherwise it
32135 ** returns the number of TCHARs written to the output
32136 ** buffer, excluding the terminating null char.
32137 */
32138 DWORD dwLen = 0;
32139 char *zOut = 0;
32140
32141 if( isNT() ){
32142 #if SQLITE_OS_WINRT
32143 WCHAR zTempWide[MAX_PATH+1]; /* NOTE: Somewhat arbitrary. */
32144 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
32145 FORMAT_MESSAGE_IGNORE_INSERTS,
32146 NULL,
32147 lastErrno,
32148 0,
32149 zTempWide,
32150 MAX_PATH,
32151 0);
32152 #else
32153 LPWSTR zTempWide = NULL;
32154 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
32155 FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32162,11 +32257,11 @@
32162 0);
32163 #endif
32164 if( dwLen > 0 ){
32165 /* allocate a buffer and convert to UTF8 */
32166 sqlite3BeginBenignMalloc();
32167 zOut = unicodeToUtf8(zTempWide);
32168 sqlite3EndBenignMalloc();
32169 #if !SQLITE_OS_WINRT
32170 /* free the system buffer allocated by FormatMessage */
32171 osLocalFree(zTempWide);
32172 #endif
@@ -32230,11 +32325,11 @@
32230 ){
32231 char zMsg[500]; /* Human readable error text */
32232 int i; /* Loop counter */
32233
32234 zMsg[0] = 0;
32235 getLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32236 assert( errcode!=SQLITE_OK );
32237 if( zPath==0 ) zPath = "";
32238 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32239 zMsg[i] = 0;
32240 sqlite3_log(errcode,
@@ -32255,30 +32350,30 @@
32255 # define SQLITE_WIN32_IOERR_RETRY 10
32256 #endif
32257 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
32258 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
32259 #endif
32260 static int win32IoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32261 static int win32IoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32262
32263 /*
32264 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
32265 ** to see if it should be retried. Return TRUE to retry. Return FALSE
32266 ** to give up with an error.
32267 */
32268 static int retryIoerr(int *pnRetry, DWORD *pError){
32269 DWORD e = osGetLastError();
32270 if( *pnRetry>=win32IoerrRetry ){
32271 if( pError ){
32272 *pError = e;
32273 }
32274 return 0;
32275 }
32276 if( e==ERROR_ACCESS_DENIED ||
32277 e==ERROR_LOCK_VIOLATION ||
32278 e==ERROR_SHARING_VIOLATION ){
32279 sqlite3_win32_sleep(win32IoerrRetryDelay*(1+*pnRetry));
32280 ++*pnRetry;
32281 return 1;
32282 }
32283 if( pError ){
32284 *pError = e;
@@ -32287,15 +32382,15 @@
32287 }
32288
32289 /*
32290 ** Log a I/O error retry episode.
32291 */
32292 static void logIoerr(int nRetry){
32293 if( nRetry ){
32294 sqlite3_log(SQLITE_IOERR,
32295 "delayed %dms for lock/sharing conflict",
32296 win32IoerrRetryDelay*nRetry*(nRetry+1)/2
32297 );
32298 }
32299 }
32300
32301 #if SQLITE_OS_WINCE
@@ -32356,11 +32451,11 @@
32356 LPWSTR zName;
32357 DWORD lastErrno;
32358 BOOL bLogged = FALSE;
32359 BOOL bInit = TRUE;
32360
32361 zName = utf8ToUnicode(zFilename);
32362 if( zName==0 ){
32363 /* out of memory */
32364 return SQLITE_IOERR_NOMEM;
32365 }
32366
@@ -32376,14 +32471,13 @@
32376
32377 /* Create/open the named mutex */
32378 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
32379 if (!pFile->hMutex){
32380 pFile->lastErrno = osGetLastError();
32381 winLogError(SQLITE_IOERR, pFile->lastErrno,
32382 "winceCreateLock1", zFilename);
32383 sqlite3_free(zName);
32384 return SQLITE_IOERR;
 
32385 }
32386
32387 /* Acquire the mutex before continuing */
32388 winceMutexAcquire(pFile->hMutex);
32389
@@ -32629,11 +32723,11 @@
32629 ** API LockFile.
32630 */
32631 return winceLockFile(phFile, offsetLow, offsetHigh,
32632 numBytesLow, numBytesHigh);
32633 #else
32634 if( isNT() ){
32635 OVERLAPPED ovlp;
32636 memset(&ovlp, 0, sizeof(OVERLAPPED));
32637 ovlp.Offset = offsetLow;
32638 ovlp.OffsetHigh = offsetHigh;
32639 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32660,11 +32754,11 @@
32660 ** API UnlockFile.
32661 */
32662 return winceUnlockFile(phFile, offsetLow, offsetHigh,
32663 numBytesLow, numBytesHigh);
32664 #else
32665 if( isNT() ){
32666 OVERLAPPED ovlp;
32667 memset(&ovlp, 0, sizeof(OVERLAPPED));
32668 ovlp.Offset = offsetLow;
32669 ovlp.OffsetHigh = offsetHigh;
32670 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32690,11 +32784,11 @@
32690 /*
32691 ** Move the current position of the file handle passed as the first
32692 ** argument to offset iOffset within the file. If successful, return 0.
32693 ** Otherwise, set pFile->lastErrno and return non-zero.
32694 */
32695 static int seekWinFile(winFile *pFile, sqlite3_int64 iOffset){
32696 #if !SQLITE_OS_WINRT
32697 LONG upperBits; /* Most sig. 32 bits of new offset */
32698 LONG lowerBits; /* Least sig. 32 bits of new offset */
32699 DWORD dwRet; /* Value returned by SetFilePointer() */
32700 DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32715,11 +32809,11 @@
32715
32716 if( (dwRet==INVALID_SET_FILE_POINTER
32717 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
32718 pFile->lastErrno = lastErrno;
32719 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32720 "seekWinFile", pFile->zPath);
32721 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32722 return 1;
32723 }
32724
32725 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32736,11 +32830,11 @@
32736 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
32737
32738 if(!bRet){
32739 pFile->lastErrno = osGetLastError();
32740 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32741 "seekWinFile", pFile->zPath);
32742 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32743 return 1;
32744 }
32745
32746 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32747,11 +32841,12 @@
32747 return 0;
32748 #endif
32749 }
32750
32751 #if SQLITE_MAX_MMAP_SIZE>0
32752 /* Forward references to VFS methods */
 
32753 static int winUnmapfile(winFile*);
32754 #endif
32755
32756 /*
32757 ** Close a file.
@@ -32851,11 +32946,11 @@
32851 }
32852 }
32853 #endif
32854
32855 #if SQLITE_OS_WINCE
32856 if( seekWinFile(pFile, offset) ){
32857 OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
32858 return SQLITE_FULL;
32859 }
32860 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32861 #else
@@ -32864,17 +32959,17 @@
32864 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
32865 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
32866 osGetLastError()!=ERROR_HANDLE_EOF ){
32867 #endif
32868 DWORD lastErrno;
32869 if( retryIoerr(&nRetry, &lastErrno) ) continue;
32870 pFile->lastErrno = lastErrno;
32871 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
32872 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
32873 "winRead", pFile->zPath);
32874 }
32875 logIoerr(nRetry);
32876 if( nRead<(DWORD)amt ){
32877 /* Unread parts of the buffer must be zero-filled */
32878 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32879 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
32880 return SQLITE_IOERR_SHORT_READ;
@@ -32923,11 +33018,11 @@
32923 }
32924 }
32925 #endif
32926
32927 #if SQLITE_OS_WINCE
32928 rc = seekWinFile(pFile, offset);
32929 if( rc==0 ){
32930 #else
32931 {
32932 #endif
32933 #if !SQLITE_OS_WINCE
@@ -32948,11 +33043,11 @@
32948 #if SQLITE_OS_WINCE
32949 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
32950 #else
32951 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
32952 #endif
32953 if( retryIoerr(&nRetry, &lastErrno) ) continue;
32954 break;
32955 }
32956 assert( nWrite==0 || nWrite<=(DWORD)nRem );
32957 if( nWrite==0 || nWrite>(DWORD)nRem ){
32958 lastErrno = osGetLastError();
@@ -32974,17 +33069,18 @@
32974
32975 if( rc ){
32976 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
32977 || ( pFile->lastErrno==ERROR_DISK_FULL )){
32978 OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
32979 return SQLITE_FULL;
 
32980 }
32981 OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
32982 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
32983 "winWrite", pFile->zPath);
32984 }else{
32985 logIoerr(nRetry);
32986 }
32987 OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
32988 return SQLITE_OK;
32989 }
32990
@@ -33009,11 +33105,11 @@
33009 if( pFile->szChunk>0 ){
33010 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33011 }
33012
33013 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33014 if( seekWinFile(pFile, nByte) ){
33015 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33016 "winTruncate1", pFile->zPath);
33017 }else if( 0==osSetEndOfFile(pFile->h) &&
33018 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33019 pFile->lastErrno = lastErrno;
@@ -33090,10 +33186,11 @@
33090
33091 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33092 ** no-op
33093 */
33094 #ifdef SQLITE_NO_SYNC
 
33095 return SQLITE_OK;
33096 #else
33097 rc = osFlushFileBuffers(pFile->h);
33098 SimulateIOError( rc=FALSE );
33099 if( rc ){
@@ -33101,11 +33198,11 @@
33101 return SQLITE_OK;
33102 }else{
33103 pFile->lastErrno = osGetLastError();
33104 OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
33105 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33106 "winSync", pFile->zPath);
33107 }
33108 #endif
33109 }
33110
33111 /*
@@ -33142,11 +33239,11 @@
33142 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
33143 if( (lowerBits == INVALID_FILE_SIZE)
33144 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
33145 pFile->lastErrno = lastErrno;
33146 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33147 "winFileSize", pFile->zPath);
33148 }
33149 }
33150 #endif
33151 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
33152 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
@@ -33187,14 +33284,14 @@
33187 /*
33188 ** Acquire a reader lock.
33189 ** Different API routines are called depending on whether or not this
33190 ** is Win9x or WinNT.
33191 */
33192 static int getReadLock(winFile *pFile){
33193 int res;
33194 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33195 if( isNT() ){
33196 #if SQLITE_OS_WINCE
33197 /*
33198 ** NOTE: Windows CE is handled differently here due its lack of the Win32
33199 ** API LockFileEx.
33200 */
@@ -33222,15 +33319,15 @@
33222 }
33223
33224 /*
33225 ** Undo a readlock
33226 */
33227 static int unlockReadLock(winFile *pFile){
33228 int res;
33229 DWORD lastErrno;
33230 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33231 if( isNT() ){
33232 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33233 }
33234 #ifdef SQLITE_WIN32_HAS_ANSI
33235 else{
33236 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33237,11 +33334,11 @@
33237 }
33238 #endif
33239 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33240 pFile->lastErrno = lastErrno;
33241 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33242 "unlockReadLock", pFile->zPath);
33243 }
33244 OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
33245 return res;
33246 }
33247
@@ -33328,11 +33425,11 @@
33328
33329 /* Acquire a shared lock
33330 */
33331 if( locktype==SHARED_LOCK && res ){
33332 assert( pFile->locktype==NO_LOCK );
33333 res = getReadLock(pFile);
33334 if( res ){
33335 newLocktype = SHARED_LOCK;
33336 }else{
33337 lastErrno = osGetLastError();
33338 }
@@ -33359,18 +33456,18 @@
33359
33360 /* Acquire an EXCLUSIVE lock
33361 */
33362 if( locktype==EXCLUSIVE_LOCK && res ){
33363 assert( pFile->locktype>=SHARED_LOCK );
33364 res = unlockReadLock(pFile);
33365 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
33366 SHARED_SIZE, 0);
33367 if( res ){
33368 newLocktype = EXCLUSIVE_LOCK;
33369 }else{
33370 lastErrno = osGetLastError();
33371 getReadLock(pFile);
33372 }
33373 }
33374
33375 /* If we are holding a PENDING lock that ought to be released, then
33376 ** release it now.
@@ -33383,14 +33480,14 @@
33383 ** return the appropriate result code.
33384 */
33385 if( res ){
33386 rc = SQLITE_OK;
33387 }else{
 
 
33388 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
33389 pFile->h, locktype, newLocktype));
33390 pFile->lastErrno = lastErrno;
33391 rc = SQLITE_BUSY;
33392 }
33393 pFile->locktype = (u8)newLocktype;
33394 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
33395 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
33396 return rc;
@@ -33446,22 +33543,22 @@
33446 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
33447 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
33448 type = pFile->locktype;
33449 if( type>=EXCLUSIVE_LOCK ){
33450 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33451 if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
33452 /* This should never happen. We should always be able to
33453 ** reacquire the read lock */
33454 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
33455 "winUnlock", pFile->zPath);
33456 }
33457 }
33458 if( type>=RESERVED_LOCK ){
33459 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
33460 }
33461 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33462 unlockReadLock(pFile);
33463 }
33464 if( type>=PENDING_LOCK ){
33465 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
33466 }
33467 pFile->locktype = (u8)locktype;
@@ -33484,15 +33581,14 @@
33484 }else{
33485 pFile->ctrlFlags |= mask;
33486 }
33487 }
33488
33489 /* Forward declaration */
33490 static int getTempname(int nBuf, char *zBuf);
33491 #if SQLITE_MAX_MMAP_SIZE>0
33492 static int winMapfile(winFile*, sqlite3_int64);
33493 #endif
33494
33495 /*
33496 ** Control and query of the open file handle.
33497 */
33498 static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33548,30 +33644,30 @@
33548 return SQLITE_OK;
33549 }
33550 case SQLITE_FCNTL_WIN32_AV_RETRY: {
33551 int *a = (int*)pArg;
33552 if( a[0]>0 ){
33553 win32IoerrRetry = a[0];
33554 }else{
33555 a[0] = win32IoerrRetry;
33556 }
33557 if( a[1]>0 ){
33558 win32IoerrRetryDelay = a[1];
33559 }else{
33560 a[1] = win32IoerrRetryDelay;
33561 }
33562 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33563 return SQLITE_OK;
33564 }
33565 case SQLITE_FCNTL_TEMPFILENAME: {
33566 char *zTFile = sqlite3MallocZero( pFile->pVfs->mxPathname );
33567 if( zTFile ){
33568 getTempname(pFile->pVfs->mxPathname, zTFile);
33569 *(char**)pArg = zTFile;
33570 }
33571 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33572 return SQLITE_OK;
33573 }
33574 #if SQLITE_MAX_MMAP_SIZE>0
33575 case SQLITE_FCNTL_MMAP_SIZE: {
33576 i64 newLimit = *(i64*)pArg;
33577 int rc = SQLITE_OK;
@@ -33584,11 +33680,11 @@
33584 if( pFile->mmapSize>0 ){
33585 (void)winUnmapfile(pFile);
33586 rc = winMapfile(pFile, -1);
33587 }
33588 }
33589 OSTRACE(("FCNTL file=%p, rc=%d\n", pFile->h, rc));
33590 return rc;
33591 }
33592 #endif
33593 }
33594 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
@@ -33900,11 +33996,11 @@
33900 */
33901 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
33902 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
33903 if( rc!=SQLITE_OK ){
33904 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
33905 "winOpenShm", pDbFd->zPath);
33906 }
33907 }
33908 if( rc==SQLITE_OK ){
33909 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
33910 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -34160,11 +34256,11 @@
34160 ** large enough to contain the requested region).
34161 */
34162 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
34163 if( rc!=SQLITE_OK ){
34164 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34165 "winShmMap1", pDbFd->zPath);
34166 goto shmpage_out;
34167 }
34168
34169 if( sz<nByte ){
34170 /* The requested memory region does not exist. If isWrite is set to
@@ -34175,11 +34271,11 @@
34175 */
34176 if( !isWrite ) goto shmpage_out;
34177 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
34178 if( rc!=SQLITE_OK ){
34179 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34180 "winShmMap2", pDbFd->zPath);
34181 goto shmpage_out;
34182 }
34183 }
34184
34185 /* Map the requested memory region into this processes address space. */
@@ -34229,11 +34325,11 @@
34229 szRegion, pMap ? "ok" : "failed"));
34230 }
34231 if( !pMap ){
34232 pShmNode->lastErrno = osGetLastError();
34233 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
34234 "winShmMap3", pDbFd->zPath);
34235 if( hMap ) osCloseHandle(hMap);
34236 goto shmpage_out;
34237 }
34238
34239 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
@@ -34277,11 +34373,11 @@
34277 pFile->lastErrno = osGetLastError();
34278 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
34279 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
34280 pFile->pMapRegion));
34281 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34282 "winUnmap1", pFile->zPath);
34283 }
34284 pFile->pMapRegion = 0;
34285 pFile->mmapSize = 0;
34286 pFile->mmapSizeActual = 0;
34287 }
@@ -34289,11 +34385,11 @@
34289 if( !osCloseHandle(pFile->hMap) ){
34290 pFile->lastErrno = osGetLastError();
34291 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
34292 osGetCurrentProcessId(), pFile, pFile->hMap));
34293 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34294 "winUnmap2", pFile->zPath);
34295 }
34296 pFile->hMap = NULL;
34297 }
34298 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
34299 osGetCurrentProcessId(), pFile));
@@ -34364,14 +34460,14 @@
34364 (DWORD)(nMap & 0xffffffff), NULL);
34365 #endif
34366 if( pFd->hMap==NULL ){
34367 pFd->lastErrno = osGetLastError();
34368 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34369 "winMapfile", pFd->zPath);
34370 /* Log the error, but continue normal operation using xRead/xWrite */
34371 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
34372 osGetCurrentProcessId(), pFd));
34373 return SQLITE_OK;
34374 }
34375 assert( (nMap % winSysInfo.dwPageSize)==0 );
34376 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
34377 #if SQLITE_OS_WINRT
@@ -34381,14 +34477,15 @@
34381 #endif
34382 if( pNew==NULL ){
34383 osCloseHandle(pFd->hMap);
34384 pFd->hMap = NULL;
34385 pFd->lastErrno = osGetLastError();
34386 winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34387 "winMapfile", pFd->zPath);
34388 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=SQLITE_IOERR_MMAP\n",
34389 osGetCurrentProcessId(), pFd));
 
34390 return SQLITE_OK;
34391 }
34392 pFd->pMapRegion = pNew;
34393 pFd->mmapSize = nMap;
34394 pFd->mmapSizeActual = nMap;
@@ -34522,21 +34619,40 @@
34522 **************************** sqlite3_vfs methods ****************************
34523 **
34524 ** This division contains the implementation of methods on the
34525 ** sqlite3_vfs object.
34526 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34527
34528 /*
34529 ** Convert a UTF-8 filename into whatever form the underlying
34530 ** operating system wants filenames in. Space to hold the result
34531 ** is obtained from malloc and must be freed by the calling
34532 ** function.
34533 */
34534 static void *convertUtf8Filename(const char *zFilename){
34535 void *zConverted = 0;
34536 if( isNT() ){
34537 zConverted = utf8ToUnicode(zFilename);
34538 }
34539 #ifdef SQLITE_WIN32_HAS_ANSI
34540 else{
34541 zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
34542 }
@@ -34544,117 +34660,220 @@
34544 /* caller will handle out of memory */
34545 return zConverted;
34546 }
34547
34548 /*
34549 ** Maximum pathname length (in bytes) for windows. The MAX_PATH macro is
34550 ** in characters, so we allocate 3 bytes per character assuming worst-case
34551 ** 3-bytes-per-character UTF8.
34552 */
34553 #ifndef SQLITE_WIN32_MAX_PATH
34554 # define SQLITE_WIN32_MAX_PATH (MAX_PATH*3)
34555 #endif
 
 
 
 
34556
34557 /*
34558 ** Create a temporary file name in zBuf. zBuf must be big enough to
34559 ** hold at pVfs->mxPathname characters.
34560 */
34561 static int getTempname(int nBuf, char *zBuf){
34562 static char zChars[] =
34563 "abcdefghijklmnopqrstuvwxyz"
34564 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34565 "0123456789";
34566 size_t i, j;
34567 int nTempPath;
34568 char zTempPath[SQLITE_WIN32_MAX_PATH+2];
34569
34570 /* It's odd to simulate an io-error here, but really this is just
34571 ** using the io-error infrastructure to test that SQLite handles this
34572 ** function failing.
34573 */
34574 SimulateIOError( return SQLITE_IOERR );
34575
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34576 if( sqlite3_temp_directory ){
34577 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s",
34578 sqlite3_temp_directory);
 
34579 }
34580 #if !SQLITE_OS_WINRT
34581 else if( isNT() ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34582 char *zMulti;
34583 WCHAR zWidePath[MAX_PATH];
34584 if( osGetTempPathW(MAX_PATH-30, zWidePath)==0 ){
 
 
 
 
 
 
 
34585 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34586 return SQLITE_IOERR_GETTEMPPATH;
 
34587 }
34588 zMulti = unicodeToUtf8(zWidePath);
34589 if( zMulti ){
34590 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zMulti);
34591 sqlite3_free(zMulti);
 
34592 }else{
 
 
34593 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34594 return SQLITE_IOERR_NOMEM;
34595 }
34596 }
34597 #ifdef SQLITE_WIN32_HAS_ANSI
34598 else{
34599 char *zUtf8;
34600 char zMbcsPath[SQLITE_WIN32_MAX_PATH];
34601 if( osGetTempPathA(SQLITE_WIN32_MAX_PATH-30, zMbcsPath)==0 ){
 
 
 
 
 
 
34602 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34603 return SQLITE_IOERR_GETTEMPPATH;
 
34604 }
34605 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34606 if( zUtf8 ){
34607 sqlite3_snprintf(SQLITE_WIN32_MAX_PATH-30, zTempPath, "%s", zUtf8);
34608 sqlite3_free(zUtf8);
34609 }else{
 
34610 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34611 return SQLITE_IOERR_NOMEM;
34612 }
34613 }
34614 #else
34615 else{
34616 /*
34617 ** Compiled without ANSI support and the current operating system
34618 ** is not Windows NT; therefore, just zero the temporary buffer.
34619 */
34620 memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34621 }
34622 #endif /* SQLITE_WIN32_HAS_ANSI */
34623 #else
34624 else{
34625 /*
34626 ** Compiled for WinRT and the sqlite3_temp_directory is not set;
34627 ** therefore, just zero the temporary buffer.
34628 */
34629 memset(zTempPath, 0, SQLITE_WIN32_MAX_PATH+2);
34630 }
34631 #endif /* !SQLITE_OS_WINRT */
34632
34633 /* Check that the output buffer is large enough for the temporary file
34634 ** name. If it is not, return SQLITE_ERROR.
34635 */
34636 nTempPath = sqlite3Strlen30(zTempPath);
34637
34638 if( (nTempPath + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
 
34639 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
34640 return SQLITE_ERROR;
34641 }
34642
34643 for(i=nTempPath; i>0 && zTempPath[i-1]=='\\'; i--){}
34644 zTempPath[i] = 0;
34645
34646 sqlite3_snprintf(nBuf-18, zBuf, (nTempPath > 0) ?
34647 "%s\\"SQLITE_TEMP_FILE_PREFIX : SQLITE_TEMP_FILE_PREFIX,
34648 zTempPath);
34649 j = sqlite3Strlen30(zBuf);
34650 sqlite3_randomness(15, &zBuf[j]);
34651 for(i=0; i<15; i++, j++){
34652 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34653 }
34654 zBuf[j] = 0;
34655 zBuf[j+1] = 0;
 
34656
34657 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
34658 return SQLITE_OK;
34659 }
34660
@@ -34666,17 +34885,17 @@
34666 static int winIsDir(const void *zConverted){
34667 DWORD attr;
34668 int rc = 0;
34669 DWORD lastErrno;
34670
34671 if( isNT() ){
34672 int cnt = 0;
34673 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34674 memset(&sAttrData, 0, sizeof(sAttrData));
34675 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
34676 GetFileExInfoStandard,
34677 &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
34678 if( !rc ){
34679 return 0; /* Invalid name? */
34680 }
34681 attr = sAttrData.dwFileAttributes;
34682 #if SQLITE_OS_WINCE==0
@@ -34689,11 +34908,11 @@
34689
34690 /*
34691 ** Open a file.
34692 */
34693 static int winOpen(
34694 sqlite3_vfs *pVfs, /* Not used */
34695 const char *zName, /* Name of the file (UTF-8) */
34696 sqlite3_file *id, /* Write the SQLite file handle here */
34697 int flags, /* Open mode flags */
34698 int *pOutFlags /* Status return flags */
34699 ){
@@ -34712,11 +34931,11 @@
34712 int cnt = 0;
34713
34714 /* If argument zPath is a NULL pointer, this function is required to open
34715 ** a temporary file. Use this buffer to store the file name in.
34716 */
34717 char zTmpname[SQLITE_WIN32_MAX_PATH+2]; /* Buffer used to create temp filename */
34718
34719 int rc = SQLITE_OK; /* Function Return Code */
34720 #if !defined(NDEBUG) || SQLITE_OS_WINCE
34721 int eType = flags&0xFFFFFF00; /* Type of file to open */
34722 #endif
@@ -34767,22 +34986,22 @@
34767 assert( pFile!=0 );
34768 memset(pFile, 0, sizeof(winFile));
34769 pFile->h = INVALID_HANDLE_VALUE;
34770
34771 #if SQLITE_OS_WINRT
34772 if( !sqlite3_temp_directory ){
34773 sqlite3_log(SQLITE_ERROR,
34774 "sqlite3_temp_directory variable should be set for WinRT");
34775 }
34776 #endif
34777
34778 /* If the second argument to this function is NULL, generate a
34779 ** temporary file name to use
34780 */
34781 if( !zUtf8Name ){
34782 assert(isDelete && !isOpenJournal);
34783 rc = getTempname(SQLITE_WIN32_MAX_PATH+2, zTmpname);
34784 if( rc!=SQLITE_OK ){
34785 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
34786 return rc;
34787 }
34788 zUtf8Name = zTmpname;
@@ -34791,21 +35010,23 @@
34791 /* Database filenames are double-zero terminated if they are not
34792 ** URIs with parameters. Hence, they can always be passed into
34793 ** sqlite3_uri_parameter().
34794 */
34795 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
34796 zUtf8Name[strlen(zUtf8Name)+1]==0 );
34797
34798 /* Convert the filename to the system encoding. */
34799 zConverted = convertUtf8Filename(zUtf8Name);
34800 if( zConverted==0 ){
 
34801 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
34802 return SQLITE_IOERR_NOMEM;
34803 }
34804
34805 if( winIsDir(zConverted) ){
34806 sqlite3_free(zConverted);
 
34807 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
34808 return SQLITE_CANTOPEN_ISDIR;
34809 }
34810
34811 if( isReadWrite ){
@@ -34848,11 +35069,11 @@
34848 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
34849 #if SQLITE_OS_WINCE
34850 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
34851 #endif
34852
34853 if( isNT() ){
34854 #if SQLITE_OS_WINRT
34855 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
34856 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
34857 extendedParameters.dwFileAttributes =
34858 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34863,21 +35084,21 @@
34863 while( (h = osCreateFile2((LPCWSTR)zConverted,
34864 dwDesiredAccess,
34865 dwShareMode,
34866 dwCreationDisposition,
34867 &extendedParameters))==INVALID_HANDLE_VALUE &&
34868 retryIoerr(&cnt, &lastErrno) ){
34869 /* Noop */
34870 }
34871 #else
34872 while( (h = osCreateFileW((LPCWSTR)zConverted,
34873 dwDesiredAccess,
34874 dwShareMode, NULL,
34875 dwCreationDisposition,
34876 dwFlagsAndAttributes,
34877 NULL))==INVALID_HANDLE_VALUE &&
34878 retryIoerr(&cnt, &lastErrno) ){
34879 /* Noop */
34880 }
34881 #endif
34882 }
34883 #ifdef SQLITE_WIN32_HAS_ANSI
@@ -34886,24 +35107,25 @@
34886 dwDesiredAccess,
34887 dwShareMode, NULL,
34888 dwCreationDisposition,
34889 dwFlagsAndAttributes,
34890 NULL))==INVALID_HANDLE_VALUE &&
34891 retryIoerr(&cnt, &lastErrno) ){
34892 /* Noop */
34893 }
34894 }
34895 #endif
34896 logIoerr(cnt);
34897
34898 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
34899 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
34900
34901 if( h==INVALID_HANDLE_VALUE ){
34902 pFile->lastErrno = lastErrno;
34903 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
34904 sqlite3_free(zConverted);
 
34905 if( isReadWrite && !isExclusive ){
34906 return winOpen(pVfs, zName, id,
34907 ((flags|SQLITE_OPEN_READONLY) &
34908 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
34909 pOutFlags);
@@ -34928,19 +35150,21 @@
34928 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
34929 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
34930 ){
34931 osCloseHandle(h);
34932 sqlite3_free(zConverted);
 
34933 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
34934 return rc;
34935 }
34936 if( isTemp ){
34937 pFile->zDeleteOnClose = zConverted;
34938 }else
34939 #endif
34940 {
34941 sqlite3_free(zConverted);
 
34942 }
34943
34944 pFile->pMethod = &winIoMethod;
34945 pFile->pVfs = pVfs;
34946 pFile->h = h;
@@ -34990,15 +35214,16 @@
34990 UNUSED_PARAMETER(syncDir);
34991
34992 SimulateIOError(return SQLITE_IOERR_DELETE);
34993 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
34994
34995 zConverted = convertUtf8Filename(zFilename);
34996 if( zConverted==0 ){
 
34997 return SQLITE_IOERR_NOMEM;
34998 }
34999 if( isNT() ){
35000 do {
35001 #if SQLITE_OS_WINRT
35002 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35003 memset(&sAttrData, 0, sizeof(sAttrData));
35004 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35033,11 +35258,11 @@
35033 }
35034 if ( osDeleteFileW(zConverted) ){
35035 rc = SQLITE_OK; /* Deleted OK. */
35036 break;
35037 }
35038 if ( !retryIoerr(&cnt, &lastErrno) ){
35039 rc = SQLITE_ERROR; /* No more retries. */
35040 break;
35041 }
35042 } while(1);
35043 }
@@ -35061,22 +35286,21 @@
35061 }
35062 if ( osDeleteFileA(zConverted) ){
35063 rc = SQLITE_OK; /* Deleted OK. */
35064 break;
35065 }
35066 if ( !retryIoerr(&cnt, &lastErrno) ){
35067 rc = SQLITE_ERROR; /* No more retries. */
35068 break;
35069 }
35070 } while(1);
35071 }
35072 #endif
35073 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
35074 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno,
35075 "winDelete", zFilename);
35076 }else{
35077 logIoerr(cnt);
35078 }
35079 sqlite3_free(zConverted);
35080 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
35081 return rc;
35082 }
@@ -35098,22 +35322,22 @@
35098
35099 SimulateIOError( return SQLITE_IOERR_ACCESS; );
35100 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
35101 zFilename, flags, pResOut));
35102
35103 zConverted = convertUtf8Filename(zFilename);
35104 if( zConverted==0 ){
35105 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35106 return SQLITE_IOERR_NOMEM;
35107 }
35108 if( isNT() ){
35109 int cnt = 0;
35110 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35111 memset(&sAttrData, 0, sizeof(sAttrData));
35112 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35113 GetFileExInfoStandard,
35114 &sAttrData)) && retryIoerr(&cnt, &lastErrno) ){}
35115 if( rc ){
35116 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
35117 ** as if it does not exist.
35118 */
35119 if( flags==SQLITE_ACCESS_EXISTS
@@ -35122,15 +35346,15 @@
35122 attr = INVALID_FILE_ATTRIBUTES;
35123 }else{
35124 attr = sAttrData.dwFileAttributes;
35125 }
35126 }else{
35127 logIoerr(cnt);
35128 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
35129 winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess", zFilename);
35130 sqlite3_free(zConverted);
35131 return SQLITE_IOERR_ACCESS;
 
35132 }else{
35133 attr = INVALID_FILE_ATTRIBUTES;
35134 }
35135 }
35136 }
@@ -35156,10 +35380,19 @@
35156 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
35157 zFilename, pResOut, *pResOut));
35158 return SQLITE_OK;
35159 }
35160
 
 
 
 
 
 
 
 
 
35161
35162 /*
35163 ** Returns non-zero if the specified path name should be used verbatim. If
35164 ** non-zero is returned from this function, the calling function must simply
35165 ** use the provided path name verbatim -OR- resolve it into a full path name
@@ -35173,21 +35406,21 @@
35173 ** a legal UNC name, a volume relative path, or an absolute path name in the
35174 ** "Unix" format on Windows. There is no easy way to differentiate between
35175 ** the final two cases; therefore, we return the safer return value of TRUE
35176 ** so that callers of this function will simply use it verbatim.
35177 */
35178 if ( zPathname[0]=='/' || zPathname[0]=='\\' ){
35179 return TRUE;
35180 }
35181
35182 /*
35183 ** If the path name starts with a letter and a colon it is either a volume
35184 ** relative path or an absolute path. Callers of this function must not
35185 ** attempt to treat it as a relative path name (i.e. they should simply use
35186 ** it verbatim).
35187 */
35188 if ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' ){
35189 return TRUE;
35190 }
35191
35192 /*
35193 ** If we get to this point, the path name should almost certainly be a purely
@@ -35209,33 +35442,35 @@
35209 ){
35210
35211 #if defined(__CYGWIN__)
35212 SimulateIOError( return SQLITE_ERROR );
35213 UNUSED_PARAMETER(nFull);
35214 assert( pVfs->mxPathname>=SQLITE_WIN32_MAX_PATH );
35215 assert( nFull>=pVfs->mxPathname );
35216 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
35217 /*
35218 ** NOTE: We are dealing with a relative path name and the data
35219 ** directory has been set. Therefore, use it as the basis
35220 ** for converting the relative path name to an absolute
35221 ** one by prepending the data directory and a slash.
35222 */
35223 char zOut[SQLITE_WIN32_MAX_PATH+1];
 
 
 
35224 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35225 SQLITE_WIN32_MAX_PATH+1)<0 ){
35226 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35227 zRelative);
35228 return SQLITE_CANTOPEN_FULLPATH;
35229 }
35230 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35231 sqlite3_data_directory, zOut);
 
35232 }else{
35233 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
35234 winLogError(SQLITE_CANTOPEN_FULLPATH, (DWORD)errno, "cygwin_conv_path",
35235 zRelative);
35236 return SQLITE_CANTOPEN_FULLPATH;
35237 }
35238 }
35239 return SQLITE_OK;
35240 #endif
35241
@@ -35248,12 +35483,12 @@
35248 ** NOTE: We are dealing with a relative path name and the data
35249 ** directory has been set. Therefore, use it as the basis
35250 ** for converting the relative path name to an absolute
35251 ** one by prepending the data directory and a backslash.
35252 */
35253 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35254 sqlite3_data_directory, zRelative);
35255 }else{
35256 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
35257 }
35258 return SQLITE_OK;
35259 #endif
@@ -35264,11 +35499,11 @@
35264 char *zOut;
35265
35266 /* If this path name begins with "/X:", where "X" is any alphabetic
35267 ** character, discard the initial "/" from the pathname.
35268 */
35269 if( zRelative[0]=='/' && sqlite3Isalpha(zRelative[1]) && zRelative[2]==':' ){
35270 zRelative++;
35271 }
35272
35273 /* It's odd to simulate an io-error here, but really this is just
35274 ** using the io-error infrastructure to test that SQLite handles this
@@ -35281,68 +35516,64 @@
35281 ** NOTE: We are dealing with a relative path name and the data
35282 ** directory has been set. Therefore, use it as the basis
35283 ** for converting the relative path name to an absolute
35284 ** one by prepending the data directory and a backslash.
35285 */
35286 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s\\%s",
35287 sqlite3_data_directory, zRelative);
35288 return SQLITE_OK;
35289 }
35290 zConverted = convertUtf8Filename(zRelative);
35291 if( zConverted==0 ){
35292 return SQLITE_IOERR_NOMEM;
35293 }
35294 if( isNT() ){
35295 LPWSTR zTemp;
35296 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
35297 if( nByte==0 ){
35298 winLogError(SQLITE_ERROR, osGetLastError(),
35299 "GetFullPathNameW1", zConverted);
35300 sqlite3_free(zConverted);
35301 return SQLITE_CANTOPEN_FULLPATH;
 
35302 }
35303 nByte += 3;
35304 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
35305 if( zTemp==0 ){
35306 sqlite3_free(zConverted);
35307 return SQLITE_IOERR_NOMEM;
35308 }
35309 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
35310 if( nByte==0 ){
35311 winLogError(SQLITE_ERROR, osGetLastError(),
35312 "GetFullPathNameW2", zConverted);
35313 sqlite3_free(zConverted);
35314 sqlite3_free(zTemp);
35315 return SQLITE_CANTOPEN_FULLPATH;
 
35316 }
35317 sqlite3_free(zConverted);
35318 zOut = unicodeToUtf8(zTemp);
35319 sqlite3_free(zTemp);
35320 }
35321 #ifdef SQLITE_WIN32_HAS_ANSI
35322 else{
35323 char *zTemp;
35324 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
35325 if( nByte==0 ){
35326 winLogError(SQLITE_ERROR, osGetLastError(),
35327 "GetFullPathNameA1", zConverted);
35328 sqlite3_free(zConverted);
35329 return SQLITE_CANTOPEN_FULLPATH;
 
35330 }
35331 nByte += 3;
35332 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
35333 if( zTemp==0 ){
35334 sqlite3_free(zConverted);
35335 return SQLITE_IOERR_NOMEM;
35336 }
35337 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
35338 if( nByte==0 ){
35339 winLogError(SQLITE_ERROR, osGetLastError(),
35340 "GetFullPathNameA2", zConverted);
35341 sqlite3_free(zConverted);
35342 sqlite3_free(zTemp);
35343 return SQLITE_CANTOPEN_FULLPATH;
 
35344 }
35345 sqlite3_free(zConverted);
35346 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
35347 sqlite3_free(zTemp);
35348 }
@@ -35366,16 +35597,16 @@
35366 ** Interfaces for opening a shared library, finding entry points
35367 ** within the shared library, and closing the shared library.
35368 */
35369 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
35370 HANDLE h;
35371 void *zConverted = convertUtf8Filename(zFilename);
35372 UNUSED_PARAMETER(pVfs);
35373 if( zConverted==0 ){
35374 return 0;
35375 }
35376 if( isNT() ){
35377 #if SQLITE_OS_WINRT
35378 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
35379 #else
35380 h = osLoadLibraryW((LPCWSTR)zConverted);
35381 #endif
@@ -35388,11 +35619,11 @@
35388 sqlite3_free(zConverted);
35389 return (void*)h;
35390 }
35391 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
35392 UNUSED_PARAMETER(pVfs);
35393 getLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35394 }
35395 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
35396 UNUSED_PARAMETER(pVfs);
35397 return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
35398 }
@@ -35564,21 +35795,21 @@
35564 ** by sqlite into the error message available to the user using
35565 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
35566 */
35567 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
35568 UNUSED_PARAMETER(pVfs);
35569 return getLastErrorMsg(osGetLastError(), nBuf, zBuf);
35570 }
35571
35572 /*
35573 ** Initialize and deinitialize the operating system interface.
35574 */
35575 SQLITE_API int sqlite3_os_init(void){
35576 static sqlite3_vfs winVfs = {
35577 3, /* iVersion */
35578 sizeof(winFile), /* szOsFile */
35579 SQLITE_WIN32_MAX_PATH, /* mxPathname */
35580 0, /* pNext */
35581 "win32", /* zName */
35582 0, /* pAppData */
35583 winOpen, /* xOpen */
35584 winDelete, /* xDelete */
@@ -35595,10 +35826,36 @@
35595 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35596 winSetSystemCall, /* xSetSystemCall */
35597 winGetSystemCall, /* xGetSystemCall */
35598 winNextSystemCall, /* xNextSystemCall */
35599 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35600
35601 /* Double-check that the aSyscall[] array has been constructed
35602 ** correctly. See ticket [bb3a86e890c8e96ab] */
35603 assert( ArraySize(aSyscall)==74 );
35604
@@ -35611,10 +35868,15 @@
35611 #endif
35612 assert( winSysInfo.dwAllocationGranularity>0 );
35613 assert( winSysInfo.dwPageSize>0 );
35614
35615 sqlite3_vfs_register(&winVfs, 1);
 
 
 
 
 
35616 return SQLITE_OK;
35617 }
35618
35619 SQLITE_API int sqlite3_os_end(void){
35620 #if SQLITE_OS_WINRT
@@ -52086,10 +52348,11 @@
52086 pBt->max1bytePayload = (u8)pBt->maxLocal;
52087 }
52088 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
52089 pBt->pPage1 = pPage1;
52090 pBt->nPage = nPage;
 
52091 return SQLITE_OK;
52092
52093 page1_init_failed:
52094 releasePage(pPage1);
52095 pBt->pPage1 = 0;
@@ -59838,43 +60101,108 @@
59838 }
59839 return p;
59840 }
59841
59842 /*
59843 ** Create a new sqlite3_value object, containing the value of pExpr.
 
 
 
 
 
 
 
 
 
 
 
 
 
59844 **
59845 ** This only works for very simple expressions that consist of one constant
59846 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
59847 ** be converted directly into a value, then the value is allocated and
59848 ** a pointer written to *ppVal. The caller is responsible for deallocating
59849 ** the value by passing it to sqlite3ValueFree() later on. If the expression
59850 ** cannot be converted to a value, then *ppVal is set to NULL.
59851 */
59852 SQLITE_PRIVATE int sqlite3ValueFromExpr(
59853 sqlite3 *db, /* The database connection */
59854 Expr *pExpr, /* The expression to evaluate */
59855 u8 enc, /* Encoding to use */
59856 u8 affinity, /* Affinity to use */
59857 sqlite3_value **ppVal /* Write the new value here */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59858 ){
59859 int op;
59860 char *zVal = 0;
59861 sqlite3_value *pVal = 0;
59862 int negInt = 1;
59863 const char *zNeg = "";
 
59864
59865 if( !pExpr ){
59866 *ppVal = 0;
59867 return SQLITE_OK;
59868 }
59869 op = pExpr->op;
59870
59871 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
59872 ** The ifdef here is to enable us to achieve 100% branch test coverage even
59873 ** when SQLITE_ENABLE_STAT3 is omitted.
59874 */
59875 #ifdef SQLITE_ENABLE_STAT3
59876 if( op==TK_REGISTER ) op = pExpr->op2;
59877 #else
59878 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
59879 #endif
59880
@@ -59888,11 +60216,11 @@
59888 negInt = -1;
59889 zNeg = "-";
59890 }
59891
59892 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
59893 pVal = sqlite3ValueNew(db);
59894 if( pVal==0 ) goto no_mem;
59895 if( ExprHasProperty(pExpr, EP_IntValue) ){
59896 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
59897 }else{
59898 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59905,15 +60233,17 @@
59905 }else{
59906 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
59907 }
59908 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
59909 if( enc!=SQLITE_UTF8 ){
59910 sqlite3VdbeChangeEncoding(pVal, enc);
59911 }
59912 }else if( op==TK_UMINUS ) {
59913 /* This branch happens for multiple negative signs. Ex: -(-5) */
59914 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal) ){
 
 
59915 sqlite3VdbeMemNumerify(pVal);
59916 if( pVal->u.i==SMALLEST_INT64 ){
59917 pVal->flags &= MEM_Int;
59918 pVal->flags |= MEM_Real;
59919 pVal->r = (double)LARGEST_INT64;
@@ -59922,19 +60252,19 @@
59922 }
59923 pVal->r = -pVal->r;
59924 sqlite3ValueApplyAffinity(pVal, affinity, enc);
59925 }
59926 }else if( op==TK_NULL ){
59927 pVal = sqlite3ValueNew(db);
59928 if( pVal==0 ) goto no_mem;
59929 }
59930 #ifndef SQLITE_OMIT_BLOB_LITERAL
59931 else if( op==TK_BLOB ){
59932 int nVal;
59933 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
59934 assert( pExpr->u.zToken[1]=='\'' );
59935 pVal = sqlite3ValueNew(db);
59936 if( !pVal ) goto no_mem;
59937 zVal = &pExpr->u.zToken[2];
59938 nVal = sqlite3Strlen30(zVal)-1;
59939 assert( zVal[nVal]=='\'' );
59940 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59944,20 +60274,206 @@
59944
59945 if( pVal ){
59946 sqlite3VdbeMemStoreType(pVal);
59947 }
59948 *ppVal = pVal;
59949 return SQLITE_OK;
59950
59951 no_mem:
59952 db->mallocFailed = 1;
59953 sqlite3DbFree(db, zVal);
59954 sqlite3ValueFree(pVal);
59955 *ppVal = 0;
 
 
 
 
59956 return SQLITE_NOMEM;
59957 }
59958
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59959 /*
59960 ** Change the string value of an sqlite3_value object
59961 */
59962 SQLITE_PRIVATE void sqlite3ValueSetStr(
59963 sqlite3_value *v, /* Value to be set */
@@ -66073,11 +66589,11 @@
66073 ** value or convert mem[p2] to a different type.
66074 */
66075 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
66076 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
66077 assert( pOp->p2>0 );
66078 assert( pOp->p2<=p->nMem );
66079 pOut = &aMem[pOp->p2];
66080 memAboutToChange(p, pOut);
66081 VdbeMemRelease(pOut);
66082 pOut->flags = MEM_Int;
66083 }
@@ -66084,34 +66600,34 @@
66084
66085 /* Sanity checking on other operands */
66086 #ifdef SQLITE_DEBUG
66087 if( (pOp->opflags & OPFLG_IN1)!=0 ){
66088 assert( pOp->p1>0 );
66089 assert( pOp->p1<=p->nMem );
66090 assert( memIsValid(&aMem[pOp->p1]) );
66091 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
66092 }
66093 if( (pOp->opflags & OPFLG_IN2)!=0 ){
66094 assert( pOp->p2>0 );
66095 assert( pOp->p2<=p->nMem );
66096 assert( memIsValid(&aMem[pOp->p2]) );
66097 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
66098 }
66099 if( (pOp->opflags & OPFLG_IN3)!=0 ){
66100 assert( pOp->p3>0 );
66101 assert( pOp->p3<=p->nMem );
66102 assert( memIsValid(&aMem[pOp->p3]) );
66103 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
66104 }
66105 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
66106 assert( pOp->p2>0 );
66107 assert( pOp->p2<=p->nMem );
66108 memAboutToChange(p, &aMem[pOp->p2]);
66109 }
66110 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
66111 assert( pOp->p3>0 );
66112 assert( pOp->p3<=p->nMem );
66113 memAboutToChange(p, &aMem[pOp->p3]);
66114 }
66115 #endif
66116
66117 switch( pOp->opcode ){
@@ -66200,11 +66716,11 @@
66200 **
66201 ** Write the current address onto register P1
66202 ** and then jump to address P2.
66203 */
66204 case OP_Gosub: { /* jump */
66205 assert( pOp->p1>0 && pOp->p1<=p->nMem );
66206 pIn1 = &aMem[pOp->p1];
66207 assert( (pIn1->flags & MEM_Dyn)==0 );
66208 memAboutToChange(p, pIn1);
66209 pIn1->flags = MEM_Int;
66210 pIn1->u.i = pc;
@@ -66416,11 +66932,11 @@
66416 #if 0 /* local variables moved into u.ab */
66417 int cnt;
66418 u16 nullFlag;
66419 #endif /* local variables moved into u.ab */
66420 u.ab.cnt = pOp->p3-pOp->p2;
66421 assert( pOp->p3<=p->nMem );
66422 pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
66423 while( u.ab.cnt>0 ){
66424 pOut++;
66425 memAboutToChange(p, pOut);
66426 VdbeMemRelease(pOut);
@@ -66489,12 +67005,12 @@
66489 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
66490
66491 pIn1 = &aMem[u.ad.p1];
66492 pOut = &aMem[u.ad.p2];
66493 while( u.ad.n-- ){
66494 assert( pOut<=&aMem[p->nMem] );
66495 assert( pIn1<=&aMem[p->nMem] );
66496 assert( memIsValid(pIn1) );
66497 memAboutToChange(p, pOut);
66498 u.ad.zMalloc = pOut->zMalloc;
66499 pOut->zMalloc = 0;
66500 sqlite3VdbeMemMove(pOut, pIn1);
@@ -66578,11 +67094,11 @@
66578 Mem *pMem;
66579 int i;
66580 #endif /* local variables moved into u.af */
66581 assert( p->nResColumn==pOp->p2 );
66582 assert( pOp->p1>0 );
66583 assert( pOp->p1+pOp->p2<=p->nMem+1 );
66584
66585 /* If this statement has violated immediate foreign key constraints, do
66586 ** not return the number of rows modified. And do not RELEASE the statement
66587 ** transaction. It needs to be rolled back. */
66588 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66858,15 +67374,15 @@
66858 #endif /* local variables moved into u.ai */
66859
66860 u.ai.n = pOp->p5;
66861 u.ai.apVal = p->apArg;
66862 assert( u.ai.apVal || u.ai.n==0 );
66863 assert( pOp->p3>0 && pOp->p3<=p->nMem );
66864 pOut = &aMem[pOp->p3];
66865 memAboutToChange(p, pOut);
66866
66867 assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=p->nMem+1) );
66868 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
66869 u.ai.pArg = &aMem[pOp->p2];
66870 for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
66871 assert( memIsValid(u.ai.pArg) );
66872 u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -67398,15 +67914,15 @@
67398 u.al.p2 = pOp->p2;
67399 #if SQLITE_DEBUG
67400 if( aPermute ){
67401 int k, mx = 0;
67402 for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67403 assert( u.al.p1>0 && u.al.p1+mx<=p->nMem+1 );
67404 assert( u.al.p2>0 && u.al.p2+mx<=p->nMem+1 );
67405 }else{
67406 assert( u.al.p1>0 && u.al.p1+u.al.n<=p->nMem+1 );
67407 assert( u.al.p2>0 && u.al.p2+u.al.n<=p->nMem+1 );
67408 }
67409 #endif /* SQLITE_DEBUG */
67410 for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
67411 u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
67412 assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67659,11 +68175,11 @@
67659 u.ao.p1 = pOp->p1;
67660 u.ao.p2 = pOp->p2;
67661 u.ao.pC = 0;
67662 memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
67663 assert( u.ao.p1<p->nCursor );
67664 assert( pOp->p3>0 && pOp->p3<=p->nMem );
67665 u.ao.pDest = &aMem[pOp->p3];
67666 memAboutToChange(p, u.ao.pDest);
67667 u.ao.zRec = 0;
67668
67669 /* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67959,11 +68475,11 @@
67959 u.ap.zAffinity = pOp->p4.z;
67960 assert( u.ap.zAffinity!=0 );
67961 assert( u.ap.zAffinity[pOp->p2]==0 );
67962 pIn1 = &aMem[pOp->p1];
67963 while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
67964 assert( pIn1 <= &p->aMem[p->nMem] );
67965 assert( memIsValid(pIn1) );
67966 ExpandBlob(pIn1);
67967 applyAffinity(pIn1, u.ap.cAff, encoding);
67968 pIn1++;
67969 }
@@ -68022,11 +68538,11 @@
68022 u.aq.nData = 0; /* Number of bytes of data space */
68023 u.aq.nHdr = 0; /* Number of bytes of header space */
68024 u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
68025 u.aq.nField = pOp->p1;
68026 u.aq.zAffinity = pOp->p4.z;
68027 assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=p->nMem+1 );
68028 u.aq.pData0 = &aMem[u.aq.nField];
68029 u.aq.nField = pOp->p2;
68030 u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
68031 u.aq.file_format = p->minWriteFileFormat;
68032
@@ -68088,11 +68604,11 @@
68088 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
68089 u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
68090 }
68091 assert( u.aq.i==u.aq.nByte );
68092
68093 assert( pOp->p3>0 && pOp->p3<=p->nMem );
68094 pOut->n = (int)u.aq.nByte;
68095 pOut->flags = MEM_Blob | MEM_Dyn;
68096 pOut->xDel = 0;
68097 if( u.aq.nZero ){
68098 pOut->u.nZero = u.aq.nZero;
@@ -68684,11 +69200,11 @@
68684 }else{
68685 u.ay.wrFlag = 0;
68686 }
68687 if( pOp->p5 & OPFLAG_P2ISREG ){
68688 assert( u.ay.p2>0 );
68689 assert( u.ay.p2<=p->nMem );
68690 pIn2 = &aMem[u.ay.p2];
68691 assert( memIsValid(pIn2) );
68692 assert( (pIn2->flags & MEM_Int)!=0 );
68693 sqlite3VdbeMemIntegerify(pIn2);
68694 u.ay.p2 = (int)pIn2->u.i;
@@ -69235,11 +69751,11 @@
69235
69236 pIn3 = &aMem[pOp->p3];
69237 u.bf.aMx = &aMem[pOp->p4.i];
69238 /* Assert that the values of parameters P1 and P4 are in range. */
69239 assert( pOp->p4type==P4_INT32 );
69240 assert( pOp->p4.i>0 && pOp->p4.i<=p->nMem );
69241 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69242
69243 /* Find the index cursor. */
69244 u.bf.pCx = p->apCsr[pOp->p1];
69245 assert( u.bf.pCx->deferredMoveto==0 );
@@ -69442,11 +69958,11 @@
69442 /* Assert that P3 is a valid memory cell. */
69443 assert( pOp->p3<=u.bh.pFrame->nMem );
69444 u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
69445 }else{
69446 /* Assert that P3 is a valid memory cell. */
69447 assert( pOp->p3<=p->nMem );
69448 u.bh.pMem = &aMem[pOp->p3];
69449 memAboutToChange(p, u.bh.pMem);
69450 }
69451 assert( memIsValid(u.bh.pMem) );
69452
@@ -70120,11 +70636,11 @@
70120 int res;
70121 UnpackedRecord r;
70122 #endif /* local variables moved into u.bt */
70123
70124 assert( pOp->p3>0 );
70125 assert( pOp->p2>0 && pOp->p2+pOp->p3<=p->nMem+1 );
70126 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70127 u.bt.pC = p->apCsr[pOp->p1];
70128 assert( u.bt.pC!=0 );
70129 u.bt.pCrsr = u.bt.pC->pCursor;
70130 if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70336,10 +70852,11 @@
70336 int nChange;
70337 #endif /* local variables moved into u.bx */
70338
70339 u.bx.nChange = 0;
70340 assert( p->readOnly==0 );
 
70341 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
70342 rc = sqlite3BtreeClearTable(
70343 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
70344 );
70345 if( pOp->p3 ){
@@ -70542,11 +71059,11 @@
70542 assert( p->bIsReader );
70543 u.ca.nRoot = pOp->p2;
70544 assert( u.ca.nRoot>0 );
70545 u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
70546 if( u.ca.aRoot==0 ) goto no_mem;
70547 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70548 u.ca.pnErr = &aMem[pOp->p3];
70549 assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
70550 assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
70551 pIn1 = &aMem[pOp->p1];
70552 for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70978,11 +71495,11 @@
70978 u.cg.apVal[u.cg.i] = u.cg.pRec;
70979 memAboutToChange(p, u.cg.pRec);
70980 sqlite3VdbeMemStoreType(u.cg.pRec);
70981 }
70982 u.cg.ctx.pFunc = pOp->p4.pFunc;
70983 assert( pOp->p3>0 && pOp->p3<=p->nMem );
70984 u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
70985 u.cg.pMem->n++;
70986 u.cg.ctx.s.flags = MEM_Null;
70987 u.cg.ctx.s.z = 0;
70988 u.cg.ctx.s.zMalloc = 0;
@@ -71027,11 +71544,11 @@
71027 */
71028 case OP_AggFinal: {
71029 #if 0 /* local variables moved into u.ch */
71030 Mem *pMem;
71031 #endif /* local variables moved into u.ch */
71032 assert( pOp->p1>0 && pOp->p1<=p->nMem );
71033 u.ch.pMem = &aMem[pOp->p1];
71034 assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
71035 rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
71036 if( rc ){
71037 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71458,11 +71975,11 @@
71458 sqlite3_context sContext;
71459 #endif /* local variables moved into u.co */
71460
71461 VdbeCursor *pCur = p->apCsr[pOp->p1];
71462 assert( pCur->pVtabCursor );
71463 assert( pOp->p3>0 && pOp->p3<=p->nMem );
71464 u.co.pDest = &aMem[pOp->p3];
71465 memAboutToChange(p, u.co.pDest);
71466 if( pCur->nullRow ){
71467 sqlite3VdbeMemSetNull(u.co.pDest);
71468 break;
@@ -76729,11 +77246,11 @@
76729 break;
76730 }
76731 case TK_UMINUS: {
76732 int v;
76733 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
76734 assert( v!=-2147483648 );
76735 *pValue = -v;
76736 rc = 1;
76737 }
76738 break;
76739 }
@@ -80389,11 +80906,11 @@
80389
80390 /* Ensure the default expression is something that sqlite3ValueFromExpr()
80391 ** can handle (i.e. not CURRENT_TIME etc.)
80392 */
80393 if( pDflt ){
80394 sqlite3_value *pVal;
80395 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
80396 db->mallocFailed = 1;
80397 return;
80398 }
80399 if( !pVal ){
@@ -80530,11 +81047,11 @@
80530 #endif /* SQLITE_ALTER_TABLE */
80531
80532 /************** End of alter.c ***********************************************/
80533 /************** Begin file analyze.c *****************************************/
80534 /*
80535 ** 2005 July 8
80536 **
80537 ** The author disclaims copyright to this source code. In place of
80538 ** a legal notice, here is a blessing:
80539 **
80540 ** May you do good and not evil.
@@ -80551,27 +81068,36 @@
80551 ** The following system tables are or have been supported:
80552 **
80553 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
80554 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
80555 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
 
80556 **
80557 ** Additional tables might be added in future releases of SQLite.
80558 ** The sqlite_stat2 table is not created or used unless the SQLite version
80559 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
80560 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
80561 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
80562 ** created and used by SQLite versions 3.7.9 and later and with
80563 ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
80564 ** is a superset of sqlite_stat2.
 
 
 
 
 
 
 
80565 **
80566 ** Format of sqlite_stat1:
80567 **
80568 ** There is normally one row per index, with the index identified by the
80569 ** name in the idx column. The tbl column is the name of the table to
80570 ** which the index belongs. In each such row, the stat column will be
80571 ** a string consisting of a list of integers. The first integer in this
80572 ** list is the number of rows in the index and in the table. The second
 
80573 ** integer is the average number of rows in the index that have the same
80574 ** value in the first column of the index. The third integer is the average
80575 ** number of rows in the index that have the same value for the first two
80576 ** columns. The N-th integer (for N>1) is the average number of rows in
80577 ** the index which have the same value for the first N-1 columns. For
@@ -80614,57 +81140,85 @@
80614 ** writes the sqlite_stat2 table. This version of SQLite only supports
80615 ** sqlite_stat3.
80616 **
80617 ** Format for sqlite_stat3:
80618 **
80619 ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
80620 ** used to avoid compatibility problems.
 
80621 **
80622 ** The format of the sqlite_stat3 table is similar to the format of
80623 ** the sqlite_stat2 table. There are multiple entries for each index.
 
 
 
 
 
 
80624 ** The idx column names the index and the tbl column is the table of the
80625 ** index. If the idx and tbl columns are the same, then the sample is
80626 ** of the INTEGER PRIMARY KEY. The sample column is a value taken from
80627 ** the left-most column of the index. The nEq column is the approximate
80628 ** number of entires in the index whose left-most column exactly matches
80629 ** the sample. nLt is the approximate number of entires whose left-most
80630 ** column is less than the sample. The nDLt column is the approximate
80631 ** number of distinct left-most entries in the index that are less than
80632 ** the sample.
80633 **
80634 ** Future versions of SQLite might change to store a string containing
80635 ** multiple integers values in the nDLt column of sqlite_stat3. The first
80636 ** integer will be the number of prior index entires that are distinct in
80637 ** the left-most column. The second integer will be the number of prior index
80638 ** entries that are distinct in the first two columns. The third integer
80639 ** will be the number of prior index entries that are distinct in the first
80640 ** three columns. And so forth. With that extension, the nDLt field is
80641 ** similar in function to the sqlite_stat1.stat field.
80642 **
80643 ** There can be an arbitrary number of sqlite_stat3 entries per index.
80644 ** The ANALYZE command will typically generate sqlite_stat3 tables
80645 ** that contain between 10 and 40 samples which are distributed across
80646 ** the key space, though not uniformly, and which include samples with
80647 ** largest possible nEq values.
 
 
 
 
 
 
 
 
 
 
80648 */
80649 #ifndef SQLITE_OMIT_ANALYZE
80650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80651 /*
80652 ** This routine generates code that opens the sqlite_stat1 table for
80653 ** writing with cursor iStatCur. If the library was built with the
80654 ** SQLITE_ENABLE_STAT3 macro defined, then the sqlite_stat3 table is
80655 ** opened for writing using cursor (iStatCur+1)
80656 **
80657 ** If the sqlite_stat1 tables does not previously exist, it is created.
80658 ** Similarly, if the sqlite_stat3 table does not exist and the library
80659 ** is compiled with SQLITE_ENABLE_STAT3 defined, it is created.
80660 **
80661 ** Argument zWhere may be a pointer to a buffer containing a table name,
80662 ** or it may be a NULL pointer. If it is not NULL, then all entries in
80663 ** the sqlite_stat1 and (if applicable) sqlite_stat3 tables associated
80664 ** with the named table are deleted. If zWhere==0, then code is generated
80665 ** to delete all stat table entries.
80666 */
80667 static void openStatTable(
80668 Parse *pParse, /* Parsing context */
80669 int iDb, /* The database we are looking in */
80670 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80674,22 +81228,28 @@
80674 static const struct {
80675 const char *zName;
80676 const char *zCols;
80677 } aTable[] = {
80678 { "sqlite_stat1", "tbl,idx,stat" },
80679 #ifdef SQLITE_ENABLE_STAT3
 
 
 
80680 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
 
 
 
 
80681 #endif
80682 };
80683
80684 int aRoot[] = {0, 0};
80685 u8 aCreateTbl[] = {0, 0};
80686
80687 int i;
80688 sqlite3 *db = pParse->db;
80689 Db *pDb;
80690 Vdbe *v = sqlite3GetVdbe(pParse);
 
 
 
80691 if( v==0 ) return;
80692 assert( sqlite3BtreeHoldsAllMutexes(db) );
80693 assert( sqlite3VdbeDb(v)==db );
80694 pDb = &db->aDb[iDb];
80695
@@ -80698,262 +81258,626 @@
80698 */
80699 for(i=0; i<ArraySize(aTable); i++){
80700 const char *zTab = aTable[i].zName;
80701 Table *pStat;
80702 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
80703 /* The sqlite_stat[12] table does not exist. Create it. Note that a
80704 ** side-effect of the CREATE TABLE statement is to leave the rootpage
80705 ** of the new table in register pParse->regRoot. This is important
80706 ** because the OpenWrite opcode below will be needing it. */
80707 sqlite3NestedParse(pParse,
80708 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
80709 );
80710 aRoot[i] = pParse->regRoot;
80711 aCreateTbl[i] = OPFLAG_P2ISREG;
 
 
80712 }else{
80713 /* The table already exists. If zWhere is not NULL, delete all entries
80714 ** associated with the table zWhere. If zWhere is NULL, delete the
80715 ** entire contents of the table. */
80716 aRoot[i] = pStat->tnum;
 
80717 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
80718 if( zWhere ){
80719 sqlite3NestedParse(pParse,
80720 "DELETE FROM %Q.%s WHERE %s=%Q", pDb->zName, zTab, zWhereType, zWhere
 
80721 );
80722 }else{
80723 /* The sqlite_stat[12] table already exists. Delete all rows. */
80724 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
80725 }
80726 }
80727 }
80728
80729 /* Open the sqlite_stat[13] tables for writing. */
80730 for(i=0; i<ArraySize(aTable); i++){
 
80731 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
80732 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
80733 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
80734 }
80735 }
80736
80737 /*
80738 ** Recommended number of samples for sqlite_stat3
80739 */
80740 #ifndef SQLITE_STAT3_SAMPLES
80741 # define SQLITE_STAT3_SAMPLES 24
80742 #endif
80743
80744 /*
80745 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
80746 ** share an instance of the following structure to hold their state
80747 ** information.
80748 */
80749 typedef struct Stat3Accum Stat3Accum;
80750 struct Stat3Accum {
 
 
 
 
 
 
 
 
 
 
 
 
80751 tRowcnt nRow; /* Number of rows in the entire table */
80752 tRowcnt nPSample; /* How often to do a periodic sample */
80753 int iMin; /* Index of entry with minimum nEq and hash */
80754 int mxSample; /* Maximum number of samples to accumulate */
80755 int nSample; /* Current number of samples */
80756 u32 iPrn; /* Pseudo-random number used for sampling */
80757 struct Stat3Sample {
80758 i64 iRowid; /* Rowid in main table of the key */
80759 tRowcnt nEq; /* sqlite_stat3.nEq */
80760 tRowcnt nLt; /* sqlite_stat3.nLt */
80761 tRowcnt nDLt; /* sqlite_stat3.nDLt */
80762 u8 isPSample; /* True if a periodic sample */
80763 u32 iHash; /* Tiebreaker hash */
80764 } *a; /* An array of samples */
80765 };
80766
80767 #ifdef SQLITE_ENABLE_STAT3
80768 /*
80769 ** Implementation of the stat3_init(C,S) SQL function. The two parameters
80770 ** are the number of rows in the table or index (C) and the number of samples
80771 ** to accumulate (S).
80772 **
80773 ** This routine allocates the Stat3Accum object.
80774 **
80775 ** The return value is the Stat3Accum object (P).
80776 */
80777 static void stat3Init(
80778 sqlite3_context *context,
80779 int argc,
80780 sqlite3_value **argv
80781 ){
80782 Stat3Accum *p;
80783 tRowcnt nRow;
80784 int mxSample;
80785 int n;
 
 
 
80786
 
80787 UNUSED_PARAMETER(argc);
80788 nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
80789 mxSample = sqlite3_value_int(argv[1]);
80790 n = sizeof(*p) + sizeof(p->a[0])*mxSample;
80791 p = sqlite3MallocZero( n );
 
 
 
 
 
 
 
 
 
 
 
80792 if( p==0 ){
80793 sqlite3_result_error_nomem(context);
80794 return;
80795 }
80796 p->a = (struct Stat3Sample*)&p[1];
80797 p->nRow = nRow;
80798 p->mxSample = mxSample;
80799 p->nPSample = p->nRow/(mxSample/3+1) + 1;
80800 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80801 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
80802 }
80803 static const FuncDef stat3InitFuncdef = {
80804 2, /* nArg */
80805 SQLITE_UTF8, /* iPrefEnc */
80806 0, /* flags */
80807 0, /* pUserData */
80808 0, /* pNext */
80809 stat3Init, /* xFunc */
80810 0, /* xStep */
80811 0, /* xFinalize */
80812 "stat3_init", /* zName */
80813 0, /* pHash */
80814 0 /* pDestructor */
80815 };
80816
80817
80818 /*
80819 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
80820 ** arguments describe a single key instance. This routine makes the
80821 ** decision about whether or not to retain this key for the sqlite_stat3
80822 ** table.
80823 **
80824 ** The return value is NULL.
80825 */
80826 static void stat3Push(
80827 sqlite3_context *context,
80828 int argc,
80829 sqlite3_value **argv
80830 ){
80831 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
80832 tRowcnt nEq = sqlite3_value_int64(argv[0]);
80833 tRowcnt nLt = sqlite3_value_int64(argv[1]);
80834 tRowcnt nDLt = sqlite3_value_int64(argv[2]);
80835 i64 rowid = sqlite3_value_int64(argv[3]);
80836 u8 isPSample = 0;
80837 u8 doInsert = 0;
80838 int iMin = p->iMin;
80839 struct Stat3Sample *pSample;
80840 int i;
80841 u32 h;
80842
80843 UNUSED_PARAMETER(context);
80844 UNUSED_PARAMETER(argc);
80845 if( nEq==0 ) return;
80846 h = p->iPrn = p->iPrn*1103515245 + 12345;
80847 if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
80848 doInsert = isPSample = 1;
80849 }else if( p->nSample<p->mxSample ){
80850 doInsert = 1;
80851 }else{
80852 if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
80853 doInsert = 1;
80854 }
80855 }
80856 if( !doInsert ) return;
80857 if( p->nSample==p->mxSample ){
80858 assert( p->nSample - iMin - 1 >= 0 );
80859 memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80860 pSample = &p->a[p->nSample-1];
80861 }else{
80862 pSample = &p->a[p->nSample++];
80863 }
80864 pSample->iRowid = rowid;
80865 pSample->nEq = nEq;
80866 pSample->nLt = nLt;
80867 pSample->nDLt = nDLt;
80868 pSample->iHash = h;
80869 pSample->isPSample = isPSample;
80870
80871 /* Find the new minimum */
80872 if( p->nSample==p->mxSample ){
80873 pSample = p->a;
80874 i = 0;
80875 while( pSample->isPSample ){
80876 i++;
80877 pSample++;
80878 assert( i<p->nSample );
80879 }
80880 nEq = pSample->nEq;
80881 h = pSample->iHash;
80882 iMin = i;
80883 for(i++, pSample++; i<p->nSample; i++, pSample++){
80884 if( pSample->isPSample ) continue;
80885 if( pSample->nEq<nEq
80886 || (pSample->nEq==nEq && pSample->iHash<h)
80887 ){
80888 iMin = i;
80889 nEq = pSample->nEq;
80890 h = pSample->iHash;
80891 }
80892 }
 
 
80893 p->iMin = iMin;
80894 }
80895 }
80896 static const FuncDef stat3PushFuncdef = {
80897 5, /* nArg */
80898 SQLITE_UTF8, /* iPrefEnc */
80899 0, /* flags */
80900 0, /* pUserData */
80901 0, /* pNext */
80902 stat3Push, /* xFunc */
80903 0, /* xStep */
80904 0, /* xFinalize */
80905 "stat3_push", /* zName */
80906 0, /* pHash */
80907 0 /* pDestructor */
80908 };
80909
80910 /*
80911 ** Implementation of the stat3_get(P,N,...) SQL function. This routine is
80912 ** used to query the results. Content is returned for the Nth sqlite_stat3
80913 ** row where N is between 0 and S-1 and S is the number of samples. The
80914 ** value returned depends on the number of arguments.
80915 **
80916 ** argc==2 result: rowid
80917 ** argc==3 result: nEq
80918 ** argc==4 result: nLt
80919 ** argc==5 result: nDLt
80920 */
80921 static void stat3Get(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80922 sqlite3_context *context,
80923 int argc,
80924 sqlite3_value **argv
80925 ){
80926 int n = sqlite3_value_int(argv[1]);
80927 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
80928
80929 assert( p!=0 );
80930 if( p->nSample<=n ) return;
80931 switch( argc ){
80932 case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
80933 case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
80934 case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
80935 default: sqlite3_result_int64(context, p->a[n].nDLt); break;
80936 }
80937 }
80938 static const FuncDef stat3GetFuncdef = {
80939 -1, /* nArg */
80940 SQLITE_UTF8, /* iPrefEnc */
80941 0, /* flags */
80942 0, /* pUserData */
80943 0, /* pNext */
80944 stat3Get, /* xFunc */
80945 0, /* xStep */
80946 0, /* xFinalize */
80947 "stat3_get", /* zName */
80948 0, /* pHash */
80949 0 /* pDestructor */
80950 };
80951 #endif /* SQLITE_ENABLE_STAT3 */
80952
80953
80954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80955
80956 /*
80957 ** Generate code to do an analysis of all indices associated with
80958 ** a single table.
80959 */
@@ -80960,46 +81884,35 @@
80960 static void analyzeOneTable(
80961 Parse *pParse, /* Parser context */
80962 Table *pTab, /* Table whose indices are to be analyzed */
80963 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
80964 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
80965 int iMem /* Available memory locations begin here */
 
80966 ){
80967 sqlite3 *db = pParse->db; /* Database handle */
80968 Index *pIdx; /* An index to being analyzed */
80969 int iIdxCur; /* Cursor open on index being analyzed */
 
80970 Vdbe *v; /* The virtual machine being built up */
80971 int i; /* Loop counter */
80972 int topOfLoop; /* The top of the loop */
80973 int endOfLoop; /* The end of the loop */
80974 int jZeroRows = -1; /* Jump from here if number of rows is zero */
80975 int iDb; /* Index of database containing pTab */
80976 u8 needTableCnt = 1; /* True to count the table */
 
 
 
 
 
 
 
80977 int regTabname = iMem++; /* Register containing table name */
80978 int regIdxname = iMem++; /* Register containing index name */
80979 int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
80980 #ifdef SQLITE_ENABLE_STAT3
80981 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
80982 int regNumLt = iMem++; /* Number of keys less than regSample */
80983 int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
80984 int regSample = iMem++; /* The next sample value */
80985 int regRowid = regSample; /* Rowid of a sample */
80986 int regAccum = iMem++; /* Register to hold Stat3Accum object */
80987 int regLoop = iMem++; /* Loop counter */
80988 int regCount = iMem++; /* Number of rows in the table or index */
80989 int regTemp1 = iMem++; /* Intermediate register */
80990 int regTemp2 = iMem++; /* Intermediate register */
80991 int once = 1; /* One-time initialization */
80992 int shortJump = 0; /* Instruction address */
80993 int iTabCur = pParse->nTab++; /* Table cursor */
80994 #endif
80995 int regCol = iMem++; /* Content of a column in analyzed table */
80996 int regRec = iMem++; /* Register holding completed record */
80997 int regTemp = iMem++; /* Temporary use register */
80998 int regNewRowid = iMem++; /* Rowid for the inserted record */
80999
81000
81001 v = sqlite3GetVdbe(pParse);
81002 if( v==0 || NEVER(pTab==0) ){
81003 return;
81004 }
81005 if( pTab->tnum==0 ){
@@ -81019,217 +81932,230 @@
81019 db->aDb[iDb].zName ) ){
81020 return;
81021 }
81022 #endif
81023
81024 /* Establish a read-lock on the table at the shared-cache level. */
 
 
 
81025 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
81026
81027 iIdxCur = pParse->nTab++;
 
 
81028 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
 
81029 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81030 int nCol;
81031 KeyInfo *pKey;
81032 int addrIfNot = 0; /* address of OP_IfNot */
81033 int *aChngAddr; /* Array of jump instruction addresses */
 
 
81034
81035 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
81036 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
81037 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
81038 nCol = pIdx->nColumn;
81039 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
81040 if( aChngAddr==0 ) continue;
81041 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
81042 if( iMem+1+(nCol*2)>pParse->nMem ){
81043 pParse->nMem = iMem+1+(nCol*2);
81044 }
81045
81046 /* Open a cursor to the index to be analyzed. */
81047 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
81048 sqlite3VdbeAddOp4(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb,
81049 (char *)pKey, P4_KEYINFO_HANDOFF);
81050 VdbeComment((v, "%s", pIdx->zName));
81051
81052 /* Populate the register containing the index name. */
81053 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
81054
81055 #ifdef SQLITE_ENABLE_STAT3
81056 if( once ){
81057 once = 0;
81058 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81059 }
81060 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
81061 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
81062 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
81063 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
81064 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
81065 sqlite3VdbeAddOp3(v, OP_Null, 0, regSample, regAccum);
81066 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
81067 (char*)&stat3InitFuncdef, P4_FUNCDEF);
81068 sqlite3VdbeChangeP5(v, 2);
81069 #endif /* SQLITE_ENABLE_STAT3 */
81070
81071 /* The block of memory cells initialized here is used as follows.
81072 **
81073 ** iMem:
81074 ** The total number of rows in the table.
81075 **
81076 ** iMem+1 .. iMem+nCol:
81077 ** Number of distinct entries in index considering the
81078 ** left-most N columns only, where N is between 1 and nCol,
81079 ** inclusive.
81080 **
81081 ** iMem+nCol+1 .. Mem+2*nCol:
81082 ** Previous value of indexed columns, from left to right.
81083 **
81084 ** Cells iMem through iMem+nCol are initialized to 0. The others are
81085 ** initialized to contain an SQL NULL.
81086 */
81087 for(i=0; i<=nCol; i++){
81088 sqlite3VdbeAddOp2(v, OP_Integer, 0, iMem+i);
81089 }
81090 for(i=0; i<nCol; i++){
81091 sqlite3VdbeAddOp2(v, OP_Null, 0, iMem+nCol+i+1);
81092 }
81093
81094 /* Start the analysis loop. This loop runs through all the entries in
81095 ** the index b-tree. */
81096 endOfLoop = sqlite3VdbeMakeLabel(v);
81097 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
81098 topOfLoop = sqlite3VdbeCurrentAddr(v);
81099 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
81100
81101 for(i=0; i<nCol; i++){
81102 CollSeq *pColl;
81103 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
81104 if( i==0 ){
81105 /* Always record the very first row */
81106 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
81107 }
81108 assert( pIdx->azColl!=0 );
81109 assert( pIdx->azColl[i]!=0 );
81110 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
81111 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
81112 (char*)pColl, P4_COLLSEQ);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81113 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
81114 VdbeComment((v, "jump if column %d changed", i));
81115 #ifdef SQLITE_ENABLE_STAT3
81116 if( i==0 ){
81117 sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
81118 VdbeComment((v, "incr repeat count"));
81119 }
81120 #endif
81121 }
81122 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
81123 for(i=0; i<nCol; i++){
81124 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
81125 if( i==0 ){
81126 sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
81127 #ifdef SQLITE_ENABLE_STAT3
81128 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81129 (char*)&stat3PushFuncdef, P4_FUNCDEF);
81130 sqlite3VdbeChangeP5(v, 5);
81131 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
81132 sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
81133 sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
81134 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
81135 #endif
81136 }
81137 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
81138 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
81139 }
81140 sqlite3DbFree(db, aChngAddr);
81141
81142 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
81143 sqlite3VdbeResolveLabel(v, endOfLoop);
81144
81145 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
81146 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81147 #ifdef SQLITE_ENABLE_STAT3
81148 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
81149 (char*)&stat3PushFuncdef, P4_FUNCDEF);
81150 sqlite3VdbeChangeP5(v, 5);
81151 sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
81152 shortJump =
81153 sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
81154 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
81155 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81156 sqlite3VdbeChangeP5(v, 2);
81157 sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
81158 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
81159 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
81160 sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
81161 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
81162 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81163 sqlite3VdbeChangeP5(v, 3);
81164 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
81165 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81166 sqlite3VdbeChangeP5(v, 4);
81167 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
81168 (char*)&stat3GetFuncdef, P4_FUNCDEF);
81169 sqlite3VdbeChangeP5(v, 5);
81170 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
81171 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
81172 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
81173 sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
81174 sqlite3VdbeJumpHere(v, shortJump+2);
81175 #endif
81176
81177 /* Store the results in sqlite_stat1.
81178 **
81179 ** The result is a single row of the sqlite_stat1 table. The first
81180 ** two columns are the names of the table and index. The third column
81181 ** is a string composed of a list of integer statistics about the
81182 ** index. The first integer in the list is the total number of entries
81183 ** in the index. There is one additional integer in the list for each
81184 ** column of the table. This additional integer is a guess of how many
81185 ** rows of the table the index will select. If D is the count of distinct
81186 ** values and K is the total number of rows, then the integer is computed
81187 ** as:
81188 **
81189 ** I = (K+D-1)/D
81190 **
81191 ** If K==0 then no entry is made into the sqlite_stat1 table.
81192 ** If K>0 then it is always the case the D>0 so division by zero
81193 ** is never possible.
81194 */
81195 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
81196 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
81197 for(i=0; i<nCol; i++){
81198 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
81199 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81200 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
81201 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
81202 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
81203 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
81204 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
81205 }
81206 if( pIdx->pPartIdxWhere!=0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81207 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81208 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81209 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81210 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81211 if( pIdx->pPartIdxWhere==0 ) sqlite3VdbeJumpHere(v, jZeroRows);
81212 }
81213
81214 /* Create a single sqlite_stat1 entry containing NULL as the index
81215 ** name and the row count as the content.
81216 */
81217 if( pOnlyIdx==0 && needTableCnt ){
81218 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
81219 VdbeComment((v, "%s", pTab->zName));
81220 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
81221 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
81222 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
81223 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
81224 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
81225 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
81226 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
81227 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
81228 sqlite3VdbeJumpHere(v, jZeroRows);
81229 }
81230 if( pParse->nMem<regRec ) pParse->nMem = regRec;
81231 }
81232
81233
81234 /*
81235 ** Generate code that will cause the most recent index analysis to
@@ -81249,20 +82175,22 @@
81249 sqlite3 *db = pParse->db;
81250 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
81251 HashElem *k;
81252 int iStatCur;
81253 int iMem;
 
81254
81255 sqlite3BeginWriteOperation(pParse, 0, iDb);
81256 iStatCur = pParse->nTab;
81257 pParse->nTab += 3;
81258 openStatTable(pParse, iDb, iStatCur, 0, 0);
81259 iMem = pParse->nMem+1;
 
81260 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81261 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
81262 Table *pTab = (Table*)sqliteHashData(k);
81263 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem);
81264 }
81265 loadAnalysis(pParse, iDb);
81266 }
81267
81268 /*
@@ -81283,11 +82211,11 @@
81283 if( pOnlyIdx ){
81284 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
81285 }else{
81286 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
81287 }
81288 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur, pParse->nMem+1);
81289 loadAnalysis(pParse, iDb);
81290 }
81291
81292 /*
81293 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -81365,10 +82293,47 @@
81365 typedef struct analysisInfo analysisInfo;
81366 struct analysisInfo {
81367 sqlite3 *db;
81368 const char *zDatabase;
81369 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81370
81371 /*
81372 ** This callback is invoked once for each index when reading the
81373 ** sqlite_stat1 table.
81374 **
@@ -81381,12 +82346,10 @@
81381 */
81382 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
81383 analysisInfo *pInfo = (analysisInfo*)pData;
81384 Index *pIndex;
81385 Table *pTable;
81386 int i, c, n;
81387 tRowcnt v;
81388 const char *z;
81389
81390 assert( argc==3 );
81391 UNUSED_PARAMETER2(NotUsed, argc);
81392
@@ -81400,45 +82363,35 @@
81400 if( argv[1] ){
81401 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
81402 }else{
81403 pIndex = 0;
81404 }
81405 n = pIndex ? pIndex->nColumn : 0;
81406 z = argv[2];
81407 for(i=0; *z && i<=n; i++){
81408 v = 0;
81409 while( (c=z[0])>='0' && c<='9' ){
81410 v = v*10 + c - '0';
81411 z++;
81412 }
81413 if( i==0 && (pIndex==0 || pIndex->pPartIdxWhere==0) ){
81414 if( v>0 ) pTable->nRowEst = v;
81415 if( pIndex==0 ) break;
81416 }
81417 pIndex->aiRowEst[i] = v;
81418 if( *z==' ' ) z++;
81419 if( strcmp(z, "unordered")==0 ){
81420 pIndex->bUnordered = 1;
81421 break;
81422 }
81423 }
81424 return 0;
81425 }
81426
81427 /*
81428 ** If the Index.aSample variable is not NULL, delete the aSample[] array
81429 ** and its contents.
81430 */
81431 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
81432 #ifdef SQLITE_ENABLE_STAT3
81433 if( pIdx->aSample ){
81434 int j;
81435 for(j=0; j<pIdx->nSample; j++){
81436 IndexSample *p = &pIdx->aSample[j];
81437 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
81438 sqlite3DbFree(db, p->u.z);
81439 }
81440 }
81441 sqlite3DbFree(db, pIdx->aSample);
81442 }
81443 if( db && db->pnBytesFreed==0 ){
81444 pIdx->nSample = 0;
@@ -81445,155 +82398,222 @@
81445 pIdx->aSample = 0;
81446 }
81447 #else
81448 UNUSED_PARAMETER(db);
81449 UNUSED_PARAMETER(pIdx);
81450 #endif
81451 }
81452
81453 #ifdef SQLITE_ENABLE_STAT3
81454 /*
81455 ** Load content from the sqlite_stat3 table into the Index.aSample[]
81456 ** arrays of all indices.
81457 */
81458 static int loadStat3(sqlite3 *db, const char *zDb){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81459 int rc; /* Result codes from subroutines */
81460 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
81461 char *zSql; /* Text of the SQL statement */
81462 Index *pPrevIdx = 0; /* Previous index in the loop */
81463 int idx = 0; /* slot in pIdx->aSample[] for next sample */
81464 int eType; /* Datatype of a sample */
81465 IndexSample *pSample; /* A slot in pIdx->aSample[] */
81466
81467 assert( db->lookaside.bEnabled==0 );
81468 if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
81469 return SQLITE_OK;
81470 }
81471
81472 zSql = sqlite3MPrintf(db,
81473 "SELECT idx,count(*) FROM %Q.sqlite_stat3"
81474 " GROUP BY idx", zDb);
81475 if( !zSql ){
81476 return SQLITE_NOMEM;
81477 }
81478 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
81479 sqlite3DbFree(db, zSql);
81480 if( rc ) return rc;
81481
81482 while( sqlite3_step(pStmt)==SQLITE_ROW ){
 
 
 
81483 char *zIndex; /* Index name */
81484 Index *pIdx; /* Pointer to the index object */
81485 int nSample; /* Number of samples */
 
 
 
81486
81487 zIndex = (char *)sqlite3_column_text(pStmt, 0);
81488 if( zIndex==0 ) continue;
81489 nSample = sqlite3_column_int(pStmt, 1);
81490 pIdx = sqlite3FindIndex(db, zIndex, zDb);
81491 if( pIdx==0 ) continue;
81492 assert( pIdx->nSample==0 );
81493 pIdx->nSample = nSample;
81494 pIdx->aSample = sqlite3DbMallocZero(db, nSample*sizeof(IndexSample));
81495 pIdx->avgEq = pIdx->aiRowEst[1];
 
 
 
 
 
 
 
 
 
81496 if( pIdx->aSample==0 ){
81497 db->mallocFailed = 1;
81498 sqlite3_finalize(pStmt);
81499 return SQLITE_NOMEM;
81500 }
 
 
 
 
 
 
 
 
81501 }
81502 rc = sqlite3_finalize(pStmt);
81503 if( rc ) return rc;
81504
81505 zSql = sqlite3MPrintf(db,
81506 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
81507 if( !zSql ){
81508 return SQLITE_NOMEM;
81509 }
81510 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
81511 sqlite3DbFree(db, zSql);
81512 if( rc ) return rc;
81513
81514 while( sqlite3_step(pStmt)==SQLITE_ROW ){
81515 char *zIndex; /* Index name */
81516 Index *pIdx; /* Pointer to the index object */
81517 int i; /* Loop counter */
81518 tRowcnt sumEq; /* Sum of the nEq values */
81519
81520 zIndex = (char *)sqlite3_column_text(pStmt, 0);
81521 if( zIndex==0 ) continue;
81522 pIdx = sqlite3FindIndex(db, zIndex, zDb);
81523 if( pIdx==0 ) continue;
81524 if( pIdx==pPrevIdx ){
81525 idx++;
81526 }else{
 
 
 
81527 pPrevIdx = pIdx;
81528 idx = 0;
81529 }
81530 assert( idx<pIdx->nSample );
81531 pSample = &pIdx->aSample[idx];
81532 pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
81533 pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
81534 pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
81535 if( idx==pIdx->nSample-1 ){
81536 if( pSample->nDLt>0 ){
81537 for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
81538 pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
81539 }
81540 if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
81541 }
81542 eType = sqlite3_column_type(pStmt, 4);
81543 pSample->eType = (u8)eType;
81544 switch( eType ){
81545 case SQLITE_INTEGER: {
81546 pSample->u.i = sqlite3_column_int64(pStmt, 4);
81547 break;
81548 }
81549 case SQLITE_FLOAT: {
81550 pSample->u.r = sqlite3_column_double(pStmt, 4);
81551 break;
81552 }
81553 case SQLITE_NULL: {
81554 break;
81555 }
81556 default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
81557 const char *z = (const char *)(
81558 (eType==SQLITE_BLOB) ?
81559 sqlite3_column_blob(pStmt, 4):
81560 sqlite3_column_text(pStmt, 4)
81561 );
81562 int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
81563 pSample->nByte = n;
81564 if( n < 1){
81565 pSample->u.z = 0;
81566 }else{
81567 pSample->u.z = sqlite3DbMallocRaw(db, n);
81568 if( pSample->u.z==0 ){
81569 db->mallocFailed = 1;
81570 sqlite3_finalize(pStmt);
81571 return SQLITE_NOMEM;
81572 }
81573 memcpy(pSample->u.z, z, n);
81574 }
81575 }
81576 }
81577 }
81578 return sqlite3_finalize(pStmt);
81579 }
81580 #endif /* SQLITE_ENABLE_STAT3 */
81581
81582 /*
81583 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
81584 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
81585 ** arrays. The contents of sqlite_stat3 are used to populate the
81586 ** Index.aSample[] arrays.
81587 **
81588 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
81589 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
81590 ** during compilation and the sqlite_stat3 table is present, no data is
81591 ** read from it.
81592 **
81593 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
81594 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
81595 ** returned. However, in this case, data is read from the sqlite_stat1
81596 ** table (if it is present) before returning.
81597 **
81598 ** If an OOM error occurs, this function always sets db->mallocFailed.
81599 ** This means if the caller does not care about other errors, the return
@@ -81611,11 +82631,11 @@
81611 /* Clear any prior statistics */
81612 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
81613 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
81614 Index *pIdx = sqliteHashData(i);
81615 sqlite3DefaultRowEst(pIdx);
81616 #ifdef SQLITE_ENABLE_STAT3
81617 sqlite3DeleteIndexSamples(db, pIdx);
81618 pIdx->aSample = 0;
81619 #endif
81620 }
81621
@@ -81635,16 +82655,16 @@
81635 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
81636 sqlite3DbFree(db, zSql);
81637 }
81638
81639
81640 /* Load the statistics from the sqlite_stat3 table. */
81641 #ifdef SQLITE_ENABLE_STAT3
81642 if( rc==SQLITE_OK ){
81643 int lookasideEnabled = db->lookaside.bEnabled;
81644 db->lookaside.bEnabled = 0;
81645 rc = loadStat3(db, sInfo.zDatabase);
81646 db->lookaside.bEnabled = lookasideEnabled;
81647 }
81648 #endif
81649
81650 if( rc==SQLITE_NOMEM ){
@@ -84496,11 +85516,11 @@
84496 const char *zType, /* "idx" or "tbl" */
84497 const char *zName /* Name of index or table */
84498 ){
84499 int i;
84500 const char *zDbName = pParse->db->aDb[iDb].zName;
84501 for(i=1; i<=3; i++){
84502 char zTab[24];
84503 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
84504 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
84505 sqlite3NestedParse(pParse,
84506 "DELETE FROM %Q.%s WHERE %s=%Q",
@@ -89167,10 +90187,13 @@
89167 sqlite3FuncDefInsert(pHash, &aFunc[i]);
89168 }
89169 sqlite3RegisterDateTimeFunctions();
89170 #ifndef SQLITE_OMIT_ALTERTABLE
89171 sqlite3AlterFunctions();
 
 
 
89172 #endif
89173 }
89174
89175 /************** End of func.c ************************************************/
89176 /************** Begin file fkey.c ********************************************/
@@ -94387,11 +95410,11 @@
94387 */
94388 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
94389 Pager *pPager = sqlite3BtreePager(pDb->pBt);
94390 i64 iLimit = -2;
94391 if( zRight ){
94392 sqlite3Atoi64(zRight, &iLimit, 1000000, SQLITE_UTF8);
94393 if( iLimit<-1 ) iLimit = -1;
94394 }
94395 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
94396 returnSingleInt(pParse, "journal_size_limit", iLimit);
94397 }else
@@ -94521,14 +95544,15 @@
94521 ** as little or as much as it wants. Except, if N is set to 0 then the
94522 ** upper layers will never invoke the xFetch interfaces to the VFS.
94523 */
94524 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
94525 sqlite3_int64 sz;
 
94526 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
94527 if( zRight ){
94528 int ii;
94529 sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
94530 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
94531 if( pId2->n==0 ) db->szMmap = sz;
94532 for(ii=db->nDb-1; ii>=0; ii--){
94533 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
94534 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94535,12 +95559,13 @@
94535 }
94536 }
94537 }
94538 sz = -1;
94539 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
94540 #if SQLITE_MAX_MMAP_SIZE==0
94541 sz = 0;
 
94542 #endif
94543 if( rc==SQLITE_OK ){
94544 returnSingleInt(pParse, "mmap_size", sz);
94545 }else if( rc!=SQLITE_NOTFOUND ){
94546 pParse->nErr++;
@@ -102688,11 +103713,11 @@
102688 ** space.
102689 */
102690 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
102691 assert( pTab!=0 );
102692 if( !pTab->pSelect ){
102693 sqlite3_value *pValue;
102694 u8 enc = ENC(sqlite3VdbeDb(v));
102695 Column *pCol = &pTab->aCol[i];
102696 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
102697 assert( i<pTab->nCol );
102698 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104937,13 +105962,13 @@
104937 /*
104938 ** Each instance of this object holds a sequence of WhereLoop objects
104939 ** that implement some or all of a query plan.
104940 **
104941 ** Think of each WhereLoop object as a node in a graph with arcs
104942 ** showing dependences and costs for travelling between nodes. (That is
104943 ** not a completely accurate description because WhereLoop costs are a
104944 ** vector, not a scalar, and because dependences are many-to-one, not
104945 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
104946 ** Then a WherePath object is a path through the graph that visits some
104947 ** or all of the WhereLoop objects once.
104948 **
104949 ** The "solver" works by creating the N best WherePath objects of length
@@ -105038,11 +106063,11 @@
105038 #define TERM_CODED 0x04 /* This term is already coded */
105039 #define TERM_COPIED 0x08 /* Has a child */
105040 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
105041 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
105042 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
105043 #ifdef SQLITE_ENABLE_STAT3
105044 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
105045 #else
105046 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
105047 #endif
105048
@@ -105144,10 +106169,14 @@
105144 WhereInfo *pWInfo; /* Information about this WHERE */
105145 WhereClause *pWC; /* WHERE clause terms */
105146 ExprList *pOrderBy; /* ORDER BY clause */
105147 WhereLoop *pNew; /* Template WhereLoop */
105148 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
 
 
 
 
105149 };
105150
105151 /*
105152 ** The WHERE clause processing routine has two halves. The
105153 ** first part does the start of the WHERE loop and the second
@@ -106543,11 +107572,11 @@
106543 pNewTerm->prereqAll = pTerm->prereqAll;
106544 }
106545 }
106546 #endif /* SQLITE_OMIT_VIRTUALTABLE */
106547
106548 #ifdef SQLITE_ENABLE_STAT3
106549 /* When sqlite_stat3 histogram data is available an operator of the
106550 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
106551 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
106552 ** virtual term of that form.
106553 **
@@ -106583,11 +107612,11 @@
106583 pTerm->nChild = 1;
106584 pTerm->wtFlags |= TERM_COPIED;
106585 pNewTerm->prereqAll = pTerm->prereqAll;
106586 }
106587 }
106588 #endif /* SQLITE_ENABLE_STAT */
106589
106590 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
106591 ** an index for tables to the left of the join.
106592 */
106593 pTerm->prereqRight |= extraRight;
@@ -107151,155 +108180,87 @@
107151 return pParse->nErr;
107152 }
107153 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
107154
107155
107156 #ifdef SQLITE_ENABLE_STAT3
107157 /*
107158 ** Estimate the location of a particular key among all keys in an
107159 ** index. Store the results in aStat as follows:
107160 **
107161 ** aStat[0] Est. number of rows less than pVal
107162 ** aStat[1] Est. number of rows equal to pVal
107163 **
107164 ** Return SQLITE_OK on success.
107165 */
107166 static int whereKeyStats(
107167 Parse *pParse, /* Database connection */
107168 Index *pIdx, /* Index to consider domain of */
107169 sqlite3_value *pVal, /* Value to consider */
107170 int roundUp, /* Round up if true. Round down if false */
107171 tRowcnt *aStat /* OUT: stats written here */
107172 ){
107173 tRowcnt n;
107174 IndexSample *aSample;
107175 int i, eType;
107176 int isEq = 0;
107177 i64 v;
107178 double r, rS;
107179
107180 assert( roundUp==0 || roundUp==1 );
 
 
107181 assert( pIdx->nSample>0 );
107182 if( pVal==0 ) return SQLITE_ERROR;
107183 n = pIdx->aiRowEst[0];
107184 aSample = pIdx->aSample;
107185 eType = sqlite3_value_type(pVal);
107186
107187 if( eType==SQLITE_INTEGER ){
107188 v = sqlite3_value_int64(pVal);
107189 r = (i64)v;
107190 for(i=0; i<pIdx->nSample; i++){
107191 if( aSample[i].eType==SQLITE_NULL ) continue;
107192 if( aSample[i].eType>=SQLITE_TEXT ) break;
107193 if( aSample[i].eType==SQLITE_INTEGER ){
107194 if( aSample[i].u.i>=v ){
107195 isEq = aSample[i].u.i==v;
107196 break;
107197 }
107198 }else{
107199 assert( aSample[i].eType==SQLITE_FLOAT );
107200 if( aSample[i].u.r>=r ){
107201 isEq = aSample[i].u.r==r;
107202 break;
107203 }
107204 }
107205 }
107206 }else if( eType==SQLITE_FLOAT ){
107207 r = sqlite3_value_double(pVal);
107208 for(i=0; i<pIdx->nSample; i++){
107209 if( aSample[i].eType==SQLITE_NULL ) continue;
107210 if( aSample[i].eType>=SQLITE_TEXT ) break;
107211 if( aSample[i].eType==SQLITE_FLOAT ){
107212 rS = aSample[i].u.r;
107213 }else{
107214 rS = aSample[i].u.i;
107215 }
107216 if( rS>=r ){
107217 isEq = rS==r;
107218 break;
107219 }
107220 }
107221 }else if( eType==SQLITE_NULL ){
107222 i = 0;
107223 if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
107224 }else{
107225 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
107226 for(i=0; i<pIdx->nSample; i++){
107227 if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
107228 break;
107229 }
107230 }
107231 if( i<pIdx->nSample ){
107232 sqlite3 *db = pParse->db;
107233 CollSeq *pColl;
107234 const u8 *z;
107235 if( eType==SQLITE_BLOB ){
107236 z = (const u8 *)sqlite3_value_blob(pVal);
107237 pColl = db->pDfltColl;
107238 assert( pColl->enc==SQLITE_UTF8 );
107239 }else{
107240 pColl = sqlite3GetCollSeq(pParse, SQLITE_UTF8, 0, *pIdx->azColl);
107241 /* If the collating sequence was unavailable, we should have failed
107242 ** long ago and never reached this point. But we'll check just to
107243 ** be doubly sure. */
107244 if( NEVER(pColl==0) ) return SQLITE_ERROR;
107245 z = (const u8 *)sqlite3ValueText(pVal, pColl->enc);
107246 if( !z ){
107247 return SQLITE_NOMEM;
107248 }
107249 assert( z && pColl && pColl->xCmp );
107250 }
107251 n = sqlite3ValueBytes(pVal, pColl->enc);
107252
107253 for(; i<pIdx->nSample; i++){
107254 int c;
107255 int eSampletype = aSample[i].eType;
107256 if( eSampletype<eType ) continue;
107257 if( eSampletype!=eType ) break;
107258 #ifndef SQLITE_OMIT_UTF16
107259 if( pColl->enc!=SQLITE_UTF8 ){
107260 int nSample;
107261 char *zSample = sqlite3Utf8to16(
107262 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
107263 );
107264 if( !zSample ){
107265 assert( db->mallocFailed );
107266 return SQLITE_NOMEM;
107267 }
107268 c = pColl->xCmp(pColl->pUser, nSample, zSample, n, z);
107269 sqlite3DbFree(db, zSample);
107270 }else
107271 #endif
107272 {
107273 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
107274 }
107275 if( c>=0 ){
107276 if( c==0 ) isEq = 1;
107277 break;
107278 }
107279 }
107280 }
107281 }
107282
107283 /* At this point, aSample[i] is the first sample that is greater than
107284 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
107285 ** than pVal. If aSample[i]==pVal, then isEq==1.
107286 */
107287 if( isEq ){
107288 assert( i<pIdx->nSample );
107289 aStat[0] = aSample[i].nLt;
107290 aStat[1] = aSample[i].nEq;
107291 }else{
107292 tRowcnt iLower, iUpper, iGap;
107293 if( i==0 ){
107294 iLower = 0;
107295 iUpper = aSample[0].nLt;
107296 }else{
107297 iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
107298 iLower = aSample[i-1].nEq + aSample[i-1].nLt;
107299 }
107300 aStat[1] = pIdx->avgEq;
107301 if( iLower>=iUpper ){
107302 iGap = 0;
107303 }else{
107304 iGap = iUpper - iLower;
107305 }
@@ -107308,48 +108269,12 @@
107308 }else{
107309 iGap = iGap/3;
107310 }
107311 aStat[0] = iLower + iGap;
107312 }
107313 return SQLITE_OK;
107314 }
107315 #endif /* SQLITE_ENABLE_STAT3 */
107316
107317 /*
107318 ** If expression pExpr represents a literal value, set *pp to point to
107319 ** an sqlite3_value structure containing the same value, with affinity
107320 ** aff applied to it, before returning. It is the responsibility of the
107321 ** caller to eventually release this structure by passing it to
107322 ** sqlite3ValueFree().
107323 **
107324 ** If the current parse is a recompile (sqlite3Reprepare()) and pExpr
107325 ** is an SQL variable that currently has a non-NULL value bound to it,
107326 ** create an sqlite3_value structure containing this value, again with
107327 ** affinity aff applied to it, instead.
107328 **
107329 ** If neither of the above apply, set *pp to NULL.
107330 **
107331 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
107332 */
107333 #ifdef SQLITE_ENABLE_STAT3
107334 static int valueFromExpr(
107335 Parse *pParse,
107336 Expr *pExpr,
107337 u8 aff,
107338 sqlite3_value **pp
107339 ){
107340 if( pExpr->op==TK_VARIABLE
107341 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
107342 ){
107343 int iVar = pExpr->iColumn;
107344 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar);
107345 *pp = sqlite3VdbeGetBoundValue(pParse->pReprepare, iVar, aff);
107346 return SQLITE_OK;
107347 }
107348 return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
107349 }
107350 #endif
107351
107352 /*
107353 ** This function is used to estimate the number of rows that will be visited
107354 ** by scanning an index for a range of values. The range may have an upper
107355 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107362,107 +108287,159 @@
107362 ** pLower pUpper
107363 **
107364 ** If either of the upper or lower bound is not present, then NULL is passed in
107365 ** place of the corresponding WhereTerm.
107366 **
107367 ** The nEq parameter is passed the index of the index column subject to the
107368 ** range constraint. Or, equivalently, the number of equality constraints
107369 ** optimized by the proposed index scan. For example, assuming index p is
107370 ** on t1(a, b), and the SQL query is:
107371 **
107372 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
107373 **
107374 ** then nEq should be passed the value 1 (as the range restricted column,
107375 ** b, is the second left-most column of the index). Or, if the query is:
107376 **
107377 ** ... FROM t1 WHERE a > ? AND a < ? ...
107378 **
107379 ** then nEq should be passed 0.
107380 **
107381 ** The returned value is an integer divisor to reduce the estimated
107382 ** search space. A return value of 1 means that range constraints are
107383 ** no help at all. A return value of 2 means range constraints are
107384 ** expected to reduce the search space by half. And so forth...
107385 **
107386 ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
107387 ** reduces the search space by a factor of 4. Hence a single constraint (x>?)
107388 ** results in a return of 4 and a range constraint (x>? AND x<?) results
107389 ** in a return of 16.
 
107390 */
107391 static int whereRangeScanEst(
107392 Parse *pParse, /* Parsing & code generating context */
107393 Index *p, /* The index containing the range-compared column; "x" */
107394 int nEq, /* index into p->aCol[] of the range-compared column */
107395 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
107396 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
107397 WhereCost *pRangeDiv /* OUT: Reduce search space by this divisor */
107398 ){
107399 int rc = SQLITE_OK;
 
107400
107401 #ifdef SQLITE_ENABLE_STAT3
 
 
107402
107403 if( nEq==0 && p->nSample && OptimizationEnabled(pParse->db, SQLITE_Stat3) ){
107404 sqlite3_value *pRangeVal;
107405 tRowcnt iLower = 0;
107406 tRowcnt iUpper = p->aiRowEst[0];
 
 
107407 tRowcnt a[2];
107408 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107410 if( pLower ){
 
107411 Expr *pExpr = pLower->pExpr->pRight;
107412 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
107413 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
107414 if( rc==SQLITE_OK
107415 && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
107416 ){
107417 iLower = a[0];
107418 if( (pLower->eOperator & WO_GT)!=0 ) iLower += a[1];
107419 }
107420 sqlite3ValueFree(pRangeVal);
107421 }
107422 if( rc==SQLITE_OK && pUpper ){
107423 Expr *pExpr = pUpper->pExpr->pRight;
107424 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
 
 
107425 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
107426 if( rc==SQLITE_OK
107427 && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
107428 ){
107429 iUpper = a[0];
107430 if( (pUpper->eOperator & WO_LE)!=0 ) iUpper += a[1];
107431 }
107432 sqlite3ValueFree(pRangeVal);
107433 }
 
 
107434 if( rc==SQLITE_OK ){
107435 WhereCost iBase = whereCost(p->aiRowEst[0]);
107436 if( iUpper>iLower ){
107437 iBase -= whereCost(iUpper - iLower);
 
 
107438 }
107439 *pRangeDiv = iBase;
107440 WHERETRACE(0x100, ("range scan regions: %u..%u div=%d\n",
107441 (u32)iLower, (u32)iUpper, *pRangeDiv));
 
 
 
107442 return SQLITE_OK;
107443 }
107444 }
107445 #else
107446 UNUSED_PARAMETER(pParse);
107447 UNUSED_PARAMETER(p);
107448 UNUSED_PARAMETER(nEq);
107449 #endif
107450 assert( pLower || pUpper );
107451 *pRangeDiv = 0;
107452 /* TUNING: Each inequality constraint reduces the search space 4-fold.
107453 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
107454 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
107455 *pRangeDiv += 20; assert( 20==whereCost(4) );
107456 }
107457 if( pUpper ){
107458 *pRangeDiv += 20; assert( 20==whereCost(4) );
107459 }
 
 
107460 return rc;
107461 }
107462
107463 #ifdef SQLITE_ENABLE_STAT3
107464 /*
107465 ** Estimate the number of rows that will be returned based on
107466 ** an equality constraint x=VALUE and where that VALUE occurs in
107467 ** the histogram data. This only works when x is the left-most
107468 ** column of an index and sqlite_stat3 histogram data is available
@@ -107478,41 +108455,57 @@
107478 ** for a UTF conversion required for comparison. The error is stored
107479 ** in the pParse structure.
107480 */
107481 static int whereEqualScanEst(
107482 Parse *pParse, /* Parsing & code generating context */
107483 Index *p, /* The index whose left-most column is pTerm */
107484 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
107485 tRowcnt *pnRow /* Write the revised row estimate here */
107486 ){
107487 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
 
 
107488 u8 aff; /* Column affinity */
107489 int rc; /* Subfunction return code */
107490 tRowcnt a[2]; /* Statistics */
 
107491
 
 
107492 assert( p->aSample!=0 );
107493 assert( p->nSample>0 );
107494 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
107495 if( pExpr ){
107496 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
107497 if( rc ) goto whereEqualScanEst_cancel;
107498 }else{
107499 pRhs = sqlite3ValueNew(pParse->db);
107500 }
107501 if( pRhs==0 ) return SQLITE_NOTFOUND;
107502 rc = whereKeyStats(pParse, p, pRhs, 0, a);
107503 if( rc==SQLITE_OK ){
107504 WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
107505 *pnRow = a[1];
107506 }
107507 whereEqualScanEst_cancel:
107508 sqlite3ValueFree(pRhs);
 
 
 
 
 
 
 
 
 
 
 
107509 return rc;
107510 }
107511 #endif /* defined(SQLITE_ENABLE_STAT3) */
107512
107513 #ifdef SQLITE_ENABLE_STAT3
107514 /*
107515 ** Estimate the number of rows that will be returned based on
107516 ** an IN constraint where the right-hand side of the IN operator
107517 ** is a list of values. Example:
107518 **
@@ -107527,33 +108520,38 @@
107527 ** for a UTF conversion required for comparison. The error is stored
107528 ** in the pParse structure.
107529 */
107530 static int whereInScanEst(
107531 Parse *pParse, /* Parsing & code generating context */
107532 Index *p, /* The index whose left-most column is pTerm */
107533 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
107534 tRowcnt *pnRow /* Write the revised row estimate here */
107535 ){
 
 
107536 int rc = SQLITE_OK; /* Subfunction return code */
107537 tRowcnt nEst; /* Number of rows for a single term */
107538 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
107539 int i; /* Loop counter */
107540
107541 assert( p->aSample!=0 );
107542 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
107543 nEst = p->aiRowEst[0];
107544 rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
107545 nRowEst += nEst;
 
107546 }
 
107547 if( rc==SQLITE_OK ){
107548 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
107549 *pnRow = nRowEst;
107550 WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
107551 }
 
107552 return rc;
107553 }
107554 #endif /* defined(SQLITE_ENABLE_STAT3) */
107555
107556 /*
107557 ** Disable a term in the WHERE clause. Except, do not disable the term
107558 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
107559 ** or USING clause of that join.
@@ -107787,11 +108785,11 @@
107787 pParse->db->mallocFailed = 1;
107788 }
107789
107790 /* Evaluate the equality constraints
107791 */
107792 assert( zAff==0 || strlen(zAff)>=nEq );
107793 for(j=0; j<nEq; j++){
107794 int r1;
107795 pTerm = pLoop->aLTerm[j];
107796 assert( pTerm!=0 );
107797 /* The following true for indices with redundant columns.
@@ -108956,19 +109954,21 @@
108956 assert( p->rSetup>=pTemplate->rSetup );
108957
108958 if( (p->prereq & pTemplate->prereq)==p->prereq
108959 && p->rSetup<=pTemplate->rSetup
108960 && p->rRun<=pTemplate->rRun
 
108961 ){
108962 /* This branch taken when p is equal or better than pTemplate in
108963 ** all of (1) dependences (2) setup-cost, and (3) run-cost. */
 
108964 assert( p->rSetup==pTemplate->rSetup );
108965 if( p->nLTerm<pTemplate->nLTerm
 
108966 && (p->wsFlags & WHERE_INDEXED)!=0
108967 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
108968 && p->u.btree.pIndex==pTemplate->u.btree.pIndex
108969 && p->prereq==pTemplate->prereq
108970 ){
108971 /* Overwrite an existing WhereLoop with an similar one that uses
108972 ** more terms of the index */
108973 pNext = p->pNextLoop;
108974 break;
@@ -108978,15 +109978,17 @@
108978 goto whereLoopInsert_noop;
108979 }
108980 }
108981 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
108982 && p->rRun>=pTemplate->rRun
 
108983 && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */
108984 ){
108985 /* Overwrite an existing WhereLoop with a better one: one that is
108986 ** better at one of (1) dependences, (2) setup-cost, or (3) run-cost
108987 ** and is no worse in any of those categories. */
 
108988 pNext = p->pNextLoop;
108989 break;
108990 }
108991 }
108992
@@ -109094,16 +110096,22 @@
109094 saved_nOut = pNew->nOut;
109095 pNew->rSetup = 0;
109096 rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
109097 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
109098 int nIn = 0;
109099 if( pTerm->prereqRight & pNew->maskSelf ) continue;
 
 
109100 if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
109101 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
109102 ){
109103 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
109104 }
 
 
 
 
109105 pNew->wsFlags = saved_wsFlags;
109106 pNew->u.btree.nEq = saved_nEq;
109107 pNew->nLTerm = saved_nLTerm;
109108 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
109109 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109156,29 +110164,34 @@
109156 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
109157 pNew->aLTerm[pNew->nLTerm-2] : 0;
109158 }
109159 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
109160 /* Adjust nOut and rRun for STAT3 range values */
109161 WhereCost rDiv;
109162 whereRangeScanEst(pParse, pProbe, pNew->u.btree.nEq,
109163 pBtm, pTop, &rDiv);
109164 pNew->nOut = saved_nOut>rDiv+10 ? saved_nOut - rDiv : 10;
109165 }
109166 #ifdef SQLITE_ENABLE_STAT3
109167 if( pNew->u.btree.nEq==1 && pProbe->nSample
109168 && OptimizationEnabled(db, SQLITE_Stat3) ){
 
 
109169 tRowcnt nOut = 0;
109170 if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
109171 testcase( pTerm->eOperator & WO_EQ );
109172 testcase( pTerm->eOperator & WO_ISNULL );
109173 rc = whereEqualScanEst(pParse, pProbe, pTerm->pExpr->pRight, &nOut);
109174 }else if( (pTerm->eOperator & WO_IN)
109175 && !ExprHasProperty(pTerm->pExpr, EP_xIsSelect) ){
109176 rc = whereInScanEst(pParse, pProbe, pTerm->pExpr->x.pList, &nOut);
109177 }
109178 assert( nOut==0 || rc==SQLITE_OK );
109179 if( nOut ) pNew->nOut = whereCost(nOut);
 
 
 
109180 }
109181 #endif
109182 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
109183 /* Each row involves a step of the index, then a binary search of
109184 ** the main table */
@@ -109191,10 +110204,14 @@
109191 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
109192 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
109193 ){
109194 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
109195 }
 
 
 
 
109196 }
109197 pNew->prereq = saved_prereq;
109198 pNew->u.btree.nEq = saved_nEq;
109199 pNew->wsFlags = saved_wsFlags;
109200 pNew->nOut = saved_nOut;
@@ -109420,11 +110437,17 @@
109420 }
109421 rc = whereLoopInsert(pBuilder, pNew);
109422 if( rc ) break;
109423 }
109424 }
 
109425 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
 
 
 
 
 
109426
109427 /* If there was an INDEXED BY clause, then only that one index is
109428 ** considered. */
109429 if( pSrc->pIndex ) break;
109430 }
@@ -110666,11 +111689,11 @@
110666 if( pWInfo->nLevel>=2
110667 && pResultSet!=0
110668 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
110669 ){
110670 Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
110671 if( pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, pOrderBy);
110672 while( pWInfo->nLevel>=2 ){
110673 WhereTerm *pTerm, *pEnd;
110674 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
110675 if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
110676 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -116698,18 +117721,20 @@
116698 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
116699 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
116700 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
116701 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
116702 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
 
116703 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
116704 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
116705 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
116706 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
116707 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
116708 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
116709 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
116710 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
 
116711 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
116712 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
116713 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
116714 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
116715 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
116716
--- 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-09-04 04:04:08 8df95bb0b3f72222cf262174247a467c234f9939"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1026,16 +1026,18 @@
1026 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1027 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1028 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1029 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1030 #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
1031 #define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
1032 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1033 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1034 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
1035 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1036 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1037 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
1038 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
1039 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
1040 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
1041 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
1042 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
1043 #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
@@ -8368,10 +8370,24 @@
8370 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8371 # undef SQLITE_DEFAULT_MMAP_SIZE
8372 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8373 #endif
8374
8375 /*
8376 ** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8377 ** Priority is given to SQLITE_ENABLE_STAT4. If either are defined, also
8378 ** define SQLITE_ENABLE_STAT3_OR_STAT4
8379 */
8380 #ifdef SQLITE_ENABLE_STAT4
8381 # undef SQLITE_ENABLE_STAT3
8382 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8383 #elif SQLITE_ENABLE_STAT3
8384 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8385 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8386 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8387 #endif
8388
8389 /*
8390 ** An instance of the following structure is used to store the busy-handler
8391 ** callback for a given sqlite handle.
8392 **
8393 ** The sqlite.busyHandler member of the sqlite struct contains the busy
@@ -10770,13 +10786,14 @@
10786 u16 nColumn; /* Number of columns in table used by this index */
10787 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10788 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10789 unsigned bUnordered:1; /* Use this index for == or IN queries only */
10790 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
10791 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
10792 int nSample; /* Number of elements in aSample[] */
10793 int nSampleCol; /* Size of IndexSample.anEq[] and so on */
10794 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
10795 IndexSample *aSample; /* Samples of the left-most key */
10796 #endif
10797 };
10798
10799 /*
@@ -10783,20 +10800,15 @@
10800 ** Each sample stored in the sqlite_stat3 table is represented in memory
10801 ** using a structure of this type. See documentation at the top of the
10802 ** analyze.c source file for additional information.
10803 */
10804 struct IndexSample {
10805 void *p; /* Pointer to sampled record */
10806 int n; /* Size of record in bytes */
10807 tRowcnt *anEq; /* Est. number of rows where the key equals this sample */
10808 tRowcnt *anLt; /* Est. number of rows where key is less than this sample */
10809 tRowcnt *anDLt; /* Est. number of distinct keys less than this sample */
 
 
 
 
 
10810 };
10811
10812 /*
10813 ** Each token coming out of the lexer is an instance of
10814 ** this structure. Tokens are also used as part of an expression.
@@ -12264,13 +12276,10 @@
12276 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12277 void(*)(void*));
12278 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12279 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12280 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
 
 
 
12281 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12282 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12283 #ifndef SQLITE_AMALGAMATION
12284 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12285 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
@@ -12332,10 +12341,16 @@
12341 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
12342 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12343
12344 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12345 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12346
12347 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12348 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12349 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12350 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12351 #endif
12352
12353 /*
12354 ** The interface to the LEMON-generated parser
12355 */
12356 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
@@ -12916,11 +12931,13 @@
12931 "ENABLE_OVERSIZE_CELL_CHECK",
12932 #endif
12933 #ifdef SQLITE_ENABLE_RTREE
12934 "ENABLE_RTREE",
12935 #endif
12936 #if defined(SQLITE_ENABLE_STAT4)
12937 "ENABLE_STAT4",
12938 #elif defined(SQLITE_ENABLE_STAT3)
12939 "ENABLE_STAT3",
12940 #endif
12941 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12942 "ENABLE_UNLOCK_NOTIFY",
12943 #endif
@@ -16075,11 +16092,11 @@
16092 struct MemBlockHdr *pHdr;
16093 if( !p ){
16094 return 0;
16095 }
16096 pHdr = sqlite3MemsysGetHeader(p);
16097 return (int)pHdr->iSize;
16098 }
16099
16100 /*
16101 ** Initialize the memory allocation subsystem.
16102 */
@@ -16117,19 +16134,19 @@
16134 static void randomFill(char *pBuf, int nByte){
16135 unsigned int x, y, r;
16136 x = SQLITE_PTR_TO_INT(pBuf);
16137 y = nByte | 1;
16138 while( nByte >= 4 ){
16139 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16140 y = y*1103515245 + 12345;
16141 r = x ^ y;
16142 *(int*)pBuf = r;
16143 pBuf += 4;
16144 nByte -= 4;
16145 }
16146 while( nByte-- > 0 ){
16147 x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16148 y = y*1103515245 + 12345;
16149 r = x ^ y;
16150 *(pBuf++) = r & 0xff;
16151 }
16152 }
@@ -16220,13 +16237,13 @@
16237 assert( mem.pLast==pHdr );
16238 mem.pLast = pHdr->pPrev;
16239 }
16240 z = (char*)pBt;
16241 z -= pHdr->nTitle;
16242 adjustStats((int)pHdr->iSize, -1);
16243 randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16244 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
16245 free(z);
16246 sqlite3_mutex_leave(mem.mutex);
16247 }
16248
16249 /*
@@ -16244,13 +16261,13 @@
16261 assert( mem.disallow==0 );
16262 assert( (nByte & 7)==0 ); /* EV: R-46199-30249 */
16263 pOldHdr = sqlite3MemsysGetHeader(pPrior);
16264 pNew = sqlite3MemMalloc(nByte);
16265 if( pNew ){
16266 memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
16267 if( nByte>pOldHdr->iSize ){
16268 randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
16269 }
16270 sqlite3MemFree(pPrior);
16271 }
16272 return pNew;
16273 }
@@ -16361,11 +16378,11 @@
16378 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16379 struct MemBlockHdr *pHdr;
16380 for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16381 void **pBt = (void**)pHdr;
16382 pBt -= pHdr->nBacktraceSlots;
16383 mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16384 }
16385 }
16386
16387 /*
16388 ** Open the file indicated and write a log of all unfreed memory
@@ -18483,11 +18500,11 @@
18500 GetVersionEx(&sInfo);
18501 osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18502 }
18503 return osType==2;
18504 }
18505 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
18506 #endif
18507
18508 #ifdef SQLITE_DEBUG
18509 /*
18510 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
@@ -18521,11 +18538,11 @@
18538 /* As winMutexInit() and winMutexEnd() are called as part
18539 ** of the sqlite3_initialize and sqlite3_shutdown()
18540 ** processing, the "interlocked" magic is probably not
18541 ** strictly necessary.
18542 */
18543 static LONG winMutex_lock = 0;
18544
18545 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18546
18547 static int winMutexInit(void){
18548 /* The first to increment to 1 does actual initialization */
@@ -21082,36 +21099,10 @@
21099 assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21100 assert( m.z || db->mallocFailed );
21101 return m.z;
21102 }
21103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21104 /*
21105 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21106 ** Return the number of bytes in the first nChar unicode characters
21107 ** in pZ. nChar must be non-negative.
21108 */
@@ -23064,15 +23055,17 @@
23055 void *lockingContext; /* Locking style specific state */
23056 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
23057 const char *zPath; /* Name of the file */
23058 unixShm *pShm; /* Shared memory segment information */
23059 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23060 #if SQLITE_MAX_MMAP_SIZE>0
23061 int nFetchOut; /* Number of outstanding xFetch refs */
23062 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23063 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23064 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23065 void *pMapRegion; /* Memory mapped region */
23066 #endif
23067 #ifdef __QNXNTO__
23068 int sectorSize; /* Device sector size */
23069 int deviceCharacteristics; /* Precomputed device characteristics */
23070 #endif
23071 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -23503,10 +23496,11 @@
23496 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23497
23498 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23499 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23500
23501 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23502 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23503 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23504
23505 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23506 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
@@ -23515,10 +23509,11 @@
23509 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23510 #else
23511 { "mremap", (sqlite3_syscall_ptr)0, 0 },
23512 #endif
23513 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23514 #endif
23515
23516 }; /* End of the overrideable system calls */
23517
23518 /*
23519 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
@@ -23601,10 +23596,19 @@
23596 if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
23597 }
23598 return 0;
23599 }
23600
23601 /*
23602 ** Do not accept any file descriptor less than this value, in order to avoid
23603 ** opening database file using file descriptors that are commonly used for
23604 ** standard input, output, and error.
23605 */
23606 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
23607 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
23608 #endif
23609
23610 /*
23611 ** Invoke open(). Do so multiple times, until it either succeeds or
23612 ** fails for some reason other than EINTR.
23613 **
23614 ** If the file creation mode "m" is 0 then set it to the default for
@@ -23621,17 +23625,27 @@
23625 ** recover the hot journals.
23626 */
23627 static int robust_open(const char *z, int f, mode_t m){
23628 int fd;
23629 mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
23630 while(1){
23631 #if defined(O_CLOEXEC)
23632 fd = osOpen(z,f|O_CLOEXEC,m2);
23633 #else
23634 fd = osOpen(z,f,m2);
23635 #endif
23636 if( fd<0 ){
23637 if( errno==EINTR ) continue;
23638 break;
23639 }
23640 if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
23641 osClose(fd);
23642 sqlite3_log(SQLITE_WARNING,
23643 "attempt to open \"%s\" as file descriptor %d", z, fd);
23644 fd = -1;
23645 if( osOpen("/dev/null", f, m)<0 ) break;
23646 }
23647 if( fd>=0 ){
23648 if( m!=0 ){
23649 struct stat statbuf;
23650 if( osFstat(fd, &statbuf)==0
23651 && statbuf.st_size==0
@@ -24925,12 +24939,14 @@
24939 static int unixUnlock(sqlite3_file *id, int eFileLock){
24940 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
24941 return posixUnlock(id, eFileLock, 0);
24942 }
24943
24944 #if SQLITE_MAX_MMAP_SIZE>0
24945 static int unixMapfile(unixFile *pFd, i64 nByte);
24946 static void unixUnmapfile(unixFile *pFd);
24947 #endif
24948
24949 /*
24950 ** This function performs the parts of the "close file" operation
24951 ** common to all locking schemes. It closes the directory and file
24952 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24940,11 +24956,13 @@
24956 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24957 ** vxworksReleaseFileId() routine.
24958 */
24959 static int closeUnixFile(sqlite3_file *id){
24960 unixFile *pFile = (unixFile*)id;
24961 #if SQLITE_MAX_MMAP_SIZE>0
24962 unixUnmapfile(pFile);
24963 #endif
24964 if( pFile->h>=0 ){
24965 robust_close(pFile, pFile->h, __LINE__);
24966 pFile->h = -1;
24967 }
24968 #if OS_VXWORKS
@@ -26145,10 +26163,11 @@
26163 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26164 i64 newOffset;
26165 #endif
26166 TIMER_START;
26167 assert( cnt==(cnt&0x1ffff) );
26168 assert( id->h>2 );
26169 cnt &= 0x1ffff;
26170 do{
26171 #if defined(USE_PREAD)
26172 got = osPread(id->h, pBuf, cnt, offset);
26173 SimulateIOError( got = -1 );
@@ -26259,10 +26278,11 @@
26278 int *piErrno /* OUT: Error number if error occurs */
26279 ){
26280 int rc = 0; /* Value returned by system call */
26281
26282 assert( nBuf==(nBuf&0x1ffff) );
26283 assert( fd>2 );
26284 nBuf &= 0x1ffff;
26285 TIMER_START;
26286
26287 #if defined(USE_PREAD)
26288 do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
@@ -26644,17 +26664,19 @@
26664 if( pFile->inNormalWrite && nByte==0 ){
26665 pFile->transCntrChng = 1;
26666 }
26667 #endif
26668
26669 #if SQLITE_MAX_MMAP_SIZE>0
26670 /* If the file was just truncated to a size smaller than the currently
26671 ** mapped region, reduce the effective mapping size as well. SQLite will
26672 ** use read() and write() to access data beyond this point from now on.
26673 */
26674 if( nByte<pFile->mmapSize ){
26675 pFile->mmapSize = nByte;
26676 }
26677 #endif
26678
26679 return SQLITE_OK;
26680 }
26681 }
26682
@@ -26740,10 +26762,11 @@
26762 }
26763 #endif
26764 }
26765 }
26766
26767 #if SQLITE_MAX_MMAP_SIZE>0
26768 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26769 int rc;
26770 if( pFile->szChunk<=0 ){
26771 if( robust_ftruncate(pFile->h, nByte) ){
26772 pFile->lastErrno = errno;
@@ -26752,10 +26775,11 @@
26775 }
26776
26777 rc = unixMapfile(pFile, nByte);
26778 return rc;
26779 }
26780 #endif
26781
26782 return SQLITE_OK;
26783 }
26784
26785 /*
@@ -26820,10 +26844,11 @@
26844 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26845 *(char**)pArg = zTFile;
26846 }
26847 return SQLITE_OK;
26848 }
26849 #if SQLITE_MAX_MMAP_SIZE>0
26850 case SQLITE_FCNTL_MMAP_SIZE: {
26851 i64 newLimit = *(i64*)pArg;
26852 int rc = SQLITE_OK;
26853 if( newLimit>sqlite3GlobalConfig.mxMmap ){
26854 newLimit = sqlite3GlobalConfig.mxMmap;
@@ -26836,10 +26861,11 @@
26861 rc = unixMapfile(pFile, -1);
26862 }
26863 }
26864 return rc;
26865 }
26866 #endif
26867 #ifdef SQLITE_DEBUG
26868 /* The pager calls this method to signal that it has done
26869 ** a rollback and that the database is therefore unchanged and
26870 ** it hence it is OK for the transaction change counter to be
26871 ** unchanged.
@@ -27646,26 +27672,24 @@
27672 # define unixShmLock 0
27673 # define unixShmBarrier 0
27674 # define unixShmUnmap 0
27675 #endif /* #ifndef SQLITE_OMIT_WAL */
27676
27677 #if SQLITE_MAX_MMAP_SIZE>0
27678 /*
27679 ** If it is currently memory mapped, unmap file pFd.
27680 */
27681 static void unixUnmapfile(unixFile *pFd){
27682 assert( pFd->nFetchOut==0 );
 
27683 if( pFd->pMapRegion ){
27684 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27685 pFd->pMapRegion = 0;
27686 pFd->mmapSize = 0;
27687 pFd->mmapSizeActual = 0;
27688 }
 
27689 }
27690
 
27691 /*
27692 ** Return the system page size.
27693 */
27694 static int unixGetPagesize(void){
27695 #if HAVE_MREMAP
@@ -27674,13 +27698,11 @@
27698 return getpagesize();
27699 #else
27700 return (int)sysconf(_SC_PAGESIZE);
27701 #endif
27702 }
 
27703
 
27704 /*
27705 ** Attempt to set the size of the memory mapping maintained by file
27706 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27707 **
27708 ** If successful, this function sets the following variables:
@@ -27761,11 +27783,10 @@
27783 pFd->mmapSizeMax = 0;
27784 }
27785 pFd->pMapRegion = (void *)pNew;
27786 pFd->mmapSize = pFd->mmapSizeActual = nNew;
27787 }
 
27788
27789 /*
27790 ** Memory map or remap the file opened by file-descriptor pFd (if the file
27791 ** is already mapped, the existing mapping is replaced by the new). Or, if
27792 ** there already exists a mapping for this file, and there are still
@@ -27780,11 +27801,10 @@
27801 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
27802 ** recreated as a result of outstanding references) or an SQLite error
27803 ** code otherwise.
27804 */
27805 static int unixMapfile(unixFile *pFd, i64 nByte){
 
27806 i64 nMap = nByte;
27807 int rc;
27808
27809 assert( nMap>=0 || pFd->nFetchOut==0 );
27810 if( pFd->nFetchOut>0 ) return SQLITE_OK;
@@ -27806,14 +27826,14 @@
27826 unixRemapfile(pFd, nMap);
27827 }else{
27828 unixUnmapfile(pFd);
27829 }
27830 }
 
27831
27832 return SQLITE_OK;
27833 }
27834 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
27835
27836 /*
27837 ** If possible, return a pointer to a mapping of file fd starting at offset
27838 ** iOff. The mapping must be valid for at least nAmt bytes.
27839 **
@@ -27858,10 +27878,11 @@
27878 */
27879 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27880 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27881 UNUSED_PARAMETER(iOff);
27882
27883 #if SQLITE_MAX_MMAP_SIZE>0
27884 /* If p==0 (unmap the entire file) then there must be no outstanding
27885 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27886 ** then there must be at least one outstanding. */
27887 assert( (p==0)==(pFd->nFetchOut==0) );
27888
@@ -27873,10 +27894,11 @@
27894 }else{
27895 unixUnmapfile(pFd);
27896 }
27897
27898 assert( pFd->nFetchOut>=0 );
27899 #endif
27900 return SQLITE_OK;
27901 }
27902
27903 /*
27904 ** Here ends the implementation of all sqlite3_file methods.
@@ -28204,11 +28226,13 @@
28226 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28227 pNew->h = h;
28228 pNew->pVfs = pVfs;
28229 pNew->zPath = zFilename;
28230 pNew->ctrlFlags = (u8)ctrlFlags;
28231 #if SQLITE_MAX_MMAP_SIZE>0
28232 pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28233 #endif
28234 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28235 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28236 pNew->ctrlFlags |= UNIXFILE_PSOW;
28237 }
28238 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -28360,10 +28384,11 @@
28384 ** If no suitable temporary file directory can be found, return NULL.
28385 */
28386 static const char *unixTempFileDir(void){
28387 static const char *azDirs[] = {
28388 0,
28389 0,
28390 0,
28391 "/var/tmp",
28392 "/usr/tmp",
28393 "/tmp",
28394 0 /* List terminator */
@@ -28371,11 +28396,12 @@
28396 unsigned int i;
28397 struct stat buf;
28398 const char *zDir = 0;
28399
28400 azDirs[0] = sqlite3_temp_directory;
28401 if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
28402 if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
28403 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
28404 if( zDir==0 ) continue;
28405 if( osStat(zDir, &buf) ) continue;
28406 if( !S_ISDIR(buf.st_mode) ) continue;
28407 if( osAccess(zDir, 07) ) continue;
@@ -30484,11 +30510,11 @@
30510 */
30511 #if SQLITE_OS_WIN /* This file is used for Windows only */
30512
30513 #ifdef __CYGWIN__
30514 # include <sys/cygwin.h>
30515 # include <errno.h> /* amalgamator: keep */
30516 #endif
30517
30518 /*
30519 ** Include code that is common to all os_*.c files
30520 */
@@ -30705,30 +30731,99 @@
30731 /*
30732 ** Compiling and using WAL mode requires several APIs that are only
30733 ** available in Windows platforms based on the NT kernel.
30734 */
30735 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
30736 # error "WAL mode requires support from the Windows NT kernel, compile\
30737 with SQLITE_OMIT_WAL."
30738 #endif
30739
30740 /*
30741 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
30742 ** based on the sub-platform)?
30743 */
30744 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
30745 # define SQLITE_WIN32_HAS_ANSI
30746 #endif
30747
30748 /*
30749 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
30750 ** based on the sub-platform)?
30751 */
30752 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
30753 !defined(SQLITE_WIN32_NO_WIDE)
30754 # define SQLITE_WIN32_HAS_WIDE
30755 #endif
30756
30757 /*
30758 ** Make sure at least one set of Win32 APIs is available.
30759 */
30760 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
30761 # error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
30762 must be defined."
30763 #endif
30764
30765 /*
30766 ** Maximum pathname length (in chars) for Win32. This should normally be
30767 ** MAX_PATH.
30768 */
30769 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
30770 # define SQLITE_WIN32_MAX_PATH_CHARS (MAX_PATH)
30771 #endif
30772
30773 /*
30774 ** Maximum pathname length (in chars) for WinNT. This should normally be
30775 ** 32767.
30776 */
30777 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
30778 # define SQLITE_WINNT_MAX_PATH_CHARS (32767)
30779 #endif
30780
30781 /*
30782 ** Maximum pathname length (in bytes) for Win32. The MAX_PATH macro is in
30783 ** characters, so we allocate 3 bytes per character assuming worst-case of
30784 ** 4-bytes-per-character for UTF8.
30785 */
30786 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
30787 # define SQLITE_WIN32_MAX_PATH_BYTES (SQLITE_WIN32_MAX_PATH_CHARS*4)
30788 #endif
30789
30790 /*
30791 ** Maximum pathname length (in bytes) for WinNT. This should normally be
30792 ** 32767 * sizeof(WCHAR).
30793 */
30794 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
30795 # define SQLITE_WINNT_MAX_PATH_BYTES \
30796 (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
30797 #endif
30798
30799 /*
30800 ** Maximum error message length (in chars) for WinRT.
30801 */
30802 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
30803 # define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
30804 #endif
30805
30806 /*
30807 ** Returns non-zero if the character should be treated as a directory
30808 ** separator.
30809 */
30810 #ifndef winIsDirSep
30811 # define winIsDirSep(a) (((a) == '/') || ((a) == '\\'))
30812 #endif
30813
30814 /*
30815 ** Returns the string that should be used as the directory separator.
30816 */
30817 #ifndef winGetDirDep
30818 # ifdef __CYGWIN__
30819 # define winGetDirDep() "/"
30820 # else
30821 # define winGetDirDep() "\\"
30822 # endif
30823 #endif
30824
30825 /*
30826 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
30827 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
30828 ** are not present in the header file)?
30829 */
@@ -30776,11 +30871,11 @@
30871 #ifndef FILE_ATTRIBUTE_MASK
30872 # define FILE_ATTRIBUTE_MASK (0x0003FFF7)
30873 #endif
30874
30875 #ifndef SQLITE_OMIT_WAL
30876 /* Forward references to structures used for WAL */
30877 typedef struct winShm winShm; /* A connection to shared-memory */
30878 typedef struct winShmNode winShmNode; /* A region of shared-memory */
30879 #endif
30880
30881 /*
@@ -31731,16 +31826,16 @@
31826 ** API as long as we don't call it when running Win95/98/ME. A call to
31827 ** this routine is used to determine if the host is Win95/98/ME or
31828 ** WinNT/2K/XP so that we will know whether or not we can safely call
31829 ** the LockFileEx() API.
31830 */
31831 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
31832 # define osIsNT() (1)
31833 #elif !defined(SQLITE_WIN32_HAS_WIDE)
31834 # define osIsNT() (0)
31835 #else
31836 static int osIsNT(void){
31837 if( sqlite3_os_type==0 ){
31838 OSVERSIONINFOA sInfo;
31839 sInfo.dwOSVersionInfoSize = sizeof(sInfo);
31840 osGetVersionExA(&sInfo);
31841 sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
@@ -31947,11 +32042,11 @@
32042 /*
32043 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
32044 **
32045 ** Space to hold the returned string is obtained from malloc.
32046 */
32047 static LPWSTR winUtf8ToUnicode(const char *zFilename){
32048 int nChar;
32049 LPWSTR zWideFilename;
32050
32051 nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32052 if( nChar==0 ){
@@ -31972,11 +32067,11 @@
32067
32068 /*
32069 ** Convert Microsoft Unicode to UTF-8. Space to hold the returned string is
32070 ** obtained from sqlite3_malloc().
32071 */
32072 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
32073 int nByte;
32074 char *zFilename;
32075
32076 nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32077 if( nByte == 0 ){
@@ -32000,11 +32095,11 @@
32095 ** current codepage settings for file apis.
32096 **
32097 ** Space to hold the returned string is obtained
32098 ** from sqlite3_malloc.
32099 */
32100 static LPWSTR winMbcsToUnicode(const char *zFilename){
32101 int nByte;
32102 LPWSTR zMbcsFilename;
32103 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32104
32105 nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
@@ -32030,11 +32125,11 @@
32125 ** user's ANSI codepage.
32126 **
32127 ** Space to hold the returned string is obtained from
32128 ** sqlite3_malloc().
32129 */
32130 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
32131 int nByte;
32132 char *zFilename;
32133 int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32134
32135 nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
@@ -32060,15 +32155,15 @@
32155 */
32156 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32157 char *zFilenameUtf8;
32158 LPWSTR zTmpWide;
32159
32160 zTmpWide = winMbcsToUnicode(zFilename);
32161 if( zTmpWide==0 ){
32162 return 0;
32163 }
32164 zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
32165 sqlite3_free(zTmpWide);
32166 return zFilenameUtf8;
32167 }
32168
32169 /*
@@ -32077,15 +32172,15 @@
32172 */
32173 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32174 char *zFilenameMbcs;
32175 LPWSTR zTmpWide;
32176
32177 zTmpWide = winUtf8ToUnicode(zFilename);
32178 if( zTmpWide==0 ){
32179 return 0;
32180 }
32181 zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
32182 sqlite3_free(zTmpWide);
32183 return zFilenameMbcs;
32184 }
32185
32186 /*
@@ -32111,11 +32206,11 @@
32206 );
32207 assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
32208 if( ppDirectory ){
32209 char *zValueUtf8 = 0;
32210 if( zValue && zValue[0] ){
32211 zValueUtf8 = winUnicodeToUtf8(zValue);
32212 if ( zValueUtf8==0 ){
32213 return SQLITE_NOMEM;
32214 }
32215 }
32216 sqlite3_free(*ppDirectory);
@@ -32124,32 +32219,32 @@
32219 }
32220 return SQLITE_ERROR;
32221 }
32222
32223 /*
32224 ** The return value of winGetLastErrorMsg
32225 ** is zero if the error message fits in the buffer, or non-zero
32226 ** otherwise (if the message was truncated).
32227 */
32228 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32229 /* FormatMessage returns 0 on failure. Otherwise it
32230 ** returns the number of TCHARs written to the output
32231 ** buffer, excluding the terminating null char.
32232 */
32233 DWORD dwLen = 0;
32234 char *zOut = 0;
32235
32236 if( osIsNT() ){
32237 #if SQLITE_OS_WINRT
32238 WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
32239 dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
32240 FORMAT_MESSAGE_IGNORE_INSERTS,
32241 NULL,
32242 lastErrno,
32243 0,
32244 zTempWide,
32245 SQLITE_WIN32_MAX_ERRMSG_CHARS,
32246 0);
32247 #else
32248 LPWSTR zTempWide = NULL;
32249 dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
32250 FORMAT_MESSAGE_FROM_SYSTEM |
@@ -32162,11 +32257,11 @@
32257 0);
32258 #endif
32259 if( dwLen > 0 ){
32260 /* allocate a buffer and convert to UTF8 */
32261 sqlite3BeginBenignMalloc();
32262 zOut = winUnicodeToUtf8(zTempWide);
32263 sqlite3EndBenignMalloc();
32264 #if !SQLITE_OS_WINRT
32265 /* free the system buffer allocated by FormatMessage */
32266 osLocalFree(zTempWide);
32267 #endif
@@ -32230,11 +32325,11 @@
32325 ){
32326 char zMsg[500]; /* Human readable error text */
32327 int i; /* Loop counter */
32328
32329 zMsg[0] = 0;
32330 winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
32331 assert( errcode!=SQLITE_OK );
32332 if( zPath==0 ) zPath = "";
32333 for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
32334 zMsg[i] = 0;
32335 sqlite3_log(errcode,
@@ -32255,30 +32350,30 @@
32350 # define SQLITE_WIN32_IOERR_RETRY 10
32351 #endif
32352 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
32353 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
32354 #endif
32355 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
32356 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
32357
32358 /*
32359 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
32360 ** to see if it should be retried. Return TRUE to retry. Return FALSE
32361 ** to give up with an error.
32362 */
32363 static int winRetryIoerr(int *pnRetry, DWORD *pError){
32364 DWORD e = osGetLastError();
32365 if( *pnRetry>=winIoerrRetry ){
32366 if( pError ){
32367 *pError = e;
32368 }
32369 return 0;
32370 }
32371 if( e==ERROR_ACCESS_DENIED ||
32372 e==ERROR_LOCK_VIOLATION ||
32373 e==ERROR_SHARING_VIOLATION ){
32374 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
32375 ++*pnRetry;
32376 return 1;
32377 }
32378 if( pError ){
32379 *pError = e;
@@ -32287,15 +32382,15 @@
32382 }
32383
32384 /*
32385 ** Log a I/O error retry episode.
32386 */
32387 static void winLogIoerr(int nRetry){
32388 if( nRetry ){
32389 sqlite3_log(SQLITE_IOERR,
32390 "delayed %dms for lock/sharing conflict",
32391 winIoerrRetryDelay*nRetry*(nRetry+1)/2
32392 );
32393 }
32394 }
32395
32396 #if SQLITE_OS_WINCE
@@ -32356,11 +32451,11 @@
32451 LPWSTR zName;
32452 DWORD lastErrno;
32453 BOOL bLogged = FALSE;
32454 BOOL bInit = TRUE;
32455
32456 zName = winUtf8ToUnicode(zFilename);
32457 if( zName==0 ){
32458 /* out of memory */
32459 return SQLITE_IOERR_NOMEM;
32460 }
32461
@@ -32376,14 +32471,13 @@
32471
32472 /* Create/open the named mutex */
32473 pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
32474 if (!pFile->hMutex){
32475 pFile->lastErrno = osGetLastError();
 
 
32476 sqlite3_free(zName);
32477 return winLogError(SQLITE_IOERR, pFile->lastErrno,
32478 "winceCreateLock1", zFilename);
32479 }
32480
32481 /* Acquire the mutex before continuing */
32482 winceMutexAcquire(pFile->hMutex);
32483
@@ -32629,11 +32723,11 @@
32723 ** API LockFile.
32724 */
32725 return winceLockFile(phFile, offsetLow, offsetHigh,
32726 numBytesLow, numBytesHigh);
32727 #else
32728 if( osIsNT() ){
32729 OVERLAPPED ovlp;
32730 memset(&ovlp, 0, sizeof(OVERLAPPED));
32731 ovlp.Offset = offsetLow;
32732 ovlp.OffsetHigh = offsetHigh;
32733 return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32660,11 +32754,11 @@
32754 ** API UnlockFile.
32755 */
32756 return winceUnlockFile(phFile, offsetLow, offsetHigh,
32757 numBytesLow, numBytesHigh);
32758 #else
32759 if( osIsNT() ){
32760 OVERLAPPED ovlp;
32761 memset(&ovlp, 0, sizeof(OVERLAPPED));
32762 ovlp.Offset = offsetLow;
32763 ovlp.OffsetHigh = offsetHigh;
32764 return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
@@ -32690,11 +32784,11 @@
32784 /*
32785 ** Move the current position of the file handle passed as the first
32786 ** argument to offset iOffset within the file. If successful, return 0.
32787 ** Otherwise, set pFile->lastErrno and return non-zero.
32788 */
32789 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
32790 #if !SQLITE_OS_WINRT
32791 LONG upperBits; /* Most sig. 32 bits of new offset */
32792 LONG lowerBits; /* Least sig. 32 bits of new offset */
32793 DWORD dwRet; /* Value returned by SetFilePointer() */
32794 DWORD lastErrno; /* Value returned by GetLastError() */
@@ -32715,11 +32809,11 @@
32809
32810 if( (dwRet==INVALID_SET_FILE_POINTER
32811 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
32812 pFile->lastErrno = lastErrno;
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));
@@ -32736,11 +32830,11 @@
32830 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
32831
32832 if(!bRet){
32833 pFile->lastErrno = osGetLastError();
32834 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
32835 "winSeekFile", pFile->zPath);
32836 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
32837 return 1;
32838 }
32839
32840 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
@@ -32747,11 +32841,12 @@
32841 return 0;
32842 #endif
32843 }
32844
32845 #if SQLITE_MAX_MMAP_SIZE>0
32846 /* Forward references to VFS helper methods used for memory mapped files */
32847 static int winMapfile(winFile*, sqlite3_int64);
32848 static int winUnmapfile(winFile*);
32849 #endif
32850
32851 /*
32852 ** Close a file.
@@ -32851,11 +32946,11 @@
32946 }
32947 }
32948 #endif
32949
32950 #if SQLITE_OS_WINCE
32951 if( winSeekFile(pFile, offset) ){
32952 OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
32953 return SQLITE_FULL;
32954 }
32955 while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
32956 #else
@@ -32864,17 +32959,17 @@
32959 overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
32960 while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
32961 osGetLastError()!=ERROR_HANDLE_EOF ){
32962 #endif
32963 DWORD lastErrno;
32964 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
32965 pFile->lastErrno = lastErrno;
32966 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
32967 return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
32968 "winRead", pFile->zPath);
32969 }
32970 winLogIoerr(nRetry);
32971 if( nRead<(DWORD)amt ){
32972 /* Unread parts of the buffer must be zero-filled */
32973 memset(&((char*)pBuf)[nRead], 0, amt-nRead);
32974 OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
32975 return SQLITE_IOERR_SHORT_READ;
@@ -32923,11 +33018,11 @@
33018 }
33019 }
33020 #endif
33021
33022 #if SQLITE_OS_WINCE
33023 rc = winSeekFile(pFile, offset);
33024 if( rc==0 ){
33025 #else
33026 {
33027 #endif
33028 #if !SQLITE_OS_WINCE
@@ -32948,11 +33043,11 @@
33043 #if SQLITE_OS_WINCE
33044 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33045 #else
33046 if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
33047 #endif
33048 if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33049 break;
33050 }
33051 assert( nWrite==0 || nWrite<=(DWORD)nRem );
33052 if( nWrite==0 || nWrite>(DWORD)nRem ){
33053 lastErrno = osGetLastError();
@@ -32974,17 +33069,18 @@
33069
33070 if( rc ){
33071 if( ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
33072 || ( pFile->lastErrno==ERROR_DISK_FULL )){
33073 OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
33074 return winLogError(SQLITE_FULL, pFile->lastErrno,
33075 "winWrite1", pFile->zPath);
33076 }
33077 OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
33078 return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33079 "winWrite2", pFile->zPath);
33080 }else{
33081 winLogIoerr(nRetry);
33082 }
33083 OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
33084 return SQLITE_OK;
33085 }
33086
@@ -33009,11 +33105,11 @@
33105 if( pFile->szChunk>0 ){
33106 nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33107 }
33108
33109 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33110 if( winSeekFile(pFile, nByte) ){
33111 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33112 "winTruncate1", pFile->zPath);
33113 }else if( 0==osSetEndOfFile(pFile->h) &&
33114 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33115 pFile->lastErrno = lastErrno;
@@ -33090,10 +33186,11 @@
33186
33187 /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33188 ** no-op
33189 */
33190 #ifdef SQLITE_NO_SYNC
33191 OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
33192 return SQLITE_OK;
33193 #else
33194 rc = osFlushFileBuffers(pFile->h);
33195 SimulateIOError( rc=FALSE );
33196 if( rc ){
@@ -33101,11 +33198,11 @@
33198 return SQLITE_OK;
33199 }else{
33200 pFile->lastErrno = osGetLastError();
33201 OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
33202 return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33203 "winSync", pFile->zPath);
33204 }
33205 #endif
33206 }
33207
33208 /*
@@ -33142,11 +33239,11 @@
33239 *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
33240 if( (lowerBits == INVALID_FILE_SIZE)
33241 && ((lastErrno = osGetLastError())!=NO_ERROR) ){
33242 pFile->lastErrno = lastErrno;
33243 rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33244 "winFileSize", pFile->zPath);
33245 }
33246 }
33247 #endif
33248 OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
33249 pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
@@ -33187,14 +33284,14 @@
33284 /*
33285 ** Acquire a reader lock.
33286 ** Different API routines are called depending on whether or not this
33287 ** is Win9x or WinNT.
33288 */
33289 static int winGetReadLock(winFile *pFile){
33290 int res;
33291 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33292 if( osIsNT() ){
33293 #if SQLITE_OS_WINCE
33294 /*
33295 ** NOTE: Windows CE is handled differently here due its lack of the Win32
33296 ** API LockFileEx.
33297 */
@@ -33222,15 +33319,15 @@
33319 }
33320
33321 /*
33322 ** Undo a readlock
33323 */
33324 static int winUnlockReadLock(winFile *pFile){
33325 int res;
33326 DWORD lastErrno;
33327 OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
33328 if( osIsNT() ){
33329 res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33330 }
33331 #ifdef SQLITE_WIN32_HAS_ANSI
33332 else{
33333 res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
@@ -33237,11 +33334,11 @@
33334 }
33335 #endif
33336 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
33337 pFile->lastErrno = lastErrno;
33338 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
33339 "winUnlockReadLock", pFile->zPath);
33340 }
33341 OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
33342 return res;
33343 }
33344
@@ -33328,11 +33425,11 @@
33425
33426 /* Acquire a shared lock
33427 */
33428 if( locktype==SHARED_LOCK && res ){
33429 assert( pFile->locktype==NO_LOCK );
33430 res = winGetReadLock(pFile);
33431 if( res ){
33432 newLocktype = SHARED_LOCK;
33433 }else{
33434 lastErrno = osGetLastError();
33435 }
@@ -33359,18 +33456,18 @@
33456
33457 /* Acquire an EXCLUSIVE lock
33458 */
33459 if( locktype==EXCLUSIVE_LOCK && res ){
33460 assert( pFile->locktype>=SHARED_LOCK );
33461 res = winUnlockReadLock(pFile);
33462 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
33463 SHARED_SIZE, 0);
33464 if( res ){
33465 newLocktype = EXCLUSIVE_LOCK;
33466 }else{
33467 lastErrno = osGetLastError();
33468 winGetReadLock(pFile);
33469 }
33470 }
33471
33472 /* If we are holding a PENDING lock that ought to be released, then
33473 ** release it now.
@@ -33383,14 +33480,14 @@
33480 ** return the appropriate result code.
33481 */
33482 if( res ){
33483 rc = SQLITE_OK;
33484 }else{
33485 pFile->lastErrno = lastErrno;
33486 rc = SQLITE_BUSY;
33487 OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
33488 pFile->h, locktype, newLocktype));
 
 
33489 }
33490 pFile->locktype = (u8)newLocktype;
33491 OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
33492 pFile->h, pFile->locktype, sqlite3ErrName(rc)));
33493 return rc;
@@ -33446,22 +33543,22 @@
33543 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
33544 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
33545 type = pFile->locktype;
33546 if( type>=EXCLUSIVE_LOCK ){
33547 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
33548 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
33549 /* This should never happen. We should always be able to
33550 ** reacquire the read lock */
33551 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
33552 "winUnlock", pFile->zPath);
33553 }
33554 }
33555 if( type>=RESERVED_LOCK ){
33556 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
33557 }
33558 if( locktype==NO_LOCK && type>=SHARED_LOCK ){
33559 winUnlockReadLock(pFile);
33560 }
33561 if( type>=PENDING_LOCK ){
33562 winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
33563 }
33564 pFile->locktype = (u8)locktype;
@@ -33484,15 +33581,14 @@
33581 }else{
33582 pFile->ctrlFlags |= mask;
33583 }
33584 }
33585
33586 /* Forward references to VFS helper methods used for temporary files */
33587 static int winGetTempname(sqlite3_vfs *, char **);
33588 static int winIsDir(const void *);
33589 static BOOL winIsDriveLetterAndColon(const char *);
 
33590
33591 /*
33592 ** Control and query of the open file handle.
33593 */
33594 static int winFileControl(sqlite3_file *id, int op, void *pArg){
@@ -33548,30 +33644,30 @@
33644 return SQLITE_OK;
33645 }
33646 case SQLITE_FCNTL_WIN32_AV_RETRY: {
33647 int *a = (int*)pArg;
33648 if( a[0]>0 ){
33649 winIoerrRetry = a[0];
33650 }else{
33651 a[0] = winIoerrRetry;
33652 }
33653 if( a[1]>0 ){
33654 winIoerrRetryDelay = a[1];
33655 }else{
33656 a[1] = winIoerrRetryDelay;
33657 }
33658 OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
33659 return SQLITE_OK;
33660 }
33661 case SQLITE_FCNTL_TEMPFILENAME: {
33662 char *zTFile = 0;
33663 int rc = winGetTempname(pFile->pVfs, &zTFile);
33664 if( rc==SQLITE_OK ){
33665 *(char**)pArg = zTFile;
33666 }
33667 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
33668 return rc;
33669 }
33670 #if SQLITE_MAX_MMAP_SIZE>0
33671 case SQLITE_FCNTL_MMAP_SIZE: {
33672 i64 newLimit = *(i64*)pArg;
33673 int rc = SQLITE_OK;
@@ -33584,11 +33680,11 @@
33680 if( pFile->mmapSize>0 ){
33681 (void)winUnmapfile(pFile);
33682 rc = winMapfile(pFile, -1);
33683 }
33684 }
33685 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
33686 return rc;
33687 }
33688 #endif
33689 }
33690 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
@@ -33900,11 +33996,11 @@
33996 */
33997 if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
33998 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
33999 if( rc!=SQLITE_OK ){
34000 rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
34001 "winOpenShm", pDbFd->zPath);
34002 }
34003 }
34004 if( rc==SQLITE_OK ){
34005 winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34006 rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
@@ -34160,11 +34256,11 @@
34256 ** large enough to contain the requested region).
34257 */
34258 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
34259 if( rc!=SQLITE_OK ){
34260 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34261 "winShmMap1", pDbFd->zPath);
34262 goto shmpage_out;
34263 }
34264
34265 if( sz<nByte ){
34266 /* The requested memory region does not exist. If isWrite is set to
@@ -34175,11 +34271,11 @@
34271 */
34272 if( !isWrite ) goto shmpage_out;
34273 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
34274 if( rc!=SQLITE_OK ){
34275 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
34276 "winShmMap2", pDbFd->zPath);
34277 goto shmpage_out;
34278 }
34279 }
34280
34281 /* Map the requested memory region into this processes address space. */
@@ -34229,11 +34325,11 @@
34325 szRegion, pMap ? "ok" : "failed"));
34326 }
34327 if( !pMap ){
34328 pShmNode->lastErrno = osGetLastError();
34329 rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
34330 "winShmMap3", pDbFd->zPath);
34331 if( hMap ) osCloseHandle(hMap);
34332 goto shmpage_out;
34333 }
34334
34335 pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
@@ -34277,11 +34373,11 @@
34373 pFile->lastErrno = osGetLastError();
34374 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
34375 "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
34376 pFile->pMapRegion));
34377 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34378 "winUnmapfile1", pFile->zPath);
34379 }
34380 pFile->pMapRegion = 0;
34381 pFile->mmapSize = 0;
34382 pFile->mmapSizeActual = 0;
34383 }
@@ -34289,11 +34385,11 @@
34385 if( !osCloseHandle(pFile->hMap) ){
34386 pFile->lastErrno = osGetLastError();
34387 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
34388 osGetCurrentProcessId(), pFile, pFile->hMap));
34389 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34390 "winUnmapfile2", pFile->zPath);
34391 }
34392 pFile->hMap = NULL;
34393 }
34394 OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
34395 osGetCurrentProcessId(), pFile));
@@ -34364,14 +34460,14 @@
34460 (DWORD)(nMap & 0xffffffff), NULL);
34461 #endif
34462 if( pFd->hMap==NULL ){
34463 pFd->lastErrno = osGetLastError();
34464 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34465 "winMapfile1", pFd->zPath);
34466 /* Log the error, but continue normal operation using xRead/xWrite */
34467 OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
34468 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
34469 return SQLITE_OK;
34470 }
34471 assert( (nMap % winSysInfo.dwPageSize)==0 );
34472 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
34473 #if SQLITE_OS_WINRT
@@ -34381,14 +34477,15 @@
34477 #endif
34478 if( pNew==NULL ){
34479 osCloseHandle(pFd->hMap);
34480 pFd->hMap = NULL;
34481 pFd->lastErrno = osGetLastError();
34482 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34483 "winMapfile2", pFd->zPath);
34484 /* Log the error, but continue normal operation using xRead/xWrite */
34485 OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
34486 osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
34487 return SQLITE_OK;
34488 }
34489 pFd->pMapRegion = pNew;
34490 pFd->mmapSize = nMap;
34491 pFd->mmapSizeActual = nMap;
@@ -34522,21 +34619,40 @@
34619 **************************** sqlite3_vfs methods ****************************
34620 **
34621 ** This division contains the implementation of methods on the
34622 ** sqlite3_vfs object.
34623 */
34624
34625 /*
34626 ** Convert a filename from whatever the underlying operating system
34627 ** supports for filenames into UTF-8. Space to hold the result is
34628 ** obtained from malloc and must be freed by the calling function.
34629 */
34630 static char *winConvertToUtf8Filename(const void *zFilename){
34631 char *zConverted = 0;
34632 if( osIsNT() ){
34633 zConverted = winUnicodeToUtf8(zFilename);
34634 }
34635 #ifdef SQLITE_WIN32_HAS_ANSI
34636 else{
34637 zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
34638 }
34639 #endif
34640 /* caller will handle out of memory */
34641 return zConverted;
34642 }
34643
34644 /*
34645 ** Convert a UTF-8 filename into whatever form the underlying
34646 ** operating system wants filenames in. Space to hold the result
34647 ** is obtained from malloc and must be freed by the calling
34648 ** function.
34649 */
34650 static void *winConvertFromUtf8Filename(const char *zFilename){
34651 void *zConverted = 0;
34652 if( osIsNT() ){
34653 zConverted = winUtf8ToUnicode(zFilename);
34654 }
34655 #ifdef SQLITE_WIN32_HAS_ANSI
34656 else{
34657 zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
34658 }
@@ -34544,117 +34660,220 @@
34660 /* caller will handle out of memory */
34661 return zConverted;
34662 }
34663
34664 /*
34665 ** This function returns non-zero if the specified UTF-8 string buffer
34666 ** ends with a directory separator character.
 
34667 */
34668 static int winEndsInDirSep(char *zBuf){
34669 if( zBuf ){
34670 int nLen = sqlite3Strlen30(zBuf);
34671 return nLen>0 && winIsDirSep(zBuf[nLen-1]);
34672 }
34673 return 0;
34674 }
34675
34676 /*
34677 ** Create a temporary file name and store the resulting pointer into pzBuf.
34678 ** The pointer returned in pzBuf must be freed via sqlite3_free().
34679 */
34680 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
34681 static char zChars[] =
34682 "abcdefghijklmnopqrstuvwxyz"
34683 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34684 "0123456789";
34685 size_t i, j;
34686 int nBuf, nLen;
34687 char *zBuf;
34688
34689 /* It's odd to simulate an io-error here, but really this is just
34690 ** using the io-error infrastructure to test that SQLite handles this
34691 ** function failing.
34692 */
34693 SimulateIOError( return SQLITE_IOERR );
34694
34695 /* Allocate a temporary buffer to store the fully qualified file
34696 ** name for the temporary file. If this fails, we cannot continue.
34697 */
34698 nBuf = pVfs->mxPathname;
34699 zBuf = sqlite3MallocZero( nBuf+2 );
34700 if( !zBuf ){
34701 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34702 return SQLITE_IOERR_NOMEM;
34703 }
34704
34705 /* Figure out the effective temporary directory. First, check if one
34706 ** has been explicitly set by the application; otherwise, use the one
34707 ** configured by the operating system.
34708 */
34709 assert( nBuf>30 );
34710 if( sqlite3_temp_directory ){
34711 sqlite3_snprintf(nBuf-30, zBuf, "%s%s", sqlite3_temp_directory,
34712 winEndsInDirSep(sqlite3_temp_directory) ? "" :
34713 winGetDirDep());
34714 }
34715 #if defined(__CYGWIN__)
34716 else{
34717 static const char *azDirs[] = {
34718 0, /* getenv("SQLITE_TMPDIR") */
34719 0, /* getenv("TMPDIR") */
34720 0, /* getenv("TMP") */
34721 0, /* getenv("TEMP") */
34722 0, /* getenv("USERPROFILE") */
34723 "/var/tmp",
34724 "/usr/tmp",
34725 "/tmp",
34726 ".",
34727 0 /* List terminator */
34728 };
34729 unsigned int i;
34730 const char *zDir = 0;
34731
34732 if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
34733 if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
34734 if( !azDirs[2] ) azDirs[2] = getenv("TMP");
34735 if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
34736 if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
34737 for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
34738 void *zConverted;
34739 if( zDir==0 ) continue;
34740 /* If the path starts with a drive letter followed by the colon
34741 ** character, assume it is already a native Win32 path; otherwise,
34742 ** it must be converted to a native Win32 path prior via the Cygwin
34743 ** API prior to using it.
34744 */
34745 if( winIsDriveLetterAndColon(zDir) ){
34746 zConverted = winConvertFromUtf8Filename(zDir);
34747 if( !zConverted ){
34748 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34749 return SQLITE_IOERR_NOMEM;
34750 }
34751 if( winIsDir(zConverted) ){
34752 sqlite3_snprintf(nBuf-30, zBuf, "%s", zDir);
34753 sqlite3_free(zConverted);
34754 break;
34755 }
34756 sqlite3_free(zConverted);
34757 }else{
34758 zConverted = sqlite3MallocZero( nBuf+1 );
34759 if( !zConverted ){
34760 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34761 return SQLITE_IOERR_NOMEM;
34762 }
34763 if( cygwin_conv_path(
34764 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
34765 zConverted, nBuf+1)<0 ){
34766 sqlite3_free(zConverted);
34767 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
34768 return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
34769 "winGetTempname1", zDir);
34770 }
34771 if( winIsDir(zConverted) ){
34772 /* At this point, we know the candidate directory exists and should
34773 ** be used. However, we may need to convert the string containing
34774 ** its name into UTF-8 (i.e. if it is UTF-16 right now).
34775 */
34776 if( osIsNT() ){
34777 char *zUtf8 = winUnicodeToUtf8(zConverted);
34778 if( !zUtf8 ){
34779 sqlite3_free(zConverted);
34780 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34781 return SQLITE_IOERR_NOMEM;
34782 }
34783 sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
34784 sqlite3_free(zUtf8);
34785 sqlite3_free(zConverted);
34786 break;
34787 }else{
34788 sqlite3_snprintf(nBuf-30, zBuf, "%s", zConverted);
34789 sqlite3_free(zConverted);
34790 break;
34791 }
34792 }
34793 sqlite3_free(zConverted);
34794 }
34795 break;
34796 }
34797 }
34798 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
34799 else if( osIsNT() ){
34800 char *zMulti;
34801 LPWSTR zWidePath = sqlite3MallocZero( nBuf*sizeof(WCHAR) );
34802 if( !zWidePath ){
34803 sqlite3_free(zBuf);
34804 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34805 return SQLITE_IOERR_NOMEM;
34806 }
34807 if( osGetTempPathW(nBuf, zWidePath)==0 ){
34808 sqlite3_free(zWidePath);
34809 sqlite3_free(zBuf);
34810 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34811 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
34812 "winGetTempname1", 0);
34813 }
34814 zMulti = winUnicodeToUtf8(zWidePath);
34815 if( zMulti ){
34816 sqlite3_snprintf(nBuf-30, zBuf, "%s", zMulti);
34817 sqlite3_free(zMulti);
34818 sqlite3_free(zWidePath);
34819 }else{
34820 sqlite3_free(zWidePath);
34821 sqlite3_free(zBuf);
34822 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34823 return SQLITE_IOERR_NOMEM;
34824 }
34825 }
34826 #ifdef SQLITE_WIN32_HAS_ANSI
34827 else{
34828 char *zUtf8;
34829 char *zMbcsPath = sqlite3MallocZero( nBuf );
34830 if( !zMbcsPath ){
34831 sqlite3_free(zBuf);
34832 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34833 return SQLITE_IOERR_NOMEM;
34834 }
34835 if( osGetTempPathA(nBuf, zMbcsPath)==0 ){
34836 sqlite3_free(zBuf);
34837 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
34838 return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
34839 "winGetTempname2", 0);
34840 }
34841 zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
34842 if( zUtf8 ){
34843 sqlite3_snprintf(nBuf-30, zBuf, "%s", zUtf8);
34844 sqlite3_free(zUtf8);
34845 }else{
34846 sqlite3_free(zBuf);
34847 OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
34848 return SQLITE_IOERR_NOMEM;
34849 }
34850 }
 
 
 
 
 
 
 
 
34851 #endif /* SQLITE_WIN32_HAS_ANSI */
 
 
 
 
 
 
 
 
34852 #endif /* !SQLITE_OS_WINRT */
34853
34854 /* Check that the output buffer is large enough for the temporary file
34855 ** name. If it is not, return SQLITE_ERROR.
34856 */
34857 nLen = sqlite3Strlen30(zBuf);
34858
34859 if( (nLen + sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX) + 18) >= nBuf ){
34860 sqlite3_free(zBuf);
34861 OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
34862 return winLogError(SQLITE_ERROR, 0, "winGetTempname3", 0);
34863 }
34864
34865 sqlite3_snprintf(nBuf-18-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
 
34866
 
 
 
34867 j = sqlite3Strlen30(zBuf);
34868 sqlite3_randomness(15, &zBuf[j]);
34869 for(i=0; i<15; i++, j++){
34870 zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
34871 }
34872 zBuf[j] = 0;
34873 zBuf[j+1] = 0;
34874 *pzBuf = zBuf;
34875
34876 OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
34877 return SQLITE_OK;
34878 }
34879
@@ -34666,17 +34885,17 @@
34885 static int winIsDir(const void *zConverted){
34886 DWORD attr;
34887 int rc = 0;
34888 DWORD lastErrno;
34889
34890 if( osIsNT() ){
34891 int cnt = 0;
34892 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
34893 memset(&sAttrData, 0, sizeof(sAttrData));
34894 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
34895 GetFileExInfoStandard,
34896 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
34897 if( !rc ){
34898 return 0; /* Invalid name? */
34899 }
34900 attr = sAttrData.dwFileAttributes;
34901 #if SQLITE_OS_WINCE==0
@@ -34689,11 +34908,11 @@
34908
34909 /*
34910 ** Open a file.
34911 */
34912 static int winOpen(
34913 sqlite3_vfs *pVfs, /* Used to get maximum path name length */
34914 const char *zName, /* Name of the file (UTF-8) */
34915 sqlite3_file *id, /* Write the SQLite file handle here */
34916 int flags, /* Open mode flags */
34917 int *pOutFlags /* Status return flags */
34918 ){
@@ -34712,11 +34931,11 @@
34931 int cnt = 0;
34932
34933 /* If argument zPath is a NULL pointer, this function is required to open
34934 ** a temporary file. Use this buffer to store the file name in.
34935 */
34936 char *zTmpname = 0; /* For temporary filename, if necessary. */
34937
34938 int rc = SQLITE_OK; /* Function Return Code */
34939 #if !defined(NDEBUG) || SQLITE_OS_WINCE
34940 int eType = flags&0xFFFFFF00; /* Type of file to open */
34941 #endif
@@ -34767,22 +34986,22 @@
34986 assert( pFile!=0 );
34987 memset(pFile, 0, sizeof(winFile));
34988 pFile->h = INVALID_HANDLE_VALUE;
34989
34990 #if SQLITE_OS_WINRT
34991 if( !zUtf8Name && !sqlite3_temp_directory ){
34992 sqlite3_log(SQLITE_ERROR,
34993 "sqlite3_temp_directory variable should be set for WinRT");
34994 }
34995 #endif
34996
34997 /* If the second argument to this function is NULL, generate a
34998 ** temporary file name to use
34999 */
35000 if( !zUtf8Name ){
35001 assert( isDelete && !isOpenJournal );
35002 rc = winGetTempname(pVfs, &zTmpname);
35003 if( rc!=SQLITE_OK ){
35004 OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
35005 return rc;
35006 }
35007 zUtf8Name = zTmpname;
@@ -34791,21 +35010,23 @@
35010 /* Database filenames are double-zero terminated if they are not
35011 ** URIs with parameters. Hence, they can always be passed into
35012 ** sqlite3_uri_parameter().
35013 */
35014 assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
35015 zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
35016
35017 /* Convert the filename to the system encoding. */
35018 zConverted = winConvertFromUtf8Filename(zUtf8Name);
35019 if( zConverted==0 ){
35020 sqlite3_free(zTmpname);
35021 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
35022 return SQLITE_IOERR_NOMEM;
35023 }
35024
35025 if( winIsDir(zConverted) ){
35026 sqlite3_free(zConverted);
35027 sqlite3_free(zTmpname);
35028 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
35029 return SQLITE_CANTOPEN_ISDIR;
35030 }
35031
35032 if( isReadWrite ){
@@ -34848,11 +35069,11 @@
35069 ** better if FILE_FLAG_RANDOM_ACCESS is used. Ticket #2699. */
35070 #if SQLITE_OS_WINCE
35071 dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
35072 #endif
35073
35074 if( osIsNT() ){
35075 #if SQLITE_OS_WINRT
35076 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
35077 extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
35078 extendedParameters.dwFileAttributes =
35079 dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
@@ -34863,21 +35084,21 @@
35084 while( (h = osCreateFile2((LPCWSTR)zConverted,
35085 dwDesiredAccess,
35086 dwShareMode,
35087 dwCreationDisposition,
35088 &extendedParameters))==INVALID_HANDLE_VALUE &&
35089 winRetryIoerr(&cnt, &lastErrno) ){
35090 /* Noop */
35091 }
35092 #else
35093 while( (h = osCreateFileW((LPCWSTR)zConverted,
35094 dwDesiredAccess,
35095 dwShareMode, NULL,
35096 dwCreationDisposition,
35097 dwFlagsAndAttributes,
35098 NULL))==INVALID_HANDLE_VALUE &&
35099 winRetryIoerr(&cnt, &lastErrno) ){
35100 /* Noop */
35101 }
35102 #endif
35103 }
35104 #ifdef SQLITE_WIN32_HAS_ANSI
@@ -34886,24 +35107,25 @@
35107 dwDesiredAccess,
35108 dwShareMode, NULL,
35109 dwCreationDisposition,
35110 dwFlagsAndAttributes,
35111 NULL))==INVALID_HANDLE_VALUE &&
35112 winRetryIoerr(&cnt, &lastErrno) ){
35113 /* Noop */
35114 }
35115 }
35116 #endif
35117 winLogIoerr(cnt);
35118
35119 OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
35120 dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
35121
35122 if( h==INVALID_HANDLE_VALUE ){
35123 pFile->lastErrno = lastErrno;
35124 winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
35125 sqlite3_free(zConverted);
35126 sqlite3_free(zTmpname);
35127 if( isReadWrite && !isExclusive ){
35128 return winOpen(pVfs, zName, id,
35129 ((flags|SQLITE_OPEN_READONLY) &
35130 ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
35131 pOutFlags);
@@ -34928,19 +35150,21 @@
35150 if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
35151 && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
35152 ){
35153 osCloseHandle(h);
35154 sqlite3_free(zConverted);
35155 sqlite3_free(zTmpname);
35156 OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
35157 return rc;
35158 }
35159 if( isTemp ){
35160 pFile->zDeleteOnClose = zConverted;
35161 }else
35162 #endif
35163 {
35164 sqlite3_free(zConverted);
35165 sqlite3_free(zTmpname);
35166 }
35167
35168 pFile->pMethod = &winIoMethod;
35169 pFile->pVfs = pVfs;
35170 pFile->h = h;
@@ -34990,15 +35214,16 @@
35214 UNUSED_PARAMETER(syncDir);
35215
35216 SimulateIOError(return SQLITE_IOERR_DELETE);
35217 OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
35218
35219 zConverted = winConvertFromUtf8Filename(zFilename);
35220 if( zConverted==0 ){
35221 OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35222 return SQLITE_IOERR_NOMEM;
35223 }
35224 if( osIsNT() ){
35225 do {
35226 #if SQLITE_OS_WINRT
35227 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35228 memset(&sAttrData, 0, sizeof(sAttrData));
35229 if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
@@ -35033,11 +35258,11 @@
35258 }
35259 if ( osDeleteFileW(zConverted) ){
35260 rc = SQLITE_OK; /* Deleted OK. */
35261 break;
35262 }
35263 if ( !winRetryIoerr(&cnt, &lastErrno) ){
35264 rc = SQLITE_ERROR; /* No more retries. */
35265 break;
35266 }
35267 } while(1);
35268 }
@@ -35061,22 +35286,21 @@
35286 }
35287 if ( osDeleteFileA(zConverted) ){
35288 rc = SQLITE_OK; /* Deleted OK. */
35289 break;
35290 }
35291 if ( !winRetryIoerr(&cnt, &lastErrno) ){
35292 rc = SQLITE_ERROR; /* No more retries. */
35293 break;
35294 }
35295 } while(1);
35296 }
35297 #endif
35298 if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
35299 rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
 
35300 }else{
35301 winLogIoerr(cnt);
35302 }
35303 sqlite3_free(zConverted);
35304 OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
35305 return rc;
35306 }
@@ -35098,22 +35322,22 @@
35322
35323 SimulateIOError( return SQLITE_IOERR_ACCESS; );
35324 OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
35325 zFilename, flags, pResOut));
35326
35327 zConverted = winConvertFromUtf8Filename(zFilename);
35328 if( zConverted==0 ){
35329 OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
35330 return SQLITE_IOERR_NOMEM;
35331 }
35332 if( osIsNT() ){
35333 int cnt = 0;
35334 WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35335 memset(&sAttrData, 0, sizeof(sAttrData));
35336 while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35337 GetFileExInfoStandard,
35338 &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
35339 if( rc ){
35340 /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
35341 ** as if it does not exist.
35342 */
35343 if( flags==SQLITE_ACCESS_EXISTS
@@ -35122,15 +35346,15 @@
35346 attr = INVALID_FILE_ATTRIBUTES;
35347 }else{
35348 attr = sAttrData.dwFileAttributes;
35349 }
35350 }else{
35351 winLogIoerr(cnt);
35352 if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
 
35353 sqlite3_free(zConverted);
35354 return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
35355 zFilename);
35356 }else{
35357 attr = INVALID_FILE_ATTRIBUTES;
35358 }
35359 }
35360 }
@@ -35156,10 +35380,19 @@
35380 OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
35381 zFilename, pResOut, *pResOut));
35382 return SQLITE_OK;
35383 }
35384
35385 /*
35386 ** Returns non-zero if the specified path name starts with a drive letter
35387 ** followed by a colon character.
35388 */
35389 static BOOL winIsDriveLetterAndColon(
35390 const char *zPathname
35391 ){
35392 return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
35393 }
35394
35395 /*
35396 ** Returns non-zero if the specified path name should be used verbatim. If
35397 ** non-zero is returned from this function, the calling function must simply
35398 ** use the provided path name verbatim -OR- resolve it into a full path name
@@ -35173,21 +35406,21 @@
35406 ** a legal UNC name, a volume relative path, or an absolute path name in the
35407 ** "Unix" format on Windows. There is no easy way to differentiate between
35408 ** the final two cases; therefore, we return the safer return value of TRUE
35409 ** so that callers of this function will simply use it verbatim.
35410 */
35411 if ( winIsDirSep(zPathname[0]) ){
35412 return TRUE;
35413 }
35414
35415 /*
35416 ** If the path name starts with a letter and a colon it is either a volume
35417 ** relative path or an absolute path. Callers of this function must not
35418 ** attempt to treat it as a relative path name (i.e. they should simply use
35419 ** it verbatim).
35420 */
35421 if ( winIsDriveLetterAndColon(zPathname) ){
35422 return TRUE;
35423 }
35424
35425 /*
35426 ** If we get to this point, the path name should almost certainly be a purely
@@ -35209,33 +35442,35 @@
35442 ){
35443
35444 #if defined(__CYGWIN__)
35445 SimulateIOError( return SQLITE_ERROR );
35446 UNUSED_PARAMETER(nFull);
 
35447 assert( nFull>=pVfs->mxPathname );
35448 if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
35449 /*
35450 ** NOTE: We are dealing with a relative path name and the data
35451 ** directory has been set. Therefore, use it as the basis
35452 ** for converting the relative path name to an absolute
35453 ** one by prepending the data directory and a slash.
35454 */
35455 char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
35456 if( !zOut ){
35457 return SQLITE_IOERR_NOMEM;
35458 }
35459 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A|CCP_RELATIVE, zRelative, zOut,
35460 pVfs->mxPathname+1)<0 ){
35461 sqlite3_free(zOut);
35462 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
35463 "winFullPathname1", zRelative);
35464 }
35465 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35466 sqlite3_data_directory, winGetDirDep(), zOut);
35467 sqlite3_free(zOut);
35468 }else{
35469 if( cygwin_conv_path(CCP_POSIX_TO_WIN_A, zRelative, zFull, nFull)<0 ){
35470 return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
35471 "winFullPathname2", zRelative);
 
35472 }
35473 }
35474 return SQLITE_OK;
35475 #endif
35476
@@ -35248,12 +35483,12 @@
35483 ** NOTE: We are dealing with a relative path name and the data
35484 ** directory has been set. Therefore, use it as the basis
35485 ** for converting the relative path name to an absolute
35486 ** one by prepending the data directory and a backslash.
35487 */
35488 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35489 sqlite3_data_directory, winGetDirDep(), zRelative);
35490 }else{
35491 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
35492 }
35493 return SQLITE_OK;
35494 #endif
@@ -35264,11 +35499,11 @@
35499 char *zOut;
35500
35501 /* If this path name begins with "/X:", where "X" is any alphabetic
35502 ** character, discard the initial "/" from the pathname.
35503 */
35504 if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
35505 zRelative++;
35506 }
35507
35508 /* It's odd to simulate an io-error here, but really this is just
35509 ** using the io-error infrastructure to test that SQLite handles this
@@ -35281,68 +35516,64 @@
35516 ** NOTE: We are dealing with a relative path name and the data
35517 ** directory has been set. Therefore, use it as the basis
35518 ** for converting the relative path name to an absolute
35519 ** one by prepending the data directory and a backslash.
35520 */
35521 sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%s%s",
35522 sqlite3_data_directory, winGetDirDep(), zRelative);
35523 return SQLITE_OK;
35524 }
35525 zConverted = winConvertFromUtf8Filename(zRelative);
35526 if( zConverted==0 ){
35527 return SQLITE_IOERR_NOMEM;
35528 }
35529 if( osIsNT() ){
35530 LPWSTR zTemp;
35531 nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
35532 if( nByte==0 ){
 
 
35533 sqlite3_free(zConverted);
35534 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35535 "winFullPathname1", zRelative);
35536 }
35537 nByte += 3;
35538 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
35539 if( zTemp==0 ){
35540 sqlite3_free(zConverted);
35541 return SQLITE_IOERR_NOMEM;
35542 }
35543 nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
35544 if( nByte==0 ){
 
 
35545 sqlite3_free(zConverted);
35546 sqlite3_free(zTemp);
35547 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35548 "winFullPathname2", zRelative);
35549 }
35550 sqlite3_free(zConverted);
35551 zOut = winUnicodeToUtf8(zTemp);
35552 sqlite3_free(zTemp);
35553 }
35554 #ifdef SQLITE_WIN32_HAS_ANSI
35555 else{
35556 char *zTemp;
35557 nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
35558 if( nByte==0 ){
 
 
35559 sqlite3_free(zConverted);
35560 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35561 "winFullPathname3", zRelative);
35562 }
35563 nByte += 3;
35564 zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
35565 if( zTemp==0 ){
35566 sqlite3_free(zConverted);
35567 return SQLITE_IOERR_NOMEM;
35568 }
35569 nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
35570 if( nByte==0 ){
 
 
35571 sqlite3_free(zConverted);
35572 sqlite3_free(zTemp);
35573 return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
35574 "winFullPathname4", zRelative);
35575 }
35576 sqlite3_free(zConverted);
35577 zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
35578 sqlite3_free(zTemp);
35579 }
@@ -35366,16 +35597,16 @@
35597 ** Interfaces for opening a shared library, finding entry points
35598 ** within the shared library, and closing the shared library.
35599 */
35600 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
35601 HANDLE h;
35602 void *zConverted = winConvertFromUtf8Filename(zFilename);
35603 UNUSED_PARAMETER(pVfs);
35604 if( zConverted==0 ){
35605 return 0;
35606 }
35607 if( osIsNT() ){
35608 #if SQLITE_OS_WINRT
35609 h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
35610 #else
35611 h = osLoadLibraryW((LPCWSTR)zConverted);
35612 #endif
@@ -35388,11 +35619,11 @@
35619 sqlite3_free(zConverted);
35620 return (void*)h;
35621 }
35622 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
35623 UNUSED_PARAMETER(pVfs);
35624 winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
35625 }
35626 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
35627 UNUSED_PARAMETER(pVfs);
35628 return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
35629 }
@@ -35564,21 +35795,21 @@
35795 ** by sqlite into the error message available to the user using
35796 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
35797 */
35798 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
35799 UNUSED_PARAMETER(pVfs);
35800 return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
35801 }
35802
35803 /*
35804 ** Initialize and deinitialize the operating system interface.
35805 */
35806 SQLITE_API int sqlite3_os_init(void){
35807 static sqlite3_vfs winVfs = {
35808 3, /* iVersion */
35809 sizeof(winFile), /* szOsFile */
35810 SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
35811 0, /* pNext */
35812 "win32", /* zName */
35813 0, /* pAppData */
35814 winOpen, /* xOpen */
35815 winDelete, /* xDelete */
@@ -35595,10 +35826,36 @@
35826 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35827 winSetSystemCall, /* xSetSystemCall */
35828 winGetSystemCall, /* xGetSystemCall */
35829 winNextSystemCall, /* xNextSystemCall */
35830 };
35831 #if defined(SQLITE_WIN32_HAS_WIDE)
35832 static sqlite3_vfs winLongPathVfs = {
35833 3, /* iVersion */
35834 sizeof(winFile), /* szOsFile */
35835 SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
35836 0, /* pNext */
35837 "win32-longpath", /* zName */
35838 0, /* pAppData */
35839 winOpen, /* xOpen */
35840 winDelete, /* xDelete */
35841 winAccess, /* xAccess */
35842 winFullPathname, /* xFullPathname */
35843 winDlOpen, /* xDlOpen */
35844 winDlError, /* xDlError */
35845 winDlSym, /* xDlSym */
35846 winDlClose, /* xDlClose */
35847 winRandomness, /* xRandomness */
35848 winSleep, /* xSleep */
35849 winCurrentTime, /* xCurrentTime */
35850 winGetLastError, /* xGetLastError */
35851 winCurrentTimeInt64, /* xCurrentTimeInt64 */
35852 winSetSystemCall, /* xSetSystemCall */
35853 winGetSystemCall, /* xGetSystemCall */
35854 winNextSystemCall, /* xNextSystemCall */
35855 };
35856 #endif
35857
35858 /* Double-check that the aSyscall[] array has been constructed
35859 ** correctly. See ticket [bb3a86e890c8e96ab] */
35860 assert( ArraySize(aSyscall)==74 );
35861
@@ -35611,10 +35868,15 @@
35868 #endif
35869 assert( winSysInfo.dwAllocationGranularity>0 );
35870 assert( winSysInfo.dwPageSize>0 );
35871
35872 sqlite3_vfs_register(&winVfs, 1);
35873
35874 #if defined(SQLITE_WIN32_HAS_WIDE)
35875 sqlite3_vfs_register(&winLongPathVfs, 0);
35876 #endif
35877
35878 return SQLITE_OK;
35879 }
35880
35881 SQLITE_API int sqlite3_os_end(void){
35882 #if SQLITE_OS_WINRT
@@ -52086,10 +52348,11 @@
52348 pBt->max1bytePayload = (u8)pBt->maxLocal;
52349 }
52350 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
52351 pBt->pPage1 = pPage1;
52352 pBt->nPage = nPage;
52353 assert( pPage1->leaf==0 || pPage1->leaf==1 );
52354 return SQLITE_OK;
52355
52356 page1_init_failed:
52357 releasePage(pPage1);
52358 pBt->pPage1 = 0;
@@ -59838,43 +60101,108 @@
60101 }
60102 return p;
60103 }
60104
60105 /*
60106 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
60107 ** valueNew(). See comments above valueNew() for details.
60108 */
60109 struct ValueNewStat4Ctx {
60110 Parse *pParse;
60111 Index *pIdx;
60112 UnpackedRecord **ppRec;
60113 int iVal;
60114 };
60115
60116 /*
60117 ** Allocate and return a pointer to a new sqlite3_value object. If
60118 ** the second argument to this function is NULL, the object is allocated
60119 ** by calling sqlite3ValueNew().
60120 **
60121 ** Otherwise, if the second argument is non-zero, then this function is
60122 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
60123 ** already been allocated, allocate the UnpackedRecord structure that
60124 ** that function will return to its caller here. Then return a pointer
60125 ** an sqlite3_value within the UnpackedRecord.a[] array.
 
60126 */
60127 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60128 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60129 if( p ){
60130 UnpackedRecord *pRec = p->ppRec[0];
60131
60132 if( pRec==0 ){
60133 Index *pIdx = p->pIdx; /* Index being probed */
60134 int nByte; /* Bytes of space to allocate */
60135 int i; /* Counter variable */
60136 int nCol = pIdx->nColumn+1; /* Number of index columns including rowid */
60137
60138 nByte = sizeof(Mem) * nCol + sizeof(UnpackedRecord);
60139 pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60140 if( pRec ){
60141 pRec->pKeyInfo = sqlite3IndexKeyinfo(p->pParse, pIdx);
60142 if( pRec->pKeyInfo ){
60143 assert( pRec->pKeyInfo->nField+1==nCol );
60144 pRec->pKeyInfo->enc = ENC(db);
60145 pRec->flags = UNPACKED_PREFIX_MATCH;
60146 pRec->aMem = (Mem *)&pRec[1];
60147 for(i=0; i<nCol; i++){
60148 pRec->aMem[i].flags = MEM_Null;
60149 pRec->aMem[i].type = SQLITE_NULL;
60150 pRec->aMem[i].db = db;
60151 }
60152 }else{
60153 sqlite3DbFree(db, pRec);
60154 pRec = 0;
60155 }
60156 }
60157 if( pRec==0 ) return 0;
60158 p->ppRec[0] = pRec;
60159 }
60160
60161 pRec->nField = p->iVal+1;
60162 return &pRec->aMem[p->iVal];
60163 }
60164 #endif
60165 return sqlite3ValueNew(db);
60166 }
60167
60168 /*
60169 ** Extract a value from the supplied expression in the manner described
60170 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
60171 ** using valueNew().
60172 **
60173 ** If pCtx is NULL and an error occurs after the sqlite3_value object
60174 ** has been allocated, it is freed before returning. Or, if pCtx is not
60175 ** NULL, it is assumed that the caller will free any allocated object
60176 ** in all cases.
60177 */
60178 int valueFromExpr(
60179 sqlite3 *db, /* The database connection */
60180 Expr *pExpr, /* The expression to evaluate */
60181 u8 enc, /* Encoding to use */
60182 u8 affinity, /* Affinity to use */
60183 sqlite3_value **ppVal, /* Write the new value here */
60184 struct ValueNewStat4Ctx *pCtx /* Second argument for valueNew() */
60185 ){
60186 int op;
60187 char *zVal = 0;
60188 sqlite3_value *pVal = 0;
60189 int negInt = 1;
60190 const char *zNeg = "";
60191 int rc = SQLITE_OK;
60192
60193 if( !pExpr ){
60194 *ppVal = 0;
60195 return SQLITE_OK;
60196 }
60197 op = pExpr->op;
60198
60199 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT4.
60200 ** The ifdef here is to enable us to achieve 100% branch test coverage even
60201 ** when SQLITE_ENABLE_STAT4 is omitted.
60202 */
60203 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60204 if( op==TK_REGISTER ) op = pExpr->op2;
60205 #else
60206 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
60207 #endif
60208
@@ -59888,11 +60216,11 @@
60216 negInt = -1;
60217 zNeg = "-";
60218 }
60219
60220 if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
60221 pVal = valueNew(db, pCtx);
60222 if( pVal==0 ) goto no_mem;
60223 if( ExprHasProperty(pExpr, EP_IntValue) ){
60224 sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
60225 }else{
60226 zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
@@ -59905,15 +60233,17 @@
60233 }else{
60234 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60235 }
60236 if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
60237 if( enc!=SQLITE_UTF8 ){
60238 rc = sqlite3VdbeChangeEncoding(pVal, enc);
60239 }
60240 }else if( op==TK_UMINUS ) {
60241 /* This branch happens for multiple negative signs. Ex: -(-5) */
60242 if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
60243 && pVal!=0
60244 ){
60245 sqlite3VdbeMemNumerify(pVal);
60246 if( pVal->u.i==SMALLEST_INT64 ){
60247 pVal->flags &= MEM_Int;
60248 pVal->flags |= MEM_Real;
60249 pVal->r = (double)LARGEST_INT64;
@@ -59922,19 +60252,19 @@
60252 }
60253 pVal->r = -pVal->r;
60254 sqlite3ValueApplyAffinity(pVal, affinity, enc);
60255 }
60256 }else if( op==TK_NULL ){
60257 pVal = valueNew(db, pCtx);
60258 if( pVal==0 ) goto no_mem;
60259 }
60260 #ifndef SQLITE_OMIT_BLOB_LITERAL
60261 else if( op==TK_BLOB ){
60262 int nVal;
60263 assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
60264 assert( pExpr->u.zToken[1]=='\'' );
60265 pVal = valueNew(db, pCtx);
60266 if( !pVal ) goto no_mem;
60267 zVal = &pExpr->u.zToken[2];
60268 nVal = sqlite3Strlen30(zVal)-1;
60269 assert( zVal[nVal]=='\'' );
60270 sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
@@ -59944,20 +60274,206 @@
60274
60275 if( pVal ){
60276 sqlite3VdbeMemStoreType(pVal);
60277 }
60278 *ppVal = pVal;
60279 return rc;
60280
60281 no_mem:
60282 db->mallocFailed = 1;
60283 sqlite3DbFree(db, zVal);
60284 assert( *ppVal==0 );
60285 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60286 if( pCtx==0 ) sqlite3ValueFree(pVal);
60287 #else
60288 assert( pCtx==0 ); sqlite3ValueFree(pVal);
60289 #endif
60290 return SQLITE_NOMEM;
60291 }
60292
60293 /*
60294 ** Create a new sqlite3_value object, containing the value of pExpr.
60295 **
60296 ** This only works for very simple expressions that consist of one constant
60297 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
60298 ** be converted directly into a value, then the value is allocated and
60299 ** a pointer written to *ppVal. The caller is responsible for deallocating
60300 ** the value by passing it to sqlite3ValueFree() later on. If the expression
60301 ** cannot be converted to a value, then *ppVal is set to NULL.
60302 */
60303 SQLITE_PRIVATE int sqlite3ValueFromExpr(
60304 sqlite3 *db, /* The database connection */
60305 Expr *pExpr, /* The expression to evaluate */
60306 u8 enc, /* Encoding to use */
60307 u8 affinity, /* Affinity to use */
60308 sqlite3_value **ppVal /* Write the new value here */
60309 ){
60310 return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
60311 }
60312
60313 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60314 /*
60315 ** The implementation of the sqlite_record() function. This function accepts
60316 ** a single argument of any type. The return value is a formatted database
60317 ** record (a blob) containing the argument value.
60318 **
60319 ** This is used to convert the value stored in the 'sample' column of the
60320 ** sqlite_stat3 table to the record format SQLite uses internally.
60321 */
60322 static void recordFunc(
60323 sqlite3_context *context,
60324 int argc,
60325 sqlite3_value **argv
60326 ){
60327 const int file_format = 1;
60328 int iSerial; /* Serial type */
60329 int nSerial; /* Bytes of space for iSerial as varint */
60330 int nVal; /* Bytes of space required for argv[0] */
60331 int nRet;
60332 sqlite3 *db;
60333 u8 *aRet;
60334
60335 iSerial = sqlite3VdbeSerialType(argv[0], file_format);
60336 nSerial = sqlite3VarintLen(iSerial);
60337 nVal = sqlite3VdbeSerialTypeLen(iSerial);
60338 db = sqlite3_context_db_handle(context);
60339
60340 nRet = 1 + nSerial + nVal;
60341 aRet = sqlite3DbMallocRaw(db, nRet);
60342 if( aRet==0 ){
60343 sqlite3_result_error_nomem(context);
60344 }else{
60345 aRet[0] = nSerial+1;
60346 sqlite3PutVarint(&aRet[1], iSerial);
60347 sqlite3VdbeSerialPut(&aRet[1+nSerial], nVal, argv[0], file_format);
60348 sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
60349 sqlite3DbFree(db, aRet);
60350 }
60351 }
60352
60353 /*
60354 ** Register built-in functions used to help read ANALYZE data.
60355 */
60356 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
60357 static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
60358 FUNCTION(sqlite_record, 1, 0, 0, recordFunc),
60359 };
60360 int i;
60361 FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
60362 FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
60363 for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
60364 sqlite3FuncDefInsert(pHash, &aFunc[i]);
60365 }
60366 }
60367
60368 /*
60369 ** This function is used to allocate and populate UnpackedRecord
60370 ** structures intended to be compared against sample index keys stored
60371 ** in the sqlite_stat4 table.
60372 **
60373 ** A single call to this function attempts to populates field iVal (leftmost
60374 ** is 0 etc.) of the unpacked record with a value extracted from expression
60375 ** pExpr. Extraction of values is possible if:
60376 **
60377 ** * (pExpr==0). In this case the value is assumed to be an SQL NULL,
60378 **
60379 ** * The expression is a bound variable, and this is a reprepare, or
60380 **
60381 ** * The sqlite3ValueFromExpr() function is able to extract a value
60382 ** from the expression (i.e. the expression is a literal value).
60383 **
60384 ** If a value can be extracted, the affinity passed as the 5th argument
60385 ** is applied to it before it is copied into the UnpackedRecord. Output
60386 ** parameter *pbOk is set to true if a value is extracted, or false
60387 ** otherwise.
60388 **
60389 ** When this function is called, *ppRec must either point to an object
60390 ** allocated by an earlier call to this function, or must be NULL. If it
60391 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
60392 ** is allocated (and *ppRec set to point to it) before returning.
60393 **
60394 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
60395 ** error if a value cannot be extracted from pExpr. If an error does
60396 ** occur, an SQLite error code is returned.
60397 */
60398 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
60399 Parse *pParse, /* Parse context */
60400 Index *pIdx, /* Index being probed */
60401 UnpackedRecord **ppRec, /* IN/OUT: Probe record */
60402 Expr *pExpr, /* The expression to extract a value from */
60403 u8 affinity, /* Affinity to use */
60404 int iVal, /* Array element to populate */
60405 int *pbOk /* OUT: True if value was extracted */
60406 ){
60407 int rc = SQLITE_OK;
60408 sqlite3_value *pVal = 0;
60409
60410 struct ValueNewStat4Ctx alloc;
60411 alloc.pParse = pParse;
60412 alloc.pIdx = pIdx;
60413 alloc.ppRec = ppRec;
60414 alloc.iVal = iVal;
60415
60416 /* Skip over any TK_COLLATE nodes */
60417 pExpr = sqlite3ExprSkipCollate(pExpr);
60418
60419 if( !pExpr ){
60420 pVal = valueNew(pParse->db, &alloc);
60421 if( pVal ){
60422 sqlite3VdbeMemSetNull((Mem*)pVal);
60423 *pbOk = 1;
60424 }
60425 }else if( pExpr->op==TK_VARIABLE
60426 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
60427 ){
60428 Vdbe *v;
60429 int iBindVar = pExpr->iColumn;
60430 sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
60431 if( (v = pParse->pReprepare)!=0 ){
60432 pVal = valueNew(pParse->db, &alloc);
60433 if( pVal ){
60434 rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
60435 if( rc==SQLITE_OK ){
60436 sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
60437 }
60438 pVal->db = pParse->db;
60439 *pbOk = 1;
60440 sqlite3VdbeMemStoreType((Mem*)pVal);
60441 }
60442 }else{
60443 *pbOk = 0;
60444 }
60445 }else{
60446 sqlite3 *db = pParse->db;
60447 rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
60448 *pbOk = (pVal!=0);
60449 }
60450
60451 assert( pVal==0 || pVal->db==pParse->db );
60452 return rc;
60453 }
60454
60455 /*
60456 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
60457 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
60458 ** the object.
60459 */
60460 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
60461 if( pRec ){
60462 int i;
60463 int nCol = pRec->pKeyInfo->nField+1;
60464 Mem *aMem = pRec->aMem;
60465 sqlite3 *db = aMem[0].db;
60466 for(i=0; i<nCol; i++){
60467 sqlite3DbFree(db, aMem[i].zMalloc);
60468 }
60469 sqlite3DbFree(db, pRec->pKeyInfo);
60470 sqlite3DbFree(db, pRec);
60471 }
60472 }
60473 #endif /* ifdef SQLITE_ENABLE_STAT4 */
60474
60475 /*
60476 ** Change the string value of an sqlite3_value object
60477 */
60478 SQLITE_PRIVATE void sqlite3ValueSetStr(
60479 sqlite3_value *v, /* Value to be set */
@@ -66073,11 +66589,11 @@
66589 ** value or convert mem[p2] to a different type.
66590 */
66591 assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
66592 if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
66593 assert( pOp->p2>0 );
66594 assert( pOp->p2<=(p->nMem-p->nCursor) );
66595 pOut = &aMem[pOp->p2];
66596 memAboutToChange(p, pOut);
66597 VdbeMemRelease(pOut);
66598 pOut->flags = MEM_Int;
66599 }
@@ -66084,34 +66600,34 @@
66600
66601 /* Sanity checking on other operands */
66602 #ifdef SQLITE_DEBUG
66603 if( (pOp->opflags & OPFLG_IN1)!=0 ){
66604 assert( pOp->p1>0 );
66605 assert( pOp->p1<=(p->nMem-p->nCursor) );
66606 assert( memIsValid(&aMem[pOp->p1]) );
66607 REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
66608 }
66609 if( (pOp->opflags & OPFLG_IN2)!=0 ){
66610 assert( pOp->p2>0 );
66611 assert( pOp->p2<=(p->nMem-p->nCursor) );
66612 assert( memIsValid(&aMem[pOp->p2]) );
66613 REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
66614 }
66615 if( (pOp->opflags & OPFLG_IN3)!=0 ){
66616 assert( pOp->p3>0 );
66617 assert( pOp->p3<=(p->nMem-p->nCursor) );
66618 assert( memIsValid(&aMem[pOp->p3]) );
66619 REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
66620 }
66621 if( (pOp->opflags & OPFLG_OUT2)!=0 ){
66622 assert( pOp->p2>0 );
66623 assert( pOp->p2<=(p->nMem-p->nCursor) );
66624 memAboutToChange(p, &aMem[pOp->p2]);
66625 }
66626 if( (pOp->opflags & OPFLG_OUT3)!=0 ){
66627 assert( pOp->p3>0 );
66628 assert( pOp->p3<=(p->nMem-p->nCursor) );
66629 memAboutToChange(p, &aMem[pOp->p3]);
66630 }
66631 #endif
66632
66633 switch( pOp->opcode ){
@@ -66200,11 +66716,11 @@
66716 **
66717 ** Write the current address onto register P1
66718 ** and then jump to address P2.
66719 */
66720 case OP_Gosub: { /* jump */
66721 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
66722 pIn1 = &aMem[pOp->p1];
66723 assert( (pIn1->flags & MEM_Dyn)==0 );
66724 memAboutToChange(p, pIn1);
66725 pIn1->flags = MEM_Int;
66726 pIn1->u.i = pc;
@@ -66416,11 +66932,11 @@
66932 #if 0 /* local variables moved into u.ab */
66933 int cnt;
66934 u16 nullFlag;
66935 #endif /* local variables moved into u.ab */
66936 u.ab.cnt = pOp->p3-pOp->p2;
66937 assert( pOp->p3<=(p->nMem-p->nCursor) );
66938 pOut->flags = u.ab.nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
66939 while( u.ab.cnt>0 ){
66940 pOut++;
66941 memAboutToChange(p, pOut);
66942 VdbeMemRelease(pOut);
@@ -66489,12 +67005,12 @@
67005 assert( u.ad.p1+u.ad.n<=u.ad.p2 || u.ad.p2+u.ad.n<=u.ad.p1 );
67006
67007 pIn1 = &aMem[u.ad.p1];
67008 pOut = &aMem[u.ad.p2];
67009 while( u.ad.n-- ){
67010 assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
67011 assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
67012 assert( memIsValid(pIn1) );
67013 memAboutToChange(p, pOut);
67014 u.ad.zMalloc = pOut->zMalloc;
67015 pOut->zMalloc = 0;
67016 sqlite3VdbeMemMove(pOut, pIn1);
@@ -66578,11 +67094,11 @@
67094 Mem *pMem;
67095 int i;
67096 #endif /* local variables moved into u.af */
67097 assert( p->nResColumn==pOp->p2 );
67098 assert( pOp->p1>0 );
67099 assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
67100
67101 /* If this statement has violated immediate foreign key constraints, do
67102 ** not return the number of rows modified. And do not RELEASE the statement
67103 ** transaction. It needs to be rolled back. */
67104 if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
@@ -66858,15 +67374,15 @@
67374 #endif /* local variables moved into u.ai */
67375
67376 u.ai.n = pOp->p5;
67377 u.ai.apVal = p->apArg;
67378 assert( u.ai.apVal || u.ai.n==0 );
67379 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
67380 pOut = &aMem[pOp->p3];
67381 memAboutToChange(p, pOut);
67382
67383 assert( u.ai.n==0 || (pOp->p2>0 && pOp->p2+u.ai.n<=(p->nMem-p->nCursor)+1) );
67384 assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+u.ai.n );
67385 u.ai.pArg = &aMem[pOp->p2];
67386 for(u.ai.i=0; u.ai.i<u.ai.n; u.ai.i++, u.ai.pArg++){
67387 assert( memIsValid(u.ai.pArg) );
67388 u.ai.apVal[u.ai.i] = u.ai.pArg;
@@ -67398,15 +67914,15 @@
67914 u.al.p2 = pOp->p2;
67915 #if SQLITE_DEBUG
67916 if( aPermute ){
67917 int k, mx = 0;
67918 for(k=0; k<u.al.n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
67919 assert( u.al.p1>0 && u.al.p1+mx<=(p->nMem-p->nCursor)+1 );
67920 assert( u.al.p2>0 && u.al.p2+mx<=(p->nMem-p->nCursor)+1 );
67921 }else{
67922 assert( u.al.p1>0 && u.al.p1+u.al.n<=(p->nMem-p->nCursor)+1 );
67923 assert( u.al.p2>0 && u.al.p2+u.al.n<=(p->nMem-p->nCursor)+1 );
67924 }
67925 #endif /* SQLITE_DEBUG */
67926 for(u.al.i=0; u.al.i<u.al.n; u.al.i++){
67927 u.al.idx = aPermute ? aPermute[u.al.i] : u.al.i;
67928 assert( memIsValid(&aMem[u.al.p1+u.al.idx]) );
@@ -67659,11 +68175,11 @@
68175 u.ao.p1 = pOp->p1;
68176 u.ao.p2 = pOp->p2;
68177 u.ao.pC = 0;
68178 memset(&u.ao.sMem, 0, sizeof(u.ao.sMem));
68179 assert( u.ao.p1<p->nCursor );
68180 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68181 u.ao.pDest = &aMem[pOp->p3];
68182 memAboutToChange(p, u.ao.pDest);
68183 u.ao.zRec = 0;
68184
68185 /* This block sets the variable u.ao.payloadSize to be the total number of
@@ -67959,11 +68475,11 @@
68475 u.ap.zAffinity = pOp->p4.z;
68476 assert( u.ap.zAffinity!=0 );
68477 assert( u.ap.zAffinity[pOp->p2]==0 );
68478 pIn1 = &aMem[pOp->p1];
68479 while( (u.ap.cAff = *(u.ap.zAffinity++))!=0 ){
68480 assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
68481 assert( memIsValid(pIn1) );
68482 ExpandBlob(pIn1);
68483 applyAffinity(pIn1, u.ap.cAff, encoding);
68484 pIn1++;
68485 }
@@ -68022,11 +68538,11 @@
68538 u.aq.nData = 0; /* Number of bytes of data space */
68539 u.aq.nHdr = 0; /* Number of bytes of header space */
68540 u.aq.nZero = 0; /* Number of zero bytes at the end of the record */
68541 u.aq.nField = pOp->p1;
68542 u.aq.zAffinity = pOp->p4.z;
68543 assert( u.aq.nField>0 && pOp->p2>0 && pOp->p2+u.aq.nField<=(p->nMem-p->nCursor)+1 );
68544 u.aq.pData0 = &aMem[u.aq.nField];
68545 u.aq.nField = pOp->p2;
68546 u.aq.pLast = &u.aq.pData0[u.aq.nField-1];
68547 u.aq.file_format = p->minWriteFileFormat;
68548
@@ -68088,11 +68604,11 @@
68604 for(u.aq.pRec=u.aq.pData0; u.aq.pRec<=u.aq.pLast; u.aq.pRec++){ /* serial data */
68605 u.aq.i += sqlite3VdbeSerialPut(&u.aq.zNewRecord[u.aq.i], (int)(u.aq.nByte-u.aq.i), u.aq.pRec,u.aq.file_format);
68606 }
68607 assert( u.aq.i==u.aq.nByte );
68608
68609 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68610 pOut->n = (int)u.aq.nByte;
68611 pOut->flags = MEM_Blob | MEM_Dyn;
68612 pOut->xDel = 0;
68613 if( u.aq.nZero ){
68614 pOut->u.nZero = u.aq.nZero;
@@ -68684,11 +69200,11 @@
69200 }else{
69201 u.ay.wrFlag = 0;
69202 }
69203 if( pOp->p5 & OPFLAG_P2ISREG ){
69204 assert( u.ay.p2>0 );
69205 assert( u.ay.p2<=(p->nMem-p->nCursor) );
69206 pIn2 = &aMem[u.ay.p2];
69207 assert( memIsValid(pIn2) );
69208 assert( (pIn2->flags & MEM_Int)!=0 );
69209 sqlite3VdbeMemIntegerify(pIn2);
69210 u.ay.p2 = (int)pIn2->u.i;
@@ -69235,11 +69751,11 @@
69751
69752 pIn3 = &aMem[pOp->p3];
69753 u.bf.aMx = &aMem[pOp->p4.i];
69754 /* Assert that the values of parameters P1 and P4 are in range. */
69755 assert( pOp->p4type==P4_INT32 );
69756 assert( pOp->p4.i>0 && pOp->p4.i<=(p->nMem-p->nCursor) );
69757 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69758
69759 /* Find the index cursor. */
69760 u.bf.pCx = p->apCsr[pOp->p1];
69761 assert( u.bf.pCx->deferredMoveto==0 );
@@ -69442,11 +69958,11 @@
69958 /* Assert that P3 is a valid memory cell. */
69959 assert( pOp->p3<=u.bh.pFrame->nMem );
69960 u.bh.pMem = &u.bh.pFrame->aMem[pOp->p3];
69961 }else{
69962 /* Assert that P3 is a valid memory cell. */
69963 assert( pOp->p3<=(p->nMem-p->nCursor) );
69964 u.bh.pMem = &aMem[pOp->p3];
69965 memAboutToChange(p, u.bh.pMem);
69966 }
69967 assert( memIsValid(u.bh.pMem) );
69968
@@ -70120,11 +70636,11 @@
70636 int res;
70637 UnpackedRecord r;
70638 #endif /* local variables moved into u.bt */
70639
70640 assert( pOp->p3>0 );
70641 assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
70642 assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70643 u.bt.pC = p->apCsr[pOp->p1];
70644 assert( u.bt.pC!=0 );
70645 u.bt.pCrsr = u.bt.pC->pCursor;
70646 if( ALWAYS(u.bt.pCrsr!=0) ){
@@ -70336,10 +70852,11 @@
70852 int nChange;
70853 #endif /* local variables moved into u.bx */
70854
70855 u.bx.nChange = 0;
70856 assert( p->readOnly==0 );
70857 assert( pOp->p1!=1 );
70858 assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
70859 rc = sqlite3BtreeClearTable(
70860 db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &u.bx.nChange : 0)
70861 );
70862 if( pOp->p3 ){
@@ -70542,11 +71059,11 @@
71059 assert( p->bIsReader );
71060 u.ca.nRoot = pOp->p2;
71061 assert( u.ca.nRoot>0 );
71062 u.ca.aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(u.ca.nRoot+1) );
71063 if( u.ca.aRoot==0 ) goto no_mem;
71064 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71065 u.ca.pnErr = &aMem[pOp->p3];
71066 assert( (u.ca.pnErr->flags & MEM_Int)!=0 );
71067 assert( (u.ca.pnErr->flags & (MEM_Str|MEM_Blob))==0 );
71068 pIn1 = &aMem[pOp->p1];
71069 for(u.ca.j=0; u.ca.j<u.ca.nRoot; u.ca.j++){
@@ -70978,11 +71495,11 @@
71495 u.cg.apVal[u.cg.i] = u.cg.pRec;
71496 memAboutToChange(p, u.cg.pRec);
71497 sqlite3VdbeMemStoreType(u.cg.pRec);
71498 }
71499 u.cg.ctx.pFunc = pOp->p4.pFunc;
71500 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71501 u.cg.ctx.pMem = u.cg.pMem = &aMem[pOp->p3];
71502 u.cg.pMem->n++;
71503 u.cg.ctx.s.flags = MEM_Null;
71504 u.cg.ctx.s.z = 0;
71505 u.cg.ctx.s.zMalloc = 0;
@@ -71027,11 +71544,11 @@
71544 */
71545 case OP_AggFinal: {
71546 #if 0 /* local variables moved into u.ch */
71547 Mem *pMem;
71548 #endif /* local variables moved into u.ch */
71549 assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
71550 u.ch.pMem = &aMem[pOp->p1];
71551 assert( (u.ch.pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
71552 rc = sqlite3VdbeMemFinalize(u.ch.pMem, pOp->p4.pFunc);
71553 if( rc ){
71554 sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(u.ch.pMem));
@@ -71458,11 +71975,11 @@
71975 sqlite3_context sContext;
71976 #endif /* local variables moved into u.co */
71977
71978 VdbeCursor *pCur = p->apCsr[pOp->p1];
71979 assert( pCur->pVtabCursor );
71980 assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71981 u.co.pDest = &aMem[pOp->p3];
71982 memAboutToChange(p, u.co.pDest);
71983 if( pCur->nullRow ){
71984 sqlite3VdbeMemSetNull(u.co.pDest);
71985 break;
@@ -76729,11 +77246,11 @@
77246 break;
77247 }
77248 case TK_UMINUS: {
77249 int v;
77250 if( sqlite3ExprIsInteger(p->pLeft, &v) ){
77251 assert( v!=(-2147483647-1) );
77252 *pValue = -v;
77253 rc = 1;
77254 }
77255 break;
77256 }
@@ -80389,11 +80906,11 @@
80906
80907 /* Ensure the default expression is something that sqlite3ValueFromExpr()
80908 ** can handle (i.e. not CURRENT_TIME etc.)
80909 */
80910 if( pDflt ){
80911 sqlite3_value *pVal = 0;
80912 if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
80913 db->mallocFailed = 1;
80914 return;
80915 }
80916 if( !pVal ){
@@ -80530,11 +81047,11 @@
81047 #endif /* SQLITE_ALTER_TABLE */
81048
81049 /************** End of alter.c ***********************************************/
81050 /************** Begin file analyze.c *****************************************/
81051 /*
81052 ** 2005-07-08
81053 **
81054 ** The author disclaims copyright to this source code. In place of
81055 ** a legal notice, here is a blessing:
81056 **
81057 ** May you do good and not evil.
@@ -80551,27 +81068,36 @@
81068 ** The following system tables are or have been supported:
81069 **
81070 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
81071 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
81072 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
81073 ** CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
81074 **
81075 ** Additional tables might be added in future releases of SQLite.
81076 ** The sqlite_stat2 table is not created or used unless the SQLite version
81077 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
81078 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
81079 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
81080 ** created and used by SQLite versions 3.7.9 and later and with
81081 ** SQLITE_ENABLE_STAT3 defined. The functionality of sqlite_stat3
81082 ** is a superset of sqlite_stat2. The sqlite_stat4 is an enhanced
81083 ** version of sqlite_stat3 and is only available when compiled with
81084 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.0 and later. It is
81085 ** not possible to enable both STAT3 and STAT4 at the same time. If they
81086 ** are both enabled, then STAT4 takes precedence.
81087 **
81088 ** For most applications, sqlite_stat1 provides all the statisics required
81089 ** for the query planner to make good choices.
81090 **
81091 ** Format of sqlite_stat1:
81092 **
81093 ** There is normally one row per index, with the index identified by the
81094 ** name in the idx column. The tbl column is the name of the table to
81095 ** which the index belongs. In each such row, the stat column will be
81096 ** a string consisting of a list of integers. The first integer in this
81097 ** list is the number of rows in the index. (This is the same as the
81098 ** number of rows in the table, except for partial indices.) The second
81099 ** integer is the average number of rows in the index that have the same
81100 ** value in the first column of the index. The third integer is the average
81101 ** number of rows in the index that have the same value for the first two
81102 ** columns. The N-th integer (for N>1) is the average number of rows in
81103 ** the index which have the same value for the first N-1 columns. For
@@ -80614,57 +81140,85 @@
81140 ** writes the sqlite_stat2 table. This version of SQLite only supports
81141 ** sqlite_stat3.
81142 **
81143 ** Format for sqlite_stat3:
81144 **
81145 ** The sqlite_stat3 format is a subset of sqlite_stat4. Hence, the
81146 ** sqlite_stat4 format will be described first. Further information
81147 ** about sqlite_stat3 follows the sqlite_stat4 description.
81148 **
81149 ** Format for sqlite_stat4:
81150 **
81151 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
81152 ** to aid the query planner in choosing good indices based on the values
81153 ** that indexed columns are compared against in the WHERE clauses of
81154 ** queries.
81155 **
81156 ** The sqlite_stat4 table contains multiple entries for each index.
81157 ** The idx column names the index and the tbl column is the table of the
81158 ** index. If the idx and tbl columns are the same, then the sample is
81159 ** of the INTEGER PRIMARY KEY. The sample column is a blob which is the
81160 ** binary encoding of a key from the index, with the trailing rowid
81161 ** omitted. The nEq column is a list of integers. The first integer
81162 ** is the approximate number of entries in the index whose left-most
81163 ** column exactly matches the left-most column of the sample. The second
81164 ** integer in nEq is the approximate number of entries in the index where
81165 ** the first two columns match the first two columns of the sample.
81166 ** And so forth. nLt is another list of integers that show the approximate
81167 ** number of entries that are strictly less than the sample. The first
81168 ** integer in nLt contains the number of entries in the index where the
81169 ** left-most column is less than the left-most column of the sample.
81170 ** The K-th integer in the nLt entry is the number of index entries
81171 ** where the first K columns are less than the first K columns of the
81172 ** sample. The nDLt column is like nLt except that it contains the
81173 ** number of distinct entries in the index that are less than the
81174 ** sample.
81175 **
81176 ** There can be an arbitrary number of sqlite_stat4 entries per index.
81177 ** The ANALYZE command will typically generate sqlite_stat4 tables
81178 ** that contain between 10 and 40 samples which are distributed across
81179 ** the key space, though not uniformly, and which include samples with
81180 ** large nEq values.
81181 **
81182 ** Format for sqlite_stat3 redux:
81183 **
81184 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
81185 ** looks at the left-most column of the index. The sqlite_stat3.sample
81186 ** column contains the actual value of the left-most column instead
81187 ** of a blob encoding of the complete index key as is found in
81188 ** sqlite_stat4.sample. The nEq, nLt, and nDLt entries of sqlite_stat3
81189 ** all contain just a single integer which is the same as the first
81190 ** integer in the equivalent columns in sqlite_stat4.
81191 */
81192 #ifndef SQLITE_OMIT_ANALYZE
81193
81194 #if defined(SQLITE_ENABLE_STAT4)
81195 # define IsStat4 1
81196 # define IsStat3 0
81197 #elif defined(SQLITE_ENABLE_STAT3)
81198 # define IsStat4 0
81199 # define IsStat3 1
81200 #else
81201 # define IsStat4 0
81202 # define IsStat3 0
81203 # undef SQLITE_STAT4_SAMPLES
81204 # define SQLITE_STAT4_SAMPLES 1
81205 #endif
81206 #define IsStat34 (IsStat3+IsStat4) /* 1 for STAT3 or STAT4. 0 otherwise */
81207
81208 /*
81209 ** This routine generates code that opens the sqlite_statN tables.
81210 ** The sqlite_stat1 table is always relevant. sqlite_stat2 is now
81211 ** obsolete. sqlite_stat3 and sqlite_stat4 are only opened when
81212 ** appropriate compile-time options are provided.
81213 **
81214 ** If the sqlite_statN tables do not previously exist, it is created.
 
 
81215 **
81216 ** Argument zWhere may be a pointer to a buffer containing a table name,
81217 ** or it may be a NULL pointer. If it is not NULL, then all entries in
81218 ** the sqlite_statN tables associated with the named table are deleted.
81219 ** If zWhere==0, then code is generated to delete all stat table entries.
 
81220 */
81221 static void openStatTable(
81222 Parse *pParse, /* Parsing context */
81223 int iDb, /* The database we are looking in */
81224 int iStatCur, /* Open the sqlite_stat1 table on this cursor */
@@ -80674,22 +81228,28 @@
81228 static const struct {
81229 const char *zName;
81230 const char *zCols;
81231 } aTable[] = {
81232 { "sqlite_stat1", "tbl,idx,stat" },
81233 #if defined(SQLITE_ENABLE_STAT4)
81234 { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
81235 { "sqlite_stat3", 0 },
81236 #elif defined(SQLITE_ENABLE_STAT3)
81237 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
81238 { "sqlite_stat4", 0 },
81239 #else
81240 { "sqlite_stat3", 0 },
81241 { "sqlite_stat4", 0 },
81242 #endif
81243 };
 
 
 
 
81244 int i;
81245 sqlite3 *db = pParse->db;
81246 Db *pDb;
81247 Vdbe *v = sqlite3GetVdbe(pParse);
81248 int aRoot[ArraySize(aTable)];
81249 u8 aCreateTbl[ArraySize(aTable)];
81250
81251 if( v==0 ) return;
81252 assert( sqlite3BtreeHoldsAllMutexes(db) );
81253 assert( sqlite3VdbeDb(v)==db );
81254 pDb = &db->aDb[iDb];
81255
@@ -80698,262 +81258,626 @@
81258 */
81259 for(i=0; i<ArraySize(aTable); i++){
81260 const char *zTab = aTable[i].zName;
81261 Table *pStat;
81262 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
81263 if( aTable[i].zCols ){
81264 /* The sqlite_statN table does not exist. Create it. Note that a
81265 ** side-effect of the CREATE TABLE statement is to leave the rootpage
81266 ** of the new table in register pParse->regRoot. This is important
81267 ** because the OpenWrite opcode below will be needing it. */
81268 sqlite3NestedParse(pParse,
81269 "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
81270 );
81271 aRoot[i] = pParse->regRoot;
81272 aCreateTbl[i] = OPFLAG_P2ISREG;
81273 }
81274 }else{
81275 /* The table already exists. If zWhere is not NULL, delete all entries
81276 ** associated with the table zWhere. If zWhere is NULL, delete the
81277 ** entire contents of the table. */
81278 aRoot[i] = pStat->tnum;
81279 aCreateTbl[i] = 0;
81280 sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
81281 if( zWhere ){
81282 sqlite3NestedParse(pParse,
81283 "DELETE FROM %Q.%s WHERE %s=%Q",
81284 pDb->zName, zTab, zWhereType, zWhere
81285 );
81286 }else{
81287 /* The sqlite_stat[134] table already exists. Delete all rows. */
81288 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
81289 }
81290 }
81291 }
81292
81293 /* Open the sqlite_stat[134] tables for writing. */
81294 for(i=0; aTable[i].zCols; i++){
81295 assert( i<ArraySize(aTable) );
81296 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
81297 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
81298 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
81299 }
81300 }
81301
81302 /*
81303 ** Recommended number of samples for sqlite_stat4
81304 */
81305 #ifndef SQLITE_STAT4_SAMPLES
81306 # define SQLITE_STAT4_SAMPLES 24
81307 #endif
81308
81309 /*
81310 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
81311 ** share an instance of the following structure to hold their state
81312 ** information.
81313 */
81314 typedef struct Stat4Accum Stat4Accum;
81315 typedef struct Stat4Sample Stat4Sample;
81316 struct Stat4Sample {
81317 tRowcnt *anEq; /* sqlite_stat4.nEq */
81318 tRowcnt *anDLt; /* sqlite_stat4.nDLt */
81319 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81320 tRowcnt *anLt; /* sqlite_stat4.nLt */
81321 i64 iRowid; /* Rowid in main table of the key */
81322 u8 isPSample; /* True if a periodic sample */
81323 int iCol; /* If !isPSample, the reason for inclusion */
81324 u32 iHash; /* Tiebreaker hash */
81325 #endif
81326 };
81327 struct Stat4Accum {
81328 tRowcnt nRow; /* Number of rows in the entire table */
81329 tRowcnt nPSample; /* How often to do a periodic sample */
81330 int nCol; /* Number of columns in index + rowid */
81331 int mxSample; /* Maximum number of samples to accumulate */
81332 Stat4Sample current; /* Current row as a Stat4Sample */
81333 u32 iPrn; /* Pseudo-random number used for sampling */
81334 Stat4Sample *aBest; /* Array of (nCol-1) best samples */
81335 int iMin; /* Index in a[] of entry with minimum score */
81336 int nSample; /* Current number of samples */
81337 int iGet; /* Index of current sample accessed by stat_get() */
81338 Stat4Sample *a; /* Array of mxSample Stat4Sample objects */
 
 
 
81339 };
81340
 
81341 /*
81342 ** Implementation of the stat_init(N,C) SQL function. The two parameters
81343 ** are the number of rows in the table or index (C) and the number of columns
81344 ** in the index (N). The second argument (C) is only used for STAT3 and STAT4.
81345 **
81346 ** This routine allocates the Stat4Accum object in heap memory. The return
81347 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
81348 ** the size of the blob is sizeof(void*) bytes).
81349 */
81350 static void statInit(
81351 sqlite3_context *context,
81352 int argc,
81353 sqlite3_value **argv
81354 ){
81355 Stat4Accum *p;
81356 int nCol; /* Number of columns in index being sampled */
81357 int nColUp; /* nCol rounded up for alignment */
81358 int n; /* Bytes of space to allocate */
81359 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81360 int mxSample = SQLITE_STAT4_SAMPLES;
81361 #endif
81362
81363 /* Decode the three function arguments */
81364 UNUSED_PARAMETER(argc);
81365 nCol = sqlite3_value_int(argv[0]);
81366 assert( nCol>1 ); /* >1 because it includes the rowid column */
81367 nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
81368
81369 /* Allocate the space required for the Stat4Accum object */
81370 n = sizeof(*p)
81371 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anEq */
81372 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anDLt */
81373 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81374 + sizeof(tRowcnt)*nColUp /* Stat4Accum.anLt */
81375 + sizeof(Stat4Sample)*(nCol+mxSample) /* Stat4Accum.aBest[], a[] */
81376 + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
81377 #endif
81378 ;
81379 p = sqlite3MallocZero(n);
81380 if( p==0 ){
81381 sqlite3_result_error_nomem(context);
81382 return;
81383 }
81384
81385 p->nRow = 0;
81386 p->nCol = nCol;
81387 p->current.anDLt = (tRowcnt*)&p[1];
81388 p->current.anEq = &p->current.anDLt[nColUp];
81389
81390 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81391 {
81392 u8 *pSpace; /* Allocated space not yet assigned */
81393 int i; /* Used to iterate through p->aSample[] */
81394
81395 p->iGet = -1;
81396 p->mxSample = mxSample;
81397 p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
81398 p->current.anLt = &p->current.anEq[nColUp];
81399 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
81400
81401 /* Set up the Stat4Accum.a[] and aBest[] arrays */
81402 p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
81403 p->aBest = &p->a[mxSample];
81404 pSpace = (u8*)(&p->a[mxSample+nCol]);
81405 for(i=0; i<(mxSample+nCol); i++){
81406 p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81407 p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81408 p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81409 }
81410 assert( (pSpace - (u8*)p)==n );
81411
81412 for(i=0; i<nCol; i++){
81413 p->aBest[i].iCol = i;
81414 }
81415 }
81416 #endif
81417
81418 /* Return a pointer to the allocated object to the caller */
81419 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
81420 }
81421 static const FuncDef statInitFuncdef = {
81422 1+IsStat34, /* nArg */
81423 SQLITE_UTF8, /* iPrefEnc */
81424 0, /* flags */
81425 0, /* pUserData */
81426 0, /* pNext */
81427 statInit, /* xFunc */
81428 0, /* xStep */
81429 0, /* xFinalize */
81430 "stat_init", /* zName */
81431 0, /* pHash */
81432 0 /* pDestructor */
81433 };
81434
81435 #ifdef SQLITE_ENABLE_STAT4
81436 /*
81437 ** pNew and pOld are both candidate non-periodic samples selected for
81438 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
81439 ** considering only any trailing columns and the sample hash value, this
81440 ** function returns true if sample pNew is to be preferred over pOld.
81441 ** In other words, if we assume that the cardinalities of the selected
81442 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
81443 **
81444 ** This function assumes that for each argument sample, the contents of
81445 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
81446 */
81447 static int sampleIsBetterPost(
81448 Stat4Accum *pAccum,
81449 Stat4Sample *pNew,
81450 Stat4Sample *pOld
81451 ){
81452 int nCol = pAccum->nCol;
81453 int i;
81454 assert( pNew->iCol==pOld->iCol );
81455 for(i=pNew->iCol+1; i<nCol; i++){
81456 if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
81457 if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
81458 }
81459 if( pNew->iHash>pOld->iHash ) return 1;
81460 return 0;
81461 }
81462 #endif
81463
81464 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81465 /*
81466 ** Return true if pNew is to be preferred over pOld.
81467 **
81468 ** This function assumes that for each argument sample, the contents of
81469 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
81470 */
81471 static int sampleIsBetter(
81472 Stat4Accum *pAccum,
81473 Stat4Sample *pNew,
81474 Stat4Sample *pOld
81475 ){
81476 tRowcnt nEqNew = pNew->anEq[pNew->iCol];
81477 tRowcnt nEqOld = pOld->anEq[pOld->iCol];
81478
81479 assert( pOld->isPSample==0 && pNew->isPSample==0 );
81480 assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
81481
81482 if( (nEqNew>nEqOld) ) return 1;
81483 #ifdef SQLITE_ENABLE_STAT4
81484 if( nEqNew==nEqOld ){
81485 if( pNew->iCol<pOld->iCol ) return 1;
81486 return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
81487 }
81488 return 0;
81489 #else
81490 return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
81491 #endif
81492 }
81493
81494 /*
81495 ** Copy the contents of object (*pFrom) into (*pTo).
81496 */
81497 void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
81498 pTo->iRowid = pFrom->iRowid;
81499 pTo->isPSample = pFrom->isPSample;
81500 pTo->iCol = pFrom->iCol;
81501 pTo->iHash = pFrom->iHash;
81502 memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
81503 memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
81504 memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
81505 }
81506
81507 /*
81508 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
81509 ** remove the least desirable sample from p->a[] to make room.
81510 */
81511 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
81512 Stat4Sample *pSample;
81513 int i;
81514
81515 assert( IsStat4 || nEqZero==0 );
81516
81517 #ifdef SQLITE_ENABLE_STAT4
81518 if( pNew->isPSample==0 ){
81519 Stat4Sample *pUpgrade = 0;
81520 assert( pNew->anEq[pNew->iCol]>0 );
81521
81522 /* This sample is being added because the prefix that ends in column
81523 ** iCol occurs many times in the table. However, if we have already
81524 ** added a sample that shares this prefix, there is no need to add
81525 ** this one. Instead, upgrade the priority of the highest priority
81526 ** existing sample that shares this prefix. */
81527 for(i=p->nSample-1; i>=0; i--){
81528 Stat4Sample *pOld = &p->a[i];
81529 if( pOld->anEq[pNew->iCol]==0 ){
81530 if( pOld->isPSample ) return;
81531 assert( pOld->iCol>pNew->iCol );
81532 assert( sampleIsBetter(p, pNew, pOld) );
81533 if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
81534 pUpgrade = pOld;
81535 }
81536 }
81537 }
81538 if( pUpgrade ){
81539 pUpgrade->iCol = pNew->iCol;
81540 pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
81541 goto find_new_min;
81542 }
81543 }
81544 #endif
81545
81546 /* If necessary, remove sample iMin to make room for the new sample. */
81547 if( p->nSample>=p->mxSample ){
81548 Stat4Sample *pMin = &p->a[p->iMin];
81549 tRowcnt *anEq = pMin->anEq;
81550 tRowcnt *anLt = pMin->anLt;
81551 tRowcnt *anDLt = pMin->anDLt;
81552 memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
81553 pSample = &p->a[p->nSample-1];
81554 pSample->anEq = anEq;
81555 pSample->anDLt = anDLt;
81556 pSample->anLt = anLt;
81557 p->nSample = p->mxSample-1;
81558 }
81559
81560 /* The "rows less-than" for the rowid column must be greater than that
81561 ** for the last sample in the p->a[] array. Otherwise, the samples would
81562 ** be out of order. */
81563 #ifdef SQLITE_ENABLE_STAT4
81564 assert( p->nSample==0
81565 || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
81566 #endif
81567
81568 /* Insert the new sample */
81569 pSample = &p->a[p->nSample];
81570 sampleCopy(p, pSample, pNew);
81571 p->nSample++;
81572
81573 /* Zero the first nEqZero entries in the anEq[] array. */
81574 memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
81575
81576 #ifdef SQLITE_ENABLE_STAT4
81577 find_new_min:
81578 #endif
81579 if( p->nSample>=p->mxSample ){
81580 int iMin = -1;
81581 for(i=0; i<p->mxSample; i++){
81582 if( p->a[i].isPSample ) continue;
81583 if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
81584 iMin = i;
81585 }
81586 }
81587 assert( iMin>=0 );
81588 p->iMin = iMin;
81589 }
81590 }
81591 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81592
81593 /*
81594 ** Field iChng of the index being scanned has changed. So at this point
81595 ** p->current contains a sample that reflects the previous row of the
81596 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
81597 ** correct at this point.
81598 */
81599 static void samplePushPrevious(Stat4Accum *p, int iChng){
81600 #ifdef SQLITE_ENABLE_STAT4
81601 int i;
81602
81603 /* Check if any samples from the aBest[] array should be pushed
81604 ** into IndexSample.a[] at this point. */
81605 for(i=(p->nCol-2); i>=iChng; i--){
81606 Stat4Sample *pBest = &p->aBest[i];
81607 pBest->anEq[i] = p->current.anEq[i];
81608 if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
81609 sampleInsert(p, pBest, i);
81610 }
81611 }
81612
81613 /* Update the anEq[] fields of any samples already collected. */
81614 for(i=p->nSample-1; i>=0; i--){
81615 int j;
81616 for(j=iChng; j<p->nCol; j++){
81617 if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
81618 }
81619 }
81620 #endif
81621
81622 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
81623 if( iChng==0 ){
81624 tRowcnt nLt = p->current.anLt[0];
81625 tRowcnt nEq = p->current.anEq[0];
81626
81627 /* Check if this is to be a periodic sample. If so, add it. */
81628 if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
81629 p->current.isPSample = 1;
81630 sampleInsert(p, &p->current, 0);
81631 p->current.isPSample = 0;
81632 }else
81633
81634 /* Or if it is a non-periodic sample. Add it in this case too. */
81635 if( p->nSample<p->mxSample
81636 || sampleIsBetter(p, &p->current, &p->a[p->iMin])
81637 ){
81638 sampleInsert(p, &p->current, 0);
81639 }
81640 }
81641 #endif
81642 }
81643
81644 /*
81645 ** Implementation of the stat_push SQL function: stat_push(P,R,C)
81646 ** Arguments:
81647 **
81648 ** P Pointer to the Stat4Accum object created by stat_init()
81649 ** C Index of left-most column to differ from previous row
81650 ** R Rowid for the current row
81651 **
81652 ** The SQL function always returns NULL.
81653 **
81654 ** The R parameter is only used for STAT3 and STAT4.
81655 */
81656 static void statPush(
81657 sqlite3_context *context,
81658 int argc,
81659 sqlite3_value **argv
81660 ){
81661 int i;
81662
81663 /* The three function arguments */
81664 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81665 int iChng = sqlite3_value_int(argv[1]);
81666
81667 assert( p->nCol>1 ); /* Includes rowid field */
81668 assert( iChng<p->nCol );
81669
81670 if( p->nRow==0 ){
81671 /* This is the first call to this function. Do initialization. */
81672 for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
81673 }else{
81674 /* Second and subsequent calls get processed here */
81675 samplePushPrevious(p, iChng);
81676
81677 /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
81678 ** to the current row of the index. */
81679 for(i=0; i<iChng; i++){
81680 p->current.anEq[i]++;
81681 }
81682 for(i=iChng; i<p->nCol; i++){
81683 p->current.anDLt[i]++;
81684 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81685 p->current.anLt[i] += p->current.anEq[i];
81686 #endif
81687 p->current.anEq[i] = 1;
81688 }
81689 }
81690 p->nRow++;
81691 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81692 p->current.iRowid = sqlite3_value_int64(argv[2]);
81693 p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
81694 #endif
81695
81696 #ifdef SQLITE_ENABLE_STAT4
81697 {
81698 tRowcnt nLt = p->current.anLt[p->nCol-1];
81699
81700 /* Check if this is to be a periodic sample. If so, add it. */
81701 if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
81702 p->current.isPSample = 1;
81703 p->current.iCol = 0;
81704 sampleInsert(p, &p->current, p->nCol-1);
81705 p->current.isPSample = 0;
81706 }
81707
81708 /* Update the aBest[] array. */
81709 for(i=0; i<(p->nCol-1); i++){
81710 p->current.iCol = i;
81711 if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
81712 sampleCopy(p, &p->aBest[i], &p->current);
81713 }
81714 }
81715 }
81716 #endif
81717 }
81718 static const FuncDef statPushFuncdef = {
81719 2+IsStat34, /* nArg */
81720 SQLITE_UTF8, /* iPrefEnc */
81721 0, /* flags */
81722 0, /* pUserData */
81723 0, /* pNext */
81724 statPush, /* xFunc */
81725 0, /* xStep */
81726 0, /* xFinalize */
81727 "stat_push", /* zName */
81728 0, /* pHash */
81729 0 /* pDestructor */
81730 };
81731
81732 #define STAT_GET_STAT1 0 /* "stat" column of stat1 table */
81733 #define STAT_GET_ROWID 1 /* "rowid" column of stat[34] entry */
81734 #define STAT_GET_NEQ 2 /* "neq" column of stat[34] entry */
81735 #define STAT_GET_NLT 3 /* "nlt" column of stat[34] entry */
81736 #define STAT_GET_NDLT 4 /* "ndlt" column of stat[34] entry */
81737
81738 /*
81739 ** Implementation of the stat_get(P,J) SQL function. This routine is
81740 ** used to query the results. Content is returned for parameter J
81741 ** which is one of the STAT_GET_xxxx values defined above.
81742 **
81743 ** If neither STAT3 nor STAT4 are enabled, then J is always
81744 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
81745 ** a one-parameter function, stat_get(P), that always returns the
81746 ** stat1 table entry information.
81747 */
81748 static void statGet(
81749 sqlite3_context *context,
81750 int argc,
81751 sqlite3_value **argv
81752 ){
81753 Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
81754 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81755 /* STAT3 and STAT4 have a parameter on this routine. */
81756 int eCall = sqlite3_value_int(argv[1]);
81757 assert( argc==2 );
81758 assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
81759 || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
81760 || eCall==STAT_GET_NDLT
81761 );
81762 if( eCall==STAT_GET_STAT1 )
81763 #else
81764 assert( argc==1 );
81765 #endif
81766 {
81767 /* Return the value to store in the "stat" column of the sqlite_stat1
81768 ** table for this index.
81769 **
81770 ** The value is a string composed of a list of integers describing
81771 ** the index. The first integer in the list is the total number of
81772 ** entries in the index. There is one additional integer in the list
81773 ** for each indexed column. This additional integer is an estimate of
81774 ** the number of rows matched by a stabbing query on the index using
81775 ** a key with the corresponding number of fields. In other words,
81776 ** if the index is on columns (a,b) and the sqlite_stat1 value is
81777 ** "100 10 2", then SQLite estimates that:
81778 **
81779 ** * the index contains 100 rows,
81780 ** * "WHERE a=?" matches 10 rows, and
81781 ** * "WHERE a=? AND b=?" matches 2 rows.
81782 **
81783 ** If D is the count of distinct values and K is the total number of
81784 ** rows, then each estimate is computed as:
81785 **
81786 ** I = (K+D-1)/D
81787 */
81788 char *z;
81789 int i;
81790
81791 char *zRet = sqlite3MallocZero(p->nCol * 25);
81792 if( zRet==0 ){
81793 sqlite3_result_error_nomem(context);
81794 return;
81795 }
81796
81797 sqlite3_snprintf(24, zRet, "%lld", p->nRow);
81798 z = zRet + sqlite3Strlen30(zRet);
81799 for(i=0; i<(p->nCol-1); i++){
81800 i64 nDistinct = p->current.anDLt[i] + 1;
81801 i64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
81802 sqlite3_snprintf(24, z, " %lld", iVal);
81803 z += sqlite3Strlen30(z);
81804 assert( p->current.anEq[i] );
81805 }
81806 assert( z[0]=='\0' && z>zRet );
81807
81808 sqlite3_result_text(context, zRet, -1, sqlite3_free);
81809 }
81810 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81811 else if( eCall==STAT_GET_ROWID ){
81812 if( p->iGet<0 ){
81813 samplePushPrevious(p, 0);
81814 p->iGet = 0;
81815 }
81816 if( p->iGet<p->nSample ){
81817 sqlite3_result_int64(context, p->a[p->iGet].iRowid);
81818 }
81819 }else{
81820 tRowcnt *aCnt = 0;
81821
81822 assert( p->iGet<p->nSample );
81823 switch( eCall ){
81824 case STAT_GET_NEQ: aCnt = p->a[p->iGet].anEq; break;
81825 case STAT_GET_NLT: aCnt = p->a[p->iGet].anLt; break;
81826 default: {
81827 aCnt = p->a[p->iGet].anDLt;
81828 p->iGet++;
81829 break;
81830 }
81831 }
81832
81833 if( IsStat3 ){
81834 sqlite3_result_int64(context, (i64)aCnt[0]);
81835 }else{
81836 char *zRet = sqlite3MallocZero(p->nCol * 25);
81837 if( zRet==0 ){
81838 sqlite3_result_error_nomem(context);
81839 }else{
81840 int i;
81841 char *z = zRet;
81842 for(i=0; i<p->nCol; i++){
81843 sqlite3_snprintf(24, z, "%lld ", aCnt[i]);
81844 z += sqlite3Strlen30(z);
81845 }
81846 assert( z[0]=='\0' && z>zRet );
81847 z[-1] = '\0';
81848 sqlite3_result_text(context, zRet, -1, sqlite3_free);
81849 }
81850 }
81851 }
81852 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
81853 }
81854 static const FuncDef statGetFuncdef = {
81855 1+IsStat34, /* nArg */
81856 SQLITE_UTF8, /* iPrefEnc */
81857 0, /* flags */
81858 0, /* pUserData */
81859 0, /* pNext */
81860 statGet, /* xFunc */
81861 0, /* xStep */
81862 0, /* xFinalize */
81863 "stat_get", /* zName */
81864 0, /* pHash */
81865 0 /* pDestructor */
81866 };
81867
81868 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
81869 assert( regOut!=regStat4 && regOut!=regStat4+1 );
81870 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81871 sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
81872 #else
81873 assert( iParam==STAT_GET_STAT1 );
81874 #endif
81875 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
81876 sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
81877 sqlite3VdbeChangeP5(v, 1 + IsStat34);
81878 }
81879
81880 /*
81881 ** Generate code to do an analysis of all indices associated with
81882 ** a single table.
81883 */
@@ -80960,46 +81884,35 @@
81884 static void analyzeOneTable(
81885 Parse *pParse, /* Parser context */
81886 Table *pTab, /* Table whose indices are to be analyzed */
81887 Index *pOnlyIdx, /* If not NULL, only analyze this one index */
81888 int iStatCur, /* Index of VdbeCursor that writes the sqlite_stat1 table */
81889 int iMem, /* Available memory locations begin here */
81890 int iTab /* Next available cursor */
81891 ){
81892 sqlite3 *db = pParse->db; /* Database handle */
81893 Index *pIdx; /* An index to being analyzed */
81894 int iIdxCur; /* Cursor open on index being analyzed */
81895 int iTabCur; /* Table cursor */
81896 Vdbe *v; /* The virtual machine being built up */
81897 int i; /* Loop counter */
 
 
81898 int jZeroRows = -1; /* Jump from here if number of rows is zero */
81899 int iDb; /* Index of database containing pTab */
81900 u8 needTableCnt = 1; /* True to count the table */
81901 int regNewRowid = iMem++; /* Rowid for the inserted record */
81902 int regStat4 = iMem++; /* Register to hold Stat4Accum object */
81903 int regChng = iMem++; /* Index of changed index field */
81904 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81905 int regRowid = iMem++; /* Rowid argument passed to stat_push() */
81906 #endif
81907 int regTemp = iMem++; /* Temporary use register */
81908 int regTabname = iMem++; /* Register containing table name */
81909 int regIdxname = iMem++; /* Register containing index name */
81910 int regStat1 = iMem++; /* Value for the stat column of sqlite_stat1 */
81911 int regPrev = iMem; /* MUST BE LAST (see below) */
81912
81913 pParse->nMem = MAX(pParse->nMem, iMem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81914 v = sqlite3GetVdbe(pParse);
81915 if( v==0 || NEVER(pTab==0) ){
81916 return;
81917 }
81918 if( pTab->tnum==0 ){
@@ -81019,217 +81932,230 @@
81932 db->aDb[iDb].zName ) ){
81933 return;
81934 }
81935 #endif
81936
81937 /* Establish a read-lock on the table at the shared-cache level.
81938 ** Open a read-only cursor on the table. Also allocate a cursor number
81939 ** to use for scanning indexes (iIdxCur). No index cursor is opened at
81940 ** this time though. */
81941 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
81942 iTabCur = iTab++;
81943 iIdxCur = iTab++;
81944 pParse->nTab = MAX(pParse->nTab, iTab);
81945 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
81946 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
81947
81948 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
81949 int nCol; /* Number of columns indexed by pIdx */
81950 KeyInfo *pKey; /* KeyInfo structure for pIdx */
81951 int *aGotoChng; /* Array of jump instruction addresses */
81952 int addrRewind; /* Address of "OP_Rewind iIdxCur" */
81953 int addrGotoChng0; /* Address of "Goto addr_chng_0" */
81954 int addrNextRow; /* Address of "next_row:" */
81955
81956 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
81957 if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
81958 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
81959 nCol = pIdx->nColumn;
81960 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
81961 if( aGotoChng==0 ) continue;
81962 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
 
 
 
 
 
 
 
 
 
81963
81964 /* Populate the register containing the index name. */
81965 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
81966
81967 /*
81968 ** Pseudo-code for loop that calls stat_push():
81969 **
81970 ** Rewind csr
81971 ** if eof(csr) goto end_of_scan;
81972 ** regChng = 0
81973 ** goto chng_addr_0;
81974 **
81975 ** next_row:
81976 ** regChng = 0
81977 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
81978 ** regChng = 1
81979 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
81980 ** ...
81981 ** regChng = N
81982 ** goto chng_addr_N
81983 **
81984 ** chng_addr_0:
81985 ** regPrev(0) = idx(0)
81986 ** chng_addr_1:
81987 ** regPrev(1) = idx(1)
81988 ** ...
81989 **
81990 ** chng_addr_N:
81991 ** regRowid = idx(rowid)
81992 ** stat_push(P, regChng, regRowid)
81993 ** Next csr
81994 ** if !eof(csr) goto next_row;
81995 **
81996 ** end_of_scan:
81997 */
81998
81999 /* Make sure there are enough memory cells allocated to accommodate
82000 ** the regPrev array and a trailing rowid (the rowid slot is required
82001 ** when building a record to insert into the sample column of
82002 ** the sqlite_stat4 table. */
82003 pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
82004
82005 /* Open a read-only cursor on the index being analyzed. */
82006 assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
82007 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
82008 sqlite3VdbeChangeP4(v, -1, (char*)pKey, P4_KEYINFO_HANDOFF);
82009 VdbeComment((v, "%s", pIdx->zName));
82010
82011 /* Invoke the stat_init() function. The arguments are:
82012 **
82013 ** (1) the number of columns in the index including the rowid,
82014 ** (2) the number of rows in the index,
82015 **
82016 ** The second argument is only used for STAT3 and STAT4
82017 */
82018 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82019 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
82020 #endif
82021 sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
82022 sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
82023 sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
82024 sqlite3VdbeChangeP5(v, 1+IsStat34);
82025
82026 /* Implementation of the following:
82027 **
82028 ** Rewind csr
82029 ** if eof(csr) goto end_of_scan;
82030 ** regChng = 0
82031 ** goto next_push_0;
82032 **
82033 */
82034 addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
82035 sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
82036 addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
82037
82038 /*
82039 ** next_row:
82040 ** regChng = 0
82041 ** if( idx(0) != regPrev(0) ) goto chng_addr_0
82042 ** regChng = 1
82043 ** if( idx(1) != regPrev(1) ) goto chng_addr_1
82044 ** ...
82045 ** regChng = N
82046 ** goto chng_addr_N
82047 */
82048 addrNextRow = sqlite3VdbeCurrentAddr(v);
82049 for(i=0; i<nCol; i++){
82050 char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
82051 sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
82052 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
82053 aGotoChng[i] =
82054 sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
82055 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
82056 }
82057 sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
82058 aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
82059
82060 /*
82061 ** chng_addr_0:
82062 ** regPrev(0) = idx(0)
82063 ** chng_addr_1:
82064 ** regPrev(1) = idx(1)
82065 ** ...
82066 */
82067 sqlite3VdbeJumpHere(v, addrGotoChng0);
82068 for(i=0; i<nCol; i++){
82069 sqlite3VdbeJumpHere(v, aGotoChng[i]);
82070 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
82071 }
82072
82073 /*
82074 ** chng_addr_N:
82075 ** regRowid = idx(rowid) // STAT34 only
82076 ** stat_push(P, regChng, regRowid) // 3rd parameter STAT34 only
82077 ** Next csr
82078 ** if !eof(csr) goto next_row;
82079 */
82080 sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
82081 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82082 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
82083 assert( regRowid==(regStat4+2) );
82084 #endif
82085 assert( regChng==(regStat4+1) );
82086 sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
82087 sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
82088 sqlite3VdbeChangeP5(v, 2+IsStat34);
82089 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
82090
82091 /* Add the entry to the stat1 table. */
82092 callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
82093 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
82094 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
82095 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
82096 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
82097
82098 /* Add the entries to the stat3 or stat4 table. */
82099 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82100 {
82101 int regEq = regStat1;
82102 int regLt = regStat1+1;
82103 int regDLt = regStat1+2;
82104 int regSample = regStat1+3;
82105 int regCol = regStat1+4;
82106 int regSampleRowid = regCol + nCol;
82107 int addrNext;
82108 int addrIsNull;
82109
82110 pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
82111
82112 addrNext = sqlite3VdbeCurrentAddr(v);
82113 callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
82114 addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
82115 callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
82116 callStatGet(v, regStat4, STAT_GET_NLT, regLt);
82117 callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
82118 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, addrNext, regSampleRowid);
82119 #ifdef SQLITE_ENABLE_STAT3
82120 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
82121 pIdx->aiColumn[0], regSample);
82122 #else
82123 for(i=0; i<nCol; i++){
82124 int iCol = pIdx->aiColumn[i];
82125 sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
82126 }
82127 sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
82128 #endif
82129 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
82130 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
82131 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
82132 sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
82133 sqlite3VdbeJumpHere(v, addrIsNull);
82134 }
82135 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82136
82137 /* End of analysis */
82138 sqlite3VdbeJumpHere(v, addrRewind);
82139 sqlite3DbFree(db, aGotoChng);
82140 }
82141
 
 
 
 
 
 
 
 
 
 
 
 
 
82142
82143 /* Create a single sqlite_stat1 entry containing NULL as the index
82144 ** name and the row count as the content.
82145 */
82146 if( pOnlyIdx==0 && needTableCnt ){
 
82147 VdbeComment((v, "%s", pTab->zName));
82148 sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
 
82149 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
82150 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
82151 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
82152 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
82153 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
82154 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
82155 sqlite3VdbeJumpHere(v, jZeroRows);
82156 }
 
82157 }
82158
82159
82160 /*
82161 ** Generate code that will cause the most recent index analysis to
@@ -81249,20 +82175,22 @@
82175 sqlite3 *db = pParse->db;
82176 Schema *pSchema = db->aDb[iDb].pSchema; /* Schema of database iDb */
82177 HashElem *k;
82178 int iStatCur;
82179 int iMem;
82180 int iTab;
82181
82182 sqlite3BeginWriteOperation(pParse, 0, iDb);
82183 iStatCur = pParse->nTab;
82184 pParse->nTab += 3;
82185 openStatTable(pParse, iDb, iStatCur, 0, 0);
82186 iMem = pParse->nMem+1;
82187 iTab = pParse->nTab;
82188 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82189 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
82190 Table *pTab = (Table*)sqliteHashData(k);
82191 analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
82192 }
82193 loadAnalysis(pParse, iDb);
82194 }
82195
82196 /*
@@ -81283,11 +82211,11 @@
82211 if( pOnlyIdx ){
82212 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
82213 }else{
82214 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
82215 }
82216 analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
82217 loadAnalysis(pParse, iDb);
82218 }
82219
82220 /*
82221 ** Generate code for the ANALYZE command. The parser calls this routine
@@ -81365,10 +82293,47 @@
82293 typedef struct analysisInfo analysisInfo;
82294 struct analysisInfo {
82295 sqlite3 *db;
82296 const char *zDatabase;
82297 };
82298
82299 /*
82300 ** The first argument points to a nul-terminated string containing a
82301 ** list of space separated integers. Read the first nOut of these into
82302 ** the array aOut[].
82303 */
82304 static void decodeIntArray(
82305 char *zIntArray,
82306 int nOut,
82307 tRowcnt *aOut,
82308 int *pbUnordered
82309 ){
82310 char *z = zIntArray;
82311 int c;
82312 int i;
82313 tRowcnt v;
82314
82315 assert( pbUnordered==0 || *pbUnordered==0 );
82316
82317 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82318 if( z==0 ) z = "";
82319 #else
82320 if( NEVER(z==0) ) z = "";
82321 #endif
82322 for(i=0; *z && i<nOut; i++){
82323 v = 0;
82324 while( (c=z[0])>='0' && c<='9' ){
82325 v = v*10 + c - '0';
82326 z++;
82327 }
82328 aOut[i] = v;
82329 if( *z==' ' ) z++;
82330 }
82331 if( pbUnordered && strcmp(z, "unordered")==0 ){
82332 *pbUnordered = 1;
82333 }
82334 }
82335
82336 /*
82337 ** This callback is invoked once for each index when reading the
82338 ** sqlite_stat1 table.
82339 **
@@ -81381,12 +82346,10 @@
82346 */
82347 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
82348 analysisInfo *pInfo = (analysisInfo*)pData;
82349 Index *pIndex;
82350 Table *pTable;
 
 
82351 const char *z;
82352
82353 assert( argc==3 );
82354 UNUSED_PARAMETER2(NotUsed, argc);
82355
@@ -81400,45 +82363,35 @@
82363 if( argv[1] ){
82364 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
82365 }else{
82366 pIndex = 0;
82367 }
 
82368 z = argv[2];
82369
82370 if( pIndex ){
82371 int bUnordered = 0;
82372 decodeIntArray((char*)z, pIndex->nColumn+1, pIndex->aiRowEst,&bUnordered);
82373 if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82374 pIndex->bUnordered = bUnordered;
82375 }else{
82376 decodeIntArray((char*)z, 1, &pTable->nRowEst, 0);
82377 }
82378
 
 
 
 
 
 
 
82379 return 0;
82380 }
82381
82382 /*
82383 ** If the Index.aSample variable is not NULL, delete the aSample[] array
82384 ** and its contents.
82385 */
82386 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
82387 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82388 if( pIdx->aSample ){
82389 int j;
82390 for(j=0; j<pIdx->nSample; j++){
82391 IndexSample *p = &pIdx->aSample[j];
82392 sqlite3DbFree(db, p->p);
 
 
82393 }
82394 sqlite3DbFree(db, pIdx->aSample);
82395 }
82396 if( db && db->pnBytesFreed==0 ){
82397 pIdx->nSample = 0;
@@ -81445,155 +82398,222 @@
82398 pIdx->aSample = 0;
82399 }
82400 #else
82401 UNUSED_PARAMETER(db);
82402 UNUSED_PARAMETER(pIdx);
82403 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82404 }
82405
82406 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82407 /*
82408 ** Populate the pIdx->aAvgEq[] array based on the samples currently
82409 ** stored in pIdx->aSample[].
82410 */
82411 static void initAvgEq(Index *pIdx){
82412 if( pIdx ){
82413 IndexSample *aSample = pIdx->aSample;
82414 IndexSample *pFinal = &aSample[pIdx->nSample-1];
82415 int iCol;
82416 for(iCol=0; iCol<pIdx->nColumn; iCol++){
82417 int i; /* Used to iterate through samples */
82418 tRowcnt sumEq = 0; /* Sum of the nEq values */
82419 tRowcnt nSum = 0; /* Number of terms contributing to sumEq */
82420 tRowcnt avgEq = 0;
82421 tRowcnt nDLt = pFinal->anDLt[iCol];
82422
82423 /* Set nSum to the number of distinct (iCol+1) field prefixes that
82424 ** occur in the stat4 table for this index before pFinal. Set
82425 ** sumEq to the sum of the nEq values for column iCol for the same
82426 ** set (adding the value only once where there exist dupicate
82427 ** prefixes). */
82428 for(i=0; i<(pIdx->nSample-1); i++){
82429 if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
82430 sumEq += aSample[i].anEq[iCol];
82431 nSum++;
82432 }
82433 }
82434 if( nDLt>nSum ){
82435 avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
82436 }
82437 if( avgEq==0 ) avgEq = 1;
82438 pIdx->aAvgEq[iCol] = avgEq;
82439 if( pIdx->nSampleCol==1 ) break;
82440 }
82441 }
82442 }
82443
82444 /*
82445 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
82446 ** into the relevant Index.aSample[] arrays.
82447 **
82448 ** Arguments zSql1 and zSql2 must point to SQL statements that return
82449 ** data equivalent to the following (statements are different for stat3,
82450 ** see the caller of this function for details):
82451 **
82452 ** zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
82453 ** zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
82454 **
82455 ** where %Q is replaced with the database name before the SQL is executed.
82456 */
82457 static int loadStatTbl(
82458 sqlite3 *db, /* Database handle */
82459 int bStat3, /* Assume single column records only */
82460 const char *zSql1, /* SQL statement 1 (see above) */
82461 const char *zSql2, /* SQL statement 2 (see above) */
82462 const char *zDb /* Database name (e.g. "main") */
82463 ){
82464 int rc; /* Result codes from subroutines */
82465 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
82466 char *zSql; /* Text of the SQL statement */
82467 Index *pPrevIdx = 0; /* Previous index in the loop */
 
 
82468 IndexSample *pSample; /* A slot in pIdx->aSample[] */
82469
82470 assert( db->lookaside.bEnabled==0 );
82471 zSql = sqlite3MPrintf(db, zSql1, zDb);
 
 
 
 
 
 
82472 if( !zSql ){
82473 return SQLITE_NOMEM;
82474 }
82475 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
82476 sqlite3DbFree(db, zSql);
82477 if( rc ) return rc;
82478
82479 while( sqlite3_step(pStmt)==SQLITE_ROW ){
82480 int nIdxCol = 1; /* Number of columns in stat4 records */
82481 int nAvgCol = 1; /* Number of entries in Index.aAvgEq */
82482
82483 char *zIndex; /* Index name */
82484 Index *pIdx; /* Pointer to the index object */
82485 int nSample; /* Number of samples */
82486 int nByte; /* Bytes of space required */
82487 int i; /* Bytes of space required */
82488 tRowcnt *pSpace;
82489
82490 zIndex = (char *)sqlite3_column_text(pStmt, 0);
82491 if( zIndex==0 ) continue;
82492 nSample = sqlite3_column_int(pStmt, 1);
82493 pIdx = sqlite3FindIndex(db, zIndex, zDb);
82494 assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
82495 /* Index.nSample is non-zero at this point if data has already been
82496 ** loaded from the stat4 table. In this case ignore stat3 data. */
82497 if( pIdx==0 || pIdx->nSample ) continue;
82498 if( bStat3==0 ){
82499 nIdxCol = pIdx->nColumn+1;
82500 nAvgCol = pIdx->nColumn;
82501 }
82502 pIdx->nSampleCol = nIdxCol;
82503 nByte = sizeof(IndexSample) * nSample;
82504 nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
82505 nByte += nAvgCol * sizeof(tRowcnt); /* Space for Index.aAvgEq[] */
82506
82507 pIdx->aSample = sqlite3DbMallocZero(db, nByte);
82508 if( pIdx->aSample==0 ){
 
82509 sqlite3_finalize(pStmt);
82510 return SQLITE_NOMEM;
82511 }
82512 pSpace = (tRowcnt*)&pIdx->aSample[nSample];
82513 pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
82514 for(i=0; i<nSample; i++){
82515 pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
82516 pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
82517 pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
82518 }
82519 assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
82520 }
82521 rc = sqlite3_finalize(pStmt);
82522 if( rc ) return rc;
82523
82524 zSql = sqlite3MPrintf(db, zSql2, zDb);
 
82525 if( !zSql ){
82526 return SQLITE_NOMEM;
82527 }
82528 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
82529 sqlite3DbFree(db, zSql);
82530 if( rc ) return rc;
82531
82532 while( sqlite3_step(pStmt)==SQLITE_ROW ){
82533 char *zIndex; /* Index name */
82534 Index *pIdx; /* Pointer to the index object */
82535 int nCol = 1; /* Number of columns in index */
 
82536
82537 zIndex = (char *)sqlite3_column_text(pStmt, 0);
82538 if( zIndex==0 ) continue;
82539 pIdx = sqlite3FindIndex(db, zIndex, zDb);
82540 if( pIdx==0 ) continue;
82541 /* This next condition is true if data has already been loaded from
82542 ** the sqlite_stat4 table. In this case ignore stat3 data. */
82543 nCol = pIdx->nSampleCol;
82544 if( bStat3 && nCol>1 ) continue;
82545 if( pIdx!=pPrevIdx ){
82546 initAvgEq(pPrevIdx);
82547 pPrevIdx = pIdx;
82548 }
82549 pSample = &pIdx->aSample[pIdx->nSample];
82550 decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
82551 decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
82552 decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
82553
82554 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
82555 ** This is in case the sample record is corrupted. In that case, the
82556 ** sqlite3VdbeRecordCompare() may read up to two varints past the
82557 ** end of the allocated buffer before it realizes it is dealing with
82558 ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
82559 ** a buffer overread. */
82560 pSample->n = sqlite3_column_bytes(pStmt, 4);
82561 pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
82562 if( pSample->p==0 ){
82563 sqlite3_finalize(pStmt);
82564 return SQLITE_NOMEM;
82565 }
82566 memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
82567 pIdx->nSample++;
82568 }
82569 rc = sqlite3_finalize(pStmt);
82570 if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
82571 return rc;
82572 }
82573
82574 /*
82575 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
82576 ** the Index.aSample[] arrays of all indices.
82577 */
82578 static int loadStat4(sqlite3 *db, const char *zDb){
82579 int rc = SQLITE_OK; /* Result codes from subroutines */
82580
82581 assert( db->lookaside.bEnabled==0 );
82582 if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
82583 rc = loadStatTbl(db, 0,
82584 "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
82585 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
82586 zDb
82587 );
82588 }
82589
82590 if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
82591 rc = loadStatTbl(db, 1,
82592 "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
82593 "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
82594 zDb
82595 );
82596 }
82597
82598 return rc;
82599 }
82600 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82601
82602 /*
82603 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
82604 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
82605 ** arrays. The contents of sqlite_stat3/4 are used to populate the
82606 ** Index.aSample[] arrays.
82607 **
82608 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
82609 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
82610 ** during compilation and the sqlite_stat3/4 table is present, no data is
82611 ** read from it.
82612 **
82613 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
82614 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
82615 ** returned. However, in this case, data is read from the sqlite_stat1
82616 ** table (if it is present) before returning.
82617 **
82618 ** If an OOM error occurs, this function always sets db->mallocFailed.
82619 ** This means if the caller does not care about other errors, the return
@@ -81611,11 +82631,11 @@
82631 /* Clear any prior statistics */
82632 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82633 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
82634 Index *pIdx = sqliteHashData(i);
82635 sqlite3DefaultRowEst(pIdx);
82636 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82637 sqlite3DeleteIndexSamples(db, pIdx);
82638 pIdx->aSample = 0;
82639 #endif
82640 }
82641
@@ -81635,16 +82655,16 @@
82655 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
82656 sqlite3DbFree(db, zSql);
82657 }
82658
82659
82660 /* Load the statistics from the sqlite_stat4 table. */
82661 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82662 if( rc==SQLITE_OK ){
82663 int lookasideEnabled = db->lookaside.bEnabled;
82664 db->lookaside.bEnabled = 0;
82665 rc = loadStat4(db, sInfo.zDatabase);
82666 db->lookaside.bEnabled = lookasideEnabled;
82667 }
82668 #endif
82669
82670 if( rc==SQLITE_NOMEM ){
@@ -84496,11 +85516,11 @@
85516 const char *zType, /* "idx" or "tbl" */
85517 const char *zName /* Name of index or table */
85518 ){
85519 int i;
85520 const char *zDbName = pParse->db->aDb[iDb].zName;
85521 for(i=1; i<=4; i++){
85522 char zTab[24];
85523 sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
85524 if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
85525 sqlite3NestedParse(pParse,
85526 "DELETE FROM %Q.%s WHERE %s=%Q",
@@ -89167,10 +90187,13 @@
90187 sqlite3FuncDefInsert(pHash, &aFunc[i]);
90188 }
90189 sqlite3RegisterDateTimeFunctions();
90190 #ifndef SQLITE_OMIT_ALTERTABLE
90191 sqlite3AlterFunctions();
90192 #endif
90193 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
90194 sqlite3AnalyzeFunctions();
90195 #endif
90196 }
90197
90198 /************** End of func.c ************************************************/
90199 /************** Begin file fkey.c ********************************************/
@@ -94387,11 +95410,11 @@
95410 */
95411 if( sqlite3StrICmp(zLeft,"journal_size_limit")==0 ){
95412 Pager *pPager = sqlite3BtreePager(pDb->pBt);
95413 i64 iLimit = -2;
95414 if( zRight ){
95415 sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
95416 if( iLimit<-1 ) iLimit = -1;
95417 }
95418 iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
95419 returnSingleInt(pParse, "journal_size_limit", iLimit);
95420 }else
@@ -94521,14 +95544,15 @@
95544 ** as little or as much as it wants. Except, if N is set to 0 then the
95545 ** upper layers will never invoke the xFetch interfaces to the VFS.
95546 */
95547 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
95548 sqlite3_int64 sz;
95549 #if SQLITE_MAX_MMAP_SIZE>0
95550 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
95551 if( zRight ){
95552 int ii;
95553 sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
95554 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
95555 if( pId2->n==0 ) db->szMmap = sz;
95556 for(ii=db->nDb-1; ii>=0; ii--){
95557 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
95558 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
@@ -94535,12 +95559,13 @@
95559 }
95560 }
95561 }
95562 sz = -1;
95563 rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
95564 #else
95565 sz = 0;
95566 rc = SQLITE_OK;
95567 #endif
95568 if( rc==SQLITE_OK ){
95569 returnSingleInt(pParse, "mmap_size", sz);
95570 }else if( rc!=SQLITE_NOTFOUND ){
95571 pParse->nErr++;
@@ -102688,11 +103713,11 @@
103713 ** space.
103714 */
103715 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
103716 assert( pTab!=0 );
103717 if( !pTab->pSelect ){
103718 sqlite3_value *pValue = 0;
103719 u8 enc = ENC(sqlite3VdbeDb(v));
103720 Column *pCol = &pTab->aCol[i];
103721 VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
103722 assert( i<pTab->nCol );
103723 sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
@@ -104937,13 +105962,13 @@
105962 /*
105963 ** Each instance of this object holds a sequence of WhereLoop objects
105964 ** that implement some or all of a query plan.
105965 **
105966 ** Think of each WhereLoop object as a node in a graph with arcs
105967 ** showing dependencies and costs for travelling between nodes. (That is
105968 ** not a completely accurate description because WhereLoop costs are a
105969 ** vector, not a scalar, and because dependencies are many-to-one, not
105970 ** one-to-one as are graph nodes. But it is a useful visualization aid.)
105971 ** Then a WherePath object is a path through the graph that visits some
105972 ** or all of the WhereLoop objects once.
105973 **
105974 ** The "solver" works by creating the N best WherePath objects of length
@@ -105038,11 +106063,11 @@
106063 #define TERM_CODED 0x04 /* This term is already coded */
106064 #define TERM_COPIED 0x08 /* Has a child */
106065 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
106066 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
106067 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
106068 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106069 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
106070 #else
106071 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
106072 #endif
106073
@@ -105144,10 +106169,14 @@
106169 WhereInfo *pWInfo; /* Information about this WHERE */
106170 WhereClause *pWC; /* WHERE clause terms */
106171 ExprList *pOrderBy; /* ORDER BY clause */
106172 WhereLoop *pNew; /* Template WhereLoop */
106173 WhereOrSet *pOrSet; /* Record best loops here, if not NULL */
106174 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
106175 UnpackedRecord *pRec; /* Probe for stat4 (if required) */
106176 int nRecValid; /* Number of valid fields currently in pRec */
106177 #endif
106178 };
106179
106180 /*
106181 ** The WHERE clause processing routine has two halves. The
106182 ** first part does the start of the WHERE loop and the second
@@ -106543,11 +107572,11 @@
107572 pNewTerm->prereqAll = pTerm->prereqAll;
107573 }
107574 }
107575 #endif /* SQLITE_OMIT_VIRTUALTABLE */
107576
107577 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
107578 /* When sqlite_stat3 histogram data is available an operator of the
107579 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
107580 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
107581 ** virtual term of that form.
107582 **
@@ -106583,11 +107612,11 @@
107612 pTerm->nChild = 1;
107613 pTerm->wtFlags |= TERM_COPIED;
107614 pNewTerm->prereqAll = pTerm->prereqAll;
107615 }
107616 }
107617 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
107618
107619 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
107620 ** an index for tables to the left of the join.
107621 */
107622 pTerm->prereqRight |= extraRight;
@@ -107151,155 +108180,87 @@
108180 return pParse->nErr;
108181 }
108182 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
108183
108184
108185 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108186 /*
108187 ** Estimate the location of a particular key among all keys in an
108188 ** index. Store the results in aStat as follows:
108189 **
108190 ** aStat[0] Est. number of rows less than pVal
108191 ** aStat[1] Est. number of rows equal to pVal
108192 **
108193 ** Return SQLITE_OK on success.
108194 */
108195 static void whereKeyStats(
108196 Parse *pParse, /* Database connection */
108197 Index *pIdx, /* Index to consider domain of */
108198 UnpackedRecord *pRec, /* Vector of values to consider */
108199 int roundUp, /* Round up if true. Round down if false */
108200 tRowcnt *aStat /* OUT: stats written here */
108201 ){
108202 IndexSample *aSample = pIdx->aSample;
108203 int iCol; /* Index of required stats in anEq[] etc. */
108204 int iMin = 0; /* Smallest sample not yet tested */
108205 int i = pIdx->nSample; /* Smallest sample larger than or equal to pRec */
108206 int iTest; /* Next sample to test */
108207 int res; /* Result of comparison operation */
108208
108209 assert( pRec!=0 || pParse->db->mallocFailed );
108210 if( pRec==0 ) return;
108211 iCol = pRec->nField - 1;
108212 assert( pIdx->nSample>0 );
108213 assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
108214 do{
108215 iTest = (iMin+i)/2;
108216 res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
108217 if( res<0 ){
108218 iMin = iTest+1;
108219 }else{
108220 i = iTest;
108221 }
108222 }while( res && iMin<i );
108223
108224 #ifdef SQLITE_DEBUG
108225 /* The following assert statements check that the binary search code
108226 ** above found the right answer. This block serves no purpose other
108227 ** than to invoke the asserts. */
108228 if( res==0 ){
108229 /* If (res==0) is true, then sample $i must be equal to pRec */
108230 assert( i<pIdx->nSample );
108231 assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
108232 || pParse->db->mallocFailed );
108233 }else{
108234 /* Otherwise, pRec must be smaller than sample $i and larger than
108235 ** sample ($i-1). */
108236 assert( i==pIdx->nSample
108237 || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
108238 || pParse->db->mallocFailed );
108239 assert( i==0
108240 || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
108241 || pParse->db->mallocFailed );
108242 }
108243 #endif /* ifdef SQLITE_DEBUG */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108244
108245 /* At this point, aSample[i] is the first sample that is greater than
108246 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
108247 ** than pVal. If aSample[i]==pVal, then res==0.
108248 */
108249 if( res==0 ){
108250 aStat[0] = aSample[i].anLt[iCol];
108251 aStat[1] = aSample[i].anEq[iCol];
 
108252 }else{
108253 tRowcnt iLower, iUpper, iGap;
108254 if( i==0 ){
108255 iLower = 0;
108256 iUpper = aSample[0].anLt[iCol];
108257 }else{
108258 iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
108259 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
108260 }
108261 aStat[1] = (pIdx->nColumn>iCol ? pIdx->aAvgEq[iCol] : 1);
108262 if( iLower>=iUpper ){
108263 iGap = 0;
108264 }else{
108265 iGap = iUpper - iLower;
108266 }
@@ -107308,48 +108269,12 @@
108269 }else{
108270 iGap = iGap/3;
108271 }
108272 aStat[0] = iLower + iGap;
108273 }
108274 }
108275 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108276
108277 /*
108278 ** This function is used to estimate the number of rows that will be visited
108279 ** by scanning an index for a range of values. The range may have an upper
108280 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -107362,107 +108287,159 @@
108287 ** pLower pUpper
108288 **
108289 ** If either of the upper or lower bound is not present, then NULL is passed in
108290 ** place of the corresponding WhereTerm.
108291 **
108292 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
108293 ** column subject to the range constraint. Or, equivalently, the number of
108294 ** equality constraints optimized by the proposed index scan. For example,
108295 ** assuming index p is on t1(a, b), and the SQL query is:
108296 **
108297 ** ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
108298 **
108299 ** then nEq is set to 1 (as the range restricted column, b, is the second
108300 ** left-most column of the index). Or, if the query is:
108301 **
108302 ** ... FROM t1 WHERE a > ? AND a < ? ...
108303 **
108304 ** then nEq is set to 0.
108305 **
108306 ** When this function is called, *pnOut is set to the whereCost() of the
108307 ** number of rows that the index scan is expected to visit without
108308 ** considering the range constraints. If nEq is 0, this is the number of
108309 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
108310 ** to account for the range contraints pLower and pUpper.
108311 **
108312 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
108313 ** used, each range inequality reduces the search space by a factor of 4.
108314 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
108315 ** rows visited by a factor of 16.
108316 */
108317 static int whereRangeScanEst(
108318 Parse *pParse, /* Parsing & code generating context */
108319 WhereLoopBuilder *pBuilder,
 
108320 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
108321 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
108322 WhereCost *pnOut /* IN/OUT: Number of rows visited */
108323 ){
108324 int rc = SQLITE_OK;
108325 int nOut = (int)*pnOut;
108326
108327 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108328 Index *p = pBuilder->pNew->u.btree.pIndex;
108329 int nEq = pBuilder->pNew->u.btree.nEq;
108330
108331 if( nEq==pBuilder->nRecValid
108332 && nEq<p->nSampleCol
108333 && p->nSample
108334 && OptimizationEnabled(pParse->db, SQLITE_Stat3)
108335 ){
108336 UnpackedRecord *pRec = pBuilder->pRec;
108337 tRowcnt a[2];
108338 u8 aff;
108339
108340 /* Variable iLower will be set to the estimate of the number of rows in
108341 ** the index that are less than the lower bound of the range query. The
108342 ** lower bound being the concatenation of $P and $L, where $P is the
108343 ** key-prefix formed by the nEq values matched against the nEq left-most
108344 ** columns of the index, and $L is the value in pLower.
108345 **
108346 ** Or, if pLower is NULL or $L cannot be extracted from it (because it
108347 ** is not a simple variable or literal value), the lower bound of the
108348 ** range is $P. Due to a quirk in the way whereKeyStats() works, even
108349 ** if $L is available, whereKeyStats() is called for both ($P) and
108350 ** ($P:$L) and the larger of the two returned values used.
108351 **
108352 ** Similarly, iUpper is to be set to the estimate of the number of rows
108353 ** less than the upper bound of the range query. Where the upper bound
108354 ** is either ($P) or ($P:$U). Again, even if $U is available, both values
108355 ** of iUpper are requested of whereKeyStats() and the smaller used.
108356 */
108357 tRowcnt iLower;
108358 tRowcnt iUpper;
108359
108360 if( nEq==p->nColumn ){
108361 aff = SQLITE_AFF_INTEGER;
108362 }else{
108363 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
108364 }
108365 /* Determine iLower and iUpper using ($P) only. */
108366 if( nEq==0 ){
108367 iLower = 0;
108368 iUpper = p->aiRowEst[0];
108369 }else{
108370 /* Note: this call could be optimized away - since the same values must
108371 ** have been requested when testing key $P in whereEqualScanEst(). */
108372 whereKeyStats(pParse, p, pRec, 0, a);
108373 iLower = a[0];
108374 iUpper = a[0] + a[1];
108375 }
108376
108377 /* If possible, improve on the iLower estimate using ($P:$L). */
108378 if( pLower ){
108379 int bOk; /* True if value is extracted from pExpr */
108380 Expr *pExpr = pLower->pExpr->pRight;
 
108381 assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
108382 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108383 if( rc==SQLITE_OK && bOk ){
108384 tRowcnt iNew;
108385 whereKeyStats(pParse, p, pRec, 0, a);
108386 iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
108387 if( iNew>iLower ) iLower = iNew;
108388 }
108389 }
108390
108391 /* If possible, improve on the iUpper estimate using ($P:$U). */
108392 if( pUpper ){
108393 int bOk; /* True if value is extracted from pExpr */
108394 Expr *pExpr = pUpper->pExpr->pRight;
108395 assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
108396 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
108397 if( rc==SQLITE_OK && bOk ){
108398 tRowcnt iNew;
108399 whereKeyStats(pParse, p, pRec, 1, a);
108400 iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
108401 if( iNew<iUpper ) iUpper = iNew;
108402 }
108403 }
108404
108405 pBuilder->pRec = pRec;
108406 if( rc==SQLITE_OK ){
108407 WhereCost nNew;
108408 if( iUpper>iLower ){
108409 nNew = whereCost(iUpper - iLower);
108410 }else{
108411 nNew = 10; assert( 10==whereCost(2) );
108412 }
108413 if( nNew<nOut ){
108414 nOut = nNew;
108415 }
108416 *pnOut = (WhereCost)nOut;
108417 WHERETRACE(0x100, ("range scan regions: %u..%u est=%d\n",
108418 (u32)iLower, (u32)iUpper, nOut));
108419 return SQLITE_OK;
108420 }
108421 }
108422 #else
108423 UNUSED_PARAMETER(pParse);
108424 UNUSED_PARAMETER(pBuilder);
 
108425 #endif
108426 assert( pLower || pUpper );
 
108427 /* TUNING: Each inequality constraint reduces the search space 4-fold.
108428 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
108429 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
108430 nOut -= 20; assert( 20==whereCost(4) );
108431 }
108432 if( pUpper ){
108433 nOut -= 20; assert( 20==whereCost(4) );
108434 }
108435 if( nOut<10 ) nOut = 10;
108436 *pnOut = (WhereCost)nOut;
108437 return rc;
108438 }
108439
108440 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108441 /*
108442 ** Estimate the number of rows that will be returned based on
108443 ** an equality constraint x=VALUE and where that VALUE occurs in
108444 ** the histogram data. This only works when x is the left-most
108445 ** column of an index and sqlite_stat3 histogram data is available
@@ -107478,41 +108455,57 @@
108455 ** for a UTF conversion required for comparison. The error is stored
108456 ** in the pParse structure.
108457 */
108458 static int whereEqualScanEst(
108459 Parse *pParse, /* Parsing & code generating context */
108460 WhereLoopBuilder *pBuilder,
108461 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
108462 tRowcnt *pnRow /* Write the revised row estimate here */
108463 ){
108464 Index *p = pBuilder->pNew->u.btree.pIndex;
108465 int nEq = pBuilder->pNew->u.btree.nEq;
108466 UnpackedRecord *pRec = pBuilder->pRec;
108467 u8 aff; /* Column affinity */
108468 int rc; /* Subfunction return code */
108469 tRowcnt a[2]; /* Statistics */
108470 int bOk;
108471
108472 assert( nEq>=1 );
108473 assert( nEq<=(p->nColumn+1) );
108474 assert( p->aSample!=0 );
108475 assert( p->nSample>0 );
108476 assert( pBuilder->nRecValid<nEq );
108477
108478 /* If values are not available for all fields of the index to the left
108479 ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
108480 if( pBuilder->nRecValid<(nEq-1) ){
108481 return SQLITE_NOTFOUND;
108482 }
108483
108484 /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
108485 ** below would return the same value. */
108486 if( nEq>p->nColumn ){
108487 *pnRow = 1;
108488 return SQLITE_OK;
108489 }
108490
108491 aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
108492 rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
108493 pBuilder->pRec = pRec;
108494 if( rc!=SQLITE_OK ) return rc;
108495 if( bOk==0 ) return SQLITE_NOTFOUND;
108496 pBuilder->nRecValid = nEq;
108497
108498 whereKeyStats(pParse, p, pRec, 0, a);
108499 WHERETRACE(0x100,("equality scan regions: %d\n", (int)a[1]));
108500 *pnRow = a[1];
108501
108502 return rc;
108503 }
108504 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
108505
108506 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108507 /*
108508 ** Estimate the number of rows that will be returned based on
108509 ** an IN constraint where the right-hand side of the IN operator
108510 ** is a list of values. Example:
108511 **
@@ -107527,33 +108520,38 @@
108520 ** for a UTF conversion required for comparison. The error is stored
108521 ** in the pParse structure.
108522 */
108523 static int whereInScanEst(
108524 Parse *pParse, /* Parsing & code generating context */
108525 WhereLoopBuilder *pBuilder,
108526 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
108527 tRowcnt *pnRow /* Write the revised row estimate here */
108528 ){
108529 Index *p = pBuilder->pNew->u.btree.pIndex;
108530 int nRecValid = pBuilder->nRecValid;
108531 int rc = SQLITE_OK; /* Subfunction return code */
108532 tRowcnt nEst; /* Number of rows for a single term */
108533 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
108534 int i; /* Loop counter */
108535
108536 assert( p->aSample!=0 );
108537 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
108538 nEst = p->aiRowEst[0];
108539 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
108540 nRowEst += nEst;
108541 pBuilder->nRecValid = nRecValid;
108542 }
108543
108544 if( rc==SQLITE_OK ){
108545 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
108546 *pnRow = nRowEst;
108547 WHERETRACE(0x100,("IN row estimate: est=%g\n", nRowEst));
108548 }
108549 assert( pBuilder->nRecValid==nRecValid );
108550 return rc;
108551 }
108552 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
108553
108554 /*
108555 ** Disable a term in the WHERE clause. Except, do not disable the term
108556 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
108557 ** or USING clause of that join.
@@ -107787,11 +108785,11 @@
108785 pParse->db->mallocFailed = 1;
108786 }
108787
108788 /* Evaluate the equality constraints
108789 */
108790 assert( zAff==0 || (int)strlen(zAff)>=nEq );
108791 for(j=0; j<nEq; j++){
108792 int r1;
108793 pTerm = pLoop->aLTerm[j];
108794 assert( pTerm!=0 );
108795 /* The following true for indices with redundant columns.
@@ -108956,19 +109954,21 @@
109954 assert( p->rSetup>=pTemplate->rSetup );
109955
109956 if( (p->prereq & pTemplate->prereq)==p->prereq
109957 && p->rSetup<=pTemplate->rSetup
109958 && p->rRun<=pTemplate->rRun
109959 && p->nOut<=pTemplate->nOut
109960 ){
109961 /* This branch taken when p is equal or better than pTemplate in
109962 ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
109963 ** (4) number of output rows. */
109964 assert( p->rSetup==pTemplate->rSetup );
109965 if( p->prereq==pTemplate->prereq
109966 && p->nLTerm<pTemplate->nLTerm
109967 && (p->wsFlags & WHERE_INDEXED)!=0
109968 && (pTemplate->wsFlags & WHERE_INDEXED)!=0
109969 && p->u.btree.pIndex==pTemplate->u.btree.pIndex
 
109970 ){
109971 /* Overwrite an existing WhereLoop with an similar one that uses
109972 ** more terms of the index */
109973 pNext = p->pNextLoop;
109974 break;
@@ -108978,15 +109978,17 @@
109978 goto whereLoopInsert_noop;
109979 }
109980 }
109981 if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
109982 && p->rRun>=pTemplate->rRun
109983 && p->nOut>=pTemplate->nOut
109984 && ALWAYS(p->rSetup>=pTemplate->rSetup) /* See SETUP-INVARIANT above */
109985 ){
109986 /* Overwrite an existing WhereLoop with a better one: one that is
109987 ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
109988 ** or (4) number of output rows, and is no worse in any of those
109989 ** categories. */
109990 pNext = p->pNextLoop;
109991 break;
109992 }
109993 }
109994
@@ -109094,16 +110096,22 @@
110096 saved_nOut = pNew->nOut;
110097 pNew->rSetup = 0;
110098 rLogSize = estLog(whereCost(pProbe->aiRowEst[0]));
110099 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
110100 int nIn = 0;
110101 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110102 int nRecValid = pBuilder->nRecValid;
110103 #endif
110104 if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
110105 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
110106 ){
110107 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
110108 }
110109 if( pTerm->prereqRight & pNew->maskSelf ) continue;
110110
110111 assert( pNew->nOut==saved_nOut );
110112
110113 pNew->wsFlags = saved_wsFlags;
110114 pNew->u.btree.nEq = saved_nEq;
110115 pNew->nLTerm = saved_nLTerm;
110116 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
110117 pNew->aLTerm[pNew->nLTerm++] = pTerm;
@@ -109156,29 +110164,34 @@
110164 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
110165 pNew->aLTerm[pNew->nLTerm-2] : 0;
110166 }
110167 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
110168 /* Adjust nOut and rRun for STAT3 range values */
110169 assert( pNew->nOut==saved_nOut );
110170 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, &pNew->nOut);
110171 }
110172 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110173 if( nInMul==0
110174 && pProbe->nSample
110175 && pNew->u.btree.nEq<=pProbe->nSampleCol
110176 && OptimizationEnabled(db, SQLITE_Stat3)
110177 ){
110178 Expr *pExpr = pTerm->pExpr;
110179 tRowcnt nOut = 0;
110180 if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
110181 testcase( pTerm->eOperator & WO_EQ );
110182 testcase( pTerm->eOperator & WO_ISNULL );
110183 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
110184 }else if( (pTerm->eOperator & WO_IN)
110185 && !ExprHasProperty(pExpr, EP_xIsSelect) ){
110186 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
110187 }
110188 assert( nOut==0 || rc==SQLITE_OK );
110189 if( nOut ){
110190 nOut = whereCost(nOut);
110191 pNew->nOut = MIN(nOut, saved_nOut);
110192 }
110193 }
110194 #endif
110195 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
110196 /* Each row involves a step of the index, then a binary search of
110197 ** the main table */
@@ -109191,10 +110204,14 @@
110204 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
110205 && pNew->u.btree.nEq<(pProbe->nColumn + (pProbe->zName!=0))
110206 ){
110207 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
110208 }
110209 pNew->nOut = saved_nOut;
110210 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110211 pBuilder->nRecValid = nRecValid;
110212 #endif
110213 }
110214 pNew->prereq = saved_prereq;
110215 pNew->u.btree.nEq = saved_nEq;
110216 pNew->wsFlags = saved_wsFlags;
110217 pNew->nOut = saved_nOut;
@@ -109420,11 +110437,17 @@
110437 }
110438 rc = whereLoopInsert(pBuilder, pNew);
110439 if( rc ) break;
110440 }
110441 }
110442
110443 rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
110444 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110445 sqlite3Stat4ProbeFree(pBuilder->pRec);
110446 pBuilder->nRecValid = 0;
110447 pBuilder->pRec = 0;
110448 #endif
110449
110450 /* If there was an INDEXED BY clause, then only that one index is
110451 ** considered. */
110452 if( pSrc->pIndex ) break;
110453 }
@@ -110666,11 +111689,11 @@
111689 if( pWInfo->nLevel>=2
111690 && pResultSet!=0
111691 && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
111692 ){
111693 Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
111694 if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
111695 while( pWInfo->nLevel>=2 ){
111696 WhereTerm *pTerm, *pEnd;
111697 pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
111698 if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
111699 if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
@@ -116698,18 +117721,20 @@
117721 case SQLITE_IOERR_SHMMAP: zName = "SQLITE_IOERR_SHMMAP"; break;
117722 case SQLITE_IOERR_SEEK: zName = "SQLITE_IOERR_SEEK"; break;
117723 case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
117724 case SQLITE_IOERR_MMAP: zName = "SQLITE_IOERR_MMAP"; break;
117725 case SQLITE_IOERR_GETTEMPPATH: zName = "SQLITE_IOERR_GETTEMPPATH"; break;
117726 case SQLITE_IOERR_CONVPATH: zName = "SQLITE_IOERR_CONVPATH"; break;
117727 case SQLITE_CORRUPT: zName = "SQLITE_CORRUPT"; break;
117728 case SQLITE_CORRUPT_VTAB: zName = "SQLITE_CORRUPT_VTAB"; break;
117729 case SQLITE_NOTFOUND: zName = "SQLITE_NOTFOUND"; break;
117730 case SQLITE_FULL: zName = "SQLITE_FULL"; break;
117731 case SQLITE_CANTOPEN: zName = "SQLITE_CANTOPEN"; break;
117732 case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
117733 case SQLITE_CANTOPEN_ISDIR: zName = "SQLITE_CANTOPEN_ISDIR"; break;
117734 case SQLITE_CANTOPEN_FULLPATH: zName = "SQLITE_CANTOPEN_FULLPATH"; break;
117735 case SQLITE_CANTOPEN_CONVPATH: zName = "SQLITE_CANTOPEN_CONVPATH"; break;
117736 case SQLITE_PROTOCOL: zName = "SQLITE_PROTOCOL"; break;
117737 case SQLITE_EMPTY: zName = "SQLITE_EMPTY"; break;
117738 case SQLITE_SCHEMA: zName = "SQLITE_SCHEMA"; break;
117739 case SQLITE_TOOBIG: zName = "SQLITE_TOOBIG"; break;
117740 case SQLITE_CONSTRAINT: zName = "SQLITE_CONSTRAINT"; break;
117741
+5 -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.1"
111
-#define SQLITE_VERSION_NUMBER 3008000
112
-#define SQLITE_SOURCE_ID "2013-08-29 13:47:05 c5857808c0707baa30994dd6aa3b9c93a74c0073"
110
+#define SQLITE_VERSION "3.8.1"
111
+#define SQLITE_VERSION_NUMBER 3008001
112
+#define SQLITE_SOURCE_ID "2013-09-04 04:04:08 8df95bb0b3f72222cf262174247a467c234f9939"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -477,16 +477,18 @@
477477
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
478478
#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
479479
#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
480480
#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
481481
#define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
482
+#define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
482483
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
483484
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
484485
#define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
485486
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
486487
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
487488
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
489
+#define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
488490
#define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
489491
#define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
490492
#define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
491493
#define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
492494
#define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
493495
--- 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.1"
111 #define SQLITE_VERSION_NUMBER 3008000
112 #define SQLITE_SOURCE_ID "2013-08-29 13:47:05 c5857808c0707baa30994dd6aa3b9c93a74c0073"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -477,16 +477,18 @@
477 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
478 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
479 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
480 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
481 #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
 
482 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
483 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
484 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
485 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
486 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
487 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
 
488 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
489 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
490 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
491 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
492 #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
493
--- 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-09-04 04:04:08 8df95bb0b3f72222cf262174247a467c234f9939"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -477,16 +477,18 @@
477 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
478 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
479 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
480 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
481 #define SQLITE_IOERR_GETTEMPPATH (SQLITE_IOERR | (25<<8))
482 #define SQLITE_IOERR_CONVPATH (SQLITE_IOERR | (26<<8))
483 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
484 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
485 #define SQLITE_BUSY_SNAPSHOT (SQLITE_BUSY | (2<<8))
486 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
487 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
488 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
489 #define SQLITE_CANTOPEN_CONVPATH (SQLITE_CANTOPEN | (4<<8))
490 #define SQLITE_CORRUPT_VTAB (SQLITE_CORRUPT | (1<<8))
491 #define SQLITE_READONLY_RECOVERY (SQLITE_READONLY | (1<<8))
492 #define SQLITE_READONLY_CANTLOCK (SQLITE_READONLY | (2<<8))
493 #define SQLITE_READONLY_ROLLBACK (SQLITE_READONLY | (3<<8))
494 #define SQLITE_ABORT_ROLLBACK (SQLITE_ABORT | (2<<8))
495
--- src/style.c
+++ src/style.c
@@ -1207,10 +1207,11 @@
12071207
if( g.perm.Setup ){
12081208
const char *zRedir = P("redirect");
12091209
if( zRedir ) cgi_redirect(zRedir);
12101210
}
12111211
style_footer();
1212
+ if( g.perm.Admin && P("err") ) fossil_fatal("%s", P("err"));
12121213
}
12131214
12141215
/*
12151216
** This page is a honeypot for spiders and bots.
12161217
**
12171218
--- src/style.c
+++ src/style.c
@@ -1207,10 +1207,11 @@
1207 if( g.perm.Setup ){
1208 const char *zRedir = P("redirect");
1209 if( zRedir ) cgi_redirect(zRedir);
1210 }
1211 style_footer();
 
1212 }
1213
1214 /*
1215 ** This page is a honeypot for spiders and bots.
1216 **
1217
--- src/style.c
+++ src/style.c
@@ -1207,10 +1207,11 @@
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
+1 -1
--- 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')",
531531
--- 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')",
531
--- 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')",
531
+1 -1
--- 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')",
531531
--- 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')",
531
--- 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')",
531
+2 -2
--- 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();
@@ -658,11 +658,11 @@
658658
}
659659
}else if( errCode<=0 ){
660660
if( revision==0 ){
661661
revision = db_text("current", "SELECT uuid FROM blob WHERE rid=%d", rid);
662662
}
663
- fossil_panic("could not parse manifest for checkin: %s", revision);
663
+ fossil_fatal("could not parse manifest for checkin: %s", revision);
664664
}
665665
return errCode;
666666
}
667667
668668
669669
--- 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();
@@ -658,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_panic("could not parse manifest for checkin: %s", revision);
664 }
665 return errCode;
666 }
667
668
669
--- 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();
@@ -658,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
@@ -277,11 +277,11 @@
277277
g.argv[4], uid
278278
);
279279
}
280280
fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
281281
}else{
282
- fossil_panic("user subcommand should be one of: "
282
+ fossil_fatal("user subcommand should be one of: "
283283
"capabilities default list new password");
284284
}
285285
}
286286
287287
/*
288288
--- src/user.c
+++ src/user.c
@@ -277,11 +277,11 @@
277 g.argv[4], uid
278 );
279 }
280 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
281 }else{
282 fossil_panic("user subcommand should be one of: "
283 "capabilities default list new password");
284 }
285 }
286
287 /*
288
--- src/user.c
+++ src/user.c
@@ -277,11 +277,11 @@
277 g.argv[4], uid
278 );
279 }
280 fossil_print("%s\n", db_text(0, "SELECT cap FROM user WHERE uid=%d", uid));
281 }else{
282 fossil_fatal("user subcommand should be one of: "
283 "capabilities default list new password");
284 }
285 }
286
287 /*
288
+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
+6 -4
--- src/vfile.c
+++ src/vfile.c
@@ -82,12 +82,14 @@
8282
return;
8383
}
8484
8585
db_begin_transaction();
8686
p = manifest_get(vid, CFTYPE_MANIFEST);
87
- if( p==0 ) return;
88
- db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid);
87
+ if( p==0 ) {
88
+ db_end_transaction(1);
89
+ return;
90
+ }
8991
db_prepare(&ins,
9092
"INSERT INTO vfile(vid,isexe,islink,rid,mrid,pathname) "
9193
" VALUES(:vid,:isexe,:islink,:id,:id,:name)");
9294
db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
9395
db_bind_int(&ins, ":vid", vid);
@@ -95,11 +97,11 @@
9597
while( (pFile = manifest_file_next(p,0))!=0 ){
9698
if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
9799
db_bind_text(&ridq, ":uuid", pFile->zUuid);
98100
if( db_step(&ridq)==SQLITE_ROW ){
99101
rid = db_column_int(&ridq, 0);
100
- size = db_column_int(&ridq, 0);
102
+ size = db_column_int(&ridq, 1);
101103
}else{
102104
rid = 0;
103105
size = 0;
104106
}
105107
db_reset(&ridq);
@@ -744,11 +746,11 @@
744746
blob_zero(pManOut);
745747
}
746748
db_must_be_within_tree();
747749
pManifest = manifest_get(vid, CFTYPE_MANIFEST);
748750
if( pManifest==0 ){
749
- fossil_panic("manifest file (%d) is malformed", vid);
751
+ fossil_fatal("manifest file (%d) is malformed", vid);
750752
}
751753
manifest_file_rewind(pManifest);
752754
while( (pFile = manifest_file_next(pManifest,0))!=0 ){
753755
if( pFile->zUuid==0 ) continue;
754756
fid = uuid_to_rid(pFile->zUuid, 0);
755757
--- src/vfile.c
+++ src/vfile.c
@@ -82,12 +82,14 @@
82 return;
83 }
84
85 db_begin_transaction();
86 p = manifest_get(vid, CFTYPE_MANIFEST);
87 if( p==0 ) return;
88 db_multi_exec("DELETE FROM vfile WHERE vid=%d", vid);
 
 
89 db_prepare(&ins,
90 "INSERT INTO vfile(vid,isexe,islink,rid,mrid,pathname) "
91 " VALUES(:vid,:isexe,:islink,:id,:id,:name)");
92 db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
93 db_bind_int(&ins, ":vid", vid);
@@ -95,11 +97,11 @@
95 while( (pFile = manifest_file_next(p,0))!=0 ){
96 if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
97 db_bind_text(&ridq, ":uuid", pFile->zUuid);
98 if( db_step(&ridq)==SQLITE_ROW ){
99 rid = db_column_int(&ridq, 0);
100 size = db_column_int(&ridq, 0);
101 }else{
102 rid = 0;
103 size = 0;
104 }
105 db_reset(&ridq);
@@ -744,11 +746,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
@@ -82,12 +82,14 @@
82 return;
83 }
84
85 db_begin_transaction();
86 p = manifest_get(vid, CFTYPE_MANIFEST);
87 if( p==0 ) {
88 db_end_transaction(1);
89 return;
90 }
91 db_prepare(&ins,
92 "INSERT INTO vfile(vid,isexe,islink,rid,mrid,pathname) "
93 " VALUES(:vid,:isexe,:islink,:id,:id,:name)");
94 db_prepare(&ridq, "SELECT rid,size FROM blob WHERE uuid=:uuid");
95 db_bind_int(&ins, ":vid", vid);
@@ -95,11 +97,11 @@
97 while( (pFile = manifest_file_next(p,0))!=0 ){
98 if( pFile->zUuid==0 || uuid_is_shunned(pFile->zUuid) ) continue;
99 db_bind_text(&ridq, ":uuid", pFile->zUuid);
100 if( db_step(&ridq)==SQLITE_ROW ){
101 rid = db_column_int(&ridq, 0);
102 size = db_column_int(&ridq, 1);
103 }else{
104 rid = 0;
105 size = 0;
106 }
107 db_reset(&ridq);
@@ -744,11 +746,11 @@
746 blob_zero(pManOut);
747 }
748 db_must_be_within_tree();
749 pManifest = manifest_get(vid, CFTYPE_MANIFEST);
750 if( pManifest==0 ){
751 fossil_fatal("manifest file (%d) is malformed", vid);
752 }
753 manifest_file_rewind(pManifest);
754 while( (pFile = manifest_file_next(pManifest,0))!=0 ){
755 if( pFile->zUuid==0 ) continue;
756 fid = uuid_to_rid(pFile->zUuid, 0);
757
--- www/changes.wiki
+++ www/changes.wiki
@@ -9,10 +9,16 @@
99
automatically closes the merged branch when committing.
1010
* Renamed <tt>/stats_report</tt> page to [/reports]. Graph width is now
1111
relative, not absolute.
1212
* Added <tt>yw=YYYY-WW</tt> (year-week) filter to timeline to limit the results
1313
to a specific year and calendar week number, e.g. [/timeline?yw=2013-01].
14
+ * Fixed an obscure condition under which an assertion failure could overwrite
15
+ part of a repository database file, corrupting it. This problem has only
16
+ been seen once in the wild.
17
+ * Added support for unlimited line lengths in side-by-side diffs.
18
+ * New --close option to [/help?cmd=commit | fossil commit], which
19
+ immediately closes the branch being committed.
1420
1521
<h2>Changes For Version 1.26 (2013-06-18)</h2>
1622
* The argument to the --port option for the [/help?cmd=ui | fossil ui] and
1723
[/help?cmd=server | fossil server] commands can take an IP address in addition
1824
to the port number, causing Fossil to bind to just that one IP address.
1925
--- www/changes.wiki
+++ www/changes.wiki
@@ -9,10 +9,16 @@
9 automatically closes the merged branch when committing.
10 * Renamed <tt>/stats_report</tt> page to [/reports]. Graph width is now
11 relative, not absolute.
12 * Added <tt>yw=YYYY-WW</tt> (year-week) filter to timeline to limit the results
13 to a specific year and calendar week number, e.g. [/timeline?yw=2013-01].
 
 
 
 
 
 
14
15 <h2>Changes For Version 1.26 (2013-06-18)</h2>
16 * The argument to the --port option for the [/help?cmd=ui | fossil ui] and
17 [/help?cmd=server | fossil server] commands can take an IP address in addition
18 to the port number, causing Fossil to bind to just that one IP address.
19
--- www/changes.wiki
+++ www/changes.wiki
@@ -9,10 +9,16 @@
9 automatically closes the merged branch when committing.
10 * Renamed <tt>/stats_report</tt> page to [/reports]. Graph width is now
11 relative, not absolute.
12 * Added <tt>yw=YYYY-WW</tt> (year-week) filter to timeline to limit the results
13 to a specific year and calendar week number, e.g. [/timeline?yw=2013-01].
14 * Fixed an obscure condition under which an assertion failure could overwrite
15 part of a repository database file, corrupting it. This problem has only
16 been seen once in the wild.
17 * Added support for unlimited line lengths in side-by-side diffs.
18 * New --close option to [/help?cmd=commit | fossil commit], which
19 immediately closes the branch being committed.
20
21 <h2>Changes For Version 1.26 (2013-06-18)</h2>
22 * The argument to the --port option for the [/help?cmd=ui | fossil ui] and
23 [/help?cmd=server | fossil server] commands can take an IP address in addition
24 to the port number, causing Fossil to bind to just that one IP address.
25
--- www/makefile.wiki
+++ www/makefile.wiki
@@ -208,11 +208,11 @@
208208
* -Dlocaltime=fossil_localtime
209209
* -DSQLITE_OMIT_LOAD_EXTENSION=1
210210
* -DSQLITE_ENABLE_LOCKING_STYLE=0
211211
* -DSQLITE_THREADSAFE=0
212212
* -DSQLITE_DEFAULT_FILE_FORMAT=4
213
- * -DSQLITE_ENABLE_STAT2
213
+ * -DSQLITE_ENABLE_STAT3
214214
215215
The first and second symbol definitions above are required; the others
216216
are merely recommended. The "localtime()" library function in SQLite must
217217
be redefined to invoke fossil_localtime() instead. The fossil_localtime()
218218
routine will invoke either gmtime() or localtime() depending on the
219219
--- www/makefile.wiki
+++ www/makefile.wiki
@@ -208,11 +208,11 @@
208 * -Dlocaltime=fossil_localtime
209 * -DSQLITE_OMIT_LOAD_EXTENSION=1
210 * -DSQLITE_ENABLE_LOCKING_STYLE=0
211 * -DSQLITE_THREADSAFE=0
212 * -DSQLITE_DEFAULT_FILE_FORMAT=4
213 * -DSQLITE_ENABLE_STAT2
214
215 The first and second symbol definitions above are required; the others
216 are merely recommended. The "localtime()" library function in SQLite must
217 be redefined to invoke fossil_localtime() instead. The fossil_localtime()
218 routine will invoke either gmtime() or localtime() depending on the
219
--- www/makefile.wiki
+++ www/makefile.wiki
@@ -208,11 +208,11 @@
208 * -Dlocaltime=fossil_localtime
209 * -DSQLITE_OMIT_LOAD_EXTENSION=1
210 * -DSQLITE_ENABLE_LOCKING_STYLE=0
211 * -DSQLITE_THREADSAFE=0
212 * -DSQLITE_DEFAULT_FILE_FORMAT=4
213 * -DSQLITE_ENABLE_STAT3
214
215 The first and second symbol definitions above are required; the others
216 are merely recommended. The "localtime()" library function in SQLite must
217 be redefined to invoke fossil_localtime() instead. The fossil_localtime()
218 routine will invoke either gmtime() or localtime() depending on the
219

Keyboard Shortcuts

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