Fossil SCM

Update to the latest 3.50.0 alpha version of SQLite, to give us access to the latest feature enhancements and for general testing of SQLite.

drh 2025-02-25 18:43 trunk
Commit d23f1ab44a98f8779565ff18eb94e5005ce0313ca1126a9b7f3e5e00943b6416
3 files changed +284 -108 +1133 -517 +46 -1
+284 -108
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -849,11 +849,11 @@
849849
850850
/*
851851
** Prompt strings. Initialized in main. Settable with
852852
** .prompt main continue
853853
*/
854
-#define PROMPT_LEN_MAX 20
854
+#define PROMPT_LEN_MAX 128
855855
/* First line prompt. default: "sqlite> " */
856856
static char mainPrompt[PROMPT_LEN_MAX];
857857
/* Continuation prompt. default: " ...> " */
858858
static char continuePrompt[PROMPT_LEN_MAX];
859859
@@ -21610,10 +21610,11 @@
2161021610
u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
2161121611
u8 bSafeMode; /* True to prohibit unsafe operations */
2161221612
u8 bSafeModePersist; /* The long-term value of bSafeMode */
2161321613
u8 eRestoreState; /* See comments above doAutoDetectRestore() */
2161421614
u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */
21615
+ u8 eEscMode; /* Escape mode for text output */
2161521616
ColModeOpts cmOpts; /* Option values affecting columnar mode output */
2161621617
unsigned statsOn; /* True to display memory stats before each finalize */
2161721618
unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
2161821619
int inputNesting; /* Track nesting level of .read and other redirects */
2161921620
int outCount; /* Revert to stdout when reaching zero */
@@ -21710,10 +21711,18 @@
2171021711
#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
2171121712
** callback limit is reached, and for each
2171221713
** top-level SQL statement */
2171321714
#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
2171421715
21716
+/* Allowed values for ShellState.eEscMode
21717
+*/
21718
+#define SHELL_ESC_SYMBOL 0 /* Substitute U+2400 graphics */
21719
+#define SHELL_ESC_ASCII 1 /* Substitute ^Y for X where Y=X+0x40 */
21720
+#define SHELL_ESC_OFF 2 /* Send characters verbatim */
21721
+
21722
+static const char *shell_EscModeNames[] = { "symbol", "ascii", "off" };
21723
+
2171521724
/*
2171621725
** These are the allowed shellFlgs values
2171721726
*/
2171821727
#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
2171921728
#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
@@ -22047,63 +22056,79 @@
2204722056
sqlite3_fprintf(out, "X'%s'", zStr);
2204822057
sqlite3_free(zStr);
2204922058
}
2205022059
2205122060
/*
22052
-** Find a string that is not found anywhere in z[]. Return a pointer
22053
-** to that string.
22054
-**
22055
-** Try to use zA and zB first. If both of those are already found in z[]
22056
-** then make up some string and store it in the buffer zBuf.
22057
-*/
22058
-static const char *unused_string(
22059
- const char *z, /* Result must not appear anywhere in z */
22060
- const char *zA, const char *zB, /* Try these first */
22061
- char *zBuf /* Space to store a generated string */
22062
-){
22063
- unsigned i = 0;
22064
- if( strstr(z, zA)==0 ) return zA;
22065
- if( strstr(z, zB)==0 ) return zB;
22066
- do{
22067
- sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
22068
- }while( strstr(z,zBuf)!=0 );
22069
- return zBuf;
22070
-}
22071
-
22072
-/*
22073
-** Output the given string as a quoted string using SQL quoting conventions.
22074
-**
22075
-** See also: output_quoted_escaped_string()
22076
-*/
22077
-static void output_quoted_string(ShellState *p, const char *z){
22078
- int i;
22079
- char c;
22061
+** Output the given string as a quoted string using SQL quoting conventions:
22062
+**
22063
+** (1) Single quotes (') within the string are doubled
22064
+** (2) The whle string is enclosed in '...'
22065
+** (3) Control characters other than \n, \t, and \r\n are escaped
22066
+** using \u00XX notation and if such substitutions occur,
22067
+** the whole string is enclosed in unistr('...') instead of '...'.
22068
+**
22069
+** Step (3) is omitted if the control-character escape mode is OFF.
22070
+**
22071
+** See also: output_quoted_escaped_string() which does the same except
22072
+** that it does not make exceptions for \n, \t, and \r\n in step (3).
22073
+*/
22074
+static void output_quoted_string(ShellState *p, const char *zInX){
22075
+ int i;
22076
+ int needUnistr = 0;
22077
+ int needDblQuote = 0;
22078
+ const unsigned char *z = (const unsigned char*)zInX;
22079
+ unsigned char c;
2208022080
FILE *out = p->out;
2208122081
sqlite3_fsetmode(out, _O_BINARY);
2208222082
if( z==0 ) return;
22083
- for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22084
- if( c==0 ){
22083
+ for(i=0; (c = z[i])!=0; i++){
22084
+ if( c=='\'' ){ needDblQuote = 1; }
22085
+ if( c>0x1f ) continue;
22086
+ if( c=='\t' || c=='\n' ) continue;
22087
+ if( c=='\r' && z[i+1]=='\n' ) continue;
22088
+ needUnistr = 1;
22089
+ break;
22090
+ }
22091
+ if( (needDblQuote==0 && needUnistr==0)
22092
+ || (needDblQuote==0 && p->eEscMode==SHELL_ESC_OFF)
22093
+ ){
2208522094
sqlite3_fprintf(out, "'%s'",z);
22095
+ }else if( p->eEscMode==SHELL_ESC_OFF ){
22096
+ char *zEncoded = sqlite3_mprintf("%Q", z);
22097
+ sqlite3_fputs(zEncoded, out);
22098
+ sqlite3_free(zEncoded);
2208622099
}else{
22087
- sqlite3_fputs("'", out);
22100
+ if( needUnistr ){
22101
+ sqlite3_fputs("unistr('", out);
22102
+ }else{
22103
+ sqlite3_fputs("'", out);
22104
+ }
2208822105
while( *z ){
22089
- for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22090
- if( c=='\'' ) i++;
22106
+ for(i=0; (c = z[i])!=0; i++){
22107
+ if( c=='\'' ) break;
22108
+ if( c>0x1f ) continue;
22109
+ if( c=='\t' || c=='\n' ) continue;
22110
+ if( c=='\r' && z[i+1]=='\n' ) continue;
22111
+ break;
22112
+ }
2209122113
if( i ){
2209222114
sqlite3_fprintf(out, "%.*s", i, z);
2209322115
z += i;
2209422116
}
22117
+ if( c==0 ) break;
2209522118
if( c=='\'' ){
22096
- sqlite3_fputs("'", out);
22097
- continue;
22098
- }
22099
- if( c==0 ){
22100
- break;
22119
+ sqlite3_fputs("''", out);
22120
+ }else{
22121
+ sqlite3_fprintf(out, "\\u%04x", c);
2210122122
}
2210222123
z++;
2210322124
}
22104
- sqlite3_fputs("'", out);
22125
+ if( needUnistr ){
22126
+ sqlite3_fputs("')", out);
22127
+ }else{
22128
+ sqlite3_fputs("'", out);
22129
+ }
2210522130
}
2210622131
setCrlfMode(p);
2210722132
}
2210822133
2210922134
/*
@@ -22114,65 +22139,19 @@
2211422139
**
2211522140
** This is like output_quoted_string() but with the addition of the \r\n
2211622141
** escape mechanism.
2211722142
*/
2211822143
static void output_quoted_escaped_string(ShellState *p, const char *z){
22119
- int i;
22120
- char c;
22121
- FILE *out = p->out;
22122
- sqlite3_fsetmode(out, _O_BINARY);
22123
- for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
22124
- if( c==0 ){
22125
- sqlite3_fprintf(out, "'%s'",z);
22144
+ char *zEscaped;
22145
+ sqlite3_fsetmode(p->out, _O_BINARY);
22146
+ if( p->eEscMode==SHELL_ESC_OFF ){
22147
+ zEscaped = sqlite3_mprintf("%Q", z);
2212622148
}else{
22127
- const char *zNL = 0;
22128
- const char *zCR = 0;
22129
- int nNL = 0;
22130
- int nCR = 0;
22131
- char zBuf1[20], zBuf2[20];
22132
- for(i=0; z[i]; i++){
22133
- if( z[i]=='\n' ) nNL++;
22134
- if( z[i]=='\r' ) nCR++;
22135
- }
22136
- if( nNL ){
22137
- sqlite3_fputs("replace(", out);
22138
- zNL = unused_string(z, "\\n", "\\012", zBuf1);
22139
- }
22140
- if( nCR ){
22141
- sqlite3_fputs("replace(", out);
22142
- zCR = unused_string(z, "\\r", "\\015", zBuf2);
22143
- }
22144
- sqlite3_fputs("'", out);
22145
- while( *z ){
22146
- for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
22147
- if( c=='\'' ) i++;
22148
- if( i ){
22149
- sqlite3_fprintf(out, "%.*s", i, z);
22150
- z += i;
22151
- }
22152
- if( c=='\'' ){
22153
- sqlite3_fputs("'", out);
22154
- continue;
22155
- }
22156
- if( c==0 ){
22157
- break;
22158
- }
22159
- z++;
22160
- if( c=='\n' ){
22161
- sqlite3_fputs(zNL, out);
22162
- continue;
22163
- }
22164
- sqlite3_fputs(zCR, out);
22165
- }
22166
- sqlite3_fputs("'", out);
22167
- if( nCR ){
22168
- sqlite3_fprintf(out, ",'%s',char(13))", zCR);
22169
- }
22170
- if( nNL ){
22171
- sqlite3_fprintf(out, ",'%s',char(10))", zNL);
22172
- }
22173
- }
22149
+ zEscaped = sqlite3_mprintf("%#Q", z);
22150
+ }
22151
+ sqlite3_fputs(zEscaped, p->out);
22152
+ sqlite3_free(zEscaped);
2217422153
setCrlfMode(p);
2217522154
}
2217622155
2217722156
/*
2217822157
** Find earliest of chars within s specified in zAny.
@@ -22317,10 +22296,97 @@
2231722296
sqlite3_fputs(ace+1, out);
2231822297
}
2231922298
}
2232022299
sqlite3_fputs(zq, out);
2232122300
}
22301
+
22302
+/*
22303
+** Escape the input string if it is needed and in accordance with
22304
+** eEscMode.
22305
+**
22306
+** Escaping is needed if the string contains any control characters
22307
+** other than \t, \n, and \r\n
22308
+**
22309
+** If no escaping is needed (the common case) then set *ppFree to NULL
22310
+** and return the original string. If escapingn is needed, write the
22311
+** escaped string into memory obtained from sqlite3_malloc64() or the
22312
+** equivalent, and return the new string and set *ppFree to the new string
22313
+** as well.
22314
+**
22315
+** The caller is responsible for freeing *ppFree if it is non-NULL in order
22316
+** to reclaim memory.
22317
+*/
22318
+static const char *escapeOutput(
22319
+ ShellState *p,
22320
+ const char *zInX,
22321
+ char **ppFree
22322
+){
22323
+ i64 i, j;
22324
+ i64 nCtrl = 0;
22325
+ unsigned char *zIn;
22326
+ unsigned char c;
22327
+ unsigned char *zOut;
22328
+
22329
+
22330
+ /* No escaping if disabled */
22331
+ if( p->eEscMode==SHELL_ESC_OFF ){
22332
+ *ppFree = 0;
22333
+ return zInX;
22334
+ }
22335
+
22336
+ /* Count the number of control characters in the string. */
22337
+ zIn = (unsigned char*)zInX;
22338
+ for(i=0; (c = zIn[i])!=0; i++){
22339
+ if( c<=0x1f
22340
+ && c!='\t'
22341
+ && c!='\n'
22342
+ && (c!='\r' || zIn[i+1]!='\n')
22343
+ ){
22344
+ nCtrl++;
22345
+ }
22346
+ }
22347
+ if( nCtrl==0 ){
22348
+ *ppFree = 0;
22349
+ return zInX;
22350
+ }
22351
+ if( p->eEscMode==SHELL_ESC_SYMBOL ) nCtrl *= 2;
22352
+ zOut = sqlite3_malloc64( i + nCtrl + 1 );
22353
+ shell_check_oom(zOut);
22354
+ for(i=j=0; (c = zIn[i])!=0; i++){
22355
+ if( c>0x1f
22356
+ || c=='\t'
22357
+ || c=='\n'
22358
+ || (c=='\r' && zIn[i+1]=='\n')
22359
+ ){
22360
+ continue;
22361
+ }
22362
+ if( i>0 ){
22363
+ memcpy(&zOut[j], zIn, i);
22364
+ j += i;
22365
+ }
22366
+ zIn += i+1;
22367
+ i = -1;
22368
+ switch( p->eEscMode ){
22369
+ case SHELL_ESC_SYMBOL:
22370
+ zOut[j++] = 0xe2;
22371
+ zOut[j++] = 0x90;
22372
+ zOut[j++] = 0x80+c;
22373
+ break;
22374
+ case SHELL_ESC_ASCII:
22375
+ zOut[j++] = '^';
22376
+ zOut[j++] = 0x40+c;
22377
+ break;
22378
+ }
22379
+ }
22380
+ if( i>0 ){
22381
+ memcpy(&zOut[j], zIn, i);
22382
+ j += i;
22383
+ }
22384
+ zOut[j] = 0;
22385
+ *ppFree = (char*)zOut;
22386
+ return (char*)zOut;
22387
+}
2232222388
2232322389
/*
2232422390
** Output the given string with characters that are special to
2232522391
** HTML escaped.
2232622392
*/
@@ -22761,12 +22827,16 @@
2276122827
int len = strlen30(azCol[i] ? azCol[i] : "");
2276222828
if( len>w ) w = len;
2276322829
}
2276422830
if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
2276522831
for(i=0; i<nArg; i++){
22832
+ char *pFree = 0;
22833
+ const char *pDisplay;
22834
+ pDisplay = escapeOutput(p, azArg[i] ? azArg[i] : p->nullValue, &pFree);
2276622835
sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
22767
- azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
22836
+ pDisplay, p->rowSeparator);
22837
+ if( pFree ) sqlite3_free(pFree);
2276822838
}
2276922839
break;
2277022840
}
2277122841
case MODE_ScanExp:
2277222842
case MODE_Explain: {
@@ -22894,19 +22964,27 @@
2289422964
break;
2289522965
}
2289622966
case MODE_List: {
2289722967
if( p->cnt++==0 && p->showHeader ){
2289822968
for(i=0; i<nArg; i++){
22899
- sqlite3_fprintf(p->out, "%s%s", azCol[i],
22969
+ char *z = azCol[i];
22970
+ char *pFree;
22971
+ const char *zOut = escapeOutput(p, z, &pFree);
22972
+ sqlite3_fprintf(p->out, "%s%s", zOut,
2290022973
i==nArg-1 ? p->rowSeparator : p->colSeparator);
22974
+ if( pFree ) sqlite3_free(pFree);
2290122975
}
2290222976
}
2290322977
if( azArg==0 ) break;
2290422978
for(i=0; i<nArg; i++){
2290522979
char *z = azArg[i];
22980
+ char *pFree;
22981
+ const char *zOut;
2290622982
if( z==0 ) z = p->nullValue;
22907
- sqlite3_fputs(z, p->out);
22983
+ zOut = escapeOutput(p, z, &pFree);
22984
+ sqlite3_fputs(zOut, p->out);
22985
+ if( pFree ) sqlite3_free(pFree);
2290822986
sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
2290922987
}
2291022988
break;
2291122989
}
2291222990
case MODE_Www:
@@ -24021,10 +24099,11 @@
2402124099
** from malloc()) of that first line, which caller should free sometime.
2402224100
** Write anything to display on the next line into *pzTail. If this is
2402324101
** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
2402424102
*/
2402524103
static char *translateForDisplayAndDup(
24104
+ ShellState *p, /* To access current settings */
2402624105
const unsigned char *z, /* Input text to be transformed */
2402724106
const unsigned char **pzTail, /* OUT: Tail of the input for next line */
2402824107
int mxWidth, /* Max width. 0 means no limit */
2402924108
u8 bWordWrap /* If true, avoid breaking mid-word */
2403024109
){
@@ -24055,19 +24134,22 @@
2405524134
n++;
2405624135
i++;
2405724136
j++;
2405824137
continue;
2405924138
}
24139
+ if( c==0 || c=='\n' || (c=='\r' && z[i+1]=='\n') ) break;
2406024140
if( c=='\t' ){
2406124141
do{
2406224142
n++;
2406324143
j++;
2406424144
}while( (n&7)!=0 && n<mxWidth );
2406524145
i++;
2406624146
continue;
2406724147
}
24068
- break;
24148
+ n++;
24149
+ j += 3;
24150
+ i++;
2406924151
}
2407024152
if( n>=mxWidth && bWordWrap ){
2407124153
/* Perhaps try to back up to a better place to break the line */
2407224154
for(k=i; k>i/2; k--){
2407324155
if( isspace(z[k-1]) ) break;
@@ -24110,23 +24192,48 @@
2411024192
if( c>=' ' ){
2411124193
n++;
2411224194
zOut[j++] = z[i++];
2411324195
continue;
2411424196
}
24197
+ if( c==0 ) break;
2411524198
if( z[i]=='\t' ){
2411624199
do{
2411724200
n++;
2411824201
zOut[j++] = ' ';
2411924202
}while( (n&7)!=0 && n<mxWidth );
2412024203
i++;
2412124204
continue;
2412224205
}
24123
- break;
24206
+ switch( p->eEscMode ){
24207
+ case SHELL_ESC_SYMBOL:
24208
+ zOut[j++] = 0xe2;
24209
+ zOut[j++] = 0x90;
24210
+ zOut[j++] = 0x80 + c;
24211
+ break;
24212
+ case SHELL_ESC_ASCII:
24213
+ zOut[j++] = '^';
24214
+ zOut[j++] = 0x40 + c;
24215
+ break;
24216
+ case SHELL_ESC_OFF:
24217
+ zOut[j++] = c;
24218
+ break;
24219
+ }
24220
+ i++;
2412424221
}
2412524222
zOut[j] = 0;
2412624223
return (char*)zOut;
2412724224
}
24225
+
24226
+/* Return true if the text string z[] contains characters that need
24227
+** unistr() escaping.
24228
+*/
24229
+static int needUnistr(const unsigned char *z){
24230
+ unsigned char c;
24231
+ if( z==0 ) return 0;
24232
+ while( (c = *z)>0x1f || c=='\t' || c=='\n' || (c=='\r' && z[1]=='\n') ){ z++; }
24233
+ return c!=0;
24234
+}
2412824235
2412924236
/* Extract the value of the i-th current column for pStmt as an SQL literal
2413024237
** value. Memory is obtained from sqlite3_malloc64() and must be freed by
2413124238
** the caller.
2413224239
*/
@@ -24138,11 +24245,12 @@
2413824245
case SQLITE_INTEGER:
2413924246
case SQLITE_FLOAT: {
2414024247
return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
2414124248
}
2414224249
case SQLITE_TEXT: {
24143
- return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
24250
+ const unsigned char *zText = sqlite3_column_text(pStmt,i);
24251
+ return sqlite3_mprintf(needUnistr(zText)?"%#Q":"%Q",zText);
2414424252
}
2414524253
case SQLITE_BLOB: {
2414624254
int j;
2414724255
sqlite3_str *pStr = sqlite3_str_new(0);
2414824256
const unsigned char *a = sqlite3_column_blob(pStmt,i);
@@ -24230,11 +24338,11 @@
2423024338
wx = p->cmOpts.iWrap;
2423124339
}
2423224340
if( wx<0 ) wx = -wx;
2423324341
uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
2423424342
if( uz==0 ) uz = (u8*)"";
24235
- azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
24343
+ azData[i] = translateForDisplayAndDup(p, uz, &zNotUsed, wx, bw);
2423624344
}
2423724345
do{
2423824346
int useNextLine = bNextLine;
2423924347
bNextLine = 0;
2424024348
if( (nRow+2)*nColumn >= nAlloc ){
@@ -24254,19 +24362,20 @@
2425424362
if( wx<0 ) wx = -wx;
2425524363
if( useNextLine ){
2425624364
uz = azNextLine[i];
2425724365
if( uz==0 ) uz = (u8*)zEmpty;
2425824366
}else if( p->cmOpts.bQuote ){
24367
+ assert( azQuoted!=0 );
2425924368
sqlite3_free(azQuoted[i]);
2426024369
azQuoted[i] = quoted_column(pStmt,i);
2426124370
uz = (const unsigned char*)azQuoted[i];
2426224371
}else{
2426324372
uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
2426424373
if( uz==0 ) uz = (u8*)zShowNull;
2426524374
}
2426624375
azData[nRow*nColumn + i]
24267
- = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
24376
+ = translateForDisplayAndDup(p, uz, &azNextLine[i], wx, bw);
2426824377
if( azNextLine[i] ){
2426924378
bNextLine = 1;
2427024379
abRowDiv[nRow-1] = 0;
2427124380
bMultiLineRowExists = 1;
2427224381
}
@@ -25185,11 +25294,11 @@
2518525294
#if !defined(SQLITE_SHELL_FIDDLE)
2518625295
".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
2518725296
#else
2518825297
".log on|off Turn logging on or off.",
2518925298
#endif
25190
- ".mode MODE ?OPTIONS? Set output mode",
25299
+ ".mode ?MODE? ?OPTIONS? Set output mode",
2519125300
" MODE is one of:",
2519225301
" ascii Columns/rows delimited by 0x1F and 0x1E",
2519325302
" box Tables using unicode box-drawing characters",
2519425303
" csv Comma-separated values",
2519525304
" column Output in columns. (See .width)",
@@ -25203,10 +25312,11 @@
2520325312
" quote Escape answers as for SQL",
2520425313
" table ASCII-art table",
2520525314
" tabs Tab-separated values",
2520625315
" tcl TCL list elements",
2520725316
" OPTIONS: (for columnar modes or insert mode):",
25317
+ " --escape T ctrl-char escape; T is one of: symbol, ascii, off",
2520825318
" --wrap N Wrap output lines to no longer than N characters",
2520925319
" --wordwrap B Wrap or not at word boundaries per B (on/off)",
2521025320
" --ww Shorthand for \"--wordwrap 1\"",
2521125321
" --quote Quote output text as SQL literals",
2521225322
" --noquote Do not quote output text",
@@ -29943,28 +30053,56 @@
2994330053
2994430054
if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
2994530055
const char *zMode = 0;
2994630056
const char *zTabname = 0;
2994730057
int i, n2;
30058
+ int chng = 0; /* 0x01: change to cmopts. 0x02: Any other change */
2994830059
ColModeOpts cmOpts = ColModeOpts_default;
2994930060
for(i=1; i<nArg; i++){
2995030061
const char *z = azArg[i];
2995130062
if( optionMatch(z,"wrap") && i+1<nArg ){
2995230063
cmOpts.iWrap = integerValue(azArg[++i]);
30064
+ chng |= 1;
2995330065
}else if( optionMatch(z,"ww") ){
2995430066
cmOpts.bWordWrap = 1;
30067
+ chng |= 1;
2995530068
}else if( optionMatch(z,"wordwrap") && i+1<nArg ){
2995630069
cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
30070
+ chng |= 1;
2995730071
}else if( optionMatch(z,"quote") ){
2995830072
cmOpts.bQuote = 1;
30073
+ chng |= 1;
2995930074
}else if( optionMatch(z,"noquote") ){
2996030075
cmOpts.bQuote = 0;
30076
+ chng |= 1;
30077
+ }else if( optionMatch(z,"escape") && i+1<nArg ){
30078
+ /* See similar code at tag-20250224-1 */
30079
+ const char *zEsc = azArg[++i];
30080
+ int k;
30081
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
30082
+ if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
30083
+ p->eEscMode = k;
30084
+ chng |= 2;
30085
+ break;
30086
+ }
30087
+ }
30088
+ if( k>=ArraySize(shell_EscModeNames) ){
30089
+ sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
30090
+ " - choices:", zEsc);
30091
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
30092
+ sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
30093
+ }
30094
+ sqlite3_fprintf(stderr, "\n");
30095
+ rc = 1;
30096
+ goto meta_command_exit;
30097
+ }
2996130098
}else if( zMode==0 ){
2996230099
zMode = z;
2996330100
/* Apply defaults for qbox pseudo-mode. If that
2996430101
* overwrites already-set values, user was informed of this.
2996530102
*/
30103
+ chng |= 1;
2996630104
if( cli_strcmp(z, "qbox")==0 ){
2996730105
ColModeOpts cmo = ColModeOpts_default_qbox;
2996830106
zMode = "box";
2996930107
cmOpts = cmo;
2997030108
}
@@ -29971,10 +30109,11 @@
2997130109
}else if( zTabname==0 ){
2997230110
zTabname = z;
2997330111
}else if( z[0]=='-' ){
2997430112
sqlite3_fprintf(stderr,"unknown option: %s\n", z);
2997530113
eputz("options:\n"
30114
+ " --escape MODE\n"
2997630115
" --noquote\n"
2997730116
" --quote\n"
2997830117
" --wordwrap on/off\n"
2997930118
" --wrap N\n"
2998030119
" --ww\n");
@@ -29984,24 +30123,33 @@
2998430123
sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
2998530124
rc = 1;
2998630125
goto meta_command_exit;
2998730126
}
2998830127
}
29989
- if( zMode==0 ){
30128
+ if( !chng ){
2999030129
if( p->mode==MODE_Column
2999130130
|| (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
2999230131
){
2999330132
sqlite3_fprintf(p->out,
29994
- "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
30133
+ "current output mode: %s --wrap %d --wordwrap %s "
30134
+ "--%squote --escape %s\n",
2999530135
modeDescr[p->mode], p->cmOpts.iWrap,
2999630136
p->cmOpts.bWordWrap ? "on" : "off",
29997
- p->cmOpts.bQuote ? "" : "no");
30137
+ p->cmOpts.bQuote ? "" : "no",
30138
+ shell_EscModeNames[p->eEscMode]
30139
+ );
2999830140
}else{
2999930141
sqlite3_fprintf(p->out,
30000
- "current output mode: %s\n", modeDescr[p->mode]);
30142
+ "current output mode: %s --escape %s\n",
30143
+ modeDescr[p->mode],
30144
+ shell_EscModeNames[p->eEscMode]
30145
+ );
3000130146
}
30147
+ }
30148
+ if( zMode==0 ){
3000230149
zMode = modeDescr[p->mode];
30150
+ if( (chng&1)==0 ) cmOpts = p->cmOpts;
3000330151
}
3000430152
n2 = strlen30(zMode);
3000530153
if( cli_strncmp(zMode,"lines",n2)==0 ){
3000630154
p->mode = MODE_Line;
3000730155
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -30030,10 +30178,15 @@
3003030178
p->mode = MODE_List;
3003130179
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
3003230180
}else if( cli_strncmp(zMode,"insert",n2)==0 ){
3003330181
p->mode = MODE_Insert;
3003430182
set_table_name(p, zTabname ? zTabname : "table");
30183
+ if( p->eEscMode==SHELL_ESC_OFF ){
30184
+ ShellSetFlag(p, SHFLG_Newlines);
30185
+ }else{
30186
+ ShellClearFlag(p, SHFLG_Newlines);
30187
+ }
3003530188
}else if( cli_strncmp(zMode,"quote",n2)==0 ){
3003630189
p->mode = MODE_Quote;
3003730190
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
3003830191
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
3003930192
}else if( cli_strncmp(zMode,"ascii",n2)==0 ){
@@ -32750,10 +32903,11 @@
3275032903
" -csv set output mode to 'csv'\n"
3275132904
#if !defined(SQLITE_OMIT_DESERIALIZE)
3275232905
" -deserialize open the database using sqlite3_deserialize()\n"
3275332906
#endif
3275432907
" -echo print inputs before execution\n"
32908
+ " -escape T ctrl-char escape; T is one of: symbol, ascii, off\n"
3275532909
" -init FILENAME read/process named file\n"
3275632910
" -[no]header turn headers on or off\n"
3275732911
#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
3275832912
" -heap SIZE Size of heap for memsys3 or memsys5\n"
3275932913
#endif
@@ -32797,11 +32951,11 @@
3279732951
#ifdef SQLITE_HAVE_ZLIB
3279832952
" -zip open the file as a ZIP Archive\n"
3279932953
#endif
3280032954
;
3280132955
static void usage(int showDetail){
32802
- sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
32956
+ sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n"
3280332957
"FILENAME is the name of an SQLite database. A new database is created\n"
3280432958
"if the file does not previously exist. Defaults to :memory:.\n", Argv0);
3280532959
if( showDetail ){
3280632960
sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
3280732961
}else{
@@ -33183,10 +33337,13 @@
3318333337
data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
3318433338
}else if( cli_strcmp(z,"-unsafe-testing")==0 ){
3318533339
ShellSetFlag(&data,SHFLG_TestingMode);
3318633340
}else if( cli_strcmp(z,"-safe")==0 ){
3318733341
/* no-op - catch this on the second pass */
33342
+ }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
33343
+ /* skip over the argument */
33344
+ i++;
3318833345
}
3318933346
}
3319033347
#ifndef SQLITE_SHELL_FIDDLE
3319133348
if( !bEnableVfstrace ) verify_uninitialized();
3319233349
#endif
@@ -33282,10 +33439,29 @@
3328233439
}else if( cli_strcmp(z,"-box")==0 ){
3328333440
data.mode = MODE_Box;
3328433441
}else if( cli_strcmp(z,"-csv")==0 ){
3328533442
data.mode = MODE_Csv;
3328633443
memcpy(data.colSeparator,",",2);
33444
+ }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
33445
+ /* See similar code at tag-20250224-1 */
33446
+ const char *zEsc = argv[++i];
33447
+ int k;
33448
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
33449
+ if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
33450
+ data.eEscMode = k;
33451
+ break;
33452
+ }
33453
+ }
33454
+ if( k>=ArraySize(shell_EscModeNames) ){
33455
+ sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
33456
+ " - choices:", zEsc);
33457
+ for(k=0; k<ArraySize(shell_EscModeNames); k++){
33458
+ sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
33459
+ }
33460
+ sqlite3_fprintf(stderr, "\n");
33461
+ exit(1);
33462
+ }
3328733463
#ifdef SQLITE_HAVE_ZLIB
3328833464
}else if( cli_strcmp(z,"-zip")==0 ){
3328933465
data.openMode = SHELL_OPEN_ZIPFILE;
3329033466
#endif
3329133467
}else if( cli_strcmp(z,"-append")==0 ){
3329233468
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -849,11 +849,11 @@
849
850 /*
851 ** Prompt strings. Initialized in main. Settable with
852 ** .prompt main continue
853 */
854 #define PROMPT_LEN_MAX 20
855 /* First line prompt. default: "sqlite> " */
856 static char mainPrompt[PROMPT_LEN_MAX];
857 /* Continuation prompt. default: " ...> " */
858 static char continuePrompt[PROMPT_LEN_MAX];
859
@@ -21610,10 +21610,11 @@
21610 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
21611 u8 bSafeMode; /* True to prohibit unsafe operations */
21612 u8 bSafeModePersist; /* The long-term value of bSafeMode */
21613 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
21614 u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */
 
21615 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
21616 unsigned statsOn; /* True to display memory stats before each finalize */
21617 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
21618 int inputNesting; /* Track nesting level of .read and other redirects */
21619 int outCount; /* Revert to stdout when reaching zero */
@@ -21710,10 +21711,18 @@
21710 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
21711 ** callback limit is reached, and for each
21712 ** top-level SQL statement */
21713 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
21714
 
 
 
 
 
 
 
 
21715 /*
21716 ** These are the allowed shellFlgs values
21717 */
21718 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
21719 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
@@ -22047,63 +22056,79 @@
22047 sqlite3_fprintf(out, "X'%s'", zStr);
22048 sqlite3_free(zStr);
22049 }
22050
22051 /*
22052 ** Find a string that is not found anywhere in z[]. Return a pointer
22053 ** to that string.
22054 **
22055 ** Try to use zA and zB first. If both of those are already found in z[]
22056 ** then make up some string and store it in the buffer zBuf.
22057 */
22058 static const char *unused_string(
22059 const char *z, /* Result must not appear anywhere in z */
22060 const char *zA, const char *zB, /* Try these first */
22061 char *zBuf /* Space to store a generated string */
22062 ){
22063 unsigned i = 0;
22064 if( strstr(z, zA)==0 ) return zA;
22065 if( strstr(z, zB)==0 ) return zB;
22066 do{
22067 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
22068 }while( strstr(z,zBuf)!=0 );
22069 return zBuf;
22070 }
22071
22072 /*
22073 ** Output the given string as a quoted string using SQL quoting conventions.
22074 **
22075 ** See also: output_quoted_escaped_string()
22076 */
22077 static void output_quoted_string(ShellState *p, const char *z){
22078 int i;
22079 char c;
22080 FILE *out = p->out;
22081 sqlite3_fsetmode(out, _O_BINARY);
22082 if( z==0 ) return;
22083 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22084 if( c==0 ){
 
 
 
 
 
 
 
 
 
22085 sqlite3_fprintf(out, "'%s'",z);
 
 
 
 
22086 }else{
22087 sqlite3_fputs("'", out);
 
 
 
 
22088 while( *z ){
22089 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
22090 if( c=='\'' ) i++;
 
 
 
 
 
22091 if( i ){
22092 sqlite3_fprintf(out, "%.*s", i, z);
22093 z += i;
22094 }
 
22095 if( c=='\'' ){
22096 sqlite3_fputs("'", out);
22097 continue;
22098 }
22099 if( c==0 ){
22100 break;
22101 }
22102 z++;
22103 }
22104 sqlite3_fputs("'", out);
 
 
 
 
22105 }
22106 setCrlfMode(p);
22107 }
22108
22109 /*
@@ -22114,65 +22139,19 @@
22114 **
22115 ** This is like output_quoted_string() but with the addition of the \r\n
22116 ** escape mechanism.
22117 */
22118 static void output_quoted_escaped_string(ShellState *p, const char *z){
22119 int i;
22120 char c;
22121 FILE *out = p->out;
22122 sqlite3_fsetmode(out, _O_BINARY);
22123 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
22124 if( c==0 ){
22125 sqlite3_fprintf(out, "'%s'",z);
22126 }else{
22127 const char *zNL = 0;
22128 const char *zCR = 0;
22129 int nNL = 0;
22130 int nCR = 0;
22131 char zBuf1[20], zBuf2[20];
22132 for(i=0; z[i]; i++){
22133 if( z[i]=='\n' ) nNL++;
22134 if( z[i]=='\r' ) nCR++;
22135 }
22136 if( nNL ){
22137 sqlite3_fputs("replace(", out);
22138 zNL = unused_string(z, "\\n", "\\012", zBuf1);
22139 }
22140 if( nCR ){
22141 sqlite3_fputs("replace(", out);
22142 zCR = unused_string(z, "\\r", "\\015", zBuf2);
22143 }
22144 sqlite3_fputs("'", out);
22145 while( *z ){
22146 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
22147 if( c=='\'' ) i++;
22148 if( i ){
22149 sqlite3_fprintf(out, "%.*s", i, z);
22150 z += i;
22151 }
22152 if( c=='\'' ){
22153 sqlite3_fputs("'", out);
22154 continue;
22155 }
22156 if( c==0 ){
22157 break;
22158 }
22159 z++;
22160 if( c=='\n' ){
22161 sqlite3_fputs(zNL, out);
22162 continue;
22163 }
22164 sqlite3_fputs(zCR, out);
22165 }
22166 sqlite3_fputs("'", out);
22167 if( nCR ){
22168 sqlite3_fprintf(out, ",'%s',char(13))", zCR);
22169 }
22170 if( nNL ){
22171 sqlite3_fprintf(out, ",'%s',char(10))", zNL);
22172 }
22173 }
22174 setCrlfMode(p);
22175 }
22176
22177 /*
22178 ** Find earliest of chars within s specified in zAny.
@@ -22317,10 +22296,97 @@
22317 sqlite3_fputs(ace+1, out);
22318 }
22319 }
22320 sqlite3_fputs(zq, out);
22321 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22322
22323 /*
22324 ** Output the given string with characters that are special to
22325 ** HTML escaped.
22326 */
@@ -22761,12 +22827,16 @@
22761 int len = strlen30(azCol[i] ? azCol[i] : "");
22762 if( len>w ) w = len;
22763 }
22764 if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
22765 for(i=0; i<nArg; i++){
 
 
 
22766 sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
22767 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
 
22768 }
22769 break;
22770 }
22771 case MODE_ScanExp:
22772 case MODE_Explain: {
@@ -22894,19 +22964,27 @@
22894 break;
22895 }
22896 case MODE_List: {
22897 if( p->cnt++==0 && p->showHeader ){
22898 for(i=0; i<nArg; i++){
22899 sqlite3_fprintf(p->out, "%s%s", azCol[i],
 
 
 
22900 i==nArg-1 ? p->rowSeparator : p->colSeparator);
 
22901 }
22902 }
22903 if( azArg==0 ) break;
22904 for(i=0; i<nArg; i++){
22905 char *z = azArg[i];
 
 
22906 if( z==0 ) z = p->nullValue;
22907 sqlite3_fputs(z, p->out);
 
 
22908 sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
22909 }
22910 break;
22911 }
22912 case MODE_Www:
@@ -24021,10 +24099,11 @@
24021 ** from malloc()) of that first line, which caller should free sometime.
24022 ** Write anything to display on the next line into *pzTail. If this is
24023 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
24024 */
24025 static char *translateForDisplayAndDup(
 
24026 const unsigned char *z, /* Input text to be transformed */
24027 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
24028 int mxWidth, /* Max width. 0 means no limit */
24029 u8 bWordWrap /* If true, avoid breaking mid-word */
24030 ){
@@ -24055,19 +24134,22 @@
24055 n++;
24056 i++;
24057 j++;
24058 continue;
24059 }
 
24060 if( c=='\t' ){
24061 do{
24062 n++;
24063 j++;
24064 }while( (n&7)!=0 && n<mxWidth );
24065 i++;
24066 continue;
24067 }
24068 break;
 
 
24069 }
24070 if( n>=mxWidth && bWordWrap ){
24071 /* Perhaps try to back up to a better place to break the line */
24072 for(k=i; k>i/2; k--){
24073 if( isspace(z[k-1]) ) break;
@@ -24110,23 +24192,48 @@
24110 if( c>=' ' ){
24111 n++;
24112 zOut[j++] = z[i++];
24113 continue;
24114 }
 
24115 if( z[i]=='\t' ){
24116 do{
24117 n++;
24118 zOut[j++] = ' ';
24119 }while( (n&7)!=0 && n<mxWidth );
24120 i++;
24121 continue;
24122 }
24123 break;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24124 }
24125 zOut[j] = 0;
24126 return (char*)zOut;
24127 }
 
 
 
 
 
 
 
 
 
 
24128
24129 /* Extract the value of the i-th current column for pStmt as an SQL literal
24130 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
24131 ** the caller.
24132 */
@@ -24138,11 +24245,12 @@
24138 case SQLITE_INTEGER:
24139 case SQLITE_FLOAT: {
24140 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
24141 }
24142 case SQLITE_TEXT: {
24143 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
 
24144 }
24145 case SQLITE_BLOB: {
24146 int j;
24147 sqlite3_str *pStr = sqlite3_str_new(0);
24148 const unsigned char *a = sqlite3_column_blob(pStmt,i);
@@ -24230,11 +24338,11 @@
24230 wx = p->cmOpts.iWrap;
24231 }
24232 if( wx<0 ) wx = -wx;
24233 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
24234 if( uz==0 ) uz = (u8*)"";
24235 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
24236 }
24237 do{
24238 int useNextLine = bNextLine;
24239 bNextLine = 0;
24240 if( (nRow+2)*nColumn >= nAlloc ){
@@ -24254,19 +24362,20 @@
24254 if( wx<0 ) wx = -wx;
24255 if( useNextLine ){
24256 uz = azNextLine[i];
24257 if( uz==0 ) uz = (u8*)zEmpty;
24258 }else if( p->cmOpts.bQuote ){
 
24259 sqlite3_free(azQuoted[i]);
24260 azQuoted[i] = quoted_column(pStmt,i);
24261 uz = (const unsigned char*)azQuoted[i];
24262 }else{
24263 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
24264 if( uz==0 ) uz = (u8*)zShowNull;
24265 }
24266 azData[nRow*nColumn + i]
24267 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
24268 if( azNextLine[i] ){
24269 bNextLine = 1;
24270 abRowDiv[nRow-1] = 0;
24271 bMultiLineRowExists = 1;
24272 }
@@ -25185,11 +25294,11 @@
25185 #if !defined(SQLITE_SHELL_FIDDLE)
25186 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
25187 #else
25188 ".log on|off Turn logging on or off.",
25189 #endif
25190 ".mode MODE ?OPTIONS? Set output mode",
25191 " MODE is one of:",
25192 " ascii Columns/rows delimited by 0x1F and 0x1E",
25193 " box Tables using unicode box-drawing characters",
25194 " csv Comma-separated values",
25195 " column Output in columns. (See .width)",
@@ -25203,10 +25312,11 @@
25203 " quote Escape answers as for SQL",
25204 " table ASCII-art table",
25205 " tabs Tab-separated values",
25206 " tcl TCL list elements",
25207 " OPTIONS: (for columnar modes or insert mode):",
 
25208 " --wrap N Wrap output lines to no longer than N characters",
25209 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
25210 " --ww Shorthand for \"--wordwrap 1\"",
25211 " --quote Quote output text as SQL literals",
25212 " --noquote Do not quote output text",
@@ -29943,28 +30053,56 @@
29943
29944 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
29945 const char *zMode = 0;
29946 const char *zTabname = 0;
29947 int i, n2;
 
29948 ColModeOpts cmOpts = ColModeOpts_default;
29949 for(i=1; i<nArg; i++){
29950 const char *z = azArg[i];
29951 if( optionMatch(z,"wrap") && i+1<nArg ){
29952 cmOpts.iWrap = integerValue(azArg[++i]);
 
29953 }else if( optionMatch(z,"ww") ){
29954 cmOpts.bWordWrap = 1;
 
29955 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
29956 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
 
29957 }else if( optionMatch(z,"quote") ){
29958 cmOpts.bQuote = 1;
 
29959 }else if( optionMatch(z,"noquote") ){
29960 cmOpts.bQuote = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29961 }else if( zMode==0 ){
29962 zMode = z;
29963 /* Apply defaults for qbox pseudo-mode. If that
29964 * overwrites already-set values, user was informed of this.
29965 */
 
29966 if( cli_strcmp(z, "qbox")==0 ){
29967 ColModeOpts cmo = ColModeOpts_default_qbox;
29968 zMode = "box";
29969 cmOpts = cmo;
29970 }
@@ -29971,10 +30109,11 @@
29971 }else if( zTabname==0 ){
29972 zTabname = z;
29973 }else if( z[0]=='-' ){
29974 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
29975 eputz("options:\n"
 
29976 " --noquote\n"
29977 " --quote\n"
29978 " --wordwrap on/off\n"
29979 " --wrap N\n"
29980 " --ww\n");
@@ -29984,24 +30123,33 @@
29984 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
29985 rc = 1;
29986 goto meta_command_exit;
29987 }
29988 }
29989 if( zMode==0 ){
29990 if( p->mode==MODE_Column
29991 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
29992 ){
29993 sqlite3_fprintf(p->out,
29994 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
 
29995 modeDescr[p->mode], p->cmOpts.iWrap,
29996 p->cmOpts.bWordWrap ? "on" : "off",
29997 p->cmOpts.bQuote ? "" : "no");
 
 
29998 }else{
29999 sqlite3_fprintf(p->out,
30000 "current output mode: %s\n", modeDescr[p->mode]);
 
 
 
30001 }
 
 
30002 zMode = modeDescr[p->mode];
 
30003 }
30004 n2 = strlen30(zMode);
30005 if( cli_strncmp(zMode,"lines",n2)==0 ){
30006 p->mode = MODE_Line;
30007 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -30030,10 +30178,15 @@
30030 p->mode = MODE_List;
30031 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
30032 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
30033 p->mode = MODE_Insert;
30034 set_table_name(p, zTabname ? zTabname : "table");
 
 
 
 
 
30035 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
30036 p->mode = MODE_Quote;
30037 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
30038 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
30039 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
@@ -32750,10 +32903,11 @@
32750 " -csv set output mode to 'csv'\n"
32751 #if !defined(SQLITE_OMIT_DESERIALIZE)
32752 " -deserialize open the database using sqlite3_deserialize()\n"
32753 #endif
32754 " -echo print inputs before execution\n"
 
32755 " -init FILENAME read/process named file\n"
32756 " -[no]header turn headers on or off\n"
32757 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
32758 " -heap SIZE Size of heap for memsys3 or memsys5\n"
32759 #endif
@@ -32797,11 +32951,11 @@
32797 #ifdef SQLITE_HAVE_ZLIB
32798 " -zip open the file as a ZIP Archive\n"
32799 #endif
32800 ;
32801 static void usage(int showDetail){
32802 sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
32803 "FILENAME is the name of an SQLite database. A new database is created\n"
32804 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
32805 if( showDetail ){
32806 sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
32807 }else{
@@ -33183,10 +33337,13 @@
33183 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
33184 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
33185 ShellSetFlag(&data,SHFLG_TestingMode);
33186 }else if( cli_strcmp(z,"-safe")==0 ){
33187 /* no-op - catch this on the second pass */
 
 
 
33188 }
33189 }
33190 #ifndef SQLITE_SHELL_FIDDLE
33191 if( !bEnableVfstrace ) verify_uninitialized();
33192 #endif
@@ -33282,10 +33439,29 @@
33282 }else if( cli_strcmp(z,"-box")==0 ){
33283 data.mode = MODE_Box;
33284 }else if( cli_strcmp(z,"-csv")==0 ){
33285 data.mode = MODE_Csv;
33286 memcpy(data.colSeparator,",",2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33287 #ifdef SQLITE_HAVE_ZLIB
33288 }else if( cli_strcmp(z,"-zip")==0 ){
33289 data.openMode = SHELL_OPEN_ZIPFILE;
33290 #endif
33291 }else if( cli_strcmp(z,"-append")==0 ){
33292
--- extsrc/shell.c
+++ extsrc/shell.c
@@ -849,11 +849,11 @@
849
850 /*
851 ** Prompt strings. Initialized in main. Settable with
852 ** .prompt main continue
853 */
854 #define PROMPT_LEN_MAX 128
855 /* First line prompt. default: "sqlite> " */
856 static char mainPrompt[PROMPT_LEN_MAX];
857 /* Continuation prompt. default: " ...> " */
858 static char continuePrompt[PROMPT_LEN_MAX];
859
@@ -21610,10 +21610,11 @@
21610 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
21611 u8 bSafeMode; /* True to prohibit unsafe operations */
21612 u8 bSafeModePersist; /* The long-term value of bSafeMode */
21613 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
21614 u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */
21615 u8 eEscMode; /* Escape mode for text output */
21616 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
21617 unsigned statsOn; /* True to display memory stats before each finalize */
21618 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
21619 int inputNesting; /* Track nesting level of .read and other redirects */
21620 int outCount; /* Revert to stdout when reaching zero */
@@ -21710,10 +21711,18 @@
21711 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
21712 ** callback limit is reached, and for each
21713 ** top-level SQL statement */
21714 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
21715
21716 /* Allowed values for ShellState.eEscMode
21717 */
21718 #define SHELL_ESC_SYMBOL 0 /* Substitute U+2400 graphics */
21719 #define SHELL_ESC_ASCII 1 /* Substitute ^Y for X where Y=X+0x40 */
21720 #define SHELL_ESC_OFF 2 /* Send characters verbatim */
21721
21722 static const char *shell_EscModeNames[] = { "symbol", "ascii", "off" };
21723
21724 /*
21725 ** These are the allowed shellFlgs values
21726 */
21727 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
21728 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
@@ -22047,63 +22056,79 @@
22056 sqlite3_fprintf(out, "X'%s'", zStr);
22057 sqlite3_free(zStr);
22058 }
22059
22060 /*
22061 ** Output the given string as a quoted string using SQL quoting conventions:
22062 **
22063 ** (1) Single quotes (') within the string are doubled
22064 ** (2) The whle string is enclosed in '...'
22065 ** (3) Control characters other than \n, \t, and \r\n are escaped
22066 ** using \u00XX notation and if such substitutions occur,
22067 ** the whole string is enclosed in unistr('...') instead of '...'.
22068 **
22069 ** Step (3) is omitted if the control-character escape mode is OFF.
22070 **
22071 ** See also: output_quoted_escaped_string() which does the same except
22072 ** that it does not make exceptions for \n, \t, and \r\n in step (3).
22073 */
22074 static void output_quoted_string(ShellState *p, const char *zInX){
22075 int i;
22076 int needUnistr = 0;
22077 int needDblQuote = 0;
22078 const unsigned char *z = (const unsigned char*)zInX;
22079 unsigned char c;
 
 
 
 
 
 
 
 
 
22080 FILE *out = p->out;
22081 sqlite3_fsetmode(out, _O_BINARY);
22082 if( z==0 ) return;
22083 for(i=0; (c = z[i])!=0; i++){
22084 if( c=='\'' ){ needDblQuote = 1; }
22085 if( c>0x1f ) continue;
22086 if( c=='\t' || c=='\n' ) continue;
22087 if( c=='\r' && z[i+1]=='\n' ) continue;
22088 needUnistr = 1;
22089 break;
22090 }
22091 if( (needDblQuote==0 && needUnistr==0)
22092 || (needDblQuote==0 && p->eEscMode==SHELL_ESC_OFF)
22093 ){
22094 sqlite3_fprintf(out, "'%s'",z);
22095 }else if( p->eEscMode==SHELL_ESC_OFF ){
22096 char *zEncoded = sqlite3_mprintf("%Q", z);
22097 sqlite3_fputs(zEncoded, out);
22098 sqlite3_free(zEncoded);
22099 }else{
22100 if( needUnistr ){
22101 sqlite3_fputs("unistr('", out);
22102 }else{
22103 sqlite3_fputs("'", out);
22104 }
22105 while( *z ){
22106 for(i=0; (c = z[i])!=0; i++){
22107 if( c=='\'' ) break;
22108 if( c>0x1f ) continue;
22109 if( c=='\t' || c=='\n' ) continue;
22110 if( c=='\r' && z[i+1]=='\n' ) continue;
22111 break;
22112 }
22113 if( i ){
22114 sqlite3_fprintf(out, "%.*s", i, z);
22115 z += i;
22116 }
22117 if( c==0 ) break;
22118 if( c=='\'' ){
22119 sqlite3_fputs("''", out);
22120 }else{
22121 sqlite3_fprintf(out, "\\u%04x", c);
 
 
22122 }
22123 z++;
22124 }
22125 if( needUnistr ){
22126 sqlite3_fputs("')", out);
22127 }else{
22128 sqlite3_fputs("'", out);
22129 }
22130 }
22131 setCrlfMode(p);
22132 }
22133
22134 /*
@@ -22114,65 +22139,19 @@
22139 **
22140 ** This is like output_quoted_string() but with the addition of the \r\n
22141 ** escape mechanism.
22142 */
22143 static void output_quoted_escaped_string(ShellState *p, const char *z){
22144 char *zEscaped;
22145 sqlite3_fsetmode(p->out, _O_BINARY);
22146 if( p->eEscMode==SHELL_ESC_OFF ){
22147 zEscaped = sqlite3_mprintf("%Q", z);
 
 
 
22148 }else{
22149 zEscaped = sqlite3_mprintf("%#Q", z);
22150 }
22151 sqlite3_fputs(zEscaped, p->out);
22152 sqlite3_free(zEscaped);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22153 setCrlfMode(p);
22154 }
22155
22156 /*
22157 ** Find earliest of chars within s specified in zAny.
@@ -22317,10 +22296,97 @@
22296 sqlite3_fputs(ace+1, out);
22297 }
22298 }
22299 sqlite3_fputs(zq, out);
22300 }
22301
22302 /*
22303 ** Escape the input string if it is needed and in accordance with
22304 ** eEscMode.
22305 **
22306 ** Escaping is needed if the string contains any control characters
22307 ** other than \t, \n, and \r\n
22308 **
22309 ** If no escaping is needed (the common case) then set *ppFree to NULL
22310 ** and return the original string. If escapingn is needed, write the
22311 ** escaped string into memory obtained from sqlite3_malloc64() or the
22312 ** equivalent, and return the new string and set *ppFree to the new string
22313 ** as well.
22314 **
22315 ** The caller is responsible for freeing *ppFree if it is non-NULL in order
22316 ** to reclaim memory.
22317 */
22318 static const char *escapeOutput(
22319 ShellState *p,
22320 const char *zInX,
22321 char **ppFree
22322 ){
22323 i64 i, j;
22324 i64 nCtrl = 0;
22325 unsigned char *zIn;
22326 unsigned char c;
22327 unsigned char *zOut;
22328
22329
22330 /* No escaping if disabled */
22331 if( p->eEscMode==SHELL_ESC_OFF ){
22332 *ppFree = 0;
22333 return zInX;
22334 }
22335
22336 /* Count the number of control characters in the string. */
22337 zIn = (unsigned char*)zInX;
22338 for(i=0; (c = zIn[i])!=0; i++){
22339 if( c<=0x1f
22340 && c!='\t'
22341 && c!='\n'
22342 && (c!='\r' || zIn[i+1]!='\n')
22343 ){
22344 nCtrl++;
22345 }
22346 }
22347 if( nCtrl==0 ){
22348 *ppFree = 0;
22349 return zInX;
22350 }
22351 if( p->eEscMode==SHELL_ESC_SYMBOL ) nCtrl *= 2;
22352 zOut = sqlite3_malloc64( i + nCtrl + 1 );
22353 shell_check_oom(zOut);
22354 for(i=j=0; (c = zIn[i])!=0; i++){
22355 if( c>0x1f
22356 || c=='\t'
22357 || c=='\n'
22358 || (c=='\r' && zIn[i+1]=='\n')
22359 ){
22360 continue;
22361 }
22362 if( i>0 ){
22363 memcpy(&zOut[j], zIn, i);
22364 j += i;
22365 }
22366 zIn += i+1;
22367 i = -1;
22368 switch( p->eEscMode ){
22369 case SHELL_ESC_SYMBOL:
22370 zOut[j++] = 0xe2;
22371 zOut[j++] = 0x90;
22372 zOut[j++] = 0x80+c;
22373 break;
22374 case SHELL_ESC_ASCII:
22375 zOut[j++] = '^';
22376 zOut[j++] = 0x40+c;
22377 break;
22378 }
22379 }
22380 if( i>0 ){
22381 memcpy(&zOut[j], zIn, i);
22382 j += i;
22383 }
22384 zOut[j] = 0;
22385 *ppFree = (char*)zOut;
22386 return (char*)zOut;
22387 }
22388
22389 /*
22390 ** Output the given string with characters that are special to
22391 ** HTML escaped.
22392 */
@@ -22761,12 +22827,16 @@
22827 int len = strlen30(azCol[i] ? azCol[i] : "");
22828 if( len>w ) w = len;
22829 }
22830 if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
22831 for(i=0; i<nArg; i++){
22832 char *pFree = 0;
22833 const char *pDisplay;
22834 pDisplay = escapeOutput(p, azArg[i] ? azArg[i] : p->nullValue, &pFree);
22835 sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
22836 pDisplay, p->rowSeparator);
22837 if( pFree ) sqlite3_free(pFree);
22838 }
22839 break;
22840 }
22841 case MODE_ScanExp:
22842 case MODE_Explain: {
@@ -22894,19 +22964,27 @@
22964 break;
22965 }
22966 case MODE_List: {
22967 if( p->cnt++==0 && p->showHeader ){
22968 for(i=0; i<nArg; i++){
22969 char *z = azCol[i];
22970 char *pFree;
22971 const char *zOut = escapeOutput(p, z, &pFree);
22972 sqlite3_fprintf(p->out, "%s%s", zOut,
22973 i==nArg-1 ? p->rowSeparator : p->colSeparator);
22974 if( pFree ) sqlite3_free(pFree);
22975 }
22976 }
22977 if( azArg==0 ) break;
22978 for(i=0; i<nArg; i++){
22979 char *z = azArg[i];
22980 char *pFree;
22981 const char *zOut;
22982 if( z==0 ) z = p->nullValue;
22983 zOut = escapeOutput(p, z, &pFree);
22984 sqlite3_fputs(zOut, p->out);
22985 if( pFree ) sqlite3_free(pFree);
22986 sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
22987 }
22988 break;
22989 }
22990 case MODE_Www:
@@ -24021,10 +24099,11 @@
24099 ** from malloc()) of that first line, which caller should free sometime.
24100 ** Write anything to display on the next line into *pzTail. If this is
24101 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
24102 */
24103 static char *translateForDisplayAndDup(
24104 ShellState *p, /* To access current settings */
24105 const unsigned char *z, /* Input text to be transformed */
24106 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
24107 int mxWidth, /* Max width. 0 means no limit */
24108 u8 bWordWrap /* If true, avoid breaking mid-word */
24109 ){
@@ -24055,19 +24134,22 @@
24134 n++;
24135 i++;
24136 j++;
24137 continue;
24138 }
24139 if( c==0 || c=='\n' || (c=='\r' && z[i+1]=='\n') ) break;
24140 if( c=='\t' ){
24141 do{
24142 n++;
24143 j++;
24144 }while( (n&7)!=0 && n<mxWidth );
24145 i++;
24146 continue;
24147 }
24148 n++;
24149 j += 3;
24150 i++;
24151 }
24152 if( n>=mxWidth && bWordWrap ){
24153 /* Perhaps try to back up to a better place to break the line */
24154 for(k=i; k>i/2; k--){
24155 if( isspace(z[k-1]) ) break;
@@ -24110,23 +24192,48 @@
24192 if( c>=' ' ){
24193 n++;
24194 zOut[j++] = z[i++];
24195 continue;
24196 }
24197 if( c==0 ) break;
24198 if( z[i]=='\t' ){
24199 do{
24200 n++;
24201 zOut[j++] = ' ';
24202 }while( (n&7)!=0 && n<mxWidth );
24203 i++;
24204 continue;
24205 }
24206 switch( p->eEscMode ){
24207 case SHELL_ESC_SYMBOL:
24208 zOut[j++] = 0xe2;
24209 zOut[j++] = 0x90;
24210 zOut[j++] = 0x80 + c;
24211 break;
24212 case SHELL_ESC_ASCII:
24213 zOut[j++] = '^';
24214 zOut[j++] = 0x40 + c;
24215 break;
24216 case SHELL_ESC_OFF:
24217 zOut[j++] = c;
24218 break;
24219 }
24220 i++;
24221 }
24222 zOut[j] = 0;
24223 return (char*)zOut;
24224 }
24225
24226 /* Return true if the text string z[] contains characters that need
24227 ** unistr() escaping.
24228 */
24229 static int needUnistr(const unsigned char *z){
24230 unsigned char c;
24231 if( z==0 ) return 0;
24232 while( (c = *z)>0x1f || c=='\t' || c=='\n' || (c=='\r' && z[1]=='\n') ){ z++; }
24233 return c!=0;
24234 }
24235
24236 /* Extract the value of the i-th current column for pStmt as an SQL literal
24237 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
24238 ** the caller.
24239 */
@@ -24138,11 +24245,12 @@
24245 case SQLITE_INTEGER:
24246 case SQLITE_FLOAT: {
24247 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
24248 }
24249 case SQLITE_TEXT: {
24250 const unsigned char *zText = sqlite3_column_text(pStmt,i);
24251 return sqlite3_mprintf(needUnistr(zText)?"%#Q":"%Q",zText);
24252 }
24253 case SQLITE_BLOB: {
24254 int j;
24255 sqlite3_str *pStr = sqlite3_str_new(0);
24256 const unsigned char *a = sqlite3_column_blob(pStmt,i);
@@ -24230,11 +24338,11 @@
24338 wx = p->cmOpts.iWrap;
24339 }
24340 if( wx<0 ) wx = -wx;
24341 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
24342 if( uz==0 ) uz = (u8*)"";
24343 azData[i] = translateForDisplayAndDup(p, uz, &zNotUsed, wx, bw);
24344 }
24345 do{
24346 int useNextLine = bNextLine;
24347 bNextLine = 0;
24348 if( (nRow+2)*nColumn >= nAlloc ){
@@ -24254,19 +24362,20 @@
24362 if( wx<0 ) wx = -wx;
24363 if( useNextLine ){
24364 uz = azNextLine[i];
24365 if( uz==0 ) uz = (u8*)zEmpty;
24366 }else if( p->cmOpts.bQuote ){
24367 assert( azQuoted!=0 );
24368 sqlite3_free(azQuoted[i]);
24369 azQuoted[i] = quoted_column(pStmt,i);
24370 uz = (const unsigned char*)azQuoted[i];
24371 }else{
24372 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
24373 if( uz==0 ) uz = (u8*)zShowNull;
24374 }
24375 azData[nRow*nColumn + i]
24376 = translateForDisplayAndDup(p, uz, &azNextLine[i], wx, bw);
24377 if( azNextLine[i] ){
24378 bNextLine = 1;
24379 abRowDiv[nRow-1] = 0;
24380 bMultiLineRowExists = 1;
24381 }
@@ -25185,11 +25294,11 @@
25294 #if !defined(SQLITE_SHELL_FIDDLE)
25295 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
25296 #else
25297 ".log on|off Turn logging on or off.",
25298 #endif
25299 ".mode ?MODE? ?OPTIONS? Set output mode",
25300 " MODE is one of:",
25301 " ascii Columns/rows delimited by 0x1F and 0x1E",
25302 " box Tables using unicode box-drawing characters",
25303 " csv Comma-separated values",
25304 " column Output in columns. (See .width)",
@@ -25203,10 +25312,11 @@
25312 " quote Escape answers as for SQL",
25313 " table ASCII-art table",
25314 " tabs Tab-separated values",
25315 " tcl TCL list elements",
25316 " OPTIONS: (for columnar modes or insert mode):",
25317 " --escape T ctrl-char escape; T is one of: symbol, ascii, off",
25318 " --wrap N Wrap output lines to no longer than N characters",
25319 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
25320 " --ww Shorthand for \"--wordwrap 1\"",
25321 " --quote Quote output text as SQL literals",
25322 " --noquote Do not quote output text",
@@ -29943,28 +30053,56 @@
30053
30054 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
30055 const char *zMode = 0;
30056 const char *zTabname = 0;
30057 int i, n2;
30058 int chng = 0; /* 0x01: change to cmopts. 0x02: Any other change */
30059 ColModeOpts cmOpts = ColModeOpts_default;
30060 for(i=1; i<nArg; i++){
30061 const char *z = azArg[i];
30062 if( optionMatch(z,"wrap") && i+1<nArg ){
30063 cmOpts.iWrap = integerValue(azArg[++i]);
30064 chng |= 1;
30065 }else if( optionMatch(z,"ww") ){
30066 cmOpts.bWordWrap = 1;
30067 chng |= 1;
30068 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
30069 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
30070 chng |= 1;
30071 }else if( optionMatch(z,"quote") ){
30072 cmOpts.bQuote = 1;
30073 chng |= 1;
30074 }else if( optionMatch(z,"noquote") ){
30075 cmOpts.bQuote = 0;
30076 chng |= 1;
30077 }else if( optionMatch(z,"escape") && i+1<nArg ){
30078 /* See similar code at tag-20250224-1 */
30079 const char *zEsc = azArg[++i];
30080 int k;
30081 for(k=0; k<ArraySize(shell_EscModeNames); k++){
30082 if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
30083 p->eEscMode = k;
30084 chng |= 2;
30085 break;
30086 }
30087 }
30088 if( k>=ArraySize(shell_EscModeNames) ){
30089 sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
30090 " - choices:", zEsc);
30091 for(k=0; k<ArraySize(shell_EscModeNames); k++){
30092 sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
30093 }
30094 sqlite3_fprintf(stderr, "\n");
30095 rc = 1;
30096 goto meta_command_exit;
30097 }
30098 }else if( zMode==0 ){
30099 zMode = z;
30100 /* Apply defaults for qbox pseudo-mode. If that
30101 * overwrites already-set values, user was informed of this.
30102 */
30103 chng |= 1;
30104 if( cli_strcmp(z, "qbox")==0 ){
30105 ColModeOpts cmo = ColModeOpts_default_qbox;
30106 zMode = "box";
30107 cmOpts = cmo;
30108 }
@@ -29971,10 +30109,11 @@
30109 }else if( zTabname==0 ){
30110 zTabname = z;
30111 }else if( z[0]=='-' ){
30112 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
30113 eputz("options:\n"
30114 " --escape MODE\n"
30115 " --noquote\n"
30116 " --quote\n"
30117 " --wordwrap on/off\n"
30118 " --wrap N\n"
30119 " --ww\n");
@@ -29984,24 +30123,33 @@
30123 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
30124 rc = 1;
30125 goto meta_command_exit;
30126 }
30127 }
30128 if( !chng ){
30129 if( p->mode==MODE_Column
30130 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
30131 ){
30132 sqlite3_fprintf(p->out,
30133 "current output mode: %s --wrap %d --wordwrap %s "
30134 "--%squote --escape %s\n",
30135 modeDescr[p->mode], p->cmOpts.iWrap,
30136 p->cmOpts.bWordWrap ? "on" : "off",
30137 p->cmOpts.bQuote ? "" : "no",
30138 shell_EscModeNames[p->eEscMode]
30139 );
30140 }else{
30141 sqlite3_fprintf(p->out,
30142 "current output mode: %s --escape %s\n",
30143 modeDescr[p->mode],
30144 shell_EscModeNames[p->eEscMode]
30145 );
30146 }
30147 }
30148 if( zMode==0 ){
30149 zMode = modeDescr[p->mode];
30150 if( (chng&1)==0 ) cmOpts = p->cmOpts;
30151 }
30152 n2 = strlen30(zMode);
30153 if( cli_strncmp(zMode,"lines",n2)==0 ){
30154 p->mode = MODE_Line;
30155 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
@@ -30030,10 +30178,15 @@
30178 p->mode = MODE_List;
30179 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
30180 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
30181 p->mode = MODE_Insert;
30182 set_table_name(p, zTabname ? zTabname : "table");
30183 if( p->eEscMode==SHELL_ESC_OFF ){
30184 ShellSetFlag(p, SHFLG_Newlines);
30185 }else{
30186 ShellClearFlag(p, SHFLG_Newlines);
30187 }
30188 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
30189 p->mode = MODE_Quote;
30190 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
30191 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
30192 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
@@ -32750,10 +32903,11 @@
32903 " -csv set output mode to 'csv'\n"
32904 #if !defined(SQLITE_OMIT_DESERIALIZE)
32905 " -deserialize open the database using sqlite3_deserialize()\n"
32906 #endif
32907 " -echo print inputs before execution\n"
32908 " -escape T ctrl-char escape; T is one of: symbol, ascii, off\n"
32909 " -init FILENAME read/process named file\n"
32910 " -[no]header turn headers on or off\n"
32911 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
32912 " -heap SIZE Size of heap for memsys3 or memsys5\n"
32913 #endif
@@ -32797,11 +32951,11 @@
32951 #ifdef SQLITE_HAVE_ZLIB
32952 " -zip open the file as a ZIP Archive\n"
32953 #endif
32954 ;
32955 static void usage(int showDetail){
32956 sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL...]]\n"
32957 "FILENAME is the name of an SQLite database. A new database is created\n"
32958 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
32959 if( showDetail ){
32960 sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
32961 }else{
@@ -33183,10 +33337,13 @@
33337 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
33338 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
33339 ShellSetFlag(&data,SHFLG_TestingMode);
33340 }else if( cli_strcmp(z,"-safe")==0 ){
33341 /* no-op - catch this on the second pass */
33342 }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
33343 /* skip over the argument */
33344 i++;
33345 }
33346 }
33347 #ifndef SQLITE_SHELL_FIDDLE
33348 if( !bEnableVfstrace ) verify_uninitialized();
33349 #endif
@@ -33282,10 +33439,29 @@
33439 }else if( cli_strcmp(z,"-box")==0 ){
33440 data.mode = MODE_Box;
33441 }else if( cli_strcmp(z,"-csv")==0 ){
33442 data.mode = MODE_Csv;
33443 memcpy(data.colSeparator,",",2);
33444 }else if( cli_strcmp(z,"-escape")==0 && i+1<argc ){
33445 /* See similar code at tag-20250224-1 */
33446 const char *zEsc = argv[++i];
33447 int k;
33448 for(k=0; k<ArraySize(shell_EscModeNames); k++){
33449 if( sqlite3_stricmp(zEsc,shell_EscModeNames[k])==0 ){
33450 data.eEscMode = k;
33451 break;
33452 }
33453 }
33454 if( k>=ArraySize(shell_EscModeNames) ){
33455 sqlite3_fprintf(stderr, "unknown control character escape mode \"%s\""
33456 " - choices:", zEsc);
33457 for(k=0; k<ArraySize(shell_EscModeNames); k++){
33458 sqlite3_fprintf(stderr, " %s", shell_EscModeNames[k]);
33459 }
33460 sqlite3_fprintf(stderr, "\n");
33461 exit(1);
33462 }
33463 #ifdef SQLITE_HAVE_ZLIB
33464 }else if( cli_strcmp(z,"-zip")==0 ){
33465 data.openMode = SHELL_OPEN_ZIPFILE;
33466 #endif
33467 }else if( cli_strcmp(z,"-append")==0 ){
33468
+1133 -517
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
1616
** if you want a wrapper to interface SQLite with your choice of programming
1717
** language. The code for the "sqlite3" command-line shell is also in a
1818
** separate file. This file contains only code for the core SQLite library.
1919
**
2020
** The content in this amalgamation comes from Fossil check-in
21
-** 57caa3136d1bfca06e4f2285734a4977b8d3 with changes in files:
21
+** e6784af6d50f715338ae3218fc8ba1b89488 with changes in files:
2222
**
2323
**
2424
*/
2525
#ifndef SQLITE_AMALGAMATION
2626
#define SQLITE_CORE 1
@@ -465,11 +465,11 @@
465465
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
466466
** [sqlite_version()] and [sqlite_source_id()].
467467
*/
468468
#define SQLITE_VERSION "3.50.0"
469469
#define SQLITE_VERSION_NUMBER 3050000
470
-#define SQLITE_SOURCE_ID "2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc"
470
+#define SQLITE_SOURCE_ID "2025-02-25 18:10:47 e6784af6d50f715338ae3218fc8ba1b894883c27d797f0b7fd2625cac17d9cd7"
471471
472472
/*
473473
** CAPI3REF: Run-Time Library Version Numbers
474474
** KEYWORDS: sqlite3_version sqlite3_sourceid
475475
**
@@ -1479,10 +1479,16 @@
14791479
** to block for up to M milliseconds before failing when attempting to
14801480
** obtain a file lock using the xLock or xShmLock methods of the VFS.
14811481
** The parameter is a pointer to a 32-bit signed integer that contains
14821482
** the value that M is to be set to. Before returning, the 32-bit signed
14831483
** integer is overwritten with the previous value of M.
1484
+**
1485
+** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1486
+** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1487
+** VFS to block when taking a SHARED lock to connect to a wal mode database.
1488
+** This is used to implement the functionality associated with
1489
+** SQLITE_SETLK_BLOCK_ON_CONNECT.
14841490
**
14851491
** <li>[[SQLITE_FCNTL_DATA_VERSION]]
14861492
** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
14871493
** a database file. The argument is a pointer to a 32-bit unsigned integer.
14881494
** The "data version" for the pager is written into the pointer. The
@@ -1576,10 +1582,11 @@
15761582
#define SQLITE_FCNTL_CKPT_START 39
15771583
#define SQLITE_FCNTL_EXTERNAL_READER 40
15781584
#define SQLITE_FCNTL_CKSM_FILE 41
15791585
#define SQLITE_FCNTL_RESET_CACHE 42
15801586
#define SQLITE_FCNTL_NULL_IO 43
1587
+#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
15811588
15821589
/* deprecated names */
15831590
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
15841591
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
15851592
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3332,10 +3339,48 @@
33323339
**
33333340
** See also: [PRAGMA busy_timeout]
33343341
*/
33353342
SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
33363343
3344
+/*
3345
+** CAPI3REF: Set the Setlk Timeout
3346
+** METHOD: sqlite3
3347
+**
3348
+** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3349
+** the VFS supports blocking locks, it sets the timeout in ms used by
3350
+** eligible locks taken on wal mode databases by the specified database
3351
+** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3352
+** not support blocking locks, this function is a no-op.
3353
+**
3354
+** Passing 0 to this function disables blocking locks altogether. Passing
3355
+** -1 to this function requests that the VFS blocks for a long time -
3356
+** indefinitely if possible. The results of passing any other negative value
3357
+** are undefined.
3358
+**
3359
+** Internally, each SQLite database handle store two timeout values - the
3360
+** busy-timeout (used for rollback mode databases, or if the VFS does not
3361
+** support blocking locks) and the setlk-timeout (used for blocking locks
3362
+** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3363
+** values, this function sets only the setlk-timeout value. Therefore,
3364
+** to configure separate busy-timeout and setlk-timeout values for a single
3365
+** database handle, call sqlite3_busy_timeout() followed by this function.
3366
+**
3367
+** Whenever the number of connections to a wal mode database falls from
3368
+** 1 to 0, the last connection takes an exclusive lock on the database,
3369
+** then checkpoints and deletes the wal file. While it is doing this, any
3370
+** new connection that tries to read from the database fails with an
3371
+** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3372
+** passed to this API, the new connection blocks until the exclusive lock
3373
+** has been released.
3374
+*/
3375
+SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3376
+
3377
+/*
3378
+** CAPI3REF: Flags for sqlite3_setlk_timeout()
3379
+*/
3380
+#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3381
+
33373382
/*
33383383
** CAPI3REF: Convenience Routines For Running Queries
33393384
** METHOD: sqlite3
33403385
**
33413386
** This is a legacy interface that is preserved for backwards compatibility.
@@ -14097,18 +14142,26 @@
1409714142
** * Terms in the SET clause of an UPDATE statement
1409814143
** * Terms in the result set of a SELECT statement
1409914144
** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
1410014145
** * Terms in the VALUES clause of an INSERT statement
1410114146
**
14102
-** The hard upper limit here is 32676. Most database people will
14147
+** The hard upper limit here is 32767. Most database people will
1410314148
** tell you that in a well-normalized database, you usually should
1410414149
** not have more than a dozen or so columns in any table. And if
1410514150
** that is the case, there is no point in having more than a few
1410614151
** dozen values in any of the other situations described above.
14152
+**
14153
+** An index can only have SQLITE_MAX_COLUMN columns from the user
14154
+** point of view, but the underlying b-tree that implements the index
14155
+** might have up to twice as many columns in a WITHOUT ROWID table,
14156
+** since must also store the primary key at the end. Hence the
14157
+** column count for Index is u16 instead of i16.
1410714158
*/
14108
-#ifndef SQLITE_MAX_COLUMN
14159
+#if !defined(SQLITE_MAX_COLUMN)
1410914160
# define SQLITE_MAX_COLUMN 2000
14161
+#elif SQLITE_MAX_COLUMN>32767
14162
+# error SQLITE_MAX_COLUMN may not exceed 32767
1411014163
#endif
1411114164
1411214165
/*
1411314166
** The maximum length of a single SQL statement in bytes.
1411414167
**
@@ -18076,10 +18129,14 @@
1807618129
BusyHandler busyHandler; /* Busy callback */
1807718130
Db aDbStatic[2]; /* Static space for the 2 default backends */
1807818131
Savepoint *pSavepoint; /* List of active savepoints */
1807918132
int nAnalysisLimit; /* Number of index rows to ANALYZE */
1808018133
int busyTimeout; /* Busy handler timeout, in msec */
18134
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
18135
+ int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
18136
+ int setlkFlags; /* Flags passed to setlk_timeout() */
18137
+#endif
1808118138
int nSavepoint; /* Number of non-transaction savepoints */
1808218139
int nStatement; /* Number of nested statement-transactions */
1808318140
i64 nDeferredCons; /* Net deferred constraints this transaction. */
1808418141
i64 nDeferredImmCons; /* Net deferred immediate constraints */
1808518142
int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
@@ -19074,11 +19131,11 @@
1907419131
Expr *pPartIdxWhere; /* WHERE clause for partial indices */
1907519132
ExprList *aColExpr; /* Column expressions */
1907619133
Pgno tnum; /* DB Page containing root of this index */
1907719134
LogEst szIdxRow; /* Estimated average row size in bytes */
1907819135
u16 nKeyCol; /* Number of columns forming the key */
19079
- u16 nColumn; /* Number of columns stored in the index */
19136
+ u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */
1908019137
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
1908119138
unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
1908219139
unsigned bUnordered:1; /* Use this index for == or IN queries only */
1908319140
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
1908419141
unsigned isResized:1; /* True if resizeIndexObject() has been called */
@@ -19412,14 +19469,14 @@
1941219469
#define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
1941319470
1941419471
/* Macros can be used to test, set, or clear bits in the
1941519472
** Expr.flags field.
1941619473
*/
19417
-#define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
19418
-#define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
19419
-#define ExprSetProperty(E,P) (E)->flags|=(P)
19420
-#define ExprClearProperty(E,P) (E)->flags&=~(P)
19474
+#define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0)
19475
+#define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P))
19476
+#define ExprSetProperty(E,P) (E)->flags|=(u32)(P)
19477
+#define ExprClearProperty(E,P) (E)->flags&=~(u32)(P)
1942119478
#define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
1942219479
#define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
1942319480
#define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
1942419481
1942519482
/* Macros used to ensure that the correct members of unions are accessed
@@ -21223,11 +21280,11 @@
2122321280
SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
2122421281
SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
2122521282
SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
2122621283
SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
2122721284
SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
21228
-SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
21285
+SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index*, int);
2122921286
#ifdef SQLITE_OMIT_GENERATED_COLUMNS
2123021287
# define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
2123121288
# define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
2123221289
#else
2123321290
SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
@@ -21321,11 +21378,11 @@
2132121378
SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
2132221379
SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
2132321380
SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
2132421381
SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
2132521382
SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
21326
-SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
21383
+SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**);
2132721384
SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
2132821385
Expr*, int, int, u8);
2132921386
SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
2133021387
SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
2133121388
SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
@@ -21457,11 +21514,12 @@
2145721514
SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
2145821515
SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
2145921516
SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
2146021517
SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
2146121518
SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21462
-SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
21519
+SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int);
21520
+SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char*, u32);
2146321521
SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
2146421522
SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
2146521523
SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
2146621524
SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
2146721525
#if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
@@ -22557,10 +22615,13 @@
2255722615
#ifdef SQLITE_ENABLE_RTREE
2255822616
"ENABLE_RTREE",
2255922617
#endif
2256022618
#ifdef SQLITE_ENABLE_SESSION
2256122619
"ENABLE_SESSION",
22620
+#endif
22621
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
22622
+ "ENABLE_SETLK_TIMEOUT",
2256222623
#endif
2256322624
#ifdef SQLITE_ENABLE_SNAPSHOT
2256422625
"ENABLE_SNAPSHOT",
2256522626
#endif
2256622627
#ifdef SQLITE_ENABLE_SORTER_REFERENCES
@@ -24372,12 +24433,13 @@
2437224433
u32 nFree = countLookasideSlots(db->lookaside.pFree);
2437324434
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
2437424435
nInit += countLookasideSlots(db->lookaside.pSmallInit);
2437524436
nFree += countLookasideSlots(db->lookaside.pSmallFree);
2437624437
#endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24377
- if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
24378
- return db->lookaside.nSlot - (nInit+nFree);
24438
+ assert( db->lookaside.nSlot >= nInit+nFree );
24439
+ if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit);
24440
+ return (int)(db->lookaside.nSlot - (nInit+nFree));
2437924441
}
2438024442
2438124443
/*
2438224444
** Query status information for a single database connection
2438324445
*/
@@ -24426,11 +24488,11 @@
2442624488
testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
2442724489
testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
2442824490
assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
2442924491
assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
2443024492
*pCurrent = 0;
24431
- *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
24493
+ *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
2443224494
if( resetFlag ){
2443324495
db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
2443424496
}
2443524497
break;
2443624498
}
@@ -31541,21 +31603,21 @@
3154131603
#define etSTRING 5 /* Strings. %s */
3154231604
#define etDYNSTRING 6 /* Dynamically allocated strings. %z */
3154331605
#define etPERCENT 7 /* Percent symbol. %% */
3154431606
#define etCHARX 8 /* Characters. %c */
3154531607
/* The rest are extensions, not normally found in printf() */
31546
-#define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
31547
-#define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
31548
- NULL pointers replaced by SQL NULL. %Q */
31549
-#define etTOKEN 11 /* a pointer to a Token structure */
31550
-#define etSRCITEM 12 /* a pointer to a SrcItem */
31551
-#define etPOINTER 13 /* The %p conversion */
31552
-#define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
31553
-#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31554
-#define etDECIMAL 16 /* %d or %u, but not %x, %o */
31555
-
31556
-#define etINVALID 17 /* Any unrecognized conversion type */
31608
+#define etESCAPE_q 9 /* Strings with '\'' doubled. %q */
31609
+#define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '',
31610
+ NULL pointers replaced by SQL NULL. %Q */
31611
+#define etTOKEN 11 /* a pointer to a Token structure */
31612
+#define etSRCITEM 12 /* a pointer to a SrcItem */
31613
+#define etPOINTER 13 /* The %p conversion */
31614
+#define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */
31615
+#define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31616
+#define etDECIMAL 16 /* %d or %u, but not %x, %o */
31617
+
31618
+#define etINVALID 17 /* Any unrecognized conversion type */
3155731619
3155831620
3155931621
/*
3156031622
** An "etByte" is an 8-bit unsigned value.
3156131623
*/
@@ -31590,13 +31652,13 @@
3159031652
static const et_info fmtinfo[] = {
3159131653
{ 'd', 10, 1, etDECIMAL, 0, 0 },
3159231654
{ 's', 0, 4, etSTRING, 0, 0 },
3159331655
{ 'g', 0, 1, etGENERIC, 30, 0 },
3159431656
{ 'z', 0, 4, etDYNSTRING, 0, 0 },
31595
- { 'q', 0, 4, etSQLESCAPE, 0, 0 },
31596
- { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
31597
- { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
31657
+ { 'q', 0, 4, etESCAPE_q, 0, 0 },
31658
+ { 'Q', 0, 4, etESCAPE_Q, 0, 0 },
31659
+ { 'w', 0, 4, etESCAPE_w, 0, 0 },
3159831660
{ 'c', 0, 0, etCHARX, 0, 0 },
3159931661
{ 'o', 8, 0, etRADIX, 0, 2 },
3160031662
{ 'u', 10, 0, etDECIMAL, 0, 0 },
3160131663
{ 'x', 16, 0, etRADIX, 16, 1 },
3160231664
{ 'X', 16, 0, etRADIX, 0, 4 },
@@ -32189,29 +32251,11 @@
3218932251
}else{
3219032252
buf[0] = 0;
3219132253
}
3219232254
}else{
3219332255
unsigned int ch = va_arg(ap,unsigned int);
32194
- if( ch<0x00080 ){
32195
- buf[0] = ch & 0xff;
32196
- length = 1;
32197
- }else if( ch<0x00800 ){
32198
- buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
32199
- buf[1] = 0x80 + (u8)(ch & 0x3f);
32200
- length = 2;
32201
- }else if( ch<0x10000 ){
32202
- buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
32203
- buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
32204
- buf[2] = 0x80 + (u8)(ch & 0x3f);
32205
- length = 3;
32206
- }else{
32207
- buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
32208
- buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
32209
- buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
32210
- buf[3] = 0x80 + (u8)(ch & 0x3f);
32211
- length = 4;
32212
- }
32256
+ length = sqlite3AppendOneUtf8Character(buf, ch);
3221332257
}
3221432258
if( precision>1 ){
3221532259
i64 nPrior = 1;
3221632260
width -= precision-1;
3221732261
if( width>1 && !flag_leftjustify ){
@@ -32287,26 +32331,35 @@
3228732331
/* Adjust width to account for extra bytes in UTF-8 characters */
3228832332
int ii = length - 1;
3228932333
while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
3229032334
}
3229132335
break;
32292
- case etSQLESCAPE: /* %q: Escape ' characters */
32293
- case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
32294
- case etSQLESCAPE3: { /* %w: Escape " characters */
32336
+ case etESCAPE_q: /* %q: Escape ' characters */
32337
+ case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */
32338
+ case etESCAPE_w: { /* %w: Escape " characters */
3229532339
i64 i, j, k, n;
32296
- int needQuote, isnull;
32340
+ int needQuote = 0;
3229732341
char ch;
32298
- char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
3229932342
char *escarg;
32343
+ char q;
3230032344
3230132345
if( bArgList ){
3230232346
escarg = getTextArg(pArgList);
3230332347
}else{
3230432348
escarg = va_arg(ap,char*);
3230532349
}
32306
- isnull = escarg==0;
32307
- if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
32350
+ if( escarg==0 ){
32351
+ escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)");
32352
+ }else if( xtype==etESCAPE_Q ){
32353
+ needQuote = 1;
32354
+ }
32355
+ if( xtype==etESCAPE_w ){
32356
+ q = '"';
32357
+ flag_alternateform = 0;
32358
+ }else{
32359
+ q = '\'';
32360
+ }
3230832361
/* For %q, %Q, and %w, the precision is the number of bytes (or
3230932362
** characters if the ! flags is present) to use from the input.
3231032363
** Because of the extra quoting characters inserted, the number
3231132364
** of output characters may be larger than the precision.
3231232365
*/
@@ -32315,26 +32368,77 @@
3231532368
if( ch==q ) n++;
3231632369
if( flag_altform2 && (ch&0xc0)==0xc0 ){
3231732370
while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
3231832371
}
3231932372
}
32320
- needQuote = !isnull && xtype==etSQLESCAPE2;
32373
+ if( flag_alternateform ){
32374
+ /* For %#q, do unistr()-style backslash escapes for
32375
+ ** all control characters, and for backslash itself.
32376
+ ** For %#Q, do the same but only if there is at least
32377
+ ** one control character. */
32378
+ u32 nBack = 0;
32379
+ u32 nCtrl = 0;
32380
+ for(k=0; k<i; k++){
32381
+ if( escarg[k]=='\\' ){
32382
+ nBack++;
32383
+ }else if( escarg[k]<=0x1f ){
32384
+ nCtrl++;
32385
+ }
32386
+ }
32387
+ if( nCtrl || xtype==etESCAPE_q ){
32388
+ n += nBack + 5*nCtrl;
32389
+ if( xtype==etESCAPE_Q ){
32390
+ n += 10;
32391
+ needQuote = 2;
32392
+ }
32393
+ }else{
32394
+ flag_alternateform = 0;
32395
+ }
32396
+ }
3232132397
n += i + 3;
3232232398
if( n>etBUFSIZE ){
3232332399
bufpt = zExtra = printfTempBuf(pAccum, n);
3232432400
if( bufpt==0 ) return;
3232532401
}else{
3232632402
bufpt = buf;
3232732403
}
3232832404
j = 0;
32329
- if( needQuote ) bufpt[j++] = q;
32405
+ if( needQuote ){
32406
+ if( needQuote==2 ){
32407
+ memcpy(&bufpt[j], "unistr('", 8);
32408
+ j += 8;
32409
+ }else{
32410
+ bufpt[j++] = '\'';
32411
+ }
32412
+ }
3233032413
k = i;
32331
- for(i=0; i<k; i++){
32332
- bufpt[j++] = ch = escarg[i];
32333
- if( ch==q ) bufpt[j++] = ch;
32414
+ if( flag_alternateform ){
32415
+ for(i=0; i<k; i++){
32416
+ bufpt[j++] = ch = escarg[i];
32417
+ if( ch==q ){
32418
+ bufpt[j++] = ch;
32419
+ }else if( ch=='\\' ){
32420
+ bufpt[j++] = '\\';
32421
+ }else if( ch<=0x1f ){
32422
+ bufpt[j-1] = '\\';
32423
+ bufpt[j++] = 'u';
32424
+ bufpt[j++] = '0';
32425
+ bufpt[j++] = '0';
32426
+ bufpt[j++] = ch>=0x10 ? '1' : '0';
32427
+ bufpt[j++] = "0123456789abcdef"[ch&0xf];
32428
+ }
32429
+ }
32430
+ }else{
32431
+ for(i=0; i<k; i++){
32432
+ bufpt[j++] = ch = escarg[i];
32433
+ if( ch==q ) bufpt[j++] = ch;
32434
+ }
3233432435
}
32335
- if( needQuote ) bufpt[j++] = q;
32436
+ if( needQuote ){
32437
+ bufpt[j++] = '\'';
32438
+ if( needQuote==2 ) bufpt[j++] = ')';
32439
+ }
3233632440
bufpt[j] = 0;
3233732441
length = j;
3233832442
goto adjust_width_for_utf8;
3233932443
}
3234032444
case etTOKEN: {
@@ -34828,10 +34932,39 @@
3482834932
*zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
3482934933
*zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
3483034934
*zOut++ = (u8)(c&0x00FF); \
3483134935
} \
3483234936
}
34937
+
34938
+/*
34939
+** Write a single UTF8 character whose value is v into the
34940
+** buffer starting at zOut. zOut must be sized to hold at
34941
+** least for bytes. Return the number of bytes needed
34942
+** to encode the new character.
34943
+*/
34944
+SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char *zOut, u32 v){
34945
+ if( v<0x00080 ){
34946
+ zOut[0] = (u8)(v & 0xff);
34947
+ return 1;
34948
+ }
34949
+ if( v<0x00800 ){
34950
+ zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f);
34951
+ zOut[1] = 0x80 + (u8)(v & 0x3f);
34952
+ return 2;
34953
+ }
34954
+ if( v<0x10000 ){
34955
+ zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f);
34956
+ zOut[1] = 0x80 + (u8)((v>>6) & 0x3f);
34957
+ zOut[2] = 0x80 + (u8)(v & 0x3f);
34958
+ return 3;
34959
+ }
34960
+ zOut[0] = 0xf0 + (u8)((v>>18) & 0x07);
34961
+ zOut[1] = 0x80 + (u8)((v>>12) & 0x3f);
34962
+ zOut[2] = 0x80 + (u8)((v>>6) & 0x3f);
34963
+ zOut[3] = 0x80 + (u8)(v & 0x3f);
34964
+ return 4;
34965
+}
3483334966
3483434967
/*
3483534968
** Translate a single UTF-8 character. Return the unicode value.
3483634969
**
3483734970
** During translation, assume that the byte that zTerm points
@@ -38911,10 +39044,11 @@
3891139044
#if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
3891239045
unsigned fsFlags; /* cached details from statfs() */
3891339046
#endif
3891439047
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
3891539048
unsigned iBusyTimeout; /* Wait this many millisec on locks */
39049
+ int bBlockOnConnect; /* True to block for SHARED locks */
3891639050
#endif
3891739051
#if OS_VXWORKS
3891839052
struct vxworksFileId *pId; /* Unique file ID */
3891939053
#endif
3892039054
#ifdef SQLITE_DEBUG
@@ -40304,10 +40438,17 @@
4030440438
pInode->nLock++;
4030540439
}else{
4030640440
rc = 0;
4030740441
}
4030840442
}else{
40443
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
40444
+ if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK
40445
+ && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE
40446
+ ){
40447
+ rc = osFcntl(pFile->h, F_SETLKW, pLock);
40448
+ }else
40449
+#endif
4030940450
rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
4031040451
}
4031140452
return rc;
4031240453
}
4031340454
@@ -42665,21 +42806,27 @@
4266542806
return SQLITE_OK;
4266642807
}
4266742808
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
4266842809
case SQLITE_FCNTL_LOCK_TIMEOUT: {
4266942810
int iOld = pFile->iBusyTimeout;
42811
+ int iNew = *(int*)pArg;
4267042812
#if SQLITE_ENABLE_SETLK_TIMEOUT==1
42671
- pFile->iBusyTimeout = *(int*)pArg;
42813
+ pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew;
4267242814
#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
4267342815
pFile->iBusyTimeout = !!(*(int*)pArg);
4267442816
#else
4267542817
# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
4267642818
#endif
4267742819
*(int*)pArg = iOld;
4267842820
return SQLITE_OK;
4267942821
}
42680
-#endif
42822
+ case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
42823
+ int iNew = *(int*)pArg;
42824
+ pFile->bBlockOnConnect = iNew;
42825
+ return SQLITE_OK;
42826
+ }
42827
+#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
4268142828
#if SQLITE_MAX_MMAP_SIZE>0
4268242829
case SQLITE_FCNTL_MMAP_SIZE: {
4268342830
i64 newLimit = *(i64*)pArg;
4268442831
int rc = SQLITE_OK;
4268542832
if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -43658,11 +43805,11 @@
4365843805
** occur later in the above list than the lock being obtained may be
4365943806
** held.
4366043807
**
4366143808
** It is not permitted to block on the RECOVER lock.
4366243809
*/
43663
-#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43810
+#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
4366443811
{
4366543812
u16 lockMask = (p->exclMask|p->sharedMask);
4366643813
assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
4366743814
(ofst!=2) /* not RECOVER */
4366843815
&& (ofst!=1 || lockMask==0 || lockMask==2)
@@ -47188,11 +47335,21 @@
4718847335
HANDLE hMap; /* Handle for accessing memory mapping */
4718947336
void *pMapRegion; /* Area memory mapped */
4719047337
sqlite3_int64 mmapSize; /* Size of mapped region */
4719147338
sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
4719247339
#endif
47340
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47341
+ DWORD iBusyTimeout; /* Wait this many millisec on locks */
47342
+ int bBlockOnConnect;
47343
+#endif
4719347344
};
47345
+
47346
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47347
+# define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout
47348
+#else
47349
+# define winFileBusyTimeout(pDbFd) 0
47350
+#endif
4719447351
4719547352
/*
4719647353
** The winVfsAppData structure is used for the pAppData member for all of the
4719747354
** Win32 VFS variants.
4719847355
*/
@@ -47623,10 +47780,16 @@
4762347780
#endif
4762447781
4762547782
#define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
4762647783
LPWSTR*))aSyscall[25].pCurrent)
4762747784
47785
+/*
47786
+** For GetLastError(), MSDN says:
47787
+**
47788
+** Minimum supported client: Windows XP [desktop apps | UWP apps]
47789
+** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
47790
+*/
4762847791
{ "GetLastError", (SYSCALL)GetLastError, 0 },
4762947792
4763047793
#define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
4763147794
4763247795
#if !defined(SQLITE_OMIT_LOAD_EXTENSION)
@@ -47905,15 +48068,17 @@
4790548068
#endif
4790648069
4790748070
#define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
4790848071
DWORD,DWORD))aSyscall[62].pCurrent)
4790948072
47910
-#if !SQLITE_OS_WINRT
48073
+/*
48074
+** For WaitForSingleObject(), MSDN says:
48075
+**
48076
+** Minimum supported client: Windows XP [desktop apps | UWP apps]
48077
+** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48078
+*/
4791148079
{ "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
47912
-#else
47913
- { "WaitForSingleObject", (SYSCALL)0, 0 },
47914
-#endif
4791548080
4791648081
#define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
4791748082
DWORD))aSyscall[63].pCurrent)
4791848083
4791948084
#if !SQLITE_OS_WINCE
@@ -48056,10 +48221,44 @@
4805648221
#endif
4805748222
4805848223
#define osFlushViewOfFile \
4805948224
((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
4806048225
48226
+/*
48227
+** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent()
48228
+** to implement blocking locks with timeouts. MSDN says:
48229
+**
48230
+** Minimum supported client: Windows XP [desktop apps | UWP apps]
48231
+** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48232
+*/
48233
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48234
+ { "CreateEvent", (SYSCALL)CreateEvent, 0 },
48235
+#else
48236
+ { "CreateEvent", (SYSCALL)0, 0 },
48237
+#endif
48238
+
48239
+#define osCreateEvent ( \
48240
+ (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \
48241
+ aSyscall[80].pCurrent \
48242
+)
48243
+
48244
+/*
48245
+** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo()
48246
+** for the case where a timeout expires and a lock request must be
48247
+** cancelled.
48248
+**
48249
+** Minimum supported client: Windows XP [desktop apps | UWP apps]
48250
+** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48251
+*/
48252
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48253
+ { "CancelIo", (SYSCALL)CancelIo, 0 },
48254
+#else
48255
+ { "CancelIo", (SYSCALL)0, 0 },
48256
+#endif
48257
+
48258
+#define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent)
48259
+
4806148260
}; /* End of the overrideable system calls */
4806248261
4806348262
/*
4806448263
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
4806548264
** "win32" VFSes. Return SQLITE_OK upon successfully updating the
@@ -48354,11 +48553,13 @@
4835448553
(sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
4835548554
#endif
4835648555
}
4835748556
return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
4835848557
#elif SQLITE_TEST
48359
- return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48558
+ return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2
48559
+ || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0
48560
+ ;
4836048561
#else
4836148562
/*
4836248563
** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
4836348564
** deprecated are always assumed to be based on the NT kernel.
4836448565
*/
@@ -49440,10 +49641,89 @@
4944049641
return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
4944149642
numBytesHigh);
4944249643
}
4944349644
#endif
4944449645
}
49646
+
49647
+/*
49648
+** Lock a region of nByte bytes starting at offset offset of file hFile.
49649
+** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock
49650
+** otherwise. If nMs is greater than zero and the lock cannot be obtained
49651
+** immediately, block for that many ms before giving up.
49652
+**
49653
+** This function returns SQLITE_OK if the lock is obtained successfully. If
49654
+** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or
49655
+** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR.
49656
+*/
49657
+static int winHandleLockTimeout(
49658
+ HANDLE hFile,
49659
+ DWORD offset,
49660
+ DWORD nByte,
49661
+ int bExcl,
49662
+ DWORD nMs
49663
+){
49664
+ DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0);
49665
+ int rc = SQLITE_OK;
49666
+ BOOL ret;
49667
+
49668
+ if( !osIsNT() ){
49669
+ ret = winLockFile(&hFile, flags, offset, 0, nByte, 0);
49670
+ }else{
49671
+ OVERLAPPED ovlp;
49672
+ memset(&ovlp, 0, sizeof(OVERLAPPED));
49673
+ ovlp.Offset = offset;
49674
+
49675
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49676
+ if( nMs!=0 ){
49677
+ flags &= ~LOCKFILE_FAIL_IMMEDIATELY;
49678
+ }
49679
+ ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL);
49680
+ if( ovlp.hEvent==NULL ){
49681
+ return SQLITE_IOERR_LOCK;
49682
+ }
49683
+#endif
49684
+
49685
+ ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp);
49686
+
49687
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49688
+ /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was
49689
+ ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to
49690
+ ** LockFileEx() may fail because the request is still pending. This can
49691
+ ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified.
49692
+ **
49693
+ ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags
49694
+ ** passed to LockFileEx(). In this case, if the operation is pending,
49695
+ ** block indefinitely until it is finished.
49696
+ **
49697
+ ** Otherwise, wait for up to nMs ms for the operation to finish. nMs
49698
+ ** may be set to INFINITE.
49699
+ */
49700
+ if( !ret && GetLastError()==ERROR_IO_PENDING ){
49701
+ DWORD nDelay = (nMs==0 ? INFINITE : nMs);
49702
+ DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay);
49703
+ if( res==WAIT_OBJECT_0 ){
49704
+ ret = TRUE;
49705
+ }else if( res==WAIT_TIMEOUT ){
49706
+ rc = SQLITE_BUSY_TIMEOUT;
49707
+ }else{
49708
+ /* Some other error has occurred */
49709
+ rc = SQLITE_IOERR_LOCK;
49710
+ }
49711
+
49712
+ /* If it is still pending, cancel the LockFileEx() call. */
49713
+ osCancelIo(hFile);
49714
+ }
49715
+
49716
+ osCloseHandle(ovlp.hEvent);
49717
+#endif
49718
+ }
49719
+
49720
+ if( rc==SQLITE_OK && !ret ){
49721
+ rc = SQLITE_BUSY;
49722
+ }
49723
+ return rc;
49724
+}
4944549725
4944649726
/*
4944749727
** Unlock a file region.
4944849728
*/
4944949729
static BOOL winUnlockFile(
@@ -49471,10 +49751,18 @@
4947149751
return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
4947249752
numBytesHigh);
4947349753
}
4947449754
#endif
4947549755
}
49756
+
49757
+/*
49758
+** Remove an nByte lock starting at offset iOff from HANDLE h.
49759
+*/
49760
+static int winHandleUnlock(HANDLE h, int iOff, int nByte){
49761
+ BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0);
49762
+ return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK);
49763
+}
4947649764
4947749765
/*****************************************************************************
4947849766
** The next group of routines implement the I/O methods specified
4947949767
** by the sqlite3_io_methods object.
4948049768
******************************************************************************/
@@ -49485,69 +49773,73 @@
4948549773
#ifndef INVALID_SET_FILE_POINTER
4948649774
# define INVALID_SET_FILE_POINTER ((DWORD)-1)
4948749775
#endif
4948849776
4948949777
/*
49490
-** Move the current position of the file handle passed as the first
49491
-** argument to offset iOffset within the file. If successful, return 0.
49492
-** Otherwise, set pFile->lastErrno and return non-zero.
49778
+** Seek the file handle h to offset nByte of the file.
49779
+**
49780
+** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite
49781
+** error code.
4949349782
*/
49494
-static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49783
+static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){
49784
+ int rc = SQLITE_OK; /* Return value */
49785
+
4949549786
#if !SQLITE_OS_WINRT
4949649787
LONG upperBits; /* Most sig. 32 bits of new offset */
4949749788
LONG lowerBits; /* Least sig. 32 bits of new offset */
4949849789
DWORD dwRet; /* Value returned by SetFilePointer() */
49499
- DWORD lastErrno; /* Value returned by GetLastError() */
49500
-
49501
- OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
4950249790
4950349791
upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
4950449792
lowerBits = (LONG)(iOffset & 0xffffffff);
49793
+
49794
+ dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN);
4950549795
4950649796
/* API oddity: If successful, SetFilePointer() returns a dword
4950749797
** containing the lower 32-bits of the new file-offset. Or, if it fails,
4950849798
** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
4950949799
** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
4951049800
** whether an error has actually occurred, it is also necessary to call
49511
- ** GetLastError().
49512
- */
49513
- dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49514
-
49515
- if( (dwRet==INVALID_SET_FILE_POINTER
49516
- && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
49517
- pFile->lastErrno = lastErrno;
49518
- winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49519
- "winSeekFile", pFile->zPath);
49520
- OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49521
- return 1;
49522
- }
49523
-
49524
- OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49525
- return 0;
49526
-#else
49527
- /*
49528
- ** Same as above, except that this implementation works for WinRT.
49529
- */
49530
-
49801
+ ** GetLastError(). */
49802
+ if( dwRet==INVALID_SET_FILE_POINTER ){
49803
+ DWORD lastErrno = osGetLastError();
49804
+ if( lastErrno!=NO_ERROR ){
49805
+ rc = SQLITE_IOERR_SEEK;
49806
+ }
49807
+ }
49808
+#else
49809
+ /* This implementation works for WinRT. */
4953149810
LARGE_INTEGER x; /* The new offset */
4953249811
BOOL bRet; /* Value returned by SetFilePointerEx() */
4953349812
4953449813
x.QuadPart = iOffset;
49535
- bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49814
+ bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN);
4953649815
4953749816
if(!bRet){
49817
+ rc = SQLITE_IOERR_SEEK;
49818
+ }
49819
+#endif
49820
+
49821
+ OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc)));
49822
+ return rc;
49823
+}
49824
+
49825
+/*
49826
+** Move the current position of the file handle passed as the first
49827
+** argument to offset iOffset within the file. If successful, return 0.
49828
+** Otherwise, set pFile->lastErrno and return non-zero.
49829
+*/
49830
+static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49831
+ int rc;
49832
+
49833
+ rc = winHandleSeek(pFile->h, iOffset);
49834
+ if( rc!=SQLITE_OK ){
4953849835
pFile->lastErrno = osGetLastError();
49539
- winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49540
- "winSeekFile", pFile->zPath);
49541
- OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49542
- return 1;
49543
- }
49544
-
49545
- OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49546
- return 0;
49547
-#endif
49548
-}
49836
+ winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath);
49837
+ }
49838
+ return rc;
49839
+}
49840
+
4954949841
4955049842
#if SQLITE_MAX_MMAP_SIZE>0
4955149843
/* Forward references to VFS helper methods used for memory mapped files */
4955249844
static int winMapfile(winFile*, sqlite3_int64);
4955349845
static int winUnmapfile(winFile*);
@@ -49803,10 +50095,64 @@
4980350095
}
4980450096
OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
4980550097
osGetCurrentProcessId(), pFile, pFile->h));
4980650098
return SQLITE_OK;
4980750099
}
50100
+
50101
+/*
50102
+** Truncate the file opened by handle h to nByte bytes in size.
50103
+*/
50104
+static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){
50105
+ int rc = SQLITE_OK; /* Return code */
50106
+ rc = winHandleSeek(h, nByte);
50107
+ if( rc==SQLITE_OK ){
50108
+ if( 0==osSetEndOfFile(h) ){
50109
+ rc = SQLITE_IOERR_TRUNCATE;
50110
+ }
50111
+ }
50112
+ return rc;
50113
+}
50114
+
50115
+/*
50116
+** Determine the size in bytes of the file opened by the handle passed as
50117
+** the first argument.
50118
+*/
50119
+static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){
50120
+ int rc = SQLITE_OK;
50121
+
50122
+#if SQLITE_OS_WINRT
50123
+ FILE_STANDARD_INFO info;
50124
+ BOOL b;
50125
+ b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info));
50126
+ if( b ){
50127
+ *pnByte = info.EndOfFile.QuadPart;
50128
+ }else{
50129
+ rc = SQLITE_IOERR_FSTAT;
50130
+ }
50131
+#else
50132
+ DWORD upperBits = 0;
50133
+ DWORD lowerBits = 0;
50134
+
50135
+ assert( pnByte );
50136
+ lowerBits = osGetFileSize(h, &upperBits);
50137
+ *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits;
50138
+ if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){
50139
+ rc = SQLITE_IOERR_FSTAT;
50140
+ }
50141
+#endif
50142
+
50143
+ return rc;
50144
+}
50145
+
50146
+/*
50147
+** Close the handle passed as the only argument.
50148
+*/
50149
+static void winHandleClose(HANDLE h){
50150
+ if( h!=INVALID_HANDLE_VALUE ){
50151
+ osCloseHandle(h);
50152
+ }
50153
+}
4980850154
4980950155
/*
4981050156
** Truncate an open file to a specified size
4981150157
*/
4981250158
static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
@@ -50059,31 +50405,32 @@
5005950405
/*
5006050406
** Acquire a reader lock.
5006150407
** Different API routines are called depending on whether or not this
5006250408
** is Win9x or WinNT.
5006350409
*/
50064
-static int winGetReadLock(winFile *pFile){
50410
+static int winGetReadLock(winFile *pFile, int bBlock){
5006550411
int res;
50412
+ DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0);
5006650413
OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
5006750414
if( osIsNT() ){
5006850415
#if SQLITE_OS_WINCE
5006950416
/*
5007050417
** NOTE: Windows CE is handled differently here due its lack of the Win32
5007150418
** API LockFileEx.
5007250419
*/
5007350420
res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
5007450421
#else
50075
- res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
50422
+ res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0,
5007650423
SHARED_SIZE, 0);
5007750424
#endif
5007850425
}
5007950426
#ifdef SQLITE_WIN32_HAS_ANSI
5008050427
else{
5008150428
int lk;
5008250429
sqlite3_randomness(sizeof(lk), &lk);
5008350430
pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
50084
- res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50431
+ res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask,
5008550432
SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
5008650433
}
5008750434
#endif
5008850435
if( res == 0 ){
5008950436
pFile->lastErrno = osGetLastError();
@@ -50174,50 +50521,66 @@
5017450521
*/
5017550522
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
5017650523
assert( locktype!=PENDING_LOCK );
5017750524
assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
5017850525
50179
- /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
50526
+ /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or
5018050527
** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
5018150528
** the PENDING_LOCK byte is temporary.
5018250529
*/
5018350530
newLocktype = pFile->locktype;
50184
- if( pFile->locktype==NO_LOCK
50185
- || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
50531
+ if( locktype==SHARED_LOCK
50532
+ || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
5018650533
){
5018750534
int cnt = 3;
50188
- while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50189
- PENDING_BYTE, 0, 1, 0))==0 ){
50535
+
50536
+ /* Flags for the LockFileEx() call. This should be an exclusive lock if
50537
+ ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to
50538
+ ** obtain SHARED. */
50539
+ int flags = LOCKFILE_FAIL_IMMEDIATELY;
50540
+ if( locktype==EXCLUSIVE_LOCK ){
50541
+ flags |= LOCKFILE_EXCLUSIVE_LOCK;
50542
+ }
50543
+ while( cnt>0 ){
5019050544
/* Try 3 times to get the pending lock. This is needed to work
5019150545
** around problems caused by indexing and/or anti-virus software on
5019250546
** Windows systems.
50547
+ **
5019350548
** If you are using this code as a model for alternative VFSes, do not
50194
- ** copy this retry logic. It is a hack intended for Windows only.
50195
- */
50549
+ ** copy this retry logic. It is a hack intended for Windows only. */
50550
+ res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0);
50551
+ if( res ) break;
50552
+
5019650553
lastErrno = osGetLastError();
5019750554
OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50198
- pFile->h, cnt, res));
50555
+ pFile->h, cnt, res
50556
+ ));
50557
+
5019950558
if( lastErrno==ERROR_INVALID_HANDLE ){
5020050559
pFile->lastErrno = lastErrno;
5020150560
rc = SQLITE_IOERR_LOCK;
5020250561
OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50203
- pFile->h, cnt, sqlite3ErrName(rc)));
50562
+ pFile->h, cnt, sqlite3ErrName(rc)
50563
+ ));
5020450564
return rc;
5020550565
}
50206
- if( cnt ) sqlite3_win32_sleep(1);
50566
+
50567
+ cnt--;
50568
+ if( cnt>0 ) sqlite3_win32_sleep(1);
5020750569
}
5020850570
gotPendingLock = res;
50209
- if( !res ){
50210
- lastErrno = osGetLastError();
50211
- }
5021250571
}
5021350572
5021450573
/* Acquire a shared lock
5021550574
*/
5021650575
if( locktype==SHARED_LOCK && res ){
5021750576
assert( pFile->locktype==NO_LOCK );
50218
- res = winGetReadLock(pFile);
50577
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50578
+ res = winGetReadLock(pFile, pFile->bBlockOnConnect);
50579
+#else
50580
+ res = winGetReadLock(pFile, 0);
50581
+#endif
5021950582
if( res ){
5022050583
newLocktype = SHARED_LOCK;
5022150584
}else{
5022250585
lastErrno = osGetLastError();
5022350586
}
@@ -50251,11 +50614,11 @@
5025150614
SHARED_SIZE, 0);
5025250615
if( res ){
5025350616
newLocktype = EXCLUSIVE_LOCK;
5025450617
}else{
5025550618
lastErrno = osGetLastError();
50256
- winGetReadLock(pFile);
50619
+ winGetReadLock(pFile, 0);
5025750620
}
5025850621
}
5025950622
5026050623
/* If we are holding a PENDING lock that ought to be released, then
5026150624
** release it now.
@@ -50331,11 +50694,11 @@
5033150694
OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
5033250695
pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
5033350696
type = pFile->locktype;
5033450697
if( type>=EXCLUSIVE_LOCK ){
5033550698
winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50336
- if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
50699
+ if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){
5033750700
/* This should never happen. We should always be able to
5033850701
** reacquire the read lock */
5033950702
rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
5034050703
"winUnlock", pFile->zPath);
5034150704
}
@@ -50541,10 +50904,32 @@
5054150904
}
5054250905
OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
5054350906
return rc;
5054450907
}
5054550908
#endif
50909
+
50910
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50911
+ case SQLITE_FCNTL_LOCK_TIMEOUT: {
50912
+ int iOld = pFile->iBusyTimeout;
50913
+ int iNew = *(int*)pArg;
50914
+#if SQLITE_ENABLE_SETLK_TIMEOUT==1
50915
+ pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew;
50916
+#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
50917
+ pFile->iBusyTimeout = (DWORD)(!!iNew);
50918
+#else
50919
+# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
50920
+#endif
50921
+ *(int*)pArg = iOld;
50922
+ return SQLITE_OK;
50923
+ }
50924
+ case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
50925
+ int iNew = *(int*)pArg;
50926
+ pFile->bBlockOnConnect = iNew;
50927
+ return SQLITE_OK;
50928
+ }
50929
+#endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
50930
+
5054650931
}
5054750932
OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
5054850933
return SQLITE_NOTFOUND;
5054950934
}
5055050935
@@ -50621,36 +51006,39 @@
5062151006
** nRef
5062251007
** pNext
5062351008
**
5062451009
** The following fields are read-only after the object is created:
5062551010
**
50626
-** fid
5062751011
** zFilename
5062851012
**
5062951013
** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
5063051014
** winShmMutexHeld() is true when reading or writing any other field
5063151015
** in this structure.
5063251016
**
51017
+** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51018
+** the *-shm file if the DMS-locking protocol demands it, and (c) map
51019
+** regions of the *-shm file into memory using MapViewOfFile() or
51020
+** similar. Other locks are taken by individual clients using the
51021
+** winShm.hShm handles.
5063351022
*/
5063451023
struct winShmNode {
5063551024
sqlite3_mutex *mutex; /* Mutex to access this object */
5063651025
char *zFilename; /* Name of the file */
50637
- winFile hFile; /* File handle from winOpen */
51026
+ HANDLE hSharedShm; /* File handle open on zFilename */
5063851027
51028
+ int isUnlocked; /* DMS lock has not yet been obtained */
51029
+ int isReadonly; /* True if read-only */
5063951030
int szRegion; /* Size of shared-memory regions */
5064051031
int nRegion; /* Size of array apRegion */
50641
- u8 isReadonly; /* True if read-only */
50642
- u8 isUnlocked; /* True if no DMS lock held */
5064351032
5064451033
struct ShmRegion {
5064551034
HANDLE hMap; /* File handle from CreateFileMapping */
5064651035
void *pMap;
5064751036
} *aRegion;
5064851037
DWORD lastErrno; /* The Windows errno from the last I/O error */
5064951038
5065051039
int nRef; /* Number of winShm objects pointing to this */
50651
- winShm *pFirst; /* All winShm objects pointing to this */
5065251040
winShmNode *pNext; /* Next in list of all winShmNode objects */
5065351041
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
5065451042
u8 nextShmId; /* Next available winShm.id value */
5065551043
#endif
5065651044
};
@@ -50662,27 +51050,19 @@
5066251050
*/
5066351051
static winShmNode *winShmNodeList = 0;
5066451052
5066551053
/*
5066651054
** Structure used internally by this VFS to record the state of an
50667
-** open shared memory connection.
50668
-**
50669
-** The following fields are initialized when this object is created and
50670
-** are read-only thereafter:
50671
-**
50672
-** winShm.pShmNode
50673
-** winShm.id
50674
-**
50675
-** All other fields are read/write. The winShm.pShmNode->mutex must be held
50676
-** while accessing any read/write fields.
51055
+** open shared memory connection. There is one such structure for each
51056
+** winFile open on a wal mode database.
5067751057
*/
5067851058
struct winShm {
5067951059
winShmNode *pShmNode; /* The underlying winShmNode object */
50680
- winShm *pNext; /* Next winShm with the same winShmNode */
50681
- u8 hasMutex; /* True if holding the winShmNode mutex */
5068251060
u16 sharedMask; /* Mask of shared locks held */
5068351061
u16 exclMask; /* Mask of exclusive locks held */
51062
+ HANDLE hShm; /* File-handle on *-shm file. For locking. */
51063
+ int bReadonly; /* True if hShm is opened read-only */
5068451064
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
5068551065
u8 id; /* Id of this connection with its winShmNode */
5068651066
#endif
5068751067
};
5068851068
@@ -50690,54 +51070,10 @@
5069051070
** Constants used for locking
5069151071
*/
5069251072
#define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
5069351073
#define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
5069451074
50695
-/*
50696
-** Apply advisory locks for all n bytes beginning at ofst.
50697
-*/
50698
-#define WINSHM_UNLCK 1
50699
-#define WINSHM_RDLCK 2
50700
-#define WINSHM_WRLCK 3
50701
-static int winShmSystemLock(
50702
- winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50703
- int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
50704
- int ofst, /* Offset to first byte to be locked/unlocked */
50705
- int nByte /* Number of bytes to lock or unlock */
50706
-){
50707
- int rc = 0; /* Result code form Lock/UnlockFileEx() */
50708
-
50709
- /* Access to the winShmNode object is serialized by the caller */
50710
- assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
50711
-
50712
- OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50713
- pFile->hFile.h, lockType, ofst, nByte));
50714
-
50715
- /* Release/Acquire the system-level lock */
50716
- if( lockType==WINSHM_UNLCK ){
50717
- rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50718
- }else{
50719
- /* Initialize the locking parameters */
50720
- DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
50721
- if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
50722
- rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50723
- }
50724
-
50725
- if( rc!= 0 ){
50726
- rc = SQLITE_OK;
50727
- }else{
50728
- pFile->lastErrno = osGetLastError();
50729
- rc = SQLITE_BUSY;
50730
- }
50731
-
50732
- OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50733
- pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50734
- "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50735
-
50736
- return rc;
50737
-}
50738
-
5073951075
/* Forward references to VFS methods */
5074051076
static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
5074151077
static int winDelete(sqlite3_vfs *,const char*,int);
5074251078
5074351079
/*
@@ -50765,15 +51101,11 @@
5076551101
bRc = osCloseHandle(p->aRegion[i].hMap);
5076651102
OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
5076751103
osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
5076851104
UNUSED_VARIABLE_VALUE(bRc);
5076951105
}
50770
- if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
50771
- SimulateIOErrorBenign(1);
50772
- winClose((sqlite3_file *)&p->hFile);
50773
- SimulateIOErrorBenign(0);
50774
- }
51106
+ winHandleClose(p->hSharedShm);
5077551107
if( deleteFlag ){
5077651108
SimulateIOErrorBenign(1);
5077751109
sqlite3BeginBenignMalloc();
5077851110
winDelete(pVfs, p->zFilename, 0);
5077951111
sqlite3EndBenignMalloc();
@@ -50787,46 +51119,166 @@
5078751119
}
5078851120
}
5078951121
}
5079051122
5079151123
/*
50792
-** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
50793
-** take it now. Return SQLITE_OK if successful, or an SQLite error
50794
-** code otherwise.
50795
-**
50796
-** If the DMS cannot be locked because this is a readonly_shm=1
50797
-** connection and no other process already holds a lock, return
50798
-** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
51124
+** The DMS lock has not yet been taken on the shm file associated with
51125
+** pShmNode. Take the lock. Truncate the *-shm file if required.
51126
+** Return SQLITE_OK if successful, or an SQLite error code otherwise.
5079951127
*/
50800
-static int winLockSharedMemory(winShmNode *pShmNode){
50801
- int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
51128
+static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){
51129
+ HANDLE h = pShmNode->hSharedShm;
51130
+ int rc = SQLITE_OK;
5080251131
51132
+ assert( sqlite3_mutex_held(pShmNode->mutex) );
51133
+ rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0);
5080351134
if( rc==SQLITE_OK ){
51135
+ /* We have an EXCLUSIVE lock on the DMS byte. This means that this
51136
+ ** is the first process to open the file. Truncate it to zero bytes
51137
+ ** in this case. */
5080451138
if( pShmNode->isReadonly ){
50805
- pShmNode->isUnlocked = 1;
50806
- winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50807
- return SQLITE_READONLY_CANTINIT;
50808
- }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50809
- winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50810
- return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
50811
- "winLockSharedMemory", pShmNode->zFilename);
50812
- }
50813
- }
50814
-
50815
- if( rc==SQLITE_OK ){
50816
- winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50817
- }
50818
-
50819
- return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
50820
-}
50821
-
50822
-/*
50823
-** Open the shared-memory area associated with database file pDbFd.
50824
-**
50825
-** When opening a new shared-memory file, if no other instances of that
50826
-** file are currently open, in this process or in other processes, then
50827
-** the file must be truncated to zero length or have its header cleared.
51139
+ rc = SQLITE_READONLY_CANTINIT;
51140
+ }else{
51141
+ rc = winHandleTruncate(h, 0);
51142
+ }
51143
+
51144
+ /* Release the EXCLUSIVE lock acquired above. */
51145
+ winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0);
51146
+ }else if( (rc & 0xFF)==SQLITE_BUSY ){
51147
+ rc = SQLITE_OK;
51148
+ }
51149
+
51150
+ if( rc==SQLITE_OK ){
51151
+ /* Take a SHARED lock on the DMS byte. */
51152
+ rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs);
51153
+ if( rc==SQLITE_OK ){
51154
+ pShmNode->isUnlocked = 0;
51155
+ }
51156
+ }
51157
+
51158
+ return rc;
51159
+}
51160
+
51161
+
51162
+/*
51163
+** Convert a UTF-8 filename into whatever form the underlying
51164
+** operating system wants filenames in. Space to hold the result
51165
+** is obtained from malloc and must be freed by the calling
51166
+** function.
51167
+*/
51168
+static void *winConvertFromUtf8Filename(const char *zFilename){
51169
+ void *zConverted = 0;
51170
+ if( osIsNT() ){
51171
+ zConverted = winUtf8ToUnicode(zFilename);
51172
+ }
51173
+#ifdef SQLITE_WIN32_HAS_ANSI
51174
+ else{
51175
+ zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51176
+ }
51177
+#endif
51178
+ /* caller will handle out of memory */
51179
+ return zConverted;
51180
+}
51181
+
51182
+/*
51183
+** This function is used to open a handle on a *-shm file.
51184
+**
51185
+** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file
51186
+** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not.
51187
+*/
51188
+static int winHandleOpen(
51189
+ const char *zUtf8, /* File to open */
51190
+ int *pbReadonly, /* IN/OUT: True for readonly handle */
51191
+ HANDLE *ph /* OUT: New HANDLE for file */
51192
+){
51193
+ int rc = SQLITE_OK;
51194
+ void *zConverted = 0;
51195
+ int bReadonly = *pbReadonly;
51196
+ HANDLE h = INVALID_HANDLE_VALUE;
51197
+
51198
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51199
+ const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED;
51200
+#else
51201
+ const DWORD flag_overlapped = 0;
51202
+#endif
51203
+
51204
+ /* Convert the filename to the system encoding. */
51205
+ zConverted = winConvertFromUtf8Filename(zUtf8);
51206
+ if( zConverted==0 ){
51207
+ OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8));
51208
+ rc = SQLITE_IOERR_NOMEM_BKPT;
51209
+ goto winopenfile_out;
51210
+ }
51211
+
51212
+ /* Ensure the file we are trying to open is not actually a directory. */
51213
+ if( winIsDir(zConverted) ){
51214
+ OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8));
51215
+ rc = SQLITE_CANTOPEN_ISDIR;
51216
+ goto winopenfile_out;
51217
+ }
51218
+
51219
+ /* TODO: platforms.
51220
+ ** TODO: retry-on-ioerr.
51221
+ */
51222
+ if( osIsNT() ){
51223
+#if SQLITE_OS_WINRT
51224
+ CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51225
+ memset(&extendedParameters, 0, sizeof(extendedParameters));
51226
+ extendedParameters.dwSize = sizeof(extendedParameters);
51227
+ extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
51228
+ extendedParameters.dwFileFlags = flag_overlapped;
51229
+ extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51230
+ h = osCreateFile2((LPCWSTR)zConverted,
51231
+ (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */
51232
+ FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51233
+ OPEN_ALWAYS, /* dwCreationDisposition */
51234
+ &extendedParameters
51235
+ );
51236
+#else
51237
+ h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */
51238
+ (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51239
+ FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51240
+ NULL, /* lpSecurityAttributes */
51241
+ OPEN_ALWAYS, /* dwCreationDisposition */
51242
+ FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51243
+ NULL
51244
+ );
51245
+#endif
51246
+ }else{
51247
+ /* Due to pre-processor directives earlier in this file,
51248
+ ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */
51249
+#ifdef SQLITE_WIN32_HAS_ANSI
51250
+ h = osCreateFileA((LPCSTR)zConverted,
51251
+ (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51252
+ FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51253
+ NULL, /* lpSecurityAttributes */
51254
+ OPEN_ALWAYS, /* dwCreationDisposition */
51255
+ FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51256
+ NULL
51257
+ );
51258
+#endif
51259
+ }
51260
+
51261
+ if( h==INVALID_HANDLE_VALUE ){
51262
+ if( bReadonly==0 ){
51263
+ bReadonly = 1;
51264
+ rc = winHandleOpen(zUtf8, &bReadonly, &h);
51265
+ }else{
51266
+ rc = SQLITE_CANTOPEN_BKPT;
51267
+ }
51268
+ }
51269
+
51270
+ winopenfile_out:
51271
+ sqlite3_free(zConverted);
51272
+ *pbReadonly = bReadonly;
51273
+ *ph = h;
51274
+ return rc;
51275
+}
51276
+
51277
+
51278
+/*
51279
+** Open the shared-memory area associated with database file pDbFd.
5082851280
*/
5082951281
static int winOpenSharedMemory(winFile *pDbFd){
5083051282
struct winShm *p; /* The connection to be opened */
5083151283
winShmNode *pShmNode = 0; /* The underlying mmapped file */
5083251284
int rc = SQLITE_OK; /* Result code */
@@ -50834,102 +51286,87 @@
5083451286
int nName; /* Size of zName in bytes */
5083551287
5083651288
assert( pDbFd->pShm==0 ); /* Not previously opened */
5083751289
5083851290
/* Allocate space for the new sqlite3_shm object. Also speculatively
50839
- ** allocate space for a new winShmNode and filename.
50840
- */
51291
+ ** allocate space for a new winShmNode and filename. */
5084151292
p = sqlite3MallocZero( sizeof(*p) );
5084251293
if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
5084351294
nName = sqlite3Strlen30(pDbFd->zPath);
5084451295
pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 );
5084551296
if( pNew==0 ){
5084651297
sqlite3_free(p);
5084751298
return SQLITE_IOERR_NOMEM_BKPT;
5084851299
}
5084951300
pNew->zFilename = (char*)&pNew[1];
51301
+ pNew->hSharedShm = INVALID_HANDLE_VALUE;
51302
+ pNew->isUnlocked = 1;
5085051303
sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
5085151304
sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
51305
+
51306
+ /* Open a file-handle on the *-shm file for this connection. This file-handle
51307
+ ** is only used for locking. The mapping of the *-shm file is created using
51308
+ ** the shared file handle in winShmNode.hSharedShm. */
51309
+ p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0);
51310
+ rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm);
5085251311
5085351312
/* Look to see if there is an existing winShmNode that can be used.
50854
- ** If no matching winShmNode currently exists, create a new one.
50855
- */
51313
+ ** If no matching winShmNode currently exists, then create a new one. */
5085651314
winShmEnterMutex();
5085751315
for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
5085851316
/* TBD need to come up with better match here. Perhaps
50859
- ** use FILE_ID_BOTH_DIR_INFO Structure.
50860
- */
51317
+ ** use FILE_ID_BOTH_DIR_INFO Structure. */
5086151318
if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
5086251319
}
50863
- if( pShmNode ){
50864
- sqlite3_free(pNew);
50865
- }else{
50866
- int inFlags = SQLITE_OPEN_WAL;
50867
- int outFlags = 0;
50868
-
51320
+ if( pShmNode==0 ){
5086951321
pShmNode = pNew;
50870
- pNew = 0;
50871
- ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50872
- pShmNode->pNext = winShmNodeList;
50873
- winShmNodeList = pShmNode;
5087451322
51323
+ /* Allocate a mutex for this winShmNode object, if one is required. */
5087551324
if( sqlite3GlobalConfig.bCoreMutex ){
5087651325
pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50877
- if( pShmNode->mutex==0 ){
50878
- rc = SQLITE_IOERR_NOMEM_BKPT;
50879
- goto shm_open_err;
50880
- }
50881
- }
50882
-
50883
- if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50884
- inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
50885
- }else{
50886
- inFlags |= SQLITE_OPEN_READONLY;
50887
- }
50888
- rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50889
- (sqlite3_file*)&pShmNode->hFile,
50890
- inFlags, &outFlags);
50891
- if( rc!=SQLITE_OK ){
50892
- rc = winLogError(rc, osGetLastError(), "winOpenShm",
50893
- pShmNode->zFilename);
50894
- goto shm_open_err;
50895
- }
50896
- if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
50897
-
50898
- rc = winLockSharedMemory(pShmNode);
50899
- if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
50900
- }
50901
-
50902
- /* Make the new connection a child of the winShmNode */
50903
- p->pShmNode = pShmNode;
51326
+ if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT;
51327
+ }
51328
+
51329
+ /* Open a file-handle to use for mappings, and for the DMS lock. */
51330
+ if( rc==SQLITE_OK ){
51331
+ HANDLE h = INVALID_HANDLE_VALUE;
51332
+ pShmNode->isReadonly = p->bReadonly;
51333
+ rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h);
51334
+ pShmNode->hSharedShm = h;
51335
+ }
51336
+
51337
+ /* If successful, link the new winShmNode into the global list. If an
51338
+ ** error occurred, free the object. */
51339
+ if( rc==SQLITE_OK ){
51340
+ pShmNode->pNext = winShmNodeList;
51341
+ winShmNodeList = pShmNode;
51342
+ pNew = 0;
51343
+ }else{
51344
+ sqlite3_mutex_free(pShmNode->mutex);
51345
+ if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){
51346
+ osCloseHandle(pShmNode->hSharedShm);
51347
+ }
51348
+ }
51349
+ }
51350
+
51351
+ /* If no error has occurred, link the winShm object to the winShmNode and
51352
+ ** the winShm to pDbFd. */
51353
+ if( rc==SQLITE_OK ){
51354
+ p->pShmNode = pShmNode;
51355
+ pShmNode->nRef++;
5090451356
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50905
- p->id = pShmNode->nextShmId++;
50906
-#endif
50907
- pShmNode->nRef++;
50908
- pDbFd->pShm = p;
50909
- winShmLeaveMutex();
50910
-
50911
- /* The reference count on pShmNode has already been incremented under
50912
- ** the cover of the winShmEnterMutex() mutex and the pointer from the
50913
- ** new (struct winShm) object to the pShmNode has been set. All that is
50914
- ** left to do is to link the new object into the linked list starting
50915
- ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50916
- ** mutex.
50917
- */
50918
- sqlite3_mutex_enter(pShmNode->mutex);
50919
- p->pNext = pShmNode->pFirst;
50920
- pShmNode->pFirst = p;
50921
- sqlite3_mutex_leave(pShmNode->mutex);
50922
- return rc;
50923
-
50924
- /* Jump here on any error */
50925
-shm_open_err:
50926
- winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50927
- winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50928
- sqlite3_free(p);
50929
- sqlite3_free(pNew);
50930
- winShmLeaveMutex();
51357
+ p->id = pShmNode->nextShmId++;
51358
+#endif
51359
+ pDbFd->pShm = p;
51360
+ }else if( p ){
51361
+ winHandleClose(p->hShm);
51362
+ sqlite3_free(p);
51363
+ }
51364
+
51365
+ assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 );
51366
+ winShmLeaveMutex();
51367
+ sqlite3_free(pNew);
5093151368
return rc;
5093251369
}
5093351370
5093451371
/*
5093551372
** Close a connection to shared-memory. Delete the underlying
@@ -50940,38 +51377,33 @@
5094051377
int deleteFlag /* Delete after closing if true */
5094151378
){
5094251379
winFile *pDbFd; /* Database holding shared-memory */
5094351380
winShm *p; /* The connection to be closed */
5094451381
winShmNode *pShmNode; /* The underlying shared-memory file */
50945
- winShm **pp; /* For looping over sibling connections */
5094651382
5094751383
pDbFd = (winFile*)fd;
5094851384
p = pDbFd->pShm;
5094951385
if( p==0 ) return SQLITE_OK;
51386
+ if( p->hShm!=INVALID_HANDLE_VALUE ){
51387
+ osCloseHandle(p->hShm);
51388
+ }
51389
+
5095051390
pShmNode = p->pShmNode;
50951
-
50952
- /* Remove connection p from the set of connections associated
50953
- ** with pShmNode */
50954
- sqlite3_mutex_enter(pShmNode->mutex);
50955
- for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50956
- *pp = p->pNext;
50957
-
50958
- /* Free the connection p */
50959
- sqlite3_free(p);
50960
- pDbFd->pShm = 0;
50961
- sqlite3_mutex_leave(pShmNode->mutex);
51391
+ winShmEnterMutex();
5096251392
5096351393
/* If pShmNode->nRef has reached 0, then close the underlying
50964
- ** shared-memory file, too */
50965
- winShmEnterMutex();
51394
+ ** shared-memory file, too. */
5096651395
assert( pShmNode->nRef>0 );
5096751396
pShmNode->nRef--;
5096851397
if( pShmNode->nRef==0 ){
5096951398
winShmPurge(pDbFd->pVfs, deleteFlag);
5097051399
}
5097151400
winShmLeaveMutex();
5097251401
51402
+ /* Free the connection p */
51403
+ sqlite3_free(p);
51404
+ pDbFd->pShm = 0;
5097351405
return SQLITE_OK;
5097451406
}
5097551407
5097651408
/*
5097751409
** Change the lock state for a shared-memory segment.
@@ -50982,14 +51414,13 @@
5098251414
int n, /* Number of locks to acquire or release */
5098351415
int flags /* What to do with the lock */
5098451416
){
5098551417
winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
5098651418
winShm *p = pDbFd->pShm; /* The shared memory being locked */
50987
- winShm *pX; /* For looping over all siblings */
5098851419
winShmNode *pShmNode;
5098951420
int rc = SQLITE_OK; /* Result code */
50990
- u16 mask; /* Mask of locks to take or release */
51421
+ u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */
5099151422
5099251423
if( p==0 ) return SQLITE_IOERR_SHMLOCK;
5099351424
pShmNode = p->pShmNode;
5099451425
if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
5099551426
@@ -50999,89 +51430,86 @@
5099951430
|| flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
5100051431
|| flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
5100151432
|| flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
5100251433
assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
5100351434
51004
- mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
51005
- assert( n>1 || mask==(1<<ofst) );
51006
- sqlite3_mutex_enter(pShmNode->mutex);
51007
- if( flags & SQLITE_SHM_UNLOCK ){
51008
- u16 allMask = 0; /* Mask of locks held by siblings */
51009
-
51010
- /* See if any siblings hold this same lock */
51011
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51012
- if( pX==p ) continue;
51013
- assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
51014
- allMask |= pX->sharedMask;
51015
- }
51016
-
51017
- /* Unlock the system-level locks */
51018
- if( (mask & allMask)==0 ){
51019
- rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
51020
- }else{
51021
- rc = SQLITE_OK;
51022
- }
51023
-
51024
- /* Undo the local locks */
51025
- if( rc==SQLITE_OK ){
51026
- p->exclMask &= ~mask;
51027
- p->sharedMask &= ~mask;
51028
- }
51029
- }else if( flags & SQLITE_SHM_SHARED ){
51030
- u16 allShared = 0; /* Union of locks held by connections other than "p" */
51031
-
51032
- /* Find out which shared locks are already held by sibling connections.
51033
- ** If any sibling already holds an exclusive lock, go ahead and return
51034
- ** SQLITE_BUSY.
51035
- */
51036
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51037
- if( (pX->exclMask & mask)!=0 ){
51038
- rc = SQLITE_BUSY;
51039
- break;
51040
- }
51041
- allShared |= pX->sharedMask;
51042
- }
51043
-
51044
- /* Get shared locks at the system level, if necessary */
51045
- if( rc==SQLITE_OK ){
51046
- if( (allShared & mask)==0 ){
51047
- rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
51048
- }else{
51049
- rc = SQLITE_OK;
51050
- }
51051
- }
51052
-
51053
- /* Get the local shared locks */
51054
- if( rc==SQLITE_OK ){
51055
- p->sharedMask |= mask;
51056
- }
51057
- }else{
51058
- /* Make sure no sibling connections hold locks that will block this
51059
- ** lock. If any do, return SQLITE_BUSY right away.
51060
- */
51061
- for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51062
- if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
51063
- rc = SQLITE_BUSY;
51064
- break;
51065
- }
51066
- }
51067
-
51068
- /* Get the exclusive locks at the system level. Then if successful
51069
- ** also mark the local connection as being locked.
51070
- */
51071
- if( rc==SQLITE_OK ){
51072
- rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
51073
- if( rc==SQLITE_OK ){
51074
- assert( (p->sharedMask & mask)==0 );
51075
- p->exclMask |= mask;
51076
- }
51077
- }
51078
- }
51079
- sqlite3_mutex_leave(pShmNode->mutex);
51080
- OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
51081
- osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51082
- sqlite3ErrName(rc)));
51435
+ /* Check that, if this to be a blocking lock, no locks that occur later
51436
+ ** in the following list than the lock being obtained are already held:
51437
+ **
51438
+ ** 1. Checkpointer lock (ofst==1).
51439
+ ** 2. Write lock (ofst==0).
51440
+ ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
51441
+ **
51442
+ ** In other words, if this is a blocking lock, none of the locks that
51443
+ ** occur later in the above list than the lock being obtained may be
51444
+ ** held.
51445
+ **
51446
+ ** It is not permitted to block on the RECOVER lock.
51447
+ */
51448
+#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
51449
+ {
51450
+ u16 lockMask = (p->exclMask|p->sharedMask);
51451
+ assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
51452
+ (ofst!=2) /* not RECOVER */
51453
+ && (ofst!=1 || lockMask==0 || lockMask==2)
51454
+ && (ofst!=0 || lockMask<3)
51455
+ && (ofst<3 || lockMask<(1<<ofst))
51456
+ ));
51457
+ }
51458
+#endif
51459
+
51460
+ /* Check if there is any work to do. There are three cases:
51461
+ **
51462
+ ** a) An unlock operation where there are locks to unlock,
51463
+ ** b) An shared lock where the requested lock is not already held
51464
+ ** c) An exclusive lock where the requested lock is not already held
51465
+ **
51466
+ ** The SQLite core never requests an exclusive lock that it already holds.
51467
+ ** This is assert()ed immediately below. */
51468
+ assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)
51469
+ || 0==(p->exclMask & mask)
51470
+ );
51471
+ if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
51472
+ || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
51473
+ || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK))
51474
+ ){
51475
+
51476
+ if( flags & SQLITE_SHM_UNLOCK ){
51477
+ /* Case (a) - unlock. */
51478
+
51479
+ assert( (p->exclMask & p->sharedMask)==0 );
51480
+ assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
51481
+ assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
51482
+
51483
+ rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n);
51484
+
51485
+ /* If successful, also clear the bits in sharedMask/exclMask */
51486
+ if( rc==SQLITE_OK ){
51487
+ p->exclMask = (p->exclMask & ~mask);
51488
+ p->sharedMask = (p->sharedMask & ~mask);
51489
+ }
51490
+ }else{
51491
+ int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE) ? 1 : 0);
51492
+ DWORD nMs = winFileBusyTimeout(pDbFd);
51493
+ rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs);
51494
+ if( rc==SQLITE_OK ){
51495
+ if( bExcl ){
51496
+ p->exclMask = (p->exclMask | mask);
51497
+ }else{
51498
+ p->sharedMask = (p->sharedMask | mask);
51499
+ }
51500
+ }
51501
+ }
51502
+ }
51503
+
51504
+ OSTRACE((
51505
+ "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x,"
51506
+ " rc=%s\n",
51507
+ ofst, n, flags,
51508
+ osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51509
+ sqlite3ErrName(rc))
51510
+ );
5108351511
return rc;
5108451512
}
5108551513
5108651514
/*
5108751515
** Implement a memory barrier or memory fence on shared memory.
@@ -51139,17 +51567,19 @@
5113951567
}
5114051568
pShmNode = pShm->pShmNode;
5114151569
5114251570
sqlite3_mutex_enter(pShmNode->mutex);
5114351571
if( pShmNode->isUnlocked ){
51144
- rc = winLockSharedMemory(pShmNode);
51572
+ /* Take the DMS lock. */
51573
+ assert( pShmNode->nRegion==0 );
51574
+ rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd));
5114551575
if( rc!=SQLITE_OK ) goto shmpage_out;
51146
- pShmNode->isUnlocked = 0;
5114751576
}
51577
+
5114851578
assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
51149
-
5115051579
if( pShmNode->nRegion<=iRegion ){
51580
+ HANDLE hShared = pShmNode->hSharedShm;
5115151581
struct ShmRegion *apNew; /* New aRegion[] array */
5115251582
int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
5115351583
sqlite3_int64 sz; /* Current size of wal-index file */
5115451584
5115551585
pShmNode->szRegion = szRegion;
@@ -51156,35 +51586,32 @@
5115651586
5115751587
/* The requested region is not mapped into this processes address space.
5115851588
** Check to see if it has been allocated (i.e. if the wal-index file is
5115951589
** large enough to contain the requested region).
5116051590
*/
51161
- rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
51591
+ rc = winHandleSize(hShared, &sz);
5116251592
if( rc!=SQLITE_OK ){
51163
- rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51164
- "winShmMap1", pDbFd->zPath);
51593
+ rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath);
5116551594
goto shmpage_out;
5116651595
}
5116751596
5116851597
if( sz<nByte ){
5116951598
/* The requested memory region does not exist. If isWrite is set to
5117051599
** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
5117151600
**
5117251601
** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51173
- ** the requested memory region.
51174
- */
51602
+ ** the requested memory region. */
5117551603
if( !isWrite ) goto shmpage_out;
51176
- rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
51604
+ rc = winHandleTruncate(hShared, nByte);
5117751605
if( rc!=SQLITE_OK ){
51178
- rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51179
- "winShmMap2", pDbFd->zPath);
51606
+ rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath);
5118051607
goto shmpage_out;
5118151608
}
5118251609
}
5118351610
5118451611
/* Map the requested memory region into this processes address space. */
51185
- apNew = (struct ShmRegion *)sqlite3_realloc64(
51612
+ apNew = (struct ShmRegion*)sqlite3_realloc64(
5118651613
pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
5118751614
);
5118851615
if( !apNew ){
5118951616
rc = SQLITE_IOERR_NOMEM_BKPT;
5119051617
goto shmpage_out;
@@ -51199,22 +51626,17 @@
5119951626
while( pShmNode->nRegion<=iRegion ){
5120051627
HANDLE hMap = NULL; /* file-mapping handle */
5120151628
void *pMap = 0; /* Mapped memory region */
5120251629
5120351630
#if SQLITE_OS_WINRT
51204
- hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
51205
- NULL, protect, nByte, NULL
51206
- );
51631
+ hMap = osCreateFileMappingFromApp(hShared, NULL, protect, nByte, NULL);
5120751632
#elif defined(SQLITE_WIN32_HAS_WIDE)
51208
- hMap = osCreateFileMappingW(pShmNode->hFile.h,
51209
- NULL, protect, 0, nByte, NULL
51210
- );
51633
+ hMap = osCreateFileMappingW(hShared, NULL, protect, 0, nByte, NULL);
5121151634
#elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51212
- hMap = osCreateFileMappingA(pShmNode->hFile.h,
51213
- NULL, protect, 0, nByte, NULL
51214
- );
51635
+ hMap = osCreateFileMappingA(hShared, NULL, protect, 0, nByte, NULL);
5121551636
#endif
51637
+
5121651638
OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
5121751639
osGetCurrentProcessId(), pShmNode->nRegion, nByte,
5121851640
hMap ? "ok" : "failed"));
5121951641
if( hMap ){
5122051642
int iOffset = pShmNode->nRegion*szRegion;
@@ -51253,11 +51675,13 @@
5125351675
char *p = (char *)pShmNode->aRegion[iRegion].pMap;
5125451676
*pp = (void *)&p[iOffsetShift];
5125551677
}else{
5125651678
*pp = 0;
5125751679
}
51258
- if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
51680
+ if( pShmNode->isReadonly && rc==SQLITE_OK ){
51681
+ rc = SQLITE_READONLY;
51682
+ }
5125951683
sqlite3_mutex_leave(pShmNode->mutex);
5126051684
return rc;
5126151685
}
5126251686
5126351687
#else
@@ -51594,30 +52018,10 @@
5159452018
/* caller will handle out of memory */
5159552019
return zConverted;
5159652020
}
5159752021
#endif
5159852022
51599
-/*
51600
-** Convert a UTF-8 filename into whatever form the underlying
51601
-** operating system wants filenames in. Space to hold the result
51602
-** is obtained from malloc and must be freed by the calling
51603
-** function.
51604
-*/
51605
-static void *winConvertFromUtf8Filename(const char *zFilename){
51606
- void *zConverted = 0;
51607
- if( osIsNT() ){
51608
- zConverted = winUtf8ToUnicode(zFilename);
51609
- }
51610
-#ifdef SQLITE_WIN32_HAS_ANSI
51611
- else{
51612
- zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51613
- }
51614
-#endif
51615
- /* caller will handle out of memory */
51616
- return zConverted;
51617
-}
51618
-
5161952023
/*
5162052024
** This function returns non-zero if the specified UTF-8 string buffer
5162152025
** ends with a directory separator character or one was successfully
5162252026
** added to it.
5162352027
*/
@@ -53067,11 +53471,11 @@
5306753471
};
5306853472
#endif
5306953473
5307053474
/* Double-check that the aSyscall[] array has been constructed
5307153475
** correctly. See ticket [bb3a86e890c8e96ab] */
53072
- assert( ArraySize(aSyscall)==80 );
53476
+ assert( ArraySize(aSyscall)==82 );
5307353477
5307453478
/* get memory map allocation granularity */
5307553479
memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
5307653480
#if SQLITE_OS_WINRT
5307753481
osGetNativeSystemInfo(&winSysInfo);
@@ -54125,11 +54529,11 @@
5412554529
** Empirical testing showed that the *37 multiplier
5412654530
** (an arbitrary prime)in the hash function provided
5412754531
** no fewer collisions than the no-op *1. */
5412854532
#define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
5412954533
54130
-#define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
54534
+#define BITVEC_NPTR ((u32)(BITVEC_USIZE/sizeof(Bitvec *)))
5413154535
5413254536
5413354537
/*
5413454538
** A bitmap is an instance of the following structure.
5413554539
**
@@ -54308,11 +54712,11 @@
5430854712
if (!p) {
5430954713
return;
5431054714
}
5431154715
}
5431254716
if( p->iSize<=BITVEC_NBIT ){
54313
- p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
54717
+ p->u.aBitmap[i/BITVEC_SZELEM] &= ~(BITVEC_TELEM)(1<<(i&(BITVEC_SZELEM-1)));
5431454718
}else{
5431554719
unsigned int j;
5431654720
u32 *aiValues = pBuf;
5431754721
memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
5431854722
memset(p->u.aHash, 0, sizeof(p->u.aHash));
@@ -54359,11 +54763,11 @@
5435954763
** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
5436054764
** Then the following macros can be used to set, clear, or test
5436154765
** individual bits within V.
5436254766
*/
5436354767
#define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
54364
-#define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
54768
+#define CLEARBIT(V,I) V[I>>3] &= ~(BITVEC_TELEM)(1<<(I&7))
5436554769
#define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
5436654770
5436754771
/*
5436854772
** This routine runs an extensive test of the Bitvec code.
5436954773
**
@@ -59211,10 +59615,19 @@
5921159615
pPager->pInJournal = 0;
5921259616
releaseAllSavepoints(pPager);
5921359617
5921459618
if( pagerUseWal(pPager) ){
5921559619
assert( !isOpen(pPager->jfd) );
59620
+ if( pPager->eState==PAGER_ERROR ){
59621
+ /* If an IO error occurs in wal.c while attempting to wrap the wal file,
59622
+ ** then the Wal object may be holding a write-lock but no read-lock.
59623
+ ** This call ensures that the write-lock is dropped as well. We cannot
59624
+ ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once
59625
+ ** did, because this would break "BEGIN EXCLUSIVE" handling for
59626
+ ** SQLITE_ENABLE_SETLK_TIMEOUT builds. */
59627
+ sqlite3WalEndWriteTransaction(pPager->pWal);
59628
+ }
5921659629
sqlite3WalEndReadTransaction(pPager->pWal);
5921759630
pPager->eState = PAGER_OPEN;
5921859631
}else if( !pPager->exclusiveMode ){
5921959632
int rc; /* Error code returned by pagerUnlockDb() */
5922059633
int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
@@ -65669,10 +66082,15 @@
6566966082
)
6567066083
6567166084
/*
6567266085
** An open write-ahead log file is represented by an instance of the
6567366086
** following object.
66087
+**
66088
+** writeLock:
66089
+** This is usually set to 1 whenever the WRITER lock is held. However,
66090
+** if it is set to 2, then the WRITER lock is held but must be released
66091
+** by walHandleException() if a SEH exception is thrown.
6567466092
*/
6567566093
struct Wal {
6567666094
sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
6567766095
sqlite3_file *pDbFd; /* File handle for the database file */
6567866096
sqlite3_file *pWalFd; /* File handle for WAL file */
@@ -67194,11 +67612,11 @@
6719467612
** or 0 otherwise.
6719567613
*/
6719667614
static int walEnableBlocking(Wal *pWal){
6719767615
int res = 0;
6719867616
if( pWal->db ){
67199
- int tmout = pWal->db->busyTimeout;
67617
+ int tmout = pWal->db->setlkTimeout;
6720067618
if( tmout ){
6720167619
res = walEnableBlockingMs(pWal, tmout);
6720267620
}
6720367621
}
6720467622
return res;
@@ -67580,11 +67998,13 @@
6758067998
static int walHandleException(Wal *pWal){
6758167999
if( pWal->exclusiveMode==0 ){
6758268000
static const int S = 1;
6758368001
static const int E = (1<<SQLITE_SHM_NLOCK);
6758468002
int ii;
67585
- u32 mUnlock = pWal->lockMask & ~(
68003
+ u32 mUnlock;
68004
+ if( pWal->writeLock==2 ) pWal->writeLock = 0;
68005
+ mUnlock = pWal->lockMask & ~(
6758668006
(pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
6758768007
| (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
6758868008
| (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
6758968009
);
6759068010
for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
@@ -67852,11 +68272,16 @@
6785268272
}else{
6785368273
int bWriteLock = pWal->writeLock;
6785468274
if( bWriteLock
6785568275
|| SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
6785668276
){
67857
- pWal->writeLock = 1;
68277
+ /* If the write-lock was just obtained, set writeLock to 2 instead of
68278
+ ** the usual 1. This causes walIndexPage() to behave as if the
68279
+ ** write-lock were held (so that it allocates new pages as required),
68280
+ ** and walHandleException() to unlock the write-lock if a SEH exception
68281
+ ** is thrown. */
68282
+ if( !bWriteLock ) pWal->writeLock = 2;
6785868283
if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
6785968284
badHdr = walIndexTryHdr(pWal, pChanged);
6786068285
if( badHdr ){
6786168286
/* If the wal-index header is still malformed even while holding
6786268287
** a WRITE lock, it can only mean that the header is corrupted and
@@ -68637,12 +69062,15 @@
6863769062
/*
6863869063
** Finish with a read transaction. All this does is release the
6863969064
** read-lock.
6864069065
*/
6864169066
SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
68642
- sqlite3WalEndWriteTransaction(pWal);
69067
+#ifndef SQLITE_ENABLE_SETLK_TIMEOUT
69068
+ assert( pWal->writeLock==0 || pWal->readLock<0 );
69069
+#endif
6864369070
if( pWal->readLock>=0 ){
69071
+ sqlite3WalEndWriteTransaction(pWal);
6864469072
walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
6864569073
pWal->readLock = -1;
6864669074
}
6864769075
}
6864869076
@@ -68831,11 +69259,11 @@
6883169259
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
6883269260
/* If the write-lock is already held, then it was obtained before the
6883369261
** read-transaction was even opened, making this call a no-op.
6883469262
** Return early. */
6883569263
if( pWal->writeLock ){
68836
- assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
69264
+ assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) );
6883769265
return SQLITE_OK;
6883869266
}
6883969267
#endif
6884069268
6884169269
/* Cannot start a write transaction without first holding a read
@@ -70280,10 +70708,16 @@
7028070708
** If a tree that appears to be taller than this is encountered, it is
7028170709
** assumed that the database is corrupt.
7028270710
*/
7028370711
#define BTCURSOR_MAX_DEPTH 20
7028470712
70713
+/*
70714
+** Maximum amount of storage local to a database page, regardless of
70715
+** page size.
70716
+*/
70717
+#define BT_MAX_LOCAL 65501 /* 65536 - 35 */
70718
+
7028570719
/*
7028670720
** A cursor is a pointer to a particular entry within a particular
7028770721
** b-tree within a database file.
7028870722
**
7028970723
** The entry is identified by its MemPage and the index in
@@ -70688,11 +71122,11 @@
7068871122
** two or more btrees in common both try to lock all their btrees
7068971123
** at the same instant.
7069071124
*/
7069171125
static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
7069271126
int i;
70693
- int skipOk = 1;
71127
+ u8 skipOk = 1;
7069471128
Btree *p;
7069571129
assert( sqlite3_mutex_held(db->mutex) );
7069671130
for(i=0; i<db->nDb; i++){
7069771131
p = db->aDb[i].pBt;
7069871132
if( p && p->sharable ){
@@ -71834,11 +72268,11 @@
7183472268
/*
7183572269
** Provide flag hints to the cursor.
7183672270
*/
7183772271
SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
7183872272
assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
71839
- pCur->hints = x;
72273
+ pCur->hints = (u8)x;
7184072274
}
7184172275
7184272276
7184372277
#ifndef SQLITE_OMIT_AUTOVACUUM
7184472278
/*
@@ -72028,18 +72462,19 @@
7202872462
** page pPage, return the number of bytes of payload stored locally.
7202972463
*/
7203072464
static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
7203172465
int maxLocal; /* Maximum amount of payload held locally */
7203272466
maxLocal = pPage->maxLocal;
72467
+ assert( nPayload>=0 );
7203372468
if( nPayload<=maxLocal ){
72034
- return nPayload;
72469
+ return (int)nPayload;
7203572470
}else{
7203672471
int minLocal; /* Minimum amount of payload held locally */
7203772472
int surplus; /* Overflow payload available for local storage */
7203872473
minLocal = pPage->minLocal;
72039
- surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
72040
- return ( surplus <= maxLocal ) ? surplus : minLocal;
72474
+ surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
72475
+ return (surplus <= maxLocal) ? surplus : minLocal;
7204172476
}
7204272477
}
7204372478
7204472479
/*
7204572480
** The following routines are implementations of the MemPage.xParseCell()
@@ -72145,15 +72580,17 @@
7214572580
pInfo->nKey = *(i64*)&iKey;
7214672581
pInfo->nPayload = nPayload;
7214772582
pInfo->pPayload = pIter;
7214872583
testcase( nPayload==pPage->maxLocal );
7214972584
testcase( nPayload==(u32)pPage->maxLocal+1 );
72585
+ assert( nPayload>=0 );
72586
+ assert( pPage->maxLocal <= BT_MAX_LOCAL );
7215072587
if( nPayload<=pPage->maxLocal ){
7215172588
/* This is the (easy) common case where the entire payload fits
7215272589
** on the local page. No overflow is required.
7215372590
*/
72154
- pInfo->nSize = nPayload + (u16)(pIter - pCell);
72591
+ pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
7215572592
if( pInfo->nSize<4 ) pInfo->nSize = 4;
7215672593
pInfo->nLocal = (u16)nPayload;
7215772594
}else{
7215872595
btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
7215972596
}
@@ -72182,15 +72619,17 @@
7218272619
pInfo->nKey = nPayload;
7218372620
pInfo->nPayload = nPayload;
7218472621
pInfo->pPayload = pIter;
7218572622
testcase( nPayload==pPage->maxLocal );
7218672623
testcase( nPayload==(u32)pPage->maxLocal+1 );
72624
+ assert( nPayload>=0 );
72625
+ assert( pPage->maxLocal <= BT_MAX_LOCAL );
7218772626
if( nPayload<=pPage->maxLocal ){
7218872627
/* This is the (easy) common case where the entire payload fits
7218972628
** on the local page. No overflow is required.
7219072629
*/
72191
- pInfo->nSize = nPayload + (u16)(pIter - pCell);
72630
+ pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
7219272631
if( pInfo->nSize<4 ) pInfo->nSize = 4;
7219372632
pInfo->nLocal = (u16)nPayload;
7219472633
}else{
7219572634
btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
7219672635
}
@@ -72725,18 +73164,18 @@
7272573164
** that routine will not detect overlap between cells or freeblocks. Nor
7272673165
** does it detect cells or freeblocks that encroach into the reserved bytes
7272773166
** at the end of the page. So do additional corruption checks inside this
7272873167
** routine and return SQLITE_CORRUPT if any problems are found.
7272973168
*/
72730
-static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72731
- u16 iPtr; /* Address of ptr to next freeblock */
72732
- u16 iFreeBlk; /* Address of the next freeblock */
73169
+static int freeSpace(MemPage *pPage, int iStart, int iSize){
73170
+ int iPtr; /* Address of ptr to next freeblock */
73171
+ int iFreeBlk; /* Address of the next freeblock */
7273373172
u8 hdr; /* Page header size. 0 or 100 */
72734
- u8 nFrag = 0; /* Reduction in fragmentation */
72735
- u16 iOrigSize = iSize; /* Original value of iSize */
72736
- u16 x; /* Offset to cell content area */
72737
- u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
73173
+ int nFrag = 0; /* Reduction in fragmentation */
73174
+ int iOrigSize = iSize; /* Original value of iSize */
73175
+ int x; /* Offset to cell content area */
73176
+ int iEnd = iStart + iSize; /* First byte past the iStart buffer */
7273873177
unsigned char *data = pPage->aData; /* Page content */
7273973178
u8 *pTmp; /* Temporary ptr into data[] */
7274073179
7274173180
assert( pPage->pBt!=0 );
7274273181
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -72759,11 +73198,11 @@
7275973198
if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
7276073199
return SQLITE_CORRUPT_PAGE(pPage);
7276173200
}
7276273201
iPtr = iFreeBlk;
7276373202
}
72764
- if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73203
+ if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
7276573204
return SQLITE_CORRUPT_PAGE(pPage);
7276673205
}
7276773206
assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
7276873207
7276973208
/* At this point:
@@ -72774,11 +73213,11 @@
7277473213
*/
7277573214
if( iFreeBlk && iEnd+3>=iFreeBlk ){
7277673215
nFrag = iFreeBlk - iEnd;
7277773216
if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
7277873217
iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
72779
- if( iEnd > pPage->pBt->usableSize ){
73218
+ if( iEnd > (int)pPage->pBt->usableSize ){
7278073219
return SQLITE_CORRUPT_PAGE(pPage);
7278173220
}
7278273221
iSize = iEnd - iStart;
7278373222
iFreeBlk = get2byte(&data[iFreeBlk]);
7278473223
}
@@ -72795,11 +73234,11 @@
7279573234
iSize = iEnd - iPtr;
7279673235
iStart = iPtr;
7279773236
}
7279873237
}
7279973238
if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
72800
- data[hdr+7] -= nFrag;
73239
+ data[hdr+7] -= (u8)nFrag;
7280173240
}
7280273241
pTmp = &data[hdr+5];
7280373242
x = get2byte(pTmp);
7280473243
if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
7280573244
/* Overwrite deleted information with zeros when the secure_delete
@@ -72816,11 +73255,12 @@
7281673255
put2byte(&data[hdr+5], iEnd);
7281773256
}else{
7281873257
/* Insert the new freeblock into the freelist */
7281973258
put2byte(&data[iPtr], iStart);
7282073259
put2byte(&data[iStart], iFreeBlk);
72821
- put2byte(&data[iStart+2], iSize);
73260
+ assert( iSize>=0 && iSize<=0xffff );
73261
+ put2byte(&data[iStart+2], (u16)iSize);
7282273262
}
7282373263
pPage->nFree += iOrigSize;
7282473264
return SQLITE_OK;
7282573265
}
7282673266
@@ -73042,11 +73482,11 @@
7304273482
return SQLITE_CORRUPT_PAGE(pPage);
7304373483
}
7304473484
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
7304573485
pPage->maskPage = (u16)(pBt->pageSize - 1);
7304673486
pPage->nOverflow = 0;
73047
- pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
73487
+ pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
7304873488
pPage->aCellIdx = data + pPage->childPtrSize + 8;
7304973489
pPage->aDataEnd = pPage->aData + pBt->pageSize;
7305073490
pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
7305173491
/* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
7305273492
** number of cells on the page. */
@@ -73076,12 +73516,12 @@
7307673516
** no entries.
7307773517
*/
7307873518
static void zeroPage(MemPage *pPage, int flags){
7307973519
unsigned char *data = pPage->aData;
7308073520
BtShared *pBt = pPage->pBt;
73081
- u8 hdr = pPage->hdrOffset;
73082
- u16 first;
73521
+ int hdr = pPage->hdrOffset;
73522
+ int first;
7308373523
7308473524
assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
7308573525
assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
7308673526
assert( sqlite3PagerGetData(pPage->pDbPage) == data );
7308773527
assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -73094,11 +73534,11 @@
7309473534
memset(&data[hdr+1], 0, 4);
7309573535
data[hdr+7] = 0;
7309673536
put2byte(&data[hdr+5], pBt->usableSize);
7309773537
pPage->nFree = (u16)(pBt->usableSize - first);
7309873538
decodeFlags(pPage, flags);
73099
- pPage->cellOffset = first;
73539
+ pPage->cellOffset = (u16)first;
7310073540
pPage->aDataEnd = &data[pBt->pageSize];
7310173541
pPage->aCellIdx = &data[first];
7310273542
pPage->aDataOfst = &data[pPage->childPtrSize];
7310373543
pPage->nOverflow = 0;
7310473544
assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
@@ -73880,11 +74320,11 @@
7388074320
int rc = SQLITE_OK;
7388174321
int x;
7388274322
BtShared *pBt = p->pBt;
7388374323
assert( nReserve>=0 && nReserve<=255 );
7388474324
sqlite3BtreeEnter(p);
73885
- pBt->nReserveWanted = nReserve;
74325
+ pBt->nReserveWanted = (u8)nReserve;
7388674326
x = pBt->pageSize - pBt->usableSize;
7388774327
if( nReserve<x ) nReserve = x;
7388874328
if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
7388974329
sqlite3BtreeLeave(p);
7389074330
return SQLITE_READONLY;
@@ -73986,11 +74426,11 @@
7398674426
sqlite3BtreeEnter(p);
7398774427
assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
7398874428
assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
7398974429
if( newFlag>=0 ){
7399074430
p->pBt->btsFlags &= ~BTS_FAST_SECURE;
73991
- p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
74431
+ p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag);
7399274432
}
7399374433
b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
7399474434
sqlite3BtreeLeave(p);
7399574435
return b;
7399674436
}
@@ -78434,11 +78874,12 @@
7843478874
pSrcEnd = pCArray->apEnd[k];
7843578875
}
7843678876
}
7843778877
7843878878
/* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78439
- pPg->nCell = nCell;
78879
+ assert( nCell < 10922 );
78880
+ pPg->nCell = (u16)nCell;
7844078881
pPg->nOverflow = 0;
7844178882
7844278883
put2byte(&aData[hdr+1], 0);
7844378884
put2byte(&aData[hdr+3], pPg->nCell);
7844478885
put2byte(&aData[hdr+5], pData - aData);
@@ -78681,13 +79122,17 @@
7868179122
assert( nCell>=0 );
7868279123
pCellptr = &pPg->aCellIdx[nCell*2];
7868379124
if( pageInsertArray(
7868479125
pPg, pBegin, &pData, pCellptr,
7868579126
iNew+nCell, nNew-nCell, pCArray
78686
- ) ) goto editpage_fail;
79127
+ )
79128
+ ){
79129
+ goto editpage_fail;
79130
+ }
7868779131
78688
- pPg->nCell = nNew;
79132
+ assert( nNew < 10922 );
79133
+ pPg->nCell = (u16)nNew;
7868979134
pPg->nOverflow = 0;
7869079135
7869179136
put2byte(&aData[hdr+3], pPg->nCell);
7869279137
put2byte(&aData[hdr+5], pData - aData);
7869379138
@@ -78992,11 +79437,11 @@
7899279437
int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
7899379438
int usableSpace; /* Bytes in pPage beyond the header */
7899479439
int pageFlags; /* Value of pPage->aData[0] */
7899579440
int iSpace1 = 0; /* First unused byte of aSpace1[] */
7899679441
int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
78997
- int szScratch; /* Size of scratch memory requested */
79442
+ u64 szScratch; /* Size of scratch memory requested */
7899879443
MemPage *apOld[NB]; /* pPage and up to two siblings */
7899979444
MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
7900079445
u8 *pRight; /* Location in parent of right-sibling pointer */
7900179446
u8 *apDiv[NB-1]; /* Divider cells in pParent */
7900279447
int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
@@ -80277,11 +80722,11 @@
8027780722
if( loc==0 ){
8027880723
getCellInfo(pCur);
8027980724
if( pCur->info.nKey==pX->nKey ){
8028080725
BtreePayload x2;
8028180726
x2.pData = pX->pKey;
80282
- x2.nData = pX->nKey;
80727
+ x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff );
8028380728
x2.nZero = 0;
8028480729
return btreeOverwriteCell(pCur, &x2);
8028580730
}
8028680731
}
8028780732
}
@@ -80458,11 +80903,11 @@
8045880903
u32 nIn; /* Size of input buffer aIn[] */
8045980904
u32 nRem; /* Bytes of data still to copy */
8046080905
8046180906
getCellInfo(pSrc);
8046280907
if( pSrc->info.nPayload<0x80 ){
80463
- *(aOut++) = pSrc->info.nPayload;
80908
+ *(aOut++) = (u8)pSrc->info.nPayload;
8046480909
}else{
8046580910
aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
8046680911
}
8046780912
if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
8046880913
nIn = pSrc->info.nLocal;
@@ -80471,11 +80916,11 @@
8047180916
return SQLITE_CORRUPT_PAGE(pSrc->pPage);
8047280917
}
8047380918
nRem = pSrc->info.nPayload;
8047480919
if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
8047580920
memcpy(aOut, aIn, nIn);
80476
- pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
80921
+ pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
8047780922
return SQLITE_OK;
8047880923
}else{
8047980924
int rc = SQLITE_OK;
8048080925
Pager *pSrcPager = pSrc->pBt->pPager;
8048180926
u8 *pPgnoOut = 0;
@@ -80483,11 +80928,11 @@
8048380928
DbPage *pPageIn = 0;
8048480929
MemPage *pPageOut = 0;
8048580930
u32 nOut; /* Size of output buffer aOut[] */
8048680931
8048780932
nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80488
- pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
80933
+ pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
8048980934
if( nOut<pSrc->info.nPayload ){
8049080935
pPgnoOut = &aOut[nOut];
8049180936
pBt->nPreformatSize += 4;
8049280937
}
8049380938
@@ -111531,11 +111976,11 @@
111531111976
pNew->pNext = pNext;
111532111977
pNew->pPrior = 0;
111533111978
pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
111534111979
pNew->iLimit = 0;
111535111980
pNew->iOffset = 0;
111536
- pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
111981
+ pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
111537111982
pNew->addrOpenEphm[0] = -1;
111538111983
pNew->addrOpenEphm[1] = -1;
111539111984
pNew->nSelectRow = p->nSelectRow;
111540111985
pNew->pWith = sqlite3WithDup(db, p->pWith);
111541111986
#ifndef SQLITE_OMIT_WINDOWFUNC
@@ -117481,17 +117926,17 @@
117481117926
pNew->nTabRef = 1;
117482117927
pNew->nCol = pTab->nCol;
117483117928
assert( pNew->nCol>0 );
117484117929
nAlloc = (((pNew->nCol-1)/8)*8)+8;
117485117930
assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
117486
- pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
117931
+ pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*(u32)nAlloc);
117487117932
pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
117488117933
if( !pNew->aCol || !pNew->zName ){
117489117934
assert( db->mallocFailed );
117490117935
goto exit_begin_add_column;
117491117936
}
117492
- memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
117937
+ memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*(size_t)pNew->nCol);
117493117938
for(i=0; i<pNew->nCol; i++){
117494117939
Column *pCol = &pNew->aCol[i];
117495117940
pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
117496117941
pCol->hName = sqlite3StrIHash(pCol->zCnName);
117497117942
}
@@ -118094,11 +118539,17 @@
118094118539
return SQLITE_NOMEM;
118095118540
}
118096118541
if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){
118097118542
return SQLITE_CORRUPT_BKPT;
118098118543
}
118099
- db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
118544
+ if( bTemp ){
118545
+ db->init.iDb = 1;
118546
+ }else{
118547
+ int iDb = sqlite3FindDbName(db, zDb);
118548
+ assert( iDb>=0 && iDb<=0xff );
118549
+ db->init.iDb = (u8)iDb;
118550
+ }
118100118551
p->eParseMode = PARSE_MODE_RENAME;
118101118552
p->db = db;
118102118553
p->nQueryLoop = 1;
118103118554
rc = sqlite3RunParser(p, zSql);
118104118555
if( db->mallocFailed ) rc = SQLITE_NOMEM;
@@ -118161,14 +118612,15 @@
118161118612
return SQLITE_NOMEM;
118162118613
}else{
118163118614
nQuot = sqlite3Strlen30(zQuot)-1;
118164118615
}
118165118616
118166
- assert( nQuot>=nNew );
118167
- zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
118617
+ assert( nQuot>=nNew && nSql>=0 && nNew>=0 );
118618
+ zOut = sqlite3DbMallocZero(db, (u64)(nSql + pRename->nList*nQuot + 1));
118168118619
}else{
118169
- zOut = (char*)sqlite3DbMallocZero(db, (nSql*2+1) * 3);
118620
+ assert( nSql>0 );
118621
+ zOut = (char*)sqlite3DbMallocZero(db, (u64)(nSql*2+1) * 3);
118170118622
if( zOut ){
118171118623
zBuf1 = &zOut[nSql*2+1];
118172118624
zBuf2 = &zOut[nSql*4+2];
118173118625
}
118174118626
}
@@ -118176,20 +118628,21 @@
118176118628
/* At this point pRename->pList contains a list of RenameToken objects
118177118629
** corresponding to all tokens in the input SQL that must be replaced
118178118630
** with the new column name, or with single-quoted versions of themselves.
118179118631
** All that remains is to construct and return the edited SQL string. */
118180118632
if( zOut ){
118181
- int nOut = nSql;
118182
- memcpy(zOut, zSql, nSql);
118633
+ i64 nOut = nSql;
118634
+ assert( nSql>0 );
118635
+ memcpy(zOut, zSql, (size_t)nSql);
118183118636
while( pRename->pList ){
118184118637
int iOff; /* Offset of token to replace in zOut */
118185
- u32 nReplace;
118638
+ i64 nReplace;
118186118639
const char *zReplace;
118187118640
RenameToken *pBest = renameColumnTokenNext(pRename);
118188118641
118189118642
if( zNew ){
118190
- if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
118643
+ if( bQuote==0 && sqlite3IsIdChar(*(u8*)pBest->t.z) ){
118191118644
nReplace = nNew;
118192118645
zReplace = zNew;
118193118646
}else{
118194118647
nReplace = nQuot;
118195118648
zReplace = zQuot;
@@ -118203,18 +118656,19 @@
118203118656
** token. This is so that (SELECT "string"'alias') maps to
118204118657
** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
118205118658
memcpy(zBuf1, pBest->t.z, pBest->t.n);
118206118659
zBuf1[pBest->t.n] = 0;
118207118660
sqlite3Dequote(zBuf1);
118208
- sqlite3_snprintf(nSql*2, zBuf2, "%Q%s", zBuf1,
118661
+ assert( nSql < 0x15555554 /* otherwise malloc would have failed */ );
118662
+ sqlite3_snprintf((int)(nSql*2), zBuf2, "%Q%s", zBuf1,
118209118663
pBest->t.z[pBest->t.n]=='\'' ? " " : ""
118210118664
);
118211118665
zReplace = zBuf2;
118212118666
nReplace = sqlite3Strlen30(zReplace);
118213118667
}
118214118668
118215
- iOff = pBest->t.z - zSql;
118669
+ iOff = (int)(pBest->t.z - zSql);
118216118670
if( pBest->t.n!=nReplace ){
118217118671
memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
118218118672
nOut - (iOff + pBest->t.n)
118219118673
);
118220118674
nOut += nReplace - pBest->t.n;
@@ -118236,15 +118690,16 @@
118236118690
118237118691
/*
118238118692
** Set all pEList->a[].fg.eEName fields in the expression-list to val.
118239118693
*/
118240118694
static void renameSetENames(ExprList *pEList, int val){
118695
+ assert( val==ENAME_NAME || val==ENAME_TAB || val==ENAME_SPAN );
118241118696
if( pEList ){
118242118697
int i;
118243118698
for(i=0; i<pEList->nExpr; i++){
118244118699
assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
118245
- pEList->a[i].fg.eEName = val;
118700
+ pEList->a[i].fg.eEName = val&0x3;
118246118701
}
118247118702
}
118248118703
}
118249118704
118250118705
/*
@@ -118497,11 +118952,11 @@
118497118952
sCtx.pTab = pTab;
118498118953
if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
118499118954
if( sParse.pNewTable ){
118500118955
if( IsView(sParse.pNewTable) ){
118501118956
Select *pSelect = sParse.pNewTable->u.view.pSelect;
118502
- pSelect->selFlags &= ~SF_View;
118957
+ pSelect->selFlags &= ~(u32)SF_View;
118503118958
sParse.rc = SQLITE_OK;
118504118959
sqlite3SelectPrep(&sParse, pSelect, 0);
118505118960
rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118506118961
if( rc==SQLITE_OK ){
118507118962
sqlite3WalkSelect(&sWalker, pSelect);
@@ -118715,11 +119170,11 @@
118715119170
NameContext sNC;
118716119171
memset(&sNC, 0, sizeof(sNC));
118717119172
sNC.pParse = &sParse;
118718119173
118719119174
assert( pSelect->selFlags & SF_View );
118720
- pSelect->selFlags &= ~SF_View;
119175
+ pSelect->selFlags &= ~(u32)SF_View;
118721119176
sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
118722119177
if( sParse.nErr ){
118723119178
rc = sParse.rc;
118724119179
}else{
118725119180
sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
@@ -118888,11 +119343,11 @@
118888119343
sWalker.u.pRename = &sCtx;
118889119344
118890119345
if( sParse.pNewTable ){
118891119346
if( IsView(sParse.pNewTable) ){
118892119347
Select *pSelect = sParse.pNewTable->u.view.pSelect;
118893
- pSelect->selFlags &= ~SF_View;
119348
+ pSelect->selFlags &= ~(u32)SF_View;
118894119349
sParse.rc = SQLITE_OK;
118895119350
sqlite3SelectPrep(&sParse, pSelect, 0);
118896119351
rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118897119352
if( rc==SQLITE_OK ){
118898119353
sqlite3WalkSelect(&sWalker, pSelect);
@@ -118987,11 +119442,11 @@
118987119442
UNUSED_PARAMETER(NotUsed);
118988119443
118989119444
if( zDb && zInput ){
118990119445
int rc;
118991119446
Parse sParse;
118992
- int flags = db->flags;
119447
+ u64 flags = db->flags;
118993119448
if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
118994119449
rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
118995119450
db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
118996119451
if( rc==SQLITE_OK ){
118997119452
if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){
@@ -119710,11 +120165,11 @@
119710120165
}
119711120166
119712120167
p->db = db;
119713120168
p->nEst = sqlite3_value_int64(argv[2]);
119714120169
p->nRow = 0;
119715
- p->nLimit = sqlite3_value_int64(argv[3]);
120170
+ p->nLimit = sqlite3_value_int(argv[3]);
119716120171
p->nCol = nCol;
119717120172
p->nKeyCol = nKeyCol;
119718120173
p->nSkipAhead = 0;
119719120174
p->current.anDLt = (tRowcnt*)&p[1];
119720120175
@@ -121519,10 +121974,17 @@
121519121974
*/
121520121975
if( rc==SQLITE_OK ){
121521121976
sqlite3BtreeEnterAll(db);
121522121977
db->init.iDb = 0;
121523121978
db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
121979
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
121980
+ if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT ){
121981
+ int val = 1;
121982
+ sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt));
121983
+ sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, &val);
121984
+ }
121985
+#endif
121524121986
if( !REOPEN_AS_MEMDB(db) ){
121525121987
rc = sqlite3Init(db, &zErrDyn);
121526121988
}
121527121989
sqlite3BtreeLeaveAll(db);
121528121990
assert( zErrDyn==0 || rc!=SQLITE_OK );
@@ -123240,14 +123702,20 @@
123240123702
** Convert an table column number into a index column number. That is,
123241123703
** for the column iCol in the table (as defined by the CREATE TABLE statement)
123242123704
** find the (first) offset of that column in index pIdx. Or return -1
123243123705
** if column iCol is not used in index pIdx.
123244123706
*/
123245
-SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
123707
+SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index *pIdx, int iCol){
123246123708
int i;
123709
+ i16 iCol16;
123710
+ assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN );
123711
+ assert( pIdx->nColumn<=SQLITE_MAX_COLUMN );
123712
+ iCol16 = iCol;
123247123713
for(i=0; i<pIdx->nColumn; i++){
123248
- if( iCol==pIdx->aiColumn[i] ) return i;
123714
+ if( iCol16==pIdx->aiColumn[i] ){
123715
+ return i;
123716
+ }
123249123717
}
123250123718
return -1;
123251123719
}
123252123720
123253123721
#ifndef SQLITE_OMIT_GENERATED_COLUMNS
@@ -124340,16 +124808,21 @@
124340124808
124341124809
/*
124342124810
** Resize an Index object to hold N columns total. Return SQLITE_OK
124343124811
** on success and SQLITE_NOMEM on an OOM error.
124344124812
*/
124345
-static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
124813
+static int resizeIndexObject(Parse *pParse, Index *pIdx, int N){
124346124814
char *zExtra;
124347
- int nByte;
124815
+ u64 nByte;
124816
+ sqlite3 *db;
124348124817
if( pIdx->nColumn>=N ) return SQLITE_OK;
124818
+ db = pParse->db;
124819
+ assert( N>0 );
124820
+ assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ );
124821
+ testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] );
124349124822
assert( pIdx->isResized==0 );
124350
- nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
124823
+ nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N;
124351124824
zExtra = sqlite3DbMallocZero(db, nByte);
124352124825
if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
124353124826
memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
124354124827
pIdx->azColl = (const char**)zExtra;
124355124828
zExtra += sizeof(char*)*N;
@@ -124359,11 +124832,11 @@
124359124832
memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
124360124833
pIdx->aiColumn = (i16*)zExtra;
124361124834
zExtra += sizeof(i16)*N;
124362124835
memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
124363124836
pIdx->aSortOrder = (u8*)zExtra;
124364
- pIdx->nColumn = N;
124837
+ pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */
124365124838
pIdx->isResized = 1;
124366124839
return SQLITE_OK;
124367124840
}
124368124841
124369124842
/*
@@ -124613,11 +125086,11 @@
124613125086
if( n==0 ){
124614125087
/* This index is a superset of the primary key */
124615125088
pIdx->nColumn = pIdx->nKeyCol;
124616125089
continue;
124617125090
}
124618
- if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
125091
+ if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return;
124619125092
for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
124620125093
if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
124621125094
testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
124622125095
pIdx->aiColumn[j] = pPk->aiColumn[i];
124623125096
pIdx->azColl[j] = pPk->azColl[i];
@@ -124637,11 +125110,11 @@
124637125110
nExtra = 0;
124638125111
for(i=0; i<pTab->nCol; i++){
124639125112
if( !hasColumn(pPk->aiColumn, nPk, i)
124640125113
&& (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
124641125114
}
124642
- if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
125115
+ if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return;
124643125116
for(i=0, j=nPk; i<pTab->nCol; i++){
124644125117
if( !hasColumn(pPk->aiColumn, j, i)
124645125118
&& (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
124646125119
){
124647125120
assert( j<pPk->nColumn );
@@ -126021,17 +126494,18 @@
126021126494
** of 8-byte aligned space after the Index object and return a
126022126495
** pointer to this extra space in *ppExtra.
126023126496
*/
126024126497
SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
126025126498
sqlite3 *db, /* Database connection */
126026
- i16 nCol, /* Total number of columns in the index */
126499
+ int nCol, /* Total number of columns in the index */
126027126500
int nExtra, /* Number of bytes of extra space to alloc */
126028126501
char **ppExtra /* Pointer to the "extra" space */
126029126502
){
126030126503
Index *p; /* Allocated index object */
126031126504
i64 nByte; /* Bytes of space for Index object + arrays */
126032126505
126506
+ assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] );
126033126507
nByte = ROUND8(sizeof(Index)) + /* Index structure */
126034126508
ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
126035126509
ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
126036126510
sizeof(i16)*nCol + /* Index.aiColumn */
126037126511
sizeof(u8)*nCol); /* Index.aSortOrder */
@@ -126040,12 +126514,13 @@
126040126514
char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
126041126515
p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
126042126516
p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
126043126517
p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
126044126518
p->aSortOrder = (u8*)pExtra;
126045
- p->nColumn = nCol;
126046
- p->nKeyCol = nCol - 1;
126519
+ assert( nCol>0 );
126520
+ p->nColumn = (u16)nCol;
126521
+ p->nKeyCol = (u16)(nCol - 1);
126047126522
*ppExtra = ((char*)p) + nByte;
126048126523
}
126049126524
return p;
126050126525
}
126051126526
@@ -130629,11 +131104,11 @@
130629131104
130630131105
/*
130631131106
** Append to pStr text that is the SQL literal representation of the
130632131107
** value contained in pValue.
130633131108
*/
130634
-SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
131109
+SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){
130635131110
/* As currently implemented, the string must be initially empty.
130636131111
** we might relax this requirement in the future, but that will
130637131112
** require enhancements to the implementation. */
130638131113
assert( pStr!=0 && pStr->nChar==0 );
130639131114
@@ -130677,20 +131152,119 @@
130677131152
}
130678131153
break;
130679131154
}
130680131155
case SQLITE_TEXT: {
130681131156
const unsigned char *zArg = sqlite3_value_text(pValue);
130682
- sqlite3_str_appendf(pStr, "%Q", zArg);
131157
+ sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg);
130683131158
break;
130684131159
}
130685131160
default: {
130686131161
assert( sqlite3_value_type(pValue)==SQLITE_NULL );
130687131162
sqlite3_str_append(pStr, "NULL", 4);
130688131163
break;
130689131164
}
130690131165
}
130691131166
}
131167
+
131168
+/*
131169
+** Return true if z[] begins with N hexadecimal digits, and write
131170
+** a decoding of those digits into *pVal. Or return false if any
131171
+** one of the first N characters in z[] is not a hexadecimal digit.
131172
+*/
131173
+static int isNHex(const char *z, int N, u32 *pVal){
131174
+ int i;
131175
+ int v = 0;
131176
+ for(i=0; i<N; i++){
131177
+ if( !sqlite3Isxdigit(z[i]) ) return 0;
131178
+ v = (v<<4) + sqlite3HexToInt(z[i]);
131179
+ }
131180
+ *pVal = v;
131181
+ return 1;
131182
+}
131183
+
131184
+/*
131185
+** Implementation of the UNISTR() function.
131186
+**
131187
+** This is intended to be a work-alike of the UNISTR() function in
131188
+** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 -
131189
+** scraped on 2025-02-22):
131190
+**
131191
+** Evaluate escaped Unicode characters in the argument. Unicode
131192
+** characters can be specified as \XXXX (4 hexadecimal digits),
131193
+** \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits),
131194
+** or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash,
131195
+** write two backslashes. All other characters are taken literally.
131196
+*/
131197
+static void unistrFunc(
131198
+ sqlite3_context *context,
131199
+ int argc,
131200
+ sqlite3_value **argv
131201
+){
131202
+ char *zOut;
131203
+ const char *zIn;
131204
+ int nIn;
131205
+ int i, j, n;
131206
+ u32 v;
131207
+
131208
+ assert( argc==1 );
131209
+ UNUSED_PARAMETER( argc );
131210
+ zIn = (const char*)sqlite3_value_text(argv[0]);
131211
+ if( zIn==0 ) return;
131212
+ nIn = sqlite3_value_bytes(argv[0]);
131213
+ zOut = sqlite3_malloc64(nIn+1);
131214
+ if( zOut==0 ){
131215
+ sqlite3_result_error_nomem(context);
131216
+ return;
131217
+ }
131218
+ i = j = 0;
131219
+ while( i<nIn ){
131220
+ char *z = strchr(&zIn[i],'\\');
131221
+ if( z==0 ){
131222
+ n = nIn - i;
131223
+ memmove(&zOut[j], &zIn[i], n);
131224
+ j += n;
131225
+ break;
131226
+ }
131227
+ n = z - &zIn[i];
131228
+ if( n>0 ){
131229
+ memmove(&zOut[j], &zIn[i], n);
131230
+ j += n;
131231
+ i += n;
131232
+ }
131233
+ if( zIn[i+1]=='\\' ){
131234
+ i += 2;
131235
+ zOut[j++] = '\\';
131236
+ }else if( sqlite3Isxdigit(zIn[i+1]) ){
131237
+ if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error;
131238
+ i += 5;
131239
+ j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131240
+ }else if( zIn[i+1]=='+' ){
131241
+ if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error;
131242
+ i += 8;
131243
+ j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131244
+ }else if( zIn[i+1]=='u' ){
131245
+ if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error;
131246
+ i += 6;
131247
+ j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131248
+ }else if( zIn[i+1]=='U' ){
131249
+ if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error;
131250
+ i += 10;
131251
+ j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131252
+ }else{
131253
+ goto unistr_error;
131254
+ }
131255
+ }
131256
+ zOut[j] = 0;
131257
+ sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8);
131258
+ return;
131259
+
131260
+unistr_error:
131261
+ sqlite3_free(zOut);
131262
+ sqlite3_result_error(context, "invalid Unicode escape", -1);
131263
+ return;
131264
+}
131265
+
130692131266
130693131267
/*
130694131268
** Implementation of the QUOTE() function.
130695131269
**
130696131270
** The quote(X) function returns the text of an SQL literal which is the
@@ -130697,18 +131271,22 @@
130697131271
** value of its argument suitable for inclusion into an SQL statement.
130698131272
** Strings are surrounded by single-quotes with escapes on interior quotes
130699131273
** as needed. BLOBs are encoded as hexadecimal literals. Strings with
130700131274
** embedded NUL characters cannot be represented as string literals in SQL
130701131275
** and hence the returned string literal is truncated prior to the first NUL.
131276
+**
131277
+** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
131278
+** implemented instead. The difference is that UNISTR_QUOTE() uses the
131279
+** UNISTR() function to escape control characters.
130702131280
*/
130703131281
static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130704131282
sqlite3_str str;
130705131283
sqlite3 *db = sqlite3_context_db_handle(context);
130706131284
assert( argc==1 );
130707131285
UNUSED_PARAMETER(argc);
130708131286
sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
130709
- sqlite3QuoteValue(&str,argv[0]);
131287
+ sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context)));
130710131288
sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
130711131289
SQLITE_DYNAMIC);
130712131290
if( str.accError!=SQLITE_OK ){
130713131291
sqlite3_result_null(context);
130714131292
sqlite3_result_error_code(context, str.accError);
@@ -132275,11 +132853,13 @@
132275132853
VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
132276132854
FUNCTION(nullif, 2, 0, 1, nullifFunc ),
132277132855
DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
132278132856
DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
132279132857
FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
132858
+ FUNCTION(unistr, 1, 0, 0, unistrFunc ),
132280132859
FUNCTION(quote, 1, 0, 0, quoteFunc ),
132860
+ FUNCTION(unistr_quote, 1, 1, 0, quoteFunc ),
132281132861
VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
132282132862
VFUNCTION(changes, 0, 0, 0, changes ),
132283132863
VFUNCTION(total_changes, 0, 0, 0, total_changes ),
132284132864
FUNCTION(replace, 3, 0, 0, replaceFunc ),
132285132865
FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
@@ -134562,11 +135142,11 @@
134562135142
}else if( pLeft->pPrior ){
134563135143
/* In this case set the SF_MultiValue flag only if it was set on pLeft */
134564135144
f = (f & pLeft->selFlags);
134565135145
}
134566135146
pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
134567
- pLeft->selFlags &= ~SF_MultiValue;
135147
+ pLeft->selFlags &= ~(u32)SF_MultiValue;
134568135148
if( pSelect ){
134569135149
pSelect->op = TK_ALL;
134570135150
pSelect->pPrior = pLeft;
134571135151
pLeft = pSelect;
134572135152
}
@@ -149534,11 +150114,11 @@
149534150114
p->pNext = 0;
149535150115
p->pWith = 0;
149536150116
#ifndef SQLITE_OMIT_WINDOWFUNC
149537150117
p->pWinDefn = 0;
149538150118
#endif
149539
- p->selFlags &= ~SF_Compound;
150119
+ p->selFlags &= ~(u32)SF_Compound;
149540150120
assert( (p->selFlags & SF_Converted)==0 );
149541150121
p->selFlags |= SF_Converted;
149542150122
assert( pNew->pPrior!=0 );
149543150123
pNew->pPrior->pNext = pNew;
149544150124
pNew->pLimit = 0;
@@ -151140,11 +151720,11 @@
151140151720
Expr *pTerm;
151141151721
pPrior = pSub->pPrior;
151142151722
pSub->pPrior = 0;
151143151723
pSub->pNext = 0;
151144151724
pSub->selFlags |= SF_Aggregate;
151145
- pSub->selFlags &= ~SF_Compound;
151725
+ pSub->selFlags &= ~(u32)SF_Compound;
151146151726
pSub->nSelectRow = 0;
151147151727
sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
151148151728
pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
151149151729
pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
151150151730
pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
@@ -151155,11 +151735,11 @@
151155151735
pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
151156151736
}
151157151737
pSub = pPrior;
151158151738
}
151159151739
p->pEList->a[0].pExpr = pExpr;
151160
- p->selFlags &= ~SF_Aggregate;
151740
+ p->selFlags &= ~(u32)SF_Aggregate;
151161151741
151162151742
#if TREETRACE_ENABLED
151163151743
if( sqlite3TreeTrace & 0x200 ){
151164151744
TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
151165151745
sqlite3TreeViewSelect(0, p, 0);
@@ -151362,11 +151942,11 @@
151362151942
sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
151363151943
p->pOrderBy);
151364151944
testcase( pParse->earlyCleanup );
151365151945
p->pOrderBy = 0;
151366151946
}
151367
- p->selFlags &= ~SF_Distinct;
151947
+ p->selFlags &= ~(u32)SF_Distinct;
151368151948
p->selFlags |= SF_NoopOrderBy;
151369151949
}
151370151950
sqlite3SelectPrep(pParse, p, 0);
151371151951
if( pParse->nErr ){
151372151952
goto select_end;
@@ -151401,11 +151981,11 @@
151401151981
151402151982
/* Clear the SF_UFSrcCheck flag. The check has already been performed,
151403151983
** and leaving this flag set can cause errors if a compound sub-query
151404151984
** in p->pSrc is flattened into this query and this function called
151405151985
** again as part of compound SELECT processing. */
151406
- p->selFlags &= ~SF_UFSrcCheck;
151986
+ p->selFlags &= ~(u32)SF_UFSrcCheck;
151407151987
}
151408151988
151409151989
if( pDest->eDest==SRT_Output ){
151410151990
sqlite3GenerateColumnNames(pParse, p);
151411151991
}
@@ -151890,11 +152470,11 @@
151890152470
&& OptimizationEnabled(db, SQLITE_GroupByOrder)
151891152471
#ifndef SQLITE_OMIT_WINDOWFUNC
151892152472
&& p->pWin==0
151893152473
#endif
151894152474
){
151895
- p->selFlags &= ~SF_Distinct;
152475
+ p->selFlags &= ~(u32)SF_Distinct;
151896152476
pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
151897152477
if( pGroupBy ){
151898152478
for(i=0; i<pGroupBy->nExpr; i++){
151899152479
pGroupBy->a[i].u.x.iOrderByCol = i+1;
151900152480
}
@@ -164580,10 +165160,12 @@
164580165160
if( pSrc->colUsed & MASKBIT(BMS-1) ){
164581165161
nKeyCol += pTable->nCol - BMS + 1;
164582165162
}
164583165163
164584165164
/* Construct the Index object to describe this index */
165165
+ assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) );
165166
+ /* ^-- This guarantees that the number of index columns will fit in the u16 */
164585165167
pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable),
164586165168
0, &zNotUsed);
164587165169
if( pIdx==0 ) goto end_auto_index_create;
164588165170
pLoop->u.btree.pIndex = pIdx;
164589165171
pIdx->zName = "auto-index";
@@ -172141,11 +172723,11 @@
172141172723
172142172724
p->pSrc = 0;
172143172725
p->pWhere = 0;
172144172726
p->pGroupBy = 0;
172145172727
p->pHaving = 0;
172146
- p->selFlags &= ~SF_Aggregate;
172728
+ p->selFlags &= ~(u32)SF_Aggregate;
172147172729
p->selFlags |= SF_WinRewrite;
172148172730
172149172731
/* Create the ORDER BY clause for the sub-select. This is the concatenation
172150172732
** of the window PARTITION and ORDER BY clauses. Then, if this makes it
172151172733
** redundant, remove the ORDER BY from the parent SELECT. */
@@ -178280,12 +178862,12 @@
178280178862
pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
178281178863
}
178282178864
if( pRhs ){
178283178865
pRhs->op = (u8)yymsp[-1].minor.yy502;
178284178866
pRhs->pPrior = pLhs;
178285
- if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
178286
- pRhs->selFlags &= ~SF_MultiValue;
178867
+ if( ALWAYS(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue;
178868
+ pRhs->selFlags &= ~(u32)SF_MultiValue;
178287178869
if( yymsp[-1].minor.yy502!=TK_ALL ) pParse->hasCompound = 1;
178288178870
}else{
178289178871
sqlite3SelectDelete(pParse->db, pLhs);
178290178872
}
178291178873
yymsp[-2].minor.yy637 = pRhs;
@@ -183396,10 +183978,13 @@
183396183978
sqlite3_mutex_enter(db->mutex);
183397183979
db->busyHandler.xBusyHandler = xBusy;
183398183980
db->busyHandler.pBusyArg = pArg;
183399183981
db->busyHandler.nBusy = 0;
183400183982
db->busyTimeout = 0;
183983
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
183984
+ db->setlkTimeout = 0;
183985
+#endif
183401183986
sqlite3_mutex_leave(db->mutex);
183402183987
return SQLITE_OK;
183403183988
}
183404183989
183405183990
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -183445,15 +184030,46 @@
183445184030
#endif
183446184031
if( ms>0 ){
183447184032
sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
183448184033
(void*)db);
183449184034
db->busyTimeout = ms;
184035
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184036
+ db->setlkTimeout = ms;
184037
+#endif
183450184038
}else{
183451184039
sqlite3_busy_handler(db, 0, 0);
183452184040
}
183453184041
return SQLITE_OK;
183454184042
}
184043
+
184044
+/*
184045
+** Set the setlk timeout value.
184046
+*/
184047
+SQLITE_API int sqlite3_setlk_timeout(sqlite3 *db, int ms, int flags){
184048
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184049
+ int iDb;
184050
+ int bBOC = ((flags & SQLITE_SETLK_BLOCK_ON_CONNECT) ? 1 : 0);
184051
+#endif
184052
+#ifdef SQLITE_ENABLE_API_ARMOR
184053
+ if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
184054
+#endif
184055
+ if( ms<-1 ) return SQLITE_RANGE;
184056
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184057
+ db->setlkTimeout = ms;
184058
+ db->setlkFlags = flags;
184059
+ sqlite3BtreeEnterAll(db);
184060
+ for(iDb=0; iDb<db->nDb; iDb++){
184061
+ Btree *pBt = db->aDb[iDb].pBt;
184062
+ if( pBt ){
184063
+ sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pBt));
184064
+ sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, (void*)&bBOC);
184065
+ }
184066
+ }
184067
+ sqlite3BtreeLeaveAll(db);
184068
+#endif
184069
+ return SQLITE_OK;
184070
+}
183455184071
183456184072
/*
183457184073
** Cause any pending operation to stop at its earliest opportunity.
183458184074
*/
183459184075
SQLITE_API void sqlite3_interrupt(sqlite3 *db){
@@ -255960,11 +256576,11 @@
255960256576
int nArg, /* Number of args */
255961256577
sqlite3_value **apUnused /* Function arguments */
255962256578
){
255963256579
assert( nArg==0 );
255964256580
UNUSED_PARAM2(nArg, apUnused);
255965
- sqlite3_result_text(pCtx, "fts5: 2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc", -1, SQLITE_TRANSIENT);
256581
+ sqlite3_result_text(pCtx, "fts5: 2025-02-25 16:39:51 6f0b6d95db17e69ac7e46a39f52770291ac4cfe43eea09add224946a6e11f04e", -1, SQLITE_TRANSIENT);
255966256582
}
255967256583
255968256584
/*
255969256585
** Implementation of fts5_locale(LOCALE, TEXT) function.
255970256586
**
@@ -256185,12 +256801,12 @@
256185256801
/* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
256186256802
** fts5_test_mi.c is compiled and linked into the executable. And call
256187256803
** its entry point to enable the matchinfo() demo. */
256188256804
#ifdef SQLITE_FTS5_ENABLE_TEST_MI
256189256805
if( rc==SQLITE_OK ){
256190
- extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
256191
- rc = sqlite3Fts5TestRegisterMatchinfo(db);
256806
+ extern int sqlite3Fts5TestRegisterMatchinfoAPI(fts5_api*);
256807
+ rc = sqlite3Fts5TestRegisterMatchinfoAPI(&pGlobal->api);
256192256808
}
256193256809
#endif
256194256810
256195256811
return rc;
256196256812
}
256197256813
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** 57caa3136d1bfca06e4f2285734a4977b8d3 with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -465,11 +465,11 @@
465 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
466 ** [sqlite_version()] and [sqlite_source_id()].
467 */
468 #define SQLITE_VERSION "3.50.0"
469 #define SQLITE_VERSION_NUMBER 3050000
470 #define SQLITE_SOURCE_ID "2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc"
471
472 /*
473 ** CAPI3REF: Run-Time Library Version Numbers
474 ** KEYWORDS: sqlite3_version sqlite3_sourceid
475 **
@@ -1479,10 +1479,16 @@
1479 ** to block for up to M milliseconds before failing when attempting to
1480 ** obtain a file lock using the xLock or xShmLock methods of the VFS.
1481 ** The parameter is a pointer to a 32-bit signed integer that contains
1482 ** the value that M is to be set to. Before returning, the 32-bit signed
1483 ** integer is overwritten with the previous value of M.
 
 
 
 
 
 
1484 **
1485 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1486 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1487 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1488 ** The "data version" for the pager is written into the pointer. The
@@ -1576,10 +1582,11 @@
1576 #define SQLITE_FCNTL_CKPT_START 39
1577 #define SQLITE_FCNTL_EXTERNAL_READER 40
1578 #define SQLITE_FCNTL_CKSM_FILE 41
1579 #define SQLITE_FCNTL_RESET_CACHE 42
1580 #define SQLITE_FCNTL_NULL_IO 43
 
1581
1582 /* deprecated names */
1583 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1584 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1585 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3332,10 +3339,48 @@
3332 **
3333 ** See also: [PRAGMA busy_timeout]
3334 */
3335 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3337 /*
3338 ** CAPI3REF: Convenience Routines For Running Queries
3339 ** METHOD: sqlite3
3340 **
3341 ** This is a legacy interface that is preserved for backwards compatibility.
@@ -14097,18 +14142,26 @@
14097 ** * Terms in the SET clause of an UPDATE statement
14098 ** * Terms in the result set of a SELECT statement
14099 ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
14100 ** * Terms in the VALUES clause of an INSERT statement
14101 **
14102 ** The hard upper limit here is 32676. Most database people will
14103 ** tell you that in a well-normalized database, you usually should
14104 ** not have more than a dozen or so columns in any table. And if
14105 ** that is the case, there is no point in having more than a few
14106 ** dozen values in any of the other situations described above.
 
 
 
 
 
 
14107 */
14108 #ifndef SQLITE_MAX_COLUMN
14109 # define SQLITE_MAX_COLUMN 2000
 
 
14110 #endif
14111
14112 /*
14113 ** The maximum length of a single SQL statement in bytes.
14114 **
@@ -18076,10 +18129,14 @@
18076 BusyHandler busyHandler; /* Busy callback */
18077 Db aDbStatic[2]; /* Static space for the 2 default backends */
18078 Savepoint *pSavepoint; /* List of active savepoints */
18079 int nAnalysisLimit; /* Number of index rows to ANALYZE */
18080 int busyTimeout; /* Busy handler timeout, in msec */
 
 
 
 
18081 int nSavepoint; /* Number of non-transaction savepoints */
18082 int nStatement; /* Number of nested statement-transactions */
18083 i64 nDeferredCons; /* Net deferred constraints this transaction. */
18084 i64 nDeferredImmCons; /* Net deferred immediate constraints */
18085 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
@@ -19074,11 +19131,11 @@
19074 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
19075 ExprList *aColExpr; /* Column expressions */
19076 Pgno tnum; /* DB Page containing root of this index */
19077 LogEst szIdxRow; /* Estimated average row size in bytes */
19078 u16 nKeyCol; /* Number of columns forming the key */
19079 u16 nColumn; /* Number of columns stored in the index */
19080 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
19081 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
19082 unsigned bUnordered:1; /* Use this index for == or IN queries only */
19083 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
19084 unsigned isResized:1; /* True if resizeIndexObject() has been called */
@@ -19412,14 +19469,14 @@
19412 #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
19413
19414 /* Macros can be used to test, set, or clear bits in the
19415 ** Expr.flags field.
19416 */
19417 #define ExprHasProperty(E,P) (((E)->flags&(P))!=0)
19418 #define ExprHasAllProperty(E,P) (((E)->flags&(P))==(P))
19419 #define ExprSetProperty(E,P) (E)->flags|=(P)
19420 #define ExprClearProperty(E,P) (E)->flags&=~(P)
19421 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19422 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19423 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19424
19425 /* Macros used to ensure that the correct members of unions are accessed
@@ -21223,11 +21280,11 @@
21223 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
21224 SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
21225 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
21226 SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
21227 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
21228 SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index*, i16);
21229 #ifdef SQLITE_OMIT_GENERATED_COLUMNS
21230 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21231 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
21232 #else
21233 SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
@@ -21321,11 +21378,11 @@
21321 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
21322 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
21323 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
21324 SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
21325 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
21326 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
21327 SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
21328 Expr*, int, int, u8);
21329 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
21330 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
21331 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
@@ -21457,11 +21514,12 @@
21457 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
21458 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21459 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
21460 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21461 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21462 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*);
 
21463 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
21464 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
21465 SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
21466 SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
21467 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
@@ -22557,10 +22615,13 @@
22557 #ifdef SQLITE_ENABLE_RTREE
22558 "ENABLE_RTREE",
22559 #endif
22560 #ifdef SQLITE_ENABLE_SESSION
22561 "ENABLE_SESSION",
 
 
 
22562 #endif
22563 #ifdef SQLITE_ENABLE_SNAPSHOT
22564 "ENABLE_SNAPSHOT",
22565 #endif
22566 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
@@ -24372,12 +24433,13 @@
24372 u32 nFree = countLookasideSlots(db->lookaside.pFree);
24373 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24374 nInit += countLookasideSlots(db->lookaside.pSmallInit);
24375 nFree += countLookasideSlots(db->lookaside.pSmallFree);
24376 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24377 if( pHighwater ) *pHighwater = db->lookaside.nSlot - nInit;
24378 return db->lookaside.nSlot - (nInit+nFree);
 
24379 }
24380
24381 /*
24382 ** Query status information for a single database connection
24383 */
@@ -24426,11 +24488,11 @@
24426 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
24427 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
24428 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
24429 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
24430 *pCurrent = 0;
24431 *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
24432 if( resetFlag ){
24433 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
24434 }
24435 break;
24436 }
@@ -31541,21 +31603,21 @@
31541 #define etSTRING 5 /* Strings. %s */
31542 #define etDYNSTRING 6 /* Dynamically allocated strings. %z */
31543 #define etPERCENT 7 /* Percent symbol. %% */
31544 #define etCHARX 8 /* Characters. %c */
31545 /* The rest are extensions, not normally found in printf() */
31546 #define etSQLESCAPE 9 /* Strings with '\'' doubled. %q */
31547 #define etSQLESCAPE2 10 /* Strings with '\'' doubled and enclosed in '',
31548 NULL pointers replaced by SQL NULL. %Q */
31549 #define etTOKEN 11 /* a pointer to a Token structure */
31550 #define etSRCITEM 12 /* a pointer to a SrcItem */
31551 #define etPOINTER 13 /* The %p conversion */
31552 #define etSQLESCAPE3 14 /* %w -> Strings with '\"' doubled */
31553 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31554 #define etDECIMAL 16 /* %d or %u, but not %x, %o */
31555
31556 #define etINVALID 17 /* Any unrecognized conversion type */
31557
31558
31559 /*
31560 ** An "etByte" is an 8-bit unsigned value.
31561 */
@@ -31590,13 +31652,13 @@
31590 static const et_info fmtinfo[] = {
31591 { 'd', 10, 1, etDECIMAL, 0, 0 },
31592 { 's', 0, 4, etSTRING, 0, 0 },
31593 { 'g', 0, 1, etGENERIC, 30, 0 },
31594 { 'z', 0, 4, etDYNSTRING, 0, 0 },
31595 { 'q', 0, 4, etSQLESCAPE, 0, 0 },
31596 { 'Q', 0, 4, etSQLESCAPE2, 0, 0 },
31597 { 'w', 0, 4, etSQLESCAPE3, 0, 0 },
31598 { 'c', 0, 0, etCHARX, 0, 0 },
31599 { 'o', 8, 0, etRADIX, 0, 2 },
31600 { 'u', 10, 0, etDECIMAL, 0, 0 },
31601 { 'x', 16, 0, etRADIX, 16, 1 },
31602 { 'X', 16, 0, etRADIX, 0, 4 },
@@ -32189,29 +32251,11 @@
32189 }else{
32190 buf[0] = 0;
32191 }
32192 }else{
32193 unsigned int ch = va_arg(ap,unsigned int);
32194 if( ch<0x00080 ){
32195 buf[0] = ch & 0xff;
32196 length = 1;
32197 }else if( ch<0x00800 ){
32198 buf[0] = 0xc0 + (u8)((ch>>6)&0x1f);
32199 buf[1] = 0x80 + (u8)(ch & 0x3f);
32200 length = 2;
32201 }else if( ch<0x10000 ){
32202 buf[0] = 0xe0 + (u8)((ch>>12)&0x0f);
32203 buf[1] = 0x80 + (u8)((ch>>6) & 0x3f);
32204 buf[2] = 0x80 + (u8)(ch & 0x3f);
32205 length = 3;
32206 }else{
32207 buf[0] = 0xf0 + (u8)((ch>>18) & 0x07);
32208 buf[1] = 0x80 + (u8)((ch>>12) & 0x3f);
32209 buf[2] = 0x80 + (u8)((ch>>6) & 0x3f);
32210 buf[3] = 0x80 + (u8)(ch & 0x3f);
32211 length = 4;
32212 }
32213 }
32214 if( precision>1 ){
32215 i64 nPrior = 1;
32216 width -= precision-1;
32217 if( width>1 && !flag_leftjustify ){
@@ -32287,26 +32331,35 @@
32287 /* Adjust width to account for extra bytes in UTF-8 characters */
32288 int ii = length - 1;
32289 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
32290 }
32291 break;
32292 case etSQLESCAPE: /* %q: Escape ' characters */
32293 case etSQLESCAPE2: /* %Q: Escape ' and enclose in '...' */
32294 case etSQLESCAPE3: { /* %w: Escape " characters */
32295 i64 i, j, k, n;
32296 int needQuote, isnull;
32297 char ch;
32298 char q = ((xtype==etSQLESCAPE3)?'"':'\''); /* Quote character */
32299 char *escarg;
 
32300
32301 if( bArgList ){
32302 escarg = getTextArg(pArgList);
32303 }else{
32304 escarg = va_arg(ap,char*);
32305 }
32306 isnull = escarg==0;
32307 if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
 
 
 
 
 
 
 
 
 
32308 /* For %q, %Q, and %w, the precision is the number of bytes (or
32309 ** characters if the ! flags is present) to use from the input.
32310 ** Because of the extra quoting characters inserted, the number
32311 ** of output characters may be larger than the precision.
32312 */
@@ -32315,26 +32368,77 @@
32315 if( ch==q ) n++;
32316 if( flag_altform2 && (ch&0xc0)==0xc0 ){
32317 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
32318 }
32319 }
32320 needQuote = !isnull && xtype==etSQLESCAPE2;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32321 n += i + 3;
32322 if( n>etBUFSIZE ){
32323 bufpt = zExtra = printfTempBuf(pAccum, n);
32324 if( bufpt==0 ) return;
32325 }else{
32326 bufpt = buf;
32327 }
32328 j = 0;
32329 if( needQuote ) bufpt[j++] = q;
 
 
 
 
 
 
 
32330 k = i;
32331 for(i=0; i<k; i++){
32332 bufpt[j++] = ch = escarg[i];
32333 if( ch==q ) bufpt[j++] = ch;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32334 }
32335 if( needQuote ) bufpt[j++] = q;
 
 
 
32336 bufpt[j] = 0;
32337 length = j;
32338 goto adjust_width_for_utf8;
32339 }
32340 case etTOKEN: {
@@ -34828,10 +34932,39 @@
34828 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34829 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
34830 *zOut++ = (u8)(c&0x00FF); \
34831 } \
34832 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34833
34834 /*
34835 ** Translate a single UTF-8 character. Return the unicode value.
34836 **
34837 ** During translation, assume that the byte that zTerm points
@@ -38911,10 +39044,11 @@
38911 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
38912 unsigned fsFlags; /* cached details from statfs() */
38913 #endif
38914 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
38915 unsigned iBusyTimeout; /* Wait this many millisec on locks */
 
38916 #endif
38917 #if OS_VXWORKS
38918 struct vxworksFileId *pId; /* Unique file ID */
38919 #endif
38920 #ifdef SQLITE_DEBUG
@@ -40304,10 +40438,17 @@
40304 pInode->nLock++;
40305 }else{
40306 rc = 0;
40307 }
40308 }else{
 
 
 
 
 
 
 
40309 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
40310 }
40311 return rc;
40312 }
40313
@@ -42665,21 +42806,27 @@
42665 return SQLITE_OK;
42666 }
42667 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42668 case SQLITE_FCNTL_LOCK_TIMEOUT: {
42669 int iOld = pFile->iBusyTimeout;
 
42670 #if SQLITE_ENABLE_SETLK_TIMEOUT==1
42671 pFile->iBusyTimeout = *(int*)pArg;
42672 #elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42673 pFile->iBusyTimeout = !!(*(int*)pArg);
42674 #else
42675 # error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
42676 #endif
42677 *(int*)pArg = iOld;
42678 return SQLITE_OK;
42679 }
42680 #endif
 
 
 
 
 
42681 #if SQLITE_MAX_MMAP_SIZE>0
42682 case SQLITE_FCNTL_MMAP_SIZE: {
42683 i64 newLimit = *(i64*)pArg;
42684 int rc = SQLITE_OK;
42685 if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -43658,11 +43805,11 @@
43658 ** occur later in the above list than the lock being obtained may be
43659 ** held.
43660 **
43661 ** It is not permitted to block on the RECOVER lock.
43662 */
43663 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43664 {
43665 u16 lockMask = (p->exclMask|p->sharedMask);
43666 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43667 (ofst!=2) /* not RECOVER */
43668 && (ofst!=1 || lockMask==0 || lockMask==2)
@@ -47188,11 +47335,21 @@
47188 HANDLE hMap; /* Handle for accessing memory mapping */
47189 void *pMapRegion; /* Area memory mapped */
47190 sqlite3_int64 mmapSize; /* Size of mapped region */
47191 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
47192 #endif
 
 
 
 
47193 };
 
 
 
 
 
 
47194
47195 /*
47196 ** The winVfsAppData structure is used for the pAppData member for all of the
47197 ** Win32 VFS variants.
47198 */
@@ -47623,10 +47780,16 @@
47623 #endif
47624
47625 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
47626 LPWSTR*))aSyscall[25].pCurrent)
47627
 
 
 
 
 
 
47628 { "GetLastError", (SYSCALL)GetLastError, 0 },
47629
47630 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
47631
47632 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
@@ -47905,15 +48068,17 @@
47905 #endif
47906
47907 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
47908 DWORD,DWORD))aSyscall[62].pCurrent)
47909
47910 #if !SQLITE_OS_WINRT
 
 
 
 
 
47911 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
47912 #else
47913 { "WaitForSingleObject", (SYSCALL)0, 0 },
47914 #endif
47915
47916 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
47917 DWORD))aSyscall[63].pCurrent)
47918
47919 #if !SQLITE_OS_WINCE
@@ -48056,10 +48221,44 @@
48056 #endif
48057
48058 #define osFlushViewOfFile \
48059 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
48060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48061 }; /* End of the overrideable system calls */
48062
48063 /*
48064 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
48065 ** "win32" VFSes. Return SQLITE_OK upon successfully updating the
@@ -48354,11 +48553,13 @@
48354 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48355 #endif
48356 }
48357 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48358 #elif SQLITE_TEST
48359 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
 
 
48360 #else
48361 /*
48362 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48363 ** deprecated are always assumed to be based on the NT kernel.
48364 */
@@ -49440,10 +49641,89 @@
49440 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49441 numBytesHigh);
49442 }
49443 #endif
49444 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49445
49446 /*
49447 ** Unlock a file region.
49448 */
49449 static BOOL winUnlockFile(
@@ -49471,10 +49751,18 @@
49471 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49472 numBytesHigh);
49473 }
49474 #endif
49475 }
 
 
 
 
 
 
 
 
49476
49477 /*****************************************************************************
49478 ** The next group of routines implement the I/O methods specified
49479 ** by the sqlite3_io_methods object.
49480 ******************************************************************************/
@@ -49485,69 +49773,73 @@
49485 #ifndef INVALID_SET_FILE_POINTER
49486 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
49487 #endif
49488
49489 /*
49490 ** Move the current position of the file handle passed as the first
49491 ** argument to offset iOffset within the file. If successful, return 0.
49492 ** Otherwise, set pFile->lastErrno and return non-zero.
 
49493 */
49494 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
 
 
49495 #if !SQLITE_OS_WINRT
49496 LONG upperBits; /* Most sig. 32 bits of new offset */
49497 LONG lowerBits; /* Least sig. 32 bits of new offset */
49498 DWORD dwRet; /* Value returned by SetFilePointer() */
49499 DWORD lastErrno; /* Value returned by GetLastError() */
49500
49501 OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
49502
49503 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
49504 lowerBits = (LONG)(iOffset & 0xffffffff);
 
 
49505
49506 /* API oddity: If successful, SetFilePointer() returns a dword
49507 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49508 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
49509 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
49510 ** whether an error has actually occurred, it is also necessary to call
49511 ** GetLastError().
49512 */
49513 dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
49514
49515 if( (dwRet==INVALID_SET_FILE_POINTER
49516 && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
49517 pFile->lastErrno = lastErrno;
49518 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49519 "winSeekFile", pFile->zPath);
49520 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49521 return 1;
49522 }
49523
49524 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49525 return 0;
49526 #else
49527 /*
49528 ** Same as above, except that this implementation works for WinRT.
49529 */
49530
49531 LARGE_INTEGER x; /* The new offset */
49532 BOOL bRet; /* Value returned by SetFilePointerEx() */
49533
49534 x.QuadPart = iOffset;
49535 bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
49536
49537 if(!bRet){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49538 pFile->lastErrno = osGetLastError();
49539 winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
49540 "winSeekFile", pFile->zPath);
49541 OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
49542 return 1;
49543 }
49544
49545 OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
49546 return 0;
49547 #endif
49548 }
49549
49550 #if SQLITE_MAX_MMAP_SIZE>0
49551 /* Forward references to VFS helper methods used for memory mapped files */
49552 static int winMapfile(winFile*, sqlite3_int64);
49553 static int winUnmapfile(winFile*);
@@ -49803,10 +50095,64 @@
49803 }
49804 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
49805 osGetCurrentProcessId(), pFile, pFile->h));
49806 return SQLITE_OK;
49807 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49808
49809 /*
49810 ** Truncate an open file to a specified size
49811 */
49812 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
@@ -50059,31 +50405,32 @@
50059 /*
50060 ** Acquire a reader lock.
50061 ** Different API routines are called depending on whether or not this
50062 ** is Win9x or WinNT.
50063 */
50064 static int winGetReadLock(winFile *pFile){
50065 int res;
 
50066 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50067 if( osIsNT() ){
50068 #if SQLITE_OS_WINCE
50069 /*
50070 ** NOTE: Windows CE is handled differently here due its lack of the Win32
50071 ** API LockFileEx.
50072 */
50073 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
50074 #else
50075 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
50076 SHARED_SIZE, 0);
50077 #endif
50078 }
50079 #ifdef SQLITE_WIN32_HAS_ANSI
50080 else{
50081 int lk;
50082 sqlite3_randomness(sizeof(lk), &lk);
50083 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
50084 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50085 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
50086 }
50087 #endif
50088 if( res == 0 ){
50089 pFile->lastErrno = osGetLastError();
@@ -50174,50 +50521,66 @@
50174 */
50175 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
50176 assert( locktype!=PENDING_LOCK );
50177 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
50178
50179 /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
50180 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
50181 ** the PENDING_LOCK byte is temporary.
50182 */
50183 newLocktype = pFile->locktype;
50184 if( pFile->locktype==NO_LOCK
50185 || (locktype==EXCLUSIVE_LOCK && pFile->locktype<=RESERVED_LOCK)
50186 ){
50187 int cnt = 3;
50188 while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
50189 PENDING_BYTE, 0, 1, 0))==0 ){
 
 
 
 
 
 
 
50190 /* Try 3 times to get the pending lock. This is needed to work
50191 ** around problems caused by indexing and/or anti-virus software on
50192 ** Windows systems.
 
50193 ** If you are using this code as a model for alternative VFSes, do not
50194 ** copy this retry logic. It is a hack intended for Windows only.
50195 */
 
 
50196 lastErrno = osGetLastError();
50197 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50198 pFile->h, cnt, res));
 
 
50199 if( lastErrno==ERROR_INVALID_HANDLE ){
50200 pFile->lastErrno = lastErrno;
50201 rc = SQLITE_IOERR_LOCK;
50202 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50203 pFile->h, cnt, sqlite3ErrName(rc)));
 
50204 return rc;
50205 }
50206 if( cnt ) sqlite3_win32_sleep(1);
 
 
50207 }
50208 gotPendingLock = res;
50209 if( !res ){
50210 lastErrno = osGetLastError();
50211 }
50212 }
50213
50214 /* Acquire a shared lock
50215 */
50216 if( locktype==SHARED_LOCK && res ){
50217 assert( pFile->locktype==NO_LOCK );
50218 res = winGetReadLock(pFile);
 
 
 
 
50219 if( res ){
50220 newLocktype = SHARED_LOCK;
50221 }else{
50222 lastErrno = osGetLastError();
50223 }
@@ -50251,11 +50614,11 @@
50251 SHARED_SIZE, 0);
50252 if( res ){
50253 newLocktype = EXCLUSIVE_LOCK;
50254 }else{
50255 lastErrno = osGetLastError();
50256 winGetReadLock(pFile);
50257 }
50258 }
50259
50260 /* If we are holding a PENDING lock that ought to be released, then
50261 ** release it now.
@@ -50331,11 +50694,11 @@
50331 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
50332 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50333 type = pFile->locktype;
50334 if( type>=EXCLUSIVE_LOCK ){
50335 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50336 if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
50337 /* This should never happen. We should always be able to
50338 ** reacquire the read lock */
50339 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
50340 "winUnlock", pFile->zPath);
50341 }
@@ -50541,10 +50904,32 @@
50541 }
50542 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50543 return rc;
50544 }
50545 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50546 }
50547 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
50548 return SQLITE_NOTFOUND;
50549 }
50550
@@ -50621,36 +51006,39 @@
50621 ** nRef
50622 ** pNext
50623 **
50624 ** The following fields are read-only after the object is created:
50625 **
50626 ** fid
50627 ** zFilename
50628 **
50629 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
50630 ** winShmMutexHeld() is true when reading or writing any other field
50631 ** in this structure.
50632 **
 
 
 
 
 
50633 */
50634 struct winShmNode {
50635 sqlite3_mutex *mutex; /* Mutex to access this object */
50636 char *zFilename; /* Name of the file */
50637 winFile hFile; /* File handle from winOpen */
50638
 
 
50639 int szRegion; /* Size of shared-memory regions */
50640 int nRegion; /* Size of array apRegion */
50641 u8 isReadonly; /* True if read-only */
50642 u8 isUnlocked; /* True if no DMS lock held */
50643
50644 struct ShmRegion {
50645 HANDLE hMap; /* File handle from CreateFileMapping */
50646 void *pMap;
50647 } *aRegion;
50648 DWORD lastErrno; /* The Windows errno from the last I/O error */
50649
50650 int nRef; /* Number of winShm objects pointing to this */
50651 winShm *pFirst; /* All winShm objects pointing to this */
50652 winShmNode *pNext; /* Next in list of all winShmNode objects */
50653 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50654 u8 nextShmId; /* Next available winShm.id value */
50655 #endif
50656 };
@@ -50662,27 +51050,19 @@
50662 */
50663 static winShmNode *winShmNodeList = 0;
50664
50665 /*
50666 ** Structure used internally by this VFS to record the state of an
50667 ** open shared memory connection.
50668 **
50669 ** The following fields are initialized when this object is created and
50670 ** are read-only thereafter:
50671 **
50672 ** winShm.pShmNode
50673 ** winShm.id
50674 **
50675 ** All other fields are read/write. The winShm.pShmNode->mutex must be held
50676 ** while accessing any read/write fields.
50677 */
50678 struct winShm {
50679 winShmNode *pShmNode; /* The underlying winShmNode object */
50680 winShm *pNext; /* Next winShm with the same winShmNode */
50681 u8 hasMutex; /* True if holding the winShmNode mutex */
50682 u16 sharedMask; /* Mask of shared locks held */
50683 u16 exclMask; /* Mask of exclusive locks held */
 
 
50684 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50685 u8 id; /* Id of this connection with its winShmNode */
50686 #endif
50687 };
50688
@@ -50690,54 +51070,10 @@
50690 ** Constants used for locking
50691 */
50692 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
50693 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
50694
50695 /*
50696 ** Apply advisory locks for all n bytes beginning at ofst.
50697 */
50698 #define WINSHM_UNLCK 1
50699 #define WINSHM_RDLCK 2
50700 #define WINSHM_WRLCK 3
50701 static int winShmSystemLock(
50702 winShmNode *pFile, /* Apply locks to this open shared-memory segment */
50703 int lockType, /* WINSHM_UNLCK, WINSHM_RDLCK, or WINSHM_WRLCK */
50704 int ofst, /* Offset to first byte to be locked/unlocked */
50705 int nByte /* Number of bytes to lock or unlock */
50706 ){
50707 int rc = 0; /* Result code form Lock/UnlockFileEx() */
50708
50709 /* Access to the winShmNode object is serialized by the caller */
50710 assert( pFile->nRef==0 || sqlite3_mutex_held(pFile->mutex) );
50711
50712 OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
50713 pFile->hFile.h, lockType, ofst, nByte));
50714
50715 /* Release/Acquire the system-level lock */
50716 if( lockType==WINSHM_UNLCK ){
50717 rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
50718 }else{
50719 /* Initialize the locking parameters */
50720 DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
50721 if( lockType == WINSHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
50722 rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
50723 }
50724
50725 if( rc!= 0 ){
50726 rc = SQLITE_OK;
50727 }else{
50728 pFile->lastErrno = osGetLastError();
50729 rc = SQLITE_BUSY;
50730 }
50731
50732 OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
50733 pFile->hFile.h, (lockType == WINSHM_UNLCK) ? "winUnlockFile" :
50734 "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
50735
50736 return rc;
50737 }
50738
50739 /* Forward references to VFS methods */
50740 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
50741 static int winDelete(sqlite3_vfs *,const char*,int);
50742
50743 /*
@@ -50765,15 +51101,11 @@
50765 bRc = osCloseHandle(p->aRegion[i].hMap);
50766 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
50767 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
50768 UNUSED_VARIABLE_VALUE(bRc);
50769 }
50770 if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
50771 SimulateIOErrorBenign(1);
50772 winClose((sqlite3_file *)&p->hFile);
50773 SimulateIOErrorBenign(0);
50774 }
50775 if( deleteFlag ){
50776 SimulateIOErrorBenign(1);
50777 sqlite3BeginBenignMalloc();
50778 winDelete(pVfs, p->zFilename, 0);
50779 sqlite3EndBenignMalloc();
@@ -50787,46 +51119,166 @@
50787 }
50788 }
50789 }
50790
50791 /*
50792 ** The DMS lock has not yet been taken on shm file pShmNode. Attempt to
50793 ** take it now. Return SQLITE_OK if successful, or an SQLite error
50794 ** code otherwise.
50795 **
50796 ** If the DMS cannot be locked because this is a readonly_shm=1
50797 ** connection and no other process already holds a lock, return
50798 ** SQLITE_READONLY_CANTINIT and set pShmNode->isUnlocked=1.
50799 */
50800 static int winLockSharedMemory(winShmNode *pShmNode){
50801 int rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, WIN_SHM_DMS, 1);
 
50802
 
 
50803 if( rc==SQLITE_OK ){
 
 
 
50804 if( pShmNode->isReadonly ){
50805 pShmNode->isUnlocked = 1;
50806 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50807 return SQLITE_READONLY_CANTINIT;
50808 }else if( winTruncate((sqlite3_file*)&pShmNode->hFile, 0) ){
50809 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50810 return winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
50811 "winLockSharedMemory", pShmNode->zFilename);
50812 }
50813 }
50814
50815 if( rc==SQLITE_OK ){
50816 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50817 }
50818
50819 return winShmSystemLock(pShmNode, WINSHM_RDLCK, WIN_SHM_DMS, 1);
50820 }
50821
50822 /*
50823 ** Open the shared-memory area associated with database file pDbFd.
50824 **
50825 ** When opening a new shared-memory file, if no other instances of that
50826 ** file are currently open, in this process or in other processes, then
50827 ** the file must be truncated to zero length or have its header cleared.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50828 */
50829 static int winOpenSharedMemory(winFile *pDbFd){
50830 struct winShm *p; /* The connection to be opened */
50831 winShmNode *pShmNode = 0; /* The underlying mmapped file */
50832 int rc = SQLITE_OK; /* Result code */
@@ -50834,102 +51286,87 @@
50834 int nName; /* Size of zName in bytes */
50835
50836 assert( pDbFd->pShm==0 ); /* Not previously opened */
50837
50838 /* Allocate space for the new sqlite3_shm object. Also speculatively
50839 ** allocate space for a new winShmNode and filename.
50840 */
50841 p = sqlite3MallocZero( sizeof(*p) );
50842 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
50843 nName = sqlite3Strlen30(pDbFd->zPath);
50844 pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 );
50845 if( pNew==0 ){
50846 sqlite3_free(p);
50847 return SQLITE_IOERR_NOMEM_BKPT;
50848 }
50849 pNew->zFilename = (char*)&pNew[1];
 
 
50850 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
50851 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
 
 
 
 
 
 
50852
50853 /* Look to see if there is an existing winShmNode that can be used.
50854 ** If no matching winShmNode currently exists, create a new one.
50855 */
50856 winShmEnterMutex();
50857 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
50858 /* TBD need to come up with better match here. Perhaps
50859 ** use FILE_ID_BOTH_DIR_INFO Structure.
50860 */
50861 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
50862 }
50863 if( pShmNode ){
50864 sqlite3_free(pNew);
50865 }else{
50866 int inFlags = SQLITE_OPEN_WAL;
50867 int outFlags = 0;
50868
50869 pShmNode = pNew;
50870 pNew = 0;
50871 ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
50872 pShmNode->pNext = winShmNodeList;
50873 winShmNodeList = pShmNode;
50874
 
50875 if( sqlite3GlobalConfig.bCoreMutex ){
50876 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
50877 if( pShmNode->mutex==0 ){
50878 rc = SQLITE_IOERR_NOMEM_BKPT;
50879 goto shm_open_err;
50880 }
50881 }
50882
50883 if( 0==sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
50884 inFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
50885 }else{
50886 inFlags |= SQLITE_OPEN_READONLY;
50887 }
50888 rc = winOpen(pDbFd->pVfs, pShmNode->zFilename,
50889 (sqlite3_file*)&pShmNode->hFile,
50890 inFlags, &outFlags);
50891 if( rc!=SQLITE_OK ){
50892 rc = winLogError(rc, osGetLastError(), "winOpenShm",
50893 pShmNode->zFilename);
50894 goto shm_open_err;
50895 }
50896 if( outFlags==SQLITE_OPEN_READONLY ) pShmNode->isReadonly = 1;
50897
50898 rc = winLockSharedMemory(pShmNode);
50899 if( rc!=SQLITE_OK && rc!=SQLITE_READONLY_CANTINIT ) goto shm_open_err;
50900 }
50901
50902 /* Make the new connection a child of the winShmNode */
50903 p->pShmNode = pShmNode;
 
 
 
50904 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
50905 p->id = pShmNode->nextShmId++;
50906 #endif
50907 pShmNode->nRef++;
50908 pDbFd->pShm = p;
50909 winShmLeaveMutex();
50910
50911 /* The reference count on pShmNode has already been incremented under
50912 ** the cover of the winShmEnterMutex() mutex and the pointer from the
50913 ** new (struct winShm) object to the pShmNode has been set. All that is
50914 ** left to do is to link the new object into the linked list starting
50915 ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
50916 ** mutex.
50917 */
50918 sqlite3_mutex_enter(pShmNode->mutex);
50919 p->pNext = pShmNode->pFirst;
50920 pShmNode->pFirst = p;
50921 sqlite3_mutex_leave(pShmNode->mutex);
50922 return rc;
50923
50924 /* Jump here on any error */
50925 shm_open_err:
50926 winShmSystemLock(pShmNode, WINSHM_UNLCK, WIN_SHM_DMS, 1);
50927 winShmPurge(pDbFd->pVfs, 0); /* This call frees pShmNode if required */
50928 sqlite3_free(p);
50929 sqlite3_free(pNew);
50930 winShmLeaveMutex();
50931 return rc;
50932 }
50933
50934 /*
50935 ** Close a connection to shared-memory. Delete the underlying
@@ -50940,38 +51377,33 @@
50940 int deleteFlag /* Delete after closing if true */
50941 ){
50942 winFile *pDbFd; /* Database holding shared-memory */
50943 winShm *p; /* The connection to be closed */
50944 winShmNode *pShmNode; /* The underlying shared-memory file */
50945 winShm **pp; /* For looping over sibling connections */
50946
50947 pDbFd = (winFile*)fd;
50948 p = pDbFd->pShm;
50949 if( p==0 ) return SQLITE_OK;
 
 
 
 
50950 pShmNode = p->pShmNode;
50951
50952 /* Remove connection p from the set of connections associated
50953 ** with pShmNode */
50954 sqlite3_mutex_enter(pShmNode->mutex);
50955 for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
50956 *pp = p->pNext;
50957
50958 /* Free the connection p */
50959 sqlite3_free(p);
50960 pDbFd->pShm = 0;
50961 sqlite3_mutex_leave(pShmNode->mutex);
50962
50963 /* If pShmNode->nRef has reached 0, then close the underlying
50964 ** shared-memory file, too */
50965 winShmEnterMutex();
50966 assert( pShmNode->nRef>0 );
50967 pShmNode->nRef--;
50968 if( pShmNode->nRef==0 ){
50969 winShmPurge(pDbFd->pVfs, deleteFlag);
50970 }
50971 winShmLeaveMutex();
50972
 
 
 
50973 return SQLITE_OK;
50974 }
50975
50976 /*
50977 ** Change the lock state for a shared-memory segment.
@@ -50982,14 +51414,13 @@
50982 int n, /* Number of locks to acquire or release */
50983 int flags /* What to do with the lock */
50984 ){
50985 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
50986 winShm *p = pDbFd->pShm; /* The shared memory being locked */
50987 winShm *pX; /* For looping over all siblings */
50988 winShmNode *pShmNode;
50989 int rc = SQLITE_OK; /* Result code */
50990 u16 mask; /* Mask of locks to take or release */
50991
50992 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
50993 pShmNode = p->pShmNode;
50994 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
50995
@@ -50999,89 +51430,86 @@
50999 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
51000 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
51001 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
51002 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
51003
51004 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
51005 assert( n>1 || mask==(1<<ofst) );
51006 sqlite3_mutex_enter(pShmNode->mutex);
51007 if( flags & SQLITE_SHM_UNLOCK ){
51008 u16 allMask = 0; /* Mask of locks held by siblings */
51009
51010 /* See if any siblings hold this same lock */
51011 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51012 if( pX==p ) continue;
51013 assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
51014 allMask |= pX->sharedMask;
51015 }
51016
51017 /* Unlock the system-level locks */
51018 if( (mask & allMask)==0 ){
51019 rc = winShmSystemLock(pShmNode, WINSHM_UNLCK, ofst+WIN_SHM_BASE, n);
51020 }else{
51021 rc = SQLITE_OK;
51022 }
51023
51024 /* Undo the local locks */
51025 if( rc==SQLITE_OK ){
51026 p->exclMask &= ~mask;
51027 p->sharedMask &= ~mask;
51028 }
51029 }else if( flags & SQLITE_SHM_SHARED ){
51030 u16 allShared = 0; /* Union of locks held by connections other than "p" */
51031
51032 /* Find out which shared locks are already held by sibling connections.
51033 ** If any sibling already holds an exclusive lock, go ahead and return
51034 ** SQLITE_BUSY.
51035 */
51036 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51037 if( (pX->exclMask & mask)!=0 ){
51038 rc = SQLITE_BUSY;
51039 break;
51040 }
51041 allShared |= pX->sharedMask;
51042 }
51043
51044 /* Get shared locks at the system level, if necessary */
51045 if( rc==SQLITE_OK ){
51046 if( (allShared & mask)==0 ){
51047 rc = winShmSystemLock(pShmNode, WINSHM_RDLCK, ofst+WIN_SHM_BASE, n);
51048 }else{
51049 rc = SQLITE_OK;
51050 }
51051 }
51052
51053 /* Get the local shared locks */
51054 if( rc==SQLITE_OK ){
51055 p->sharedMask |= mask;
51056 }
51057 }else{
51058 /* Make sure no sibling connections hold locks that will block this
51059 ** lock. If any do, return SQLITE_BUSY right away.
51060 */
51061 for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
51062 if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
51063 rc = SQLITE_BUSY;
51064 break;
51065 }
51066 }
51067
51068 /* Get the exclusive locks at the system level. Then if successful
51069 ** also mark the local connection as being locked.
51070 */
51071 if( rc==SQLITE_OK ){
51072 rc = winShmSystemLock(pShmNode, WINSHM_WRLCK, ofst+WIN_SHM_BASE, n);
51073 if( rc==SQLITE_OK ){
51074 assert( (p->sharedMask & mask)==0 );
51075 p->exclMask |= mask;
51076 }
51077 }
51078 }
51079 sqlite3_mutex_leave(pShmNode->mutex);
51080 OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
51081 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51082 sqlite3ErrName(rc)));
51083 return rc;
51084 }
51085
51086 /*
51087 ** Implement a memory barrier or memory fence on shared memory.
@@ -51139,17 +51567,19 @@
51139 }
51140 pShmNode = pShm->pShmNode;
51141
51142 sqlite3_mutex_enter(pShmNode->mutex);
51143 if( pShmNode->isUnlocked ){
51144 rc = winLockSharedMemory(pShmNode);
 
 
51145 if( rc!=SQLITE_OK ) goto shmpage_out;
51146 pShmNode->isUnlocked = 0;
51147 }
 
51148 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
51149
51150 if( pShmNode->nRegion<=iRegion ){
 
51151 struct ShmRegion *apNew; /* New aRegion[] array */
51152 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
51153 sqlite3_int64 sz; /* Current size of wal-index file */
51154
51155 pShmNode->szRegion = szRegion;
@@ -51156,35 +51586,32 @@
51156
51157 /* The requested region is not mapped into this processes address space.
51158 ** Check to see if it has been allocated (i.e. if the wal-index file is
51159 ** large enough to contain the requested region).
51160 */
51161 rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
51162 if( rc!=SQLITE_OK ){
51163 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51164 "winShmMap1", pDbFd->zPath);
51165 goto shmpage_out;
51166 }
51167
51168 if( sz<nByte ){
51169 /* The requested memory region does not exist. If isWrite is set to
51170 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
51171 **
51172 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51173 ** the requested memory region.
51174 */
51175 if( !isWrite ) goto shmpage_out;
51176 rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
51177 if( rc!=SQLITE_OK ){
51178 rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
51179 "winShmMap2", pDbFd->zPath);
51180 goto shmpage_out;
51181 }
51182 }
51183
51184 /* Map the requested memory region into this processes address space. */
51185 apNew = (struct ShmRegion *)sqlite3_realloc64(
51186 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51187 );
51188 if( !apNew ){
51189 rc = SQLITE_IOERR_NOMEM_BKPT;
51190 goto shmpage_out;
@@ -51199,22 +51626,17 @@
51199 while( pShmNode->nRegion<=iRegion ){
51200 HANDLE hMap = NULL; /* file-mapping handle */
51201 void *pMap = 0; /* Mapped memory region */
51202
51203 #if SQLITE_OS_WINRT
51204 hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
51205 NULL, protect, nByte, NULL
51206 );
51207 #elif defined(SQLITE_WIN32_HAS_WIDE)
51208 hMap = osCreateFileMappingW(pShmNode->hFile.h,
51209 NULL, protect, 0, nByte, NULL
51210 );
51211 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51212 hMap = osCreateFileMappingA(pShmNode->hFile.h,
51213 NULL, protect, 0, nByte, NULL
51214 );
51215 #endif
 
51216 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51217 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51218 hMap ? "ok" : "failed"));
51219 if( hMap ){
51220 int iOffset = pShmNode->nRegion*szRegion;
@@ -51253,11 +51675,13 @@
51253 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51254 *pp = (void *)&p[iOffsetShift];
51255 }else{
51256 *pp = 0;
51257 }
51258 if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
 
 
51259 sqlite3_mutex_leave(pShmNode->mutex);
51260 return rc;
51261 }
51262
51263 #else
@@ -51594,30 +52018,10 @@
51594 /* caller will handle out of memory */
51595 return zConverted;
51596 }
51597 #endif
51598
51599 /*
51600 ** Convert a UTF-8 filename into whatever form the underlying
51601 ** operating system wants filenames in. Space to hold the result
51602 ** is obtained from malloc and must be freed by the calling
51603 ** function.
51604 */
51605 static void *winConvertFromUtf8Filename(const char *zFilename){
51606 void *zConverted = 0;
51607 if( osIsNT() ){
51608 zConverted = winUtf8ToUnicode(zFilename);
51609 }
51610 #ifdef SQLITE_WIN32_HAS_ANSI
51611 else{
51612 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51613 }
51614 #endif
51615 /* caller will handle out of memory */
51616 return zConverted;
51617 }
51618
51619 /*
51620 ** This function returns non-zero if the specified UTF-8 string buffer
51621 ** ends with a directory separator character or one was successfully
51622 ** added to it.
51623 */
@@ -53067,11 +53471,11 @@
53067 };
53068 #endif
53069
53070 /* Double-check that the aSyscall[] array has been constructed
53071 ** correctly. See ticket [bb3a86e890c8e96ab] */
53072 assert( ArraySize(aSyscall)==80 );
53073
53074 /* get memory map allocation granularity */
53075 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
53076 #if SQLITE_OS_WINRT
53077 osGetNativeSystemInfo(&winSysInfo);
@@ -54125,11 +54529,11 @@
54125 ** Empirical testing showed that the *37 multiplier
54126 ** (an arbitrary prime)in the hash function provided
54127 ** no fewer collisions than the no-op *1. */
54128 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
54129
54130 #define BITVEC_NPTR (BITVEC_USIZE/sizeof(Bitvec *))
54131
54132
54133 /*
54134 ** A bitmap is an instance of the following structure.
54135 **
@@ -54308,11 +54712,11 @@
54308 if (!p) {
54309 return;
54310 }
54311 }
54312 if( p->iSize<=BITVEC_NBIT ){
54313 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
54314 }else{
54315 unsigned int j;
54316 u32 *aiValues = pBuf;
54317 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54318 memset(p->u.aHash, 0, sizeof(p->u.aHash));
@@ -54359,11 +54763,11 @@
54359 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
54360 ** Then the following macros can be used to set, clear, or test
54361 ** individual bits within V.
54362 */
54363 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
54364 #define CLEARBIT(V,I) V[I>>3] &= ~(1<<(I&7))
54365 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
54366
54367 /*
54368 ** This routine runs an extensive test of the Bitvec code.
54369 **
@@ -59211,10 +59615,19 @@
59211 pPager->pInJournal = 0;
59212 releaseAllSavepoints(pPager);
59213
59214 if( pagerUseWal(pPager) ){
59215 assert( !isOpen(pPager->jfd) );
 
 
 
 
 
 
 
 
 
59216 sqlite3WalEndReadTransaction(pPager->pWal);
59217 pPager->eState = PAGER_OPEN;
59218 }else if( !pPager->exclusiveMode ){
59219 int rc; /* Error code returned by pagerUnlockDb() */
59220 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
@@ -65669,10 +66082,15 @@
65669 )
65670
65671 /*
65672 ** An open write-ahead log file is represented by an instance of the
65673 ** following object.
 
 
 
 
 
65674 */
65675 struct Wal {
65676 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
65677 sqlite3_file *pDbFd; /* File handle for the database file */
65678 sqlite3_file *pWalFd; /* File handle for WAL file */
@@ -67194,11 +67612,11 @@
67194 ** or 0 otherwise.
67195 */
67196 static int walEnableBlocking(Wal *pWal){
67197 int res = 0;
67198 if( pWal->db ){
67199 int tmout = pWal->db->busyTimeout;
67200 if( tmout ){
67201 res = walEnableBlockingMs(pWal, tmout);
67202 }
67203 }
67204 return res;
@@ -67580,11 +67998,13 @@
67580 static int walHandleException(Wal *pWal){
67581 if( pWal->exclusiveMode==0 ){
67582 static const int S = 1;
67583 static const int E = (1<<SQLITE_SHM_NLOCK);
67584 int ii;
67585 u32 mUnlock = pWal->lockMask & ~(
 
 
67586 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
67587 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
67588 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
67589 );
67590 for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
@@ -67852,11 +68272,16 @@
67852 }else{
67853 int bWriteLock = pWal->writeLock;
67854 if( bWriteLock
67855 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
67856 ){
67857 pWal->writeLock = 1;
 
 
 
 
 
67858 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
67859 badHdr = walIndexTryHdr(pWal, pChanged);
67860 if( badHdr ){
67861 /* If the wal-index header is still malformed even while holding
67862 ** a WRITE lock, it can only mean that the header is corrupted and
@@ -68637,12 +69062,15 @@
68637 /*
68638 ** Finish with a read transaction. All this does is release the
68639 ** read-lock.
68640 */
68641 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
68642 sqlite3WalEndWriteTransaction(pWal);
 
 
68643 if( pWal->readLock>=0 ){
 
68644 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
68645 pWal->readLock = -1;
68646 }
68647 }
68648
@@ -68831,11 +69259,11 @@
68831 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
68832 /* If the write-lock is already held, then it was obtained before the
68833 ** read-transaction was even opened, making this call a no-op.
68834 ** Return early. */
68835 if( pWal->writeLock ){
68836 assert( !memcmp(&pWal->hdr,(void *)walIndexHdr(pWal),sizeof(WalIndexHdr)) );
68837 return SQLITE_OK;
68838 }
68839 #endif
68840
68841 /* Cannot start a write transaction without first holding a read
@@ -70280,10 +70708,16 @@
70280 ** If a tree that appears to be taller than this is encountered, it is
70281 ** assumed that the database is corrupt.
70282 */
70283 #define BTCURSOR_MAX_DEPTH 20
70284
 
 
 
 
 
 
70285 /*
70286 ** A cursor is a pointer to a particular entry within a particular
70287 ** b-tree within a database file.
70288 **
70289 ** The entry is identified by its MemPage and the index in
@@ -70688,11 +71122,11 @@
70688 ** two or more btrees in common both try to lock all their btrees
70689 ** at the same instant.
70690 */
70691 static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
70692 int i;
70693 int skipOk = 1;
70694 Btree *p;
70695 assert( sqlite3_mutex_held(db->mutex) );
70696 for(i=0; i<db->nDb; i++){
70697 p = db->aDb[i].pBt;
70698 if( p && p->sharable ){
@@ -71834,11 +72268,11 @@
71834 /*
71835 ** Provide flag hints to the cursor.
71836 */
71837 SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
71838 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
71839 pCur->hints = x;
71840 }
71841
71842
71843 #ifndef SQLITE_OMIT_AUTOVACUUM
71844 /*
@@ -72028,18 +72462,19 @@
72028 ** page pPage, return the number of bytes of payload stored locally.
72029 */
72030 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
72031 int maxLocal; /* Maximum amount of payload held locally */
72032 maxLocal = pPage->maxLocal;
 
72033 if( nPayload<=maxLocal ){
72034 return nPayload;
72035 }else{
72036 int minLocal; /* Minimum amount of payload held locally */
72037 int surplus; /* Overflow payload available for local storage */
72038 minLocal = pPage->minLocal;
72039 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4);
72040 return ( surplus <= maxLocal ) ? surplus : minLocal;
72041 }
72042 }
72043
72044 /*
72045 ** The following routines are implementations of the MemPage.xParseCell()
@@ -72145,15 +72580,17 @@
72145 pInfo->nKey = *(i64*)&iKey;
72146 pInfo->nPayload = nPayload;
72147 pInfo->pPayload = pIter;
72148 testcase( nPayload==pPage->maxLocal );
72149 testcase( nPayload==(u32)pPage->maxLocal+1 );
 
 
72150 if( nPayload<=pPage->maxLocal ){
72151 /* This is the (easy) common case where the entire payload fits
72152 ** on the local page. No overflow is required.
72153 */
72154 pInfo->nSize = nPayload + (u16)(pIter - pCell);
72155 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72156 pInfo->nLocal = (u16)nPayload;
72157 }else{
72158 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72159 }
@@ -72182,15 +72619,17 @@
72182 pInfo->nKey = nPayload;
72183 pInfo->nPayload = nPayload;
72184 pInfo->pPayload = pIter;
72185 testcase( nPayload==pPage->maxLocal );
72186 testcase( nPayload==(u32)pPage->maxLocal+1 );
 
 
72187 if( nPayload<=pPage->maxLocal ){
72188 /* This is the (easy) common case where the entire payload fits
72189 ** on the local page. No overflow is required.
72190 */
72191 pInfo->nSize = nPayload + (u16)(pIter - pCell);
72192 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72193 pInfo->nLocal = (u16)nPayload;
72194 }else{
72195 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72196 }
@@ -72725,18 +73164,18 @@
72725 ** that routine will not detect overlap between cells or freeblocks. Nor
72726 ** does it detect cells or freeblocks that encroach into the reserved bytes
72727 ** at the end of the page. So do additional corruption checks inside this
72728 ** routine and return SQLITE_CORRUPT if any problems are found.
72729 */
72730 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){
72731 u16 iPtr; /* Address of ptr to next freeblock */
72732 u16 iFreeBlk; /* Address of the next freeblock */
72733 u8 hdr; /* Page header size. 0 or 100 */
72734 u8 nFrag = 0; /* Reduction in fragmentation */
72735 u16 iOrigSize = iSize; /* Original value of iSize */
72736 u16 x; /* Offset to cell content area */
72737 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */
72738 unsigned char *data = pPage->aData; /* Page content */
72739 u8 *pTmp; /* Temporary ptr into data[] */
72740
72741 assert( pPage->pBt!=0 );
72742 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -72759,11 +73198,11 @@
72759 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
72760 return SQLITE_CORRUPT_PAGE(pPage);
72761 }
72762 iPtr = iFreeBlk;
72763 }
72764 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
72765 return SQLITE_CORRUPT_PAGE(pPage);
72766 }
72767 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
72768
72769 /* At this point:
@@ -72774,11 +73213,11 @@
72774 */
72775 if( iFreeBlk && iEnd+3>=iFreeBlk ){
72776 nFrag = iFreeBlk - iEnd;
72777 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
72778 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
72779 if( iEnd > pPage->pBt->usableSize ){
72780 return SQLITE_CORRUPT_PAGE(pPage);
72781 }
72782 iSize = iEnd - iStart;
72783 iFreeBlk = get2byte(&data[iFreeBlk]);
72784 }
@@ -72795,11 +73234,11 @@
72795 iSize = iEnd - iPtr;
72796 iStart = iPtr;
72797 }
72798 }
72799 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
72800 data[hdr+7] -= nFrag;
72801 }
72802 pTmp = &data[hdr+5];
72803 x = get2byte(pTmp);
72804 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
72805 /* Overwrite deleted information with zeros when the secure_delete
@@ -72816,11 +73255,12 @@
72816 put2byte(&data[hdr+5], iEnd);
72817 }else{
72818 /* Insert the new freeblock into the freelist */
72819 put2byte(&data[iPtr], iStart);
72820 put2byte(&data[iStart], iFreeBlk);
72821 put2byte(&data[iStart+2], iSize);
 
72822 }
72823 pPage->nFree += iOrigSize;
72824 return SQLITE_OK;
72825 }
72826
@@ -73042,11 +73482,11 @@
73042 return SQLITE_CORRUPT_PAGE(pPage);
73043 }
73044 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
73045 pPage->maskPage = (u16)(pBt->pageSize - 1);
73046 pPage->nOverflow = 0;
73047 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize;
73048 pPage->aCellIdx = data + pPage->childPtrSize + 8;
73049 pPage->aDataEnd = pPage->aData + pBt->pageSize;
73050 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
73051 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
73052 ** number of cells on the page. */
@@ -73076,12 +73516,12 @@
73076 ** no entries.
73077 */
73078 static void zeroPage(MemPage *pPage, int flags){
73079 unsigned char *data = pPage->aData;
73080 BtShared *pBt = pPage->pBt;
73081 u8 hdr = pPage->hdrOffset;
73082 u16 first;
73083
73084 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
73085 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
73086 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
73087 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -73094,11 +73534,11 @@
73094 memset(&data[hdr+1], 0, 4);
73095 data[hdr+7] = 0;
73096 put2byte(&data[hdr+5], pBt->usableSize);
73097 pPage->nFree = (u16)(pBt->usableSize - first);
73098 decodeFlags(pPage, flags);
73099 pPage->cellOffset = first;
73100 pPage->aDataEnd = &data[pBt->pageSize];
73101 pPage->aCellIdx = &data[first];
73102 pPage->aDataOfst = &data[pPage->childPtrSize];
73103 pPage->nOverflow = 0;
73104 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
@@ -73880,11 +74320,11 @@
73880 int rc = SQLITE_OK;
73881 int x;
73882 BtShared *pBt = p->pBt;
73883 assert( nReserve>=0 && nReserve<=255 );
73884 sqlite3BtreeEnter(p);
73885 pBt->nReserveWanted = nReserve;
73886 x = pBt->pageSize - pBt->usableSize;
73887 if( nReserve<x ) nReserve = x;
73888 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
73889 sqlite3BtreeLeave(p);
73890 return SQLITE_READONLY;
@@ -73986,11 +74426,11 @@
73986 sqlite3BtreeEnter(p);
73987 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
73988 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
73989 if( newFlag>=0 ){
73990 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
73991 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag;
73992 }
73993 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
73994 sqlite3BtreeLeave(p);
73995 return b;
73996 }
@@ -78434,11 +78874,12 @@
78434 pSrcEnd = pCArray->apEnd[k];
78435 }
78436 }
78437
78438 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78439 pPg->nCell = nCell;
 
78440 pPg->nOverflow = 0;
78441
78442 put2byte(&aData[hdr+1], 0);
78443 put2byte(&aData[hdr+3], pPg->nCell);
78444 put2byte(&aData[hdr+5], pData - aData);
@@ -78681,13 +79122,17 @@
78681 assert( nCell>=0 );
78682 pCellptr = &pPg->aCellIdx[nCell*2];
78683 if( pageInsertArray(
78684 pPg, pBegin, &pData, pCellptr,
78685 iNew+nCell, nNew-nCell, pCArray
78686 ) ) goto editpage_fail;
 
 
 
78687
78688 pPg->nCell = nNew;
 
78689 pPg->nOverflow = 0;
78690
78691 put2byte(&aData[hdr+3], pPg->nCell);
78692 put2byte(&aData[hdr+5], pData - aData);
78693
@@ -78992,11 +79437,11 @@
78992 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
78993 int usableSpace; /* Bytes in pPage beyond the header */
78994 int pageFlags; /* Value of pPage->aData[0] */
78995 int iSpace1 = 0; /* First unused byte of aSpace1[] */
78996 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
78997 int szScratch; /* Size of scratch memory requested */
78998 MemPage *apOld[NB]; /* pPage and up to two siblings */
78999 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
79000 u8 *pRight; /* Location in parent of right-sibling pointer */
79001 u8 *apDiv[NB-1]; /* Divider cells in pParent */
79002 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
@@ -80277,11 +80722,11 @@
80277 if( loc==0 ){
80278 getCellInfo(pCur);
80279 if( pCur->info.nKey==pX->nKey ){
80280 BtreePayload x2;
80281 x2.pData = pX->pKey;
80282 x2.nData = pX->nKey;
80283 x2.nZero = 0;
80284 return btreeOverwriteCell(pCur, &x2);
80285 }
80286 }
80287 }
@@ -80458,11 +80903,11 @@
80458 u32 nIn; /* Size of input buffer aIn[] */
80459 u32 nRem; /* Bytes of data still to copy */
80460
80461 getCellInfo(pSrc);
80462 if( pSrc->info.nPayload<0x80 ){
80463 *(aOut++) = pSrc->info.nPayload;
80464 }else{
80465 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
80466 }
80467 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
80468 nIn = pSrc->info.nLocal;
@@ -80471,11 +80916,11 @@
80471 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80472 }
80473 nRem = pSrc->info.nPayload;
80474 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80475 memcpy(aOut, aIn, nIn);
80476 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace);
80477 return SQLITE_OK;
80478 }else{
80479 int rc = SQLITE_OK;
80480 Pager *pSrcPager = pSrc->pBt->pPager;
80481 u8 *pPgnoOut = 0;
@@ -80483,11 +80928,11 @@
80483 DbPage *pPageIn = 0;
80484 MemPage *pPageOut = 0;
80485 u32 nOut; /* Size of output buffer aOut[] */
80486
80487 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80488 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace);
80489 if( nOut<pSrc->info.nPayload ){
80490 pPgnoOut = &aOut[nOut];
80491 pBt->nPreformatSize += 4;
80492 }
80493
@@ -111531,11 +111976,11 @@
111531 pNew->pNext = pNext;
111532 pNew->pPrior = 0;
111533 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
111534 pNew->iLimit = 0;
111535 pNew->iOffset = 0;
111536 pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
111537 pNew->addrOpenEphm[0] = -1;
111538 pNew->addrOpenEphm[1] = -1;
111539 pNew->nSelectRow = p->nSelectRow;
111540 pNew->pWith = sqlite3WithDup(db, p->pWith);
111541 #ifndef SQLITE_OMIT_WINDOWFUNC
@@ -117481,17 +117926,17 @@
117481 pNew->nTabRef = 1;
117482 pNew->nCol = pTab->nCol;
117483 assert( pNew->nCol>0 );
117484 nAlloc = (((pNew->nCol-1)/8)*8)+8;
117485 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
117486 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
117487 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
117488 if( !pNew->aCol || !pNew->zName ){
117489 assert( db->mallocFailed );
117490 goto exit_begin_add_column;
117491 }
117492 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
117493 for(i=0; i<pNew->nCol; i++){
117494 Column *pCol = &pNew->aCol[i];
117495 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
117496 pCol->hName = sqlite3StrIHash(pCol->zCnName);
117497 }
@@ -118094,11 +118539,17 @@
118094 return SQLITE_NOMEM;
118095 }
118096 if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){
118097 return SQLITE_CORRUPT_BKPT;
118098 }
118099 db->init.iDb = bTemp ? 1 : sqlite3FindDbName(db, zDb);
 
 
 
 
 
 
118100 p->eParseMode = PARSE_MODE_RENAME;
118101 p->db = db;
118102 p->nQueryLoop = 1;
118103 rc = sqlite3RunParser(p, zSql);
118104 if( db->mallocFailed ) rc = SQLITE_NOMEM;
@@ -118161,14 +118612,15 @@
118161 return SQLITE_NOMEM;
118162 }else{
118163 nQuot = sqlite3Strlen30(zQuot)-1;
118164 }
118165
118166 assert( nQuot>=nNew );
118167 zOut = sqlite3DbMallocZero(db, nSql + pRename->nList*nQuot + 1);
118168 }else{
118169 zOut = (char*)sqlite3DbMallocZero(db, (nSql*2+1) * 3);
 
118170 if( zOut ){
118171 zBuf1 = &zOut[nSql*2+1];
118172 zBuf2 = &zOut[nSql*4+2];
118173 }
118174 }
@@ -118176,20 +118628,21 @@
118176 /* At this point pRename->pList contains a list of RenameToken objects
118177 ** corresponding to all tokens in the input SQL that must be replaced
118178 ** with the new column name, or with single-quoted versions of themselves.
118179 ** All that remains is to construct and return the edited SQL string. */
118180 if( zOut ){
118181 int nOut = nSql;
118182 memcpy(zOut, zSql, nSql);
 
118183 while( pRename->pList ){
118184 int iOff; /* Offset of token to replace in zOut */
118185 u32 nReplace;
118186 const char *zReplace;
118187 RenameToken *pBest = renameColumnTokenNext(pRename);
118188
118189 if( zNew ){
118190 if( bQuote==0 && sqlite3IsIdChar(*pBest->t.z) ){
118191 nReplace = nNew;
118192 zReplace = zNew;
118193 }else{
118194 nReplace = nQuot;
118195 zReplace = zQuot;
@@ -118203,18 +118656,19 @@
118203 ** token. This is so that (SELECT "string"'alias') maps to
118204 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
118205 memcpy(zBuf1, pBest->t.z, pBest->t.n);
118206 zBuf1[pBest->t.n] = 0;
118207 sqlite3Dequote(zBuf1);
118208 sqlite3_snprintf(nSql*2, zBuf2, "%Q%s", zBuf1,
 
118209 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
118210 );
118211 zReplace = zBuf2;
118212 nReplace = sqlite3Strlen30(zReplace);
118213 }
118214
118215 iOff = pBest->t.z - zSql;
118216 if( pBest->t.n!=nReplace ){
118217 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
118218 nOut - (iOff + pBest->t.n)
118219 );
118220 nOut += nReplace - pBest->t.n;
@@ -118236,15 +118690,16 @@
118236
118237 /*
118238 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
118239 */
118240 static void renameSetENames(ExprList *pEList, int val){
 
118241 if( pEList ){
118242 int i;
118243 for(i=0; i<pEList->nExpr; i++){
118244 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
118245 pEList->a[i].fg.eEName = val;
118246 }
118247 }
118248 }
118249
118250 /*
@@ -118497,11 +118952,11 @@
118497 sCtx.pTab = pTab;
118498 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
118499 if( sParse.pNewTable ){
118500 if( IsView(sParse.pNewTable) ){
118501 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118502 pSelect->selFlags &= ~SF_View;
118503 sParse.rc = SQLITE_OK;
118504 sqlite3SelectPrep(&sParse, pSelect, 0);
118505 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118506 if( rc==SQLITE_OK ){
118507 sqlite3WalkSelect(&sWalker, pSelect);
@@ -118715,11 +119170,11 @@
118715 NameContext sNC;
118716 memset(&sNC, 0, sizeof(sNC));
118717 sNC.pParse = &sParse;
118718
118719 assert( pSelect->selFlags & SF_View );
118720 pSelect->selFlags &= ~SF_View;
118721 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
118722 if( sParse.nErr ){
118723 rc = sParse.rc;
118724 }else{
118725 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
@@ -118888,11 +119343,11 @@
118888 sWalker.u.pRename = &sCtx;
118889
118890 if( sParse.pNewTable ){
118891 if( IsView(sParse.pNewTable) ){
118892 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118893 pSelect->selFlags &= ~SF_View;
118894 sParse.rc = SQLITE_OK;
118895 sqlite3SelectPrep(&sParse, pSelect, 0);
118896 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118897 if( rc==SQLITE_OK ){
118898 sqlite3WalkSelect(&sWalker, pSelect);
@@ -118987,11 +119442,11 @@
118987 UNUSED_PARAMETER(NotUsed);
118988
118989 if( zDb && zInput ){
118990 int rc;
118991 Parse sParse;
118992 int flags = db->flags;
118993 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
118994 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
118995 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
118996 if( rc==SQLITE_OK ){
118997 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){
@@ -119710,11 +120165,11 @@
119710 }
119711
119712 p->db = db;
119713 p->nEst = sqlite3_value_int64(argv[2]);
119714 p->nRow = 0;
119715 p->nLimit = sqlite3_value_int64(argv[3]);
119716 p->nCol = nCol;
119717 p->nKeyCol = nKeyCol;
119718 p->nSkipAhead = 0;
119719 p->current.anDLt = (tRowcnt*)&p[1];
119720
@@ -121519,10 +121974,17 @@
121519 */
121520 if( rc==SQLITE_OK ){
121521 sqlite3BtreeEnterAll(db);
121522 db->init.iDb = 0;
121523 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
 
 
 
 
 
 
 
121524 if( !REOPEN_AS_MEMDB(db) ){
121525 rc = sqlite3Init(db, &zErrDyn);
121526 }
121527 sqlite3BtreeLeaveAll(db);
121528 assert( zErrDyn==0 || rc!=SQLITE_OK );
@@ -123240,14 +123702,20 @@
123240 ** Convert an table column number into a index column number. That is,
123241 ** for the column iCol in the table (as defined by the CREATE TABLE statement)
123242 ** find the (first) offset of that column in index pIdx. Or return -1
123243 ** if column iCol is not used in index pIdx.
123244 */
123245 SQLITE_PRIVATE i16 sqlite3TableColumnToIndex(Index *pIdx, i16 iCol){
123246 int i;
 
 
 
 
123247 for(i=0; i<pIdx->nColumn; i++){
123248 if( iCol==pIdx->aiColumn[i] ) return i;
 
 
123249 }
123250 return -1;
123251 }
123252
123253 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
@@ -124340,16 +124808,21 @@
124340
124341 /*
124342 ** Resize an Index object to hold N columns total. Return SQLITE_OK
124343 ** on success and SQLITE_NOMEM on an OOM error.
124344 */
124345 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
124346 char *zExtra;
124347 int nByte;
 
124348 if( pIdx->nColumn>=N ) return SQLITE_OK;
 
 
 
 
124349 assert( pIdx->isResized==0 );
124350 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*N;
124351 zExtra = sqlite3DbMallocZero(db, nByte);
124352 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
124353 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
124354 pIdx->azColl = (const char**)zExtra;
124355 zExtra += sizeof(char*)*N;
@@ -124359,11 +124832,11 @@
124359 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
124360 pIdx->aiColumn = (i16*)zExtra;
124361 zExtra += sizeof(i16)*N;
124362 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
124363 pIdx->aSortOrder = (u8*)zExtra;
124364 pIdx->nColumn = N;
124365 pIdx->isResized = 1;
124366 return SQLITE_OK;
124367 }
124368
124369 /*
@@ -124613,11 +125086,11 @@
124613 if( n==0 ){
124614 /* This index is a superset of the primary key */
124615 pIdx->nColumn = pIdx->nKeyCol;
124616 continue;
124617 }
124618 if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
124619 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
124620 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
124621 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
124622 pIdx->aiColumn[j] = pPk->aiColumn[i];
124623 pIdx->azColl[j] = pPk->azColl[i];
@@ -124637,11 +125110,11 @@
124637 nExtra = 0;
124638 for(i=0; i<pTab->nCol; i++){
124639 if( !hasColumn(pPk->aiColumn, nPk, i)
124640 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
124641 }
124642 if( resizeIndexObject(db, pPk, nPk+nExtra) ) return;
124643 for(i=0, j=nPk; i<pTab->nCol; i++){
124644 if( !hasColumn(pPk->aiColumn, j, i)
124645 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
124646 ){
124647 assert( j<pPk->nColumn );
@@ -126021,17 +126494,18 @@
126021 ** of 8-byte aligned space after the Index object and return a
126022 ** pointer to this extra space in *ppExtra.
126023 */
126024 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
126025 sqlite3 *db, /* Database connection */
126026 i16 nCol, /* Total number of columns in the index */
126027 int nExtra, /* Number of bytes of extra space to alloc */
126028 char **ppExtra /* Pointer to the "extra" space */
126029 ){
126030 Index *p; /* Allocated index object */
126031 i64 nByte; /* Bytes of space for Index object + arrays */
126032
 
126033 nByte = ROUND8(sizeof(Index)) + /* Index structure */
126034 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
126035 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
126036 sizeof(i16)*nCol + /* Index.aiColumn */
126037 sizeof(u8)*nCol); /* Index.aSortOrder */
@@ -126040,12 +126514,13 @@
126040 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
126041 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
126042 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
126043 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
126044 p->aSortOrder = (u8*)pExtra;
126045 p->nColumn = nCol;
126046 p->nKeyCol = nCol - 1;
 
126047 *ppExtra = ((char*)p) + nByte;
126048 }
126049 return p;
126050 }
126051
@@ -130629,11 +131104,11 @@
130629
130630 /*
130631 ** Append to pStr text that is the SQL literal representation of the
130632 ** value contained in pValue.
130633 */
130634 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue){
130635 /* As currently implemented, the string must be initially empty.
130636 ** we might relax this requirement in the future, but that will
130637 ** require enhancements to the implementation. */
130638 assert( pStr!=0 && pStr->nChar==0 );
130639
@@ -130677,20 +131152,119 @@
130677 }
130678 break;
130679 }
130680 case SQLITE_TEXT: {
130681 const unsigned char *zArg = sqlite3_value_text(pValue);
130682 sqlite3_str_appendf(pStr, "%Q", zArg);
130683 break;
130684 }
130685 default: {
130686 assert( sqlite3_value_type(pValue)==SQLITE_NULL );
130687 sqlite3_str_append(pStr, "NULL", 4);
130688 break;
130689 }
130690 }
130691 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130692
130693 /*
130694 ** Implementation of the QUOTE() function.
130695 **
130696 ** The quote(X) function returns the text of an SQL literal which is the
@@ -130697,18 +131271,22 @@
130697 ** value of its argument suitable for inclusion into an SQL statement.
130698 ** Strings are surrounded by single-quotes with escapes on interior quotes
130699 ** as needed. BLOBs are encoded as hexadecimal literals. Strings with
130700 ** embedded NUL characters cannot be represented as string literals in SQL
130701 ** and hence the returned string literal is truncated prior to the first NUL.
 
 
 
 
130702 */
130703 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
130704 sqlite3_str str;
130705 sqlite3 *db = sqlite3_context_db_handle(context);
130706 assert( argc==1 );
130707 UNUSED_PARAMETER(argc);
130708 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
130709 sqlite3QuoteValue(&str,argv[0]);
130710 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
130711 SQLITE_DYNAMIC);
130712 if( str.accError!=SQLITE_OK ){
130713 sqlite3_result_null(context);
130714 sqlite3_result_error_code(context, str.accError);
@@ -132275,11 +132853,13 @@
132275 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
132276 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
132277 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
132278 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
132279 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
 
132280 FUNCTION(quote, 1, 0, 0, quoteFunc ),
 
132281 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
132282 VFUNCTION(changes, 0, 0, 0, changes ),
132283 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
132284 FUNCTION(replace, 3, 0, 0, replaceFunc ),
132285 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
@@ -134562,11 +135142,11 @@
134562 }else if( pLeft->pPrior ){
134563 /* In this case set the SF_MultiValue flag only if it was set on pLeft */
134564 f = (f & pLeft->selFlags);
134565 }
134566 pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
134567 pLeft->selFlags &= ~SF_MultiValue;
134568 if( pSelect ){
134569 pSelect->op = TK_ALL;
134570 pSelect->pPrior = pLeft;
134571 pLeft = pSelect;
134572 }
@@ -149534,11 +150114,11 @@
149534 p->pNext = 0;
149535 p->pWith = 0;
149536 #ifndef SQLITE_OMIT_WINDOWFUNC
149537 p->pWinDefn = 0;
149538 #endif
149539 p->selFlags &= ~SF_Compound;
149540 assert( (p->selFlags & SF_Converted)==0 );
149541 p->selFlags |= SF_Converted;
149542 assert( pNew->pPrior!=0 );
149543 pNew->pPrior->pNext = pNew;
149544 pNew->pLimit = 0;
@@ -151140,11 +151720,11 @@
151140 Expr *pTerm;
151141 pPrior = pSub->pPrior;
151142 pSub->pPrior = 0;
151143 pSub->pNext = 0;
151144 pSub->selFlags |= SF_Aggregate;
151145 pSub->selFlags &= ~SF_Compound;
151146 pSub->nSelectRow = 0;
151147 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
151148 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
151149 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
151150 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
@@ -151155,11 +151735,11 @@
151155 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
151156 }
151157 pSub = pPrior;
151158 }
151159 p->pEList->a[0].pExpr = pExpr;
151160 p->selFlags &= ~SF_Aggregate;
151161
151162 #if TREETRACE_ENABLED
151163 if( sqlite3TreeTrace & 0x200 ){
151164 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
151165 sqlite3TreeViewSelect(0, p, 0);
@@ -151362,11 +151942,11 @@
151362 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
151363 p->pOrderBy);
151364 testcase( pParse->earlyCleanup );
151365 p->pOrderBy = 0;
151366 }
151367 p->selFlags &= ~SF_Distinct;
151368 p->selFlags |= SF_NoopOrderBy;
151369 }
151370 sqlite3SelectPrep(pParse, p, 0);
151371 if( pParse->nErr ){
151372 goto select_end;
@@ -151401,11 +151981,11 @@
151401
151402 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
151403 ** and leaving this flag set can cause errors if a compound sub-query
151404 ** in p->pSrc is flattened into this query and this function called
151405 ** again as part of compound SELECT processing. */
151406 p->selFlags &= ~SF_UFSrcCheck;
151407 }
151408
151409 if( pDest->eDest==SRT_Output ){
151410 sqlite3GenerateColumnNames(pParse, p);
151411 }
@@ -151890,11 +152470,11 @@
151890 && OptimizationEnabled(db, SQLITE_GroupByOrder)
151891 #ifndef SQLITE_OMIT_WINDOWFUNC
151892 && p->pWin==0
151893 #endif
151894 ){
151895 p->selFlags &= ~SF_Distinct;
151896 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
151897 if( pGroupBy ){
151898 for(i=0; i<pGroupBy->nExpr; i++){
151899 pGroupBy->a[i].u.x.iOrderByCol = i+1;
151900 }
@@ -164580,10 +165160,12 @@
164580 if( pSrc->colUsed & MASKBIT(BMS-1) ){
164581 nKeyCol += pTable->nCol - BMS + 1;
164582 }
164583
164584 /* Construct the Index object to describe this index */
 
 
164585 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable),
164586 0, &zNotUsed);
164587 if( pIdx==0 ) goto end_auto_index_create;
164588 pLoop->u.btree.pIndex = pIdx;
164589 pIdx->zName = "auto-index";
@@ -172141,11 +172723,11 @@
172141
172142 p->pSrc = 0;
172143 p->pWhere = 0;
172144 p->pGroupBy = 0;
172145 p->pHaving = 0;
172146 p->selFlags &= ~SF_Aggregate;
172147 p->selFlags |= SF_WinRewrite;
172148
172149 /* Create the ORDER BY clause for the sub-select. This is the concatenation
172150 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
172151 ** redundant, remove the ORDER BY from the parent SELECT. */
@@ -178280,12 +178862,12 @@
178280 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
178281 }
178282 if( pRhs ){
178283 pRhs->op = (u8)yymsp[-1].minor.yy502;
178284 pRhs->pPrior = pLhs;
178285 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~SF_MultiValue;
178286 pRhs->selFlags &= ~SF_MultiValue;
178287 if( yymsp[-1].minor.yy502!=TK_ALL ) pParse->hasCompound = 1;
178288 }else{
178289 sqlite3SelectDelete(pParse->db, pLhs);
178290 }
178291 yymsp[-2].minor.yy637 = pRhs;
@@ -183396,10 +183978,13 @@
183396 sqlite3_mutex_enter(db->mutex);
183397 db->busyHandler.xBusyHandler = xBusy;
183398 db->busyHandler.pBusyArg = pArg;
183399 db->busyHandler.nBusy = 0;
183400 db->busyTimeout = 0;
 
 
 
183401 sqlite3_mutex_leave(db->mutex);
183402 return SQLITE_OK;
183403 }
183404
183405 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -183445,15 +184030,46 @@
183445 #endif
183446 if( ms>0 ){
183447 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
183448 (void*)db);
183449 db->busyTimeout = ms;
 
 
 
183450 }else{
183451 sqlite3_busy_handler(db, 0, 0);
183452 }
183453 return SQLITE_OK;
183454 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183455
183456 /*
183457 ** Cause any pending operation to stop at its earliest opportunity.
183458 */
183459 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
@@ -255960,11 +256576,11 @@
255960 int nArg, /* Number of args */
255961 sqlite3_value **apUnused /* Function arguments */
255962 ){
255963 assert( nArg==0 );
255964 UNUSED_PARAM2(nArg, apUnused);
255965 sqlite3_result_text(pCtx, "fts5: 2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc", -1, SQLITE_TRANSIENT);
255966 }
255967
255968 /*
255969 ** Implementation of fts5_locale(LOCALE, TEXT) function.
255970 **
@@ -256185,12 +256801,12 @@
256185 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
256186 ** fts5_test_mi.c is compiled and linked into the executable. And call
256187 ** its entry point to enable the matchinfo() demo. */
256188 #ifdef SQLITE_FTS5_ENABLE_TEST_MI
256189 if( rc==SQLITE_OK ){
256190 extern int sqlite3Fts5TestRegisterMatchinfo(sqlite3*);
256191 rc = sqlite3Fts5TestRegisterMatchinfo(db);
256192 }
256193 #endif
256194
256195 return rc;
256196 }
256197
--- extsrc/sqlite3.c
+++ extsrc/sqlite3.c
@@ -16,11 +16,11 @@
16 ** if you want a wrapper to interface SQLite with your choice of programming
17 ** language. The code for the "sqlite3" command-line shell is also in a
18 ** separate file. This file contains only code for the core SQLite library.
19 **
20 ** The content in this amalgamation comes from Fossil check-in
21 ** e6784af6d50f715338ae3218fc8ba1b89488 with changes in files:
22 **
23 **
24 */
25 #ifndef SQLITE_AMALGAMATION
26 #define SQLITE_CORE 1
@@ -465,11 +465,11 @@
465 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
466 ** [sqlite_version()] and [sqlite_source_id()].
467 */
468 #define SQLITE_VERSION "3.50.0"
469 #define SQLITE_VERSION_NUMBER 3050000
470 #define SQLITE_SOURCE_ID "2025-02-25 18:10:47 e6784af6d50f715338ae3218fc8ba1b894883c27d797f0b7fd2625cac17d9cd7"
471
472 /*
473 ** CAPI3REF: Run-Time Library Version Numbers
474 ** KEYWORDS: sqlite3_version sqlite3_sourceid
475 **
@@ -1479,10 +1479,16 @@
1479 ** to block for up to M milliseconds before failing when attempting to
1480 ** obtain a file lock using the xLock or xShmLock methods of the VFS.
1481 ** The parameter is a pointer to a 32-bit signed integer that contains
1482 ** the value that M is to be set to. Before returning, the 32-bit signed
1483 ** integer is overwritten with the previous value of M.
1484 **
1485 ** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1486 ** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1487 ** VFS to block when taking a SHARED lock to connect to a wal mode database.
1488 ** This is used to implement the functionality associated with
1489 ** SQLITE_SETLK_BLOCK_ON_CONNECT.
1490 **
1491 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1492 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1493 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1494 ** The "data version" for the pager is written into the pointer. The
@@ -1576,10 +1582,11 @@
1582 #define SQLITE_FCNTL_CKPT_START 39
1583 #define SQLITE_FCNTL_EXTERNAL_READER 40
1584 #define SQLITE_FCNTL_CKSM_FILE 41
1585 #define SQLITE_FCNTL_RESET_CACHE 42
1586 #define SQLITE_FCNTL_NULL_IO 43
1587 #define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
1588
1589 /* deprecated names */
1590 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1591 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1592 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3332,10 +3339,48 @@
3339 **
3340 ** See also: [PRAGMA busy_timeout]
3341 */
3342 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3343
3344 /*
3345 ** CAPI3REF: Set the Setlk Timeout
3346 ** METHOD: sqlite3
3347 **
3348 ** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3349 ** the VFS supports blocking locks, it sets the timeout in ms used by
3350 ** eligible locks taken on wal mode databases by the specified database
3351 ** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3352 ** not support blocking locks, this function is a no-op.
3353 **
3354 ** Passing 0 to this function disables blocking locks altogether. Passing
3355 ** -1 to this function requests that the VFS blocks for a long time -
3356 ** indefinitely if possible. The results of passing any other negative value
3357 ** are undefined.
3358 **
3359 ** Internally, each SQLite database handle store two timeout values - the
3360 ** busy-timeout (used for rollback mode databases, or if the VFS does not
3361 ** support blocking locks) and the setlk-timeout (used for blocking locks
3362 ** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3363 ** values, this function sets only the setlk-timeout value. Therefore,
3364 ** to configure separate busy-timeout and setlk-timeout values for a single
3365 ** database handle, call sqlite3_busy_timeout() followed by this function.
3366 **
3367 ** Whenever the number of connections to a wal mode database falls from
3368 ** 1 to 0, the last connection takes an exclusive lock on the database,
3369 ** then checkpoints and deletes the wal file. While it is doing this, any
3370 ** new connection that tries to read from the database fails with an
3371 ** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3372 ** passed to this API, the new connection blocks until the exclusive lock
3373 ** has been released.
3374 */
3375 SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3376
3377 /*
3378 ** CAPI3REF: Flags for sqlite3_setlk_timeout()
3379 */
3380 #define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3381
3382 /*
3383 ** CAPI3REF: Convenience Routines For Running Queries
3384 ** METHOD: sqlite3
3385 **
3386 ** This is a legacy interface that is preserved for backwards compatibility.
@@ -14097,18 +14142,26 @@
14142 ** * Terms in the SET clause of an UPDATE statement
14143 ** * Terms in the result set of a SELECT statement
14144 ** * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
14145 ** * Terms in the VALUES clause of an INSERT statement
14146 **
14147 ** The hard upper limit here is 32767. Most database people will
14148 ** tell you that in a well-normalized database, you usually should
14149 ** not have more than a dozen or so columns in any table. And if
14150 ** that is the case, there is no point in having more than a few
14151 ** dozen values in any of the other situations described above.
14152 **
14153 ** An index can only have SQLITE_MAX_COLUMN columns from the user
14154 ** point of view, but the underlying b-tree that implements the index
14155 ** might have up to twice as many columns in a WITHOUT ROWID table,
14156 ** since must also store the primary key at the end. Hence the
14157 ** column count for Index is u16 instead of i16.
14158 */
14159 #if !defined(SQLITE_MAX_COLUMN)
14160 # define SQLITE_MAX_COLUMN 2000
14161 #elif SQLITE_MAX_COLUMN>32767
14162 # error SQLITE_MAX_COLUMN may not exceed 32767
14163 #endif
14164
14165 /*
14166 ** The maximum length of a single SQL statement in bytes.
14167 **
@@ -18076,10 +18129,14 @@
18129 BusyHandler busyHandler; /* Busy callback */
18130 Db aDbStatic[2]; /* Static space for the 2 default backends */
18131 Savepoint *pSavepoint; /* List of active savepoints */
18132 int nAnalysisLimit; /* Number of index rows to ANALYZE */
18133 int busyTimeout; /* Busy handler timeout, in msec */
18134 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
18135 int setlkTimeout; /* Blocking lock timeout, in msec. -1 -> inf. */
18136 int setlkFlags; /* Flags passed to setlk_timeout() */
18137 #endif
18138 int nSavepoint; /* Number of non-transaction savepoints */
18139 int nStatement; /* Number of nested statement-transactions */
18140 i64 nDeferredCons; /* Net deferred constraints this transaction. */
18141 i64 nDeferredImmCons; /* Net deferred immediate constraints */
18142 int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
@@ -19074,11 +19131,11 @@
19131 Expr *pPartIdxWhere; /* WHERE clause for partial indices */
19132 ExprList *aColExpr; /* Column expressions */
19133 Pgno tnum; /* DB Page containing root of this index */
19134 LogEst szIdxRow; /* Estimated average row size in bytes */
19135 u16 nKeyCol; /* Number of columns forming the key */
19136 u16 nColumn; /* Nr columns in btree. Can be 2*Table.nCol */
19137 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
19138 unsigned idxType:2; /* 0:Normal 1:UNIQUE, 2:PRIMARY KEY, 3:IPK */
19139 unsigned bUnordered:1; /* Use this index for == or IN queries only */
19140 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
19141 unsigned isResized:1; /* True if resizeIndexObject() has been called */
@@ -19412,14 +19469,14 @@
19469 #define EP_Propagate (EP_Collate|EP_Subquery|EP_HasFunc)
19470
19471 /* Macros can be used to test, set, or clear bits in the
19472 ** Expr.flags field.
19473 */
19474 #define ExprHasProperty(E,P) (((E)->flags&(u32)(P))!=0)
19475 #define ExprHasAllProperty(E,P) (((E)->flags&(u32)(P))==(u32)(P))
19476 #define ExprSetProperty(E,P) (E)->flags|=(u32)(P)
19477 #define ExprClearProperty(E,P) (E)->flags&=~(u32)(P)
19478 #define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19479 #define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19480 #define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19481
19482 /* Macros used to ensure that the correct members of unions are accessed
@@ -21223,11 +21280,11 @@
21280 SQLITE_PRIVATE int sqlite3ColumnsFromExprList(Parse*,ExprList*,i16*,Column**);
21281 SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(Parse*,Table*,Select*,char);
21282 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*,char);
21283 SQLITE_PRIVATE void sqlite3OpenSchemaTable(Parse *, int);
21284 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
21285 SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index*, int);
21286 #ifdef SQLITE_OMIT_GENERATED_COLUMNS
21287 # define sqlite3TableColumnToStorage(T,X) (X) /* No-op pass-through */
21288 # define sqlite3StorageColumnToTable(T,X) (X) /* No-op pass-through */
21289 #else
21290 SQLITE_PRIVATE i16 sqlite3TableColumnToStorage(Table*, i16);
@@ -21321,11 +21378,11 @@
21378 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(Parse*,SrcList*);
21379 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
21380 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
21381 SQLITE_PRIVATE void sqlite3ClearOnOrUsing(sqlite3*, OnOrUsing*);
21382 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
21383 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,int,int,char**);
21384 SQLITE_PRIVATE void sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
21385 Expr*, int, int, u8);
21386 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
21387 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
21388 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
@@ -21457,11 +21514,12 @@
21514 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,const IdList*);
21515 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,const Select*,int);
21516 SQLITE_PRIVATE FuncDef *sqlite3FunctionSearch(int,const char*);
21517 SQLITE_PRIVATE void sqlite3InsertBuiltinFuncs(FuncDef*,int);
21518 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,u8,u8);
21519 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum*,sqlite3_value*,int);
21520 SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char*, u32);
21521 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(void);
21522 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
21523 SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void);
21524 SQLITE_PRIVATE void sqlite3RegisterPerConnectionBuiltinFunctions(sqlite3*);
21525 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && !defined(SQLITE_OMIT_JSON)
@@ -22557,10 +22615,13 @@
22615 #ifdef SQLITE_ENABLE_RTREE
22616 "ENABLE_RTREE",
22617 #endif
22618 #ifdef SQLITE_ENABLE_SESSION
22619 "ENABLE_SESSION",
22620 #endif
22621 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
22622 "ENABLE_SETLK_TIMEOUT",
22623 #endif
22624 #ifdef SQLITE_ENABLE_SNAPSHOT
22625 "ENABLE_SNAPSHOT",
22626 #endif
22627 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
@@ -24372,12 +24433,13 @@
24433 u32 nFree = countLookasideSlots(db->lookaside.pFree);
24434 #ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
24435 nInit += countLookasideSlots(db->lookaside.pSmallInit);
24436 nFree += countLookasideSlots(db->lookaside.pSmallFree);
24437 #endif /* SQLITE_OMIT_TWOSIZE_LOOKASIDE */
24438 assert( db->lookaside.nSlot >= nInit+nFree );
24439 if( pHighwater ) *pHighwater = (int)(db->lookaside.nSlot - nInit);
24440 return (int)(db->lookaside.nSlot - (nInit+nFree));
24441 }
24442
24443 /*
24444 ** Query status information for a single database connection
24445 */
@@ -24426,11 +24488,11 @@
24488 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
24489 testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
24490 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
24491 assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
24492 *pCurrent = 0;
24493 *pHighwater = (int)db->lookaside.anStat[op-SQLITE_DBSTATUS_LOOKASIDE_HIT];
24494 if( resetFlag ){
24495 db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
24496 }
24497 break;
24498 }
@@ -31541,21 +31603,21 @@
31603 #define etSTRING 5 /* Strings. %s */
31604 #define etDYNSTRING 6 /* Dynamically allocated strings. %z */
31605 #define etPERCENT 7 /* Percent symbol. %% */
31606 #define etCHARX 8 /* Characters. %c */
31607 /* The rest are extensions, not normally found in printf() */
31608 #define etESCAPE_q 9 /* Strings with '\'' doubled. %q */
31609 #define etESCAPE_Q 10 /* Strings with '\'' doubled and enclosed in '',
31610 NULL pointers replaced by SQL NULL. %Q */
31611 #define etTOKEN 11 /* a pointer to a Token structure */
31612 #define etSRCITEM 12 /* a pointer to a SrcItem */
31613 #define etPOINTER 13 /* The %p conversion */
31614 #define etESCAPE_w 14 /* %w -> Strings with '\"' doubled */
31615 #define etORDINAL 15 /* %r -> 1st, 2nd, 3rd, 4th, etc. English only */
31616 #define etDECIMAL 16 /* %d or %u, but not %x, %o */
31617
31618 #define etINVALID 17 /* Any unrecognized conversion type */
31619
31620
31621 /*
31622 ** An "etByte" is an 8-bit unsigned value.
31623 */
@@ -31590,13 +31652,13 @@
31652 static const et_info fmtinfo[] = {
31653 { 'd', 10, 1, etDECIMAL, 0, 0 },
31654 { 's', 0, 4, etSTRING, 0, 0 },
31655 { 'g', 0, 1, etGENERIC, 30, 0 },
31656 { 'z', 0, 4, etDYNSTRING, 0, 0 },
31657 { 'q', 0, 4, etESCAPE_q, 0, 0 },
31658 { 'Q', 0, 4, etESCAPE_Q, 0, 0 },
31659 { 'w', 0, 4, etESCAPE_w, 0, 0 },
31660 { 'c', 0, 0, etCHARX, 0, 0 },
31661 { 'o', 8, 0, etRADIX, 0, 2 },
31662 { 'u', 10, 0, etDECIMAL, 0, 0 },
31663 { 'x', 16, 0, etRADIX, 16, 1 },
31664 { 'X', 16, 0, etRADIX, 0, 4 },
@@ -32189,29 +32251,11 @@
32251 }else{
32252 buf[0] = 0;
32253 }
32254 }else{
32255 unsigned int ch = va_arg(ap,unsigned int);
32256 length = sqlite3AppendOneUtf8Character(buf, ch);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32257 }
32258 if( precision>1 ){
32259 i64 nPrior = 1;
32260 width -= precision-1;
32261 if( width>1 && !flag_leftjustify ){
@@ -32287,26 +32331,35 @@
32331 /* Adjust width to account for extra bytes in UTF-8 characters */
32332 int ii = length - 1;
32333 while( ii>=0 ) if( (bufpt[ii--] & 0xc0)==0x80 ) width++;
32334 }
32335 break;
32336 case etESCAPE_q: /* %q: Escape ' characters */
32337 case etESCAPE_Q: /* %Q: Escape ' and enclose in '...' */
32338 case etESCAPE_w: { /* %w: Escape " characters */
32339 i64 i, j, k, n;
32340 int needQuote = 0;
32341 char ch;
 
32342 char *escarg;
32343 char q;
32344
32345 if( bArgList ){
32346 escarg = getTextArg(pArgList);
32347 }else{
32348 escarg = va_arg(ap,char*);
32349 }
32350 if( escarg==0 ){
32351 escarg = (xtype==etESCAPE_Q ? "NULL" : "(NULL)");
32352 }else if( xtype==etESCAPE_Q ){
32353 needQuote = 1;
32354 }
32355 if( xtype==etESCAPE_w ){
32356 q = '"';
32357 flag_alternateform = 0;
32358 }else{
32359 q = '\'';
32360 }
32361 /* For %q, %Q, and %w, the precision is the number of bytes (or
32362 ** characters if the ! flags is present) to use from the input.
32363 ** Because of the extra quoting characters inserted, the number
32364 ** of output characters may be larger than the precision.
32365 */
@@ -32315,26 +32368,77 @@
32368 if( ch==q ) n++;
32369 if( flag_altform2 && (ch&0xc0)==0xc0 ){
32370 while( (escarg[i+1]&0xc0)==0x80 ){ i++; }
32371 }
32372 }
32373 if( flag_alternateform ){
32374 /* For %#q, do unistr()-style backslash escapes for
32375 ** all control characters, and for backslash itself.
32376 ** For %#Q, do the same but only if there is at least
32377 ** one control character. */
32378 u32 nBack = 0;
32379 u32 nCtrl = 0;
32380 for(k=0; k<i; k++){
32381 if( escarg[k]=='\\' ){
32382 nBack++;
32383 }else if( escarg[k]<=0x1f ){
32384 nCtrl++;
32385 }
32386 }
32387 if( nCtrl || xtype==etESCAPE_q ){
32388 n += nBack + 5*nCtrl;
32389 if( xtype==etESCAPE_Q ){
32390 n += 10;
32391 needQuote = 2;
32392 }
32393 }else{
32394 flag_alternateform = 0;
32395 }
32396 }
32397 n += i + 3;
32398 if( n>etBUFSIZE ){
32399 bufpt = zExtra = printfTempBuf(pAccum, n);
32400 if( bufpt==0 ) return;
32401 }else{
32402 bufpt = buf;
32403 }
32404 j = 0;
32405 if( needQuote ){
32406 if( needQuote==2 ){
32407 memcpy(&bufpt[j], "unistr('", 8);
32408 j += 8;
32409 }else{
32410 bufpt[j++] = '\'';
32411 }
32412 }
32413 k = i;
32414 if( flag_alternateform ){
32415 for(i=0; i<k; i++){
32416 bufpt[j++] = ch = escarg[i];
32417 if( ch==q ){
32418 bufpt[j++] = ch;
32419 }else if( ch=='\\' ){
32420 bufpt[j++] = '\\';
32421 }else if( ch<=0x1f ){
32422 bufpt[j-1] = '\\';
32423 bufpt[j++] = 'u';
32424 bufpt[j++] = '0';
32425 bufpt[j++] = '0';
32426 bufpt[j++] = ch>=0x10 ? '1' : '0';
32427 bufpt[j++] = "0123456789abcdef"[ch&0xf];
32428 }
32429 }
32430 }else{
32431 for(i=0; i<k; i++){
32432 bufpt[j++] = ch = escarg[i];
32433 if( ch==q ) bufpt[j++] = ch;
32434 }
32435 }
32436 if( needQuote ){
32437 bufpt[j++] = '\'';
32438 if( needQuote==2 ) bufpt[j++] = ')';
32439 }
32440 bufpt[j] = 0;
32441 length = j;
32442 goto adjust_width_for_utf8;
32443 }
32444 case etTOKEN: {
@@ -34828,10 +34932,39 @@
34932 *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0)); \
34933 *zOut++ = (u8)(0x00DC + ((c>>8)&0x03)); \
34934 *zOut++ = (u8)(c&0x00FF); \
34935 } \
34936 }
34937
34938 /*
34939 ** Write a single UTF8 character whose value is v into the
34940 ** buffer starting at zOut. zOut must be sized to hold at
34941 ** least for bytes. Return the number of bytes needed
34942 ** to encode the new character.
34943 */
34944 SQLITE_PRIVATE int sqlite3AppendOneUtf8Character(char *zOut, u32 v){
34945 if( v<0x00080 ){
34946 zOut[0] = (u8)(v & 0xff);
34947 return 1;
34948 }
34949 if( v<0x00800 ){
34950 zOut[0] = 0xc0 + (u8)((v>>6) & 0x1f);
34951 zOut[1] = 0x80 + (u8)(v & 0x3f);
34952 return 2;
34953 }
34954 if( v<0x10000 ){
34955 zOut[0] = 0xe0 + (u8)((v>>12) & 0x0f);
34956 zOut[1] = 0x80 + (u8)((v>>6) & 0x3f);
34957 zOut[2] = 0x80 + (u8)(v & 0x3f);
34958 return 3;
34959 }
34960 zOut[0] = 0xf0 + (u8)((v>>18) & 0x07);
34961 zOut[1] = 0x80 + (u8)((v>>12) & 0x3f);
34962 zOut[2] = 0x80 + (u8)((v>>6) & 0x3f);
34963 zOut[3] = 0x80 + (u8)(v & 0x3f);
34964 return 4;
34965 }
34966
34967 /*
34968 ** Translate a single UTF-8 character. Return the unicode value.
34969 **
34970 ** During translation, assume that the byte that zTerm points
@@ -38911,10 +39044,11 @@
39044 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
39045 unsigned fsFlags; /* cached details from statfs() */
39046 #endif
39047 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
39048 unsigned iBusyTimeout; /* Wait this many millisec on locks */
39049 int bBlockOnConnect; /* True to block for SHARED locks */
39050 #endif
39051 #if OS_VXWORKS
39052 struct vxworksFileId *pId; /* Unique file ID */
39053 #endif
39054 #ifdef SQLITE_DEBUG
@@ -40304,10 +40438,17 @@
40438 pInode->nLock++;
40439 }else{
40440 rc = 0;
40441 }
40442 }else{
40443 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
40444 if( pFile->bBlockOnConnect && pLock->l_type==F_RDLCK
40445 && pLock->l_start==SHARED_FIRST && pLock->l_len==SHARED_SIZE
40446 ){
40447 rc = osFcntl(pFile->h, F_SETLKW, pLock);
40448 }else
40449 #endif
40450 rc = osSetPosixAdvisoryLock(pFile->h, pLock, pFile);
40451 }
40452 return rc;
40453 }
40454
@@ -42665,21 +42806,27 @@
42806 return SQLITE_OK;
42807 }
42808 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42809 case SQLITE_FCNTL_LOCK_TIMEOUT: {
42810 int iOld = pFile->iBusyTimeout;
42811 int iNew = *(int*)pArg;
42812 #if SQLITE_ENABLE_SETLK_TIMEOUT==1
42813 pFile->iBusyTimeout = iNew<0 ? 0x7FFFFFFF : (unsigned)iNew;
42814 #elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42815 pFile->iBusyTimeout = !!(*(int*)pArg);
42816 #else
42817 # error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
42818 #endif
42819 *(int*)pArg = iOld;
42820 return SQLITE_OK;
42821 }
42822 case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
42823 int iNew = *(int*)pArg;
42824 pFile->bBlockOnConnect = iNew;
42825 return SQLITE_OK;
42826 }
42827 #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
42828 #if SQLITE_MAX_MMAP_SIZE>0
42829 case SQLITE_FCNTL_MMAP_SIZE: {
42830 i64 newLimit = *(i64*)pArg;
42831 int rc = SQLITE_OK;
42832 if( newLimit>sqlite3GlobalConfig.mxMmap ){
@@ -43658,11 +43805,11 @@
43805 ** occur later in the above list than the lock being obtained may be
43806 ** held.
43807 **
43808 ** It is not permitted to block on the RECOVER lock.
43809 */
43810 #if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
43811 {
43812 u16 lockMask = (p->exclMask|p->sharedMask);
43813 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43814 (ofst!=2) /* not RECOVER */
43815 && (ofst!=1 || lockMask==0 || lockMask==2)
@@ -47188,11 +47335,21 @@
47335 HANDLE hMap; /* Handle for accessing memory mapping */
47336 void *pMapRegion; /* Area memory mapped */
47337 sqlite3_int64 mmapSize; /* Size of mapped region */
47338 sqlite3_int64 mmapSizeMax; /* Configured FCNTL_MMAP_SIZE value */
47339 #endif
47340 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47341 DWORD iBusyTimeout; /* Wait this many millisec on locks */
47342 int bBlockOnConnect;
47343 #endif
47344 };
47345
47346 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
47347 # define winFileBusyTimeout(pDbFd) pDbFd->iBusyTimeout
47348 #else
47349 # define winFileBusyTimeout(pDbFd) 0
47350 #endif
47351
47352 /*
47353 ** The winVfsAppData structure is used for the pAppData member for all of the
47354 ** Win32 VFS variants.
47355 */
@@ -47623,10 +47780,16 @@
47780 #endif
47781
47782 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
47783 LPWSTR*))aSyscall[25].pCurrent)
47784
47785 /*
47786 ** For GetLastError(), MSDN says:
47787 **
47788 ** Minimum supported client: Windows XP [desktop apps | UWP apps]
47789 ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
47790 */
47791 { "GetLastError", (SYSCALL)GetLastError, 0 },
47792
47793 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
47794
47795 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
@@ -47905,15 +48068,17 @@
48068 #endif
48069
48070 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
48071 DWORD,DWORD))aSyscall[62].pCurrent)
48072
48073 /*
48074 ** For WaitForSingleObject(), MSDN says:
48075 **
48076 ** Minimum supported client: Windows XP [desktop apps | UWP apps]
48077 ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48078 */
48079 { "WaitForSingleObject", (SYSCALL)WaitForSingleObject, 0 },
 
 
 
48080
48081 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
48082 DWORD))aSyscall[63].pCurrent)
48083
48084 #if !SQLITE_OS_WINCE
@@ -48056,10 +48221,44 @@
48221 #endif
48222
48223 #define osFlushViewOfFile \
48224 ((BOOL(WINAPI*)(LPCVOID,SIZE_T))aSyscall[79].pCurrent)
48225
48226 /*
48227 ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CreateEvent()
48228 ** to implement blocking locks with timeouts. MSDN says:
48229 **
48230 ** Minimum supported client: Windows XP [desktop apps | UWP apps]
48231 ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48232 */
48233 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48234 { "CreateEvent", (SYSCALL)CreateEvent, 0 },
48235 #else
48236 { "CreateEvent", (SYSCALL)0, 0 },
48237 #endif
48238
48239 #define osCreateEvent ( \
48240 (HANDLE(WINAPI*) (LPSECURITY_ATTRIBUTES,BOOL,BOOL,LPCSTR)) \
48241 aSyscall[80].pCurrent \
48242 )
48243
48244 /*
48245 ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined, we require CancelIo()
48246 ** for the case where a timeout expires and a lock request must be
48247 ** cancelled.
48248 **
48249 ** Minimum supported client: Windows XP [desktop apps | UWP apps]
48250 ** Minimum supported server: Windows Server 2003 [desktop apps | UWP apps]
48251 */
48252 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
48253 { "CancelIo", (SYSCALL)CancelIo, 0 },
48254 #else
48255 { "CancelIo", (SYSCALL)0, 0 },
48256 #endif
48257
48258 #define osCancelIo ((BOOL(WINAPI*)(HANDLE))aSyscall[81].pCurrent)
48259
48260 }; /* End of the overrideable system calls */
48261
48262 /*
48263 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
48264 ** "win32" VFSes. Return SQLITE_OK upon successfully updating the
@@ -48354,11 +48553,13 @@
48553 (sInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) ? 2 : 1, 0);
48554 #endif
48555 }
48556 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2;
48557 #elif SQLITE_TEST
48558 return osInterlockedCompareExchange(&sqlite3_os_type, 2, 2)==2
48559 || osInterlockedCompareExchange(&sqlite3_os_type, 0, 0)==0
48560 ;
48561 #else
48562 /*
48563 ** NOTE: All sub-platforms where the GetVersionEx[AW] functions are
48564 ** deprecated are always assumed to be based on the NT kernel.
48565 */
@@ -49440,10 +49641,89 @@
49641 return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49642 numBytesHigh);
49643 }
49644 #endif
49645 }
49646
49647 /*
49648 ** Lock a region of nByte bytes starting at offset offset of file hFile.
49649 ** Take an EXCLUSIVE lock if parameter bExclusive is true, or a SHARED lock
49650 ** otherwise. If nMs is greater than zero and the lock cannot be obtained
49651 ** immediately, block for that many ms before giving up.
49652 **
49653 ** This function returns SQLITE_OK if the lock is obtained successfully. If
49654 ** some other process holds the lock, SQLITE_BUSY is returned if nMs==0, or
49655 ** SQLITE_BUSY_TIMEOUT otherwise. Or, if an error occurs, SQLITE_IOERR.
49656 */
49657 static int winHandleLockTimeout(
49658 HANDLE hFile,
49659 DWORD offset,
49660 DWORD nByte,
49661 int bExcl,
49662 DWORD nMs
49663 ){
49664 DWORD flags = LOCKFILE_FAIL_IMMEDIATELY | (bExcl?LOCKFILE_EXCLUSIVE_LOCK:0);
49665 int rc = SQLITE_OK;
49666 BOOL ret;
49667
49668 if( !osIsNT() ){
49669 ret = winLockFile(&hFile, flags, offset, 0, nByte, 0);
49670 }else{
49671 OVERLAPPED ovlp;
49672 memset(&ovlp, 0, sizeof(OVERLAPPED));
49673 ovlp.Offset = offset;
49674
49675 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49676 if( nMs!=0 ){
49677 flags &= ~LOCKFILE_FAIL_IMMEDIATELY;
49678 }
49679 ovlp.hEvent = osCreateEvent(NULL, TRUE, FALSE, NULL);
49680 if( ovlp.hEvent==NULL ){
49681 return SQLITE_IOERR_LOCK;
49682 }
49683 #endif
49684
49685 ret = osLockFileEx(hFile, flags, 0, nByte, 0, &ovlp);
49686
49687 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
49688 /* If SQLITE_ENABLE_SETLK_TIMEOUT is defined, then the file-handle was
49689 ** opened with FILE_FLAG_OVERHEAD specified. In this case, the call to
49690 ** LockFileEx() may fail because the request is still pending. This can
49691 ** happen even if LOCKFILE_FAIL_IMMEDIATELY was specified.
49692 **
49693 ** If nMs is 0, then LOCKFILE_FAIL_IMMEDIATELY was set in the flags
49694 ** passed to LockFileEx(). In this case, if the operation is pending,
49695 ** block indefinitely until it is finished.
49696 **
49697 ** Otherwise, wait for up to nMs ms for the operation to finish. nMs
49698 ** may be set to INFINITE.
49699 */
49700 if( !ret && GetLastError()==ERROR_IO_PENDING ){
49701 DWORD nDelay = (nMs==0 ? INFINITE : nMs);
49702 DWORD res = osWaitForSingleObject(ovlp.hEvent, nDelay);
49703 if( res==WAIT_OBJECT_0 ){
49704 ret = TRUE;
49705 }else if( res==WAIT_TIMEOUT ){
49706 rc = SQLITE_BUSY_TIMEOUT;
49707 }else{
49708 /* Some other error has occurred */
49709 rc = SQLITE_IOERR_LOCK;
49710 }
49711
49712 /* If it is still pending, cancel the LockFileEx() call. */
49713 osCancelIo(hFile);
49714 }
49715
49716 osCloseHandle(ovlp.hEvent);
49717 #endif
49718 }
49719
49720 if( rc==SQLITE_OK && !ret ){
49721 rc = SQLITE_BUSY;
49722 }
49723 return rc;
49724 }
49725
49726 /*
49727 ** Unlock a file region.
49728 */
49729 static BOOL winUnlockFile(
@@ -49471,10 +49751,18 @@
49751 return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
49752 numBytesHigh);
49753 }
49754 #endif
49755 }
49756
49757 /*
49758 ** Remove an nByte lock starting at offset iOff from HANDLE h.
49759 */
49760 static int winHandleUnlock(HANDLE h, int iOff, int nByte){
49761 BOOL ret = winUnlockFile(&h, iOff, 0, nByte, 0);
49762 return (ret ? SQLITE_OK : SQLITE_IOERR_UNLOCK);
49763 }
49764
49765 /*****************************************************************************
49766 ** The next group of routines implement the I/O methods specified
49767 ** by the sqlite3_io_methods object.
49768 ******************************************************************************/
@@ -49485,69 +49773,73 @@
49773 #ifndef INVALID_SET_FILE_POINTER
49774 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
49775 #endif
49776
49777 /*
49778 ** Seek the file handle h to offset nByte of the file.
49779 **
49780 ** If successful, return SQLITE_OK. Or, if an error occurs, return an SQLite
49781 ** error code.
49782 */
49783 static int winHandleSeek(HANDLE h, sqlite3_int64 iOffset){
49784 int rc = SQLITE_OK; /* Return value */
49785
49786 #if !SQLITE_OS_WINRT
49787 LONG upperBits; /* Most sig. 32 bits of new offset */
49788 LONG lowerBits; /* Least sig. 32 bits of new offset */
49789 DWORD dwRet; /* Value returned by SetFilePointer() */
 
 
 
49790
49791 upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
49792 lowerBits = (LONG)(iOffset & 0xffffffff);
49793
49794 dwRet = osSetFilePointer(h, lowerBits, &upperBits, FILE_BEGIN);
49795
49796 /* API oddity: If successful, SetFilePointer() returns a dword
49797 ** containing the lower 32-bits of the new file-offset. Or, if it fails,
49798 ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
49799 ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
49800 ** whether an error has actually occurred, it is also necessary to call
49801 ** GetLastError(). */
49802 if( dwRet==INVALID_SET_FILE_POINTER ){
49803 DWORD lastErrno = osGetLastError();
49804 if( lastErrno!=NO_ERROR ){
49805 rc = SQLITE_IOERR_SEEK;
49806 }
49807 }
49808 #else
49809 /* This implementation works for WinRT. */
 
 
 
 
 
 
 
 
 
 
 
49810 LARGE_INTEGER x; /* The new offset */
49811 BOOL bRet; /* Value returned by SetFilePointerEx() */
49812
49813 x.QuadPart = iOffset;
49814 bRet = osSetFilePointerEx(h, x, 0, FILE_BEGIN);
49815
49816 if(!bRet){
49817 rc = SQLITE_IOERR_SEEK;
49818 }
49819 #endif
49820
49821 OSTRACE(("SEEK file=%p, offset=%lld rc=%s\n", h, iOffset, sqlite3ErrName(rc)));
49822 return rc;
49823 }
49824
49825 /*
49826 ** Move the current position of the file handle passed as the first
49827 ** argument to offset iOffset within the file. If successful, return 0.
49828 ** Otherwise, set pFile->lastErrno and return non-zero.
49829 */
49830 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
49831 int rc;
49832
49833 rc = winHandleSeek(pFile->h, iOffset);
49834 if( rc!=SQLITE_OK ){
49835 pFile->lastErrno = osGetLastError();
49836 winLogError(rc, pFile->lastErrno, "winSeekFile", pFile->zPath);
49837 }
49838 return rc;
49839 }
49840
 
 
 
 
 
49841
49842 #if SQLITE_MAX_MMAP_SIZE>0
49843 /* Forward references to VFS helper methods used for memory mapped files */
49844 static int winMapfile(winFile*, sqlite3_int64);
49845 static int winUnmapfile(winFile*);
@@ -49803,10 +50095,64 @@
50095 }
50096 OSTRACE(("WRITE pid=%lu, pFile=%p, file=%p, rc=SQLITE_OK\n",
50097 osGetCurrentProcessId(), pFile, pFile->h));
50098 return SQLITE_OK;
50099 }
50100
50101 /*
50102 ** Truncate the file opened by handle h to nByte bytes in size.
50103 */
50104 static int winHandleTruncate(HANDLE h, sqlite3_int64 nByte){
50105 int rc = SQLITE_OK; /* Return code */
50106 rc = winHandleSeek(h, nByte);
50107 if( rc==SQLITE_OK ){
50108 if( 0==osSetEndOfFile(h) ){
50109 rc = SQLITE_IOERR_TRUNCATE;
50110 }
50111 }
50112 return rc;
50113 }
50114
50115 /*
50116 ** Determine the size in bytes of the file opened by the handle passed as
50117 ** the first argument.
50118 */
50119 static int winHandleSize(HANDLE h, sqlite3_int64 *pnByte){
50120 int rc = SQLITE_OK;
50121
50122 #if SQLITE_OS_WINRT
50123 FILE_STANDARD_INFO info;
50124 BOOL b;
50125 b = osGetFileInformationByHandleEx(h, FileStandardInfo, &info, sizeof(info));
50126 if( b ){
50127 *pnByte = info.EndOfFile.QuadPart;
50128 }else{
50129 rc = SQLITE_IOERR_FSTAT;
50130 }
50131 #else
50132 DWORD upperBits = 0;
50133 DWORD lowerBits = 0;
50134
50135 assert( pnByte );
50136 lowerBits = osGetFileSize(h, &upperBits);
50137 *pnByte = (((sqlite3_int64)upperBits)<<32) + lowerBits;
50138 if( lowerBits==INVALID_FILE_SIZE && osGetLastError()!=NO_ERROR ){
50139 rc = SQLITE_IOERR_FSTAT;
50140 }
50141 #endif
50142
50143 return rc;
50144 }
50145
50146 /*
50147 ** Close the handle passed as the only argument.
50148 */
50149 static void winHandleClose(HANDLE h){
50150 if( h!=INVALID_HANDLE_VALUE ){
50151 osCloseHandle(h);
50152 }
50153 }
50154
50155 /*
50156 ** Truncate an open file to a specified size
50157 */
50158 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
@@ -50059,31 +50405,32 @@
50405 /*
50406 ** Acquire a reader lock.
50407 ** Different API routines are called depending on whether or not this
50408 ** is Win9x or WinNT.
50409 */
50410 static int winGetReadLock(winFile *pFile, int bBlock){
50411 int res;
50412 DWORD mask = ~(bBlock ? LOCKFILE_FAIL_IMMEDIATELY : 0);
50413 OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
50414 if( osIsNT() ){
50415 #if SQLITE_OS_WINCE
50416 /*
50417 ** NOTE: Windows CE is handled differently here due its lack of the Win32
50418 ** API LockFileEx.
50419 */
50420 res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
50421 #else
50422 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS&mask, SHARED_FIRST, 0,
50423 SHARED_SIZE, 0);
50424 #endif
50425 }
50426 #ifdef SQLITE_WIN32_HAS_ANSI
50427 else{
50428 int lk;
50429 sqlite3_randomness(sizeof(lk), &lk);
50430 pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
50431 res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS&mask,
50432 SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
50433 }
50434 #endif
50435 if( res == 0 ){
50436 pFile->lastErrno = osGetLastError();
@@ -50174,50 +50521,66 @@
50521 */
50522 assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
50523 assert( locktype!=PENDING_LOCK );
50524 assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
50525
50526 /* Lock the PENDING_LOCK byte if we need to acquire an EXCLUSIVE lock or
50527 ** a SHARED lock. If we are acquiring a SHARED lock, the acquisition of
50528 ** the PENDING_LOCK byte is temporary.
50529 */
50530 newLocktype = pFile->locktype;
50531 if( locktype==SHARED_LOCK
50532 || (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
50533 ){
50534 int cnt = 3;
50535
50536 /* Flags for the LockFileEx() call. This should be an exclusive lock if
50537 ** this call is to obtain EXCLUSIVE, or a shared lock if this call is to
50538 ** obtain SHARED. */
50539 int flags = LOCKFILE_FAIL_IMMEDIATELY;
50540 if( locktype==EXCLUSIVE_LOCK ){
50541 flags |= LOCKFILE_EXCLUSIVE_LOCK;
50542 }
50543 while( cnt>0 ){
50544 /* Try 3 times to get the pending lock. This is needed to work
50545 ** around problems caused by indexing and/or anti-virus software on
50546 ** Windows systems.
50547 **
50548 ** If you are using this code as a model for alternative VFSes, do not
50549 ** copy this retry logic. It is a hack intended for Windows only. */
50550 res = winLockFile(&pFile->h, flags, PENDING_BYTE, 0, 1, 0);
50551 if( res ) break;
50552
50553 lastErrno = osGetLastError();
50554 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
50555 pFile->h, cnt, res
50556 ));
50557
50558 if( lastErrno==ERROR_INVALID_HANDLE ){
50559 pFile->lastErrno = lastErrno;
50560 rc = SQLITE_IOERR_LOCK;
50561 OSTRACE(("LOCK-FAIL file=%p, count=%d, rc=%s\n",
50562 pFile->h, cnt, sqlite3ErrName(rc)
50563 ));
50564 return rc;
50565 }
50566
50567 cnt--;
50568 if( cnt>0 ) sqlite3_win32_sleep(1);
50569 }
50570 gotPendingLock = res;
 
 
 
50571 }
50572
50573 /* Acquire a shared lock
50574 */
50575 if( locktype==SHARED_LOCK && res ){
50576 assert( pFile->locktype==NO_LOCK );
50577 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50578 res = winGetReadLock(pFile, pFile->bBlockOnConnect);
50579 #else
50580 res = winGetReadLock(pFile, 0);
50581 #endif
50582 if( res ){
50583 newLocktype = SHARED_LOCK;
50584 }else{
50585 lastErrno = osGetLastError();
50586 }
@@ -50251,11 +50614,11 @@
50614 SHARED_SIZE, 0);
50615 if( res ){
50616 newLocktype = EXCLUSIVE_LOCK;
50617 }else{
50618 lastErrno = osGetLastError();
50619 winGetReadLock(pFile, 0);
50620 }
50621 }
50622
50623 /* If we are holding a PENDING lock that ought to be released, then
50624 ** release it now.
@@ -50331,11 +50694,11 @@
50694 OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
50695 pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
50696 type = pFile->locktype;
50697 if( type>=EXCLUSIVE_LOCK ){
50698 winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
50699 if( locktype==SHARED_LOCK && !winGetReadLock(pFile, 0) ){
50700 /* This should never happen. We should always be able to
50701 ** reacquire the read lock */
50702 rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
50703 "winUnlock", pFile->zPath);
50704 }
@@ -50541,10 +50904,32 @@
50904 }
50905 OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
50906 return rc;
50907 }
50908 #endif
50909
50910 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
50911 case SQLITE_FCNTL_LOCK_TIMEOUT: {
50912 int iOld = pFile->iBusyTimeout;
50913 int iNew = *(int*)pArg;
50914 #if SQLITE_ENABLE_SETLK_TIMEOUT==1
50915 pFile->iBusyTimeout = (iNew < 0) ? INFINITE : (DWORD)iNew;
50916 #elif SQLITE_ENABLE_SETLK_TIMEOUT==2
50917 pFile->iBusyTimeout = (DWORD)(!!iNew);
50918 #else
50919 # error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
50920 #endif
50921 *(int*)pArg = iOld;
50922 return SQLITE_OK;
50923 }
50924 case SQLITE_FCNTL_BLOCK_ON_CONNECT: {
50925 int iNew = *(int*)pArg;
50926 pFile->bBlockOnConnect = iNew;
50927 return SQLITE_OK;
50928 }
50929 #endif /* SQLITE_ENABLE_SETLK_TIMEOUT */
50930
50931 }
50932 OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
50933 return SQLITE_NOTFOUND;
50934 }
50935
@@ -50621,36 +51006,39 @@
51006 ** nRef
51007 ** pNext
51008 **
51009 ** The following fields are read-only after the object is created:
51010 **
 
51011 ** zFilename
51012 **
51013 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
51014 ** winShmMutexHeld() is true when reading or writing any other field
51015 ** in this structure.
51016 **
51017 ** File-handle hSharedShm is used to (a) take the DMS lock, (b) truncate
51018 ** the *-shm file if the DMS-locking protocol demands it, and (c) map
51019 ** regions of the *-shm file into memory using MapViewOfFile() or
51020 ** similar. Other locks are taken by individual clients using the
51021 ** winShm.hShm handles.
51022 */
51023 struct winShmNode {
51024 sqlite3_mutex *mutex; /* Mutex to access this object */
51025 char *zFilename; /* Name of the file */
51026 HANDLE hSharedShm; /* File handle open on zFilename */
51027
51028 int isUnlocked; /* DMS lock has not yet been obtained */
51029 int isReadonly; /* True if read-only */
51030 int szRegion; /* Size of shared-memory regions */
51031 int nRegion; /* Size of array apRegion */
 
 
51032
51033 struct ShmRegion {
51034 HANDLE hMap; /* File handle from CreateFileMapping */
51035 void *pMap;
51036 } *aRegion;
51037 DWORD lastErrno; /* The Windows errno from the last I/O error */
51038
51039 int nRef; /* Number of winShm objects pointing to this */
 
51040 winShmNode *pNext; /* Next in list of all winShmNode objects */
51041 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51042 u8 nextShmId; /* Next available winShm.id value */
51043 #endif
51044 };
@@ -50662,27 +51050,19 @@
51050 */
51051 static winShmNode *winShmNodeList = 0;
51052
51053 /*
51054 ** Structure used internally by this VFS to record the state of an
51055 ** open shared memory connection. There is one such structure for each
51056 ** winFile open on a wal mode database.
 
 
 
 
 
 
 
 
51057 */
51058 struct winShm {
51059 winShmNode *pShmNode; /* The underlying winShmNode object */
 
 
51060 u16 sharedMask; /* Mask of shared locks held */
51061 u16 exclMask; /* Mask of exclusive locks held */
51062 HANDLE hShm; /* File-handle on *-shm file. For locking. */
51063 int bReadonly; /* True if hShm is opened read-only */
51064 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51065 u8 id; /* Id of this connection with its winShmNode */
51066 #endif
51067 };
51068
@@ -50690,54 +51070,10 @@
51070 ** Constants used for locking
51071 */
51072 #define WIN_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */
51073 #define WIN_SHM_DMS (WIN_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */
51074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51075 /* Forward references to VFS methods */
51076 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
51077 static int winDelete(sqlite3_vfs *,const char*,int);
51078
51079 /*
@@ -50765,15 +51101,11 @@
51101 bRc = osCloseHandle(p->aRegion[i].hMap);
51102 OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
51103 osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
51104 UNUSED_VARIABLE_VALUE(bRc);
51105 }
51106 winHandleClose(p->hSharedShm);
 
 
 
 
51107 if( deleteFlag ){
51108 SimulateIOErrorBenign(1);
51109 sqlite3BeginBenignMalloc();
51110 winDelete(pVfs, p->zFilename, 0);
51111 sqlite3EndBenignMalloc();
@@ -50787,46 +51119,166 @@
51119 }
51120 }
51121 }
51122
51123 /*
51124 ** The DMS lock has not yet been taken on the shm file associated with
51125 ** pShmNode. Take the lock. Truncate the *-shm file if required.
51126 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
 
 
 
 
51127 */
51128 static int winLockSharedMemory(winShmNode *pShmNode, DWORD nMs){
51129 HANDLE h = pShmNode->hSharedShm;
51130 int rc = SQLITE_OK;
51131
51132 assert( sqlite3_mutex_held(pShmNode->mutex) );
51133 rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 1, 0);
51134 if( rc==SQLITE_OK ){
51135 /* We have an EXCLUSIVE lock on the DMS byte. This means that this
51136 ** is the first process to open the file. Truncate it to zero bytes
51137 ** in this case. */
51138 if( pShmNode->isReadonly ){
51139 rc = SQLITE_READONLY_CANTINIT;
51140 }else{
51141 rc = winHandleTruncate(h, 0);
51142 }
51143
51144 /* Release the EXCLUSIVE lock acquired above. */
51145 winUnlockFile(&h, WIN_SHM_DMS, 0, 1, 0);
51146 }else if( (rc & 0xFF)==SQLITE_BUSY ){
51147 rc = SQLITE_OK;
51148 }
51149
51150 if( rc==SQLITE_OK ){
51151 /* Take a SHARED lock on the DMS byte. */
51152 rc = winHandleLockTimeout(h, WIN_SHM_DMS, 1, 0, nMs);
51153 if( rc==SQLITE_OK ){
51154 pShmNode->isUnlocked = 0;
51155 }
51156 }
51157
51158 return rc;
51159 }
51160
51161
51162 /*
51163 ** Convert a UTF-8 filename into whatever form the underlying
51164 ** operating system wants filenames in. Space to hold the result
51165 ** is obtained from malloc and must be freed by the calling
51166 ** function.
51167 */
51168 static void *winConvertFromUtf8Filename(const char *zFilename){
51169 void *zConverted = 0;
51170 if( osIsNT() ){
51171 zConverted = winUtf8ToUnicode(zFilename);
51172 }
51173 #ifdef SQLITE_WIN32_HAS_ANSI
51174 else{
51175 zConverted = winUtf8ToMbcs(zFilename, osAreFileApisANSI());
51176 }
51177 #endif
51178 /* caller will handle out of memory */
51179 return zConverted;
51180 }
51181
51182 /*
51183 ** This function is used to open a handle on a *-shm file.
51184 **
51185 ** If SQLITE_ENABLE_SETLK_TIMEOUT is defined at build time, then the file
51186 ** is opened with FILE_FLAG_OVERLAPPED specified. If not, it is not.
51187 */
51188 static int winHandleOpen(
51189 const char *zUtf8, /* File to open */
51190 int *pbReadonly, /* IN/OUT: True for readonly handle */
51191 HANDLE *ph /* OUT: New HANDLE for file */
51192 ){
51193 int rc = SQLITE_OK;
51194 void *zConverted = 0;
51195 int bReadonly = *pbReadonly;
51196 HANDLE h = INVALID_HANDLE_VALUE;
51197
51198 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
51199 const DWORD flag_overlapped = FILE_FLAG_OVERLAPPED;
51200 #else
51201 const DWORD flag_overlapped = 0;
51202 #endif
51203
51204 /* Convert the filename to the system encoding. */
51205 zConverted = winConvertFromUtf8Filename(zUtf8);
51206 if( zConverted==0 ){
51207 OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8));
51208 rc = SQLITE_IOERR_NOMEM_BKPT;
51209 goto winopenfile_out;
51210 }
51211
51212 /* Ensure the file we are trying to open is not actually a directory. */
51213 if( winIsDir(zConverted) ){
51214 OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8));
51215 rc = SQLITE_CANTOPEN_ISDIR;
51216 goto winopenfile_out;
51217 }
51218
51219 /* TODO: platforms.
51220 ** TODO: retry-on-ioerr.
51221 */
51222 if( osIsNT() ){
51223 #if SQLITE_OS_WINRT
51224 CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
51225 memset(&extendedParameters, 0, sizeof(extendedParameters));
51226 extendedParameters.dwSize = sizeof(extendedParameters);
51227 extendedParameters.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
51228 extendedParameters.dwFileFlags = flag_overlapped;
51229 extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
51230 h = osCreateFile2((LPCWSTR)zConverted,
51231 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)),/* dwDesiredAccess */
51232 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51233 OPEN_ALWAYS, /* dwCreationDisposition */
51234 &extendedParameters
51235 );
51236 #else
51237 h = osCreateFileW((LPCWSTR)zConverted, /* lpFileName */
51238 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51239 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51240 NULL, /* lpSecurityAttributes */
51241 OPEN_ALWAYS, /* dwCreationDisposition */
51242 FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51243 NULL
51244 );
51245 #endif
51246 }else{
51247 /* Due to pre-processor directives earlier in this file,
51248 ** SQLITE_WIN32_HAS_ANSI is always defined if osIsNT() is false. */
51249 #ifdef SQLITE_WIN32_HAS_ANSI
51250 h = osCreateFileA((LPCSTR)zConverted,
51251 (GENERIC_READ | (bReadonly ? 0 : GENERIC_WRITE)), /* dwDesiredAccess */
51252 FILE_SHARE_READ | FILE_SHARE_WRITE, /* dwShareMode */
51253 NULL, /* lpSecurityAttributes */
51254 OPEN_ALWAYS, /* dwCreationDisposition */
51255 FILE_ATTRIBUTE_NORMAL|flag_overlapped,
51256 NULL
51257 );
51258 #endif
51259 }
51260
51261 if( h==INVALID_HANDLE_VALUE ){
51262 if( bReadonly==0 ){
51263 bReadonly = 1;
51264 rc = winHandleOpen(zUtf8, &bReadonly, &h);
51265 }else{
51266 rc = SQLITE_CANTOPEN_BKPT;
51267 }
51268 }
51269
51270 winopenfile_out:
51271 sqlite3_free(zConverted);
51272 *pbReadonly = bReadonly;
51273 *ph = h;
51274 return rc;
51275 }
51276
51277
51278 /*
51279 ** Open the shared-memory area associated with database file pDbFd.
51280 */
51281 static int winOpenSharedMemory(winFile *pDbFd){
51282 struct winShm *p; /* The connection to be opened */
51283 winShmNode *pShmNode = 0; /* The underlying mmapped file */
51284 int rc = SQLITE_OK; /* Result code */
@@ -50834,102 +51286,87 @@
51286 int nName; /* Size of zName in bytes */
51287
51288 assert( pDbFd->pShm==0 ); /* Not previously opened */
51289
51290 /* Allocate space for the new sqlite3_shm object. Also speculatively
51291 ** allocate space for a new winShmNode and filename. */
 
51292 p = sqlite3MallocZero( sizeof(*p) );
51293 if( p==0 ) return SQLITE_IOERR_NOMEM_BKPT;
51294 nName = sqlite3Strlen30(pDbFd->zPath);
51295 pNew = sqlite3MallocZero( sizeof(*pShmNode) + (i64)nName + 17 );
51296 if( pNew==0 ){
51297 sqlite3_free(p);
51298 return SQLITE_IOERR_NOMEM_BKPT;
51299 }
51300 pNew->zFilename = (char*)&pNew[1];
51301 pNew->hSharedShm = INVALID_HANDLE_VALUE;
51302 pNew->isUnlocked = 1;
51303 sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
51304 sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
51305
51306 /* Open a file-handle on the *-shm file for this connection. This file-handle
51307 ** is only used for locking. The mapping of the *-shm file is created using
51308 ** the shared file handle in winShmNode.hSharedShm. */
51309 p->bReadonly = sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0);
51310 rc = winHandleOpen(pNew->zFilename, &p->bReadonly, &p->hShm);
51311
51312 /* Look to see if there is an existing winShmNode that can be used.
51313 ** If no matching winShmNode currently exists, then create a new one. */
 
51314 winShmEnterMutex();
51315 for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
51316 /* TBD need to come up with better match here. Perhaps
51317 ** use FILE_ID_BOTH_DIR_INFO Structure. */
 
51318 if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
51319 }
51320 if( pShmNode==0 ){
 
 
 
 
 
51321 pShmNode = pNew;
 
 
 
 
51322
51323 /* Allocate a mutex for this winShmNode object, if one is required. */
51324 if( sqlite3GlobalConfig.bCoreMutex ){
51325 pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
51326 if( pShmNode->mutex==0 ) rc = SQLITE_IOERR_NOMEM_BKPT;
51327 }
51328
51329 /* Open a file-handle to use for mappings, and for the DMS lock. */
51330 if( rc==SQLITE_OK ){
51331 HANDLE h = INVALID_HANDLE_VALUE;
51332 pShmNode->isReadonly = p->bReadonly;
51333 rc = winHandleOpen(pNew->zFilename, &pShmNode->isReadonly, &h);
51334 pShmNode->hSharedShm = h;
51335 }
51336
51337 /* If successful, link the new winShmNode into the global list. If an
51338 ** error occurred, free the object. */
51339 if( rc==SQLITE_OK ){
51340 pShmNode->pNext = winShmNodeList;
51341 winShmNodeList = pShmNode;
51342 pNew = 0;
51343 }else{
51344 sqlite3_mutex_free(pShmNode->mutex);
51345 if( pShmNode->hSharedShm!=INVALID_HANDLE_VALUE ){
51346 osCloseHandle(pShmNode->hSharedShm);
51347 }
51348 }
51349 }
51350
51351 /* If no error has occurred, link the winShm object to the winShmNode and
51352 ** the winShm to pDbFd. */
51353 if( rc==SQLITE_OK ){
51354 p->pShmNode = pShmNode;
51355 pShmNode->nRef++;
51356 #if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
51357 p->id = pShmNode->nextShmId++;
51358 #endif
51359 pDbFd->pShm = p;
51360 }else if( p ){
51361 winHandleClose(p->hShm);
51362 sqlite3_free(p);
51363 }
51364
51365 assert( rc!=SQLITE_OK || pShmNode->isUnlocked==0 || pShmNode->nRegion==0 );
51366 winShmLeaveMutex();
51367 sqlite3_free(pNew);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51368 return rc;
51369 }
51370
51371 /*
51372 ** Close a connection to shared-memory. Delete the underlying
@@ -50940,38 +51377,33 @@
51377 int deleteFlag /* Delete after closing if true */
51378 ){
51379 winFile *pDbFd; /* Database holding shared-memory */
51380 winShm *p; /* The connection to be closed */
51381 winShmNode *pShmNode; /* The underlying shared-memory file */
 
51382
51383 pDbFd = (winFile*)fd;
51384 p = pDbFd->pShm;
51385 if( p==0 ) return SQLITE_OK;
51386 if( p->hShm!=INVALID_HANDLE_VALUE ){
51387 osCloseHandle(p->hShm);
51388 }
51389
51390 pShmNode = p->pShmNode;
51391 winShmEnterMutex();
 
 
 
 
 
 
 
 
 
 
51392
51393 /* If pShmNode->nRef has reached 0, then close the underlying
51394 ** shared-memory file, too. */
 
51395 assert( pShmNode->nRef>0 );
51396 pShmNode->nRef--;
51397 if( pShmNode->nRef==0 ){
51398 winShmPurge(pDbFd->pVfs, deleteFlag);
51399 }
51400 winShmLeaveMutex();
51401
51402 /* Free the connection p */
51403 sqlite3_free(p);
51404 pDbFd->pShm = 0;
51405 return SQLITE_OK;
51406 }
51407
51408 /*
51409 ** Change the lock state for a shared-memory segment.
@@ -50982,14 +51414,13 @@
51414 int n, /* Number of locks to acquire or release */
51415 int flags /* What to do with the lock */
51416 ){
51417 winFile *pDbFd = (winFile*)fd; /* Connection holding shared memory */
51418 winShm *p = pDbFd->pShm; /* The shared memory being locked */
 
51419 winShmNode *pShmNode;
51420 int rc = SQLITE_OK; /* Result code */
51421 u16 mask = (u16)((1U<<(ofst+n)) - (1U<<ofst)); /* Mask of locks to [un]take */
51422
51423 if( p==0 ) return SQLITE_IOERR_SHMLOCK;
51424 pShmNode = p->pShmNode;
51425 if( NEVER(pShmNode==0) ) return SQLITE_IOERR_SHMLOCK;
51426
@@ -50999,89 +51430,86 @@
51430 || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
51431 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
51432 || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
51433 assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
51434
51435 /* Check that, if this to be a blocking lock, no locks that occur later
51436 ** in the following list than the lock being obtained are already held:
51437 **
51438 ** 1. Checkpointer lock (ofst==1).
51439 ** 2. Write lock (ofst==0).
51440 ** 3. Read locks (ofst>=3 && ofst<SQLITE_SHM_NLOCK).
51441 **
51442 ** In other words, if this is a blocking lock, none of the locks that
51443 ** occur later in the above list than the lock being obtained may be
51444 ** held.
51445 **
51446 ** It is not permitted to block on the RECOVER lock.
51447 */
51448 #if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && defined(SQLITE_DEBUG)
51449 {
51450 u16 lockMask = (p->exclMask|p->sharedMask);
51451 assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
51452 (ofst!=2) /* not RECOVER */
51453 && (ofst!=1 || lockMask==0 || lockMask==2)
51454 && (ofst!=0 || lockMask<3)
51455 && (ofst<3 || lockMask<(1<<ofst))
51456 ));
51457 }
51458 #endif
51459
51460 /* Check if there is any work to do. There are three cases:
51461 **
51462 ** a) An unlock operation where there are locks to unlock,
51463 ** b) An shared lock where the requested lock is not already held
51464 ** c) An exclusive lock where the requested lock is not already held
51465 **
51466 ** The SQLite core never requests an exclusive lock that it already holds.
51467 ** This is assert()ed immediately below. */
51468 assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)
51469 || 0==(p->exclMask & mask)
51470 );
51471 if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
51472 || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
51473 || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK))
51474 ){
51475
51476 if( flags & SQLITE_SHM_UNLOCK ){
51477 /* Case (a) - unlock. */
51478
51479 assert( (p->exclMask & p->sharedMask)==0 );
51480 assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
51481 assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
51482
51483 rc = winHandleUnlock(p->hShm, ofst+WIN_SHM_BASE, n);
51484
51485 /* If successful, also clear the bits in sharedMask/exclMask */
51486 if( rc==SQLITE_OK ){
51487 p->exclMask = (p->exclMask & ~mask);
51488 p->sharedMask = (p->sharedMask & ~mask);
51489 }
51490 }else{
51491 int bExcl = ((flags & SQLITE_SHM_EXCLUSIVE) ? 1 : 0);
51492 DWORD nMs = winFileBusyTimeout(pDbFd);
51493 rc = winHandleLockTimeout(p->hShm, ofst+WIN_SHM_BASE, n, bExcl, nMs);
51494 if( rc==SQLITE_OK ){
51495 if( bExcl ){
51496 p->exclMask = (p->exclMask | mask);
51497 }else{
51498 p->sharedMask = (p->sharedMask | mask);
51499 }
51500 }
51501 }
51502 }
51503
51504 OSTRACE((
51505 "SHM-LOCK(%d,%d,%d) pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x,"
51506 " rc=%s\n",
51507 ofst, n, flags,
51508 osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
51509 sqlite3ErrName(rc))
51510 );
 
 
 
51511 return rc;
51512 }
51513
51514 /*
51515 ** Implement a memory barrier or memory fence on shared memory.
@@ -51139,17 +51567,19 @@
51567 }
51568 pShmNode = pShm->pShmNode;
51569
51570 sqlite3_mutex_enter(pShmNode->mutex);
51571 if( pShmNode->isUnlocked ){
51572 /* Take the DMS lock. */
51573 assert( pShmNode->nRegion==0 );
51574 rc = winLockSharedMemory(pShmNode, winFileBusyTimeout(pDbFd));
51575 if( rc!=SQLITE_OK ) goto shmpage_out;
 
51576 }
51577
51578 assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
 
51579 if( pShmNode->nRegion<=iRegion ){
51580 HANDLE hShared = pShmNode->hSharedShm;
51581 struct ShmRegion *apNew; /* New aRegion[] array */
51582 int nByte = (iRegion+1)*szRegion; /* Minimum required file size */
51583 sqlite3_int64 sz; /* Current size of wal-index file */
51584
51585 pShmNode->szRegion = szRegion;
@@ -51156,35 +51586,32 @@
51586
51587 /* The requested region is not mapped into this processes address space.
51588 ** Check to see if it has been allocated (i.e. if the wal-index file is
51589 ** large enough to contain the requested region).
51590 */
51591 rc = winHandleSize(hShared, &sz);
51592 if( rc!=SQLITE_OK ){
51593 rc = winLogError(rc, osGetLastError(), "winShmMap1", pDbFd->zPath);
 
51594 goto shmpage_out;
51595 }
51596
51597 if( sz<nByte ){
51598 /* The requested memory region does not exist. If isWrite is set to
51599 ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
51600 **
51601 ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
51602 ** the requested memory region. */
 
51603 if( !isWrite ) goto shmpage_out;
51604 rc = winHandleTruncate(hShared, nByte);
51605 if( rc!=SQLITE_OK ){
51606 rc = winLogError(rc, osGetLastError(), "winShmMap2", pDbFd->zPath);
 
51607 goto shmpage_out;
51608 }
51609 }
51610
51611 /* Map the requested memory region into this processes address space. */
51612 apNew = (struct ShmRegion*)sqlite3_realloc64(
51613 pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
51614 );
51615 if( !apNew ){
51616 rc = SQLITE_IOERR_NOMEM_BKPT;
51617 goto shmpage_out;
@@ -51199,22 +51626,17 @@
51626 while( pShmNode->nRegion<=iRegion ){
51627 HANDLE hMap = NULL; /* file-mapping handle */
51628 void *pMap = 0; /* Mapped memory region */
51629
51630 #if SQLITE_OS_WINRT
51631 hMap = osCreateFileMappingFromApp(hShared, NULL, protect, nByte, NULL);
 
 
51632 #elif defined(SQLITE_WIN32_HAS_WIDE)
51633 hMap = osCreateFileMappingW(hShared, NULL, protect, 0, nByte, NULL);
 
 
51634 #elif defined(SQLITE_WIN32_HAS_ANSI) && SQLITE_WIN32_CREATEFILEMAPPINGA
51635 hMap = osCreateFileMappingA(hShared, NULL, protect, 0, nByte, NULL);
 
 
51636 #endif
51637
51638 OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
51639 osGetCurrentProcessId(), pShmNode->nRegion, nByte,
51640 hMap ? "ok" : "failed"));
51641 if( hMap ){
51642 int iOffset = pShmNode->nRegion*szRegion;
@@ -51253,11 +51675,13 @@
51675 char *p = (char *)pShmNode->aRegion[iRegion].pMap;
51676 *pp = (void *)&p[iOffsetShift];
51677 }else{
51678 *pp = 0;
51679 }
51680 if( pShmNode->isReadonly && rc==SQLITE_OK ){
51681 rc = SQLITE_READONLY;
51682 }
51683 sqlite3_mutex_leave(pShmNode->mutex);
51684 return rc;
51685 }
51686
51687 #else
@@ -51594,30 +52018,10 @@
52018 /* caller will handle out of memory */
52019 return zConverted;
52020 }
52021 #endif
52022
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52023 /*
52024 ** This function returns non-zero if the specified UTF-8 string buffer
52025 ** ends with a directory separator character or one was successfully
52026 ** added to it.
52027 */
@@ -53067,11 +53471,11 @@
53471 };
53472 #endif
53473
53474 /* Double-check that the aSyscall[] array has been constructed
53475 ** correctly. See ticket [bb3a86e890c8e96ab] */
53476 assert( ArraySize(aSyscall)==82 );
53477
53478 /* get memory map allocation granularity */
53479 memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
53480 #if SQLITE_OS_WINRT
53481 osGetNativeSystemInfo(&winSysInfo);
@@ -54125,11 +54529,11 @@
54529 ** Empirical testing showed that the *37 multiplier
54530 ** (an arbitrary prime)in the hash function provided
54531 ** no fewer collisions than the no-op *1. */
54532 #define BITVEC_HASH(X) (((X)*1)%BITVEC_NINT)
54533
54534 #define BITVEC_NPTR ((u32)(BITVEC_USIZE/sizeof(Bitvec *)))
54535
54536
54537 /*
54538 ** A bitmap is an instance of the following structure.
54539 **
@@ -54308,11 +54712,11 @@
54712 if (!p) {
54713 return;
54714 }
54715 }
54716 if( p->iSize<=BITVEC_NBIT ){
54717 p->u.aBitmap[i/BITVEC_SZELEM] &= ~(BITVEC_TELEM)(1<<(i&(BITVEC_SZELEM-1)));
54718 }else{
54719 unsigned int j;
54720 u32 *aiValues = pBuf;
54721 memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
54722 memset(p->u.aHash, 0, sizeof(p->u.aHash));
@@ -54359,11 +54763,11 @@
54763 ** up to N bits. Let I be an integer between 0 and N. 0<=I<N.
54764 ** Then the following macros can be used to set, clear, or test
54765 ** individual bits within V.
54766 */
54767 #define SETBIT(V,I) V[I>>3] |= (1<<(I&7))
54768 #define CLEARBIT(V,I) V[I>>3] &= ~(BITVEC_TELEM)(1<<(I&7))
54769 #define TESTBIT(V,I) (V[I>>3]&(1<<(I&7)))!=0
54770
54771 /*
54772 ** This routine runs an extensive test of the Bitvec code.
54773 **
@@ -59211,10 +59615,19 @@
59615 pPager->pInJournal = 0;
59616 releaseAllSavepoints(pPager);
59617
59618 if( pagerUseWal(pPager) ){
59619 assert( !isOpen(pPager->jfd) );
59620 if( pPager->eState==PAGER_ERROR ){
59621 /* If an IO error occurs in wal.c while attempting to wrap the wal file,
59622 ** then the Wal object may be holding a write-lock but no read-lock.
59623 ** This call ensures that the write-lock is dropped as well. We cannot
59624 ** have sqlite3WalEndReadTransaction() drop the write-lock, as it once
59625 ** did, because this would break "BEGIN EXCLUSIVE" handling for
59626 ** SQLITE_ENABLE_SETLK_TIMEOUT builds. */
59627 sqlite3WalEndWriteTransaction(pPager->pWal);
59628 }
59629 sqlite3WalEndReadTransaction(pPager->pWal);
59630 pPager->eState = PAGER_OPEN;
59631 }else if( !pPager->exclusiveMode ){
59632 int rc; /* Error code returned by pagerUnlockDb() */
59633 int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
@@ -65669,10 +66082,15 @@
66082 )
66083
66084 /*
66085 ** An open write-ahead log file is represented by an instance of the
66086 ** following object.
66087 **
66088 ** writeLock:
66089 ** This is usually set to 1 whenever the WRITER lock is held. However,
66090 ** if it is set to 2, then the WRITER lock is held but must be released
66091 ** by walHandleException() if a SEH exception is thrown.
66092 */
66093 struct Wal {
66094 sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */
66095 sqlite3_file *pDbFd; /* File handle for the database file */
66096 sqlite3_file *pWalFd; /* File handle for WAL file */
@@ -67194,11 +67612,11 @@
67612 ** or 0 otherwise.
67613 */
67614 static int walEnableBlocking(Wal *pWal){
67615 int res = 0;
67616 if( pWal->db ){
67617 int tmout = pWal->db->setlkTimeout;
67618 if( tmout ){
67619 res = walEnableBlockingMs(pWal, tmout);
67620 }
67621 }
67622 return res;
@@ -67580,11 +67998,13 @@
67998 static int walHandleException(Wal *pWal){
67999 if( pWal->exclusiveMode==0 ){
68000 static const int S = 1;
68001 static const int E = (1<<SQLITE_SHM_NLOCK);
68002 int ii;
68003 u32 mUnlock;
68004 if( pWal->writeLock==2 ) pWal->writeLock = 0;
68005 mUnlock = pWal->lockMask & ~(
68006 (pWal->readLock<0 ? 0 : (S << WAL_READ_LOCK(pWal->readLock)))
68007 | (pWal->writeLock ? (E << WAL_WRITE_LOCK) : 0)
68008 | (pWal->ckptLock ? (E << WAL_CKPT_LOCK) : 0)
68009 );
68010 for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
@@ -67852,11 +68272,16 @@
68272 }else{
68273 int bWriteLock = pWal->writeLock;
68274 if( bWriteLock
68275 || SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1))
68276 ){
68277 /* If the write-lock was just obtained, set writeLock to 2 instead of
68278 ** the usual 1. This causes walIndexPage() to behave as if the
68279 ** write-lock were held (so that it allocates new pages as required),
68280 ** and walHandleException() to unlock the write-lock if a SEH exception
68281 ** is thrown. */
68282 if( !bWriteLock ) pWal->writeLock = 2;
68283 if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
68284 badHdr = walIndexTryHdr(pWal, pChanged);
68285 if( badHdr ){
68286 /* If the wal-index header is still malformed even while holding
68287 ** a WRITE lock, it can only mean that the header is corrupted and
@@ -68637,12 +69062,15 @@
69062 /*
69063 ** Finish with a read transaction. All this does is release the
69064 ** read-lock.
69065 */
69066 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
69067 #ifndef SQLITE_ENABLE_SETLK_TIMEOUT
69068 assert( pWal->writeLock==0 || pWal->readLock<0 );
69069 #endif
69070 if( pWal->readLock>=0 ){
69071 sqlite3WalEndWriteTransaction(pWal);
69072 walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
69073 pWal->readLock = -1;
69074 }
69075 }
69076
@@ -68831,11 +69259,11 @@
69259 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
69260 /* If the write-lock is already held, then it was obtained before the
69261 ** read-transaction was even opened, making this call a no-op.
69262 ** Return early. */
69263 if( pWal->writeLock ){
69264 assert( !memcmp(&pWal->hdr,(void*)pWal->apWiData[0],sizeof(WalIndexHdr)) );
69265 return SQLITE_OK;
69266 }
69267 #endif
69268
69269 /* Cannot start a write transaction without first holding a read
@@ -70280,10 +70708,16 @@
70708 ** If a tree that appears to be taller than this is encountered, it is
70709 ** assumed that the database is corrupt.
70710 */
70711 #define BTCURSOR_MAX_DEPTH 20
70712
70713 /*
70714 ** Maximum amount of storage local to a database page, regardless of
70715 ** page size.
70716 */
70717 #define BT_MAX_LOCAL 65501 /* 65536 - 35 */
70718
70719 /*
70720 ** A cursor is a pointer to a particular entry within a particular
70721 ** b-tree within a database file.
70722 **
70723 ** The entry is identified by its MemPage and the index in
@@ -70688,11 +71122,11 @@
71122 ** two or more btrees in common both try to lock all their btrees
71123 ** at the same instant.
71124 */
71125 static void SQLITE_NOINLINE btreeEnterAll(sqlite3 *db){
71126 int i;
71127 u8 skipOk = 1;
71128 Btree *p;
71129 assert( sqlite3_mutex_held(db->mutex) );
71130 for(i=0; i<db->nDb; i++){
71131 p = db->aDb[i].pBt;
71132 if( p && p->sharable ){
@@ -71834,11 +72268,11 @@
72268 /*
72269 ** Provide flag hints to the cursor.
72270 */
72271 SQLITE_PRIVATE void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){
72272 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 );
72273 pCur->hints = (u8)x;
72274 }
72275
72276
72277 #ifndef SQLITE_OMIT_AUTOVACUUM
72278 /*
@@ -72028,18 +72462,19 @@
72462 ** page pPage, return the number of bytes of payload stored locally.
72463 */
72464 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){
72465 int maxLocal; /* Maximum amount of payload held locally */
72466 maxLocal = pPage->maxLocal;
72467 assert( nPayload>=0 );
72468 if( nPayload<=maxLocal ){
72469 return (int)nPayload;
72470 }else{
72471 int minLocal; /* Minimum amount of payload held locally */
72472 int surplus; /* Overflow payload available for local storage */
72473 minLocal = pPage->minLocal;
72474 surplus = (int)(minLocal +(nPayload - minLocal)%(pPage->pBt->usableSize-4));
72475 return (surplus <= maxLocal) ? surplus : minLocal;
72476 }
72477 }
72478
72479 /*
72480 ** The following routines are implementations of the MemPage.xParseCell()
@@ -72145,15 +72580,17 @@
72580 pInfo->nKey = *(i64*)&iKey;
72581 pInfo->nPayload = nPayload;
72582 pInfo->pPayload = pIter;
72583 testcase( nPayload==pPage->maxLocal );
72584 testcase( nPayload==(u32)pPage->maxLocal+1 );
72585 assert( nPayload>=0 );
72586 assert( pPage->maxLocal <= BT_MAX_LOCAL );
72587 if( nPayload<=pPage->maxLocal ){
72588 /* This is the (easy) common case where the entire payload fits
72589 ** on the local page. No overflow is required.
72590 */
72591 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72592 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72593 pInfo->nLocal = (u16)nPayload;
72594 }else{
72595 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72596 }
@@ -72182,15 +72619,17 @@
72619 pInfo->nKey = nPayload;
72620 pInfo->nPayload = nPayload;
72621 pInfo->pPayload = pIter;
72622 testcase( nPayload==pPage->maxLocal );
72623 testcase( nPayload==(u32)pPage->maxLocal+1 );
72624 assert( nPayload>=0 );
72625 assert( pPage->maxLocal <= BT_MAX_LOCAL );
72626 if( nPayload<=pPage->maxLocal ){
72627 /* This is the (easy) common case where the entire payload fits
72628 ** on the local page. No overflow is required.
72629 */
72630 pInfo->nSize = (u16)nPayload + (u16)(pIter - pCell);
72631 if( pInfo->nSize<4 ) pInfo->nSize = 4;
72632 pInfo->nLocal = (u16)nPayload;
72633 }else{
72634 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo);
72635 }
@@ -72725,18 +73164,18 @@
73164 ** that routine will not detect overlap between cells or freeblocks. Nor
73165 ** does it detect cells or freeblocks that encroach into the reserved bytes
73166 ** at the end of the page. So do additional corruption checks inside this
73167 ** routine and return SQLITE_CORRUPT if any problems are found.
73168 */
73169 static int freeSpace(MemPage *pPage, int iStart, int iSize){
73170 int iPtr; /* Address of ptr to next freeblock */
73171 int iFreeBlk; /* Address of the next freeblock */
73172 u8 hdr; /* Page header size. 0 or 100 */
73173 int nFrag = 0; /* Reduction in fragmentation */
73174 int iOrigSize = iSize; /* Original value of iSize */
73175 int x; /* Offset to cell content area */
73176 int iEnd = iStart + iSize; /* First byte past the iStart buffer */
73177 unsigned char *data = pPage->aData; /* Page content */
73178 u8 *pTmp; /* Temporary ptr into data[] */
73179
73180 assert( pPage->pBt!=0 );
73181 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -72759,11 +73198,11 @@
73198 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */
73199 return SQLITE_CORRUPT_PAGE(pPage);
73200 }
73201 iPtr = iFreeBlk;
73202 }
73203 if( iFreeBlk>(int)pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */
73204 return SQLITE_CORRUPT_PAGE(pPage);
73205 }
73206 assert( iFreeBlk>iPtr || iFreeBlk==0 || CORRUPT_DB );
73207
73208 /* At this point:
@@ -72774,11 +73213,11 @@
73213 */
73214 if( iFreeBlk && iEnd+3>=iFreeBlk ){
73215 nFrag = iFreeBlk - iEnd;
73216 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage);
73217 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]);
73218 if( iEnd > (int)pPage->pBt->usableSize ){
73219 return SQLITE_CORRUPT_PAGE(pPage);
73220 }
73221 iSize = iEnd - iStart;
73222 iFreeBlk = get2byte(&data[iFreeBlk]);
73223 }
@@ -72795,11 +73234,11 @@
73234 iSize = iEnd - iPtr;
73235 iStart = iPtr;
73236 }
73237 }
73238 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage);
73239 data[hdr+7] -= (u8)nFrag;
73240 }
73241 pTmp = &data[hdr+5];
73242 x = get2byte(pTmp);
73243 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){
73244 /* Overwrite deleted information with zeros when the secure_delete
@@ -72816,11 +73255,12 @@
73255 put2byte(&data[hdr+5], iEnd);
73256 }else{
73257 /* Insert the new freeblock into the freelist */
73258 put2byte(&data[iPtr], iStart);
73259 put2byte(&data[iStart], iFreeBlk);
73260 assert( iSize>=0 && iSize<=0xffff );
73261 put2byte(&data[iStart+2], (u16)iSize);
73262 }
73263 pPage->nFree += iOrigSize;
73264 return SQLITE_OK;
73265 }
73266
@@ -73042,11 +73482,11 @@
73482 return SQLITE_CORRUPT_PAGE(pPage);
73483 }
73484 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
73485 pPage->maskPage = (u16)(pBt->pageSize - 1);
73486 pPage->nOverflow = 0;
73487 pPage->cellOffset = (u16)(pPage->hdrOffset + 8 + pPage->childPtrSize);
73488 pPage->aCellIdx = data + pPage->childPtrSize + 8;
73489 pPage->aDataEnd = pPage->aData + pBt->pageSize;
73490 pPage->aDataOfst = pPage->aData + pPage->childPtrSize;
73491 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the
73492 ** number of cells on the page. */
@@ -73076,12 +73516,12 @@
73516 ** no entries.
73517 */
73518 static void zeroPage(MemPage *pPage, int flags){
73519 unsigned char *data = pPage->aData;
73520 BtShared *pBt = pPage->pBt;
73521 int hdr = pPage->hdrOffset;
73522 int first;
73523
73524 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno || CORRUPT_DB );
73525 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
73526 assert( sqlite3PagerGetData(pPage->pDbPage) == data );
73527 assert( sqlite3PagerIswriteable(pPage->pDbPage) );
@@ -73094,11 +73534,11 @@
73534 memset(&data[hdr+1], 0, 4);
73535 data[hdr+7] = 0;
73536 put2byte(&data[hdr+5], pBt->usableSize);
73537 pPage->nFree = (u16)(pBt->usableSize - first);
73538 decodeFlags(pPage, flags);
73539 pPage->cellOffset = (u16)first;
73540 pPage->aDataEnd = &data[pBt->pageSize];
73541 pPage->aCellIdx = &data[first];
73542 pPage->aDataOfst = &data[pPage->childPtrSize];
73543 pPage->nOverflow = 0;
73544 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
@@ -73880,11 +74320,11 @@
74320 int rc = SQLITE_OK;
74321 int x;
74322 BtShared *pBt = p->pBt;
74323 assert( nReserve>=0 && nReserve<=255 );
74324 sqlite3BtreeEnter(p);
74325 pBt->nReserveWanted = (u8)nReserve;
74326 x = pBt->pageSize - pBt->usableSize;
74327 if( nReserve<x ) nReserve = x;
74328 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
74329 sqlite3BtreeLeave(p);
74330 return SQLITE_READONLY;
@@ -73986,11 +74426,11 @@
74426 sqlite3BtreeEnter(p);
74427 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 );
74428 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) );
74429 if( newFlag>=0 ){
74430 p->pBt->btsFlags &= ~BTS_FAST_SECURE;
74431 p->pBt->btsFlags |= (u16)(BTS_SECURE_DELETE*newFlag);
74432 }
74433 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE;
74434 sqlite3BtreeLeave(p);
74435 return b;
74436 }
@@ -78434,11 +78874,12 @@
78874 pSrcEnd = pCArray->apEnd[k];
78875 }
78876 }
78877
78878 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */
78879 assert( nCell < 10922 );
78880 pPg->nCell = (u16)nCell;
78881 pPg->nOverflow = 0;
78882
78883 put2byte(&aData[hdr+1], 0);
78884 put2byte(&aData[hdr+3], pPg->nCell);
78885 put2byte(&aData[hdr+5], pData - aData);
@@ -78681,13 +79122,17 @@
79122 assert( nCell>=0 );
79123 pCellptr = &pPg->aCellIdx[nCell*2];
79124 if( pageInsertArray(
79125 pPg, pBegin, &pData, pCellptr,
79126 iNew+nCell, nNew-nCell, pCArray
79127 )
79128 ){
79129 goto editpage_fail;
79130 }
79131
79132 assert( nNew < 10922 );
79133 pPg->nCell = (u16)nNew;
79134 pPg->nOverflow = 0;
79135
79136 put2byte(&aData[hdr+3], pPg->nCell);
79137 put2byte(&aData[hdr+5], pData - aData);
79138
@@ -78992,11 +79437,11 @@
79437 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */
79438 int usableSpace; /* Bytes in pPage beyond the header */
79439 int pageFlags; /* Value of pPage->aData[0] */
79440 int iSpace1 = 0; /* First unused byte of aSpace1[] */
79441 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */
79442 u64 szScratch; /* Size of scratch memory requested */
79443 MemPage *apOld[NB]; /* pPage and up to two siblings */
79444 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */
79445 u8 *pRight; /* Location in parent of right-sibling pointer */
79446 u8 *apDiv[NB-1]; /* Divider cells in pParent */
79447 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */
@@ -80277,11 +80722,11 @@
80722 if( loc==0 ){
80723 getCellInfo(pCur);
80724 if( pCur->info.nKey==pX->nKey ){
80725 BtreePayload x2;
80726 x2.pData = pX->pKey;
80727 x2.nData = (int)pX->nKey; assert( pX->nKey<=0x7fffffff );
80728 x2.nZero = 0;
80729 return btreeOverwriteCell(pCur, &x2);
80730 }
80731 }
80732 }
@@ -80458,11 +80903,11 @@
80903 u32 nIn; /* Size of input buffer aIn[] */
80904 u32 nRem; /* Bytes of data still to copy */
80905
80906 getCellInfo(pSrc);
80907 if( pSrc->info.nPayload<0x80 ){
80908 *(aOut++) = (u8)pSrc->info.nPayload;
80909 }else{
80910 aOut += sqlite3PutVarint(aOut, pSrc->info.nPayload);
80911 }
80912 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey);
80913 nIn = pSrc->info.nLocal;
@@ -80471,11 +80916,11 @@
80916 return SQLITE_CORRUPT_PAGE(pSrc->pPage);
80917 }
80918 nRem = pSrc->info.nPayload;
80919 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){
80920 memcpy(aOut, aIn, nIn);
80921 pBt->nPreformatSize = nIn + (int)(aOut - pBt->pTmpSpace);
80922 return SQLITE_OK;
80923 }else{
80924 int rc = SQLITE_OK;
80925 Pager *pSrcPager = pSrc->pBt->pPager;
80926 u8 *pPgnoOut = 0;
@@ -80483,11 +80928,11 @@
80928 DbPage *pPageIn = 0;
80929 MemPage *pPageOut = 0;
80930 u32 nOut; /* Size of output buffer aOut[] */
80931
80932 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload);
80933 pBt->nPreformatSize = (int)nOut + (int)(aOut - pBt->pTmpSpace);
80934 if( nOut<pSrc->info.nPayload ){
80935 pPgnoOut = &aOut[nOut];
80936 pBt->nPreformatSize += 4;
80937 }
80938
@@ -111531,11 +111976,11 @@
111976 pNew->pNext = pNext;
111977 pNew->pPrior = 0;
111978 pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
111979 pNew->iLimit = 0;
111980 pNew->iOffset = 0;
111981 pNew->selFlags = p->selFlags & ~(u32)SF_UsesEphemeral;
111982 pNew->addrOpenEphm[0] = -1;
111983 pNew->addrOpenEphm[1] = -1;
111984 pNew->nSelectRow = p->nSelectRow;
111985 pNew->pWith = sqlite3WithDup(db, p->pWith);
111986 #ifndef SQLITE_OMIT_WINDOWFUNC
@@ -117481,17 +117926,17 @@
117926 pNew->nTabRef = 1;
117927 pNew->nCol = pTab->nCol;
117928 assert( pNew->nCol>0 );
117929 nAlloc = (((pNew->nCol-1)/8)*8)+8;
117930 assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
117931 pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*(u32)nAlloc);
117932 pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
117933 if( !pNew->aCol || !pNew->zName ){
117934 assert( db->mallocFailed );
117935 goto exit_begin_add_column;
117936 }
117937 memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*(size_t)pNew->nCol);
117938 for(i=0; i<pNew->nCol; i++){
117939 Column *pCol = &pNew->aCol[i];
117940 pCol->zCnName = sqlite3DbStrDup(db, pCol->zCnName);
117941 pCol->hName = sqlite3StrIHash(pCol->zCnName);
117942 }
@@ -118094,11 +118539,17 @@
118539 return SQLITE_NOMEM;
118540 }
118541 if( sqlite3StrNICmp(zSql,"CREATE ",7)!=0 ){
118542 return SQLITE_CORRUPT_BKPT;
118543 }
118544 if( bTemp ){
118545 db->init.iDb = 1;
118546 }else{
118547 int iDb = sqlite3FindDbName(db, zDb);
118548 assert( iDb>=0 && iDb<=0xff );
118549 db->init.iDb = (u8)iDb;
118550 }
118551 p->eParseMode = PARSE_MODE_RENAME;
118552 p->db = db;
118553 p->nQueryLoop = 1;
118554 rc = sqlite3RunParser(p, zSql);
118555 if( db->mallocFailed ) rc = SQLITE_NOMEM;
@@ -118161,14 +118612,15 @@
118612 return SQLITE_NOMEM;
118613 }else{
118614 nQuot = sqlite3Strlen30(zQuot)-1;
118615 }
118616
118617 assert( nQuot>=nNew && nSql>=0 && nNew>=0 );
118618 zOut = sqlite3DbMallocZero(db, (u64)(nSql + pRename->nList*nQuot + 1));
118619 }else{
118620 assert( nSql>0 );
118621 zOut = (char*)sqlite3DbMallocZero(db, (u64)(nSql*2+1) * 3);
118622 if( zOut ){
118623 zBuf1 = &zOut[nSql*2+1];
118624 zBuf2 = &zOut[nSql*4+2];
118625 }
118626 }
@@ -118176,20 +118628,21 @@
118628 /* At this point pRename->pList contains a list of RenameToken objects
118629 ** corresponding to all tokens in the input SQL that must be replaced
118630 ** with the new column name, or with single-quoted versions of themselves.
118631 ** All that remains is to construct and return the edited SQL string. */
118632 if( zOut ){
118633 i64 nOut = nSql;
118634 assert( nSql>0 );
118635 memcpy(zOut, zSql, (size_t)nSql);
118636 while( pRename->pList ){
118637 int iOff; /* Offset of token to replace in zOut */
118638 i64 nReplace;
118639 const char *zReplace;
118640 RenameToken *pBest = renameColumnTokenNext(pRename);
118641
118642 if( zNew ){
118643 if( bQuote==0 && sqlite3IsIdChar(*(u8*)pBest->t.z) ){
118644 nReplace = nNew;
118645 zReplace = zNew;
118646 }else{
118647 nReplace = nQuot;
118648 zReplace = zQuot;
@@ -118203,18 +118656,19 @@
118656 ** token. This is so that (SELECT "string"'alias') maps to
118657 ** (SELECT 'string' 'alias'), and not (SELECT 'string''alias'). */
118658 memcpy(zBuf1, pBest->t.z, pBest->t.n);
118659 zBuf1[pBest->t.n] = 0;
118660 sqlite3Dequote(zBuf1);
118661 assert( nSql < 0x15555554 /* otherwise malloc would have failed */ );
118662 sqlite3_snprintf((int)(nSql*2), zBuf2, "%Q%s", zBuf1,
118663 pBest->t.z[pBest->t.n]=='\'' ? " " : ""
118664 );
118665 zReplace = zBuf2;
118666 nReplace = sqlite3Strlen30(zReplace);
118667 }
118668
118669 iOff = (int)(pBest->t.z - zSql);
118670 if( pBest->t.n!=nReplace ){
118671 memmove(&zOut[iOff + nReplace], &zOut[iOff + pBest->t.n],
118672 nOut - (iOff + pBest->t.n)
118673 );
118674 nOut += nReplace - pBest->t.n;
@@ -118236,15 +118690,16 @@
118690
118691 /*
118692 ** Set all pEList->a[].fg.eEName fields in the expression-list to val.
118693 */
118694 static void renameSetENames(ExprList *pEList, int val){
118695 assert( val==ENAME_NAME || val==ENAME_TAB || val==ENAME_SPAN );
118696 if( pEList ){
118697 int i;
118698 for(i=0; i<pEList->nExpr; i++){
118699 assert( val==ENAME_NAME || pEList->a[i].fg.eEName==ENAME_NAME );
118700 pEList->a[i].fg.eEName = val&0x3;
118701 }
118702 }
118703 }
118704
118705 /*
@@ -118497,11 +118952,11 @@
118952 sCtx.pTab = pTab;
118953 if( rc!=SQLITE_OK ) goto renameColumnFunc_done;
118954 if( sParse.pNewTable ){
118955 if( IsView(sParse.pNewTable) ){
118956 Select *pSelect = sParse.pNewTable->u.view.pSelect;
118957 pSelect->selFlags &= ~(u32)SF_View;
118958 sParse.rc = SQLITE_OK;
118959 sqlite3SelectPrep(&sParse, pSelect, 0);
118960 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
118961 if( rc==SQLITE_OK ){
118962 sqlite3WalkSelect(&sWalker, pSelect);
@@ -118715,11 +119170,11 @@
119170 NameContext sNC;
119171 memset(&sNC, 0, sizeof(sNC));
119172 sNC.pParse = &sParse;
119173
119174 assert( pSelect->selFlags & SF_View );
119175 pSelect->selFlags &= ~(u32)SF_View;
119176 sqlite3SelectPrep(&sParse, pTab->u.view.pSelect, &sNC);
119177 if( sParse.nErr ){
119178 rc = sParse.rc;
119179 }else{
119180 sqlite3WalkSelect(&sWalker, pTab->u.view.pSelect);
@@ -118888,11 +119343,11 @@
119343 sWalker.u.pRename = &sCtx;
119344
119345 if( sParse.pNewTable ){
119346 if( IsView(sParse.pNewTable) ){
119347 Select *pSelect = sParse.pNewTable->u.view.pSelect;
119348 pSelect->selFlags &= ~(u32)SF_View;
119349 sParse.rc = SQLITE_OK;
119350 sqlite3SelectPrep(&sParse, pSelect, 0);
119351 rc = (db->mallocFailed ? SQLITE_NOMEM : sParse.rc);
119352 if( rc==SQLITE_OK ){
119353 sqlite3WalkSelect(&sWalker, pSelect);
@@ -118987,11 +119442,11 @@
119442 UNUSED_PARAMETER(NotUsed);
119443
119444 if( zDb && zInput ){
119445 int rc;
119446 Parse sParse;
119447 u64 flags = db->flags;
119448 if( bNoDQS ) db->flags &= ~(SQLITE_DqsDML|SQLITE_DqsDDL);
119449 rc = renameParseSql(&sParse, zDb, db, zInput, bTemp);
119450 db->flags |= (flags & (SQLITE_DqsDML|SQLITE_DqsDDL));
119451 if( rc==SQLITE_OK ){
119452 if( isLegacy==0 && sParse.pNewTable && IsView(sParse.pNewTable) ){
@@ -119710,11 +120165,11 @@
120165 }
120166
120167 p->db = db;
120168 p->nEst = sqlite3_value_int64(argv[2]);
120169 p->nRow = 0;
120170 p->nLimit = sqlite3_value_int(argv[3]);
120171 p->nCol = nCol;
120172 p->nKeyCol = nKeyCol;
120173 p->nSkipAhead = 0;
120174 p->current.anDLt = (tRowcnt*)&p[1];
120175
@@ -121519,10 +121974,17 @@
121974 */
121975 if( rc==SQLITE_OK ){
121976 sqlite3BtreeEnterAll(db);
121977 db->init.iDb = 0;
121978 db->mDbFlags &= ~(DBFLAG_SchemaKnownOk);
121979 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
121980 if( db->setlkFlags & SQLITE_SETLK_BLOCK_ON_CONNECT ){
121981 int val = 1;
121982 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pNew->pBt));
121983 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, &val);
121984 }
121985 #endif
121986 if( !REOPEN_AS_MEMDB(db) ){
121987 rc = sqlite3Init(db, &zErrDyn);
121988 }
121989 sqlite3BtreeLeaveAll(db);
121990 assert( zErrDyn==0 || rc!=SQLITE_OK );
@@ -123240,14 +123702,20 @@
123702 ** Convert an table column number into a index column number. That is,
123703 ** for the column iCol in the table (as defined by the CREATE TABLE statement)
123704 ** find the (first) offset of that column in index pIdx. Or return -1
123705 ** if column iCol is not used in index pIdx.
123706 */
123707 SQLITE_PRIVATE int sqlite3TableColumnToIndex(Index *pIdx, int iCol){
123708 int i;
123709 i16 iCol16;
123710 assert( iCol>=(-1) && iCol<=SQLITE_MAX_COLUMN );
123711 assert( pIdx->nColumn<=SQLITE_MAX_COLUMN );
123712 iCol16 = iCol;
123713 for(i=0; i<pIdx->nColumn; i++){
123714 if( iCol16==pIdx->aiColumn[i] ){
123715 return i;
123716 }
123717 }
123718 return -1;
123719 }
123720
123721 #ifndef SQLITE_OMIT_GENERATED_COLUMNS
@@ -124340,16 +124808,21 @@
124808
124809 /*
124810 ** Resize an Index object to hold N columns total. Return SQLITE_OK
124811 ** on success and SQLITE_NOMEM on an OOM error.
124812 */
124813 static int resizeIndexObject(Parse *pParse, Index *pIdx, int N){
124814 char *zExtra;
124815 u64 nByte;
124816 sqlite3 *db;
124817 if( pIdx->nColumn>=N ) return SQLITE_OK;
124818 db = pParse->db;
124819 assert( N>0 );
124820 assert( N <= SQLITE_MAX_COLUMN*2 /* tag-20250221-1 */ );
124821 testcase( N==2*pParse->db->aLimit[SQLITE_LIMIT_COLUMN] );
124822 assert( pIdx->isResized==0 );
124823 nByte = (sizeof(char*) + sizeof(LogEst) + sizeof(i16) + 1)*(u64)N;
124824 zExtra = sqlite3DbMallocZero(db, nByte);
124825 if( zExtra==0 ) return SQLITE_NOMEM_BKPT;
124826 memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
124827 pIdx->azColl = (const char**)zExtra;
124828 zExtra += sizeof(char*)*N;
@@ -124359,11 +124832,11 @@
124832 memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
124833 pIdx->aiColumn = (i16*)zExtra;
124834 zExtra += sizeof(i16)*N;
124835 memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
124836 pIdx->aSortOrder = (u8*)zExtra;
124837 pIdx->nColumn = (u16)N; /* See tag-20250221-1 above for proof of safety */
124838 pIdx->isResized = 1;
124839 return SQLITE_OK;
124840 }
124841
124842 /*
@@ -124613,11 +125086,11 @@
125086 if( n==0 ){
125087 /* This index is a superset of the primary key */
125088 pIdx->nColumn = pIdx->nKeyCol;
125089 continue;
125090 }
125091 if( resizeIndexObject(pParse, pIdx, pIdx->nKeyCol+n) ) return;
125092 for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
125093 if( !isDupColumn(pIdx, pIdx->nKeyCol, pPk, i) ){
125094 testcase( hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) );
125095 pIdx->aiColumn[j] = pPk->aiColumn[i];
125096 pIdx->azColl[j] = pPk->azColl[i];
@@ -124637,11 +125110,11 @@
125110 nExtra = 0;
125111 for(i=0; i<pTab->nCol; i++){
125112 if( !hasColumn(pPk->aiColumn, nPk, i)
125113 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0 ) nExtra++;
125114 }
125115 if( resizeIndexObject(pParse, pPk, nPk+nExtra) ) return;
125116 for(i=0, j=nPk; i<pTab->nCol; i++){
125117 if( !hasColumn(pPk->aiColumn, j, i)
125118 && (pTab->aCol[i].colFlags & COLFLAG_VIRTUAL)==0
125119 ){
125120 assert( j<pPk->nColumn );
@@ -126021,17 +126494,18 @@
126494 ** of 8-byte aligned space after the Index object and return a
126495 ** pointer to this extra space in *ppExtra.
126496 */
126497 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
126498 sqlite3 *db, /* Database connection */
126499 int nCol, /* Total number of columns in the index */
126500 int nExtra, /* Number of bytes of extra space to alloc */
126501 char **ppExtra /* Pointer to the "extra" space */
126502 ){
126503 Index *p; /* Allocated index object */
126504 i64 nByte; /* Bytes of space for Index object + arrays */
126505
126506 assert( nCol <= 2*db->aLimit[SQLITE_LIMIT_COLUMN] );
126507 nByte = ROUND8(sizeof(Index)) + /* Index structure */
126508 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
126509 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
126510 sizeof(i16)*nCol + /* Index.aiColumn */
126511 sizeof(u8)*nCol); /* Index.aSortOrder */
@@ -126040,12 +126514,13 @@
126514 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
126515 p->azColl = (const char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
126516 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
126517 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
126518 p->aSortOrder = (u8*)pExtra;
126519 assert( nCol>0 );
126520 p->nColumn = (u16)nCol;
126521 p->nKeyCol = (u16)(nCol - 1);
126522 *ppExtra = ((char*)p) + nByte;
126523 }
126524 return p;
126525 }
126526
@@ -130629,11 +131104,11 @@
131104
131105 /*
131106 ** Append to pStr text that is the SQL literal representation of the
131107 ** value contained in pValue.
131108 */
131109 SQLITE_PRIVATE void sqlite3QuoteValue(StrAccum *pStr, sqlite3_value *pValue, int bEscape){
131110 /* As currently implemented, the string must be initially empty.
131111 ** we might relax this requirement in the future, but that will
131112 ** require enhancements to the implementation. */
131113 assert( pStr!=0 && pStr->nChar==0 );
131114
@@ -130677,20 +131152,119 @@
131152 }
131153 break;
131154 }
131155 case SQLITE_TEXT: {
131156 const unsigned char *zArg = sqlite3_value_text(pValue);
131157 sqlite3_str_appendf(pStr, bEscape ? "%#Q" : "%Q", zArg);
131158 break;
131159 }
131160 default: {
131161 assert( sqlite3_value_type(pValue)==SQLITE_NULL );
131162 sqlite3_str_append(pStr, "NULL", 4);
131163 break;
131164 }
131165 }
131166 }
131167
131168 /*
131169 ** Return true if z[] begins with N hexadecimal digits, and write
131170 ** a decoding of those digits into *pVal. Or return false if any
131171 ** one of the first N characters in z[] is not a hexadecimal digit.
131172 */
131173 static int isNHex(const char *z, int N, u32 *pVal){
131174 int i;
131175 int v = 0;
131176 for(i=0; i<N; i++){
131177 if( !sqlite3Isxdigit(z[i]) ) return 0;
131178 v = (v<<4) + sqlite3HexToInt(z[i]);
131179 }
131180 *pVal = v;
131181 return 1;
131182 }
131183
131184 /*
131185 ** Implementation of the UNISTR() function.
131186 **
131187 ** This is intended to be a work-alike of the UNISTR() function in
131188 ** PostgreSQL. Quoting from the PG documentation (PostgreSQL 17 -
131189 ** scraped on 2025-02-22):
131190 **
131191 ** Evaluate escaped Unicode characters in the argument. Unicode
131192 ** characters can be specified as \XXXX (4 hexadecimal digits),
131193 ** \+XXXXXX (6 hexadecimal digits), \uXXXX (4 hexadecimal digits),
131194 ** or \UXXXXXXXX (8 hexadecimal digits). To specify a backslash,
131195 ** write two backslashes. All other characters are taken literally.
131196 */
131197 static void unistrFunc(
131198 sqlite3_context *context,
131199 int argc,
131200 sqlite3_value **argv
131201 ){
131202 char *zOut;
131203 const char *zIn;
131204 int nIn;
131205 int i, j, n;
131206 u32 v;
131207
131208 assert( argc==1 );
131209 UNUSED_PARAMETER( argc );
131210 zIn = (const char*)sqlite3_value_text(argv[0]);
131211 if( zIn==0 ) return;
131212 nIn = sqlite3_value_bytes(argv[0]);
131213 zOut = sqlite3_malloc64(nIn+1);
131214 if( zOut==0 ){
131215 sqlite3_result_error_nomem(context);
131216 return;
131217 }
131218 i = j = 0;
131219 while( i<nIn ){
131220 char *z = strchr(&zIn[i],'\\');
131221 if( z==0 ){
131222 n = nIn - i;
131223 memmove(&zOut[j], &zIn[i], n);
131224 j += n;
131225 break;
131226 }
131227 n = z - &zIn[i];
131228 if( n>0 ){
131229 memmove(&zOut[j], &zIn[i], n);
131230 j += n;
131231 i += n;
131232 }
131233 if( zIn[i+1]=='\\' ){
131234 i += 2;
131235 zOut[j++] = '\\';
131236 }else if( sqlite3Isxdigit(zIn[i+1]) ){
131237 if( !isNHex(&zIn[i+1], 4, &v) ) goto unistr_error;
131238 i += 5;
131239 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131240 }else if( zIn[i+1]=='+' ){
131241 if( !isNHex(&zIn[i+2], 6, &v) ) goto unistr_error;
131242 i += 8;
131243 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131244 }else if( zIn[i+1]=='u' ){
131245 if( !isNHex(&zIn[i+2], 4, &v) ) goto unistr_error;
131246 i += 6;
131247 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131248 }else if( zIn[i+1]=='U' ){
131249 if( !isNHex(&zIn[i+2], 8, &v) ) goto unistr_error;
131250 i += 10;
131251 j += sqlite3AppendOneUtf8Character(&zOut[j], v);
131252 }else{
131253 goto unistr_error;
131254 }
131255 }
131256 zOut[j] = 0;
131257 sqlite3_result_text64(context, zOut, j, sqlite3_free, SQLITE_UTF8);
131258 return;
131259
131260 unistr_error:
131261 sqlite3_free(zOut);
131262 sqlite3_result_error(context, "invalid Unicode escape", -1);
131263 return;
131264 }
131265
131266
131267 /*
131268 ** Implementation of the QUOTE() function.
131269 **
131270 ** The quote(X) function returns the text of an SQL literal which is the
@@ -130697,18 +131271,22 @@
131271 ** value of its argument suitable for inclusion into an SQL statement.
131272 ** Strings are surrounded by single-quotes with escapes on interior quotes
131273 ** as needed. BLOBs are encoded as hexadecimal literals. Strings with
131274 ** embedded NUL characters cannot be represented as string literals in SQL
131275 ** and hence the returned string literal is truncated prior to the first NUL.
131276 **
131277 ** If sqlite3_user_data() is non-zero, then the UNISTR_QUOTE() function is
131278 ** implemented instead. The difference is that UNISTR_QUOTE() uses the
131279 ** UNISTR() function to escape control characters.
131280 */
131281 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
131282 sqlite3_str str;
131283 sqlite3 *db = sqlite3_context_db_handle(context);
131284 assert( argc==1 );
131285 UNUSED_PARAMETER(argc);
131286 sqlite3StrAccumInit(&str, db, 0, 0, db->aLimit[SQLITE_LIMIT_LENGTH]);
131287 sqlite3QuoteValue(&str,argv[0],SQLITE_PTR_TO_INT(sqlite3_user_data(context)));
131288 sqlite3_result_text(context, sqlite3StrAccumFinish(&str), str.nChar,
131289 SQLITE_DYNAMIC);
131290 if( str.accError!=SQLITE_OK ){
131291 sqlite3_result_null(context);
131292 sqlite3_result_error_code(context, str.accError);
@@ -132275,11 +132853,13 @@
132853 VFUNCTION(randomblob, 1, 0, 0, randomBlob ),
132854 FUNCTION(nullif, 2, 0, 1, nullifFunc ),
132855 DFUNCTION(sqlite_version, 0, 0, 0, versionFunc ),
132856 DFUNCTION(sqlite_source_id, 0, 0, 0, sourceidFunc ),
132857 FUNCTION(sqlite_log, 2, 0, 0, errlogFunc ),
132858 FUNCTION(unistr, 1, 0, 0, unistrFunc ),
132859 FUNCTION(quote, 1, 0, 0, quoteFunc ),
132860 FUNCTION(unistr_quote, 1, 1, 0, quoteFunc ),
132861 VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
132862 VFUNCTION(changes, 0, 0, 0, changes ),
132863 VFUNCTION(total_changes, 0, 0, 0, total_changes ),
132864 FUNCTION(replace, 3, 0, 0, replaceFunc ),
132865 FUNCTION(zeroblob, 1, 0, 0, zeroblobFunc ),
@@ -134562,11 +135142,11 @@
135142 }else if( pLeft->pPrior ){
135143 /* In this case set the SF_MultiValue flag only if it was set on pLeft */
135144 f = (f & pLeft->selFlags);
135145 }
135146 pSelect = sqlite3SelectNew(pParse, pRow, 0, 0, 0, 0, 0, f, 0);
135147 pLeft->selFlags &= ~(u32)SF_MultiValue;
135148 if( pSelect ){
135149 pSelect->op = TK_ALL;
135150 pSelect->pPrior = pLeft;
135151 pLeft = pSelect;
135152 }
@@ -149534,11 +150114,11 @@
150114 p->pNext = 0;
150115 p->pWith = 0;
150116 #ifndef SQLITE_OMIT_WINDOWFUNC
150117 p->pWinDefn = 0;
150118 #endif
150119 p->selFlags &= ~(u32)SF_Compound;
150120 assert( (p->selFlags & SF_Converted)==0 );
150121 p->selFlags |= SF_Converted;
150122 assert( pNew->pPrior!=0 );
150123 pNew->pPrior->pNext = pNew;
150124 pNew->pLimit = 0;
@@ -151140,11 +151720,11 @@
151720 Expr *pTerm;
151721 pPrior = pSub->pPrior;
151722 pSub->pPrior = 0;
151723 pSub->pNext = 0;
151724 pSub->selFlags |= SF_Aggregate;
151725 pSub->selFlags &= ~(u32)SF_Compound;
151726 pSub->nSelectRow = 0;
151727 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric, pSub->pEList);
151728 pTerm = pPrior ? sqlite3ExprDup(db, pCount, 0) : pCount;
151729 pSub->pEList = sqlite3ExprListAppend(pParse, 0, pTerm);
151730 pTerm = sqlite3PExpr(pParse, TK_SELECT, 0, 0);
@@ -151155,11 +151735,11 @@
151735 pExpr = sqlite3PExpr(pParse, TK_PLUS, pTerm, pExpr);
151736 }
151737 pSub = pPrior;
151738 }
151739 p->pEList->a[0].pExpr = pExpr;
151740 p->selFlags &= ~(u32)SF_Aggregate;
151741
151742 #if TREETRACE_ENABLED
151743 if( sqlite3TreeTrace & 0x200 ){
151744 TREETRACE(0x200,pParse,p,("After count-of-view optimization:\n"));
151745 sqlite3TreeViewSelect(0, p, 0);
@@ -151362,11 +151942,11 @@
151942 sqlite3ParserAddCleanup(pParse, sqlite3ExprListDeleteGeneric,
151943 p->pOrderBy);
151944 testcase( pParse->earlyCleanup );
151945 p->pOrderBy = 0;
151946 }
151947 p->selFlags &= ~(u32)SF_Distinct;
151948 p->selFlags |= SF_NoopOrderBy;
151949 }
151950 sqlite3SelectPrep(pParse, p, 0);
151951 if( pParse->nErr ){
151952 goto select_end;
@@ -151401,11 +151981,11 @@
151981
151982 /* Clear the SF_UFSrcCheck flag. The check has already been performed,
151983 ** and leaving this flag set can cause errors if a compound sub-query
151984 ** in p->pSrc is flattened into this query and this function called
151985 ** again as part of compound SELECT processing. */
151986 p->selFlags &= ~(u32)SF_UFSrcCheck;
151987 }
151988
151989 if( pDest->eDest==SRT_Output ){
151990 sqlite3GenerateColumnNames(pParse, p);
151991 }
@@ -151890,11 +152470,11 @@
152470 && OptimizationEnabled(db, SQLITE_GroupByOrder)
152471 #ifndef SQLITE_OMIT_WINDOWFUNC
152472 && p->pWin==0
152473 #endif
152474 ){
152475 p->selFlags &= ~(u32)SF_Distinct;
152476 pGroupBy = p->pGroupBy = sqlite3ExprListDup(db, pEList, 0);
152477 if( pGroupBy ){
152478 for(i=0; i<pGroupBy->nExpr; i++){
152479 pGroupBy->a[i].u.x.iOrderByCol = i+1;
152480 }
@@ -164580,10 +165160,12 @@
165160 if( pSrc->colUsed & MASKBIT(BMS-1) ){
165161 nKeyCol += pTable->nCol - BMS + 1;
165162 }
165163
165164 /* Construct the Index object to describe this index */
165165 assert( nKeyCol <= pTable->nCol + MAX(0, pTable->nCol - BMS + 1) );
165166 /* ^-- This guarantees that the number of index columns will fit in the u16 */
165167 pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+HasRowid(pTable),
165168 0, &zNotUsed);
165169 if( pIdx==0 ) goto end_auto_index_create;
165170 pLoop->u.btree.pIndex = pIdx;
165171 pIdx->zName = "auto-index";
@@ -172141,11 +172723,11 @@
172723
172724 p->pSrc = 0;
172725 p->pWhere = 0;
172726 p->pGroupBy = 0;
172727 p->pHaving = 0;
172728 p->selFlags &= ~(u32)SF_Aggregate;
172729 p->selFlags |= SF_WinRewrite;
172730
172731 /* Create the ORDER BY clause for the sub-select. This is the concatenation
172732 ** of the window PARTITION and ORDER BY clauses. Then, if this makes it
172733 ** redundant, remove the ORDER BY from the parent SELECT. */
@@ -178280,12 +178862,12 @@
178862 pRhs = sqlite3SelectNew(pParse,0,pFrom,0,0,0,0,0,0);
178863 }
178864 if( pRhs ){
178865 pRhs->op = (u8)yymsp[-1].minor.yy502;
178866 pRhs->pPrior = pLhs;
178867 if( ALWAYS(pLhs) ) pLhs->selFlags &= ~(u32)SF_MultiValue;
178868 pRhs->selFlags &= ~(u32)SF_MultiValue;
178869 if( yymsp[-1].minor.yy502!=TK_ALL ) pParse->hasCompound = 1;
178870 }else{
178871 sqlite3SelectDelete(pParse->db, pLhs);
178872 }
178873 yymsp[-2].minor.yy637 = pRhs;
@@ -183396,10 +183978,13 @@
183978 sqlite3_mutex_enter(db->mutex);
183979 db->busyHandler.xBusyHandler = xBusy;
183980 db->busyHandler.pBusyArg = pArg;
183981 db->busyHandler.nBusy = 0;
183982 db->busyTimeout = 0;
183983 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
183984 db->setlkTimeout = 0;
183985 #endif
183986 sqlite3_mutex_leave(db->mutex);
183987 return SQLITE_OK;
183988 }
183989
183990 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
@@ -183445,15 +184030,46 @@
184030 #endif
184031 if( ms>0 ){
184032 sqlite3_busy_handler(db, (int(*)(void*,int))sqliteDefaultBusyCallback,
184033 (void*)db);
184034 db->busyTimeout = ms;
184035 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184036 db->setlkTimeout = ms;
184037 #endif
184038 }else{
184039 sqlite3_busy_handler(db, 0, 0);
184040 }
184041 return SQLITE_OK;
184042 }
184043
184044 /*
184045 ** Set the setlk timeout value.
184046 */
184047 SQLITE_API int sqlite3_setlk_timeout(sqlite3 *db, int ms, int flags){
184048 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184049 int iDb;
184050 int bBOC = ((flags & SQLITE_SETLK_BLOCK_ON_CONNECT) ? 1 : 0);
184051 #endif
184052 #ifdef SQLITE_ENABLE_API_ARMOR
184053 if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
184054 #endif
184055 if( ms<-1 ) return SQLITE_RANGE;
184056 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT
184057 db->setlkTimeout = ms;
184058 db->setlkFlags = flags;
184059 sqlite3BtreeEnterAll(db);
184060 for(iDb=0; iDb<db->nDb; iDb++){
184061 Btree *pBt = db->aDb[iDb].pBt;
184062 if( pBt ){
184063 sqlite3_file *fd = sqlite3PagerFile(sqlite3BtreePager(pBt));
184064 sqlite3OsFileControlHint(fd, SQLITE_FCNTL_BLOCK_ON_CONNECT, (void*)&bBOC);
184065 }
184066 }
184067 sqlite3BtreeLeaveAll(db);
184068 #endif
184069 return SQLITE_OK;
184070 }
184071
184072 /*
184073 ** Cause any pending operation to stop at its earliest opportunity.
184074 */
184075 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
@@ -255960,11 +256576,11 @@
256576 int nArg, /* Number of args */
256577 sqlite3_value **apUnused /* Function arguments */
256578 ){
256579 assert( nArg==0 );
256580 UNUSED_PARAM2(nArg, apUnused);
256581 sqlite3_result_text(pCtx, "fts5: 2025-02-25 16:39:51 6f0b6d95db17e69ac7e46a39f52770291ac4cfe43eea09add224946a6e11f04e", -1, SQLITE_TRANSIENT);
256582 }
256583
256584 /*
256585 ** Implementation of fts5_locale(LOCALE, TEXT) function.
256586 **
@@ -256185,12 +256801,12 @@
256801 /* If SQLITE_FTS5_ENABLE_TEST_MI is defined, assume that the file
256802 ** fts5_test_mi.c is compiled and linked into the executable. And call
256803 ** its entry point to enable the matchinfo() demo. */
256804 #ifdef SQLITE_FTS5_ENABLE_TEST_MI
256805 if( rc==SQLITE_OK ){
256806 extern int sqlite3Fts5TestRegisterMatchinfoAPI(fts5_api*);
256807 rc = sqlite3Fts5TestRegisterMatchinfoAPI(&pGlobal->api);
256808 }
256809 #endif
256810
256811 return rc;
256812 }
256813
+46 -1
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147147
** [sqlite_version()] and [sqlite_source_id()].
148148
*/
149149
#define SQLITE_VERSION "3.50.0"
150150
#define SQLITE_VERSION_NUMBER 3050000
151
-#define SQLITE_SOURCE_ID "2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc"
151
+#define SQLITE_SOURCE_ID "2025-02-25 18:10:47 e6784af6d50f715338ae3218fc8ba1b894883c27d797f0b7fd2625cac17d9cd7"
152152
153153
/*
154154
** CAPI3REF: Run-Time Library Version Numbers
155155
** KEYWORDS: sqlite3_version sqlite3_sourceid
156156
**
@@ -1160,10 +1160,16 @@
11601160
** to block for up to M milliseconds before failing when attempting to
11611161
** obtain a file lock using the xLock or xShmLock methods of the VFS.
11621162
** The parameter is a pointer to a 32-bit signed integer that contains
11631163
** the value that M is to be set to. Before returning, the 32-bit signed
11641164
** integer is overwritten with the previous value of M.
1165
+**
1166
+** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1167
+** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1168
+** VFS to block when taking a SHARED lock to connect to a wal mode database.
1169
+** This is used to implement the functionality associated with
1170
+** SQLITE_SETLK_BLOCK_ON_CONNECT.
11651171
**
11661172
** <li>[[SQLITE_FCNTL_DATA_VERSION]]
11671173
** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
11681174
** a database file. The argument is a pointer to a 32-bit unsigned integer.
11691175
** The "data version" for the pager is written into the pointer. The
@@ -1257,10 +1263,11 @@
12571263
#define SQLITE_FCNTL_CKPT_START 39
12581264
#define SQLITE_FCNTL_EXTERNAL_READER 40
12591265
#define SQLITE_FCNTL_CKSM_FILE 41
12601266
#define SQLITE_FCNTL_RESET_CACHE 42
12611267
#define SQLITE_FCNTL_NULL_IO 43
1268
+#define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
12621269
12631270
/* deprecated names */
12641271
#define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
12651272
#define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
12661273
#define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3013,10 +3020,48 @@
30133020
**
30143021
** See also: [PRAGMA busy_timeout]
30153022
*/
30163023
SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
30173024
3025
+/*
3026
+** CAPI3REF: Set the Setlk Timeout
3027
+** METHOD: sqlite3
3028
+**
3029
+** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3030
+** the VFS supports blocking locks, it sets the timeout in ms used by
3031
+** eligible locks taken on wal mode databases by the specified database
3032
+** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3033
+** not support blocking locks, this function is a no-op.
3034
+**
3035
+** Passing 0 to this function disables blocking locks altogether. Passing
3036
+** -1 to this function requests that the VFS blocks for a long time -
3037
+** indefinitely if possible. The results of passing any other negative value
3038
+** are undefined.
3039
+**
3040
+** Internally, each SQLite database handle store two timeout values - the
3041
+** busy-timeout (used for rollback mode databases, or if the VFS does not
3042
+** support blocking locks) and the setlk-timeout (used for blocking locks
3043
+** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3044
+** values, this function sets only the setlk-timeout value. Therefore,
3045
+** to configure separate busy-timeout and setlk-timeout values for a single
3046
+** database handle, call sqlite3_busy_timeout() followed by this function.
3047
+**
3048
+** Whenever the number of connections to a wal mode database falls from
3049
+** 1 to 0, the last connection takes an exclusive lock on the database,
3050
+** then checkpoints and deletes the wal file. While it is doing this, any
3051
+** new connection that tries to read from the database fails with an
3052
+** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3053
+** passed to this API, the new connection blocks until the exclusive lock
3054
+** has been released.
3055
+*/
3056
+SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3057
+
3058
+/*
3059
+** CAPI3REF: Flags for sqlite3_setlk_timeout()
3060
+*/
3061
+#define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3062
+
30183063
/*
30193064
** CAPI3REF: Convenience Routines For Running Queries
30203065
** METHOD: sqlite3
30213066
**
30223067
** This is a legacy interface that is preserved for backwards compatibility.
30233068
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.50.0"
150 #define SQLITE_VERSION_NUMBER 3050000
151 #define SQLITE_SOURCE_ID "2025-02-18 01:16:26 57caa3136d1bfca06e4f2285734a4977b8d3fa1f75bf87453b975867e9de38fc"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1160,10 +1160,16 @@
1160 ** to block for up to M milliseconds before failing when attempting to
1161 ** obtain a file lock using the xLock or xShmLock methods of the VFS.
1162 ** The parameter is a pointer to a 32-bit signed integer that contains
1163 ** the value that M is to be set to. Before returning, the 32-bit signed
1164 ** integer is overwritten with the previous value of M.
 
 
 
 
 
 
1165 **
1166 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1167 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1168 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1169 ** The "data version" for the pager is written into the pointer. The
@@ -1257,10 +1263,11 @@
1257 #define SQLITE_FCNTL_CKPT_START 39
1258 #define SQLITE_FCNTL_EXTERNAL_READER 40
1259 #define SQLITE_FCNTL_CKSM_FILE 41
1260 #define SQLITE_FCNTL_RESET_CACHE 42
1261 #define SQLITE_FCNTL_NULL_IO 43
 
1262
1263 /* deprecated names */
1264 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1265 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1266 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3013,10 +3020,48 @@
3013 **
3014 ** See also: [PRAGMA busy_timeout]
3015 */
3016 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3017
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3018 /*
3019 ** CAPI3REF: Convenience Routines For Running Queries
3020 ** METHOD: sqlite3
3021 **
3022 ** This is a legacy interface that is preserved for backwards compatibility.
3023
--- extsrc/sqlite3.h
+++ extsrc/sqlite3.h
@@ -146,11 +146,11 @@
146 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147 ** [sqlite_version()] and [sqlite_source_id()].
148 */
149 #define SQLITE_VERSION "3.50.0"
150 #define SQLITE_VERSION_NUMBER 3050000
151 #define SQLITE_SOURCE_ID "2025-02-25 18:10:47 e6784af6d50f715338ae3218fc8ba1b894883c27d797f0b7fd2625cac17d9cd7"
152
153 /*
154 ** CAPI3REF: Run-Time Library Version Numbers
155 ** KEYWORDS: sqlite3_version sqlite3_sourceid
156 **
@@ -1160,10 +1160,16 @@
1160 ** to block for up to M milliseconds before failing when attempting to
1161 ** obtain a file lock using the xLock or xShmLock methods of the VFS.
1162 ** The parameter is a pointer to a 32-bit signed integer that contains
1163 ** the value that M is to be set to. Before returning, the 32-bit signed
1164 ** integer is overwritten with the previous value of M.
1165 **
1166 ** <li>[[SQLITE_FCNTL_BLOCK_ON_CONNECT]]
1167 ** The [SQLITE_FCNTL_BLOCK_ON_CONNECT] opcode is used to configure the
1168 ** VFS to block when taking a SHARED lock to connect to a wal mode database.
1169 ** This is used to implement the functionality associated with
1170 ** SQLITE_SETLK_BLOCK_ON_CONNECT.
1171 **
1172 ** <li>[[SQLITE_FCNTL_DATA_VERSION]]
1173 ** The [SQLITE_FCNTL_DATA_VERSION] opcode is used to detect changes to
1174 ** a database file. The argument is a pointer to a 32-bit unsigned integer.
1175 ** The "data version" for the pager is written into the pointer. The
@@ -1257,10 +1263,11 @@
1263 #define SQLITE_FCNTL_CKPT_START 39
1264 #define SQLITE_FCNTL_EXTERNAL_READER 40
1265 #define SQLITE_FCNTL_CKSM_FILE 41
1266 #define SQLITE_FCNTL_RESET_CACHE 42
1267 #define SQLITE_FCNTL_NULL_IO 43
1268 #define SQLITE_FCNTL_BLOCK_ON_CONNECT 44
1269
1270 /* deprecated names */
1271 #define SQLITE_GET_LOCKPROXYFILE SQLITE_FCNTL_GET_LOCKPROXYFILE
1272 #define SQLITE_SET_LOCKPROXYFILE SQLITE_FCNTL_SET_LOCKPROXYFILE
1273 #define SQLITE_LAST_ERRNO SQLITE_FCNTL_LAST_ERRNO
@@ -3013,10 +3020,48 @@
3020 **
3021 ** See also: [PRAGMA busy_timeout]
3022 */
3023 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
3024
3025 /*
3026 ** CAPI3REF: Set the Setlk Timeout
3027 ** METHOD: sqlite3
3028 **
3029 ** This routine is only useful in SQLITE_ENABLE_SETLK_TIMEOUT builds. If
3030 ** the VFS supports blocking locks, it sets the timeout in ms used by
3031 ** eligible locks taken on wal mode databases by the specified database
3032 ** handle. In non-SQLITE_ENABLE_SETLK_TIMEOUT builds, or if the VFS does
3033 ** not support blocking locks, this function is a no-op.
3034 **
3035 ** Passing 0 to this function disables blocking locks altogether. Passing
3036 ** -1 to this function requests that the VFS blocks for a long time -
3037 ** indefinitely if possible. The results of passing any other negative value
3038 ** are undefined.
3039 **
3040 ** Internally, each SQLite database handle store two timeout values - the
3041 ** busy-timeout (used for rollback mode databases, or if the VFS does not
3042 ** support blocking locks) and the setlk-timeout (used for blocking locks
3043 ** on wal-mode databases). The sqlite3_busy_timeout() method sets both
3044 ** values, this function sets only the setlk-timeout value. Therefore,
3045 ** to configure separate busy-timeout and setlk-timeout values for a single
3046 ** database handle, call sqlite3_busy_timeout() followed by this function.
3047 **
3048 ** Whenever the number of connections to a wal mode database falls from
3049 ** 1 to 0, the last connection takes an exclusive lock on the database,
3050 ** then checkpoints and deletes the wal file. While it is doing this, any
3051 ** new connection that tries to read from the database fails with an
3052 ** SQLITE_BUSY error. Or, if the SQLITE_SETLK_BLOCK_ON_CONNECT flag is
3053 ** passed to this API, the new connection blocks until the exclusive lock
3054 ** has been released.
3055 */
3056 SQLITE_API int sqlite3_setlk_timeout(sqlite3*, int ms, int flags);
3057
3058 /*
3059 ** CAPI3REF: Flags for sqlite3_setlk_timeout()
3060 */
3061 #define SQLITE_SETLK_BLOCK_ON_CONNECT 0x01
3062
3063 /*
3064 ** CAPI3REF: Convenience Routines For Running Queries
3065 ** METHOD: sqlite3
3066 **
3067 ** This is a legacy interface that is preserved for backwards compatibility.
3068

Keyboard Shortcuts

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