Upgrade sqlite version to 3.45.3 (#17769)
sqlite upgrade (3.42.0 to 3.45.3)
Stelios Fragkakis committed
Jun 3, 2024 at 13:59 UTC
065356bedc4cd99cc673b49675115cbc8f7d9184
4 files changed
+14515
-5836
src/database/sqlite/dbdata.c
+145
-81
@@ -91,6 +91,15 @@ typedef unsigned int u32;
91
92
typedef struct DbdataTable DbdataTable;
93
typedef struct DbdataCursor DbdataCursor;
94
+typedef struct DbdataBuffer DbdataBuffer;
95
+
96
+/*
97
+** Buffer type.
98
+*/
99
+struct DbdataBuffer {
100
+ u8 *aBuf;
101
+ sqlite3_int64 nBuf;
102
+};
103
104
/* Cursor object */
105
struct DbdataCursor {
@@ -107,7 +116,7 @@ struct DbdataCursor {
116
sqlite3_int64 iRowid;
117
118
/* Only for the sqlite_dbdata table */
110
- u8 *pRec; /* Buffer containing current record */
119
+ DbdataBuffer rec;
120
sqlite3_int64 nRec; /* Size of pRec[] in bytes */
121
sqlite3_int64 nHdr; /* Size of header in bytes */
122
int iField; /* Current field number */
@@ -152,6 +161,31 @@ struct DbdataTable {
161
" schema TEXT HIDDEN" \
162
")"
163
164
+/*
165
+** Ensure the buffer passed as the first argument is at least nMin bytes
166
+** in size. If an error occurs while attempting to resize the buffer,
167
+** SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
168
+*/
169
+static int dbdataBufferSize(DbdataBuffer *pBuf, sqlite3_int64 nMin){
170
+ if( nMin>pBuf->nBuf ){
171
+ sqlite3_int64 nNew = nMin+16384;
172
+ u8 *aNew = (u8*)sqlite3_realloc64(pBuf->aBuf, nNew);
173
+
174
+ if( aNew==0 ) return SQLITE_NOMEM;
175
+ pBuf->aBuf = aNew;
176
+ pBuf->nBuf = nNew;
177
+ }
178
+ return SQLITE_OK;
179
+}
180
+
181
+/*
182
+** Release the allocation managed by buffer pBuf.
183
+*/
184
+static void dbdataBufferFree(DbdataBuffer *pBuf){
185
+ sqlite3_free(pBuf->aBuf);
186
+ memset(pBuf, 0, sizeof(*pBuf));
187
+}
188
+
189
/*
190
** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
191
** table.
@@ -292,9 +326,9 @@ static void dbdataResetCursor(DbdataCursor *pCsr){
326
pCsr->iField = 0;
327
pCsr->bOnePage = 0;
328
sqlite3_free(pCsr->aPage);
295
- sqlite3_free(pCsr->pRec);
296
- pCsr->pRec = 0;
329
+ dbdataBufferFree(&pCsr->rec);
330
pCsr->aPage = 0;
331
+ pCsr->nRec = 0;
332
}
333
334
/*
@@ -436,67 +470,88 @@ static void dbdataValue(
470
u8 *pData,
471
sqlite3_int64 nData
472
){
439
- if( eType>=0 && dbdataValueBytes(eType)<=nData ){
440
- switch( eType ){
441
- case 0:
442
- case 10:
443
- case 11:
444
- sqlite3_result_null(pCtx);
445
- break;
446
-
447
- case 8:
448
- sqlite3_result_int(pCtx, 0);
449
- break;
450
- case 9:
451
- sqlite3_result_int(pCtx, 1);
452
- break;
453
-
454
- case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
455
- sqlite3_uint64 v = (signed char)pData[0];
456
- pData++;
457
- switch( eType ){
458
- case 7:
459
- case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
460
- case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
461
- case 4: v = (v<<8) + pData[0]; pData++;
462
- case 3: v = (v<<8) + pData[0]; pData++;
463
- case 2: v = (v<<8) + pData[0]; pData++;
464
- }
465
-
466
- if( eType==7 ){
467
- double r;
468
- memcpy(&r, &v, sizeof(r));
469
- sqlite3_result_double(pCtx, r);
470
- }else{
471
- sqlite3_result_int64(pCtx, (sqlite3_int64)v);
473
+ if( eType>=0 ){
474
+ if( dbdataValueBytes(eType)<=nData ){
475
+ switch( eType ){
476
+ case 0:
477
+ case 10:
478
+ case 11:
479
+ sqlite3_result_null(pCtx);
480
+ break;
481
+
482
+ case 8:
483
+ sqlite3_result_int(pCtx, 0);
484
+ break;
485
+ case 9:
486
+ sqlite3_result_int(pCtx, 1);
487
+ break;
488
+
489
+ case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
490
+ sqlite3_uint64 v = (signed char)pData[0];
491
+ pData++;
492
+ switch( eType ){
493
+ case 7:
494
+ case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
495
+ case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
496
+ case 4: v = (v<<8) + pData[0]; pData++;
497
+ case 3: v = (v<<8) + pData[0]; pData++;
498
+ case 2: v = (v<<8) + pData[0]; pData++;
499
+ }
500
+
501
+ if( eType==7 ){
502
+ double r;
503
+ memcpy(&r, &v, sizeof(r));
504
+ sqlite3_result_double(pCtx, r);
505
+ }else{
506
+ sqlite3_result_int64(pCtx, (sqlite3_int64)v);
507
+ }
508
+ break;
509
}
473
- break;
474
- }
475
-
476
- default: {
477
- int n = ((eType-12) / 2);
478
- if( eType % 2 ){
479
- switch( enc ){
480
-#ifndef SQLITE_OMIT_UTF16
481
- case SQLITE_UTF16BE:
482
- sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
483
- break;
484
- case SQLITE_UTF16LE:
485
- sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
486
- break;
487
-#endif
488
- default:
489
- sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
490
- break;
510
+
511
+ default: {
512
+ int n = ((eType-12) / 2);
513
+ if( eType % 2 ){
514
+ switch( enc ){
515
+ #ifndef SQLITE_OMIT_UTF16
516
+ case SQLITE_UTF16BE:
517
+ sqlite3_result_text16be(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
518
+ break;
519
+ case SQLITE_UTF16LE:
520
+ sqlite3_result_text16le(pCtx, (void*)pData, n, SQLITE_TRANSIENT);
521
+ break;
522
+ #endif
523
+ default:
524
+ sqlite3_result_text(pCtx, (char*)pData, n, SQLITE_TRANSIENT);
525
+ break;
526
+ }
527
+ }else{
528
+ sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
529
}
492
- }else{
493
- sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
530
}
531
}
532
+ }else{
533
+ if( eType==7 ){
534
+ sqlite3_result_double(pCtx, 0.0);
535
+ }else if( eType<7 ){
536
+ sqlite3_result_int(pCtx, 0);
537
+ }else if( eType%2 ){
538
+ sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
539
+ }else{
540
+ sqlite3_result_blob(pCtx, "", 0, SQLITE_STATIC);
541
+ }
542
}
543
}
544
}
545
546
+/* This macro is a copy of the MX_CELL() macro in the SQLite core. Given
547
+** a page-size, it returns the maximum number of cells that may be present
548
+** on the page. */
549
+#define DBDATA_MX_CELL(pgsz) ((pgsz-8)/6)
550
+
551
+/* Maximum number of fields that may appear in a single record. This is
552
+** the "hard-limit", according to comments in sqliteLimit.h. */
553
+#define DBDATA_MX_FIELD 32676
554
+
555
/*
556
** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
557
*/
@@ -525,6 +580,9 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
580
assert( iOff+3+2<=pCsr->nPage );
581
pCsr->iCell = pTab->bPtr ? -2 : 0;
582
pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
583
+ if( pCsr->nCell>DBDATA_MX_CELL(pCsr->nPage) ){
584
+ pCsr->nCell = DBDATA_MX_CELL(pCsr->nPage);
585
+ }
586
}
587
588
if( pTab->bPtr ){
@@ -542,7 +600,8 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
600
}
601
}else{
602
/* If there is no record loaded, load it now. */
545
- if( pCsr->pRec==0 ){
603
+ assert( pCsr->rec.aBuf!=0 || pCsr->nRec==0 );
604
+ if( pCsr->nRec==0 ){
605
int bHasRowid = 0;
606
int nPointer = 0;
607
sqlite3_int64 nPayload = 0;
@@ -569,22 +628,24 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
628
if( pCsr->iCell>=pCsr->nCell ){
629
bNextPage = 1;
630
}else{
631
+ int iCellPtr = iOff + 8 + nPointer + pCsr->iCell*2;
632
573
- iOff += 8 + nPointer + pCsr->iCell*2;
574
- if( iOff>pCsr->nPage ){
633
+ if( iCellPtr>pCsr->nPage ){
634
bNextPage = 1;
635
}else{
577
- iOff = get_uint16(&pCsr->aPage[iOff]);
636
+ iOff = get_uint16(&pCsr->aPage[iCellPtr]);
637
}
638
639
/* For an interior node cell, skip past the child-page number */
640
iOff += nPointer;
641
642
/* Load the "byte of payload including overflow" field */
584
- if( bNextPage || iOff>pCsr->nPage ){
643
+ if( bNextPage || iOff>pCsr->nPage || iOff<=iCellPtr ){
644
bNextPage = 1;
645
}else{
646
iOff += dbdataGetVarintU32(&pCsr->aPage[iOff], &nPayload);
647
+ if( nPayload>0x7fffff00 ) nPayload &= 0x3fff;
648
+ if( nPayload==0 ) nPayload = 1;
649
}
650
651
/* If this is a leaf intkey cell, load the rowid */
@@ -619,13 +680,12 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
680
/* Allocate space for payload. And a bit more to catch small buffer
681
** overruns caused by attempting to read a varint or similar from
682
** near the end of a corrupt record. */
622
- pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
623
- if( pCsr->pRec==0 ) return SQLITE_NOMEM;
624
- memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
625
- pCsr->nRec = nPayload;
683
+ rc = dbdataBufferSize(&pCsr->rec, nPayload+DBDATA_PADDING_BYTES);
684
+ if( rc!=SQLITE_OK ) return rc;
685
+ assert( nPayload!=0 );
686
687
/* Load the nLocal bytes of payload */
628
- memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
688
+ memcpy(pCsr->rec.aBuf, &pCsr->aPage[iOff], nLocal);
689
iOff += nLocal;
690
691
/* Load content from overflow pages */
@@ -643,19 +703,22 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
703
704
nCopy = U-4;
705
if( nCopy>nRem ) nCopy = nRem;
646
- memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
706
+ memcpy(&pCsr->rec.aBuf[nPayload-nRem], &aOvfl[4], nCopy);
707
nRem -= nCopy;
708
709
pgnoOvfl = get_uint32(aOvfl);
710
sqlite3_free(aOvfl);
711
}
712
+ nPayload -= nRem;
713
}
714
+ memset(&pCsr->rec.aBuf[nPayload], 0, DBDATA_PADDING_BYTES);
715
+ pCsr->nRec = nPayload;
716
654
- iHdr = dbdataGetVarintU32(pCsr->pRec, &nHdr);
717
+ iHdr = dbdataGetVarintU32(pCsr->rec.aBuf, &nHdr);
718
if( nHdr>nPayload ) nHdr = 0;
719
pCsr->nHdr = nHdr;
657
- pCsr->pHdrPtr = &pCsr->pRec[iHdr];
658
- pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
720
+ pCsr->pHdrPtr = &pCsr->rec.aBuf[iHdr];
721
+ pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nHdr];
722
pCsr->iField = (bHasRowid ? -1 : 0);
723
}
724
}
@@ -663,14 +726,16 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
726
pCsr->iField++;
727
if( pCsr->iField>0 ){
728
sqlite3_int64 iType;
666
- if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
729
+ if( pCsr->pHdrPtr>=&pCsr->rec.aBuf[pCsr->nRec]
730
+ || pCsr->iField>=DBDATA_MX_FIELD
731
+ ){
732
bNextPage = 1;
733
}else{
734
int szField = 0;
735
pCsr->pHdrPtr += dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
736
szField = dbdataValueBytes(iType);
672
- if( (pCsr->nRec - (pCsr->pPtr - pCsr->pRec))<szField ){
673
- pCsr->pPtr = &pCsr->pRec[pCsr->nRec];
737
+ if( (pCsr->nRec - (pCsr->pPtr - pCsr->rec.aBuf))<szField ){
738
+ pCsr->pPtr = &pCsr->rec.aBuf[pCsr->nRec];
739
}else{
740
pCsr->pPtr += szField;
741
}
@@ -680,20 +745,18 @@ static int dbdataNext(sqlite3_vtab_cursor *pCursor){
745
746
if( bNextPage ){
747
sqlite3_free(pCsr->aPage);
683
- sqlite3_free(pCsr->pRec);
748
pCsr->aPage = 0;
685
- pCsr->pRec = 0;
749
+ pCsr->nRec = 0;
750
if( pCsr->bOnePage ) return SQLITE_OK;
751
pCsr->iPgno++;
752
}else{
689
- if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
753
+ if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->rec.aBuf[pCsr->nHdr] ){
754
return SQLITE_OK;
755
}
756
757
/* Advance to the next cell. The next iteration of the loop will load
758
** the record and so on. */
695
- sqlite3_free(pCsr->pRec);
696
- pCsr->pRec = 0;
759
+ pCsr->nRec = 0;
760
pCsr->iCell++;
761
}
762
}
@@ -883,12 +946,12 @@ static int dbdataColumn(
946
case DBDATA_COLUMN_VALUE: {
947
if( pCsr->iField<0 ){
948
sqlite3_result_int64(ctx, pCsr->iIntkey);
886
- }else if( &pCsr->pRec[pCsr->nRec] >= pCsr->pPtr ){
949
+ }else if( &pCsr->rec.aBuf[pCsr->nRec] >= pCsr->pPtr ){
950
sqlite3_int64 iType;
951
dbdataGetVarintU32(pCsr->pHdrPtr, &iType);
952
dbdataValue(
953
ctx, pCsr->enc, iType, pCsr->pPtr,
891
- &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
954
+ &pCsr->rec.aBuf[pCsr->nRec] - pCsr->pPtr
955
);
956
}
957
break;
@@ -936,7 +999,8 @@ static int sqlite3DbdataRegister(sqlite3 *db){
999
0, /* xSavepoint */
1000
0, /* xRelease */
1001
0, /* xRollbackTo */
939
- 0 /* xShadowName */
1002
+ 0, /* xShadowName */
1003
+ 0 /* xIntegrity */
1004
};
1005
1006
int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
src/database/sqlite/sqlite3.c
+13989
-5680
@@ -1,6 +1,6 @@
1
/******************************************************************************
2
** This file is an amalgamation of many separate C source files from SQLite
3
-** version 3.42.0. By combining all the individual C code files into this
3
+** version 3.45.3. By combining all the individual C code files into this
4
** single large file, the entire code can be compiled as a single translation
5
** unit. This allows many compilers to do optimizations that would not be
6
** possible if the files were compiled separately. Performance improvements
@@ -16,6 +16,9 @@
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
+** 8653b758870e6ef0c98d46b3ace27849054a.
22
*/
23
#pragma GCC diagnostic push
24
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
@@ -58,11 +61,11 @@
61
** used on lines of code that actually
62
** implement parts of coverage testing.
63
**
61
-** OPTIMIZATION-IF-TRUE - This branch is allowed to alway be false
64
+** OPTIMIZATION-IF-TRUE - This branch is allowed to always be false
65
** and the correct answer is still obtained,
66
** though perhaps more slowly.
67
**
65
-** OPTIMIZATION-IF-FALSE - This branch is allowed to alway be true
68
+** OPTIMIZATION-IF-FALSE - This branch is allowed to always be true
69
** and the correct answer is still obtained,
70
** though perhaps more slowly.
71
**
@@ -464,9 +467,9 @@ extern "C" {
467
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
468
** [sqlite_version()] and [sqlite_source_id()].
469
*/
467
-#define SQLITE_VERSION "3.42.0"
468
-#define SQLITE_VERSION_NUMBER 3042000
469
-#define SQLITE_SOURCE_ID "2023-05-16 12:36:15 831d0fb2836b71c9bc51067c49fee4b8f18047814f2ff22d817d25195cf350b0"
470
+#define SQLITE_VERSION "3.45.3"
471
+#define SQLITE_VERSION_NUMBER 3045003
472
+#define SQLITE_SOURCE_ID "2024-04-15 13:34:05 8653b758870e6ef0c98d46b3ace27849054af85da891eb121e9aaa537f1e8355"
473
474
/*
475
** CAPI3REF: Run-Time Library Version Numbers
@@ -738,6 +741,8 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
741
** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
742
** <li> The application must not modify the SQL statement text passed into
743
** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
744
+** <li> The application must not dereference the arrays or string pointers
745
+** passed as the 3rd and 4th callback parameters after it returns.
746
** </ul>
747
*/
748
SQLITE_API int sqlite3_exec(
@@ -846,6 +851,7 @@ SQLITE_API int sqlite3_exec(
851
#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
852
#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
853
#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8))
854
+#define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8))
855
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
856
#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
857
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
@@ -1508,7 +1514,7 @@ struct sqlite3_io_methods {
1514
** by clients within the current process, only within other processes.
1515
**
1516
** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1511
-** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use interally by the
1517
+** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the
1518
** [checksum VFS shim] only.
1519
**
1520
** <li>[[SQLITE_FCNTL_RESET_CACHE]]
@@ -2444,7 +2450,7 @@ struct sqlite3_mem_methods {
2450
** is stored in each sorted record and the required column values loaded
2451
** from the database as records are returned in sorted order. The default
2452
** value for this option is to never use this optimization. Specifying a
2447
-** negative value for this option restores the default behaviour.
2453
+** negative value for this option restores the default behavior.
2454
** This option is only available if SQLite is compiled with the
2455
** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2456
**
@@ -2458,6 +2464,22 @@ struct sqlite3_mem_methods {
2464
** configuration setting is never used, then the default maximum is determined
2465
** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2466
** compile-time option is not set, then the default maximum is 1073741824.
2467
+**
2468
+** [[SQLITE_CONFIG_ROWID_IN_VIEW]]
2469
+** <dt>SQLITE_CONFIG_ROWID_IN_VIEW
2470
+** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability
2471
+** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is
2472
+** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2473
+** defaults to on. This configuration option queries the current setting or
2474
+** changes the setting to off or on. The argument is a pointer to an integer.
2475
+** If that integer initially holds a value of 1, then the ability for VIEWs to
2476
+** have ROWIDs is activated. If the integer initially holds zero, then the
2477
+** ability is deactivated. Any other initial value for the integer leaves the
2478
+** setting unchanged. After changes, if any, the integer is written with
2479
+** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite
2480
+** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2481
+** recommended case) then the integer is always filled with zero, regardless
2482
+** if its initial value.
2483
** </dl>
2484
*/
2485
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -2489,6 +2511,7 @@ struct sqlite3_mem_methods {
2511
#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
2512
#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
2513
#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */
2514
+#define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */
2515
2516
/*
2517
** CAPI3REF: Database Connection Configuration Options
@@ -2619,7 +2642,7 @@ struct sqlite3_mem_methods {
2642
** database handle, SQLite checks if this will mean that there are now no
2643
** connections at all to the database. If so, it performs a checkpoint
2644
** operation before closing the connection. This option may be used to
2622
-** override this behaviour. The first parameter passed to this operation
2645
+** override this behavior. The first parameter passed to this operation
2646
** is an integer - positive to disable checkpoints-on-close, or zero (the
2647
** default) to enable them, and negative to leave the setting unchanged.
2648
** The second parameter is a pointer to an integer
@@ -2772,7 +2795,7 @@ struct sqlite3_mem_methods {
2795
** the [VACUUM] command will fail with an obscure error when attempting to
2796
** process a table with generated columns and a descending index. This is
2797
** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2775
-** either generated columns or decending indexes.
2798
+** either generated columns or descending indexes.
2799
** </dd>
2800
**
2801
** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]]
@@ -3053,6 +3076,7 @@ SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
3076
**
3077
** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether
3078
** or not an interrupt is currently in effect for [database connection] D.
3079
+** It returns 1 if an interrupt is currently in effect, or 0 otherwise.
3080
*/
3081
SQLITE_API void sqlite3_interrupt(sqlite3*);
3082
SQLITE_API int sqlite3_is_interrupted(sqlite3*);
@@ -3706,8 +3730,10 @@ SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3730
** M argument should be the bitwise OR-ed combination of
3731
** zero or more [SQLITE_TRACE] constants.
3732
**
3709
-** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
3710
-** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
3733
+** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P)
3734
+** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or
3735
+** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each
3736
+** database connection may have at most one trace callback.
3737
**
3738
** ^The X callback is invoked whenever any of the events identified by
3739
** mask M occur. ^The integer return value from the callback is currently
@@ -4076,7 +4102,7 @@ SQLITE_API int sqlite3_open_v2(
4102
** as F) must be one of:
4103
** <ul>
4104
** <li> A database filename pointer created by the SQLite core and
4079
-** passed into the xOpen() method of a VFS implemention, or
4105
+** passed into the xOpen() method of a VFS implementation, or
4106
** <li> A filename obtained from [sqlite3_db_filename()], or
4107
** <li> A new filename constructed using [sqlite3_create_filename()].
4108
** </ul>
@@ -4189,7 +4215,7 @@ SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
4215
/*
4216
** CAPI3REF: Create and Destroy VFS Filenames
4217
**
4192
-** These interfces are provided for use by [VFS shim] implementations and
4218
+** These interfaces are provided for use by [VFS shim] implementations and
4219
** are not useful outside of that context.
4220
**
4221
** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
@@ -4268,14 +4294,17 @@ SQLITE_API void sqlite3_free_filename(sqlite3_filename);
4294
** </ul>
4295
**
4296
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
4271
-** text that describes the error, as either UTF-8 or UTF-16 respectively.
4297
+** text that describes the error, as either UTF-8 or UTF-16 respectively,
4298
+** or NULL if no error message is available.
4299
+** (See how SQLite handles [invalid UTF] for exceptions to this rule.)
4300
** ^(Memory to hold the error message string is managed internally.
4301
** The application does not need to worry about freeing the result.
4302
** However, the error string might be overwritten or deallocated by
4303
** subsequent calls to other SQLite interface functions.)^
4304
**
4277
-** ^The sqlite3_errstr() interface returns the English-language text
4278
-** that describes the [result code], as UTF-8.
4305
+** ^The sqlite3_errstr(E) interface returns the English-language text
4306
+** that describes the [result code] E, as UTF-8, or NULL if E is not an
4307
+** result code for which a text error message is available.
4308
** ^(Memory to hold the error message string is managed internally
4309
** and must not be freed by the application)^.
4310
**
@@ -4736,6 +4765,41 @@ SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4765
*/
4766
SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4767
4768
+/*
4769
+** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement
4770
+** METHOD: sqlite3_stmt
4771
+**
4772
+** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN
4773
+** setting for [prepared statement] S. If E is zero, then S becomes
4774
+** a normal prepared statement. If E is 1, then S behaves as if
4775
+** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if
4776
+** its SQL text began with "[EXPLAIN QUERY PLAN]".
4777
+**
4778
+** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared.
4779
+** SQLite tries to avoid a reprepare, but a reprepare might be necessary
4780
+** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode.
4781
+**
4782
+** Because of the potential need to reprepare, a call to
4783
+** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be
4784
+** reprepared because it was created using [sqlite3_prepare()] instead of
4785
+** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and
4786
+** hence has no saved SQL text with which to reprepare.
4787
+**
4788
+** Changing the explain setting for a prepared statement does not change
4789
+** the original SQL text for the statement. Hence, if the SQL text originally
4790
+** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0)
4791
+** is called to convert the statement into an ordinary statement, the EXPLAIN
4792
+** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S)
4793
+** output, even though the statement now acts like a normal SQL statement.
4794
+**
4795
+** This routine returns SQLITE_OK if the explain mode is successfully
4796
+** changed, or an error code if the explain mode could not be changed.
4797
+** The explain mode cannot be changed while a statement is active.
4798
+** Hence, it is good practice to call [sqlite3_reset(S)]
4799
+** immediately prior to calling sqlite3_stmt_explain(S,E).
4800
+*/
4801
+SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4802
+
4803
/*
4804
** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4805
** METHOD: sqlite3_stmt
@@ -4899,7 +4963,7 @@ typedef struct sqlite3_context sqlite3_context;
4963
** with it may be passed. ^It is called to dispose of the BLOB or string even
4964
** if the call to the bind API fails, except the destructor is not called if
4965
** the third parameter is a NULL pointer or the fourth parameter is negative.
4902
-** ^ (2) The special constant, [SQLITE_STATIC], may be passsed to indicate that
4966
+** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that
4967
** the application remains responsible for disposing of the object. ^In this
4968
** case, the object and the provided pointer to it must remain valid until
4969
** either the prepared statement is finalized or the same SQL parameter is
@@ -5578,20 +5642,33 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5642
** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5643
** back to the beginning of its program.
5644
**
5581
-** ^If the most recent call to [sqlite3_step(S)] for the
5582
-** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5583
-** or if [sqlite3_step(S)] has never before been called on S,
5584
-** then [sqlite3_reset(S)] returns [SQLITE_OK].
5645
+** ^The return code from [sqlite3_reset(S)] indicates whether or not
5646
+** the previous evaluation of prepared statement S completed successfully.
5647
+** ^If [sqlite3_step(S)] has never before been called on S or if
5648
+** [sqlite3_step(S)] has not been called since the previous call
5649
+** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return
5650
+** [SQLITE_OK].
5651
**
5652
** ^If the most recent call to [sqlite3_step(S)] for the
5653
** [prepared statement] S indicated an error, then
5654
** [sqlite3_reset(S)] returns an appropriate [error code].
5655
+** ^The [sqlite3_reset(S)] interface might also return an [error code]
5656
+** if there were no prior errors but the process of resetting
5657
+** the prepared statement caused a new error. ^For example, if an
5658
+** [INSERT] statement with a [RETURNING] clause is only stepped one time,
5659
+** that one call to [sqlite3_step(S)] might return SQLITE_ROW but
5660
+** the overall statement might still fail and the [sqlite3_reset(S)] call
5661
+** might return SQLITE_BUSY if locking constraints prevent the
5662
+** database change from committing. Therefore, it is important that
5663
+** applications check the return code from [sqlite3_reset(S)] even if
5664
+** no prior call to [sqlite3_step(S)] indicated a problem.
5665
**
5666
** ^The [sqlite3_reset(S)] interface does not change the values
5667
** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5668
*/
5669
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5670
5671
+
5672
/*
5673
** CAPI3REF: Create Or Redefine SQL Functions
5674
** KEYWORDS: {function creation routines}
@@ -5802,7 +5879,7 @@ SQLITE_API int sqlite3_create_window_function(
5879
** [application-defined SQL function]
5880
** that has side-effects or that could potentially leak sensitive information.
5881
** This will prevent attacks in which an application is tricked
5805
-** into using a database file that has had its schema surreptiously
5882
+** into using a database file that has had its schema surreptitiously
5883
** modified to invoke the application-defined function in ways that are
5884
** harmful.
5885
** <p>
@@ -5838,13 +5915,27 @@ SQLITE_API int sqlite3_create_window_function(
5915
** </dd>
5916
**
5917
** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5841
-** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5918
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5919
** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5843
-** Specifying this flag makes no difference for scalar or aggregate user
5844
-** functions. However, if it is not specified for a user-defined window
5845
-** function, then any sub-types belonging to arguments passed to the window
5846
-** function may be discarded before the window function is called (i.e.
5847
-** sqlite3_value_subtype() will always return 0).
5920
+** This flag instructs SQLite to omit some corner-case optimizations that
5921
+** might disrupt the operation of the [sqlite3_value_subtype()] function,
5922
+** causing it to return zero rather than the correct subtype().
5923
+** SQL functions that invokes [sqlite3_value_subtype()] should have this
5924
+** property. If the SQLITE_SUBTYPE property is omitted, then the return
5925
+** value from [sqlite3_value_subtype()] might sometimes be zero even though
5926
+** a non-zero subtype was specified by the function argument expression.
5927
+**
5928
+** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5929
+** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5930
+** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5931
+** result.
5932
+** Every function that invokes [sqlite3_result_subtype()] should have this
5933
+** property. If it does not, then the call to [sqlite3_result_subtype()]
5934
+** might become a no-op if the function is used as term in an
5935
+** [expression index]. On the other hand, SQL functions that never invoke
5936
+** [sqlite3_result_subtype()] should avoid setting this property, as the
5937
+** purpose of this property is to disable certain optimizations that are
5938
+** incompatible with subtypes.
5939
** </dd>
5940
** </dl>
5941
*/
@@ -5852,6 +5943,7 @@ SQLITE_API int sqlite3_create_window_function(
5943
#define SQLITE_DIRECTONLY 0x000080000
5944
#define SQLITE_SUBTYPE 0x000100000
5945
#define SQLITE_INNOCUOUS 0x000200000
5946
+#define SQLITE_RESULT_SUBTYPE 0x001000000
5947
5948
/*
5949
** CAPI3REF: Deprecated Functions
@@ -6048,6 +6140,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6140
** information can be used to pass a limited amount of context from
6141
** one SQL function to another. Use the [sqlite3_result_subtype()]
6142
** routine to set the subtype for the return value of an SQL function.
6143
+**
6144
+** Every [application-defined SQL function] that invoke this interface
6145
+** should include the [SQLITE_SUBTYPE] property in the text
6146
+** encoding argument when the function is [sqlite3_create_function|registered].
6147
+** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6148
+** might return zero instead of the upstream subtype in some corner cases.
6149
*/
6150
SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6151
@@ -6146,48 +6244,56 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6244
** METHOD: sqlite3_context
6245
**
6246
** These functions may be used by (non-aggregate) SQL functions to
6149
-** associate metadata with argument values. If the same value is passed to
6150
-** multiple invocations of the same SQL function during query execution, under
6151
-** some circumstances the associated metadata may be preserved. An example
6152
-** of where this might be useful is in a regular-expression matching
6153
-** function. The compiled version of the regular expression can be stored as
6154
-** metadata associated with the pattern string.
6247
+** associate auxiliary data with argument values. If the same argument
6248
+** value is passed to multiple invocations of the same SQL function during
6249
+** query execution, under some circumstances the associated auxiliary data
6250
+** might be preserved. An example of where this might be useful is in a
6251
+** regular-expression matching function. The compiled version of the regular
6252
+** expression can be stored as auxiliary data associated with the pattern string.
6253
** Then as long as the pattern string remains the same,
6254
** the compiled regular expression can be reused on multiple
6255
** invocations of the same function.
6256
**
6159
-** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
6257
+** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data
6258
** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
6259
** value to the application-defined function. ^N is zero for the left-most
6162
-** function argument. ^If there is no metadata
6260
+** function argument. ^If there is no auxiliary data
6261
** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
6262
** returns a NULL pointer.
6263
**
6166
-** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
6167
-** argument of the application-defined function. ^Subsequent
6264
+** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the
6265
+** N-th argument of the application-defined function. ^Subsequent
6266
** calls to sqlite3_get_auxdata(C,N) return P from the most recent
6169
-** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
6170
-** NULL if the metadata has been discarded.
6267
+** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or
6268
+** NULL if the auxiliary data has been discarded.
6269
** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
6270
** SQLite will invoke the destructor function X with parameter P exactly
6173
-** once, when the metadata is discarded.
6174
-** SQLite is free to discard the metadata at any time, including: <ul>
6271
+** once, when the auxiliary data is discarded.
6272
+** SQLite is free to discard the auxiliary data at any time, including: <ul>
6273
** <li> ^(when the corresponding function parameter changes)^, or
6274
** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
6275
** SQL statement)^, or
6276
** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6277
** parameter)^, or
6278
** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6181
-** allocation error occurs.)^ </ul>
6279
+** allocation error occurs.)^
6280
+** <li> ^(during the original sqlite3_set_auxdata() call if the function
6281
+** is evaluated during query planning instead of during query execution,
6282
+** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6283
**
6183
-** Note the last bullet in particular. The destructor X in
6284
+** Note the last two bullets in particular. The destructor X in
6285
** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6286
** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6287
** should be called near the end of the function implementation and the
6288
** function implementation should not make any use of P after
6188
-** sqlite3_set_auxdata() has been called.
6189
-**
6190
-** ^(In practice, metadata is preserved between function calls for
6289
+** sqlite3_set_auxdata() has been called. Furthermore, a call to
6290
+** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6291
+** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6292
+** condition occurred during the sqlite3_set_auxdata() call or if the
6293
+** function is being evaluated during query planning rather than during
6294
+** query execution.
6295
+**
6296
+** ^(In practice, auxiliary data is preserved between function calls for
6297
** function parameters that are compile-time constants, including literal
6298
** values and [parameters] and expressions composed from the same.)^
6299
**
@@ -6197,10 +6303,67 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6303
**
6304
** These routines must be called from the same thread in which
6305
** the SQL function is running.
6306
+**
6307
+** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()].
6308
*/
6309
SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
6310
SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
6311
6312
+/*
6313
+** CAPI3REF: Database Connection Client Data
6314
+** METHOD: sqlite3
6315
+**
6316
+** These functions are used to associate one or more named pointers
6317
+** with a [database connection].
6318
+** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P
6319
+** to be attached to [database connection] D using name N. Subsequent
6320
+** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P
6321
+** or a NULL pointer if there were no prior calls to
6322
+** sqlite3_set_clientdata() with the same values of D and N.
6323
+** Names are compared using strcmp() and are thus case sensitive.
6324
+**
6325
+** If P and X are both non-NULL, then the destructor X is invoked with
6326
+** argument P on the first of the following occurrences:
6327
+** <ul>
6328
+** <li> An out-of-memory error occurs during the call to
6329
+** sqlite3_set_clientdata() which attempts to register pointer P.
6330
+** <li> A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made
6331
+** with the same D and N parameters.
6332
+** <li> The database connection closes. SQLite does not make any guarantees
6333
+** about the order in which destructors are called, only that all
6334
+** destructors will be called exactly once at some point during the
6335
+** database connection closing process.
6336
+** </ul>
6337
+**
6338
+** SQLite does not do anything with client data other than invoke
6339
+** destructors on the client data at the appropriate time. The intended
6340
+** use for client data is to provide a mechanism for wrapper libraries
6341
+** to store additional information about an SQLite database connection.
6342
+**
6343
+** There is no limit (other than available memory) on the number of different
6344
+** client data pointers (with different names) that can be attached to a
6345
+** single database connection. However, the implementation is optimized
6346
+** for the case of having only one or two different client data names.
6347
+** Applications and wrapper libraries are discouraged from using more than
6348
+** one client data name each.
6349
+**
6350
+** There is no way to enumerate the client data pointers
6351
+** associated with a database connection. The N parameter can be thought
6352
+** of as a secret key such that only code that knows the secret key is able
6353
+** to access the associated data.
6354
+**
6355
+** Security Warning: These interfaces should not be exposed in scripting
6356
+** languages or in other circumstances where it might be possible for an
6357
+** an attacker to invoke them. Any agent that can invoke these interfaces
6358
+** can probably also take control of the process.
6359
+**
6360
+** Database connection client data is only available for SQLite
6361
+** version 3.44.0 ([dateof:3.44.0]) and later.
6362
+**
6363
+** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()].
6364
+*/
6365
+SQLITE_API void *sqlite3_get_clientdata(sqlite3*,const char*);
6366
+SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*));
6367
6368
/*
6369
** CAPI3REF: Constants Defining Special Destructor Behavior
@@ -6402,6 +6565,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6565
** higher order bits are discarded.
6566
** The number of subtype bytes preserved by SQLite might increase
6567
** in future releases of SQLite.
6568
+**
6569
+** Every [application-defined SQL function] that invokes this interface
6570
+** should include the [SQLITE_RESULT_SUBTYPE] property in its
6571
+** text encoding argument when the SQL function is
6572
+** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6573
+** property is omitted from the function that invokes sqlite3_result_subtype(),
6574
+** then in some cases the sqlite3_result_subtype() might fail to set
6575
+** the result subtype.
6576
+**
6577
+** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6578
+** SQL function that invokes the sqlite3_result_subtype() interface
6579
+** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6580
+** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6581
+** by default.
6582
*/
6583
SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6584
@@ -6833,7 +7010,7 @@ SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
7010
SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
7011
7012
/*
6836
-** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
7013
+** CAPI3REF: Allowed return values from sqlite3_txn_state()
7014
** KEYWORDS: {transaction state}
7015
**
7016
** These constants define the current transaction state of a database file.
@@ -6965,7 +7142,7 @@ SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
7142
** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
7143
** previous invocations for that database connection. ^If the callback
7144
** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
6968
-** then the autovacuum steps callback is cancelled. The return value
7145
+** then the autovacuum steps callback is canceled. The return value
7146
** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
7147
** be some other error code if something goes wrong. The current
7148
** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
@@ -7484,6 +7661,10 @@ struct sqlite3_module {
7661
/* The methods above are in versions 1 and 2 of the sqlite_module object.
7662
** Those below are for version 3 and greater. */
7663
int (*xShadowName)(const char*);
7664
+ /* The methods above are in versions 1 through 3 of the sqlite_module object.
7665
+ ** Those below are for version 4 and greater. */
7666
+ int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema,
7667
+ const char *zTabName, int mFlags, char **pzErr);
7668
};
7669
7670
/*
@@ -7971,7 +8152,7 @@ SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
8152
** code is returned and the transaction rolled back.
8153
**
8154
** Calling this function with an argument that is not a NULL pointer or an
7974
-** open blob handle results in undefined behaviour. ^Calling this routine
8155
+** open blob handle results in undefined behavior. ^Calling this routine
8156
** with a null pointer (such as would be returned by a failed call to
8157
** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
8158
** is passed a valid open blob handle, the values returned by the
@@ -8198,9 +8379,11 @@ SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8379
**
8380
** ^(Some systems (for example, Windows 95) do not support the operation
8381
** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
8201
-** will always return SQLITE_BUSY. The SQLite core only ever uses
8202
-** sqlite3_mutex_try() as an optimization so this is acceptable
8203
-** behavior.)^
8382
+** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8383
+** sqlite3_mutex_try() as an optimization, so this is acceptable
8384
+** behavior. The exceptions are unix builds that set the
8385
+** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8386
+** sqlite3_mutex_try() is required.)^
8387
**
8388
** ^The sqlite3_mutex_leave() routine exits a mutex that was
8389
** previously entered by the same thread. The behavior
@@ -8451,6 +8634,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8634
#define SQLITE_TESTCTRL_PRNG_SAVE 5
8635
#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8636
#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */
8637
+#define SQLITE_TESTCTRL_FK_NO_ACTION 7
8638
#define SQLITE_TESTCTRL_BITVEC_TEST 8
8639
#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8640
#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
@@ -8458,6 +8642,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8642
#define SQLITE_TESTCTRL_ASSERT 12
8643
#define SQLITE_TESTCTRL_ALWAYS 13
8644
#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */
8645
+#define SQLITE_TESTCTRL_JSON_SELFCHECK 14
8646
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8647
#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8648
#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
@@ -8479,7 +8664,8 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8664
#define SQLITE_TESTCTRL_TRACEFLAGS 31
8665
#define SQLITE_TESTCTRL_TUNE 32
8666
#define SQLITE_TESTCTRL_LOGEST 33
8482
-#define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */
8667
+#define SQLITE_TESTCTRL_USELONGDOUBLE 34
8668
+#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
8669
8670
/*
8671
** CAPI3REF: SQL Keyword Checking
@@ -9935,7 +10121,7 @@ SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
10121
** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
10122
** <dd>Calls of the form
10123
** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9938
-** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
10124
+** the [xConnect] or [xCreate] methods of a [virtual table] implementation
10125
** prohibits that virtual table from being used from within triggers and
10126
** views.
10127
** </dd>
@@ -10125,7 +10311,7 @@ SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
10311
** communicated to the xBestIndex method as a
10312
** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
10313
** this constraint, it must set the corresponding
10128
-** aConstraintUsage[].argvIndex to a postive integer. ^(Then, under
10314
+** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under
10315
** the usual mode of handling IN operators, SQLite generates [bytecode]
10316
** that invokes the [xFilter|xFilter() method] once for each value
10317
** on the right-hand side of the IN operator.)^ Thus the virtual table
@@ -10554,7 +10740,7 @@ SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10740
** When the [sqlite3_blob_write()] API is used to update a blob column,
10741
** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10742
** in this case the new values are not available. In this case, when a
10557
-** callback made with op==SQLITE_DELETE is actuall a write using the
10743
+** callback made with op==SQLITE_DELETE is actually a write using the
10744
** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10745
** the index of the column being written. In other cases, where the
10746
** pre-update hook is being invoked for some other reason, including a
@@ -10815,6 +11001,13 @@ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const c
11001
** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
11002
** of the database exists.
11003
**
11004
+** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set,
11005
+** the returned buffer content will remain accessible and unchanged
11006
+** until either the next write operation on the connection or when
11007
+** the connection is closed, and applications must not modify the
11008
+** buffer. If the bit had been clear, the returned buffer will not
11009
+** be accessed by SQLite after the call.
11010
+**
11011
** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
11012
** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
11013
** allocation error occurs.
@@ -10863,6 +11056,9 @@ SQLITE_API unsigned char *sqlite3_serialize(
11056
** SQLite will try to increase the buffer size using sqlite3_realloc64()
11057
** if writes on the database cause it to grow larger than M bytes.
11058
**
11059
+** Applications must not modify the buffer P or invalidate it before
11060
+** the database connection D is closed.
11061
+**
11062
** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
11063
** database is currently in a read transaction or is involved in a backup
11064
** operation.
@@ -10871,6 +11067,13 @@ SQLITE_API unsigned char *sqlite3_serialize(
11067
** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
11068
** function returns SQLITE_ERROR.
11069
**
11070
+** The deserialized database should not be in [WAL mode]. If the database
11071
+** is in WAL mode, then any attempt to use the database file will result
11072
+** in an [SQLITE_CANTOPEN] error. The application can set the
11073
+** [file format version numbers] (bytes 18 and 19) of the input database P
11074
+** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the
11075
+** database file into rollback mode and work around this limitation.
11076
+**
11077
** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
11078
** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
11079
** [sqlite3_free()] is invoked on argument P prior to returning.
@@ -11943,6 +12146,18 @@ SQLITE_API int sqlite3changeset_concat(
12146
);
12147
12148
12149
+/*
12150
+** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
12151
+*/
12152
+SQLITE_API int sqlite3changeset_upgrade(
12153
+ sqlite3 *db,
12154
+ const char *zDb,
12155
+ int nIn, const void *pIn, /* Input changeset */
12156
+ int *pnOut, void **ppOut /* OUT: Inverse of input */
12157
+);
12158
+
12159
+
12160
+
12161
/*
12162
** CAPI3REF: Changegroup Handle
12163
**
@@ -11989,6 +12204,38 @@ typedef struct sqlite3_changegroup sqlite3_changegroup;
12204
*/
12205
SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12206
12207
+/*
12208
+** CAPI3REF: Add a Schema to a Changegroup
12209
+** METHOD: sqlite3_changegroup_schema
12210
+**
12211
+** This method may be used to optionally enforce the rule that the changesets
12212
+** added to the changegroup handle must match the schema of database zDb
12213
+** ("main", "temp", or the name of an attached database). If
12214
+** sqlite3changegroup_add() is called to add a changeset that is not compatible
12215
+** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
12216
+** object is left in an undefined state.
12217
+**
12218
+** A changeset schema is considered compatible with the database schema in
12219
+** the same way as for sqlite3changeset_apply(). Specifically, for each
12220
+** table in the changeset, there exists a database table with:
12221
+**
12222
+** <ul>
12223
+** <li> The name identified by the changeset, and
12224
+** <li> at least as many columns as recorded in the changeset, and
12225
+** <li> the primary key columns in the same position as recorded in
12226
+** the changeset.
12227
+** </ul>
12228
+**
12229
+** The output of the changegroup object always has the same schema as the
12230
+** database nominated using this function. In cases where changesets passed
12231
+** to sqlite3changegroup_add() have fewer columns than the corresponding table
12232
+** in the database schema, these are filled in using the default column
12233
+** values from the database schema. This makes it possible to combined
12234
+** changesets that have different numbers of columns for a single table
12235
+** within a changegroup, provided that they are otherwise compatible.
12236
+*/
12237
+SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
12238
+
12239
/*
12240
** CAPI3REF: Add A Changeset To A Changegroup
12241
** METHOD: sqlite3_changegroup
@@ -12057,13 +12304,18 @@ SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
12304
** If the new changeset contains changes to a table that is already present
12305
** in the changegroup, then the number of columns and the position of the
12306
** primary key columns for the table must be consistent. If this is not the
12060
-** case, this function fails with SQLITE_SCHEMA. If the input changeset
12061
-** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
12062
-** returned. Or, if an out-of-memory condition occurs during processing, this
12063
-** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
12064
-** of the final contents of the changegroup is undefined.
12307
+** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
12308
+** object has been configured with a database schema using the
12309
+** sqlite3changegroup_schema() API, then it is possible to combine changesets
12310
+** with different numbers of columns for a single table, provided that
12311
+** they are otherwise compatible.
12312
+**
12313
+** If the input changeset appears to be corrupt and the corruption is
12314
+** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
12315
+** occurs during processing, this function returns SQLITE_NOMEM.
12316
**
12066
-** If no error occurs, SQLITE_OK is returned.
12317
+** In all cases, if an error occurs the state of the final contents of the
12318
+** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
12319
*/
12320
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12321
@@ -12328,10 +12580,17 @@ SQLITE_API int sqlite3changeset_apply_v2(
12580
** <li>an insert change if all fields of the conflicting row match
12581
** the row being inserted.
12582
** </ul>
12583
+**
12584
+** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
12585
+** If this flag it set, then all foreign key constraints in the target
12586
+** database behave as if they were declared with "ON UPDATE NO ACTION ON
12587
+** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
12588
+** or SET DEFAULT.
12589
*/
12590
#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
12591
#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
12592
#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
12593
+#define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008
12594
12595
/*
12596
** CAPI3REF: Constants Passed To The Conflict Handler
@@ -12897,8 +13156,11 @@ struct Fts5PhraseIter {
13156
** created with the "columnsize=0" option.
13157
**
13158
** xColumnText:
12900
-** This function attempts to retrieve the text of column iCol of the
12901
-** current document. If successful, (*pz) is set to point to a buffer
13159
+** If parameter iCol is less than zero, or greater than or equal to the
13160
+** number of columns in the table, SQLITE_RANGE is returned.
13161
+**
13162
+** Otherwise, this function attempts to retrieve the text of column iCol of
13163
+** the current document. If successful, (*pz) is set to point to a buffer
13164
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
13165
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
13166
** if an error occurs, an SQLite error code is returned and the final values
@@ -12908,8 +13170,10 @@ struct Fts5PhraseIter {
13170
** Returns the number of phrases in the current query expression.
13171
**
13172
** xPhraseSize:
12911
-** Returns the number of tokens in phrase iPhrase of the query. Phrases
12912
-** are numbered starting from zero.
13173
+** If parameter iCol is less than zero, or greater than or equal to the
13174
+** number of phrases in the current query, as returned by xPhraseCount,
13175
+** 0 is returned. Otherwise, this function returns the number of tokens in
13176
+** phrase iPhrase of the query. Phrases are numbered starting from zero.
13177
**
13178
** xInstCount:
13179
** Set *pnInst to the total number of occurrences of all phrases within
@@ -12925,12 +13189,13 @@ struct Fts5PhraseIter {
13189
** Query for the details of phrase match iIdx within the current row.
13190
** Phrase matches are numbered starting from zero, so the iIdx argument
13191
** should be greater than or equal to zero and smaller than the value
12928
-** output by xInstCount().
13192
+** output by xInstCount(). If iIdx is less than zero or greater than
13193
+** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
13194
**
12930
-** Usually, output parameter *piPhrase is set to the phrase number, *piCol
13195
+** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
13196
** to the column in which it occurs and *piOff the token offset of the
12932
-** first token of the phrase. Returns SQLITE_OK if successful, or an error
12933
-** code (i.e. SQLITE_NOMEM) if an error occurs.
13197
+** first token of the phrase. SQLITE_OK is returned if successful, or an
13198
+** error code (i.e. SQLITE_NOMEM) if an error occurs.
13199
**
13200
** This API can be quite slow if used with an FTS5 table created with the
13201
** "detail=none" or "detail=column" option.
@@ -12956,6 +13221,10 @@ struct Fts5PhraseIter {
13221
** Invoking Api.xUserData() returns a copy of the pointer passed as
13222
** the third argument to pUserData.
13223
**
13224
+** If parameter iPhrase is less than zero, or greater than or equal to
13225
+** the number of phrases in the query, as returned by xPhraseCount(),
13226
+** this function returns SQLITE_RANGE.
13227
+**
13228
** If the callback function returns any value other than SQLITE_OK, the
13229
** query is abandoned and the xQueryPhrase function returns immediately.
13230
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
@@ -13070,6 +13339,39 @@ struct Fts5PhraseIter {
13339
**
13340
** xPhraseNextColumn()
13341
** See xPhraseFirstColumn above.
13342
+**
13343
+** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13344
+** This is used to access token iToken of phrase iPhrase of the current
13345
+** query. Before returning, output parameter *ppToken is set to point
13346
+** to a buffer containing the requested token, and *pnToken to the
13347
+** size of this buffer in bytes.
13348
+**
13349
+** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13350
+** or equal to the number of phrases in the query as reported by
13351
+** xPhraseCount(), or if iToken is equal to or greater than the number of
13352
+** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13353
+ are both zeroed.
13354
+**
13355
+** The output text is not a copy of the query text that specified the
13356
+** token. It is the output of the tokenizer module. For tokendata=1
13357
+** tables, this includes any embedded 0x00 and trailing data.
13358
+**
13359
+** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13360
+** This is used to access token iToken of phrase hit iIdx within the
13361
+** current row. If iIdx is less than zero or greater than or equal to the
13362
+** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13363
+** output variable (*ppToken) is set to point to a buffer containing the
13364
+** matching document token, and (*pnToken) to the size of that buffer in
13365
+** bytes. This API is not available if the specified token matches a
13366
+** prefix query term. In that case both output variables are always set
13367
+** to 0.
13368
+**
13369
+** The output text is not a copy of the document text that was tokenized.
13370
+** It is the output of the tokenizer module. For tokendata=1 tables, this
13371
+** includes any embedded 0x00 and trailing data.
13372
+**
13373
+** This API can be quite slow if used with an FTS5 table created with the
13374
+** "detail=none" or "detail=column" option.
13375
*/
13376
struct Fts5ExtensionApi {
13377
int iVersion; /* Currently always set to 3 */
@@ -13107,6 +13409,13 @@ struct Fts5ExtensionApi {
13409
13410
int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13411
void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13412
+
13413
+ /* Below this point are iVersion>=3 only */
13414
+ int (*xQueryToken)(Fts5Context*,
13415
+ int iPhrase, int iToken,
13416
+ const char **ppToken, int *pnToken
13417
+ );
13418
+ int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
13419
};
13420
13421
/*
@@ -13301,8 +13610,8 @@ struct Fts5ExtensionApi {
13610
** as separate queries of the FTS index are required for each synonym.
13611
**
13612
** When using methods (2) or (3), it is important that the tokenizer only
13304
-** provide synonyms when tokenizing document text (method (2)) or query
13305
-** text (method (3)), not both. Doing so will not cause any errors, but is
13613
+** provide synonyms when tokenizing document text (method (3)) or query
13614
+** text (method (2)), not both. Doing so will not cause any errors, but is
13615
** inefficient.
13616
*/
13617
typedef struct Fts5Tokenizer Fts5Tokenizer;
@@ -13350,7 +13659,7 @@ struct fts5_api {
13659
int (*xCreateTokenizer)(
13660
fts5_api *pApi,
13661
const char *zName,
13353
- void *pContext,
13662
+ void *pUserData,
13663
fts5_tokenizer *pTokenizer,
13664
void (*xDestroy)(void*)
13665
);
@@ -13359,7 +13668,7 @@ struct fts5_api {
13668
int (*xFindTokenizer)(
13669
fts5_api *pApi,
13670
const char *zName,
13362
- void **ppContext,
13671
+ void **ppUserData,
13672
fts5_tokenizer *pTokenizer
13673
);
13674
@@ -13367,7 +13676,7 @@ struct fts5_api {
13676
int (*xCreateFunction)(
13677
fts5_api *pApi,
13678
const char *zName,
13370
- void *pContext,
13679
+ void *pUserData,
13680
fts5_extension_function xFunction,
13681
void (*xDestroy)(void*)
13682
);
@@ -13478,7 +13787,7 @@ struct fts5_api {
13787
** level of recursion for each term. A stack overflow can result
13788
** if the number of terms is too large. In practice, most SQL
13789
** never has more than 3 or 4 terms. Use a value of 0 to disable
13481
-** any limit on the number of terms in a compount SELECT.
13790
+** any limit on the number of terms in a compound SELECT.
13791
*/
13792
#ifndef SQLITE_MAX_COMPOUND_SELECT
13793
# define SQLITE_MAX_COMPOUND_SELECT 500
@@ -13593,7 +13902,7 @@ struct fts5_api {
13902
** max_page_count macro.
13903
*/
13904
#ifndef SQLITE_MAX_PAGE_COUNT
13596
-# define SQLITE_MAX_PAGE_COUNT 1073741823
13905
+# define SQLITE_MAX_PAGE_COUNT 0xfffffffe /* 4294967294 */
13906
#endif
13907
13908
/*
@@ -13722,6 +14031,29 @@ struct fts5_api {
14031
# endif
14032
#endif
14033
14034
+/*
14035
+** Enable SQLITE_USE_SEH by default on MSVC builds. Only omit
14036
+** SEH support if the -DSQLITE_OMIT_SEH option is given.
14037
+*/
14038
+#if defined(_MSC_VER) && !defined(SQLITE_OMIT_SEH)
14039
+# define SQLITE_USE_SEH 1
14040
+#else
14041
+# undef SQLITE_USE_SEH
14042
+#endif
14043
+
14044
+/*
14045
+** Enable SQLITE_DIRECT_OVERFLOW_READ, unless the build explicitly
14046
+** disables it using -DSQLITE_DIRECT_OVERFLOW_READ=0
14047
+*/
14048
+#if defined(SQLITE_DIRECT_OVERFLOW_READ) && SQLITE_DIRECT_OVERFLOW_READ+1==1
14049
+ /* Disable if -DSQLITE_DIRECT_OVERFLOW_READ=0 */
14050
+# undef SQLITE_DIRECT_OVERFLOW_READ
14051
+#else
14052
+ /* In all other cases, enable */
14053
+# define SQLITE_DIRECT_OVERFLOW_READ 1
14054
+#endif
14055
+
14056
+
14057
/*
14058
** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
14059
** 0 means mutexes are permanently disable and the library is never
@@ -14581,8 +14913,31 @@ typedef INT16_TYPE LogEst;
14913
** the end of buffer S. This macro returns true if P points to something
14914
** contained within the buffer S.
14915
*/
14584
-#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14916
+#define SQLITE_WITHIN(P,S,E) (((uptr)(P)>=(uptr)(S))&&((uptr)(P)<(uptr)(E)))
14917
14918
+/*
14919
+** P is one byte past the end of a large buffer. Return true if a span of bytes
14920
+** between S..E crosses the end of that buffer. In other words, return true
14921
+** if the sub-buffer S..E-1 overflows the buffer whose last byte is P-1.
14922
+**
14923
+** S is the start of the span. E is one byte past the end of end of span.
14924
+**
14925
+** P
14926
+** |-----------------| FALSE
14927
+** |-------|
14928
+** S E
14929
+**
14930
+** P
14931
+** |-----------------|
14932
+** |-------| TRUE
14933
+** S E
14934
+**
14935
+** P
14936
+** |-----------------|
14937
+** |-------| FALSE
14938
+** S E
14939
+*/
14940
+#define SQLITE_OVERFLOW(P,S,E) (((uptr)(S)<(uptr)(P))&&((uptr)(E)>(uptr)(P)))
14941
14942
/*
14943
** Macros to determine whether the machine is big or little endian,
@@ -14592,16 +14947,33 @@ typedef INT16_TYPE LogEst;
14947
** using C-preprocessor macros. If that is unsuccessful, or if
14948
** -DSQLITE_BYTEORDER=0 is set, then byte-order is determined
14949
** at run-time.
14950
+**
14951
+** If you are building SQLite on some obscure platform for which the
14952
+** following ifdef magic does not work, you can always include either:
14953
+**
14954
+** -DSQLITE_BYTEORDER=1234
14955
+**
14956
+** or
14957
+**
14958
+** -DSQLITE_BYTEORDER=4321
14959
+**
14960
+** to cause the build to work for little-endian or big-endian processors,
14961
+** respectively.
14962
*/
14596
-#ifndef SQLITE_BYTEORDER
14597
-# if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14963
+#ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
14964
+# if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
14965
+# define SQLITE_BYTEORDER 4321
14966
+# elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
14967
+# define SQLITE_BYTEORDER 1234
14968
+# elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
14969
+# define SQLITE_BYTEORDER 4321
14970
+# elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
14971
defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
14972
defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
14973
defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
14601
-# define SQLITE_BYTEORDER 1234
14602
-# elif defined(sparc) || defined(__ppc__) || \
14603
- defined(__ARMEB__) || defined(__AARCH64EB__)
14604
-# define SQLITE_BYTEORDER 4321
14974
+# define SQLITE_BYTEORDER 1234
14975
+# elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
14976
+# define SQLITE_BYTEORDER 4321
14977
# else
14978
# define SQLITE_BYTEORDER 0
14979
# endif
@@ -14752,6 +15124,7 @@ SQLITE_PRIVATE u32 sqlite3TreeTrace;
15124
** 0x00010000 Beginning of DELETE/INSERT/UPDATE processing
15125
** 0x00020000 Transform DISTINCT into GROUP BY
15126
** 0x00040000 SELECT tree dump after all code has been generated
15127
+** 0x00080000 NOT NULL strength reduction
15128
*/
15129
15130
/*
@@ -14816,7 +15189,7 @@ struct BusyHandler {
15189
/*
15190
** Name of table that holds the database schema.
15191
**
14819
-** The PREFERRED names are used whereever possible. But LEGACY is also
15192
+** The PREFERRED names are used wherever possible. But LEGACY is also
15193
** used for backwards compatibility.
15194
**
15195
** 1. Queries can use either the PREFERRED or the LEGACY names
@@ -14925,11 +15298,13 @@ typedef struct Column Column;
15298
typedef struct Cte Cte;
15299
typedef struct CteUse CteUse;
15300
typedef struct Db Db;
15301
+typedef struct DbClientData DbClientData;
15302
typedef struct DbFixer DbFixer;
15303
typedef struct Schema Schema;
15304
typedef struct Expr Expr;
15305
typedef struct ExprList ExprList;
15306
typedef struct FKey FKey;
15307
+typedef struct FpDecode FpDecode;
15308
typedef struct FuncDestructor FuncDestructor;
15309
typedef struct FuncDef FuncDef;
15310
typedef struct FuncDefHash FuncDefHash;
@@ -14948,6 +15323,7 @@ typedef struct Parse Parse;
15323
typedef struct ParseCleanup ParseCleanup;
15324
typedef struct PreUpdate PreUpdate;
15325
typedef struct PrintfArguments PrintfArguments;
15326
+typedef struct RCStr RCStr;
15327
typedef struct RenameToken RenameToken;
15328
typedef struct Returning Returning;
15329
typedef struct RowSet RowSet;
@@ -15561,7 +15937,7 @@ SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager*);
15937
SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
15938
SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
15939
SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
15564
-SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
15940
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, u64*);
15941
SQLITE_PRIVATE void sqlite3PagerClearCache(Pager*);
15942
SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
15943
@@ -15585,6 +15961,10 @@ SQLITE_PRIVATE void sqlite3PagerRefdump(Pager*);
15961
# define enable_simulated_io_errors()
15962
#endif
15963
15964
+#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
15965
+SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager*);
15966
+#endif
15967
+
15968
#endif /* SQLITE_PAGER_H */
15969
15970
/************** End of pager.h ***********************************************/
@@ -15914,9 +16294,7 @@ SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int flags);
16294
SQLITE_PRIVATE i64 sqlite3BtreeIntegerKey(BtCursor*);
16295
SQLITE_PRIVATE void sqlite3BtreeCursorPin(BtCursor*);
16296
SQLITE_PRIVATE void sqlite3BtreeCursorUnpin(BtCursor*);
15917
-#ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC
16297
SQLITE_PRIVATE i64 sqlite3BtreeOffset(BtCursor*);
15919
-#endif
16298
SQLITE_PRIVATE int sqlite3BtreePayload(BtCursor*, u32 offset, u32 amt, void*);
16299
SQLITE_PRIVATE const void *sqlite3BtreePayloadFetch(BtCursor*, u32 *pAmt);
16300
SQLITE_PRIVATE u32 sqlite3BtreePayloadSize(BtCursor*);
@@ -16146,6 +16524,7 @@ typedef struct VdbeOpList VdbeOpList;
16524
#define P4_INT64 (-13) /* P4 is a 64-bit signed integer */
16525
#define P4_INTARRAY (-14) /* P4 is a vector of 32-bit integers */
16526
#define P4_FUNCCTX (-15) /* P4 is a pointer to an sqlite3_context object */
16527
+#define P4_TABLEREF (-16) /* Like P4_TABLE, but reference counted */
16528
16529
/* Error message codes for OP_Halt */
16530
#define P5_ConstraintNotNull 1
@@ -16361,19 +16740,22 @@ typedef struct VdbeOpList VdbeOpList;
16740
#define OP_VCreate 171
16741
#define OP_VDestroy 172
16742
#define OP_VOpen 173
16364
-#define OP_VInitIn 174 /* synopsis: r[P2]=ValueList(P1,P3) */
16365
-#define OP_VColumn 175 /* synopsis: r[P3]=vcolumn(P2) */
16366
-#define OP_VRename 176
16367
-#define OP_Pagecount 177
16368
-#define OP_MaxPgcnt 178
16369
-#define OP_ClrSubtype 179 /* synopsis: r[P1].subtype = 0 */
16370
-#define OP_FilterAdd 180 /* synopsis: filter(P1) += key(P3@P4) */
16371
-#define OP_Trace 181
16372
-#define OP_CursorHint 182
16373
-#define OP_ReleaseReg 183 /* synopsis: release r[P1@P2] mask P3 */
16374
-#define OP_Noop 184
16375
-#define OP_Explain 185
16376
-#define OP_Abortable 186
16743
+#define OP_VCheck 174
16744
+#define OP_VInitIn 175 /* synopsis: r[P2]=ValueList(P1,P3) */
16745
+#define OP_VColumn 176 /* synopsis: r[P3]=vcolumn(P2) */
16746
+#define OP_VRename 177
16747
+#define OP_Pagecount 178
16748
+#define OP_MaxPgcnt 179
16749
+#define OP_ClrSubtype 180 /* synopsis: r[P1].subtype = 0 */
16750
+#define OP_GetSubtype 181 /* synopsis: r[P2] = r[P1].subtype */
16751
+#define OP_SetSubtype 182 /* synopsis: r[P2].subtype = r[P1] */
16752
+#define OP_FilterAdd 183 /* synopsis: filter(P1) += key(P3@P4) */
16753
+#define OP_Trace 184
16754
+#define OP_CursorHint 185
16755
+#define OP_ReleaseReg 186 /* synopsis: release r[P1@P2] mask P3 */
16756
+#define OP_Noop 187
16757
+#define OP_Explain 188
16758
+#define OP_Abortable 189
16759
16760
/* Properties such as "out2" or "jump" that are specified in
16761
** comments following the "case" for each opcode in the vdbe.c
@@ -16391,7 +16773,7 @@ typedef struct VdbeOpList VdbeOpList;
16773
/* 8 */ 0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x01, 0x01,\
16774
/* 16 */ 0x03, 0x03, 0x01, 0x12, 0x01, 0x49, 0x49, 0x49,\
16775
/* 24 */ 0x49, 0x01, 0x49, 0x49, 0x49, 0x49, 0x49, 0x49,\
16394
-/* 32 */ 0x41, 0x01, 0x01, 0x01, 0x41, 0x01, 0x41, 0x41,\
16776
+/* 32 */ 0x41, 0x01, 0x41, 0x41, 0x41, 0x01, 0x41, 0x41,\
16777
/* 40 */ 0x41, 0x41, 0x41, 0x26, 0x26, 0x41, 0x23, 0x0b,\
16778
/* 48 */ 0x01, 0x01, 0x03, 0x03, 0x0b, 0x0b, 0x0b, 0x0b,\
16779
/* 56 */ 0x0b, 0x0b, 0x01, 0x03, 0x03, 0x03, 0x01, 0x41,\
@@ -16403,14 +16785,14 @@ typedef struct VdbeOpList VdbeOpList;
16785
/* 104 */ 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26, 0x26,\
16786
/* 112 */ 0x40, 0x00, 0x12, 0x40, 0x40, 0x10, 0x40, 0x00,\
16787
/* 120 */ 0x00, 0x00, 0x40, 0x00, 0x40, 0x40, 0x10, 0x10,\
16406
-/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50,\
16788
+/* 128 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x50,\
16789
/* 136 */ 0x00, 0x40, 0x04, 0x04, 0x00, 0x40, 0x50, 0x40,\
16790
/* 144 */ 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,\
16791
/* 152 */ 0x00, 0x10, 0x00, 0x00, 0x06, 0x10, 0x00, 0x04,\
16792
/* 160 */ 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
16411
-/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x50, 0x40,\
16412
-/* 176 */ 0x00, 0x10, 0x10, 0x02, 0x00, 0x00, 0x00, 0x00,\
16413
-/* 184 */ 0x00, 0x00, 0x00,}
16793
+/* 168 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x10, 0x50,\
16794
+/* 176 */ 0x40, 0x00, 0x10, 0x10, 0x02, 0x12, 0x12, 0x00,\
16795
+/* 184 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,}
16796
16797
/* The resolve3P2Values() routine is able to run faster if it knows
16798
** the value of the largest JUMP opcode. The smaller the maximum
@@ -16585,7 +16967,7 @@ SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
16967
** The VdbeCoverage macros are used to set a coverage testing point
16968
** for VDBE branch instructions. The coverage testing points are line
16969
** numbers in the sqlite3.c source file. VDBE branch coverage testing
16588
-** only works with an amalagmation build. That's ok since a VDBE branch
16970
+** only works with an amalgamation build. That's ok since a VDBE branch
16971
** coverage build designed for testing the test suite only. No application
16972
** should ever ship with VDBE branch coverage measuring turned on.
16973
**
@@ -16603,7 +16985,7 @@ SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
16985
** // NULL option is not possible
16986
**
16987
** VdbeCoverageEqNe(v) // Previous OP_Jump is only interested
16606
-** // in distingishing equal and not-equal.
16988
+** // in distinguishing equal and not-equal.
16989
**
16990
** Every VDBE branch operation must be tagged with one of the macros above.
16991
** If not, then when "make test" is run with -DSQLITE_VDBE_COVERAGE and
@@ -16613,7 +16995,7 @@ SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
16995
** During testing, the test application will invoke
16996
** sqlite3_test_control(SQLITE_TESTCTRL_VDBE_COVERAGE,...) to set a callback
16997
** routine that is invoked as each bytecode branch is taken. The callback
16616
-** contains the sqlite3.c source line number ov the VdbeCoverage macro and
16998
+** contains the sqlite3.c source line number of the VdbeCoverage macro and
16999
** flags to indicate whether or not the branch was taken. The test application
17000
** is responsible for keeping track of this and reporting byte-code branches
17001
** that are never taken.
@@ -16952,7 +17334,7 @@ SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
17334
/*
17335
** Default synchronous levels.
17336
**
16955
-** Note that (for historcal reasons) the PAGER_SYNCHRONOUS_* macros differ
17337
+** Note that (for historical reasons) the PAGER_SYNCHRONOUS_* macros differ
17338
** from the SQLITE_DEFAULT_SYNCHRONOUS value by 1.
17339
**
17340
** PAGER_SYNCHRONOUS DEFAULT_SYNCHRONOUS
@@ -16991,7 +17373,7 @@ struct Db {
17373
** An instance of the following structure stores a database schema.
17374
**
17375
** Most Schema objects are associated with a Btree. The exception is
16994
-** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
17376
+** the Schema for the TEMP database (sqlite3.aDb[1]) which is free-standing.
17377
** In shared cache mode, a single Schema object can be shared by multiple
17378
** Btrees that refer to the same underlying BtShared object.
17379
**
@@ -17102,7 +17484,7 @@ struct Lookaside {
17484
LookasideSlot *pInit; /* List of buffers not previously used */
17485
LookasideSlot *pFree; /* List of available buffers */
17486
#ifndef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17105
- LookasideSlot *pSmallInit; /* List of small buffers not prediously used */
17487
+ LookasideSlot *pSmallInit; /* List of small buffers not previously used */
17488
LookasideSlot *pSmallFree; /* List of available small buffers */
17489
void *pMiddle; /* First byte past end of full-size buffers and
17490
** the first byte of LOOKASIDE_SMALL buffers */
@@ -17119,7 +17501,7 @@ struct LookasideSlot {
17501
#define EnableLookaside db->lookaside.bDisable--;\
17502
db->lookaside.sz=db->lookaside.bDisable?0:db->lookaside.szTrue
17503
17122
-/* Size of the smaller allocations in two-size lookside */
17504
+/* Size of the smaller allocations in two-size lookaside */
17505
#ifdef SQLITE_OMIT_TWOSIZE_LOOKASIDE
17506
# define LOOKASIDE_SMALL 0
17507
#else
@@ -17319,6 +17701,7 @@ struct sqlite3 {
17701
i64 nDeferredCons; /* Net deferred constraints this transaction. */
17702
i64 nDeferredImmCons; /* Net deferred immediate constraints */
17703
int *pnBytesFreed; /* If not NULL, increment this in DbFree() */
17704
+ DbClientData *pDbData; /* sqlite3_set_clientdata() content */
17705
#ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
17706
/* The following variables are all protected by the STATIC_MAIN
17707
** mutex, not by sqlite3.mutex. They are used by code in notify.c.
@@ -17401,6 +17784,7 @@ struct sqlite3 {
17784
/* the count using a callback. */
17785
#define SQLITE_CorruptRdOnly HI(0x00002) /* Prohibit writes due to error */
17786
#define SQLITE_ReadUncommit HI(0x00004) /* READ UNCOMMITTED in shared-cache */
17787
+#define SQLITE_FkNoAction HI(0x00008) /* Treat all FK as NO ACTION */
17788
17789
/* Flags used only if debugging */
17790
#ifdef SQLITE_DEBUG
@@ -17458,6 +17842,7 @@ struct sqlite3 {
17842
#define SQLITE_IndexedExpr 0x01000000 /* Pull exprs from index when able */
17843
#define SQLITE_Coroutines 0x02000000 /* Co-routines for subqueries */
17844
#define SQLITE_NullUnusedCols 0x04000000 /* NULL unused columns in subqueries */
17845
+#define SQLITE_OnePass 0x08000000 /* Single-pass DELETE and UPDATE */
17846
#define SQLITE_AllOpts 0xffffffff /* All optimizations */
17847
17848
/*
@@ -17540,6 +17925,7 @@ struct FuncDestructor {
17925
** SQLITE_FUNC_ANYORDER == NC_OrderAgg == SF_OrderByReqd
17926
** SQLITE_FUNC_LENGTH == OPFLAG_LENGTHARG
17927
** SQLITE_FUNC_TYPEOF == OPFLAG_TYPEOFARG
17928
+** SQLITE_FUNC_BYTELEN == OPFLAG_BYTELENARG
17929
** SQLITE_FUNC_CONSTANT == SQLITE_DETERMINISTIC from the API
17930
** SQLITE_FUNC_DIRECT == SQLITE_DIRECTONLY from the API
17931
** SQLITE_FUNC_UNSAFE == SQLITE_INNOCUOUS -- opposite meanings!!!
@@ -17547,7 +17933,7 @@ struct FuncDestructor {
17933
**
17934
** Note that even though SQLITE_FUNC_UNSAFE and SQLITE_INNOCUOUS have the
17935
** same bit value, their meanings are inverted. SQLITE_FUNC_UNSAFE is
17550
-** used internally and if set means tha the function has side effects.
17936
+** used internally and if set means that the function has side effects.
17937
** SQLITE_INNOCUOUS is used by application code and means "not unsafe".
17938
** See multiple instances of tag-20230109-1.
17939
*/
@@ -17558,6 +17944,7 @@ struct FuncDestructor {
17944
#define SQLITE_FUNC_NEEDCOLL 0x0020 /* sqlite3GetFuncCollSeq() might be called*/
17945
#define SQLITE_FUNC_LENGTH 0x0040 /* Built-in length() function */
17946
#define SQLITE_FUNC_TYPEOF 0x0080 /* Built-in typeof() function */
17947
+#define SQLITE_FUNC_BYTELEN 0x00c0 /* Built-in octet_length() function */
17948
#define SQLITE_FUNC_COUNT 0x0100 /* Built-in count(*) aggregate */
17949
/* 0x0200 -- available for reuse */
17950
#define SQLITE_FUNC_UNLIKELY 0x0400 /* Built-in unlikely() function */
@@ -17566,14 +17953,15 @@ struct FuncDestructor {
17953
#define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17954
** single query - might change over time */
17955
#define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17569
-/* 0x8000 -- available for reuse */
17956
+#define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
17957
#define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17958
#define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17959
#define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17573
-#define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17960
+/* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
17961
#define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17962
#define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17963
#define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17964
+/* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
17965
#define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17966
17967
/* Identifier numbers for each in-line function */
@@ -17665,10 +18053,11 @@ struct FuncDestructor {
18053
#define MFUNCTION(zName, nArg, xPtr, xFunc) \
18054
{nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
18055
xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
17668
-#define JFUNCTION(zName, nArg, iArg, xFunc) \
17669
- {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|\
17670
- SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
17671
- SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
18056
+#define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, bJsonB, iArg, xFunc) \
18057
+ {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
18058
+ SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
18059
+ ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
18060
+ SQLITE_INT_TO_PTR(iArg|((bJsonB)*JSON_BLOB)),0,xFunc,0, 0, 0, #zName, {0} }
18061
#define INLINE_FUNC(zName, nArg, iArg, mFlags) \
18062
{nArg, SQLITE_FUNC_BUILTIN|\
18063
SQLITE_UTF8|SQLITE_FUNC_INLINE|SQLITE_FUNC_CONSTANT|(mFlags), \
@@ -18066,6 +18455,15 @@ struct Table {
18455
#define HasRowid(X) (((X)->tabFlags & TF_WithoutRowid)==0)
18456
#define VisibleRowid(X) (((X)->tabFlags & TF_NoVisibleRowid)==0)
18457
18458
+/* Macro is true if the SQLITE_ALLOW_ROWID_IN_VIEW (mis-)feature is
18459
+** available. By default, this macro is false
18460
+*/
18461
+#ifndef SQLITE_ALLOW_ROWID_IN_VIEW
18462
+# define ViewCanHaveRowid 0
18463
+#else
18464
+# define ViewCanHaveRowid (sqlite3Config.mNoVisibleRowid==0)
18465
+#endif
18466
+
18467
/*
18468
** Each foreign key constraint is an instance of the following structure.
18469
**
@@ -18137,7 +18535,7 @@ struct FKey {
18535
** foreign key.
18536
**
18537
** The OE_Default value is a place holder that means to use whatever
18140
-** conflict resolution algorthm is required from context.
18538
+** conflict resolution algorithm is required from context.
18539
**
18540
** The following symbolic values are used to record which type
18541
** of conflict resolution action to take.
@@ -18303,6 +18701,7 @@ struct Index {
18701
unsigned isCovering:1; /* True if this is a covering index */
18702
unsigned noSkipScan:1; /* Do not try to use skip-scan if true */
18703
unsigned hasStat1:1; /* aiRowLogEst values come from sqlite_stat1 */
18704
+ unsigned bLowQual:1; /* sqlite_stat1 says this is a low-quality index */
18705
unsigned bNoQuery:1; /* Do not use this index to optimize queries */
18706
unsigned bAscKeyBug:1; /* True if the bba7b69f9849b5bf bug applies */
18707
unsigned bHasVCol:1; /* Index references one or more VIRTUAL columns */
@@ -18413,6 +18812,10 @@ struct AggInfo {
18812
FuncDef *pFunc; /* The aggregate function implementation */
18813
int iDistinct; /* Ephemeral table used to enforce DISTINCT */
18814
int iDistAddr; /* Address of OP_OpenEphemeral */
18815
+ int iOBTab; /* Ephemeral table to implement ORDER BY */
18816
+ u8 bOBPayload; /* iOBTab has payload columns separate from key */
18817
+ u8 bOBUnique; /* Enforce uniqueness on iOBTab keys */
18818
+ u8 bUseSubtype; /* Transfer subtype info through sorter */
18819
} *aFunc;
18820
int nFunc; /* Number of entries in aFunc[] */
18821
u32 selId; /* Select to which this AggInfo belongs */
@@ -18551,7 +18954,7 @@ struct Expr {
18954
** TK_REGISTER: register number
18955
** TK_TRIGGER: 1 -> new, 0 -> old
18956
** EP_Unlikely: 134217728 times likelihood
18554
- ** TK_IN: ephemerial table holding RHS
18957
+ ** TK_IN: ephemeral table holding RHS
18958
** TK_SELECT_COLUMN: Number of columns on the LHS
18959
** TK_SELECT: 1st register of result vector */
18960
ynVar iColumn; /* TK_COLUMN: column index. -1 for rowid.
@@ -18597,7 +19000,7 @@ struct Expr {
19000
#define EP_Reduced 0x004000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
19001
#define EP_Win 0x008000 /* Contains window functions */
19002
#define EP_TokenOnly 0x010000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
18600
- /* 0x020000 // Available for reuse */
19003
+#define EP_FullSize 0x020000 /* Expr structure must remain full sized */
19004
#define EP_IfNullRow 0x040000 /* The TK_IF_NULL_ROW opcode */
19005
#define EP_Unlikely 0x080000 /* unlikely() or likelihood() function */
19006
#define EP_ConstFunc 0x100000 /* A SQLITE_FUNC_CONSTANT or _SLOCHNG function */
@@ -18627,12 +19030,15 @@ struct Expr {
19030
#define ExprClearProperty(E,P) (E)->flags&=~(P)
19031
#define ExprAlwaysTrue(E) (((E)->flags&(EP_OuterON|EP_IsTrue))==EP_IsTrue)
19032
#define ExprAlwaysFalse(E) (((E)->flags&(EP_OuterON|EP_IsFalse))==EP_IsFalse)
19033
+#define ExprIsFullSize(E) (((E)->flags&(EP_Reduced|EP_TokenOnly))==0)
19034
19035
/* Macros used to ensure that the correct members of unions are accessed
19036
** in Expr.
19037
*/
19038
#define ExprUseUToken(E) (((E)->flags&EP_IntValue)==0)
19039
#define ExprUseUValue(E) (((E)->flags&EP_IntValue)!=0)
19040
+#define ExprUseWOfst(E) (((E)->flags&(EP_InnerON|EP_OuterON))==0)
19041
+#define ExprUseWJoin(E) (((E)->flags&(EP_InnerON|EP_OuterON))!=0)
19042
#define ExprUseXList(E) (((E)->flags&EP_xIsSelect)==0)
19043
#define ExprUseXSelect(E) (((E)->flags&EP_xIsSelect)!=0)
19044
#define ExprUseYTab(E) (((E)->flags&(EP_WinFunc|EP_Subrtn))==0)
@@ -18742,6 +19148,7 @@ struct ExprList {
19148
#define ENAME_NAME 0 /* The AS clause of a result set */
19149
#define ENAME_SPAN 1 /* Complete text of the result set expression */
19150
#define ENAME_TAB 2 /* "DB.TABLE.NAME" for the result set */
19151
+#define ENAME_ROWID 3 /* "DB.TABLE._rowid_" for * expansion of rowid */
19152
19153
/*
19154
** An instance of this structure can hold a simple list of identifiers,
@@ -18821,7 +19228,7 @@ struct SrcItem {
19228
unsigned notCte :1; /* This item may not match a CTE */
19229
unsigned isUsing :1; /* u3.pUsing is valid */
19230
unsigned isOn :1; /* u3.pOn was once valid and non-NULL */
18824
- unsigned isSynthUsing :1; /* u3.pUsing is synthensized from NATURAL */
19231
+ unsigned isSynthUsing :1; /* u3.pUsing is synthesized from NATURAL */
19232
unsigned isNestedFrom :1; /* pSelect is a SF_NestedFrom subquery */
19233
} fg;
19234
int iCursor; /* The VDBE cursor number used to access this table */
@@ -18942,6 +19349,7 @@ struct NameContext {
19349
int nRef; /* Number of names resolved by this context */
19350
int nNcErr; /* Number of errors encountered while resolving names */
19351
int ncFlags; /* Zero or more NC_* flags defined below */
19352
+ u32 nNestedSelect; /* Number of nested selects using this NC */
19353
Select *pWinSelect; /* SELECT statement for any window functions */
19354
};
19355
@@ -18975,6 +19383,7 @@ struct NameContext {
19383
#define NC_InAggFunc 0x020000 /* True if analyzing arguments to an agg func */
19384
#define NC_FromDDL 0x040000 /* SQL text comes from sqlite_schema */
19385
#define NC_NoSelect 0x080000 /* Do not descend into sub-selects */
19386
+#define NC_Where 0x100000 /* Processing WHERE clause of a SELECT */
19387
#define NC_OrderAgg 0x8000000 /* Has an aggregate other than count/min/max */
19388
19389
/*
@@ -18998,6 +19407,7 @@ struct Upsert {
19407
Expr *pUpsertWhere; /* WHERE clause for the ON CONFLICT UPDATE */
19408
Upsert *pNextUpsert; /* Next ON CONFLICT clause in the list */
19409
u8 isDoUpdate; /* True for DO UPDATE. False for DO NOTHING */
19410
+ u8 isDup; /* True if 2nd or later with same pUpsertIdx */
19411
/* Above this point is the parse tree for the ON CONFLICT clauses.
19412
** The next group of fields stores intermediate data. */
19413
void *pToFree; /* Free memory when deleting the Upsert object */
@@ -19350,6 +19760,7 @@ struct Parse {
19760
int *aLabel; /* Space to hold the labels */
19761
ExprList *pConstExpr;/* Constant expressions */
19762
IndexedExpr *pIdxEpr;/* List of expressions used by active indexes */
19763
+ IndexedExpr *pIdxPartExpr; /* Exprs constrained by index WHERE clauses */
19764
Token constraintName;/* Name of the constraint currently being parsed */
19765
yDbMask writeMask; /* Start a write transaction on these databases */
19766
yDbMask cookieMask; /* Bitmask of schema verified databases */
@@ -19357,6 +19768,9 @@ struct Parse {
19768
int regRoot; /* Register holding root page number for new objects */
19769
int nMaxArg; /* Max args passed to user function by sub-program */
19770
int nSelect; /* Number of SELECT stmts. Counter for Select.selId */
19771
+#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19772
+ u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
19773
+#endif
19774
#ifndef SQLITE_OMIT_SHARED_CACHE
19775
int nTableLock; /* Number of locks in aTableLock */
19776
TableLock *aTableLock; /* Required table locks for shared-cache mode */
@@ -19370,12 +19784,9 @@ struct Parse {
19784
int addrCrTab; /* Address of OP_CreateBtree on CREATE TABLE */
19785
Returning *pReturning; /* The RETURNING clause */
19786
} u1;
19373
- u32 nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
19787
u32 oldmask; /* Mask of old.* columns referenced */
19788
u32 newmask; /* Mask of new.* columns referenced */
19376
-#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19377
- u32 nProgressSteps; /* xProgress steps taken during sqlite3_prepare() */
19378
-#endif
19789
+ LogEst nQueryLoop; /* Est number of iterations of a query (10*log2(N)) */
19790
u8 eTriggerOp; /* TK_UPDATE, TK_INSERT or TK_DELETE */
19791
u8 bReturning; /* Coding a RETURNING trigger */
19792
u8 eOrconf; /* Default ON CONFLICT policy for trigger steps */
@@ -19499,6 +19910,7 @@ struct AuthContext {
19910
#define OPFLAG_ISNOOP 0x40 /* OP_Delete does pre-update-hook only */
19911
#define OPFLAG_LENGTHARG 0x40 /* OP_Column only used for length() */
19912
#define OPFLAG_TYPEOFARG 0x80 /* OP_Column only used for typeof() */
19913
+#define OPFLAG_BYTELENARG 0xc0 /* OP_Column only for octet_length() */
19914
#define OPFLAG_BULKCSR 0x01 /* OP_Open** used to open bulk cursor */
19915
#define OPFLAG_SEEKEQ 0x02 /* OP_Open** cursor uses EQ seek only */
19916
#define OPFLAG_FORDELETE 0x08 /* OP_Open should use BTREE_FORDELETE */
@@ -19620,6 +20032,7 @@ struct Returning {
20032
int iRetCur; /* Transient table holding RETURNING results */
20033
int nRetCol; /* Number of in pReturnEL after expansion */
20034
int iRetReg; /* Register array for holding a row of RETURNING */
20035
+ char zName[40]; /* Name of trigger: "sqlite_returning_%p" */
20036
};
20037
20038
/*
@@ -19641,6 +20054,28 @@ struct sqlite3_str {
20054
20055
#define isMalloced(X) (((X)->printfFlags & SQLITE_PRINTF_MALLOCED)!=0)
20056
20057
+/*
20058
+** The following object is the header for an "RCStr" or "reference-counted
20059
+** string". An RCStr is passed around and used like any other char*
20060
+** that has been dynamically allocated. The important interface
20061
+** differences:
20062
+**
20063
+** 1. RCStr strings are reference counted. They are deallocated
20064
+** when the reference count reaches zero.
20065
+**
20066
+** 2. Use sqlite3RCStrUnref() to free an RCStr string rather than
20067
+** sqlite3_free()
20068
+**
20069
+** 3. Make a (read-only) copy of a read-only RCStr string using
20070
+** sqlite3RCStrRef().
20071
+**
20072
+** "String" is in the name, but an RCStr object can also be used to hold
20073
+** binary data.
20074
+*/
20075
+struct RCStr {
20076
+ u64 nRCRef; /* Number of references */
20077
+ /* Total structure size should be a multiple of 8 bytes for alignment */
20078
+};
20079
20080
/*
20081
** A pointer to this structure is used to communicate information
@@ -19667,7 +20102,7 @@ typedef struct {
20102
/* Tuning parameters are set using SQLITE_TESTCTRL_TUNE and are controlled
20103
** on debug-builds of the CLI using ".testctrl tune ID VALUE". Tuning
20104
** parameters are for temporary use during development, to help find
19670
-** optimial values for parameters in the query planner. The should not
20105
+** optimal values for parameters in the query planner. The should not
20106
** be used on trunk check-ins. They are a temporary mechanism available
20107
** for transient development builds only.
20108
**
@@ -19693,6 +20128,10 @@ struct Sqlite3Config {
20128
u8 bUseCis; /* Use covering indices for full-scans */
20129
u8 bSmallMalloc; /* Avoid large memory allocations if true */
20130
u8 bExtraSchemaChecks; /* Verify type,name,tbl_name in schema */
20131
+ u8 bUseLongDouble; /* Make use of long double */
20132
+#ifdef SQLITE_DEBUG
20133
+ u8 bJsonSelfcheck; /* Double-check JSON parsing */
20134
+#endif
20135
int mxStrlen; /* Maximum string length */
20136
int neverCorrupt; /* Database is always well-formed */
20137
int szLookaside; /* Default lookaside buffer size */
@@ -19739,6 +20178,11 @@ struct Sqlite3Config {
20178
#endif
20179
#ifndef SQLITE_UNTESTABLE
20180
int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
20181
+#endif
20182
+#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
20183
+ u32 mNoVisibleRowid; /* TF_NoVisibleRowid if the ROWID_IN_VIEW
20184
+ ** feature is disabled. 0 if rowids can
20185
+ ** occur in views. */
20186
#endif
20187
int bLocaltimeFault; /* True to fail localtime() calls */
20188
int (*xAltLocaltime)(const void*,void*); /* Alternative localtime() routine */
@@ -19779,6 +20223,7 @@ struct Walker {
20223
void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
20224
int walkerDepth; /* Number of subqueries */
20225
u16 eCode; /* A small processing code */
20226
+ u16 mWFlags; /* Use-dependent flags */
20227
union { /* Extra data for callback */
20228
NameContext *pNC; /* Naming context */
20229
int n; /* A counter */
@@ -19818,6 +20263,7 @@ struct DbFixer {
20263
20264
/* Forward declarations */
20265
SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
20266
+SQLITE_PRIVATE int sqlite3WalkExprNN(Walker*, Expr*);
20267
SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
20268
SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
20269
SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
@@ -19898,6 +20344,16 @@ struct CteUse {
20344
};
20345
20346
20347
+/* Client data associated with sqlite3_set_clientdata() and
20348
+** sqlite3_get_clientdata().
20349
+*/
20350
+struct DbClientData {
20351
+ DbClientData *pNext; /* Next in a linked list */
20352
+ void *pData; /* The data */
20353
+ void (*xDestructor)(void*); /* Destructor. Might be NULL */
20354
+ char zName[1]; /* Name of this client data. MUST BE LAST */
20355
+};
20356
+
20357
#ifdef SQLITE_DEBUG
20358
/*
20359
** An instance of the TreeView object is used for printing the content of
@@ -20183,10 +20639,13 @@ SQLITE_PRIVATE void sqlite3MutexWarnOnContention(sqlite3_mutex*);
20639
# define EXP754 (((u64)0x7ff)<<52)
20640
# define MAN754 ((((u64)1)<<52)-1)
20641
# define IsNaN(X) (((X)&EXP754)==EXP754 && ((X)&MAN754)!=0)
20642
+# define IsOvfl(X) (((X)&EXP754)==EXP754)
20643
SQLITE_PRIVATE int sqlite3IsNaN(double);
20644
+SQLITE_PRIVATE int sqlite3IsOverflow(double);
20645
#else
20188
-# define IsNaN(X) 0
20189
-# define sqlite3IsNaN(X) 0
20646
+# define IsNaN(X) 0
20647
+# define sqlite3IsNaN(X) 0
20648
+# define sqlite3IsOVerflow(X) 0
20649
#endif
20650
20651
/*
@@ -20199,6 +20658,20 @@ struct PrintfArguments {
20658
sqlite3_value **apArg; /* The argument values */
20659
};
20660
20661
+/*
20662
+** An instance of this object receives the decoding of a floating point
20663
+** value into an approximate decimal representation.
20664
+*/
20665
+struct FpDecode {
20666
+ char sign; /* '+' or '-' */
20667
+ char isSpecial; /* 1: Infinity 2: NaN */
20668
+ int n; /* Significant digits in the decode */
20669
+ int iDP; /* Location of the decimal point */
20670
+ char *z; /* Start of significant digits */
20671
+ char zBuf[24]; /* Storage for significant digits */
20672
+};
20673
+
20674
+SQLITE_PRIVATE void sqlite3FpDecode(FpDecode*,double,int,int);
20675
SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
20676
SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
20677
#if defined(SQLITE_DEBUG) || defined(SQLITE_HAVE_OS_TRACE)
@@ -20288,9 +20761,12 @@ SQLITE_PRIVATE void sqlite3PExprAddSelect(Parse*, Expr*, Select*);
20761
SQLITE_PRIVATE Expr *sqlite3ExprAnd(Parse*,Expr*, Expr*);
20762
SQLITE_PRIVATE Expr *sqlite3ExprSimplifiedAndOr(Expr*);
20763
SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, const Token*, int);
20764
+SQLITE_PRIVATE void sqlite3ExprAddFunctionOrderBy(Parse*,Expr*,ExprList*);
20765
+SQLITE_PRIVATE void sqlite3ExprOrderByAggregateError(Parse*,Expr*);
20766
SQLITE_PRIVATE void sqlite3ExprFunctionUsable(Parse*,const Expr*,const FuncDef*);
20767
SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*, u32);
20768
SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
20769
+SQLITE_PRIVATE void sqlite3ExprDeleteGeneric(sqlite3*,void*);
20770
SQLITE_PRIVATE void sqlite3ExprDeferredDelete(Parse*, Expr*);
20771
SQLITE_PRIVATE void sqlite3ExprUnmapAndDelete(Parse*, Expr*);
20772
SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
@@ -20300,6 +20776,7 @@ SQLITE_PRIVATE void sqlite3ExprListSetSortOrder(ExprList*,int,int);
20776
SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,const Token*,int);
20777
SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,const char*,const char*);
20778
SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
20779
+SQLITE_PRIVATE void sqlite3ExprListDeleteGeneric(sqlite3*,void*);
20780
SQLITE_PRIVATE u32 sqlite3ExprListFlags(const ExprList*);
20781
SQLITE_PRIVATE int sqlite3IndexHasDuplicateRootPage(Index*);
20782
SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
@@ -20390,6 +20867,7 @@ SQLITE_PRIVATE int sqlite3DbMaskAllZero(yDbMask);
20867
SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
20868
SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
20869
SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
20870
+SQLITE_PRIVATE void sqlite3DeleteTableGeneric(sqlite3*, void*);
20871
SQLITE_PRIVATE void sqlite3FreeIndex(sqlite3*, Index*);
20872
#ifndef SQLITE_OMIT_AUTOINCREMENT
20873
SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse);
@@ -20426,6 +20904,7 @@ SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
20904
SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
20905
Expr*,ExprList*,u32,Expr*);
20906
SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
20907
+SQLITE_PRIVATE void sqlite3SelectDeleteGeneric(sqlite3*,void*);
20908
SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
20909
SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, Trigger*);
20910
SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
@@ -20489,7 +20968,7 @@ SQLITE_PRIVATE int sqlite3ExprCompare(const Parse*,const Expr*,const Expr*, int)
20968
SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr*,Expr*,int);
20969
SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList*,const ExprList*, int);
20970
SQLITE_PRIVATE int sqlite3ExprImpliesExpr(const Parse*,const Expr*,const Expr*, int);
20492
-SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int);
20971
+SQLITE_PRIVATE int sqlite3ExprImpliesNonNullRow(Expr*,int,int);
20972
SQLITE_PRIVATE void sqlite3AggInfoPersistWalkerInit(Walker*,Parse*);
20973
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
20974
SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
@@ -20524,6 +21003,7 @@ SQLITE_PRIVATE int sqlite3ExprIsInteger(const Expr*, int*);
21003
SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
21004
SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
21005
SQLITE_PRIVATE int sqlite3IsRowid(const char*);
21006
+SQLITE_PRIVATE const char *sqlite3RowidAlias(Table *pTab);
21007
SQLITE_PRIVATE void sqlite3GenerateRowDelete(
21008
Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8,int);
21009
SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*, int);
@@ -20638,6 +21118,7 @@ SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
21118
SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
21119
SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
21120
SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
21121
+
21122
SQLITE_PRIVATE int sqlite3RealSameAsInt(double,sqlite3_int64);
21123
SQLITE_PRIVATE i64 sqlite3RealToI64(double);
21124
SQLITE_PRIVATE int sqlite3Int64ToText(i64,char*);
@@ -20650,6 +21131,7 @@ SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
21131
#endif
21132
SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
21133
SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
21134
+SQLITE_PRIVATE int sqlite3Utf8ReadLimited(const u8*, int, u32*);
21135
SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
21136
SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
21137
SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
@@ -20742,6 +21224,7 @@ SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
21224
SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,u8);
21225
21226
SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
21227
+SQLITE_PRIVATE int sqlite3ValueIsOfClass(const sqlite3_value*, void(*)(void*));
21228
SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
21229
SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
21230
void(*)(void*));
@@ -20793,7 +21276,8 @@ SQLITE_PRIVATE int sqlite3MatchEName(
21276
const struct ExprList_item*,
21277
const char*,
21278
const char*,
20796
- const char*
21279
+ const char*,
21280
+ int*
21281
);
21282
SQLITE_PRIVATE Bitmask sqlite3ExprColUsed(Expr*);
21283
SQLITE_PRIVATE u8 sqlite3StrIHash(const char*);
@@ -20849,6 +21333,11 @@ SQLITE_PRIVATE void sqlite3OomClear(sqlite3*);
21333
SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
21334
SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
21335
21336
+SQLITE_PRIVATE char *sqlite3RCStrRef(char*);
21337
+SQLITE_PRIVATE void sqlite3RCStrUnref(void*);
21338
+SQLITE_PRIVATE char *sqlite3RCStrNew(u64);
21339
+SQLITE_PRIVATE char *sqlite3RCStrResize(char*,u64);
21340
+
21341
SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, sqlite3*, char*, int, int);
21342
SQLITE_PRIVATE int sqlite3StrAccumEnlarge(StrAccum*, i64);
21343
SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
@@ -20989,6 +21478,7 @@ SQLITE_PRIVATE Cte *sqlite3CteNew(Parse*,Token*,ExprList*,Select*,u8);
21478
SQLITE_PRIVATE void sqlite3CteDelete(sqlite3*,Cte*);
21479
SQLITE_PRIVATE With *sqlite3WithAdd(Parse*,With*,Cte*);
21480
SQLITE_PRIVATE void sqlite3WithDelete(sqlite3*,With*);
21481
+SQLITE_PRIVATE void sqlite3WithDeleteGeneric(sqlite3*,void*);
21482
SQLITE_PRIVATE With *sqlite3WithPush(Parse*, With*, u8);
21483
#else
21484
# define sqlite3CteNew(P,T,E,S) ((void*)0)
@@ -21001,7 +21491,7 @@ SQLITE_PRIVATE With *sqlite3WithPush(Parse*, With*, u8);
21491
SQLITE_PRIVATE Upsert *sqlite3UpsertNew(sqlite3*,ExprList*,Expr*,ExprList*,Expr*,Upsert*);
21492
SQLITE_PRIVATE void sqlite3UpsertDelete(sqlite3*,Upsert*);
21493
SQLITE_PRIVATE Upsert *sqlite3UpsertDup(sqlite3*,Upsert*);
21004
-SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*);
21494
+SQLITE_PRIVATE int sqlite3UpsertAnalyzeTarget(Parse*,SrcList*,Upsert*,Upsert*);
21495
SQLITE_PRIVATE void sqlite3UpsertDoUpdate(Parse*,Upsert*,Table*,Index*,int);
21496
SQLITE_PRIVATE Upsert *sqlite3UpsertOfIndex(Upsert*,Index*);
21497
SQLITE_PRIVATE int sqlite3UpsertNextIsIPK(Upsert*);
@@ -21100,6 +21590,7 @@ SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse*, int);
21590
#define sqlite3SelectExprHeight(x) 0
21591
#define sqlite3ExprCheckHeight(x,y)
21592
#endif
21593
+SQLITE_PRIVATE void sqlite3ExprSetErrorOffset(Expr*,int);
21594
21595
SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
21596
SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
@@ -21385,14 +21876,14 @@ static const char * const sqlite3azCompileOpt[] = {
21876
#ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
21877
"4_BYTE_ALIGNED_MALLOC",
21878
#endif
21388
-#ifdef SQLITE_64BIT_STATS
21389
- "64BIT_STATS",
21390
-#endif
21879
#ifdef SQLITE_ALLOW_COVERING_INDEX_SCAN
21880
# if SQLITE_ALLOW_COVERING_INDEX_SCAN != 1
21881
"ALLOW_COVERING_INDEX_SCAN=" CTIMEOPT_VAL(SQLITE_ALLOW_COVERING_INDEX_SCAN),
21882
# endif
21883
#endif
21884
+#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
21885
+ "ALLOW_ROWID_IN_VIEW",
21886
+#endif
21887
#ifdef SQLITE_ALLOW_URI_AUTHORITY
21888
"ALLOW_URI_AUTHORITY",
21889
#endif
@@ -21683,6 +22174,9 @@ static const char * const sqlite3azCompileOpt[] = {
22174
#ifdef SQLITE_EXPLAIN_ESTIMATED_ROWS
22175
"EXPLAIN_ESTIMATED_ROWS",
22176
#endif
22177
+#ifdef SQLITE_EXTRA_AUTOEXT
22178
+ "EXTRA_AUTOEXT=" CTIMEOPT_VAL(SQLITE_EXTRA_AUTOEXT),
22179
+#endif
22180
#ifdef SQLITE_EXTRA_IFNULLROW
22181
"EXTRA_IFNULLROW",
22182
#endif
@@ -21724,6 +22218,9 @@ static const char * const sqlite3azCompileOpt[] = {
22218
#ifdef SQLITE_INTEGRITY_CHECK_ERROR_MAX
22219
"INTEGRITY_CHECK_ERROR_MAX=" CTIMEOPT_VAL(SQLITE_INTEGRITY_CHECK_ERROR_MAX),
22220
#endif
22221
+#ifdef SQLITE_LEGACY_JSON_VALID
22222
+ "LEGACY_JSON_VALID",
22223
+#endif
22224
#ifdef SQLITE_LIKE_DOESNT_MATCH_BLOBS
22225
"LIKE_DOESNT_MATCH_BLOBS",
22226
#endif
@@ -21961,6 +22458,9 @@ static const char * const sqlite3azCompileOpt[] = {
22458
#ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
22459
"OMIT_SCHEMA_VERSION_PRAGMAS",
22460
#endif
22461
+#ifdef SQLITE_OMIT_SEH
22462
+ "OMIT_SEH",
22463
+#endif
22464
#ifdef SQLITE_OMIT_SHARED_CACHE
22465
"OMIT_SHARED_CACHE",
22466
#endif
@@ -22358,6 +22858,10 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
22858
SQLITE_ALLOW_COVERING_INDEX_SCAN, /* bUseCis */
22859
0, /* bSmallMalloc */
22860
1, /* bExtraSchemaChecks */
22861
+ sizeof(LONGDOUBLE_TYPE)>8, /* bUseLongDouble */
22862
+#ifdef SQLITE_DEBUG
22863
+ 0, /* bJsonSelfcheck */
22864
+#endif
22865
0x7ffffffe, /* mxStrlen */
22866
0, /* neverCorrupt */
22867
SQLITE_DEFAULT_LOOKASIDE, /* szLookaside, nLookaside */
@@ -22399,6 +22903,9 @@ SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
22903
#endif
22904
#ifndef SQLITE_UNTESTABLE
22905
0, /* xTestCallback */
22906
+#endif
22907
+#ifdef SQLITE_ALLOW_ROWID_IN_VIEW
22908
+ 0, /* mNoVisibleRowid. 0 == allow rowid-in-view */
22909
#endif
22910
0, /* bLocaltimeFault */
22911
0, /* xAltLocaltime */
@@ -22587,6 +23094,9 @@ typedef struct VdbeSorter VdbeSorter;
23094
/* Elements of the linked list at Vdbe.pAuxData */
23095
typedef struct AuxData AuxData;
23096
23097
+/* A cache of large TEXT or BLOB values in a VdbeCursor */
23098
+typedef struct VdbeTxtBlbCache VdbeTxtBlbCache;
23099
+
23100
/* Types of VDBE cursors */
23101
#define CURTYPE_BTREE 0
23102
#define CURTYPE_SORTER 1
@@ -22618,6 +23128,7 @@ struct VdbeCursor {
23128
Bool useRandomRowid:1; /* Generate new record numbers semi-randomly */
23129
Bool isOrdered:1; /* True if the table is not BTREE_UNORDERED */
23130
Bool noReuse:1; /* OpenEphemeral may not reuse this cursor */
23131
+ Bool colCache:1; /* pCache pointer is initialized and non-NULL */
23132
u16 seekHit; /* See the OP_SeekHit and OP_IfNoHope opcodes */
23133
union { /* pBtx for isEphermeral. pAltMap otherwise */
23134
Btree *pBtx; /* Separate file holding temporary table */
@@ -22658,6 +23169,7 @@ struct VdbeCursor {
23169
#ifdef SQLITE_ENABLE_COLUMN_USED_MASK
23170
u64 maskUsed; /* Mask of columns used by this cursor */
23171
#endif
23172
+ VdbeTxtBlbCache *pCache; /* Cache of large TEXT or BLOB values */
23173
23174
/* 2*nField extra array elements allocated for aType[], beyond the one
23175
** static element declared in the structure. nField total array slots for
@@ -22670,12 +23182,25 @@ struct VdbeCursor {
23182
#define IsNullCursor(P) \
23183
((P)->eCurType==CURTYPE_PSEUDO && (P)->nullRow && (P)->seekResult==0)
23184
22673
-
23185
/*
23186
** A value for VdbeCursor.cacheStatus that means the cache is always invalid.
23187
*/
23188
#define CACHE_STALE 0
23189
23190
+/*
23191
+** Large TEXT or BLOB values can be slow to load, so we want to avoid
23192
+** loading them more than once. For that reason, large TEXT and BLOB values
23193
+** can be stored in a cache defined by this object, and attached to the
23194
+** VdbeCursor using the pCache field.
23195
+*/
23196
+struct VdbeTxtBlbCache {
23197
+ char *pCValue; /* A RCStr buffer to hold the value */
23198
+ i64 iOffset; /* File offset of the row being cached */
23199
+ int iCol; /* Column for which the cache is valid */
23200
+ u32 cacheStatus; /* Vdbe.cacheCtr value */
23201
+ u32 colCacheCtr; /* Column cache counter */
23202
+};
23203
+
23204
/*
23205
** When a sub-program is executed (OP_Program), a structure of this type
23206
** is allocated to store the current value of the program counter, as
@@ -22996,16 +23521,18 @@ struct Vdbe {
23521
u32 nWrite; /* Number of write operations that have occurred */
23522
#endif
23523
u16 nResColumn; /* Number of columns in one row of the result set */
23524
+ u16 nResAlloc; /* Column slots allocated to aColName[] */
23525
u8 errorAction; /* Recovery action to do in case of an error */
23526
u8 minWriteFileFormat; /* Minimum file format for writable database files */
23527
u8 prepFlags; /* SQLITE_PREPARE_* flags */
23528
u8 eVdbeState; /* On of the VDBE_*_STATE values */
23529
bft expired:2; /* 1: recompile VM immediately 2: when convenient */
23004
- bft explain:2; /* True if EXPLAIN present on SQL command */
23530
+ bft explain:2; /* 0: normal, 1: EXPLAIN, 2: EXPLAIN QUERY PLAN */
23531
bft changeCntOn:1; /* True to update the change-counter */
23532
bft usesStmtJournal:1; /* True if uses a statement journal */
23533
bft readOnly:1; /* True for statements that do not write */
23534
bft bIsReader:1; /* True for statements that read */
23535
+ bft haveEqpOps:1; /* Bytecode supports EXPLAIN QUERY PLAN */
23536
yDbMask btreeMask; /* Bitmask of db->aDb[] entries referenced */
23537
yDbMask lockMask; /* Subset of btreeMask that requires a lock */
23538
u32 aCounter[9]; /* Counters used by sqlite3_stmt_status() */
@@ -23052,7 +23579,7 @@ struct PreUpdate {
23579
i64 iKey1; /* First key value passed to hook */
23580
i64 iKey2; /* Second key value passed to hook */
23581
Mem *aNew; /* Array of new.* values */
23055
- Table *pTab; /* Schema object being upated */
23582
+ Table *pTab; /* Schema object being updated */
23583
Index *pPk; /* PK index if pTab is WITHOUT ROWID */
23584
};
23585
@@ -23142,6 +23669,7 @@ SQLITE_PRIVATE int sqlite3VdbeMemSetZeroBlob(Mem*,int);
23669
SQLITE_PRIVATE int sqlite3VdbeMemIsRowSet(const Mem*);
23670
#endif
23671
SQLITE_PRIVATE int sqlite3VdbeMemSetRowSet(Mem*);
23672
+SQLITE_PRIVATE void sqlite3VdbeMemZeroTerminateIfAble(Mem*);
23673
SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
23674
SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, u8, u8);
23675
SQLITE_PRIVATE int sqlite3IntFloatCompare(i64,double);
@@ -23589,7 +24117,7 @@ SQLITE_API int sqlite3_db_status(
24117
case SQLITE_DBSTATUS_CACHE_MISS:
24118
case SQLITE_DBSTATUS_CACHE_WRITE:{
24119
int i;
23592
- int nRet = 0;
24120
+ u64 nRet = 0;
24121
assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
24122
assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
24123
@@ -23602,7 +24130,7 @@ SQLITE_API int sqlite3_db_status(
24130
*pHighwater = 0; /* IMP: R-42420-56072 */
24131
/* IMP: R-54100-20147 */
24132
/* IMP: R-29431-39229 */
23605
- *pCurrent = nRet;
24133
+ *pCurrent = (int)nRet & 0x7fffffff;
24134
break;
24135
}
24136
@@ -23738,8 +24266,8 @@ struct DateTime {
24266
*/
24267
static int getDigits(const char *zDate, const char *zFormat, ...){
24268
/* The aMx[] array translates the 3rd character of each format
23741
- ** spec into a max size: a b c d e f */
23742
- static const u16 aMx[] = { 12, 14, 24, 31, 59, 9999 };
24269
+ ** spec into a max size: a b c d e f */
24270
+ static const u16 aMx[] = { 12, 14, 24, 31, 59, 14712 };
24271
va_list ap;
24272
int cnt = 0;
24273
char nextC;
@@ -24080,17 +24608,14 @@ static void computeYMD(DateTime *p){
24608
** Compute the Hour, Minute, and Seconds from the julian day number.
24609
*/
24610
static void computeHMS(DateTime *p){
24083
- int s;
24611
+ int day_ms, day_min; /* milliseconds, minutes into the day */
24612
if( p->validHMS ) return;
24613
computeJD(p);
24086
- s = (int)((p->iJD + 43200000) % 86400000);
24087
- p->s = s/1000.0;
24088
- s = (int)p->s;
24089
- p->s -= s;
24090
- p->h = s/3600;
24091
- s -= p->h*3600;
24092
- p->m = s/60;
24093
- p->s += s - p->m*60;
24614
+ day_ms = (int)((p->iJD + 43200000) % 86400000);
24615
+ p->s = (day_ms % 60000)/1000.0;
24616
+ day_min = day_ms/60000;
24617
+ p->m = day_min % 60;
24618
+ p->h = day_min / 60;
24619
p->rawS = 0;
24620
p->validHMS = 1;
24621
}
@@ -24269,6 +24794,25 @@ static const struct {
24794
{ 4, "year", 14713.0, 31536000.0 },
24795
};
24796
24797
+/*
24798
+** If the DateTime p is raw number, try to figure out if it is
24799
+** a julian day number of a unix timestamp. Set the p value
24800
+** appropriately.
24801
+*/
24802
+static void autoAdjustDate(DateTime *p){
24803
+ if( !p->rawS || p->validJD ){
24804
+ p->rawS = 0;
24805
+ }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
24806
+ && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
24807
+ ){
24808
+ double r = p->s*1000.0 + 210866760000000.0;
24809
+ clearYMD_HMS_TZ(p);
24810
+ p->iJD = (sqlite3_int64)(r + 0.5);
24811
+ p->validJD = 1;
24812
+ p->rawS = 0;
24813
+ }
24814
+}
24815
+
24816
/*
24817
** Process a modifier to a date-time stamp. The modifiers are
24818
** as follows:
@@ -24312,19 +24856,8 @@ static int parseModifier(
24856
*/
24857
if( sqlite3_stricmp(z, "auto")==0 ){
24858
if( idx>1 ) return 1; /* IMP: R-33611-57934 */
24315
- if( !p->rawS || p->validJD ){
24316
- rc = 0;
24317
- p->rawS = 0;
24318
- }else if( p->s>=-21086676*(i64)10000 /* -4713-11-24 12:00:00 */
24319
- && p->s<=(25340230*(i64)10000)+799 /* 9999-12-31 23:59:59 */
24320
- ){
24321
- r = p->s*1000.0 + 210866760000000.0;
24322
- clearYMD_HMS_TZ(p);
24323
- p->iJD = (sqlite3_int64)(r + 0.5);
24324
- p->validJD = 1;
24325
- p->rawS = 0;
24326
- rc = 0;
24327
- }
24859
+ autoAdjustDate(p);
24860
+ rc = 0;
24861
}
24862
break;
24863
}
@@ -24490,18 +25023,73 @@ static int parseModifier(
25023
case '9': {
25024
double rRounder;
25025
int i;
24493
- for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
25026
+ int Y,M,D,h,m,x;
25027
+ const char *z2 = z;
25028
+ char z0 = z[0];
25029
+ for(n=1; z[n]; n++){
25030
+ if( z[n]==':' ) break;
25031
+ if( sqlite3Isspace(z[n]) ) break;
25032
+ if( z[n]=='-' ){
25033
+ if( n==5 && getDigits(&z[1], "40f", &Y)==1 ) break;
25034
+ if( n==6 && getDigits(&z[1], "50f", &Y)==1 ) break;
25035
+ }
25036
+ }
25037
if( sqlite3AtoF(z, &r, n, SQLITE_UTF8)<=0 ){
24495
- rc = 1;
25038
+ assert( rc==1 );
25039
break;
25040
}
24498
- if( z[n]==':' ){
25041
+ if( z[n]=='-' ){
25042
+ /* A modifier of the form (+|-)YYYY-MM-DD adds or subtracts the
25043
+ ** specified number of years, months, and days. MM is limited to
25044
+ ** the range 0-11 and DD is limited to 0-30.
25045
+ */
25046
+ if( z0!='+' && z0!='-' ) break; /* Must start with +/- */
25047
+ if( n==5 ){
25048
+ if( getDigits(&z[1], "40f-20a-20d", &Y, &M, &D)!=3 ) break;
25049
+ }else{
25050
+ assert( n==6 );
25051
+ if( getDigits(&z[1], "50f-20a-20d", &Y, &M, &D)!=3 ) break;
25052
+ z++;
25053
+ }
25054
+ if( M>=12 ) break; /* M range 0..11 */
25055
+ if( D>=31 ) break; /* D range 0..30 */
25056
+ computeYMD_HMS(p);
25057
+ p->validJD = 0;
25058
+ if( z0=='-' ){
25059
+ p->Y -= Y;
25060
+ p->M -= M;
25061
+ D = -D;
25062
+ }else{
25063
+ p->Y += Y;
25064
+ p->M += M;
25065
+ }
25066
+ x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
25067
+ p->Y += x;
25068
+ p->M -= x*12;
25069
+ computeJD(p);
25070
+ p->validHMS = 0;
25071
+ p->validYMD = 0;
25072
+ p->iJD += (i64)D*86400000;
25073
+ if( z[11]==0 ){
25074
+ rc = 0;
25075
+ break;
25076
+ }
25077
+ if( sqlite3Isspace(z[11])
25078
+ && getDigits(&z[12], "20c:20e", &h, &m)==2
25079
+ ){
25080
+ z2 = &z[12];
25081
+ n = 2;
25082
+ }else{
25083
+ break;
25084
+ }
25085
+ }
25086
+ if( z2[n]==':' ){
25087
/* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
25088
** specified number of hours, minutes, seconds, and fractional seconds
25089
** to the time. The ".FFF" may be omitted. The ":SS.FFF" may be
25090
** omitted.
25091
*/
24504
- const char *z2 = z;
25092
+
25093
DateTime tx;
25094
sqlite3_int64 day;
25095
if( !sqlite3Isdigit(*z2) ) z2++;
@@ -24511,7 +25099,7 @@ static int parseModifier(
25099
tx.iJD -= 43200000;
25100
day = tx.iJD/86400000;
25101
tx.iJD -= day*86400000;
24514
- if( z[0]=='-' ) tx.iJD = -tx.iJD;
25102
+ if( z0=='-' ) tx.iJD = -tx.iJD;
25103
computeJD(p);
25104
clearYMD_HMS_TZ(p);
25105
p->iJD += tx.iJD;
@@ -24527,7 +25115,7 @@ static int parseModifier(
25115
if( n>10 || n<3 ) break;
25116
if( sqlite3UpperToLower[(u8)z[n-1]]=='s' ) n--;
25117
computeJD(p);
24530
- rc = 1;
25118
+ assert( rc==1 );
25119
rRounder = r<0 ? -0.5 : +0.5;
25120
for(i=0; i<ArraySize(aXformType); i++){
25121
if( aXformType[i].nName==n
@@ -24536,7 +25124,6 @@ static int parseModifier(
25124
){
25125
switch( i ){
25126
case 4: { /* Special processing to add months */
24539
- int x;
25127
assert( strcmp(aXformType[i].zName,"month")==0 );
25128
computeYMD_HMS(p);
25129
p->M += (int)r;
@@ -24612,6 +25199,12 @@ static int isDate(
25199
}
25200
computeJD(p);
25201
if( p->isError || !validJulianDay(p->iJD) ) return 1;
25202
+ if( argc==1 && p->validYMD && p->D>28 ){
25203
+ /* Make sure a YYYY-MM-DD is normalized.
25204
+ ** Example: 2023-02-31 -> 2023-03-03 */
25205
+ assert( p->validJD );
25206
+ p->validYMD = 0;
25207
+ }
25208
return 0;
25209
}
25210
@@ -24695,7 +25288,7 @@ static void datetimeFunc(
25288
zBuf[16] = '0' + (x.m)%10;
25289
zBuf[17] = ':';
25290
if( x.useSubsec ){
24698
- s = (int)1000.0*x.s;
25291
+ s = (int)(1000.0*x.s + 0.5);
25292
zBuf[18] = '0' + (s/10000)%10;
25293
zBuf[19] = '0' + (s/1000)%10;
25294
zBuf[20] = '.';
@@ -24742,7 +25335,7 @@ static void timeFunc(
25335
zBuf[4] = '0' + (x.m)%10;
25336
zBuf[5] = ':';
25337
if( x.useSubsec ){
24745
- s = (int)1000.0*x.s;
25338
+ s = (int)(1000.0*x.s + 0.5);
25339
zBuf[6] = '0' + (s/10000)%10;
25340
zBuf[7] = '0' + (s/1000)%10;
25341
zBuf[8] = '.';
@@ -24813,7 +25406,7 @@ static void dateFunc(
25406
** %M minute 00-59
25407
** %s seconds since 1970-01-01
25408
** %S seconds 00-59
24816
-** %w day of week 0-6 sunday==0
25409
+** %w day of week 0-6 Sunday==0
25410
** %W week of year 00-53
25411
** %Y year 0000-9999
25412
** %% %
@@ -24839,13 +25432,16 @@ static void strftimeFunc(
25432
computeJD(&x);
25433
computeYMD_HMS(&x);
25434
for(i=j=0; zFmt[i]; i++){
25435
+ char cf;
25436
if( zFmt[i]!='%' ) continue;
25437
if( j<i ) sqlite3_str_append(&sRes, zFmt+j, (int)(i-j));
25438
i++;
25439
j = i + 1;
24846
- switch( zFmt[i] ){
24847
- case 'd': {
24848
- sqlite3_str_appendf(&sRes, "%02d", x.D);
25440
+ cf = zFmt[i];
25441
+ switch( cf ){
25442
+ case 'd': /* Fall thru */
25443
+ case 'e': {
25444
+ sqlite3_str_appendf(&sRes, cf=='d' ? "%02d" : "%2d", x.D);
25445
break;
25446
}
25447
case 'f': {
@@ -24854,8 +25450,21 @@ static void strftimeFunc(
25450
sqlite3_str_appendf(&sRes, "%06.3f", s);
25451
break;
25452
}
24857
- case 'H': {
24858
- sqlite3_str_appendf(&sRes, "%02d", x.h);
25453
+ case 'F': {
25454
+ sqlite3_str_appendf(&sRes, "%04d-%02d-%02d", x.Y, x.M, x.D);
25455
+ break;
25456
+ }
25457
+ case 'H':
25458
+ case 'k': {
25459
+ sqlite3_str_appendf(&sRes, cf=='H' ? "%02d" : "%2d", x.h);
25460
+ break;
25461
+ }
25462
+ case 'I': /* Fall thru */
25463
+ case 'l': {
25464
+ int h = x.h;
25465
+ if( h>12 ) h -= 12;
25466
+ if( h==0 ) h = 12;
25467
+ sqlite3_str_appendf(&sRes, cf=='I' ? "%02d" : "%2d", h);
25468
break;
25469
}
25470
case 'W': /* Fall thru */
@@ -24867,7 +25476,7 @@ static void strftimeFunc(
25476
y.D = 1;
25477
computeJD(&y);
25478
nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
24870
- if( zFmt[i]=='W' ){
25479
+ if( cf=='W' ){
25480
int wd; /* 0=Monday, 1=Tuesday, ... 6=Sunday */
25481
wd = (int)(((x.iJD+43200000)/86400000)%7);
25482
sqlite3_str_appendf(&sRes,"%02d",(nDay+7-wd)/7);
@@ -24888,6 +25497,19 @@ static void strftimeFunc(
25497
sqlite3_str_appendf(&sRes,"%02d",x.m);
25498
break;
25499
}
25500
+ case 'p': /* Fall thru */
25501
+ case 'P': {
25502
+ if( x.h>=12 ){
25503
+ sqlite3_str_append(&sRes, cf=='p' ? "PM" : "pm", 2);
25504
+ }else{
25505
+ sqlite3_str_append(&sRes, cf=='p' ? "AM" : "am", 2);
25506
+ }
25507
+ break;
25508
+ }
25509
+ case 'R': {
25510
+ sqlite3_str_appendf(&sRes, "%02d:%02d", x.h, x.m);
25511
+ break;
25512
+ }
25513
case 's': {
25514
if( x.useSubsec ){
25515
sqlite3_str_appendf(&sRes,"%.3f",
@@ -24902,9 +25524,15 @@ static void strftimeFunc(
25524
sqlite3_str_appendf(&sRes,"%02d",(int)x.s);
25525
break;
25526
}
25527
+ case 'T': {
25528
+ sqlite3_str_appendf(&sRes,"%02d:%02d:%02d", x.h, x.m, (int)x.s);
25529
+ break;
25530
+ }
25531
+ case 'u': /* Fall thru */
25532
case 'w': {
24906
- sqlite3_str_appendchar(&sRes, 1,
24907
- (char)(((x.iJD+129600000)/86400000) % 7) + '0');
25533
+ char c = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
25534
+ if( c=='0' && cf=='u' ) c = '7';
25535
+ sqlite3_str_appendchar(&sRes, 1, c);
25536
break;
25537
}
25538
case 'Y': {
@@ -24953,6 +25581,117 @@ static void cdateFunc(
25581
dateFunc(context, 0, 0);
25582
}
25583
25584
+/*
25585
+** timediff(DATE1, DATE2)
25586
+**
25587
+** Return the amount of time that must be added to DATE2 in order to
25588
+** convert it into DATE2. The time difference format is:
25589
+**
25590
+** +YYYY-MM-DD HH:MM:SS.SSS
25591
+**
25592
+** The initial "+" becomes "-" if DATE1 occurs before DATE2. For
25593
+** date/time values A and B, the following invariant should hold:
25594
+**
25595
+** datetime(A) == (datetime(B, timediff(A,B))
25596
+**
25597
+** Both DATE arguments must be either a julian day number, or an
25598
+** ISO-8601 string. The unix timestamps are not supported by this
25599
+** routine.
25600
+*/
25601
+static void timediffFunc(
25602
+ sqlite3_context *context,
25603
+ int NotUsed1,
25604
+ sqlite3_value **argv
25605
+){
25606
+ char sign;
25607
+ int Y, M;
25608
+ DateTime d1, d2;
25609
+ sqlite3_str sRes;
25610
+ UNUSED_PARAMETER(NotUsed1);
25611
+ if( isDate(context, 1, &argv[0], &d1) ) return;
25612
+ if( isDate(context, 1, &argv[1], &d2) ) return;
25613
+ computeYMD_HMS(&d1);
25614
+ computeYMD_HMS(&d2);
25615
+ if( d1.iJD>=d2.iJD ){
25616
+ sign = '+';
25617
+ Y = d1.Y - d2.Y;
25618
+ if( Y ){
25619
+ d2.Y = d1.Y;
25620
+ d2.validJD = 0;
25621
+ computeJD(&d2);
25622
+ }
25623
+ M = d1.M - d2.M;
25624
+ if( M<0 ){
25625
+ Y--;
25626
+ M += 12;
25627
+ }
25628
+ if( M!=0 ){
25629
+ d2.M = d1.M;
25630
+ d2.validJD = 0;
25631
+ computeJD(&d2);
25632
+ }
25633
+ while( d1.iJD<d2.iJD ){
25634
+ M--;
25635
+ if( M<0 ){
25636
+ M = 11;
25637
+ Y--;
25638
+ }
25639
+ d2.M--;
25640
+ if( d2.M<1 ){
25641
+ d2.M = 12;
25642
+ d2.Y--;
25643
+ }
25644
+ d2.validJD = 0;
25645
+ computeJD(&d2);
25646
+ }
25647
+ d1.iJD -= d2.iJD;
25648
+ d1.iJD += (u64)1486995408 * (u64)100000;
25649
+ }else /* d1<d2 */{
25650
+ sign = '-';
25651
+ Y = d2.Y - d1.Y;
25652
+ if( Y ){
25653
+ d2.Y = d1.Y;
25654
+ d2.validJD = 0;
25655
+ computeJD(&d2);
25656
+ }
25657
+ M = d2.M - d1.M;
25658
+ if( M<0 ){
25659
+ Y--;
25660
+ M += 12;
25661
+ }
25662
+ if( M!=0 ){
25663
+ d2.M = d1.M;
25664
+ d2.validJD = 0;
25665
+ computeJD(&d2);
25666
+ }
25667
+ while( d1.iJD>d2.iJD ){
25668
+ M--;
25669
+ if( M<0 ){
25670
+ M = 11;
25671
+ Y--;
25672
+ }
25673
+ d2.M++;
25674
+ if( d2.M>12 ){
25675
+ d2.M = 1;
25676
+ d2.Y++;
25677
+ }
25678
+ d2.validJD = 0;
25679
+ computeJD(&d2);
25680
+ }
25681
+ d1.iJD = d2.iJD - d1.iJD;
25682
+ d1.iJD += (u64)1486995408 * (u64)100000;
25683
+ }
25684
+ d1.validYMD = 0;
25685
+ d1.validHMS = 0;
25686
+ d1.validTZ = 0;
25687
+ computeYMD_HMS(&d1);
25688
+ sqlite3StrAccumInit(&sRes, 0, 0, 0, 100);
25689
+ sqlite3_str_appendf(&sRes, "%c%04d-%02d-%02d %02d:%02d:%06.3f",
25690
+ sign, Y, M, d1.D-1, d1.h, d1.m, d1.s);
25691
+ sqlite3ResultStrAccum(context, &sRes);
25692
+}
25693
+
25694
+
25695
/*
25696
** current_timestamp()
25697
**
@@ -25027,6 +25766,7 @@ SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
25766
PURE_DATE(time, -1, 0, 0, timeFunc ),
25767
PURE_DATE(datetime, -1, 0, 0, datetimeFunc ),
25768
PURE_DATE(strftime, -1, 0, 0, strftimeFunc ),
25769
+ PURE_DATE(timediff, 2, 0, 0, timediffFunc ),
25770
DFUNCTION(current_time, 0, 0, 0, ctimeFunc ),
25771
DFUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
25772
DFUNCTION(current_date, 0, 0, 0, cdateFunc ),
@@ -25180,7 +25920,7 @@ SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
25920
/* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
25921
** is using a regular VFS, it is called after the corresponding
25922
** transaction has been committed. Injecting a fault at this point
25183
- ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
25923
+ ** confuses the test scripts - the COMMIT command returns SQLITE_NOMEM
25924
** but the transaction is committed anyway.
25925
**
25926
** The core must call OsFileControl() though, not OsFileControlHint(),
@@ -25801,7 +26541,7 @@ static void *sqlite3MemMalloc(int nByte){
26541
** or sqlite3MemRealloc().
26542
**
26543
** For this low-level routine, we already know that pPrior!=0 since
25804
-** cases where pPrior==0 will have been intecepted and dealt with
26544
+** cases where pPrior==0 will have been intercepted and dealt with
26545
** by higher-level routines.
26546
*/
26547
static void sqlite3MemFree(void *pPrior){
@@ -25889,7 +26629,7 @@ static int sqlite3MemInit(void *NotUsed){
26629
return SQLITE_OK;
26630
}
26631
len = sizeof(cpuCount);
25892
- /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
26632
+ /* One usually wants to use hw.activecpu for MT decisions, but not here */
26633
sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
26634
if( cpuCount>1 ){
26635
/* defer MT decisions to system malloc */
@@ -27881,7 +28621,7 @@ static void checkMutexFree(sqlite3_mutex *p){
28621
assert( SQLITE_MUTEX_FAST<2 );
28622
assert( SQLITE_MUTEX_WARNONCONTENTION<2 );
28623
27884
-#if SQLITE_ENABLE_API_ARMOR
28624
+#ifdef SQLITE_ENABLE_API_ARMOR
28625
if( ((CheckMutex*)p)->iType<2 )
28626
#endif
28627
{
@@ -28356,7 +29096,7 @@ SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
29096
29097
/*
29098
** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
28359
-** are necessary under two condidtions: (1) Debug builds and (2) using
29099
+** are necessary under two conditions: (1) Debug builds and (2) using
29100
** home-grown mutexes. Encapsulate these conditions into a single #define.
29101
*/
29102
#if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
@@ -28553,7 +29293,7 @@ static sqlite3_mutex *pthreadMutexAlloc(int iType){
29293
*/
29294
static void pthreadMutexFree(sqlite3_mutex *p){
29295
assert( p->nRef==0 );
28556
-#if SQLITE_ENABLE_API_ARMOR
29296
+#ifdef SQLITE_ENABLE_API_ARMOR
29297
if( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE )
29298
#endif
29299
{
@@ -28857,7 +29597,7 @@ struct sqlite3_mutex {
29597
CRITICAL_SECTION mutex; /* Mutex controlling the lock */
29598
int id; /* Mutex type */
29599
#ifdef SQLITE_DEBUG
28860
- volatile int nRef; /* Number of enterances */
29600
+ volatile int nRef; /* Number of entrances */
29601
volatile DWORD owner; /* Thread holding this mutex */
29602
volatile LONG trace; /* True to trace changes */
29603
#endif
@@ -28906,7 +29646,7 @@ SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29646
SQLITE_MEMORY_BARRIER;
29647
#elif defined(__GNUC__)
29648
__sync_synchronize();
28909
-#elif MSVC_VERSION>=1300
29649
+#elif MSVC_VERSION>=1400
29650
_ReadWriteBarrier();
29651
#elif defined(MemoryBarrier)
29652
MemoryBarrier();
@@ -30117,7 +30857,7 @@ SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
30857
if( db->mallocFailed || rc ){
30858
return apiHandleError(db, rc);
30859
}
30120
- return rc & db->errMask;
30860
+ return 0;
30861
}
30862
30863
/************** End of malloc.c **********************************************/
@@ -30229,57 +30969,6 @@ static const et_info fmtinfo[] = {
30969
** %!S Like %S but prefer the zName over the zAlias
30970
*/
30971
30232
-/* Floating point constants used for rounding */
30233
-static const double arRound[] = {
30234
- 5.0e-01, 5.0e-02, 5.0e-03, 5.0e-04, 5.0e-05,
30235
- 5.0e-06, 5.0e-07, 5.0e-08, 5.0e-09, 5.0e-10,
30236
-};
30237
-
30238
-/*
30239
-** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
30240
-** conversions will work.
30241
-*/
30242
-#ifndef SQLITE_OMIT_FLOATING_POINT
30243
-/*
30244
-** "*val" is a double such that 0.1 <= *val < 10.0
30245
-** Return the ascii code for the leading digit of *val, then
30246
-** multiply "*val" by 10.0 to renormalize.
30247
-**
30248
-** Example:
30249
-** input: *val = 3.14159
30250
-** output: *val = 1.4159 function return = '3'
30251
-**
30252
-** The counter *cnt is incremented each time. After counter exceeds
30253
-** 16 (the number of significant digits in a 64-bit float) '0' is
30254
-** always returned.
30255
-*/
30256
-static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
30257
- int digit;
30258
- LONGDOUBLE_TYPE d;
30259
- if( (*cnt)<=0 ) return '0';
30260
- (*cnt)--;
30261
- digit = (int)*val;
30262
- d = digit;
30263
- digit += '0';
30264
- *val = (*val - d)*10.0;
30265
- return (char)digit;
30266
-}
30267
-#endif /* SQLITE_OMIT_FLOATING_POINT */
30268
-
30269
-#ifndef SQLITE_OMIT_FLOATING_POINT
30270
-/*
30271
-** "*val" is a u64. *msd is a divisor used to extract the
30272
-** most significant digit of *val. Extract that most significant
30273
-** digit and return it.
30274
-*/
30275
-static char et_getdigit_int(u64 *val, u64 *msd){
30276
- u64 x = (*val)/(*msd);
30277
- *val -= x*(*msd);
30278
- if( *msd>=10 ) *msd /= 10;
30279
- return '0' + (char)(x & 15);
30280
-}
30281
-#endif /* SQLITE_OMIT_FLOATING_POINT */
30282
-
30972
/*
30973
** Set the StrAccum object to an error mode.
30974
*/
@@ -30371,20 +31060,15 @@ SQLITE_API void sqlite3_str_vappendf(
31060
u8 bArgList; /* True for SQLITE_PRINTF_SQLFUNC */
31061
char prefix; /* Prefix character. "+" or "-" or " " or '\0'. */
31062
sqlite_uint64 longvalue; /* Value for integer types */
30374
- LONGDOUBLE_TYPE realvalue; /* Value for real types */
30375
- sqlite_uint64 msd; /* Divisor to get most-significant-digit
30376
- ** of longvalue */
31063
+ double realvalue; /* Value for real types */
31064
const et_info *infop; /* Pointer to the appropriate info structure */
31065
char *zOut; /* Rendering buffer */
31066
int nOut; /* Size of the rendering buffer */
31067
char *zExtra = 0; /* Malloced memory used by some conversion */
30381
-#ifndef SQLITE_OMIT_FLOATING_POINT
30382
- int exp, e2; /* exponent of real numbers */
30383
- int nsd; /* Number of significant digits returned */
30384
- double rounder; /* Used for rounding floating point values */
31068
+ int exp, e2; /* exponent of real numbers */
31069
etByte flag_dp; /* True if decimal point should be shown */
31070
etByte flag_rtz; /* True if trailing zeros should be removed */
30387
-#endif
31071
+
31072
PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
31073
char buf[etBUFSIZE]; /* Conversion buffer */
31074
@@ -30659,94 +31343,62 @@ SQLITE_API void sqlite3_str_vappendf(
31343
break;
31344
case etFLOAT:
31345
case etEXP:
30662
- case etGENERIC:
31346
+ case etGENERIC: {
31347
+ FpDecode s;
31348
+ int iRound;
31349
+ int j;
31350
+
31351
if( bArgList ){
31352
realvalue = getDoubleArg(pArgList);
31353
}else{
31354
realvalue = va_arg(ap,double);
31355
}
30668
-#ifdef SQLITE_OMIT_FLOATING_POINT
30669
- length = 0;
30670
-#else
31356
if( precision<0 ) precision = 6; /* Set default precision */
31357
#ifdef SQLITE_FP_PRECISION_LIMIT
31358
if( precision>SQLITE_FP_PRECISION_LIMIT ){
31359
precision = SQLITE_FP_PRECISION_LIMIT;
31360
}
31361
#endif
30677
- if( realvalue<0.0 ){
30678
- realvalue = -realvalue;
30679
- prefix = '-';
31362
+ if( xtype==etFLOAT ){
31363
+ iRound = -precision;
31364
+ }else if( xtype==etGENERIC ){
31365
+ if( precision==0 ) precision = 1;
31366
+ iRound = precision;
31367
}else{
30681
- prefix = flag_prefix;
31368
+ iRound = precision+1;
31369
}
30683
- exp = 0;
30684
- if( xtype==etGENERIC && precision>0 ) precision--;
30685
- testcase( precision>0xfff );
30686
- if( realvalue<1.0e+16
30687
- && realvalue==(LONGDOUBLE_TYPE)(longvalue = (u64)realvalue)
30688
- ){
30689
- /* Number is a pure integer that can be represented as u64 */
30690
- for(msd=1; msd*10<=longvalue; msd *= 10, exp++){}
30691
- if( exp>precision && xtype!=etFLOAT ){
30692
- u64 rnd = msd/2;
30693
- int kk = precision;
30694
- while( kk-- > 0 ){ rnd /= 10; }
30695
- longvalue += rnd;
30696
- }
30697
- }else{
30698
- msd = 0;
30699
- longvalue = 0; /* To prevent a compiler warning */
30700
- idx = precision & 0xfff;
30701
- rounder = arRound[idx%10];
30702
- while( idx>=10 ){ rounder *= 1.0e-10; idx -= 10; }
30703
- if( xtype==etFLOAT ){
30704
- double rx = (double)realvalue;
30705
- sqlite3_uint64 u;
30706
- int ex;
30707
- memcpy(&u, &rx, sizeof(u));
30708
- ex = -1023 + (int)((u>>52)&0x7ff);
30709
- if( precision+(ex/3) < 15 ) rounder += realvalue*3e-16;
30710
- realvalue += rounder;
30711
- }
30712
- if( sqlite3IsNaN((double)realvalue) ){
30713
- if( flag_zeropad ){
30714
- bufpt = "null";
30715
- length = 4;
31370
+ sqlite3FpDecode(&s, realvalue, iRound, flag_altform2 ? 26 : 16);
31371
+ if( s.isSpecial ){
31372
+ if( s.isSpecial==2 ){
31373
+ bufpt = flag_zeropad ? "null" : "NaN";
31374
+ length = sqlite3Strlen30(bufpt);
31375
+ break;
31376
+ }else if( flag_zeropad ){
31377
+ s.z[0] = '9';
31378
+ s.iDP = 1000;
31379
+ s.n = 1;
31380
+ }else{
31381
+ memcpy(buf, "-Inf", 5);
31382
+ bufpt = buf;
31383
+ if( s.sign=='-' ){
31384
+ /* no-op */
31385
+ }else if( flag_prefix ){
31386
+ buf[0] = flag_prefix;
31387
}else{
30717
- bufpt = "NaN";
30718
- length = 3;
31388
+ bufpt++;
31389
}
31390
+ length = sqlite3Strlen30(bufpt);
31391
break;
31392
}
30722
-
30723
- /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
30724
- if( ALWAYS(realvalue>0.0) ){
30725
- LONGDOUBLE_TYPE scale = 1.0;
30726
- while( realvalue>=1e100*scale && exp<=350){ scale*=1e100;exp+=100;}
30727
- while( realvalue>=1e10*scale && exp<=350 ){ scale*=1e10; exp+=10; }
30728
- while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
30729
- realvalue /= scale;
30730
- while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
30731
- while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
30732
- if( exp>350 ){
30733
- if( flag_zeropad ){
30734
- realvalue = 9.0;
30735
- exp = 999;
30736
- }else{
30737
- bufpt = buf;
30738
- buf[0] = prefix;
30739
- memcpy(buf+(prefix!=0),"Inf",4);
30740
- length = 3+(prefix!=0);
30741
- break;
30742
- }
30743
- }
30744
- if( xtype!=etFLOAT ){
30745
- realvalue += rounder;
30746
- if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
30747
- }
30748
- }
31393
}
31394
+ if( s.sign=='-' ){
31395
+ prefix = '-';
31396
+ }else{
31397
+ prefix = flag_prefix;
31398
+ }
31399
+
31400
+ exp = s.iDP-1;
31401
+ if( xtype==etGENERIC && precision>0 ) precision--;
31402
31403
/*
31404
** If the field type is etGENERIC, then convert to either etEXP
@@ -30766,9 +31418,8 @@ SQLITE_API void sqlite3_str_vappendf(
31418
if( xtype==etEXP ){
31419
e2 = 0;
31420
}else{
30769
- e2 = exp;
31421
+ e2 = s.iDP - 1;
31422
}
30771
- nsd = 16 + flag_altform2*10;
31423
bufpt = buf;
31424
{
31425
i64 szBufNeeded; /* Size of a temporary buffer needed */
@@ -30786,16 +31437,12 @@ SQLITE_API void sqlite3_str_vappendf(
31437
*(bufpt++) = prefix;
31438
}
31439
/* Digits prior to the decimal point */
31440
+ j = 0;
31441
if( e2<0 ){
31442
*(bufpt++) = '0';
30791
- }else if( msd>0 ){
30792
- for(; e2>=0; e2--){
30793
- *(bufpt++) = et_getdigit_int(&longvalue,&msd);
30794
- if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
30795
- }
31443
}else{
31444
for(; e2>=0; e2--){
30798
- *(bufpt++) = et_getdigit(&realvalue,&nsd);
31445
+ *(bufpt++) = j<s.n ? s.z[j++] : '0';
31446
if( cThousand && (e2%3)==0 && e2>1 ) *(bufpt++) = ',';
31447
}
31448
}
@@ -30805,19 +31452,12 @@ SQLITE_API void sqlite3_str_vappendf(
31452
}
31453
/* "0" digits after the decimal point but before the first
31454
** significant digit of the number */
30808
- for(e2++; e2<0; precision--, e2++){
30809
- assert( precision>0 );
31455
+ for(e2++; e2<0 && precision>0; precision--, e2++){
31456
*(bufpt++) = '0';
31457
}
31458
/* Significant digits after the decimal point */
30813
- if( msd>0 ){
30814
- while( (precision--)>0 ){
30815
- *(bufpt++) = et_getdigit_int(&longvalue,&msd);
30816
- }
30817
- }else{
30818
- while( (precision--)>0 ){
30819
- *(bufpt++) = et_getdigit(&realvalue,&nsd);
30820
- }
31459
+ while( (precision--)>0 ){
31460
+ *(bufpt++) = j<s.n ? s.z[j++] : '0';
31461
}
31462
/* Remove trailing zeros and the "." if no digits follow the "." */
31463
if( flag_rtz && flag_dp ){
@@ -30833,6 +31473,7 @@ SQLITE_API void sqlite3_str_vappendf(
31473
}
31474
/* Add the "eNNN" suffix */
31475
if( xtype==etEXP ){
31476
+ exp = s.iDP - 1;
31477
*(bufpt++) = aDigits[infop->charset];
31478
if( exp<0 ){
31479
*(bufpt++) = '-'; exp = -exp;
@@ -30866,8 +31507,8 @@ SQLITE_API void sqlite3_str_vappendf(
31507
while( nPad-- ) bufpt[i++] = '0';
31508
length = width;
31509
}
30869
-#endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
31510
break;
31511
+ }
31512
case etSIZE:
31513
if( !bArgList ){
31514
*(va_arg(ap,int*)) = pAccum->nChar;
@@ -31591,6 +32232,75 @@ SQLITE_API void sqlite3_str_appendf(StrAccum *p, const char *zFormat, ...){
32232
va_end(ap);
32233
}
32234
32235
+
32236
+/*****************************************************************************
32237
+** Reference counted string/blob storage
32238
+*****************************************************************************/
32239
+
32240
+/*
32241
+** Increase the reference count of the string by one.
32242
+**
32243
+** The input parameter is returned.
32244
+*/
32245
+SQLITE_PRIVATE char *sqlite3RCStrRef(char *z){
32246
+ RCStr *p = (RCStr*)z;
32247
+ assert( p!=0 );
32248
+ p--;
32249
+ p->nRCRef++;
32250
+ return z;
32251
+}
32252
+
32253
+/*
32254
+** Decrease the reference count by one. Free the string when the
32255
+** reference count reaches zero.
32256
+*/
32257
+SQLITE_PRIVATE void sqlite3RCStrUnref(void *z){
32258
+ RCStr *p = (RCStr*)z;
32259
+ assert( p!=0 );
32260
+ p--;
32261
+ assert( p->nRCRef>0 );
32262
+ if( p->nRCRef>=2 ){
32263
+ p->nRCRef--;
32264
+ }else{
32265
+ sqlite3_free(p);
32266
+ }
32267
+}
32268
+
32269
+/*
32270
+** Create a new string that is capable of holding N bytes of text, not counting
32271
+** the zero byte at the end. The string is uninitialized.
32272
+**
32273
+** The reference count is initially 1. Call sqlite3RCStrUnref() to free the
32274
+** newly allocated string.
32275
+**
32276
+** This routine returns 0 on an OOM.
32277
+*/
32278
+SQLITE_PRIVATE char *sqlite3RCStrNew(u64 N){
32279
+ RCStr *p = sqlite3_malloc64( N + sizeof(*p) + 1 );
32280
+ if( p==0 ) return 0;
32281
+ p->nRCRef = 1;
32282
+ return (char*)&p[1];
32283
+}
32284
+
32285
+/*
32286
+** Change the size of the string so that it is able to hold N bytes.
32287
+** The string might be reallocated, so return the new allocation.
32288
+*/
32289
+SQLITE_PRIVATE char *sqlite3RCStrResize(char *z, u64 N){
32290
+ RCStr *p = (RCStr*)z;
32291
+ RCStr *pNew;
32292
+ assert( p!=0 );
32293
+ p--;
32294
+ assert( p->nRCRef==1 );
32295
+ pNew = sqlite3_realloc64(p, N+sizeof(RCStr)+1);
32296
+ if( pNew==0 ){
32297
+ sqlite3_free(p);
32298
+ return 0;
32299
+ }else{
32300
+ return (char*)&pNew[1];
32301
+ }
32302
+}
32303
+
32304
/************** End of printf.c **********************************************/
32305
/************** Begin file treeview.c ****************************************/
32306
/*
@@ -32007,6 +32717,7 @@ SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u
32717
sqlite3TreeViewItem(pView, "FILTER", 1);
32718
sqlite3TreeViewExpr(pView, pWin->pFilter, 0);
32719
sqlite3TreeViewPop(&pView);
32720
+ if( pWin->eFrmType==TK_FILTER ) return;
32721
}
32722
sqlite3TreeViewPush(&pView, more);
32723
if( pWin->zName ){
@@ -32016,7 +32727,7 @@ SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u
32727
}
32728
if( pWin->zBase ) nElement++;
32729
if( pWin->pOrderBy ) nElement++;
32019
- if( pWin->eFrmType ) nElement++;
32730
+ if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ) nElement++;
32731
if( pWin->eExclude ) nElement++;
32732
if( pWin->zBase ){
32733
sqlite3TreeViewPush(&pView, (--nElement)>0);
@@ -32029,7 +32740,7 @@ SQLITE_PRIVATE void sqlite3TreeViewWindow(TreeView *pView, const Window *pWin, u
32740
if( pWin->pOrderBy ){
32741
sqlite3TreeViewExprList(pView, pWin->pOrderBy, (--nElement)>0, "ORDER-BY");
32742
}
32032
- if( pWin->eFrmType ){
32743
+ if( pWin->eFrmType!=0 && pWin->eFrmType!=TK_FILTER ){
32744
char zBuf[30];
32745
const char *zFrmType = "ROWS";
32746
if( pWin->eFrmType==TK_RANGE ) zFrmType = "RANGE";
@@ -32238,7 +32949,8 @@ SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
32949
};
32950
assert( pExpr->op2==TK_IS || pExpr->op2==TK_ISNOT );
32951
assert( pExpr->pRight );
32241
- assert( sqlite3ExprSkipCollate(pExpr->pRight)->op==TK_TRUEFALSE );
32952
+ assert( sqlite3ExprSkipCollateAndLikely(pExpr->pRight)->op
32953
+ == TK_TRUEFALSE );
32954
x = (pExpr->op2==TK_ISNOT)*2 + sqlite3ExprTruthValue(pExpr->pRight);
32955
zUniOp = azOp[x];
32956
break;
@@ -32276,7 +32988,7 @@ SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
32988
assert( ExprUseXList(pExpr) );
32989
pFarg = pExpr->x.pList;
32990
#ifndef SQLITE_OMIT_WINDOWFUNC
32279
- pWin = ExprHasProperty(pExpr, EP_WinFunc) ? pExpr->y.pWin : 0;
32991
+ pWin = IsWindowFunc(pExpr) ? pExpr->y.pWin : 0;
32992
#else
32993
pWin = 0;
32994
#endif
@@ -32302,7 +33014,13 @@ SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
33014
sqlite3TreeViewLine(pView, "FUNCTION %Q%s", pExpr->u.zToken, zFlgs);
33015
}
33016
if( pFarg ){
32305
- sqlite3TreeViewExprList(pView, pFarg, pWin!=0, 0);
33017
+ sqlite3TreeViewExprList(pView, pFarg, pWin!=0 || pExpr->pLeft, 0);
33018
+ if( pExpr->pLeft ){
33019
+ Expr *pOB = pExpr->pLeft;
33020
+ assert( pOB->op==TK_ORDER );
33021
+ assert( ExprUseXList(pOB) );
33022
+ sqlite3TreeViewExprList(pView, pOB->x.pList, pWin!=0, "ORDERBY");
33023
+ }
33024
}
33025
#ifndef SQLITE_OMIT_WINDOWFUNC
33026
if( pWin ){
@@ -32311,6 +33029,10 @@ SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
33029
#endif
33030
break;
33031
}
33032
+ case TK_ORDER: {
33033
+ sqlite3TreeViewExprList(pView, pExpr->x.pList, 0, "ORDERBY");
33034
+ break;
33035
+ }
33036
#ifndef SQLITE_OMIT_SUBQUERY
33037
case TK_EXISTS: {
33038
assert( ExprUseXSelect(pExpr) );
@@ -32364,7 +33086,7 @@ SQLITE_PRIVATE void sqlite3TreeViewExpr(TreeView *pView, const Expr *pExpr, u8 m
33086
assert( pExpr->x.pList->nExpr==2 );
33087
pY = pExpr->x.pList->a[0].pExpr;
33088
pZ = pExpr->x.pList->a[1].pExpr;
32367
- sqlite3TreeViewLine(pView, "BETWEEN");
33089
+ sqlite3TreeViewLine(pView, "BETWEEN%s", zFlgs);
33090
sqlite3TreeViewExpr(pView, pX, 1);
33091
sqlite3TreeViewExpr(pView, pY, 1);
33092
sqlite3TreeViewExpr(pView, pZ, 0);
@@ -33499,7 +34221,38 @@ SQLITE_PRIVATE u32 sqlite3Utf8Read(
34221
return c;
34222
}
34223
33502
-
34224
+/*
34225
+** Read a single UTF8 character out of buffer z[], but reading no
34226
+** more than n characters from the buffer. z[] is not zero-terminated.
34227
+**
34228
+** Return the number of bytes used to construct the character.
34229
+**
34230
+** Invalid UTF8 might generate a strange result. No effort is made
34231
+** to detect invalid UTF8.
34232
+**
34233
+** At most 4 bytes will be read out of z[]. The return value will always
34234
+** be between 1 and 4.
34235
+*/
34236
+SQLITE_PRIVATE int sqlite3Utf8ReadLimited(
34237
+ const u8 *z,
34238
+ int n,
34239
+ u32 *piOut
34240
+){
34241
+ u32 c;
34242
+ int i = 1;
34243
+ assert( n>0 );
34244
+ c = z[0];
34245
+ if( c>=0xc0 ){
34246
+ c = sqlite3Utf8Trans1[c-0xc0];
34247
+ if( n>4 ) n = 4;
34248
+ while( i<n && (z[i] & 0xc0)==0x80 ){
34249
+ c = (c<<6) + (0x3f & z[i]);
34250
+ i++;
34251
+ }
34252
+ }
34253
+ *piOut = c;
34254
+ return i;
34255
+}
34256
34257
34258
/*
@@ -33897,7 +34650,7 @@ SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
34650
/*
34651
** Calls to sqlite3FaultSim() are used to simulate a failure during testing,
34652
** or to bypass normal error detection during testing in order to let
33900
-** execute proceed futher downstream.
34653
+** execute proceed further downstream.
34654
**
34655
** In deployment, sqlite3FaultSim() *always* return SQLITE_OK (0). The
34656
** sqlite3FaultSim() function only returns non-zero during testing.
@@ -33941,6 +34694,19 @@ SQLITE_PRIVATE int sqlite3IsNaN(double x){
34694
}
34695
#endif /* SQLITE_OMIT_FLOATING_POINT */
34696
34697
+#ifndef SQLITE_OMIT_FLOATING_POINT
34698
+/*
34699
+** Return true if the floating point value is NaN or +Inf or -Inf.
34700
+*/
34701
+SQLITE_PRIVATE int sqlite3IsOverflow(double x){
34702
+ int rc; /* The value return */
34703
+ u64 y;
34704
+ memcpy(&y,&x,sizeof(y));
34705
+ rc = IsOvfl(y);
34706
+ return rc;
34707
+}
34708
+#endif /* SQLITE_OMIT_FLOATING_POINT */
34709
+
34710
/*
34711
** Compute a string length that is limited to what can be stored in
34712
** lower 30 bits of a 32-bit signed integer.
@@ -34014,6 +34780,23 @@ SQLITE_PRIVATE void sqlite3ErrorClear(sqlite3 *db){
34780
*/
34781
SQLITE_PRIVATE void sqlite3SystemError(sqlite3 *db, int rc){
34782
if( rc==SQLITE_IOERR_NOMEM ) return;
34783
+#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
34784
+ if( rc==SQLITE_IOERR_IN_PAGE ){
34785
+ int ii;
34786
+ int iErr;
34787
+ sqlite3BtreeEnterAll(db);
34788
+ for(ii=0; ii<db->nDb; ii++){
34789
+ if( db->aDb[ii].pBt ){
34790
+ iErr = sqlite3PagerWalSystemErrno(sqlite3BtreePager(db->aDb[ii].pBt));
34791
+ if( iErr ){
34792
+ db->iSysErrno = iErr;
34793
+ }
34794
+ }
34795
+ }
34796
+ sqlite3BtreeLeaveAll(db);
34797
+ return;
34798
+ }
34799
+#endif
34800
rc &= 0xff;
34801
if( rc==SQLITE_CANTOPEN || rc==SQLITE_IOERR ){
34802
db->iSysErrno = sqlite3OsGetLastError(db->pVfs);
@@ -34058,12 +34841,16 @@ SQLITE_PRIVATE void sqlite3ProgressCheck(Parse *p){
34841
p->rc = SQLITE_INTERRUPT;
34842
}
34843
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
34061
- if( db->xProgress && (++p->nProgressSteps)>=db->nProgressOps ){
34062
- if( db->xProgress(db->pProgressArg) ){
34063
- p->nErr++;
34064
- p->rc = SQLITE_INTERRUPT;
34844
+ if( db->xProgress ){
34845
+ if( p->rc==SQLITE_INTERRUPT ){
34846
+ p->nProgressSteps = 0;
34847
+ }else if( (++p->nProgressSteps)>=db->nProgressOps ){
34848
+ if( db->xProgress(db->pProgressArg) ){
34849
+ p->nErr++;
34850
+ p->rc = SQLITE_INTERRUPT;
34851
+ }
34852
+ p->nProgressSteps = 0;
34853
}
34066
- p->nProgressSteps = 0;
34854
}
34855
#endif
34856
}
@@ -34259,43 +35046,40 @@ SQLITE_PRIVATE u8 sqlite3StrIHash(const char *z){
35046
return h;
35047
}
35048
34262
-/*
34263
-** Compute 10 to the E-th power. Examples: E==1 results in 10.
34264
-** E==2 results in 100. E==50 results in 1.0e50.
35049
+/* Double-Double multiplication. (x[0],x[1]) *= (y,yy)
35050
**
34266
-** This routine only works for values of E between 1 and 341.
35051
+** Reference:
35052
+** T. J. Dekker, "A Floating-Point Technique for Extending the
35053
+** Available Precision". 1971-07-26.
35054
*/
34268
-static LONGDOUBLE_TYPE sqlite3Pow10(int E){
34269
-#if defined(_MSC_VER)
34270
- static const LONGDOUBLE_TYPE x[] = {
34271
- 1.0e+001L,
34272
- 1.0e+002L,
34273
- 1.0e+004L,
34274
- 1.0e+008L,
34275
- 1.0e+016L,
34276
- 1.0e+032L,
34277
- 1.0e+064L,
34278
- 1.0e+128L,
34279
- 1.0e+256L
34280
- };
34281
- LONGDOUBLE_TYPE r = 1.0;
34282
- int i;
34283
- assert( E>=0 && E<=307 );
34284
- for(i=0; E!=0; i++, E >>=1){
34285
- if( E & 1 ) r *= x[i];
34286
- }
34287
- return r;
34288
-#else
34289
- LONGDOUBLE_TYPE x = 10.0;
34290
- LONGDOUBLE_TYPE r = 1.0;
34291
- while(1){
34292
- if( E & 1 ) r *= x;
34293
- E >>= 1;
34294
- if( E==0 ) break;
34295
- x *= x;
34296
- }
34297
- return r;
34298
-#endif
35055
+static void dekkerMul2(volatile double *x, double y, double yy){
35056
+ /*
35057
+ ** The "volatile" keywords on parameter x[] and on local variables
35058
+ ** below are needed force intermediate results to be truncated to
35059
+ ** binary64 rather than be carried around in an extended-precision
35060
+ ** format. The truncation is necessary for the Dekker algorithm to
35061
+ ** work. Intel x86 floating point might omit the truncation without
35062
+ ** the use of volatile.
35063
+ */
35064
+ volatile double tx, ty, p, q, c, cc;
35065
+ double hx, hy;
35066
+ u64 m;
35067
+ memcpy(&m, (void*)&x[0], 8);
35068
+ m &= 0xfffffffffc000000LL;
35069
+ memcpy(&hx, &m, 8);
35070
+ tx = x[0] - hx;
35071
+ memcpy(&m, &y, 8);
35072
+ m &= 0xfffffffffc000000LL;
35073
+ memcpy(&hy, &m, 8);
35074
+ ty = y - hy;
35075
+ p = hx*hy;
35076
+ q = hx*ty + tx*hy;
35077
+ c = p+q;
35078
+ cc = p - c + q + tx*ty;
35079
+ cc = x[0]*yy + x[1]*y + cc;
35080
+ x[0] = c + cc;
35081
+ x[1] = c - x[0];
35082
+ x[1] += cc;
35083
}
35084
35085
/*
@@ -34336,12 +35120,11 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
35120
const char *zEnd;
35121
/* sign * significand * (10 ^ (esign * exponent)) */
35122
int sign = 1; /* sign of significand */
34339
- i64 s = 0; /* significand */
35123
+ u64 s = 0; /* significand */
35124
int d = 0; /* adjust exponent for shifting decimal point */
35125
int esign = 1; /* sign of exponent */
35126
int e = 0; /* exponent */
35127
int eValid = 1; /* True exponent is either not used or is well-formed */
34344
- double result;
35128
int nDigit = 0; /* Number of digits processed */
35129
int eType = 1; /* 1: pure integer, 2+: fractional -1 or less: bad UTF16 */
35130
@@ -34381,7 +35164,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
35164
while( z<zEnd && sqlite3Isdigit(*z) ){
35165
s = s*10 + (*z - '0');
35166
z+=incr; nDigit++;
34384
- if( s>=((LARGEST_INT64-9)/10) ){
35167
+ if( s>=((LARGEST_UINT64-9)/10) ){
35168
/* skip non-significant significand digits
35169
** (increase exponent by d to shift decimal left) */
35170
while( z<zEnd && sqlite3Isdigit(*z) ){ z+=incr; d++; }
@@ -34396,7 +35179,7 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
35179
/* copy digits from after decimal to significand
35180
** (decrease exponent by d to shift decimal right) */
35181
while( z<zEnd && sqlite3Isdigit(*z) ){
34399
- if( s<((LARGEST_INT64-9)/10) ){
35182
+ if( s<((LARGEST_UINT64-9)/10) ){
35183
s = s*10 + (*z - '0');
35184
d--;
35185
nDigit++;
@@ -34436,79 +35219,92 @@ SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 en
35219
while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
35220
35221
do_atof_calc:
34439
- /* adjust exponent by d, and update sign */
34440
- e = (e*esign) + d;
34441
- if( e<0 ) {
34442
- esign = -1;
34443
- e *= -1;
34444
- } else {
34445
- esign = 1;
35222
+ /* Zero is a special case */
35223
+ if( s==0 ){
35224
+ *pResult = sign<0 ? -0.0 : +0.0;
35225
+ goto atof_return;
35226
}
35227
34448
- if( s==0 ) {
34449
- /* In the IEEE 754 standard, zero is signed. */
34450
- result = sign<0 ? -(double)0 : (double)0;
34451
- } else {
34452
- /* Attempt to reduce exponent.
34453
- **
34454
- ** Branches that are not required for the correct answer but which only
34455
- ** help to obtain the correct answer faster are marked with special
34456
- ** comments, as a hint to the mutation tester.
34457
- */
34458
- while( e>0 ){ /*OPTIMIZATION-IF-TRUE*/
34459
- if( esign>0 ){
34460
- if( s>=(LARGEST_INT64/10) ) break; /*OPTIMIZATION-IF-FALSE*/
34461
- s *= 10;
34462
- }else{
34463
- if( s%10!=0 ) break; /*OPTIMIZATION-IF-FALSE*/
34464
- s /= 10;
34465
- }
34466
- e--;
34467
- }
35228
+ /* adjust exponent by d, and update sign */
35229
+ e = (e*esign) + d;
35230
34469
- /* adjust the sign of significand */
34470
- s = sign<0 ? -s : s;
35231
+ /* Try to adjust the exponent to make it smaller */
35232
+ while( e>0 && s<(LARGEST_UINT64/10) ){
35233
+ s *= 10;
35234
+ e--;
35235
+ }
35236
+ while( e<0 && (s%10)==0 ){
35237
+ s /= 10;
35238
+ e++;
35239
+ }
35240
34472
- if( e==0 ){ /*OPTIMIZATION-IF-TRUE*/
34473
- result = (double)s;
35241
+ if( e==0 ){
35242
+ *pResult = s;
35243
+ }else if( sqlite3Config.bUseLongDouble ){
35244
+ LONGDOUBLE_TYPE r = (LONGDOUBLE_TYPE)s;
35245
+ if( e>0 ){
35246
+ while( e>=100 ){ e-=100; r *= 1.0e+100L; }
35247
+ while( e>=10 ){ e-=10; r *= 1.0e+10L; }
35248
+ while( e>=1 ){ e-=1; r *= 1.0e+01L; }
35249
}else{
34475
- /* attempt to handle extremely small/large numbers better */
34476
- if( e>307 ){ /*OPTIMIZATION-IF-TRUE*/
34477
- if( e<342 ){ /*OPTIMIZATION-IF-TRUE*/
34478
- LONGDOUBLE_TYPE scale = sqlite3Pow10(e-308);
34479
- if( esign<0 ){
34480
- result = s / scale;
34481
- result /= 1.0e+308;
34482
- }else{
34483
- result = s * scale;
34484
- result *= 1.0e+308;
34485
- }
34486
- }else{ assert( e>=342 );
34487
- if( esign<0 ){
34488
- result = 0.0*s;
34489
- }else{
35250
+ while( e<=-100 ){ e+=100; r *= 1.0e-100L; }
35251
+ while( e<=-10 ){ e+=10; r *= 1.0e-10L; }
35252
+ while( e<=-1 ){ e+=1; r *= 1.0e-01L; }
35253
+ }
35254
+ assert( r>=0.0 );
35255
+ if( r>+1.7976931348623157081452742373e+308L ){
35256
#ifdef INFINITY
34491
- result = INFINITY*s;
35257
+ *pResult = +INFINITY;
35258
#else
34493
- result = 1e308*1e308*s; /* Infinity */
35259
+ *pResult = 1.0e308*10.0;
35260
#endif
34495
- }
34496
- }
34497
- }else{
34498
- LONGDOUBLE_TYPE scale = sqlite3Pow10(e);
34499
- if( esign<0 ){
34500
- result = s / scale;
34501
- }else{
34502
- result = s * scale;
34503
- }
35261
+ }else{
35262
+ *pResult = (double)r;
35263
+ }
35264
+ }else{
35265
+ double rr[2];
35266
+ u64 s2;
35267
+ rr[0] = (double)s;
35268
+ s2 = (u64)rr[0];
35269
+#if defined(_MSC_VER) && _MSC_VER<1700
35270
+ if( s2==0x8000000000000000LL ){ s2 = 2*(u64)(0.5*rr[0]); }
35271
+#endif
35272
+ rr[1] = s>=s2 ? (double)(s - s2) : -(double)(s2 - s);
35273
+ if( e>0 ){
35274
+ while( e>=100 ){
35275
+ e -= 100;
35276
+ dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
35277
+ }
35278
+ while( e>=10 ){
35279
+ e -= 10;
35280
+ dekkerMul2(rr, 1.0e+10, 0.0);
35281
+ }
35282
+ while( e>=1 ){
35283
+ e -= 1;
35284
+ dekkerMul2(rr, 1.0e+01, 0.0);
35285
+ }
35286
+ }else{
35287
+ while( e<=-100 ){
35288
+ e += 100;
35289
+ dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
35290
+ }
35291
+ while( e<=-10 ){
35292
+ e += 10;
35293
+ dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
35294
+ }
35295
+ while( e<=-1 ){
35296
+ e += 1;
35297
+ dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
35298
}
35299
}
35300
+ *pResult = rr[0]+rr[1];
35301
+ if( sqlite3IsNaN(*pResult) ) *pResult = 1e300*1e300;
35302
}
35303
+ if( sign<0 ) *pResult = -*pResult;
35304
+ assert( !sqlite3IsNaN(*pResult) );
35305
34508
- /* store the result */
34509
- *pResult = result;
34510
-
34511
- /* return true if number and no extra non-whitespace chracters after */
35306
+atof_return:
35307
+ /* return true if number and no extra non-whitespace characters after */
35308
if( z==zEnd && nDigit>0 && eValid && eType>0 ){
35309
return eType;
35310
}else if( eType>=2 && (eType==3 || eValid) && nDigit>0 ){
@@ -34644,7 +35440,7 @@ SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc
35440
/* This test and assignment is needed only to suppress UB warnings
35441
** from clang and -fsanitize=undefined. This test and assignment make
35442
** the code a little larger and slower, and no harm comes from omitting
34647
- ** them, but we must appaise the undefined-behavior pharisees. */
35443
+ ** them, but we must appease the undefined-behavior pharisees. */
35444
*pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
35445
}else if( neg ){
35446
*pNum = -(i64)u;
@@ -34722,7 +35518,9 @@ SQLITE_PRIVATE int sqlite3DecOrHexToI64(const char *z, i64 *pOut){
35518
}else
35519
#endif /* SQLITE_OMIT_HEX_INTEGER */
35520
{
34725
- return sqlite3Atoi64(z, pOut, sqlite3Strlen30(z), SQLITE_UTF8);
35521
+ int n = (int)(0x3fffffff&strspn(z,"+- \n\t0123456789"));
35522
+ if( z[n] ) n++;
35523
+ return sqlite3Atoi64(z, pOut, n, SQLITE_UTF8);
35524
}
35525
}
35526
@@ -34801,6 +35599,153 @@ SQLITE_PRIVATE int sqlite3Atoi(const char *z){
35599
return x;
35600
}
35601
35602
+/*
35603
+** Decode a floating-point value into an approximate decimal
35604
+** representation.
35605
+**
35606
+** Round the decimal representation to n significant digits if
35607
+** n is positive. Or round to -n signficant digits after the
35608
+** decimal point if n is negative. No rounding is performed if
35609
+** n is zero.
35610
+**
35611
+** The significant digits of the decimal representation are
35612
+** stored in p->z[] which is a often (but not always) a pointer
35613
+** into the middle of p->zBuf[]. There are p->n significant digits.
35614
+** The p->z[] array is *not* zero-terminated.
35615
+*/
35616
+SQLITE_PRIVATE void sqlite3FpDecode(FpDecode *p, double r, int iRound, int mxRound){
35617
+ int i;
35618
+ u64 v;
35619
+ int e, exp = 0;
35620
+ p->isSpecial = 0;
35621
+ p->z = p->zBuf;
35622
+
35623
+ /* Convert negative numbers to positive. Deal with Infinity, 0.0, and
35624
+ ** NaN. */
35625
+ if( r<0.0 ){
35626
+ p->sign = '-';
35627
+ r = -r;
35628
+ }else if( r==0.0 ){
35629
+ p->sign = '+';
35630
+ p->n = 1;
35631
+ p->iDP = 1;
35632
+ p->z = "0";
35633
+ return;
35634
+ }else{
35635
+ p->sign = '+';
35636
+ }
35637
+ memcpy(&v,&r,8);
35638
+ e = v>>52;
35639
+ if( (e&0x7ff)==0x7ff ){
35640
+ p->isSpecial = 1 + (v!=0x7ff0000000000000LL);
35641
+ p->n = 0;
35642
+ p->iDP = 0;
35643
+ return;
35644
+ }
35645
+
35646
+ /* Multiply r by powers of ten until it lands somewhere in between
35647
+ ** 1.0e+19 and 1.0e+17.
35648
+ */
35649
+ if( sqlite3Config.bUseLongDouble ){
35650
+ LONGDOUBLE_TYPE rr = r;
35651
+ if( rr>=1.0e+19 ){
35652
+ while( rr>=1.0e+119L ){ exp+=100; rr *= 1.0e-100L; }
35653
+ while( rr>=1.0e+29L ){ exp+=10; rr *= 1.0e-10L; }
35654
+ while( rr>=1.0e+19L ){ exp++; rr *= 1.0e-1L; }
35655
+ }else{
35656
+ while( rr<1.0e-97L ){ exp-=100; rr *= 1.0e+100L; }
35657
+ while( rr<1.0e+07L ){ exp-=10; rr *= 1.0e+10L; }
35658
+ while( rr<1.0e+17L ){ exp--; rr *= 1.0e+1L; }
35659
+ }
35660
+ v = (u64)rr;
35661
+ }else{
35662
+ /* If high-precision floating point is not available using "long double",
35663
+ ** then use Dekker-style double-double computation to increase the
35664
+ ** precision.
35665
+ **
35666
+ ** The error terms on constants like 1.0e+100 computed using the
35667
+ ** decimal extension, for example as follows:
35668
+ **
35669
+ ** SELECT decimal_exp(decimal_sub('1.0e+100',decimal(1.0e+100)));
35670
+ */
35671
+ double rr[2];
35672
+ rr[0] = r;
35673
+ rr[1] = 0.0;
35674
+ if( rr[0]>9.223372036854774784e+18 ){
35675
+ while( rr[0]>9.223372036854774784e+118 ){
35676
+ exp += 100;
35677
+ dekkerMul2(rr, 1.0e-100, -1.99918998026028836196e-117);
35678
+ }
35679
+ while( rr[0]>9.223372036854774784e+28 ){
35680
+ exp += 10;
35681
+ dekkerMul2(rr, 1.0e-10, -3.6432197315497741579e-27);
35682
+ }
35683
+ while( rr[0]>9.223372036854774784e+18 ){
35684
+ exp += 1;
35685
+ dekkerMul2(rr, 1.0e-01, -5.5511151231257827021e-18);
35686
+ }
35687
+ }else{
35688
+ while( rr[0]<9.223372036854774784e-83 ){
35689
+ exp -= 100;
35690
+ dekkerMul2(rr, 1.0e+100, -1.5902891109759918046e+83);
35691
+ }
35692
+ while( rr[0]<9.223372036854774784e+07 ){
35693
+ exp -= 10;
35694
+ dekkerMul2(rr, 1.0e+10, 0.0);
35695
+ }
35696
+ while( rr[0]<9.22337203685477478e+17 ){
35697
+ exp -= 1;
35698
+ dekkerMul2(rr, 1.0e+01, 0.0);
35699
+ }
35700
+ }
35701
+ v = rr[1]<0.0 ? (u64)rr[0]-(u64)(-rr[1]) : (u64)rr[0]+(u64)rr[1];
35702
+ }
35703
+
35704
+
35705
+ /* Extract significant digits. */
35706
+ i = sizeof(p->zBuf)-1;
35707
+ assert( v>0 );
35708
+ while( v ){ p->zBuf[i--] = (v%10) + '0'; v /= 10; }
35709
+ assert( i>=0 && i<sizeof(p->zBuf)-1 );
35710
+ p->n = sizeof(p->zBuf) - 1 - i;
35711
+ assert( p->n>0 );
35712
+ assert( p->n<sizeof(p->zBuf) );
35713
+ p->iDP = p->n + exp;
35714
+ if( iRound<=0 ){
35715
+ iRound = p->iDP - iRound;
35716
+ if( iRound==0 && p->zBuf[i+1]>='5' ){
35717
+ iRound = 1;
35718
+ p->zBuf[i--] = '0';
35719
+ p->n++;
35720
+ p->iDP++;
35721
+ }
35722
+ }
35723
+ if( iRound>0 && (iRound<p->n || p->n>mxRound) ){
35724
+ char *z = &p->zBuf[i+1];
35725
+ if( iRound>mxRound ) iRound = mxRound;
35726
+ p->n = iRound;
35727
+ if( z[iRound]>='5' ){
35728
+ int j = iRound-1;
35729
+ while( 1 /*exit-by-break*/ ){
35730
+ z[j]++;
35731
+ if( z[j]<='9' ) break;
35732
+ z[j] = '0';
35733
+ if( j==0 ){
35734
+ p->z[i--] = '1';
35735
+ p->n++;
35736
+ p->iDP++;
35737
+ break;
35738
+ }else{
35739
+ j--;
35740
+ }
35741
+ }
35742
+ }
35743
+ }
35744
+ p->z = &p->zBuf[i+1];
35745
+ assert( i+p->n < sizeof(p->zBuf) );
35746
+ while( ALWAYS(p->n>0) && p->z[p->n-1]=='0' ){ p->n--; }
35747
+}
35748
+
35749
/*
35750
** Try to convert z into an unsigned 32-bit integer. Return true on
35751
** success and false if there is an error.
@@ -35064,121 +36009,32 @@ SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
36009
** this function assumes the single-byte case has already been handled.
36010
*/
36011
SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
35067
- u32 a,b;
36012
+ u64 v64;
36013
+ u8 n;
36014
35069
- /* The 1-byte case. Overwhelmingly the most common. Handled inline
35070
- ** by the getVarin32() macro */
35071
- a = *p;
35072
- /* a: p0 (unmasked) */
35073
-#ifndef getVarint32
35074
- if (!(a&0x80))
35075
- {
35076
- /* Values between 0 and 127 */
35077
- *v = a;
35078
- return 1;
35079
- }
35080
-#endif
36015
+ /* Assume that the single-byte case has already been handled by
36016
+ ** the getVarint32() macro */
36017
+ assert( (p[0] & 0x80)!=0 );
36018
35082
- /* The 2-byte case */
35083
- p++;
35084
- b = *p;
35085
- /* b: p1 (unmasked) */
35086
- if (!(b&0x80))
35087
- {
35088
- /* Values between 128 and 16383 */
35089
- a &= 0x7f;
35090
- a = a<<7;
35091
- *v = a | b;
36019
+ if( (p[1] & 0x80)==0 ){
36020
+ /* This is the two-byte case */
36021
+ *v = ((p[0]&0x7f)<<7) | p[1];
36022
return 2;
36023
}
35094
-
35095
- /* The 3-byte case */
35096
- p++;
35097
- a = a<<14;
35098
- a |= *p;
35099
- /* a: p0<<14 | p2 (unmasked) */
35100
- if (!(a&0x80))
35101
- {
35102
- /* Values between 16384 and 2097151 */
35103
- a &= (0x7f<<14)|(0x7f);
35104
- b &= 0x7f;
35105
- b = b<<7;
35106
- *v = a | b;
36024
+ if( (p[2] & 0x80)==0 ){
36025
+ /* This is the three-byte case */
36026
+ *v = ((p[0]&0x7f)<<14) | ((p[1]&0x7f)<<7) | p[2];
36027
return 3;
36028
}
35109
-
35110
- /* A 32-bit varint is used to store size information in btrees.
35111
- ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
35112
- ** A 3-byte varint is sufficient, for example, to record the size
35113
- ** of a 1048569-byte BLOB or string.
35114
- **
35115
- ** We only unroll the first 1-, 2-, and 3- byte cases. The very
35116
- ** rare larger cases can be handled by the slower 64-bit varint
35117
- ** routine.
35118
- */
35119
-#if 1
35120
- {
35121
- u64 v64;
35122
- u8 n;
35123
-
35124
- n = sqlite3GetVarint(p-2, &v64);
35125
- assert( n>3 && n<=9 );
35126
- if( (v64 & SQLITE_MAX_U32)!=v64 ){
35127
- *v = 0xffffffff;
35128
- }else{
35129
- *v = (u32)v64;
35130
- }
35131
- return n;
35132
- }
35133
-
35134
-#else
35135
- /* For following code (kept for historical record only) shows an
35136
- ** unrolling for the 3- and 4-byte varint cases. This code is
35137
- ** slightly faster, but it is also larger and much harder to test.
35138
- */
35139
- p++;
35140
- b = b<<14;
35141
- b |= *p;
35142
- /* b: p1<<14 | p3 (unmasked) */
35143
- if (!(b&0x80))
35144
- {
35145
- /* Values between 2097152 and 268435455 */
35146
- b &= (0x7f<<14)|(0x7f);
35147
- a &= (0x7f<<14)|(0x7f);
35148
- a = a<<7;
35149
- *v = a | b;
35150
- return 4;
35151
- }
35152
-
35153
- p++;
35154
- a = a<<14;
35155
- a |= *p;
35156
- /* a: p0<<28 | p2<<14 | p4 (unmasked) */
35157
- if (!(a&0x80))
35158
- {
35159
- /* Values between 268435456 and 34359738367 */
35160
- a &= SLOT_4_2_0;
35161
- b &= SLOT_4_2_0;
35162
- b = b<<7;
35163
- *v = a | b;
35164
- return 5;
35165
- }
35166
-
35167
- /* We can only reach this point when reading a corrupt database
35168
- ** file. In that case we are not in any hurry. Use the (relatively
35169
- ** slow) general-purpose sqlite3GetVarint() routine to extract the
35170
- ** value. */
35171
- {
35172
- u64 v64;
35173
- u8 n;
35174
-
35175
- p -= 4;
35176
- n = sqlite3GetVarint(p, &v64);
35177
- assert( n>5 && n<=9 );
36029
+ /* four or more bytes */
36030
+ n = sqlite3GetVarint(p, &v64);
36031
+ assert( n>3 && n<=9 );
36032
+ if( (v64 & SQLITE_MAX_U32)!=v64 ){
36033
+ *v = 0xffffffff;
36034
+ }else{
36035
*v = (u32)v64;
35179
- return n;
36036
}
35181
-#endif
36037
+ return n;
36038
}
36039
36040
/*
@@ -35329,7 +36185,7 @@ SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
36185
}
36186
36187
/*
35332
-** Attempt to add, substract, or multiply the 64-bit signed value iB against
36188
+** Attempt to add, subtract, or multiply the 64-bit signed value iB against
36189
** the other 64-bit signed integer at *pA and store the result in *pA.
36190
** Return 0 on success. Or if the operation would have resulted in an
36191
** overflow, leave *pA unchanged and return 1.
@@ -35642,7 +36498,7 @@ SQLITE_PRIVATE int sqlite3VListNameToNum(VList *pIn, const char *zName, int nNam
36498
#define SQLITE_HWTIME_H
36499
36500
/*
35645
-** The following routine only works on pentium-class (or newer) processors.
36501
+** The following routine only works on Pentium-class (or newer) processors.
36502
** It uses the RDTSC opcode to read the cycle count value out of the
36503
** processor and returns that value. This can be used for high-res
36504
** profiling.
@@ -35814,7 +36670,7 @@ static void insertElement(
36670
}
36671
36672
35817
-/* Resize the hash table so that it cantains "new_size" buckets.
36673
+/* Resize the hash table so that it contains "new_size" buckets.
36674
**
36675
** The hash table might fail to resize if sqlite3_malloc() fails or
36676
** if the new size is the same as the prior size.
@@ -36174,19 +37030,22 @@ SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
37030
/* 171 */ "VCreate" OpHelp(""),
37031
/* 172 */ "VDestroy" OpHelp(""),
37032
/* 173 */ "VOpen" OpHelp(""),
36177
- /* 174 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
36178
- /* 175 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
36179
- /* 176 */ "VRename" OpHelp(""),
36180
- /* 177 */ "Pagecount" OpHelp(""),
36181
- /* 178 */ "MaxPgcnt" OpHelp(""),
36182
- /* 179 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
36183
- /* 180 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
36184
- /* 181 */ "Trace" OpHelp(""),
36185
- /* 182 */ "CursorHint" OpHelp(""),
36186
- /* 183 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
36187
- /* 184 */ "Noop" OpHelp(""),
36188
- /* 185 */ "Explain" OpHelp(""),
36189
- /* 186 */ "Abortable" OpHelp(""),
37033
+ /* 174 */ "VCheck" OpHelp(""),
37034
+ /* 175 */ "VInitIn" OpHelp("r[P2]=ValueList(P1,P3)"),
37035
+ /* 176 */ "VColumn" OpHelp("r[P3]=vcolumn(P2)"),
37036
+ /* 177 */ "VRename" OpHelp(""),
37037
+ /* 178 */ "Pagecount" OpHelp(""),
37038
+ /* 179 */ "MaxPgcnt" OpHelp(""),
37039
+ /* 180 */ "ClrSubtype" OpHelp("r[P1].subtype = 0"),
37040
+ /* 181 */ "GetSubtype" OpHelp("r[P2] = r[P1].subtype"),
37041
+ /* 182 */ "SetSubtype" OpHelp("r[P2].subtype = r[P1]"),
37042
+ /* 183 */ "FilterAdd" OpHelp("filter(P1) += key(P3@P4)"),
37043
+ /* 184 */ "Trace" OpHelp(""),
37044
+ /* 185 */ "CursorHint" OpHelp(""),
37045
+ /* 186 */ "ReleaseReg" OpHelp("release r[P1@P2] mask P3"),
37046
+ /* 187 */ "Noop" OpHelp(""),
37047
+ /* 188 */ "Explain" OpHelp(""),
37048
+ /* 189 */ "Abortable" OpHelp(""),
37049
};
37050
return azName[i];
37051
}
@@ -37200,7 +38059,7 @@ SQLITE_PRIVATE int sqlite3KvvfsInit(void){
38059
** This source file is organized into divisions where the logic for various
38060
** subfunctions is contained within the appropriate division. PLEASE
38061
** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed
37203
-** in the correct division and should be clearly labeled.
38062
+** in the correct division and should be clearly labelled.
38063
**
38064
** The layout of divisions is as follows:
38065
**
@@ -37787,7 +38646,7 @@ static int robustFchown(int fd, uid_t uid, gid_t gid){
38646
38647
/*
38648
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
37790
-** "unix" VFSes. Return SQLITE_OK opon successfully updating the
38649
+** "unix" VFSes. Return SQLITE_OK upon successfully updating the
38650
** system call pointer, or SQLITE_NOTFOUND if there is no configurable
38651
** system call named zName.
38652
*/
@@ -38309,7 +39168,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){
39168
** If you close a file descriptor that points to a file that has locks,
39169
** all locks on that file that are owned by the current process are
39170
** released. To work around this problem, each unixInodeInfo object
38312
-** maintains a count of the number of pending locks on tha inode.
39171
+** maintains a count of the number of pending locks on the inode.
39172
** When an attempt is made to close an unixFile, if there are
39173
** other unixFile open on the same inode that are holding locks, the call
39174
** to close() the file descriptor is deferred until all of the locks clear.
@@ -38323,7 +39182,7 @@ static void vxworksReleaseFileId(struct vxworksFileId *pId){
39182
** not posix compliant. Under LinuxThreads, a lock created by thread
39183
** A cannot be modified or overridden by a different thread B.
39184
** Only thread A can modify the lock. Locking behavior is correct
38326
-** if the appliation uses the newer Native Posix Thread Library (NPTL)
39185
+** if the application uses the newer Native Posix Thread Library (NPTL)
39186
** on linux - with NPTL a lock created by thread A can override locks
39187
** in thread B. But there is no way to know at compile-time which
39188
** threading library is being used. So there is no way to know at
@@ -38525,7 +39384,7 @@ static void storeLastErrno(unixFile *pFile, int error){
39384
}
39385
39386
/*
38528
-** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
39387
+** Close all file descriptors accumulated in the unixInodeInfo->pUnused list.
39388
*/
39389
static void closePendingFds(unixFile *pFile){
39390
unixInodeInfo *pInode = pFile->pInode;
@@ -38888,7 +39747,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
39747
** slightly in order to be compatible with Windows95 systems simultaneously
39748
** accessing the same database file, in case that is ever required.
39749
**
38891
- ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
39750
+ ** Symbols defined in os.h identify the 'pending byte' and the 'reserved
39751
** byte', each single bytes at well known offsets, and the 'shared byte
39752
** range', a range of 510 bytes at a well known offset.
39753
**
@@ -38896,7 +39755,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
39755
** byte'. If this is successful, 'shared byte range' is read-locked
39756
** and the lock on the 'pending byte' released. (Legacy note: When
39757
** SQLite was first developed, Windows95 systems were still very common,
38899
- ** and Widnows95 lacks a shared-lock capability. So on Windows95, a
39758
+ ** and Windows95 lacks a shared-lock capability. So on Windows95, a
39759
** single randomly selected by from the 'shared byte range' is locked.
39760
** Windows95 is now pretty much extinct, but this work-around for the
39761
** lack of shared-locks on Windows95 lives on, for backwards
@@ -38917,7 +39776,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
39776
** obtaining a write-lock on the 'pending byte'. This ensures that no new
39777
** SHARED locks can be obtained, but existing SHARED locks are allowed to
39778
** persist. If the call to this function fails to obtain the EXCLUSIVE
38920
- ** lock in this case, it holds the PENDING lock intead. The client may
39779
+ ** lock in this case, it holds the PENDING lock instead. The client may
39780
** then re-attempt the EXCLUSIVE lock later on, after existing SHARED
39781
** locks have cleared.
39782
*/
@@ -38945,7 +39804,7 @@ static int unixLock(sqlite3_file *id, int eFileLock){
39804
39805
/* Make sure the locking sequence is correct.
39806
** (1) We never move from unlocked to anything higher than shared lock.
38948
- ** (2) SQLite never explicitly requests a pendig lock.
39807
+ ** (2) SQLite never explicitly requests a pending lock.
39808
** (3) A shared lock is always held when a reserve lock is requested.
39809
*/
39810
assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
@@ -40163,7 +41022,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){
41022
41023
/* Make sure the locking sequence is correct
41024
** (1) We never move from unlocked to anything higher than shared lock.
40166
- ** (2) SQLite never explicitly requests a pendig lock.
41025
+ ** (2) SQLite never explicitly requests a pending lock.
41026
** (3) A shared lock is always held when a reserve lock is requested.
41027
*/
41028
assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
@@ -40279,7 +41138,7 @@ static int afpLock(sqlite3_file *id, int eFileLock){
41138
if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
41139
pInode->sharedByte, 1, 0)) ){
41140
int failed2 = SQLITE_OK;
40282
- /* now attemmpt to get the exclusive lock range */
41141
+ /* now attempt to get the exclusive lock range */
41142
failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
41143
SHARED_SIZE, 1);
41144
if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
@@ -40328,9 +41187,6 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
41187
unixInodeInfo *pInode;
41188
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
41189
int skipShared = 0;
40331
-#ifdef SQLITE_TEST
40332
- int h = pFile->h;
40333
-#endif
41190
41191
assert( pFile );
41192
OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
@@ -40346,9 +41202,6 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
41202
assert( pInode->nShared!=0 );
41203
if( pFile->eFileLock>SHARED_LOCK ){
41204
assert( pInode->eFileLock==pFile->eFileLock );
40349
- SimulateIOErrorBenign(1);
40350
- SimulateIOError( h=(-1) )
40351
- SimulateIOErrorBenign(0);
41205
41206
#ifdef SQLITE_DEBUG
41207
/* When reducing a lock such that other processes can start
@@ -40397,9 +41250,6 @@ static int afpUnlock(sqlite3_file *id, int eFileLock) {
41250
unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
41251
pInode->nShared--;
41252
if( pInode->nShared==0 ){
40400
- SimulateIOErrorBenign(1);
40401
- SimulateIOError( h=(-1) )
40402
- SimulateIOErrorBenign(0);
41253
if( !skipShared ){
41254
rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
41255
}
@@ -40574,7 +41424,7 @@ static int unixRead(
41424
#endif
41425
41426
#if SQLITE_MAX_MMAP_SIZE>0
40577
- /* Deal with as much of this read request as possible by transfering
41427
+ /* Deal with as much of this read request as possible by transferring
41428
** data from the memory mapping using memcpy(). */
41429
if( offset<pFile->mmapSize ){
41430
if( offset+amt <= pFile->mmapSize ){
@@ -40726,7 +41576,7 @@ static int unixWrite(
41576
#endif
41577
41578
#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
40729
- /* Deal with as much of this write request as possible by transfering
41579
+ /* Deal with as much of this write request as possible by transferring
41580
** data from the memory mapping using memcpy(). */
41581
if( offset<pFile->mmapSize ){
41582
if( offset+amt <= pFile->mmapSize ){
@@ -40848,7 +41698,7 @@ static int full_fsync(int fd, int fullSync, int dataOnly){
41698
/* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
41699
** no-op. But go ahead and call fstat() to validate the file
41700
** descriptor as we need a method to provoke a failure during
40851
- ** coverate testing.
41701
+ ** coverage testing.
41702
*/
41703
#ifdef SQLITE_NO_SYNC
41704
{
@@ -41241,7 +42091,13 @@ static int unixFileControl(sqlite3_file *id, int op, void *pArg){
42091
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42092
case SQLITE_FCNTL_LOCK_TIMEOUT: {
42093
int iOld = pFile->iBusyTimeout;
42094
+#if SQLITE_ENABLE_SETLK_TIMEOUT==1
42095
pFile->iBusyTimeout = *(int*)pArg;
42096
+#elif SQLITE_ENABLE_SETLK_TIMEOUT==2
42097
+ pFile->iBusyTimeout = !!(*(int*)pArg);
42098
+#else
42099
+# error "SQLITE_ENABLE_SETLK_TIMEOUT must be set to 1 or 2"
42100
+#endif
42101
*(int*)pArg = iOld;
42102
return SQLITE_OK;
42103
}
@@ -41494,6 +42350,25 @@ static int unixGetpagesize(void){
42350
** Either unixShmNode.pShmMutex must be held or unixShmNode.nRef==0 and
42351
** unixMutexHeld() is true when reading or writing any other field
42352
** in this structure.
42353
+**
42354
+** aLock[SQLITE_SHM_NLOCK]:
42355
+** This array records the various locks held by clients on each of the
42356
+** SQLITE_SHM_NLOCK slots. If the aLock[] entry is set to 0, then no
42357
+** locks are held by the process on this slot. If it is set to -1, then
42358
+** some client holds an EXCLUSIVE lock on the locking slot. If the aLock[]
42359
+** value is set to a positive value, then it is the number of shared
42360
+** locks currently held on the slot.
42361
+**
42362
+** aMutex[SQLITE_SHM_NLOCK]:
42363
+** Normally, when SQLITE_ENABLE_SETLK_TIMEOUT is not defined, mutex
42364
+** pShmMutex is used to protect the aLock[] array and the right to
42365
+** call fcntl() on unixShmNode.hShm to obtain or release locks.
42366
+**
42367
+** If SQLITE_ENABLE_SETLK_TIMEOUT is defined though, we use an array
42368
+** of mutexes - one for each locking slot. To read or write locking
42369
+** slot aLock[iSlot], the caller must hold the corresponding mutex
42370
+** aMutex[iSlot]. Similarly, to call fcntl() to obtain or release a
42371
+** lock corresponding to slot iSlot, mutex aMutex[iSlot] must be held.
42372
*/
42373
struct unixShmNode {
42374
unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */
@@ -41507,10 +42382,11 @@ struct unixShmNode {
42382
char **apRegion; /* Array of mapped shared-memory regions */
42383
int nRef; /* Number of unixShm objects pointing to this */
42384
unixShm *pFirst; /* All unixShm objects pointing to this */
42385
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42386
+ sqlite3_mutex *aMutex[SQLITE_SHM_NLOCK];
42387
+#endif
42388
int aLock[SQLITE_SHM_NLOCK]; /* # shared locks on slot, -1==excl lock */
42389
#ifdef SQLITE_DEBUG
41512
- u8 exclMask; /* Mask of exclusive locks held */
41513
- u8 sharedMask; /* Mask of shared locks held */
42390
u8 nextShmId; /* Next available unixShm.id value */
42391
#endif
42392
};
@@ -41593,16 +42469,35 @@ static int unixShmSystemLock(
42469
struct flock f; /* The posix advisory locking structure */
42470
int rc = SQLITE_OK; /* Result code form fcntl() */
42471
41596
- /* Access to the unixShmNode object is serialized by the caller */
42472
pShmNode = pFile->pInode->pShmNode;
41598
- assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
41599
- assert( pShmNode->nRef>0 || unixMutexHeld() );
42473
+
42474
+ /* Assert that the parameters are within expected range and that the
42475
+ ** correct mutex or mutexes are held. */
42476
+ assert( pShmNode->nRef>=0 );
42477
+ assert( (ofst==UNIX_SHM_DMS && n==1)
42478
+ || (ofst>=UNIX_SHM_BASE && ofst+n<=(UNIX_SHM_BASE+SQLITE_SHM_NLOCK))
42479
+ );
42480
+ if( ofst==UNIX_SHM_DMS ){
42481
+ assert( pShmNode->nRef>0 || unixMutexHeld() );
42482
+ assert( pShmNode->nRef==0 || sqlite3_mutex_held(pShmNode->pShmMutex) );
42483
+ }else{
42484
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42485
+ int ii;
42486
+ for(ii=ofst-UNIX_SHM_BASE; ii<ofst-UNIX_SHM_BASE+n; ii++){
42487
+ assert( sqlite3_mutex_held(pShmNode->aMutex[ii]) );
42488
+ }
42489
+#else
42490
+ assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
42491
+ assert( pShmNode->nRef>0 );
42492
+#endif
42493
+ }
42494
42495
/* Shared locks never span more than one byte */
42496
assert( n==1 || lockType!=F_RDLCK );
42497
42498
/* Locks are within range */
42499
assert( n>=1 && n<=SQLITE_SHM_NLOCK );
42500
+ assert( ofst>=UNIX_SHM_BASE && ofst<=(UNIX_SHM_DMS+SQLITE_SHM_NLOCK) );
42501
42502
if( pShmNode->hShm>=0 ){
42503
int res;
@@ -41613,7 +42508,7 @@ static int unixShmSystemLock(
42508
f.l_len = n;
42509
res = osSetPosixAdvisoryLock(pShmNode->hShm, &f, pFile);
42510
if( res==-1 ){
41616
-#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42511
+#if defined(SQLITE_ENABLE_SETLK_TIMEOUT) && SQLITE_ENABLE_SETLK_TIMEOUT==1
42512
rc = (pFile->iBusyTimeout ? SQLITE_BUSY_TIMEOUT : SQLITE_BUSY);
42513
#else
42514
rc = SQLITE_BUSY;
@@ -41621,39 +42516,28 @@ static int unixShmSystemLock(
42516
}
42517
}
42518
41624
- /* Update the global lock state and do debug tracing */
42519
+ /* Do debug tracing */
42520
#ifdef SQLITE_DEBUG
41626
- { u16 mask;
42521
OSTRACE(("SHM-LOCK "));
41628
- mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
42522
if( rc==SQLITE_OK ){
42523
if( lockType==F_UNLCK ){
41631
- OSTRACE(("unlock %d ok", ofst));
41632
- pShmNode->exclMask &= ~mask;
41633
- pShmNode->sharedMask &= ~mask;
42524
+ OSTRACE(("unlock %d..%d ok\n", ofst, ofst+n-1));
42525
}else if( lockType==F_RDLCK ){
41635
- OSTRACE(("read-lock %d ok", ofst));
41636
- pShmNode->exclMask &= ~mask;
41637
- pShmNode->sharedMask |= mask;
42526
+ OSTRACE(("read-lock %d..%d ok\n", ofst, ofst+n-1));
42527
}else{
42528
assert( lockType==F_WRLCK );
41640
- OSTRACE(("write-lock %d ok", ofst));
41641
- pShmNode->exclMask |= mask;
41642
- pShmNode->sharedMask &= ~mask;
42529
+ OSTRACE(("write-lock %d..%d ok\n", ofst, ofst+n-1));
42530
}
42531
}else{
42532
if( lockType==F_UNLCK ){
41646
- OSTRACE(("unlock %d failed", ofst));
42533
+ OSTRACE(("unlock %d..%d failed\n", ofst, ofst+n-1));
42534
}else if( lockType==F_RDLCK ){
41648
- OSTRACE(("read-lock failed"));
42535
+ OSTRACE(("read-lock %d..%d failed\n", ofst, ofst+n-1));
42536
}else{
42537
assert( lockType==F_WRLCK );
41651
- OSTRACE(("write-lock %d failed", ofst));
42538
+ OSTRACE(("write-lock %d..%d failed\n", ofst, ofst+n-1));
42539
}
42540
}
41654
- OSTRACE((" - afterwards %03x,%03x\n",
41655
- pShmNode->sharedMask, pShmNode->exclMask));
41656
- }
42541
#endif
42542
42543
return rc;
@@ -41690,6 +42574,11 @@ static void unixShmPurge(unixFile *pFd){
42574
int i;
42575
assert( p->pInode==pFd->pInode );
42576
sqlite3_mutex_free(p->pShmMutex);
42577
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42578
+ for(i=0; i<SQLITE_SHM_NLOCK; i++){
42579
+ sqlite3_mutex_free(p->aMutex[i]);
42580
+ }
42581
+#endif
42582
for(i=0; i<p->nRegion; i+=nShmPerMap){
42583
if( p->hShm>=0 ){
42584
osMunmap(p->apRegion[i], p->szRegion);
@@ -41749,7 +42638,20 @@ static int unixLockSharedMemory(unixFile *pDbFd, unixShmNode *pShmNode){
42638
pShmNode->isUnlocked = 1;
42639
rc = SQLITE_READONLY_CANTINIT;
42640
}else{
42641
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42642
+ /* Do not use a blocking lock here. If the lock cannot be obtained
42643
+ ** immediately, it means some other connection is truncating the
42644
+ ** *-shm file. And after it has done so, it will not release its
42645
+ ** lock, but only downgrade it to a shared lock. So no point in
42646
+ ** blocking here. The call below to obtain the shared DMS lock may
42647
+ ** use a blocking lock. */
42648
+ int iSaveTimeout = pDbFd->iBusyTimeout;
42649
+ pDbFd->iBusyTimeout = 0;
42650
+#endif
42651
rc = unixShmSystemLock(pDbFd, F_WRLCK, UNIX_SHM_DMS, 1);
42652
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42653
+ pDbFd->iBusyTimeout = iSaveTimeout;
42654
+#endif
42655
/* The first connection to attach must truncate the -shm file. We
42656
** truncate to 3 bytes (an arbitrary small number, less than the
42657
** -shm header size) rather than 0 as a system debugging aid, to
@@ -41870,6 +42772,18 @@ static int unixOpenSharedMemory(unixFile *pDbFd){
42772
rc = SQLITE_NOMEM_BKPT;
42773
goto shm_open_err;
42774
}
42775
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42776
+ {
42777
+ int ii;
42778
+ for(ii=0; ii<SQLITE_SHM_NLOCK; ii++){
42779
+ pShmNode->aMutex[ii] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
42780
+ if( pShmNode->aMutex[ii]==0 ){
42781
+ rc = SQLITE_NOMEM_BKPT;
42782
+ goto shm_open_err;
42783
+ }
42784
+ }
42785
+ }
42786
+#endif
42787
}
42788
42789
if( pInode->bProcessLock==0 ){
@@ -42091,9 +43005,11 @@ shmpage_out:
43005
*/
43006
#ifdef SQLITE_DEBUG
43007
static int assertLockingArrayOk(unixShmNode *pShmNode){
43008
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43009
+ return 1;
43010
+#else
43011
unixShm *pX;
43012
int aLock[SQLITE_SHM_NLOCK];
42096
- assert( sqlite3_mutex_held(pShmNode->pShmMutex) );
43013
43014
memset(aLock, 0, sizeof(aLock));
43015
for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
@@ -42111,13 +43027,14 @@ static int assertLockingArrayOk(unixShmNode *pShmNode){
43027
43028
assert( 0==memcmp(pShmNode->aLock, aLock, sizeof(aLock)) );
43029
return (memcmp(pShmNode->aLock, aLock, sizeof(aLock))==0);
43030
+#endif
43031
}
43032
#endif
43033
43034
/*
43035
** Change the lock state for a shared-memory segment.
43036
**
42120
-** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
43037
+** Note that the relationship between SHARED and EXCLUSIVE locks is a little
43038
** different here than in posix. In xShmLock(), one can go from unlocked
43039
** to shared and back or from unlocked to exclusive and back. But one may
43040
** not go from shared to exclusive or from exclusive to shared.
@@ -42132,7 +43049,7 @@ static int unixShmLock(
43049
unixShm *p; /* The shared memory being locked */
43050
unixShmNode *pShmNode; /* The underlying file iNode */
43051
int rc = SQLITE_OK; /* Result code */
42135
- u16 mask; /* Mask of locks to take or release */
43052
+ u16 mask = (1<<(ofst+n)) - (1<<ofst); /* Mask of locks to take or release */
43053
int *aLock;
43054
43055
p = pDbFd->pShm;
@@ -42167,88 +43084,151 @@ static int unixShmLock(
43084
** It is not permitted to block on the RECOVER lock.
43085
*/
43086
#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
42170
- assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
42171
- (ofst!=2) /* not RECOVER */
42172
- && (ofst!=1 || (p->exclMask|p->sharedMask)==0)
42173
- && (ofst!=0 || (p->exclMask|p->sharedMask)<3)
42174
- && (ofst<3 || (p->exclMask|p->sharedMask)<(1<<ofst))
42175
- ));
43087
+ {
43088
+ u16 lockMask = (p->exclMask|p->sharedMask);
43089
+ assert( (flags & SQLITE_SHM_UNLOCK) || pDbFd->iBusyTimeout==0 || (
43090
+ (ofst!=2) /* not RECOVER */
43091
+ && (ofst!=1 || lockMask==0 || lockMask==2)
43092
+ && (ofst!=0 || lockMask<3)
43093
+ && (ofst<3 || lockMask<(1<<ofst))
43094
+ ));
43095
+ }
43096
#endif
43097
42178
- mask = (1<<(ofst+n)) - (1<<ofst);
42179
- assert( n>1 || mask==(1<<ofst) );
42180
- sqlite3_mutex_enter(pShmNode->pShmMutex);
42181
- assert( assertLockingArrayOk(pShmNode) );
42182
- if( flags & SQLITE_SHM_UNLOCK ){
42183
- if( (p->exclMask|p->sharedMask) & mask ){
42184
- int ii;
42185
- int bUnlock = 1;
43098
+ /* Check if there is any work to do. There are three cases:
43099
+ **
43100
+ ** a) An unlock operation where there are locks to unlock,
43101
+ ** b) An shared lock where the requested lock is not already held
43102
+ ** c) An exclusive lock where the requested lock is not already held
43103
+ **
43104
+ ** The SQLite core never requests an exclusive lock that it already holds.
43105
+ ** This is assert()ed below.
43106
+ */
43107
+ assert( flags!=(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK)
43108
+ || 0==(p->exclMask & mask)
43109
+ );
43110
+ if( ((flags & SQLITE_SHM_UNLOCK) && ((p->exclMask|p->sharedMask) & mask))
43111
+ || (flags==(SQLITE_SHM_SHARED|SQLITE_SHM_LOCK) && 0==(p->sharedMask & mask))
43112
+ || (flags==(SQLITE_SHM_EXCLUSIVE|SQLITE_SHM_LOCK))
43113
+ ){
43114
42187
- for(ii=ofst; ii<ofst+n; ii++){
42188
- if( aLock[ii]>((p->sharedMask & (1<<ii)) ? 1 : 0) ){
42189
- bUnlock = 0;
42190
- }
43115
+ /* Take the required mutexes. In SETLK_TIMEOUT mode (blocking locks), if
43116
+ ** this is an attempt on an exclusive lock use sqlite3_mutex_try(). If any
43117
+ ** other thread is holding this mutex, then it is either holding or about
43118
+ ** to hold a lock exclusive to the one being requested, and we may
43119
+ ** therefore return SQLITE_BUSY to the caller.
43120
+ **
43121
+ ** Doing this prevents some deadlock scenarios. For example, thread 1 may
43122
+ ** be a checkpointer blocked waiting on the WRITER lock. And thread 2
43123
+ ** may be a normal SQL client upgrading to a write transaction. In this
43124
+ ** case thread 2 does a non-blocking request for the WRITER lock. But -
43125
+ ** if it were to use sqlite3_mutex_enter() then it would effectively
43126
+ ** become a (doomed) blocking request, as thread 2 would block until thread
43127
+ ** 1 obtained WRITER and released the mutex. Since thread 2 already holds
43128
+ ** a lock on a read-locking slot at this point, this breaks the
43129
+ ** anti-deadlock rules (see above). */
43130
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43131
+ int iMutex;
43132
+ for(iMutex=ofst; iMutex<ofst+n; iMutex++){
43133
+ if( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) ){
43134
+ rc = sqlite3_mutex_try(pShmNode->aMutex[iMutex]);
43135
+ if( rc!=SQLITE_OK ) goto leave_shmnode_mutexes;
43136
+ }else{
43137
+ sqlite3_mutex_enter(pShmNode->aMutex[iMutex]);
43138
}
43139
+ }
43140
+#else
43141
+ sqlite3_mutex_enter(pShmNode->pShmMutex);
43142
+#endif
43143
42193
- if( bUnlock ){
42194
- rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
42195
- if( rc==SQLITE_OK ){
42196
- memset(&aLock[ofst], 0, sizeof(int)*n);
43144
+ if( ALWAYS(rc==SQLITE_OK) ){
43145
+ if( flags & SQLITE_SHM_UNLOCK ){
43146
+ /* Case (a) - unlock. */
43147
+ int bUnlock = 1;
43148
+ assert( (p->exclMask & p->sharedMask)==0 );
43149
+ assert( !(flags & SQLITE_SHM_EXCLUSIVE) || (p->exclMask & mask)==mask );
43150
+ assert( !(flags & SQLITE_SHM_SHARED) || (p->sharedMask & mask)==mask );
43151
+
43152
+ /* If this is a SHARED lock being unlocked, it is possible that other
43153
+ ** clients within this process are holding the same SHARED lock. In
43154
+ ** this case, set bUnlock to 0 so that the posix lock is not removed
43155
+ ** from the file-descriptor below. */
43156
+ if( flags & SQLITE_SHM_SHARED ){
43157
+ assert( n==1 );
43158
+ assert( aLock[ofst]>=1 );
43159
+ if( aLock[ofst]>1 ){
43160
+ bUnlock = 0;
43161
+ aLock[ofst]--;
43162
+ p->sharedMask &= ~mask;
43163
+ }
43164
}
42198
- }else if( ALWAYS(p->sharedMask & (1<<ofst)) ){
42199
- assert( n==1 && aLock[ofst]>1 );
42200
- aLock[ofst]--;
42201
- }
43165
42203
- /* Undo the local locks */
42204
- if( rc==SQLITE_OK ){
42205
- p->exclMask &= ~mask;
42206
- p->sharedMask &= ~mask;
42207
- }
42208
- }
42209
- }else if( flags & SQLITE_SHM_SHARED ){
42210
- assert( n==1 );
42211
- assert( (p->exclMask & (1<<ofst))==0 );
42212
- if( (p->sharedMask & mask)==0 ){
42213
- if( aLock[ofst]<0 ){
42214
- rc = SQLITE_BUSY;
42215
- }else if( aLock[ofst]==0 ){
42216
- rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
42217
- }
43166
+ if( bUnlock ){
43167
+ rc = unixShmSystemLock(pDbFd, F_UNLCK, ofst+UNIX_SHM_BASE, n);
43168
+ if( rc==SQLITE_OK ){
43169
+ memset(&aLock[ofst], 0, sizeof(int)*n);
43170
+ p->sharedMask &= ~mask;
43171
+ p->exclMask &= ~mask;
43172
+ }
43173
+ }
43174
+ }else if( flags & SQLITE_SHM_SHARED ){
43175
+ /* Case (b) - a shared lock. */
43176
42219
- /* Get the local shared locks */
42220
- if( rc==SQLITE_OK ){
42221
- p->sharedMask |= mask;
42222
- aLock[ofst]++;
42223
- }
42224
- }
42225
- }else{
42226
- /* Make sure no sibling connections hold locks that will block this
42227
- ** lock. If any do, return SQLITE_BUSY right away. */
42228
- int ii;
42229
- for(ii=ofst; ii<ofst+n; ii++){
42230
- assert( (p->sharedMask & mask)==0 );
42231
- if( ALWAYS((p->exclMask & (1<<ii))==0) && aLock[ii] ){
42232
- rc = SQLITE_BUSY;
42233
- break;
42234
- }
42235
- }
43177
+ if( aLock[ofst]<0 ){
43178
+ /* An exclusive lock is held by some other connection. BUSY. */
43179
+ rc = SQLITE_BUSY;
43180
+ }else if( aLock[ofst]==0 ){
43181
+ rc = unixShmSystemLock(pDbFd, F_RDLCK, ofst+UNIX_SHM_BASE, n);
43182
+ }
43183
42237
- /* Get the exclusive locks at the system level. Then if successful
42238
- ** also update the in-memory values. */
42239
- if( rc==SQLITE_OK ){
42240
- rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
42241
- if( rc==SQLITE_OK ){
43184
+ /* Get the local shared locks */
43185
+ if( rc==SQLITE_OK ){
43186
+ p->sharedMask |= mask;
43187
+ aLock[ofst]++;
43188
+ }
43189
+ }else{
43190
+ /* Case (c) - an exclusive lock. */
43191
+ int ii;
43192
+
43193
+ assert( flags==(SQLITE_SHM_LOCK|SQLITE_SHM_EXCLUSIVE) );
43194
assert( (p->sharedMask & mask)==0 );
42243
- p->exclMask |= mask;
43195
+ assert( (p->exclMask & mask)==0 );
43196
+
43197
+ /* Make sure no sibling connections hold locks that will block this
43198
+ ** lock. If any do, return SQLITE_BUSY right away. */
43199
for(ii=ofst; ii<ofst+n; ii++){
42245
- aLock[ii] = -1;
43200
+ if( aLock[ii] ){
43201
+ rc = SQLITE_BUSY;
43202
+ break;
43203
+ }
43204
+ }
43205
+
43206
+ /* Get the exclusive locks at the system level. Then if successful
43207
+ ** also update the in-memory values. */
43208
+ if( rc==SQLITE_OK ){
43209
+ rc = unixShmSystemLock(pDbFd, F_WRLCK, ofst+UNIX_SHM_BASE, n);
43210
+ if( rc==SQLITE_OK ){
43211
+ p->exclMask |= mask;
43212
+ for(ii=ofst; ii<ofst+n; ii++){
43213
+ aLock[ii] = -1;
43214
+ }
43215
+ }
43216
}
43217
}
43218
+ assert( assertLockingArrayOk(pShmNode) );
43219
+ }
43220
+
43221
+ /* Drop the mutexes acquired above. */
43222
+#ifdef SQLITE_ENABLE_SETLK_TIMEOUT
43223
+ leave_shmnode_mutexes:
43224
+ for(iMutex--; iMutex>=ofst; iMutex--){
43225
+ sqlite3_mutex_leave(pShmNode->aMutex[iMutex]);
43226
}
43227
+#else
43228
+ sqlite3_mutex_leave(pShmNode->pShmMutex);
43229
+#endif
43230
}
42250
- assert( assertLockingArrayOk(pShmNode) );
42251
- sqlite3_mutex_leave(pShmNode->pShmMutex);
43231
+
43232
OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
43233
p->id, osGetpid(0), p->sharedMask, p->exclMask));
43234
return rc;
@@ -42498,11 +43478,16 @@ static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
43478
43479
#if SQLITE_MAX_MMAP_SIZE>0
43480
if( pFd->mmapSizeMax>0 ){
43481
+ /* Ensure that there is always at least a 256 byte buffer of addressable
43482
+ ** memory following the returned page. If the database is corrupt,
43483
+ ** SQLite may overread the page slightly (in practice only a few bytes,
43484
+ ** but 256 is safe, round, number). */
43485
+ const int nEofBuffer = 256;
43486
if( pFd->pMapRegion==0 ){
43487
int rc = unixMapfile(pFd, -1);
43488
if( rc!=SQLITE_OK ) return rc;
43489
}
42505
- if( pFd->mmapSize >= iOff+nAmt ){
43490
+ if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
43491
*pp = &((u8 *)pFd->pMapRegion)[iOff];
43492
pFd->nFetchOut++;
43493
}
@@ -43893,12 +44878,17 @@ static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
44878
** than the argument.
44879
*/
44880
static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
43896
-#if OS_VXWORKS || _POSIX_C_SOURCE >= 199309L
44881
+#if !defined(HAVE_NANOSLEEP) || HAVE_NANOSLEEP+0
44882
struct timespec sp;
43898
-
44883
sp.tv_sec = microseconds / 1000000;
44884
sp.tv_nsec = (microseconds % 1000000) * 1000;
44885
+
44886
+ /* Almost all modern unix systems support nanosleep(). But if you are
44887
+ ** compiling for one of the rare exceptions, you can use
44888
+ ** -DHAVE_NANOSLEEP=0 (perhaps in conjuction with -DHAVE_USLEEP if
44889
+ ** usleep() is available) in order to bypass the use of nanosleep() */
44890
nanosleep(&sp, NULL);
44891
+
44892
UNUSED_PARAMETER(NotUsed);
44893
return microseconds;
44894
#elif defined(HAVE_USLEEP) && HAVE_USLEEP
@@ -46488,7 +47478,7 @@ static struct win_syscall {
47478
47479
/*
47480
** This is the xSetSystemCall() method of sqlite3_vfs for all of the
46491
-** "win32" VFSes. Return SQLITE_OK opon successfully updating the
47481
+** "win32" VFSes. Return SQLITE_OK upon successfully updating the
47482
** system call pointer, or SQLITE_NOTFOUND if there is no configurable
47483
** system call named zName.
47484
*/
@@ -48068,7 +49058,7 @@ static int winRead(
49058
pFile->h, pBuf, amt, offset, pFile->locktype));
49059
49060
#if SQLITE_MAX_MMAP_SIZE>0
48071
- /* Deal with as much of this read request as possible by transfering
49061
+ /* Deal with as much of this read request as possible by transferring
49062
** data from the memory mapping using memcpy(). */
49063
if( offset<pFile->mmapSize ){
49064
if( offset+amt <= pFile->mmapSize ){
@@ -48146,7 +49136,7 @@ static int winWrite(
49136
pFile->h, pBuf, amt, offset, pFile->locktype));
49137
49138
#if defined(SQLITE_MMAP_READWRITE) && SQLITE_MAX_MMAP_SIZE>0
48149
- /* Deal with as much of this write request as possible by transfering
49139
+ /* Deal with as much of this write request as possible by transferring
49140
** data from the memory mapping using memcpy(). */
49141
if( offset<pFile->mmapSize ){
49142
if( offset+amt <= pFile->mmapSize ){
@@ -48256,7 +49246,7 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
49246
** all references to memory-mapped content are closed. That is doable,
49247
** but involves adding a few branches in the common write code path which
49248
** could slow down normal operations slightly. Hence, we have decided for
48259
- ** now to simply make trancations a no-op if there are pending reads. We
49249
+ ** now to simply make transactions a no-op if there are pending reads. We
49250
** can maybe revisit this decision in the future.
49251
*/
49252
return SQLITE_OK;
@@ -48315,7 +49305,7 @@ static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
49305
#ifdef SQLITE_TEST
49306
/*
49307
** Count the number of fullsyncs and normal syncs. This is used to test
48318
-** that syncs and fullsyncs are occuring at the right times.
49308
+** that syncs and fullsyncs are occurring at the right times.
49309
*/
49310
SQLITE_API int sqlite3_sync_count = 0;
49311
SQLITE_API int sqlite3_fullsync_count = 0;
@@ -48672,7 +49662,7 @@ static int winLock(sqlite3_file *id, int locktype){
49662
*/
49663
if( locktype==EXCLUSIVE_LOCK && res ){
49664
assert( pFile->locktype>=SHARED_LOCK );
48675
- res = winUnlockReadLock(pFile);
49665
+ (void)winUnlockReadLock(pFile);
49666
res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
49667
SHARED_SIZE, 0);
49668
if( res ){
@@ -49850,6 +50840,11 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
50840
50841
#if SQLITE_MAX_MMAP_SIZE>0
50842
if( pFd->mmapSizeMax>0 ){
50843
+ /* Ensure that there is always at least a 256 byte buffer of addressable
50844
+ ** memory following the returned page. If the database is corrupt,
50845
+ ** SQLite may overread the page slightly (in practice only a few bytes,
50846
+ ** but 256 is safe, round, number). */
50847
+ const int nEofBuffer = 256;
50848
if( pFd->pMapRegion==0 ){
50849
int rc = winMapfile(pFd, -1);
50850
if( rc!=SQLITE_OK ){
@@ -49858,7 +50853,7 @@ static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
50853
return rc;
50854
}
50855
}
49861
- if( pFd->mmapSize >= iOff+nAmt ){
50856
+ if( pFd->mmapSize >= (iOff+nAmt+nEofBuffer) ){
50857
assert( pFd->pMapRegion!=0 );
50858
*pp = &((u8 *)pFd->pMapRegion)[iOff];
50859
pFd->nFetchOut++;
@@ -50076,6 +51071,7 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
51071
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
51072
"0123456789";
51073
size_t i, j;
51074
+ DWORD pid;
51075
int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
51076
int nMax, nBuf, nDir, nLen;
51077
char *zBuf;
@@ -50288,7 +51284,10 @@ static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
51284
51285
j = sqlite3Strlen30(zBuf);
51286
sqlite3_randomness(15, &zBuf[j]);
51287
+ pid = osGetCurrentProcessId();
51288
for(i=0; i<15; i++, j++){
51289
+ zBuf[j] += pid & 0xff;
51290
+ pid >>= 8;
51291
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
51292
}
51293
zBuf[j] = 0;
@@ -52333,6 +53332,14 @@ SQLITE_API unsigned char *sqlite3_serialize(
53332
pOut = 0;
53333
}else{
53334
sz = sqlite3_column_int64(pStmt, 0)*szPage;
53335
+ if( sz==0 ){
53336
+ sqlite3_reset(pStmt);
53337
+ sqlite3_exec(db, "BEGIN IMMEDIATE; COMMIT;", 0, 0, 0);
53338
+ rc = sqlite3_step(pStmt);
53339
+ if( rc==SQLITE_ROW ){
53340
+ sz = sqlite3_column_int64(pStmt, 0)*szPage;
53341
+ }
53342
+ }
53343
if( piSize ) *piSize = sz;
53344
if( mFlags & SQLITE_SERIALIZE_NOCOPY ){
53345
pOut = 0;
@@ -52653,7 +53660,7 @@ SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
53660
h = BITVEC_HASH(i++);
53661
/* if there wasn't a hash collision, and this doesn't */
53662
/* completely fill the hash, then just add it without */
52656
- /* worring about sub-dividing and re-hashing. */
53663
+ /* worrying about sub-dividing and re-hashing. */
53664
if( !p->u.aHash[h] ){
53665
if (p->nSet<(BITVEC_NINT-1)) {
53666
goto bitvec_set_end;
@@ -52986,7 +53993,7 @@ struct PCache {
53993
** Return 1 if pPg is on the dirty list for pCache. Return 0 if not.
53994
** This routine runs inside of assert() statements only.
53995
*/
52989
-#ifdef SQLITE_DEBUG
53996
+#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
53997
static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
53998
PgHdr *p;
53999
for(p=pCache->pDirty; p; p=p->pDirtyNext){
@@ -52994,6 +54001,16 @@ static int pageOnDirtyList(PCache *pCache, PgHdr *pPg){
54001
}
54002
return 0;
54003
}
54004
+static int pageNotOnDirtyList(PCache *pCache, PgHdr *pPg){
54005
+ PgHdr *p;
54006
+ for(p=pCache->pDirty; p; p=p->pDirtyNext){
54007
+ if( p==pPg ) return 0;
54008
+ }
54009
+ return 1;
54010
+}
54011
+#else
54012
+# define pageOnDirtyList(A,B) 1
54013
+# define pageNotOnDirtyList(A,B) 1
54014
#endif
54015
54016
/*
@@ -53014,7 +54031,7 @@ SQLITE_PRIVATE int sqlite3PcachePageSanity(PgHdr *pPg){
54031
assert( pCache!=0 ); /* Every page has an associated PCache */
54032
if( pPg->flags & PGHDR_CLEAN ){
54033
assert( (pPg->flags & PGHDR_DIRTY)==0 );/* Cannot be both CLEAN and DIRTY */
53017
- assert( !pageOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirty list */
54034
+ assert( pageNotOnDirtyList(pCache, pPg) );/* CLEAN pages not on dirtylist */
54035
}else{
54036
assert( (pPg->flags & PGHDR_DIRTY)!=0 );/* If not CLEAN must be DIRTY */
54037
assert( pPg->pDirtyNext==0 || pPg->pDirtyNext->pDirtyPrev==pPg );
@@ -53150,7 +54167,7 @@ static int numberOfCachePages(PCache *p){
54167
return p->szCache;
54168
}else{
54169
i64 n;
53153
- /* IMPLEMANTATION-OF: R-59858-46238 If the argument N is negative, then the
54170
+ /* IMPLEMENTATION-OF: R-59858-46238 If the argument N is negative, then the
54171
** number of cache pages is adjusted to be a number of pages that would
54172
** use approximately abs(N*1024) bytes of memory based on the current
54173
** page size. */
@@ -53638,7 +54655,7 @@ static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
54655
}
54656
54657
/*
53641
-** Sort the list of pages in accending order by pgno. Pages are
54658
+** Sort the list of pages in ascending order by pgno. Pages are
54659
** connected by pDirty pointers. The pDirtyPrev pointers are
54660
** corrupted by this sort.
54661
**
@@ -53878,7 +54895,7 @@ SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHd
54895
** If N is positive, then N pages worth of memory are allocated using a single
54896
** sqlite3Malloc() call and that memory is used for the first N pages allocated.
54897
** Or if N is negative, then -1024*N bytes of memory are allocated and used
53881
-** for as many pages as can be accomodated.
54898
+** for as many pages as can be accommodated.
54899
**
54900
** Only one of (2) or (3) can be used. Once the memory available to (2) or
54901
** (3) is exhausted, subsequent allocations fail over to the general-purpose
@@ -53912,7 +54929,7 @@ typedef struct PGroup PGroup;
54929
** in memory directly after the associated page data, if the database is
54930
** corrupt, code at the b-tree layer may overread the page buffer and
54931
** read part of this structure before the corruption is detected. This
53915
-** can cause a valgrind error if the unitialized gap is accessed. Using u16
54932
+** can cause a valgrind error if the uninitialized gap is accessed. Using u16
54933
** ensures there is no such gap, and therefore no bytes of uninitialized
54934
** memory in the structure.
54935
**
@@ -55132,7 +56149,7 @@ SQLITE_PRIVATE void sqlite3PcacheStats(
56149
** The TEST primitive includes a "batch" number. The TEST primitive
56150
** will only see elements that were inserted before the last change
56151
** in the batch number. In other words, if an INSERT occurs between
55135
-** two TESTs where the TESTs have the same batch nubmer, then the
56152
+** two TESTs where the TESTs have the same batch number, then the
56153
** value added by the INSERT will not be visible to the second TEST.
56154
** The initial batch number is zero, so if the very first TEST contains
56155
** a non-zero batch number, it will see all prior INSERTs.
@@ -55664,6 +56681,7 @@ SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, int iBatch, sqlite3_int64
56681
# define sqlite3WalFramesize(z) 0
56682
# define sqlite3WalFindFrame(x,y,z) 0
56683
# define sqlite3WalFile(x) 0
56684
+# undef SQLITE_USE_SEH
56685
#else
56686
56687
#define WAL_SAVEPOINT_NDATA 4
@@ -55770,6 +56788,10 @@ SQLITE_PRIVATE int sqlite3WalWriteLock(Wal *pWal, int bLock);
56788
SQLITE_PRIVATE void sqlite3WalDb(Wal *pWal, sqlite3 *db);
56789
#endif
56790
56791
+#ifdef SQLITE_USE_SEH
56792
+SQLITE_PRIVATE int sqlite3WalSystemErrno(Wal*);
56793
+#endif
56794
+
56795
#endif /* ifndef SQLITE_OMIT_WAL */
56796
#endif /* SQLITE_WAL_H */
56797
@@ -56055,7 +57077,7 @@ int sqlite3PagerTrace=1; /* True to enable tracing */
57077
** outstanding transactions have been abandoned, the pager is able to
57078
** transition back to OPEN state, discarding the contents of the
57079
** page-cache and any other in-memory state at the same time. Everything
56058
-** is reloaded from disk (and, if necessary, hot-journal rollback peformed)
57080
+** is reloaded from disk (and, if necessary, hot-journal rollback performed)
57081
** when a read-transaction is next opened on the pager (transitioning
57082
** the pager into READER state). At that point the system has recovered
57083
** from the error.
@@ -56442,7 +57464,7 @@ struct Pager {
57464
char *zJournal; /* Name of the journal file */
57465
int (*xBusyHandler)(void*); /* Function to call when busy */
57466
void *pBusyHandlerArg; /* Context argument for xBusyHandler */
56445
- int aStat[4]; /* Total cache hits, misses, writes, spills */
57467
+ u32 aStat[4]; /* Total cache hits, misses, writes, spills */
57468
#ifdef SQLITE_TEST
57469
int nRead; /* Database pages read */
57470
#endif
@@ -56572,9 +57594,8 @@ SQLITE_PRIVATE int sqlite3PagerDirectReadOk(Pager *pPager, Pgno pgno){
57594
#ifndef SQLITE_OMIT_WAL
57595
if( pPager->pWal ){
57596
u32 iRead = 0;
56575
- int rc;
56576
- rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
56577
- return (rc==SQLITE_OK && iRead==0);
57597
+ (void)sqlite3WalFindFrame(pPager->pWal, pgno, &iRead);
57598
+ return iRead==0;
57599
}
57600
#endif
57601
return 1;
@@ -57246,9 +58267,32 @@ static int writeJournalHdr(Pager *pPager){
58267
memset(zHeader, 0, sizeof(aJournalMagic)+4);
58268
}
58269
58270
+
58271
+
58272
/* The random check-hash initializer */
57250
- sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58273
+ if( pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
58274
+ sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
58275
+ }
58276
+#ifdef SQLITE_DEBUG
58277
+ else{
58278
+ /* The Pager.cksumInit variable is usually randomized above to protect
58279
+ ** against there being existing records in the journal file. This is
58280
+ ** dangerous, as following a crash they may be mistaken for records
58281
+ ** written by the current transaction and rolled back into the database
58282
+ ** file, causing corruption. The following assert statements verify
58283
+ ** that this is not required in "journal_mode=memory" mode, as in that
58284
+ ** case the journal file is always 0 bytes in size at this point.
58285
+ ** It is advantageous to avoid the sqlite3_randomness() call if possible
58286
+ ** as it takes the global PRNG mutex. */
58287
+ i64 sz = 0;
58288
+ sqlite3OsFileSize(pPager->jfd, &sz);
58289
+ assert( sz==0 );
58290
+ assert( pPager->journalOff==journalHdrOffset(pPager) );
58291
+ assert( sqlite3JournalIsInMemory(pPager->jfd) );
58292
+ }
58293
+#endif
58294
put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
58295
+
58296
/* The initial database size */
58297
put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
58298
/* The assumed sector size for this process */
@@ -57428,7 +58472,7 @@ static int readJournalHdr(
58472
** + 4 bytes: super-journal name checksum.
58473
** + 8 bytes: aJournalMagic[].
58474
**
57431
-** The super-journal page checksum is the sum of the bytes in thesuper-journal
58475
+** The super-journal page checksum is the sum of the bytes in the super-journal
58476
** name, where each byte is interpreted as a signed 8-bit integer.
58477
**
58478
** If zSuper is a NULL pointer (occurs for a single database transaction),
@@ -57481,7 +58525,7 @@ static int writeSuperJournal(Pager *pPager, const char *zSuper){
58525
}
58526
pPager->journalOff += (nSuper+20);
58527
57484
- /* If the pager is in peristent-journal mode, then the physical
58528
+ /* If the pager is in persistent-journal mode, then the physical
58529
** journal-file may extend past the end of the super-journal name
58530
** and 8 bytes of magic data just written to the file. This is
58531
** dangerous because the code to rollback a hot-journal file
@@ -57651,7 +58695,7 @@ static void pager_unlock(Pager *pPager){
58695
58696
/*
58697
** This function is called whenever an IOERR or FULL error that requires
57654
-** the pager to transition into the ERROR state may ahve occurred.
58698
+** the pager to transition into the ERROR state may have occurred.
58699
** The first argument is a pointer to the pager structure, the second
58700
** the error-code about to be returned by a pager API function. The
58701
** value returned is a copy of the second argument to this function.
@@ -57892,6 +58936,9 @@ static int pager_end_transaction(Pager *pPager, int hasSuper, int bCommit){
58936
return (rc==SQLITE_OK?rc2:rc);
58937
}
58938
58939
+/* Forward reference */
58940
+static int pager_playback(Pager *pPager, int isHot);
58941
+
58942
/*
58943
** Execute a rollback if a transaction is active and unlock the
58944
** database file.
@@ -57920,13 +58967,28 @@ static void pagerUnlockAndRollback(Pager *pPager){
58967
assert( pPager->eState==PAGER_READER );
58968
pager_end_transaction(pPager, 0, 0);
58969
}
58970
+ }else if( pPager->eState==PAGER_ERROR
58971
+ && pPager->journalMode==PAGER_JOURNALMODE_MEMORY
58972
+ && isOpen(pPager->jfd)
58973
+ ){
58974
+ /* Special case for a ROLLBACK due to I/O error with an in-memory
58975
+ ** journal: We have to rollback immediately, before the journal is
58976
+ ** closed, because once it is closed, all content is forgotten. */
58977
+ int errCode = pPager->errCode;
58978
+ u8 eLock = pPager->eLock;
58979
+ pPager->eState = PAGER_OPEN;
58980
+ pPager->errCode = SQLITE_OK;
58981
+ pPager->eLock = EXCLUSIVE_LOCK;
58982
+ pager_playback(pPager, 1);
58983
+ pPager->errCode = errCode;
58984
+ pPager->eLock = eLock;
58985
}
58986
pager_unlock(pPager);
58987
}
58988
58989
/*
58990
** Parameter aData must point to a buffer of pPager->pageSize bytes
57929
-** of data. Compute and return a checksum based ont the contents of the
58991
+** of data. Compute and return a checksum based on the contents of the
58992
** page of data and the current value of pPager->cksumInit.
58993
**
58994
** This is not a real checksum. It is really just the sum of the
@@ -58892,7 +59954,7 @@ static int pagerWalFrames(
59954
assert( pPager->pWal );
59955
assert( pList );
59956
#ifdef SQLITE_DEBUG
58895
- /* Verify that the page list is in accending order */
59957
+ /* Verify that the page list is in ascending order */
59958
for(p=pList; p && p->pDirty; p=p->pDirty){
59959
assert( p->pgno < p->pDirty->pgno );
59960
}
@@ -59023,7 +60085,7 @@ static int pagerPagecount(Pager *pPager, Pgno *pnPage){
60085
#ifndef SQLITE_OMIT_WAL
60086
/*
60087
** Check if the *-wal file that corresponds to the database opened by pPager
59026
-** exists if the database is not empy, or verify that the *-wal file does
60088
+** exists if the database is not empty, or verify that the *-wal file does
60089
** not exist (by deleting it) if the database file is empty.
60090
**
60091
** If the database is not empty and the *-wal file exists, open the pager
@@ -60433,11 +61495,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
61495
int rc = SQLITE_OK; /* Return code */
61496
int tempFile = 0; /* True for temp files (incl. in-memory files) */
61497
int memDb = 0; /* True if this is an in-memory file */
60436
-#ifndef SQLITE_OMIT_DESERIALIZE
61498
int memJM = 0; /* Memory journal mode */
60438
-#else
60439
-# define memJM 0
60440
-#endif
61499
int readOnly = 0; /* True if this is a read-only file */
61500
int journalFileSize; /* Bytes to allocate for each journal fd */
61501
char *zPathname = 0; /* Full path to database file */
@@ -60556,12 +61614,13 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
61614
** specific formatting and order of the various filenames, so if the format
61615
** changes here, be sure to change it there as well.
61616
*/
61617
+ assert( SQLITE_PTRSIZE==sizeof(Pager*) );
61618
pPtr = (u8 *)sqlite3MallocZero(
61619
ROUND8(sizeof(*pPager)) + /* Pager structure */
61620
ROUND8(pcacheSize) + /* PCache object */
61621
ROUND8(pVfs->szOsFile) + /* The main db file */
61622
journalFileSize * 2 + /* The two journal files */
60564
- sizeof(pPager) + /* Space to hold a pointer */
61623
+ SQLITE_PTRSIZE + /* Space to hold a pointer */
61624
4 + /* Database prefix */
61625
nPathname + 1 + /* database filename */
61626
nUriByte + /* query parameters */
@@ -60582,7 +61641,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
61641
pPager->sjfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61642
pPager->jfd = (sqlite3_file*)pPtr; pPtr += journalFileSize;
61643
assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
60585
- memcpy(pPtr, &pPager, sizeof(pPager)); pPtr += sizeof(pPager);
61644
+ memcpy(pPtr, &pPager, SQLITE_PTRSIZE); pPtr += SQLITE_PTRSIZE;
61645
61646
/* Fill in the Pager.zFilename and pPager.zQueryParam fields */
61647
pPtr += 4; /* Skip zero prefix */
@@ -60636,9 +61695,7 @@ SQLITE_PRIVATE int sqlite3PagerOpen(
61695
int fout = 0; /* VFS flags returned by xOpen() */
61696
rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
61697
assert( !memDb );
60639
-#ifndef SQLITE_OMIT_DESERIALIZE
61698
pPager->memVfs = memJM = (fout&SQLITE_OPEN_MEMORY)!=0;
60641
-#endif
61699
readOnly = (fout&SQLITE_OPEN_READONLY)!=0;
61700
61701
/* If the file was successfully opened for read/write access,
@@ -60775,15 +61832,18 @@ act_like_temp_file:
61832
61833
/*
61834
** Return the sqlite3_file for the main database given the name
60778
-** of the corresonding WAL or Journal name as passed into
61835
+** of the corresponding WAL or Journal name as passed into
61836
** xOpen.
61837
*/
61838
SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61839
Pager *pPager;
61840
+ const char *p;
61841
while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61842
zName--;
61843
}
60786
- pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
61844
+ p = zName - 4 - sizeof(Pager*);
61845
+ assert( EIGHT_BYTE_ALIGNMENT(p) );
61846
+ pPager = *(Pager**)p;
61847
return pPager->fd;
61848
}
61849
@@ -61417,8 +62477,20 @@ SQLITE_PRIVATE int sqlite3PagerGet(
62477
DbPage **ppPage, /* Write a pointer to the page here */
62478
int flags /* PAGER_GET_XXX flags */
62479
){
61420
- /* printf("PAGE %u\n", pgno); fflush(stdout); */
62480
+#if 0 /* Trace page fetch by setting to 1 */
62481
+ int rc;
62482
+ printf("PAGE %u\n", pgno);
62483
+ fflush(stdout);
62484
+ rc = pPager->xGet(pPager, pgno, ppPage, flags);
62485
+ if( rc ){
62486
+ printf("PAGE %u failed with 0x%02x\n", pgno, rc);
62487
+ fflush(stdout);
62488
+ }
62489
+ return rc;
62490
+#else
62491
+ /* Normal, high-speed version of sqlite3PagerGet() */
62492
return pPager->xGet(pPager, pgno, ppPage, flags);
62493
+#endif
62494
}
62495
62496
/*
@@ -62294,6 +63366,13 @@ SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
63366
rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_BEGIN_ATOMIC_WRITE, 0);
63367
if( rc==SQLITE_OK ){
63368
rc = pager_write_pagelist(pPager, pList);
63369
+ if( rc==SQLITE_OK && pPager->dbSize>pPager->dbFileSize ){
63370
+ char *pTmp = pPager->pTmpSpace;
63371
+ int szPage = (int)pPager->pageSize;
63372
+ memset(pTmp, 0, szPage);
63373
+ rc = sqlite3OsWrite(pPager->fd, pTmp, szPage,
63374
+ ((i64)pPager->dbSize*pPager->pageSize)-szPage);
63375
+ }
63376
if( rc==SQLITE_OK ){
63377
rc = sqlite3OsFileControl(fd, SQLITE_FCNTL_COMMIT_ATOMIC_WRITE, 0);
63378
}
@@ -62528,11 +63607,11 @@ SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
63607
a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
63608
a[4] = pPager->eState;
63609
a[5] = pPager->errCode;
62531
- a[6] = pPager->aStat[PAGER_STAT_HIT];
62532
- a[7] = pPager->aStat[PAGER_STAT_MISS];
63610
+ a[6] = (int)pPager->aStat[PAGER_STAT_HIT] & 0x7fffffff;
63611
+ a[7] = (int)pPager->aStat[PAGER_STAT_MISS] & 0x7fffffff;
63612
a[8] = 0; /* Used to be pPager->nOvfl */
63613
a[9] = pPager->nRead;
62535
- a[10] = pPager->aStat[PAGER_STAT_WRITE];
63614
+ a[10] = (int)pPager->aStat[PAGER_STAT_WRITE] & 0x7fffffff;
63615
return a;
63616
}
63617
#endif
@@ -62548,7 +63627,7 @@ SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
63627
** reset parameter is non-zero, the cache hit or miss count is zeroed before
63628
** returning.
63629
*/
62551
-SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
63630
+SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, u64 *pnVal){
63631
63632
assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
63633
|| eStat==SQLITE_DBSTATUS_CACHE_MISS
@@ -62784,7 +63863,7 @@ SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
63863
** This will be either the rollback journal or the WAL file.
63864
*/
63865
SQLITE_PRIVATE sqlite3_file *sqlite3PagerJrnlFile(Pager *pPager){
62787
-#if SQLITE_OMIT_WAL
63866
+#ifdef SQLITE_OMIT_WAL
63867
return pPager->jfd;
63868
#else
63869
return pPager->pWal ? sqlite3WalFile(pPager->pWal) : pPager->jfd;
@@ -63060,7 +64139,7 @@ SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
64139
assert( pPager->eState!=PAGER_ERROR );
64140
pPager->journalMode = (u8)eMode;
64141
63063
- /* When transistioning from TRUNCATE or PERSIST to any other journal
64142
+ /* When transitioning from TRUNCATE or PERSIST to any other journal
64143
** mode except WAL, unless the pager is in locking_mode=exclusive mode,
64144
** delete the journal file.
64145
*/
@@ -63105,7 +64184,7 @@ SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
64184
}
64185
assert( state==pPager->eState );
64186
}
63108
- }else if( eMode==PAGER_JOURNALMODE_OFF ){
64187
+ }else if( eMode==PAGER_JOURNALMODE_OFF || eMode==PAGER_JOURNALMODE_MEMORY ){
64188
sqlite3OsClose(pPager->jfd);
64189
}
64190
}
@@ -63488,6 +64567,12 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
64567
}
64568
#endif
64569
64570
+#if defined(SQLITE_USE_SEH) && !defined(SQLITE_OMIT_WAL)
64571
+SQLITE_PRIVATE int sqlite3PagerWalSystemErrno(Pager *pPager){
64572
+ return sqlite3WalSystemErrno(pPager->pWal);
64573
+}
64574
+#endif
64575
+
64576
#endif /* SQLITE_OMIT_DISKIO */
64577
64578
/************** End of pager.c ***********************************************/
@@ -63778,7 +64863,7 @@ SQLITE_PRIVATE int sqlite3WalTrace = 0;
64863
**
64864
** Technically, the various VFSes are free to implement these locks however
64865
** they see fit. However, compatibility is encouraged so that VFSes can
63781
-** interoperate. The standard implemention used on both unix and windows
64866
+** interoperate. The standard implementation used on both unix and windows
64867
** is for the index number to indicate a byte offset into the
64868
** WalCkptInfo.aLock[] array in the wal-index header. In other words, all
64869
** locks are on the shm file. The WALINDEX_LOCK_OFFSET constant (which
@@ -63854,7 +64939,7 @@ struct WalIndexHdr {
64939
** the mxFrame for that reader. The value READMARK_NOT_USED (0xffffffff)
64940
** for any aReadMark[] means that entry is unused. aReadMark[0] is
64941
** a special case; its value is never used and it exists as a place-holder
63857
-** to avoid having to offset aReadMark[] indexs by one. Readers holding
64942
+** to avoid having to offset aReadMark[] indexes by one. Readers holding
64943
** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
64944
** directly from the database.
64945
**
@@ -64022,7 +65107,15 @@ struct Wal {
65107
u32 iReCksum; /* On commit, recalculate checksums from here */
65108
const char *zWalName; /* Name of WAL file */
65109
u32 nCkpt; /* Checkpoint sequence counter in the wal-header */
65110
+#ifdef SQLITE_USE_SEH
65111
+ u32 lockMask; /* Mask of locks held */
65112
+ void *pFree; /* Pointer to sqlite3_free() if exception thrown */
65113
+ u32 *pWiValue; /* Value to write into apWiData[iWiPg] */
65114
+ int iWiPg; /* Write pWiValue into apWiData[iWiPg] */
65115
+ int iSysErrno; /* System error code following exception */
65116
+#endif
65117
#ifdef SQLITE_DEBUG
65118
+ int nSehTry; /* Number of nested SEH_TRY{} blocks */
65119
u8 lockError; /* True if a locking error has occurred */
65120
#endif
65121
#ifdef SQLITE_ENABLE_SNAPSHOT
@@ -64104,6 +65197,113 @@ struct WalIterator {
65197
sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
65198
)
65199
65200
+/*
65201
+** Structured Exception Handling (SEH) is a Windows-specific technique
65202
+** for catching exceptions raised while accessing memory-mapped files.
65203
+**
65204
+** The -DSQLITE_USE_SEH compile-time option means to use SEH to catch and
65205
+** deal with system-level errors that arise during WAL -shm file processing.
65206
+** Without this compile-time option, any system-level faults that appear
65207
+** while accessing the memory-mapped -shm file will cause a process-wide
65208
+** signal to be deliver, which will more than likely cause the entire
65209
+** process to exit.
65210
+*/
65211
+#ifdef SQLITE_USE_SEH
65212
+#include <Windows.h>
65213
+
65214
+/* Beginning of a block of code in which an exception might occur */
65215
+# define SEH_TRY __try { \
65216
+ assert( walAssertLockmask(pWal) && pWal->nSehTry==0 ); \
65217
+ VVA_ONLY(pWal->nSehTry++);
65218
+
65219
+/* The end of a block of code in which an exception might occur */
65220
+# define SEH_EXCEPT(X) \
65221
+ VVA_ONLY(pWal->nSehTry--); \
65222
+ assert( pWal->nSehTry==0 ); \
65223
+ } __except( sehExceptionFilter(pWal, GetExceptionCode(), GetExceptionInformation() ) ){ X }
65224
+
65225
+/* Simulate a memory-mapping fault in the -shm file for testing purposes */
65226
+# define SEH_INJECT_FAULT sehInjectFault(pWal)
65227
+
65228
+/*
65229
+** The second argument is the return value of GetExceptionCode() for the
65230
+** current exception. Return EXCEPTION_EXECUTE_HANDLER if the exception code
65231
+** indicates that the exception may have been caused by accessing the *-shm
65232
+** file mapping. Or EXCEPTION_CONTINUE_SEARCH otherwise.
65233
+*/
65234
+static int sehExceptionFilter(Wal *pWal, int eCode, EXCEPTION_POINTERS *p){
65235
+ VVA_ONLY(pWal->nSehTry--);
65236
+ if( eCode==EXCEPTION_IN_PAGE_ERROR ){
65237
+ if( p && p->ExceptionRecord && p->ExceptionRecord->NumberParameters>=3 ){
65238
+ /* From MSDN: For this type of exception, the first element of the
65239
+ ** ExceptionInformation[] array is a read-write flag - 0 if the exception
65240
+ ** was thrown while reading, 1 if while writing. The second element is
65241
+ ** the virtual address being accessed. The "third array element specifies
65242
+ ** the underlying NTSTATUS code that resulted in the exception". */
65243
+ pWal->iSysErrno = (int)p->ExceptionRecord->ExceptionInformation[2];
65244
+ }
65245
+ return EXCEPTION_EXECUTE_HANDLER;
65246
+ }
65247
+ return EXCEPTION_CONTINUE_SEARCH;
65248
+}
65249
+
65250
+/*
65251
+** If one is configured, invoke the xTestCallback callback with 650 as
65252
+** the argument. If it returns true, throw the same exception that is
65253
+** thrown by the system if the *-shm file mapping is accessed after it
65254
+** has been invalidated.
65255
+*/
65256
+static void sehInjectFault(Wal *pWal){
65257
+ int res;
65258
+ assert( pWal->nSehTry>0 );
65259
+
65260
+ res = sqlite3FaultSim(650);
65261
+ if( res!=0 ){
65262
+ ULONG_PTR aArg[3];
65263
+ aArg[0] = 0;
65264
+ aArg[1] = 0;
65265
+ aArg[2] = (ULONG_PTR)res;
65266
+ RaiseException(EXCEPTION_IN_PAGE_ERROR, 0, 3, (const ULONG_PTR*)aArg);
65267
+ }
65268
+}
65269
+
65270
+/*
65271
+** There are two ways to use this macro. To set a pointer to be freed
65272
+** if an exception is thrown:
65273
+**
65274
+** SEH_FREE_ON_ERROR(0, pPtr);
65275
+**
65276
+** and to cancel the same:
65277
+**
65278
+** SEH_FREE_ON_ERROR(pPtr, 0);
65279
+**
65280
+** In the first case, there must not already be a pointer registered to
65281
+** be freed. In the second case, pPtr must be the registered pointer.
65282
+*/
65283
+#define SEH_FREE_ON_ERROR(X,Y) \
65284
+ assert( (X==0 || Y==0) && pWal->pFree==X ); pWal->pFree = Y
65285
+
65286
+/*
65287
+** There are two ways to use this macro. To arrange for pWal->apWiData[iPg]
65288
+** to be set to pValue if an exception is thrown:
65289
+**
65290
+** SEH_SET_ON_ERROR(iPg, pValue);
65291
+**
65292
+** and to cancel the same:
65293
+**
65294
+** SEH_SET_ON_ERROR(0, 0);
65295
+*/
65296
+#define SEH_SET_ON_ERROR(X,Y) pWal->iWiPg = X; pWal->pWiValue = Y
65297
+
65298
+#else
65299
+# define SEH_TRY VVA_ONLY(pWal->nSehTry++);
65300
+# define SEH_EXCEPT(X) VVA_ONLY(pWal->nSehTry--); assert( pWal->nSehTry==0 );
65301
+# define SEH_INJECT_FAULT assert( pWal->nSehTry>0 );
65302
+# define SEH_FREE_ON_ERROR(X,Y)
65303
+# define SEH_SET_ON_ERROR(X,Y)
65304
+#endif /* ifdef SQLITE_USE_SEH */
65305
+
65306
+
65307
/*
65308
** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
65309
** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
@@ -64176,6 +65376,7 @@ static int walIndexPage(
65376
int iPage, /* The page we seek */
65377
volatile u32 **ppPage /* Write the page pointer here */
65378
){
65379
+ SEH_INJECT_FAULT;
65380
if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
65381
return walIndexPageRealloc(pWal, iPage, ppPage);
65382
}
@@ -64187,6 +65388,7 @@ static int walIndexPage(
65388
*/
65389
static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
65390
assert( pWal->nWiData>0 && pWal->apWiData[0] );
65391
+ SEH_INJECT_FAULT;
65392
return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
65393
}
65394
@@ -64195,6 +65397,7 @@ static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
65397
*/
65398
static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
65399
assert( pWal->nWiData>0 && pWal->apWiData[0] );
65400
+ SEH_INJECT_FAULT;
65401
return (volatile WalIndexHdr*)pWal->apWiData[0];
65402
}
65403
@@ -64384,7 +65587,7 @@ static int walDecodeFrame(
65587
return 0;
65588
}
65589
64387
- /* A frame is only valid if the page number is creater than zero.
65590
+ /* A frame is only valid if the page number is greater than zero.
65591
*/
65592
pgno = sqlite3Get4byte(&aFrame[0]);
65593
if( pgno==0 ){
@@ -64392,7 +65595,7 @@ static int walDecodeFrame(
65595
}
65596
65597
/* A frame is only valid if a checksum of the WAL header,
64395
- ** all prior frams, the first 16 bytes of this frame-header,
This file is too large to show in full.
src/database/sqlite/sqlite3.h
+380
-74
@@ -146,9 +146,9 @@ extern "C" {
146
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
147
** [sqlite_version()] and [sqlite_source_id()].
148
*/
149
-#define SQLITE_VERSION "3.42.0"
150
-#define SQLITE_VERSION_NUMBER 3042000
151
-#define SQLITE_SOURCE_ID "2023-05-16 12:36:15 831d0fb2836b71c9bc51067c49fee4b8f18047814f2ff22d817d25195cf350b0"
149
+#define SQLITE_VERSION "3.45.3"
150
+#define SQLITE_VERSION_NUMBER 3045003
151
+#define SQLITE_SOURCE_ID "2024-04-15 13:34:05 8653b758870e6ef0c98d46b3ace27849054af85da891eb121e9aaa537f1e8355"
152
153
/*
154
** CAPI3REF: Run-Time Library Version Numbers
@@ -420,6 +420,8 @@ typedef int (*sqlite3_callback)(void*,int,char**, char**);
420
** the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
421
** <li> The application must not modify the SQL statement text passed into
422
** the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
423
+** <li> The application must not dereference the arrays or string pointers
424
+** passed as the 3rd and 4th callback parameters after it returns.
425
** </ul>
426
*/
427
SQLITE_API int sqlite3_exec(
@@ -528,6 +530,7 @@ SQLITE_API int sqlite3_exec(
530
#define SQLITE_IOERR_ROLLBACK_ATOMIC (SQLITE_IOERR | (31<<8))
531
#define SQLITE_IOERR_DATA (SQLITE_IOERR | (32<<8))
532
#define SQLITE_IOERR_CORRUPTFS (SQLITE_IOERR | (33<<8))
533
+#define SQLITE_IOERR_IN_PAGE (SQLITE_IOERR | (34<<8))
534
#define SQLITE_LOCKED_SHAREDCACHE (SQLITE_LOCKED | (1<<8))
535
#define SQLITE_LOCKED_VTAB (SQLITE_LOCKED | (2<<8))
536
#define SQLITE_BUSY_RECOVERY (SQLITE_BUSY | (1<<8))
@@ -1190,7 +1193,7 @@ struct sqlite3_io_methods {
1193
** by clients within the current process, only within other processes.
1194
**
1195
** <li>[[SQLITE_FCNTL_CKSM_FILE]]
1193
-** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use interally by the
1196
+** The [SQLITE_FCNTL_CKSM_FILE] opcode is for use internally by the
1197
** [checksum VFS shim] only.
1198
**
1199
** <li>[[SQLITE_FCNTL_RESET_CACHE]]
@@ -2126,7 +2129,7 @@ struct sqlite3_mem_methods {
2129
** is stored in each sorted record and the required column values loaded
2130
** from the database as records are returned in sorted order. The default
2131
** value for this option is to never use this optimization. Specifying a
2129
-** negative value for this option restores the default behaviour.
2132
+** negative value for this option restores the default behavior.
2133
** This option is only available if SQLite is compiled with the
2134
** [SQLITE_ENABLE_SORTER_REFERENCES] compile-time option.
2135
**
@@ -2140,6 +2143,22 @@ struct sqlite3_mem_methods {
2143
** configuration setting is never used, then the default maximum is determined
2144
** by the [SQLITE_MEMDB_DEFAULT_MAXSIZE] compile-time option. If that
2145
** compile-time option is not set, then the default maximum is 1073741824.
2146
+**
2147
+** [[SQLITE_CONFIG_ROWID_IN_VIEW]]
2148
+** <dt>SQLITE_CONFIG_ROWID_IN_VIEW
2149
+** <dd>The SQLITE_CONFIG_ROWID_IN_VIEW option enables or disables the ability
2150
+** for VIEWs to have a ROWID. The capability can only be enabled if SQLite is
2151
+** compiled with -DSQLITE_ALLOW_ROWID_IN_VIEW, in which case the capability
2152
+** defaults to on. This configuration option queries the current setting or
2153
+** changes the setting to off or on. The argument is a pointer to an integer.
2154
+** If that integer initially holds a value of 1, then the ability for VIEWs to
2155
+** have ROWIDs is activated. If the integer initially holds zero, then the
2156
+** ability is deactivated. Any other initial value for the integer leaves the
2157
+** setting unchanged. After changes, if any, the integer is written with
2158
+** a 1 or 0, if the ability for VIEWs to have ROWIDs is on or off. If SQLite
2159
+** is compiled without -DSQLITE_ALLOW_ROWID_IN_VIEW (which is the usual and
2160
+** recommended case) then the integer is always filled with zero, regardless
2161
+** if its initial value.
2162
** </dl>
2163
*/
2164
#define SQLITE_CONFIG_SINGLETHREAD 1 /* nil */
@@ -2171,6 +2190,7 @@ struct sqlite3_mem_methods {
2190
#define SQLITE_CONFIG_SMALL_MALLOC 27 /* boolean */
2191
#define SQLITE_CONFIG_SORTERREF_SIZE 28 /* int nByte */
2192
#define SQLITE_CONFIG_MEMDB_MAXSIZE 29 /* sqlite3_int64 */
2193
+#define SQLITE_CONFIG_ROWID_IN_VIEW 30 /* int* */
2194
2195
/*
2196
** CAPI3REF: Database Connection Configuration Options
@@ -2301,7 +2321,7 @@ struct sqlite3_mem_methods {
2321
** database handle, SQLite checks if this will mean that there are now no
2322
** connections at all to the database. If so, it performs a checkpoint
2323
** operation before closing the connection. This option may be used to
2304
-** override this behaviour. The first parameter passed to this operation
2324
+** override this behavior. The first parameter passed to this operation
2325
** is an integer - positive to disable checkpoints-on-close, or zero (the
2326
** default) to enable them, and negative to leave the setting unchanged.
2327
** The second parameter is a pointer to an integer
@@ -2454,7 +2474,7 @@ struct sqlite3_mem_methods {
2474
** the [VACUUM] command will fail with an obscure error when attempting to
2475
** process a table with generated columns and a descending index. This is
2476
** not considered a bug since SQLite versions 3.3.0 and earlier do not support
2457
-** either generated columns or decending indexes.
2477
+** either generated columns or descending indexes.
2478
** </dd>
2479
**
2480
** [[SQLITE_DBCONFIG_STMT_SCANSTATUS]]
@@ -2735,6 +2755,7 @@ SQLITE_API sqlite3_int64 sqlite3_total_changes64(sqlite3*);
2755
**
2756
** ^The [sqlite3_is_interrupted(D)] interface can be used to determine whether
2757
** or not an interrupt is currently in effect for [database connection] D.
2758
+** It returns 1 if an interrupt is currently in effect, or 0 otherwise.
2759
*/
2760
SQLITE_API void sqlite3_interrupt(sqlite3*);
2761
SQLITE_API int sqlite3_is_interrupted(sqlite3*);
@@ -3388,8 +3409,10 @@ SQLITE_API SQLITE_DEPRECATED void *sqlite3_profile(sqlite3*,
3409
** M argument should be the bitwise OR-ed combination of
3410
** zero or more [SQLITE_TRACE] constants.
3411
**
3391
-** ^Each call to either sqlite3_trace() or sqlite3_trace_v2() overrides
3392
-** (cancels) any prior calls to sqlite3_trace() or sqlite3_trace_v2().
3412
+** ^Each call to either sqlite3_trace(D,X,P) or sqlite3_trace_v2(D,M,X,P)
3413
+** overrides (cancels) all prior calls to sqlite3_trace(D,X,P) or
3414
+** sqlite3_trace_v2(D,M,X,P) for the [database connection] D. Each
3415
+** database connection may have at most one trace callback.
3416
**
3417
** ^The X callback is invoked whenever any of the events identified by
3418
** mask M occur. ^The integer return value from the callback is currently
@@ -3758,7 +3781,7 @@ SQLITE_API int sqlite3_open_v2(
3781
** as F) must be one of:
3782
** <ul>
3783
** <li> A database filename pointer created by the SQLite core and
3761
-** passed into the xOpen() method of a VFS implemention, or
3784
+** passed into the xOpen() method of a VFS implementation, or
3785
** <li> A filename obtained from [sqlite3_db_filename()], or
3786
** <li> A new filename constructed using [sqlite3_create_filename()].
3787
** </ul>
@@ -3871,7 +3894,7 @@ SQLITE_API sqlite3_file *sqlite3_database_file_object(const char*);
3894
/*
3895
** CAPI3REF: Create and Destroy VFS Filenames
3896
**
3874
-** These interfces are provided for use by [VFS shim] implementations and
3897
+** These interfaces are provided for use by [VFS shim] implementations and
3898
** are not useful outside of that context.
3899
**
3900
** The sqlite3_create_filename(D,J,W,N,P) allocates memory to hold a version of
@@ -3950,14 +3973,17 @@ SQLITE_API void sqlite3_free_filename(sqlite3_filename);
3973
** </ul>
3974
**
3975
** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
3953
-** text that describes the error, as either UTF-8 or UTF-16 respectively.
3976
+** text that describes the error, as either UTF-8 or UTF-16 respectively,
3977
+** or NULL if no error message is available.
3978
+** (See how SQLite handles [invalid UTF] for exceptions to this rule.)
3979
** ^(Memory to hold the error message string is managed internally.
3980
** The application does not need to worry about freeing the result.
3981
** However, the error string might be overwritten or deallocated by
3982
** subsequent calls to other SQLite interface functions.)^
3983
**
3959
-** ^The sqlite3_errstr() interface returns the English-language text
3960
-** that describes the [result code], as UTF-8.
3984
+** ^The sqlite3_errstr(E) interface returns the English-language text
3985
+** that describes the [result code] E, as UTF-8, or NULL if E is not an
3986
+** result code for which a text error message is available.
3987
** ^(Memory to hold the error message string is managed internally
3988
** and must not be freed by the application)^.
3989
**
@@ -4418,6 +4444,41 @@ SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
4444
*/
4445
SQLITE_API int sqlite3_stmt_isexplain(sqlite3_stmt *pStmt);
4446
4447
+/*
4448
+** CAPI3REF: Change The EXPLAIN Setting For A Prepared Statement
4449
+** METHOD: sqlite3_stmt
4450
+**
4451
+** The sqlite3_stmt_explain(S,E) interface changes the EXPLAIN
4452
+** setting for [prepared statement] S. If E is zero, then S becomes
4453
+** a normal prepared statement. If E is 1, then S behaves as if
4454
+** its SQL text began with "[EXPLAIN]". If E is 2, then S behaves as if
4455
+** its SQL text began with "[EXPLAIN QUERY PLAN]".
4456
+**
4457
+** Calling sqlite3_stmt_explain(S,E) might cause S to be reprepared.
4458
+** SQLite tries to avoid a reprepare, but a reprepare might be necessary
4459
+** on the first transition into EXPLAIN or EXPLAIN QUERY PLAN mode.
4460
+**
4461
+** Because of the potential need to reprepare, a call to
4462
+** sqlite3_stmt_explain(S,E) will fail with SQLITE_ERROR if S cannot be
4463
+** reprepared because it was created using [sqlite3_prepare()] instead of
4464
+** the newer [sqlite3_prepare_v2()] or [sqlite3_prepare_v3()] interfaces and
4465
+** hence has no saved SQL text with which to reprepare.
4466
+**
4467
+** Changing the explain setting for a prepared statement does not change
4468
+** the original SQL text for the statement. Hence, if the SQL text originally
4469
+** began with EXPLAIN or EXPLAIN QUERY PLAN, but sqlite3_stmt_explain(S,0)
4470
+** is called to convert the statement into an ordinary statement, the EXPLAIN
4471
+** or EXPLAIN QUERY PLAN keywords will still appear in the sqlite3_sql(S)
4472
+** output, even though the statement now acts like a normal SQL statement.
4473
+**
4474
+** This routine returns SQLITE_OK if the explain mode is successfully
4475
+** changed, or an error code if the explain mode could not be changed.
4476
+** The explain mode cannot be changed while a statement is active.
4477
+** Hence, it is good practice to call [sqlite3_reset(S)]
4478
+** immediately prior to calling sqlite3_stmt_explain(S,E).
4479
+*/
4480
+SQLITE_API int sqlite3_stmt_explain(sqlite3_stmt *pStmt, int eMode);
4481
+
4482
/*
4483
** CAPI3REF: Determine If A Prepared Statement Has Been Reset
4484
** METHOD: sqlite3_stmt
@@ -4581,7 +4642,7 @@ typedef struct sqlite3_context sqlite3_context;
4642
** with it may be passed. ^It is called to dispose of the BLOB or string even
4643
** if the call to the bind API fails, except the destructor is not called if
4644
** the third parameter is a NULL pointer or the fourth parameter is negative.
4584
-** ^ (2) The special constant, [SQLITE_STATIC], may be passsed to indicate that
4645
+** ^ (2) The special constant, [SQLITE_STATIC], may be passed to indicate that
4646
** the application remains responsible for disposing of the object. ^In this
4647
** case, the object and the provided pointer to it must remain valid until
4648
** either the prepared statement is finalized or the same SQL parameter is
@@ -5260,20 +5321,33 @@ SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
5321
** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
5322
** back to the beginning of its program.
5323
**
5263
-** ^If the most recent call to [sqlite3_step(S)] for the
5264
-** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
5265
-** or if [sqlite3_step(S)] has never before been called on S,
5266
-** then [sqlite3_reset(S)] returns [SQLITE_OK].
5324
+** ^The return code from [sqlite3_reset(S)] indicates whether or not
5325
+** the previous evaluation of prepared statement S completed successfully.
5326
+** ^If [sqlite3_step(S)] has never before been called on S or if
5327
+** [sqlite3_step(S)] has not been called since the previous call
5328
+** to [sqlite3_reset(S)], then [sqlite3_reset(S)] will return
5329
+** [SQLITE_OK].
5330
**
5331
** ^If the most recent call to [sqlite3_step(S)] for the
5332
** [prepared statement] S indicated an error, then
5333
** [sqlite3_reset(S)] returns an appropriate [error code].
5334
+** ^The [sqlite3_reset(S)] interface might also return an [error code]
5335
+** if there were no prior errors but the process of resetting
5336
+** the prepared statement caused a new error. ^For example, if an
5337
+** [INSERT] statement with a [RETURNING] clause is only stepped one time,
5338
+** that one call to [sqlite3_step(S)] might return SQLITE_ROW but
5339
+** the overall statement might still fail and the [sqlite3_reset(S)] call
5340
+** might return SQLITE_BUSY if locking constraints prevent the
5341
+** database change from committing. Therefore, it is important that
5342
+** applications check the return code from [sqlite3_reset(S)] even if
5343
+** no prior call to [sqlite3_step(S)] indicated a problem.
5344
**
5345
** ^The [sqlite3_reset(S)] interface does not change the values
5346
** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
5347
*/
5348
SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
5349
5350
+
5351
/*
5352
** CAPI3REF: Create Or Redefine SQL Functions
5353
** KEYWORDS: {function creation routines}
@@ -5484,7 +5558,7 @@ SQLITE_API int sqlite3_create_window_function(
5558
** [application-defined SQL function]
5559
** that has side-effects or that could potentially leak sensitive information.
5560
** This will prevent attacks in which an application is tricked
5487
-** into using a database file that has had its schema surreptiously
5561
+** into using a database file that has had its schema surreptitiously
5562
** modified to invoke the application-defined function in ways that are
5563
** harmful.
5564
** <p>
@@ -5520,13 +5594,27 @@ SQLITE_API int sqlite3_create_window_function(
5594
** </dd>
5595
**
5596
** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5523
-** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
5597
+** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5598
** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5525
-** Specifying this flag makes no difference for scalar or aggregate user
5526
-** functions. However, if it is not specified for a user-defined window
5527
-** function, then any sub-types belonging to arguments passed to the window
5528
-** function may be discarded before the window function is called (i.e.
5529
-** sqlite3_value_subtype() will always return 0).
5599
+** This flag instructs SQLite to omit some corner-case optimizations that
5600
+** might disrupt the operation of the [sqlite3_value_subtype()] function,
5601
+** causing it to return zero rather than the correct subtype().
5602
+** SQL functions that invokes [sqlite3_value_subtype()] should have this
5603
+** property. If the SQLITE_SUBTYPE property is omitted, then the return
5604
+** value from [sqlite3_value_subtype()] might sometimes be zero even though
5605
+** a non-zero subtype was specified by the function argument expression.
5606
+**
5607
+** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
5608
+** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
5609
+** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
5610
+** result.
5611
+** Every function that invokes [sqlite3_result_subtype()] should have this
5612
+** property. If it does not, then the call to [sqlite3_result_subtype()]
5613
+** might become a no-op if the function is used as term in an
5614
+** [expression index]. On the other hand, SQL functions that never invoke
5615
+** [sqlite3_result_subtype()] should avoid setting this property, as the
5616
+** purpose of this property is to disable certain optimizations that are
5617
+** incompatible with subtypes.
5618
** </dd>
5619
** </dl>
5620
*/
@@ -5534,6 +5622,7 @@ SQLITE_API int sqlite3_create_window_function(
5622
#define SQLITE_DIRECTONLY 0x000080000
5623
#define SQLITE_SUBTYPE 0x000100000
5624
#define SQLITE_INNOCUOUS 0x000200000
5625
+#define SQLITE_RESULT_SUBTYPE 0x001000000
5626
5627
/*
5628
** CAPI3REF: Deprecated Functions
@@ -5730,6 +5819,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
5819
** information can be used to pass a limited amount of context from
5820
** one SQL function to another. Use the [sqlite3_result_subtype()]
5821
** routine to set the subtype for the return value of an SQL function.
5822
+**
5823
+** Every [application-defined SQL function] that invoke this interface
5824
+** should include the [SQLITE_SUBTYPE] property in the text
5825
+** encoding argument when the function is [sqlite3_create_function|registered].
5826
+** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
5827
+** might return zero instead of the upstream subtype in some corner cases.
5828
*/
5829
SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
5830
@@ -5828,48 +5923,56 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
5923
** METHOD: sqlite3_context
5924
**
5925
** These functions may be used by (non-aggregate) SQL functions to
5831
-** associate metadata with argument values. If the same value is passed to
5832
-** multiple invocations of the same SQL function during query execution, under
5833
-** some circumstances the associated metadata may be preserved. An example
5834
-** of where this might be useful is in a regular-expression matching
5835
-** function. The compiled version of the regular expression can be stored as
5836
-** metadata associated with the pattern string.
5926
+** associate auxiliary data with argument values. If the same argument
5927
+** value is passed to multiple invocations of the same SQL function during
5928
+** query execution, under some circumstances the associated auxiliary data
5929
+** might be preserved. An example of where this might be useful is in a
5930
+** regular-expression matching function. The compiled version of the regular
5931
+** expression can be stored as auxiliary data associated with the pattern string.
5932
** Then as long as the pattern string remains the same,
5933
** the compiled regular expression can be reused on multiple
5934
** invocations of the same function.
5935
**
5841
-** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the metadata
5936
+** ^The sqlite3_get_auxdata(C,N) interface returns a pointer to the auxiliary data
5937
** associated by the sqlite3_set_auxdata(C,N,P,X) function with the Nth argument
5938
** value to the application-defined function. ^N is zero for the left-most
5844
-** function argument. ^If there is no metadata
5939
+** function argument. ^If there is no auxiliary data
5940
** associated with the function argument, the sqlite3_get_auxdata(C,N) interface
5941
** returns a NULL pointer.
5942
**
5848
-** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
5849
-** argument of the application-defined function. ^Subsequent
5943
+** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as auxiliary data for the
5944
+** N-th argument of the application-defined function. ^Subsequent
5945
** calls to sqlite3_get_auxdata(C,N) return P from the most recent
5851
-** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
5852
-** NULL if the metadata has been discarded.
5946
+** sqlite3_set_auxdata(C,N,P,X) call if the auxiliary data is still valid or
5947
+** NULL if the auxiliary data has been discarded.
5948
** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
5949
** SQLite will invoke the destructor function X with parameter P exactly
5855
-** once, when the metadata is discarded.
5856
-** SQLite is free to discard the metadata at any time, including: <ul>
5950
+** once, when the auxiliary data is discarded.
5951
+** SQLite is free to discard the auxiliary data at any time, including: <ul>
5952
** <li> ^(when the corresponding function parameter changes)^, or
5953
** <li> ^(when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
5954
** SQL statement)^, or
5955
** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
5956
** parameter)^, or
5957
** <li> ^(during the original sqlite3_set_auxdata() call when a memory
5863
-** allocation error occurs.)^ </ul>
5958
+** allocation error occurs.)^
5959
+** <li> ^(during the original sqlite3_set_auxdata() call if the function
5960
+** is evaluated during query planning instead of during query execution,
5961
+** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
5962
**
5865
-** Note the last bullet in particular. The destructor X in
5963
+** Note the last two bullets in particular. The destructor X in
5964
** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
5965
** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
5966
** should be called near the end of the function implementation and the
5967
** function implementation should not make any use of P after
5870
-** sqlite3_set_auxdata() has been called.
5871
-**
5872
-** ^(In practice, metadata is preserved between function calls for
5968
+** sqlite3_set_auxdata() has been called. Furthermore, a call to
5969
+** sqlite3_get_auxdata() that occurs immediately after a corresponding call
5970
+** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
5971
+** condition occurred during the sqlite3_set_auxdata() call or if the
5972
+** function is being evaluated during query planning rather than during
5973
+** query execution.
5974
+**
5975
+** ^(In practice, auxiliary data is preserved between function calls for
5976
** function parameters that are compile-time constants, including literal
5977
** values and [parameters] and expressions composed from the same.)^
5978
**
@@ -5879,10 +5982,67 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
5982
**
5983
** These routines must be called from the same thread in which
5984
** the SQL function is running.
5985
+**
5986
+** See also: [sqlite3_get_clientdata()] and [sqlite3_set_clientdata()].
5987
*/
5988
SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
5989
SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
5990
5991
+/*
5992
+** CAPI3REF: Database Connection Client Data
5993
+** METHOD: sqlite3
5994
+**
5995
+** These functions are used to associate one or more named pointers
5996
+** with a [database connection].
5997
+** A call to sqlite3_set_clientdata(D,N,P,X) causes the pointer P
5998
+** to be attached to [database connection] D using name N. Subsequent
5999
+** calls to sqlite3_get_clientdata(D,N) will return a copy of pointer P
6000
+** or a NULL pointer if there were no prior calls to
6001
+** sqlite3_set_clientdata() with the same values of D and N.
6002
+** Names are compared using strcmp() and are thus case sensitive.
6003
+**
6004
+** If P and X are both non-NULL, then the destructor X is invoked with
6005
+** argument P on the first of the following occurrences:
6006
+** <ul>
6007
+** <li> An out-of-memory error occurs during the call to
6008
+** sqlite3_set_clientdata() which attempts to register pointer P.
6009
+** <li> A subsequent call to sqlite3_set_clientdata(D,N,P,X) is made
6010
+** with the same D and N parameters.
6011
+** <li> The database connection closes. SQLite does not make any guarantees
6012
+** about the order in which destructors are called, only that all
6013
+** destructors will be called exactly once at some point during the
6014
+** database connection closing process.
6015
+** </ul>
6016
+**
6017
+** SQLite does not do anything with client data other than invoke
6018
+** destructors on the client data at the appropriate time. The intended
6019
+** use for client data is to provide a mechanism for wrapper libraries
6020
+** to store additional information about an SQLite database connection.
6021
+**
6022
+** There is no limit (other than available memory) on the number of different
6023
+** client data pointers (with different names) that can be attached to a
6024
+** single database connection. However, the implementation is optimized
6025
+** for the case of having only one or two different client data names.
6026
+** Applications and wrapper libraries are discouraged from using more than
6027
+** one client data name each.
6028
+**
6029
+** There is no way to enumerate the client data pointers
6030
+** associated with a database connection. The N parameter can be thought
6031
+** of as a secret key such that only code that knows the secret key is able
6032
+** to access the associated data.
6033
+**
6034
+** Security Warning: These interfaces should not be exposed in scripting
6035
+** languages or in other circumstances where it might be possible for an
6036
+** an attacker to invoke them. Any agent that can invoke these interfaces
6037
+** can probably also take control of the process.
6038
+**
6039
+** Database connection client data is only available for SQLite
6040
+** version 3.44.0 ([dateof:3.44.0]) and later.
6041
+**
6042
+** See also: [sqlite3_set_auxdata()] and [sqlite3_get_auxdata()].
6043
+*/
6044
+SQLITE_API void *sqlite3_get_clientdata(sqlite3*,const char*);
6045
+SQLITE_API int sqlite3_set_clientdata(sqlite3*, const char*, void*, void(*)(void*));
6046
6047
/*
6048
** CAPI3REF: Constants Defining Special Destructor Behavior
@@ -6084,6 +6244,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6244
** higher order bits are discarded.
6245
** The number of subtype bytes preserved by SQLite might increase
6246
** in future releases of SQLite.
6247
+**
6248
+** Every [application-defined SQL function] that invokes this interface
6249
+** should include the [SQLITE_RESULT_SUBTYPE] property in its
6250
+** text encoding argument when the SQL function is
6251
+** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6252
+** property is omitted from the function that invokes sqlite3_result_subtype(),
6253
+** then in some cases the sqlite3_result_subtype() might fail to set
6254
+** the result subtype.
6255
+**
6256
+** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6257
+** SQL function that invokes the sqlite3_result_subtype() interface
6258
+** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6259
+** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6260
+** by default.
6261
*/
6262
SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6263
@@ -6515,7 +6689,7 @@ SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
6689
SQLITE_API int sqlite3_txn_state(sqlite3*,const char *zSchema);
6690
6691
/*
6518
-** CAPI3REF: Allowed return values from [sqlite3_txn_state()]
6692
+** CAPI3REF: Allowed return values from sqlite3_txn_state()
6693
** KEYWORDS: {transaction state}
6694
**
6695
** These constants define the current transaction state of a database file.
@@ -6647,7 +6821,7 @@ SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
6821
** ^Each call to the sqlite3_autovacuum_pages() interface overrides all
6822
** previous invocations for that database connection. ^If the callback
6823
** argument (C) to sqlite3_autovacuum_pages(D,C,P,X) is a NULL pointer,
6650
-** then the autovacuum steps callback is cancelled. The return value
6824
+** then the autovacuum steps callback is canceled. The return value
6825
** from sqlite3_autovacuum_pages() is normally SQLITE_OK, but might
6826
** be some other error code if something goes wrong. The current
6827
** implementation will only return SQLITE_OK or SQLITE_MISUSE, but other
@@ -7166,6 +7340,10 @@ struct sqlite3_module {
7340
/* The methods above are in versions 1 and 2 of the sqlite_module object.
7341
** Those below are for version 3 and greater. */
7342
int (*xShadowName)(const char*);
7343
+ /* The methods above are in versions 1 through 3 of the sqlite_module object.
7344
+ ** Those below are for version 4 and greater. */
7345
+ int (*xIntegrity)(sqlite3_vtab *pVTab, const char *zSchema,
7346
+ const char *zTabName, int mFlags, char **pzErr);
7347
};
7348
7349
/*
@@ -7653,7 +7831,7 @@ SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
7831
** code is returned and the transaction rolled back.
7832
**
7833
** Calling this function with an argument that is not a NULL pointer or an
7656
-** open blob handle results in undefined behaviour. ^Calling this routine
7834
+** open blob handle results in undefined behavior. ^Calling this routine
7835
** with a null pointer (such as would be returned by a failed call to
7836
** [sqlite3_blob_open()]) is a harmless no-op. ^Otherwise, if this function
7837
** is passed a valid open blob handle, the values returned by the
@@ -7880,9 +8058,11 @@ SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
8058
**
8059
** ^(Some systems (for example, Windows 95) do not support the operation
8060
** implemented by sqlite3_mutex_try(). On those systems, sqlite3_mutex_try()
7883
-** will always return SQLITE_BUSY. The SQLite core only ever uses
7884
-** sqlite3_mutex_try() as an optimization so this is acceptable
7885
-** behavior.)^
8061
+** will always return SQLITE_BUSY. In most cases the SQLite core only uses
8062
+** sqlite3_mutex_try() as an optimization, so this is acceptable
8063
+** behavior. The exceptions are unix builds that set the
8064
+** SQLITE_ENABLE_SETLK_TIMEOUT build option. In that case a working
8065
+** sqlite3_mutex_try() is required.)^
8066
**
8067
** ^The sqlite3_mutex_leave() routine exits a mutex that was
8068
** previously entered by the same thread. The behavior
@@ -8133,6 +8313,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8313
#define SQLITE_TESTCTRL_PRNG_SAVE 5
8314
#define SQLITE_TESTCTRL_PRNG_RESTORE 6
8315
#define SQLITE_TESTCTRL_PRNG_RESET 7 /* NOT USED */
8316
+#define SQLITE_TESTCTRL_FK_NO_ACTION 7
8317
#define SQLITE_TESTCTRL_BITVEC_TEST 8
8318
#define SQLITE_TESTCTRL_FAULT_INSTALL 9
8319
#define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS 10
@@ -8140,6 +8321,7 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8321
#define SQLITE_TESTCTRL_ASSERT 12
8322
#define SQLITE_TESTCTRL_ALWAYS 13
8323
#define SQLITE_TESTCTRL_RESERVE 14 /* NOT USED */
8324
+#define SQLITE_TESTCTRL_JSON_SELFCHECK 14
8325
#define SQLITE_TESTCTRL_OPTIMIZATIONS 15
8326
#define SQLITE_TESTCTRL_ISKEYWORD 16 /* NOT USED */
8327
#define SQLITE_TESTCTRL_SCRATCHMALLOC 17 /* NOT USED */
@@ -8161,7 +8343,8 @@ SQLITE_API int sqlite3_test_control(int op, ...);
8343
#define SQLITE_TESTCTRL_TRACEFLAGS 31
8344
#define SQLITE_TESTCTRL_TUNE 32
8345
#define SQLITE_TESTCTRL_LOGEST 33
8164
-#define SQLITE_TESTCTRL_LAST 33 /* Largest TESTCTRL */
8346
+#define SQLITE_TESTCTRL_USELONGDOUBLE 34
8347
+#define SQLITE_TESTCTRL_LAST 34 /* Largest TESTCTRL */
8348
8349
/*
8350
** CAPI3REF: SQL Keyword Checking
@@ -9617,7 +9800,7 @@ SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
9800
** [[SQLITE_VTAB_DIRECTONLY]]<dt>SQLITE_VTAB_DIRECTONLY</dt>
9801
** <dd>Calls of the form
9802
** [sqlite3_vtab_config](db,SQLITE_VTAB_DIRECTONLY) from within the
9620
-** the [xConnect] or [xCreate] methods of a [virtual table] implmentation
9803
+** the [xConnect] or [xCreate] methods of a [virtual table] implementation
9804
** prohibits that virtual table from being used from within triggers and
9805
** views.
9806
** </dd>
@@ -9807,7 +9990,7 @@ SQLITE_API int sqlite3_vtab_distinct(sqlite3_index_info*);
9990
** communicated to the xBestIndex method as a
9991
** [SQLITE_INDEX_CONSTRAINT_EQ] constraint.)^ If xBestIndex wants to use
9992
** this constraint, it must set the corresponding
9810
-** aConstraintUsage[].argvIndex to a postive integer. ^(Then, under
9993
+** aConstraintUsage[].argvIndex to a positive integer. ^(Then, under
9994
** the usual mode of handling IN operators, SQLite generates [bytecode]
9995
** that invokes the [xFilter|xFilter() method] once for each value
9996
** on the right-hand side of the IN operator.)^ Thus the virtual table
@@ -10236,7 +10419,7 @@ SQLITE_API int sqlite3_db_cacheflush(sqlite3*);
10419
** When the [sqlite3_blob_write()] API is used to update a blob column,
10420
** the pre-update hook is invoked with SQLITE_DELETE. This is because the
10421
** in this case the new values are not available. In this case, when a
10239
-** callback made with op==SQLITE_DELETE is actuall a write using the
10422
+** callback made with op==SQLITE_DELETE is actually a write using the
10423
** sqlite3_blob_write() API, the [sqlite3_preupdate_blobwrite()] returns
10424
** the index of the column being written. In other cases, where the
10425
** pre-update hook is being invoked for some other reason, including a
@@ -10497,6 +10680,13 @@ SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_snapshot_recover(sqlite3 *db, const c
10680
** SQLITE_SERIALIZE_NOCOPY bit is set but no contiguous copy
10681
** of the database exists.
10682
**
10683
+** After the call, if the SQLITE_SERIALIZE_NOCOPY bit had been set,
10684
+** the returned buffer content will remain accessible and unchanged
10685
+** until either the next write operation on the connection or when
10686
+** the connection is closed, and applications must not modify the
10687
+** buffer. If the bit had been clear, the returned buffer will not
10688
+** be accessed by SQLite after the call.
10689
+**
10690
** A call to sqlite3_serialize(D,S,P,F) might return NULL even if the
10691
** SQLITE_SERIALIZE_NOCOPY bit is omitted from argument F if a memory
10692
** allocation error occurs.
@@ -10545,6 +10735,9 @@ SQLITE_API unsigned char *sqlite3_serialize(
10735
** SQLite will try to increase the buffer size using sqlite3_realloc64()
10736
** if writes on the database cause it to grow larger than M bytes.
10737
**
10738
+** Applications must not modify the buffer P or invalidate it before
10739
+** the database connection D is closed.
10740
+**
10741
** The sqlite3_deserialize() interface will fail with SQLITE_BUSY if the
10742
** database is currently in a read transaction or is involved in a backup
10743
** operation.
@@ -10553,6 +10746,13 @@ SQLITE_API unsigned char *sqlite3_serialize(
10746
** S argument to sqlite3_deserialize(D,S,P,N,M,F) is "temp" then the
10747
** function returns SQLITE_ERROR.
10748
**
10749
+** The deserialized database should not be in [WAL mode]. If the database
10750
+** is in WAL mode, then any attempt to use the database file will result
10751
+** in an [SQLITE_CANTOPEN] error. The application can set the
10752
+** [file format version numbers] (bytes 18 and 19) of the input database P
10753
+** to 0x01 prior to invoking sqlite3_deserialize(D,S,P,N,M,F) to force the
10754
+** database file into rollback mode and work around this limitation.
10755
+**
10756
** If sqlite3_deserialize(D,S,P,N,M,F) fails for any reason and if the
10757
** SQLITE_DESERIALIZE_FREEONCLOSE bit is set in argument F, then
10758
** [sqlite3_free()] is invoked on argument P prior to returning.
@@ -11625,6 +11825,18 @@ SQLITE_API int sqlite3changeset_concat(
11825
);
11826
11827
11828
+/*
11829
+** CAPI3REF: Upgrade the Schema of a Changeset/Patchset
11830
+*/
11831
+SQLITE_API int sqlite3changeset_upgrade(
11832
+ sqlite3 *db,
11833
+ const char *zDb,
11834
+ int nIn, const void *pIn, /* Input changeset */
11835
+ int *pnOut, void **ppOut /* OUT: Inverse of input */
11836
+);
11837
+
11838
+
11839
+
11840
/*
11841
** CAPI3REF: Changegroup Handle
11842
**
@@ -11671,6 +11883,38 @@ typedef struct sqlite3_changegroup sqlite3_changegroup;
11883
*/
11884
SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11885
11886
+/*
11887
+** CAPI3REF: Add a Schema to a Changegroup
11888
+** METHOD: sqlite3_changegroup_schema
11889
+**
11890
+** This method may be used to optionally enforce the rule that the changesets
11891
+** added to the changegroup handle must match the schema of database zDb
11892
+** ("main", "temp", or the name of an attached database). If
11893
+** sqlite3changegroup_add() is called to add a changeset that is not compatible
11894
+** with the configured schema, SQLITE_SCHEMA is returned and the changegroup
11895
+** object is left in an undefined state.
11896
+**
11897
+** A changeset schema is considered compatible with the database schema in
11898
+** the same way as for sqlite3changeset_apply(). Specifically, for each
11899
+** table in the changeset, there exists a database table with:
11900
+**
11901
+** <ul>
11902
+** <li> The name identified by the changeset, and
11903
+** <li> at least as many columns as recorded in the changeset, and
11904
+** <li> the primary key columns in the same position as recorded in
11905
+** the changeset.
11906
+** </ul>
11907
+**
11908
+** The output of the changegroup object always has the same schema as the
11909
+** database nominated using this function. In cases where changesets passed
11910
+** to sqlite3changegroup_add() have fewer columns than the corresponding table
11911
+** in the database schema, these are filled in using the default column
11912
+** values from the database schema. This makes it possible to combined
11913
+** changesets that have different numbers of columns for a single table
11914
+** within a changegroup, provided that they are otherwise compatible.
11915
+*/
11916
+SQLITE_API int sqlite3changegroup_schema(sqlite3_changegroup*, sqlite3*, const char *zDb);
11917
+
11918
/*
11919
** CAPI3REF: Add A Changeset To A Changegroup
11920
** METHOD: sqlite3_changegroup
@@ -11739,13 +11983,18 @@ SQLITE_API int sqlite3changegroup_new(sqlite3_changegroup **pp);
11983
** If the new changeset contains changes to a table that is already present
11984
** in the changegroup, then the number of columns and the position of the
11985
** primary key columns for the table must be consistent. If this is not the
11742
-** case, this function fails with SQLITE_SCHEMA. If the input changeset
11743
-** appears to be corrupt and the corruption is detected, SQLITE_CORRUPT is
11744
-** returned. Or, if an out-of-memory condition occurs during processing, this
11745
-** function returns SQLITE_NOMEM. In all cases, if an error occurs the state
11746
-** of the final contents of the changegroup is undefined.
11986
+** case, this function fails with SQLITE_SCHEMA. Except, if the changegroup
11987
+** object has been configured with a database schema using the
11988
+** sqlite3changegroup_schema() API, then it is possible to combine changesets
11989
+** with different numbers of columns for a single table, provided that
11990
+** they are otherwise compatible.
11991
+**
11992
+** If the input changeset appears to be corrupt and the corruption is
11993
+** detected, SQLITE_CORRUPT is returned. Or, if an out-of-memory condition
11994
+** occurs during processing, this function returns SQLITE_NOMEM.
11995
**
11748
-** If no error occurs, SQLITE_OK is returned.
11996
+** In all cases, if an error occurs the state of the final contents of the
11997
+** changegroup is undefined. If no error occurs, SQLITE_OK is returned.
11998
*/
11999
SQLITE_API int sqlite3changegroup_add(sqlite3_changegroup*, int nData, void *pData);
12000
@@ -12010,10 +12259,17 @@ SQLITE_API int sqlite3changeset_apply_v2(
12259
** <li>an insert change if all fields of the conflicting row match
12260
** the row being inserted.
12261
** </ul>
12262
+**
12263
+** <dt>SQLITE_CHANGESETAPPLY_FKNOACTION <dd>
12264
+** If this flag it set, then all foreign key constraints in the target
12265
+** database behave as if they were declared with "ON UPDATE NO ACTION ON
12266
+** DELETE NO ACTION", even if they are actually CASCADE, RESTRICT, SET NULL
12267
+** or SET DEFAULT.
12268
*/
12269
#define SQLITE_CHANGESETAPPLY_NOSAVEPOINT 0x0001
12270
#define SQLITE_CHANGESETAPPLY_INVERT 0x0002
12271
#define SQLITE_CHANGESETAPPLY_IGNORENOOP 0x0004
12272
+#define SQLITE_CHANGESETAPPLY_FKNOACTION 0x0008
12273
12274
/*
12275
** CAPI3REF: Constants Passed To The Conflict Handler
@@ -12579,8 +12835,11 @@ struct Fts5PhraseIter {
12835
** created with the "columnsize=0" option.
12836
**
12837
** xColumnText:
12582
-** This function attempts to retrieve the text of column iCol of the
12583
-** current document. If successful, (*pz) is set to point to a buffer
12838
+** If parameter iCol is less than zero, or greater than or equal to the
12839
+** number of columns in the table, SQLITE_RANGE is returned.
12840
+**
12841
+** Otherwise, this function attempts to retrieve the text of column iCol of
12842
+** the current document. If successful, (*pz) is set to point to a buffer
12843
** containing the text in utf-8 encoding, (*pn) is set to the size in bytes
12844
** (not characters) of the buffer and SQLITE_OK is returned. Otherwise,
12845
** if an error occurs, an SQLite error code is returned and the final values
@@ -12590,8 +12849,10 @@ struct Fts5PhraseIter {
12849
** Returns the number of phrases in the current query expression.
12850
**
12851
** xPhraseSize:
12593
-** Returns the number of tokens in phrase iPhrase of the query. Phrases
12594
-** are numbered starting from zero.
12852
+** If parameter iCol is less than zero, or greater than or equal to the
12853
+** number of phrases in the current query, as returned by xPhraseCount,
12854
+** 0 is returned. Otherwise, this function returns the number of tokens in
12855
+** phrase iPhrase of the query. Phrases are numbered starting from zero.
12856
**
12857
** xInstCount:
12858
** Set *pnInst to the total number of occurrences of all phrases within
@@ -12607,12 +12868,13 @@ struct Fts5PhraseIter {
12868
** Query for the details of phrase match iIdx within the current row.
12869
** Phrase matches are numbered starting from zero, so the iIdx argument
12870
** should be greater than or equal to zero and smaller than the value
12610
-** output by xInstCount().
12871
+** output by xInstCount(). If iIdx is less than zero or greater than
12872
+** or equal to the value returned by xInstCount(), SQLITE_RANGE is returned.
12873
**
12612
-** Usually, output parameter *piPhrase is set to the phrase number, *piCol
12874
+** Otherwise, output parameter *piPhrase is set to the phrase number, *piCol
12875
** to the column in which it occurs and *piOff the token offset of the
12614
-** first token of the phrase. Returns SQLITE_OK if successful, or an error
12615
-** code (i.e. SQLITE_NOMEM) if an error occurs.
12876
+** first token of the phrase. SQLITE_OK is returned if successful, or an
12877
+** error code (i.e. SQLITE_NOMEM) if an error occurs.
12878
**
12879
** This API can be quite slow if used with an FTS5 table created with the
12880
** "detail=none" or "detail=column" option.
@@ -12638,6 +12900,10 @@ struct Fts5PhraseIter {
12900
** Invoking Api.xUserData() returns a copy of the pointer passed as
12901
** the third argument to pUserData.
12902
**
12903
+** If parameter iPhrase is less than zero, or greater than or equal to
12904
+** the number of phrases in the query, as returned by xPhraseCount(),
12905
+** this function returns SQLITE_RANGE.
12906
+**
12907
** If the callback function returns any value other than SQLITE_OK, the
12908
** query is abandoned and the xQueryPhrase function returns immediately.
12909
** If the returned value is SQLITE_DONE, xQueryPhrase returns SQLITE_OK.
@@ -12752,6 +13018,39 @@ struct Fts5PhraseIter {
13018
**
13019
** xPhraseNextColumn()
13020
** See xPhraseFirstColumn above.
13021
+**
13022
+** xQueryToken(pFts5, iPhrase, iToken, ppToken, pnToken)
13023
+** This is used to access token iToken of phrase iPhrase of the current
13024
+** query. Before returning, output parameter *ppToken is set to point
13025
+** to a buffer containing the requested token, and *pnToken to the
13026
+** size of this buffer in bytes.
13027
+**
13028
+** If iPhrase or iToken are less than zero, or if iPhrase is greater than
13029
+** or equal to the number of phrases in the query as reported by
13030
+** xPhraseCount(), or if iToken is equal to or greater than the number of
13031
+** tokens in the phrase, SQLITE_RANGE is returned and *ppToken and *pnToken
13032
+ are both zeroed.
13033
+**
13034
+** The output text is not a copy of the query text that specified the
13035
+** token. It is the output of the tokenizer module. For tokendata=1
13036
+** tables, this includes any embedded 0x00 and trailing data.
13037
+**
13038
+** xInstToken(pFts5, iIdx, iToken, ppToken, pnToken)
13039
+** This is used to access token iToken of phrase hit iIdx within the
13040
+** current row. If iIdx is less than zero or greater than or equal to the
13041
+** value returned by xInstCount(), SQLITE_RANGE is returned. Otherwise,
13042
+** output variable (*ppToken) is set to point to a buffer containing the
13043
+** matching document token, and (*pnToken) to the size of that buffer in
13044
+** bytes. This API is not available if the specified token matches a
13045
+** prefix query term. In that case both output variables are always set
13046
+** to 0.
13047
+**
13048
+** The output text is not a copy of the document text that was tokenized.
13049
+** It is the output of the tokenizer module. For tokendata=1 tables, this
13050
+** includes any embedded 0x00 and trailing data.
13051
+**
13052
+** This API can be quite slow if used with an FTS5 table created with the
13053
+** "detail=none" or "detail=column" option.
13054
*/
13055
struct Fts5ExtensionApi {
13056
int iVersion; /* Currently always set to 3 */
@@ -12789,6 +13088,13 @@ struct Fts5ExtensionApi {
13088
13089
int (*xPhraseFirstColumn)(Fts5Context*, int iPhrase, Fts5PhraseIter*, int*);
13090
void (*xPhraseNextColumn)(Fts5Context*, Fts5PhraseIter*, int *piCol);
13091
+
13092
+ /* Below this point are iVersion>=3 only */
13093
+ int (*xQueryToken)(Fts5Context*,
13094
+ int iPhrase, int iToken,
13095
+ const char **ppToken, int *pnToken
13096
+ );
13097
+ int (*xInstToken)(Fts5Context*, int iIdx, int iToken, const char**, int*);
13098
};
13099
13100
/*
@@ -12983,8 +13289,8 @@ struct Fts5ExtensionApi {
13289
** as separate queries of the FTS index are required for each synonym.
13290
**
13291
** When using methods (2) or (3), it is important that the tokenizer only
12986
-** provide synonyms when tokenizing document text (method (2)) or query
12987
-** text (method (3)), not both. Doing so will not cause any errors, but is
13292
+** provide synonyms when tokenizing document text (method (3)) or query
13293
+** text (method (2)), not both. Doing so will not cause any errors, but is
13294
** inefficient.
13295
*/
13296
typedef struct Fts5Tokenizer Fts5Tokenizer;
@@ -13032,7 +13338,7 @@ struct fts5_api {
13338
int (*xCreateTokenizer)(
13339
fts5_api *pApi,
13340
const char *zName,
13035
- void *pContext,
13341
+ void *pUserData,
13342
fts5_tokenizer *pTokenizer,
13343
void (*xDestroy)(void*)
13344
);
@@ -13041,7 +13347,7 @@ struct fts5_api {
13347
int (*xFindTokenizer)(
13348
fts5_api *pApi,
13349
const char *zName,
13044
- void **ppContext,
13350
+ void **ppUserData,
13351
fts5_tokenizer *pTokenizer
13352
);
13353
@@ -13049,7 +13355,7 @@ struct fts5_api {
13355
int (*xCreateFunction)(
13356
fts5_api *pApi,
13357
const char *zName,
13052
- void *pContext,
13358
+ void *pUserData,
13359
fts5_extension_function xFunction,
13360
void (*xDestroy)(void*)
13361
);
src/database/sqlite/sqlite3recover.c
+1
-1
@@ -1190,7 +1190,7 @@ static int recoverWriteSchema1(sqlite3_recover *p){
1190
if( bTable && !bVirtual ){
1191
if( SQLITE_ROW==sqlite3_step(pTblname) ){
1192
const char *zTbl = (const char*)sqlite3_column_text(pTblname, 0);
1193
- recoverAddTable(p, zTbl, iRoot);
1193
+ if( zTbl ) recoverAddTable(p, zTbl, iRoot);
1194
}
1195
recoverReset(p, pTblname);
1196
}