Fossil SCM

Update the built-in SQLite to the latest trunk version from upstream.

drh 2013-04-25 18:10 trunk
Commit 7d1d9953171d14468ea475e1e1f3987bf715cef5
3 files changed +52 -21 +1962 -227 +78 -16
+52 -21
--- src/shell.c
+++ src/shell.c
@@ -1478,22 +1478,10 @@
14781478
p->zDbFilename, sqlite3_errmsg(db));
14791479
exit(1);
14801480
}
14811481
#ifndef SQLITE_OMIT_LOAD_EXTENSION
14821482
sqlite3_enable_load_extension(p->db, 1);
1483
-#endif
1484
-#ifdef SQLITE_ENABLE_REGEXP
1485
- {
1486
- extern int sqlite3_add_regexp_func(sqlite3*);
1487
- sqlite3_add_regexp_func(db);
1488
- }
1489
-#endif
1490
-#ifdef SQLITE_ENABLE_SPELLFIX
1491
- {
1492
- extern int sqlite3_spellfix1_register(sqlite3*);
1493
- sqlite3_spellfix1_register(db);
1494
- }
14951483
#endif
14961484
}
14971485
}
14981486
14991487
/*
@@ -1549,10 +1537,47 @@
15491537
}
15501538
fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
15511539
zArg);
15521540
return 0;
15531541
}
1542
+
1543
+/*
1544
+** Interpret zArg as an integer value, possibly with suffixes.
1545
+*/
1546
+static sqlite3_int64 integerValue(const char *zArg){
1547
+ sqlite3_int64 v = 0;
1548
+ static const struct { char *zSuffix; int iMult; } aMult[] = {
1549
+ { "KiB", 1024 },
1550
+ { "MiB", 1024*1024 },
1551
+ { "GiB", 1024*1024*1024 },
1552
+ { "KB", 1000 },
1553
+ { "MB", 1000000 },
1554
+ { "GB", 1000000000 },
1555
+ { "K", 1000 },
1556
+ { "M", 1000000 },
1557
+ { "G", 1000000000 },
1558
+ };
1559
+ int i;
1560
+ int isNeg = 0;
1561
+ if( zArg[0]=='-' ){
1562
+ isNeg = 1;
1563
+ zArg++;
1564
+ }else if( zArg[0]=='+' ){
1565
+ zArg++;
1566
+ }
1567
+ while( isdigit(zArg[0]) ){
1568
+ v = v*10 + zArg[0] - '0';
1569
+ zArg++;
1570
+ }
1571
+ for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
1572
+ if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1573
+ v *= aMult[i].iMult;
1574
+ break;
1575
+ }
1576
+ }
1577
+ return isNeg? -v : v;
1578
+}
15541579
15551580
/*
15561581
** Close an output file, assuming it is not stderr or stdout
15571582
*/
15581583
static void output_file_close(FILE *f){
@@ -2467,11 +2492,11 @@
24672492
break;
24682493
24692494
/* sqlite3_test_control(int, uint) */
24702495
case SQLITE_TESTCTRL_PENDING_BYTE:
24712496
if( nArg==3 ){
2472
- unsigned int opt = (unsigned int)atoi(azArg[2]);
2497
+ unsigned int opt = (unsigned int)integerValue(azArg[2]);
24732498
rc = sqlite3_test_control(testctrl, opt);
24742499
printf("%d (0x%08x)\n", rc, rc);
24752500
} else {
24762501
fprintf(stderr,"Error: testctrl %s takes a single unsigned"
24772502
" int option\n", azArg[1]);
@@ -2559,11 +2584,11 @@
25592584
}else
25602585
25612586
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
25622587
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
25632588
extern int sqlite3WhereTrace;
2564
- sqlite3WhereTrace = atoi(azArg[1]);
2589
+ sqlite3WhereTrace = booleanValue(azArg[1]);
25652590
}else
25662591
#endif
25672592
25682593
if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
25692594
int j;
@@ -2745,10 +2770,14 @@
27452770
errCnt++;
27462771
}
27472772
free(zSql);
27482773
zSql = 0;
27492774
nSql = 0;
2775
+ }else if( zSql && _all_whitespace(zSql) ){
2776
+ free(zSql);
2777
+ zSql = 0;
2778
+ nSql = 0;
27502779
}
27512780
}
27522781
if( zSql ){
27532782
if( !_all_whitespace(zSql) ){
27542783
fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
@@ -2880,10 +2909,11 @@
28802909
" -help show this message\n"
28812910
" -html set output mode to HTML\n"
28822911
" -interactive force interactive I/O\n"
28832912
" -line set output mode to 'line'\n"
28842913
" -list set output mode to 'list'\n"
2914
+ " -mmap N default mmap size set to N\n"
28852915
#ifdef SQLITE_ENABLE_MULTIPLEX
28862916
" -multiplex enable the multiplexor VFS\n"
28872917
#endif
28882918
" -nullvalue TEXT set text string for NULL values. Default ''\n"
28892919
" -separator SEP set output field separator. Default: '|'\n"
@@ -2999,16 +3029,11 @@
29993029
int j, c;
30003030
const char *zSize;
30013031
sqlite3_int64 szHeap;
30023032
30033033
zSize = cmdline_option_value(argc, argv, ++i);
3004
- szHeap = atoi(zSize);
3005
- for(j=0; (c = zSize[j])!=0; j++){
3006
- if( c=='M' ){ szHeap *= 1000000; break; }
3007
- if( c=='K' ){ szHeap *= 1000; break; }
3008
- if( c=='G' ){ szHeap *= 1000000000; break; }
3009
- }
3034
+ szHeap = integerValue(zSize);
30103035
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
30113036
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
30123037
#endif
30133038
#ifdef SQLITE_ENABLE_VFSTRACE
30143039
}else if( strcmp(z,"-vfstrace")==0 ){
@@ -3024,10 +3049,13 @@
30243049
#ifdef SQLITE_ENABLE_MULTIPLEX
30253050
}else if( strcmp(z,"-multiplex")==0 ){
30263051
extern int sqlite3_multiple_initialize(const char*,int);
30273052
sqlite3_multiplex_initialize(0, 1);
30283053
#endif
3054
+ }else if( strcmp(z,"-mmap")==0 ){
3055
+ sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
3056
+ sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
30293057
}else if( strcmp(z,"-vfs")==0 ){
30303058
sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
30313059
if( pVfs ){
30323060
sqlite3_vfs_register(pVfs, 1);
30333061
}else{
@@ -3115,10 +3143,12 @@
31153143
stdin_is_interactive = 1;
31163144
}else if( strcmp(z,"-batch")==0 ){
31173145
stdin_is_interactive = 0;
31183146
}else if( strcmp(z,"-heap")==0 ){
31193147
i++;
3148
+ }else if( strcmp(z,"-mmap")==0 ){
3149
+ i++;
31203150
}else if( strcmp(z,"-vfs")==0 ){
31213151
i++;
31223152
#ifdef SQLITE_ENABLE_VFSTRACE
31233153
}else if( strcmp(z,"-vfstrace")==0 ){
31243154
i++;
@@ -3132,11 +3162,11 @@
31323162
}else if( strcmp(z,"-cmd")==0 ){
31333163
if( i==argc-1 ) break;
31343164
z = cmdline_option_value(argc,argv,++i);
31353165
if( z[0]=='.' ){
31363166
rc = do_meta_command(z, &data);
3137
- if( rc && bail_on_error ) return rc;
3167
+ if( rc && bail_on_error ) return rc==2 ? 0 : rc;
31383168
}else{
31393169
open_db(&data);
31403170
rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
31413171
if( zErrMsg!=0 ){
31423172
fprintf(stderr,"Error: %s\n", zErrMsg);
@@ -3156,10 +3186,11 @@
31563186
if( zFirstCmd ){
31573187
/* Run just the command that follows the database name
31583188
*/
31593189
if( zFirstCmd[0]=='.' ){
31603190
rc = do_meta_command(zFirstCmd, &data);
3191
+ if( rc==2 ) rc = 0;
31613192
}else{
31623193
open_db(&data);
31633194
rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
31643195
if( zErrMsg!=0 ){
31653196
fprintf(stderr,"Error: %s\n", zErrMsg);
31663197
--- src/shell.c
+++ src/shell.c
@@ -1478,22 +1478,10 @@
1478 p->zDbFilename, sqlite3_errmsg(db));
1479 exit(1);
1480 }
1481 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1482 sqlite3_enable_load_extension(p->db, 1);
1483 #endif
1484 #ifdef SQLITE_ENABLE_REGEXP
1485 {
1486 extern int sqlite3_add_regexp_func(sqlite3*);
1487 sqlite3_add_regexp_func(db);
1488 }
1489 #endif
1490 #ifdef SQLITE_ENABLE_SPELLFIX
1491 {
1492 extern int sqlite3_spellfix1_register(sqlite3*);
1493 sqlite3_spellfix1_register(db);
1494 }
1495 #endif
1496 }
1497 }
1498
1499 /*
@@ -1549,10 +1537,47 @@
1549 }
1550 fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
1551 zArg);
1552 return 0;
1553 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1554
1555 /*
1556 ** Close an output file, assuming it is not stderr or stdout
1557 */
1558 static void output_file_close(FILE *f){
@@ -2467,11 +2492,11 @@
2467 break;
2468
2469 /* sqlite3_test_control(int, uint) */
2470 case SQLITE_TESTCTRL_PENDING_BYTE:
2471 if( nArg==3 ){
2472 unsigned int opt = (unsigned int)atoi(azArg[2]);
2473 rc = sqlite3_test_control(testctrl, opt);
2474 printf("%d (0x%08x)\n", rc, rc);
2475 } else {
2476 fprintf(stderr,"Error: testctrl %s takes a single unsigned"
2477 " int option\n", azArg[1]);
@@ -2559,11 +2584,11 @@
2559 }else
2560
2561 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2562 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
2563 extern int sqlite3WhereTrace;
2564 sqlite3WhereTrace = atoi(azArg[1]);
2565 }else
2566 #endif
2567
2568 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2569 int j;
@@ -2745,10 +2770,14 @@
2745 errCnt++;
2746 }
2747 free(zSql);
2748 zSql = 0;
2749 nSql = 0;
 
 
 
 
2750 }
2751 }
2752 if( zSql ){
2753 if( !_all_whitespace(zSql) ){
2754 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
@@ -2880,10 +2909,11 @@
2880 " -help show this message\n"
2881 " -html set output mode to HTML\n"
2882 " -interactive force interactive I/O\n"
2883 " -line set output mode to 'line'\n"
2884 " -list set output mode to 'list'\n"
 
2885 #ifdef SQLITE_ENABLE_MULTIPLEX
2886 " -multiplex enable the multiplexor VFS\n"
2887 #endif
2888 " -nullvalue TEXT set text string for NULL values. Default ''\n"
2889 " -separator SEP set output field separator. Default: '|'\n"
@@ -2999,16 +3029,11 @@
2999 int j, c;
3000 const char *zSize;
3001 sqlite3_int64 szHeap;
3002
3003 zSize = cmdline_option_value(argc, argv, ++i);
3004 szHeap = atoi(zSize);
3005 for(j=0; (c = zSize[j])!=0; j++){
3006 if( c=='M' ){ szHeap *= 1000000; break; }
3007 if( c=='K' ){ szHeap *= 1000; break; }
3008 if( c=='G' ){ szHeap *= 1000000000; break; }
3009 }
3010 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3011 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3012 #endif
3013 #ifdef SQLITE_ENABLE_VFSTRACE
3014 }else if( strcmp(z,"-vfstrace")==0 ){
@@ -3024,10 +3049,13 @@
3024 #ifdef SQLITE_ENABLE_MULTIPLEX
3025 }else if( strcmp(z,"-multiplex")==0 ){
3026 extern int sqlite3_multiple_initialize(const char*,int);
3027 sqlite3_multiplex_initialize(0, 1);
3028 #endif
 
 
 
3029 }else if( strcmp(z,"-vfs")==0 ){
3030 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
3031 if( pVfs ){
3032 sqlite3_vfs_register(pVfs, 1);
3033 }else{
@@ -3115,10 +3143,12 @@
3115 stdin_is_interactive = 1;
3116 }else if( strcmp(z,"-batch")==0 ){
3117 stdin_is_interactive = 0;
3118 }else if( strcmp(z,"-heap")==0 ){
3119 i++;
 
 
3120 }else if( strcmp(z,"-vfs")==0 ){
3121 i++;
3122 #ifdef SQLITE_ENABLE_VFSTRACE
3123 }else if( strcmp(z,"-vfstrace")==0 ){
3124 i++;
@@ -3132,11 +3162,11 @@
3132 }else if( strcmp(z,"-cmd")==0 ){
3133 if( i==argc-1 ) break;
3134 z = cmdline_option_value(argc,argv,++i);
3135 if( z[0]=='.' ){
3136 rc = do_meta_command(z, &data);
3137 if( rc && bail_on_error ) return rc;
3138 }else{
3139 open_db(&data);
3140 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
3141 if( zErrMsg!=0 ){
3142 fprintf(stderr,"Error: %s\n", zErrMsg);
@@ -3156,10 +3186,11 @@
3156 if( zFirstCmd ){
3157 /* Run just the command that follows the database name
3158 */
3159 if( zFirstCmd[0]=='.' ){
3160 rc = do_meta_command(zFirstCmd, &data);
 
3161 }else{
3162 open_db(&data);
3163 rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
3164 if( zErrMsg!=0 ){
3165 fprintf(stderr,"Error: %s\n", zErrMsg);
3166
--- src/shell.c
+++ src/shell.c
@@ -1478,22 +1478,10 @@
1478 p->zDbFilename, sqlite3_errmsg(db));
1479 exit(1);
1480 }
1481 #ifndef SQLITE_OMIT_LOAD_EXTENSION
1482 sqlite3_enable_load_extension(p->db, 1);
 
 
 
 
 
 
 
 
 
 
 
 
1483 #endif
1484 }
1485 }
1486
1487 /*
@@ -1549,10 +1537,47 @@
1537 }
1538 fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
1539 zArg);
1540 return 0;
1541 }
1542
1543 /*
1544 ** Interpret zArg as an integer value, possibly with suffixes.
1545 */
1546 static sqlite3_int64 integerValue(const char *zArg){
1547 sqlite3_int64 v = 0;
1548 static const struct { char *zSuffix; int iMult; } aMult[] = {
1549 { "KiB", 1024 },
1550 { "MiB", 1024*1024 },
1551 { "GiB", 1024*1024*1024 },
1552 { "KB", 1000 },
1553 { "MB", 1000000 },
1554 { "GB", 1000000000 },
1555 { "K", 1000 },
1556 { "M", 1000000 },
1557 { "G", 1000000000 },
1558 };
1559 int i;
1560 int isNeg = 0;
1561 if( zArg[0]=='-' ){
1562 isNeg = 1;
1563 zArg++;
1564 }else if( zArg[0]=='+' ){
1565 zArg++;
1566 }
1567 while( isdigit(zArg[0]) ){
1568 v = v*10 + zArg[0] - '0';
1569 zArg++;
1570 }
1571 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
1572 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1573 v *= aMult[i].iMult;
1574 break;
1575 }
1576 }
1577 return isNeg? -v : v;
1578 }
1579
1580 /*
1581 ** Close an output file, assuming it is not stderr or stdout
1582 */
1583 static void output_file_close(FILE *f){
@@ -2467,11 +2492,11 @@
2492 break;
2493
2494 /* sqlite3_test_control(int, uint) */
2495 case SQLITE_TESTCTRL_PENDING_BYTE:
2496 if( nArg==3 ){
2497 unsigned int opt = (unsigned int)integerValue(azArg[2]);
2498 rc = sqlite3_test_control(testctrl, opt);
2499 printf("%d (0x%08x)\n", rc, rc);
2500 } else {
2501 fprintf(stderr,"Error: testctrl %s takes a single unsigned"
2502 " int option\n", azArg[1]);
@@ -2559,11 +2584,11 @@
2584 }else
2585
2586 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2587 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
2588 extern int sqlite3WhereTrace;
2589 sqlite3WhereTrace = booleanValue(azArg[1]);
2590 }else
2591 #endif
2592
2593 if( c=='w' && strncmp(azArg[0], "width", n)==0 && nArg>1 ){
2594 int j;
@@ -2745,10 +2770,14 @@
2770 errCnt++;
2771 }
2772 free(zSql);
2773 zSql = 0;
2774 nSql = 0;
2775 }else if( zSql && _all_whitespace(zSql) ){
2776 free(zSql);
2777 zSql = 0;
2778 nSql = 0;
2779 }
2780 }
2781 if( zSql ){
2782 if( !_all_whitespace(zSql) ){
2783 fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
@@ -2880,10 +2909,11 @@
2909 " -help show this message\n"
2910 " -html set output mode to HTML\n"
2911 " -interactive force interactive I/O\n"
2912 " -line set output mode to 'line'\n"
2913 " -list set output mode to 'list'\n"
2914 " -mmap N default mmap size set to N\n"
2915 #ifdef SQLITE_ENABLE_MULTIPLEX
2916 " -multiplex enable the multiplexor VFS\n"
2917 #endif
2918 " -nullvalue TEXT set text string for NULL values. Default ''\n"
2919 " -separator SEP set output field separator. Default: '|'\n"
@@ -2999,16 +3029,11 @@
3029 int j, c;
3030 const char *zSize;
3031 sqlite3_int64 szHeap;
3032
3033 zSize = cmdline_option_value(argc, argv, ++i);
3034 szHeap = integerValue(zSize);
 
 
 
 
 
3035 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
3036 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
3037 #endif
3038 #ifdef SQLITE_ENABLE_VFSTRACE
3039 }else if( strcmp(z,"-vfstrace")==0 ){
@@ -3024,10 +3049,13 @@
3049 #ifdef SQLITE_ENABLE_MULTIPLEX
3050 }else if( strcmp(z,"-multiplex")==0 ){
3051 extern int sqlite3_multiple_initialize(const char*,int);
3052 sqlite3_multiplex_initialize(0, 1);
3053 #endif
3054 }else if( strcmp(z,"-mmap")==0 ){
3055 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
3056 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
3057 }else if( strcmp(z,"-vfs")==0 ){
3058 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
3059 if( pVfs ){
3060 sqlite3_vfs_register(pVfs, 1);
3061 }else{
@@ -3115,10 +3143,12 @@
3143 stdin_is_interactive = 1;
3144 }else if( strcmp(z,"-batch")==0 ){
3145 stdin_is_interactive = 0;
3146 }else if( strcmp(z,"-heap")==0 ){
3147 i++;
3148 }else if( strcmp(z,"-mmap")==0 ){
3149 i++;
3150 }else if( strcmp(z,"-vfs")==0 ){
3151 i++;
3152 #ifdef SQLITE_ENABLE_VFSTRACE
3153 }else if( strcmp(z,"-vfstrace")==0 ){
3154 i++;
@@ -3132,11 +3162,11 @@
3162 }else if( strcmp(z,"-cmd")==0 ){
3163 if( i==argc-1 ) break;
3164 z = cmdline_option_value(argc,argv,++i);
3165 if( z[0]=='.' ){
3166 rc = do_meta_command(z, &data);
3167 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
3168 }else{
3169 open_db(&data);
3170 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
3171 if( zErrMsg!=0 ){
3172 fprintf(stderr,"Error: %s\n", zErrMsg);
@@ -3156,10 +3186,11 @@
3186 if( zFirstCmd ){
3187 /* Run just the command that follows the database name
3188 */
3189 if( zFirstCmd[0]=='.' ){
3190 rc = do_meta_command(zFirstCmd, &data);
3191 if( rc==2 ) rc = 0;
3192 }else{
3193 open_db(&data);
3194 rc = shell_exec(data.db, zFirstCmd, shell_callback, &data, &zErrMsg);
3195 if( zErrMsg!=0 ){
3196 fprintf(stderr,"Error: %s\n", zErrMsg);
3197
+1962 -227
--- 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.16.1. By combining all the individual C code files into this
3
+** version 3.7.17. 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.
@@ -360,15 +360,15 @@
360360
**
361361
** Older versions of SQLite used an optional THREADSAFE macro.
362362
** We support that for legacy.
363363
*/
364364
#if !defined(SQLITE_THREADSAFE)
365
-#if defined(THREADSAFE)
366
-# define SQLITE_THREADSAFE THREADSAFE
367
-#else
368
-# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
369
-#endif
365
+# if defined(THREADSAFE)
366
+# define SQLITE_THREADSAFE THREADSAFE
367
+# else
368
+# define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
369
+# endif
370370
#endif
371371
372372
/*
373373
** Powersafe overwrite is on by default. But can be turned off using
374374
** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
@@ -676,13 +676,13 @@
676676
**
677677
** See also: [sqlite3_libversion()],
678678
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
679679
** [sqlite_version()] and [sqlite_source_id()].
680680
*/
681
-#define SQLITE_VERSION "3.7.16.1"
682
-#define SQLITE_VERSION_NUMBER 3007016
683
-#define SQLITE_SOURCE_ID "2013-03-27 20:41:15 274d2a22660c7b34b8bbd85f3c29cbafbcb1b4e7"
681
+#define SQLITE_VERSION "3.7.17"
682
+#define SQLITE_VERSION_NUMBER 3007017
683
+#define SQLITE_SOURCE_ID "2013-04-25 17:27:08 aabeea98f53edde68f484f1794ae70789dac3889"
684684
685685
/*
686686
** CAPI3REF: Run-Time Library Version Numbers
687687
** KEYWORDS: sqlite3_version, sqlite3_sourceid
688688
**
@@ -994,10 +994,12 @@
994994
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
995995
#define SQLITE_AUTH 23 /* Authorization denied */
996996
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
997997
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
998998
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
999
+#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
1000
+#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
9991001
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
10001002
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
10011003
/* end-of-error-codes */
10021004
10031005
/*
@@ -1044,10 +1046,11 @@
10441046
#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
10451047
#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
10461048
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
10471049
#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
10481050
#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1051
+#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
10491052
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
10501053
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
10511054
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
10521055
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
10531056
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -1063,10 +1066,12 @@
10631066
#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
10641067
#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
10651068
#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
10661069
#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
10671070
#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
1071
+#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
1072
+#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
10681073
10691074
/*
10701075
** CAPI3REF: Flags For File Open Operations
10711076
**
10721077
** These bit values are intended for use in the
@@ -1302,10 +1307,13 @@
13021307
int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
13031308
int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
13041309
void (*xShmBarrier)(sqlite3_file*);
13051310
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
13061311
/* Methods above are valid for version 2 */
1312
+ int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1313
+ int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1314
+ /* Methods above are valid for version 3 */
13071315
/* Additional methods may be added in future releases */
13081316
};
13091317
13101318
/*
13111319
** CAPI3REF: Standard File Control Opcodes
@@ -1438,11 +1446,12 @@
14381446
** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
14391447
** file control occurs at the beginning of pragma statement analysis and so
14401448
** it is able to override built-in [PRAGMA] statements.
14411449
**
14421450
** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1443
-** ^This file-control may be invoked by SQLite on the database file handle
1451
+** ^The [SQLITE_FCNTL_BUSYHANDLER]
1452
+** file-control may be invoked by SQLite on the database file handle
14441453
** shortly after it is opened in order to provide a custom VFS with access
14451454
** to the connections busy-handler callback. The argument is of type (void **)
14461455
** - an array of two (void *) values. The first (void *) actually points
14471456
** to a function of type (int (*)(void *)). In order to invoke the connections
14481457
** busy-handler, this function should be invoked with the second (void *) in
@@ -1449,17 +1458,28 @@
14491458
** the array as the only argument. If it returns non-zero, then the operation
14501459
** should be retried. If it returns zero, the custom VFS should abandon the
14511460
** current operation.
14521461
**
14531462
** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1454
-** ^Application can invoke this file-control to have SQLite generate a
1463
+** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1464
+** to have SQLite generate a
14551465
** temporary filename using the same algorithm that is followed to generate
14561466
** temporary filenames for TEMP tables and other internal uses. The
14571467
** argument should be a char** which will be filled with the filename
14581468
** written into memory obtained from [sqlite3_malloc()]. The caller should
14591469
** invoke [sqlite3_free()] on the result to avoid a memory leak.
14601470
**
1471
+** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1472
+** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1473
+** maximum number of bytes that will be used for memory-mapped I/O.
1474
+** The argument is a pointer to a value of type sqlite3_int64 that
1475
+** is an advisory maximum number of bytes in the file to memory map. The
1476
+** pointer is overwritten with the old value. The limit is not changed if
1477
+** the value originally pointed to is negative, and so the current limit
1478
+** can be queried by passing in a pointer to a negative number. This
1479
+** file-control is used internally to implement [PRAGMA mmap_size].
1480
+**
14611481
** </ul>
14621482
*/
14631483
#define SQLITE_FCNTL_LOCKSTATE 1
14641484
#define SQLITE_GET_LOCKPROXYFILE 2
14651485
#define SQLITE_SET_LOCKPROXYFILE 3
@@ -1474,10 +1494,11 @@
14741494
#define SQLITE_FCNTL_VFSNAME 12
14751495
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
14761496
#define SQLITE_FCNTL_PRAGMA 14
14771497
#define SQLITE_FCNTL_BUSYHANDLER 15
14781498
#define SQLITE_FCNTL_TEMPFILENAME 16
1499
+#define SQLITE_FCNTL_MMAP_SIZE 18
14791500
14801501
/*
14811502
** CAPI3REF: Mutex Handle
14821503
**
14831504
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2186,26 +2207,42 @@
21862207
**
21872208
** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
21882209
** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
21892210
** <dd> These options are obsolete and should not be used by new code.
21902211
** They are retained for backwards compatibility but are now no-ops.
2191
-** </dl>
2212
+** </dd>
21922213
**
21932214
** [[SQLITE_CONFIG_SQLLOG]]
21942215
** <dt>SQLITE_CONFIG_SQLLOG
21952216
** <dd>This option is only available if sqlite is compiled with the
2196
-** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
2217
+** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
21972218
** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
21982219
** The second should be of type (void*). The callback is invoked by the library
21992220
** in three separate circumstances, identified by the value passed as the
22002221
** fourth parameter. If the fourth parameter is 0, then the database connection
22012222
** passed as the second argument has just been opened. The third argument
22022223
** points to a buffer containing the name of the main database file. If the
22032224
** fourth parameter is 1, then the SQL statement that the third parameter
22042225
** points to has just been executed. Or, if the fourth parameter is 2, then
22052226
** the connection being passed as the second parameter is being closed. The
2206
-** third parameter is passed NULL In this case.
2227
+** third parameter is passed NULL In this case. An example of using this
2228
+** configuration option can be seen in the "test_sqllog.c" source file in
2229
+** the canonical SQLite source tree.</dd>
2230
+**
2231
+** [[SQLITE_CONFIG_MMAP_SIZE]]
2232
+** <dt>SQLITE_CONFIG_MMAP_SIZE
2233
+** <dd>SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2234
+** that are the default mmap size limit (the default setting for
2235
+** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2236
+** The default setting can be overridden by each database connection using
2237
+** either the [PRAGMA mmap_size] command, or by using the
2238
+** [SQLITE_FCNTL_MMAP_SIZE] file control. The maximum allowed mmap size
2239
+** cannot be changed at run-time. Nor may the maximum allowed mmap size
2240
+** exceed the compile-time maximum mmap size set by the
2241
+** [SQLITE_MAX_MMAP_SIZE] compile-time option.
2242
+** If either argument to this option is negative, then that argument is
2243
+** changed to its compile-time default.
22072244
** </dl>
22082245
*/
22092246
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
22102247
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
22112248
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2225,10 +2262,11 @@
22252262
#define SQLITE_CONFIG_URI 17 /* int */
22262263
#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
22272264
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
22282265
#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
22292266
#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
2267
+#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
22302268
22312269
/*
22322270
** CAPI3REF: Database Connection Configuration Options
22332271
**
22342272
** These constants are the available integer configuration options that
@@ -4756,11 +4794,11 @@
47564794
** SQLITE_TRANSIENT value means that the content will likely change in
47574795
** the near future and that SQLite should make its own private copy of
47584796
** the content before returning.
47594797
**
47604798
** The typedef is necessary to work around problems in certain
4761
-** C++ compilers. See ticket #2191.
4799
+** C++ compilers.
47624800
*/
47634801
typedef void (*sqlite3_destructor_type)(void*);
47644802
#define SQLITE_STATIC ((sqlite3_destructor_type)0)
47654803
#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
47664804
@@ -5555,15 +5593,24 @@
55555593
** CAPI3REF: Load An Extension
55565594
**
55575595
** ^This interface loads an SQLite extension library from the named file.
55585596
**
55595597
** ^The sqlite3_load_extension() interface attempts to load an
5560
-** SQLite extension library contained in the file zFile.
5598
+** [SQLite extension] library contained in the file zFile. If
5599
+** the file cannot be loaded directly, attempts are made to load
5600
+** with various operating-system specific extensions added.
5601
+** So for example, if "samplelib" cannot be loaded, then names like
5602
+** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5603
+** be tried also.
55615604
**
55625605
** ^The entry point is zProc.
5563
-** ^zProc may be 0, in which case the name of the entry point
5564
-** defaults to "sqlite3_extension_init".
5606
+** ^(zProc may be 0, in which case SQLite will try to come up with an
5607
+** entry point name on its own. It first tries "sqlite3_extension_init".
5608
+** If that does not work, it constructs a name "sqlite3_X_init" where the
5609
+** X is consists of the lower-case equivalent of all ASCII alphabetic
5610
+** characters in the filename from the last "/" to the first following
5611
+** "." and omitting any initial "lib".)^
55655612
** ^The sqlite3_load_extension() interface returns
55665613
** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
55675614
** ^If an error occurs and pzErrMsg is not 0, then the
55685615
** [sqlite3_load_extension()] interface shall attempt to
55695616
** fill *pzErrMsg with error message text stored in memory
@@ -5585,15 +5632,15 @@
55855632
55865633
/*
55875634
** CAPI3REF: Enable Or Disable Extension Loading
55885635
**
55895636
** ^So as not to open security holes in older applications that are
5590
-** unprepared to deal with extension loading, and as a means of disabling
5591
-** extension loading while evaluating user-entered SQL, the following API
5637
+** unprepared to deal with [extension loading], and as a means of disabling
5638
+** [extension loading] while evaluating user-entered SQL, the following API
55925639
** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
55935640
**
5594
-** ^Extension loading is off by default. See ticket #1863.
5641
+** ^Extension loading is off by default.
55955642
** ^Call the sqlite3_enable_load_extension() routine with onoff==1
55965643
** to turn extension loading on and call it with onoff==0 to turn
55975644
** it back off again.
55985645
*/
55995646
SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5601,11 +5648,11 @@
56015648
/*
56025649
** CAPI3REF: Automatically Load Statically Linked Extensions
56035650
**
56045651
** ^This interface causes the xEntryPoint() function to be invoked for
56055652
** each new [database connection] that is created. The idea here is that
5606
-** xEntryPoint() is the entry point for a statically linked SQLite extension
5653
+** xEntryPoint() is the entry point for a statically linked [SQLite extension]
56075654
** that is to be automatically loaded into all new database connections.
56085655
**
56095656
** ^(Even though the function prototype shows that xEntryPoint() takes
56105657
** no arguments and returns void, SQLite invokes xEntryPoint() with three
56115658
** arguments and expects and integer result as if the signature of the
@@ -7381,10 +7428,25 @@
73817428
** independence" that SQLite uses internally when comparing identifiers.
73827429
*/
73837430
SQLITE_API int sqlite3_stricmp(const char *, const char *);
73847431
SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
73857432
7433
+/*
7434
+** CAPI3REF: String Globbing
7435
+*
7436
+** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
7437
+** the glob pattern P, and it returns non-zero if string X does not match
7438
+** the glob pattern P. ^The definition of glob pattern matching used in
7439
+** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
7440
+** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case
7441
+** sensitive.
7442
+**
7443
+** Note that this routine returns zero on a match and non-zero if the strings
7444
+** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
7445
+*/
7446
+SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
7447
+
73867448
/*
73877449
** CAPI3REF: Error Logging Interface
73887450
**
73897451
** ^The [sqlite3_log()] interface writes a message into the error log
73907452
** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
@@ -8069,10 +8131,11 @@
80698131
** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
80708132
** on the command-line
80718133
*/
80728134
#ifndef SQLITE_TEMP_STORE
80738135
# define SQLITE_TEMP_STORE 1
8136
+# define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */
80748137
#endif
80758138
80768139
/*
80778140
** GCC does not define the offsetof() macro so we'll have to do it
80788141
** ourselves.
@@ -8216,10 +8279,53 @@
82168279
# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
82178280
#else
82188281
# define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
82198282
#endif
82208283
8284
+/*
8285
+** Disable MMAP on platforms where it is known to not work
8286
+*/
8287
+#if defined(__OpenBSD__) || defined(__QNXNTO__)
8288
+# undef SQLITE_MAX_MMAP_SIZE
8289
+# define SQLITE_MAX_MMAP_SIZE 0
8290
+#endif
8291
+
8292
+/*
8293
+** Default maximum size of memory used by memory-mapped I/O in the VFS
8294
+*/
8295
+#ifdef __APPLE__
8296
+# include <TargetConditionals.h>
8297
+# if TARGET_OS_IPHONE
8298
+# undef SQLITE_MAX_MMAP_SIZE
8299
+# define SQLITE_MAX_MMAP_SIZE 0
8300
+# endif
8301
+#endif
8302
+#ifndef SQLITE_MAX_MMAP_SIZE
8303
+# if defined(__linux__) \
8304
+ || defined(_WIN32) \
8305
+ || (defined(__APPLE__) && defined(__MACH__)) \
8306
+ || defined(__sun)
8307
+# define SQLITE_MAX_MMAP_SIZE 2147483648
8308
+# else
8309
+# define SQLITE_MAX_MMAP_SIZE 0
8310
+# endif
8311
+# define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
8312
+#endif
8313
+
8314
+/*
8315
+** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
8316
+** default MMAP_SIZE is specified at compile-time, make sure that it does
8317
+** not exceed the maximum mmap size.
8318
+*/
8319
+#ifndef SQLITE_DEFAULT_MMAP_SIZE
8320
+# define SQLITE_DEFAULT_MMAP_SIZE 0
8321
+# define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */
8322
+#endif
8323
+#if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8324
+# undef SQLITE_DEFAULT_MMAP_SIZE
8325
+# define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8326
+#endif
82218327
82228328
/*
82238329
** An instance of the following structure is used to store the busy-handler
82248330
** callback for a given sqlite handle.
82258331
**
@@ -8437,10 +8543,11 @@
84378543
#define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
84388544
#define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
84398545
84408546
SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
84418547
SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8548
+SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
84428549
SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
84438550
SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
84448551
SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
84458552
SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
84468553
SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
@@ -9137,10 +9244,16 @@
91379244
#define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
91389245
#define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
91399246
#define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
91409247
#define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
91419248
9249
+/*
9250
+** Flags that make up the mask passed to sqlite3PagerAcquire().
9251
+*/
9252
+#define PAGER_ACQUIRE_NOCONTENT 0x01 /* Do not load data from disk */
9253
+#define PAGER_ACQUIRE_READONLY 0x02 /* Read-only page is acceptable */
9254
+
91429255
/*
91439256
** The remainder of this file contains the declarations of the functions
91449257
** that make up the Pager sub-system API. See source code comments for
91459258
** a detailed description of each routine.
91469259
*/
@@ -9161,10 +9274,11 @@
91619274
/* Functions used to configure a Pager object. */
91629275
SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
91639276
SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
91649277
SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
91659278
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9279
+SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
91669280
SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
91679281
SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
91689282
SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
91699283
SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
91709284
SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
@@ -9306,10 +9420,12 @@
93069420
#define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
93079421
** writing this page to the database */
93089422
#define PGHDR_NEED_READ 0x008 /* Content is unread */
93099423
#define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
93109424
#define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
9425
+
9426
+#define PGHDR_MMAP 0x040 /* This is an mmap page object */
93119427
93129428
/* Initialize and shutdown the page cache subsystem */
93139429
SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
93149430
SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
93159431
@@ -9518,18 +9634,10 @@
95189634
*/
95199635
#if !defined(SQLITE_OS_WINRT)
95209636
# define SQLITE_OS_WINRT 0
95219637
#endif
95229638
9523
-/*
9524
-** When compiled for WinCE or WinRT, there is no concept of the current
9525
-** directory.
9526
- */
9527
-#if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
9528
-# define SQLITE_CURDIR 1
9529
-#endif
9530
-
95319639
/* If the SET_FULLSYNC macro is not defined above, then make it
95329640
** a no-op
95339641
*/
95349642
#ifndef SET_FULLSYNC
95359643
# define SET_FULLSYNC(x,y)
@@ -9678,10 +9786,12 @@
96789786
SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
96799787
SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
96809788
SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
96819789
SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
96829790
SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9791
+SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
9792
+SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
96839793
96849794
96859795
/*
96869796
** Functions for accessing sqlite3_vfs methods
96879797
*/
@@ -9917,10 +10027,11 @@
991710027
sqlite3_mutex *mutex; /* Connection mutex */
991810028
Db *aDb; /* All backends */
991910029
int nDb; /* Number of backends currently in use */
992010030
int flags; /* Miscellaneous flags. See below */
992110031
i64 lastRowid; /* ROWID of most recent insert (see above) */
10032
+ i64 szMmap; /* Default mmap_size setting */
992210033
unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
992310034
int errCode; /* Most recent error code (SQLITE_*) */
992410035
int errMask; /* & result codes with this before returning */
992510036
u16 dbOptFlags; /* Flags to enable/disable optimizations */
992610037
u8 autoCommit; /* The auto-commit flag. */
@@ -11153,10 +11264,12 @@
1115311264
*/
1115411265
#define NC_AllowAgg 0x01 /* Aggregate functions are allowed here */
1115511266
#define NC_HasAgg 0x02 /* One or more aggregate functions seen */
1115611267
#define NC_IsCheck 0x04 /* True if resolving names in a CHECK constraint */
1115711268
#define NC_InAggFunc 0x08 /* True if analyzing arguments to an agg func */
11269
+#define NC_AsMaybe 0x10 /* Resolve to AS terms of the result set only
11270
+ ** if no other resolution is available */
1115811271
1115911272
/*
1116011273
** An instance of the following structure contains all information
1116111274
** needed to generate code for a single SELECT statement.
1116211275
**
@@ -11588,10 +11701,12 @@
1158811701
sqlite3_mutex_methods mutex; /* Low-level mutex interface */
1158911702
sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
1159011703
void *pHeap; /* Heap storage space */
1159111704
int nHeap; /* Size of pHeap[] */
1159211705
int mnReq, mxReq; /* Min and max heap requests sizes */
11706
+ sqlite3_int64 szMmap; /* mmap() space per open file */
11707
+ sqlite3_int64 mxMmap; /* Maximum value for szMmap */
1159311708
void *pScratch; /* Scratch memory */
1159411709
int szScratch; /* Size of each scratch buffer */
1159511710
int nScratch; /* Number of scratch buffers */
1159611711
void *pPage; /* Page cache memory */
1159711712
int szPage; /* Size of each page in pPage[] */
@@ -11622,10 +11737,11 @@
1162211737
struct Walker {
1162311738
int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
1162411739
int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
1162511740
Parse *pParse; /* Parser context. */
1162611741
int walkerDepth; /* Number of subqueries */
11742
+ u8 bSelectDepthFirst; /* Do subqueries first */
1162711743
union { /* Extra data for callback */
1162811744
NameContext *pNC; /* Naming context */
1162911745
int i; /* Integer value */
1163011746
SrcList *pSrcList; /* FROM clause */
1163111747
struct SrcCount *pSrcCount; /* Counting column references */
@@ -12609,10 +12725,12 @@
1260912725
{0,0,0,0,0,0,0,0,0}, /* mutex */
1261012726
{0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
1261112727
(void*)0, /* pHeap */
1261212728
0, /* nHeap */
1261312729
0, 0, /* mnHeap, mxHeap */
12730
+ SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
12731
+ SQLITE_MAX_MMAP_SIZE, /* mxMmap */
1261412732
(void*)0, /* pScratch */
1261512733
0, /* szScratch */
1261612734
0, /* nScratch */
1261712735
(void*)0, /* pPage */
1261812736
0, /* szPage */
@@ -12732,18 +12850,18 @@
1273212850
"CHECK_PAGES",
1273312851
#endif
1273412852
#ifdef SQLITE_COVERAGE_TEST
1273512853
"COVERAGE_TEST",
1273612854
#endif
12737
-#ifdef SQLITE_CURDIR
12738
- "CURDIR",
12739
-#endif
1274012855
#ifdef SQLITE_DEBUG
1274112856
"DEBUG",
1274212857
#endif
1274312858
#ifdef SQLITE_DEFAULT_LOCKING_MODE
1274412859
"DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
12860
+#endif
12861
+#if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
12862
+ "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
1274512863
#endif
1274612864
#ifdef SQLITE_DISABLE_DIRSYNC
1274712865
"DISABLE_DIRSYNC",
1274812866
#endif
1274912867
#ifdef SQLITE_DISABLE_LFS
@@ -12831,10 +12949,13 @@
1283112949
"INT64_TYPE",
1283212950
#endif
1283312951
#ifdef SQLITE_LOCK_TRACE
1283412952
"LOCK_TRACE",
1283512953
#endif
12954
+#if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
12955
+ "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
12956
+#endif
1283612957
#ifdef SQLITE_MAX_SCHEMA_RETRY
1283712958
"MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
1283812959
#endif
1283912960
#ifdef SQLITE_MEMDEBUG
1284012961
"MEMDEBUG",
@@ -12888,15 +13009,10 @@
1288813009
"OMIT_CAST",
1288913010
#endif
1289013011
#ifdef SQLITE_OMIT_CHECK
1289113012
"OMIT_CHECK",
1289213013
#endif
12893
-/* // redundant
12894
-** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
12895
-** "OMIT_COMPILEOPTION_DIAGS",
12896
-** #endif
12897
-*/
1289813014
#ifdef SQLITE_OMIT_COMPLETE
1289913015
"OMIT_COMPLETE",
1290013016
#endif
1290113017
#ifdef SQLITE_OMIT_COMPOUND_SELECT
1290213018
"OMIT_COMPOUND_SELECT",
@@ -13034,17 +13150,17 @@
1303413150
"SOUNDEX",
1303513151
#endif
1303613152
#ifdef SQLITE_TCL
1303713153
"TCL",
1303813154
#endif
13039
-#ifdef SQLITE_TEMP_STORE
13155
+#if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
1304013156
"TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
1304113157
#endif
1304213158
#ifdef SQLITE_TEST
1304313159
"TEST",
1304413160
#endif
13045
-#ifdef SQLITE_THREADSAFE
13161
+#if defined(SQLITE_THREADSAFE)
1304613162
"THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
1304713163
#endif
1304813164
#ifdef SQLITE_USE_ALLOCA
1304913165
"USE_ALLOCA",
1305013166
#endif
@@ -13066,12 +13182,15 @@
1306613182
n = sqlite3Strlen30(zOptName);
1306713183
1306813184
/* Since ArraySize(azCompileOpt) is normally in single digits, a
1306913185
** linear search is adequate. No need for a binary search. */
1307013186
for(i=0; i<ArraySize(azCompileOpt); i++){
13071
- if( (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
13072
- && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
13187
+ if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13188
+ && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13189
+ ){
13190
+ return 1;
13191
+ }
1307313192
}
1307413193
return 0;
1307513194
}
1307613195
1307713196
/*
@@ -13124,10 +13243,18 @@
1312413243
** this header information was factored out.
1312513244
*/
1312613245
#ifndef _VDBEINT_H_
1312713246
#define _VDBEINT_H_
1312813247
13248
+/*
13249
+** The maximum number of times that a statement will try to reparse
13250
+** itself before giving up and returning SQLITE_SCHEMA.
13251
+*/
13252
+#ifndef SQLITE_MAX_SCHEMA_RETRY
13253
+# define SQLITE_MAX_SCHEMA_RETRY 50
13254
+#endif
13255
+
1312913256
/*
1313013257
** SQL is translated into a sequence of instructions to be
1313113258
** executed by a virtual machine. Each instruction is an instance
1313213259
** of the following structure.
1313313260
*/
@@ -15089,10 +15216,30 @@
1508915216
void volatile **pp /* OUT: Pointer to mapping */
1509015217
){
1509115218
DO_OS_MALLOC_TEST(id);
1509215219
return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
1509315220
}
15221
+
15222
+#if SQLITE_MAX_MMAP_SIZE>0
15223
+/* The real implementation of xFetch and xUnfetch */
15224
+SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15225
+ DO_OS_MALLOC_TEST(id);
15226
+ return id->pMethods->xFetch(id, iOff, iAmt, pp);
15227
+}
15228
+SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15229
+ return id->pMethods->xUnfetch(id, iOff, p);
15230
+}
15231
+#else
15232
+/* No-op stubs to use when memory-mapped I/O is disabled */
15233
+SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15234
+ *pp = 0;
15235
+ return SQLITE_OK;
15236
+}
15237
+SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15238
+ return SQLITE_OK;
15239
+}
15240
+#endif
1509415241
1509515242
/*
1509615243
** The next group of routines are convenience wrappers around the
1509715244
** VFS methods.
1509815245
*/
@@ -21584,11 +21731,11 @@
2158421731
*pNum = (i64)u;
2158521732
}
2158621733
testcase( i==18 );
2158721734
testcase( i==19 );
2158821735
testcase( i==20 );
21589
- if( (c+nonNum!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
21736
+ if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
2159021737
/* zNum is empty or contains non-numeric text or is longer
2159121738
** than 19 digits (thus guaranteeing that it is too large) */
2159221739
return 1;
2159321740
}else if( i<19*incr ){
2159421741
/* Less than 19 digits, so we know that it fits in 64 bits */
@@ -22945,10 +23092,15 @@
2294523092
void *lockingContext; /* Locking style specific state */
2294623093
UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
2294723094
const char *zPath; /* Name of the file */
2294823095
unixShm *pShm; /* Shared memory segment information */
2294923096
int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23097
+ int nFetchOut; /* Number of outstanding xFetch refs */
23098
+ sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23099
+ sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23100
+ sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23101
+ void *pMapRegion; /* Memory mapped region */
2295023102
#ifdef __QNXNTO__
2295123103
int sectorSize; /* Device sector size */
2295223104
int deviceCharacteristics; /* Precomputed device characteristics */
2295323105
#endif
2295423106
#if SQLITE_ENABLE_LOCKING_STYLE
@@ -22969,11 +23121,13 @@
2296923121
** one described by ticket #3584.
2297023122
*/
2297123123
unsigned char transCntrChng; /* True if the transaction counter changed */
2297223124
unsigned char dbUpdate; /* True if any part of database file changed */
2297323125
unsigned char inNormalWrite; /* True if in a normal write operation */
23126
+
2297423127
#endif
23128
+
2297523129
#ifdef SQLITE_TEST
2297623130
/* In test mode, increase the size of this structure a bit so that
2297723131
** it is larger than the struct CrashFile defined in test6.c.
2297823132
*/
2297923133
char aPadding[32];
@@ -22993,10 +23147,11 @@
2299323147
#endif
2299423148
#define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
2299523149
#define UNIXFILE_DELETE 0x20 /* Delete on close */
2299623150
#define UNIXFILE_URI 0x40 /* Filename might have query parameters */
2299723151
#define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
23152
+#define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issued */
2299823153
2299923154
/*
2300023155
** Include code that is common to all os_*.c files
2300123156
*/
2300223157
/************** Include os_common.h in the middle of os_unix.c ***************/
@@ -23234,10 +23389,21 @@
2323423389
#define threadid pthread_self()
2323523390
#else
2323623391
#define threadid 0
2323723392
#endif
2323823393
23394
+/*
23395
+** HAVE_MREMAP defaults to true on Linux and false everywhere else.
23396
+*/
23397
+#if !defined(HAVE_MREMAP)
23398
+# if defined(__linux__) && defined(_GNU_SOURCE)
23399
+# define HAVE_MREMAP 1
23400
+# else
23401
+# define HAVE_MREMAP 0
23402
+# endif
23403
+#endif
23404
+
2323923405
/*
2324023406
** Different Unix systems declare open() in different ways. Same use
2324123407
** open(const char*,int,mode_t). Others use open(const char*,int,...).
2324223408
** The difference is important when using a pointer to the function.
2324323409
**
@@ -23365,10 +23531,23 @@
2336523531
#define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
2336623532
2336723533
{ "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
2336823534
#define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
2336923535
23536
+ { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23537
+#define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23538
+
23539
+ { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23540
+#define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
23541
+
23542
+#if HAVE_MREMAP
23543
+ { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23544
+#else
23545
+ { "mremap", (sqlite3_syscall_ptr)0, 0 },
23546
+#endif
23547
+#define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23548
+
2337023549
}; /* End of the overrideable system calls */
2337123550
2337223551
/*
2337323552
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
2337423553
** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -23694,11 +23873,10 @@
2369423873
2369523874
default:
2369623875
return sqliteIOErr;
2369723876
}
2369823877
}
23699
-
2370023878
2370123879
2370223880
/******************************************************************************
2370323881
****************** Begin Unique File ID Utility Used By VxWorks ***************
2370423882
**
@@ -24032,11 +24210,10 @@
2403224210
#else
2403324211
/* Non-threadsafe build, use strerror(). */
2403424212
zErr = strerror(iErrno);
2403524213
#endif
2403624214
24037
- assert( errcode!=SQLITE_OK );
2403824215
if( zPath==0 ) zPath = "";
2403924216
sqlite3_log(errcode,
2404024217
"os_unix.c:%d: (%d) %s(%s) - %s",
2404124218
iLine, iErrno, zFunc, zPath, zErr
2404224219
);
@@ -24197,10 +24374,54 @@
2419724374
}
2419824375
*ppInode = pInode;
2419924376
return SQLITE_OK;
2420024377
}
2420124378
24379
+
24380
+/*
24381
+** Check a unixFile that is a database. Verify the following:
24382
+**
24383
+** (1) There is exactly one hard link on the file
24384
+** (2) The file is not a symbolic link
24385
+** (3) The file has not been renamed or unlinked
24386
+**
24387
+** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
24388
+*/
24389
+static void verifyDbFile(unixFile *pFile){
24390
+ struct stat buf;
24391
+ int rc;
24392
+ if( pFile->ctrlFlags & UNIXFILE_WARNED ){
24393
+ /* One or more of the following warnings have already been issued. Do not
24394
+ ** repeat them so as not to clutter the error log */
24395
+ return;
24396
+ }
24397
+ rc = osFstat(pFile->h, &buf);
24398
+ if( rc!=0 ){
24399
+ sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
24400
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
24401
+ return;
24402
+ }
24403
+ if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
24404
+ sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
24405
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
24406
+ return;
24407
+ }
24408
+ if( buf.st_nlink>1 ){
24409
+ sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
24410
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
24411
+ return;
24412
+ }
24413
+ if( pFile->pInode!=0
24414
+ && ((rc = osStat(pFile->zPath, &buf))!=0
24415
+ || buf.st_ino!=pFile->pInode->fileId.ino)
24416
+ ){
24417
+ sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
24418
+ pFile->ctrlFlags |= UNIXFILE_WARNED;
24419
+ return;
24420
+ }
24421
+}
24422
+
2420224423
2420324424
/*
2420424425
** This routine checks if there is a RESERVED lock held on the specified
2420524426
** file by this or any other process. If such a lock is held, set *pResOut
2420624427
** to a non-zero value otherwise *pResOut is set to zero. The return value
@@ -24728,12 +24949,16 @@
2472824949
**
2472924950
** If the locking level of the file descriptor is already at or below
2473024951
** the requested locking level, this routine is a no-op.
2473124952
*/
2473224953
static int unixUnlock(sqlite3_file *id, int eFileLock){
24954
+ assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
2473324955
return posixUnlock(id, eFileLock, 0);
2473424956
}
24957
+
24958
+static int unixMapfile(unixFile *pFd, i64 nByte);
24959
+static void unixUnmapfile(unixFile *pFd);
2473524960
2473624961
/*
2473724962
** This function performs the parts of the "close file" operation
2473824963
** common to all locking schemes. It closes the directory and file
2473924964
** handles, if they are valid, and sets all fields of the unixFile
@@ -24743,10 +24968,11 @@
2474324968
** even on VxWorks. A mutex will be acquired on VxWorks by the
2474424969
** vxworksReleaseFileId() routine.
2474524970
*/
2474624971
static int closeUnixFile(sqlite3_file *id){
2474724972
unixFile *pFile = (unixFile*)id;
24973
+ unixUnmapfile(pFile);
2474824974
if( pFile->h>=0 ){
2474924975
robust_close(pFile, pFile->h, __LINE__);
2475024976
pFile->h = -1;
2475124977
}
2475224978
#if OS_VXWORKS
@@ -24769,10 +24995,11 @@
2476924995
** Close a file.
2477024996
*/
2477124997
static int unixClose(sqlite3_file *id){
2477224998
int rc = SQLITE_OK;
2477324999
unixFile *pFile = (unixFile *)id;
25000
+ verifyDbFile(pFile);
2477425001
unixUnlock(id, NO_LOCK);
2477525002
unixEnterMutex();
2477625003
2477725004
/* unixFile.pInode is always valid here. Otherwise, a different close
2477825005
** routine (e.g. nolockClose()) would be called instead.
@@ -26009,10 +26236,27 @@
2600926236
assert( pFile->pUnused==0
2601026237
|| offset>=PENDING_BYTE+512
2601126238
|| offset+amt<=PENDING_BYTE
2601226239
);
2601326240
#endif
26241
+
26242
+#if SQLITE_MAX_MMAP_SIZE>0
26243
+ /* Deal with as much of this read request as possible by transfering
26244
+ ** data from the memory mapping using memcpy(). */
26245
+ if( offset<pFile->mmapSize ){
26246
+ if( offset+amt <= pFile->mmapSize ){
26247
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
26248
+ return SQLITE_OK;
26249
+ }else{
26250
+ int nCopy = pFile->mmapSize - offset;
26251
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
26252
+ pBuf = &((u8 *)pBuf)[nCopy];
26253
+ amt -= nCopy;
26254
+ offset += nCopy;
26255
+ }
26256
+ }
26257
+#endif
2601426258
2601526259
got = seekAndRead(pFile, offset, pBuf, amt);
2601626260
if( got==amt ){
2601726261
return SQLITE_OK;
2601826262
}else if( got<0 ){
@@ -26111,10 +26355,27 @@
2611126355
SimulateIOErrorBenign(0);
2611226356
if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
2611326357
pFile->transCntrChng = 1; /* The transaction counter has changed */
2611426358
}
2611526359
}
26360
+ }
26361
+#endif
26362
+
26363
+#if SQLITE_MAX_MMAP_SIZE>0
26364
+ /* Deal with as much of this write request as possible by transfering
26365
+ ** data from the memory mapping using memcpy(). */
26366
+ if( offset<pFile->mmapSize ){
26367
+ if( offset+amt <= pFile->mmapSize ){
26368
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
26369
+ return SQLITE_OK;
26370
+ }else{
26371
+ int nCopy = pFile->mmapSize - offset;
26372
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
26373
+ pBuf = &((u8 *)pBuf)[nCopy];
26374
+ amt -= nCopy;
26375
+ offset += nCopy;
26376
+ }
2611626377
}
2611726378
#endif
2611826379
2611926380
while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
2612026381
amt -= wrote;
@@ -26395,10 +26656,18 @@
2639526656
*/
2639626657
if( pFile->inNormalWrite && nByte==0 ){
2639726658
pFile->transCntrChng = 1;
2639826659
}
2639926660
#endif
26661
+
26662
+ /* If the file was just truncated to a size smaller than the currently
26663
+ ** mapped region, reduce the effective mapping size as well. SQLite will
26664
+ ** use read() and write() to access data beyond this point from now on.
26665
+ */
26666
+ if( nByte<pFile->mmapSize ){
26667
+ pFile->mmapSize = nByte;
26668
+ }
2640026669
2640126670
return SQLITE_OK;
2640226671
}
2640326672
}
2640426673
@@ -26483,10 +26752,23 @@
2648326752
iWrite += nBlk;
2648426753
}
2648526754
#endif
2648626755
}
2648726756
}
26757
+
26758
+ if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26759
+ int rc;
26760
+ if( pFile->szChunk<=0 ){
26761
+ if( robust_ftruncate(pFile->h, nByte) ){
26762
+ pFile->lastErrno = errno;
26763
+ return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
26764
+ }
26765
+ }
26766
+
26767
+ rc = unixMapfile(pFile, nByte);
26768
+ return rc;
26769
+ }
2648826770
2648926771
return SQLITE_OK;
2649026772
}
2649126773
2649226774
/*
@@ -26550,10 +26832,22 @@
2655026832
if( zTFile ){
2655126833
unixGetTempname(pFile->pVfs->mxPathname, zTFile);
2655226834
*(char**)pArg = zTFile;
2655326835
}
2655426836
return SQLITE_OK;
26837
+ }
26838
+ case SQLITE_FCNTL_MMAP_SIZE: {
26839
+ i64 newLimit = *(i64*)pArg;
26840
+ if( newLimit>sqlite3GlobalConfig.mxMmap ){
26841
+ newLimit = sqlite3GlobalConfig.mxMmap;
26842
+ }
26843
+ *(i64*)pArg = pFile->mmapSizeMax;
26844
+ if( newLimit>=0 ){
26845
+ pFile->mmapSizeMax = newLimit;
26846
+ if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit;
26847
+ }
26848
+ return SQLITE_OK;
2655526849
}
2655626850
#ifdef SQLITE_DEBUG
2655726851
/* The pager calls this method to signal that it has done
2655826852
** a rollback and that the database is therefore unchanged and
2655926853
** it hence it is OK for the transaction change counter to be
@@ -26863,11 +27157,11 @@
2686327157
int i;
2686427158
assert( p->pInode==pFd->pInode );
2686527159
sqlite3_mutex_free(p->mutex);
2686627160
for(i=0; i<p->nRegion; i++){
2686727161
if( p->h>=0 ){
26868
- munmap(p->apRegion[i], p->szRegion);
27162
+ osMunmap(p->apRegion[i], p->szRegion);
2686927163
}else{
2687027164
sqlite3_free(p->apRegion[i]);
2687127165
}
2687227166
}
2687327167
sqlite3_free(p->apRegion);
@@ -27136,11 +27430,11 @@
2713627430
}
2713727431
pShmNode->apRegion = apNew;
2713827432
while(pShmNode->nRegion<=iRegion){
2713927433
void *pMem;
2714027434
if( pShmNode->h>=0 ){
27141
- pMem = mmap(0, szRegion,
27435
+ pMem = osMmap(0, szRegion,
2714227436
pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
2714327437
MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
2714427438
);
2714527439
if( pMem==MAP_FAILED ){
2714627440
rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
@@ -27352,10 +27646,240 @@
2735227646
# define unixShmMap 0
2735327647
# define unixShmLock 0
2735427648
# define unixShmBarrier 0
2735527649
# define unixShmUnmap 0
2735627650
#endif /* #ifndef SQLITE_OMIT_WAL */
27651
+
27652
+/*
27653
+** If it is currently memory mapped, unmap file pFd.
27654
+*/
27655
+static void unixUnmapfile(unixFile *pFd){
27656
+ assert( pFd->nFetchOut==0 );
27657
+#if SQLITE_MAX_MMAP_SIZE>0
27658
+ if( pFd->pMapRegion ){
27659
+ osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27660
+ pFd->pMapRegion = 0;
27661
+ pFd->mmapSize = 0;
27662
+ pFd->mmapSizeActual = 0;
27663
+ }
27664
+#endif
27665
+}
27666
+
27667
+#if SQLITE_MAX_MMAP_SIZE>0
27668
+/*
27669
+** Return the system page size.
27670
+*/
27671
+static int unixGetPagesize(void){
27672
+#if HAVE_MREMAP
27673
+ return 512;
27674
+#elif defined(_BSD_SOURCE)
27675
+ return getpagesize();
27676
+#else
27677
+ return (int)sysconf(_SC_PAGESIZE);
27678
+#endif
27679
+}
27680
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
27681
+
27682
+#if SQLITE_MAX_MMAP_SIZE>0
27683
+/*
27684
+** Attempt to set the size of the memory mapping maintained by file
27685
+** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27686
+**
27687
+** If successful, this function sets the following variables:
27688
+**
27689
+** unixFile.pMapRegion
27690
+** unixFile.mmapSize
27691
+** unixFile.mmapSizeActual
27692
+**
27693
+** If unsuccessful, an error message is logged via sqlite3_log() and
27694
+** the three variables above are zeroed. In this case SQLite should
27695
+** continue accessing the database using the xRead() and xWrite()
27696
+** methods.
27697
+*/
27698
+static void unixRemapfile(
27699
+ unixFile *pFd, /* File descriptor object */
27700
+ i64 nNew /* Required mapping size */
27701
+){
27702
+ const char *zErr = "mmap";
27703
+ int h = pFd->h; /* File descriptor open on db file */
27704
+ u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
27705
+ i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
27706
+ u8 *pNew = 0; /* Location of new mapping */
27707
+ int flags = PROT_READ; /* Flags to pass to mmap() */
27708
+
27709
+ assert( pFd->nFetchOut==0 );
27710
+ assert( nNew>pFd->mmapSize );
27711
+ assert( nNew<=pFd->mmapSizeMax );
27712
+ assert( nNew>0 );
27713
+ assert( pFd->mmapSizeActual>=pFd->mmapSize );
27714
+ assert( MAP_FAILED!=0 );
27715
+
27716
+ if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
27717
+
27718
+ if( pOrig ){
27719
+ const int szSyspage = unixGetPagesize();
27720
+ i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
27721
+ u8 *pReq = &pOrig[nReuse];
27722
+
27723
+ /* Unmap any pages of the existing mapping that cannot be reused. */
27724
+ if( nReuse!=nOrig ){
27725
+ osMunmap(pReq, nOrig-nReuse);
27726
+ }
27727
+
27728
+#if HAVE_MREMAP
27729
+ pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
27730
+ zErr = "mremap";
27731
+#else
27732
+ pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
27733
+ if( pNew!=MAP_FAILED ){
27734
+ if( pNew!=pReq ){
27735
+ osMunmap(pNew, nNew - nReuse);
27736
+ pNew = 0;
27737
+ }else{
27738
+ pNew = pOrig;
27739
+ }
27740
+ }
27741
+#endif
27742
+
27743
+ /* The attempt to extend the existing mapping failed. Free it. */
27744
+ if( pNew==MAP_FAILED || pNew==0 ){
27745
+ osMunmap(pOrig, nReuse);
27746
+ }
27747
+ }
27748
+
27749
+ /* If pNew is still NULL, try to create an entirely new mapping. */
27750
+ if( pNew==0 ){
27751
+ pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
27752
+ }
27753
+
27754
+ if( pNew==MAP_FAILED ){
27755
+ pNew = 0;
27756
+ nNew = 0;
27757
+ unixLogError(SQLITE_OK, zErr, pFd->zPath);
27758
+
27759
+ /* If the mmap() above failed, assume that all subsequent mmap() calls
27760
+ ** will probably fail too. Fall back to using xRead/xWrite exclusively
27761
+ ** in this case. */
27762
+ pFd->mmapSizeMax = 0;
27763
+ }
27764
+ pFd->pMapRegion = (void *)pNew;
27765
+ pFd->mmapSize = pFd->mmapSizeActual = nNew;
27766
+}
27767
+#endif
27768
+
27769
+/*
27770
+** Memory map or remap the file opened by file-descriptor pFd (if the file
27771
+** is already mapped, the existing mapping is replaced by the new). Or, if
27772
+** there already exists a mapping for this file, and there are still
27773
+** outstanding xFetch() references to it, this function is a no-op.
27774
+**
27775
+** If parameter nByte is non-negative, then it is the requested size of
27776
+** the mapping to create. Otherwise, if nByte is less than zero, then the
27777
+** requested size is the size of the file on disk. The actual size of the
27778
+** created mapping is either the requested size or the value configured
27779
+** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
27780
+**
27781
+** SQLITE_OK is returned if no error occurs (even if the mapping is not
27782
+** recreated as a result of outstanding references) or an SQLite error
27783
+** code otherwise.
27784
+*/
27785
+static int unixMapfile(unixFile *pFd, i64 nByte){
27786
+#if SQLITE_MAX_MMAP_SIZE>0
27787
+ i64 nMap = nByte;
27788
+ int rc;
27789
+
27790
+ assert( nMap>=0 || pFd->nFetchOut==0 );
27791
+ if( pFd->nFetchOut>0 ) return SQLITE_OK;
27792
+
27793
+ if( nMap<0 ){
27794
+ struct stat statbuf; /* Low-level file information */
27795
+ rc = osFstat(pFd->h, &statbuf);
27796
+ if( rc!=SQLITE_OK ){
27797
+ return SQLITE_IOERR_FSTAT;
27798
+ }
27799
+ nMap = statbuf.st_size;
27800
+ }
27801
+ if( nMap>pFd->mmapSizeMax ){
27802
+ nMap = pFd->mmapSizeMax;
27803
+ }
27804
+
27805
+ if( nMap!=pFd->mmapSize ){
27806
+ if( nMap>0 ){
27807
+ unixRemapfile(pFd, nMap);
27808
+ }else{
27809
+ unixUnmapfile(pFd);
27810
+ }
27811
+ }
27812
+#endif
27813
+
27814
+ return SQLITE_OK;
27815
+}
27816
+
27817
+/*
27818
+** If possible, return a pointer to a mapping of file fd starting at offset
27819
+** iOff. The mapping must be valid for at least nAmt bytes.
27820
+**
27821
+** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
27822
+** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
27823
+** Finally, if an error does occur, return an SQLite error code. The final
27824
+** value of *pp is undefined in this case.
27825
+**
27826
+** If this function does return a pointer, the caller must eventually
27827
+** release the reference by calling unixUnfetch().
27828
+*/
27829
+static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
27830
+#if SQLITE_MAX_MMAP_SIZE>0
27831
+ unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27832
+#endif
27833
+ *pp = 0;
27834
+
27835
+#if SQLITE_MAX_MMAP_SIZE>0
27836
+ if( pFd->mmapSizeMax>0 ){
27837
+ if( pFd->pMapRegion==0 ){
27838
+ int rc = unixMapfile(pFd, -1);
27839
+ if( rc!=SQLITE_OK ) return rc;
27840
+ }
27841
+ if( pFd->mmapSize >= iOff+nAmt ){
27842
+ *pp = &((u8 *)pFd->pMapRegion)[iOff];
27843
+ pFd->nFetchOut++;
27844
+ }
27845
+ }
27846
+#endif
27847
+ return SQLITE_OK;
27848
+}
27849
+
27850
+/*
27851
+** If the third argument is non-NULL, then this function releases a
27852
+** reference obtained by an earlier call to unixFetch(). The second
27853
+** argument passed to this function must be the same as the corresponding
27854
+** argument that was passed to the unixFetch() invocation.
27855
+**
27856
+** Or, if the third argument is NULL, then this function is being called
27857
+** to inform the VFS layer that, according to POSIX, any existing mapping
27858
+** may now be invalid and should be unmapped.
27859
+*/
27860
+static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27861
+ unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27862
+ UNUSED_PARAMETER(iOff);
27863
+
27864
+ /* If p==0 (unmap the entire file) then there must be no outstanding
27865
+ ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27866
+ ** then there must be at least one outstanding. */
27867
+ assert( (p==0)==(pFd->nFetchOut==0) );
27868
+
27869
+ /* If p!=0, it must match the iOff value. */
27870
+ assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
27871
+
27872
+ if( p ){
27873
+ pFd->nFetchOut--;
27874
+ }else{
27875
+ unixUnmapfile(pFd);
27876
+ }
27877
+
27878
+ assert( pFd->nFetchOut>=0 );
27879
+ return SQLITE_OK;
27880
+}
2735727881
2735827882
/*
2735927883
** Here ends the implementation of all sqlite3_file methods.
2736027884
**
2736127885
********************** End sqlite3_file Methods *******************************
@@ -27411,11 +27935,13 @@
2741127935
unixSectorSize, /* xSectorSize */ \
2741227936
unixDeviceCharacteristics, /* xDeviceCapabilities */ \
2741327937
unixShmMap, /* xShmMap */ \
2741427938
unixShmLock, /* xShmLock */ \
2741527939
unixShmBarrier, /* xShmBarrier */ \
27416
- unixShmUnmap /* xShmUnmap */ \
27940
+ unixShmUnmap, /* xShmUnmap */ \
27941
+ unixFetch, /* xFetch */ \
27942
+ unixUnfetch, /* xUnfetch */ \
2741727943
}; \
2741827944
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
2741927945
UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
2742027946
return &METHOD; \
2742127947
} \
@@ -27428,11 +27954,11 @@
2742827954
** are also created.
2742927955
*/
2743027956
IOMETHODS(
2743127957
posixIoFinder, /* Finder function name */
2743227958
posixIoMethods, /* sqlite3_io_methods object name */
27433
- 2, /* shared memory is enabled */
27959
+ 3, /* shared memory and mmap are enabled */
2743427960
unixClose, /* xClose method */
2743527961
unixLock, /* xLock method */
2743627962
unixUnlock, /* xUnlock method */
2743727963
unixCheckReservedLock /* xCheckReservedLock method */
2743827964
)
@@ -27679,10 +28205,11 @@
2767928205
OSTRACE(("OPEN %-3d %s\n", h, zFilename));
2768028206
pNew->h = h;
2768128207
pNew->pVfs = pVfs;
2768228208
pNew->zPath = zFilename;
2768328209
pNew->ctrlFlags = (u8)ctrlFlags;
28210
+ pNew->mmapSizeMax = sqlite3GlobalConfig.mxMmap;
2768428211
if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
2768528212
"psow", SQLITE_POWERSAFE_OVERWRITE) ){
2768628213
pNew->ctrlFlags |= UNIXFILE_PSOW;
2768728214
}
2768828215
if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -27823,10 +28350,11 @@
2782328350
if( rc!=SQLITE_OK ){
2782428351
if( h>=0 ) robust_close(pNew, h, __LINE__);
2782528352
}else{
2782628353
pNew->pMethod = pLockingStyle;
2782728354
OpenCounter(+1);
28355
+ verifyDbFile(pNew);
2782828356
}
2782928357
return rc;
2783028358
}
2783128359
2783228360
/*
@@ -29916,11 +30444,11 @@
2991630444
};
2991730445
unsigned int i; /* Loop counter */
2991830446
2991930447
/* Double-check that the aSyscall[] array has been constructed
2992030448
** correctly. See ticket [bb3a86e890c8e96ab] */
29921
- assert( ArraySize(aSyscall)==21 );
30449
+ assert( ArraySize(aSyscall)==24 );
2992230450
2992330451
/* Register all VFSes defined in the aVfs[] array */
2992430452
for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
2992530453
sqlite3_vfs_register(&aVfs[i], i==0);
2992630454
}
@@ -30299,15 +30827,24 @@
3029930827
HANDLE hMutex; /* Mutex used to control access to shared lock */
3030030828
HANDLE hShared; /* Shared memory segment used for locking */
3030130829
winceLock local; /* Locks obtained by this instance of winFile */
3030230830
winceLock *shared; /* Global shared lock memory for the file */
3030330831
#endif
30832
+#if SQLITE_MAX_MMAP_SIZE>0
30833
+ int nFetchOut; /* Number of outstanding xFetch references */
30834
+ HANDLE hMap; /* Handle for accessing memory mapping */
30835
+ void *pMapRegion; /* Area memory mapped */
30836
+ sqlite3_int64 mmapSize; /* Usable size of mapped region */
30837
+ sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
30838
+ sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
30839
+#endif
3030430840
};
3030530841
3030630842
/*
3030730843
** Allowed values for winFile.ctrlFlags
3030830844
*/
30845
+#define WINFILE_RDONLY 0x02 /* Connection is read only */
3030930846
#define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
3031030847
#define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
3031130848
3031230849
/*
3031330850
* The size of the buffer used by sqlite3_win32_write_debug().
@@ -32210,10 +32747,15 @@
3221032747
3221132748
return 0;
3221232749
#endif
3221332750
}
3221432751
32752
+#if SQLITE_MAX_MMAP_SIZE>0
32753
+/* Forward references to VFS methods */
32754
+static int winUnmapfile(winFile*);
32755
+#endif
32756
+
3221532757
/*
3221632758
** Close a file.
3221732759
**
3221832760
** It is reported that an attempt to close a handle might sometimes
3221932761
** fail. This is a very unreasonable result, but Windows is notorious
@@ -32231,10 +32773,16 @@
3223132773
#ifndef SQLITE_OMIT_WAL
3223232774
assert( pFile->pShm==0 );
3223332775
#endif
3223432776
OSTRACE(("CLOSE %d\n", pFile->h));
3223532777
assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
32778
+
32779
+#if SQLITE_MAX_MMAP_SIZE>0
32780
+ rc = winUnmapfile(pFile);
32781
+ if( rc!=SQLITE_OK ) return rc;
32782
+#endif
32783
+
3223632784
do{
3223732785
rc = osCloseHandle(pFile->h);
3223832786
/* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
3223932787
}while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
3224032788
#if SQLITE_OS_WINCE
@@ -32279,12 +32827,30 @@
3227932827
winFile *pFile = (winFile*)id; /* file handle */
3228032828
DWORD nRead; /* Number of bytes actually read from file */
3228132829
int nRetry = 0; /* Number of retrys */
3228232830
3228332831
assert( id!=0 );
32832
+ assert( amt>0 );
3228432833
SimulateIOError(return SQLITE_IOERR_READ);
3228532834
OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
32835
+
32836
+#if SQLITE_MAX_MMAP_SIZE>0
32837
+ /* Deal with as much of this read request as possible by transfering
32838
+ ** data from the memory mapping using memcpy(). */
32839
+ if( offset<pFile->mmapSize ){
32840
+ if( offset+amt <= pFile->mmapSize ){
32841
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
32842
+ return SQLITE_OK;
32843
+ }else{
32844
+ int nCopy = (int)(pFile->mmapSize - offset);
32845
+ memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
32846
+ pBuf = &((u8 *)pBuf)[nCopy];
32847
+ amt -= nCopy;
32848
+ offset += nCopy;
32849
+ }
32850
+ }
32851
+#endif
3228632852
3228732853
#if SQLITE_OS_WINCE
3228832854
if( seekWinFile(pFile, offset) ){
3228932855
return SQLITE_FULL;
3229032856
}
@@ -32330,10 +32896,27 @@
3233032896
assert( pFile );
3233132897
SimulateIOError(return SQLITE_IOERR_WRITE);
3233232898
SimulateDiskfullError(return SQLITE_FULL);
3233332899
3233432900
OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
32901
+
32902
+#if SQLITE_MAX_MMAP_SIZE>0
32903
+ /* Deal with as much of this write request as possible by transfering
32904
+ ** data from the memory mapping using memcpy(). */
32905
+ if( offset<pFile->mmapSize ){
32906
+ if( offset+amt <= pFile->mmapSize ){
32907
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
32908
+ return SQLITE_OK;
32909
+ }else{
32910
+ int nCopy = (int)(pFile->mmapSize - offset);
32911
+ memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
32912
+ pBuf = &((u8 *)pBuf)[nCopy];
32913
+ amt -= nCopy;
32914
+ offset += nCopy;
32915
+ }
32916
+ }
32917
+#endif
3233532918
3233632919
#if SQLITE_OS_WINCE
3233732920
rc = seekWinFile(pFile, offset);
3233832921
if( rc==0 ){
3233932922
#else
@@ -32398,10 +32981,11 @@
3239832981
** Truncate an open file to a specified size
3239932982
*/
3240032983
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
3240132984
winFile *pFile = (winFile*)id; /* File handle object */
3240232985
int rc = SQLITE_OK; /* Return code for this function */
32986
+ DWORD lastErrno;
3240332987
3240432988
assert( pFile );
3240532989
3240632990
OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
3240732991
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
@@ -32416,16 +33000,27 @@
3241633000
}
3241733001
3241833002
/* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
3241933003
if( seekWinFile(pFile, nByte) ){
3242033004
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
32421
- "winTruncate1", pFile->zPath);
32422
- }else if( 0==osSetEndOfFile(pFile->h) ){
32423
- pFile->lastErrno = osGetLastError();
33005
+ "winTruncate1", pFile->zPath);
33006
+ }else if( 0==osSetEndOfFile(pFile->h) &&
33007
+ ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33008
+ pFile->lastErrno = lastErrno;
3242433009
rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
32425
- "winTruncate2", pFile->zPath);
33010
+ "winTruncate2", pFile->zPath);
3242633011
}
33012
+
33013
+#if SQLITE_MAX_MMAP_SIZE>0
33014
+ /* If the file was truncated to a size smaller than the currently
33015
+ ** mapped region, reduce the effective mapping size as well. SQLite will
33016
+ ** use read() and write() to access data beyond this point from now on.
33017
+ */
33018
+ if( pFile->pMapRegion && nByte<pFile->mmapSize ){
33019
+ pFile->mmapSize = nByte;
33020
+ }
33021
+#endif
3242733022
3242833023
OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
3242933024
return rc;
3243033025
}
3243133026
@@ -32790,11 +33385,11 @@
3279033385
assert( id!=0 );
3279133386
if( pFile->locktype>=RESERVED_LOCK ){
3279233387
rc = 1;
3279333388
OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
3279433389
}else{
32795
- rc = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
33390
+ rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
3279633391
if( rc ){
3279733392
winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
3279833393
}
3279933394
rc = !rc;
3280033395
OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
@@ -32930,10 +33525,21 @@
3293033525
getTempname(pFile->pVfs->mxPathname, zTFile);
3293133526
*(char**)pArg = zTFile;
3293233527
}
3293333528
return SQLITE_OK;
3293433529
}
33530
+#if SQLITE_MAX_MMAP_SIZE>0
33531
+ case SQLITE_FCNTL_MMAP_SIZE: {
33532
+ i64 newLimit = *(i64*)pArg;
33533
+ if( newLimit>sqlite3GlobalConfig.mxMmap ){
33534
+ newLimit = sqlite3GlobalConfig.mxMmap;
33535
+ }
33536
+ *(i64*)pArg = pFile->mmapSizeMax;
33537
+ if( newLimit>=0 ) pFile->mmapSizeMax = newLimit;
33538
+ return SQLITE_OK;
33539
+ }
33540
+#endif
3293533541
}
3293633542
return SQLITE_NOTFOUND;
3293733543
}
3293833544
3293933545
/*
@@ -33599,10 +34205,196 @@
3359934205
# define winShmMap 0
3360034206
# define winShmLock 0
3360134207
# define winShmBarrier 0
3360234208
# define winShmUnmap 0
3360334209
#endif /* #ifndef SQLITE_OMIT_WAL */
34210
+
34211
+/*
34212
+** Cleans up the mapped region of the specified file, if any.
34213
+*/
34214
+#if SQLITE_MAX_MMAP_SIZE>0
34215
+static int winUnmapfile(winFile *pFile){
34216
+ assert( pFile!=0 );
34217
+ if( pFile->pMapRegion ){
34218
+ if( !osUnmapViewOfFile(pFile->pMapRegion) ){
34219
+ pFile->lastErrno = osGetLastError();
34220
+ return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34221
+ "winUnmap1", pFile->zPath);
34222
+ }
34223
+ pFile->pMapRegion = 0;
34224
+ pFile->mmapSize = 0;
34225
+ pFile->mmapSizeActual = 0;
34226
+ }
34227
+ if( pFile->hMap!=NULL ){
34228
+ if( !osCloseHandle(pFile->hMap) ){
34229
+ pFile->lastErrno = osGetLastError();
34230
+ return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34231
+ "winUnmap2", pFile->zPath);
34232
+ }
34233
+ pFile->hMap = NULL;
34234
+ }
34235
+ return SQLITE_OK;
34236
+}
34237
+
34238
+/*
34239
+** Memory map or remap the file opened by file-descriptor pFd (if the file
34240
+** is already mapped, the existing mapping is replaced by the new). Or, if
34241
+** there already exists a mapping for this file, and there are still
34242
+** outstanding xFetch() references to it, this function is a no-op.
34243
+**
34244
+** If parameter nByte is non-negative, then it is the requested size of
34245
+** the mapping to create. Otherwise, if nByte is less than zero, then the
34246
+** requested size is the size of the file on disk. The actual size of the
34247
+** created mapping is either the requested size or the value configured
34248
+** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
34249
+**
34250
+** SQLITE_OK is returned if no error occurs (even if the mapping is not
34251
+** recreated as a result of outstanding references) or an SQLite error
34252
+** code otherwise.
34253
+*/
34254
+static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
34255
+ sqlite3_int64 nMap = nByte;
34256
+ int rc;
34257
+
34258
+ assert( nMap>=0 || pFd->nFetchOut==0 );
34259
+ if( pFd->nFetchOut>0 ) return SQLITE_OK;
34260
+
34261
+ if( nMap<0 ){
34262
+ rc = winFileSize((sqlite3_file*)pFd, &nMap);
34263
+ if( rc ){
34264
+ return SQLITE_IOERR_FSTAT;
34265
+ }
34266
+ }
34267
+ if( nMap>pFd->mmapSizeMax ){
34268
+ nMap = pFd->mmapSizeMax;
34269
+ }
34270
+ nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
34271
+
34272
+ if( nMap==0 && pFd->mmapSize>0 ){
34273
+ winUnmapfile(pFd);
34274
+ }
34275
+ if( nMap!=pFd->mmapSize ){
34276
+ void *pNew = 0;
34277
+ DWORD protect = PAGE_READONLY;
34278
+ DWORD flags = FILE_MAP_READ;
34279
+
34280
+ winUnmapfile(pFd);
34281
+ if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
34282
+ protect = PAGE_READWRITE;
34283
+ flags |= FILE_MAP_WRITE;
34284
+ }
34285
+#if SQLITE_OS_WINRT
34286
+ pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
34287
+#elif defined(SQLITE_WIN32_HAS_WIDE)
34288
+ pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
34289
+ (DWORD)((nMap>>32) & 0xffffffff),
34290
+ (DWORD)(nMap & 0xffffffff), NULL);
34291
+#elif defined(SQLITE_WIN32_HAS_ANSI)
34292
+ pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
34293
+ (DWORD)((nMap>>32) & 0xffffffff),
34294
+ (DWORD)(nMap & 0xffffffff), NULL);
34295
+#endif
34296
+ if( pFd->hMap==NULL ){
34297
+ pFd->lastErrno = osGetLastError();
34298
+ rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34299
+ "winMapfile", pFd->zPath);
34300
+ /* Log the error, but continue normal operation using xRead/xWrite */
34301
+ return SQLITE_OK;
34302
+ }
34303
+ assert( (nMap % winSysInfo.dwPageSize)==0 );
34304
+#if SQLITE_OS_WINRT
34305
+ pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, nMap);
34306
+#else
34307
+ assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
34308
+ pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
34309
+#endif
34310
+ if( pNew==NULL ){
34311
+ osCloseHandle(pFd->hMap);
34312
+ pFd->hMap = NULL;
34313
+ pFd->lastErrno = osGetLastError();
34314
+ winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34315
+ "winMapfile", pFd->zPath);
34316
+ return SQLITE_OK;
34317
+ }
34318
+ pFd->pMapRegion = pNew;
34319
+ pFd->mmapSize = nMap;
34320
+ pFd->mmapSizeActual = nMap;
34321
+ }
34322
+
34323
+ return SQLITE_OK;
34324
+}
34325
+#endif /* SQLITE_MAX_MMAP_SIZE>0 */
34326
+
34327
+/*
34328
+** If possible, return a pointer to a mapping of file fd starting at offset
34329
+** iOff. The mapping must be valid for at least nAmt bytes.
34330
+**
34331
+** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
34332
+** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
34333
+** Finally, if an error does occur, return an SQLite error code. The final
34334
+** value of *pp is undefined in this case.
34335
+**
34336
+** If this function does return a pointer, the caller must eventually
34337
+** release the reference by calling unixUnfetch().
34338
+*/
34339
+static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
34340
+#if SQLITE_MAX_MMAP_SIZE>0
34341
+ winFile *pFd = (winFile*)fd; /* The underlying database file */
34342
+#endif
34343
+ *pp = 0;
34344
+
34345
+#if SQLITE_MAX_MMAP_SIZE>0
34346
+ if( pFd->mmapSizeMax>0 ){
34347
+ if( pFd->pMapRegion==0 ){
34348
+ int rc = winMapfile(pFd, -1);
34349
+ if( rc!=SQLITE_OK ) return rc;
34350
+ }
34351
+ if( pFd->mmapSize >= iOff+nAmt ){
34352
+ *pp = &((u8 *)pFd->pMapRegion)[iOff];
34353
+ pFd->nFetchOut++;
34354
+ }
34355
+ }
34356
+#endif
34357
+ return SQLITE_OK;
34358
+}
34359
+
34360
+/*
34361
+** If the third argument is non-NULL, then this function releases a
34362
+** reference obtained by an earlier call to unixFetch(). The second
34363
+** argument passed to this function must be the same as the corresponding
34364
+** argument that was passed to the unixFetch() invocation.
34365
+**
34366
+** Or, if the third argument is NULL, then this function is being called
34367
+** to inform the VFS layer that, according to POSIX, any existing mapping
34368
+** may now be invalid and should be unmapped.
34369
+*/
34370
+static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
34371
+#if SQLITE_MAX_MMAP_SIZE>0
34372
+ winFile *pFd = (winFile*)fd; /* The underlying database file */
34373
+
34374
+ /* If p==0 (unmap the entire file) then there must be no outstanding
34375
+ ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
34376
+ ** then there must be at least one outstanding. */
34377
+ assert( (p==0)==(pFd->nFetchOut==0) );
34378
+
34379
+ /* If p!=0, it must match the iOff value. */
34380
+ assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
34381
+
34382
+ if( p ){
34383
+ pFd->nFetchOut--;
34384
+ }else{
34385
+ /* FIXME: If Windows truly always prevents truncating or deleting a
34386
+ ** file while a mapping is held, then the following winUnmapfile() call
34387
+ ** is unnecessary can can be omitted - potentially improving
34388
+ ** performance. */
34389
+ winUnmapfile(pFd);
34390
+ }
34391
+
34392
+ assert( pFd->nFetchOut>=0 );
34393
+#endif
34394
+ return SQLITE_OK;
34395
+}
3360434396
3360534397
/*
3360634398
** Here ends the implementation of all sqlite3_file methods.
3360734399
**
3360834400
********************** End sqlite3_file Methods *******************************
@@ -33611,11 +34403,11 @@
3361134403
/*
3361234404
** This vector defines all the methods that can operate on an
3361334405
** sqlite3_file for win32.
3361434406
*/
3361534407
static const sqlite3_io_methods winIoMethod = {
33616
- 2, /* iVersion */
34408
+ 3, /* iVersion */
3361734409
winClose, /* xClose */
3361834410
winRead, /* xRead */
3361934411
winWrite, /* xWrite */
3362034412
winTruncate, /* xTruncate */
3362134413
winSync, /* xSync */
@@ -33627,11 +34419,13 @@
3362734419
winSectorSize, /* xSectorSize */
3362834420
winDeviceCharacteristics, /* xDeviceCharacteristics */
3362934421
winShmMap, /* xShmMap */
3363034422
winShmLock, /* xShmLock */
3363134423
winShmBarrier, /* xShmBarrier */
33632
- winShmUnmap /* xShmUnmap */
34424
+ winShmUnmap, /* xShmUnmap */
34425
+ winFetch, /* xFetch */
34426
+ winUnfetch /* xUnfetch */
3363334427
};
3363434428
3363534429
/****************************************************************************
3363634430
**************************** sqlite3_vfs methods ****************************
3363734431
**
@@ -33803,13 +34597,11 @@
3380334597
#endif
3380434598
3380534599
int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
3380634600
int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
3380734601
int isCreate = (flags & SQLITE_OPEN_CREATE);
33808
-#ifndef NDEBUG
3380934602
int isReadonly = (flags & SQLITE_OPEN_READONLY);
33810
-#endif
3381134603
int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
3381234604
3381334605
#ifndef NDEBUG
3381434606
int isOpenJournal = (isCreate && (
3381534607
eType==SQLITE_OPEN_MASTER_JOURNAL
@@ -34016,15 +34808,25 @@
3401634808
}
3401734809
3401834810
pFile->pMethod = &winIoMethod;
3401934811
pFile->pVfs = pVfs;
3402034812
pFile->h = h;
34813
+ if( isReadonly ){
34814
+ pFile->ctrlFlags |= WINFILE_RDONLY;
34815
+ }
3402134816
if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
3402234817
pFile->ctrlFlags |= WINFILE_PSOW;
3402334818
}
3402434819
pFile->lastErrno = NO_ERROR;
3402534820
pFile->zPath = zName;
34821
+#if SQLITE_MAX_MMAP_SIZE>0
34822
+ pFile->hMap = NULL;
34823
+ pFile->pMapRegion = 0;
34824
+ pFile->mmapSize = 0;
34825
+ pFile->mmapSizeActual = 0;
34826
+ pFile->mmapSizeMax = sqlite3GlobalConfig.mxMmap;
34827
+#endif
3402634828
3402734829
OpenCounter(+1);
3402834830
return rc;
3402934831
}
3403034832
@@ -34649,20 +35451,19 @@
3464935451
3465035452
/* Double-check that the aSyscall[] array has been constructed
3465135453
** correctly. See ticket [bb3a86e890c8e96ab] */
3465235454
assert( ArraySize(aSyscall)==74 );
3465335455
34654
-#ifndef SQLITE_OMIT_WAL
3465535456
/* get memory map allocation granularity */
3465635457
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
3465735458
#if SQLITE_OS_WINRT
3465835459
osGetNativeSystemInfo(&winSysInfo);
3465935460
#else
3466035461
osGetSystemInfo(&winSysInfo);
3466135462
#endif
34662
- assert(winSysInfo.dwAllocationGranularity > 0);
34663
-#endif
35463
+ assert( winSysInfo.dwAllocationGranularity>0 );
35464
+ assert( winSysInfo.dwPageSize>0 );
3466435465
3466535466
sqlite3_vfs_register(&winVfs, 1);
3466635467
return SQLITE_OK;
3466735468
}
3466835469
@@ -37295,11 +38096,10 @@
3729538096
# define sqlite3WalOpen(x,y,z) 0
3729638097
# define sqlite3WalLimit(x,y)
3729738098
# define sqlite3WalClose(w,x,y,z) 0
3729838099
# define sqlite3WalBeginReadTransaction(y,z) 0
3729938100
# define sqlite3WalEndReadTransaction(z)
37300
-# define sqlite3WalRead(v,w,x,y,z) 0
3730138101
# define sqlite3WalDbsize(y) 0
3730238102
# define sqlite3WalBeginWriteTransaction(y) 0
3730338103
# define sqlite3WalEndWriteTransaction(x) 0
3730438104
# define sqlite3WalUndo(x,y,z) 0
3730538105
# define sqlite3WalSavepoint(y,z)
@@ -37335,11 +38135,12 @@
3733538135
*/
3733638136
SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
3733738137
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
3733838138
3733938139
/* Read a page from the write-ahead log, if it is present. */
37340
-SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
38140
+SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
38141
+SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
3734138142
3734238143
/* If the WAL is not empty, return the size of the database. */
3734338144
SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
3734438145
3734538146
/* Obtain or release the WRITER lock. */
@@ -38035,10 +38836,15 @@
3803538836
i64 journalHdr; /* Byte offset to previous journal header */
3803638837
sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
3803738838
PagerSavepoint *aSavepoint; /* Array of active savepoints */
3803838839
int nSavepoint; /* Number of elements in aSavepoint[] */
3803938840
char dbFileVers[16]; /* Changes whenever database file changes */
38841
+
38842
+ u8 bUseFetch; /* True to use xFetch() */
38843
+ int nMmapOut; /* Number of mmap pages currently outstanding */
38844
+ sqlite3_int64 szMmap; /* Desired maximum mmap size */
38845
+ PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
3804038846
/*
3804138847
** End of the routinely-changing class members
3804238848
***************************************************************************/
3804338849
3804438850
u16 nExtra; /* Add this many bytes to each in-memory page */
@@ -38145,10 +38951,20 @@
3814538951
# define MEMDB 0
3814638952
#else
3814738953
# define MEMDB pPager->memDb
3814838954
#endif
3814938955
38956
+/*
38957
+** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
38958
+** interfaces to access the database using memory-mapped I/O.
38959
+*/
38960
+#if SQLITE_MAX_MMAP_SIZE>0
38961
+# define USEFETCH(x) ((x)->bUseFetch)
38962
+#else
38963
+# define USEFETCH(x) 0
38964
+#endif
38965
+
3815038966
/*
3815138967
** The maximum legal page number is (2^31 - 1).
3815238968
*/
3815338969
#define PAGER_MAX_PGNO 2147483647
3815438970
@@ -39632,11 +40448,11 @@
3963240448
&& isSynced
3963340449
){
3963440450
i64 ofst = (pgno-1)*(i64)pPager->pageSize;
3963540451
testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
3963640452
assert( !pagerUseWal(pPager) );
39637
- rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
40453
+ rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
3963840454
if( pgno>pPager->dbFileSize ){
3963940455
pPager->dbFileSize = pgno;
3964040456
}
3964140457
if( pPager->pBackup ){
3964240458
CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
@@ -40023,10 +40839,11 @@
4002340839
Pgno mxPg = 0; /* Size of the original file in pages */
4002440840
int rc; /* Result code of a subroutine */
4002540841
int res = 1; /* Value returned by sqlite3OsAccess() */
4002640842
char *zMaster = 0; /* Name of master journal file if any */
4002740843
int needPagerReset; /* True to reset page prior to first page rollback */
40844
+ int nPlayback = 0; /* Total number of pages restored from journal */
4002840845
4002940846
/* Figure out how many records are in the journal. Abort early if
4003040847
** the journal is empty.
4003140848
*/
4003240849
assert( isOpen(pPager->jfd) );
@@ -40123,11 +40940,13 @@
4012340940
if( needPagerReset ){
4012440941
pager_reset(pPager);
4012540942
needPagerReset = 0;
4012640943
}
4012740944
rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40128
- if( rc!=SQLITE_OK ){
40945
+ if( rc==SQLITE_OK ){
40946
+ nPlayback++;
40947
+ }else{
4012940948
if( rc==SQLITE_DONE ){
4013040949
pPager->journalOff = szJ;
4013140950
break;
4013240951
}else if( rc==SQLITE_IOERR_SHORT_READ ){
4013340952
/* If the journal has been truncated, simply stop reading and
@@ -40193,10 +41012,14 @@
4019341012
** see if it is possible to delete the master journal.
4019441013
*/
4019541014
rc = pager_delmaster(pPager, zMaster);
4019641015
testcase( rc!=SQLITE_OK );
4019741016
}
41017
+ if( isHot && nPlayback ){
41018
+ sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
41019
+ nPlayback, pPager->zJournal);
41020
+ }
4019841021
4019941022
/* The Pager.sectorSize variable may have been updated while rolling
4020041023
** back a journal created by a process with a different sector size
4020141024
** value. Reset it to the correct value for this process.
4020241025
*/
@@ -40214,15 +41037,14 @@
4021441037
** the value read from the database file.
4021541038
**
4021641039
** If an IO error occurs, then the IO error is returned to the caller.
4021741040
** Otherwise, SQLITE_OK is returned.
4021841041
*/
40219
-static int readDbPage(PgHdr *pPg){
41042
+static int readDbPage(PgHdr *pPg, u32 iFrame){
4022041043
Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
4022141044
Pgno pgno = pPg->pgno; /* Page number to read */
4022241045
int rc = SQLITE_OK; /* Return code */
40223
- int isInWal = 0; /* True if page is in log file */
4022441046
int pgsz = pPager->pageSize; /* Number of bytes to read */
4022541047
4022641048
assert( pPager->eState>=PAGER_READER && !MEMDB );
4022741049
assert( isOpen(pPager->fd) );
4022841050
@@ -40230,15 +41052,14 @@
4023041052
assert( pPager->tempFile );
4023141053
memset(pPg->pData, 0, pPager->pageSize);
4023241054
return SQLITE_OK;
4023341055
}
4023441056
40235
- if( pagerUseWal(pPager) ){
41057
+ if( iFrame ){
4023641058
/* Try to pull the page from the write-ahead log. */
40237
- rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
40238
- }
40239
- if( rc==SQLITE_OK && !isInWal ){
41059
+ rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
41060
+ }else{
4024041061
i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
4024141062
rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
4024241063
if( rc==SQLITE_IOERR_SHORT_READ ){
4024341064
rc = SQLITE_OK;
4024441065
}
@@ -40313,16 +41134,21 @@
4031341134
static int pagerUndoCallback(void *pCtx, Pgno iPg){
4031441135
int rc = SQLITE_OK;
4031541136
Pager *pPager = (Pager *)pCtx;
4031641137
PgHdr *pPg;
4031741138
41139
+ assert( pagerUseWal(pPager) );
4031841140
pPg = sqlite3PagerLookup(pPager, iPg);
4031941141
if( pPg ){
4032041142
if( sqlite3PcachePageRefcount(pPg)==1 ){
4032141143
sqlite3PcacheDrop(pPg);
4032241144
}else{
40323
- rc = readDbPage(pPg);
41145
+ u32 iFrame = 0;
41146
+ rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
41147
+ if( rc==SQLITE_OK ){
41148
+ rc = readDbPage(pPg, iFrame);
41149
+ }
4032441150
if( rc==SQLITE_OK ){
4032541151
pPager->xReiniter(pPg);
4032641152
}
4032741153
sqlite3PagerUnref(pPg);
4032841154
}
@@ -40462,10 +41288,11 @@
4046241288
sqlite3WalEndReadTransaction(pPager->pWal);
4046341289
4046441290
rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
4046541291
if( rc!=SQLITE_OK || changed ){
4046641292
pager_reset(pPager);
41293
+ if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
4046741294
}
4046841295
4046941296
return rc;
4047041297
}
4047141298
#endif
@@ -40722,10 +41549,33 @@
4072241549
** Change the maximum number of in-memory pages that are allowed.
4072341550
*/
4072441551
SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
4072541552
sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
4072641553
}
41554
+
41555
+/*
41556
+** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
41557
+*/
41558
+static void pagerFixMaplimit(Pager *pPager){
41559
+#if SQLITE_MAX_MMAP_SIZE>0
41560
+ sqlite3_file *fd = pPager->fd;
41561
+ if( isOpen(fd) ){
41562
+ sqlite3_int64 sz;
41563
+ pPager->bUseFetch = (fd->pMethods->iVersion>=3) && pPager->szMmap>0;
41564
+ sz = pPager->szMmap;
41565
+ sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
41566
+ }
41567
+#endif
41568
+}
41569
+
41570
+/*
41571
+** Change the maximum size of any memory mapping made of the database file.
41572
+*/
41573
+SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
41574
+ pPager->szMmap = szMmap;
41575
+ pagerFixMaplimit(pPager);
41576
+}
4072741577
4072841578
/*
4072941579
** Free as much memory as possible from the pager.
4073041580
*/
4073141581
SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
@@ -40958,10 +41808,11 @@
4095841808
if( rc==SQLITE_OK ){
4095941809
if( nReserve<0 ) nReserve = pPager->nReserve;
4096041810
assert( nReserve>=0 && nReserve<1000 );
4096141811
pPager->nReserve = (i16)nReserve;
4096241812
pagerReportSize(pPager);
41813
+ pagerFixMaplimit(pPager);
4096341814
}
4096441815
return rc;
4096541816
}
4096641817
4096741818
/*
@@ -41182,10 +42033,85 @@
4118242033
if( rc==SQLITE_OK ){
4118342034
rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
4118442035
}
4118542036
return rc;
4118642037
}
42038
+
42039
+/*
42040
+** Obtain a reference to a memory mapped page object for page number pgno.
42041
+** The new object will use the pointer pData, obtained from xFetch().
42042
+** If successful, set *ppPage to point to the new page reference
42043
+** and return SQLITE_OK. Otherwise, return an SQLite error code and set
42044
+** *ppPage to zero.
42045
+**
42046
+** Page references obtained by calling this function should be released
42047
+** by calling pagerReleaseMapPage().
42048
+*/
42049
+static int pagerAcquireMapPage(
42050
+ Pager *pPager, /* Pager object */
42051
+ Pgno pgno, /* Page number */
42052
+ void *pData, /* xFetch()'d data for this page */
42053
+ PgHdr **ppPage /* OUT: Acquired page object */
42054
+){
42055
+ PgHdr *p; /* Memory mapped page to return */
42056
+
42057
+ if( pPager->pMmapFreelist ){
42058
+ *ppPage = p = pPager->pMmapFreelist;
42059
+ pPager->pMmapFreelist = p->pDirty;
42060
+ p->pDirty = 0;
42061
+ memset(p->pExtra, 0, pPager->nExtra);
42062
+ }else{
42063
+ *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
42064
+ if( p==0 ){
42065
+ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
42066
+ return SQLITE_NOMEM;
42067
+ }
42068
+ p->pExtra = (void *)&p[1];
42069
+ p->flags = PGHDR_MMAP;
42070
+ p->nRef = 1;
42071
+ p->pPager = pPager;
42072
+ }
42073
+
42074
+ assert( p->pExtra==(void *)&p[1] );
42075
+ assert( p->pPage==0 );
42076
+ assert( p->flags==PGHDR_MMAP );
42077
+ assert( p->pPager==pPager );
42078
+ assert( p->nRef==1 );
42079
+
42080
+ p->pgno = pgno;
42081
+ p->pData = pData;
42082
+ pPager->nMmapOut++;
42083
+
42084
+ return SQLITE_OK;
42085
+}
42086
+
42087
+/*
42088
+** Release a reference to page pPg. pPg must have been returned by an
42089
+** earlier call to pagerAcquireMapPage().
42090
+*/
42091
+static void pagerReleaseMapPage(PgHdr *pPg){
42092
+ Pager *pPager = pPg->pPager;
42093
+ pPager->nMmapOut--;
42094
+ pPg->pDirty = pPager->pMmapFreelist;
42095
+ pPager->pMmapFreelist = pPg;
42096
+
42097
+ assert( pPager->fd->pMethods->iVersion>=3 );
42098
+ sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
42099
+}
42100
+
42101
+/*
42102
+** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
42103
+*/
42104
+static void pagerFreeMapHdrs(Pager *pPager){
42105
+ PgHdr *p;
42106
+ PgHdr *pNext;
42107
+ for(p=pPager->pMmapFreelist; p; p=pNext){
42108
+ pNext = p->pDirty;
42109
+ sqlite3_free(p);
42110
+ }
42111
+}
42112
+
4118742113
4118842114
/*
4118942115
** Shutdown the page cache. Free all memory and close all files.
4119042116
**
4119142117
** If a transaction was in progress when this routine is called, that
@@ -41203,10 +42129,11 @@
4120342129
u8 *pTmp = (u8 *)pPager->pTmpSpace;
4120442130
4120542131
assert( assert_pager_state(pPager) );
4120642132
disable_simulated_io_errors();
4120742133
sqlite3BeginBenignMalloc();
42134
+ pagerFreeMapHdrs(pPager);
4120842135
/* pPager->errCode = 0; */
4120942136
pPager->exclusiveMode = 0;
4121042137
#ifndef SQLITE_OMIT_WAL
4121142138
sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
4121242139
pPager->pWal = 0;
@@ -41464,11 +42391,13 @@
4146442391
4146542392
/* Before the first write, give the VFS a hint of what the final
4146642393
** file size will be.
4146742394
*/
4146842395
assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
41469
- if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
42396
+ if( rc==SQLITE_OK
42397
+ && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize
42398
+ ){
4147042399
sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
4147142400
sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
4147242401
pPager->dbHintSize = pPager->dbSize;
4147342402
}
4147442403
@@ -42018,10 +42947,11 @@
4201842947
}
4201942948
/* pPager->xBusyHandler = 0; */
4202042949
/* pPager->pBusyHandlerArg = 0; */
4202142950
pPager->xReiniter = xReinit;
4202242951
/* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
42952
+ /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
4202342953
4202442954
*ppPager = pPager;
4202542955
return SQLITE_OK;
4202642956
}
4202742957
@@ -42309,13 +43239,15 @@
4230943239
assert( (pPager->eLock==SHARED_LOCK)
4231043240
|| (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
4231143241
);
4231243242
}
4231343243
42314
- if( !pPager->tempFile
42315
- && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
42316
- ){
43244
+ if( !pPager->tempFile && (
43245
+ pPager->pBackup
43246
+ || sqlite3PcachePagecount(pPager->pPCache)>0
43247
+ || USEFETCH(pPager)
43248
+ )){
4231743249
/* The shared-lock has just been acquired on the database file
4231843250
** and there are already pages in the cache (from a previous
4231943251
** read or write transaction). Check to see if the database
4232043252
** has been modified. If the database has changed, flush the
4232143253
** cache.
@@ -42337,19 +43269,29 @@
4233743269
if( rc ) goto failed;
4233843270
4233943271
if( nPage>0 ){
4234043272
IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
4234143273
rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
42342
- if( rc!=SQLITE_OK ){
43274
+ if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
4234343275
goto failed;
4234443276
}
4234543277
}else{
4234643278
memset(dbFileVers, 0, sizeof(dbFileVers));
4234743279
}
4234843280
4234943281
if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
4235043282
pager_reset(pPager);
43283
+
43284
+ /* Unmap the database file. It is possible that external processes
43285
+ ** may have truncated the database file and then extended it back
43286
+ ** to its original size while this process was not holding a lock.
43287
+ ** In this case there may exist a Pager.pMap mapping that appears
43288
+ ** to be the right size but is not actually valid. Avoid this
43289
+ ** possibility by unmapping the db here. */
43290
+ if( USEFETCH(pPager) ){
43291
+ sqlite3OsUnfetch(pPager->fd, 0, 0);
43292
+ }
4235143293
}
4235243294
}
4235343295
4235443296
/* If there is a WAL file in the file-system, open this database in WAL
4235543297
** mode. Otherwise, the following function call is a no-op.
@@ -42387,11 +43329,11 @@
4238743329
** Except, in locking_mode=EXCLUSIVE when there is nothing to in
4238843330
** the rollback journal, the unlock is not performed and there is
4238943331
** nothing to rollback, so this routine is a no-op.
4239043332
*/
4239143333
static void pagerUnlockIfUnused(Pager *pPager){
42392
- if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
43334
+ if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
4239343335
pagerUnlockAndRollback(pPager);
4239443336
}
4239543337
}
4239643338
4239743339
/*
@@ -42446,17 +43388,31 @@
4244643388
*/
4244743389
SQLITE_PRIVATE int sqlite3PagerAcquire(
4244843390
Pager *pPager, /* The pager open on the database file */
4244943391
Pgno pgno, /* Page number to fetch */
4245043392
DbPage **ppPage, /* Write a pointer to the page here */
42451
- int noContent /* Do not bother reading content from disk if true */
43393
+ int flags /* PAGER_ACQUIRE_XXX flags */
4245243394
){
42453
- int rc;
42454
- PgHdr *pPg;
43395
+ int rc = SQLITE_OK;
43396
+ PgHdr *pPg = 0;
43397
+ u32 iFrame = 0; /* Frame to read from WAL file */
43398
+ const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);
43399
+
43400
+ /* It is acceptable to use a read-only (mmap) page for any page except
43401
+ ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
43402
+ ** flag was specified by the caller. And so long as the db is not a
43403
+ ** temporary or in-memory database. */
43404
+ const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
43405
+ && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
43406
+#ifdef SQLITE_HAS_CODEC
43407
+ && pPager->xCodec==0
43408
+#endif
43409
+ );
4245543410
4245643411
assert( pPager->eState>=PAGER_READER );
4245743412
assert( assert_pager_state(pPager) );
43413
+ assert( noContent==0 || bMmapOk==0 );
4245843414
4245943415
if( pgno==0 ){
4246043416
return SQLITE_CORRUPT_BKPT;
4246143417
}
4246243418
@@ -42463,10 +43419,43 @@
4246343419
/* If the pager is in the error state, return an error immediately.
4246443420
** Otherwise, request the page from the PCache layer. */
4246543421
if( pPager->errCode!=SQLITE_OK ){
4246643422
rc = pPager->errCode;
4246743423
}else{
43424
+
43425
+ if( bMmapOk && pagerUseWal(pPager) ){
43426
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
43427
+ if( rc!=SQLITE_OK ) goto pager_acquire_err;
43428
+ }
43429
+
43430
+ if( iFrame==0 && bMmapOk ){
43431
+ void *pData = 0;
43432
+
43433
+ rc = sqlite3OsFetch(pPager->fd,
43434
+ (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
43435
+ );
43436
+
43437
+ if( rc==SQLITE_OK && pData ){
43438
+ if( pPager->eState>PAGER_READER ){
43439
+ (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
43440
+ }
43441
+ if( pPg==0 ){
43442
+ rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
43443
+ }else{
43444
+ sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
43445
+ }
43446
+ if( pPg ){
43447
+ assert( rc==SQLITE_OK );
43448
+ *ppPage = pPg;
43449
+ return SQLITE_OK;
43450
+ }
43451
+ }
43452
+ if( rc!=SQLITE_OK ){
43453
+ goto pager_acquire_err;
43454
+ }
43455
+ }
43456
+
4246843457
rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
4246943458
}
4247043459
4247143460
if( rc!=SQLITE_OK ){
4247243461
/* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -42521,13 +43510,17 @@
4252143510
sqlite3EndBenignMalloc();
4252243511
}
4252343512
memset(pPg->pData, 0, pPager->pageSize);
4252443513
IOTRACE(("ZERO %p %d\n", pPager, pgno));
4252543514
}else{
43515
+ if( pagerUseWal(pPager) && bMmapOk==0 ){
43516
+ rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
43517
+ if( rc!=SQLITE_OK ) goto pager_acquire_err;
43518
+ }
4252643519
assert( pPg->pPager==pPager );
4252743520
pPager->aStat[PAGER_STAT_MISS]++;
42528
- rc = readDbPage(pPg);
43521
+ rc = readDbPage(pPg, iFrame);
4252943522
if( rc!=SQLITE_OK ){
4253043523
goto pager_acquire_err;
4253143524
}
4253243525
}
4253343526
pager_set_pagehash(pPg);
@@ -42576,11 +43569,15 @@
4257643569
** removed.
4257743570
*/
4257843571
SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
4257943572
if( pPg ){
4258043573
Pager *pPager = pPg->pPager;
42581
- sqlite3PcacheRelease(pPg);
43574
+ if( pPg->flags & PGHDR_MMAP ){
43575
+ pagerReleaseMapPage(pPg);
43576
+ }else{
43577
+ sqlite3PcacheRelease(pPg);
43578
+ }
4258243579
pagerUnlockIfUnused(pPager);
4258343580
}
4258443581
}
4258543582
4258643583
/*
@@ -42911,10 +43908,11 @@
4291143908
4291243909
PgHdr *pPg = pDbPage;
4291343910
Pager *pPager = pPg->pPager;
4291443911
Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4291543912
43913
+ assert( (pPg->flags & PGHDR_MMAP)==0 );
4291643914
assert( pPager->eState>=PAGER_WRITER_LOCKED );
4291743915
assert( pPager->eState!=PAGER_ERROR );
4291843916
assert( assert_pager_state(pPager) );
4291943917
4292043918
if( nPagePerSector>1 ){
@@ -43467,11 +44465,11 @@
4346744465
}else{
4346844466
rc = pager_playback(pPager, 0);
4346944467
}
4347044468
4347144469
assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
43472
- assert( rc==SQLITE_OK || rc==SQLITE_FULL
44470
+ assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
4347344471
|| rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
4347444472
4347544473
/* If an error occurs during a ROLLBACK, we can no longer trust the pager
4347644474
** cache. So call pager_error() on the way out to make any error persistent.
4347744475
*/
@@ -44201,15 +45199,16 @@
4420145199
4420245200
/* Open the connection to the log file. If this operation fails,
4420345201
** (e.g. due to malloc() failure), return an error code.
4420445202
*/
4420545203
if( rc==SQLITE_OK ){
44206
- rc = sqlite3WalOpen(pPager->pVfs,
45204
+ rc = sqlite3WalOpen(pPager->pVfs,
4420745205
pPager->fd, pPager->zWal, pPager->exclusiveMode,
4420845206
pPager->journalSizeLimit, &pPager->pWal
4420945207
);
4421045208
}
45209
+ pagerFixMaplimit(pPager);
4421145210
4421245211
return rc;
4421345212
}
4421445213
4421545214
@@ -44296,10 +45295,11 @@
4429645295
rc = pagerExclusiveLock(pPager);
4429745296
if( rc==SQLITE_OK ){
4429845297
rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
4429945298
pPager->pageSize, (u8*)pPager->pTmpSpace);
4430045299
pPager->pWal = 0;
45300
+ pagerFixMaplimit(pPager);
4430145301
}
4430245302
}
4430345303
return rc;
4430445304
}
4430545305
@@ -45544,12 +46544,13 @@
4554446544
** event via sqlite3_log(). This is to help with identifying performance
4554546545
** problems caused by applications routinely shutting down without
4554646546
** checkpointing the log file.
4554746547
*/
4554846548
if( pWal->hdr.nPage ){
45549
- sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
45550
- pWal->hdr.nPage, pWal->zWalName
46549
+ sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
46550
+ "recovered %d frames from WAL file %s",
46551
+ pWal->hdr.mxFrame, pWal->zWalName
4555146552
);
4555246553
}
4555346554
}
4555446555
4555546556
recovery_error:
@@ -46059,20 +47060,21 @@
4605947060
/* Sync the WAL to disk */
4606047061
if( sync_flags ){
4606147062
rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
4606247063
}
4606347064
46064
- /* If the database file may grow as a result of this checkpoint, hint
46065
- ** about the eventual size of the db file to the VFS layer.
47065
+ /* If the database may grow as a result of this checkpoint, hint
47066
+ ** about the eventual size of the db file to the VFS layer.
4606647067
*/
4606747068
if( rc==SQLITE_OK ){
4606847069
i64 nReq = ((i64)mxPage * szPage);
4606947070
rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
4607047071
if( rc==SQLITE_OK && nSize<nReq ){
4607147072
sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
4607247073
}
4607347074
}
47075
+
4607447076
4607547077
/* Iterate through the contents of the WAL, copying data to the db file. */
4607647078
while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
4607747079
i64 iOffset;
4607847080
assert( walFramePgno(pWal, iFrame)==iDbpage );
@@ -46624,23 +47626,21 @@
4662447626
pWal->readLock = -1;
4662547627
}
4662647628
}
4662747629
4662847630
/*
46629
-** Read a page from the WAL, if it is present in the WAL and if the
46630
-** current read transaction is configured to use the WAL.
47631
+** Search the wal file for page pgno. If found, set *piRead to the frame that
47632
+** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
47633
+** to zero.
4663147634
**
46632
-** The *pInWal is set to 1 if the requested page is in the WAL and
46633
-** has been loaded. Or *pInWal is set to 0 if the page was not in
46634
-** the WAL and needs to be read out of the database.
47635
+** Return SQLITE_OK if successful, or an error code if an error occurs. If an
47636
+** error does occur, the final value of *piRead is undefined.
4663547637
*/
46636
-SQLITE_PRIVATE int sqlite3WalRead(
47638
+SQLITE_PRIVATE int sqlite3WalFindFrame(
4663747639
Wal *pWal, /* WAL handle */
4663847640
Pgno pgno, /* Database page number to read data for */
46639
- int *pInWal, /* OUT: True if data is read from WAL */
46640
- int nOut, /* Size of buffer pOut in bytes */
46641
- u8 *pOut /* Buffer to write page data to */
47641
+ u32 *piRead /* OUT: Frame number (or zero) */
4664247642
){
4664347643
u32 iRead = 0; /* If !=0, WAL frame to return data from */
4664447644
u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
4664547645
int iHash; /* Used to loop through N hash tables */
4664647646
@@ -46652,11 +47652,11 @@
4665247652
** in this case as an optimization. Likewise, if pWal->readLock==0,
4665347653
** then the WAL is ignored by the reader so return early, as if the
4665447654
** WAL were empty.
4665547655
*/
4665647656
if( iLast==0 || pWal->readLock==0 ){
46657
- *pInWal = 0;
47657
+ *piRead = 0;
4665847658
return SQLITE_OK;
4665947659
}
4666047660
4666147661
/* Search the hash table or tables for an entry matching page number
4666247662
** pgno. Each iteration of the following for() loop searches one
@@ -46723,30 +47723,35 @@
4672347723
}
4672447724
assert( iRead==iRead2 );
4672547725
}
4672647726
#endif
4672747727
46728
- /* If iRead is non-zero, then it is the log frame number that contains the
46729
- ** required page. Read and return data from the log file.
46730
- */
46731
- if( iRead ){
46732
- int sz;
46733
- i64 iOffset;
46734
- sz = pWal->hdr.szPage;
46735
- sz = (sz&0xfe00) + ((sz&0x0001)<<16);
46736
- testcase( sz<=32768 );
46737
- testcase( sz>=65536 );
46738
- iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
46739
- *pInWal = 1;
46740
- /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46741
- return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
46742
- }
46743
-
46744
- *pInWal = 0;
47728
+ *piRead = iRead;
4674547729
return SQLITE_OK;
4674647730
}
4674747731
47732
+/*
47733
+** Read the contents of frame iRead from the wal file into buffer pOut
47734
+** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
47735
+** error code otherwise.
47736
+*/
47737
+SQLITE_PRIVATE int sqlite3WalReadFrame(
47738
+ Wal *pWal, /* WAL handle */
47739
+ u32 iRead, /* Frame to read */
47740
+ int nOut, /* Size of buffer pOut in bytes */
47741
+ u8 *pOut /* Buffer to write page data to */
47742
+){
47743
+ int sz;
47744
+ i64 iOffset;
47745
+ sz = pWal->hdr.szPage;
47746
+ sz = (sz&0xfe00) + ((sz&0x0001)<<16);
47747
+ testcase( sz<=32768 );
47748
+ testcase( sz>=65536 );
47749
+ iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
47750
+ /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
47751
+ return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
47752
+}
4674847753
4674947754
/*
4675047755
** Return the size of the database in pages (or zero, if unknown).
4675147756
*/
4675247757
SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
@@ -47289,10 +48294,13 @@
4728948294
}
4729048295
4729148296
/* Read the wal-index header. */
4729248297
if( rc==SQLITE_OK ){
4729348298
rc = walIndexReadHdr(pWal, &isChanged);
48299
+ if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
48300
+ sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
48301
+ }
4729448302
}
4729548303
4729648304
/* Copy data from the log to the database file. */
4729748305
if( rc==SQLITE_OK ){
4729848306
if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
@@ -49960,17 +50968,21 @@
4996050968
*/
4996150969
static int btreeGetPage(
4996250970
BtShared *pBt, /* The btree */
4996350971
Pgno pgno, /* Number of the page to fetch */
4996450972
MemPage **ppPage, /* Return the page in this parameter */
49965
- int noContent /* Do not load page content if true */
50973
+ int noContent, /* Do not load page content if true */
50974
+ int bReadonly /* True if a read-only (mmap) page is ok */
4996650975
){
4996750976
int rc;
4996850977
DbPage *pDbPage;
50978
+ int flags = (noContent ? PAGER_ACQUIRE_NOCONTENT : 0)
50979
+ | (bReadonly ? PAGER_ACQUIRE_READONLY : 0);
4996950980
50981
+ assert( noContent==0 || bReadonly==0 );
4997050982
assert( sqlite3_mutex_held(pBt->mutex) );
49971
- rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
50983
+ rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
4997250984
if( rc ) return rc;
4997350985
*ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
4997450986
return SQLITE_OK;
4997550987
}
4997650988
@@ -50009,21 +51021,22 @@
5000951021
**
5001051022
** If an error occurs, then the value *ppPage is set to is undefined. It
5001151023
** may remain unchanged, or it may be set to an invalid value.
5001251024
*/
5001351025
static int getAndInitPage(
50014
- BtShared *pBt, /* The database file */
50015
- Pgno pgno, /* Number of the page to get */
50016
- MemPage **ppPage /* Write the page pointer here */
51026
+ BtShared *pBt, /* The database file */
51027
+ Pgno pgno, /* Number of the page to get */
51028
+ MemPage **ppPage, /* Write the page pointer here */
51029
+ int bReadonly /* True if a read-only (mmap) page is ok */
5001751030
){
5001851031
int rc;
5001951032
assert( sqlite3_mutex_held(pBt->mutex) );
5002051033
5002151034
if( pgno>btreePagecount(pBt) ){
5002251035
rc = SQLITE_CORRUPT_BKPT;
5002351036
}else{
50024
- rc = btreeGetPage(pBt, pgno, ppPage, 0);
51037
+ rc = btreeGetPage(pBt, pgno, ppPage, 0, bReadonly);
5002551038
if( rc==SQLITE_OK ){
5002651039
rc = btreeInitPage(*ppPage);
5002751040
if( rc!=SQLITE_OK ){
5002851041
releasePage(*ppPage);
5002951042
}
@@ -50250,10 +51263,11 @@
5025051263
goto btree_open_out;
5025151264
}
5025251265
rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
5025351266
EXTRA_SIZE, flags, vfsFlags, pageReinit);
5025451267
if( rc==SQLITE_OK ){
51268
+ sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
5025551269
rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
5025651270
}
5025751271
if( rc!=SQLITE_OK ){
5025851272
goto btree_open_out;
5025951273
}
@@ -50515,10 +51529,23 @@
5051551529
sqlite3BtreeEnter(p);
5051651530
sqlite3PagerSetCachesize(pBt->pPager, mxPage);
5051751531
sqlite3BtreeLeave(p);
5051851532
return SQLITE_OK;
5051951533
}
51534
+
51535
+/*
51536
+** Change the limit on the amount of the database file that may be
51537
+** memory mapped.
51538
+*/
51539
+SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
51540
+ BtShared *pBt = p->pBt;
51541
+ assert( sqlite3_mutex_held(p->db->mutex) );
51542
+ sqlite3BtreeEnter(p);
51543
+ sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
51544
+ sqlite3BtreeLeave(p);
51545
+ return SQLITE_OK;
51546
+}
5052051547
5052151548
/*
5052251549
** Change the way data is synced to disk in order to increase or decrease
5052351550
** how well the database resists damage due to OS crashes and power
5052451551
** failures. Level 1 is the same as asynchronous (no syncs() occur and
@@ -50741,11 +51768,11 @@
5074151768
5074251769
assert( sqlite3_mutex_held(pBt->mutex) );
5074351770
assert( pBt->pPage1==0 );
5074451771
rc = sqlite3PagerSharedLock(pBt->pPager);
5074551772
if( rc!=SQLITE_OK ) return rc;
50746
- rc = btreeGetPage(pBt, 1, &pPage1, 0);
51773
+ rc = btreeGetPage(pBt, 1, &pPage1, 0, 0);
5074751774
if( rc!=SQLITE_OK ) return rc;
5074851775
5074951776
/* Do some checking to help insure the file we opened really is
5075051777
** a valid database file.
5075151778
*/
@@ -51300,11 +52327,11 @@
5130052327
/* Fix the database pointer on page iPtrPage that pointed at iDbPage so
5130152328
** that it points at iFreePage. Also fix the pointer map entry for
5130252329
** iPtrPage.
5130352330
*/
5130452331
if( eType!=PTRMAP_ROOTPAGE ){
51305
- rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
52332
+ rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0, 0);
5130652333
if( rc!=SQLITE_OK ){
5130752334
return rc;
5130852335
}
5130952336
rc = sqlite3PagerWrite(pPtrPage->pDbPage);
5131052337
if( rc!=SQLITE_OK ){
@@ -51384,11 +52411,11 @@
5138452411
Pgno iFreePg; /* Index of free page to move pLastPg to */
5138552412
MemPage *pLastPg;
5138652413
u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
5138752414
Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
5138852415
51389
- rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
52416
+ rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0, 0);
5139052417
if( rc!=SQLITE_OK ){
5139152418
return rc;
5139252419
}
5139352420
5139452421
/* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -51476,12 +52503,15 @@
5147652503
Pgno nFin = finalDbSize(pBt, nOrig, nFree);
5147752504
5147852505
if( nOrig<nFin ){
5147952506
rc = SQLITE_CORRUPT_BKPT;
5148052507
}else if( nFree>0 ){
51481
- invalidateAllOverflowCache(pBt);
51482
- rc = incrVacuumStep(pBt, nFin, nOrig, 0);
52508
+ rc = saveAllCursors(pBt, 0, 0);
52509
+ if( rc==SQLITE_OK ){
52510
+ invalidateAllOverflowCache(pBt);
52511
+ rc = incrVacuumStep(pBt, nFin, nOrig, 0);
52512
+ }
5148352513
if( rc==SQLITE_OK ){
5148452514
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
5148552515
put4byte(&pBt->pPage1->aData[28], pBt->nPage);
5148652516
}
5148752517
}else{
@@ -51525,11 +52555,13 @@
5152552555
}
5152652556
5152752557
nFree = get4byte(&pBt->pPage1->aData[36]);
5152852558
nFin = finalDbSize(pBt, nOrig, nFree);
5152952559
if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
51530
-
52560
+ if( nFin<nOrig ){
52561
+ rc = saveAllCursors(pBt, 0, 0);
52562
+ }
5153152563
for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
5153252564
rc = incrVacuumStep(pBt, nFin, iFree, 1);
5153352565
}
5153452566
if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
5153552567
rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
@@ -51542,11 +52574,11 @@
5154252574
if( rc!=SQLITE_OK ){
5154352575
sqlite3PagerRollback(pPager);
5154452576
}
5154552577
}
5154652578
51547
- assert( nRef==sqlite3PagerRefcount(pPager) );
52579
+ assert( nRef>=sqlite3PagerRefcount(pPager) );
5154852580
return rc;
5154952581
}
5155052582
5155152583
#else /* ifndef SQLITE_OMIT_AUTOVACUUM */
5155252584
# define setChildPtrmaps(x) SQLITE_OK
@@ -51798,11 +52830,11 @@
5179852830
}
5179952831
5180052832
/* The rollback may have destroyed the pPage1->aData value. So
5180152833
** call btreeGetPage() on page 1 again to make
5180252834
** sure pPage1->aData is set correctly. */
51803
- if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
52835
+ if( btreeGetPage(pBt, 1, &pPage1, 0, 0)==SQLITE_OK ){
5180452836
int nPage = get4byte(28+(u8*)pPage1->aData);
5180552837
testcase( nPage==0 );
5180652838
if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
5180752839
testcase( pBt->nPage!=nPage );
5180852840
pBt->nPage = nPage;
@@ -52232,11 +53264,11 @@
5223253264
}
5223353265
#endif
5223453266
5223553267
assert( next==0 || rc==SQLITE_DONE );
5223653268
if( rc==SQLITE_OK ){
52237
- rc = btreeGetPage(pBt, ovfl, &pPage, 0);
53269
+ rc = btreeGetPage(pBt, ovfl, &pPage, 0, (ppPage==0));
5223853270
assert( rc==SQLITE_OK || pPage==0 );
5223953271
if( rc==SQLITE_OK ){
5224053272
next = get4byte(pPage->aData);
5224153273
}
5224253274
}
@@ -52453,11 +53485,13 @@
5245353485
}else
5245453486
#endif
5245553487
5245653488
{
5245753489
DbPage *pDbPage;
52458
- rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
53490
+ rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
53491
+ (eOp==0 ? PAGER_ACQUIRE_READONLY : 0)
53492
+ );
5245953493
if( rc==SQLITE_OK ){
5246053494
aPayload = sqlite3PagerGetData(pDbPage);
5246153495
nextPage = get4byte(aPayload);
5246253496
rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
5246353497
sqlite3PagerUnref(pDbPage);
@@ -52632,14 +53666,15 @@
5263253666
BtShared *pBt = pCur->pBt;
5263353667
5263453668
assert( cursorHoldsMutex(pCur) );
5263553669
assert( pCur->eState==CURSOR_VALID );
5263653670
assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
53671
+ assert( pCur->iPage>=0 );
5263753672
if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
5263853673
return SQLITE_CORRUPT_BKPT;
5263953674
}
52640
- rc = getAndInitPage(pBt, newPgno, &pNewPage);
53675
+ rc = getAndInitPage(pBt, newPgno, &pNewPage, (pCur->wrFlag==0));
5264153676
if( rc ) return rc;
5264253677
pCur->apPage[i+1] = pNewPage;
5264353678
pCur->aiIdx[i+1] = 0;
5264453679
pCur->iPage++;
5264553680
@@ -52752,11 +53787,11 @@
5275253787
pCur->iPage = 0;
5275353788
}else if( pCur->pgnoRoot==0 ){
5275453789
pCur->eState = CURSOR_INVALID;
5275553790
return SQLITE_OK;
5275653791
}else{
52757
- rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
53792
+ rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0], pCur->wrFlag==0);
5275853793
if( rc!=SQLITE_OK ){
5275953794
pCur->eState = CURSOR_INVALID;
5276053795
return rc;
5276153796
}
5276253797
pCur->iPage = 0;
@@ -53366,11 +54401,11 @@
5336654401
}
5336754402
testcase( iTrunk==mxPage );
5336854403
if( iTrunk>mxPage ){
5336954404
rc = SQLITE_CORRUPT_BKPT;
5337054405
}else{
53371
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
54406
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
5337254407
}
5337354408
if( rc ){
5337454409
pTrunk = 0;
5337554410
goto end_allocate_page;
5337654411
}
@@ -53430,11 +54465,11 @@
5343054465
if( iNewTrunk>mxPage ){
5343154466
rc = SQLITE_CORRUPT_BKPT;
5343254467
goto end_allocate_page;
5343354468
}
5343454469
testcase( iNewTrunk==mxPage );
53435
- rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
54470
+ rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0, 0);
5343654471
if( rc!=SQLITE_OK ){
5343754472
goto end_allocate_page;
5343854473
}
5343954474
rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
5344054475
if( rc!=SQLITE_OK ){
@@ -53510,11 +54545,11 @@
5351054545
if( closest<k-1 ){
5351154546
memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
5351254547
}
5351354548
put4byte(&aData[4], k-1);
5351454549
noContent = !btreeGetHasContent(pBt, *pPgno);
53515
- rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
54550
+ rc = btreeGetPage(pBt, *pPgno, ppPage, noContent, 0);
5351654551
if( rc==SQLITE_OK ){
5351754552
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5351854553
if( rc!=SQLITE_OK ){
5351954554
releasePage(*ppPage);
5352054555
}
@@ -53558,11 +54593,11 @@
5355854593
** becomes a new pointer-map page, the second is used by the caller.
5355954594
*/
5356054595
MemPage *pPg = 0;
5356154596
TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
5356254597
assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
53563
- rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
54598
+ rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent, 0);
5356454599
if( rc==SQLITE_OK ){
5356554600
rc = sqlite3PagerWrite(pPg->pDbPage);
5356654601
releasePage(pPg);
5356754602
}
5356854603
if( rc ) return rc;
@@ -53572,11 +54607,11 @@
5357254607
#endif
5357354608
put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
5357454609
*pPgno = pBt->nPage;
5357554610
5357654611
assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
53577
- rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
54612
+ rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent, 0);
5357854613
if( rc ) return rc;
5357954614
rc = sqlite3PagerWrite((*ppPage)->pDbPage);
5358054615
if( rc!=SQLITE_OK ){
5358154616
releasePage(*ppPage);
5358254617
}
@@ -53640,11 +54675,11 @@
5364054675
5364154676
if( pBt->btsFlags & BTS_SECURE_DELETE ){
5364254677
/* If the secure_delete option is enabled, then
5364354678
** always fully overwrite deleted information with zeros.
5364454679
*/
53645
- if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
54680
+ if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0, 0))!=0) )
5364654681
|| ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
5364754682
){
5364854683
goto freepage_out;
5364954684
}
5365054685
memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -53667,11 +54702,11 @@
5366754702
*/
5366854703
if( nFree!=0 ){
5366954704
u32 nLeaf; /* Initial number of leaf cells on trunk page */
5367054705
5367154706
iTrunk = get4byte(&pPage1->aData[32]);
53672
- rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
54707
+ rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
5367354708
if( rc!=SQLITE_OK ){
5367454709
goto freepage_out;
5367554710
}
5367654711
5367754712
nLeaf = get4byte(&pTrunk->aData[4]);
@@ -53713,11 +54748,11 @@
5371354748
** the page being freed as a leaf page of the first trunk in the free-list.
5371454749
** Possibly because the free-list is empty, or possibly because the
5371554750
** first trunk in the free-list is full. Either way, the page being freed
5371654751
** will become the new first trunk page in the free-list.
5371754752
*/
53718
- if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
54753
+ if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0, 0)) ){
5371954754
goto freepage_out;
5372054755
}
5372154756
rc = sqlite3PagerWrite(pPage->pDbPage);
5372254757
if( rc!=SQLITE_OK ){
5372354758
goto freepage_out;
@@ -54514,11 +55549,11 @@
5451455549
}else{
5451555550
pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
5451655551
}
5451755552
pgno = get4byte(pRight);
5451855553
while( 1 ){
54519
- rc = getAndInitPage(pBt, pgno, &apOld[i]);
55554
+ rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
5452055555
if( rc ){
5452155556
memset(apOld, 0, (i+1)*sizeof(MemPage*));
5452255557
goto balance_cleanup;
5452355558
}
5452455559
nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -55602,14 +56637,21 @@
5560256637
** is already journaled.
5560356638
*/
5560456639
u8 eType = 0;
5560556640
Pgno iPtrPage = 0;
5560656641
56642
+ /* Save the positions of any open cursors. This is required in
56643
+ ** case they are holding a reference to an xFetch reference
56644
+ ** corresponding to page pgnoRoot. */
56645
+ rc = saveAllCursors(pBt, 0, 0);
5560756646
releasePage(pPageMove);
56647
+ if( rc!=SQLITE_OK ){
56648
+ return rc;
56649
+ }
5560856650
5560956651
/* Move the page currently at pgnoRoot to pgnoMove. */
55610
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
56652
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
5561156653
if( rc!=SQLITE_OK ){
5561256654
return rc;
5561356655
}
5561456656
rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
5561556657
if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -55626,11 +56668,11 @@
5562656668
5562756669
/* Obtain the page at pgnoRoot */
5562856670
if( rc!=SQLITE_OK ){
5562956671
return rc;
5563056672
}
55631
- rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
56673
+ rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
5563256674
if( rc!=SQLITE_OK ){
5563356675
return rc;
5563456676
}
5563556677
rc = sqlite3PagerWrite(pRoot->pDbPage);
5563656678
if( rc!=SQLITE_OK ){
@@ -55702,11 +56744,11 @@
5570256744
assert( sqlite3_mutex_held(pBt->mutex) );
5570356745
if( pgno>btreePagecount(pBt) ){
5570456746
return SQLITE_CORRUPT_BKPT;
5570556747
}
5570656748
55707
- rc = getAndInitPage(pBt, pgno, &pPage);
56749
+ rc = getAndInitPage(pBt, pgno, &pPage, 0);
5570856750
if( rc ) return rc;
5570956751
for(i=0; i<pPage->nCell; i++){
5571056752
pCell = findCell(pPage, i);
5571156753
if( !pPage->leaf ){
5571256754
rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
@@ -55804,11 +56846,11 @@
5580456846
if( NEVER(pBt->pCursor) ){
5580556847
sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
5580656848
return SQLITE_LOCKED_SHAREDCACHE;
5580756849
}
5580856850
55809
- rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
56851
+ rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0, 0);
5581056852
if( rc ) return rc;
5581156853
rc = sqlite3BtreeClearTable(p, iTable, 0);
5581256854
if( rc ){
5581356855
releasePage(pPage);
5581456856
return rc;
@@ -55839,21 +56881,21 @@
5583956881
** number in the database. So move the page that does into the
5584056882
** gap left by the deleted root-page.
5584156883
*/
5584256884
MemPage *pMove;
5584356885
releasePage(pPage);
55844
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
56886
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
5584556887
if( rc!=SQLITE_OK ){
5584656888
return rc;
5584756889
}
5584856890
rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
5584956891
releasePage(pMove);
5585056892
if( rc!=SQLITE_OK ){
5585156893
return rc;
5585256894
}
5585356895
pMove = 0;
55854
- rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
56896
+ rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
5585556897
freePage(pMove, &rc);
5585656898
releasePage(pMove);
5585756899
if( rc!=SQLITE_OK ){
5585856900
return rc;
5585956901
}
@@ -56261,11 +57303,11 @@
5626157303
*/
5626257304
pBt = pCheck->pBt;
5626357305
usableSize = pBt->usableSize;
5626457306
if( iPage==0 ) return 0;
5626557307
if( checkRef(pCheck, iPage, zParentContext) ) return 0;
56266
- if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
57308
+ if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0, 0))!=0 ){
5626757309
checkAppendMsg(pCheck, zContext,
5626857310
"unable to get the page. error code=%d", rc);
5626957311
return 0;
5627057312
}
5627157313
@@ -56732,10 +57774,21 @@
5673257774
}
5673357775
assert( pCsr->eState!=CURSOR_REQUIRESEEK );
5673457776
if( pCsr->eState!=CURSOR_VALID ){
5673557777
return SQLITE_ABORT;
5673657778
}
57779
+
57780
+ /* Save the positions of all other cursors open on this table. This is
57781
+ ** required in case any of them are holding references to an xFetch
57782
+ ** version of the b-tree page modified by the accessPayload call below.
57783
+ **
57784
+ ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
57785
+ ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
57786
+ ** saveAllCursors can only return SQLITE_OK.
57787
+ */
57788
+ VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
57789
+ assert( rc==SQLITE_OK );
5673757790
5673857791
/* Check some assumptions:
5673957792
** (a) the cursor is open for writing,
5674057793
** (b) there is a read/write transaction open,
5674157794
** (c) the connection holds a write-lock on the table (if required),
@@ -57214,11 +58267,12 @@
5721458267
assert( nSrcPage>=0 );
5721558268
for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
5721658269
const Pgno iSrcPg = p->iNext; /* Source page number */
5721758270
if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
5721858271
DbPage *pSrcPg; /* Source page object */
57219
- rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
58272
+ rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
58273
+ PAGER_ACQUIRE_READONLY);
5722058274
if( rc==SQLITE_OK ){
5722158275
rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
5722258276
sqlite3PagerUnref(pSrcPg);
5722358277
}
5722458278
}
@@ -62437,18 +63491,10 @@
6243763491
rc = sqlite3VdbeTransferError(p);
6243863492
}
6243963493
return (rc&db->errMask);
6244063494
}
6244163495
62442
-/*
62443
-** The maximum number of times that a statement will try to reparse
62444
-** itself before giving up and returning SQLITE_SCHEMA.
62445
-*/
62446
-#ifndef SQLITE_MAX_SCHEMA_RETRY
62447
-# define SQLITE_MAX_SCHEMA_RETRY 5
62448
-#endif
62449
-
6245063496
/*
6245163497
** This is the top-level implementation of sqlite3_step(). Call
6245263498
** sqlite3Step() to do most of the work. If a schema error occurs,
6245363499
** call sqlite3Reprepare() and try again.
6245463500
*/
@@ -63347,10 +64393,15 @@
6334764393
** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
6334864394
** string contains a copy of zRawSql but with host parameters expanded to
6334964395
** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
6335064396
** then the returned string holds a copy of zRawSql with "-- " prepended
6335164397
** to each line of text.
64398
+**
64399
+** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
64400
+** then long strings and blobs are truncated to that many bytes. This
64401
+** can be used to prevent unreasonably large trace strings when dealing
64402
+** with large (multi-megabyte) strings and blobs.
6335264403
**
6335364404
** The calling function is responsible for making sure the memory returned
6335464405
** is eventually freed.
6335564406
**
6335664407
** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -63418,34 +64469,53 @@
6341864469
}else if( pVar->flags & MEM_Int ){
6341964470
sqlite3XPrintf(&out, "%lld", pVar->u.i);
6342064471
}else if( pVar->flags & MEM_Real ){
6342164472
sqlite3XPrintf(&out, "%!.15g", pVar->r);
6342264473
}else if( pVar->flags & MEM_Str ){
64474
+ int nOut; /* Number of bytes of the string text to include in output */
6342364475
#ifndef SQLITE_OMIT_UTF16
6342464476
u8 enc = ENC(db);
64477
+ Mem utf8;
6342564478
if( enc!=SQLITE_UTF8 ){
63426
- Mem utf8;
6342764479
memset(&utf8, 0, sizeof(utf8));
6342864480
utf8.db = db;
6342964481
sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
6343064482
sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
63431
- sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
63432
- sqlite3VdbeMemRelease(&utf8);
63433
- }else
64483
+ pVar = &utf8;
64484
+ }
6343464485
#endif
63435
- {
63436
- sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
64486
+ nOut = pVar->n;
64487
+#ifdef SQLITE_TRACE_SIZE_LIMIT
64488
+ if( n>SQLITE_TRACE_SIZE_LIMIT ){
64489
+ nOut = SQLITE_TRACE_SIZE_LIMIT;
64490
+ while( nOut<pVar->n && (pVar->z[n]&0xc0)==0x80 ){ n++; }
6343764491
}
64492
+#endif
64493
+ sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
64494
+#ifdef SQLITE_TRACE_SIZE_LIMIT
64495
+ if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n);
64496
+#endif
64497
+#ifndef SQLITE_OMIT_UTF16
64498
+ if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
64499
+#endif
6343864500
}else if( pVar->flags & MEM_Zero ){
6343964501
sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
6344064502
}else{
64503
+ int nOut; /* Number of bytes of the blob to include in output */
6344164504
assert( pVar->flags & MEM_Blob );
6344264505
sqlite3StrAccumAppend(&out, "x'", 2);
63443
- for(i=0; i<pVar->n; i++){
64506
+ nOut = pVar->n;
64507
+#ifdef SQLITE_TRACE_SIZE_LIMIT
64508
+ if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
64509
+#endif
64510
+ for(i=0; i<nOut; i++){
6344464511
sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
6344564512
}
6344664513
sqlite3StrAccumAppend(&out, "'", 1);
64514
+#ifdef SQLITE_TRACE_SIZE_LIMIT
64515
+ if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n);
64516
+#endif
6344764517
}
6344864518
}
6344964519
}
6345064520
return sqlite3StrAccumFinish(&out);
6345164521
}
@@ -67658,11 +68728,11 @@
6765868728
** u.bc.r.flags = UNPACKED_INCRKEY;
6765968729
** }else{
6766068730
** u.bc.r.flags = 0;
6766168731
** }
6766268732
*/
67663
- u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
68733
+ u.bc.r.flags = (u8)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
6766468734
assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
6766568735
assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
6766668736
assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
6766768737
assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
6766868738
@@ -70783,11 +71853,11 @@
7078371853
if( db->mallocFailed ){
7078471854
goto blob_open_out;
7078571855
}
7078671856
sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
7078771857
rc = blobSeekToRow(pBlob, iRow, &zErr);
70788
- } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
71858
+ } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
7078971859
7079071860
blob_open_out:
7079171861
if( rc==SQLITE_OK && db->mallocFailed==0 ){
7079271862
*ppBlob = (sqlite3_blob *)pBlob;
7079371863
}else{
@@ -72468,11 +73538,13 @@
7246873538
0, /* xSectorSize */
7246973539
0, /* xDeviceCharacteristics */
7247073540
0, /* xShmMap */
7247173541
0, /* xShmLock */
7247273542
0, /* xShmBarrier */
72473
- 0 /* xShmUnlock */
73543
+ 0, /* xShmUnmap */
73544
+ 0, /* xFetch */
73545
+ 0 /* xUnfetch */
7247473546
};
7247573547
7247673548
/*
7247773549
** Open a journal file.
7247873550
*/
@@ -72612,11 +73684,13 @@
7261273684
}
7261373685
7261473686
/*
7261573687
** Call sqlite3WalkExpr() for every expression in Select statement p.
7261673688
** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
72617
-** on the compound select chain, p->pPrior.
73689
+** on the compound select chain, p->pPrior. Invoke the xSelectCallback()
73690
+** either before or after the walk of expressions and FROM clause, depending
73691
+** on whether pWalker->bSelectDepthFirst is false or true, respectively.
7261873692
**
7261973693
** Return WRC_Continue under normal conditions. Return WRC_Abort if
7262073694
** there is an abort request.
7262173695
**
7262273696
** If the Walker does not have an xSelectCallback() then this routine
@@ -72626,17 +73700,26 @@
7262673700
int rc;
7262773701
if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
7262873702
rc = WRC_Continue;
7262973703
pWalker->walkerDepth++;
7263073704
while( p ){
72631
- rc = pWalker->xSelectCallback(pWalker, p);
72632
- if( rc ) break;
73705
+ if( !pWalker->bSelectDepthFirst ){
73706
+ rc = pWalker->xSelectCallback(pWalker, p);
73707
+ if( rc ) break;
73708
+ }
7263373709
if( sqlite3WalkSelectExpr(pWalker, p)
7263473710
|| sqlite3WalkSelectFrom(pWalker, p)
7263573711
){
7263673712
pWalker->walkerDepth--;
7263773713
return WRC_Abort;
73714
+ }
73715
+ if( pWalker->bSelectDepthFirst ){
73716
+ rc = pWalker->xSelectCallback(pWalker, p);
73717
+ /* Depth-first search is currently only used for
73718
+ ** selectAddSubqueryTypeInfo() and that routine always returns
73719
+ ** WRC_Continue (0). So the following branch is never taken. */
73720
+ if( NEVER(rc) ) break;
7263873721
}
7263973722
p = p->pPrior;
7264073723
}
7264173724
pWalker->walkerDepth--;
7264273725
return rc & WRC_Abort;
@@ -73031,11 +74114,14 @@
7303174114
** In cases like this, replace pExpr with a copy of the expression that
7303274115
** forms the result set entry ("a+b" in the example) and return immediately.
7303374116
** Note that the expression in the result set should have already been
7303474117
** resolved by the time the WHERE clause is resolved.
7303574118
*/
73036
- if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
74119
+ if( (pEList = pNC->pEList)!=0
74120
+ && zTab==0
74121
+ && ((pNC->ncFlags & NC_AsMaybe)==0 || cnt==0)
74122
+ ){
7303774123
for(j=0; j<pEList->nExpr; j++){
7303874124
char *zAs = pEList->a[j].zName;
7303974125
if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
7304074126
Expr *pOrig;
7304174127
assert( pExpr->pLeft==0 && pExpr->pRight==0 );
@@ -73122,11 +74208,13 @@
7312274208
pExpr->pRight = 0;
7312374209
pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
7312474210
lookupname_end:
7312574211
if( cnt==1 ){
7312674212
assert( pNC!=0 );
73127
- sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
74213
+ if( pExpr->op!=TK_AS ){
74214
+ sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
74215
+ }
7312874216
/* Increment the nRef value on all name contexts from TopNC up to
7312974217
** the point where the name matched. */
7313074218
for(;;){
7313174219
assert( pTopNC!=0 );
7313274220
pTopNC->nRef++;
@@ -73797,15 +74885,14 @@
7379774885
**
7379874886
** Minor point: If this is the case, then the expression will be
7379974887
** re-evaluated for each reference to it.
7380074888
*/
7380174889
sNC.pEList = p->pEList;
73802
- if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
73803
- sqlite3ResolveExprNames(&sNC, p->pHaving)
73804
- ){
73805
- return WRC_Abort;
73806
- }
74890
+ if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
74891
+ sNC.ncFlags |= NC_AsMaybe;
74892
+ if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
74893
+ sNC.ncFlags &= ~NC_AsMaybe;
7380774894
7380874895
/* The ORDER BY and GROUP BY clauses may not refer to terms in
7380974896
** outer queries
7381074897
*/
7381174898
sNC.pNext = 0;
@@ -73922,10 +75009,11 @@
7392275009
pParse->nHeight += pExpr->nHeight;
7392375010
}
7392475011
#endif
7392575012
savedHasAgg = pNC->ncFlags & NC_HasAgg;
7392675013
pNC->ncFlags &= ~NC_HasAgg;
75014
+ memset(&w, 0, sizeof(w));
7392775015
w.xExprCallback = resolveExprStep;
7392875016
w.xSelectCallback = resolveSelectStep;
7392975017
w.pParse = pNC->pParse;
7393075018
w.u.pNC = pNC;
7393175019
sqlite3WalkExpr(&w, pExpr);
@@ -73962,10 +75050,11 @@
7396275050
NameContext *pOuterNC /* Name context for parent SELECT statement */
7396375051
){
7396475052
Walker w;
7396575053
7396675054
assert( p!=0 );
75055
+ memset(&w, 0, sizeof(w));
7396775056
w.xExprCallback = resolveExprStep;
7396875057
w.xSelectCallback = resolveSelectStep;
7396975058
w.pParse = pParse;
7397075059
w.u.pNC = pOuterNC;
7397175060
sqlite3WalkSelect(&w, p);
@@ -75186,10 +76275,11 @@
7518676275
pWalker->u.i = 0;
7518776276
return WRC_Abort;
7518876277
}
7518976278
static int exprIsConst(Expr *p, int initFlag){
7519076279
Walker w;
76280
+ memset(&w, 0, sizeof(w));
7519176281
w.u.i = initFlag;
7519276282
w.xExprCallback = exprNodeIsConstant;
7519376283
w.xSelectCallback = selectNodeIsConstant;
7519476284
sqlite3WalkExpr(&w, p);
7519576285
return w.u.i;
@@ -77400,12 +78490,12 @@
7740078490
*/
7740178491
SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
7740278492
Walker w;
7740378493
if( pParse->cookieGoto ) return;
7740478494
if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
78495
+ memset(&w, 0, sizeof(w));
7740578496
w.xExprCallback = evalConstExpr;
77406
- w.xSelectCallback = 0;
7740778497
w.pParse = pParse;
7740878498
sqlite3WalkExpr(&w, pExpr);
7740978499
}
7741078500
7741178501
@@ -86603,10 +87693,17 @@
8660387693
prevEscape = 0;
8660487694
}
8660587695
}
8660687696
return *zString==0;
8660787697
}
87698
+
87699
+/*
87700
+** The sqlite3_strglob() interface.
87701
+*/
87702
+SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
87703
+ return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
87704
+}
8660887705
8660987706
/*
8661087707
** Count the number of times that the LIKE operator (or GLOB which is
8661187708
** just a variation of LIKE) gets called. This is used for testing
8661287709
** only.
@@ -90804,24 +91901,23 @@
9080491901
){
9080591902
int rc = SQLITE_OK; /* Return code */
9080691903
const char *zLeftover; /* Tail of unprocessed SQL */
9080791904
sqlite3_stmt *pStmt = 0; /* The current SQL statement */
9080891905
char **azCols = 0; /* Names of result columns */
90809
- int nRetry = 0; /* Number of retry attempts */
9081091906
int callbackIsInit; /* True if callback data is initialized */
9081191907
9081291908
if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
9081391909
if( zSql==0 ) zSql = "";
9081491910
9081591911
sqlite3_mutex_enter(db->mutex);
9081691912
sqlite3Error(db, SQLITE_OK, 0);
90817
- while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
91913
+ while( rc==SQLITE_OK && zSql[0] ){
9081891914
int nCol;
9081991915
char **azVals = 0;
9082091916
9082191917
pStmt = 0;
90822
- rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
91918
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
9082391919
assert( rc==SQLITE_OK || pStmt==0 );
9082491920
if( rc!=SQLITE_OK ){
9082591921
continue;
9082691922
}
9082791923
if( !pStmt ){
@@ -90874,15 +91970,12 @@
9087491970
}
9087591971
9087691972
if( rc!=SQLITE_ROW ){
9087791973
rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
9087891974
pStmt = 0;
90879
- if( rc!=SQLITE_SCHEMA ){
90880
- nRetry = 0;
90881
- zSql = zLeftover;
90882
- while( sqlite3Isspace(zSql[0]) ) zSql++;
90883
- }
91975
+ zSql = zLeftover;
91976
+ while( sqlite3Isspace(zSql[0]) ) zSql++;
9088491977
break;
9088591978
}
9088691979
}
9088791980
9088891981
sqlite3DbFree(db, azCols);
@@ -91402,12 +92495,21 @@
9140292495
#define sqlite3_uri_parameter sqlite3_api->uri_parameter
9140392496
#define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
9140492497
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
9140592498
#endif /* SQLITE_CORE */
9140692499
91407
-#define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
91408
-#define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
92500
+#ifndef SQLITE_CORE
92501
+ /* This case when the file really is being compiled as a loadable
92502
+ ** extension */
92503
+# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
92504
+# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
92505
+#else
92506
+ /* This case when the file is being statically linked into the
92507
+ ** application */
92508
+# define SQLITE_EXTENSION_INIT1 /*no-op*/
92509
+# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
92510
+#endif
9140992511
9141092512
#endif /* _SQLITE3EXT_H_ */
9141192513
9141292514
/************** End of sqlite3ext.h ******************************************/
9141392515
/************** Continuing where we left off in loadext.c ********************/
@@ -91806,12 +92908,27 @@
9180692908
){
9180792909
sqlite3_vfs *pVfs = db->pVfs;
9180892910
void *handle;
9180992911
int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
9181092912
char *zErrmsg = 0;
92913
+ const char *zEntry;
92914
+ char *zAltEntry = 0;
9181192915
void **aHandle;
9181292916
int nMsg = 300 + sqlite3Strlen30(zFile);
92917
+ int ii;
92918
+
92919
+ /* Shared library endings to try if zFile cannot be loaded as written */
92920
+ static const char *azEndings[] = {
92921
+#if SQLITE_OS_WIN
92922
+ "dll"
92923
+#elif defined(__APPLE__)
92924
+ "dylib"
92925
+#else
92926
+ "so"
92927
+#endif
92928
+ };
92929
+
9181392930
9181492931
if( pzErrMsg ) *pzErrMsg = 0;
9181592932
9181692933
/* Ticket #1863. To avoid a creating security problems for older
9181792934
** applications that relink against newer versions of SQLite, the
@@ -91824,15 +92941,21 @@
9182492941
*pzErrMsg = sqlite3_mprintf("not authorized");
9182592942
}
9182692943
return SQLITE_ERROR;
9182792944
}
9182892945
91829
- if( zProc==0 ){
91830
- zProc = "sqlite3_extension_init";
91831
- }
92946
+ zEntry = zProc ? zProc : "sqlite3_extension_init";
9183292947
9183392948
handle = sqlite3OsDlOpen(pVfs, zFile);
92949
+#if SQLITE_OS_UNIX || SQLITE_OS_WIN
92950
+ for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
92951
+ char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
92952
+ if( zAltFile==0 ) return SQLITE_NOMEM;
92953
+ handle = sqlite3OsDlOpen(pVfs, zAltFile);
92954
+ sqlite3_free(zAltFile);
92955
+ }
92956
+#endif
9183492957
if( handle==0 ){
9183592958
if( pzErrMsg ){
9183692959
*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
9183792960
if( zErrmsg ){
9183892961
sqlite3_snprintf(nMsg, zErrmsg,
@@ -91841,24 +92964,61 @@
9184192964
}
9184292965
}
9184392966
return SQLITE_ERROR;
9184492967
}
9184592968
xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
91846
- sqlite3OsDlSym(pVfs, handle, zProc);
92969
+ sqlite3OsDlSym(pVfs, handle, zEntry);
92970
+
92971
+ /* If no entry point was specified and the default legacy
92972
+ ** entry point name "sqlite3_extension_init" was not found, then
92973
+ ** construct an entry point name "sqlite3_X_init" where the X is
92974
+ ** replaced by the lowercase value of every ASCII alphabetic
92975
+ ** character in the filename after the last "/" upto the first ".",
92976
+ ** and eliding the first three characters if they are "lib".
92977
+ ** Examples:
92978
+ **
92979
+ ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
92980
+ ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
92981
+ */
92982
+ if( xInit==0 && zProc==0 ){
92983
+ int iFile, iEntry, c;
92984
+ int ncFile = sqlite3Strlen30(zFile);
92985
+ zAltEntry = sqlite3_malloc(ncFile+30);
92986
+ if( zAltEntry==0 ){
92987
+ sqlite3OsDlClose(pVfs, handle);
92988
+ return SQLITE_NOMEM;
92989
+ }
92990
+ memcpy(zAltEntry, "sqlite3_", 8);
92991
+ for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
92992
+ iFile++;
92993
+ if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
92994
+ for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
92995
+ if( sqlite3Isalpha(c) ){
92996
+ zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
92997
+ }
92998
+ }
92999
+ memcpy(zAltEntry+iEntry, "_init", 6);
93000
+ zEntry = zAltEntry;
93001
+ xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
93002
+ sqlite3OsDlSym(pVfs, handle, zEntry);
93003
+ }
9184793004
if( xInit==0 ){
9184893005
if( pzErrMsg ){
91849
- nMsg += sqlite3Strlen30(zProc);
93006
+ nMsg += sqlite3Strlen30(zEntry);
9185093007
*pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
9185193008
if( zErrmsg ){
9185293009
sqlite3_snprintf(nMsg, zErrmsg,
91853
- "no entry point [%s] in shared library [%s]", zProc,zFile);
93010
+ "no entry point [%s] in shared library [%s]", zEntry, zFile);
9185493011
sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
9185593012
}
91856
- sqlite3OsDlClose(pVfs, handle);
9185793013
}
93014
+ sqlite3OsDlClose(pVfs, handle);
93015
+ sqlite3_free(zAltEntry);
9185893016
return SQLITE_ERROR;
91859
- }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
93017
+ }
93018
+ sqlite3_free(zAltEntry);
93019
+ if( xInit(db, &zErrmsg, &sqlite3Apis) ){
9186093020
if( pzErrMsg ){
9186193021
*pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
9186293022
}
9186393023
sqlite3_free(zErrmsg);
9186493024
sqlite3OsDlClose(pVfs, handle);
@@ -92383,11 +93543,11 @@
9238393543
int iDb; /* Database index for <database> */
9238493544
char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
9238593545
int rc; /* return value form SQLITE_FCNTL_PRAGMA */
9238693546
sqlite3 *db = pParse->db; /* The database connection */
9238793547
Db *pDb; /* The specific database being pragmaed */
92388
- Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); /* Prepared statement */
93548
+ Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
9238993549
9239093550
if( v==0 ) return;
9239193551
sqlite3VdbeRunOnlyOnce(v);
9239293552
pParse->nMem = 2;
9239393553
@@ -92466,15 +93626,16 @@
9246693626
*/
9246793627
if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
9246893628
static const VdbeOpList getCacheSize[] = {
9246993629
{ OP_Transaction, 0, 0, 0}, /* 0 */
9247093630
{ OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
92471
- { OP_IfPos, 1, 7, 0},
93631
+ { OP_IfPos, 1, 8, 0},
9247293632
{ OP_Integer, 0, 2, 0},
9247393633
{ OP_Subtract, 1, 2, 1},
92474
- { OP_IfPos, 1, 7, 0},
93634
+ { OP_IfPos, 1, 8, 0},
9247593635
{ OP_Integer, 0, 1, 0}, /* 6 */
93636
+ { OP_Noop, 0, 0, 0},
9247693637
{ OP_ResultRow, 1, 1, 0},
9247793638
};
9247893639
int addr;
9247993640
if( sqlite3ReadSchema(pParse) ) goto pragma_out;
9248093641
sqlite3VdbeUsesBtree(v, iDb);
@@ -92808,10 +93969,47 @@
9280893969
pDb->pSchema->cache_size = size;
9280993970
sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
9281093971
}
9281193972
}else
9281293973
93974
+ /*
93975
+ ** PRAGMA [database.]mmap_size(N)
93976
+ **
93977
+ ** Used to set mapping size limit. The mapping size limit is
93978
+ ** used to limit the aggregate size of all memory mapped regions of the
93979
+ ** database file. If this parameter is set to zero, then memory mapping
93980
+ ** is not used at all. If N is negative, then the default memory map
93981
+ ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
93982
+ ** The parameter N is measured in bytes.
93983
+ **
93984
+ ** This value is advisory. The underlying VFS is free to memory map
93985
+ ** as little or as much as it wants. Except, if N is set to 0 then the
93986
+ ** upper layers will never invoke the xFetch interfaces to the VFS.
93987
+ */
93988
+ if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
93989
+ sqlite3_int64 sz;
93990
+ assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
93991
+ if( zRight ){
93992
+ int ii;
93993
+ sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
93994
+ if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
93995
+ if( pId2->n==0 ) db->szMmap = sz;
93996
+ for(ii=db->nDb-1; ii>=0; ii--){
93997
+ if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
93998
+ sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
93999
+ }
94000
+ }
94001
+ }
94002
+ sz = -1;
94003
+ if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){
94004
+#if SQLITE_MAX_MMAP_SIZE==0
94005
+ sz = 0;
94006
+#endif
94007
+ returnSingleInt(pParse, "mmap_size", sz);
94008
+ }
94009
+ }else
94010
+
9281394011
/*
9281494012
** PRAGMA temp_store
9281594013
** PRAGMA temp_store = "default"|"memory"|"file"
9281694014
**
9281794015
** Return or set the local value of the temp_store flag. Changing
@@ -94498,11 +95696,10 @@
9449895696
azColName[i], SQLITE_STATIC);
9449995697
}
9450095698
}
9450195699
#endif
9450295700
94503
- assert( db->init.busy==0 || saveSqlFlag==0 );
9450495701
if( db->init.busy==0 ){
9450595702
Vdbe *pVdbe = pParse->pVdbe;
9450695703
sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
9450795704
}
9450895705
if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
@@ -98290,10 +99487,11 @@
9829099487
** The calling function can detect the problem by looking at pParse->nErr
9829199488
** and/or pParse->db->mallocFailed.
9829299489
*/
9829399490
static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
9829499491
Walker w;
99492
+ memset(&w, 0, sizeof(w));
9829599493
w.xSelectCallback = selectExpander;
9829699494
w.xExprCallback = exprWalkNoop;
9829799495
w.pParse = pParse;
9829899496
sqlite3WalkSelect(&w, pSelect);
9829999497
}
@@ -98348,13 +99546,15 @@
9834899546
** Use this routine after name resolution.
9834999547
*/
9835099548
static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
9835199549
#ifndef SQLITE_OMIT_SUBQUERY
9835299550
Walker w;
99551
+ memset(&w, 0, sizeof(w));
9835399552
w.xSelectCallback = selectAddSubqueryTypeInfo;
9835499553
w.xExprCallback = exprWalkNoop;
9835599554
w.pParse = pParse;
99555
+ w.bSelectDepthFirst = 1;
9835699556
sqlite3WalkSelect(&w, pSelect);
9835799557
#endif
9835899558
}
9835999559
9836099560
@@ -98761,11 +99961,11 @@
9876199961
pItem->regReturn = ++pParse->nMem;
9876299962
topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
9876399963
pItem->addrFillSub = topAddr+1;
9876499964
VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
9876599965
if( pItem->isCorrelated==0 ){
98766
- /* If the subquery is no correlated and if we are not inside of
99966
+ /* If the subquery is not correlated and if we are not inside of
9876799967
** a trigger, then we only need to compute the value of the subquery
9876899968
** once. */
9876999969
onceAddr = sqlite3CodeOnce(pParse);
9877099970
}
9877199971
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
@@ -105219,13 +106419,12 @@
105219106419
Table *pTab = pSrc->pTab;
105220106420
sqlite3_index_info *pIdxInfo;
105221106421
struct sqlite3_index_constraint *pIdxCons;
105222106422
struct sqlite3_index_constraint_usage *pUsage;
105223106423
WhereTerm *pTerm;
105224
- int i, j, k;
106424
+ int i, j;
105225106425
int nOrderBy;
105226
- int sortOrder; /* Sort order for IN clauses */
105227106426
int bAllowIN; /* Allow IN optimizations */
105228106427
double rCost;
105229106428
105230106429
/* Make sure wsFlags is initialized to some sane value. Otherwise, if the
105231106430
** malloc in allocateIndexInfo() fails and this function returns leaving
@@ -105320,11 +106519,10 @@
105320106519
105321106520
if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
105322106521
return;
105323106522
}
105324106523
105325
- sortOrder = SQLITE_SO_ASC;
105326106524
pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
105327106525
for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
105328106526
if( pUsage[i].argvIndex>0 ){
105329106527
j = pIdxCons->iTermOffset;
105330106528
pTerm = &pWC->a[j];
@@ -105335,21 +106533,32 @@
105335106533
** says that the equivalent EQ constraint cannot be safely omitted.
105336106534
** If we do attempt to use such a constraint, some rows might be
105337106535
** repeated in the output. */
105338106536
break;
105339106537
}
105340
- for(k=0; k<pIdxInfo->nOrderBy; k++){
105341
- if( pIdxInfo->aOrderBy[k].iColumn==pIdxCons->iColumn ){
105342
- sortOrder = pIdxInfo->aOrderBy[k].desc;
105343
- break;
105344
- }
105345
- }
106538
+ /* A virtual table that is constrained by an IN clause may not
106539
+ ** consume the ORDER BY clause because (1) the order of IN terms
106540
+ ** is not necessarily related to the order of output terms and
106541
+ ** (2) Multiple outputs from a single IN value will not merge
106542
+ ** together. */
106543
+ pIdxInfo->orderByConsumed = 0;
105346106544
}
105347106545
}
105348106546
}
105349106547
if( i>=pIdxInfo->nConstraint ) break;
105350106548
}
106549
+
106550
+ /* The orderByConsumed signal is only valid if all outer loops collectively
106551
+ ** generate just a single row of output.
106552
+ */
106553
+ if( pIdxInfo->orderByConsumed ){
106554
+ for(i=0; i<p->i; i++){
106555
+ if( (p->aLevel[i].plan.wsFlags & WHERE_UNIQUE)==0 ){
106556
+ pIdxInfo->orderByConsumed = 0;
106557
+ }
106558
+ }
106559
+ }
105351106560
105352106561
/* If there is an ORDER BY clause, and the selected virtual table index
105353106562
** does not satisfy it, increase the cost of the scan accordingly. This
105354106563
** matches the processing for non-virtual tables in bestBtreeIndex().
105355106564
*/
@@ -105370,12 +106579,11 @@
105370106579
}else{
105371106580
p->cost.rCost = rCost;
105372106581
}
105373106582
p->cost.plan.u.pVtabIdx = pIdxInfo;
105374106583
if( pIdxInfo->orderByConsumed ){
105375
- assert( sortOrder==0 || sortOrder==1 );
105376
- p->cost.plan.wsFlags |= WHERE_ORDERED + sortOrder*WHERE_REVERSE;
106584
+ p->cost.plan.wsFlags |= WHERE_ORDERED;
105377106585
p->cost.plan.nOBSat = nOrderBy;
105378106586
}else{
105379106587
p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
105380106588
}
105381106589
p->cost.plan.nEq = 0;
@@ -107108,10 +108316,11 @@
107108108316
struct SrcList_item *pTabItem; /* FROM clause term being coded */
107109108317
int addrBrk; /* Jump here to break out of the loop */
107110108318
int addrCont; /* Jump here to continue with next cycle */
107111108319
int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
107112108320
int iReleaseReg = 0; /* Temp register to free before returning */
108321
+ Bitmask newNotReady; /* Return value */
107113108322
107114108323
pParse = pWInfo->pParse;
107115108324
v = pParse->pVdbe;
107116108325
pWC = pWInfo->pWC;
107117108326
pLevel = &pWInfo->a[iLevel];
@@ -107118,10 +108327,11 @@
107118108327
pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
107119108328
iCur = pTabItem->iCursor;
107120108329
bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
107121108330
omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
107122108331
&& (wctrlFlags & WHERE_FORCE_TABLE)==0;
108332
+ VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
107123108333
107124108334
/* Create labels for the "break" and "continue" instructions
107125108335
** for the current loop. Jump to addrBrk to break out of a loop.
107126108336
** Jump to cont to go immediately to the next iteration of the
107127108337
** loop.
@@ -107768,11 +108978,11 @@
107768108978
pLevel->op = aStep[bRev];
107769108979
pLevel->p1 = iCur;
107770108980
pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
107771108981
pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
107772108982
}
107773
- notReady &= ~getMask(pWC->pMaskSet, iCur);
108983
+ newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
107774108984
107775108985
/* Insert code to test every subexpression that can be completely
107776108986
** computed using the current set of tables.
107777108987
**
107778108988
** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
@@ -107782,11 +108992,11 @@
107782108992
for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
107783108993
Expr *pE;
107784108994
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
107785108995
testcase( pTerm->wtFlags & TERM_CODED );
107786108996
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
107787
- if( (pTerm->prereqAll & notReady)!=0 ){
108997
+ if( (pTerm->prereqAll & newNotReady)!=0 ){
107788108998
testcase( pWInfo->untestedTerms==0
107789108999
&& (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
107790109000
pWInfo->untestedTerms = 1;
107791109001
continue;
107792109002
}
@@ -107796,10 +109006,36 @@
107796109006
continue;
107797109007
}
107798109008
sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
107799109009
pTerm->wtFlags |= TERM_CODED;
107800109010
}
109011
+
109012
+ /* Insert code to test for implied constraints based on transitivity
109013
+ ** of the "==" operator.
109014
+ **
109015
+ ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
109016
+ ** and we are coding the t1 loop and the t2 loop has not yet coded,
109017
+ ** then we cannot use the "t1.a=t2.b" constraint, but we can code
109018
+ ** the implied "t1.a=123" constraint.
109019
+ */
109020
+ for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
109021
+ Expr *pE;
109022
+ WhereTerm *pAlt;
109023
+ Expr sEq;
109024
+ if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
109025
+ if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
109026
+ if( pTerm->leftCursor!=iCur ) continue;
109027
+ pE = pTerm->pExpr;
109028
+ assert( !ExprHasProperty(pE, EP_FromJoin) );
109029
+ assert( (pTerm->prereqRight & newNotReady)!=0 );
109030
+ pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
109031
+ if( pAlt==0 ) continue;
109032
+ VdbeNoopComment((v, "begin transitive constraint"));
109033
+ sEq = *pAlt->pExpr;
109034
+ sEq.pLeft = pE->pLeft;
109035
+ sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL);
109036
+ }
107801109037
107802109038
/* For a LEFT OUTER JOIN, generate code that will record the fact that
107803109039
** at least one row of the right table has matched the left table.
107804109040
*/
107805109041
if( pLevel->iLeftJoin ){
@@ -107809,11 +109045,11 @@
107809109045
sqlite3ExprCacheClear(pParse);
107810109046
for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
107811109047
testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
107812109048
testcase( pTerm->wtFlags & TERM_CODED );
107813109049
if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
107814
- if( (pTerm->prereqAll & notReady)!=0 ){
109050
+ if( (pTerm->prereqAll & newNotReady)!=0 ){
107815109051
assert( pWInfo->untestedTerms );
107816109052
continue;
107817109053
}
107818109054
assert( pTerm->pExpr );
107819109055
sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
@@ -107820,11 +109056,11 @@
107820109056
pTerm->wtFlags |= TERM_CODED;
107821109057
}
107822109058
}
107823109059
sqlite3ReleaseTempReg(pParse, iReleaseReg);
107824109060
107825
- return notReady;
109061
+ return newNotReady;
107826109062
}
107827109063
107828109064
#if defined(SQLITE_TEST)
107829109065
/*
107830109066
** The following variable holds a text description of query plan generated
@@ -113805,10 +115041,23 @@
113805115041
sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
113806115042
sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
113807115043
break;
113808115044
}
113809115045
#endif
115046
+
115047
+ case SQLITE_CONFIG_MMAP_SIZE: {
115048
+ sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
115049
+ sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
115050
+ if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
115051
+ mxMmap = SQLITE_MAX_MMAP_SIZE;
115052
+ }
115053
+ sqlite3GlobalConfig.mxMmap = mxMmap;
115054
+ if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
115055
+ if( szMmap>mxMmap) szMmap = mxMmap;
115056
+ sqlite3GlobalConfig.szMmap = szMmap;
115057
+ break;
115058
+ }
113810115059
113811115060
default: {
113812115061
rc = SQLITE_ERROR;
113813115062
break;
113814115063
}
@@ -115626,10 +116875,11 @@
115626116875
115627116876
assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
115628116877
memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
115629116878
db->autoCommit = 1;
115630116879
db->nextAutovac = -1;
116880
+ db->szMmap = sqlite3GlobalConfig.szMmap;
115631116881
db->nextPagesize = 0;
115632116882
db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
115633116883
#if SQLITE_DEFAULT_FILE_FORMAT<4
115634116884
| SQLITE_LegacyFileFmt
115635116885
#endif
@@ -117967,10 +119217,13 @@
117967119217
Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
117968119218
SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
117969119219
SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
117970119220
SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
117971119221
119222
+/* fts3_tokenize_vtab.c */
119223
+SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*);
119224
+
117972119225
/* fts3_unicode2.c (functions generated by parsing unicode text files) */
117973119226
#ifdef SQLITE_ENABLE_FTS4_UNICODE61
117974119227
SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
117975119228
SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
117976119229
SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
@@ -121281,10 +122534,13 @@
121281122534
#endif
121282122535
121283122536
rc = sqlite3Fts3InitAux(db);
121284122537
if( rc!=SQLITE_OK ) return rc;
121285122538
122539
+ rc = sqlite3Fts3InitTok(db);
122540
+ if( rc!=SQLITE_OK ) return rc;
122541
+
121286122542
sqlite3Fts3SimpleTokenizerModule(&pSimple);
121287122543
sqlite3Fts3PorterTokenizerModule(&pPorter);
121288122544
121289122545
/* Allocate and initialize the hash-table used to store tokenizers. */
121290122546
pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -123110,21 +124366,30 @@
123110124366
int rc; /* value returned by declare_vtab() */
123111124367
Fts3auxTable *p; /* Virtual table object to return */
123112124368
123113124369
UNUSED_PARAMETER(pUnused);
123114124370
123115
- /* The user should specify a single argument - the name of an fts3 table. */
123116
- if( argc!=4 ){
123117
- *pzErr = sqlite3_mprintf(
123118
- "wrong number of arguments to fts4aux constructor"
123119
- );
123120
- return SQLITE_ERROR;
123121
- }
124371
+ /* The user should invoke this in one of two forms:
124372
+ **
124373
+ ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
124374
+ ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
124375
+ */
124376
+ if( argc!=4 && argc!=5 ) goto bad_args;
123122124377
123123124378
zDb = argv[1];
123124124379
nDb = (int)strlen(zDb);
123125
- zFts3 = argv[3];
124380
+ if( argc==5 ){
124381
+ if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
124382
+ zDb = argv[3];
124383
+ nDb = (int)strlen(zDb);
124384
+ zFts3 = argv[4];
124385
+ }else{
124386
+ goto bad_args;
124387
+ }
124388
+ }else{
124389
+ zFts3 = argv[3];
124390
+ }
123126124391
nFts3 = (int)strlen(zFts3);
123127124392
123128124393
rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
123129124394
if( rc!=SQLITE_OK ) return rc;
123130124395
@@ -123143,10 +124408,14 @@
123143124408
memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
123144124409
sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
123145124410
123146124411
*ppVtab = (sqlite3_vtab *)p;
123147124412
return SQLITE_OK;
124413
+
124414
+ bad_args:
124415
+ *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
124416
+ return SQLITE_ERROR;
123148124417
}
123149124418
123150124419
/*
123151124420
** This function does the work for both the xDisconnect and xDestroy methods.
123152124421
** These tables have no persistent representation of their own, so xDisconnect
@@ -126285,10 +127554,476 @@
126285127554
}
126286127555
126287127556
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
126288127557
126289127558
/************** End of fts3_tokenizer1.c *************************************/
127559
+/************** Begin file fts3_tokenize_vtab.c ******************************/
127560
+/*
127561
+** 2013 Apr 22
127562
+**
127563
+** The author disclaims copyright to this source code. In place of
127564
+** a legal notice, here is a blessing:
127565
+**
127566
+** May you do good and not evil.
127567
+** May you find forgiveness for yourself and forgive others.
127568
+** May you share freely, never taking more than you give.
127569
+**
127570
+******************************************************************************
127571
+**
127572
+** This file contains code for the "fts3tokenize" virtual table module.
127573
+** An fts3tokenize virtual table is created as follows:
127574
+**
127575
+** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
127576
+** <tokenizer-name>, <arg-1>, ...
127577
+** );
127578
+**
127579
+** The table created has the following schema:
127580
+**
127581
+** CREATE TABLE <tbl>(input, token, start, end, position)
127582
+**
127583
+** When queried, the query must include a WHERE clause of type:
127584
+**
127585
+** input = <string>
127586
+**
127587
+** The virtual table module tokenizes this <string>, using the FTS3
127588
+** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
127589
+** statement and returns one row for each token in the result. With
127590
+** fields set as follows:
127591
+**
127592
+** input: Always set to a copy of <string>
127593
+** token: A token from the input.
127594
+** start: Byte offset of the token within the input <string>.
127595
+** end: Byte offset of the byte immediately following the end of the
127596
+** token within the input string.
127597
+** pos: Token offset of token within input.
127598
+**
127599
+*/
127600
+#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
127601
+
127602
+/* #include <string.h> */
127603
+/* #include <assert.h> */
127604
+
127605
+typedef struct Fts3tokTable Fts3tokTable;
127606
+typedef struct Fts3tokCursor Fts3tokCursor;
127607
+
127608
+/*
127609
+** Virtual table structure.
127610
+*/
127611
+struct Fts3tokTable {
127612
+ sqlite3_vtab base; /* Base class used by SQLite core */
127613
+ const sqlite3_tokenizer_module *pMod;
127614
+ sqlite3_tokenizer *pTok;
127615
+};
127616
+
127617
+/*
127618
+** Virtual table cursor structure.
127619
+*/
127620
+struct Fts3tokCursor {
127621
+ sqlite3_vtab_cursor base; /* Base class used by SQLite core */
127622
+ char *zInput; /* Input string */
127623
+ sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
127624
+ int iRowid; /* Current 'rowid' value */
127625
+ const char *zToken; /* Current 'token' value */
127626
+ int nToken; /* Size of zToken in bytes */
127627
+ int iStart; /* Current 'start' value */
127628
+ int iEnd; /* Current 'end' value */
127629
+ int iPos; /* Current 'pos' value */
127630
+};
127631
+
127632
+/*
127633
+** Query FTS for the tokenizer implementation named zName.
127634
+*/
127635
+static int fts3tokQueryTokenizer(
127636
+ sqlite3 *db,
127637
+ const char *zName,
127638
+ const sqlite3_tokenizer_module **pp
127639
+){
127640
+ int rc;
127641
+ sqlite3_stmt *pStmt;
127642
+ const char *zSql = "SELECT fts3_tokenizer(?)";
127643
+
127644
+ *pp = 0;
127645
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127646
+ if( rc!=SQLITE_OK ){
127647
+ return rc;
127648
+ }
127649
+
127650
+ sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
127651
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
127652
+ if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
127653
+ memcpy((void*)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
127654
+ }
127655
+ }
127656
+
127657
+ return sqlite3_finalize(pStmt);
127658
+}
127659
+
127660
+/*
127661
+** The second argument, argv[], is an array of pointers to nul-terminated
127662
+** strings. This function makes a copy of the array and strings into a
127663
+** single block of memory. It then dequotes any of the strings that appear
127664
+** to be quoted.
127665
+**
127666
+** If successful, output parameter *pazDequote is set to point at the
127667
+** array of dequoted strings and SQLITE_OK is returned. The caller is
127668
+** responsible for eventually calling sqlite3_free() to free the array
127669
+** in this case. Or, if an error occurs, an SQLite error code is returned.
127670
+** The final value of *pazDequote is undefined in this case.
127671
+*/
127672
+static int fts3tokDequoteArray(
127673
+ int argc, /* Number of elements in argv[] */
127674
+ const char * const *argv, /* Input array */
127675
+ char ***pazDequote /* Output array */
127676
+){
127677
+ int rc = SQLITE_OK; /* Return code */
127678
+ if( argc==0 ){
127679
+ *pazDequote = 0;
127680
+ }else{
127681
+ int i;
127682
+ int nByte = 0;
127683
+ char **azDequote;
127684
+
127685
+ for(i=0; i<argc; i++){
127686
+ nByte += (int)(strlen(argv[i]) + 1);
127687
+ }
127688
+
127689
+ *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
127690
+ if( azDequote==0 ){
127691
+ rc = SQLITE_NOMEM;
127692
+ }else{
127693
+ char *pSpace = (char *)&azDequote[argc];
127694
+ for(i=0; i<argc; i++){
127695
+ int n = (int)strlen(argv[i]);
127696
+ azDequote[i] = pSpace;
127697
+ memcpy(pSpace, argv[i], n+1);
127698
+ sqlite3Fts3Dequote(pSpace);
127699
+ pSpace += (n+1);
127700
+ }
127701
+ }
127702
+ }
127703
+
127704
+ return rc;
127705
+}
127706
+
127707
+/*
127708
+** Schema of the tokenizer table.
127709
+*/
127710
+#define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
127711
+
127712
+/*
127713
+** This function does all the work for both the xConnect and xCreate methods.
127714
+** These tables have no persistent representation of their own, so xConnect
127715
+** and xCreate are identical operations.
127716
+**
127717
+** argv[0]: module name
127718
+** argv[1]: database name
127719
+** argv[2]: table name
127720
+** argv[3]: first argument (tokenizer name)
127721
+*/
127722
+static int fts3tokConnectMethod(
127723
+ sqlite3 *db, /* Database connection */
127724
+ void *pUnused, /* Unused */
127725
+ int argc, /* Number of elements in argv array */
127726
+ const char * const *argv, /* xCreate/xConnect argument array */
127727
+ sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
127728
+ char **pzErr /* OUT: sqlite3_malloc'd error message */
127729
+){
127730
+ Fts3tokTable *pTab;
127731
+ const sqlite3_tokenizer_module *pMod = 0;
127732
+ sqlite3_tokenizer *pTok = 0;
127733
+ int rc;
127734
+ char **azDequote = 0;
127735
+ int nDequote;
127736
+ UNUSED_PARAMETER(pUnused);
127737
+
127738
+ rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
127739
+ if( rc!=SQLITE_OK ) return rc;
127740
+
127741
+ nDequote = argc-3;
127742
+ rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
127743
+
127744
+ if( rc==SQLITE_OK ){
127745
+ const char *zModule;
127746
+ if( nDequote<1 ){
127747
+ zModule = "simple";
127748
+ }else{
127749
+ zModule = azDequote[0];
127750
+ }
127751
+ rc = fts3tokQueryTokenizer(db, zModule, &pMod);
127752
+ }
127753
+
127754
+ if( rc!=SQLITE_OK ){
127755
+ *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
127756
+ }else if( pMod==0 ){
127757
+ rc = SQLITE_ERROR;
127758
+ }else{
127759
+ const char * const *azArg = (const char * const *)&azDequote[1];
127760
+ rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
127761
+ }
127762
+
127763
+ if( rc==SQLITE_OK ){
127764
+ pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
127765
+ if( pTab==0 ){
127766
+ rc = SQLITE_NOMEM;
127767
+ }
127768
+ }
127769
+
127770
+ if( rc==SQLITE_OK ){
127771
+ memset(pTab, 0, sizeof(Fts3tokTable));
127772
+ pTab->pMod = pMod;
127773
+ pTab->pTok = pTok;
127774
+ *ppVtab = &pTab->base;
127775
+ }else{
127776
+ if( pTok ){
127777
+ pMod->xDestroy(pTok);
127778
+ }
127779
+ }
127780
+
127781
+ sqlite3_free(azDequote);
127782
+ return rc;
127783
+}
127784
+
127785
+/*
127786
+** This function does the work for both the xDisconnect and xDestroy methods.
127787
+** These tables have no persistent representation of their own, so xDisconnect
127788
+** and xDestroy are identical operations.
127789
+*/
127790
+static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
127791
+ Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
127792
+
127793
+ pTab->pMod->xDestroy(pTab->pTok);
127794
+ sqlite3_free(pTab);
127795
+ return SQLITE_OK;
127796
+}
127797
+
127798
+/*
127799
+** xBestIndex - Analyze a WHERE and ORDER BY clause.
127800
+*/
127801
+static int fts3tokBestIndexMethod(
127802
+ sqlite3_vtab *pVTab,
127803
+ sqlite3_index_info *pInfo
127804
+){
127805
+ int i;
127806
+ UNUSED_PARAMETER(pVTab);
127807
+
127808
+ for(i=0; i<pInfo->nConstraint; i++){
127809
+ if( pInfo->aConstraint[i].usable
127810
+ && pInfo->aConstraint[i].iColumn==0
127811
+ && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
127812
+ ){
127813
+ pInfo->idxNum = 1;
127814
+ pInfo->aConstraintUsage[i].argvIndex = 1;
127815
+ pInfo->aConstraintUsage[i].omit = 1;
127816
+ pInfo->estimatedCost = 1;
127817
+ return SQLITE_OK;
127818
+ }
127819
+ }
127820
+
127821
+ pInfo->idxNum = 0;
127822
+ assert( pInfo->estimatedCost>1000000.0 );
127823
+
127824
+ return SQLITE_OK;
127825
+}
127826
+
127827
+/*
127828
+** xOpen - Open a cursor.
127829
+*/
127830
+static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
127831
+ Fts3tokCursor *pCsr;
127832
+ UNUSED_PARAMETER(pVTab);
127833
+
127834
+ pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
127835
+ if( pCsr==0 ){
127836
+ return SQLITE_NOMEM;
127837
+ }
127838
+ memset(pCsr, 0, sizeof(Fts3tokCursor));
127839
+
127840
+ *ppCsr = (sqlite3_vtab_cursor *)pCsr;
127841
+ return SQLITE_OK;
127842
+}
127843
+
127844
+/*
127845
+** Reset the tokenizer cursor passed as the only argument. As if it had
127846
+** just been returned by fts3tokOpenMethod().
127847
+*/
127848
+static void fts3tokResetCursor(Fts3tokCursor *pCsr){
127849
+ if( pCsr->pCsr ){
127850
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
127851
+ pTab->pMod->xClose(pCsr->pCsr);
127852
+ pCsr->pCsr = 0;
127853
+ }
127854
+ sqlite3_free(pCsr->zInput);
127855
+ pCsr->zInput = 0;
127856
+ pCsr->zToken = 0;
127857
+ pCsr->nToken = 0;
127858
+ pCsr->iStart = 0;
127859
+ pCsr->iEnd = 0;
127860
+ pCsr->iPos = 0;
127861
+ pCsr->iRowid = 0;
127862
+}
127863
+
127864
+/*
127865
+** xClose - Close a cursor.
127866
+*/
127867
+static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
127868
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127869
+
127870
+ fts3tokResetCursor(pCsr);
127871
+ sqlite3_free(pCsr);
127872
+ return SQLITE_OK;
127873
+}
127874
+
127875
+/*
127876
+** xNext - Advance the cursor to the next row, if any.
127877
+*/
127878
+static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
127879
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127880
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
127881
+ int rc; /* Return code */
127882
+
127883
+ pCsr->iRowid++;
127884
+ rc = pTab->pMod->xNext(pCsr->pCsr,
127885
+ &pCsr->zToken, &pCsr->nToken,
127886
+ &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
127887
+ );
127888
+
127889
+ if( rc!=SQLITE_OK ){
127890
+ fts3tokResetCursor(pCsr);
127891
+ if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127892
+ }
127893
+
127894
+ return rc;
127895
+}
127896
+
127897
+/*
127898
+** xFilter - Initialize a cursor to point at the start of its data.
127899
+*/
127900
+static int fts3tokFilterMethod(
127901
+ sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
127902
+ int idxNum, /* Strategy index */
127903
+ const char *idxStr, /* Unused */
127904
+ int nVal, /* Number of elements in apVal */
127905
+ sqlite3_value **apVal /* Arguments for the indexing scheme */
127906
+){
127907
+ int rc = SQLITE_ERROR;
127908
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127909
+ Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
127910
+ UNUSED_PARAMETER(idxStr);
127911
+ UNUSED_PARAMETER(nVal);
127912
+
127913
+ fts3tokResetCursor(pCsr);
127914
+ if( idxNum==1 ){
127915
+ const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
127916
+ int nByte = sqlite3_value_bytes(apVal[0]);
127917
+ pCsr->zInput = sqlite3_malloc(nByte+1);
127918
+ if( pCsr->zInput==0 ){
127919
+ rc = SQLITE_NOMEM;
127920
+ }else{
127921
+ memcpy(pCsr->zInput, zByte, nByte);
127922
+ pCsr->zInput[nByte] = 0;
127923
+ rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
127924
+ if( rc==SQLITE_OK ){
127925
+ pCsr->pCsr->pTokenizer = pTab->pTok;
127926
+ }
127927
+ }
127928
+ }
127929
+
127930
+ if( rc!=SQLITE_OK ) return rc;
127931
+ return fts3tokNextMethod(pCursor);
127932
+}
127933
+
127934
+/*
127935
+** xEof - Return true if the cursor is at EOF, or false otherwise.
127936
+*/
127937
+static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
127938
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127939
+ return (pCsr->zToken==0);
127940
+}
127941
+
127942
+/*
127943
+** xColumn - Return a column value.
127944
+*/
127945
+static int fts3tokColumnMethod(
127946
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
127947
+ sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
127948
+ int iCol /* Index of column to read value from */
127949
+){
127950
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127951
+
127952
+ /* CREATE TABLE x(input, token, start, end, position) */
127953
+ switch( iCol ){
127954
+ case 0:
127955
+ sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
127956
+ break;
127957
+ case 1:
127958
+ sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
127959
+ break;
127960
+ case 2:
127961
+ sqlite3_result_int(pCtx, pCsr->iStart);
127962
+ break;
127963
+ case 3:
127964
+ sqlite3_result_int(pCtx, pCsr->iEnd);
127965
+ break;
127966
+ default:
127967
+ assert( iCol==4 );
127968
+ sqlite3_result_int(pCtx, pCsr->iPos);
127969
+ break;
127970
+ }
127971
+ return SQLITE_OK;
127972
+}
127973
+
127974
+/*
127975
+** xRowid - Return the current rowid for the cursor.
127976
+*/
127977
+static int fts3tokRowidMethod(
127978
+ sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
127979
+ sqlite_int64 *pRowid /* OUT: Rowid value */
127980
+){
127981
+ Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127982
+ *pRowid = (sqlite3_int64)pCsr->iRowid;
127983
+ return SQLITE_OK;
127984
+}
127985
+
127986
+/*
127987
+** Register the fts3tok module with database connection db. Return SQLITE_OK
127988
+** if successful or an error code if sqlite3_create_module() fails.
127989
+*/
127990
+SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db){
127991
+ static const sqlite3_module fts3tok_module = {
127992
+ 0, /* iVersion */
127993
+ fts3tokConnectMethod, /* xCreate */
127994
+ fts3tokConnectMethod, /* xConnect */
127995
+ fts3tokBestIndexMethod, /* xBestIndex */
127996
+ fts3tokDisconnectMethod, /* xDisconnect */
127997
+ fts3tokDisconnectMethod, /* xDestroy */
127998
+ fts3tokOpenMethod, /* xOpen */
127999
+ fts3tokCloseMethod, /* xClose */
128000
+ fts3tokFilterMethod, /* xFilter */
128001
+ fts3tokNextMethod, /* xNext */
128002
+ fts3tokEofMethod, /* xEof */
128003
+ fts3tokColumnMethod, /* xColumn */
128004
+ fts3tokRowidMethod, /* xRowid */
128005
+ 0, /* xUpdate */
128006
+ 0, /* xBegin */
128007
+ 0, /* xSync */
128008
+ 0, /* xCommit */
128009
+ 0, /* xRollback */
128010
+ 0, /* xFindFunction */
128011
+ 0, /* xRename */
128012
+ 0, /* xSavepoint */
128013
+ 0, /* xRelease */
128014
+ 0 /* xRollbackTo */
128015
+ };
128016
+ int rc; /* Return code */
128017
+
128018
+ rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, 0);
128019
+ return rc;
128020
+}
128021
+
128022
+#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
128023
+
128024
+/************** End of fts3_tokenize_vtab.c **********************************/
126290128025
/************** Begin file fts3_write.c **************************************/
126291128026
/*
126292128027
** 2009 Oct 23
126293128028
**
126294128029
** The author disclaims copyright to this source code. In place of
126295128030
--- 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.16.1. By combining all the individual C code files into this
4 ** single large file, the entire code can be compiled as a single translation
5 ** unit. This allows many compilers to do optimizations that would not be
6 ** possible if the files were compiled separately. Performance improvements
7 ** of 5% or more are commonly seen when SQLite is compiled as a single
8 ** translation unit.
@@ -360,15 +360,15 @@
360 **
361 ** Older versions of SQLite used an optional THREADSAFE macro.
362 ** We support that for legacy.
363 */
364 #if !defined(SQLITE_THREADSAFE)
365 #if defined(THREADSAFE)
366 # define SQLITE_THREADSAFE THREADSAFE
367 #else
368 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
369 #endif
370 #endif
371
372 /*
373 ** Powersafe overwrite is on by default. But can be turned off using
374 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
@@ -676,13 +676,13 @@
676 **
677 ** See also: [sqlite3_libversion()],
678 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
679 ** [sqlite_version()] and [sqlite_source_id()].
680 */
681 #define SQLITE_VERSION "3.7.16.1"
682 #define SQLITE_VERSION_NUMBER 3007016
683 #define SQLITE_SOURCE_ID "2013-03-27 20:41:15 274d2a22660c7b34b8bbd85f3c29cbafbcb1b4e7"
684
685 /*
686 ** CAPI3REF: Run-Time Library Version Numbers
687 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
688 **
@@ -994,10 +994,12 @@
994 #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
995 #define SQLITE_AUTH 23 /* Authorization denied */
996 #define SQLITE_FORMAT 24 /* Auxiliary database format error */
997 #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
998 #define SQLITE_NOTADB 26 /* File opened that is not a database file */
 
 
999 #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
1000 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
1001 /* end-of-error-codes */
1002
1003 /*
@@ -1044,10 +1046,11 @@
1044 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
1045 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
1046 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1047 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1048 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
 
1049 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1050 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1051 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1052 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1053 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -1063,10 +1066,12 @@
1063 #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1064 #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
1065 #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
1066 #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
1067 #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
 
 
1068
1069 /*
1070 ** CAPI3REF: Flags For File Open Operations
1071 **
1072 ** These bit values are intended for use in the
@@ -1302,10 +1307,13 @@
1302 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1303 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1304 void (*xShmBarrier)(sqlite3_file*);
1305 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1306 /* Methods above are valid for version 2 */
 
 
 
1307 /* Additional methods may be added in future releases */
1308 };
1309
1310 /*
1311 ** CAPI3REF: Standard File Control Opcodes
@@ -1438,11 +1446,12 @@
1438 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1439 ** file control occurs at the beginning of pragma statement analysis and so
1440 ** it is able to override built-in [PRAGMA] statements.
1441 **
1442 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1443 ** ^This file-control may be invoked by SQLite on the database file handle
 
1444 ** shortly after it is opened in order to provide a custom VFS with access
1445 ** to the connections busy-handler callback. The argument is of type (void **)
1446 ** - an array of two (void *) values. The first (void *) actually points
1447 ** to a function of type (int (*)(void *)). In order to invoke the connections
1448 ** busy-handler, this function should be invoked with the second (void *) in
@@ -1449,17 +1458,28 @@
1449 ** the array as the only argument. If it returns non-zero, then the operation
1450 ** should be retried. If it returns zero, the custom VFS should abandon the
1451 ** current operation.
1452 **
1453 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1454 ** ^Application can invoke this file-control to have SQLite generate a
 
1455 ** temporary filename using the same algorithm that is followed to generate
1456 ** temporary filenames for TEMP tables and other internal uses. The
1457 ** argument should be a char** which will be filled with the filename
1458 ** written into memory obtained from [sqlite3_malloc()]. The caller should
1459 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
1460 **
 
 
 
 
 
 
 
 
 
 
1461 ** </ul>
1462 */
1463 #define SQLITE_FCNTL_LOCKSTATE 1
1464 #define SQLITE_GET_LOCKPROXYFILE 2
1465 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -1474,10 +1494,11 @@
1474 #define SQLITE_FCNTL_VFSNAME 12
1475 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1476 #define SQLITE_FCNTL_PRAGMA 14
1477 #define SQLITE_FCNTL_BUSYHANDLER 15
1478 #define SQLITE_FCNTL_TEMPFILENAME 16
 
1479
1480 /*
1481 ** CAPI3REF: Mutex Handle
1482 **
1483 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2186,26 +2207,42 @@
2186 **
2187 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2188 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2189 ** <dd> These options are obsolete and should not be used by new code.
2190 ** They are retained for backwards compatibility but are now no-ops.
2191 ** </dl>
2192 **
2193 ** [[SQLITE_CONFIG_SQLLOG]]
2194 ** <dt>SQLITE_CONFIG_SQLLOG
2195 ** <dd>This option is only available if sqlite is compiled with the
2196 ** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
2197 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2198 ** The second should be of type (void*). The callback is invoked by the library
2199 ** in three separate circumstances, identified by the value passed as the
2200 ** fourth parameter. If the fourth parameter is 0, then the database connection
2201 ** passed as the second argument has just been opened. The third argument
2202 ** points to a buffer containing the name of the main database file. If the
2203 ** fourth parameter is 1, then the SQL statement that the third parameter
2204 ** points to has just been executed. Or, if the fourth parameter is 2, then
2205 ** the connection being passed as the second parameter is being closed. The
2206 ** third parameter is passed NULL In this case.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2207 ** </dl>
2208 */
2209 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2210 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2211 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2225,10 +2262,11 @@
2225 #define SQLITE_CONFIG_URI 17 /* int */
2226 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2227 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
2228 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
2229 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
 
2230
2231 /*
2232 ** CAPI3REF: Database Connection Configuration Options
2233 **
2234 ** These constants are the available integer configuration options that
@@ -4756,11 +4794,11 @@
4756 ** SQLITE_TRANSIENT value means that the content will likely change in
4757 ** the near future and that SQLite should make its own private copy of
4758 ** the content before returning.
4759 **
4760 ** The typedef is necessary to work around problems in certain
4761 ** C++ compilers. See ticket #2191.
4762 */
4763 typedef void (*sqlite3_destructor_type)(void*);
4764 #define SQLITE_STATIC ((sqlite3_destructor_type)0)
4765 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
4766
@@ -5555,15 +5593,24 @@
5555 ** CAPI3REF: Load An Extension
5556 **
5557 ** ^This interface loads an SQLite extension library from the named file.
5558 **
5559 ** ^The sqlite3_load_extension() interface attempts to load an
5560 ** SQLite extension library contained in the file zFile.
 
 
 
 
 
5561 **
5562 ** ^The entry point is zProc.
5563 ** ^zProc may be 0, in which case the name of the entry point
5564 ** defaults to "sqlite3_extension_init".
 
 
 
 
5565 ** ^The sqlite3_load_extension() interface returns
5566 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5567 ** ^If an error occurs and pzErrMsg is not 0, then the
5568 ** [sqlite3_load_extension()] interface shall attempt to
5569 ** fill *pzErrMsg with error message text stored in memory
@@ -5585,15 +5632,15 @@
5585
5586 /*
5587 ** CAPI3REF: Enable Or Disable Extension Loading
5588 **
5589 ** ^So as not to open security holes in older applications that are
5590 ** unprepared to deal with extension loading, and as a means of disabling
5591 ** extension loading while evaluating user-entered SQL, the following API
5592 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5593 **
5594 ** ^Extension loading is off by default. See ticket #1863.
5595 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5596 ** to turn extension loading on and call it with onoff==0 to turn
5597 ** it back off again.
5598 */
5599 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5601,11 +5648,11 @@
5601 /*
5602 ** CAPI3REF: Automatically Load Statically Linked Extensions
5603 **
5604 ** ^This interface causes the xEntryPoint() function to be invoked for
5605 ** each new [database connection] that is created. The idea here is that
5606 ** xEntryPoint() is the entry point for a statically linked SQLite extension
5607 ** that is to be automatically loaded into all new database connections.
5608 **
5609 ** ^(Even though the function prototype shows that xEntryPoint() takes
5610 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5611 ** arguments and expects and integer result as if the signature of the
@@ -7381,10 +7428,25 @@
7381 ** independence" that SQLite uses internally when comparing identifiers.
7382 */
7383 SQLITE_API int sqlite3_stricmp(const char *, const char *);
7384 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
7385
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7386 /*
7387 ** CAPI3REF: Error Logging Interface
7388 **
7389 ** ^The [sqlite3_log()] interface writes a message into the error log
7390 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
@@ -8069,10 +8131,11 @@
8069 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
8070 ** on the command-line
8071 */
8072 #ifndef SQLITE_TEMP_STORE
8073 # define SQLITE_TEMP_STORE 1
 
8074 #endif
8075
8076 /*
8077 ** GCC does not define the offsetof() macro so we'll have to do it
8078 ** ourselves.
@@ -8216,10 +8279,53 @@
8216 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
8217 #else
8218 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
8219 #endif
8220
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8221
8222 /*
8223 ** An instance of the following structure is used to store the busy-handler
8224 ** callback for a given sqlite handle.
8225 **
@@ -8437,10 +8543,11 @@
8437 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
8438 #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
8439
8440 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8441 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
 
8442 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
8443 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8444 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8445 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8446 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
@@ -9137,10 +9244,16 @@
9137 #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
9138 #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
9139 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
9140 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
9141
 
 
 
 
 
 
9142 /*
9143 ** The remainder of this file contains the declarations of the functions
9144 ** that make up the Pager sub-system API. See source code comments for
9145 ** a detailed description of each routine.
9146 */
@@ -9161,10 +9274,11 @@
9161 /* Functions used to configure a Pager object. */
9162 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
9163 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9164 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9165 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
 
9166 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9167 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
9168 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9169 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9170 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
@@ -9306,10 +9420,12 @@
9306 #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
9307 ** writing this page to the database */
9308 #define PGHDR_NEED_READ 0x008 /* Content is unread */
9309 #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
9310 #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
 
 
9311
9312 /* Initialize and shutdown the page cache subsystem */
9313 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
9314 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
9315
@@ -9518,18 +9634,10 @@
9518 */
9519 #if !defined(SQLITE_OS_WINRT)
9520 # define SQLITE_OS_WINRT 0
9521 #endif
9522
9523 /*
9524 ** When compiled for WinCE or WinRT, there is no concept of the current
9525 ** directory.
9526 */
9527 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
9528 # define SQLITE_CURDIR 1
9529 #endif
9530
9531 /* If the SET_FULLSYNC macro is not defined above, then make it
9532 ** a no-op
9533 */
9534 #ifndef SET_FULLSYNC
9535 # define SET_FULLSYNC(x,y)
@@ -9678,10 +9786,12 @@
9678 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9679 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9680 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9681 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9682 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
 
 
9683
9684
9685 /*
9686 ** Functions for accessing sqlite3_vfs methods
9687 */
@@ -9917,10 +10027,11 @@
9917 sqlite3_mutex *mutex; /* Connection mutex */
9918 Db *aDb; /* All backends */
9919 int nDb; /* Number of backends currently in use */
9920 int flags; /* Miscellaneous flags. See below */
9921 i64 lastRowid; /* ROWID of most recent insert (see above) */
 
9922 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
9923 int errCode; /* Most recent error code (SQLITE_*) */
9924 int errMask; /* & result codes with this before returning */
9925 u16 dbOptFlags; /* Flags to enable/disable optimizations */
9926 u8 autoCommit; /* The auto-commit flag. */
@@ -11153,10 +11264,12 @@
11153 */
11154 #define NC_AllowAgg 0x01 /* Aggregate functions are allowed here */
11155 #define NC_HasAgg 0x02 /* One or more aggregate functions seen */
11156 #define NC_IsCheck 0x04 /* True if resolving names in a CHECK constraint */
11157 #define NC_InAggFunc 0x08 /* True if analyzing arguments to an agg func */
 
 
11158
11159 /*
11160 ** An instance of the following structure contains all information
11161 ** needed to generate code for a single SELECT statement.
11162 **
@@ -11588,10 +11701,12 @@
11588 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
11589 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
11590 void *pHeap; /* Heap storage space */
11591 int nHeap; /* Size of pHeap[] */
11592 int mnReq, mxReq; /* Min and max heap requests sizes */
 
 
11593 void *pScratch; /* Scratch memory */
11594 int szScratch; /* Size of each scratch buffer */
11595 int nScratch; /* Number of scratch buffers */
11596 void *pPage; /* Page cache memory */
11597 int szPage; /* Size of each page in pPage[] */
@@ -11622,10 +11737,11 @@
11622 struct Walker {
11623 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
11624 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
11625 Parse *pParse; /* Parser context. */
11626 int walkerDepth; /* Number of subqueries */
 
11627 union { /* Extra data for callback */
11628 NameContext *pNC; /* Naming context */
11629 int i; /* Integer value */
11630 SrcList *pSrcList; /* FROM clause */
11631 struct SrcCount *pSrcCount; /* Counting column references */
@@ -12609,10 +12725,12 @@
12609 {0,0,0,0,0,0,0,0,0}, /* mutex */
12610 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
12611 (void*)0, /* pHeap */
12612 0, /* nHeap */
12613 0, 0, /* mnHeap, mxHeap */
 
 
12614 (void*)0, /* pScratch */
12615 0, /* szScratch */
12616 0, /* nScratch */
12617 (void*)0, /* pPage */
12618 0, /* szPage */
@@ -12732,18 +12850,18 @@
12732 "CHECK_PAGES",
12733 #endif
12734 #ifdef SQLITE_COVERAGE_TEST
12735 "COVERAGE_TEST",
12736 #endif
12737 #ifdef SQLITE_CURDIR
12738 "CURDIR",
12739 #endif
12740 #ifdef SQLITE_DEBUG
12741 "DEBUG",
12742 #endif
12743 #ifdef SQLITE_DEFAULT_LOCKING_MODE
12744 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
 
 
 
12745 #endif
12746 #ifdef SQLITE_DISABLE_DIRSYNC
12747 "DISABLE_DIRSYNC",
12748 #endif
12749 #ifdef SQLITE_DISABLE_LFS
@@ -12831,10 +12949,13 @@
12831 "INT64_TYPE",
12832 #endif
12833 #ifdef SQLITE_LOCK_TRACE
12834 "LOCK_TRACE",
12835 #endif
 
 
 
12836 #ifdef SQLITE_MAX_SCHEMA_RETRY
12837 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
12838 #endif
12839 #ifdef SQLITE_MEMDEBUG
12840 "MEMDEBUG",
@@ -12888,15 +13009,10 @@
12888 "OMIT_CAST",
12889 #endif
12890 #ifdef SQLITE_OMIT_CHECK
12891 "OMIT_CHECK",
12892 #endif
12893 /* // redundant
12894 ** #ifdef SQLITE_OMIT_COMPILEOPTION_DIAGS
12895 ** "OMIT_COMPILEOPTION_DIAGS",
12896 ** #endif
12897 */
12898 #ifdef SQLITE_OMIT_COMPLETE
12899 "OMIT_COMPLETE",
12900 #endif
12901 #ifdef SQLITE_OMIT_COMPOUND_SELECT
12902 "OMIT_COMPOUND_SELECT",
@@ -13034,17 +13150,17 @@
13034 "SOUNDEX",
13035 #endif
13036 #ifdef SQLITE_TCL
13037 "TCL",
13038 #endif
13039 #ifdef SQLITE_TEMP_STORE
13040 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
13041 #endif
13042 #ifdef SQLITE_TEST
13043 "TEST",
13044 #endif
13045 #ifdef SQLITE_THREADSAFE
13046 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
13047 #endif
13048 #ifdef SQLITE_USE_ALLOCA
13049 "USE_ALLOCA",
13050 #endif
@@ -13066,12 +13182,15 @@
13066 n = sqlite3Strlen30(zOptName);
13067
13068 /* Since ArraySize(azCompileOpt) is normally in single digits, a
13069 ** linear search is adequate. No need for a binary search. */
13070 for(i=0; i<ArraySize(azCompileOpt); i++){
13071 if( (sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0)
13072 && ( (azCompileOpt[i][n]==0) || (azCompileOpt[i][n]=='=') ) ) return 1;
 
 
 
13073 }
13074 return 0;
13075 }
13076
13077 /*
@@ -13124,10 +13243,18 @@
13124 ** this header information was factored out.
13125 */
13126 #ifndef _VDBEINT_H_
13127 #define _VDBEINT_H_
13128
 
 
 
 
 
 
 
 
13129 /*
13130 ** SQL is translated into a sequence of instructions to be
13131 ** executed by a virtual machine. Each instruction is an instance
13132 ** of the following structure.
13133 */
@@ -15089,10 +15216,30 @@
15089 void volatile **pp /* OUT: Pointer to mapping */
15090 ){
15091 DO_OS_MALLOC_TEST(id);
15092 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
15093 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15094
15095 /*
15096 ** The next group of routines are convenience wrappers around the
15097 ** VFS methods.
15098 */
@@ -21584,11 +21731,11 @@
21584 *pNum = (i64)u;
21585 }
21586 testcase( i==18 );
21587 testcase( i==19 );
21588 testcase( i==20 );
21589 if( (c+nonNum!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr ){
21590 /* zNum is empty or contains non-numeric text or is longer
21591 ** than 19 digits (thus guaranteeing that it is too large) */
21592 return 1;
21593 }else if( i<19*incr ){
21594 /* Less than 19 digits, so we know that it fits in 64 bits */
@@ -22945,10 +23092,15 @@
22945 void *lockingContext; /* Locking style specific state */
22946 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
22947 const char *zPath; /* Name of the file */
22948 unixShm *pShm; /* Shared memory segment information */
22949 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
 
 
 
 
 
22950 #ifdef __QNXNTO__
22951 int sectorSize; /* Device sector size */
22952 int deviceCharacteristics; /* Precomputed device characteristics */
22953 #endif
22954 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -22969,11 +23121,13 @@
22969 ** one described by ticket #3584.
22970 */
22971 unsigned char transCntrChng; /* True if the transaction counter changed */
22972 unsigned char dbUpdate; /* True if any part of database file changed */
22973 unsigned char inNormalWrite; /* True if in a normal write operation */
 
22974 #endif
 
22975 #ifdef SQLITE_TEST
22976 /* In test mode, increase the size of this structure a bit so that
22977 ** it is larger than the struct CrashFile defined in test6.c.
22978 */
22979 char aPadding[32];
@@ -22993,10 +23147,11 @@
22993 #endif
22994 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
22995 #define UNIXFILE_DELETE 0x20 /* Delete on close */
22996 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
22997 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
 
22998
22999 /*
23000 ** Include code that is common to all os_*.c files
23001 */
23002 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -23234,10 +23389,21 @@
23234 #define threadid pthread_self()
23235 #else
23236 #define threadid 0
23237 #endif
23238
 
 
 
 
 
 
 
 
 
 
 
23239 /*
23240 ** Different Unix systems declare open() in different ways. Same use
23241 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
23242 ** The difference is important when using a pointer to the function.
23243 **
@@ -23365,10 +23531,23 @@
23365 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23366
23367 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23368 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23369
 
 
 
 
 
 
 
 
 
 
 
 
 
23370 }; /* End of the overrideable system calls */
23371
23372 /*
23373 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
23374 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -23694,11 +23873,10 @@
23694
23695 default:
23696 return sqliteIOErr;
23697 }
23698 }
23699
23700
23701
23702 /******************************************************************************
23703 ****************** Begin Unique File ID Utility Used By VxWorks ***************
23704 **
@@ -24032,11 +24210,10 @@
24032 #else
24033 /* Non-threadsafe build, use strerror(). */
24034 zErr = strerror(iErrno);
24035 #endif
24036
24037 assert( errcode!=SQLITE_OK );
24038 if( zPath==0 ) zPath = "";
24039 sqlite3_log(errcode,
24040 "os_unix.c:%d: (%d) %s(%s) - %s",
24041 iLine, iErrno, zFunc, zPath, zErr
24042 );
@@ -24197,10 +24374,54 @@
24197 }
24198 *ppInode = pInode;
24199 return SQLITE_OK;
24200 }
24201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24202
24203 /*
24204 ** This routine checks if there is a RESERVED lock held on the specified
24205 ** file by this or any other process. If such a lock is held, set *pResOut
24206 ** to a non-zero value otherwise *pResOut is set to zero. The return value
@@ -24728,12 +24949,16 @@
24728 **
24729 ** If the locking level of the file descriptor is already at or below
24730 ** the requested locking level, this routine is a no-op.
24731 */
24732 static int unixUnlock(sqlite3_file *id, int eFileLock){
 
24733 return posixUnlock(id, eFileLock, 0);
24734 }
 
 
 
24735
24736 /*
24737 ** This function performs the parts of the "close file" operation
24738 ** common to all locking schemes. It closes the directory and file
24739 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24743,10 +24968,11 @@
24743 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24744 ** vxworksReleaseFileId() routine.
24745 */
24746 static int closeUnixFile(sqlite3_file *id){
24747 unixFile *pFile = (unixFile*)id;
 
24748 if( pFile->h>=0 ){
24749 robust_close(pFile, pFile->h, __LINE__);
24750 pFile->h = -1;
24751 }
24752 #if OS_VXWORKS
@@ -24769,10 +24995,11 @@
24769 ** Close a file.
24770 */
24771 static int unixClose(sqlite3_file *id){
24772 int rc = SQLITE_OK;
24773 unixFile *pFile = (unixFile *)id;
 
24774 unixUnlock(id, NO_LOCK);
24775 unixEnterMutex();
24776
24777 /* unixFile.pInode is always valid here. Otherwise, a different close
24778 ** routine (e.g. nolockClose()) would be called instead.
@@ -26009,10 +26236,27 @@
26009 assert( pFile->pUnused==0
26010 || offset>=PENDING_BYTE+512
26011 || offset+amt<=PENDING_BYTE
26012 );
26013 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26014
26015 got = seekAndRead(pFile, offset, pBuf, amt);
26016 if( got==amt ){
26017 return SQLITE_OK;
26018 }else if( got<0 ){
@@ -26111,10 +26355,27 @@
26111 SimulateIOErrorBenign(0);
26112 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
26113 pFile->transCntrChng = 1; /* The transaction counter has changed */
26114 }
26115 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26116 }
26117 #endif
26118
26119 while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
26120 amt -= wrote;
@@ -26395,10 +26656,18 @@
26395 */
26396 if( pFile->inNormalWrite && nByte==0 ){
26397 pFile->transCntrChng = 1;
26398 }
26399 #endif
 
 
 
 
 
 
 
 
26400
26401 return SQLITE_OK;
26402 }
26403 }
26404
@@ -26483,10 +26752,23 @@
26483 iWrite += nBlk;
26484 }
26485 #endif
26486 }
26487 }
 
 
 
 
 
 
 
 
 
 
 
 
 
26488
26489 return SQLITE_OK;
26490 }
26491
26492 /*
@@ -26550,10 +26832,22 @@
26550 if( zTFile ){
26551 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26552 *(char**)pArg = zTFile;
26553 }
26554 return SQLITE_OK;
 
 
 
 
 
 
 
 
 
 
 
 
26555 }
26556 #ifdef SQLITE_DEBUG
26557 /* The pager calls this method to signal that it has done
26558 ** a rollback and that the database is therefore unchanged and
26559 ** it hence it is OK for the transaction change counter to be
@@ -26863,11 +27157,11 @@
26863 int i;
26864 assert( p->pInode==pFd->pInode );
26865 sqlite3_mutex_free(p->mutex);
26866 for(i=0; i<p->nRegion; i++){
26867 if( p->h>=0 ){
26868 munmap(p->apRegion[i], p->szRegion);
26869 }else{
26870 sqlite3_free(p->apRegion[i]);
26871 }
26872 }
26873 sqlite3_free(p->apRegion);
@@ -27136,11 +27430,11 @@
27136 }
27137 pShmNode->apRegion = apNew;
27138 while(pShmNode->nRegion<=iRegion){
27139 void *pMem;
27140 if( pShmNode->h>=0 ){
27141 pMem = mmap(0, szRegion,
27142 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
27143 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
27144 );
27145 if( pMem==MAP_FAILED ){
27146 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
@@ -27352,10 +27646,240 @@
27352 # define unixShmMap 0
27353 # define unixShmLock 0
27354 # define unixShmBarrier 0
27355 # define unixShmUnmap 0
27356 #endif /* #ifndef SQLITE_OMIT_WAL */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27357
27358 /*
27359 ** Here ends the implementation of all sqlite3_file methods.
27360 **
27361 ********************** End sqlite3_file Methods *******************************
@@ -27411,11 +27935,13 @@
27411 unixSectorSize, /* xSectorSize */ \
27412 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
27413 unixShmMap, /* xShmMap */ \
27414 unixShmLock, /* xShmLock */ \
27415 unixShmBarrier, /* xShmBarrier */ \
27416 unixShmUnmap /* xShmUnmap */ \
 
 
27417 }; \
27418 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
27419 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
27420 return &METHOD; \
27421 } \
@@ -27428,11 +27954,11 @@
27428 ** are also created.
27429 */
27430 IOMETHODS(
27431 posixIoFinder, /* Finder function name */
27432 posixIoMethods, /* sqlite3_io_methods object name */
27433 2, /* shared memory is enabled */
27434 unixClose, /* xClose method */
27435 unixLock, /* xLock method */
27436 unixUnlock, /* xUnlock method */
27437 unixCheckReservedLock /* xCheckReservedLock method */
27438 )
@@ -27679,10 +28205,11 @@
27679 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
27680 pNew->h = h;
27681 pNew->pVfs = pVfs;
27682 pNew->zPath = zFilename;
27683 pNew->ctrlFlags = (u8)ctrlFlags;
 
27684 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
27685 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
27686 pNew->ctrlFlags |= UNIXFILE_PSOW;
27687 }
27688 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -27823,10 +28350,11 @@
27823 if( rc!=SQLITE_OK ){
27824 if( h>=0 ) robust_close(pNew, h, __LINE__);
27825 }else{
27826 pNew->pMethod = pLockingStyle;
27827 OpenCounter(+1);
 
27828 }
27829 return rc;
27830 }
27831
27832 /*
@@ -29916,11 +30444,11 @@
29916 };
29917 unsigned int i; /* Loop counter */
29918
29919 /* Double-check that the aSyscall[] array has been constructed
29920 ** correctly. See ticket [bb3a86e890c8e96ab] */
29921 assert( ArraySize(aSyscall)==21 );
29922
29923 /* Register all VFSes defined in the aVfs[] array */
29924 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
29925 sqlite3_vfs_register(&aVfs[i], i==0);
29926 }
@@ -30299,15 +30827,24 @@
30299 HANDLE hMutex; /* Mutex used to control access to shared lock */
30300 HANDLE hShared; /* Shared memory segment used for locking */
30301 winceLock local; /* Locks obtained by this instance of winFile */
30302 winceLock *shared; /* Global shared lock memory for the file */
30303 #endif
 
 
 
 
 
 
 
 
30304 };
30305
30306 /*
30307 ** Allowed values for winFile.ctrlFlags
30308 */
 
30309 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
30310 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
30311
30312 /*
30313 * The size of the buffer used by sqlite3_win32_write_debug().
@@ -32210,10 +32747,15 @@
32210
32211 return 0;
32212 #endif
32213 }
32214
 
 
 
 
 
32215 /*
32216 ** Close a file.
32217 **
32218 ** It is reported that an attempt to close a handle might sometimes
32219 ** fail. This is a very unreasonable result, but Windows is notorious
@@ -32231,10 +32773,16 @@
32231 #ifndef SQLITE_OMIT_WAL
32232 assert( pFile->pShm==0 );
32233 #endif
32234 OSTRACE(("CLOSE %d\n", pFile->h));
32235 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
 
 
 
 
 
 
32236 do{
32237 rc = osCloseHandle(pFile->h);
32238 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32239 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
32240 #if SQLITE_OS_WINCE
@@ -32279,12 +32827,30 @@
32279 winFile *pFile = (winFile*)id; /* file handle */
32280 DWORD nRead; /* Number of bytes actually read from file */
32281 int nRetry = 0; /* Number of retrys */
32282
32283 assert( id!=0 );
 
32284 SimulateIOError(return SQLITE_IOERR_READ);
32285 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32286
32287 #if SQLITE_OS_WINCE
32288 if( seekWinFile(pFile, offset) ){
32289 return SQLITE_FULL;
32290 }
@@ -32330,10 +32896,27 @@
32330 assert( pFile );
32331 SimulateIOError(return SQLITE_IOERR_WRITE);
32332 SimulateDiskfullError(return SQLITE_FULL);
32333
32334 OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32335
32336 #if SQLITE_OS_WINCE
32337 rc = seekWinFile(pFile, offset);
32338 if( rc==0 ){
32339 #else
@@ -32398,10 +32981,11 @@
32398 ** Truncate an open file to a specified size
32399 */
32400 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
32401 winFile *pFile = (winFile*)id; /* File handle object */
32402 int rc = SQLITE_OK; /* Return code for this function */
 
32403
32404 assert( pFile );
32405
32406 OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
32407 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
@@ -32416,16 +33000,27 @@
32416 }
32417
32418 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
32419 if( seekWinFile(pFile, nByte) ){
32420 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
32421 "winTruncate1", pFile->zPath);
32422 }else if( 0==osSetEndOfFile(pFile->h) ){
32423 pFile->lastErrno = osGetLastError();
 
32424 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
32425 "winTruncate2", pFile->zPath);
32426 }
 
 
 
 
 
 
 
 
 
 
32427
32428 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
32429 return rc;
32430 }
32431
@@ -32790,11 +33385,11 @@
32790 assert( id!=0 );
32791 if( pFile->locktype>=RESERVED_LOCK ){
32792 rc = 1;
32793 OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
32794 }else{
32795 rc = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
32796 if( rc ){
32797 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
32798 }
32799 rc = !rc;
32800 OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
@@ -32930,10 +33525,21 @@
32930 getTempname(pFile->pVfs->mxPathname, zTFile);
32931 *(char**)pArg = zTFile;
32932 }
32933 return SQLITE_OK;
32934 }
 
 
 
 
 
 
 
 
 
 
 
32935 }
32936 return SQLITE_NOTFOUND;
32937 }
32938
32939 /*
@@ -33599,10 +34205,196 @@
33599 # define winShmMap 0
33600 # define winShmLock 0
33601 # define winShmBarrier 0
33602 # define winShmUnmap 0
33603 #endif /* #ifndef SQLITE_OMIT_WAL */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33604
33605 /*
33606 ** Here ends the implementation of all sqlite3_file methods.
33607 **
33608 ********************** End sqlite3_file Methods *******************************
@@ -33611,11 +34403,11 @@
33611 /*
33612 ** This vector defines all the methods that can operate on an
33613 ** sqlite3_file for win32.
33614 */
33615 static const sqlite3_io_methods winIoMethod = {
33616 2, /* iVersion */
33617 winClose, /* xClose */
33618 winRead, /* xRead */
33619 winWrite, /* xWrite */
33620 winTruncate, /* xTruncate */
33621 winSync, /* xSync */
@@ -33627,11 +34419,13 @@
33627 winSectorSize, /* xSectorSize */
33628 winDeviceCharacteristics, /* xDeviceCharacteristics */
33629 winShmMap, /* xShmMap */
33630 winShmLock, /* xShmLock */
33631 winShmBarrier, /* xShmBarrier */
33632 winShmUnmap /* xShmUnmap */
 
 
33633 };
33634
33635 /****************************************************************************
33636 **************************** sqlite3_vfs methods ****************************
33637 **
@@ -33803,13 +34597,11 @@
33803 #endif
33804
33805 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
33806 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
33807 int isCreate = (flags & SQLITE_OPEN_CREATE);
33808 #ifndef NDEBUG
33809 int isReadonly = (flags & SQLITE_OPEN_READONLY);
33810 #endif
33811 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
33812
33813 #ifndef NDEBUG
33814 int isOpenJournal = (isCreate && (
33815 eType==SQLITE_OPEN_MASTER_JOURNAL
@@ -34016,15 +34808,25 @@
34016 }
34017
34018 pFile->pMethod = &winIoMethod;
34019 pFile->pVfs = pVfs;
34020 pFile->h = h;
 
 
 
34021 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
34022 pFile->ctrlFlags |= WINFILE_PSOW;
34023 }
34024 pFile->lastErrno = NO_ERROR;
34025 pFile->zPath = zName;
 
 
 
 
 
 
 
34026
34027 OpenCounter(+1);
34028 return rc;
34029 }
34030
@@ -34649,20 +35451,19 @@
34649
34650 /* Double-check that the aSyscall[] array has been constructed
34651 ** correctly. See ticket [bb3a86e890c8e96ab] */
34652 assert( ArraySize(aSyscall)==74 );
34653
34654 #ifndef SQLITE_OMIT_WAL
34655 /* get memory map allocation granularity */
34656 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
34657 #if SQLITE_OS_WINRT
34658 osGetNativeSystemInfo(&winSysInfo);
34659 #else
34660 osGetSystemInfo(&winSysInfo);
34661 #endif
34662 assert(winSysInfo.dwAllocationGranularity > 0);
34663 #endif
34664
34665 sqlite3_vfs_register(&winVfs, 1);
34666 return SQLITE_OK;
34667 }
34668
@@ -37295,11 +38096,10 @@
37295 # define sqlite3WalOpen(x,y,z) 0
37296 # define sqlite3WalLimit(x,y)
37297 # define sqlite3WalClose(w,x,y,z) 0
37298 # define sqlite3WalBeginReadTransaction(y,z) 0
37299 # define sqlite3WalEndReadTransaction(z)
37300 # define sqlite3WalRead(v,w,x,y,z) 0
37301 # define sqlite3WalDbsize(y) 0
37302 # define sqlite3WalBeginWriteTransaction(y) 0
37303 # define sqlite3WalEndWriteTransaction(x) 0
37304 # define sqlite3WalUndo(x,y,z) 0
37305 # define sqlite3WalSavepoint(y,z)
@@ -37335,11 +38135,12 @@
37335 */
37336 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
37337 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
37338
37339 /* Read a page from the write-ahead log, if it is present. */
37340 SQLITE_PRIVATE int sqlite3WalRead(Wal *pWal, Pgno pgno, int *pInWal, int nOut, u8 *pOut);
 
37341
37342 /* If the WAL is not empty, return the size of the database. */
37343 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
37344
37345 /* Obtain or release the WRITER lock. */
@@ -38035,10 +38836,15 @@
38035 i64 journalHdr; /* Byte offset to previous journal header */
38036 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
38037 PagerSavepoint *aSavepoint; /* Array of active savepoints */
38038 int nSavepoint; /* Number of elements in aSavepoint[] */
38039 char dbFileVers[16]; /* Changes whenever database file changes */
 
 
 
 
 
38040 /*
38041 ** End of the routinely-changing class members
38042 ***************************************************************************/
38043
38044 u16 nExtra; /* Add this many bytes to each in-memory page */
@@ -38145,10 +38951,20 @@
38145 # define MEMDB 0
38146 #else
38147 # define MEMDB pPager->memDb
38148 #endif
38149
 
 
 
 
 
 
 
 
 
 
38150 /*
38151 ** The maximum legal page number is (2^31 - 1).
38152 */
38153 #define PAGER_MAX_PGNO 2147483647
38154
@@ -39632,11 +40448,11 @@
39632 && isSynced
39633 ){
39634 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
39635 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
39636 assert( !pagerUseWal(pPager) );
39637 rc = sqlite3OsWrite(pPager->fd, (u8*)aData, pPager->pageSize, ofst);
39638 if( pgno>pPager->dbFileSize ){
39639 pPager->dbFileSize = pgno;
39640 }
39641 if( pPager->pBackup ){
39642 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
@@ -40023,10 +40839,11 @@
40023 Pgno mxPg = 0; /* Size of the original file in pages */
40024 int rc; /* Result code of a subroutine */
40025 int res = 1; /* Value returned by sqlite3OsAccess() */
40026 char *zMaster = 0; /* Name of master journal file if any */
40027 int needPagerReset; /* True to reset page prior to first page rollback */
 
40028
40029 /* Figure out how many records are in the journal. Abort early if
40030 ** the journal is empty.
40031 */
40032 assert( isOpen(pPager->jfd) );
@@ -40123,11 +40940,13 @@
40123 if( needPagerReset ){
40124 pager_reset(pPager);
40125 needPagerReset = 0;
40126 }
40127 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40128 if( rc!=SQLITE_OK ){
 
 
40129 if( rc==SQLITE_DONE ){
40130 pPager->journalOff = szJ;
40131 break;
40132 }else if( rc==SQLITE_IOERR_SHORT_READ ){
40133 /* If the journal has been truncated, simply stop reading and
@@ -40193,10 +41012,14 @@
40193 ** see if it is possible to delete the master journal.
40194 */
40195 rc = pager_delmaster(pPager, zMaster);
40196 testcase( rc!=SQLITE_OK );
40197 }
 
 
 
 
40198
40199 /* The Pager.sectorSize variable may have been updated while rolling
40200 ** back a journal created by a process with a different sector size
40201 ** value. Reset it to the correct value for this process.
40202 */
@@ -40214,15 +41037,14 @@
40214 ** the value read from the database file.
40215 **
40216 ** If an IO error occurs, then the IO error is returned to the caller.
40217 ** Otherwise, SQLITE_OK is returned.
40218 */
40219 static int readDbPage(PgHdr *pPg){
40220 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
40221 Pgno pgno = pPg->pgno; /* Page number to read */
40222 int rc = SQLITE_OK; /* Return code */
40223 int isInWal = 0; /* True if page is in log file */
40224 int pgsz = pPager->pageSize; /* Number of bytes to read */
40225
40226 assert( pPager->eState>=PAGER_READER && !MEMDB );
40227 assert( isOpen(pPager->fd) );
40228
@@ -40230,15 +41052,14 @@
40230 assert( pPager->tempFile );
40231 memset(pPg->pData, 0, pPager->pageSize);
40232 return SQLITE_OK;
40233 }
40234
40235 if( pagerUseWal(pPager) ){
40236 /* Try to pull the page from the write-ahead log. */
40237 rc = sqlite3WalRead(pPager->pWal, pgno, &isInWal, pgsz, pPg->pData);
40238 }
40239 if( rc==SQLITE_OK && !isInWal ){
40240 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
40241 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
40242 if( rc==SQLITE_IOERR_SHORT_READ ){
40243 rc = SQLITE_OK;
40244 }
@@ -40313,16 +41134,21 @@
40313 static int pagerUndoCallback(void *pCtx, Pgno iPg){
40314 int rc = SQLITE_OK;
40315 Pager *pPager = (Pager *)pCtx;
40316 PgHdr *pPg;
40317
 
40318 pPg = sqlite3PagerLookup(pPager, iPg);
40319 if( pPg ){
40320 if( sqlite3PcachePageRefcount(pPg)==1 ){
40321 sqlite3PcacheDrop(pPg);
40322 }else{
40323 rc = readDbPage(pPg);
 
 
 
 
40324 if( rc==SQLITE_OK ){
40325 pPager->xReiniter(pPg);
40326 }
40327 sqlite3PagerUnref(pPg);
40328 }
@@ -40462,10 +41288,11 @@
40462 sqlite3WalEndReadTransaction(pPager->pWal);
40463
40464 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
40465 if( rc!=SQLITE_OK || changed ){
40466 pager_reset(pPager);
 
40467 }
40468
40469 return rc;
40470 }
40471 #endif
@@ -40722,10 +41549,33 @@
40722 ** Change the maximum number of in-memory pages that are allowed.
40723 */
40724 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
40725 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
40726 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40727
40728 /*
40729 ** Free as much memory as possible from the pager.
40730 */
40731 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
@@ -40958,10 +41808,11 @@
40958 if( rc==SQLITE_OK ){
40959 if( nReserve<0 ) nReserve = pPager->nReserve;
40960 assert( nReserve>=0 && nReserve<1000 );
40961 pPager->nReserve = (i16)nReserve;
40962 pagerReportSize(pPager);
 
40963 }
40964 return rc;
40965 }
40966
40967 /*
@@ -41182,10 +42033,85 @@
41182 if( rc==SQLITE_OK ){
41183 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
41184 }
41185 return rc;
41186 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41187
41188 /*
41189 ** Shutdown the page cache. Free all memory and close all files.
41190 **
41191 ** If a transaction was in progress when this routine is called, that
@@ -41203,10 +42129,11 @@
41203 u8 *pTmp = (u8 *)pPager->pTmpSpace;
41204
41205 assert( assert_pager_state(pPager) );
41206 disable_simulated_io_errors();
41207 sqlite3BeginBenignMalloc();
 
41208 /* pPager->errCode = 0; */
41209 pPager->exclusiveMode = 0;
41210 #ifndef SQLITE_OMIT_WAL
41211 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
41212 pPager->pWal = 0;
@@ -41464,11 +42391,13 @@
41464
41465 /* Before the first write, give the VFS a hint of what the final
41466 ** file size will be.
41467 */
41468 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
41469 if( rc==SQLITE_OK && pPager->dbSize>pPager->dbHintSize ){
 
 
41470 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
41471 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
41472 pPager->dbHintSize = pPager->dbSize;
41473 }
41474
@@ -42018,10 +42947,11 @@
42018 }
42019 /* pPager->xBusyHandler = 0; */
42020 /* pPager->pBusyHandlerArg = 0; */
42021 pPager->xReiniter = xReinit;
42022 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
 
42023
42024 *ppPager = pPager;
42025 return SQLITE_OK;
42026 }
42027
@@ -42309,13 +43239,15 @@
42309 assert( (pPager->eLock==SHARED_LOCK)
42310 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
42311 );
42312 }
42313
42314 if( !pPager->tempFile
42315 && (pPager->pBackup || sqlite3PcachePagecount(pPager->pPCache)>0)
42316 ){
 
 
42317 /* The shared-lock has just been acquired on the database file
42318 ** and there are already pages in the cache (from a previous
42319 ** read or write transaction). Check to see if the database
42320 ** has been modified. If the database has changed, flush the
42321 ** cache.
@@ -42337,19 +43269,29 @@
42337 if( rc ) goto failed;
42338
42339 if( nPage>0 ){
42340 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
42341 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
42342 if( rc!=SQLITE_OK ){
42343 goto failed;
42344 }
42345 }else{
42346 memset(dbFileVers, 0, sizeof(dbFileVers));
42347 }
42348
42349 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
42350 pager_reset(pPager);
 
 
 
 
 
 
 
 
 
 
42351 }
42352 }
42353
42354 /* If there is a WAL file in the file-system, open this database in WAL
42355 ** mode. Otherwise, the following function call is a no-op.
@@ -42387,11 +43329,11 @@
42387 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
42388 ** the rollback journal, the unlock is not performed and there is
42389 ** nothing to rollback, so this routine is a no-op.
42390 */
42391 static void pagerUnlockIfUnused(Pager *pPager){
42392 if( (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
42393 pagerUnlockAndRollback(pPager);
42394 }
42395 }
42396
42397 /*
@@ -42446,17 +43388,31 @@
42446 */
42447 SQLITE_PRIVATE int sqlite3PagerAcquire(
42448 Pager *pPager, /* The pager open on the database file */
42449 Pgno pgno, /* Page number to fetch */
42450 DbPage **ppPage, /* Write a pointer to the page here */
42451 int noContent /* Do not bother reading content from disk if true */
42452 ){
42453 int rc;
42454 PgHdr *pPg;
 
 
 
 
 
 
 
 
 
 
 
 
 
42455
42456 assert( pPager->eState>=PAGER_READER );
42457 assert( assert_pager_state(pPager) );
 
42458
42459 if( pgno==0 ){
42460 return SQLITE_CORRUPT_BKPT;
42461 }
42462
@@ -42463,10 +43419,43 @@
42463 /* If the pager is in the error state, return an error immediately.
42464 ** Otherwise, request the page from the PCache layer. */
42465 if( pPager->errCode!=SQLITE_OK ){
42466 rc = pPager->errCode;
42467 }else{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42468 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
42469 }
42470
42471 if( rc!=SQLITE_OK ){
42472 /* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -42521,13 +43510,17 @@
42521 sqlite3EndBenignMalloc();
42522 }
42523 memset(pPg->pData, 0, pPager->pageSize);
42524 IOTRACE(("ZERO %p %d\n", pPager, pgno));
42525 }else{
 
 
 
 
42526 assert( pPg->pPager==pPager );
42527 pPager->aStat[PAGER_STAT_MISS]++;
42528 rc = readDbPage(pPg);
42529 if( rc!=SQLITE_OK ){
42530 goto pager_acquire_err;
42531 }
42532 }
42533 pager_set_pagehash(pPg);
@@ -42576,11 +43569,15 @@
42576 ** removed.
42577 */
42578 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
42579 if( pPg ){
42580 Pager *pPager = pPg->pPager;
42581 sqlite3PcacheRelease(pPg);
 
 
 
 
42582 pagerUnlockIfUnused(pPager);
42583 }
42584 }
42585
42586 /*
@@ -42911,10 +43908,11 @@
42911
42912 PgHdr *pPg = pDbPage;
42913 Pager *pPager = pPg->pPager;
42914 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
42915
 
42916 assert( pPager->eState>=PAGER_WRITER_LOCKED );
42917 assert( pPager->eState!=PAGER_ERROR );
42918 assert( assert_pager_state(pPager) );
42919
42920 if( nPagePerSector>1 ){
@@ -43467,11 +44465,11 @@
43467 }else{
43468 rc = pager_playback(pPager, 0);
43469 }
43470
43471 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
43472 assert( rc==SQLITE_OK || rc==SQLITE_FULL
43473 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
43474
43475 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
43476 ** cache. So call pager_error() on the way out to make any error persistent.
43477 */
@@ -44201,15 +45199,16 @@
44201
44202 /* Open the connection to the log file. If this operation fails,
44203 ** (e.g. due to malloc() failure), return an error code.
44204 */
44205 if( rc==SQLITE_OK ){
44206 rc = sqlite3WalOpen(pPager->pVfs,
44207 pPager->fd, pPager->zWal, pPager->exclusiveMode,
44208 pPager->journalSizeLimit, &pPager->pWal
44209 );
44210 }
 
44211
44212 return rc;
44213 }
44214
44215
@@ -44296,10 +45295,11 @@
44296 rc = pagerExclusiveLock(pPager);
44297 if( rc==SQLITE_OK ){
44298 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
44299 pPager->pageSize, (u8*)pPager->pTmpSpace);
44300 pPager->pWal = 0;
 
44301 }
44302 }
44303 return rc;
44304 }
44305
@@ -45544,12 +46544,13 @@
45544 ** event via sqlite3_log(). This is to help with identifying performance
45545 ** problems caused by applications routinely shutting down without
45546 ** checkpointing the log file.
45547 */
45548 if( pWal->hdr.nPage ){
45549 sqlite3_log(SQLITE_OK, "Recovered %d frames from WAL file %s",
45550 pWal->hdr.nPage, pWal->zWalName
 
45551 );
45552 }
45553 }
45554
45555 recovery_error:
@@ -46059,20 +47060,21 @@
46059 /* Sync the WAL to disk */
46060 if( sync_flags ){
46061 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
46062 }
46063
46064 /* If the database file may grow as a result of this checkpoint, hint
46065 ** about the eventual size of the db file to the VFS layer.
46066 */
46067 if( rc==SQLITE_OK ){
46068 i64 nReq = ((i64)mxPage * szPage);
46069 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
46070 if( rc==SQLITE_OK && nSize<nReq ){
46071 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
46072 }
46073 }
 
46074
46075 /* Iterate through the contents of the WAL, copying data to the db file. */
46076 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
46077 i64 iOffset;
46078 assert( walFramePgno(pWal, iFrame)==iDbpage );
@@ -46624,23 +47626,21 @@
46624 pWal->readLock = -1;
46625 }
46626 }
46627
46628 /*
46629 ** Read a page from the WAL, if it is present in the WAL and if the
46630 ** current read transaction is configured to use the WAL.
 
46631 **
46632 ** The *pInWal is set to 1 if the requested page is in the WAL and
46633 ** has been loaded. Or *pInWal is set to 0 if the page was not in
46634 ** the WAL and needs to be read out of the database.
46635 */
46636 SQLITE_PRIVATE int sqlite3WalRead(
46637 Wal *pWal, /* WAL handle */
46638 Pgno pgno, /* Database page number to read data for */
46639 int *pInWal, /* OUT: True if data is read from WAL */
46640 int nOut, /* Size of buffer pOut in bytes */
46641 u8 *pOut /* Buffer to write page data to */
46642 ){
46643 u32 iRead = 0; /* If !=0, WAL frame to return data from */
46644 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
46645 int iHash; /* Used to loop through N hash tables */
46646
@@ -46652,11 +47652,11 @@
46652 ** in this case as an optimization. Likewise, if pWal->readLock==0,
46653 ** then the WAL is ignored by the reader so return early, as if the
46654 ** WAL were empty.
46655 */
46656 if( iLast==0 || pWal->readLock==0 ){
46657 *pInWal = 0;
46658 return SQLITE_OK;
46659 }
46660
46661 /* Search the hash table or tables for an entry matching page number
46662 ** pgno. Each iteration of the following for() loop searches one
@@ -46723,30 +47723,35 @@
46723 }
46724 assert( iRead==iRead2 );
46725 }
46726 #endif
46727
46728 /* If iRead is non-zero, then it is the log frame number that contains the
46729 ** required page. Read and return data from the log file.
46730 */
46731 if( iRead ){
46732 int sz;
46733 i64 iOffset;
46734 sz = pWal->hdr.szPage;
46735 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
46736 testcase( sz<=32768 );
46737 testcase( sz>=65536 );
46738 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
46739 *pInWal = 1;
46740 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
46741 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
46742 }
46743
46744 *pInWal = 0;
46745 return SQLITE_OK;
46746 }
46747
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46748
46749 /*
46750 ** Return the size of the database in pages (or zero, if unknown).
46751 */
46752 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
@@ -47289,10 +48294,13 @@
47289 }
47290
47291 /* Read the wal-index header. */
47292 if( rc==SQLITE_OK ){
47293 rc = walIndexReadHdr(pWal, &isChanged);
 
 
 
47294 }
47295
47296 /* Copy data from the log to the database file. */
47297 if( rc==SQLITE_OK ){
47298 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
@@ -49960,17 +50968,21 @@
49960 */
49961 static int btreeGetPage(
49962 BtShared *pBt, /* The btree */
49963 Pgno pgno, /* Number of the page to fetch */
49964 MemPage **ppPage, /* Return the page in this parameter */
49965 int noContent /* Do not load page content if true */
 
49966 ){
49967 int rc;
49968 DbPage *pDbPage;
 
 
49969
 
49970 assert( sqlite3_mutex_held(pBt->mutex) );
49971 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, noContent);
49972 if( rc ) return rc;
49973 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
49974 return SQLITE_OK;
49975 }
49976
@@ -50009,21 +51021,22 @@
50009 **
50010 ** If an error occurs, then the value *ppPage is set to is undefined. It
50011 ** may remain unchanged, or it may be set to an invalid value.
50012 */
50013 static int getAndInitPage(
50014 BtShared *pBt, /* The database file */
50015 Pgno pgno, /* Number of the page to get */
50016 MemPage **ppPage /* Write the page pointer here */
 
50017 ){
50018 int rc;
50019 assert( sqlite3_mutex_held(pBt->mutex) );
50020
50021 if( pgno>btreePagecount(pBt) ){
50022 rc = SQLITE_CORRUPT_BKPT;
50023 }else{
50024 rc = btreeGetPage(pBt, pgno, ppPage, 0);
50025 if( rc==SQLITE_OK ){
50026 rc = btreeInitPage(*ppPage);
50027 if( rc!=SQLITE_OK ){
50028 releasePage(*ppPage);
50029 }
@@ -50250,10 +51263,11 @@
50250 goto btree_open_out;
50251 }
50252 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
50253 EXTRA_SIZE, flags, vfsFlags, pageReinit);
50254 if( rc==SQLITE_OK ){
 
50255 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
50256 }
50257 if( rc!=SQLITE_OK ){
50258 goto btree_open_out;
50259 }
@@ -50515,10 +51529,23 @@
50515 sqlite3BtreeEnter(p);
50516 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
50517 sqlite3BtreeLeave(p);
50518 return SQLITE_OK;
50519 }
 
 
 
 
 
 
 
 
 
 
 
 
 
50520
50521 /*
50522 ** Change the way data is synced to disk in order to increase or decrease
50523 ** how well the database resists damage due to OS crashes and power
50524 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
@@ -50741,11 +51768,11 @@
50741
50742 assert( sqlite3_mutex_held(pBt->mutex) );
50743 assert( pBt->pPage1==0 );
50744 rc = sqlite3PagerSharedLock(pBt->pPager);
50745 if( rc!=SQLITE_OK ) return rc;
50746 rc = btreeGetPage(pBt, 1, &pPage1, 0);
50747 if( rc!=SQLITE_OK ) return rc;
50748
50749 /* Do some checking to help insure the file we opened really is
50750 ** a valid database file.
50751 */
@@ -51300,11 +52327,11 @@
51300 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
51301 ** that it points at iFreePage. Also fix the pointer map entry for
51302 ** iPtrPage.
51303 */
51304 if( eType!=PTRMAP_ROOTPAGE ){
51305 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
51306 if( rc!=SQLITE_OK ){
51307 return rc;
51308 }
51309 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
51310 if( rc!=SQLITE_OK ){
@@ -51384,11 +52411,11 @@
51384 Pgno iFreePg; /* Index of free page to move pLastPg to */
51385 MemPage *pLastPg;
51386 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
51387 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
51388
51389 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
51390 if( rc!=SQLITE_OK ){
51391 return rc;
51392 }
51393
51394 /* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -51476,12 +52503,15 @@
51476 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
51477
51478 if( nOrig<nFin ){
51479 rc = SQLITE_CORRUPT_BKPT;
51480 }else if( nFree>0 ){
51481 invalidateAllOverflowCache(pBt);
51482 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
 
 
 
51483 if( rc==SQLITE_OK ){
51484 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
51485 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
51486 }
51487 }else{
@@ -51525,11 +52555,13 @@
51525 }
51526
51527 nFree = get4byte(&pBt->pPage1->aData[36]);
51528 nFin = finalDbSize(pBt, nOrig, nFree);
51529 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
51530
 
 
51531 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
51532 rc = incrVacuumStep(pBt, nFin, iFree, 1);
51533 }
51534 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
51535 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
@@ -51542,11 +52574,11 @@
51542 if( rc!=SQLITE_OK ){
51543 sqlite3PagerRollback(pPager);
51544 }
51545 }
51546
51547 assert( nRef==sqlite3PagerRefcount(pPager) );
51548 return rc;
51549 }
51550
51551 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
51552 # define setChildPtrmaps(x) SQLITE_OK
@@ -51798,11 +52830,11 @@
51798 }
51799
51800 /* The rollback may have destroyed the pPage1->aData value. So
51801 ** call btreeGetPage() on page 1 again to make
51802 ** sure pPage1->aData is set correctly. */
51803 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
51804 int nPage = get4byte(28+(u8*)pPage1->aData);
51805 testcase( nPage==0 );
51806 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
51807 testcase( pBt->nPage!=nPage );
51808 pBt->nPage = nPage;
@@ -52232,11 +53264,11 @@
52232 }
52233 #endif
52234
52235 assert( next==0 || rc==SQLITE_DONE );
52236 if( rc==SQLITE_OK ){
52237 rc = btreeGetPage(pBt, ovfl, &pPage, 0);
52238 assert( rc==SQLITE_OK || pPage==0 );
52239 if( rc==SQLITE_OK ){
52240 next = get4byte(pPage->aData);
52241 }
52242 }
@@ -52453,11 +53485,13 @@
52453 }else
52454 #endif
52455
52456 {
52457 DbPage *pDbPage;
52458 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage);
 
 
52459 if( rc==SQLITE_OK ){
52460 aPayload = sqlite3PagerGetData(pDbPage);
52461 nextPage = get4byte(aPayload);
52462 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
52463 sqlite3PagerUnref(pDbPage);
@@ -52632,14 +53666,15 @@
52632 BtShared *pBt = pCur->pBt;
52633
52634 assert( cursorHoldsMutex(pCur) );
52635 assert( pCur->eState==CURSOR_VALID );
52636 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
 
52637 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
52638 return SQLITE_CORRUPT_BKPT;
52639 }
52640 rc = getAndInitPage(pBt, newPgno, &pNewPage);
52641 if( rc ) return rc;
52642 pCur->apPage[i+1] = pNewPage;
52643 pCur->aiIdx[i+1] = 0;
52644 pCur->iPage++;
52645
@@ -52752,11 +53787,11 @@
52752 pCur->iPage = 0;
52753 }else if( pCur->pgnoRoot==0 ){
52754 pCur->eState = CURSOR_INVALID;
52755 return SQLITE_OK;
52756 }else{
52757 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0]);
52758 if( rc!=SQLITE_OK ){
52759 pCur->eState = CURSOR_INVALID;
52760 return rc;
52761 }
52762 pCur->iPage = 0;
@@ -53366,11 +54401,11 @@
53366 }
53367 testcase( iTrunk==mxPage );
53368 if( iTrunk>mxPage ){
53369 rc = SQLITE_CORRUPT_BKPT;
53370 }else{
53371 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
53372 }
53373 if( rc ){
53374 pTrunk = 0;
53375 goto end_allocate_page;
53376 }
@@ -53430,11 +54465,11 @@
53430 if( iNewTrunk>mxPage ){
53431 rc = SQLITE_CORRUPT_BKPT;
53432 goto end_allocate_page;
53433 }
53434 testcase( iNewTrunk==mxPage );
53435 rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
53436 if( rc!=SQLITE_OK ){
53437 goto end_allocate_page;
53438 }
53439 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
53440 if( rc!=SQLITE_OK ){
@@ -53510,11 +54545,11 @@
53510 if( closest<k-1 ){
53511 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
53512 }
53513 put4byte(&aData[4], k-1);
53514 noContent = !btreeGetHasContent(pBt, *pPgno);
53515 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
53516 if( rc==SQLITE_OK ){
53517 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
53518 if( rc!=SQLITE_OK ){
53519 releasePage(*ppPage);
53520 }
@@ -53558,11 +54593,11 @@
53558 ** becomes a new pointer-map page, the second is used by the caller.
53559 */
53560 MemPage *pPg = 0;
53561 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
53562 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
53563 rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
53564 if( rc==SQLITE_OK ){
53565 rc = sqlite3PagerWrite(pPg->pDbPage);
53566 releasePage(pPg);
53567 }
53568 if( rc ) return rc;
@@ -53572,11 +54607,11 @@
53572 #endif
53573 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
53574 *pPgno = pBt->nPage;
53575
53576 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
53577 rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
53578 if( rc ) return rc;
53579 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
53580 if( rc!=SQLITE_OK ){
53581 releasePage(*ppPage);
53582 }
@@ -53640,11 +54675,11 @@
53640
53641 if( pBt->btsFlags & BTS_SECURE_DELETE ){
53642 /* If the secure_delete option is enabled, then
53643 ** always fully overwrite deleted information with zeros.
53644 */
53645 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
53646 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
53647 ){
53648 goto freepage_out;
53649 }
53650 memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -53667,11 +54702,11 @@
53667 */
53668 if( nFree!=0 ){
53669 u32 nLeaf; /* Initial number of leaf cells on trunk page */
53670
53671 iTrunk = get4byte(&pPage1->aData[32]);
53672 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
53673 if( rc!=SQLITE_OK ){
53674 goto freepage_out;
53675 }
53676
53677 nLeaf = get4byte(&pTrunk->aData[4]);
@@ -53713,11 +54748,11 @@
53713 ** the page being freed as a leaf page of the first trunk in the free-list.
53714 ** Possibly because the free-list is empty, or possibly because the
53715 ** first trunk in the free-list is full. Either way, the page being freed
53716 ** will become the new first trunk page in the free-list.
53717 */
53718 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
53719 goto freepage_out;
53720 }
53721 rc = sqlite3PagerWrite(pPage->pDbPage);
53722 if( rc!=SQLITE_OK ){
53723 goto freepage_out;
@@ -54514,11 +55549,11 @@
54514 }else{
54515 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
54516 }
54517 pgno = get4byte(pRight);
54518 while( 1 ){
54519 rc = getAndInitPage(pBt, pgno, &apOld[i]);
54520 if( rc ){
54521 memset(apOld, 0, (i+1)*sizeof(MemPage*));
54522 goto balance_cleanup;
54523 }
54524 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -55602,14 +56637,21 @@
55602 ** is already journaled.
55603 */
55604 u8 eType = 0;
55605 Pgno iPtrPage = 0;
55606
 
 
 
 
55607 releasePage(pPageMove);
 
 
 
55608
55609 /* Move the page currently at pgnoRoot to pgnoMove. */
55610 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
55611 if( rc!=SQLITE_OK ){
55612 return rc;
55613 }
55614 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
55615 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -55626,11 +56668,11 @@
55626
55627 /* Obtain the page at pgnoRoot */
55628 if( rc!=SQLITE_OK ){
55629 return rc;
55630 }
55631 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
55632 if( rc!=SQLITE_OK ){
55633 return rc;
55634 }
55635 rc = sqlite3PagerWrite(pRoot->pDbPage);
55636 if( rc!=SQLITE_OK ){
@@ -55702,11 +56744,11 @@
55702 assert( sqlite3_mutex_held(pBt->mutex) );
55703 if( pgno>btreePagecount(pBt) ){
55704 return SQLITE_CORRUPT_BKPT;
55705 }
55706
55707 rc = getAndInitPage(pBt, pgno, &pPage);
55708 if( rc ) return rc;
55709 for(i=0; i<pPage->nCell; i++){
55710 pCell = findCell(pPage, i);
55711 if( !pPage->leaf ){
55712 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
@@ -55804,11 +56846,11 @@
55804 if( NEVER(pBt->pCursor) ){
55805 sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
55806 return SQLITE_LOCKED_SHAREDCACHE;
55807 }
55808
55809 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
55810 if( rc ) return rc;
55811 rc = sqlite3BtreeClearTable(p, iTable, 0);
55812 if( rc ){
55813 releasePage(pPage);
55814 return rc;
@@ -55839,21 +56881,21 @@
55839 ** number in the database. So move the page that does into the
55840 ** gap left by the deleted root-page.
55841 */
55842 MemPage *pMove;
55843 releasePage(pPage);
55844 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
55845 if( rc!=SQLITE_OK ){
55846 return rc;
55847 }
55848 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
55849 releasePage(pMove);
55850 if( rc!=SQLITE_OK ){
55851 return rc;
55852 }
55853 pMove = 0;
55854 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
55855 freePage(pMove, &rc);
55856 releasePage(pMove);
55857 if( rc!=SQLITE_OK ){
55858 return rc;
55859 }
@@ -56261,11 +57303,11 @@
56261 */
56262 pBt = pCheck->pBt;
56263 usableSize = pBt->usableSize;
56264 if( iPage==0 ) return 0;
56265 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
56266 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
56267 checkAppendMsg(pCheck, zContext,
56268 "unable to get the page. error code=%d", rc);
56269 return 0;
56270 }
56271
@@ -56732,10 +57774,21 @@
56732 }
56733 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
56734 if( pCsr->eState!=CURSOR_VALID ){
56735 return SQLITE_ABORT;
56736 }
 
 
 
 
 
 
 
 
 
 
 
56737
56738 /* Check some assumptions:
56739 ** (a) the cursor is open for writing,
56740 ** (b) there is a read/write transaction open,
56741 ** (c) the connection holds a write-lock on the table (if required),
@@ -57214,11 +58267,12 @@
57214 assert( nSrcPage>=0 );
57215 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
57216 const Pgno iSrcPg = p->iNext; /* Source page number */
57217 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
57218 DbPage *pSrcPg; /* Source page object */
57219 rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
 
57220 if( rc==SQLITE_OK ){
57221 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
57222 sqlite3PagerUnref(pSrcPg);
57223 }
57224 }
@@ -62437,18 +63491,10 @@
62437 rc = sqlite3VdbeTransferError(p);
62438 }
62439 return (rc&db->errMask);
62440 }
62441
62442 /*
62443 ** The maximum number of times that a statement will try to reparse
62444 ** itself before giving up and returning SQLITE_SCHEMA.
62445 */
62446 #ifndef SQLITE_MAX_SCHEMA_RETRY
62447 # define SQLITE_MAX_SCHEMA_RETRY 5
62448 #endif
62449
62450 /*
62451 ** This is the top-level implementation of sqlite3_step(). Call
62452 ** sqlite3Step() to do most of the work. If a schema error occurs,
62453 ** call sqlite3Reprepare() and try again.
62454 */
@@ -63347,10 +64393,15 @@
63347 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
63348 ** string contains a copy of zRawSql but with host parameters expanded to
63349 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
63350 ** then the returned string holds a copy of zRawSql with "-- " prepended
63351 ** to each line of text.
 
 
 
 
 
63352 **
63353 ** The calling function is responsible for making sure the memory returned
63354 ** is eventually freed.
63355 **
63356 ** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -63418,34 +64469,53 @@
63418 }else if( pVar->flags & MEM_Int ){
63419 sqlite3XPrintf(&out, "%lld", pVar->u.i);
63420 }else if( pVar->flags & MEM_Real ){
63421 sqlite3XPrintf(&out, "%!.15g", pVar->r);
63422 }else if( pVar->flags & MEM_Str ){
 
63423 #ifndef SQLITE_OMIT_UTF16
63424 u8 enc = ENC(db);
 
63425 if( enc!=SQLITE_UTF8 ){
63426 Mem utf8;
63427 memset(&utf8, 0, sizeof(utf8));
63428 utf8.db = db;
63429 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
63430 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
63431 sqlite3XPrintf(&out, "'%.*q'", utf8.n, utf8.z);
63432 sqlite3VdbeMemRelease(&utf8);
63433 }else
63434 #endif
63435 {
63436 sqlite3XPrintf(&out, "'%.*q'", pVar->n, pVar->z);
 
 
 
63437 }
 
 
 
 
 
 
 
 
63438 }else if( pVar->flags & MEM_Zero ){
63439 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
63440 }else{
 
63441 assert( pVar->flags & MEM_Blob );
63442 sqlite3StrAccumAppend(&out, "x'", 2);
63443 for(i=0; i<pVar->n; i++){
 
 
 
 
63444 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
63445 }
63446 sqlite3StrAccumAppend(&out, "'", 1);
 
 
 
63447 }
63448 }
63449 }
63450 return sqlite3StrAccumFinish(&out);
63451 }
@@ -67658,11 +68728,11 @@
67658 ** u.bc.r.flags = UNPACKED_INCRKEY;
67659 ** }else{
67660 ** u.bc.r.flags = 0;
67661 ** }
67662 */
67663 u.bc.r.flags = (u16)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
67664 assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
67665 assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
67666 assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
67667 assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
67668
@@ -70783,11 +71853,11 @@
70783 if( db->mallocFailed ){
70784 goto blob_open_out;
70785 }
70786 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
70787 rc = blobSeekToRow(pBlob, iRow, &zErr);
70788 } while( (++nAttempt)<5 && rc==SQLITE_SCHEMA );
70789
70790 blob_open_out:
70791 if( rc==SQLITE_OK && db->mallocFailed==0 ){
70792 *ppBlob = (sqlite3_blob *)pBlob;
70793 }else{
@@ -72468,11 +73538,13 @@
72468 0, /* xSectorSize */
72469 0, /* xDeviceCharacteristics */
72470 0, /* xShmMap */
72471 0, /* xShmLock */
72472 0, /* xShmBarrier */
72473 0 /* xShmUnlock */
 
 
72474 };
72475
72476 /*
72477 ** Open a journal file.
72478 */
@@ -72612,11 +73684,13 @@
72612 }
72613
72614 /*
72615 ** Call sqlite3WalkExpr() for every expression in Select statement p.
72616 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
72617 ** on the compound select chain, p->pPrior.
 
 
72618 **
72619 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
72620 ** there is an abort request.
72621 **
72622 ** If the Walker does not have an xSelectCallback() then this routine
@@ -72626,17 +73700,26 @@
72626 int rc;
72627 if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
72628 rc = WRC_Continue;
72629 pWalker->walkerDepth++;
72630 while( p ){
72631 rc = pWalker->xSelectCallback(pWalker, p);
72632 if( rc ) break;
 
 
72633 if( sqlite3WalkSelectExpr(pWalker, p)
72634 || sqlite3WalkSelectFrom(pWalker, p)
72635 ){
72636 pWalker->walkerDepth--;
72637 return WRC_Abort;
 
 
 
 
 
 
 
72638 }
72639 p = p->pPrior;
72640 }
72641 pWalker->walkerDepth--;
72642 return rc & WRC_Abort;
@@ -73031,11 +74114,14 @@
73031 ** In cases like this, replace pExpr with a copy of the expression that
73032 ** forms the result set entry ("a+b" in the example) and return immediately.
73033 ** Note that the expression in the result set should have already been
73034 ** resolved by the time the WHERE clause is resolved.
73035 */
73036 if( cnt==0 && (pEList = pNC->pEList)!=0 && zTab==0 ){
 
 
 
73037 for(j=0; j<pEList->nExpr; j++){
73038 char *zAs = pEList->a[j].zName;
73039 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
73040 Expr *pOrig;
73041 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
@@ -73122,11 +74208,13 @@
73122 pExpr->pRight = 0;
73123 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
73124 lookupname_end:
73125 if( cnt==1 ){
73126 assert( pNC!=0 );
73127 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
 
 
73128 /* Increment the nRef value on all name contexts from TopNC up to
73129 ** the point where the name matched. */
73130 for(;;){
73131 assert( pTopNC!=0 );
73132 pTopNC->nRef++;
@@ -73797,15 +74885,14 @@
73797 **
73798 ** Minor point: If this is the case, then the expression will be
73799 ** re-evaluated for each reference to it.
73800 */
73801 sNC.pEList = p->pEList;
73802 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ||
73803 sqlite3ResolveExprNames(&sNC, p->pHaving)
73804 ){
73805 return WRC_Abort;
73806 }
73807
73808 /* The ORDER BY and GROUP BY clauses may not refer to terms in
73809 ** outer queries
73810 */
73811 sNC.pNext = 0;
@@ -73922,10 +75009,11 @@
73922 pParse->nHeight += pExpr->nHeight;
73923 }
73924 #endif
73925 savedHasAgg = pNC->ncFlags & NC_HasAgg;
73926 pNC->ncFlags &= ~NC_HasAgg;
 
73927 w.xExprCallback = resolveExprStep;
73928 w.xSelectCallback = resolveSelectStep;
73929 w.pParse = pNC->pParse;
73930 w.u.pNC = pNC;
73931 sqlite3WalkExpr(&w, pExpr);
@@ -73962,10 +75050,11 @@
73962 NameContext *pOuterNC /* Name context for parent SELECT statement */
73963 ){
73964 Walker w;
73965
73966 assert( p!=0 );
 
73967 w.xExprCallback = resolveExprStep;
73968 w.xSelectCallback = resolveSelectStep;
73969 w.pParse = pParse;
73970 w.u.pNC = pOuterNC;
73971 sqlite3WalkSelect(&w, p);
@@ -75186,10 +76275,11 @@
75186 pWalker->u.i = 0;
75187 return WRC_Abort;
75188 }
75189 static int exprIsConst(Expr *p, int initFlag){
75190 Walker w;
 
75191 w.u.i = initFlag;
75192 w.xExprCallback = exprNodeIsConstant;
75193 w.xSelectCallback = selectNodeIsConstant;
75194 sqlite3WalkExpr(&w, p);
75195 return w.u.i;
@@ -77400,12 +78490,12 @@
77400 */
77401 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
77402 Walker w;
77403 if( pParse->cookieGoto ) return;
77404 if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
 
77405 w.xExprCallback = evalConstExpr;
77406 w.xSelectCallback = 0;
77407 w.pParse = pParse;
77408 sqlite3WalkExpr(&w, pExpr);
77409 }
77410
77411
@@ -86603,10 +87693,17 @@
86603 prevEscape = 0;
86604 }
86605 }
86606 return *zString==0;
86607 }
 
 
 
 
 
 
 
86608
86609 /*
86610 ** Count the number of times that the LIKE operator (or GLOB which is
86611 ** just a variation of LIKE) gets called. This is used for testing
86612 ** only.
@@ -90804,24 +91901,23 @@
90804 ){
90805 int rc = SQLITE_OK; /* Return code */
90806 const char *zLeftover; /* Tail of unprocessed SQL */
90807 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
90808 char **azCols = 0; /* Names of result columns */
90809 int nRetry = 0; /* Number of retry attempts */
90810 int callbackIsInit; /* True if callback data is initialized */
90811
90812 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
90813 if( zSql==0 ) zSql = "";
90814
90815 sqlite3_mutex_enter(db->mutex);
90816 sqlite3Error(db, SQLITE_OK, 0);
90817 while( (rc==SQLITE_OK || (rc==SQLITE_SCHEMA && (++nRetry)<2)) && zSql[0] ){
90818 int nCol;
90819 char **azVals = 0;
90820
90821 pStmt = 0;
90822 rc = sqlite3_prepare(db, zSql, -1, &pStmt, &zLeftover);
90823 assert( rc==SQLITE_OK || pStmt==0 );
90824 if( rc!=SQLITE_OK ){
90825 continue;
90826 }
90827 if( !pStmt ){
@@ -90874,15 +91970,12 @@
90874 }
90875
90876 if( rc!=SQLITE_ROW ){
90877 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
90878 pStmt = 0;
90879 if( rc!=SQLITE_SCHEMA ){
90880 nRetry = 0;
90881 zSql = zLeftover;
90882 while( sqlite3Isspace(zSql[0]) ) zSql++;
90883 }
90884 break;
90885 }
90886 }
90887
90888 sqlite3DbFree(db, azCols);
@@ -91402,12 +92495,21 @@
91402 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
91403 #define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
91404 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
91405 #endif /* SQLITE_CORE */
91406
91407 #define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api = 0;
91408 #define SQLITE_EXTENSION_INIT2(v) sqlite3_api = v;
 
 
 
 
 
 
 
 
 
91409
91410 #endif /* _SQLITE3EXT_H_ */
91411
91412 /************** End of sqlite3ext.h ******************************************/
91413 /************** Continuing where we left off in loadext.c ********************/
@@ -91806,12 +92908,27 @@
91806 ){
91807 sqlite3_vfs *pVfs = db->pVfs;
91808 void *handle;
91809 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
91810 char *zErrmsg = 0;
 
 
91811 void **aHandle;
91812 int nMsg = 300 + sqlite3Strlen30(zFile);
 
 
 
 
 
 
 
 
 
 
 
 
 
91813
91814 if( pzErrMsg ) *pzErrMsg = 0;
91815
91816 /* Ticket #1863. To avoid a creating security problems for older
91817 ** applications that relink against newer versions of SQLite, the
@@ -91824,15 +92941,21 @@
91824 *pzErrMsg = sqlite3_mprintf("not authorized");
91825 }
91826 return SQLITE_ERROR;
91827 }
91828
91829 if( zProc==0 ){
91830 zProc = "sqlite3_extension_init";
91831 }
91832
91833 handle = sqlite3OsDlOpen(pVfs, zFile);
 
 
 
 
 
 
 
 
91834 if( handle==0 ){
91835 if( pzErrMsg ){
91836 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
91837 if( zErrmsg ){
91838 sqlite3_snprintf(nMsg, zErrmsg,
@@ -91841,24 +92964,61 @@
91841 }
91842 }
91843 return SQLITE_ERROR;
91844 }
91845 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
91846 sqlite3OsDlSym(pVfs, handle, zProc);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91847 if( xInit==0 ){
91848 if( pzErrMsg ){
91849 nMsg += sqlite3Strlen30(zProc);
91850 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
91851 if( zErrmsg ){
91852 sqlite3_snprintf(nMsg, zErrmsg,
91853 "no entry point [%s] in shared library [%s]", zProc,zFile);
91854 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
91855 }
91856 sqlite3OsDlClose(pVfs, handle);
91857 }
 
 
91858 return SQLITE_ERROR;
91859 }else if( xInit(db, &zErrmsg, &sqlite3Apis) ){
 
 
91860 if( pzErrMsg ){
91861 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
91862 }
91863 sqlite3_free(zErrmsg);
91864 sqlite3OsDlClose(pVfs, handle);
@@ -92383,11 +93543,11 @@
92383 int iDb; /* Database index for <database> */
92384 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
92385 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
92386 sqlite3 *db = pParse->db; /* The database connection */
92387 Db *pDb; /* The specific database being pragmaed */
92388 Vdbe *v = pParse->pVdbe = sqlite3VdbeCreate(db); /* Prepared statement */
92389
92390 if( v==0 ) return;
92391 sqlite3VdbeRunOnlyOnce(v);
92392 pParse->nMem = 2;
92393
@@ -92466,15 +93626,16 @@
92466 */
92467 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
92468 static const VdbeOpList getCacheSize[] = {
92469 { OP_Transaction, 0, 0, 0}, /* 0 */
92470 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
92471 { OP_IfPos, 1, 7, 0},
92472 { OP_Integer, 0, 2, 0},
92473 { OP_Subtract, 1, 2, 1},
92474 { OP_IfPos, 1, 7, 0},
92475 { OP_Integer, 0, 1, 0}, /* 6 */
 
92476 { OP_ResultRow, 1, 1, 0},
92477 };
92478 int addr;
92479 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
92480 sqlite3VdbeUsesBtree(v, iDb);
@@ -92808,10 +93969,47 @@
92808 pDb->pSchema->cache_size = size;
92809 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
92810 }
92811 }else
92812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92813 /*
92814 ** PRAGMA temp_store
92815 ** PRAGMA temp_store = "default"|"memory"|"file"
92816 **
92817 ** Return or set the local value of the temp_store flag. Changing
@@ -94498,11 +95696,10 @@
94498 azColName[i], SQLITE_STATIC);
94499 }
94500 }
94501 #endif
94502
94503 assert( db->init.busy==0 || saveSqlFlag==0 );
94504 if( db->init.busy==0 ){
94505 Vdbe *pVdbe = pParse->pVdbe;
94506 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
94507 }
94508 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
@@ -98290,10 +99487,11 @@
98290 ** The calling function can detect the problem by looking at pParse->nErr
98291 ** and/or pParse->db->mallocFailed.
98292 */
98293 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
98294 Walker w;
 
98295 w.xSelectCallback = selectExpander;
98296 w.xExprCallback = exprWalkNoop;
98297 w.pParse = pParse;
98298 sqlite3WalkSelect(&w, pSelect);
98299 }
@@ -98348,13 +99546,15 @@
98348 ** Use this routine after name resolution.
98349 */
98350 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
98351 #ifndef SQLITE_OMIT_SUBQUERY
98352 Walker w;
 
98353 w.xSelectCallback = selectAddSubqueryTypeInfo;
98354 w.xExprCallback = exprWalkNoop;
98355 w.pParse = pParse;
 
98356 sqlite3WalkSelect(&w, pSelect);
98357 #endif
98358 }
98359
98360
@@ -98761,11 +99961,11 @@
98761 pItem->regReturn = ++pParse->nMem;
98762 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
98763 pItem->addrFillSub = topAddr+1;
98764 VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
98765 if( pItem->isCorrelated==0 ){
98766 /* If the subquery is no correlated and if we are not inside of
98767 ** a trigger, then we only need to compute the value of the subquery
98768 ** once. */
98769 onceAddr = sqlite3CodeOnce(pParse);
98770 }
98771 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
@@ -105219,13 +106419,12 @@
105219 Table *pTab = pSrc->pTab;
105220 sqlite3_index_info *pIdxInfo;
105221 struct sqlite3_index_constraint *pIdxCons;
105222 struct sqlite3_index_constraint_usage *pUsage;
105223 WhereTerm *pTerm;
105224 int i, j, k;
105225 int nOrderBy;
105226 int sortOrder; /* Sort order for IN clauses */
105227 int bAllowIN; /* Allow IN optimizations */
105228 double rCost;
105229
105230 /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
105231 ** malloc in allocateIndexInfo() fails and this function returns leaving
@@ -105320,11 +106519,10 @@
105320
105321 if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
105322 return;
105323 }
105324
105325 sortOrder = SQLITE_SO_ASC;
105326 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
105327 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
105328 if( pUsage[i].argvIndex>0 ){
105329 j = pIdxCons->iTermOffset;
105330 pTerm = &pWC->a[j];
@@ -105335,21 +106533,32 @@
105335 ** says that the equivalent EQ constraint cannot be safely omitted.
105336 ** If we do attempt to use such a constraint, some rows might be
105337 ** repeated in the output. */
105338 break;
105339 }
105340 for(k=0; k<pIdxInfo->nOrderBy; k++){
105341 if( pIdxInfo->aOrderBy[k].iColumn==pIdxCons->iColumn ){
105342 sortOrder = pIdxInfo->aOrderBy[k].desc;
105343 break;
105344 }
105345 }
105346 }
105347 }
105348 }
105349 if( i>=pIdxInfo->nConstraint ) break;
105350 }
 
 
 
 
 
 
 
 
 
 
 
105351
105352 /* If there is an ORDER BY clause, and the selected virtual table index
105353 ** does not satisfy it, increase the cost of the scan accordingly. This
105354 ** matches the processing for non-virtual tables in bestBtreeIndex().
105355 */
@@ -105370,12 +106579,11 @@
105370 }else{
105371 p->cost.rCost = rCost;
105372 }
105373 p->cost.plan.u.pVtabIdx = pIdxInfo;
105374 if( pIdxInfo->orderByConsumed ){
105375 assert( sortOrder==0 || sortOrder==1 );
105376 p->cost.plan.wsFlags |= WHERE_ORDERED + sortOrder*WHERE_REVERSE;
105377 p->cost.plan.nOBSat = nOrderBy;
105378 }else{
105379 p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
105380 }
105381 p->cost.plan.nEq = 0;
@@ -107108,10 +108316,11 @@
107108 struct SrcList_item *pTabItem; /* FROM clause term being coded */
107109 int addrBrk; /* Jump here to break out of the loop */
107110 int addrCont; /* Jump here to continue with next cycle */
107111 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
107112 int iReleaseReg = 0; /* Temp register to free before returning */
 
107113
107114 pParse = pWInfo->pParse;
107115 v = pParse->pVdbe;
107116 pWC = pWInfo->pWC;
107117 pLevel = &pWInfo->a[iLevel];
@@ -107118,10 +108327,11 @@
107118 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
107119 iCur = pTabItem->iCursor;
107120 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
107121 omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
107122 && (wctrlFlags & WHERE_FORCE_TABLE)==0;
 
107123
107124 /* Create labels for the "break" and "continue" instructions
107125 ** for the current loop. Jump to addrBrk to break out of a loop.
107126 ** Jump to cont to go immediately to the next iteration of the
107127 ** loop.
@@ -107768,11 +108978,11 @@
107768 pLevel->op = aStep[bRev];
107769 pLevel->p1 = iCur;
107770 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
107771 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
107772 }
107773 notReady &= ~getMask(pWC->pMaskSet, iCur);
107774
107775 /* Insert code to test every subexpression that can be completely
107776 ** computed using the current set of tables.
107777 **
107778 ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
@@ -107782,11 +108992,11 @@
107782 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
107783 Expr *pE;
107784 testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
107785 testcase( pTerm->wtFlags & TERM_CODED );
107786 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
107787 if( (pTerm->prereqAll & notReady)!=0 ){
107788 testcase( pWInfo->untestedTerms==0
107789 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
107790 pWInfo->untestedTerms = 1;
107791 continue;
107792 }
@@ -107796,10 +109006,36 @@
107796 continue;
107797 }
107798 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
107799 pTerm->wtFlags |= TERM_CODED;
107800 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107801
107802 /* For a LEFT OUTER JOIN, generate code that will record the fact that
107803 ** at least one row of the right table has matched the left table.
107804 */
107805 if( pLevel->iLeftJoin ){
@@ -107809,11 +109045,11 @@
107809 sqlite3ExprCacheClear(pParse);
107810 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
107811 testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
107812 testcase( pTerm->wtFlags & TERM_CODED );
107813 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
107814 if( (pTerm->prereqAll & notReady)!=0 ){
107815 assert( pWInfo->untestedTerms );
107816 continue;
107817 }
107818 assert( pTerm->pExpr );
107819 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
@@ -107820,11 +109056,11 @@
107820 pTerm->wtFlags |= TERM_CODED;
107821 }
107822 }
107823 sqlite3ReleaseTempReg(pParse, iReleaseReg);
107824
107825 return notReady;
107826 }
107827
107828 #if defined(SQLITE_TEST)
107829 /*
107830 ** The following variable holds a text description of query plan generated
@@ -113805,10 +115041,23 @@
113805 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
113806 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
113807 break;
113808 }
113809 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
113810
113811 default: {
113812 rc = SQLITE_ERROR;
113813 break;
113814 }
@@ -115626,10 +116875,11 @@
115626
115627 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
115628 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
115629 db->autoCommit = 1;
115630 db->nextAutovac = -1;
 
115631 db->nextPagesize = 0;
115632 db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
115633 #if SQLITE_DEFAULT_FILE_FORMAT<4
115634 | SQLITE_LegacyFileFmt
115635 #endif
@@ -117967,10 +119217,13 @@
117967 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
117968 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
117969 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
117970 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
117971
 
 
 
117972 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
117973 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
117974 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
117975 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
117976 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
@@ -121281,10 +122534,13 @@
121281 #endif
121282
121283 rc = sqlite3Fts3InitAux(db);
121284 if( rc!=SQLITE_OK ) return rc;
121285
 
 
 
121286 sqlite3Fts3SimpleTokenizerModule(&pSimple);
121287 sqlite3Fts3PorterTokenizerModule(&pPorter);
121288
121289 /* Allocate and initialize the hash-table used to store tokenizers. */
121290 pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -123110,21 +124366,30 @@
123110 int rc; /* value returned by declare_vtab() */
123111 Fts3auxTable *p; /* Virtual table object to return */
123112
123113 UNUSED_PARAMETER(pUnused);
123114
123115 /* The user should specify a single argument - the name of an fts3 table. */
123116 if( argc!=4 ){
123117 *pzErr = sqlite3_mprintf(
123118 "wrong number of arguments to fts4aux constructor"
123119 );
123120 return SQLITE_ERROR;
123121 }
123122
123123 zDb = argv[1];
123124 nDb = (int)strlen(zDb);
123125 zFts3 = argv[3];
 
 
 
 
 
 
 
 
 
 
123126 nFts3 = (int)strlen(zFts3);
123127
123128 rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
123129 if( rc!=SQLITE_OK ) return rc;
123130
@@ -123143,10 +124408,14 @@
123143 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
123144 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
123145
123146 *ppVtab = (sqlite3_vtab *)p;
123147 return SQLITE_OK;
 
 
 
 
123148 }
123149
123150 /*
123151 ** This function does the work for both the xDisconnect and xDestroy methods.
123152 ** These tables have no persistent representation of their own, so xDisconnect
@@ -126285,10 +127554,476 @@
126285 }
126286
126287 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
126288
126289 /************** End of fts3_tokenizer1.c *************************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126290 /************** Begin file fts3_write.c **************************************/
126291 /*
126292 ** 2009 Oct 23
126293 **
126294 ** The author disclaims copyright to this source code. In place of
126295
--- 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.17. 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.
@@ -360,15 +360,15 @@
360 **
361 ** Older versions of SQLite used an optional THREADSAFE macro.
362 ** We support that for legacy.
363 */
364 #if !defined(SQLITE_THREADSAFE)
365 # if defined(THREADSAFE)
366 # define SQLITE_THREADSAFE THREADSAFE
367 # else
368 # define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
369 # endif
370 #endif
371
372 /*
373 ** Powersafe overwrite is on by default. But can be turned off using
374 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
@@ -676,13 +676,13 @@
676 **
677 ** See also: [sqlite3_libversion()],
678 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
679 ** [sqlite_version()] and [sqlite_source_id()].
680 */
681 #define SQLITE_VERSION "3.7.17"
682 #define SQLITE_VERSION_NUMBER 3007017
683 #define SQLITE_SOURCE_ID "2013-04-25 17:27:08 aabeea98f53edde68f484f1794ae70789dac3889"
684
685 /*
686 ** CAPI3REF: Run-Time Library Version Numbers
687 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
688 **
@@ -994,10 +994,12 @@
994 #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
995 #define SQLITE_AUTH 23 /* Authorization denied */
996 #define SQLITE_FORMAT 24 /* Auxiliary database format error */
997 #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
998 #define SQLITE_NOTADB 26 /* File opened that is not a database file */
999 #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
1000 #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
1001 #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
1002 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
1003 /* end-of-error-codes */
1004
1005 /*
@@ -1044,10 +1046,11 @@
1046 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
1047 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
1048 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
1049 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
1050 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
1051 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
1052 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
1053 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
1054 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
1055 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
1056 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -1063,10 +1066,12 @@
1066 #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
1067 #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
1068 #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
1069 #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
1070 #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
1071 #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
1072 #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
1073
1074 /*
1075 ** CAPI3REF: Flags For File Open Operations
1076 **
1077 ** These bit values are intended for use in the
@@ -1302,10 +1307,13 @@
1307 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
1308 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
1309 void (*xShmBarrier)(sqlite3_file*);
1310 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
1311 /* Methods above are valid for version 2 */
1312 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
1313 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
1314 /* Methods above are valid for version 3 */
1315 /* Additional methods may be added in future releases */
1316 };
1317
1318 /*
1319 ** CAPI3REF: Standard File Control Opcodes
@@ -1438,11 +1446,12 @@
1446 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
1447 ** file control occurs at the beginning of pragma statement analysis and so
1448 ** it is able to override built-in [PRAGMA] statements.
1449 **
1450 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
1451 ** ^The [SQLITE_FCNTL_BUSYHANDLER]
1452 ** file-control may be invoked by SQLite on the database file handle
1453 ** shortly after it is opened in order to provide a custom VFS with access
1454 ** to the connections busy-handler callback. The argument is of type (void **)
1455 ** - an array of two (void *) values. The first (void *) actually points
1456 ** to a function of type (int (*)(void *)). In order to invoke the connections
1457 ** busy-handler, this function should be invoked with the second (void *) in
@@ -1449,17 +1458,28 @@
1458 ** the array as the only argument. If it returns non-zero, then the operation
1459 ** should be retried. If it returns zero, the custom VFS should abandon the
1460 ** current operation.
1461 **
1462 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
1463 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
1464 ** to have SQLite generate a
1465 ** temporary filename using the same algorithm that is followed to generate
1466 ** temporary filenames for TEMP tables and other internal uses. The
1467 ** argument should be a char** which will be filled with the filename
1468 ** written into memory obtained from [sqlite3_malloc()]. The caller should
1469 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
1470 **
1471 ** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
1472 ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
1473 ** maximum number of bytes that will be used for memory-mapped I/O.
1474 ** The argument is a pointer to a value of type sqlite3_int64 that
1475 ** is an advisory maximum number of bytes in the file to memory map. The
1476 ** pointer is overwritten with the old value. The limit is not changed if
1477 ** the value originally pointed to is negative, and so the current limit
1478 ** can be queried by passing in a pointer to a negative number. This
1479 ** file-control is used internally to implement [PRAGMA mmap_size].
1480 **
1481 ** </ul>
1482 */
1483 #define SQLITE_FCNTL_LOCKSTATE 1
1484 #define SQLITE_GET_LOCKPROXYFILE 2
1485 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -1474,10 +1494,11 @@
1494 #define SQLITE_FCNTL_VFSNAME 12
1495 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
1496 #define SQLITE_FCNTL_PRAGMA 14
1497 #define SQLITE_FCNTL_BUSYHANDLER 15
1498 #define SQLITE_FCNTL_TEMPFILENAME 16
1499 #define SQLITE_FCNTL_MMAP_SIZE 18
1500
1501 /*
1502 ** CAPI3REF: Mutex Handle
1503 **
1504 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -2186,26 +2207,42 @@
2207 **
2208 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
2209 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
2210 ** <dd> These options are obsolete and should not be used by new code.
2211 ** They are retained for backwards compatibility but are now no-ops.
2212 ** </dd>
2213 **
2214 ** [[SQLITE_CONFIG_SQLLOG]]
2215 ** <dt>SQLITE_CONFIG_SQLLOG
2216 ** <dd>This option is only available if sqlite is compiled with the
2217 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
2218 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
2219 ** The second should be of type (void*). The callback is invoked by the library
2220 ** in three separate circumstances, identified by the value passed as the
2221 ** fourth parameter. If the fourth parameter is 0, then the database connection
2222 ** passed as the second argument has just been opened. The third argument
2223 ** points to a buffer containing the name of the main database file. If the
2224 ** fourth parameter is 1, then the SQL statement that the third parameter
2225 ** points to has just been executed. Or, if the fourth parameter is 2, then
2226 ** the connection being passed as the second parameter is being closed. The
2227 ** third parameter is passed NULL In this case. An example of using this
2228 ** configuration option can be seen in the "test_sqllog.c" source file in
2229 ** the canonical SQLite source tree.</dd>
2230 **
2231 ** [[SQLITE_CONFIG_MMAP_SIZE]]
2232 ** <dt>SQLITE_CONFIG_MMAP_SIZE
2233 ** <dd>SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
2234 ** that are the default mmap size limit (the default setting for
2235 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
2236 ** The default setting can be overridden by each database connection using
2237 ** either the [PRAGMA mmap_size] command, or by using the
2238 ** [SQLITE_FCNTL_MMAP_SIZE] file control. The maximum allowed mmap size
2239 ** cannot be changed at run-time. Nor may the maximum allowed mmap size
2240 ** exceed the compile-time maximum mmap size set by the
2241 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.
2242 ** If either argument to this option is negative, then that argument is
2243 ** changed to its compile-time default.
2244 ** </dl>
2245 */
2246 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
2247 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
2248 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -2225,10 +2262,11 @@
2262 #define SQLITE_CONFIG_URI 17 /* int */
2263 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
2264 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
2265 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
2266 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
2267 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
2268
2269 /*
2270 ** CAPI3REF: Database Connection Configuration Options
2271 **
2272 ** These constants are the available integer configuration options that
@@ -4756,11 +4794,11 @@
4794 ** SQLITE_TRANSIENT value means that the content will likely change in
4795 ** the near future and that SQLite should make its own private copy of
4796 ** the content before returning.
4797 **
4798 ** The typedef is necessary to work around problems in certain
4799 ** C++ compilers.
4800 */
4801 typedef void (*sqlite3_destructor_type)(void*);
4802 #define SQLITE_STATIC ((sqlite3_destructor_type)0)
4803 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
4804
@@ -5555,15 +5593,24 @@
5593 ** CAPI3REF: Load An Extension
5594 **
5595 ** ^This interface loads an SQLite extension library from the named file.
5596 **
5597 ** ^The sqlite3_load_extension() interface attempts to load an
5598 ** [SQLite extension] library contained in the file zFile. If
5599 ** the file cannot be loaded directly, attempts are made to load
5600 ** with various operating-system specific extensions added.
5601 ** So for example, if "samplelib" cannot be loaded, then names like
5602 ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5603 ** be tried also.
5604 **
5605 ** ^The entry point is zProc.
5606 ** ^(zProc may be 0, in which case SQLite will try to come up with an
5607 ** entry point name on its own. It first tries "sqlite3_extension_init".
5608 ** If that does not work, it constructs a name "sqlite3_X_init" where the
5609 ** X is consists of the lower-case equivalent of all ASCII alphabetic
5610 ** characters in the filename from the last "/" to the first following
5611 ** "." and omitting any initial "lib".)^
5612 ** ^The sqlite3_load_extension() interface returns
5613 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5614 ** ^If an error occurs and pzErrMsg is not 0, then the
5615 ** [sqlite3_load_extension()] interface shall attempt to
5616 ** fill *pzErrMsg with error message text stored in memory
@@ -5585,15 +5632,15 @@
5632
5633 /*
5634 ** CAPI3REF: Enable Or Disable Extension Loading
5635 **
5636 ** ^So as not to open security holes in older applications that are
5637 ** unprepared to deal with [extension loading], and as a means of disabling
5638 ** [extension loading] while evaluating user-entered SQL, the following API
5639 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5640 **
5641 ** ^Extension loading is off by default.
5642 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5643 ** to turn extension loading on and call it with onoff==0 to turn
5644 ** it back off again.
5645 */
5646 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5601,11 +5648,11 @@
5648 /*
5649 ** CAPI3REF: Automatically Load Statically Linked Extensions
5650 **
5651 ** ^This interface causes the xEntryPoint() function to be invoked for
5652 ** each new [database connection] that is created. The idea here is that
5653 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
5654 ** that is to be automatically loaded into all new database connections.
5655 **
5656 ** ^(Even though the function prototype shows that xEntryPoint() takes
5657 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5658 ** arguments and expects and integer result as if the signature of the
@@ -7381,10 +7428,25 @@
7428 ** independence" that SQLite uses internally when comparing identifiers.
7429 */
7430 SQLITE_API int sqlite3_stricmp(const char *, const char *);
7431 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
7432
7433 /*
7434 ** CAPI3REF: String Globbing
7435 *
7436 ** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
7437 ** the glob pattern P, and it returns non-zero if string X does not match
7438 ** the glob pattern P. ^The definition of glob pattern matching used in
7439 ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
7440 ** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case
7441 ** sensitive.
7442 **
7443 ** Note that this routine returns zero on a match and non-zero if the strings
7444 ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
7445 */
7446 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
7447
7448 /*
7449 ** CAPI3REF: Error Logging Interface
7450 **
7451 ** ^The [sqlite3_log()] interface writes a message into the error log
7452 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
@@ -8069,10 +8131,11 @@
8131 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
8132 ** on the command-line
8133 */
8134 #ifndef SQLITE_TEMP_STORE
8135 # define SQLITE_TEMP_STORE 1
8136 # define SQLITE_TEMP_STORE_xc 1 /* Exclude from ctime.c */
8137 #endif
8138
8139 /*
8140 ** GCC does not define the offsetof() macro so we'll have to do it
8141 ** ourselves.
@@ -8216,10 +8279,53 @@
8279 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&3)==0)
8280 #else
8281 # define EIGHT_BYTE_ALIGNMENT(X) ((((char*)(X) - (char*)0)&7)==0)
8282 #endif
8283
8284 /*
8285 ** Disable MMAP on platforms where it is known to not work
8286 */
8287 #if defined(__OpenBSD__) || defined(__QNXNTO__)
8288 # undef SQLITE_MAX_MMAP_SIZE
8289 # define SQLITE_MAX_MMAP_SIZE 0
8290 #endif
8291
8292 /*
8293 ** Default maximum size of memory used by memory-mapped I/O in the VFS
8294 */
8295 #ifdef __APPLE__
8296 # include <TargetConditionals.h>
8297 # if TARGET_OS_IPHONE
8298 # undef SQLITE_MAX_MMAP_SIZE
8299 # define SQLITE_MAX_MMAP_SIZE 0
8300 # endif
8301 #endif
8302 #ifndef SQLITE_MAX_MMAP_SIZE
8303 # if defined(__linux__) \
8304 || defined(_WIN32) \
8305 || (defined(__APPLE__) && defined(__MACH__)) \
8306 || defined(__sun)
8307 # define SQLITE_MAX_MMAP_SIZE 2147483648
8308 # else
8309 # define SQLITE_MAX_MMAP_SIZE 0
8310 # endif
8311 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
8312 #endif
8313
8314 /*
8315 ** The default MMAP_SIZE is zero on all platforms. Or, even if a larger
8316 ** default MMAP_SIZE is specified at compile-time, make sure that it does
8317 ** not exceed the maximum mmap size.
8318 */
8319 #ifndef SQLITE_DEFAULT_MMAP_SIZE
8320 # define SQLITE_DEFAULT_MMAP_SIZE 0
8321 # define SQLITE_DEFAULT_MMAP_SIZE_xc 1 /* Exclude from ctime.c */
8322 #endif
8323 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8324 # undef SQLITE_DEFAULT_MMAP_SIZE
8325 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8326 #endif
8327
8328 /*
8329 ** An instance of the following structure is used to store the busy-handler
8330 ** callback for a given sqlite handle.
8331 **
@@ -8437,10 +8543,11 @@
8543 #define BTREE_SINGLE 4 /* The file contains at most 1 b-tree */
8544 #define BTREE_UNORDERED 8 /* Use of a hash implementation is OK */
8545
8546 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8547 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8548 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8549 SQLITE_PRIVATE int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
8550 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8551 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8552 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8553 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
@@ -9137,10 +9244,16 @@
9244 #define PAGER_JOURNALMODE_OFF 2 /* Journal omitted. */
9245 #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */
9246 #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */
9247 #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */
9248
9249 /*
9250 ** Flags that make up the mask passed to sqlite3PagerAcquire().
9251 */
9252 #define PAGER_ACQUIRE_NOCONTENT 0x01 /* Do not load data from disk */
9253 #define PAGER_ACQUIRE_READONLY 0x02 /* Read-only page is acceptable */
9254
9255 /*
9256 ** The remainder of this file contains the declarations of the functions
9257 ** that make up the Pager sub-system API. See source code comments for
9258 ** a detailed description of each routine.
9259 */
@@ -9161,10 +9274,11 @@
9274 /* Functions used to configure a Pager object. */
9275 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
9276 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9277 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9278 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9279 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
9280 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9281 SQLITE_PRIVATE void sqlite3PagerSetSafetyLevel(Pager*,int,int,int);
9282 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9283 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9284 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
@@ -9306,10 +9420,12 @@
9420 #define PGHDR_NEED_SYNC 0x004 /* Fsync the rollback journal before
9421 ** writing this page to the database */
9422 #define PGHDR_NEED_READ 0x008 /* Content is unread */
9423 #define PGHDR_REUSE_UNLIKELY 0x010 /* A hint that reuse is unlikely */
9424 #define PGHDR_DONT_WRITE 0x020 /* Do not write content to disk */
9425
9426 #define PGHDR_MMAP 0x040 /* This is an mmap page object */
9427
9428 /* Initialize and shutdown the page cache subsystem */
9429 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
9430 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
9431
@@ -9518,18 +9634,10 @@
9634 */
9635 #if !defined(SQLITE_OS_WINRT)
9636 # define SQLITE_OS_WINRT 0
9637 #endif
9638
 
 
 
 
 
 
 
 
9639 /* If the SET_FULLSYNC macro is not defined above, then make it
9640 ** a no-op
9641 */
9642 #ifndef SET_FULLSYNC
9643 # define SET_FULLSYNC(x,y)
@@ -9678,10 +9786,12 @@
9786 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
9787 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
9788 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
9789 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
9790 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
9791 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
9792 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
9793
9794
9795 /*
9796 ** Functions for accessing sqlite3_vfs methods
9797 */
@@ -9917,10 +10027,11 @@
10027 sqlite3_mutex *mutex; /* Connection mutex */
10028 Db *aDb; /* All backends */
10029 int nDb; /* Number of backends currently in use */
10030 int flags; /* Miscellaneous flags. See below */
10031 i64 lastRowid; /* ROWID of most recent insert (see above) */
10032 i64 szMmap; /* Default mmap_size setting */
10033 unsigned int openFlags; /* Flags passed to sqlite3_vfs.xOpen() */
10034 int errCode; /* Most recent error code (SQLITE_*) */
10035 int errMask; /* & result codes with this before returning */
10036 u16 dbOptFlags; /* Flags to enable/disable optimizations */
10037 u8 autoCommit; /* The auto-commit flag. */
@@ -11153,10 +11264,12 @@
11264 */
11265 #define NC_AllowAgg 0x01 /* Aggregate functions are allowed here */
11266 #define NC_HasAgg 0x02 /* One or more aggregate functions seen */
11267 #define NC_IsCheck 0x04 /* True if resolving names in a CHECK constraint */
11268 #define NC_InAggFunc 0x08 /* True if analyzing arguments to an agg func */
11269 #define NC_AsMaybe 0x10 /* Resolve to AS terms of the result set only
11270 ** if no other resolution is available */
11271
11272 /*
11273 ** An instance of the following structure contains all information
11274 ** needed to generate code for a single SELECT statement.
11275 **
@@ -11588,10 +11701,12 @@
11701 sqlite3_mutex_methods mutex; /* Low-level mutex interface */
11702 sqlite3_pcache_methods2 pcache2; /* Low-level page-cache interface */
11703 void *pHeap; /* Heap storage space */
11704 int nHeap; /* Size of pHeap[] */
11705 int mnReq, mxReq; /* Min and max heap requests sizes */
11706 sqlite3_int64 szMmap; /* mmap() space per open file */
11707 sqlite3_int64 mxMmap; /* Maximum value for szMmap */
11708 void *pScratch; /* Scratch memory */
11709 int szScratch; /* Size of each scratch buffer */
11710 int nScratch; /* Number of scratch buffers */
11711 void *pPage; /* Page cache memory */
11712 int szPage; /* Size of each page in pPage[] */
@@ -11622,10 +11737,11 @@
11737 struct Walker {
11738 int (*xExprCallback)(Walker*, Expr*); /* Callback for expressions */
11739 int (*xSelectCallback)(Walker*,Select*); /* Callback for SELECTs */
11740 Parse *pParse; /* Parser context. */
11741 int walkerDepth; /* Number of subqueries */
11742 u8 bSelectDepthFirst; /* Do subqueries first */
11743 union { /* Extra data for callback */
11744 NameContext *pNC; /* Naming context */
11745 int i; /* Integer value */
11746 SrcList *pSrcList; /* FROM clause */
11747 struct SrcCount *pSrcCount; /* Counting column references */
@@ -12609,10 +12725,12 @@
12725 {0,0,0,0,0,0,0,0,0}, /* mutex */
12726 {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
12727 (void*)0, /* pHeap */
12728 0, /* nHeap */
12729 0, 0, /* mnHeap, mxHeap */
12730 SQLITE_DEFAULT_MMAP_SIZE, /* szMmap */
12731 SQLITE_MAX_MMAP_SIZE, /* mxMmap */
12732 (void*)0, /* pScratch */
12733 0, /* szScratch */
12734 0, /* nScratch */
12735 (void*)0, /* pPage */
12736 0, /* szPage */
@@ -12732,18 +12850,18 @@
12850 "CHECK_PAGES",
12851 #endif
12852 #ifdef SQLITE_COVERAGE_TEST
12853 "COVERAGE_TEST",
12854 #endif
 
 
 
12855 #ifdef SQLITE_DEBUG
12856 "DEBUG",
12857 #endif
12858 #ifdef SQLITE_DEFAULT_LOCKING_MODE
12859 "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
12860 #endif
12861 #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
12862 "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
12863 #endif
12864 #ifdef SQLITE_DISABLE_DIRSYNC
12865 "DISABLE_DIRSYNC",
12866 #endif
12867 #ifdef SQLITE_DISABLE_LFS
@@ -12831,10 +12949,13 @@
12949 "INT64_TYPE",
12950 #endif
12951 #ifdef SQLITE_LOCK_TRACE
12952 "LOCK_TRACE",
12953 #endif
12954 #if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
12955 "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
12956 #endif
12957 #ifdef SQLITE_MAX_SCHEMA_RETRY
12958 "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
12959 #endif
12960 #ifdef SQLITE_MEMDEBUG
12961 "MEMDEBUG",
@@ -12888,15 +13009,10 @@
13009 "OMIT_CAST",
13010 #endif
13011 #ifdef SQLITE_OMIT_CHECK
13012 "OMIT_CHECK",
13013 #endif
 
 
 
 
 
13014 #ifdef SQLITE_OMIT_COMPLETE
13015 "OMIT_COMPLETE",
13016 #endif
13017 #ifdef SQLITE_OMIT_COMPOUND_SELECT
13018 "OMIT_COMPOUND_SELECT",
@@ -13034,17 +13150,17 @@
13150 "SOUNDEX",
13151 #endif
13152 #ifdef SQLITE_TCL
13153 "TCL",
13154 #endif
13155 #if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
13156 "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
13157 #endif
13158 #ifdef SQLITE_TEST
13159 "TEST",
13160 #endif
13161 #if defined(SQLITE_THREADSAFE)
13162 "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
13163 #endif
13164 #ifdef SQLITE_USE_ALLOCA
13165 "USE_ALLOCA",
13166 #endif
@@ -13066,12 +13182,15 @@
13182 n = sqlite3Strlen30(zOptName);
13183
13184 /* Since ArraySize(azCompileOpt) is normally in single digits, a
13185 ** linear search is adequate. No need for a binary search. */
13186 for(i=0; i<ArraySize(azCompileOpt); i++){
13187 if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13188 && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13189 ){
13190 return 1;
13191 }
13192 }
13193 return 0;
13194 }
13195
13196 /*
@@ -13124,10 +13243,18 @@
13243 ** this header information was factored out.
13244 */
13245 #ifndef _VDBEINT_H_
13246 #define _VDBEINT_H_
13247
13248 /*
13249 ** The maximum number of times that a statement will try to reparse
13250 ** itself before giving up and returning SQLITE_SCHEMA.
13251 */
13252 #ifndef SQLITE_MAX_SCHEMA_RETRY
13253 # define SQLITE_MAX_SCHEMA_RETRY 50
13254 #endif
13255
13256 /*
13257 ** SQL is translated into a sequence of instructions to be
13258 ** executed by a virtual machine. Each instruction is an instance
13259 ** of the following structure.
13260 */
@@ -15089,10 +15216,30 @@
15216 void volatile **pp /* OUT: Pointer to mapping */
15217 ){
15218 DO_OS_MALLOC_TEST(id);
15219 return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
15220 }
15221
15222 #if SQLITE_MAX_MMAP_SIZE>0
15223 /* The real implementation of xFetch and xUnfetch */
15224 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15225 DO_OS_MALLOC_TEST(id);
15226 return id->pMethods->xFetch(id, iOff, iAmt, pp);
15227 }
15228 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15229 return id->pMethods->xUnfetch(id, iOff, p);
15230 }
15231 #else
15232 /* No-op stubs to use when memory-mapped I/O is disabled */
15233 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15234 *pp = 0;
15235 return SQLITE_OK;
15236 }
15237 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15238 return SQLITE_OK;
15239 }
15240 #endif
15241
15242 /*
15243 ** The next group of routines are convenience wrappers around the
15244 ** VFS methods.
15245 */
@@ -21584,11 +21731,11 @@
21731 *pNum = (i64)u;
21732 }
21733 testcase( i==18 );
21734 testcase( i==19 );
21735 testcase( i==20 );
21736 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
21737 /* zNum is empty or contains non-numeric text or is longer
21738 ** than 19 digits (thus guaranteeing that it is too large) */
21739 return 1;
21740 }else if( i<19*incr ){
21741 /* Less than 19 digits, so we know that it fits in 64 bits */
@@ -22945,10 +23092,15 @@
23092 void *lockingContext; /* Locking style specific state */
23093 UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */
23094 const char *zPath; /* Name of the file */
23095 unixShm *pShm; /* Shared memory segment information */
23096 int szChunk; /* Configured by FCNTL_CHUNK_SIZE */
23097 int nFetchOut; /* Number of outstanding xFetch refs */
23098 sqlite3_int64 mmapSize; /* Usable size of mapping at pMapRegion */
23099 sqlite3_int64 mmapSizeActual; /* Actual size of mapping at pMapRegion */
23100 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
23101 void *pMapRegion; /* Memory mapped region */
23102 #ifdef __QNXNTO__
23103 int sectorSize; /* Device sector size */
23104 int deviceCharacteristics; /* Precomputed device characteristics */
23105 #endif
23106 #if SQLITE_ENABLE_LOCKING_STYLE
@@ -22969,11 +23121,13 @@
23121 ** one described by ticket #3584.
23122 */
23123 unsigned char transCntrChng; /* True if the transaction counter changed */
23124 unsigned char dbUpdate; /* True if any part of database file changed */
23125 unsigned char inNormalWrite; /* True if in a normal write operation */
23126
23127 #endif
23128
23129 #ifdef SQLITE_TEST
23130 /* In test mode, increase the size of this structure a bit so that
23131 ** it is larger than the struct CrashFile defined in test6.c.
23132 */
23133 char aPadding[32];
@@ -22993,10 +23147,11 @@
23147 #endif
23148 #define UNIXFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
23149 #define UNIXFILE_DELETE 0x20 /* Delete on close */
23150 #define UNIXFILE_URI 0x40 /* Filename might have query parameters */
23151 #define UNIXFILE_NOLOCK 0x80 /* Do no file locking */
23152 #define UNIXFILE_WARNED 0x0100 /* verifyDbFile() warnings have been issued */
23153
23154 /*
23155 ** Include code that is common to all os_*.c files
23156 */
23157 /************** Include os_common.h in the middle of os_unix.c ***************/
@@ -23234,10 +23389,21 @@
23389 #define threadid pthread_self()
23390 #else
23391 #define threadid 0
23392 #endif
23393
23394 /*
23395 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
23396 */
23397 #if !defined(HAVE_MREMAP)
23398 # if defined(__linux__) && defined(_GNU_SOURCE)
23399 # define HAVE_MREMAP 1
23400 # else
23401 # define HAVE_MREMAP 0
23402 # endif
23403 #endif
23404
23405 /*
23406 ** Different Unix systems declare open() in different ways. Same use
23407 ** open(const char*,int,mode_t). Others use open(const char*,int,...).
23408 ** The difference is important when using a pointer to the function.
23409 **
@@ -23365,10 +23531,23 @@
23531 #define osRmdir ((int(*)(const char*))aSyscall[19].pCurrent)
23532
23533 { "fchown", (sqlite3_syscall_ptr)posixFchown, 0 },
23534 #define osFchown ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
23535
23536 { "mmap", (sqlite3_syscall_ptr)mmap, 0 },
23537 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
23538
23539 { "munmap", (sqlite3_syscall_ptr)munmap, 0 },
23540 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
23541
23542 #if HAVE_MREMAP
23543 { "mremap", (sqlite3_syscall_ptr)mremap, 0 },
23544 #else
23545 { "mremap", (sqlite3_syscall_ptr)0, 0 },
23546 #endif
23547 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
23548
23549 }; /* End of the overrideable system calls */
23550
23551 /*
23552 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
23553 ** "unix" VFSes. Return SQLITE_OK opon successfully updating the
@@ -23694,11 +23873,10 @@
23873
23874 default:
23875 return sqliteIOErr;
23876 }
23877 }
 
23878
23879
23880 /******************************************************************************
23881 ****************** Begin Unique File ID Utility Used By VxWorks ***************
23882 **
@@ -24032,11 +24210,10 @@
24210 #else
24211 /* Non-threadsafe build, use strerror(). */
24212 zErr = strerror(iErrno);
24213 #endif
24214
 
24215 if( zPath==0 ) zPath = "";
24216 sqlite3_log(errcode,
24217 "os_unix.c:%d: (%d) %s(%s) - %s",
24218 iLine, iErrno, zFunc, zPath, zErr
24219 );
@@ -24197,10 +24374,54 @@
24374 }
24375 *ppInode = pInode;
24376 return SQLITE_OK;
24377 }
24378
24379
24380 /*
24381 ** Check a unixFile that is a database. Verify the following:
24382 **
24383 ** (1) There is exactly one hard link on the file
24384 ** (2) The file is not a symbolic link
24385 ** (3) The file has not been renamed or unlinked
24386 **
24387 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
24388 */
24389 static void verifyDbFile(unixFile *pFile){
24390 struct stat buf;
24391 int rc;
24392 if( pFile->ctrlFlags & UNIXFILE_WARNED ){
24393 /* One or more of the following warnings have already been issued. Do not
24394 ** repeat them so as not to clutter the error log */
24395 return;
24396 }
24397 rc = osFstat(pFile->h, &buf);
24398 if( rc!=0 ){
24399 sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
24400 pFile->ctrlFlags |= UNIXFILE_WARNED;
24401 return;
24402 }
24403 if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
24404 sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
24405 pFile->ctrlFlags |= UNIXFILE_WARNED;
24406 return;
24407 }
24408 if( buf.st_nlink>1 ){
24409 sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
24410 pFile->ctrlFlags |= UNIXFILE_WARNED;
24411 return;
24412 }
24413 if( pFile->pInode!=0
24414 && ((rc = osStat(pFile->zPath, &buf))!=0
24415 || buf.st_ino!=pFile->pInode->fileId.ino)
24416 ){
24417 sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
24418 pFile->ctrlFlags |= UNIXFILE_WARNED;
24419 return;
24420 }
24421 }
24422
24423
24424 /*
24425 ** This routine checks if there is a RESERVED lock held on the specified
24426 ** file by this or any other process. If such a lock is held, set *pResOut
24427 ** to a non-zero value otherwise *pResOut is set to zero. The return value
@@ -24728,12 +24949,16 @@
24949 **
24950 ** If the locking level of the file descriptor is already at or below
24951 ** the requested locking level, this routine is a no-op.
24952 */
24953 static int unixUnlock(sqlite3_file *id, int eFileLock){
24954 assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
24955 return posixUnlock(id, eFileLock, 0);
24956 }
24957
24958 static int unixMapfile(unixFile *pFd, i64 nByte);
24959 static void unixUnmapfile(unixFile *pFd);
24960
24961 /*
24962 ** This function performs the parts of the "close file" operation
24963 ** common to all locking schemes. It closes the directory and file
24964 ** handles, if they are valid, and sets all fields of the unixFile
@@ -24743,10 +24968,11 @@
24968 ** even on VxWorks. A mutex will be acquired on VxWorks by the
24969 ** vxworksReleaseFileId() routine.
24970 */
24971 static int closeUnixFile(sqlite3_file *id){
24972 unixFile *pFile = (unixFile*)id;
24973 unixUnmapfile(pFile);
24974 if( pFile->h>=0 ){
24975 robust_close(pFile, pFile->h, __LINE__);
24976 pFile->h = -1;
24977 }
24978 #if OS_VXWORKS
@@ -24769,10 +24995,11 @@
24995 ** Close a file.
24996 */
24997 static int unixClose(sqlite3_file *id){
24998 int rc = SQLITE_OK;
24999 unixFile *pFile = (unixFile *)id;
25000 verifyDbFile(pFile);
25001 unixUnlock(id, NO_LOCK);
25002 unixEnterMutex();
25003
25004 /* unixFile.pInode is always valid here. Otherwise, a different close
25005 ** routine (e.g. nolockClose()) would be called instead.
@@ -26009,10 +26236,27 @@
26236 assert( pFile->pUnused==0
26237 || offset>=PENDING_BYTE+512
26238 || offset+amt<=PENDING_BYTE
26239 );
26240 #endif
26241
26242 #if SQLITE_MAX_MMAP_SIZE>0
26243 /* Deal with as much of this read request as possible by transfering
26244 ** data from the memory mapping using memcpy(). */
26245 if( offset<pFile->mmapSize ){
26246 if( offset+amt <= pFile->mmapSize ){
26247 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
26248 return SQLITE_OK;
26249 }else{
26250 int nCopy = pFile->mmapSize - offset;
26251 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
26252 pBuf = &((u8 *)pBuf)[nCopy];
26253 amt -= nCopy;
26254 offset += nCopy;
26255 }
26256 }
26257 #endif
26258
26259 got = seekAndRead(pFile, offset, pBuf, amt);
26260 if( got==amt ){
26261 return SQLITE_OK;
26262 }else if( got<0 ){
@@ -26111,10 +26355,27 @@
26355 SimulateIOErrorBenign(0);
26356 if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
26357 pFile->transCntrChng = 1; /* The transaction counter has changed */
26358 }
26359 }
26360 }
26361 #endif
26362
26363 #if SQLITE_MAX_MMAP_SIZE>0
26364 /* Deal with as much of this write request as possible by transfering
26365 ** data from the memory mapping using memcpy(). */
26366 if( offset<pFile->mmapSize ){
26367 if( offset+amt <= pFile->mmapSize ){
26368 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
26369 return SQLITE_OK;
26370 }else{
26371 int nCopy = pFile->mmapSize - offset;
26372 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
26373 pBuf = &((u8 *)pBuf)[nCopy];
26374 amt -= nCopy;
26375 offset += nCopy;
26376 }
26377 }
26378 #endif
26379
26380 while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
26381 amt -= wrote;
@@ -26395,10 +26656,18 @@
26656 */
26657 if( pFile->inNormalWrite && nByte==0 ){
26658 pFile->transCntrChng = 1;
26659 }
26660 #endif
26661
26662 /* If the file was just truncated to a size smaller than the currently
26663 ** mapped region, reduce the effective mapping size as well. SQLite will
26664 ** use read() and write() to access data beyond this point from now on.
26665 */
26666 if( nByte<pFile->mmapSize ){
26667 pFile->mmapSize = nByte;
26668 }
26669
26670 return SQLITE_OK;
26671 }
26672 }
26673
@@ -26483,10 +26752,23 @@
26752 iWrite += nBlk;
26753 }
26754 #endif
26755 }
26756 }
26757
26758 if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
26759 int rc;
26760 if( pFile->szChunk<=0 ){
26761 if( robust_ftruncate(pFile->h, nByte) ){
26762 pFile->lastErrno = errno;
26763 return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
26764 }
26765 }
26766
26767 rc = unixMapfile(pFile, nByte);
26768 return rc;
26769 }
26770
26771 return SQLITE_OK;
26772 }
26773
26774 /*
@@ -26550,10 +26832,22 @@
26832 if( zTFile ){
26833 unixGetTempname(pFile->pVfs->mxPathname, zTFile);
26834 *(char**)pArg = zTFile;
26835 }
26836 return SQLITE_OK;
26837 }
26838 case SQLITE_FCNTL_MMAP_SIZE: {
26839 i64 newLimit = *(i64*)pArg;
26840 if( newLimit>sqlite3GlobalConfig.mxMmap ){
26841 newLimit = sqlite3GlobalConfig.mxMmap;
26842 }
26843 *(i64*)pArg = pFile->mmapSizeMax;
26844 if( newLimit>=0 ){
26845 pFile->mmapSizeMax = newLimit;
26846 if( newLimit<pFile->mmapSize ) pFile->mmapSize = newLimit;
26847 }
26848 return SQLITE_OK;
26849 }
26850 #ifdef SQLITE_DEBUG
26851 /* The pager calls this method to signal that it has done
26852 ** a rollback and that the database is therefore unchanged and
26853 ** it hence it is OK for the transaction change counter to be
@@ -26863,11 +27157,11 @@
27157 int i;
27158 assert( p->pInode==pFd->pInode );
27159 sqlite3_mutex_free(p->mutex);
27160 for(i=0; i<p->nRegion; i++){
27161 if( p->h>=0 ){
27162 osMunmap(p->apRegion[i], p->szRegion);
27163 }else{
27164 sqlite3_free(p->apRegion[i]);
27165 }
27166 }
27167 sqlite3_free(p->apRegion);
@@ -27136,11 +27430,11 @@
27430 }
27431 pShmNode->apRegion = apNew;
27432 while(pShmNode->nRegion<=iRegion){
27433 void *pMem;
27434 if( pShmNode->h>=0 ){
27435 pMem = osMmap(0, szRegion,
27436 pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
27437 MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
27438 );
27439 if( pMem==MAP_FAILED ){
27440 rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
@@ -27352,10 +27646,240 @@
27646 # define unixShmMap 0
27647 # define unixShmLock 0
27648 # define unixShmBarrier 0
27649 # define unixShmUnmap 0
27650 #endif /* #ifndef SQLITE_OMIT_WAL */
27651
27652 /*
27653 ** If it is currently memory mapped, unmap file pFd.
27654 */
27655 static void unixUnmapfile(unixFile *pFd){
27656 assert( pFd->nFetchOut==0 );
27657 #if SQLITE_MAX_MMAP_SIZE>0
27658 if( pFd->pMapRegion ){
27659 osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
27660 pFd->pMapRegion = 0;
27661 pFd->mmapSize = 0;
27662 pFd->mmapSizeActual = 0;
27663 }
27664 #endif
27665 }
27666
27667 #if SQLITE_MAX_MMAP_SIZE>0
27668 /*
27669 ** Return the system page size.
27670 */
27671 static int unixGetPagesize(void){
27672 #if HAVE_MREMAP
27673 return 512;
27674 #elif defined(_BSD_SOURCE)
27675 return getpagesize();
27676 #else
27677 return (int)sysconf(_SC_PAGESIZE);
27678 #endif
27679 }
27680 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
27681
27682 #if SQLITE_MAX_MMAP_SIZE>0
27683 /*
27684 ** Attempt to set the size of the memory mapping maintained by file
27685 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
27686 **
27687 ** If successful, this function sets the following variables:
27688 **
27689 ** unixFile.pMapRegion
27690 ** unixFile.mmapSize
27691 ** unixFile.mmapSizeActual
27692 **
27693 ** If unsuccessful, an error message is logged via sqlite3_log() and
27694 ** the three variables above are zeroed. In this case SQLite should
27695 ** continue accessing the database using the xRead() and xWrite()
27696 ** methods.
27697 */
27698 static void unixRemapfile(
27699 unixFile *pFd, /* File descriptor object */
27700 i64 nNew /* Required mapping size */
27701 ){
27702 const char *zErr = "mmap";
27703 int h = pFd->h; /* File descriptor open on db file */
27704 u8 *pOrig = (u8 *)pFd->pMapRegion; /* Pointer to current file mapping */
27705 i64 nOrig = pFd->mmapSizeActual; /* Size of pOrig region in bytes */
27706 u8 *pNew = 0; /* Location of new mapping */
27707 int flags = PROT_READ; /* Flags to pass to mmap() */
27708
27709 assert( pFd->nFetchOut==0 );
27710 assert( nNew>pFd->mmapSize );
27711 assert( nNew<=pFd->mmapSizeMax );
27712 assert( nNew>0 );
27713 assert( pFd->mmapSizeActual>=pFd->mmapSize );
27714 assert( MAP_FAILED!=0 );
27715
27716 if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
27717
27718 if( pOrig ){
27719 const int szSyspage = unixGetPagesize();
27720 i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
27721 u8 *pReq = &pOrig[nReuse];
27722
27723 /* Unmap any pages of the existing mapping that cannot be reused. */
27724 if( nReuse!=nOrig ){
27725 osMunmap(pReq, nOrig-nReuse);
27726 }
27727
27728 #if HAVE_MREMAP
27729 pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
27730 zErr = "mremap";
27731 #else
27732 pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
27733 if( pNew!=MAP_FAILED ){
27734 if( pNew!=pReq ){
27735 osMunmap(pNew, nNew - nReuse);
27736 pNew = 0;
27737 }else{
27738 pNew = pOrig;
27739 }
27740 }
27741 #endif
27742
27743 /* The attempt to extend the existing mapping failed. Free it. */
27744 if( pNew==MAP_FAILED || pNew==0 ){
27745 osMunmap(pOrig, nReuse);
27746 }
27747 }
27748
27749 /* If pNew is still NULL, try to create an entirely new mapping. */
27750 if( pNew==0 ){
27751 pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
27752 }
27753
27754 if( pNew==MAP_FAILED ){
27755 pNew = 0;
27756 nNew = 0;
27757 unixLogError(SQLITE_OK, zErr, pFd->zPath);
27758
27759 /* If the mmap() above failed, assume that all subsequent mmap() calls
27760 ** will probably fail too. Fall back to using xRead/xWrite exclusively
27761 ** in this case. */
27762 pFd->mmapSizeMax = 0;
27763 }
27764 pFd->pMapRegion = (void *)pNew;
27765 pFd->mmapSize = pFd->mmapSizeActual = nNew;
27766 }
27767 #endif
27768
27769 /*
27770 ** Memory map or remap the file opened by file-descriptor pFd (if the file
27771 ** is already mapped, the existing mapping is replaced by the new). Or, if
27772 ** there already exists a mapping for this file, and there are still
27773 ** outstanding xFetch() references to it, this function is a no-op.
27774 **
27775 ** If parameter nByte is non-negative, then it is the requested size of
27776 ** the mapping to create. Otherwise, if nByte is less than zero, then the
27777 ** requested size is the size of the file on disk. The actual size of the
27778 ** created mapping is either the requested size or the value configured
27779 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
27780 **
27781 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
27782 ** recreated as a result of outstanding references) or an SQLite error
27783 ** code otherwise.
27784 */
27785 static int unixMapfile(unixFile *pFd, i64 nByte){
27786 #if SQLITE_MAX_MMAP_SIZE>0
27787 i64 nMap = nByte;
27788 int rc;
27789
27790 assert( nMap>=0 || pFd->nFetchOut==0 );
27791 if( pFd->nFetchOut>0 ) return SQLITE_OK;
27792
27793 if( nMap<0 ){
27794 struct stat statbuf; /* Low-level file information */
27795 rc = osFstat(pFd->h, &statbuf);
27796 if( rc!=SQLITE_OK ){
27797 return SQLITE_IOERR_FSTAT;
27798 }
27799 nMap = statbuf.st_size;
27800 }
27801 if( nMap>pFd->mmapSizeMax ){
27802 nMap = pFd->mmapSizeMax;
27803 }
27804
27805 if( nMap!=pFd->mmapSize ){
27806 if( nMap>0 ){
27807 unixRemapfile(pFd, nMap);
27808 }else{
27809 unixUnmapfile(pFd);
27810 }
27811 }
27812 #endif
27813
27814 return SQLITE_OK;
27815 }
27816
27817 /*
27818 ** If possible, return a pointer to a mapping of file fd starting at offset
27819 ** iOff. The mapping must be valid for at least nAmt bytes.
27820 **
27821 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
27822 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
27823 ** Finally, if an error does occur, return an SQLite error code. The final
27824 ** value of *pp is undefined in this case.
27825 **
27826 ** If this function does return a pointer, the caller must eventually
27827 ** release the reference by calling unixUnfetch().
27828 */
27829 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
27830 #if SQLITE_MAX_MMAP_SIZE>0
27831 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27832 #endif
27833 *pp = 0;
27834
27835 #if SQLITE_MAX_MMAP_SIZE>0
27836 if( pFd->mmapSizeMax>0 ){
27837 if( pFd->pMapRegion==0 ){
27838 int rc = unixMapfile(pFd, -1);
27839 if( rc!=SQLITE_OK ) return rc;
27840 }
27841 if( pFd->mmapSize >= iOff+nAmt ){
27842 *pp = &((u8 *)pFd->pMapRegion)[iOff];
27843 pFd->nFetchOut++;
27844 }
27845 }
27846 #endif
27847 return SQLITE_OK;
27848 }
27849
27850 /*
27851 ** If the third argument is non-NULL, then this function releases a
27852 ** reference obtained by an earlier call to unixFetch(). The second
27853 ** argument passed to this function must be the same as the corresponding
27854 ** argument that was passed to the unixFetch() invocation.
27855 **
27856 ** Or, if the third argument is NULL, then this function is being called
27857 ** to inform the VFS layer that, according to POSIX, any existing mapping
27858 ** may now be invalid and should be unmapped.
27859 */
27860 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
27861 unixFile *pFd = (unixFile *)fd; /* The underlying database file */
27862 UNUSED_PARAMETER(iOff);
27863
27864 /* If p==0 (unmap the entire file) then there must be no outstanding
27865 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
27866 ** then there must be at least one outstanding. */
27867 assert( (p==0)==(pFd->nFetchOut==0) );
27868
27869 /* If p!=0, it must match the iOff value. */
27870 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
27871
27872 if( p ){
27873 pFd->nFetchOut--;
27874 }else{
27875 unixUnmapfile(pFd);
27876 }
27877
27878 assert( pFd->nFetchOut>=0 );
27879 return SQLITE_OK;
27880 }
27881
27882 /*
27883 ** Here ends the implementation of all sqlite3_file methods.
27884 **
27885 ********************** End sqlite3_file Methods *******************************
@@ -27411,11 +27935,13 @@
27935 unixSectorSize, /* xSectorSize */ \
27936 unixDeviceCharacteristics, /* xDeviceCapabilities */ \
27937 unixShmMap, /* xShmMap */ \
27938 unixShmLock, /* xShmLock */ \
27939 unixShmBarrier, /* xShmBarrier */ \
27940 unixShmUnmap, /* xShmUnmap */ \
27941 unixFetch, /* xFetch */ \
27942 unixUnfetch, /* xUnfetch */ \
27943 }; \
27944 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \
27945 UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \
27946 return &METHOD; \
27947 } \
@@ -27428,11 +27954,11 @@
27954 ** are also created.
27955 */
27956 IOMETHODS(
27957 posixIoFinder, /* Finder function name */
27958 posixIoMethods, /* sqlite3_io_methods object name */
27959 3, /* shared memory and mmap are enabled */
27960 unixClose, /* xClose method */
27961 unixLock, /* xLock method */
27962 unixUnlock, /* xUnlock method */
27963 unixCheckReservedLock /* xCheckReservedLock method */
27964 )
@@ -27679,10 +28205,11 @@
28205 OSTRACE(("OPEN %-3d %s\n", h, zFilename));
28206 pNew->h = h;
28207 pNew->pVfs = pVfs;
28208 pNew->zPath = zFilename;
28209 pNew->ctrlFlags = (u8)ctrlFlags;
28210 pNew->mmapSizeMax = sqlite3GlobalConfig.mxMmap;
28211 if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28212 "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28213 pNew->ctrlFlags |= UNIXFILE_PSOW;
28214 }
28215 if( strcmp(pVfs->zName,"unix-excl")==0 ){
@@ -27823,10 +28350,11 @@
28350 if( rc!=SQLITE_OK ){
28351 if( h>=0 ) robust_close(pNew, h, __LINE__);
28352 }else{
28353 pNew->pMethod = pLockingStyle;
28354 OpenCounter(+1);
28355 verifyDbFile(pNew);
28356 }
28357 return rc;
28358 }
28359
28360 /*
@@ -29916,11 +30444,11 @@
30444 };
30445 unsigned int i; /* Loop counter */
30446
30447 /* Double-check that the aSyscall[] array has been constructed
30448 ** correctly. See ticket [bb3a86e890c8e96ab] */
30449 assert( ArraySize(aSyscall)==24 );
30450
30451 /* Register all VFSes defined in the aVfs[] array */
30452 for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
30453 sqlite3_vfs_register(&aVfs[i], i==0);
30454 }
@@ -30299,15 +30827,24 @@
30827 HANDLE hMutex; /* Mutex used to control access to shared lock */
30828 HANDLE hShared; /* Shared memory segment used for locking */
30829 winceLock local; /* Locks obtained by this instance of winFile */
30830 winceLock *shared; /* Global shared lock memory for the file */
30831 #endif
30832 #if SQLITE_MAX_MMAP_SIZE>0
30833 int nFetchOut; /* Number of outstanding xFetch references */
30834 HANDLE hMap; /* Handle for accessing memory mapping */
30835 void *pMapRegion; /* Area memory mapped */
30836 sqlite3_int64 mmapSize; /* Usable size of mapped region */
30837 sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
30838 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
30839 #endif
30840 };
30841
30842 /*
30843 ** Allowed values for winFile.ctrlFlags
30844 */
30845 #define WINFILE_RDONLY 0x02 /* Connection is read only */
30846 #define WINFILE_PERSIST_WAL 0x04 /* Persistent WAL mode */
30847 #define WINFILE_PSOW 0x10 /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
30848
30849 /*
30850 * The size of the buffer used by sqlite3_win32_write_debug().
@@ -32210,10 +32747,15 @@
32747
32748 return 0;
32749 #endif
32750 }
32751
32752 #if SQLITE_MAX_MMAP_SIZE>0
32753 /* Forward references to VFS methods */
32754 static int winUnmapfile(winFile*);
32755 #endif
32756
32757 /*
32758 ** Close a file.
32759 **
32760 ** It is reported that an attempt to close a handle might sometimes
32761 ** fail. This is a very unreasonable result, but Windows is notorious
@@ -32231,10 +32773,16 @@
32773 #ifndef SQLITE_OMIT_WAL
32774 assert( pFile->pShm==0 );
32775 #endif
32776 OSTRACE(("CLOSE %d\n", pFile->h));
32777 assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
32778
32779 #if SQLITE_MAX_MMAP_SIZE>0
32780 rc = winUnmapfile(pFile);
32781 if( rc!=SQLITE_OK ) return rc;
32782 #endif
32783
32784 do{
32785 rc = osCloseHandle(pFile->h);
32786 /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
32787 }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
32788 #if SQLITE_OS_WINCE
@@ -32279,12 +32827,30 @@
32827 winFile *pFile = (winFile*)id; /* file handle */
32828 DWORD nRead; /* Number of bytes actually read from file */
32829 int nRetry = 0; /* Number of retrys */
32830
32831 assert( id!=0 );
32832 assert( amt>0 );
32833 SimulateIOError(return SQLITE_IOERR_READ);
32834 OSTRACE(("READ %d lock=%d\n", pFile->h, pFile->locktype));
32835
32836 #if SQLITE_MAX_MMAP_SIZE>0
32837 /* Deal with as much of this read request as possible by transfering
32838 ** data from the memory mapping using memcpy(). */
32839 if( offset<pFile->mmapSize ){
32840 if( offset+amt <= pFile->mmapSize ){
32841 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
32842 return SQLITE_OK;
32843 }else{
32844 int nCopy = (int)(pFile->mmapSize - offset);
32845 memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
32846 pBuf = &((u8 *)pBuf)[nCopy];
32847 amt -= nCopy;
32848 offset += nCopy;
32849 }
32850 }
32851 #endif
32852
32853 #if SQLITE_OS_WINCE
32854 if( seekWinFile(pFile, offset) ){
32855 return SQLITE_FULL;
32856 }
@@ -32330,10 +32896,27 @@
32896 assert( pFile );
32897 SimulateIOError(return SQLITE_IOERR_WRITE);
32898 SimulateDiskfullError(return SQLITE_FULL);
32899
32900 OSTRACE(("WRITE %d lock=%d\n", pFile->h, pFile->locktype));
32901
32902 #if SQLITE_MAX_MMAP_SIZE>0
32903 /* Deal with as much of this write request as possible by transfering
32904 ** data from the memory mapping using memcpy(). */
32905 if( offset<pFile->mmapSize ){
32906 if( offset+amt <= pFile->mmapSize ){
32907 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
32908 return SQLITE_OK;
32909 }else{
32910 int nCopy = (int)(pFile->mmapSize - offset);
32911 memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
32912 pBuf = &((u8 *)pBuf)[nCopy];
32913 amt -= nCopy;
32914 offset += nCopy;
32915 }
32916 }
32917 #endif
32918
32919 #if SQLITE_OS_WINCE
32920 rc = seekWinFile(pFile, offset);
32921 if( rc==0 ){
32922 #else
@@ -32398,10 +32981,11 @@
32981 ** Truncate an open file to a specified size
32982 */
32983 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
32984 winFile *pFile = (winFile*)id; /* File handle object */
32985 int rc = SQLITE_OK; /* Return code for this function */
32986 DWORD lastErrno;
32987
32988 assert( pFile );
32989
32990 OSTRACE(("TRUNCATE %d %lld\n", pFile->h, nByte));
32991 SimulateIOError(return SQLITE_IOERR_TRUNCATE);
@@ -32416,16 +33000,27 @@
33000 }
33001
33002 /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33003 if( seekWinFile(pFile, nByte) ){
33004 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33005 "winTruncate1", pFile->zPath);
33006 }else if( 0==osSetEndOfFile(pFile->h) &&
33007 ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33008 pFile->lastErrno = lastErrno;
33009 rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33010 "winTruncate2", pFile->zPath);
33011 }
33012
33013 #if SQLITE_MAX_MMAP_SIZE>0
33014 /* If the file was truncated to a size smaller than the currently
33015 ** mapped region, reduce the effective mapping size as well. SQLite will
33016 ** use read() and write() to access data beyond this point from now on.
33017 */
33018 if( pFile->pMapRegion && nByte<pFile->mmapSize ){
33019 pFile->mmapSize = nByte;
33020 }
33021 #endif
33022
33023 OSTRACE(("TRUNCATE %d %lld %s\n", pFile->h, nByte, rc ? "failed" : "ok"));
33024 return rc;
33025 }
33026
@@ -32790,11 +33385,11 @@
33385 assert( id!=0 );
33386 if( pFile->locktype>=RESERVED_LOCK ){
33387 rc = 1;
33388 OSTRACE(("TEST WR-LOCK %d %d (local)\n", pFile->h, rc));
33389 }else{
33390 rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
33391 if( rc ){
33392 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
33393 }
33394 rc = !rc;
33395 OSTRACE(("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc));
@@ -32930,10 +33525,21 @@
33525 getTempname(pFile->pVfs->mxPathname, zTFile);
33526 *(char**)pArg = zTFile;
33527 }
33528 return SQLITE_OK;
33529 }
33530 #if SQLITE_MAX_MMAP_SIZE>0
33531 case SQLITE_FCNTL_MMAP_SIZE: {
33532 i64 newLimit = *(i64*)pArg;
33533 if( newLimit>sqlite3GlobalConfig.mxMmap ){
33534 newLimit = sqlite3GlobalConfig.mxMmap;
33535 }
33536 *(i64*)pArg = pFile->mmapSizeMax;
33537 if( newLimit>=0 ) pFile->mmapSizeMax = newLimit;
33538 return SQLITE_OK;
33539 }
33540 #endif
33541 }
33542 return SQLITE_NOTFOUND;
33543 }
33544
33545 /*
@@ -33599,10 +34205,196 @@
34205 # define winShmMap 0
34206 # define winShmLock 0
34207 # define winShmBarrier 0
34208 # define winShmUnmap 0
34209 #endif /* #ifndef SQLITE_OMIT_WAL */
34210
34211 /*
34212 ** Cleans up the mapped region of the specified file, if any.
34213 */
34214 #if SQLITE_MAX_MMAP_SIZE>0
34215 static int winUnmapfile(winFile *pFile){
34216 assert( pFile!=0 );
34217 if( pFile->pMapRegion ){
34218 if( !osUnmapViewOfFile(pFile->pMapRegion) ){
34219 pFile->lastErrno = osGetLastError();
34220 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34221 "winUnmap1", pFile->zPath);
34222 }
34223 pFile->pMapRegion = 0;
34224 pFile->mmapSize = 0;
34225 pFile->mmapSizeActual = 0;
34226 }
34227 if( pFile->hMap!=NULL ){
34228 if( !osCloseHandle(pFile->hMap) ){
34229 pFile->lastErrno = osGetLastError();
34230 return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
34231 "winUnmap2", pFile->zPath);
34232 }
34233 pFile->hMap = NULL;
34234 }
34235 return SQLITE_OK;
34236 }
34237
34238 /*
34239 ** Memory map or remap the file opened by file-descriptor pFd (if the file
34240 ** is already mapped, the existing mapping is replaced by the new). Or, if
34241 ** there already exists a mapping for this file, and there are still
34242 ** outstanding xFetch() references to it, this function is a no-op.
34243 **
34244 ** If parameter nByte is non-negative, then it is the requested size of
34245 ** the mapping to create. Otherwise, if nByte is less than zero, then the
34246 ** requested size is the size of the file on disk. The actual size of the
34247 ** created mapping is either the requested size or the value configured
34248 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
34249 **
34250 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
34251 ** recreated as a result of outstanding references) or an SQLite error
34252 ** code otherwise.
34253 */
34254 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
34255 sqlite3_int64 nMap = nByte;
34256 int rc;
34257
34258 assert( nMap>=0 || pFd->nFetchOut==0 );
34259 if( pFd->nFetchOut>0 ) return SQLITE_OK;
34260
34261 if( nMap<0 ){
34262 rc = winFileSize((sqlite3_file*)pFd, &nMap);
34263 if( rc ){
34264 return SQLITE_IOERR_FSTAT;
34265 }
34266 }
34267 if( nMap>pFd->mmapSizeMax ){
34268 nMap = pFd->mmapSizeMax;
34269 }
34270 nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
34271
34272 if( nMap==0 && pFd->mmapSize>0 ){
34273 winUnmapfile(pFd);
34274 }
34275 if( nMap!=pFd->mmapSize ){
34276 void *pNew = 0;
34277 DWORD protect = PAGE_READONLY;
34278 DWORD flags = FILE_MAP_READ;
34279
34280 winUnmapfile(pFd);
34281 if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
34282 protect = PAGE_READWRITE;
34283 flags |= FILE_MAP_WRITE;
34284 }
34285 #if SQLITE_OS_WINRT
34286 pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
34287 #elif defined(SQLITE_WIN32_HAS_WIDE)
34288 pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
34289 (DWORD)((nMap>>32) & 0xffffffff),
34290 (DWORD)(nMap & 0xffffffff), NULL);
34291 #elif defined(SQLITE_WIN32_HAS_ANSI)
34292 pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
34293 (DWORD)((nMap>>32) & 0xffffffff),
34294 (DWORD)(nMap & 0xffffffff), NULL);
34295 #endif
34296 if( pFd->hMap==NULL ){
34297 pFd->lastErrno = osGetLastError();
34298 rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34299 "winMapfile", pFd->zPath);
34300 /* Log the error, but continue normal operation using xRead/xWrite */
34301 return SQLITE_OK;
34302 }
34303 assert( (nMap % winSysInfo.dwPageSize)==0 );
34304 #if SQLITE_OS_WINRT
34305 pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, nMap);
34306 #else
34307 assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
34308 pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
34309 #endif
34310 if( pNew==NULL ){
34311 osCloseHandle(pFd->hMap);
34312 pFd->hMap = NULL;
34313 pFd->lastErrno = osGetLastError();
34314 winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
34315 "winMapfile", pFd->zPath);
34316 return SQLITE_OK;
34317 }
34318 pFd->pMapRegion = pNew;
34319 pFd->mmapSize = nMap;
34320 pFd->mmapSizeActual = nMap;
34321 }
34322
34323 return SQLITE_OK;
34324 }
34325 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
34326
34327 /*
34328 ** If possible, return a pointer to a mapping of file fd starting at offset
34329 ** iOff. The mapping must be valid for at least nAmt bytes.
34330 **
34331 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
34332 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
34333 ** Finally, if an error does occur, return an SQLite error code. The final
34334 ** value of *pp is undefined in this case.
34335 **
34336 ** If this function does return a pointer, the caller must eventually
34337 ** release the reference by calling unixUnfetch().
34338 */
34339 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
34340 #if SQLITE_MAX_MMAP_SIZE>0
34341 winFile *pFd = (winFile*)fd; /* The underlying database file */
34342 #endif
34343 *pp = 0;
34344
34345 #if SQLITE_MAX_MMAP_SIZE>0
34346 if( pFd->mmapSizeMax>0 ){
34347 if( pFd->pMapRegion==0 ){
34348 int rc = winMapfile(pFd, -1);
34349 if( rc!=SQLITE_OK ) return rc;
34350 }
34351 if( pFd->mmapSize >= iOff+nAmt ){
34352 *pp = &((u8 *)pFd->pMapRegion)[iOff];
34353 pFd->nFetchOut++;
34354 }
34355 }
34356 #endif
34357 return SQLITE_OK;
34358 }
34359
34360 /*
34361 ** If the third argument is non-NULL, then this function releases a
34362 ** reference obtained by an earlier call to unixFetch(). The second
34363 ** argument passed to this function must be the same as the corresponding
34364 ** argument that was passed to the unixFetch() invocation.
34365 **
34366 ** Or, if the third argument is NULL, then this function is being called
34367 ** to inform the VFS layer that, according to POSIX, any existing mapping
34368 ** may now be invalid and should be unmapped.
34369 */
34370 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
34371 #if SQLITE_MAX_MMAP_SIZE>0
34372 winFile *pFd = (winFile*)fd; /* The underlying database file */
34373
34374 /* If p==0 (unmap the entire file) then there must be no outstanding
34375 ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
34376 ** then there must be at least one outstanding. */
34377 assert( (p==0)==(pFd->nFetchOut==0) );
34378
34379 /* If p!=0, it must match the iOff value. */
34380 assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
34381
34382 if( p ){
34383 pFd->nFetchOut--;
34384 }else{
34385 /* FIXME: If Windows truly always prevents truncating or deleting a
34386 ** file while a mapping is held, then the following winUnmapfile() call
34387 ** is unnecessary can can be omitted - potentially improving
34388 ** performance. */
34389 winUnmapfile(pFd);
34390 }
34391
34392 assert( pFd->nFetchOut>=0 );
34393 #endif
34394 return SQLITE_OK;
34395 }
34396
34397 /*
34398 ** Here ends the implementation of all sqlite3_file methods.
34399 **
34400 ********************** End sqlite3_file Methods *******************************
@@ -33611,11 +34403,11 @@
34403 /*
34404 ** This vector defines all the methods that can operate on an
34405 ** sqlite3_file for win32.
34406 */
34407 static const sqlite3_io_methods winIoMethod = {
34408 3, /* iVersion */
34409 winClose, /* xClose */
34410 winRead, /* xRead */
34411 winWrite, /* xWrite */
34412 winTruncate, /* xTruncate */
34413 winSync, /* xSync */
@@ -33627,11 +34419,13 @@
34419 winSectorSize, /* xSectorSize */
34420 winDeviceCharacteristics, /* xDeviceCharacteristics */
34421 winShmMap, /* xShmMap */
34422 winShmLock, /* xShmLock */
34423 winShmBarrier, /* xShmBarrier */
34424 winShmUnmap, /* xShmUnmap */
34425 winFetch, /* xFetch */
34426 winUnfetch /* xUnfetch */
34427 };
34428
34429 /****************************************************************************
34430 **************************** sqlite3_vfs methods ****************************
34431 **
@@ -33803,13 +34597,11 @@
34597 #endif
34598
34599 int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE);
34600 int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE);
34601 int isCreate = (flags & SQLITE_OPEN_CREATE);
 
34602 int isReadonly = (flags & SQLITE_OPEN_READONLY);
 
34603 int isReadWrite = (flags & SQLITE_OPEN_READWRITE);
34604
34605 #ifndef NDEBUG
34606 int isOpenJournal = (isCreate && (
34607 eType==SQLITE_OPEN_MASTER_JOURNAL
@@ -34016,15 +34808,25 @@
34808 }
34809
34810 pFile->pMethod = &winIoMethod;
34811 pFile->pVfs = pVfs;
34812 pFile->h = h;
34813 if( isReadonly ){
34814 pFile->ctrlFlags |= WINFILE_RDONLY;
34815 }
34816 if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
34817 pFile->ctrlFlags |= WINFILE_PSOW;
34818 }
34819 pFile->lastErrno = NO_ERROR;
34820 pFile->zPath = zName;
34821 #if SQLITE_MAX_MMAP_SIZE>0
34822 pFile->hMap = NULL;
34823 pFile->pMapRegion = 0;
34824 pFile->mmapSize = 0;
34825 pFile->mmapSizeActual = 0;
34826 pFile->mmapSizeMax = sqlite3GlobalConfig.mxMmap;
34827 #endif
34828
34829 OpenCounter(+1);
34830 return rc;
34831 }
34832
@@ -34649,20 +35451,19 @@
35451
35452 /* Double-check that the aSyscall[] array has been constructed
35453 ** correctly. See ticket [bb3a86e890c8e96ab] */
35454 assert( ArraySize(aSyscall)==74 );
35455
 
35456 /* get memory map allocation granularity */
35457 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
35458 #if SQLITE_OS_WINRT
35459 osGetNativeSystemInfo(&winSysInfo);
35460 #else
35461 osGetSystemInfo(&winSysInfo);
35462 #endif
35463 assert( winSysInfo.dwAllocationGranularity>0 );
35464 assert( winSysInfo.dwPageSize>0 );
35465
35466 sqlite3_vfs_register(&winVfs, 1);
35467 return SQLITE_OK;
35468 }
35469
@@ -37295,11 +38096,10 @@
38096 # define sqlite3WalOpen(x,y,z) 0
38097 # define sqlite3WalLimit(x,y)
38098 # define sqlite3WalClose(w,x,y,z) 0
38099 # define sqlite3WalBeginReadTransaction(y,z) 0
38100 # define sqlite3WalEndReadTransaction(z)
 
38101 # define sqlite3WalDbsize(y) 0
38102 # define sqlite3WalBeginWriteTransaction(y) 0
38103 # define sqlite3WalEndWriteTransaction(x) 0
38104 # define sqlite3WalUndo(x,y,z) 0
38105 # define sqlite3WalSavepoint(y,z)
@@ -37335,11 +38135,12 @@
38135 */
38136 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
38137 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
38138
38139 /* Read a page from the write-ahead log, if it is present. */
38140 SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
38141 SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
38142
38143 /* If the WAL is not empty, return the size of the database. */
38144 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
38145
38146 /* Obtain or release the WRITER lock. */
@@ -38035,10 +38836,15 @@
38836 i64 journalHdr; /* Byte offset to previous journal header */
38837 sqlite3_backup *pBackup; /* Pointer to list of ongoing backup processes */
38838 PagerSavepoint *aSavepoint; /* Array of active savepoints */
38839 int nSavepoint; /* Number of elements in aSavepoint[] */
38840 char dbFileVers[16]; /* Changes whenever database file changes */
38841
38842 u8 bUseFetch; /* True to use xFetch() */
38843 int nMmapOut; /* Number of mmap pages currently outstanding */
38844 sqlite3_int64 szMmap; /* Desired maximum mmap size */
38845 PgHdr *pMmapFreelist; /* List of free mmap page headers (pDirty) */
38846 /*
38847 ** End of the routinely-changing class members
38848 ***************************************************************************/
38849
38850 u16 nExtra; /* Add this many bytes to each in-memory page */
@@ -38145,10 +38951,20 @@
38951 # define MEMDB 0
38952 #else
38953 # define MEMDB pPager->memDb
38954 #endif
38955
38956 /*
38957 ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
38958 ** interfaces to access the database using memory-mapped I/O.
38959 */
38960 #if SQLITE_MAX_MMAP_SIZE>0
38961 # define USEFETCH(x) ((x)->bUseFetch)
38962 #else
38963 # define USEFETCH(x) 0
38964 #endif
38965
38966 /*
38967 ** The maximum legal page number is (2^31 - 1).
38968 */
38969 #define PAGER_MAX_PGNO 2147483647
38970
@@ -39632,11 +40448,11 @@
40448 && isSynced
40449 ){
40450 i64 ofst = (pgno-1)*(i64)pPager->pageSize;
40451 testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
40452 assert( !pagerUseWal(pPager) );
40453 rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
40454 if( pgno>pPager->dbFileSize ){
40455 pPager->dbFileSize = pgno;
40456 }
40457 if( pPager->pBackup ){
40458 CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
@@ -40023,10 +40839,11 @@
40839 Pgno mxPg = 0; /* Size of the original file in pages */
40840 int rc; /* Result code of a subroutine */
40841 int res = 1; /* Value returned by sqlite3OsAccess() */
40842 char *zMaster = 0; /* Name of master journal file if any */
40843 int needPagerReset; /* True to reset page prior to first page rollback */
40844 int nPlayback = 0; /* Total number of pages restored from journal */
40845
40846 /* Figure out how many records are in the journal. Abort early if
40847 ** the journal is empty.
40848 */
40849 assert( isOpen(pPager->jfd) );
@@ -40123,11 +40940,13 @@
40940 if( needPagerReset ){
40941 pager_reset(pPager);
40942 needPagerReset = 0;
40943 }
40944 rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
40945 if( rc==SQLITE_OK ){
40946 nPlayback++;
40947 }else{
40948 if( rc==SQLITE_DONE ){
40949 pPager->journalOff = szJ;
40950 break;
40951 }else if( rc==SQLITE_IOERR_SHORT_READ ){
40952 /* If the journal has been truncated, simply stop reading and
@@ -40193,10 +41012,14 @@
41012 ** see if it is possible to delete the master journal.
41013 */
41014 rc = pager_delmaster(pPager, zMaster);
41015 testcase( rc!=SQLITE_OK );
41016 }
41017 if( isHot && nPlayback ){
41018 sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
41019 nPlayback, pPager->zJournal);
41020 }
41021
41022 /* The Pager.sectorSize variable may have been updated while rolling
41023 ** back a journal created by a process with a different sector size
41024 ** value. Reset it to the correct value for this process.
41025 */
@@ -40214,15 +41037,14 @@
41037 ** the value read from the database file.
41038 **
41039 ** If an IO error occurs, then the IO error is returned to the caller.
41040 ** Otherwise, SQLITE_OK is returned.
41041 */
41042 static int readDbPage(PgHdr *pPg, u32 iFrame){
41043 Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
41044 Pgno pgno = pPg->pgno; /* Page number to read */
41045 int rc = SQLITE_OK; /* Return code */
 
41046 int pgsz = pPager->pageSize; /* Number of bytes to read */
41047
41048 assert( pPager->eState>=PAGER_READER && !MEMDB );
41049 assert( isOpen(pPager->fd) );
41050
@@ -40230,15 +41052,14 @@
41052 assert( pPager->tempFile );
41053 memset(pPg->pData, 0, pPager->pageSize);
41054 return SQLITE_OK;
41055 }
41056
41057 if( iFrame ){
41058 /* Try to pull the page from the write-ahead log. */
41059 rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
41060 }else{
 
41061 i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
41062 rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
41063 if( rc==SQLITE_IOERR_SHORT_READ ){
41064 rc = SQLITE_OK;
41065 }
@@ -40313,16 +41134,21 @@
41134 static int pagerUndoCallback(void *pCtx, Pgno iPg){
41135 int rc = SQLITE_OK;
41136 Pager *pPager = (Pager *)pCtx;
41137 PgHdr *pPg;
41138
41139 assert( pagerUseWal(pPager) );
41140 pPg = sqlite3PagerLookup(pPager, iPg);
41141 if( pPg ){
41142 if( sqlite3PcachePageRefcount(pPg)==1 ){
41143 sqlite3PcacheDrop(pPg);
41144 }else{
41145 u32 iFrame = 0;
41146 rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
41147 if( rc==SQLITE_OK ){
41148 rc = readDbPage(pPg, iFrame);
41149 }
41150 if( rc==SQLITE_OK ){
41151 pPager->xReiniter(pPg);
41152 }
41153 sqlite3PagerUnref(pPg);
41154 }
@@ -40462,10 +41288,11 @@
41288 sqlite3WalEndReadTransaction(pPager->pWal);
41289
41290 rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
41291 if( rc!=SQLITE_OK || changed ){
41292 pager_reset(pPager);
41293 if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
41294 }
41295
41296 return rc;
41297 }
41298 #endif
@@ -40722,10 +41549,33 @@
41549 ** Change the maximum number of in-memory pages that are allowed.
41550 */
41551 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
41552 sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
41553 }
41554
41555 /*
41556 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
41557 */
41558 static void pagerFixMaplimit(Pager *pPager){
41559 #if SQLITE_MAX_MMAP_SIZE>0
41560 sqlite3_file *fd = pPager->fd;
41561 if( isOpen(fd) ){
41562 sqlite3_int64 sz;
41563 pPager->bUseFetch = (fd->pMethods->iVersion>=3) && pPager->szMmap>0;
41564 sz = pPager->szMmap;
41565 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
41566 }
41567 #endif
41568 }
41569
41570 /*
41571 ** Change the maximum size of any memory mapping made of the database file.
41572 */
41573 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
41574 pPager->szMmap = szMmap;
41575 pagerFixMaplimit(pPager);
41576 }
41577
41578 /*
41579 ** Free as much memory as possible from the pager.
41580 */
41581 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
@@ -40958,10 +41808,11 @@
41808 if( rc==SQLITE_OK ){
41809 if( nReserve<0 ) nReserve = pPager->nReserve;
41810 assert( nReserve>=0 && nReserve<1000 );
41811 pPager->nReserve = (i16)nReserve;
41812 pagerReportSize(pPager);
41813 pagerFixMaplimit(pPager);
41814 }
41815 return rc;
41816 }
41817
41818 /*
@@ -41182,10 +42033,85 @@
42033 if( rc==SQLITE_OK ){
42034 rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
42035 }
42036 return rc;
42037 }
42038
42039 /*
42040 ** Obtain a reference to a memory mapped page object for page number pgno.
42041 ** The new object will use the pointer pData, obtained from xFetch().
42042 ** If successful, set *ppPage to point to the new page reference
42043 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set
42044 ** *ppPage to zero.
42045 **
42046 ** Page references obtained by calling this function should be released
42047 ** by calling pagerReleaseMapPage().
42048 */
42049 static int pagerAcquireMapPage(
42050 Pager *pPager, /* Pager object */
42051 Pgno pgno, /* Page number */
42052 void *pData, /* xFetch()'d data for this page */
42053 PgHdr **ppPage /* OUT: Acquired page object */
42054 ){
42055 PgHdr *p; /* Memory mapped page to return */
42056
42057 if( pPager->pMmapFreelist ){
42058 *ppPage = p = pPager->pMmapFreelist;
42059 pPager->pMmapFreelist = p->pDirty;
42060 p->pDirty = 0;
42061 memset(p->pExtra, 0, pPager->nExtra);
42062 }else{
42063 *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
42064 if( p==0 ){
42065 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
42066 return SQLITE_NOMEM;
42067 }
42068 p->pExtra = (void *)&p[1];
42069 p->flags = PGHDR_MMAP;
42070 p->nRef = 1;
42071 p->pPager = pPager;
42072 }
42073
42074 assert( p->pExtra==(void *)&p[1] );
42075 assert( p->pPage==0 );
42076 assert( p->flags==PGHDR_MMAP );
42077 assert( p->pPager==pPager );
42078 assert( p->nRef==1 );
42079
42080 p->pgno = pgno;
42081 p->pData = pData;
42082 pPager->nMmapOut++;
42083
42084 return SQLITE_OK;
42085 }
42086
42087 /*
42088 ** Release a reference to page pPg. pPg must have been returned by an
42089 ** earlier call to pagerAcquireMapPage().
42090 */
42091 static void pagerReleaseMapPage(PgHdr *pPg){
42092 Pager *pPager = pPg->pPager;
42093 pPager->nMmapOut--;
42094 pPg->pDirty = pPager->pMmapFreelist;
42095 pPager->pMmapFreelist = pPg;
42096
42097 assert( pPager->fd->pMethods->iVersion>=3 );
42098 sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
42099 }
42100
42101 /*
42102 ** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
42103 */
42104 static void pagerFreeMapHdrs(Pager *pPager){
42105 PgHdr *p;
42106 PgHdr *pNext;
42107 for(p=pPager->pMmapFreelist; p; p=pNext){
42108 pNext = p->pDirty;
42109 sqlite3_free(p);
42110 }
42111 }
42112
42113
42114 /*
42115 ** Shutdown the page cache. Free all memory and close all files.
42116 **
42117 ** If a transaction was in progress when this routine is called, that
@@ -41203,10 +42129,11 @@
42129 u8 *pTmp = (u8 *)pPager->pTmpSpace;
42130
42131 assert( assert_pager_state(pPager) );
42132 disable_simulated_io_errors();
42133 sqlite3BeginBenignMalloc();
42134 pagerFreeMapHdrs(pPager);
42135 /* pPager->errCode = 0; */
42136 pPager->exclusiveMode = 0;
42137 #ifndef SQLITE_OMIT_WAL
42138 sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
42139 pPager->pWal = 0;
@@ -41464,11 +42391,13 @@
42391
42392 /* Before the first write, give the VFS a hint of what the final
42393 ** file size will be.
42394 */
42395 assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
42396 if( rc==SQLITE_OK
42397 && (pList->pDirty ? pPager->dbSize : pList->pgno+1)>pPager->dbHintSize
42398 ){
42399 sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
42400 sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
42401 pPager->dbHintSize = pPager->dbSize;
42402 }
42403
@@ -42018,10 +42947,11 @@
42947 }
42948 /* pPager->xBusyHandler = 0; */
42949 /* pPager->pBusyHandlerArg = 0; */
42950 pPager->xReiniter = xReinit;
42951 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
42952 /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
42953
42954 *ppPager = pPager;
42955 return SQLITE_OK;
42956 }
42957
@@ -42309,13 +43239,15 @@
43239 assert( (pPager->eLock==SHARED_LOCK)
43240 || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
43241 );
43242 }
43243
43244 if( !pPager->tempFile && (
43245 pPager->pBackup
43246 || sqlite3PcachePagecount(pPager->pPCache)>0
43247 || USEFETCH(pPager)
43248 )){
43249 /* The shared-lock has just been acquired on the database file
43250 ** and there are already pages in the cache (from a previous
43251 ** read or write transaction). Check to see if the database
43252 ** has been modified. If the database has changed, flush the
43253 ** cache.
@@ -42337,19 +43269,29 @@
43269 if( rc ) goto failed;
43270
43271 if( nPage>0 ){
43272 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
43273 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
43274 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
43275 goto failed;
43276 }
43277 }else{
43278 memset(dbFileVers, 0, sizeof(dbFileVers));
43279 }
43280
43281 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
43282 pager_reset(pPager);
43283
43284 /* Unmap the database file. It is possible that external processes
43285 ** may have truncated the database file and then extended it back
43286 ** to its original size while this process was not holding a lock.
43287 ** In this case there may exist a Pager.pMap mapping that appears
43288 ** to be the right size but is not actually valid. Avoid this
43289 ** possibility by unmapping the db here. */
43290 if( USEFETCH(pPager) ){
43291 sqlite3OsUnfetch(pPager->fd, 0, 0);
43292 }
43293 }
43294 }
43295
43296 /* If there is a WAL file in the file-system, open this database in WAL
43297 ** mode. Otherwise, the following function call is a no-op.
@@ -42387,11 +43329,11 @@
43329 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
43330 ** the rollback journal, the unlock is not performed and there is
43331 ** nothing to rollback, so this routine is a no-op.
43332 */
43333 static void pagerUnlockIfUnused(Pager *pPager){
43334 if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
43335 pagerUnlockAndRollback(pPager);
43336 }
43337 }
43338
43339 /*
@@ -42446,17 +43388,31 @@
43388 */
43389 SQLITE_PRIVATE int sqlite3PagerAcquire(
43390 Pager *pPager, /* The pager open on the database file */
43391 Pgno pgno, /* Page number to fetch */
43392 DbPage **ppPage, /* Write a pointer to the page here */
43393 int flags /* PAGER_ACQUIRE_XXX flags */
43394 ){
43395 int rc = SQLITE_OK;
43396 PgHdr *pPg = 0;
43397 u32 iFrame = 0; /* Frame to read from WAL file */
43398 const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);
43399
43400 /* It is acceptable to use a read-only (mmap) page for any page except
43401 ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
43402 ** flag was specified by the caller. And so long as the db is not a
43403 ** temporary or in-memory database. */
43404 const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
43405 && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
43406 #ifdef SQLITE_HAS_CODEC
43407 && pPager->xCodec==0
43408 #endif
43409 );
43410
43411 assert( pPager->eState>=PAGER_READER );
43412 assert( assert_pager_state(pPager) );
43413 assert( noContent==0 || bMmapOk==0 );
43414
43415 if( pgno==0 ){
43416 return SQLITE_CORRUPT_BKPT;
43417 }
43418
@@ -42463,10 +43419,43 @@
43419 /* If the pager is in the error state, return an error immediately.
43420 ** Otherwise, request the page from the PCache layer. */
43421 if( pPager->errCode!=SQLITE_OK ){
43422 rc = pPager->errCode;
43423 }else{
43424
43425 if( bMmapOk && pagerUseWal(pPager) ){
43426 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
43427 if( rc!=SQLITE_OK ) goto pager_acquire_err;
43428 }
43429
43430 if( iFrame==0 && bMmapOk ){
43431 void *pData = 0;
43432
43433 rc = sqlite3OsFetch(pPager->fd,
43434 (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
43435 );
43436
43437 if( rc==SQLITE_OK && pData ){
43438 if( pPager->eState>PAGER_READER ){
43439 (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
43440 }
43441 if( pPg==0 ){
43442 rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
43443 }else{
43444 sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
43445 }
43446 if( pPg ){
43447 assert( rc==SQLITE_OK );
43448 *ppPage = pPg;
43449 return SQLITE_OK;
43450 }
43451 }
43452 if( rc!=SQLITE_OK ){
43453 goto pager_acquire_err;
43454 }
43455 }
43456
43457 rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
43458 }
43459
43460 if( rc!=SQLITE_OK ){
43461 /* Either the call to sqlite3PcacheFetch() returned an error or the
@@ -42521,13 +43510,17 @@
43510 sqlite3EndBenignMalloc();
43511 }
43512 memset(pPg->pData, 0, pPager->pageSize);
43513 IOTRACE(("ZERO %p %d\n", pPager, pgno));
43514 }else{
43515 if( pagerUseWal(pPager) && bMmapOk==0 ){
43516 rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
43517 if( rc!=SQLITE_OK ) goto pager_acquire_err;
43518 }
43519 assert( pPg->pPager==pPager );
43520 pPager->aStat[PAGER_STAT_MISS]++;
43521 rc = readDbPage(pPg, iFrame);
43522 if( rc!=SQLITE_OK ){
43523 goto pager_acquire_err;
43524 }
43525 }
43526 pager_set_pagehash(pPg);
@@ -42576,11 +43569,15 @@
43569 ** removed.
43570 */
43571 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
43572 if( pPg ){
43573 Pager *pPager = pPg->pPager;
43574 if( pPg->flags & PGHDR_MMAP ){
43575 pagerReleaseMapPage(pPg);
43576 }else{
43577 sqlite3PcacheRelease(pPg);
43578 }
43579 pagerUnlockIfUnused(pPager);
43580 }
43581 }
43582
43583 /*
@@ -42911,10 +43908,11 @@
43908
43909 PgHdr *pPg = pDbPage;
43910 Pager *pPager = pPg->pPager;
43911 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
43912
43913 assert( (pPg->flags & PGHDR_MMAP)==0 );
43914 assert( pPager->eState>=PAGER_WRITER_LOCKED );
43915 assert( pPager->eState!=PAGER_ERROR );
43916 assert( assert_pager_state(pPager) );
43917
43918 if( nPagePerSector>1 ){
@@ -43467,11 +44465,11 @@
44465 }else{
44466 rc = pager_playback(pPager, 0);
44467 }
44468
44469 assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
44470 assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
44471 || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR );
44472
44473 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
44474 ** cache. So call pager_error() on the way out to make any error persistent.
44475 */
@@ -44201,15 +45199,16 @@
45199
45200 /* Open the connection to the log file. If this operation fails,
45201 ** (e.g. due to malloc() failure), return an error code.
45202 */
45203 if( rc==SQLITE_OK ){
45204 rc = sqlite3WalOpen(pPager->pVfs,
45205 pPager->fd, pPager->zWal, pPager->exclusiveMode,
45206 pPager->journalSizeLimit, &pPager->pWal
45207 );
45208 }
45209 pagerFixMaplimit(pPager);
45210
45211 return rc;
45212 }
45213
45214
@@ -44296,10 +45295,11 @@
45295 rc = pagerExclusiveLock(pPager);
45296 if( rc==SQLITE_OK ){
45297 rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
45298 pPager->pageSize, (u8*)pPager->pTmpSpace);
45299 pPager->pWal = 0;
45300 pagerFixMaplimit(pPager);
45301 }
45302 }
45303 return rc;
45304 }
45305
@@ -45544,12 +46544,13 @@
46544 ** event via sqlite3_log(). This is to help with identifying performance
46545 ** problems caused by applications routinely shutting down without
46546 ** checkpointing the log file.
46547 */
46548 if( pWal->hdr.nPage ){
46549 sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
46550 "recovered %d frames from WAL file %s",
46551 pWal->hdr.mxFrame, pWal->zWalName
46552 );
46553 }
46554 }
46555
46556 recovery_error:
@@ -46059,20 +47060,21 @@
47060 /* Sync the WAL to disk */
47061 if( sync_flags ){
47062 rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
47063 }
47064
47065 /* If the database may grow as a result of this checkpoint, hint
47066 ** about the eventual size of the db file to the VFS layer.
47067 */
47068 if( rc==SQLITE_OK ){
47069 i64 nReq = ((i64)mxPage * szPage);
47070 rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
47071 if( rc==SQLITE_OK && nSize<nReq ){
47072 sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
47073 }
47074 }
47075
47076
47077 /* Iterate through the contents of the WAL, copying data to the db file. */
47078 while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
47079 i64 iOffset;
47080 assert( walFramePgno(pWal, iFrame)==iDbpage );
@@ -46624,23 +47626,21 @@
47626 pWal->readLock = -1;
47627 }
47628 }
47629
47630 /*
47631 ** Search the wal file for page pgno. If found, set *piRead to the frame that
47632 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
47633 ** to zero.
47634 **
47635 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
47636 ** error does occur, the final value of *piRead is undefined.
 
47637 */
47638 SQLITE_PRIVATE int sqlite3WalFindFrame(
47639 Wal *pWal, /* WAL handle */
47640 Pgno pgno, /* Database page number to read data for */
47641 u32 *piRead /* OUT: Frame number (or zero) */
 
 
47642 ){
47643 u32 iRead = 0; /* If !=0, WAL frame to return data from */
47644 u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */
47645 int iHash; /* Used to loop through N hash tables */
47646
@@ -46652,11 +47652,11 @@
47652 ** in this case as an optimization. Likewise, if pWal->readLock==0,
47653 ** then the WAL is ignored by the reader so return early, as if the
47654 ** WAL were empty.
47655 */
47656 if( iLast==0 || pWal->readLock==0 ){
47657 *piRead = 0;
47658 return SQLITE_OK;
47659 }
47660
47661 /* Search the hash table or tables for an entry matching page number
47662 ** pgno. Each iteration of the following for() loop searches one
@@ -46723,30 +47723,35 @@
47723 }
47724 assert( iRead==iRead2 );
47725 }
47726 #endif
47727
47728 *piRead = iRead;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47729 return SQLITE_OK;
47730 }
47731
47732 /*
47733 ** Read the contents of frame iRead from the wal file into buffer pOut
47734 ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
47735 ** error code otherwise.
47736 */
47737 SQLITE_PRIVATE int sqlite3WalReadFrame(
47738 Wal *pWal, /* WAL handle */
47739 u32 iRead, /* Frame to read */
47740 int nOut, /* Size of buffer pOut in bytes */
47741 u8 *pOut /* Buffer to write page data to */
47742 ){
47743 int sz;
47744 i64 iOffset;
47745 sz = pWal->hdr.szPage;
47746 sz = (sz&0xfe00) + ((sz&0x0001)<<16);
47747 testcase( sz<=32768 );
47748 testcase( sz>=65536 );
47749 iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
47750 /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
47751 return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
47752 }
47753
47754 /*
47755 ** Return the size of the database in pages (or zero, if unknown).
47756 */
47757 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
@@ -47289,10 +48294,13 @@
48294 }
48295
48296 /* Read the wal-index header. */
48297 if( rc==SQLITE_OK ){
48298 rc = walIndexReadHdr(pWal, &isChanged);
48299 if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
48300 sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
48301 }
48302 }
48303
48304 /* Copy data from the log to the database file. */
48305 if( rc==SQLITE_OK ){
48306 if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
@@ -49960,17 +50968,21 @@
50968 */
50969 static int btreeGetPage(
50970 BtShared *pBt, /* The btree */
50971 Pgno pgno, /* Number of the page to fetch */
50972 MemPage **ppPage, /* Return the page in this parameter */
50973 int noContent, /* Do not load page content if true */
50974 int bReadonly /* True if a read-only (mmap) page is ok */
50975 ){
50976 int rc;
50977 DbPage *pDbPage;
50978 int flags = (noContent ? PAGER_ACQUIRE_NOCONTENT : 0)
50979 | (bReadonly ? PAGER_ACQUIRE_READONLY : 0);
50980
50981 assert( noContent==0 || bReadonly==0 );
50982 assert( sqlite3_mutex_held(pBt->mutex) );
50983 rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
50984 if( rc ) return rc;
50985 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
50986 return SQLITE_OK;
50987 }
50988
@@ -50009,21 +51021,22 @@
51021 **
51022 ** If an error occurs, then the value *ppPage is set to is undefined. It
51023 ** may remain unchanged, or it may be set to an invalid value.
51024 */
51025 static int getAndInitPage(
51026 BtShared *pBt, /* The database file */
51027 Pgno pgno, /* Number of the page to get */
51028 MemPage **ppPage, /* Write the page pointer here */
51029 int bReadonly /* True if a read-only (mmap) page is ok */
51030 ){
51031 int rc;
51032 assert( sqlite3_mutex_held(pBt->mutex) );
51033
51034 if( pgno>btreePagecount(pBt) ){
51035 rc = SQLITE_CORRUPT_BKPT;
51036 }else{
51037 rc = btreeGetPage(pBt, pgno, ppPage, 0, bReadonly);
51038 if( rc==SQLITE_OK ){
51039 rc = btreeInitPage(*ppPage);
51040 if( rc!=SQLITE_OK ){
51041 releasePage(*ppPage);
51042 }
@@ -50250,10 +51263,11 @@
51263 goto btree_open_out;
51264 }
51265 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
51266 EXTRA_SIZE, flags, vfsFlags, pageReinit);
51267 if( rc==SQLITE_OK ){
51268 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
51269 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
51270 }
51271 if( rc!=SQLITE_OK ){
51272 goto btree_open_out;
51273 }
@@ -50515,10 +51529,23 @@
51529 sqlite3BtreeEnter(p);
51530 sqlite3PagerSetCachesize(pBt->pPager, mxPage);
51531 sqlite3BtreeLeave(p);
51532 return SQLITE_OK;
51533 }
51534
51535 /*
51536 ** Change the limit on the amount of the database file that may be
51537 ** memory mapped.
51538 */
51539 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
51540 BtShared *pBt = p->pBt;
51541 assert( sqlite3_mutex_held(p->db->mutex) );
51542 sqlite3BtreeEnter(p);
51543 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
51544 sqlite3BtreeLeave(p);
51545 return SQLITE_OK;
51546 }
51547
51548 /*
51549 ** Change the way data is synced to disk in order to increase or decrease
51550 ** how well the database resists damage due to OS crashes and power
51551 ** failures. Level 1 is the same as asynchronous (no syncs() occur and
@@ -50741,11 +51768,11 @@
51768
51769 assert( sqlite3_mutex_held(pBt->mutex) );
51770 assert( pBt->pPage1==0 );
51771 rc = sqlite3PagerSharedLock(pBt->pPager);
51772 if( rc!=SQLITE_OK ) return rc;
51773 rc = btreeGetPage(pBt, 1, &pPage1, 0, 0);
51774 if( rc!=SQLITE_OK ) return rc;
51775
51776 /* Do some checking to help insure the file we opened really is
51777 ** a valid database file.
51778 */
@@ -51300,11 +52327,11 @@
52327 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
52328 ** that it points at iFreePage. Also fix the pointer map entry for
52329 ** iPtrPage.
52330 */
52331 if( eType!=PTRMAP_ROOTPAGE ){
52332 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0, 0);
52333 if( rc!=SQLITE_OK ){
52334 return rc;
52335 }
52336 rc = sqlite3PagerWrite(pPtrPage->pDbPage);
52337 if( rc!=SQLITE_OK ){
@@ -51384,11 +52411,11 @@
52411 Pgno iFreePg; /* Index of free page to move pLastPg to */
52412 MemPage *pLastPg;
52413 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */
52414 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */
52415
52416 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0, 0);
52417 if( rc!=SQLITE_OK ){
52418 return rc;
52419 }
52420
52421 /* If bCommit is zero, this loop runs exactly once and page pLastPg
@@ -51476,12 +52503,15 @@
52503 Pgno nFin = finalDbSize(pBt, nOrig, nFree);
52504
52505 if( nOrig<nFin ){
52506 rc = SQLITE_CORRUPT_BKPT;
52507 }else if( nFree>0 ){
52508 rc = saveAllCursors(pBt, 0, 0);
52509 if( rc==SQLITE_OK ){
52510 invalidateAllOverflowCache(pBt);
52511 rc = incrVacuumStep(pBt, nFin, nOrig, 0);
52512 }
52513 if( rc==SQLITE_OK ){
52514 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
52515 put4byte(&pBt->pPage1->aData[28], pBt->nPage);
52516 }
52517 }else{
@@ -51525,11 +52555,13 @@
52555 }
52556
52557 nFree = get4byte(&pBt->pPage1->aData[36]);
52558 nFin = finalDbSize(pBt, nOrig, nFree);
52559 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
52560 if( nFin<nOrig ){
52561 rc = saveAllCursors(pBt, 0, 0);
52562 }
52563 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
52564 rc = incrVacuumStep(pBt, nFin, iFree, 1);
52565 }
52566 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
52567 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
@@ -51542,11 +52574,11 @@
52574 if( rc!=SQLITE_OK ){
52575 sqlite3PagerRollback(pPager);
52576 }
52577 }
52578
52579 assert( nRef>=sqlite3PagerRefcount(pPager) );
52580 return rc;
52581 }
52582
52583 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
52584 # define setChildPtrmaps(x) SQLITE_OK
@@ -51798,11 +52830,11 @@
52830 }
52831
52832 /* The rollback may have destroyed the pPage1->aData value. So
52833 ** call btreeGetPage() on page 1 again to make
52834 ** sure pPage1->aData is set correctly. */
52835 if( btreeGetPage(pBt, 1, &pPage1, 0, 0)==SQLITE_OK ){
52836 int nPage = get4byte(28+(u8*)pPage1->aData);
52837 testcase( nPage==0 );
52838 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
52839 testcase( pBt->nPage!=nPage );
52840 pBt->nPage = nPage;
@@ -52232,11 +53264,11 @@
53264 }
53265 #endif
53266
53267 assert( next==0 || rc==SQLITE_DONE );
53268 if( rc==SQLITE_OK ){
53269 rc = btreeGetPage(pBt, ovfl, &pPage, 0, (ppPage==0));
53270 assert( rc==SQLITE_OK || pPage==0 );
53271 if( rc==SQLITE_OK ){
53272 next = get4byte(pPage->aData);
53273 }
53274 }
@@ -52453,11 +53485,13 @@
53485 }else
53486 #endif
53487
53488 {
53489 DbPage *pDbPage;
53490 rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
53491 (eOp==0 ? PAGER_ACQUIRE_READONLY : 0)
53492 );
53493 if( rc==SQLITE_OK ){
53494 aPayload = sqlite3PagerGetData(pDbPage);
53495 nextPage = get4byte(aPayload);
53496 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
53497 sqlite3PagerUnref(pDbPage);
@@ -52632,14 +53666,15 @@
53666 BtShared *pBt = pCur->pBt;
53667
53668 assert( cursorHoldsMutex(pCur) );
53669 assert( pCur->eState==CURSOR_VALID );
53670 assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
53671 assert( pCur->iPage>=0 );
53672 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
53673 return SQLITE_CORRUPT_BKPT;
53674 }
53675 rc = getAndInitPage(pBt, newPgno, &pNewPage, (pCur->wrFlag==0));
53676 if( rc ) return rc;
53677 pCur->apPage[i+1] = pNewPage;
53678 pCur->aiIdx[i+1] = 0;
53679 pCur->iPage++;
53680
@@ -52752,11 +53787,11 @@
53787 pCur->iPage = 0;
53788 }else if( pCur->pgnoRoot==0 ){
53789 pCur->eState = CURSOR_INVALID;
53790 return SQLITE_OK;
53791 }else{
53792 rc = getAndInitPage(pBt, pCur->pgnoRoot, &pCur->apPage[0], pCur->wrFlag==0);
53793 if( rc!=SQLITE_OK ){
53794 pCur->eState = CURSOR_INVALID;
53795 return rc;
53796 }
53797 pCur->iPage = 0;
@@ -53366,11 +54401,11 @@
54401 }
54402 testcase( iTrunk==mxPage );
54403 if( iTrunk>mxPage ){
54404 rc = SQLITE_CORRUPT_BKPT;
54405 }else{
54406 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
54407 }
54408 if( rc ){
54409 pTrunk = 0;
54410 goto end_allocate_page;
54411 }
@@ -53430,11 +54465,11 @@
54465 if( iNewTrunk>mxPage ){
54466 rc = SQLITE_CORRUPT_BKPT;
54467 goto end_allocate_page;
54468 }
54469 testcase( iNewTrunk==mxPage );
54470 rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0, 0);
54471 if( rc!=SQLITE_OK ){
54472 goto end_allocate_page;
54473 }
54474 rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
54475 if( rc!=SQLITE_OK ){
@@ -53510,11 +54545,11 @@
54545 if( closest<k-1 ){
54546 memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
54547 }
54548 put4byte(&aData[4], k-1);
54549 noContent = !btreeGetHasContent(pBt, *pPgno);
54550 rc = btreeGetPage(pBt, *pPgno, ppPage, noContent, 0);
54551 if( rc==SQLITE_OK ){
54552 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54553 if( rc!=SQLITE_OK ){
54554 releasePage(*ppPage);
54555 }
@@ -53558,11 +54593,11 @@
54593 ** becomes a new pointer-map page, the second is used by the caller.
54594 */
54595 MemPage *pPg = 0;
54596 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
54597 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
54598 rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent, 0);
54599 if( rc==SQLITE_OK ){
54600 rc = sqlite3PagerWrite(pPg->pDbPage);
54601 releasePage(pPg);
54602 }
54603 if( rc ) return rc;
@@ -53572,11 +54607,11 @@
54607 #endif
54608 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
54609 *pPgno = pBt->nPage;
54610
54611 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
54612 rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent, 0);
54613 if( rc ) return rc;
54614 rc = sqlite3PagerWrite((*ppPage)->pDbPage);
54615 if( rc!=SQLITE_OK ){
54616 releasePage(*ppPage);
54617 }
@@ -53640,11 +54675,11 @@
54675
54676 if( pBt->btsFlags & BTS_SECURE_DELETE ){
54677 /* If the secure_delete option is enabled, then
54678 ** always fully overwrite deleted information with zeros.
54679 */
54680 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0, 0))!=0) )
54681 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
54682 ){
54683 goto freepage_out;
54684 }
54685 memset(pPage->aData, 0, pPage->pBt->pageSize);
@@ -53667,11 +54702,11 @@
54702 */
54703 if( nFree!=0 ){
54704 u32 nLeaf; /* Initial number of leaf cells on trunk page */
54705
54706 iTrunk = get4byte(&pPage1->aData[32]);
54707 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0, 0);
54708 if( rc!=SQLITE_OK ){
54709 goto freepage_out;
54710 }
54711
54712 nLeaf = get4byte(&pTrunk->aData[4]);
@@ -53713,11 +54748,11 @@
54748 ** the page being freed as a leaf page of the first trunk in the free-list.
54749 ** Possibly because the free-list is empty, or possibly because the
54750 ** first trunk in the free-list is full. Either way, the page being freed
54751 ** will become the new first trunk page in the free-list.
54752 */
54753 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0, 0)) ){
54754 goto freepage_out;
54755 }
54756 rc = sqlite3PagerWrite(pPage->pDbPage);
54757 if( rc!=SQLITE_OK ){
54758 goto freepage_out;
@@ -54514,11 +55549,11 @@
55549 }else{
55550 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
55551 }
55552 pgno = get4byte(pRight);
55553 while( 1 ){
55554 rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
55555 if( rc ){
55556 memset(apOld, 0, (i+1)*sizeof(MemPage*));
55557 goto balance_cleanup;
55558 }
55559 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
@@ -55602,14 +56637,21 @@
56637 ** is already journaled.
56638 */
56639 u8 eType = 0;
56640 Pgno iPtrPage = 0;
56641
56642 /* Save the positions of any open cursors. This is required in
56643 ** case they are holding a reference to an xFetch reference
56644 ** corresponding to page pgnoRoot. */
56645 rc = saveAllCursors(pBt, 0, 0);
56646 releasePage(pPageMove);
56647 if( rc!=SQLITE_OK ){
56648 return rc;
56649 }
56650
56651 /* Move the page currently at pgnoRoot to pgnoMove. */
56652 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
56653 if( rc!=SQLITE_OK ){
56654 return rc;
56655 }
56656 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
56657 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
@@ -55626,11 +56668,11 @@
56668
56669 /* Obtain the page at pgnoRoot */
56670 if( rc!=SQLITE_OK ){
56671 return rc;
56672 }
56673 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0, 0);
56674 if( rc!=SQLITE_OK ){
56675 return rc;
56676 }
56677 rc = sqlite3PagerWrite(pRoot->pDbPage);
56678 if( rc!=SQLITE_OK ){
@@ -55702,11 +56744,11 @@
56744 assert( sqlite3_mutex_held(pBt->mutex) );
56745 if( pgno>btreePagecount(pBt) ){
56746 return SQLITE_CORRUPT_BKPT;
56747 }
56748
56749 rc = getAndInitPage(pBt, pgno, &pPage, 0);
56750 if( rc ) return rc;
56751 for(i=0; i<pPage->nCell; i++){
56752 pCell = findCell(pPage, i);
56753 if( !pPage->leaf ){
56754 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
@@ -55804,11 +56846,11 @@
56846 if( NEVER(pBt->pCursor) ){
56847 sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
56848 return SQLITE_LOCKED_SHAREDCACHE;
56849 }
56850
56851 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0, 0);
56852 if( rc ) return rc;
56853 rc = sqlite3BtreeClearTable(p, iTable, 0);
56854 if( rc ){
56855 releasePage(pPage);
56856 return rc;
@@ -55839,21 +56881,21 @@
56881 ** number in the database. So move the page that does into the
56882 ** gap left by the deleted root-page.
56883 */
56884 MemPage *pMove;
56885 releasePage(pPage);
56886 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
56887 if( rc!=SQLITE_OK ){
56888 return rc;
56889 }
56890 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
56891 releasePage(pMove);
56892 if( rc!=SQLITE_OK ){
56893 return rc;
56894 }
56895 pMove = 0;
56896 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0, 0);
56897 freePage(pMove, &rc);
56898 releasePage(pMove);
56899 if( rc!=SQLITE_OK ){
56900 return rc;
56901 }
@@ -56261,11 +57303,11 @@
57303 */
57304 pBt = pCheck->pBt;
57305 usableSize = pBt->usableSize;
57306 if( iPage==0 ) return 0;
57307 if( checkRef(pCheck, iPage, zParentContext) ) return 0;
57308 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0, 0))!=0 ){
57309 checkAppendMsg(pCheck, zContext,
57310 "unable to get the page. error code=%d", rc);
57311 return 0;
57312 }
57313
@@ -56732,10 +57774,21 @@
57774 }
57775 assert( pCsr->eState!=CURSOR_REQUIRESEEK );
57776 if( pCsr->eState!=CURSOR_VALID ){
57777 return SQLITE_ABORT;
57778 }
57779
57780 /* Save the positions of all other cursors open on this table. This is
57781 ** required in case any of them are holding references to an xFetch
57782 ** version of the b-tree page modified by the accessPayload call below.
57783 **
57784 ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
57785 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
57786 ** saveAllCursors can only return SQLITE_OK.
57787 */
57788 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
57789 assert( rc==SQLITE_OK );
57790
57791 /* Check some assumptions:
57792 ** (a) the cursor is open for writing,
57793 ** (b) there is a read/write transaction open,
57794 ** (c) the connection holds a write-lock on the table (if required),
@@ -57214,11 +58267,12 @@
58267 assert( nSrcPage>=0 );
58268 for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
58269 const Pgno iSrcPg = p->iNext; /* Source page number */
58270 if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
58271 DbPage *pSrcPg; /* Source page object */
58272 rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
58273 PAGER_ACQUIRE_READONLY);
58274 if( rc==SQLITE_OK ){
58275 rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
58276 sqlite3PagerUnref(pSrcPg);
58277 }
58278 }
@@ -62437,18 +63491,10 @@
63491 rc = sqlite3VdbeTransferError(p);
63492 }
63493 return (rc&db->errMask);
63494 }
63495
 
 
 
 
 
 
 
 
63496 /*
63497 ** This is the top-level implementation of sqlite3_step(). Call
63498 ** sqlite3Step() to do most of the work. If a schema error occurs,
63499 ** call sqlite3Reprepare() and try again.
63500 */
@@ -63347,10 +64393,15 @@
64393 ** obtained from sqlite3DbMalloc(). If sqlite3.vdbeExecCnt is 1, then the
64394 ** string contains a copy of zRawSql but with host parameters expanded to
64395 ** their current bindings. Or, if sqlite3.vdbeExecCnt is greater than 1,
64396 ** then the returned string holds a copy of zRawSql with "-- " prepended
64397 ** to each line of text.
64398 **
64399 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
64400 ** then long strings and blobs are truncated to that many bytes. This
64401 ** can be used to prevent unreasonably large trace strings when dealing
64402 ** with large (multi-megabyte) strings and blobs.
64403 **
64404 ** The calling function is responsible for making sure the memory returned
64405 ** is eventually freed.
64406 **
64407 ** ALGORITHM: Scan the input string looking for host parameters in any of
@@ -63418,34 +64469,53 @@
64469 }else if( pVar->flags & MEM_Int ){
64470 sqlite3XPrintf(&out, "%lld", pVar->u.i);
64471 }else if( pVar->flags & MEM_Real ){
64472 sqlite3XPrintf(&out, "%!.15g", pVar->r);
64473 }else if( pVar->flags & MEM_Str ){
64474 int nOut; /* Number of bytes of the string text to include in output */
64475 #ifndef SQLITE_OMIT_UTF16
64476 u8 enc = ENC(db);
64477 Mem utf8;
64478 if( enc!=SQLITE_UTF8 ){
 
64479 memset(&utf8, 0, sizeof(utf8));
64480 utf8.db = db;
64481 sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
64482 sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
64483 pVar = &utf8;
64484 }
 
64485 #endif
64486 nOut = pVar->n;
64487 #ifdef SQLITE_TRACE_SIZE_LIMIT
64488 if( n>SQLITE_TRACE_SIZE_LIMIT ){
64489 nOut = SQLITE_TRACE_SIZE_LIMIT;
64490 while( nOut<pVar->n && (pVar->z[n]&0xc0)==0x80 ){ n++; }
64491 }
64492 #endif
64493 sqlite3XPrintf(&out, "'%.*q'", nOut, pVar->z);
64494 #ifdef SQLITE_TRACE_SIZE_LIMIT
64495 if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n);
64496 #endif
64497 #ifndef SQLITE_OMIT_UTF16
64498 if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
64499 #endif
64500 }else if( pVar->flags & MEM_Zero ){
64501 sqlite3XPrintf(&out, "zeroblob(%d)", pVar->u.nZero);
64502 }else{
64503 int nOut; /* Number of bytes of the blob to include in output */
64504 assert( pVar->flags & MEM_Blob );
64505 sqlite3StrAccumAppend(&out, "x'", 2);
64506 nOut = pVar->n;
64507 #ifdef SQLITE_TRACE_SIZE_LIMIT
64508 if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
64509 #endif
64510 for(i=0; i<nOut; i++){
64511 sqlite3XPrintf(&out, "%02x", pVar->z[i]&0xff);
64512 }
64513 sqlite3StrAccumAppend(&out, "'", 1);
64514 #ifdef SQLITE_TRACE_SIZE_LIMIT
64515 if( nOut<pVar->n ) sqlite3XPrintf(&out, "/*+%d bytes*/", pVar->n-n);
64516 #endif
64517 }
64518 }
64519 }
64520 return sqlite3StrAccumFinish(&out);
64521 }
@@ -67658,11 +68728,11 @@
68728 ** u.bc.r.flags = UNPACKED_INCRKEY;
68729 ** }else{
68730 ** u.bc.r.flags = 0;
68731 ** }
68732 */
68733 u.bc.r.flags = (u8)(UNPACKED_INCRKEY * (1 & (u.bc.oc - OP_SeekLt)));
68734 assert( u.bc.oc!=OP_SeekGt || u.bc.r.flags==UNPACKED_INCRKEY );
68735 assert( u.bc.oc!=OP_SeekLe || u.bc.r.flags==UNPACKED_INCRKEY );
68736 assert( u.bc.oc!=OP_SeekGe || u.bc.r.flags==0 );
68737 assert( u.bc.oc!=OP_SeekLt || u.bc.r.flags==0 );
68738
@@ -70783,11 +71853,11 @@
71853 if( db->mallocFailed ){
71854 goto blob_open_out;
71855 }
71856 sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
71857 rc = blobSeekToRow(pBlob, iRow, &zErr);
71858 } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
71859
71860 blob_open_out:
71861 if( rc==SQLITE_OK && db->mallocFailed==0 ){
71862 *ppBlob = (sqlite3_blob *)pBlob;
71863 }else{
@@ -72468,11 +73538,13 @@
73538 0, /* xSectorSize */
73539 0, /* xDeviceCharacteristics */
73540 0, /* xShmMap */
73541 0, /* xShmLock */
73542 0, /* xShmBarrier */
73543 0, /* xShmUnmap */
73544 0, /* xFetch */
73545 0 /* xUnfetch */
73546 };
73547
73548 /*
73549 ** Open a journal file.
73550 */
@@ -72612,11 +73684,13 @@
73684 }
73685
73686 /*
73687 ** Call sqlite3WalkExpr() for every expression in Select statement p.
73688 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
73689 ** on the compound select chain, p->pPrior. Invoke the xSelectCallback()
73690 ** either before or after the walk of expressions and FROM clause, depending
73691 ** on whether pWalker->bSelectDepthFirst is false or true, respectively.
73692 **
73693 ** Return WRC_Continue under normal conditions. Return WRC_Abort if
73694 ** there is an abort request.
73695 **
73696 ** If the Walker does not have an xSelectCallback() then this routine
@@ -72626,17 +73700,26 @@
73700 int rc;
73701 if( p==0 || pWalker->xSelectCallback==0 ) return WRC_Continue;
73702 rc = WRC_Continue;
73703 pWalker->walkerDepth++;
73704 while( p ){
73705 if( !pWalker->bSelectDepthFirst ){
73706 rc = pWalker->xSelectCallback(pWalker, p);
73707 if( rc ) break;
73708 }
73709 if( sqlite3WalkSelectExpr(pWalker, p)
73710 || sqlite3WalkSelectFrom(pWalker, p)
73711 ){
73712 pWalker->walkerDepth--;
73713 return WRC_Abort;
73714 }
73715 if( pWalker->bSelectDepthFirst ){
73716 rc = pWalker->xSelectCallback(pWalker, p);
73717 /* Depth-first search is currently only used for
73718 ** selectAddSubqueryTypeInfo() and that routine always returns
73719 ** WRC_Continue (0). So the following branch is never taken. */
73720 if( NEVER(rc) ) break;
73721 }
73722 p = p->pPrior;
73723 }
73724 pWalker->walkerDepth--;
73725 return rc & WRC_Abort;
@@ -73031,11 +74114,14 @@
74114 ** In cases like this, replace pExpr with a copy of the expression that
74115 ** forms the result set entry ("a+b" in the example) and return immediately.
74116 ** Note that the expression in the result set should have already been
74117 ** resolved by the time the WHERE clause is resolved.
74118 */
74119 if( (pEList = pNC->pEList)!=0
74120 && zTab==0
74121 && ((pNC->ncFlags & NC_AsMaybe)==0 || cnt==0)
74122 ){
74123 for(j=0; j<pEList->nExpr; j++){
74124 char *zAs = pEList->a[j].zName;
74125 if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
74126 Expr *pOrig;
74127 assert( pExpr->pLeft==0 && pExpr->pRight==0 );
@@ -73122,11 +74208,13 @@
74208 pExpr->pRight = 0;
74209 pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
74210 lookupname_end:
74211 if( cnt==1 ){
74212 assert( pNC!=0 );
74213 if( pExpr->op!=TK_AS ){
74214 sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
74215 }
74216 /* Increment the nRef value on all name contexts from TopNC up to
74217 ** the point where the name matched. */
74218 for(;;){
74219 assert( pTopNC!=0 );
74220 pTopNC->nRef++;
@@ -73797,15 +74885,14 @@
74885 **
74886 ** Minor point: If this is the case, then the expression will be
74887 ** re-evaluated for each reference to it.
74888 */
74889 sNC.pEList = p->pEList;
74890 if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
74891 sNC.ncFlags |= NC_AsMaybe;
74892 if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
74893 sNC.ncFlags &= ~NC_AsMaybe;
 
74894
74895 /* The ORDER BY and GROUP BY clauses may not refer to terms in
74896 ** outer queries
74897 */
74898 sNC.pNext = 0;
@@ -73922,10 +75009,11 @@
75009 pParse->nHeight += pExpr->nHeight;
75010 }
75011 #endif
75012 savedHasAgg = pNC->ncFlags & NC_HasAgg;
75013 pNC->ncFlags &= ~NC_HasAgg;
75014 memset(&w, 0, sizeof(w));
75015 w.xExprCallback = resolveExprStep;
75016 w.xSelectCallback = resolveSelectStep;
75017 w.pParse = pNC->pParse;
75018 w.u.pNC = pNC;
75019 sqlite3WalkExpr(&w, pExpr);
@@ -73962,10 +75050,11 @@
75050 NameContext *pOuterNC /* Name context for parent SELECT statement */
75051 ){
75052 Walker w;
75053
75054 assert( p!=0 );
75055 memset(&w, 0, sizeof(w));
75056 w.xExprCallback = resolveExprStep;
75057 w.xSelectCallback = resolveSelectStep;
75058 w.pParse = pParse;
75059 w.u.pNC = pOuterNC;
75060 sqlite3WalkSelect(&w, p);
@@ -75186,10 +76275,11 @@
76275 pWalker->u.i = 0;
76276 return WRC_Abort;
76277 }
76278 static int exprIsConst(Expr *p, int initFlag){
76279 Walker w;
76280 memset(&w, 0, sizeof(w));
76281 w.u.i = initFlag;
76282 w.xExprCallback = exprNodeIsConstant;
76283 w.xSelectCallback = selectNodeIsConstant;
76284 sqlite3WalkExpr(&w, p);
76285 return w.u.i;
@@ -77400,12 +78490,12 @@
78490 */
78491 SQLITE_PRIVATE void sqlite3ExprCodeConstants(Parse *pParse, Expr *pExpr){
78492 Walker w;
78493 if( pParse->cookieGoto ) return;
78494 if( OptimizationDisabled(pParse->db, SQLITE_FactorOutConst) ) return;
78495 memset(&w, 0, sizeof(w));
78496 w.xExprCallback = evalConstExpr;
 
78497 w.pParse = pParse;
78498 sqlite3WalkExpr(&w, pExpr);
78499 }
78500
78501
@@ -86603,10 +87693,17 @@
87693 prevEscape = 0;
87694 }
87695 }
87696 return *zString==0;
87697 }
87698
87699 /*
87700 ** The sqlite3_strglob() interface.
87701 */
87702 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
87703 return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
87704 }
87705
87706 /*
87707 ** Count the number of times that the LIKE operator (or GLOB which is
87708 ** just a variation of LIKE) gets called. This is used for testing
87709 ** only.
@@ -90804,24 +91901,23 @@
91901 ){
91902 int rc = SQLITE_OK; /* Return code */
91903 const char *zLeftover; /* Tail of unprocessed SQL */
91904 sqlite3_stmt *pStmt = 0; /* The current SQL statement */
91905 char **azCols = 0; /* Names of result columns */
 
91906 int callbackIsInit; /* True if callback data is initialized */
91907
91908 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
91909 if( zSql==0 ) zSql = "";
91910
91911 sqlite3_mutex_enter(db->mutex);
91912 sqlite3Error(db, SQLITE_OK, 0);
91913 while( rc==SQLITE_OK && zSql[0] ){
91914 int nCol;
91915 char **azVals = 0;
91916
91917 pStmt = 0;
91918 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
91919 assert( rc==SQLITE_OK || pStmt==0 );
91920 if( rc!=SQLITE_OK ){
91921 continue;
91922 }
91923 if( !pStmt ){
@@ -90874,15 +91970,12 @@
91970 }
91971
91972 if( rc!=SQLITE_ROW ){
91973 rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
91974 pStmt = 0;
91975 zSql = zLeftover;
91976 while( sqlite3Isspace(zSql[0]) ) zSql++;
 
 
 
91977 break;
91978 }
91979 }
91980
91981 sqlite3DbFree(db, azCols);
@@ -91402,12 +92495,21 @@
92495 #define sqlite3_uri_parameter sqlite3_api->uri_parameter
92496 #define sqlite3_uri_vsnprintf sqlite3_api->vsnprintf
92497 #define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
92498 #endif /* SQLITE_CORE */
92499
92500 #ifndef SQLITE_CORE
92501 /* This case when the file really is being compiled as a loadable
92502 ** extension */
92503 # define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
92504 # define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
92505 #else
92506 /* This case when the file is being statically linked into the
92507 ** application */
92508 # define SQLITE_EXTENSION_INIT1 /*no-op*/
92509 # define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
92510 #endif
92511
92512 #endif /* _SQLITE3EXT_H_ */
92513
92514 /************** End of sqlite3ext.h ******************************************/
92515 /************** Continuing where we left off in loadext.c ********************/
@@ -91806,12 +92908,27 @@
92908 ){
92909 sqlite3_vfs *pVfs = db->pVfs;
92910 void *handle;
92911 int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
92912 char *zErrmsg = 0;
92913 const char *zEntry;
92914 char *zAltEntry = 0;
92915 void **aHandle;
92916 int nMsg = 300 + sqlite3Strlen30(zFile);
92917 int ii;
92918
92919 /* Shared library endings to try if zFile cannot be loaded as written */
92920 static const char *azEndings[] = {
92921 #if SQLITE_OS_WIN
92922 "dll"
92923 #elif defined(__APPLE__)
92924 "dylib"
92925 #else
92926 "so"
92927 #endif
92928 };
92929
92930
92931 if( pzErrMsg ) *pzErrMsg = 0;
92932
92933 /* Ticket #1863. To avoid a creating security problems for older
92934 ** applications that relink against newer versions of SQLite, the
@@ -91824,15 +92941,21 @@
92941 *pzErrMsg = sqlite3_mprintf("not authorized");
92942 }
92943 return SQLITE_ERROR;
92944 }
92945
92946 zEntry = zProc ? zProc : "sqlite3_extension_init";
 
 
92947
92948 handle = sqlite3OsDlOpen(pVfs, zFile);
92949 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
92950 for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
92951 char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
92952 if( zAltFile==0 ) return SQLITE_NOMEM;
92953 handle = sqlite3OsDlOpen(pVfs, zAltFile);
92954 sqlite3_free(zAltFile);
92955 }
92956 #endif
92957 if( handle==0 ){
92958 if( pzErrMsg ){
92959 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
92960 if( zErrmsg ){
92961 sqlite3_snprintf(nMsg, zErrmsg,
@@ -91841,24 +92964,61 @@
92964 }
92965 }
92966 return SQLITE_ERROR;
92967 }
92968 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
92969 sqlite3OsDlSym(pVfs, handle, zEntry);
92970
92971 /* If no entry point was specified and the default legacy
92972 ** entry point name "sqlite3_extension_init" was not found, then
92973 ** construct an entry point name "sqlite3_X_init" where the X is
92974 ** replaced by the lowercase value of every ASCII alphabetic
92975 ** character in the filename after the last "/" upto the first ".",
92976 ** and eliding the first three characters if they are "lib".
92977 ** Examples:
92978 **
92979 ** /usr/local/lib/libExample5.4.3.so ==> sqlite3_example_init
92980 ** C:/lib/mathfuncs.dll ==> sqlite3_mathfuncs_init
92981 */
92982 if( xInit==0 && zProc==0 ){
92983 int iFile, iEntry, c;
92984 int ncFile = sqlite3Strlen30(zFile);
92985 zAltEntry = sqlite3_malloc(ncFile+30);
92986 if( zAltEntry==0 ){
92987 sqlite3OsDlClose(pVfs, handle);
92988 return SQLITE_NOMEM;
92989 }
92990 memcpy(zAltEntry, "sqlite3_", 8);
92991 for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
92992 iFile++;
92993 if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
92994 for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
92995 if( sqlite3Isalpha(c) ){
92996 zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
92997 }
92998 }
92999 memcpy(zAltEntry+iEntry, "_init", 6);
93000 zEntry = zAltEntry;
93001 xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
93002 sqlite3OsDlSym(pVfs, handle, zEntry);
93003 }
93004 if( xInit==0 ){
93005 if( pzErrMsg ){
93006 nMsg += sqlite3Strlen30(zEntry);
93007 *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
93008 if( zErrmsg ){
93009 sqlite3_snprintf(nMsg, zErrmsg,
93010 "no entry point [%s] in shared library [%s]", zEntry, zFile);
93011 sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
93012 }
 
93013 }
93014 sqlite3OsDlClose(pVfs, handle);
93015 sqlite3_free(zAltEntry);
93016 return SQLITE_ERROR;
93017 }
93018 sqlite3_free(zAltEntry);
93019 if( xInit(db, &zErrmsg, &sqlite3Apis) ){
93020 if( pzErrMsg ){
93021 *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
93022 }
93023 sqlite3_free(zErrmsg);
93024 sqlite3OsDlClose(pVfs, handle);
@@ -92383,11 +93543,11 @@
93543 int iDb; /* Database index for <database> */
93544 char *aFcntl[4]; /* Argument to SQLITE_FCNTL_PRAGMA */
93545 int rc; /* return value form SQLITE_FCNTL_PRAGMA */
93546 sqlite3 *db = pParse->db; /* The database connection */
93547 Db *pDb; /* The specific database being pragmaed */
93548 Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
93549
93550 if( v==0 ) return;
93551 sqlite3VdbeRunOnlyOnce(v);
93552 pParse->nMem = 2;
93553
@@ -92466,15 +93626,16 @@
93626 */
93627 if( sqlite3StrICmp(zLeft,"default_cache_size")==0 ){
93628 static const VdbeOpList getCacheSize[] = {
93629 { OP_Transaction, 0, 0, 0}, /* 0 */
93630 { OP_ReadCookie, 0, 1, BTREE_DEFAULT_CACHE_SIZE}, /* 1 */
93631 { OP_IfPos, 1, 8, 0},
93632 { OP_Integer, 0, 2, 0},
93633 { OP_Subtract, 1, 2, 1},
93634 { OP_IfPos, 1, 8, 0},
93635 { OP_Integer, 0, 1, 0}, /* 6 */
93636 { OP_Noop, 0, 0, 0},
93637 { OP_ResultRow, 1, 1, 0},
93638 };
93639 int addr;
93640 if( sqlite3ReadSchema(pParse) ) goto pragma_out;
93641 sqlite3VdbeUsesBtree(v, iDb);
@@ -92808,10 +93969,47 @@
93969 pDb->pSchema->cache_size = size;
93970 sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
93971 }
93972 }else
93973
93974 /*
93975 ** PRAGMA [database.]mmap_size(N)
93976 **
93977 ** Used to set mapping size limit. The mapping size limit is
93978 ** used to limit the aggregate size of all memory mapped regions of the
93979 ** database file. If this parameter is set to zero, then memory mapping
93980 ** is not used at all. If N is negative, then the default memory map
93981 ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
93982 ** The parameter N is measured in bytes.
93983 **
93984 ** This value is advisory. The underlying VFS is free to memory map
93985 ** as little or as much as it wants. Except, if N is set to 0 then the
93986 ** upper layers will never invoke the xFetch interfaces to the VFS.
93987 */
93988 if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
93989 sqlite3_int64 sz;
93990 assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
93991 if( zRight ){
93992 int ii;
93993 sqlite3Atoi64(zRight, &sz, 1000, SQLITE_UTF8);
93994 if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
93995 if( pId2->n==0 ) db->szMmap = sz;
93996 for(ii=db->nDb-1; ii>=0; ii--){
93997 if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
93998 sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
93999 }
94000 }
94001 }
94002 sz = -1;
94003 if( sqlite3_file_control(db,zDb,SQLITE_FCNTL_MMAP_SIZE,&sz)==SQLITE_OK ){
94004 #if SQLITE_MAX_MMAP_SIZE==0
94005 sz = 0;
94006 #endif
94007 returnSingleInt(pParse, "mmap_size", sz);
94008 }
94009 }else
94010
94011 /*
94012 ** PRAGMA temp_store
94013 ** PRAGMA temp_store = "default"|"memory"|"file"
94014 **
94015 ** Return or set the local value of the temp_store flag. Changing
@@ -94498,11 +95696,10 @@
95696 azColName[i], SQLITE_STATIC);
95697 }
95698 }
95699 #endif
95700
 
95701 if( db->init.busy==0 ){
95702 Vdbe *pVdbe = pParse->pVdbe;
95703 sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
95704 }
95705 if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
@@ -98290,10 +99487,11 @@
99487 ** The calling function can detect the problem by looking at pParse->nErr
99488 ** and/or pParse->db->mallocFailed.
99489 */
99490 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
99491 Walker w;
99492 memset(&w, 0, sizeof(w));
99493 w.xSelectCallback = selectExpander;
99494 w.xExprCallback = exprWalkNoop;
99495 w.pParse = pParse;
99496 sqlite3WalkSelect(&w, pSelect);
99497 }
@@ -98348,13 +99546,15 @@
99546 ** Use this routine after name resolution.
99547 */
99548 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
99549 #ifndef SQLITE_OMIT_SUBQUERY
99550 Walker w;
99551 memset(&w, 0, sizeof(w));
99552 w.xSelectCallback = selectAddSubqueryTypeInfo;
99553 w.xExprCallback = exprWalkNoop;
99554 w.pParse = pParse;
99555 w.bSelectDepthFirst = 1;
99556 sqlite3WalkSelect(&w, pSelect);
99557 #endif
99558 }
99559
99560
@@ -98761,11 +99961,11 @@
99961 pItem->regReturn = ++pParse->nMem;
99962 topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
99963 pItem->addrFillSub = topAddr+1;
99964 VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
99965 if( pItem->isCorrelated==0 ){
99966 /* If the subquery is not correlated and if we are not inside of
99967 ** a trigger, then we only need to compute the value of the subquery
99968 ** once. */
99969 onceAddr = sqlite3CodeOnce(pParse);
99970 }
99971 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
@@ -105219,13 +106419,12 @@
106419 Table *pTab = pSrc->pTab;
106420 sqlite3_index_info *pIdxInfo;
106421 struct sqlite3_index_constraint *pIdxCons;
106422 struct sqlite3_index_constraint_usage *pUsage;
106423 WhereTerm *pTerm;
106424 int i, j;
106425 int nOrderBy;
 
106426 int bAllowIN; /* Allow IN optimizations */
106427 double rCost;
106428
106429 /* Make sure wsFlags is initialized to some sane value. Otherwise, if the
106430 ** malloc in allocateIndexInfo() fails and this function returns leaving
@@ -105320,11 +106519,10 @@
106519
106520 if( vtabBestIndex(pParse, pTab, pIdxInfo) ){
106521 return;
106522 }
106523
 
106524 pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
106525 for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
106526 if( pUsage[i].argvIndex>0 ){
106527 j = pIdxCons->iTermOffset;
106528 pTerm = &pWC->a[j];
@@ -105335,21 +106533,32 @@
106533 ** says that the equivalent EQ constraint cannot be safely omitted.
106534 ** If we do attempt to use such a constraint, some rows might be
106535 ** repeated in the output. */
106536 break;
106537 }
106538 /* A virtual table that is constrained by an IN clause may not
106539 ** consume the ORDER BY clause because (1) the order of IN terms
106540 ** is not necessarily related to the order of output terms and
106541 ** (2) Multiple outputs from a single IN value will not merge
106542 ** together. */
106543 pIdxInfo->orderByConsumed = 0;
106544 }
106545 }
106546 }
106547 if( i>=pIdxInfo->nConstraint ) break;
106548 }
106549
106550 /* The orderByConsumed signal is only valid if all outer loops collectively
106551 ** generate just a single row of output.
106552 */
106553 if( pIdxInfo->orderByConsumed ){
106554 for(i=0; i<p->i; i++){
106555 if( (p->aLevel[i].plan.wsFlags & WHERE_UNIQUE)==0 ){
106556 pIdxInfo->orderByConsumed = 0;
106557 }
106558 }
106559 }
106560
106561 /* If there is an ORDER BY clause, and the selected virtual table index
106562 ** does not satisfy it, increase the cost of the scan accordingly. This
106563 ** matches the processing for non-virtual tables in bestBtreeIndex().
106564 */
@@ -105370,12 +106579,11 @@
106579 }else{
106580 p->cost.rCost = rCost;
106581 }
106582 p->cost.plan.u.pVtabIdx = pIdxInfo;
106583 if( pIdxInfo->orderByConsumed ){
106584 p->cost.plan.wsFlags |= WHERE_ORDERED;
 
106585 p->cost.plan.nOBSat = nOrderBy;
106586 }else{
106587 p->cost.plan.nOBSat = p->i ? p->aLevel[p->i-1].plan.nOBSat : 0;
106588 }
106589 p->cost.plan.nEq = 0;
@@ -107108,10 +108316,11 @@
108316 struct SrcList_item *pTabItem; /* FROM clause term being coded */
108317 int addrBrk; /* Jump here to break out of the loop */
108318 int addrCont; /* Jump here to continue with next cycle */
108319 int iRowidReg = 0; /* Rowid is stored in this register, if not zero */
108320 int iReleaseReg = 0; /* Temp register to free before returning */
108321 Bitmask newNotReady; /* Return value */
108322
108323 pParse = pWInfo->pParse;
108324 v = pParse->pVdbe;
108325 pWC = pWInfo->pWC;
108326 pLevel = &pWInfo->a[iLevel];
@@ -107118,10 +108327,11 @@
108327 pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
108328 iCur = pTabItem->iCursor;
108329 bRev = (pLevel->plan.wsFlags & WHERE_REVERSE)!=0;
108330 omitTable = (pLevel->plan.wsFlags & WHERE_IDX_ONLY)!=0
108331 && (wctrlFlags & WHERE_FORCE_TABLE)==0;
108332 VdbeNoopComment((v, "Begin Join Loop %d", iLevel));
108333
108334 /* Create labels for the "break" and "continue" instructions
108335 ** for the current loop. Jump to addrBrk to break out of a loop.
108336 ** Jump to cont to go immediately to the next iteration of the
108337 ** loop.
@@ -107768,11 +108978,11 @@
108978 pLevel->op = aStep[bRev];
108979 pLevel->p1 = iCur;
108980 pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
108981 pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
108982 }
108983 newNotReady = notReady & ~getMask(pWC->pMaskSet, iCur);
108984
108985 /* Insert code to test every subexpression that can be completely
108986 ** computed using the current set of tables.
108987 **
108988 ** IMPLEMENTATION-OF: R-49525-50935 Terms that cannot be satisfied through
@@ -107782,11 +108992,11 @@
108992 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
108993 Expr *pE;
108994 testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
108995 testcase( pTerm->wtFlags & TERM_CODED );
108996 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
108997 if( (pTerm->prereqAll & newNotReady)!=0 ){
108998 testcase( pWInfo->untestedTerms==0
108999 && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
109000 pWInfo->untestedTerms = 1;
109001 continue;
109002 }
@@ -107796,10 +109006,36 @@
109006 continue;
109007 }
109008 sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
109009 pTerm->wtFlags |= TERM_CODED;
109010 }
109011
109012 /* Insert code to test for implied constraints based on transitivity
109013 ** of the "==" operator.
109014 **
109015 ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
109016 ** and we are coding the t1 loop and the t2 loop has not yet coded,
109017 ** then we cannot use the "t1.a=t2.b" constraint, but we can code
109018 ** the implied "t1.a=123" constraint.
109019 */
109020 for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
109021 Expr *pE;
109022 WhereTerm *pAlt;
109023 Expr sEq;
109024 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
109025 if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
109026 if( pTerm->leftCursor!=iCur ) continue;
109027 pE = pTerm->pExpr;
109028 assert( !ExprHasProperty(pE, EP_FromJoin) );
109029 assert( (pTerm->prereqRight & newNotReady)!=0 );
109030 pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
109031 if( pAlt==0 ) continue;
109032 VdbeNoopComment((v, "begin transitive constraint"));
109033 sEq = *pAlt->pExpr;
109034 sEq.pLeft = pE->pLeft;
109035 sqlite3ExprIfFalse(pParse, &sEq, addrCont, SQLITE_JUMPIFNULL);
109036 }
109037
109038 /* For a LEFT OUTER JOIN, generate code that will record the fact that
109039 ** at least one row of the right table has matched the left table.
109040 */
109041 if( pLevel->iLeftJoin ){
@@ -107809,11 +109045,11 @@
109045 sqlite3ExprCacheClear(pParse);
109046 for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
109047 testcase( pTerm->wtFlags & TERM_VIRTUAL ); /* IMP: R-30575-11662 */
109048 testcase( pTerm->wtFlags & TERM_CODED );
109049 if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
109050 if( (pTerm->prereqAll & newNotReady)!=0 ){
109051 assert( pWInfo->untestedTerms );
109052 continue;
109053 }
109054 assert( pTerm->pExpr );
109055 sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
@@ -107820,11 +109056,11 @@
109056 pTerm->wtFlags |= TERM_CODED;
109057 }
109058 }
109059 sqlite3ReleaseTempReg(pParse, iReleaseReg);
109060
109061 return newNotReady;
109062 }
109063
109064 #if defined(SQLITE_TEST)
109065 /*
109066 ** The following variable holds a text description of query plan generated
@@ -113805,10 +115041,23 @@
115041 sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
115042 sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
115043 break;
115044 }
115045 #endif
115046
115047 case SQLITE_CONFIG_MMAP_SIZE: {
115048 sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
115049 sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
115050 if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
115051 mxMmap = SQLITE_MAX_MMAP_SIZE;
115052 }
115053 sqlite3GlobalConfig.mxMmap = mxMmap;
115054 if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
115055 if( szMmap>mxMmap) szMmap = mxMmap;
115056 sqlite3GlobalConfig.szMmap = szMmap;
115057 break;
115058 }
115059
115060 default: {
115061 rc = SQLITE_ERROR;
115062 break;
115063 }
@@ -115626,10 +116875,11 @@
116875
116876 assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
116877 memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
116878 db->autoCommit = 1;
116879 db->nextAutovac = -1;
116880 db->szMmap = sqlite3GlobalConfig.szMmap;
116881 db->nextPagesize = 0;
116882 db->flags |= SQLITE_ShortColNames | SQLITE_AutoIndex | SQLITE_EnableTrigger
116883 #if SQLITE_DEFAULT_FILE_FORMAT<4
116884 | SQLITE_LegacyFileFmt
116885 #endif
@@ -117967,10 +119217,13 @@
119217 Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
119218 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
119219 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
119220 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
119221
119222 /* fts3_tokenize_vtab.c */
119223 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*);
119224
119225 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
119226 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
119227 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
119228 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
119229 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
@@ -121281,10 +122534,13 @@
122534 #endif
122535
122536 rc = sqlite3Fts3InitAux(db);
122537 if( rc!=SQLITE_OK ) return rc;
122538
122539 rc = sqlite3Fts3InitTok(db);
122540 if( rc!=SQLITE_OK ) return rc;
122541
122542 sqlite3Fts3SimpleTokenizerModule(&pSimple);
122543 sqlite3Fts3PorterTokenizerModule(&pPorter);
122544
122545 /* Allocate and initialize the hash-table used to store tokenizers. */
122546 pHash = sqlite3_malloc(sizeof(Fts3Hash));
@@ -123110,21 +124366,30 @@
124366 int rc; /* value returned by declare_vtab() */
124367 Fts3auxTable *p; /* Virtual table object to return */
124368
124369 UNUSED_PARAMETER(pUnused);
124370
124371 /* The user should invoke this in one of two forms:
124372 **
124373 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
124374 ** CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
124375 */
124376 if( argc!=4 && argc!=5 ) goto bad_args;
 
124377
124378 zDb = argv[1];
124379 nDb = (int)strlen(zDb);
124380 if( argc==5 ){
124381 if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
124382 zDb = argv[3];
124383 nDb = (int)strlen(zDb);
124384 zFts3 = argv[4];
124385 }else{
124386 goto bad_args;
124387 }
124388 }else{
124389 zFts3 = argv[3];
124390 }
124391 nFts3 = (int)strlen(zFts3);
124392
124393 rc = sqlite3_declare_vtab(db, FTS3_TERMS_SCHEMA);
124394 if( rc!=SQLITE_OK ) return rc;
124395
@@ -123143,10 +124408,14 @@
124408 memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
124409 sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
124410
124411 *ppVtab = (sqlite3_vtab *)p;
124412 return SQLITE_OK;
124413
124414 bad_args:
124415 *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
124416 return SQLITE_ERROR;
124417 }
124418
124419 /*
124420 ** This function does the work for both the xDisconnect and xDestroy methods.
124421 ** These tables have no persistent representation of their own, so xDisconnect
@@ -126285,10 +127554,476 @@
127554 }
127555
127556 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
127557
127558 /************** End of fts3_tokenizer1.c *************************************/
127559 /************** Begin file fts3_tokenize_vtab.c ******************************/
127560 /*
127561 ** 2013 Apr 22
127562 **
127563 ** The author disclaims copyright to this source code. In place of
127564 ** a legal notice, here is a blessing:
127565 **
127566 ** May you do good and not evil.
127567 ** May you find forgiveness for yourself and forgive others.
127568 ** May you share freely, never taking more than you give.
127569 **
127570 ******************************************************************************
127571 **
127572 ** This file contains code for the "fts3tokenize" virtual table module.
127573 ** An fts3tokenize virtual table is created as follows:
127574 **
127575 ** CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
127576 ** <tokenizer-name>, <arg-1>, ...
127577 ** );
127578 **
127579 ** The table created has the following schema:
127580 **
127581 ** CREATE TABLE <tbl>(input, token, start, end, position)
127582 **
127583 ** When queried, the query must include a WHERE clause of type:
127584 **
127585 ** input = <string>
127586 **
127587 ** The virtual table module tokenizes this <string>, using the FTS3
127588 ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
127589 ** statement and returns one row for each token in the result. With
127590 ** fields set as follows:
127591 **
127592 ** input: Always set to a copy of <string>
127593 ** token: A token from the input.
127594 ** start: Byte offset of the token within the input <string>.
127595 ** end: Byte offset of the byte immediately following the end of the
127596 ** token within the input string.
127597 ** pos: Token offset of token within input.
127598 **
127599 */
127600 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
127601
127602 /* #include <string.h> */
127603 /* #include <assert.h> */
127604
127605 typedef struct Fts3tokTable Fts3tokTable;
127606 typedef struct Fts3tokCursor Fts3tokCursor;
127607
127608 /*
127609 ** Virtual table structure.
127610 */
127611 struct Fts3tokTable {
127612 sqlite3_vtab base; /* Base class used by SQLite core */
127613 const sqlite3_tokenizer_module *pMod;
127614 sqlite3_tokenizer *pTok;
127615 };
127616
127617 /*
127618 ** Virtual table cursor structure.
127619 */
127620 struct Fts3tokCursor {
127621 sqlite3_vtab_cursor base; /* Base class used by SQLite core */
127622 char *zInput; /* Input string */
127623 sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
127624 int iRowid; /* Current 'rowid' value */
127625 const char *zToken; /* Current 'token' value */
127626 int nToken; /* Size of zToken in bytes */
127627 int iStart; /* Current 'start' value */
127628 int iEnd; /* Current 'end' value */
127629 int iPos; /* Current 'pos' value */
127630 };
127631
127632 /*
127633 ** Query FTS for the tokenizer implementation named zName.
127634 */
127635 static int fts3tokQueryTokenizer(
127636 sqlite3 *db,
127637 const char *zName,
127638 const sqlite3_tokenizer_module **pp
127639 ){
127640 int rc;
127641 sqlite3_stmt *pStmt;
127642 const char *zSql = "SELECT fts3_tokenizer(?)";
127643
127644 *pp = 0;
127645 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
127646 if( rc!=SQLITE_OK ){
127647 return rc;
127648 }
127649
127650 sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
127651 if( SQLITE_ROW==sqlite3_step(pStmt) ){
127652 if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
127653 memcpy((void*)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
127654 }
127655 }
127656
127657 return sqlite3_finalize(pStmt);
127658 }
127659
127660 /*
127661 ** The second argument, argv[], is an array of pointers to nul-terminated
127662 ** strings. This function makes a copy of the array and strings into a
127663 ** single block of memory. It then dequotes any of the strings that appear
127664 ** to be quoted.
127665 **
127666 ** If successful, output parameter *pazDequote is set to point at the
127667 ** array of dequoted strings and SQLITE_OK is returned. The caller is
127668 ** responsible for eventually calling sqlite3_free() to free the array
127669 ** in this case. Or, if an error occurs, an SQLite error code is returned.
127670 ** The final value of *pazDequote is undefined in this case.
127671 */
127672 static int fts3tokDequoteArray(
127673 int argc, /* Number of elements in argv[] */
127674 const char * const *argv, /* Input array */
127675 char ***pazDequote /* Output array */
127676 ){
127677 int rc = SQLITE_OK; /* Return code */
127678 if( argc==0 ){
127679 *pazDequote = 0;
127680 }else{
127681 int i;
127682 int nByte = 0;
127683 char **azDequote;
127684
127685 for(i=0; i<argc; i++){
127686 nByte += (int)(strlen(argv[i]) + 1);
127687 }
127688
127689 *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
127690 if( azDequote==0 ){
127691 rc = SQLITE_NOMEM;
127692 }else{
127693 char *pSpace = (char *)&azDequote[argc];
127694 for(i=0; i<argc; i++){
127695 int n = (int)strlen(argv[i]);
127696 azDequote[i] = pSpace;
127697 memcpy(pSpace, argv[i], n+1);
127698 sqlite3Fts3Dequote(pSpace);
127699 pSpace += (n+1);
127700 }
127701 }
127702 }
127703
127704 return rc;
127705 }
127706
127707 /*
127708 ** Schema of the tokenizer table.
127709 */
127710 #define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
127711
127712 /*
127713 ** This function does all the work for both the xConnect and xCreate methods.
127714 ** These tables have no persistent representation of their own, so xConnect
127715 ** and xCreate are identical operations.
127716 **
127717 ** argv[0]: module name
127718 ** argv[1]: database name
127719 ** argv[2]: table name
127720 ** argv[3]: first argument (tokenizer name)
127721 */
127722 static int fts3tokConnectMethod(
127723 sqlite3 *db, /* Database connection */
127724 void *pUnused, /* Unused */
127725 int argc, /* Number of elements in argv array */
127726 const char * const *argv, /* xCreate/xConnect argument array */
127727 sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
127728 char **pzErr /* OUT: sqlite3_malloc'd error message */
127729 ){
127730 Fts3tokTable *pTab;
127731 const sqlite3_tokenizer_module *pMod = 0;
127732 sqlite3_tokenizer *pTok = 0;
127733 int rc;
127734 char **azDequote = 0;
127735 int nDequote;
127736 UNUSED_PARAMETER(pUnused);
127737
127738 rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
127739 if( rc!=SQLITE_OK ) return rc;
127740
127741 nDequote = argc-3;
127742 rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
127743
127744 if( rc==SQLITE_OK ){
127745 const char *zModule;
127746 if( nDequote<1 ){
127747 zModule = "simple";
127748 }else{
127749 zModule = azDequote[0];
127750 }
127751 rc = fts3tokQueryTokenizer(db, zModule, &pMod);
127752 }
127753
127754 if( rc!=SQLITE_OK ){
127755 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
127756 }else if( pMod==0 ){
127757 rc = SQLITE_ERROR;
127758 }else{
127759 const char * const *azArg = (const char * const *)&azDequote[1];
127760 rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
127761 }
127762
127763 if( rc==SQLITE_OK ){
127764 pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
127765 if( pTab==0 ){
127766 rc = SQLITE_NOMEM;
127767 }
127768 }
127769
127770 if( rc==SQLITE_OK ){
127771 memset(pTab, 0, sizeof(Fts3tokTable));
127772 pTab->pMod = pMod;
127773 pTab->pTok = pTok;
127774 *ppVtab = &pTab->base;
127775 }else{
127776 if( pTok ){
127777 pMod->xDestroy(pTok);
127778 }
127779 }
127780
127781 sqlite3_free(azDequote);
127782 return rc;
127783 }
127784
127785 /*
127786 ** This function does the work for both the xDisconnect and xDestroy methods.
127787 ** These tables have no persistent representation of their own, so xDisconnect
127788 ** and xDestroy are identical operations.
127789 */
127790 static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
127791 Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
127792
127793 pTab->pMod->xDestroy(pTab->pTok);
127794 sqlite3_free(pTab);
127795 return SQLITE_OK;
127796 }
127797
127798 /*
127799 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
127800 */
127801 static int fts3tokBestIndexMethod(
127802 sqlite3_vtab *pVTab,
127803 sqlite3_index_info *pInfo
127804 ){
127805 int i;
127806 UNUSED_PARAMETER(pVTab);
127807
127808 for(i=0; i<pInfo->nConstraint; i++){
127809 if( pInfo->aConstraint[i].usable
127810 && pInfo->aConstraint[i].iColumn==0
127811 && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
127812 ){
127813 pInfo->idxNum = 1;
127814 pInfo->aConstraintUsage[i].argvIndex = 1;
127815 pInfo->aConstraintUsage[i].omit = 1;
127816 pInfo->estimatedCost = 1;
127817 return SQLITE_OK;
127818 }
127819 }
127820
127821 pInfo->idxNum = 0;
127822 assert( pInfo->estimatedCost>1000000.0 );
127823
127824 return SQLITE_OK;
127825 }
127826
127827 /*
127828 ** xOpen - Open a cursor.
127829 */
127830 static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
127831 Fts3tokCursor *pCsr;
127832 UNUSED_PARAMETER(pVTab);
127833
127834 pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
127835 if( pCsr==0 ){
127836 return SQLITE_NOMEM;
127837 }
127838 memset(pCsr, 0, sizeof(Fts3tokCursor));
127839
127840 *ppCsr = (sqlite3_vtab_cursor *)pCsr;
127841 return SQLITE_OK;
127842 }
127843
127844 /*
127845 ** Reset the tokenizer cursor passed as the only argument. As if it had
127846 ** just been returned by fts3tokOpenMethod().
127847 */
127848 static void fts3tokResetCursor(Fts3tokCursor *pCsr){
127849 if( pCsr->pCsr ){
127850 Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
127851 pTab->pMod->xClose(pCsr->pCsr);
127852 pCsr->pCsr = 0;
127853 }
127854 sqlite3_free(pCsr->zInput);
127855 pCsr->zInput = 0;
127856 pCsr->zToken = 0;
127857 pCsr->nToken = 0;
127858 pCsr->iStart = 0;
127859 pCsr->iEnd = 0;
127860 pCsr->iPos = 0;
127861 pCsr->iRowid = 0;
127862 }
127863
127864 /*
127865 ** xClose - Close a cursor.
127866 */
127867 static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
127868 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127869
127870 fts3tokResetCursor(pCsr);
127871 sqlite3_free(pCsr);
127872 return SQLITE_OK;
127873 }
127874
127875 /*
127876 ** xNext - Advance the cursor to the next row, if any.
127877 */
127878 static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
127879 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127880 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
127881 int rc; /* Return code */
127882
127883 pCsr->iRowid++;
127884 rc = pTab->pMod->xNext(pCsr->pCsr,
127885 &pCsr->zToken, &pCsr->nToken,
127886 &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
127887 );
127888
127889 if( rc!=SQLITE_OK ){
127890 fts3tokResetCursor(pCsr);
127891 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
127892 }
127893
127894 return rc;
127895 }
127896
127897 /*
127898 ** xFilter - Initialize a cursor to point at the start of its data.
127899 */
127900 static int fts3tokFilterMethod(
127901 sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
127902 int idxNum, /* Strategy index */
127903 const char *idxStr, /* Unused */
127904 int nVal, /* Number of elements in apVal */
127905 sqlite3_value **apVal /* Arguments for the indexing scheme */
127906 ){
127907 int rc = SQLITE_ERROR;
127908 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127909 Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
127910 UNUSED_PARAMETER(idxStr);
127911 UNUSED_PARAMETER(nVal);
127912
127913 fts3tokResetCursor(pCsr);
127914 if( idxNum==1 ){
127915 const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
127916 int nByte = sqlite3_value_bytes(apVal[0]);
127917 pCsr->zInput = sqlite3_malloc(nByte+1);
127918 if( pCsr->zInput==0 ){
127919 rc = SQLITE_NOMEM;
127920 }else{
127921 memcpy(pCsr->zInput, zByte, nByte);
127922 pCsr->zInput[nByte] = 0;
127923 rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
127924 if( rc==SQLITE_OK ){
127925 pCsr->pCsr->pTokenizer = pTab->pTok;
127926 }
127927 }
127928 }
127929
127930 if( rc!=SQLITE_OK ) return rc;
127931 return fts3tokNextMethod(pCursor);
127932 }
127933
127934 /*
127935 ** xEof - Return true if the cursor is at EOF, or false otherwise.
127936 */
127937 static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
127938 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127939 return (pCsr->zToken==0);
127940 }
127941
127942 /*
127943 ** xColumn - Return a column value.
127944 */
127945 static int fts3tokColumnMethod(
127946 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
127947 sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
127948 int iCol /* Index of column to read value from */
127949 ){
127950 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127951
127952 /* CREATE TABLE x(input, token, start, end, position) */
127953 switch( iCol ){
127954 case 0:
127955 sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
127956 break;
127957 case 1:
127958 sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
127959 break;
127960 case 2:
127961 sqlite3_result_int(pCtx, pCsr->iStart);
127962 break;
127963 case 3:
127964 sqlite3_result_int(pCtx, pCsr->iEnd);
127965 break;
127966 default:
127967 assert( iCol==4 );
127968 sqlite3_result_int(pCtx, pCsr->iPos);
127969 break;
127970 }
127971 return SQLITE_OK;
127972 }
127973
127974 /*
127975 ** xRowid - Return the current rowid for the cursor.
127976 */
127977 static int fts3tokRowidMethod(
127978 sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
127979 sqlite_int64 *pRowid /* OUT: Rowid value */
127980 ){
127981 Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
127982 *pRowid = (sqlite3_int64)pCsr->iRowid;
127983 return SQLITE_OK;
127984 }
127985
127986 /*
127987 ** Register the fts3tok module with database connection db. Return SQLITE_OK
127988 ** if successful or an error code if sqlite3_create_module() fails.
127989 */
127990 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db){
127991 static const sqlite3_module fts3tok_module = {
127992 0, /* iVersion */
127993 fts3tokConnectMethod, /* xCreate */
127994 fts3tokConnectMethod, /* xConnect */
127995 fts3tokBestIndexMethod, /* xBestIndex */
127996 fts3tokDisconnectMethod, /* xDisconnect */
127997 fts3tokDisconnectMethod, /* xDestroy */
127998 fts3tokOpenMethod, /* xOpen */
127999 fts3tokCloseMethod, /* xClose */
128000 fts3tokFilterMethod, /* xFilter */
128001 fts3tokNextMethod, /* xNext */
128002 fts3tokEofMethod, /* xEof */
128003 fts3tokColumnMethod, /* xColumn */
128004 fts3tokRowidMethod, /* xRowid */
128005 0, /* xUpdate */
128006 0, /* xBegin */
128007 0, /* xSync */
128008 0, /* xCommit */
128009 0, /* xRollback */
128010 0, /* xFindFunction */
128011 0, /* xRename */
128012 0, /* xSavepoint */
128013 0, /* xRelease */
128014 0 /* xRollbackTo */
128015 };
128016 int rc; /* Return code */
128017
128018 rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, 0);
128019 return rc;
128020 }
128021
128022 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
128023
128024 /************** End of fts3_tokenize_vtab.c **********************************/
128025 /************** Begin file fts3_write.c **************************************/
128026 /*
128027 ** 2009 Oct 23
128028 **
128029 ** The author disclaims copyright to this source code. In place of
128030
+78 -16
--- 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.16.1"
111
-#define SQLITE_VERSION_NUMBER 3007016
112
-#define SQLITE_SOURCE_ID "2013-03-27 20:41:15 274d2a22660c7b34b8bbd85f3c29cbafbcb1b4e7"
110
+#define SQLITE_VERSION "3.7.17"
111
+#define SQLITE_VERSION_NUMBER 3007017
112
+#define SQLITE_SOURCE_ID "2013-04-25 17:27:08 aabeea98f53edde68f484f1794ae70789dac3889"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -423,10 +423,12 @@
423423
#define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
424424
#define SQLITE_AUTH 23 /* Authorization denied */
425425
#define SQLITE_FORMAT 24 /* Auxiliary database format error */
426426
#define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
427427
#define SQLITE_NOTADB 26 /* File opened that is not a database file */
428
+#define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
429
+#define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
428430
#define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
429431
#define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
430432
/* end-of-error-codes */
431433
432434
/*
@@ -473,10 +475,11 @@
473475
#define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
474476
#define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
475477
#define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
476478
#define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
477479
#define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
480
+#define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
478481
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
479482
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
480483
#define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
481484
#define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
482485
#define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -492,10 +495,12 @@
492495
#define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
493496
#define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
494497
#define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
495498
#define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
496499
#define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
500
+#define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
501
+#define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
497502
498503
/*
499504
** CAPI3REF: Flags For File Open Operations
500505
**
501506
** These bit values are intended for use in the
@@ -731,10 +736,13 @@
731736
int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
732737
int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
733738
void (*xShmBarrier)(sqlite3_file*);
734739
int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
735740
/* Methods above are valid for version 2 */
741
+ int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
742
+ int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
743
+ /* Methods above are valid for version 3 */
736744
/* Additional methods may be added in future releases */
737745
};
738746
739747
/*
740748
** CAPI3REF: Standard File Control Opcodes
@@ -867,11 +875,12 @@
867875
** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
868876
** file control occurs at the beginning of pragma statement analysis and so
869877
** it is able to override built-in [PRAGMA] statements.
870878
**
871879
** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
872
-** ^This file-control may be invoked by SQLite on the database file handle
880
+** ^The [SQLITE_FCNTL_BUSYHANDLER]
881
+** file-control may be invoked by SQLite on the database file handle
873882
** shortly after it is opened in order to provide a custom VFS with access
874883
** to the connections busy-handler callback. The argument is of type (void **)
875884
** - an array of two (void *) values. The first (void *) actually points
876885
** to a function of type (int (*)(void *)). In order to invoke the connections
877886
** busy-handler, this function should be invoked with the second (void *) in
@@ -878,17 +887,28 @@
878887
** the array as the only argument. If it returns non-zero, then the operation
879888
** should be retried. If it returns zero, the custom VFS should abandon the
880889
** current operation.
881890
**
882891
** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
883
-** ^Application can invoke this file-control to have SQLite generate a
892
+** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
893
+** to have SQLite generate a
884894
** temporary filename using the same algorithm that is followed to generate
885895
** temporary filenames for TEMP tables and other internal uses. The
886896
** argument should be a char** which will be filled with the filename
887897
** written into memory obtained from [sqlite3_malloc()]. The caller should
888898
** invoke [sqlite3_free()] on the result to avoid a memory leak.
889899
**
900
+** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
901
+** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
902
+** maximum number of bytes that will be used for memory-mapped I/O.
903
+** The argument is a pointer to a value of type sqlite3_int64 that
904
+** is an advisory maximum number of bytes in the file to memory map. The
905
+** pointer is overwritten with the old value. The limit is not changed if
906
+** the value originally pointed to is negative, and so the current limit
907
+** can be queried by passing in a pointer to a negative number. This
908
+** file-control is used internally to implement [PRAGMA mmap_size].
909
+**
890910
** </ul>
891911
*/
892912
#define SQLITE_FCNTL_LOCKSTATE 1
893913
#define SQLITE_GET_LOCKPROXYFILE 2
894914
#define SQLITE_SET_LOCKPROXYFILE 3
@@ -903,10 +923,11 @@
903923
#define SQLITE_FCNTL_VFSNAME 12
904924
#define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
905925
#define SQLITE_FCNTL_PRAGMA 14
906926
#define SQLITE_FCNTL_BUSYHANDLER 15
907927
#define SQLITE_FCNTL_TEMPFILENAME 16
928
+#define SQLITE_FCNTL_MMAP_SIZE 18
908929
909930
/*
910931
** CAPI3REF: Mutex Handle
911932
**
912933
** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1615,26 +1636,42 @@
16151636
**
16161637
** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
16171638
** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
16181639
** <dd> These options are obsolete and should not be used by new code.
16191640
** They are retained for backwards compatibility but are now no-ops.
1620
-** </dl>
1641
+** </dd>
16211642
**
16221643
** [[SQLITE_CONFIG_SQLLOG]]
16231644
** <dt>SQLITE_CONFIG_SQLLOG
16241645
** <dd>This option is only available if sqlite is compiled with the
1625
-** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
1646
+** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
16261647
** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
16271648
** The second should be of type (void*). The callback is invoked by the library
16281649
** in three separate circumstances, identified by the value passed as the
16291650
** fourth parameter. If the fourth parameter is 0, then the database connection
16301651
** passed as the second argument has just been opened. The third argument
16311652
** points to a buffer containing the name of the main database file. If the
16321653
** fourth parameter is 1, then the SQL statement that the third parameter
16331654
** points to has just been executed. Or, if the fourth parameter is 2, then
16341655
** the connection being passed as the second parameter is being closed. The
1635
-** third parameter is passed NULL In this case.
1656
+** third parameter is passed NULL In this case. An example of using this
1657
+** configuration option can be seen in the "test_sqllog.c" source file in
1658
+** the canonical SQLite source tree.</dd>
1659
+**
1660
+** [[SQLITE_CONFIG_MMAP_SIZE]]
1661
+** <dt>SQLITE_CONFIG_MMAP_SIZE
1662
+** <dd>SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1663
+** that are the default mmap size limit (the default setting for
1664
+** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1665
+** The default setting can be overridden by each database connection using
1666
+** either the [PRAGMA mmap_size] command, or by using the
1667
+** [SQLITE_FCNTL_MMAP_SIZE] file control. The maximum allowed mmap size
1668
+** cannot be changed at run-time. Nor may the maximum allowed mmap size
1669
+** exceed the compile-time maximum mmap size set by the
1670
+** [SQLITE_MAX_MMAP_SIZE] compile-time option.
1671
+** If either argument to this option is negative, then that argument is
1672
+** changed to its compile-time default.
16361673
** </dl>
16371674
*/
16381675
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
16391676
#define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
16401677
#define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1654,10 +1691,11 @@
16541691
#define SQLITE_CONFIG_URI 17 /* int */
16551692
#define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
16561693
#define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
16571694
#define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
16581695
#define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1696
+#define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
16591697
16601698
/*
16611699
** CAPI3REF: Database Connection Configuration Options
16621700
**
16631701
** These constants are the available integer configuration options that
@@ -4185,11 +4223,11 @@
41854223
** SQLITE_TRANSIENT value means that the content will likely change in
41864224
** the near future and that SQLite should make its own private copy of
41874225
** the content before returning.
41884226
**
41894227
** The typedef is necessary to work around problems in certain
4190
-** C++ compilers. See ticket #2191.
4228
+** C++ compilers.
41914229
*/
41924230
typedef void (*sqlite3_destructor_type)(void*);
41934231
#define SQLITE_STATIC ((sqlite3_destructor_type)0)
41944232
#define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
41954233
@@ -4984,15 +5022,24 @@
49845022
** CAPI3REF: Load An Extension
49855023
**
49865024
** ^This interface loads an SQLite extension library from the named file.
49875025
**
49885026
** ^The sqlite3_load_extension() interface attempts to load an
4989
-** SQLite extension library contained in the file zFile.
5027
+** [SQLite extension] library contained in the file zFile. If
5028
+** the file cannot be loaded directly, attempts are made to load
5029
+** with various operating-system specific extensions added.
5030
+** So for example, if "samplelib" cannot be loaded, then names like
5031
+** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5032
+** be tried also.
49905033
**
49915034
** ^The entry point is zProc.
4992
-** ^zProc may be 0, in which case the name of the entry point
4993
-** defaults to "sqlite3_extension_init".
5035
+** ^(zProc may be 0, in which case SQLite will try to come up with an
5036
+** entry point name on its own. It first tries "sqlite3_extension_init".
5037
+** If that does not work, it constructs a name "sqlite3_X_init" where the
5038
+** X is consists of the lower-case equivalent of all ASCII alphabetic
5039
+** characters in the filename from the last "/" to the first following
5040
+** "." and omitting any initial "lib".)^
49945041
** ^The sqlite3_load_extension() interface returns
49955042
** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
49965043
** ^If an error occurs and pzErrMsg is not 0, then the
49975044
** [sqlite3_load_extension()] interface shall attempt to
49985045
** fill *pzErrMsg with error message text stored in memory
@@ -5014,15 +5061,15 @@
50145061
50155062
/*
50165063
** CAPI3REF: Enable Or Disable Extension Loading
50175064
**
50185065
** ^So as not to open security holes in older applications that are
5019
-** unprepared to deal with extension loading, and as a means of disabling
5020
-** extension loading while evaluating user-entered SQL, the following API
5066
+** unprepared to deal with [extension loading], and as a means of disabling
5067
+** [extension loading] while evaluating user-entered SQL, the following API
50215068
** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
50225069
**
5023
-** ^Extension loading is off by default. See ticket #1863.
5070
+** ^Extension loading is off by default.
50245071
** ^Call the sqlite3_enable_load_extension() routine with onoff==1
50255072
** to turn extension loading on and call it with onoff==0 to turn
50265073
** it back off again.
50275074
*/
50285075
SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5030,11 +5077,11 @@
50305077
/*
50315078
** CAPI3REF: Automatically Load Statically Linked Extensions
50325079
**
50335080
** ^This interface causes the xEntryPoint() function to be invoked for
50345081
** each new [database connection] that is created. The idea here is that
5035
-** xEntryPoint() is the entry point for a statically linked SQLite extension
5082
+** xEntryPoint() is the entry point for a statically linked [SQLite extension]
50365083
** that is to be automatically loaded into all new database connections.
50375084
**
50385085
** ^(Even though the function prototype shows that xEntryPoint() takes
50395086
** no arguments and returns void, SQLite invokes xEntryPoint() with three
50405087
** arguments and expects and integer result as if the signature of the
@@ -6810,10 +6857,25 @@
68106857
** independence" that SQLite uses internally when comparing identifiers.
68116858
*/
68126859
SQLITE_API int sqlite3_stricmp(const char *, const char *);
68136860
SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
68146861
6862
+/*
6863
+** CAPI3REF: String Globbing
6864
+*
6865
+** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
6866
+** the glob pattern P, and it returns non-zero if string X does not match
6867
+** the glob pattern P. ^The definition of glob pattern matching used in
6868
+** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
6869
+** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case
6870
+** sensitive.
6871
+**
6872
+** Note that this routine returns zero on a match and non-zero if the strings
6873
+** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
6874
+*/
6875
+SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
6876
+
68156877
/*
68166878
** CAPI3REF: Error Logging Interface
68176879
**
68186880
** ^The [sqlite3_log()] interface writes a message into the error log
68196881
** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
68206882
--- 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.16.1"
111 #define SQLITE_VERSION_NUMBER 3007016
112 #define SQLITE_SOURCE_ID "2013-03-27 20:41:15 274d2a22660c7b34b8bbd85f3c29cbafbcb1b4e7"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -423,10 +423,12 @@
423 #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
424 #define SQLITE_AUTH 23 /* Authorization denied */
425 #define SQLITE_FORMAT 24 /* Auxiliary database format error */
426 #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
427 #define SQLITE_NOTADB 26 /* File opened that is not a database file */
 
 
428 #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
429 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
430 /* end-of-error-codes */
431
432 /*
@@ -473,10 +475,11 @@
473 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
474 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
475 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
476 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
477 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
 
478 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
479 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
480 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
481 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
482 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -492,10 +495,12 @@
492 #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
493 #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
494 #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
495 #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
496 #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
 
 
497
498 /*
499 ** CAPI3REF: Flags For File Open Operations
500 **
501 ** These bit values are intended for use in the
@@ -731,10 +736,13 @@
731 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
732 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
733 void (*xShmBarrier)(sqlite3_file*);
734 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
735 /* Methods above are valid for version 2 */
 
 
 
736 /* Additional methods may be added in future releases */
737 };
738
739 /*
740 ** CAPI3REF: Standard File Control Opcodes
@@ -867,11 +875,12 @@
867 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
868 ** file control occurs at the beginning of pragma statement analysis and so
869 ** it is able to override built-in [PRAGMA] statements.
870 **
871 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
872 ** ^This file-control may be invoked by SQLite on the database file handle
 
873 ** shortly after it is opened in order to provide a custom VFS with access
874 ** to the connections busy-handler callback. The argument is of type (void **)
875 ** - an array of two (void *) values. The first (void *) actually points
876 ** to a function of type (int (*)(void *)). In order to invoke the connections
877 ** busy-handler, this function should be invoked with the second (void *) in
@@ -878,17 +887,28 @@
878 ** the array as the only argument. If it returns non-zero, then the operation
879 ** should be retried. If it returns zero, the custom VFS should abandon the
880 ** current operation.
881 **
882 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
883 ** ^Application can invoke this file-control to have SQLite generate a
 
884 ** temporary filename using the same algorithm that is followed to generate
885 ** temporary filenames for TEMP tables and other internal uses. The
886 ** argument should be a char** which will be filled with the filename
887 ** written into memory obtained from [sqlite3_malloc()]. The caller should
888 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
889 **
 
 
 
 
 
 
 
 
 
 
890 ** </ul>
891 */
892 #define SQLITE_FCNTL_LOCKSTATE 1
893 #define SQLITE_GET_LOCKPROXYFILE 2
894 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -903,10 +923,11 @@
903 #define SQLITE_FCNTL_VFSNAME 12
904 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
905 #define SQLITE_FCNTL_PRAGMA 14
906 #define SQLITE_FCNTL_BUSYHANDLER 15
907 #define SQLITE_FCNTL_TEMPFILENAME 16
 
908
909 /*
910 ** CAPI3REF: Mutex Handle
911 **
912 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1615,26 +1636,42 @@
1615 **
1616 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1617 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1618 ** <dd> These options are obsolete and should not be used by new code.
1619 ** They are retained for backwards compatibility but are now no-ops.
1620 ** </dl>
1621 **
1622 ** [[SQLITE_CONFIG_SQLLOG]]
1623 ** <dt>SQLITE_CONFIG_SQLLOG
1624 ** <dd>This option is only available if sqlite is compiled with the
1625 ** SQLITE_ENABLE_SQLLOG pre-processor macro defined. The first argument should
1626 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1627 ** The second should be of type (void*). The callback is invoked by the library
1628 ** in three separate circumstances, identified by the value passed as the
1629 ** fourth parameter. If the fourth parameter is 0, then the database connection
1630 ** passed as the second argument has just been opened. The third argument
1631 ** points to a buffer containing the name of the main database file. If the
1632 ** fourth parameter is 1, then the SQL statement that the third parameter
1633 ** points to has just been executed. Or, if the fourth parameter is 2, then
1634 ** the connection being passed as the second parameter is being closed. The
1635 ** third parameter is passed NULL In this case.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1636 ** </dl>
1637 */
1638 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1639 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1640 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1654,10 +1691,11 @@
1654 #define SQLITE_CONFIG_URI 17 /* int */
1655 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
1656 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1657 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1658 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
 
1659
1660 /*
1661 ** CAPI3REF: Database Connection Configuration Options
1662 **
1663 ** These constants are the available integer configuration options that
@@ -4185,11 +4223,11 @@
4185 ** SQLITE_TRANSIENT value means that the content will likely change in
4186 ** the near future and that SQLite should make its own private copy of
4187 ** the content before returning.
4188 **
4189 ** The typedef is necessary to work around problems in certain
4190 ** C++ compilers. See ticket #2191.
4191 */
4192 typedef void (*sqlite3_destructor_type)(void*);
4193 #define SQLITE_STATIC ((sqlite3_destructor_type)0)
4194 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
4195
@@ -4984,15 +5022,24 @@
4984 ** CAPI3REF: Load An Extension
4985 **
4986 ** ^This interface loads an SQLite extension library from the named file.
4987 **
4988 ** ^The sqlite3_load_extension() interface attempts to load an
4989 ** SQLite extension library contained in the file zFile.
 
 
 
 
 
4990 **
4991 ** ^The entry point is zProc.
4992 ** ^zProc may be 0, in which case the name of the entry point
4993 ** defaults to "sqlite3_extension_init".
 
 
 
 
4994 ** ^The sqlite3_load_extension() interface returns
4995 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
4996 ** ^If an error occurs and pzErrMsg is not 0, then the
4997 ** [sqlite3_load_extension()] interface shall attempt to
4998 ** fill *pzErrMsg with error message text stored in memory
@@ -5014,15 +5061,15 @@
5014
5015 /*
5016 ** CAPI3REF: Enable Or Disable Extension Loading
5017 **
5018 ** ^So as not to open security holes in older applications that are
5019 ** unprepared to deal with extension loading, and as a means of disabling
5020 ** extension loading while evaluating user-entered SQL, the following API
5021 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5022 **
5023 ** ^Extension loading is off by default. See ticket #1863.
5024 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5025 ** to turn extension loading on and call it with onoff==0 to turn
5026 ** it back off again.
5027 */
5028 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5030,11 +5077,11 @@
5030 /*
5031 ** CAPI3REF: Automatically Load Statically Linked Extensions
5032 **
5033 ** ^This interface causes the xEntryPoint() function to be invoked for
5034 ** each new [database connection] that is created. The idea here is that
5035 ** xEntryPoint() is the entry point for a statically linked SQLite extension
5036 ** that is to be automatically loaded into all new database connections.
5037 **
5038 ** ^(Even though the function prototype shows that xEntryPoint() takes
5039 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5040 ** arguments and expects and integer result as if the signature of the
@@ -6810,10 +6857,25 @@
6810 ** independence" that SQLite uses internally when comparing identifiers.
6811 */
6812 SQLITE_API int sqlite3_stricmp(const char *, const char *);
6813 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6815 /*
6816 ** CAPI3REF: Error Logging Interface
6817 **
6818 ** ^The [sqlite3_log()] interface writes a message into the error log
6819 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
6820
--- 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.17"
111 #define SQLITE_VERSION_NUMBER 3007017
112 #define SQLITE_SOURCE_ID "2013-04-25 17:27:08 aabeea98f53edde68f484f1794ae70789dac3889"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -423,10 +423,12 @@
423 #define SQLITE_NOLFS 22 /* Uses OS features not supported on host */
424 #define SQLITE_AUTH 23 /* Authorization denied */
425 #define SQLITE_FORMAT 24 /* Auxiliary database format error */
426 #define SQLITE_RANGE 25 /* 2nd parameter to sqlite3_bind out of range */
427 #define SQLITE_NOTADB 26 /* File opened that is not a database file */
428 #define SQLITE_NOTICE 27 /* Notifications from sqlite3_log() */
429 #define SQLITE_WARNING 28 /* Warnings from sqlite3_log() */
430 #define SQLITE_ROW 100 /* sqlite3_step() has another row ready */
431 #define SQLITE_DONE 101 /* sqlite3_step() has finished executing */
432 /* end-of-error-codes */
433
434 /*
@@ -473,10 +475,11 @@
475 #define SQLITE_IOERR_SHMSIZE (SQLITE_IOERR | (19<<8))
476 #define SQLITE_IOERR_SHMLOCK (SQLITE_IOERR | (20<<8))
477 #define SQLITE_IOERR_SHMMAP (SQLITE_IOERR | (21<<8))
478 #define SQLITE_IOERR_SEEK (SQLITE_IOERR | (22<<8))
479 #define SQLITE_IOERR_DELETE_NOENT (SQLITE_IOERR | (23<<8))
480 #define SQLITE_IOERR_MMAP (SQLITE_IOERR | (24<<8))
481 #define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
482 #define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
483 #define SQLITE_CANTOPEN_NOTEMPDIR (SQLITE_CANTOPEN | (1<<8))
484 #define SQLITE_CANTOPEN_ISDIR (SQLITE_CANTOPEN | (2<<8))
485 #define SQLITE_CANTOPEN_FULLPATH (SQLITE_CANTOPEN | (3<<8))
@@ -492,10 +495,12 @@
495 #define SQLITE_CONSTRAINT_NOTNULL (SQLITE_CONSTRAINT | (5<<8))
496 #define SQLITE_CONSTRAINT_PRIMARYKEY (SQLITE_CONSTRAINT | (6<<8))
497 #define SQLITE_CONSTRAINT_TRIGGER (SQLITE_CONSTRAINT | (7<<8))
498 #define SQLITE_CONSTRAINT_UNIQUE (SQLITE_CONSTRAINT | (8<<8))
499 #define SQLITE_CONSTRAINT_VTAB (SQLITE_CONSTRAINT | (9<<8))
500 #define SQLITE_NOTICE_RECOVER_WAL (SQLITE_NOTICE | (1<<8))
501 #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
502
503 /*
504 ** CAPI3REF: Flags For File Open Operations
505 **
506 ** These bit values are intended for use in the
@@ -731,10 +736,13 @@
736 int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
737 int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
738 void (*xShmBarrier)(sqlite3_file*);
739 int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
740 /* Methods above are valid for version 2 */
741 int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
742 int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
743 /* Methods above are valid for version 3 */
744 /* Additional methods may be added in future releases */
745 };
746
747 /*
748 ** CAPI3REF: Standard File Control Opcodes
@@ -867,11 +875,12 @@
875 ** compilation of the PRAGMA fails with an error. ^The [SQLITE_FCNTL_PRAGMA]
876 ** file control occurs at the beginning of pragma statement analysis and so
877 ** it is able to override built-in [PRAGMA] statements.
878 **
879 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
880 ** ^The [SQLITE_FCNTL_BUSYHANDLER]
881 ** file-control may be invoked by SQLite on the database file handle
882 ** shortly after it is opened in order to provide a custom VFS with access
883 ** to the connections busy-handler callback. The argument is of type (void **)
884 ** - an array of two (void *) values. The first (void *) actually points
885 ** to a function of type (int (*)(void *)). In order to invoke the connections
886 ** busy-handler, this function should be invoked with the second (void *) in
@@ -878,17 +887,28 @@
887 ** the array as the only argument. If it returns non-zero, then the operation
888 ** should be retried. If it returns zero, the custom VFS should abandon the
889 ** current operation.
890 **
891 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
892 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
893 ** to have SQLite generate a
894 ** temporary filename using the same algorithm that is followed to generate
895 ** temporary filenames for TEMP tables and other internal uses. The
896 ** argument should be a char** which will be filled with the filename
897 ** written into memory obtained from [sqlite3_malloc()]. The caller should
898 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
899 **
900 ** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
901 ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
902 ** maximum number of bytes that will be used for memory-mapped I/O.
903 ** The argument is a pointer to a value of type sqlite3_int64 that
904 ** is an advisory maximum number of bytes in the file to memory map. The
905 ** pointer is overwritten with the old value. The limit is not changed if
906 ** the value originally pointed to is negative, and so the current limit
907 ** can be queried by passing in a pointer to a negative number. This
908 ** file-control is used internally to implement [PRAGMA mmap_size].
909 **
910 ** </ul>
911 */
912 #define SQLITE_FCNTL_LOCKSTATE 1
913 #define SQLITE_GET_LOCKPROXYFILE 2
914 #define SQLITE_SET_LOCKPROXYFILE 3
@@ -903,10 +923,11 @@
923 #define SQLITE_FCNTL_VFSNAME 12
924 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE 13
925 #define SQLITE_FCNTL_PRAGMA 14
926 #define SQLITE_FCNTL_BUSYHANDLER 15
927 #define SQLITE_FCNTL_TEMPFILENAME 16
928 #define SQLITE_FCNTL_MMAP_SIZE 18
929
930 /*
931 ** CAPI3REF: Mutex Handle
932 **
933 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
@@ -1615,26 +1636,42 @@
1636 **
1637 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1638 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1639 ** <dd> These options are obsolete and should not be used by new code.
1640 ** They are retained for backwards compatibility but are now no-ops.
1641 ** </dd>
1642 **
1643 ** [[SQLITE_CONFIG_SQLLOG]]
1644 ** <dt>SQLITE_CONFIG_SQLLOG
1645 ** <dd>This option is only available if sqlite is compiled with the
1646 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
1647 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1648 ** The second should be of type (void*). The callback is invoked by the library
1649 ** in three separate circumstances, identified by the value passed as the
1650 ** fourth parameter. If the fourth parameter is 0, then the database connection
1651 ** passed as the second argument has just been opened. The third argument
1652 ** points to a buffer containing the name of the main database file. If the
1653 ** fourth parameter is 1, then the SQL statement that the third parameter
1654 ** points to has just been executed. Or, if the fourth parameter is 2, then
1655 ** the connection being passed as the second parameter is being closed. The
1656 ** third parameter is passed NULL In this case. An example of using this
1657 ** configuration option can be seen in the "test_sqllog.c" source file in
1658 ** the canonical SQLite source tree.</dd>
1659 **
1660 ** [[SQLITE_CONFIG_MMAP_SIZE]]
1661 ** <dt>SQLITE_CONFIG_MMAP_SIZE
1662 ** <dd>SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1663 ** that are the default mmap size limit (the default setting for
1664 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1665 ** The default setting can be overridden by each database connection using
1666 ** either the [PRAGMA mmap_size] command, or by using the
1667 ** [SQLITE_FCNTL_MMAP_SIZE] file control. The maximum allowed mmap size
1668 ** cannot be changed at run-time. Nor may the maximum allowed mmap size
1669 ** exceed the compile-time maximum mmap size set by the
1670 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.
1671 ** If either argument to this option is negative, then that argument is
1672 ** changed to its compile-time default.
1673 ** </dl>
1674 */
1675 #define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
1676 #define SQLITE_CONFIG_MULTITHREAD 2 /* nil */
1677 #define SQLITE_CONFIG_SERIALIZED 3 /* nil */
@@ -1654,10 +1691,11 @@
1691 #define SQLITE_CONFIG_URI 17 /* int */
1692 #define SQLITE_CONFIG_PCACHE2 18 /* sqlite3_pcache_methods2* */
1693 #define SQLITE_CONFIG_GETPCACHE2 19 /* sqlite3_pcache_methods2* */
1694 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20 /* int */
1695 #define SQLITE_CONFIG_SQLLOG 21 /* xSqllog, void* */
1696 #define SQLITE_CONFIG_MMAP_SIZE 22 /* sqlite3_int64, sqlite3_int64 */
1697
1698 /*
1699 ** CAPI3REF: Database Connection Configuration Options
1700 **
1701 ** These constants are the available integer configuration options that
@@ -4185,11 +4223,11 @@
4223 ** SQLITE_TRANSIENT value means that the content will likely change in
4224 ** the near future and that SQLite should make its own private copy of
4225 ** the content before returning.
4226 **
4227 ** The typedef is necessary to work around problems in certain
4228 ** C++ compilers.
4229 */
4230 typedef void (*sqlite3_destructor_type)(void*);
4231 #define SQLITE_STATIC ((sqlite3_destructor_type)0)
4232 #define SQLITE_TRANSIENT ((sqlite3_destructor_type)-1)
4233
@@ -4984,15 +5022,24 @@
5022 ** CAPI3REF: Load An Extension
5023 **
5024 ** ^This interface loads an SQLite extension library from the named file.
5025 **
5026 ** ^The sqlite3_load_extension() interface attempts to load an
5027 ** [SQLite extension] library contained in the file zFile. If
5028 ** the file cannot be loaded directly, attempts are made to load
5029 ** with various operating-system specific extensions added.
5030 ** So for example, if "samplelib" cannot be loaded, then names like
5031 ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5032 ** be tried also.
5033 **
5034 ** ^The entry point is zProc.
5035 ** ^(zProc may be 0, in which case SQLite will try to come up with an
5036 ** entry point name on its own. It first tries "sqlite3_extension_init".
5037 ** If that does not work, it constructs a name "sqlite3_X_init" where the
5038 ** X is consists of the lower-case equivalent of all ASCII alphabetic
5039 ** characters in the filename from the last "/" to the first following
5040 ** "." and omitting any initial "lib".)^
5041 ** ^The sqlite3_load_extension() interface returns
5042 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5043 ** ^If an error occurs and pzErrMsg is not 0, then the
5044 ** [sqlite3_load_extension()] interface shall attempt to
5045 ** fill *pzErrMsg with error message text stored in memory
@@ -5014,15 +5061,15 @@
5061
5062 /*
5063 ** CAPI3REF: Enable Or Disable Extension Loading
5064 **
5065 ** ^So as not to open security holes in older applications that are
5066 ** unprepared to deal with [extension loading], and as a means of disabling
5067 ** [extension loading] while evaluating user-entered SQL, the following API
5068 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5069 **
5070 ** ^Extension loading is off by default.
5071 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5072 ** to turn extension loading on and call it with onoff==0 to turn
5073 ** it back off again.
5074 */
5075 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
@@ -5030,11 +5077,11 @@
5077 /*
5078 ** CAPI3REF: Automatically Load Statically Linked Extensions
5079 **
5080 ** ^This interface causes the xEntryPoint() function to be invoked for
5081 ** each new [database connection] that is created. The idea here is that
5082 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
5083 ** that is to be automatically loaded into all new database connections.
5084 **
5085 ** ^(Even though the function prototype shows that xEntryPoint() takes
5086 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5087 ** arguments and expects and integer result as if the signature of the
@@ -6810,10 +6857,25 @@
6857 ** independence" that SQLite uses internally when comparing identifiers.
6858 */
6859 SQLITE_API int sqlite3_stricmp(const char *, const char *);
6860 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
6861
6862 /*
6863 ** CAPI3REF: String Globbing
6864 *
6865 ** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
6866 ** the glob pattern P, and it returns non-zero if string X does not match
6867 ** the glob pattern P. ^The definition of glob pattern matching used in
6868 ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
6869 ** SQL dialect used by SQLite. ^The sqlite3_strglob(P,X) function is case
6870 ** sensitive.
6871 **
6872 ** Note that this routine returns zero on a match and non-zero if the strings
6873 ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
6874 */
6875 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
6876
6877 /*
6878 ** CAPI3REF: Error Logging Interface
6879 **
6880 ** ^The [sqlite3_log()] interface writes a message into the error log
6881 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
6882

Keyboard Shortcuts

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