Fossil SCM

Merge updates from trunk.

mistachkin 2011-10-19 01:29 UTC tcl-integration merge
Commit 62d928cb41a13700a1dcc2d51069b4b771ea566d
+10 -3
--- src/add.c
+++ src/add.c
@@ -57,14 +57,21 @@
5757
*/
5858
static const char *azManifest[] = {
5959
"manifest",
6060
"manifest.uuid",
6161
};
62
+
63
+ /* Cached setting "manifest" */
64
+ static int cachedManifest = -1;
65
+
66
+ if( cachedManifest == -1 ){
67
+ cachedManifest = db_get_boolean("manifest",0);
68
+ }
6269
6370
if( N>=0 && N<count(azName) ) return azName[N];
6471
if( N>=count(azName) && N<count(azName)+count(azManifest)
65
- && db_get_boolean("manifest",0) ){
72
+ && cachedManifest ){
6673
return azManifest[N-count(azName)];
6774
}
6875
return 0;
6976
}
7077
@@ -228,11 +235,11 @@
228235
int isDir;
229236
Blob fullName;
230237
231238
file_canonical_name(g.argv[i], &fullName);
232239
zName = blob_str(&fullName);
233
- isDir = file_isdir(zName);
240
+ isDir = file_wd_isdir(zName);
234241
if( isDir==1 ){
235242
vfile_scan(&fullName, nRoot-1, includeDotFiles, pIgnore);
236243
}else if( isDir==0 ){
237244
fossil_fatal("not found: %s", zName);
238245
}else if( file_access(zName, R_OK) ){
@@ -506,11 +513,11 @@
506513
"UPDATE vfile SET origname=pathname WHERE origname IS NULL;"
507514
);
508515
db_multi_exec(
509516
"CREATE TEMP TABLE mv(f TEXT UNIQUE ON CONFLICT IGNORE, t TEXT);"
510517
);
511
- if( file_isdir(zDest)!=1 ){
518
+ if( file_wd_isdir(zDest)!=1 ){
512519
Blob orig;
513520
if( g.argc!=4 ){
514521
usage("OLDNAME NEWNAME");
515522
}
516523
file_tree_name(g.argv[2], &orig, 1);
517524
--- src/add.c
+++ src/add.c
@@ -57,14 +57,21 @@
57 */
58 static const char *azManifest[] = {
59 "manifest",
60 "manifest.uuid",
61 };
 
 
 
 
 
 
 
62
63 if( N>=0 && N<count(azName) ) return azName[N];
64 if( N>=count(azName) && N<count(azName)+count(azManifest)
65 && db_get_boolean("manifest",0) ){
66 return azManifest[N-count(azName)];
67 }
68 return 0;
69 }
70
@@ -228,11 +235,11 @@
228 int isDir;
229 Blob fullName;
230
231 file_canonical_name(g.argv[i], &fullName);
232 zName = blob_str(&fullName);
233 isDir = file_isdir(zName);
234 if( isDir==1 ){
235 vfile_scan(&fullName, nRoot-1, includeDotFiles, pIgnore);
236 }else if( isDir==0 ){
237 fossil_fatal("not found: %s", zName);
238 }else if( file_access(zName, R_OK) ){
@@ -506,11 +513,11 @@
506 "UPDATE vfile SET origname=pathname WHERE origname IS NULL;"
507 );
508 db_multi_exec(
509 "CREATE TEMP TABLE mv(f TEXT UNIQUE ON CONFLICT IGNORE, t TEXT);"
510 );
511 if( file_isdir(zDest)!=1 ){
512 Blob orig;
513 if( g.argc!=4 ){
514 usage("OLDNAME NEWNAME");
515 }
516 file_tree_name(g.argv[2], &orig, 1);
517
--- src/add.c
+++ src/add.c
@@ -57,14 +57,21 @@
57 */
58 static const char *azManifest[] = {
59 "manifest",
60 "manifest.uuid",
61 };
62
63 /* Cached setting "manifest" */
64 static int cachedManifest = -1;
65
66 if( cachedManifest == -1 ){
67 cachedManifest = db_get_boolean("manifest",0);
68 }
69
70 if( N>=0 && N<count(azName) ) return azName[N];
71 if( N>=count(azName) && N<count(azName)+count(azManifest)
72 && cachedManifest ){
73 return azManifest[N-count(azName)];
74 }
75 return 0;
76 }
77
@@ -228,11 +235,11 @@
235 int isDir;
236 Blob fullName;
237
238 file_canonical_name(g.argv[i], &fullName);
239 zName = blob_str(&fullName);
240 isDir = file_wd_isdir(zName);
241 if( isDir==1 ){
242 vfile_scan(&fullName, nRoot-1, includeDotFiles, pIgnore);
243 }else if( isDir==0 ){
244 fossil_fatal("not found: %s", zName);
245 }else if( file_access(zName, R_OK) ){
@@ -506,11 +513,11 @@
513 "UPDATE vfile SET origname=pathname WHERE origname IS NULL;"
514 );
515 db_multi_exec(
516 "CREATE TEMP TABLE mv(f TEXT UNIQUE ON CONFLICT IGNORE, t TEXT);"
517 );
518 if( file_wd_isdir(zDest)!=1 ){
519 Blob orig;
520 if( g.argc!=4 ){
521 usage("OLDNAME NEWNAME");
522 }
523 file_tree_name(g.argv[2], &orig, 1);
524
+3 -3
--- src/allrepo.c
+++ src/allrepo.c
@@ -74,13 +74,13 @@
7474
** rebuild Rebuild on all repositories
7575
**
7676
** sync Run a "sync" on all repositories
7777
**
7878
** Respositories are automatically added to the set of known repositories
79
-** when one of the following commands against the repository: clone, info,
80
-** pull, push, or sync. Even previously ignored repositories are added back
81
-** to the list of repositories by these commands.
79
+** when one of the following commands are run against the repository: clone,
80
+** info, pull, push, or sync. Even previously ignored repositories are
81
+** added back to the list of repositories by these commands.
8282
*/
8383
void all_cmd(void){
8484
int n;
8585
Stmt q;
8686
const char *zCmd;
8787
--- src/allrepo.c
+++ src/allrepo.c
@@ -74,13 +74,13 @@
74 ** rebuild Rebuild on all repositories
75 **
76 ** sync Run a "sync" on all repositories
77 **
78 ** Respositories are automatically added to the set of known repositories
79 ** when one of the following commands against the repository: clone, info,
80 ** pull, push, or sync. Even previously ignored repositories are added back
81 ** to the list of repositories by these commands.
82 */
83 void all_cmd(void){
84 int n;
85 Stmt q;
86 const char *zCmd;
87
--- src/allrepo.c
+++ src/allrepo.c
@@ -74,13 +74,13 @@
74 ** rebuild Rebuild on all repositories
75 **
76 ** sync Run a "sync" on all repositories
77 **
78 ** Respositories are automatically added to the set of known repositories
79 ** when one of the following commands are run against the repository: clone,
80 ** info, pull, push, or sync. Even previously ignored repositories are
81 ** added back to the list of repositories by these commands.
82 */
83 void all_cmd(void){
84 int n;
85 Stmt q;
86 const char *zCmd;
87
+26
--- src/blob.c
+++ src/blob.c
@@ -311,10 +311,36 @@
311311
sz = szA<szB ? szA : szB;
312312
rc = memcmp(blob_buffer(pA), blob_buffer(pB), sz);
313313
if( rc==0 ){
314314
rc = szA - szB;
315315
}
316
+ return rc;
317
+}
318
+
319
+/*
320
+** Compare two blobs in constant time and return zero if they are equal.
321
+** Constant time comparison only applies for blobs of the same length.
322
+** If lengths are different, immediately returns 1.
323
+*/
324
+int blob_constant_time_cmp(Blob *pA, Blob *pB){
325
+ int szA, szB, i;
326
+ unsigned char *buf1, *buf2;
327
+ unsigned char rc = 0;
328
+
329
+ blob_is_init(pA);
330
+ blob_is_init(pB);
331
+ szA = blob_size(pA);
332
+ szB = blob_size(pB);
333
+ if( szA!=szB || szA==0 ) return 1;
334
+
335
+ buf1 = (unsigned char*)blob_buffer(pA);
336
+ buf2 = (unsigned char*)blob_buffer(pB);
337
+
338
+ for( i=0; i<szA; i++ ){
339
+ rc = rc | (buf1[i] ^ buf2[i]);
340
+ }
341
+
316342
return rc;
317343
}
318344
319345
/*
320346
** Compare a blob to a string. Return TRUE if they are equal.
321347
--- src/blob.c
+++ src/blob.c
@@ -311,10 +311,36 @@
311 sz = szA<szB ? szA : szB;
312 rc = memcmp(blob_buffer(pA), blob_buffer(pB), sz);
313 if( rc==0 ){
314 rc = szA - szB;
315 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
316 return rc;
317 }
318
319 /*
320 ** Compare a blob to a string. Return TRUE if they are equal.
321
--- src/blob.c
+++ src/blob.c
@@ -311,10 +311,36 @@
311 sz = szA<szB ? szA : szB;
312 rc = memcmp(blob_buffer(pA), blob_buffer(pB), sz);
313 if( rc==0 ){
314 rc = szA - szB;
315 }
316 return rc;
317 }
318
319 /*
320 ** Compare two blobs in constant time and return zero if they are equal.
321 ** Constant time comparison only applies for blobs of the same length.
322 ** If lengths are different, immediately returns 1.
323 */
324 int blob_constant_time_cmp(Blob *pA, Blob *pB){
325 int szA, szB, i;
326 unsigned char *buf1, *buf2;
327 unsigned char rc = 0;
328
329 blob_is_init(pA);
330 blob_is_init(pB);
331 szA = blob_size(pA);
332 szB = blob_size(pB);
333 if( szA!=szB || szA==0 ) return 1;
334
335 buf1 = (unsigned char*)blob_buffer(pA);
336 buf2 = (unsigned char*)blob_buffer(pB);
337
338 for( i=0; i<szA; i++ ){
339 rc = rc | (buf1[i] ^ buf2[i]);
340 }
341
342 return rc;
343 }
344
345 /*
346 ** Compare a blob to a string. Return TRUE if they are equal.
347
+3 -1
--- src/browse.c
+++ src/browse.c
@@ -249,11 +249,13 @@
249249
/* Generate a multi-column table listing the contents of zD[]
250250
** directory.
251251
*/
252252
mxLen = db_int(12, "SELECT max(length(x)) FROM localfiles /*scan*/");
253253
cnt = db_int(0, "SELECT count(*) FROM localfiles /*scan*/");
254
- nCol = 4;
254
+ nCol = 100/mxLen;
255
+ if( nCol<1 ) nCol = 1;
256
+ if( nCol>5 ) nCol = 5;
255257
nRow = (cnt+nCol-1)/nCol;
256258
db_prepare(&q, "SELECT x, u FROM localfiles ORDER BY x /*scan*/");
257259
@ <table class="browser"><tr><td class="browser"><ul class="browser">
258260
i = 0;
259261
while( db_step(&q)==SQLITE_ROW ){
260262
--- src/browse.c
+++ src/browse.c
@@ -249,11 +249,13 @@
249 /* Generate a multi-column table listing the contents of zD[]
250 ** directory.
251 */
252 mxLen = db_int(12, "SELECT max(length(x)) FROM localfiles /*scan*/");
253 cnt = db_int(0, "SELECT count(*) FROM localfiles /*scan*/");
254 nCol = 4;
 
 
255 nRow = (cnt+nCol-1)/nCol;
256 db_prepare(&q, "SELECT x, u FROM localfiles ORDER BY x /*scan*/");
257 @ <table class="browser"><tr><td class="browser"><ul class="browser">
258 i = 0;
259 while( db_step(&q)==SQLITE_ROW ){
260
--- src/browse.c
+++ src/browse.c
@@ -249,11 +249,13 @@
249 /* Generate a multi-column table listing the contents of zD[]
250 ** directory.
251 */
252 mxLen = db_int(12, "SELECT max(length(x)) FROM localfiles /*scan*/");
253 cnt = db_int(0, "SELECT count(*) FROM localfiles /*scan*/");
254 nCol = 100/mxLen;
255 if( nCol<1 ) nCol = 1;
256 if( nCol>5 ) nCol = 5;
257 nRow = (cnt+nCol-1)/nCol;
258 db_prepare(&q, "SELECT x, u FROM localfiles ORDER BY x /*scan*/");
259 @ <table class="browser"><tr><td class="browser"><ul class="browser">
260 i = 0;
261 while( db_step(&q)==SQLITE_ROW ){
262
+15 -17
--- src/cgi.c
+++ src/cgi.c
@@ -135,11 +135,11 @@
135135
}
136136
137137
/*
138138
** Return a pointer to the HTTP reply text.
139139
*/
140
-char *cgi_extract_content(int *pnAmt){
140
+char *cgi_extract_content(void){
141141
cgi_combine_header_and_body();
142142
return blob_buffer(&cgiContent[0]);
143143
}
144144
145145
/*
@@ -325,11 +325,11 @@
325325
*/
326326
/*time_t expires = time(0) + atoi(db_config("constant_expires","604800"));*/
327327
time_t expires = time(0) + 604800;
328328
fprintf(g.httpOut, "Expires: %s\r\n", cgi_rfc822_datestamp(expires));
329329
}else{
330
- fprintf(g.httpOut, "Cache-control: no-cache, no-store\r\n");
330
+ fprintf(g.httpOut, "Cache-control: no-cache\r\n");
331331
}
332332
333333
/* Content intended for logged in users should only be cached in
334334
** the browser, not some shared location.
335335
*/
@@ -362,11 +362,11 @@
362362
/*
363363
** Do a redirect request to the URL given in the argument.
364364
**
365365
** The URL must be relative to the base of the fossil server.
366366
*/
367
-void cgi_redirect(const char *zURL){
367
+NORETURN void cgi_redirect(const char *zURL){
368368
char *zLocation;
369369
CGIDEBUG(("redirect to %s\n", zURL));
370370
if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
371371
zLocation = mprintf("Location: %s\r\n", zURL);
372372
}else if( *zURL=='/' ){
@@ -381,11 +381,11 @@
381381
cgi_set_status(302, "Moved Temporarily");
382382
free(zLocation);
383383
cgi_reply();
384384
fossil_exit(0);
385385
}
386
-void cgi_redirectf(const char *zFormat, ...){
386
+NORETURN void cgi_redirectf(const char *zFormat, ...){
387387
va_list ap;
388388
va_start(ap, zFormat);
389389
cgi_redirect(vmprintf(zFormat, ap));
390390
va_end(ap);
391391
}
@@ -888,26 +888,23 @@
888888
}
889889
890890
/*
891891
** Print all query parameters on standard output. Format the
892892
** parameters as HTML. This is used for testing and debugging.
893
-** Release builds omit the values of the cookies to avoid defeating
894
-** the purpose of setting HttpOnly cookies.
893
+**
894
+** Omit the values of the cookies unless showAll is true.
895895
*/
896
-void cgi_print_all(void){
896
+void cgi_print_all(int showAll){
897897
int i;
898
- int showAll = 0;
899
-#ifdef FOSSIL_DEBUG
900
- /* Show the values of cookies in debug mode. */
901
- showAll = 1;
902
-#endif
903898
cgi_parameter("",""); /* Force the parameters into sorted order */
904899
for(i=0; i<nUsedQP; i++){
905
- if( showAll || (fossil_stricmp("HTTP_COOKIE",aParamQP[i].zName)!=0 && fossil_strnicmp("fossil-",aParamQP[i].zName,7)!=0) ){
906
- cgi_printf("%s = %s <br />\n",
907
- htmlize(aParamQP[i].zName, -1), htmlize(aParamQP[i].zValue, -1));
900
+ const char *zName = aParamQP[i].zName;
901
+ if( !showAll ){
902
+ if( fossil_stricmp("HTTP_COOKIE",zName)==0 ) continue;
903
+ if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
908904
}
905
+ cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
909906
}
910907
}
911908
912909
/*
913910
** This routine works like "printf" except that it has the
@@ -930,11 +927,11 @@
930927
931928
932929
/*
933930
** Send a reply indicating that the HTTP request was malformed
934931
*/
935
-static void malformed_request(void){
932
+static NORETURN void malformed_request(void){
936933
cgi_set_status(501, "Not Implemented");
937934
cgi_printf(
938935
"<html><body>Unrecognized HTTP Request</body></html>\n"
939936
);
940937
cgi_reply();
@@ -942,11 +939,11 @@
942939
}
943940
944941
/*
945942
** Panic and die while processing a webpage.
946943
*/
947
-void cgi_panic(const char *zFormat, ...){
944
+NORETURN void cgi_panic(const char *zFormat, ...){
948945
va_list ap;
949946
cgi_reset_content();
950947
cgi_set_status(500, "Internal Server Error");
951948
cgi_printf(
952949
"<html><body><h1>Internal Server Error</h1>\n"
@@ -1165,10 +1162,11 @@
11651162
sleep( nchildren-MAX_PARALLEL );
11661163
}
11671164
delay.tv_sec = 60;
11681165
delay.tv_usec = 0;
11691166
FD_ZERO(&readfds);
1167
+ assert( listener>=0 );
11701168
FD_SET( listener, &readfds);
11711169
select( listener+1, &readfds, 0, 0, &delay);
11721170
if( FD_ISSET(listener, &readfds) ){
11731171
lenaddr = sizeof(inaddr);
11741172
connection = accept(listener, (struct sockaddr*)&inaddr, &lenaddr);
11751173
--- src/cgi.c
+++ src/cgi.c
@@ -135,11 +135,11 @@
135 }
136
137 /*
138 ** Return a pointer to the HTTP reply text.
139 */
140 char *cgi_extract_content(int *pnAmt){
141 cgi_combine_header_and_body();
142 return blob_buffer(&cgiContent[0]);
143 }
144
145 /*
@@ -325,11 +325,11 @@
325 */
326 /*time_t expires = time(0) + atoi(db_config("constant_expires","604800"));*/
327 time_t expires = time(0) + 604800;
328 fprintf(g.httpOut, "Expires: %s\r\n", cgi_rfc822_datestamp(expires));
329 }else{
330 fprintf(g.httpOut, "Cache-control: no-cache, no-store\r\n");
331 }
332
333 /* Content intended for logged in users should only be cached in
334 ** the browser, not some shared location.
335 */
@@ -362,11 +362,11 @@
362 /*
363 ** Do a redirect request to the URL given in the argument.
364 **
365 ** The URL must be relative to the base of the fossil server.
366 */
367 void cgi_redirect(const char *zURL){
368 char *zLocation;
369 CGIDEBUG(("redirect to %s\n", zURL));
370 if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
371 zLocation = mprintf("Location: %s\r\n", zURL);
372 }else if( *zURL=='/' ){
@@ -381,11 +381,11 @@
381 cgi_set_status(302, "Moved Temporarily");
382 free(zLocation);
383 cgi_reply();
384 fossil_exit(0);
385 }
386 void cgi_redirectf(const char *zFormat, ...){
387 va_list ap;
388 va_start(ap, zFormat);
389 cgi_redirect(vmprintf(zFormat, ap));
390 va_end(ap);
391 }
@@ -888,26 +888,23 @@
888 }
889
890 /*
891 ** Print all query parameters on standard output. Format the
892 ** parameters as HTML. This is used for testing and debugging.
893 ** Release builds omit the values of the cookies to avoid defeating
894 ** the purpose of setting HttpOnly cookies.
895 */
896 void cgi_print_all(void){
897 int i;
898 int showAll = 0;
899 #ifdef FOSSIL_DEBUG
900 /* Show the values of cookies in debug mode. */
901 showAll = 1;
902 #endif
903 cgi_parameter("",""); /* Force the parameters into sorted order */
904 for(i=0; i<nUsedQP; i++){
905 if( showAll || (fossil_stricmp("HTTP_COOKIE",aParamQP[i].zName)!=0 && fossil_strnicmp("fossil-",aParamQP[i].zName,7)!=0) ){
906 cgi_printf("%s = %s <br />\n",
907 htmlize(aParamQP[i].zName, -1), htmlize(aParamQP[i].zValue, -1));
 
908 }
 
909 }
910 }
911
912 /*
913 ** This routine works like "printf" except that it has the
@@ -930,11 +927,11 @@
930
931
932 /*
933 ** Send a reply indicating that the HTTP request was malformed
934 */
935 static void malformed_request(void){
936 cgi_set_status(501, "Not Implemented");
937 cgi_printf(
938 "<html><body>Unrecognized HTTP Request</body></html>\n"
939 );
940 cgi_reply();
@@ -942,11 +939,11 @@
942 }
943
944 /*
945 ** Panic and die while processing a webpage.
946 */
947 void cgi_panic(const char *zFormat, ...){
948 va_list ap;
949 cgi_reset_content();
950 cgi_set_status(500, "Internal Server Error");
951 cgi_printf(
952 "<html><body><h1>Internal Server Error</h1>\n"
@@ -1165,10 +1162,11 @@
1165 sleep( nchildren-MAX_PARALLEL );
1166 }
1167 delay.tv_sec = 60;
1168 delay.tv_usec = 0;
1169 FD_ZERO(&readfds);
 
1170 FD_SET( listener, &readfds);
1171 select( listener+1, &readfds, 0, 0, &delay);
1172 if( FD_ISSET(listener, &readfds) ){
1173 lenaddr = sizeof(inaddr);
1174 connection = accept(listener, (struct sockaddr*)&inaddr, &lenaddr);
1175
--- src/cgi.c
+++ src/cgi.c
@@ -135,11 +135,11 @@
135 }
136
137 /*
138 ** Return a pointer to the HTTP reply text.
139 */
140 char *cgi_extract_content(void){
141 cgi_combine_header_and_body();
142 return blob_buffer(&cgiContent[0]);
143 }
144
145 /*
@@ -325,11 +325,11 @@
325 */
326 /*time_t expires = time(0) + atoi(db_config("constant_expires","604800"));*/
327 time_t expires = time(0) + 604800;
328 fprintf(g.httpOut, "Expires: %s\r\n", cgi_rfc822_datestamp(expires));
329 }else{
330 fprintf(g.httpOut, "Cache-control: no-cache\r\n");
331 }
332
333 /* Content intended for logged in users should only be cached in
334 ** the browser, not some shared location.
335 */
@@ -362,11 +362,11 @@
362 /*
363 ** Do a redirect request to the URL given in the argument.
364 **
365 ** The URL must be relative to the base of the fossil server.
366 */
367 NORETURN void cgi_redirect(const char *zURL){
368 char *zLocation;
369 CGIDEBUG(("redirect to %s\n", zURL));
370 if( strncmp(zURL,"http:",5)==0 || strncmp(zURL,"https:",6)==0 ){
371 zLocation = mprintf("Location: %s\r\n", zURL);
372 }else if( *zURL=='/' ){
@@ -381,11 +381,11 @@
381 cgi_set_status(302, "Moved Temporarily");
382 free(zLocation);
383 cgi_reply();
384 fossil_exit(0);
385 }
386 NORETURN void cgi_redirectf(const char *zFormat, ...){
387 va_list ap;
388 va_start(ap, zFormat);
389 cgi_redirect(vmprintf(zFormat, ap));
390 va_end(ap);
391 }
@@ -888,26 +888,23 @@
888 }
889
890 /*
891 ** Print all query parameters on standard output. Format the
892 ** parameters as HTML. This is used for testing and debugging.
893 **
894 ** Omit the values of the cookies unless showAll is true.
895 */
896 void cgi_print_all(int showAll){
897 int i;
 
 
 
 
 
898 cgi_parameter("",""); /* Force the parameters into sorted order */
899 for(i=0; i<nUsedQP; i++){
900 const char *zName = aParamQP[i].zName;
901 if( !showAll ){
902 if( fossil_stricmp("HTTP_COOKIE",zName)==0 ) continue;
903 if( fossil_strnicmp("fossil-",zName,7)==0 ) continue;
904 }
905 cgi_printf("%h = %h <br />\n", zName, aParamQP[i].zValue);
906 }
907 }
908
909 /*
910 ** This routine works like "printf" except that it has the
@@ -930,11 +927,11 @@
927
928
929 /*
930 ** Send a reply indicating that the HTTP request was malformed
931 */
932 static NORETURN void malformed_request(void){
933 cgi_set_status(501, "Not Implemented");
934 cgi_printf(
935 "<html><body>Unrecognized HTTP Request</body></html>\n"
936 );
937 cgi_reply();
@@ -942,11 +939,11 @@
939 }
940
941 /*
942 ** Panic and die while processing a webpage.
943 */
944 NORETURN void cgi_panic(const char *zFormat, ...){
945 va_list ap;
946 cgi_reset_content();
947 cgi_set_status(500, "Internal Server Error");
948 cgi_printf(
949 "<html><body><h1>Internal Server Error</h1>\n"
@@ -1165,10 +1162,11 @@
1162 sleep( nchildren-MAX_PARALLEL );
1163 }
1164 delay.tv_sec = 60;
1165 delay.tv_usec = 0;
1166 FD_ZERO(&readfds);
1167 assert( listener>=0 );
1168 FD_SET( listener, &readfds);
1169 select( listener+1, &readfds, 0, 0, &delay);
1170 if( FD_ISSET(listener, &readfds) ){
1171 lenaddr = sizeof(inaddr);
1172 connection = accept(listener, (struct sockaddr*)&inaddr, &lenaddr);
1173
+11 -12
--- src/checkin.c
+++ src/checkin.c
@@ -282,18 +282,16 @@
282282
Stmt q;
283283
int n;
284284
const char *zIgnoreFlag = find_option("ignore",0,1);
285285
int allFlag = find_option("dotfiles",0,0)!=0;
286286
int cwdRelative = 0;
287
- int outputManifest;
288287
Glob *pIgnore;
289288
Blob rewrittenPathname;
290289
const char *zPathname, *zDisplayName;
291290
292291
db_must_be_within_tree();
293292
cwdRelative = determine_cwd_relative_option();
294
- outputManifest = db_get_boolean("manifest",0);
295293
db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
296294
n = strlen(g.zLocalRoot);
297295
blob_init(&path, g.zLocalRoot, n-1);
298296
if( zIgnoreFlag==0 ){
299297
zIgnoreFlag = db_get("ignore-glob", 0);
@@ -658,30 +656,31 @@
658656
while( db_step(&q)==SQLITE_ROW ){
659657
const char *zName = db_column_text(&q, 0);
660658
const char *zUuid = db_column_text(&q, 1);
661659
const char *zOrig = db_column_text(&q, 2);
662660
int frid = db_column_int(&q, 3);
663
- int isexe = db_column_int(&q, 4);
661
+ int isExe = db_column_int(&q, 4);
664662
int isLink = db_column_int(&q, 5);
665663
int isSelected = db_column_int(&q, 6);
666664
const char *zPerm;
667665
int cmp;
668666
#if !defined(_WIN32)
669
- /* For unix, extract the "executable" permission bit directly from
670
- ** the filesystem. On windows, the "executable" bit is retained
667
+ int mPerm;
668
+
669
+ /* For unix, extract the "executable" and "symlink" permissions
670
+ ** directly from the filesystem. On windows, permissions are
671671
** unchanged from the original.
672672
*/
673
+
673674
blob_resize(&filename, nBasename);
674675
blob_append(&filename, zName, -1);
675
- isexe = file_wd_isexe(blob_str(&filename));
676
-
677
- /* For unix, check if the file on the filesystem is symlink.
678
- ** On windows, the bit is retained unchanged from original.
679
- */
680
- isLink = file_wd_islink(blob_str(&filename));
676
+
677
+ mPerm = file_wd_perm(blob_str(&filename));
678
+ isExe = ( mPerm==PERM_EXE );
679
+ isLink = ( mPerm==PERM_LNK );
681680
#endif
682
- if( isexe ){
681
+ if( isExe ){
683682
zPerm = " x";
684683
}else if( isLink ){
685684
zPerm = " l"; /* note: symlinks don't have executable bit on unix */
686685
}else{
687686
zPerm = "";
688687
--- src/checkin.c
+++ src/checkin.c
@@ -282,18 +282,16 @@
282 Stmt q;
283 int n;
284 const char *zIgnoreFlag = find_option("ignore",0,1);
285 int allFlag = find_option("dotfiles",0,0)!=0;
286 int cwdRelative = 0;
287 int outputManifest;
288 Glob *pIgnore;
289 Blob rewrittenPathname;
290 const char *zPathname, *zDisplayName;
291
292 db_must_be_within_tree();
293 cwdRelative = determine_cwd_relative_option();
294 outputManifest = db_get_boolean("manifest",0);
295 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
296 n = strlen(g.zLocalRoot);
297 blob_init(&path, g.zLocalRoot, n-1);
298 if( zIgnoreFlag==0 ){
299 zIgnoreFlag = db_get("ignore-glob", 0);
@@ -658,30 +656,31 @@
658 while( db_step(&q)==SQLITE_ROW ){
659 const char *zName = db_column_text(&q, 0);
660 const char *zUuid = db_column_text(&q, 1);
661 const char *zOrig = db_column_text(&q, 2);
662 int frid = db_column_int(&q, 3);
663 int isexe = db_column_int(&q, 4);
664 int isLink = db_column_int(&q, 5);
665 int isSelected = db_column_int(&q, 6);
666 const char *zPerm;
667 int cmp;
668 #if !defined(_WIN32)
669 /* For unix, extract the "executable" permission bit directly from
670 ** the filesystem. On windows, the "executable" bit is retained
 
 
671 ** unchanged from the original.
672 */
 
673 blob_resize(&filename, nBasename);
674 blob_append(&filename, zName, -1);
675 isexe = file_wd_isexe(blob_str(&filename));
676
677 /* For unix, check if the file on the filesystem is symlink.
678 ** On windows, the bit is retained unchanged from original.
679 */
680 isLink = file_wd_islink(blob_str(&filename));
681 #endif
682 if( isexe ){
683 zPerm = " x";
684 }else if( isLink ){
685 zPerm = " l"; /* note: symlinks don't have executable bit on unix */
686 }else{
687 zPerm = "";
688
--- src/checkin.c
+++ src/checkin.c
@@ -282,18 +282,16 @@
282 Stmt q;
283 int n;
284 const char *zIgnoreFlag = find_option("ignore",0,1);
285 int allFlag = find_option("dotfiles",0,0)!=0;
286 int cwdRelative = 0;
 
287 Glob *pIgnore;
288 Blob rewrittenPathname;
289 const char *zPathname, *zDisplayName;
290
291 db_must_be_within_tree();
292 cwdRelative = determine_cwd_relative_option();
 
293 db_multi_exec("CREATE TEMP TABLE sfile(x TEXT PRIMARY KEY)");
294 n = strlen(g.zLocalRoot);
295 blob_init(&path, g.zLocalRoot, n-1);
296 if( zIgnoreFlag==0 ){
297 zIgnoreFlag = db_get("ignore-glob", 0);
@@ -658,30 +656,31 @@
656 while( db_step(&q)==SQLITE_ROW ){
657 const char *zName = db_column_text(&q, 0);
658 const char *zUuid = db_column_text(&q, 1);
659 const char *zOrig = db_column_text(&q, 2);
660 int frid = db_column_int(&q, 3);
661 int isExe = db_column_int(&q, 4);
662 int isLink = db_column_int(&q, 5);
663 int isSelected = db_column_int(&q, 6);
664 const char *zPerm;
665 int cmp;
666 #if !defined(_WIN32)
667 int mPerm;
668
669 /* For unix, extract the "executable" and "symlink" permissions
670 ** directly from the filesystem. On windows, permissions are
671 ** unchanged from the original.
672 */
673
674 blob_resize(&filename, nBasename);
675 blob_append(&filename, zName, -1);
676
677 mPerm = file_wd_perm(blob_str(&filename));
678 isExe = ( mPerm==PERM_EXE );
679 isLink = ( mPerm==PERM_LNK );
 
 
680 #endif
681 if( isExe ){
682 zPerm = " x";
683 }else if( isLink ){
684 zPerm = " l"; /* note: symlinks don't have executable bit on unix */
685 }else{
686 zPerm = "";
687
--- src/config.h
+++ src/config.h
@@ -134,6 +134,15 @@
134134
#else /* Generates a warning - but it always works */
135135
# define FOSSIL_INT_TO_PTR(X) ((void*)(X))
136136
# define FOSSIL_PTR_TO_INT(X) ((int)(X))
137137
#endif
138138
139
+/*
140
+** A marker for functions that never return.
141
+*/
142
+#if defined(__GNUC__) || defined(__clang__)
143
+# define NORETURN __attribute__((__noreturn__))
144
+#else
145
+# define NORETURN
146
+#endif
147
+
139148
#endif /* _RC_COMPILE_ */
140149
--- src/config.h
+++ src/config.h
@@ -134,6 +134,15 @@
134 #else /* Generates a warning - but it always works */
135 # define FOSSIL_INT_TO_PTR(X) ((void*)(X))
136 # define FOSSIL_PTR_TO_INT(X) ((int)(X))
137 #endif
138
 
 
 
 
 
 
 
 
 
139 #endif /* _RC_COMPILE_ */
140
--- src/config.h
+++ src/config.h
@@ -134,6 +134,15 @@
134 #else /* Generates a warning - but it always works */
135 # define FOSSIL_INT_TO_PTR(X) ((void*)(X))
136 # define FOSSIL_PTR_TO_INT(X) ((int)(X))
137 #endif
138
139 /*
140 ** A marker for functions that never return.
141 */
142 #if defined(__GNUC__) || defined(__clang__)
143 # define NORETURN __attribute__((__noreturn__))
144 #else
145 # define NORETURN
146 #endif
147
148 #endif /* _RC_COMPILE_ */
149
--- src/configure.c
+++ src/configure.c
@@ -785,10 +785,11 @@
785785
const char *zMethod;
786786
if( g.argc<3 ){
787787
usage("export|import|merge|pull|reset ...");
788788
}
789789
db_find_and_open_repository(0, 0);
790
+ db_open_config(0);
790791
zMethod = g.argv[2];
791792
n = strlen(zMethod);
792793
if( strncmp(zMethod, "export", n)==0 ){
793794
int mask;
794795
const char *zSince = find_option("since",0,1);
795796
--- src/configure.c
+++ src/configure.c
@@ -785,10 +785,11 @@
785 const char *zMethod;
786 if( g.argc<3 ){
787 usage("export|import|merge|pull|reset ...");
788 }
789 db_find_and_open_repository(0, 0);
 
790 zMethod = g.argv[2];
791 n = strlen(zMethod);
792 if( strncmp(zMethod, "export", n)==0 ){
793 int mask;
794 const char *zSince = find_option("since",0,1);
795
--- src/configure.c
+++ src/configure.c
@@ -785,10 +785,11 @@
785 const char *zMethod;
786 if( g.argc<3 ){
787 usage("export|import|merge|pull|reset ...");
788 }
789 db_find_and_open_repository(0, 0);
790 db_open_config(0);
791 zMethod = g.argv[2];
792 n = strlen(zMethod);
793 if( strncmp(zMethod, "export", n)==0 ){
794 int mask;
795 const char *zSince = find_option("since",0,1);
796
+2 -2
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742742
/*
743743
* * Returns TRUE if zTable exists in the local database.
744744
*/
745745
static int db_local_table_exists(const char *zTable){
746746
return db_exists("SELECT 1 FROM %s.sqlite_master"
747
- " WHERE name=='%s'",
747
+ " WHERE name=='%s' /*scan*/",
748748
db_name("localdb"), zTable);
749749
}
750750
751751
/*
752752
** Returns TRUE if zColumn exists in zTable in the local database.
753753
*/
754754
static int db_local_column_exists(const char *zTable, const char *zColumn){
755755
return db_exists("SELECT 1 FROM %s.sqlite_master"
756
- " WHERE name=='%s' AND sql GLOB '* %s *'",
756
+ " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/",
757757
db_name("localdb"), zTable, zColumn);
758758
}
759759
760760
/*
761761
** If zDbName is a valid local database file, open it and return
762762
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742 /*
743 * * Returns TRUE if zTable exists in the local database.
744 */
745 static int db_local_table_exists(const char *zTable){
746 return db_exists("SELECT 1 FROM %s.sqlite_master"
747 " WHERE name=='%s'",
748 db_name("localdb"), zTable);
749 }
750
751 /*
752 ** Returns TRUE if zColumn exists in zTable in the local database.
753 */
754 static int db_local_column_exists(const char *zTable, const char *zColumn){
755 return db_exists("SELECT 1 FROM %s.sqlite_master"
756 " WHERE name=='%s' AND sql GLOB '* %s *'",
757 db_name("localdb"), zTable, zColumn);
758 }
759
760 /*
761 ** If zDbName is a valid local database file, open it and return
762
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742 /*
743 * * Returns TRUE if zTable exists in the local database.
744 */
745 static int db_local_table_exists(const char *zTable){
746 return db_exists("SELECT 1 FROM %s.sqlite_master"
747 " WHERE name=='%s' /*scan*/",
748 db_name("localdb"), zTable);
749 }
750
751 /*
752 ** Returns TRUE if zColumn exists in zTable in the local database.
753 */
754 static int db_local_column_exists(const char *zTable, const char *zColumn){
755 return db_exists("SELECT 1 FROM %s.sqlite_master"
756 " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/",
757 db_name("localdb"), zTable, zColumn);
758 }
759
760 /*
761 ** If zDbName is a valid local database file, open it and return
762
+2 -2
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742742
/*
743743
* * Returns TRUE if zTable exists in the local database.
744744
*/
745745
static int db_local_table_exists(const char *zTable){
746746
return db_exists("SELECT 1 FROM %s.sqlite_master"
747
- " WHERE name=='%s'",
747
+ " WHERE name=='%s' /*scan*/",
748748
db_name("localdb"), zTable);
749749
}
750750
751751
/*
752752
** Returns TRUE if zColumn exists in zTable in the local database.
753753
*/
754754
static int db_local_column_exists(const char *zTable, const char *zColumn){
755755
return db_exists("SELECT 1 FROM %s.sqlite_master"
756
- " WHERE name=='%s' AND sql GLOB '* %s *'",
756
+ " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/",
757757
db_name("localdb"), zTable, zColumn);
758758
}
759759
760760
/*
761761
** If zDbName is a valid local database file, open it and return
762762
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742 /*
743 * * Returns TRUE if zTable exists in the local database.
744 */
745 static int db_local_table_exists(const char *zTable){
746 return db_exists("SELECT 1 FROM %s.sqlite_master"
747 " WHERE name=='%s'",
748 db_name("localdb"), zTable);
749 }
750
751 /*
752 ** Returns TRUE if zColumn exists in zTable in the local database.
753 */
754 static int db_local_column_exists(const char *zTable, const char *zColumn){
755 return db_exists("SELECT 1 FROM %s.sqlite_master"
756 " WHERE name=='%s' AND sql GLOB '* %s *'",
757 db_name("localdb"), zTable, zColumn);
758 }
759
760 /*
761 ** If zDbName is a valid local database file, open it and return
762
--- src/db.c
+++ src/db.c
@@ -742,20 +742,20 @@
742 /*
743 * * Returns TRUE if zTable exists in the local database.
744 */
745 static int db_local_table_exists(const char *zTable){
746 return db_exists("SELECT 1 FROM %s.sqlite_master"
747 " WHERE name=='%s' /*scan*/",
748 db_name("localdb"), zTable);
749 }
750
751 /*
752 ** Returns TRUE if zColumn exists in zTable in the local database.
753 */
754 static int db_local_column_exists(const char *zTable, const char *zColumn){
755 return db_exists("SELECT 1 FROM %s.sqlite_master"
756 " WHERE name=='%s' AND sql GLOB '* %s *' /*scan*/",
757 db_name("localdb"), zTable, zColumn);
758 }
759
760 /*
761 ** If zDbName is a valid local database file, open it and return
762
+184 -1
--- src/diff.c
+++ src/diff.c
@@ -578,10 +578,194 @@
578578
free(c.aFrom);
579579
free(c.aTo);
580580
return c.aEdit;
581581
}
582582
}
583
+
584
+/*
585
+** Copy a line with a limit. Used for side-by-side diffs to enforce a maximum
586
+** line length limit.
587
+*/
588
+static char *copylimline(char *out, DLine *dl, int lim){
589
+ int len;
590
+ len = dl->h & LENGTH_MASK;
591
+ if( lim && len > lim ){
592
+ memcpy(out, dl->z, lim-3);
593
+ strcpy(&out[lim-3], "...");
594
+ }else{
595
+ memcpy(out, dl->z, len);
596
+ out[len] = '\0';
597
+ }
598
+ return out;
599
+}
600
+
601
+/*
602
+** Output table body of a side-by-side diff. Prior to the call, the caller
603
+** should have output:
604
+** <table class="sbsdiff">
605
+** <tr><th colspan="2" class="diffhdr">Old title</th><th/>
606
+** <th colspan="2" class="diffhdr">New title</th></tr>
607
+**
608
+** And after the call, it should output:
609
+** </table>
610
+**
611
+** Some good reference diffs in the fossil repository for testing:
612
+** /vdiff?from=080d27a&to=4b0f813&detail=1
613
+** /vdiff?from=636804745b&to=c1d78e0556&detail=1
614
+** /vdiff?from=c0b6c28d29&to=25169506b7&detail=1
615
+** /vdiff?from=e3d022dffa&to=48bcfbd47b&detail=1
616
+*/
617
+int html_sbsdiff(
618
+ Blob *pA_Blob, /* FROM file */
619
+ Blob *pB_Blob, /* TO file */
620
+ int nContext, /* Amount of context to unified diff */
621
+ int ignoreEolWs /* Ignore whitespace at the end of lines */
622
+){
623
+ DContext c;
624
+ int i;
625
+ int iFrom, iTo;
626
+ char *linebuf;
627
+ int collim=0; /* Currently not settable; allows a column limit for diffs */
628
+ int allowExp=0; /* Currently not settable; (dis)allow expansion of rows */
629
+
630
+ /* Prepare the input files */
631
+ memset(&c, 0, sizeof(c));
632
+ c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
633
+ &c.nFrom, ignoreEolWs);
634
+ c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
635
+ &c.nTo, ignoreEolWs);
636
+ if( c.aFrom==0 || c.aTo==0 ){
637
+ free(c.aFrom);
638
+ free(c.aTo);
639
+ /* Note: This would be generated within a table. */
640
+ @ <p class="generalError" style="white-space: nowrap">cannot compute
641
+ @ difference between binary files</p>
642
+ return 0;
643
+ }
644
+
645
+ collim = collim < 4 ? 0 : collim;
646
+
647
+ /* Compute the difference */
648
+ diff_all(&c);
649
+
650
+ linebuf = fossil_malloc(LENGTH_MASK+1);
651
+ if( !linebuf ){
652
+ free(c.aFrom);
653
+ free(c.aTo);
654
+ free(c.aEdit);
655
+ return 0;
656
+ }
657
+
658
+ iFrom=iTo=0;
659
+ i=0;
660
+ while( i<c.nEdit ){
661
+ int j;
662
+ /* Copied lines */
663
+ for( j=0; j<c.aEdit[i]; j++){
664
+ /* Hide lines which are copied and are further away from block boundaries
665
+ ** than nConext lines. For each block with hidden lines, show a row
666
+ ** notifying the user about the hidden rows.
667
+ */
668
+ if( j<nContext || j>c.aEdit[i]-nContext-1 ){
669
+ @ <tr>
670
+ }else if( j==nContext && j<c.aEdit[i]-nContext-1 ){
671
+ @ <tr>
672
+ @ <td class="meta" colspan="5" style="white-space: nowrap;">
673
+ @ %d(c.aEdit[i]-2*nContext) hidden lines</td>
674
+ @ </tr>
675
+ if( !allowExp )
676
+ continue;
677
+ @ <tr style="display:none;">
678
+ }else{
679
+ if( !allowExp )
680
+ continue;
681
+ @ <tr style="display:none;">
682
+ }
683
+
684
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
685
+ @ <td class="lineno">%d(iFrom+j+1)</td>
686
+ @ <td class="srcline">%h(linebuf)</td>
687
+
688
+ @ <td> </td>
689
+
690
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
691
+ @ <td class="lineno">%d(iTo+j+1)</td>
692
+ @ <td class="srcline">%h(linebuf)</td>
693
+
694
+ @ </tr>
695
+ }
696
+ iFrom+=c.aEdit[i];
697
+ iTo+=c.aEdit[i];
698
+
699
+ if( c.aEdit[i+1]!=0 && c.aEdit[i+2]!=0 ){
700
+ int lim;
701
+ lim = c.aEdit[i+1] > c.aEdit[i+2] ? c.aEdit[i+1] : c.aEdit[i+2];
702
+
703
+ /* Assume changed lines */
704
+ for( j=0; j<lim; j++ ){
705
+ @ <tr>
706
+
707
+ if( j<c.aEdit[i+1] ){
708
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
709
+ @ <td class="changed lineno">%d(iFrom+j+1)</td>
710
+ @ <td class="changed srcline">%h(linebuf)</td>
711
+ }else{
712
+ @ <td colspan="2" class="changedvoid"/>
713
+ }
714
+
715
+ @ <td class="changed">|</td>
716
+
717
+ if( j<c.aEdit[i+2] ){
718
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
719
+ @ <td class="changed lineno">%d(iTo+j+1)</td>
720
+ @ <td class="changed srcline">%h(linebuf)</td>
721
+ }else{
722
+ @ <td colspan="2" class="changedvoid"/>
723
+ }
724
+
725
+ @ </tr>
726
+ }
727
+ iFrom+=c.aEdit[i+1];
728
+ iTo+=c.aEdit[i+2];
729
+ }else{
730
+
731
+ /* Process deleted lines */
732
+ for( j=0; j<c.aEdit[i+1]; j++ ){
733
+ @ <tr>
734
+
735
+ copylimline(linebuf, &c.aFrom[iFrom+j], collim);
736
+ @ <td class="removed lineno">%d(iFrom+j+1)</td>
737
+ @ <td class="removed srcline">%h(linebuf)</td>
738
+ @ <td>&lt;</td>
739
+ @ <td colspan="2" class="removedvoid"/>
740
+ @ </tr>
741
+ }
742
+ iFrom+=c.aEdit[i+1];
743
+
744
+ /* Process inserted lines */
745
+ for( j=0; j<c.aEdit[i+2]; j++ ){
746
+ @ <tr>
747
+ @ <td colspan="2" class="addedvoid"/>
748
+ @ <td>&gt;</td>
749
+ copylimline(linebuf, &c.aTo[iTo+j], collim);
750
+ @ <td class="added lineno">%d(iTo+j+1)</td>
751
+ @ <td class="added srcline">%h(linebuf)</td>
752
+ @ </tr>
753
+ }
754
+ iTo+=c.aEdit[i+2];
755
+ }
756
+
757
+ i+=3;
758
+ }
759
+
760
+ free(linebuf);
761
+ free(c.aFrom);
762
+ free(c.aTo);
763
+ free(c.aEdit);
764
+ return 1;
765
+}
766
+
583767
584768
/*
585769
** COMMAND: test-rawdiff
586770
*/
587771
void test_rawdiff_cmd(void){
@@ -711,11 +895,10 @@
711895
p->c.nEdit = 0;
712896
p->c.nEditAlloc = 0;
713897
714898
/* Clear out the from file */
715899
free(p->c.aFrom);
716
- blob_zero(pParent);
717900
718901
/* Return no errors */
719902
return 0;
720903
}
721904
722905
--- src/diff.c
+++ src/diff.c
@@ -578,10 +578,194 @@
578 free(c.aFrom);
579 free(c.aTo);
580 return c.aEdit;
581 }
582 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
583
584 /*
585 ** COMMAND: test-rawdiff
586 */
587 void test_rawdiff_cmd(void){
@@ -711,11 +895,10 @@
711 p->c.nEdit = 0;
712 p->c.nEditAlloc = 0;
713
714 /* Clear out the from file */
715 free(p->c.aFrom);
716 blob_zero(pParent);
717
718 /* Return no errors */
719 return 0;
720 }
721
722
--- src/diff.c
+++ src/diff.c
@@ -578,10 +578,194 @@
578 free(c.aFrom);
579 free(c.aTo);
580 return c.aEdit;
581 }
582 }
583
584 /*
585 ** Copy a line with a limit. Used for side-by-side diffs to enforce a maximum
586 ** line length limit.
587 */
588 static char *copylimline(char *out, DLine *dl, int lim){
589 int len;
590 len = dl->h & LENGTH_MASK;
591 if( lim && len > lim ){
592 memcpy(out, dl->z, lim-3);
593 strcpy(&out[lim-3], "...");
594 }else{
595 memcpy(out, dl->z, len);
596 out[len] = '\0';
597 }
598 return out;
599 }
600
601 /*
602 ** Output table body of a side-by-side diff. Prior to the call, the caller
603 ** should have output:
604 ** <table class="sbsdiff">
605 ** <tr><th colspan="2" class="diffhdr">Old title</th><th/>
606 ** <th colspan="2" class="diffhdr">New title</th></tr>
607 **
608 ** And after the call, it should output:
609 ** </table>
610 **
611 ** Some good reference diffs in the fossil repository for testing:
612 ** /vdiff?from=080d27a&to=4b0f813&detail=1
613 ** /vdiff?from=636804745b&to=c1d78e0556&detail=1
614 ** /vdiff?from=c0b6c28d29&to=25169506b7&detail=1
615 ** /vdiff?from=e3d022dffa&to=48bcfbd47b&detail=1
616 */
617 int html_sbsdiff(
618 Blob *pA_Blob, /* FROM file */
619 Blob *pB_Blob, /* TO file */
620 int nContext, /* Amount of context to unified diff */
621 int ignoreEolWs /* Ignore whitespace at the end of lines */
622 ){
623 DContext c;
624 int i;
625 int iFrom, iTo;
626 char *linebuf;
627 int collim=0; /* Currently not settable; allows a column limit for diffs */
628 int allowExp=0; /* Currently not settable; (dis)allow expansion of rows */
629
630 /* Prepare the input files */
631 memset(&c, 0, sizeof(c));
632 c.aFrom = break_into_lines(blob_str(pA_Blob), blob_size(pA_Blob),
633 &c.nFrom, ignoreEolWs);
634 c.aTo = break_into_lines(blob_str(pB_Blob), blob_size(pB_Blob),
635 &c.nTo, ignoreEolWs);
636 if( c.aFrom==0 || c.aTo==0 ){
637 free(c.aFrom);
638 free(c.aTo);
639 /* Note: This would be generated within a table. */
640 @ <p class="generalError" style="white-space: nowrap">cannot compute
641 @ difference between binary files</p>
642 return 0;
643 }
644
645 collim = collim < 4 ? 0 : collim;
646
647 /* Compute the difference */
648 diff_all(&c);
649
650 linebuf = fossil_malloc(LENGTH_MASK+1);
651 if( !linebuf ){
652 free(c.aFrom);
653 free(c.aTo);
654 free(c.aEdit);
655 return 0;
656 }
657
658 iFrom=iTo=0;
659 i=0;
660 while( i<c.nEdit ){
661 int j;
662 /* Copied lines */
663 for( j=0; j<c.aEdit[i]; j++){
664 /* Hide lines which are copied and are further away from block boundaries
665 ** than nConext lines. For each block with hidden lines, show a row
666 ** notifying the user about the hidden rows.
667 */
668 if( j<nContext || j>c.aEdit[i]-nContext-1 ){
669 @ <tr>
670 }else if( j==nContext && j<c.aEdit[i]-nContext-1 ){
671 @ <tr>
672 @ <td class="meta" colspan="5" style="white-space: nowrap;">
673 @ %d(c.aEdit[i]-2*nContext) hidden lines</td>
674 @ </tr>
675 if( !allowExp )
676 continue;
677 @ <tr style="display:none;">
678 }else{
679 if( !allowExp )
680 continue;
681 @ <tr style="display:none;">
682 }
683
684 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
685 @ <td class="lineno">%d(iFrom+j+1)</td>
686 @ <td class="srcline">%h(linebuf)</td>
687
688 @ <td> </td>
689
690 copylimline(linebuf, &c.aTo[iTo+j], collim);
691 @ <td class="lineno">%d(iTo+j+1)</td>
692 @ <td class="srcline">%h(linebuf)</td>
693
694 @ </tr>
695 }
696 iFrom+=c.aEdit[i];
697 iTo+=c.aEdit[i];
698
699 if( c.aEdit[i+1]!=0 && c.aEdit[i+2]!=0 ){
700 int lim;
701 lim = c.aEdit[i+1] > c.aEdit[i+2] ? c.aEdit[i+1] : c.aEdit[i+2];
702
703 /* Assume changed lines */
704 for( j=0; j<lim; j++ ){
705 @ <tr>
706
707 if( j<c.aEdit[i+1] ){
708 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
709 @ <td class="changed lineno">%d(iFrom+j+1)</td>
710 @ <td class="changed srcline">%h(linebuf)</td>
711 }else{
712 @ <td colspan="2" class="changedvoid"/>
713 }
714
715 @ <td class="changed">|</td>
716
717 if( j<c.aEdit[i+2] ){
718 copylimline(linebuf, &c.aTo[iTo+j], collim);
719 @ <td class="changed lineno">%d(iTo+j+1)</td>
720 @ <td class="changed srcline">%h(linebuf)</td>
721 }else{
722 @ <td colspan="2" class="changedvoid"/>
723 }
724
725 @ </tr>
726 }
727 iFrom+=c.aEdit[i+1];
728 iTo+=c.aEdit[i+2];
729 }else{
730
731 /* Process deleted lines */
732 for( j=0; j<c.aEdit[i+1]; j++ ){
733 @ <tr>
734
735 copylimline(linebuf, &c.aFrom[iFrom+j], collim);
736 @ <td class="removed lineno">%d(iFrom+j+1)</td>
737 @ <td class="removed srcline">%h(linebuf)</td>
738 @ <td>&lt;</td>
739 @ <td colspan="2" class="removedvoid"/>
740 @ </tr>
741 }
742 iFrom+=c.aEdit[i+1];
743
744 /* Process inserted lines */
745 for( j=0; j<c.aEdit[i+2]; j++ ){
746 @ <tr>
747 @ <td colspan="2" class="addedvoid"/>
748 @ <td>&gt;</td>
749 copylimline(linebuf, &c.aTo[iTo+j], collim);
750 @ <td class="added lineno">%d(iTo+j+1)</td>
751 @ <td class="added srcline">%h(linebuf)</td>
752 @ </tr>
753 }
754 iTo+=c.aEdit[i+2];
755 }
756
757 i+=3;
758 }
759
760 free(linebuf);
761 free(c.aFrom);
762 free(c.aTo);
763 free(c.aEdit);
764 return 1;
765 }
766
767
768 /*
769 ** COMMAND: test-rawdiff
770 */
771 void test_rawdiff_cmd(void){
@@ -711,11 +895,10 @@
895 p->c.nEdit = 0;
896 p->c.nEditAlloc = 0;
897
898 /* Clear out the from file */
899 free(p->c.aFrom);
 
900
901 /* Return no errors */
902 return 0;
903 }
904
905
+3 -3
--- src/file.c
+++ src/file.c
@@ -34,11 +34,11 @@
3434
** The file status information from the most recent stat() call.
3535
**
3636
** Use _stati64 rather than stat on windows, in order to handle files
3737
** larger than 2GB.
3838
*/
39
-#if defined(_WIN32) && defined(__MSVCRT__)
39
+#if defined(_WIN32) && (defined(__MSVCRT__) || defined(_MSC_VER))
4040
# define stat _stati64
4141
#endif
4242
/*
4343
** On Windows S_ISLNK always returns FALSE.
4444
*/
@@ -394,11 +394,11 @@
394394
** with the same name.
395395
**
396396
** Return the number of errors.
397397
*/
398398
int file_mkdir(const char *zName, int forceFlag){
399
- int rc = file_isdir(zName);
399
+ int rc = file_wd_isdir(zName);
400400
if( rc==2 ){
401401
if( !forceFlag ) return 1;
402402
file_delete(zName);
403403
}
404404
if( rc!=1 ){
@@ -899,11 +899,11 @@
899899
int file_is_the_same(Blob *pContent, const char *zName){
900900
i64 iSize;
901901
int rc;
902902
Blob onDisk;
903903
904
- iSize = file_size(zName);
904
+ iSize = file_wd_size(zName);
905905
if( iSize<0 ) return 0;
906906
if( iSize!=blob_size(pContent) ) return 0;
907907
if( file_wd_islink(zName) ){
908908
blob_read_link(&onDisk, zName);
909909
}else{
910910
--- src/file.c
+++ src/file.c
@@ -34,11 +34,11 @@
34 ** The file status information from the most recent stat() call.
35 **
36 ** Use _stati64 rather than stat on windows, in order to handle files
37 ** larger than 2GB.
38 */
39 #if defined(_WIN32) && defined(__MSVCRT__)
40 # define stat _stati64
41 #endif
42 /*
43 ** On Windows S_ISLNK always returns FALSE.
44 */
@@ -394,11 +394,11 @@
394 ** with the same name.
395 **
396 ** Return the number of errors.
397 */
398 int file_mkdir(const char *zName, int forceFlag){
399 int rc = file_isdir(zName);
400 if( rc==2 ){
401 if( !forceFlag ) return 1;
402 file_delete(zName);
403 }
404 if( rc!=1 ){
@@ -899,11 +899,11 @@
899 int file_is_the_same(Blob *pContent, const char *zName){
900 i64 iSize;
901 int rc;
902 Blob onDisk;
903
904 iSize = file_size(zName);
905 if( iSize<0 ) return 0;
906 if( iSize!=blob_size(pContent) ) return 0;
907 if( file_wd_islink(zName) ){
908 blob_read_link(&onDisk, zName);
909 }else{
910
--- src/file.c
+++ src/file.c
@@ -34,11 +34,11 @@
34 ** The file status information from the most recent stat() call.
35 **
36 ** Use _stati64 rather than stat on windows, in order to handle files
37 ** larger than 2GB.
38 */
39 #if defined(_WIN32) && (defined(__MSVCRT__) || defined(_MSC_VER))
40 # define stat _stati64
41 #endif
42 /*
43 ** On Windows S_ISLNK always returns FALSE.
44 */
@@ -394,11 +394,11 @@
394 ** with the same name.
395 **
396 ** Return the number of errors.
397 */
398 int file_mkdir(const char *zName, int forceFlag){
399 int rc = file_wd_isdir(zName);
400 if( rc==2 ){
401 if( !forceFlag ) return 1;
402 file_delete(zName);
403 }
404 if( rc!=1 ){
@@ -899,11 +899,11 @@
899 int file_is_the_same(Blob *pContent, const char *zName){
900 i64 iSize;
901 int rc;
902 Blob onDisk;
903
904 iSize = file_wd_size(zName);
905 if( iSize<0 ) return 0;
906 if( iSize!=blob_size(pContent) ) return 0;
907 if( file_wd_islink(zName) ){
908 blob_read_link(&onDisk, zName);
909 }else{
910
+5 -4
--- src/gzip.c
+++ src/gzip.c
@@ -47,20 +47,21 @@
4747
}
4848
4949
/*
5050
** Begin constructing a gzip file.
5151
*/
52
-void gzip_begin(void){
52
+void gzip_begin(sqlite3_int64 now){
5353
char aHdr[10];
54
- sqlite3_int64 now;
5554
assert( gzip.eState==0 );
5655
blob_zero(&gzip.out);
5756
aHdr[0] = 0x1f;
5857
aHdr[1] = 0x8b;
5958
aHdr[2] = 8;
6059
aHdr[3] = 0;
61
- now = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0");
60
+ if( now==0 ){
61
+ now = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0");
62
+ }
6263
put32(&aHdr[4], now&0xffffffff);
6364
aHdr[8] = 2;
6465
aHdr[9] = 255;
6566
blob_append(&gzip.out, aHdr, 10);
6667
gzip.iCRC = 0;
@@ -125,15 +126,15 @@
125126
void test_gzip_cmd(void){
126127
Blob b;
127128
char *zOut;
128129
if( g.argc!=3 ) usage("FILENAME");
129130
sqlite3_open(":memory:", &g.db);
130
- gzip_begin();
131
+ gzip_begin(0);
131132
blob_read_from_file(&b, g.argv[2]);
132133
zOut = mprintf("%s.gz", g.argv[2]);
133134
gzip_step(blob_buffer(&b), blob_size(&b));
134135
blob_reset(&b);
135136
gzip_finish(&b);
136137
blob_write_to_file(&b, zOut);
137138
blob_reset(&b);
138139
fossil_free(zOut);
139140
}
140141
--- src/gzip.c
+++ src/gzip.c
@@ -47,20 +47,21 @@
47 }
48
49 /*
50 ** Begin constructing a gzip file.
51 */
52 void gzip_begin(void){
53 char aHdr[10];
54 sqlite3_int64 now;
55 assert( gzip.eState==0 );
56 blob_zero(&gzip.out);
57 aHdr[0] = 0x1f;
58 aHdr[1] = 0x8b;
59 aHdr[2] = 8;
60 aHdr[3] = 0;
61 now = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0");
 
 
62 put32(&aHdr[4], now&0xffffffff);
63 aHdr[8] = 2;
64 aHdr[9] = 255;
65 blob_append(&gzip.out, aHdr, 10);
66 gzip.iCRC = 0;
@@ -125,15 +126,15 @@
125 void test_gzip_cmd(void){
126 Blob b;
127 char *zOut;
128 if( g.argc!=3 ) usage("FILENAME");
129 sqlite3_open(":memory:", &g.db);
130 gzip_begin();
131 blob_read_from_file(&b, g.argv[2]);
132 zOut = mprintf("%s.gz", g.argv[2]);
133 gzip_step(blob_buffer(&b), blob_size(&b));
134 blob_reset(&b);
135 gzip_finish(&b);
136 blob_write_to_file(&b, zOut);
137 blob_reset(&b);
138 fossil_free(zOut);
139 }
140
--- src/gzip.c
+++ src/gzip.c
@@ -47,20 +47,21 @@
47 }
48
49 /*
50 ** Begin constructing a gzip file.
51 */
52 void gzip_begin(sqlite3_int64 now){
53 char aHdr[10];
 
54 assert( gzip.eState==0 );
55 blob_zero(&gzip.out);
56 aHdr[0] = 0x1f;
57 aHdr[1] = 0x8b;
58 aHdr[2] = 8;
59 aHdr[3] = 0;
60 if( now==0 ){
61 now = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0");
62 }
63 put32(&aHdr[4], now&0xffffffff);
64 aHdr[8] = 2;
65 aHdr[9] = 255;
66 blob_append(&gzip.out, aHdr, 10);
67 gzip.iCRC = 0;
@@ -125,15 +126,15 @@
126 void test_gzip_cmd(void){
127 Blob b;
128 char *zOut;
129 if( g.argc!=3 ) usage("FILENAME");
130 sqlite3_open(":memory:", &g.db);
131 gzip_begin(0);
132 blob_read_from_file(&b, g.argv[2]);
133 zOut = mprintf("%s.gz", g.argv[2]);
134 gzip_step(blob_buffer(&b), blob_size(&b));
135 blob_reset(&b);
136 gzip_finish(&b);
137 blob_write_to_file(&b, zOut);
138 blob_reset(&b);
139 fossil_free(zOut);
140 }
141
+1 -1
--- src/http.c
+++ src/http.c
@@ -136,11 +136,11 @@
136136
Blob login; /* The login card */
137137
Blob payload; /* The complete payload including login card */
138138
Blob hdr; /* The HTTP request header */
139139
int closeConnection; /* True to close the connection when done */
140140
int iLength; /* Length of the reply payload */
141
- int rc; /* Result code */
141
+ int rc = 0; /* Result code */
142142
int iHttpVersion; /* Which version of HTTP protocol server uses */
143143
char *zLine; /* A single line of the reply header */
144144
int i; /* Loop counter */
145145
int isError = 0; /* True if the reply is an error message */
146146
int isCompressed = 1; /* True if the reply is compressed */
147147
--- src/http.c
+++ src/http.c
@@ -136,11 +136,11 @@
136 Blob login; /* The login card */
137 Blob payload; /* The complete payload including login card */
138 Blob hdr; /* The HTTP request header */
139 int closeConnection; /* True to close the connection when done */
140 int iLength; /* Length of the reply payload */
141 int rc; /* Result code */
142 int iHttpVersion; /* Which version of HTTP protocol server uses */
143 char *zLine; /* A single line of the reply header */
144 int i; /* Loop counter */
145 int isError = 0; /* True if the reply is an error message */
146 int isCompressed = 1; /* True if the reply is compressed */
147
--- src/http.c
+++ src/http.c
@@ -136,11 +136,11 @@
136 Blob login; /* The login card */
137 Blob payload; /* The complete payload including login card */
138 Blob hdr; /* The HTTP request header */
139 int closeConnection; /* True to close the connection when done */
140 int iLength; /* Length of the reply payload */
141 int rc = 0; /* Result code */
142 int iHttpVersion; /* Which version of HTTP protocol server uses */
143 char *zLine; /* A single line of the reply header */
144 int i; /* Loop counter */
145 int isError = 0; /* True if the reply is an error message */
146 int isCompressed = 1; /* True if the reply is compressed */
147
--- src/http_socket.c
+++ src/http_socket.c
@@ -200,16 +200,16 @@
200200
201201
/*
202202
** Receive content back from the open socket connection.
203203
*/
204204
size_t socket_receive(void *NotUsed, void *pContent, size_t N){
205
- size_t got;
205
+ ssize_t got;
206206
size_t total = 0;
207207
while( N>0 ){
208208
got = recv(iSocket, pContent, N, 0);
209209
if( got<=0 ) break;
210
- total += got;
211
- N -= got;
210
+ total += (size_t)got;
211
+ N -= (size_t)got;
212212
pContent = (void*)&((char*)pContent)[got];
213213
}
214214
return total;
215215
}
216216
--- src/http_socket.c
+++ src/http_socket.c
@@ -200,16 +200,16 @@
200
201 /*
202 ** Receive content back from the open socket connection.
203 */
204 size_t socket_receive(void *NotUsed, void *pContent, size_t N){
205 size_t got;
206 size_t total = 0;
207 while( N>0 ){
208 got = recv(iSocket, pContent, N, 0);
209 if( got<=0 ) break;
210 total += got;
211 N -= got;
212 pContent = (void*)&((char*)pContent)[got];
213 }
214 return total;
215 }
216
--- src/http_socket.c
+++ src/http_socket.c
@@ -200,16 +200,16 @@
200
201 /*
202 ** Receive content back from the open socket connection.
203 */
204 size_t socket_receive(void *NotUsed, void *pContent, size_t N){
205 ssize_t got;
206 size_t total = 0;
207 while( N>0 ){
208 got = recv(iSocket, pContent, N, 0);
209 if( got<=0 ) break;
210 total += (size_t)got;
211 N -= (size_t)got;
212 pContent = (void*)&((char*)pContent)[got];
213 }
214 return total;
215 }
216
+54 -23
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -97,10 +97,11 @@
9797
** Call this routine once before any other use of the SSL interface.
9898
** This routine does initial configuration of the SSL module.
9999
*/
100100
void ssl_global_init(void){
101101
const char *zCaSetting = 0, *zCaFile = 0, *zCaDirectory = 0;
102
+ const char *identityFile;
102103
103104
if( sslIsInit==0 ){
104105
SSL_library_init();
105106
SSL_load_error_strings();
106107
ERR_load_BIO_strings();
@@ -135,19 +136,26 @@
135136
fossil_fatal("Failed to use CA root certificates from "
136137
"ssl-ca-location '%s'", zCaSetting);
137138
}
138139
}
139140
140
- /* Load client SSL identity, preferring the filename specified on the command line */
141
- const char *identityFile = ( g.zSSLIdentity!= 0) ? g.zSSLIdentity : db_get("ssl-identity", 0);
141
+ /* Load client SSL identity, preferring the filename specified on the
142
+ ** command line */
143
+ if( g.zSSLIdentity!=0 ){
144
+ identityFile = g.zSSLIdentity;
145
+ }else{
146
+ identityFile = db_get("ssl-identity", 0);
147
+ }
142148
if( identityFile!=0 && identityFile[0]!='\0' ){
143
- if( SSL_CTX_use_certificate_file(sslCtx, identityFile, SSL_FILETYPE_PEM)!= 1
144
- || SSL_CTX_use_PrivateKey_file(sslCtx, identityFile, SSL_FILETYPE_PEM)!=1 ){
149
+ if( SSL_CTX_use_certificate_file(sslCtx,identityFile,SSL_FILETYPE_PEM)!=1
150
+ || SSL_CTX_use_PrivateKey_file(sslCtx,identityFile,SSL_FILETYPE_PEM)!=1
151
+ ){
145152
fossil_fatal("Could not load SSL identity from %s", identityFile);
146153
}
147154
}
148
- /* Register a callback to tell the user what to do when the server asks for a cert */
155
+ /* Register a callback to tell the user what to do when the server asks
156
+ ** for a cert */
149157
SSL_CTX_set_client_cert_cb(sslCtx, ssl_client_cert_callback);
150158
151159
sslIsInit = 1;
152160
}
153161
}
@@ -184,17 +192,20 @@
184192
** Return the number of errors.
185193
*/
186194
int ssl_open(void){
187195
X509 *cert;
188196
int hasSavedCertificate = 0;
189
-char *connStr ;
197
+ int trusted = 0;
198
+ char *connStr ;
199
+ unsigned long e;
200
+
190201
ssl_global_init();
191202
192203
/* Get certificate for current server from global config and
193204
* (if we have it in config) add it to certificate store.
194205
*/
195
- cert = ssl_get_certificate();
206
+ cert = ssl_get_certificate(&trusted);
196207
if ( cert!=NULL ){
197208
X509_STORE_add_cert(SSL_CTX_get_cert_store(sslCtx), cert);
198209
X509_free(cert);
199210
hasSavedCertificate = 1;
200211
}
@@ -232,11 +243,11 @@
232243
ssl_set_errmsg("No SSL certificate was presented by the peer");
233244
ssl_close();
234245
return 1;
235246
}
236247
237
- if( SSL_get_verify_result(ssl) != X509_V_OK ){
248
+ if( trusted<=0 && (e = SSL_get_verify_result(ssl)) != X509_V_OK ){
238249
char *desc, *prompt;
239250
char *warning = "";
240251
Blob ans;
241252
BIO *mem;
242253
unsigned char md[32];
@@ -258,19 +269,22 @@
258269
259270
if( hasSavedCertificate ){
260271
warning = "WARNING: Certificate doesn't match the "
261272
"saved certificate for this host!";
262273
}
263
- prompt = mprintf("\nUnknown SSL certificate:\n\n%s\n\n%s\n"
264
- "Either:\n"
265
- " * verify the certificate is correct using the "
266
- "SHA1 fingerprint above\n"
267
- " * use the global ssl-ca-location setting to specify your CA root\n"
268
- " certificates list\n\n"
269
- "If you are not expecting this message, answer no and "
270
- "contact your server\nadministrator.\n\n"
271
- "Accept certificate [a=always/y/N]? ", desc, warning);
274
+ prompt = mprintf("\nSSL verification failed: %s\n"
275
+ "Certificate received: \n\n%s\n\n%s\n"
276
+ "Either:\n"
277
+ " * verify the certificate is correct using the "
278
+ "SHA1 fingerprint above\n"
279
+ " * use the global ssl-ca-location setting to specify your CA root\n"
280
+ " certificates list\n\n"
281
+ "If you are not expecting this message, answer no and "
282
+ "contact your server\nadministrator.\n\n"
283
+ "Accept certificate for host %s [a=always/y/N]? ",
284
+ X509_verify_cert_error_string(e), desc, warning,
285
+ g.urlName);
272286
BIO_free(mem);
273287
274288
prompt_user(prompt, &ans);
275289
free(prompt);
276290
if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
@@ -278,33 +292,40 @@
278292
ssl_set_errmsg("SSL certificate declined");
279293
ssl_close();
280294
return 1;
281295
}
282296
if( blob_str(&ans)[0]=='a' ) {
283
- ssl_save_certificate(cert);
297
+ if ( trusted==0 ){
298
+ Blob ans2;
299
+ prompt_user("\nSave this certificate as fully trusted [a=always/N]? ",
300
+ &ans2);
301
+ trusted = (blob_str(&ans2)[0]=='a');
302
+ blob_reset(&ans2);
303
+ }
304
+ ssl_save_certificate(cert, trusted);
284305
}
285306
blob_reset(&ans);
286307
}
287308
288309
/* Set the Global.zIpAddr variable to the server we are talking to.
289310
** This is used to populate the ipaddr column of the rcvfrom table,
290311
** if any files are received from the server.
291312
*/
292313
{
293
- /* IPv4 only code */
294
- const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
295
- g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
314
+ /* IPv4 only code */
315
+ const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
316
+ g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
296317
}
297318
298319
X509_free(cert);
299320
return 0;
300321
}
301322
302323
/*
303324
** Save certificate to global config.
304325
*/
305
-void ssl_save_certificate(X509 *cert){
326
+void ssl_save_certificate(X509 *cert, int trusted){
306327
BIO *mem;
307328
char *zCert, *zHost;
308329
309330
mem = BIO_new(BIO_s_mem());
310331
PEM_write_bio_X509(mem, cert);
@@ -311,27 +332,37 @@
311332
BIO_write(mem, "", 1); /* nul-terminate mem buffer */
312333
BIO_get_mem_data(mem, &zCert);
313334
zHost = mprintf("cert:%s", g.urlName);
314335
db_set(zHost, zCert, 1);
315336
free(zHost);
337
+ zHost = mprintf("trusted:%s", g.urlName);
338
+ db_set_int(zHost, trusted, 1);
339
+ free(zHost);
316340
BIO_free(mem);
317341
}
318342
319343
/*
320344
** Get certificate for g.urlName from global config.
321345
** Return NULL if no certificate found.
322346
*/
323
-X509 *ssl_get_certificate(void){
347
+X509 *ssl_get_certificate(int *pTrusted){
324348
char *zHost, *zCert;
325349
BIO *mem;
326350
X509 *cert;
327351
328352
zHost = mprintf("cert:%s", g.urlName);
329353
zCert = db_get(zHost, NULL);
330354
free(zHost);
331355
if ( zCert==NULL )
332356
return NULL;
357
+
358
+ if ( pTrusted!=0 ){
359
+ zHost = mprintf("trusted:%s", g.urlName);
360
+ *pTrusted = db_get_int(zHost, 0);
361
+ free(zHost);
362
+ }
363
+
333364
mem = BIO_new(BIO_s_mem());
334365
BIO_puts(mem, zCert);
335366
cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
336367
free(zCert);
337368
BIO_free(mem);
338369
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -97,10 +97,11 @@
97 ** Call this routine once before any other use of the SSL interface.
98 ** This routine does initial configuration of the SSL module.
99 */
100 void ssl_global_init(void){
101 const char *zCaSetting = 0, *zCaFile = 0, *zCaDirectory = 0;
 
102
103 if( sslIsInit==0 ){
104 SSL_library_init();
105 SSL_load_error_strings();
106 ERR_load_BIO_strings();
@@ -135,19 +136,26 @@
135 fossil_fatal("Failed to use CA root certificates from "
136 "ssl-ca-location '%s'", zCaSetting);
137 }
138 }
139
140 /* Load client SSL identity, preferring the filename specified on the command line */
141 const char *identityFile = ( g.zSSLIdentity!= 0) ? g.zSSLIdentity : db_get("ssl-identity", 0);
 
 
 
 
 
142 if( identityFile!=0 && identityFile[0]!='\0' ){
143 if( SSL_CTX_use_certificate_file(sslCtx, identityFile, SSL_FILETYPE_PEM)!= 1
144 || SSL_CTX_use_PrivateKey_file(sslCtx, identityFile, SSL_FILETYPE_PEM)!=1 ){
 
145 fossil_fatal("Could not load SSL identity from %s", identityFile);
146 }
147 }
148 /* Register a callback to tell the user what to do when the server asks for a cert */
 
149 SSL_CTX_set_client_cert_cb(sslCtx, ssl_client_cert_callback);
150
151 sslIsInit = 1;
152 }
153 }
@@ -184,17 +192,20 @@
184 ** Return the number of errors.
185 */
186 int ssl_open(void){
187 X509 *cert;
188 int hasSavedCertificate = 0;
189 char *connStr ;
 
 
 
190 ssl_global_init();
191
192 /* Get certificate for current server from global config and
193 * (if we have it in config) add it to certificate store.
194 */
195 cert = ssl_get_certificate();
196 if ( cert!=NULL ){
197 X509_STORE_add_cert(SSL_CTX_get_cert_store(sslCtx), cert);
198 X509_free(cert);
199 hasSavedCertificate = 1;
200 }
@@ -232,11 +243,11 @@
232 ssl_set_errmsg("No SSL certificate was presented by the peer");
233 ssl_close();
234 return 1;
235 }
236
237 if( SSL_get_verify_result(ssl) != X509_V_OK ){
238 char *desc, *prompt;
239 char *warning = "";
240 Blob ans;
241 BIO *mem;
242 unsigned char md[32];
@@ -258,19 +269,22 @@
258
259 if( hasSavedCertificate ){
260 warning = "WARNING: Certificate doesn't match the "
261 "saved certificate for this host!";
262 }
263 prompt = mprintf("\nUnknown SSL certificate:\n\n%s\n\n%s\n"
264 "Either:\n"
265 " * verify the certificate is correct using the "
266 "SHA1 fingerprint above\n"
267 " * use the global ssl-ca-location setting to specify your CA root\n"
268 " certificates list\n\n"
269 "If you are not expecting this message, answer no and "
270 "contact your server\nadministrator.\n\n"
271 "Accept certificate [a=always/y/N]? ", desc, warning);
 
 
 
272 BIO_free(mem);
273
274 prompt_user(prompt, &ans);
275 free(prompt);
276 if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
@@ -278,33 +292,40 @@
278 ssl_set_errmsg("SSL certificate declined");
279 ssl_close();
280 return 1;
281 }
282 if( blob_str(&ans)[0]=='a' ) {
283 ssl_save_certificate(cert);
 
 
 
 
 
 
 
284 }
285 blob_reset(&ans);
286 }
287
288 /* Set the Global.zIpAddr variable to the server we are talking to.
289 ** This is used to populate the ipaddr column of the rcvfrom table,
290 ** if any files are received from the server.
291 */
292 {
293 /* IPv4 only code */
294 const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
295 g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
296 }
297
298 X509_free(cert);
299 return 0;
300 }
301
302 /*
303 ** Save certificate to global config.
304 */
305 void ssl_save_certificate(X509 *cert){
306 BIO *mem;
307 char *zCert, *zHost;
308
309 mem = BIO_new(BIO_s_mem());
310 PEM_write_bio_X509(mem, cert);
@@ -311,27 +332,37 @@
311 BIO_write(mem, "", 1); /* nul-terminate mem buffer */
312 BIO_get_mem_data(mem, &zCert);
313 zHost = mprintf("cert:%s", g.urlName);
314 db_set(zHost, zCert, 1);
315 free(zHost);
 
 
 
316 BIO_free(mem);
317 }
318
319 /*
320 ** Get certificate for g.urlName from global config.
321 ** Return NULL if no certificate found.
322 */
323 X509 *ssl_get_certificate(void){
324 char *zHost, *zCert;
325 BIO *mem;
326 X509 *cert;
327
328 zHost = mprintf("cert:%s", g.urlName);
329 zCert = db_get(zHost, NULL);
330 free(zHost);
331 if ( zCert==NULL )
332 return NULL;
 
 
 
 
 
 
 
333 mem = BIO_new(BIO_s_mem());
334 BIO_puts(mem, zCert);
335 cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
336 free(zCert);
337 BIO_free(mem);
338
--- src/http_ssl.c
+++ src/http_ssl.c
@@ -97,10 +97,11 @@
97 ** Call this routine once before any other use of the SSL interface.
98 ** This routine does initial configuration of the SSL module.
99 */
100 void ssl_global_init(void){
101 const char *zCaSetting = 0, *zCaFile = 0, *zCaDirectory = 0;
102 const char *identityFile;
103
104 if( sslIsInit==0 ){
105 SSL_library_init();
106 SSL_load_error_strings();
107 ERR_load_BIO_strings();
@@ -135,19 +136,26 @@
136 fossil_fatal("Failed to use CA root certificates from "
137 "ssl-ca-location '%s'", zCaSetting);
138 }
139 }
140
141 /* Load client SSL identity, preferring the filename specified on the
142 ** command line */
143 if( g.zSSLIdentity!=0 ){
144 identityFile = g.zSSLIdentity;
145 }else{
146 identityFile = db_get("ssl-identity", 0);
147 }
148 if( identityFile!=0 && identityFile[0]!='\0' ){
149 if( SSL_CTX_use_certificate_file(sslCtx,identityFile,SSL_FILETYPE_PEM)!=1
150 || SSL_CTX_use_PrivateKey_file(sslCtx,identityFile,SSL_FILETYPE_PEM)!=1
151 ){
152 fossil_fatal("Could not load SSL identity from %s", identityFile);
153 }
154 }
155 /* Register a callback to tell the user what to do when the server asks
156 ** for a cert */
157 SSL_CTX_set_client_cert_cb(sslCtx, ssl_client_cert_callback);
158
159 sslIsInit = 1;
160 }
161 }
@@ -184,17 +192,20 @@
192 ** Return the number of errors.
193 */
194 int ssl_open(void){
195 X509 *cert;
196 int hasSavedCertificate = 0;
197 int trusted = 0;
198 char *connStr ;
199 unsigned long e;
200
201 ssl_global_init();
202
203 /* Get certificate for current server from global config and
204 * (if we have it in config) add it to certificate store.
205 */
206 cert = ssl_get_certificate(&trusted);
207 if ( cert!=NULL ){
208 X509_STORE_add_cert(SSL_CTX_get_cert_store(sslCtx), cert);
209 X509_free(cert);
210 hasSavedCertificate = 1;
211 }
@@ -232,11 +243,11 @@
243 ssl_set_errmsg("No SSL certificate was presented by the peer");
244 ssl_close();
245 return 1;
246 }
247
248 if( trusted<=0 && (e = SSL_get_verify_result(ssl)) != X509_V_OK ){
249 char *desc, *prompt;
250 char *warning = "";
251 Blob ans;
252 BIO *mem;
253 unsigned char md[32];
@@ -258,19 +269,22 @@
269
270 if( hasSavedCertificate ){
271 warning = "WARNING: Certificate doesn't match the "
272 "saved certificate for this host!";
273 }
274 prompt = mprintf("\nSSL verification failed: %s\n"
275 "Certificate received: \n\n%s\n\n%s\n"
276 "Either:\n"
277 " * verify the certificate is correct using the "
278 "SHA1 fingerprint above\n"
279 " * use the global ssl-ca-location setting to specify your CA root\n"
280 " certificates list\n\n"
281 "If you are not expecting this message, answer no and "
282 "contact your server\nadministrator.\n\n"
283 "Accept certificate for host %s [a=always/y/N]? ",
284 X509_verify_cert_error_string(e), desc, warning,
285 g.urlName);
286 BIO_free(mem);
287
288 prompt_user(prompt, &ans);
289 free(prompt);
290 if( blob_str(&ans)[0]!='y' && blob_str(&ans)[0]!='a' ) {
@@ -278,33 +292,40 @@
292 ssl_set_errmsg("SSL certificate declined");
293 ssl_close();
294 return 1;
295 }
296 if( blob_str(&ans)[0]=='a' ) {
297 if ( trusted==0 ){
298 Blob ans2;
299 prompt_user("\nSave this certificate as fully trusted [a=always/N]? ",
300 &ans2);
301 trusted = (blob_str(&ans2)[0]=='a');
302 blob_reset(&ans2);
303 }
304 ssl_save_certificate(cert, trusted);
305 }
306 blob_reset(&ans);
307 }
308
309 /* Set the Global.zIpAddr variable to the server we are talking to.
310 ** This is used to populate the ipaddr column of the rcvfrom table,
311 ** if any files are received from the server.
312 */
313 {
314 /* IPv4 only code */
315 const unsigned char *ip = (const unsigned char *) BIO_get_conn_ip(iBio);
316 g.zIpAddr = mprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
317 }
318
319 X509_free(cert);
320 return 0;
321 }
322
323 /*
324 ** Save certificate to global config.
325 */
326 void ssl_save_certificate(X509 *cert, int trusted){
327 BIO *mem;
328 char *zCert, *zHost;
329
330 mem = BIO_new(BIO_s_mem());
331 PEM_write_bio_X509(mem, cert);
@@ -311,27 +332,37 @@
332 BIO_write(mem, "", 1); /* nul-terminate mem buffer */
333 BIO_get_mem_data(mem, &zCert);
334 zHost = mprintf("cert:%s", g.urlName);
335 db_set(zHost, zCert, 1);
336 free(zHost);
337 zHost = mprintf("trusted:%s", g.urlName);
338 db_set_int(zHost, trusted, 1);
339 free(zHost);
340 BIO_free(mem);
341 }
342
343 /*
344 ** Get certificate for g.urlName from global config.
345 ** Return NULL if no certificate found.
346 */
347 X509 *ssl_get_certificate(int *pTrusted){
348 char *zHost, *zCert;
349 BIO *mem;
350 X509 *cert;
351
352 zHost = mprintf("cert:%s", g.urlName);
353 zCert = db_get(zHost, NULL);
354 free(zHost);
355 if ( zCert==NULL )
356 return NULL;
357
358 if ( pTrusted!=0 ){
359 zHost = mprintf("trusted:%s", g.urlName);
360 *pTrusted = db_get_int(zHost, 0);
361 free(zHost);
362 }
363
364 mem = BIO_new(BIO_s_mem());
365 BIO_puts(mem, zCert);
366 cert = PEM_read_bio_X509(mem, NULL, 0, NULL);
367 free(zCert);
368 BIO_free(mem);
369
--- src/http_transport.c
+++ src/http_transport.c
@@ -265,14 +265,12 @@
265265
void transport_send(Blob *toSend){
266266
char *z = blob_buffer(toSend);
267267
int n = blob_size(toSend);
268268
transport.nSent += n;
269269
if( g.urlIsSsh ){
270
- int sent;
271
- sent = fwrite(z, 1, n, sshOut);
270
+ fwrite(z, 1, n, sshOut);
272271
fflush(sshOut);
273
- /* printf("sent %d of %d bytes\n", sent, n); fflush(stdout); */
274272
}else if( g.urlIsHttps ){
275273
#ifdef FOSSIL_ENABLE_SSL
276274
int sent;
277275
while( n>0 ){
278276
sent = ssl_send(0, z, n);
279277
--- src/http_transport.c
+++ src/http_transport.c
@@ -265,14 +265,12 @@
265 void transport_send(Blob *toSend){
266 char *z = blob_buffer(toSend);
267 int n = blob_size(toSend);
268 transport.nSent += n;
269 if( g.urlIsSsh ){
270 int sent;
271 sent = fwrite(z, 1, n, sshOut);
272 fflush(sshOut);
273 /* printf("sent %d of %d bytes\n", sent, n); fflush(stdout); */
274 }else if( g.urlIsHttps ){
275 #ifdef FOSSIL_ENABLE_SSL
276 int sent;
277 while( n>0 ){
278 sent = ssl_send(0, z, n);
279
--- src/http_transport.c
+++ src/http_transport.c
@@ -265,14 +265,12 @@
265 void transport_send(Blob *toSend){
266 char *z = blob_buffer(toSend);
267 int n = blob_size(toSend);
268 transport.nSent += n;
269 if( g.urlIsSsh ){
270 fwrite(z, 1, n, sshOut);
 
271 fflush(sshOut);
 
272 }else if( g.urlIsHttps ){
273 #ifdef FOSSIL_ENABLE_SSL
274 int sent;
275 while( n>0 ){
276 sent = ssl_send(0, z, n);
277
+150 -26
--- src/info.c
+++ src/info.c
@@ -276,10 +276,40 @@
276276
@ %h(blob_str(&out))
277277
blob_reset(&from);
278278
blob_reset(&to);
279279
blob_reset(&out);
280280
}
281
+
282
+
283
+/*
284
+** Write the difference between two RIDs to the output
285
+*/
286
+static void generate_sbsdiff(const char *zFrom, const char *zTo){
287
+ int fromid;
288
+ int toid;
289
+ Blob from, to;
290
+ if( zFrom ){
291
+ fromid = uuid_to_rid(zFrom, 0);
292
+ content_get(fromid, &from);
293
+ }else{
294
+ blob_zero(&from);
295
+ }
296
+ if( zTo ){
297
+ toid = uuid_to_rid(zTo, 0);
298
+ content_get(toid, &to);
299
+ }else{
300
+ blob_zero(&to);
301
+ }
302
+ @ <table class="sbsdiff">
303
+ @ <tr><th colspan="2" class="diffhdr">Old (%S(zFrom))</th><th/>
304
+ @ <th colspan="2" class="diffhdr">New (%S(zTo))</th></tr>
305
+ html_sbsdiff(&from, &to, 5, 1);
306
+ @ </table>
307
+ blob_reset(&from);
308
+ blob_reset(&to);
309
+}
310
+
281311
282312
/*
283313
** Write a line of web-page output that shows changes that have occurred
284314
** to a file between two check-ins.
285315
*/
@@ -287,10 +317,11 @@
287317
const char *zName, /* Name of the file that has changed */
288318
const char *zOld, /* blob.uuid before change. NULL for added files */
289319
const char *zNew, /* blob.uuid after change. NULL for deletes */
290320
const char *zOldName, /* Prior name. NULL if no name change. */
291321
int showDiff, /* Show edit diffs if true */
322
+ int sideBySide, /* Show diffs side-by-side */
292323
int mperm /* executable or symlink permission for zNew */
293324
){
294325
if( !g.perm.History ){
295326
if( zNew==0 ){
296327
@ <p>Deleted %h(zName)</p>
@@ -329,13 +360,17 @@
329360
}else{
330361
@ <p>Added <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
331362
@ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
332363
}
333364
if( showDiff ){
334
- @ <blockquote><pre>
335
- append_diff(zOld, zNew);
336
- @ </pre></blockquote>
365
+ if( sideBySide ){
366
+ generate_sbsdiff(zOld, zNew);
367
+ }else{
368
+ @ <blockquote><pre>
369
+ append_diff(zOld, zNew);
370
+ @ </pre></blockquote>
371
+ }
337372
}else if( zOld && zNew && fossil_strcmp(zOld,zNew)!=0 ){
338373
@ &nbsp;&nbsp;
339374
@ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&amp;v2=%S(zNew)">[diff]</a>
340375
}
341376
@ </p>
@@ -361,10 +396,11 @@
361396
void ci_page(void){
362397
Stmt q;
363398
int rid;
364399
int isLeaf;
365400
int showDiff;
401
+ int sideBySide;
366402
const char *zName; /* Name of the checkin to be displayed */
367403
const char *zUuid; /* UUID of zName */
368404
const char *zParent; /* UUID of the parent checkin (if any) */
369405
370406
login_check_credentials();
@@ -390,10 +426,11 @@
390426
" FROM blob, event"
391427
" WHERE blob.rid=%d"
392428
" AND event.objid=%d",
393429
rid, rid
394430
);
431
+ sideBySide = atoi(PD("sbs","1"));
395432
if( db_step(&q)==SQLITE_ROW ){
396433
const char *zUuid = db_column_text(&q, 0);
397434
char *zTitle = mprintf("Check-in [%.10s]", zUuid);
398435
char *zEUser, *zEComment;
399436
const char *zUser;
@@ -467,11 +504,11 @@
467504
}
468505
if( !isLeaf ){
469506
@ | <a href="%s(g.zTop)/timeline?d=%S(zUuid)">descendants</a>
470507
}
471508
if( zParent && !isLeaf ){
472
- @ | <a href="%s(g.zTop)/timeline?d=%S(zUuid)&amp;p=%S(zUuid)">both</a>
509
+ @ | <a href="%s(g.zTop)/timeline?dp=%S(zUuid)">both</a>
473510
}
474511
db_prepare(&q, "SELECT substr(tag.tagname,5) FROM tagxref, tag "
475512
" WHERE rid=%d AND tagtype>0 "
476513
" AND tag.tagid=tagxref.tagid "
477514
" AND +tag.tagname GLOB 'sym-*'", rid);
@@ -511,18 +548,42 @@
511548
showDiff = g.zPath[0]!='c';
512549
if( db_get_boolean("show-version-diffs", 0)==0 ){
513550
showDiff = !showDiff;
514551
if( showDiff ){
515552
@ <a href="%s(g.zTop)/vinfo/%T(zName)">[hide&nbsp;diffs]</a>
553
+ @ &nbsp;&nbsp;
554
+ if( sideBySide ){
555
+ @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=0">
556
+ @ [unified&nbsp;diffs]</a>
557
+ }else{
558
+ @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=1">
559
+ @ [side-by-side&nbsp;diffs]</a>
560
+ }
516561
}else{
517
- @ <a href="%s(g.zTop)/ci/%T(zName)">[show&nbsp;diffs]</a>
562
+ @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=0">
563
+ @ [show&nbsp;unified&nbsp;diffs]</a>
564
+ @ &nbsp;&nbsp;
565
+ @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=1">
566
+ @ [show&nbsp;side-by-side&nbsp;diffs]</a>
518567
}
519568
}else{
520569
if( showDiff ){
521570
@ <a href="%s(g.zTop)/ci/%T(zName)">[hide&nbsp;diffs]</a>
571
+ @ &nbsp;&nbsp;
572
+ if( sideBySide ){
573
+ @ <a href="%s(g.zTop)/info/%T(zName)?sbs=0">
574
+ @ [unified&nbsp;diffs]</a>
575
+ }else{
576
+ @ <a href="%s(g.zTop)/info/%T(zName)?sbs=1">
577
+ @ [side-by-side&nbsp;diffs]</a>
578
+ }
522579
}else{
523
- @ <a href="%s(g.zTop)/vinfo/%T(zName)">[show&nbsp;diffs]</a>
580
+ @ <a href="%s(g.zTop)/vinfo/%T(zName)?sbs=0">
581
+ @ [show&nbsp;unified&nbsp;diffs]</a>
582
+ @ &nbsp;&nbsp;
583
+ @ <a href="%s(g.zTop)/vinfo/%T(zName)?sbs=1">
584
+ @ [show&nbsp;side-by-side&nbsp;diffs]</a>
524585
}
525586
}
526587
@ &nbsp;&nbsp;
527588
@ <a href="%s(g.zTop)/vpatch?from=%S(zParent)&to=%S(zUuid)">[patch]</a><br/>
528589
db_prepare(&q,
@@ -540,11 +601,12 @@
540601
const char *zName = db_column_text(&q,0);
541602
int mperm = db_column_int(&q, 1);
542603
const char *zOld = db_column_text(&q,2);
543604
const char *zNew = db_column_text(&q,3);
544605
const char *zOldName = db_column_text(&q, 4);
545
- append_file_change_line(zName, zOld, zNew, zOldName, showDiff, mperm);
606
+ append_file_change_line(zName, zOld, zNew, zOldName, showDiff,
607
+ sideBySide, mperm);
546608
}
547609
db_finalize(&q);
548610
}
549611
style_footer();
550612
}
@@ -690,17 +752,18 @@
690752
}
691753
692754
693755
/*
694756
** WEBPAGE: vdiff
695
-** URL: /vdiff?from=UUID&amp;to=UUID&amp;detail=BOOLEAN
757
+** URL: /vdiff?from=UUID&amp;to=UUID&amp;detail=BOOLEAN;sbs=BOOLEAN
696758
**
697759
** Show all differences between two checkins.
698760
*/
699761
void vdiff_page(void){
700762
int ridFrom, ridTo;
701763
int showDetail = 0;
764
+ int sideBySide = 0;
702765
Manifest *pFrom, *pTo;
703766
ManifestFile *pFileFrom, *pFileTo;
704767
705768
login_check_credentials();
706769
if( !g.perm.Read ){ login_needed(); return; }
@@ -709,10 +772,20 @@
709772
pFrom = vdiff_parse_manifest("from", &ridFrom);
710773
if( pFrom==0 ) return;
711774
pTo = vdiff_parse_manifest("to", &ridTo);
712775
if( pTo==0 ) return;
713776
showDetail = atoi(PD("detail","0"));
777
+ sideBySide = atoi(PD("sbs","1"));
778
+ if( !sideBySide ){
779
+ style_submenu_element("Side-by-side Diff", "sbsdiff",
780
+ "%s/vdiff?from=%T&to=%T&detail=%d&sbs=1",
781
+ g.zTop, P("from"), P("to"), showDetail);
782
+ }else{
783
+ style_submenu_element("Unified Diff", "udiff",
784
+ "%s/vdiff?from=%T&to=%T&detail=%d&sbs=0",
785
+ g.zTop, P("from"), P("to"), showDetail);
786
+ }
714787
style_header("Check-in Differences");
715788
@ <h2>Difference From:</h2><blockquote>
716789
checkin_description(ridFrom);
717790
@ </blockquote><h2>To:</h2><blockquote>
718791
checkin_description(ridTo);
@@ -731,25 +804,25 @@
731804
}else{
732805
cmp = fossil_strcmp(pFileFrom->zName, pFileTo->zName);
733806
}
734807
if( cmp<0 ){
735808
append_file_change_line(pFileFrom->zName,
736
- pFileFrom->zUuid, 0, 0, 0, 0);
809
+ pFileFrom->zUuid, 0, 0, 0, 0, 0);
737810
pFileFrom = manifest_file_next(pFrom, 0);
738811
}else if( cmp>0 ){
739812
append_file_change_line(pFileTo->zName,
740
- 0, pFileTo->zUuid, 0, 0,
813
+ 0, pFileTo->zUuid, 0, 0, 0,
741814
manifest_file_mperm(pFileTo));
742815
pFileTo = manifest_file_next(pTo, 0);
743816
}else if( fossil_strcmp(pFileFrom->zUuid, pFileTo->zUuid)==0 ){
744817
/* No changes */
745818
pFileFrom = manifest_file_next(pFrom, 0);
746819
pFileTo = manifest_file_next(pTo, 0);
747820
}else{
748821
append_file_change_line(pFileFrom->zName,
749822
pFileFrom->zUuid,
750
- pFileTo->zUuid, 0, showDetail,
823
+ pFileTo->zUuid, 0, showDetail, sideBySide,
751824
manifest_file_mperm(pFileTo));
752825
pFileFrom = manifest_file_next(pFrom, 0);
753826
pFileTo = manifest_file_next(pTo, 0);
754827
}
755828
}
@@ -797,11 +870,11 @@
797870
" WHERE filename.fnid=mlink.fnid"
798871
" AND event.objid=mlink.mid"
799872
" AND a.rid=mlink.fid"
800873
" AND b.rid=mlink.mid"
801874
" AND mlink.fid=%d"
802
- " ORDER BY filename.name, event.mtime",
875
+ " ORDER BY filename.name, event.mtime /*sort*/",
803876
TAG_BRANCH, rid
804877
);
805878
@ <ul>
806879
while( db_step(&q)==SQLITE_ROW ){
807880
const char *zName = db_column_text(&q, 0);
@@ -983,28 +1056,30 @@
9831056
}
9841057
9851058
9861059
/*
9871060
** WEBPAGE: fdiff
988
-** URL: fdiff?v1=UUID&v2=UUID&patch
1061
+** URL: fdiff?v1=UUID&v2=UUID&patch&sbs=BOOLEAN
9891062
**
990
-** Two arguments, v1 and v2, identify the files to be diffed. Show the
991
-** difference between the two artifacts. Generate plaintext if "patch"
992
-** is present.
1063
+** Two arguments, v1 and v2, identify the files to be diffed. Show the
1064
+** difference between the two artifacts. Show diff side by side unless sbs
1065
+** is 0. Generate plaintext if "patch" is present.
9931066
*/
9941067
void diff_page(void){
9951068
int v1, v2;
9961069
int isPatch;
1070
+ int sideBySide;
9971071
Blob c1, c2, diff, *pOut;
9981072
char *zV1;
9991073
char *zV2;
10001074
10011075
login_check_credentials();
10021076
if( !g.perm.Read ){ login_needed(); return; }
10031077
v1 = name_to_rid_www("v1");
10041078
v2 = name_to_rid_www("v2");
10051079
if( v1==0 || v2==0 ) fossil_redirect_home();
1080
+ sideBySide = atoi(PD("sbs","1"));
10061081
zV1 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v1);
10071082
zV2 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v2);
10081083
isPatch = P("patch")!=0;
10091084
if( isPatch ){
10101085
pOut = cgi_output_blob();
@@ -1011,28 +1086,44 @@
10111086
cgi_set_content_type("text/plain");
10121087
}else{
10131088
blob_zero(&diff);
10141089
pOut = &diff;
10151090
}
1016
- content_get(v1, &c1);
1017
- content_get(v2, &c2);
1018
- text_diff(&c1, &c2, pOut, 4, 1);
1019
- blob_reset(&c1);
1020
- blob_reset(&c2);
1091
+ if( !sideBySide || isPatch ){
1092
+ content_get(v1, &c1);
1093
+ content_get(v2, &c2);
1094
+ text_diff(&c1, &c2, pOut, 4, 1);
1095
+ blob_reset(&c1);
1096
+ blob_reset(&c2);
1097
+ }
10211098
if( !isPatch ){
10221099
style_header("Diff");
10231100
style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
10241101
g.zTop, P("v1"), P("v2"));
1102
+ if( !sideBySide ){
1103
+ style_submenu_element("Side-by-side Diff", "sbsdiff",
1104
+ "%s/fdiff?v1=%T&v2=%T&sbs=1",
1105
+ g.zTop, P("v1"), P("v2"));
1106
+ }else{
1107
+ style_submenu_element("Unified Diff", "udiff",
1108
+ "%s/fdiff?v1=%T&v2=%T&sbs=0",
1109
+ g.zTop, P("v1"), P("v2"));
1110
+ }
1111
+
10251112
@ <h2>Differences From
10261113
@ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
10271114
object_description(v1, 0, 0);
10281115
@ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
10291116
object_description(v2, 0, 0);
10301117
@ <hr />
1031
- @ <blockquote><pre>
1032
- @ %h(blob_str(&diff))
1033
- @ </pre></blockquote>
1118
+ if( sideBySide ){
1119
+ generate_sbsdiff(zV1, zV2);
1120
+ }else{
1121
+ @ <blockquote><pre>
1122
+ @ %h(blob_str(&diff))
1123
+ @ </pre></blockquote>
1124
+ }
10341125
blob_reset(&diff);
10351126
style_footer();
10361127
}
10371128
}
10381129
@@ -1584,10 +1675,42 @@
15841675
@ value="%h(stdClrFound?"":zDefaultColor)" />
15851676
@ </td>
15861677
@ </tr>
15871678
@ </table>
15881679
}
1680
+
1681
+/*
1682
+** Do a comment comparison.
1683
+**
1684
+** + Leading and trailing whitespace are ignored.
1685
+** + \r\n characters compare equal to \n
1686
+**
1687
+** Return true if equal and false if not equal.
1688
+*/
1689
+static int comment_compare(const char *zA, const char *zB){
1690
+ if( zA==0 ) zA = "";
1691
+ if( zB==0 ) zB = "";
1692
+ while( fossil_isspace(zA[0]) ) zA++;
1693
+ while( fossil_isspace(zB[0]) ) zB++;
1694
+ while( zA[0] && zB[0] ){
1695
+ if( zA[0]==zB[0] ){ zA++; zB++; continue; }
1696
+ if( zA[0]=='\r' && zA[1]=='\n' && zB[0]=='\n' ){
1697
+ zA += 2;
1698
+ zB++;
1699
+ continue;
1700
+ }
1701
+ if( zB[0]=='\r' && zB[1]=='\n' && zA[0]=='\n' ){
1702
+ zB += 2;
1703
+ zA++;
1704
+ continue;
1705
+ }
1706
+ return 0;
1707
+ }
1708
+ while( fossil_isspace(zB[0]) ) zB++;
1709
+ while( fossil_isspace(zA[0]) ) zA++;
1710
+ return zA[0]==0 && zB[0]==0;
1711
+}
15891712
15901713
/*
15911714
** WEBPAGE: ci_edit
15921715
** URL: ci_edit?r=RID&c=NEWCOMMENT&u=NEWUSER
15931716
**
@@ -1661,11 +1784,12 @@
16611784
blob_zero(&ctrl);
16621785
zNow = date_in_standard_format("now");
16631786
blob_appendf(&ctrl, "D %s\n", zNow);
16641787
db_multi_exec("CREATE TEMP TABLE newtags(tag UNIQUE, prefix, value)");
16651788
if( zNewColor[0]
1666
- && (fPropagateColor!=fNewPropagateColor || fossil_strcmp(zColor,zNewColor)!=0)
1789
+ && (fPropagateColor!=fNewPropagateColor
1790
+ || fossil_strcmp(zColor,zNewColor)!=0)
16671791
){
16681792
char *zPrefix = "+";
16691793
if( fNewPropagateColor ){
16701794
zPrefix = "*";
16711795
}
@@ -1673,11 +1797,11 @@
16731797
zPrefix, zNewColor);
16741798
}
16751799
if( zNewColor[0]==0 && zColor[0]!=0 ){
16761800
db_multi_exec("REPLACE INTO newtags VALUES('bgcolor','-',NULL)");
16771801
}
1678
- if( fossil_strcmp(zComment,zNewComment)!=0 ){
1802
+ if( comment_compare(zComment,zNewComment)==0 ){
16791803
db_multi_exec("REPLACE INTO newtags VALUES('comment','+',%Q)",
16801804
zNewComment);
16811805
}
16821806
if( fossil_strcmp(zDate,zNewDate)!=0 ){
16831807
db_multi_exec("REPLACE INTO newtags VALUES('date','+',%Q)",
16841808
--- src/info.c
+++ src/info.c
@@ -276,10 +276,40 @@
276 @ %h(blob_str(&out))
277 blob_reset(&from);
278 blob_reset(&to);
279 blob_reset(&out);
280 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
282 /*
283 ** Write a line of web-page output that shows changes that have occurred
284 ** to a file between two check-ins.
285 */
@@ -287,10 +317,11 @@
287 const char *zName, /* Name of the file that has changed */
288 const char *zOld, /* blob.uuid before change. NULL for added files */
289 const char *zNew, /* blob.uuid after change. NULL for deletes */
290 const char *zOldName, /* Prior name. NULL if no name change. */
291 int showDiff, /* Show edit diffs if true */
 
292 int mperm /* executable or symlink permission for zNew */
293 ){
294 if( !g.perm.History ){
295 if( zNew==0 ){
296 @ <p>Deleted %h(zName)</p>
@@ -329,13 +360,17 @@
329 }else{
330 @ <p>Added <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
331 @ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
332 }
333 if( showDiff ){
334 @ <blockquote><pre>
335 append_diff(zOld, zNew);
336 @ </pre></blockquote>
 
 
 
 
337 }else if( zOld && zNew && fossil_strcmp(zOld,zNew)!=0 ){
338 @ &nbsp;&nbsp;
339 @ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&amp;v2=%S(zNew)">[diff]</a>
340 }
341 @ </p>
@@ -361,10 +396,11 @@
361 void ci_page(void){
362 Stmt q;
363 int rid;
364 int isLeaf;
365 int showDiff;
 
366 const char *zName; /* Name of the checkin to be displayed */
367 const char *zUuid; /* UUID of zName */
368 const char *zParent; /* UUID of the parent checkin (if any) */
369
370 login_check_credentials();
@@ -390,10 +426,11 @@
390 " FROM blob, event"
391 " WHERE blob.rid=%d"
392 " AND event.objid=%d",
393 rid, rid
394 );
 
395 if( db_step(&q)==SQLITE_ROW ){
396 const char *zUuid = db_column_text(&q, 0);
397 char *zTitle = mprintf("Check-in [%.10s]", zUuid);
398 char *zEUser, *zEComment;
399 const char *zUser;
@@ -467,11 +504,11 @@
467 }
468 if( !isLeaf ){
469 @ | <a href="%s(g.zTop)/timeline?d=%S(zUuid)">descendants</a>
470 }
471 if( zParent && !isLeaf ){
472 @ | <a href="%s(g.zTop)/timeline?d=%S(zUuid)&amp;p=%S(zUuid)">both</a>
473 }
474 db_prepare(&q, "SELECT substr(tag.tagname,5) FROM tagxref, tag "
475 " WHERE rid=%d AND tagtype>0 "
476 " AND tag.tagid=tagxref.tagid "
477 " AND +tag.tagname GLOB 'sym-*'", rid);
@@ -511,18 +548,42 @@
511 showDiff = g.zPath[0]!='c';
512 if( db_get_boolean("show-version-diffs", 0)==0 ){
513 showDiff = !showDiff;
514 if( showDiff ){
515 @ <a href="%s(g.zTop)/vinfo/%T(zName)">[hide&nbsp;diffs]</a>
 
 
 
 
 
 
 
 
516 }else{
517 @ <a href="%s(g.zTop)/ci/%T(zName)">[show&nbsp;diffs]</a>
 
 
 
 
518 }
519 }else{
520 if( showDiff ){
521 @ <a href="%s(g.zTop)/ci/%T(zName)">[hide&nbsp;diffs]</a>
 
 
 
 
 
 
 
 
522 }else{
523 @ <a href="%s(g.zTop)/vinfo/%T(zName)">[show&nbsp;diffs]</a>
 
 
 
 
524 }
525 }
526 @ &nbsp;&nbsp;
527 @ <a href="%s(g.zTop)/vpatch?from=%S(zParent)&to=%S(zUuid)">[patch]</a><br/>
528 db_prepare(&q,
@@ -540,11 +601,12 @@
540 const char *zName = db_column_text(&q,0);
541 int mperm = db_column_int(&q, 1);
542 const char *zOld = db_column_text(&q,2);
543 const char *zNew = db_column_text(&q,3);
544 const char *zOldName = db_column_text(&q, 4);
545 append_file_change_line(zName, zOld, zNew, zOldName, showDiff, mperm);
 
546 }
547 db_finalize(&q);
548 }
549 style_footer();
550 }
@@ -690,17 +752,18 @@
690 }
691
692
693 /*
694 ** WEBPAGE: vdiff
695 ** URL: /vdiff?from=UUID&amp;to=UUID&amp;detail=BOOLEAN
696 **
697 ** Show all differences between two checkins.
698 */
699 void vdiff_page(void){
700 int ridFrom, ridTo;
701 int showDetail = 0;
 
702 Manifest *pFrom, *pTo;
703 ManifestFile *pFileFrom, *pFileTo;
704
705 login_check_credentials();
706 if( !g.perm.Read ){ login_needed(); return; }
@@ -709,10 +772,20 @@
709 pFrom = vdiff_parse_manifest("from", &ridFrom);
710 if( pFrom==0 ) return;
711 pTo = vdiff_parse_manifest("to", &ridTo);
712 if( pTo==0 ) return;
713 showDetail = atoi(PD("detail","0"));
 
 
 
 
 
 
 
 
 
 
714 style_header("Check-in Differences");
715 @ <h2>Difference From:</h2><blockquote>
716 checkin_description(ridFrom);
717 @ </blockquote><h2>To:</h2><blockquote>
718 checkin_description(ridTo);
@@ -731,25 +804,25 @@
731 }else{
732 cmp = fossil_strcmp(pFileFrom->zName, pFileTo->zName);
733 }
734 if( cmp<0 ){
735 append_file_change_line(pFileFrom->zName,
736 pFileFrom->zUuid, 0, 0, 0, 0);
737 pFileFrom = manifest_file_next(pFrom, 0);
738 }else if( cmp>0 ){
739 append_file_change_line(pFileTo->zName,
740 0, pFileTo->zUuid, 0, 0,
741 manifest_file_mperm(pFileTo));
742 pFileTo = manifest_file_next(pTo, 0);
743 }else if( fossil_strcmp(pFileFrom->zUuid, pFileTo->zUuid)==0 ){
744 /* No changes */
745 pFileFrom = manifest_file_next(pFrom, 0);
746 pFileTo = manifest_file_next(pTo, 0);
747 }else{
748 append_file_change_line(pFileFrom->zName,
749 pFileFrom->zUuid,
750 pFileTo->zUuid, 0, showDetail,
751 manifest_file_mperm(pFileTo));
752 pFileFrom = manifest_file_next(pFrom, 0);
753 pFileTo = manifest_file_next(pTo, 0);
754 }
755 }
@@ -797,11 +870,11 @@
797 " WHERE filename.fnid=mlink.fnid"
798 " AND event.objid=mlink.mid"
799 " AND a.rid=mlink.fid"
800 " AND b.rid=mlink.mid"
801 " AND mlink.fid=%d"
802 " ORDER BY filename.name, event.mtime",
803 TAG_BRANCH, rid
804 );
805 @ <ul>
806 while( db_step(&q)==SQLITE_ROW ){
807 const char *zName = db_column_text(&q, 0);
@@ -983,28 +1056,30 @@
983 }
984
985
986 /*
987 ** WEBPAGE: fdiff
988 ** URL: fdiff?v1=UUID&v2=UUID&patch
989 **
990 ** Two arguments, v1 and v2, identify the files to be diffed. Show the
991 ** difference between the two artifacts. Generate plaintext if "patch"
992 ** is present.
993 */
994 void diff_page(void){
995 int v1, v2;
996 int isPatch;
 
997 Blob c1, c2, diff, *pOut;
998 char *zV1;
999 char *zV2;
1000
1001 login_check_credentials();
1002 if( !g.perm.Read ){ login_needed(); return; }
1003 v1 = name_to_rid_www("v1");
1004 v2 = name_to_rid_www("v2");
1005 if( v1==0 || v2==0 ) fossil_redirect_home();
 
1006 zV1 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v1);
1007 zV2 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v2);
1008 isPatch = P("patch")!=0;
1009 if( isPatch ){
1010 pOut = cgi_output_blob();
@@ -1011,28 +1086,44 @@
1011 cgi_set_content_type("text/plain");
1012 }else{
1013 blob_zero(&diff);
1014 pOut = &diff;
1015 }
1016 content_get(v1, &c1);
1017 content_get(v2, &c2);
1018 text_diff(&c1, &c2, pOut, 4, 1);
1019 blob_reset(&c1);
1020 blob_reset(&c2);
 
 
1021 if( !isPatch ){
1022 style_header("Diff");
1023 style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
1024 g.zTop, P("v1"), P("v2"));
 
 
 
 
 
 
 
 
 
 
1025 @ <h2>Differences From
1026 @ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
1027 object_description(v1, 0, 0);
1028 @ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
1029 object_description(v2, 0, 0);
1030 @ <hr />
1031 @ <blockquote><pre>
1032 @ %h(blob_str(&diff))
1033 @ </pre></blockquote>
 
 
 
 
1034 blob_reset(&diff);
1035 style_footer();
1036 }
1037 }
1038
@@ -1584,10 +1675,42 @@
1584 @ value="%h(stdClrFound?"":zDefaultColor)" />
1585 @ </td>
1586 @ </tr>
1587 @ </table>
1588 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1589
1590 /*
1591 ** WEBPAGE: ci_edit
1592 ** URL: ci_edit?r=RID&c=NEWCOMMENT&u=NEWUSER
1593 **
@@ -1661,11 +1784,12 @@
1661 blob_zero(&ctrl);
1662 zNow = date_in_standard_format("now");
1663 blob_appendf(&ctrl, "D %s\n", zNow);
1664 db_multi_exec("CREATE TEMP TABLE newtags(tag UNIQUE, prefix, value)");
1665 if( zNewColor[0]
1666 && (fPropagateColor!=fNewPropagateColor || fossil_strcmp(zColor,zNewColor)!=0)
 
1667 ){
1668 char *zPrefix = "+";
1669 if( fNewPropagateColor ){
1670 zPrefix = "*";
1671 }
@@ -1673,11 +1797,11 @@
1673 zPrefix, zNewColor);
1674 }
1675 if( zNewColor[0]==0 && zColor[0]!=0 ){
1676 db_multi_exec("REPLACE INTO newtags VALUES('bgcolor','-',NULL)");
1677 }
1678 if( fossil_strcmp(zComment,zNewComment)!=0 ){
1679 db_multi_exec("REPLACE INTO newtags VALUES('comment','+',%Q)",
1680 zNewComment);
1681 }
1682 if( fossil_strcmp(zDate,zNewDate)!=0 ){
1683 db_multi_exec("REPLACE INTO newtags VALUES('date','+',%Q)",
1684
--- src/info.c
+++ src/info.c
@@ -276,10 +276,40 @@
276 @ %h(blob_str(&out))
277 blob_reset(&from);
278 blob_reset(&to);
279 blob_reset(&out);
280 }
281
282
283 /*
284 ** Write the difference between two RIDs to the output
285 */
286 static void generate_sbsdiff(const char *zFrom, const char *zTo){
287 int fromid;
288 int toid;
289 Blob from, to;
290 if( zFrom ){
291 fromid = uuid_to_rid(zFrom, 0);
292 content_get(fromid, &from);
293 }else{
294 blob_zero(&from);
295 }
296 if( zTo ){
297 toid = uuid_to_rid(zTo, 0);
298 content_get(toid, &to);
299 }else{
300 blob_zero(&to);
301 }
302 @ <table class="sbsdiff">
303 @ <tr><th colspan="2" class="diffhdr">Old (%S(zFrom))</th><th/>
304 @ <th colspan="2" class="diffhdr">New (%S(zTo))</th></tr>
305 html_sbsdiff(&from, &to, 5, 1);
306 @ </table>
307 blob_reset(&from);
308 blob_reset(&to);
309 }
310
311
312 /*
313 ** Write a line of web-page output that shows changes that have occurred
314 ** to a file between two check-ins.
315 */
@@ -287,10 +317,11 @@
317 const char *zName, /* Name of the file that has changed */
318 const char *zOld, /* blob.uuid before change. NULL for added files */
319 const char *zNew, /* blob.uuid after change. NULL for deletes */
320 const char *zOldName, /* Prior name. NULL if no name change. */
321 int showDiff, /* Show edit diffs if true */
322 int sideBySide, /* Show diffs side-by-side */
323 int mperm /* executable or symlink permission for zNew */
324 ){
325 if( !g.perm.History ){
326 if( zNew==0 ){
327 @ <p>Deleted %h(zName)</p>
@@ -329,13 +360,17 @@
360 }else{
361 @ <p>Added <a href="%s(g.zTop)/finfo?name=%T(zName)">%h(zName)</a>
362 @ version <a href="%s(g.zTop)/artifact/%s(zNew)">[%S(zNew)]</a>
363 }
364 if( showDiff ){
365 if( sideBySide ){
366 generate_sbsdiff(zOld, zNew);
367 }else{
368 @ <blockquote><pre>
369 append_diff(zOld, zNew);
370 @ </pre></blockquote>
371 }
372 }else if( zOld && zNew && fossil_strcmp(zOld,zNew)!=0 ){
373 @ &nbsp;&nbsp;
374 @ <a href="%s(g.zTop)/fdiff?v1=%S(zOld)&amp;v2=%S(zNew)">[diff]</a>
375 }
376 @ </p>
@@ -361,10 +396,11 @@
396 void ci_page(void){
397 Stmt q;
398 int rid;
399 int isLeaf;
400 int showDiff;
401 int sideBySide;
402 const char *zName; /* Name of the checkin to be displayed */
403 const char *zUuid; /* UUID of zName */
404 const char *zParent; /* UUID of the parent checkin (if any) */
405
406 login_check_credentials();
@@ -390,10 +426,11 @@
426 " FROM blob, event"
427 " WHERE blob.rid=%d"
428 " AND event.objid=%d",
429 rid, rid
430 );
431 sideBySide = atoi(PD("sbs","1"));
432 if( db_step(&q)==SQLITE_ROW ){
433 const char *zUuid = db_column_text(&q, 0);
434 char *zTitle = mprintf("Check-in [%.10s]", zUuid);
435 char *zEUser, *zEComment;
436 const char *zUser;
@@ -467,11 +504,11 @@
504 }
505 if( !isLeaf ){
506 @ | <a href="%s(g.zTop)/timeline?d=%S(zUuid)">descendants</a>
507 }
508 if( zParent && !isLeaf ){
509 @ | <a href="%s(g.zTop)/timeline?dp=%S(zUuid)">both</a>
510 }
511 db_prepare(&q, "SELECT substr(tag.tagname,5) FROM tagxref, tag "
512 " WHERE rid=%d AND tagtype>0 "
513 " AND tag.tagid=tagxref.tagid "
514 " AND +tag.tagname GLOB 'sym-*'", rid);
@@ -511,18 +548,42 @@
548 showDiff = g.zPath[0]!='c';
549 if( db_get_boolean("show-version-diffs", 0)==0 ){
550 showDiff = !showDiff;
551 if( showDiff ){
552 @ <a href="%s(g.zTop)/vinfo/%T(zName)">[hide&nbsp;diffs]</a>
553 @ &nbsp;&nbsp;
554 if( sideBySide ){
555 @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=0">
556 @ [unified&nbsp;diffs]</a>
557 }else{
558 @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=1">
559 @ [side-by-side&nbsp;diffs]</a>
560 }
561 }else{
562 @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=0">
563 @ [show&nbsp;unified&nbsp;diffs]</a>
564 @ &nbsp;&nbsp;
565 @ <a href="%s(g.zTop)/ci/%T(zName)?sbs=1">
566 @ [show&nbsp;side-by-side&nbsp;diffs]</a>
567 }
568 }else{
569 if( showDiff ){
570 @ <a href="%s(g.zTop)/ci/%T(zName)">[hide&nbsp;diffs]</a>
571 @ &nbsp;&nbsp;
572 if( sideBySide ){
573 @ <a href="%s(g.zTop)/info/%T(zName)?sbs=0">
574 @ [unified&nbsp;diffs]</a>
575 }else{
576 @ <a href="%s(g.zTop)/info/%T(zName)?sbs=1">
577 @ [side-by-side&nbsp;diffs]</a>
578 }
579 }else{
580 @ <a href="%s(g.zTop)/vinfo/%T(zName)?sbs=0">
581 @ [show&nbsp;unified&nbsp;diffs]</a>
582 @ &nbsp;&nbsp;
583 @ <a href="%s(g.zTop)/vinfo/%T(zName)?sbs=1">
584 @ [show&nbsp;side-by-side&nbsp;diffs]</a>
585 }
586 }
587 @ &nbsp;&nbsp;
588 @ <a href="%s(g.zTop)/vpatch?from=%S(zParent)&to=%S(zUuid)">[patch]</a><br/>
589 db_prepare(&q,
@@ -540,11 +601,12 @@
601 const char *zName = db_column_text(&q,0);
602 int mperm = db_column_int(&q, 1);
603 const char *zOld = db_column_text(&q,2);
604 const char *zNew = db_column_text(&q,3);
605 const char *zOldName = db_column_text(&q, 4);
606 append_file_change_line(zName, zOld, zNew, zOldName, showDiff,
607 sideBySide, mperm);
608 }
609 db_finalize(&q);
610 }
611 style_footer();
612 }
@@ -690,17 +752,18 @@
752 }
753
754
755 /*
756 ** WEBPAGE: vdiff
757 ** URL: /vdiff?from=UUID&amp;to=UUID&amp;detail=BOOLEAN;sbs=BOOLEAN
758 **
759 ** Show all differences between two checkins.
760 */
761 void vdiff_page(void){
762 int ridFrom, ridTo;
763 int showDetail = 0;
764 int sideBySide = 0;
765 Manifest *pFrom, *pTo;
766 ManifestFile *pFileFrom, *pFileTo;
767
768 login_check_credentials();
769 if( !g.perm.Read ){ login_needed(); return; }
@@ -709,10 +772,20 @@
772 pFrom = vdiff_parse_manifest("from", &ridFrom);
773 if( pFrom==0 ) return;
774 pTo = vdiff_parse_manifest("to", &ridTo);
775 if( pTo==0 ) return;
776 showDetail = atoi(PD("detail","0"));
777 sideBySide = atoi(PD("sbs","1"));
778 if( !sideBySide ){
779 style_submenu_element("Side-by-side Diff", "sbsdiff",
780 "%s/vdiff?from=%T&to=%T&detail=%d&sbs=1",
781 g.zTop, P("from"), P("to"), showDetail);
782 }else{
783 style_submenu_element("Unified Diff", "udiff",
784 "%s/vdiff?from=%T&to=%T&detail=%d&sbs=0",
785 g.zTop, P("from"), P("to"), showDetail);
786 }
787 style_header("Check-in Differences");
788 @ <h2>Difference From:</h2><blockquote>
789 checkin_description(ridFrom);
790 @ </blockquote><h2>To:</h2><blockquote>
791 checkin_description(ridTo);
@@ -731,25 +804,25 @@
804 }else{
805 cmp = fossil_strcmp(pFileFrom->zName, pFileTo->zName);
806 }
807 if( cmp<0 ){
808 append_file_change_line(pFileFrom->zName,
809 pFileFrom->zUuid, 0, 0, 0, 0, 0);
810 pFileFrom = manifest_file_next(pFrom, 0);
811 }else if( cmp>0 ){
812 append_file_change_line(pFileTo->zName,
813 0, pFileTo->zUuid, 0, 0, 0,
814 manifest_file_mperm(pFileTo));
815 pFileTo = manifest_file_next(pTo, 0);
816 }else if( fossil_strcmp(pFileFrom->zUuid, pFileTo->zUuid)==0 ){
817 /* No changes */
818 pFileFrom = manifest_file_next(pFrom, 0);
819 pFileTo = manifest_file_next(pTo, 0);
820 }else{
821 append_file_change_line(pFileFrom->zName,
822 pFileFrom->zUuid,
823 pFileTo->zUuid, 0, showDetail, sideBySide,
824 manifest_file_mperm(pFileTo));
825 pFileFrom = manifest_file_next(pFrom, 0);
826 pFileTo = manifest_file_next(pTo, 0);
827 }
828 }
@@ -797,11 +870,11 @@
870 " WHERE filename.fnid=mlink.fnid"
871 " AND event.objid=mlink.mid"
872 " AND a.rid=mlink.fid"
873 " AND b.rid=mlink.mid"
874 " AND mlink.fid=%d"
875 " ORDER BY filename.name, event.mtime /*sort*/",
876 TAG_BRANCH, rid
877 );
878 @ <ul>
879 while( db_step(&q)==SQLITE_ROW ){
880 const char *zName = db_column_text(&q, 0);
@@ -983,28 +1056,30 @@
1056 }
1057
1058
1059 /*
1060 ** WEBPAGE: fdiff
1061 ** URL: fdiff?v1=UUID&v2=UUID&patch&sbs=BOOLEAN
1062 **
1063 ** Two arguments, v1 and v2, identify the files to be diffed. Show the
1064 ** difference between the two artifacts. Show diff side by side unless sbs
1065 ** is 0. Generate plaintext if "patch" is present.
1066 */
1067 void diff_page(void){
1068 int v1, v2;
1069 int isPatch;
1070 int sideBySide;
1071 Blob c1, c2, diff, *pOut;
1072 char *zV1;
1073 char *zV2;
1074
1075 login_check_credentials();
1076 if( !g.perm.Read ){ login_needed(); return; }
1077 v1 = name_to_rid_www("v1");
1078 v2 = name_to_rid_www("v2");
1079 if( v1==0 || v2==0 ) fossil_redirect_home();
1080 sideBySide = atoi(PD("sbs","1"));
1081 zV1 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v1);
1082 zV2 = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", v2);
1083 isPatch = P("patch")!=0;
1084 if( isPatch ){
1085 pOut = cgi_output_blob();
@@ -1011,28 +1086,44 @@
1086 cgi_set_content_type("text/plain");
1087 }else{
1088 blob_zero(&diff);
1089 pOut = &diff;
1090 }
1091 if( !sideBySide || isPatch ){
1092 content_get(v1, &c1);
1093 content_get(v2, &c2);
1094 text_diff(&c1, &c2, pOut, 4, 1);
1095 blob_reset(&c1);
1096 blob_reset(&c2);
1097 }
1098 if( !isPatch ){
1099 style_header("Diff");
1100 style_submenu_element("Patch", "Patch", "%s/fdiff?v1=%T&v2=%T&patch",
1101 g.zTop, P("v1"), P("v2"));
1102 if( !sideBySide ){
1103 style_submenu_element("Side-by-side Diff", "sbsdiff",
1104 "%s/fdiff?v1=%T&v2=%T&sbs=1",
1105 g.zTop, P("v1"), P("v2"));
1106 }else{
1107 style_submenu_element("Unified Diff", "udiff",
1108 "%s/fdiff?v1=%T&v2=%T&sbs=0",
1109 g.zTop, P("v1"), P("v2"));
1110 }
1111
1112 @ <h2>Differences From
1113 @ Artifact <a href="%s(g.zTop)/artifact/%S(zV1)">[%S(zV1)]</a>:</h2>
1114 object_description(v1, 0, 0);
1115 @ <h2>To Artifact <a href="%s(g.zTop)/artifact/%S(zV2)">[%S(zV2)]</a>:</h2>
1116 object_description(v2, 0, 0);
1117 @ <hr />
1118 if( sideBySide ){
1119 generate_sbsdiff(zV1, zV2);
1120 }else{
1121 @ <blockquote><pre>
1122 @ %h(blob_str(&diff))
1123 @ </pre></blockquote>
1124 }
1125 blob_reset(&diff);
1126 style_footer();
1127 }
1128 }
1129
@@ -1584,10 +1675,42 @@
1675 @ value="%h(stdClrFound?"":zDefaultColor)" />
1676 @ </td>
1677 @ </tr>
1678 @ </table>
1679 }
1680
1681 /*
1682 ** Do a comment comparison.
1683 **
1684 ** + Leading and trailing whitespace are ignored.
1685 ** + \r\n characters compare equal to \n
1686 **
1687 ** Return true if equal and false if not equal.
1688 */
1689 static int comment_compare(const char *zA, const char *zB){
1690 if( zA==0 ) zA = "";
1691 if( zB==0 ) zB = "";
1692 while( fossil_isspace(zA[0]) ) zA++;
1693 while( fossil_isspace(zB[0]) ) zB++;
1694 while( zA[0] && zB[0] ){
1695 if( zA[0]==zB[0] ){ zA++; zB++; continue; }
1696 if( zA[0]=='\r' && zA[1]=='\n' && zB[0]=='\n' ){
1697 zA += 2;
1698 zB++;
1699 continue;
1700 }
1701 if( zB[0]=='\r' && zB[1]=='\n' && zA[0]=='\n' ){
1702 zB += 2;
1703 zA++;
1704 continue;
1705 }
1706 return 0;
1707 }
1708 while( fossil_isspace(zB[0]) ) zB++;
1709 while( fossil_isspace(zA[0]) ) zA++;
1710 return zA[0]==0 && zB[0]==0;
1711 }
1712
1713 /*
1714 ** WEBPAGE: ci_edit
1715 ** URL: ci_edit?r=RID&c=NEWCOMMENT&u=NEWUSER
1716 **
@@ -1661,11 +1784,12 @@
1784 blob_zero(&ctrl);
1785 zNow = date_in_standard_format("now");
1786 blob_appendf(&ctrl, "D %s\n", zNow);
1787 db_multi_exec("CREATE TEMP TABLE newtags(tag UNIQUE, prefix, value)");
1788 if( zNewColor[0]
1789 && (fPropagateColor!=fNewPropagateColor
1790 || fossil_strcmp(zColor,zNewColor)!=0)
1791 ){
1792 char *zPrefix = "+";
1793 if( fNewPropagateColor ){
1794 zPrefix = "*";
1795 }
@@ -1673,11 +1797,11 @@
1797 zPrefix, zNewColor);
1798 }
1799 if( zNewColor[0]==0 && zColor[0]!=0 ){
1800 db_multi_exec("REPLACE INTO newtags VALUES('bgcolor','-',NULL)");
1801 }
1802 if( comment_compare(zComment,zNewComment)==0 ){
1803 db_multi_exec("REPLACE INTO newtags VALUES('comment','+',%Q)",
1804 zNewComment);
1805 }
1806 if( fossil_strcmp(zDate,zNewDate)!=0 ){
1807 db_multi_exec("REPLACE INTO newtags VALUES('date','+',%Q)",
1808
+46 -12
--- src/login.c
+++ src/login.c
@@ -91,11 +91,11 @@
9191
if( zCookieName==0 ){
9292
zCookieName = db_text(0,
9393
"SELECT 'fossil-' || substr(value,1,16)"
9494
" FROM config"
9595
" WHERE name IN ('project-code','login-group-code')"
96
- " ORDER BY name;"
96
+ " ORDER BY name /*sort*/"
9797
);
9898
}
9999
return zCookieName;
100100
}
101101
@@ -230,12 +230,14 @@
230230
}
231231
if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232232
/* The user requests a password change */
233233
zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234234
if( db_int(1, "SELECT 0 FROM user"
235
- " WHERE uid=%d AND (pw=%Q OR pw=%Q)",
236
- g.userUid, zPasswd, zSha1Pw) ){
235
+ " WHERE uid=%d"
236
+ " AND (constant_time_cmp(pw,%Q)=0"
237
+ " OR constant_time_cmp(pw,%Q)=0)",
238
+ g.userUid, zSha1Pw, zPasswd) ){
237239
sleep(1);
238240
zErrMsg =
239241
@ <p><span class="loginError">
240242
@ You entered an incorrect old password while attempting to change
241243
@ your password. Your password is unchanged.
@@ -308,12 +310,12 @@
308310
uid = db_int(0,
309311
"SELECT uid FROM user"
310312
" WHERE login=%Q"
311313
" AND length(cap)>0 AND length(pw)>0"
312314
" AND login NOT IN ('anonymous','nobody','developer','reader')"
313
- " AND (pw=%Q OR pw=%Q)",
314
- zUsername, zPasswd, zSha1Pw
315
+ " AND (constant_time_cmp(pw,%Q)=0 OR constant_time_cmp(pw,%Q)=0)",
316
+ zUsername, zSha1Pw, zPasswd
315317
);
316318
if( uid<=0 ){
317319
sleep(1);
318320
zErrMsg =
319321
@ <p><span class="loginError">
@@ -451,10 +453,37 @@
451453
@ </table>
452454
@ </form>
453455
}
454456
style_footer();
455457
}
458
+
459
+/*
460
+** SQL function for constant time comparison of two values.
461
+** Sets result to 0 if two values are equal.
462
+*/
463
+static void constant_time_cmp_function(
464
+ sqlite3_context *context,
465
+ int argc,
466
+ sqlite3_value **argv
467
+){
468
+ const unsigned char *buf1, *buf2;
469
+ int len, i;
470
+ unsigned char rc = 0;
471
+
472
+ assert( argc==2 );
473
+ len = sqlite3_value_bytes(argv[0]);
474
+ if( len==0 || len!=sqlite3_value_bytes(argv[1]) ){
475
+ rc = 1;
476
+ }else{
477
+ buf1 = sqlite3_value_text(argv[0]);
478
+ buf2 = sqlite3_value_text(argv[1]);
479
+ for( i=0; i<len; i++ ){
480
+ rc = rc | (buf1[i] ^ buf2[i]);
481
+ }
482
+ }
483
+ sqlite3_result_int(context, rc);
484
+}
456485
457486
/*
458487
** Attempt to find login credentials for user zLogin on a peer repository
459488
** with project code zCode. Transfer those credentials to the local
460489
** repository.
@@ -481,20 +510,22 @@
481510
if( zOtherRepo==0 ) return 0; /* No such peer repository */
482511
483512
rc = sqlite3_open(zOtherRepo, &pOther);
484513
if( rc==SQLITE_OK ){
485514
sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
515
+ sqlite3_create_function(pOther, "constant_time_cmp", 2, SQLITE_UTF8, 0,
516
+ constant_time_cmp_function, 0, 0);
486517
sqlite3_busy_timeout(pOther, 5000);
487518
zSQL = mprintf(
488519
"SELECT cexpire FROM user"
489
- " WHERE cookie=%Q"
520
+ " WHERE login=%Q"
490521
" AND ipaddr=%Q"
491
- " AND login=%Q"
492522
" AND length(cap)>0"
493523
" AND length(pw)>0"
494
- " AND cexpire>julianday('now')",
495
- zHash, zRemoteAddr, zLogin
524
+ " AND cexpire>julianday('now')"
525
+ " AND constant_time_cmp(cookie,%Q)=0",
526
+ zLogin, zRemoteAddr, zHash
496527
);
497528
pStmt = 0;
498529
rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
499530
if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
500531
db_multi_exec(
@@ -527,16 +558,16 @@
527558
if( fossil_strcmp(zLogin, "developer")==0 ) return 0;
528559
if( fossil_strcmp(zLogin, "reader")==0 ) return 0;
529560
uid = db_int(0,
530561
"SELECT uid FROM user"
531562
" WHERE login=%Q"
532
- " AND cookie=%Q"
533563
" AND ipaddr=%Q"
534564
" AND cexpire>julianday('now')"
535565
" AND length(cap)>0"
536
- " AND length(pw)>0",
537
- zLogin, zCookie, zRemoteAddr
566
+ " AND length(pw)>0"
567
+ " AND constant_time_cmp(cookie,%Q)=0",
568
+ zLogin, zRemoteAddr, zCookie
538569
);
539570
return uid;
540571
}
541572
542573
/*
@@ -554,10 +585,13 @@
554585
char *zRemoteAddr; /* Abbreviated IP address of the requestor */
555586
const char *zCap = 0; /* Capability string */
556587
557588
/* Only run this check once. */
558589
if( g.userUid!=0 ) return;
590
+
591
+ sqlite3_create_function(g.db, "constant_time_cmp", 2, SQLITE_UTF8, 0,
592
+ constant_time_cmp_function, 0, 0);
559593
560594
/* If the HTTP connection is coming over 127.0.0.1 and if
561595
** local login is disabled and if we are using HTTP and not HTTPS,
562596
** then there is no need to check user credentials.
563597
**
564598
--- src/login.c
+++ src/login.c
@@ -91,11 +91,11 @@
91 if( zCookieName==0 ){
92 zCookieName = db_text(0,
93 "SELECT 'fossil-' || substr(value,1,16)"
94 " FROM config"
95 " WHERE name IN ('project-code','login-group-code')"
96 " ORDER BY name;"
97 );
98 }
99 return zCookieName;
100 }
101
@@ -230,12 +230,14 @@
230 }
231 if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232 /* The user requests a password change */
233 zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234 if( db_int(1, "SELECT 0 FROM user"
235 " WHERE uid=%d AND (pw=%Q OR pw=%Q)",
236 g.userUid, zPasswd, zSha1Pw) ){
 
 
237 sleep(1);
238 zErrMsg =
239 @ <p><span class="loginError">
240 @ You entered an incorrect old password while attempting to change
241 @ your password. Your password is unchanged.
@@ -308,12 +310,12 @@
308 uid = db_int(0,
309 "SELECT uid FROM user"
310 " WHERE login=%Q"
311 " AND length(cap)>0 AND length(pw)>0"
312 " AND login NOT IN ('anonymous','nobody','developer','reader')"
313 " AND (pw=%Q OR pw=%Q)",
314 zUsername, zPasswd, zSha1Pw
315 );
316 if( uid<=0 ){
317 sleep(1);
318 zErrMsg =
319 @ <p><span class="loginError">
@@ -451,10 +453,37 @@
451 @ </table>
452 @ </form>
453 }
454 style_footer();
455 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
456
457 /*
458 ** Attempt to find login credentials for user zLogin on a peer repository
459 ** with project code zCode. Transfer those credentials to the local
460 ** repository.
@@ -481,20 +510,22 @@
481 if( zOtherRepo==0 ) return 0; /* No such peer repository */
482
483 rc = sqlite3_open(zOtherRepo, &pOther);
484 if( rc==SQLITE_OK ){
485 sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
 
 
486 sqlite3_busy_timeout(pOther, 5000);
487 zSQL = mprintf(
488 "SELECT cexpire FROM user"
489 " WHERE cookie=%Q"
490 " AND ipaddr=%Q"
491 " AND login=%Q"
492 " AND length(cap)>0"
493 " AND length(pw)>0"
494 " AND cexpire>julianday('now')",
495 zHash, zRemoteAddr, zLogin
 
496 );
497 pStmt = 0;
498 rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
499 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
500 db_multi_exec(
@@ -527,16 +558,16 @@
527 if( fossil_strcmp(zLogin, "developer")==0 ) return 0;
528 if( fossil_strcmp(zLogin, "reader")==0 ) return 0;
529 uid = db_int(0,
530 "SELECT uid FROM user"
531 " WHERE login=%Q"
532 " AND cookie=%Q"
533 " AND ipaddr=%Q"
534 " AND cexpire>julianday('now')"
535 " AND length(cap)>0"
536 " AND length(pw)>0",
537 zLogin, zCookie, zRemoteAddr
 
538 );
539 return uid;
540 }
541
542 /*
@@ -554,10 +585,13 @@
554 char *zRemoteAddr; /* Abbreviated IP address of the requestor */
555 const char *zCap = 0; /* Capability string */
556
557 /* Only run this check once. */
558 if( g.userUid!=0 ) return;
 
 
 
559
560 /* If the HTTP connection is coming over 127.0.0.1 and if
561 ** local login is disabled and if we are using HTTP and not HTTPS,
562 ** then there is no need to check user credentials.
563 **
564
--- src/login.c
+++ src/login.c
@@ -91,11 +91,11 @@
91 if( zCookieName==0 ){
92 zCookieName = db_text(0,
93 "SELECT 'fossil-' || substr(value,1,16)"
94 " FROM config"
95 " WHERE name IN ('project-code','login-group-code')"
96 " ORDER BY name /*sort*/"
97 );
98 }
99 return zCookieName;
100 }
101
@@ -230,12 +230,14 @@
230 }
231 if( g.perm.Password && zPasswd && (zNew1 = P("n1"))!=0 && (zNew2 = P("n2"))!=0 ){
232 /* The user requests a password change */
233 zSha1Pw = sha1_shared_secret(zPasswd, g.zLogin, 0);
234 if( db_int(1, "SELECT 0 FROM user"
235 " WHERE uid=%d"
236 " AND (constant_time_cmp(pw,%Q)=0"
237 " OR constant_time_cmp(pw,%Q)=0)",
238 g.userUid, zSha1Pw, zPasswd) ){
239 sleep(1);
240 zErrMsg =
241 @ <p><span class="loginError">
242 @ You entered an incorrect old password while attempting to change
243 @ your password. Your password is unchanged.
@@ -308,12 +310,12 @@
310 uid = db_int(0,
311 "SELECT uid FROM user"
312 " WHERE login=%Q"
313 " AND length(cap)>0 AND length(pw)>0"
314 " AND login NOT IN ('anonymous','nobody','developer','reader')"
315 " AND (constant_time_cmp(pw,%Q)=0 OR constant_time_cmp(pw,%Q)=0)",
316 zUsername, zSha1Pw, zPasswd
317 );
318 if( uid<=0 ){
319 sleep(1);
320 zErrMsg =
321 @ <p><span class="loginError">
@@ -451,10 +453,37 @@
453 @ </table>
454 @ </form>
455 }
456 style_footer();
457 }
458
459 /*
460 ** SQL function for constant time comparison of two values.
461 ** Sets result to 0 if two values are equal.
462 */
463 static void constant_time_cmp_function(
464 sqlite3_context *context,
465 int argc,
466 sqlite3_value **argv
467 ){
468 const unsigned char *buf1, *buf2;
469 int len, i;
470 unsigned char rc = 0;
471
472 assert( argc==2 );
473 len = sqlite3_value_bytes(argv[0]);
474 if( len==0 || len!=sqlite3_value_bytes(argv[1]) ){
475 rc = 1;
476 }else{
477 buf1 = sqlite3_value_text(argv[0]);
478 buf2 = sqlite3_value_text(argv[1]);
479 for( i=0; i<len; i++ ){
480 rc = rc | (buf1[i] ^ buf2[i]);
481 }
482 }
483 sqlite3_result_int(context, rc);
484 }
485
486 /*
487 ** Attempt to find login credentials for user zLogin on a peer repository
488 ** with project code zCode. Transfer those credentials to the local
489 ** repository.
@@ -481,20 +510,22 @@
510 if( zOtherRepo==0 ) return 0; /* No such peer repository */
511
512 rc = sqlite3_open(zOtherRepo, &pOther);
513 if( rc==SQLITE_OK ){
514 sqlite3_create_function(pOther,"now",0,SQLITE_ANY,0,db_now_function,0,0);
515 sqlite3_create_function(pOther, "constant_time_cmp", 2, SQLITE_UTF8, 0,
516 constant_time_cmp_function, 0, 0);
517 sqlite3_busy_timeout(pOther, 5000);
518 zSQL = mprintf(
519 "SELECT cexpire FROM user"
520 " WHERE login=%Q"
521 " AND ipaddr=%Q"
 
522 " AND length(cap)>0"
523 " AND length(pw)>0"
524 " AND cexpire>julianday('now')"
525 " AND constant_time_cmp(cookie,%Q)=0",
526 zLogin, zRemoteAddr, zHash
527 );
528 pStmt = 0;
529 rc = sqlite3_prepare_v2(pOther, zSQL, -1, &pStmt, 0);
530 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
531 db_multi_exec(
@@ -527,16 +558,16 @@
558 if( fossil_strcmp(zLogin, "developer")==0 ) return 0;
559 if( fossil_strcmp(zLogin, "reader")==0 ) return 0;
560 uid = db_int(0,
561 "SELECT uid FROM user"
562 " WHERE login=%Q"
 
563 " AND ipaddr=%Q"
564 " AND cexpire>julianday('now')"
565 " AND length(cap)>0"
566 " AND length(pw)>0"
567 " AND constant_time_cmp(cookie,%Q)=0",
568 zLogin, zRemoteAddr, zCookie
569 );
570 return uid;
571 }
572
573 /*
@@ -554,10 +585,13 @@
585 char *zRemoteAddr; /* Abbreviated IP address of the requestor */
586 const char *zCap = 0; /* Capability string */
587
588 /* Only run this check once. */
589 if( g.userUid!=0 ) return;
590
591 sqlite3_create_function(g.db, "constant_time_cmp", 2, SQLITE_UTF8, 0,
592 constant_time_cmp_function, 0, 0);
593
594 /* If the HTTP connection is coming over 127.0.0.1 and if
595 ** local login is disabled and if we are using HTTP and not HTTPS,
596 ** then there is no need to check user credentials.
597 **
598
+82 -5
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153153
154154
/* For defense against Cross-site Request Forgery attacks */
155155
char zCsrfToken[12]; /* Value of the anti-CSRF token */
156156
int okCsrf; /* Anti-CSRF token is present and valid */
157157
158
+ int parseCnt[10]; /* Counts of artifacts parsed */
158159
FILE *fDebug; /* Write debug information here, if the file exists */
159160
int thTrace; /* True to enable TH1 debugging output */
160161
Blob thLog; /* Text of the TH1 debugging output */
161162
162163
int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
219220
upr = mid - 1;
220221
}else{
221222
lwr = mid + 1;
222223
}
223224
}
224
- for(m=cnt=0, i=upr-2; i<=upr+3 && i<nMap; i++){
225
+ for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){
225226
if( i<0 ) continue;
226227
if( strncmp(zName, aMap[i].zName, n)==0 ){
227228
m = i;
228229
cnt++;
229230
}
@@ -233,10 +234,83 @@
233234
return 0;
234235
}
235236
return 1+(cnt>1);
236237
}
237238
239
+/*
240
+** Search g.argv for arguments "--args FILENAME". If found, then
241
+** (1) remove the two arguments from g.argv
242
+** (2) Read the file FILENAME
243
+** (3) Use the contents of FILE to replace the two removed arguments:
244
+** (a) Ignore blank lines in the file
245
+** (b) Each non-empty line of the file is an argument, except
246
+** (c) If the line begins with "-" and contains a space, it is broken
247
+** into two arguments at the space.
248
+*/
249
+static void expand_args_option(void){
250
+ Blob file = empty_blob; /* Content of the file */
251
+ Blob line = empty_blob; /* One line of the file */
252
+ unsigned int nLine; /* Number of lines in the file*/
253
+ unsigned int i, j, k; /* Loop counters */
254
+ int n; /* Number of bytes in one line */
255
+ char *z; /* General use string pointer */
256
+ char **newArgv; /* New expanded g.argv under construction */
257
+ char const * zFileName; /* input file name */
258
+ FILE * zInFile; /* input FILE */
259
+ for(i=1; i<g.argc-1; i++){
260
+ z = g.argv[i];
261
+ if( z[0]!='-' ) continue;
262
+ z++;
263
+ if( z[0]=='-' ) z++;
264
+ if( z[0]==0 ) return; /* Stop searching at "--" */
265
+ if( fossil_strcmp(z, "args")==0 ) break;
266
+ }
267
+ if( i>=g.argc-1 ) return;
268
+
269
+ zFileName = g.argv[i+1];
270
+ zInFile = (0==strcmp("-",zFileName))
271
+ ? stdin
272
+ : fopen(zFileName,"rb");
273
+ if(!zInFile){
274
+ fossil_panic("Cannot open -args file [%s]", zFileName);
275
+ }else{
276
+ blob_read_from_channel(&file, zInFile, -1);
277
+ if(stdin != zInFile){
278
+ fclose(zInFile);
279
+ }
280
+ zInFile = NULL;
281
+ }
282
+ z = blob_str(&file);
283
+ for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
284
+ newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
285
+ for(j=0; j<i; j++) newArgv[j] = g.argv[j];
286
+
287
+ blob_rewind(&file);
288
+ while( (n = blob_line(&file, &line))>0 ){
289
+ if( n<=1 ) continue;
290
+ z = blob_buffer(&line);
291
+ z[n-1] = 0;
292
+ if((n>1) && ('\r'==z[n-2])){
293
+ if(n==2) continue /*empty line*/;
294
+ z[n-2] = 0;
295
+ }
296
+ newArgv[j++] = z;
297
+ if( z[0]=='-' ){
298
+ for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
299
+ if( z[k] ){
300
+ z[k] = 0;
301
+ k++;
302
+ if( z[k] ) newArgv[j++] = &z[k];
303
+ }
304
+ }
305
+ }
306
+ i += 2;
307
+ while( i<g.argc ) newArgv[j++] = g.argv[i++];
308
+ newArgv[j] = 0;
309
+ g.argc = j;
310
+ g.argv = newArgv;
311
+}
238312
239313
/*
240314
** This procedure runs first.
241315
*/
242316
int main(int argc, char **argv){
@@ -251,10 +325,13 @@
251325
252326
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
253327
g.now = time(0);
254328
g.argc = argc;
255329
g.argv = argv;
330
+ expand_args_option();
331
+ argc = g.argc;
332
+ argv = g.argv;
256333
for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
257334
if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
258335
zCmdName = "cgi";
259336
}else if( argc<2 ){
260337
fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
330407
}
331408
332409
/*
333410
** Exit. Take care to close the database first.
334411
*/
335
-void fossil_exit(int rc){
412
+NORETURN void fossil_exit(int rc){
336413
db_close(1);
337414
exit(rc);
338415
}
339416
340417
/*
341418
** Print an error message, rollback all databases, and quit. These
342419
** routines never return.
343420
*/
344
-void fossil_panic(const char *zFormat, ...){
421
+NORETURN void fossil_panic(const char *zFormat, ...){
345422
char *z;
346423
va_list ap;
347424
static int once = 1;
348425
mainInFatalError = 1;
349426
va_start(ap, zFormat);
@@ -358,11 +435,11 @@
358435
fossil_puts(zOut, 1);
359436
}
360437
db_force_rollback();
361438
fossil_exit(1);
362439
}
363
-void fossil_fatal(const char *zFormat, ...){
440
+NORETURN void fossil_fatal(const char *zFormat, ...){
364441
char *z;
365442
va_list ap;
366443
mainInFatalError = 1;
367444
va_start(ap, zFormat);
368445
z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
846923
}
847924
848925
/*
849926
** Send an HTTP redirect back to the designated Index Page.
850927
*/
851
-void fossil_redirect_home(void){
928
+NORETURN void fossil_redirect_home(void){
852929
cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
853930
}
854931
855932
/*
856933
** If running as root, chroot to the directory containing the
857934
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153
154 /* For defense against Cross-site Request Forgery attacks */
155 char zCsrfToken[12]; /* Value of the anti-CSRF token */
156 int okCsrf; /* Anti-CSRF token is present and valid */
157
 
158 FILE *fDebug; /* Write debug information here, if the file exists */
159 int thTrace; /* True to enable TH1 debugging output */
160 Blob thLog; /* Text of the TH1 debugging output */
161
162 int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
219 upr = mid - 1;
220 }else{
221 lwr = mid + 1;
222 }
223 }
224 for(m=cnt=0, i=upr-2; i<=upr+3 && i<nMap; i++){
225 if( i<0 ) continue;
226 if( strncmp(zName, aMap[i].zName, n)==0 ){
227 m = i;
228 cnt++;
229 }
@@ -233,10 +234,83 @@
233 return 0;
234 }
235 return 1+(cnt>1);
236 }
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
239 /*
240 ** This procedure runs first.
241 */
242 int main(int argc, char **argv){
@@ -251,10 +325,13 @@
251
252 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
253 g.now = time(0);
254 g.argc = argc;
255 g.argv = argv;
 
 
 
256 for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
257 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
258 zCmdName = "cgi";
259 }else if( argc<2 ){
260 fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
330 }
331
332 /*
333 ** Exit. Take care to close the database first.
334 */
335 void fossil_exit(int rc){
336 db_close(1);
337 exit(rc);
338 }
339
340 /*
341 ** Print an error message, rollback all databases, and quit. These
342 ** routines never return.
343 */
344 void fossil_panic(const char *zFormat, ...){
345 char *z;
346 va_list ap;
347 static int once = 1;
348 mainInFatalError = 1;
349 va_start(ap, zFormat);
@@ -358,11 +435,11 @@
358 fossil_puts(zOut, 1);
359 }
360 db_force_rollback();
361 fossil_exit(1);
362 }
363 void fossil_fatal(const char *zFormat, ...){
364 char *z;
365 va_list ap;
366 mainInFatalError = 1;
367 va_start(ap, zFormat);
368 z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
846 }
847
848 /*
849 ** Send an HTTP redirect back to the designated Index Page.
850 */
851 void fossil_redirect_home(void){
852 cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
853 }
854
855 /*
856 ** If running as root, chroot to the directory containing the
857
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153
154 /* For defense against Cross-site Request Forgery attacks */
155 char zCsrfToken[12]; /* Value of the anti-CSRF token */
156 int okCsrf; /* Anti-CSRF token is present and valid */
157
158 int parseCnt[10]; /* Counts of artifacts parsed */
159 FILE *fDebug; /* Write debug information here, if the file exists */
160 int thTrace; /* True to enable TH1 debugging output */
161 Blob thLog; /* Text of the TH1 debugging output */
162
163 int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
220 upr = mid - 1;
221 }else{
222 lwr = mid + 1;
223 }
224 }
225 for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){
226 if( i<0 ) continue;
227 if( strncmp(zName, aMap[i].zName, n)==0 ){
228 m = i;
229 cnt++;
230 }
@@ -233,10 +234,83 @@
234 return 0;
235 }
236 return 1+(cnt>1);
237 }
238
239 /*
240 ** Search g.argv for arguments "--args FILENAME". If found, then
241 ** (1) remove the two arguments from g.argv
242 ** (2) Read the file FILENAME
243 ** (3) Use the contents of FILE to replace the two removed arguments:
244 ** (a) Ignore blank lines in the file
245 ** (b) Each non-empty line of the file is an argument, except
246 ** (c) If the line begins with "-" and contains a space, it is broken
247 ** into two arguments at the space.
248 */
249 static void expand_args_option(void){
250 Blob file = empty_blob; /* Content of the file */
251 Blob line = empty_blob; /* One line of the file */
252 unsigned int nLine; /* Number of lines in the file*/
253 unsigned int i, j, k; /* Loop counters */
254 int n; /* Number of bytes in one line */
255 char *z; /* General use string pointer */
256 char **newArgv; /* New expanded g.argv under construction */
257 char const * zFileName; /* input file name */
258 FILE * zInFile; /* input FILE */
259 for(i=1; i<g.argc-1; i++){
260 z = g.argv[i];
261 if( z[0]!='-' ) continue;
262 z++;
263 if( z[0]=='-' ) z++;
264 if( z[0]==0 ) return; /* Stop searching at "--" */
265 if( fossil_strcmp(z, "args")==0 ) break;
266 }
267 if( i>=g.argc-1 ) return;
268
269 zFileName = g.argv[i+1];
270 zInFile = (0==strcmp("-",zFileName))
271 ? stdin
272 : fopen(zFileName,"rb");
273 if(!zInFile){
274 fossil_panic("Cannot open -args file [%s]", zFileName);
275 }else{
276 blob_read_from_channel(&file, zInFile, -1);
277 if(stdin != zInFile){
278 fclose(zInFile);
279 }
280 zInFile = NULL;
281 }
282 z = blob_str(&file);
283 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
284 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
285 for(j=0; j<i; j++) newArgv[j] = g.argv[j];
286
287 blob_rewind(&file);
288 while( (n = blob_line(&file, &line))>0 ){
289 if( n<=1 ) continue;
290 z = blob_buffer(&line);
291 z[n-1] = 0;
292 if((n>1) && ('\r'==z[n-2])){
293 if(n==2) continue /*empty line*/;
294 z[n-2] = 0;
295 }
296 newArgv[j++] = z;
297 if( z[0]=='-' ){
298 for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
299 if( z[k] ){
300 z[k] = 0;
301 k++;
302 if( z[k] ) newArgv[j++] = &z[k];
303 }
304 }
305 }
306 i += 2;
307 while( i<g.argc ) newArgv[j++] = g.argv[i++];
308 newArgv[j] = 0;
309 g.argc = j;
310 g.argv = newArgv;
311 }
312
313 /*
314 ** This procedure runs first.
315 */
316 int main(int argc, char **argv){
@@ -251,10 +325,13 @@
325
326 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
327 g.now = time(0);
328 g.argc = argc;
329 g.argv = argv;
330 expand_args_option();
331 argc = g.argc;
332 argv = g.argv;
333 for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
334 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
335 zCmdName = "cgi";
336 }else if( argc<2 ){
337 fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
407 }
408
409 /*
410 ** Exit. Take care to close the database first.
411 */
412 NORETURN void fossil_exit(int rc){
413 db_close(1);
414 exit(rc);
415 }
416
417 /*
418 ** Print an error message, rollback all databases, and quit. These
419 ** routines never return.
420 */
421 NORETURN void fossil_panic(const char *zFormat, ...){
422 char *z;
423 va_list ap;
424 static int once = 1;
425 mainInFatalError = 1;
426 va_start(ap, zFormat);
@@ -358,11 +435,11 @@
435 fossil_puts(zOut, 1);
436 }
437 db_force_rollback();
438 fossil_exit(1);
439 }
440 NORETURN void fossil_fatal(const char *zFormat, ...){
441 char *z;
442 va_list ap;
443 mainInFatalError = 1;
444 va_start(ap, zFormat);
445 z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
923 }
924
925 /*
926 ** Send an HTTP redirect back to the designated Index Page.
927 */
928 NORETURN void fossil_redirect_home(void){
929 cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
930 }
931
932 /*
933 ** If running as root, chroot to the directory containing the
934
+82 -5
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153153
154154
/* For defense against Cross-site Request Forgery attacks */
155155
char zCsrfToken[12]; /* Value of the anti-CSRF token */
156156
int okCsrf; /* Anti-CSRF token is present and valid */
157157
158
+ int parseCnt[10]; /* Counts of artifacts parsed */
158159
FILE *fDebug; /* Write debug information here, if the file exists */
159160
int thTrace; /* True to enable TH1 debugging output */
160161
Blob thLog; /* Text of the TH1 debugging output */
161162
162163
int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
219220
upr = mid - 1;
220221
}else{
221222
lwr = mid + 1;
222223
}
223224
}
224
- for(m=cnt=0, i=upr-2; i<=upr+3 && i<nMap; i++){
225
+ for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){
225226
if( i<0 ) continue;
226227
if( strncmp(zName, aMap[i].zName, n)==0 ){
227228
m = i;
228229
cnt++;
229230
}
@@ -233,10 +234,83 @@
233234
return 0;
234235
}
235236
return 1+(cnt>1);
236237
}
237238
239
+/*
240
+** Search g.argv for arguments "--args FILENAME". If found, then
241
+** (1) remove the two arguments from g.argv
242
+** (2) Read the file FILENAME
243
+** (3) Use the contents of FILE to replace the two removed arguments:
244
+** (a) Ignore blank lines in the file
245
+** (b) Each non-empty line of the file is an argument, except
246
+** (c) If the line begins with "-" and contains a space, it is broken
247
+** into two arguments at the space.
248
+*/
249
+static void expand_args_option(void){
250
+ Blob file = empty_blob; /* Content of the file */
251
+ Blob line = empty_blob; /* One line of the file */
252
+ unsigned int nLine; /* Number of lines in the file*/
253
+ unsigned int i, j, k; /* Loop counters */
254
+ int n; /* Number of bytes in one line */
255
+ char *z; /* General use string pointer */
256
+ char **newArgv; /* New expanded g.argv under construction */
257
+ char const * zFileName; /* input file name */
258
+ FILE * zInFile; /* input FILE */
259
+ for(i=1; i<g.argc-1; i++){
260
+ z = g.argv[i];
261
+ if( z[0]!='-' ) continue;
262
+ z++;
263
+ if( z[0]=='-' ) z++;
264
+ if( z[0]==0 ) return; /* Stop searching at "--" */
265
+ if( fossil_strcmp(z, "args")==0 ) break;
266
+ }
267
+ if( i>=g.argc-1 ) return;
268
+
269
+ zFileName = g.argv[i+1];
270
+ zInFile = (0==strcmp("-",zFileName))
271
+ ? stdin
272
+ : fopen(zFileName,"rb");
273
+ if(!zInFile){
274
+ fossil_panic("Cannot open -args file [%s]", zFileName);
275
+ }else{
276
+ blob_read_from_channel(&file, zInFile, -1);
277
+ if(stdin != zInFile){
278
+ fclose(zInFile);
279
+ }
280
+ zInFile = NULL;
281
+ }
282
+ z = blob_str(&file);
283
+ for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
284
+ newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
285
+ for(j=0; j<i; j++) newArgv[j] = g.argv[j];
286
+
287
+ blob_rewind(&file);
288
+ while( (n = blob_line(&file, &line))>0 ){
289
+ if( n<=1 ) continue;
290
+ z = blob_buffer(&line);
291
+ z[n-1] = 0;
292
+ if((n>1) && ('\r'==z[n-2])){
293
+ if(n==2) continue /*empty line*/;
294
+ z[n-2] = 0;
295
+ }
296
+ newArgv[j++] = z;
297
+ if( z[0]=='-' ){
298
+ for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
299
+ if( z[k] ){
300
+ z[k] = 0;
301
+ k++;
302
+ if( z[k] ) newArgv[j++] = &z[k];
303
+ }
304
+ }
305
+ }
306
+ i += 2;
307
+ while( i<g.argc ) newArgv[j++] = g.argv[i++];
308
+ newArgv[j] = 0;
309
+ g.argc = j;
310
+ g.argv = newArgv;
311
+}
238312
239313
/*
240314
** This procedure runs first.
241315
*/
242316
int main(int argc, char **argv){
@@ -251,10 +325,13 @@
251325
252326
sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
253327
g.now = time(0);
254328
g.argc = argc;
255329
g.argv = argv;
330
+ expand_args_option();
331
+ argc = g.argc;
332
+ argv = g.argv;
256333
for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
257334
if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
258335
zCmdName = "cgi";
259336
}else if( argc<2 ){
260337
fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
330407
}
331408
332409
/*
333410
** Exit. Take care to close the database first.
334411
*/
335
-void fossil_exit(int rc){
412
+NORETURN void fossil_exit(int rc){
336413
db_close(1);
337414
exit(rc);
338415
}
339416
340417
/*
341418
** Print an error message, rollback all databases, and quit. These
342419
** routines never return.
343420
*/
344
-void fossil_panic(const char *zFormat, ...){
421
+NORETURN void fossil_panic(const char *zFormat, ...){
345422
char *z;
346423
va_list ap;
347424
static int once = 1;
348425
mainInFatalError = 1;
349426
va_start(ap, zFormat);
@@ -358,11 +435,11 @@
358435
fossil_puts(zOut, 1);
359436
}
360437
db_force_rollback();
361438
fossil_exit(1);
362439
}
363
-void fossil_fatal(const char *zFormat, ...){
440
+NORETURN void fossil_fatal(const char *zFormat, ...){
364441
char *z;
365442
va_list ap;
366443
mainInFatalError = 1;
367444
va_start(ap, zFormat);
368445
z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
846923
}
847924
848925
/*
849926
** Send an HTTP redirect back to the designated Index Page.
850927
*/
851
-void fossil_redirect_home(void){
928
+NORETURN void fossil_redirect_home(void){
852929
cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
853930
}
854931
855932
/*
856933
** If running as root, chroot to the directory containing the
857934
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153
154 /* For defense against Cross-site Request Forgery attacks */
155 char zCsrfToken[12]; /* Value of the anti-CSRF token */
156 int okCsrf; /* Anti-CSRF token is present and valid */
157
 
158 FILE *fDebug; /* Write debug information here, if the file exists */
159 int thTrace; /* True to enable TH1 debugging output */
160 Blob thLog; /* Text of the TH1 debugging output */
161
162 int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
219 upr = mid - 1;
220 }else{
221 lwr = mid + 1;
222 }
223 }
224 for(m=cnt=0, i=upr-2; i<=upr+3 && i<nMap; i++){
225 if( i<0 ) continue;
226 if( strncmp(zName, aMap[i].zName, n)==0 ){
227 m = i;
228 cnt++;
229 }
@@ -233,10 +234,83 @@
233 return 0;
234 }
235 return 1+(cnt>1);
236 }
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
239 /*
240 ** This procedure runs first.
241 */
242 int main(int argc, char **argv){
@@ -251,10 +325,13 @@
251
252 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
253 g.now = time(0);
254 g.argc = argc;
255 g.argv = argv;
 
 
 
256 for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
257 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
258 zCmdName = "cgi";
259 }else if( argc<2 ){
260 fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
330 }
331
332 /*
333 ** Exit. Take care to close the database first.
334 */
335 void fossil_exit(int rc){
336 db_close(1);
337 exit(rc);
338 }
339
340 /*
341 ** Print an error message, rollback all databases, and quit. These
342 ** routines never return.
343 */
344 void fossil_panic(const char *zFormat, ...){
345 char *z;
346 va_list ap;
347 static int once = 1;
348 mainInFatalError = 1;
349 va_start(ap, zFormat);
@@ -358,11 +435,11 @@
358 fossil_puts(zOut, 1);
359 }
360 db_force_rollback();
361 fossil_exit(1);
362 }
363 void fossil_fatal(const char *zFormat, ...){
364 char *z;
365 va_list ap;
366 mainInFatalError = 1;
367 va_start(ap, zFormat);
368 z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
846 }
847
848 /*
849 ** Send an HTTP redirect back to the designated Index Page.
850 */
851 void fossil_redirect_home(void){
852 cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
853 }
854
855 /*
856 ** If running as root, chroot to the directory containing the
857
--- src/main.c
+++ src/main.c
@@ -153,10 +153,11 @@
153
154 /* For defense against Cross-site Request Forgery attacks */
155 char zCsrfToken[12]; /* Value of the anti-CSRF token */
156 int okCsrf; /* Anti-CSRF token is present and valid */
157
158 int parseCnt[10]; /* Counts of artifacts parsed */
159 FILE *fDebug; /* Write debug information here, if the file exists */
160 int thTrace; /* True to enable TH1 debugging output */
161 Blob thLog; /* Text of the TH1 debugging output */
162
163 int isHome; /* True if rendering the "home" page */
@@ -219,11 +220,11 @@
220 upr = mid - 1;
221 }else{
222 lwr = mid + 1;
223 }
224 }
225 for(m=cnt=0, i=upr-2; cnt<2 && i<=upr+3 && i<nMap; i++){
226 if( i<0 ) continue;
227 if( strncmp(zName, aMap[i].zName, n)==0 ){
228 m = i;
229 cnt++;
230 }
@@ -233,10 +234,83 @@
234 return 0;
235 }
236 return 1+(cnt>1);
237 }
238
239 /*
240 ** Search g.argv for arguments "--args FILENAME". If found, then
241 ** (1) remove the two arguments from g.argv
242 ** (2) Read the file FILENAME
243 ** (3) Use the contents of FILE to replace the two removed arguments:
244 ** (a) Ignore blank lines in the file
245 ** (b) Each non-empty line of the file is an argument, except
246 ** (c) If the line begins with "-" and contains a space, it is broken
247 ** into two arguments at the space.
248 */
249 static void expand_args_option(void){
250 Blob file = empty_blob; /* Content of the file */
251 Blob line = empty_blob; /* One line of the file */
252 unsigned int nLine; /* Number of lines in the file*/
253 unsigned int i, j, k; /* Loop counters */
254 int n; /* Number of bytes in one line */
255 char *z; /* General use string pointer */
256 char **newArgv; /* New expanded g.argv under construction */
257 char const * zFileName; /* input file name */
258 FILE * zInFile; /* input FILE */
259 for(i=1; i<g.argc-1; i++){
260 z = g.argv[i];
261 if( z[0]!='-' ) continue;
262 z++;
263 if( z[0]=='-' ) z++;
264 if( z[0]==0 ) return; /* Stop searching at "--" */
265 if( fossil_strcmp(z, "args")==0 ) break;
266 }
267 if( i>=g.argc-1 ) return;
268
269 zFileName = g.argv[i+1];
270 zInFile = (0==strcmp("-",zFileName))
271 ? stdin
272 : fopen(zFileName,"rb");
273 if(!zInFile){
274 fossil_panic("Cannot open -args file [%s]", zFileName);
275 }else{
276 blob_read_from_channel(&file, zInFile, -1);
277 if(stdin != zInFile){
278 fclose(zInFile);
279 }
280 zInFile = NULL;
281 }
282 z = blob_str(&file);
283 for(k=0, nLine=1; z[k]; k++) if( z[k]=='\n' ) nLine++;
284 newArgv = fossil_malloc( sizeof(char*)*(g.argc + nLine*2) );
285 for(j=0; j<i; j++) newArgv[j] = g.argv[j];
286
287 blob_rewind(&file);
288 while( (n = blob_line(&file, &line))>0 ){
289 if( n<=1 ) continue;
290 z = blob_buffer(&line);
291 z[n-1] = 0;
292 if((n>1) && ('\r'==z[n-2])){
293 if(n==2) continue /*empty line*/;
294 z[n-2] = 0;
295 }
296 newArgv[j++] = z;
297 if( z[0]=='-' ){
298 for(k=1; z[k] && !fossil_isspace(z[k]); k++){}
299 if( z[k] ){
300 z[k] = 0;
301 k++;
302 if( z[k] ) newArgv[j++] = &z[k];
303 }
304 }
305 }
306 i += 2;
307 while( i<g.argc ) newArgv[j++] = g.argv[i++];
308 newArgv[j] = 0;
309 g.argc = j;
310 g.argv = newArgv;
311 }
312
313 /*
314 ** This procedure runs first.
315 */
316 int main(int argc, char **argv){
@@ -251,10 +325,13 @@
325
326 sqlite3_config(SQLITE_CONFIG_LOG, fossil_sqlite_log, 0);
327 g.now = time(0);
328 g.argc = argc;
329 g.argv = argv;
330 expand_args_option();
331 argc = g.argc;
332 argv = g.argv;
333 for(i=0; i<argc; i++) g.argv[i] = fossil_mbcs_to_utf8(argv[i]);
334 if( getenv("GATEWAY_INTERFACE")!=0 && !find_option("nocgi", 0, 0)){
335 zCmdName = "cgi";
336 }else if( argc<2 ){
337 fossil_fatal("Usage: %s COMMAND ...\n"
@@ -330,20 +407,20 @@
407 }
408
409 /*
410 ** Exit. Take care to close the database first.
411 */
412 NORETURN void fossil_exit(int rc){
413 db_close(1);
414 exit(rc);
415 }
416
417 /*
418 ** Print an error message, rollback all databases, and quit. These
419 ** routines never return.
420 */
421 NORETURN void fossil_panic(const char *zFormat, ...){
422 char *z;
423 va_list ap;
424 static int once = 1;
425 mainInFatalError = 1;
426 va_start(ap, zFormat);
@@ -358,11 +435,11 @@
435 fossil_puts(zOut, 1);
436 }
437 db_force_rollback();
438 fossil_exit(1);
439 }
440 NORETURN void fossil_fatal(const char *zFormat, ...){
441 char *z;
442 va_list ap;
443 mainInFatalError = 1;
444 va_start(ap, zFormat);
445 z = vmprintf(zFormat, ap);
@@ -846,11 +923,11 @@
923 }
924
925 /*
926 ** Send an HTTP redirect back to the designated Index Page.
927 */
928 NORETURN void fossil_redirect_home(void){
929 cgi_redirectf("%s%s", g.zTop, db_get("index-page", "/index"));
930 }
931
932 /*
933 ** If running as root, chroot to the directory containing the
934
--- src/manifest.c
+++ src/manifest.c
@@ -345,14 +345,24 @@
345345
char cType;
346346
char *z;
347347
int n;
348348
char *zUuid;
349349
int sz = 0;
350
+ int isRepeat;
351
+ static Bag seen;
352
+
353
+ if( bag_find(&seen, rid) ){
354
+ isRepeat = 1;
355
+ }else{
356
+ isRepeat = 0;
357
+ bag_insert(&seen, rid);
358
+ }
350359
351360
/* Every control artifact ends with a '\n' character. Exit early
352361
** if that is not the case for this artifact.
353362
*/
363
+ if( !isRepeat ) g.parseCnt[0]++;
354364
z = blob_materialize(pContent);
355365
n = blob_size(pContent);
356366
if( n<=0 || z[n-1]!='\n' ){
357367
blob_reset(pContent);
358368
return 0;
@@ -885,10 +895,11 @@
885895
if( p->zWikiTitle ) goto manifest_syntax_error;
886896
if( p->zTicketUuid ) goto manifest_syntax_error;
887897
p->type = CFTYPE_MANIFEST;
888898
}
889899
md5sum_init();
900
+ if( !isRepeat ) g.parseCnt[p->type]++;
890901
return p;
891902
892903
manifest_syntax_error:
893904
/*fprintf(stderr, "Manifest error on line %i\n", lineNo);fflush(stderr);*/
894905
md5sum_init();
@@ -1808,10 +1819,48 @@
18081819
"VALUES('t',%.17g,%d,%Q,%Q)",
18091820
p->rDate, rid, p->zUser, zComment
18101821
);
18111822
free(zComment);
18121823
}
1824
+ }
1825
+ if( p->type==CFTYPE_CONTROL ){
1826
+ Blob comment;
1827
+ int i;
1828
+ const char *zName;
1829
+ const char *zValue;
1830
+ const char *zUuid;
1831
+ blob_zero(&comment);
1832
+ for(i=0; i<p->nTag; i++){
1833
+ zUuid = p->aTag[i].zUuid;
1834
+ if( i==0 || fossil_strcmp(zUuid, p->aTag[i-1].zUuid)!=0 ){
1835
+ if( i>0 ) blob_append(&comment, " ", 1);
1836
+ blob_appendf(&comment, "Tag changes on [/timeline?dp=%S&n=4 | %S]:",
1837
+ zUuid, zUuid);
1838
+ }
1839
+ zName = p->aTag[i].zName;
1840
+ zValue = p->aTag[i].zValue;
1841
+ if( zName[0]=='-' ){
1842
+ blob_appendf(&comment, " Cancel");
1843
+ }else if( zName[0]=='+' ){
1844
+ blob_appendf(&comment, " Add");
1845
+ }else{
1846
+ blob_appendf(&comment, " Add propagating");
1847
+ }
1848
+ if( memcmp(&zName[1], "sym-",4)==0 ){
1849
+ blob_appendf(&comment, " symbolic tag \"%h\".", &zName[5]);
1850
+ }else if( fossil_strcmp(&zName[1], "comment")!=0 && zValue && zValue[0] ){
1851
+ blob_appendf(&comment, " %h=%h.", &zName[1], zValue);
1852
+ }else{
1853
+ blob_appendf(&comment, " %h.", &zName[1]);
1854
+ }
1855
+ }
1856
+ db_multi_exec(
1857
+ "REPLACE INTO event(type,mtime,objid,user,comment)"
1858
+ "VALUES('g',%.17g,%d,%Q,%Q)",
1859
+ p->rDate, rid, p->zUser, blob_str(&comment)
1860
+ );
1861
+ blob_reset(&comment);
18131862
}
18141863
db_end_transaction(0);
18151864
if( p->type==CFTYPE_MANIFEST ){
18161865
manifest_cache_insert(p);
18171866
}else{
18181867
--- src/manifest.c
+++ src/manifest.c
@@ -345,14 +345,24 @@
345 char cType;
346 char *z;
347 int n;
348 char *zUuid;
349 int sz = 0;
 
 
 
 
 
 
 
 
 
350
351 /* Every control artifact ends with a '\n' character. Exit early
352 ** if that is not the case for this artifact.
353 */
 
354 z = blob_materialize(pContent);
355 n = blob_size(pContent);
356 if( n<=0 || z[n-1]!='\n' ){
357 blob_reset(pContent);
358 return 0;
@@ -885,10 +895,11 @@
885 if( p->zWikiTitle ) goto manifest_syntax_error;
886 if( p->zTicketUuid ) goto manifest_syntax_error;
887 p->type = CFTYPE_MANIFEST;
888 }
889 md5sum_init();
 
890 return p;
891
892 manifest_syntax_error:
893 /*fprintf(stderr, "Manifest error on line %i\n", lineNo);fflush(stderr);*/
894 md5sum_init();
@@ -1808,10 +1819,48 @@
1808 "VALUES('t',%.17g,%d,%Q,%Q)",
1809 p->rDate, rid, p->zUser, zComment
1810 );
1811 free(zComment);
1812 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1813 }
1814 db_end_transaction(0);
1815 if( p->type==CFTYPE_MANIFEST ){
1816 manifest_cache_insert(p);
1817 }else{
1818
--- src/manifest.c
+++ src/manifest.c
@@ -345,14 +345,24 @@
345 char cType;
346 char *z;
347 int n;
348 char *zUuid;
349 int sz = 0;
350 int isRepeat;
351 static Bag seen;
352
353 if( bag_find(&seen, rid) ){
354 isRepeat = 1;
355 }else{
356 isRepeat = 0;
357 bag_insert(&seen, rid);
358 }
359
360 /* Every control artifact ends with a '\n' character. Exit early
361 ** if that is not the case for this artifact.
362 */
363 if( !isRepeat ) g.parseCnt[0]++;
364 z = blob_materialize(pContent);
365 n = blob_size(pContent);
366 if( n<=0 || z[n-1]!='\n' ){
367 blob_reset(pContent);
368 return 0;
@@ -885,10 +895,11 @@
895 if( p->zWikiTitle ) goto manifest_syntax_error;
896 if( p->zTicketUuid ) goto manifest_syntax_error;
897 p->type = CFTYPE_MANIFEST;
898 }
899 md5sum_init();
900 if( !isRepeat ) g.parseCnt[p->type]++;
901 return p;
902
903 manifest_syntax_error:
904 /*fprintf(stderr, "Manifest error on line %i\n", lineNo);fflush(stderr);*/
905 md5sum_init();
@@ -1808,10 +1819,48 @@
1819 "VALUES('t',%.17g,%d,%Q,%Q)",
1820 p->rDate, rid, p->zUser, zComment
1821 );
1822 free(zComment);
1823 }
1824 }
1825 if( p->type==CFTYPE_CONTROL ){
1826 Blob comment;
1827 int i;
1828 const char *zName;
1829 const char *zValue;
1830 const char *zUuid;
1831 blob_zero(&comment);
1832 for(i=0; i<p->nTag; i++){
1833 zUuid = p->aTag[i].zUuid;
1834 if( i==0 || fossil_strcmp(zUuid, p->aTag[i-1].zUuid)!=0 ){
1835 if( i>0 ) blob_append(&comment, " ", 1);
1836 blob_appendf(&comment, "Tag changes on [/timeline?dp=%S&n=4 | %S]:",
1837 zUuid, zUuid);
1838 }
1839 zName = p->aTag[i].zName;
1840 zValue = p->aTag[i].zValue;
1841 if( zName[0]=='-' ){
1842 blob_appendf(&comment, " Cancel");
1843 }else if( zName[0]=='+' ){
1844 blob_appendf(&comment, " Add");
1845 }else{
1846 blob_appendf(&comment, " Add propagating");
1847 }
1848 if( memcmp(&zName[1], "sym-",4)==0 ){
1849 blob_appendf(&comment, " symbolic tag \"%h\".", &zName[5]);
1850 }else if( fossil_strcmp(&zName[1], "comment")!=0 && zValue && zValue[0] ){
1851 blob_appendf(&comment, " %h=%h.", &zName[1], zValue);
1852 }else{
1853 blob_appendf(&comment, " %h.", &zName[1]);
1854 }
1855 }
1856 db_multi_exec(
1857 "REPLACE INTO event(type,mtime,objid,user,comment)"
1858 "VALUES('g',%.17g,%d,%Q,%Q)",
1859 p->rDate, rid, p->zUser, blob_str(&comment)
1860 );
1861 blob_reset(&comment);
1862 }
1863 db_end_transaction(0);
1864 if( p->type==CFTYPE_MANIFEST ){
1865 manifest_cache_insert(p);
1866 }else{
1867
+18 -4
--- src/merge.c
+++ src/merge.c
@@ -73,10 +73,11 @@
7373
int debugFlag; /* True if --debug is present */
7474
int nChng; /* Number of file name changes */
7575
int *aChng; /* An array of file name changes */
7676
int i; /* Loop counter */
7777
int nConflict = 0; /* Number of conflicts seen */
78
+ int nOverwrite = 0; /* Number of unmanaged files overwritten */
7879
int caseSensitive; /* True for case-sensitive filenames */
7980
Stmt q;
8081
8182
8283
/* Notation:
@@ -319,19 +320,27 @@
319320
while( db_step(&q)==SQLITE_ROW ){
320321
int idm = db_column_int(&q, 0);
321322
int rowid = db_column_int(&q, 1);
322323
int idv;
323324
const char *zName;
325
+ char *zFullName;
324326
db_multi_exec(
325327
"INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,islink,pathname)"
326328
" SELECT %d,3,0,rid,mrid,isexe,islink,pathname FROM vfile WHERE id=%d",
327329
vid, idm
328330
);
329331
idv = db_last_insert_rowid();
330332
db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
331333
zName = db_column_text(&q, 2);
332
- fossil_print("ADDED %s\n", zName);
334
+ zFullName = mprintf("%s%s", g.zLocalRoot, zName);
335
+ if( file_wd_isfile_or_link(zFullName) ){
336
+ fossil_print("ADDED %s (overwrites an unmanaged file)\n", zName);
337
+ nOverwrite++;
338
+ }else{
339
+ fossil_print("ADDED %s\n", zName);
340
+ }
341
+ fossil_free(zFullName);
333342
if( !nochangeFlag ){
334343
undo_save(zName);
335344
vfile_to_disk(0, idm, 0, 0);
336345
}
337346
}
@@ -495,13 +504,18 @@
495504
db_finalize(&q);
496505
497506
498507
/* Report on conflicts
499508
*/
500
- if( nConflict && !nochangeFlag ){
501
- fossil_warning(
502
- "WARNING: merge conflicts - see messages above for details.\n");
509
+ if( !nochangeFlag ){
510
+ if( nConflict ){
511
+ fossil_print("WARNING: %d merge conflicts", nConflict);
512
+ }
513
+ if( nOverwrite ){
514
+ fossil_warning("WARNING: %d unmanaged files were overwritten",
515
+ nOverwrite);
516
+ }
503517
}
504518
505519
/*
506520
** Clean up the mid and pid VFILE entries. Then commit the changes.
507521
*/
508522
--- src/merge.c
+++ src/merge.c
@@ -73,10 +73,11 @@
73 int debugFlag; /* True if --debug is present */
74 int nChng; /* Number of file name changes */
75 int *aChng; /* An array of file name changes */
76 int i; /* Loop counter */
77 int nConflict = 0; /* Number of conflicts seen */
 
78 int caseSensitive; /* True for case-sensitive filenames */
79 Stmt q;
80
81
82 /* Notation:
@@ -319,19 +320,27 @@
319 while( db_step(&q)==SQLITE_ROW ){
320 int idm = db_column_int(&q, 0);
321 int rowid = db_column_int(&q, 1);
322 int idv;
323 const char *zName;
 
324 db_multi_exec(
325 "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,islink,pathname)"
326 " SELECT %d,3,0,rid,mrid,isexe,islink,pathname FROM vfile WHERE id=%d",
327 vid, idm
328 );
329 idv = db_last_insert_rowid();
330 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
331 zName = db_column_text(&q, 2);
332 fossil_print("ADDED %s\n", zName);
 
 
 
 
 
 
 
333 if( !nochangeFlag ){
334 undo_save(zName);
335 vfile_to_disk(0, idm, 0, 0);
336 }
337 }
@@ -495,13 +504,18 @@
495 db_finalize(&q);
496
497
498 /* Report on conflicts
499 */
500 if( nConflict && !nochangeFlag ){
501 fossil_warning(
502 "WARNING: merge conflicts - see messages above for details.\n");
 
 
 
 
 
503 }
504
505 /*
506 ** Clean up the mid and pid VFILE entries. Then commit the changes.
507 */
508
--- src/merge.c
+++ src/merge.c
@@ -73,10 +73,11 @@
73 int debugFlag; /* True if --debug is present */
74 int nChng; /* Number of file name changes */
75 int *aChng; /* An array of file name changes */
76 int i; /* Loop counter */
77 int nConflict = 0; /* Number of conflicts seen */
78 int nOverwrite = 0; /* Number of unmanaged files overwritten */
79 int caseSensitive; /* True for case-sensitive filenames */
80 Stmt q;
81
82
83 /* Notation:
@@ -319,19 +320,27 @@
320 while( db_step(&q)==SQLITE_ROW ){
321 int idm = db_column_int(&q, 0);
322 int rowid = db_column_int(&q, 1);
323 int idv;
324 const char *zName;
325 char *zFullName;
326 db_multi_exec(
327 "INSERT INTO vfile(vid,chnged,deleted,rid,mrid,isexe,islink,pathname)"
328 " SELECT %d,3,0,rid,mrid,isexe,islink,pathname FROM vfile WHERE id=%d",
329 vid, idm
330 );
331 idv = db_last_insert_rowid();
332 db_multi_exec("UPDATE fv SET idv=%d WHERE rowid=%d", idv, rowid);
333 zName = db_column_text(&q, 2);
334 zFullName = mprintf("%s%s", g.zLocalRoot, zName);
335 if( file_wd_isfile_or_link(zFullName) ){
336 fossil_print("ADDED %s (overwrites an unmanaged file)\n", zName);
337 nOverwrite++;
338 }else{
339 fossil_print("ADDED %s\n", zName);
340 }
341 fossil_free(zFullName);
342 if( !nochangeFlag ){
343 undo_save(zName);
344 vfile_to_disk(0, idm, 0, 0);
345 }
346 }
@@ -495,13 +504,18 @@
504 db_finalize(&q);
505
506
507 /* Report on conflicts
508 */
509 if( !nochangeFlag ){
510 if( nConflict ){
511 fossil_print("WARNING: %d merge conflicts", nConflict);
512 }
513 if( nOverwrite ){
514 fossil_warning("WARNING: %d unmanaged files were overwritten",
515 nOverwrite);
516 }
517 }
518
519 /*
520 ** Clean up the mid and pid VFILE entries. Then commit the changes.
521 */
522
+2 -2
--- src/name.c
+++ src/name.c
@@ -154,11 +154,11 @@
154154
" WHERE tag.tagname='sym-%q' "
155155
" AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
156156
" AND event.objid=tagxref.rid "
157157
" AND blob.rid=event.objid "
158158
" AND event.type GLOB '%q'"
159
- " ORDER BY event.mtime DESC ",
159
+ " ORDER BY event.mtime DESC /*sort*/",
160160
zTag, zType
161161
);
162162
if( zUuid==0 ){
163163
int nTag = strlen(zTag);
164164
int i;
@@ -180,11 +180,11 @@
180180
" AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
181181
" AND event.objid=tagxref.rid "
182182
" AND blob.rid=event.objid "
183183
" AND event.mtime<=julianday(%Q %s)"
184184
" AND event.type GLOB '%q'"
185
- " ORDER BY event.mtime DESC ",
185
+ " ORDER BY event.mtime DESC /*sort*/ ",
186186
zTagBase, zDate, (useUtc ? "" : ",'utc'"), zType
187187
);
188188
break;
189189
}
190190
}
191191
--- src/name.c
+++ src/name.c
@@ -154,11 +154,11 @@
154 " WHERE tag.tagname='sym-%q' "
155 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
156 " AND event.objid=tagxref.rid "
157 " AND blob.rid=event.objid "
158 " AND event.type GLOB '%q'"
159 " ORDER BY event.mtime DESC ",
160 zTag, zType
161 );
162 if( zUuid==0 ){
163 int nTag = strlen(zTag);
164 int i;
@@ -180,11 +180,11 @@
180 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
181 " AND event.objid=tagxref.rid "
182 " AND blob.rid=event.objid "
183 " AND event.mtime<=julianday(%Q %s)"
184 " AND event.type GLOB '%q'"
185 " ORDER BY event.mtime DESC ",
186 zTagBase, zDate, (useUtc ? "" : ",'utc'"), zType
187 );
188 break;
189 }
190 }
191
--- src/name.c
+++ src/name.c
@@ -154,11 +154,11 @@
154 " WHERE tag.tagname='sym-%q' "
155 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
156 " AND event.objid=tagxref.rid "
157 " AND blob.rid=event.objid "
158 " AND event.type GLOB '%q'"
159 " ORDER BY event.mtime DESC /*sort*/",
160 zTag, zType
161 );
162 if( zUuid==0 ){
163 int nTag = strlen(zTag);
164 int i;
@@ -180,11 +180,11 @@
180 " AND tagxref.tagid=tag.tagid AND tagxref.tagtype>0 "
181 " AND event.objid=tagxref.rid "
182 " AND blob.rid=event.objid "
183 " AND event.mtime<=julianday(%Q %s)"
184 " AND event.type GLOB '%q'"
185 " ORDER BY event.mtime DESC /*sort*/ ",
186 zTagBase, zDate, (useUtc ? "" : ",'utc'"), zType
187 );
188 break;
189 }
190 }
191
+2 -1
--- src/path.c
+++ src/path.c
@@ -95,12 +95,13 @@
9595
}
9696
9797
/*
9898
** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
9999
*/
100
-void path_reverse_path(void){
100
+static void path_reverse_path(void){
101101
PathNode *p;
102
+ assert( path.pEnd!=0 );
102103
for(p=path.pEnd; p && p->pFrom; p = p->pFrom){
103104
p->pFrom->u.pTo = p;
104105
}
105106
path.pEnd->u.pTo = 0;
106107
assert( p==path.pStart );
107108
--- src/path.c
+++ src/path.c
@@ -95,12 +95,13 @@
95 }
96
97 /*
98 ** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
99 */
100 void path_reverse_path(void){
101 PathNode *p;
 
102 for(p=path.pEnd; p && p->pFrom; p = p->pFrom){
103 p->pFrom->u.pTo = p;
104 }
105 path.pEnd->u.pTo = 0;
106 assert( p==path.pStart );
107
--- src/path.c
+++ src/path.c
@@ -95,12 +95,13 @@
95 }
96
97 /*
98 ** Construct the path from path.pStart to path.pEnd in the u.pTo fields.
99 */
100 static void path_reverse_path(void){
101 PathNode *p;
102 assert( path.pEnd!=0 );
103 for(p=path.pEnd; p && p->pFrom; p = p->pFrom){
104 p->pFrom->u.pTo = p;
105 }
106 path.pEnd->u.pTo = 0;
107 assert( p==path.pStart );
108
--- src/rebuild.c
+++ src/rebuild.c
@@ -500,10 +500,11 @@
500500
** --noverify Skip the verification of changes to the BLOB table
501501
** --pagesize N Set the database pagesize to N. (512..65536 and power of 2)
502502
** --randomize Scan artifacts in a random order
503503
** --vacuum Run VACUUM on the database after rebuilding
504504
** --wal Set Write-Ahead-Log journalling mode on the database
505
+** --stats Show artifact statistics after rebuilding
505506
**
506507
** See also: deconstruct, reconstruct
507508
*/
508509
void rebuild_database(void){
509510
int forceFlag;
@@ -514,18 +515,20 @@
514515
const char *zPagesize;
515516
int newPagesize = 0;
516517
int activateWal;
517518
int runVacuum;
518519
int runCompress;
520
+ int showStats;
519521
520522
omitVerify = find_option("noverify",0,0)!=0;
521523
forceFlag = find_option("force","f",0)!=0;
522524
randomizeFlag = find_option("randomize", 0, 0)!=0;
523525
doClustering = find_option("cluster", 0, 0)!=0;
524526
runVacuum = find_option("vacuum",0,0)!=0;
525527
runCompress = find_option("compress",0,0)!=0;
526528
zPagesize = find_option("pagesize",0,1);
529
+ showStats = find_option("stats",0,0)!=0;
527530
if( zPagesize ){
528531
newPagesize = atoi(zPagesize);
529532
if( newPagesize<512 || newPagesize>65536
530533
|| (newPagesize&(newPagesize-1))!=0
531534
){
@@ -579,10 +582,30 @@
579582
}
580583
if( activateWal ){
581584
db_multi_exec("PRAGMA journal_mode=WAL;");
582585
}
583586
}
587
+ if( showStats ){
588
+ static struct { int idx; const char *zLabel; } aStat[] = {
589
+ { CFTYPE_ANY, "Artifacts:" },
590
+ { CFTYPE_MANIFEST, "Manifests:" },
591
+ { CFTYPE_CLUSTER, "Clusters:" },
592
+ { CFTYPE_CONTROL, "Tags:" },
593
+ { CFTYPE_WIKI, "Wikis:" },
594
+ { CFTYPE_TICKET, "Tickets:" },
595
+ { CFTYPE_ATTACHMENT,"Attachments:" },
596
+ { CFTYPE_EVENT, "Events:" },
597
+ };
598
+ int i;
599
+ int subtotal = 0;
600
+ for(i=0; i<count(aStat); i++){
601
+ int k = aStat[i].idx;
602
+ fossil_print("%-15s %6d\n", aStat[i].zLabel, g.parseCnt[k]);
603
+ if( k>0 ) subtotal += g.parseCnt[k];
604
+ }
605
+ fossil_print("%-15s %6d\n", "Other:", g.parseCnt[CFTYPE_ANY] - subtotal);
606
+ }
584607
}
585608
586609
/*
587610
** COMMAND: test-detach ?REPOSITORY?
588611
**
@@ -763,10 +786,11 @@
763786
);
764787
if( bVerily ){
765788
db_multi_exec(
766789
"DELETE FROM concealed;"
767790
"UPDATE rcvfrom SET ipaddr='unknown';"
791
+ "DROP TABLE IF EXISTS accesslog;"
768792
"UPDATE user SET photo=NULL, info='';"
769793
);
770794
}
771795
}
772796
if( !bNeedRebuild ){
773797
--- src/rebuild.c
+++ src/rebuild.c
@@ -500,10 +500,11 @@
500 ** --noverify Skip the verification of changes to the BLOB table
501 ** --pagesize N Set the database pagesize to N. (512..65536 and power of 2)
502 ** --randomize Scan artifacts in a random order
503 ** --vacuum Run VACUUM on the database after rebuilding
504 ** --wal Set Write-Ahead-Log journalling mode on the database
 
505 **
506 ** See also: deconstruct, reconstruct
507 */
508 void rebuild_database(void){
509 int forceFlag;
@@ -514,18 +515,20 @@
514 const char *zPagesize;
515 int newPagesize = 0;
516 int activateWal;
517 int runVacuum;
518 int runCompress;
 
519
520 omitVerify = find_option("noverify",0,0)!=0;
521 forceFlag = find_option("force","f",0)!=0;
522 randomizeFlag = find_option("randomize", 0, 0)!=0;
523 doClustering = find_option("cluster", 0, 0)!=0;
524 runVacuum = find_option("vacuum",0,0)!=0;
525 runCompress = find_option("compress",0,0)!=0;
526 zPagesize = find_option("pagesize",0,1);
 
527 if( zPagesize ){
528 newPagesize = atoi(zPagesize);
529 if( newPagesize<512 || newPagesize>65536
530 || (newPagesize&(newPagesize-1))!=0
531 ){
@@ -579,10 +582,30 @@
579 }
580 if( activateWal ){
581 db_multi_exec("PRAGMA journal_mode=WAL;");
582 }
583 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584 }
585
586 /*
587 ** COMMAND: test-detach ?REPOSITORY?
588 **
@@ -763,10 +786,11 @@
763 );
764 if( bVerily ){
765 db_multi_exec(
766 "DELETE FROM concealed;"
767 "UPDATE rcvfrom SET ipaddr='unknown';"
 
768 "UPDATE user SET photo=NULL, info='';"
769 );
770 }
771 }
772 if( !bNeedRebuild ){
773
--- src/rebuild.c
+++ src/rebuild.c
@@ -500,10 +500,11 @@
500 ** --noverify Skip the verification of changes to the BLOB table
501 ** --pagesize N Set the database pagesize to N. (512..65536 and power of 2)
502 ** --randomize Scan artifacts in a random order
503 ** --vacuum Run VACUUM on the database after rebuilding
504 ** --wal Set Write-Ahead-Log journalling mode on the database
505 ** --stats Show artifact statistics after rebuilding
506 **
507 ** See also: deconstruct, reconstruct
508 */
509 void rebuild_database(void){
510 int forceFlag;
@@ -514,18 +515,20 @@
515 const char *zPagesize;
516 int newPagesize = 0;
517 int activateWal;
518 int runVacuum;
519 int runCompress;
520 int showStats;
521
522 omitVerify = find_option("noverify",0,0)!=0;
523 forceFlag = find_option("force","f",0)!=0;
524 randomizeFlag = find_option("randomize", 0, 0)!=0;
525 doClustering = find_option("cluster", 0, 0)!=0;
526 runVacuum = find_option("vacuum",0,0)!=0;
527 runCompress = find_option("compress",0,0)!=0;
528 zPagesize = find_option("pagesize",0,1);
529 showStats = find_option("stats",0,0)!=0;
530 if( zPagesize ){
531 newPagesize = atoi(zPagesize);
532 if( newPagesize<512 || newPagesize>65536
533 || (newPagesize&(newPagesize-1))!=0
534 ){
@@ -579,10 +582,30 @@
582 }
583 if( activateWal ){
584 db_multi_exec("PRAGMA journal_mode=WAL;");
585 }
586 }
587 if( showStats ){
588 static struct { int idx; const char *zLabel; } aStat[] = {
589 { CFTYPE_ANY, "Artifacts:" },
590 { CFTYPE_MANIFEST, "Manifests:" },
591 { CFTYPE_CLUSTER, "Clusters:" },
592 { CFTYPE_CONTROL, "Tags:" },
593 { CFTYPE_WIKI, "Wikis:" },
594 { CFTYPE_TICKET, "Tickets:" },
595 { CFTYPE_ATTACHMENT,"Attachments:" },
596 { CFTYPE_EVENT, "Events:" },
597 };
598 int i;
599 int subtotal = 0;
600 for(i=0; i<count(aStat); i++){
601 int k = aStat[i].idx;
602 fossil_print("%-15s %6d\n", aStat[i].zLabel, g.parseCnt[k]);
603 if( k>0 ) subtotal += g.parseCnt[k];
604 }
605 fossil_print("%-15s %6d\n", "Other:", g.parseCnt[CFTYPE_ANY] - subtotal);
606 }
607 }
608
609 /*
610 ** COMMAND: test-detach ?REPOSITORY?
611 **
@@ -763,10 +786,11 @@
786 );
787 if( bVerily ){
788 db_multi_exec(
789 "DELETE FROM concealed;"
790 "UPDATE rcvfrom SET ipaddr='unknown';"
791 "DROP TABLE IF EXISTS accesslog;"
792 "UPDATE user SET photo=NULL, info='';"
793 );
794 }
795 }
796 if( !bNeedRebuild ){
797
+3 -19
--- src/report.c
+++ src/report.c
@@ -630,17 +630,11 @@
630630
char **azName /* Names of the columns */
631631
){
632632
struct GenerateHTML *pState = (struct GenerateHTML*)pUser;
633633
int i;
634634
const char *zTid; /* Ticket UUID. (value of column named '#') */
635
- int rn; /* Report number */
636635
char *zBg = 0; /* Use this background color */
637
- char zPage[30]; /* Text version of the ticket number */
638
-
639
- /* Get the report number
640
- */
641
- rn = pState->rn;
642636
643637
/* Do initialization
644638
*/
645639
if( pState->nCount==0 ){
646640
/* Turn off the authorizer. It is no longer doing anything since the
@@ -719,11 +713,10 @@
719713
*/
720714
zBg = pState->iBg>=0 ? azArg[pState->iBg] : 0;
721715
if( zBg==0 ) zBg = "white";
722716
@ <tr style="background-color:%h(zBg)">
723717
zTid = 0;
724
- zPage[0] = 0;
725718
for(i=0; i<nArg; i++){
726719
char *zData;
727720
if( i==pState->iBg ) continue;
728721
zData = azArg[i];
729722
if( zData==0 ) zData = "";
@@ -1108,41 +1101,32 @@
11081101
const char *zFilter,
11091102
tTktShowEncoding enc
11101103
){
11111104
Stmt q;
11121105
char *zSql;
1113
- const char *zTitle;
1114
- const char *zOwner;
1115
- const char *zClrKey;
11161106
char *zErr1 = 0;
11171107
char *zErr2 = 0;
11181108
int count = 0;
11191109
int rn;
11201110
11211111
if (!zRep || !strcmp(zRep,zFullTicketRptRn) || !strcmp(zRep,zFullTicketRptTitle) ){
1122
- zTitle = zFullTicketRptTitle;
11231112
zSql = "SELECT * FROM ticket";
1124
- zOwner = g.zLogin;
1125
- zClrKey = "";
11261113
}else{
11271114
rn = atoi(zRep);
11281115
if( rn ){
11291116
db_prepare(&q,
1130
- "SELECT title, sqlcode, owner, cols FROM reportfmt WHERE rn=%d", rn);
1117
+ "SELECT sqlcode FROM reportfmt WHERE rn=%d", rn);
11311118
}else{
11321119
db_prepare(&q,
1133
- "SELECT title, sqlcode, owner, cols FROM reportfmt WHERE title='%s'", zRep);
1120
+ "SELECT sqlcode FROM reportfmt WHERE title='%s'", zRep);
11341121
}
11351122
if( db_step(&q)!=SQLITE_ROW ){
11361123
db_finalize(&q);
11371124
rpt_list_reports();
11381125
fossil_fatal("unknown report format(%s)!",zRep);
11391126
}
1140
- zTitle = db_column_malloc(&q, 0);
1141
- zSql = db_column_malloc(&q, 1);
1142
- zOwner = db_column_malloc(&q, 2);
1143
- zClrKey = db_column_malloc(&q, 3);
1127
+ zSql = db_column_malloc(&q, 0);
11441128
db_finalize(&q);
11451129
}
11461130
if( zFilter ){
11471131
zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
11481132
}
11491133
--- src/report.c
+++ src/report.c
@@ -630,17 +630,11 @@
630 char **azName /* Names of the columns */
631 ){
632 struct GenerateHTML *pState = (struct GenerateHTML*)pUser;
633 int i;
634 const char *zTid; /* Ticket UUID. (value of column named '#') */
635 int rn; /* Report number */
636 char *zBg = 0; /* Use this background color */
637 char zPage[30]; /* Text version of the ticket number */
638
639 /* Get the report number
640 */
641 rn = pState->rn;
642
643 /* Do initialization
644 */
645 if( pState->nCount==0 ){
646 /* Turn off the authorizer. It is no longer doing anything since the
@@ -719,11 +713,10 @@
719 */
720 zBg = pState->iBg>=0 ? azArg[pState->iBg] : 0;
721 if( zBg==0 ) zBg = "white";
722 @ <tr style="background-color:%h(zBg)">
723 zTid = 0;
724 zPage[0] = 0;
725 for(i=0; i<nArg; i++){
726 char *zData;
727 if( i==pState->iBg ) continue;
728 zData = azArg[i];
729 if( zData==0 ) zData = "";
@@ -1108,41 +1101,32 @@
1108 const char *zFilter,
1109 tTktShowEncoding enc
1110 ){
1111 Stmt q;
1112 char *zSql;
1113 const char *zTitle;
1114 const char *zOwner;
1115 const char *zClrKey;
1116 char *zErr1 = 0;
1117 char *zErr2 = 0;
1118 int count = 0;
1119 int rn;
1120
1121 if (!zRep || !strcmp(zRep,zFullTicketRptRn) || !strcmp(zRep,zFullTicketRptTitle) ){
1122 zTitle = zFullTicketRptTitle;
1123 zSql = "SELECT * FROM ticket";
1124 zOwner = g.zLogin;
1125 zClrKey = "";
1126 }else{
1127 rn = atoi(zRep);
1128 if( rn ){
1129 db_prepare(&q,
1130 "SELECT title, sqlcode, owner, cols FROM reportfmt WHERE rn=%d", rn);
1131 }else{
1132 db_prepare(&q,
1133 "SELECT title, sqlcode, owner, cols FROM reportfmt WHERE title='%s'", zRep);
1134 }
1135 if( db_step(&q)!=SQLITE_ROW ){
1136 db_finalize(&q);
1137 rpt_list_reports();
1138 fossil_fatal("unknown report format(%s)!",zRep);
1139 }
1140 zTitle = db_column_malloc(&q, 0);
1141 zSql = db_column_malloc(&q, 1);
1142 zOwner = db_column_malloc(&q, 2);
1143 zClrKey = db_column_malloc(&q, 3);
1144 db_finalize(&q);
1145 }
1146 if( zFilter ){
1147 zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
1148 }
1149
--- src/report.c
+++ src/report.c
@@ -630,17 +630,11 @@
630 char **azName /* Names of the columns */
631 ){
632 struct GenerateHTML *pState = (struct GenerateHTML*)pUser;
633 int i;
634 const char *zTid; /* Ticket UUID. (value of column named '#') */
 
635 char *zBg = 0; /* Use this background color */
 
 
 
 
 
636
637 /* Do initialization
638 */
639 if( pState->nCount==0 ){
640 /* Turn off the authorizer. It is no longer doing anything since the
@@ -719,11 +713,10 @@
713 */
714 zBg = pState->iBg>=0 ? azArg[pState->iBg] : 0;
715 if( zBg==0 ) zBg = "white";
716 @ <tr style="background-color:%h(zBg)">
717 zTid = 0;
 
718 for(i=0; i<nArg; i++){
719 char *zData;
720 if( i==pState->iBg ) continue;
721 zData = azArg[i];
722 if( zData==0 ) zData = "";
@@ -1108,41 +1101,32 @@
1101 const char *zFilter,
1102 tTktShowEncoding enc
1103 ){
1104 Stmt q;
1105 char *zSql;
 
 
 
1106 char *zErr1 = 0;
1107 char *zErr2 = 0;
1108 int count = 0;
1109 int rn;
1110
1111 if (!zRep || !strcmp(zRep,zFullTicketRptRn) || !strcmp(zRep,zFullTicketRptTitle) ){
 
1112 zSql = "SELECT * FROM ticket";
 
 
1113 }else{
1114 rn = atoi(zRep);
1115 if( rn ){
1116 db_prepare(&q,
1117 "SELECT sqlcode FROM reportfmt WHERE rn=%d", rn);
1118 }else{
1119 db_prepare(&q,
1120 "SELECT sqlcode FROM reportfmt WHERE title='%s'", zRep);
1121 }
1122 if( db_step(&q)!=SQLITE_ROW ){
1123 db_finalize(&q);
1124 rpt_list_reports();
1125 fossil_fatal("unknown report format(%s)!",zRep);
1126 }
1127 zSql = db_column_malloc(&q, 0);
 
 
 
1128 db_finalize(&q);
1129 }
1130 if( zFilter ){
1131 zSql = mprintf("SELECT * FROM (%s) WHERE %s",zSql,zFilter);
1132 }
1133
+1 -1
--- src/schema.c
+++ src/schema.c
@@ -251,11 +251,11 @@
251251
@ CREATE TABLE leaf(rid INTEGER PRIMARY KEY);
252252
@
253253
@ -- Events used to generate a timeline
254254
@ --
255255
@ CREATE TABLE event(
256
-@ type TEXT, -- Type of event: 'ci', 'w', 'e', 't'
256
+@ type TEXT, -- Type of event: 'ci', 'w', 'e', 't', 'g'
257257
@ mtime DATETIME, -- Time of occurrence. Julian day.
258258
@ objid INTEGER PRIMARY KEY, -- Associated record ID
259259
@ tagid INTEGER, -- Associated ticket or wiki name tag
260260
@ uid INTEGER REFERENCES user, -- User who caused the event
261261
@ bgcolor TEXT, -- Color set by 'bgcolor' property
262262
--- src/schema.c
+++ src/schema.c
@@ -251,11 +251,11 @@
251 @ CREATE TABLE leaf(rid INTEGER PRIMARY KEY);
252 @
253 @ -- Events used to generate a timeline
254 @ --
255 @ CREATE TABLE event(
256 @ type TEXT, -- Type of event: 'ci', 'w', 'e', 't'
257 @ mtime DATETIME, -- Time of occurrence. Julian day.
258 @ objid INTEGER PRIMARY KEY, -- Associated record ID
259 @ tagid INTEGER, -- Associated ticket or wiki name tag
260 @ uid INTEGER REFERENCES user, -- User who caused the event
261 @ bgcolor TEXT, -- Color set by 'bgcolor' property
262
--- src/schema.c
+++ src/schema.c
@@ -251,11 +251,11 @@
251 @ CREATE TABLE leaf(rid INTEGER PRIMARY KEY);
252 @
253 @ -- Events used to generate a timeline
254 @ --
255 @ CREATE TABLE event(
256 @ type TEXT, -- Type of event: 'ci', 'w', 'e', 't', 'g'
257 @ mtime DATETIME, -- Time of occurrence. Julian day.
258 @ objid INTEGER PRIMARY KEY, -- Associated record ID
259 @ tagid INTEGER, -- Associated ticket or wiki name tag
260 @ uid INTEGER REFERENCES user, -- User who caused the event
261 @ bgcolor TEXT, -- Color set by 'bgcolor' property
262
+10 -1
--- src/setup.c
+++ src/setup.c
@@ -848,18 +848,27 @@
848848
@ <a href="%s(g.zTop)/help/server">fossil http</a> commands
849849
@ without the "--localauth" option.
850850
@ <li> The server is started from CGI without the "localauth" keyword
851851
@ in the CGI script.
852852
@ </ol>
853
+ @
854
+ @ <hr />
855
+ onoff_attribute("Enable /test_env",
856
+ "test_env_enable", "test_env_enable", 0);
857
+ @ <p>When enabled, the %h(g.zBaseURL)/test_env URL is available to all
858
+ @ users. When disabled (the default) only users Admin and Setup can visit
859
+ @ the /test_env page.
860
+ @ </p>
861
+ @
853862
@ <hr />
854863
onoff_attribute("Allow REMOTE_USER authentication",
855864
"remote_user_ok", "remote_user_ok", 0);
856865
@ <p>When enabled, if the REMOTE_USER environment variable is set to the
857866
@ login name of a valid user and no other login credentials are available,
858867
@ then the REMOTE_USER is accepted as an authenticated user.
859868
@ </p>
860
-
869
+ @
861870
@ <hr />
862871
entry_attribute("Login expiration time", 6, "cookie-expire", "cex", "8766");
863872
@ <p>The number of hours for which a login is valid. This must be a
864873
@ positive number. The default is 8760 hours which is approximately equal
865874
@ to a year.</p>
866875
--- src/setup.c
+++ src/setup.c
@@ -848,18 +848,27 @@
848 @ <a href="%s(g.zTop)/help/server">fossil http</a> commands
849 @ without the "--localauth" option.
850 @ <li> The server is started from CGI without the "localauth" keyword
851 @ in the CGI script.
852 @ </ol>
 
 
 
 
 
 
 
 
 
853 @ <hr />
854 onoff_attribute("Allow REMOTE_USER authentication",
855 "remote_user_ok", "remote_user_ok", 0);
856 @ <p>When enabled, if the REMOTE_USER environment variable is set to the
857 @ login name of a valid user and no other login credentials are available,
858 @ then the REMOTE_USER is accepted as an authenticated user.
859 @ </p>
860
861 @ <hr />
862 entry_attribute("Login expiration time", 6, "cookie-expire", "cex", "8766");
863 @ <p>The number of hours for which a login is valid. This must be a
864 @ positive number. The default is 8760 hours which is approximately equal
865 @ to a year.</p>
866
--- src/setup.c
+++ src/setup.c
@@ -848,18 +848,27 @@
848 @ <a href="%s(g.zTop)/help/server">fossil http</a> commands
849 @ without the "--localauth" option.
850 @ <li> The server is started from CGI without the "localauth" keyword
851 @ in the CGI script.
852 @ </ol>
853 @
854 @ <hr />
855 onoff_attribute("Enable /test_env",
856 "test_env_enable", "test_env_enable", 0);
857 @ <p>When enabled, the %h(g.zBaseURL)/test_env URL is available to all
858 @ users. When disabled (the default) only users Admin and Setup can visit
859 @ the /test_env page.
860 @ </p>
861 @
862 @ <hr />
863 onoff_attribute("Allow REMOTE_USER authentication",
864 "remote_user_ok", "remote_user_ok", 0);
865 @ <p>When enabled, if the REMOTE_USER environment variable is set to the
866 @ login name of a valid user and no other login credentials are available,
867 @ then the REMOTE_USER is accepted as an authenticated user.
868 @ </p>
869 @
870 @ <hr />
871 entry_attribute("Login expiration time", 6, "cookie-expire", "cex", "8766");
872 @ <p>The number of hours for which a login is valid. This must be a
873 @ positive number. The default is 8760 hours which is approximately equal
874 @ to a year.</p>
875
+1 -1
--- src/sha1.c
+++ src/sha1.c
@@ -82,11 +82,11 @@
8282
#define d qq[3]
8383
#define e qq[4]
8484
8585
void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
8686
{
87
- unsigned int qq[5]; // a, b, c, d, e;
87
+ unsigned int qq[5]; /* a, b, c, d, e; */
8888
static int one = 1;
8989
unsigned int block[16];
9090
memcpy(block, buffer, 64);
9191
memcpy(qq,state,5*sizeof(unsigned int));
9292
9393
--- src/sha1.c
+++ src/sha1.c
@@ -82,11 +82,11 @@
82 #define d qq[3]
83 #define e qq[4]
84
85 void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
86 {
87 unsigned int qq[5]; // a, b, c, d, e;
88 static int one = 1;
89 unsigned int block[16];
90 memcpy(block, buffer, 64);
91 memcpy(qq,state,5*sizeof(unsigned int));
92
93
--- src/sha1.c
+++ src/sha1.c
@@ -82,11 +82,11 @@
82 #define d qq[3]
83 #define e qq[4]
84
85 void SHA1Transform(unsigned int state[5], const unsigned char buffer[64])
86 {
87 unsigned int qq[5]; /* a, b, c, d, e; */
88 static int one = 1;
89 unsigned int block[16];
90 memcpy(block, buffer, 64);
91 memcpy(qq,state,5*sizeof(unsigned int));
92
93
+107 -60
--- src/shell.c
+++ src/shell.c
@@ -10,15 +10,26 @@
1010
**
1111
*************************************************************************
1212
** This file contains code to implement the "sqlite" command line
1313
** utility for accessing SQLite databases.
1414
*/
15
-#if defined(_WIN32) || defined(WIN32)
15
+#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
1616
/* This needs to come before any includes for MSVC compiler */
1717
#define _CRT_SECURE_NO_WARNINGS
1818
#endif
1919
20
+/*
21
+** Enable large-file support for fopen() and friends on unix.
22
+*/
23
+#ifndef SQLITE_DISABLE_LFS
24
+# define _LARGE_FILE 1
25
+# ifndef _FILE_OFFSET_BITS
26
+# define _FILE_OFFSET_BITS 64
27
+# endif
28
+# define _LARGEFILE_SOURCE 1
29
+#endif
30
+
2031
#include <stdlib.h>
2132
#include <string.h>
2233
#include <stdio.h>
2334
#include <assert.h>
2435
#include "sqlite3.h"
@@ -58,11 +69,11 @@
5869
#define isatty(h) _isatty(h)
5970
#define access(f,m) _access((f),(m))
6071
#else
6172
/* Make sure isatty() has a prototype.
6273
*/
63
-extern int isatty();
74
+extern int isatty(int);
6475
#endif
6576
6677
#if defined(_WIN32_WCE)
6778
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
6879
* thus we always assume that we have a console. That can be
@@ -71,10 +82,15 @@
7182
#define isatty(x) 1
7283
#endif
7384
7485
/* True if the timer is enabled */
7586
static int enableTimer = 0;
87
+
88
+/* ctype macros that work with signed characters */
89
+#define IsSpace(X) isspace((unsigned char)X)
90
+#define IsDigit(X) isdigit((unsigned char)X)
91
+#define ToLower(X) (char)tolower((unsigned char)X)
7692
7793
#if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
7894
#include <sys/time.h>
7995
#include <sys/resource.h>
8096
@@ -263,27 +279,27 @@
263279
/*
264280
** Determines if a string is a number of not.
265281
*/
266282
static int isNumber(const char *z, int *realnum){
267283
if( *z=='-' || *z=='+' ) z++;
268
- if( !isdigit(*z) ){
284
+ if( !IsDigit(*z) ){
269285
return 0;
270286
}
271287
z++;
272288
if( realnum ) *realnum = 0;
273
- while( isdigit(*z) ){ z++; }
289
+ while( IsDigit(*z) ){ z++; }
274290
if( *z=='.' ){
275291
z++;
276
- if( !isdigit(*z) ) return 0;
277
- while( isdigit(*z) ){ z++; }
292
+ if( !IsDigit(*z) ) return 0;
293
+ while( IsDigit(*z) ){ z++; }
278294
if( realnum ) *realnum = 1;
279295
}
280296
if( *z=='e' || *z=='E' ){
281297
z++;
282298
if( *z=='+' || *z=='-' ) z++;
283
- if( !isdigit(*z) ) return 0;
284
- while( isdigit(*z) ){ z++; }
299
+ if( !IsDigit(*z) ) return 0;
300
+ while( IsDigit(*z) ){ z++; }
285301
if( realnum ) *realnum = 1;
286302
}
287303
return *z==0;
288304
}
289305
@@ -320,22 +336,20 @@
320336
*/
321337
static char *local_getline(char *zPrompt, FILE *in){
322338
char *zLine;
323339
int nLine;
324340
int n;
325
- int eol;
326341
327342
if( zPrompt && *zPrompt ){
328343
printf("%s",zPrompt);
329344
fflush(stdout);
330345
}
331346
nLine = 100;
332347
zLine = malloc( nLine );
333348
if( zLine==0 ) return 0;
334349
n = 0;
335
- eol = 0;
336
- while( !eol ){
350
+ while( 1 ){
337351
if( n+100>nLine ){
338352
nLine = nLine*2 + 100;
339353
zLine = realloc(zLine, nLine);
340354
if( zLine==0 ) return 0;
341355
}
@@ -343,19 +357,18 @@
343357
if( n==0 ){
344358
free(zLine);
345359
return 0;
346360
}
347361
zLine[n] = 0;
348
- eol = 1;
349362
break;
350363
}
351364
while( zLine[n] ){ n++; }
352365
if( n>0 && zLine[n-1]=='\n' ){
353366
n--;
354367
if( n>0 && zLine[n-1]=='\r' ) n--;
355368
zLine[n] = 0;
356
- eol = 1;
369
+ break;
357370
}
358371
}
359372
zLine = realloc( zLine, n+1 );
360373
return zLine;
361374
}
@@ -400,10 +413,11 @@
400413
sqlite3 *db; /* The database */
401414
int echoOn; /* True to echo input commands */
402415
int statsOn; /* True to display memory stats before each finalize */
403416
int cnt; /* Number of records displayed so far */
404417
FILE *out; /* Write results here */
418
+ int nErr; /* Number of errors seen */
405419
int mode; /* An output mode setting */
406420
int writableSchema; /* True if PRAGMA writable_schema=ON */
407421
int showHeader; /* True to show column names in List or Column mode */
408422
char *zDestTable; /* Name of destination table when MODE_Insert */
409423
char separator[20]; /* Separator character for MODE_List */
@@ -925,31 +939,37 @@
925939
**
926940
** This is used, for example, to show the schema of the database by
927941
** querying the SQLITE_MASTER table.
928942
*/
929943
static int run_table_dump_query(
930
- FILE *out, /* Send output here */
931
- sqlite3 *db, /* Database to query */
932
- const char *zSelect, /* SELECT statement to extract content */
933
- const char *zFirstRow /* Print before first row, if not NULL */
944
+ struct callback_data *p, /* Query context */
945
+ const char *zSelect, /* SELECT statement to extract content */
946
+ const char *zFirstRow /* Print before first row, if not NULL */
934947
){
935948
sqlite3_stmt *pSelect;
936949
int rc;
937
- rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
950
+ rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
938951
if( rc!=SQLITE_OK || !pSelect ){
952
+ fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
953
+ p->nErr++;
939954
return rc;
940955
}
941956
rc = sqlite3_step(pSelect);
942957
while( rc==SQLITE_ROW ){
943958
if( zFirstRow ){
944
- fprintf(out, "%s", zFirstRow);
959
+ fprintf(p->out, "%s", zFirstRow);
945960
zFirstRow = 0;
946961
}
947
- fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
962
+ fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
948963
rc = sqlite3_step(pSelect);
949964
}
950
- return sqlite3_finalize(pSelect);
965
+ rc = sqlite3_finalize(pSelect);
966
+ if( rc!=SQLITE_OK ){
967
+ fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
968
+ p->nErr++;
969
+ }
970
+ return rc;
951971
}
952972
953973
/*
954974
** Allocate space and save off current error string.
955975
*/
@@ -1027,11 +1047,16 @@
10271047
fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
10281048
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
10291049
fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
10301050
iHiwtr = iCur = -1;
10311051
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1032
- fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
1052
+ fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1053
+ sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1054
+ fprintf(pArg->out, "Page cache hits: %d\n", iCur);
1055
+ iHiwtr = iCur = -1;
1056
+ sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1057
+ fprintf(pArg->out, "Page cache misses: %d\n", iCur);
10331058
iHiwtr = iCur = -1;
10341059
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
10351060
fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
10361061
iHiwtr = iCur = -1;
10371062
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
@@ -1067,10 +1092,11 @@
10671092
struct callback_data *pArg, /* Pointer to struct callback_data */
10681093
char **pzErrMsg /* Error msg written here */
10691094
){
10701095
sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10711096
int rc = SQLITE_OK; /* Return Code */
1097
+ int rc2;
10721098
const char *zLeftover; /* Tail of unprocessed SQL */
10731099
10741100
if( pzErrMsg ){
10751101
*pzErrMsg = NULL;
10761102
}
@@ -1083,11 +1109,11 @@
10831109
}
10841110
}else{
10851111
if( !pStmt ){
10861112
/* this happens for a comment or white-space */
10871113
zSql = zLeftover;
1088
- while( isspace(zSql[0]) ) zSql++;
1114
+ while( IsSpace(zSql[0]) ) zSql++;
10891115
continue;
10901116
}
10911117
10921118
/* save off the prepared statment handle and reset row count */
10931119
if( pArg ){
@@ -1160,14 +1186,15 @@
11601186
}
11611187
11621188
/* Finalize the statement just executed. If this fails, save a
11631189
** copy of the error message. Otherwise, set zSql to point to the
11641190
** next statement to execute. */
1165
- rc = sqlite3_finalize(pStmt);
1191
+ rc2 = sqlite3_finalize(pStmt);
1192
+ if( rc!=SQLITE_NOMEM ) rc = rc2;
11661193
if( rc==SQLITE_OK ){
11671194
zSql = zLeftover;
1168
- while( isspace(zSql[0]) ) zSql++;
1195
+ while( IsSpace(zSql[0]) ) zSql++;
11691196
}else if( pzErrMsg ){
11701197
*pzErrMsg = save_err_msg(db);
11711198
}
11721199
11731200
/* clear saved stmt handle */
@@ -1266,14 +1293,14 @@
12661293
return 1;
12671294
}
12681295
zSelect = appendText(zSelect, "|| ')' FROM ", 0);
12691296
zSelect = appendText(zSelect, zTable, '"');
12701297
1271
- rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt);
1298
+ rc = run_table_dump_query(p, zSelect, zPrepStmt);
12721299
if( rc==SQLITE_CORRUPT ){
12731300
zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1274
- rc = run_table_dump_query(p->out, p->db, zSelect, 0);
1301
+ run_table_dump_query(p, zSelect, 0);
12751302
}
12761303
if( zSelect ) free(zSelect);
12771304
}
12781305
return 0;
12791306
}
@@ -1285,23 +1312,34 @@
12851312
** If we get a SQLITE_CORRUPT error, rerun the query after appending
12861313
** "ORDER BY rowid DESC" to the end.
12871314
*/
12881315
static int run_schema_dump_query(
12891316
struct callback_data *p,
1290
- const char *zQuery,
1291
- char **pzErrMsg
1317
+ const char *zQuery
12921318
){
12931319
int rc;
1294
- rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
1320
+ char *zErr = 0;
1321
+ rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
12951322
if( rc==SQLITE_CORRUPT ){
12961323
char *zQ2;
12971324
int len = strlen30(zQuery);
1298
- if( pzErrMsg ) sqlite3_free(*pzErrMsg);
1325
+ fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
1326
+ if( zErr ){
1327
+ fprintf(p->out, "/****** %s ******/\n", zErr);
1328
+ sqlite3_free(zErr);
1329
+ zErr = 0;
1330
+ }
12991331
zQ2 = malloc( len+100 );
13001332
if( zQ2==0 ) return rc;
13011333
sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1302
- rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
1334
+ rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
1335
+ if( rc ){
1336
+ fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
1337
+ }else{
1338
+ rc = SQLITE_CORRUPT;
1339
+ }
1340
+ sqlite3_free(zErr);
13031341
free(zQ2);
13041342
}
13051343
return rc;
13061344
}
13071345
@@ -1434,11 +1472,11 @@
14341472
*/
14351473
static int booleanValue(char *zArg){
14361474
int val = atoi(zArg);
14371475
int j;
14381476
for(j=0; zArg[j]; j++){
1439
- zArg[j] = (char)tolower(zArg[j]);
1477
+ zArg[j] = ToLower(zArg[j]);
14401478
}
14411479
if( strcmp(zArg,"on")==0 ){
14421480
val = 1;
14431481
}else if( strcmp(zArg,"yes")==0 ){
14441482
val = 1;
@@ -1460,11 +1498,11 @@
14601498
char *azArg[50];
14611499
14621500
/* Parse the input line into tokens.
14631501
*/
14641502
while( zLine[i] && nArg<ArraySize(azArg) ){
1465
- while( isspace((unsigned char)zLine[i]) ){ i++; }
1503
+ while( IsSpace(zLine[i]) ){ i++; }
14661504
if( zLine[i]==0 ) break;
14671505
if( zLine[i]=='\'' || zLine[i]=='"' ){
14681506
int delim = zLine[i++];
14691507
azArg[nArg++] = &zLine[i];
14701508
while( zLine[i] && zLine[i]!=delim ){ i++; }
@@ -1472,11 +1510,11 @@
14721510
zLine[i++] = 0;
14731511
}
14741512
if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
14751513
}else{
14761514
azArg[nArg++] = &zLine[i];
1477
- while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
1515
+ while( zLine[i] && !IsSpace(zLine[i]) ){ i++; }
14781516
if( zLine[i] ) zLine[i++] = 0;
14791517
resolve_backslashes(azArg[nArg-1]);
14801518
}
14811519
}
14821520
@@ -1543,29 +1581,29 @@
15431581
rc = 1;
15441582
}
15451583
}else
15461584
15471585
if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
1548
- char *zErrMsg = 0;
15491586
open_db(p);
15501587
/* When playing back a "dump", the content might appear in an order
15511588
** which causes immediate foreign key constraints to be violated.
15521589
** So disable foreign-key constraint enforcement to prevent problems. */
15531590
fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
15541591
fprintf(p->out, "BEGIN TRANSACTION;\n");
15551592
p->writableSchema = 0;
1556
- sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0);
1593
+ sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
1594
+ p->nErr = 0;
15571595
if( nArg==1 ){
15581596
run_schema_dump_query(p,
15591597
"SELECT name, type, sql FROM sqlite_master "
1560
- "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0
1598
+ "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
15611599
);
15621600
run_schema_dump_query(p,
15631601
"SELECT name, type, sql FROM sqlite_master "
1564
- "WHERE name=='sqlite_sequence'", 0
1602
+ "WHERE name=='sqlite_sequence'"
15651603
);
1566
- run_table_dump_query(p->out, p->db,
1604
+ run_table_dump_query(p,
15671605
"SELECT sql FROM sqlite_master "
15681606
"WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
15691607
);
15701608
}else{
15711609
int i;
@@ -1572,12 +1610,12 @@
15721610
for(i=1; i<nArg; i++){
15731611
zShellStatic = azArg[i];
15741612
run_schema_dump_query(p,
15751613
"SELECT name, type, sql FROM sqlite_master "
15761614
"WHERE tbl_name LIKE shellstatic() AND type=='table'"
1577
- " AND sql NOT NULL", 0);
1578
- run_table_dump_query(p->out, p->db,
1615
+ " AND sql NOT NULL");
1616
+ run_table_dump_query(p,
15791617
"SELECT sql FROM sqlite_master "
15801618
"WHERE sql NOT NULL"
15811619
" AND type IN ('index','trigger','view')"
15821620
" AND tbl_name LIKE shellstatic()", 0
15831621
);
@@ -1586,17 +1624,13 @@
15861624
}
15871625
if( p->writableSchema ){
15881626
fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
15891627
p->writableSchema = 0;
15901628
}
1591
- sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0);
1592
- if( zErrMsg ){
1593
- fprintf(stderr,"Error: %s\n", zErrMsg);
1594
- sqlite3_free(zErrMsg);
1595
- }else{
1596
- fprintf(p->out, "COMMIT;\n");
1597
- }
1629
+ sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
1630
+ sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
1631
+ fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
15981632
}else
15991633
16001634
if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
16011635
p->echoOn = booleanValue(azArg[1]);
16021636
}else
@@ -1671,11 +1705,11 @@
16711705
nSep = strlen30(p->separator);
16721706
if( nSep==0 ){
16731707
fprintf(stderr, "Error: non-null separator required for import\n");
16741708
return 1;
16751709
}
1676
- zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1710
+ zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16771711
if( zSql==0 ){
16781712
fprintf(stderr, "Error: out of memory\n");
16791713
return 1;
16801714
}
16811715
nByte = strlen30(zSql);
@@ -1693,11 +1727,11 @@
16931727
zSql = malloc( nByte + 20 + nCol*2 );
16941728
if( zSql==0 ){
16951729
fprintf(stderr, "Error: out of memory\n");
16961730
return 1;
16971731
}
1698
- sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1732
+ sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable);
16991733
j = strlen30(zSql);
17001734
for(i=1; i<nCol; i++){
17011735
zSql[j++] = ',';
17021736
zSql[j++] = '?';
17031737
}
@@ -1725,11 +1759,10 @@
17251759
}
17261760
sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
17271761
zCommit = "COMMIT";
17281762
while( (zLine = local_getline(0, in))!=0 ){
17291763
char *z;
1730
- i = 0;
17311764
lineno++;
17321765
azCol[0] = zLine;
17331766
for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
17341767
if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
17351768
*z = 0;
@@ -2014,11 +2047,11 @@
20142047
memcpy(&data, p, sizeof(data));
20152048
data.showHeader = 0;
20162049
data.mode = MODE_Semi;
20172050
if( nArg>1 ){
20182051
int i;
2019
- for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]);
2052
+ for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
20202053
if( strcmp(azArg[1],"sqlite_master")==0 ){
20212054
char *new_argv[2], *new_colv[2];
20222055
new_argv[0] = "CREATE TABLE sqlite_master (\n"
20232056
" type text,\n"
20242057
" name text,\n"
@@ -2200,11 +2233,11 @@
22002233
for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
22012234
if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
22022235
if( testctrl<0 ){
22032236
testctrl = aCtrl[i].ctrlCode;
22042237
}else{
2205
- fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[i]);
2238
+ fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
22062239
testctrl = -1;
22072240
break;
22082241
}
22092242
}
22102243
}
@@ -2337,11 +2370,11 @@
23372370
/*
23382371
** Test to see if a line consists entirely of whitespace.
23392372
*/
23402373
static int _all_whitespace(const char *z){
23412374
for(; *z; z++){
2342
- if( isspace(*(unsigned char*)z) ) continue;
2375
+ if( IsSpace(z[0]) ) continue;
23432376
if( *z=='/' && z[1]=='*' ){
23442377
z += 2;
23452378
while( *z && (*z!='*' || z[1]!='/') ){ z++; }
23462379
if( *z==0 ) return 0;
23472380
z++;
@@ -2362,15 +2395,15 @@
23622395
** Return TRUE if the line typed in is an SQL command terminator other
23632396
** than a semi-colon. The SQL Server style "go" command is understood
23642397
** as is the Oracle "/".
23652398
*/
23662399
static int _is_command_terminator(const char *zLine){
2367
- while( isspace(*(unsigned char*)zLine) ){ zLine++; };
2400
+ while( IsSpace(zLine[0]) ){ zLine++; };
23682401
if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
23692402
return 1; /* Oracle */
23702403
}
2371
- if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
2404
+ if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
23722405
&& _all_whitespace(&zLine[2]) ){
23732406
return 1; /* SQL Server */
23742407
}
23752408
return 0;
23762409
}
@@ -2436,11 +2469,11 @@
24362469
memcpy(zLine,";",2);
24372470
}
24382471
nSqlPrior = nSql;
24392472
if( zSql==0 ){
24402473
int i;
2441
- for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
2474
+ for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
24422475
if( zLine[i]!=0 ){
24432476
nSql = strlen30(zLine);
24442477
zSql = malloc( nSql+3 );
24452478
if( zSql==0 ){
24462479
fprintf(stderr, "Error: out of memory\n");
@@ -2630,10 +2663,13 @@
26302663
" -version show SQLite version\n"
26312664
" -vfs NAME use NAME as the default VFS\n"
26322665
#ifdef SQLITE_ENABLE_VFSTRACE
26332666
" -vfstrace enable tracing of all VFS calls\n"
26342667
#endif
2668
+#ifdef SQLITE_ENABLE_MULTIPLEX
2669
+ " -multiplex enable the multiplexor VFS\n"
2670
+#endif
26352671
;
26362672
static void usage(int showDetail){
26372673
fprintf(stderr,
26382674
"Usage: %s [OPTIONS] FILENAME [SQL]\n"
26392675
"FILENAME is the name of an SQLite database. A new database is created\n"
@@ -2705,10 +2741,11 @@
27052741
** we do the actual processing of arguments later in a second pass.
27062742
*/
27072743
}else if( strcmp(argv[i],"-batch")==0 ){
27082744
stdin_is_interactive = 0;
27092745
}else if( strcmp(argv[i],"-heap")==0 ){
2746
+#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
27102747
int j, c;
27112748
const char *zSize;
27122749
sqlite3_int64 szHeap;
27132750
27142751
zSize = argv[++i];
@@ -2717,11 +2754,10 @@
27172754
if( c=='M' ){ szHeap *= 1000000; break; }
27182755
if( c=='K' ){ szHeap *= 1000; break; }
27192756
if( c=='G' ){ szHeap *= 1000000000; break; }
27202757
}
27212758
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2722
-#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
27232759
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
27242760
#endif
27252761
#ifdef SQLITE_ENABLE_VFSTRACE
27262762
}else if( strcmp(argv[i],"-vfstrace")==0 ){
27272763
extern int vfstrace_register(
@@ -2731,10 +2767,15 @@
27312767
void *pOutArg,
27322768
int makeDefault
27332769
);
27342770
vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
27352771
#endif
2772
+#ifdef SQLITE_ENABLE_MULTIPLEX
2773
+ }else if( strcmp(argv[i],"-multiplex")==0 ){
2774
+ extern int sqlite3_multiple_initialize(const char*,int);
2775
+ sqlite3_multiplex_initialize(0, 1);
2776
+#endif
27362777
}else if( strcmp(argv[i],"-vfs")==0 ){
27372778
sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
27382779
if( pVfs ){
27392780
sqlite3_vfs_register(pVfs, 1);
27402781
}else{
@@ -2753,11 +2794,11 @@
27532794
#ifndef SQLITE_OMIT_MEMORYDB
27542795
data.zDbFilename = ":memory:";
27552796
#else
27562797
data.zDbFilename = 0;
27572798
#endif
2758
- /***** Begin Fossil Patch *****/
2799
+ /***** Begin Fossil Patch *****/
27592800
{
27602801
extern void fossil_open(const char **);
27612802
fossil_open(&data.zDbFilename);
27622803
}
27632804
/***** End Fossil Patch *****/
@@ -2855,12 +2896,18 @@
28552896
stdin_is_interactive = 0;
28562897
}else if( strcmp(z,"-heap")==0 ){
28572898
i++;
28582899
}else if( strcmp(z,"-vfs")==0 ){
28592900
i++;
2901
+#ifdef SQLITE_ENABLE_VFSTRACE
28602902
}else if( strcmp(z,"-vfstrace")==0 ){
28612903
i++;
2904
+#endif
2905
+#ifdef SQLITE_ENABLE_MULTIPLEX
2906
+ }else if( strcmp(z,"-multiplex")==0 ){
2907
+ i++;
2908
+#endif
28622909
}else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
28632910
usage(1);
28642911
}else{
28652912
fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
28662913
fprintf(stderr,"Use -help for a list of options.\n");
28672914
--- src/shell.c
+++ src/shell.c
@@ -10,15 +10,26 @@
10 **
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
14 */
15 #if defined(_WIN32) || defined(WIN32)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
19
 
 
 
 
 
 
 
 
 
 
 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include "sqlite3.h"
@@ -58,11 +69,11 @@
58 #define isatty(h) _isatty(h)
59 #define access(f,m) _access((f),(m))
60 #else
61 /* Make sure isatty() has a prototype.
62 */
63 extern int isatty();
64 #endif
65
66 #if defined(_WIN32_WCE)
67 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
68 * thus we always assume that we have a console. That can be
@@ -71,10 +82,15 @@
71 #define isatty(x) 1
72 #endif
73
74 /* True if the timer is enabled */
75 static int enableTimer = 0;
 
 
 
 
 
76
77 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
78 #include <sys/time.h>
79 #include <sys/resource.h>
80
@@ -263,27 +279,27 @@
263 /*
264 ** Determines if a string is a number of not.
265 */
266 static int isNumber(const char *z, int *realnum){
267 if( *z=='-' || *z=='+' ) z++;
268 if( !isdigit(*z) ){
269 return 0;
270 }
271 z++;
272 if( realnum ) *realnum = 0;
273 while( isdigit(*z) ){ z++; }
274 if( *z=='.' ){
275 z++;
276 if( !isdigit(*z) ) return 0;
277 while( isdigit(*z) ){ z++; }
278 if( realnum ) *realnum = 1;
279 }
280 if( *z=='e' || *z=='E' ){
281 z++;
282 if( *z=='+' || *z=='-' ) z++;
283 if( !isdigit(*z) ) return 0;
284 while( isdigit(*z) ){ z++; }
285 if( realnum ) *realnum = 1;
286 }
287 return *z==0;
288 }
289
@@ -320,22 +336,20 @@
320 */
321 static char *local_getline(char *zPrompt, FILE *in){
322 char *zLine;
323 int nLine;
324 int n;
325 int eol;
326
327 if( zPrompt && *zPrompt ){
328 printf("%s",zPrompt);
329 fflush(stdout);
330 }
331 nLine = 100;
332 zLine = malloc( nLine );
333 if( zLine==0 ) return 0;
334 n = 0;
335 eol = 0;
336 while( !eol ){
337 if( n+100>nLine ){
338 nLine = nLine*2 + 100;
339 zLine = realloc(zLine, nLine);
340 if( zLine==0 ) return 0;
341 }
@@ -343,19 +357,18 @@
343 if( n==0 ){
344 free(zLine);
345 return 0;
346 }
347 zLine[n] = 0;
348 eol = 1;
349 break;
350 }
351 while( zLine[n] ){ n++; }
352 if( n>0 && zLine[n-1]=='\n' ){
353 n--;
354 if( n>0 && zLine[n-1]=='\r' ) n--;
355 zLine[n] = 0;
356 eol = 1;
357 }
358 }
359 zLine = realloc( zLine, n+1 );
360 return zLine;
361 }
@@ -400,10 +413,11 @@
400 sqlite3 *db; /* The database */
401 int echoOn; /* True to echo input commands */
402 int statsOn; /* True to display memory stats before each finalize */
403 int cnt; /* Number of records displayed so far */
404 FILE *out; /* Write results here */
 
405 int mode; /* An output mode setting */
406 int writableSchema; /* True if PRAGMA writable_schema=ON */
407 int showHeader; /* True to show column names in List or Column mode */
408 char *zDestTable; /* Name of destination table when MODE_Insert */
409 char separator[20]; /* Separator character for MODE_List */
@@ -925,31 +939,37 @@
925 **
926 ** This is used, for example, to show the schema of the database by
927 ** querying the SQLITE_MASTER table.
928 */
929 static int run_table_dump_query(
930 FILE *out, /* Send output here */
931 sqlite3 *db, /* Database to query */
932 const char *zSelect, /* SELECT statement to extract content */
933 const char *zFirstRow /* Print before first row, if not NULL */
934 ){
935 sqlite3_stmt *pSelect;
936 int rc;
937 rc = sqlite3_prepare(db, zSelect, -1, &pSelect, 0);
938 if( rc!=SQLITE_OK || !pSelect ){
 
 
939 return rc;
940 }
941 rc = sqlite3_step(pSelect);
942 while( rc==SQLITE_ROW ){
943 if( zFirstRow ){
944 fprintf(out, "%s", zFirstRow);
945 zFirstRow = 0;
946 }
947 fprintf(out, "%s;\n", sqlite3_column_text(pSelect, 0));
948 rc = sqlite3_step(pSelect);
949 }
950 return sqlite3_finalize(pSelect);
 
 
 
 
 
951 }
952
953 /*
954 ** Allocate space and save off current error string.
955 */
@@ -1027,11 +1047,16 @@
1027 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1028 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1029 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1030 iHiwtr = iCur = -1;
1031 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1032 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur);
 
 
 
 
 
1033 iHiwtr = iCur = -1;
1034 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1035 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
1036 iHiwtr = iCur = -1;
1037 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
@@ -1067,10 +1092,11 @@
1067 struct callback_data *pArg, /* Pointer to struct callback_data */
1068 char **pzErrMsg /* Error msg written here */
1069 ){
1070 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1071 int rc = SQLITE_OK; /* Return Code */
 
1072 const char *zLeftover; /* Tail of unprocessed SQL */
1073
1074 if( pzErrMsg ){
1075 *pzErrMsg = NULL;
1076 }
@@ -1083,11 +1109,11 @@
1083 }
1084 }else{
1085 if( !pStmt ){
1086 /* this happens for a comment or white-space */
1087 zSql = zLeftover;
1088 while( isspace(zSql[0]) ) zSql++;
1089 continue;
1090 }
1091
1092 /* save off the prepared statment handle and reset row count */
1093 if( pArg ){
@@ -1160,14 +1186,15 @@
1160 }
1161
1162 /* Finalize the statement just executed. If this fails, save a
1163 ** copy of the error message. Otherwise, set zSql to point to the
1164 ** next statement to execute. */
1165 rc = sqlite3_finalize(pStmt);
 
1166 if( rc==SQLITE_OK ){
1167 zSql = zLeftover;
1168 while( isspace(zSql[0]) ) zSql++;
1169 }else if( pzErrMsg ){
1170 *pzErrMsg = save_err_msg(db);
1171 }
1172
1173 /* clear saved stmt handle */
@@ -1266,14 +1293,14 @@
1266 return 1;
1267 }
1268 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
1269 zSelect = appendText(zSelect, zTable, '"');
1270
1271 rc = run_table_dump_query(p->out, p->db, zSelect, zPrepStmt);
1272 if( rc==SQLITE_CORRUPT ){
1273 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1274 rc = run_table_dump_query(p->out, p->db, zSelect, 0);
1275 }
1276 if( zSelect ) free(zSelect);
1277 }
1278 return 0;
1279 }
@@ -1285,23 +1312,34 @@
1285 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1286 ** "ORDER BY rowid DESC" to the end.
1287 */
1288 static int run_schema_dump_query(
1289 struct callback_data *p,
1290 const char *zQuery,
1291 char **pzErrMsg
1292 ){
1293 int rc;
1294 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, pzErrMsg);
 
1295 if( rc==SQLITE_CORRUPT ){
1296 char *zQ2;
1297 int len = strlen30(zQuery);
1298 if( pzErrMsg ) sqlite3_free(*pzErrMsg);
 
 
 
 
 
1299 zQ2 = malloc( len+100 );
1300 if( zQ2==0 ) return rc;
1301 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1302 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, pzErrMsg);
 
 
 
 
 
 
1303 free(zQ2);
1304 }
1305 return rc;
1306 }
1307
@@ -1434,11 +1472,11 @@
1434 */
1435 static int booleanValue(char *zArg){
1436 int val = atoi(zArg);
1437 int j;
1438 for(j=0; zArg[j]; j++){
1439 zArg[j] = (char)tolower(zArg[j]);
1440 }
1441 if( strcmp(zArg,"on")==0 ){
1442 val = 1;
1443 }else if( strcmp(zArg,"yes")==0 ){
1444 val = 1;
@@ -1460,11 +1498,11 @@
1460 char *azArg[50];
1461
1462 /* Parse the input line into tokens.
1463 */
1464 while( zLine[i] && nArg<ArraySize(azArg) ){
1465 while( isspace((unsigned char)zLine[i]) ){ i++; }
1466 if( zLine[i]==0 ) break;
1467 if( zLine[i]=='\'' || zLine[i]=='"' ){
1468 int delim = zLine[i++];
1469 azArg[nArg++] = &zLine[i];
1470 while( zLine[i] && zLine[i]!=delim ){ i++; }
@@ -1472,11 +1510,11 @@
1472 zLine[i++] = 0;
1473 }
1474 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
1475 }else{
1476 azArg[nArg++] = &zLine[i];
1477 while( zLine[i] && !isspace((unsigned char)zLine[i]) ){ i++; }
1478 if( zLine[i] ) zLine[i++] = 0;
1479 resolve_backslashes(azArg[nArg-1]);
1480 }
1481 }
1482
@@ -1543,29 +1581,29 @@
1543 rc = 1;
1544 }
1545 }else
1546
1547 if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
1548 char *zErrMsg = 0;
1549 open_db(p);
1550 /* When playing back a "dump", the content might appear in an order
1551 ** which causes immediate foreign key constraints to be violated.
1552 ** So disable foreign-key constraint enforcement to prevent problems. */
1553 fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
1554 fprintf(p->out, "BEGIN TRANSACTION;\n");
1555 p->writableSchema = 0;
1556 sqlite3_exec(p->db, "PRAGMA writable_schema=ON", 0, 0, 0);
 
1557 if( nArg==1 ){
1558 run_schema_dump_query(p,
1559 "SELECT name, type, sql FROM sqlite_master "
1560 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'", 0
1561 );
1562 run_schema_dump_query(p,
1563 "SELECT name, type, sql FROM sqlite_master "
1564 "WHERE name=='sqlite_sequence'", 0
1565 );
1566 run_table_dump_query(p->out, p->db,
1567 "SELECT sql FROM sqlite_master "
1568 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
1569 );
1570 }else{
1571 int i;
@@ -1572,12 +1610,12 @@
1572 for(i=1; i<nArg; i++){
1573 zShellStatic = azArg[i];
1574 run_schema_dump_query(p,
1575 "SELECT name, type, sql FROM sqlite_master "
1576 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
1577 " AND sql NOT NULL", 0);
1578 run_table_dump_query(p->out, p->db,
1579 "SELECT sql FROM sqlite_master "
1580 "WHERE sql NOT NULL"
1581 " AND type IN ('index','trigger','view')"
1582 " AND tbl_name LIKE shellstatic()", 0
1583 );
@@ -1586,17 +1624,13 @@
1586 }
1587 if( p->writableSchema ){
1588 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
1589 p->writableSchema = 0;
1590 }
1591 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF", 0, 0, 0);
1592 if( zErrMsg ){
1593 fprintf(stderr,"Error: %s\n", zErrMsg);
1594 sqlite3_free(zErrMsg);
1595 }else{
1596 fprintf(p->out, "COMMIT;\n");
1597 }
1598 }else
1599
1600 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
1601 p->echoOn = booleanValue(azArg[1]);
1602 }else
@@ -1671,11 +1705,11 @@
1671 nSep = strlen30(p->separator);
1672 if( nSep==0 ){
1673 fprintf(stderr, "Error: non-null separator required for import\n");
1674 return 1;
1675 }
1676 zSql = sqlite3_mprintf("SELECT * FROM '%q'", zTable);
1677 if( zSql==0 ){
1678 fprintf(stderr, "Error: out of memory\n");
1679 return 1;
1680 }
1681 nByte = strlen30(zSql);
@@ -1693,11 +1727,11 @@
1693 zSql = malloc( nByte + 20 + nCol*2 );
1694 if( zSql==0 ){
1695 fprintf(stderr, "Error: out of memory\n");
1696 return 1;
1697 }
1698 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO '%q' VALUES(?", zTable);
1699 j = strlen30(zSql);
1700 for(i=1; i<nCol; i++){
1701 zSql[j++] = ',';
1702 zSql[j++] = '?';
1703 }
@@ -1725,11 +1759,10 @@
1725 }
1726 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1727 zCommit = "COMMIT";
1728 while( (zLine = local_getline(0, in))!=0 ){
1729 char *z;
1730 i = 0;
1731 lineno++;
1732 azCol[0] = zLine;
1733 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
1734 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1735 *z = 0;
@@ -2014,11 +2047,11 @@
2014 memcpy(&data, p, sizeof(data));
2015 data.showHeader = 0;
2016 data.mode = MODE_Semi;
2017 if( nArg>1 ){
2018 int i;
2019 for(i=0; azArg[1][i]; i++) azArg[1][i] = (char)tolower(azArg[1][i]);
2020 if( strcmp(azArg[1],"sqlite_master")==0 ){
2021 char *new_argv[2], *new_colv[2];
2022 new_argv[0] = "CREATE TABLE sqlite_master (\n"
2023 " type text,\n"
2024 " name text,\n"
@@ -2200,11 +2233,11 @@
2200 for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
2201 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
2202 if( testctrl<0 ){
2203 testctrl = aCtrl[i].ctrlCode;
2204 }else{
2205 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[i]);
2206 testctrl = -1;
2207 break;
2208 }
2209 }
2210 }
@@ -2337,11 +2370,11 @@
2337 /*
2338 ** Test to see if a line consists entirely of whitespace.
2339 */
2340 static int _all_whitespace(const char *z){
2341 for(; *z; z++){
2342 if( isspace(*(unsigned char*)z) ) continue;
2343 if( *z=='/' && z[1]=='*' ){
2344 z += 2;
2345 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
2346 if( *z==0 ) return 0;
2347 z++;
@@ -2362,15 +2395,15 @@
2362 ** Return TRUE if the line typed in is an SQL command terminator other
2363 ** than a semi-colon. The SQL Server style "go" command is understood
2364 ** as is the Oracle "/".
2365 */
2366 static int _is_command_terminator(const char *zLine){
2367 while( isspace(*(unsigned char*)zLine) ){ zLine++; };
2368 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2369 return 1; /* Oracle */
2370 }
2371 if( tolower(zLine[0])=='g' && tolower(zLine[1])=='o'
2372 && _all_whitespace(&zLine[2]) ){
2373 return 1; /* SQL Server */
2374 }
2375 return 0;
2376 }
@@ -2436,11 +2469,11 @@
2436 memcpy(zLine,";",2);
2437 }
2438 nSqlPrior = nSql;
2439 if( zSql==0 ){
2440 int i;
2441 for(i=0; zLine[i] && isspace((unsigned char)zLine[i]); i++){}
2442 if( zLine[i]!=0 ){
2443 nSql = strlen30(zLine);
2444 zSql = malloc( nSql+3 );
2445 if( zSql==0 ){
2446 fprintf(stderr, "Error: out of memory\n");
@@ -2630,10 +2663,13 @@
2630 " -version show SQLite version\n"
2631 " -vfs NAME use NAME as the default VFS\n"
2632 #ifdef SQLITE_ENABLE_VFSTRACE
2633 " -vfstrace enable tracing of all VFS calls\n"
2634 #endif
 
 
 
2635 ;
2636 static void usage(int showDetail){
2637 fprintf(stderr,
2638 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
2639 "FILENAME is the name of an SQLite database. A new database is created\n"
@@ -2705,10 +2741,11 @@
2705 ** we do the actual processing of arguments later in a second pass.
2706 */
2707 }else if( strcmp(argv[i],"-batch")==0 ){
2708 stdin_is_interactive = 0;
2709 }else if( strcmp(argv[i],"-heap")==0 ){
 
2710 int j, c;
2711 const char *zSize;
2712 sqlite3_int64 szHeap;
2713
2714 zSize = argv[++i];
@@ -2717,11 +2754,10 @@
2717 if( c=='M' ){ szHeap *= 1000000; break; }
2718 if( c=='K' ){ szHeap *= 1000; break; }
2719 if( c=='G' ){ szHeap *= 1000000000; break; }
2720 }
2721 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
2722 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2723 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2724 #endif
2725 #ifdef SQLITE_ENABLE_VFSTRACE
2726 }else if( strcmp(argv[i],"-vfstrace")==0 ){
2727 extern int vfstrace_register(
@@ -2731,10 +2767,15 @@
2731 void *pOutArg,
2732 int makeDefault
2733 );
2734 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
2735 #endif
 
 
 
 
 
2736 }else if( strcmp(argv[i],"-vfs")==0 ){
2737 sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
2738 if( pVfs ){
2739 sqlite3_vfs_register(pVfs, 1);
2740 }else{
@@ -2753,11 +2794,11 @@
2753 #ifndef SQLITE_OMIT_MEMORYDB
2754 data.zDbFilename = ":memory:";
2755 #else
2756 data.zDbFilename = 0;
2757 #endif
2758 /***** Begin Fossil Patch *****/
2759 {
2760 extern void fossil_open(const char **);
2761 fossil_open(&data.zDbFilename);
2762 }
2763 /***** End Fossil Patch *****/
@@ -2855,12 +2896,18 @@
2855 stdin_is_interactive = 0;
2856 }else if( strcmp(z,"-heap")==0 ){
2857 i++;
2858 }else if( strcmp(z,"-vfs")==0 ){
2859 i++;
 
2860 }else if( strcmp(z,"-vfstrace")==0 ){
2861 i++;
 
 
 
 
 
2862 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
2863 usage(1);
2864 }else{
2865 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2866 fprintf(stderr,"Use -help for a list of options.\n");
2867
--- src/shell.c
+++ src/shell.c
@@ -10,15 +10,26 @@
10 **
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
14 */
15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
19
20 /*
21 ** Enable large-file support for fopen() and friends on unix.
22 */
23 #ifndef SQLITE_DISABLE_LFS
24 # define _LARGE_FILE 1
25 # ifndef _FILE_OFFSET_BITS
26 # define _FILE_OFFSET_BITS 64
27 # endif
28 # define _LARGEFILE_SOURCE 1
29 #endif
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <assert.h>
35 #include "sqlite3.h"
@@ -58,11 +69,11 @@
69 #define isatty(h) _isatty(h)
70 #define access(f,m) _access((f),(m))
71 #else
72 /* Make sure isatty() has a prototype.
73 */
74 extern int isatty(int);
75 #endif
76
77 #if defined(_WIN32_WCE)
78 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
79 * thus we always assume that we have a console. That can be
@@ -71,10 +82,15 @@
82 #define isatty(x) 1
83 #endif
84
85 /* True if the timer is enabled */
86 static int enableTimer = 0;
87
88 /* ctype macros that work with signed characters */
89 #define IsSpace(X) isspace((unsigned char)X)
90 #define IsDigit(X) isdigit((unsigned char)X)
91 #define ToLower(X) (char)tolower((unsigned char)X)
92
93 #if !defined(_WIN32) && !defined(WIN32) && !defined(__OS2__) && !defined(__RTP__) && !defined(_WRS_KERNEL)
94 #include <sys/time.h>
95 #include <sys/resource.h>
96
@@ -263,27 +279,27 @@
279 /*
280 ** Determines if a string is a number of not.
281 */
282 static int isNumber(const char *z, int *realnum){
283 if( *z=='-' || *z=='+' ) z++;
284 if( !IsDigit(*z) ){
285 return 0;
286 }
287 z++;
288 if( realnum ) *realnum = 0;
289 while( IsDigit(*z) ){ z++; }
290 if( *z=='.' ){
291 z++;
292 if( !IsDigit(*z) ) return 0;
293 while( IsDigit(*z) ){ z++; }
294 if( realnum ) *realnum = 1;
295 }
296 if( *z=='e' || *z=='E' ){
297 z++;
298 if( *z=='+' || *z=='-' ) z++;
299 if( !IsDigit(*z) ) return 0;
300 while( IsDigit(*z) ){ z++; }
301 if( realnum ) *realnum = 1;
302 }
303 return *z==0;
304 }
305
@@ -320,22 +336,20 @@
336 */
337 static char *local_getline(char *zPrompt, FILE *in){
338 char *zLine;
339 int nLine;
340 int n;
 
341
342 if( zPrompt && *zPrompt ){
343 printf("%s",zPrompt);
344 fflush(stdout);
345 }
346 nLine = 100;
347 zLine = malloc( nLine );
348 if( zLine==0 ) return 0;
349 n = 0;
350 while( 1 ){
 
351 if( n+100>nLine ){
352 nLine = nLine*2 + 100;
353 zLine = realloc(zLine, nLine);
354 if( zLine==0 ) return 0;
355 }
@@ -343,19 +357,18 @@
357 if( n==0 ){
358 free(zLine);
359 return 0;
360 }
361 zLine[n] = 0;
 
362 break;
363 }
364 while( zLine[n] ){ n++; }
365 if( n>0 && zLine[n-1]=='\n' ){
366 n--;
367 if( n>0 && zLine[n-1]=='\r' ) n--;
368 zLine[n] = 0;
369 break;
370 }
371 }
372 zLine = realloc( zLine, n+1 );
373 return zLine;
374 }
@@ -400,10 +413,11 @@
413 sqlite3 *db; /* The database */
414 int echoOn; /* True to echo input commands */
415 int statsOn; /* True to display memory stats before each finalize */
416 int cnt; /* Number of records displayed so far */
417 FILE *out; /* Write results here */
418 int nErr; /* Number of errors seen */
419 int mode; /* An output mode setting */
420 int writableSchema; /* True if PRAGMA writable_schema=ON */
421 int showHeader; /* True to show column names in List or Column mode */
422 char *zDestTable; /* Name of destination table when MODE_Insert */
423 char separator[20]; /* Separator character for MODE_List */
@@ -925,31 +939,37 @@
939 **
940 ** This is used, for example, to show the schema of the database by
941 ** querying the SQLITE_MASTER table.
942 */
943 static int run_table_dump_query(
944 struct callback_data *p, /* Query context */
945 const char *zSelect, /* SELECT statement to extract content */
946 const char *zFirstRow /* Print before first row, if not NULL */
 
947 ){
948 sqlite3_stmt *pSelect;
949 int rc;
950 rc = sqlite3_prepare(p->db, zSelect, -1, &pSelect, 0);
951 if( rc!=SQLITE_OK || !pSelect ){
952 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
953 p->nErr++;
954 return rc;
955 }
956 rc = sqlite3_step(pSelect);
957 while( rc==SQLITE_ROW ){
958 if( zFirstRow ){
959 fprintf(p->out, "%s", zFirstRow);
960 zFirstRow = 0;
961 }
962 fprintf(p->out, "%s;\n", sqlite3_column_text(pSelect, 0));
963 rc = sqlite3_step(pSelect);
964 }
965 rc = sqlite3_finalize(pSelect);
966 if( rc!=SQLITE_OK ){
967 fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
968 p->nErr++;
969 }
970 return rc;
971 }
972
973 /*
974 ** Allocate space and save off current error string.
975 */
@@ -1027,11 +1047,16 @@
1047 fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
1048 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur, &iHiwtr, bReset);
1049 fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
1050 iHiwtr = iCur = -1;
1051 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1052 fprintf(pArg->out, "Pager Heap Usage: %d bytes\n", iCur); iHiwtr = iCur = -1;
1053 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1054 fprintf(pArg->out, "Page cache hits: %d\n", iCur);
1055 iHiwtr = iCur = -1;
1056 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1057 fprintf(pArg->out, "Page cache misses: %d\n", iCur);
1058 iHiwtr = iCur = -1;
1059 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1060 fprintf(pArg->out, "Schema Heap Usage: %d bytes\n", iCur);
1061 iHiwtr = iCur = -1;
1062 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
@@ -1067,10 +1092,11 @@
1092 struct callback_data *pArg, /* Pointer to struct callback_data */
1093 char **pzErrMsg /* Error msg written here */
1094 ){
1095 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
1096 int rc = SQLITE_OK; /* Return Code */
1097 int rc2;
1098 const char *zLeftover; /* Tail of unprocessed SQL */
1099
1100 if( pzErrMsg ){
1101 *pzErrMsg = NULL;
1102 }
@@ -1083,11 +1109,11 @@
1109 }
1110 }else{
1111 if( !pStmt ){
1112 /* this happens for a comment or white-space */
1113 zSql = zLeftover;
1114 while( IsSpace(zSql[0]) ) zSql++;
1115 continue;
1116 }
1117
1118 /* save off the prepared statment handle and reset row count */
1119 if( pArg ){
@@ -1160,14 +1186,15 @@
1186 }
1187
1188 /* Finalize the statement just executed. If this fails, save a
1189 ** copy of the error message. Otherwise, set zSql to point to the
1190 ** next statement to execute. */
1191 rc2 = sqlite3_finalize(pStmt);
1192 if( rc!=SQLITE_NOMEM ) rc = rc2;
1193 if( rc==SQLITE_OK ){
1194 zSql = zLeftover;
1195 while( IsSpace(zSql[0]) ) zSql++;
1196 }else if( pzErrMsg ){
1197 *pzErrMsg = save_err_msg(db);
1198 }
1199
1200 /* clear saved stmt handle */
@@ -1266,14 +1293,14 @@
1293 return 1;
1294 }
1295 zSelect = appendText(zSelect, "|| ')' FROM ", 0);
1296 zSelect = appendText(zSelect, zTable, '"');
1297
1298 rc = run_table_dump_query(p, zSelect, zPrepStmt);
1299 if( rc==SQLITE_CORRUPT ){
1300 zSelect = appendText(zSelect, " ORDER BY rowid DESC", 0);
1301 run_table_dump_query(p, zSelect, 0);
1302 }
1303 if( zSelect ) free(zSelect);
1304 }
1305 return 0;
1306 }
@@ -1285,23 +1312,34 @@
1312 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
1313 ** "ORDER BY rowid DESC" to the end.
1314 */
1315 static int run_schema_dump_query(
1316 struct callback_data *p,
1317 const char *zQuery
 
1318 ){
1319 int rc;
1320 char *zErr = 0;
1321 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
1322 if( rc==SQLITE_CORRUPT ){
1323 char *zQ2;
1324 int len = strlen30(zQuery);
1325 fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
1326 if( zErr ){
1327 fprintf(p->out, "/****** %s ******/\n", zErr);
1328 sqlite3_free(zErr);
1329 zErr = 0;
1330 }
1331 zQ2 = malloc( len+100 );
1332 if( zQ2==0 ) return rc;
1333 sqlite3_snprintf(sizeof(zQ2), zQ2, "%s ORDER BY rowid DESC", zQuery);
1334 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
1335 if( rc ){
1336 fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
1337 }else{
1338 rc = SQLITE_CORRUPT;
1339 }
1340 sqlite3_free(zErr);
1341 free(zQ2);
1342 }
1343 return rc;
1344 }
1345
@@ -1434,11 +1472,11 @@
1472 */
1473 static int booleanValue(char *zArg){
1474 int val = atoi(zArg);
1475 int j;
1476 for(j=0; zArg[j]; j++){
1477 zArg[j] = ToLower(zArg[j]);
1478 }
1479 if( strcmp(zArg,"on")==0 ){
1480 val = 1;
1481 }else if( strcmp(zArg,"yes")==0 ){
1482 val = 1;
@@ -1460,11 +1498,11 @@
1498 char *azArg[50];
1499
1500 /* Parse the input line into tokens.
1501 */
1502 while( zLine[i] && nArg<ArraySize(azArg) ){
1503 while( IsSpace(zLine[i]) ){ i++; }
1504 if( zLine[i]==0 ) break;
1505 if( zLine[i]=='\'' || zLine[i]=='"' ){
1506 int delim = zLine[i++];
1507 azArg[nArg++] = &zLine[i];
1508 while( zLine[i] && zLine[i]!=delim ){ i++; }
@@ -1472,11 +1510,11 @@
1510 zLine[i++] = 0;
1511 }
1512 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
1513 }else{
1514 azArg[nArg++] = &zLine[i];
1515 while( zLine[i] && !IsSpace(zLine[i]) ){ i++; }
1516 if( zLine[i] ) zLine[i++] = 0;
1517 resolve_backslashes(azArg[nArg-1]);
1518 }
1519 }
1520
@@ -1543,29 +1581,29 @@
1581 rc = 1;
1582 }
1583 }else
1584
1585 if( c=='d' && strncmp(azArg[0], "dump", n)==0 && nArg<3 ){
 
1586 open_db(p);
1587 /* When playing back a "dump", the content might appear in an order
1588 ** which causes immediate foreign key constraints to be violated.
1589 ** So disable foreign-key constraint enforcement to prevent problems. */
1590 fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
1591 fprintf(p->out, "BEGIN TRANSACTION;\n");
1592 p->writableSchema = 0;
1593 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
1594 p->nErr = 0;
1595 if( nArg==1 ){
1596 run_schema_dump_query(p,
1597 "SELECT name, type, sql FROM sqlite_master "
1598 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
1599 );
1600 run_schema_dump_query(p,
1601 "SELECT name, type, sql FROM sqlite_master "
1602 "WHERE name=='sqlite_sequence'"
1603 );
1604 run_table_dump_query(p,
1605 "SELECT sql FROM sqlite_master "
1606 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
1607 );
1608 }else{
1609 int i;
@@ -1572,12 +1610,12 @@
1610 for(i=1; i<nArg; i++){
1611 zShellStatic = azArg[i];
1612 run_schema_dump_query(p,
1613 "SELECT name, type, sql FROM sqlite_master "
1614 "WHERE tbl_name LIKE shellstatic() AND type=='table'"
1615 " AND sql NOT NULL");
1616 run_table_dump_query(p,
1617 "SELECT sql FROM sqlite_master "
1618 "WHERE sql NOT NULL"
1619 " AND type IN ('index','trigger','view')"
1620 " AND tbl_name LIKE shellstatic()", 0
1621 );
@@ -1586,17 +1624,13 @@
1624 }
1625 if( p->writableSchema ){
1626 fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
1627 p->writableSchema = 0;
1628 }
1629 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
1630 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
1631 fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
 
 
 
 
1632 }else
1633
1634 if( c=='e' && strncmp(azArg[0], "echo", n)==0 && nArg>1 && nArg<3 ){
1635 p->echoOn = booleanValue(azArg[1]);
1636 }else
@@ -1671,11 +1705,11 @@
1705 nSep = strlen30(p->separator);
1706 if( nSep==0 ){
1707 fprintf(stderr, "Error: non-null separator required for import\n");
1708 return 1;
1709 }
1710 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
1711 if( zSql==0 ){
1712 fprintf(stderr, "Error: out of memory\n");
1713 return 1;
1714 }
1715 nByte = strlen30(zSql);
@@ -1693,11 +1727,11 @@
1727 zSql = malloc( nByte + 20 + nCol*2 );
1728 if( zSql==0 ){
1729 fprintf(stderr, "Error: out of memory\n");
1730 return 1;
1731 }
1732 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zTable);
1733 j = strlen30(zSql);
1734 for(i=1; i<nCol; i++){
1735 zSql[j++] = ',';
1736 zSql[j++] = '?';
1737 }
@@ -1725,11 +1759,10 @@
1759 }
1760 sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
1761 zCommit = "COMMIT";
1762 while( (zLine = local_getline(0, in))!=0 ){
1763 char *z;
 
1764 lineno++;
1765 azCol[0] = zLine;
1766 for(i=0, z=zLine; *z && *z!='\n' && *z!='\r'; z++){
1767 if( *z==p->separator[0] && strncmp(z, p->separator, nSep)==0 ){
1768 *z = 0;
@@ -2014,11 +2047,11 @@
2047 memcpy(&data, p, sizeof(data));
2048 data.showHeader = 0;
2049 data.mode = MODE_Semi;
2050 if( nArg>1 ){
2051 int i;
2052 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
2053 if( strcmp(azArg[1],"sqlite_master")==0 ){
2054 char *new_argv[2], *new_colv[2];
2055 new_argv[0] = "CREATE TABLE sqlite_master (\n"
2056 " type text,\n"
2057 " name text,\n"
@@ -2200,11 +2233,11 @@
2233 for(i=0; i<(int)(sizeof(aCtrl)/sizeof(aCtrl[0])); i++){
2234 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n)==0 ){
2235 if( testctrl<0 ){
2236 testctrl = aCtrl[i].ctrlCode;
2237 }else{
2238 fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
2239 testctrl = -1;
2240 break;
2241 }
2242 }
2243 }
@@ -2337,11 +2370,11 @@
2370 /*
2371 ** Test to see if a line consists entirely of whitespace.
2372 */
2373 static int _all_whitespace(const char *z){
2374 for(; *z; z++){
2375 if( IsSpace(z[0]) ) continue;
2376 if( *z=='/' && z[1]=='*' ){
2377 z += 2;
2378 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
2379 if( *z==0 ) return 0;
2380 z++;
@@ -2362,15 +2395,15 @@
2395 ** Return TRUE if the line typed in is an SQL command terminator other
2396 ** than a semi-colon. The SQL Server style "go" command is understood
2397 ** as is the Oracle "/".
2398 */
2399 static int _is_command_terminator(const char *zLine){
2400 while( IsSpace(zLine[0]) ){ zLine++; };
2401 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
2402 return 1; /* Oracle */
2403 }
2404 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
2405 && _all_whitespace(&zLine[2]) ){
2406 return 1; /* SQL Server */
2407 }
2408 return 0;
2409 }
@@ -2436,11 +2469,11 @@
2469 memcpy(zLine,";",2);
2470 }
2471 nSqlPrior = nSql;
2472 if( zSql==0 ){
2473 int i;
2474 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
2475 if( zLine[i]!=0 ){
2476 nSql = strlen30(zLine);
2477 zSql = malloc( nSql+3 );
2478 if( zSql==0 ){
2479 fprintf(stderr, "Error: out of memory\n");
@@ -2630,10 +2663,13 @@
2663 " -version show SQLite version\n"
2664 " -vfs NAME use NAME as the default VFS\n"
2665 #ifdef SQLITE_ENABLE_VFSTRACE
2666 " -vfstrace enable tracing of all VFS calls\n"
2667 #endif
2668 #ifdef SQLITE_ENABLE_MULTIPLEX
2669 " -multiplex enable the multiplexor VFS\n"
2670 #endif
2671 ;
2672 static void usage(int showDetail){
2673 fprintf(stderr,
2674 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
2675 "FILENAME is the name of an SQLite database. A new database is created\n"
@@ -2705,10 +2741,11 @@
2741 ** we do the actual processing of arguments later in a second pass.
2742 */
2743 }else if( strcmp(argv[i],"-batch")==0 ){
2744 stdin_is_interactive = 0;
2745 }else if( strcmp(argv[i],"-heap")==0 ){
2746 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
2747 int j, c;
2748 const char *zSize;
2749 sqlite3_int64 szHeap;
2750
2751 zSize = argv[++i];
@@ -2717,11 +2754,10 @@
2754 if( c=='M' ){ szHeap *= 1000000; break; }
2755 if( c=='K' ){ szHeap *= 1000; break; }
2756 if( c=='G' ){ szHeap *= 1000000000; break; }
2757 }
2758 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
 
2759 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
2760 #endif
2761 #ifdef SQLITE_ENABLE_VFSTRACE
2762 }else if( strcmp(argv[i],"-vfstrace")==0 ){
2763 extern int vfstrace_register(
@@ -2731,10 +2767,15 @@
2767 void *pOutArg,
2768 int makeDefault
2769 );
2770 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
2771 #endif
2772 #ifdef SQLITE_ENABLE_MULTIPLEX
2773 }else if( strcmp(argv[i],"-multiplex")==0 ){
2774 extern int sqlite3_multiple_initialize(const char*,int);
2775 sqlite3_multiplex_initialize(0, 1);
2776 #endif
2777 }else if( strcmp(argv[i],"-vfs")==0 ){
2778 sqlite3_vfs *pVfs = sqlite3_vfs_find(argv[++i]);
2779 if( pVfs ){
2780 sqlite3_vfs_register(pVfs, 1);
2781 }else{
@@ -2753,11 +2794,11 @@
2794 #ifndef SQLITE_OMIT_MEMORYDB
2795 data.zDbFilename = ":memory:";
2796 #else
2797 data.zDbFilename = 0;
2798 #endif
2799 /***** Begin Fossil Patch *****/
2800 {
2801 extern void fossil_open(const char **);
2802 fossil_open(&data.zDbFilename);
2803 }
2804 /***** End Fossil Patch *****/
@@ -2855,12 +2896,18 @@
2896 stdin_is_interactive = 0;
2897 }else if( strcmp(z,"-heap")==0 ){
2898 i++;
2899 }else if( strcmp(z,"-vfs")==0 ){
2900 i++;
2901 #ifdef SQLITE_ENABLE_VFSTRACE
2902 }else if( strcmp(z,"-vfstrace")==0 ){
2903 i++;
2904 #endif
2905 #ifdef SQLITE_ENABLE_MULTIPLEX
2906 }else if( strcmp(z,"-multiplex")==0 ){
2907 i++;
2908 #endif
2909 }else if( strcmp(z,"-help")==0 || strcmp(z, "--help")==0 ){
2910 usage(1);
2911 }else{
2912 fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
2913 fprintf(stderr,"Use -help for a list of options.\n");
2914
+205 -1
--- src/skins.c
+++ src/skins.c
@@ -152,10 +152,55 @@
152152
@ /* The label/value pairs on (for example) the vinfo page */
153153
@ table.label-value th {
154154
@ vertical-align: top;
155155
@ text-align: right;
156156
@ padding: 0.2ex 2ex;
157
+@ }
158
+@
159
+@ /* Side-by-side diff */
160
+@ table.sbsdiff {
161
+@ background-color: white;
162
+@ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
163
+@ font-size: 8pt;
164
+@ border-collapse:collapse;
165
+@ white-space: pre;
166
+@ width: 98%;
167
+@ border: 1px #000 dashed;
168
+@ }
169
+@
170
+@ table.sbsdiff th.diffhdr {
171
+@ border-bottom: dotted;
172
+@ border-width: 1px;
173
+@ }
174
+@
175
+@ table.sbsdiff tr td {
176
+@ white-space: pre;
177
+@ padding-left: 3px;
178
+@ padding-right: 3px;
179
+@ margin: 0px;
180
+@ }
181
+@
182
+@ table.sbsdiff tr td.lineno {
183
+@ text-align: right;
184
+@ }
185
+@
186
+@ table.sbsdiff tr td.meta {
187
+@ color: white;
188
+@ background-color: rgb(20, 20, 20);
189
+@ text-align: center;
190
+@ }
191
+@
192
+@ table.sbsdiff tr td.added {
193
+@ background-color: rgb(230, 230, 230);
194
+@ }
195
+@
196
+@ table.sbsdiff tr td.removed {
197
+@ background-color: rgb(200, 200, 200);
198
+@ }
199
+@
200
+@ table.sbsdiff tr td.changed {
201
+@ background-color: rgb(220, 220, 220);
157202
@ }');
158203
@ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
159204
@ <head>
160205
@ <title>$<project_name>: $<title></title>
161206
@ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -356,11 +401,54 @@
356401
@ table.label-value th {
357402
@ vertical-align: top;
358403
@ text-align: right;
359404
@ padding: 0.2ex 2ex;
360405
@ }
361
-@ ');
406
+@
407
+@ /* Side-by-side diff */
408
+@ table.sbsdiff {
409
+@ background-color: #ffffc5;
410
+@ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
411
+@ font-size: 8pt;
412
+@ border-collapse:collapse;
413
+@ white-space: pre;
414
+@ width: 98%;
415
+@ border: 1px #000 dashed;
416
+@ }
417
+@
418
+@ table.sbsdiff th.diffhdr {
419
+@ border-bottom: dotted;
420
+@ border-width: 1px;
421
+@ }
422
+@
423
+@ table.sbsdiff tr td {
424
+@ white-space: pre;
425
+@ padding-left: 3px;
426
+@ padding-right: 3px;
427
+@ margin: 0px;
428
+@ }
429
+@
430
+@ table.sbsdiff tr td.lineno {
431
+@ text-align: right;
432
+@ }
433
+@
434
+@ table.sbsdiff tr td.meta {
435
+@ background-color: #a09048;
436
+@ text-align: center;
437
+@ }
438
+@
439
+@ table.sbsdiff tr td.added {
440
+@ background-color: rgb(210, 210, 100);
441
+@ }
442
+@
443
+@ table.sbsdiff tr td.removed {
444
+@ background-color: rgb(190, 200, 110);
445
+@ }
446
+@
447
+@ table.sbsdiff tr td.changed {
448
+@ background-color: rgb(200, 210, 120);
449
+@ }');
362450
@ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
363451
@ <head>
364452
@ <title>$<project_name>: $<title></title>
365453
@ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
366454
@ href="$home/timeline.rss">
@@ -590,10 +678,56 @@
590678
@ /* The label/value pairs on (for example) the ci page */
591679
@ table.label-value th {
592680
@ vertical-align: top;
593681
@ text-align: right;
594682
@ padding: 0.2ex 2ex;
683
+@ }
684
+@
685
+@ /* Side-by-side diff */
686
+@ table.sbsdiff {
687
+@ background-color: white;
688
+@ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
689
+@ font-size: 6pt;
690
+@ border-collapse:collapse;
691
+@ white-space: pre;
692
+@ width: 98%;
693
+@ border: 1px #000 dashed;
694
+@ }
695
+@
696
+@ table.sbsdiff th.diffhdr {
697
+@ border-bottom: dotted;
698
+@ border-width: 1px;
699
+@ }
700
+@
701
+@ table.sbsdiff tr td {
702
+@ white-space: pre;
703
+@ padding-left: 3px;
704
+@ padding-right: 3px;
705
+@ margin: 0px;
706
+@ }
707
+@
708
+@ table.sbsdiff tr td.lineno {
709
+@ text-align: right;
710
+@ }
711
+@
712
+@ table.sbsdiff tr td.meta {
713
+@ color: white;
714
+@ background-color: black;
715
+@ text-align: center;
716
+@ }
717
+@
718
+@ table.sbsdiff tr td.added {
719
+@ background-color: white;
720
+@ }
721
+@
722
+@ table.sbsdiff tr td.removed {
723
+@ background-color: white;
724
+@ text-decoration: line-through;
725
+@ }
726
+@
727
+@ table.sbsdiff tr td.changed {
728
+@ background-color: white;
595729
@ }');
596730
@ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
597731
@ <head>
598732
@ <title>$<project_name>: $<title></title>
599733
@ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -885,10 +1019,77 @@
8851019
@ padding: 3px 5px;
8861020
@ }
8871021
@
8881022
@ textarea {
8891023
@ font-size: 1em;
1024
+@ }
1025
+@
1026
+@ /* Side-by-side diff */
1027
+@ table.sbsdiff {
1028
+@ background-color: white;
1029
+@ font-family: Dejavu Sans Mono, Monaco, Lucida Console, monospace;
1030
+@ font-size: 6pt;
1031
+@ border-collapse:collapse;
1032
+@ width: 98%;
1033
+@ border: 1px #000 dashed;
1034
+@ margin-left: auto;
1035
+@ margin-right: auto;
1036
+@ }
1037
+@
1038
+@ table.sbsdiff th.diffhdr {
1039
+@ border-bottom: dotted;
1040
+@ border-width: 1px;
1041
+@ }
1042
+@
1043
+@ table.sbsdiff tr td {
1044
+@ padding-left: 3px;
1045
+@ padding-right: 3px;
1046
+@ margin: 0px;
1047
+@ vertical-align: top;
1048
+@ white-space: pre-wrap;
1049
+@ }
1050
+@
1051
+@ table.sbsdiff tr td.lineno {
1052
+@ text-align: right;
1053
+@ /* border-bottom: 1px solid rgb(220, 220, 220); */
1054
+@ }
1055
+@
1056
+@ table.sbsdiff tr td.srcline {
1057
+@ /* max-width: 400px; */
1058
+@ /* Note: May partially hide long lines without whitespaces */
1059
+@ /* overflow: hidden; */
1060
+@ /* border-bottom: 1px solid rgb(220, 220, 220); */
1061
+@ }
1062
+@
1063
+@ table.sbsdiff tr td.meta {
1064
+@ background-color: rgb(170, 160, 255);
1065
+@ padding-top: 0.25em;
1066
+@ padding-bottom: 0.25em;
1067
+@ text-align: center;
1068
+@ -moz-border-radius: 5px;
1069
+@ -moz-border-radius: 5px;
1070
+@ -webkit-border-radius: 5px;
1071
+@ -webkit-border-radius: 5px;
1072
+@ -border-radius: 5px;
1073
+@ -border-radius: 5px;
1074
+@ border-radius: 5px;
1075
+@ border-radius: 5px;
1076
+@ }
1077
+@
1078
+@ table.sbsdiff tr td.added {
1079
+@ background-color: rgb(180, 250, 180);
1080
+@ /* border-bottom: 1px solid rgb(160, 230, 160); */
1081
+@ }
1082
+@
1083
+@ table.sbsdiff tr td.removed {
1084
+@ background-color: rgb(250, 130, 130);
1085
+@ /* border-bottom: 1px solid rgb(230, 110, 110); */
1086
+@ }
1087
+@
1088
+@ table.sbsdiff tr td.changed {
1089
+@ background-color: rgb(210, 210, 200);
1090
+@ /* border-bottom: 1px solid rgb(190, 190, 180); */
8901091
@ }');
8911092
@ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
8921093
@ <head>
8931094
@ <title>$<project_name>: $<title></title>
8941095
@ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -1100,10 +1301,13 @@
11001301
db_multi_exec("%s", zCurrent);
11011302
}
11021303
}
11031304
11041305
style_header("Skins");
1306
+ if( zErr ){
1307
+ @ <p><font color="red">%h(zErr)</font></p>
1308
+ }
11051309
@ <p>A "skin" is a combination of
11061310
@ <a href="setup_editcss">CSS</a>,
11071311
@ <a href="setup_header">Header</a>,
11081312
@ <a href="setup_footer">Footer</a>, and
11091313
@ <a href="setup_logo">Logo</a> that determines the look and feel
11101314
--- src/skins.c
+++ src/skins.c
@@ -152,10 +152,55 @@
152 @ /* The label/value pairs on (for example) the vinfo page */
153 @ table.label-value th {
154 @ vertical-align: top;
155 @ text-align: right;
156 @ padding: 0.2ex 2ex;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157 @ }');
158 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
159 @ <head>
160 @ <title>$<project_name>: $<title></title>
161 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -356,11 +401,54 @@
356 @ table.label-value th {
357 @ vertical-align: top;
358 @ text-align: right;
359 @ padding: 0.2ex 2ex;
360 @ }
361 @ ');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
363 @ <head>
364 @ <title>$<project_name>: $<title></title>
365 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
366 @ href="$home/timeline.rss">
@@ -590,10 +678,56 @@
590 @ /* The label/value pairs on (for example) the ci page */
591 @ table.label-value th {
592 @ vertical-align: top;
593 @ text-align: right;
594 @ padding: 0.2ex 2ex;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
595 @ }');
596 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
597 @ <head>
598 @ <title>$<project_name>: $<title></title>
599 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -885,10 +1019,77 @@
885 @ padding: 3px 5px;
886 @ }
887 @
888 @ textarea {
889 @ font-size: 1em;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
890 @ }');
891 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
892 @ <head>
893 @ <title>$<project_name>: $<title></title>
894 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -1100,10 +1301,13 @@
1100 db_multi_exec("%s", zCurrent);
1101 }
1102 }
1103
1104 style_header("Skins");
 
 
 
1105 @ <p>A "skin" is a combination of
1106 @ <a href="setup_editcss">CSS</a>,
1107 @ <a href="setup_header">Header</a>,
1108 @ <a href="setup_footer">Footer</a>, and
1109 @ <a href="setup_logo">Logo</a> that determines the look and feel
1110
--- src/skins.c
+++ src/skins.c
@@ -152,10 +152,55 @@
152 @ /* The label/value pairs on (for example) the vinfo page */
153 @ table.label-value th {
154 @ vertical-align: top;
155 @ text-align: right;
156 @ padding: 0.2ex 2ex;
157 @ }
158 @
159 @ /* Side-by-side diff */
160 @ table.sbsdiff {
161 @ background-color: white;
162 @ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
163 @ font-size: 8pt;
164 @ border-collapse:collapse;
165 @ white-space: pre;
166 @ width: 98%;
167 @ border: 1px #000 dashed;
168 @ }
169 @
170 @ table.sbsdiff th.diffhdr {
171 @ border-bottom: dotted;
172 @ border-width: 1px;
173 @ }
174 @
175 @ table.sbsdiff tr td {
176 @ white-space: pre;
177 @ padding-left: 3px;
178 @ padding-right: 3px;
179 @ margin: 0px;
180 @ }
181 @
182 @ table.sbsdiff tr td.lineno {
183 @ text-align: right;
184 @ }
185 @
186 @ table.sbsdiff tr td.meta {
187 @ color: white;
188 @ background-color: rgb(20, 20, 20);
189 @ text-align: center;
190 @ }
191 @
192 @ table.sbsdiff tr td.added {
193 @ background-color: rgb(230, 230, 230);
194 @ }
195 @
196 @ table.sbsdiff tr td.removed {
197 @ background-color: rgb(200, 200, 200);
198 @ }
199 @
200 @ table.sbsdiff tr td.changed {
201 @ background-color: rgb(220, 220, 220);
202 @ }');
203 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
204 @ <head>
205 @ <title>$<project_name>: $<title></title>
206 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -356,11 +401,54 @@
401 @ table.label-value th {
402 @ vertical-align: top;
403 @ text-align: right;
404 @ padding: 0.2ex 2ex;
405 @ }
406 @
407 @ /* Side-by-side diff */
408 @ table.sbsdiff {
409 @ background-color: #ffffc5;
410 @ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
411 @ font-size: 8pt;
412 @ border-collapse:collapse;
413 @ white-space: pre;
414 @ width: 98%;
415 @ border: 1px #000 dashed;
416 @ }
417 @
418 @ table.sbsdiff th.diffhdr {
419 @ border-bottom: dotted;
420 @ border-width: 1px;
421 @ }
422 @
423 @ table.sbsdiff tr td {
424 @ white-space: pre;
425 @ padding-left: 3px;
426 @ padding-right: 3px;
427 @ margin: 0px;
428 @ }
429 @
430 @ table.sbsdiff tr td.lineno {
431 @ text-align: right;
432 @ }
433 @
434 @ table.sbsdiff tr td.meta {
435 @ background-color: #a09048;
436 @ text-align: center;
437 @ }
438 @
439 @ table.sbsdiff tr td.added {
440 @ background-color: rgb(210, 210, 100);
441 @ }
442 @
443 @ table.sbsdiff tr td.removed {
444 @ background-color: rgb(190, 200, 110);
445 @ }
446 @
447 @ table.sbsdiff tr td.changed {
448 @ background-color: rgb(200, 210, 120);
449 @ }');
450 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
451 @ <head>
452 @ <title>$<project_name>: $<title></title>
453 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
454 @ href="$home/timeline.rss">
@@ -590,10 +678,56 @@
678 @ /* The label/value pairs on (for example) the ci page */
679 @ table.label-value th {
680 @ vertical-align: top;
681 @ text-align: right;
682 @ padding: 0.2ex 2ex;
683 @ }
684 @
685 @ /* Side-by-side diff */
686 @ table.sbsdiff {
687 @ background-color: white;
688 @ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
689 @ font-size: 6pt;
690 @ border-collapse:collapse;
691 @ white-space: pre;
692 @ width: 98%;
693 @ border: 1px #000 dashed;
694 @ }
695 @
696 @ table.sbsdiff th.diffhdr {
697 @ border-bottom: dotted;
698 @ border-width: 1px;
699 @ }
700 @
701 @ table.sbsdiff tr td {
702 @ white-space: pre;
703 @ padding-left: 3px;
704 @ padding-right: 3px;
705 @ margin: 0px;
706 @ }
707 @
708 @ table.sbsdiff tr td.lineno {
709 @ text-align: right;
710 @ }
711 @
712 @ table.sbsdiff tr td.meta {
713 @ color: white;
714 @ background-color: black;
715 @ text-align: center;
716 @ }
717 @
718 @ table.sbsdiff tr td.added {
719 @ background-color: white;
720 @ }
721 @
722 @ table.sbsdiff tr td.removed {
723 @ background-color: white;
724 @ text-decoration: line-through;
725 @ }
726 @
727 @ table.sbsdiff tr td.changed {
728 @ background-color: white;
729 @ }');
730 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
731 @ <head>
732 @ <title>$<project_name>: $<title></title>
733 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -885,10 +1019,77 @@
1019 @ padding: 3px 5px;
1020 @ }
1021 @
1022 @ textarea {
1023 @ font-size: 1em;
1024 @ }
1025 @
1026 @ /* Side-by-side diff */
1027 @ table.sbsdiff {
1028 @ background-color: white;
1029 @ font-family: Dejavu Sans Mono, Monaco, Lucida Console, monospace;
1030 @ font-size: 6pt;
1031 @ border-collapse:collapse;
1032 @ width: 98%;
1033 @ border: 1px #000 dashed;
1034 @ margin-left: auto;
1035 @ margin-right: auto;
1036 @ }
1037 @
1038 @ table.sbsdiff th.diffhdr {
1039 @ border-bottom: dotted;
1040 @ border-width: 1px;
1041 @ }
1042 @
1043 @ table.sbsdiff tr td {
1044 @ padding-left: 3px;
1045 @ padding-right: 3px;
1046 @ margin: 0px;
1047 @ vertical-align: top;
1048 @ white-space: pre-wrap;
1049 @ }
1050 @
1051 @ table.sbsdiff tr td.lineno {
1052 @ text-align: right;
1053 @ /* border-bottom: 1px solid rgb(220, 220, 220); */
1054 @ }
1055 @
1056 @ table.sbsdiff tr td.srcline {
1057 @ /* max-width: 400px; */
1058 @ /* Note: May partially hide long lines without whitespaces */
1059 @ /* overflow: hidden; */
1060 @ /* border-bottom: 1px solid rgb(220, 220, 220); */
1061 @ }
1062 @
1063 @ table.sbsdiff tr td.meta {
1064 @ background-color: rgb(170, 160, 255);
1065 @ padding-top: 0.25em;
1066 @ padding-bottom: 0.25em;
1067 @ text-align: center;
1068 @ -moz-border-radius: 5px;
1069 @ -moz-border-radius: 5px;
1070 @ -webkit-border-radius: 5px;
1071 @ -webkit-border-radius: 5px;
1072 @ -border-radius: 5px;
1073 @ -border-radius: 5px;
1074 @ border-radius: 5px;
1075 @ border-radius: 5px;
1076 @ }
1077 @
1078 @ table.sbsdiff tr td.added {
1079 @ background-color: rgb(180, 250, 180);
1080 @ /* border-bottom: 1px solid rgb(160, 230, 160); */
1081 @ }
1082 @
1083 @ table.sbsdiff tr td.removed {
1084 @ background-color: rgb(250, 130, 130);
1085 @ /* border-bottom: 1px solid rgb(230, 110, 110); */
1086 @ }
1087 @
1088 @ table.sbsdiff tr td.changed {
1089 @ background-color: rgb(210, 210, 200);
1090 @ /* border-bottom: 1px solid rgb(190, 190, 180); */
1091 @ }');
1092 @ REPLACE INTO config(name,mtime,value) VALUES('header',now(),'<html>
1093 @ <head>
1094 @ <title>$<project_name>: $<title></title>
1095 @ <link rel="alternate" type="application/rss+xml" title="RSS Feed"
@@ -1100,10 +1301,13 @@
1301 db_multi_exec("%s", zCurrent);
1302 }
1303 }
1304
1305 style_header("Skins");
1306 if( zErr ){
1307 @ <p><font color="red">%h(zErr)</font></p>
1308 }
1309 @ <p>A "skin" is a combination of
1310 @ <a href="setup_editcss">CSS</a>,
1311 @ <a href="setup_header">Header</a>,
1312 @ <a href="setup_footer">Footer</a>, and
1313 @ <a href="setup_logo">Logo</a> that determines the look and feel
1314
+1454 -1081
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
11
/******************************************************************************
22
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.7.8. By combining all the individual C code files into this
3
+** version 3.7.9. 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.
@@ -394,11 +394,11 @@
394394
** assert() macro is enabled, each call into the Win32 native heap subsystem
395395
** will cause HeapValidate to be called. If heap validation should fail, an
396396
** assertion will be triggered.
397397
**
398398
** (Historical note: There used to be several other options, but we've
399
-** pared it down to just these two.)
399
+** pared it down to just these three.)
400400
**
401401
** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402402
** the default.
403403
*/
404404
#if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
@@ -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.7.8"
660
-#define SQLITE_VERSION_NUMBER 3007008
661
-#define SQLITE_SOURCE_ID "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
659
+#define SQLITE_VERSION "3.7.9"
660
+#define SQLITE_VERSION_NUMBER 3007009
661
+#define SQLITE_SOURCE_ID "2011-10-15 00:16:30 39408702a989f907261c298bf0947f3e68bd10fe"
662662
663663
/*
664664
** CAPI3REF: Run-Time Library Version Numbers
665665
** KEYWORDS: sqlite3_version, sqlite3_sourceid
666666
**
@@ -1318,11 +1318,15 @@
13181318
** in order for the database to be readable. The fourth parameter to
13191319
** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
13201320
** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
13211321
** WAL mode. If the integer is -1, then it is overwritten with the current
13221322
** WAL persistence setting.
1323
-**
1323
+**
1324
+** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1325
+** a write transaction to indicate that, unless it is rolled back for some
1326
+** reason, the entire database file will be overwritten by the current
1327
+** transaction. This is used by VACUUM operations.
13241328
*/
13251329
#define SQLITE_FCNTL_LOCKSTATE 1
13261330
#define SQLITE_GET_LOCKPROXYFILE 2
13271331
#define SQLITE_SET_LOCKPROXYFILE 3
13281332
#define SQLITE_LAST_ERRNO 4
@@ -1330,10 +1334,11 @@
13301334
#define SQLITE_FCNTL_CHUNK_SIZE 6
13311335
#define SQLITE_FCNTL_FILE_POINTER 7
13321336
#define SQLITE_FCNTL_SYNC_OMITTED 8
13331337
#define SQLITE_FCNTL_WIN32_AV_RETRY 9
13341338
#define SQLITE_FCNTL_PERSIST_WAL 10
1339
+#define SQLITE_FCNTL_OVERWRITE 11
13351340
13361341
/*
13371342
** CAPI3REF: Mutex Handle
13381343
**
13391344
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3346,11 +3351,12 @@
33463351
** zSql string ends at either the first '\000' or '\u0000' character or
33473352
** the nByte-th byte, whichever comes first. If the caller knows
33483353
** that the supplied string is nul-terminated, then there is a small
33493354
** performance advantage to be gained by passing an nByte parameter that
33503355
** is equal to the number of bytes in the input string <i>including</i>
3351
-** the nul-terminator bytes.
3356
+** the nul-terminator bytes as this saves SQLite from having to
3357
+** make a copy of the input string.
33523358
**
33533359
** ^If pzTail is not NULL then *pzTail is made to point to the first byte
33543360
** past the end of the first SQL statement in zSql. These routines only
33553361
** compile the first statement in zSql, so *pzTail is left pointing to
33563362
** what remains uncompiled.
@@ -3397,11 +3403,11 @@
33973403
** a schema change, on the first [sqlite3_step()] call following any change
33983404
** to the [sqlite3_bind_text | bindings] of that [parameter].
33993405
** ^The specific value of WHERE-clause [parameter] might influence the
34003406
** choice of query plan if the parameter is the left-hand side of a [LIKE]
34013407
** or [GLOB] operator or if the parameter is compared to an indexed column
3402
-** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3408
+** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
34033409
** the
34043410
** </li>
34053411
** </ol>
34063412
*/
34073413
SQLITE_API int sqlite3_prepare(
@@ -3567,10 +3573,17 @@
35673573
** ^(In those routines that have a fourth argument, its value is the
35683574
** number of bytes in the parameter. To be clear: the value is the
35693575
** number of <u>bytes</u> in the value, not the number of characters.)^
35703576
** ^If the fourth parameter is negative, the length of the string is
35713577
** the number of bytes up to the first zero terminator.
3578
+** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3579
+** or sqlite3_bind_text16() then that parameter must be the byte offset
3580
+** where the NUL terminator would occur assuming the string were NUL
3581
+** terminated. If any NUL characters occur at byte offsets less than
3582
+** the value of the fourth parameter then the resulting string value will
3583
+** contain embedded NULs. The result of expressions involving strings
3584
+** with embedded NULs is undefined.
35723585
**
35733586
** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
35743587
** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
35753588
** string after SQLite has finished with it. ^The destructor is called
35763589
** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3900,10 +3913,16 @@
39003913
** current row of the result set of [prepared statement] P.
39013914
** ^If prepared statement P does not have results ready to return
39023915
** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
39033916
** interfaces) then sqlite3_data_count(P) returns 0.
39043917
** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3918
+** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3919
+** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
3920
+** will return non-zero if previous call to [sqlite3_step](P) returned
3921
+** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3922
+** where it always returns zero since each step of that multi-step
3923
+** pragma returns 0 columns of data.
39053924
**
39063925
** See also: [sqlite3_column_count()]
39073926
*/
39083927
SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
39093928
@@ -4579,11 +4598,16 @@
45794598
** is negative, then SQLite takes result text from the 2nd parameter
45804599
** through the first zero character.
45814600
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
45824601
** is non-negative, then as many bytes (not characters) of the text
45834602
** pointed to by the 2nd parameter are taken as the application-defined
4584
-** function result.
4603
+** function result. If the 3rd parameter is non-negative, then it
4604
+** must be the byte offset into the string where the NUL terminator would
4605
+** appear if the string where NUL terminated. If any NUL characters occur
4606
+** in the string at a byte offset that is less than the value of the 3rd
4607
+** parameter, then the resulting string will contain embedded NULs and the
4608
+** result of expressions operating on strings with embedded NULs is undefined.
45854609
** ^If the 4th parameter to the sqlite3_result_text* interfaces
45864610
** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
45874611
** function as the destructor on the text or BLOB result when it has
45884612
** finished using that result.
45894613
** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -6362,20 +6386,34 @@
63626386
** <dd>This parameter returns the approximate number of of bytes of heap
63636387
** and lookaside memory used by all prepared statements associated with
63646388
** the database connection.)^
63656389
** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
63666390
** </dd>
6391
+**
6392
+** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
6393
+** <dd>This parameter returns the number of pager cache hits that have
6394
+** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
6395
+** is always 0.
6396
+** </dd>
6397
+**
6398
+** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
6399
+** <dd>This parameter returns the number of pager cache misses that have
6400
+** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
6401
+** is always 0.
6402
+** </dd>
63676403
** </dl>
63686404
*/
63696405
#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
63706406
#define SQLITE_DBSTATUS_CACHE_USED 1
63716407
#define SQLITE_DBSTATUS_SCHEMA_USED 2
63726408
#define SQLITE_DBSTATUS_STMT_USED 3
63736409
#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
63746410
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
63756411
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
6376
-#define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */
6412
+#define SQLITE_DBSTATUS_CACHE_HIT 7
6413
+#define SQLITE_DBSTATUS_CACHE_MISS 8
6414
+#define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
63776415
63786416
63796417
/*
63806418
** CAPI3REF: Prepared Statement Status
63816419
**
@@ -6425,11 +6463,10 @@
64256463
** <dd>^This is the number of rows inserted into transient indices that
64266464
** were created automatically in order to help joins run faster.
64276465
** A non-zero value in this counter may indicate an opportunity to
64286466
** improvement performance by adding permanent indices that do not
64296467
** need to be reinitialized each time the statement is run.</dd>
6430
-**
64316468
** </dl>
64326469
*/
64336470
#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
64346471
#define SQLITE_STMTSTATUS_SORT 2
64356472
#define SQLITE_STMTSTATUS_AUTOINDEX 3
@@ -7711,10 +7748,22 @@
77117748
** is 0x00000000ffffffff. But because of quirks of some compilers, we
77127749
** have to specify the value in the less intuitive manner shown:
77137750
*/
77147751
#define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
77157752
7753
+/*
7754
+** The datatype used to store estimates of the number of rows in a
7755
+** table or index. This is an unsigned integer type. For 99.9% of
7756
+** the world, a 32-bit integer is sufficient. But a 64-bit integer
7757
+** can be used at compile-time if desired.
7758
+*/
7759
+#ifdef SQLITE_64BIT_STATS
7760
+ typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
7761
+#else
7762
+ typedef u32 tRowcnt; /* 32-bit is the default */
7763
+#endif
7764
+
77167765
/*
77177766
** Macros to determine whether the machine is big or little endian,
77187767
** evaluated at runtime.
77197768
*/
77207769
#ifdef SQLITE_AMALGAMATION
@@ -8742,10 +8791,11 @@
87428791
SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
87438792
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
87448793
SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
87458794
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
87468795
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8796
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
87478797
87488798
/* Functions used to truncate the database file. */
87498799
SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
87508800
87518801
#if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9278,18 +9328,21 @@
92789328
/*
92799329
** If this is a no-op implementation, implement everything as macros.
92809330
*/
92819331
#define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
92829332
#define sqlite3_mutex_free(X)
9283
-#define sqlite3_mutex_enter(X)
9333
+#define sqlite3_mutex_enter(X)
92849334
#define sqlite3_mutex_try(X) SQLITE_OK
9285
-#define sqlite3_mutex_leave(X)
9335
+#define sqlite3_mutex_leave(X)
92869336
#define sqlite3_mutex_held(X) ((void)(X),1)
92879337
#define sqlite3_mutex_notheld(X) ((void)(X),1)
92889338
#define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
92899339
#define sqlite3MutexInit() SQLITE_OK
92909340
#define sqlite3MutexEnd()
9341
+#define MUTEX_LOGIC(X)
9342
+#else
9343
+#define MUTEX_LOGIC(X) X
92919344
#endif /* defined(SQLITE_MUTEX_OMIT) */
92929345
92939346
/************** End of mutex.h ***********************************************/
92949347
/************** Continuing where we left off in sqliteInt.h ******************/
92959348
@@ -9918,11 +9971,11 @@
99189971
int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
99199972
int nCol; /* Number of columns in this table */
99209973
Column *aCol; /* Information about each column */
99219974
Index *pIndex; /* List of SQL indexes on this table. */
99229975
int tnum; /* Root BTree node for this table (see note above) */
9923
- unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9976
+ tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
99249977
Select *pSelect; /* NULL for tables. Points to definition if a view. */
99259978
u16 nRef; /* Number of pointers to this Table */
99269979
u8 tabFlags; /* Mask of TF_* values */
99279980
u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
99289981
FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10117,11 +10170,11 @@
1011710170
*/
1011810171
struct Index {
1011910172
char *zName; /* Name of this index */
1012010173
int nColumn; /* Number of columns in the table used by this index */
1012110174
int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10122
- unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10175
+ tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
1012310176
Table *pTable; /* The SQL table being indexed */
1012410177
int tnum; /* Page containing root of this index in database file */
1012510178
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1012610179
u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
1012710180
u8 bUnordered; /* Use this index for == or IN queries only */
@@ -10128,24 +10181,32 @@
1012810181
char *zColAff; /* String defining the affinity of each column */
1012910182
Index *pNext; /* The next index associated with the same table */
1013010183
Schema *pSchema; /* Schema containing this index */
1013110184
u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
1013210185
char **azColl; /* Array of collation sequence names for index */
10133
- IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
10186
+#ifdef SQLITE_ENABLE_STAT3
10187
+ int nSample; /* Number of elements in aSample[] */
10188
+ tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10189
+ IndexSample *aSample; /* Samples of the left-most key */
10190
+#endif
1013410191
};
1013510192
1013610193
/*
1013710194
** Each sample stored in the sqlite_stat2 table is represented in memory
1013810195
** using a structure of this type.
1013910196
*/
1014010197
struct IndexSample {
1014110198
union {
1014210199
char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10143
- double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
10200
+ double r; /* Value if eType is SQLITE_FLOAT */
10201
+ i64 i; /* Value if eType is SQLITE_INTEGER */
1014410202
} u;
1014510203
u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10146
- u8 nByte; /* Size in byte of text or blob. */
10204
+ int nByte; /* Size in byte of text or blob. */
10205
+ tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10206
+ tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10207
+ tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
1014710208
};
1014810209
1014910210
/*
1015010211
** Each token coming out of the lexer is an instance of
1015110212
** this structure. Tokens are also used as part of an expression.
@@ -10593,14 +10654,14 @@
1059310654
#define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
1059410655
#define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
1059510656
#define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
1059610657
#define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
1059710658
#define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
10598
-#define WHERE_OMIT_OPEN 0x0010 /* Table cursors are already open */
10599
-#define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */
10600
-#define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */
10601
-#define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */
10659
+#define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
10660
+#define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
10661
+#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
10662
+#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
1060210663
1060310664
/*
1060410665
** The WHERE clause processing routine has two halves. The
1060510666
** first part does the start of the WHERE loop and the second
1060610667
** half does the tail of the WHERE loop. An instance of
@@ -11350,10 +11411,11 @@
1135011411
#else
1135111412
# define sqlite3ViewGetColumnNames(A,B) 0
1135211413
#endif
1135311414
1135411415
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11416
+SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
1135511417
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
1135611418
#ifndef SQLITE_OMIT_AUTOINCREMENT
1135711419
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
1135811420
SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
1135911421
#else
@@ -11606,11 +11668,11 @@
1160611668
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
1160711669
void(*)(void*));
1160811670
SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
1160911671
SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
1161011672
SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11611
-#ifdef SQLITE_ENABLE_STAT2
11673
+#ifdef SQLITE_ENABLE_STAT3
1161211674
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
1161311675
#endif
1161411676
SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
1161511677
SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
1161611678
#ifndef SQLITE_AMALGAMATION
@@ -11708,19 +11770,21 @@
1170811770
# define sqlite3VtabInSync(db) 0
1170911771
# define sqlite3VtabLock(X)
1171011772
# define sqlite3VtabUnlock(X)
1171111773
# define sqlite3VtabUnlockList(X)
1171211774
# define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
11775
+# define sqlite3GetVTable(X,Y) ((VTable*)0)
1171311776
#else
1171411777
SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
1171511778
SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
1171611779
SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
1171711780
SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
1171811781
SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
1171911782
SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
1172011783
SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
1172111784
SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
11785
+SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
1172211786
# define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
1172311787
#endif
1172411788
SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
1172511789
SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
1172611790
SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
@@ -11736,11 +11800,10 @@
1173611800
SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
1173711801
SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
1173811802
SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
1173911803
SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
1174011804
SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11741
-SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
1174211805
SQLITE_PRIVATE const char *sqlite3JournalModename(int);
1174311806
SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
1174411807
SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
1174511808
1174611809
/* Declarations for functions in fkey.c. All of these are replaced by
@@ -12232,10 +12295,13 @@
1223212295
#ifdef SQLITE_ENABLE_RTREE
1223312296
"ENABLE_RTREE",
1223412297
#endif
1223512298
#ifdef SQLITE_ENABLE_STAT2
1223612299
"ENABLE_STAT2",
12300
+#endif
12301
+#ifdef SQLITE_ENABLE_STAT3
12302
+ "ENABLE_STAT3",
1223712303
#endif
1223812304
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
1223912305
"ENABLE_UNLOCK_NOTIFY",
1224012306
#endif
1224112307
#ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
@@ -12445,13 +12511,10 @@
1244512511
"OMIT_WSD",
1244612512
#endif
1244712513
#ifdef SQLITE_OMIT_XFER_OPT
1244812514
"OMIT_XFER_OPT",
1244912515
#endif
12450
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
12451
- "PAGECACHE_BLOCKALLOC",
12452
-#endif
1245312516
#ifdef SQLITE_PERFORMANCE_TRACE
1245412517
"PERFORMANCE_TRACE",
1245512518
#endif
1245612519
#ifdef SQLITE_PROXY_DEBUG
1245712520
"PROXY_DEBUG",
@@ -13189,10 +13252,32 @@
1318913252
*pHighwater = 0;
1319013253
*pCurrent = nByte;
1319113254
1319213255
break;
1319313256
}
13257
+
13258
+ /*
13259
+ ** Set *pCurrent to the total cache hits or misses encountered by all
13260
+ ** pagers the database handle is connected to. *pHighwater is always set
13261
+ ** to zero.
13262
+ */
13263
+ case SQLITE_DBSTATUS_CACHE_HIT:
13264
+ case SQLITE_DBSTATUS_CACHE_MISS: {
13265
+ int i;
13266
+ int nRet = 0;
13267
+ assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
13268
+
13269
+ for(i=0; i<db->nDb; i++){
13270
+ if( db->aDb[i].pBt ){
13271
+ Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
13272
+ sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
13273
+ }
13274
+ }
13275
+ *pHighwater = 0;
13276
+ *pCurrent = nRet;
13277
+ break;
13278
+ }
1319413279
1319513280
default: {
1319613281
rc = SQLITE_ERROR;
1319713282
}
1319813283
}
@@ -13490,16 +13575,22 @@
1349013575
}
1349113576
return 0;
1349213577
}
1349313578
1349413579
/*
13495
-** Set the time to the current time reported by the VFS
13580
+** Set the time to the current time reported by the VFS.
13581
+**
13582
+** Return the number of errors.
1349613583
*/
13497
-static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13584
+static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
1349813585
sqlite3 *db = sqlite3_context_db_handle(context);
13499
- sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
13500
- p->validJD = 1;
13586
+ if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
13587
+ p->validJD = 1;
13588
+ return 0;
13589
+ }else{
13590
+ return 1;
13591
+ }
1350113592
}
1350213593
1350313594
/*
1350413595
** Attempt to parse the given string into a Julian Day Number. Return
1350513596
** the number of errors.
@@ -13525,12 +13616,11 @@
1352513616
if( parseYyyyMmDd(zDate,p)==0 ){
1352613617
return 0;
1352713618
}else if( parseHhMmSs(zDate, p)==0 ){
1352813619
return 0;
1352913620
}else if( sqlite3StrICmp(zDate,"now")==0){
13530
- setDateTimeToCurrent(context, p);
13531
- return 0;
13621
+ return setDateTimeToCurrent(context, p);
1353213622
}else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
1353313623
p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
1353413624
p->validJD = 1;
1353513625
return 0;
1353613626
}
@@ -13953,12 +14043,13 @@
1395314043
int i;
1395414044
const unsigned char *z;
1395514045
int eType;
1395614046
memset(p, 0, sizeof(*p));
1395714047
if( argc==0 ){
13958
- setDateTimeToCurrent(context, p);
13959
- }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
14048
+ return setDateTimeToCurrent(context, p);
14049
+ }
14050
+ if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
1396014051
|| eType==SQLITE_INTEGER ){
1396114052
p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
1396214053
p->validJD = 1;
1396314054
}else{
1396414055
z = sqlite3_value_text(argv[0]);
@@ -14266,35 +14357,32 @@
1426614357
){
1426714358
time_t t;
1426814359
char *zFormat = (char *)sqlite3_user_data(context);
1426914360
sqlite3 *db;
1427014361
sqlite3_int64 iT;
14362
+ struct tm *pTm;
14363
+ struct tm sNow;
1427114364
char zBuf[20];
1427214365
1427314366
UNUSED_PARAMETER(argc);
1427414367
UNUSED_PARAMETER(argv);
1427514368
1427614369
db = sqlite3_context_db_handle(context);
14277
- sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
14370
+ if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
1427814371
t = iT/1000 - 10000*(sqlite3_int64)21086676;
1427914372
#ifdef HAVE_GMTIME_R
14280
- {
14281
- struct tm sNow;
14282
- gmtime_r(&t, &sNow);
14373
+ pTm = gmtime_r(&t, &sNow);
14374
+#else
14375
+ sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14376
+ pTm = gmtime(&t);
14377
+ if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
14378
+ sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14379
+#endif
14380
+ if( pTm ){
1428314381
strftime(zBuf, 20, zFormat, &sNow);
14284
- }
14285
-#else
14286
- {
14287
- struct tm *pTm;
14288
- sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14289
- pTm = gmtime(&t);
14290
- strftime(zBuf, 20, zFormat, pTm);
14291
- sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14292
- }
14293
-#endif
14294
-
14295
- sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14382
+ sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14383
+ }
1429614384
}
1429714385
#endif
1429814386
1429914387
/*
1430014388
** This function registered all of the above C functions as SQL
@@ -14625,16 +14713,16 @@
1462514713
** Register a VFS with the system. It is harmless to register the same
1462614714
** VFS multiple times. The new VFS becomes the default if makeDflt is
1462714715
** true.
1462814716
*/
1462914717
SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14630
- sqlite3_mutex *mutex = 0;
14718
+ MUTEX_LOGIC(sqlite3_mutex *mutex;)
1463114719
#ifndef SQLITE_OMIT_AUTOINIT
1463214720
int rc = sqlite3_initialize();
1463314721
if( rc ) return rc;
1463414722
#endif
14635
- mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14723
+ MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
1463614724
sqlite3_mutex_enter(mutex);
1463714725
vfsUnlink(pVfs);
1463814726
if( makeDflt || vfsList==0 ){
1463914727
pVfs->pNext = vfsList;
1464014728
vfsList = pVfs;
@@ -18878,52 +18966,14 @@
1887818966
** an historical reference. Most of the "enhancements" have been backed
1887918967
** out so that the functionality is now the same as standard printf().
1888018968
**
1888118969
**************************************************************************
1888218970
**
18883
-** The following modules is an enhanced replacement for the "printf" subroutines
18884
-** found in the standard C library. The following enhancements are
18885
-** supported:
18886
-**
18887
-** + Additional functions. The standard set of "printf" functions
18888
-** includes printf, fprintf, sprintf, vprintf, vfprintf, and
18889
-** vsprintf. This module adds the following:
18890
-**
18891
-** * snprintf -- Works like sprintf, but has an extra argument
18892
-** which is the size of the buffer written to.
18893
-**
18894
-** * mprintf -- Similar to sprintf. Writes output to memory
18895
-** obtained from malloc.
18896
-**
18897
-** * xprintf -- Calls a function to dispose of output.
18898
-**
18899
-** * nprintf -- No output, but returns the number of characters
18900
-** that would have been output by printf.
18901
-**
18902
-** * A v- version (ex: vsnprintf) of every function is also
18903
-** supplied.
18904
-**
18905
-** + A few extensions to the formatting notation are supported:
18906
-**
18907
-** * The "=" flag (similar to "-") causes the output to be
18908
-** be centered in the appropriately sized field.
18909
-**
18910
-** * The %b field outputs an integer in binary notation.
18911
-**
18912
-** * The %c field now accepts a precision. The character output
18913
-** is repeated by the number of times the precision specifies.
18914
-**
18915
-** * The %' field works like %c, but takes as its character the
18916
-** next character of the format string, instead of the next
18917
-** argument. For example, printf("%.78'-") prints 78 minus
18918
-** signs, the same as printf("%.78c",'-').
18919
-**
18920
-** + When compiled using GCC on a SPARC, this version of printf is
18921
-** faster than the library printf for SUN OS 4.1.
18922
-**
18923
-** + All functions are fully reentrant.
18924
-**
18971
+** This file contains code for a set of "printf"-like routines. These
18972
+** routines format strings much like the printf() from the standard C
18973
+** library, though the implementation here has enhancements to support
18974
+** SQLlite.
1892518975
*/
1892618976
1892718977
/*
1892818978
** Conversion types fall into various categories as defined by the
1892918979
** following enumeration.
@@ -19057,47 +19107,19 @@
1905719107
}
1905819108
}
1905919109
1906019110
/*
1906119111
** On machines with a small stack size, you can redefine the
19062
-** SQLITE_PRINT_BUF_SIZE to be less than 350.
19112
+** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
1906319113
*/
1906419114
#ifndef SQLITE_PRINT_BUF_SIZE
19065
-# if defined(SQLITE_SMALL_STACK)
19066
-# define SQLITE_PRINT_BUF_SIZE 50
19067
-# else
19068
-# define SQLITE_PRINT_BUF_SIZE 350
19069
-# endif
19115
+# define SQLITE_PRINT_BUF_SIZE 70
1907019116
#endif
1907119117
#define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
1907219118
1907319119
/*
19074
-** The root program. All variations call this core.
19075
-**
19076
-** INPUTS:
19077
-** func This is a pointer to a function taking three arguments
19078
-** 1. A pointer to anything. Same as the "arg" parameter.
19079
-** 2. A pointer to the list of characters to be output
19080
-** (Note, this list is NOT null terminated.)
19081
-** 3. An integer number of characters to be output.
19082
-** (Note: This number might be zero.)
19083
-**
19084
-** arg This is the pointer to anything which will be passed as the
19085
-** first argument to "func". Use it for whatever you like.
19086
-**
19087
-** fmt This is the format string, as in the usual print.
19088
-**
19089
-** ap This is a pointer to a list of arguments. Same as in
19090
-** vfprint.
19091
-**
19092
-** OUTPUTS:
19093
-** The return value is the total number of characters sent to
19094
-** the function "func". Returns -1 on a error.
19095
-**
19096
-** Note that the order in which automatic variables are declared below
19097
-** seems to make a big difference in determining how fast this beast
19098
-** will run.
19120
+** Render a string given by "fmt" into the StrAccum object.
1909919121
*/
1910019122
SQLITE_PRIVATE void sqlite3VXPrintf(
1910119123
StrAccum *pAccum, /* Accumulate results here */
1910219124
int useExtended, /* Allow extended %-conversions */
1910319125
const char *fmt, /* Format string */
@@ -19116,27 +19138,27 @@
1911619138
etByte flag_altform2; /* True if "!" flag is present */
1911719139
etByte flag_zeropad; /* True if field width constant starts with zero */
1911819140
etByte flag_long; /* True if "l" flag is present */
1911919141
etByte flag_longlong; /* True if the "ll" flag is present */
1912019142
etByte done; /* Loop termination flag */
19143
+ etByte xtype = 0; /* Conversion paradigm */
19144
+ char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
1912119145
sqlite_uint64 longvalue; /* Value for integer types */
1912219146
LONGDOUBLE_TYPE realvalue; /* Value for real types */
1912319147
const et_info *infop; /* Pointer to the appropriate info structure */
19124
- char buf[etBUFSIZE]; /* Conversion buffer */
19125
- char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
19126
- etByte xtype = 0; /* Conversion paradigm */
19127
- char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
19148
+ char *zOut; /* Rendering buffer */
19149
+ int nOut; /* Size of the rendering buffer */
19150
+ char *zExtra; /* Malloced memory used by some conversion */
1912819151
#ifndef SQLITE_OMIT_FLOATING_POINT
1912919152
int exp, e2; /* exponent of real numbers */
19153
+ int nsd; /* Number of significant digits returned */
1913019154
double rounder; /* Used for rounding floating point values */
1913119155
etByte flag_dp; /* True if decimal point should be shown */
1913219156
etByte flag_rtz; /* True if trailing zeros should be removed */
19133
- etByte flag_exp; /* True to force display of the exponent */
19134
- int nsd; /* Number of significant digits returned */
1913519157
#endif
19158
+ char buf[etBUFSIZE]; /* Conversion buffer */
1913619159
19137
- length = 0;
1913819160
bufpt = 0;
1913919161
for(; (c=(*fmt))!=0; ++fmt){
1914019162
if( c!='%' ){
1914119163
int amt;
1914219164
bufpt = (char *)fmt;
@@ -19177,13 +19199,10 @@
1917719199
while( c>='0' && c<='9' ){
1917819200
width = width*10 + c - '0';
1917919201
c = *++fmt;
1918019202
}
1918119203
}
19182
- if( width > etBUFSIZE-10 ){
19183
- width = etBUFSIZE-10;
19184
- }
1918519204
/* Get the precision */
1918619205
if( c=='.' ){
1918719206
precision = 0;
1918819207
c = *++fmt;
1918919208
if( c=='*' ){
@@ -19226,16 +19245,10 @@
1922619245
break;
1922719246
}
1922819247
}
1922919248
zExtra = 0;
1923019249
19231
-
19232
- /* Limit the precision to prevent overflowing buf[] during conversion */
19233
- if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
19234
- precision = etBUFSIZE-40;
19235
- }
19236
-
1923719250
/*
1923819251
** At this point, variables are initialized as follows:
1923919252
**
1924019253
** flag_alternateform TRUE if a '#' is present.
1924119254
** flag_altform2 TRUE if a '!' is present.
@@ -19296,20 +19309,30 @@
1929619309
}
1929719310
if( longvalue==0 ) flag_alternateform = 0;
1929819311
if( flag_zeropad && precision<width-(prefix!=0) ){
1929919312
precision = width-(prefix!=0);
1930019313
}
19301
- bufpt = &buf[etBUFSIZE-1];
19314
+ if( precision<etBUFSIZE-10 ){
19315
+ nOut = etBUFSIZE;
19316
+ zOut = buf;
19317
+ }else{
19318
+ nOut = precision + 10;
19319
+ zOut = zExtra = sqlite3Malloc( nOut );
19320
+ if( zOut==0 ){
19321
+ pAccum->mallocFailed = 1;
19322
+ return;
19323
+ }
19324
+ }
19325
+ bufpt = &zOut[nOut-1];
1930219326
if( xtype==etORDINAL ){
1930319327
static const char zOrd[] = "thstndrd";
1930419328
int x = (int)(longvalue % 10);
1930519329
if( x>=4 || (longvalue/10)%10==1 ){
1930619330
x = 0;
1930719331
}
19308
- buf[etBUFSIZE-3] = zOrd[x*2];
19309
- buf[etBUFSIZE-2] = zOrd[x*2+1];
19310
- bufpt -= 2;
19332
+ *(--bufpt) = zOrd[x*2+1];
19333
+ *(--bufpt) = zOrd[x*2];
1931119334
}
1931219335
{
1931319336
register const char *cset; /* Use registers for speed */
1931419337
register int base;
1931519338
cset = &aDigits[infop->charset];
@@ -19317,11 +19340,11 @@
1931719340
do{ /* Convert to ascii */
1931819341
*(--bufpt) = cset[longvalue%base];
1931919342
longvalue = longvalue/base;
1932019343
}while( longvalue>0 );
1932119344
}
19322
- length = (int)(&buf[etBUFSIZE-1]-bufpt);
19345
+ length = (int)(&zOut[nOut-1]-bufpt);
1932319346
for(idx=precision-length; idx>0; idx--){
1932419347
*(--bufpt) = '0'; /* Zero pad */
1932519348
}
1932619349
if( prefix ) *(--bufpt) = prefix; /* Add sign */
1932719350
if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
@@ -19328,21 +19351,20 @@
1932819351
const char *pre;
1932919352
char x;
1933019353
pre = &aPrefix[infop->prefix];
1933119354
for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
1933219355
}
19333
- length = (int)(&buf[etBUFSIZE-1]-bufpt);
19356
+ length = (int)(&zOut[nOut-1]-bufpt);
1933419357
break;
1933519358
case etFLOAT:
1933619359
case etEXP:
1933719360
case etGENERIC:
1933819361
realvalue = va_arg(ap,double);
1933919362
#ifdef SQLITE_OMIT_FLOATING_POINT
1934019363
length = 0;
1934119364
#else
1934219365
if( precision<0 ) precision = 6; /* Set default precision */
19343
- if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
1934419366
if( realvalue<0.0 ){
1934519367
realvalue = -realvalue;
1934619368
prefix = '-';
1934719369
}else{
1934819370
if( flag_plussign ) prefix = '+';
@@ -19386,11 +19408,10 @@
1938619408
bufpt = buf;
1938719409
/*
1938819410
** If the field type is etGENERIC, then convert to either etEXP
1938919411
** or etFLOAT, as appropriate.
1939019412
*/
19391
- flag_exp = xtype==etEXP;
1939219413
if( xtype!=etFLOAT ){
1939319414
realvalue += rounder;
1939419415
if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
1939519416
}
1939619417
if( xtype==etGENERIC ){
@@ -19407,10 +19428,18 @@
1940719428
if( xtype==etEXP ){
1940819429
e2 = 0;
1940919430
}else{
1941019431
e2 = exp;
1941119432
}
19433
+ if( e2+precision+width > etBUFSIZE - 15 ){
19434
+ bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
19435
+ if( bufpt==0 ){
19436
+ pAccum->mallocFailed = 1;
19437
+ return;
19438
+ }
19439
+ }
19440
+ zOut = bufpt;
1941219441
nsd = 0;
1941319442
flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
1941419443
/* The sign in front of the number */
1941519444
if( prefix ){
1941619445
*(bufpt++) = prefix;
@@ -19438,21 +19467,21 @@
1943819467
*(bufpt++) = et_getdigit(&realvalue,&nsd);
1943919468
}
1944019469
/* Remove trailing zeros and the "." if no digits follow the "." */
1944119470
if( flag_rtz && flag_dp ){
1944219471
while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19443
- assert( bufpt>buf );
19472
+ assert( bufpt>zOut );
1944419473
if( bufpt[-1]=='.' ){
1944519474
if( flag_altform2 ){
1944619475
*(bufpt++) = '0';
1944719476
}else{
1944819477
*(--bufpt) = 0;
1944919478
}
1945019479
}
1945119480
}
1945219481
/* Add the "eNNN" suffix */
19453
- if( flag_exp || xtype==etEXP ){
19482
+ if( xtype==etEXP ){
1945419483
*(bufpt++) = aDigits[infop->charset];
1945519484
if( exp<0 ){
1945619485
*(bufpt++) = '-'; exp = -exp;
1945719486
}else{
1945819487
*(bufpt++) = '+';
@@ -19467,12 +19496,12 @@
1946719496
*bufpt = 0;
1946819497
1946919498
/* The converted number is in buf[] and zero terminated. Output it.
1947019499
** Note that the number is in the usual order, not reversed as with
1947119500
** integer conversions. */
19472
- length = (int)(bufpt-buf);
19473
- bufpt = buf;
19501
+ length = (int)(bufpt-zOut);
19502
+ bufpt = zOut;
1947419503
1947519504
/* Special case: Add leading zeros if the flag_zeropad flag is
1947619505
** set and we are not left justified */
1947719506
if( flag_zeropad && !flag_leftjustify && length < width){
1947819507
int i;
@@ -19606,13 +19635,11 @@
1960619635
nspace = width-length;
1960719636
if( nspace>0 ){
1960819637
appendSpace(pAccum, nspace);
1960919638
}
1961019639
}
19611
- if( zExtra ){
19612
- sqlite3_free(zExtra);
19613
- }
19640
+ sqlite3_free(zExtra);
1961419641
}/* End for loop over the format string */
1961519642
} /* End of function */
1961619643
1961719644
/*
1961819645
** Append N bytes of text from z to the StrAccum object.
@@ -19622,10 +19649,11 @@
1962219649
if( p->tooBig | p->mallocFailed ){
1962319650
testcase(p->tooBig);
1962419651
testcase(p->mallocFailed);
1962519652
return;
1962619653
}
19654
+ assert( p->zText!=0 || p->nChar==0 );
1962719655
if( N<0 ){
1962819656
N = sqlite3Strlen30(z);
1962919657
}
1963019658
if( N==0 || NEVER(z==0) ){
1963119659
return;
@@ -19653,19 +19681,20 @@
1965319681
zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
1965419682
}else{
1965519683
zNew = sqlite3_realloc(zOld, p->nAlloc);
1965619684
}
1965719685
if( zNew ){
19658
- if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
19686
+ if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
1965919687
p->zText = zNew;
1966019688
}else{
1966119689
p->mallocFailed = 1;
1966219690
sqlite3StrAccumReset(p);
1966319691
return;
1966419692
}
1966519693
}
1966619694
}
19695
+ assert( p->zText );
1966719696
memcpy(&p->zText[p->nChar], z, N);
1966819697
p->nChar += N;
1966919698
}
1967019699
1967119700
/*
@@ -20511,11 +20540,11 @@
2051120540
** no longer required.
2051220541
**
2051320542
** If a malloc failure occurs, NULL is returned and the db.mallocFailed
2051420543
** flag set.
2051520544
*/
20516
-#ifdef SQLITE_ENABLE_STAT2
20545
+#ifdef SQLITE_ENABLE_STAT3
2051720546
SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
2051820547
Mem m;
2051920548
memset(&m, 0, sizeof(m));
2052020549
m.db = db;
2052120550
sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -25102,11 +25131,11 @@
2510225131
return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
2510325132
}
2510425133
#endif
2510525134
2510625135
25107
-#ifdef SQLITE_DEBUG
25136
+#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
2510825137
/*
2510925138
** Helper function for printing out trace information from debugging
2511025139
** binaries. This returns the string represetation of the supplied
2511125140
** integer lock-type.
2511225141
*/
@@ -25937,18 +25966,18 @@
2593725966
** locking a random byte from a range, concurrent SHARED locks may exist
2593825967
** even if the locking primitive used is always a write-lock.
2593925968
*/
2594025969
int rc = SQLITE_OK;
2594125970
unixFile *pFile = (unixFile*)id;
25942
- unixInodeInfo *pInode = pFile->pInode;
25971
+ unixInodeInfo *pInode;
2594325972
struct flock lock;
2594425973
int tErrno = 0;
2594525974
2594625975
assert( pFile );
2594725976
OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
2594825977
azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25949
- azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
25978
+ azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
2595025979
2595125980
/* If there is already a lock of this type or more restrictive on the
2595225981
** unixFile, do nothing. Don't use the end_lock: exit path, as
2595325982
** unixEnterMutex() hasn't been called yet.
2595425983
*/
@@ -26148,11 +26177,10 @@
2614826177
static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
2614926178
unixFile *pFile = (unixFile*)id;
2615026179
unixInodeInfo *pInode;
2615126180
struct flock lock;
2615226181
int rc = SQLITE_OK;
26153
- int h;
2615426182
2615526183
assert( pFile );
2615626184
OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
2615726185
pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
2615826186
getpid()));
@@ -26160,18 +26188,14 @@
2616026188
assert( eFileLock<=SHARED_LOCK );
2616126189
if( pFile->eFileLock<=eFileLock ){
2616226190
return SQLITE_OK;
2616326191
}
2616426192
unixEnterMutex();
26165
- h = pFile->h;
2616626193
pInode = pFile->pInode;
2616726194
assert( pInode->nShared!=0 );
2616826195
if( pFile->eFileLock>SHARED_LOCK ){
2616926196
assert( pInode->eFileLock==pFile->eFileLock );
26170
- SimulateIOErrorBenign(1);
26171
- SimulateIOError( h=(-1) )
26172
- SimulateIOErrorBenign(0);
2617326197
2617426198
#ifndef NDEBUG
2617526199
/* When reducing a lock such that other processes can start
2617626200
** reading the database file again, make sure that the
2617726201
** transaction counter was updated if any part of the database
@@ -26178,15 +26202,10 @@
2617826202
** file changed. If the transaction counter is not updated,
2617926203
** other connections to the same file might not realize that
2618026204
** the file has changed and hence might not know to flush their
2618126205
** cache. The use of a stale cache can lead to database corruption.
2618226206
*/
26183
-#if 0
26184
- assert( pFile->inNormalWrite==0
26185
- || pFile->dbUpdate==0
26186
- || pFile->transCntrChng==1 );
26187
-#endif
2618826207
pFile->inNormalWrite = 0;
2618926208
#endif
2619026209
2619126210
/* downgrading to a shared lock on NFS involves clearing the write lock
2619226211
** before establishing the readlock - to avoid a race condition we downgrade
@@ -26284,13 +26303,10 @@
2628426303
pInode->nShared--;
2628526304
if( pInode->nShared==0 ){
2628626305
lock.l_type = F_UNLCK;
2628726306
lock.l_whence = SEEK_SET;
2628826307
lock.l_start = lock.l_len = 0L;
26289
- SimulateIOErrorBenign(1);
26290
- SimulateIOError( h=(-1) )
26291
- SimulateIOErrorBenign(0);
2629226308
if( unixFileLock(pFile, &lock)==0 ){
2629326309
pInode->eFileLock = NO_LOCK;
2629426310
}else{
2629526311
rc = SQLITE_IOERR_UNLOCK;
2629626312
pFile->lastErrno = errno;
@@ -28428,20 +28444,19 @@
2842828444
rc = SQLITE_NOMEM;
2842928445
goto shm_open_err;
2843028446
}
2843128447
2843228448
if( pInode->bProcessLock==0 ){
28433
- pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
28434
- (sStat.st_mode & 0777));
28449
+ const char *zRO;
28450
+ int openFlags = O_RDWR | O_CREAT;
28451
+ zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28452
+ if( zRO && sqlite3GetBoolean(zRO) ){
28453
+ openFlags = O_RDONLY;
28454
+ pShmNode->isReadonly = 1;
28455
+ }
28456
+ pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
2843528457
if( pShmNode->h<0 ){
28436
- const char *zRO;
28437
- zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28438
- if( zRO && sqlite3GetBoolean(zRO) ){
28439
- pShmNode->h = robust_open(zShmFilename, O_RDONLY,
28440
- (sStat.st_mode & 0777));
28441
- pShmNode->isReadonly = 1;
28442
- }
2844328458
if( pShmNode->h<0 ){
2844428459
rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
2844528460
goto shm_open_err;
2844628461
}
2844728462
}
@@ -29122,10 +29137,13 @@
2912229137
assert( zFilename==0 || zFilename[0]=='/'
2912329138
|| pVfs->pAppData==(void*)&autolockIoFinder );
2912429139
#else
2912529140
assert( zFilename==0 || zFilename[0]=='/' );
2912629141
#endif
29142
+
29143
+ /* No locking occurs in temporary files */
29144
+ assert( zFilename!=0 || noLock );
2912729145
2912829146
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2912929147
pNew->h = h;
2913029148
pNew->zPath = zFilename;
2913129149
if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
@@ -29224,10 +29242,11 @@
2922429242
/* Dotfile locking uses the file path so it needs to be included in
2922529243
** the dotlockLockingContext
2922629244
*/
2922729245
char *zLockFile;
2922829246
int nFilename;
29247
+ assert( zFilename!=0 );
2922929248
nFilename = (int)strlen(zFilename) + 6;
2923029249
zLockFile = (char *)sqlite3_malloc(nFilename);
2923129250
if( zLockFile==0 ){
2923229251
rc = SQLITE_NOMEM;
2923329252
}else{
@@ -29462,12 +29481,20 @@
2946229481
**
2946329482
** where NN is a 4 digit decimal number. The NN naming schemes are
2946429483
** used by the test_multiplex.c module.
2946529484
*/
2946629485
nDb = sqlite3Strlen30(zPath) - 1;
29467
- while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29468
- if( nDb==0 ) return SQLITE_OK;
29486
+#ifdef SQLITE_ENABLE_8_3_NAMES
29487
+ while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--;
29488
+ if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK;
29489
+#else
29490
+ while( zPath[nDb]!='-' ){
29491
+ assert( nDb>0 );
29492
+ assert( zPath[nDb]!='\n' );
29493
+ nDb--;
29494
+ }
29495
+#endif
2946929496
memcpy(zDb, zPath, nDb);
2947029497
zDb[nDb] = '\0';
2947129498
2947229499
if( 0==osStat(zDb, &sStat) ){
2947329500
*pMode = sStat.st_mode & 0777;
@@ -29995,14 +30022,16 @@
2999530022
** the current time and date as a Julian Day number times 86_400_000. In
2999630023
** other words, write into *piNow the number of milliseconds since the Julian
2999730024
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
2999830025
** proleptic Gregorian calendar.
2999930026
**
30000
-** On success, return 0. Return 1 if the time and date cannot be found.
30027
+** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
30028
+** cannot be found.
3000130029
*/
3000230030
static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
3000330031
static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
30032
+ int rc = SQLITE_OK;
3000430033
#if defined(NO_GETTOD)
3000530034
time_t t;
3000630035
time(&t);
3000730036
*piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
3000830037
#elif OS_VXWORKS
@@ -30009,34 +30038,38 @@
3000930038
struct timespec sNow;
3001030039
clock_gettime(CLOCK_REALTIME, &sNow);
3001130040
*piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
3001230041
#else
3001330042
struct timeval sNow;
30014
- gettimeofday(&sNow, 0);
30015
- *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
30043
+ if( gettimeofday(&sNow, 0)==0 ){
30044
+ *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
30045
+ }else{
30046
+ rc = SQLITE_ERROR;
30047
+ }
3001630048
#endif
3001730049
3001830050
#ifdef SQLITE_TEST
3001930051
if( sqlite3_current_time ){
3002030052
*piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
3002130053
}
3002230054
#endif
3002330055
UNUSED_PARAMETER(NotUsed);
30024
- return 0;
30056
+ return rc;
3002530057
}
3002630058
3002730059
/*
3002830060
** Find the current time (in Universal Coordinated Time). Write the
3002930061
** current time and date as a Julian Day number into *prNow and
3003030062
** return 0. Return 1 if the time and date cannot be found.
3003130063
*/
3003230064
static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
30033
- sqlite3_int64 i;
30065
+ sqlite3_int64 i = 0;
30066
+ int rc;
3003430067
UNUSED_PARAMETER(NotUsed);
30035
- unixCurrentTimeInt64(0, &i);
30068
+ rc = unixCurrentTimeInt64(0, &i);
3003630069
*prNow = i/86400000.0;
30037
- return 0;
30070
+ return rc;
3003830071
}
3003930072
3004030073
/*
3004130074
** We added the xGetLastError() method with the intention of providing
3004230075
** better low-level error messages when operating-system problems come up
@@ -34169,11 +34202,11 @@
3416934202
3417034203
if( h==INVALID_HANDLE_VALUE ){
3417134204
pFile->lastErrno = GetLastError();
3417234205
winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
3417334206
free(zConverted);
34174
- if( isReadWrite ){
34207
+ if( isReadWrite && !isExclusive ){
3417534208
return winOpen(pVfs, zName, id,
3417634209
((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
3417734210
}else{
3417834211
return SQLITE_CANTOPEN_BKPT;
3417934212
}
@@ -34535,11 +34568,11 @@
3453534568
}
3453634569
static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
3453734570
UNUSED_PARAMETER(pVfs);
3453834571
getLastErrorMsg(nBuf, zBufOut);
3453934572
}
34540
-void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34573
+static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
3454134574
UNUSED_PARAMETER(pVfs);
3454234575
#if SQLITE_OS_WINCE
3454334576
/* The GetProcAddressA() routine is only available on wince. */
3454434577
return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
3454534578
#else
@@ -34546,11 +34579,11 @@
3454634579
/* All other windows platforms expect GetProcAddress() to take
3454734580
** an Ansi string regardless of the _UNICODE setting */
3454834581
return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
3454934582
#endif
3455034583
}
34551
-void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34584
+static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
3455234585
UNUSED_PARAMETER(pVfs);
3455334586
FreeLibrary((HANDLE)pHandle);
3455434587
}
3455534588
#else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
3455634589
#define winDlOpen 0
@@ -34620,11 +34653,12 @@
3462034653
** the current time and date as a Julian Day number times 86_400_000. In
3462134654
** other words, write into *piNow the number of milliseconds since the Julian
3462234655
** epoch of noon in Greenwich on November 24, 4714 B.C according to the
3462334656
** proleptic Gregorian calendar.
3462434657
**
34625
-** On success, return 0. Return 1 if the time and date cannot be found.
34658
+** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
34659
+** cannot be found.
3462634660
*/
3462734661
static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
3462834662
/* FILETIME structure is a 64-bit value representing the number of
3462934663
100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
3463034664
*/
@@ -34640,11 +34674,11 @@
3464034674
#if SQLITE_OS_WINCE
3464134675
SYSTEMTIME time;
3464234676
GetSystemTime(&time);
3464334677
/* if SystemTimeToFileTime() fails, it returns zero. */
3464434678
if (!SystemTimeToFileTime(&time,&ft)){
34645
- return 1;
34679
+ return SQLITE_ERROR;
3464634680
}
3464734681
#else
3464834682
GetSystemTimeAsFileTime( &ft );
3464934683
#endif
3465034684
@@ -34656,19 +34690,19 @@
3465634690
if( sqlite3_current_time ){
3465734691
*piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
3465834692
}
3465934693
#endif
3466034694
UNUSED_PARAMETER(pVfs);
34661
- return 0;
34695
+ return SQLITE_OK;
3466234696
}
3466334697
3466434698
/*
3466534699
** Find the current time (in Universal Coordinated Time). Write the
3466634700
** current time and date as a Julian Day number into *prNow and
3466734701
** return 0. Return 1 if the time and date cannot be found.
3466834702
*/
34669
-int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
34703
+static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
3467034704
int rc;
3467134705
sqlite3_int64 i;
3467234706
rc = winCurrentTimeInt64(pVfs, &i);
3467334707
if( !rc ){
3467434708
*prNow = i/86400000.0;
@@ -35789,12 +35823,10 @@
3578935823
typedef struct PCache1 PCache1;
3579035824
typedef struct PgHdr1 PgHdr1;
3579135825
typedef struct PgFreeslot PgFreeslot;
3579235826
typedef struct PGroup PGroup;
3579335827
35794
-typedef struct PGroupBlock PGroupBlock;
35795
-typedef struct PGroupBlockList PGroupBlockList;
3579635828
3579735829
/* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
3579835830
** of one or more PCaches that are able to recycle each others unpinned
3579935831
** pages when they are under memory pressure. A PGroup is an instance of
3580035832
** the following object.
@@ -35821,69 +35853,11 @@
3582135853
int nMaxPage; /* Sum of nMax for purgeable caches */
3582235854
int nMinPage; /* Sum of nMin for purgeable caches */
3582335855
int mxPinned; /* nMaxpage + 10 - nMinPage */
3582435856
int nCurrentPage; /* Number of purgeable pages allocated */
3582535857
PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35826
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
35827
- int isBusy; /* Do not run ReleaseMemory() if true */
35828
- PGroupBlockList *pBlockList; /* List of block-lists for this group */
35829
-#endif
35830
-};
35831
-
35832
-/*
35833
-** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35834
-** each PGroup structure has a linked list of the the following starting
35835
-** at PGroup.pBlockList. There is one entry for each distinct page-size
35836
-** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35837
-** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35838
-** size requested by each pcache, which is the database page-size plus
35839
-** the various header structures used by the pcache, pager and btree layers.
35840
-** Usually around (pgsz+200) bytes.
35841
-**
35842
-** This size (pgsz+200) bytes is not allocated efficiently by some
35843
-** implementations of malloc. In particular, some implementations are only
35844
-** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35845
-** integer value. Since the page-size is a power of 2, this means we
35846
-** end up wasting (pgsz-200) bytes in each allocation.
35847
-**
35848
-** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35849
-** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35850
-** are requested from malloc allocator. After a block is returned,
35851
-** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35852
-** allocations can fit in the space returned by malloc(). This value may
35853
-** be more than M.
35854
-**
35855
-** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35856
-** contains the number of allocations that will fit in the aData[] space.
35857
-** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35858
-** within aData is in use, the corresponding bit in mUsed is set. Thus
35859
-** when (mUsed+1==(1 << nEntry)) the block is completely full.
35860
-**
35861
-** Each time a slot within a block is freed, the block is moved to the start
35862
-** of the linked-list. And if a block becomes completely full, then it is
35863
-** moved to the end of the list. As a result, when searching for a free
35864
-** slot, only the first block in the list need be examined. If it is full,
35865
-** then it is guaranteed that all blocks are full.
35866
-*/
35867
-struct PGroupBlockList {
35868
- int nByte; /* Size of each allocation in bytes */
35869
- PGroupBlock *pFirst; /* First PGroupBlock in list */
35870
- PGroupBlock *pLast; /* Last PGroupBlock in list */
35871
- PGroupBlockList *pNext; /* Next block-list attached to group */
35872
-};
35873
-
35874
-struct PGroupBlock {
35875
- Bitmask mUsed; /* Mask of used slots */
35876
- int nEntry; /* Maximum number of allocations in aData[] */
35877
- u8 *aData; /* Pointer to data block */
35878
- PGroupBlock *pNext; /* Next PGroupBlock in list */
35879
- PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35880
- PGroupBlockList *pList; /* Owner list */
35881
-};
35882
-
35883
-/* Minimum value for PGroupBlock.nEntry */
35884
-#define PAGECACHE_BLOCKALLOC_MINENTRY 15
35858
+};
3588535859
3588635860
/* Each page cache is an instance of the following object. Every
3588735861
** open database file (including each in-memory database and each
3588835862
** temporary or transient database) has a single page cache which
3588935863
** is an instance of this object.
@@ -35983,21 +35957,10 @@
3598335957
**
3598435958
** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
3598535959
*/
3598635960
#define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
3598735961
#define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35988
-
35989
-/*
35990
-** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35991
-** a PGroupBlock pointer based on a pointer to a page buffer.
35992
-*/
35993
-#define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35994
- ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35995
-
35996
-#define PAGE_GET_BLOCKPTR(pCache, pPg) \
35997
- ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35998
-
3599935962
3600035963
/*
3600135964
** Macros to enter and leave the PCache LRU mutex.
3600235965
*/
3600335966
#define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -36120,159 +36083,32 @@
3612036083
return iSize;
3612136084
}
3612236085
}
3612336086
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
3612436087
36125
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36126
-/*
36127
-** The block pBlock belongs to list pList but is not currently linked in.
36128
-** Insert it into the start of the list.
36129
-*/
36130
-static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36131
- pBlock->pPrev = 0;
36132
- pBlock->pNext = pList->pFirst;
36133
- pList->pFirst = pBlock;
36134
- if( pBlock->pNext ){
36135
- pBlock->pNext->pPrev = pBlock;
36136
- }else{
36137
- assert( pList->pLast==0 );
36138
- pList->pLast = pBlock;
36139
- }
36140
-}
36141
-
36142
-/*
36143
-** If there are no blocks in the list headed by pList, remove pList
36144
-** from the pGroup->pBlockList list and free it with sqlite3_free().
36145
-*/
36146
-static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36147
- assert( sqlite3_mutex_held(pGroup->mutex) );
36148
- if( pList->pFirst==0 ){
36149
- PGroupBlockList **pp;
36150
- for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36151
- *pp = (*pp)->pNext;
36152
- sqlite3_free(pList);
36153
- }
36154
-}
36155
-#endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36156
-
3615736088
/*
3615836089
** Allocate a new page object initially associated with cache pCache.
3615936090
*/
3616036091
static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
3616136092
int nByte = sizeof(PgHdr1) + pCache->szPage;
36162
- void *pPg = 0;
36163
- PgHdr1 *p;
36164
-
36165
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36166
- PGroup *pGroup = pCache->pGroup;
36167
- PGroupBlockList *pList;
36168
- PGroupBlock *pBlock;
36169
- int i;
36170
-
36171
- nByte += sizeof(PGroupBlockList *);
36172
- nByte = ROUND8(nByte);
36173
-
36174
- for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36175
- if( pList->nByte==nByte ) break;
36176
- }
36177
- if( pList==0 ){
36178
- PGroupBlockList *pNew;
36179
- assert( pGroup->isBusy==0 );
36180
- assert( sqlite3_mutex_held(pGroup->mutex) );
36181
- pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36182
- pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36183
- pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36184
- if( pNew==0 ){
36185
- /* malloc() failure. Return early. */
36186
- return 0;
36187
- }
36188
-#ifdef SQLITE_DEBUG
36189
- for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36190
- assert( pList->nByte!=nByte );
36191
- }
36192
-#endif
36193
- pNew->nByte = nByte;
36194
- pNew->pNext = pGroup->pBlockList;
36195
- pGroup->pBlockList = pNew;
36196
- pList = pNew;
36197
- }
36198
-
36199
- pBlock = pList->pFirst;
36200
- if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36201
- int sz;
36202
-
36203
- /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36204
- ** structure and MINENTRY allocations of nByte bytes each. If the
36205
- ** allocator returns more memory than requested, then more than MINENTRY
36206
- ** allocations may fit in it. */
36207
- assert( sqlite3_mutex_held(pGroup->mutex) );
36208
- pcache1LeaveMutex(pCache->pGroup);
36209
- sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36210
- pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36211
- pcache1EnterMutex(pCache->pGroup);
36212
-
36213
- if( !pBlock ){
36214
- freeListIfEmpty(pGroup, pList);
36215
- return 0;
36216
- }
36217
- pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36218
- if( pBlock->nEntry>=BMS ){
36219
- pBlock->nEntry = BMS-1;
36220
- }
36221
- pBlock->pList = pList;
36222
- pBlock->mUsed = 0;
36223
- pBlock->aData = (u8 *)&pBlock[1];
36224
- addBlockToList(pList, pBlock);
36225
-
36226
- sz = sqlite3MallocSize(pBlock);
36227
- sqlite3_mutex_enter(pcache1.mutex);
36228
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36229
- sqlite3_mutex_leave(pcache1.mutex);
36230
- }
36231
-
36232
- for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36233
- if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36234
- pBlock->mUsed |= ((Bitmask)1<<i);
36235
- pPg = (void *)&pBlock->aData[pList->nByte * i];
36236
- }
36237
- }
36238
- assert( pPg );
36239
- PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36240
-
36241
- /* If the block is now full, shift it to the end of the list */
36242
- if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36243
- assert( pList->pFirst==pBlock );
36244
- assert( pBlock->pPrev==0 );
36245
- assert( pList->pLast->pNext==0 );
36246
- pList->pFirst = pBlock->pNext;
36247
- pList->pFirst->pPrev = 0;
36248
- pBlock->pPrev = pList->pLast;
36249
- pBlock->pNext = 0;
36250
- pList->pLast->pNext = pBlock;
36251
- pList->pLast = pBlock;
36252
- }
36253
- p = PAGE_TO_PGHDR1(pCache, pPg);
36254
- if( pCache->bPurgeable ){
36255
- pCache->pGroup->nCurrentPage++;
36256
- }
36257
-#else
36093
+ PgHdr1 *p = 0;
36094
+ void *pPg;
36095
+
3625836096
/* The group mutex must be released before pcache1Alloc() is called. This
3625936097
** is because it may call sqlite3_release_memory(), which assumes that
3626036098
** this mutex is not held. */
3626136099
assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
3626236100
pcache1LeaveMutex(pCache->pGroup);
3626336101
pPg = pcache1Alloc(nByte);
3626436102
pcache1EnterMutex(pCache->pGroup);
36103
+
3626536104
if( pPg ){
3626636105
p = PAGE_TO_PGHDR1(pCache, pPg);
3626736106
if( pCache->bPurgeable ){
3626836107
pCache->pGroup->nCurrentPage++;
3626936108
}
36270
- }else{
36271
- p = 0;
3627236109
}
36273
-#endif
3627436110
return p;
3627536111
}
3627636112
3627736113
/*
3627836114
** Free a page object allocated by pcache1AllocPage().
@@ -36282,53 +36118,12 @@
3628236118
** with a NULL pointer, so we mark the NULL test with ALWAYS().
3628336119
*/
3628436120
static void pcache1FreePage(PgHdr1 *p){
3628536121
if( ALWAYS(p) ){
3628636122
PCache1 *pCache = p->pCache;
36287
- void *pPg = PGHDR1_TO_PAGE(p);
36288
-
36289
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36290
- PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36291
- PGroupBlockList *pList = pBlock->pList;
36292
- int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36293
-
36294
- assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36295
- assert( pBlock->mUsed & ((Bitmask)1<<i) );
36296
- pBlock->mUsed &= ~((Bitmask)1<<i);
36297
-
36298
- /* Remove the block from the list. If it is completely empty, free it.
36299
- ** Or if it is not completely empty, re-insert it at the start of the
36300
- ** list. */
36301
- if( pList->pFirst==pBlock ){
36302
- pList->pFirst = pBlock->pNext;
36303
- if( pList->pFirst ) pList->pFirst->pPrev = 0;
36304
- }else{
36305
- pBlock->pPrev->pNext = pBlock->pNext;
36306
- }
36307
- if( pList->pLast==pBlock ){
36308
- pList->pLast = pBlock->pPrev;
36309
- if( pList->pLast ) pList->pLast->pNext = 0;
36310
- }else{
36311
- pBlock->pNext->pPrev = pBlock->pPrev;
36312
- }
36313
-
36314
- if( pBlock->mUsed==0 ){
36315
- PGroup *pGroup = p->pCache->pGroup;
36316
-
36317
- int sz = sqlite3MallocSize(pBlock);
36318
- sqlite3_mutex_enter(pcache1.mutex);
36319
- sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36320
- sqlite3_mutex_leave(pcache1.mutex);
36321
- freeListIfEmpty(pGroup, pList);
36322
- sqlite3_free(pBlock);
36323
- }else{
36324
- addBlockToList(pList, pBlock);
36325
- }
36326
-#else
3632736123
assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36328
- pcache1Free(pPg);
36329
-#endif
36124
+ pcache1Free(PGHDR1_TO_PAGE(p));
3633036125
if( pCache->bPurgeable ){
3633136126
pCache->pGroup->nCurrentPage--;
3633236127
}
3633336128
}
3633436129
}
@@ -36935,13 +36730,10 @@
3693536730
** been released, the function returns. The return value is the total number
3693636731
** of bytes of memory released.
3693736732
*/
3693836733
SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
3693936734
int nFree = 0;
36940
-#ifdef SQLITE_PAGECACHE_BLOCKALLOC
36941
- if( pcache1.grp.isBusy ) return 0;
36942
-#endif
3694336735
assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
3694436736
assert( sqlite3_mutex_notheld(pcache1.mutex) );
3694536737
if( pcache1.pStart==0 ){
3694636738
PgHdr1 *p;
3694736739
pcache1EnterMutex(&pcache1.grp);
@@ -38200,12 +37992,12 @@
3820037992
i64 journalSizeLimit; /* Size limit for persistent journal files */
3820137993
char *zFilename; /* Name of the database file */
3820237994
char *zJournal; /* Name of the journal file */
3820337995
int (*xBusyHandler)(void*); /* Function to call when busy */
3820437996
void *pBusyHandlerArg; /* Context argument for xBusyHandler */
37997
+ int nHit, nMiss; /* Total cache hits and misses */
3820537998
#ifdef SQLITE_TEST
38206
- int nHit, nMiss; /* Cache hits and missing */
3820737999
int nRead, nWrite; /* Database pages read/written */
3820838000
#endif
3820938001
void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
3821038002
#ifdef SQLITE_HAS_CODEC
3821138003
void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
@@ -40233,11 +40025,10 @@
4023340025
needPagerReset = 0;
4023440026
}
4023540027
rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
4023640028
if( rc!=SQLITE_OK ){
4023740029
if( rc==SQLITE_DONE ){
40238
- rc = SQLITE_OK;
4023940030
pPager->journalOff = szJ;
4024040031
break;
4024140032
}else if( rc==SQLITE_IOERR_SHORT_READ ){
4024240033
/* If the journal has been truncated, simply stop reading and
4024340034
** processing the journal. This might happen if the journal was
@@ -40495,10 +40286,11 @@
4049540286
#if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
4049640287
PgHdr *p; /* For looping over pages */
4049740288
#endif
4049840289
4049940290
assert( pPager->pWal );
40291
+ assert( pList );
4050040292
#ifdef SQLITE_DEBUG
4050140293
/* Verify that the page list is in accending order */
4050240294
for(p=pList; p && p->pDirty; p=p->pDirty){
4050340295
assert( p->pgno < p->pDirty->pgno );
4050440296
}
@@ -41699,11 +41491,11 @@
4169941491
** The doNotSpill flag inhibits all cache spilling regardless of whether
4170041492
** or not a sync is required. This is set during a rollback.
4170141493
**
4170241494
** Spilling is also prohibited when in an error state since that could
4170341495
** lead to database corruption. In the current implementaton it
41704
- ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41496
+ ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
4170541497
** while in the error state, hence it is impossible for this routine to
4170641498
** be called in the error state. Nevertheless, we include a NEVER()
4170741499
** test for the error state as a safeguard against future changes.
4170841500
*/
4170941501
if( NEVER(pPager->errCode) ) return SQLITE_OK;
@@ -42535,18 +42327,17 @@
4253542327
4253642328
if( (*ppPage)->pPager && !noContent ){
4253742329
/* In this case the pcache already contains an initialized copy of
4253842330
** the page. Return without further ado. */
4253942331
assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
42540
- PAGER_INCR(pPager->nHit);
42332
+ pPager->nHit++;
4254142333
return SQLITE_OK;
4254242334
4254342335
}else{
4254442336
/* The pager cache has created a new page. Its content needs to
4254542337
** be initialized. */
4254642338
42547
- PAGER_INCR(pPager->nMiss);
4254842339
pPg = *ppPage;
4254942340
pPg->pPager = pPager;
4255042341
4255142342
/* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
4255242343
** number greater than this, or the unused locking-page, is requested. */
@@ -42578,10 +42369,11 @@
4257842369
}
4257942370
memset(pPg->pData, 0, pPager->pageSize);
4258042371
IOTRACE(("ZERO %p %d\n", pPager, pgno));
4258142372
}else{
4258242373
assert( pPg->pPager==pPager );
42374
+ pPager->nMiss++;
4258342375
rc = readDbPage(pPg);
4258442376
if( rc!=SQLITE_OK ){
4258542377
goto pager_acquire_err;
4258642378
}
4258742379
}
@@ -43611,10 +43403,35 @@
4361143403
a[9] = pPager->nRead;
4361243404
a[10] = pPager->nWrite;
4361343405
return a;
4361443406
}
4361543407
#endif
43408
+
43409
+/*
43410
+** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
43411
+** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
43412
+** current cache hit or miss count, according to the value of eStat. If the
43413
+** reset parameter is non-zero, the cache hit or miss count is zeroed before
43414
+** returning.
43415
+*/
43416
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
43417
+ int *piStat;
43418
+
43419
+ assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
43420
+ || eStat==SQLITE_DBSTATUS_CACHE_MISS
43421
+ );
43422
+ if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
43423
+ piStat = &pPager->nHit;
43424
+ }else{
43425
+ piStat = &pPager->nMiss;
43426
+ }
43427
+
43428
+ *pnVal += *piStat;
43429
+ if( reset ){
43430
+ *piStat = 0;
43431
+ }
43432
+}
4361643433
4361743434
/*
4361843435
** Return true if this is an in-memory pager.
4361943436
*/
4362043437
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
@@ -46706,11 +46523,11 @@
4670646523
*/
4670746524
if( iRead ){
4670846525
int sz;
4670946526
i64 iOffset;
4671046527
sz = pWal->hdr.szPage;
46711
- sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
46528
+ sz = (sz&0xfe00) + ((sz&0x0001)<<16);
4671246529
testcase( sz<=32768 );
4671346530
testcase( sz>=65536 );
4671446531
iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
4671546532
*pInWal = 1;
4671646533
/* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
@@ -50019,21 +49836,23 @@
5001949836
*/
5002049837
if( isMemdb==0 && isTempDb==0 ){
5002149838
if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
5002249839
int nFullPathname = pVfs->mxPathname+1;
5002349840
char *zFullPathname = sqlite3Malloc(nFullPathname);
50024
- sqlite3_mutex *mutexShared;
49841
+ MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
5002549842
p->sharable = 1;
5002649843
if( !zFullPathname ){
5002749844
sqlite3_free(p);
5002849845
return SQLITE_NOMEM;
5002949846
}
5003049847
sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
49848
+#if SQLITE_THREADSAFE
5003149849
mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
5003249850
sqlite3_mutex_enter(mutexOpen);
5003349851
mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
5003449852
sqlite3_mutex_enter(mutexShared);
49853
+#endif
5003549854
for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
5003649855
assert( pBt->nRef>0 );
5003749856
if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
5003849857
&& sqlite3PagerVfs(pBt->pPager)==pVfs ){
5003949858
int iDb;
@@ -50135,13 +49954,13 @@
5013549954
5013649955
#if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
5013749956
/* Add the new BtShared object to the linked list sharable BtShareds.
5013849957
*/
5013949958
if( p->sharable ){
50140
- sqlite3_mutex *mutexShared;
49959
+ MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
5014149960
pBt->nRef = 1;
50142
- mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49961
+ MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
5014349962
if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
5014449963
pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
5014549964
if( pBt->mutex==0 ){
5014649965
rc = SQLITE_NOMEM;
5014749966
db->mallocFailed = 0;
@@ -50219,16 +50038,16 @@
5021950038
** true if the BtShared.nRef counter reaches zero and return
5022050039
** false if it is still positive.
5022150040
*/
5022250041
static int removeFromSharingList(BtShared *pBt){
5022350042
#ifndef SQLITE_OMIT_SHARED_CACHE
50224
- sqlite3_mutex *pMaster;
50043
+ MUTEX_LOGIC( sqlite3_mutex *pMaster; )
5022550044
BtShared *pList;
5022650045
int removed = 0;
5022750046
5022850047
assert( sqlite3_mutex_notheld(pBt->mutex) );
50229
- pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50048
+ MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
5023050049
sqlite3_mutex_enter(pMaster);
5023150050
pBt->nRef--;
5023250051
if( pBt->nRef<=0 ){
5023350052
if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
5023450053
GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
@@ -52191,25 +52010,59 @@
5219152010
offset -= ovflSize;
5219252011
}else{
5219352012
/* Need to read this page properly. It contains some of the
5219452013
** range of data that is being read (eOp==0) or written (eOp!=0).
5219552014
*/
52196
- DbPage *pDbPage;
52015
+#ifdef SQLITE_DIRECT_OVERFLOW_READ
52016
+ sqlite3_file *fd;
52017
+#endif
5219752018
int a = amt;
52198
- rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
52199
- if( rc==SQLITE_OK ){
52200
- aPayload = sqlite3PagerGetData(pDbPage);
52201
- nextPage = get4byte(aPayload);
52202
- if( a + offset > ovflSize ){
52203
- a = ovflSize - offset;
52204
- }
52205
- rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52206
- sqlite3PagerUnref(pDbPage);
52207
- offset = 0;
52208
- amt -= a;
52209
- pBuf += a;
52210
- }
52019
+ if( a + offset > ovflSize ){
52020
+ a = ovflSize - offset;
52021
+ }
52022
+
52023
+#ifdef SQLITE_DIRECT_OVERFLOW_READ
52024
+ /* If all the following are true:
52025
+ **
52026
+ ** 1) this is a read operation, and
52027
+ ** 2) data is required from the start of this overflow page, and
52028
+ ** 3) the database is file-backed, and
52029
+ ** 4) there is no open write-transaction, and
52030
+ ** 5) the database is not a WAL database,
52031
+ **
52032
+ ** then data can be read directly from the database file into the
52033
+ ** output buffer, bypassing the page-cache altogether. This speeds
52034
+ ** up loading large records that span many overflow pages.
52035
+ */
52036
+ if( eOp==0 /* (1) */
52037
+ && offset==0 /* (2) */
52038
+ && pBt->inTransaction==TRANS_READ /* (4) */
52039
+ && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
52040
+ && pBt->pPage1->aData[19]==0x01 /* (5) */
52041
+ ){
52042
+ u8 aSave[4];
52043
+ u8 *aWrite = &pBuf[-4];
52044
+ memcpy(aSave, aWrite, 4);
52045
+ rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
52046
+ nextPage = get4byte(aWrite);
52047
+ memcpy(aWrite, aSave, 4);
52048
+ }else
52049
+#endif
52050
+
52051
+ {
52052
+ DbPage *pDbPage;
52053
+ rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
52054
+ if( rc==SQLITE_OK ){
52055
+ aPayload = sqlite3PagerGetData(pDbPage);
52056
+ nextPage = get4byte(aPayload);
52057
+ rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52058
+ sqlite3PagerUnref(pDbPage);
52059
+ offset = 0;
52060
+ }
52061
+ }
52062
+ amt -= a;
52063
+ pBuf += a;
5221152064
}
5221252065
}
5221352066
}
5221452067
5221552068
if( rc==SQLITE_OK && amt>0 ){
@@ -52804,11 +52657,10 @@
5280452657
}
5280552658
}
5280652659
if( c==0 ){
5280752660
if( pPage->intKey && !pPage->leaf ){
5280852661
lwr = idx;
52809
- upr = lwr - 1;
5281052662
break;
5281152663
}else{
5281252664
*pRes = 0;
5281352665
rc = SQLITE_OK;
5281452666
goto moveto_finish;
@@ -52822,11 +52674,11 @@
5282252674
if( lwr>upr ){
5282352675
break;
5282452676
}
5282552677
pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
5282652678
}
52827
- assert( lwr==upr+1 );
52679
+ assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
5282852680
assert( pPage->isInit );
5282952681
if( pPage->leaf ){
5283052682
chldPg = 0;
5283152683
}else if( lwr>=pPage->nCell ){
5283252684
chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
@@ -53087,10 +52939,12 @@
5308752939
}
5308852940
if( rc ){
5308952941
pTrunk = 0;
5309052942
goto end_allocate_page;
5309152943
}
52944
+ assert( pTrunk!=0 );
52945
+ assert( pTrunk->aData!=0 );
5309252946
5309352947
k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
5309452948
if( k==0 && !searchList ){
5309552949
/* The trunk has no leaves and the list is not being searched.
5309652950
** So extract the trunk page itself and use it as the newly
@@ -54214,17 +54068,19 @@
5421454068
** This is safe because dropping a cell only overwrites the first
5421554069
** four bytes of it, and this function does not need the first
5421654070
** four bytes of the divider cell. So the pointer is safe to use
5421754071
** later on.
5421854072
**
54219
- ** Unless SQLite is compiled in secure-delete mode. In this case,
54073
+ ** But not if we are in secure-delete mode. In secure-delete mode,
5422054074
** the dropCell() routine will overwrite the entire cell with zeroes.
5422154075
** In this case, temporarily copy the cell into the aOvflSpace[]
5422254076
** buffer. It will be copied out again as soon as the aSpace[] buffer
5422354077
** is allocated. */
5422454078
if( pBt->secureDelete ){
54225
- int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
54079
+ int iOff;
54080
+
54081
+ iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
5422654082
if( (iOff+szNew[i])>(int)pBt->usableSize ){
5422754083
rc = SQLITE_CORRUPT_BKPT;
5422854084
memset(apOld, 0, (i+1)*sizeof(MemPage*));
5422954085
goto balance_cleanup;
5423054086
}else{
@@ -54640,10 +54496,11 @@
5464054496
int isDivider = 0;
5464154497
while( i==iNextOld ){
5464254498
/* Cell i is the cell immediately following the last cell on old
5464354499
** sibling page j. If the siblings are not leaf pages of an
5464454500
** intkey b-tree, then cell i was a divider cell. */
54501
+ assert( j+1 < ArraySize(apCopy) );
5464554502
pOld = apCopy[++j];
5464654503
iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
5464754504
if( pOld->nOverflow ){
5464854505
nOverflow = pOld->nOverflow;
5464954506
iOverflow = i + !leafData + pOld->aOvfl[0].idx;
@@ -56982,18 +56839,18 @@
5698256839
/*
5698356840
** Release all resources associated with an sqlite3_backup* handle.
5698456841
*/
5698556842
SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
5698656843
sqlite3_backup **pp; /* Ptr to head of pagers backup list */
56987
- sqlite3_mutex *mutex; /* Mutex to protect source database */
56844
+ MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
5698856845
int rc; /* Value to return */
5698956846
5699056847
/* Enter the mutexes */
5699156848
if( p==0 ) return SQLITE_OK;
5699256849
sqlite3_mutex_enter(p->pSrcDb->mutex);
5699356850
sqlite3BtreeEnter(p->pSrc);
56994
- mutex = p->pSrcDb->mutex;
56851
+ MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
5699556852
if( p->pDestDb ){
5699656853
sqlite3_mutex_enter(p->pDestDb->mutex);
5699756854
}
5699856855
5699956856
/* Detach this backup from the source pager. */
@@ -57108,13 +56965,21 @@
5710856965
** goes wrong, the transaction on pTo is rolled back. If successful, the
5710956966
** transaction is committed before returning.
5711056967
*/
5711156968
SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
5711256969
int rc;
56970
+ sqlite3_file *pFd; /* File descriptor for database pTo */
5711356971
sqlite3_backup b;
5711456972
sqlite3BtreeEnter(pTo);
5711556973
sqlite3BtreeEnter(pFrom);
56974
+
56975
+ assert( sqlite3BtreeIsInTrans(pTo) );
56976
+ pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
56977
+ if( pFd->pMethods ){
56978
+ i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
56979
+ sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
56980
+ }
5711656981
5711756982
/* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
5711856983
** to 0. This is used by the implementations of sqlite3_backup_step()
5711956984
** and sqlite3_backup_finish() to detect that they are being called
5712056985
** from this function, not directly by the user.
@@ -57137,10 +57002,11 @@
5713757002
rc = sqlite3_backup_finish(&b);
5713857003
if( rc==SQLITE_OK ){
5713957004
pTo->pBt->pageSizeFixed = 0;
5714057005
}
5714157006
57007
+ assert( sqlite3BtreeIsInTrans(pTo)==0 );
5714257008
sqlite3BtreeLeave(pFrom);
5714357009
sqlite3BtreeLeave(pTo);
5714457010
return rc;
5714557011
}
5714657012
#endif /* SQLITE_OMIT_VACUUM */
@@ -58171,15 +58037,15 @@
5817158037
*ppVal = 0;
5817258038
return SQLITE_OK;
5817358039
}
5817458040
op = pExpr->op;
5817558041
58176
- /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
58042
+ /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
5817758043
** The ifdef here is to enable us to achieve 100% branch test coverage even
58178
- ** when SQLITE_ENABLE_STAT2 is omitted.
58044
+ ** when SQLITE_ENABLE_STAT3 is omitted.
5817958045
*/
58180
-#ifdef SQLITE_ENABLE_STAT2
58046
+#ifdef SQLITE_ENABLE_STAT3
5818158047
if( op==TK_REGISTER ) op = pExpr->op2;
5818258048
#else
5818358049
if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
5818458050
#endif
5818558051
@@ -58874,12 +58740,12 @@
5887458740
/*
5887558741
** Change the P2 operand of instruction addr so that it points to
5887658742
** the address of the next instruction to be coded.
5887758743
*/
5887858744
SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58879
- assert( addr>=0 );
58880
- sqlite3VdbeChangeP2(p, addr, p->nOp);
58745
+ assert( addr>=0 || p->db->mallocFailed );
58746
+ if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
5888158747
}
5888258748
5888358749
5888458750
/*
5888558751
** If the input FuncDef structure is ephemeral, then free it. If
@@ -59080,34 +58946,33 @@
5908058946
** Change the comment on the the most recently coded instruction. Or
5908158947
** insert a No-op and add the comment to that new instruction. This
5908258948
** makes the code easier to read during debugging. None of this happens
5908358949
** in a production build.
5908458950
*/
59085
-SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
59086
- va_list ap;
59087
- if( !p ) return;
58951
+static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
5908858952
assert( p->nOp>0 || p->aOp==0 );
5908958953
assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
5909058954
if( p->nOp ){
59091
- char **pz = &p->aOp[p->nOp-1].zComment;
58955
+ assert( p->aOp );
58956
+ sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
58957
+ p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
58958
+ }
58959
+}
58960
+SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
58961
+ va_list ap;
58962
+ if( p ){
5909258963
va_start(ap, zFormat);
59093
- sqlite3DbFree(p->db, *pz);
59094
- *pz = sqlite3VMPrintf(p->db, zFormat, ap);
58964
+ vdbeVComment(p, zFormat, ap);
5909558965
va_end(ap);
5909658966
}
5909758967
}
5909858968
SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
5909958969
va_list ap;
59100
- if( !p ) return;
59101
- sqlite3VdbeAddOp0(p, OP_Noop);
59102
- assert( p->nOp>0 || p->aOp==0 );
59103
- assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
59104
- if( p->nOp ){
59105
- char **pz = &p->aOp[p->nOp-1].zComment;
58970
+ if( p ){
58971
+ sqlite3VdbeAddOp0(p, OP_Noop);
5910658972
va_start(ap, zFormat);
59107
- sqlite3DbFree(p->db, *pz);
59108
- *pz = sqlite3VMPrintf(p->db, zFormat, ap);
58973
+ vdbeVComment(p, zFormat, ap);
5910958974
va_end(ap);
5911058975
}
5911158976
}
5911258977
#endif /* NDEBUG */
5911358978
@@ -59441,11 +59306,11 @@
5944159306
SubProgram **apSub = 0; /* Array of sub-vdbes */
5944259307
Mem *pSub = 0; /* Memory cell hold array of subprogs */
5944359308
sqlite3 *db = p->db; /* The database connection */
5944459309
int i; /* Loop counter */
5944559310
int rc = SQLITE_OK; /* Return code */
59446
- Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */
59311
+ Mem *pMem = &p->aMem[1]; /* First Mem of result set */
5944759312
5944859313
assert( p->explain );
5944959314
assert( p->magic==VDBE_MAGIC_RUN );
5945059315
assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
5945159316
@@ -59452,10 +59317,11 @@
5945259317
/* Even though this opcode does not use dynamic strings for
5945359318
** the result, result columns may become dynamic if the user calls
5945459319
** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
5945559320
*/
5945659321
releaseMemArray(pMem, 8);
59322
+ p->pResultSet = 0;
5945759323
5945859324
if( p->rc==SQLITE_NOMEM ){
5945959325
/* This happens if a malloc() inside a call to sqlite3_column_text() or
5946059326
** sqlite3_column_text16() failed. */
5946159327
db->mallocFailed = 1;
@@ -59606,10 +59472,11 @@
5960659472
pMem->type = SQLITE_NULL;
5960759473
}
5960859474
}
5960959475
5961059476
p->nResColumn = 8 - 4*(p->explain-1);
59477
+ p->pResultSet = &p->aMem[1];
5961159478
p->rc = SQLITE_OK;
5961259479
rc = SQLITE_ROW;
5961359480
}
5961459481
return rc;
5961559482
}
@@ -61361,11 +61228,11 @@
6136161228
** than 2GiB are support - anything large must be database corruption.
6136261229
** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
6136361230
** this code can safely assume that nCellKey is 32-bits
6136461231
*/
6136561232
assert( sqlite3BtreeCursorIsValid(pCur) );
61366
- rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61233
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
6136761234
assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
6136861235
assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
6136961236
6137061237
/* Read in the complete content of the index entry */
6137161238
memset(&m, 0, sizeof(m));
@@ -61436,11 +61303,11 @@
6143661303
int rc;
6143761304
BtCursor *pCur = pC->pCursor;
6143861305
Mem m;
6143961306
6144061307
assert( sqlite3BtreeCursorIsValid(pCur) );
61441
- rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61308
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
6144261309
assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
6144361310
/* nCellKey will always be between 0 and 0xffffffff because of the say
6144461311
** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
6144561312
if( nCellKey<=0 || nCellKey>0x7fffffff ){
6144661313
*res = 0;
@@ -65712,20 +65579,20 @@
6571265579
}else if( u.am.pC->cacheStatus==p->cacheCtr ){
6571365580
u.am.payloadSize = u.am.pC->payloadSize;
6571465581
u.am.zRec = (char*)u.am.pC->aRow;
6571565582
}else if( u.am.pC->isIndex ){
6571665583
assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65717
- rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
65584
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
6571865585
assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
6571965586
/* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
6572065587
** payload size, so it is impossible for u.am.payloadSize64 to be
6572165588
** larger than 32 bits. */
6572265589
assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
6572365590
u.am.payloadSize = (u32)u.am.payloadSize64;
6572465591
}else{
6572565592
assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65726
- rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65593
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
6572765594
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6572865595
}
6572965596
}else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
6573065597
u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
6573165598
assert( u.am.pReg->flags & MEM_Blob );
@@ -67773,18 +67640,18 @@
6777367640
rc = sqlite3VdbeCursorMoveto(u.bk.pC);
6777467641
if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
6777567642
6777667643
if( u.bk.pC->isIndex ){
6777767644
assert( !u.bk.pC->isTable );
67778
- rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67645
+ VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
6777967646
assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
6778067647
if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
6778167648
goto too_big;
6778267649
}
6778367650
u.bk.n = (u32)u.bk.n64;
6778467651
}else{
67785
- rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67652
+ VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
6778667653
assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
6778767654
if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
6778867655
goto too_big;
6778967656
}
6779067657
}
@@ -73417,11 +73284,12 @@
7341773284
pNew->flags |= EP_IntValue;
7341873285
pNew->u.iValue = iValue;
7341973286
}else{
7342073287
int c;
7342173288
pNew->u.zToken = (char*)&pNew[1];
73422
- memcpy(pNew->u.zToken, pToken->z, pToken->n);
73289
+ assert( pToken->z!=0 || pToken->n==0 );
73290
+ if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
7342373291
pNew->u.zToken[pToken->n] = 0;
7342473292
if( dequote && nExtra>=3
7342573293
&& ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
7342673294
sqlite3Dequote(pNew->u.zToken);
7342773295
if( c=='"' ) pNew->flags |= EP_DblQuoted;
@@ -74456,15 +74324,23 @@
7445674324
** ephemeral table.
7445774325
*/
7445874326
p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
7445974327
if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
7446074328
sqlite3 *db = pParse->db; /* Database connection */
74461
- Expr *pExpr = p->pEList->a[0].pExpr; /* Expression <column> */
74462
- int iCol = pExpr->iColumn; /* Index of column <column> */
7446374329
Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
74464
- Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */
74330
+ Table *pTab; /* Table <table>. */
74331
+ Expr *pExpr; /* Expression <column> */
74332
+ int iCol; /* Index of column <column> */
7446574333
int iDb; /* Database idx for pTab */
74334
+
74335
+ assert( p ); /* Because of isCandidateForInOpt(p) */
74336
+ assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
74337
+ assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
74338
+ assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
74339
+ pTab = p->pSrc->a[0].pTab;
74340
+ pExpr = p->pEList->a[0].pExpr;
74341
+ iCol = pExpr->iColumn;
7446674342
7446774343
/* Code an OP_VerifyCookie and OP_TableLock for <table>. */
7446874344
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
7446974345
sqlite3CodeVerifySchema(pParse, iDb);
7447074346
sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
@@ -76467,11 +76343,11 @@
7646776343
if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
7646876344
return 2;
7646976345
}
7647076346
}else if( pA->op!=TK_COLUMN && pA->u.zToken ){
7647176347
if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
76472
- if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
76348
+ if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
7647376349
return 2;
7647476350
}
7647576351
}
7647676352
if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
7647776353
if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
@@ -77610,10 +77486,112 @@
7761077486
** May you find forgiveness for yourself and forgive others.
7761177487
** May you share freely, never taking more than you give.
7761277488
**
7761377489
*************************************************************************
7761477490
** This file contains code associated with the ANALYZE command.
77491
+**
77492
+** The ANALYZE command gather statistics about the content of tables
77493
+** and indices. These statistics are made available to the query planner
77494
+** to help it make better decisions about how to perform queries.
77495
+**
77496
+** The following system tables are or have been supported:
77497
+**
77498
+** CREATE TABLE sqlite_stat1(tbl, idx, stat);
77499
+** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
77500
+** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
77501
+**
77502
+** Additional tables might be added in future releases of SQLite.
77503
+** The sqlite_stat2 table is not created or used unless the SQLite version
77504
+** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
77505
+** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
77506
+** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
77507
+** created and used by SQLite versions 3.7.9 and later and with
77508
+** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
77509
+** is a superset of sqlite_stat2.
77510
+**
77511
+** Format of sqlite_stat1:
77512
+**
77513
+** There is normally one row per index, with the index identified by the
77514
+** name in the idx column. The tbl column is the name of the table to
77515
+** which the index belongs. In each such row, the stat column will be
77516
+** a string consisting of a list of integers. The first integer in this
77517
+** list is the number of rows in the index and in the table. The second
77518
+** integer is the average number of rows in the index that have the same
77519
+** value in the first column of the index. The third integer is the average
77520
+** number of rows in the index that have the same value for the first two
77521
+** columns. The N-th integer (for N>1) is the average number of rows in
77522
+** the index which have the same value for the first N-1 columns. For
77523
+** a K-column index, there will be K+1 integers in the stat column. If
77524
+** the index is unique, then the last integer will be 1.
77525
+**
77526
+** The list of integers in the stat column can optionally be followed
77527
+** by the keyword "unordered". The "unordered" keyword, if it is present,
77528
+** must be separated from the last integer by a single space. If the
77529
+** "unordered" keyword is present, then the query planner assumes that
77530
+** the index is unordered and will not use the index for a range query.
77531
+**
77532
+** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
77533
+** column contains a single integer which is the (estimated) number of
77534
+** rows in the table identified by sqlite_stat1.tbl.
77535
+**
77536
+** Format of sqlite_stat2:
77537
+**
77538
+** The sqlite_stat2 is only created and is only used if SQLite is compiled
77539
+** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
77540
+** 3.6.18 and 3.7.8. The "stat2" table contains additional information
77541
+** about the distribution of keys within an index. The index is identified by
77542
+** the "idx" column and the "tbl" column is the name of the table to which
77543
+** the index belongs. There are usually 10 rows in the sqlite_stat2
77544
+** table for each index.
77545
+**
77546
+** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
77547
+** inclusive are samples of the left-most key value in the index taken at
77548
+** evenly spaced points along the index. Let the number of samples be S
77549
+** (10 in the standard build) and let C be the number of rows in the index.
77550
+** Then the sampled rows are given by:
77551
+**
77552
+** rownumber = (i*C*2 + C)/(S*2)
77553
+**
77554
+** For i between 0 and S-1. Conceptually, the index space is divided into
77555
+** S uniform buckets and the samples are the middle row from each bucket.
77556
+**
77557
+** The format for sqlite_stat2 is recorded here for legacy reference. This
77558
+** version of SQLite does not support sqlite_stat2. It neither reads nor
77559
+** writes the sqlite_stat2 table. This version of SQLite only supports
77560
+** sqlite_stat3.
77561
+**
77562
+** Format for sqlite_stat3:
77563
+**
77564
+** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
77565
+** used to avoid compatibility problems.
77566
+**
77567
+** The format of the sqlite_stat3 table is similar to the format of
77568
+** the sqlite_stat2 table. There are multiple entries for each index.
77569
+** The idx column names the index and the tbl column is the table of the
77570
+** index. If the idx and tbl columns are the same, then the sample is
77571
+** of the INTEGER PRIMARY KEY. The sample column is a value taken from
77572
+** the left-most column of the index. The nEq column is the approximate
77573
+** number of entires in the index whose left-most column exactly matches
77574
+** the sample. nLt is the approximate number of entires whose left-most
77575
+** column is less than the sample. The nDLt column is the approximate
77576
+** number of distinct left-most entries in the index that are less than
77577
+** the sample.
77578
+**
77579
+** Future versions of SQLite might change to store a string containing
77580
+** multiple integers values in the nDLt column of sqlite_stat3. The first
77581
+** integer will be the number of prior index entires that are distinct in
77582
+** the left-most column. The second integer will be the number of prior index
77583
+** entries that are distinct in the first two columns. The third integer
77584
+** will be the number of prior index entries that are distinct in the first
77585
+** three columns. And so forth. With that extension, the nDLt field is
77586
+** similar in function to the sqlite_stat1.stat field.
77587
+**
77588
+** There can be an arbitrary number of sqlite_stat3 entries per index.
77589
+** The ANALYZE command will typically generate sqlite_stat3 tables
77590
+** that contain between 10 and 40 samples which are distributed across
77591
+** the key space, though not uniformly, and which include samples with
77592
+** largest possible nEq values.
7761577593
*/
7761677594
#ifndef SQLITE_OMIT_ANALYZE
7761777595
7761877596
/*
7761977597
** This routine generates code that opens the sqlite_stat1 table for
@@ -77641,12 +77619,12 @@
7764177619
static const struct {
7764277620
const char *zName;
7764377621
const char *zCols;
7764477622
} aTable[] = {
7764577623
{ "sqlite_stat1", "tbl,idx,stat" },
77646
-#ifdef SQLITE_ENABLE_STAT2
77647
- { "sqlite_stat2", "tbl,idx,sampleno,sample" },
77624
+#ifdef SQLITE_ENABLE_STAT3
77625
+ { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
7764877626
#endif
7764977627
};
7765077628
7765177629
int aRoot[] = {0, 0};
7765277630
u8 aCreateTbl[] = {0, 0};
@@ -77658,10 +77636,13 @@
7765877636
if( v==0 ) return;
7765977637
assert( sqlite3BtreeHoldsAllMutexes(db) );
7766077638
assert( sqlite3VdbeDb(v)==db );
7766177639
pDb = &db->aDb[iDb];
7766277640
77641
+ /* Create new statistic tables if they do not exist, or clear them
77642
+ ** if they do already exist.
77643
+ */
7766377644
for(i=0; i<ArraySize(aTable); i++){
7766477645
const char *zTab = aTable[i].zName;
7766577646
Table *pStat;
7766677647
if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
7766777648
/* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -77688,17 +77669,237 @@
7768877669
sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
7768977670
}
7769077671
}
7769177672
}
7769277673
77693
- /* Open the sqlite_stat[12] tables for writing. */
77674
+ /* Open the sqlite_stat[13] tables for writing. */
7769477675
for(i=0; i<ArraySize(aTable); i++){
7769577676
sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
7769677677
sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
7769777678
sqlite3VdbeChangeP5(v, aCreateTbl[i]);
7769877679
}
7769977680
}
77681
+
77682
+/*
77683
+** Recommended number of samples for sqlite_stat3
77684
+*/
77685
+#ifndef SQLITE_STAT3_SAMPLES
77686
+# define SQLITE_STAT3_SAMPLES 24
77687
+#endif
77688
+
77689
+/*
77690
+** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
77691
+** share an instance of the following structure to hold their state
77692
+** information.
77693
+*/
77694
+typedef struct Stat3Accum Stat3Accum;
77695
+struct Stat3Accum {
77696
+ tRowcnt nRow; /* Number of rows in the entire table */
77697
+ tRowcnt nPSample; /* How often to do a periodic sample */
77698
+ int iMin; /* Index of entry with minimum nEq and hash */
77699
+ int mxSample; /* Maximum number of samples to accumulate */
77700
+ int nSample; /* Current number of samples */
77701
+ u32 iPrn; /* Pseudo-random number used for sampling */
77702
+ struct Stat3Sample {
77703
+ i64 iRowid; /* Rowid in main table of the key */
77704
+ tRowcnt nEq; /* sqlite_stat3.nEq */
77705
+ tRowcnt nLt; /* sqlite_stat3.nLt */
77706
+ tRowcnt nDLt; /* sqlite_stat3.nDLt */
77707
+ u8 isPSample; /* True if a periodic sample */
77708
+ u32 iHash; /* Tiebreaker hash */
77709
+ } *a; /* An array of samples */
77710
+};
77711
+
77712
+#ifdef SQLITE_ENABLE_STAT3
77713
+/*
77714
+** Implementation of the stat3_init(C,S) SQL function. The two parameters
77715
+** are the number of rows in the table or index (C) and the number of samples
77716
+** to accumulate (S).
77717
+**
77718
+** This routine allocates the Stat3Accum object.
77719
+**
77720
+** The return value is the Stat3Accum object (P).
77721
+*/
77722
+static void stat3Init(
77723
+ sqlite3_context *context,
77724
+ int argc,
77725
+ sqlite3_value **argv
77726
+){
77727
+ Stat3Accum *p;
77728
+ tRowcnt nRow;
77729
+ int mxSample;
77730
+ int n;
77731
+
77732
+ UNUSED_PARAMETER(argc);
77733
+ nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
77734
+ mxSample = sqlite3_value_int(argv[1]);
77735
+ n = sizeof(*p) + sizeof(p->a[0])*mxSample;
77736
+ p = sqlite3_malloc( n );
77737
+ if( p==0 ){
77738
+ sqlite3_result_error_nomem(context);
77739
+ return;
77740
+ }
77741
+ memset(p, 0, n);
77742
+ p->a = (struct Stat3Sample*)&p[1];
77743
+ p->nRow = nRow;
77744
+ p->mxSample = mxSample;
77745
+ p->nPSample = p->nRow/(mxSample/3+1) + 1;
77746
+ sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
77747
+ sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
77748
+}
77749
+static const FuncDef stat3InitFuncdef = {
77750
+ 2, /* nArg */
77751
+ SQLITE_UTF8, /* iPrefEnc */
77752
+ 0, /* flags */
77753
+ 0, /* pUserData */
77754
+ 0, /* pNext */
77755
+ stat3Init, /* xFunc */
77756
+ 0, /* xStep */
77757
+ 0, /* xFinalize */
77758
+ "stat3_init", /* zName */
77759
+ 0, /* pHash */
77760
+ 0 /* pDestructor */
77761
+};
77762
+
77763
+
77764
+/*
77765
+** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
77766
+** arguments describe a single key instance. This routine makes the
77767
+** decision about whether or not to retain this key for the sqlite_stat3
77768
+** table.
77769
+**
77770
+** The return value is NULL.
77771
+*/
77772
+static void stat3Push(
77773
+ sqlite3_context *context,
77774
+ int argc,
77775
+ sqlite3_value **argv
77776
+){
77777
+ Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
77778
+ tRowcnt nEq = sqlite3_value_int64(argv[0]);
77779
+ tRowcnt nLt = sqlite3_value_int64(argv[1]);
77780
+ tRowcnt nDLt = sqlite3_value_int64(argv[2]);
77781
+ i64 rowid = sqlite3_value_int64(argv[3]);
77782
+ u8 isPSample = 0;
77783
+ u8 doInsert = 0;
77784
+ int iMin = p->iMin;
77785
+ struct Stat3Sample *pSample;
77786
+ int i;
77787
+ u32 h;
77788
+
77789
+ UNUSED_PARAMETER(context);
77790
+ UNUSED_PARAMETER(argc);
77791
+ if( nEq==0 ) return;
77792
+ h = p->iPrn = p->iPrn*1103515245 + 12345;
77793
+ if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
77794
+ doInsert = isPSample = 1;
77795
+ }else if( p->nSample<p->mxSample ){
77796
+ doInsert = 1;
77797
+ }else{
77798
+ if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
77799
+ doInsert = 1;
77800
+ }
77801
+ }
77802
+ if( !doInsert ) return;
77803
+ if( p->nSample==p->mxSample ){
77804
+ assert( p->nSample - iMin - 1 >= 0 );
77805
+ memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
77806
+ pSample = &p->a[p->nSample-1];
77807
+ }else{
77808
+ pSample = &p->a[p->nSample++];
77809
+ }
77810
+ pSample->iRowid = rowid;
77811
+ pSample->nEq = nEq;
77812
+ pSample->nLt = nLt;
77813
+ pSample->nDLt = nDLt;
77814
+ pSample->iHash = h;
77815
+ pSample->isPSample = isPSample;
77816
+
77817
+ /* Find the new minimum */
77818
+ if( p->nSample==p->mxSample ){
77819
+ pSample = p->a;
77820
+ i = 0;
77821
+ while( pSample->isPSample ){
77822
+ i++;
77823
+ pSample++;
77824
+ assert( i<p->nSample );
77825
+ }
77826
+ nEq = pSample->nEq;
77827
+ h = pSample->iHash;
77828
+ iMin = i;
77829
+ for(i++, pSample++; i<p->nSample; i++, pSample++){
77830
+ if( pSample->isPSample ) continue;
77831
+ if( pSample->nEq<nEq
77832
+ || (pSample->nEq==nEq && pSample->iHash<h)
77833
+ ){
77834
+ iMin = i;
77835
+ nEq = pSample->nEq;
77836
+ h = pSample->iHash;
77837
+ }
77838
+ }
77839
+ p->iMin = iMin;
77840
+ }
77841
+}
77842
+static const FuncDef stat3PushFuncdef = {
77843
+ 5, /* nArg */
77844
+ SQLITE_UTF8, /* iPrefEnc */
77845
+ 0, /* flags */
77846
+ 0, /* pUserData */
77847
+ 0, /* pNext */
77848
+ stat3Push, /* xFunc */
77849
+ 0, /* xStep */
77850
+ 0, /* xFinalize */
77851
+ "stat3_push", /* zName */
77852
+ 0, /* pHash */
77853
+ 0 /* pDestructor */
77854
+};
77855
+
77856
+/*
77857
+** Implementation of the stat3_get(P,N,...) SQL function. This routine is
77858
+** used to query the results. Content is returned for the Nth sqlite_stat3
77859
+** row where N is between 0 and S-1 and S is the number of samples. The
77860
+** value returned depends on the number of arguments.
77861
+**
77862
+** argc==2 result: rowid
77863
+** argc==3 result: nEq
77864
+** argc==4 result: nLt
77865
+** argc==5 result: nDLt
77866
+*/
77867
+static void stat3Get(
77868
+ sqlite3_context *context,
77869
+ int argc,
77870
+ sqlite3_value **argv
77871
+){
77872
+ int n = sqlite3_value_int(argv[1]);
77873
+ Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
77874
+
77875
+ assert( p!=0 );
77876
+ if( p->nSample<=n ) return;
77877
+ switch( argc ){
77878
+ case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
77879
+ case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
77880
+ case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
77881
+ default: sqlite3_result_int64(context, p->a[n].nDLt); break;
77882
+ }
77883
+}
77884
+static const FuncDef stat3GetFuncdef = {
77885
+ -1, /* nArg */
77886
+ SQLITE_UTF8, /* iPrefEnc */
77887
+ 0, /* flags */
77888
+ 0, /* pUserData */
77889
+ 0, /* pNext */
77890
+ stat3Get, /* xFunc */
77891
+ 0, /* xStep */
77892
+ 0, /* xFinalize */
77893
+ "stat3_get", /* zName */
77894
+ 0, /* pHash */
77895
+ 0 /* pDestructor */
77896
+};
77897
+#endif /* SQLITE_ENABLE_STAT3 */
77898
+
77899
+
77900
+
7770077901
7770177902
/*
7770277903
** Generate code to do an analysis of all indices associated with
7770377904
** a single table.
7770477905
*/
@@ -77718,24 +77919,31 @@
7771877919
int endOfLoop; /* The end of the loop */
7771977920
int jZeroRows = -1; /* Jump from here if number of rows is zero */
7772077921
int iDb; /* Index of database containing pTab */
7772177922
int regTabname = iMem++; /* Register containing table name */
7772277923
int regIdxname = iMem++; /* Register containing index name */
77723
- int regSampleno = iMem++; /* Register containing next sample number */
77724
- int regCol = iMem++; /* Content of a column analyzed table */
77924
+ int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
77925
+#ifdef SQLITE_ENABLE_STAT3
77926
+ int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
77927
+ int regNumLt = iMem++; /* Number of keys less than regSample */
77928
+ int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
77929
+ int regSample = iMem++; /* The next sample value */
77930
+ int regRowid = regSample; /* Rowid of a sample */
77931
+ int regAccum = iMem++; /* Register to hold Stat3Accum object */
77932
+ int regLoop = iMem++; /* Loop counter */
77933
+ int regCount = iMem++; /* Number of rows in the table or index */
77934
+ int regTemp1 = iMem++; /* Intermediate register */
77935
+ int regTemp2 = iMem++; /* Intermediate register */
77936
+ int once = 1; /* One-time initialization */
77937
+ int shortJump = 0; /* Instruction address */
77938
+ int iTabCur = pParse->nTab++; /* Table cursor */
77939
+#endif
77940
+ int regCol = iMem++; /* Content of a column in analyzed table */
7772577941
int regRec = iMem++; /* Register holding completed record */
7772677942
int regTemp = iMem++; /* Temporary use register */
77727
- int regRowid = iMem++; /* Rowid for the inserted record */
77728
-
77729
-#ifdef SQLITE_ENABLE_STAT2
77730
- int addr = 0; /* Instruction address */
77731
- int regTemp2 = iMem++; /* Temporary use register */
77732
- int regSamplerecno = iMem++; /* Index of next sample to record */
77733
- int regRecno = iMem++; /* Current sample index */
77734
- int regLast = iMem++; /* Index of last sample to record */
77735
- int regFirst = iMem++; /* Index of first sample to record */
77736
-#endif
77943
+ int regNewRowid = iMem++; /* Rowid for the inserted record */
77944
+
7773777945
7773877946
v = sqlite3GetVdbe(pParse);
7773977947
if( v==0 || NEVER(pTab==0) ){
7774077948
return;
7774177949
}
@@ -77764,13 +77972,18 @@
7776477972
iIdxCur = pParse->nTab++;
7776577973
sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
7776677974
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
7776777975
int nCol;
7776877976
KeyInfo *pKey;
77977
+ int addrIfNot = 0; /* address of OP_IfNot */
77978
+ int *aChngAddr; /* Array of jump instruction addresses */
7776977979
7777077980
if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
77981
+ VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
7777177982
nCol = pIdx->nColumn;
77983
+ aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
77984
+ if( aChngAddr==0 ) continue;
7777277985
pKey = sqlite3IndexKeyinfo(pParse, pIdx);
7777377986
if( iMem+1+(nCol*2)>pParse->nMem ){
7777477987
pParse->nMem = iMem+1+(nCol*2);
7777577988
}
7777677989
@@ -77781,35 +77994,24 @@
7778177994
VdbeComment((v, "%s", pIdx->zName));
7778277995
7778377996
/* Populate the register containing the index name. */
7778477997
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
7778577998
77786
-#ifdef SQLITE_ENABLE_STAT2
77787
-
77788
- /* If this iteration of the loop is generating code to analyze the
77789
- ** first index in the pTab->pIndex list, then register regLast has
77790
- ** not been populated. In this case populate it now. */
77791
- if( pTab->pIndex==pIdx ){
77792
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77793
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77794
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77795
-
77796
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77797
- sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77798
- addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77799
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77800
- sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77801
- sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77802
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77803
- sqlite3VdbeJumpHere(v, addr);
77804
- }
77805
-
77806
- /* Zero the regSampleno and regRecno registers. */
77807
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77808
- sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77809
- sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77810
-#endif
77999
+#ifdef SQLITE_ENABLE_STAT3
78000
+ if( once ){
78001
+ once = 0;
78002
+ sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
78003
+ }
78004
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
78005
+ sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
78006
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
78007
+ sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
78008
+ sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
78009
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
78010
+ (char*)&stat3InitFuncdef, P4_FUNCDEF);
78011
+ sqlite3VdbeChangeP5(v, 2);
78012
+#endif /* SQLITE_ENABLE_STAT3 */
7781178013
7781278014
/* The block of memory cells initialized here is used as follows.
7781378015
**
7781478016
** iMem:
7781578017
** The total number of rows in the table.
@@ -77835,79 +78037,87 @@
7783578037
/* Start the analysis loop. This loop runs through all the entries in
7783678038
** the index b-tree. */
7783778039
endOfLoop = sqlite3VdbeMakeLabel(v);
7783878040
sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
7783978041
topOfLoop = sqlite3VdbeCurrentAddr(v);
77840
- sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
78042
+ sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
7784178043
7784278044
for(i=0; i<nCol; i++){
7784378045
CollSeq *pColl;
7784478046
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
7784578047
if( i==0 ){
77846
-#ifdef SQLITE_ENABLE_STAT2
77847
- /* Check if the record that cursor iIdxCur points to contains a
77848
- ** value that should be stored in the sqlite_stat2 table. If so,
77849
- ** store it. */
77850
- int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77851
- assert( regTabname+1==regIdxname
77852
- && regTabname+2==regSampleno
77853
- && regTabname+3==regCol
77854
- );
77855
- sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77856
- sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77857
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77858
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77859
-
77860
- /* Calculate new values for regSamplerecno and regSampleno.
77861
- **
77862
- ** sampleno = sampleno + 1
77863
- ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77864
- */
77865
- sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77866
- sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77867
- sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77868
- sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77869
- sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77870
- sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77871
- sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77872
-
77873
- sqlite3VdbeJumpHere(v, ne);
77874
- sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77875
-#endif
77876
-
7787778048
/* Always record the very first row */
77878
- sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
78049
+ addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
7787978050
}
7788078051
assert( pIdx->azColl!=0 );
7788178052
assert( pIdx->azColl[i]!=0 );
7788278053
pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
77883
- sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77884
- (char*)pColl, P4_COLLSEQ);
78054
+ aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
78055
+ (char*)pColl, P4_COLLSEQ);
7788578056
sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
77886
- }
77887
- if( db->mallocFailed ){
77888
- /* If a malloc failure has occurred, then the result of the expression
77889
- ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77890
- ** below may be negative. Which causes an assert() to fail (or an
77891
- ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77892
- return;
78057
+ VdbeComment((v, "jump if column %d changed", i));
78058
+#ifdef SQLITE_ENABLE_STAT3
78059
+ if( i==0 ){
78060
+ sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
78061
+ VdbeComment((v, "incr repeat count"));
78062
+ }
78063
+#endif
7789378064
}
7789478065
sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
7789578066
for(i=0; i<nCol; i++){
77896
- int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
78067
+ sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
7789778068
if( i==0 ){
77898
- sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
78069
+ sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
78070
+#ifdef SQLITE_ENABLE_STAT3
78071
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
78072
+ (char*)&stat3PushFuncdef, P4_FUNCDEF);
78073
+ sqlite3VdbeChangeP5(v, 5);
78074
+ sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
78075
+ sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
78076
+ sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
78077
+ sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
78078
+#endif
7789978079
}
77900
- sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
7790178080
sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
7790278081
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
7790378082
}
78083
+ sqlite3DbFree(db, aChngAddr);
7790478084
77905
- /* End of the analysis loop. */
78085
+ /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
7790678086
sqlite3VdbeResolveLabel(v, endOfLoop);
78087
+
7790778088
sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
7790878089
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
78090
+#ifdef SQLITE_ENABLE_STAT3
78091
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
78092
+ (char*)&stat3PushFuncdef, P4_FUNCDEF);
78093
+ sqlite3VdbeChangeP5(v, 5);
78094
+ sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
78095
+ shortJump =
78096
+ sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
78097
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
78098
+ (char*)&stat3GetFuncdef, P4_FUNCDEF);
78099
+ sqlite3VdbeChangeP5(v, 2);
78100
+ sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
78101
+ sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
78102
+ sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
78103
+ sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
78104
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
78105
+ (char*)&stat3GetFuncdef, P4_FUNCDEF);
78106
+ sqlite3VdbeChangeP5(v, 3);
78107
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
78108
+ (char*)&stat3GetFuncdef, P4_FUNCDEF);
78109
+ sqlite3VdbeChangeP5(v, 4);
78110
+ sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
78111
+ (char*)&stat3GetFuncdef, P4_FUNCDEF);
78112
+ sqlite3VdbeChangeP5(v, 5);
78113
+ sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
78114
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
78115
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
78116
+ sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
78117
+ sqlite3VdbeJumpHere(v, shortJump+2);
78118
+#endif
7790978119
7791078120
/* Store the results in sqlite_stat1.
7791178121
**
7791278122
** The result is a single row of the sqlite_stat1 table. The first
7791378123
** two columns are the names of the table and index. The third column
@@ -77923,50 +78133,51 @@
7792378133
**
7792478134
** If K==0 then no entry is made into the sqlite_stat1 table.
7792578135
** If K>0 then it is always the case the D>0 so division by zero
7792678136
** is never possible.
7792778137
*/
77928
- sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
78138
+ sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
7792978139
if( jZeroRows<0 ){
7793078140
jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
7793178141
}
7793278142
for(i=0; i<nCol; i++){
7793378143
sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
77934
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
78144
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
7793578145
sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
7793678146
sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
7793778147
sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
7793878148
sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
77939
- sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
78149
+ sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
7794078150
}
7794178151
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77942
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77943
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
78152
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
78153
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
7794478154
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7794578155
}
7794678156
7794778157
/* If the table has no indices, create a single sqlite_stat1 entry
7794878158
** containing NULL as the index name and the row count as the content.
7794978159
*/
7795078160
if( pTab->pIndex==0 ){
7795178161
sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
7795278162
VdbeComment((v, "%s", pTab->zName));
77953
- sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
78163
+ sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
7795478164
sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77955
- jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
78165
+ jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
7795678166
}else{
7795778167
sqlite3VdbeJumpHere(v, jZeroRows);
7795878168
jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
7795978169
}
7796078170
sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
7796178171
sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77962
- sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77963
- sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
78172
+ sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
78173
+ sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
7796478174
sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
7796578175
if( pParse->nMem<regRec ) pParse->nMem = regRec;
7796678176
sqlite3VdbeJumpHere(v, jZeroRows);
7796778177
}
78178
+
7796878179
7796978180
/*
7797078181
** Generate code that will cause the most recent index analysis to
7797178182
** be loaded into internal hash tables where is can be used.
7797278183
*/
@@ -77987,11 +78198,11 @@
7798778198
int iStatCur;
7798878199
int iMem;
7798978200
7799078201
sqlite3BeginWriteOperation(pParse, 0, iDb);
7799178202
iStatCur = pParse->nTab;
77992
- pParse->nTab += 2;
78203
+ pParse->nTab += 3;
7799378204
openStatTable(pParse, iDb, iStatCur, 0, 0);
7799478205
iMem = pParse->nMem+1;
7799578206
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7799678207
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
7799778208
Table *pTab = (Table*)sqliteHashData(k);
@@ -78012,11 +78223,11 @@
7801278223
assert( pTab!=0 );
7801378224
assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
7801478225
iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
7801578226
sqlite3BeginWriteOperation(pParse, 0, iDb);
7801678227
iStatCur = pParse->nTab;
78017
- pParse->nTab += 2;
78228
+ pParse->nTab += 3;
7801878229
if( pOnlyIdx ){
7801978230
openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
7802078231
}else{
7802178232
openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
7802278233
}
@@ -78117,11 +78328,11 @@
7811778328
static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
7811878329
analysisInfo *pInfo = (analysisInfo*)pData;
7811978330
Index *pIndex;
7812078331
Table *pTable;
7812178332
int i, c, n;
78122
- unsigned int v;
78333
+ tRowcnt v;
7812378334
const char *z;
7812478335
7812578336
assert( argc==3 );
7812678337
UNUSED_PARAMETER2(NotUsed, argc);
7812778338
@@ -78160,40 +78371,172 @@
7816078371
/*
7816178372
** If the Index.aSample variable is not NULL, delete the aSample[] array
7816278373
** and its contents.
7816378374
*/
7816478375
SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
78165
-#ifdef SQLITE_ENABLE_STAT2
78376
+#ifdef SQLITE_ENABLE_STAT3
7816678377
if( pIdx->aSample ){
7816778378
int j;
78168
- for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
78379
+ for(j=0; j<pIdx->nSample; j++){
7816978380
IndexSample *p = &pIdx->aSample[j];
7817078381
if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
7817178382
sqlite3DbFree(db, p->u.z);
7817278383
}
7817378384
}
7817478385
sqlite3DbFree(db, pIdx->aSample);
78386
+ }
78387
+ if( db && db->pnBytesFreed==0 ){
78388
+ pIdx->nSample = 0;
78389
+ pIdx->aSample = 0;
7817578390
}
7817678391
#else
7817778392
UNUSED_PARAMETER(db);
7817878393
UNUSED_PARAMETER(pIdx);
7817978394
#endif
7818078395
}
7818178396
78397
+#ifdef SQLITE_ENABLE_STAT3
7818278398
/*
78183
-** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
78399
+** Load content from the sqlite_stat3 table into the Index.aSample[]
78400
+** arrays of all indices.
78401
+*/
78402
+static int loadStat3(sqlite3 *db, const char *zDb){
78403
+ int rc; /* Result codes from subroutines */
78404
+ sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
78405
+ char *zSql; /* Text of the SQL statement */
78406
+ Index *pPrevIdx = 0; /* Previous index in the loop */
78407
+ int idx = 0; /* slot in pIdx->aSample[] for next sample */
78408
+ int eType; /* Datatype of a sample */
78409
+ IndexSample *pSample; /* A slot in pIdx->aSample[] */
78410
+
78411
+ if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
78412
+ return SQLITE_OK;
78413
+ }
78414
+
78415
+ zSql = sqlite3MPrintf(db,
78416
+ "SELECT idx,count(*) FROM %Q.sqlite_stat3"
78417
+ " GROUP BY idx", zDb);
78418
+ if( !zSql ){
78419
+ return SQLITE_NOMEM;
78420
+ }
78421
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78422
+ sqlite3DbFree(db, zSql);
78423
+ if( rc ) return rc;
78424
+
78425
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
78426
+ char *zIndex; /* Index name */
78427
+ Index *pIdx; /* Pointer to the index object */
78428
+ int nSample; /* Number of samples */
78429
+
78430
+ zIndex = (char *)sqlite3_column_text(pStmt, 0);
78431
+ if( zIndex==0 ) continue;
78432
+ nSample = sqlite3_column_int(pStmt, 1);
78433
+ pIdx = sqlite3FindIndex(db, zIndex, zDb);
78434
+ if( pIdx==0 ) continue;
78435
+ assert( pIdx->nSample==0 );
78436
+ pIdx->nSample = nSample;
78437
+ pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
78438
+ pIdx->avgEq = pIdx->aiRowEst[1];
78439
+ if( pIdx->aSample==0 ){
78440
+ db->mallocFailed = 1;
78441
+ sqlite3_finalize(pStmt);
78442
+ return SQLITE_NOMEM;
78443
+ }
78444
+ }
78445
+ rc = sqlite3_finalize(pStmt);
78446
+ if( rc ) return rc;
78447
+
78448
+ zSql = sqlite3MPrintf(db,
78449
+ "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
78450
+ if( !zSql ){
78451
+ return SQLITE_NOMEM;
78452
+ }
78453
+ rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78454
+ sqlite3DbFree(db, zSql);
78455
+ if( rc ) return rc;
78456
+
78457
+ while( sqlite3_step(pStmt)==SQLITE_ROW ){
78458
+ char *zIndex; /* Index name */
78459
+ Index *pIdx; /* Pointer to the index object */
78460
+ int i; /* Loop counter */
78461
+ tRowcnt sumEq; /* Sum of the nEq values */
78462
+
78463
+ zIndex = (char *)sqlite3_column_text(pStmt, 0);
78464
+ if( zIndex==0 ) continue;
78465
+ pIdx = sqlite3FindIndex(db, zIndex, zDb);
78466
+ if( pIdx==0 ) continue;
78467
+ if( pIdx==pPrevIdx ){
78468
+ idx++;
78469
+ }else{
78470
+ pPrevIdx = pIdx;
78471
+ idx = 0;
78472
+ }
78473
+ assert( idx<pIdx->nSample );
78474
+ pSample = &pIdx->aSample[idx];
78475
+ pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
78476
+ pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
78477
+ pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
78478
+ if( idx==pIdx->nSample-1 ){
78479
+ if( pSample->nDLt>0 ){
78480
+ for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
78481
+ pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
78482
+ }
78483
+ if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
78484
+ }
78485
+ eType = sqlite3_column_type(pStmt, 4);
78486
+ pSample->eType = (u8)eType;
78487
+ switch( eType ){
78488
+ case SQLITE_INTEGER: {
78489
+ pSample->u.i = sqlite3_column_int64(pStmt, 4);
78490
+ break;
78491
+ }
78492
+ case SQLITE_FLOAT: {
78493
+ pSample->u.r = sqlite3_column_double(pStmt, 4);
78494
+ break;
78495
+ }
78496
+ case SQLITE_NULL: {
78497
+ break;
78498
+ }
78499
+ default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
78500
+ const char *z = (const char *)(
78501
+ (eType==SQLITE_BLOB) ?
78502
+ sqlite3_column_blob(pStmt, 4):
78503
+ sqlite3_column_text(pStmt, 4)
78504
+ );
78505
+ int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
78506
+ pSample->nByte = n;
78507
+ if( n < 1){
78508
+ pSample->u.z = 0;
78509
+ }else{
78510
+ pSample->u.z = sqlite3Malloc(n);
78511
+ if( pSample->u.z==0 ){
78512
+ db->mallocFailed = 1;
78513
+ sqlite3_finalize(pStmt);
78514
+ return SQLITE_NOMEM;
78515
+ }
78516
+ memcpy(pSample->u.z, z, n);
78517
+ }
78518
+ }
78519
+ }
78520
+ }
78521
+ return sqlite3_finalize(pStmt);
78522
+}
78523
+#endif /* SQLITE_ENABLE_STAT3 */
78524
+
78525
+/*
78526
+** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
7818478527
** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
78185
-** arrays. The contents of sqlite_stat2 are used to populate the
78528
+** arrays. The contents of sqlite_stat3 are used to populate the
7818678529
** Index.aSample[] arrays.
7818778530
**
7818878531
** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
78189
-** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
78190
-** during compilation and the sqlite_stat2 table is present, no data is
78532
+** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
78533
+** during compilation and the sqlite_stat3 table is present, no data is
7819178534
** read from it.
7819278535
**
78193
-** If SQLITE_ENABLE_STAT2 was defined during compilation and the
78194
-** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
78536
+** If SQLITE_ENABLE_STAT3 was defined during compilation and the
78537
+** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
7819578538
** returned. However, in this case, data is read from the sqlite_stat1
7819678539
** table (if it is present) before returning.
7819778540
**
7819878541
** If an OOM error occurs, this function always sets db->mallocFailed.
7819978542
** This means if the caller does not care about other errors, the return
@@ -78211,12 +78554,14 @@
7821178554
/* Clear any prior statistics */
7821278555
assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
7821378556
for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
7821478557
Index *pIdx = sqliteHashData(i);
7821578558
sqlite3DefaultRowEst(pIdx);
78559
+#ifdef SQLITE_ENABLE_STAT3
7821678560
sqlite3DeleteIndexSamples(db, pIdx);
7821778561
pIdx->aSample = 0;
78562
+#endif
7821878563
}
7821978564
7822078565
/* Check to make sure the sqlite_stat1 table exists */
7822178566
sInfo.db = db;
7822278567
sInfo.zDatabase = db->aDb[iDb].zName;
@@ -78224,91 +78569,23 @@
7822478569
return SQLITE_ERROR;
7822578570
}
7822678571
7822778572
/* Load new statistics out of the sqlite_stat1 table */
7822878573
zSql = sqlite3MPrintf(db,
78229
- "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78574
+ "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
7823078575
if( zSql==0 ){
7823178576
rc = SQLITE_NOMEM;
7823278577
}else{
7823378578
rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
7823478579
sqlite3DbFree(db, zSql);
7823578580
}
7823678581
7823778582
78238
- /* Load the statistics from the sqlite_stat2 table. */
78239
-#ifdef SQLITE_ENABLE_STAT2
78240
- if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78241
- rc = SQLITE_ERROR;
78242
- }
78243
- if( rc==SQLITE_OK ){
78244
- sqlite3_stmt *pStmt = 0;
78245
-
78246
- zSql = sqlite3MPrintf(db,
78247
- "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78248
- if( !zSql ){
78249
- rc = SQLITE_NOMEM;
78250
- }else{
78251
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78252
- sqlite3DbFree(db, zSql);
78253
- }
78254
-
78255
- if( rc==SQLITE_OK ){
78256
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
78257
- char *zIndex; /* Index name */
78258
- Index *pIdx; /* Pointer to the index object */
78259
-
78260
- zIndex = (char *)sqlite3_column_text(pStmt, 0);
78261
- pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78262
- if( pIdx ){
78263
- int iSample = sqlite3_column_int(pStmt, 1);
78264
- if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78265
- int eType = sqlite3_column_type(pStmt, 2);
78266
-
78267
- if( pIdx->aSample==0 ){
78268
- static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78269
- pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78270
- if( pIdx->aSample==0 ){
78271
- db->mallocFailed = 1;
78272
- break;
78273
- }
78274
- memset(pIdx->aSample, 0, sz);
78275
- }
78276
-
78277
- assert( pIdx->aSample );
78278
- {
78279
- IndexSample *pSample = &pIdx->aSample[iSample];
78280
- pSample->eType = (u8)eType;
78281
- if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78282
- pSample->u.r = sqlite3_column_double(pStmt, 2);
78283
- }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78284
- const char *z = (const char *)(
78285
- (eType==SQLITE_BLOB) ?
78286
- sqlite3_column_blob(pStmt, 2):
78287
- sqlite3_column_text(pStmt, 2)
78288
- );
78289
- int n = sqlite3_column_bytes(pStmt, 2);
78290
- if( n>24 ){
78291
- n = 24;
78292
- }
78293
- pSample->nByte = (u8)n;
78294
- if( n < 1){
78295
- pSample->u.z = 0;
78296
- }else{
78297
- pSample->u.z = sqlite3DbStrNDup(0, z, n);
78298
- if( pSample->u.z==0 ){
78299
- db->mallocFailed = 1;
78300
- break;
78301
- }
78302
- }
78303
- }
78304
- }
78305
- }
78306
- }
78307
- }
78308
- rc = sqlite3_finalize(pStmt);
78309
- }
78583
+ /* Load the statistics from the sqlite_stat3 table. */
78584
+#ifdef SQLITE_ENABLE_STAT3
78585
+ if( rc==SQLITE_OK ){
78586
+ rc = loadStat3(db, sInfo.zDatabase);
7831078587
}
7831178588
#endif
7831278589
7831378590
if( rc==SQLITE_NOMEM ){
7831478591
db->mallocFailed = 1;
@@ -81120,11 +81397,15 @@
8112081397
Parse *pParse, /* The parsing context */
8112181398
int iDb, /* The database number */
8112281399
const char *zType, /* "idx" or "tbl" */
8112381400
const char *zName /* Name of index or table */
8112481401
){
81125
- static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
81402
+ static const char *azStatTab[] = {
81403
+ "sqlite_stat1",
81404
+ "sqlite_stat2",
81405
+ "sqlite_stat3",
81406
+ };
8112681407
int i;
8112781408
const char *zDbName = pParse->db->aDb[iDb].zName;
8112881409
for(i=0; i<ArraySize(azStatTab); i++){
8112981410
if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
8113081411
sqlite3NestedParse(pParse,
@@ -81132,10 +81413,80 @@
8113281413
zDbName, azStatTab[i], zType, zName
8113381414
);
8113481415
}
8113581416
}
8113681417
}
81418
+
81419
+/*
81420
+** Generate code to drop a table.
81421
+*/
81422
+SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
81423
+ Vdbe *v;
81424
+ sqlite3 *db = pParse->db;
81425
+ Trigger *pTrigger;
81426
+ Db *pDb = &db->aDb[iDb];
81427
+
81428
+ v = sqlite3GetVdbe(pParse);
81429
+ assert( v!=0 );
81430
+ sqlite3BeginWriteOperation(pParse, 1, iDb);
81431
+
81432
+#ifndef SQLITE_OMIT_VIRTUALTABLE
81433
+ if( IsVirtual(pTab) ){
81434
+ sqlite3VdbeAddOp0(v, OP_VBegin);
81435
+ }
81436
+#endif
81437
+
81438
+ /* Drop all triggers associated with the table being dropped. Code
81439
+ ** is generated to remove entries from sqlite_master and/or
81440
+ ** sqlite_temp_master if required.
81441
+ */
81442
+ pTrigger = sqlite3TriggerList(pParse, pTab);
81443
+ while( pTrigger ){
81444
+ assert( pTrigger->pSchema==pTab->pSchema ||
81445
+ pTrigger->pSchema==db->aDb[1].pSchema );
81446
+ sqlite3DropTriggerPtr(pParse, pTrigger);
81447
+ pTrigger = pTrigger->pNext;
81448
+ }
81449
+
81450
+#ifndef SQLITE_OMIT_AUTOINCREMENT
81451
+ /* Remove any entries of the sqlite_sequence table associated with
81452
+ ** the table being dropped. This is done before the table is dropped
81453
+ ** at the btree level, in case the sqlite_sequence table needs to
81454
+ ** move as a result of the drop (can happen in auto-vacuum mode).
81455
+ */
81456
+ if( pTab->tabFlags & TF_Autoincrement ){
81457
+ sqlite3NestedParse(pParse,
81458
+ "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
81459
+ pDb->zName, pTab->zName
81460
+ );
81461
+ }
81462
+#endif
81463
+
81464
+ /* Drop all SQLITE_MASTER table and index entries that refer to the
81465
+ ** table. The program name loops through the master table and deletes
81466
+ ** every row that refers to a table of the same name as the one being
81467
+ ** dropped. Triggers are handled seperately because a trigger can be
81468
+ ** created in the temp database that refers to a table in another
81469
+ ** database.
81470
+ */
81471
+ sqlite3NestedParse(pParse,
81472
+ "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81473
+ pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81474
+ if( !isView && !IsVirtual(pTab) ){
81475
+ destroyTable(pParse, pTab);
81476
+ }
81477
+
81478
+ /* Remove the table entry from SQLite's internal schema and modify
81479
+ ** the schema cookie.
81480
+ */
81481
+ if( IsVirtual(pTab) ){
81482
+ sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81483
+ }
81484
+ sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81485
+ sqlite3ChangeCookie(pParse, iDb);
81486
+ sqliteViewResetAll(db, iDb);
81487
+}
8113781488
8113881489
/*
8113981490
** This routine is called to do the work of a DROP TABLE statement.
8114081491
** pName is the name of the table to be dropped.
8114181492
*/
@@ -81201,11 +81552,12 @@
8120181552
if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
8120281553
goto exit_drop_table;
8120381554
}
8120481555
}
8120581556
#endif
81206
- if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
81557
+ if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
81558
+ && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
8120781559
sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
8120881560
goto exit_drop_table;
8120981561
}
8121081562
8121181563
#ifndef SQLITE_OMIT_VIEW
@@ -81225,72 +81577,15 @@
8122581577
/* Generate code to remove the table from the master table
8122681578
** on disk.
8122781579
*/
8122881580
v = sqlite3GetVdbe(pParse);
8122981581
if( v ){
81230
- Trigger *pTrigger;
81231
- Db *pDb = &db->aDb[iDb];
8123281582
sqlite3BeginWriteOperation(pParse, 1, iDb);
81233
-
81234
-#ifndef SQLITE_OMIT_VIRTUALTABLE
81235
- if( IsVirtual(pTab) ){
81236
- sqlite3VdbeAddOp0(v, OP_VBegin);
81237
- }
81238
-#endif
81583
+ sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
8123981584
sqlite3FkDropTable(pParse, pName, pTab);
81240
-
81241
- /* Drop all triggers associated with the table being dropped. Code
81242
- ** is generated to remove entries from sqlite_master and/or
81243
- ** sqlite_temp_master if required.
81244
- */
81245
- pTrigger = sqlite3TriggerList(pParse, pTab);
81246
- while( pTrigger ){
81247
- assert( pTrigger->pSchema==pTab->pSchema ||
81248
- pTrigger->pSchema==db->aDb[1].pSchema );
81249
- sqlite3DropTriggerPtr(pParse, pTrigger);
81250
- pTrigger = pTrigger->pNext;
81251
- }
81252
-
81253
-#ifndef SQLITE_OMIT_AUTOINCREMENT
81254
- /* Remove any entries of the sqlite_sequence table associated with
81255
- ** the table being dropped. This is done before the table is dropped
81256
- ** at the btree level, in case the sqlite_sequence table needs to
81257
- ** move as a result of the drop (can happen in auto-vacuum mode).
81258
- */
81259
- if( pTab->tabFlags & TF_Autoincrement ){
81260
- sqlite3NestedParse(pParse,
81261
- "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81262
- pDb->zName, pTab->zName
81263
- );
81264
- }
81265
-#endif
81266
-
81267
- /* Drop all SQLITE_MASTER table and index entries that refer to the
81268
- ** table. The program name loops through the master table and deletes
81269
- ** every row that refers to a table of the same name as the one being
81270
- ** dropped. Triggers are handled seperately because a trigger can be
81271
- ** created in the temp database that refers to a table in another
81272
- ** database.
81273
- */
81274
- sqlite3NestedParse(pParse,
81275
- "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81276
- pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81277
- sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81278
- if( !isView && !IsVirtual(pTab) ){
81279
- destroyTable(pParse, pTab);
81280
- }
81281
-
81282
- /* Remove the table entry from SQLite's internal schema and modify
81283
- ** the schema cookie.
81284
- */
81285
- if( IsVirtual(pTab) ){
81286
- sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81287
- }
81288
- sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81289
- sqlite3ChangeCookie(pParse, iDb);
81290
- }
81291
- sqliteViewResetAll(db, iDb);
81585
+ sqlite3CodeDropTable(pParse, pTab, iDb, isView);
81586
+ }
8129281587
8129381588
exit_drop_table:
8129481589
sqlite3SrcListDelete(db, pName);
8129581590
}
8129681591
@@ -81454,17 +81749,19 @@
8145481749
*/
8145581750
static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
8145681751
Table *pTab = pIndex->pTable; /* The table that is indexed */
8145781752
int iTab = pParse->nTab++; /* Btree cursor used for pTab */
8145881753
int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81459
- int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
81754
+ int iSorter; /* Cursor opened by OpenSorter (if in use) */
8146081755
int addr1; /* Address of top of loop */
8146181756
int addr2; /* Address to jump to for next iteration */
8146281757
int tnum; /* Root page of index */
8146381758
Vdbe *v; /* Generate code into this virtual machine */
8146481759
KeyInfo *pKey; /* KeyInfo for index */
81760
+#ifdef SQLITE_OMIT_MERGE_SORT
8146581761
int regIdxKey; /* Registers containing the index key */
81762
+#endif
8146681763
int regRecord; /* Register holding assemblied index record */
8146781764
sqlite3 *db = pParse->db; /* The database connection */
8146881765
int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
8146981766
8147081767
#ifndef SQLITE_OMIT_AUTHORIZATION
@@ -81494,21 +81791,22 @@
8149481791
8149581792
#ifndef SQLITE_OMIT_MERGE_SORT
8149681793
/* Open the sorter cursor if we are to use one. */
8149781794
iSorter = pParse->nTab++;
8149881795
sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81796
+#else
81797
+ iSorter = iTab;
8149981798
#endif
8150081799
8150181800
/* Open the table. Loop through all rows of the table, inserting index
8150281801
** records into the sorter. */
8150381802
sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
8150481803
addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81505
- addr2 = addr1 + 1;
8150681804
regRecord = sqlite3GetTempReg(pParse);
81507
- regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
8150881805
8150981806
#ifndef SQLITE_OMIT_MERGE_SORT
81807
+ sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
8151081808
sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
8151181809
sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
8151281810
sqlite3VdbeJumpHere(v, addr1);
8151381811
addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
8151481812
if( pIndex->onError!=OE_None ){
@@ -81524,10 +81822,12 @@
8152481822
}
8152581823
sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
8152681824
sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
8152781825
sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
8152881826
#else
81827
+ regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81828
+ addr2 = addr1 + 1;
8152981829
if( pIndex->onError!=OE_None ){
8153081830
const int regRowid = regIdxKey + pIndex->nColumn;
8153181831
const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
8153281832
void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
8153381833
@@ -81621,10 +81921,11 @@
8162181921
** before looking up the table.
8162281922
*/
8162381923
assert( pName1 && pName2 );
8162481924
iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
8162581925
if( iDb<0 ) goto exit_create_index;
81926
+ assert( pName && pName->z );
8162681927
8162781928
#ifndef SQLITE_OMIT_TEMPDB
8162881929
/* If the index name was unqualified, check if the the table
8162981930
** is a temp table. If so, set the database to 1. Do not do this
8163081931
** if initialising a database schema.
@@ -81648,10 +81949,11 @@
8164881949
pTblName->a[0].zDatabase);
8164981950
if( !pTab || db->mallocFailed ) goto exit_create_index;
8165081951
assert( db->aDb[iDb].pSchema==pTab->pSchema );
8165181952
}else{
8165281953
assert( pName==0 );
81954
+ assert( pStart==0 );
8165381955
pTab = pParse->pNewTable;
8165481956
if( !pTab ) goto exit_create_index;
8165581957
iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
8165681958
}
8165781959
pDb = &db->aDb[iDb];
@@ -81690,10 +81992,11 @@
8169081992
** own name.
8169181993
*/
8169281994
if( pName ){
8169381995
zName = sqlite3NameFromToken(db, pName);
8169481996
if( zName==0 ) goto exit_create_index;
81997
+ assert( pName->z!=0 );
8169581998
if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
8169681999
goto exit_create_index;
8169782000
}
8169882001
if( !db->init.busy ){
8169982002
if( sqlite3FindTable(db, zName, 0)!=0 ){
@@ -81769,24 +82072,24 @@
8176982072
*/
8177082073
nName = sqlite3Strlen30(zName);
8177182074
nCol = pList->nExpr;
8177282075
pIndex = sqlite3DbMallocZero(db,
8177382076
sizeof(Index) + /* Index structure */
82077
+ sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
8177482078
sizeof(int)*nCol + /* Index.aiColumn */
81775
- sizeof(int)*(nCol+1) + /* Index.aiRowEst */
8177682079
sizeof(char *)*nCol + /* Index.azColl */
8177782080
sizeof(u8)*nCol + /* Index.aSortOrder */
8177882081
nName + 1 + /* Index.zName */
8177982082
nExtra /* Collation sequence names */
8178082083
);
8178182084
if( db->mallocFailed ){
8178282085
goto exit_create_index;
8178382086
}
81784
- pIndex->azColl = (char**)(&pIndex[1]);
82087
+ pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
82088
+ pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
8178582089
pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
81786
- pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81787
- pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
82090
+ pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
8178882091
pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
8178982092
zExtra = (char *)(&pIndex->zName[nName+1]);
8179082093
memcpy(pIndex->zName, zName, nName+1);
8179182094
pIndex->pTable = pTab;
8179282095
pIndex->nColumn = pList->nExpr;
@@ -82059,13 +82362,13 @@
8205982362
** Apart from that, we have little to go on besides intuition as to
8206082363
** how aiRowEst[] should be initialized. The numbers generated here
8206182364
** are based on typical values found in actual indices.
8206282365
*/
8206382366
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
82064
- unsigned *a = pIdx->aiRowEst;
82367
+ tRowcnt *a = pIdx->aiRowEst;
8206582368
int i;
82066
- unsigned n;
82369
+ tRowcnt n;
8206782370
assert( a!=0 );
8206882371
a[0] = pIdx->pTable->nRowEst;
8206982372
if( a[0]<10 ) a[0] = 10;
8207082373
n = 10;
8207182374
for(i=1; i<=pIdx->nColumn; i++){
@@ -82545,17 +82848,14 @@
8254582848
8254682849
/*
8254782850
** Commit a transaction
8254882851
*/
8254982852
SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
82550
- sqlite3 *db;
8255182853
Vdbe *v;
8255282854
8255382855
assert( pParse!=0 );
82554
- db = pParse->db;
82555
- assert( db!=0 );
82556
-/* if( db->aDb[0].pBt==0 ) return; */
82856
+ assert( pParse->db!=0 );
8255782857
if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
8255882858
return;
8255982859
}
8256082860
v = sqlite3GetVdbe(pParse);
8256182861
if( v ){
@@ -82565,17 +82865,14 @@
8256582865
8256682866
/*
8256782867
** Rollback a transaction
8256882868
*/
8256982869
SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
82570
- sqlite3 *db;
8257182870
Vdbe *v;
8257282871
8257382872
assert( pParse!=0 );
82574
- db = pParse->db;
82575
- assert( db!=0 );
82576
-/* if( db->aDb[0].pBt==0 ) return; */
82873
+ assert( pParse->db!=0 );
8257782874
if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
8257882875
return;
8257982876
}
8258082877
v = sqlite3GetVdbe(pParse);
8258182878
if( v ){
@@ -84377,20 +84674,19 @@
8437784674
/* Verify that the call to _bytes() does not invalidate the _text() pointer */
8437884675
assert( z2==(char*)sqlite3_value_text(argv[0]) );
8437984676
if( z2 ){
8438084677
z1 = contextMalloc(context, ((i64)n)+1);
8438184678
if( z1 ){
84382
- memcpy(z1, z2, n+1);
84383
- for(i=0; z1[i]; i++){
84384
- z1[i] = (char)sqlite3Toupper(z1[i]);
84679
+ for(i=0; i<n; i++){
84680
+ z1[i] = (char)sqlite3Toupper(z2[i]);
8438584681
}
84386
- sqlite3_result_text(context, z1, -1, sqlite3_free);
84682
+ sqlite3_result_text(context, z1, n, sqlite3_free);
8438784683
}
8438884684
}
8438984685
}
8439084686
static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84391
- u8 *z1;
84687
+ char *z1;
8439284688
const char *z2;
8439384689
int i, n;
8439484690
UNUSED_PARAMETER(argc);
8439584691
z2 = (char*)sqlite3_value_text(argv[0]);
8439684692
n = sqlite3_value_bytes(argv[0]);
@@ -84397,15 +84693,14 @@
8439784693
/* Verify that the call to _bytes() does not invalidate the _text() pointer */
8439884694
assert( z2==(char*)sqlite3_value_text(argv[0]) );
8439984695
if( z2 ){
8440084696
z1 = contextMalloc(context, ((i64)n)+1);
8440184697
if( z1 ){
84402
- memcpy(z1, z2, n+1);
84403
- for(i=0; z1[i]; i++){
84404
- z1[i] = sqlite3Tolower(z1[i]);
84698
+ for(i=0; i<n; i++){
84699
+ z1[i] = sqlite3Tolower(z2[i]);
8440584700
}
84406
- sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
84701
+ sqlite3_result_text(context, z1, n, sqlite3_free);
8440784702
}
8440884703
}
8440984704
}
8441084705
8441184706
@@ -86778,10 +87073,11 @@
8677887073
sqlite3SelectDelete(db, pSelect);
8677987074
if( db->mallocFailed==1 ){
8678087075
fkTriggerDelete(db, pTrigger);
8678187076
return 0;
8678287077
}
87078
+ assert( pStep!=0 );
8678387079
8678487080
switch( action ){
8678587081
case OE_Restrict:
8678687082
pStep->op = TK_SELECT;
8678787083
break;
@@ -88621,10 +88917,13 @@
8862188917
*/
8862288918
if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
8862388919
return 0;
8862488920
}
8862588921
#endif
88922
+ if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
88923
+ return 0;
88924
+ }
8862688925
8862788926
/* If we get this far, it means either:
8862888927
**
8862988928
** * We can always do the transfer if the table contains an
8863088929
** an integer primary key
@@ -89698,11 +89997,11 @@
8969889997
sqlite3_vfs *pVfs = db->pVfs;
8969989998
void *handle;
8970089999
int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
8970190000
char *zErrmsg = 0;
8970290001
void **aHandle;
89703
- const int nMsg = 300;
90002
+ int nMsg = 300 + sqlite3Strlen30(zFile);
8970490003
8970590004
if( pzErrMsg ) *pzErrMsg = 0;
8970690005
8970790006
/* Ticket #1863. To avoid a creating security problems for older
8970890007
** applications that relink against newer versions of SQLite, the
@@ -89735,10 +90034,11 @@
8973590034
}
8973690035
xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
8973790036
sqlite3OsDlSym(pVfs, handle, zProc);
8973890037
if( xInit==0 ){
8973990038
if( pzErrMsg ){
90039
+ nMsg += sqlite3Strlen30(zProc);
8974090040
*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
8974190041
if( zErrmsg ){
8974290042
sqlite3_snprintf(nMsg, zErrmsg,
8974390043
"no entry point [%s] in shared library [%s]", zProc,zFile);
8974490044
sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
@@ -90420,11 +90720,11 @@
9042090720
){
9042190721
int iReg;
9042290722
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
9042390723
sqlite3CodeVerifySchema(pParse, iDb);
9042490724
iReg = ++pParse->nMem;
90425
- if( zLeft[0]=='p' ){
90725
+ if( sqlite3Tolower(zLeft[0])=='p' ){
9042690726
sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
9042790727
}else{
9042890728
sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
9042990729
}
9043090730
sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
@@ -90486,12 +90786,14 @@
9048690786
*/
9048790787
if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
9048890788
int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
9048990789
int ii; /* Loop counter */
9049090790
90491
- /* Force the schema to be loaded on all databases. This cases all
90492
- ** database files to be opened and the journal_modes set. */
90791
+ /* Force the schema to be loaded on all databases. This causes all
90792
+ ** database files to be opened and the journal_modes set. This is
90793
+ ** necessary because subsequent processing must know if the databases
90794
+ ** are in WAL mode. */
9049390795
if( sqlite3ReadSchema(pParse) ){
9049490796
goto pragma_out;
9049590797
}
9049690798
9049790799
sqlite3VdbeSetNumCols(v, 1);
@@ -91031,11 +91333,11 @@
9103191333
{ OP_IfNeg, 1, 0, 0}, /* 1 */
9103291334
{ OP_String8, 0, 3, 0}, /* 2 */
9103391335
{ OP_ResultRow, 3, 1, 0},
9103491336
};
9103591337
91036
- int isQuick = (zLeft[0]=='q');
91338
+ int isQuick = (sqlite3Tolower(zLeft[0])=='q');
9103791339
9103891340
/* Initialize the VDBE program */
9103991341
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
9104091342
pParse->nMem = 6;
9104191343
sqlite3VdbeSetNumCols(v, 1);
@@ -92406,10 +92708,11 @@
9240692708
Select standin;
9240792709
sqlite3 *db = pParse->db;
9240892710
pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
9240992711
assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
9241092712
if( pNew==0 ){
92713
+ assert( db->mallocFailed );
9241192714
pNew = &standin;
9241292715
memset(pNew, 0, sizeof(*pNew));
9241392716
}
9241492717
if( pEList==0 ){
9241592718
pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
@@ -92433,10 +92736,11 @@
9243392736
if( pNew!=&standin ) sqlite3DbFree(db, pNew);
9243492737
pNew = 0;
9243592738
}else{
9243692739
assert( pNew->pSrc!=0 || pParse->nErr>0 );
9243792740
}
92741
+ assert( pNew!=&standin );
9243892742
return pNew;
9243992743
}
9244092744
9244192745
/*
9244292746
** Delete the given Select structure and all of its substructures.
@@ -93611,11 +93915,14 @@
9361193915
/* If the column contains an "AS <name>" phrase, use <name> as the name */
9361293916
zName = sqlite3DbStrDup(db, zName);
9361393917
}else{
9361493918
Expr *pColExpr = p; /* The expression that is the result column name */
9361593919
Table *pTab; /* Table associated with this expression */
93616
- while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
93920
+ while( pColExpr->op==TK_DOT ){
93921
+ pColExpr = pColExpr->pRight;
93922
+ assert( pColExpr!=0 );
93923
+ }
9361793924
if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
9361893925
/* For columns use the column name name */
9361993926
int iCol = pColExpr->iColumn;
9362093927
pTab = pColExpr->pTab;
9362193928
if( iCol<0 ) iCol = pTab->iPKey;
@@ -98609,10 +98916,11 @@
9860998916
break;
9861098917
}
9861198918
}
9861298919
}
9861398920
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
98921
+ assert( aRegIdx );
9861498922
if( openAll || aRegIdx[i]>0 ){
9861598923
KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
9861698924
sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
9861798925
(char*)pKey, P4_KEYINFO_HANDOFF);
9861898926
assert( pParse->nTab>iCur+i+1 );
@@ -98782,10 +99090,11 @@
9878299090
sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
9878399091
sqlite3VdbeJumpHere(v, addr);
9878499092
9878599093
/* Close all tables */
9878699094
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
99095
+ assert( aRegIdx );
9878799096
if( openAll || aRegIdx[i]>0 ){
9878899097
sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
9878999098
}
9879099099
}
9879199100
sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
@@ -98969,11 +99278,11 @@
9896999278
if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
9897099279
sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
9897199280
return sqlite3_errcode(db);
9897299281
}
9897399282
VVA_ONLY( rc = ) sqlite3_step(pStmt);
98974
- assert( rc!=SQLITE_ROW );
99283
+ assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
9897599284
return vacuumFinalize(db, pStmt, pzErrMsg);
9897699285
}
9897799286
9897899287
/*
9897999288
** Execute zSql on database db. The statement returns exactly
@@ -99187,17 +99496,15 @@
9918799496
" WHERE type='view' OR type='trigger'"
9918899497
" OR (type='table' AND rootpage=0)"
9918999498
);
9919099499
if( rc ) goto end_of_vacuum;
9919199500
99192
- /* At this point, unless the main db was completely empty, there is now a
99193
- ** transaction open on the vacuum database, but not on the main database.
99194
- ** Open a btree level transaction on the main database. This allows a
99195
- ** call to sqlite3BtreeCopyFile(). The main database btree level
99196
- ** transaction is then committed, so the SQL level never knows it was
99197
- ** opened for writing. This way, the SQL transaction used to create the
99198
- ** temporary database never needs to be committed.
99501
+ /* At this point, there is a write transaction open on both the
99502
+ ** vacuum database and the main database. Assuming no error occurs,
99503
+ ** both transactions are closed by this block - the main database
99504
+ ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
99505
+ ** call to sqlite3BtreeCommit().
9919999506
*/
9920099507
{
9920199508
u32 meta;
9920299509
int i;
9920399510
@@ -100457,25 +100764,35 @@
100457100764
#define TERM_CODED 0x04 /* This term is already coded */
100458100765
#define TERM_COPIED 0x08 /* Has a child */
100459100766
#define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
100460100767
#define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
100461100768
#define TERM_OR_OK 0x40 /* Used during OR-clause processing */
100462
-#ifdef SQLITE_ENABLE_STAT2
100769
+#ifdef SQLITE_ENABLE_STAT3
100463100770
# define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
100464100771
#else
100465
-# define TERM_VNULL 0x00 /* Disabled if not using stat2 */
100772
+# define TERM_VNULL 0x00 /* Disabled if not using stat3 */
100466100773
#endif
100467100774
100468100775
/*
100469100776
** An instance of the following structure holds all information about a
100470100777
** WHERE clause. Mostly this is a container for one or more WhereTerms.
100778
+**
100779
+** Explanation of pOuter: For a WHERE clause of the form
100780
+**
100781
+** a AND ((b AND c) OR (d AND e)) AND f
100782
+**
100783
+** There are separate WhereClause objects for the whole clause and for
100784
+** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
100785
+** subclauses points to the WhereClause object for the whole clause.
100471100786
*/
100472100787
struct WhereClause {
100473100788
Parse *pParse; /* The parser context */
100474100789
WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */
100475100790
Bitmask vmask; /* Bitmask identifying virtual table cursors */
100791
+ WhereClause *pOuter; /* Outer conjunction */
100476100792
u8 op; /* Split operator. TK_AND or TK_OR */
100793
+ u16 wctrlFlags; /* Might include WHERE_AND_ONLY */
100477100794
int nTerm; /* Number of terms */
100478100795
int nSlot; /* Number of entries in a[] */
100479100796
WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
100480100797
#if defined(SQLITE_SMALL_STACK)
100481100798
WhereTerm aStatic[1]; /* Initial static space for a[] */
@@ -100600,18 +100917,21 @@
100600100917
** Initialize a preallocated WhereClause structure.
100601100918
*/
100602100919
static void whereClauseInit(
100603100920
WhereClause *pWC, /* The WhereClause to be initialized */
100604100921
Parse *pParse, /* The parsing context */
100605
- WhereMaskSet *pMaskSet /* Mapping from table cursor numbers to bitmasks */
100922
+ WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */
100923
+ u16 wctrlFlags /* Might include WHERE_AND_ONLY */
100606100924
){
100607100925
pWC->pParse = pParse;
100608100926
pWC->pMaskSet = pMaskSet;
100927
+ pWC->pOuter = 0;
100609100928
pWC->nTerm = 0;
100610100929
pWC->nSlot = ArraySize(pWC->aStatic);
100611100930
pWC->a = pWC->aStatic;
100612100931
pWC->vmask = 0;
100932
+ pWC->wctrlFlags = wctrlFlags;
100613100933
}
100614100934
100615100935
/* Forward reference */
100616100936
static void whereClauseClear(WhereClause*);
100617100937
@@ -100923,40 +101243,42 @@
100923101243
){
100924101244
WhereTerm *pTerm;
100925101245
int k;
100926101246
assert( iCur>=0 );
100927101247
op &= WO_ALL;
100928
- for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
100929
- if( pTerm->leftCursor==iCur
100930
- && (pTerm->prereqRight & notReady)==0
100931
- && pTerm->u.leftColumn==iColumn
100932
- && (pTerm->eOperator & op)!=0
100933
- ){
100934
- if( pIdx && pTerm->eOperator!=WO_ISNULL ){
100935
- Expr *pX = pTerm->pExpr;
100936
- CollSeq *pColl;
100937
- char idxaff;
100938
- int j;
100939
- Parse *pParse = pWC->pParse;
100940
-
100941
- idxaff = pIdx->pTable->aCol[iColumn].affinity;
100942
- if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
100943
-
100944
- /* Figure out the collation sequence required from an index for
100945
- ** it to be useful for optimising expression pX. Store this
100946
- ** value in variable pColl.
100947
- */
100948
- assert(pX->pLeft);
100949
- pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
100950
- assert(pColl || pParse->nErr);
100951
-
100952
- for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
100953
- if( NEVER(j>=pIdx->nColumn) ) return 0;
100954
- }
100955
- if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
100956
- }
100957
- return pTerm;
101248
+ for(; pWC; pWC=pWC->pOuter){
101249
+ for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
101250
+ if( pTerm->leftCursor==iCur
101251
+ && (pTerm->prereqRight & notReady)==0
101252
+ && pTerm->u.leftColumn==iColumn
101253
+ && (pTerm->eOperator & op)!=0
101254
+ ){
101255
+ if( pIdx && pTerm->eOperator!=WO_ISNULL ){
101256
+ Expr *pX = pTerm->pExpr;
101257
+ CollSeq *pColl;
101258
+ char idxaff;
101259
+ int j;
101260
+ Parse *pParse = pWC->pParse;
101261
+
101262
+ idxaff = pIdx->pTable->aCol[iColumn].affinity;
101263
+ if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
101264
+
101265
+ /* Figure out the collation sequence required from an index for
101266
+ ** it to be useful for optimising expression pX. Store this
101267
+ ** value in variable pColl.
101268
+ */
101269
+ assert(pX->pLeft);
101270
+ pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
101271
+ assert(pColl || pParse->nErr);
101272
+
101273
+ for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
101274
+ if( NEVER(j>=pIdx->nColumn) ) return 0;
101275
+ }
101276
+ if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
101277
+ }
101278
+ return pTerm;
101279
+ }
100958101280
}
100959101281
}
100960101282
return 0;
100961101283
}
100962101284
@@ -101029,11 +101351,11 @@
101029101351
int iCol = pRight->iColumn;
101030101352
pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
101031101353
if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
101032101354
z = (char *)sqlite3_value_text(pVal);
101033101355
}
101034
- sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
101356
+ sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-31526-56213 */
101035101357
assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
101036101358
}else if( op==TK_STRING ){
101037101359
z = pRight->u.zToken;
101038101360
}
101039101361
if( z ){
@@ -101047,11 +101369,11 @@
101047101369
pPrefix = sqlite3Expr(db, TK_STRING, z);
101048101370
if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
101049101371
*ppPrefix = pPrefix;
101050101372
if( op==TK_VARIABLE ){
101051101373
Vdbe *v = pParse->pVdbe;
101052
- sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
101374
+ sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-31526-56213 */
101053101375
if( *pisComplete && pRight->u.zToken[1] ){
101054101376
/* If the rhs of the LIKE expression is a variable, and the current
101055101377
** value of the variable means there is no need to invoke the LIKE
101056101378
** function, then no OP_Variable will be added to the program.
101057101379
** This causes problems for the sqlite3_bind_parameter_name()
@@ -101216,11 +101538,11 @@
101216101538
assert( pExpr->op==TK_OR );
101217101539
pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
101218101540
if( pOrInfo==0 ) return;
101219101541
pTerm->wtFlags |= TERM_ORINFO;
101220101542
pOrWc = &pOrInfo->wc;
101221
- whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
101543
+ whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101222101544
whereSplit(pOrWc, pExpr, TK_OR);
101223101545
exprAnalyzeAll(pSrc, pOrWc);
101224101546
if( db->mallocFailed ) return;
101225101547
assert( pOrWc->nTerm>=2 );
101226101548
@@ -101243,13 +101565,14 @@
101243101565
Bitmask b = 0;
101244101566
pOrTerm->u.pAndInfo = pAndInfo;
101245101567
pOrTerm->wtFlags |= TERM_ANDINFO;
101246101568
pOrTerm->eOperator = WO_AND;
101247101569
pAndWC = &pAndInfo->wc;
101248
- whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
101570
+ whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101249101571
whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
101250101572
exprAnalyzeAll(pSrc, pAndWC);
101573
+ pAndWC->pOuter = pWC;
101251101574
testcase( db->mallocFailed );
101252101575
if( !db->mallocFailed ){
101253101576
for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
101254101577
assert( pAndTerm->pExpr );
101255101578
if( allowedOp(pAndTerm->pExpr->op) ){
@@ -101679,12 +102002,12 @@
101679102002
pNewTerm->prereqAll = pTerm->prereqAll;
101680102003
}
101681102004
}
101682102005
#endif /* SQLITE_OMIT_VIRTUALTABLE */
101683102006
101684
-#ifdef SQLITE_ENABLE_STAT2
101685
- /* When sqlite_stat2 histogram data is available an operator of the
102007
+#ifdef SQLITE_ENABLE_STAT3
102008
+ /* When sqlite_stat3 histogram data is available an operator of the
101686102009
** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101687102010
** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
101688102011
** virtual term of that form.
101689102012
**
101690102013
** Note that the virtual term must be tagged with TERM_VNULL. This
@@ -101718,11 +102041,11 @@
101718102041
pTerm->nChild = 1;
101719102042
pTerm->wtFlags |= TERM_COPIED;
101720102043
pNewTerm->prereqAll = pTerm->prereqAll;
101721102044
}
101722102045
}
101723
-#endif /* SQLITE_ENABLE_STAT2 */
102046
+#endif /* SQLITE_ENABLE_STAT */
101724102047
101725102048
/* Prevent ON clause terms of a LEFT JOIN from being used to drive
101726102049
** an index for tables to the left of the join.
101727102050
*/
101728102051
pTerm->prereqRight |= extraRight;
@@ -102140,14 +102463,17 @@
102140102463
const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
102141102464
const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
102142102465
WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
102143102466
WhereTerm *pTerm; /* A single term of the WHERE clause */
102144102467
102145
- /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
102146
- ** are used */
102468
+ /* The OR-clause optimization is disallowed if the INDEXED BY or
102469
+ ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
102147102470
if( pSrc->notIndexed || pSrc->pIndex!=0 ){
102148102471
return;
102472
+ }
102473
+ if( pWC->wctrlFlags & WHERE_AND_ONLY ){
102474
+ return;
102149102475
}
102150102476
102151102477
/* Search the WHERE clause terms for a usable WO_OR term. */
102152102478
for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102153102479
if( pTerm->eOperator==WO_OR
@@ -102172,10 +102498,11 @@
102172102498
bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
102173102499
}else if( pOrTerm->leftCursor==iCur ){
102174102500
WhereClause tempWC;
102175102501
tempWC.pParse = pWC->pParse;
102176102502
tempWC.pMaskSet = pWC->pMaskSet;
102503
+ tempWC.pOuter = pWC;
102177102504
tempWC.op = TK_AND;
102178102505
tempWC.a = pOrTerm;
102179102506
tempWC.nTerm = 1;
102180102507
bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
102181102508
}else{
@@ -102766,71 +103093,89 @@
102766103093
*/
102767103094
bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102768103095
}
102769103096
#endif /* SQLITE_OMIT_VIRTUALTABLE */
102770103097
103098
+#ifdef SQLITE_ENABLE_STAT3
102771103099
/*
102772
-** Argument pIdx is a pointer to an index structure that has an array of
102773
-** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102774
-** stored in Index.aSample. These samples divide the domain of values stored
102775
-** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102776
-** Region 0 contains all values less than the first sample value. Region
102777
-** 1 contains values between the first and second samples. Region 2 contains
102778
-** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102779
-** contains values larger than the last sample.
102780
-**
102781
-** If the index contains many duplicates of a single value, then it is
102782
-** possible that two or more adjacent samples can hold the same value.
102783
-** When that is the case, the smallest possible region code is returned
102784
-** when roundUp is false and the largest possible region code is returned
102785
-** when roundUp is true.
102786
-**
102787
-** If successful, this function determines which of the regions value
102788
-** pVal lies in, sets *piRegion to the region index (a value between 0
102789
-** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102790
-** Or, if an OOM occurs while converting text values between encodings,
102791
-** SQLITE_NOMEM is returned and *piRegion is undefined.
102792
-*/
102793
-#ifdef SQLITE_ENABLE_STAT2
102794
-static int whereRangeRegion(
103100
+** Estimate the location of a particular key among all keys in an
103101
+** index. Store the results in aStat as follows:
103102
+**
103103
+** aStat[0] Est. number of rows less than pVal
103104
+** aStat[1] Est. number of rows equal to pVal
103105
+**
103106
+** Return SQLITE_OK on success.
103107
+*/
103108
+static int whereKeyStats(
102795103109
Parse *pParse, /* Database connection */
102796103110
Index *pIdx, /* Index to consider domain of */
102797103111
sqlite3_value *pVal, /* Value to consider */
102798
- int roundUp, /* Return largest valid region if true */
102799
- int *piRegion /* OUT: Region of domain in which value lies */
103112
+ int roundUp, /* Round up if true. Round down if false */
103113
+ tRowcnt *aStat /* OUT: stats written here */
102800103114
){
103115
+ tRowcnt n;
103116
+ IndexSample *aSample;
103117
+ int i, eType;
103118
+ int isEq = 0;
103119
+ i64 v;
103120
+ double r, rS;
103121
+
102801103122
assert( roundUp==0 || roundUp==1 );
102802
- if( ALWAYS(pVal) ){
102803
- IndexSample *aSample = pIdx->aSample;
102804
- int i = 0;
102805
- int eType = sqlite3_value_type(pVal);
102806
-
102807
- if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102808
- double r = sqlite3_value_double(pVal);
102809
- for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102810
- if( aSample[i].eType==SQLITE_NULL ) continue;
102811
- if( aSample[i].eType>=SQLITE_TEXT ) break;
102812
- if( roundUp ){
102813
- if( aSample[i].u.r>r ) break;
102814
- }else{
102815
- if( aSample[i].u.r>=r ) break;
102816
- }
102817
- }
102818
- }else if( eType==SQLITE_NULL ){
102819
- i = 0;
102820
- if( roundUp ){
102821
- while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102822
- }
102823
- }else{
103123
+ assert( pIdx->nSample>0 );
103124
+ if( pVal==0 ) return SQLITE_ERROR;
103125
+ n = pIdx->aiRowEst[0];
103126
+ aSample = pIdx->aSample;
103127
+ eType = sqlite3_value_type(pVal);
103128
+
103129
+ if( eType==SQLITE_INTEGER ){
103130
+ v = sqlite3_value_int64(pVal);
103131
+ r = (i64)v;
103132
+ for(i=0; i<pIdx->nSample; i++){
103133
+ if( aSample[i].eType==SQLITE_NULL ) continue;
103134
+ if( aSample[i].eType>=SQLITE_TEXT ) break;
103135
+ if( aSample[i].eType==SQLITE_INTEGER ){
103136
+ if( aSample[i].u.i>=v ){
103137
+ isEq = aSample[i].u.i==v;
103138
+ break;
103139
+ }
103140
+ }else{
103141
+ assert( aSample[i].eType==SQLITE_FLOAT );
103142
+ if( aSample[i].u.r>=r ){
103143
+ isEq = aSample[i].u.r==r;
103144
+ break;
103145
+ }
103146
+ }
103147
+ }
103148
+ }else if( eType==SQLITE_FLOAT ){
103149
+ r = sqlite3_value_double(pVal);
103150
+ for(i=0; i<pIdx->nSample; i++){
103151
+ if( aSample[i].eType==SQLITE_NULL ) continue;
103152
+ if( aSample[i].eType>=SQLITE_TEXT ) break;
103153
+ if( aSample[i].eType==SQLITE_FLOAT ){
103154
+ rS = aSample[i].u.r;
103155
+ }else{
103156
+ rS = aSample[i].u.i;
103157
+ }
103158
+ if( rS>=r ){
103159
+ isEq = rS==r;
103160
+ break;
103161
+ }
103162
+ }
103163
+ }else if( eType==SQLITE_NULL ){
103164
+ i = 0;
103165
+ if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
103166
+ }else{
103167
+ assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
103168
+ for(i=0; i<pIdx->nSample; i++){
103169
+ if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
103170
+ break;
103171
+ }
103172
+ }
103173
+ if( i<pIdx->nSample ){
102824103174
sqlite3 *db = pParse->db;
102825103175
CollSeq *pColl;
102826103176
const u8 *z;
102827
- int n;
102828
-
102829
- /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102830
- assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102831
-
102832103177
if( eType==SQLITE_BLOB ){
102833103178
z = (const u8 *)sqlite3_value_blob(pVal);
102834103179
pColl = db->pDfltColl;
102835103180
assert( pColl->enc==SQLITE_UTF8 );
102836103181
}else{
@@ -102845,16 +103190,16 @@
102845103190
return SQLITE_NOMEM;
102846103191
}
102847103192
assert( z && pColl && pColl->xCmp );
102848103193
}
102849103194
n = sqlite3ValueBytes(pVal, pColl->enc);
102850
-
102851
- for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
103195
+
103196
+ for(; i<pIdx->nSample; i++){
102852103197
int c;
102853103198
int eSampletype = aSample[i].eType;
102854
- if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102855
- if( (eSampletype!=eType) ) break;
103199
+ if( eSampletype<eType ) continue;
103200
+ if( eSampletype!=eType ) break;
102856103201
#ifndef SQLITE_OMIT_UTF16
102857103202
if( pColl->enc!=SQLITE_UTF8 ){
102858103203
int nSample;
102859103204
char *zSample = sqlite3Utf8to16(
102860103205
db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -102868,20 +103213,51 @@
102868103213
}else
102869103214
#endif
102870103215
{
102871103216
c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102872103217
}
102873
- if( c-roundUp>=0 ) break;
103218
+ if( c>=0 ){
103219
+ if( c==0 ) isEq = 1;
103220
+ break;
103221
+ }
102874103222
}
102875103223
}
103224
+ }
102876103225
102877
- assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102878
- *piRegion = i;
103226
+ /* At this point, aSample[i] is the first sample that is greater than
103227
+ ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
103228
+ ** than pVal. If aSample[i]==pVal, then isEq==1.
103229
+ */
103230
+ if( isEq ){
103231
+ assert( i<pIdx->nSample );
103232
+ aStat[0] = aSample[i].nLt;
103233
+ aStat[1] = aSample[i].nEq;
103234
+ }else{
103235
+ tRowcnt iLower, iUpper, iGap;
103236
+ if( i==0 ){
103237
+ iLower = 0;
103238
+ iUpper = aSample[0].nLt;
103239
+ }else{
103240
+ iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
103241
+ iLower = aSample[i-1].nEq + aSample[i-1].nLt;
103242
+ }
103243
+ aStat[1] = pIdx->avgEq;
103244
+ if( iLower>=iUpper ){
103245
+ iGap = 0;
103246
+ }else{
103247
+ iGap = iUpper - iLower;
103248
+ }
103249
+ if( roundUp ){
103250
+ iGap = (iGap*2)/3;
103251
+ }else{
103252
+ iGap = iGap/3;
103253
+ }
103254
+ aStat[0] = iLower + iGap;
102879103255
}
102880103256
return SQLITE_OK;
102881103257
}
102882
-#endif /* #ifdef SQLITE_ENABLE_STAT2 */
103258
+#endif /* SQLITE_ENABLE_STAT3 */
102883103259
102884103260
/*
102885103261
** If expression pExpr represents a literal value, set *pp to point to
102886103262
** an sqlite3_value structure containing the same value, with affinity
102887103263
** aff applied to it, before returning. It is the responsibility of the
@@ -102895,11 +103271,11 @@
102895103271
**
102896103272
** If neither of the above apply, set *pp to NULL.
102897103273
**
102898103274
** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102899103275
*/
102900
-#ifdef SQLITE_ENABLE_STAT2
103276
+#ifdef SQLITE_ENABLE_STAT3
102901103277
static int valueFromExpr(
102902103278
Parse *pParse,
102903103279
Expr *pExpr,
102904103280
u8 aff,
102905103281
sqlite3_value **pp
@@ -102906,11 +103282,11 @@
102906103282
){
102907103283
if( pExpr->op==TK_VARIABLE
102908103284
|| (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
102909103285
){
102910103286
int iVar = pExpr->iColumn;
102911
- sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
103287
+ sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-31526-56213 */
102912103288
*pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
102913103289
return SQLITE_OK;
102914103290
}
102915103291
return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
102916103292
}
@@ -102943,106 +103319,92 @@
102943103319
**
102944103320
** ... FROM t1 WHERE a > ? AND a < ? ...
102945103321
**
102946103322
** then nEq should be passed 0.
102947103323
**
102948
-** The returned value is an integer between 1 and 100, inclusive. A return
102949
-** value of 1 indicates that the proposed range scan is expected to visit
102950
-** approximately 1/100th (1%) of the rows selected by the nEq equality
102951
-** constraints (if any). A return value of 100 indicates that it is expected
102952
-** that the range scan will visit every row (100%) selected by the equality
102953
-** constraints.
103324
+** The returned value is an integer divisor to reduce the estimated
103325
+** search space. A return value of 1 means that range constraints are
103326
+** no help at all. A return value of 2 means range constraints are
103327
+** expected to reduce the search space by half. And so forth...
102954103328
**
102955
-** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102956
-** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102957
-** results in a return of 25 and a range constraint (x>? AND x<?) results
102958
-** in a return of 6.
103329
+** In the absence of sqlite_stat3 ANALYZE data, each range inequality
103330
+** reduces the search space by a factor of 4. Hence a single constraint (x>?)
103331
+** results in a return of 4 and a range constraint (x>? AND x<?) results
103332
+** in a return of 16.
102959103333
*/
102960103334
static int whereRangeScanEst(
102961103335
Parse *pParse, /* Parsing & code generating context */
102962103336
Index *p, /* The index containing the range-compared column; "x" */
102963103337
int nEq, /* index into p->aCol[] of the range-compared column */
102964103338
WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
102965103339
WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
102966
- int *piEst /* OUT: Return value */
103340
+ double *pRangeDiv /* OUT: Reduce search space by this divisor */
102967103341
){
102968103342
int rc = SQLITE_OK;
102969103343
102970
-#ifdef SQLITE_ENABLE_STAT2
102971
-
102972
- if( nEq==0 && p->aSample ){
102973
- sqlite3_value *pLowerVal = 0;
102974
- sqlite3_value *pUpperVal = 0;
102975
- int iEst;
102976
- int iLower = 0;
102977
- int iUpper = SQLITE_INDEX_SAMPLES;
102978
- int roundUpUpper = 0;
102979
- int roundUpLower = 0;
103344
+#ifdef SQLITE_ENABLE_STAT3
103345
+
103346
+ if( nEq==0 && p->nSample ){
103347
+ sqlite3_value *pRangeVal;
103348
+ tRowcnt iLower = 0;
103349
+ tRowcnt iUpper = p->aiRowEst[0];
103350
+ tRowcnt a[2];
102980103351
u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102981103352
102982103353
if( pLower ){
102983103354
Expr *pExpr = pLower->pExpr->pRight;
102984
- rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
103355
+ rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102985103356
assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102986
- roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
103357
+ if( rc==SQLITE_OK
103358
+ && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
103359
+ ){
103360
+ iLower = a[0];
103361
+ if( pLower->eOperator==WO_GT ) iLower += a[1];
103362
+ }
103363
+ sqlite3ValueFree(pRangeVal);
102987103364
}
102988103365
if( rc==SQLITE_OK && pUpper ){
102989103366
Expr *pExpr = pUpper->pExpr->pRight;
102990
- rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
103367
+ rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
102991103368
assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102992
- roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102993
- }
102994
-
102995
- if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102996
- sqlite3ValueFree(pLowerVal);
102997
- sqlite3ValueFree(pUpperVal);
102998
- goto range_est_fallback;
102999
- }else if( pLowerVal==0 ){
103000
- rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103001
- if( pLower ) iLower = iUpper/2;
103002
- }else if( pUpperVal==0 ){
103003
- rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103004
- if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
103005
- }else{
103006
- rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103007
- if( rc==SQLITE_OK ){
103008
- rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103009
- }
103010
- }
103011
- WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
103012
-
103013
- iEst = iUpper - iLower;
103014
- testcase( iEst==SQLITE_INDEX_SAMPLES );
103015
- assert( iEst<=SQLITE_INDEX_SAMPLES );
103016
- if( iEst<1 ){
103017
- *piEst = 50/SQLITE_INDEX_SAMPLES;
103018
- }else{
103019
- *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103020
- }
103021
- sqlite3ValueFree(pLowerVal);
103022
- sqlite3ValueFree(pUpperVal);
103023
- return rc;
103024
- }
103025
-range_est_fallback:
103369
+ if( rc==SQLITE_OK
103370
+ && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
103371
+ ){
103372
+ iUpper = a[0];
103373
+ if( pUpper->eOperator==WO_LE ) iUpper += a[1];
103374
+ }
103375
+ sqlite3ValueFree(pRangeVal);
103376
+ }
103377
+ if( rc==SQLITE_OK ){
103378
+ if( iUpper<=iLower ){
103379
+ *pRangeDiv = (double)p->aiRowEst[0];
103380
+ }else{
103381
+ *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
103382
+ }
103383
+ WHERETRACE(("range scan regions: %u..%u div=%g\n",
103384
+ (u32)iLower, (u32)iUpper, *pRangeDiv));
103385
+ return SQLITE_OK;
103386
+ }
103387
+ }
103026103388
#else
103027103389
UNUSED_PARAMETER(pParse);
103028103390
UNUSED_PARAMETER(p);
103029103391
UNUSED_PARAMETER(nEq);
103030103392
#endif
103031103393
assert( pLower || pUpper );
103032
- *piEst = 100;
103033
- if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103034
- if( pUpper ) *piEst /= 4;
103394
+ *pRangeDiv = (double)1;
103395
+ if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
103396
+ if( pUpper ) *pRangeDiv *= (double)4;
103035103397
return rc;
103036103398
}
103037103399
103038
-#ifdef SQLITE_ENABLE_STAT2
103400
+#ifdef SQLITE_ENABLE_STAT3
103039103401
/*
103040103402
** Estimate the number of rows that will be returned based on
103041103403
** an equality constraint x=VALUE and where that VALUE occurs in
103042103404
** the histogram data. This only works when x is the left-most
103043
-** column of an index and sqlite_stat2 histogram data is available
103405
+** column of an index and sqlite_stat3 histogram data is available
103044103406
** for that index. When pExpr==NULL that means the constraint is
103045103407
** "x IS NULL" instead of "x=VALUE".
103046103408
**
103047103409
** Write the estimated row count into *pnRow and return SQLITE_OK.
103048103410
** If unable to make an estimate, leave *pnRow unchanged and return
@@ -103058,44 +103420,36 @@
103058103420
Index *p, /* The index whose left-most column is pTerm */
103059103421
Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
103060103422
double *pnRow /* Write the revised row estimate here */
103061103423
){
103062103424
sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
103063
- int iLower, iUpper; /* Range of histogram regions containing pRhs */
103064103425
u8 aff; /* Column affinity */
103065103426
int rc; /* Subfunction return code */
103066
- double nRowEst; /* New estimate of the number of rows */
103427
+ tRowcnt a[2]; /* Statistics */
103067103428
103068103429
assert( p->aSample!=0 );
103430
+ assert( p->nSample>0 );
103069103431
aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103070103432
if( pExpr ){
103071103433
rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
103072103434
if( rc ) goto whereEqualScanEst_cancel;
103073103435
}else{
103074103436
pRhs = sqlite3ValueNew(pParse->db);
103075103437
}
103076103438
if( pRhs==0 ) return SQLITE_NOTFOUND;
103077
- rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103078
- if( rc ) goto whereEqualScanEst_cancel;
103079
- rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103080
- if( rc ) goto whereEqualScanEst_cancel;
103081
- WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103082
- if( iLower>=iUpper ){
103083
- nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103084
- if( nRowEst<*pnRow ) *pnRow = nRowEst;
103085
- }else{
103086
- nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103087
- *pnRow = nRowEst;
103088
- }
103089
-
103439
+ rc = whereKeyStats(pParse, p, pRhs, 0, a);
103440
+ if( rc==SQLITE_OK ){
103441
+ WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
103442
+ *pnRow = a[1];
103443
+ }
103090103444
whereEqualScanEst_cancel:
103091103445
sqlite3ValueFree(pRhs);
103092103446
return rc;
103093103447
}
103094
-#endif /* defined(SQLITE_ENABLE_STAT2) */
103448
+#endif /* defined(SQLITE_ENABLE_STAT3) */
103095103449
103096
-#ifdef SQLITE_ENABLE_STAT2
103450
+#ifdef SQLITE_ENABLE_STAT3
103097103451
/*
103098103452
** Estimate the number of rows that will be returned based on
103099103453
** an IN constraint where the right-hand side of the IN operator
103100103454
** is a list of values. Example:
103101103455
**
@@ -103114,64 +103468,29 @@
103114103468
Parse *pParse, /* Parsing & code generating context */
103115103469
Index *p, /* The index whose left-most column is pTerm */
103116103470
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103117103471
double *pnRow /* Write the revised row estimate here */
103118103472
){
103119
- sqlite3_value *pVal = 0; /* One value from list */
103120
- int iLower, iUpper; /* Range of histogram regions containing pRhs */
103121
- u8 aff; /* Column affinity */
103122
- int rc = SQLITE_OK; /* Subfunction return code */
103123
- double nRowEst; /* New estimate of the number of rows */
103124
- int nSpan = 0; /* Number of histogram regions spanned */
103125
- int nSingle = 0; /* Histogram regions hit by a single value */
103126
- int nNotFound = 0; /* Count of values that are not constants */
103127
- int i; /* Loop counter */
103128
- u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
103129
- u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
103473
+ int rc = SQLITE_OK; /* Subfunction return code */
103474
+ double nEst; /* Number of rows for a single term */
103475
+ double nRowEst = (double)0; /* New estimate of the number of rows */
103476
+ int i; /* Loop counter */
103130103477
103131103478
assert( p->aSample!=0 );
103132
- aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103133
- memset(aSpan, 0, sizeof(aSpan));
103134
- memset(aSingle, 0, sizeof(aSingle));
103135
- for(i=0; i<pList->nExpr; i++){
103136
- sqlite3ValueFree(pVal);
103137
- rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103138
- if( rc ) break;
103139
- if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103140
- nNotFound++;
103141
- continue;
103142
- }
103143
- rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103144
- if( rc ) break;
103145
- rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103146
- if( rc ) break;
103147
- if( iLower>=iUpper ){
103148
- aSingle[iLower] = 1;
103149
- }else{
103150
- assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103151
- while( iLower<iUpper ) aSpan[iLower++] = 1;
103152
- }
103479
+ for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
103480
+ nEst = p->aiRowEst[0];
103481
+ rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
103482
+ nRowEst += nEst;
103153103483
}
103154103484
if( rc==SQLITE_OK ){
103155
- for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103156
- if( aSpan[i] ){
103157
- nSpan++;
103158
- }else if( aSingle[i] ){
103159
- nSingle++;
103160
- }
103161
- }
103162
- nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103163
- + nNotFound*p->aiRowEst[1];
103164103485
if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103165103486
*pnRow = nRowEst;
103166
- WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103167
- nSpan, nSingle, nNotFound, nRowEst));
103487
+ WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
103168103488
}
103169
- sqlite3ValueFree(pVal);
103170103489
return rc;
103171103490
}
103172
-#endif /* defined(SQLITE_ENABLE_STAT2) */
103491
+#endif /* defined(SQLITE_ENABLE_STAT3) */
103173103492
103174103493
103175103494
/*
103176103495
** Find the best query plan for accessing a particular table. Write the
103177103496
** best query plan and its cost into the WhereCost object supplied as the
@@ -103214,11 +103533,11 @@
103214103533
Index *pProbe; /* An index we are evaluating */
103215103534
Index *pIdx; /* Copy of pProbe, or zero for IPK index */
103216103535
int eqTermMask; /* Current mask of valid equality operators */
103217103536
int idxEqTermMask; /* Index mask of valid equality operators */
103218103537
Index sPk; /* A fake index object for the primary key */
103219
- unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103538
+ tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103220103539
int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
103221103540
int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
103222103541
103223103542
/* Initialize the cost to a worst-case value */
103224103543
memset(pCost, 0, sizeof(*pCost));
@@ -103269,14 +103588,14 @@
103269103588
}
103270103589
103271103590
/* Loop over all indices looking for the best one to use
103272103591
*/
103273103592
for(; pProbe; pIdx=pProbe=pProbe->pNext){
103274
- const unsigned int * const aiRowEst = pProbe->aiRowEst;
103593
+ const tRowcnt * const aiRowEst = pProbe->aiRowEst;
103275103594
double cost; /* Cost of using pProbe */
103276103595
double nRow; /* Estimated number of rows in result set */
103277
- double log10N; /* base-10 logarithm of nRow (inexact) */
103596
+ double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
103278103597
int rev; /* True to scan in reverse order */
103279103598
int wsFlags = 0;
103280103599
Bitmask used = 0;
103281103600
103282103601
/* The following variables are populated based on the properties of
@@ -103312,18 +103631,16 @@
103312103631
** Set to true if there was at least one "x IN (SELECT ...)" term used
103313103632
** in determining the value of nInMul. Note that the RHS of the
103314103633
** IN operator must be a SELECT, not a value list, for this variable
103315103634
** to be true.
103316103635
**
103317
- ** estBound:
103318
- ** An estimate on the amount of the table that must be searched. A
103319
- ** value of 100 means the entire table is searched. Range constraints
103320
- ** might reduce this to a value less than 100 to indicate that only
103321
- ** a fraction of the table needs searching. In the absence of
103322
- ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103323
- ** space to 1/4rd its original size. So an x>? constraint reduces
103324
- ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
103636
+ ** rangeDiv:
103637
+ ** An estimate of a divisor by which to reduce the search space due
103638
+ ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
103639
+ ** data, a single inequality reduces the search space to 1/4rd its
103640
+ ** original size (rangeDiv==4). Two inequalities reduce the search
103641
+ ** space to 1/16th of its original size (rangeDiv==16).
103325103642
**
103326103643
** bSort:
103327103644
** Boolean. True if there is an ORDER BY clause that will require an
103328103645
** external sort (i.e. scanning the index being evaluated will not
103329103646
** correctly order records).
@@ -103344,26 +103661,27 @@
103344103661
** SELECT a, b, c FROM tbl WHERE a = 1;
103345103662
*/
103346103663
int nEq; /* Number of == or IN terms matching index */
103347103664
int bInEst = 0; /* True if "x IN (SELECT...)" seen */
103348103665
int nInMul = 1; /* Number of distinct equalities to lookup */
103349
- int estBound = 100; /* Estimated reduction in search space */
103666
+ double rangeDiv = (double)1; /* Estimated reduction in search space */
103350103667
int nBound = 0; /* Number of range constraints seen */
103351103668
int bSort = !!pOrderBy; /* True if external sort required */
103352103669
int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
103353103670
int bLookup = 0; /* True if not a covering index */
103354103671
WhereTerm *pTerm; /* A single term of the WHERE clause */
103355
-#ifdef SQLITE_ENABLE_STAT2
103672
+#ifdef SQLITE_ENABLE_STAT3
103356103673
WhereTerm *pFirstTerm = 0; /* First term matching the index */
103357103674
#endif
103358103675
103359103676
/* Determine the values of nEq and nInMul */
103360103677
for(nEq=0; nEq<pProbe->nColumn; nEq++){
103361103678
int j = pProbe->aiColumn[nEq];
103362103679
pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
103363103680
if( pTerm==0 ) break;
103364103681
wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
103682
+ testcase( pTerm->pWC!=pWC );
103365103683
if( pTerm->eOperator & WO_IN ){
103366103684
Expr *pExpr = pTerm->pExpr;
103367103685
wsFlags |= WHERE_COLUMN_IN;
103368103686
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103369103687
/* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
@@ -103374,32 +103692,34 @@
103374103692
nInMul *= pExpr->x.pList->nExpr;
103375103693
}
103376103694
}else if( pTerm->eOperator & WO_ISNULL ){
103377103695
wsFlags |= WHERE_COLUMN_NULL;
103378103696
}
103379
-#ifdef SQLITE_ENABLE_STAT2
103697
+#ifdef SQLITE_ENABLE_STAT3
103380103698
if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103381103699
#endif
103382103700
used |= pTerm->prereqRight;
103383103701
}
103384103702
103385
- /* Determine the value of estBound. */
103703
+ /* Determine the value of rangeDiv */
103386103704
if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103387103705
int j = pProbe->aiColumn[nEq];
103388103706
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103389103707
WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103390103708
WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103391
- whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
103709
+ whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103392103710
if( pTop ){
103393103711
nBound = 1;
103394103712
wsFlags |= WHERE_TOP_LIMIT;
103395103713
used |= pTop->prereqRight;
103714
+ testcase( pTop->pWC!=pWC );
103396103715
}
103397103716
if( pBtm ){
103398103717
nBound++;
103399103718
wsFlags |= WHERE_BTM_LIMIT;
103400103719
used |= pBtm->prereqRight;
103720
+ testcase( pBtm->pWC!=pWC );
103401103721
}
103402103722
wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103403103723
}
103404103724
}else if( pProbe->onError!=OE_None ){
103405103725
testcase( wsFlags & WHERE_COLUMN_IN );
@@ -103458,32 +103778,34 @@
103458103778
if( bInEst && nRow*2>aiRowEst[0] ){
103459103779
nRow = aiRowEst[0]/2;
103460103780
nInMul = (int)(nRow / aiRowEst[nEq]);
103461103781
}
103462103782
103463
-#ifdef SQLITE_ENABLE_STAT2
103783
+#ifdef SQLITE_ENABLE_STAT3
103464103784
/* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103465103785
** and we do not think that values of x are unique and if histogram
103466103786
** data is available for column x, then it might be possible
103467103787
** to get a better estimate on the number of rows based on
103468103788
** VALUE and how common that value is according to the histogram.
103469103789
*/
103470103790
if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
103791
+ assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
103471103792
if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
103472103793
testcase( pFirstTerm->eOperator==WO_EQ );
103473103794
testcase( pFirstTerm->eOperator==WO_ISNULL );
103474103795
whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103475
- }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
103796
+ }else if( bInEst==0 ){
103797
+ assert( pFirstTerm->eOperator==WO_IN );
103476103798
whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103477103799
}
103478103800
}
103479
-#endif /* SQLITE_ENABLE_STAT2 */
103801
+#endif /* SQLITE_ENABLE_STAT3 */
103480103802
103481103803
/* Adjust the number of output rows and downward to reflect rows
103482103804
** that are excluded by range constraints.
103483103805
*/
103484
- nRow = (nRow * (double)estBound) / (double)100;
103806
+ nRow = nRow/rangeDiv;
103485103807
if( nRow<1 ) nRow = 1;
103486103808
103487103809
/* Experiments run on real SQLite databases show that the time needed
103488103810
** to do a binary search to locate a row in a table or index is roughly
103489103811
** log10(N) times the time to move from one row to the next row within
@@ -103608,14 +103930,14 @@
103608103930
if( nRow<2 ) nRow = 2;
103609103931
}
103610103932
103611103933
103612103934
WHERETRACE((
103613
- "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103935
+ "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103614103936
" notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103615103937
pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103616
- nEq, nInMul, estBound, bSort, bLookup, wsFlags,
103938
+ nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
103617103939
notReady, log10N, nRow, cost, used
103618103940
));
103619103941
103620103942
/* If this index is the best we have seen so far, then record this
103621103943
** index and its cost in the pCost structure.
@@ -104115,11 +104437,12 @@
104115104437
*/
104116104438
static Bitmask codeOneLoopStart(
104117104439
WhereInfo *pWInfo, /* Complete information about the WHERE clause */
104118104440
int iLevel, /* Which level of pWInfo->a[] should be coded */
104119104441
u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
104120
- Bitmask notReady /* Which tables are currently available */
104442
+ Bitmask notReady, /* Which tables are currently available */
104443
+ Expr *pWhere /* Complete WHERE clause */
104121104444
){
104122104445
int j, k; /* Loop counters */
104123104446
int iCur; /* The VDBE cursor for the table */
104124104447
int addrNxt; /* Where to jump to continue with the next IN case */
104125104448
int omitTable; /* True if we use the index only */
@@ -104597,11 +104920,12 @@
104597104920
int regRowset = 0; /* Register for RowSet object */
104598104921
int regRowid = 0; /* Register holding rowid */
104599104922
int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
104600104923
int iRetInit; /* Address of regReturn init */
104601104924
int untestedTerms = 0; /* Some terms not completely tested */
104602
- int ii;
104925
+ int ii; /* Loop counter */
104926
+ Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
104603104927
104604104928
pTerm = pLevel->plan.u.pTerm;
104605104929
assert( pTerm!=0 );
104606104930
assert( pTerm->eOperator==WO_OR );
104607104931
assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -104646,18 +104970,33 @@
104646104970
regRowset = ++pParse->nMem;
104647104971
regRowid = ++pParse->nMem;
104648104972
sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
104649104973
}
104650104974
iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
104975
+
104976
+ /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
104977
+ ** Then for every term xN, evaluate as the subexpression: xN AND z
104978
+ ** That way, terms in y that are factored into the disjunction will
104979
+ ** be picked up by the recursive calls to sqlite3WhereBegin() below.
104980
+ */
104981
+ if( pWC->nTerm>1 ){
104982
+ pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
104983
+ pAndExpr->pRight = pWhere;
104984
+ }
104651104985
104652104986
for(ii=0; ii<pOrWc->nTerm; ii++){
104653104987
WhereTerm *pOrTerm = &pOrWc->a[ii];
104654104988
if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
104655104989
WhereInfo *pSubWInfo; /* Info for single OR-term scan */
104990
+ Expr *pOrExpr = pOrTerm->pExpr;
104991
+ if( pAndExpr ){
104992
+ pAndExpr->pLeft = pOrExpr;
104993
+ pOrExpr = pAndExpr;
104994
+ }
104656104995
/* Loop through table entries that match term pOrTerm. */
104657
- pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0,
104658
- WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
104996
+ pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
104997
+ WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
104659104998
WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
104660104999
if( pSubWInfo ){
104661105000
explainOneScan(
104662105001
pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
104663105002
);
@@ -104681,10 +105020,11 @@
104681105020
/* Finish the loop through table entries that match term pOrTerm. */
104682105021
sqlite3WhereEnd(pSubWInfo);
104683105022
}
104684105023
}
104685105024
}
105025
+ sqlite3DbFree(pParse->db, pAndExpr);
104686105026
sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
104687105027
sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
104688105028
sqlite3VdbeResolveLabel(v, iLoopBody);
104689105029
104690105030
if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
@@ -104962,11 +105302,11 @@
104962105302
104963105303
/* Split the WHERE clause into separate subexpressions where each
104964105304
** subexpression is separated by an AND operator.
104965105305
*/
104966105306
initMaskSet(pMaskSet);
104967
- whereClauseInit(pWC, pParse, pMaskSet);
105307
+ whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
104968105308
sqlite3ExprCodeConstants(pParse, pWhere);
104969105309
whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
104970105310
104971105311
/* Special case: a WHERE clause that is constant. Evaluate the
104972105312
** expression and either jump over all of the code or fall thru.
@@ -105201,11 +105541,12 @@
105201105541
assert( bestJ>=0 );
105202105542
assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
105203105543
WHERETRACE(("*** Optimizer selects table %d for loop %d"
105204105544
" with cost=%g and nRow=%g\n",
105205105545
bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
105206
- if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
105546
+ /* The ALWAYS() that follows was added to hush up clang scan-build */
105547
+ if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
105207105548
*ppOrderBy = 0;
105208105549
}
105209105550
if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
105210105551
assert( pWInfo->eDistinct==0 );
105211105552
pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
@@ -105290,11 +105631,11 @@
105290105631
int iCur = pTabItem->iCursor;
105291105632
sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
105292105633
}else
105293105634
#endif
105294105635
if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105295
- && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
105636
+ && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
105296105637
int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
105297105638
sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
105298105639
testcase( pTab->nCol==BMS-1 );
105299105640
testcase( pTab->nCol==BMS );
105300105641
if( !pWInfo->okOnePass && pTab->nCol<BMS ){
@@ -105335,11 +105676,11 @@
105335105676
*/
105336105677
notReady = ~(Bitmask)0;
105337105678
for(i=0; i<nTabList; i++){
105338105679
pLevel = &pWInfo->a[i];
105339105680
explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
105340
- notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
105681
+ notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
105341105682
pWInfo->iContinue = pLevel->addrCont;
105342105683
}
105343105684
105344105685
#ifdef SQLITE_TEST /* For testing and debugging use only */
105345105686
/* Record in the query plan information about the current table
@@ -105470,11 +105811,11 @@
105470105811
struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
105471105812
Table *pTab = pTabItem->pTab;
105472105813
assert( pTab!=0 );
105473105814
if( (pTab->tabFlags & TF_Ephemeral)==0
105474105815
&& pTab->pSelect==0
105475
- && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
105816
+ && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
105476105817
){
105477105818
int ws = pLevel->plan.wsFlags;
105478105819
if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
105479105820
sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
105480105821
}
@@ -108817,11 +109158,13 @@
108817109158
sqlite3ParserTOKENTYPE yyminor /* The value for the token */
108818109159
sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
108819109160
){
108820109161
YYMINORTYPE yyminorunion;
108821109162
int yyact; /* The parser action. */
109163
+#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108822109164
int yyendofinput; /* True if we are at the end of input */
109165
+#endif
108823109166
#ifdef YYERRORSYMBOL
108824109167
int yyerrorhit = 0; /* True if yymajor has invoked an error */
108825109168
#endif
108826109169
yyParser *yypParser; /* The parser */
108827109170
@@ -108840,11 +109183,13 @@
108840109183
yypParser->yyerrcnt = -1;
108841109184
yypParser->yystack[0].stateno = 0;
108842109185
yypParser->yystack[0].major = 0;
108843109186
}
108844109187
yyminorunion.yy0 = yyminor;
109188
+#if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
108845109189
yyendofinput = (yymajor==0);
109190
+#endif
108846109191
sqlite3ParserARG_STORE;
108847109192
108848109193
#ifndef NDEBUG
108849109194
if( yyTraceFILE ){
108850109195
fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
@@ -108852,11 +109197,10 @@
108852109197
#endif
108853109198
108854109199
do{
108855109200
yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
108856109201
if( yyact<YYNSTATE ){
108857
- assert( !yyendofinput ); /* Impossible to shift the $ token */
108858109202
yy_shift(yypParser,yyact,yymajor,&yyminorunion);
108859109203
yypParser->yyerrcnt--;
108860109204
yymajor = YYNOCODE;
108861109205
}else if( yyact < YYNSTATE + YYNRULE ){
108862109206
yy_reduce(yypParser,yyact-YYNSTATE);
@@ -110244,11 +110588,11 @@
110244110588
**
110245110589
** * Recursive calls to this routine from thread X return immediately
110246110590
** without blocking.
110247110591
*/
110248110592
SQLITE_API int sqlite3_initialize(void){
110249
- sqlite3_mutex *pMaster; /* The main static mutex */
110593
+ MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
110250110594
int rc; /* Result code */
110251110595
110252110596
#ifdef SQLITE_OMIT_WSD
110253110597
rc = sqlite3_wsd_init(4096, 24);
110254110598
if( rc!=SQLITE_OK ){
@@ -110278,11 +110622,11 @@
110278110622
** This operation is protected by the STATIC_MASTER mutex. Note that
110279110623
** MutexAlloc() is called for a static mutex prior to initializing the
110280110624
** malloc subsystem - this implies that the allocation of a static
110281110625
** mutex must not require support from the malloc subsystem.
110282110626
*/
110283
- pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
110627
+ MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
110284110628
sqlite3_mutex_enter(pMaster);
110285110629
sqlite3GlobalConfig.isMutexInit = 1;
110286110630
if( !sqlite3GlobalConfig.isMallocInit ){
110287110631
rc = sqlite3MallocInit();
110288110632
}
@@ -111352,17 +111696,17 @@
111352111696
sqlite3 *db,
111353111697
const char *zName,
111354111698
int nArg
111355111699
){
111356111700
int nName = sqlite3Strlen30(zName);
111357
- int rc;
111701
+ int rc = SQLITE_OK;
111358111702
sqlite3_mutex_enter(db->mutex);
111359111703
if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
111360
- sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111361
- 0, sqlite3InvalidFunction, 0, 0, 0);
111704
+ rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111705
+ 0, sqlite3InvalidFunction, 0, 0, 0);
111362111706
}
111363
- rc = sqlite3ApiExit(db, SQLITE_OK);
111707
+ rc = sqlite3ApiExit(db, rc);
111364111708
sqlite3_mutex_leave(db->mutex);
111365111709
return rc;
111366111710
}
111367111711
111368111712
#ifndef SQLITE_OMIT_TRACE
@@ -112420,10 +112764,11 @@
112420112764
if( db ){
112421112765
assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
112422112766
sqlite3_mutex_leave(db->mutex);
112423112767
}
112424112768
rc = sqlite3_errcode(db);
112769
+ assert( db!=0 || rc==SQLITE_NOMEM );
112425112770
if( rc==SQLITE_NOMEM ){
112426112771
sqlite3_close(db);
112427112772
db = 0;
112428112773
}else if( rc!=SQLITE_OK ){
112429112774
db->magic = SQLITE_MAGIC_SICK;
@@ -114148,10 +114493,17 @@
114148114493
#else
114149114494
# define TESTONLY(X)
114150114495
#endif
114151114496
114152114497
#endif /* SQLITE_AMALGAMATION */
114498
+
114499
+#ifdef SQLITE_DEBUG
114500
+SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
114501
+# define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
114502
+#else
114503
+# define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
114504
+#endif
114153114505
114154114506
typedef struct Fts3Table Fts3Table;
114155114507
typedef struct Fts3Cursor Fts3Cursor;
114156114508
typedef struct Fts3Expr Fts3Expr;
114157114509
typedef struct Fts3Phrase Fts3Phrase;
@@ -114649,11 +115001,11 @@
114649115001
char **pp,
114650115002
char *pStart,
114651115003
sqlite3_int64 *pVal
114652115004
){
114653115005
sqlite3_int64 iVal;
114654
- char *p = *pp;
115006
+ char *p;
114655115007
114656115008
/* Pointer p now points at the first byte past the varint we are
114657115009
** interested in. So, unless the doclist is corrupt, the 0x80 bit is
114658115010
** clear on character p[-1]. */
114659115011
for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
@@ -115050,11 +115402,11 @@
115050115402
** the output value undefined. Otherwise SQLITE_OK is returned.
115051115403
**
115052115404
** This function is used when parsing the "prefix=" FTS4 parameter.
115053115405
*/
115054115406
static int fts3GobbleInt(const char **pp, int *pnOut){
115055
- const char *p = *pp; /* Iterator pointer */
115407
+ const char *p; /* Iterator pointer */
115056115408
int nInt = 0; /* Output value */
115057115409
115058115410
for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
115059115411
nInt = nInt * 10 + (p[0] - '0');
115060115412
}
@@ -115549,11 +115901,11 @@
115549115901
if( rc==SQLITE_OK ){
115550115902
/* If no row was found and no error has occured, then the %_content
115551115903
** table is missing a row that is present in the full-text index.
115552115904
** The data structures are corrupt.
115553115905
*/
115554
- rc = SQLITE_CORRUPT_VTAB;
115906
+ rc = FTS_CORRUPT_VTAB;
115555115907
}
115556115908
pCsr->isEof = 1;
115557115909
if( pContext ){
115558115910
sqlite3_result_error_code(pContext, rc);
115559115911
}
@@ -115609,11 +115961,11 @@
115609115961
** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
115610115962
*/
115611115963
zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115612115964
zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115613115965
if( zCsr>zEnd ){
115614
- return SQLITE_CORRUPT_VTAB;
115966
+ return FTS_CORRUPT_VTAB;
115615115967
}
115616115968
115617115969
while( zCsr<zEnd && (piFirst || piLast) ){
115618115970
int cmp; /* memcmp() result */
115619115971
int nSuffix; /* Size of term suffix */
@@ -115627,11 +115979,11 @@
115627115979
}
115628115980
isFirstTerm = 0;
115629115981
zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
115630115982
115631115983
if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
115632
- rc = SQLITE_CORRUPT_VTAB;
115984
+ rc = FTS_CORRUPT_VTAB;
115633115985
goto finish_scan;
115634115986
}
115635115987
if( nPrefix+nSuffix>nAlloc ){
115636115988
char *zNew;
115637115989
nAlloc = (nPrefix+nSuffix) * 2;
@@ -115640,10 +115992,11 @@
115640115992
rc = SQLITE_NOMEM;
115641115993
goto finish_scan;
115642115994
}
115643115995
zBuffer = zNew;
115644115996
}
115997
+ assert( zBuffer );
115645115998
memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
115646115999
nBuffer = nPrefix + nSuffix;
115647116000
zCsr += nSuffix;
115648116001
115649116002
/* Compare the term we are searching for with the term just loaded from
@@ -117076,11 +117429,11 @@
117076117429
** moves *ppPoslist so that it instead points to the first byte of the
117077117430
** same position list.
117078117431
*/
117079117432
static void fts3ReversePoslist(char *pStart, char **ppPoslist){
117080117433
char *p = &(*ppPoslist)[-2];
117081
- char c;
117434
+ char c = 0;
117082117435
117083117436
while( p>pStart && (c=*p--)==0 );
117084117437
while( p>pStart && (*p & 0x80) | c ){
117085117438
c = *p--;
117086117439
}
@@ -118070,11 +118423,11 @@
118070118423
while( a<pEnd ){
118071118424
a += sqlite3Fts3GetVarint(a, &nByte);
118072118425
}
118073118426
if( nDoc==0 || nByte==0 ){
118074118427
sqlite3_reset(pStmt);
118075
- return SQLITE_CORRUPT_VTAB;
118428
+ return FTS_CORRUPT_VTAB;
118076118429
}
118077118430
118078118431
pCsr->nDoc = nDoc;
118079118432
pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
118080118433
assert( pCsr->nRowAvg>0 );
@@ -118546,12 +118899,15 @@
118546118899
}
118547118900
118548118901
aPoslist = pExpr->pRight->pPhrase->doclist.pList;
118549118902
nToken = pExpr->pRight->pPhrase->nToken;
118550118903
for(p=pExpr->pLeft; p && res; p=p->pLeft){
118551
- int nNear = p->pParent->nNear;
118552
- Fts3Phrase *pPhrase = (
118904
+ int nNear;
118905
+ Fts3Phrase *pPhrase;
118906
+ assert( p->pParent && p->pParent->pLeft==p );
118907
+ nNear = p->pParent->nNear;
118908
+ pPhrase = (
118553118909
p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
118554118910
);
118555118911
res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118556118912
}
118557118913
}
@@ -119037,10 +119393,19 @@
119037119393
pPhrase->aToken[i].pSegcsr = 0;
119038119394
}
119039119395
}
119040119396
}
119041119397
119398
+/*
119399
+** Return SQLITE_CORRUPT_VTAB.
119400
+*/
119401
+#ifdef SQLITE_DEBUG
119402
+SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
119403
+ return SQLITE_CORRUPT_VTAB;
119404
+}
119405
+#endif
119406
+
119042119407
#if !SQLITE_CORE
119043119408
/*
119044119409
** Initialize API pointer table, if required.
119045119410
*/
119046119411
SQLITE_API int sqlite3_extension_init(
@@ -119834,12 +120199,16 @@
119834120199
p->pPhrase = (Fts3Phrase *)&p[1];
119835120200
p->pPhrase->iColumn = pParse->iDefaultCol;
119836120201
p->pPhrase->nToken = nToken;
119837120202
119838120203
zBuf = (char *)&p->pPhrase->aToken[nToken];
119839
- memcpy(zBuf, zTemp, nTemp);
119840
- sqlite3_free(zTemp);
120204
+ if( zTemp ){
120205
+ memcpy(zBuf, zTemp, nTemp);
120206
+ sqlite3_free(zTemp);
120207
+ }else{
120208
+ assert( nTemp==0 );
120209
+ }
119841120210
119842120211
for(jj=0; jj<p->pPhrase->nToken; jj++){
119843120212
p->pPhrase->aToken[jj].z = zBuf;
119844120213
zBuf += p->pPhrase->aToken[jj].n;
119845120214
}
@@ -122594,11 +122963,11 @@
122594122963
sqlite3_bind_int64(pStmt, 1, iDocid);
122595122964
}
122596122965
rc = sqlite3_step(pStmt);
122597122966
if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
122598122967
rc = sqlite3_reset(pStmt);
122599
- if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB;
122968
+ if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
122600122969
pStmt = 0;
122601122970
}else{
122602122971
rc = SQLITE_OK;
122603122972
}
122604122973
}
@@ -123398,11 +123767,11 @@
123398123767
pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
123399123768
pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
123400123769
if( nPrefix<0 || nSuffix<=0
123401123770
|| &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
123402123771
){
123403
- return SQLITE_CORRUPT_VTAB;
123772
+ return FTS_CORRUPT_VTAB;
123404123773
}
123405123774
123406123775
if( nPrefix+nSuffix>pReader->nTermAlloc ){
123407123776
int nNew = (nPrefix+nSuffix)*2;
123408123777
char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
@@ -123428,11 +123797,11 @@
123428123797
** of these statements is untrue, then the data structure is corrupt.
123429123798
*/
123430123799
if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
123431123800
|| (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
123432123801
){
123433
- return SQLITE_CORRUPT_VTAB;
123802
+ return FTS_CORRUPT_VTAB;
123434123803
}
123435123804
return SQLITE_OK;
123436123805
}
123437123806
123438123807
/*
@@ -125382,11 +125751,10 @@
125382125751
sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
125383125752
){
125384125753
Fts3Table *p = (Fts3Table *)pVtab;
125385125754
int rc = SQLITE_OK; /* Return Code */
125386125755
int isRemove = 0; /* True for an UPDATE or DELETE */
125387
- sqlite3_int64 iRemove = 0; /* Rowid removed by UPDATE or DELETE */
125388125756
u32 *aSzIns = 0; /* Sizes of inserted documents */
125389125757
u32 *aSzDel; /* Sizes of deleted documents */
125390125758
int nChng = 0; /* Net change in number of documents */
125391125759
int bInsertDone = 0;
125392125760
@@ -125465,23 +125833,23 @@
125465125833
/* If this is a DELETE or UPDATE operation, remove the old record. */
125466125834
if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
125467125835
assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
125468125836
rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
125469125837
isRemove = 1;
125470
- iRemove = sqlite3_value_int64(apVal[0]);
125471125838
}
125472125839
125473125840
/* If this is an INSERT or UPDATE operation, insert the new record. */
125474125841
if( nArg>1 && rc==SQLITE_OK ){
125475125842
if( bInsertDone==0 ){
125476125843
rc = fts3InsertData(p, apVal, pRowid);
125477
- if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB;
125844
+ if( rc==SQLITE_CONSTRAINT ) rc = FTS_CORRUPT_VTAB;
125478125845
}
125479
- if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
125846
+ if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
125480125847
rc = fts3PendingTermsDocid(p, *pRowid);
125481125848
}
125482125849
if( rc==SQLITE_OK ){
125850
+ assert( p->iPrevDocid==*pRowid );
125483125851
rc = fts3InsertTerms(p, apVal, aSzIns);
125484125852
}
125485125853
if( p->bHasDocsize ){
125486125854
fts3InsertDocsize(&rc, p, aSzIns);
125487125855
}
@@ -126371,11 +126739,11 @@
126371126739
pStmt = *ppStmt;
126372126740
assert( sqlite3_data_count(pStmt)==1 );
126373126741
126374126742
a = sqlite3_column_blob(pStmt, 0);
126375126743
a += sqlite3Fts3GetVarint(a, &nDoc);
126376
- if( nDoc==0 ) return SQLITE_CORRUPT_VTAB;
126744
+ if( nDoc==0 ) return FTS_CORRUPT_VTAB;
126377126745
*pnDoc = (u32)nDoc;
126378126746
126379126747
if( paLen ) *paLen = a;
126380126748
return SQLITE_OK;
126381126749
}
@@ -126950,11 +127318,11 @@
126950127318
sqlite3_snprintf(sizeof(aBuffer), aBuffer,
126951127319
"%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
126952127320
);
126953127321
rc = fts3StringAppend(&res, aBuffer, -1);
126954127322
}else if( rc==SQLITE_DONE ){
126955
- rc = SQLITE_CORRUPT_VTAB;
127323
+ rc = FTS_CORRUPT_VTAB;
126956127324
}
126957127325
}
126958127326
}
126959127327
if( rc==SQLITE_DONE ){
126960127328
rc = SQLITE_OK;
@@ -128291,11 +128659,12 @@
128291128659
pCsr->nConstraint = argc;
128292128660
if( !pCsr->aConstraint ){
128293128661
rc = SQLITE_NOMEM;
128294128662
}else{
128295128663
memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
128296
- assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
128664
+ assert( (idxStr==0 && argc==0)
128665
+ || (idxStr && (int)strlen(idxStr)==argc*2) );
128297128666
for(ii=0; ii<argc; ii++){
128298128667
RtreeConstraint *p = &pCsr->aConstraint[ii];
128299128668
p->op = idxStr[ii*2];
128300128669
p->iCoord = idxStr[ii*2+1]-'a';
128301128670
if( p->op==RTREE_MATCH ){
@@ -128592,11 +128961,14 @@
128592128961
int iCell;
128593128962
sqlite3_int64 iBest = 0;
128594128963
128595128964
float fMinGrowth = 0.0;
128596128965
float fMinArea = 0.0;
128966
+#if VARIANT_RSTARTREE_CHOOSESUBTREE
128597128967
float fMinOverlap = 0.0;
128968
+ float overlap;
128969
+#endif
128598128970
128599128971
int nCell = NCELL(pNode);
128600128972
RtreeCell cell;
128601128973
RtreeNode *pChild;
128602128974
@@ -128624,33 +128996,34 @@
128624128996
*/
128625128997
for(iCell=0; iCell<nCell; iCell++){
128626128998
int bBest = 0;
128627128999
float growth;
128628129000
float area;
128629
- float overlap = 0.0;
128630129001
nodeGetCell(pRtree, pNode, iCell, &cell);
128631129002
growth = cellGrowth(pRtree, &cell, pCell);
128632129003
area = cellArea(pRtree, &cell);
128633129004
128634129005
#if VARIANT_RSTARTREE_CHOOSESUBTREE
128635129006
if( ii==(pRtree->iDepth-1) ){
128636129007
overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
129008
+ }else{
129009
+ overlap = 0.0;
128637129010
}
128638129011
if( (iCell==0)
128639129012
|| (overlap<fMinOverlap)
128640129013
|| (overlap==fMinOverlap && growth<fMinGrowth)
128641129014
|| (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
128642129015
){
128643129016
bBest = 1;
129017
+ fMinOverlap = overlap;
128644129018
}
128645129019
#else
128646129020
if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
128647129021
bBest = 1;
128648129022
}
128649129023
#endif
128650129024
if( bBest ){
128651
- fMinOverlap = overlap;
128652129025
fMinGrowth = growth;
128653129026
fMinArea = area;
128654129027
iBest = cell.iRowid;
128655129028
}
128656129029
}
128657129030
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.8. 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.
@@ -394,11 +394,11 @@
394 ** assert() macro is enabled, each call into the Win32 native heap subsystem
395 ** will cause HeapValidate to be called. If heap validation should fail, an
396 ** assertion will be triggered.
397 **
398 ** (Historical note: There used to be several other options, but we've
399 ** pared it down to just these two.)
400 **
401 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402 ** the default.
403 */
404 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
@@ -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.7.8"
660 #define SQLITE_VERSION_NUMBER 3007008
661 #define SQLITE_SOURCE_ID "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1318,11 +1318,15 @@
1318 ** in order for the database to be readable. The fourth parameter to
1319 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1320 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1321 ** WAL mode. If the integer is -1, then it is overwritten with the current
1322 ** WAL persistence setting.
1323 **
 
 
 
 
1324 */
1325 #define SQLITE_FCNTL_LOCKSTATE 1
1326 #define SQLITE_GET_LOCKPROXYFILE 2
1327 #define SQLITE_SET_LOCKPROXYFILE 3
1328 #define SQLITE_LAST_ERRNO 4
@@ -1330,10 +1334,11 @@
1330 #define SQLITE_FCNTL_CHUNK_SIZE 6
1331 #define SQLITE_FCNTL_FILE_POINTER 7
1332 #define SQLITE_FCNTL_SYNC_OMITTED 8
1333 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
1334 #define SQLITE_FCNTL_PERSIST_WAL 10
 
1335
1336 /*
1337 ** CAPI3REF: Mutex Handle
1338 **
1339 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3346,11 +3351,12 @@
3346 ** zSql string ends at either the first '\000' or '\u0000' character or
3347 ** the nByte-th byte, whichever comes first. If the caller knows
3348 ** that the supplied string is nul-terminated, then there is a small
3349 ** performance advantage to be gained by passing an nByte parameter that
3350 ** is equal to the number of bytes in the input string <i>including</i>
3351 ** the nul-terminator bytes.
 
3352 **
3353 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3354 ** past the end of the first SQL statement in zSql. These routines only
3355 ** compile the first statement in zSql, so *pzTail is left pointing to
3356 ** what remains uncompiled.
@@ -3397,11 +3403,11 @@
3397 ** a schema change, on the first [sqlite3_step()] call following any change
3398 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3399 ** ^The specific value of WHERE-clause [parameter] might influence the
3400 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3401 ** or [GLOB] operator or if the parameter is compared to an indexed column
3402 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
3403 ** the
3404 ** </li>
3405 ** </ol>
3406 */
3407 SQLITE_API int sqlite3_prepare(
@@ -3567,10 +3573,17 @@
3567 ** ^(In those routines that have a fourth argument, its value is the
3568 ** number of bytes in the parameter. To be clear: the value is the
3569 ** number of <u>bytes</u> in the value, not the number of characters.)^
3570 ** ^If the fourth parameter is negative, the length of the string is
3571 ** the number of bytes up to the first zero terminator.
 
 
 
 
 
 
 
3572 **
3573 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3574 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3575 ** string after SQLite has finished with it. ^The destructor is called
3576 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3900,10 +3913,16 @@
3900 ** current row of the result set of [prepared statement] P.
3901 ** ^If prepared statement P does not have results ready to return
3902 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3903 ** interfaces) then sqlite3_data_count(P) returns 0.
3904 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
 
 
 
 
 
 
3905 **
3906 ** See also: [sqlite3_column_count()]
3907 */
3908 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3909
@@ -4579,11 +4598,16 @@
4579 ** is negative, then SQLite takes result text from the 2nd parameter
4580 ** through the first zero character.
4581 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4582 ** is non-negative, then as many bytes (not characters) of the text
4583 ** pointed to by the 2nd parameter are taken as the application-defined
4584 ** function result.
 
 
 
 
 
4585 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4586 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4587 ** function as the destructor on the text or BLOB result when it has
4588 ** finished using that result.
4589 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -6362,20 +6386,34 @@
6362 ** <dd>This parameter returns the approximate number of of bytes of heap
6363 ** and lookaside memory used by all prepared statements associated with
6364 ** the database connection.)^
6365 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6366 ** </dd>
 
 
 
 
 
 
 
 
 
 
 
 
6367 ** </dl>
6368 */
6369 #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
6370 #define SQLITE_DBSTATUS_CACHE_USED 1
6371 #define SQLITE_DBSTATUS_SCHEMA_USED 2
6372 #define SQLITE_DBSTATUS_STMT_USED 3
6373 #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
6374 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
6375 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
6376 #define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */
 
 
6377
6378
6379 /*
6380 ** CAPI3REF: Prepared Statement Status
6381 **
@@ -6425,11 +6463,10 @@
6425 ** <dd>^This is the number of rows inserted into transient indices that
6426 ** were created automatically in order to help joins run faster.
6427 ** A non-zero value in this counter may indicate an opportunity to
6428 ** improvement performance by adding permanent indices that do not
6429 ** need to be reinitialized each time the statement is run.</dd>
6430 **
6431 ** </dl>
6432 */
6433 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
6434 #define SQLITE_STMTSTATUS_SORT 2
6435 #define SQLITE_STMTSTATUS_AUTOINDEX 3
@@ -7711,10 +7748,22 @@
7711 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7712 ** have to specify the value in the less intuitive manner shown:
7713 */
7714 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7715
 
 
 
 
 
 
 
 
 
 
 
 
7716 /*
7717 ** Macros to determine whether the machine is big or little endian,
7718 ** evaluated at runtime.
7719 */
7720 #ifdef SQLITE_AMALGAMATION
@@ -8742,10 +8791,11 @@
8742 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8743 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8744 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8745 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8746 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
 
8747
8748 /* Functions used to truncate the database file. */
8749 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8750
8751 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9278,18 +9328,21 @@
9278 /*
9279 ** If this is a no-op implementation, implement everything as macros.
9280 */
9281 #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
9282 #define sqlite3_mutex_free(X)
9283 #define sqlite3_mutex_enter(X)
9284 #define sqlite3_mutex_try(X) SQLITE_OK
9285 #define sqlite3_mutex_leave(X)
9286 #define sqlite3_mutex_held(X) ((void)(X),1)
9287 #define sqlite3_mutex_notheld(X) ((void)(X),1)
9288 #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
9289 #define sqlite3MutexInit() SQLITE_OK
9290 #define sqlite3MutexEnd()
 
 
 
9291 #endif /* defined(SQLITE_MUTEX_OMIT) */
9292
9293 /************** End of mutex.h ***********************************************/
9294 /************** Continuing where we left off in sqliteInt.h ******************/
9295
@@ -9918,11 +9971,11 @@
9918 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9919 int nCol; /* Number of columns in this table */
9920 Column *aCol; /* Information about each column */
9921 Index *pIndex; /* List of SQL indexes on this table. */
9922 int tnum; /* Root BTree node for this table (see note above) */
9923 unsigned nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9924 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9925 u16 nRef; /* Number of pointers to this Table */
9926 u8 tabFlags; /* Mask of TF_* values */
9927 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9928 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10117,11 +10170,11 @@
10117 */
10118 struct Index {
10119 char *zName; /* Name of this index */
10120 int nColumn; /* Number of columns in the table used by this index */
10121 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10122 unsigned *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10123 Table *pTable; /* The SQL table being indexed */
10124 int tnum; /* Page containing root of this index in database file */
10125 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10126 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10127 u8 bUnordered; /* Use this index for == or IN queries only */
@@ -10128,24 +10181,32 @@
10128 char *zColAff; /* String defining the affinity of each column */
10129 Index *pNext; /* The next index associated with the same table */
10130 Schema *pSchema; /* Schema containing this index */
10131 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10132 char **azColl; /* Array of collation sequence names for index */
10133 IndexSample *aSample; /* Array of SQLITE_INDEX_SAMPLES samples */
 
 
 
 
10134 };
10135
10136 /*
10137 ** Each sample stored in the sqlite_stat2 table is represented in memory
10138 ** using a structure of this type.
10139 */
10140 struct IndexSample {
10141 union {
10142 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10143 double r; /* Value if eType is SQLITE_FLOAT or SQLITE_INTEGER */
 
10144 } u;
10145 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10146 u8 nByte; /* Size in byte of text or blob. */
 
 
 
10147 };
10148
10149 /*
10150 ** Each token coming out of the lexer is an instance of
10151 ** this structure. Tokens are also used as part of an expression.
@@ -10593,14 +10654,14 @@
10593 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
10594 #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
10595 #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
10596 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
10597 #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
10598 #define WHERE_OMIT_OPEN 0x0010 /* Table cursors are already open */
10599 #define WHERE_OMIT_CLOSE 0x0020 /* Omit close of table & index cursors */
10600 #define WHERE_FORCE_TABLE 0x0040 /* Do not use an index-only search */
10601 #define WHERE_ONETABLE_ONLY 0x0080 /* Only code the 1st table in pTabList */
10602
10603 /*
10604 ** The WHERE clause processing routine has two halves. The
10605 ** first part does the start of the WHERE loop and the second
10606 ** half does the tail of the WHERE loop. An instance of
@@ -11350,10 +11411,11 @@
11350 #else
11351 # define sqlite3ViewGetColumnNames(A,B) 0
11352 #endif
11353
11354 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
 
11355 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11356 #ifndef SQLITE_OMIT_AUTOINCREMENT
11357 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11358 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11359 #else
@@ -11606,11 +11668,11 @@
11606 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11607 void(*)(void*));
11608 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11609 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11610 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11611 #ifdef SQLITE_ENABLE_STAT2
11612 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11613 #endif
11614 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11615 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11616 #ifndef SQLITE_AMALGAMATION
@@ -11708,19 +11770,21 @@
11708 # define sqlite3VtabInSync(db) 0
11709 # define sqlite3VtabLock(X)
11710 # define sqlite3VtabUnlock(X)
11711 # define sqlite3VtabUnlockList(X)
11712 # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
 
11713 #else
11714 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
11715 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
11716 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
11717 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
11718 SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
11719 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
11720 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
11721 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
 
11722 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11723 #endif
11724 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
11725 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
11726 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
@@ -11736,11 +11800,10 @@
11736 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
11737 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
11738 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11739 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11740 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
11741 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11742 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11743 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11744 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11745
11746 /* Declarations for functions in fkey.c. All of these are replaced by
@@ -12232,10 +12295,13 @@
12232 #ifdef SQLITE_ENABLE_RTREE
12233 "ENABLE_RTREE",
12234 #endif
12235 #ifdef SQLITE_ENABLE_STAT2
12236 "ENABLE_STAT2",
 
 
 
12237 #endif
12238 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12239 "ENABLE_UNLOCK_NOTIFY",
12240 #endif
12241 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
@@ -12445,13 +12511,10 @@
12445 "OMIT_WSD",
12446 #endif
12447 #ifdef SQLITE_OMIT_XFER_OPT
12448 "OMIT_XFER_OPT",
12449 #endif
12450 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
12451 "PAGECACHE_BLOCKALLOC",
12452 #endif
12453 #ifdef SQLITE_PERFORMANCE_TRACE
12454 "PERFORMANCE_TRACE",
12455 #endif
12456 #ifdef SQLITE_PROXY_DEBUG
12457 "PROXY_DEBUG",
@@ -13189,10 +13252,32 @@
13189 *pHighwater = 0;
13190 *pCurrent = nByte;
13191
13192 break;
13193 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13194
13195 default: {
13196 rc = SQLITE_ERROR;
13197 }
13198 }
@@ -13490,16 +13575,22 @@
13490 }
13491 return 0;
13492 }
13493
13494 /*
13495 ** Set the time to the current time reported by the VFS
 
 
13496 */
13497 static void setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13498 sqlite3 *db = sqlite3_context_db_handle(context);
13499 sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD);
13500 p->validJD = 1;
 
 
 
 
13501 }
13502
13503 /*
13504 ** Attempt to parse the given string into a Julian Day Number. Return
13505 ** the number of errors.
@@ -13525,12 +13616,11 @@
13525 if( parseYyyyMmDd(zDate,p)==0 ){
13526 return 0;
13527 }else if( parseHhMmSs(zDate, p)==0 ){
13528 return 0;
13529 }else if( sqlite3StrICmp(zDate,"now")==0){
13530 setDateTimeToCurrent(context, p);
13531 return 0;
13532 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
13533 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
13534 p->validJD = 1;
13535 return 0;
13536 }
@@ -13953,12 +14043,13 @@
13953 int i;
13954 const unsigned char *z;
13955 int eType;
13956 memset(p, 0, sizeof(*p));
13957 if( argc==0 ){
13958 setDateTimeToCurrent(context, p);
13959 }else if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
 
13960 || eType==SQLITE_INTEGER ){
13961 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
13962 p->validJD = 1;
13963 }else{
13964 z = sqlite3_value_text(argv[0]);
@@ -14266,35 +14357,32 @@
14266 ){
14267 time_t t;
14268 char *zFormat = (char *)sqlite3_user_data(context);
14269 sqlite3 *db;
14270 sqlite3_int64 iT;
 
 
14271 char zBuf[20];
14272
14273 UNUSED_PARAMETER(argc);
14274 UNUSED_PARAMETER(argv);
14275
14276 db = sqlite3_context_db_handle(context);
14277 sqlite3OsCurrentTimeInt64(db->pVfs, &iT);
14278 t = iT/1000 - 10000*(sqlite3_int64)21086676;
14279 #ifdef HAVE_GMTIME_R
14280 {
14281 struct tm sNow;
14282 gmtime_r(&t, &sNow);
 
 
 
 
 
14283 strftime(zBuf, 20, zFormat, &sNow);
14284 }
14285 #else
14286 {
14287 struct tm *pTm;
14288 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14289 pTm = gmtime(&t);
14290 strftime(zBuf, 20, zFormat, pTm);
14291 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14292 }
14293 #endif
14294
14295 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14296 }
14297 #endif
14298
14299 /*
14300 ** This function registered all of the above C functions as SQL
@@ -14625,16 +14713,16 @@
14625 ** Register a VFS with the system. It is harmless to register the same
14626 ** VFS multiple times. The new VFS becomes the default if makeDflt is
14627 ** true.
14628 */
14629 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14630 sqlite3_mutex *mutex = 0;
14631 #ifndef SQLITE_OMIT_AUTOINIT
14632 int rc = sqlite3_initialize();
14633 if( rc ) return rc;
14634 #endif
14635 mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14636 sqlite3_mutex_enter(mutex);
14637 vfsUnlink(pVfs);
14638 if( makeDflt || vfsList==0 ){
14639 pVfs->pNext = vfsList;
14640 vfsList = pVfs;
@@ -18878,52 +18966,14 @@
18878 ** an historical reference. Most of the "enhancements" have been backed
18879 ** out so that the functionality is now the same as standard printf().
18880 **
18881 **************************************************************************
18882 **
18883 ** The following modules is an enhanced replacement for the "printf" subroutines
18884 ** found in the standard C library. The following enhancements are
18885 ** supported:
18886 **
18887 ** + Additional functions. The standard set of "printf" functions
18888 ** includes printf, fprintf, sprintf, vprintf, vfprintf, and
18889 ** vsprintf. This module adds the following:
18890 **
18891 ** * snprintf -- Works like sprintf, but has an extra argument
18892 ** which is the size of the buffer written to.
18893 **
18894 ** * mprintf -- Similar to sprintf. Writes output to memory
18895 ** obtained from malloc.
18896 **
18897 ** * xprintf -- Calls a function to dispose of output.
18898 **
18899 ** * nprintf -- No output, but returns the number of characters
18900 ** that would have been output by printf.
18901 **
18902 ** * A v- version (ex: vsnprintf) of every function is also
18903 ** supplied.
18904 **
18905 ** + A few extensions to the formatting notation are supported:
18906 **
18907 ** * The "=" flag (similar to "-") causes the output to be
18908 ** be centered in the appropriately sized field.
18909 **
18910 ** * The %b field outputs an integer in binary notation.
18911 **
18912 ** * The %c field now accepts a precision. The character output
18913 ** is repeated by the number of times the precision specifies.
18914 **
18915 ** * The %' field works like %c, but takes as its character the
18916 ** next character of the format string, instead of the next
18917 ** argument. For example, printf("%.78'-") prints 78 minus
18918 ** signs, the same as printf("%.78c",'-').
18919 **
18920 ** + When compiled using GCC on a SPARC, this version of printf is
18921 ** faster than the library printf for SUN OS 4.1.
18922 **
18923 ** + All functions are fully reentrant.
18924 **
18925 */
18926
18927 /*
18928 ** Conversion types fall into various categories as defined by the
18929 ** following enumeration.
@@ -19057,47 +19107,19 @@
19057 }
19058 }
19059
19060 /*
19061 ** On machines with a small stack size, you can redefine the
19062 ** SQLITE_PRINT_BUF_SIZE to be less than 350.
19063 */
19064 #ifndef SQLITE_PRINT_BUF_SIZE
19065 # if defined(SQLITE_SMALL_STACK)
19066 # define SQLITE_PRINT_BUF_SIZE 50
19067 # else
19068 # define SQLITE_PRINT_BUF_SIZE 350
19069 # endif
19070 #endif
19071 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
19072
19073 /*
19074 ** The root program. All variations call this core.
19075 **
19076 ** INPUTS:
19077 ** func This is a pointer to a function taking three arguments
19078 ** 1. A pointer to anything. Same as the "arg" parameter.
19079 ** 2. A pointer to the list of characters to be output
19080 ** (Note, this list is NOT null terminated.)
19081 ** 3. An integer number of characters to be output.
19082 ** (Note: This number might be zero.)
19083 **
19084 ** arg This is the pointer to anything which will be passed as the
19085 ** first argument to "func". Use it for whatever you like.
19086 **
19087 ** fmt This is the format string, as in the usual print.
19088 **
19089 ** ap This is a pointer to a list of arguments. Same as in
19090 ** vfprint.
19091 **
19092 ** OUTPUTS:
19093 ** The return value is the total number of characters sent to
19094 ** the function "func". Returns -1 on a error.
19095 **
19096 ** Note that the order in which automatic variables are declared below
19097 ** seems to make a big difference in determining how fast this beast
19098 ** will run.
19099 */
19100 SQLITE_PRIVATE void sqlite3VXPrintf(
19101 StrAccum *pAccum, /* Accumulate results here */
19102 int useExtended, /* Allow extended %-conversions */
19103 const char *fmt, /* Format string */
@@ -19116,27 +19138,27 @@
19116 etByte flag_altform2; /* True if "!" flag is present */
19117 etByte flag_zeropad; /* True if field width constant starts with zero */
19118 etByte flag_long; /* True if "l" flag is present */
19119 etByte flag_longlong; /* True if the "ll" flag is present */
19120 etByte done; /* Loop termination flag */
 
 
19121 sqlite_uint64 longvalue; /* Value for integer types */
19122 LONGDOUBLE_TYPE realvalue; /* Value for real types */
19123 const et_info *infop; /* Pointer to the appropriate info structure */
19124 char buf[etBUFSIZE]; /* Conversion buffer */
19125 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
19126 etByte xtype = 0; /* Conversion paradigm */
19127 char *zExtra; /* Extra memory used for etTCLESCAPE conversions */
19128 #ifndef SQLITE_OMIT_FLOATING_POINT
19129 int exp, e2; /* exponent of real numbers */
 
19130 double rounder; /* Used for rounding floating point values */
19131 etByte flag_dp; /* True if decimal point should be shown */
19132 etByte flag_rtz; /* True if trailing zeros should be removed */
19133 etByte flag_exp; /* True to force display of the exponent */
19134 int nsd; /* Number of significant digits returned */
19135 #endif
 
19136
19137 length = 0;
19138 bufpt = 0;
19139 for(; (c=(*fmt))!=0; ++fmt){
19140 if( c!='%' ){
19141 int amt;
19142 bufpt = (char *)fmt;
@@ -19177,13 +19199,10 @@
19177 while( c>='0' && c<='9' ){
19178 width = width*10 + c - '0';
19179 c = *++fmt;
19180 }
19181 }
19182 if( width > etBUFSIZE-10 ){
19183 width = etBUFSIZE-10;
19184 }
19185 /* Get the precision */
19186 if( c=='.' ){
19187 precision = 0;
19188 c = *++fmt;
19189 if( c=='*' ){
@@ -19226,16 +19245,10 @@
19226 break;
19227 }
19228 }
19229 zExtra = 0;
19230
19231
19232 /* Limit the precision to prevent overflowing buf[] during conversion */
19233 if( precision>etBUFSIZE-40 && (infop->flags & FLAG_STRING)==0 ){
19234 precision = etBUFSIZE-40;
19235 }
19236
19237 /*
19238 ** At this point, variables are initialized as follows:
19239 **
19240 ** flag_alternateform TRUE if a '#' is present.
19241 ** flag_altform2 TRUE if a '!' is present.
@@ -19296,20 +19309,30 @@
19296 }
19297 if( longvalue==0 ) flag_alternateform = 0;
19298 if( flag_zeropad && precision<width-(prefix!=0) ){
19299 precision = width-(prefix!=0);
19300 }
19301 bufpt = &buf[etBUFSIZE-1];
 
 
 
 
 
 
 
 
 
 
 
19302 if( xtype==etORDINAL ){
19303 static const char zOrd[] = "thstndrd";
19304 int x = (int)(longvalue % 10);
19305 if( x>=4 || (longvalue/10)%10==1 ){
19306 x = 0;
19307 }
19308 buf[etBUFSIZE-3] = zOrd[x*2];
19309 buf[etBUFSIZE-2] = zOrd[x*2+1];
19310 bufpt -= 2;
19311 }
19312 {
19313 register const char *cset; /* Use registers for speed */
19314 register int base;
19315 cset = &aDigits[infop->charset];
@@ -19317,11 +19340,11 @@
19317 do{ /* Convert to ascii */
19318 *(--bufpt) = cset[longvalue%base];
19319 longvalue = longvalue/base;
19320 }while( longvalue>0 );
19321 }
19322 length = (int)(&buf[etBUFSIZE-1]-bufpt);
19323 for(idx=precision-length; idx>0; idx--){
19324 *(--bufpt) = '0'; /* Zero pad */
19325 }
19326 if( prefix ) *(--bufpt) = prefix; /* Add sign */
19327 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
@@ -19328,21 +19351,20 @@
19328 const char *pre;
19329 char x;
19330 pre = &aPrefix[infop->prefix];
19331 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
19332 }
19333 length = (int)(&buf[etBUFSIZE-1]-bufpt);
19334 break;
19335 case etFLOAT:
19336 case etEXP:
19337 case etGENERIC:
19338 realvalue = va_arg(ap,double);
19339 #ifdef SQLITE_OMIT_FLOATING_POINT
19340 length = 0;
19341 #else
19342 if( precision<0 ) precision = 6; /* Set default precision */
19343 if( precision>etBUFSIZE/2-10 ) precision = etBUFSIZE/2-10;
19344 if( realvalue<0.0 ){
19345 realvalue = -realvalue;
19346 prefix = '-';
19347 }else{
19348 if( flag_plussign ) prefix = '+';
@@ -19386,11 +19408,10 @@
19386 bufpt = buf;
19387 /*
19388 ** If the field type is etGENERIC, then convert to either etEXP
19389 ** or etFLOAT, as appropriate.
19390 */
19391 flag_exp = xtype==etEXP;
19392 if( xtype!=etFLOAT ){
19393 realvalue += rounder;
19394 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
19395 }
19396 if( xtype==etGENERIC ){
@@ -19407,10 +19428,18 @@
19407 if( xtype==etEXP ){
19408 e2 = 0;
19409 }else{
19410 e2 = exp;
19411 }
 
 
 
 
 
 
 
 
19412 nsd = 0;
19413 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19414 /* The sign in front of the number */
19415 if( prefix ){
19416 *(bufpt++) = prefix;
@@ -19438,21 +19467,21 @@
19438 *(bufpt++) = et_getdigit(&realvalue,&nsd);
19439 }
19440 /* Remove trailing zeros and the "." if no digits follow the "." */
19441 if( flag_rtz && flag_dp ){
19442 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19443 assert( bufpt>buf );
19444 if( bufpt[-1]=='.' ){
19445 if( flag_altform2 ){
19446 *(bufpt++) = '0';
19447 }else{
19448 *(--bufpt) = 0;
19449 }
19450 }
19451 }
19452 /* Add the "eNNN" suffix */
19453 if( flag_exp || xtype==etEXP ){
19454 *(bufpt++) = aDigits[infop->charset];
19455 if( exp<0 ){
19456 *(bufpt++) = '-'; exp = -exp;
19457 }else{
19458 *(bufpt++) = '+';
@@ -19467,12 +19496,12 @@
19467 *bufpt = 0;
19468
19469 /* The converted number is in buf[] and zero terminated. Output it.
19470 ** Note that the number is in the usual order, not reversed as with
19471 ** integer conversions. */
19472 length = (int)(bufpt-buf);
19473 bufpt = buf;
19474
19475 /* Special case: Add leading zeros if the flag_zeropad flag is
19476 ** set and we are not left justified */
19477 if( flag_zeropad && !flag_leftjustify && length < width){
19478 int i;
@@ -19606,13 +19635,11 @@
19606 nspace = width-length;
19607 if( nspace>0 ){
19608 appendSpace(pAccum, nspace);
19609 }
19610 }
19611 if( zExtra ){
19612 sqlite3_free(zExtra);
19613 }
19614 }/* End for loop over the format string */
19615 } /* End of function */
19616
19617 /*
19618 ** Append N bytes of text from z to the StrAccum object.
@@ -19622,10 +19649,11 @@
19622 if( p->tooBig | p->mallocFailed ){
19623 testcase(p->tooBig);
19624 testcase(p->mallocFailed);
19625 return;
19626 }
 
19627 if( N<0 ){
19628 N = sqlite3Strlen30(z);
19629 }
19630 if( N==0 || NEVER(z==0) ){
19631 return;
@@ -19653,19 +19681,20 @@
19653 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19654 }else{
19655 zNew = sqlite3_realloc(zOld, p->nAlloc);
19656 }
19657 if( zNew ){
19658 if( zOld==0 ) memcpy(zNew, p->zText, p->nChar);
19659 p->zText = zNew;
19660 }else{
19661 p->mallocFailed = 1;
19662 sqlite3StrAccumReset(p);
19663 return;
19664 }
19665 }
19666 }
 
19667 memcpy(&p->zText[p->nChar], z, N);
19668 p->nChar += N;
19669 }
19670
19671 /*
@@ -20511,11 +20540,11 @@
20511 ** no longer required.
20512 **
20513 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20514 ** flag set.
20515 */
20516 #ifdef SQLITE_ENABLE_STAT2
20517 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20518 Mem m;
20519 memset(&m, 0, sizeof(m));
20520 m.db = db;
20521 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -25102,11 +25131,11 @@
25102 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
25103 }
25104 #endif
25105
25106
25107 #ifdef SQLITE_DEBUG
25108 /*
25109 ** Helper function for printing out trace information from debugging
25110 ** binaries. This returns the string represetation of the supplied
25111 ** integer lock-type.
25112 */
@@ -25937,18 +25966,18 @@
25937 ** locking a random byte from a range, concurrent SHARED locks may exist
25938 ** even if the locking primitive used is always a write-lock.
25939 */
25940 int rc = SQLITE_OK;
25941 unixFile *pFile = (unixFile*)id;
25942 unixInodeInfo *pInode = pFile->pInode;
25943 struct flock lock;
25944 int tErrno = 0;
25945
25946 assert( pFile );
25947 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25948 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25949 azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
25950
25951 /* If there is already a lock of this type or more restrictive on the
25952 ** unixFile, do nothing. Don't use the end_lock: exit path, as
25953 ** unixEnterMutex() hasn't been called yet.
25954 */
@@ -26148,11 +26177,10 @@
26148 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
26149 unixFile *pFile = (unixFile*)id;
26150 unixInodeInfo *pInode;
26151 struct flock lock;
26152 int rc = SQLITE_OK;
26153 int h;
26154
26155 assert( pFile );
26156 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
26157 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26158 getpid()));
@@ -26160,18 +26188,14 @@
26160 assert( eFileLock<=SHARED_LOCK );
26161 if( pFile->eFileLock<=eFileLock ){
26162 return SQLITE_OK;
26163 }
26164 unixEnterMutex();
26165 h = pFile->h;
26166 pInode = pFile->pInode;
26167 assert( pInode->nShared!=0 );
26168 if( pFile->eFileLock>SHARED_LOCK ){
26169 assert( pInode->eFileLock==pFile->eFileLock );
26170 SimulateIOErrorBenign(1);
26171 SimulateIOError( h=(-1) )
26172 SimulateIOErrorBenign(0);
26173
26174 #ifndef NDEBUG
26175 /* When reducing a lock such that other processes can start
26176 ** reading the database file again, make sure that the
26177 ** transaction counter was updated if any part of the database
@@ -26178,15 +26202,10 @@
26178 ** file changed. If the transaction counter is not updated,
26179 ** other connections to the same file might not realize that
26180 ** the file has changed and hence might not know to flush their
26181 ** cache. The use of a stale cache can lead to database corruption.
26182 */
26183 #if 0
26184 assert( pFile->inNormalWrite==0
26185 || pFile->dbUpdate==0
26186 || pFile->transCntrChng==1 );
26187 #endif
26188 pFile->inNormalWrite = 0;
26189 #endif
26190
26191 /* downgrading to a shared lock on NFS involves clearing the write lock
26192 ** before establishing the readlock - to avoid a race condition we downgrade
@@ -26284,13 +26303,10 @@
26284 pInode->nShared--;
26285 if( pInode->nShared==0 ){
26286 lock.l_type = F_UNLCK;
26287 lock.l_whence = SEEK_SET;
26288 lock.l_start = lock.l_len = 0L;
26289 SimulateIOErrorBenign(1);
26290 SimulateIOError( h=(-1) )
26291 SimulateIOErrorBenign(0);
26292 if( unixFileLock(pFile, &lock)==0 ){
26293 pInode->eFileLock = NO_LOCK;
26294 }else{
26295 rc = SQLITE_IOERR_UNLOCK;
26296 pFile->lastErrno = errno;
@@ -28428,20 +28444,19 @@
28428 rc = SQLITE_NOMEM;
28429 goto shm_open_err;
28430 }
28431
28432 if( pInode->bProcessLock==0 ){
28433 pShmNode->h = robust_open(zShmFilename, O_RDWR|O_CREAT,
28434 (sStat.st_mode & 0777));
 
 
 
 
 
 
28435 if( pShmNode->h<0 ){
28436 const char *zRO;
28437 zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28438 if( zRO && sqlite3GetBoolean(zRO) ){
28439 pShmNode->h = robust_open(zShmFilename, O_RDONLY,
28440 (sStat.st_mode & 0777));
28441 pShmNode->isReadonly = 1;
28442 }
28443 if( pShmNode->h<0 ){
28444 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
28445 goto shm_open_err;
28446 }
28447 }
@@ -29122,10 +29137,13 @@
29122 assert( zFilename==0 || zFilename[0]=='/'
29123 || pVfs->pAppData==(void*)&autolockIoFinder );
29124 #else
29125 assert( zFilename==0 || zFilename[0]=='/' );
29126 #endif
 
 
 
29127
29128 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
29129 pNew->h = h;
29130 pNew->zPath = zFilename;
29131 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
@@ -29224,10 +29242,11 @@
29224 /* Dotfile locking uses the file path so it needs to be included in
29225 ** the dotlockLockingContext
29226 */
29227 char *zLockFile;
29228 int nFilename;
 
29229 nFilename = (int)strlen(zFilename) + 6;
29230 zLockFile = (char *)sqlite3_malloc(nFilename);
29231 if( zLockFile==0 ){
29232 rc = SQLITE_NOMEM;
29233 }else{
@@ -29462,12 +29481,20 @@
29462 **
29463 ** where NN is a 4 digit decimal number. The NN naming schemes are
29464 ** used by the test_multiplex.c module.
29465 */
29466 nDb = sqlite3Strlen30(zPath) - 1;
29467 while( nDb>0 && zPath[nDb]!='-' ) nDb--;
29468 if( nDb==0 ) return SQLITE_OK;
 
 
 
 
 
 
 
 
29469 memcpy(zDb, zPath, nDb);
29470 zDb[nDb] = '\0';
29471
29472 if( 0==osStat(zDb, &sStat) ){
29473 *pMode = sStat.st_mode & 0777;
@@ -29995,14 +30022,16 @@
29995 ** the current time and date as a Julian Day number times 86_400_000. In
29996 ** other words, write into *piNow the number of milliseconds since the Julian
29997 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
29998 ** proleptic Gregorian calendar.
29999 **
30000 ** On success, return 0. Return 1 if the time and date cannot be found.
 
30001 */
30002 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
30003 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
 
30004 #if defined(NO_GETTOD)
30005 time_t t;
30006 time(&t);
30007 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
30008 #elif OS_VXWORKS
@@ -30009,34 +30038,38 @@
30009 struct timespec sNow;
30010 clock_gettime(CLOCK_REALTIME, &sNow);
30011 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
30012 #else
30013 struct timeval sNow;
30014 gettimeofday(&sNow, 0);
30015 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
 
 
 
30016 #endif
30017
30018 #ifdef SQLITE_TEST
30019 if( sqlite3_current_time ){
30020 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
30021 }
30022 #endif
30023 UNUSED_PARAMETER(NotUsed);
30024 return 0;
30025 }
30026
30027 /*
30028 ** Find the current time (in Universal Coordinated Time). Write the
30029 ** current time and date as a Julian Day number into *prNow and
30030 ** return 0. Return 1 if the time and date cannot be found.
30031 */
30032 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
30033 sqlite3_int64 i;
 
30034 UNUSED_PARAMETER(NotUsed);
30035 unixCurrentTimeInt64(0, &i);
30036 *prNow = i/86400000.0;
30037 return 0;
30038 }
30039
30040 /*
30041 ** We added the xGetLastError() method with the intention of providing
30042 ** better low-level error messages when operating-system problems come up
@@ -34169,11 +34202,11 @@
34169
34170 if( h==INVALID_HANDLE_VALUE ){
34171 pFile->lastErrno = GetLastError();
34172 winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
34173 free(zConverted);
34174 if( isReadWrite ){
34175 return winOpen(pVfs, zName, id,
34176 ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
34177 }else{
34178 return SQLITE_CANTOPEN_BKPT;
34179 }
@@ -34535,11 +34568,11 @@
34535 }
34536 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
34537 UNUSED_PARAMETER(pVfs);
34538 getLastErrorMsg(nBuf, zBufOut);
34539 }
34540 void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34541 UNUSED_PARAMETER(pVfs);
34542 #if SQLITE_OS_WINCE
34543 /* The GetProcAddressA() routine is only available on wince. */
34544 return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34545 #else
@@ -34546,11 +34579,11 @@
34546 /* All other windows platforms expect GetProcAddress() to take
34547 ** an Ansi string regardless of the _UNICODE setting */
34548 return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34549 #endif
34550 }
34551 void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34552 UNUSED_PARAMETER(pVfs);
34553 FreeLibrary((HANDLE)pHandle);
34554 }
34555 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
34556 #define winDlOpen 0
@@ -34620,11 +34653,12 @@
34620 ** the current time and date as a Julian Day number times 86_400_000. In
34621 ** other words, write into *piNow the number of milliseconds since the Julian
34622 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
34623 ** proleptic Gregorian calendar.
34624 **
34625 ** On success, return 0. Return 1 if the time and date cannot be found.
 
34626 */
34627 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
34628 /* FILETIME structure is a 64-bit value representing the number of
34629 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
34630 */
@@ -34640,11 +34674,11 @@
34640 #if SQLITE_OS_WINCE
34641 SYSTEMTIME time;
34642 GetSystemTime(&time);
34643 /* if SystemTimeToFileTime() fails, it returns zero. */
34644 if (!SystemTimeToFileTime(&time,&ft)){
34645 return 1;
34646 }
34647 #else
34648 GetSystemTimeAsFileTime( &ft );
34649 #endif
34650
@@ -34656,19 +34690,19 @@
34656 if( sqlite3_current_time ){
34657 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
34658 }
34659 #endif
34660 UNUSED_PARAMETER(pVfs);
34661 return 0;
34662 }
34663
34664 /*
34665 ** Find the current time (in Universal Coordinated Time). Write the
34666 ** current time and date as a Julian Day number into *prNow and
34667 ** return 0. Return 1 if the time and date cannot be found.
34668 */
34669 int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
34670 int rc;
34671 sqlite3_int64 i;
34672 rc = winCurrentTimeInt64(pVfs, &i);
34673 if( !rc ){
34674 *prNow = i/86400000.0;
@@ -35789,12 +35823,10 @@
35789 typedef struct PCache1 PCache1;
35790 typedef struct PgHdr1 PgHdr1;
35791 typedef struct PgFreeslot PgFreeslot;
35792 typedef struct PGroup PGroup;
35793
35794 typedef struct PGroupBlock PGroupBlock;
35795 typedef struct PGroupBlockList PGroupBlockList;
35796
35797 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35798 ** of one or more PCaches that are able to recycle each others unpinned
35799 ** pages when they are under memory pressure. A PGroup is an instance of
35800 ** the following object.
@@ -35821,69 +35853,11 @@
35821 int nMaxPage; /* Sum of nMax for purgeable caches */
35822 int nMinPage; /* Sum of nMin for purgeable caches */
35823 int mxPinned; /* nMaxpage + 10 - nMinPage */
35824 int nCurrentPage; /* Number of purgeable pages allocated */
35825 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35826 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
35827 int isBusy; /* Do not run ReleaseMemory() if true */
35828 PGroupBlockList *pBlockList; /* List of block-lists for this group */
35829 #endif
35830 };
35831
35832 /*
35833 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined when the library is built,
35834 ** each PGroup structure has a linked list of the the following starting
35835 ** at PGroup.pBlockList. There is one entry for each distinct page-size
35836 ** currently used by members of the PGroup (i.e. 1024 bytes, 4096 bytes
35837 ** etc.). Variable PGroupBlockList.nByte is set to the actual allocation
35838 ** size requested by each pcache, which is the database page-size plus
35839 ** the various header structures used by the pcache, pager and btree layers.
35840 ** Usually around (pgsz+200) bytes.
35841 **
35842 ** This size (pgsz+200) bytes is not allocated efficiently by some
35843 ** implementations of malloc. In particular, some implementations are only
35844 ** able to allocate blocks of memory chunks of 2^N bytes, where N is some
35845 ** integer value. Since the page-size is a power of 2, this means we
35846 ** end up wasting (pgsz-200) bytes in each allocation.
35847 **
35848 ** If SQLITE_PAGECACHE_BLOCKALLOC is defined, the (pgsz+200) byte blocks
35849 ** are not allocated directly. Instead, blocks of roughly M*(pgsz+200) bytes
35850 ** are requested from malloc allocator. After a block is returned,
35851 ** sqlite3MallocSize() is used to determine how many (pgsz+200) byte
35852 ** allocations can fit in the space returned by malloc(). This value may
35853 ** be more than M.
35854 **
35855 ** The blocks are stored in a doubly-linked list. Variable PGroupBlock.nEntry
35856 ** contains the number of allocations that will fit in the aData[] space.
35857 ** nEntry is limited to the number of bits in bitmask mUsed. If a slot
35858 ** within aData is in use, the corresponding bit in mUsed is set. Thus
35859 ** when (mUsed+1==(1 << nEntry)) the block is completely full.
35860 **
35861 ** Each time a slot within a block is freed, the block is moved to the start
35862 ** of the linked-list. And if a block becomes completely full, then it is
35863 ** moved to the end of the list. As a result, when searching for a free
35864 ** slot, only the first block in the list need be examined. If it is full,
35865 ** then it is guaranteed that all blocks are full.
35866 */
35867 struct PGroupBlockList {
35868 int nByte; /* Size of each allocation in bytes */
35869 PGroupBlock *pFirst; /* First PGroupBlock in list */
35870 PGroupBlock *pLast; /* Last PGroupBlock in list */
35871 PGroupBlockList *pNext; /* Next block-list attached to group */
35872 };
35873
35874 struct PGroupBlock {
35875 Bitmask mUsed; /* Mask of used slots */
35876 int nEntry; /* Maximum number of allocations in aData[] */
35877 u8 *aData; /* Pointer to data block */
35878 PGroupBlock *pNext; /* Next PGroupBlock in list */
35879 PGroupBlock *pPrev; /* Previous PGroupBlock in list */
35880 PGroupBlockList *pList; /* Owner list */
35881 };
35882
35883 /* Minimum value for PGroupBlock.nEntry */
35884 #define PAGECACHE_BLOCKALLOC_MINENTRY 15
35885
35886 /* Each page cache is an instance of the following object. Every
35887 ** open database file (including each in-memory database and each
35888 ** temporary or transient database) has a single page cache which
35889 ** is an instance of this object.
@@ -35983,21 +35957,10 @@
35983 **
35984 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35985 */
35986 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35987 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
35988
35989 /*
35990 ** Blocks used by the SQLITE_PAGECACHE_BLOCKALLOC blocks to store/retrieve
35991 ** a PGroupBlock pointer based on a pointer to a page buffer.
35992 */
35993 #define PAGE_SET_BLOCKPTR(pCache, pPg, pBlock) \
35994 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) = pBlock )
35995
35996 #define PAGE_GET_BLOCKPTR(pCache, pPg) \
35997 ( *(PGroupBlock **)&(((u8*)pPg)[sizeof(PgHdr1) + pCache->szPage]) )
35998
35999
36000 /*
36001 ** Macros to enter and leave the PCache LRU mutex.
36002 */
36003 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -36120,159 +36083,32 @@
36120 return iSize;
36121 }
36122 }
36123 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36124
36125 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36126 /*
36127 ** The block pBlock belongs to list pList but is not currently linked in.
36128 ** Insert it into the start of the list.
36129 */
36130 static void addBlockToList(PGroupBlockList *pList, PGroupBlock *pBlock){
36131 pBlock->pPrev = 0;
36132 pBlock->pNext = pList->pFirst;
36133 pList->pFirst = pBlock;
36134 if( pBlock->pNext ){
36135 pBlock->pNext->pPrev = pBlock;
36136 }else{
36137 assert( pList->pLast==0 );
36138 pList->pLast = pBlock;
36139 }
36140 }
36141
36142 /*
36143 ** If there are no blocks in the list headed by pList, remove pList
36144 ** from the pGroup->pBlockList list and free it with sqlite3_free().
36145 */
36146 static void freeListIfEmpty(PGroup *pGroup, PGroupBlockList *pList){
36147 assert( sqlite3_mutex_held(pGroup->mutex) );
36148 if( pList->pFirst==0 ){
36149 PGroupBlockList **pp;
36150 for(pp=&pGroup->pBlockList; *pp!=pList; pp=&(*pp)->pNext);
36151 *pp = (*pp)->pNext;
36152 sqlite3_free(pList);
36153 }
36154 }
36155 #endif /* SQLITE_PAGECACHE_BLOCKALLOC */
36156
36157 /*
36158 ** Allocate a new page object initially associated with cache pCache.
36159 */
36160 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36161 int nByte = sizeof(PgHdr1) + pCache->szPage;
36162 void *pPg = 0;
36163 PgHdr1 *p;
36164
36165 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36166 PGroup *pGroup = pCache->pGroup;
36167 PGroupBlockList *pList;
36168 PGroupBlock *pBlock;
36169 int i;
36170
36171 nByte += sizeof(PGroupBlockList *);
36172 nByte = ROUND8(nByte);
36173
36174 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36175 if( pList->nByte==nByte ) break;
36176 }
36177 if( pList==0 ){
36178 PGroupBlockList *pNew;
36179 assert( pGroup->isBusy==0 );
36180 assert( sqlite3_mutex_held(pGroup->mutex) );
36181 pGroup->isBusy = 1; /* Disable sqlite3PcacheReleaseMemory() */
36182 pNew = (PGroupBlockList *)sqlite3MallocZero(sizeof(PGroupBlockList));
36183 pGroup->isBusy = 0; /* Reenable sqlite3PcacheReleaseMemory() */
36184 if( pNew==0 ){
36185 /* malloc() failure. Return early. */
36186 return 0;
36187 }
36188 #ifdef SQLITE_DEBUG
36189 for(pList=pGroup->pBlockList; pList; pList=pList->pNext){
36190 assert( pList->nByte!=nByte );
36191 }
36192 #endif
36193 pNew->nByte = nByte;
36194 pNew->pNext = pGroup->pBlockList;
36195 pGroup->pBlockList = pNew;
36196 pList = pNew;
36197 }
36198
36199 pBlock = pList->pFirst;
36200 if( pBlock==0 || pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) ){
36201 int sz;
36202
36203 /* Allocate a new block. Try to allocate enough space for the PGroupBlock
36204 ** structure and MINENTRY allocations of nByte bytes each. If the
36205 ** allocator returns more memory than requested, then more than MINENTRY
36206 ** allocations may fit in it. */
36207 assert( sqlite3_mutex_held(pGroup->mutex) );
36208 pcache1LeaveMutex(pCache->pGroup);
36209 sz = sizeof(PGroupBlock) + PAGECACHE_BLOCKALLOC_MINENTRY * nByte;
36210 pBlock = (PGroupBlock *)sqlite3Malloc(sz);
36211 pcache1EnterMutex(pCache->pGroup);
36212
36213 if( !pBlock ){
36214 freeListIfEmpty(pGroup, pList);
36215 return 0;
36216 }
36217 pBlock->nEntry = (sqlite3MallocSize(pBlock) - sizeof(PGroupBlock)) / nByte;
36218 if( pBlock->nEntry>=BMS ){
36219 pBlock->nEntry = BMS-1;
36220 }
36221 pBlock->pList = pList;
36222 pBlock->mUsed = 0;
36223 pBlock->aData = (u8 *)&pBlock[1];
36224 addBlockToList(pList, pBlock);
36225
36226 sz = sqlite3MallocSize(pBlock);
36227 sqlite3_mutex_enter(pcache1.mutex);
36228 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
36229 sqlite3_mutex_leave(pcache1.mutex);
36230 }
36231
36232 for(i=0; pPg==0 && ALWAYS(i<pBlock->nEntry); i++){
36233 if( 0==(pBlock->mUsed & ((Bitmask)1<<i)) ){
36234 pBlock->mUsed |= ((Bitmask)1<<i);
36235 pPg = (void *)&pBlock->aData[pList->nByte * i];
36236 }
36237 }
36238 assert( pPg );
36239 PAGE_SET_BLOCKPTR(pCache, pPg, pBlock);
36240
36241 /* If the block is now full, shift it to the end of the list */
36242 if( pBlock->mUsed==(((Bitmask)1<<pBlock->nEntry)-1) && pList->pLast!=pBlock ){
36243 assert( pList->pFirst==pBlock );
36244 assert( pBlock->pPrev==0 );
36245 assert( pList->pLast->pNext==0 );
36246 pList->pFirst = pBlock->pNext;
36247 pList->pFirst->pPrev = 0;
36248 pBlock->pPrev = pList->pLast;
36249 pBlock->pNext = 0;
36250 pList->pLast->pNext = pBlock;
36251 pList->pLast = pBlock;
36252 }
36253 p = PAGE_TO_PGHDR1(pCache, pPg);
36254 if( pCache->bPurgeable ){
36255 pCache->pGroup->nCurrentPage++;
36256 }
36257 #else
36258 /* The group mutex must be released before pcache1Alloc() is called. This
36259 ** is because it may call sqlite3_release_memory(), which assumes that
36260 ** this mutex is not held. */
36261 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36262 pcache1LeaveMutex(pCache->pGroup);
36263 pPg = pcache1Alloc(nByte);
36264 pcache1EnterMutex(pCache->pGroup);
 
36265 if( pPg ){
36266 p = PAGE_TO_PGHDR1(pCache, pPg);
36267 if( pCache->bPurgeable ){
36268 pCache->pGroup->nCurrentPage++;
36269 }
36270 }else{
36271 p = 0;
36272 }
36273 #endif
36274 return p;
36275 }
36276
36277 /*
36278 ** Free a page object allocated by pcache1AllocPage().
@@ -36282,53 +36118,12 @@
36282 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
36283 */
36284 static void pcache1FreePage(PgHdr1 *p){
36285 if( ALWAYS(p) ){
36286 PCache1 *pCache = p->pCache;
36287 void *pPg = PGHDR1_TO_PAGE(p);
36288
36289 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36290 PGroupBlock *pBlock = PAGE_GET_BLOCKPTR(pCache, pPg);
36291 PGroupBlockList *pList = pBlock->pList;
36292 int i = ((u8 *)pPg - pBlock->aData) / pList->nByte;
36293
36294 assert( pPg==(void *)&pBlock->aData[i*pList->nByte] );
36295 assert( pBlock->mUsed & ((Bitmask)1<<i) );
36296 pBlock->mUsed &= ~((Bitmask)1<<i);
36297
36298 /* Remove the block from the list. If it is completely empty, free it.
36299 ** Or if it is not completely empty, re-insert it at the start of the
36300 ** list. */
36301 if( pList->pFirst==pBlock ){
36302 pList->pFirst = pBlock->pNext;
36303 if( pList->pFirst ) pList->pFirst->pPrev = 0;
36304 }else{
36305 pBlock->pPrev->pNext = pBlock->pNext;
36306 }
36307 if( pList->pLast==pBlock ){
36308 pList->pLast = pBlock->pPrev;
36309 if( pList->pLast ) pList->pLast->pNext = 0;
36310 }else{
36311 pBlock->pNext->pPrev = pBlock->pPrev;
36312 }
36313
36314 if( pBlock->mUsed==0 ){
36315 PGroup *pGroup = p->pCache->pGroup;
36316
36317 int sz = sqlite3MallocSize(pBlock);
36318 sqlite3_mutex_enter(pcache1.mutex);
36319 sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -sz);
36320 sqlite3_mutex_leave(pcache1.mutex);
36321 freeListIfEmpty(pGroup, pList);
36322 sqlite3_free(pBlock);
36323 }else{
36324 addBlockToList(pList, pBlock);
36325 }
36326 #else
36327 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36328 pcache1Free(pPg);
36329 #endif
36330 if( pCache->bPurgeable ){
36331 pCache->pGroup->nCurrentPage--;
36332 }
36333 }
36334 }
@@ -36935,13 +36730,10 @@
36935 ** been released, the function returns. The return value is the total number
36936 ** of bytes of memory released.
36937 */
36938 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36939 int nFree = 0;
36940 #ifdef SQLITE_PAGECACHE_BLOCKALLOC
36941 if( pcache1.grp.isBusy ) return 0;
36942 #endif
36943 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36944 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36945 if( pcache1.pStart==0 ){
36946 PgHdr1 *p;
36947 pcache1EnterMutex(&pcache1.grp);
@@ -38200,12 +37992,12 @@
38200 i64 journalSizeLimit; /* Size limit for persistent journal files */
38201 char *zFilename; /* Name of the database file */
38202 char *zJournal; /* Name of the journal file */
38203 int (*xBusyHandler)(void*); /* Function to call when busy */
38204 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
 
38205 #ifdef SQLITE_TEST
38206 int nHit, nMiss; /* Cache hits and missing */
38207 int nRead, nWrite; /* Database pages read/written */
38208 #endif
38209 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
38210 #ifdef SQLITE_HAS_CODEC
38211 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
@@ -40233,11 +40025,10 @@
40233 needPagerReset = 0;
40234 }
40235 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40236 if( rc!=SQLITE_OK ){
40237 if( rc==SQLITE_DONE ){
40238 rc = SQLITE_OK;
40239 pPager->journalOff = szJ;
40240 break;
40241 }else if( rc==SQLITE_IOERR_SHORT_READ ){
40242 /* If the journal has been truncated, simply stop reading and
40243 ** processing the journal. This might happen if the journal was
@@ -40495,10 +40286,11 @@
40495 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
40496 PgHdr *p; /* For looping over pages */
40497 #endif
40498
40499 assert( pPager->pWal );
 
40500 #ifdef SQLITE_DEBUG
40501 /* Verify that the page list is in accending order */
40502 for(p=pList; p && p->pDirty; p=p->pDirty){
40503 assert( p->pgno < p->pDirty->pgno );
40504 }
@@ -41699,11 +41491,11 @@
41699 ** The doNotSpill flag inhibits all cache spilling regardless of whether
41700 ** or not a sync is required. This is set during a rollback.
41701 **
41702 ** Spilling is also prohibited when in an error state since that could
41703 ** lead to database corruption. In the current implementaton it
41704 ** is impossible for sqlite3PCacheFetch() to be called with createFlag==1
41705 ** while in the error state, hence it is impossible for this routine to
41706 ** be called in the error state. Nevertheless, we include a NEVER()
41707 ** test for the error state as a safeguard against future changes.
41708 */
41709 if( NEVER(pPager->errCode) ) return SQLITE_OK;
@@ -42535,18 +42327,17 @@
42535
42536 if( (*ppPage)->pPager && !noContent ){
42537 /* In this case the pcache already contains an initialized copy of
42538 ** the page. Return without further ado. */
42539 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
42540 PAGER_INCR(pPager->nHit);
42541 return SQLITE_OK;
42542
42543 }else{
42544 /* The pager cache has created a new page. Its content needs to
42545 ** be initialized. */
42546
42547 PAGER_INCR(pPager->nMiss);
42548 pPg = *ppPage;
42549 pPg->pPager = pPager;
42550
42551 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
42552 ** number greater than this, or the unused locking-page, is requested. */
@@ -42578,10 +42369,11 @@
42578 }
42579 memset(pPg->pData, 0, pPager->pageSize);
42580 IOTRACE(("ZERO %p %d\n", pPager, pgno));
42581 }else{
42582 assert( pPg->pPager==pPager );
 
42583 rc = readDbPage(pPg);
42584 if( rc!=SQLITE_OK ){
42585 goto pager_acquire_err;
42586 }
42587 }
@@ -43611,10 +43403,35 @@
43611 a[9] = pPager->nRead;
43612 a[10] = pPager->nWrite;
43613 return a;
43614 }
43615 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43616
43617 /*
43618 ** Return true if this is an in-memory pager.
43619 */
43620 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
@@ -46706,11 +46523,11 @@
46706 */
46707 if( iRead ){
46708 int sz;
46709 i64 iOffset;
46710 sz = pWal->hdr.szPage;
46711 sz = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
46712 testcase( sz<=32768 );
46713 testcase( sz>=65536 );
46714 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
46715 *pInWal = 1;
46716 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
@@ -50019,21 +49836,23 @@
50019 */
50020 if( isMemdb==0 && isTempDb==0 ){
50021 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
50022 int nFullPathname = pVfs->mxPathname+1;
50023 char *zFullPathname = sqlite3Malloc(nFullPathname);
50024 sqlite3_mutex *mutexShared;
50025 p->sharable = 1;
50026 if( !zFullPathname ){
50027 sqlite3_free(p);
50028 return SQLITE_NOMEM;
50029 }
50030 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
 
50031 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
50032 sqlite3_mutex_enter(mutexOpen);
50033 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50034 sqlite3_mutex_enter(mutexShared);
 
50035 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
50036 assert( pBt->nRef>0 );
50037 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
50038 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
50039 int iDb;
@@ -50135,13 +49954,13 @@
50135
50136 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
50137 /* Add the new BtShared object to the linked list sharable BtShareds.
50138 */
50139 if( p->sharable ){
50140 sqlite3_mutex *mutexShared;
50141 pBt->nRef = 1;
50142 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50143 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
50144 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
50145 if( pBt->mutex==0 ){
50146 rc = SQLITE_NOMEM;
50147 db->mallocFailed = 0;
@@ -50219,16 +50038,16 @@
50219 ** true if the BtShared.nRef counter reaches zero and return
50220 ** false if it is still positive.
50221 */
50222 static int removeFromSharingList(BtShared *pBt){
50223 #ifndef SQLITE_OMIT_SHARED_CACHE
50224 sqlite3_mutex *pMaster;
50225 BtShared *pList;
50226 int removed = 0;
50227
50228 assert( sqlite3_mutex_notheld(pBt->mutex) );
50229 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
50230 sqlite3_mutex_enter(pMaster);
50231 pBt->nRef--;
50232 if( pBt->nRef<=0 ){
50233 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
50234 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
@@ -52191,25 +52010,59 @@
52191 offset -= ovflSize;
52192 }else{
52193 /* Need to read this page properly. It contains some of the
52194 ** range of data that is being read (eOp==0) or written (eOp!=0).
52195 */
52196 DbPage *pDbPage;
 
 
52197 int a = amt;
52198 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
52199 if( rc==SQLITE_OK ){
52200 aPayload = sqlite3PagerGetData(pDbPage);
52201 nextPage = get4byte(aPayload);
52202 if( a + offset > ovflSize ){
52203 a = ovflSize - offset;
52204 }
52205 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52206 sqlite3PagerUnref(pDbPage);
52207 offset = 0;
52208 amt -= a;
52209 pBuf += a;
52210 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52211 }
52212 }
52213 }
52214
52215 if( rc==SQLITE_OK && amt>0 ){
@@ -52804,11 +52657,10 @@
52804 }
52805 }
52806 if( c==0 ){
52807 if( pPage->intKey && !pPage->leaf ){
52808 lwr = idx;
52809 upr = lwr - 1;
52810 break;
52811 }else{
52812 *pRes = 0;
52813 rc = SQLITE_OK;
52814 goto moveto_finish;
@@ -52822,11 +52674,11 @@
52822 if( lwr>upr ){
52823 break;
52824 }
52825 pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
52826 }
52827 assert( lwr==upr+1 );
52828 assert( pPage->isInit );
52829 if( pPage->leaf ){
52830 chldPg = 0;
52831 }else if( lwr>=pPage->nCell ){
52832 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
@@ -53087,10 +52939,12 @@
53087 }
53088 if( rc ){
53089 pTrunk = 0;
53090 goto end_allocate_page;
53091 }
 
 
53092
53093 k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
53094 if( k==0 && !searchList ){
53095 /* The trunk has no leaves and the list is not being searched.
53096 ** So extract the trunk page itself and use it as the newly
@@ -54214,17 +54068,19 @@
54214 ** This is safe because dropping a cell only overwrites the first
54215 ** four bytes of it, and this function does not need the first
54216 ** four bytes of the divider cell. So the pointer is safe to use
54217 ** later on.
54218 **
54219 ** Unless SQLite is compiled in secure-delete mode. In this case,
54220 ** the dropCell() routine will overwrite the entire cell with zeroes.
54221 ** In this case, temporarily copy the cell into the aOvflSpace[]
54222 ** buffer. It will be copied out again as soon as the aSpace[] buffer
54223 ** is allocated. */
54224 if( pBt->secureDelete ){
54225 int iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
 
 
54226 if( (iOff+szNew[i])>(int)pBt->usableSize ){
54227 rc = SQLITE_CORRUPT_BKPT;
54228 memset(apOld, 0, (i+1)*sizeof(MemPage*));
54229 goto balance_cleanup;
54230 }else{
@@ -54640,10 +54496,11 @@
54640 int isDivider = 0;
54641 while( i==iNextOld ){
54642 /* Cell i is the cell immediately following the last cell on old
54643 ** sibling page j. If the siblings are not leaf pages of an
54644 ** intkey b-tree, then cell i was a divider cell. */
 
54645 pOld = apCopy[++j];
54646 iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
54647 if( pOld->nOverflow ){
54648 nOverflow = pOld->nOverflow;
54649 iOverflow = i + !leafData + pOld->aOvfl[0].idx;
@@ -56982,18 +56839,18 @@
56982 /*
56983 ** Release all resources associated with an sqlite3_backup* handle.
56984 */
56985 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
56986 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
56987 sqlite3_mutex *mutex; /* Mutex to protect source database */
56988 int rc; /* Value to return */
56989
56990 /* Enter the mutexes */
56991 if( p==0 ) return SQLITE_OK;
56992 sqlite3_mutex_enter(p->pSrcDb->mutex);
56993 sqlite3BtreeEnter(p->pSrc);
56994 mutex = p->pSrcDb->mutex;
56995 if( p->pDestDb ){
56996 sqlite3_mutex_enter(p->pDestDb->mutex);
56997 }
56998
56999 /* Detach this backup from the source pager. */
@@ -57108,13 +56965,21 @@
57108 ** goes wrong, the transaction on pTo is rolled back. If successful, the
57109 ** transaction is committed before returning.
57110 */
57111 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
57112 int rc;
 
57113 sqlite3_backup b;
57114 sqlite3BtreeEnter(pTo);
57115 sqlite3BtreeEnter(pFrom);
 
 
 
 
 
 
 
57116
57117 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
57118 ** to 0. This is used by the implementations of sqlite3_backup_step()
57119 ** and sqlite3_backup_finish() to detect that they are being called
57120 ** from this function, not directly by the user.
@@ -57137,10 +57002,11 @@
57137 rc = sqlite3_backup_finish(&b);
57138 if( rc==SQLITE_OK ){
57139 pTo->pBt->pageSizeFixed = 0;
57140 }
57141
 
57142 sqlite3BtreeLeave(pFrom);
57143 sqlite3BtreeLeave(pTo);
57144 return rc;
57145 }
57146 #endif /* SQLITE_OMIT_VACUUM */
@@ -58171,15 +58037,15 @@
58171 *ppVal = 0;
58172 return SQLITE_OK;
58173 }
58174 op = pExpr->op;
58175
58176 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT2.
58177 ** The ifdef here is to enable us to achieve 100% branch test coverage even
58178 ** when SQLITE_ENABLE_STAT2 is omitted.
58179 */
58180 #ifdef SQLITE_ENABLE_STAT2
58181 if( op==TK_REGISTER ) op = pExpr->op2;
58182 #else
58183 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
58184 #endif
58185
@@ -58874,12 +58740,12 @@
58874 /*
58875 ** Change the P2 operand of instruction addr so that it points to
58876 ** the address of the next instruction to be coded.
58877 */
58878 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58879 assert( addr>=0 );
58880 sqlite3VdbeChangeP2(p, addr, p->nOp);
58881 }
58882
58883
58884 /*
58885 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -59080,34 +58946,33 @@
59080 ** Change the comment on the the most recently coded instruction. Or
59081 ** insert a No-op and add the comment to that new instruction. This
59082 ** makes the code easier to read during debugging. None of this happens
59083 ** in a production build.
59084 */
59085 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
59086 va_list ap;
59087 if( !p ) return;
59088 assert( p->nOp>0 || p->aOp==0 );
59089 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
59090 if( p->nOp ){
59091 char **pz = &p->aOp[p->nOp-1].zComment;
 
 
 
 
 
 
 
59092 va_start(ap, zFormat);
59093 sqlite3DbFree(p->db, *pz);
59094 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
59095 va_end(ap);
59096 }
59097 }
59098 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
59099 va_list ap;
59100 if( !p ) return;
59101 sqlite3VdbeAddOp0(p, OP_Noop);
59102 assert( p->nOp>0 || p->aOp==0 );
59103 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
59104 if( p->nOp ){
59105 char **pz = &p->aOp[p->nOp-1].zComment;
59106 va_start(ap, zFormat);
59107 sqlite3DbFree(p->db, *pz);
59108 *pz = sqlite3VMPrintf(p->db, zFormat, ap);
59109 va_end(ap);
59110 }
59111 }
59112 #endif /* NDEBUG */
59113
@@ -59441,11 +59306,11 @@
59441 SubProgram **apSub = 0; /* Array of sub-vdbes */
59442 Mem *pSub = 0; /* Memory cell hold array of subprogs */
59443 sqlite3 *db = p->db; /* The database connection */
59444 int i; /* Loop counter */
59445 int rc = SQLITE_OK; /* Return code */
59446 Mem *pMem = p->pResultSet = &p->aMem[1]; /* First Mem of result set */
59447
59448 assert( p->explain );
59449 assert( p->magic==VDBE_MAGIC_RUN );
59450 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
59451
@@ -59452,10 +59317,11 @@
59452 /* Even though this opcode does not use dynamic strings for
59453 ** the result, result columns may become dynamic if the user calls
59454 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
59455 */
59456 releaseMemArray(pMem, 8);
 
59457
59458 if( p->rc==SQLITE_NOMEM ){
59459 /* This happens if a malloc() inside a call to sqlite3_column_text() or
59460 ** sqlite3_column_text16() failed. */
59461 db->mallocFailed = 1;
@@ -59606,10 +59472,11 @@
59606 pMem->type = SQLITE_NULL;
59607 }
59608 }
59609
59610 p->nResColumn = 8 - 4*(p->explain-1);
 
59611 p->rc = SQLITE_OK;
59612 rc = SQLITE_ROW;
59613 }
59614 return rc;
59615 }
@@ -61361,11 +61228,11 @@
61361 ** than 2GiB are support - anything large must be database corruption.
61362 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
61363 ** this code can safely assume that nCellKey is 32-bits
61364 */
61365 assert( sqlite3BtreeCursorIsValid(pCur) );
61366 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61367 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
61368 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
61369
61370 /* Read in the complete content of the index entry */
61371 memset(&m, 0, sizeof(m));
@@ -61436,11 +61303,11 @@
61436 int rc;
61437 BtCursor *pCur = pC->pCursor;
61438 Mem m;
61439
61440 assert( sqlite3BtreeCursorIsValid(pCur) );
61441 rc = sqlite3BtreeKeySize(pCur, &nCellKey);
61442 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
61443 /* nCellKey will always be between 0 and 0xffffffff because of the say
61444 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
61445 if( nCellKey<=0 || nCellKey>0x7fffffff ){
61446 *res = 0;
@@ -65712,20 +65579,20 @@
65712 }else if( u.am.pC->cacheStatus==p->cacheCtr ){
65713 u.am.payloadSize = u.am.pC->payloadSize;
65714 u.am.zRec = (char*)u.am.pC->aRow;
65715 }else if( u.am.pC->isIndex ){
65716 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65717 rc = sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
65718 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
65719 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65720 ** payload size, so it is impossible for u.am.payloadSize64 to be
65721 ** larger than 32 bits. */
65722 assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
65723 u.am.payloadSize = (u32)u.am.payloadSize64;
65724 }else{
65725 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65726 rc = sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65727 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65728 }
65729 }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65730 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65731 assert( u.am.pReg->flags & MEM_Blob );
@@ -67773,18 +67640,18 @@
67773 rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67774 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67775
67776 if( u.bk.pC->isIndex ){
67777 assert( !u.bk.pC->isTable );
67778 rc = sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67779 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67780 if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67781 goto too_big;
67782 }
67783 u.bk.n = (u32)u.bk.n64;
67784 }else{
67785 rc = sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67786 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67787 if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67788 goto too_big;
67789 }
67790 }
@@ -73417,11 +73284,12 @@
73417 pNew->flags |= EP_IntValue;
73418 pNew->u.iValue = iValue;
73419 }else{
73420 int c;
73421 pNew->u.zToken = (char*)&pNew[1];
73422 memcpy(pNew->u.zToken, pToken->z, pToken->n);
 
73423 pNew->u.zToken[pToken->n] = 0;
73424 if( dequote && nExtra>=3
73425 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
73426 sqlite3Dequote(pNew->u.zToken);
73427 if( c=='"' ) pNew->flags |= EP_DblQuoted;
@@ -74456,15 +74324,23 @@
74456 ** ephemeral table.
74457 */
74458 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
74459 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
74460 sqlite3 *db = pParse->db; /* Database connection */
74461 Expr *pExpr = p->pEList->a[0].pExpr; /* Expression <column> */
74462 int iCol = pExpr->iColumn; /* Index of column <column> */
74463 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
74464 Table *pTab = p->pSrc->a[0].pTab; /* Table <table>. */
 
 
74465 int iDb; /* Database idx for pTab */
 
 
 
 
 
 
 
 
74466
74467 /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
74468 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
74469 sqlite3CodeVerifySchema(pParse, iDb);
74470 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
@@ -76467,11 +76343,11 @@
76467 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
76468 return 2;
76469 }
76470 }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
76471 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
76472 if( sqlite3StrICmp(pA->u.zToken,pB->u.zToken)!=0 ){
76473 return 2;
76474 }
76475 }
76476 if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
76477 if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
@@ -77610,10 +77486,112 @@
77610 ** May you find forgiveness for yourself and forgive others.
77611 ** May you share freely, never taking more than you give.
77612 **
77613 *************************************************************************
77614 ** This file contains code associated with the ANALYZE command.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77615 */
77616 #ifndef SQLITE_OMIT_ANALYZE
77617
77618 /*
77619 ** This routine generates code that opens the sqlite_stat1 table for
@@ -77641,12 +77619,12 @@
77641 static const struct {
77642 const char *zName;
77643 const char *zCols;
77644 } aTable[] = {
77645 { "sqlite_stat1", "tbl,idx,stat" },
77646 #ifdef SQLITE_ENABLE_STAT2
77647 { "sqlite_stat2", "tbl,idx,sampleno,sample" },
77648 #endif
77649 };
77650
77651 int aRoot[] = {0, 0};
77652 u8 aCreateTbl[] = {0, 0};
@@ -77658,10 +77636,13 @@
77658 if( v==0 ) return;
77659 assert( sqlite3BtreeHoldsAllMutexes(db) );
77660 assert( sqlite3VdbeDb(v)==db );
77661 pDb = &db->aDb[iDb];
77662
 
 
 
77663 for(i=0; i<ArraySize(aTable); i++){
77664 const char *zTab = aTable[i].zName;
77665 Table *pStat;
77666 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
77667 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -77688,17 +77669,237 @@
77688 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
77689 }
77690 }
77691 }
77692
77693 /* Open the sqlite_stat[12] tables for writing. */
77694 for(i=0; i<ArraySize(aTable); i++){
77695 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
77696 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
77697 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
77698 }
77699 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77700
77701 /*
77702 ** Generate code to do an analysis of all indices associated with
77703 ** a single table.
77704 */
@@ -77718,24 +77919,31 @@
77718 int endOfLoop; /* The end of the loop */
77719 int jZeroRows = -1; /* Jump from here if number of rows is zero */
77720 int iDb; /* Index of database containing pTab */
77721 int regTabname = iMem++; /* Register containing table name */
77722 int regIdxname = iMem++; /* Register containing index name */
77723 int regSampleno = iMem++; /* Register containing next sample number */
77724 int regCol = iMem++; /* Content of a column analyzed table */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77725 int regRec = iMem++; /* Register holding completed record */
77726 int regTemp = iMem++; /* Temporary use register */
77727 int regRowid = iMem++; /* Rowid for the inserted record */
77728
77729 #ifdef SQLITE_ENABLE_STAT2
77730 int addr = 0; /* Instruction address */
77731 int regTemp2 = iMem++; /* Temporary use register */
77732 int regSamplerecno = iMem++; /* Index of next sample to record */
77733 int regRecno = iMem++; /* Current sample index */
77734 int regLast = iMem++; /* Index of last sample to record */
77735 int regFirst = iMem++; /* Index of first sample to record */
77736 #endif
77737
77738 v = sqlite3GetVdbe(pParse);
77739 if( v==0 || NEVER(pTab==0) ){
77740 return;
77741 }
@@ -77764,13 +77972,18 @@
77764 iIdxCur = pParse->nTab++;
77765 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
77766 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
77767 int nCol;
77768 KeyInfo *pKey;
 
 
77769
77770 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
 
77771 nCol = pIdx->nColumn;
 
 
77772 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
77773 if( iMem+1+(nCol*2)>pParse->nMem ){
77774 pParse->nMem = iMem+1+(nCol*2);
77775 }
77776
@@ -77781,35 +77994,24 @@
77781 VdbeComment((v, "%s", pIdx->zName));
77782
77783 /* Populate the register containing the index name. */
77784 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
77785
77786 #ifdef SQLITE_ENABLE_STAT2
77787
77788 /* If this iteration of the loop is generating code to analyze the
77789 ** first index in the pTab->pIndex list, then register regLast has
77790 ** not been populated. In this case populate it now. */
77791 if( pTab->pIndex==pIdx ){
77792 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regSamplerecno);
77793 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2-1, regTemp);
77794 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES*2, regTemp2);
77795
77796 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regLast);
77797 sqlite3VdbeAddOp2(v, OP_Null, 0, regFirst);
77798 addr = sqlite3VdbeAddOp3(v, OP_Lt, regSamplerecno, 0, regLast);
77799 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regFirst);
77800 sqlite3VdbeAddOp3(v, OP_Multiply, regLast, regTemp, regLast);
77801 sqlite3VdbeAddOp2(v, OP_AddImm, regLast, SQLITE_INDEX_SAMPLES*2-2);
77802 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regLast, regLast);
77803 sqlite3VdbeJumpHere(v, addr);
77804 }
77805
77806 /* Zero the regSampleno and regRecno registers. */
77807 sqlite3VdbeAddOp2(v, OP_Integer, 0, regSampleno);
77808 sqlite3VdbeAddOp2(v, OP_Integer, 0, regRecno);
77809 sqlite3VdbeAddOp2(v, OP_Copy, regFirst, regSamplerecno);
77810 #endif
77811
77812 /* The block of memory cells initialized here is used as follows.
77813 **
77814 ** iMem:
77815 ** The total number of rows in the table.
@@ -77835,79 +78037,87 @@
77835 /* Start the analysis loop. This loop runs through all the entries in
77836 ** the index b-tree. */
77837 endOfLoop = sqlite3VdbeMakeLabel(v);
77838 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
77839 topOfLoop = sqlite3VdbeCurrentAddr(v);
77840 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1);
77841
77842 for(i=0; i<nCol; i++){
77843 CollSeq *pColl;
77844 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
77845 if( i==0 ){
77846 #ifdef SQLITE_ENABLE_STAT2
77847 /* Check if the record that cursor iIdxCur points to contains a
77848 ** value that should be stored in the sqlite_stat2 table. If so,
77849 ** store it. */
77850 int ne = sqlite3VdbeAddOp3(v, OP_Ne, regRecno, 0, regSamplerecno);
77851 assert( regTabname+1==regIdxname
77852 && regTabname+2==regSampleno
77853 && regTabname+3==regCol
77854 );
77855 sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
77856 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 4, regRec, "aaab", 0);
77857 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regRowid);
77858 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regRowid);
77859
77860 /* Calculate new values for regSamplerecno and regSampleno.
77861 **
77862 ** sampleno = sampleno + 1
77863 ** samplerecno = samplerecno+(remaining records)/(remaining samples)
77864 */
77865 sqlite3VdbeAddOp2(v, OP_AddImm, regSampleno, 1);
77866 sqlite3VdbeAddOp3(v, OP_Subtract, regRecno, regLast, regTemp);
77867 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77868 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_INDEX_SAMPLES, regTemp2);
77869 sqlite3VdbeAddOp3(v, OP_Subtract, regSampleno, regTemp2, regTemp2);
77870 sqlite3VdbeAddOp3(v, OP_Divide, regTemp2, regTemp, regTemp);
77871 sqlite3VdbeAddOp3(v, OP_Add, regSamplerecno, regTemp, regSamplerecno);
77872
77873 sqlite3VdbeJumpHere(v, ne);
77874 sqlite3VdbeAddOp2(v, OP_AddImm, regRecno, 1);
77875 #endif
77876
77877 /* Always record the very first row */
77878 sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
77879 }
77880 assert( pIdx->azColl!=0 );
77881 assert( pIdx->azColl[i]!=0 );
77882 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
77883 sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
77884 (char*)pColl, P4_COLLSEQ);
77885 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
77886 }
77887 if( db->mallocFailed ){
77888 /* If a malloc failure has occurred, then the result of the expression
77889 ** passed as the second argument to the call to sqlite3VdbeJumpHere()
77890 ** below may be negative. Which causes an assert() to fail (or an
77891 ** out-of-bounds write if SQLITE_DEBUG is not defined). */
77892 return;
77893 }
77894 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
77895 for(i=0; i<nCol; i++){
77896 int addr2 = sqlite3VdbeCurrentAddr(v) - (nCol*2);
77897 if( i==0 ){
77898 sqlite3VdbeJumpHere(v, addr2-1); /* Set jump dest for the OP_IfNot */
 
 
 
 
 
 
 
 
 
77899 }
77900 sqlite3VdbeJumpHere(v, addr2); /* Set jump dest for the OP_Ne */
77901 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
77902 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
77903 }
 
77904
77905 /* End of the analysis loop. */
77906 sqlite3VdbeResolveLabel(v, endOfLoop);
 
77907 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
77908 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77909
77910 /* Store the results in sqlite_stat1.
77911 **
77912 ** The result is a single row of the sqlite_stat1 table. The first
77913 ** two columns are the names of the table and index. The third column
@@ -77923,50 +78133,51 @@
77923 **
77924 ** If K==0 then no entry is made into the sqlite_stat1 table.
77925 ** If K>0 then it is always the case the D>0 so division by zero
77926 ** is never possible.
77927 */
77928 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regSampleno);
77929 if( jZeroRows<0 ){
77930 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
77931 }
77932 for(i=0; i<nCol; i++){
77933 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
77934 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77935 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
77936 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
77937 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
77938 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
77939 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regSampleno, regSampleno);
77940 }
77941 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77942 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77943 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77944 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77945 }
77946
77947 /* If the table has no indices, create a single sqlite_stat1 entry
77948 ** containing NULL as the index name and the row count as the content.
77949 */
77950 if( pTab->pIndex==0 ){
77951 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
77952 VdbeComment((v, "%s", pTab->zName));
77953 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regSampleno);
77954 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
77955 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regSampleno);
77956 }else{
77957 sqlite3VdbeJumpHere(v, jZeroRows);
77958 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
77959 }
77960 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
77961 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
77962 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regRowid);
77963 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regRowid);
77964 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
77965 if( pParse->nMem<regRec ) pParse->nMem = regRec;
77966 sqlite3VdbeJumpHere(v, jZeroRows);
77967 }
 
77968
77969 /*
77970 ** Generate code that will cause the most recent index analysis to
77971 ** be loaded into internal hash tables where is can be used.
77972 */
@@ -77987,11 +78198,11 @@
77987 int iStatCur;
77988 int iMem;
77989
77990 sqlite3BeginWriteOperation(pParse, 0, iDb);
77991 iStatCur = pParse->nTab;
77992 pParse->nTab += 2;
77993 openStatTable(pParse, iDb, iStatCur, 0, 0);
77994 iMem = pParse->nMem+1;
77995 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
77996 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
77997 Table *pTab = (Table*)sqliteHashData(k);
@@ -78012,11 +78223,11 @@
78012 assert( pTab!=0 );
78013 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
78014 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78015 sqlite3BeginWriteOperation(pParse, 0, iDb);
78016 iStatCur = pParse->nTab;
78017 pParse->nTab += 2;
78018 if( pOnlyIdx ){
78019 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
78020 }else{
78021 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
78022 }
@@ -78117,11 +78328,11 @@
78117 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
78118 analysisInfo *pInfo = (analysisInfo*)pData;
78119 Index *pIndex;
78120 Table *pTable;
78121 int i, c, n;
78122 unsigned int v;
78123 const char *z;
78124
78125 assert( argc==3 );
78126 UNUSED_PARAMETER2(NotUsed, argc);
78127
@@ -78160,40 +78371,172 @@
78160 /*
78161 ** If the Index.aSample variable is not NULL, delete the aSample[] array
78162 ** and its contents.
78163 */
78164 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
78165 #ifdef SQLITE_ENABLE_STAT2
78166 if( pIdx->aSample ){
78167 int j;
78168 for(j=0; j<SQLITE_INDEX_SAMPLES; j++){
78169 IndexSample *p = &pIdx->aSample[j];
78170 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
78171 sqlite3DbFree(db, p->u.z);
78172 }
78173 }
78174 sqlite3DbFree(db, pIdx->aSample);
 
 
 
 
78175 }
78176 #else
78177 UNUSED_PARAMETER(db);
78178 UNUSED_PARAMETER(pIdx);
78179 #endif
78180 }
78181
 
78182 /*
78183 ** Load the content of the sqlite_stat1 and sqlite_stat2 tables. The
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78184 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
78185 ** arrays. The contents of sqlite_stat2 are used to populate the
78186 ** Index.aSample[] arrays.
78187 **
78188 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
78189 ** is returned. In this case, even if SQLITE_ENABLE_STAT2 was defined
78190 ** during compilation and the sqlite_stat2 table is present, no data is
78191 ** read from it.
78192 **
78193 ** If SQLITE_ENABLE_STAT2 was defined during compilation and the
78194 ** sqlite_stat2 table is not present in the database, SQLITE_ERROR is
78195 ** returned. However, in this case, data is read from the sqlite_stat1
78196 ** table (if it is present) before returning.
78197 **
78198 ** If an OOM error occurs, this function always sets db->mallocFailed.
78199 ** This means if the caller does not care about other errors, the return
@@ -78211,12 +78554,14 @@
78211 /* Clear any prior statistics */
78212 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78213 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
78214 Index *pIdx = sqliteHashData(i);
78215 sqlite3DefaultRowEst(pIdx);
 
78216 sqlite3DeleteIndexSamples(db, pIdx);
78217 pIdx->aSample = 0;
 
78218 }
78219
78220 /* Check to make sure the sqlite_stat1 table exists */
78221 sInfo.db = db;
78222 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -78224,91 +78569,23 @@
78224 return SQLITE_ERROR;
78225 }
78226
78227 /* Load new statistics out of the sqlite_stat1 table */
78228 zSql = sqlite3MPrintf(db,
78229 "SELECT tbl, idx, stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78230 if( zSql==0 ){
78231 rc = SQLITE_NOMEM;
78232 }else{
78233 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
78234 sqlite3DbFree(db, zSql);
78235 }
78236
78237
78238 /* Load the statistics from the sqlite_stat2 table. */
78239 #ifdef SQLITE_ENABLE_STAT2
78240 if( rc==SQLITE_OK && !sqlite3FindTable(db, "sqlite_stat2", sInfo.zDatabase) ){
78241 rc = SQLITE_ERROR;
78242 }
78243 if( rc==SQLITE_OK ){
78244 sqlite3_stmt *pStmt = 0;
78245
78246 zSql = sqlite3MPrintf(db,
78247 "SELECT idx,sampleno,sample FROM %Q.sqlite_stat2", sInfo.zDatabase);
78248 if( !zSql ){
78249 rc = SQLITE_NOMEM;
78250 }else{
78251 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78252 sqlite3DbFree(db, zSql);
78253 }
78254
78255 if( rc==SQLITE_OK ){
78256 while( sqlite3_step(pStmt)==SQLITE_ROW ){
78257 char *zIndex; /* Index name */
78258 Index *pIdx; /* Pointer to the index object */
78259
78260 zIndex = (char *)sqlite3_column_text(pStmt, 0);
78261 pIdx = zIndex ? sqlite3FindIndex(db, zIndex, sInfo.zDatabase) : 0;
78262 if( pIdx ){
78263 int iSample = sqlite3_column_int(pStmt, 1);
78264 if( iSample<SQLITE_INDEX_SAMPLES && iSample>=0 ){
78265 int eType = sqlite3_column_type(pStmt, 2);
78266
78267 if( pIdx->aSample==0 ){
78268 static const int sz = sizeof(IndexSample)*SQLITE_INDEX_SAMPLES;
78269 pIdx->aSample = (IndexSample *)sqlite3DbMallocRaw(0, sz);
78270 if( pIdx->aSample==0 ){
78271 db->mallocFailed = 1;
78272 break;
78273 }
78274 memset(pIdx->aSample, 0, sz);
78275 }
78276
78277 assert( pIdx->aSample );
78278 {
78279 IndexSample *pSample = &pIdx->aSample[iSample];
78280 pSample->eType = (u8)eType;
78281 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
78282 pSample->u.r = sqlite3_column_double(pStmt, 2);
78283 }else if( eType==SQLITE_TEXT || eType==SQLITE_BLOB ){
78284 const char *z = (const char *)(
78285 (eType==SQLITE_BLOB) ?
78286 sqlite3_column_blob(pStmt, 2):
78287 sqlite3_column_text(pStmt, 2)
78288 );
78289 int n = sqlite3_column_bytes(pStmt, 2);
78290 if( n>24 ){
78291 n = 24;
78292 }
78293 pSample->nByte = (u8)n;
78294 if( n < 1){
78295 pSample->u.z = 0;
78296 }else{
78297 pSample->u.z = sqlite3DbStrNDup(0, z, n);
78298 if( pSample->u.z==0 ){
78299 db->mallocFailed = 1;
78300 break;
78301 }
78302 }
78303 }
78304 }
78305 }
78306 }
78307 }
78308 rc = sqlite3_finalize(pStmt);
78309 }
78310 }
78311 #endif
78312
78313 if( rc==SQLITE_NOMEM ){
78314 db->mallocFailed = 1;
@@ -81120,11 +81397,15 @@
81120 Parse *pParse, /* The parsing context */
81121 int iDb, /* The database number */
81122 const char *zType, /* "idx" or "tbl" */
81123 const char *zName /* Name of index or table */
81124 ){
81125 static const char *azStatTab[] = { "sqlite_stat1", "sqlite_stat2" };
 
 
 
 
81126 int i;
81127 const char *zDbName = pParse->db->aDb[iDb].zName;
81128 for(i=0; i<ArraySize(azStatTab); i++){
81129 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
81130 sqlite3NestedParse(pParse,
@@ -81132,10 +81413,80 @@
81132 zDbName, azStatTab[i], zType, zName
81133 );
81134 }
81135 }
81136 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81137
81138 /*
81139 ** This routine is called to do the work of a DROP TABLE statement.
81140 ** pName is the name of the table to be dropped.
81141 */
@@ -81201,11 +81552,12 @@
81201 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
81202 goto exit_drop_table;
81203 }
81204 }
81205 #endif
81206 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
 
81207 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
81208 goto exit_drop_table;
81209 }
81210
81211 #ifndef SQLITE_OMIT_VIEW
@@ -81225,72 +81577,15 @@
81225 /* Generate code to remove the table from the master table
81226 ** on disk.
81227 */
81228 v = sqlite3GetVdbe(pParse);
81229 if( v ){
81230 Trigger *pTrigger;
81231 Db *pDb = &db->aDb[iDb];
81232 sqlite3BeginWriteOperation(pParse, 1, iDb);
81233
81234 #ifndef SQLITE_OMIT_VIRTUALTABLE
81235 if( IsVirtual(pTab) ){
81236 sqlite3VdbeAddOp0(v, OP_VBegin);
81237 }
81238 #endif
81239 sqlite3FkDropTable(pParse, pName, pTab);
81240
81241 /* Drop all triggers associated with the table being dropped. Code
81242 ** is generated to remove entries from sqlite_master and/or
81243 ** sqlite_temp_master if required.
81244 */
81245 pTrigger = sqlite3TriggerList(pParse, pTab);
81246 while( pTrigger ){
81247 assert( pTrigger->pSchema==pTab->pSchema ||
81248 pTrigger->pSchema==db->aDb[1].pSchema );
81249 sqlite3DropTriggerPtr(pParse, pTrigger);
81250 pTrigger = pTrigger->pNext;
81251 }
81252
81253 #ifndef SQLITE_OMIT_AUTOINCREMENT
81254 /* Remove any entries of the sqlite_sequence table associated with
81255 ** the table being dropped. This is done before the table is dropped
81256 ** at the btree level, in case the sqlite_sequence table needs to
81257 ** move as a result of the drop (can happen in auto-vacuum mode).
81258 */
81259 if( pTab->tabFlags & TF_Autoincrement ){
81260 sqlite3NestedParse(pParse,
81261 "DELETE FROM %s.sqlite_sequence WHERE name=%Q",
81262 pDb->zName, pTab->zName
81263 );
81264 }
81265 #endif
81266
81267 /* Drop all SQLITE_MASTER table and index entries that refer to the
81268 ** table. The program name loops through the master table and deletes
81269 ** every row that refers to a table of the same name as the one being
81270 ** dropped. Triggers are handled seperately because a trigger can be
81271 ** created in the temp database that refers to a table in another
81272 ** database.
81273 */
81274 sqlite3NestedParse(pParse,
81275 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81276 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81277 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
81278 if( !isView && !IsVirtual(pTab) ){
81279 destroyTable(pParse, pTab);
81280 }
81281
81282 /* Remove the table entry from SQLite's internal schema and modify
81283 ** the schema cookie.
81284 */
81285 if( IsVirtual(pTab) ){
81286 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81287 }
81288 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81289 sqlite3ChangeCookie(pParse, iDb);
81290 }
81291 sqliteViewResetAll(db, iDb);
81292
81293 exit_drop_table:
81294 sqlite3SrcListDelete(db, pName);
81295 }
81296
@@ -81454,17 +81749,19 @@
81454 */
81455 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
81456 Table *pTab = pIndex->pTable; /* The table that is indexed */
81457 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
81458 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81459 int iSorter = iTab; /* Cursor opened by OpenSorter (if in use) */
81460 int addr1; /* Address of top of loop */
81461 int addr2; /* Address to jump to for next iteration */
81462 int tnum; /* Root page of index */
81463 Vdbe *v; /* Generate code into this virtual machine */
81464 KeyInfo *pKey; /* KeyInfo for index */
 
81465 int regIdxKey; /* Registers containing the index key */
 
81466 int regRecord; /* Register holding assemblied index record */
81467 sqlite3 *db = pParse->db; /* The database connection */
81468 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
81469
81470 #ifndef SQLITE_OMIT_AUTHORIZATION
@@ -81494,21 +81791,22 @@
81494
81495 #ifndef SQLITE_OMIT_MERGE_SORT
81496 /* Open the sorter cursor if we are to use one. */
81497 iSorter = pParse->nTab++;
81498 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
 
 
81499 #endif
81500
81501 /* Open the table. Loop through all rows of the table, inserting index
81502 ** records into the sorter. */
81503 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
81504 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
81505 addr2 = addr1 + 1;
81506 regRecord = sqlite3GetTempReg(pParse);
81507 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81508
81509 #ifndef SQLITE_OMIT_MERGE_SORT
 
81510 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
81511 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81512 sqlite3VdbeJumpHere(v, addr1);
81513 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
81514 if( pIndex->onError!=OE_None ){
@@ -81524,10 +81822,12 @@
81524 }
81525 sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
81526 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
81527 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81528 #else
 
 
81529 if( pIndex->onError!=OE_None ){
81530 const int regRowid = regIdxKey + pIndex->nColumn;
81531 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
81532 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
81533
@@ -81621,10 +81921,11 @@
81621 ** before looking up the table.
81622 */
81623 assert( pName1 && pName2 );
81624 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
81625 if( iDb<0 ) goto exit_create_index;
 
81626
81627 #ifndef SQLITE_OMIT_TEMPDB
81628 /* If the index name was unqualified, check if the the table
81629 ** is a temp table. If so, set the database to 1. Do not do this
81630 ** if initialising a database schema.
@@ -81648,10 +81949,11 @@
81648 pTblName->a[0].zDatabase);
81649 if( !pTab || db->mallocFailed ) goto exit_create_index;
81650 assert( db->aDb[iDb].pSchema==pTab->pSchema );
81651 }else{
81652 assert( pName==0 );
 
81653 pTab = pParse->pNewTable;
81654 if( !pTab ) goto exit_create_index;
81655 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81656 }
81657 pDb = &db->aDb[iDb];
@@ -81690,10 +81992,11 @@
81690 ** own name.
81691 */
81692 if( pName ){
81693 zName = sqlite3NameFromToken(db, pName);
81694 if( zName==0 ) goto exit_create_index;
 
81695 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
81696 goto exit_create_index;
81697 }
81698 if( !db->init.busy ){
81699 if( sqlite3FindTable(db, zName, 0)!=0 ){
@@ -81769,24 +82072,24 @@
81769 */
81770 nName = sqlite3Strlen30(zName);
81771 nCol = pList->nExpr;
81772 pIndex = sqlite3DbMallocZero(db,
81773 sizeof(Index) + /* Index structure */
 
81774 sizeof(int)*nCol + /* Index.aiColumn */
81775 sizeof(int)*(nCol+1) + /* Index.aiRowEst */
81776 sizeof(char *)*nCol + /* Index.azColl */
81777 sizeof(u8)*nCol + /* Index.aSortOrder */
81778 nName + 1 + /* Index.zName */
81779 nExtra /* Collation sequence names */
81780 );
81781 if( db->mallocFailed ){
81782 goto exit_create_index;
81783 }
81784 pIndex->azColl = (char**)(&pIndex[1]);
 
81785 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
81786 pIndex->aiRowEst = (unsigned *)(&pIndex->aiColumn[nCol]);
81787 pIndex->aSortOrder = (u8 *)(&pIndex->aiRowEst[nCol+1]);
81788 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
81789 zExtra = (char *)(&pIndex->zName[nName+1]);
81790 memcpy(pIndex->zName, zName, nName+1);
81791 pIndex->pTable = pTab;
81792 pIndex->nColumn = pList->nExpr;
@@ -82059,13 +82362,13 @@
82059 ** Apart from that, we have little to go on besides intuition as to
82060 ** how aiRowEst[] should be initialized. The numbers generated here
82061 ** are based on typical values found in actual indices.
82062 */
82063 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
82064 unsigned *a = pIdx->aiRowEst;
82065 int i;
82066 unsigned n;
82067 assert( a!=0 );
82068 a[0] = pIdx->pTable->nRowEst;
82069 if( a[0]<10 ) a[0] = 10;
82070 n = 10;
82071 for(i=1; i<=pIdx->nColumn; i++){
@@ -82545,17 +82848,14 @@
82545
82546 /*
82547 ** Commit a transaction
82548 */
82549 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
82550 sqlite3 *db;
82551 Vdbe *v;
82552
82553 assert( pParse!=0 );
82554 db = pParse->db;
82555 assert( db!=0 );
82556 /* if( db->aDb[0].pBt==0 ) return; */
82557 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
82558 return;
82559 }
82560 v = sqlite3GetVdbe(pParse);
82561 if( v ){
@@ -82565,17 +82865,14 @@
82565
82566 /*
82567 ** Rollback a transaction
82568 */
82569 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
82570 sqlite3 *db;
82571 Vdbe *v;
82572
82573 assert( pParse!=0 );
82574 db = pParse->db;
82575 assert( db!=0 );
82576 /* if( db->aDb[0].pBt==0 ) return; */
82577 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
82578 return;
82579 }
82580 v = sqlite3GetVdbe(pParse);
82581 if( v ){
@@ -84377,20 +84674,19 @@
84377 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84378 assert( z2==(char*)sqlite3_value_text(argv[0]) );
84379 if( z2 ){
84380 z1 = contextMalloc(context, ((i64)n)+1);
84381 if( z1 ){
84382 memcpy(z1, z2, n+1);
84383 for(i=0; z1[i]; i++){
84384 z1[i] = (char)sqlite3Toupper(z1[i]);
84385 }
84386 sqlite3_result_text(context, z1, -1, sqlite3_free);
84387 }
84388 }
84389 }
84390 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84391 u8 *z1;
84392 const char *z2;
84393 int i, n;
84394 UNUSED_PARAMETER(argc);
84395 z2 = (char*)sqlite3_value_text(argv[0]);
84396 n = sqlite3_value_bytes(argv[0]);
@@ -84397,15 +84693,14 @@
84397 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84398 assert( z2==(char*)sqlite3_value_text(argv[0]) );
84399 if( z2 ){
84400 z1 = contextMalloc(context, ((i64)n)+1);
84401 if( z1 ){
84402 memcpy(z1, z2, n+1);
84403 for(i=0; z1[i]; i++){
84404 z1[i] = sqlite3Tolower(z1[i]);
84405 }
84406 sqlite3_result_text(context, (char *)z1, -1, sqlite3_free);
84407 }
84408 }
84409 }
84410
84411
@@ -86778,10 +87073,11 @@
86778 sqlite3SelectDelete(db, pSelect);
86779 if( db->mallocFailed==1 ){
86780 fkTriggerDelete(db, pTrigger);
86781 return 0;
86782 }
 
86783
86784 switch( action ){
86785 case OE_Restrict:
86786 pStep->op = TK_SELECT;
86787 break;
@@ -88621,10 +88917,13 @@
88621 */
88622 if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
88623 return 0;
88624 }
88625 #endif
 
 
 
88626
88627 /* If we get this far, it means either:
88628 **
88629 ** * We can always do the transfer if the table contains an
88630 ** an integer primary key
@@ -89698,11 +89997,11 @@
89698 sqlite3_vfs *pVfs = db->pVfs;
89699 void *handle;
89700 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
89701 char *zErrmsg = 0;
89702 void **aHandle;
89703 const int nMsg = 300;
89704
89705 if( pzErrMsg ) *pzErrMsg = 0;
89706
89707 /* Ticket #1863. To avoid a creating security problems for older
89708 ** applications that relink against newer versions of SQLite, the
@@ -89735,10 +90034,11 @@
89735 }
89736 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
89737 sqlite3OsDlSym(pVfs, handle, zProc);
89738 if( xInit==0 ){
89739 if( pzErrMsg ){
 
89740 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
89741 if( zErrmsg ){
89742 sqlite3_snprintf(nMsg, zErrmsg,
89743 "no entry point [%s] in shared library [%s]", zProc,zFile);
89744 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
@@ -90420,11 +90720,11 @@
90420 ){
90421 int iReg;
90422 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90423 sqlite3CodeVerifySchema(pParse, iDb);
90424 iReg = ++pParse->nMem;
90425 if( zLeft[0]=='p' ){
90426 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
90427 }else{
90428 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
90429 }
90430 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
@@ -90486,12 +90786,14 @@
90486 */
90487 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
90488 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
90489 int ii; /* Loop counter */
90490
90491 /* Force the schema to be loaded on all databases. This cases all
90492 ** database files to be opened and the journal_modes set. */
 
 
90493 if( sqlite3ReadSchema(pParse) ){
90494 goto pragma_out;
90495 }
90496
90497 sqlite3VdbeSetNumCols(v, 1);
@@ -91031,11 +91333,11 @@
91031 { OP_IfNeg, 1, 0, 0}, /* 1 */
91032 { OP_String8, 0, 3, 0}, /* 2 */
91033 { OP_ResultRow, 3, 1, 0},
91034 };
91035
91036 int isQuick = (zLeft[0]=='q');
91037
91038 /* Initialize the VDBE program */
91039 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91040 pParse->nMem = 6;
91041 sqlite3VdbeSetNumCols(v, 1);
@@ -92406,10 +92708,11 @@
92406 Select standin;
92407 sqlite3 *db = pParse->db;
92408 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
92409 assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
92410 if( pNew==0 ){
 
92411 pNew = &standin;
92412 memset(pNew, 0, sizeof(*pNew));
92413 }
92414 if( pEList==0 ){
92415 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
@@ -92433,10 +92736,11 @@
92433 if( pNew!=&standin ) sqlite3DbFree(db, pNew);
92434 pNew = 0;
92435 }else{
92436 assert( pNew->pSrc!=0 || pParse->nErr>0 );
92437 }
 
92438 return pNew;
92439 }
92440
92441 /*
92442 ** Delete the given Select structure and all of its substructures.
@@ -93611,11 +93915,14 @@
93611 /* If the column contains an "AS <name>" phrase, use <name> as the name */
93612 zName = sqlite3DbStrDup(db, zName);
93613 }else{
93614 Expr *pColExpr = p; /* The expression that is the result column name */
93615 Table *pTab; /* Table associated with this expression */
93616 while( pColExpr->op==TK_DOT ) pColExpr = pColExpr->pRight;
 
 
 
93617 if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
93618 /* For columns use the column name name */
93619 int iCol = pColExpr->iColumn;
93620 pTab = pColExpr->pTab;
93621 if( iCol<0 ) iCol = pTab->iPKey;
@@ -98609,10 +98916,11 @@
98609 break;
98610 }
98611 }
98612 }
98613 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 
98614 if( openAll || aRegIdx[i]>0 ){
98615 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
98616 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
98617 (char*)pKey, P4_KEYINFO_HANDOFF);
98618 assert( pParse->nTab>iCur+i+1 );
@@ -98782,10 +99090,11 @@
98782 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
98783 sqlite3VdbeJumpHere(v, addr);
98784
98785 /* Close all tables */
98786 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
 
98787 if( openAll || aRegIdx[i]>0 ){
98788 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
98789 }
98790 }
98791 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
@@ -98969,11 +99278,11 @@
98969 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
98970 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
98971 return sqlite3_errcode(db);
98972 }
98973 VVA_ONLY( rc = ) sqlite3_step(pStmt);
98974 assert( rc!=SQLITE_ROW );
98975 return vacuumFinalize(db, pStmt, pzErrMsg);
98976 }
98977
98978 /*
98979 ** Execute zSql on database db. The statement returns exactly
@@ -99187,17 +99496,15 @@
99187 " WHERE type='view' OR type='trigger'"
99188 " OR (type='table' AND rootpage=0)"
99189 );
99190 if( rc ) goto end_of_vacuum;
99191
99192 /* At this point, unless the main db was completely empty, there is now a
99193 ** transaction open on the vacuum database, but not on the main database.
99194 ** Open a btree level transaction on the main database. This allows a
99195 ** call to sqlite3BtreeCopyFile(). The main database btree level
99196 ** transaction is then committed, so the SQL level never knows it was
99197 ** opened for writing. This way, the SQL transaction used to create the
99198 ** temporary database never needs to be committed.
99199 */
99200 {
99201 u32 meta;
99202 int i;
99203
@@ -100457,25 +100764,35 @@
100457 #define TERM_CODED 0x04 /* This term is already coded */
100458 #define TERM_COPIED 0x08 /* Has a child */
100459 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
100460 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
100461 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
100462 #ifdef SQLITE_ENABLE_STAT2
100463 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
100464 #else
100465 # define TERM_VNULL 0x00 /* Disabled if not using stat2 */
100466 #endif
100467
100468 /*
100469 ** An instance of the following structure holds all information about a
100470 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
 
 
 
 
 
 
 
 
100471 */
100472 struct WhereClause {
100473 Parse *pParse; /* The parser context */
100474 WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */
100475 Bitmask vmask; /* Bitmask identifying virtual table cursors */
 
100476 u8 op; /* Split operator. TK_AND or TK_OR */
 
100477 int nTerm; /* Number of terms */
100478 int nSlot; /* Number of entries in a[] */
100479 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
100480 #if defined(SQLITE_SMALL_STACK)
100481 WhereTerm aStatic[1]; /* Initial static space for a[] */
@@ -100600,18 +100917,21 @@
100600 ** Initialize a preallocated WhereClause structure.
100601 */
100602 static void whereClauseInit(
100603 WhereClause *pWC, /* The WhereClause to be initialized */
100604 Parse *pParse, /* The parsing context */
100605 WhereMaskSet *pMaskSet /* Mapping from table cursor numbers to bitmasks */
 
100606 ){
100607 pWC->pParse = pParse;
100608 pWC->pMaskSet = pMaskSet;
 
100609 pWC->nTerm = 0;
100610 pWC->nSlot = ArraySize(pWC->aStatic);
100611 pWC->a = pWC->aStatic;
100612 pWC->vmask = 0;
 
100613 }
100614
100615 /* Forward reference */
100616 static void whereClauseClear(WhereClause*);
100617
@@ -100923,40 +101243,42 @@
100923 ){
100924 WhereTerm *pTerm;
100925 int k;
100926 assert( iCur>=0 );
100927 op &= WO_ALL;
100928 for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
100929 if( pTerm->leftCursor==iCur
100930 && (pTerm->prereqRight & notReady)==0
100931 && pTerm->u.leftColumn==iColumn
100932 && (pTerm->eOperator & op)!=0
100933 ){
100934 if( pIdx && pTerm->eOperator!=WO_ISNULL ){
100935 Expr *pX = pTerm->pExpr;
100936 CollSeq *pColl;
100937 char idxaff;
100938 int j;
100939 Parse *pParse = pWC->pParse;
100940
100941 idxaff = pIdx->pTable->aCol[iColumn].affinity;
100942 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
100943
100944 /* Figure out the collation sequence required from an index for
100945 ** it to be useful for optimising expression pX. Store this
100946 ** value in variable pColl.
100947 */
100948 assert(pX->pLeft);
100949 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
100950 assert(pColl || pParse->nErr);
100951
100952 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
100953 if( NEVER(j>=pIdx->nColumn) ) return 0;
100954 }
100955 if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
100956 }
100957 return pTerm;
 
 
100958 }
100959 }
100960 return 0;
100961 }
100962
@@ -101029,11 +101351,11 @@
101029 int iCol = pRight->iColumn;
101030 pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
101031 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
101032 z = (char *)sqlite3_value_text(pVal);
101033 }
101034 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-23257-02778 */
101035 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
101036 }else if( op==TK_STRING ){
101037 z = pRight->u.zToken;
101038 }
101039 if( z ){
@@ -101047,11 +101369,11 @@
101047 pPrefix = sqlite3Expr(db, TK_STRING, z);
101048 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
101049 *ppPrefix = pPrefix;
101050 if( op==TK_VARIABLE ){
101051 Vdbe *v = pParse->pVdbe;
101052 sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-23257-02778 */
101053 if( *pisComplete && pRight->u.zToken[1] ){
101054 /* If the rhs of the LIKE expression is a variable, and the current
101055 ** value of the variable means there is no need to invoke the LIKE
101056 ** function, then no OP_Variable will be added to the program.
101057 ** This causes problems for the sqlite3_bind_parameter_name()
@@ -101216,11 +101538,11 @@
101216 assert( pExpr->op==TK_OR );
101217 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
101218 if( pOrInfo==0 ) return;
101219 pTerm->wtFlags |= TERM_ORINFO;
101220 pOrWc = &pOrInfo->wc;
101221 whereClauseInit(pOrWc, pWC->pParse, pMaskSet);
101222 whereSplit(pOrWc, pExpr, TK_OR);
101223 exprAnalyzeAll(pSrc, pOrWc);
101224 if( db->mallocFailed ) return;
101225 assert( pOrWc->nTerm>=2 );
101226
@@ -101243,13 +101565,14 @@
101243 Bitmask b = 0;
101244 pOrTerm->u.pAndInfo = pAndInfo;
101245 pOrTerm->wtFlags |= TERM_ANDINFO;
101246 pOrTerm->eOperator = WO_AND;
101247 pAndWC = &pAndInfo->wc;
101248 whereClauseInit(pAndWC, pWC->pParse, pMaskSet);
101249 whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
101250 exprAnalyzeAll(pSrc, pAndWC);
 
101251 testcase( db->mallocFailed );
101252 if( !db->mallocFailed ){
101253 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
101254 assert( pAndTerm->pExpr );
101255 if( allowedOp(pAndTerm->pExpr->op) ){
@@ -101679,12 +102002,12 @@
101679 pNewTerm->prereqAll = pTerm->prereqAll;
101680 }
101681 }
101682 #endif /* SQLITE_OMIT_VIRTUALTABLE */
101683
101684 #ifdef SQLITE_ENABLE_STAT2
101685 /* When sqlite_stat2 histogram data is available an operator of the
101686 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
101687 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
101688 ** virtual term of that form.
101689 **
101690 ** Note that the virtual term must be tagged with TERM_VNULL. This
@@ -101718,11 +102041,11 @@
101718 pTerm->nChild = 1;
101719 pTerm->wtFlags |= TERM_COPIED;
101720 pNewTerm->prereqAll = pTerm->prereqAll;
101721 }
101722 }
101723 #endif /* SQLITE_ENABLE_STAT2 */
101724
101725 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
101726 ** an index for tables to the left of the join.
101727 */
101728 pTerm->prereqRight |= extraRight;
@@ -102140,14 +102463,17 @@
102140 const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
102141 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
102142 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
102143 WhereTerm *pTerm; /* A single term of the WHERE clause */
102144
102145 /* No OR-clause optimization allowed if the INDEXED BY or NOT INDEXED clauses
102146 ** are used */
102147 if( pSrc->notIndexed || pSrc->pIndex!=0 ){
102148 return;
 
 
 
102149 }
102150
102151 /* Search the WHERE clause terms for a usable WO_OR term. */
102152 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102153 if( pTerm->eOperator==WO_OR
@@ -102172,10 +102498,11 @@
102172 bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
102173 }else if( pOrTerm->leftCursor==iCur ){
102174 WhereClause tempWC;
102175 tempWC.pParse = pWC->pParse;
102176 tempWC.pMaskSet = pWC->pMaskSet;
 
102177 tempWC.op = TK_AND;
102178 tempWC.a = pOrTerm;
102179 tempWC.nTerm = 1;
102180 bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
102181 }else{
@@ -102766,71 +103093,89 @@
102766 */
102767 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
102768 }
102769 #endif /* SQLITE_OMIT_VIRTUALTABLE */
102770
 
102771 /*
102772 ** Argument pIdx is a pointer to an index structure that has an array of
102773 ** SQLITE_INDEX_SAMPLES evenly spaced samples of the first indexed column
102774 ** stored in Index.aSample. These samples divide the domain of values stored
102775 ** the index into (SQLITE_INDEX_SAMPLES+1) regions.
102776 ** Region 0 contains all values less than the first sample value. Region
102777 ** 1 contains values between the first and second samples. Region 2 contains
102778 ** values between samples 2 and 3. And so on. Region SQLITE_INDEX_SAMPLES
102779 ** contains values larger than the last sample.
102780 **
102781 ** If the index contains many duplicates of a single value, then it is
102782 ** possible that two or more adjacent samples can hold the same value.
102783 ** When that is the case, the smallest possible region code is returned
102784 ** when roundUp is false and the largest possible region code is returned
102785 ** when roundUp is true.
102786 **
102787 ** If successful, this function determines which of the regions value
102788 ** pVal lies in, sets *piRegion to the region index (a value between 0
102789 ** and SQLITE_INDEX_SAMPLES+1, inclusive) and returns SQLITE_OK.
102790 ** Or, if an OOM occurs while converting text values between encodings,
102791 ** SQLITE_NOMEM is returned and *piRegion is undefined.
102792 */
102793 #ifdef SQLITE_ENABLE_STAT2
102794 static int whereRangeRegion(
102795 Parse *pParse, /* Database connection */
102796 Index *pIdx, /* Index to consider domain of */
102797 sqlite3_value *pVal, /* Value to consider */
102798 int roundUp, /* Return largest valid region if true */
102799 int *piRegion /* OUT: Region of domain in which value lies */
102800 ){
 
 
 
 
 
 
 
102801 assert( roundUp==0 || roundUp==1 );
102802 if( ALWAYS(pVal) ){
102803 IndexSample *aSample = pIdx->aSample;
102804 int i = 0;
102805 int eType = sqlite3_value_type(pVal);
102806
102807 if( eType==SQLITE_INTEGER || eType==SQLITE_FLOAT ){
102808 double r = sqlite3_value_double(pVal);
102809 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102810 if( aSample[i].eType==SQLITE_NULL ) continue;
102811 if( aSample[i].eType>=SQLITE_TEXT ) break;
102812 if( roundUp ){
102813 if( aSample[i].u.r>r ) break;
102814 }else{
102815 if( aSample[i].u.r>=r ) break;
102816 }
102817 }
102818 }else if( eType==SQLITE_NULL ){
102819 i = 0;
102820 if( roundUp ){
102821 while( i<SQLITE_INDEX_SAMPLES && aSample[i].eType==SQLITE_NULL ) i++;
102822 }
102823 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102824 sqlite3 *db = pParse->db;
102825 CollSeq *pColl;
102826 const u8 *z;
102827 int n;
102828
102829 /* pVal comes from sqlite3ValueFromExpr() so the type cannot be NULL */
102830 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
102831
102832 if( eType==SQLITE_BLOB ){
102833 z = (const u8 *)sqlite3_value_blob(pVal);
102834 pColl = db->pDfltColl;
102835 assert( pColl->enc==SQLITE_UTF8 );
102836 }else{
@@ -102845,16 +103190,16 @@
102845 return SQLITE_NOMEM;
102846 }
102847 assert( z && pColl && pColl->xCmp );
102848 }
102849 n = sqlite3ValueBytes(pVal, pColl->enc);
102850
102851 for(i=0; i<SQLITE_INDEX_SAMPLES; i++){
102852 int c;
102853 int eSampletype = aSample[i].eType;
102854 if( eSampletype==SQLITE_NULL || eSampletype<eType ) continue;
102855 if( (eSampletype!=eType) ) break;
102856 #ifndef SQLITE_OMIT_UTF16
102857 if( pColl->enc!=SQLITE_UTF8 ){
102858 int nSample;
102859 char *zSample = sqlite3Utf8to16(
102860 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -102868,20 +103213,51 @@
102868 }else
102869 #endif
102870 {
102871 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
102872 }
102873 if( c-roundUp>=0 ) break;
 
 
 
102874 }
102875 }
 
102876
102877 assert( i>=0 && i<=SQLITE_INDEX_SAMPLES );
102878 *piRegion = i;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102879 }
102880 return SQLITE_OK;
102881 }
102882 #endif /* #ifdef SQLITE_ENABLE_STAT2 */
102883
102884 /*
102885 ** If expression pExpr represents a literal value, set *pp to point to
102886 ** an sqlite3_value structure containing the same value, with affinity
102887 ** aff applied to it, before returning. It is the responsibility of the
@@ -102895,11 +103271,11 @@
102895 **
102896 ** If neither of the above apply, set *pp to NULL.
102897 **
102898 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
102899 */
102900 #ifdef SQLITE_ENABLE_STAT2
102901 static int valueFromExpr(
102902 Parse *pParse,
102903 Expr *pExpr,
102904 u8 aff,
102905 sqlite3_value **pp
@@ -102906,11 +103282,11 @@
102906 ){
102907 if( pExpr->op==TK_VARIABLE
102908 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
102909 ){
102910 int iVar = pExpr->iColumn;
102911 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-23257-02778 */
102912 *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
102913 return SQLITE_OK;
102914 }
102915 return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
102916 }
@@ -102943,106 +103319,92 @@
102943 **
102944 ** ... FROM t1 WHERE a > ? AND a < ? ...
102945 **
102946 ** then nEq should be passed 0.
102947 **
102948 ** The returned value is an integer between 1 and 100, inclusive. A return
102949 ** value of 1 indicates that the proposed range scan is expected to visit
102950 ** approximately 1/100th (1%) of the rows selected by the nEq equality
102951 ** constraints (if any). A return value of 100 indicates that it is expected
102952 ** that the range scan will visit every row (100%) selected by the equality
102953 ** constraints.
102954 **
102955 ** In the absence of sqlite_stat2 ANALYZE data, each range inequality
102956 ** reduces the search space by 3/4ths. Hence a single constraint (x>?)
102957 ** results in a return of 25 and a range constraint (x>? AND x<?) results
102958 ** in a return of 6.
102959 */
102960 static int whereRangeScanEst(
102961 Parse *pParse, /* Parsing & code generating context */
102962 Index *p, /* The index containing the range-compared column; "x" */
102963 int nEq, /* index into p->aCol[] of the range-compared column */
102964 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
102965 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
102966 int *piEst /* OUT: Return value */
102967 ){
102968 int rc = SQLITE_OK;
102969
102970 #ifdef SQLITE_ENABLE_STAT2
102971
102972 if( nEq==0 && p->aSample ){
102973 sqlite3_value *pLowerVal = 0;
102974 sqlite3_value *pUpperVal = 0;
102975 int iEst;
102976 int iLower = 0;
102977 int iUpper = SQLITE_INDEX_SAMPLES;
102978 int roundUpUpper = 0;
102979 int roundUpLower = 0;
102980 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
102981
102982 if( pLower ){
102983 Expr *pExpr = pLower->pExpr->pRight;
102984 rc = valueFromExpr(pParse, pExpr, aff, &pLowerVal);
102985 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
102986 roundUpLower = (pLower->eOperator==WO_GT) ?1:0;
 
 
 
 
 
 
102987 }
102988 if( rc==SQLITE_OK && pUpper ){
102989 Expr *pExpr = pUpper->pExpr->pRight;
102990 rc = valueFromExpr(pParse, pExpr, aff, &pUpperVal);
102991 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
102992 roundUpUpper = (pUpper->eOperator==WO_LE) ?1:0;
102993 }
102994
102995 if( rc!=SQLITE_OK || (pLowerVal==0 && pUpperVal==0) ){
102996 sqlite3ValueFree(pLowerVal);
102997 sqlite3ValueFree(pUpperVal);
102998 goto range_est_fallback;
102999 }else if( pLowerVal==0 ){
103000 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103001 if( pLower ) iLower = iUpper/2;
103002 }else if( pUpperVal==0 ){
103003 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103004 if( pUpper ) iUpper = (iLower + SQLITE_INDEX_SAMPLES + 1)/2;
103005 }else{
103006 rc = whereRangeRegion(pParse, p, pUpperVal, roundUpUpper, &iUpper);
103007 if( rc==SQLITE_OK ){
103008 rc = whereRangeRegion(pParse, p, pLowerVal, roundUpLower, &iLower);
103009 }
103010 }
103011 WHERETRACE(("range scan regions: %d..%d\n", iLower, iUpper));
103012
103013 iEst = iUpper - iLower;
103014 testcase( iEst==SQLITE_INDEX_SAMPLES );
103015 assert( iEst<=SQLITE_INDEX_SAMPLES );
103016 if( iEst<1 ){
103017 *piEst = 50/SQLITE_INDEX_SAMPLES;
103018 }else{
103019 *piEst = (iEst*100)/SQLITE_INDEX_SAMPLES;
103020 }
103021 sqlite3ValueFree(pLowerVal);
103022 sqlite3ValueFree(pUpperVal);
103023 return rc;
103024 }
103025 range_est_fallback:
103026 #else
103027 UNUSED_PARAMETER(pParse);
103028 UNUSED_PARAMETER(p);
103029 UNUSED_PARAMETER(nEq);
103030 #endif
103031 assert( pLower || pUpper );
103032 *piEst = 100;
103033 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *piEst /= 4;
103034 if( pUpper ) *piEst /= 4;
103035 return rc;
103036 }
103037
103038 #ifdef SQLITE_ENABLE_STAT2
103039 /*
103040 ** Estimate the number of rows that will be returned based on
103041 ** an equality constraint x=VALUE and where that VALUE occurs in
103042 ** the histogram data. This only works when x is the left-most
103043 ** column of an index and sqlite_stat2 histogram data is available
103044 ** for that index. When pExpr==NULL that means the constraint is
103045 ** "x IS NULL" instead of "x=VALUE".
103046 **
103047 ** Write the estimated row count into *pnRow and return SQLITE_OK.
103048 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -103058,44 +103420,36 @@
103058 Index *p, /* The index whose left-most column is pTerm */
103059 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
103060 double *pnRow /* Write the revised row estimate here */
103061 ){
103062 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
103063 int iLower, iUpper; /* Range of histogram regions containing pRhs */
103064 u8 aff; /* Column affinity */
103065 int rc; /* Subfunction return code */
103066 double nRowEst; /* New estimate of the number of rows */
103067
103068 assert( p->aSample!=0 );
 
103069 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103070 if( pExpr ){
103071 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
103072 if( rc ) goto whereEqualScanEst_cancel;
103073 }else{
103074 pRhs = sqlite3ValueNew(pParse->db);
103075 }
103076 if( pRhs==0 ) return SQLITE_NOTFOUND;
103077 rc = whereRangeRegion(pParse, p, pRhs, 0, &iLower);
103078 if( rc ) goto whereEqualScanEst_cancel;
103079 rc = whereRangeRegion(pParse, p, pRhs, 1, &iUpper);
103080 if( rc ) goto whereEqualScanEst_cancel;
103081 WHERETRACE(("equality scan regions: %d..%d\n", iLower, iUpper));
103082 if( iLower>=iUpper ){
103083 nRowEst = p->aiRowEst[0]/(SQLITE_INDEX_SAMPLES*2);
103084 if( nRowEst<*pnRow ) *pnRow = nRowEst;
103085 }else{
103086 nRowEst = (iUpper-iLower)*p->aiRowEst[0]/SQLITE_INDEX_SAMPLES;
103087 *pnRow = nRowEst;
103088 }
103089
103090 whereEqualScanEst_cancel:
103091 sqlite3ValueFree(pRhs);
103092 return rc;
103093 }
103094 #endif /* defined(SQLITE_ENABLE_STAT2) */
103095
103096 #ifdef SQLITE_ENABLE_STAT2
103097 /*
103098 ** Estimate the number of rows that will be returned based on
103099 ** an IN constraint where the right-hand side of the IN operator
103100 ** is a list of values. Example:
103101 **
@@ -103114,64 +103468,29 @@
103114 Parse *pParse, /* Parsing & code generating context */
103115 Index *p, /* The index whose left-most column is pTerm */
103116 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103117 double *pnRow /* Write the revised row estimate here */
103118 ){
103119 sqlite3_value *pVal = 0; /* One value from list */
103120 int iLower, iUpper; /* Range of histogram regions containing pRhs */
103121 u8 aff; /* Column affinity */
103122 int rc = SQLITE_OK; /* Subfunction return code */
103123 double nRowEst; /* New estimate of the number of rows */
103124 int nSpan = 0; /* Number of histogram regions spanned */
103125 int nSingle = 0; /* Histogram regions hit by a single value */
103126 int nNotFound = 0; /* Count of values that are not constants */
103127 int i; /* Loop counter */
103128 u8 aSpan[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions that are spanned */
103129 u8 aSingle[SQLITE_INDEX_SAMPLES+1]; /* Histogram regions hit once */
103130
103131 assert( p->aSample!=0 );
103132 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103133 memset(aSpan, 0, sizeof(aSpan));
103134 memset(aSingle, 0, sizeof(aSingle));
103135 for(i=0; i<pList->nExpr; i++){
103136 sqlite3ValueFree(pVal);
103137 rc = valueFromExpr(pParse, pList->a[i].pExpr, aff, &pVal);
103138 if( rc ) break;
103139 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
103140 nNotFound++;
103141 continue;
103142 }
103143 rc = whereRangeRegion(pParse, p, pVal, 0, &iLower);
103144 if( rc ) break;
103145 rc = whereRangeRegion(pParse, p, pVal, 1, &iUpper);
103146 if( rc ) break;
103147 if( iLower>=iUpper ){
103148 aSingle[iLower] = 1;
103149 }else{
103150 assert( iLower>=0 && iUpper<=SQLITE_INDEX_SAMPLES );
103151 while( iLower<iUpper ) aSpan[iLower++] = 1;
103152 }
103153 }
103154 if( rc==SQLITE_OK ){
103155 for(i=nSpan=0; i<=SQLITE_INDEX_SAMPLES; i++){
103156 if( aSpan[i] ){
103157 nSpan++;
103158 }else if( aSingle[i] ){
103159 nSingle++;
103160 }
103161 }
103162 nRowEst = (nSpan*2+nSingle)*p->aiRowEst[0]/(2*SQLITE_INDEX_SAMPLES)
103163 + nNotFound*p->aiRowEst[1];
103164 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103165 *pnRow = nRowEst;
103166 WHERETRACE(("IN row estimate: nSpan=%d, nSingle=%d, nNotFound=%d, est=%g\n",
103167 nSpan, nSingle, nNotFound, nRowEst));
103168 }
103169 sqlite3ValueFree(pVal);
103170 return rc;
103171 }
103172 #endif /* defined(SQLITE_ENABLE_STAT2) */
103173
103174
103175 /*
103176 ** Find the best query plan for accessing a particular table. Write the
103177 ** best query plan and its cost into the WhereCost object supplied as the
@@ -103214,11 +103533,11 @@
103214 Index *pProbe; /* An index we are evaluating */
103215 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
103216 int eqTermMask; /* Current mask of valid equality operators */
103217 int idxEqTermMask; /* Index mask of valid equality operators */
103218 Index sPk; /* A fake index object for the primary key */
103219 unsigned int aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103220 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
103221 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
103222
103223 /* Initialize the cost to a worst-case value */
103224 memset(pCost, 0, sizeof(*pCost));
@@ -103269,14 +103588,14 @@
103269 }
103270
103271 /* Loop over all indices looking for the best one to use
103272 */
103273 for(; pProbe; pIdx=pProbe=pProbe->pNext){
103274 const unsigned int * const aiRowEst = pProbe->aiRowEst;
103275 double cost; /* Cost of using pProbe */
103276 double nRow; /* Estimated number of rows in result set */
103277 double log10N; /* base-10 logarithm of nRow (inexact) */
103278 int rev; /* True to scan in reverse order */
103279 int wsFlags = 0;
103280 Bitmask used = 0;
103281
103282 /* The following variables are populated based on the properties of
@@ -103312,18 +103631,16 @@
103312 ** Set to true if there was at least one "x IN (SELECT ...)" term used
103313 ** in determining the value of nInMul. Note that the RHS of the
103314 ** IN operator must be a SELECT, not a value list, for this variable
103315 ** to be true.
103316 **
103317 ** estBound:
103318 ** An estimate on the amount of the table that must be searched. A
103319 ** value of 100 means the entire table is searched. Range constraints
103320 ** might reduce this to a value less than 100 to indicate that only
103321 ** a fraction of the table needs searching. In the absence of
103322 ** sqlite_stat2 ANALYZE data, a single inequality reduces the search
103323 ** space to 1/4rd its original size. So an x>? constraint reduces
103324 ** estBound to 25. Two constraints (x>? AND x<?) reduce estBound to 6.
103325 **
103326 ** bSort:
103327 ** Boolean. True if there is an ORDER BY clause that will require an
103328 ** external sort (i.e. scanning the index being evaluated will not
103329 ** correctly order records).
@@ -103344,26 +103661,27 @@
103344 ** SELECT a, b, c FROM tbl WHERE a = 1;
103345 */
103346 int nEq; /* Number of == or IN terms matching index */
103347 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
103348 int nInMul = 1; /* Number of distinct equalities to lookup */
103349 int estBound = 100; /* Estimated reduction in search space */
103350 int nBound = 0; /* Number of range constraints seen */
103351 int bSort = !!pOrderBy; /* True if external sort required */
103352 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
103353 int bLookup = 0; /* True if not a covering index */
103354 WhereTerm *pTerm; /* A single term of the WHERE clause */
103355 #ifdef SQLITE_ENABLE_STAT2
103356 WhereTerm *pFirstTerm = 0; /* First term matching the index */
103357 #endif
103358
103359 /* Determine the values of nEq and nInMul */
103360 for(nEq=0; nEq<pProbe->nColumn; nEq++){
103361 int j = pProbe->aiColumn[nEq];
103362 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
103363 if( pTerm==0 ) break;
103364 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
 
103365 if( pTerm->eOperator & WO_IN ){
103366 Expr *pExpr = pTerm->pExpr;
103367 wsFlags |= WHERE_COLUMN_IN;
103368 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103369 /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
@@ -103374,32 +103692,34 @@
103374 nInMul *= pExpr->x.pList->nExpr;
103375 }
103376 }else if( pTerm->eOperator & WO_ISNULL ){
103377 wsFlags |= WHERE_COLUMN_NULL;
103378 }
103379 #ifdef SQLITE_ENABLE_STAT2
103380 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103381 #endif
103382 used |= pTerm->prereqRight;
103383 }
103384
103385 /* Determine the value of estBound. */
103386 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103387 int j = pProbe->aiColumn[nEq];
103388 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103389 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103390 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103391 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &estBound);
103392 if( pTop ){
103393 nBound = 1;
103394 wsFlags |= WHERE_TOP_LIMIT;
103395 used |= pTop->prereqRight;
 
103396 }
103397 if( pBtm ){
103398 nBound++;
103399 wsFlags |= WHERE_BTM_LIMIT;
103400 used |= pBtm->prereqRight;
 
103401 }
103402 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103403 }
103404 }else if( pProbe->onError!=OE_None ){
103405 testcase( wsFlags & WHERE_COLUMN_IN );
@@ -103458,32 +103778,34 @@
103458 if( bInEst && nRow*2>aiRowEst[0] ){
103459 nRow = aiRowEst[0]/2;
103460 nInMul = (int)(nRow / aiRowEst[nEq]);
103461 }
103462
103463 #ifdef SQLITE_ENABLE_STAT2
103464 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103465 ** and we do not think that values of x are unique and if histogram
103466 ** data is available for column x, then it might be possible
103467 ** to get a better estimate on the number of rows based on
103468 ** VALUE and how common that value is according to the histogram.
103469 */
103470 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
 
103471 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
103472 testcase( pFirstTerm->eOperator==WO_EQ );
103473 testcase( pFirstTerm->eOperator==WO_ISNULL );
103474 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103475 }else if( pFirstTerm->eOperator==WO_IN && bInEst==0 ){
 
103476 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103477 }
103478 }
103479 #endif /* SQLITE_ENABLE_STAT2 */
103480
103481 /* Adjust the number of output rows and downward to reflect rows
103482 ** that are excluded by range constraints.
103483 */
103484 nRow = (nRow * (double)estBound) / (double)100;
103485 if( nRow<1 ) nRow = 1;
103486
103487 /* Experiments run on real SQLite databases show that the time needed
103488 ** to do a binary search to locate a row in a table or index is roughly
103489 ** log10(N) times the time to move from one row to the next row within
@@ -103608,14 +103930,14 @@
103608 if( nRow<2 ) nRow = 2;
103609 }
103610
103611
103612 WHERETRACE((
103613 "%s(%s): nEq=%d nInMul=%d estBound=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103614 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103615 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103616 nEq, nInMul, estBound, bSort, bLookup, wsFlags,
103617 notReady, log10N, nRow, cost, used
103618 ));
103619
103620 /* If this index is the best we have seen so far, then record this
103621 ** index and its cost in the pCost structure.
@@ -104115,11 +104437,12 @@
104115 */
104116 static Bitmask codeOneLoopStart(
104117 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
104118 int iLevel, /* Which level of pWInfo->a[] should be coded */
104119 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
104120 Bitmask notReady /* Which tables are currently available */
 
104121 ){
104122 int j, k; /* Loop counters */
104123 int iCur; /* The VDBE cursor for the table */
104124 int addrNxt; /* Where to jump to continue with the next IN case */
104125 int omitTable; /* True if we use the index only */
@@ -104597,11 +104920,12 @@
104597 int regRowset = 0; /* Register for RowSet object */
104598 int regRowid = 0; /* Register holding rowid */
104599 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
104600 int iRetInit; /* Address of regReturn init */
104601 int untestedTerms = 0; /* Some terms not completely tested */
104602 int ii;
 
104603
104604 pTerm = pLevel->plan.u.pTerm;
104605 assert( pTerm!=0 );
104606 assert( pTerm->eOperator==WO_OR );
104607 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -104646,18 +104970,33 @@
104646 regRowset = ++pParse->nMem;
104647 regRowid = ++pParse->nMem;
104648 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
104649 }
104650 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
 
 
 
 
 
 
 
 
 
 
104651
104652 for(ii=0; ii<pOrWc->nTerm; ii++){
104653 WhereTerm *pOrTerm = &pOrWc->a[ii];
104654 if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
104655 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
 
 
 
 
 
104656 /* Loop through table entries that match term pOrTerm. */
104657 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrTerm->pExpr, 0, 0,
104658 WHERE_OMIT_OPEN | WHERE_OMIT_CLOSE |
104659 WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
104660 if( pSubWInfo ){
104661 explainOneScan(
104662 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
104663 );
@@ -104681,10 +105020,11 @@
104681 /* Finish the loop through table entries that match term pOrTerm. */
104682 sqlite3WhereEnd(pSubWInfo);
104683 }
104684 }
104685 }
 
104686 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
104687 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
104688 sqlite3VdbeResolveLabel(v, iLoopBody);
104689
104690 if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
@@ -104962,11 +105302,11 @@
104962
104963 /* Split the WHERE clause into separate subexpressions where each
104964 ** subexpression is separated by an AND operator.
104965 */
104966 initMaskSet(pMaskSet);
104967 whereClauseInit(pWC, pParse, pMaskSet);
104968 sqlite3ExprCodeConstants(pParse, pWhere);
104969 whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
104970
104971 /* Special case: a WHERE clause that is constant. Evaluate the
104972 ** expression and either jump over all of the code or fall thru.
@@ -105201,11 +105541,12 @@
105201 assert( bestJ>=0 );
105202 assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
105203 WHERETRACE(("*** Optimizer selects table %d for loop %d"
105204 " with cost=%g and nRow=%g\n",
105205 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
105206 if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 ){
 
105207 *ppOrderBy = 0;
105208 }
105209 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
105210 assert( pWInfo->eDistinct==0 );
105211 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
@@ -105290,11 +105631,11 @@
105290 int iCur = pTabItem->iCursor;
105291 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
105292 }else
105293 #endif
105294 if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105295 && (wctrlFlags & WHERE_OMIT_OPEN)==0 ){
105296 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
105297 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
105298 testcase( pTab->nCol==BMS-1 );
105299 testcase( pTab->nCol==BMS );
105300 if( !pWInfo->okOnePass && pTab->nCol<BMS ){
@@ -105335,11 +105676,11 @@
105335 */
105336 notReady = ~(Bitmask)0;
105337 for(i=0; i<nTabList; i++){
105338 pLevel = &pWInfo->a[i];
105339 explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
105340 notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady);
105341 pWInfo->iContinue = pLevel->addrCont;
105342 }
105343
105344 #ifdef SQLITE_TEST /* For testing and debugging use only */
105345 /* Record in the query plan information about the current table
@@ -105470,11 +105811,11 @@
105470 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
105471 Table *pTab = pTabItem->pTab;
105472 assert( pTab!=0 );
105473 if( (pTab->tabFlags & TF_Ephemeral)==0
105474 && pTab->pSelect==0
105475 && (pWInfo->wctrlFlags & WHERE_OMIT_CLOSE)==0
105476 ){
105477 int ws = pLevel->plan.wsFlags;
105478 if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
105479 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
105480 }
@@ -108817,11 +109158,13 @@
108817 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
108818 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
108819 ){
108820 YYMINORTYPE yyminorunion;
108821 int yyact; /* The parser action. */
 
108822 int yyendofinput; /* True if we are at the end of input */
 
108823 #ifdef YYERRORSYMBOL
108824 int yyerrorhit = 0; /* True if yymajor has invoked an error */
108825 #endif
108826 yyParser *yypParser; /* The parser */
108827
@@ -108840,11 +109183,13 @@
108840 yypParser->yyerrcnt = -1;
108841 yypParser->yystack[0].stateno = 0;
108842 yypParser->yystack[0].major = 0;
108843 }
108844 yyminorunion.yy0 = yyminor;
 
108845 yyendofinput = (yymajor==0);
 
108846 sqlite3ParserARG_STORE;
108847
108848 #ifndef NDEBUG
108849 if( yyTraceFILE ){
108850 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
@@ -108852,11 +109197,10 @@
108852 #endif
108853
108854 do{
108855 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
108856 if( yyact<YYNSTATE ){
108857 assert( !yyendofinput ); /* Impossible to shift the $ token */
108858 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
108859 yypParser->yyerrcnt--;
108860 yymajor = YYNOCODE;
108861 }else if( yyact < YYNSTATE + YYNRULE ){
108862 yy_reduce(yypParser,yyact-YYNSTATE);
@@ -110244,11 +110588,11 @@
110244 **
110245 ** * Recursive calls to this routine from thread X return immediately
110246 ** without blocking.
110247 */
110248 SQLITE_API int sqlite3_initialize(void){
110249 sqlite3_mutex *pMaster; /* The main static mutex */
110250 int rc; /* Result code */
110251
110252 #ifdef SQLITE_OMIT_WSD
110253 rc = sqlite3_wsd_init(4096, 24);
110254 if( rc!=SQLITE_OK ){
@@ -110278,11 +110622,11 @@
110278 ** This operation is protected by the STATIC_MASTER mutex. Note that
110279 ** MutexAlloc() is called for a static mutex prior to initializing the
110280 ** malloc subsystem - this implies that the allocation of a static
110281 ** mutex must not require support from the malloc subsystem.
110282 */
110283 pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
110284 sqlite3_mutex_enter(pMaster);
110285 sqlite3GlobalConfig.isMutexInit = 1;
110286 if( !sqlite3GlobalConfig.isMallocInit ){
110287 rc = sqlite3MallocInit();
110288 }
@@ -111352,17 +111696,17 @@
111352 sqlite3 *db,
111353 const char *zName,
111354 int nArg
111355 ){
111356 int nName = sqlite3Strlen30(zName);
111357 int rc;
111358 sqlite3_mutex_enter(db->mutex);
111359 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
111360 sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111361 0, sqlite3InvalidFunction, 0, 0, 0);
111362 }
111363 rc = sqlite3ApiExit(db, SQLITE_OK);
111364 sqlite3_mutex_leave(db->mutex);
111365 return rc;
111366 }
111367
111368 #ifndef SQLITE_OMIT_TRACE
@@ -112420,10 +112764,11 @@
112420 if( db ){
112421 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
112422 sqlite3_mutex_leave(db->mutex);
112423 }
112424 rc = sqlite3_errcode(db);
 
112425 if( rc==SQLITE_NOMEM ){
112426 sqlite3_close(db);
112427 db = 0;
112428 }else if( rc!=SQLITE_OK ){
112429 db->magic = SQLITE_MAGIC_SICK;
@@ -114148,10 +114493,17 @@
114148 #else
114149 # define TESTONLY(X)
114150 #endif
114151
114152 #endif /* SQLITE_AMALGAMATION */
 
 
 
 
 
 
 
114153
114154 typedef struct Fts3Table Fts3Table;
114155 typedef struct Fts3Cursor Fts3Cursor;
114156 typedef struct Fts3Expr Fts3Expr;
114157 typedef struct Fts3Phrase Fts3Phrase;
@@ -114649,11 +115001,11 @@
114649 char **pp,
114650 char *pStart,
114651 sqlite3_int64 *pVal
114652 ){
114653 sqlite3_int64 iVal;
114654 char *p = *pp;
114655
114656 /* Pointer p now points at the first byte past the varint we are
114657 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
114658 ** clear on character p[-1]. */
114659 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
@@ -115050,11 +115402,11 @@
115050 ** the output value undefined. Otherwise SQLITE_OK is returned.
115051 **
115052 ** This function is used when parsing the "prefix=" FTS4 parameter.
115053 */
115054 static int fts3GobbleInt(const char **pp, int *pnOut){
115055 const char *p = *pp; /* Iterator pointer */
115056 int nInt = 0; /* Output value */
115057
115058 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
115059 nInt = nInt * 10 + (p[0] - '0');
115060 }
@@ -115549,11 +115901,11 @@
115549 if( rc==SQLITE_OK ){
115550 /* If no row was found and no error has occured, then the %_content
115551 ** table is missing a row that is present in the full-text index.
115552 ** The data structures are corrupt.
115553 */
115554 rc = SQLITE_CORRUPT_VTAB;
115555 }
115556 pCsr->isEof = 1;
115557 if( pContext ){
115558 sqlite3_result_error_code(pContext, rc);
115559 }
@@ -115609,11 +115961,11 @@
115609 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
115610 */
115611 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115612 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115613 if( zCsr>zEnd ){
115614 return SQLITE_CORRUPT_VTAB;
115615 }
115616
115617 while( zCsr<zEnd && (piFirst || piLast) ){
115618 int cmp; /* memcmp() result */
115619 int nSuffix; /* Size of term suffix */
@@ -115627,11 +115979,11 @@
115627 }
115628 isFirstTerm = 0;
115629 zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
115630
115631 if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
115632 rc = SQLITE_CORRUPT_VTAB;
115633 goto finish_scan;
115634 }
115635 if( nPrefix+nSuffix>nAlloc ){
115636 char *zNew;
115637 nAlloc = (nPrefix+nSuffix) * 2;
@@ -115640,10 +115992,11 @@
115640 rc = SQLITE_NOMEM;
115641 goto finish_scan;
115642 }
115643 zBuffer = zNew;
115644 }
 
115645 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
115646 nBuffer = nPrefix + nSuffix;
115647 zCsr += nSuffix;
115648
115649 /* Compare the term we are searching for with the term just loaded from
@@ -117076,11 +117429,11 @@
117076 ** moves *ppPoslist so that it instead points to the first byte of the
117077 ** same position list.
117078 */
117079 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
117080 char *p = &(*ppPoslist)[-2];
117081 char c;
117082
117083 while( p>pStart && (c=*p--)==0 );
117084 while( p>pStart && (*p & 0x80) | c ){
117085 c = *p--;
117086 }
@@ -118070,11 +118423,11 @@
118070 while( a<pEnd ){
118071 a += sqlite3Fts3GetVarint(a, &nByte);
118072 }
118073 if( nDoc==0 || nByte==0 ){
118074 sqlite3_reset(pStmt);
118075 return SQLITE_CORRUPT_VTAB;
118076 }
118077
118078 pCsr->nDoc = nDoc;
118079 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
118080 assert( pCsr->nRowAvg>0 );
@@ -118546,12 +118899,15 @@
118546 }
118547
118548 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
118549 nToken = pExpr->pRight->pPhrase->nToken;
118550 for(p=pExpr->pLeft; p && res; p=p->pLeft){
118551 int nNear = p->pParent->nNear;
118552 Fts3Phrase *pPhrase = (
 
 
 
118553 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
118554 );
118555 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118556 }
118557 }
@@ -119037,10 +119393,19 @@
119037 pPhrase->aToken[i].pSegcsr = 0;
119038 }
119039 }
119040 }
119041
 
 
 
 
 
 
 
 
 
119042 #if !SQLITE_CORE
119043 /*
119044 ** Initialize API pointer table, if required.
119045 */
119046 SQLITE_API int sqlite3_extension_init(
@@ -119834,12 +120199,16 @@
119834 p->pPhrase = (Fts3Phrase *)&p[1];
119835 p->pPhrase->iColumn = pParse->iDefaultCol;
119836 p->pPhrase->nToken = nToken;
119837
119838 zBuf = (char *)&p->pPhrase->aToken[nToken];
119839 memcpy(zBuf, zTemp, nTemp);
119840 sqlite3_free(zTemp);
 
 
 
 
119841
119842 for(jj=0; jj<p->pPhrase->nToken; jj++){
119843 p->pPhrase->aToken[jj].z = zBuf;
119844 zBuf += p->pPhrase->aToken[jj].n;
119845 }
@@ -122594,11 +122963,11 @@
122594 sqlite3_bind_int64(pStmt, 1, iDocid);
122595 }
122596 rc = sqlite3_step(pStmt);
122597 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
122598 rc = sqlite3_reset(pStmt);
122599 if( rc==SQLITE_OK ) rc = SQLITE_CORRUPT_VTAB;
122600 pStmt = 0;
122601 }else{
122602 rc = SQLITE_OK;
122603 }
122604 }
@@ -123398,11 +123767,11 @@
123398 pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
123399 pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
123400 if( nPrefix<0 || nSuffix<=0
123401 || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
123402 ){
123403 return SQLITE_CORRUPT_VTAB;
123404 }
123405
123406 if( nPrefix+nSuffix>pReader->nTermAlloc ){
123407 int nNew = (nPrefix+nSuffix)*2;
123408 char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
@@ -123428,11 +123797,11 @@
123428 ** of these statements is untrue, then the data structure is corrupt.
123429 */
123430 if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
123431 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
123432 ){
123433 return SQLITE_CORRUPT_VTAB;
123434 }
123435 return SQLITE_OK;
123436 }
123437
123438 /*
@@ -125382,11 +125751,10 @@
125382 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
125383 ){
125384 Fts3Table *p = (Fts3Table *)pVtab;
125385 int rc = SQLITE_OK; /* Return Code */
125386 int isRemove = 0; /* True for an UPDATE or DELETE */
125387 sqlite3_int64 iRemove = 0; /* Rowid removed by UPDATE or DELETE */
125388 u32 *aSzIns = 0; /* Sizes of inserted documents */
125389 u32 *aSzDel; /* Sizes of deleted documents */
125390 int nChng = 0; /* Net change in number of documents */
125391 int bInsertDone = 0;
125392
@@ -125465,23 +125833,23 @@
125465 /* If this is a DELETE or UPDATE operation, remove the old record. */
125466 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
125467 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
125468 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
125469 isRemove = 1;
125470 iRemove = sqlite3_value_int64(apVal[0]);
125471 }
125472
125473 /* If this is an INSERT or UPDATE operation, insert the new record. */
125474 if( nArg>1 && rc==SQLITE_OK ){
125475 if( bInsertDone==0 ){
125476 rc = fts3InsertData(p, apVal, pRowid);
125477 if( rc==SQLITE_CONSTRAINT ) rc = SQLITE_CORRUPT_VTAB;
125478 }
125479 if( rc==SQLITE_OK && (!isRemove || *pRowid!=iRemove) ){
125480 rc = fts3PendingTermsDocid(p, *pRowid);
125481 }
125482 if( rc==SQLITE_OK ){
 
125483 rc = fts3InsertTerms(p, apVal, aSzIns);
125484 }
125485 if( p->bHasDocsize ){
125486 fts3InsertDocsize(&rc, p, aSzIns);
125487 }
@@ -126371,11 +126739,11 @@
126371 pStmt = *ppStmt;
126372 assert( sqlite3_data_count(pStmt)==1 );
126373
126374 a = sqlite3_column_blob(pStmt, 0);
126375 a += sqlite3Fts3GetVarint(a, &nDoc);
126376 if( nDoc==0 ) return SQLITE_CORRUPT_VTAB;
126377 *pnDoc = (u32)nDoc;
126378
126379 if( paLen ) *paLen = a;
126380 return SQLITE_OK;
126381 }
@@ -126950,11 +127318,11 @@
126950 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
126951 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
126952 );
126953 rc = fts3StringAppend(&res, aBuffer, -1);
126954 }else if( rc==SQLITE_DONE ){
126955 rc = SQLITE_CORRUPT_VTAB;
126956 }
126957 }
126958 }
126959 if( rc==SQLITE_DONE ){
126960 rc = SQLITE_OK;
@@ -128291,11 +128659,12 @@
128291 pCsr->nConstraint = argc;
128292 if( !pCsr->aConstraint ){
128293 rc = SQLITE_NOMEM;
128294 }else{
128295 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
128296 assert( (idxStr==0 && argc==0) || (int)strlen(idxStr)==argc*2 );
 
128297 for(ii=0; ii<argc; ii++){
128298 RtreeConstraint *p = &pCsr->aConstraint[ii];
128299 p->op = idxStr[ii*2];
128300 p->iCoord = idxStr[ii*2+1]-'a';
128301 if( p->op==RTREE_MATCH ){
@@ -128592,11 +128961,14 @@
128592 int iCell;
128593 sqlite3_int64 iBest = 0;
128594
128595 float fMinGrowth = 0.0;
128596 float fMinArea = 0.0;
 
128597 float fMinOverlap = 0.0;
 
 
128598
128599 int nCell = NCELL(pNode);
128600 RtreeCell cell;
128601 RtreeNode *pChild;
128602
@@ -128624,33 +128996,34 @@
128624 */
128625 for(iCell=0; iCell<nCell; iCell++){
128626 int bBest = 0;
128627 float growth;
128628 float area;
128629 float overlap = 0.0;
128630 nodeGetCell(pRtree, pNode, iCell, &cell);
128631 growth = cellGrowth(pRtree, &cell, pCell);
128632 area = cellArea(pRtree, &cell);
128633
128634 #if VARIANT_RSTARTREE_CHOOSESUBTREE
128635 if( ii==(pRtree->iDepth-1) ){
128636 overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
 
 
128637 }
128638 if( (iCell==0)
128639 || (overlap<fMinOverlap)
128640 || (overlap==fMinOverlap && growth<fMinGrowth)
128641 || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
128642 ){
128643 bBest = 1;
 
128644 }
128645 #else
128646 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
128647 bBest = 1;
128648 }
128649 #endif
128650 if( bBest ){
128651 fMinOverlap = overlap;
128652 fMinGrowth = growth;
128653 fMinArea = area;
128654 iBest = cell.iRowid;
128655 }
128656 }
128657
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -1,8 +1,8 @@
1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.7.9. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -394,11 +394,11 @@
394 ** assert() macro is enabled, each call into the Win32 native heap subsystem
395 ** will cause HeapValidate to be called. If heap validation should fail, an
396 ** assertion will be triggered.
397 **
398 ** (Historical note: There used to be several other options, but we've
399 ** pared it down to just these three.)
400 **
401 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
402 ** the default.
403 */
404 #if defined(SQLITE_SYSTEM_MALLOC)+defined(SQLITE_WIN32_MALLOC)+defined(SQLITE_MEMDEBUG)>1
@@ -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.7.9"
660 #define SQLITE_VERSION_NUMBER 3007009
661 #define SQLITE_SOURCE_ID "2011-10-15 00:16:30 39408702a989f907261c298bf0947f3e68bd10fe"
662
663 /*
664 ** CAPI3REF: Run-Time Library Version Numbers
665 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
666 **
@@ -1318,11 +1318,15 @@
1318 ** in order for the database to be readable. The fourth parameter to
1319 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
1320 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
1321 ** WAL mode. If the integer is -1, then it is overwritten with the current
1322 ** WAL persistence setting.
1323 **
1324 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
1325 ** a write transaction to indicate that, unless it is rolled back for some
1326 ** reason, the entire database file will be overwritten by the current
1327 ** transaction. This is used by VACUUM operations.
1328 */
1329 #define SQLITE_FCNTL_LOCKSTATE 1
1330 #define SQLITE_GET_LOCKPROXYFILE 2
1331 #define SQLITE_SET_LOCKPROXYFILE 3
1332 #define SQLITE_LAST_ERRNO 4
@@ -1330,10 +1334,11 @@
1334 #define SQLITE_FCNTL_CHUNK_SIZE 6
1335 #define SQLITE_FCNTL_FILE_POINTER 7
1336 #define SQLITE_FCNTL_SYNC_OMITTED 8
1337 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
1338 #define SQLITE_FCNTL_PERSIST_WAL 10
1339 #define SQLITE_FCNTL_OVERWRITE 11
1340
1341 /*
1342 ** CAPI3REF: Mutex Handle
1343 **
1344 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -3346,11 +3351,12 @@
3351 ** zSql string ends at either the first '\000' or '\u0000' character or
3352 ** the nByte-th byte, whichever comes first. If the caller knows
3353 ** that the supplied string is nul-terminated, then there is a small
3354 ** performance advantage to be gained by passing an nByte parameter that
3355 ** is equal to the number of bytes in the input string <i>including</i>
3356 ** the nul-terminator bytes as this saves SQLite from having to
3357 ** make a copy of the input string.
3358 **
3359 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3360 ** past the end of the first SQL statement in zSql. These routines only
3361 ** compile the first statement in zSql, so *pzTail is left pointing to
3362 ** what remains uncompiled.
@@ -3397,11 +3403,11 @@
3403 ** a schema change, on the first [sqlite3_step()] call following any change
3404 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3405 ** ^The specific value of WHERE-clause [parameter] might influence the
3406 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3407 ** or [GLOB] operator or if the parameter is compared to an indexed column
3408 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3409 ** the
3410 ** </li>
3411 ** </ol>
3412 */
3413 SQLITE_API int sqlite3_prepare(
@@ -3567,10 +3573,17 @@
3573 ** ^(In those routines that have a fourth argument, its value is the
3574 ** number of bytes in the parameter. To be clear: the value is the
3575 ** number of <u>bytes</u> in the value, not the number of characters.)^
3576 ** ^If the fourth parameter is negative, the length of the string is
3577 ** the number of bytes up to the first zero terminator.
3578 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3579 ** or sqlite3_bind_text16() then that parameter must be the byte offset
3580 ** where the NUL terminator would occur assuming the string were NUL
3581 ** terminated. If any NUL characters occur at byte offsets less than
3582 ** the value of the fourth parameter then the resulting string value will
3583 ** contain embedded NULs. The result of expressions involving strings
3584 ** with embedded NULs is undefined.
3585 **
3586 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3587 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3588 ** string after SQLite has finished with it. ^The destructor is called
3589 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3900,10 +3913,16 @@
3913 ** current row of the result set of [prepared statement] P.
3914 ** ^If prepared statement P does not have results ready to return
3915 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3916 ** interfaces) then sqlite3_data_count(P) returns 0.
3917 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3918 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3919 ** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
3920 ** will return non-zero if previous call to [sqlite3_step](P) returned
3921 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3922 ** where it always returns zero since each step of that multi-step
3923 ** pragma returns 0 columns of data.
3924 **
3925 ** See also: [sqlite3_column_count()]
3926 */
3927 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3928
@@ -4579,11 +4598,16 @@
4598 ** is negative, then SQLite takes result text from the 2nd parameter
4599 ** through the first zero character.
4600 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4601 ** is non-negative, then as many bytes (not characters) of the text
4602 ** pointed to by the 2nd parameter are taken as the application-defined
4603 ** function result. If the 3rd parameter is non-negative, then it
4604 ** must be the byte offset into the string where the NUL terminator would
4605 ** appear if the string where NUL terminated. If any NUL characters occur
4606 ** in the string at a byte offset that is less than the value of the 3rd
4607 ** parameter, then the resulting string will contain embedded NULs and the
4608 ** result of expressions operating on strings with embedded NULs is undefined.
4609 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4610 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4611 ** function as the destructor on the text or BLOB result when it has
4612 ** finished using that result.
4613 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -6362,20 +6386,34 @@
6386 ** <dd>This parameter returns the approximate number of of bytes of heap
6387 ** and lookaside memory used by all prepared statements associated with
6388 ** the database connection.)^
6389 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6390 ** </dd>
6391 **
6392 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
6393 ** <dd>This parameter returns the number of pager cache hits that have
6394 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
6395 ** is always 0.
6396 ** </dd>
6397 **
6398 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
6399 ** <dd>This parameter returns the number of pager cache misses that have
6400 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
6401 ** is always 0.
6402 ** </dd>
6403 ** </dl>
6404 */
6405 #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
6406 #define SQLITE_DBSTATUS_CACHE_USED 1
6407 #define SQLITE_DBSTATUS_SCHEMA_USED 2
6408 #define SQLITE_DBSTATUS_STMT_USED 3
6409 #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
6410 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
6411 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
6412 #define SQLITE_DBSTATUS_CACHE_HIT 7
6413 #define SQLITE_DBSTATUS_CACHE_MISS 8
6414 #define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
6415
6416
6417 /*
6418 ** CAPI3REF: Prepared Statement Status
6419 **
@@ -6425,11 +6463,10 @@
6463 ** <dd>^This is the number of rows inserted into transient indices that
6464 ** were created automatically in order to help joins run faster.
6465 ** A non-zero value in this counter may indicate an opportunity to
6466 ** improvement performance by adding permanent indices that do not
6467 ** need to be reinitialized each time the statement is run.</dd>
 
6468 ** </dl>
6469 */
6470 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
6471 #define SQLITE_STMTSTATUS_SORT 2
6472 #define SQLITE_STMTSTATUS_AUTOINDEX 3
@@ -7711,10 +7748,22 @@
7748 ** is 0x00000000ffffffff. But because of quirks of some compilers, we
7749 ** have to specify the value in the less intuitive manner shown:
7750 */
7751 #define SQLITE_MAX_U32 ((((u64)1)<<32)-1)
7752
7753 /*
7754 ** The datatype used to store estimates of the number of rows in a
7755 ** table or index. This is an unsigned integer type. For 99.9% of
7756 ** the world, a 32-bit integer is sufficient. But a 64-bit integer
7757 ** can be used at compile-time if desired.
7758 */
7759 #ifdef SQLITE_64BIT_STATS
7760 typedef u64 tRowcnt; /* 64-bit only if requested at compile-time */
7761 #else
7762 typedef u32 tRowcnt; /* 32-bit is the default */
7763 #endif
7764
7765 /*
7766 ** Macros to determine whether the machine is big or little endian,
7767 ** evaluated at runtime.
7768 */
7769 #ifdef SQLITE_AMALGAMATION
@@ -8742,10 +8791,11 @@
8791 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
8792 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
8793 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
8794 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
8795 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
8796 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
8797
8798 /* Functions used to truncate the database file. */
8799 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
8800
8801 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
@@ -9278,18 +9328,21 @@
9328 /*
9329 ** If this is a no-op implementation, implement everything as macros.
9330 */
9331 #define sqlite3_mutex_alloc(X) ((sqlite3_mutex*)8)
9332 #define sqlite3_mutex_free(X)
9333 #define sqlite3_mutex_enter(X)
9334 #define sqlite3_mutex_try(X) SQLITE_OK
9335 #define sqlite3_mutex_leave(X)
9336 #define sqlite3_mutex_held(X) ((void)(X),1)
9337 #define sqlite3_mutex_notheld(X) ((void)(X),1)
9338 #define sqlite3MutexAlloc(X) ((sqlite3_mutex*)8)
9339 #define sqlite3MutexInit() SQLITE_OK
9340 #define sqlite3MutexEnd()
9341 #define MUTEX_LOGIC(X)
9342 #else
9343 #define MUTEX_LOGIC(X) X
9344 #endif /* defined(SQLITE_MUTEX_OMIT) */
9345
9346 /************** End of mutex.h ***********************************************/
9347 /************** Continuing where we left off in sqliteInt.h ******************/
9348
@@ -9918,11 +9971,11 @@
9971 int iPKey; /* If not negative, use aCol[iPKey] as the primary key */
9972 int nCol; /* Number of columns in this table */
9973 Column *aCol; /* Information about each column */
9974 Index *pIndex; /* List of SQL indexes on this table. */
9975 int tnum; /* Root BTree node for this table (see note above) */
9976 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
9977 Select *pSelect; /* NULL for tables. Points to definition if a view. */
9978 u16 nRef; /* Number of pointers to this Table */
9979 u8 tabFlags; /* Mask of TF_* values */
9980 u8 keyConf; /* What to do in case of uniqueness conflict on iPKey */
9981 FKey *pFKey; /* Linked list of all foreign keys in this table */
@@ -10117,11 +10170,11 @@
10170 */
10171 struct Index {
10172 char *zName; /* Name of this index */
10173 int nColumn; /* Number of columns in the table used by this index */
10174 int *aiColumn; /* Which columns are used by this index. 1st is 0 */
10175 tRowcnt *aiRowEst; /* Result of ANALYZE: Est. rows selected by each column */
10176 Table *pTable; /* The SQL table being indexed */
10177 int tnum; /* Page containing root of this index in database file */
10178 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10179 u8 autoIndex; /* True if is automatically created (ex: by UNIQUE) */
10180 u8 bUnordered; /* Use this index for == or IN queries only */
@@ -10128,24 +10181,32 @@
10181 char *zColAff; /* String defining the affinity of each column */
10182 Index *pNext; /* The next index associated with the same table */
10183 Schema *pSchema; /* Schema containing this index */
10184 u8 *aSortOrder; /* Array of size Index.nColumn. True==DESC, False==ASC */
10185 char **azColl; /* Array of collation sequence names for index */
10186 #ifdef SQLITE_ENABLE_STAT3
10187 int nSample; /* Number of elements in aSample[] */
10188 tRowcnt avgEq; /* Average nEq value for key values not in aSample */
10189 IndexSample *aSample; /* Samples of the left-most key */
10190 #endif
10191 };
10192
10193 /*
10194 ** Each sample stored in the sqlite_stat2 table is represented in memory
10195 ** using a structure of this type.
10196 */
10197 struct IndexSample {
10198 union {
10199 char *z; /* Value if eType is SQLITE_TEXT or SQLITE_BLOB */
10200 double r; /* Value if eType is SQLITE_FLOAT */
10201 i64 i; /* Value if eType is SQLITE_INTEGER */
10202 } u;
10203 u8 eType; /* SQLITE_NULL, SQLITE_INTEGER ... etc. */
10204 int nByte; /* Size in byte of text or blob. */
10205 tRowcnt nEq; /* Est. number of rows where the key equals this sample */
10206 tRowcnt nLt; /* Est. number of rows where key is less than this sample */
10207 tRowcnt nDLt; /* Est. number of distinct keys less than this sample */
10208 };
10209
10210 /*
10211 ** Each token coming out of the lexer is an instance of
10212 ** this structure. Tokens are also used as part of an expression.
@@ -10593,14 +10654,14 @@
10654 #define WHERE_ORDERBY_NORMAL 0x0000 /* No-op */
10655 #define WHERE_ORDERBY_MIN 0x0001 /* ORDER BY processing for min() func */
10656 #define WHERE_ORDERBY_MAX 0x0002 /* ORDER BY processing for max() func */
10657 #define WHERE_ONEPASS_DESIRED 0x0004 /* Want to do one-pass UPDATE/DELETE */
10658 #define WHERE_DUPLICATES_OK 0x0008 /* Ok to return a row more than once */
10659 #define WHERE_OMIT_OPEN_CLOSE 0x0010 /* Table cursors are already open */
10660 #define WHERE_FORCE_TABLE 0x0020 /* Do not use an index-only search */
10661 #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
10662 #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
10663
10664 /*
10665 ** The WHERE clause processing routine has two halves. The
10666 ** first part does the start of the WHERE loop and the second
10667 ** half does the tail of the WHERE loop. An instance of
@@ -11350,10 +11411,11 @@
11411 #else
11412 # define sqlite3ViewGetColumnNames(A,B) 0
11413 #endif
11414
11415 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
11416 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
11417 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
11418 #ifndef SQLITE_OMIT_AUTOINCREMENT
11419 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
11420 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse);
11421 #else
@@ -11606,11 +11668,11 @@
11668 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
11669 void(*)(void*));
11670 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
11671 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
11672 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
11673 #ifdef SQLITE_ENABLE_STAT3
11674 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *, u8, char *, int, int *);
11675 #endif
11676 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
11677 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
11678 #ifndef SQLITE_AMALGAMATION
@@ -11708,19 +11770,21 @@
11770 # define sqlite3VtabInSync(db) 0
11771 # define sqlite3VtabLock(X)
11772 # define sqlite3VtabUnlock(X)
11773 # define sqlite3VtabUnlockList(X)
11774 # define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
11775 # define sqlite3GetVTable(X,Y) ((VTable*)0)
11776 #else
11777 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table*);
11778 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, char **);
11779 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db);
11780 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db);
11781 SQLITE_PRIVATE void sqlite3VtabLock(VTable *);
11782 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *);
11783 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3*);
11784 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *, int, int);
11785 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3*, Table*);
11786 # define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
11787 #endif
11788 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
11789 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*);
11790 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
@@ -11736,11 +11800,10 @@
11800 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
11801 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
11802 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
11803 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
11804 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
 
11805 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
11806 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
11807 SQLITE_PRIVATE int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
11808
11809 /* Declarations for functions in fkey.c. All of these are replaced by
@@ -12232,10 +12295,13 @@
12295 #ifdef SQLITE_ENABLE_RTREE
12296 "ENABLE_RTREE",
12297 #endif
12298 #ifdef SQLITE_ENABLE_STAT2
12299 "ENABLE_STAT2",
12300 #endif
12301 #ifdef SQLITE_ENABLE_STAT3
12302 "ENABLE_STAT3",
12303 #endif
12304 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12305 "ENABLE_UNLOCK_NOTIFY",
12306 #endif
12307 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
@@ -12445,13 +12511,10 @@
12511 "OMIT_WSD",
12512 #endif
12513 #ifdef SQLITE_OMIT_XFER_OPT
12514 "OMIT_XFER_OPT",
12515 #endif
 
 
 
12516 #ifdef SQLITE_PERFORMANCE_TRACE
12517 "PERFORMANCE_TRACE",
12518 #endif
12519 #ifdef SQLITE_PROXY_DEBUG
12520 "PROXY_DEBUG",
@@ -13189,10 +13252,32 @@
13252 *pHighwater = 0;
13253 *pCurrent = nByte;
13254
13255 break;
13256 }
13257
13258 /*
13259 ** Set *pCurrent to the total cache hits or misses encountered by all
13260 ** pagers the database handle is connected to. *pHighwater is always set
13261 ** to zero.
13262 */
13263 case SQLITE_DBSTATUS_CACHE_HIT:
13264 case SQLITE_DBSTATUS_CACHE_MISS: {
13265 int i;
13266 int nRet = 0;
13267 assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
13268
13269 for(i=0; i<db->nDb; i++){
13270 if( db->aDb[i].pBt ){
13271 Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
13272 sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
13273 }
13274 }
13275 *pHighwater = 0;
13276 *pCurrent = nRet;
13277 break;
13278 }
13279
13280 default: {
13281 rc = SQLITE_ERROR;
13282 }
13283 }
@@ -13490,16 +13575,22 @@
13575 }
13576 return 0;
13577 }
13578
13579 /*
13580 ** Set the time to the current time reported by the VFS.
13581 **
13582 ** Return the number of errors.
13583 */
13584 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
13585 sqlite3 *db = sqlite3_context_db_handle(context);
13586 if( sqlite3OsCurrentTimeInt64(db->pVfs, &p->iJD)==SQLITE_OK ){
13587 p->validJD = 1;
13588 return 0;
13589 }else{
13590 return 1;
13591 }
13592 }
13593
13594 /*
13595 ** Attempt to parse the given string into a Julian Day Number. Return
13596 ** the number of errors.
@@ -13525,12 +13616,11 @@
13616 if( parseYyyyMmDd(zDate,p)==0 ){
13617 return 0;
13618 }else if( parseHhMmSs(zDate, p)==0 ){
13619 return 0;
13620 }else if( sqlite3StrICmp(zDate,"now")==0){
13621 return setDateTimeToCurrent(context, p);
 
13622 }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
13623 p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
13624 p->validJD = 1;
13625 return 0;
13626 }
@@ -13953,12 +14043,13 @@
14043 int i;
14044 const unsigned char *z;
14045 int eType;
14046 memset(p, 0, sizeof(*p));
14047 if( argc==0 ){
14048 return setDateTimeToCurrent(context, p);
14049 }
14050 if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
14051 || eType==SQLITE_INTEGER ){
14052 p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
14053 p->validJD = 1;
14054 }else{
14055 z = sqlite3_value_text(argv[0]);
@@ -14266,35 +14357,32 @@
14357 ){
14358 time_t t;
14359 char *zFormat = (char *)sqlite3_user_data(context);
14360 sqlite3 *db;
14361 sqlite3_int64 iT;
14362 struct tm *pTm;
14363 struct tm sNow;
14364 char zBuf[20];
14365
14366 UNUSED_PARAMETER(argc);
14367 UNUSED_PARAMETER(argv);
14368
14369 db = sqlite3_context_db_handle(context);
14370 if( sqlite3OsCurrentTimeInt64(db->pVfs, &iT) ) return;
14371 t = iT/1000 - 10000*(sqlite3_int64)21086676;
14372 #ifdef HAVE_GMTIME_R
14373 pTm = gmtime_r(&t, &sNow);
14374 #else
14375 sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14376 pTm = gmtime(&t);
14377 if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
14378 sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
14379 #endif
14380 if( pTm ){
14381 strftime(zBuf, 20, zFormat, &sNow);
14382 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
14383 }
 
 
 
 
 
 
 
 
 
 
14384 }
14385 #endif
14386
14387 /*
14388 ** This function registered all of the above C functions as SQL
@@ -14625,16 +14713,16 @@
14713 ** Register a VFS with the system. It is harmless to register the same
14714 ** VFS multiple times. The new VFS becomes the default if makeDflt is
14715 ** true.
14716 */
14717 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
14718 MUTEX_LOGIC(sqlite3_mutex *mutex;)
14719 #ifndef SQLITE_OMIT_AUTOINIT
14720 int rc = sqlite3_initialize();
14721 if( rc ) return rc;
14722 #endif
14723 MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
14724 sqlite3_mutex_enter(mutex);
14725 vfsUnlink(pVfs);
14726 if( makeDflt || vfsList==0 ){
14727 pVfs->pNext = vfsList;
14728 vfsList = pVfs;
@@ -18878,52 +18966,14 @@
18966 ** an historical reference. Most of the "enhancements" have been backed
18967 ** out so that the functionality is now the same as standard printf().
18968 **
18969 **************************************************************************
18970 **
18971 ** This file contains code for a set of "printf"-like routines. These
18972 ** routines format strings much like the printf() from the standard C
18973 ** library, though the implementation here has enhancements to support
18974 ** SQLlite.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18975 */
18976
18977 /*
18978 ** Conversion types fall into various categories as defined by the
18979 ** following enumeration.
@@ -19057,47 +19107,19 @@
19107 }
19108 }
19109
19110 /*
19111 ** On machines with a small stack size, you can redefine the
19112 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
19113 */
19114 #ifndef SQLITE_PRINT_BUF_SIZE
19115 # define SQLITE_PRINT_BUF_SIZE 70
 
 
 
 
19116 #endif
19117 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE /* Size of the output buffer */
19118
19119 /*
19120 ** Render a string given by "fmt" into the StrAccum object.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19121 */
19122 SQLITE_PRIVATE void sqlite3VXPrintf(
19123 StrAccum *pAccum, /* Accumulate results here */
19124 int useExtended, /* Allow extended %-conversions */
19125 const char *fmt, /* Format string */
@@ -19116,27 +19138,27 @@
19138 etByte flag_altform2; /* True if "!" flag is present */
19139 etByte flag_zeropad; /* True if field width constant starts with zero */
19140 etByte flag_long; /* True if "l" flag is present */
19141 etByte flag_longlong; /* True if the "ll" flag is present */
19142 etByte done; /* Loop termination flag */
19143 etByte xtype = 0; /* Conversion paradigm */
19144 char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
19145 sqlite_uint64 longvalue; /* Value for integer types */
19146 LONGDOUBLE_TYPE realvalue; /* Value for real types */
19147 const et_info *infop; /* Pointer to the appropriate info structure */
19148 char *zOut; /* Rendering buffer */
19149 int nOut; /* Size of the rendering buffer */
19150 char *zExtra; /* Malloced memory used by some conversion */
 
19151 #ifndef SQLITE_OMIT_FLOATING_POINT
19152 int exp, e2; /* exponent of real numbers */
19153 int nsd; /* Number of significant digits returned */
19154 double rounder; /* Used for rounding floating point values */
19155 etByte flag_dp; /* True if decimal point should be shown */
19156 etByte flag_rtz; /* True if trailing zeros should be removed */
 
 
19157 #endif
19158 char buf[etBUFSIZE]; /* Conversion buffer */
19159
 
19160 bufpt = 0;
19161 for(; (c=(*fmt))!=0; ++fmt){
19162 if( c!='%' ){
19163 int amt;
19164 bufpt = (char *)fmt;
@@ -19177,13 +19199,10 @@
19199 while( c>='0' && c<='9' ){
19200 width = width*10 + c - '0';
19201 c = *++fmt;
19202 }
19203 }
 
 
 
19204 /* Get the precision */
19205 if( c=='.' ){
19206 precision = 0;
19207 c = *++fmt;
19208 if( c=='*' ){
@@ -19226,16 +19245,10 @@
19245 break;
19246 }
19247 }
19248 zExtra = 0;
19249
 
 
 
 
 
 
19250 /*
19251 ** At this point, variables are initialized as follows:
19252 **
19253 ** flag_alternateform TRUE if a '#' is present.
19254 ** flag_altform2 TRUE if a '!' is present.
@@ -19296,20 +19309,30 @@
19309 }
19310 if( longvalue==0 ) flag_alternateform = 0;
19311 if( flag_zeropad && precision<width-(prefix!=0) ){
19312 precision = width-(prefix!=0);
19313 }
19314 if( precision<etBUFSIZE-10 ){
19315 nOut = etBUFSIZE;
19316 zOut = buf;
19317 }else{
19318 nOut = precision + 10;
19319 zOut = zExtra = sqlite3Malloc( nOut );
19320 if( zOut==0 ){
19321 pAccum->mallocFailed = 1;
19322 return;
19323 }
19324 }
19325 bufpt = &zOut[nOut-1];
19326 if( xtype==etORDINAL ){
19327 static const char zOrd[] = "thstndrd";
19328 int x = (int)(longvalue % 10);
19329 if( x>=4 || (longvalue/10)%10==1 ){
19330 x = 0;
19331 }
19332 *(--bufpt) = zOrd[x*2+1];
19333 *(--bufpt) = zOrd[x*2];
 
19334 }
19335 {
19336 register const char *cset; /* Use registers for speed */
19337 register int base;
19338 cset = &aDigits[infop->charset];
@@ -19317,11 +19340,11 @@
19340 do{ /* Convert to ascii */
19341 *(--bufpt) = cset[longvalue%base];
19342 longvalue = longvalue/base;
19343 }while( longvalue>0 );
19344 }
19345 length = (int)(&zOut[nOut-1]-bufpt);
19346 for(idx=precision-length; idx>0; idx--){
19347 *(--bufpt) = '0'; /* Zero pad */
19348 }
19349 if( prefix ) *(--bufpt) = prefix; /* Add sign */
19350 if( flag_alternateform && infop->prefix ){ /* Add "0" or "0x" */
@@ -19328,21 +19351,20 @@
19351 const char *pre;
19352 char x;
19353 pre = &aPrefix[infop->prefix];
19354 for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
19355 }
19356 length = (int)(&zOut[nOut-1]-bufpt);
19357 break;
19358 case etFLOAT:
19359 case etEXP:
19360 case etGENERIC:
19361 realvalue = va_arg(ap,double);
19362 #ifdef SQLITE_OMIT_FLOATING_POINT
19363 length = 0;
19364 #else
19365 if( precision<0 ) precision = 6; /* Set default precision */
 
19366 if( realvalue<0.0 ){
19367 realvalue = -realvalue;
19368 prefix = '-';
19369 }else{
19370 if( flag_plussign ) prefix = '+';
@@ -19386,11 +19408,10 @@
19408 bufpt = buf;
19409 /*
19410 ** If the field type is etGENERIC, then convert to either etEXP
19411 ** or etFLOAT, as appropriate.
19412 */
 
19413 if( xtype!=etFLOAT ){
19414 realvalue += rounder;
19415 if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
19416 }
19417 if( xtype==etGENERIC ){
@@ -19407,10 +19428,18 @@
19428 if( xtype==etEXP ){
19429 e2 = 0;
19430 }else{
19431 e2 = exp;
19432 }
19433 if( e2+precision+width > etBUFSIZE - 15 ){
19434 bufpt = zExtra = sqlite3Malloc( e2+precision+width+15 );
19435 if( bufpt==0 ){
19436 pAccum->mallocFailed = 1;
19437 return;
19438 }
19439 }
19440 zOut = bufpt;
19441 nsd = 0;
19442 flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
19443 /* The sign in front of the number */
19444 if( prefix ){
19445 *(bufpt++) = prefix;
@@ -19438,21 +19467,21 @@
19467 *(bufpt++) = et_getdigit(&realvalue,&nsd);
19468 }
19469 /* Remove trailing zeros and the "." if no digits follow the "." */
19470 if( flag_rtz && flag_dp ){
19471 while( bufpt[-1]=='0' ) *(--bufpt) = 0;
19472 assert( bufpt>zOut );
19473 if( bufpt[-1]=='.' ){
19474 if( flag_altform2 ){
19475 *(bufpt++) = '0';
19476 }else{
19477 *(--bufpt) = 0;
19478 }
19479 }
19480 }
19481 /* Add the "eNNN" suffix */
19482 if( xtype==etEXP ){
19483 *(bufpt++) = aDigits[infop->charset];
19484 if( exp<0 ){
19485 *(bufpt++) = '-'; exp = -exp;
19486 }else{
19487 *(bufpt++) = '+';
@@ -19467,12 +19496,12 @@
19496 *bufpt = 0;
19497
19498 /* The converted number is in buf[] and zero terminated. Output it.
19499 ** Note that the number is in the usual order, not reversed as with
19500 ** integer conversions. */
19501 length = (int)(bufpt-zOut);
19502 bufpt = zOut;
19503
19504 /* Special case: Add leading zeros if the flag_zeropad flag is
19505 ** set and we are not left justified */
19506 if( flag_zeropad && !flag_leftjustify && length < width){
19507 int i;
@@ -19606,13 +19635,11 @@
19635 nspace = width-length;
19636 if( nspace>0 ){
19637 appendSpace(pAccum, nspace);
19638 }
19639 }
19640 sqlite3_free(zExtra);
 
 
19641 }/* End for loop over the format string */
19642 } /* End of function */
19643
19644 /*
19645 ** Append N bytes of text from z to the StrAccum object.
@@ -19622,10 +19649,11 @@
19649 if( p->tooBig | p->mallocFailed ){
19650 testcase(p->tooBig);
19651 testcase(p->mallocFailed);
19652 return;
19653 }
19654 assert( p->zText!=0 || p->nChar==0 );
19655 if( N<0 ){
19656 N = sqlite3Strlen30(z);
19657 }
19658 if( N==0 || NEVER(z==0) ){
19659 return;
@@ -19653,19 +19681,20 @@
19681 zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
19682 }else{
19683 zNew = sqlite3_realloc(zOld, p->nAlloc);
19684 }
19685 if( zNew ){
19686 if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
19687 p->zText = zNew;
19688 }else{
19689 p->mallocFailed = 1;
19690 sqlite3StrAccumReset(p);
19691 return;
19692 }
19693 }
19694 }
19695 assert( p->zText );
19696 memcpy(&p->zText[p->nChar], z, N);
19697 p->nChar += N;
19698 }
19699
19700 /*
@@ -20511,11 +20540,11 @@
20540 ** no longer required.
20541 **
20542 ** If a malloc failure occurs, NULL is returned and the db.mallocFailed
20543 ** flag set.
20544 */
20545 #ifdef SQLITE_ENABLE_STAT3
20546 SQLITE_PRIVATE char *sqlite3Utf8to16(sqlite3 *db, u8 enc, char *z, int n, int *pnOut){
20547 Mem m;
20548 memset(&m, 0, sizeof(m));
20549 m.db = db;
20550 sqlite3VdbeMemSetStr(&m, z, n, SQLITE_UTF8, SQLITE_STATIC);
@@ -25102,11 +25131,11 @@
25131 return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
25132 }
25133 #endif
25134
25135
25136 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
25137 /*
25138 ** Helper function for printing out trace information from debugging
25139 ** binaries. This returns the string represetation of the supplied
25140 ** integer lock-type.
25141 */
@@ -25937,18 +25966,18 @@
25966 ** locking a random byte from a range, concurrent SHARED locks may exist
25967 ** even if the locking primitive used is always a write-lock.
25968 */
25969 int rc = SQLITE_OK;
25970 unixFile *pFile = (unixFile*)id;
25971 unixInodeInfo *pInode;
25972 struct flock lock;
25973 int tErrno = 0;
25974
25975 assert( pFile );
25976 OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25977 azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25978 azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
25979
25980 /* If there is already a lock of this type or more restrictive on the
25981 ** unixFile, do nothing. Don't use the end_lock: exit path, as
25982 ** unixEnterMutex() hasn't been called yet.
25983 */
@@ -26148,11 +26177,10 @@
26177 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
26178 unixFile *pFile = (unixFile*)id;
26179 unixInodeInfo *pInode;
26180 struct flock lock;
26181 int rc = SQLITE_OK;
 
26182
26183 assert( pFile );
26184 OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
26185 pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26186 getpid()));
@@ -26160,18 +26188,14 @@
26188 assert( eFileLock<=SHARED_LOCK );
26189 if( pFile->eFileLock<=eFileLock ){
26190 return SQLITE_OK;
26191 }
26192 unixEnterMutex();
 
26193 pInode = pFile->pInode;
26194 assert( pInode->nShared!=0 );
26195 if( pFile->eFileLock>SHARED_LOCK ){
26196 assert( pInode->eFileLock==pFile->eFileLock );
 
 
 
26197
26198 #ifndef NDEBUG
26199 /* When reducing a lock such that other processes can start
26200 ** reading the database file again, make sure that the
26201 ** transaction counter was updated if any part of the database
@@ -26178,15 +26202,10 @@
26202 ** file changed. If the transaction counter is not updated,
26203 ** other connections to the same file might not realize that
26204 ** the file has changed and hence might not know to flush their
26205 ** cache. The use of a stale cache can lead to database corruption.
26206 */
 
 
 
 
 
26207 pFile->inNormalWrite = 0;
26208 #endif
26209
26210 /* downgrading to a shared lock on NFS involves clearing the write lock
26211 ** before establishing the readlock - to avoid a race condition we downgrade
@@ -26284,13 +26303,10 @@
26303 pInode->nShared--;
26304 if( pInode->nShared==0 ){
26305 lock.l_type = F_UNLCK;
26306 lock.l_whence = SEEK_SET;
26307 lock.l_start = lock.l_len = 0L;
 
 
 
26308 if( unixFileLock(pFile, &lock)==0 ){
26309 pInode->eFileLock = NO_LOCK;
26310 }else{
26311 rc = SQLITE_IOERR_UNLOCK;
26312 pFile->lastErrno = errno;
@@ -28428,20 +28444,19 @@
28444 rc = SQLITE_NOMEM;
28445 goto shm_open_err;
28446 }
28447
28448 if( pInode->bProcessLock==0 ){
28449 const char *zRO;
28450 int openFlags = O_RDWR | O_CREAT;
28451 zRO = sqlite3_uri_parameter(pDbFd->zPath, "readonly_shm");
28452 if( zRO && sqlite3GetBoolean(zRO) ){
28453 openFlags = O_RDONLY;
28454 pShmNode->isReadonly = 1;
28455 }
28456 pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
28457 if( pShmNode->h<0 ){
 
 
 
 
 
 
 
28458 if( pShmNode->h<0 ){
28459 rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
28460 goto shm_open_err;
28461 }
28462 }
@@ -29122,10 +29137,13 @@
29137 assert( zFilename==0 || zFilename[0]=='/'
29138 || pVfs->pAppData==(void*)&autolockIoFinder );
29139 #else
29140 assert( zFilename==0 || zFilename[0]=='/' );
29141 #endif
29142
29143 /* No locking occurs in temporary files */
29144 assert( zFilename!=0 || noLock );
29145
29146 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
29147 pNew->h = h;
29148 pNew->zPath = zFilename;
29149 if( memcmp(pVfs->zName,"unix-excl",10)==0 ){
@@ -29224,10 +29242,11 @@
29242 /* Dotfile locking uses the file path so it needs to be included in
29243 ** the dotlockLockingContext
29244 */
29245 char *zLockFile;
29246 int nFilename;
29247 assert( zFilename!=0 );
29248 nFilename = (int)strlen(zFilename) + 6;
29249 zLockFile = (char *)sqlite3_malloc(nFilename);
29250 if( zLockFile==0 ){
29251 rc = SQLITE_NOMEM;
29252 }else{
@@ -29462,12 +29481,20 @@
29481 **
29482 ** where NN is a 4 digit decimal number. The NN naming schemes are
29483 ** used by the test_multiplex.c module.
29484 */
29485 nDb = sqlite3Strlen30(zPath) - 1;
29486 #ifdef SQLITE_ENABLE_8_3_NAMES
29487 while( nDb>0 && zPath[nDb]!='-' && zPath[nDb]!='/' ) nDb--;
29488 if( nDb==0 || zPath[nDb]=='/' ) return SQLITE_OK;
29489 #else
29490 while( zPath[nDb]!='-' ){
29491 assert( nDb>0 );
29492 assert( zPath[nDb]!='\n' );
29493 nDb--;
29494 }
29495 #endif
29496 memcpy(zDb, zPath, nDb);
29497 zDb[nDb] = '\0';
29498
29499 if( 0==osStat(zDb, &sStat) ){
29500 *pMode = sStat.st_mode & 0777;
@@ -29995,14 +30022,16 @@
30022 ** the current time and date as a Julian Day number times 86_400_000. In
30023 ** other words, write into *piNow the number of milliseconds since the Julian
30024 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
30025 ** proleptic Gregorian calendar.
30026 **
30027 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
30028 ** cannot be found.
30029 */
30030 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
30031 static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
30032 int rc = SQLITE_OK;
30033 #if defined(NO_GETTOD)
30034 time_t t;
30035 time(&t);
30036 *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
30037 #elif OS_VXWORKS
@@ -30009,34 +30038,38 @@
30038 struct timespec sNow;
30039 clock_gettime(CLOCK_REALTIME, &sNow);
30040 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
30041 #else
30042 struct timeval sNow;
30043 if( gettimeofday(&sNow, 0)==0 ){
30044 *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
30045 }else{
30046 rc = SQLITE_ERROR;
30047 }
30048 #endif
30049
30050 #ifdef SQLITE_TEST
30051 if( sqlite3_current_time ){
30052 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
30053 }
30054 #endif
30055 UNUSED_PARAMETER(NotUsed);
30056 return rc;
30057 }
30058
30059 /*
30060 ** Find the current time (in Universal Coordinated Time). Write the
30061 ** current time and date as a Julian Day number into *prNow and
30062 ** return 0. Return 1 if the time and date cannot be found.
30063 */
30064 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
30065 sqlite3_int64 i = 0;
30066 int rc;
30067 UNUSED_PARAMETER(NotUsed);
30068 rc = unixCurrentTimeInt64(0, &i);
30069 *prNow = i/86400000.0;
30070 return rc;
30071 }
30072
30073 /*
30074 ** We added the xGetLastError() method with the intention of providing
30075 ** better low-level error messages when operating-system problems come up
@@ -34169,11 +34202,11 @@
34202
34203 if( h==INVALID_HANDLE_VALUE ){
34204 pFile->lastErrno = GetLastError();
34205 winLogError(SQLITE_CANTOPEN, "winOpen", zUtf8Name);
34206 free(zConverted);
34207 if( isReadWrite && !isExclusive ){
34208 return winOpen(pVfs, zName, id,
34209 ((flags|SQLITE_OPEN_READONLY)&~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)), pOutFlags);
34210 }else{
34211 return SQLITE_CANTOPEN_BKPT;
34212 }
@@ -34535,11 +34568,11 @@
34568 }
34569 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
34570 UNUSED_PARAMETER(pVfs);
34571 getLastErrorMsg(nBuf, zBufOut);
34572 }
34573 static void (*winDlSym(sqlite3_vfs *pVfs, void *pHandle, const char *zSymbol))(void){
34574 UNUSED_PARAMETER(pVfs);
34575 #if SQLITE_OS_WINCE
34576 /* The GetProcAddressA() routine is only available on wince. */
34577 return (void(*)(void))GetProcAddressA((HANDLE)pHandle, zSymbol);
34578 #else
@@ -34546,11 +34579,11 @@
34579 /* All other windows platforms expect GetProcAddress() to take
34580 ** an Ansi string regardless of the _UNICODE setting */
34581 return (void(*)(void))GetProcAddress((HANDLE)pHandle, zSymbol);
34582 #endif
34583 }
34584 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
34585 UNUSED_PARAMETER(pVfs);
34586 FreeLibrary((HANDLE)pHandle);
34587 }
34588 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
34589 #define winDlOpen 0
@@ -34620,11 +34653,12 @@
34653 ** the current time and date as a Julian Day number times 86_400_000. In
34654 ** other words, write into *piNow the number of milliseconds since the Julian
34655 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
34656 ** proleptic Gregorian calendar.
34657 **
34658 ** On success, return SQLITE_OK. Return SQLITE_ERROR if the time and date
34659 ** cannot be found.
34660 */
34661 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
34662 /* FILETIME structure is a 64-bit value representing the number of
34663 100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
34664 */
@@ -34640,11 +34674,11 @@
34674 #if SQLITE_OS_WINCE
34675 SYSTEMTIME time;
34676 GetSystemTime(&time);
34677 /* if SystemTimeToFileTime() fails, it returns zero. */
34678 if (!SystemTimeToFileTime(&time,&ft)){
34679 return SQLITE_ERROR;
34680 }
34681 #else
34682 GetSystemTimeAsFileTime( &ft );
34683 #endif
34684
@@ -34656,19 +34690,19 @@
34690 if( sqlite3_current_time ){
34691 *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
34692 }
34693 #endif
34694 UNUSED_PARAMETER(pVfs);
34695 return SQLITE_OK;
34696 }
34697
34698 /*
34699 ** Find the current time (in Universal Coordinated Time). Write the
34700 ** current time and date as a Julian Day number into *prNow and
34701 ** return 0. Return 1 if the time and date cannot be found.
34702 */
34703 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
34704 int rc;
34705 sqlite3_int64 i;
34706 rc = winCurrentTimeInt64(pVfs, &i);
34707 if( !rc ){
34708 *prNow = i/86400000.0;
@@ -35789,12 +35823,10 @@
35823 typedef struct PCache1 PCache1;
35824 typedef struct PgHdr1 PgHdr1;
35825 typedef struct PgFreeslot PgFreeslot;
35826 typedef struct PGroup PGroup;
35827
 
 
35828
35829 /* Each page cache (or PCache) belongs to a PGroup. A PGroup is a set
35830 ** of one or more PCaches that are able to recycle each others unpinned
35831 ** pages when they are under memory pressure. A PGroup is an instance of
35832 ** the following object.
@@ -35821,69 +35853,11 @@
35853 int nMaxPage; /* Sum of nMax for purgeable caches */
35854 int nMinPage; /* Sum of nMin for purgeable caches */
35855 int mxPinned; /* nMaxpage + 10 - nMinPage */
35856 int nCurrentPage; /* Number of purgeable pages allocated */
35857 PgHdr1 *pLruHead, *pLruTail; /* LRU list of unpinned pages */
35858 };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35859
35860 /* Each page cache is an instance of the following object. Every
35861 ** open database file (including each in-memory database and each
35862 ** temporary or transient database) has a single page cache which
35863 ** is an instance of this object.
@@ -35983,21 +35957,10 @@
35957 **
35958 ** assert( PGHDR1_TO_PAGE(PAGE_TO_PGHDR1(pCache, X))==X );
35959 */
35960 #define PGHDR1_TO_PAGE(p) (void*)(((char*)p) - p->pCache->szPage)
35961 #define PAGE_TO_PGHDR1(c, p) (PgHdr1*)(((char*)p) + c->szPage)
 
 
 
 
 
 
 
 
 
 
 
35962
35963 /*
35964 ** Macros to enter and leave the PCache LRU mutex.
35965 */
35966 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
@@ -36120,159 +36083,32 @@
36083 return iSize;
36084 }
36085 }
36086 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
36087
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36088 /*
36089 ** Allocate a new page object initially associated with cache pCache.
36090 */
36091 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
36092 int nByte = sizeof(PgHdr1) + pCache->szPage;
36093 PgHdr1 *p = 0;
36094 void *pPg;
36095
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36096 /* The group mutex must be released before pcache1Alloc() is called. This
36097 ** is because it may call sqlite3_release_memory(), which assumes that
36098 ** this mutex is not held. */
36099 assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
36100 pcache1LeaveMutex(pCache->pGroup);
36101 pPg = pcache1Alloc(nByte);
36102 pcache1EnterMutex(pCache->pGroup);
36103
36104 if( pPg ){
36105 p = PAGE_TO_PGHDR1(pCache, pPg);
36106 if( pCache->bPurgeable ){
36107 pCache->pGroup->nCurrentPage++;
36108 }
 
 
36109 }
 
36110 return p;
36111 }
36112
36113 /*
36114 ** Free a page object allocated by pcache1AllocPage().
@@ -36282,53 +36118,12 @@
36118 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
36119 */
36120 static void pcache1FreePage(PgHdr1 *p){
36121 if( ALWAYS(p) ){
36122 PCache1 *pCache = p->pCache;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36123 assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
36124 pcache1Free(PGHDR1_TO_PAGE(p));
 
36125 if( pCache->bPurgeable ){
36126 pCache->pGroup->nCurrentPage--;
36127 }
36128 }
36129 }
@@ -36935,13 +36730,10 @@
36730 ** been released, the function returns. The return value is the total number
36731 ** of bytes of memory released.
36732 */
36733 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
36734 int nFree = 0;
 
 
 
36735 assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
36736 assert( sqlite3_mutex_notheld(pcache1.mutex) );
36737 if( pcache1.pStart==0 ){
36738 PgHdr1 *p;
36739 pcache1EnterMutex(&pcache1.grp);
@@ -38200,12 +37992,12 @@
37992 i64 journalSizeLimit; /* Size limit for persistent journal files */
37993 char *zFilename; /* Name of the database file */
37994 char *zJournal; /* Name of the journal file */
37995 int (*xBusyHandler)(void*); /* Function to call when busy */
37996 void *pBusyHandlerArg; /* Context argument for xBusyHandler */
37997 int nHit, nMiss; /* Total cache hits and misses */
37998 #ifdef SQLITE_TEST
 
37999 int nRead, nWrite; /* Database pages read/written */
38000 #endif
38001 void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
38002 #ifdef SQLITE_HAS_CODEC
38003 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
@@ -40233,11 +40025,10 @@
40025 needPagerReset = 0;
40026 }
40027 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40028 if( rc!=SQLITE_OK ){
40029 if( rc==SQLITE_DONE ){
 
40030 pPager->journalOff = szJ;
40031 break;
40032 }else if( rc==SQLITE_IOERR_SHORT_READ ){
40033 /* If the journal has been truncated, simply stop reading and
40034 ** processing the journal. This might happen if the journal was
@@ -40495,10 +40286,11 @@
40286 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
40287 PgHdr *p; /* For looping over pages */
40288 #endif
40289
40290 assert( pPager->pWal );
40291 assert( pList );
40292 #ifdef SQLITE_DEBUG
40293 /* Verify that the page list is in accending order */
40294 for(p=pList; p && p->pDirty; p=p->pDirty){
40295 assert( p->pgno < p->pDirty->pgno );
40296 }
@@ -41699,11 +41491,11 @@
41491 ** The doNotSpill flag inhibits all cache spilling regardless of whether
41492 ** or not a sync is required. This is set during a rollback.
41493 **
41494 ** Spilling is also prohibited when in an error state since that could
41495 ** lead to database corruption. In the current implementaton it
41496 ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
41497 ** while in the error state, hence it is impossible for this routine to
41498 ** be called in the error state. Nevertheless, we include a NEVER()
41499 ** test for the error state as a safeguard against future changes.
41500 */
41501 if( NEVER(pPager->errCode) ) return SQLITE_OK;
@@ -42535,18 +42327,17 @@
42327
42328 if( (*ppPage)->pPager && !noContent ){
42329 /* In this case the pcache already contains an initialized copy of
42330 ** the page. Return without further ado. */
42331 assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
42332 pPager->nHit++;
42333 return SQLITE_OK;
42334
42335 }else{
42336 /* The pager cache has created a new page. Its content needs to
42337 ** be initialized. */
42338
 
42339 pPg = *ppPage;
42340 pPg->pPager = pPager;
42341
42342 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
42343 ** number greater than this, or the unused locking-page, is requested. */
@@ -42578,10 +42369,11 @@
42369 }
42370 memset(pPg->pData, 0, pPager->pageSize);
42371 IOTRACE(("ZERO %p %d\n", pPager, pgno));
42372 }else{
42373 assert( pPg->pPager==pPager );
42374 pPager->nMiss++;
42375 rc = readDbPage(pPg);
42376 if( rc!=SQLITE_OK ){
42377 goto pager_acquire_err;
42378 }
42379 }
@@ -43611,10 +43403,35 @@
43403 a[9] = pPager->nRead;
43404 a[10] = pPager->nWrite;
43405 return a;
43406 }
43407 #endif
43408
43409 /*
43410 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
43411 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
43412 ** current cache hit or miss count, according to the value of eStat. If the
43413 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
43414 ** returning.
43415 */
43416 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
43417 int *piStat;
43418
43419 assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
43420 || eStat==SQLITE_DBSTATUS_CACHE_MISS
43421 );
43422 if( eStat==SQLITE_DBSTATUS_CACHE_HIT ){
43423 piStat = &pPager->nHit;
43424 }else{
43425 piStat = &pPager->nMiss;
43426 }
43427
43428 *pnVal += *piStat;
43429 if( reset ){
43430 *piStat = 0;
43431 }
43432 }
43433
43434 /*
43435 ** Return true if this is an in-memory pager.
43436 */
43437 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
@@ -46706,11 +46523,11 @@
46523 */
46524 if( iRead ){
46525 int sz;
46526 i64 iOffset;
46527 sz = pWal->hdr.szPage;
46528 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
46529 testcase( sz<=32768 );
46530 testcase( sz>=65536 );
46531 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
46532 *pInWal = 1;
46533 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
@@ -50019,21 +49836,23 @@
49836 */
49837 if( isMemdb==0 && isTempDb==0 ){
49838 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
49839 int nFullPathname = pVfs->mxPathname+1;
49840 char *zFullPathname = sqlite3Malloc(nFullPathname);
49841 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
49842 p->sharable = 1;
49843 if( !zFullPathname ){
49844 sqlite3_free(p);
49845 return SQLITE_NOMEM;
49846 }
49847 sqlite3OsFullPathname(pVfs, zFilename, nFullPathname, zFullPathname);
49848 #if SQLITE_THREADSAFE
49849 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
49850 sqlite3_mutex_enter(mutexOpen);
49851 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
49852 sqlite3_mutex_enter(mutexShared);
49853 #endif
49854 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
49855 assert( pBt->nRef>0 );
49856 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager))
49857 && sqlite3PagerVfs(pBt->pPager)==pVfs ){
49858 int iDb;
@@ -50135,13 +49954,13 @@
49954
49955 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
49956 /* Add the new BtShared object to the linked list sharable BtShareds.
49957 */
49958 if( p->sharable ){
49959 MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
49960 pBt->nRef = 1;
49961 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
49962 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
49963 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
49964 if( pBt->mutex==0 ){
49965 rc = SQLITE_NOMEM;
49966 db->mallocFailed = 0;
@@ -50219,16 +50038,16 @@
50038 ** true if the BtShared.nRef counter reaches zero and return
50039 ** false if it is still positive.
50040 */
50041 static int removeFromSharingList(BtShared *pBt){
50042 #ifndef SQLITE_OMIT_SHARED_CACHE
50043 MUTEX_LOGIC( sqlite3_mutex *pMaster; )
50044 BtShared *pList;
50045 int removed = 0;
50046
50047 assert( sqlite3_mutex_notheld(pBt->mutex) );
50048 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
50049 sqlite3_mutex_enter(pMaster);
50050 pBt->nRef--;
50051 if( pBt->nRef<=0 ){
50052 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
50053 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
@@ -52191,25 +52010,59 @@
52010 offset -= ovflSize;
52011 }else{
52012 /* Need to read this page properly. It contains some of the
52013 ** range of data that is being read (eOp==0) or written (eOp!=0).
52014 */
52015 #ifdef SQLITE_DIRECT_OVERFLOW_READ
52016 sqlite3_file *fd;
52017 #endif
52018 int a = amt;
52019 if( a + offset > ovflSize ){
52020 a = ovflSize - offset;
52021 }
52022
52023 #ifdef SQLITE_DIRECT_OVERFLOW_READ
52024 /* If all the following are true:
52025 **
52026 ** 1) this is a read operation, and
52027 ** 2) data is required from the start of this overflow page, and
52028 ** 3) the database is file-backed, and
52029 ** 4) there is no open write-transaction, and
52030 ** 5) the database is not a WAL database,
52031 **
52032 ** then data can be read directly from the database file into the
52033 ** output buffer, bypassing the page-cache altogether. This speeds
52034 ** up loading large records that span many overflow pages.
52035 */
52036 if( eOp==0 /* (1) */
52037 && offset==0 /* (2) */
52038 && pBt->inTransaction==TRANS_READ /* (4) */
52039 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (3) */
52040 && pBt->pPage1->aData[19]==0x01 /* (5) */
52041 ){
52042 u8 aSave[4];
52043 u8 *aWrite = &pBuf[-4];
52044 memcpy(aSave, aWrite, 4);
52045 rc = sqlite3OsRead(fd, aWrite, a+4, pBt->pageSize * (nextPage-1));
52046 nextPage = get4byte(aWrite);
52047 memcpy(aWrite, aSave, 4);
52048 }else
52049 #endif
52050
52051 {
52052 DbPage *pDbPage;
52053 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
52054 if( rc==SQLITE_OK ){
52055 aPayload = sqlite3PagerGetData(pDbPage);
52056 nextPage = get4byte(aPayload);
52057 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52058 sqlite3PagerUnref(pDbPage);
52059 offset = 0;
52060 }
52061 }
52062 amt -= a;
52063 pBuf += a;
52064 }
52065 }
52066 }
52067
52068 if( rc==SQLITE_OK && amt>0 ){
@@ -52804,11 +52657,10 @@
52657 }
52658 }
52659 if( c==0 ){
52660 if( pPage->intKey && !pPage->leaf ){
52661 lwr = idx;
 
52662 break;
52663 }else{
52664 *pRes = 0;
52665 rc = SQLITE_OK;
52666 goto moveto_finish;
@@ -52822,11 +52674,11 @@
52674 if( lwr>upr ){
52675 break;
52676 }
52677 pCur->aiIdx[pCur->iPage] = (u16)(idx = (lwr+upr)/2);
52678 }
52679 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
52680 assert( pPage->isInit );
52681 if( pPage->leaf ){
52682 chldPg = 0;
52683 }else if( lwr>=pPage->nCell ){
52684 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
@@ -53087,10 +52939,12 @@
52939 }
52940 if( rc ){
52941 pTrunk = 0;
52942 goto end_allocate_page;
52943 }
52944 assert( pTrunk!=0 );
52945 assert( pTrunk->aData!=0 );
52946
52947 k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
52948 if( k==0 && !searchList ){
52949 /* The trunk has no leaves and the list is not being searched.
52950 ** So extract the trunk page itself and use it as the newly
@@ -54214,17 +54068,19 @@
54068 ** This is safe because dropping a cell only overwrites the first
54069 ** four bytes of it, and this function does not need the first
54070 ** four bytes of the divider cell. So the pointer is safe to use
54071 ** later on.
54072 **
54073 ** But not if we are in secure-delete mode. In secure-delete mode,
54074 ** the dropCell() routine will overwrite the entire cell with zeroes.
54075 ** In this case, temporarily copy the cell into the aOvflSpace[]
54076 ** buffer. It will be copied out again as soon as the aSpace[] buffer
54077 ** is allocated. */
54078 if( pBt->secureDelete ){
54079 int iOff;
54080
54081 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
54082 if( (iOff+szNew[i])>(int)pBt->usableSize ){
54083 rc = SQLITE_CORRUPT_BKPT;
54084 memset(apOld, 0, (i+1)*sizeof(MemPage*));
54085 goto balance_cleanup;
54086 }else{
@@ -54640,10 +54496,11 @@
54496 int isDivider = 0;
54497 while( i==iNextOld ){
54498 /* Cell i is the cell immediately following the last cell on old
54499 ** sibling page j. If the siblings are not leaf pages of an
54500 ** intkey b-tree, then cell i was a divider cell. */
54501 assert( j+1 < ArraySize(apCopy) );
54502 pOld = apCopy[++j];
54503 iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
54504 if( pOld->nOverflow ){
54505 nOverflow = pOld->nOverflow;
54506 iOverflow = i + !leafData + pOld->aOvfl[0].idx;
@@ -56982,18 +56839,18 @@
56839 /*
56840 ** Release all resources associated with an sqlite3_backup* handle.
56841 */
56842 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
56843 sqlite3_backup **pp; /* Ptr to head of pagers backup list */
56844 MUTEX_LOGIC( sqlite3_mutex *mutex; ) /* Mutex to protect source database */
56845 int rc; /* Value to return */
56846
56847 /* Enter the mutexes */
56848 if( p==0 ) return SQLITE_OK;
56849 sqlite3_mutex_enter(p->pSrcDb->mutex);
56850 sqlite3BtreeEnter(p->pSrc);
56851 MUTEX_LOGIC( mutex = p->pSrcDb->mutex; )
56852 if( p->pDestDb ){
56853 sqlite3_mutex_enter(p->pDestDb->mutex);
56854 }
56855
56856 /* Detach this backup from the source pager. */
@@ -57108,13 +56965,21 @@
56965 ** goes wrong, the transaction on pTo is rolled back. If successful, the
56966 ** transaction is committed before returning.
56967 */
56968 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
56969 int rc;
56970 sqlite3_file *pFd; /* File descriptor for database pTo */
56971 sqlite3_backup b;
56972 sqlite3BtreeEnter(pTo);
56973 sqlite3BtreeEnter(pFrom);
56974
56975 assert( sqlite3BtreeIsInTrans(pTo) );
56976 pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
56977 if( pFd->pMethods ){
56978 i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
56979 sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
56980 }
56981
56982 /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
56983 ** to 0. This is used by the implementations of sqlite3_backup_step()
56984 ** and sqlite3_backup_finish() to detect that they are being called
56985 ** from this function, not directly by the user.
@@ -57137,10 +57002,11 @@
57002 rc = sqlite3_backup_finish(&b);
57003 if( rc==SQLITE_OK ){
57004 pTo->pBt->pageSizeFixed = 0;
57005 }
57006
57007 assert( sqlite3BtreeIsInTrans(pTo)==0 );
57008 sqlite3BtreeLeave(pFrom);
57009 sqlite3BtreeLeave(pTo);
57010 return rc;
57011 }
57012 #endif /* SQLITE_OMIT_VACUUM */
@@ -58171,15 +58037,15 @@
58037 *ppVal = 0;
58038 return SQLITE_OK;
58039 }
58040 op = pExpr->op;
58041
58042 /* op can only be TK_REGISTER if we have compiled with SQLITE_ENABLE_STAT3.
58043 ** The ifdef here is to enable us to achieve 100% branch test coverage even
58044 ** when SQLITE_ENABLE_STAT3 is omitted.
58045 */
58046 #ifdef SQLITE_ENABLE_STAT3
58047 if( op==TK_REGISTER ) op = pExpr->op2;
58048 #else
58049 if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
58050 #endif
58051
@@ -58874,12 +58740,12 @@
58740 /*
58741 ** Change the P2 operand of instruction addr so that it points to
58742 ** the address of the next instruction to be coded.
58743 */
58744 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
58745 assert( addr>=0 || p->db->mallocFailed );
58746 if( addr>=0 ) sqlite3VdbeChangeP2(p, addr, p->nOp);
58747 }
58748
58749
58750 /*
58751 ** If the input FuncDef structure is ephemeral, then free it. If
@@ -59080,34 +58946,33 @@
58946 ** Change the comment on the the most recently coded instruction. Or
58947 ** insert a No-op and add the comment to that new instruction. This
58948 ** makes the code easier to read during debugging. None of this happens
58949 ** in a production build.
58950 */
58951 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
 
 
58952 assert( p->nOp>0 || p->aOp==0 );
58953 assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
58954 if( p->nOp ){
58955 assert( p->aOp );
58956 sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
58957 p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
58958 }
58959 }
58960 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
58961 va_list ap;
58962 if( p ){
58963 va_start(ap, zFormat);
58964 vdbeVComment(p, zFormat, ap);
 
58965 va_end(ap);
58966 }
58967 }
58968 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
58969 va_list ap;
58970 if( p ){
58971 sqlite3VdbeAddOp0(p, OP_Noop);
 
 
 
 
58972 va_start(ap, zFormat);
58973 vdbeVComment(p, zFormat, ap);
 
58974 va_end(ap);
58975 }
58976 }
58977 #endif /* NDEBUG */
58978
@@ -59441,11 +59306,11 @@
59306 SubProgram **apSub = 0; /* Array of sub-vdbes */
59307 Mem *pSub = 0; /* Memory cell hold array of subprogs */
59308 sqlite3 *db = p->db; /* The database connection */
59309 int i; /* Loop counter */
59310 int rc = SQLITE_OK; /* Return code */
59311 Mem *pMem = &p->aMem[1]; /* First Mem of result set */
59312
59313 assert( p->explain );
59314 assert( p->magic==VDBE_MAGIC_RUN );
59315 assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
59316
@@ -59452,10 +59317,11 @@
59317 /* Even though this opcode does not use dynamic strings for
59318 ** the result, result columns may become dynamic if the user calls
59319 ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
59320 */
59321 releaseMemArray(pMem, 8);
59322 p->pResultSet = 0;
59323
59324 if( p->rc==SQLITE_NOMEM ){
59325 /* This happens if a malloc() inside a call to sqlite3_column_text() or
59326 ** sqlite3_column_text16() failed. */
59327 db->mallocFailed = 1;
@@ -59606,10 +59472,11 @@
59472 pMem->type = SQLITE_NULL;
59473 }
59474 }
59475
59476 p->nResColumn = 8 - 4*(p->explain-1);
59477 p->pResultSet = &p->aMem[1];
59478 p->rc = SQLITE_OK;
59479 rc = SQLITE_ROW;
59480 }
59481 return rc;
59482 }
@@ -61361,11 +61228,11 @@
61228 ** than 2GiB are support - anything large must be database corruption.
61229 ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
61230 ** this code can safely assume that nCellKey is 32-bits
61231 */
61232 assert( sqlite3BtreeCursorIsValid(pCur) );
61233 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
61234 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
61235 assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
61236
61237 /* Read in the complete content of the index entry */
61238 memset(&m, 0, sizeof(m));
@@ -61436,11 +61303,11 @@
61303 int rc;
61304 BtCursor *pCur = pC->pCursor;
61305 Mem m;
61306
61307 assert( sqlite3BtreeCursorIsValid(pCur) );
61308 VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
61309 assert( rc==SQLITE_OK ); /* pCur is always valid so KeySize cannot fail */
61310 /* nCellKey will always be between 0 and 0xffffffff because of the say
61311 ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
61312 if( nCellKey<=0 || nCellKey>0x7fffffff ){
61313 *res = 0;
@@ -65712,20 +65579,20 @@
65579 }else if( u.am.pC->cacheStatus==p->cacheCtr ){
65580 u.am.payloadSize = u.am.pC->payloadSize;
65581 u.am.zRec = (char*)u.am.pC->aRow;
65582 }else if( u.am.pC->isIndex ){
65583 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65584 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.am.pCrsr, &u.am.payloadSize64);
65585 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
65586 /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
65587 ** payload size, so it is impossible for u.am.payloadSize64 to be
65588 ** larger than 32 bits. */
65589 assert( (u.am.payloadSize64 & SQLITE_MAX_U32)==(u64)u.am.payloadSize64 );
65590 u.am.payloadSize = (u32)u.am.payloadSize64;
65591 }else{
65592 assert( sqlite3BtreeCursorIsValid(u.am.pCrsr) );
65593 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.am.pCrsr, &u.am.payloadSize);
65594 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
65595 }
65596 }else if( ALWAYS(u.am.pC->pseudoTableReg>0) ){
65597 u.am.pReg = &aMem[u.am.pC->pseudoTableReg];
65598 assert( u.am.pReg->flags & MEM_Blob );
@@ -67773,18 +67640,18 @@
67640 rc = sqlite3VdbeCursorMoveto(u.bk.pC);
67641 if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
67642
67643 if( u.bk.pC->isIndex ){
67644 assert( !u.bk.pC->isTable );
67645 VVA_ONLY(rc =) sqlite3BtreeKeySize(u.bk.pCrsr, &u.bk.n64);
67646 assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
67647 if( u.bk.n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67648 goto too_big;
67649 }
67650 u.bk.n = (u32)u.bk.n64;
67651 }else{
67652 VVA_ONLY(rc =) sqlite3BtreeDataSize(u.bk.pCrsr, &u.bk.n);
67653 assert( rc==SQLITE_OK ); /* DataSize() cannot fail */
67654 if( u.bk.n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
67655 goto too_big;
67656 }
67657 }
@@ -73417,11 +73284,12 @@
73284 pNew->flags |= EP_IntValue;
73285 pNew->u.iValue = iValue;
73286 }else{
73287 int c;
73288 pNew->u.zToken = (char*)&pNew[1];
73289 assert( pToken->z!=0 || pToken->n==0 );
73290 if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
73291 pNew->u.zToken[pToken->n] = 0;
73292 if( dequote && nExtra>=3
73293 && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
73294 sqlite3Dequote(pNew->u.zToken);
73295 if( c=='"' ) pNew->flags |= EP_DblQuoted;
@@ -74456,15 +74324,23 @@
74324 ** ephemeral table.
74325 */
74326 p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
74327 if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
74328 sqlite3 *db = pParse->db; /* Database connection */
 
 
74329 Vdbe *v = sqlite3GetVdbe(pParse); /* Virtual machine being coded */
74330 Table *pTab; /* Table <table>. */
74331 Expr *pExpr; /* Expression <column> */
74332 int iCol; /* Index of column <column> */
74333 int iDb; /* Database idx for pTab */
74334
74335 assert( p ); /* Because of isCandidateForInOpt(p) */
74336 assert( p->pEList!=0 ); /* Because of isCandidateForInOpt(p) */
74337 assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
74338 assert( p->pSrc!=0 ); /* Because of isCandidateForInOpt(p) */
74339 pTab = p->pSrc->a[0].pTab;
74340 pExpr = p->pEList->a[0].pExpr;
74341 iCol = pExpr->iColumn;
74342
74343 /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
74344 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
74345 sqlite3CodeVerifySchema(pParse, iDb);
74346 sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
@@ -76467,11 +76343,11 @@
76343 if( !ExprHasProperty(pB, EP_IntValue) || pA->u.iValue!=pB->u.iValue ){
76344 return 2;
76345 }
76346 }else if( pA->op!=TK_COLUMN && pA->u.zToken ){
76347 if( ExprHasProperty(pB, EP_IntValue) || NEVER(pB->u.zToken==0) ) return 2;
76348 if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
76349 return 2;
76350 }
76351 }
76352 if( (pA->flags & EP_ExpCollate)!=(pB->flags & EP_ExpCollate) ) return 1;
76353 if( (pA->flags & EP_ExpCollate)!=0 && pA->pColl!=pB->pColl ) return 2;
@@ -77610,10 +77486,112 @@
77486 ** May you find forgiveness for yourself and forgive others.
77487 ** May you share freely, never taking more than you give.
77488 **
77489 *************************************************************************
77490 ** This file contains code associated with the ANALYZE command.
77491 **
77492 ** The ANALYZE command gather statistics about the content of tables
77493 ** and indices. These statistics are made available to the query planner
77494 ** to help it make better decisions about how to perform queries.
77495 **
77496 ** The following system tables are or have been supported:
77497 **
77498 ** CREATE TABLE sqlite_stat1(tbl, idx, stat);
77499 ** CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
77500 ** CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
77501 **
77502 ** Additional tables might be added in future releases of SQLite.
77503 ** The sqlite_stat2 table is not created or used unless the SQLite version
77504 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
77505 ** with SQLITE_ENABLE_STAT2. The sqlite_stat2 table is deprecated.
77506 ** The sqlite_stat2 table is superceded by sqlite_stat3, which is only
77507 ** created and used by SQLite versions 3.7.9 and later and with
77508 ** SQLITE_ENABLE_STAT3 defined. The fucntionality of sqlite_stat3
77509 ** is a superset of sqlite_stat2.
77510 **
77511 ** Format of sqlite_stat1:
77512 **
77513 ** There is normally one row per index, with the index identified by the
77514 ** name in the idx column. The tbl column is the name of the table to
77515 ** which the index belongs. In each such row, the stat column will be
77516 ** a string consisting of a list of integers. The first integer in this
77517 ** list is the number of rows in the index and in the table. The second
77518 ** integer is the average number of rows in the index that have the same
77519 ** value in the first column of the index. The third integer is the average
77520 ** number of rows in the index that have the same value for the first two
77521 ** columns. The N-th integer (for N>1) is the average number of rows in
77522 ** the index which have the same value for the first N-1 columns. For
77523 ** a K-column index, there will be K+1 integers in the stat column. If
77524 ** the index is unique, then the last integer will be 1.
77525 **
77526 ** The list of integers in the stat column can optionally be followed
77527 ** by the keyword "unordered". The "unordered" keyword, if it is present,
77528 ** must be separated from the last integer by a single space. If the
77529 ** "unordered" keyword is present, then the query planner assumes that
77530 ** the index is unordered and will not use the index for a range query.
77531 **
77532 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
77533 ** column contains a single integer which is the (estimated) number of
77534 ** rows in the table identified by sqlite_stat1.tbl.
77535 **
77536 ** Format of sqlite_stat2:
77537 **
77538 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
77539 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
77540 ** 3.6.18 and 3.7.8. The "stat2" table contains additional information
77541 ** about the distribution of keys within an index. The index is identified by
77542 ** the "idx" column and the "tbl" column is the name of the table to which
77543 ** the index belongs. There are usually 10 rows in the sqlite_stat2
77544 ** table for each index.
77545 **
77546 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
77547 ** inclusive are samples of the left-most key value in the index taken at
77548 ** evenly spaced points along the index. Let the number of samples be S
77549 ** (10 in the standard build) and let C be the number of rows in the index.
77550 ** Then the sampled rows are given by:
77551 **
77552 ** rownumber = (i*C*2 + C)/(S*2)
77553 **
77554 ** For i between 0 and S-1. Conceptually, the index space is divided into
77555 ** S uniform buckets and the samples are the middle row from each bucket.
77556 **
77557 ** The format for sqlite_stat2 is recorded here for legacy reference. This
77558 ** version of SQLite does not support sqlite_stat2. It neither reads nor
77559 ** writes the sqlite_stat2 table. This version of SQLite only supports
77560 ** sqlite_stat3.
77561 **
77562 ** Format for sqlite_stat3:
77563 **
77564 ** The sqlite_stat3 is an enhancement to sqlite_stat2. A new name is
77565 ** used to avoid compatibility problems.
77566 **
77567 ** The format of the sqlite_stat3 table is similar to the format of
77568 ** the sqlite_stat2 table. There are multiple entries for each index.
77569 ** The idx column names the index and the tbl column is the table of the
77570 ** index. If the idx and tbl columns are the same, then the sample is
77571 ** of the INTEGER PRIMARY KEY. The sample column is a value taken from
77572 ** the left-most column of the index. The nEq column is the approximate
77573 ** number of entires in the index whose left-most column exactly matches
77574 ** the sample. nLt is the approximate number of entires whose left-most
77575 ** column is less than the sample. The nDLt column is the approximate
77576 ** number of distinct left-most entries in the index that are less than
77577 ** the sample.
77578 **
77579 ** Future versions of SQLite might change to store a string containing
77580 ** multiple integers values in the nDLt column of sqlite_stat3. The first
77581 ** integer will be the number of prior index entires that are distinct in
77582 ** the left-most column. The second integer will be the number of prior index
77583 ** entries that are distinct in the first two columns. The third integer
77584 ** will be the number of prior index entries that are distinct in the first
77585 ** three columns. And so forth. With that extension, the nDLt field is
77586 ** similar in function to the sqlite_stat1.stat field.
77587 **
77588 ** There can be an arbitrary number of sqlite_stat3 entries per index.
77589 ** The ANALYZE command will typically generate sqlite_stat3 tables
77590 ** that contain between 10 and 40 samples which are distributed across
77591 ** the key space, though not uniformly, and which include samples with
77592 ** largest possible nEq values.
77593 */
77594 #ifndef SQLITE_OMIT_ANALYZE
77595
77596 /*
77597 ** This routine generates code that opens the sqlite_stat1 table for
@@ -77641,12 +77619,12 @@
77619 static const struct {
77620 const char *zName;
77621 const char *zCols;
77622 } aTable[] = {
77623 { "sqlite_stat1", "tbl,idx,stat" },
77624 #ifdef SQLITE_ENABLE_STAT3
77625 { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
77626 #endif
77627 };
77628
77629 int aRoot[] = {0, 0};
77630 u8 aCreateTbl[] = {0, 0};
@@ -77658,10 +77636,13 @@
77636 if( v==0 ) return;
77637 assert( sqlite3BtreeHoldsAllMutexes(db) );
77638 assert( sqlite3VdbeDb(v)==db );
77639 pDb = &db->aDb[iDb];
77640
77641 /* Create new statistic tables if they do not exist, or clear them
77642 ** if they do already exist.
77643 */
77644 for(i=0; i<ArraySize(aTable); i++){
77645 const char *zTab = aTable[i].zName;
77646 Table *pStat;
77647 if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
77648 /* The sqlite_stat[12] table does not exist. Create it. Note that a
@@ -77688,17 +77669,237 @@
77669 sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
77670 }
77671 }
77672 }
77673
77674 /* Open the sqlite_stat[13] tables for writing. */
77675 for(i=0; i<ArraySize(aTable); i++){
77676 sqlite3VdbeAddOp3(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb);
77677 sqlite3VdbeChangeP4(v, -1, (char *)3, P4_INT32);
77678 sqlite3VdbeChangeP5(v, aCreateTbl[i]);
77679 }
77680 }
77681
77682 /*
77683 ** Recommended number of samples for sqlite_stat3
77684 */
77685 #ifndef SQLITE_STAT3_SAMPLES
77686 # define SQLITE_STAT3_SAMPLES 24
77687 #endif
77688
77689 /*
77690 ** Three SQL functions - stat3_init(), stat3_push(), and stat3_pop() -
77691 ** share an instance of the following structure to hold their state
77692 ** information.
77693 */
77694 typedef struct Stat3Accum Stat3Accum;
77695 struct Stat3Accum {
77696 tRowcnt nRow; /* Number of rows in the entire table */
77697 tRowcnt nPSample; /* How often to do a periodic sample */
77698 int iMin; /* Index of entry with minimum nEq and hash */
77699 int mxSample; /* Maximum number of samples to accumulate */
77700 int nSample; /* Current number of samples */
77701 u32 iPrn; /* Pseudo-random number used for sampling */
77702 struct Stat3Sample {
77703 i64 iRowid; /* Rowid in main table of the key */
77704 tRowcnt nEq; /* sqlite_stat3.nEq */
77705 tRowcnt nLt; /* sqlite_stat3.nLt */
77706 tRowcnt nDLt; /* sqlite_stat3.nDLt */
77707 u8 isPSample; /* True if a periodic sample */
77708 u32 iHash; /* Tiebreaker hash */
77709 } *a; /* An array of samples */
77710 };
77711
77712 #ifdef SQLITE_ENABLE_STAT3
77713 /*
77714 ** Implementation of the stat3_init(C,S) SQL function. The two parameters
77715 ** are the number of rows in the table or index (C) and the number of samples
77716 ** to accumulate (S).
77717 **
77718 ** This routine allocates the Stat3Accum object.
77719 **
77720 ** The return value is the Stat3Accum object (P).
77721 */
77722 static void stat3Init(
77723 sqlite3_context *context,
77724 int argc,
77725 sqlite3_value **argv
77726 ){
77727 Stat3Accum *p;
77728 tRowcnt nRow;
77729 int mxSample;
77730 int n;
77731
77732 UNUSED_PARAMETER(argc);
77733 nRow = (tRowcnt)sqlite3_value_int64(argv[0]);
77734 mxSample = sqlite3_value_int(argv[1]);
77735 n = sizeof(*p) + sizeof(p->a[0])*mxSample;
77736 p = sqlite3_malloc( n );
77737 if( p==0 ){
77738 sqlite3_result_error_nomem(context);
77739 return;
77740 }
77741 memset(p, 0, n);
77742 p->a = (struct Stat3Sample*)&p[1];
77743 p->nRow = nRow;
77744 p->mxSample = mxSample;
77745 p->nPSample = p->nRow/(mxSample/3+1) + 1;
77746 sqlite3_randomness(sizeof(p->iPrn), &p->iPrn);
77747 sqlite3_result_blob(context, p, sizeof(p), sqlite3_free);
77748 }
77749 static const FuncDef stat3InitFuncdef = {
77750 2, /* nArg */
77751 SQLITE_UTF8, /* iPrefEnc */
77752 0, /* flags */
77753 0, /* pUserData */
77754 0, /* pNext */
77755 stat3Init, /* xFunc */
77756 0, /* xStep */
77757 0, /* xFinalize */
77758 "stat3_init", /* zName */
77759 0, /* pHash */
77760 0 /* pDestructor */
77761 };
77762
77763
77764 /*
77765 ** Implementation of the stat3_push(nEq,nLt,nDLt,rowid,P) SQL function. The
77766 ** arguments describe a single key instance. This routine makes the
77767 ** decision about whether or not to retain this key for the sqlite_stat3
77768 ** table.
77769 **
77770 ** The return value is NULL.
77771 */
77772 static void stat3Push(
77773 sqlite3_context *context,
77774 int argc,
77775 sqlite3_value **argv
77776 ){
77777 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[4]);
77778 tRowcnt nEq = sqlite3_value_int64(argv[0]);
77779 tRowcnt nLt = sqlite3_value_int64(argv[1]);
77780 tRowcnt nDLt = sqlite3_value_int64(argv[2]);
77781 i64 rowid = sqlite3_value_int64(argv[3]);
77782 u8 isPSample = 0;
77783 u8 doInsert = 0;
77784 int iMin = p->iMin;
77785 struct Stat3Sample *pSample;
77786 int i;
77787 u32 h;
77788
77789 UNUSED_PARAMETER(context);
77790 UNUSED_PARAMETER(argc);
77791 if( nEq==0 ) return;
77792 h = p->iPrn = p->iPrn*1103515245 + 12345;
77793 if( (nLt/p->nPSample)!=((nEq+nLt)/p->nPSample) ){
77794 doInsert = isPSample = 1;
77795 }else if( p->nSample<p->mxSample ){
77796 doInsert = 1;
77797 }else{
77798 if( nEq>p->a[iMin].nEq || (nEq==p->a[iMin].nEq && h>p->a[iMin].iHash) ){
77799 doInsert = 1;
77800 }
77801 }
77802 if( !doInsert ) return;
77803 if( p->nSample==p->mxSample ){
77804 assert( p->nSample - iMin - 1 >= 0 );
77805 memmove(&p->a[iMin], &p->a[iMin+1], sizeof(p->a[0])*(p->nSample-iMin-1));
77806 pSample = &p->a[p->nSample-1];
77807 }else{
77808 pSample = &p->a[p->nSample++];
77809 }
77810 pSample->iRowid = rowid;
77811 pSample->nEq = nEq;
77812 pSample->nLt = nLt;
77813 pSample->nDLt = nDLt;
77814 pSample->iHash = h;
77815 pSample->isPSample = isPSample;
77816
77817 /* Find the new minimum */
77818 if( p->nSample==p->mxSample ){
77819 pSample = p->a;
77820 i = 0;
77821 while( pSample->isPSample ){
77822 i++;
77823 pSample++;
77824 assert( i<p->nSample );
77825 }
77826 nEq = pSample->nEq;
77827 h = pSample->iHash;
77828 iMin = i;
77829 for(i++, pSample++; i<p->nSample; i++, pSample++){
77830 if( pSample->isPSample ) continue;
77831 if( pSample->nEq<nEq
77832 || (pSample->nEq==nEq && pSample->iHash<h)
77833 ){
77834 iMin = i;
77835 nEq = pSample->nEq;
77836 h = pSample->iHash;
77837 }
77838 }
77839 p->iMin = iMin;
77840 }
77841 }
77842 static const FuncDef stat3PushFuncdef = {
77843 5, /* nArg */
77844 SQLITE_UTF8, /* iPrefEnc */
77845 0, /* flags */
77846 0, /* pUserData */
77847 0, /* pNext */
77848 stat3Push, /* xFunc */
77849 0, /* xStep */
77850 0, /* xFinalize */
77851 "stat3_push", /* zName */
77852 0, /* pHash */
77853 0 /* pDestructor */
77854 };
77855
77856 /*
77857 ** Implementation of the stat3_get(P,N,...) SQL function. This routine is
77858 ** used to query the results. Content is returned for the Nth sqlite_stat3
77859 ** row where N is between 0 and S-1 and S is the number of samples. The
77860 ** value returned depends on the number of arguments.
77861 **
77862 ** argc==2 result: rowid
77863 ** argc==3 result: nEq
77864 ** argc==4 result: nLt
77865 ** argc==5 result: nDLt
77866 */
77867 static void stat3Get(
77868 sqlite3_context *context,
77869 int argc,
77870 sqlite3_value **argv
77871 ){
77872 int n = sqlite3_value_int(argv[1]);
77873 Stat3Accum *p = (Stat3Accum*)sqlite3_value_blob(argv[0]);
77874
77875 assert( p!=0 );
77876 if( p->nSample<=n ) return;
77877 switch( argc ){
77878 case 2: sqlite3_result_int64(context, p->a[n].iRowid); break;
77879 case 3: sqlite3_result_int64(context, p->a[n].nEq); break;
77880 case 4: sqlite3_result_int64(context, p->a[n].nLt); break;
77881 default: sqlite3_result_int64(context, p->a[n].nDLt); break;
77882 }
77883 }
77884 static const FuncDef stat3GetFuncdef = {
77885 -1, /* nArg */
77886 SQLITE_UTF8, /* iPrefEnc */
77887 0, /* flags */
77888 0, /* pUserData */
77889 0, /* pNext */
77890 stat3Get, /* xFunc */
77891 0, /* xStep */
77892 0, /* xFinalize */
77893 "stat3_get", /* zName */
77894 0, /* pHash */
77895 0 /* pDestructor */
77896 };
77897 #endif /* SQLITE_ENABLE_STAT3 */
77898
77899
77900
77901
77902 /*
77903 ** Generate code to do an analysis of all indices associated with
77904 ** a single table.
77905 */
@@ -77718,24 +77919,31 @@
77919 int endOfLoop; /* The end of the loop */
77920 int jZeroRows = -1; /* Jump from here if number of rows is zero */
77921 int iDb; /* Index of database containing pTab */
77922 int regTabname = iMem++; /* Register containing table name */
77923 int regIdxname = iMem++; /* Register containing index name */
77924 int regStat1 = iMem++; /* The stat column of sqlite_stat1 */
77925 #ifdef SQLITE_ENABLE_STAT3
77926 int regNumEq = regStat1; /* Number of instances. Same as regStat1 */
77927 int regNumLt = iMem++; /* Number of keys less than regSample */
77928 int regNumDLt = iMem++; /* Number of distinct keys less than regSample */
77929 int regSample = iMem++; /* The next sample value */
77930 int regRowid = regSample; /* Rowid of a sample */
77931 int regAccum = iMem++; /* Register to hold Stat3Accum object */
77932 int regLoop = iMem++; /* Loop counter */
77933 int regCount = iMem++; /* Number of rows in the table or index */
77934 int regTemp1 = iMem++; /* Intermediate register */
77935 int regTemp2 = iMem++; /* Intermediate register */
77936 int once = 1; /* One-time initialization */
77937 int shortJump = 0; /* Instruction address */
77938 int iTabCur = pParse->nTab++; /* Table cursor */
77939 #endif
77940 int regCol = iMem++; /* Content of a column in analyzed table */
77941 int regRec = iMem++; /* Register holding completed record */
77942 int regTemp = iMem++; /* Temporary use register */
77943 int regNewRowid = iMem++; /* Rowid for the inserted record */
77944
 
 
 
 
 
 
 
 
77945
77946 v = sqlite3GetVdbe(pParse);
77947 if( v==0 || NEVER(pTab==0) ){
77948 return;
77949 }
@@ -77764,13 +77972,18 @@
77972 iIdxCur = pParse->nTab++;
77973 sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
77974 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
77975 int nCol;
77976 KeyInfo *pKey;
77977 int addrIfNot = 0; /* address of OP_IfNot */
77978 int *aChngAddr; /* Array of jump instruction addresses */
77979
77980 if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
77981 VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
77982 nCol = pIdx->nColumn;
77983 aChngAddr = sqlite3DbMallocRaw(db, sizeof(int)*nCol);
77984 if( aChngAddr==0 ) continue;
77985 pKey = sqlite3IndexKeyinfo(pParse, pIdx);
77986 if( iMem+1+(nCol*2)>pParse->nMem ){
77987 pParse->nMem = iMem+1+(nCol*2);
77988 }
77989
@@ -77781,35 +77994,24 @@
77994 VdbeComment((v, "%s", pIdx->zName));
77995
77996 /* Populate the register containing the index name. */
77997 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, pIdx->zName, 0);
77998
77999 #ifdef SQLITE_ENABLE_STAT3
78000 if( once ){
78001 once = 0;
78002 sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
78003 }
78004 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regCount);
78005 sqlite3VdbeAddOp2(v, OP_Integer, SQLITE_STAT3_SAMPLES, regTemp1);
78006 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumEq);
78007 sqlite3VdbeAddOp2(v, OP_Integer, 0, regNumLt);
78008 sqlite3VdbeAddOp2(v, OP_Integer, -1, regNumDLt);
78009 sqlite3VdbeAddOp4(v, OP_Function, 1, regCount, regAccum,
78010 (char*)&stat3InitFuncdef, P4_FUNCDEF);
78011 sqlite3VdbeChangeP5(v, 2);
78012 #endif /* SQLITE_ENABLE_STAT3 */
 
 
 
 
 
 
 
 
 
 
 
78013
78014 /* The block of memory cells initialized here is used as follows.
78015 **
78016 ** iMem:
78017 ** The total number of rows in the table.
@@ -77835,79 +78037,87 @@
78037 /* Start the analysis loop. This loop runs through all the entries in
78038 ** the index b-tree. */
78039 endOfLoop = sqlite3VdbeMakeLabel(v);
78040 sqlite3VdbeAddOp2(v, OP_Rewind, iIdxCur, endOfLoop);
78041 topOfLoop = sqlite3VdbeCurrentAddr(v);
78042 sqlite3VdbeAddOp2(v, OP_AddImm, iMem, 1); /* Increment row counter */
78043
78044 for(i=0; i<nCol; i++){
78045 CollSeq *pColl;
78046 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regCol);
78047 if( i==0 ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78048 /* Always record the very first row */
78049 addrIfNot = sqlite3VdbeAddOp1(v, OP_IfNot, iMem+1);
78050 }
78051 assert( pIdx->azColl!=0 );
78052 assert( pIdx->azColl[i]!=0 );
78053 pColl = sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
78054 aChngAddr[i] = sqlite3VdbeAddOp4(v, OP_Ne, regCol, 0, iMem+nCol+i+1,
78055 (char*)pColl, P4_COLLSEQ);
78056 sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
78057 VdbeComment((v, "jump if column %d changed", i));
78058 #ifdef SQLITE_ENABLE_STAT3
78059 if( i==0 ){
78060 sqlite3VdbeAddOp2(v, OP_AddImm, regNumEq, 1);
78061 VdbeComment((v, "incr repeat count"));
78062 }
78063 #endif
78064 }
78065 sqlite3VdbeAddOp2(v, OP_Goto, 0, endOfLoop);
78066 for(i=0; i<nCol; i++){
78067 sqlite3VdbeJumpHere(v, aChngAddr[i]); /* Set jump dest for the OP_Ne */
78068 if( i==0 ){
78069 sqlite3VdbeJumpHere(v, addrIfNot); /* Jump dest for OP_IfNot */
78070 #ifdef SQLITE_ENABLE_STAT3
78071 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
78072 (char*)&stat3PushFuncdef, P4_FUNCDEF);
78073 sqlite3VdbeChangeP5(v, 5);
78074 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, pIdx->nColumn, regRowid);
78075 sqlite3VdbeAddOp3(v, OP_Add, regNumEq, regNumLt, regNumLt);
78076 sqlite3VdbeAddOp2(v, OP_AddImm, regNumDLt, 1);
78077 sqlite3VdbeAddOp2(v, OP_Integer, 1, regNumEq);
78078 #endif
78079 }
 
78080 sqlite3VdbeAddOp2(v, OP_AddImm, iMem+i+1, 1);
78081 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, iMem+nCol+i+1);
78082 }
78083 sqlite3DbFree(db, aChngAddr);
78084
78085 /* Always jump here after updating the iMem+1...iMem+1+nCol counters */
78086 sqlite3VdbeResolveLabel(v, endOfLoop);
78087
78088 sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, topOfLoop);
78089 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
78090 #ifdef SQLITE_ENABLE_STAT3
78091 sqlite3VdbeAddOp4(v, OP_Function, 1, regNumEq, regTemp2,
78092 (char*)&stat3PushFuncdef, P4_FUNCDEF);
78093 sqlite3VdbeChangeP5(v, 5);
78094 sqlite3VdbeAddOp2(v, OP_Integer, -1, regLoop);
78095 shortJump =
78096 sqlite3VdbeAddOp2(v, OP_AddImm, regLoop, 1);
78097 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regTemp1,
78098 (char*)&stat3GetFuncdef, P4_FUNCDEF);
78099 sqlite3VdbeChangeP5(v, 2);
78100 sqlite3VdbeAddOp1(v, OP_IsNull, regTemp1);
78101 sqlite3VdbeAddOp3(v, OP_NotExists, iTabCur, shortJump, regTemp1);
78102 sqlite3VdbeAddOp3(v, OP_Column, iTabCur, pIdx->aiColumn[0], regSample);
78103 sqlite3ColumnDefault(v, pTab, pIdx->aiColumn[0], regSample);
78104 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumEq,
78105 (char*)&stat3GetFuncdef, P4_FUNCDEF);
78106 sqlite3VdbeChangeP5(v, 3);
78107 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumLt,
78108 (char*)&stat3GetFuncdef, P4_FUNCDEF);
78109 sqlite3VdbeChangeP5(v, 4);
78110 sqlite3VdbeAddOp4(v, OP_Function, 1, regAccum, regNumDLt,
78111 (char*)&stat3GetFuncdef, P4_FUNCDEF);
78112 sqlite3VdbeChangeP5(v, 5);
78113 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regRec, "bbbbbb", 0);
78114 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
78115 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regRec, regNewRowid);
78116 sqlite3VdbeAddOp2(v, OP_Goto, 0, shortJump);
78117 sqlite3VdbeJumpHere(v, shortJump+2);
78118 #endif
78119
78120 /* Store the results in sqlite_stat1.
78121 **
78122 ** The result is a single row of the sqlite_stat1 table. The first
78123 ** two columns are the names of the table and index. The third column
@@ -77923,50 +78133,51 @@
78133 **
78134 ** If K==0 then no entry is made into the sqlite_stat1 table.
78135 ** If K>0 then it is always the case the D>0 so division by zero
78136 ** is never possible.
78137 */
78138 sqlite3VdbeAddOp2(v, OP_SCopy, iMem, regStat1);
78139 if( jZeroRows<0 ){
78140 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, iMem);
78141 }
78142 for(i=0; i<nCol; i++){
78143 sqlite3VdbeAddOp4(v, OP_String8, 0, regTemp, 0, " ", 0);
78144 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
78145 sqlite3VdbeAddOp3(v, OP_Add, iMem, iMem+i+1, regTemp);
78146 sqlite3VdbeAddOp2(v, OP_AddImm, regTemp, -1);
78147 sqlite3VdbeAddOp3(v, OP_Divide, iMem+i+1, regTemp, regTemp);
78148 sqlite3VdbeAddOp1(v, OP_ToInt, regTemp);
78149 sqlite3VdbeAddOp3(v, OP_Concat, regTemp, regStat1, regStat1);
78150 }
78151 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
78152 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
78153 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
78154 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
78155 }
78156
78157 /* If the table has no indices, create a single sqlite_stat1 entry
78158 ** containing NULL as the index name and the row count as the content.
78159 */
78160 if( pTab->pIndex==0 ){
78161 sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pTab->tnum, iDb);
78162 VdbeComment((v, "%s", pTab->zName));
78163 sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat1);
78164 sqlite3VdbeAddOp1(v, OP_Close, iIdxCur);
78165 jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
78166 }else{
78167 sqlite3VdbeJumpHere(v, jZeroRows);
78168 jZeroRows = sqlite3VdbeAddOp0(v, OP_Goto);
78169 }
78170 sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
78171 sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regRec, "aaa", 0);
78172 sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
78173 sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regRec, regNewRowid);
78174 sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
78175 if( pParse->nMem<regRec ) pParse->nMem = regRec;
78176 sqlite3VdbeJumpHere(v, jZeroRows);
78177 }
78178
78179
78180 /*
78181 ** Generate code that will cause the most recent index analysis to
78182 ** be loaded into internal hash tables where is can be used.
78183 */
@@ -77987,11 +78198,11 @@
78198 int iStatCur;
78199 int iMem;
78200
78201 sqlite3BeginWriteOperation(pParse, 0, iDb);
78202 iStatCur = pParse->nTab;
78203 pParse->nTab += 3;
78204 openStatTable(pParse, iDb, iStatCur, 0, 0);
78205 iMem = pParse->nMem+1;
78206 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78207 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
78208 Table *pTab = (Table*)sqliteHashData(k);
@@ -78012,11 +78223,11 @@
78223 assert( pTab!=0 );
78224 assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
78225 iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
78226 sqlite3BeginWriteOperation(pParse, 0, iDb);
78227 iStatCur = pParse->nTab;
78228 pParse->nTab += 3;
78229 if( pOnlyIdx ){
78230 openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
78231 }else{
78232 openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
78233 }
@@ -78117,11 +78328,11 @@
78328 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
78329 analysisInfo *pInfo = (analysisInfo*)pData;
78330 Index *pIndex;
78331 Table *pTable;
78332 int i, c, n;
78333 tRowcnt v;
78334 const char *z;
78335
78336 assert( argc==3 );
78337 UNUSED_PARAMETER2(NotUsed, argc);
78338
@@ -78160,40 +78371,172 @@
78371 /*
78372 ** If the Index.aSample variable is not NULL, delete the aSample[] array
78373 ** and its contents.
78374 */
78375 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
78376 #ifdef SQLITE_ENABLE_STAT3
78377 if( pIdx->aSample ){
78378 int j;
78379 for(j=0; j<pIdx->nSample; j++){
78380 IndexSample *p = &pIdx->aSample[j];
78381 if( p->eType==SQLITE_TEXT || p->eType==SQLITE_BLOB ){
78382 sqlite3DbFree(db, p->u.z);
78383 }
78384 }
78385 sqlite3DbFree(db, pIdx->aSample);
78386 }
78387 if( db && db->pnBytesFreed==0 ){
78388 pIdx->nSample = 0;
78389 pIdx->aSample = 0;
78390 }
78391 #else
78392 UNUSED_PARAMETER(db);
78393 UNUSED_PARAMETER(pIdx);
78394 #endif
78395 }
78396
78397 #ifdef SQLITE_ENABLE_STAT3
78398 /*
78399 ** Load content from the sqlite_stat3 table into the Index.aSample[]
78400 ** arrays of all indices.
78401 */
78402 static int loadStat3(sqlite3 *db, const char *zDb){
78403 int rc; /* Result codes from subroutines */
78404 sqlite3_stmt *pStmt = 0; /* An SQL statement being run */
78405 char *zSql; /* Text of the SQL statement */
78406 Index *pPrevIdx = 0; /* Previous index in the loop */
78407 int idx = 0; /* slot in pIdx->aSample[] for next sample */
78408 int eType; /* Datatype of a sample */
78409 IndexSample *pSample; /* A slot in pIdx->aSample[] */
78410
78411 if( !sqlite3FindTable(db, "sqlite_stat3", zDb) ){
78412 return SQLITE_OK;
78413 }
78414
78415 zSql = sqlite3MPrintf(db,
78416 "SELECT idx,count(*) FROM %Q.sqlite_stat3"
78417 " GROUP BY idx", zDb);
78418 if( !zSql ){
78419 return SQLITE_NOMEM;
78420 }
78421 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78422 sqlite3DbFree(db, zSql);
78423 if( rc ) return rc;
78424
78425 while( sqlite3_step(pStmt)==SQLITE_ROW ){
78426 char *zIndex; /* Index name */
78427 Index *pIdx; /* Pointer to the index object */
78428 int nSample; /* Number of samples */
78429
78430 zIndex = (char *)sqlite3_column_text(pStmt, 0);
78431 if( zIndex==0 ) continue;
78432 nSample = sqlite3_column_int(pStmt, 1);
78433 pIdx = sqlite3FindIndex(db, zIndex, zDb);
78434 if( pIdx==0 ) continue;
78435 assert( pIdx->nSample==0 );
78436 pIdx->nSample = nSample;
78437 pIdx->aSample = sqlite3MallocZero( nSample*sizeof(IndexSample) );
78438 pIdx->avgEq = pIdx->aiRowEst[1];
78439 if( pIdx->aSample==0 ){
78440 db->mallocFailed = 1;
78441 sqlite3_finalize(pStmt);
78442 return SQLITE_NOMEM;
78443 }
78444 }
78445 rc = sqlite3_finalize(pStmt);
78446 if( rc ) return rc;
78447
78448 zSql = sqlite3MPrintf(db,
78449 "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat3", zDb);
78450 if( !zSql ){
78451 return SQLITE_NOMEM;
78452 }
78453 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
78454 sqlite3DbFree(db, zSql);
78455 if( rc ) return rc;
78456
78457 while( sqlite3_step(pStmt)==SQLITE_ROW ){
78458 char *zIndex; /* Index name */
78459 Index *pIdx; /* Pointer to the index object */
78460 int i; /* Loop counter */
78461 tRowcnt sumEq; /* Sum of the nEq values */
78462
78463 zIndex = (char *)sqlite3_column_text(pStmt, 0);
78464 if( zIndex==0 ) continue;
78465 pIdx = sqlite3FindIndex(db, zIndex, zDb);
78466 if( pIdx==0 ) continue;
78467 if( pIdx==pPrevIdx ){
78468 idx++;
78469 }else{
78470 pPrevIdx = pIdx;
78471 idx = 0;
78472 }
78473 assert( idx<pIdx->nSample );
78474 pSample = &pIdx->aSample[idx];
78475 pSample->nEq = (tRowcnt)sqlite3_column_int64(pStmt, 1);
78476 pSample->nLt = (tRowcnt)sqlite3_column_int64(pStmt, 2);
78477 pSample->nDLt = (tRowcnt)sqlite3_column_int64(pStmt, 3);
78478 if( idx==pIdx->nSample-1 ){
78479 if( pSample->nDLt>0 ){
78480 for(i=0, sumEq=0; i<=idx-1; i++) sumEq += pIdx->aSample[i].nEq;
78481 pIdx->avgEq = (pSample->nLt - sumEq)/pSample->nDLt;
78482 }
78483 if( pIdx->avgEq<=0 ) pIdx->avgEq = 1;
78484 }
78485 eType = sqlite3_column_type(pStmt, 4);
78486 pSample->eType = (u8)eType;
78487 switch( eType ){
78488 case SQLITE_INTEGER: {
78489 pSample->u.i = sqlite3_column_int64(pStmt, 4);
78490 break;
78491 }
78492 case SQLITE_FLOAT: {
78493 pSample->u.r = sqlite3_column_double(pStmt, 4);
78494 break;
78495 }
78496 case SQLITE_NULL: {
78497 break;
78498 }
78499 default: assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB ); {
78500 const char *z = (const char *)(
78501 (eType==SQLITE_BLOB) ?
78502 sqlite3_column_blob(pStmt, 4):
78503 sqlite3_column_text(pStmt, 4)
78504 );
78505 int n = z ? sqlite3_column_bytes(pStmt, 4) : 0;
78506 pSample->nByte = n;
78507 if( n < 1){
78508 pSample->u.z = 0;
78509 }else{
78510 pSample->u.z = sqlite3Malloc(n);
78511 if( pSample->u.z==0 ){
78512 db->mallocFailed = 1;
78513 sqlite3_finalize(pStmt);
78514 return SQLITE_NOMEM;
78515 }
78516 memcpy(pSample->u.z, z, n);
78517 }
78518 }
78519 }
78520 }
78521 return sqlite3_finalize(pStmt);
78522 }
78523 #endif /* SQLITE_ENABLE_STAT3 */
78524
78525 /*
78526 ** Load the content of the sqlite_stat1 and sqlite_stat3 tables. The
78527 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
78528 ** arrays. The contents of sqlite_stat3 are used to populate the
78529 ** Index.aSample[] arrays.
78530 **
78531 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
78532 ** is returned. In this case, even if SQLITE_ENABLE_STAT3 was defined
78533 ** during compilation and the sqlite_stat3 table is present, no data is
78534 ** read from it.
78535 **
78536 ** If SQLITE_ENABLE_STAT3 was defined during compilation and the
78537 ** sqlite_stat3 table is not present in the database, SQLITE_ERROR is
78538 ** returned. However, in this case, data is read from the sqlite_stat1
78539 ** table (if it is present) before returning.
78540 **
78541 ** If an OOM error occurs, this function always sets db->mallocFailed.
78542 ** This means if the caller does not care about other errors, the return
@@ -78211,12 +78554,14 @@
78554 /* Clear any prior statistics */
78555 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
78556 for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
78557 Index *pIdx = sqliteHashData(i);
78558 sqlite3DefaultRowEst(pIdx);
78559 #ifdef SQLITE_ENABLE_STAT3
78560 sqlite3DeleteIndexSamples(db, pIdx);
78561 pIdx->aSample = 0;
78562 #endif
78563 }
78564
78565 /* Check to make sure the sqlite_stat1 table exists */
78566 sInfo.db = db;
78567 sInfo.zDatabase = db->aDb[iDb].zName;
@@ -78224,91 +78569,23 @@
78569 return SQLITE_ERROR;
78570 }
78571
78572 /* Load new statistics out of the sqlite_stat1 table */
78573 zSql = sqlite3MPrintf(db,
78574 "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
78575 if( zSql==0 ){
78576 rc = SQLITE_NOMEM;
78577 }else{
78578 rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
78579 sqlite3DbFree(db, zSql);
78580 }
78581
78582
78583 /* Load the statistics from the sqlite_stat3 table. */
78584 #ifdef SQLITE_ENABLE_STAT3
78585 if( rc==SQLITE_OK ){
78586 rc = loadStat3(db, sInfo.zDatabase);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78587 }
78588 #endif
78589
78590 if( rc==SQLITE_NOMEM ){
78591 db->mallocFailed = 1;
@@ -81120,11 +81397,15 @@
81397 Parse *pParse, /* The parsing context */
81398 int iDb, /* The database number */
81399 const char *zType, /* "idx" or "tbl" */
81400 const char *zName /* Name of index or table */
81401 ){
81402 static const char *azStatTab[] = {
81403 "sqlite_stat1",
81404 "sqlite_stat2",
81405 "sqlite_stat3",
81406 };
81407 int i;
81408 const char *zDbName = pParse->db->aDb[iDb].zName;
81409 for(i=0; i<ArraySize(azStatTab); i++){
81410 if( sqlite3FindTable(pParse->db, azStatTab[i], zDbName) ){
81411 sqlite3NestedParse(pParse,
@@ -81132,10 +81413,80 @@
81413 zDbName, azStatTab[i], zType, zName
81414 );
81415 }
81416 }
81417 }
81418
81419 /*
81420 ** Generate code to drop a table.
81421 */
81422 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
81423 Vdbe *v;
81424 sqlite3 *db = pParse->db;
81425 Trigger *pTrigger;
81426 Db *pDb = &db->aDb[iDb];
81427
81428 v = sqlite3GetVdbe(pParse);
81429 assert( v!=0 );
81430 sqlite3BeginWriteOperation(pParse, 1, iDb);
81431
81432 #ifndef SQLITE_OMIT_VIRTUALTABLE
81433 if( IsVirtual(pTab) ){
81434 sqlite3VdbeAddOp0(v, OP_VBegin);
81435 }
81436 #endif
81437
81438 /* Drop all triggers associated with the table being dropped. Code
81439 ** is generated to remove entries from sqlite_master and/or
81440 ** sqlite_temp_master if required.
81441 */
81442 pTrigger = sqlite3TriggerList(pParse, pTab);
81443 while( pTrigger ){
81444 assert( pTrigger->pSchema==pTab->pSchema ||
81445 pTrigger->pSchema==db->aDb[1].pSchema );
81446 sqlite3DropTriggerPtr(pParse, pTrigger);
81447 pTrigger = pTrigger->pNext;
81448 }
81449
81450 #ifndef SQLITE_OMIT_AUTOINCREMENT
81451 /* Remove any entries of the sqlite_sequence table associated with
81452 ** the table being dropped. This is done before the table is dropped
81453 ** at the btree level, in case the sqlite_sequence table needs to
81454 ** move as a result of the drop (can happen in auto-vacuum mode).
81455 */
81456 if( pTab->tabFlags & TF_Autoincrement ){
81457 sqlite3NestedParse(pParse,
81458 "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
81459 pDb->zName, pTab->zName
81460 );
81461 }
81462 #endif
81463
81464 /* Drop all SQLITE_MASTER table and index entries that refer to the
81465 ** table. The program name loops through the master table and deletes
81466 ** every row that refers to a table of the same name as the one being
81467 ** dropped. Triggers are handled seperately because a trigger can be
81468 ** created in the temp database that refers to a table in another
81469 ** database.
81470 */
81471 sqlite3NestedParse(pParse,
81472 "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
81473 pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
81474 if( !isView && !IsVirtual(pTab) ){
81475 destroyTable(pParse, pTab);
81476 }
81477
81478 /* Remove the table entry from SQLite's internal schema and modify
81479 ** the schema cookie.
81480 */
81481 if( IsVirtual(pTab) ){
81482 sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
81483 }
81484 sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81485 sqlite3ChangeCookie(pParse, iDb);
81486 sqliteViewResetAll(db, iDb);
81487 }
81488
81489 /*
81490 ** This routine is called to do the work of a DROP TABLE statement.
81491 ** pName is the name of the table to be dropped.
81492 */
@@ -81201,11 +81552,12 @@
81552 if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
81553 goto exit_drop_table;
81554 }
81555 }
81556 #endif
81557 if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
81558 && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
81559 sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
81560 goto exit_drop_table;
81561 }
81562
81563 #ifndef SQLITE_OMIT_VIEW
@@ -81225,72 +81577,15 @@
81577 /* Generate code to remove the table from the master table
81578 ** on disk.
81579 */
81580 v = sqlite3GetVdbe(pParse);
81581 if( v ){
 
 
81582 sqlite3BeginWriteOperation(pParse, 1, iDb);
81583 sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
 
 
 
 
 
81584 sqlite3FkDropTable(pParse, pName, pTab);
81585 sqlite3CodeDropTable(pParse, pTab, iDb, isView);
81586 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81587
81588 exit_drop_table:
81589 sqlite3SrcListDelete(db, pName);
81590 }
81591
@@ -81454,17 +81749,19 @@
81749 */
81750 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
81751 Table *pTab = pIndex->pTable; /* The table that is indexed */
81752 int iTab = pParse->nTab++; /* Btree cursor used for pTab */
81753 int iIdx = pParse->nTab++; /* Btree cursor used for pIndex */
81754 int iSorter; /* Cursor opened by OpenSorter (if in use) */
81755 int addr1; /* Address of top of loop */
81756 int addr2; /* Address to jump to for next iteration */
81757 int tnum; /* Root page of index */
81758 Vdbe *v; /* Generate code into this virtual machine */
81759 KeyInfo *pKey; /* KeyInfo for index */
81760 #ifdef SQLITE_OMIT_MERGE_SORT
81761 int regIdxKey; /* Registers containing the index key */
81762 #endif
81763 int regRecord; /* Register holding assemblied index record */
81764 sqlite3 *db = pParse->db; /* The database connection */
81765 int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
81766
81767 #ifndef SQLITE_OMIT_AUTHORIZATION
@@ -81494,21 +81791,22 @@
81791
81792 #ifndef SQLITE_OMIT_MERGE_SORT
81793 /* Open the sorter cursor if we are to use one. */
81794 iSorter = pParse->nTab++;
81795 sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO);
81796 #else
81797 iSorter = iTab;
81798 #endif
81799
81800 /* Open the table. Loop through all rows of the table, inserting index
81801 ** records into the sorter. */
81802 sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
81803 addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
 
81804 regRecord = sqlite3GetTempReg(pParse);
 
81805
81806 #ifndef SQLITE_OMIT_MERGE_SORT
81807 sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81808 sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
81809 sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
81810 sqlite3VdbeJumpHere(v, addr1);
81811 addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
81812 if( pIndex->onError!=OE_None ){
@@ -81524,10 +81822,12 @@
81822 }
81823 sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
81824 sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
81825 sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
81826 #else
81827 regIdxKey = sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1);
81828 addr2 = addr1 + 1;
81829 if( pIndex->onError!=OE_None ){
81830 const int regRowid = regIdxKey + pIndex->nColumn;
81831 const int j2 = sqlite3VdbeCurrentAddr(v) + 2;
81832 void * const pRegKey = SQLITE_INT_TO_PTR(regIdxKey);
81833
@@ -81621,10 +81921,11 @@
81921 ** before looking up the table.
81922 */
81923 assert( pName1 && pName2 );
81924 iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
81925 if( iDb<0 ) goto exit_create_index;
81926 assert( pName && pName->z );
81927
81928 #ifndef SQLITE_OMIT_TEMPDB
81929 /* If the index name was unqualified, check if the the table
81930 ** is a temp table. If so, set the database to 1. Do not do this
81931 ** if initialising a database schema.
@@ -81648,10 +81949,11 @@
81949 pTblName->a[0].zDatabase);
81950 if( !pTab || db->mallocFailed ) goto exit_create_index;
81951 assert( db->aDb[iDb].pSchema==pTab->pSchema );
81952 }else{
81953 assert( pName==0 );
81954 assert( pStart==0 );
81955 pTab = pParse->pNewTable;
81956 if( !pTab ) goto exit_create_index;
81957 iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81958 }
81959 pDb = &db->aDb[iDb];
@@ -81690,10 +81992,11 @@
81992 ** own name.
81993 */
81994 if( pName ){
81995 zName = sqlite3NameFromToken(db, pName);
81996 if( zName==0 ) goto exit_create_index;
81997 assert( pName->z!=0 );
81998 if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
81999 goto exit_create_index;
82000 }
82001 if( !db->init.busy ){
82002 if( sqlite3FindTable(db, zName, 0)!=0 ){
@@ -81769,24 +82072,24 @@
82072 */
82073 nName = sqlite3Strlen30(zName);
82074 nCol = pList->nExpr;
82075 pIndex = sqlite3DbMallocZero(db,
82076 sizeof(Index) + /* Index structure */
82077 sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
82078 sizeof(int)*nCol + /* Index.aiColumn */
 
82079 sizeof(char *)*nCol + /* Index.azColl */
82080 sizeof(u8)*nCol + /* Index.aSortOrder */
82081 nName + 1 + /* Index.zName */
82082 nExtra /* Collation sequence names */
82083 );
82084 if( db->mallocFailed ){
82085 goto exit_create_index;
82086 }
82087 pIndex->aiRowEst = (tRowcnt*)(&pIndex[1]);
82088 pIndex->azColl = (char**)(&pIndex->aiRowEst[nCol+1]);
82089 pIndex->aiColumn = (int *)(&pIndex->azColl[nCol]);
82090 pIndex->aSortOrder = (u8 *)(&pIndex->aiColumn[nCol]);
 
82091 pIndex->zName = (char *)(&pIndex->aSortOrder[nCol]);
82092 zExtra = (char *)(&pIndex->zName[nName+1]);
82093 memcpy(pIndex->zName, zName, nName+1);
82094 pIndex->pTable = pTab;
82095 pIndex->nColumn = pList->nExpr;
@@ -82059,13 +82362,13 @@
82362 ** Apart from that, we have little to go on besides intuition as to
82363 ** how aiRowEst[] should be initialized. The numbers generated here
82364 ** are based on typical values found in actual indices.
82365 */
82366 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
82367 tRowcnt *a = pIdx->aiRowEst;
82368 int i;
82369 tRowcnt n;
82370 assert( a!=0 );
82371 a[0] = pIdx->pTable->nRowEst;
82372 if( a[0]<10 ) a[0] = 10;
82373 n = 10;
82374 for(i=1; i<=pIdx->nColumn; i++){
@@ -82545,17 +82848,14 @@
82848
82849 /*
82850 ** Commit a transaction
82851 */
82852 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
 
82853 Vdbe *v;
82854
82855 assert( pParse!=0 );
82856 assert( pParse->db!=0 );
 
 
82857 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
82858 return;
82859 }
82860 v = sqlite3GetVdbe(pParse);
82861 if( v ){
@@ -82565,17 +82865,14 @@
82865
82866 /*
82867 ** Rollback a transaction
82868 */
82869 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
 
82870 Vdbe *v;
82871
82872 assert( pParse!=0 );
82873 assert( pParse->db!=0 );
 
 
82874 if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
82875 return;
82876 }
82877 v = sqlite3GetVdbe(pParse);
82878 if( v ){
@@ -84377,20 +84674,19 @@
84674 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84675 assert( z2==(char*)sqlite3_value_text(argv[0]) );
84676 if( z2 ){
84677 z1 = contextMalloc(context, ((i64)n)+1);
84678 if( z1 ){
84679 for(i=0; i<n; i++){
84680 z1[i] = (char)sqlite3Toupper(z2[i]);
 
84681 }
84682 sqlite3_result_text(context, z1, n, sqlite3_free);
84683 }
84684 }
84685 }
84686 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
84687 char *z1;
84688 const char *z2;
84689 int i, n;
84690 UNUSED_PARAMETER(argc);
84691 z2 = (char*)sqlite3_value_text(argv[0]);
84692 n = sqlite3_value_bytes(argv[0]);
@@ -84397,15 +84693,14 @@
84693 /* Verify that the call to _bytes() does not invalidate the _text() pointer */
84694 assert( z2==(char*)sqlite3_value_text(argv[0]) );
84695 if( z2 ){
84696 z1 = contextMalloc(context, ((i64)n)+1);
84697 if( z1 ){
84698 for(i=0; i<n; i++){
84699 z1[i] = sqlite3Tolower(z2[i]);
 
84700 }
84701 sqlite3_result_text(context, z1, n, sqlite3_free);
84702 }
84703 }
84704 }
84705
84706
@@ -86778,10 +87073,11 @@
87073 sqlite3SelectDelete(db, pSelect);
87074 if( db->mallocFailed==1 ){
87075 fkTriggerDelete(db, pTrigger);
87076 return 0;
87077 }
87078 assert( pStep!=0 );
87079
87080 switch( action ){
87081 case OE_Restrict:
87082 pStep->op = TK_SELECT;
87083 break;
@@ -88621,10 +88917,13 @@
88917 */
88918 if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
88919 return 0;
88920 }
88921 #endif
88922 if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
88923 return 0;
88924 }
88925
88926 /* If we get this far, it means either:
88927 **
88928 ** * We can always do the transfer if the table contains an
88929 ** an integer primary key
@@ -89698,11 +89997,11 @@
89997 sqlite3_vfs *pVfs = db->pVfs;
89998 void *handle;
89999 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
90000 char *zErrmsg = 0;
90001 void **aHandle;
90002 int nMsg = 300 + sqlite3Strlen30(zFile);
90003
90004 if( pzErrMsg ) *pzErrMsg = 0;
90005
90006 /* Ticket #1863. To avoid a creating security problems for older
90007 ** applications that relink against newer versions of SQLite, the
@@ -89735,10 +90034,11 @@
90034 }
90035 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
90036 sqlite3OsDlSym(pVfs, handle, zProc);
90037 if( xInit==0 ){
90038 if( pzErrMsg ){
90039 nMsg += sqlite3Strlen30(zProc);
90040 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
90041 if( zErrmsg ){
90042 sqlite3_snprintf(nMsg, zErrmsg,
90043 "no entry point [%s] in shared library [%s]", zProc,zFile);
90044 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
@@ -90420,11 +90720,11 @@
90720 ){
90721 int iReg;
90722 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
90723 sqlite3CodeVerifySchema(pParse, iDb);
90724 iReg = ++pParse->nMem;
90725 if( sqlite3Tolower(zLeft[0])=='p' ){
90726 sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
90727 }else{
90728 sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg, sqlite3Atoi(zRight));
90729 }
90730 sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
@@ -90486,12 +90786,14 @@
90786 */
90787 if( sqlite3StrICmp(zLeft,"journal_mode")==0 ){
90788 int eMode; /* One of the PAGER_JOURNALMODE_XXX symbols */
90789 int ii; /* Loop counter */
90790
90791 /* Force the schema to be loaded on all databases. This causes all
90792 ** database files to be opened and the journal_modes set. This is
90793 ** necessary because subsequent processing must know if the databases
90794 ** are in WAL mode. */
90795 if( sqlite3ReadSchema(pParse) ){
90796 goto pragma_out;
90797 }
90798
90799 sqlite3VdbeSetNumCols(v, 1);
@@ -91031,11 +91333,11 @@
91333 { OP_IfNeg, 1, 0, 0}, /* 1 */
91334 { OP_String8, 0, 3, 0}, /* 2 */
91335 { OP_ResultRow, 3, 1, 0},
91336 };
91337
91338 int isQuick = (sqlite3Tolower(zLeft[0])=='q');
91339
91340 /* Initialize the VDBE program */
91341 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
91342 pParse->nMem = 6;
91343 sqlite3VdbeSetNumCols(v, 1);
@@ -92406,10 +92708,11 @@
92708 Select standin;
92709 sqlite3 *db = pParse->db;
92710 pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
92711 assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
92712 if( pNew==0 ){
92713 assert( db->mallocFailed );
92714 pNew = &standin;
92715 memset(pNew, 0, sizeof(*pNew));
92716 }
92717 if( pEList==0 ){
92718 pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
@@ -92433,10 +92736,11 @@
92736 if( pNew!=&standin ) sqlite3DbFree(db, pNew);
92737 pNew = 0;
92738 }else{
92739 assert( pNew->pSrc!=0 || pParse->nErr>0 );
92740 }
92741 assert( pNew!=&standin );
92742 return pNew;
92743 }
92744
92745 /*
92746 ** Delete the given Select structure and all of its substructures.
@@ -93611,11 +93915,14 @@
93915 /* If the column contains an "AS <name>" phrase, use <name> as the name */
93916 zName = sqlite3DbStrDup(db, zName);
93917 }else{
93918 Expr *pColExpr = p; /* The expression that is the result column name */
93919 Table *pTab; /* Table associated with this expression */
93920 while( pColExpr->op==TK_DOT ){
93921 pColExpr = pColExpr->pRight;
93922 assert( pColExpr!=0 );
93923 }
93924 if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
93925 /* For columns use the column name name */
93926 int iCol = pColExpr->iColumn;
93927 pTab = pColExpr->pTab;
93928 if( iCol<0 ) iCol = pTab->iPKey;
@@ -98609,10 +98916,11 @@
98916 break;
98917 }
98918 }
98919 }
98920 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
98921 assert( aRegIdx );
98922 if( openAll || aRegIdx[i]>0 ){
98923 KeyInfo *pKey = sqlite3IndexKeyinfo(pParse, pIdx);
98924 sqlite3VdbeAddOp4(v, OP_OpenWrite, iCur+i+1, pIdx->tnum, iDb,
98925 (char*)pKey, P4_KEYINFO_HANDOFF);
98926 assert( pParse->nTab>iCur+i+1 );
@@ -98782,10 +99090,11 @@
99090 sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
99091 sqlite3VdbeJumpHere(v, addr);
99092
99093 /* Close all tables */
99094 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
99095 assert( aRegIdx );
99096 if( openAll || aRegIdx[i]>0 ){
99097 sqlite3VdbeAddOp2(v, OP_Close, iCur+i+1, 0);
99098 }
99099 }
99100 sqlite3VdbeAddOp2(v, OP_Close, iCur, 0);
@@ -98969,11 +99278,11 @@
99278 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
99279 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
99280 return sqlite3_errcode(db);
99281 }
99282 VVA_ONLY( rc = ) sqlite3_step(pStmt);
99283 assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
99284 return vacuumFinalize(db, pStmt, pzErrMsg);
99285 }
99286
99287 /*
99288 ** Execute zSql on database db. The statement returns exactly
@@ -99187,17 +99496,15 @@
99496 " WHERE type='view' OR type='trigger'"
99497 " OR (type='table' AND rootpage=0)"
99498 );
99499 if( rc ) goto end_of_vacuum;
99500
99501 /* At this point, there is a write transaction open on both the
99502 ** vacuum database and the main database. Assuming no error occurs,
99503 ** both transactions are closed by this block - the main database
99504 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
99505 ** call to sqlite3BtreeCommit().
 
 
99506 */
99507 {
99508 u32 meta;
99509 int i;
99510
@@ -100457,25 +100764,35 @@
100764 #define TERM_CODED 0x04 /* This term is already coded */
100765 #define TERM_COPIED 0x08 /* Has a child */
100766 #define TERM_ORINFO 0x10 /* Need to free the WhereTerm.u.pOrInfo object */
100767 #define TERM_ANDINFO 0x20 /* Need to free the WhereTerm.u.pAndInfo obj */
100768 #define TERM_OR_OK 0x40 /* Used during OR-clause processing */
100769 #ifdef SQLITE_ENABLE_STAT3
100770 # define TERM_VNULL 0x80 /* Manufactured x>NULL or x<=NULL term */
100771 #else
100772 # define TERM_VNULL 0x00 /* Disabled if not using stat3 */
100773 #endif
100774
100775 /*
100776 ** An instance of the following structure holds all information about a
100777 ** WHERE clause. Mostly this is a container for one or more WhereTerms.
100778 **
100779 ** Explanation of pOuter: For a WHERE clause of the form
100780 **
100781 ** a AND ((b AND c) OR (d AND e)) AND f
100782 **
100783 ** There are separate WhereClause objects for the whole clause and for
100784 ** the subclauses "(b AND c)" and "(d AND e)". The pOuter field of the
100785 ** subclauses points to the WhereClause object for the whole clause.
100786 */
100787 struct WhereClause {
100788 Parse *pParse; /* The parser context */
100789 WhereMaskSet *pMaskSet; /* Mapping of table cursor numbers to bitmasks */
100790 Bitmask vmask; /* Bitmask identifying virtual table cursors */
100791 WhereClause *pOuter; /* Outer conjunction */
100792 u8 op; /* Split operator. TK_AND or TK_OR */
100793 u16 wctrlFlags; /* Might include WHERE_AND_ONLY */
100794 int nTerm; /* Number of terms */
100795 int nSlot; /* Number of entries in a[] */
100796 WhereTerm *a; /* Each a[] describes a term of the WHERE cluase */
100797 #if defined(SQLITE_SMALL_STACK)
100798 WhereTerm aStatic[1]; /* Initial static space for a[] */
@@ -100600,18 +100917,21 @@
100917 ** Initialize a preallocated WhereClause structure.
100918 */
100919 static void whereClauseInit(
100920 WhereClause *pWC, /* The WhereClause to be initialized */
100921 Parse *pParse, /* The parsing context */
100922 WhereMaskSet *pMaskSet, /* Mapping from table cursor numbers to bitmasks */
100923 u16 wctrlFlags /* Might include WHERE_AND_ONLY */
100924 ){
100925 pWC->pParse = pParse;
100926 pWC->pMaskSet = pMaskSet;
100927 pWC->pOuter = 0;
100928 pWC->nTerm = 0;
100929 pWC->nSlot = ArraySize(pWC->aStatic);
100930 pWC->a = pWC->aStatic;
100931 pWC->vmask = 0;
100932 pWC->wctrlFlags = wctrlFlags;
100933 }
100934
100935 /* Forward reference */
100936 static void whereClauseClear(WhereClause*);
100937
@@ -100923,40 +101243,42 @@
101243 ){
101244 WhereTerm *pTerm;
101245 int k;
101246 assert( iCur>=0 );
101247 op &= WO_ALL;
101248 for(; pWC; pWC=pWC->pOuter){
101249 for(pTerm=pWC->a, k=pWC->nTerm; k; k--, pTerm++){
101250 if( pTerm->leftCursor==iCur
101251 && (pTerm->prereqRight & notReady)==0
101252 && pTerm->u.leftColumn==iColumn
101253 && (pTerm->eOperator & op)!=0
101254 ){
101255 if( pIdx && pTerm->eOperator!=WO_ISNULL ){
101256 Expr *pX = pTerm->pExpr;
101257 CollSeq *pColl;
101258 char idxaff;
101259 int j;
101260 Parse *pParse = pWC->pParse;
101261
101262 idxaff = pIdx->pTable->aCol[iColumn].affinity;
101263 if( !sqlite3IndexAffinityOk(pX, idxaff) ) continue;
101264
101265 /* Figure out the collation sequence required from an index for
101266 ** it to be useful for optimising expression pX. Store this
101267 ** value in variable pColl.
101268 */
101269 assert(pX->pLeft);
101270 pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
101271 assert(pColl || pParse->nErr);
101272
101273 for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
101274 if( NEVER(j>=pIdx->nColumn) ) return 0;
101275 }
101276 if( pColl && sqlite3StrICmp(pColl->zName, pIdx->azColl[j]) ) continue;
101277 }
101278 return pTerm;
101279 }
101280 }
101281 }
101282 return 0;
101283 }
101284
@@ -101029,11 +101351,11 @@
101351 int iCol = pRight->iColumn;
101352 pVal = sqlite3VdbeGetValue(pReprepare, iCol, SQLITE_AFF_NONE);
101353 if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
101354 z = (char *)sqlite3_value_text(pVal);
101355 }
101356 sqlite3VdbeSetVarmask(pParse->pVdbe, iCol); /* IMP: R-31526-56213 */
101357 assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
101358 }else if( op==TK_STRING ){
101359 z = pRight->u.zToken;
101360 }
101361 if( z ){
@@ -101047,11 +101369,11 @@
101369 pPrefix = sqlite3Expr(db, TK_STRING, z);
101370 if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
101371 *ppPrefix = pPrefix;
101372 if( op==TK_VARIABLE ){
101373 Vdbe *v = pParse->pVdbe;
101374 sqlite3VdbeSetVarmask(v, pRight->iColumn); /* IMP: R-31526-56213 */
101375 if( *pisComplete && pRight->u.zToken[1] ){
101376 /* If the rhs of the LIKE expression is a variable, and the current
101377 ** value of the variable means there is no need to invoke the LIKE
101378 ** function, then no OP_Variable will be added to the program.
101379 ** This causes problems for the sqlite3_bind_parameter_name()
@@ -101216,11 +101538,11 @@
101538 assert( pExpr->op==TK_OR );
101539 pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
101540 if( pOrInfo==0 ) return;
101541 pTerm->wtFlags |= TERM_ORINFO;
101542 pOrWc = &pOrInfo->wc;
101543 whereClauseInit(pOrWc, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101544 whereSplit(pOrWc, pExpr, TK_OR);
101545 exprAnalyzeAll(pSrc, pOrWc);
101546 if( db->mallocFailed ) return;
101547 assert( pOrWc->nTerm>=2 );
101548
@@ -101243,13 +101565,14 @@
101565 Bitmask b = 0;
101566 pOrTerm->u.pAndInfo = pAndInfo;
101567 pOrTerm->wtFlags |= TERM_ANDINFO;
101568 pOrTerm->eOperator = WO_AND;
101569 pAndWC = &pAndInfo->wc;
101570 whereClauseInit(pAndWC, pWC->pParse, pMaskSet, pWC->wctrlFlags);
101571 whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
101572 exprAnalyzeAll(pSrc, pAndWC);
101573 pAndWC->pOuter = pWC;
101574 testcase( db->mallocFailed );
101575 if( !db->mallocFailed ){
101576 for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
101577 assert( pAndTerm->pExpr );
101578 if( allowedOp(pAndTerm->pExpr->op) ){
@@ -101679,12 +102002,12 @@
102002 pNewTerm->prereqAll = pTerm->prereqAll;
102003 }
102004 }
102005 #endif /* SQLITE_OMIT_VIRTUALTABLE */
102006
102007 #ifdef SQLITE_ENABLE_STAT3
102008 /* When sqlite_stat3 histogram data is available an operator of the
102009 ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
102010 ** as "x>NULL" if x is not an INTEGER PRIMARY KEY. So construct a
102011 ** virtual term of that form.
102012 **
102013 ** Note that the virtual term must be tagged with TERM_VNULL. This
@@ -101718,11 +102041,11 @@
102041 pTerm->nChild = 1;
102042 pTerm->wtFlags |= TERM_COPIED;
102043 pNewTerm->prereqAll = pTerm->prereqAll;
102044 }
102045 }
102046 #endif /* SQLITE_ENABLE_STAT */
102047
102048 /* Prevent ON clause terms of a LEFT JOIN from being used to drive
102049 ** an index for tables to the left of the join.
102050 */
102051 pTerm->prereqRight |= extraRight;
@@ -102140,14 +102463,17 @@
102463 const int iCur = pSrc->iCursor; /* The cursor of the table to be accessed */
102464 const Bitmask maskSrc = getMask(pWC->pMaskSet, iCur); /* Bitmask for pSrc */
102465 WhereTerm * const pWCEnd = &pWC->a[pWC->nTerm]; /* End of pWC->a[] */
102466 WhereTerm *pTerm; /* A single term of the WHERE clause */
102467
102468 /* The OR-clause optimization is disallowed if the INDEXED BY or
102469 ** NOT INDEXED clauses are used or if the WHERE_AND_ONLY bit is set. */
102470 if( pSrc->notIndexed || pSrc->pIndex!=0 ){
102471 return;
102472 }
102473 if( pWC->wctrlFlags & WHERE_AND_ONLY ){
102474 return;
102475 }
102476
102477 /* Search the WHERE clause terms for a usable WO_OR term. */
102478 for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
102479 if( pTerm->eOperator==WO_OR
@@ -102172,10 +102498,11 @@
102498 bestIndex(pParse, pAndWC, pSrc, notReady, notValid, 0, &sTermCost);
102499 }else if( pOrTerm->leftCursor==iCur ){
102500 WhereClause tempWC;
102501 tempWC.pParse = pWC->pParse;
102502 tempWC.pMaskSet = pWC->pMaskSet;
102503 tempWC.pOuter = pWC;
102504 tempWC.op = TK_AND;
102505 tempWC.a = pOrTerm;
102506 tempWC.nTerm = 1;
102507 bestIndex(pParse, &tempWC, pSrc, notReady, notValid, 0, &sTermCost);
102508 }else{
@@ -102766,71 +103093,89 @@
103093 */
103094 bestOrClauseIndex(pParse, pWC, pSrc, notReady, notValid, pOrderBy, pCost);
103095 }
103096 #endif /* SQLITE_OMIT_VIRTUALTABLE */
103097
103098 #ifdef SQLITE_ENABLE_STAT3
103099 /*
103100 ** Estimate the location of a particular key among all keys in an
103101 ** index. Store the results in aStat as follows:
103102 **
103103 ** aStat[0] Est. number of rows less than pVal
103104 ** aStat[1] Est. number of rows equal to pVal
103105 **
103106 ** Return SQLITE_OK on success.
103107 */
103108 static int whereKeyStats(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103109 Parse *pParse, /* Database connection */
103110 Index *pIdx, /* Index to consider domain of */
103111 sqlite3_value *pVal, /* Value to consider */
103112 int roundUp, /* Round up if true. Round down if false */
103113 tRowcnt *aStat /* OUT: stats written here */
103114 ){
103115 tRowcnt n;
103116 IndexSample *aSample;
103117 int i, eType;
103118 int isEq = 0;
103119 i64 v;
103120 double r, rS;
103121
103122 assert( roundUp==0 || roundUp==1 );
103123 assert( pIdx->nSample>0 );
103124 if( pVal==0 ) return SQLITE_ERROR;
103125 n = pIdx->aiRowEst[0];
103126 aSample = pIdx->aSample;
103127 eType = sqlite3_value_type(pVal);
103128
103129 if( eType==SQLITE_INTEGER ){
103130 v = sqlite3_value_int64(pVal);
103131 r = (i64)v;
103132 for(i=0; i<pIdx->nSample; i++){
103133 if( aSample[i].eType==SQLITE_NULL ) continue;
103134 if( aSample[i].eType>=SQLITE_TEXT ) break;
103135 if( aSample[i].eType==SQLITE_INTEGER ){
103136 if( aSample[i].u.i>=v ){
103137 isEq = aSample[i].u.i==v;
103138 break;
103139 }
103140 }else{
103141 assert( aSample[i].eType==SQLITE_FLOAT );
103142 if( aSample[i].u.r>=r ){
103143 isEq = aSample[i].u.r==r;
103144 break;
103145 }
103146 }
103147 }
103148 }else if( eType==SQLITE_FLOAT ){
103149 r = sqlite3_value_double(pVal);
103150 for(i=0; i<pIdx->nSample; i++){
103151 if( aSample[i].eType==SQLITE_NULL ) continue;
103152 if( aSample[i].eType>=SQLITE_TEXT ) break;
103153 if( aSample[i].eType==SQLITE_FLOAT ){
103154 rS = aSample[i].u.r;
103155 }else{
103156 rS = aSample[i].u.i;
103157 }
103158 if( rS>=r ){
103159 isEq = rS==r;
103160 break;
103161 }
103162 }
103163 }else if( eType==SQLITE_NULL ){
103164 i = 0;
103165 if( aSample[0].eType==SQLITE_NULL ) isEq = 1;
103166 }else{
103167 assert( eType==SQLITE_TEXT || eType==SQLITE_BLOB );
103168 for(i=0; i<pIdx->nSample; i++){
103169 if( aSample[i].eType==SQLITE_TEXT || aSample[i].eType==SQLITE_BLOB ){
103170 break;
103171 }
103172 }
103173 if( i<pIdx->nSample ){
103174 sqlite3 *db = pParse->db;
103175 CollSeq *pColl;
103176 const u8 *z;
 
 
 
 
 
103177 if( eType==SQLITE_BLOB ){
103178 z = (const u8 *)sqlite3_value_blob(pVal);
103179 pColl = db->pDfltColl;
103180 assert( pColl->enc==SQLITE_UTF8 );
103181 }else{
@@ -102845,16 +103190,16 @@
103190 return SQLITE_NOMEM;
103191 }
103192 assert( z && pColl && pColl->xCmp );
103193 }
103194 n = sqlite3ValueBytes(pVal, pColl->enc);
103195
103196 for(; i<pIdx->nSample; i++){
103197 int c;
103198 int eSampletype = aSample[i].eType;
103199 if( eSampletype<eType ) continue;
103200 if( eSampletype!=eType ) break;
103201 #ifndef SQLITE_OMIT_UTF16
103202 if( pColl->enc!=SQLITE_UTF8 ){
103203 int nSample;
103204 char *zSample = sqlite3Utf8to16(
103205 db, pColl->enc, aSample[i].u.z, aSample[i].nByte, &nSample
@@ -102868,20 +103213,51 @@
103213 }else
103214 #endif
103215 {
103216 c = pColl->xCmp(pColl->pUser, aSample[i].nByte, aSample[i].u.z, n, z);
103217 }
103218 if( c>=0 ){
103219 if( c==0 ) isEq = 1;
103220 break;
103221 }
103222 }
103223 }
103224 }
103225
103226 /* At this point, aSample[i] is the first sample that is greater than
103227 ** or equal to pVal. Or if i==pIdx->nSample, then all samples are less
103228 ** than pVal. If aSample[i]==pVal, then isEq==1.
103229 */
103230 if( isEq ){
103231 assert( i<pIdx->nSample );
103232 aStat[0] = aSample[i].nLt;
103233 aStat[1] = aSample[i].nEq;
103234 }else{
103235 tRowcnt iLower, iUpper, iGap;
103236 if( i==0 ){
103237 iLower = 0;
103238 iUpper = aSample[0].nLt;
103239 }else{
103240 iUpper = i>=pIdx->nSample ? n : aSample[i].nLt;
103241 iLower = aSample[i-1].nEq + aSample[i-1].nLt;
103242 }
103243 aStat[1] = pIdx->avgEq;
103244 if( iLower>=iUpper ){
103245 iGap = 0;
103246 }else{
103247 iGap = iUpper - iLower;
103248 }
103249 if( roundUp ){
103250 iGap = (iGap*2)/3;
103251 }else{
103252 iGap = iGap/3;
103253 }
103254 aStat[0] = iLower + iGap;
103255 }
103256 return SQLITE_OK;
103257 }
103258 #endif /* SQLITE_ENABLE_STAT3 */
103259
103260 /*
103261 ** If expression pExpr represents a literal value, set *pp to point to
103262 ** an sqlite3_value structure containing the same value, with affinity
103263 ** aff applied to it, before returning. It is the responsibility of the
@@ -102895,11 +103271,11 @@
103271 **
103272 ** If neither of the above apply, set *pp to NULL.
103273 **
103274 ** If an error occurs, return an error code. Otherwise, SQLITE_OK.
103275 */
103276 #ifdef SQLITE_ENABLE_STAT3
103277 static int valueFromExpr(
103278 Parse *pParse,
103279 Expr *pExpr,
103280 u8 aff,
103281 sqlite3_value **pp
@@ -102906,11 +103282,11 @@
103282 ){
103283 if( pExpr->op==TK_VARIABLE
103284 || (pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
103285 ){
103286 int iVar = pExpr->iColumn;
103287 sqlite3VdbeSetVarmask(pParse->pVdbe, iVar); /* IMP: R-31526-56213 */
103288 *pp = sqlite3VdbeGetValue(pParse->pReprepare, iVar, aff);
103289 return SQLITE_OK;
103290 }
103291 return sqlite3ValueFromExpr(pParse->db, pExpr, SQLITE_UTF8, aff, pp);
103292 }
@@ -102943,106 +103319,92 @@
103319 **
103320 ** ... FROM t1 WHERE a > ? AND a < ? ...
103321 **
103322 ** then nEq should be passed 0.
103323 **
103324 ** The returned value is an integer divisor to reduce the estimated
103325 ** search space. A return value of 1 means that range constraints are
103326 ** no help at all. A return value of 2 means range constraints are
103327 ** expected to reduce the search space by half. And so forth...
 
 
103328 **
103329 ** In the absence of sqlite_stat3 ANALYZE data, each range inequality
103330 ** reduces the search space by a factor of 4. Hence a single constraint (x>?)
103331 ** results in a return of 4 and a range constraint (x>? AND x<?) results
103332 ** in a return of 16.
103333 */
103334 static int whereRangeScanEst(
103335 Parse *pParse, /* Parsing & code generating context */
103336 Index *p, /* The index containing the range-compared column; "x" */
103337 int nEq, /* index into p->aCol[] of the range-compared column */
103338 WhereTerm *pLower, /* Lower bound on the range. ex: "x>123" Might be NULL */
103339 WhereTerm *pUpper, /* Upper bound on the range. ex: "x<455" Might be NULL */
103340 double *pRangeDiv /* OUT: Reduce search space by this divisor */
103341 ){
103342 int rc = SQLITE_OK;
103343
103344 #ifdef SQLITE_ENABLE_STAT3
103345
103346 if( nEq==0 && p->nSample ){
103347 sqlite3_value *pRangeVal;
103348 tRowcnt iLower = 0;
103349 tRowcnt iUpper = p->aiRowEst[0];
103350 tRowcnt a[2];
 
 
 
103351 u8 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103352
103353 if( pLower ){
103354 Expr *pExpr = pLower->pExpr->pRight;
103355 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
103356 assert( pLower->eOperator==WO_GT || pLower->eOperator==WO_GE );
103357 if( rc==SQLITE_OK
103358 && whereKeyStats(pParse, p, pRangeVal, 0, a)==SQLITE_OK
103359 ){
103360 iLower = a[0];
103361 if( pLower->eOperator==WO_GT ) iLower += a[1];
103362 }
103363 sqlite3ValueFree(pRangeVal);
103364 }
103365 if( rc==SQLITE_OK && pUpper ){
103366 Expr *pExpr = pUpper->pExpr->pRight;
103367 rc = valueFromExpr(pParse, pExpr, aff, &pRangeVal);
103368 assert( pUpper->eOperator==WO_LT || pUpper->eOperator==WO_LE );
103369 if( rc==SQLITE_OK
103370 && whereKeyStats(pParse, p, pRangeVal, 1, a)==SQLITE_OK
103371 ){
103372 iUpper = a[0];
103373 if( pUpper->eOperator==WO_LE ) iUpper += a[1];
103374 }
103375 sqlite3ValueFree(pRangeVal);
103376 }
103377 if( rc==SQLITE_OK ){
103378 if( iUpper<=iLower ){
103379 *pRangeDiv = (double)p->aiRowEst[0];
103380 }else{
103381 *pRangeDiv = (double)p->aiRowEst[0]/(double)(iUpper - iLower);
103382 }
103383 WHERETRACE(("range scan regions: %u..%u div=%g\n",
103384 (u32)iLower, (u32)iUpper, *pRangeDiv));
103385 return SQLITE_OK;
103386 }
103387 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103388 #else
103389 UNUSED_PARAMETER(pParse);
103390 UNUSED_PARAMETER(p);
103391 UNUSED_PARAMETER(nEq);
103392 #endif
103393 assert( pLower || pUpper );
103394 *pRangeDiv = (double)1;
103395 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ) *pRangeDiv *= (double)4;
103396 if( pUpper ) *pRangeDiv *= (double)4;
103397 return rc;
103398 }
103399
103400 #ifdef SQLITE_ENABLE_STAT3
103401 /*
103402 ** Estimate the number of rows that will be returned based on
103403 ** an equality constraint x=VALUE and where that VALUE occurs in
103404 ** the histogram data. This only works when x is the left-most
103405 ** column of an index and sqlite_stat3 histogram data is available
103406 ** for that index. When pExpr==NULL that means the constraint is
103407 ** "x IS NULL" instead of "x=VALUE".
103408 **
103409 ** Write the estimated row count into *pnRow and return SQLITE_OK.
103410 ** If unable to make an estimate, leave *pnRow unchanged and return
@@ -103058,44 +103420,36 @@
103420 Index *p, /* The index whose left-most column is pTerm */
103421 Expr *pExpr, /* Expression for VALUE in the x=VALUE constraint */
103422 double *pnRow /* Write the revised row estimate here */
103423 ){
103424 sqlite3_value *pRhs = 0; /* VALUE on right-hand side of pTerm */
 
103425 u8 aff; /* Column affinity */
103426 int rc; /* Subfunction return code */
103427 tRowcnt a[2]; /* Statistics */
103428
103429 assert( p->aSample!=0 );
103430 assert( p->nSample>0 );
103431 aff = p->pTable->aCol[p->aiColumn[0]].affinity;
103432 if( pExpr ){
103433 rc = valueFromExpr(pParse, pExpr, aff, &pRhs);
103434 if( rc ) goto whereEqualScanEst_cancel;
103435 }else{
103436 pRhs = sqlite3ValueNew(pParse->db);
103437 }
103438 if( pRhs==0 ) return SQLITE_NOTFOUND;
103439 rc = whereKeyStats(pParse, p, pRhs, 0, a);
103440 if( rc==SQLITE_OK ){
103441 WHERETRACE(("equality scan regions: %d\n", (int)a[1]));
103442 *pnRow = a[1];
103443 }
 
 
 
 
 
 
 
 
103444 whereEqualScanEst_cancel:
103445 sqlite3ValueFree(pRhs);
103446 return rc;
103447 }
103448 #endif /* defined(SQLITE_ENABLE_STAT3) */
103449
103450 #ifdef SQLITE_ENABLE_STAT3
103451 /*
103452 ** Estimate the number of rows that will be returned based on
103453 ** an IN constraint where the right-hand side of the IN operator
103454 ** is a list of values. Example:
103455 **
@@ -103114,64 +103468,29 @@
103468 Parse *pParse, /* Parsing & code generating context */
103469 Index *p, /* The index whose left-most column is pTerm */
103470 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
103471 double *pnRow /* Write the revised row estimate here */
103472 ){
103473 int rc = SQLITE_OK; /* Subfunction return code */
103474 double nEst; /* Number of rows for a single term */
103475 double nRowEst = (double)0; /* New estimate of the number of rows */
103476 int i; /* Loop counter */
 
 
 
 
 
 
 
103477
103478 assert( p->aSample!=0 );
103479 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
103480 nEst = p->aiRowEst[0];
103481 rc = whereEqualScanEst(pParse, p, pList->a[i].pExpr, &nEst);
103482 nRowEst += nEst;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103483 }
103484 if( rc==SQLITE_OK ){
 
 
 
 
 
 
 
 
 
103485 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
103486 *pnRow = nRowEst;
103487 WHERETRACE(("IN row estimate: est=%g\n", nRowEst));
 
103488 }
 
103489 return rc;
103490 }
103491 #endif /* defined(SQLITE_ENABLE_STAT3) */
103492
103493
103494 /*
103495 ** Find the best query plan for accessing a particular table. Write the
103496 ** best query plan and its cost into the WhereCost object supplied as the
@@ -103214,11 +103533,11 @@
103533 Index *pProbe; /* An index we are evaluating */
103534 Index *pIdx; /* Copy of pProbe, or zero for IPK index */
103535 int eqTermMask; /* Current mask of valid equality operators */
103536 int idxEqTermMask; /* Index mask of valid equality operators */
103537 Index sPk; /* A fake index object for the primary key */
103538 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
103539 int aiColumnPk = -1; /* The aColumn[] value for the sPk index */
103540 int wsFlagMask; /* Allowed flags in pCost->plan.wsFlag */
103541
103542 /* Initialize the cost to a worst-case value */
103543 memset(pCost, 0, sizeof(*pCost));
@@ -103269,14 +103588,14 @@
103588 }
103589
103590 /* Loop over all indices looking for the best one to use
103591 */
103592 for(; pProbe; pIdx=pProbe=pProbe->pNext){
103593 const tRowcnt * const aiRowEst = pProbe->aiRowEst;
103594 double cost; /* Cost of using pProbe */
103595 double nRow; /* Estimated number of rows in result set */
103596 double log10N = (double)1; /* base-10 logarithm of nRow (inexact) */
103597 int rev; /* True to scan in reverse order */
103598 int wsFlags = 0;
103599 Bitmask used = 0;
103600
103601 /* The following variables are populated based on the properties of
@@ -103312,18 +103631,16 @@
103631 ** Set to true if there was at least one "x IN (SELECT ...)" term used
103632 ** in determining the value of nInMul. Note that the RHS of the
103633 ** IN operator must be a SELECT, not a value list, for this variable
103634 ** to be true.
103635 **
103636 ** rangeDiv:
103637 ** An estimate of a divisor by which to reduce the search space due
103638 ** to inequality constraints. In the absence of sqlite_stat3 ANALYZE
103639 ** data, a single inequality reduces the search space to 1/4rd its
103640 ** original size (rangeDiv==4). Two inequalities reduce the search
103641 ** space to 1/16th of its original size (rangeDiv==16).
 
 
103642 **
103643 ** bSort:
103644 ** Boolean. True if there is an ORDER BY clause that will require an
103645 ** external sort (i.e. scanning the index being evaluated will not
103646 ** correctly order records).
@@ -103344,26 +103661,27 @@
103661 ** SELECT a, b, c FROM tbl WHERE a = 1;
103662 */
103663 int nEq; /* Number of == or IN terms matching index */
103664 int bInEst = 0; /* True if "x IN (SELECT...)" seen */
103665 int nInMul = 1; /* Number of distinct equalities to lookup */
103666 double rangeDiv = (double)1; /* Estimated reduction in search space */
103667 int nBound = 0; /* Number of range constraints seen */
103668 int bSort = !!pOrderBy; /* True if external sort required */
103669 int bDist = !!pDistinct; /* True if index cannot help with DISTINCT */
103670 int bLookup = 0; /* True if not a covering index */
103671 WhereTerm *pTerm; /* A single term of the WHERE clause */
103672 #ifdef SQLITE_ENABLE_STAT3
103673 WhereTerm *pFirstTerm = 0; /* First term matching the index */
103674 #endif
103675
103676 /* Determine the values of nEq and nInMul */
103677 for(nEq=0; nEq<pProbe->nColumn; nEq++){
103678 int j = pProbe->aiColumn[nEq];
103679 pTerm = findTerm(pWC, iCur, j, notReady, eqTermMask, pIdx);
103680 if( pTerm==0 ) break;
103681 wsFlags |= (WHERE_COLUMN_EQ|WHERE_ROWID_EQ);
103682 testcase( pTerm->pWC!=pWC );
103683 if( pTerm->eOperator & WO_IN ){
103684 Expr *pExpr = pTerm->pExpr;
103685 wsFlags |= WHERE_COLUMN_IN;
103686 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
103687 /* "x IN (SELECT ...)": Assume the SELECT returns 25 rows */
@@ -103374,32 +103692,34 @@
103692 nInMul *= pExpr->x.pList->nExpr;
103693 }
103694 }else if( pTerm->eOperator & WO_ISNULL ){
103695 wsFlags |= WHERE_COLUMN_NULL;
103696 }
103697 #ifdef SQLITE_ENABLE_STAT3
103698 if( nEq==0 && pProbe->aSample ) pFirstTerm = pTerm;
103699 #endif
103700 used |= pTerm->prereqRight;
103701 }
103702
103703 /* Determine the value of rangeDiv */
103704 if( nEq<pProbe->nColumn && pProbe->bUnordered==0 ){
103705 int j = pProbe->aiColumn[nEq];
103706 if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pIdx) ){
103707 WhereTerm *pTop = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pIdx);
103708 WhereTerm *pBtm = findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pIdx);
103709 whereRangeScanEst(pParse, pProbe, nEq, pBtm, pTop, &rangeDiv);
103710 if( pTop ){
103711 nBound = 1;
103712 wsFlags |= WHERE_TOP_LIMIT;
103713 used |= pTop->prereqRight;
103714 testcase( pTop->pWC!=pWC );
103715 }
103716 if( pBtm ){
103717 nBound++;
103718 wsFlags |= WHERE_BTM_LIMIT;
103719 used |= pBtm->prereqRight;
103720 testcase( pBtm->pWC!=pWC );
103721 }
103722 wsFlags |= (WHERE_COLUMN_RANGE|WHERE_ROWID_RANGE);
103723 }
103724 }else if( pProbe->onError!=OE_None ){
103725 testcase( wsFlags & WHERE_COLUMN_IN );
@@ -103458,32 +103778,34 @@
103778 if( bInEst && nRow*2>aiRowEst[0] ){
103779 nRow = aiRowEst[0]/2;
103780 nInMul = (int)(nRow / aiRowEst[nEq]);
103781 }
103782
103783 #ifdef SQLITE_ENABLE_STAT3
103784 /* If the constraint is of the form x=VALUE or x IN (E1,E2,...)
103785 ** and we do not think that values of x are unique and if histogram
103786 ** data is available for column x, then it might be possible
103787 ** to get a better estimate on the number of rows based on
103788 ** VALUE and how common that value is according to the histogram.
103789 */
103790 if( nRow>(double)1 && nEq==1 && pFirstTerm!=0 && aiRowEst[1]>1 ){
103791 assert( (pFirstTerm->eOperator & (WO_EQ|WO_ISNULL|WO_IN))!=0 );
103792 if( pFirstTerm->eOperator & (WO_EQ|WO_ISNULL) ){
103793 testcase( pFirstTerm->eOperator==WO_EQ );
103794 testcase( pFirstTerm->eOperator==WO_ISNULL );
103795 whereEqualScanEst(pParse, pProbe, pFirstTerm->pExpr->pRight, &nRow);
103796 }else if( bInEst==0 ){
103797 assert( pFirstTerm->eOperator==WO_IN );
103798 whereInScanEst(pParse, pProbe, pFirstTerm->pExpr->x.pList, &nRow);
103799 }
103800 }
103801 #endif /* SQLITE_ENABLE_STAT3 */
103802
103803 /* Adjust the number of output rows and downward to reflect rows
103804 ** that are excluded by range constraints.
103805 */
103806 nRow = nRow/rangeDiv;
103807 if( nRow<1 ) nRow = 1;
103808
103809 /* Experiments run on real SQLite databases show that the time needed
103810 ** to do a binary search to locate a row in a table or index is roughly
103811 ** log10(N) times the time to move from one row to the next row within
@@ -103608,14 +103930,14 @@
103930 if( nRow<2 ) nRow = 2;
103931 }
103932
103933
103934 WHERETRACE((
103935 "%s(%s): nEq=%d nInMul=%d rangeDiv=%d bSort=%d bLookup=%d wsFlags=0x%x\n"
103936 " notReady=0x%llx log10N=%.1f nRow=%.1f cost=%.1f used=0x%llx\n",
103937 pSrc->pTab->zName, (pIdx ? pIdx->zName : "ipk"),
103938 nEq, nInMul, (int)rangeDiv, bSort, bLookup, wsFlags,
103939 notReady, log10N, nRow, cost, used
103940 ));
103941
103942 /* If this index is the best we have seen so far, then record this
103943 ** index and its cost in the pCost structure.
@@ -104115,11 +104437,12 @@
104437 */
104438 static Bitmask codeOneLoopStart(
104439 WhereInfo *pWInfo, /* Complete information about the WHERE clause */
104440 int iLevel, /* Which level of pWInfo->a[] should be coded */
104441 u16 wctrlFlags, /* One of the WHERE_* flags defined in sqliteInt.h */
104442 Bitmask notReady, /* Which tables are currently available */
104443 Expr *pWhere /* Complete WHERE clause */
104444 ){
104445 int j, k; /* Loop counters */
104446 int iCur; /* The VDBE cursor for the table */
104447 int addrNxt; /* Where to jump to continue with the next IN case */
104448 int omitTable; /* True if we use the index only */
@@ -104597,11 +104920,12 @@
104920 int regRowset = 0; /* Register for RowSet object */
104921 int regRowid = 0; /* Register holding rowid */
104922 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
104923 int iRetInit; /* Address of regReturn init */
104924 int untestedTerms = 0; /* Some terms not completely tested */
104925 int ii; /* Loop counter */
104926 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
104927
104928 pTerm = pLevel->plan.u.pTerm;
104929 assert( pTerm!=0 );
104930 assert( pTerm->eOperator==WO_OR );
104931 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -104646,18 +104970,33 @@
104970 regRowset = ++pParse->nMem;
104971 regRowid = ++pParse->nMem;
104972 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
104973 }
104974 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
104975
104976 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
104977 ** Then for every term xN, evaluate as the subexpression: xN AND z
104978 ** That way, terms in y that are factored into the disjunction will
104979 ** be picked up by the recursive calls to sqlite3WhereBegin() below.
104980 */
104981 if( pWC->nTerm>1 ){
104982 pAndExpr = sqlite3ExprAlloc(pParse->db, TK_AND, 0, 0);
104983 pAndExpr->pRight = pWhere;
104984 }
104985
104986 for(ii=0; ii<pOrWc->nTerm; ii++){
104987 WhereTerm *pOrTerm = &pOrWc->a[ii];
104988 if( pOrTerm->leftCursor==iCur || pOrTerm->eOperator==WO_AND ){
104989 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
104990 Expr *pOrExpr = pOrTerm->pExpr;
104991 if( pAndExpr ){
104992 pAndExpr->pLeft = pOrExpr;
104993 pOrExpr = pAndExpr;
104994 }
104995 /* Loop through table entries that match term pOrTerm. */
104996 pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
104997 WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
104998 WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY);
104999 if( pSubWInfo ){
105000 explainOneScan(
105001 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
105002 );
@@ -104681,10 +105020,11 @@
105020 /* Finish the loop through table entries that match term pOrTerm. */
105021 sqlite3WhereEnd(pSubWInfo);
105022 }
105023 }
105024 }
105025 sqlite3DbFree(pParse->db, pAndExpr);
105026 sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
105027 sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
105028 sqlite3VdbeResolveLabel(v, iLoopBody);
105029
105030 if( pWInfo->nLevel>1 ) sqlite3StackFree(pParse->db, pOrTab);
@@ -104962,11 +105302,11 @@
105302
105303 /* Split the WHERE clause into separate subexpressions where each
105304 ** subexpression is separated by an AND operator.
105305 */
105306 initMaskSet(pMaskSet);
105307 whereClauseInit(pWC, pParse, pMaskSet, wctrlFlags);
105308 sqlite3ExprCodeConstants(pParse, pWhere);
105309 whereSplit(pWC, pWhere, TK_AND); /* IMP: R-15842-53296 */
105310
105311 /* Special case: a WHERE clause that is constant. Evaluate the
105312 ** expression and either jump over all of the code or fall thru.
@@ -105201,11 +105541,12 @@
105541 assert( bestJ>=0 );
105542 assert( notReady & getMask(pMaskSet, pTabList->a[bestJ].iCursor) );
105543 WHERETRACE(("*** Optimizer selects table %d for loop %d"
105544 " with cost=%g and nRow=%g\n",
105545 bestJ, pLevel-pWInfo->a, bestPlan.rCost, bestPlan.plan.nRow));
105546 /* The ALWAYS() that follows was added to hush up clang scan-build */
105547 if( (bestPlan.plan.wsFlags & WHERE_ORDERBY)!=0 && ALWAYS(ppOrderBy) ){
105548 *ppOrderBy = 0;
105549 }
105550 if( (bestPlan.plan.wsFlags & WHERE_DISTINCT)!=0 ){
105551 assert( pWInfo->eDistinct==0 );
105552 pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
@@ -105290,11 +105631,11 @@
105631 int iCur = pTabItem->iCursor;
105632 sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
105633 }else
105634 #endif
105635 if( (pLevel->plan.wsFlags & WHERE_IDX_ONLY)==0
105636 && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
105637 int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead;
105638 sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
105639 testcase( pTab->nCol==BMS-1 );
105640 testcase( pTab->nCol==BMS );
105641 if( !pWInfo->okOnePass && pTab->nCol<BMS ){
@@ -105335,11 +105676,11 @@
105676 */
105677 notReady = ~(Bitmask)0;
105678 for(i=0; i<nTabList; i++){
105679 pLevel = &pWInfo->a[i];
105680 explainOneScan(pParse, pTabList, pLevel, i, pLevel->iFrom, wctrlFlags);
105681 notReady = codeOneLoopStart(pWInfo, i, wctrlFlags, notReady, pWhere);
105682 pWInfo->iContinue = pLevel->addrCont;
105683 }
105684
105685 #ifdef SQLITE_TEST /* For testing and debugging use only */
105686 /* Record in the query plan information about the current table
@@ -105470,11 +105811,11 @@
105811 struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
105812 Table *pTab = pTabItem->pTab;
105813 assert( pTab!=0 );
105814 if( (pTab->tabFlags & TF_Ephemeral)==0
105815 && pTab->pSelect==0
105816 && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
105817 ){
105818 int ws = pLevel->plan.wsFlags;
105819 if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
105820 sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
105821 }
@@ -108817,11 +109158,13 @@
109158 sqlite3ParserTOKENTYPE yyminor /* The value for the token */
109159 sqlite3ParserARG_PDECL /* Optional %extra_argument parameter */
109160 ){
109161 YYMINORTYPE yyminorunion;
109162 int yyact; /* The parser action. */
109163 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
109164 int yyendofinput; /* True if we are at the end of input */
109165 #endif
109166 #ifdef YYERRORSYMBOL
109167 int yyerrorhit = 0; /* True if yymajor has invoked an error */
109168 #endif
109169 yyParser *yypParser; /* The parser */
109170
@@ -108840,11 +109183,13 @@
109183 yypParser->yyerrcnt = -1;
109184 yypParser->yystack[0].stateno = 0;
109185 yypParser->yystack[0].major = 0;
109186 }
109187 yyminorunion.yy0 = yyminor;
109188 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
109189 yyendofinput = (yymajor==0);
109190 #endif
109191 sqlite3ParserARG_STORE;
109192
109193 #ifndef NDEBUG
109194 if( yyTraceFILE ){
109195 fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
@@ -108852,11 +109197,10 @@
109197 #endif
109198
109199 do{
109200 yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
109201 if( yyact<YYNSTATE ){
 
109202 yy_shift(yypParser,yyact,yymajor,&yyminorunion);
109203 yypParser->yyerrcnt--;
109204 yymajor = YYNOCODE;
109205 }else if( yyact < YYNSTATE + YYNRULE ){
109206 yy_reduce(yypParser,yyact-YYNSTATE);
@@ -110244,11 +110588,11 @@
110588 **
110589 ** * Recursive calls to this routine from thread X return immediately
110590 ** without blocking.
110591 */
110592 SQLITE_API int sqlite3_initialize(void){
110593 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
110594 int rc; /* Result code */
110595
110596 #ifdef SQLITE_OMIT_WSD
110597 rc = sqlite3_wsd_init(4096, 24);
110598 if( rc!=SQLITE_OK ){
@@ -110278,11 +110622,11 @@
110622 ** This operation is protected by the STATIC_MASTER mutex. Note that
110623 ** MutexAlloc() is called for a static mutex prior to initializing the
110624 ** malloc subsystem - this implies that the allocation of a static
110625 ** mutex must not require support from the malloc subsystem.
110626 */
110627 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
110628 sqlite3_mutex_enter(pMaster);
110629 sqlite3GlobalConfig.isMutexInit = 1;
110630 if( !sqlite3GlobalConfig.isMallocInit ){
110631 rc = sqlite3MallocInit();
110632 }
@@ -111352,17 +111696,17 @@
111696 sqlite3 *db,
111697 const char *zName,
111698 int nArg
111699 ){
111700 int nName = sqlite3Strlen30(zName);
111701 int rc = SQLITE_OK;
111702 sqlite3_mutex_enter(db->mutex);
111703 if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
111704 rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
111705 0, sqlite3InvalidFunction, 0, 0, 0);
111706 }
111707 rc = sqlite3ApiExit(db, rc);
111708 sqlite3_mutex_leave(db->mutex);
111709 return rc;
111710 }
111711
111712 #ifndef SQLITE_OMIT_TRACE
@@ -112420,10 +112764,11 @@
112764 if( db ){
112765 assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
112766 sqlite3_mutex_leave(db->mutex);
112767 }
112768 rc = sqlite3_errcode(db);
112769 assert( db!=0 || rc==SQLITE_NOMEM );
112770 if( rc==SQLITE_NOMEM ){
112771 sqlite3_close(db);
112772 db = 0;
112773 }else if( rc!=SQLITE_OK ){
112774 db->magic = SQLITE_MAGIC_SICK;
@@ -114148,10 +114493,17 @@
114493 #else
114494 # define TESTONLY(X)
114495 #endif
114496
114497 #endif /* SQLITE_AMALGAMATION */
114498
114499 #ifdef SQLITE_DEBUG
114500 SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
114501 # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
114502 #else
114503 # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
114504 #endif
114505
114506 typedef struct Fts3Table Fts3Table;
114507 typedef struct Fts3Cursor Fts3Cursor;
114508 typedef struct Fts3Expr Fts3Expr;
114509 typedef struct Fts3Phrase Fts3Phrase;
@@ -114649,11 +115001,11 @@
115001 char **pp,
115002 char *pStart,
115003 sqlite3_int64 *pVal
115004 ){
115005 sqlite3_int64 iVal;
115006 char *p;
115007
115008 /* Pointer p now points at the first byte past the varint we are
115009 ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
115010 ** clear on character p[-1]. */
115011 for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
@@ -115050,11 +115402,11 @@
115402 ** the output value undefined. Otherwise SQLITE_OK is returned.
115403 **
115404 ** This function is used when parsing the "prefix=" FTS4 parameter.
115405 */
115406 static int fts3GobbleInt(const char **pp, int *pnOut){
115407 const char *p; /* Iterator pointer */
115408 int nInt = 0; /* Output value */
115409
115410 for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
115411 nInt = nInt * 10 + (p[0] - '0');
115412 }
@@ -115549,11 +115901,11 @@
115901 if( rc==SQLITE_OK ){
115902 /* If no row was found and no error has occured, then the %_content
115903 ** table is missing a row that is present in the full-text index.
115904 ** The data structures are corrupt.
115905 */
115906 rc = FTS_CORRUPT_VTAB;
115907 }
115908 pCsr->isEof = 1;
115909 if( pContext ){
115910 sqlite3_result_error_code(pContext, rc);
115911 }
@@ -115609,11 +115961,11 @@
115961 ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
115962 */
115963 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115964 zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
115965 if( zCsr>zEnd ){
115966 return FTS_CORRUPT_VTAB;
115967 }
115968
115969 while( zCsr<zEnd && (piFirst || piLast) ){
115970 int cmp; /* memcmp() result */
115971 int nSuffix; /* Size of term suffix */
@@ -115627,11 +115979,11 @@
115979 }
115980 isFirstTerm = 0;
115981 zCsr += sqlite3Fts3GetVarint32(zCsr, &nSuffix);
115982
115983 if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
115984 rc = FTS_CORRUPT_VTAB;
115985 goto finish_scan;
115986 }
115987 if( nPrefix+nSuffix>nAlloc ){
115988 char *zNew;
115989 nAlloc = (nPrefix+nSuffix) * 2;
@@ -115640,10 +115992,11 @@
115992 rc = SQLITE_NOMEM;
115993 goto finish_scan;
115994 }
115995 zBuffer = zNew;
115996 }
115997 assert( zBuffer );
115998 memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
115999 nBuffer = nPrefix + nSuffix;
116000 zCsr += nSuffix;
116001
116002 /* Compare the term we are searching for with the term just loaded from
@@ -117076,11 +117429,11 @@
117429 ** moves *ppPoslist so that it instead points to the first byte of the
117430 ** same position list.
117431 */
117432 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
117433 char *p = &(*ppPoslist)[-2];
117434 char c = 0;
117435
117436 while( p>pStart && (c=*p--)==0 );
117437 while( p>pStart && (*p & 0x80) | c ){
117438 c = *p--;
117439 }
@@ -118070,11 +118423,11 @@
118423 while( a<pEnd ){
118424 a += sqlite3Fts3GetVarint(a, &nByte);
118425 }
118426 if( nDoc==0 || nByte==0 ){
118427 sqlite3_reset(pStmt);
118428 return FTS_CORRUPT_VTAB;
118429 }
118430
118431 pCsr->nDoc = nDoc;
118432 pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
118433 assert( pCsr->nRowAvg>0 );
@@ -118546,12 +118899,15 @@
118899 }
118900
118901 aPoslist = pExpr->pRight->pPhrase->doclist.pList;
118902 nToken = pExpr->pRight->pPhrase->nToken;
118903 for(p=pExpr->pLeft; p && res; p=p->pLeft){
118904 int nNear;
118905 Fts3Phrase *pPhrase;
118906 assert( p->pParent && p->pParent->pLeft==p );
118907 nNear = p->pParent->nNear;
118908 pPhrase = (
118909 p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
118910 );
118911 res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
118912 }
118913 }
@@ -119037,10 +119393,19 @@
119393 pPhrase->aToken[i].pSegcsr = 0;
119394 }
119395 }
119396 }
119397
119398 /*
119399 ** Return SQLITE_CORRUPT_VTAB.
119400 */
119401 #ifdef SQLITE_DEBUG
119402 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
119403 return SQLITE_CORRUPT_VTAB;
119404 }
119405 #endif
119406
119407 #if !SQLITE_CORE
119408 /*
119409 ** Initialize API pointer table, if required.
119410 */
119411 SQLITE_API int sqlite3_extension_init(
@@ -119834,12 +120199,16 @@
120199 p->pPhrase = (Fts3Phrase *)&p[1];
120200 p->pPhrase->iColumn = pParse->iDefaultCol;
120201 p->pPhrase->nToken = nToken;
120202
120203 zBuf = (char *)&p->pPhrase->aToken[nToken];
120204 if( zTemp ){
120205 memcpy(zBuf, zTemp, nTemp);
120206 sqlite3_free(zTemp);
120207 }else{
120208 assert( nTemp==0 );
120209 }
120210
120211 for(jj=0; jj<p->pPhrase->nToken; jj++){
120212 p->pPhrase->aToken[jj].z = zBuf;
120213 zBuf += p->pPhrase->aToken[jj].n;
120214 }
@@ -122594,11 +122963,11 @@
122963 sqlite3_bind_int64(pStmt, 1, iDocid);
122964 }
122965 rc = sqlite3_step(pStmt);
122966 if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
122967 rc = sqlite3_reset(pStmt);
122968 if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
122969 pStmt = 0;
122970 }else{
122971 rc = SQLITE_OK;
122972 }
122973 }
@@ -123398,11 +123767,11 @@
123767 pNext += sqlite3Fts3GetVarint32(pNext, &nPrefix);
123768 pNext += sqlite3Fts3GetVarint32(pNext, &nSuffix);
123769 if( nPrefix<0 || nSuffix<=0
123770 || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
123771 ){
123772 return FTS_CORRUPT_VTAB;
123773 }
123774
123775 if( nPrefix+nSuffix>pReader->nTermAlloc ){
123776 int nNew = (nPrefix+nSuffix)*2;
123777 char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
@@ -123428,11 +123797,11 @@
123797 ** of these statements is untrue, then the data structure is corrupt.
123798 */
123799 if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
123800 || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
123801 ){
123802 return FTS_CORRUPT_VTAB;
123803 }
123804 return SQLITE_OK;
123805 }
123806
123807 /*
@@ -125382,11 +125751,10 @@
125751 sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
125752 ){
125753 Fts3Table *p = (Fts3Table *)pVtab;
125754 int rc = SQLITE_OK; /* Return Code */
125755 int isRemove = 0; /* True for an UPDATE or DELETE */
 
125756 u32 *aSzIns = 0; /* Sizes of inserted documents */
125757 u32 *aSzDel; /* Sizes of deleted documents */
125758 int nChng = 0; /* Net change in number of documents */
125759 int bInsertDone = 0;
125760
@@ -125465,23 +125833,23 @@
125833 /* If this is a DELETE or UPDATE operation, remove the old record. */
125834 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
125835 assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
125836 rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
125837 isRemove = 1;
 
125838 }
125839
125840 /* If this is an INSERT or UPDATE operation, insert the new record. */
125841 if( nArg>1 && rc==SQLITE_OK ){
125842 if( bInsertDone==0 ){
125843 rc = fts3InsertData(p, apVal, pRowid);
125844 if( rc==SQLITE_CONSTRAINT ) rc = FTS_CORRUPT_VTAB;
125845 }
125846 if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
125847 rc = fts3PendingTermsDocid(p, *pRowid);
125848 }
125849 if( rc==SQLITE_OK ){
125850 assert( p->iPrevDocid==*pRowid );
125851 rc = fts3InsertTerms(p, apVal, aSzIns);
125852 }
125853 if( p->bHasDocsize ){
125854 fts3InsertDocsize(&rc, p, aSzIns);
125855 }
@@ -126371,11 +126739,11 @@
126739 pStmt = *ppStmt;
126740 assert( sqlite3_data_count(pStmt)==1 );
126741
126742 a = sqlite3_column_blob(pStmt, 0);
126743 a += sqlite3Fts3GetVarint(a, &nDoc);
126744 if( nDoc==0 ) return FTS_CORRUPT_VTAB;
126745 *pnDoc = (u32)nDoc;
126746
126747 if( paLen ) *paLen = a;
126748 return SQLITE_OK;
126749 }
@@ -126950,11 +127318,11 @@
127318 sqlite3_snprintf(sizeof(aBuffer), aBuffer,
127319 "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
127320 );
127321 rc = fts3StringAppend(&res, aBuffer, -1);
127322 }else if( rc==SQLITE_DONE ){
127323 rc = FTS_CORRUPT_VTAB;
127324 }
127325 }
127326 }
127327 if( rc==SQLITE_DONE ){
127328 rc = SQLITE_OK;
@@ -128291,11 +128659,12 @@
128659 pCsr->nConstraint = argc;
128660 if( !pCsr->aConstraint ){
128661 rc = SQLITE_NOMEM;
128662 }else{
128663 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
128664 assert( (idxStr==0 && argc==0)
128665 || (idxStr && (int)strlen(idxStr)==argc*2) );
128666 for(ii=0; ii<argc; ii++){
128667 RtreeConstraint *p = &pCsr->aConstraint[ii];
128668 p->op = idxStr[ii*2];
128669 p->iCoord = idxStr[ii*2+1]-'a';
128670 if( p->op==RTREE_MATCH ){
@@ -128592,11 +128961,14 @@
128961 int iCell;
128962 sqlite3_int64 iBest = 0;
128963
128964 float fMinGrowth = 0.0;
128965 float fMinArea = 0.0;
128966 #if VARIANT_RSTARTREE_CHOOSESUBTREE
128967 float fMinOverlap = 0.0;
128968 float overlap;
128969 #endif
128970
128971 int nCell = NCELL(pNode);
128972 RtreeCell cell;
128973 RtreeNode *pChild;
128974
@@ -128624,33 +128996,34 @@
128996 */
128997 for(iCell=0; iCell<nCell; iCell++){
128998 int bBest = 0;
128999 float growth;
129000 float area;
 
129001 nodeGetCell(pRtree, pNode, iCell, &cell);
129002 growth = cellGrowth(pRtree, &cell, pCell);
129003 area = cellArea(pRtree, &cell);
129004
129005 #if VARIANT_RSTARTREE_CHOOSESUBTREE
129006 if( ii==(pRtree->iDepth-1) ){
129007 overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
129008 }else{
129009 overlap = 0.0;
129010 }
129011 if( (iCell==0)
129012 || (overlap<fMinOverlap)
129013 || (overlap==fMinOverlap && growth<fMinGrowth)
129014 || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
129015 ){
129016 bBest = 1;
129017 fMinOverlap = overlap;
129018 }
129019 #else
129020 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
129021 bBest = 1;
129022 }
129023 #endif
129024 if( bBest ){
 
129025 fMinGrowth = growth;
129026 fMinArea = area;
129027 iBest = cell.iRowid;
129028 }
129029 }
129030
+46 -9
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105105
**
106106
** See also: [sqlite3_libversion()],
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110
-#define SQLITE_VERSION "3.7.8"
111
-#define SQLITE_VERSION_NUMBER 3007008
112
-#define SQLITE_SOURCE_ID "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
110
+#define SQLITE_VERSION "3.7.9"
111
+#define SQLITE_VERSION_NUMBER 3007009
112
+#define SQLITE_SOURCE_ID "2011-10-15 00:16:30 39408702a989f907261c298bf0947f3e68bd10fe"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -769,11 +769,15 @@
769769
** in order for the database to be readable. The fourth parameter to
770770
** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771771
** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772772
** WAL mode. If the integer is -1, then it is overwritten with the current
773773
** WAL persistence setting.
774
-**
774
+**
775
+** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
776
+** a write transaction to indicate that, unless it is rolled back for some
777
+** reason, the entire database file will be overwritten by the current
778
+** transaction. This is used by VACUUM operations.
775779
*/
776780
#define SQLITE_FCNTL_LOCKSTATE 1
777781
#define SQLITE_GET_LOCKPROXYFILE 2
778782
#define SQLITE_SET_LOCKPROXYFILE 3
779783
#define SQLITE_LAST_ERRNO 4
@@ -781,10 +785,11 @@
781785
#define SQLITE_FCNTL_CHUNK_SIZE 6
782786
#define SQLITE_FCNTL_FILE_POINTER 7
783787
#define SQLITE_FCNTL_SYNC_OMITTED 8
784788
#define SQLITE_FCNTL_WIN32_AV_RETRY 9
785789
#define SQLITE_FCNTL_PERSIST_WAL 10
790
+#define SQLITE_FCNTL_OVERWRITE 11
786791
787792
/*
788793
** CAPI3REF: Mutex Handle
789794
**
790795
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2797,11 +2802,12 @@
27972802
** zSql string ends at either the first '\000' or '\u0000' character or
27982803
** the nByte-th byte, whichever comes first. If the caller knows
27992804
** that the supplied string is nul-terminated, then there is a small
28002805
** performance advantage to be gained by passing an nByte parameter that
28012806
** is equal to the number of bytes in the input string <i>including</i>
2802
-** the nul-terminator bytes.
2807
+** the nul-terminator bytes as this saves SQLite from having to
2808
+** make a copy of the input string.
28032809
**
28042810
** ^If pzTail is not NULL then *pzTail is made to point to the first byte
28052811
** past the end of the first SQL statement in zSql. These routines only
28062812
** compile the first statement in zSql, so *pzTail is left pointing to
28072813
** what remains uncompiled.
@@ -2848,11 +2854,11 @@
28482854
** a schema change, on the first [sqlite3_step()] call following any change
28492855
** to the [sqlite3_bind_text | bindings] of that [parameter].
28502856
** ^The specific value of WHERE-clause [parameter] might influence the
28512857
** choice of query plan if the parameter is the left-hand side of a [LIKE]
28522858
** or [GLOB] operator or if the parameter is compared to an indexed column
2853
-** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2859
+** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
28542860
** the
28552861
** </li>
28562862
** </ol>
28572863
*/
28582864
SQLITE_API int sqlite3_prepare(
@@ -3018,10 +3024,17 @@
30183024
** ^(In those routines that have a fourth argument, its value is the
30193025
** number of bytes in the parameter. To be clear: the value is the
30203026
** number of <u>bytes</u> in the value, not the number of characters.)^
30213027
** ^If the fourth parameter is negative, the length of the string is
30223028
** the number of bytes up to the first zero terminator.
3029
+** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3030
+** or sqlite3_bind_text16() then that parameter must be the byte offset
3031
+** where the NUL terminator would occur assuming the string were NUL
3032
+** terminated. If any NUL characters occur at byte offsets less than
3033
+** the value of the fourth parameter then the resulting string value will
3034
+** contain embedded NULs. The result of expressions involving strings
3035
+** with embedded NULs is undefined.
30233036
**
30243037
** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
30253038
** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
30263039
** string after SQLite has finished with it. ^The destructor is called
30273040
** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3351,10 +3364,16 @@
33513364
** current row of the result set of [prepared statement] P.
33523365
** ^If prepared statement P does not have results ready to return
33533366
** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
33543367
** interfaces) then sqlite3_data_count(P) returns 0.
33553368
** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3369
+** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3370
+** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
3371
+** will return non-zero if previous call to [sqlite3_step](P) returned
3372
+** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3373
+** where it always returns zero since each step of that multi-step
3374
+** pragma returns 0 columns of data.
33563375
**
33573376
** See also: [sqlite3_column_count()]
33583377
*/
33593378
SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
33603379
@@ -4030,11 +4049,16 @@
40304049
** is negative, then SQLite takes result text from the 2nd parameter
40314050
** through the first zero character.
40324051
** ^If the 3rd parameter to the sqlite3_result_text* interfaces
40334052
** is non-negative, then as many bytes (not characters) of the text
40344053
** pointed to by the 2nd parameter are taken as the application-defined
4035
-** function result.
4054
+** function result. If the 3rd parameter is non-negative, then it
4055
+** must be the byte offset into the string where the NUL terminator would
4056
+** appear if the string where NUL terminated. If any NUL characters occur
4057
+** in the string at a byte offset that is less than the value of the 3rd
4058
+** parameter, then the resulting string will contain embedded NULs and the
4059
+** result of expressions operating on strings with embedded NULs is undefined.
40364060
** ^If the 4th parameter to the sqlite3_result_text* interfaces
40374061
** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
40384062
** function as the destructor on the text or BLOB result when it has
40394063
** finished using that result.
40404064
** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -5813,20 +5837,34 @@
58135837
** <dd>This parameter returns the approximate number of of bytes of heap
58145838
** and lookaside memory used by all prepared statements associated with
58155839
** the database connection.)^
58165840
** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
58175841
** </dd>
5842
+**
5843
+** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
5844
+** <dd>This parameter returns the number of pager cache hits that have
5845
+** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
5846
+** is always 0.
5847
+** </dd>
5848
+**
5849
+** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
5850
+** <dd>This parameter returns the number of pager cache misses that have
5851
+** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
5852
+** is always 0.
5853
+** </dd>
58185854
** </dl>
58195855
*/
58205856
#define SQLITE_DBSTATUS_LOOKASIDE_USED 0
58215857
#define SQLITE_DBSTATUS_CACHE_USED 1
58225858
#define SQLITE_DBSTATUS_SCHEMA_USED 2
58235859
#define SQLITE_DBSTATUS_STMT_USED 3
58245860
#define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
58255861
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
58265862
#define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
5827
-#define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */
5863
+#define SQLITE_DBSTATUS_CACHE_HIT 7
5864
+#define SQLITE_DBSTATUS_CACHE_MISS 8
5865
+#define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
58285866
58295867
58305868
/*
58315869
** CAPI3REF: Prepared Statement Status
58325870
**
@@ -5876,11 +5914,10 @@
58765914
** <dd>^This is the number of rows inserted into transient indices that
58775915
** were created automatically in order to help joins run faster.
58785916
** A non-zero value in this counter may indicate an opportunity to
58795917
** improvement performance by adding permanent indices that do not
58805918
** need to be reinitialized each time the statement is run.</dd>
5881
-**
58825919
** </dl>
58835920
*/
58845921
#define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
58855922
#define SQLITE_STMTSTATUS_SORT 2
58865923
#define SQLITE_STMTSTATUS_AUTOINDEX 3
58875924
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.8"
111 #define SQLITE_VERSION_NUMBER 3007008
112 #define SQLITE_SOURCE_ID "2011-09-19 14:49:19 3e0da808d2f5b4d12046e05980ca04578f581177"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -769,11 +769,15 @@
769 ** in order for the database to be readable. The fourth parameter to
770 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772 ** WAL mode. If the integer is -1, then it is overwritten with the current
773 ** WAL persistence setting.
774 **
 
 
 
 
775 */
776 #define SQLITE_FCNTL_LOCKSTATE 1
777 #define SQLITE_GET_LOCKPROXYFILE 2
778 #define SQLITE_SET_LOCKPROXYFILE 3
779 #define SQLITE_LAST_ERRNO 4
@@ -781,10 +785,11 @@
781 #define SQLITE_FCNTL_CHUNK_SIZE 6
782 #define SQLITE_FCNTL_FILE_POINTER 7
783 #define SQLITE_FCNTL_SYNC_OMITTED 8
784 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
785 #define SQLITE_FCNTL_PERSIST_WAL 10
 
786
787 /*
788 ** CAPI3REF: Mutex Handle
789 **
790 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2797,11 +2802,12 @@
2797 ** zSql string ends at either the first '\000' or '\u0000' character or
2798 ** the nByte-th byte, whichever comes first. If the caller knows
2799 ** that the supplied string is nul-terminated, then there is a small
2800 ** performance advantage to be gained by passing an nByte parameter that
2801 ** is equal to the number of bytes in the input string <i>including</i>
2802 ** the nul-terminator bytes.
 
2803 **
2804 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
2805 ** past the end of the first SQL statement in zSql. These routines only
2806 ** compile the first statement in zSql, so *pzTail is left pointing to
2807 ** what remains uncompiled.
@@ -2848,11 +2854,11 @@
2848 ** a schema change, on the first [sqlite3_step()] call following any change
2849 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2850 ** ^The specific value of WHERE-clause [parameter] might influence the
2851 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2852 ** or [GLOB] operator or if the parameter is compared to an indexed column
2853 ** and the [SQLITE_ENABLE_STAT2] compile-time option is enabled.
2854 ** the
2855 ** </li>
2856 ** </ol>
2857 */
2858 SQLITE_API int sqlite3_prepare(
@@ -3018,10 +3024,17 @@
3018 ** ^(In those routines that have a fourth argument, its value is the
3019 ** number of bytes in the parameter. To be clear: the value is the
3020 ** number of <u>bytes</u> in the value, not the number of characters.)^
3021 ** ^If the fourth parameter is negative, the length of the string is
3022 ** the number of bytes up to the first zero terminator.
 
 
 
 
 
 
 
3023 **
3024 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3025 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3026 ** string after SQLite has finished with it. ^The destructor is called
3027 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3351,10 +3364,16 @@
3351 ** current row of the result set of [prepared statement] P.
3352 ** ^If prepared statement P does not have results ready to return
3353 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3354 ** interfaces) then sqlite3_data_count(P) returns 0.
3355 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
 
 
 
 
 
 
3356 **
3357 ** See also: [sqlite3_column_count()]
3358 */
3359 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3360
@@ -4030,11 +4049,16 @@
4030 ** is negative, then SQLite takes result text from the 2nd parameter
4031 ** through the first zero character.
4032 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4033 ** is non-negative, then as many bytes (not characters) of the text
4034 ** pointed to by the 2nd parameter are taken as the application-defined
4035 ** function result.
 
 
 
 
 
4036 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4037 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4038 ** function as the destructor on the text or BLOB result when it has
4039 ** finished using that result.
4040 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -5813,20 +5837,34 @@
5813 ** <dd>This parameter returns the approximate number of of bytes of heap
5814 ** and lookaside memory used by all prepared statements associated with
5815 ** the database connection.)^
5816 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
5817 ** </dd>
 
 
 
 
 
 
 
 
 
 
 
 
5818 ** </dl>
5819 */
5820 #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
5821 #define SQLITE_DBSTATUS_CACHE_USED 1
5822 #define SQLITE_DBSTATUS_SCHEMA_USED 2
5823 #define SQLITE_DBSTATUS_STMT_USED 3
5824 #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
5825 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
5826 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
5827 #define SQLITE_DBSTATUS_MAX 6 /* Largest defined DBSTATUS */
 
 
5828
5829
5830 /*
5831 ** CAPI3REF: Prepared Statement Status
5832 **
@@ -5876,11 +5914,10 @@
5876 ** <dd>^This is the number of rows inserted into transient indices that
5877 ** were created automatically in order to help joins run faster.
5878 ** A non-zero value in this counter may indicate an opportunity to
5879 ** improvement performance by adding permanent indices that do not
5880 ** need to be reinitialized each time the statement is run.</dd>
5881 **
5882 ** </dl>
5883 */
5884 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
5885 #define SQLITE_STMTSTATUS_SORT 2
5886 #define SQLITE_STMTSTATUS_AUTOINDEX 3
5887
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -105,13 +105,13 @@
105 **
106 ** See also: [sqlite3_libversion()],
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.7.9"
111 #define SQLITE_VERSION_NUMBER 3007009
112 #define SQLITE_SOURCE_ID "2011-10-15 00:16:30 39408702a989f907261c298bf0947f3e68bd10fe"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -769,11 +769,15 @@
769 ** in order for the database to be readable. The fourth parameter to
770 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
771 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
772 ** WAL mode. If the integer is -1, then it is overwritten with the current
773 ** WAL persistence setting.
774 **
775 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
776 ** a write transaction to indicate that, unless it is rolled back for some
777 ** reason, the entire database file will be overwritten by the current
778 ** transaction. This is used by VACUUM operations.
779 */
780 #define SQLITE_FCNTL_LOCKSTATE 1
781 #define SQLITE_GET_LOCKPROXYFILE 2
782 #define SQLITE_SET_LOCKPROXYFILE 3
783 #define SQLITE_LAST_ERRNO 4
@@ -781,10 +785,11 @@
785 #define SQLITE_FCNTL_CHUNK_SIZE 6
786 #define SQLITE_FCNTL_FILE_POINTER 7
787 #define SQLITE_FCNTL_SYNC_OMITTED 8
788 #define SQLITE_FCNTL_WIN32_AV_RETRY 9
789 #define SQLITE_FCNTL_PERSIST_WAL 10
790 #define SQLITE_FCNTL_OVERWRITE 11
791
792 /*
793 ** CAPI3REF: Mutex Handle
794 **
795 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2797,11 +2802,12 @@
2802 ** zSql string ends at either the first '\000' or '\u0000' character or
2803 ** the nByte-th byte, whichever comes first. If the caller knows
2804 ** that the supplied string is nul-terminated, then there is a small
2805 ** performance advantage to be gained by passing an nByte parameter that
2806 ** is equal to the number of bytes in the input string <i>including</i>
2807 ** the nul-terminator bytes as this saves SQLite from having to
2808 ** make a copy of the input string.
2809 **
2810 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
2811 ** past the end of the first SQL statement in zSql. These routines only
2812 ** compile the first statement in zSql, so *pzTail is left pointing to
2813 ** what remains uncompiled.
@@ -2848,11 +2854,11 @@
2854 ** a schema change, on the first [sqlite3_step()] call following any change
2855 ** to the [sqlite3_bind_text | bindings] of that [parameter].
2856 ** ^The specific value of WHERE-clause [parameter] might influence the
2857 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
2858 ** or [GLOB] operator or if the parameter is compared to an indexed column
2859 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
2860 ** the
2861 ** </li>
2862 ** </ol>
2863 */
2864 SQLITE_API int sqlite3_prepare(
@@ -3018,10 +3024,17 @@
3024 ** ^(In those routines that have a fourth argument, its value is the
3025 ** number of bytes in the parameter. To be clear: the value is the
3026 ** number of <u>bytes</u> in the value, not the number of characters.)^
3027 ** ^If the fourth parameter is negative, the length of the string is
3028 ** the number of bytes up to the first zero terminator.
3029 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3030 ** or sqlite3_bind_text16() then that parameter must be the byte offset
3031 ** where the NUL terminator would occur assuming the string were NUL
3032 ** terminated. If any NUL characters occur at byte offsets less than
3033 ** the value of the fourth parameter then the resulting string value will
3034 ** contain embedded NULs. The result of expressions involving strings
3035 ** with embedded NULs is undefined.
3036 **
3037 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3038 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3039 ** string after SQLite has finished with it. ^The destructor is called
3040 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
@@ -3351,10 +3364,16 @@
3364 ** current row of the result set of [prepared statement] P.
3365 ** ^If prepared statement P does not have results ready to return
3366 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3367 ** interfaces) then sqlite3_data_count(P) returns 0.
3368 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3369 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3370 ** [sqlite3_step](P) returned [SQLITE_DONE]. ^The sqlite3_data_count(P)
3371 ** will return non-zero if previous call to [sqlite3_step](P) returned
3372 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3373 ** where it always returns zero since each step of that multi-step
3374 ** pragma returns 0 columns of data.
3375 **
3376 ** See also: [sqlite3_column_count()]
3377 */
3378 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3379
@@ -4030,11 +4049,16 @@
4049 ** is negative, then SQLite takes result text from the 2nd parameter
4050 ** through the first zero character.
4051 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4052 ** is non-negative, then as many bytes (not characters) of the text
4053 ** pointed to by the 2nd parameter are taken as the application-defined
4054 ** function result. If the 3rd parameter is non-negative, then it
4055 ** must be the byte offset into the string where the NUL terminator would
4056 ** appear if the string where NUL terminated. If any NUL characters occur
4057 ** in the string at a byte offset that is less than the value of the 3rd
4058 ** parameter, then the resulting string will contain embedded NULs and the
4059 ** result of expressions operating on strings with embedded NULs is undefined.
4060 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4061 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4062 ** function as the destructor on the text or BLOB result when it has
4063 ** finished using that result.
4064 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
@@ -5813,20 +5837,34 @@
5837 ** <dd>This parameter returns the approximate number of of bytes of heap
5838 ** and lookaside memory used by all prepared statements associated with
5839 ** the database connection.)^
5840 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
5841 ** </dd>
5842 **
5843 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
5844 ** <dd>This parameter returns the number of pager cache hits that have
5845 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
5846 ** is always 0.
5847 ** </dd>
5848 **
5849 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
5850 ** <dd>This parameter returns the number of pager cache misses that have
5851 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
5852 ** is always 0.
5853 ** </dd>
5854 ** </dl>
5855 */
5856 #define SQLITE_DBSTATUS_LOOKASIDE_USED 0
5857 #define SQLITE_DBSTATUS_CACHE_USED 1
5858 #define SQLITE_DBSTATUS_SCHEMA_USED 2
5859 #define SQLITE_DBSTATUS_STMT_USED 3
5860 #define SQLITE_DBSTATUS_LOOKASIDE_HIT 4
5861 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE 5
5862 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL 6
5863 #define SQLITE_DBSTATUS_CACHE_HIT 7
5864 #define SQLITE_DBSTATUS_CACHE_MISS 8
5865 #define SQLITE_DBSTATUS_MAX 8 /* Largest defined DBSTATUS */
5866
5867
5868 /*
5869 ** CAPI3REF: Prepared Statement Status
5870 **
@@ -5876,11 +5914,10 @@
5914 ** <dd>^This is the number of rows inserted into transient indices that
5915 ** were created automatically in order to help joins run faster.
5916 ** A non-zero value in this counter may indicate an opportunity to
5917 ** improvement performance by adding permanent indices that do not
5918 ** need to be reinitialized each time the statement is run.</dd>
 
5919 ** </dl>
5920 */
5921 #define SQLITE_STMTSTATUS_FULLSCAN_STEP 1
5922 #define SQLITE_STMTSTATUS_SORT 2
5923 #define SQLITE_STMTSTATUS_AUTOINDEX 3
5924
+6 -6
--- src/stash.c
+++ src/stash.c
@@ -482,11 +482,11 @@
482482
db_finalize(&q);
483483
if( n==0 ) fossil_print("empty stash\n");
484484
}else
485485
if( memcmp(zCmd, "drop", nCmd)==0 || memcmp(zCmd, "rm", nCmd)==0 ){
486486
int allFlag = find_option("all", 0, 0)!=0;
487
- if( g.argc>4 ) usage("stash apply STASHID");
487
+ if( g.argc>4 ) usage("apply STASHID");
488488
if( allFlag ){
489489
db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
490490
}else{
491491
stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
492492
undo_begin();
@@ -494,30 +494,30 @@
494494
stash_drop(stashid);
495495
undo_finish();
496496
}
497497
}else
498498
if( memcmp(zCmd, "pop", nCmd)==0 ){
499
- if( g.argc>3 ) usage("stash pop");
499
+ if( g.argc>3 ) usage("pop");
500500
stashid = stash_get_id(0);
501501
undo_begin();
502502
stash_apply(stashid, 0);
503503
undo_save_stash(stashid);
504504
undo_finish();
505505
stash_drop(stashid);
506506
}else
507507
if( memcmp(zCmd, "apply", nCmd)==0 ){
508
- if( g.argc>4 ) usage("stash apply STASHID");
508
+ if( g.argc>4 ) usage("apply STASHID");
509509
stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
510510
undo_begin();
511511
stash_apply(stashid, 0);
512512
undo_finish();
513513
}else
514514
if( memcmp(zCmd, "goto", nCmd)==0 ){
515515
int nConflict;
516516
int vid;
517517
518
- if( g.argc>4 ) usage("stash apply STASHID");
518
+ if( g.argc>4 ) usage("apply STASHID");
519519
stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
520520
undo_begin();
521521
vid = db_int(0, "SELECT vid FROM stash WHERE stashid=%d", stashid);
522522
nConflict = update_to(vid);
523523
stash_apply(stashid, nConflict);
@@ -526,20 +526,20 @@
526526
stashid);
527527
undo_finish();
528528
}else
529529
if( memcmp(zCmd, "diff", nCmd)==0 ){
530530
const char *zDiffCmd = db_get("diff-command", 0);
531
- if( g.argc>4 ) usage("stash diff STASHID");
531
+ if( g.argc>4 ) usage("diff STASHID");
532532
stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
533533
stash_diff(stashid, zDiffCmd);
534534
}else
535535
if( memcmp(zCmd, "gdiff", nCmd)==0 ){
536536
const char *zDiffCmd = db_get("gdiff-command", 0);
537
- if( g.argc>4 ) usage("stash diff STASHID");
537
+ if( g.argc>4 ) usage("diff STASHID");
538538
stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
539539
stash_diff(stashid, zDiffCmd);
540540
}else
541541
{
542542
usage("SUBCOMMAND ARGS...");
543543
}
544544
db_end_transaction(0);
545545
}
546546
--- src/stash.c
+++ src/stash.c
@@ -482,11 +482,11 @@
482 db_finalize(&q);
483 if( n==0 ) fossil_print("empty stash\n");
484 }else
485 if( memcmp(zCmd, "drop", nCmd)==0 || memcmp(zCmd, "rm", nCmd)==0 ){
486 int allFlag = find_option("all", 0, 0)!=0;
487 if( g.argc>4 ) usage("stash apply STASHID");
488 if( allFlag ){
489 db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
490 }else{
491 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
492 undo_begin();
@@ -494,30 +494,30 @@
494 stash_drop(stashid);
495 undo_finish();
496 }
497 }else
498 if( memcmp(zCmd, "pop", nCmd)==0 ){
499 if( g.argc>3 ) usage("stash pop");
500 stashid = stash_get_id(0);
501 undo_begin();
502 stash_apply(stashid, 0);
503 undo_save_stash(stashid);
504 undo_finish();
505 stash_drop(stashid);
506 }else
507 if( memcmp(zCmd, "apply", nCmd)==0 ){
508 if( g.argc>4 ) usage("stash apply STASHID");
509 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
510 undo_begin();
511 stash_apply(stashid, 0);
512 undo_finish();
513 }else
514 if( memcmp(zCmd, "goto", nCmd)==0 ){
515 int nConflict;
516 int vid;
517
518 if( g.argc>4 ) usage("stash apply STASHID");
519 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
520 undo_begin();
521 vid = db_int(0, "SELECT vid FROM stash WHERE stashid=%d", stashid);
522 nConflict = update_to(vid);
523 stash_apply(stashid, nConflict);
@@ -526,20 +526,20 @@
526 stashid);
527 undo_finish();
528 }else
529 if( memcmp(zCmd, "diff", nCmd)==0 ){
530 const char *zDiffCmd = db_get("diff-command", 0);
531 if( g.argc>4 ) usage("stash diff STASHID");
532 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
533 stash_diff(stashid, zDiffCmd);
534 }else
535 if( memcmp(zCmd, "gdiff", nCmd)==0 ){
536 const char *zDiffCmd = db_get("gdiff-command", 0);
537 if( g.argc>4 ) usage("stash diff STASHID");
538 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
539 stash_diff(stashid, zDiffCmd);
540 }else
541 {
542 usage("SUBCOMMAND ARGS...");
543 }
544 db_end_transaction(0);
545 }
546
--- src/stash.c
+++ src/stash.c
@@ -482,11 +482,11 @@
482 db_finalize(&q);
483 if( n==0 ) fossil_print("empty stash\n");
484 }else
485 if( memcmp(zCmd, "drop", nCmd)==0 || memcmp(zCmd, "rm", nCmd)==0 ){
486 int allFlag = find_option("all", 0, 0)!=0;
487 if( g.argc>4 ) usage("apply STASHID");
488 if( allFlag ){
489 db_multi_exec("DELETE FROM stash; DELETE FROM stashfile;");
490 }else{
491 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
492 undo_begin();
@@ -494,30 +494,30 @@
494 stash_drop(stashid);
495 undo_finish();
496 }
497 }else
498 if( memcmp(zCmd, "pop", nCmd)==0 ){
499 if( g.argc>3 ) usage("pop");
500 stashid = stash_get_id(0);
501 undo_begin();
502 stash_apply(stashid, 0);
503 undo_save_stash(stashid);
504 undo_finish();
505 stash_drop(stashid);
506 }else
507 if( memcmp(zCmd, "apply", nCmd)==0 ){
508 if( g.argc>4 ) usage("apply STASHID");
509 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
510 undo_begin();
511 stash_apply(stashid, 0);
512 undo_finish();
513 }else
514 if( memcmp(zCmd, "goto", nCmd)==0 ){
515 int nConflict;
516 int vid;
517
518 if( g.argc>4 ) usage("apply STASHID");
519 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
520 undo_begin();
521 vid = db_int(0, "SELECT vid FROM stash WHERE stashid=%d", stashid);
522 nConflict = update_to(vid);
523 stash_apply(stashid, nConflict);
@@ -526,20 +526,20 @@
526 stashid);
527 undo_finish();
528 }else
529 if( memcmp(zCmd, "diff", nCmd)==0 ){
530 const char *zDiffCmd = db_get("diff-command", 0);
531 if( g.argc>4 ) usage("diff STASHID");
532 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
533 stash_diff(stashid, zDiffCmd);
534 }else
535 if( memcmp(zCmd, "gdiff", nCmd)==0 ){
536 const char *zDiffCmd = db_get("gdiff-command", 0);
537 if( g.argc>4 ) usage("diff STASHID");
538 stashid = stash_get_id(g.argc==4 ? g.argv[3] : 0);
539 stash_diff(stashid, zDiffCmd);
540 }else
541 {
542 usage("SUBCOMMAND ARGS...");
543 }
544 db_end_transaction(0);
545 }
546
+73 -2
--- src/style.c
+++ src/style.c
@@ -397,10 +397,69 @@
397397
@ vertical-align: top;
398398
@ text-align: right;
399399
@ padding: 0.2ex 2ex;
400400
@ }
401401
@
402
+@ /* Side-by-side diff */
403
+@ table.sbsdiff {
404
+@ background-color: white;
405
+@ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
406
+@ font-size: 8pt;
407
+@ border-collapse:collapse;
408
+@ white-space: pre;
409
+@ width: 98%;
410
+@ border: 1px #000 dashed;
411
+@ margin-left: auto;
412
+@ margin-right: auto;
413
+@ }
414
+@
415
+@ table.sbsdiff th.diffhdr {
416
+@ border-bottom: dotted;
417
+@ border-width: 1px;
418
+@ }
419
+@
420
+@ table.sbsdiff tr td {
421
+@ white-space: pre;
422
+@ padding-left: 3px;
423
+@ padding-right: 3px;
424
+@ margin: 0px;
425
+@ vertical-align: top;
426
+@ }
427
+@
428
+@ table.sbsdiff tr td.lineno {
429
+@ text-align: right;
430
+@ }
431
+@
432
+@ table.sbsdiff tr td.srcline {
433
+@ }
434
+@
435
+@ table.sbsdiff tr td.meta {
436
+@ background-color: rgb(170, 160, 255);
437
+@ text-align: center;
438
+@ }
439
+@
440
+@ table.sbsdiff tr td.added {
441
+@ background-color: rgb(180, 250, 180);
442
+@ }
443
+@ table.sbsdiff tr td.addedvoid {
444
+@ background-color: rgb(190, 190, 180);
445
+@ }
446
+@
447
+@ table.sbsdiff tr td.removed {
448
+@ background-color: rgb(250, 130, 130);
449
+@ }
450
+@ table.sbsdiff tr td.removedvoid {
451
+@ background-color: rgb(190, 190, 180);
452
+@ }
453
+@
454
+@ table.sbsdiff tr td.changed {
455
+@ background-color: rgb(210, 210, 200);
456
+@ }
457
+@ table.sbsdiff tr td.changedvoid {
458
+@ background-color: rgb(190, 190, 180);
459
+@ }
460
+@
402461
;
403462
404463
405464
/* The following table contains bits of default CSS that must
406465
** be included if they are not found in the application-defined
@@ -803,14 +862,26 @@
803862
** WEBPAGE: test_env
804863
*/
805864
void page_test_env(void){
806865
char c;
807866
int i;
867
+ int showAll;
808868
char zCap[30];
809869
login_check_credentials();
810
- if( !g.perm.Admin && !g.perm.Setup ){ login_needed(); return; }
870
+ if( !g.perm.Admin && !g.perm.Setup && !db_get_boolean("test_env_enable",0) ){
871
+ login_needed();
872
+ return;
873
+ }
811874
style_header("Environment Test");
875
+ showAll = atoi(PD("showall","0"));
876
+ if( !showAll ){
877
+ style_submenu_element("Show Cookies", "Show Cookies",
878
+ "%s/test_env?showall=1", g.zTop);
879
+ }else{
880
+ style_submenu_element("Hide Cookies", "Hide Cookies",
881
+ "%s/test_env", g.zTop);
882
+ }
812883
#if !defined(_WIN32)
813884
@ uid=%d(getuid()), gid=%d(getgid())<br />
814885
#endif
815886
@ g.zBaseURL = %h(g.zBaseURL)<br />
816887
@ g.zTop = %h(g.zTop)<br />
@@ -820,12 +891,12 @@
820891
zCap[i] = 0;
821892
@ g.userUid = %d(g.userUid)<br />
822893
@ g.zLogin = %h(g.zLogin)<br />
823894
@ capabilities = %s(zCap)<br />
824895
@ <hr>
825
- cgi_print_all();
896
+ cgi_print_all(atoi(PD("showall","0")));
826897
if( g.perm.Setup ){
827898
const char *zRedir = P("redirect");
828899
if( zRedir ) cgi_redirect(zRedir);
829900
}
830901
style_footer();
831902
}
832903
--- src/style.c
+++ src/style.c
@@ -397,10 +397,69 @@
397 @ vertical-align: top;
398 @ text-align: right;
399 @ padding: 0.2ex 2ex;
400 @ }
401 @
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402 ;
403
404
405 /* The following table contains bits of default CSS that must
406 ** be included if they are not found in the application-defined
@@ -803,14 +862,26 @@
803 ** WEBPAGE: test_env
804 */
805 void page_test_env(void){
806 char c;
807 int i;
 
808 char zCap[30];
809 login_check_credentials();
810 if( !g.perm.Admin && !g.perm.Setup ){ login_needed(); return; }
 
 
 
811 style_header("Environment Test");
 
 
 
 
 
 
 
 
812 #if !defined(_WIN32)
813 @ uid=%d(getuid()), gid=%d(getgid())<br />
814 #endif
815 @ g.zBaseURL = %h(g.zBaseURL)<br />
816 @ g.zTop = %h(g.zTop)<br />
@@ -820,12 +891,12 @@
820 zCap[i] = 0;
821 @ g.userUid = %d(g.userUid)<br />
822 @ g.zLogin = %h(g.zLogin)<br />
823 @ capabilities = %s(zCap)<br />
824 @ <hr>
825 cgi_print_all();
826 if( g.perm.Setup ){
827 const char *zRedir = P("redirect");
828 if( zRedir ) cgi_redirect(zRedir);
829 }
830 style_footer();
831 }
832
--- src/style.c
+++ src/style.c
@@ -397,10 +397,69 @@
397 @ vertical-align: top;
398 @ text-align: right;
399 @ padding: 0.2ex 2ex;
400 @ }
401 @
402 @ /* Side-by-side diff */
403 @ table.sbsdiff {
404 @ background-color: white;
405 @ font-family: fixed, Dejavu Sans Mono, Monaco, Lucida Console, monospace;
406 @ font-size: 8pt;
407 @ border-collapse:collapse;
408 @ white-space: pre;
409 @ width: 98%;
410 @ border: 1px #000 dashed;
411 @ margin-left: auto;
412 @ margin-right: auto;
413 @ }
414 @
415 @ table.sbsdiff th.diffhdr {
416 @ border-bottom: dotted;
417 @ border-width: 1px;
418 @ }
419 @
420 @ table.sbsdiff tr td {
421 @ white-space: pre;
422 @ padding-left: 3px;
423 @ padding-right: 3px;
424 @ margin: 0px;
425 @ vertical-align: top;
426 @ }
427 @
428 @ table.sbsdiff tr td.lineno {
429 @ text-align: right;
430 @ }
431 @
432 @ table.sbsdiff tr td.srcline {
433 @ }
434 @
435 @ table.sbsdiff tr td.meta {
436 @ background-color: rgb(170, 160, 255);
437 @ text-align: center;
438 @ }
439 @
440 @ table.sbsdiff tr td.added {
441 @ background-color: rgb(180, 250, 180);
442 @ }
443 @ table.sbsdiff tr td.addedvoid {
444 @ background-color: rgb(190, 190, 180);
445 @ }
446 @
447 @ table.sbsdiff tr td.removed {
448 @ background-color: rgb(250, 130, 130);
449 @ }
450 @ table.sbsdiff tr td.removedvoid {
451 @ background-color: rgb(190, 190, 180);
452 @ }
453 @
454 @ table.sbsdiff tr td.changed {
455 @ background-color: rgb(210, 210, 200);
456 @ }
457 @ table.sbsdiff tr td.changedvoid {
458 @ background-color: rgb(190, 190, 180);
459 @ }
460 @
461 ;
462
463
464 /* The following table contains bits of default CSS that must
465 ** be included if they are not found in the application-defined
@@ -803,14 +862,26 @@
862 ** WEBPAGE: test_env
863 */
864 void page_test_env(void){
865 char c;
866 int i;
867 int showAll;
868 char zCap[30];
869 login_check_credentials();
870 if( !g.perm.Admin && !g.perm.Setup && !db_get_boolean("test_env_enable",0) ){
871 login_needed();
872 return;
873 }
874 style_header("Environment Test");
875 showAll = atoi(PD("showall","0"));
876 if( !showAll ){
877 style_submenu_element("Show Cookies", "Show Cookies",
878 "%s/test_env?showall=1", g.zTop);
879 }else{
880 style_submenu_element("Hide Cookies", "Hide Cookies",
881 "%s/test_env", g.zTop);
882 }
883 #if !defined(_WIN32)
884 @ uid=%d(getuid()), gid=%d(getgid())<br />
885 #endif
886 @ g.zBaseURL = %h(g.zBaseURL)<br />
887 @ g.zTop = %h(g.zTop)<br />
@@ -820,12 +891,12 @@
891 zCap[i] = 0;
892 @ g.userUid = %d(g.userUid)<br />
893 @ g.zLogin = %h(g.zLogin)<br />
894 @ capabilities = %s(zCap)<br />
895 @ <hr>
896 cgi_print_all(atoi(PD("showall","0")));
897 if( g.perm.Setup ){
898 const char *zRedir = P("redirect");
899 if( zRedir ) cgi_redirect(zRedir);
900 }
901 style_footer();
902 }
903
+5 -4
--- src/tar.c
+++ src/tar.c
@@ -44,11 +44,11 @@
4444
/*
4545
** Begin the process of generating a tarball.
4646
**
4747
** Initialize the GZIP compressor and the table of directory names.
4848
*/
49
-static void tar_begin(void){
49
+static void tar_begin(sqlite3_int64 mTime){
5050
assert( tball.aHdr==0 );
5151
tball.aHdr = fossil_malloc(512+512);
5252
memset(tball.aHdr, 0, 512+512);
5353
tball.zSpaces = (char*)&tball.aHdr[512];
5454
/* zPrevDir init */
@@ -60,11 +60,11 @@
6060
memcpy(&tball.aHdr[108], "0000000", 8); /* Owner ID */
6161
memcpy(&tball.aHdr[116], "0000000", 8); /* Group ID */
6262
memcpy(&tball.aHdr[257], "ustar\00000", 8); /* POSIX.1 format */
6363
memcpy(&tball.aHdr[265], "nobody", 7); /* Owner name */
6464
memcpy(&tball.aHdr[297], "nobody", 7); /* Group name */
65
- gzip_begin();
65
+ gzip_begin(mTime);
6666
db_multi_exec(
6767
"CREATE TEMP TABLE dir(name UNIQUE);"
6868
);
6969
}
7070
@@ -427,11 +427,11 @@
427427
Blob file;
428428
if( g.argc<3 ){
429429
usage("ARCHIVE FILE....");
430430
}
431431
sqlite3_open(":memory:", &g.db);
432
- tar_begin();
432
+ tar_begin(0);
433433
for(i=3; i<g.argc; i++){
434434
blob_zero(&file);
435435
blob_read_from_file(&file, g.argv[i]);
436436
tar_add_file(g.argv[i], &file,
437437
file_wd_perm(g.argv[i]), file_wd_mtime(g.argv[i]));
@@ -473,20 +473,20 @@
473473
blob_zero(pTar);
474474
return;
475475
}
476476
blob_zero(&hash);
477477
blob_zero(&filename);
478
- tar_begin();
479478
480479
if( zDir && zDir[0] ){
481480
blob_appendf(&filename, "%s/", zDir);
482481
}
483482
nPrefix = blob_size(&filename);
484483
485484
pManifest = manifest_get(rid, CFTYPE_MANIFEST);
486485
if( pManifest ){
487486
mTime = (pManifest->rDate - 2440587.5)*86400.0;
487
+ tar_begin(mTime);
488488
if( db_get_boolean("manifest", 0) ){
489489
blob_append(&filename, "manifest", -1);
490490
zName = blob_str(&filename);
491491
tar_add_file(zName, &mfile, 0, mTime);
492492
sha1sum_blob(&mfile, &hash);
@@ -513,10 +513,11 @@
513513
}else{
514514
sha1sum_blob(&mfile, &hash);
515515
blob_append(&filename, blob_str(&hash), 16);
516516
zName = blob_str(&filename);
517517
mTime = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0;");
518
+ tar_begin(mTime);
518519
tar_add_file(zName, &mfile, 0, mTime);
519520
}
520521
manifest_destroy(pManifest);
521522
blob_reset(&mfile);
522523
blob_reset(&filename);
523524
--- src/tar.c
+++ src/tar.c
@@ -44,11 +44,11 @@
44 /*
45 ** Begin the process of generating a tarball.
46 **
47 ** Initialize the GZIP compressor and the table of directory names.
48 */
49 static void tar_begin(void){
50 assert( tball.aHdr==0 );
51 tball.aHdr = fossil_malloc(512+512);
52 memset(tball.aHdr, 0, 512+512);
53 tball.zSpaces = (char*)&tball.aHdr[512];
54 /* zPrevDir init */
@@ -60,11 +60,11 @@
60 memcpy(&tball.aHdr[108], "0000000", 8); /* Owner ID */
61 memcpy(&tball.aHdr[116], "0000000", 8); /* Group ID */
62 memcpy(&tball.aHdr[257], "ustar\00000", 8); /* POSIX.1 format */
63 memcpy(&tball.aHdr[265], "nobody", 7); /* Owner name */
64 memcpy(&tball.aHdr[297], "nobody", 7); /* Group name */
65 gzip_begin();
66 db_multi_exec(
67 "CREATE TEMP TABLE dir(name UNIQUE);"
68 );
69 }
70
@@ -427,11 +427,11 @@
427 Blob file;
428 if( g.argc<3 ){
429 usage("ARCHIVE FILE....");
430 }
431 sqlite3_open(":memory:", &g.db);
432 tar_begin();
433 for(i=3; i<g.argc; i++){
434 blob_zero(&file);
435 blob_read_from_file(&file, g.argv[i]);
436 tar_add_file(g.argv[i], &file,
437 file_wd_perm(g.argv[i]), file_wd_mtime(g.argv[i]));
@@ -473,20 +473,20 @@
473 blob_zero(pTar);
474 return;
475 }
476 blob_zero(&hash);
477 blob_zero(&filename);
478 tar_begin();
479
480 if( zDir && zDir[0] ){
481 blob_appendf(&filename, "%s/", zDir);
482 }
483 nPrefix = blob_size(&filename);
484
485 pManifest = manifest_get(rid, CFTYPE_MANIFEST);
486 if( pManifest ){
487 mTime = (pManifest->rDate - 2440587.5)*86400.0;
 
488 if( db_get_boolean("manifest", 0) ){
489 blob_append(&filename, "manifest", -1);
490 zName = blob_str(&filename);
491 tar_add_file(zName, &mfile, 0, mTime);
492 sha1sum_blob(&mfile, &hash);
@@ -513,10 +513,11 @@
513 }else{
514 sha1sum_blob(&mfile, &hash);
515 blob_append(&filename, blob_str(&hash), 16);
516 zName = blob_str(&filename);
517 mTime = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0;");
 
518 tar_add_file(zName, &mfile, 0, mTime);
519 }
520 manifest_destroy(pManifest);
521 blob_reset(&mfile);
522 blob_reset(&filename);
523
--- src/tar.c
+++ src/tar.c
@@ -44,11 +44,11 @@
44 /*
45 ** Begin the process of generating a tarball.
46 **
47 ** Initialize the GZIP compressor and the table of directory names.
48 */
49 static void tar_begin(sqlite3_int64 mTime){
50 assert( tball.aHdr==0 );
51 tball.aHdr = fossil_malloc(512+512);
52 memset(tball.aHdr, 0, 512+512);
53 tball.zSpaces = (char*)&tball.aHdr[512];
54 /* zPrevDir init */
@@ -60,11 +60,11 @@
60 memcpy(&tball.aHdr[108], "0000000", 8); /* Owner ID */
61 memcpy(&tball.aHdr[116], "0000000", 8); /* Group ID */
62 memcpy(&tball.aHdr[257], "ustar\00000", 8); /* POSIX.1 format */
63 memcpy(&tball.aHdr[265], "nobody", 7); /* Owner name */
64 memcpy(&tball.aHdr[297], "nobody", 7); /* Group name */
65 gzip_begin(mTime);
66 db_multi_exec(
67 "CREATE TEMP TABLE dir(name UNIQUE);"
68 );
69 }
70
@@ -427,11 +427,11 @@
427 Blob file;
428 if( g.argc<3 ){
429 usage("ARCHIVE FILE....");
430 }
431 sqlite3_open(":memory:", &g.db);
432 tar_begin(0);
433 for(i=3; i<g.argc; i++){
434 blob_zero(&file);
435 blob_read_from_file(&file, g.argv[i]);
436 tar_add_file(g.argv[i], &file,
437 file_wd_perm(g.argv[i]), file_wd_mtime(g.argv[i]));
@@ -473,20 +473,20 @@
473 blob_zero(pTar);
474 return;
475 }
476 blob_zero(&hash);
477 blob_zero(&filename);
 
478
479 if( zDir && zDir[0] ){
480 blob_appendf(&filename, "%s/", zDir);
481 }
482 nPrefix = blob_size(&filename);
483
484 pManifest = manifest_get(rid, CFTYPE_MANIFEST);
485 if( pManifest ){
486 mTime = (pManifest->rDate - 2440587.5)*86400.0;
487 tar_begin(mTime);
488 if( db_get_boolean("manifest", 0) ){
489 blob_append(&filename, "manifest", -1);
490 zName = blob_str(&filename);
491 tar_add_file(zName, &mfile, 0, mTime);
492 sha1sum_blob(&mfile, &hash);
@@ -513,10 +513,11 @@
513 }else{
514 sha1sum_blob(&mfile, &hash);
515 blob_append(&filename, blob_str(&hash), 16);
516 zName = blob_str(&filename);
517 mTime = db_int64(0, "SELECT (julianday('now') - 2440587.5)*86400.0;");
518 tar_begin(mTime);
519 tar_add_file(zName, &mfile, 0, mTime);
520 }
521 manifest_destroy(pManifest);
522 blob_reset(&mfile);
523 blob_reset(&filename);
524
+2 -2
--- src/th.c
+++ src/th.c
@@ -1817,12 +1817,12 @@
18171817
rc = thSubstWord(interp, pExpr->zValue, pExpr->nValue);
18181818
}else{
18191819
int eArgType = 0; /* Actual type of arguments */
18201820
18211821
/* Argument values */
1822
- int iLeft;
1823
- int iRight;
1822
+ int iLeft = 0;
1823
+ int iRight = 0;
18241824
double fLeft;
18251825
double fRight;
18261826
18271827
/* Left and right arguments as strings */
18281828
char *zLeft = 0; int nLeft = 0;
18291829
--- src/th.c
+++ src/th.c
@@ -1817,12 +1817,12 @@
1817 rc = thSubstWord(interp, pExpr->zValue, pExpr->nValue);
1818 }else{
1819 int eArgType = 0; /* Actual type of arguments */
1820
1821 /* Argument values */
1822 int iLeft;
1823 int iRight;
1824 double fLeft;
1825 double fRight;
1826
1827 /* Left and right arguments as strings */
1828 char *zLeft = 0; int nLeft = 0;
1829
--- src/th.c
+++ src/th.c
@@ -1817,12 +1817,12 @@
1817 rc = thSubstWord(interp, pExpr->zValue, pExpr->nValue);
1818 }else{
1819 int eArgType = 0; /* Actual type of arguments */
1820
1821 /* Argument values */
1822 int iLeft = 0;
1823 int iRight = 0;
1824 double fLeft;
1825 double fRight;
1826
1827 /* Left and right arguments as strings */
1828 char *zLeft = 0; int nLeft = 0;
1829
+28 -12
--- src/timeline.c
+++ src/timeline.c
@@ -179,11 +179,11 @@
179179
** 2. Date/Time
180180
** 3. Comment string
181181
** 4. User
182182
** 5. True if is a leaf
183183
** 6. background color
184
-** 7. type ("ci", "w", "t", "e", "div")
184
+** 7. type ("ci", "w", "t", "e", "g", "div")
185185
** 8. list of symbolic tags.
186186
** 9. tagid for ticket or wiki or event
187187
** 10. Short comment to user for repeated tickets and wiki
188188
*/
189189
void www_print_timeline(
@@ -316,10 +316,13 @@
316316
@</td>
317317
if( zBgClr && zBgClr[0] ){
318318
@ <td class="timelineTableCell" style="background-color: %h(zBgClr);">
319319
}else{
320320
@ <td class="timelineTableCell">
321
+ }
322
+ if( pGraph && zType[0]!='c' ){
323
+ @ &bull;
321324
}
322325
if( zType[0]=='c' ){
323326
hyperlink_to_uuid(zUuid);
324327
if( isLeaf ){
325328
if( db_exists("SELECT 1 FROM tagxref"
@@ -840,23 +843,24 @@
840843
**
841844
** a=TIMESTAMP after this date
842845
** b=TIMESTAMP before this date.
843846
** c=TIMESTAMP "circa" this date.
844847
** n=COUNT number of events in output
845
-** p=RID artifact RID and up to COUNT parents and ancestors
846
-** d=RID artifact RID and up to COUNT descendants
848
+** p=UUID artifact and up to COUNT parents and ancestors
849
+** d=UUID artifact and up to COUNT descendants
850
+** dp=UUUID The same as d=UUID&p=UUID
847851
** t=TAGID show only check-ins with the given tagid
848852
** r=TAGID show check-ins related to tagid
849853
** u=USER only if belonging to this user
850854
** y=TYPE 'ci', 'w', 't', 'e'
851855
** s=TEXT string search (comment and brief)
852856
** ng Suppress the graph if present
853857
** nd Suppress "divider" lines
854858
** fc Show details of files changed
855
-** f=RID Show family (immediate parents and children) of RID
856
-** from=RID Path from...
857
-** to=RID ... to this
859
+** f=UUID Show family (immediate parents and children) of UUID
860
+** from=UUID Path from...
861
+** to=UUID ... to this
858862
** nomerge ... avoid merge links on the path
859863
** brbg Background color from branch name
860864
** ubg Background color from user
861865
**
862866
** p= and d= can appear individually or together. If either p= or d=
@@ -892,15 +896,23 @@
892896
int from_rid = name_to_typed_rid(P("from"),"ci"); /* from= for paths */
893897
int to_rid = name_to_typed_rid(P("to"),"ci"); /* to= for path timelines */
894898
int noMerge = P("nomerge")!=0; /* Do not follow merge links */
895899
int me_rid = name_to_typed_rid(P("me"),"ci"); /* me= for common ancestory */
896900
int you_rid = name_to_typed_rid(P("you"),"ci");/* you= for common ancst */
901
+ int pd_rid;
897902
898903
/* To view the timeline, must have permission to read project data.
899904
*/
905
+ pd_rid = name_to_typed_rid(P("dp"),"ci");
906
+ if( pd_rid ){
907
+ p_rid = d_rid = pd_rid;
908
+ }
900909
login_check_credentials();
901
- if( !g.perm.Read && !g.perm.RdTkt && !g.perm.RdWiki ){ login_needed(); return; }
910
+ if( !g.perm.Read && !g.perm.RdTkt && !g.perm.RdWiki ){
911
+ login_needed();
912
+ return;
913
+ }
902914
if( zTagName && g.perm.Read ){
903915
tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'", zTagName);
904916
zThisTag = zTagName;
905917
}else if( zBrName && g.perm.Read ){
906918
tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'",zBrName);
@@ -988,14 +1000,12 @@
9881000
blob_appendf(&sql, " AND event.objid IN ok");
9891001
nd = 0;
9901002
if( d_rid ){
9911003
compute_descendants(d_rid, nEntry+1);
9921004
nd = db_int(0, "SELECT count(*)-1 FROM ok");
993
- if( nd>=0 ){
994
- db_multi_exec("%s", blob_str(&sql));
995
- blob_appendf(&desc, "%d descendant%s", nd,(1==nd)?"":"s");
996
- }
1005
+ if( nd>=0 ) db_multi_exec("%s", blob_str(&sql));
1006
+ if( nd>0 ) blob_appendf(&desc, "%d descendant%s", nd,(1==nd)?"":"s");
9971007
if( useDividers ) timeline_add_dividers(0, d_rid);
9981008
db_multi_exec("DELETE FROM ok");
9991009
}
10001010
if( p_rid ){
10011011
compute_ancestors(p_rid, nEntry+1);
@@ -1075,19 +1085,20 @@
10751085
}
10761086
if( (zType[0]=='w' && !g.perm.RdWiki)
10771087
|| (zType[0]=='t' && !g.perm.RdTkt)
10781088
|| (zType[0]=='e' && !g.perm.RdWiki)
10791089
|| (zType[0]=='c' && !g.perm.Read)
1090
+ || (zType[0]=='g' && !g.perm.Read)
10801091
){
10811092
zType = "all";
10821093
}
10831094
if( zType[0]=='a' ){
10841095
if( !g.perm.Read || !g.perm.RdWiki || !g.perm.RdTkt ){
10851096
char cSep = '(';
10861097
blob_appendf(&sql, " AND event.type IN ");
10871098
if( g.perm.Read ){
1088
- blob_appendf(&sql, "%c'ci'", cSep);
1099
+ blob_appendf(&sql, "%c'ci','g'", cSep);
10891100
cSep = ',';
10901101
}
10911102
if( g.perm.RdWiki ){
10921103
blob_appendf(&sql, "%c'w','e'", cSep);
10931104
cSep = ',';
@@ -1107,10 +1118,12 @@
11071118
zEType = "wiki edit";
11081119
}else if( zType[0]=='t' ){
11091120
zEType = "ticket change";
11101121
}else if( zType[0]=='e' ){
11111122
zEType = "event";
1123
+ }else if( zType[0]=='g' ){
1124
+ zEType = "tag";
11121125
}
11131126
}
11141127
if( zUser ){
11151128
blob_appendf(&sql, " AND (event.user=%Q OR event.euser=%Q)",
11161129
zUser, zUser);
@@ -1226,10 +1239,13 @@
12261239
timeline_submenu(&url, "Tickets Only", "y", "t", 0);
12271240
}
12281241
if( zType[0]!='e' && g.perm.RdWiki ){
12291242
timeline_submenu(&url, "Events Only", "y", "e", 0);
12301243
}
1244
+ if( zType[0]!='g' && g.perm.Read ){
1245
+ timeline_submenu(&url, "Tags Only", "y", "g", 0);
1246
+ }
12311247
}
12321248
if( nEntry>20 ){
12331249
timeline_submenu(&url, "20 Entries", "n", "20", 0);
12341250
}
12351251
if( nEntry<200 ){
12361252
--- src/timeline.c
+++ src/timeline.c
@@ -179,11 +179,11 @@
179 ** 2. Date/Time
180 ** 3. Comment string
181 ** 4. User
182 ** 5. True if is a leaf
183 ** 6. background color
184 ** 7. type ("ci", "w", "t", "e", "div")
185 ** 8. list of symbolic tags.
186 ** 9. tagid for ticket or wiki or event
187 ** 10. Short comment to user for repeated tickets and wiki
188 */
189 void www_print_timeline(
@@ -316,10 +316,13 @@
316 @</td>
317 if( zBgClr && zBgClr[0] ){
318 @ <td class="timelineTableCell" style="background-color: %h(zBgClr);">
319 }else{
320 @ <td class="timelineTableCell">
 
 
 
321 }
322 if( zType[0]=='c' ){
323 hyperlink_to_uuid(zUuid);
324 if( isLeaf ){
325 if( db_exists("SELECT 1 FROM tagxref"
@@ -840,23 +843,24 @@
840 **
841 ** a=TIMESTAMP after this date
842 ** b=TIMESTAMP before this date.
843 ** c=TIMESTAMP "circa" this date.
844 ** n=COUNT number of events in output
845 ** p=RID artifact RID and up to COUNT parents and ancestors
846 ** d=RID artifact RID and up to COUNT descendants
 
847 ** t=TAGID show only check-ins with the given tagid
848 ** r=TAGID show check-ins related to tagid
849 ** u=USER only if belonging to this user
850 ** y=TYPE 'ci', 'w', 't', 'e'
851 ** s=TEXT string search (comment and brief)
852 ** ng Suppress the graph if present
853 ** nd Suppress "divider" lines
854 ** fc Show details of files changed
855 ** f=RID Show family (immediate parents and children) of RID
856 ** from=RID Path from...
857 ** to=RID ... to this
858 ** nomerge ... avoid merge links on the path
859 ** brbg Background color from branch name
860 ** ubg Background color from user
861 **
862 ** p= and d= can appear individually or together. If either p= or d=
@@ -892,15 +896,23 @@
892 int from_rid = name_to_typed_rid(P("from"),"ci"); /* from= for paths */
893 int to_rid = name_to_typed_rid(P("to"),"ci"); /* to= for path timelines */
894 int noMerge = P("nomerge")!=0; /* Do not follow merge links */
895 int me_rid = name_to_typed_rid(P("me"),"ci"); /* me= for common ancestory */
896 int you_rid = name_to_typed_rid(P("you"),"ci");/* you= for common ancst */
 
897
898 /* To view the timeline, must have permission to read project data.
899 */
 
 
 
 
900 login_check_credentials();
901 if( !g.perm.Read && !g.perm.RdTkt && !g.perm.RdWiki ){ login_needed(); return; }
 
 
 
902 if( zTagName && g.perm.Read ){
903 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'", zTagName);
904 zThisTag = zTagName;
905 }else if( zBrName && g.perm.Read ){
906 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'",zBrName);
@@ -988,14 +1000,12 @@
988 blob_appendf(&sql, " AND event.objid IN ok");
989 nd = 0;
990 if( d_rid ){
991 compute_descendants(d_rid, nEntry+1);
992 nd = db_int(0, "SELECT count(*)-1 FROM ok");
993 if( nd>=0 ){
994 db_multi_exec("%s", blob_str(&sql));
995 blob_appendf(&desc, "%d descendant%s", nd,(1==nd)?"":"s");
996 }
997 if( useDividers ) timeline_add_dividers(0, d_rid);
998 db_multi_exec("DELETE FROM ok");
999 }
1000 if( p_rid ){
1001 compute_ancestors(p_rid, nEntry+1);
@@ -1075,19 +1085,20 @@
1075 }
1076 if( (zType[0]=='w' && !g.perm.RdWiki)
1077 || (zType[0]=='t' && !g.perm.RdTkt)
1078 || (zType[0]=='e' && !g.perm.RdWiki)
1079 || (zType[0]=='c' && !g.perm.Read)
 
1080 ){
1081 zType = "all";
1082 }
1083 if( zType[0]=='a' ){
1084 if( !g.perm.Read || !g.perm.RdWiki || !g.perm.RdTkt ){
1085 char cSep = '(';
1086 blob_appendf(&sql, " AND event.type IN ");
1087 if( g.perm.Read ){
1088 blob_appendf(&sql, "%c'ci'", cSep);
1089 cSep = ',';
1090 }
1091 if( g.perm.RdWiki ){
1092 blob_appendf(&sql, "%c'w','e'", cSep);
1093 cSep = ',';
@@ -1107,10 +1118,12 @@
1107 zEType = "wiki edit";
1108 }else if( zType[0]=='t' ){
1109 zEType = "ticket change";
1110 }else if( zType[0]=='e' ){
1111 zEType = "event";
 
 
1112 }
1113 }
1114 if( zUser ){
1115 blob_appendf(&sql, " AND (event.user=%Q OR event.euser=%Q)",
1116 zUser, zUser);
@@ -1226,10 +1239,13 @@
1226 timeline_submenu(&url, "Tickets Only", "y", "t", 0);
1227 }
1228 if( zType[0]!='e' && g.perm.RdWiki ){
1229 timeline_submenu(&url, "Events Only", "y", "e", 0);
1230 }
 
 
 
1231 }
1232 if( nEntry>20 ){
1233 timeline_submenu(&url, "20 Entries", "n", "20", 0);
1234 }
1235 if( nEntry<200 ){
1236
--- src/timeline.c
+++ src/timeline.c
@@ -179,11 +179,11 @@
179 ** 2. Date/Time
180 ** 3. Comment string
181 ** 4. User
182 ** 5. True if is a leaf
183 ** 6. background color
184 ** 7. type ("ci", "w", "t", "e", "g", "div")
185 ** 8. list of symbolic tags.
186 ** 9. tagid for ticket or wiki or event
187 ** 10. Short comment to user for repeated tickets and wiki
188 */
189 void www_print_timeline(
@@ -316,10 +316,13 @@
316 @</td>
317 if( zBgClr && zBgClr[0] ){
318 @ <td class="timelineTableCell" style="background-color: %h(zBgClr);">
319 }else{
320 @ <td class="timelineTableCell">
321 }
322 if( pGraph && zType[0]!='c' ){
323 @ &bull;
324 }
325 if( zType[0]=='c' ){
326 hyperlink_to_uuid(zUuid);
327 if( isLeaf ){
328 if( db_exists("SELECT 1 FROM tagxref"
@@ -840,23 +843,24 @@
843 **
844 ** a=TIMESTAMP after this date
845 ** b=TIMESTAMP before this date.
846 ** c=TIMESTAMP "circa" this date.
847 ** n=COUNT number of events in output
848 ** p=UUID artifact and up to COUNT parents and ancestors
849 ** d=UUID artifact and up to COUNT descendants
850 ** dp=UUUID The same as d=UUID&p=UUID
851 ** t=TAGID show only check-ins with the given tagid
852 ** r=TAGID show check-ins related to tagid
853 ** u=USER only if belonging to this user
854 ** y=TYPE 'ci', 'w', 't', 'e'
855 ** s=TEXT string search (comment and brief)
856 ** ng Suppress the graph if present
857 ** nd Suppress "divider" lines
858 ** fc Show details of files changed
859 ** f=UUID Show family (immediate parents and children) of UUID
860 ** from=UUID Path from...
861 ** to=UUID ... to this
862 ** nomerge ... avoid merge links on the path
863 ** brbg Background color from branch name
864 ** ubg Background color from user
865 **
866 ** p= and d= can appear individually or together. If either p= or d=
@@ -892,15 +896,23 @@
896 int from_rid = name_to_typed_rid(P("from"),"ci"); /* from= for paths */
897 int to_rid = name_to_typed_rid(P("to"),"ci"); /* to= for path timelines */
898 int noMerge = P("nomerge")!=0; /* Do not follow merge links */
899 int me_rid = name_to_typed_rid(P("me"),"ci"); /* me= for common ancestory */
900 int you_rid = name_to_typed_rid(P("you"),"ci");/* you= for common ancst */
901 int pd_rid;
902
903 /* To view the timeline, must have permission to read project data.
904 */
905 pd_rid = name_to_typed_rid(P("dp"),"ci");
906 if( pd_rid ){
907 p_rid = d_rid = pd_rid;
908 }
909 login_check_credentials();
910 if( !g.perm.Read && !g.perm.RdTkt && !g.perm.RdWiki ){
911 login_needed();
912 return;
913 }
914 if( zTagName && g.perm.Read ){
915 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'", zTagName);
916 zThisTag = zTagName;
917 }else if( zBrName && g.perm.Read ){
918 tagid = db_int(0, "SELECT tagid FROM tag WHERE tagname='sym-%q'",zBrName);
@@ -988,14 +1000,12 @@
1000 blob_appendf(&sql, " AND event.objid IN ok");
1001 nd = 0;
1002 if( d_rid ){
1003 compute_descendants(d_rid, nEntry+1);
1004 nd = db_int(0, "SELECT count(*)-1 FROM ok");
1005 if( nd>=0 ) db_multi_exec("%s", blob_str(&sql));
1006 if( nd>0 ) blob_appendf(&desc, "%d descendant%s", nd,(1==nd)?"":"s");
 
 
1007 if( useDividers ) timeline_add_dividers(0, d_rid);
1008 db_multi_exec("DELETE FROM ok");
1009 }
1010 if( p_rid ){
1011 compute_ancestors(p_rid, nEntry+1);
@@ -1075,19 +1085,20 @@
1085 }
1086 if( (zType[0]=='w' && !g.perm.RdWiki)
1087 || (zType[0]=='t' && !g.perm.RdTkt)
1088 || (zType[0]=='e' && !g.perm.RdWiki)
1089 || (zType[0]=='c' && !g.perm.Read)
1090 || (zType[0]=='g' && !g.perm.Read)
1091 ){
1092 zType = "all";
1093 }
1094 if( zType[0]=='a' ){
1095 if( !g.perm.Read || !g.perm.RdWiki || !g.perm.RdTkt ){
1096 char cSep = '(';
1097 blob_appendf(&sql, " AND event.type IN ");
1098 if( g.perm.Read ){
1099 blob_appendf(&sql, "%c'ci','g'", cSep);
1100 cSep = ',';
1101 }
1102 if( g.perm.RdWiki ){
1103 blob_appendf(&sql, "%c'w','e'", cSep);
1104 cSep = ',';
@@ -1107,10 +1118,12 @@
1118 zEType = "wiki edit";
1119 }else if( zType[0]=='t' ){
1120 zEType = "ticket change";
1121 }else if( zType[0]=='e' ){
1122 zEType = "event";
1123 }else if( zType[0]=='g' ){
1124 zEType = "tag";
1125 }
1126 }
1127 if( zUser ){
1128 blob_appendf(&sql, " AND (event.user=%Q OR event.euser=%Q)",
1129 zUser, zUser);
@@ -1226,10 +1239,13 @@
1239 timeline_submenu(&url, "Tickets Only", "y", "t", 0);
1240 }
1241 if( zType[0]!='e' && g.perm.RdWiki ){
1242 timeline_submenu(&url, "Events Only", "y", "e", 0);
1243 }
1244 if( zType[0]!='g' && g.perm.Read ){
1245 timeline_submenu(&url, "Tags Only", "y", "g", 0);
1246 }
1247 }
1248 if( nEntry>20 ){
1249 timeline_submenu(&url, "20 Entries", "n", "20", 0);
1250 }
1251 if( nEntry<200 ){
1252
-2
--- src/tkt.c
+++ src/tkt.c
@@ -170,11 +170,10 @@
170170
*/
171171
int ticket_insert(const Manifest *p, int createFlag, int rid){
172172
Blob sql;
173173
Stmt q;
174174
int i;
175
- const char *zSep;
176175
int rc = 0;
177176
178177
getAllTicketFields();
179178
if( createFlag ){
180179
db_multi_exec("INSERT OR IGNORE INTO ticket(tkt_uuid, tkt_mtime) "
@@ -181,11 +180,10 @@
181180
"VALUES(%Q, 0)", p->zTicketUuid);
182181
rc = db_changes();
183182
}
184183
blob_zero(&sql);
185184
blob_appendf(&sql, "UPDATE OR REPLACE ticket SET tkt_mtime=:mtime");
186
- zSep = "SET";
187185
for(i=0; i<p->nField; i++){
188186
const char *zName = p->aField[i].zName;
189187
if( zName[0]=='+' ){
190188
zName++;
191189
if( fieldId(zName)<0 ) continue;
192190
--- src/tkt.c
+++ src/tkt.c
@@ -170,11 +170,10 @@
170 */
171 int ticket_insert(const Manifest *p, int createFlag, int rid){
172 Blob sql;
173 Stmt q;
174 int i;
175 const char *zSep;
176 int rc = 0;
177
178 getAllTicketFields();
179 if( createFlag ){
180 db_multi_exec("INSERT OR IGNORE INTO ticket(tkt_uuid, tkt_mtime) "
@@ -181,11 +180,10 @@
181 "VALUES(%Q, 0)", p->zTicketUuid);
182 rc = db_changes();
183 }
184 blob_zero(&sql);
185 blob_appendf(&sql, "UPDATE OR REPLACE ticket SET tkt_mtime=:mtime");
186 zSep = "SET";
187 for(i=0; i<p->nField; i++){
188 const char *zName = p->aField[i].zName;
189 if( zName[0]=='+' ){
190 zName++;
191 if( fieldId(zName)<0 ) continue;
192
--- src/tkt.c
+++ src/tkt.c
@@ -170,11 +170,10 @@
170 */
171 int ticket_insert(const Manifest *p, int createFlag, int rid){
172 Blob sql;
173 Stmt q;
174 int i;
 
175 int rc = 0;
176
177 getAllTicketFields();
178 if( createFlag ){
179 db_multi_exec("INSERT OR IGNORE INTO ticket(tkt_uuid, tkt_mtime) "
@@ -181,11 +180,10 @@
180 "VALUES(%Q, 0)", p->zTicketUuid);
181 rc = db_changes();
182 }
183 blob_zero(&sql);
184 blob_appendf(&sql, "UPDATE OR REPLACE ticket SET tkt_mtime=:mtime");
 
185 for(i=0; i<p->nField; i++){
186 const char *zName = p->aField[i].zName;
187 if( zName[0]=='+' ){
188 zName++;
189 if( fieldId(zName)<0 ) continue;
190
+21 -10
--- src/update.c
+++ src/update.c
@@ -96,10 +96,11 @@
9696
int debugFlag; /* --debug option */
9797
int nChng; /* Number of file renames */
9898
int *aChng; /* Array of file renames */
9999
int i; /* Loop counter */
100100
int nConflict = 0; /* Number of merge conflicts */
101
+ int nOverwrite = 0; /* Number of unmanaged files overwritten */
101102
Stmt mtimeXfer; /* Statment to transfer mtimes */
102103
103104
if( !internalUpdate ){
104105
undo_capture_command_line();
105106
url_proxy_options();
@@ -302,11 +303,11 @@
302303
blob_zero(&sql);
303304
blob_append(&sql, "DELETE FROM fv WHERE ", -1);
304305
zSep = "";
305306
for(i=3; i<g.argc; i++){
306307
file_tree_name(g.argv[i], &treename, 1);
307
- if( file_isdir(g.argv[i])==1 ){
308
+ if( file_wd_isdir(g.argv[i])==1 ){
308309
if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
309310
blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
310311
}else{
311312
blob_reset(&sql);
312313
break;
@@ -359,11 +360,16 @@
359360
*/
360361
fossil_print("CONFLICT %s\n", zName);
361362
nConflict++;
362363
}else if( idt>0 && idv==0 ){
363364
/* File added in the target. */
364
- fossil_print("ADD %s\n", zName);
365
+ if( file_wd_isfile_or_link(zFullPath) ){
366
+ fossil_print("ADD %s (overwrites an unmanaged file)\n", zName);
367
+ nOverwrite++;
368
+ }else{
369
+ fossil_print("ADD %s\n", zName);
370
+ }
365371
undo_save(zName);
366372
if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
367373
}else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
368374
/* The file is unedited. Change it to the target version */
369375
undo_save(zName);
@@ -449,17 +455,22 @@
449455
fossil_print("--------------\n");
450456
show_common_info(tid, "updated-to:", 1, 0);
451457
452458
/* Report on conflicts
453459
*/
454
- if( nConflict && !nochangeFlag ){
455
- if( internalUpdate ){
456
- internalConflictCnt = nConflict;
457
- }else{
458
- fossil_print(
459
- "WARNING: %d merge conflicts - see messages above for details.\n",
460
- nConflict);
460
+ if( !nochangeFlag ){
461
+ if( nConflict ){
462
+ if( internalUpdate ){
463
+ internalConflictCnt = nConflict;
464
+ nConflict = 0;
465
+ }else{
466
+ fossil_print("WARNING: %d merge conflicts", nConflict);
467
+ }
468
+ }
469
+ if( nOverwrite ){
470
+ fossil_warning("WARNING: %d unmanaged files were overwritten",
471
+ nOverwrite);
461472
}
462473
}
463474
464475
/*
465476
** Clean up the mid and pid VFILE entries. Then commit the changes.
@@ -513,11 +524,11 @@
513524
514525
blob_zero(&path);
515526
blob_appendf(&path, "%s/%s", g.zLocalRoot, zDir);
516527
zPath = blob_str(&path);
517528
/* Handle various cases of existence of the directory */
518
- switch( file_isdir(zPath) ){
529
+ switch( file_wd_isdir(zPath) ){
519530
case 0: { /* doesn't exist */
520531
if( file_mkdir(zPath, 0)!=0 ) {
521532
fossil_warning("couldn't create directory %s as "
522533
"required by empty-dirs setting", zDir);
523534
}
524535
--- src/update.c
+++ src/update.c
@@ -96,10 +96,11 @@
96 int debugFlag; /* --debug option */
97 int nChng; /* Number of file renames */
98 int *aChng; /* Array of file renames */
99 int i; /* Loop counter */
100 int nConflict = 0; /* Number of merge conflicts */
 
101 Stmt mtimeXfer; /* Statment to transfer mtimes */
102
103 if( !internalUpdate ){
104 undo_capture_command_line();
105 url_proxy_options();
@@ -302,11 +303,11 @@
302 blob_zero(&sql);
303 blob_append(&sql, "DELETE FROM fv WHERE ", -1);
304 zSep = "";
305 for(i=3; i<g.argc; i++){
306 file_tree_name(g.argv[i], &treename, 1);
307 if( file_isdir(g.argv[i])==1 ){
308 if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
309 blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
310 }else{
311 blob_reset(&sql);
312 break;
@@ -359,11 +360,16 @@
359 */
360 fossil_print("CONFLICT %s\n", zName);
361 nConflict++;
362 }else if( idt>0 && idv==0 ){
363 /* File added in the target. */
364 fossil_print("ADD %s\n", zName);
 
 
 
 
 
365 undo_save(zName);
366 if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
367 }else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
368 /* The file is unedited. Change it to the target version */
369 undo_save(zName);
@@ -449,17 +455,22 @@
449 fossil_print("--------------\n");
450 show_common_info(tid, "updated-to:", 1, 0);
451
452 /* Report on conflicts
453 */
454 if( nConflict && !nochangeFlag ){
455 if( internalUpdate ){
456 internalConflictCnt = nConflict;
457 }else{
458 fossil_print(
459 "WARNING: %d merge conflicts - see messages above for details.\n",
460 nConflict);
 
 
 
 
 
461 }
462 }
463
464 /*
465 ** Clean up the mid and pid VFILE entries. Then commit the changes.
@@ -513,11 +524,11 @@
513
514 blob_zero(&path);
515 blob_appendf(&path, "%s/%s", g.zLocalRoot, zDir);
516 zPath = blob_str(&path);
517 /* Handle various cases of existence of the directory */
518 switch( file_isdir(zPath) ){
519 case 0: { /* doesn't exist */
520 if( file_mkdir(zPath, 0)!=0 ) {
521 fossil_warning("couldn't create directory %s as "
522 "required by empty-dirs setting", zDir);
523 }
524
--- src/update.c
+++ src/update.c
@@ -96,10 +96,11 @@
96 int debugFlag; /* --debug option */
97 int nChng; /* Number of file renames */
98 int *aChng; /* Array of file renames */
99 int i; /* Loop counter */
100 int nConflict = 0; /* Number of merge conflicts */
101 int nOverwrite = 0; /* Number of unmanaged files overwritten */
102 Stmt mtimeXfer; /* Statment to transfer mtimes */
103
104 if( !internalUpdate ){
105 undo_capture_command_line();
106 url_proxy_options();
@@ -302,11 +303,11 @@
303 blob_zero(&sql);
304 blob_append(&sql, "DELETE FROM fv WHERE ", -1);
305 zSep = "";
306 for(i=3; i<g.argc; i++){
307 file_tree_name(g.argv[i], &treename, 1);
308 if( file_wd_isdir(g.argv[i])==1 ){
309 if( blob_size(&treename) != 1 || blob_str(&treename)[0] != '.' ){
310 blob_appendf(&sql, "%sfn NOT GLOB '%b/*' ", zSep, &treename);
311 }else{
312 blob_reset(&sql);
313 break;
@@ -359,11 +360,16 @@
360 */
361 fossil_print("CONFLICT %s\n", zName);
362 nConflict++;
363 }else if( idt>0 && idv==0 ){
364 /* File added in the target. */
365 if( file_wd_isfile_or_link(zFullPath) ){
366 fossil_print("ADD %s (overwrites an unmanaged file)\n", zName);
367 nOverwrite++;
368 }else{
369 fossil_print("ADD %s\n", zName);
370 }
371 undo_save(zName);
372 if( !nochangeFlag ) vfile_to_disk(0, idt, 0, 0);
373 }else if( idt>0 && idv>0 && ridt!=ridv && chnged==0 ){
374 /* The file is unedited. Change it to the target version */
375 undo_save(zName);
@@ -449,17 +455,22 @@
455 fossil_print("--------------\n");
456 show_common_info(tid, "updated-to:", 1, 0);
457
458 /* Report on conflicts
459 */
460 if( !nochangeFlag ){
461 if( nConflict ){
462 if( internalUpdate ){
463 internalConflictCnt = nConflict;
464 nConflict = 0;
465 }else{
466 fossil_print("WARNING: %d merge conflicts", nConflict);
467 }
468 }
469 if( nOverwrite ){
470 fossil_warning("WARNING: %d unmanaged files were overwritten",
471 nOverwrite);
472 }
473 }
474
475 /*
476 ** Clean up the mid and pid VFILE entries. Then commit the changes.
@@ -513,11 +524,11 @@
524
525 blob_zero(&path);
526 blob_appendf(&path, "%s/%s", g.zLocalRoot, zDir);
527 zPath = blob_str(&path);
528 /* Handle various cases of existence of the directory */
529 switch( file_wd_isdir(zPath) ){
530 case 0: { /* doesn't exist */
531 if( file_mkdir(zPath, 0)!=0 ) {
532 fossil_warning("couldn't create directory %s as "
533 "required by empty-dirs setting", zDir);
534 }
535
+2 -2
--- src/vfile.c
+++ src/vfile.c
@@ -272,11 +272,11 @@
272272
blob_reset(&content);
273273
continue;
274274
}
275275
}
276276
if( verbose ) fossil_print("%s\n", &zName[nRepos]);
277
- if( file_isdir(zName) == 1 ){
277
+ if( file_wd_isdir(zName) == 1 ){
278278
/*TODO(dchest): remove directories? */
279279
fossil_fatal("%s is directory, cannot overwrite\n", zName);
280280
}
281281
if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(zName)) ){
282282
file_delete(zName);
@@ -389,11 +389,11 @@
389389
blob_appendf(pPath, "/%s", zUtf8);
390390
fossil_mbcs_free(zUtf8);
391391
zPath = blob_str(pPath);
392392
if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
393393
/* do nothing */
394
- }else if( file_isdir(zPath)==1 ){
394
+ }else if( file_wd_isdir(zPath)==1 ){
395395
if( !vfile_top_of_checkout(zPath) ){
396396
vfile_scan(pPath, nPrefix, allFlag, pIgnore);
397397
}
398398
}else if( file_wd_isfile_or_link(zPath) ){
399399
db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
400400
--- src/vfile.c
+++ src/vfile.c
@@ -272,11 +272,11 @@
272 blob_reset(&content);
273 continue;
274 }
275 }
276 if( verbose ) fossil_print("%s\n", &zName[nRepos]);
277 if( file_isdir(zName) == 1 ){
278 /*TODO(dchest): remove directories? */
279 fossil_fatal("%s is directory, cannot overwrite\n", zName);
280 }
281 if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(zName)) ){
282 file_delete(zName);
@@ -389,11 +389,11 @@
389 blob_appendf(pPath, "/%s", zUtf8);
390 fossil_mbcs_free(zUtf8);
391 zPath = blob_str(pPath);
392 if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
393 /* do nothing */
394 }else if( file_isdir(zPath)==1 ){
395 if( !vfile_top_of_checkout(zPath) ){
396 vfile_scan(pPath, nPrefix, allFlag, pIgnore);
397 }
398 }else if( file_wd_isfile_or_link(zPath) ){
399 db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
400
--- src/vfile.c
+++ src/vfile.c
@@ -272,11 +272,11 @@
272 blob_reset(&content);
273 continue;
274 }
275 }
276 if( verbose ) fossil_print("%s\n", &zName[nRepos]);
277 if( file_wd_isdir(zName) == 1 ){
278 /*TODO(dchest): remove directories? */
279 fossil_fatal("%s is directory, cannot overwrite\n", zName);
280 }
281 if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(zName)) ){
282 file_delete(zName);
@@ -389,11 +389,11 @@
389 blob_appendf(pPath, "/%s", zUtf8);
390 fossil_mbcs_free(zUtf8);
391 zPath = blob_str(pPath);
392 if( glob_match(pIgnore, &zPath[nPrefix+1]) ){
393 /* do nothing */
394 }else if( file_wd_isdir(zPath)==1 ){
395 if( !vfile_top_of_checkout(zPath) ){
396 vfile_scan(pPath, nPrefix, allFlag, pIgnore);
397 }
398 }else if( file_wd_isfile_or_link(zPath) ){
399 db_bind_text(&ins, ":file", &zPath[nPrefix+1]);
400
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1708,20 +1708,18 @@
17081708
/* Enter <verbatim> processing. With verbatim enabled, all other
17091709
** markup other than the corresponding end-tag with the same ID is
17101710
** ignored.
17111711
*/
17121712
if( markup.iCode==MARKUP_VERBATIM ){
1713
- int vAttrIdx, vAttrDidAppend=0;
1713
+ int vAttrIdx;
17141714
renderer.zVerbatimId = 0;
17151715
renderer.inVerbatim = 1;
17161716
renderer.preVerbState = renderer.state;
17171717
renderer.state &= ~ALLOW_WIKI;
17181718
for (vAttrIdx = 0; vAttrIdx < markup.nAttr; vAttrIdx++){
17191719
if( markup.aAttr[vAttrIdx].iACode == ATTR_ID ){
17201720
renderer.zVerbatimId = markup.aAttr[0].zValue;
1721
- }else if( markup.aAttr[vAttrIdx].iACode == ATTR_TYPE ){
1722
- vAttrDidAppend=1;
17231721
}
17241722
}
17251723
renderer.wantAutoParagraph = 0;
17261724
}
17271725
17281726
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1708,20 +1708,18 @@
1708 /* Enter <verbatim> processing. With verbatim enabled, all other
1709 ** markup other than the corresponding end-tag with the same ID is
1710 ** ignored.
1711 */
1712 if( markup.iCode==MARKUP_VERBATIM ){
1713 int vAttrIdx, vAttrDidAppend=0;
1714 renderer.zVerbatimId = 0;
1715 renderer.inVerbatim = 1;
1716 renderer.preVerbState = renderer.state;
1717 renderer.state &= ~ALLOW_WIKI;
1718 for (vAttrIdx = 0; vAttrIdx < markup.nAttr; vAttrIdx++){
1719 if( markup.aAttr[vAttrIdx].iACode == ATTR_ID ){
1720 renderer.zVerbatimId = markup.aAttr[0].zValue;
1721 }else if( markup.aAttr[vAttrIdx].iACode == ATTR_TYPE ){
1722 vAttrDidAppend=1;
1723 }
1724 }
1725 renderer.wantAutoParagraph = 0;
1726 }
1727
1728
--- src/wikiformat.c
+++ src/wikiformat.c
@@ -1708,20 +1708,18 @@
1708 /* Enter <verbatim> processing. With verbatim enabled, all other
1709 ** markup other than the corresponding end-tag with the same ID is
1710 ** ignored.
1711 */
1712 if( markup.iCode==MARKUP_VERBATIM ){
1713 int vAttrIdx;
1714 renderer.zVerbatimId = 0;
1715 renderer.inVerbatim = 1;
1716 renderer.preVerbState = renderer.state;
1717 renderer.state &= ~ALLOW_WIKI;
1718 for (vAttrIdx = 0; vAttrIdx < markup.nAttr; vAttrIdx++){
1719 if( markup.aAttr[vAttrIdx].iACode == ATTR_ID ){
1720 renderer.zVerbatimId = markup.aAttr[0].zValue;
 
 
1721 }
1722 }
1723 renderer.wantAutoParagraph = 0;
1724 }
1725
1726
+3 -6
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573573
blob_zero(&combined);
574574
blob_copy(&combined, pNonce);
575575
blob_append(&combined, blob_buffer(&pw), szPw);
576576
sha1sum_blob(&combined, &hash);
577577
assert( blob_size(&hash)==40 );
578
- rc = blob_compare(&hash, pSig);
578
+ rc = blob_constant_time_cmp(&hash, pSig);
579579
blob_reset(&hash);
580580
blob_reset(&combined);
581581
if( rc!=0 && szPw!=40 ){
582582
/* If this server stores cleartext passwords and the password did not
583583
** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588588
blob_zero(&combined);
589589
blob_copy(&combined, pNonce);
590590
blob_append(&combined, zSecret, -1);
591591
free(zSecret);
592592
sha1sum_blob(&combined, &hash);
593
- rc = blob_compare(&hash, pSig);
593
+ rc = blob_constant_time_cmp(&hash, pSig);
594594
blob_reset(&hash);
595595
blob_reset(&combined);
596596
}
597597
if( rc==0 ){
598598
const char *zCap;
@@ -1198,20 +1198,19 @@
11981198
**
11991199
** gdb fossil
12001200
** r test-xfer out.txt
12011201
*/
12021202
void cmd_test_xfer(void){
1203
- int notUsed;
12041203
db_find_and_open_repository(0,0);
12051204
if( g.argc!=2 && g.argc!=3 ){
12061205
usage("?MESSAGEFILE?");
12071206
}
12081207
blob_zero(&g.cgiIn);
12091208
blob_read_from_file(&g.cgiIn, g.argc==2 ? "-" : g.argv[2]);
12101209
disableLogin = 1;
12111210
page_xfer();
1212
- fossil_print("%s\n", cgi_extract_content(&notUsed));
1211
+ fossil_print("%s\n", cgi_extract_content());
12131212
}
12141213
12151214
/*
12161215
** Format strings for progress reporting.
12171216
*/
@@ -1238,11 +1237,10 @@
12381237
int go = 1; /* Loop until zero */
12391238
int nCardSent = 0; /* Number of cards sent */
12401239
int nCardRcvd = 0; /* Number of cards received */
12411240
int nCycle = 0; /* Number of round trips to the server */
12421241
int size; /* Size of a config value */
1243
- int nFileSend = 0;
12441242
int origConfigRcvMask; /* Original value of configRcvMask */
12451243
int nFileRecv; /* Number of files received */
12461244
int mxPhantomReq = 200; /* Max number of phantoms to request per comm */
12471245
const char *zCookie; /* Server cookie */
12481246
i64 nSent, nRcvd; /* Bytes sent and received (after compression) */
@@ -1380,11 +1378,10 @@
13801378
zRandomness = db_text(0, "SELECT hex(randomblob(20))");
13811379
blob_appendf(&send, "# %s\n", zRandomness);
13821380
free(zRandomness);
13831381
13841382
/* Exchange messages with the server */
1385
- nFileSend = xfer.nFileSent + xfer.nDeltaSent;
13861383
fossil_print(zValueFormat, "Sent:",
13871384
blob_size(&send), nCardSent+xfer.nGimmeSent+xfer.nIGotSent,
13881385
xfer.nFileSent, xfer.nDeltaSent);
13891386
nCardSent = 0;
13901387
nCardRcvd = 0;
13911388
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573 blob_zero(&combined);
574 blob_copy(&combined, pNonce);
575 blob_append(&combined, blob_buffer(&pw), szPw);
576 sha1sum_blob(&combined, &hash);
577 assert( blob_size(&hash)==40 );
578 rc = blob_compare(&hash, pSig);
579 blob_reset(&hash);
580 blob_reset(&combined);
581 if( rc!=0 && szPw!=40 ){
582 /* If this server stores cleartext passwords and the password did not
583 ** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588 blob_zero(&combined);
589 blob_copy(&combined, pNonce);
590 blob_append(&combined, zSecret, -1);
591 free(zSecret);
592 sha1sum_blob(&combined, &hash);
593 rc = blob_compare(&hash, pSig);
594 blob_reset(&hash);
595 blob_reset(&combined);
596 }
597 if( rc==0 ){
598 const char *zCap;
@@ -1198,20 +1198,19 @@
1198 **
1199 ** gdb fossil
1200 ** r test-xfer out.txt
1201 */
1202 void cmd_test_xfer(void){
1203 int notUsed;
1204 db_find_and_open_repository(0,0);
1205 if( g.argc!=2 && g.argc!=3 ){
1206 usage("?MESSAGEFILE?");
1207 }
1208 blob_zero(&g.cgiIn);
1209 blob_read_from_file(&g.cgiIn, g.argc==2 ? "-" : g.argv[2]);
1210 disableLogin = 1;
1211 page_xfer();
1212 fossil_print("%s\n", cgi_extract_content(&notUsed));
1213 }
1214
1215 /*
1216 ** Format strings for progress reporting.
1217 */
@@ -1238,11 +1237,10 @@
1238 int go = 1; /* Loop until zero */
1239 int nCardSent = 0; /* Number of cards sent */
1240 int nCardRcvd = 0; /* Number of cards received */
1241 int nCycle = 0; /* Number of round trips to the server */
1242 int size; /* Size of a config value */
1243 int nFileSend = 0;
1244 int origConfigRcvMask; /* Original value of configRcvMask */
1245 int nFileRecv; /* Number of files received */
1246 int mxPhantomReq = 200; /* Max number of phantoms to request per comm */
1247 const char *zCookie; /* Server cookie */
1248 i64 nSent, nRcvd; /* Bytes sent and received (after compression) */
@@ -1380,11 +1378,10 @@
1380 zRandomness = db_text(0, "SELECT hex(randomblob(20))");
1381 blob_appendf(&send, "# %s\n", zRandomness);
1382 free(zRandomness);
1383
1384 /* Exchange messages with the server */
1385 nFileSend = xfer.nFileSent + xfer.nDeltaSent;
1386 fossil_print(zValueFormat, "Sent:",
1387 blob_size(&send), nCardSent+xfer.nGimmeSent+xfer.nIGotSent,
1388 xfer.nFileSent, xfer.nDeltaSent);
1389 nCardSent = 0;
1390 nCardRcvd = 0;
1391
--- src/xfer.c
+++ src/xfer.c
@@ -573,11 +573,11 @@
573 blob_zero(&combined);
574 blob_copy(&combined, pNonce);
575 blob_append(&combined, blob_buffer(&pw), szPw);
576 sha1sum_blob(&combined, &hash);
577 assert( blob_size(&hash)==40 );
578 rc = blob_constant_time_cmp(&hash, pSig);
579 blob_reset(&hash);
580 blob_reset(&combined);
581 if( rc!=0 && szPw!=40 ){
582 /* If this server stores cleartext passwords and the password did not
583 ** match, then perhaps the client is sending SHA1 passwords. Try
@@ -588,11 +588,11 @@
588 blob_zero(&combined);
589 blob_copy(&combined, pNonce);
590 blob_append(&combined, zSecret, -1);
591 free(zSecret);
592 sha1sum_blob(&combined, &hash);
593 rc = blob_constant_time_cmp(&hash, pSig);
594 blob_reset(&hash);
595 blob_reset(&combined);
596 }
597 if( rc==0 ){
598 const char *zCap;
@@ -1198,20 +1198,19 @@
1198 **
1199 ** gdb fossil
1200 ** r test-xfer out.txt
1201 */
1202 void cmd_test_xfer(void){
 
1203 db_find_and_open_repository(0,0);
1204 if( g.argc!=2 && g.argc!=3 ){
1205 usage("?MESSAGEFILE?");
1206 }
1207 blob_zero(&g.cgiIn);
1208 blob_read_from_file(&g.cgiIn, g.argc==2 ? "-" : g.argv[2]);
1209 disableLogin = 1;
1210 page_xfer();
1211 fossil_print("%s\n", cgi_extract_content());
1212 }
1213
1214 /*
1215 ** Format strings for progress reporting.
1216 */
@@ -1238,11 +1237,10 @@
1237 int go = 1; /* Loop until zero */
1238 int nCardSent = 0; /* Number of cards sent */
1239 int nCardRcvd = 0; /* Number of cards received */
1240 int nCycle = 0; /* Number of round trips to the server */
1241 int size; /* Size of a config value */
 
1242 int origConfigRcvMask; /* Original value of configRcvMask */
1243 int nFileRecv; /* Number of files received */
1244 int mxPhantomReq = 200; /* Max number of phantoms to request per comm */
1245 const char *zCookie; /* Server cookie */
1246 i64 nSent, nRcvd; /* Bytes sent and received (after compression) */
@@ -1380,11 +1378,10 @@
1378 zRandomness = db_text(0, "SELECT hex(randomblob(20))");
1379 blob_appendf(&send, "# %s\n", zRandomness);
1380 free(zRandomness);
1381
1382 /* Exchange messages with the server */
 
1383 fossil_print(zValueFormat, "Sent:",
1384 blob_size(&send), nCardSent+xfer.nGimmeSent+xfer.nIGotSent,
1385 xfer.nFileSent, xfer.nDeltaSent);
1386 nCardSent = 0;
1387 nCardRcvd = 0;
1388
--- test/merge_renames.test
+++ test/merge_renames.test
@@ -3,11 +3,11 @@
33
#
44
#
55
66
catch {exec $::fossilexe info} res
77
puts res=$res
8
-if {![regexp {not within an open checkout} $res]} {
8
+if {![regexp {use --repository} $res]} {
99
puts stderr "Cannot run this test within an open checkout"
1010
return
1111
}
1212
1313
######################################
1414
--- test/merge_renames.test
+++ test/merge_renames.test
@@ -3,11 +3,11 @@
3 #
4 #
5
6 catch {exec $::fossilexe info} res
7 puts res=$res
8 if {![regexp {not within an open checkout} $res]} {
9 puts stderr "Cannot run this test within an open checkout"
10 return
11 }
12
13 ######################################
14
--- test/merge_renames.test
+++ test/merge_renames.test
@@ -3,11 +3,11 @@
3 #
4 #
5
6 catch {exec $::fossilexe info} res
7 puts res=$res
8 if {![regexp {use --repository} $res]} {
9 puts stderr "Cannot run this test within an open checkout"
10 return
11 }
12
13 ######################################
14
--- www/fossil-v-git.wiki
+++ www/fossil-v-git.wiki
@@ -19,11 +19,11 @@
1919
<blockquote><center><table border=1 cellpadding=5>
2020
<tr><th width="50%">GIT</th><th width="50%">FOSSIL</th></tr>
2121
<tr><td>File versioning only</td>
2222
<td>Versioning, Tickets, Wiki, and Blog/News</td></tr>
2323
<tr><td>Sharding</td><td>Replicating</td></tr>
24
-<tr><td>Huge community</td><td>Road less traveled</td></tr>
24
+<tr><td>Developer branches</td><td>Feature branches</td></tr>
2525
<tr><td>Complex</td><td>Intuitive</td></tr>
2626
<tr><td>Separate web tools</td><td>Integrated Web interface</td></tr>
2727
<tr><td>Lots of little tools</td><td>Single executable</td></tr>
2828
<tr><td>Pile-of-files repository</td><td>Single file repository</td></tr>
2929
<tr><td>Uses "<tt>rebase</tt>"</td><td>Immutable</td></tr>
@@ -79,38 +79,49 @@
7979
The [concepts.wiki#workflow | autosync] mode of Fossil makes it easy
8080
for multiple developers to work on a single branch and maintain
8181
linear development on that branch and avoid needless forking
8282
and merging.
8383
84
-<h3>3.3 Community</h3>
85
-
86
-Git has a huge user community. If following the herd and being
87
-like everybody else is important to you, then you should choose Git.
88
-
89
-Fossil is clearly the "road less traveled":
90
-
91
-<blockquote>
92
-Two roads diverged in a wood, and I &#151;<br>
93
-I took the one less traveled by,<br>
94
-And that has made all the difference.<br>
95
-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
96
-<small>- Robert Frost, <i>The Road Not Taken</i>, 1916</small>
97
-</blockquote>
98
-</i></blockquote>
99
-
100
-Among the advantages of Git's huge user community are that new team
101
-members may already be familiar with Git's operation and hence can
102
-bypass the VCS learning curve. Also, if you need an add-on tool or
103
-script of some kind, a Google search will likely turn up a suitable
104
-tool that you can just download and use. A huge community also means
105
-that somebody else has likely already encountered and fixed the bugs
106
-so that Git will work for you and your project as advertised.
107
-
108
-Among the advantages of the "road less traveled" is that your particular
109
-project will be bigger percentage of the total user base, and is thus
110
-more likely to receive personal attention from the Fossil maintainers
111
-if you do encounter problems.
84
+<h3>3.3 Branches</h3>
85
+
86
+Git (and especially GitHub) encourages a workflow where each developer
87
+has his or her own branch or branches. Developers then send "pull requests"
88
+to have their changes be merged into "official" branches by integrators.
89
+For example, the Linux kernel team has a hierarchy of integrators with
90
+Linus Torvalds at the root. Individual developers each have their own
91
+private branches of the source tree into which they make their own changes.
92
+They then encourage first-tier integrators to pull those changes. The
93
+first-tier integrators merge together changes from multiple contributors
94
+then try to get second-tier integrators to pull their branches. The
95
+changes merge up the the hierarchy until (hopefully) they are pulled into
96
+"Linus's branch", at which time they become part of the "official" Linux.
97
+
98
+In Git, each branch is "owned" by the person who creates it and works
99
+on it. The owner might pull changes from others, but the owner is always
100
+in control of the branch. Branches are developer-centric.
101
+
102
+Fossil, on the other hand, encourages a workflow where branches are
103
+associated with features or releases, not individual developers.
104
+All developers share all branches in common, and two
105
+or more developers can and often do intersperse commits onto the same branch.
106
+Branches do not belong to individuals. All branches are read/write
107
+accessible to all developers at all times. There is no need
108
+for integrators to merge together changes from various independent
109
+developers. Instead, all of the developers work together cooperatively
110
+and the changes stay integrated naturally.
111
+
112
+So to a first approximation, branches in Git are developer-centric whereas
113
+branches in Fossil are feature-centric.
114
+
115
+The Git approach scales much better for large projects like the Linux
116
+kernel with thousands of contributors who in many cases don't even know
117
+each others names. The integrators serve a gatekeeper role to help keep
118
+undesirable code out of the official Linux source tree. On the other hand,
119
+not many projects are as big or as loosely organized as the Linux kernel.
120
+Most project, have a small team of developers who all know each other
121
+well and trust each other, and who enjoy working together collaboratively
122
+without the overhead and hierarchy of integrators.
112123
113124
<h3>3.4 Complexity</h3>
114125
115126
Git is a complex system. It can be tricky to use and requires a fair
116127
amount of knowledge and experience to master. Fossil strives to be
117128
--- www/fossil-v-git.wiki
+++ www/fossil-v-git.wiki
@@ -19,11 +19,11 @@
19 <blockquote><center><table border=1 cellpadding=5>
20 <tr><th width="50%">GIT</th><th width="50%">FOSSIL</th></tr>
21 <tr><td>File versioning only</td>
22 <td>Versioning, Tickets, Wiki, and Blog/News</td></tr>
23 <tr><td>Sharding</td><td>Replicating</td></tr>
24 <tr><td>Huge community</td><td>Road less traveled</td></tr>
25 <tr><td>Complex</td><td>Intuitive</td></tr>
26 <tr><td>Separate web tools</td><td>Integrated Web interface</td></tr>
27 <tr><td>Lots of little tools</td><td>Single executable</td></tr>
28 <tr><td>Pile-of-files repository</td><td>Single file repository</td></tr>
29 <tr><td>Uses "<tt>rebase</tt>"</td><td>Immutable</td></tr>
@@ -79,38 +79,49 @@
79 The [concepts.wiki#workflow | autosync] mode of Fossil makes it easy
80 for multiple developers to work on a single branch and maintain
81 linear development on that branch and avoid needless forking
82 and merging.
83
84 <h3>3.3 Community</h3>
85
86 Git has a huge user community. If following the herd and being
87 like everybody else is important to you, then you should choose Git.
88
89 Fossil is clearly the "road less traveled":
90
91 <blockquote>
92 Two roads diverged in a wood, and I &#151;<br>
93 I took the one less traveled by,<br>
94 And that has made all the difference.<br>
95 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
96 <small>- Robert Frost, <i>The Road Not Taken</i>, 1916</small>
97 </blockquote>
98 </i></blockquote>
99
100 Among the advantages of Git's huge user community are that new team
101 members may already be familiar with Git's operation and hence can
102 bypass the VCS learning curve. Also, if you need an add-on tool or
103 script of some kind, a Google search will likely turn up a suitable
104 tool that you can just download and use. A huge community also means
105 that somebody else has likely already encountered and fixed the bugs
106 so that Git will work for you and your project as advertised.
107
108 Among the advantages of the "road less traveled" is that your particular
109 project will be bigger percentage of the total user base, and is thus
110 more likely to receive personal attention from the Fossil maintainers
111 if you do encounter problems.
 
 
 
 
 
 
 
 
 
 
 
112
113 <h3>3.4 Complexity</h3>
114
115 Git is a complex system. It can be tricky to use and requires a fair
116 amount of knowledge and experience to master. Fossil strives to be
117
--- www/fossil-v-git.wiki
+++ www/fossil-v-git.wiki
@@ -19,11 +19,11 @@
19 <blockquote><center><table border=1 cellpadding=5>
20 <tr><th width="50%">GIT</th><th width="50%">FOSSIL</th></tr>
21 <tr><td>File versioning only</td>
22 <td>Versioning, Tickets, Wiki, and Blog/News</td></tr>
23 <tr><td>Sharding</td><td>Replicating</td></tr>
24 <tr><td>Developer branches</td><td>Feature branches</td></tr>
25 <tr><td>Complex</td><td>Intuitive</td></tr>
26 <tr><td>Separate web tools</td><td>Integrated Web interface</td></tr>
27 <tr><td>Lots of little tools</td><td>Single executable</td></tr>
28 <tr><td>Pile-of-files repository</td><td>Single file repository</td></tr>
29 <tr><td>Uses "<tt>rebase</tt>"</td><td>Immutable</td></tr>
@@ -79,38 +79,49 @@
79 The [concepts.wiki#workflow | autosync] mode of Fossil makes it easy
80 for multiple developers to work on a single branch and maintain
81 linear development on that branch and avoid needless forking
82 and merging.
83
84 <h3>3.3 Branches</h3>
85
86 Git (and especially GitHub) encourages a workflow where each developer
87 has his or her own branch or branches. Developers then send "pull requests"
88 to have their changes be merged into "official" branches by integrators.
89 For example, the Linux kernel team has a hierarchy of integrators with
90 Linus Torvalds at the root. Individual developers each have their own
91 private branches of the source tree into which they make their own changes.
92 They then encourage first-tier integrators to pull those changes. The
93 first-tier integrators merge together changes from multiple contributors
94 then try to get second-tier integrators to pull their branches. The
95 changes merge up the the hierarchy until (hopefully) they are pulled into
96 "Linus's branch", at which time they become part of the "official" Linux.
97
98 In Git, each branch is "owned" by the person who creates it and works
99 on it. The owner might pull changes from others, but the owner is always
100 in control of the branch. Branches are developer-centric.
101
102 Fossil, on the other hand, encourages a workflow where branches are
103 associated with features or releases, not individual developers.
104 All developers share all branches in common, and two
105 or more developers can and often do intersperse commits onto the same branch.
106 Branches do not belong to individuals. All branches are read/write
107 accessible to all developers at all times. There is no need
108 for integrators to merge together changes from various independent
109 developers. Instead, all of the developers work together cooperatively
110 and the changes stay integrated naturally.
111
112 So to a first approximation, branches in Git are developer-centric whereas
113 branches in Fossil are feature-centric.
114
115 The Git approach scales much better for large projects like the Linux
116 kernel with thousands of contributors who in many cases don't even know
117 each others names. The integrators serve a gatekeeper role to help keep
118 undesirable code out of the official Linux source tree. On the other hand,
119 not many projects are as big or as loosely organized as the Linux kernel.
120 Most project, have a small team of developers who all know each other
121 well and trust each other, and who enjoy working together collaboratively
122 without the overhead and hierarchy of integrators.
123
124 <h3>3.4 Complexity</h3>
125
126 Git is a complex system. It can be tricky to use and requires a fair
127 amount of knowledge and experience to master. Fossil strives to be
128
--- www/quotes.wiki
+++ www/quotes.wiki
@@ -85,6 +85,19 @@
8585
been a smoother ride than Git was.
8686
8787
<blockquote>
8888
<i>viablepanic at [http://www.reddit.com/r/programming/comments/bxcto/why_not_fossil_scm/]</i>
8989
</blockquote>
90
+
91
+<li>In the fossil community - and hence in fossil itself - development history
92
+is pretty much sacrosanct. The very name "fossil" was to chosen to
93
+reflect the unchanging nature of things in that history.
94
+
95
+<p>In git (or rather, the git community), the development history is part of
96
+the published aspect of the project, so it provides tools for rearranging
97
+that history so you can present what you "should" have done rather
98
+than what you actually did.
99
+
100
+<blockquote>
101
+<i>Mike Meyer on the Fossil mailing list, 2011-10-04</i>
102
+</blockquote>
90103
</ol>
91104
--- www/quotes.wiki
+++ www/quotes.wiki
@@ -85,6 +85,19 @@
85 been a smoother ride than Git was.
86
87 <blockquote>
88 <i>viablepanic at [http://www.reddit.com/r/programming/comments/bxcto/why_not_fossil_scm/]</i>
89 </blockquote>
 
 
 
 
 
 
 
 
 
 
 
 
 
90 </ol>
91
--- www/quotes.wiki
+++ www/quotes.wiki
@@ -85,6 +85,19 @@
85 been a smoother ride than Git was.
86
87 <blockquote>
88 <i>viablepanic at [http://www.reddit.com/r/programming/comments/bxcto/why_not_fossil_scm/]</i>
89 </blockquote>
90
91 <li>In the fossil community - and hence in fossil itself - development history
92 is pretty much sacrosanct. The very name "fossil" was to chosen to
93 reflect the unchanging nature of things in that history.
94
95 <p>In git (or rather, the git community), the development history is part of
96 the published aspect of the project, so it provides tools for rearranging
97 that history so you can present what you "should" have done rather
98 than what you actually did.
99
100 <blockquote>
101 <i>Mike Meyer on the Fossil mailing list, 2011-10-04</i>
102 </blockquote>
103 </ol>
104

Keyboard Shortcuts

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